regulator: pwm: Properly initialize the ->state field
[deliverable/linux.git] / drivers / regulator / pwm-regulator.c
CommitLineData
aa66cc66
CZ
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
4773be18 13#include <linux/delay.h>
aa66cc66
CZ
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>
27bfa889 23#include <linux/gpio/consumer.h>
aa66cc66
CZ
24
25struct pwm_regulator_data {
4773be18 26 /* Shared */
aa66cc66 27 struct pwm_device *pwm;
4773be18
LJ
28
29 /* Voltage table */
30 struct pwm_voltages *duty_cycle_table;
f907a0a9
LD
31
32 /* regulator descriptor */
33 struct regulator_desc desc;
34
35 /* Regulator ops */
36 struct regulator_ops ops;
37
aa66cc66 38 int state;
4773be18
LJ
39
40 /* Continuous voltage */
4773be18 41 int volt_uV;
27bfa889
AC
42
43 /* Enable GPIO */
44 struct gpio_desc *enb_gpio;
aa66cc66
CZ
45};
46
47struct pwm_voltages {
48 unsigned int uV;
49 unsigned int dutycycle;
50};
51
4773be18
LJ
52/**
53 * Voltage table call-backs
54 */
87248991
BB
55static void pwm_regulator_init_state(struct regulator_dev *rdev)
56{
57 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
58 struct pwm_state pwm_state;
59 unsigned int dutycycle;
60 int i;
61
62 pwm_get_state(drvdata->pwm, &pwm_state);
63 dutycycle = pwm_get_relative_duty_cycle(&pwm_state, 100);
64
65 for (i = 0; i < rdev->desc->n_voltages; i++) {
66 if (dutycycle == drvdata->duty_cycle_table[i].dutycycle) {
67 drvdata->state = i;
68 return;
69 }
70 }
71}
72
ab101e35 73static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev)
aa66cc66 74{
ab101e35 75 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
aa66cc66 76
87248991
BB
77 if (drvdata->state < 0)
78 pwm_regulator_init_state(rdev);
79
aa66cc66
CZ
80 return drvdata->state;
81}
82
ab101e35 83static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev,
aa66cc66
CZ
84 unsigned selector)
85{
ab101e35 86 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
3f4eb39b 87 struct pwm_state pstate;
aa66cc66
CZ
88 int ret;
89
3f4eb39b
BB
90 pwm_init_state(drvdata->pwm, &pstate);
91 pwm_set_relative_duty_cycle(&pstate,
92 drvdata->duty_cycle_table[selector].dutycycle, 100);
aa66cc66 93
3f4eb39b 94 ret = pwm_apply_state(drvdata->pwm, &pstate);
aa66cc66 95 if (ret) {
5bf59bd5 96 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
aa66cc66
CZ
97 return ret;
98 }
99
100 drvdata->state = selector;
101
aa66cc66
CZ
102 return 0;
103}
104
ab101e35 105static int pwm_regulator_list_voltage(struct regulator_dev *rdev,
aa66cc66
CZ
106 unsigned selector)
107{
ab101e35 108 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
aa66cc66 109
ab101e35 110 if (selector >= rdev->desc->n_voltages)
aa66cc66
CZ
111 return -EINVAL;
112
113 return drvdata->duty_cycle_table[selector].uV;
114}
4773be18 115
1de7d802
BB
116static int pwm_regulator_enable(struct regulator_dev *dev)
117{
118 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
119
27bfa889
AC
120 if (drvdata->enb_gpio)
121 gpiod_set_value_cansleep(drvdata->enb_gpio, 1);
122
1de7d802
BB
123 return pwm_enable(drvdata->pwm);
124}
125
126static int pwm_regulator_disable(struct regulator_dev *dev)
127{
128 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
129
130 pwm_disable(drvdata->pwm);
131
27bfa889
AC
132 if (drvdata->enb_gpio)
133 gpiod_set_value_cansleep(drvdata->enb_gpio, 0);
134
1de7d802
BB
135 return 0;
136}
137
138static int pwm_regulator_is_enabled(struct regulator_dev *dev)
139{
140 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
141
27bfa889
AC
142 if (drvdata->enb_gpio && !gpiod_get_value_cansleep(drvdata->enb_gpio))
143 return false;
144
1de7d802
BB
145 return pwm_is_enabled(drvdata->pwm);
146}
147
4773be18
LJ
148static int pwm_regulator_get_voltage(struct regulator_dev *rdev)
149{
150 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
151
152 return drvdata->volt_uV;
153}
154
155static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
156 int min_uV, int max_uV,
157 unsigned *selector)
158{
159 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
160 unsigned int ramp_delay = rdev->constraints->ramp_delay;
fd786fb0 161 unsigned int req_diff = min_uV - rdev->constraints->min_uV;
3f4eb39b 162 struct pwm_state pstate;
fd786fb0 163 unsigned int diff;
c2588393 164 int old_uV = pwm_regulator_get_voltage(rdev);
4773be18
LJ
165 int ret;
166
3f4eb39b 167 pwm_init_state(drvdata->pwm, &pstate);
fd786fb0
LD
168 diff = rdev->constraints->max_uV - rdev->constraints->min_uV;
169
3f4eb39b
BB
170 /* We pass diff as the scale to get a uV precision. */
171 pwm_set_relative_duty_cycle(&pstate, req_diff, diff);
4773be18 172
3f4eb39b 173 ret = pwm_apply_state(drvdata->pwm, &pstate);
4773be18 174 if (ret) {
5bf59bd5 175 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
4773be18
LJ
176 return ret;
177 }
178
4773be18
LJ
179 drvdata->volt_uV = min_uV;
180
c2588393
DA
181 if ((ramp_delay == 0) || !pwm_regulator_is_enabled(rdev))
182 return 0;
183
184 /* Ramp delay is in uV/uS. Adjust to uS and delay */
185 ramp_delay = DIV_ROUND_UP(abs(min_uV - old_uV), ramp_delay);
186 usleep_range(ramp_delay, ramp_delay + DIV_ROUND_UP(ramp_delay, 10));
4773be18
LJ
187
188 return 0;
189}
190
f9178dad 191static struct regulator_ops pwm_regulator_voltage_table_ops = {
aa66cc66
CZ
192 .set_voltage_sel = pwm_regulator_set_voltage_sel,
193 .get_voltage_sel = pwm_regulator_get_voltage_sel,
194 .list_voltage = pwm_regulator_list_voltage,
195 .map_voltage = regulator_map_voltage_iterate,
1de7d802
BB
196 .enable = pwm_regulator_enable,
197 .disable = pwm_regulator_disable,
198 .is_enabled = pwm_regulator_is_enabled,
aa66cc66
CZ
199};
200
4773be18
LJ
201static struct regulator_ops pwm_regulator_voltage_continuous_ops = {
202 .get_voltage = pwm_regulator_get_voltage,
203 .set_voltage = pwm_regulator_set_voltage,
1de7d802
BB
204 .enable = pwm_regulator_enable,
205 .disable = pwm_regulator_disable,
206 .is_enabled = pwm_regulator_is_enabled,
4773be18
LJ
207};
208
b6f55e74 209static struct regulator_desc pwm_regulator_desc = {
aa66cc66 210 .name = "pwm-regulator",
aa66cc66
CZ
211 .type = REGULATOR_VOLTAGE,
212 .owner = THIS_MODULE,
213 .supply_name = "pwm",
214};
215
f9178dad
LJ
216static int pwm_regulator_init_table(struct platform_device *pdev,
217 struct pwm_regulator_data *drvdata)
218{
219 struct device_node *np = pdev->dev.of_node;
220 struct pwm_voltages *duty_cycle_table;
60cb65eb 221 unsigned int length = 0;
f9178dad
LJ
222 int ret;
223
224 of_find_property(np, "voltage-table", &length);
225
226 if ((length < sizeof(*duty_cycle_table)) ||
227 (length % sizeof(*duty_cycle_table))) {
5bf59bd5 228 dev_err(&pdev->dev, "voltage-table length(%d) is invalid\n",
f9178dad
LJ
229 length);
230 return -EINVAL;
231 }
232
233 duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
234 if (!duty_cycle_table)
235 return -ENOMEM;
236
237 ret = of_property_read_u32_array(np, "voltage-table",
238 (u32 *)duty_cycle_table,
239 length / sizeof(u32));
240 if (ret) {
5bf59bd5 241 dev_err(&pdev->dev, "Failed to read voltage-table: %d\n", ret);
f9178dad
LJ
242 return ret;
243 }
244
87248991 245 drvdata->state = -EINVAL;
f9178dad 246 drvdata->duty_cycle_table = duty_cycle_table;
f907a0a9
LD
247 memcpy(&drvdata->ops, &pwm_regulator_voltage_table_ops,
248 sizeof(drvdata->ops));
249 drvdata->desc.ops = &drvdata->ops;
250 drvdata->desc.n_voltages = length / sizeof(*duty_cycle_table);
f9178dad
LJ
251
252 return 0;
253}
254
4773be18
LJ
255static int pwm_regulator_init_continuous(struct platform_device *pdev,
256 struct pwm_regulator_data *drvdata)
257{
f907a0a9
LD
258 memcpy(&drvdata->ops, &pwm_regulator_voltage_continuous_ops,
259 sizeof(drvdata->ops));
260 drvdata->desc.ops = &drvdata->ops;
261 drvdata->desc.continuous_voltage_range = true;
4773be18
LJ
262
263 return 0;
264}
265
aa66cc66
CZ
266static int pwm_regulator_probe(struct platform_device *pdev)
267{
5ad2cb14 268 const struct regulator_init_data *init_data;
aa66cc66 269 struct pwm_regulator_data *drvdata;
aa66cc66
CZ
270 struct regulator_dev *regulator;
271 struct regulator_config config = { };
272 struct device_node *np = pdev->dev.of_node;
27bfa889 273 enum gpiod_flags gpio_flags;
f9178dad 274 int ret;
aa66cc66
CZ
275
276 if (!np) {
277 dev_err(&pdev->dev, "Device Tree node missing\n");
278 return -EINVAL;
279 }
280
281 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
282 if (!drvdata)
283 return -ENOMEM;
284
f907a0a9
LD
285 memcpy(&drvdata->desc, &pwm_regulator_desc, sizeof(drvdata->desc));
286
4773be18 287 if (of_find_property(np, "voltage-table", NULL))
f9178dad 288 ret = pwm_regulator_init_table(pdev, drvdata);
4773be18
LJ
289 else
290 ret = pwm_regulator_init_continuous(pdev, drvdata);
291 if (ret)
292 return ret;
aa66cc66 293
5ad2cb14 294 init_data = of_get_regulator_init_data(&pdev->dev, np,
f907a0a9 295 &drvdata->desc);
5ad2cb14 296 if (!init_data)
aa66cc66
CZ
297 return -ENOMEM;
298
299 config.of_node = np;
300 config.dev = &pdev->dev;
301 config.driver_data = drvdata;
5ad2cb14 302 config.init_data = init_data;
aa66cc66
CZ
303
304 drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
305 if (IS_ERR(drvdata->pwm)) {
5bf59bd5
LD
306 ret = PTR_ERR(drvdata->pwm);
307 dev_err(&pdev->dev, "Failed to get PWM: %d\n", ret);
308 return ret;
aa66cc66
CZ
309 }
310
27bfa889
AC
311 if (init_data->constraints.boot_on || init_data->constraints.always_on)
312 gpio_flags = GPIOD_OUT_HIGH;
313 else
314 gpio_flags = GPIOD_OUT_LOW;
315 drvdata->enb_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
316 gpio_flags);
317 if (IS_ERR(drvdata->enb_gpio)) {
318 ret = PTR_ERR(drvdata->enb_gpio);
319 dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n", ret);
320 return ret;
321 }
322
fd4f99c4
BB
323 ret = pwm_adjust_config(drvdata->pwm);
324 if (ret)
325 return ret;
8c12ad8e 326
aa66cc66 327 regulator = devm_regulator_register(&pdev->dev,
f907a0a9 328 &drvdata->desc, &config);
aa66cc66 329 if (IS_ERR(regulator)) {
5bf59bd5
LD
330 ret = PTR_ERR(regulator);
331 dev_err(&pdev->dev, "Failed to register regulator %s: %d\n",
332 drvdata->desc.name, ret);
333 return ret;
aa66cc66
CZ
334 }
335
336 return 0;
337}
338
339static const struct of_device_id pwm_of_match[] = {
340 { .compatible = "pwm-regulator" },
341 { },
342};
343MODULE_DEVICE_TABLE(of, pwm_of_match);
344
345static struct platform_driver pwm_regulator_driver = {
346 .driver = {
347 .name = "pwm-regulator",
aa66cc66
CZ
348 .of_match_table = of_match_ptr(pwm_of_match),
349 },
350 .probe = pwm_regulator_probe,
351};
352
353module_platform_driver(pwm_regulator_driver);
354
355MODULE_LICENSE("GPL");
356MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
357MODULE_DESCRIPTION("PWM Regulator Driver");
358MODULE_ALIAS("platform:pwm-regulator");
This page took 0.095014 seconds and 5 git commands to generate.