blob: 947f812d099fe4eb31a4a04dab4c6f2c496d2aaa [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
7#include <common.h>
8#include <fdtdec.h>
9#include <errno.h>
10#include <dm.h>
11#include <i2c.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Keerthy17b3fe72016-09-15 17:04:06 +053013#include <asm/gpio.h>
14#include <power/pmic.h>
15#include <power/regulator.h>
16
Simon Glass1c1ddf62020-07-19 10:15:44 -060017#include "regulator_common.h"
18
Keerthy17b3fe72016-09-15 17:04:06 +053019#define GPIO_REGULATOR_MAX_STATES 2
20
21DECLARE_GLOBAL_DATA_PTR;
22
23struct gpio_regulator_platdata {
Sven Schwermerdb58d9e2019-06-24 13:03:34 +020024 struct regulator_common_platdata common;
Keerthy17b3fe72016-09-15 17:04:06 +053025 struct gpio_desc gpio; /* GPIO for regulator voltage control */
26 int states[GPIO_REGULATOR_MAX_STATES];
27 int voltages[GPIO_REGULATOR_MAX_STATES];
28};
29
30static int gpio_regulator_ofdata_to_platdata(struct udevice *dev)
31{
32 struct dm_regulator_uclass_platdata *uc_pdata;
33 struct gpio_regulator_platdata *dev_pdata;
34 struct gpio_desc *gpio;
35 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -070036 int node = dev_of_offset(dev);
Keerthy17b3fe72016-09-15 17:04:06 +053037 int ret, count, i, j;
38 u32 states_array[8];
39
40 dev_pdata = dev_get_platdata(dev);
41 uc_pdata = dev_get_uclass_platdata(dev);
42 if (!uc_pdata)
43 return -ENXIO;
44
45 /* Set type to gpio */
46 uc_pdata->type = REGULATOR_TYPE_GPIO;
47
48 /*
49 * Get gpio regulator gpio desc
50 * Assuming one GPIO per regulator.
51 * Can be extended later to multiple GPIOs
52 * per gpio-regulator. As of now no instance with multiple
53 * gpios is presnt
54 */
55 gpio = &dev_pdata->gpio;
56 ret = gpio_request_by_name(dev, "gpios", 0, gpio, GPIOD_IS_OUT);
57 if (ret)
58 debug("regulator gpio - not found! Error: %d", ret);
59
60 count = fdtdec_get_int_array_count(blob, node, "states",
61 states_array, 8);
62
63 if (!count)
64 return -EINVAL;
65
66 for (i = 0, j = 0; i < count; i += 2) {
67 dev_pdata->voltages[j] = states_array[i];
68 dev_pdata->states[j] = states_array[i + 1];
69 j++;
70 }
71
Sven Schwermerdb58d9e2019-06-24 13:03:34 +020072 return regulator_common_ofdata_to_platdata(dev, &dev_pdata->common, "enable-gpios");
Keerthy17b3fe72016-09-15 17:04:06 +053073}
74
75static int gpio_regulator_get_value(struct udevice *dev)
76{
77 struct dm_regulator_uclass_platdata *uc_pdata;
78 struct gpio_regulator_platdata *dev_pdata = dev_get_platdata(dev);
79 int enable;
80
81 if (!dev_pdata->gpio.dev)
82 return -ENOSYS;
83
84 uc_pdata = dev_get_uclass_platdata(dev);
85 if (uc_pdata->min_uV > uc_pdata->max_uV) {
86 debug("Invalid constraints for: %s\n", uc_pdata->name);
87 return -EINVAL;
88 }
89
90 enable = dm_gpio_get_value(&dev_pdata->gpio);
91 if (enable == dev_pdata->states[0])
92 return dev_pdata->voltages[0];
93 else
94 return dev_pdata->voltages[1];
95}
96
97static int gpio_regulator_set_value(struct udevice *dev, int uV)
98{
99 struct gpio_regulator_platdata *dev_pdata = dev_get_platdata(dev);
100 int ret;
101 bool enable;
102
103 if (!dev_pdata->gpio.dev)
104 return -ENOSYS;
105
106 if (uV == dev_pdata->voltages[0])
107 enable = dev_pdata->states[0];
108 else if (uV == dev_pdata->voltages[1])
109 enable = dev_pdata->states[1];
110 else
111 return -EINVAL;
112
113 ret = dm_gpio_set_value(&dev_pdata->gpio, enable);
114 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900115 pr_err("Can't set regulator : %s gpio to: %d\n", dev->name,
Keerthy17b3fe72016-09-15 17:04:06 +0530116 enable);
117 return ret;
118 }
119
120 return 0;
121}
122
Sven Schwermerdb58d9e2019-06-24 13:03:34 +0200123static int gpio_regulator_get_enable(struct udevice *dev)
124{
125 struct gpio_regulator_platdata *dev_pdata = dev_get_platdata(dev);
126 return regulator_common_get_enable(dev, &dev_pdata->common);
127}
128
129static int gpio_regulator_set_enable(struct udevice *dev, bool enable)
130{
131 struct gpio_regulator_platdata *dev_pdata = dev_get_platdata(dev);
132 return regulator_common_set_enable(dev, &dev_pdata->common, enable);
133}
134
Keerthy17b3fe72016-09-15 17:04:06 +0530135static const struct dm_regulator_ops gpio_regulator_ops = {
136 .get_value = gpio_regulator_get_value,
137 .set_value = gpio_regulator_set_value,
Sven Schwermerdb58d9e2019-06-24 13:03:34 +0200138 .get_enable = gpio_regulator_get_enable,
139 .set_enable = gpio_regulator_set_enable,
Keerthy17b3fe72016-09-15 17:04:06 +0530140};
141
142static const struct udevice_id gpio_regulator_ids[] = {
143 { .compatible = "regulator-gpio" },
144 { },
145};
146
147U_BOOT_DRIVER(gpio_regulator) = {
148 .name = "gpio regulator",
149 .id = UCLASS_REGULATOR,
150 .ops = &gpio_regulator_ops,
151 .of_match = gpio_regulator_ids,
152 .ofdata_to_platdata = gpio_regulator_ofdata_to_platdata,
153 .platdata_auto_alloc_size = sizeof(struct gpio_regulator_platdata),
154};