Lines Matching +full:pwm +full:- +full:regulator

1 // SPDX-License-Identifier: GPL-2.0-only
3 * Regulator driver for PWM Regulators
5 * Copyright (C) 2014 - STMicroelectronics Inc.
14 #include <linux/regulator/driver.h>
15 #include <linux/regulator/machine.h>
16 #include <linux/regulator/of_regulator.h>
18 #include <linux/pwm.h>
29 struct pwm_device *pwm; member
37 /* regulator descriptor */
52 * Voltage table call-backs
61 pwm_get_state(drvdata->pwm, &pwm_state); in pwm_regulator_init_state()
64 for (i = 0; i < rdev->desc->n_voltages; i++) { in pwm_regulator_init_state()
65 if (dutycycle == drvdata->duty_cycle_table[i].dutycycle) { in pwm_regulator_init_state()
66 drvdata->state = i; in pwm_regulator_init_state()
76 if (drvdata->state < 0) in pwm_regulator_get_voltage_sel()
79 return drvdata->state; in pwm_regulator_get_voltage_sel()
89 pwm_init_state(drvdata->pwm, &pstate); in pwm_regulator_set_voltage_sel()
91 drvdata->duty_cycle_table[selector].dutycycle, 100); in pwm_regulator_set_voltage_sel()
93 ret = pwm_apply_might_sleep(drvdata->pwm, &pstate); in pwm_regulator_set_voltage_sel()
95 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret); in pwm_regulator_set_voltage_sel()
99 drvdata->state = selector; in pwm_regulator_set_voltage_sel()
109 if (selector >= rdev->desc->n_voltages) in pwm_regulator_list_voltage()
110 return -EINVAL; in pwm_regulator_list_voltage()
112 return drvdata->duty_cycle_table[selector].uV; in pwm_regulator_list_voltage()
119 gpiod_set_value_cansleep(drvdata->enb_gpio, 1); in pwm_regulator_enable()
121 return pwm_enable(drvdata->pwm); in pwm_regulator_enable()
128 pwm_disable(drvdata->pwm); in pwm_regulator_disable()
130 gpiod_set_value_cansleep(drvdata->enb_gpio, 0); in pwm_regulator_disable()
139 if (drvdata->enb_gpio && !gpiod_get_value_cansleep(drvdata->enb_gpio)) in pwm_regulator_is_enabled()
142 return pwm_is_enabled(drvdata->pwm); in pwm_regulator_is_enabled()
148 unsigned int min_uV_duty = drvdata->continuous.min_uV_dutycycle; in pwm_regulator_get_voltage()
149 unsigned int max_uV_duty = drvdata->continuous.max_uV_dutycycle; in pwm_regulator_get_voltage()
150 unsigned int duty_unit = drvdata->continuous.dutycycle_unit; in pwm_regulator_get_voltage()
151 int min_uV = rdev->constraints->min_uV; in pwm_regulator_get_voltage()
152 int max_uV = rdev->constraints->max_uV; in pwm_regulator_get_voltage()
153 int diff_uV = max_uV - min_uV; in pwm_regulator_get_voltage()
158 pwm_get_state(drvdata->pwm, &pstate); in pwm_regulator_get_voltage()
170 return -ENOTRECOVERABLE; in pwm_regulator_get_voltage()
175 * PWM device does not support inversing it in hardware. in pwm_regulator_get_voltage()
178 voltage = min_uV_duty - voltage; in pwm_regulator_get_voltage()
179 diff_duty = min_uV_duty - max_uV_duty; in pwm_regulator_get_voltage()
181 voltage = voltage - min_uV_duty; in pwm_regulator_get_voltage()
182 diff_duty = max_uV_duty - min_uV_duty; in pwm_regulator_get_voltage()
195 unsigned int min_uV_duty = drvdata->continuous.min_uV_dutycycle; in pwm_regulator_set_voltage()
196 unsigned int max_uV_duty = drvdata->continuous.max_uV_dutycycle; in pwm_regulator_set_voltage()
197 unsigned int duty_unit = drvdata->continuous.dutycycle_unit; in pwm_regulator_set_voltage()
198 int min_uV = rdev->constraints->min_uV; in pwm_regulator_set_voltage()
199 int max_uV = rdev->constraints->max_uV; in pwm_regulator_set_voltage()
200 int diff_uV = max_uV - min_uV; in pwm_regulator_set_voltage()
206 pwm_init_state(drvdata->pwm, &pstate); in pwm_regulator_set_voltage()
211 * PWM device does not support inversing it in hardware. in pwm_regulator_set_voltage()
214 diff_duty = min_uV_duty - max_uV_duty; in pwm_regulator_set_voltage()
216 diff_duty = max_uV_duty - min_uV_duty; in pwm_regulator_set_voltage()
218 dutycycle = DIV_ROUND_CLOSEST_ULL((u64)(req_min_uV - min_uV) * in pwm_regulator_set_voltage()
223 dutycycle = min_uV_duty - dutycycle; in pwm_regulator_set_voltage()
229 ret = pwm_apply_might_sleep(drvdata->pwm, &pstate); in pwm_regulator_set_voltage()
231 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret); in pwm_regulator_set_voltage()
257 .name = "pwm-regulator",
260 .supply_name = "pwm",
266 struct device_node *np = pdev->dev.of_node; in pwm_regulator_init_table()
271 of_find_property(np, "voltage-table", &length); in pwm_regulator_init_table()
275 return dev_err_probe(&pdev->dev, -EINVAL, in pwm_regulator_init_table()
276 "voltage-table length(%d) is invalid\n", in pwm_regulator_init_table()
279 duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL); in pwm_regulator_init_table()
281 return -ENOMEM; in pwm_regulator_init_table()
283 ret = of_property_read_u32_array(np, "voltage-table", in pwm_regulator_init_table()
287 return dev_err_probe(&pdev->dev, ret, in pwm_regulator_init_table()
288 "Failed to read voltage-table\n"); in pwm_regulator_init_table()
290 drvdata->state = -ENOTRECOVERABLE; in pwm_regulator_init_table()
291 drvdata->duty_cycle_table = duty_cycle_table; in pwm_regulator_init_table()
292 drvdata->desc.ops = &pwm_regulator_voltage_table_ops; in pwm_regulator_init_table()
293 drvdata->desc.n_voltages = length / sizeof(*duty_cycle_table); in pwm_regulator_init_table()
304 drvdata->desc.ops = &pwm_regulator_voltage_continuous_ops; in pwm_regulator_init_continuous()
305 drvdata->desc.continuous_voltage_range = true; in pwm_regulator_init_continuous()
307 of_property_read_u32_array(pdev->dev.of_node, in pwm_regulator_init_continuous()
308 "pwm-dutycycle-range", in pwm_regulator_init_continuous()
310 of_property_read_u32(pdev->dev.of_node, "pwm-dutycycle-unit", in pwm_regulator_init_continuous()
315 return -EINVAL; in pwm_regulator_init_continuous()
317 drvdata->continuous.dutycycle_unit = dutycycle_unit; in pwm_regulator_init_continuous()
318 drvdata->continuous.min_uV_dutycycle = dutycycle_range[0]; in pwm_regulator_init_continuous()
319 drvdata->continuous.max_uV_dutycycle = dutycycle_range[1]; in pwm_regulator_init_continuous()
330 if (!init_data->constraints.boot_on || drvdata->enb_gpio) in pwm_regulator_init_boot_on()
333 pwm_get_state(drvdata->pwm, &pstate); in pwm_regulator_init_boot_on()
339 * when the regulator core enables the regulator (and in pwm_regulator_init_boot_on()
340 * thus the PWM channel). in pwm_regulator_init_boot_on()
347 return pwm_apply_might_sleep(drvdata->pwm, &pstate); in pwm_regulator_init_boot_on()
354 struct regulator_dev *regulator; in pwm_regulator_probe() local
356 struct device_node *np = pdev->dev.of_node; in pwm_regulator_probe()
361 return dev_err_probe(&pdev->dev, -EINVAL, in pwm_regulator_probe()
364 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL); in pwm_regulator_probe()
366 return -ENOMEM; in pwm_regulator_probe()
368 memcpy(&drvdata->desc, &pwm_regulator_desc, sizeof(drvdata->desc)); in pwm_regulator_probe()
370 if (of_property_present(np, "voltage-table")) in pwm_regulator_probe()
377 init_data = of_get_regulator_init_data(&pdev->dev, np, in pwm_regulator_probe()
378 &drvdata->desc); in pwm_regulator_probe()
380 return -ENOMEM; in pwm_regulator_probe()
383 config.dev = &pdev->dev; in pwm_regulator_probe()
387 drvdata->pwm = devm_pwm_get(&pdev->dev, NULL); in pwm_regulator_probe()
388 if (IS_ERR(drvdata->pwm)) in pwm_regulator_probe()
389 return dev_err_probe(&pdev->dev, PTR_ERR(drvdata->pwm), in pwm_regulator_probe()
390 "Failed to get PWM\n"); in pwm_regulator_probe()
392 if (init_data->constraints.boot_on || init_data->constraints.always_on) in pwm_regulator_probe()
396 drvdata->enb_gpio = devm_gpiod_get_optional(&pdev->dev, "enable", in pwm_regulator_probe()
398 if (IS_ERR(drvdata->enb_gpio)) { in pwm_regulator_probe()
399 ret = PTR_ERR(drvdata->enb_gpio); in pwm_regulator_probe()
400 return dev_err_probe(&pdev->dev, ret, "Failed to get enable GPIO\n"); in pwm_regulator_probe()
403 ret = pwm_adjust_config(drvdata->pwm); in pwm_regulator_probe()
409 return dev_err_probe(&pdev->dev, ret, in pwm_regulator_probe()
412 regulator = devm_regulator_register(&pdev->dev, in pwm_regulator_probe()
413 &drvdata->desc, &config); in pwm_regulator_probe()
414 if (IS_ERR(regulator)) { in pwm_regulator_probe()
415 ret = PTR_ERR(regulator); in pwm_regulator_probe()
416 return dev_err_probe(&pdev->dev, ret, in pwm_regulator_probe()
417 "Failed to register regulator %s\n", in pwm_regulator_probe()
418 drvdata->desc.name); in pwm_regulator_probe()
425 { .compatible = "pwm-regulator" },
432 .name = "pwm-regulator",
443 MODULE_DESCRIPTION("PWM Regulator Driver");
444 MODULE_ALIAS("platform:pwm-regulator");