]> git.ipfire.org Git - thirdparty/u-boot.git/blob - drivers/power/regulator/regulator_common.c
e26f5ebec34700dbecda028d28778fe25d1d13a6
[thirdparty/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 <common.h>
8 #include <dm.h>
9 #include <log.h>
10 #include <linux/delay.h>
11 #include <power/regulator.h>
12
13 #include "regulator_common.h"
14
15 int regulator_common_of_to_plat(struct udevice *dev,
16 struct regulator_common_plat *plat,
17 const char *enable_gpio_name)
18 {
19 struct gpio_desc *gpio;
20 int flags = GPIOD_IS_OUT;
21 int ret;
22
23 if (!dev_read_bool(dev, "enable-active-high"))
24 flags |= GPIOD_ACTIVE_LOW;
25 if (dev_read_bool(dev, "regulator-boot-on"))
26 flags |= GPIOD_IS_OUT_ACTIVE;
27
28 /* Get optional enable GPIO desc */
29 gpio = &plat->gpio;
30 ret = gpio_request_by_name(dev, enable_gpio_name, 0, gpio, flags);
31 if (ret) {
32 debug("Regulator '%s' optional enable GPIO - not found! Error: %d\n",
33 dev->name, ret);
34 if (ret != -ENOENT)
35 return ret;
36 }
37
38 /* Get optional ramp up delay */
39 plat->startup_delay_us = dev_read_u32_default(dev,
40 "startup-delay-us", 0);
41 plat->off_on_delay_us = dev_read_u32_default(dev, "off-on-delay-us", 0);
42 if (!plat->off_on_delay_us) {
43 plat->off_on_delay_us =
44 dev_read_u32_default(dev, "u-boot,off-on-delay-us", 0);
45 }
46
47 return 0;
48 }
49
50 int regulator_common_get_enable(const struct udevice *dev,
51 struct regulator_common_plat *plat)
52 {
53 /* Enable GPIO is optional */
54 if (!plat->gpio.dev)
55 return true;
56
57 return dm_gpio_get_value(&plat->gpio);
58 }
59
60 int regulator_common_set_enable(const struct udevice *dev,
61 struct regulator_common_plat *plat, bool enable)
62 {
63 int ret;
64
65 debug("%s: dev='%s', enable=%d, delay=%d, has_gpio=%d\n", __func__,
66 dev->name, enable, plat->startup_delay_us,
67 dm_gpio_is_valid(&plat->gpio));
68 /* Enable GPIO is optional */
69 if (!dm_gpio_is_valid(&plat->gpio)) {
70 if (!enable)
71 return -ENOSYS;
72 return 0;
73 }
74
75 /* If previously enabled, increase count */
76 if (enable && plat->enable_count > 0) {
77 plat->enable_count++;
78 return -EALREADY;
79 }
80
81 if (!enable) {
82 if (plat->enable_count > 1) {
83 /* If enabled multiple times, decrease count */
84 plat->enable_count--;
85 return -EBUSY;
86 } else if (!plat->enable_count) {
87 /* If already disabled, do nothing */
88 return -EALREADY;
89 }
90 }
91
92 ret = dm_gpio_set_value(&plat->gpio, enable);
93 if (ret) {
94 pr_err("Can't set regulator : %s gpio to: %d\n", dev->name,
95 enable);
96 return ret;
97 }
98
99 if (enable && plat->startup_delay_us)
100 udelay(plat->startup_delay_us);
101 debug("%s: done\n", __func__);
102
103 if (!enable && plat->off_on_delay_us)
104 udelay(plat->off_on_delay_us);
105
106 if (enable)
107 plat->enable_count++;
108 else
109 plat->enable_count--;
110
111 return 0;
112 }