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 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\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 unsigned int period
= pwm_get_period(drvdata
->pwm
);
145 duty_cycle
= pwm_voltage_to_duty_cycle_percentage(rdev
, min_uV
);
147 ret
= pwm_config(drvdata
->pwm
, (period
/ 100) * duty_cycle
, period
);
149 dev_err(&rdev
->dev
, "Failed to configure PWM\n");
153 ret
= pwm_enable(drvdata
->pwm
);
155 dev_err(&rdev
->dev
, "Failed to enable PWM\n");
158 drvdata
->volt_uV
= min_uV
;
160 /* Delay required by PWM regulator to settle to the new voltage */
161 usleep_range(ramp_delay
, ramp_delay
+ 1000);
166 static struct regulator_ops pwm_regulator_voltage_table_ops
= {
167 .set_voltage_sel
= pwm_regulator_set_voltage_sel
,
168 .get_voltage_sel
= pwm_regulator_get_voltage_sel
,
169 .list_voltage
= pwm_regulator_list_voltage
,
170 .map_voltage
= regulator_map_voltage_iterate
,
171 .enable
= pwm_regulator_enable
,
172 .disable
= pwm_regulator_disable
,
173 .is_enabled
= pwm_regulator_is_enabled
,
176 static struct regulator_ops pwm_regulator_voltage_continuous_ops
= {
177 .get_voltage
= pwm_regulator_get_voltage
,
178 .set_voltage
= pwm_regulator_set_voltage
,
179 .enable
= pwm_regulator_enable
,
180 .disable
= pwm_regulator_disable
,
181 .is_enabled
= pwm_regulator_is_enabled
,
184 static struct regulator_desc pwm_regulator_desc
= {
185 .name
= "pwm-regulator",
186 .type
= REGULATOR_VOLTAGE
,
187 .owner
= THIS_MODULE
,
188 .supply_name
= "pwm",
191 static int pwm_regulator_init_table(struct platform_device
*pdev
,
192 struct pwm_regulator_data
*drvdata
)
194 struct device_node
*np
= pdev
->dev
.of_node
;
195 struct pwm_voltages
*duty_cycle_table
;
196 unsigned int length
= 0;
199 of_find_property(np
, "voltage-table", &length
);
201 if ((length
< sizeof(*duty_cycle_table
)) ||
202 (length
% sizeof(*duty_cycle_table
))) {
204 "voltage-table length(%d) is invalid\n",
209 duty_cycle_table
= devm_kzalloc(&pdev
->dev
, length
, GFP_KERNEL
);
210 if (!duty_cycle_table
)
213 ret
= of_property_read_u32_array(np
, "voltage-table",
214 (u32
*)duty_cycle_table
,
215 length
/ sizeof(u32
));
217 dev_err(&pdev
->dev
, "Failed to read voltage-table\n");
221 drvdata
->duty_cycle_table
= duty_cycle_table
;
222 memcpy(&drvdata
->ops
, &pwm_regulator_voltage_table_ops
,
223 sizeof(drvdata
->ops
));
224 drvdata
->desc
.ops
= &drvdata
->ops
;
225 drvdata
->desc
.n_voltages
= length
/ sizeof(*duty_cycle_table
);
230 static int pwm_regulator_init_continuous(struct platform_device
*pdev
,
231 struct pwm_regulator_data
*drvdata
)
233 memcpy(&drvdata
->ops
, &pwm_regulator_voltage_continuous_ops
,
234 sizeof(drvdata
->ops
));
235 drvdata
->desc
.ops
= &drvdata
->ops
;
236 drvdata
->desc
.continuous_voltage_range
= true;
241 static int pwm_regulator_probe(struct platform_device
*pdev
)
243 const struct regulator_init_data
*init_data
;
244 struct pwm_regulator_data
*drvdata
;
245 struct regulator_dev
*regulator
;
246 struct regulator_config config
= { };
247 struct device_node
*np
= pdev
->dev
.of_node
;
251 dev_err(&pdev
->dev
, "Device Tree node missing\n");
255 drvdata
= devm_kzalloc(&pdev
->dev
, sizeof(*drvdata
), GFP_KERNEL
);
259 memcpy(&drvdata
->desc
, &pwm_regulator_desc
, sizeof(drvdata
->desc
));
261 if (of_find_property(np
, "voltage-table", NULL
))
262 ret
= pwm_regulator_init_table(pdev
, drvdata
);
264 ret
= pwm_regulator_init_continuous(pdev
, drvdata
);
268 init_data
= of_get_regulator_init_data(&pdev
->dev
, np
,
274 config
.dev
= &pdev
->dev
;
275 config
.driver_data
= drvdata
;
276 config
.init_data
= init_data
;
278 drvdata
->pwm
= devm_pwm_get(&pdev
->dev
, NULL
);
279 if (IS_ERR(drvdata
->pwm
)) {
280 dev_err(&pdev
->dev
, "Failed to get PWM\n");
281 return PTR_ERR(drvdata
->pwm
);
284 regulator
= devm_regulator_register(&pdev
->dev
,
285 &drvdata
->desc
, &config
);
286 if (IS_ERR(regulator
)) {
287 dev_err(&pdev
->dev
, "Failed to register regulator %s\n",
289 return PTR_ERR(regulator
);
295 static const struct of_device_id pwm_of_match
[] = {
296 { .compatible
= "pwm-regulator" },
299 MODULE_DEVICE_TABLE(of
, pwm_of_match
);
301 static struct platform_driver pwm_regulator_driver
= {
303 .name
= "pwm-regulator",
304 .of_match_table
= of_match_ptr(pwm_of_match
),
306 .probe
= pwm_regulator_probe
,
309 module_platform_driver(pwm_regulator_driver
);
311 MODULE_LICENSE("GPL");
312 MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
313 MODULE_DESCRIPTION("PWM Regulator Driver");
314 MODULE_ALIAS("platform:pwm-regulator");