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