blob: 74137b7b876fd7b6201891d85739247633893ac6 [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>
Simon Glassbdd5f812023-09-14 18:21:46 -060013#include <linux/printk.h>
Keerthy17b3fe72016-09-15 17:04:06 +053014#include <power/pmic.h>
15#include <power/regulator.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060016#include "regulator_common.h"
Keerthy17b3fe72016-09-15 17:04:06 +053017
Simon Glass1c1ddf62020-07-19 10:15:44 -060018#include "regulator_common.h"
19
Keerthy17b3fe72016-09-15 17:04:06 +053020#define GPIO_REGULATOR_MAX_STATES 2
21
Simon Glassb75b15b2020-12-03 16:55:23 -070022struct gpio_regulator_plat {
23 struct regulator_common_plat 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
Simon Glassaad29ae2020-12-03 16:55:21 -070029static int gpio_regulator_of_to_plat(struct udevice *dev)
Keerthy17b3fe72016-09-15 17:04:06 +053030{
Simon Glass71fa5b42020-12-03 16:55:18 -070031 struct dm_regulator_uclass_plat *uc_pdata;
Eugen Hristev81aa1922023-04-19 16:45:25 +030032 struct gpio_regulator_plat *plat;
Keerthy17b3fe72016-09-15 17:04:06 +053033 struct gpio_desc *gpio;
Keerthy17b3fe72016-09-15 17:04:06 +053034 int ret, count, i, j;
Patrick Delaunay23f988a2020-09-10 18:18:16 +020035 u32 states_array[GPIO_REGULATOR_MAX_STATES * 2];
Keerthy17b3fe72016-09-15 17:04:06 +053036
Eugen Hristev81aa1922023-04-19 16:45:25 +030037 plat = dev_get_plat(dev);
Simon Glass71fa5b42020-12-03 16:55:18 -070038 uc_pdata = dev_get_uclass_plat(dev);
Keerthy17b3fe72016-09-15 17:04:06 +053039 if (!uc_pdata)
40 return -ENXIO;
41
42 /* Set type to gpio */
43 uc_pdata->type = REGULATOR_TYPE_GPIO;
44
45 /*
46 * Get gpio regulator gpio desc
47 * Assuming one GPIO per regulator.
48 * Can be extended later to multiple GPIOs
49 * per gpio-regulator. As of now no instance with multiple
50 * gpios is presnt
51 */
Eugen Hristev81aa1922023-04-19 16:45:25 +030052 gpio = &plat->gpio;
Keerthy17b3fe72016-09-15 17:04:06 +053053 ret = gpio_request_by_name(dev, "gpios", 0, gpio, GPIOD_IS_OUT);
54 if (ret)
55 debug("regulator gpio - not found! Error: %d", ret);
56
Patrick Delaunaydeb5b482020-09-10 18:18:17 +020057 ret = dev_read_size(dev, "states");
58 if (ret < 0)
59 return ret;
60
61 count = ret / sizeof(states_array[0]);
62 if (count > ARRAY_SIZE(states_array)) {
63 debug("regulator gpio - to many states (%d > %d)",
64 count / 2, GPIO_REGULATOR_MAX_STATES);
65 count = ARRAY_SIZE(states_array);
66 }
Keerthy17b3fe72016-09-15 17:04:06 +053067
Patrick Delaunaydeb5b482020-09-10 18:18:17 +020068 ret = dev_read_u32_array(dev, "states", states_array, count);
69 if (ret < 0)
70 return ret;
Keerthy17b3fe72016-09-15 17:04:06 +053071
72 for (i = 0, j = 0; i < count; i += 2) {
Eugen Hristev81aa1922023-04-19 16:45:25 +030073 plat->voltages[j] = states_array[i];
74 plat->states[j] = states_array[i + 1];
Keerthy17b3fe72016-09-15 17:04:06 +053075 j++;
76 }
77
Eugen Hristev81aa1922023-04-19 16:45:25 +030078 return regulator_common_of_to_plat(dev, &plat->common, "enable-gpios");
Keerthy17b3fe72016-09-15 17:04:06 +053079}
80
81static int gpio_regulator_get_value(struct udevice *dev)
82{
Simon Glass71fa5b42020-12-03 16:55:18 -070083 struct dm_regulator_uclass_plat *uc_pdata;
Eugen Hristev81aa1922023-04-19 16:45:25 +030084 struct gpio_regulator_plat *plat = dev_get_plat(dev);
Keerthy17b3fe72016-09-15 17:04:06 +053085 int enable;
86
Eugen Hristev81aa1922023-04-19 16:45:25 +030087 if (!plat->gpio.dev)
Keerthy17b3fe72016-09-15 17:04:06 +053088 return -ENOSYS;
89
Simon Glass71fa5b42020-12-03 16:55:18 -070090 uc_pdata = dev_get_uclass_plat(dev);
Keerthy17b3fe72016-09-15 17:04:06 +053091 if (uc_pdata->min_uV > uc_pdata->max_uV) {
92 debug("Invalid constraints for: %s\n", uc_pdata->name);
93 return -EINVAL;
94 }
95
Eugen Hristev81aa1922023-04-19 16:45:25 +030096 enable = dm_gpio_get_value(&plat->gpio);
97 if (enable == plat->states[0])
98 return plat->voltages[0];
Keerthy17b3fe72016-09-15 17:04:06 +053099 else
Eugen Hristev81aa1922023-04-19 16:45:25 +0300100 return plat->voltages[1];
Keerthy17b3fe72016-09-15 17:04:06 +0530101}
102
103static int gpio_regulator_set_value(struct udevice *dev, int uV)
104{
Eugen Hristev81aa1922023-04-19 16:45:25 +0300105 struct gpio_regulator_plat *plat = dev_get_plat(dev);
Keerthy17b3fe72016-09-15 17:04:06 +0530106 int ret;
107 bool enable;
108
Eugen Hristev81aa1922023-04-19 16:45:25 +0300109 if (!plat->gpio.dev)
Keerthy17b3fe72016-09-15 17:04:06 +0530110 return -ENOSYS;
111
Eugen Hristev81aa1922023-04-19 16:45:25 +0300112 if (uV == plat->voltages[0])
113 enable = plat->states[0];
114 else if (uV == plat->voltages[1])
115 enable = plat->states[1];
Keerthy17b3fe72016-09-15 17:04:06 +0530116 else
117 return -EINVAL;
118
Eugen Hristev81aa1922023-04-19 16:45:25 +0300119 ret = dm_gpio_set_value(&plat->gpio, enable);
Keerthy17b3fe72016-09-15 17:04:06 +0530120 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900121 pr_err("Can't set regulator : %s gpio to: %d\n", dev->name,
Keerthy17b3fe72016-09-15 17:04:06 +0530122 enable);
123 return ret;
124 }
125
126 return 0;
127}
128
Sven Schwermerdb58d9e2019-06-24 13:03:34 +0200129static int gpio_regulator_get_enable(struct udevice *dev)
130{
Eugen Hristev81aa1922023-04-19 16:45:25 +0300131 struct gpio_regulator_plat *plat = dev_get_plat(dev);
132 return regulator_common_get_enable(dev, &plat->common);
Sven Schwermerdb58d9e2019-06-24 13:03:34 +0200133}
134
135static int gpio_regulator_set_enable(struct udevice *dev, bool enable)
136{
Eugen Hristev81aa1922023-04-19 16:45:25 +0300137 struct gpio_regulator_plat *plat = dev_get_plat(dev);
138 return regulator_common_set_enable(dev, &plat->common, enable);
Sven Schwermerdb58d9e2019-06-24 13:03:34 +0200139}
140
Keerthy17b3fe72016-09-15 17:04:06 +0530141static const struct dm_regulator_ops gpio_regulator_ops = {
142 .get_value = gpio_regulator_get_value,
143 .set_value = gpio_regulator_set_value,
Sven Schwermerdb58d9e2019-06-24 13:03:34 +0200144 .get_enable = gpio_regulator_get_enable,
145 .set_enable = gpio_regulator_set_enable,
Keerthy17b3fe72016-09-15 17:04:06 +0530146};
147
148static const struct udevice_id gpio_regulator_ids[] = {
149 { .compatible = "regulator-gpio" },
150 { },
151};
152
153U_BOOT_DRIVER(gpio_regulator) = {
154 .name = "gpio regulator",
155 .id = UCLASS_REGULATOR,
156 .ops = &gpio_regulator_ops,
157 .of_match = gpio_regulator_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700158 .of_to_plat = gpio_regulator_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700159 .plat_auto = sizeof(struct gpio_regulator_plat),
Keerthy17b3fe72016-09-15 17:04:06 +0530160};