13cad5fc8SAxel Lin // SPDX-License-Identifier: GPL-2.0+
23cad5fc8SAxel Lin //
33cad5fc8SAxel Lin // wm831x-dcdc.c  --  DC-DC buck converter driver for the WM831x series
43cad5fc8SAxel Lin //
53cad5fc8SAxel Lin // Copyright 2009 Wolfson Microelectronics PLC.
63cad5fc8SAxel Lin //
73cad5fc8SAxel Lin // Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8e4ee831fSMark Brown 
9e4ee831fSMark Brown #include <linux/module.h>
10e4ee831fSMark Brown #include <linux/moduleparam.h>
11e4ee831fSMark Brown #include <linux/init.h>
12e4ee831fSMark Brown #include <linux/bitops.h>
13e4ee831fSMark Brown #include <linux/err.h>
14e4ee831fSMark Brown #include <linux/i2c.h>
15e4ee831fSMark Brown #include <linux/platform_device.h>
16e4ee831fSMark Brown #include <linux/regulator/driver.h>
17e24a04c4SMark Brown #include <linux/regulator/machine.h>
189a5ed0baSLinus Walleij #include <linux/gpio/consumer.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
20e4ee831fSMark Brown 
21e4ee831fSMark Brown #include <linux/mfd/wm831x/core.h>
22e4ee831fSMark Brown #include <linux/mfd/wm831x/regulator.h>
23e4ee831fSMark Brown #include <linux/mfd/wm831x/pdata.h>
24e4ee831fSMark Brown 
25e4ee831fSMark Brown #define WM831X_BUCKV_MAX_SELECTOR 0x68
26e4ee831fSMark Brown #define WM831X_BUCKP_MAX_SELECTOR 0x66
27e4ee831fSMark Brown 
28e4ee831fSMark Brown #define WM831X_DCDC_MODE_FAST    0
29e4ee831fSMark Brown #define WM831X_DCDC_MODE_NORMAL  1
30e4ee831fSMark Brown #define WM831X_DCDC_MODE_IDLE    2
31e4ee831fSMark Brown #define WM831X_DCDC_MODE_STANDBY 3
32e4ee831fSMark Brown 
3382caa978SMark Brown #define WM831X_DCDC_MAX_NAME 9
34e4ee831fSMark Brown 
35e4ee831fSMark Brown /* Register offsets in control block */
36e4ee831fSMark Brown #define WM831X_DCDC_CONTROL_1     0
37e4ee831fSMark Brown #define WM831X_DCDC_CONTROL_2     1
38e4ee831fSMark Brown #define WM831X_DCDC_ON_CONFIG     2
39e4ee831fSMark Brown #define WM831X_DCDC_SLEEP_CONTROL 3
40e24a04c4SMark Brown #define WM831X_DCDC_DVS_CONTROL   4
41e4ee831fSMark Brown 
42e4ee831fSMark Brown /*
43e4ee831fSMark Brown  * Shared
44e4ee831fSMark Brown  */
45e4ee831fSMark Brown 
46e4ee831fSMark Brown struct wm831x_dcdc {
47e4ee831fSMark Brown 	char name[WM831X_DCDC_MAX_NAME];
4882caa978SMark Brown 	char supply_name[WM831X_DCDC_MAX_NAME];
49e4ee831fSMark Brown 	struct regulator_desc desc;
50e4ee831fSMark Brown 	int base;
51e4ee831fSMark Brown 	struct wm831x *wm831x;
52e4ee831fSMark Brown 	struct regulator_dev *regulator;
539a5ed0baSLinus Walleij 	struct gpio_desc *dvs_gpiod;
54e24a04c4SMark Brown 	int dvs_gpio_state;
55e24a04c4SMark Brown 	int on_vsel;
56e24a04c4SMark Brown 	int dvs_vsel;
57e4ee831fSMark Brown };
58e4ee831fSMark Brown 
wm831x_dcdc_get_mode(struct regulator_dev * rdev)59e4ee831fSMark Brown static unsigned int wm831x_dcdc_get_mode(struct regulator_dev *rdev)
60e4ee831fSMark Brown 
61e4ee831fSMark Brown {
62e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
63e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
64e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
65e4ee831fSMark Brown 	int val;
66e4ee831fSMark Brown 
67e4ee831fSMark Brown 	val = wm831x_reg_read(wm831x, reg);
68e4ee831fSMark Brown 	if (val < 0)
69e4ee831fSMark Brown 		return val;
70e4ee831fSMark Brown 
71e4ee831fSMark Brown 	val = (val & WM831X_DC1_ON_MODE_MASK) >> WM831X_DC1_ON_MODE_SHIFT;
72e4ee831fSMark Brown 
73e4ee831fSMark Brown 	switch (val) {
74e4ee831fSMark Brown 	case WM831X_DCDC_MODE_FAST:
75e4ee831fSMark Brown 		return REGULATOR_MODE_FAST;
76e4ee831fSMark Brown 	case WM831X_DCDC_MODE_NORMAL:
77e4ee831fSMark Brown 		return REGULATOR_MODE_NORMAL;
78e4ee831fSMark Brown 	case WM831X_DCDC_MODE_STANDBY:
79e4ee831fSMark Brown 		return REGULATOR_MODE_STANDBY;
80e4ee831fSMark Brown 	case WM831X_DCDC_MODE_IDLE:
81e4ee831fSMark Brown 		return REGULATOR_MODE_IDLE;
82e4ee831fSMark Brown 	default:
83e4ee831fSMark Brown 		BUG();
849ee291a4SMark Brown 		return -EINVAL;
85e4ee831fSMark Brown 	}
86e4ee831fSMark Brown }
87e4ee831fSMark Brown 
wm831x_dcdc_set_mode_int(struct wm831x * wm831x,int reg,unsigned int mode)88e4ee831fSMark Brown static int wm831x_dcdc_set_mode_int(struct wm831x *wm831x, int reg,
89e4ee831fSMark Brown 				    unsigned int mode)
90e4ee831fSMark Brown {
91e4ee831fSMark Brown 	int val;
92e4ee831fSMark Brown 
93e4ee831fSMark Brown 	switch (mode) {
94e4ee831fSMark Brown 	case REGULATOR_MODE_FAST:
95e4ee831fSMark Brown 		val = WM831X_DCDC_MODE_FAST;
96e4ee831fSMark Brown 		break;
97e4ee831fSMark Brown 	case REGULATOR_MODE_NORMAL:
98e4ee831fSMark Brown 		val = WM831X_DCDC_MODE_NORMAL;
99e4ee831fSMark Brown 		break;
100e4ee831fSMark Brown 	case REGULATOR_MODE_STANDBY:
101e4ee831fSMark Brown 		val = WM831X_DCDC_MODE_STANDBY;
102e4ee831fSMark Brown 		break;
103e4ee831fSMark Brown 	case REGULATOR_MODE_IDLE:
104e4ee831fSMark Brown 		val = WM831X_DCDC_MODE_IDLE;
105e4ee831fSMark Brown 		break;
106e4ee831fSMark Brown 	default:
107e4ee831fSMark Brown 		return -EINVAL;
108e4ee831fSMark Brown 	}
109e4ee831fSMark Brown 
110e4ee831fSMark Brown 	return wm831x_set_bits(wm831x, reg, WM831X_DC1_ON_MODE_MASK,
111e4ee831fSMark Brown 			       val << WM831X_DC1_ON_MODE_SHIFT);
112e4ee831fSMark Brown }
113e4ee831fSMark Brown 
wm831x_dcdc_set_mode(struct regulator_dev * rdev,unsigned int mode)114e4ee831fSMark Brown static int wm831x_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
115e4ee831fSMark Brown {
116e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
117e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
118e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
119e4ee831fSMark Brown 
120e4ee831fSMark Brown 	return wm831x_dcdc_set_mode_int(wm831x, reg, mode);
121e4ee831fSMark Brown }
122e4ee831fSMark Brown 
wm831x_dcdc_set_suspend_mode(struct regulator_dev * rdev,unsigned int mode)123e4ee831fSMark Brown static int wm831x_dcdc_set_suspend_mode(struct regulator_dev *rdev,
124e4ee831fSMark Brown 					unsigned int mode)
125e4ee831fSMark Brown {
126e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
127e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
128e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
129e4ee831fSMark Brown 
130e4ee831fSMark Brown 	return wm831x_dcdc_set_mode_int(wm831x, reg, mode);
131e4ee831fSMark Brown }
132e4ee831fSMark Brown 
wm831x_dcdc_get_status(struct regulator_dev * rdev)133e4ee831fSMark Brown static int wm831x_dcdc_get_status(struct regulator_dev *rdev)
134e4ee831fSMark Brown {
135e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
136e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
137e4ee831fSMark Brown 	int ret;
138e4ee831fSMark Brown 
139e4ee831fSMark Brown 	/* First, check for errors */
140e4ee831fSMark Brown 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
141e4ee831fSMark Brown 	if (ret < 0)
142e4ee831fSMark Brown 		return ret;
143e4ee831fSMark Brown 
144e4ee831fSMark Brown 	if (ret & (1 << rdev_get_id(rdev))) {
145e4ee831fSMark Brown 		dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
146e4ee831fSMark Brown 			rdev_get_id(rdev) + 1);
147e4ee831fSMark Brown 		return REGULATOR_STATUS_ERROR;
148e4ee831fSMark Brown 	}
149e4ee831fSMark Brown 
150e4ee831fSMark Brown 	/* DCDC1 and DCDC2 can additionally detect high voltage/current */
151e4ee831fSMark Brown 	if (rdev_get_id(rdev) < 2) {
152e4ee831fSMark Brown 		if (ret & (WM831X_DC1_OV_STS << rdev_get_id(rdev))) {
153e4ee831fSMark Brown 			dev_dbg(wm831x->dev, "DCDC%d over voltage\n",
154e4ee831fSMark Brown 				rdev_get_id(rdev) + 1);
155e4ee831fSMark Brown 			return REGULATOR_STATUS_ERROR;
156e4ee831fSMark Brown 		}
157e4ee831fSMark Brown 
158e4ee831fSMark Brown 		if (ret & (WM831X_DC1_HC_STS << rdev_get_id(rdev))) {
159e4ee831fSMark Brown 			dev_dbg(wm831x->dev, "DCDC%d over current\n",
160e4ee831fSMark Brown 				rdev_get_id(rdev) + 1);
161e4ee831fSMark Brown 			return REGULATOR_STATUS_ERROR;
162e4ee831fSMark Brown 		}
163e4ee831fSMark Brown 	}
164e4ee831fSMark Brown 
165e4ee831fSMark Brown 	/* Is the regulator on? */
166e4ee831fSMark Brown 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
167e4ee831fSMark Brown 	if (ret < 0)
168e4ee831fSMark Brown 		return ret;
169e4ee831fSMark Brown 	if (!(ret & (1 << rdev_get_id(rdev))))
170e4ee831fSMark Brown 		return REGULATOR_STATUS_OFF;
171e4ee831fSMark Brown 
172e4ee831fSMark Brown 	/* TODO: When we handle hardware control modes so we can report the
173e4ee831fSMark Brown 	 * current mode. */
174e4ee831fSMark Brown 	return REGULATOR_STATUS_ON;
175e4ee831fSMark Brown }
176e4ee831fSMark Brown 
wm831x_dcdc_uv_irq(int irq,void * data)177e4ee831fSMark Brown static irqreturn_t wm831x_dcdc_uv_irq(int irq, void *data)
178e4ee831fSMark Brown {
179e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = data;
180e4ee831fSMark Brown 
181e4ee831fSMark Brown 	regulator_notifier_call_chain(dcdc->regulator,
182e4ee831fSMark Brown 				      REGULATOR_EVENT_UNDER_VOLTAGE,
183e4ee831fSMark Brown 				      NULL);
184e4ee831fSMark Brown 
185e4ee831fSMark Brown 	return IRQ_HANDLED;
186e4ee831fSMark Brown }
187e4ee831fSMark Brown 
wm831x_dcdc_oc_irq(int irq,void * data)188e4ee831fSMark Brown static irqreturn_t wm831x_dcdc_oc_irq(int irq, void *data)
189e4ee831fSMark Brown {
190e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = data;
191e4ee831fSMark Brown 
192e4ee831fSMark Brown 	regulator_notifier_call_chain(dcdc->regulator,
193e4ee831fSMark Brown 				      REGULATOR_EVENT_OVER_CURRENT,
194e4ee831fSMark Brown 				      NULL);
195e4ee831fSMark Brown 
196e4ee831fSMark Brown 	return IRQ_HANDLED;
197e4ee831fSMark Brown }
198e4ee831fSMark Brown 
199e4ee831fSMark Brown /*
200e4ee831fSMark Brown  * BUCKV specifics
201e4ee831fSMark Brown  */
202e4ee831fSMark Brown 
20360ab7f41SMatti Vaittinen static const struct linear_range wm831x_buckv_ranges[] = {
204ccffcb8eSAxel Lin 	REGULATOR_LINEAR_RANGE(600000, 0, 0x7, 0),
205ccffcb8eSAxel Lin 	REGULATOR_LINEAR_RANGE(600000, 0x8, 0x68, 12500),
206ccffcb8eSAxel Lin };
207e24a04c4SMark Brown 
wm831x_buckv_set_dvs(struct regulator_dev * rdev,int state)208e24a04c4SMark Brown static int wm831x_buckv_set_dvs(struct regulator_dev *rdev, int state)
209e24a04c4SMark Brown {
210e24a04c4SMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
211e24a04c4SMark Brown 
212e24a04c4SMark Brown 	if (state == dcdc->dvs_gpio_state)
213e24a04c4SMark Brown 		return 0;
214e24a04c4SMark Brown 
215e24a04c4SMark Brown 	dcdc->dvs_gpio_state = state;
2169a5ed0baSLinus Walleij 	gpiod_set_value(dcdc->dvs_gpiod, state);
217e24a04c4SMark Brown 
218e24a04c4SMark Brown 	/* Should wait for DVS state change to be asserted if we have
219e24a04c4SMark Brown 	 * a GPIO for it, for now assume the device is configured
220e24a04c4SMark Brown 	 * for the fastest possible transition.
221e24a04c4SMark Brown 	 */
222e24a04c4SMark Brown 
223e24a04c4SMark Brown 	return 0;
224e4ee831fSMark Brown }
225e4ee831fSMark Brown 
wm831x_buckv_set_voltage_sel(struct regulator_dev * rdev,unsigned vsel)226b5fb77e0SAxel Lin static int wm831x_buckv_set_voltage_sel(struct regulator_dev *rdev,
227b5fb77e0SAxel Lin 					unsigned vsel)
228e4ee831fSMark Brown {
229e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
230e24a04c4SMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
231e24a04c4SMark Brown 	int on_reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
232e24a04c4SMark Brown 	int dvs_reg = dcdc->base + WM831X_DCDC_DVS_CONTROL;
233b5fb77e0SAxel Lin 	int ret;
2343a93f2a9SMark Brown 
235e24a04c4SMark Brown 	/* If this value is already set then do a GPIO update if we can */
2369a5ed0baSLinus Walleij 	if (dcdc->dvs_gpiod && dcdc->on_vsel == vsel)
237e24a04c4SMark Brown 		return wm831x_buckv_set_dvs(rdev, 0);
238e24a04c4SMark Brown 
2399a5ed0baSLinus Walleij 	if (dcdc->dvs_gpiod && dcdc->dvs_vsel == vsel)
240e24a04c4SMark Brown 		return wm831x_buckv_set_dvs(rdev, 1);
241e24a04c4SMark Brown 
242e24a04c4SMark Brown 	/* Always set the ON status to the minimum voltage */
243e24a04c4SMark Brown 	ret = wm831x_set_bits(wm831x, on_reg, WM831X_DC1_ON_VSEL_MASK, vsel);
244e24a04c4SMark Brown 	if (ret < 0)
245e24a04c4SMark Brown 		return ret;
246e24a04c4SMark Brown 	dcdc->on_vsel = vsel;
247e24a04c4SMark Brown 
2489a5ed0baSLinus Walleij 	if (!dcdc->dvs_gpiod)
249e24a04c4SMark Brown 		return ret;
250e24a04c4SMark Brown 
251e24a04c4SMark Brown 	/* Kick the voltage transition now */
252e24a04c4SMark Brown 	ret = wm831x_buckv_set_dvs(rdev, 0);
253e24a04c4SMark Brown 	if (ret < 0)
254e24a04c4SMark Brown 		return ret;
255e24a04c4SMark Brown 
25688cda60eSMark Brown 	/*
25788cda60eSMark Brown 	 * If this VSEL is higher than the last one we've seen then
25888cda60eSMark Brown 	 * remember it as the DVS VSEL.  This is optimised for CPUfreq
25988cda60eSMark Brown 	 * usage where we want to get to the highest voltage very
26088cda60eSMark Brown 	 * quickly.
26188cda60eSMark Brown 	 */
26288cda60eSMark Brown 	if (vsel > dcdc->dvs_vsel) {
26388cda60eSMark Brown 		ret = wm831x_set_bits(wm831x, dvs_reg,
26488cda60eSMark Brown 				      WM831X_DC1_DVS_VSEL_MASK,
26513ae633cSMark Brown 				      vsel);
266e24a04c4SMark Brown 		if (ret == 0)
267e24a04c4SMark Brown 			dcdc->dvs_vsel = vsel;
268e24a04c4SMark Brown 		else
26988cda60eSMark Brown 			dev_warn(wm831x->dev,
27088cda60eSMark Brown 				 "Failed to set DCDC DVS VSEL: %d\n", ret);
27188cda60eSMark Brown 	}
272e24a04c4SMark Brown 
273e24a04c4SMark Brown 	return 0;
274e4ee831fSMark Brown }
275e4ee831fSMark Brown 
wm831x_buckv_set_suspend_voltage(struct regulator_dev * rdev,int uV)276e4ee831fSMark Brown static int wm831x_buckv_set_suspend_voltage(struct regulator_dev *rdev,
277e4ee831fSMark Brown 					    int uV)
278e4ee831fSMark Brown {
279e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
280e24a04c4SMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
281e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
282e24a04c4SMark Brown 	int vsel;
283e4ee831fSMark Brown 
284ccffcb8eSAxel Lin 	vsel = regulator_map_voltage_linear_range(rdev, uV, uV);
285e24a04c4SMark Brown 	if (vsel < 0)
286e24a04c4SMark Brown 		return vsel;
287e24a04c4SMark Brown 
288e24a04c4SMark Brown 	return wm831x_set_bits(wm831x, reg, WM831X_DC1_SLP_VSEL_MASK, vsel);
289e4ee831fSMark Brown }
290e4ee831fSMark Brown 
wm831x_buckv_get_voltage_sel(struct regulator_dev * rdev)291afb8bb80SMark Brown static int wm831x_buckv_get_voltage_sel(struct regulator_dev *rdev)
292e4ee831fSMark Brown {
293e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
294e4ee831fSMark Brown 
2959a5ed0baSLinus Walleij 	if (dcdc->dvs_gpiod && dcdc->dvs_gpio_state)
296afb8bb80SMark Brown 		return dcdc->dvs_vsel;
297e24a04c4SMark Brown 	else
298afb8bb80SMark Brown 		return dcdc->on_vsel;
299e4ee831fSMark Brown }
300e4ee831fSMark Brown 
301e4ee831fSMark Brown /* Current limit options */
302c25d4788SAxel Lin static const unsigned int wm831x_dcdc_ilim[] = {
303c25d4788SAxel Lin 	125000, 250000, 375000, 500000, 625000, 750000, 875000, 1000000
304e4ee831fSMark Brown };
305e4ee831fSMark Brown 
306b0d6dd3bSJulia Lawall static const struct regulator_ops wm831x_buckv_ops = {
307b5fb77e0SAxel Lin 	.set_voltage_sel = wm831x_buckv_set_voltage_sel,
308afb8bb80SMark Brown 	.get_voltage_sel = wm831x_buckv_get_voltage_sel,
309ccffcb8eSAxel Lin 	.list_voltage = regulator_list_voltage_linear_range,
310ccffcb8eSAxel Lin 	.map_voltage = regulator_map_voltage_linear_range,
311e4ee831fSMark Brown 	.set_suspend_voltage = wm831x_buckv_set_suspend_voltage,
31220eb641eSAxel Lin 	.set_current_limit = regulator_set_current_limit_regmap,
31320eb641eSAxel Lin 	.get_current_limit = regulator_get_current_limit_regmap,
314e4ee831fSMark Brown 
3153d138fccSMark Brown 	.is_enabled = regulator_is_enabled_regmap,
3163d138fccSMark Brown 	.enable = regulator_enable_regmap,
3173d138fccSMark Brown 	.disable = regulator_disable_regmap,
318e4ee831fSMark Brown 	.get_status = wm831x_dcdc_get_status,
319e4ee831fSMark Brown 	.get_mode = wm831x_dcdc_get_mode,
320e4ee831fSMark Brown 	.set_mode = wm831x_dcdc_set_mode,
321e4ee831fSMark Brown 	.set_suspend_mode = wm831x_dcdc_set_suspend_mode,
322e4ee831fSMark Brown };
323e4ee831fSMark Brown 
324e24a04c4SMark Brown /*
325e24a04c4SMark Brown  * Set up DVS control.  We just log errors since we can still run
326e24a04c4SMark Brown  * (with reduced performance) if we fail.
327e24a04c4SMark Brown  */
wm831x_buckv_dvs_init(struct platform_device * pdev,struct wm831x_dcdc * dcdc,struct wm831x_buckv_pdata * pdata)3281aa20d27SMark Brown static void wm831x_buckv_dvs_init(struct platform_device *pdev,
3291aa20d27SMark Brown 				  struct wm831x_dcdc *dcdc,
330e24a04c4SMark Brown 				  struct wm831x_buckv_pdata *pdata)
331e24a04c4SMark Brown {
332e24a04c4SMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
333e24a04c4SMark Brown 	int ret;
334e24a04c4SMark Brown 	u16 ctrl;
335e24a04c4SMark Brown 
3369a5ed0baSLinus Walleij 	if (!pdata)
337e24a04c4SMark Brown 		return;
338e24a04c4SMark Brown 
339e24a04c4SMark Brown 	/* gpiolib won't let us read the GPIO status so pick the higher
340e24a04c4SMark Brown 	 * of the two existing voltages so we take it as platform data.
341e24a04c4SMark Brown 	 */
342e24a04c4SMark Brown 	dcdc->dvs_gpio_state = pdata->dvs_init_state;
343e24a04c4SMark Brown 
3449a5ed0baSLinus Walleij 	dcdc->dvs_gpiod = devm_gpiod_get(&pdev->dev, "dvs",
3459a5ed0baSLinus Walleij 			dcdc->dvs_gpio_state ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW);
3469a5ed0baSLinus Walleij 	if (IS_ERR(dcdc->dvs_gpiod)) {
3479a5ed0baSLinus Walleij 		dev_err(wm831x->dev, "Failed to get %s DVS GPIO: %ld\n",
3489a5ed0baSLinus Walleij 			dcdc->name, PTR_ERR(dcdc->dvs_gpiod));
349e24a04c4SMark Brown 		return;
350e24a04c4SMark Brown 	}
351e24a04c4SMark Brown 
352b47ba9fdSMark Brown 	switch (pdata->dvs_control_src) {
353b47ba9fdSMark Brown 	case 1:
354b47ba9fdSMark Brown 		ctrl = 2 << WM831X_DC1_DVS_SRC_SHIFT;
355b47ba9fdSMark Brown 		break;
356b47ba9fdSMark Brown 	case 2:
357b47ba9fdSMark Brown 		ctrl = 3 << WM831X_DC1_DVS_SRC_SHIFT;
358b47ba9fdSMark Brown 		break;
359b47ba9fdSMark Brown 	default:
360b47ba9fdSMark Brown 		dev_err(wm831x->dev, "Invalid DVS control source %d for %s\n",
361b47ba9fdSMark Brown 			pdata->dvs_control_src, dcdc->name);
362b47ba9fdSMark Brown 		return;
363b47ba9fdSMark Brown 	}
364b47ba9fdSMark Brown 
365c439b8f4SMark Brown 	/* If DVS_VSEL is set to the minimum value then raise it to ON_VSEL
366c439b8f4SMark Brown 	 * to make bootstrapping a bit smoother.
367c439b8f4SMark Brown 	 */
368c439b8f4SMark Brown 	if (!dcdc->dvs_vsel) {
369c439b8f4SMark Brown 		ret = wm831x_set_bits(wm831x,
370c439b8f4SMark Brown 				      dcdc->base + WM831X_DCDC_DVS_CONTROL,
371c439b8f4SMark Brown 				      WM831X_DC1_DVS_VSEL_MASK, dcdc->on_vsel);
372c439b8f4SMark Brown 		if (ret == 0)
373c439b8f4SMark Brown 			dcdc->dvs_vsel = dcdc->on_vsel;
374c439b8f4SMark Brown 		else
375c439b8f4SMark Brown 			dev_warn(wm831x->dev, "Failed to set DVS_VSEL: %d\n",
376c439b8f4SMark Brown 				 ret);
377c439b8f4SMark Brown 	}
378c439b8f4SMark Brown 
379b47ba9fdSMark Brown 	ret = wm831x_set_bits(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL,
380b47ba9fdSMark Brown 			      WM831X_DC1_DVS_SRC_MASK, ctrl);
381b47ba9fdSMark Brown 	if (ret < 0) {
382b47ba9fdSMark Brown 		dev_err(wm831x->dev, "Failed to set %s DVS source: %d\n",
383b47ba9fdSMark Brown 			dcdc->name, ret);
384b47ba9fdSMark Brown 	}
385e24a04c4SMark Brown }
386e24a04c4SMark Brown 
wm831x_buckv_probe(struct platform_device * pdev)387a5023574SBill Pemberton static int wm831x_buckv_probe(struct platform_device *pdev)
388e4ee831fSMark Brown {
389e4ee831fSMark Brown 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
390dff91d0bSJingoo Han 	struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
391c172708dSMark Brown 	struct regulator_config config = { };
392137a6354SMark Brown 	int id;
393e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc;
394e4ee831fSMark Brown 	struct resource *res;
395e4ee831fSMark Brown 	int ret, irq;
396e4ee831fSMark Brown 
397137a6354SMark Brown 	if (pdata && pdata->wm831x_num)
398137a6354SMark Brown 		id = (pdata->wm831x_num * 10) + 1;
399137a6354SMark Brown 	else
400137a6354SMark Brown 		id = 0;
401137a6354SMark Brown 	id = pdev->id - id;
402137a6354SMark Brown 
403e4ee831fSMark Brown 	dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
404e4ee831fSMark Brown 
405fded2f4fSMark Brown 	dcdc = devm_kzalloc(&pdev->dev,  sizeof(struct wm831x_dcdc),
406fded2f4fSMark Brown 			    GFP_KERNEL);
4075730aa57SSachin Kamat 	if (!dcdc)
408e4ee831fSMark Brown 		return -ENOMEM;
409e4ee831fSMark Brown 
410e4ee831fSMark Brown 	dcdc->wm831x = wm831x;
411e4ee831fSMark Brown 
4125656098eSMark Brown 	res = platform_get_resource(pdev, IORESOURCE_REG, 0);
413e4ee831fSMark Brown 	if (res == NULL) {
4145656098eSMark Brown 		dev_err(&pdev->dev, "No REG resource\n");
415e4ee831fSMark Brown 		ret = -EINVAL;
416e4ee831fSMark Brown 		goto err;
417e4ee831fSMark Brown 	}
418e4ee831fSMark Brown 	dcdc->base = res->start;
419e4ee831fSMark Brown 
420e4ee831fSMark Brown 	snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
421e4ee831fSMark Brown 	dcdc->desc.name = dcdc->name;
42282caa978SMark Brown 
42382caa978SMark Brown 	snprintf(dcdc->supply_name, sizeof(dcdc->supply_name),
42482caa978SMark Brown 		 "DC%dVDD", id + 1);
42582caa978SMark Brown 	dcdc->desc.supply_name = dcdc->supply_name;
42682caa978SMark Brown 
427e4ee831fSMark Brown 	dcdc->desc.id = id;
428e4ee831fSMark Brown 	dcdc->desc.type = REGULATOR_VOLTAGE;
429e4ee831fSMark Brown 	dcdc->desc.n_voltages = WM831X_BUCKV_MAX_SELECTOR + 1;
430ccffcb8eSAxel Lin 	dcdc->desc.linear_ranges = wm831x_buckv_ranges;
431ccffcb8eSAxel Lin 	dcdc->desc.n_linear_ranges = ARRAY_SIZE(wm831x_buckv_ranges);
432e4ee831fSMark Brown 	dcdc->desc.ops = &wm831x_buckv_ops;
433e4ee831fSMark Brown 	dcdc->desc.owner = THIS_MODULE;
4343d138fccSMark Brown 	dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
4353d138fccSMark Brown 	dcdc->desc.enable_mask = 1 << id;
43620eb641eSAxel Lin 	dcdc->desc.csel_reg = dcdc->base + WM831X_DCDC_CONTROL_2;
43720eb641eSAxel Lin 	dcdc->desc.csel_mask = WM831X_DC1_HC_THR_MASK;
43820eb641eSAxel Lin 	dcdc->desc.n_current_limits = ARRAY_SIZE(wm831x_dcdc_ilim);
43920eb641eSAxel Lin 	dcdc->desc.curr_table = wm831x_dcdc_ilim;
440e4ee831fSMark Brown 
441e24a04c4SMark Brown 	ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG);
442e24a04c4SMark Brown 	if (ret < 0) {
443e24a04c4SMark Brown 		dev_err(wm831x->dev, "Failed to read ON VSEL: %d\n", ret);
444e24a04c4SMark Brown 		goto err;
445e24a04c4SMark Brown 	}
446e24a04c4SMark Brown 	dcdc->on_vsel = ret & WM831X_DC1_ON_VSEL_MASK;
447e24a04c4SMark Brown 
448a1b81dd3SMark Brown 	ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL);
449e24a04c4SMark Brown 	if (ret < 0) {
450e24a04c4SMark Brown 		dev_err(wm831x->dev, "Failed to read DVS VSEL: %d\n", ret);
451e24a04c4SMark Brown 		goto err;
452e24a04c4SMark Brown 	}
453e24a04c4SMark Brown 	dcdc->dvs_vsel = ret & WM831X_DC1_DVS_VSEL_MASK;
454e24a04c4SMark Brown 
455f0b067d9SMark Brown 	if (pdata && pdata->dcdc[id])
4561aa20d27SMark Brown 		wm831x_buckv_dvs_init(pdev, dcdc,
4571aa20d27SMark Brown 				      pdata->dcdc[id]->driver_data);
458e24a04c4SMark Brown 
459c172708dSMark Brown 	config.dev = pdev->dev.parent;
460b7ca8788SMark Brown 	if (pdata)
461c172708dSMark Brown 		config.init_data = pdata->dcdc[id];
462c172708dSMark Brown 	config.driver_data = dcdc;
4633d138fccSMark Brown 	config.regmap = wm831x->regmap;
464c172708dSMark Brown 
465d73b4cb7SMark Brown 	dcdc->regulator = devm_regulator_register(&pdev->dev, &dcdc->desc,
466d73b4cb7SMark Brown 						  &config);
467e4ee831fSMark Brown 	if (IS_ERR(dcdc->regulator)) {
468e4ee831fSMark Brown 		ret = PTR_ERR(dcdc->regulator);
469e4ee831fSMark Brown 		dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
470e4ee831fSMark Brown 			id + 1, ret);
471e4ee831fSMark Brown 		goto err;
472e4ee831fSMark Brown 	}
473e4ee831fSMark Brown 
474cd99758bSMark Brown 	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
475b0c4c0c6SMark Brown 	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
476b0c4c0c6SMark Brown 					wm831x_dcdc_uv_irq,
47729454738SFabio Estevam 					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
47829454738SFabio Estevam 					dcdc->name, dcdc);
479e4ee831fSMark Brown 	if (ret != 0) {
480e4ee831fSMark Brown 		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
481e4ee831fSMark Brown 			irq, ret);
482d73b4cb7SMark Brown 		goto err;
483e4ee831fSMark Brown 	}
484e4ee831fSMark Brown 
485cd99758bSMark Brown 	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "HC"));
486b0c4c0c6SMark Brown 	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
487b0c4c0c6SMark Brown 					wm831x_dcdc_oc_irq,
48829454738SFabio Estevam 					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
48929454738SFabio Estevam 					dcdc->name, dcdc);
490e4ee831fSMark Brown 	if (ret != 0) {
491e4ee831fSMark Brown 		dev_err(&pdev->dev, "Failed to request HC IRQ %d: %d\n",
492e4ee831fSMark Brown 			irq, ret);
493d73b4cb7SMark Brown 		goto err;
494e4ee831fSMark Brown 	}
495e4ee831fSMark Brown 
496e4ee831fSMark Brown 	platform_set_drvdata(pdev, dcdc);
497e4ee831fSMark Brown 
498e4ee831fSMark Brown 	return 0;
499e4ee831fSMark Brown 
500e4ee831fSMark Brown err:
501e4ee831fSMark Brown 	return ret;
502e4ee831fSMark Brown }
503e4ee831fSMark Brown 
504e4ee831fSMark Brown static struct platform_driver wm831x_buckv_driver = {
505e4ee831fSMark Brown 	.probe = wm831x_buckv_probe,
506e4ee831fSMark Brown 	.driver		= {
507e4ee831fSMark Brown 		.name	= "wm831x-buckv",
508*259b93b2SDouglas Anderson 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
509e4ee831fSMark Brown 	},
510e4ee831fSMark Brown };
511e4ee831fSMark Brown 
512e4ee831fSMark Brown /*
513e4ee831fSMark Brown  * BUCKP specifics
514e4ee831fSMark Brown  */
515e4ee831fSMark Brown 
wm831x_buckp_set_suspend_voltage(struct regulator_dev * rdev,int uV)516d580cb5eSAxel Lin static int wm831x_buckp_set_suspend_voltage(struct regulator_dev *rdev, int uV)
517e4ee831fSMark Brown {
518e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
519e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
520e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
521d580cb5eSAxel Lin 	int sel;
522e4ee831fSMark Brown 
523d580cb5eSAxel Lin 	sel = regulator_map_voltage_linear(rdev, uV, uV);
524d580cb5eSAxel Lin 	if (sel < 0)
525d580cb5eSAxel Lin 		return sel;
526d580cb5eSAxel Lin 
527d580cb5eSAxel Lin 	return wm831x_set_bits(wm831x, reg, WM831X_DC3_ON_VSEL_MASK, sel);
528e4ee831fSMark Brown }
529e4ee831fSMark Brown 
530b0d6dd3bSJulia Lawall static const struct regulator_ops wm831x_buckp_ops = {
531d580cb5eSAxel Lin 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
532817436e7SMark Brown 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
533c70ad9dcSAxel Lin 	.list_voltage = regulator_list_voltage_linear,
534d580cb5eSAxel Lin 	.map_voltage = regulator_map_voltage_linear,
535e4ee831fSMark Brown 	.set_suspend_voltage = wm831x_buckp_set_suspend_voltage,
536e4ee831fSMark Brown 
5373d138fccSMark Brown 	.is_enabled = regulator_is_enabled_regmap,
5383d138fccSMark Brown 	.enable = regulator_enable_regmap,
5393d138fccSMark Brown 	.disable = regulator_disable_regmap,
540e4ee831fSMark Brown 	.get_status = wm831x_dcdc_get_status,
541e4ee831fSMark Brown 	.get_mode = wm831x_dcdc_get_mode,
542e4ee831fSMark Brown 	.set_mode = wm831x_dcdc_set_mode,
543e4ee831fSMark Brown 	.set_suspend_mode = wm831x_dcdc_set_suspend_mode,
544e4ee831fSMark Brown };
545e4ee831fSMark Brown 
wm831x_buckp_probe(struct platform_device * pdev)546a5023574SBill Pemberton static int wm831x_buckp_probe(struct platform_device *pdev)
547e4ee831fSMark Brown {
548e4ee831fSMark Brown 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
549dff91d0bSJingoo Han 	struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
550c172708dSMark Brown 	struct regulator_config config = { };
551137a6354SMark Brown 	int id;
552e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc;
553e4ee831fSMark Brown 	struct resource *res;
554e4ee831fSMark Brown 	int ret, irq;
555e4ee831fSMark Brown 
556137a6354SMark Brown 	if (pdata && pdata->wm831x_num)
557137a6354SMark Brown 		id = (pdata->wm831x_num * 10) + 1;
558137a6354SMark Brown 	else
559137a6354SMark Brown 		id = 0;
560137a6354SMark Brown 	id = pdev->id - id;
561137a6354SMark Brown 
562e4ee831fSMark Brown 	dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
563e4ee831fSMark Brown 
564fded2f4fSMark Brown 	dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc),
565fded2f4fSMark Brown 			    GFP_KERNEL);
5665730aa57SSachin Kamat 	if (!dcdc)
567e4ee831fSMark Brown 		return -ENOMEM;
568e4ee831fSMark Brown 
569e4ee831fSMark Brown 	dcdc->wm831x = wm831x;
570e4ee831fSMark Brown 
5715656098eSMark Brown 	res = platform_get_resource(pdev, IORESOURCE_REG, 0);
572e4ee831fSMark Brown 	if (res == NULL) {
5735656098eSMark Brown 		dev_err(&pdev->dev, "No REG resource\n");
574e4ee831fSMark Brown 		ret = -EINVAL;
575e4ee831fSMark Brown 		goto err;
576e4ee831fSMark Brown 	}
577e4ee831fSMark Brown 	dcdc->base = res->start;
578e4ee831fSMark Brown 
579e4ee831fSMark Brown 	snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
580e4ee831fSMark Brown 	dcdc->desc.name = dcdc->name;
58182caa978SMark Brown 
58282caa978SMark Brown 	snprintf(dcdc->supply_name, sizeof(dcdc->supply_name),
58382caa978SMark Brown 		 "DC%dVDD", id + 1);
58482caa978SMark Brown 	dcdc->desc.supply_name = dcdc->supply_name;
58582caa978SMark Brown 
586e4ee831fSMark Brown 	dcdc->desc.id = id;
587e4ee831fSMark Brown 	dcdc->desc.type = REGULATOR_VOLTAGE;
588e4ee831fSMark Brown 	dcdc->desc.n_voltages = WM831X_BUCKP_MAX_SELECTOR + 1;
589e4ee831fSMark Brown 	dcdc->desc.ops = &wm831x_buckp_ops;
590e4ee831fSMark Brown 	dcdc->desc.owner = THIS_MODULE;
591817436e7SMark Brown 	dcdc->desc.vsel_reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
592817436e7SMark Brown 	dcdc->desc.vsel_mask = WM831X_DC3_ON_VSEL_MASK;
5933d138fccSMark Brown 	dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
5943d138fccSMark Brown 	dcdc->desc.enable_mask = 1 << id;
595c70ad9dcSAxel Lin 	dcdc->desc.min_uV = 850000;
596c70ad9dcSAxel Lin 	dcdc->desc.uV_step = 25000;
597e4ee831fSMark Brown 
598c172708dSMark Brown 	config.dev = pdev->dev.parent;
599b7ca8788SMark Brown 	if (pdata)
600c172708dSMark Brown 		config.init_data = pdata->dcdc[id];
601c172708dSMark Brown 	config.driver_data = dcdc;
602817436e7SMark Brown 	config.regmap = wm831x->regmap;
603c172708dSMark Brown 
604d73b4cb7SMark Brown 	dcdc->regulator = devm_regulator_register(&pdev->dev, &dcdc->desc,
605d73b4cb7SMark Brown 						  &config);
606e4ee831fSMark Brown 	if (IS_ERR(dcdc->regulator)) {
607e4ee831fSMark Brown 		ret = PTR_ERR(dcdc->regulator);
608e4ee831fSMark Brown 		dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
609e4ee831fSMark Brown 			id + 1, ret);
610e4ee831fSMark Brown 		goto err;
611e4ee831fSMark Brown 	}
612e4ee831fSMark Brown 
613cd99758bSMark Brown 	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
614b0c4c0c6SMark Brown 	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
615b0c4c0c6SMark Brown 					wm831x_dcdc_uv_irq,
61629454738SFabio Estevam 					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
61729454738SFabio Estevam 					dcdc->name, dcdc);
618e4ee831fSMark Brown 	if (ret != 0) {
619e4ee831fSMark Brown 		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
620e4ee831fSMark Brown 			irq, ret);
621d73b4cb7SMark Brown 		goto err;
622e4ee831fSMark Brown 	}
623e4ee831fSMark Brown 
624e4ee831fSMark Brown 	platform_set_drvdata(pdev, dcdc);
625e4ee831fSMark Brown 
626e4ee831fSMark Brown 	return 0;
627e4ee831fSMark Brown 
628e4ee831fSMark Brown err:
629e4ee831fSMark Brown 	return ret;
630e4ee831fSMark Brown }
631e4ee831fSMark Brown 
632e4ee831fSMark Brown static struct platform_driver wm831x_buckp_driver = {
633e4ee831fSMark Brown 	.probe = wm831x_buckp_probe,
634e4ee831fSMark Brown 	.driver		= {
635e4ee831fSMark Brown 		.name	= "wm831x-buckp",
636*259b93b2SDouglas Anderson 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
637e4ee831fSMark Brown 	},
638e4ee831fSMark Brown };
639e4ee831fSMark Brown 
6408267a9baSMark Brown /*
6411304850dSMark Brown  * DCDC boost convertors
6421304850dSMark Brown  */
6431304850dSMark Brown 
wm831x_boostp_get_status(struct regulator_dev * rdev)6441304850dSMark Brown static int wm831x_boostp_get_status(struct regulator_dev *rdev)
6451304850dSMark Brown {
6461304850dSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
6471304850dSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
6481304850dSMark Brown 	int ret;
6491304850dSMark Brown 
6501304850dSMark Brown 	/* First, check for errors */
6511304850dSMark Brown 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
6521304850dSMark Brown 	if (ret < 0)
6531304850dSMark Brown 		return ret;
6541304850dSMark Brown 
6551304850dSMark Brown 	if (ret & (1 << rdev_get_id(rdev))) {
6561304850dSMark Brown 		dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
6571304850dSMark Brown 			rdev_get_id(rdev) + 1);
6581304850dSMark Brown 		return REGULATOR_STATUS_ERROR;
6591304850dSMark Brown 	}
6601304850dSMark Brown 
6611304850dSMark Brown 	/* Is the regulator on? */
6621304850dSMark Brown 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
6631304850dSMark Brown 	if (ret < 0)
6641304850dSMark Brown 		return ret;
6651304850dSMark Brown 	if (ret & (1 << rdev_get_id(rdev)))
6661304850dSMark Brown 		return REGULATOR_STATUS_ON;
6671304850dSMark Brown 	else
6681304850dSMark Brown 		return REGULATOR_STATUS_OFF;
6691304850dSMark Brown }
6701304850dSMark Brown 
671b0d6dd3bSJulia Lawall static const struct regulator_ops wm831x_boostp_ops = {
6721304850dSMark Brown 	.get_status = wm831x_boostp_get_status,
6731304850dSMark Brown 
6743d138fccSMark Brown 	.is_enabled = regulator_is_enabled_regmap,
6753d138fccSMark Brown 	.enable = regulator_enable_regmap,
6763d138fccSMark Brown 	.disable = regulator_disable_regmap,
6771304850dSMark Brown };
6781304850dSMark Brown 
wm831x_boostp_probe(struct platform_device * pdev)679a5023574SBill Pemberton static int wm831x_boostp_probe(struct platform_device *pdev)
6801304850dSMark Brown {
6811304850dSMark Brown 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
682dff91d0bSJingoo Han 	struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
683c172708dSMark Brown 	struct regulator_config config = { };
6841304850dSMark Brown 	int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
6851304850dSMark Brown 	struct wm831x_dcdc *dcdc;
6861304850dSMark Brown 	struct resource *res;
6871304850dSMark Brown 	int ret, irq;
6881304850dSMark Brown 
6891304850dSMark Brown 	dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
6901304850dSMark Brown 
6911304850dSMark Brown 	if (pdata == NULL || pdata->dcdc[id] == NULL)
6921304850dSMark Brown 		return -ENODEV;
6931304850dSMark Brown 
6944c60165dSAxel Lin 	dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), GFP_KERNEL);
6955730aa57SSachin Kamat 	if (!dcdc)
6961304850dSMark Brown 		return -ENOMEM;
6971304850dSMark Brown 
6981304850dSMark Brown 	dcdc->wm831x = wm831x;
6991304850dSMark Brown 
7005656098eSMark Brown 	res = platform_get_resource(pdev, IORESOURCE_REG, 0);
7011304850dSMark Brown 	if (res == NULL) {
7025656098eSMark Brown 		dev_err(&pdev->dev, "No REG resource\n");
703477b2bacSFabio Estevam 		return -EINVAL;
7041304850dSMark Brown 	}
7051304850dSMark Brown 	dcdc->base = res->start;
7061304850dSMark Brown 
7071304850dSMark Brown 	snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
7081304850dSMark Brown 	dcdc->desc.name = dcdc->name;
7091304850dSMark Brown 	dcdc->desc.id = id;
7101304850dSMark Brown 	dcdc->desc.type = REGULATOR_VOLTAGE;
7111304850dSMark Brown 	dcdc->desc.ops = &wm831x_boostp_ops;
7121304850dSMark Brown 	dcdc->desc.owner = THIS_MODULE;
7133d138fccSMark Brown 	dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
7143d138fccSMark Brown 	dcdc->desc.enable_mask = 1 << id;
7151304850dSMark Brown 
716c172708dSMark Brown 	config.dev = pdev->dev.parent;
717f0b067d9SMark Brown 	if (pdata)
718c172708dSMark Brown 		config.init_data = pdata->dcdc[id];
719c172708dSMark Brown 	config.driver_data = dcdc;
7203d138fccSMark Brown 	config.regmap = wm831x->regmap;
721c172708dSMark Brown 
722d73b4cb7SMark Brown 	dcdc->regulator = devm_regulator_register(&pdev->dev, &dcdc->desc,
723d73b4cb7SMark Brown 						  &config);
7241304850dSMark Brown 	if (IS_ERR(dcdc->regulator)) {
7251304850dSMark Brown 		ret = PTR_ERR(dcdc->regulator);
7261304850dSMark Brown 		dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
7271304850dSMark Brown 			id + 1, ret);
728477b2bacSFabio Estevam 		return ret;
7291304850dSMark Brown 	}
7301304850dSMark Brown 
731cd99758bSMark Brown 	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
732b0c4c0c6SMark Brown 	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
733b0c4c0c6SMark Brown 					wm831x_dcdc_uv_irq,
73429454738SFabio Estevam 					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
73529454738SFabio Estevam 					dcdc->name,
7361304850dSMark Brown 					dcdc);
7371304850dSMark Brown 	if (ret != 0) {
7381304850dSMark Brown 		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
7391304850dSMark Brown 			irq, ret);
740477b2bacSFabio Estevam 		return ret;
7411304850dSMark Brown 	}
7421304850dSMark Brown 
7431304850dSMark Brown 	platform_set_drvdata(pdev, dcdc);
7441304850dSMark Brown 
7451304850dSMark Brown 	return 0;
7461304850dSMark Brown }
7471304850dSMark Brown 
7481304850dSMark Brown static struct platform_driver wm831x_boostp_driver = {
7491304850dSMark Brown 	.probe = wm831x_boostp_probe,
7501304850dSMark Brown 	.driver		= {
7511304850dSMark Brown 		.name	= "wm831x-boostp",
752*259b93b2SDouglas Anderson 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
7531304850dSMark Brown 	},
7541304850dSMark Brown };
7551304850dSMark Brown 
7561304850dSMark Brown /*
7578267a9baSMark Brown  * External Power Enable
7588267a9baSMark Brown  *
7598267a9baSMark Brown  * These aren't actually DCDCs but look like them in hardware so share
7608267a9baSMark Brown  * code.
7618267a9baSMark Brown  */
7628267a9baSMark Brown 
7638267a9baSMark Brown #define WM831X_EPE_BASE 6
7648267a9baSMark Brown 
765b0d6dd3bSJulia Lawall static const struct regulator_ops wm831x_epe_ops = {
7663d138fccSMark Brown 	.is_enabled = regulator_is_enabled_regmap,
7673d138fccSMark Brown 	.enable = regulator_enable_regmap,
7683d138fccSMark Brown 	.disable = regulator_disable_regmap,
7698267a9baSMark Brown 	.get_status = wm831x_dcdc_get_status,
7708267a9baSMark Brown };
7718267a9baSMark Brown 
wm831x_epe_probe(struct platform_device * pdev)772a5023574SBill Pemberton static int wm831x_epe_probe(struct platform_device *pdev)
7738267a9baSMark Brown {
7748267a9baSMark Brown 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
775dff91d0bSJingoo Han 	struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
776c172708dSMark Brown 	struct regulator_config config = { };
7778267a9baSMark Brown 	int id = pdev->id % ARRAY_SIZE(pdata->epe);
7788267a9baSMark Brown 	struct wm831x_dcdc *dcdc;
7798267a9baSMark Brown 	int ret;
7808267a9baSMark Brown 
7818267a9baSMark Brown 	dev_dbg(&pdev->dev, "Probing EPE%d\n", id + 1);
7828267a9baSMark Brown 
7834c60165dSAxel Lin 	dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), GFP_KERNEL);
7845730aa57SSachin Kamat 	if (!dcdc)
7858267a9baSMark Brown 		return -ENOMEM;
7868267a9baSMark Brown 
7878267a9baSMark Brown 	dcdc->wm831x = wm831x;
7888267a9baSMark Brown 
7898267a9baSMark Brown 	/* For current parts this is correct; probably need to revisit
7908267a9baSMark Brown 	 * in future.
7918267a9baSMark Brown 	 */
7928267a9baSMark Brown 	snprintf(dcdc->name, sizeof(dcdc->name), "EPE%d", id + 1);
7938267a9baSMark Brown 	dcdc->desc.name = dcdc->name;
7948267a9baSMark Brown 	dcdc->desc.id = id + WM831X_EPE_BASE; /* Offset in DCDC registers */
7958267a9baSMark Brown 	dcdc->desc.ops = &wm831x_epe_ops;
7968267a9baSMark Brown 	dcdc->desc.type = REGULATOR_VOLTAGE;
7978267a9baSMark Brown 	dcdc->desc.owner = THIS_MODULE;
7983d138fccSMark Brown 	dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
7993d138fccSMark Brown 	dcdc->desc.enable_mask = 1 << dcdc->desc.id;
8008267a9baSMark Brown 
801c172708dSMark Brown 	config.dev = pdev->dev.parent;
802b7ca8788SMark Brown 	if (pdata)
803c172708dSMark Brown 		config.init_data = pdata->epe[id];
804c172708dSMark Brown 	config.driver_data = dcdc;
8053d138fccSMark Brown 	config.regmap = wm831x->regmap;
806c172708dSMark Brown 
807d73b4cb7SMark Brown 	dcdc->regulator = devm_regulator_register(&pdev->dev, &dcdc->desc,
808d73b4cb7SMark Brown 						  &config);
8098267a9baSMark Brown 	if (IS_ERR(dcdc->regulator)) {
8108267a9baSMark Brown 		ret = PTR_ERR(dcdc->regulator);
8118267a9baSMark Brown 		dev_err(wm831x->dev, "Failed to register EPE%d: %d\n",
8128267a9baSMark Brown 			id + 1, ret);
8138267a9baSMark Brown 		goto err;
8148267a9baSMark Brown 	}
8158267a9baSMark Brown 
8168267a9baSMark Brown 	platform_set_drvdata(pdev, dcdc);
8178267a9baSMark Brown 
8188267a9baSMark Brown 	return 0;
8198267a9baSMark Brown 
8208267a9baSMark Brown err:
8218267a9baSMark Brown 	return ret;
8228267a9baSMark Brown }
8238267a9baSMark Brown 
8248267a9baSMark Brown static struct platform_driver wm831x_epe_driver = {
8258267a9baSMark Brown 	.probe = wm831x_epe_probe,
8268267a9baSMark Brown 	.driver		= {
8278267a9baSMark Brown 		.name	= "wm831x-epe",
828*259b93b2SDouglas Anderson 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
8298267a9baSMark Brown 	},
8308267a9baSMark Brown };
8318267a9baSMark Brown 
83255e03e9cSThierry Reding static struct platform_driver * const drivers[] = {
83355e03e9cSThierry Reding 	&wm831x_buckv_driver,
83455e03e9cSThierry Reding 	&wm831x_buckp_driver,
83555e03e9cSThierry Reding 	&wm831x_boostp_driver,
83655e03e9cSThierry Reding 	&wm831x_epe_driver,
83755e03e9cSThierry Reding };
83855e03e9cSThierry Reding 
wm831x_dcdc_init(void)839e4ee831fSMark Brown static int __init wm831x_dcdc_init(void)
840e4ee831fSMark Brown {
84155e03e9cSThierry Reding 	return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
842e4ee831fSMark Brown }
843e4ee831fSMark Brown subsys_initcall(wm831x_dcdc_init);
844e4ee831fSMark Brown 
wm831x_dcdc_exit(void)845e4ee831fSMark Brown static void __exit wm831x_dcdc_exit(void)
846e4ee831fSMark Brown {
84755e03e9cSThierry Reding 	platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
848e4ee831fSMark Brown }
849e4ee831fSMark Brown module_exit(wm831x_dcdc_exit);
850e4ee831fSMark Brown 
851e4ee831fSMark Brown /* Module information */
852e4ee831fSMark Brown MODULE_AUTHOR("Mark Brown");
853e4ee831fSMark Brown MODULE_DESCRIPTION("WM831x DC-DC convertor driver");
854e4ee831fSMark Brown MODULE_LICENSE("GPL");
855e4ee831fSMark Brown MODULE_ALIAS("platform:wm831x-buckv");
856e4ee831fSMark Brown MODULE_ALIAS("platform:wm831x-buckp");
85743f1f216SAxel Lin MODULE_ALIAS("platform:wm831x-boostp");
85824b43150SMark Brown MODULE_ALIAS("platform:wm831x-epe");
859