1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Regulator driver for PWM Regulators
4  *
5  * Copyright (C) 2014 - STMicroelectronics Inc.
6  *
7  * Author: Lee Jones <lee.jones@linaro.org>
8  */
9 
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/err.h>
13 #include <linux/platform_device.h>
14 #include <linux/regulator/driver.h>
15 #include <linux/regulator/machine.h>
16 #include <linux/regulator/of_regulator.h>
17 #include <linux/of.h>
18 #include <linux/pwm.h>
19 #include <linux/gpio/consumer.h>
20 
21 struct pwm_continuous_reg_data {
22 	unsigned int min_uV_dutycycle;
23 	unsigned int max_uV_dutycycle;
24 	unsigned int dutycycle_unit;
25 };
26 
27 struct pwm_regulator_data {
28 	/*  Shared */
29 	struct pwm_device *pwm;
30 
31 	/* Voltage table */
32 	struct pwm_voltages *duty_cycle_table;
33 
34 	/* Continuous mode info */
35 	struct pwm_continuous_reg_data continuous;
36 
37 	/* regulator descriptor */
38 	struct regulator_desc desc;
39 
40 	int state;
41 
42 	/* Enable GPIO */
43 	struct gpio_desc *enb_gpio;
44 };
45 
46 struct pwm_voltages {
47 	unsigned int uV;
48 	unsigned int dutycycle;
49 };
50 
51 /*
52  * Voltage table call-backs
53  */
54 static void pwm_regulator_init_state(struct regulator_dev *rdev)
55 {
56 	struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
57 	struct pwm_state pwm_state;
58 	unsigned int dutycycle;
59 	int i;
60 
61 	pwm_get_state(drvdata->pwm, &pwm_state);
62 	dutycycle = pwm_get_relative_duty_cycle(&pwm_state, 100);
63 
64 	for (i = 0; i < rdev->desc->n_voltages; i++) {
65 		if (dutycycle == drvdata->duty_cycle_table[i].dutycycle) {
66 			drvdata->state = i;
67 			return;
68 		}
69 	}
70 }
71 
72 static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev)
73 {
74 	struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
75 
76 	if (drvdata->state < 0)
77 		pwm_regulator_init_state(rdev);
78 
79 	return drvdata->state;
80 }
81 
82 static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev,
83 					 unsigned selector)
84 {
85 	struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
86 	struct pwm_state pstate;
87 	int ret;
88 
89 	pwm_init_state(drvdata->pwm, &pstate);
90 	pwm_set_relative_duty_cycle(&pstate,
91 			drvdata->duty_cycle_table[selector].dutycycle, 100);
92 
93 	ret = pwm_apply_might_sleep(drvdata->pwm, &pstate);
94 	if (ret) {
95 		dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
96 		return ret;
97 	}
98 
99 	drvdata->state = selector;
100 
101 	return 0;
102 }
103 
104 static int pwm_regulator_list_voltage(struct regulator_dev *rdev,
105 				      unsigned selector)
106 {
107 	struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
108 
109 	if (selector >= rdev->desc->n_voltages)
110 		return -EINVAL;
111 
112 	return drvdata->duty_cycle_table[selector].uV;
113 }
114 
115 static int pwm_regulator_enable(struct regulator_dev *dev)
116 {
117 	struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
118 
119 	gpiod_set_value_cansleep(drvdata->enb_gpio, 1);
120 
121 	return pwm_enable(drvdata->pwm);
122 }
123 
124 static int pwm_regulator_disable(struct regulator_dev *dev)
125 {
126 	struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
127 
128 	pwm_disable(drvdata->pwm);
129 
130 	gpiod_set_value_cansleep(drvdata->enb_gpio, 0);
131 
132 	return 0;
133 }
134 
135 static int pwm_regulator_is_enabled(struct regulator_dev *dev)
136 {
137 	struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
138 
139 	if (drvdata->enb_gpio && !gpiod_get_value_cansleep(drvdata->enb_gpio))
140 		return false;
141 
142 	return pwm_is_enabled(drvdata->pwm);
143 }
144 
145 static int pwm_regulator_get_voltage(struct regulator_dev *rdev)
146 {
147 	struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
148 	unsigned int min_uV_duty = drvdata->continuous.min_uV_dutycycle;
149 	unsigned int max_uV_duty = drvdata->continuous.max_uV_dutycycle;
150 	unsigned int duty_unit = drvdata->continuous.dutycycle_unit;
151 	int min_uV = rdev->constraints->min_uV;
152 	int max_uV = rdev->constraints->max_uV;
153 	int diff_uV = max_uV - min_uV;
154 	struct pwm_state pstate;
155 	unsigned int diff_duty;
156 	unsigned int voltage;
157 
158 	pwm_get_state(drvdata->pwm, &pstate);
159 
160 	voltage = pwm_get_relative_duty_cycle(&pstate, duty_unit);
161 	if (voltage < min(max_uV_duty, min_uV_duty) ||
162 	    voltage > max(max_uV_duty, min_uV_duty))
163 		return -ENOTRECOVERABLE;
164 
165 	/*
166 	 * The dutycycle for min_uV might be greater than the one for max_uV.
167 	 * This is happening when the user needs an inversed polarity, but the
168 	 * PWM device does not support inversing it in hardware.
169 	 */
170 	if (max_uV_duty < min_uV_duty) {
171 		voltage = min_uV_duty - voltage;
172 		diff_duty = min_uV_duty - max_uV_duty;
173 	} else {
174 		voltage = voltage - min_uV_duty;
175 		diff_duty = max_uV_duty - min_uV_duty;
176 	}
177 
178 	voltage = DIV_ROUND_CLOSEST_ULL((u64)voltage * diff_uV, diff_duty);
179 
180 	return voltage + min_uV;
181 }
182 
183 static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
184 				     int req_min_uV, int req_max_uV,
185 				     unsigned int *selector)
186 {
187 	struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
188 	unsigned int min_uV_duty = drvdata->continuous.min_uV_dutycycle;
189 	unsigned int max_uV_duty = drvdata->continuous.max_uV_dutycycle;
190 	unsigned int duty_unit = drvdata->continuous.dutycycle_unit;
191 	int min_uV = rdev->constraints->min_uV;
192 	int max_uV = rdev->constraints->max_uV;
193 	int diff_uV = max_uV - min_uV;
194 	struct pwm_state pstate;
195 	unsigned int diff_duty;
196 	unsigned int dutycycle;
197 	int ret;
198 
199 	pwm_init_state(drvdata->pwm, &pstate);
200 
201 	/*
202 	 * The dutycycle for min_uV might be greater than the one for max_uV.
203 	 * This is happening when the user needs an inversed polarity, but the
204 	 * PWM device does not support inversing it in hardware.
205 	 */
206 	if (max_uV_duty < min_uV_duty)
207 		diff_duty = min_uV_duty - max_uV_duty;
208 	else
209 		diff_duty = max_uV_duty - min_uV_duty;
210 
211 	dutycycle = DIV_ROUND_CLOSEST_ULL((u64)(req_min_uV - min_uV) *
212 					  diff_duty,
213 					  diff_uV);
214 
215 	if (max_uV_duty < min_uV_duty)
216 		dutycycle = min_uV_duty - dutycycle;
217 	else
218 		dutycycle = min_uV_duty + dutycycle;
219 
220 	pwm_set_relative_duty_cycle(&pstate, dutycycle, duty_unit);
221 
222 	ret = pwm_apply_might_sleep(drvdata->pwm, &pstate);
223 	if (ret) {
224 		dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
225 		return ret;
226 	}
227 
228 	return 0;
229 }
230 
231 static const struct regulator_ops pwm_regulator_voltage_table_ops = {
232 	.set_voltage_sel = pwm_regulator_set_voltage_sel,
233 	.get_voltage_sel = pwm_regulator_get_voltage_sel,
234 	.list_voltage    = pwm_regulator_list_voltage,
235 	.map_voltage     = regulator_map_voltage_iterate,
236 	.enable          = pwm_regulator_enable,
237 	.disable         = pwm_regulator_disable,
238 	.is_enabled      = pwm_regulator_is_enabled,
239 };
240 
241 static const struct regulator_ops pwm_regulator_voltage_continuous_ops = {
242 	.get_voltage = pwm_regulator_get_voltage,
243 	.set_voltage = pwm_regulator_set_voltage,
244 	.enable          = pwm_regulator_enable,
245 	.disable         = pwm_regulator_disable,
246 	.is_enabled      = pwm_regulator_is_enabled,
247 };
248 
249 static const struct regulator_desc pwm_regulator_desc = {
250 	.name		= "pwm-regulator",
251 	.type		= REGULATOR_VOLTAGE,
252 	.owner		= THIS_MODULE,
253 	.supply_name    = "pwm",
254 };
255 
256 static int pwm_regulator_init_table(struct platform_device *pdev,
257 				    struct pwm_regulator_data *drvdata)
258 {
259 	struct device_node *np = pdev->dev.of_node;
260 	struct pwm_voltages *duty_cycle_table;
261 	unsigned int length = 0;
262 	int ret;
263 
264 	of_find_property(np, "voltage-table", &length);
265 
266 	if ((length < sizeof(*duty_cycle_table)) ||
267 	    (length % sizeof(*duty_cycle_table))) {
268 		dev_err(&pdev->dev, "voltage-table length(%d) is invalid\n",
269 			length);
270 		return -EINVAL;
271 	}
272 
273 	duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
274 	if (!duty_cycle_table)
275 		return -ENOMEM;
276 
277 	ret = of_property_read_u32_array(np, "voltage-table",
278 					 (u32 *)duty_cycle_table,
279 					 length / sizeof(u32));
280 	if (ret) {
281 		dev_err(&pdev->dev, "Failed to read voltage-table: %d\n", ret);
282 		return ret;
283 	}
284 
285 	drvdata->state			= -ENOTRECOVERABLE;
286 	drvdata->duty_cycle_table	= duty_cycle_table;
287 	drvdata->desc.ops = &pwm_regulator_voltage_table_ops;
288 	drvdata->desc.n_voltages	= length / sizeof(*duty_cycle_table);
289 
290 	return 0;
291 }
292 
293 static int pwm_regulator_init_continuous(struct platform_device *pdev,
294 					 struct pwm_regulator_data *drvdata)
295 {
296 	u32 dutycycle_range[2] = { 0, 100 };
297 	u32 dutycycle_unit = 100;
298 
299 	drvdata->desc.ops = &pwm_regulator_voltage_continuous_ops;
300 	drvdata->desc.continuous_voltage_range = true;
301 
302 	of_property_read_u32_array(pdev->dev.of_node,
303 				   "pwm-dutycycle-range",
304 				   dutycycle_range, 2);
305 	of_property_read_u32(pdev->dev.of_node, "pwm-dutycycle-unit",
306 			     &dutycycle_unit);
307 
308 	if (dutycycle_range[0] > dutycycle_unit ||
309 	    dutycycle_range[1] > dutycycle_unit)
310 		return -EINVAL;
311 
312 	drvdata->continuous.dutycycle_unit = dutycycle_unit;
313 	drvdata->continuous.min_uV_dutycycle = dutycycle_range[0];
314 	drvdata->continuous.max_uV_dutycycle = dutycycle_range[1];
315 
316 	return 0;
317 }
318 
319 static int pwm_regulator_probe(struct platform_device *pdev)
320 {
321 	const struct regulator_init_data *init_data;
322 	struct pwm_regulator_data *drvdata;
323 	struct regulator_dev *regulator;
324 	struct regulator_config config = { };
325 	struct device_node *np = pdev->dev.of_node;
326 	enum gpiod_flags gpio_flags;
327 	int ret;
328 
329 	if (!np) {
330 		dev_err(&pdev->dev, "Device Tree node missing\n");
331 		return -EINVAL;
332 	}
333 
334 	drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
335 	if (!drvdata)
336 		return -ENOMEM;
337 
338 	memcpy(&drvdata->desc, &pwm_regulator_desc, sizeof(drvdata->desc));
339 
340 	if (of_property_present(np, "voltage-table"))
341 		ret = pwm_regulator_init_table(pdev, drvdata);
342 	else
343 		ret = pwm_regulator_init_continuous(pdev, drvdata);
344 	if (ret)
345 		return ret;
346 
347 	init_data = of_get_regulator_init_data(&pdev->dev, np,
348 					       &drvdata->desc);
349 	if (!init_data)
350 		return -ENOMEM;
351 
352 	config.of_node = np;
353 	config.dev = &pdev->dev;
354 	config.driver_data = drvdata;
355 	config.init_data = init_data;
356 
357 	drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
358 	if (IS_ERR(drvdata->pwm))
359 		return dev_err_probe(&pdev->dev, PTR_ERR(drvdata->pwm),
360 				     "Failed to get PWM\n");
361 
362 	if (init_data->constraints.boot_on || init_data->constraints.always_on)
363 		gpio_flags = GPIOD_OUT_HIGH;
364 	else
365 		gpio_flags = GPIOD_OUT_LOW;
366 	drvdata->enb_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
367 						    gpio_flags);
368 	if (IS_ERR(drvdata->enb_gpio)) {
369 		ret = PTR_ERR(drvdata->enb_gpio);
370 		dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n", ret);
371 		return ret;
372 	}
373 
374 	ret = pwm_adjust_config(drvdata->pwm);
375 	if (ret)
376 		return ret;
377 
378 	regulator = devm_regulator_register(&pdev->dev,
379 					    &drvdata->desc, &config);
380 	if (IS_ERR(regulator)) {
381 		ret = PTR_ERR(regulator);
382 		dev_err(&pdev->dev, "Failed to register regulator %s: %d\n",
383 			drvdata->desc.name, ret);
384 		return ret;
385 	}
386 
387 	return 0;
388 }
389 
390 static const struct of_device_id __maybe_unused pwm_of_match[] = {
391 	{ .compatible = "pwm-regulator" },
392 	{ },
393 };
394 MODULE_DEVICE_TABLE(of, pwm_of_match);
395 
396 static struct platform_driver pwm_regulator_driver = {
397 	.driver = {
398 		.name		= "pwm-regulator",
399 		.probe_type	= PROBE_PREFER_ASYNCHRONOUS,
400 		.of_match_table = of_match_ptr(pwm_of_match),
401 	},
402 	.probe = pwm_regulator_probe,
403 };
404 
405 module_platform_driver(pwm_regulator_driver);
406 
407 MODULE_LICENSE("GPL");
408 MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
409 MODULE_DESCRIPTION("PWM Regulator Driver");
410 MODULE_ALIAS("platform:pwm-regulator");
411