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>
23 struct pwm_regulator_data
{
24 struct regulator_desc desc
;
25 struct pwm_voltages
*duty_cycle_table
;
26 struct pwm_device
*pwm
;
33 unsigned int dutycycle
;
36 static int pwm_regulator_get_voltage_sel(struct regulator_dev
*dev
)
38 struct pwm_regulator_data
*drvdata
= rdev_get_drvdata(dev
);
40 return drvdata
->state
;
43 static int pwm_regulator_set_voltage_sel(struct regulator_dev
*dev
,
46 struct pwm_regulator_data
*drvdata
= rdev_get_drvdata(dev
);
47 unsigned int pwm_reg_period
;
51 pwm_reg_period
= pwm_get_period(drvdata
->pwm
);
53 dutycycle
= (pwm_reg_period
*
54 drvdata
->duty_cycle_table
[selector
].dutycycle
) / 100;
56 ret
= pwm_config(drvdata
->pwm
, dutycycle
, pwm_reg_period
);
58 dev_err(&dev
->dev
, "Failed to configure PWM\n");
62 drvdata
->state
= selector
;
64 if (!drvdata
->enabled
) {
65 ret
= pwm_enable(drvdata
->pwm
);
67 dev_err(&dev
->dev
, "Failed to enable PWM\n");
70 drvdata
->enabled
= true;
76 static int pwm_regulator_list_voltage(struct regulator_dev
*dev
,
79 struct pwm_regulator_data
*drvdata
= rdev_get_drvdata(dev
);
81 if (selector
>= drvdata
->desc
.n_voltages
)
84 return drvdata
->duty_cycle_table
[selector
].uV
;
87 static struct regulator_ops pwm_regulator_voltage_ops
= {
88 .set_voltage_sel
= pwm_regulator_set_voltage_sel
,
89 .get_voltage_sel
= pwm_regulator_get_voltage_sel
,
90 .list_voltage
= pwm_regulator_list_voltage
,
91 .map_voltage
= regulator_map_voltage_iterate
,
94 static const struct regulator_desc pwm_regulator_desc
= {
95 .name
= "pwm-regulator",
96 .ops
= &pwm_regulator_voltage_ops
,
97 .type
= REGULATOR_VOLTAGE
,
102 static int pwm_regulator_probe(struct platform_device
*pdev
)
104 struct pwm_regulator_data
*drvdata
;
105 struct property
*prop
;
106 struct regulator_dev
*regulator
;
107 struct regulator_config config
= { };
108 struct device_node
*np
= pdev
->dev
.of_node
;
112 dev_err(&pdev
->dev
, "Device Tree node missing\n");
116 drvdata
= devm_kzalloc(&pdev
->dev
, sizeof(*drvdata
), GFP_KERNEL
);
120 memcpy(&drvdata
->desc
, &pwm_regulator_desc
, sizeof(pwm_regulator_desc
));
122 /* determine the number of voltage-table */
123 prop
= of_find_property(np
, "voltage-table", &length
);
125 dev_err(&pdev
->dev
, "No voltage-table\n");
129 if ((length
< sizeof(*drvdata
->duty_cycle_table
)) ||
130 (length
% sizeof(*drvdata
->duty_cycle_table
))) {
131 dev_err(&pdev
->dev
, "voltage-table length(%d) is invalid\n",
136 drvdata
->desc
.n_voltages
= length
/ sizeof(*drvdata
->duty_cycle_table
);
138 drvdata
->duty_cycle_table
= devm_kzalloc(&pdev
->dev
,
140 if (!drvdata
->duty_cycle_table
)
143 /* read voltage table from DT property */
144 ret
= of_property_read_u32_array(np
, "voltage-table",
145 (u32
*)drvdata
->duty_cycle_table
,
146 length
/ sizeof(u32
));
148 dev_err(&pdev
->dev
, "read voltage-table failed\n");
152 config
.init_data
= of_get_regulator_init_data(&pdev
->dev
, np
,
154 if (!config
.init_data
)
158 config
.dev
= &pdev
->dev
;
159 config
.driver_data
= drvdata
;
161 drvdata
->pwm
= devm_pwm_get(&pdev
->dev
, NULL
);
162 if (IS_ERR(drvdata
->pwm
)) {
163 dev_err(&pdev
->dev
, "Failed to get PWM\n");
164 return PTR_ERR(drvdata
->pwm
);
167 regulator
= devm_regulator_register(&pdev
->dev
,
168 &drvdata
->desc
, &config
);
169 if (IS_ERR(regulator
)) {
170 dev_err(&pdev
->dev
, "Failed to register regulator %s\n",
172 return PTR_ERR(regulator
);
178 static const struct of_device_id pwm_of_match
[] = {
179 { .compatible
= "pwm-regulator" },
182 MODULE_DEVICE_TABLE(of
, pwm_of_match
);
184 static struct platform_driver pwm_regulator_driver
= {
186 .name
= "pwm-regulator",
187 .of_match_table
= of_match_ptr(pwm_of_match
),
189 .probe
= pwm_regulator_probe
,
192 module_platform_driver(pwm_regulator_driver
);
194 MODULE_LICENSE("GPL");
195 MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
196 MODULE_DESCRIPTION("PWM Regulator Driver");
197 MODULE_ALIAS("platform:pwm-regulator");