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 
38e4ee831fSMark Brown #define WM831X_DCDC_MAX_NAME 6
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];
53e4ee831fSMark Brown 	struct regulator_desc desc;
54e4ee831fSMark Brown 	int base;
55e4ee831fSMark Brown 	struct wm831x *wm831x;
56e4ee831fSMark Brown 	struct regulator_dev *regulator;
57e24a04c4SMark Brown 	int dvs_gpio;
58e24a04c4SMark Brown 	int dvs_gpio_state;
59e24a04c4SMark Brown 	int on_vsel;
60e24a04c4SMark Brown 	int dvs_vsel;
61e4ee831fSMark Brown };
62e4ee831fSMark Brown 
63e4ee831fSMark Brown static int wm831x_dcdc_is_enabled(struct regulator_dev *rdev)
64e4ee831fSMark Brown {
65e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
66e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
67e4ee831fSMark Brown 	int mask = 1 << rdev_get_id(rdev);
68e4ee831fSMark Brown 	int reg;
69e4ee831fSMark Brown 
70e4ee831fSMark Brown 	reg = wm831x_reg_read(wm831x, WM831X_DCDC_ENABLE);
71e4ee831fSMark Brown 	if (reg < 0)
72e4ee831fSMark Brown 		return reg;
73e4ee831fSMark Brown 
74e4ee831fSMark Brown 	if (reg & mask)
75e4ee831fSMark Brown 		return 1;
76e4ee831fSMark Brown 	else
77e4ee831fSMark Brown 		return 0;
78e4ee831fSMark Brown }
79e4ee831fSMark Brown 
80e4ee831fSMark Brown static int wm831x_dcdc_enable(struct regulator_dev *rdev)
81e4ee831fSMark Brown {
82e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
83e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
84e4ee831fSMark Brown 	int mask = 1 << rdev_get_id(rdev);
85e4ee831fSMark Brown 
86e4ee831fSMark Brown 	return wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE, mask, mask);
87e4ee831fSMark Brown }
88e4ee831fSMark Brown 
89e4ee831fSMark Brown static int wm831x_dcdc_disable(struct regulator_dev *rdev)
90e4ee831fSMark Brown {
91e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
92e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
93e4ee831fSMark Brown 	int mask = 1 << rdev_get_id(rdev);
94e4ee831fSMark Brown 
95e4ee831fSMark Brown 	return wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE, mask, 0);
96e4ee831fSMark Brown }
97e4ee831fSMark Brown 
98e4ee831fSMark Brown static unsigned int wm831x_dcdc_get_mode(struct regulator_dev *rdev)
99e4ee831fSMark Brown 
100e4ee831fSMark Brown {
101e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
102e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
103e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
104e4ee831fSMark Brown 	int val;
105e4ee831fSMark Brown 
106e4ee831fSMark Brown 	val = wm831x_reg_read(wm831x, reg);
107e4ee831fSMark Brown 	if (val < 0)
108e4ee831fSMark Brown 		return val;
109e4ee831fSMark Brown 
110e4ee831fSMark Brown 	val = (val & WM831X_DC1_ON_MODE_MASK) >> WM831X_DC1_ON_MODE_SHIFT;
111e4ee831fSMark Brown 
112e4ee831fSMark Brown 	switch (val) {
113e4ee831fSMark Brown 	case WM831X_DCDC_MODE_FAST:
114e4ee831fSMark Brown 		return REGULATOR_MODE_FAST;
115e4ee831fSMark Brown 	case WM831X_DCDC_MODE_NORMAL:
116e4ee831fSMark Brown 		return REGULATOR_MODE_NORMAL;
117e4ee831fSMark Brown 	case WM831X_DCDC_MODE_STANDBY:
118e4ee831fSMark Brown 		return REGULATOR_MODE_STANDBY;
119e4ee831fSMark Brown 	case WM831X_DCDC_MODE_IDLE:
120e4ee831fSMark Brown 		return REGULATOR_MODE_IDLE;
121e4ee831fSMark Brown 	default:
122e4ee831fSMark Brown 		BUG();
1239ee291a4SMark Brown 		return -EINVAL;
124e4ee831fSMark Brown 	}
125e4ee831fSMark Brown }
126e4ee831fSMark Brown 
127e4ee831fSMark Brown static int wm831x_dcdc_set_mode_int(struct wm831x *wm831x, int reg,
128e4ee831fSMark Brown 				    unsigned int mode)
129e4ee831fSMark Brown {
130e4ee831fSMark Brown 	int val;
131e4ee831fSMark Brown 
132e4ee831fSMark Brown 	switch (mode) {
133e4ee831fSMark Brown 	case REGULATOR_MODE_FAST:
134e4ee831fSMark Brown 		val = WM831X_DCDC_MODE_FAST;
135e4ee831fSMark Brown 		break;
136e4ee831fSMark Brown 	case REGULATOR_MODE_NORMAL:
137e4ee831fSMark Brown 		val = WM831X_DCDC_MODE_NORMAL;
138e4ee831fSMark Brown 		break;
139e4ee831fSMark Brown 	case REGULATOR_MODE_STANDBY:
140e4ee831fSMark Brown 		val = WM831X_DCDC_MODE_STANDBY;
141e4ee831fSMark Brown 		break;
142e4ee831fSMark Brown 	case REGULATOR_MODE_IDLE:
143e4ee831fSMark Brown 		val = WM831X_DCDC_MODE_IDLE;
144e4ee831fSMark Brown 		break;
145e4ee831fSMark Brown 	default:
146e4ee831fSMark Brown 		return -EINVAL;
147e4ee831fSMark Brown 	}
148e4ee831fSMark Brown 
149e4ee831fSMark Brown 	return wm831x_set_bits(wm831x, reg, WM831X_DC1_ON_MODE_MASK,
150e4ee831fSMark Brown 			       val << WM831X_DC1_ON_MODE_SHIFT);
151e4ee831fSMark Brown }
152e4ee831fSMark Brown 
153e4ee831fSMark Brown static int wm831x_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
154e4ee831fSMark Brown {
155e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
156e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
157e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
158e4ee831fSMark Brown 
159e4ee831fSMark Brown 	return wm831x_dcdc_set_mode_int(wm831x, reg, mode);
160e4ee831fSMark Brown }
161e4ee831fSMark Brown 
162e4ee831fSMark Brown static int wm831x_dcdc_set_suspend_mode(struct regulator_dev *rdev,
163e4ee831fSMark Brown 					unsigned int mode)
164e4ee831fSMark Brown {
165e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
166e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
167e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
168e4ee831fSMark Brown 
169e4ee831fSMark Brown 	return wm831x_dcdc_set_mode_int(wm831x, reg, mode);
170e4ee831fSMark Brown }
171e4ee831fSMark Brown 
172e4ee831fSMark Brown static int wm831x_dcdc_get_status(struct regulator_dev *rdev)
173e4ee831fSMark Brown {
174e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
175e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
176e4ee831fSMark Brown 	int ret;
177e4ee831fSMark Brown 
178e4ee831fSMark Brown 	/* First, check for errors */
179e4ee831fSMark Brown 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
180e4ee831fSMark Brown 	if (ret < 0)
181e4ee831fSMark Brown 		return ret;
182e4ee831fSMark Brown 
183e4ee831fSMark Brown 	if (ret & (1 << rdev_get_id(rdev))) {
184e4ee831fSMark Brown 		dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
185e4ee831fSMark Brown 			rdev_get_id(rdev) + 1);
186e4ee831fSMark Brown 		return REGULATOR_STATUS_ERROR;
187e4ee831fSMark Brown 	}
188e4ee831fSMark Brown 
189e4ee831fSMark Brown 	/* DCDC1 and DCDC2 can additionally detect high voltage/current */
190e4ee831fSMark Brown 	if (rdev_get_id(rdev) < 2) {
191e4ee831fSMark Brown 		if (ret & (WM831X_DC1_OV_STS << rdev_get_id(rdev))) {
192e4ee831fSMark Brown 			dev_dbg(wm831x->dev, "DCDC%d over voltage\n",
193e4ee831fSMark Brown 				rdev_get_id(rdev) + 1);
194e4ee831fSMark Brown 			return REGULATOR_STATUS_ERROR;
195e4ee831fSMark Brown 		}
196e4ee831fSMark Brown 
197e4ee831fSMark Brown 		if (ret & (WM831X_DC1_HC_STS << rdev_get_id(rdev))) {
198e4ee831fSMark Brown 			dev_dbg(wm831x->dev, "DCDC%d over current\n",
199e4ee831fSMark Brown 				rdev_get_id(rdev) + 1);
200e4ee831fSMark Brown 			return REGULATOR_STATUS_ERROR;
201e4ee831fSMark Brown 		}
202e4ee831fSMark Brown 	}
203e4ee831fSMark Brown 
204e4ee831fSMark Brown 	/* Is the regulator on? */
205e4ee831fSMark Brown 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
206e4ee831fSMark Brown 	if (ret < 0)
207e4ee831fSMark Brown 		return ret;
208e4ee831fSMark Brown 	if (!(ret & (1 << rdev_get_id(rdev))))
209e4ee831fSMark Brown 		return REGULATOR_STATUS_OFF;
210e4ee831fSMark Brown 
211e4ee831fSMark Brown 	/* TODO: When we handle hardware control modes so we can report the
212e4ee831fSMark Brown 	 * current mode. */
213e4ee831fSMark Brown 	return REGULATOR_STATUS_ON;
214e4ee831fSMark Brown }
215e4ee831fSMark Brown 
216e4ee831fSMark Brown static irqreturn_t wm831x_dcdc_uv_irq(int irq, void *data)
217e4ee831fSMark Brown {
218e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = data;
219e4ee831fSMark Brown 
220e4ee831fSMark Brown 	regulator_notifier_call_chain(dcdc->regulator,
221e4ee831fSMark Brown 				      REGULATOR_EVENT_UNDER_VOLTAGE,
222e4ee831fSMark Brown 				      NULL);
223e4ee831fSMark Brown 
224e4ee831fSMark Brown 	return IRQ_HANDLED;
225e4ee831fSMark Brown }
226e4ee831fSMark Brown 
227e4ee831fSMark Brown static irqreturn_t wm831x_dcdc_oc_irq(int irq, void *data)
228e4ee831fSMark Brown {
229e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = data;
230e4ee831fSMark Brown 
231e4ee831fSMark Brown 	regulator_notifier_call_chain(dcdc->regulator,
232e4ee831fSMark Brown 				      REGULATOR_EVENT_OVER_CURRENT,
233e4ee831fSMark Brown 				      NULL);
234e4ee831fSMark Brown 
235e4ee831fSMark Brown 	return IRQ_HANDLED;
236e4ee831fSMark Brown }
237e4ee831fSMark Brown 
238e4ee831fSMark Brown /*
239e4ee831fSMark Brown  * BUCKV specifics
240e4ee831fSMark Brown  */
241e4ee831fSMark Brown 
242e4ee831fSMark Brown static int wm831x_buckv_list_voltage(struct regulator_dev *rdev,
243e4ee831fSMark Brown 				      unsigned selector)
244e4ee831fSMark Brown {
245e4ee831fSMark Brown 	if (selector <= 0x8)
246e4ee831fSMark Brown 		return 600000;
247e4ee831fSMark Brown 	if (selector <= WM831X_BUCKV_MAX_SELECTOR)
248e4ee831fSMark Brown 		return 600000 + ((selector - 0x8) * 12500);
249e4ee831fSMark Brown 	return -EINVAL;
250e4ee831fSMark Brown }
251e4ee831fSMark Brown 
252e24a04c4SMark Brown static int wm831x_buckv_select_min_voltage(struct regulator_dev *rdev,
253e4ee831fSMark Brown 					   int min_uV, int max_uV)
254e4ee831fSMark Brown {
255e4ee831fSMark Brown 	u16 vsel;
256e4ee831fSMark Brown 
257e4ee831fSMark Brown 	if (min_uV < 600000)
258e4ee831fSMark Brown 		vsel = 0;
259e4ee831fSMark Brown 	else if (min_uV <= 1800000)
260e4ee831fSMark Brown 		vsel = ((min_uV - 600000) / 12500) + 8;
261e4ee831fSMark Brown 	else
262e4ee831fSMark Brown 		return -EINVAL;
263e4ee831fSMark Brown 
264e4ee831fSMark Brown 	if (wm831x_buckv_list_voltage(rdev, vsel) > max_uV)
265e4ee831fSMark Brown 		return -EINVAL;
266e4ee831fSMark Brown 
267e24a04c4SMark Brown 	return vsel;
268e24a04c4SMark Brown }
269e24a04c4SMark Brown 
270e24a04c4SMark Brown static int wm831x_buckv_select_max_voltage(struct regulator_dev *rdev,
271e24a04c4SMark Brown 					   int min_uV, int max_uV)
272e24a04c4SMark Brown {
273e24a04c4SMark Brown 	u16 vsel;
274e24a04c4SMark Brown 
275e24a04c4SMark Brown 	if (max_uV < 600000 || max_uV > 1800000)
276e24a04c4SMark Brown 		return -EINVAL;
277e24a04c4SMark Brown 
278e24a04c4SMark Brown 	vsel = ((max_uV - 600000) / 12500) + 8;
279e24a04c4SMark Brown 
280e24a04c4SMark Brown 	if (wm831x_buckv_list_voltage(rdev, vsel) < min_uV ||
281e24a04c4SMark Brown 	    wm831x_buckv_list_voltage(rdev, vsel) < max_uV)
282e24a04c4SMark Brown 		return -EINVAL;
283e24a04c4SMark Brown 
284e24a04c4SMark Brown 	return vsel;
285e24a04c4SMark Brown }
286e24a04c4SMark Brown 
287e24a04c4SMark Brown static int wm831x_buckv_set_dvs(struct regulator_dev *rdev, int state)
288e24a04c4SMark Brown {
289e24a04c4SMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
290e24a04c4SMark Brown 
291e24a04c4SMark Brown 	if (state == dcdc->dvs_gpio_state)
292e24a04c4SMark Brown 		return 0;
293e24a04c4SMark Brown 
294e24a04c4SMark Brown 	dcdc->dvs_gpio_state = state;
295e24a04c4SMark Brown 	gpio_set_value(dcdc->dvs_gpio, state);
296e24a04c4SMark Brown 
297e24a04c4SMark Brown 	/* Should wait for DVS state change to be asserted if we have
298e24a04c4SMark Brown 	 * a GPIO for it, for now assume the device is configured
299e24a04c4SMark Brown 	 * for the fastest possible transition.
300e24a04c4SMark Brown 	 */
301e24a04c4SMark Brown 
302e24a04c4SMark Brown 	return 0;
303e4ee831fSMark Brown }
304e4ee831fSMark Brown 
305e4ee831fSMark Brown static int wm831x_buckv_set_voltage(struct regulator_dev *rdev,
3063a93f2a9SMark Brown 				    int min_uV, int max_uV, unsigned *selector)
307e4ee831fSMark Brown {
308e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
309e24a04c4SMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
310e24a04c4SMark Brown 	int on_reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
311e24a04c4SMark Brown 	int dvs_reg = dcdc->base + WM831X_DCDC_DVS_CONTROL;
312e24a04c4SMark Brown 	int vsel, ret;
313e4ee831fSMark Brown 
314e24a04c4SMark Brown 	vsel = wm831x_buckv_select_min_voltage(rdev, min_uV, max_uV);
315e24a04c4SMark Brown 	if (vsel < 0)
316e24a04c4SMark Brown 		return vsel;
317e24a04c4SMark Brown 
3183a93f2a9SMark Brown 	*selector = vsel;
3193a93f2a9SMark Brown 
320e24a04c4SMark Brown 	/* If this value is already set then do a GPIO update if we can */
321e24a04c4SMark Brown 	if (dcdc->dvs_gpio && dcdc->on_vsel == vsel)
322e24a04c4SMark Brown 		return wm831x_buckv_set_dvs(rdev, 0);
323e24a04c4SMark Brown 
324e24a04c4SMark Brown 	if (dcdc->dvs_gpio && dcdc->dvs_vsel == vsel)
325e24a04c4SMark Brown 		return wm831x_buckv_set_dvs(rdev, 1);
326e24a04c4SMark Brown 
327e24a04c4SMark Brown 	/* Always set the ON status to the minimum voltage */
328e24a04c4SMark Brown 	ret = wm831x_set_bits(wm831x, on_reg, WM831X_DC1_ON_VSEL_MASK, vsel);
329e24a04c4SMark Brown 	if (ret < 0)
330e24a04c4SMark Brown 		return ret;
331e24a04c4SMark Brown 	dcdc->on_vsel = vsel;
332e24a04c4SMark Brown 
333e24a04c4SMark Brown 	if (!dcdc->dvs_gpio)
334e24a04c4SMark Brown 		return ret;
335e24a04c4SMark Brown 
336e24a04c4SMark Brown 	/* Kick the voltage transition now */
337e24a04c4SMark Brown 	ret = wm831x_buckv_set_dvs(rdev, 0);
338e24a04c4SMark Brown 	if (ret < 0)
339e24a04c4SMark Brown 		return ret;
340e24a04c4SMark Brown 
341e24a04c4SMark Brown 	/* Set the high voltage as the DVS voltage.  This is optimised
342e24a04c4SMark Brown 	 * for CPUfreq usage, most processors will keep the maximum
343e24a04c4SMark Brown 	 * voltage constant and lower the minimum with the frequency. */
344e24a04c4SMark Brown 	vsel = wm831x_buckv_select_max_voltage(rdev, min_uV, max_uV);
345e24a04c4SMark Brown 	if (vsel < 0) {
346e24a04c4SMark Brown 		/* This should never happen - at worst the same vsel
347e24a04c4SMark Brown 		 * should be chosen */
348e24a04c4SMark Brown 		WARN_ON(vsel < 0);
349e24a04c4SMark Brown 		return 0;
350e24a04c4SMark Brown 	}
351e24a04c4SMark Brown 
352e24a04c4SMark Brown 	/* Don't bother if it's the same VSEL we're already using */
353e24a04c4SMark Brown 	if (vsel == dcdc->on_vsel)
354e24a04c4SMark Brown 		return 0;
355e24a04c4SMark Brown 
356e24a04c4SMark Brown 	ret = wm831x_set_bits(wm831x, dvs_reg, WM831X_DC1_DVS_VSEL_MASK, vsel);
357e24a04c4SMark Brown 	if (ret == 0)
358e24a04c4SMark Brown 		dcdc->dvs_vsel = vsel;
359e24a04c4SMark Brown 	else
360e24a04c4SMark Brown 		dev_warn(wm831x->dev, "Failed to set DCDC DVS VSEL: %d\n",
361e24a04c4SMark Brown 			 ret);
362e24a04c4SMark Brown 
363e24a04c4SMark Brown 	return 0;
364e4ee831fSMark Brown }
365e4ee831fSMark Brown 
366e4ee831fSMark Brown static int wm831x_buckv_set_suspend_voltage(struct regulator_dev *rdev,
367e4ee831fSMark Brown 					    int uV)
368e4ee831fSMark Brown {
369e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
370e24a04c4SMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
371e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
372e24a04c4SMark Brown 	int vsel;
373e4ee831fSMark Brown 
374e24a04c4SMark Brown 	vsel = wm831x_buckv_select_min_voltage(rdev, uV, uV);
375e24a04c4SMark Brown 	if (vsel < 0)
376e24a04c4SMark Brown 		return vsel;
377e24a04c4SMark Brown 
378e24a04c4SMark Brown 	return wm831x_set_bits(wm831x, reg, WM831X_DC1_SLP_VSEL_MASK, vsel);
379e4ee831fSMark Brown }
380e4ee831fSMark Brown 
381afb8bb80SMark Brown static int wm831x_buckv_get_voltage_sel(struct regulator_dev *rdev)
382e4ee831fSMark Brown {
383e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
384e4ee831fSMark Brown 
385e24a04c4SMark Brown 	if (dcdc->dvs_gpio && dcdc->dvs_gpio_state)
386afb8bb80SMark Brown 		return dcdc->dvs_vsel;
387e24a04c4SMark Brown 	else
388afb8bb80SMark Brown 		return dcdc->on_vsel;
389e4ee831fSMark Brown }
390e4ee831fSMark Brown 
391e4ee831fSMark Brown /* Current limit options */
392e4ee831fSMark Brown static u16 wm831x_dcdc_ilim[] = {
393e4ee831fSMark Brown 	125, 250, 375, 500, 625, 750, 875, 1000
394e4ee831fSMark Brown };
395e4ee831fSMark Brown 
396e4ee831fSMark Brown static int wm831x_buckv_set_current_limit(struct regulator_dev *rdev,
397e4ee831fSMark Brown 					   int min_uA, int max_uA)
398e4ee831fSMark Brown {
399e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
400e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
401e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2;
402e4ee831fSMark Brown 	int i;
403e4ee831fSMark Brown 
404e4ee831fSMark Brown 	for (i = 0; i < ARRAY_SIZE(wm831x_dcdc_ilim); i++) {
405e4ee831fSMark Brown 		if (max_uA <= wm831x_dcdc_ilim[i])
406e4ee831fSMark Brown 			break;
407e4ee831fSMark Brown 	}
408e4ee831fSMark Brown 	if (i == ARRAY_SIZE(wm831x_dcdc_ilim))
409e4ee831fSMark Brown 		return -EINVAL;
410e4ee831fSMark Brown 
411e4ee831fSMark Brown 	return wm831x_set_bits(wm831x, reg, WM831X_DC1_HC_THR_MASK, i);
412e4ee831fSMark Brown }
413e4ee831fSMark Brown 
414e4ee831fSMark Brown static int wm831x_buckv_get_current_limit(struct regulator_dev *rdev)
415e4ee831fSMark Brown {
416e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
417e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
418e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2;
419e4ee831fSMark Brown 	int val;
420e4ee831fSMark Brown 
421e4ee831fSMark Brown 	val = wm831x_reg_read(wm831x, reg);
422e4ee831fSMark Brown 	if (val < 0)
423e4ee831fSMark Brown 		return val;
424e4ee831fSMark Brown 
425e4ee831fSMark Brown 	return wm831x_dcdc_ilim[val & WM831X_DC1_HC_THR_MASK];
426e4ee831fSMark Brown }
427e4ee831fSMark Brown 
428e4ee831fSMark Brown static struct regulator_ops wm831x_buckv_ops = {
429e4ee831fSMark Brown 	.set_voltage = wm831x_buckv_set_voltage,
430afb8bb80SMark Brown 	.get_voltage_sel = wm831x_buckv_get_voltage_sel,
431e4ee831fSMark Brown 	.list_voltage = wm831x_buckv_list_voltage,
432e4ee831fSMark Brown 	.set_suspend_voltage = wm831x_buckv_set_suspend_voltage,
433e4ee831fSMark Brown 	.set_current_limit = wm831x_buckv_set_current_limit,
434e4ee831fSMark Brown 	.get_current_limit = wm831x_buckv_get_current_limit,
435e4ee831fSMark Brown 
436e4ee831fSMark Brown 	.is_enabled = wm831x_dcdc_is_enabled,
437e4ee831fSMark Brown 	.enable = wm831x_dcdc_enable,
438e4ee831fSMark Brown 	.disable = wm831x_dcdc_disable,
439e4ee831fSMark Brown 	.get_status = wm831x_dcdc_get_status,
440e4ee831fSMark Brown 	.get_mode = wm831x_dcdc_get_mode,
441e4ee831fSMark Brown 	.set_mode = wm831x_dcdc_set_mode,
442e4ee831fSMark Brown 	.set_suspend_mode = wm831x_dcdc_set_suspend_mode,
443e4ee831fSMark Brown };
444e4ee831fSMark Brown 
445e24a04c4SMark Brown /*
446e24a04c4SMark Brown  * Set up DVS control.  We just log errors since we can still run
447e24a04c4SMark Brown  * (with reduced performance) if we fail.
448e24a04c4SMark Brown  */
449e24a04c4SMark Brown static __devinit void wm831x_buckv_dvs_init(struct wm831x_dcdc *dcdc,
450e24a04c4SMark Brown 					    struct wm831x_buckv_pdata *pdata)
451e24a04c4SMark Brown {
452e24a04c4SMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
453e24a04c4SMark Brown 	int ret;
454e24a04c4SMark Brown 	u16 ctrl;
455e24a04c4SMark Brown 
456e24a04c4SMark Brown 	if (!pdata || !pdata->dvs_gpio)
457e24a04c4SMark Brown 		return;
458e24a04c4SMark Brown 
459e24a04c4SMark Brown 	switch (pdata->dvs_control_src) {
460e24a04c4SMark Brown 	case 1:
461e24a04c4SMark Brown 		ctrl = 2 << WM831X_DC1_DVS_SRC_SHIFT;
462e24a04c4SMark Brown 		break;
463e24a04c4SMark Brown 	case 2:
464e24a04c4SMark Brown 		ctrl = 3 << WM831X_DC1_DVS_SRC_SHIFT;
465e24a04c4SMark Brown 		break;
466e24a04c4SMark Brown 	default:
467e24a04c4SMark Brown 		dev_err(wm831x->dev, "Invalid DVS control source %d for %s\n",
468e24a04c4SMark Brown 			pdata->dvs_control_src, dcdc->name);
469e24a04c4SMark Brown 		return;
470e24a04c4SMark Brown 	}
471e24a04c4SMark Brown 
472e24a04c4SMark Brown 	ret = wm831x_set_bits(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL,
473e24a04c4SMark Brown 			      WM831X_DC1_DVS_SRC_MASK, ctrl);
474e24a04c4SMark Brown 	if (ret < 0) {
475e24a04c4SMark Brown 		dev_err(wm831x->dev, "Failed to set %s DVS source: %d\n",
476e24a04c4SMark Brown 			dcdc->name, ret);
477e24a04c4SMark Brown 		return;
478e24a04c4SMark Brown 	}
479e24a04c4SMark Brown 
480e24a04c4SMark Brown 	ret = gpio_request(pdata->dvs_gpio, "DCDC DVS");
481e24a04c4SMark Brown 	if (ret < 0) {
482e24a04c4SMark Brown 		dev_err(wm831x->dev, "Failed to get %s DVS GPIO: %d\n",
483e24a04c4SMark Brown 			dcdc->name, ret);
484e24a04c4SMark Brown 		return;
485e24a04c4SMark Brown 	}
486e24a04c4SMark Brown 
487e24a04c4SMark Brown 	/* gpiolib won't let us read the GPIO status so pick the higher
488e24a04c4SMark Brown 	 * of the two existing voltages so we take it as platform data.
489e24a04c4SMark Brown 	 */
490e24a04c4SMark Brown 	dcdc->dvs_gpio_state = pdata->dvs_init_state;
491e24a04c4SMark Brown 
492e24a04c4SMark Brown 	ret = gpio_direction_output(pdata->dvs_gpio, dcdc->dvs_gpio_state);
493e24a04c4SMark Brown 	if (ret < 0) {
494e24a04c4SMark Brown 		dev_err(wm831x->dev, "Failed to enable %s DVS GPIO: %d\n",
495e24a04c4SMark Brown 			dcdc->name, ret);
496e24a04c4SMark Brown 		gpio_free(pdata->dvs_gpio);
497e24a04c4SMark Brown 		return;
498e24a04c4SMark Brown 	}
499e24a04c4SMark Brown 
500e24a04c4SMark Brown 	dcdc->dvs_gpio = pdata->dvs_gpio;
501e24a04c4SMark Brown }
502e24a04c4SMark Brown 
503e4ee831fSMark Brown static __devinit int wm831x_buckv_probe(struct platform_device *pdev)
504e4ee831fSMark Brown {
505e4ee831fSMark Brown 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
506e4ee831fSMark Brown 	struct wm831x_pdata *pdata = wm831x->dev->platform_data;
507137a6354SMark Brown 	int id;
508e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc;
509e4ee831fSMark Brown 	struct resource *res;
510e4ee831fSMark Brown 	int ret, irq;
511e4ee831fSMark Brown 
512137a6354SMark Brown 	if (pdata && pdata->wm831x_num)
513137a6354SMark Brown 		id = (pdata->wm831x_num * 10) + 1;
514137a6354SMark Brown 	else
515137a6354SMark Brown 		id = 0;
516137a6354SMark Brown 	id = pdev->id - id;
517137a6354SMark Brown 
518e4ee831fSMark Brown 	dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
519e4ee831fSMark Brown 
520e4ee831fSMark Brown 	if (pdata == NULL || pdata->dcdc[id] == NULL)
521e4ee831fSMark Brown 		return -ENODEV;
522e4ee831fSMark Brown 
523e4ee831fSMark Brown 	dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
524e4ee831fSMark Brown 	if (dcdc == NULL) {
525e4ee831fSMark Brown 		dev_err(&pdev->dev, "Unable to allocate private data\n");
526e4ee831fSMark Brown 		return -ENOMEM;
527e4ee831fSMark Brown 	}
528e4ee831fSMark Brown 
529e4ee831fSMark Brown 	dcdc->wm831x = wm831x;
530e4ee831fSMark Brown 
531e4ee831fSMark Brown 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
532e4ee831fSMark Brown 	if (res == NULL) {
533e4ee831fSMark Brown 		dev_err(&pdev->dev, "No I/O resource\n");
534e4ee831fSMark Brown 		ret = -EINVAL;
535e4ee831fSMark Brown 		goto err;
536e4ee831fSMark Brown 	}
537e4ee831fSMark Brown 	dcdc->base = res->start;
538e4ee831fSMark Brown 
539e4ee831fSMark Brown 	snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
540e4ee831fSMark Brown 	dcdc->desc.name = dcdc->name;
541e4ee831fSMark Brown 	dcdc->desc.id = id;
542e4ee831fSMark Brown 	dcdc->desc.type = REGULATOR_VOLTAGE;
543e4ee831fSMark Brown 	dcdc->desc.n_voltages = WM831X_BUCKV_MAX_SELECTOR + 1;
544e4ee831fSMark Brown 	dcdc->desc.ops = &wm831x_buckv_ops;
545e4ee831fSMark Brown 	dcdc->desc.owner = THIS_MODULE;
546e4ee831fSMark Brown 
547e24a04c4SMark Brown 	ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG);
548e24a04c4SMark Brown 	if (ret < 0) {
549e24a04c4SMark Brown 		dev_err(wm831x->dev, "Failed to read ON VSEL: %d\n", ret);
550e24a04c4SMark Brown 		goto err;
551e24a04c4SMark Brown 	}
552e24a04c4SMark Brown 	dcdc->on_vsel = ret & WM831X_DC1_ON_VSEL_MASK;
553e24a04c4SMark Brown 
554a1b81dd3SMark Brown 	ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL);
555e24a04c4SMark Brown 	if (ret < 0) {
556e24a04c4SMark Brown 		dev_err(wm831x->dev, "Failed to read DVS VSEL: %d\n", ret);
557e24a04c4SMark Brown 		goto err;
558e24a04c4SMark Brown 	}
559e24a04c4SMark Brown 	dcdc->dvs_vsel = ret & WM831X_DC1_DVS_VSEL_MASK;
560e24a04c4SMark Brown 
561e24a04c4SMark Brown 	if (pdata->dcdc[id])
562e24a04c4SMark Brown 		wm831x_buckv_dvs_init(dcdc, pdata->dcdc[id]->driver_data);
563e24a04c4SMark Brown 
564e4ee831fSMark Brown 	dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
565e4ee831fSMark Brown 					     pdata->dcdc[id], dcdc);
566e4ee831fSMark Brown 	if (IS_ERR(dcdc->regulator)) {
567e4ee831fSMark Brown 		ret = PTR_ERR(dcdc->regulator);
568e4ee831fSMark Brown 		dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
569e4ee831fSMark Brown 			id + 1, ret);
570e4ee831fSMark Brown 		goto err;
571e4ee831fSMark Brown 	}
572e4ee831fSMark Brown 
573e4ee831fSMark Brown 	irq = platform_get_irq_byname(pdev, "UV");
574dfda9c27SMark Brown 	ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq,
575dfda9c27SMark Brown 				   IRQF_TRIGGER_RISING, dcdc->name, dcdc);
576e4ee831fSMark Brown 	if (ret != 0) {
577e4ee831fSMark Brown 		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
578e4ee831fSMark Brown 			irq, ret);
579e4ee831fSMark Brown 		goto err_regulator;
580e4ee831fSMark Brown 	}
581e4ee831fSMark Brown 
582e4ee831fSMark Brown 	irq = platform_get_irq_byname(pdev, "HC");
583dfda9c27SMark Brown 	ret = request_threaded_irq(irq, NULL, wm831x_dcdc_oc_irq,
584dfda9c27SMark Brown 				   IRQF_TRIGGER_RISING, dcdc->name, dcdc);
585e4ee831fSMark Brown 	if (ret != 0) {
586e4ee831fSMark Brown 		dev_err(&pdev->dev, "Failed to request HC IRQ %d: %d\n",
587e4ee831fSMark Brown 			irq, ret);
588e4ee831fSMark Brown 		goto err_uv;
589e4ee831fSMark Brown 	}
590e4ee831fSMark Brown 
591e4ee831fSMark Brown 	platform_set_drvdata(pdev, dcdc);
592e4ee831fSMark Brown 
593e4ee831fSMark Brown 	return 0;
594e4ee831fSMark Brown 
595e4ee831fSMark Brown err_uv:
596dfda9c27SMark Brown 	free_irq(platform_get_irq_byname(pdev, "UV"), dcdc);
597e4ee831fSMark Brown err_regulator:
598e4ee831fSMark Brown 	regulator_unregister(dcdc->regulator);
599e4ee831fSMark Brown err:
600e24a04c4SMark Brown 	if (dcdc->dvs_gpio)
601e24a04c4SMark Brown 		gpio_free(dcdc->dvs_gpio);
602e4ee831fSMark Brown 	kfree(dcdc);
603e4ee831fSMark Brown 	return ret;
604e4ee831fSMark Brown }
605e4ee831fSMark Brown 
606e4ee831fSMark Brown static __devexit int wm831x_buckv_remove(struct platform_device *pdev)
607e4ee831fSMark Brown {
608e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
609e4ee831fSMark Brown 
610eb66d565SDmitry Torokhov 	platform_set_drvdata(pdev, NULL);
611eb66d565SDmitry Torokhov 
61269952369SMark Brown 	free_irq(platform_get_irq_byname(pdev, "HC"), dcdc);
61369952369SMark Brown 	free_irq(platform_get_irq_byname(pdev, "UV"), dcdc);
614e4ee831fSMark Brown 	regulator_unregister(dcdc->regulator);
615e24a04c4SMark Brown 	if (dcdc->dvs_gpio)
616e24a04c4SMark Brown 		gpio_free(dcdc->dvs_gpio);
617e4ee831fSMark Brown 	kfree(dcdc);
618e4ee831fSMark Brown 
619e4ee831fSMark Brown 	return 0;
620e4ee831fSMark Brown }
621e4ee831fSMark Brown 
622e4ee831fSMark Brown static struct platform_driver wm831x_buckv_driver = {
623e4ee831fSMark Brown 	.probe = wm831x_buckv_probe,
624e4ee831fSMark Brown 	.remove = __devexit_p(wm831x_buckv_remove),
625e4ee831fSMark Brown 	.driver		= {
626e4ee831fSMark Brown 		.name	= "wm831x-buckv",
627eb66d565SDmitry Torokhov 		.owner	= THIS_MODULE,
628e4ee831fSMark Brown 	},
629e4ee831fSMark Brown };
630e4ee831fSMark Brown 
631e4ee831fSMark Brown /*
632e4ee831fSMark Brown  * BUCKP specifics
633e4ee831fSMark Brown  */
634e4ee831fSMark Brown 
635e4ee831fSMark Brown static int wm831x_buckp_list_voltage(struct regulator_dev *rdev,
636e4ee831fSMark Brown 				      unsigned selector)
637e4ee831fSMark Brown {
638e4ee831fSMark Brown 	if (selector <= WM831X_BUCKP_MAX_SELECTOR)
639e4ee831fSMark Brown 		return 850000 + (selector * 25000);
640e4ee831fSMark Brown 	else
641e4ee831fSMark Brown 		return -EINVAL;
642e4ee831fSMark Brown }
643e4ee831fSMark Brown 
644e4ee831fSMark Brown static int wm831x_buckp_set_voltage_int(struct regulator_dev *rdev, int reg,
6453a93f2a9SMark Brown 					int min_uV, int max_uV, int *selector)
646e4ee831fSMark Brown {
647e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
648e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
649e4ee831fSMark Brown 	u16 vsel;
650e4ee831fSMark Brown 
651e4ee831fSMark Brown 	if (min_uV <= 34000000)
652e4ee831fSMark Brown 		vsel = (min_uV - 850000) / 25000;
653e4ee831fSMark Brown 	else
654e4ee831fSMark Brown 		return -EINVAL;
655e4ee831fSMark Brown 
656e4ee831fSMark Brown 	if (wm831x_buckp_list_voltage(rdev, vsel) > max_uV)
657e4ee831fSMark Brown 		return -EINVAL;
658e4ee831fSMark Brown 
6593a93f2a9SMark Brown 	*selector = vsel;
6603a93f2a9SMark Brown 
661e4ee831fSMark Brown 	return wm831x_set_bits(wm831x, reg, WM831X_DC3_ON_VSEL_MASK, vsel);
662e4ee831fSMark Brown }
663e4ee831fSMark Brown 
664e4ee831fSMark Brown static int wm831x_buckp_set_voltage(struct regulator_dev *rdev,
6653a93f2a9SMark Brown 				    int min_uV, int max_uV,
6663a93f2a9SMark Brown 				    unsigned *selector)
667e4ee831fSMark Brown {
668e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
669e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
670e4ee831fSMark Brown 
6713a93f2a9SMark Brown 	return wm831x_buckp_set_voltage_int(rdev, reg, min_uV, max_uV,
6723a93f2a9SMark Brown 					    selector);
673e4ee831fSMark Brown }
674e4ee831fSMark Brown 
675e4ee831fSMark Brown static int wm831x_buckp_set_suspend_voltage(struct regulator_dev *rdev,
676e4ee831fSMark Brown 					    int uV)
677e4ee831fSMark Brown {
678e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
679e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
6803a93f2a9SMark Brown 	unsigned selector;
681e4ee831fSMark Brown 
6823a93f2a9SMark Brown 	return wm831x_buckp_set_voltage_int(rdev, reg, uV, uV, &selector);
683e4ee831fSMark Brown }
684e4ee831fSMark Brown 
685afb8bb80SMark Brown static int wm831x_buckp_get_voltage_sel(struct regulator_dev *rdev)
686e4ee831fSMark Brown {
687e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
688e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
689e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
690e4ee831fSMark Brown 	int val;
691e4ee831fSMark Brown 
692e4ee831fSMark Brown 	val = wm831x_reg_read(wm831x, reg);
693e4ee831fSMark Brown 	if (val < 0)
694e4ee831fSMark Brown 		return val;
695e4ee831fSMark Brown 
696afb8bb80SMark Brown 	return val & WM831X_DC3_ON_VSEL_MASK;
697e4ee831fSMark Brown }
698e4ee831fSMark Brown 
699e4ee831fSMark Brown static struct regulator_ops wm831x_buckp_ops = {
700e4ee831fSMark Brown 	.set_voltage = wm831x_buckp_set_voltage,
701afb8bb80SMark Brown 	.get_voltage_sel = wm831x_buckp_get_voltage_sel,
702e4ee831fSMark Brown 	.list_voltage = wm831x_buckp_list_voltage,
703e4ee831fSMark Brown 	.set_suspend_voltage = wm831x_buckp_set_suspend_voltage,
704e4ee831fSMark Brown 
705e4ee831fSMark Brown 	.is_enabled = wm831x_dcdc_is_enabled,
706e4ee831fSMark Brown 	.enable = wm831x_dcdc_enable,
707e4ee831fSMark Brown 	.disable = wm831x_dcdc_disable,
708e4ee831fSMark Brown 	.get_status = wm831x_dcdc_get_status,
709e4ee831fSMark Brown 	.get_mode = wm831x_dcdc_get_mode,
710e4ee831fSMark Brown 	.set_mode = wm831x_dcdc_set_mode,
711e4ee831fSMark Brown 	.set_suspend_mode = wm831x_dcdc_set_suspend_mode,
712e4ee831fSMark Brown };
713e4ee831fSMark Brown 
714e4ee831fSMark Brown static __devinit int wm831x_buckp_probe(struct platform_device *pdev)
715e4ee831fSMark Brown {
716e4ee831fSMark Brown 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
717e4ee831fSMark Brown 	struct wm831x_pdata *pdata = wm831x->dev->platform_data;
718137a6354SMark Brown 	int id;
719e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc;
720e4ee831fSMark Brown 	struct resource *res;
721e4ee831fSMark Brown 	int ret, irq;
722e4ee831fSMark Brown 
723137a6354SMark Brown 	if (pdata && pdata->wm831x_num)
724137a6354SMark Brown 		id = (pdata->wm831x_num * 10) + 1;
725137a6354SMark Brown 	else
726137a6354SMark Brown 		id = 0;
727137a6354SMark Brown 	id = pdev->id - id;
728137a6354SMark Brown 
729e4ee831fSMark Brown 	dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
730e4ee831fSMark Brown 
731e4ee831fSMark Brown 	if (pdata == NULL || pdata->dcdc[id] == NULL)
732e4ee831fSMark Brown 		return -ENODEV;
733e4ee831fSMark Brown 
734e4ee831fSMark Brown 	dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
735e4ee831fSMark Brown 	if (dcdc == NULL) {
736e4ee831fSMark Brown 		dev_err(&pdev->dev, "Unable to allocate private data\n");
737e4ee831fSMark Brown 		return -ENOMEM;
738e4ee831fSMark Brown 	}
739e4ee831fSMark Brown 
740e4ee831fSMark Brown 	dcdc->wm831x = wm831x;
741e4ee831fSMark Brown 
742e4ee831fSMark Brown 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
743e4ee831fSMark Brown 	if (res == NULL) {
744e4ee831fSMark Brown 		dev_err(&pdev->dev, "No I/O resource\n");
745e4ee831fSMark Brown 		ret = -EINVAL;
746e4ee831fSMark Brown 		goto err;
747e4ee831fSMark Brown 	}
748e4ee831fSMark Brown 	dcdc->base = res->start;
749e4ee831fSMark Brown 
750e4ee831fSMark Brown 	snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
751e4ee831fSMark Brown 	dcdc->desc.name = dcdc->name;
752e4ee831fSMark Brown 	dcdc->desc.id = id;
753e4ee831fSMark Brown 	dcdc->desc.type = REGULATOR_VOLTAGE;
754e4ee831fSMark Brown 	dcdc->desc.n_voltages = WM831X_BUCKP_MAX_SELECTOR + 1;
755e4ee831fSMark Brown 	dcdc->desc.ops = &wm831x_buckp_ops;
756e4ee831fSMark Brown 	dcdc->desc.owner = THIS_MODULE;
757e4ee831fSMark Brown 
758e4ee831fSMark Brown 	dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
759e4ee831fSMark Brown 					     pdata->dcdc[id], dcdc);
760e4ee831fSMark Brown 	if (IS_ERR(dcdc->regulator)) {
761e4ee831fSMark Brown 		ret = PTR_ERR(dcdc->regulator);
762e4ee831fSMark Brown 		dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
763e4ee831fSMark Brown 			id + 1, ret);
764e4ee831fSMark Brown 		goto err;
765e4ee831fSMark Brown 	}
766e4ee831fSMark Brown 
767e4ee831fSMark Brown 	irq = platform_get_irq_byname(pdev, "UV");
768dfda9c27SMark Brown 	ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq,
769dfda9c27SMark Brown 				   IRQF_TRIGGER_RISING,	dcdc->name, dcdc);
770e4ee831fSMark Brown 	if (ret != 0) {
771e4ee831fSMark Brown 		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
772e4ee831fSMark Brown 			irq, ret);
773e4ee831fSMark Brown 		goto err_regulator;
774e4ee831fSMark Brown 	}
775e4ee831fSMark Brown 
776e4ee831fSMark Brown 	platform_set_drvdata(pdev, dcdc);
777e4ee831fSMark Brown 
778e4ee831fSMark Brown 	return 0;
779e4ee831fSMark Brown 
780e4ee831fSMark Brown err_regulator:
781e4ee831fSMark Brown 	regulator_unregister(dcdc->regulator);
782e4ee831fSMark Brown err:
783e4ee831fSMark Brown 	kfree(dcdc);
784e4ee831fSMark Brown 	return ret;
785e4ee831fSMark Brown }
786e4ee831fSMark Brown 
787e4ee831fSMark Brown static __devexit int wm831x_buckp_remove(struct platform_device *pdev)
788e4ee831fSMark Brown {
789e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
790e4ee831fSMark Brown 
791eb66d565SDmitry Torokhov 	platform_set_drvdata(pdev, NULL);
792eb66d565SDmitry Torokhov 
79369952369SMark Brown 	free_irq(platform_get_irq_byname(pdev, "UV"), dcdc);
794e4ee831fSMark Brown 	regulator_unregister(dcdc->regulator);
795e4ee831fSMark Brown 	kfree(dcdc);
796e4ee831fSMark Brown 
797e4ee831fSMark Brown 	return 0;
798e4ee831fSMark Brown }
799e4ee831fSMark Brown 
800e4ee831fSMark Brown static struct platform_driver wm831x_buckp_driver = {
801e4ee831fSMark Brown 	.probe = wm831x_buckp_probe,
802e4ee831fSMark Brown 	.remove = __devexit_p(wm831x_buckp_remove),
803e4ee831fSMark Brown 	.driver		= {
804e4ee831fSMark Brown 		.name	= "wm831x-buckp",
805eb66d565SDmitry Torokhov 		.owner	= THIS_MODULE,
806e4ee831fSMark Brown 	},
807e4ee831fSMark Brown };
808e4ee831fSMark Brown 
8098267a9baSMark Brown /*
8101304850dSMark Brown  * DCDC boost convertors
8111304850dSMark Brown  */
8121304850dSMark Brown 
8131304850dSMark Brown static int wm831x_boostp_get_status(struct regulator_dev *rdev)
8141304850dSMark Brown {
8151304850dSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
8161304850dSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
8171304850dSMark Brown 	int ret;
8181304850dSMark Brown 
8191304850dSMark Brown 	/* First, check for errors */
8201304850dSMark Brown 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
8211304850dSMark Brown 	if (ret < 0)
8221304850dSMark Brown 		return ret;
8231304850dSMark Brown 
8241304850dSMark Brown 	if (ret & (1 << rdev_get_id(rdev))) {
8251304850dSMark Brown 		dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
8261304850dSMark Brown 			rdev_get_id(rdev) + 1);
8271304850dSMark Brown 		return REGULATOR_STATUS_ERROR;
8281304850dSMark Brown 	}
8291304850dSMark Brown 
8301304850dSMark Brown 	/* Is the regulator on? */
8311304850dSMark Brown 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
8321304850dSMark Brown 	if (ret < 0)
8331304850dSMark Brown 		return ret;
8341304850dSMark Brown 	if (ret & (1 << rdev_get_id(rdev)))
8351304850dSMark Brown 		return REGULATOR_STATUS_ON;
8361304850dSMark Brown 	else
8371304850dSMark Brown 		return REGULATOR_STATUS_OFF;
8381304850dSMark Brown }
8391304850dSMark Brown 
8401304850dSMark Brown static struct regulator_ops wm831x_boostp_ops = {
8411304850dSMark Brown 	.get_status = wm831x_boostp_get_status,
8421304850dSMark Brown 
8431304850dSMark Brown 	.is_enabled = wm831x_dcdc_is_enabled,
8441304850dSMark Brown 	.enable = wm831x_dcdc_enable,
8451304850dSMark Brown 	.disable = wm831x_dcdc_disable,
8461304850dSMark Brown };
8471304850dSMark Brown 
8481304850dSMark Brown static __devinit int wm831x_boostp_probe(struct platform_device *pdev)
8491304850dSMark Brown {
8501304850dSMark Brown 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
8511304850dSMark Brown 	struct wm831x_pdata *pdata = wm831x->dev->platform_data;
8521304850dSMark Brown 	int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
8531304850dSMark Brown 	struct wm831x_dcdc *dcdc;
8541304850dSMark Brown 	struct resource *res;
8551304850dSMark Brown 	int ret, irq;
8561304850dSMark Brown 
8571304850dSMark Brown 	dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
8581304850dSMark Brown 
8591304850dSMark Brown 	if (pdata == NULL || pdata->dcdc[id] == NULL)
8601304850dSMark Brown 		return -ENODEV;
8611304850dSMark Brown 
8621304850dSMark Brown 	dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
8631304850dSMark Brown 	if (dcdc == NULL) {
8641304850dSMark Brown 		dev_err(&pdev->dev, "Unable to allocate private data\n");
8651304850dSMark Brown 		return -ENOMEM;
8661304850dSMark Brown 	}
8671304850dSMark Brown 
8681304850dSMark Brown 	dcdc->wm831x = wm831x;
8691304850dSMark Brown 
8701304850dSMark Brown 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
8711304850dSMark Brown 	if (res == NULL) {
8721304850dSMark Brown 		dev_err(&pdev->dev, "No I/O resource\n");
8731304850dSMark Brown 		ret = -EINVAL;
8741304850dSMark Brown 		goto err;
8751304850dSMark Brown 	}
8761304850dSMark Brown 	dcdc->base = res->start;
8771304850dSMark Brown 
8781304850dSMark Brown 	snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
8791304850dSMark Brown 	dcdc->desc.name = dcdc->name;
8801304850dSMark Brown 	dcdc->desc.id = id;
8811304850dSMark Brown 	dcdc->desc.type = REGULATOR_VOLTAGE;
8821304850dSMark Brown 	dcdc->desc.ops = &wm831x_boostp_ops;
8831304850dSMark Brown 	dcdc->desc.owner = THIS_MODULE;
8841304850dSMark Brown 
8851304850dSMark Brown 	dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
8861304850dSMark Brown 					     pdata->dcdc[id], dcdc);
8871304850dSMark Brown 	if (IS_ERR(dcdc->regulator)) {
8881304850dSMark Brown 		ret = PTR_ERR(dcdc->regulator);
8891304850dSMark Brown 		dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
8901304850dSMark Brown 			id + 1, ret);
8911304850dSMark Brown 		goto err;
8921304850dSMark Brown 	}
8931304850dSMark Brown 
8941304850dSMark Brown 	irq = platform_get_irq_byname(pdev, "UV");
895dfda9c27SMark Brown 	ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq,
8961304850dSMark Brown 				   IRQF_TRIGGER_RISING, dcdc->name,
8971304850dSMark Brown 				   dcdc);
8981304850dSMark Brown 	if (ret != 0) {
8991304850dSMark Brown 		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
9001304850dSMark Brown 			irq, ret);
9011304850dSMark Brown 		goto err_regulator;
9021304850dSMark Brown 	}
9031304850dSMark Brown 
9041304850dSMark Brown 	platform_set_drvdata(pdev, dcdc);
9051304850dSMark Brown 
9061304850dSMark Brown 	return 0;
9071304850dSMark Brown 
9081304850dSMark Brown err_regulator:
9091304850dSMark Brown 	regulator_unregister(dcdc->regulator);
9101304850dSMark Brown err:
9111304850dSMark Brown 	kfree(dcdc);
9121304850dSMark Brown 	return ret;
9131304850dSMark Brown }
9141304850dSMark Brown 
9151304850dSMark Brown static __devexit int wm831x_boostp_remove(struct platform_device *pdev)
9161304850dSMark Brown {
9171304850dSMark Brown 	struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
9181304850dSMark Brown 
919eb66d565SDmitry Torokhov 	platform_set_drvdata(pdev, NULL);
920eb66d565SDmitry Torokhov 
921dfda9c27SMark Brown 	free_irq(platform_get_irq_byname(pdev, "UV"), dcdc);
9221304850dSMark Brown 	regulator_unregister(dcdc->regulator);
9231304850dSMark Brown 	kfree(dcdc);
9241304850dSMark Brown 
9251304850dSMark Brown 	return 0;
9261304850dSMark Brown }
9271304850dSMark Brown 
9281304850dSMark Brown static struct platform_driver wm831x_boostp_driver = {
9291304850dSMark Brown 	.probe = wm831x_boostp_probe,
9301304850dSMark Brown 	.remove = __devexit_p(wm831x_boostp_remove),
9311304850dSMark Brown 	.driver		= {
9321304850dSMark Brown 		.name	= "wm831x-boostp",
933eb66d565SDmitry Torokhov 		.owner	= THIS_MODULE,
9341304850dSMark Brown 	},
9351304850dSMark Brown };
9361304850dSMark Brown 
9371304850dSMark Brown /*
9388267a9baSMark Brown  * External Power Enable
9398267a9baSMark Brown  *
9408267a9baSMark Brown  * These aren't actually DCDCs but look like them in hardware so share
9418267a9baSMark Brown  * code.
9428267a9baSMark Brown  */
9438267a9baSMark Brown 
9448267a9baSMark Brown #define WM831X_EPE_BASE 6
9458267a9baSMark Brown 
9468267a9baSMark Brown static struct regulator_ops wm831x_epe_ops = {
9478267a9baSMark Brown 	.is_enabled = wm831x_dcdc_is_enabled,
9488267a9baSMark Brown 	.enable = wm831x_dcdc_enable,
9498267a9baSMark Brown 	.disable = wm831x_dcdc_disable,
9508267a9baSMark Brown 	.get_status = wm831x_dcdc_get_status,
9518267a9baSMark Brown };
9528267a9baSMark Brown 
9538267a9baSMark Brown static __devinit int wm831x_epe_probe(struct platform_device *pdev)
9548267a9baSMark Brown {
9558267a9baSMark Brown 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
9568267a9baSMark Brown 	struct wm831x_pdata *pdata = wm831x->dev->platform_data;
9578267a9baSMark Brown 	int id = pdev->id % ARRAY_SIZE(pdata->epe);
9588267a9baSMark Brown 	struct wm831x_dcdc *dcdc;
9598267a9baSMark Brown 	int ret;
9608267a9baSMark Brown 
9618267a9baSMark Brown 	dev_dbg(&pdev->dev, "Probing EPE%d\n", id + 1);
9628267a9baSMark Brown 
9638267a9baSMark Brown 	if (pdata == NULL || pdata->epe[id] == NULL)
9648267a9baSMark Brown 		return -ENODEV;
9658267a9baSMark Brown 
9668267a9baSMark Brown 	dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
9678267a9baSMark Brown 	if (dcdc == NULL) {
9688267a9baSMark Brown 		dev_err(&pdev->dev, "Unable to allocate private data\n");
9698267a9baSMark Brown 		return -ENOMEM;
9708267a9baSMark Brown 	}
9718267a9baSMark Brown 
9728267a9baSMark Brown 	dcdc->wm831x = wm831x;
9738267a9baSMark Brown 
9748267a9baSMark Brown 	/* For current parts this is correct; probably need to revisit
9758267a9baSMark Brown 	 * in future.
9768267a9baSMark Brown 	 */
9778267a9baSMark Brown 	snprintf(dcdc->name, sizeof(dcdc->name), "EPE%d", id + 1);
9788267a9baSMark Brown 	dcdc->desc.name = dcdc->name;
9798267a9baSMark Brown 	dcdc->desc.id = id + WM831X_EPE_BASE; /* Offset in DCDC registers */
9808267a9baSMark Brown 	dcdc->desc.ops = &wm831x_epe_ops;
9818267a9baSMark Brown 	dcdc->desc.type = REGULATOR_VOLTAGE;
9828267a9baSMark Brown 	dcdc->desc.owner = THIS_MODULE;
9838267a9baSMark Brown 
9848267a9baSMark Brown 	dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
9858267a9baSMark Brown 					     pdata->epe[id], dcdc);
9868267a9baSMark Brown 	if (IS_ERR(dcdc->regulator)) {
9878267a9baSMark Brown 		ret = PTR_ERR(dcdc->regulator);
9888267a9baSMark Brown 		dev_err(wm831x->dev, "Failed to register EPE%d: %d\n",
9898267a9baSMark Brown 			id + 1, ret);
9908267a9baSMark Brown 		goto err;
9918267a9baSMark Brown 	}
9928267a9baSMark Brown 
9938267a9baSMark Brown 	platform_set_drvdata(pdev, dcdc);
9948267a9baSMark Brown 
9958267a9baSMark Brown 	return 0;
9968267a9baSMark Brown 
9978267a9baSMark Brown err:
9988267a9baSMark Brown 	kfree(dcdc);
9998267a9baSMark Brown 	return ret;
10008267a9baSMark Brown }
10018267a9baSMark Brown 
10028267a9baSMark Brown static __devexit int wm831x_epe_remove(struct platform_device *pdev)
10038267a9baSMark Brown {
10048267a9baSMark Brown 	struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
10058267a9baSMark Brown 
1006eb66d565SDmitry Torokhov 	platform_set_drvdata(pdev, NULL);
1007eb66d565SDmitry Torokhov 
10088267a9baSMark Brown 	regulator_unregister(dcdc->regulator);
10098267a9baSMark Brown 	kfree(dcdc);
10108267a9baSMark Brown 
10118267a9baSMark Brown 	return 0;
10128267a9baSMark Brown }
10138267a9baSMark Brown 
10148267a9baSMark Brown static struct platform_driver wm831x_epe_driver = {
10158267a9baSMark Brown 	.probe = wm831x_epe_probe,
10168267a9baSMark Brown 	.remove = __devexit_p(wm831x_epe_remove),
10178267a9baSMark Brown 	.driver		= {
10188267a9baSMark Brown 		.name	= "wm831x-epe",
1019eb66d565SDmitry Torokhov 		.owner	= THIS_MODULE,
10208267a9baSMark Brown 	},
10218267a9baSMark Brown };
10228267a9baSMark Brown 
1023e4ee831fSMark Brown static int __init wm831x_dcdc_init(void)
1024e4ee831fSMark Brown {
1025e4ee831fSMark Brown 	int ret;
1026e4ee831fSMark Brown 	ret = platform_driver_register(&wm831x_buckv_driver);
1027e4ee831fSMark Brown 	if (ret != 0)
1028e4ee831fSMark Brown 		pr_err("Failed to register WM831x BUCKV driver: %d\n", ret);
1029e4ee831fSMark Brown 
1030e4ee831fSMark Brown 	ret = platform_driver_register(&wm831x_buckp_driver);
1031e4ee831fSMark Brown 	if (ret != 0)
1032e4ee831fSMark Brown 		pr_err("Failed to register WM831x BUCKP driver: %d\n", ret);
1033e4ee831fSMark Brown 
10341304850dSMark Brown 	ret = platform_driver_register(&wm831x_boostp_driver);
10351304850dSMark Brown 	if (ret != 0)
10361304850dSMark Brown 		pr_err("Failed to register WM831x BOOST driver: %d\n", ret);
10371304850dSMark Brown 
10388267a9baSMark Brown 	ret = platform_driver_register(&wm831x_epe_driver);
10398267a9baSMark Brown 	if (ret != 0)
10408267a9baSMark Brown 		pr_err("Failed to register WM831x EPE driver: %d\n", ret);
10418267a9baSMark Brown 
1042e4ee831fSMark Brown 	return 0;
1043e4ee831fSMark Brown }
1044e4ee831fSMark Brown subsys_initcall(wm831x_dcdc_init);
1045e4ee831fSMark Brown 
1046e4ee831fSMark Brown static void __exit wm831x_dcdc_exit(void)
1047e4ee831fSMark Brown {
10488267a9baSMark Brown 	platform_driver_unregister(&wm831x_epe_driver);
10491304850dSMark Brown 	platform_driver_unregister(&wm831x_boostp_driver);
1050e4ee831fSMark Brown 	platform_driver_unregister(&wm831x_buckp_driver);
1051e4ee831fSMark Brown 	platform_driver_unregister(&wm831x_buckv_driver);
1052e4ee831fSMark Brown }
1053e4ee831fSMark Brown module_exit(wm831x_dcdc_exit);
1054e4ee831fSMark Brown 
1055e4ee831fSMark Brown /* Module information */
1056e4ee831fSMark Brown MODULE_AUTHOR("Mark Brown");
1057e4ee831fSMark Brown MODULE_DESCRIPTION("WM831x DC-DC convertor driver");
1058e4ee831fSMark Brown MODULE_LICENSE("GPL");
1059e4ee831fSMark Brown MODULE_ALIAS("platform:wm831x-buckv");
1060e4ee831fSMark Brown MODULE_ALIAS("platform:wm831x-buckp");
1061