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