common: Drop linux/delay.h from common header
[oweals/u-boot.git] / drivers / power / regulator / regulator_common.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2019 Disruptive Technologies Research AS
4  * Sven Schwermer <sven.svenschwermer@disruptive-technologies.com>
5  */
6
7 #include "regulator_common.h"
8 #include <common.h>
9 #include <log.h>
10 #include <linux/delay.h>
11 #include <power/regulator.h>
12
13 int regulator_common_ofdata_to_platdata(struct udevice *dev,
14         struct regulator_common_platdata *dev_pdata, const char *enable_gpio_name)
15 {
16         struct gpio_desc *gpio;
17         int flags = GPIOD_IS_OUT;
18         int ret;
19
20         if (!dev_read_bool(dev, "enable-active-high"))
21                 flags |= GPIOD_ACTIVE_LOW;
22
23         /* Get optional enable GPIO desc */
24         gpio = &dev_pdata->gpio;
25         ret = gpio_request_by_name(dev, enable_gpio_name, 0, gpio, flags);
26         if (ret) {
27                 debug("Regulator '%s' optional enable GPIO - not found! Error: %d\n",
28                       dev->name, ret);
29                 if (ret != -ENOENT)
30                         return ret;
31         }
32
33         /* Get optional ramp up delay */
34         dev_pdata->startup_delay_us = dev_read_u32_default(dev,
35                                                         "startup-delay-us", 0);
36         dev_pdata->off_on_delay_us =
37                 dev_read_u32_default(dev, "off-on-delay-us", 0);
38         if (!dev_pdata->off_on_delay_us) {
39                 dev_pdata->off_on_delay_us =
40                         dev_read_u32_default(dev, "u-boot,off-on-delay-us", 0);
41         }
42
43         return 0;
44 }
45
46 int regulator_common_get_enable(const struct udevice *dev,
47         struct regulator_common_platdata *dev_pdata)
48 {
49         /* Enable GPIO is optional */
50         if (!dev_pdata->gpio.dev)
51                 return true;
52
53         return dm_gpio_get_value(&dev_pdata->gpio);
54 }
55
56 int regulator_common_set_enable(const struct udevice *dev,
57         struct regulator_common_platdata *dev_pdata, bool enable)
58 {
59         int ret;
60
61         debug("%s: dev='%s', enable=%d, delay=%d, has_gpio=%d\n", __func__,
62               dev->name, enable, dev_pdata->startup_delay_us,
63               dm_gpio_is_valid(&dev_pdata->gpio));
64         /* Enable GPIO is optional */
65         if (!dm_gpio_is_valid(&dev_pdata->gpio)) {
66                 if (!enable)
67                         return -ENOSYS;
68                 return 0;
69         }
70
71         ret = dm_gpio_set_value(&dev_pdata->gpio, enable);
72         if (ret) {
73                 pr_err("Can't set regulator : %s gpio to: %d\n", dev->name,
74                       enable);
75                 return ret;
76         }
77
78         if (enable && dev_pdata->startup_delay_us)
79                 udelay(dev_pdata->startup_delay_us);
80         debug("%s: done\n", __func__);
81
82         if (!enable && dev_pdata->off_on_delay_us)
83                 udelay(dev_pdata->off_on_delay_us);
84
85         return 0;
86 }