blob: 638329bd0745e99b782309a1a01101c85cf8b9ca [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Chris Zhongaa66cc62014-09-28 10:28:53 +08002/*
3 * Regulator driver for PWM Regulators
4 *
5 * Copyright (C) 2014 - STMicroelectronics Inc.
6 *
7 * Author: Lee Jones <lee.jones@linaro.org>
Chris Zhongaa66cc62014-09-28 10:28:53 +08008 */
9
10#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/err.h>
13#include <linux/regulator/driver.h>
14#include <linux/regulator/machine.h>
15#include <linux/regulator/of_regulator.h>
16#include <linux/of.h>
17#include <linux/of_device.h>
18#include <linux/pwm.h>
Alexandre Courbot27bfa882016-06-23 16:39:44 +090019#include <linux/gpio/consumer.h>
Chris Zhongaa66cc62014-09-28 10:28:53 +080020
Boris Brezillonea398e22016-06-14 11:13:21 +020021struct pwm_continuous_reg_data {
22 unsigned int min_uV_dutycycle;
23 unsigned int max_uV_dutycycle;
24 unsigned int dutycycle_unit;
25};
26
Chris Zhongaa66cc62014-09-28 10:28:53 +080027struct pwm_regulator_data {
Lee Jones4773be12015-07-07 16:06:51 +010028 /* Shared */
Chris Zhongaa66cc62014-09-28 10:28:53 +080029 struct pwm_device *pwm;
Lee Jones4773be12015-07-07 16:06:51 +010030
31 /* Voltage table */
32 struct pwm_voltages *duty_cycle_table;
Laxman Dewanganf907a0a2016-03-08 16:23:22 +053033
Boris Brezillonea398e22016-06-14 11:13:21 +020034 /* Continuous mode info */
35 struct pwm_continuous_reg_data continuous;
36
Laxman Dewanganf907a0a2016-03-08 16:23:22 +053037 /* regulator descriptor */
38 struct regulator_desc desc;
39
Chris Zhongaa66cc62014-09-28 10:28:53 +080040 int state;
Lee Jones4773be12015-07-07 16:06:51 +010041
Alexandre Courbot27bfa882016-06-23 16:39:44 +090042 /* Enable GPIO */
43 struct gpio_desc *enb_gpio;
Chris Zhongaa66cc62014-09-28 10:28:53 +080044};
45
46struct pwm_voltages {
47 unsigned int uV;
48 unsigned int dutycycle;
49};
50
Lee Jones4773be12015-07-07 16:06:51 +010051/**
52 * Voltage table call-backs
53 */
Boris Brezillon87248992016-06-14 11:13:19 +020054static void pwm_regulator_init_state(struct regulator_dev *rdev)
55{
56 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
57 struct pwm_state pwm_state;
58 unsigned int dutycycle;
59 int i;
60
61 pwm_get_state(drvdata->pwm, &pwm_state);
62 dutycycle = pwm_get_relative_duty_cycle(&pwm_state, 100);
63
64 for (i = 0; i < rdev->desc->n_voltages; i++) {
65 if (dutycycle == drvdata->duty_cycle_table[i].dutycycle) {
66 drvdata->state = i;
67 return;
68 }
69 }
70}
71
Lee Jonesab101e32015-06-05 19:42:47 +010072static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev)
Chris Zhongaa66cc62014-09-28 10:28:53 +080073{
Lee Jonesab101e32015-06-05 19:42:47 +010074 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Chris Zhongaa66cc62014-09-28 10:28:53 +080075
Boris Brezillon87248992016-06-14 11:13:19 +020076 if (drvdata->state < 0)
77 pwm_regulator_init_state(rdev);
78
Chris Zhongaa66cc62014-09-28 10:28:53 +080079 return drvdata->state;
80}
81
Lee Jonesab101e32015-06-05 19:42:47 +010082static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev,
Chris Zhongaa66cc62014-09-28 10:28:53 +080083 unsigned selector)
84{
Lee Jonesab101e32015-06-05 19:42:47 +010085 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Boris Brezillon3f4eb392016-06-14 11:13:18 +020086 struct pwm_state pstate;
Chris Zhongaa66cc62014-09-28 10:28:53 +080087 int ret;
88
Boris Brezillon3f4eb392016-06-14 11:13:18 +020089 pwm_init_state(drvdata->pwm, &pstate);
90 pwm_set_relative_duty_cycle(&pstate,
91 drvdata->duty_cycle_table[selector].dutycycle, 100);
Chris Zhongaa66cc62014-09-28 10:28:53 +080092
Boris Brezillon3f4eb392016-06-14 11:13:18 +020093 ret = pwm_apply_state(drvdata->pwm, &pstate);
Chris Zhongaa66cc62014-09-28 10:28:53 +080094 if (ret) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +053095 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
Chris Zhongaa66cc62014-09-28 10:28:53 +080096 return ret;
97 }
98
99 drvdata->state = selector;
100
Chris Zhongaa66cc62014-09-28 10:28:53 +0800101 return 0;
102}
103
Lee Jonesab101e32015-06-05 19:42:47 +0100104static int pwm_regulator_list_voltage(struct regulator_dev *rdev,
Chris Zhongaa66cc62014-09-28 10:28:53 +0800105 unsigned selector)
106{
Lee Jonesab101e32015-06-05 19:42:47 +0100107 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Chris Zhongaa66cc62014-09-28 10:28:53 +0800108
Lee Jonesab101e32015-06-05 19:42:47 +0100109 if (selector >= rdev->desc->n_voltages)
Chris Zhongaa66cc62014-09-28 10:28:53 +0800110 return -EINVAL;
111
112 return drvdata->duty_cycle_table[selector].uV;
113}
Lee Jones4773be12015-07-07 16:06:51 +0100114
Boris Brezillon1de7d802015-09-21 11:33:28 +0200115static int pwm_regulator_enable(struct regulator_dev *dev)
116{
117 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
118
Fabio Estevama4aae5a2017-07-23 23:10:48 -0300119 gpiod_set_value_cansleep(drvdata->enb_gpio, 1);
Alexandre Courbot27bfa882016-06-23 16:39:44 +0900120
Boris Brezillon1de7d802015-09-21 11:33:28 +0200121 return pwm_enable(drvdata->pwm);
122}
123
124static int pwm_regulator_disable(struct regulator_dev *dev)
125{
126 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
127
128 pwm_disable(drvdata->pwm);
129
Fabio Estevama4aae5a2017-07-23 23:10:48 -0300130 gpiod_set_value_cansleep(drvdata->enb_gpio, 0);
Alexandre Courbot27bfa882016-06-23 16:39:44 +0900131
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 Brezillonea398e22016-06-14 11:13:21 +0200148 unsigned int min_uV_duty = drvdata->continuous.min_uV_dutycycle;
149 unsigned int max_uV_duty = drvdata->continuous.max_uV_dutycycle;
150 unsigned int duty_unit = drvdata->continuous.dutycycle_unit;
Boris Brezillond9070fd2016-06-14 11:13:20 +0200151 int min_uV = rdev->constraints->min_uV;
Boris Brezillonea398e22016-06-14 11:13:21 +0200152 int max_uV = rdev->constraints->max_uV;
153 int diff_uV = max_uV - min_uV;
Boris Brezillond9070fd2016-06-14 11:13:20 +0200154 struct pwm_state pstate;
Boris Brezillonea398e22016-06-14 11:13:21 +0200155 unsigned int diff_duty;
156 unsigned int voltage;
Lee Jones4773be12015-07-07 16:06:51 +0100157
Boris Brezillond9070fd2016-06-14 11:13:20 +0200158 pwm_get_state(drvdata->pwm, &pstate);
159
Boris Brezillonea398e22016-06-14 11:13:21 +0200160 voltage = pwm_get_relative_duty_cycle(&pstate, duty_unit);
161
162 /*
163 * The dutycycle for min_uV might be greater than the one for max_uV.
164 * This is happening when the user needs an inversed polarity, but the
165 * PWM device does not support inversing it in hardware.
166 */
167 if (max_uV_duty < min_uV_duty) {
168 voltage = min_uV_duty - voltage;
169 diff_duty = min_uV_duty - max_uV_duty;
170 } else {
171 voltage = voltage - min_uV_duty;
172 diff_duty = max_uV_duty - min_uV_duty;
173 }
174
175 voltage = DIV_ROUND_CLOSEST_ULL((u64)voltage * diff_uV, diff_duty);
176
177 return voltage + min_uV;
Lee Jones4773be12015-07-07 16:06:51 +0100178}
179
180static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
Boris Brezillonea398e22016-06-14 11:13:21 +0200181 int req_min_uV, int req_max_uV,
182 unsigned int *selector)
Lee Jones4773be12015-07-07 16:06:51 +0100183{
184 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Boris Brezillonea398e22016-06-14 11:13:21 +0200185 unsigned int min_uV_duty = drvdata->continuous.min_uV_dutycycle;
186 unsigned int max_uV_duty = drvdata->continuous.max_uV_dutycycle;
187 unsigned int duty_unit = drvdata->continuous.dutycycle_unit;
Boris Brezillonea398e22016-06-14 11:13:21 +0200188 int min_uV = rdev->constraints->min_uV;
189 int max_uV = rdev->constraints->max_uV;
190 int diff_uV = max_uV - min_uV;
Boris Brezillon3f4eb392016-06-14 11:13:18 +0200191 struct pwm_state pstate;
Boris Brezillonea398e22016-06-14 11:13:21 +0200192 unsigned int diff_duty;
193 unsigned int dutycycle;
Lee Jones4773be12015-07-07 16:06:51 +0100194 int ret;
195
Boris Brezillon3f4eb392016-06-14 11:13:18 +0200196 pwm_init_state(drvdata->pwm, &pstate);
Lee Jones4773be12015-07-07 16:06:51 +0100197
Boris Brezillonea398e22016-06-14 11:13:21 +0200198 /*
199 * The dutycycle for min_uV might be greater than the one for max_uV.
200 * This is happening when the user needs an inversed polarity, but the
201 * PWM device does not support inversing it in hardware.
202 */
203 if (max_uV_duty < min_uV_duty)
204 diff_duty = min_uV_duty - max_uV_duty;
205 else
206 diff_duty = max_uV_duty - min_uV_duty;
207
208 dutycycle = DIV_ROUND_CLOSEST_ULL((u64)(req_min_uV - min_uV) *
209 diff_duty,
210 diff_uV);
211
212 if (max_uV_duty < min_uV_duty)
213 dutycycle = min_uV_duty - dutycycle;
214 else
215 dutycycle = min_uV_duty + dutycycle;
216
217 pwm_set_relative_duty_cycle(&pstate, dutycycle, duty_unit);
Laxman Dewanganfd786fb02016-04-05 15:09:48 +0530218
Boris Brezillon3f4eb392016-06-14 11:13:18 +0200219 ret = pwm_apply_state(drvdata->pwm, &pstate);
Lee Jones4773be12015-07-07 16:06:51 +0100220 if (ret) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530221 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
Lee Jones4773be12015-07-07 16:06:51 +0100222 return ret;
223 }
224
Lee Jones4773be12015-07-07 16:06:51 +0100225 return 0;
226}
227
Axel Lin638aef72019-01-12 12:30:13 +0800228static const struct regulator_ops pwm_regulator_voltage_table_ops = {
Chris Zhongaa66cc62014-09-28 10:28:53 +0800229 .set_voltage_sel = pwm_regulator_set_voltage_sel,
230 .get_voltage_sel = pwm_regulator_get_voltage_sel,
231 .list_voltage = pwm_regulator_list_voltage,
232 .map_voltage = regulator_map_voltage_iterate,
Boris Brezillon1de7d802015-09-21 11:33:28 +0200233 .enable = pwm_regulator_enable,
234 .disable = pwm_regulator_disable,
235 .is_enabled = pwm_regulator_is_enabled,
Chris Zhongaa66cc62014-09-28 10:28:53 +0800236};
237
Axel Lin638aef72019-01-12 12:30:13 +0800238static const struct regulator_ops pwm_regulator_voltage_continuous_ops = {
Lee Jones4773be12015-07-07 16:06:51 +0100239 .get_voltage = pwm_regulator_get_voltage,
240 .set_voltage = pwm_regulator_set_voltage,
Boris Brezillon1de7d802015-09-21 11:33:28 +0200241 .enable = pwm_regulator_enable,
242 .disable = pwm_regulator_disable,
243 .is_enabled = pwm_regulator_is_enabled,
Lee Jones4773be12015-07-07 16:06:51 +0100244};
245
Axel Lin638aef72019-01-12 12:30:13 +0800246static const struct regulator_desc pwm_regulator_desc = {
Chris Zhongaa66cc62014-09-28 10:28:53 +0800247 .name = "pwm-regulator",
Chris Zhongaa66cc62014-09-28 10:28:53 +0800248 .type = REGULATOR_VOLTAGE,
249 .owner = THIS_MODULE,
250 .supply_name = "pwm",
251};
252
Lee Jonesf9178da2015-07-06 09:58:29 +0100253static int pwm_regulator_init_table(struct platform_device *pdev,
254 struct pwm_regulator_data *drvdata)
255{
256 struct device_node *np = pdev->dev.of_node;
257 struct pwm_voltages *duty_cycle_table;
Lee Jones60cb65e2015-07-10 08:45:36 +0100258 unsigned int length = 0;
Lee Jonesf9178da2015-07-06 09:58:29 +0100259 int ret;
260
261 of_find_property(np, "voltage-table", &length);
262
263 if ((length < sizeof(*duty_cycle_table)) ||
264 (length % sizeof(*duty_cycle_table))) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530265 dev_err(&pdev->dev, "voltage-table length(%d) is invalid\n",
Lee Jonesf9178da2015-07-06 09:58:29 +0100266 length);
267 return -EINVAL;
268 }
269
270 duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
271 if (!duty_cycle_table)
272 return -ENOMEM;
273
274 ret = of_property_read_u32_array(np, "voltage-table",
275 (u32 *)duty_cycle_table,
276 length / sizeof(u32));
277 if (ret) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530278 dev_err(&pdev->dev, "Failed to read voltage-table: %d\n", ret);
Lee Jonesf9178da2015-07-06 09:58:29 +0100279 return ret;
280 }
281
Boris Brezillon87248992016-06-14 11:13:19 +0200282 drvdata->state = -EINVAL;
Lee Jonesf9178da2015-07-06 09:58:29 +0100283 drvdata->duty_cycle_table = duty_cycle_table;
Axel Lin638aef72019-01-12 12:30:13 +0800284 drvdata->desc.ops = &pwm_regulator_voltage_table_ops;
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530285 drvdata->desc.n_voltages = length / sizeof(*duty_cycle_table);
Lee Jonesf9178da2015-07-06 09:58:29 +0100286
287 return 0;
288}
289
Lee Jones4773be12015-07-07 16:06:51 +0100290static int pwm_regulator_init_continuous(struct platform_device *pdev,
291 struct pwm_regulator_data *drvdata)
292{
Boris Brezillonea398e22016-06-14 11:13:21 +0200293 u32 dutycycle_range[2] = { 0, 100 };
294 u32 dutycycle_unit = 100;
295
Axel Lin638aef72019-01-12 12:30:13 +0800296 drvdata->desc.ops = &pwm_regulator_voltage_continuous_ops;
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530297 drvdata->desc.continuous_voltage_range = true;
Lee Jones4773be12015-07-07 16:06:51 +0100298
Boris Brezillonea398e22016-06-14 11:13:21 +0200299 of_property_read_u32_array(pdev->dev.of_node,
300 "pwm-dutycycle-range",
301 dutycycle_range, 2);
302 of_property_read_u32(pdev->dev.of_node, "pwm-dutycycle-unit",
303 &dutycycle_unit);
304
305 if (dutycycle_range[0] > dutycycle_unit ||
306 dutycycle_range[1] > dutycycle_unit)
307 return -EINVAL;
308
309 drvdata->continuous.dutycycle_unit = dutycycle_unit;
310 drvdata->continuous.min_uV_dutycycle = dutycycle_range[0];
311 drvdata->continuous.max_uV_dutycycle = dutycycle_range[1];
312
Lee Jones4773be12015-07-07 16:06:51 +0100313 return 0;
314}
315
Chris Zhongaa66cc62014-09-28 10:28:53 +0800316static int pwm_regulator_probe(struct platform_device *pdev)
317{
Lee Jones5ad2cb12015-07-07 16:06:53 +0100318 const struct regulator_init_data *init_data;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800319 struct pwm_regulator_data *drvdata;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800320 struct regulator_dev *regulator;
321 struct regulator_config config = { };
322 struct device_node *np = pdev->dev.of_node;
Alexandre Courbot27bfa882016-06-23 16:39:44 +0900323 enum gpiod_flags gpio_flags;
Lee Jonesf9178da2015-07-06 09:58:29 +0100324 int ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800325
326 if (!np) {
327 dev_err(&pdev->dev, "Device Tree node missing\n");
328 return -EINVAL;
329 }
330
331 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
332 if (!drvdata)
333 return -ENOMEM;
334
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530335 memcpy(&drvdata->desc, &pwm_regulator_desc, sizeof(drvdata->desc));
336
Lee Jones4773be12015-07-07 16:06:51 +0100337 if (of_find_property(np, "voltage-table", NULL))
Lee Jonesf9178da2015-07-06 09:58:29 +0100338 ret = pwm_regulator_init_table(pdev, drvdata);
Lee Jones4773be12015-07-07 16:06:51 +0100339 else
340 ret = pwm_regulator_init_continuous(pdev, drvdata);
341 if (ret)
342 return ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800343
Lee Jones5ad2cb12015-07-07 16:06:53 +0100344 init_data = of_get_regulator_init_data(&pdev->dev, np,
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530345 &drvdata->desc);
Lee Jones5ad2cb12015-07-07 16:06:53 +0100346 if (!init_data)
Chris Zhongaa66cc62014-09-28 10:28:53 +0800347 return -ENOMEM;
348
349 config.of_node = np;
350 config.dev = &pdev->dev;
351 config.driver_data = drvdata;
Lee Jones5ad2cb12015-07-07 16:06:53 +0100352 config.init_data = init_data;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800353
354 drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
355 if (IS_ERR(drvdata->pwm)) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530356 ret = PTR_ERR(drvdata->pwm);
Jon Hunter0cd71b92020-03-02 14:14:28 +0000357 if (ret == -EPROBE_DEFER)
358 dev_dbg(&pdev->dev,
359 "Failed to get PWM, deferring probe\n");
360 else
361 dev_err(&pdev->dev, "Failed to get PWM: %d\n", ret);
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530362 return ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800363 }
364
Alexandre Courbot27bfa882016-06-23 16:39:44 +0900365 if (init_data->constraints.boot_on || init_data->constraints.always_on)
366 gpio_flags = GPIOD_OUT_HIGH;
367 else
368 gpio_flags = GPIOD_OUT_LOW;
369 drvdata->enb_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
370 gpio_flags);
371 if (IS_ERR(drvdata->enb_gpio)) {
372 ret = PTR_ERR(drvdata->enb_gpio);
373 dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n", ret);
374 return ret;
375 }
376
Boris Brezillonfd4f99c2016-06-14 11:13:17 +0200377 ret = pwm_adjust_config(drvdata->pwm);
378 if (ret)
379 return ret;
Boris Brezillon8c12ad82016-04-14 21:17:27 +0200380
Chris Zhongaa66cc62014-09-28 10:28:53 +0800381 regulator = devm_regulator_register(&pdev->dev,
Laxman Dewanganf907a0a2016-03-08 16:23:22 +0530382 &drvdata->desc, &config);
Chris Zhongaa66cc62014-09-28 10:28:53 +0800383 if (IS_ERR(regulator)) {
Laxman Dewangan5bf59bd2016-03-14 18:20:13 +0530384 ret = PTR_ERR(regulator);
385 dev_err(&pdev->dev, "Failed to register regulator %s: %d\n",
386 drvdata->desc.name, ret);
387 return ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800388 }
389
390 return 0;
391}
392
393static const struct of_device_id pwm_of_match[] = {
394 { .compatible = "pwm-regulator" },
395 { },
396};
397MODULE_DEVICE_TABLE(of, pwm_of_match);
398
399static struct platform_driver pwm_regulator_driver = {
400 .driver = {
401 .name = "pwm-regulator",
Chris Zhongaa66cc62014-09-28 10:28:53 +0800402 .of_match_table = of_match_ptr(pwm_of_match),
403 },
404 .probe = pwm_regulator_probe,
405};
406
407module_platform_driver(pwm_regulator_driver);
408
409MODULE_LICENSE("GPL");
410MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
411MODULE_DESCRIPTION("PWM Regulator Driver");
412MODULE_ALIAS("platform:pwm-regulator");