blob: 7920411057af301a25b6c0a0d6a686c11e871935 [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
40 /* Continuous voltage */
Lee Jones4773be12015-07-07 16:06:51 +010041 int volt_uV;
Alexandre Courbot27bfa882016-06-23 16:39:44 +090042
43 /* Enable GPIO */
44 struct gpio_desc *enb_gpio;
Chris Zhongaa66cc62014-09-28 10:28:53 +080045};
46
47struct pwm_voltages {
48 unsigned int uV;
49 unsigned int dutycycle;
50};
51
Lee Jones4773be12015-07-07 16:06:51 +010052/**
53 * Voltage table call-backs
54 */
Lee Jonesab101e32015-06-05 19:42:47 +010055static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev)
Chris Zhongaa66cc62014-09-28 10:28:53 +080056{
Lee Jonesab101e32015-06-05 19:42:47 +010057 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Chris Zhongaa66cc62014-09-28 10:28:53 +080058
59 return drvdata->state;
60}
61
Lee Jonesab101e32015-06-05 19:42:47 +010062static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev,
Chris Zhongaa66cc62014-09-28 10:28:53 +080063 unsigned selector)
64{
Lee Jonesab101e32015-06-05 19:42:47 +010065 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Boris Brezillon3f4eb392016-06-14 11:13:18 +020066 struct pwm_state pstate;
Chris Zhongaa66cc62014-09-28 10:28:53 +080067 int ret;
68
Boris Brezillon3f4eb392016-06-14 11:13:18 +020069 pwm_init_state(drvdata->pwm, &pstate);
70 pwm_set_relative_duty_cycle(&pstate,
71 drvdata->duty_cycle_table[selector].dutycycle, 100);
Chris Zhongaa66cc62014-09-28 10:28:53 +080072
Boris Brezillon3f4eb392016-06-14 11:13:18 +020073 ret = pwm_apply_state(drvdata->pwm, &pstate);
Chris Zhongaa66cc62014-09-28 10:28:53 +080074 if (ret) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +053075 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
Chris Zhongaa66cc62014-09-28 10:28:53 +080076 return ret;
77 }
78
79 drvdata->state = selector;
80
Chris Zhongaa66cc62014-09-28 10:28:53 +080081 return 0;
82}
83
Lee Jonesab101e32015-06-05 19:42:47 +010084static int pwm_regulator_list_voltage(struct regulator_dev *rdev,
Chris Zhongaa66cc62014-09-28 10:28:53 +080085 unsigned selector)
86{
Lee Jonesab101e32015-06-05 19:42:47 +010087 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Chris Zhongaa66cc62014-09-28 10:28:53 +080088
Lee Jonesab101e32015-06-05 19:42:47 +010089 if (selector >= rdev->desc->n_voltages)
Chris Zhongaa66cc62014-09-28 10:28:53 +080090 return -EINVAL;
91
92 return drvdata->duty_cycle_table[selector].uV;
93}
Lee Jones4773be12015-07-07 16:06:51 +010094
Boris Brezillon1de7d802015-09-21 11:33:28 +020095static int pwm_regulator_enable(struct regulator_dev *dev)
96{
97 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
98
Alexandre Courbot27bfa882016-06-23 16:39:44 +090099 if (drvdata->enb_gpio)
100 gpiod_set_value_cansleep(drvdata->enb_gpio, 1);
101
Boris Brezillon1de7d802015-09-21 11:33:28 +0200102 return pwm_enable(drvdata->pwm);
103}
104
105static int pwm_regulator_disable(struct regulator_dev *dev)
106{
107 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
108
109 pwm_disable(drvdata->pwm);
110
Alexandre Courbot27bfa882016-06-23 16:39:44 +0900111 if (drvdata->enb_gpio)
112 gpiod_set_value_cansleep(drvdata->enb_gpio, 0);
113
Boris Brezillon1de7d802015-09-21 11:33:28 +0200114 return 0;
115}
116
117static int pwm_regulator_is_enabled(struct regulator_dev *dev)
118{
119 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
120
Alexandre Courbot27bfa882016-06-23 16:39:44 +0900121 if (drvdata->enb_gpio && !gpiod_get_value_cansleep(drvdata->enb_gpio))
122 return false;
123
Boris Brezillon1de7d802015-09-21 11:33:28 +0200124 return pwm_is_enabled(drvdata->pwm);
125}
126
Lee Jones4773be12015-07-07 16:06:51 +0100127static int pwm_regulator_get_voltage(struct regulator_dev *rdev)
128{
129 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
130
131 return drvdata->volt_uV;
132}
133
134static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
135 int min_uV, int max_uV,
136 unsigned *selector)
137{
138 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
139 unsigned int ramp_delay = rdev->constraints->ramp_delay;
Laxman Dewanganfd786fb02016-04-05 15:09:48 +0530140 unsigned int req_diff = min_uV - rdev->constraints->min_uV;
Boris Brezillon3f4eb392016-06-14 11:13:18 +0200141 struct pwm_state pstate;
Laxman Dewanganfd786fb02016-04-05 15:09:48 +0530142 unsigned int diff;
Douglas Andersonc2588392016-07-06 11:42:01 -0700143 int old_uV = pwm_regulator_get_voltage(rdev);
Lee Jones4773be12015-07-07 16:06:51 +0100144 int ret;
145
Boris Brezillon3f4eb392016-06-14 11:13:18 +0200146 pwm_init_state(drvdata->pwm, &pstate);
Laxman Dewanganfd786fb02016-04-05 15:09:48 +0530147 diff = rdev->constraints->max_uV - rdev->constraints->min_uV;
Lee Jones4773be12015-07-07 16:06:51 +0100148
Boris Brezillon3f4eb392016-06-14 11:13:18 +0200149 /* We pass diff as the scale to get a uV precision. */
150 pwm_set_relative_duty_cycle(&pstate, req_diff, diff);
Laxman Dewanganfd786fb02016-04-05 15:09:48 +0530151
Boris Brezillon3f4eb392016-06-14 11:13:18 +0200152 ret = pwm_apply_state(drvdata->pwm, &pstate);
Lee Jones4773be12015-07-07 16:06:51 +0100153 if (ret) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530154 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
Lee Jones4773be12015-07-07 16:06:51 +0100155 return ret;
156 }
157
Lee Jones4773be12015-07-07 16:06:51 +0100158 drvdata->volt_uV = min_uV;
159
Douglas Andersonc2588392016-07-06 11:42:01 -0700160 if ((ramp_delay == 0) || !pwm_regulator_is_enabled(rdev))
161 return 0;
162
163 /* Ramp delay is in uV/uS. Adjust to uS and delay */
164 ramp_delay = DIV_ROUND_UP(abs(min_uV - old_uV), ramp_delay);
165 usleep_range(ramp_delay, ramp_delay + DIV_ROUND_UP(ramp_delay, 10));
Lee Jones4773be12015-07-07 16:06:51 +0100166
167 return 0;
168}
169
Lee Jonesf9178da2015-07-06 09:58:29 +0100170static struct regulator_ops pwm_regulator_voltage_table_ops = {
Chris Zhongaa66cc62014-09-28 10:28:53 +0800171 .set_voltage_sel = pwm_regulator_set_voltage_sel,
172 .get_voltage_sel = pwm_regulator_get_voltage_sel,
173 .list_voltage = pwm_regulator_list_voltage,
174 .map_voltage = regulator_map_voltage_iterate,
Boris Brezillon1de7d802015-09-21 11:33:28 +0200175 .enable = pwm_regulator_enable,
176 .disable = pwm_regulator_disable,
177 .is_enabled = pwm_regulator_is_enabled,
Chris Zhongaa66cc62014-09-28 10:28:53 +0800178};
179
Lee Jones4773be12015-07-07 16:06:51 +0100180static struct regulator_ops pwm_regulator_voltage_continuous_ops = {
181 .get_voltage = pwm_regulator_get_voltage,
182 .set_voltage = pwm_regulator_set_voltage,
Boris Brezillon1de7d802015-09-21 11:33:28 +0200183 .enable = pwm_regulator_enable,
184 .disable = pwm_regulator_disable,
185 .is_enabled = pwm_regulator_is_enabled,
Lee Jones4773be12015-07-07 16:06:51 +0100186};
187
Lee Jonesb6f55e72015-06-05 19:42:45 +0100188static struct regulator_desc pwm_regulator_desc = {
Chris Zhongaa66cc62014-09-28 10:28:53 +0800189 .name = "pwm-regulator",
Chris Zhongaa66cc62014-09-28 10:28:53 +0800190 .type = REGULATOR_VOLTAGE,
191 .owner = THIS_MODULE,
192 .supply_name = "pwm",
193};
194
Lee Jonesf9178da2015-07-06 09:58:29 +0100195static int pwm_regulator_init_table(struct platform_device *pdev,
196 struct pwm_regulator_data *drvdata)
197{
198 struct device_node *np = pdev->dev.of_node;
199 struct pwm_voltages *duty_cycle_table;
Lee Jones60cb65e2015-07-10 08:45:36 +0100200 unsigned int length = 0;
Lee Jonesf9178da2015-07-06 09:58:29 +0100201 int ret;
202
203 of_find_property(np, "voltage-table", &length);
204
205 if ((length < sizeof(*duty_cycle_table)) ||
206 (length % sizeof(*duty_cycle_table))) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530207 dev_err(&pdev->dev, "voltage-table length(%d) is invalid\n",
Lee Jonesf9178da2015-07-06 09:58:29 +0100208 length);
209 return -EINVAL;
210 }
211
212 duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
213 if (!duty_cycle_table)
214 return -ENOMEM;
215
216 ret = of_property_read_u32_array(np, "voltage-table",
217 (u32 *)duty_cycle_table,
218 length / sizeof(u32));
219 if (ret) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530220 dev_err(&pdev->dev, "Failed to read voltage-table: %d\n", ret);
Lee Jonesf9178da2015-07-06 09:58:29 +0100221 return ret;
222 }
223
224 drvdata->duty_cycle_table = duty_cycle_table;
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530225 memcpy(&drvdata->ops, &pwm_regulator_voltage_table_ops,
226 sizeof(drvdata->ops));
227 drvdata->desc.ops = &drvdata->ops;
228 drvdata->desc.n_voltages = length / sizeof(*duty_cycle_table);
Lee Jonesf9178da2015-07-06 09:58:29 +0100229
230 return 0;
231}
232
Lee Jones4773be12015-07-07 16:06:51 +0100233static int pwm_regulator_init_continuous(struct platform_device *pdev,
234 struct pwm_regulator_data *drvdata)
235{
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530236 memcpy(&drvdata->ops, &pwm_regulator_voltage_continuous_ops,
237 sizeof(drvdata->ops));
238 drvdata->desc.ops = &drvdata->ops;
239 drvdata->desc.continuous_voltage_range = true;
Lee Jones4773be12015-07-07 16:06:51 +0100240
241 return 0;
242}
243
Chris Zhongaa66cc62014-09-28 10:28:53 +0800244static int pwm_regulator_probe(struct platform_device *pdev)
245{
Lee Jones5ad2cb12015-07-07 16:06:53 +0100246 const struct regulator_init_data *init_data;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800247 struct pwm_regulator_data *drvdata;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800248 struct regulator_dev *regulator;
249 struct regulator_config config = { };
250 struct device_node *np = pdev->dev.of_node;
Alexandre Courbot27bfa882016-06-23 16:39:44 +0900251 enum gpiod_flags gpio_flags;
Lee Jonesf9178da2015-07-06 09:58:29 +0100252 int ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800253
254 if (!np) {
255 dev_err(&pdev->dev, "Device Tree node missing\n");
256 return -EINVAL;
257 }
258
259 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
260 if (!drvdata)
261 return -ENOMEM;
262
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530263 memcpy(&drvdata->desc, &pwm_regulator_desc, sizeof(drvdata->desc));
264
Lee Jones4773be12015-07-07 16:06:51 +0100265 if (of_find_property(np, "voltage-table", NULL))
Lee Jonesf9178da2015-07-06 09:58:29 +0100266 ret = pwm_regulator_init_table(pdev, drvdata);
Lee Jones4773be12015-07-07 16:06:51 +0100267 else
268 ret = pwm_regulator_init_continuous(pdev, drvdata);
269 if (ret)
270 return ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800271
Lee Jones5ad2cb12015-07-07 16:06:53 +0100272 init_data = of_get_regulator_init_data(&pdev->dev, np,
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530273 &drvdata->desc);
Lee Jones5ad2cb12015-07-07 16:06:53 +0100274 if (!init_data)
Chris Zhongaa66cc62014-09-28 10:28:53 +0800275 return -ENOMEM;
276
277 config.of_node = np;
278 config.dev = &pdev->dev;
279 config.driver_data = drvdata;
Lee Jones5ad2cb12015-07-07 16:06:53 +0100280 config.init_data = init_data;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800281
282 drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
283 if (IS_ERR(drvdata->pwm)) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530284 ret = PTR_ERR(drvdata->pwm);
285 dev_err(&pdev->dev, "Failed to get PWM: %d\n", ret);
286 return ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800287 }
288
Alexandre Courbot27bfa882016-06-23 16:39:44 +0900289 if (init_data->constraints.boot_on || init_data->constraints.always_on)
290 gpio_flags = GPIOD_OUT_HIGH;
291 else
292 gpio_flags = GPIOD_OUT_LOW;
293 drvdata->enb_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
294 gpio_flags);
295 if (IS_ERR(drvdata->enb_gpio)) {
296 ret = PTR_ERR(drvdata->enb_gpio);
297 dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n", ret);
298 return ret;
299 }
300
Boris Brezillonfd4f99c2016-06-14 11:13:17 +0200301 ret = pwm_adjust_config(drvdata->pwm);
302 if (ret)
303 return ret;
Boris Brezillon8c12ad82016-04-14 21:17:27 +0200304
Chris Zhongaa66cc62014-09-28 10:28:53 +0800305 regulator = devm_regulator_register(&pdev->dev,
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530306 &drvdata->desc, &config);
Chris Zhongaa66cc62014-09-28 10:28:53 +0800307 if (IS_ERR(regulator)) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530308 ret = PTR_ERR(regulator);
309 dev_err(&pdev->dev, "Failed to register regulator %s: %d\n",
310 drvdata->desc.name, ret);
311 return ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800312 }
313
314 return 0;
315}
316
317static const struct of_device_id pwm_of_match[] = {
318 { .compatible = "pwm-regulator" },
319 { },
320};
321MODULE_DEVICE_TABLE(of, pwm_of_match);
322
323static struct platform_driver pwm_regulator_driver = {
324 .driver = {
325 .name = "pwm-regulator",
Chris Zhongaa66cc62014-09-28 10:28:53 +0800326 .of_match_table = of_match_ptr(pwm_of_match),
327 },
328 .probe = pwm_regulator_probe,
329};
330
331module_platform_driver(pwm_regulator_driver);
332
333MODULE_LICENSE("GPL");
334MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
335MODULE_DESCRIPTION("PWM Regulator Driver");
336MODULE_ALIAS("platform:pwm-regulator");