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