blob: 90f8b7fd04374ee7796baef7e7a654b776d44e5d [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;
Lee Jones4773be12015-07-07 16:06:51 +0100148 int ret;
149
Boris Brezillon8c12ad82016-04-14 21:17:27 +0200150 pwm_get_args(drvdata->pwm, &pargs);
Laxman Dewanganfd786fb2016-04-05 15:09:48 +0530151 diff = rdev->constraints->max_uV - rdev->constraints->min_uV;
Lee Jones4773be12015-07-07 16:06:51 +0100152
Laxman Dewanganfd786fb2016-04-05 15:09:48 +0530153 /* First try to find out if we get the iduty cycle time which is
154 * factor of PWM period time. If (request_diff_to_min * pwm_period)
155 * is perfect divided by voltage_range_diff then it is possible to
156 * get duty cycle time which is factor of PWM period. This will help
157 * to get output voltage nearer to requested value as there is no
158 * calculation loss.
159 */
Mark Brownbc0868c2016-05-03 15:09:56 +0100160 req_period = req_diff * pargs.period;
Laxman Dewanganfd786fb2016-04-05 15:09:48 +0530161 div_u64_rem(req_period, diff, &rem);
162 if (!rem) {
163 do_div(req_period, diff);
164 duty_pulse = (unsigned int)req_period;
165 } else {
Mark Brownbc0868c2016-05-03 15:09:56 +0100166 duty_pulse = (pargs.period / 100) * ((req_diff * 100) / diff);
Laxman Dewanganfd786fb2016-04-05 15:09:48 +0530167 }
168
Mark Brownbc0868c2016-05-03 15:09:56 +0100169 ret = pwm_config(drvdata->pwm, duty_pulse, pargs.period);
Lee Jones4773be12015-07-07 16:06:51 +0100170 if (ret) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530171 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
Lee Jones4773be12015-07-07 16:06:51 +0100172 return ret;
173 }
174
Lee Jones4773be12015-07-07 16:06:51 +0100175 drvdata->volt_uV = min_uV;
176
177 /* Delay required by PWM regulator to settle to the new voltage */
178 usleep_range(ramp_delay, ramp_delay + 1000);
179
180 return 0;
181}
182
Lee Jonesf9178da2015-07-06 09:58:29 +0100183static struct regulator_ops pwm_regulator_voltage_table_ops = {
Chris Zhongaa66cc62014-09-28 10:28:53 +0800184 .set_voltage_sel = pwm_regulator_set_voltage_sel,
185 .get_voltage_sel = pwm_regulator_get_voltage_sel,
186 .list_voltage = pwm_regulator_list_voltage,
187 .map_voltage = regulator_map_voltage_iterate,
Boris Brezillon1de7d802015-09-21 11:33:28 +0200188 .enable = pwm_regulator_enable,
189 .disable = pwm_regulator_disable,
190 .is_enabled = pwm_regulator_is_enabled,
Chris Zhongaa66cc62014-09-28 10:28:53 +0800191};
192
Lee Jones4773be12015-07-07 16:06:51 +0100193static struct regulator_ops pwm_regulator_voltage_continuous_ops = {
194 .get_voltage = pwm_regulator_get_voltage,
195 .set_voltage = pwm_regulator_set_voltage,
Boris Brezillon1de7d802015-09-21 11:33:28 +0200196 .enable = pwm_regulator_enable,
197 .disable = pwm_regulator_disable,
198 .is_enabled = pwm_regulator_is_enabled,
Lee Jones4773be12015-07-07 16:06:51 +0100199};
200
Lee Jonesb6f55e72015-06-05 19:42:45 +0100201static struct regulator_desc pwm_regulator_desc = {
Chris Zhongaa66cc62014-09-28 10:28:53 +0800202 .name = "pwm-regulator",
Chris Zhongaa66cc62014-09-28 10:28:53 +0800203 .type = REGULATOR_VOLTAGE,
204 .owner = THIS_MODULE,
205 .supply_name = "pwm",
206};
207
Lee Jonesf9178da2015-07-06 09:58:29 +0100208static int pwm_regulator_init_table(struct platform_device *pdev,
209 struct pwm_regulator_data *drvdata)
210{
211 struct device_node *np = pdev->dev.of_node;
212 struct pwm_voltages *duty_cycle_table;
Lee Jones60cb65e2015-07-10 08:45:36 +0100213 unsigned int length = 0;
Lee Jonesf9178da2015-07-06 09:58:29 +0100214 int ret;
215
216 of_find_property(np, "voltage-table", &length);
217
218 if ((length < sizeof(*duty_cycle_table)) ||
219 (length % sizeof(*duty_cycle_table))) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530220 dev_err(&pdev->dev, "voltage-table length(%d) is invalid\n",
Lee Jonesf9178da2015-07-06 09:58:29 +0100221 length);
222 return -EINVAL;
223 }
224
225 duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
226 if (!duty_cycle_table)
227 return -ENOMEM;
228
229 ret = of_property_read_u32_array(np, "voltage-table",
230 (u32 *)duty_cycle_table,
231 length / sizeof(u32));
232 if (ret) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530233 dev_err(&pdev->dev, "Failed to read voltage-table: %d\n", ret);
Lee Jonesf9178da2015-07-06 09:58:29 +0100234 return ret;
235 }
236
237 drvdata->duty_cycle_table = duty_cycle_table;
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530238 memcpy(&drvdata->ops, &pwm_regulator_voltage_table_ops,
239 sizeof(drvdata->ops));
240 drvdata->desc.ops = &drvdata->ops;
241 drvdata->desc.n_voltages = length / sizeof(*duty_cycle_table);
Lee Jonesf9178da2015-07-06 09:58:29 +0100242
243 return 0;
244}
245
Lee Jones4773be12015-07-07 16:06:51 +0100246static int pwm_regulator_init_continuous(struct platform_device *pdev,
247 struct pwm_regulator_data *drvdata)
248{
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530249 memcpy(&drvdata->ops, &pwm_regulator_voltage_continuous_ops,
250 sizeof(drvdata->ops));
251 drvdata->desc.ops = &drvdata->ops;
252 drvdata->desc.continuous_voltage_range = true;
Lee Jones4773be12015-07-07 16:06:51 +0100253
254 return 0;
255}
256
Chris Zhongaa66cc62014-09-28 10:28:53 +0800257static int pwm_regulator_probe(struct platform_device *pdev)
258{
Lee Jones5ad2cb12015-07-07 16:06:53 +0100259 const struct regulator_init_data *init_data;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800260 struct pwm_regulator_data *drvdata;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800261 struct regulator_dev *regulator;
262 struct regulator_config config = { };
263 struct device_node *np = pdev->dev.of_node;
Alexandre Courbot27bfa882016-06-23 16:39:44 +0900264 enum gpiod_flags gpio_flags;
Lee Jonesf9178da2015-07-06 09:58:29 +0100265 int ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800266
267 if (!np) {
268 dev_err(&pdev->dev, "Device Tree node missing\n");
269 return -EINVAL;
270 }
271
272 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
273 if (!drvdata)
274 return -ENOMEM;
275
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530276 memcpy(&drvdata->desc, &pwm_regulator_desc, sizeof(drvdata->desc));
277
Lee Jones4773be12015-07-07 16:06:51 +0100278 if (of_find_property(np, "voltage-table", NULL))
Lee Jonesf9178da2015-07-06 09:58:29 +0100279 ret = pwm_regulator_init_table(pdev, drvdata);
Lee Jones4773be12015-07-07 16:06:51 +0100280 else
281 ret = pwm_regulator_init_continuous(pdev, drvdata);
282 if (ret)
283 return ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800284
Lee Jones5ad2cb12015-07-07 16:06:53 +0100285 init_data = of_get_regulator_init_data(&pdev->dev, np,
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530286 &drvdata->desc);
Lee Jones5ad2cb12015-07-07 16:06:53 +0100287 if (!init_data)
Chris Zhongaa66cc62014-09-28 10:28:53 +0800288 return -ENOMEM;
289
290 config.of_node = np;
291 config.dev = &pdev->dev;
292 config.driver_data = drvdata;
Lee Jones5ad2cb12015-07-07 16:06:53 +0100293 config.init_data = init_data;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800294
295 drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
296 if (IS_ERR(drvdata->pwm)) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530297 ret = PTR_ERR(drvdata->pwm);
298 dev_err(&pdev->dev, "Failed to get PWM: %d\n", ret);
299 return ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800300 }
301
Alexandre Courbot27bfa882016-06-23 16:39:44 +0900302 if (init_data->constraints.boot_on || init_data->constraints.always_on)
303 gpio_flags = GPIOD_OUT_HIGH;
304 else
305 gpio_flags = GPIOD_OUT_LOW;
306 drvdata->enb_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
307 gpio_flags);
308 if (IS_ERR(drvdata->enb_gpio)) {
309 ret = PTR_ERR(drvdata->enb_gpio);
310 dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n", ret);
311 return ret;
312 }
313
Boris Brezillon8c12ad82016-04-14 21:17:27 +0200314 /*
315 * FIXME: pwm_apply_args() should be removed when switching to the
316 * atomic PWM API.
317 */
318 pwm_apply_args(drvdata->pwm);
319
Chris Zhongaa66cc62014-09-28 10:28:53 +0800320 regulator = devm_regulator_register(&pdev->dev,
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530321 &drvdata->desc, &config);
Chris Zhongaa66cc62014-09-28 10:28:53 +0800322 if (IS_ERR(regulator)) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530323 ret = PTR_ERR(regulator);
324 dev_err(&pdev->dev, "Failed to register regulator %s: %d\n",
325 drvdata->desc.name, ret);
326 return ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800327 }
328
329 return 0;
330}
331
332static const struct of_device_id pwm_of_match[] = {
333 { .compatible = "pwm-regulator" },
334 { },
335};
336MODULE_DEVICE_TABLE(of, pwm_of_match);
337
338static struct platform_driver pwm_regulator_driver = {
339 .driver = {
340 .name = "pwm-regulator",
Chris Zhongaa66cc62014-09-28 10:28:53 +0800341 .of_match_table = of_match_ptr(pwm_of_match),
342 },
343 .probe = pwm_regulator_probe,
344};
345
346module_platform_driver(pwm_regulator_driver);
347
348MODULE_LICENSE("GPL");
349MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
350MODULE_DESCRIPTION("PWM Regulator Driver");
351MODULE_ALIAS("platform:pwm-regulator");