blob: ae0bebbfda9db0a4b1809cd6447e0506719ab480 [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>
Alexandre Courbot27bfa882016-06-23 16:39:44 +090023#include <linux/gpio/consumer.h>
Chris Zhongaa66cc62014-09-28 10:28:53 +080024
25struct pwm_regulator_data {
Lee Jones4773be12015-07-07 16:06:51 +010026 /* Shared */
Chris Zhongaa66cc62014-09-28 10:28:53 +080027 struct pwm_device *pwm;
Lee Jones4773be12015-07-07 16:06:51 +010028
29 /* Voltage table */
30 struct pwm_voltages *duty_cycle_table;
Laxman Dewanganf907a0a2016-03-08 16:23:22 +053031
32 /* regulator descriptor */
33 struct regulator_desc desc;
34
35 /* Regulator ops */
36 struct regulator_ops ops;
37
Chris Zhongaa66cc62014-09-28 10:28:53 +080038 int state;
Lee Jones4773be12015-07-07 16:06:51 +010039
Alexandre Courbot27bfa882016-06-23 16:39:44 +090040 /* Enable GPIO */
41 struct gpio_desc *enb_gpio;
Chris Zhongaa66cc62014-09-28 10:28:53 +080042};
43
44struct pwm_voltages {
45 unsigned int uV;
46 unsigned int dutycycle;
47};
48
Lee Jones4773be12015-07-07 16:06:51 +010049/**
50 * Voltage table call-backs
51 */
Boris Brezillon87248992016-06-14 11:13:19 +020052static void pwm_regulator_init_state(struct regulator_dev *rdev)
53{
54 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
55 struct pwm_state pwm_state;
56 unsigned int dutycycle;
57 int i;
58
59 pwm_get_state(drvdata->pwm, &pwm_state);
60 dutycycle = pwm_get_relative_duty_cycle(&pwm_state, 100);
61
62 for (i = 0; i < rdev->desc->n_voltages; i++) {
63 if (dutycycle == drvdata->duty_cycle_table[i].dutycycle) {
64 drvdata->state = i;
65 return;
66 }
67 }
68}
69
Lee Jonesab101e32015-06-05 19:42:47 +010070static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev)
Chris Zhongaa66cc62014-09-28 10:28:53 +080071{
Lee Jonesab101e32015-06-05 19:42:47 +010072 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Chris Zhongaa66cc62014-09-28 10:28:53 +080073
Boris Brezillon87248992016-06-14 11:13:19 +020074 if (drvdata->state < 0)
75 pwm_regulator_init_state(rdev);
76
Chris Zhongaa66cc62014-09-28 10:28:53 +080077 return drvdata->state;
78}
79
Lee Jonesab101e32015-06-05 19:42:47 +010080static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev,
Chris Zhongaa66cc62014-09-28 10:28:53 +080081 unsigned selector)
82{
Lee Jonesab101e32015-06-05 19:42:47 +010083 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Boris Brezillon3f4eb392016-06-14 11:13:18 +020084 struct pwm_state pstate;
Chris Zhongaa66cc62014-09-28 10:28:53 +080085 int ret;
86
Boris Brezillon3f4eb392016-06-14 11:13:18 +020087 pwm_init_state(drvdata->pwm, &pstate);
88 pwm_set_relative_duty_cycle(&pstate,
89 drvdata->duty_cycle_table[selector].dutycycle, 100);
Chris Zhongaa66cc62014-09-28 10:28:53 +080090
Boris Brezillon3f4eb392016-06-14 11:13:18 +020091 ret = pwm_apply_state(drvdata->pwm, &pstate);
Chris Zhongaa66cc62014-09-28 10:28:53 +080092 if (ret) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +053093 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
Chris Zhongaa66cc62014-09-28 10:28:53 +080094 return ret;
95 }
96
97 drvdata->state = selector;
98
Chris Zhongaa66cc62014-09-28 10:28:53 +080099 return 0;
100}
101
Lee Jonesab101e32015-06-05 19:42:47 +0100102static int pwm_regulator_list_voltage(struct regulator_dev *rdev,
Chris Zhongaa66cc62014-09-28 10:28:53 +0800103 unsigned selector)
104{
Lee Jonesab101e32015-06-05 19:42:47 +0100105 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Chris Zhongaa66cc62014-09-28 10:28:53 +0800106
Lee Jonesab101e32015-06-05 19:42:47 +0100107 if (selector >= rdev->desc->n_voltages)
Chris Zhongaa66cc62014-09-28 10:28:53 +0800108 return -EINVAL;
109
110 return drvdata->duty_cycle_table[selector].uV;
111}
Lee Jones4773be12015-07-07 16:06:51 +0100112
Boris Brezillon1de7d802015-09-21 11:33:28 +0200113static int pwm_regulator_enable(struct regulator_dev *dev)
114{
115 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
116
Alexandre Courbot27bfa882016-06-23 16:39:44 +0900117 if (drvdata->enb_gpio)
118 gpiod_set_value_cansleep(drvdata->enb_gpio, 1);
119
Boris Brezillon1de7d802015-09-21 11:33:28 +0200120 return pwm_enable(drvdata->pwm);
121}
122
123static int pwm_regulator_disable(struct regulator_dev *dev)
124{
125 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
126
127 pwm_disable(drvdata->pwm);
128
Alexandre Courbot27bfa882016-06-23 16:39:44 +0900129 if (drvdata->enb_gpio)
130 gpiod_set_value_cansleep(drvdata->enb_gpio, 0);
131
Boris Brezillon1de7d802015-09-21 11:33:28 +0200132 return 0;
133}
134
135static int pwm_regulator_is_enabled(struct regulator_dev *dev)
136{
137 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
138
Alexandre Courbot27bfa882016-06-23 16:39:44 +0900139 if (drvdata->enb_gpio && !gpiod_get_value_cansleep(drvdata->enb_gpio))
140 return false;
141
Boris Brezillon1de7d802015-09-21 11:33:28 +0200142 return pwm_is_enabled(drvdata->pwm);
143}
144
Lee Jones4773be12015-07-07 16:06:51 +0100145static int pwm_regulator_get_voltage(struct regulator_dev *rdev)
146{
147 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Boris Brezillond9070fd2016-06-14 11:13:20 +0200148 int min_uV = rdev->constraints->min_uV;
149 int diff = rdev->constraints->max_uV - min_uV;
150 struct pwm_state pstate;
Lee Jones4773be12015-07-07 16:06:51 +0100151
Boris Brezillond9070fd2016-06-14 11:13:20 +0200152 pwm_get_state(drvdata->pwm, &pstate);
153
154 return min_uV + pwm_get_relative_duty_cycle(&pstate, diff);
Lee Jones4773be12015-07-07 16:06:51 +0100155}
156
157static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
158 int min_uV, int max_uV,
159 unsigned *selector)
160{
161 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
162 unsigned int ramp_delay = rdev->constraints->ramp_delay;
Laxman Dewanganfd786fb02016-04-05 15:09:48 +0530163 unsigned int req_diff = min_uV - rdev->constraints->min_uV;
Boris Brezillon3f4eb392016-06-14 11:13:18 +0200164 struct pwm_state pstate;
Laxman Dewanganfd786fb02016-04-05 15:09:48 +0530165 unsigned int diff;
Douglas Andersonc2588392016-07-06 11:42:01 -0700166 int old_uV = pwm_regulator_get_voltage(rdev);
Lee Jones4773be12015-07-07 16:06:51 +0100167 int ret;
168
Boris Brezillon3f4eb392016-06-14 11:13:18 +0200169 pwm_init_state(drvdata->pwm, &pstate);
Laxman Dewanganfd786fb02016-04-05 15:09:48 +0530170 diff = rdev->constraints->max_uV - rdev->constraints->min_uV;
Lee Jones4773be12015-07-07 16:06:51 +0100171
Boris Brezillon3f4eb392016-06-14 11:13:18 +0200172 /* We pass diff as the scale to get a uV precision. */
173 pwm_set_relative_duty_cycle(&pstate, req_diff, diff);
Laxman Dewanganfd786fb02016-04-05 15:09:48 +0530174
Boris Brezillon3f4eb392016-06-14 11:13:18 +0200175 ret = pwm_apply_state(drvdata->pwm, &pstate);
Lee Jones4773be12015-07-07 16:06:51 +0100176 if (ret) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530177 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
Lee Jones4773be12015-07-07 16:06:51 +0100178 return ret;
179 }
180
Douglas Andersonc2588392016-07-06 11:42:01 -0700181 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));
Lee Jones4773be12015-07-07 16:06:51 +0100187
188 return 0;
189}
190
Lee Jonesf9178da2015-07-06 09:58:29 +0100191static struct regulator_ops pwm_regulator_voltage_table_ops = {
Chris Zhongaa66cc62014-09-28 10:28:53 +0800192 .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,
Boris Brezillon1de7d802015-09-21 11:33:28 +0200196 .enable = pwm_regulator_enable,
197 .disable = pwm_regulator_disable,
198 .is_enabled = pwm_regulator_is_enabled,
Chris Zhongaa66cc62014-09-28 10:28:53 +0800199};
200
Lee Jones4773be12015-07-07 16:06:51 +0100201static struct regulator_ops pwm_regulator_voltage_continuous_ops = {
202 .get_voltage = pwm_regulator_get_voltage,
203 .set_voltage = pwm_regulator_set_voltage,
Boris Brezillon1de7d802015-09-21 11:33:28 +0200204 .enable = pwm_regulator_enable,
205 .disable = pwm_regulator_disable,
206 .is_enabled = pwm_regulator_is_enabled,
Lee Jones4773be12015-07-07 16:06:51 +0100207};
208
Lee Jonesb6f55e72015-06-05 19:42:45 +0100209static struct regulator_desc pwm_regulator_desc = {
Chris Zhongaa66cc62014-09-28 10:28:53 +0800210 .name = "pwm-regulator",
Chris Zhongaa66cc62014-09-28 10:28:53 +0800211 .type = REGULATOR_VOLTAGE,
212 .owner = THIS_MODULE,
213 .supply_name = "pwm",
214};
215
Lee Jonesf9178da2015-07-06 09:58:29 +0100216static 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;
Lee Jones60cb65e2015-07-10 08:45:36 +0100221 unsigned int length = 0;
Lee Jonesf9178da2015-07-06 09:58:29 +0100222 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))) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530228 dev_err(&pdev->dev, "voltage-table length(%d) is invalid\n",
Lee Jonesf9178da2015-07-06 09:58:29 +0100229 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) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530241 dev_err(&pdev->dev, "Failed to read voltage-table: %d\n", ret);
Lee Jonesf9178da2015-07-06 09:58:29 +0100242 return ret;
243 }
244
Boris Brezillon87248992016-06-14 11:13:19 +0200245 drvdata->state = -EINVAL;
Lee Jonesf9178da2015-07-06 09:58:29 +0100246 drvdata->duty_cycle_table = duty_cycle_table;
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530247 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);
Lee Jonesf9178da2015-07-06 09:58:29 +0100251
252 return 0;
253}
254
Lee Jones4773be12015-07-07 16:06:51 +0100255static int pwm_regulator_init_continuous(struct platform_device *pdev,
256 struct pwm_regulator_data *drvdata)
257{
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530258 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;
Lee Jones4773be12015-07-07 16:06:51 +0100262
263 return 0;
264}
265
Chris Zhongaa66cc62014-09-28 10:28:53 +0800266static int pwm_regulator_probe(struct platform_device *pdev)
267{
Lee Jones5ad2cb12015-07-07 16:06:53 +0100268 const struct regulator_init_data *init_data;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800269 struct pwm_regulator_data *drvdata;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800270 struct regulator_dev *regulator;
271 struct regulator_config config = { };
272 struct device_node *np = pdev->dev.of_node;
Alexandre Courbot27bfa882016-06-23 16:39:44 +0900273 enum gpiod_flags gpio_flags;
Lee Jonesf9178da2015-07-06 09:58:29 +0100274 int ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800275
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
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530285 memcpy(&drvdata->desc, &pwm_regulator_desc, sizeof(drvdata->desc));
286
Lee Jones4773be12015-07-07 16:06:51 +0100287 if (of_find_property(np, "voltage-table", NULL))
Lee Jonesf9178da2015-07-06 09:58:29 +0100288 ret = pwm_regulator_init_table(pdev, drvdata);
Lee Jones4773be12015-07-07 16:06:51 +0100289 else
290 ret = pwm_regulator_init_continuous(pdev, drvdata);
291 if (ret)
292 return ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800293
Lee Jones5ad2cb12015-07-07 16:06:53 +0100294 init_data = of_get_regulator_init_data(&pdev->dev, np,
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530295 &drvdata->desc);
Lee Jones5ad2cb12015-07-07 16:06:53 +0100296 if (!init_data)
Chris Zhongaa66cc62014-09-28 10:28:53 +0800297 return -ENOMEM;
298
299 config.of_node = np;
300 config.dev = &pdev->dev;
301 config.driver_data = drvdata;
Lee Jones5ad2cb12015-07-07 16:06:53 +0100302 config.init_data = init_data;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800303
304 drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
305 if (IS_ERR(drvdata->pwm)) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530306 ret = PTR_ERR(drvdata->pwm);
307 dev_err(&pdev->dev, "Failed to get PWM: %d\n", ret);
308 return ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800309 }
310
Alexandre Courbot27bfa882016-06-23 16:39:44 +0900311 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
Boris Brezillonfd4f99c2016-06-14 11:13:17 +0200323 ret = pwm_adjust_config(drvdata->pwm);
324 if (ret)
325 return ret;
Boris Brezillon8c12ad82016-04-14 21:17:27 +0200326
Chris Zhongaa66cc62014-09-28 10:28:53 +0800327 regulator = devm_regulator_register(&pdev->dev,
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530328 &drvdata->desc, &config);
Chris Zhongaa66cc62014-09-28 10:28:53 +0800329 if (IS_ERR(regulator)) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530330 ret = PTR_ERR(regulator);
331 dev_err(&pdev->dev, "Failed to register regulator %s: %d\n",
332 drvdata->desc.name, ret);
333 return ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800334 }
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",
Chris Zhongaa66cc62014-09-28 10:28:53 +0800348 .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");