blob: dac145db305c77568ab29d46fb86477d9391a1d4 [file] [log] [blame]
Chris Zhongaa66cc62014-09-28 10:28:53 +08001/*
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
Lee Jones4773be12015-07-07 16:06:51 +010013#include <linux/delay.h>
Chris Zhongaa66cc62014-09-28 10:28:53 +080014#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 {
Lee Jones4773be12015-07-07 16:06:51 +010025 /* Shared */
Chris Zhongaa66cc62014-09-28 10:28:53 +080026 struct pwm_device *pwm;
Lee Jones4773be12015-07-07 16:06:51 +010027
28 /* Voltage table */
29 struct pwm_voltages *duty_cycle_table;
Chris Zhongaa66cc62014-09-28 10:28:53 +080030 int state;
Lee Jones4773be12015-07-07 16:06:51 +010031
32 /* Continuous voltage */
33 u32 max_duty_cycle;
34 int volt_uV;
Chris Zhongaa66cc62014-09-28 10:28:53 +080035};
36
37struct pwm_voltages {
38 unsigned int uV;
39 unsigned int dutycycle;
40};
41
Lee Jones4773be12015-07-07 16:06:51 +010042/**
43 * Voltage table call-backs
44 */
Lee Jonesab101e32015-06-05 19:42:47 +010045static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev)
Chris Zhongaa66cc62014-09-28 10:28:53 +080046{
Lee Jonesab101e32015-06-05 19:42:47 +010047 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Chris Zhongaa66cc62014-09-28 10:28:53 +080048
49 return drvdata->state;
50}
51
Lee Jonesab101e32015-06-05 19:42:47 +010052static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev,
Chris Zhongaa66cc62014-09-28 10:28:53 +080053 unsigned selector)
54{
Lee Jonesab101e32015-06-05 19:42:47 +010055 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Chris Zhongaa66cc62014-09-28 10:28:53 +080056 unsigned int pwm_reg_period;
57 int dutycycle;
58 int ret;
59
60 pwm_reg_period = pwm_get_period(drvdata->pwm);
61
62 dutycycle = (pwm_reg_period *
63 drvdata->duty_cycle_table[selector].dutycycle) / 100;
64
65 ret = pwm_config(drvdata->pwm, dutycycle, pwm_reg_period);
66 if (ret) {
Lee Jonesab101e32015-06-05 19:42:47 +010067 dev_err(&rdev->dev, "Failed to configure PWM\n");
Chris Zhongaa66cc62014-09-28 10:28:53 +080068 return ret;
69 }
70
71 drvdata->state = selector;
72
Lee Jonesc779ceb2015-06-05 19:42:46 +010073 ret = pwm_enable(drvdata->pwm);
74 if (ret) {
Lee Jonesab101e32015-06-05 19:42:47 +010075 dev_err(&rdev->dev, "Failed to enable PWM\n");
Lee Jonesc779ceb2015-06-05 19:42:46 +010076 return ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +080077 }
78
79 return 0;
80}
81
Lee Jonesab101e32015-06-05 19:42:47 +010082static int pwm_regulator_list_voltage(struct regulator_dev *rdev,
Chris Zhongaa66cc62014-09-28 10:28:53 +080083 unsigned selector)
84{
Lee Jonesab101e32015-06-05 19:42:47 +010085 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Chris Zhongaa66cc62014-09-28 10:28:53 +080086
Lee Jonesab101e32015-06-05 19:42:47 +010087 if (selector >= rdev->desc->n_voltages)
Chris Zhongaa66cc62014-09-28 10:28:53 +080088 return -EINVAL;
89
90 return drvdata->duty_cycle_table[selector].uV;
91}
Lee Jones4773be12015-07-07 16:06:51 +010092
93/**
94 * Continuous voltage call-backs
95 */
96static int pwm_voltage_to_duty_cycle(struct regulator_dev *rdev,
97 int volt_mV)
98{
99 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
100 int min_mV = rdev->constraints->min_uV / 1000;
101 int max_mV = rdev->constraints->max_uV / 1000;
102 int max_duty_cycle = drvdata->max_duty_cycle;
103 int vdiff = min_mV - max_mV;
104 int pwm_code;
105 int tmp;
106
107 tmp = ((max_duty_cycle - min_mV) * max_duty_cycle) / vdiff;
108 pwm_code = ((tmp + max_duty_cycle) * volt_mV) / vdiff;
109
110 if (pwm_code < 0)
111 pwm_code = 0;
112 if (pwm_code > max_duty_cycle)
113 pwm_code = max_duty_cycle;
114
115 return pwm_code * 100 / max_duty_cycle;
116}
117
118static int pwm_regulator_get_voltage(struct regulator_dev *rdev)
119{
120 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
121
122 return drvdata->volt_uV;
123}
124
125static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
126 int min_uV, int max_uV,
127 unsigned *selector)
128{
129 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
130 unsigned int ramp_delay = rdev->constraints->ramp_delay;
131 int duty_cycle;
132 int ret;
133
134 duty_cycle = pwm_voltage_to_duty_cycle(rdev, min_uV / 1000);
135
136 ret = pwm_config(drvdata->pwm,
137 (drvdata->pwm->period / 100) * duty_cycle,
138 drvdata->pwm->period);
139 if (ret) {
140 dev_err(&rdev->dev, "Failed to configure PWM\n");
141 return ret;
142 }
143
144 ret = pwm_enable(drvdata->pwm);
145 if (ret) {
146 dev_err(&rdev->dev, "Failed to enable PWM\n");
147 return ret;
148 }
149 drvdata->volt_uV = min_uV;
150
151 /* Delay required by PWM regulator to settle to the new voltage */
152 usleep_range(ramp_delay, ramp_delay + 1000);
153
154 return 0;
155}
156
Lee Jonesf9178da2015-07-06 09:58:29 +0100157static struct regulator_ops pwm_regulator_voltage_table_ops = {
Chris Zhongaa66cc62014-09-28 10:28:53 +0800158 .set_voltage_sel = pwm_regulator_set_voltage_sel,
159 .get_voltage_sel = pwm_regulator_get_voltage_sel,
160 .list_voltage = pwm_regulator_list_voltage,
161 .map_voltage = regulator_map_voltage_iterate,
162};
163
Lee Jones4773be12015-07-07 16:06:51 +0100164static struct regulator_ops pwm_regulator_voltage_continuous_ops = {
165 .get_voltage = pwm_regulator_get_voltage,
166 .set_voltage = pwm_regulator_set_voltage,
167};
168
Lee Jonesb6f55e72015-06-05 19:42:45 +0100169static struct regulator_desc pwm_regulator_desc = {
Chris Zhongaa66cc62014-09-28 10:28:53 +0800170 .name = "pwm-regulator",
Chris Zhongaa66cc62014-09-28 10:28:53 +0800171 .type = REGULATOR_VOLTAGE,
172 .owner = THIS_MODULE,
173 .supply_name = "pwm",
174};
175
Lee Jonesf9178da2015-07-06 09:58:29 +0100176static int pwm_regulator_init_table(struct platform_device *pdev,
177 struct pwm_regulator_data *drvdata)
178{
179 struct device_node *np = pdev->dev.of_node;
180 struct pwm_voltages *duty_cycle_table;
181 int length;
182 int ret;
183
184 of_find_property(np, "voltage-table", &length);
185
186 if ((length < sizeof(*duty_cycle_table)) ||
187 (length % sizeof(*duty_cycle_table))) {
188 dev_err(&pdev->dev,
189 "voltage-table length(%d) is invalid\n",
190 length);
191 return -EINVAL;
192 }
193
194 duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
195 if (!duty_cycle_table)
196 return -ENOMEM;
197
198 ret = of_property_read_u32_array(np, "voltage-table",
199 (u32 *)duty_cycle_table,
200 length / sizeof(u32));
201 if (ret) {
202 dev_err(&pdev->dev, "Failed to read voltage-table\n");
203 return ret;
204 }
205
206 drvdata->duty_cycle_table = duty_cycle_table;
207 pwm_regulator_desc.ops = &pwm_regulator_voltage_table_ops;
208 pwm_regulator_desc.n_voltages = length / sizeof(*duty_cycle_table);
209
210 return 0;
211}
212
Lee Jones4773be12015-07-07 16:06:51 +0100213static int pwm_regulator_init_continuous(struct platform_device *pdev,
214 struct pwm_regulator_data *drvdata)
215{
216 struct device_node *np = pdev->dev.of_node;
217 int ret;
218
219 ret = of_property_read_u32(np, "max-duty-cycle",
220 &drvdata->max_duty_cycle);
221 if (ret) {
222 dev_err(&pdev->dev, "Failed to read \"pwm-max-value\"\n");
223 return ret;
224 }
225
226 pwm_regulator_desc.ops = &pwm_regulator_voltage_continuous_ops;
227 pwm_regulator_desc.continuous_voltage_range = true;
228
229 return 0;
230}
231
Chris Zhongaa66cc62014-09-28 10:28:53 +0800232static int pwm_regulator_probe(struct platform_device *pdev)
233{
234 struct pwm_regulator_data *drvdata;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800235 struct regulator_dev *regulator;
236 struct regulator_config config = { };
237 struct device_node *np = pdev->dev.of_node;
Lee Jonesf9178da2015-07-06 09:58:29 +0100238 int ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800239
240 if (!np) {
241 dev_err(&pdev->dev, "Device Tree node missing\n");
242 return -EINVAL;
243 }
244
245 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
246 if (!drvdata)
247 return -ENOMEM;
248
Lee Jones4773be12015-07-07 16:06:51 +0100249 if (of_find_property(np, "voltage-table", NULL))
Lee Jonesf9178da2015-07-06 09:58:29 +0100250 ret = pwm_regulator_init_table(pdev, drvdata);
Lee Jones4773be12015-07-07 16:06:51 +0100251 else
252 ret = pwm_regulator_init_continuous(pdev, drvdata);
253 if (ret)
254 return ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800255
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100256 config.init_data = of_get_regulator_init_data(&pdev->dev, np,
Lee Jonesb6f55e72015-06-05 19:42:45 +0100257 &pwm_regulator_desc);
Chris Zhongaa66cc62014-09-28 10:28:53 +0800258 if (!config.init_data)
259 return -ENOMEM;
260
261 config.of_node = np;
262 config.dev = &pdev->dev;
263 config.driver_data = drvdata;
264
265 drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
266 if (IS_ERR(drvdata->pwm)) {
267 dev_err(&pdev->dev, "Failed to get PWM\n");
268 return PTR_ERR(drvdata->pwm);
269 }
270
271 regulator = devm_regulator_register(&pdev->dev,
Lee Jonesb6f55e72015-06-05 19:42:45 +0100272 &pwm_regulator_desc, &config);
Chris Zhongaa66cc62014-09-28 10:28:53 +0800273 if (IS_ERR(regulator)) {
274 dev_err(&pdev->dev, "Failed to register regulator %s\n",
Lee Jonesb6f55e72015-06-05 19:42:45 +0100275 pwm_regulator_desc.name);
Chris Zhongaa66cc62014-09-28 10:28:53 +0800276 return PTR_ERR(regulator);
277 }
278
279 return 0;
280}
281
282static const struct of_device_id pwm_of_match[] = {
283 { .compatible = "pwm-regulator" },
284 { },
285};
286MODULE_DEVICE_TABLE(of, pwm_of_match);
287
288static struct platform_driver pwm_regulator_driver = {
289 .driver = {
290 .name = "pwm-regulator",
Chris Zhongaa66cc62014-09-28 10:28:53 +0800291 .of_match_table = of_match_ptr(pwm_of_match),
292 },
293 .probe = pwm_regulator_probe,
294};
295
296module_platform_driver(pwm_regulator_driver);
297
298MODULE_LICENSE("GPL");
299MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
300MODULE_DESCRIPTION("PWM Regulator Driver");
301MODULE_ALIAS("platform:pwm-regulator");