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