2 * Regulator driver for PWM Regulators
4 * Copyright (C) 2014 - STMicroelectronics Inc.
6 * Author: Lee Jones <lee.jones@linaro.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/err.h>
16 #include <linux/regulator/driver.h>
17 #include <linux/regulator/machine.h>
18 #include <linux/regulator/of_regulator.h>
20 #include <linux/of_device.h>
21 #include <linux/pwm.h>
22 #include <linux/gpio/consumer.h>
24 struct pwm_continuous_reg_data {
25 unsigned int min_uV_dutycycle;
26 unsigned int max_uV_dutycycle;
27 unsigned int dutycycle_unit;
30 struct pwm_regulator_data {
32 struct pwm_device *pwm;
35 struct pwm_voltages *duty_cycle_table;
37 /* Continuous mode info */
38 struct pwm_continuous_reg_data continuous;
40 /* regulator descriptor */
41 struct regulator_desc desc;
44 struct regulator_ops ops;
49 struct gpio_desc *enb_gpio;
54 unsigned int dutycycle;
58 * Voltage table call-backs
60 static void pwm_regulator_init_state(struct regulator_dev *rdev)
62 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
63 struct pwm_state pwm_state;
64 unsigned int dutycycle;
67 pwm_get_state(drvdata->pwm, &pwm_state);
68 dutycycle = pwm_get_relative_duty_cycle(&pwm_state, 100);
70 for (i = 0; i < rdev->desc->n_voltages; i++) {
71 if (dutycycle == drvdata->duty_cycle_table[i].dutycycle) {
78 static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev)
80 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
82 if (drvdata->state < 0)
83 pwm_regulator_init_state(rdev);
85 return drvdata->state;
88 static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev,
91 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
92 struct pwm_state pstate;
95 pwm_init_state(drvdata->pwm, &pstate);
96 pwm_set_relative_duty_cycle(&pstate,
97 drvdata->duty_cycle_table[selector].dutycycle, 100);
99 ret = pwm_apply_state(drvdata->pwm, &pstate);
101 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
105 drvdata->state = selector;
110 static int pwm_regulator_list_voltage(struct regulator_dev *rdev,
113 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
115 if (selector >= rdev->desc->n_voltages)
118 return drvdata->duty_cycle_table[selector].uV;
121 static int pwm_regulator_enable(struct regulator_dev *dev)
123 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
125 gpiod_set_value_cansleep(drvdata->enb_gpio, 1);
127 return pwm_enable(drvdata->pwm);
130 static int pwm_regulator_disable(struct regulator_dev *dev)
132 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
134 pwm_disable(drvdata->pwm);
136 gpiod_set_value_cansleep(drvdata->enb_gpio, 0);
141 static int pwm_regulator_is_enabled(struct regulator_dev *dev)
143 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
145 if (drvdata->enb_gpio && !gpiod_get_value_cansleep(drvdata->enb_gpio))
148 return pwm_is_enabled(drvdata->pwm);
151 static int pwm_regulator_get_voltage(struct regulator_dev *rdev)
153 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
154 unsigned int min_uV_duty = drvdata->continuous.min_uV_dutycycle;
155 unsigned int max_uV_duty = drvdata->continuous.max_uV_dutycycle;
156 unsigned int duty_unit = drvdata->continuous.dutycycle_unit;
157 int min_uV = rdev->constraints->min_uV;
158 int max_uV = rdev->constraints->max_uV;
159 int diff_uV = max_uV - min_uV;
160 struct pwm_state pstate;
161 unsigned int diff_duty;
162 unsigned int voltage;
164 pwm_get_state(drvdata->pwm, &pstate);
166 voltage = pwm_get_relative_duty_cycle(&pstate, duty_unit);
167 if (voltage < min(max_uV_duty, min_uV_duty) ||
168 voltage > max(max_uV_duty, min_uV_duty))
169 return -ENOTRECOVERABLE;
172 * The dutycycle for min_uV might be greater than the one for max_uV.
173 * This is happening when the user needs an inversed polarity, but the
174 * PWM device does not support inversing it in hardware.
176 if (max_uV_duty < min_uV_duty) {
177 voltage = min_uV_duty - voltage;
178 diff_duty = min_uV_duty - max_uV_duty;
180 voltage = voltage - min_uV_duty;
181 diff_duty = max_uV_duty - min_uV_duty;
184 voltage = DIV_ROUND_CLOSEST_ULL((u64)voltage * diff_uV, diff_duty);
186 return voltage + min_uV;
189 static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
190 int req_min_uV, int req_max_uV,
191 unsigned int *selector)
193 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
194 unsigned int min_uV_duty = drvdata->continuous.min_uV_dutycycle;
195 unsigned int max_uV_duty = drvdata->continuous.max_uV_dutycycle;
196 unsigned int duty_unit = drvdata->continuous.dutycycle_unit;
197 int min_uV = rdev->constraints->min_uV;
198 int max_uV = rdev->constraints->max_uV;
199 int diff_uV = max_uV - min_uV;
200 struct pwm_state pstate;
201 unsigned int diff_duty;
202 unsigned int dutycycle;
205 pwm_init_state(drvdata->pwm, &pstate);
208 * The dutycycle for min_uV might be greater than the one for max_uV.
209 * This is happening when the user needs an inversed polarity, but the
210 * PWM device does not support inversing it in hardware.
212 if (max_uV_duty < min_uV_duty)
213 diff_duty = min_uV_duty - max_uV_duty;
215 diff_duty = max_uV_duty - min_uV_duty;
217 dutycycle = DIV_ROUND_CLOSEST_ULL((u64)(req_min_uV - min_uV) *
221 if (max_uV_duty < min_uV_duty)
222 dutycycle = min_uV_duty - dutycycle;
224 dutycycle = min_uV_duty + dutycycle;
226 pwm_set_relative_duty_cycle(&pstate, dutycycle, duty_unit);
228 ret = pwm_apply_state(drvdata->pwm, &pstate);
230 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
237 static struct regulator_ops pwm_regulator_voltage_table_ops = {
238 .set_voltage_sel = pwm_regulator_set_voltage_sel,
239 .get_voltage_sel = pwm_regulator_get_voltage_sel,
240 .list_voltage = pwm_regulator_list_voltage,
241 .map_voltage = regulator_map_voltage_iterate,
242 .enable = pwm_regulator_enable,
243 .disable = pwm_regulator_disable,
244 .is_enabled = pwm_regulator_is_enabled,
247 static struct regulator_ops pwm_regulator_voltage_continuous_ops = {
248 .get_voltage = pwm_regulator_get_voltage,
249 .set_voltage = pwm_regulator_set_voltage,
250 .enable = pwm_regulator_enable,
251 .disable = pwm_regulator_disable,
252 .is_enabled = pwm_regulator_is_enabled,
255 static struct regulator_desc pwm_regulator_desc = {
256 .name = "pwm-regulator",
257 .type = REGULATOR_VOLTAGE,
258 .owner = THIS_MODULE,
259 .supply_name = "pwm",
262 static int pwm_regulator_init_table(struct platform_device *pdev,
263 struct pwm_regulator_data *drvdata)
265 struct device_node *np = pdev->dev.of_node;
266 struct pwm_voltages *duty_cycle_table;
267 unsigned int length = 0;
270 of_find_property(np, "voltage-table", &length);
272 if ((length < sizeof(*duty_cycle_table)) ||
273 (length % sizeof(*duty_cycle_table))) {
274 dev_err(&pdev->dev, "voltage-table length(%d) is invalid\n",
279 duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
280 if (!duty_cycle_table)
283 ret = of_property_read_u32_array(np, "voltage-table",
284 (u32 *)duty_cycle_table,
285 length / sizeof(u32));
287 dev_err(&pdev->dev, "Failed to read voltage-table: %d\n", ret);
291 drvdata->state = -ENOTRECOVERABLE;
292 drvdata->duty_cycle_table = duty_cycle_table;
293 memcpy(&drvdata->ops, &pwm_regulator_voltage_table_ops,
294 sizeof(drvdata->ops));
295 drvdata->desc.ops = &drvdata->ops;
296 drvdata->desc.n_voltages = length / sizeof(*duty_cycle_table);
301 static int pwm_regulator_init_continuous(struct platform_device *pdev,
302 struct pwm_regulator_data *drvdata)
304 u32 dutycycle_range[2] = { 0, 100 };
305 u32 dutycycle_unit = 100;
307 memcpy(&drvdata->ops, &pwm_regulator_voltage_continuous_ops,
308 sizeof(drvdata->ops));
309 drvdata->desc.ops = &drvdata->ops;
310 drvdata->desc.continuous_voltage_range = true;
312 of_property_read_u32_array(pdev->dev.of_node,
313 "pwm-dutycycle-range",
315 of_property_read_u32(pdev->dev.of_node, "pwm-dutycycle-unit",
318 if (dutycycle_range[0] > dutycycle_unit ||
319 dutycycle_range[1] > dutycycle_unit)
322 drvdata->continuous.dutycycle_unit = dutycycle_unit;
323 drvdata->continuous.min_uV_dutycycle = dutycycle_range[0];
324 drvdata->continuous.max_uV_dutycycle = dutycycle_range[1];
329 static int pwm_regulator_probe(struct platform_device *pdev)
331 const struct regulator_init_data *init_data;
332 struct pwm_regulator_data *drvdata;
333 struct regulator_dev *regulator;
334 struct regulator_config config = { };
335 struct device_node *np = pdev->dev.of_node;
336 enum gpiod_flags gpio_flags;
340 dev_err(&pdev->dev, "Device Tree node missing\n");
344 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
348 memcpy(&drvdata->desc, &pwm_regulator_desc, sizeof(drvdata->desc));
350 if (of_find_property(np, "voltage-table", NULL))
351 ret = pwm_regulator_init_table(pdev, drvdata);
353 ret = pwm_regulator_init_continuous(pdev, drvdata);
357 init_data = of_get_regulator_init_data(&pdev->dev, np,
363 config.dev = &pdev->dev;
364 config.driver_data = drvdata;
365 config.init_data = init_data;
367 drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
368 if (IS_ERR(drvdata->pwm)) {
369 ret = PTR_ERR(drvdata->pwm);
370 dev_err(&pdev->dev, "Failed to get PWM: %d\n", ret);
374 if (init_data->constraints.boot_on || init_data->constraints.always_on)
375 gpio_flags = GPIOD_OUT_HIGH;
377 gpio_flags = GPIOD_OUT_LOW;
378 drvdata->enb_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
380 if (IS_ERR(drvdata->enb_gpio)) {
381 ret = PTR_ERR(drvdata->enb_gpio);
382 dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n", ret);
386 ret = pwm_adjust_config(drvdata->pwm);
390 regulator = devm_regulator_register(&pdev->dev,
391 &drvdata->desc, &config);
392 if (IS_ERR(regulator)) {
393 ret = PTR_ERR(regulator);
394 dev_err(&pdev->dev, "Failed to register regulator %s: %d\n",
395 drvdata->desc.name, ret);
402 static const struct of_device_id pwm_of_match[] = {
403 { .compatible = "pwm-regulator" },
406 MODULE_DEVICE_TABLE(of, pwm_of_match);
408 static struct platform_driver pwm_regulator_driver = {
410 .name = "pwm-regulator",
411 .of_match_table = of_match_ptr(pwm_of_match),
413 .probe = pwm_regulator_probe,
416 module_platform_driver(pwm_regulator_driver);
418 MODULE_LICENSE("GPL");
419 MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
420 MODULE_DESCRIPTION("PWM Regulator Driver");
421 MODULE_ALIAS("platform:pwm-regulator");