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 
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 	const void *blob = gd->fdt_blob;
33 	int node = dev_of_offset(dev);
34 	int ret, count, i, j;
35 	u32 states_array[8];
36 
37 	dev_pdata = dev_get_platdata(dev);
38 	uc_pdata = dev_get_uclass_platdata(dev);
39 	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 	 */
52 	gpio = &dev_pdata->gpio;
53 	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 
57 	count = fdtdec_get_int_array_count(blob, node, "states",
58 					   states_array, 8);
59 
60 	if (!count)
61 		return -EINVAL;
62 
63 	for (i = 0, j = 0; i < count; i += 2) {
64 		dev_pdata->voltages[j] = states_array[i];
65 		dev_pdata->states[j] = states_array[i + 1];
66 		j++;
67 	}
68 
69 	return 0;
70 }
71 
72 static int gpio_regulator_get_value(struct udevice *dev)
73 {
74 	struct dm_regulator_uclass_platdata *uc_pdata;
75 	struct gpio_regulator_platdata *dev_pdata = dev_get_platdata(dev);
76 	int enable;
77 
78 	if (!dev_pdata->gpio.dev)
79 		return -ENOSYS;
80 
81 	uc_pdata = dev_get_uclass_platdata(dev);
82 	if (uc_pdata->min_uV > uc_pdata->max_uV) {
83 		debug("Invalid constraints for: %s\n", uc_pdata->name);
84 		return -EINVAL;
85 	}
86 
87 	enable = dm_gpio_get_value(&dev_pdata->gpio);
88 	if (enable == dev_pdata->states[0])
89 		return dev_pdata->voltages[0];
90 	else
91 		return dev_pdata->voltages[1];
92 }
93 
94 static int gpio_regulator_set_value(struct udevice *dev, int uV)
95 {
96 	struct gpio_regulator_platdata *dev_pdata = dev_get_platdata(dev);
97 	int ret;
98 	bool enable;
99 
100 	if (!dev_pdata->gpio.dev)
101 		return -ENOSYS;
102 
103 	if (uV == dev_pdata->voltages[0])
104 		enable = dev_pdata->states[0];
105 	else if (uV == dev_pdata->voltages[1])
106 		enable = dev_pdata->states[1];
107 	else
108 		return -EINVAL;
109 
110 	ret = dm_gpio_set_value(&dev_pdata->gpio, enable);
111 	if (ret) {
112 		pr_err("Can't set regulator : %s gpio to: %d\n", dev->name,
113 		      enable);
114 		return ret;
115 	}
116 
117 	return 0;
118 }
119 
120 static const struct dm_regulator_ops gpio_regulator_ops = {
121 	.get_value	= gpio_regulator_get_value,
122 	.set_value	= gpio_regulator_set_value,
123 };
124 
125 static const struct udevice_id gpio_regulator_ids[] = {
126 	{ .compatible = "regulator-gpio" },
127 	{ },
128 };
129 
130 U_BOOT_DRIVER(gpio_regulator) = {
131 	.name = "gpio regulator",
132 	.id = UCLASS_REGULATOR,
133 	.ops = &gpio_regulator_ops,
134 	.of_match = gpio_regulator_ids,
135 	.ofdata_to_platdata = gpio_regulator_ofdata_to_platdata,
136 	.platdata_auto_alloc_size = sizeof(struct gpio_regulator_platdata),
137 };
138