2 * Regulator driver for PWM Regulators
4 * Copyright (C) 2014 - STMicroelectronics Inc.
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 unsigned int pwm_reg_period;
66 pwm_reg_period = pwm_get_period(drvdata->pwm);
68 dutycycle = (pwm_reg_period *
69 drvdata->duty_cycle_table[selector].dutycycle) / 100;
71 ret = pwm_config(drvdata->pwm, dutycycle, pwm_reg_period);
73 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
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);
116 static int pwm_regulator_get_voltage(struct regulator_dev *rdev)
118 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
120 return drvdata->volt_uV;
123 static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
124 int min_uV, int max_uV,
127 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
128 unsigned int ramp_delay = rdev->constraints->ramp_delay;
129 unsigned int period = pwm_get_period(drvdata->pwm);
130 unsigned int req_diff = min_uV - rdev->constraints->min_uV;
132 unsigned int duty_pulse;
137 diff = rdev->constraints->max_uV - rdev->constraints->min_uV;
139 /* First try to find out if we get the iduty cycle time which is
140 * factor of PWM period time. If (request_diff_to_min * pwm_period)
141 * is perfect divided by voltage_range_diff then it is possible to
142 * get duty cycle time which is factor of PWM period. This will help
143 * to get output voltage nearer to requested value as there is no
146 req_period = req_diff * period;
147 div_u64_rem(req_period, diff, &rem);
149 do_div(req_period, diff);
150 duty_pulse = (unsigned int)req_period;
152 duty_pulse = (period / 100) * ((req_diff * 100) / diff);
155 ret = pwm_config(drvdata->pwm, duty_pulse, period);
157 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
161 ret = pwm_enable(drvdata->pwm);
163 dev_err(&rdev->dev, "Failed to enable PWM: %d\n", ret);
166 drvdata->volt_uV = min_uV;
168 /* Delay required by PWM regulator to settle to the new voltage */
169 usleep_range(ramp_delay, ramp_delay + 1000);
174 static struct regulator_ops pwm_regulator_voltage_table_ops = {
175 .set_voltage_sel = pwm_regulator_set_voltage_sel,
176 .get_voltage_sel = pwm_regulator_get_voltage_sel,
177 .list_voltage = pwm_regulator_list_voltage,
178 .map_voltage = regulator_map_voltage_iterate,
179 .enable = pwm_regulator_enable,
180 .disable = pwm_regulator_disable,
181 .is_enabled = pwm_regulator_is_enabled,
184 static struct regulator_ops pwm_regulator_voltage_continuous_ops = {
185 .get_voltage = pwm_regulator_get_voltage,
186 .set_voltage = pwm_regulator_set_voltage,
187 .enable = pwm_regulator_enable,
188 .disable = pwm_regulator_disable,
189 .is_enabled = pwm_regulator_is_enabled,
192 static struct regulator_desc pwm_regulator_desc = {
193 .name = "pwm-regulator",
194 .type = REGULATOR_VOLTAGE,
195 .owner = THIS_MODULE,
196 .supply_name = "pwm",
199 static int pwm_regulator_init_table(struct platform_device *pdev,
200 struct pwm_regulator_data *drvdata)
202 struct device_node *np = pdev->dev.of_node;
203 struct pwm_voltages *duty_cycle_table;
204 unsigned int length = 0;
207 of_find_property(np, "voltage-table", &length);
209 if ((length < sizeof(*duty_cycle_table)) ||
210 (length % sizeof(*duty_cycle_table))) {
211 dev_err(&pdev->dev, "voltage-table length(%d) is invalid\n",
216 duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
217 if (!duty_cycle_table)
220 ret = of_property_read_u32_array(np, "voltage-table",
221 (u32 *)duty_cycle_table,
222 length / sizeof(u32));
224 dev_err(&pdev->dev, "Failed to read voltage-table: %d\n", ret);
228 drvdata->duty_cycle_table = duty_cycle_table;
229 memcpy(&drvdata->ops, &pwm_regulator_voltage_table_ops,
230 sizeof(drvdata->ops));
231 drvdata->desc.ops = &drvdata->ops;
232 drvdata->desc.n_voltages = length / sizeof(*duty_cycle_table);
237 static int pwm_regulator_init_continuous(struct platform_device *pdev,
238 struct pwm_regulator_data *drvdata)
240 memcpy(&drvdata->ops, &pwm_regulator_voltage_continuous_ops,
241 sizeof(drvdata->ops));
242 drvdata->desc.ops = &drvdata->ops;
243 drvdata->desc.continuous_voltage_range = true;
248 static int pwm_regulator_probe(struct platform_device *pdev)
250 const struct regulator_init_data *init_data;
251 struct pwm_regulator_data *drvdata;
252 struct regulator_dev *regulator;
253 struct regulator_config config = { };
254 struct device_node *np = pdev->dev.of_node;
258 dev_err(&pdev->dev, "Device Tree node missing\n");
262 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
266 memcpy(&drvdata->desc, &pwm_regulator_desc, sizeof(drvdata->desc));
268 if (of_find_property(np, "voltage-table", NULL))
269 ret = pwm_regulator_init_table(pdev, drvdata);
271 ret = pwm_regulator_init_continuous(pdev, drvdata);
275 init_data = of_get_regulator_init_data(&pdev->dev, np,
281 config.dev = &pdev->dev;
282 config.driver_data = drvdata;
283 config.init_data = init_data;
285 drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
286 if (IS_ERR(drvdata->pwm)) {
287 ret = PTR_ERR(drvdata->pwm);
288 dev_err(&pdev->dev, "Failed to get PWM: %d\n", ret);
292 regulator = devm_regulator_register(&pdev->dev,
293 &drvdata->desc, &config);
294 if (IS_ERR(regulator)) {
295 ret = PTR_ERR(regulator);
296 dev_err(&pdev->dev, "Failed to register regulator %s: %d\n",
297 drvdata->desc.name, ret);
304 static const struct of_device_id pwm_of_match[] = {
305 { .compatible = "pwm-regulator" },
308 MODULE_DEVICE_TABLE(of, pwm_of_match);
310 static struct platform_driver pwm_regulator_driver = {
312 .name = "pwm-regulator",
313 .of_match_table = of_match_ptr(pwm_of_match),
315 .probe = pwm_regulator_probe,
318 module_platform_driver(pwm_regulator_driver);
320 MODULE_LICENSE("GPL");
322 MODULE_DESCRIPTION("PWM Regulator Driver");
323 MODULE_ALIAS("platform:pwm-regulator");