blob: 666bc3bb52ef83790df5df9afab68306a39d766a [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 Brezillon8c12ad82016-04-14 21:17:27 +020066 struct pwm_args pargs;
Chris Zhongaa66cc62014-09-28 10:28:53 +080067 int dutycycle;
68 int ret;
69
Boris Brezillon8c12ad82016-04-14 21:17:27 +020070 pwm_get_args(drvdata->pwm, &pargs);
Chris Zhongaa66cc62014-09-28 10:28:53 +080071
Boris Brezillon8c12ad82016-04-14 21:17:27 +020072 dutycycle = (pargs.period *
Chris Zhongaa66cc62014-09-28 10:28:53 +080073 drvdata->duty_cycle_table[selector].dutycycle) / 100;
74
Boris Brezillon8c12ad82016-04-14 21:17:27 +020075 ret = pwm_config(drvdata->pwm, dutycycle, pargs.period);
Chris Zhongaa66cc62014-09-28 10:28:53 +080076 if (ret) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +053077 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
Chris Zhongaa66cc62014-09-28 10:28:53 +080078 return ret;
79 }
80
81 drvdata->state = selector;
82
Chris Zhongaa66cc62014-09-28 10:28:53 +080083 return 0;
84}
85
Lee Jonesab101e32015-06-05 19:42:47 +010086static int pwm_regulator_list_voltage(struct regulator_dev *rdev,
Chris Zhongaa66cc62014-09-28 10:28:53 +080087 unsigned selector)
88{
Lee Jonesab101e32015-06-05 19:42:47 +010089 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Chris Zhongaa66cc62014-09-28 10:28:53 +080090
Lee Jonesab101e32015-06-05 19:42:47 +010091 if (selector >= rdev->desc->n_voltages)
Chris Zhongaa66cc62014-09-28 10:28:53 +080092 return -EINVAL;
93
94 return drvdata->duty_cycle_table[selector].uV;
95}
Lee Jones4773be12015-07-07 16:06:51 +010096
Boris Brezillon1de7d802015-09-21 11:33:28 +020097static int pwm_regulator_enable(struct regulator_dev *dev)
98{
99 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
100
Alexandre Courbot27bfa882016-06-23 16:39:44 +0900101 if (drvdata->enb_gpio)
102 gpiod_set_value_cansleep(drvdata->enb_gpio, 1);
103
Boris Brezillon1de7d802015-09-21 11:33:28 +0200104 return pwm_enable(drvdata->pwm);
105}
106
107static int pwm_regulator_disable(struct regulator_dev *dev)
108{
109 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
110
111 pwm_disable(drvdata->pwm);
112
Alexandre Courbot27bfa882016-06-23 16:39:44 +0900113 if (drvdata->enb_gpio)
114 gpiod_set_value_cansleep(drvdata->enb_gpio, 0);
115
Boris Brezillon1de7d802015-09-21 11:33:28 +0200116 return 0;
117}
118
119static int pwm_regulator_is_enabled(struct regulator_dev *dev)
120{
121 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
122
Alexandre Courbot27bfa882016-06-23 16:39:44 +0900123 if (drvdata->enb_gpio && !gpiod_get_value_cansleep(drvdata->enb_gpio))
124 return false;
125
Boris Brezillon1de7d802015-09-21 11:33:28 +0200126 return pwm_is_enabled(drvdata->pwm);
127}
128
Lee Jones4773be12015-07-07 16:06:51 +0100129static int pwm_regulator_get_voltage(struct regulator_dev *rdev)
130{
131 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
132
133 return drvdata->volt_uV;
134}
135
136static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
137 int min_uV, int max_uV,
138 unsigned *selector)
139{
140 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
141 unsigned int ramp_delay = rdev->constraints->ramp_delay;
Boris Brezillon8c12ad82016-04-14 21:17:27 +0200142 struct pwm_args pargs;
Laxman Dewanganfd786fb2016-04-05 15:09:48 +0530143 unsigned int req_diff = min_uV - rdev->constraints->min_uV;
144 unsigned int diff;
145 unsigned int duty_pulse;
146 u64 req_period;
147 u32 rem;
Douglas Andersonc2588392016-07-06 11:42:01 -0700148 int old_uV = pwm_regulator_get_voltage(rdev);
Lee Jones4773be12015-07-07 16:06:51 +0100149 int ret;
150
Boris Brezillon8c12ad82016-04-14 21:17:27 +0200151 pwm_get_args(drvdata->pwm, &pargs);
Laxman Dewanganfd786fb2016-04-05 15:09:48 +0530152 diff = rdev->constraints->max_uV - rdev->constraints->min_uV;
Lee Jones4773be12015-07-07 16:06:51 +0100153
Laxman Dewanganfd786fb2016-04-05 15:09:48 +0530154 /* First try to find out if we get the iduty cycle time which is
155 * factor of PWM period time. If (request_diff_to_min * pwm_period)
156 * is perfect divided by voltage_range_diff then it is possible to
157 * get duty cycle time which is factor of PWM period. This will help
158 * to get output voltage nearer to requested value as there is no
159 * calculation loss.
160 */
Mark Brownbc0868c2016-05-03 15:09:56 +0100161 req_period = req_diff * pargs.period;
Laxman Dewanganfd786fb2016-04-05 15:09:48 +0530162 div_u64_rem(req_period, diff, &rem);
163 if (!rem) {
164 do_div(req_period, diff);
165 duty_pulse = (unsigned int)req_period;
166 } else {
Mark Brownbc0868c2016-05-03 15:09:56 +0100167 duty_pulse = (pargs.period / 100) * ((req_diff * 100) / diff);
Laxman Dewanganfd786fb2016-04-05 15:09:48 +0530168 }
169
Mark Brownbc0868c2016-05-03 15:09:56 +0100170 ret = pwm_config(drvdata->pwm, duty_pulse, pargs.period);
Lee Jones4773be12015-07-07 16:06:51 +0100171 if (ret) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530172 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
Lee Jones4773be12015-07-07 16:06:51 +0100173 return ret;
174 }
175
Lee Jones4773be12015-07-07 16:06:51 +0100176 drvdata->volt_uV = min_uV;
177
Douglas Andersonc2588392016-07-06 11:42:01 -0700178 if ((ramp_delay == 0) || !pwm_regulator_is_enabled(rdev))
179 return 0;
180
181 /* Ramp delay is in uV/uS. Adjust to uS and delay */
182 ramp_delay = DIV_ROUND_UP(abs(min_uV - old_uV), ramp_delay);
183 usleep_range(ramp_delay, ramp_delay + DIV_ROUND_UP(ramp_delay, 10));
Lee Jones4773be12015-07-07 16:06:51 +0100184
185 return 0;
186}
187
Lee Jonesf9178da2015-07-06 09:58:29 +0100188static struct regulator_ops pwm_regulator_voltage_table_ops = {
Chris Zhongaa66cc62014-09-28 10:28:53 +0800189 .set_voltage_sel = pwm_regulator_set_voltage_sel,
190 .get_voltage_sel = pwm_regulator_get_voltage_sel,
191 .list_voltage = pwm_regulator_list_voltage,
192 .map_voltage = regulator_map_voltage_iterate,
Boris Brezillon1de7d802015-09-21 11:33:28 +0200193 .enable = pwm_regulator_enable,
194 .disable = pwm_regulator_disable,
195 .is_enabled = pwm_regulator_is_enabled,
Chris Zhongaa66cc62014-09-28 10:28:53 +0800196};
197
Lee Jones4773be12015-07-07 16:06:51 +0100198static struct regulator_ops pwm_regulator_voltage_continuous_ops = {
199 .get_voltage = pwm_regulator_get_voltage,
200 .set_voltage = pwm_regulator_set_voltage,
Boris Brezillon1de7d802015-09-21 11:33:28 +0200201 .enable = pwm_regulator_enable,
202 .disable = pwm_regulator_disable,
203 .is_enabled = pwm_regulator_is_enabled,
Lee Jones4773be12015-07-07 16:06:51 +0100204};
205
Lee Jonesb6f55e72015-06-05 19:42:45 +0100206static struct regulator_desc pwm_regulator_desc = {
Chris Zhongaa66cc62014-09-28 10:28:53 +0800207 .name = "pwm-regulator",
Chris Zhongaa66cc62014-09-28 10:28:53 +0800208 .type = REGULATOR_VOLTAGE,
209 .owner = THIS_MODULE,
210 .supply_name = "pwm",
211};
212
Lee Jonesf9178da2015-07-06 09:58:29 +0100213static int pwm_regulator_init_table(struct platform_device *pdev,
214 struct pwm_regulator_data *drvdata)
215{
216 struct device_node *np = pdev->dev.of_node;
217 struct pwm_voltages *duty_cycle_table;
Lee Jones60cb65e2015-07-10 08:45:36 +0100218 unsigned int length = 0;
Lee Jonesf9178da2015-07-06 09:58:29 +0100219 int ret;
220
221 of_find_property(np, "voltage-table", &length);
222
223 if ((length < sizeof(*duty_cycle_table)) ||
224 (length % sizeof(*duty_cycle_table))) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530225 dev_err(&pdev->dev, "voltage-table length(%d) is invalid\n",
Lee Jonesf9178da2015-07-06 09:58:29 +0100226 length);
227 return -EINVAL;
228 }
229
230 duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
231 if (!duty_cycle_table)
232 return -ENOMEM;
233
234 ret = of_property_read_u32_array(np, "voltage-table",
235 (u32 *)duty_cycle_table,
236 length / sizeof(u32));
237 if (ret) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530238 dev_err(&pdev->dev, "Failed to read voltage-table: %d\n", ret);
Lee Jonesf9178da2015-07-06 09:58:29 +0100239 return ret;
240 }
241
242 drvdata->duty_cycle_table = duty_cycle_table;
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530243 memcpy(&drvdata->ops, &pwm_regulator_voltage_table_ops,
244 sizeof(drvdata->ops));
245 drvdata->desc.ops = &drvdata->ops;
246 drvdata->desc.n_voltages = length / sizeof(*duty_cycle_table);
Lee Jonesf9178da2015-07-06 09:58:29 +0100247
248 return 0;
249}
250
Lee Jones4773be12015-07-07 16:06:51 +0100251static int pwm_regulator_init_continuous(struct platform_device *pdev,
252 struct pwm_regulator_data *drvdata)
253{
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530254 memcpy(&drvdata->ops, &pwm_regulator_voltage_continuous_ops,
255 sizeof(drvdata->ops));
256 drvdata->desc.ops = &drvdata->ops;
257 drvdata->desc.continuous_voltage_range = true;
Lee Jones4773be12015-07-07 16:06:51 +0100258
259 return 0;
260}
261
Chris Zhongaa66cc62014-09-28 10:28:53 +0800262static int pwm_regulator_probe(struct platform_device *pdev)
263{
Lee Jones5ad2cb12015-07-07 16:06:53 +0100264 const struct regulator_init_data *init_data;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800265 struct pwm_regulator_data *drvdata;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800266 struct regulator_dev *regulator;
267 struct regulator_config config = { };
268 struct device_node *np = pdev->dev.of_node;
Alexandre Courbot27bfa882016-06-23 16:39:44 +0900269 enum gpiod_flags gpio_flags;
Lee Jonesf9178da2015-07-06 09:58:29 +0100270 int ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800271
272 if (!np) {
273 dev_err(&pdev->dev, "Device Tree node missing\n");
274 return -EINVAL;
275 }
276
277 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
278 if (!drvdata)
279 return -ENOMEM;
280
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530281 memcpy(&drvdata->desc, &pwm_regulator_desc, sizeof(drvdata->desc));
282
Lee Jones4773be12015-07-07 16:06:51 +0100283 if (of_find_property(np, "voltage-table", NULL))
Lee Jonesf9178da2015-07-06 09:58:29 +0100284 ret = pwm_regulator_init_table(pdev, drvdata);
Lee Jones4773be12015-07-07 16:06:51 +0100285 else
286 ret = pwm_regulator_init_continuous(pdev, drvdata);
287 if (ret)
288 return ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800289
Lee Jones5ad2cb12015-07-07 16:06:53 +0100290 init_data = of_get_regulator_init_data(&pdev->dev, np,
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530291 &drvdata->desc);
Lee Jones5ad2cb12015-07-07 16:06:53 +0100292 if (!init_data)
Chris Zhongaa66cc62014-09-28 10:28:53 +0800293 return -ENOMEM;
294
295 config.of_node = np;
296 config.dev = &pdev->dev;
297 config.driver_data = drvdata;
Lee Jones5ad2cb12015-07-07 16:06:53 +0100298 config.init_data = init_data;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800299
300 drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
301 if (IS_ERR(drvdata->pwm)) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530302 ret = PTR_ERR(drvdata->pwm);
303 dev_err(&pdev->dev, "Failed to get PWM: %d\n", ret);
304 return ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800305 }
306
Alexandre Courbot27bfa882016-06-23 16:39:44 +0900307 if (init_data->constraints.boot_on || init_data->constraints.always_on)
308 gpio_flags = GPIOD_OUT_HIGH;
309 else
310 gpio_flags = GPIOD_OUT_LOW;
311 drvdata->enb_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
312 gpio_flags);
313 if (IS_ERR(drvdata->enb_gpio)) {
314 ret = PTR_ERR(drvdata->enb_gpio);
315 dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n", ret);
316 return ret;
317 }
318
Boris Brezillon8c12ad82016-04-14 21:17:27 +0200319 /*
320 * FIXME: pwm_apply_args() should be removed when switching to the
321 * atomic PWM API.
322 */
323 pwm_apply_args(drvdata->pwm);
324
Chris Zhongaa66cc62014-09-28 10:28:53 +0800325 regulator = devm_regulator_register(&pdev->dev,
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530326 &drvdata->desc, &config);
Chris Zhongaa66cc62014-09-28 10:28:53 +0800327 if (IS_ERR(regulator)) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530328 ret = PTR_ERR(regulator);
329 dev_err(&pdev->dev, "Failed to register regulator %s: %d\n",
330 drvdata->desc.name, ret);
331 return ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800332 }
333
334 return 0;
335}
336
337static const struct of_device_id pwm_of_match[] = {
338 { .compatible = "pwm-regulator" },
339 { },
340};
341MODULE_DEVICE_TABLE(of, pwm_of_match);
342
343static struct platform_driver pwm_regulator_driver = {
344 .driver = {
345 .name = "pwm-regulator",
Chris Zhongaa66cc62014-09-28 10:28:53 +0800346 .of_match_table = of_match_ptr(pwm_of_match),
347 },
348 .probe = pwm_regulator_probe,
349};
350
351module_platform_driver(pwm_regulator_driver);
352
353MODULE_LICENSE("GPL");
354MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
355MODULE_DESCRIPTION("PWM Regulator Driver");
356MODULE_ALIAS("platform:pwm-regulator");