regulator: pwm: Try to avoid voltage error in duty cycle calculation
[cascardo/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         unsigned int pwm_reg_period;
63         int dutycycle;
64         int ret;
65
66         pwm_reg_period = pwm_get_period(drvdata->pwm);
67
68         dutycycle = (pwm_reg_period *
69                     drvdata->duty_cycle_table[selector].dutycycle) / 100;
70
71         ret = pwm_config(drvdata->pwm, dutycycle, pwm_reg_period);
72         if (ret) {
73                 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
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 static int pwm_regulator_get_voltage(struct regulator_dev *rdev)
117 {
118         struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
119
120         return drvdata->volt_uV;
121 }
122
123 static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
124                                         int min_uV, int max_uV,
125                                         unsigned *selector)
126 {
127         struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
128         unsigned int ramp_delay = rdev->constraints->ramp_delay;
129         unsigned int period = pwm_get_period(drvdata->pwm);
130         unsigned int req_diff = min_uV - rdev->constraints->min_uV;
131         unsigned int diff;
132         unsigned int duty_pulse;
133         u64 req_period;
134         u32 rem;
135         int ret;
136
137         diff = rdev->constraints->max_uV - rdev->constraints->min_uV;
138
139         /* First try to find out if we get the iduty cycle time which is
140          * factor of PWM period time. If (request_diff_to_min * pwm_period)
141          * is perfect divided by voltage_range_diff then it is possible to
142          * get duty cycle time which is factor of PWM period. This will help
143          * to get output voltage nearer to requested value as there is no
144          * calculation loss.
145          */
146         req_period = req_diff * period;
147         div_u64_rem(req_period, diff, &rem);
148         if (!rem) {
149                 do_div(req_period, diff);
150                 duty_pulse = (unsigned int)req_period;
151         } else {
152                 duty_pulse = (period / 100) * ((req_diff * 100) / diff);
153         }
154
155         ret = pwm_config(drvdata->pwm, duty_pulse, period);
156         if (ret) {
157                 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
158                 return ret;
159         }
160
161         ret = pwm_enable(drvdata->pwm);
162         if (ret) {
163                 dev_err(&rdev->dev, "Failed to enable PWM: %d\n", ret);
164                 return ret;
165         }
166         drvdata->volt_uV = min_uV;
167
168         /* Delay required by PWM regulator to settle to the new voltage */
169         usleep_range(ramp_delay, ramp_delay + 1000);
170
171         return 0;
172 }
173
174 static struct regulator_ops pwm_regulator_voltage_table_ops = {
175         .set_voltage_sel = pwm_regulator_set_voltage_sel,
176         .get_voltage_sel = pwm_regulator_get_voltage_sel,
177         .list_voltage    = pwm_regulator_list_voltage,
178         .map_voltage     = regulator_map_voltage_iterate,
179         .enable          = pwm_regulator_enable,
180         .disable         = pwm_regulator_disable,
181         .is_enabled      = pwm_regulator_is_enabled,
182 };
183
184 static struct regulator_ops pwm_regulator_voltage_continuous_ops = {
185         .get_voltage = pwm_regulator_get_voltage,
186         .set_voltage = pwm_regulator_set_voltage,
187         .enable          = pwm_regulator_enable,
188         .disable         = pwm_regulator_disable,
189         .is_enabled      = pwm_regulator_is_enabled,
190 };
191
192 static struct regulator_desc pwm_regulator_desc = {
193         .name           = "pwm-regulator",
194         .type           = REGULATOR_VOLTAGE,
195         .owner          = THIS_MODULE,
196         .supply_name    = "pwm",
197 };
198
199 static int pwm_regulator_init_table(struct platform_device *pdev,
200                                     struct pwm_regulator_data *drvdata)
201 {
202         struct device_node *np = pdev->dev.of_node;
203         struct pwm_voltages *duty_cycle_table;
204         unsigned int length = 0;
205         int ret;
206
207         of_find_property(np, "voltage-table", &length);
208
209         if ((length < sizeof(*duty_cycle_table)) ||
210             (length % sizeof(*duty_cycle_table))) {
211                 dev_err(&pdev->dev, "voltage-table length(%d) is invalid\n",
212                         length);
213                 return -EINVAL;
214         }
215
216         duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
217         if (!duty_cycle_table)
218                 return -ENOMEM;
219
220         ret = of_property_read_u32_array(np, "voltage-table",
221                                          (u32 *)duty_cycle_table,
222                                          length / sizeof(u32));
223         if (ret) {
224                 dev_err(&pdev->dev, "Failed to read voltage-table: %d\n", ret);
225                 return ret;
226         }
227
228         drvdata->duty_cycle_table       = duty_cycle_table;
229         memcpy(&drvdata->ops, &pwm_regulator_voltage_table_ops,
230                sizeof(drvdata->ops));
231         drvdata->desc.ops = &drvdata->ops;
232         drvdata->desc.n_voltages        = length / sizeof(*duty_cycle_table);
233
234         return 0;
235 }
236
237 static int pwm_regulator_init_continuous(struct platform_device *pdev,
238                                          struct pwm_regulator_data *drvdata)
239 {
240         memcpy(&drvdata->ops, &pwm_regulator_voltage_continuous_ops,
241                sizeof(drvdata->ops));
242         drvdata->desc.ops = &drvdata->ops;
243         drvdata->desc.continuous_voltage_range = true;
244
245         return 0;
246 }
247
248 static int pwm_regulator_probe(struct platform_device *pdev)
249 {
250         const struct regulator_init_data *init_data;
251         struct pwm_regulator_data *drvdata;
252         struct regulator_dev *regulator;
253         struct regulator_config config = { };
254         struct device_node *np = pdev->dev.of_node;
255         int ret;
256
257         if (!np) {
258                 dev_err(&pdev->dev, "Device Tree node missing\n");
259                 return -EINVAL;
260         }
261
262         drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
263         if (!drvdata)
264                 return -ENOMEM;
265
266         memcpy(&drvdata->desc, &pwm_regulator_desc, sizeof(drvdata->desc));
267
268         if (of_find_property(np, "voltage-table", NULL))
269                 ret = pwm_regulator_init_table(pdev, drvdata);
270         else
271                 ret = pwm_regulator_init_continuous(pdev, drvdata);
272         if (ret)
273                 return ret;
274
275         init_data = of_get_regulator_init_data(&pdev->dev, np,
276                                                &drvdata->desc);
277         if (!init_data)
278                 return -ENOMEM;
279
280         config.of_node = np;
281         config.dev = &pdev->dev;
282         config.driver_data = drvdata;
283         config.init_data = init_data;
284
285         drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
286         if (IS_ERR(drvdata->pwm)) {
287                 ret = PTR_ERR(drvdata->pwm);
288                 dev_err(&pdev->dev, "Failed to get PWM: %d\n", ret);
289                 return ret;
290         }
291
292         regulator = devm_regulator_register(&pdev->dev,
293                                             &drvdata->desc, &config);
294         if (IS_ERR(regulator)) {
295                 ret = PTR_ERR(regulator);
296                 dev_err(&pdev->dev, "Failed to register regulator %s: %d\n",
297                         drvdata->desc.name, ret);
298                 return ret;
299         }
300
301         return 0;
302 }
303
304 static const struct of_device_id pwm_of_match[] = {
305         { .compatible = "pwm-regulator" },
306         { },
307 };
308 MODULE_DEVICE_TABLE(of, pwm_of_match);
309
310 static struct platform_driver pwm_regulator_driver = {
311         .driver = {
312                 .name           = "pwm-regulator",
313                 .of_match_table = of_match_ptr(pwm_of_match),
314         },
315         .probe = pwm_regulator_probe,
316 };
317
318 module_platform_driver(pwm_regulator_driver);
319
320 MODULE_LICENSE("GPL");
321 MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
322 MODULE_DESCRIPTION("PWM Regulator Driver");
323 MODULE_ALIAS("platform:pwm-regulator");