blob: 1b88e0e15a70ce2fed55456c11c57d1ee9a64c69 [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
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/err.h>
16#include <linux/regulator/driver.h>
17#include <linux/regulator/machine.h>
18#include <linux/regulator/of_regulator.h>
19#include <linux/of.h>
20#include <linux/of_device.h>
21#include <linux/pwm.h>
Alexandre Courbot27bfa882016-06-23 16:39:44 +090022#include <linux/gpio/consumer.h>
Chris Zhongaa66cc62014-09-28 10:28:53 +080023
Boris Brezillonea398e22016-06-14 11:13:21 +020024struct pwm_continuous_reg_data {
25 unsigned int min_uV_dutycycle;
26 unsigned int max_uV_dutycycle;
27 unsigned int dutycycle_unit;
28};
29
Chris Zhongaa66cc62014-09-28 10:28:53 +080030struct pwm_regulator_data {
Lee Jones4773be12015-07-07 16:06:51 +010031 /* Shared */
Chris Zhongaa66cc62014-09-28 10:28:53 +080032 struct pwm_device *pwm;
Lee Jones4773be12015-07-07 16:06:51 +010033
34 /* Voltage table */
35 struct pwm_voltages *duty_cycle_table;
Laxman Dewanganf907a0a2016-03-08 16:23:22 +053036
Boris Brezillonea398e22016-06-14 11:13:21 +020037 /* Continuous mode info */
38 struct pwm_continuous_reg_data continuous;
39
Laxman Dewanganf907a0a2016-03-08 16:23:22 +053040 /* regulator descriptor */
41 struct regulator_desc desc;
42
43 /* Regulator ops */
44 struct regulator_ops ops;
45
Chris Zhongaa66cc62014-09-28 10:28:53 +080046 int state;
Lee Jones4773be12015-07-07 16:06:51 +010047
Alexandre Courbot27bfa882016-06-23 16:39:44 +090048 /* Enable GPIO */
49 struct gpio_desc *enb_gpio;
Chris Zhongaa66cc62014-09-28 10:28:53 +080050};
51
52struct pwm_voltages {
53 unsigned int uV;
54 unsigned int dutycycle;
55};
56
Lee Jones4773be12015-07-07 16:06:51 +010057/**
58 * Voltage table call-backs
59 */
Boris Brezillon87248992016-06-14 11:13:19 +020060static void pwm_regulator_init_state(struct regulator_dev *rdev)
61{
62 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
63 struct pwm_state pwm_state;
64 unsigned int dutycycle;
65 int i;
66
67 pwm_get_state(drvdata->pwm, &pwm_state);
68 dutycycle = pwm_get_relative_duty_cycle(&pwm_state, 100);
69
70 for (i = 0; i < rdev->desc->n_voltages; i++) {
71 if (dutycycle == drvdata->duty_cycle_table[i].dutycycle) {
72 drvdata->state = i;
73 return;
74 }
75 }
76}
77
Lee Jonesab101e32015-06-05 19:42:47 +010078static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev)
Chris Zhongaa66cc62014-09-28 10:28:53 +080079{
Lee Jonesab101e32015-06-05 19:42:47 +010080 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Chris Zhongaa66cc62014-09-28 10:28:53 +080081
Boris Brezillon87248992016-06-14 11:13:19 +020082 if (drvdata->state < 0)
83 pwm_regulator_init_state(rdev);
84
Chris Zhongaa66cc62014-09-28 10:28:53 +080085 return drvdata->state;
86}
87
Lee Jonesab101e32015-06-05 19:42:47 +010088static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev,
Chris Zhongaa66cc62014-09-28 10:28:53 +080089 unsigned selector)
90{
Lee Jonesab101e32015-06-05 19:42:47 +010091 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Boris Brezillon3f4eb392016-06-14 11:13:18 +020092 struct pwm_state pstate;
Chris Zhongaa66cc62014-09-28 10:28:53 +080093 int ret;
94
Boris Brezillon3f4eb392016-06-14 11:13:18 +020095 pwm_init_state(drvdata->pwm, &pstate);
96 pwm_set_relative_duty_cycle(&pstate,
97 drvdata->duty_cycle_table[selector].dutycycle, 100);
Chris Zhongaa66cc62014-09-28 10:28:53 +080098
Boris Brezillon3f4eb392016-06-14 11:13:18 +020099 ret = pwm_apply_state(drvdata->pwm, &pstate);
Chris Zhongaa66cc62014-09-28 10:28:53 +0800100 if (ret) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530101 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
Chris Zhongaa66cc62014-09-28 10:28:53 +0800102 return ret;
103 }
104
105 drvdata->state = selector;
106
Chris Zhongaa66cc62014-09-28 10:28:53 +0800107 return 0;
108}
109
Lee Jonesab101e32015-06-05 19:42:47 +0100110static int pwm_regulator_list_voltage(struct regulator_dev *rdev,
Chris Zhongaa66cc62014-09-28 10:28:53 +0800111 unsigned selector)
112{
Lee Jonesab101e32015-06-05 19:42:47 +0100113 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Chris Zhongaa66cc62014-09-28 10:28:53 +0800114
Lee Jonesab101e32015-06-05 19:42:47 +0100115 if (selector >= rdev->desc->n_voltages)
Chris Zhongaa66cc62014-09-28 10:28:53 +0800116 return -EINVAL;
117
118 return drvdata->duty_cycle_table[selector].uV;
119}
Lee Jones4773be12015-07-07 16:06:51 +0100120
Boris Brezillon1de7d802015-09-21 11:33:28 +0200121static int pwm_regulator_enable(struct regulator_dev *dev)
122{
123 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
124
Alexandre Courbot27bfa882016-06-23 16:39:44 +0900125 if (drvdata->enb_gpio)
126 gpiod_set_value_cansleep(drvdata->enb_gpio, 1);
127
Boris Brezillon1de7d802015-09-21 11:33:28 +0200128 return pwm_enable(drvdata->pwm);
129}
130
131static int pwm_regulator_disable(struct regulator_dev *dev)
132{
133 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
134
135 pwm_disable(drvdata->pwm);
136
Alexandre Courbot27bfa882016-06-23 16:39:44 +0900137 if (drvdata->enb_gpio)
138 gpiod_set_value_cansleep(drvdata->enb_gpio, 0);
139
Boris Brezillon1de7d802015-09-21 11:33:28 +0200140 return 0;
141}
142
143static int pwm_regulator_is_enabled(struct regulator_dev *dev)
144{
145 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
146
Alexandre Courbot27bfa882016-06-23 16:39:44 +0900147 if (drvdata->enb_gpio && !gpiod_get_value_cansleep(drvdata->enb_gpio))
148 return false;
149
Boris Brezillon1de7d802015-09-21 11:33:28 +0200150 return pwm_is_enabled(drvdata->pwm);
151}
152
Lee Jones4773be12015-07-07 16:06:51 +0100153static int pwm_regulator_get_voltage(struct regulator_dev *rdev)
154{
155 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Boris Brezillonea398e22016-06-14 11:13:21 +0200156 unsigned int min_uV_duty = drvdata->continuous.min_uV_dutycycle;
157 unsigned int max_uV_duty = drvdata->continuous.max_uV_dutycycle;
158 unsigned int duty_unit = drvdata->continuous.dutycycle_unit;
Boris Brezillond9070fd2016-06-14 11:13:20 +0200159 int min_uV = rdev->constraints->min_uV;
Boris Brezillonea398e22016-06-14 11:13:21 +0200160 int max_uV = rdev->constraints->max_uV;
161 int diff_uV = max_uV - min_uV;
Boris Brezillond9070fd2016-06-14 11:13:20 +0200162 struct pwm_state pstate;
Boris Brezillonea398e22016-06-14 11:13:21 +0200163 unsigned int diff_duty;
164 unsigned int voltage;
Lee Jones4773be12015-07-07 16:06:51 +0100165
Boris Brezillond9070fd2016-06-14 11:13:20 +0200166 pwm_get_state(drvdata->pwm, &pstate);
167
Boris Brezillonea398e22016-06-14 11:13:21 +0200168 voltage = pwm_get_relative_duty_cycle(&pstate, duty_unit);
169
170 /*
171 * The dutycycle for min_uV might be greater than the one for max_uV.
172 * This is happening when the user needs an inversed polarity, but the
173 * PWM device does not support inversing it in hardware.
174 */
175 if (max_uV_duty < min_uV_duty) {
176 voltage = min_uV_duty - voltage;
177 diff_duty = min_uV_duty - max_uV_duty;
178 } else {
179 voltage = voltage - min_uV_duty;
180 diff_duty = max_uV_duty - min_uV_duty;
181 }
182
183 voltage = DIV_ROUND_CLOSEST_ULL((u64)voltage * diff_uV, diff_duty);
184
185 return voltage + min_uV;
Lee Jones4773be12015-07-07 16:06:51 +0100186}
187
188static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
Boris Brezillonea398e22016-06-14 11:13:21 +0200189 int req_min_uV, int req_max_uV,
190 unsigned int *selector)
Lee Jones4773be12015-07-07 16:06:51 +0100191{
192 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Boris Brezillonea398e22016-06-14 11:13:21 +0200193 unsigned int min_uV_duty = drvdata->continuous.min_uV_dutycycle;
194 unsigned int max_uV_duty = drvdata->continuous.max_uV_dutycycle;
195 unsigned int duty_unit = drvdata->continuous.dutycycle_unit;
Boris Brezillonea398e22016-06-14 11:13:21 +0200196 int min_uV = rdev->constraints->min_uV;
197 int max_uV = rdev->constraints->max_uV;
198 int diff_uV = max_uV - min_uV;
Boris Brezillon3f4eb392016-06-14 11:13:18 +0200199 struct pwm_state pstate;
Boris Brezillonea398e22016-06-14 11:13:21 +0200200 unsigned int diff_duty;
201 unsigned int dutycycle;
Lee Jones4773be12015-07-07 16:06:51 +0100202 int ret;
203
Boris Brezillon3f4eb392016-06-14 11:13:18 +0200204 pwm_init_state(drvdata->pwm, &pstate);
Lee Jones4773be12015-07-07 16:06:51 +0100205
Boris Brezillonea398e22016-06-14 11:13:21 +0200206 /*
207 * The dutycycle for min_uV might be greater than the one for max_uV.
208 * This is happening when the user needs an inversed polarity, but the
209 * PWM device does not support inversing it in hardware.
210 */
211 if (max_uV_duty < min_uV_duty)
212 diff_duty = min_uV_duty - max_uV_duty;
213 else
214 diff_duty = max_uV_duty - min_uV_duty;
215
216 dutycycle = DIV_ROUND_CLOSEST_ULL((u64)(req_min_uV - min_uV) *
217 diff_duty,
218 diff_uV);
219
220 if (max_uV_duty < min_uV_duty)
221 dutycycle = min_uV_duty - dutycycle;
222 else
223 dutycycle = min_uV_duty + dutycycle;
224
225 pwm_set_relative_duty_cycle(&pstate, dutycycle, duty_unit);
Laxman Dewanganfd786fb2016-04-05 15:09:48 +0530226
Boris Brezillon3f4eb392016-06-14 11:13:18 +0200227 ret = pwm_apply_state(drvdata->pwm, &pstate);
Lee Jones4773be12015-07-07 16:06:51 +0100228 if (ret) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530229 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
Lee Jones4773be12015-07-07 16:06:51 +0100230 return ret;
231 }
232
Lee Jones4773be12015-07-07 16:06:51 +0100233 return 0;
234}
235
Lee Jonesf9178da2015-07-06 09:58:29 +0100236static struct regulator_ops pwm_regulator_voltage_table_ops = {
Chris Zhongaa66cc62014-09-28 10:28:53 +0800237 .set_voltage_sel = pwm_regulator_set_voltage_sel,
238 .get_voltage_sel = pwm_regulator_get_voltage_sel,
239 .list_voltage = pwm_regulator_list_voltage,
240 .map_voltage = regulator_map_voltage_iterate,
Boris Brezillon1de7d802015-09-21 11:33:28 +0200241 .enable = pwm_regulator_enable,
242 .disable = pwm_regulator_disable,
243 .is_enabled = pwm_regulator_is_enabled,
Chris Zhongaa66cc62014-09-28 10:28:53 +0800244};
245
Lee Jones4773be12015-07-07 16:06:51 +0100246static struct regulator_ops pwm_regulator_voltage_continuous_ops = {
247 .get_voltage = pwm_regulator_get_voltage,
248 .set_voltage = pwm_regulator_set_voltage,
Boris Brezillon1de7d802015-09-21 11:33:28 +0200249 .enable = pwm_regulator_enable,
250 .disable = pwm_regulator_disable,
251 .is_enabled = pwm_regulator_is_enabled,
Lee Jones4773be12015-07-07 16:06:51 +0100252};
253
Lee Jonesb6f55e72015-06-05 19:42:45 +0100254static struct regulator_desc pwm_regulator_desc = {
Chris Zhongaa66cc62014-09-28 10:28:53 +0800255 .name = "pwm-regulator",
Chris Zhongaa66cc62014-09-28 10:28:53 +0800256 .type = REGULATOR_VOLTAGE,
257 .owner = THIS_MODULE,
258 .supply_name = "pwm",
259};
260
Lee Jonesf9178da2015-07-06 09:58:29 +0100261static int pwm_regulator_init_table(struct platform_device *pdev,
262 struct pwm_regulator_data *drvdata)
263{
264 struct device_node *np = pdev->dev.of_node;
265 struct pwm_voltages *duty_cycle_table;
Lee Jones60cb65e2015-07-10 08:45:36 +0100266 unsigned int length = 0;
Lee Jonesf9178da2015-07-06 09:58:29 +0100267 int ret;
268
269 of_find_property(np, "voltage-table", &length);
270
271 if ((length < sizeof(*duty_cycle_table)) ||
272 (length % sizeof(*duty_cycle_table))) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530273 dev_err(&pdev->dev, "voltage-table length(%d) is invalid\n",
Lee Jonesf9178da2015-07-06 09:58:29 +0100274 length);
275 return -EINVAL;
276 }
277
278 duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
279 if (!duty_cycle_table)
280 return -ENOMEM;
281
282 ret = of_property_read_u32_array(np, "voltage-table",
283 (u32 *)duty_cycle_table,
284 length / sizeof(u32));
285 if (ret) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530286 dev_err(&pdev->dev, "Failed to read voltage-table: %d\n", ret);
Lee Jonesf9178da2015-07-06 09:58:29 +0100287 return ret;
288 }
289
Boris Brezillon87248992016-06-14 11:13:19 +0200290 drvdata->state = -EINVAL;
Lee Jonesf9178da2015-07-06 09:58:29 +0100291 drvdata->duty_cycle_table = duty_cycle_table;
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530292 memcpy(&drvdata->ops, &pwm_regulator_voltage_table_ops,
293 sizeof(drvdata->ops));
294 drvdata->desc.ops = &drvdata->ops;
295 drvdata->desc.n_voltages = length / sizeof(*duty_cycle_table);
Lee Jonesf9178da2015-07-06 09:58:29 +0100296
297 return 0;
298}
299
Lee Jones4773be12015-07-07 16:06:51 +0100300static int pwm_regulator_init_continuous(struct platform_device *pdev,
301 struct pwm_regulator_data *drvdata)
302{
Boris Brezillonea398e22016-06-14 11:13:21 +0200303 u32 dutycycle_range[2] = { 0, 100 };
304 u32 dutycycle_unit = 100;
305
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530306 memcpy(&drvdata->ops, &pwm_regulator_voltage_continuous_ops,
307 sizeof(drvdata->ops));
308 drvdata->desc.ops = &drvdata->ops;
309 drvdata->desc.continuous_voltage_range = true;
Lee Jones4773be12015-07-07 16:06:51 +0100310
Boris Brezillonea398e22016-06-14 11:13:21 +0200311 of_property_read_u32_array(pdev->dev.of_node,
312 "pwm-dutycycle-range",
313 dutycycle_range, 2);
314 of_property_read_u32(pdev->dev.of_node, "pwm-dutycycle-unit",
315 &dutycycle_unit);
316
317 if (dutycycle_range[0] > dutycycle_unit ||
318 dutycycle_range[1] > dutycycle_unit)
319 return -EINVAL;
320
321 drvdata->continuous.dutycycle_unit = dutycycle_unit;
322 drvdata->continuous.min_uV_dutycycle = dutycycle_range[0];
323 drvdata->continuous.max_uV_dutycycle = dutycycle_range[1];
324
Lee Jones4773be12015-07-07 16:06:51 +0100325 return 0;
326}
327
Chris Zhongaa66cc62014-09-28 10:28:53 +0800328static int pwm_regulator_probe(struct platform_device *pdev)
329{
Lee Jones5ad2cb12015-07-07 16:06:53 +0100330 const struct regulator_init_data *init_data;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800331 struct pwm_regulator_data *drvdata;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800332 struct regulator_dev *regulator;
333 struct regulator_config config = { };
334 struct device_node *np = pdev->dev.of_node;
Alexandre Courbot27bfa882016-06-23 16:39:44 +0900335 enum gpiod_flags gpio_flags;
Lee Jonesf9178da2015-07-06 09:58:29 +0100336 int ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800337
338 if (!np) {
339 dev_err(&pdev->dev, "Device Tree node missing\n");
340 return -EINVAL;
341 }
342
343 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
344 if (!drvdata)
345 return -ENOMEM;
346
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530347 memcpy(&drvdata->desc, &pwm_regulator_desc, sizeof(drvdata->desc));
348
Lee Jones4773be12015-07-07 16:06:51 +0100349 if (of_find_property(np, "voltage-table", NULL))
Lee Jonesf9178da2015-07-06 09:58:29 +0100350 ret = pwm_regulator_init_table(pdev, drvdata);
Lee Jones4773be12015-07-07 16:06:51 +0100351 else
352 ret = pwm_regulator_init_continuous(pdev, drvdata);
353 if (ret)
354 return ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800355
Lee Jones5ad2cb12015-07-07 16:06:53 +0100356 init_data = of_get_regulator_init_data(&pdev->dev, np,
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530357 &drvdata->desc);
Lee Jones5ad2cb12015-07-07 16:06:53 +0100358 if (!init_data)
Chris Zhongaa66cc62014-09-28 10:28:53 +0800359 return -ENOMEM;
360
361 config.of_node = np;
362 config.dev = &pdev->dev;
363 config.driver_data = drvdata;
Lee Jones5ad2cb12015-07-07 16:06:53 +0100364 config.init_data = init_data;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800365
366 drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
367 if (IS_ERR(drvdata->pwm)) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530368 ret = PTR_ERR(drvdata->pwm);
369 dev_err(&pdev->dev, "Failed to get PWM: %d\n", ret);
370 return ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800371 }
372
Alexandre Courbot27bfa882016-06-23 16:39:44 +0900373 if (init_data->constraints.boot_on || init_data->constraints.always_on)
374 gpio_flags = GPIOD_OUT_HIGH;
375 else
376 gpio_flags = GPIOD_OUT_LOW;
377 drvdata->enb_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
378 gpio_flags);
379 if (IS_ERR(drvdata->enb_gpio)) {
380 ret = PTR_ERR(drvdata->enb_gpio);
381 dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n", ret);
382 return ret;
383 }
384
Boris Brezillonfd4f99c2016-06-14 11:13:17 +0200385 ret = pwm_adjust_config(drvdata->pwm);
386 if (ret)
387 return ret;
Boris Brezillon8c12ad82016-04-14 21:17:27 +0200388
Chris Zhongaa66cc62014-09-28 10:28:53 +0800389 regulator = devm_regulator_register(&pdev->dev,
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530390 &drvdata->desc, &config);
Chris Zhongaa66cc62014-09-28 10:28:53 +0800391 if (IS_ERR(regulator)) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530392 ret = PTR_ERR(regulator);
393 dev_err(&pdev->dev, "Failed to register regulator %s: %d\n",
394 drvdata->desc.name, ret);
395 return ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800396 }
397
398 return 0;
399}
400
401static const struct of_device_id pwm_of_match[] = {
402 { .compatible = "pwm-regulator" },
403 { },
404};
405MODULE_DEVICE_TABLE(of, pwm_of_match);
406
407static struct platform_driver pwm_regulator_driver = {
408 .driver = {
409 .name = "pwm-regulator",
Chris Zhongaa66cc62014-09-28 10:28:53 +0800410 .of_match_table = of_match_ptr(pwm_of_match),
411 },
412 .probe = pwm_regulator_probe,
413};
414
415module_platform_driver(pwm_regulator_driver);
416
417MODULE_LICENSE("GPL");
418MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
419MODULE_DESCRIPTION("PWM Regulator Driver");
420MODULE_ALIAS("platform:pwm-regulator");