Lines Matching +full:pwm +full:- +full:dutycycle +full:- +full:unit

1 // SPDX-License-Identifier: GPL-2.0-only
3 * Regulator driver for PWM Regulators
5 * Copyright (C) 2014 - STMicroelectronics Inc.
18 #include <linux/pwm.h>
29 struct pwm_device *pwm; member
48 unsigned int dutycycle; member
52 * Voltage table call-backs
58 unsigned int dutycycle; in pwm_regulator_init_state() local
61 pwm_get_state(drvdata->pwm, &pwm_state); in pwm_regulator_init_state()
62 dutycycle = pwm_get_relative_duty_cycle(&pwm_state, 100); 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_state(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()
163 * The dutycycle for min_uV might be greater than the one for max_uV. in pwm_regulator_get_voltage()
165 * PWM device does not support inversing it in hardware. in pwm_regulator_get_voltage()
168 voltage = min_uV_duty - voltage; in pwm_regulator_get_voltage()
169 diff_duty = min_uV_duty - max_uV_duty; in pwm_regulator_get_voltage()
171 voltage = voltage - min_uV_duty; in pwm_regulator_get_voltage()
172 diff_duty = max_uV_duty - min_uV_duty; in pwm_regulator_get_voltage()
185 unsigned int min_uV_duty = drvdata->continuous.min_uV_dutycycle; in pwm_regulator_set_voltage()
186 unsigned int max_uV_duty = drvdata->continuous.max_uV_dutycycle; in pwm_regulator_set_voltage()
187 unsigned int duty_unit = drvdata->continuous.dutycycle_unit; in pwm_regulator_set_voltage()
188 int min_uV = rdev->constraints->min_uV; in pwm_regulator_set_voltage()
189 int max_uV = rdev->constraints->max_uV; in pwm_regulator_set_voltage()
190 int diff_uV = max_uV - min_uV; in pwm_regulator_set_voltage()
193 unsigned int dutycycle; in pwm_regulator_set_voltage() local
196 pwm_init_state(drvdata->pwm, &pstate); in pwm_regulator_set_voltage()
199 * The dutycycle for min_uV might be greater than the one for max_uV. in pwm_regulator_set_voltage()
201 * PWM device does not support inversing it in hardware. in pwm_regulator_set_voltage()
204 diff_duty = min_uV_duty - max_uV_duty; in pwm_regulator_set_voltage()
206 diff_duty = max_uV_duty - min_uV_duty; in pwm_regulator_set_voltage()
208 dutycycle = DIV_ROUND_CLOSEST_ULL((u64)(req_min_uV - min_uV) * in pwm_regulator_set_voltage()
213 dutycycle = min_uV_duty - dutycycle; in pwm_regulator_set_voltage()
215 dutycycle = min_uV_duty + dutycycle; in pwm_regulator_set_voltage()
217 pwm_set_relative_duty_cycle(&pstate, dutycycle, duty_unit); in pwm_regulator_set_voltage()
219 ret = pwm_apply_state(drvdata->pwm, &pstate); in pwm_regulator_set_voltage()
221 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret); in pwm_regulator_set_voltage()
247 .name = "pwm-regulator",
250 .supply_name = "pwm",
256 struct device_node *np = pdev->dev.of_node; in pwm_regulator_init_table()
261 of_find_property(np, "voltage-table", &length); in pwm_regulator_init_table()
265 dev_err(&pdev->dev, "voltage-table length(%d) is invalid\n", in pwm_regulator_init_table()
267 return -EINVAL; in pwm_regulator_init_table()
270 duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL); in pwm_regulator_init_table()
272 return -ENOMEM; in pwm_regulator_init_table()
274 ret = of_property_read_u32_array(np, "voltage-table", in pwm_regulator_init_table()
278 dev_err(&pdev->dev, "Failed to read voltage-table: %d\n", ret); in pwm_regulator_init_table()
282 drvdata->state = -ENOTRECOVERABLE; in pwm_regulator_init_table()
283 drvdata->duty_cycle_table = duty_cycle_table; in pwm_regulator_init_table()
284 drvdata->desc.ops = &pwm_regulator_voltage_table_ops; in pwm_regulator_init_table()
285 drvdata->desc.n_voltages = length / sizeof(*duty_cycle_table); in pwm_regulator_init_table()
296 drvdata->desc.ops = &pwm_regulator_voltage_continuous_ops; in pwm_regulator_init_continuous()
297 drvdata->desc.continuous_voltage_range = true; in pwm_regulator_init_continuous()
299 of_property_read_u32_array(pdev->dev.of_node, in pwm_regulator_init_continuous()
300 "pwm-dutycycle-range", in pwm_regulator_init_continuous()
302 of_property_read_u32(pdev->dev.of_node, "pwm-dutycycle-unit", in pwm_regulator_init_continuous()
307 return -EINVAL; in pwm_regulator_init_continuous()
309 drvdata->continuous.dutycycle_unit = dutycycle_unit; in pwm_regulator_init_continuous()
310 drvdata->continuous.min_uV_dutycycle = dutycycle_range[0]; in pwm_regulator_init_continuous()
311 drvdata->continuous.max_uV_dutycycle = dutycycle_range[1]; in pwm_regulator_init_continuous()
322 struct device_node *np = pdev->dev.of_node; in pwm_regulator_probe()
327 dev_err(&pdev->dev, "Device Tree node missing\n"); in pwm_regulator_probe()
328 return -EINVAL; in pwm_regulator_probe()
331 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL); in pwm_regulator_probe()
333 return -ENOMEM; in pwm_regulator_probe()
335 memcpy(&drvdata->desc, &pwm_regulator_desc, sizeof(drvdata->desc)); in pwm_regulator_probe()
337 if (of_property_present(np, "voltage-table")) in pwm_regulator_probe()
344 init_data = of_get_regulator_init_data(&pdev->dev, np, in pwm_regulator_probe()
345 &drvdata->desc); in pwm_regulator_probe()
347 return -ENOMEM; in pwm_regulator_probe()
350 config.dev = &pdev->dev; in pwm_regulator_probe()
354 drvdata->pwm = devm_pwm_get(&pdev->dev, NULL); in pwm_regulator_probe()
355 if (IS_ERR(drvdata->pwm)) in pwm_regulator_probe()
356 return dev_err_probe(&pdev->dev, PTR_ERR(drvdata->pwm), in pwm_regulator_probe()
357 "Failed to get PWM\n"); in pwm_regulator_probe()
359 if (init_data->constraints.boot_on || init_data->constraints.always_on) in pwm_regulator_probe()
363 drvdata->enb_gpio = devm_gpiod_get_optional(&pdev->dev, "enable", in pwm_regulator_probe()
365 if (IS_ERR(drvdata->enb_gpio)) { in pwm_regulator_probe()
366 ret = PTR_ERR(drvdata->enb_gpio); in pwm_regulator_probe()
367 dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n", ret); in pwm_regulator_probe()
371 ret = pwm_adjust_config(drvdata->pwm); in pwm_regulator_probe()
375 regulator = devm_regulator_register(&pdev->dev, in pwm_regulator_probe()
376 &drvdata->desc, &config); in pwm_regulator_probe()
379 dev_err(&pdev->dev, "Failed to register regulator %s: %d\n", in pwm_regulator_probe()
380 drvdata->desc.name, ret); in pwm_regulator_probe()
388 { .compatible = "pwm-regulator" },
395 .name = "pwm-regulator",
406 MODULE_DESCRIPTION("PWM Regulator Driver");
407 MODULE_ALIAS("platform:pwm-regulator");