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