1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2aa66cc66SChris Zhong /*
3aa66cc66SChris Zhong * Regulator driver for PWM Regulators
4aa66cc66SChris Zhong *
5aa66cc66SChris Zhong * Copyright (C) 2014 - STMicroelectronics Inc.
6aa66cc66SChris Zhong *
7aa66cc66SChris Zhong * Author: Lee Jones <lee.jones@linaro.org>
8aa66cc66SChris Zhong */
9aa66cc66SChris Zhong
10aa66cc66SChris Zhong #include <linux/module.h>
11aa66cc66SChris Zhong #include <linux/init.h>
12aa66cc66SChris Zhong #include <linux/err.h>
13045a44d4SRob Herring #include <linux/platform_device.h>
14aa66cc66SChris Zhong #include <linux/regulator/driver.h>
15aa66cc66SChris Zhong #include <linux/regulator/machine.h>
16aa66cc66SChris Zhong #include <linux/regulator/of_regulator.h>
17aa66cc66SChris Zhong #include <linux/of.h>
18aa66cc66SChris Zhong #include <linux/pwm.h>
1927bfa889SAlexandre Courbot #include <linux/gpio/consumer.h>
20aa66cc66SChris Zhong
21ea398e28SBoris Brezillon struct pwm_continuous_reg_data {
22ea398e28SBoris Brezillon unsigned int min_uV_dutycycle;
23ea398e28SBoris Brezillon unsigned int max_uV_dutycycle;
24ea398e28SBoris Brezillon unsigned int dutycycle_unit;
25ea398e28SBoris Brezillon };
26ea398e28SBoris Brezillon
27aa66cc66SChris Zhong struct pwm_regulator_data {
284773be18SLee Jones /* Shared */
29aa66cc66SChris Zhong struct pwm_device *pwm;
304773be18SLee Jones
314773be18SLee Jones /* Voltage table */
324773be18SLee Jones struct pwm_voltages *duty_cycle_table;
33f907a0a9SLaxman Dewangan
34ea398e28SBoris Brezillon /* Continuous mode info */
35ea398e28SBoris Brezillon struct pwm_continuous_reg_data continuous;
36ea398e28SBoris Brezillon
37f907a0a9SLaxman Dewangan /* regulator descriptor */
38f907a0a9SLaxman Dewangan struct regulator_desc desc;
39f907a0a9SLaxman Dewangan
40aa66cc66SChris Zhong int state;
414773be18SLee Jones
4227bfa889SAlexandre Courbot /* Enable GPIO */
4327bfa889SAlexandre Courbot struct gpio_desc *enb_gpio;
44aa66cc66SChris Zhong };
45aa66cc66SChris Zhong
46aa66cc66SChris Zhong struct pwm_voltages {
47aa66cc66SChris Zhong unsigned int uV;
48aa66cc66SChris Zhong unsigned int dutycycle;
49aa66cc66SChris Zhong };
50aa66cc66SChris Zhong
514e773e73SLee Jones /*
524773be18SLee Jones * Voltage table call-backs
534773be18SLee Jones */
pwm_regulator_init_state(struct regulator_dev * rdev)5487248991SBoris Brezillon static void pwm_regulator_init_state(struct regulator_dev *rdev)
5587248991SBoris Brezillon {
5687248991SBoris Brezillon struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
5787248991SBoris Brezillon struct pwm_state pwm_state;
5887248991SBoris Brezillon unsigned int dutycycle;
5987248991SBoris Brezillon int i;
6087248991SBoris Brezillon
6187248991SBoris Brezillon pwm_get_state(drvdata->pwm, &pwm_state);
6287248991SBoris Brezillon dutycycle = pwm_get_relative_duty_cycle(&pwm_state, 100);
6387248991SBoris Brezillon
6487248991SBoris Brezillon for (i = 0; i < rdev->desc->n_voltages; i++) {
6587248991SBoris Brezillon if (dutycycle == drvdata->duty_cycle_table[i].dutycycle) {
6687248991SBoris Brezillon drvdata->state = i;
6787248991SBoris Brezillon return;
6887248991SBoris Brezillon }
6987248991SBoris Brezillon }
7087248991SBoris Brezillon }
7187248991SBoris Brezillon
pwm_regulator_get_voltage_sel(struct regulator_dev * rdev)72ab101e35SLee Jones static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev)
73aa66cc66SChris Zhong {
74ab101e35SLee Jones struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
75aa66cc66SChris Zhong
7687248991SBoris Brezillon if (drvdata->state < 0)
7787248991SBoris Brezillon pwm_regulator_init_state(rdev);
7887248991SBoris Brezillon
79aa66cc66SChris Zhong return drvdata->state;
80aa66cc66SChris Zhong }
81aa66cc66SChris Zhong
pwm_regulator_set_voltage_sel(struct regulator_dev * rdev,unsigned selector)82ab101e35SLee Jones static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev,
83aa66cc66SChris Zhong unsigned selector)
84aa66cc66SChris Zhong {
85ab101e35SLee Jones struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
863f4eb39bSBoris Brezillon struct pwm_state pstate;
87aa66cc66SChris Zhong int ret;
88aa66cc66SChris Zhong
893f4eb39bSBoris Brezillon pwm_init_state(drvdata->pwm, &pstate);
903f4eb39bSBoris Brezillon pwm_set_relative_duty_cycle(&pstate,
913f4eb39bSBoris Brezillon drvdata->duty_cycle_table[selector].dutycycle, 100);
92aa66cc66SChris Zhong
93a10c3d5fSSean Young ret = pwm_apply_might_sleep(drvdata->pwm, &pstate);
94aa66cc66SChris Zhong if (ret) {
955bf59bd5SLaxman Dewangan dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
96aa66cc66SChris Zhong return ret;
97aa66cc66SChris Zhong }
98aa66cc66SChris Zhong
99aa66cc66SChris Zhong drvdata->state = selector;
100aa66cc66SChris Zhong
101aa66cc66SChris Zhong return 0;
102aa66cc66SChris Zhong }
103aa66cc66SChris Zhong
pwm_regulator_list_voltage(struct regulator_dev * rdev,unsigned selector)104ab101e35SLee Jones static int pwm_regulator_list_voltage(struct regulator_dev *rdev,
105aa66cc66SChris Zhong unsigned selector)
106aa66cc66SChris Zhong {
107ab101e35SLee Jones struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
108aa66cc66SChris Zhong
109ab101e35SLee Jones if (selector >= rdev->desc->n_voltages)
110aa66cc66SChris Zhong return -EINVAL;
111aa66cc66SChris Zhong
112aa66cc66SChris Zhong return drvdata->duty_cycle_table[selector].uV;
113aa66cc66SChris Zhong }
1144773be18SLee Jones
pwm_regulator_enable(struct regulator_dev * dev)1151de7d802SBoris Brezillon static int pwm_regulator_enable(struct regulator_dev *dev)
1161de7d802SBoris Brezillon {
1171de7d802SBoris Brezillon struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
1181de7d802SBoris Brezillon
11927bfa889SAlexandre Courbot gpiod_set_value_cansleep(drvdata->enb_gpio, 1);
12027bfa889SAlexandre Courbot
1211de7d802SBoris Brezillon return pwm_enable(drvdata->pwm);
1221de7d802SBoris Brezillon }
1231de7d802SBoris Brezillon
pwm_regulator_disable(struct regulator_dev * dev)1241de7d802SBoris Brezillon static int pwm_regulator_disable(struct regulator_dev *dev)
1251de7d802SBoris Brezillon {
1261de7d802SBoris Brezillon struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
1271de7d802SBoris Brezillon
1281de7d802SBoris Brezillon pwm_disable(drvdata->pwm);
1291de7d802SBoris Brezillon
13027bfa889SAlexandre Courbot gpiod_set_value_cansleep(drvdata->enb_gpio, 0);
13127bfa889SAlexandre Courbot
1321de7d802SBoris Brezillon return 0;
1331de7d802SBoris Brezillon }
1341de7d802SBoris Brezillon
pwm_regulator_is_enabled(struct regulator_dev * dev)1351de7d802SBoris Brezillon static int pwm_regulator_is_enabled(struct regulator_dev *dev)
1361de7d802SBoris Brezillon {
1371de7d802SBoris Brezillon struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
1381de7d802SBoris Brezillon
13927bfa889SAlexandre Courbot if (drvdata->enb_gpio && !gpiod_get_value_cansleep(drvdata->enb_gpio))
14027bfa889SAlexandre Courbot return false;
14127bfa889SAlexandre Courbot
1421de7d802SBoris Brezillon return pwm_is_enabled(drvdata->pwm);
1431de7d802SBoris Brezillon }
1441de7d802SBoris Brezillon
pwm_regulator_get_voltage(struct regulator_dev * rdev)1454773be18SLee Jones static int pwm_regulator_get_voltage(struct regulator_dev *rdev)
1464773be18SLee Jones {
1474773be18SLee Jones struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
148ea398e28SBoris Brezillon unsigned int min_uV_duty = drvdata->continuous.min_uV_dutycycle;
149ea398e28SBoris Brezillon unsigned int max_uV_duty = drvdata->continuous.max_uV_dutycycle;
150ea398e28SBoris Brezillon unsigned int duty_unit = drvdata->continuous.dutycycle_unit;
151d9070fdbSBoris Brezillon int min_uV = rdev->constraints->min_uV;
152ea398e28SBoris Brezillon int max_uV = rdev->constraints->max_uV;
153ea398e28SBoris Brezillon int diff_uV = max_uV - min_uV;
154d9070fdbSBoris Brezillon struct pwm_state pstate;
155ea398e28SBoris Brezillon unsigned int diff_duty;
156ea398e28SBoris Brezillon unsigned int voltage;
1574773be18SLee Jones
158d9070fdbSBoris Brezillon pwm_get_state(drvdata->pwm, &pstate);
159d9070fdbSBoris Brezillon
160ea398e28SBoris Brezillon voltage = pwm_get_relative_duty_cycle(&pstate, duty_unit);
161e1b38b91SMartin Blumenstingl if (voltage < min(max_uV_duty, min_uV_duty) ||
162e1b38b91SMartin Blumenstingl voltage > max(max_uV_duty, min_uV_duty))
163e1b38b91SMartin Blumenstingl return -ENOTRECOVERABLE;
164ea398e28SBoris Brezillon
165ea398e28SBoris Brezillon /*
166ea398e28SBoris Brezillon * The dutycycle for min_uV might be greater than the one for max_uV.
167ea398e28SBoris Brezillon * This is happening when the user needs an inversed polarity, but the
168ea398e28SBoris Brezillon * PWM device does not support inversing it in hardware.
169ea398e28SBoris Brezillon */
170ea398e28SBoris Brezillon if (max_uV_duty < min_uV_duty) {
171ea398e28SBoris Brezillon voltage = min_uV_duty - voltage;
172ea398e28SBoris Brezillon diff_duty = min_uV_duty - max_uV_duty;
173ea398e28SBoris Brezillon } else {
174ea398e28SBoris Brezillon voltage = voltage - min_uV_duty;
175ea398e28SBoris Brezillon diff_duty = max_uV_duty - min_uV_duty;
176ea398e28SBoris Brezillon }
177ea398e28SBoris Brezillon
178ea398e28SBoris Brezillon voltage = DIV_ROUND_CLOSEST_ULL((u64)voltage * diff_uV, diff_duty);
179ea398e28SBoris Brezillon
180ea398e28SBoris Brezillon return voltage + min_uV;
1814773be18SLee Jones }
1824773be18SLee Jones
pwm_regulator_set_voltage(struct regulator_dev * rdev,int req_min_uV,int req_max_uV,unsigned int * selector)1834773be18SLee Jones static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
184ea398e28SBoris Brezillon int req_min_uV, int req_max_uV,
185ea398e28SBoris Brezillon unsigned int *selector)
1864773be18SLee Jones {
1874773be18SLee Jones struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
188ea398e28SBoris Brezillon unsigned int min_uV_duty = drvdata->continuous.min_uV_dutycycle;
189ea398e28SBoris Brezillon unsigned int max_uV_duty = drvdata->continuous.max_uV_dutycycle;
190ea398e28SBoris Brezillon unsigned int duty_unit = drvdata->continuous.dutycycle_unit;
191ea398e28SBoris Brezillon int min_uV = rdev->constraints->min_uV;
192ea398e28SBoris Brezillon int max_uV = rdev->constraints->max_uV;
193ea398e28SBoris Brezillon int diff_uV = max_uV - min_uV;
1943f4eb39bSBoris Brezillon struct pwm_state pstate;
195ea398e28SBoris Brezillon unsigned int diff_duty;
196ea398e28SBoris Brezillon unsigned int dutycycle;
1974773be18SLee Jones int ret;
1984773be18SLee Jones
1993f4eb39bSBoris Brezillon pwm_init_state(drvdata->pwm, &pstate);
2004773be18SLee Jones
201ea398e28SBoris Brezillon /*
202ea398e28SBoris Brezillon * The dutycycle for min_uV might be greater than the one for max_uV.
203ea398e28SBoris Brezillon * This is happening when the user needs an inversed polarity, but the
204ea398e28SBoris Brezillon * PWM device does not support inversing it in hardware.
205ea398e28SBoris Brezillon */
206ea398e28SBoris Brezillon if (max_uV_duty < min_uV_duty)
207ea398e28SBoris Brezillon diff_duty = min_uV_duty - max_uV_duty;
208ea398e28SBoris Brezillon else
209ea398e28SBoris Brezillon diff_duty = max_uV_duty - min_uV_duty;
210ea398e28SBoris Brezillon
211ea398e28SBoris Brezillon dutycycle = DIV_ROUND_CLOSEST_ULL((u64)(req_min_uV - min_uV) *
212ea398e28SBoris Brezillon diff_duty,
213ea398e28SBoris Brezillon diff_uV);
214ea398e28SBoris Brezillon
215ea398e28SBoris Brezillon if (max_uV_duty < min_uV_duty)
216ea398e28SBoris Brezillon dutycycle = min_uV_duty - dutycycle;
217ea398e28SBoris Brezillon else
218ea398e28SBoris Brezillon dutycycle = min_uV_duty + dutycycle;
219ea398e28SBoris Brezillon
220ea398e28SBoris Brezillon pwm_set_relative_duty_cycle(&pstate, dutycycle, duty_unit);
221fd786fb0SLaxman Dewangan
222a10c3d5fSSean Young ret = pwm_apply_might_sleep(drvdata->pwm, &pstate);
2234773be18SLee Jones if (ret) {
2245bf59bd5SLaxman Dewangan dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
2254773be18SLee Jones return ret;
2264773be18SLee Jones }
2274773be18SLee Jones
2284773be18SLee Jones return 0;
2294773be18SLee Jones }
2304773be18SLee Jones
231638aef7aSAxel Lin static const struct regulator_ops pwm_regulator_voltage_table_ops = {
232aa66cc66SChris Zhong .set_voltage_sel = pwm_regulator_set_voltage_sel,
233aa66cc66SChris Zhong .get_voltage_sel = pwm_regulator_get_voltage_sel,
234aa66cc66SChris Zhong .list_voltage = pwm_regulator_list_voltage,
235aa66cc66SChris Zhong .map_voltage = regulator_map_voltage_iterate,
2361de7d802SBoris Brezillon .enable = pwm_regulator_enable,
2371de7d802SBoris Brezillon .disable = pwm_regulator_disable,
2381de7d802SBoris Brezillon .is_enabled = pwm_regulator_is_enabled,
239aa66cc66SChris Zhong };
240aa66cc66SChris Zhong
241638aef7aSAxel Lin static const struct regulator_ops pwm_regulator_voltage_continuous_ops = {
2424773be18SLee Jones .get_voltage = pwm_regulator_get_voltage,
2434773be18SLee Jones .set_voltage = pwm_regulator_set_voltage,
2441de7d802SBoris Brezillon .enable = pwm_regulator_enable,
2451de7d802SBoris Brezillon .disable = pwm_regulator_disable,
2461de7d802SBoris Brezillon .is_enabled = pwm_regulator_is_enabled,
2474773be18SLee Jones };
2484773be18SLee Jones
249638aef7aSAxel Lin static const struct regulator_desc pwm_regulator_desc = {
250aa66cc66SChris Zhong .name = "pwm-regulator",
251aa66cc66SChris Zhong .type = REGULATOR_VOLTAGE,
252aa66cc66SChris Zhong .owner = THIS_MODULE,
253aa66cc66SChris Zhong .supply_name = "pwm",
254aa66cc66SChris Zhong };
255aa66cc66SChris Zhong
pwm_regulator_init_table(struct platform_device * pdev,struct pwm_regulator_data * drvdata)256f9178dadSLee Jones static int pwm_regulator_init_table(struct platform_device *pdev,
257f9178dadSLee Jones struct pwm_regulator_data *drvdata)
258f9178dadSLee Jones {
259f9178dadSLee Jones struct device_node *np = pdev->dev.of_node;
260f9178dadSLee Jones struct pwm_voltages *duty_cycle_table;
26160cb65ebSLee Jones unsigned int length = 0;
262f9178dadSLee Jones int ret;
263f9178dadSLee Jones
264f9178dadSLee Jones of_find_property(np, "voltage-table", &length);
265f9178dadSLee Jones
266f9178dadSLee Jones if ((length < sizeof(*duty_cycle_table)) ||
267f9178dadSLee Jones (length % sizeof(*duty_cycle_table))) {
2685bf59bd5SLaxman Dewangan dev_err(&pdev->dev, "voltage-table length(%d) is invalid\n",
269f9178dadSLee Jones length);
270f9178dadSLee Jones return -EINVAL;
271f9178dadSLee Jones }
272f9178dadSLee Jones
273f9178dadSLee Jones duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
274f9178dadSLee Jones if (!duty_cycle_table)
275f9178dadSLee Jones return -ENOMEM;
276f9178dadSLee Jones
277f9178dadSLee Jones ret = of_property_read_u32_array(np, "voltage-table",
278f9178dadSLee Jones (u32 *)duty_cycle_table,
279f9178dadSLee Jones length / sizeof(u32));
280f9178dadSLee Jones if (ret) {
2815bf59bd5SLaxman Dewangan dev_err(&pdev->dev, "Failed to read voltage-table: %d\n", ret);
282f9178dadSLee Jones return ret;
283f9178dadSLee Jones }
284f9178dadSLee Jones
28559ae97a7SVincent Whitchurch drvdata->state = -ENOTRECOVERABLE;
286f9178dadSLee Jones drvdata->duty_cycle_table = duty_cycle_table;
287638aef7aSAxel Lin drvdata->desc.ops = &pwm_regulator_voltage_table_ops;
288f907a0a9SLaxman Dewangan drvdata->desc.n_voltages = length / sizeof(*duty_cycle_table);
289f9178dadSLee Jones
290f9178dadSLee Jones return 0;
291f9178dadSLee Jones }
292f9178dadSLee Jones
pwm_regulator_init_continuous(struct platform_device * pdev,struct pwm_regulator_data * drvdata)2934773be18SLee Jones static int pwm_regulator_init_continuous(struct platform_device *pdev,
2944773be18SLee Jones struct pwm_regulator_data *drvdata)
2954773be18SLee Jones {
296ea398e28SBoris Brezillon u32 dutycycle_range[2] = { 0, 100 };
297ea398e28SBoris Brezillon u32 dutycycle_unit = 100;
298ea398e28SBoris Brezillon
299638aef7aSAxel Lin drvdata->desc.ops = &pwm_regulator_voltage_continuous_ops;
300f907a0a9SLaxman Dewangan drvdata->desc.continuous_voltage_range = true;
3014773be18SLee Jones
302ea398e28SBoris Brezillon of_property_read_u32_array(pdev->dev.of_node,
303ea398e28SBoris Brezillon "pwm-dutycycle-range",
304ea398e28SBoris Brezillon dutycycle_range, 2);
305ea398e28SBoris Brezillon of_property_read_u32(pdev->dev.of_node, "pwm-dutycycle-unit",
306ea398e28SBoris Brezillon &dutycycle_unit);
307ea398e28SBoris Brezillon
308ea398e28SBoris Brezillon if (dutycycle_range[0] > dutycycle_unit ||
309ea398e28SBoris Brezillon dutycycle_range[1] > dutycycle_unit)
310ea398e28SBoris Brezillon return -EINVAL;
311ea398e28SBoris Brezillon
312ea398e28SBoris Brezillon drvdata->continuous.dutycycle_unit = dutycycle_unit;
313ea398e28SBoris Brezillon drvdata->continuous.min_uV_dutycycle = dutycycle_range[0];
314ea398e28SBoris Brezillon drvdata->continuous.max_uV_dutycycle = dutycycle_range[1];
315ea398e28SBoris Brezillon
3164773be18SLee Jones return 0;
3174773be18SLee Jones }
3184773be18SLee Jones
pwm_regulator_probe(struct platform_device * pdev)319aa66cc66SChris Zhong static int pwm_regulator_probe(struct platform_device *pdev)
320aa66cc66SChris Zhong {
3215ad2cb14SLee Jones const struct regulator_init_data *init_data;
322aa66cc66SChris Zhong struct pwm_regulator_data *drvdata;
323aa66cc66SChris Zhong struct regulator_dev *regulator;
324aa66cc66SChris Zhong struct regulator_config config = { };
325aa66cc66SChris Zhong struct device_node *np = pdev->dev.of_node;
32627bfa889SAlexandre Courbot enum gpiod_flags gpio_flags;
327f9178dadSLee Jones int ret;
328aa66cc66SChris Zhong
329aa66cc66SChris Zhong if (!np) {
330aa66cc66SChris Zhong dev_err(&pdev->dev, "Device Tree node missing\n");
331aa66cc66SChris Zhong return -EINVAL;
332aa66cc66SChris Zhong }
333aa66cc66SChris Zhong
334aa66cc66SChris Zhong drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
335aa66cc66SChris Zhong if (!drvdata)
336aa66cc66SChris Zhong return -ENOMEM;
337aa66cc66SChris Zhong
338f907a0a9SLaxman Dewangan memcpy(&drvdata->desc, &pwm_regulator_desc, sizeof(drvdata->desc));
339f907a0a9SLaxman Dewangan
3407dda20c9SRob Herring if (of_property_present(np, "voltage-table"))
341f9178dadSLee Jones ret = pwm_regulator_init_table(pdev, drvdata);
3424773be18SLee Jones else
3434773be18SLee Jones ret = pwm_regulator_init_continuous(pdev, drvdata);
344f9178dadSLee Jones if (ret)
345aa66cc66SChris Zhong return ret;
346aa66cc66SChris Zhong
3475ad2cb14SLee Jones init_data = of_get_regulator_init_data(&pdev->dev, np,
348f907a0a9SLaxman Dewangan &drvdata->desc);
3495ad2cb14SLee Jones if (!init_data)
350aa66cc66SChris Zhong return -ENOMEM;
351aa66cc66SChris Zhong
352aa66cc66SChris Zhong config.of_node = np;
353aa66cc66SChris Zhong config.dev = &pdev->dev;
354aa66cc66SChris Zhong config.driver_data = drvdata;
3555ad2cb14SLee Jones config.init_data = init_data;
356aa66cc66SChris Zhong
357aa66cc66SChris Zhong drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
358e458d3f3SAnand Moon if (IS_ERR(drvdata->pwm))
359e458d3f3SAnand Moon return dev_err_probe(&pdev->dev, PTR_ERR(drvdata->pwm),
360e458d3f3SAnand Moon "Failed to get PWM\n");
361aa66cc66SChris Zhong
36227bfa889SAlexandre Courbot if (init_data->constraints.boot_on || init_data->constraints.always_on)
36327bfa889SAlexandre Courbot gpio_flags = GPIOD_OUT_HIGH;
36427bfa889SAlexandre Courbot else
36527bfa889SAlexandre Courbot gpio_flags = GPIOD_OUT_LOW;
36627bfa889SAlexandre Courbot drvdata->enb_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
36727bfa889SAlexandre Courbot gpio_flags);
36827bfa889SAlexandre Courbot if (IS_ERR(drvdata->enb_gpio)) {
36927bfa889SAlexandre Courbot ret = PTR_ERR(drvdata->enb_gpio);
37027bfa889SAlexandre Courbot dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n", ret);
37127bfa889SAlexandre Courbot return ret;
37227bfa889SAlexandre Courbot }
37327bfa889SAlexandre Courbot
374fd4f99c4SBoris Brezillon ret = pwm_adjust_config(drvdata->pwm);
375fd4f99c4SBoris Brezillon if (ret)
376fd4f99c4SBoris Brezillon return ret;
3778c12ad8eSBoris Brezillon
378aa66cc66SChris Zhong regulator = devm_regulator_register(&pdev->dev,
379f907a0a9SLaxman Dewangan &drvdata->desc, &config);
380aa66cc66SChris Zhong if (IS_ERR(regulator)) {
3815bf59bd5SLaxman Dewangan ret = PTR_ERR(regulator);
3825bf59bd5SLaxman Dewangan dev_err(&pdev->dev, "Failed to register regulator %s: %d\n",
3835bf59bd5SLaxman Dewangan drvdata->desc.name, ret);
3845bf59bd5SLaxman Dewangan return ret;
385aa66cc66SChris Zhong }
386aa66cc66SChris Zhong
387aa66cc66SChris Zhong return 0;
388aa66cc66SChris Zhong }
389aa66cc66SChris Zhong
390dc8c5ea3SJisheng Zhang static const struct of_device_id __maybe_unused pwm_of_match[] = {
391aa66cc66SChris Zhong { .compatible = "pwm-regulator" },
392aa66cc66SChris Zhong { },
393aa66cc66SChris Zhong };
394aa66cc66SChris Zhong MODULE_DEVICE_TABLE(of, pwm_of_match);
395aa66cc66SChris Zhong
396aa66cc66SChris Zhong static struct platform_driver pwm_regulator_driver = {
397aa66cc66SChris Zhong .driver = {
398aa66cc66SChris Zhong .name = "pwm-regulator",
399259b93b2SDouglas Anderson .probe_type = PROBE_PREFER_ASYNCHRONOUS,
400aa66cc66SChris Zhong .of_match_table = of_match_ptr(pwm_of_match),
401aa66cc66SChris Zhong },
402aa66cc66SChris Zhong .probe = pwm_regulator_probe,
403aa66cc66SChris Zhong };
404aa66cc66SChris Zhong
405aa66cc66SChris Zhong module_platform_driver(pwm_regulator_driver);
406aa66cc66SChris Zhong
407aa66cc66SChris Zhong MODULE_LICENSE("GPL");
408aa66cc66SChris Zhong MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
409aa66cc66SChris Zhong MODULE_DESCRIPTION("PWM Regulator Driver");
410aa66cc66SChris Zhong MODULE_ALIAS("platform:pwm-regulator");
411