Merge tag 'efi-2020-07-rc6' of https://gitlab.denx.de/u-boot/custodians/u-boot-efi
[oweals/u-boot.git] / drivers / power / regulator / gpio-regulator.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2016 Texas Instruments Incorporated, <www.ti.com>
4  * Keerthy <j-keerthy@ti.com>
5  */
6
7 #include "regulator_common.h"
8 #include <common.h>
9 #include <fdtdec.h>
10 #include <errno.h>
11 #include <dm.h>
12 #include <i2c.h>
13 #include <log.h>
14 #include <asm/gpio.h>
15 #include <power/pmic.h>
16 #include <power/regulator.h>
17
18 #define GPIO_REGULATOR_MAX_STATES       2
19
20 DECLARE_GLOBAL_DATA_PTR;
21
22 struct gpio_regulator_platdata {
23         struct regulator_common_platdata common;
24         struct gpio_desc gpio; /* GPIO for regulator voltage control */
25         int states[GPIO_REGULATOR_MAX_STATES];
26         int voltages[GPIO_REGULATOR_MAX_STATES];
27 };
28
29 static int gpio_regulator_ofdata_to_platdata(struct udevice *dev)
30 {
31         struct dm_regulator_uclass_platdata *uc_pdata;
32         struct gpio_regulator_platdata *dev_pdata;
33         struct gpio_desc *gpio;
34         const void *blob = gd->fdt_blob;
35         int node = dev_of_offset(dev);
36         int ret, count, i, j;
37         u32 states_array[8];
38
39         dev_pdata = dev_get_platdata(dev);
40         uc_pdata = dev_get_uclass_platdata(dev);
41         if (!uc_pdata)
42                 return -ENXIO;
43
44         /* Set type to gpio */
45         uc_pdata->type = REGULATOR_TYPE_GPIO;
46
47         /*
48          * Get gpio regulator gpio desc
49          * Assuming one GPIO per regulator.
50          * Can be extended later to multiple GPIOs
51          * per gpio-regulator. As of now no instance with multiple
52          * gpios is presnt
53          */
54         gpio = &dev_pdata->gpio;
55         ret = gpio_request_by_name(dev, "gpios", 0, gpio, GPIOD_IS_OUT);
56         if (ret)
57                 debug("regulator gpio - not found! Error: %d", ret);
58
59         count = fdtdec_get_int_array_count(blob, node, "states",
60                                            states_array, 8);
61
62         if (!count)
63                 return -EINVAL;
64
65         for (i = 0, j = 0; i < count; i += 2) {
66                 dev_pdata->voltages[j] = states_array[i];
67                 dev_pdata->states[j] = states_array[i + 1];
68                 j++;
69         }
70
71         return regulator_common_ofdata_to_platdata(dev, &dev_pdata->common, "enable-gpios");
72 }
73
74 static int gpio_regulator_get_value(struct udevice *dev)
75 {
76         struct dm_regulator_uclass_platdata *uc_pdata;
77         struct gpio_regulator_platdata *dev_pdata = dev_get_platdata(dev);
78         int enable;
79
80         if (!dev_pdata->gpio.dev)
81                 return -ENOSYS;
82
83         uc_pdata = dev_get_uclass_platdata(dev);
84         if (uc_pdata->min_uV > uc_pdata->max_uV) {
85                 debug("Invalid constraints for: %s\n", uc_pdata->name);
86                 return -EINVAL;
87         }
88
89         enable = dm_gpio_get_value(&dev_pdata->gpio);
90         if (enable == dev_pdata->states[0])
91                 return dev_pdata->voltages[0];
92         else
93                 return dev_pdata->voltages[1];
94 }
95
96 static int gpio_regulator_set_value(struct udevice *dev, int uV)
97 {
98         struct gpio_regulator_platdata *dev_pdata = dev_get_platdata(dev);
99         int ret;
100         bool enable;
101
102         if (!dev_pdata->gpio.dev)
103                 return -ENOSYS;
104
105         if (uV == dev_pdata->voltages[0])
106                 enable = dev_pdata->states[0];
107         else if (uV == dev_pdata->voltages[1])
108                 enable = dev_pdata->states[1];
109         else
110                 return -EINVAL;
111
112         ret = dm_gpio_set_value(&dev_pdata->gpio, enable);
113         if (ret) {
114                 pr_err("Can't set regulator : %s gpio to: %d\n", dev->name,
115                       enable);
116                 return ret;
117         }
118
119         return 0;
120 }
121
122 static int gpio_regulator_get_enable(struct udevice *dev)
123 {
124         struct gpio_regulator_platdata *dev_pdata = dev_get_platdata(dev);
125         return regulator_common_get_enable(dev, &dev_pdata->common);
126 }
127
128 static int gpio_regulator_set_enable(struct udevice *dev, bool enable)
129 {
130         struct gpio_regulator_platdata *dev_pdata = dev_get_platdata(dev);
131         return regulator_common_set_enable(dev, &dev_pdata->common, enable);
132 }
133
134 static const struct dm_regulator_ops gpio_regulator_ops = {
135         .get_value      = gpio_regulator_get_value,
136         .set_value      = gpio_regulator_set_value,
137         .get_enable     = gpio_regulator_get_enable,
138         .set_enable     = gpio_regulator_set_enable,
139 };
140
141 static const struct udevice_id gpio_regulator_ids[] = {
142         { .compatible = "regulator-gpio" },
143         { },
144 };
145
146 U_BOOT_DRIVER(gpio_regulator) = {
147         .name = "gpio regulator",
148         .id = UCLASS_REGULATOR,
149         .ops = &gpio_regulator_ops,
150         .of_match = gpio_regulator_ids,
151         .ofdata_to_platdata = gpio_regulator_ofdata_to_platdata,
152         .platdata_auto_alloc_size = sizeof(struct gpio_regulator_platdata),
153 };