blob: cf3fbae79de83993ae4ecc81e9365d8d3bbc4c28 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Keerthy17b3fe72016-09-15 17:04:06 +05302/*
3 * (C) Copyright 2016 Texas Instruments Incorporated, <www.ti.com>
4 * Keerthy <j-keerthy@ti.com>
Keerthy17b3fe72016-09-15 17:04:06 +05305 */
6
Sven Schwermerdb58d9e2019-06-24 13:03:34 +02007#include "regulator_common.h"
Keerthy17b3fe72016-09-15 17:04:06 +05308#include <common.h>
9#include <fdtdec.h>
10#include <errno.h>
11#include <dm.h>
12#include <i2c.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Keerthy17b3fe72016-09-15 17:04:06 +053014#include <asm/gpio.h>
15#include <power/pmic.h>
16#include <power/regulator.h>
17
18#define GPIO_REGULATOR_MAX_STATES 2
19
20DECLARE_GLOBAL_DATA_PTR;
21
22struct gpio_regulator_platdata {
Sven Schwermerdb58d9e2019-06-24 13:03:34 +020023 struct regulator_common_platdata common;
Keerthy17b3fe72016-09-15 17:04:06 +053024 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
29static 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;
Simon Glassdd79d6e2017-01-17 16:52:55 -070035 int node = dev_of_offset(dev);
Keerthy17b3fe72016-09-15 17:04:06 +053036 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
Sven Schwermerdb58d9e2019-06-24 13:03:34 +020071 return regulator_common_ofdata_to_platdata(dev, &dev_pdata->common, "enable-gpios");
Keerthy17b3fe72016-09-15 17:04:06 +053072}
73
74static 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
96static 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) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900114 pr_err("Can't set regulator : %s gpio to: %d\n", dev->name,
Keerthy17b3fe72016-09-15 17:04:06 +0530115 enable);
116 return ret;
117 }
118
119 return 0;
120}
121
Sven Schwermerdb58d9e2019-06-24 13:03:34 +0200122static 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
128static 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
Keerthy17b3fe72016-09-15 17:04:06 +0530134static const struct dm_regulator_ops gpio_regulator_ops = {
135 .get_value = gpio_regulator_get_value,
136 .set_value = gpio_regulator_set_value,
Sven Schwermerdb58d9e2019-06-24 13:03:34 +0200137 .get_enable = gpio_regulator_get_enable,
138 .set_enable = gpio_regulator_set_enable,
Keerthy17b3fe72016-09-15 17:04:06 +0530139};
140
141static const struct udevice_id gpio_regulator_ids[] = {
142 { .compatible = "regulator-gpio" },
143 { },
144};
145
146U_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};