xref: /OK3568_Linux_fs/u-boot/drivers/power/regulator/gpio-regulator.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * (C) Copyright 2016 Texas Instruments Incorporated, <www.ti.com>
3  * Keerthy <j-keerthy@ti.com>
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7 
8 #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 
19 DECLARE_GLOBAL_DATA_PTR;
20 
21 struct gpio_regulator_platdata {
22 	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 
gpio_regulator_ofdata_to_platdata(struct udevice * dev)27 static int gpio_regulator_ofdata_to_platdata(struct udevice *dev)
28 {
29 	struct dm_regulator_uclass_platdata *uc_pdata;
30 	struct gpio_regulator_platdata *dev_pdata;
31 	struct gpio_desc *gpio;
32 	int ret, len, i, j;
33 	u32 states_array[8];
34 
35 	dev_pdata = dev_get_platdata(dev);
36 	uc_pdata = dev_get_uclass_platdata(dev);
37 	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 
55 	len = dev_read_size(dev, "states");
56 	if (len < 0)
57 		return len;
58 
59 	len /= sizeof(fdt32_t);
60 
61 	ret = dev_read_u32_array(dev, "states", states_array, len);
62 	if (ret)
63 		return -EINVAL;
64 
65 	for (i = 0, j = 0; i < len; i += 2) {
66 		dev_pdata->voltages[j] = states_array[i];
67 		dev_pdata->states[j] = states_array[i + 1];
68 		j++;
69 	}
70 
71 	return 0;
72 }
73 
gpio_regulator_get_value(struct udevice * dev)74 static int gpio_regulator_get_value(struct udevice *dev)
75 {
76 	struct dm_regulator_uclass_platdata *uc_pdata;
77 	struct gpio_regulator_platdata *dev_pdata = dev_get_platdata(dev);
78 	int enable;
79 
80 	if (!dev_pdata->gpio.dev)
81 		return -ENOSYS;
82 
83 	uc_pdata = dev_get_uclass_platdata(dev);
84 	if (uc_pdata->min_uV > uc_pdata->max_uV) {
85 		debug("Invalid constraints for: %s\n", uc_pdata->name);
86 		return -EINVAL;
87 	}
88 
89 	enable = dm_gpio_get_value(&dev_pdata->gpio);
90 	if (enable == dev_pdata->states[0])
91 		return dev_pdata->voltages[0];
92 	else
93 		return dev_pdata->voltages[1];
94 }
95 
gpio_regulator_set_value(struct udevice * dev,int uV)96 static int gpio_regulator_set_value(struct udevice *dev, int uV)
97 {
98 	struct gpio_regulator_platdata *dev_pdata = dev_get_platdata(dev);
99 	int ret;
100 	bool enable;
101 
102 	if (!dev_pdata->gpio.dev)
103 		return -ENOSYS;
104 
105 	if (uV == dev_pdata->voltages[0])
106 		enable = dev_pdata->states[0];
107 	else if (uV == dev_pdata->voltages[1])
108 		enable = dev_pdata->states[1];
109 	else
110 		return -EINVAL;
111 
112 	ret = dm_gpio_set_value(&dev_pdata->gpio, enable);
113 	if (ret) {
114 		pr_err("Can't set regulator : %s gpio to: %d\n", dev->name,
115 		      enable);
116 		return ret;
117 	}
118 
119 	return 0;
120 }
121 
122 static const struct dm_regulator_ops gpio_regulator_ops = {
123 	.get_value	= gpio_regulator_get_value,
124 	.set_value	= gpio_regulator_set_value,
125 };
126 
127 static const struct udevice_id gpio_regulator_ids[] = {
128 	{ .compatible = "regulator-gpio" },
129 	{ },
130 };
131 
132 U_BOOT_DRIVER(gpio_regulator) = {
133 	.name = "gpio regulator",
134 	.id = UCLASS_REGULATOR,
135 	.ops = &gpio_regulator_ops,
136 	.of_match = gpio_regulator_ids,
137 	.ofdata_to_platdata = gpio_regulator_ofdata_to_platdata,
138 	.platdata_auto_alloc_size = sizeof(struct gpio_regulator_platdata),
139 };
140