1e4ee831fSMark Brown /*
2e4ee831fSMark Brown  * wm831x-dcdc.c  --  DC-DC buck convertor driver for the WM831x series
3e4ee831fSMark Brown  *
4e4ee831fSMark Brown  * Copyright 2009 Wolfson Microelectronics PLC.
5e4ee831fSMark Brown  *
6e4ee831fSMark Brown  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7e4ee831fSMark Brown  *
8e4ee831fSMark Brown  *  This program is free software; you can redistribute  it and/or modify it
9e4ee831fSMark Brown  *  under  the terms of  the GNU General  Public License as published by the
10e4ee831fSMark Brown  *  Free Software Foundation;  either version 2 of the  License, or (at your
11e4ee831fSMark Brown  *  option) any later version.
12e4ee831fSMark Brown  */
13e4ee831fSMark Brown 
14e4ee831fSMark Brown #include <linux/module.h>
15e4ee831fSMark Brown #include <linux/moduleparam.h>
16e4ee831fSMark Brown #include <linux/init.h>
17e4ee831fSMark Brown #include <linux/bitops.h>
18e4ee831fSMark Brown #include <linux/err.h>
19e4ee831fSMark Brown #include <linux/i2c.h>
20e4ee831fSMark Brown #include <linux/platform_device.h>
21e4ee831fSMark Brown #include <linux/regulator/driver.h>
22e24a04c4SMark Brown #include <linux/regulator/machine.h>
23e24a04c4SMark Brown #include <linux/gpio.h>
245a0e3ad6STejun Heo #include <linux/slab.h>
25e4ee831fSMark Brown 
26e4ee831fSMark Brown #include <linux/mfd/wm831x/core.h>
27e4ee831fSMark Brown #include <linux/mfd/wm831x/regulator.h>
28e4ee831fSMark Brown #include <linux/mfd/wm831x/pdata.h>
29e4ee831fSMark Brown 
30e4ee831fSMark Brown #define WM831X_BUCKV_MAX_SELECTOR 0x68
31e4ee831fSMark Brown #define WM831X_BUCKP_MAX_SELECTOR 0x66
32e4ee831fSMark Brown 
33e4ee831fSMark Brown #define WM831X_DCDC_MODE_FAST    0
34e4ee831fSMark Brown #define WM831X_DCDC_MODE_NORMAL  1
35e4ee831fSMark Brown #define WM831X_DCDC_MODE_IDLE    2
36e4ee831fSMark Brown #define WM831X_DCDC_MODE_STANDBY 3
37e4ee831fSMark Brown 
3882caa978SMark Brown #define WM831X_DCDC_MAX_NAME 9
39e4ee831fSMark Brown 
40e4ee831fSMark Brown /* Register offsets in control block */
41e4ee831fSMark Brown #define WM831X_DCDC_CONTROL_1     0
42e4ee831fSMark Brown #define WM831X_DCDC_CONTROL_2     1
43e4ee831fSMark Brown #define WM831X_DCDC_ON_CONFIG     2
44e4ee831fSMark Brown #define WM831X_DCDC_SLEEP_CONTROL 3
45e24a04c4SMark Brown #define WM831X_DCDC_DVS_CONTROL   4
46e4ee831fSMark Brown 
47e4ee831fSMark Brown /*
48e4ee831fSMark Brown  * Shared
49e4ee831fSMark Brown  */
50e4ee831fSMark Brown 
51e4ee831fSMark Brown struct wm831x_dcdc {
52e4ee831fSMark Brown 	char name[WM831X_DCDC_MAX_NAME];
5382caa978SMark Brown 	char supply_name[WM831X_DCDC_MAX_NAME];
54e4ee831fSMark Brown 	struct regulator_desc desc;
55e4ee831fSMark Brown 	int base;
56e4ee831fSMark Brown 	struct wm831x *wm831x;
57e4ee831fSMark Brown 	struct regulator_dev *regulator;
58e24a04c4SMark Brown 	int dvs_gpio;
59e24a04c4SMark Brown 	int dvs_gpio_state;
60e24a04c4SMark Brown 	int on_vsel;
61e24a04c4SMark Brown 	int dvs_vsel;
62e4ee831fSMark Brown };
63e4ee831fSMark Brown 
64e4ee831fSMark Brown static unsigned int wm831x_dcdc_get_mode(struct regulator_dev *rdev)
65e4ee831fSMark Brown 
66e4ee831fSMark Brown {
67e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
68e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
69e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
70e4ee831fSMark Brown 	int val;
71e4ee831fSMark Brown 
72e4ee831fSMark Brown 	val = wm831x_reg_read(wm831x, reg);
73e4ee831fSMark Brown 	if (val < 0)
74e4ee831fSMark Brown 		return val;
75e4ee831fSMark Brown 
76e4ee831fSMark Brown 	val = (val & WM831X_DC1_ON_MODE_MASK) >> WM831X_DC1_ON_MODE_SHIFT;
77e4ee831fSMark Brown 
78e4ee831fSMark Brown 	switch (val) {
79e4ee831fSMark Brown 	case WM831X_DCDC_MODE_FAST:
80e4ee831fSMark Brown 		return REGULATOR_MODE_FAST;
81e4ee831fSMark Brown 	case WM831X_DCDC_MODE_NORMAL:
82e4ee831fSMark Brown 		return REGULATOR_MODE_NORMAL;
83e4ee831fSMark Brown 	case WM831X_DCDC_MODE_STANDBY:
84e4ee831fSMark Brown 		return REGULATOR_MODE_STANDBY;
85e4ee831fSMark Brown 	case WM831X_DCDC_MODE_IDLE:
86e4ee831fSMark Brown 		return REGULATOR_MODE_IDLE;
87e4ee831fSMark Brown 	default:
88e4ee831fSMark Brown 		BUG();
899ee291a4SMark Brown 		return -EINVAL;
90e4ee831fSMark Brown 	}
91e4ee831fSMark Brown }
92e4ee831fSMark Brown 
93e4ee831fSMark Brown static int wm831x_dcdc_set_mode_int(struct wm831x *wm831x, int reg,
94e4ee831fSMark Brown 				    unsigned int mode)
95e4ee831fSMark Brown {
96e4ee831fSMark Brown 	int val;
97e4ee831fSMark Brown 
98e4ee831fSMark Brown 	switch (mode) {
99e4ee831fSMark Brown 	case REGULATOR_MODE_FAST:
100e4ee831fSMark Brown 		val = WM831X_DCDC_MODE_FAST;
101e4ee831fSMark Brown 		break;
102e4ee831fSMark Brown 	case REGULATOR_MODE_NORMAL:
103e4ee831fSMark Brown 		val = WM831X_DCDC_MODE_NORMAL;
104e4ee831fSMark Brown 		break;
105e4ee831fSMark Brown 	case REGULATOR_MODE_STANDBY:
106e4ee831fSMark Brown 		val = WM831X_DCDC_MODE_STANDBY;
107e4ee831fSMark Brown 		break;
108e4ee831fSMark Brown 	case REGULATOR_MODE_IDLE:
109e4ee831fSMark Brown 		val = WM831X_DCDC_MODE_IDLE;
110e4ee831fSMark Brown 		break;
111e4ee831fSMark Brown 	default:
112e4ee831fSMark Brown 		return -EINVAL;
113e4ee831fSMark Brown 	}
114e4ee831fSMark Brown 
115e4ee831fSMark Brown 	return wm831x_set_bits(wm831x, reg, WM831X_DC1_ON_MODE_MASK,
116e4ee831fSMark Brown 			       val << WM831X_DC1_ON_MODE_SHIFT);
117e4ee831fSMark Brown }
118e4ee831fSMark Brown 
119e4ee831fSMark Brown static int wm831x_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
120e4ee831fSMark Brown {
121e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
122e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
123e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
124e4ee831fSMark Brown 
125e4ee831fSMark Brown 	return wm831x_dcdc_set_mode_int(wm831x, reg, mode);
126e4ee831fSMark Brown }
127e4ee831fSMark Brown 
128e4ee831fSMark Brown static int wm831x_dcdc_set_suspend_mode(struct regulator_dev *rdev,
129e4ee831fSMark Brown 					unsigned int mode)
130e4ee831fSMark Brown {
131e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
132e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
133e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
134e4ee831fSMark Brown 
135e4ee831fSMark Brown 	return wm831x_dcdc_set_mode_int(wm831x, reg, mode);
136e4ee831fSMark Brown }
137e4ee831fSMark Brown 
138e4ee831fSMark Brown static int wm831x_dcdc_get_status(struct regulator_dev *rdev)
139e4ee831fSMark Brown {
140e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
141e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
142e4ee831fSMark Brown 	int ret;
143e4ee831fSMark Brown 
144e4ee831fSMark Brown 	/* First, check for errors */
145e4ee831fSMark Brown 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
146e4ee831fSMark Brown 	if (ret < 0)
147e4ee831fSMark Brown 		return ret;
148e4ee831fSMark Brown 
149e4ee831fSMark Brown 	if (ret & (1 << rdev_get_id(rdev))) {
150e4ee831fSMark Brown 		dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
151e4ee831fSMark Brown 			rdev_get_id(rdev) + 1);
152e4ee831fSMark Brown 		return REGULATOR_STATUS_ERROR;
153e4ee831fSMark Brown 	}
154e4ee831fSMark Brown 
155e4ee831fSMark Brown 	/* DCDC1 and DCDC2 can additionally detect high voltage/current */
156e4ee831fSMark Brown 	if (rdev_get_id(rdev) < 2) {
157e4ee831fSMark Brown 		if (ret & (WM831X_DC1_OV_STS << rdev_get_id(rdev))) {
158e4ee831fSMark Brown 			dev_dbg(wm831x->dev, "DCDC%d over voltage\n",
159e4ee831fSMark Brown 				rdev_get_id(rdev) + 1);
160e4ee831fSMark Brown 			return REGULATOR_STATUS_ERROR;
161e4ee831fSMark Brown 		}
162e4ee831fSMark Brown 
163e4ee831fSMark Brown 		if (ret & (WM831X_DC1_HC_STS << rdev_get_id(rdev))) {
164e4ee831fSMark Brown 			dev_dbg(wm831x->dev, "DCDC%d over current\n",
165e4ee831fSMark Brown 				rdev_get_id(rdev) + 1);
166e4ee831fSMark Brown 			return REGULATOR_STATUS_ERROR;
167e4ee831fSMark Brown 		}
168e4ee831fSMark Brown 	}
169e4ee831fSMark Brown 
170e4ee831fSMark Brown 	/* Is the regulator on? */
171e4ee831fSMark Brown 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
172e4ee831fSMark Brown 	if (ret < 0)
173e4ee831fSMark Brown 		return ret;
174e4ee831fSMark Brown 	if (!(ret & (1 << rdev_get_id(rdev))))
175e4ee831fSMark Brown 		return REGULATOR_STATUS_OFF;
176e4ee831fSMark Brown 
177e4ee831fSMark Brown 	/* TODO: When we handle hardware control modes so we can report the
178e4ee831fSMark Brown 	 * current mode. */
179e4ee831fSMark Brown 	return REGULATOR_STATUS_ON;
180e4ee831fSMark Brown }
181e4ee831fSMark Brown 
182e4ee831fSMark Brown static irqreturn_t wm831x_dcdc_uv_irq(int irq, void *data)
183e4ee831fSMark Brown {
184e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = data;
185e4ee831fSMark Brown 
186e4ee831fSMark Brown 	regulator_notifier_call_chain(dcdc->regulator,
187e4ee831fSMark Brown 				      REGULATOR_EVENT_UNDER_VOLTAGE,
188e4ee831fSMark Brown 				      NULL);
189e4ee831fSMark Brown 
190e4ee831fSMark Brown 	return IRQ_HANDLED;
191e4ee831fSMark Brown }
192e4ee831fSMark Brown 
193e4ee831fSMark Brown static irqreturn_t wm831x_dcdc_oc_irq(int irq, void *data)
194e4ee831fSMark Brown {
195e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = data;
196e4ee831fSMark Brown 
197e4ee831fSMark Brown 	regulator_notifier_call_chain(dcdc->regulator,
198e4ee831fSMark Brown 				      REGULATOR_EVENT_OVER_CURRENT,
199e4ee831fSMark Brown 				      NULL);
200e4ee831fSMark Brown 
201e4ee831fSMark Brown 	return IRQ_HANDLED;
202e4ee831fSMark Brown }
203e4ee831fSMark Brown 
204e4ee831fSMark Brown /*
205e4ee831fSMark Brown  * BUCKV specifics
206e4ee831fSMark Brown  */
207e4ee831fSMark Brown 
208e4ee831fSMark Brown static int wm831x_buckv_list_voltage(struct regulator_dev *rdev,
209e4ee831fSMark Brown 				      unsigned selector)
210e4ee831fSMark Brown {
211e4ee831fSMark Brown 	if (selector <= 0x8)
212e4ee831fSMark Brown 		return 600000;
213e4ee831fSMark Brown 	if (selector <= WM831X_BUCKV_MAX_SELECTOR)
214e4ee831fSMark Brown 		return 600000 + ((selector - 0x8) * 12500);
215e4ee831fSMark Brown 	return -EINVAL;
216e4ee831fSMark Brown }
217e4ee831fSMark Brown 
218b5fb77e0SAxel Lin static int wm831x_buckv_map_voltage(struct regulator_dev *rdev,
219e4ee831fSMark Brown 				   int min_uV, int max_uV)
220e4ee831fSMark Brown {
221e4ee831fSMark Brown 	u16 vsel;
222e4ee831fSMark Brown 
223e4ee831fSMark Brown 	if (min_uV < 600000)
224e4ee831fSMark Brown 		vsel = 0;
225e4ee831fSMark Brown 	else if (min_uV <= 1800000)
2261a679930SAxel Lin 		vsel = DIV_ROUND_UP(min_uV - 600000, 12500) + 8;
227e4ee831fSMark Brown 	else
228e4ee831fSMark Brown 		return -EINVAL;
229e4ee831fSMark Brown 
230e4ee831fSMark Brown 	if (wm831x_buckv_list_voltage(rdev, vsel) > max_uV)
231e4ee831fSMark Brown 		return -EINVAL;
232e4ee831fSMark Brown 
233e24a04c4SMark Brown 	return vsel;
234e24a04c4SMark Brown }
235e24a04c4SMark Brown 
236e24a04c4SMark Brown static int wm831x_buckv_set_dvs(struct regulator_dev *rdev, int state)
237e24a04c4SMark Brown {
238e24a04c4SMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
239e24a04c4SMark Brown 
240e24a04c4SMark Brown 	if (state == dcdc->dvs_gpio_state)
241e24a04c4SMark Brown 		return 0;
242e24a04c4SMark Brown 
243e24a04c4SMark Brown 	dcdc->dvs_gpio_state = state;
244e24a04c4SMark Brown 	gpio_set_value(dcdc->dvs_gpio, state);
245e24a04c4SMark Brown 
246e24a04c4SMark Brown 	/* Should wait for DVS state change to be asserted if we have
247e24a04c4SMark Brown 	 * a GPIO for it, for now assume the device is configured
248e24a04c4SMark Brown 	 * for the fastest possible transition.
249e24a04c4SMark Brown 	 */
250e24a04c4SMark Brown 
251e24a04c4SMark Brown 	return 0;
252e4ee831fSMark Brown }
253e4ee831fSMark Brown 
254b5fb77e0SAxel Lin static int wm831x_buckv_set_voltage_sel(struct regulator_dev *rdev,
255b5fb77e0SAxel Lin 					unsigned vsel)
256e4ee831fSMark Brown {
257e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
258e24a04c4SMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
259e24a04c4SMark Brown 	int on_reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
260e24a04c4SMark Brown 	int dvs_reg = dcdc->base + WM831X_DCDC_DVS_CONTROL;
261b5fb77e0SAxel Lin 	int ret;
2623a93f2a9SMark Brown 
263e24a04c4SMark Brown 	/* If this value is already set then do a GPIO update if we can */
264e24a04c4SMark Brown 	if (dcdc->dvs_gpio && dcdc->on_vsel == vsel)
265e24a04c4SMark Brown 		return wm831x_buckv_set_dvs(rdev, 0);
266e24a04c4SMark Brown 
267e24a04c4SMark Brown 	if (dcdc->dvs_gpio && dcdc->dvs_vsel == vsel)
268e24a04c4SMark Brown 		return wm831x_buckv_set_dvs(rdev, 1);
269e24a04c4SMark Brown 
270e24a04c4SMark Brown 	/* Always set the ON status to the minimum voltage */
271e24a04c4SMark Brown 	ret = wm831x_set_bits(wm831x, on_reg, WM831X_DC1_ON_VSEL_MASK, vsel);
272e24a04c4SMark Brown 	if (ret < 0)
273e24a04c4SMark Brown 		return ret;
274e24a04c4SMark Brown 	dcdc->on_vsel = vsel;
275e24a04c4SMark Brown 
276e24a04c4SMark Brown 	if (!dcdc->dvs_gpio)
277e24a04c4SMark Brown 		return ret;
278e24a04c4SMark Brown 
279e24a04c4SMark Brown 	/* Kick the voltage transition now */
280e24a04c4SMark Brown 	ret = wm831x_buckv_set_dvs(rdev, 0);
281e24a04c4SMark Brown 	if (ret < 0)
282e24a04c4SMark Brown 		return ret;
283e24a04c4SMark Brown 
28488cda60eSMark Brown 	/*
28588cda60eSMark Brown 	 * If this VSEL is higher than the last one we've seen then
28688cda60eSMark Brown 	 * remember it as the DVS VSEL.  This is optimised for CPUfreq
28788cda60eSMark Brown 	 * usage where we want to get to the highest voltage very
28888cda60eSMark Brown 	 * quickly.
28988cda60eSMark Brown 	 */
29088cda60eSMark Brown 	if (vsel > dcdc->dvs_vsel) {
29188cda60eSMark Brown 		ret = wm831x_set_bits(wm831x, dvs_reg,
29288cda60eSMark Brown 				      WM831X_DC1_DVS_VSEL_MASK,
29313ae633cSMark Brown 				      vsel);
294e24a04c4SMark Brown 		if (ret == 0)
295e24a04c4SMark Brown 			dcdc->dvs_vsel = vsel;
296e24a04c4SMark Brown 		else
29788cda60eSMark Brown 			dev_warn(wm831x->dev,
29888cda60eSMark Brown 				 "Failed to set DCDC DVS VSEL: %d\n", ret);
29988cda60eSMark Brown 	}
300e24a04c4SMark Brown 
301e24a04c4SMark Brown 	return 0;
302e4ee831fSMark Brown }
303e4ee831fSMark Brown 
304e4ee831fSMark Brown static int wm831x_buckv_set_suspend_voltage(struct regulator_dev *rdev,
305e4ee831fSMark Brown 					    int uV)
306e4ee831fSMark Brown {
307e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
308e24a04c4SMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
309e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
310e24a04c4SMark Brown 	int vsel;
311e4ee831fSMark Brown 
312b5fb77e0SAxel Lin 	vsel = wm831x_buckv_map_voltage(rdev, uV, uV);
313e24a04c4SMark Brown 	if (vsel < 0)
314e24a04c4SMark Brown 		return vsel;
315e24a04c4SMark Brown 
316e24a04c4SMark Brown 	return wm831x_set_bits(wm831x, reg, WM831X_DC1_SLP_VSEL_MASK, vsel);
317e4ee831fSMark Brown }
318e4ee831fSMark Brown 
319afb8bb80SMark Brown static int wm831x_buckv_get_voltage_sel(struct regulator_dev *rdev)
320e4ee831fSMark Brown {
321e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
322e4ee831fSMark Brown 
323e24a04c4SMark Brown 	if (dcdc->dvs_gpio && dcdc->dvs_gpio_state)
324afb8bb80SMark Brown 		return dcdc->dvs_vsel;
325e24a04c4SMark Brown 	else
326afb8bb80SMark Brown 		return dcdc->on_vsel;
327e4ee831fSMark Brown }
328e4ee831fSMark Brown 
329e4ee831fSMark Brown /* Current limit options */
330e4ee831fSMark Brown static u16 wm831x_dcdc_ilim[] = {
331e4ee831fSMark Brown 	125, 250, 375, 500, 625, 750, 875, 1000
332e4ee831fSMark Brown };
333e4ee831fSMark Brown 
334e4ee831fSMark Brown static int wm831x_buckv_set_current_limit(struct regulator_dev *rdev,
335e4ee831fSMark Brown 					   int min_uA, int max_uA)
336e4ee831fSMark Brown {
337e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
338e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
339e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2;
340e4ee831fSMark Brown 	int i;
341e4ee831fSMark Brown 
342422294deSAxel Lin 	for (i = ARRAY_SIZE(wm831x_dcdc_ilim) - 1; i >= 0; i--) {
343a171e782SAxel Lin 		if ((min_uA <= wm831x_dcdc_ilim[i]) &&
344a171e782SAxel Lin 		    (wm831x_dcdc_ilim[i] <= max_uA))
345422294deSAxel Lin 			return wm831x_set_bits(wm831x, reg,
346422294deSAxel Lin 					       WM831X_DC1_HC_THR_MASK,
34709bf14b9SAxel Lin 						i << WM831X_DC1_HC_THR_SHIFT);
348e4ee831fSMark Brown 	}
349e4ee831fSMark Brown 
350422294deSAxel Lin 	return -EINVAL;
351422294deSAxel Lin }
352422294deSAxel Lin 
353e4ee831fSMark Brown static int wm831x_buckv_get_current_limit(struct regulator_dev *rdev)
354e4ee831fSMark Brown {
355e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
356e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
357e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2;
358e4ee831fSMark Brown 	int val;
359e4ee831fSMark Brown 
360e4ee831fSMark Brown 	val = wm831x_reg_read(wm831x, reg);
361e4ee831fSMark Brown 	if (val < 0)
362e4ee831fSMark Brown 		return val;
363e4ee831fSMark Brown 
36409bf14b9SAxel Lin 	val = (val & WM831X_DC1_HC_THR_MASK) >> WM831X_DC1_HC_THR_SHIFT;
36509bf14b9SAxel Lin 	return wm831x_dcdc_ilim[val];
366e4ee831fSMark Brown }
367e4ee831fSMark Brown 
368e4ee831fSMark Brown static struct regulator_ops wm831x_buckv_ops = {
369b5fb77e0SAxel Lin 	.set_voltage_sel = wm831x_buckv_set_voltage_sel,
370afb8bb80SMark Brown 	.get_voltage_sel = wm831x_buckv_get_voltage_sel,
371e4ee831fSMark Brown 	.list_voltage = wm831x_buckv_list_voltage,
372b5fb77e0SAxel Lin 	.map_voltage = wm831x_buckv_map_voltage,
373e4ee831fSMark Brown 	.set_suspend_voltage = wm831x_buckv_set_suspend_voltage,
374e4ee831fSMark Brown 	.set_current_limit = wm831x_buckv_set_current_limit,
375e4ee831fSMark Brown 	.get_current_limit = wm831x_buckv_get_current_limit,
376e4ee831fSMark Brown 
3773d138fccSMark Brown 	.is_enabled = regulator_is_enabled_regmap,
3783d138fccSMark Brown 	.enable = regulator_enable_regmap,
3793d138fccSMark Brown 	.disable = regulator_disable_regmap,
380e4ee831fSMark Brown 	.get_status = wm831x_dcdc_get_status,
381e4ee831fSMark Brown 	.get_mode = wm831x_dcdc_get_mode,
382e4ee831fSMark Brown 	.set_mode = wm831x_dcdc_set_mode,
383e4ee831fSMark Brown 	.set_suspend_mode = wm831x_dcdc_set_suspend_mode,
384e4ee831fSMark Brown };
385e4ee831fSMark Brown 
386e24a04c4SMark Brown /*
387e24a04c4SMark Brown  * Set up DVS control.  We just log errors since we can still run
388e24a04c4SMark Brown  * (with reduced performance) if we fail.
389e24a04c4SMark Brown  */
3901aa20d27SMark Brown static void wm831x_buckv_dvs_init(struct platform_device *pdev,
3911aa20d27SMark Brown 				  struct wm831x_dcdc *dcdc,
392e24a04c4SMark Brown 				  struct wm831x_buckv_pdata *pdata)
393e24a04c4SMark Brown {
394e24a04c4SMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
395e24a04c4SMark Brown 	int ret;
396e24a04c4SMark Brown 	u16 ctrl;
397e24a04c4SMark Brown 
398e24a04c4SMark Brown 	if (!pdata || !pdata->dvs_gpio)
399e24a04c4SMark Brown 		return;
400e24a04c4SMark Brown 
401e24a04c4SMark Brown 	/* gpiolib won't let us read the GPIO status so pick the higher
402e24a04c4SMark Brown 	 * of the two existing voltages so we take it as platform data.
403e24a04c4SMark Brown 	 */
404e24a04c4SMark Brown 	dcdc->dvs_gpio_state = pdata->dvs_init_state;
405e24a04c4SMark Brown 
4061aa20d27SMark Brown 	ret = devm_gpio_request_one(&pdev->dev, pdata->dvs_gpio,
407053fa1b7SMark Brown 				    dcdc->dvs_gpio_state ? GPIOF_INIT_HIGH : 0,
408053fa1b7SMark Brown 				    "DCDC DVS");
409e24a04c4SMark Brown 	if (ret < 0) {
410053fa1b7SMark Brown 		dev_err(wm831x->dev, "Failed to get %s DVS GPIO: %d\n",
411e24a04c4SMark Brown 			dcdc->name, ret);
412e24a04c4SMark Brown 		return;
413e24a04c4SMark Brown 	}
414e24a04c4SMark Brown 
415e24a04c4SMark Brown 	dcdc->dvs_gpio = pdata->dvs_gpio;
416b47ba9fdSMark Brown 
417b47ba9fdSMark Brown 	switch (pdata->dvs_control_src) {
418b47ba9fdSMark Brown 	case 1:
419b47ba9fdSMark Brown 		ctrl = 2 << WM831X_DC1_DVS_SRC_SHIFT;
420b47ba9fdSMark Brown 		break;
421b47ba9fdSMark Brown 	case 2:
422b47ba9fdSMark Brown 		ctrl = 3 << WM831X_DC1_DVS_SRC_SHIFT;
423b47ba9fdSMark Brown 		break;
424b47ba9fdSMark Brown 	default:
425b47ba9fdSMark Brown 		dev_err(wm831x->dev, "Invalid DVS control source %d for %s\n",
426b47ba9fdSMark Brown 			pdata->dvs_control_src, dcdc->name);
427b47ba9fdSMark Brown 		return;
428b47ba9fdSMark Brown 	}
429b47ba9fdSMark Brown 
430c439b8f4SMark Brown 	/* If DVS_VSEL is set to the minimum value then raise it to ON_VSEL
431c439b8f4SMark Brown 	 * to make bootstrapping a bit smoother.
432c439b8f4SMark Brown 	 */
433c439b8f4SMark Brown 	if (!dcdc->dvs_vsel) {
434c439b8f4SMark Brown 		ret = wm831x_set_bits(wm831x,
435c439b8f4SMark Brown 				      dcdc->base + WM831X_DCDC_DVS_CONTROL,
436c439b8f4SMark Brown 				      WM831X_DC1_DVS_VSEL_MASK, dcdc->on_vsel);
437c439b8f4SMark Brown 		if (ret == 0)
438c439b8f4SMark Brown 			dcdc->dvs_vsel = dcdc->on_vsel;
439c439b8f4SMark Brown 		else
440c439b8f4SMark Brown 			dev_warn(wm831x->dev, "Failed to set DVS_VSEL: %d\n",
441c439b8f4SMark Brown 				 ret);
442c439b8f4SMark Brown 	}
443c439b8f4SMark Brown 
444b47ba9fdSMark Brown 	ret = wm831x_set_bits(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL,
445b47ba9fdSMark Brown 			      WM831X_DC1_DVS_SRC_MASK, ctrl);
446b47ba9fdSMark Brown 	if (ret < 0) {
447b47ba9fdSMark Brown 		dev_err(wm831x->dev, "Failed to set %s DVS source: %d\n",
448b47ba9fdSMark Brown 			dcdc->name, ret);
449b47ba9fdSMark Brown 	}
450e24a04c4SMark Brown }
451e24a04c4SMark Brown 
452a5023574SBill Pemberton static int wm831x_buckv_probe(struct platform_device *pdev)
453e4ee831fSMark Brown {
454e4ee831fSMark Brown 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
455dff91d0bSJingoo Han 	struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
456c172708dSMark Brown 	struct regulator_config config = { };
457137a6354SMark Brown 	int id;
458e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc;
459e4ee831fSMark Brown 	struct resource *res;
460e4ee831fSMark Brown 	int ret, irq;
461e4ee831fSMark Brown 
462137a6354SMark Brown 	if (pdata && pdata->wm831x_num)
463137a6354SMark Brown 		id = (pdata->wm831x_num * 10) + 1;
464137a6354SMark Brown 	else
465137a6354SMark Brown 		id = 0;
466137a6354SMark Brown 	id = pdev->id - id;
467137a6354SMark Brown 
468e4ee831fSMark Brown 	dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
469e4ee831fSMark Brown 
470fded2f4fSMark Brown 	dcdc = devm_kzalloc(&pdev->dev,  sizeof(struct wm831x_dcdc),
471fded2f4fSMark Brown 			    GFP_KERNEL);
472e4ee831fSMark Brown 	if (dcdc == NULL) {
473e4ee831fSMark Brown 		dev_err(&pdev->dev, "Unable to allocate private data\n");
474e4ee831fSMark Brown 		return -ENOMEM;
475e4ee831fSMark Brown 	}
476e4ee831fSMark Brown 
477e4ee831fSMark Brown 	dcdc->wm831x = wm831x;
478e4ee831fSMark Brown 
4795656098eSMark Brown 	res = platform_get_resource(pdev, IORESOURCE_REG, 0);
480e4ee831fSMark Brown 	if (res == NULL) {
4815656098eSMark Brown 		dev_err(&pdev->dev, "No REG resource\n");
482e4ee831fSMark Brown 		ret = -EINVAL;
483e4ee831fSMark Brown 		goto err;
484e4ee831fSMark Brown 	}
485e4ee831fSMark Brown 	dcdc->base = res->start;
486e4ee831fSMark Brown 
487e4ee831fSMark Brown 	snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
488e4ee831fSMark Brown 	dcdc->desc.name = dcdc->name;
48982caa978SMark Brown 
49082caa978SMark Brown 	snprintf(dcdc->supply_name, sizeof(dcdc->supply_name),
49182caa978SMark Brown 		 "DC%dVDD", id + 1);
49282caa978SMark Brown 	dcdc->desc.supply_name = dcdc->supply_name;
49382caa978SMark Brown 
494e4ee831fSMark Brown 	dcdc->desc.id = id;
495e4ee831fSMark Brown 	dcdc->desc.type = REGULATOR_VOLTAGE;
496e4ee831fSMark Brown 	dcdc->desc.n_voltages = WM831X_BUCKV_MAX_SELECTOR + 1;
497e4ee831fSMark Brown 	dcdc->desc.ops = &wm831x_buckv_ops;
498e4ee831fSMark Brown 	dcdc->desc.owner = THIS_MODULE;
4993d138fccSMark Brown 	dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
5003d138fccSMark Brown 	dcdc->desc.enable_mask = 1 << id;
501e4ee831fSMark Brown 
502e24a04c4SMark Brown 	ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG);
503e24a04c4SMark Brown 	if (ret < 0) {
504e24a04c4SMark Brown 		dev_err(wm831x->dev, "Failed to read ON VSEL: %d\n", ret);
505e24a04c4SMark Brown 		goto err;
506e24a04c4SMark Brown 	}
507e24a04c4SMark Brown 	dcdc->on_vsel = ret & WM831X_DC1_ON_VSEL_MASK;
508e24a04c4SMark Brown 
509a1b81dd3SMark Brown 	ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL);
510e24a04c4SMark Brown 	if (ret < 0) {
511e24a04c4SMark Brown 		dev_err(wm831x->dev, "Failed to read DVS VSEL: %d\n", ret);
512e24a04c4SMark Brown 		goto err;
513e24a04c4SMark Brown 	}
514e24a04c4SMark Brown 	dcdc->dvs_vsel = ret & WM831X_DC1_DVS_VSEL_MASK;
515e24a04c4SMark Brown 
516f0b067d9SMark Brown 	if (pdata && pdata->dcdc[id])
5171aa20d27SMark Brown 		wm831x_buckv_dvs_init(pdev, dcdc,
5181aa20d27SMark Brown 				      pdata->dcdc[id]->driver_data);
519e24a04c4SMark Brown 
520c172708dSMark Brown 	config.dev = pdev->dev.parent;
521b7ca8788SMark Brown 	if (pdata)
522c172708dSMark Brown 		config.init_data = pdata->dcdc[id];
523c172708dSMark Brown 	config.driver_data = dcdc;
5243d138fccSMark Brown 	config.regmap = wm831x->regmap;
525c172708dSMark Brown 
526d73b4cb7SMark Brown 	dcdc->regulator = devm_regulator_register(&pdev->dev, &dcdc->desc,
527d73b4cb7SMark Brown 						  &config);
528e4ee831fSMark Brown 	if (IS_ERR(dcdc->regulator)) {
529e4ee831fSMark Brown 		ret = PTR_ERR(dcdc->regulator);
530e4ee831fSMark Brown 		dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
531e4ee831fSMark Brown 			id + 1, ret);
532e4ee831fSMark Brown 		goto err;
533e4ee831fSMark Brown 	}
534e4ee831fSMark Brown 
535cd99758bSMark Brown 	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
536b0c4c0c6SMark Brown 	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
537b0c4c0c6SMark Brown 					wm831x_dcdc_uv_irq,
538dfda9c27SMark Brown 					IRQF_TRIGGER_RISING, dcdc->name, dcdc);
539e4ee831fSMark Brown 	if (ret != 0) {
540e4ee831fSMark Brown 		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
541e4ee831fSMark Brown 			irq, ret);
542d73b4cb7SMark Brown 		goto err;
543e4ee831fSMark Brown 	}
544e4ee831fSMark Brown 
545cd99758bSMark Brown 	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "HC"));
546b0c4c0c6SMark Brown 	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
547b0c4c0c6SMark Brown 					wm831x_dcdc_oc_irq,
548dfda9c27SMark Brown 					IRQF_TRIGGER_RISING, dcdc->name, dcdc);
549e4ee831fSMark Brown 	if (ret != 0) {
550e4ee831fSMark Brown 		dev_err(&pdev->dev, "Failed to request HC IRQ %d: %d\n",
551e4ee831fSMark Brown 			irq, ret);
552d73b4cb7SMark Brown 		goto err;
553e4ee831fSMark Brown 	}
554e4ee831fSMark Brown 
555e4ee831fSMark Brown 	platform_set_drvdata(pdev, dcdc);
556e4ee831fSMark Brown 
557e4ee831fSMark Brown 	return 0;
558e4ee831fSMark Brown 
559e4ee831fSMark Brown err:
560e4ee831fSMark Brown 	return ret;
561e4ee831fSMark Brown }
562e4ee831fSMark Brown 
563e4ee831fSMark Brown static struct platform_driver wm831x_buckv_driver = {
564e4ee831fSMark Brown 	.probe = wm831x_buckv_probe,
565e4ee831fSMark Brown 	.driver		= {
566e4ee831fSMark Brown 		.name	= "wm831x-buckv",
567eb66d565SDmitry Torokhov 		.owner	= THIS_MODULE,
568e4ee831fSMark Brown 	},
569e4ee831fSMark Brown };
570e4ee831fSMark Brown 
571e4ee831fSMark Brown /*
572e4ee831fSMark Brown  * BUCKP specifics
573e4ee831fSMark Brown  */
574e4ee831fSMark Brown 
575d580cb5eSAxel Lin static int wm831x_buckp_set_suspend_voltage(struct regulator_dev *rdev, int uV)
576e4ee831fSMark Brown {
577e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
578e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
579e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
580d580cb5eSAxel Lin 	int sel;
581e4ee831fSMark Brown 
582d580cb5eSAxel Lin 	sel = regulator_map_voltage_linear(rdev, uV, uV);
583d580cb5eSAxel Lin 	if (sel < 0)
584d580cb5eSAxel Lin 		return sel;
585d580cb5eSAxel Lin 
586d580cb5eSAxel Lin 	return wm831x_set_bits(wm831x, reg, WM831X_DC3_ON_VSEL_MASK, sel);
587e4ee831fSMark Brown }
588e4ee831fSMark Brown 
589e4ee831fSMark Brown static struct regulator_ops wm831x_buckp_ops = {
590d580cb5eSAxel Lin 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
591817436e7SMark Brown 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
592c70ad9dcSAxel Lin 	.list_voltage = regulator_list_voltage_linear,
593d580cb5eSAxel Lin 	.map_voltage = regulator_map_voltage_linear,
594e4ee831fSMark Brown 	.set_suspend_voltage = wm831x_buckp_set_suspend_voltage,
595e4ee831fSMark Brown 
5963d138fccSMark Brown 	.is_enabled = regulator_is_enabled_regmap,
5973d138fccSMark Brown 	.enable = regulator_enable_regmap,
5983d138fccSMark Brown 	.disable = regulator_disable_regmap,
599e4ee831fSMark Brown 	.get_status = wm831x_dcdc_get_status,
600e4ee831fSMark Brown 	.get_mode = wm831x_dcdc_get_mode,
601e4ee831fSMark Brown 	.set_mode = wm831x_dcdc_set_mode,
602e4ee831fSMark Brown 	.set_suspend_mode = wm831x_dcdc_set_suspend_mode,
603e4ee831fSMark Brown };
604e4ee831fSMark Brown 
605a5023574SBill Pemberton static int wm831x_buckp_probe(struct platform_device *pdev)
606e4ee831fSMark Brown {
607e4ee831fSMark Brown 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
608dff91d0bSJingoo Han 	struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
609c172708dSMark Brown 	struct regulator_config config = { };
610137a6354SMark Brown 	int id;
611e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc;
612e4ee831fSMark Brown 	struct resource *res;
613e4ee831fSMark Brown 	int ret, irq;
614e4ee831fSMark Brown 
615137a6354SMark Brown 	if (pdata && pdata->wm831x_num)
616137a6354SMark Brown 		id = (pdata->wm831x_num * 10) + 1;
617137a6354SMark Brown 	else
618137a6354SMark Brown 		id = 0;
619137a6354SMark Brown 	id = pdev->id - id;
620137a6354SMark Brown 
621e4ee831fSMark Brown 	dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
622e4ee831fSMark Brown 
623fded2f4fSMark Brown 	dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc),
624fded2f4fSMark Brown 			    GFP_KERNEL);
625e4ee831fSMark Brown 	if (dcdc == NULL) {
626e4ee831fSMark Brown 		dev_err(&pdev->dev, "Unable to allocate private data\n");
627e4ee831fSMark Brown 		return -ENOMEM;
628e4ee831fSMark Brown 	}
629e4ee831fSMark Brown 
630e4ee831fSMark Brown 	dcdc->wm831x = wm831x;
631e4ee831fSMark Brown 
6325656098eSMark Brown 	res = platform_get_resource(pdev, IORESOURCE_REG, 0);
633e4ee831fSMark Brown 	if (res == NULL) {
6345656098eSMark Brown 		dev_err(&pdev->dev, "No REG resource\n");
635e4ee831fSMark Brown 		ret = -EINVAL;
636e4ee831fSMark Brown 		goto err;
637e4ee831fSMark Brown 	}
638e4ee831fSMark Brown 	dcdc->base = res->start;
639e4ee831fSMark Brown 
640e4ee831fSMark Brown 	snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
641e4ee831fSMark Brown 	dcdc->desc.name = dcdc->name;
64282caa978SMark Brown 
64382caa978SMark Brown 	snprintf(dcdc->supply_name, sizeof(dcdc->supply_name),
64482caa978SMark Brown 		 "DC%dVDD", id + 1);
64582caa978SMark Brown 	dcdc->desc.supply_name = dcdc->supply_name;
64682caa978SMark Brown 
647e4ee831fSMark Brown 	dcdc->desc.id = id;
648e4ee831fSMark Brown 	dcdc->desc.type = REGULATOR_VOLTAGE;
649e4ee831fSMark Brown 	dcdc->desc.n_voltages = WM831X_BUCKP_MAX_SELECTOR + 1;
650e4ee831fSMark Brown 	dcdc->desc.ops = &wm831x_buckp_ops;
651e4ee831fSMark Brown 	dcdc->desc.owner = THIS_MODULE;
652817436e7SMark Brown 	dcdc->desc.vsel_reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
653817436e7SMark Brown 	dcdc->desc.vsel_mask = WM831X_DC3_ON_VSEL_MASK;
6543d138fccSMark Brown 	dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
6553d138fccSMark Brown 	dcdc->desc.enable_mask = 1 << id;
656c70ad9dcSAxel Lin 	dcdc->desc.min_uV = 850000;
657c70ad9dcSAxel Lin 	dcdc->desc.uV_step = 25000;
658e4ee831fSMark Brown 
659c172708dSMark Brown 	config.dev = pdev->dev.parent;
660b7ca8788SMark Brown 	if (pdata)
661c172708dSMark Brown 		config.init_data = pdata->dcdc[id];
662c172708dSMark Brown 	config.driver_data = dcdc;
663817436e7SMark Brown 	config.regmap = wm831x->regmap;
664c172708dSMark Brown 
665d73b4cb7SMark Brown 	dcdc->regulator = devm_regulator_register(&pdev->dev, &dcdc->desc,
666d73b4cb7SMark Brown 						  &config);
667e4ee831fSMark Brown 	if (IS_ERR(dcdc->regulator)) {
668e4ee831fSMark Brown 		ret = PTR_ERR(dcdc->regulator);
669e4ee831fSMark Brown 		dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
670e4ee831fSMark Brown 			id + 1, ret);
671e4ee831fSMark Brown 		goto err;
672e4ee831fSMark Brown 	}
673e4ee831fSMark Brown 
674cd99758bSMark Brown 	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
675b0c4c0c6SMark Brown 	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
676b0c4c0c6SMark Brown 					wm831x_dcdc_uv_irq,
677dfda9c27SMark Brown 					IRQF_TRIGGER_RISING, dcdc->name, dcdc);
678e4ee831fSMark Brown 	if (ret != 0) {
679e4ee831fSMark Brown 		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
680e4ee831fSMark Brown 			irq, ret);
681d73b4cb7SMark Brown 		goto err;
682e4ee831fSMark Brown 	}
683e4ee831fSMark Brown 
684e4ee831fSMark Brown 	platform_set_drvdata(pdev, dcdc);
685e4ee831fSMark Brown 
686e4ee831fSMark Brown 	return 0;
687e4ee831fSMark Brown 
688e4ee831fSMark Brown err:
689e4ee831fSMark Brown 	return ret;
690e4ee831fSMark Brown }
691e4ee831fSMark Brown 
692e4ee831fSMark Brown static struct platform_driver wm831x_buckp_driver = {
693e4ee831fSMark Brown 	.probe = wm831x_buckp_probe,
694e4ee831fSMark Brown 	.driver		= {
695e4ee831fSMark Brown 		.name	= "wm831x-buckp",
696eb66d565SDmitry Torokhov 		.owner	= THIS_MODULE,
697e4ee831fSMark Brown 	},
698e4ee831fSMark Brown };
699e4ee831fSMark Brown 
7008267a9baSMark Brown /*
7011304850dSMark Brown  * DCDC boost convertors
7021304850dSMark Brown  */
7031304850dSMark Brown 
7041304850dSMark Brown static int wm831x_boostp_get_status(struct regulator_dev *rdev)
7051304850dSMark Brown {
7061304850dSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
7071304850dSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
7081304850dSMark Brown 	int ret;
7091304850dSMark Brown 
7101304850dSMark Brown 	/* First, check for errors */
7111304850dSMark Brown 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
7121304850dSMark Brown 	if (ret < 0)
7131304850dSMark Brown 		return ret;
7141304850dSMark Brown 
7151304850dSMark Brown 	if (ret & (1 << rdev_get_id(rdev))) {
7161304850dSMark Brown 		dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
7171304850dSMark Brown 			rdev_get_id(rdev) + 1);
7181304850dSMark Brown 		return REGULATOR_STATUS_ERROR;
7191304850dSMark Brown 	}
7201304850dSMark Brown 
7211304850dSMark Brown 	/* Is the regulator on? */
7221304850dSMark Brown 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
7231304850dSMark Brown 	if (ret < 0)
7241304850dSMark Brown 		return ret;
7251304850dSMark Brown 	if (ret & (1 << rdev_get_id(rdev)))
7261304850dSMark Brown 		return REGULATOR_STATUS_ON;
7271304850dSMark Brown 	else
7281304850dSMark Brown 		return REGULATOR_STATUS_OFF;
7291304850dSMark Brown }
7301304850dSMark Brown 
7311304850dSMark Brown static struct regulator_ops wm831x_boostp_ops = {
7321304850dSMark Brown 	.get_status = wm831x_boostp_get_status,
7331304850dSMark Brown 
7343d138fccSMark Brown 	.is_enabled = regulator_is_enabled_regmap,
7353d138fccSMark Brown 	.enable = regulator_enable_regmap,
7363d138fccSMark Brown 	.disable = regulator_disable_regmap,
7371304850dSMark Brown };
7381304850dSMark Brown 
739a5023574SBill Pemberton static int wm831x_boostp_probe(struct platform_device *pdev)
7401304850dSMark Brown {
7411304850dSMark Brown 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
742dff91d0bSJingoo Han 	struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
743c172708dSMark Brown 	struct regulator_config config = { };
7441304850dSMark Brown 	int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
7451304850dSMark Brown 	struct wm831x_dcdc *dcdc;
7461304850dSMark Brown 	struct resource *res;
7471304850dSMark Brown 	int ret, irq;
7481304850dSMark Brown 
7491304850dSMark Brown 	dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
7501304850dSMark Brown 
7511304850dSMark Brown 	if (pdata == NULL || pdata->dcdc[id] == NULL)
7521304850dSMark Brown 		return -ENODEV;
7531304850dSMark Brown 
7544c60165dSAxel Lin 	dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), GFP_KERNEL);
7551304850dSMark Brown 	if (dcdc == NULL) {
7561304850dSMark Brown 		dev_err(&pdev->dev, "Unable to allocate private data\n");
7571304850dSMark Brown 		return -ENOMEM;
7581304850dSMark Brown 	}
7591304850dSMark Brown 
7601304850dSMark Brown 	dcdc->wm831x = wm831x;
7611304850dSMark Brown 
7625656098eSMark Brown 	res = platform_get_resource(pdev, IORESOURCE_REG, 0);
7631304850dSMark Brown 	if (res == NULL) {
7645656098eSMark Brown 		dev_err(&pdev->dev, "No REG resource\n");
7651304850dSMark Brown 		ret = -EINVAL;
7661304850dSMark Brown 		goto err;
7671304850dSMark Brown 	}
7681304850dSMark Brown 	dcdc->base = res->start;
7691304850dSMark Brown 
7701304850dSMark Brown 	snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
7711304850dSMark Brown 	dcdc->desc.name = dcdc->name;
7721304850dSMark Brown 	dcdc->desc.id = id;
7731304850dSMark Brown 	dcdc->desc.type = REGULATOR_VOLTAGE;
7741304850dSMark Brown 	dcdc->desc.ops = &wm831x_boostp_ops;
7751304850dSMark Brown 	dcdc->desc.owner = THIS_MODULE;
7763d138fccSMark Brown 	dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
7773d138fccSMark Brown 	dcdc->desc.enable_mask = 1 << id;
7781304850dSMark Brown 
779c172708dSMark Brown 	config.dev = pdev->dev.parent;
780f0b067d9SMark Brown 	if (pdata)
781c172708dSMark Brown 		config.init_data = pdata->dcdc[id];
782c172708dSMark Brown 	config.driver_data = dcdc;
7833d138fccSMark Brown 	config.regmap = wm831x->regmap;
784c172708dSMark Brown 
785d73b4cb7SMark Brown 	dcdc->regulator = devm_regulator_register(&pdev->dev, &dcdc->desc,
786d73b4cb7SMark Brown 						  &config);
7871304850dSMark Brown 	if (IS_ERR(dcdc->regulator)) {
7881304850dSMark Brown 		ret = PTR_ERR(dcdc->regulator);
7891304850dSMark Brown 		dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
7901304850dSMark Brown 			id + 1, ret);
7911304850dSMark Brown 		goto err;
7921304850dSMark Brown 	}
7931304850dSMark Brown 
794cd99758bSMark Brown 	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
795b0c4c0c6SMark Brown 	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
796b0c4c0c6SMark Brown 					wm831x_dcdc_uv_irq,
7971304850dSMark Brown 					IRQF_TRIGGER_RISING, dcdc->name,
7981304850dSMark Brown 					dcdc);
7991304850dSMark Brown 	if (ret != 0) {
8001304850dSMark Brown 		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
8011304850dSMark Brown 			irq, ret);
802d73b4cb7SMark Brown 		goto err;
8031304850dSMark Brown 	}
8041304850dSMark Brown 
8051304850dSMark Brown 	platform_set_drvdata(pdev, dcdc);
8061304850dSMark Brown 
8071304850dSMark Brown 	return 0;
8081304850dSMark Brown 
8091304850dSMark Brown err:
8101304850dSMark Brown 	return ret;
8111304850dSMark Brown }
8121304850dSMark Brown 
8131304850dSMark Brown static struct platform_driver wm831x_boostp_driver = {
8141304850dSMark Brown 	.probe = wm831x_boostp_probe,
8151304850dSMark Brown 	.driver		= {
8161304850dSMark Brown 		.name	= "wm831x-boostp",
817eb66d565SDmitry Torokhov 		.owner	= THIS_MODULE,
8181304850dSMark Brown 	},
8191304850dSMark Brown };
8201304850dSMark Brown 
8211304850dSMark Brown /*
8228267a9baSMark Brown  * External Power Enable
8238267a9baSMark Brown  *
8248267a9baSMark Brown  * These aren't actually DCDCs but look like them in hardware so share
8258267a9baSMark Brown  * code.
8268267a9baSMark Brown  */
8278267a9baSMark Brown 
8288267a9baSMark Brown #define WM831X_EPE_BASE 6
8298267a9baSMark Brown 
8308267a9baSMark Brown static struct regulator_ops wm831x_epe_ops = {
8313d138fccSMark Brown 	.is_enabled = regulator_is_enabled_regmap,
8323d138fccSMark Brown 	.enable = regulator_enable_regmap,
8333d138fccSMark Brown 	.disable = regulator_disable_regmap,
8348267a9baSMark Brown 	.get_status = wm831x_dcdc_get_status,
8358267a9baSMark Brown };
8368267a9baSMark Brown 
837a5023574SBill Pemberton static int wm831x_epe_probe(struct platform_device *pdev)
8388267a9baSMark Brown {
8398267a9baSMark Brown 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
840dff91d0bSJingoo Han 	struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
841c172708dSMark Brown 	struct regulator_config config = { };
8428267a9baSMark Brown 	int id = pdev->id % ARRAY_SIZE(pdata->epe);
8438267a9baSMark Brown 	struct wm831x_dcdc *dcdc;
8448267a9baSMark Brown 	int ret;
8458267a9baSMark Brown 
8468267a9baSMark Brown 	dev_dbg(&pdev->dev, "Probing EPE%d\n", id + 1);
8478267a9baSMark Brown 
8484c60165dSAxel Lin 	dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), GFP_KERNEL);
8498267a9baSMark Brown 	if (dcdc == NULL) {
8508267a9baSMark Brown 		dev_err(&pdev->dev, "Unable to allocate private data\n");
8518267a9baSMark Brown 		return -ENOMEM;
8528267a9baSMark Brown 	}
8538267a9baSMark Brown 
8548267a9baSMark Brown 	dcdc->wm831x = wm831x;
8558267a9baSMark Brown 
8568267a9baSMark Brown 	/* For current parts this is correct; probably need to revisit
8578267a9baSMark Brown 	 * in future.
8588267a9baSMark Brown 	 */
8598267a9baSMark Brown 	snprintf(dcdc->name, sizeof(dcdc->name), "EPE%d", id + 1);
8608267a9baSMark Brown 	dcdc->desc.name = dcdc->name;
8618267a9baSMark Brown 	dcdc->desc.id = id + WM831X_EPE_BASE; /* Offset in DCDC registers */
8628267a9baSMark Brown 	dcdc->desc.ops = &wm831x_epe_ops;
8638267a9baSMark Brown 	dcdc->desc.type = REGULATOR_VOLTAGE;
8648267a9baSMark Brown 	dcdc->desc.owner = THIS_MODULE;
8653d138fccSMark Brown 	dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
8663d138fccSMark Brown 	dcdc->desc.enable_mask = 1 << dcdc->desc.id;
8678267a9baSMark Brown 
868c172708dSMark Brown 	config.dev = pdev->dev.parent;
869b7ca8788SMark Brown 	if (pdata)
870c172708dSMark Brown 		config.init_data = pdata->epe[id];
871c172708dSMark Brown 	config.driver_data = dcdc;
8723d138fccSMark Brown 	config.regmap = wm831x->regmap;
873c172708dSMark Brown 
874d73b4cb7SMark Brown 	dcdc->regulator = devm_regulator_register(&pdev->dev, &dcdc->desc,
875d73b4cb7SMark Brown 						  &config);
8768267a9baSMark Brown 	if (IS_ERR(dcdc->regulator)) {
8778267a9baSMark Brown 		ret = PTR_ERR(dcdc->regulator);
8788267a9baSMark Brown 		dev_err(wm831x->dev, "Failed to register EPE%d: %d\n",
8798267a9baSMark Brown 			id + 1, ret);
8808267a9baSMark Brown 		goto err;
8818267a9baSMark Brown 	}
8828267a9baSMark Brown 
8838267a9baSMark Brown 	platform_set_drvdata(pdev, dcdc);
8848267a9baSMark Brown 
8858267a9baSMark Brown 	return 0;
8868267a9baSMark Brown 
8878267a9baSMark Brown err:
8888267a9baSMark Brown 	return ret;
8898267a9baSMark Brown }
8908267a9baSMark Brown 
8918267a9baSMark Brown static struct platform_driver wm831x_epe_driver = {
8928267a9baSMark Brown 	.probe = wm831x_epe_probe,
8938267a9baSMark Brown 	.driver		= {
8948267a9baSMark Brown 		.name	= "wm831x-epe",
895eb66d565SDmitry Torokhov 		.owner	= THIS_MODULE,
8968267a9baSMark Brown 	},
8978267a9baSMark Brown };
8988267a9baSMark Brown 
899e4ee831fSMark Brown static int __init wm831x_dcdc_init(void)
900e4ee831fSMark Brown {
901e4ee831fSMark Brown 	int ret;
902e4ee831fSMark Brown 	ret = platform_driver_register(&wm831x_buckv_driver);
903e4ee831fSMark Brown 	if (ret != 0)
904e4ee831fSMark Brown 		pr_err("Failed to register WM831x BUCKV driver: %d\n", ret);
905e4ee831fSMark Brown 
906e4ee831fSMark Brown 	ret = platform_driver_register(&wm831x_buckp_driver);
907e4ee831fSMark Brown 	if (ret != 0)
908e4ee831fSMark Brown 		pr_err("Failed to register WM831x BUCKP driver: %d\n", ret);
909e4ee831fSMark Brown 
9101304850dSMark Brown 	ret = platform_driver_register(&wm831x_boostp_driver);
9111304850dSMark Brown 	if (ret != 0)
9121304850dSMark Brown 		pr_err("Failed to register WM831x BOOST driver: %d\n", ret);
9131304850dSMark Brown 
9148267a9baSMark Brown 	ret = platform_driver_register(&wm831x_epe_driver);
9158267a9baSMark Brown 	if (ret != 0)
9168267a9baSMark Brown 		pr_err("Failed to register WM831x EPE driver: %d\n", ret);
9178267a9baSMark Brown 
918e4ee831fSMark Brown 	return 0;
919e4ee831fSMark Brown }
920e4ee831fSMark Brown subsys_initcall(wm831x_dcdc_init);
921e4ee831fSMark Brown 
922e4ee831fSMark Brown static void __exit wm831x_dcdc_exit(void)
923e4ee831fSMark Brown {
9248267a9baSMark Brown 	platform_driver_unregister(&wm831x_epe_driver);
9251304850dSMark Brown 	platform_driver_unregister(&wm831x_boostp_driver);
926e4ee831fSMark Brown 	platform_driver_unregister(&wm831x_buckp_driver);
927e4ee831fSMark Brown 	platform_driver_unregister(&wm831x_buckv_driver);
928e4ee831fSMark Brown }
929e4ee831fSMark Brown module_exit(wm831x_dcdc_exit);
930e4ee831fSMark Brown 
931e4ee831fSMark Brown /* Module information */
932e4ee831fSMark Brown MODULE_AUTHOR("Mark Brown");
933e4ee831fSMark Brown MODULE_DESCRIPTION("WM831x DC-DC convertor driver");
934e4ee831fSMark Brown MODULE_LICENSE("GPL");
935e4ee831fSMark Brown MODULE_ALIAS("platform:wm831x-buckv");
936e4ee831fSMark Brown MODULE_ALIAS("platform:wm831x-buckp");
93743f1f216SAxel Lin MODULE_ALIAS("platform:wm831x-boostp");
93824b43150SMark Brown MODULE_ALIAS("platform:wm831x-epe");
939