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;
32 /* Continuous voltage */
38 unsigned int dutycycle;
42 * Voltage table call-backs
44 static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev)
46 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
48 return drvdata->state;
51 static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev,
54 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
55 unsigned int pwm_reg_period;
59 pwm_reg_period = pwm_get_period(drvdata->pwm);
61 dutycycle = (pwm_reg_period *
62 drvdata->duty_cycle_table[selector].dutycycle) / 100;
64 ret = pwm_config(drvdata->pwm, dutycycle, pwm_reg_period);
66 dev_err(&rdev->dev, "Failed to configure PWM\n");
70 drvdata->state = selector;
72 ret = pwm_enable(drvdata->pwm);
74 dev_err(&rdev->dev, "Failed to enable PWM\n");
81 static int pwm_regulator_list_voltage(struct regulator_dev *rdev,
84 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
86 if (selector >= rdev->desc->n_voltages)
89 return drvdata->duty_cycle_table[selector].uV;
93 * Continuous voltage call-backs
95 static int pwm_voltage_to_duty_cycle_percentage(struct regulator_dev *rdev, int req_uV)
97 int min_uV = rdev->constraints->min_uV;
98 int max_uV = rdev->constraints->max_uV;
99 int diff = max_uV - min_uV;
101 return 100 - (((req_uV * 100) - (min_uV * 100)) / diff);
104 static int pwm_regulator_get_voltage(struct regulator_dev *rdev)
106 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
108 return drvdata->volt_uV;
111 static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
112 int min_uV, int max_uV,
115 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
116 unsigned int ramp_delay = rdev->constraints->ramp_delay;
117 unsigned int period = pwm_get_period(drvdata->pwm);
121 duty_cycle = pwm_voltage_to_duty_cycle_percentage(rdev, min_uV);
123 ret = pwm_config(drvdata->pwm, (period / 100) * duty_cycle, period);
125 dev_err(&rdev->dev, "Failed to configure PWM\n");
129 ret = pwm_enable(drvdata->pwm);
131 dev_err(&rdev->dev, "Failed to enable PWM\n");
134 drvdata->volt_uV = min_uV;
136 /* Delay required by PWM regulator to settle to the new voltage */
137 usleep_range(ramp_delay, ramp_delay + 1000);
142 static struct regulator_ops pwm_regulator_voltage_table_ops = {
143 .set_voltage_sel = pwm_regulator_set_voltage_sel,
144 .get_voltage_sel = pwm_regulator_get_voltage_sel,
145 .list_voltage = pwm_regulator_list_voltage,
146 .map_voltage = regulator_map_voltage_iterate,
149 static struct regulator_ops pwm_regulator_voltage_continuous_ops = {
150 .get_voltage = pwm_regulator_get_voltage,
151 .set_voltage = pwm_regulator_set_voltage,
154 static struct regulator_desc pwm_regulator_desc = {
155 .name = "pwm-regulator",
156 .type = REGULATOR_VOLTAGE,
157 .owner = THIS_MODULE,
158 .supply_name = "pwm",
161 static int pwm_regulator_init_table(struct platform_device *pdev,
162 struct pwm_regulator_data *drvdata)
164 struct device_node *np = pdev->dev.of_node;
165 struct pwm_voltages *duty_cycle_table;
166 unsigned int length = 0;
169 of_find_property(np, "voltage-table", &length);
171 if ((length < sizeof(*duty_cycle_table)) ||
172 (length % sizeof(*duty_cycle_table))) {
174 "voltage-table length(%d) is invalid\n",
179 duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
180 if (!duty_cycle_table)
183 ret = of_property_read_u32_array(np, "voltage-table",
184 (u32 *)duty_cycle_table,
185 length / sizeof(u32));
187 dev_err(&pdev->dev, "Failed to read voltage-table\n");
191 drvdata->duty_cycle_table = duty_cycle_table;
192 pwm_regulator_desc.ops = &pwm_regulator_voltage_table_ops;
193 pwm_regulator_desc.n_voltages = length / sizeof(*duty_cycle_table);
198 static int pwm_regulator_init_continuous(struct platform_device *pdev,
199 struct pwm_regulator_data *drvdata)
201 pwm_regulator_desc.ops = &pwm_regulator_voltage_continuous_ops;
202 pwm_regulator_desc.continuous_voltage_range = true;
207 static int pwm_regulator_probe(struct platform_device *pdev)
209 const struct regulator_init_data *init_data;
210 struct pwm_regulator_data *drvdata;
211 struct regulator_dev *regulator;
212 struct regulator_config config = { };
213 struct device_node *np = pdev->dev.of_node;
217 dev_err(&pdev->dev, "Device Tree node missing\n");
221 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
225 if (of_find_property(np, "voltage-table", NULL))
226 ret = pwm_regulator_init_table(pdev, drvdata);
228 ret = pwm_regulator_init_continuous(pdev, drvdata);
232 init_data = of_get_regulator_init_data(&pdev->dev, np,
233 &pwm_regulator_desc);
238 config.dev = &pdev->dev;
239 config.driver_data = drvdata;
240 config.init_data = init_data;
242 drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
243 if (IS_ERR(drvdata->pwm)) {
244 dev_err(&pdev->dev, "Failed to get PWM\n");
245 return PTR_ERR(drvdata->pwm);
248 regulator = devm_regulator_register(&pdev->dev,
249 &pwm_regulator_desc, &config);
250 if (IS_ERR(regulator)) {
251 dev_err(&pdev->dev, "Failed to register regulator %s\n",
252 pwm_regulator_desc.name);
253 return PTR_ERR(regulator);
259 static const struct of_device_id pwm_of_match[] = {
260 { .compatible = "pwm-regulator" },
263 MODULE_DEVICE_TABLE(of, pwm_of_match);
265 static struct platform_driver pwm_regulator_driver = {
267 .name = "pwm-regulator",
268 .of_match_table = of_match_ptr(pwm_of_match),
270 .probe = pwm_regulator_probe,
273 module_platform_driver(pwm_regulator_driver);
275 MODULE_LICENSE("GPL");
276 MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
277 MODULE_DESCRIPTION("PWM Regulator Driver");
278 MODULE_ALIAS("platform:pwm-regulator");