blob: 9c0a68aa5af49c4f05ef4f4dd4dc93b74ec37100 [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>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Keerthy17b3fe72016-09-15 17:04:06 +053012#include <asm/gpio.h>
13#include <power/pmic.h>
14#include <power/regulator.h>
15
Simon Glass1c1ddf62020-07-19 10:15:44 -060016#include "regulator_common.h"
17
Keerthy17b3fe72016-09-15 17:04:06 +053018#define GPIO_REGULATOR_MAX_STATES 2
19
Simon Glassb75b15b2020-12-03 16:55:23 -070020struct gpio_regulator_plat {
21 struct regulator_common_plat common;
Keerthy17b3fe72016-09-15 17:04:06 +053022 struct gpio_desc gpio; /* GPIO for regulator voltage control */
23 int states[GPIO_REGULATOR_MAX_STATES];
24 int voltages[GPIO_REGULATOR_MAX_STATES];
25};
26
Simon Glassaad29ae2020-12-03 16:55:21 -070027static int gpio_regulator_of_to_plat(struct udevice *dev)
Keerthy17b3fe72016-09-15 17:04:06 +053028{
Simon Glass71fa5b42020-12-03 16:55:18 -070029 struct dm_regulator_uclass_plat *uc_pdata;
Simon Glassb75b15b2020-12-03 16:55:23 -070030 struct gpio_regulator_plat *dev_pdata;
Keerthy17b3fe72016-09-15 17:04:06 +053031 struct gpio_desc *gpio;
Keerthy17b3fe72016-09-15 17:04:06 +053032 int ret, count, i, j;
Patrick Delaunay23f988a2020-09-10 18:18:16 +020033 u32 states_array[GPIO_REGULATOR_MAX_STATES * 2];
Keerthy17b3fe72016-09-15 17:04:06 +053034
Simon Glassfa20e932020-12-03 16:55:20 -070035 dev_pdata = dev_get_plat(dev);
Simon Glass71fa5b42020-12-03 16:55:18 -070036 uc_pdata = dev_get_uclass_plat(dev);
Keerthy17b3fe72016-09-15 17:04:06 +053037 if (!uc_pdata)
38 return -ENXIO;
39
40 /* Set type to gpio */
41 uc_pdata->type = REGULATOR_TYPE_GPIO;
42
43 /*
44 * Get gpio regulator gpio desc
45 * Assuming one GPIO per regulator.
46 * Can be extended later to multiple GPIOs
47 * per gpio-regulator. As of now no instance with multiple
48 * gpios is presnt
49 */
50 gpio = &dev_pdata->gpio;
51 ret = gpio_request_by_name(dev, "gpios", 0, gpio, GPIOD_IS_OUT);
52 if (ret)
53 debug("regulator gpio - not found! Error: %d", ret);
54
Patrick Delaunaydeb5b482020-09-10 18:18:17 +020055 ret = dev_read_size(dev, "states");
56 if (ret < 0)
57 return ret;
58
59 count = ret / sizeof(states_array[0]);
60 if (count > ARRAY_SIZE(states_array)) {
61 debug("regulator gpio - to many states (%d > %d)",
62 count / 2, GPIO_REGULATOR_MAX_STATES);
63 count = ARRAY_SIZE(states_array);
64 }
Keerthy17b3fe72016-09-15 17:04:06 +053065
Patrick Delaunaydeb5b482020-09-10 18:18:17 +020066 ret = dev_read_u32_array(dev, "states", states_array, count);
67 if (ret < 0)
68 return ret;
Keerthy17b3fe72016-09-15 17:04:06 +053069
70 for (i = 0, j = 0; i < count; i += 2) {
71 dev_pdata->voltages[j] = states_array[i];
72 dev_pdata->states[j] = states_array[i + 1];
73 j++;
74 }
75
Simon Glassaad29ae2020-12-03 16:55:21 -070076 return regulator_common_of_to_plat(dev, &dev_pdata->common, "enable-gpios");
Keerthy17b3fe72016-09-15 17:04:06 +053077}
78
79static int gpio_regulator_get_value(struct udevice *dev)
80{
Simon Glass71fa5b42020-12-03 16:55:18 -070081 struct dm_regulator_uclass_plat *uc_pdata;
Simon Glassb75b15b2020-12-03 16:55:23 -070082 struct gpio_regulator_plat *dev_pdata = dev_get_plat(dev);
Keerthy17b3fe72016-09-15 17:04:06 +053083 int enable;
84
85 if (!dev_pdata->gpio.dev)
86 return -ENOSYS;
87
Simon Glass71fa5b42020-12-03 16:55:18 -070088 uc_pdata = dev_get_uclass_plat(dev);
Keerthy17b3fe72016-09-15 17:04:06 +053089 if (uc_pdata->min_uV > uc_pdata->max_uV) {
90 debug("Invalid constraints for: %s\n", uc_pdata->name);
91 return -EINVAL;
92 }
93
94 enable = dm_gpio_get_value(&dev_pdata->gpio);
95 if (enable == dev_pdata->states[0])
96 return dev_pdata->voltages[0];
97 else
98 return dev_pdata->voltages[1];
99}
100
101static int gpio_regulator_set_value(struct udevice *dev, int uV)
102{
Simon Glassb75b15b2020-12-03 16:55:23 -0700103 struct gpio_regulator_plat *dev_pdata = dev_get_plat(dev);
Keerthy17b3fe72016-09-15 17:04:06 +0530104 int ret;
105 bool enable;
106
107 if (!dev_pdata->gpio.dev)
108 return -ENOSYS;
109
110 if (uV == dev_pdata->voltages[0])
111 enable = dev_pdata->states[0];
112 else if (uV == dev_pdata->voltages[1])
113 enable = dev_pdata->states[1];
114 else
115 return -EINVAL;
116
117 ret = dm_gpio_set_value(&dev_pdata->gpio, enable);
118 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900119 pr_err("Can't set regulator : %s gpio to: %d\n", dev->name,
Keerthy17b3fe72016-09-15 17:04:06 +0530120 enable);
121 return ret;
122 }
123
124 return 0;
125}
126
Sven Schwermerdb58d9e2019-06-24 13:03:34 +0200127static int gpio_regulator_get_enable(struct udevice *dev)
128{
Simon Glassb75b15b2020-12-03 16:55:23 -0700129 struct gpio_regulator_plat *dev_pdata = dev_get_plat(dev);
Sven Schwermerdb58d9e2019-06-24 13:03:34 +0200130 return regulator_common_get_enable(dev, &dev_pdata->common);
131}
132
133static int gpio_regulator_set_enable(struct udevice *dev, bool enable)
134{
Simon Glassb75b15b2020-12-03 16:55:23 -0700135 struct gpio_regulator_plat *dev_pdata = dev_get_plat(dev);
Sven Schwermerdb58d9e2019-06-24 13:03:34 +0200136 return regulator_common_set_enable(dev, &dev_pdata->common, enable);
137}
138
Keerthy17b3fe72016-09-15 17:04:06 +0530139static const struct dm_regulator_ops gpio_regulator_ops = {
140 .get_value = gpio_regulator_get_value,
141 .set_value = gpio_regulator_set_value,
Sven Schwermerdb58d9e2019-06-24 13:03:34 +0200142 .get_enable = gpio_regulator_get_enable,
143 .set_enable = gpio_regulator_set_enable,
Keerthy17b3fe72016-09-15 17:04:06 +0530144};
145
146static const struct udevice_id gpio_regulator_ids[] = {
147 { .compatible = "regulator-gpio" },
148 { },
149};
150
151U_BOOT_DRIVER(gpio_regulator) = {
152 .name = "gpio regulator",
153 .id = UCLASS_REGULATOR,
154 .ops = &gpio_regulator_ops,
155 .of_match = gpio_regulator_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700156 .of_to_plat = gpio_regulator_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700157 .plat_auto = sizeof(struct gpio_regulator_plat),
Keerthy17b3fe72016-09-15 17:04:06 +0530158};