blob: ffdb895ace0ad112d7bd4ebbfa99f460f98732fd [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);
Boris Brezillon8c12ad82016-04-14 21:17:27 +020062 struct pwm_args pargs;
Chris Zhongaa66cc62014-09-28 10:28:53 +080063 int dutycycle;
64 int ret;
65
Boris Brezillon8c12ad82016-04-14 21:17:27 +020066 pwm_get_args(drvdata->pwm, &pargs);
Chris Zhongaa66cc62014-09-28 10:28:53 +080067
Boris Brezillon8c12ad82016-04-14 21:17:27 +020068 dutycycle = (pargs.period *
Chris Zhongaa66cc62014-09-28 10:28:53 +080069 drvdata->duty_cycle_table[selector].dutycycle) / 100;
70
Boris Brezillon8c12ad82016-04-14 21:17:27 +020071 ret = pwm_config(drvdata->pwm, dutycycle, pargs.period);
Chris Zhongaa66cc62014-09-28 10:28:53 +080072 if (ret) {
Lee Jonesab101e32015-06-05 19:42:47 +010073 dev_err(&rdev->dev, "Failed to configure PWM\n");
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;
Boris Brezillon8c12ad82016-04-14 21:17:27 +0200141 struct pwm_args pargs;
Lee Jones4773be12015-07-07 16:06:51 +0100142 int duty_cycle;
143 int ret;
144
Boris Brezillon8c12ad82016-04-14 21:17:27 +0200145 pwm_get_args(drvdata->pwm, &pargs);
Lee Jonesf3f64392015-07-09 16:35:28 +0100146 duty_cycle = pwm_voltage_to_duty_cycle_percentage(rdev, min_uV);
Lee Jones4773be12015-07-07 16:06:51 +0100147
Boris Brezillon8c12ad82016-04-14 21:17:27 +0200148 ret = pwm_config(drvdata->pwm, (pargs.period / 100) * duty_cycle,
149 pargs.period);
Lee Jones4773be12015-07-07 16:06:51 +0100150 if (ret) {
151 dev_err(&rdev->dev, "Failed to configure PWM\n");
152 return ret;
153 }
154
155 ret = pwm_enable(drvdata->pwm);
156 if (ret) {
157 dev_err(&rdev->dev, "Failed to enable PWM\n");
158 return ret;
159 }
160 drvdata->volt_uV = min_uV;
161
162 /* Delay required by PWM regulator to settle to the new voltage */
163 usleep_range(ramp_delay, ramp_delay + 1000);
164
165 return 0;
166}
167
Lee Jonesf9178da2015-07-06 09:58:29 +0100168static struct regulator_ops pwm_regulator_voltage_table_ops = {
Chris Zhongaa66cc62014-09-28 10:28:53 +0800169 .set_voltage_sel = pwm_regulator_set_voltage_sel,
170 .get_voltage_sel = pwm_regulator_get_voltage_sel,
171 .list_voltage = pwm_regulator_list_voltage,
172 .map_voltage = regulator_map_voltage_iterate,
Boris Brezillon1de7d802015-09-21 11:33:28 +0200173 .enable = pwm_regulator_enable,
174 .disable = pwm_regulator_disable,
175 .is_enabled = pwm_regulator_is_enabled,
Chris Zhongaa66cc62014-09-28 10:28:53 +0800176};
177
Lee Jones4773be12015-07-07 16:06:51 +0100178static struct regulator_ops pwm_regulator_voltage_continuous_ops = {
179 .get_voltage = pwm_regulator_get_voltage,
180 .set_voltage = pwm_regulator_set_voltage,
Boris Brezillon1de7d802015-09-21 11:33:28 +0200181 .enable = pwm_regulator_enable,
182 .disable = pwm_regulator_disable,
183 .is_enabled = pwm_regulator_is_enabled,
Lee Jones4773be12015-07-07 16:06:51 +0100184};
185
Lee Jonesb6f55e72015-06-05 19:42:45 +0100186static struct regulator_desc pwm_regulator_desc = {
Chris Zhongaa66cc62014-09-28 10:28:53 +0800187 .name = "pwm-regulator",
Chris Zhongaa66cc62014-09-28 10:28:53 +0800188 .type = REGULATOR_VOLTAGE,
189 .owner = THIS_MODULE,
190 .supply_name = "pwm",
191};
192
Lee Jonesf9178da2015-07-06 09:58:29 +0100193static int pwm_regulator_init_table(struct platform_device *pdev,
194 struct pwm_regulator_data *drvdata)
195{
196 struct device_node *np = pdev->dev.of_node;
197 struct pwm_voltages *duty_cycle_table;
Lee Jones60cb65e2015-07-10 08:45:36 +0100198 unsigned int length = 0;
Lee Jonesf9178da2015-07-06 09:58:29 +0100199 int ret;
200
201 of_find_property(np, "voltage-table", &length);
202
203 if ((length < sizeof(*duty_cycle_table)) ||
204 (length % sizeof(*duty_cycle_table))) {
205 dev_err(&pdev->dev,
206 "voltage-table length(%d) is invalid\n",
207 length);
208 return -EINVAL;
209 }
210
211 duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
212 if (!duty_cycle_table)
213 return -ENOMEM;
214
215 ret = of_property_read_u32_array(np, "voltage-table",
216 (u32 *)duty_cycle_table,
217 length / sizeof(u32));
218 if (ret) {
219 dev_err(&pdev->dev, "Failed to read voltage-table\n");
220 return ret;
221 }
222
223 drvdata->duty_cycle_table = duty_cycle_table;
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530224 memcpy(&drvdata->ops, &pwm_regulator_voltage_table_ops,
225 sizeof(drvdata->ops));
226 drvdata->desc.ops = &drvdata->ops;
227 drvdata->desc.n_voltages = length / sizeof(*duty_cycle_table);
Lee Jonesf9178da2015-07-06 09:58:29 +0100228
229 return 0;
230}
231
Lee Jones4773be12015-07-07 16:06:51 +0100232static int pwm_regulator_init_continuous(struct platform_device *pdev,
233 struct pwm_regulator_data *drvdata)
234{
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530235 memcpy(&drvdata->ops, &pwm_regulator_voltage_continuous_ops,
236 sizeof(drvdata->ops));
237 drvdata->desc.ops = &drvdata->ops;
238 drvdata->desc.continuous_voltage_range = true;
Lee Jones4773be12015-07-07 16:06:51 +0100239
240 return 0;
241}
242
Chris Zhongaa66cc62014-09-28 10:28:53 +0800243static int pwm_regulator_probe(struct platform_device *pdev)
244{
Lee Jones5ad2cb12015-07-07 16:06:53 +0100245 const struct regulator_init_data *init_data;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800246 struct pwm_regulator_data *drvdata;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800247 struct regulator_dev *regulator;
248 struct regulator_config config = { };
249 struct device_node *np = pdev->dev.of_node;
Lee Jonesf9178da2015-07-06 09:58:29 +0100250 int ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800251
252 if (!np) {
253 dev_err(&pdev->dev, "Device Tree node missing\n");
254 return -EINVAL;
255 }
256
257 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
258 if (!drvdata)
259 return -ENOMEM;
260
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530261 memcpy(&drvdata->desc, &pwm_regulator_desc, sizeof(drvdata->desc));
262
Lee Jones4773be12015-07-07 16:06:51 +0100263 if (of_find_property(np, "voltage-table", NULL))
Lee Jonesf9178da2015-07-06 09:58:29 +0100264 ret = pwm_regulator_init_table(pdev, drvdata);
Lee Jones4773be12015-07-07 16:06:51 +0100265 else
266 ret = pwm_regulator_init_continuous(pdev, drvdata);
267 if (ret)
268 return ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800269
Lee Jones5ad2cb12015-07-07 16:06:53 +0100270 init_data = of_get_regulator_init_data(&pdev->dev, np,
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530271 &drvdata->desc);
Lee Jones5ad2cb12015-07-07 16:06:53 +0100272 if (!init_data)
Chris Zhongaa66cc62014-09-28 10:28:53 +0800273 return -ENOMEM;
274
275 config.of_node = np;
276 config.dev = &pdev->dev;
277 config.driver_data = drvdata;
Lee Jones5ad2cb12015-07-07 16:06:53 +0100278 config.init_data = init_data;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800279
280 drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
281 if (IS_ERR(drvdata->pwm)) {
282 dev_err(&pdev->dev, "Failed to get PWM\n");
283 return PTR_ERR(drvdata->pwm);
284 }
285
Boris Brezillon8c12ad82016-04-14 21:17:27 +0200286 /*
287 * FIXME: pwm_apply_args() should be removed when switching to the
288 * atomic PWM API.
289 */
290 pwm_apply_args(drvdata->pwm);
291
Chris Zhongaa66cc62014-09-28 10:28:53 +0800292 regulator = devm_regulator_register(&pdev->dev,
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530293 &drvdata->desc, &config);
Chris Zhongaa66cc62014-09-28 10:28:53 +0800294 if (IS_ERR(regulator)) {
295 dev_err(&pdev->dev, "Failed to register regulator %s\n",
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530296 drvdata->desc.name);
Chris Zhongaa66cc62014-09-28 10:28:53 +0800297 return PTR_ERR(regulator);
298 }
299
300 return 0;
301}
302
303static const struct of_device_id pwm_of_match[] = {
304 { .compatible = "pwm-regulator" },
305 { },
306};
307MODULE_DEVICE_TABLE(of, pwm_of_match);
308
309static struct platform_driver pwm_regulator_driver = {
310 .driver = {
311 .name = "pwm-regulator",
Chris Zhongaa66cc62014-09-28 10:28:53 +0800312 .of_match_table = of_match_ptr(pwm_of_match),
313 },
314 .probe = pwm_regulator_probe,
315};
316
317module_platform_driver(pwm_regulator_driver);
318
319MODULE_LICENSE("GPL");
320MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
321MODULE_DESCRIPTION("PWM Regulator Driver");
322MODULE_ALIAS("platform:pwm-regulator");