regulator: pwm: Use pwm_get_args() where appropriate
[deliverable/linux.git] / drivers / regulator / pwm-regulator.c
1 /*
2 * Regulator driver for PWM Regulators
3 *
4 * Copyright (C) 2014 - STMicroelectronics Inc.
5 *
6 * Author: Lee Jones <lee.jones@linaro.org>
7 *
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.
11 */
12
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>
20 #include <linux/of.h>
21 #include <linux/of_device.h>
22 #include <linux/pwm.h>
23
24 struct pwm_regulator_data {
25 /* Shared */
26 struct pwm_device *pwm;
27
28 /* Voltage table */
29 struct pwm_voltages *duty_cycle_table;
30
31 /* regulator descriptor */
32 struct regulator_desc desc;
33
34 /* Regulator ops */
35 struct regulator_ops ops;
36
37 int state;
38
39 /* Continuous voltage */
40 int volt_uV;
41 };
42
43 struct pwm_voltages {
44 unsigned int uV;
45 unsigned int dutycycle;
46 };
47
48 /**
49 * Voltage table call-backs
50 */
51 static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev)
52 {
53 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
54
55 return drvdata->state;
56 }
57
58 static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev,
59 unsigned selector)
60 {
61 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
62 struct pwm_args pargs;
63 int dutycycle;
64 int ret;
65
66 pwm_get_args(drvdata->pwm, &pargs);
67
68 dutycycle = (pargs.period *
69 drvdata->duty_cycle_table[selector].dutycycle) / 100;
70
71 ret = pwm_config(drvdata->pwm, dutycycle, pargs.period);
72 if (ret) {
73 dev_err(&rdev->dev, "Failed to configure PWM\n");
74 return ret;
75 }
76
77 drvdata->state = selector;
78
79 return 0;
80 }
81
82 static int pwm_regulator_list_voltage(struct regulator_dev *rdev,
83 unsigned selector)
84 {
85 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
86
87 if (selector >= rdev->desc->n_voltages)
88 return -EINVAL;
89
90 return drvdata->duty_cycle_table[selector].uV;
91 }
92
93 static int pwm_regulator_enable(struct regulator_dev *dev)
94 {
95 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
96
97 return pwm_enable(drvdata->pwm);
98 }
99
100 static int pwm_regulator_disable(struct regulator_dev *dev)
101 {
102 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
103
104 pwm_disable(drvdata->pwm);
105
106 return 0;
107 }
108
109 static int pwm_regulator_is_enabled(struct regulator_dev *dev)
110 {
111 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
112
113 return pwm_is_enabled(drvdata->pwm);
114 }
115
116 /**
117 * Continuous voltage call-backs
118 */
119 static int pwm_voltage_to_duty_cycle_percentage(struct regulator_dev *rdev, int req_uV)
120 {
121 int min_uV = rdev->constraints->min_uV;
122 int max_uV = rdev->constraints->max_uV;
123 int diff = max_uV - min_uV;
124
125 return ((req_uV * 100) - (min_uV * 100)) / diff;
126 }
127
128 static int pwm_regulator_get_voltage(struct regulator_dev *rdev)
129 {
130 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
131
132 return drvdata->volt_uV;
133 }
134
135 static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
136 int min_uV, int max_uV,
137 unsigned *selector)
138 {
139 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
140 unsigned int ramp_delay = rdev->constraints->ramp_delay;
141 struct pwm_args pargs;
142 int duty_cycle;
143 int ret;
144
145 pwm_get_args(drvdata->pwm, &pargs);
146 duty_cycle = pwm_voltage_to_duty_cycle_percentage(rdev, min_uV);
147
148 ret = pwm_config(drvdata->pwm, (pargs.period / 100) * duty_cycle,
149 pargs.period);
150 if (ret) {
151 dev_err(&rdev->dev, "Failed to configure PWM\n");
152 return ret;
153 }
154
155 ret = pwm_enable(drvdata->pwm);
156 if (ret) {
157 dev_err(&rdev->dev, "Failed to enable PWM\n");
158 return ret;
159 }
160 drvdata->volt_uV = min_uV;
161
162 /* Delay required by PWM regulator to settle to the new voltage */
163 usleep_range(ramp_delay, ramp_delay + 1000);
164
165 return 0;
166 }
167
168 static struct regulator_ops pwm_regulator_voltage_table_ops = {
169 .set_voltage_sel = pwm_regulator_set_voltage_sel,
170 .get_voltage_sel = pwm_regulator_get_voltage_sel,
171 .list_voltage = pwm_regulator_list_voltage,
172 .map_voltage = regulator_map_voltage_iterate,
173 .enable = pwm_regulator_enable,
174 .disable = pwm_regulator_disable,
175 .is_enabled = pwm_regulator_is_enabled,
176 };
177
178 static struct regulator_ops pwm_regulator_voltage_continuous_ops = {
179 .get_voltage = pwm_regulator_get_voltage,
180 .set_voltage = pwm_regulator_set_voltage,
181 .enable = pwm_regulator_enable,
182 .disable = pwm_regulator_disable,
183 .is_enabled = pwm_regulator_is_enabled,
184 };
185
186 static struct regulator_desc pwm_regulator_desc = {
187 .name = "pwm-regulator",
188 .type = REGULATOR_VOLTAGE,
189 .owner = THIS_MODULE,
190 .supply_name = "pwm",
191 };
192
193 static int pwm_regulator_init_table(struct platform_device *pdev,
194 struct pwm_regulator_data *drvdata)
195 {
196 struct device_node *np = pdev->dev.of_node;
197 struct pwm_voltages *duty_cycle_table;
198 unsigned int length = 0;
199 int ret;
200
201 of_find_property(np, "voltage-table", &length);
202
203 if ((length < sizeof(*duty_cycle_table)) ||
204 (length % sizeof(*duty_cycle_table))) {
205 dev_err(&pdev->dev,
206 "voltage-table length(%d) is invalid\n",
207 length);
208 return -EINVAL;
209 }
210
211 duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
212 if (!duty_cycle_table)
213 return -ENOMEM;
214
215 ret = of_property_read_u32_array(np, "voltage-table",
216 (u32 *)duty_cycle_table,
217 length / sizeof(u32));
218 if (ret) {
219 dev_err(&pdev->dev, "Failed to read voltage-table\n");
220 return ret;
221 }
222
223 drvdata->duty_cycle_table = duty_cycle_table;
224 memcpy(&drvdata->ops, &pwm_regulator_voltage_table_ops,
225 sizeof(drvdata->ops));
226 drvdata->desc.ops = &drvdata->ops;
227 drvdata->desc.n_voltages = length / sizeof(*duty_cycle_table);
228
229 return 0;
230 }
231
232 static int pwm_regulator_init_continuous(struct platform_device *pdev,
233 struct pwm_regulator_data *drvdata)
234 {
235 memcpy(&drvdata->ops, &pwm_regulator_voltage_continuous_ops,
236 sizeof(drvdata->ops));
237 drvdata->desc.ops = &drvdata->ops;
238 drvdata->desc.continuous_voltage_range = true;
239
240 return 0;
241 }
242
243 static int pwm_regulator_probe(struct platform_device *pdev)
244 {
245 const struct regulator_init_data *init_data;
246 struct pwm_regulator_data *drvdata;
247 struct regulator_dev *regulator;
248 struct regulator_config config = { };
249 struct device_node *np = pdev->dev.of_node;
250 int ret;
251
252 if (!np) {
253 dev_err(&pdev->dev, "Device Tree node missing\n");
254 return -EINVAL;
255 }
256
257 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
258 if (!drvdata)
259 return -ENOMEM;
260
261 memcpy(&drvdata->desc, &pwm_regulator_desc, sizeof(drvdata->desc));
262
263 if (of_find_property(np, "voltage-table", NULL))
264 ret = pwm_regulator_init_table(pdev, drvdata);
265 else
266 ret = pwm_regulator_init_continuous(pdev, drvdata);
267 if (ret)
268 return ret;
269
270 init_data = of_get_regulator_init_data(&pdev->dev, np,
271 &drvdata->desc);
272 if (!init_data)
273 return -ENOMEM;
274
275 config.of_node = np;
276 config.dev = &pdev->dev;
277 config.driver_data = drvdata;
278 config.init_data = init_data;
279
280 drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
281 if (IS_ERR(drvdata->pwm)) {
282 dev_err(&pdev->dev, "Failed to get PWM\n");
283 return PTR_ERR(drvdata->pwm);
284 }
285
286 /*
287 * FIXME: pwm_apply_args() should be removed when switching to the
288 * atomic PWM API.
289 */
290 pwm_apply_args(drvdata->pwm);
291
292 regulator = devm_regulator_register(&pdev->dev,
293 &drvdata->desc, &config);
294 if (IS_ERR(regulator)) {
295 dev_err(&pdev->dev, "Failed to register regulator %s\n",
296 drvdata->desc.name);
297 return PTR_ERR(regulator);
298 }
299
300 return 0;
301 }
302
303 static const struct of_device_id pwm_of_match[] = {
304 { .compatible = "pwm-regulator" },
305 { },
306 };
307 MODULE_DEVICE_TABLE(of, pwm_of_match);
308
309 static struct platform_driver pwm_regulator_driver = {
310 .driver = {
311 .name = "pwm-regulator",
312 .of_match_table = of_match_ptr(pwm_of_match),
313 },
314 .probe = pwm_regulator_probe,
315 };
316
317 module_platform_driver(pwm_regulator_driver);
318
319 MODULE_LICENSE("GPL");
320 MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
321 MODULE_DESCRIPTION("PWM Regulator Driver");
322 MODULE_ALIAS("platform:pwm-regulator");
This page took 0.038549 seconds and 5 git commands to generate.