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