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/delay.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/err.h>
17 #include <linux/regulator/driver.h>
18 #include <linux/regulator/machine.h>
19 #include <linux/regulator/of_regulator.h>
21 #include <linux/of_device.h>
22 #include <linux/pwm.h>
24 struct pwm_regulator_data {
26 struct pwm_device *pwm;
29 struct pwm_voltages *duty_cycle_table;
31 /* regulator descriptor */
32 struct regulator_desc desc;
35 struct regulator_ops ops;
39 /* Continuous voltage */
45 unsigned int dutycycle;
49 * Voltage table call-backs
51 static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev)
53 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
55 return drvdata->state;
58 static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev,
61 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
62 struct pwm_args pargs;
66 pwm_get_args(drvdata->pwm, &pargs);
68 dutycycle = (pargs.period *
69 drvdata->duty_cycle_table[selector].dutycycle) / 100;
71 ret = pwm_config(drvdata->pwm, dutycycle, pargs.period);
73 dev_err(&rdev->dev, "Failed to configure PWM\n");
77 drvdata->state = selector;
82 static int pwm_regulator_list_voltage(struct regulator_dev *rdev,
85 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
87 if (selector >= rdev->desc->n_voltages)
90 return drvdata->duty_cycle_table[selector].uV;
93 static int pwm_regulator_enable(struct regulator_dev *dev)
95 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
97 return pwm_enable(drvdata->pwm);
100 static int pwm_regulator_disable(struct regulator_dev *dev)
102 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
104 pwm_disable(drvdata->pwm);
109 static int pwm_regulator_is_enabled(struct regulator_dev *dev)
111 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
113 return pwm_is_enabled(drvdata->pwm);
117 * Continuous voltage call-backs
119 static int pwm_voltage_to_duty_cycle_percentage(struct regulator_dev *rdev, int req_uV)
121 int min_uV = rdev->constraints->min_uV;
122 int max_uV = rdev->constraints->max_uV;
123 int diff = max_uV - min_uV;
125 return ((req_uV * 100) - (min_uV * 100)) / diff;
128 static int pwm_regulator_get_voltage(struct regulator_dev *rdev)
130 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
132 return drvdata->volt_uV;
135 static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
136 int min_uV, int max_uV,
139 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
140 unsigned int ramp_delay = rdev->constraints->ramp_delay;
141 struct pwm_args pargs;
145 pwm_get_args(drvdata->pwm, &pargs);
146 duty_cycle = pwm_voltage_to_duty_cycle_percentage(rdev, min_uV);
148 ret = pwm_config(drvdata->pwm, (pargs.period / 100) * duty_cycle,
151 dev_err(&rdev->dev, "Failed to configure PWM\n");
155 ret = pwm_enable(drvdata->pwm);
157 dev_err(&rdev->dev, "Failed to enable PWM\n");
160 drvdata->volt_uV = min_uV;
162 /* Delay required by PWM regulator to settle to the new voltage */
163 usleep_range(ramp_delay, ramp_delay + 1000);
168 static struct regulator_ops pwm_regulator_voltage_table_ops = {
169 .set_voltage_sel = pwm_regulator_set_voltage_sel,
170 .get_voltage_sel = pwm_regulator_get_voltage_sel,
171 .list_voltage = pwm_regulator_list_voltage,
172 .map_voltage = regulator_map_voltage_iterate,
173 .enable = pwm_regulator_enable,
174 .disable = pwm_regulator_disable,
175 .is_enabled = pwm_regulator_is_enabled,
178 static struct regulator_ops pwm_regulator_voltage_continuous_ops = {
179 .get_voltage = pwm_regulator_get_voltage,
180 .set_voltage = pwm_regulator_set_voltage,
181 .enable = pwm_regulator_enable,
182 .disable = pwm_regulator_disable,
183 .is_enabled = pwm_regulator_is_enabled,
186 static struct regulator_desc pwm_regulator_desc = {
187 .name = "pwm-regulator",
188 .type = REGULATOR_VOLTAGE,
189 .owner = THIS_MODULE,
190 .supply_name = "pwm",
193 static int pwm_regulator_init_table(struct platform_device *pdev,
194 struct pwm_regulator_data *drvdata)
196 struct device_node *np = pdev->dev.of_node;
197 struct pwm_voltages *duty_cycle_table;
198 unsigned int length = 0;
201 of_find_property(np, "voltage-table", &length);
203 if ((length < sizeof(*duty_cycle_table)) ||
204 (length % sizeof(*duty_cycle_table))) {
206 "voltage-table length(%d) is invalid\n",
211 duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
212 if (!duty_cycle_table)
215 ret = of_property_read_u32_array(np, "voltage-table",
216 (u32 *)duty_cycle_table,
217 length / sizeof(u32));
219 dev_err(&pdev->dev, "Failed to read voltage-table\n");
223 drvdata->duty_cycle_table = duty_cycle_table;
224 memcpy(&drvdata->ops, &pwm_regulator_voltage_table_ops,
225 sizeof(drvdata->ops));
226 drvdata->desc.ops = &drvdata->ops;
227 drvdata->desc.n_voltages = length / sizeof(*duty_cycle_table);
232 static int pwm_regulator_init_continuous(struct platform_device *pdev,
233 struct pwm_regulator_data *drvdata)
235 memcpy(&drvdata->ops, &pwm_regulator_voltage_continuous_ops,
236 sizeof(drvdata->ops));
237 drvdata->desc.ops = &drvdata->ops;
238 drvdata->desc.continuous_voltage_range = true;
243 static int pwm_regulator_probe(struct platform_device *pdev)
245 const struct regulator_init_data *init_data;
246 struct pwm_regulator_data *drvdata;
247 struct regulator_dev *regulator;
248 struct regulator_config config = { };
249 struct device_node *np = pdev->dev.of_node;
253 dev_err(&pdev->dev, "Device Tree node missing\n");
257 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
261 memcpy(&drvdata->desc, &pwm_regulator_desc, sizeof(drvdata->desc));
263 if (of_find_property(np, "voltage-table", NULL))
264 ret = pwm_regulator_init_table(pdev, drvdata);
266 ret = pwm_regulator_init_continuous(pdev, drvdata);
270 init_data = of_get_regulator_init_data(&pdev->dev, np,
276 config.dev = &pdev->dev;
277 config.driver_data = drvdata;
278 config.init_data = init_data;
280 drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
281 if (IS_ERR(drvdata->pwm)) {
282 dev_err(&pdev->dev, "Failed to get PWM\n");
283 return PTR_ERR(drvdata->pwm);
287 * FIXME: pwm_apply_args() should be removed when switching to the
290 pwm_apply_args(drvdata->pwm);
292 regulator = devm_regulator_register(&pdev->dev,
293 &drvdata->desc, &config);
294 if (IS_ERR(regulator)) {
295 dev_err(&pdev->dev, "Failed to register regulator %s\n",
297 return PTR_ERR(regulator);
303 static const struct of_device_id pwm_of_match[] = {
304 { .compatible = "pwm-regulator" },
307 MODULE_DEVICE_TABLE(of, pwm_of_match);
309 static struct platform_driver pwm_regulator_driver = {
311 .name = "pwm-regulator",
312 .of_match_table = of_match_ptr(pwm_of_match),
314 .probe = pwm_regulator_probe,
317 module_platform_driver(pwm_regulator_driver);
319 MODULE_LICENSE("GPL");
320 MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
321 MODULE_DESCRIPTION("PWM Regulator Driver");
322 MODULE_ALIAS("platform:pwm-regulator");