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
;
46 struct gpio_desc
*enb_gpio
;
51 unsigned int dutycycle
;
55 * Voltage table call-backs
57 static void pwm_regulator_init_state(struct regulator_dev
*rdev
)
59 struct pwm_regulator_data
*drvdata
= rdev_get_drvdata(rdev
);
60 struct pwm_state pwm_state
;
61 unsigned int dutycycle
;
64 pwm_get_state(drvdata
->pwm
, &pwm_state
);
65 dutycycle
= pwm_get_relative_duty_cycle(&pwm_state
, 100);
67 for (i
= 0; i
< rdev
->desc
->n_voltages
; i
++) {
68 if (dutycycle
== drvdata
->duty_cycle_table
[i
].dutycycle
) {
75 static int pwm_regulator_get_voltage_sel(struct regulator_dev
*rdev
)
77 struct pwm_regulator_data
*drvdata
= rdev_get_drvdata(rdev
);
79 if (drvdata
->state
< 0)
80 pwm_regulator_init_state(rdev
);
82 return drvdata
->state
;
85 static int pwm_regulator_set_voltage_sel(struct regulator_dev
*rdev
,
88 struct pwm_regulator_data
*drvdata
= rdev_get_drvdata(rdev
);
89 struct pwm_state pstate
;
92 pwm_init_state(drvdata
->pwm
, &pstate
);
93 pwm_set_relative_duty_cycle(&pstate
,
94 drvdata
->duty_cycle_table
[selector
].dutycycle
, 100);
96 ret
= pwm_apply_state(drvdata
->pwm
, &pstate
);
98 dev_err(&rdev
->dev
, "Failed to configure PWM: %d\n", ret
);
102 drvdata
->state
= selector
;
107 static int pwm_regulator_list_voltage(struct regulator_dev
*rdev
,
110 struct pwm_regulator_data
*drvdata
= rdev_get_drvdata(rdev
);
112 if (selector
>= rdev
->desc
->n_voltages
)
115 return drvdata
->duty_cycle_table
[selector
].uV
;
118 static int pwm_regulator_enable(struct regulator_dev
*dev
)
120 struct pwm_regulator_data
*drvdata
= rdev_get_drvdata(dev
);
122 gpiod_set_value_cansleep(drvdata
->enb_gpio
, 1);
124 return pwm_enable(drvdata
->pwm
);
127 static int pwm_regulator_disable(struct regulator_dev
*dev
)
129 struct pwm_regulator_data
*drvdata
= rdev_get_drvdata(dev
);
131 pwm_disable(drvdata
->pwm
);
133 gpiod_set_value_cansleep(drvdata
->enb_gpio
, 0);
138 static int pwm_regulator_is_enabled(struct regulator_dev
*dev
)
140 struct pwm_regulator_data
*drvdata
= rdev_get_drvdata(dev
);
142 if (drvdata
->enb_gpio
&& !gpiod_get_value_cansleep(drvdata
->enb_gpio
))
145 return pwm_is_enabled(drvdata
->pwm
);
148 static int pwm_regulator_get_voltage(struct regulator_dev
*rdev
)
150 struct pwm_regulator_data
*drvdata
= rdev_get_drvdata(rdev
);
151 unsigned int min_uV_duty
= drvdata
->continuous
.min_uV_dutycycle
;
152 unsigned int max_uV_duty
= drvdata
->continuous
.max_uV_dutycycle
;
153 unsigned int duty_unit
= drvdata
->continuous
.dutycycle_unit
;
154 int min_uV
= rdev
->constraints
->min_uV
;
155 int max_uV
= rdev
->constraints
->max_uV
;
156 int diff_uV
= max_uV
- min_uV
;
157 struct pwm_state pstate
;
158 unsigned int diff_duty
;
159 unsigned int voltage
;
161 pwm_get_state(drvdata
->pwm
, &pstate
);
163 voltage
= pwm_get_relative_duty_cycle(&pstate
, duty_unit
);
166 * The dutycycle for min_uV might be greater than the one for max_uV.
167 * This is happening when the user needs an inversed polarity, but the
168 * PWM device does not support inversing it in hardware.
170 if (max_uV_duty
< min_uV_duty
) {
171 voltage
= min_uV_duty
- voltage
;
172 diff_duty
= min_uV_duty
- max_uV_duty
;
174 voltage
= voltage
- min_uV_duty
;
175 diff_duty
= max_uV_duty
- min_uV_duty
;
178 voltage
= DIV_ROUND_CLOSEST_ULL((u64
)voltage
* diff_uV
, diff_duty
);
180 return voltage
+ min_uV
;
183 static int pwm_regulator_set_voltage(struct regulator_dev
*rdev
,
184 int req_min_uV
, int req_max_uV
,
185 unsigned int *selector
)
187 struct pwm_regulator_data
*drvdata
= rdev_get_drvdata(rdev
);
188 unsigned int min_uV_duty
= drvdata
->continuous
.min_uV_dutycycle
;
189 unsigned int max_uV_duty
= drvdata
->continuous
.max_uV_dutycycle
;
190 unsigned int duty_unit
= drvdata
->continuous
.dutycycle_unit
;
191 int min_uV
= rdev
->constraints
->min_uV
;
192 int max_uV
= rdev
->constraints
->max_uV
;
193 int diff_uV
= max_uV
- min_uV
;
194 struct pwm_state pstate
;
195 unsigned int diff_duty
;
196 unsigned int dutycycle
;
199 pwm_init_state(drvdata
->pwm
, &pstate
);
202 * The dutycycle for min_uV might be greater than the one for max_uV.
203 * This is happening when the user needs an inversed polarity, but the
204 * PWM device does not support inversing it in hardware.
206 if (max_uV_duty
< min_uV_duty
)
207 diff_duty
= min_uV_duty
- max_uV_duty
;
209 diff_duty
= max_uV_duty
- min_uV_duty
;
211 dutycycle
= DIV_ROUND_CLOSEST_ULL((u64
)(req_min_uV
- min_uV
) *
215 if (max_uV_duty
< min_uV_duty
)
216 dutycycle
= min_uV_duty
- dutycycle
;
218 dutycycle
= min_uV_duty
+ dutycycle
;
220 pwm_set_relative_duty_cycle(&pstate
, dutycycle
, duty_unit
);
222 ret
= pwm_apply_state(drvdata
->pwm
, &pstate
);
224 dev_err(&rdev
->dev
, "Failed to configure PWM: %d\n", ret
);
231 static const struct regulator_ops pwm_regulator_voltage_table_ops
= {
232 .set_voltage_sel
= pwm_regulator_set_voltage_sel
,
233 .get_voltage_sel
= pwm_regulator_get_voltage_sel
,
234 .list_voltage
= pwm_regulator_list_voltage
,
235 .map_voltage
= regulator_map_voltage_iterate
,
236 .enable
= pwm_regulator_enable
,
237 .disable
= pwm_regulator_disable
,
238 .is_enabled
= pwm_regulator_is_enabled
,
241 static const struct regulator_ops pwm_regulator_voltage_continuous_ops
= {
242 .get_voltage
= pwm_regulator_get_voltage
,
243 .set_voltage
= pwm_regulator_set_voltage
,
244 .enable
= pwm_regulator_enable
,
245 .disable
= pwm_regulator_disable
,
246 .is_enabled
= pwm_regulator_is_enabled
,
249 static const struct regulator_desc pwm_regulator_desc
= {
250 .name
= "pwm-regulator",
251 .type
= REGULATOR_VOLTAGE
,
252 .owner
= THIS_MODULE
,
253 .supply_name
= "pwm",
256 static int pwm_regulator_init_table(struct platform_device
*pdev
,
257 struct pwm_regulator_data
*drvdata
)
259 struct device_node
*np
= pdev
->dev
.of_node
;
260 struct pwm_voltages
*duty_cycle_table
;
261 unsigned int length
= 0;
264 of_find_property(np
, "voltage-table", &length
);
266 if ((length
< sizeof(*duty_cycle_table
)) ||
267 (length
% sizeof(*duty_cycle_table
))) {
268 dev_err(&pdev
->dev
, "voltage-table length(%d) is invalid\n",
273 duty_cycle_table
= devm_kzalloc(&pdev
->dev
, length
, GFP_KERNEL
);
274 if (!duty_cycle_table
)
277 ret
= of_property_read_u32_array(np
, "voltage-table",
278 (u32
*)duty_cycle_table
,
279 length
/ sizeof(u32
));
281 dev_err(&pdev
->dev
, "Failed to read voltage-table: %d\n", ret
);
285 drvdata
->state
= -EINVAL
;
286 drvdata
->duty_cycle_table
= duty_cycle_table
;
287 drvdata
->desc
.ops
= &pwm_regulator_voltage_table_ops
;
288 drvdata
->desc
.n_voltages
= length
/ sizeof(*duty_cycle_table
);
293 static int pwm_regulator_init_continuous(struct platform_device
*pdev
,
294 struct pwm_regulator_data
*drvdata
)
296 u32 dutycycle_range
[2] = { 0, 100 };
297 u32 dutycycle_unit
= 100;
299 drvdata
->desc
.ops
= &pwm_regulator_voltage_continuous_ops
;
300 drvdata
->desc
.continuous_voltage_range
= true;
302 of_property_read_u32_array(pdev
->dev
.of_node
,
303 "pwm-dutycycle-range",
305 of_property_read_u32(pdev
->dev
.of_node
, "pwm-dutycycle-unit",
308 if (dutycycle_range
[0] > dutycycle_unit
||
309 dutycycle_range
[1] > dutycycle_unit
)
312 drvdata
->continuous
.dutycycle_unit
= dutycycle_unit
;
313 drvdata
->continuous
.min_uV_dutycycle
= dutycycle_range
[0];
314 drvdata
->continuous
.max_uV_dutycycle
= dutycycle_range
[1];
319 static int pwm_regulator_probe(struct platform_device
*pdev
)
321 const struct regulator_init_data
*init_data
;
322 struct pwm_regulator_data
*drvdata
;
323 struct regulator_dev
*regulator
;
324 struct regulator_config config
= { };
325 struct device_node
*np
= pdev
->dev
.of_node
;
326 enum gpiod_flags gpio_flags
;
330 dev_err(&pdev
->dev
, "Device Tree node missing\n");
334 drvdata
= devm_kzalloc(&pdev
->dev
, sizeof(*drvdata
), GFP_KERNEL
);
338 memcpy(&drvdata
->desc
, &pwm_regulator_desc
, sizeof(drvdata
->desc
));
340 if (of_find_property(np
, "voltage-table", NULL
))
341 ret
= pwm_regulator_init_table(pdev
, drvdata
);
343 ret
= pwm_regulator_init_continuous(pdev
, drvdata
);
347 init_data
= of_get_regulator_init_data(&pdev
->dev
, np
,
353 config
.dev
= &pdev
->dev
;
354 config
.driver_data
= drvdata
;
355 config
.init_data
= init_data
;
357 drvdata
->pwm
= devm_pwm_get(&pdev
->dev
, NULL
);
358 if (IS_ERR(drvdata
->pwm
)) {
359 ret
= PTR_ERR(drvdata
->pwm
);
360 dev_err(&pdev
->dev
, "Failed to get PWM: %d\n", ret
);
364 if (init_data
->constraints
.boot_on
|| init_data
->constraints
.always_on
)
365 gpio_flags
= GPIOD_OUT_HIGH
;
367 gpio_flags
= GPIOD_OUT_LOW
;
368 drvdata
->enb_gpio
= devm_gpiod_get_optional(&pdev
->dev
, "enable",
370 if (IS_ERR(drvdata
->enb_gpio
)) {
371 ret
= PTR_ERR(drvdata
->enb_gpio
);
372 dev_err(&pdev
->dev
, "Failed to get enable GPIO: %d\n", ret
);
376 ret
= pwm_adjust_config(drvdata
->pwm
);
380 regulator
= devm_regulator_register(&pdev
->dev
,
381 &drvdata
->desc
, &config
);
382 if (IS_ERR(regulator
)) {
383 ret
= PTR_ERR(regulator
);
384 dev_err(&pdev
->dev
, "Failed to register regulator %s: %d\n",
385 drvdata
->desc
.name
, ret
);
392 static const struct of_device_id pwm_of_match
[] = {
393 { .compatible
= "pwm-regulator" },
396 MODULE_DEVICE_TABLE(of
, pwm_of_match
);
398 static struct platform_driver pwm_regulator_driver
= {
400 .name
= "pwm-regulator",
401 .of_match_table
= of_match_ptr(pwm_of_match
),
403 .probe
= pwm_regulator_probe
,
406 module_platform_driver(pwm_regulator_driver
);
408 MODULE_LICENSE("GPL");
409 MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
410 MODULE_DESCRIPTION("PWM Regulator Driver");
411 MODULE_ALIAS("platform:pwm-regulator");