blob: f99a6970be29f83ae6ee13f22b91ee3ff16e4b0e [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;
Laxman Dewanganf907a0a2016-03-08 16:23:22 +053030
31 /* regulator descriptor */
32 struct regulator_desc desc;
33
34 /* Regulator ops */
35 struct regulator_ops ops;
36
Chris Zhongaa66cc62014-09-28 10:28:53 +080037 int state;
Lee Jones4773be12015-07-07 16:06:51 +010038
39 /* Continuous voltage */
Lee Jones4773be12015-07-07 16:06:51 +010040 int volt_uV;
Chris Zhongaa66cc62014-09-28 10:28:53 +080041};
42
43struct pwm_voltages {
44 unsigned int uV;
45 unsigned int dutycycle;
46};
47
Lee Jones4773be12015-07-07 16:06:51 +010048/**
49 * Voltage table call-backs
50 */
Lee Jonesab101e32015-06-05 19:42:47 +010051static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev)
Chris Zhongaa66cc62014-09-28 10:28:53 +080052{
Lee Jonesab101e32015-06-05 19:42:47 +010053 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Chris Zhongaa66cc62014-09-28 10:28:53 +080054
55 return drvdata->state;
56}
57
Lee Jonesab101e32015-06-05 19:42:47 +010058static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev,
Chris Zhongaa66cc62014-09-28 10:28:53 +080059 unsigned selector)
60{
Lee Jonesab101e32015-06-05 19:42:47 +010061 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Chris Zhongaa66cc62014-09-28 10:28:53 +080062 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) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +053073 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
Chris Zhongaa66cc62014-09-28 10:28:53 +080074 return ret;
75 }
76
77 drvdata->state = selector;
78
Chris Zhongaa66cc62014-09-28 10:28:53 +080079 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
Boris Brezillon1de7d802015-09-21 11:33:28 +020093static 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
100static 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
109static 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
Lee Jones4773be12015-07-07 16:06:51 +0100116/**
117 * Continuous voltage call-backs
118 */
Lee Jonesf3f64392015-07-09 16:35:28 +0100119static int pwm_voltage_to_duty_cycle_percentage(struct regulator_dev *rdev, int req_uV)
Lee Jones4773be12015-07-07 16:06:51 +0100120{
Lee Jonescae897d2015-07-07 16:06:52 +0100121 int min_uV = rdev->constraints->min_uV;
122 int max_uV = rdev->constraints->max_uV;
123 int diff = max_uV - min_uV;
Lee Jones4773be12015-07-07 16:06:51 +0100124
Laxman Dewangan1aaab342016-03-08 16:23:21 +0530125 return ((req_uV * 100) - (min_uV * 100)) / diff;
Lee Jones4773be12015-07-07 16:06:51 +0100126}
127
128static 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
135static 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;
Lee Jonesf3f64392015-07-09 16:35:28 +0100141 unsigned int period = pwm_get_period(drvdata->pwm);
Lee Jones4773be12015-07-07 16:06:51 +0100142 int duty_cycle;
143 int ret;
144
Lee Jonesf3f64392015-07-09 16:35:28 +0100145 duty_cycle = pwm_voltage_to_duty_cycle_percentage(rdev, min_uV);
Lee Jones4773be12015-07-07 16:06:51 +0100146
Lee Jonesf3f64392015-07-09 16:35:28 +0100147 ret = pwm_config(drvdata->pwm, (period / 100) * duty_cycle, period);
Lee Jones4773be12015-07-07 16:06:51 +0100148 if (ret) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530149 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
Lee Jones4773be12015-07-07 16:06:51 +0100150 return ret;
151 }
152
153 ret = pwm_enable(drvdata->pwm);
154 if (ret) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530155 dev_err(&rdev->dev, "Failed to enable PWM: %d\n", ret);
Lee Jones4773be12015-07-07 16:06:51 +0100156 return ret;
157 }
158 drvdata->volt_uV = min_uV;
159
160 /* Delay required by PWM regulator to settle to the new voltage */
161 usleep_range(ramp_delay, ramp_delay + 1000);
162
163 return 0;
164}
165
Lee Jonesf9178da2015-07-06 09:58:29 +0100166static struct regulator_ops pwm_regulator_voltage_table_ops = {
Chris Zhongaa66cc62014-09-28 10:28:53 +0800167 .set_voltage_sel = pwm_regulator_set_voltage_sel,
168 .get_voltage_sel = pwm_regulator_get_voltage_sel,
169 .list_voltage = pwm_regulator_list_voltage,
170 .map_voltage = regulator_map_voltage_iterate,
Boris Brezillon1de7d802015-09-21 11:33:28 +0200171 .enable = pwm_regulator_enable,
172 .disable = pwm_regulator_disable,
173 .is_enabled = pwm_regulator_is_enabled,
Chris Zhongaa66cc62014-09-28 10:28:53 +0800174};
175
Lee Jones4773be12015-07-07 16:06:51 +0100176static struct regulator_ops pwm_regulator_voltage_continuous_ops = {
177 .get_voltage = pwm_regulator_get_voltage,
178 .set_voltage = pwm_regulator_set_voltage,
Boris Brezillon1de7d802015-09-21 11:33:28 +0200179 .enable = pwm_regulator_enable,
180 .disable = pwm_regulator_disable,
181 .is_enabled = pwm_regulator_is_enabled,
Lee Jones4773be12015-07-07 16:06:51 +0100182};
183
Lee Jonesb6f55e72015-06-05 19:42:45 +0100184static struct regulator_desc pwm_regulator_desc = {
Chris Zhongaa66cc62014-09-28 10:28:53 +0800185 .name = "pwm-regulator",
Chris Zhongaa66cc62014-09-28 10:28:53 +0800186 .type = REGULATOR_VOLTAGE,
187 .owner = THIS_MODULE,
188 .supply_name = "pwm",
189};
190
Lee Jonesf9178da2015-07-06 09:58:29 +0100191static int pwm_regulator_init_table(struct platform_device *pdev,
192 struct pwm_regulator_data *drvdata)
193{
194 struct device_node *np = pdev->dev.of_node;
195 struct pwm_voltages *duty_cycle_table;
Lee Jones60cb65e2015-07-10 08:45:36 +0100196 unsigned int length = 0;
Lee Jonesf9178da2015-07-06 09:58:29 +0100197 int ret;
198
199 of_find_property(np, "voltage-table", &length);
200
201 if ((length < sizeof(*duty_cycle_table)) ||
202 (length % sizeof(*duty_cycle_table))) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530203 dev_err(&pdev->dev, "voltage-table length(%d) is invalid\n",
Lee Jonesf9178da2015-07-06 09:58:29 +0100204 length);
205 return -EINVAL;
206 }
207
208 duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
209 if (!duty_cycle_table)
210 return -ENOMEM;
211
212 ret = of_property_read_u32_array(np, "voltage-table",
213 (u32 *)duty_cycle_table,
214 length / sizeof(u32));
215 if (ret) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530216 dev_err(&pdev->dev, "Failed to read voltage-table: %d\n", ret);
Lee Jonesf9178da2015-07-06 09:58:29 +0100217 return ret;
218 }
219
220 drvdata->duty_cycle_table = duty_cycle_table;
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530221 memcpy(&drvdata->ops, &pwm_regulator_voltage_table_ops,
222 sizeof(drvdata->ops));
223 drvdata->desc.ops = &drvdata->ops;
224 drvdata->desc.n_voltages = length / sizeof(*duty_cycle_table);
Lee Jonesf9178da2015-07-06 09:58:29 +0100225
226 return 0;
227}
228
Lee Jones4773be12015-07-07 16:06:51 +0100229static int pwm_regulator_init_continuous(struct platform_device *pdev,
230 struct pwm_regulator_data *drvdata)
231{
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530232 memcpy(&drvdata->ops, &pwm_regulator_voltage_continuous_ops,
233 sizeof(drvdata->ops));
234 drvdata->desc.ops = &drvdata->ops;
235 drvdata->desc.continuous_voltage_range = true;
Lee Jones4773be12015-07-07 16:06:51 +0100236
237 return 0;
238}
239
Chris Zhongaa66cc62014-09-28 10:28:53 +0800240static int pwm_regulator_probe(struct platform_device *pdev)
241{
Lee Jones5ad2cb12015-07-07 16:06:53 +0100242 const struct regulator_init_data *init_data;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800243 struct pwm_regulator_data *drvdata;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800244 struct regulator_dev *regulator;
245 struct regulator_config config = { };
246 struct device_node *np = pdev->dev.of_node;
Lee Jonesf9178da2015-07-06 09:58:29 +0100247 int ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800248
249 if (!np) {
250 dev_err(&pdev->dev, "Device Tree node missing\n");
251 return -EINVAL;
252 }
253
254 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
255 if (!drvdata)
256 return -ENOMEM;
257
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530258 memcpy(&drvdata->desc, &pwm_regulator_desc, sizeof(drvdata->desc));
259
Lee Jones4773be12015-07-07 16:06:51 +0100260 if (of_find_property(np, "voltage-table", NULL))
Lee Jonesf9178da2015-07-06 09:58:29 +0100261 ret = pwm_regulator_init_table(pdev, drvdata);
Lee Jones4773be12015-07-07 16:06:51 +0100262 else
263 ret = pwm_regulator_init_continuous(pdev, drvdata);
264 if (ret)
265 return ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800266
Lee Jones5ad2cb12015-07-07 16:06:53 +0100267 init_data = of_get_regulator_init_data(&pdev->dev, np,
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530268 &drvdata->desc);
Lee Jones5ad2cb12015-07-07 16:06:53 +0100269 if (!init_data)
Chris Zhongaa66cc62014-09-28 10:28:53 +0800270 return -ENOMEM;
271
272 config.of_node = np;
273 config.dev = &pdev->dev;
274 config.driver_data = drvdata;
Lee Jones5ad2cb12015-07-07 16:06:53 +0100275 config.init_data = init_data;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800276
277 drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
278 if (IS_ERR(drvdata->pwm)) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530279 ret = PTR_ERR(drvdata->pwm);
280 dev_err(&pdev->dev, "Failed to get PWM: %d\n", ret);
281 return ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800282 }
283
284 regulator = devm_regulator_register(&pdev->dev,
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530285 &drvdata->desc, &config);
Chris Zhongaa66cc62014-09-28 10:28:53 +0800286 if (IS_ERR(regulator)) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530287 ret = PTR_ERR(regulator);
288 dev_err(&pdev->dev, "Failed to register regulator %s: %d\n",
289 drvdata->desc.name, ret);
290 return ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800291 }
292
293 return 0;
294}
295
296static const struct of_device_id pwm_of_match[] = {
297 { .compatible = "pwm-regulator" },
298 { },
299};
300MODULE_DEVICE_TABLE(of, pwm_of_match);
301
302static struct platform_driver pwm_regulator_driver = {
303 .driver = {
304 .name = "pwm-regulator",
Chris Zhongaa66cc62014-09-28 10:28:53 +0800305 .of_match_table = of_match_ptr(pwm_of_match),
306 },
307 .probe = pwm_regulator_probe,
308};
309
310module_platform_driver(pwm_regulator_driver);
311
312MODULE_LICENSE("GPL");
313MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
314MODULE_DESCRIPTION("PWM Regulator Driver");
315MODULE_ALIAS("platform:pwm-regulator");