blob: 38b22535c3dd4998a2670d9a1b1d24dc1e47519d [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
Keerthy17b3fe72016-09-15 17:04:06 +05307#include <fdtdec.h>
8#include <errno.h>
9#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Keerthy17b3fe72016-09-15 17:04:06 +053011#include <asm/gpio.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060012#include <linux/printk.h>
Keerthy17b3fe72016-09-15 17:04:06 +053013#include <power/pmic.h>
14#include <power/regulator.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060015#include "regulator_common.h"
Keerthy17b3fe72016-09-15 17:04:06 +053016
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
Simon Glassb75b15b2020-12-03 16:55:23 -070021struct gpio_regulator_plat {
22 struct regulator_common_plat common;
Keerthy17b3fe72016-09-15 17:04:06 +053023 struct gpio_desc gpio; /* GPIO for regulator voltage control */
24 int states[GPIO_REGULATOR_MAX_STATES];
25 int voltages[GPIO_REGULATOR_MAX_STATES];
26};
27
Simon Glassaad29ae2020-12-03 16:55:21 -070028static int gpio_regulator_of_to_plat(struct udevice *dev)
Keerthy17b3fe72016-09-15 17:04:06 +053029{
Simon Glass71fa5b42020-12-03 16:55:18 -070030 struct dm_regulator_uclass_plat *uc_pdata;
Eugen Hristev81aa1922023-04-19 16:45:25 +030031 struct gpio_regulator_plat *plat;
Keerthy17b3fe72016-09-15 17:04:06 +053032 struct gpio_desc *gpio;
Keerthy17b3fe72016-09-15 17:04:06 +053033 int ret, count, i, j;
Patrick Delaunay23f988a2020-09-10 18:18:16 +020034 u32 states_array[GPIO_REGULATOR_MAX_STATES * 2];
Keerthy17b3fe72016-09-15 17:04:06 +053035
Eugen Hristev81aa1922023-04-19 16:45:25 +030036 plat = dev_get_plat(dev);
Simon Glass71fa5b42020-12-03 16:55:18 -070037 uc_pdata = dev_get_uclass_plat(dev);
Keerthy17b3fe72016-09-15 17:04:06 +053038 if (!uc_pdata)
39 return -ENXIO;
40
41 /* Set type to gpio */
42 uc_pdata->type = REGULATOR_TYPE_GPIO;
43
44 /*
45 * Get gpio regulator gpio desc
46 * Assuming one GPIO per regulator.
47 * Can be extended later to multiple GPIOs
48 * per gpio-regulator. As of now no instance with multiple
49 * gpios is presnt
50 */
Eugen Hristev81aa1922023-04-19 16:45:25 +030051 gpio = &plat->gpio;
Keerthy17b3fe72016-09-15 17:04:06 +053052 ret = gpio_request_by_name(dev, "gpios", 0, gpio, GPIOD_IS_OUT);
53 if (ret)
54 debug("regulator gpio - not found! Error: %d", ret);
55
Patrick Delaunaydeb5b482020-09-10 18:18:17 +020056 ret = dev_read_size(dev, "states");
57 if (ret < 0)
58 return ret;
59
60 count = ret / sizeof(states_array[0]);
61 if (count > ARRAY_SIZE(states_array)) {
62 debug("regulator gpio - to many states (%d > %d)",
63 count / 2, GPIO_REGULATOR_MAX_STATES);
64 count = ARRAY_SIZE(states_array);
65 }
Keerthy17b3fe72016-09-15 17:04:06 +053066
Patrick Delaunaydeb5b482020-09-10 18:18:17 +020067 ret = dev_read_u32_array(dev, "states", states_array, count);
68 if (ret < 0)
69 return ret;
Keerthy17b3fe72016-09-15 17:04:06 +053070
71 for (i = 0, j = 0; i < count; i += 2) {
Eugen Hristev81aa1922023-04-19 16:45:25 +030072 plat->voltages[j] = states_array[i];
73 plat->states[j] = states_array[i + 1];
Keerthy17b3fe72016-09-15 17:04:06 +053074 j++;
75 }
76
Eugen Hristev81aa1922023-04-19 16:45:25 +030077 return regulator_common_of_to_plat(dev, &plat->common, "enable-gpios");
Keerthy17b3fe72016-09-15 17:04:06 +053078}
79
80static int gpio_regulator_get_value(struct udevice *dev)
81{
Simon Glass71fa5b42020-12-03 16:55:18 -070082 struct dm_regulator_uclass_plat *uc_pdata;
Eugen Hristev81aa1922023-04-19 16:45:25 +030083 struct gpio_regulator_plat *plat = dev_get_plat(dev);
Keerthy17b3fe72016-09-15 17:04:06 +053084 int enable;
85
Eugen Hristev81aa1922023-04-19 16:45:25 +030086 if (!plat->gpio.dev)
Keerthy17b3fe72016-09-15 17:04:06 +053087 return -ENOSYS;
88
Simon Glass71fa5b42020-12-03 16:55:18 -070089 uc_pdata = dev_get_uclass_plat(dev);
Keerthy17b3fe72016-09-15 17:04:06 +053090 if (uc_pdata->min_uV > uc_pdata->max_uV) {
91 debug("Invalid constraints for: %s\n", uc_pdata->name);
92 return -EINVAL;
93 }
94
Eugen Hristev81aa1922023-04-19 16:45:25 +030095 enable = dm_gpio_get_value(&plat->gpio);
96 if (enable == plat->states[0])
97 return plat->voltages[0];
Keerthy17b3fe72016-09-15 17:04:06 +053098 else
Eugen Hristev81aa1922023-04-19 16:45:25 +030099 return plat->voltages[1];
Keerthy17b3fe72016-09-15 17:04:06 +0530100}
101
102static int gpio_regulator_set_value(struct udevice *dev, int uV)
103{
Eugen Hristev81aa1922023-04-19 16:45:25 +0300104 struct gpio_regulator_plat *plat = dev_get_plat(dev);
Keerthy17b3fe72016-09-15 17:04:06 +0530105 int ret;
106 bool enable;
107
Eugen Hristev81aa1922023-04-19 16:45:25 +0300108 if (!plat->gpio.dev)
Keerthy17b3fe72016-09-15 17:04:06 +0530109 return -ENOSYS;
110
Eugen Hristev81aa1922023-04-19 16:45:25 +0300111 if (uV == plat->voltages[0])
112 enable = plat->states[0];
113 else if (uV == plat->voltages[1])
114 enable = plat->states[1];
Keerthy17b3fe72016-09-15 17:04:06 +0530115 else
116 return -EINVAL;
117
Eugen Hristev81aa1922023-04-19 16:45:25 +0300118 ret = dm_gpio_set_value(&plat->gpio, enable);
Keerthy17b3fe72016-09-15 17:04:06 +0530119 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900120 pr_err("Can't set regulator : %s gpio to: %d\n", dev->name,
Keerthy17b3fe72016-09-15 17:04:06 +0530121 enable);
122 return ret;
123 }
124
125 return 0;
126}
127
Sven Schwermerdb58d9e2019-06-24 13:03:34 +0200128static int gpio_regulator_get_enable(struct udevice *dev)
129{
Eugen Hristev81aa1922023-04-19 16:45:25 +0300130 struct gpio_regulator_plat *plat = dev_get_plat(dev);
131 return regulator_common_get_enable(dev, &plat->common);
Sven Schwermerdb58d9e2019-06-24 13:03:34 +0200132}
133
134static int gpio_regulator_set_enable(struct udevice *dev, bool enable)
135{
Eugen Hristev81aa1922023-04-19 16:45:25 +0300136 struct gpio_regulator_plat *plat = dev_get_plat(dev);
137 return regulator_common_set_enable(dev, &plat->common, enable);
Sven Schwermerdb58d9e2019-06-24 13:03:34 +0200138}
139
Keerthy17b3fe72016-09-15 17:04:06 +0530140static const struct dm_regulator_ops gpio_regulator_ops = {
141 .get_value = gpio_regulator_get_value,
142 .set_value = gpio_regulator_set_value,
Sven Schwermerdb58d9e2019-06-24 13:03:34 +0200143 .get_enable = gpio_regulator_get_enable,
144 .set_enable = gpio_regulator_set_enable,
Keerthy17b3fe72016-09-15 17:04:06 +0530145};
146
147static const struct udevice_id gpio_regulator_ids[] = {
148 { .compatible = "regulator-gpio" },
149 { },
150};
151
152U_BOOT_DRIVER(gpio_regulator) = {
153 .name = "gpio regulator",
154 .id = UCLASS_REGULATOR,
155 .ops = &gpio_regulator_ops,
156 .of_match = gpio_regulator_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700157 .of_to_plat = gpio_regulator_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700158 .plat_auto = sizeof(struct gpio_regulator_plat),
Keerthy17b3fe72016-09-15 17:04:06 +0530159};