1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2016 Texas Instruments Incorporated, <www.ti.com>
4 * Keerthy <j-keerthy@ti.com>
7 #include "regulator_common.h"
15 #include <power/pmic.h>
16 #include <power/regulator.h>
18 #define GPIO_REGULATOR_MAX_STATES 2
20 DECLARE_GLOBAL_DATA_PTR;
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];
29 static int gpio_regulator_ofdata_to_platdata(struct udevice *dev)
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);
39 dev_pdata = dev_get_platdata(dev);
40 uc_pdata = dev_get_uclass_platdata(dev);
44 /* Set type to gpio */
45 uc_pdata->type = REGULATOR_TYPE_GPIO;
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
54 gpio = &dev_pdata->gpio;
55 ret = gpio_request_by_name(dev, "gpios", 0, gpio, GPIOD_IS_OUT);
57 debug("regulator gpio - not found! Error: %d", ret);
59 count = fdtdec_get_int_array_count(blob, node, "states",
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];
71 return regulator_common_ofdata_to_platdata(dev, &dev_pdata->common, "enable-gpios");
74 static int gpio_regulator_get_value(struct udevice *dev)
76 struct dm_regulator_uclass_platdata *uc_pdata;
77 struct gpio_regulator_platdata *dev_pdata = dev_get_platdata(dev);
80 if (!dev_pdata->gpio.dev)
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);
89 enable = dm_gpio_get_value(&dev_pdata->gpio);
90 if (enable == dev_pdata->states[0])
91 return dev_pdata->voltages[0];
93 return dev_pdata->voltages[1];
96 static int gpio_regulator_set_value(struct udevice *dev, int uV)
98 struct gpio_regulator_platdata *dev_pdata = dev_get_platdata(dev);
102 if (!dev_pdata->gpio.dev)
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];
112 ret = dm_gpio_set_value(&dev_pdata->gpio, enable);
114 pr_err("Can't set regulator : %s gpio to: %d\n", dev->name,
122 static int gpio_regulator_get_enable(struct udevice *dev)
124 struct gpio_regulator_platdata *dev_pdata = dev_get_platdata(dev);
125 return regulator_common_get_enable(dev, &dev_pdata->common);
128 static int gpio_regulator_set_enable(struct udevice *dev, bool enable)
130 struct gpio_regulator_platdata *dev_pdata = dev_get_platdata(dev);
131 return regulator_common_set_enable(dev, &dev_pdata->common, enable);
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,
141 static const struct udevice_id gpio_regulator_ids[] = {
142 { .compatible = "regulator-gpio" },
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),