1 /*
2  * Copyright 2023 EPAM Systems
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #define DT_DRV_COMPAT regulator_gpio
7 
8 #include <stdint.h>
9 
10 #include <zephyr/drivers/regulator.h>
11 #include <zephyr/drivers/gpio.h>
12 #include <zephyr/logging/log.h>
13 
14 LOG_MODULE_REGISTER(regulator_gpio, CONFIG_REGULATOR_LOG_LEVEL);
15 
16 struct regulator_gpio_config {
17 	struct regulator_common_config common;
18 
19 	const struct gpio_dt_spec *gpios;
20 	uint8_t num_gpios;
21 
22 	const int32_t *states;
23 	uint8_t states_cnt;
24 
25 	const struct gpio_dt_spec enable;
26 };
27 
28 struct regulator_gpio_data {
29 	struct regulator_common_data common;
30 	int32_t current_volt_uv;
31 };
32 
regulator_gpio_apply_state(const struct device * dev,uint32_t state)33 static int regulator_gpio_apply_state(const struct device *dev, uint32_t state)
34 {
35 	const struct regulator_gpio_config *cfg = dev->config;
36 
37 	for (unsigned int gpio_idx = 0; gpio_idx < cfg->num_gpios; gpio_idx++) {
38 		int ret;
39 		int new_state_of_gpio = (state >> gpio_idx) & 0x1;
40 
41 		ret = gpio_pin_get_dt(&cfg->gpios[gpio_idx]);
42 		if (ret < 0) {
43 			LOG_ERR("%s: can't get pin state", dev->name);
44 			return ret;
45 		}
46 
47 		if (ret != new_state_of_gpio) {
48 			ret = gpio_pin_set_dt(&cfg->gpios[gpio_idx], new_state_of_gpio);
49 			if (ret < 0) {
50 				LOG_ERR("%s: can't set pin state", dev->name);
51 				return ret;
52 			}
53 		}
54 	}
55 
56 	return 0;
57 }
58 
regulator_gpio_enable(const struct device * dev)59 static int regulator_gpio_enable(const struct device *dev)
60 {
61 	const struct regulator_gpio_config *cfg = dev->config;
62 	int ret;
63 
64 	if (cfg->enable.port == NULL) {
65 		return 0;
66 	}
67 
68 	ret = gpio_pin_set_dt(&cfg->enable, 1);
69 	if (ret < 0) {
70 		LOG_ERR("%s: can't enable regulator!", dev->name);
71 		return ret;
72 	}
73 
74 	return 0;
75 }
76 
regulator_gpio_disable(const struct device * dev)77 static int regulator_gpio_disable(const struct device *dev)
78 {
79 	const struct regulator_gpio_config *cfg = dev->config;
80 
81 	if (cfg->enable.port == NULL) {
82 		return 0;
83 	}
84 
85 	return gpio_pin_set_dt(&cfg->enable, 0);
86 }
87 
regulator_gpio_count_voltages(const struct device * dev)88 static unsigned int regulator_gpio_count_voltages(const struct device *dev)
89 {
90 	const struct regulator_gpio_config *cfg = dev->config;
91 
92 	return cfg->states_cnt;
93 }
94 
regulator_gpio_list_voltage(const struct device * dev,unsigned int idx,int32_t * volt_uv)95 static int regulator_gpio_list_voltage(const struct device *dev, unsigned int idx, int32_t *volt_uv)
96 {
97 	const struct regulator_gpio_config *cfg = dev->config;
98 
99 	if (idx >= cfg->states_cnt) {
100 		LOG_ERR("%s: can't get list voltage for idx %u", dev->name, idx);
101 		return -EINVAL;
102 	}
103 
104 	*volt_uv = cfg->states[idx * 2];
105 	return 0;
106 }
107 
regulator_gpio_set_voltage(const struct device * dev,int32_t min_uv,int32_t max_uv)108 static int regulator_gpio_set_voltage(const struct device *dev, int32_t min_uv, int32_t max_uv)
109 {
110 	const struct regulator_gpio_config *cfg = dev->config;
111 	struct regulator_gpio_data *data = dev->data;
112 	int32_t best_voltage = INT32_MAX;
113 	unsigned int best_state;
114 	int ret = 0;
115 
116 	/* choose minimum possible voltage in range provided by a caller */
117 	for (unsigned int state_idx = 0; state_idx < cfg->states_cnt; state_idx++) {
118 		if (!IN_RANGE(cfg->states[state_idx * 2], min_uv, max_uv) ||
119 		    cfg->states[state_idx * 2] >= best_voltage) {
120 			continue;
121 		}
122 
123 		best_voltage = cfg->states[state_idx * 2];
124 		best_state = cfg->states[state_idx * 2 + 1];
125 	}
126 
127 	if (best_voltage == INT32_MAX) {
128 		LOG_ERR("%s: can't find voltage is states", dev->name);
129 		return -EINVAL;
130 	}
131 
132 	if (best_voltage == data->current_volt_uv) {
133 		return 0;
134 	}
135 
136 	ret = regulator_gpio_apply_state(dev, best_state);
137 	if (ret) {
138 		return ret;
139 	}
140 
141 	data->current_volt_uv = best_voltage;
142 	return 0;
143 }
144 
regulator_gpio_get_voltage(const struct device * dev,int32_t * volt_uv)145 static int regulator_gpio_get_voltage(const struct device *dev, int32_t *volt_uv)
146 {
147 	const struct regulator_gpio_data *data = dev->data;
148 
149 	*volt_uv = data->current_volt_uv;
150 	return 0;
151 }
152 
153 static const struct regulator_driver_api regulator_gpio_api = {
154 	.enable = regulator_gpio_enable,
155 	.disable = regulator_gpio_disable,
156 	.set_voltage = regulator_gpio_set_voltage,
157 	.get_voltage = regulator_gpio_get_voltage,
158 	.count_voltages = regulator_gpio_count_voltages,
159 	.list_voltage = regulator_gpio_list_voltage,
160 };
161 
regulator_gpio_init(const struct device * dev)162 static int regulator_gpio_init(const struct device *dev)
163 {
164 	const struct regulator_gpio_config *cfg = dev->config;
165 	int ret;
166 
167 	regulator_common_data_init(dev);
168 
169 	for (unsigned int gpio_idx = 0; gpio_idx < cfg->num_gpios; gpio_idx++) {
170 		int ret;
171 
172 		if (!gpio_is_ready_dt(&cfg->gpios[gpio_idx])) {
173 			LOG_ERR("%s: gpio pin: %s not ready", dev->name,
174 				cfg->gpios[gpio_idx].port ? cfg->gpios[gpio_idx].port->name
175 							  : "null");
176 			return -ENODEV;
177 		}
178 
179 		ret = gpio_pin_configure_dt(&cfg->gpios[gpio_idx], GPIO_OUTPUT);
180 		if (ret < 0) {
181 			LOG_ERR("%s: can't configure pin (%d) as output", dev->name,
182 				cfg->gpios[gpio_idx].pin);
183 			return ret;
184 		}
185 	}
186 
187 	if (cfg->enable.port != NULL) {
188 		if (!gpio_is_ready_dt(&cfg->enable)) {
189 			LOG_ERR("%s: gpio pin: %s not ready", dev->name, cfg->enable.port->name);
190 			return -ENODEV;
191 		}
192 
193 		ret = gpio_pin_configure_dt(&cfg->enable, GPIO_OUTPUT | GPIO_OUTPUT_INIT_LOW);
194 		if (ret < 0) {
195 			LOG_ERR("%s: can't configure enable pin (%d) as output", dev->name,
196 				cfg->enable.pin);
197 			return ret;
198 		}
199 	}
200 
201 	return regulator_common_init(dev, false);
202 }
203 
204 #define REG_GPIO_CONTEXT_GPIOS_SPEC_ELEM(_node_id, _prop, _idx)                                    \
205 	GPIO_DT_SPEC_GET_BY_IDX(_node_id, _prop, _idx),
206 
207 #define REG_GPIO_CONTEXT_GPIOS_FOREACH_ELEM(inst)                                                  \
208 	DT_FOREACH_PROP_ELEM(DT_DRV_INST(inst), gpios, REG_GPIO_CONTEXT_GPIOS_SPEC_ELEM)
209 
210 #define REG_GPIO_CONTEXT_GPIOS_INITIALIZE(inst)                                                    \
211 	.gpios = (const struct gpio_dt_spec[]){REG_GPIO_CONTEXT_GPIOS_FOREACH_ELEM(inst)},         \
212 	.num_gpios = DT_INST_PROP_LEN(inst, gpios)
213 
214 #define REGULATOR_GPIO_DEFINE(inst)                                                                \
215 	static struct regulator_gpio_data data##inst = {                                           \
216 		.current_volt_uv = INT32_MAX,                                                      \
217 	};                                                                                         \
218 	BUILD_ASSERT(!(DT_INST_PROP_LEN(inst, states) & 0x1),                                      \
219 		     "Number of regulator states should be even");                                 \
220 	static const struct regulator_gpio_config config##inst = {                                 \
221 		.common = REGULATOR_DT_INST_COMMON_CONFIG_INIT(inst),                              \
222 		REG_GPIO_CONTEXT_GPIOS_INITIALIZE(inst),                                           \
223 		.enable = GPIO_DT_SPEC_INST_GET_OR(inst, enable_gpios, {0}),                       \
224 		.states = ((const int[])DT_INST_PROP(inst, states)),                               \
225 		.states_cnt = DT_INST_PROP_LEN(inst, states) / 2,                                  \
226 	};                                                                                         \
227 	DEVICE_DT_INST_DEFINE(inst, regulator_gpio_init, NULL, &data##inst, &config##inst,         \
228 			      POST_KERNEL, CONFIG_REGULATOR_GPIO_INIT_PRIORITY,                    \
229 			      &regulator_gpio_api);
230 
231 DT_INST_FOREACH_STATUS_OKAY(REGULATOR_GPIO_DEFINE)
232