1e4ee831fSMark Brown /*
2e4ee831fSMark Brown  * wm831x-dcdc.c  --  DC-DC buck convertor driver for the WM831x series
3e4ee831fSMark Brown  *
4e4ee831fSMark Brown  * Copyright 2009 Wolfson Microelectronics PLC.
5e4ee831fSMark Brown  *
6e4ee831fSMark Brown  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7e4ee831fSMark Brown  *
8e4ee831fSMark Brown  *  This program is free software; you can redistribute  it and/or modify it
9e4ee831fSMark Brown  *  under  the terms of  the GNU General  Public License as published by the
10e4ee831fSMark Brown  *  Free Software Foundation;  either version 2 of the  License, or (at your
11e4ee831fSMark Brown  *  option) any later version.
12e4ee831fSMark Brown  */
13e4ee831fSMark Brown 
14e4ee831fSMark Brown #include <linux/module.h>
15e4ee831fSMark Brown #include <linux/moduleparam.h>
16e4ee831fSMark Brown #include <linux/init.h>
17e4ee831fSMark Brown #include <linux/bitops.h>
18e4ee831fSMark Brown #include <linux/err.h>
19e4ee831fSMark Brown #include <linux/i2c.h>
20e4ee831fSMark Brown #include <linux/platform_device.h>
21e4ee831fSMark Brown #include <linux/regulator/driver.h>
22e24a04c4SMark Brown #include <linux/regulator/machine.h>
23e24a04c4SMark Brown #include <linux/gpio.h>
245a0e3ad6STejun Heo #include <linux/slab.h>
25e4ee831fSMark Brown 
26e4ee831fSMark Brown #include <linux/mfd/wm831x/core.h>
27e4ee831fSMark Brown #include <linux/mfd/wm831x/regulator.h>
28e4ee831fSMark Brown #include <linux/mfd/wm831x/pdata.h>
29e4ee831fSMark Brown 
30e4ee831fSMark Brown #define WM831X_BUCKV_MAX_SELECTOR 0x68
31e4ee831fSMark Brown #define WM831X_BUCKP_MAX_SELECTOR 0x66
32e4ee831fSMark Brown 
33e4ee831fSMark Brown #define WM831X_DCDC_MODE_FAST    0
34e4ee831fSMark Brown #define WM831X_DCDC_MODE_NORMAL  1
35e4ee831fSMark Brown #define WM831X_DCDC_MODE_IDLE    2
36e4ee831fSMark Brown #define WM831X_DCDC_MODE_STANDBY 3
37e4ee831fSMark Brown 
3882caa978SMark Brown #define WM831X_DCDC_MAX_NAME 9
39e4ee831fSMark Brown 
40e4ee831fSMark Brown /* Register offsets in control block */
41e4ee831fSMark Brown #define WM831X_DCDC_CONTROL_1     0
42e4ee831fSMark Brown #define WM831X_DCDC_CONTROL_2     1
43e4ee831fSMark Brown #define WM831X_DCDC_ON_CONFIG     2
44e4ee831fSMark Brown #define WM831X_DCDC_SLEEP_CONTROL 3
45e24a04c4SMark Brown #define WM831X_DCDC_DVS_CONTROL   4
46e4ee831fSMark Brown 
47e4ee831fSMark Brown /*
48e4ee831fSMark Brown  * Shared
49e4ee831fSMark Brown  */
50e4ee831fSMark Brown 
51e4ee831fSMark Brown struct wm831x_dcdc {
52e4ee831fSMark Brown 	char name[WM831X_DCDC_MAX_NAME];
5382caa978SMark Brown 	char supply_name[WM831X_DCDC_MAX_NAME];
54e4ee831fSMark Brown 	struct regulator_desc desc;
55e4ee831fSMark Brown 	int base;
56e4ee831fSMark Brown 	struct wm831x *wm831x;
57e4ee831fSMark Brown 	struct regulator_dev *regulator;
58e24a04c4SMark Brown 	int dvs_gpio;
59e24a04c4SMark Brown 	int dvs_gpio_state;
60e24a04c4SMark Brown 	int on_vsel;
61e24a04c4SMark Brown 	int dvs_vsel;
62e4ee831fSMark Brown };
63e4ee831fSMark Brown 
64e4ee831fSMark Brown static unsigned int wm831x_dcdc_get_mode(struct regulator_dev *rdev)
65e4ee831fSMark Brown 
66e4ee831fSMark Brown {
67e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
68e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
69e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
70e4ee831fSMark Brown 	int val;
71e4ee831fSMark Brown 
72e4ee831fSMark Brown 	val = wm831x_reg_read(wm831x, reg);
73e4ee831fSMark Brown 	if (val < 0)
74e4ee831fSMark Brown 		return val;
75e4ee831fSMark Brown 
76e4ee831fSMark Brown 	val = (val & WM831X_DC1_ON_MODE_MASK) >> WM831X_DC1_ON_MODE_SHIFT;
77e4ee831fSMark Brown 
78e4ee831fSMark Brown 	switch (val) {
79e4ee831fSMark Brown 	case WM831X_DCDC_MODE_FAST:
80e4ee831fSMark Brown 		return REGULATOR_MODE_FAST;
81e4ee831fSMark Brown 	case WM831X_DCDC_MODE_NORMAL:
82e4ee831fSMark Brown 		return REGULATOR_MODE_NORMAL;
83e4ee831fSMark Brown 	case WM831X_DCDC_MODE_STANDBY:
84e4ee831fSMark Brown 		return REGULATOR_MODE_STANDBY;
85e4ee831fSMark Brown 	case WM831X_DCDC_MODE_IDLE:
86e4ee831fSMark Brown 		return REGULATOR_MODE_IDLE;
87e4ee831fSMark Brown 	default:
88e4ee831fSMark Brown 		BUG();
899ee291a4SMark Brown 		return -EINVAL;
90e4ee831fSMark Brown 	}
91e4ee831fSMark Brown }
92e4ee831fSMark Brown 
93e4ee831fSMark Brown static int wm831x_dcdc_set_mode_int(struct wm831x *wm831x, int reg,
94e4ee831fSMark Brown 				    unsigned int mode)
95e4ee831fSMark Brown {
96e4ee831fSMark Brown 	int val;
97e4ee831fSMark Brown 
98e4ee831fSMark Brown 	switch (mode) {
99e4ee831fSMark Brown 	case REGULATOR_MODE_FAST:
100e4ee831fSMark Brown 		val = WM831X_DCDC_MODE_FAST;
101e4ee831fSMark Brown 		break;
102e4ee831fSMark Brown 	case REGULATOR_MODE_NORMAL:
103e4ee831fSMark Brown 		val = WM831X_DCDC_MODE_NORMAL;
104e4ee831fSMark Brown 		break;
105e4ee831fSMark Brown 	case REGULATOR_MODE_STANDBY:
106e4ee831fSMark Brown 		val = WM831X_DCDC_MODE_STANDBY;
107e4ee831fSMark Brown 		break;
108e4ee831fSMark Brown 	case REGULATOR_MODE_IDLE:
109e4ee831fSMark Brown 		val = WM831X_DCDC_MODE_IDLE;
110e4ee831fSMark Brown 		break;
111e4ee831fSMark Brown 	default:
112e4ee831fSMark Brown 		return -EINVAL;
113e4ee831fSMark Brown 	}
114e4ee831fSMark Brown 
115e4ee831fSMark Brown 	return wm831x_set_bits(wm831x, reg, WM831X_DC1_ON_MODE_MASK,
116e4ee831fSMark Brown 			       val << WM831X_DC1_ON_MODE_SHIFT);
117e4ee831fSMark Brown }
118e4ee831fSMark Brown 
119e4ee831fSMark Brown static int wm831x_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
120e4ee831fSMark Brown {
121e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
122e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
123e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
124e4ee831fSMark Brown 
125e4ee831fSMark Brown 	return wm831x_dcdc_set_mode_int(wm831x, reg, mode);
126e4ee831fSMark Brown }
127e4ee831fSMark Brown 
128e4ee831fSMark Brown static int wm831x_dcdc_set_suspend_mode(struct regulator_dev *rdev,
129e4ee831fSMark Brown 					unsigned int mode)
130e4ee831fSMark Brown {
131e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
132e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
133e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
134e4ee831fSMark Brown 
135e4ee831fSMark Brown 	return wm831x_dcdc_set_mode_int(wm831x, reg, mode);
136e4ee831fSMark Brown }
137e4ee831fSMark Brown 
138e4ee831fSMark Brown static int wm831x_dcdc_get_status(struct regulator_dev *rdev)
139e4ee831fSMark Brown {
140e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
141e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
142e4ee831fSMark Brown 	int ret;
143e4ee831fSMark Brown 
144e4ee831fSMark Brown 	/* First, check for errors */
145e4ee831fSMark Brown 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
146e4ee831fSMark Brown 	if (ret < 0)
147e4ee831fSMark Brown 		return ret;
148e4ee831fSMark Brown 
149e4ee831fSMark Brown 	if (ret & (1 << rdev_get_id(rdev))) {
150e4ee831fSMark Brown 		dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
151e4ee831fSMark Brown 			rdev_get_id(rdev) + 1);
152e4ee831fSMark Brown 		return REGULATOR_STATUS_ERROR;
153e4ee831fSMark Brown 	}
154e4ee831fSMark Brown 
155e4ee831fSMark Brown 	/* DCDC1 and DCDC2 can additionally detect high voltage/current */
156e4ee831fSMark Brown 	if (rdev_get_id(rdev) < 2) {
157e4ee831fSMark Brown 		if (ret & (WM831X_DC1_OV_STS << rdev_get_id(rdev))) {
158e4ee831fSMark Brown 			dev_dbg(wm831x->dev, "DCDC%d over voltage\n",
159e4ee831fSMark Brown 				rdev_get_id(rdev) + 1);
160e4ee831fSMark Brown 			return REGULATOR_STATUS_ERROR;
161e4ee831fSMark Brown 		}
162e4ee831fSMark Brown 
163e4ee831fSMark Brown 		if (ret & (WM831X_DC1_HC_STS << rdev_get_id(rdev))) {
164e4ee831fSMark Brown 			dev_dbg(wm831x->dev, "DCDC%d over current\n",
165e4ee831fSMark Brown 				rdev_get_id(rdev) + 1);
166e4ee831fSMark Brown 			return REGULATOR_STATUS_ERROR;
167e4ee831fSMark Brown 		}
168e4ee831fSMark Brown 	}
169e4ee831fSMark Brown 
170e4ee831fSMark Brown 	/* Is the regulator on? */
171e4ee831fSMark Brown 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
172e4ee831fSMark Brown 	if (ret < 0)
173e4ee831fSMark Brown 		return ret;
174e4ee831fSMark Brown 	if (!(ret & (1 << rdev_get_id(rdev))))
175e4ee831fSMark Brown 		return REGULATOR_STATUS_OFF;
176e4ee831fSMark Brown 
177e4ee831fSMark Brown 	/* TODO: When we handle hardware control modes so we can report the
178e4ee831fSMark Brown 	 * current mode. */
179e4ee831fSMark Brown 	return REGULATOR_STATUS_ON;
180e4ee831fSMark Brown }
181e4ee831fSMark Brown 
182e4ee831fSMark Brown static irqreturn_t wm831x_dcdc_uv_irq(int irq, void *data)
183e4ee831fSMark Brown {
184e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = data;
185e4ee831fSMark Brown 
186e4ee831fSMark Brown 	regulator_notifier_call_chain(dcdc->regulator,
187e4ee831fSMark Brown 				      REGULATOR_EVENT_UNDER_VOLTAGE,
188e4ee831fSMark Brown 				      NULL);
189e4ee831fSMark Brown 
190e4ee831fSMark Brown 	return IRQ_HANDLED;
191e4ee831fSMark Brown }
192e4ee831fSMark Brown 
193e4ee831fSMark Brown static irqreturn_t wm831x_dcdc_oc_irq(int irq, void *data)
194e4ee831fSMark Brown {
195e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = data;
196e4ee831fSMark Brown 
197e4ee831fSMark Brown 	regulator_notifier_call_chain(dcdc->regulator,
198e4ee831fSMark Brown 				      REGULATOR_EVENT_OVER_CURRENT,
199e4ee831fSMark Brown 				      NULL);
200e4ee831fSMark Brown 
201e4ee831fSMark Brown 	return IRQ_HANDLED;
202e4ee831fSMark Brown }
203e4ee831fSMark Brown 
204e4ee831fSMark Brown /*
205e4ee831fSMark Brown  * BUCKV specifics
206e4ee831fSMark Brown  */
207e4ee831fSMark Brown 
208e4ee831fSMark Brown static int wm831x_buckv_list_voltage(struct regulator_dev *rdev,
209e4ee831fSMark Brown 				      unsigned selector)
210e4ee831fSMark Brown {
211e4ee831fSMark Brown 	if (selector <= 0x8)
212e4ee831fSMark Brown 		return 600000;
213e4ee831fSMark Brown 	if (selector <= WM831X_BUCKV_MAX_SELECTOR)
214e4ee831fSMark Brown 		return 600000 + ((selector - 0x8) * 12500);
215e4ee831fSMark Brown 	return -EINVAL;
216e4ee831fSMark Brown }
217e4ee831fSMark Brown 
218b5fb77e0SAxel Lin static int wm831x_buckv_map_voltage(struct regulator_dev *rdev,
219e4ee831fSMark Brown 				   int min_uV, int max_uV)
220e4ee831fSMark Brown {
221e4ee831fSMark Brown 	u16 vsel;
222e4ee831fSMark Brown 
223e4ee831fSMark Brown 	if (min_uV < 600000)
224e4ee831fSMark Brown 		vsel = 0;
225e4ee831fSMark Brown 	else if (min_uV <= 1800000)
226e4ee831fSMark Brown 		vsel = ((min_uV - 600000) / 12500) + 8;
227e4ee831fSMark Brown 	else
228e4ee831fSMark Brown 		return -EINVAL;
229e4ee831fSMark Brown 
230e4ee831fSMark Brown 	if (wm831x_buckv_list_voltage(rdev, vsel) > max_uV)
231e4ee831fSMark Brown 		return -EINVAL;
232e4ee831fSMark Brown 
233e24a04c4SMark Brown 	return vsel;
234e24a04c4SMark Brown }
235e24a04c4SMark Brown 
236e24a04c4SMark Brown static int wm831x_buckv_set_dvs(struct regulator_dev *rdev, int state)
237e24a04c4SMark Brown {
238e24a04c4SMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
239e24a04c4SMark Brown 
240e24a04c4SMark Brown 	if (state == dcdc->dvs_gpio_state)
241e24a04c4SMark Brown 		return 0;
242e24a04c4SMark Brown 
243e24a04c4SMark Brown 	dcdc->dvs_gpio_state = state;
244e24a04c4SMark Brown 	gpio_set_value(dcdc->dvs_gpio, state);
245e24a04c4SMark Brown 
246e24a04c4SMark Brown 	/* Should wait for DVS state change to be asserted if we have
247e24a04c4SMark Brown 	 * a GPIO for it, for now assume the device is configured
248e24a04c4SMark Brown 	 * for the fastest possible transition.
249e24a04c4SMark Brown 	 */
250e24a04c4SMark Brown 
251e24a04c4SMark Brown 	return 0;
252e4ee831fSMark Brown }
253e4ee831fSMark Brown 
254b5fb77e0SAxel Lin static int wm831x_buckv_set_voltage_sel(struct regulator_dev *rdev,
255b5fb77e0SAxel Lin 					unsigned vsel)
256e4ee831fSMark Brown {
257e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
258e24a04c4SMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
259e24a04c4SMark Brown 	int on_reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
260e24a04c4SMark Brown 	int dvs_reg = dcdc->base + WM831X_DCDC_DVS_CONTROL;
261b5fb77e0SAxel Lin 	int ret;
2623a93f2a9SMark Brown 
263e24a04c4SMark Brown 	/* If this value is already set then do a GPIO update if we can */
264e24a04c4SMark Brown 	if (dcdc->dvs_gpio && dcdc->on_vsel == vsel)
265e24a04c4SMark Brown 		return wm831x_buckv_set_dvs(rdev, 0);
266e24a04c4SMark Brown 
267e24a04c4SMark Brown 	if (dcdc->dvs_gpio && dcdc->dvs_vsel == vsel)
268e24a04c4SMark Brown 		return wm831x_buckv_set_dvs(rdev, 1);
269e24a04c4SMark Brown 
270e24a04c4SMark Brown 	/* Always set the ON status to the minimum voltage */
271e24a04c4SMark Brown 	ret = wm831x_set_bits(wm831x, on_reg, WM831X_DC1_ON_VSEL_MASK, vsel);
272e24a04c4SMark Brown 	if (ret < 0)
273e24a04c4SMark Brown 		return ret;
274e24a04c4SMark Brown 	dcdc->on_vsel = vsel;
275e24a04c4SMark Brown 
276e24a04c4SMark Brown 	if (!dcdc->dvs_gpio)
277e24a04c4SMark Brown 		return ret;
278e24a04c4SMark Brown 
279e24a04c4SMark Brown 	/* Kick the voltage transition now */
280e24a04c4SMark Brown 	ret = wm831x_buckv_set_dvs(rdev, 0);
281e24a04c4SMark Brown 	if (ret < 0)
282e24a04c4SMark Brown 		return ret;
283e24a04c4SMark Brown 
28488cda60eSMark Brown 	/*
28588cda60eSMark Brown 	 * If this VSEL is higher than the last one we've seen then
28688cda60eSMark Brown 	 * remember it as the DVS VSEL.  This is optimised for CPUfreq
28788cda60eSMark Brown 	 * usage where we want to get to the highest voltage very
28888cda60eSMark Brown 	 * quickly.
28988cda60eSMark Brown 	 */
29088cda60eSMark Brown 	if (vsel > dcdc->dvs_vsel) {
29188cda60eSMark Brown 		ret = wm831x_set_bits(wm831x, dvs_reg,
29288cda60eSMark Brown 				      WM831X_DC1_DVS_VSEL_MASK,
29313ae633cSMark Brown 				      vsel);
294e24a04c4SMark Brown 		if (ret == 0)
295e24a04c4SMark Brown 			dcdc->dvs_vsel = vsel;
296e24a04c4SMark Brown 		else
29788cda60eSMark Brown 			dev_warn(wm831x->dev,
29888cda60eSMark Brown 				 "Failed to set DCDC DVS VSEL: %d\n", ret);
29988cda60eSMark Brown 	}
300e24a04c4SMark Brown 
301e24a04c4SMark Brown 	return 0;
302e4ee831fSMark Brown }
303e4ee831fSMark Brown 
304e4ee831fSMark Brown static int wm831x_buckv_set_suspend_voltage(struct regulator_dev *rdev,
305e4ee831fSMark Brown 					    int uV)
306e4ee831fSMark Brown {
307e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
308e24a04c4SMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
309e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
310e24a04c4SMark Brown 	int vsel;
311e4ee831fSMark Brown 
312b5fb77e0SAxel Lin 	vsel = wm831x_buckv_map_voltage(rdev, uV, uV);
313e24a04c4SMark Brown 	if (vsel < 0)
314e24a04c4SMark Brown 		return vsel;
315e24a04c4SMark Brown 
316e24a04c4SMark Brown 	return wm831x_set_bits(wm831x, reg, WM831X_DC1_SLP_VSEL_MASK, vsel);
317e4ee831fSMark Brown }
318e4ee831fSMark Brown 
319afb8bb80SMark Brown static int wm831x_buckv_get_voltage_sel(struct regulator_dev *rdev)
320e4ee831fSMark Brown {
321e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
322e4ee831fSMark Brown 
323e24a04c4SMark Brown 	if (dcdc->dvs_gpio && dcdc->dvs_gpio_state)
324afb8bb80SMark Brown 		return dcdc->dvs_vsel;
325e24a04c4SMark Brown 	else
326afb8bb80SMark Brown 		return dcdc->on_vsel;
327e4ee831fSMark Brown }
328e4ee831fSMark Brown 
329e4ee831fSMark Brown /* Current limit options */
330e4ee831fSMark Brown static u16 wm831x_dcdc_ilim[] = {
331e4ee831fSMark Brown 	125, 250, 375, 500, 625, 750, 875, 1000
332e4ee831fSMark Brown };
333e4ee831fSMark Brown 
334e4ee831fSMark Brown static int wm831x_buckv_set_current_limit(struct regulator_dev *rdev,
335e4ee831fSMark Brown 					   int min_uA, int max_uA)
336e4ee831fSMark Brown {
337e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
338e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
339e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2;
340e4ee831fSMark Brown 	int i;
341e4ee831fSMark Brown 
342422294deSAxel Lin 	for (i = ARRAY_SIZE(wm831x_dcdc_ilim) - 1; i >= 0; i--) {
343a171e782SAxel Lin 		if ((min_uA <= wm831x_dcdc_ilim[i]) &&
344a171e782SAxel Lin 		    (wm831x_dcdc_ilim[i] <= max_uA))
345422294deSAxel Lin 			return wm831x_set_bits(wm831x, reg,
346422294deSAxel Lin 					       WM831X_DC1_HC_THR_MASK,
34709bf14b9SAxel Lin 						i << WM831X_DC1_HC_THR_SHIFT);
348e4ee831fSMark Brown 	}
349e4ee831fSMark Brown 
350422294deSAxel Lin 	return -EINVAL;
351422294deSAxel Lin }
352422294deSAxel Lin 
353e4ee831fSMark Brown static int wm831x_buckv_get_current_limit(struct regulator_dev *rdev)
354e4ee831fSMark Brown {
355e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
356e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
357e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2;
358e4ee831fSMark Brown 	int val;
359e4ee831fSMark Brown 
360e4ee831fSMark Brown 	val = wm831x_reg_read(wm831x, reg);
361e4ee831fSMark Brown 	if (val < 0)
362e4ee831fSMark Brown 		return val;
363e4ee831fSMark Brown 
36409bf14b9SAxel Lin 	val = (val & WM831X_DC1_HC_THR_MASK) >> WM831X_DC1_HC_THR_SHIFT;
36509bf14b9SAxel Lin 	return wm831x_dcdc_ilim[val];
366e4ee831fSMark Brown }
367e4ee831fSMark Brown 
368e4ee831fSMark Brown static struct regulator_ops wm831x_buckv_ops = {
369b5fb77e0SAxel Lin 	.set_voltage_sel = wm831x_buckv_set_voltage_sel,
370afb8bb80SMark Brown 	.get_voltage_sel = wm831x_buckv_get_voltage_sel,
371e4ee831fSMark Brown 	.list_voltage = wm831x_buckv_list_voltage,
372b5fb77e0SAxel Lin 	.map_voltage = wm831x_buckv_map_voltage,
373e4ee831fSMark Brown 	.set_suspend_voltage = wm831x_buckv_set_suspend_voltage,
374e4ee831fSMark Brown 	.set_current_limit = wm831x_buckv_set_current_limit,
375e4ee831fSMark Brown 	.get_current_limit = wm831x_buckv_get_current_limit,
376e4ee831fSMark Brown 
3773d138fccSMark Brown 	.is_enabled = regulator_is_enabled_regmap,
3783d138fccSMark Brown 	.enable = regulator_enable_regmap,
3793d138fccSMark Brown 	.disable = regulator_disable_regmap,
380e4ee831fSMark Brown 	.get_status = wm831x_dcdc_get_status,
381e4ee831fSMark Brown 	.get_mode = wm831x_dcdc_get_mode,
382e4ee831fSMark Brown 	.set_mode = wm831x_dcdc_set_mode,
383e4ee831fSMark Brown 	.set_suspend_mode = wm831x_dcdc_set_suspend_mode,
384e4ee831fSMark Brown };
385e4ee831fSMark Brown 
386e24a04c4SMark Brown /*
387e24a04c4SMark Brown  * Set up DVS control.  We just log errors since we can still run
388e24a04c4SMark Brown  * (with reduced performance) if we fail.
389e24a04c4SMark Brown  */
390e24a04c4SMark Brown static __devinit void wm831x_buckv_dvs_init(struct wm831x_dcdc *dcdc,
391e24a04c4SMark Brown 					    struct wm831x_buckv_pdata *pdata)
392e24a04c4SMark Brown {
393e24a04c4SMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
394e24a04c4SMark Brown 	int ret;
395e24a04c4SMark Brown 	u16 ctrl;
396e24a04c4SMark Brown 
397e24a04c4SMark Brown 	if (!pdata || !pdata->dvs_gpio)
398e24a04c4SMark Brown 		return;
399e24a04c4SMark Brown 
400e24a04c4SMark Brown 	/* gpiolib won't let us read the GPIO status so pick the higher
401e24a04c4SMark Brown 	 * of the two existing voltages so we take it as platform data.
402e24a04c4SMark Brown 	 */
403e24a04c4SMark Brown 	dcdc->dvs_gpio_state = pdata->dvs_init_state;
404e24a04c4SMark Brown 
405053fa1b7SMark Brown 	ret = gpio_request_one(pdata->dvs_gpio,
406053fa1b7SMark Brown 			       dcdc->dvs_gpio_state ? GPIOF_INIT_HIGH : 0,
407053fa1b7SMark Brown 			       "DCDC DVS");
408e24a04c4SMark Brown 	if (ret < 0) {
409053fa1b7SMark Brown 		dev_err(wm831x->dev, "Failed to get %s DVS GPIO: %d\n",
410e24a04c4SMark Brown 			dcdc->name, ret);
411e24a04c4SMark Brown 		return;
412e24a04c4SMark Brown 	}
413e24a04c4SMark Brown 
414e24a04c4SMark Brown 	dcdc->dvs_gpio = pdata->dvs_gpio;
415b47ba9fdSMark Brown 
416b47ba9fdSMark Brown 	switch (pdata->dvs_control_src) {
417b47ba9fdSMark Brown 	case 1:
418b47ba9fdSMark Brown 		ctrl = 2 << WM831X_DC1_DVS_SRC_SHIFT;
419b47ba9fdSMark Brown 		break;
420b47ba9fdSMark Brown 	case 2:
421b47ba9fdSMark Brown 		ctrl = 3 << WM831X_DC1_DVS_SRC_SHIFT;
422b47ba9fdSMark Brown 		break;
423b47ba9fdSMark Brown 	default:
424b47ba9fdSMark Brown 		dev_err(wm831x->dev, "Invalid DVS control source %d for %s\n",
425b47ba9fdSMark Brown 			pdata->dvs_control_src, dcdc->name);
426b47ba9fdSMark Brown 		return;
427b47ba9fdSMark Brown 	}
428b47ba9fdSMark Brown 
429c439b8f4SMark Brown 	/* If DVS_VSEL is set to the minimum value then raise it to ON_VSEL
430c439b8f4SMark Brown 	 * to make bootstrapping a bit smoother.
431c439b8f4SMark Brown 	 */
432c439b8f4SMark Brown 	if (!dcdc->dvs_vsel) {
433c439b8f4SMark Brown 		ret = wm831x_set_bits(wm831x,
434c439b8f4SMark Brown 				      dcdc->base + WM831X_DCDC_DVS_CONTROL,
435c439b8f4SMark Brown 				      WM831X_DC1_DVS_VSEL_MASK, dcdc->on_vsel);
436c439b8f4SMark Brown 		if (ret == 0)
437c439b8f4SMark Brown 			dcdc->dvs_vsel = dcdc->on_vsel;
438c439b8f4SMark Brown 		else
439c439b8f4SMark Brown 			dev_warn(wm831x->dev, "Failed to set DVS_VSEL: %d\n",
440c439b8f4SMark Brown 				 ret);
441c439b8f4SMark Brown 	}
442c439b8f4SMark Brown 
443b47ba9fdSMark Brown 	ret = wm831x_set_bits(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL,
444b47ba9fdSMark Brown 			      WM831X_DC1_DVS_SRC_MASK, ctrl);
445b47ba9fdSMark Brown 	if (ret < 0) {
446b47ba9fdSMark Brown 		dev_err(wm831x->dev, "Failed to set %s DVS source: %d\n",
447b47ba9fdSMark Brown 			dcdc->name, ret);
448b47ba9fdSMark Brown 	}
449e24a04c4SMark Brown }
450e24a04c4SMark Brown 
451e4ee831fSMark Brown static __devinit int wm831x_buckv_probe(struct platform_device *pdev)
452e4ee831fSMark Brown {
453e4ee831fSMark Brown 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
454e4ee831fSMark Brown 	struct wm831x_pdata *pdata = wm831x->dev->platform_data;
455c172708dSMark Brown 	struct regulator_config config = { };
456137a6354SMark Brown 	int id;
457e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc;
458e4ee831fSMark Brown 	struct resource *res;
459e4ee831fSMark Brown 	int ret, irq;
460e4ee831fSMark Brown 
461137a6354SMark Brown 	if (pdata && pdata->wm831x_num)
462137a6354SMark Brown 		id = (pdata->wm831x_num * 10) + 1;
463137a6354SMark Brown 	else
464137a6354SMark Brown 		id = 0;
465137a6354SMark Brown 	id = pdev->id - id;
466137a6354SMark Brown 
467e4ee831fSMark Brown 	dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
468e4ee831fSMark Brown 
469fded2f4fSMark Brown 	dcdc = devm_kzalloc(&pdev->dev,  sizeof(struct wm831x_dcdc),
470fded2f4fSMark Brown 			    GFP_KERNEL);
471e4ee831fSMark Brown 	if (dcdc == NULL) {
472e4ee831fSMark Brown 		dev_err(&pdev->dev, "Unable to allocate private data\n");
473e4ee831fSMark Brown 		return -ENOMEM;
474e4ee831fSMark Brown 	}
475e4ee831fSMark Brown 
476e4ee831fSMark Brown 	dcdc->wm831x = wm831x;
477e4ee831fSMark Brown 
4785656098eSMark Brown 	res = platform_get_resource(pdev, IORESOURCE_REG, 0);
479e4ee831fSMark Brown 	if (res == NULL) {
4805656098eSMark Brown 		dev_err(&pdev->dev, "No REG resource\n");
481e4ee831fSMark Brown 		ret = -EINVAL;
482e4ee831fSMark Brown 		goto err;
483e4ee831fSMark Brown 	}
484e4ee831fSMark Brown 	dcdc->base = res->start;
485e4ee831fSMark Brown 
486e4ee831fSMark Brown 	snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
487e4ee831fSMark Brown 	dcdc->desc.name = dcdc->name;
48882caa978SMark Brown 
48982caa978SMark Brown 	snprintf(dcdc->supply_name, sizeof(dcdc->supply_name),
49082caa978SMark Brown 		 "DC%dVDD", id + 1);
49182caa978SMark Brown 	dcdc->desc.supply_name = dcdc->supply_name;
49282caa978SMark Brown 
493e4ee831fSMark Brown 	dcdc->desc.id = id;
494e4ee831fSMark Brown 	dcdc->desc.type = REGULATOR_VOLTAGE;
495e4ee831fSMark Brown 	dcdc->desc.n_voltages = WM831X_BUCKV_MAX_SELECTOR + 1;
496e4ee831fSMark Brown 	dcdc->desc.ops = &wm831x_buckv_ops;
497e4ee831fSMark Brown 	dcdc->desc.owner = THIS_MODULE;
4983d138fccSMark Brown 	dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
4993d138fccSMark Brown 	dcdc->desc.enable_mask = 1 << id;
500e4ee831fSMark Brown 
501e24a04c4SMark Brown 	ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG);
502e24a04c4SMark Brown 	if (ret < 0) {
503e24a04c4SMark Brown 		dev_err(wm831x->dev, "Failed to read ON VSEL: %d\n", ret);
504e24a04c4SMark Brown 		goto err;
505e24a04c4SMark Brown 	}
506e24a04c4SMark Brown 	dcdc->on_vsel = ret & WM831X_DC1_ON_VSEL_MASK;
507e24a04c4SMark Brown 
508a1b81dd3SMark Brown 	ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL);
509e24a04c4SMark Brown 	if (ret < 0) {
510e24a04c4SMark Brown 		dev_err(wm831x->dev, "Failed to read DVS VSEL: %d\n", ret);
511e24a04c4SMark Brown 		goto err;
512e24a04c4SMark Brown 	}
513e24a04c4SMark Brown 	dcdc->dvs_vsel = ret & WM831X_DC1_DVS_VSEL_MASK;
514e24a04c4SMark Brown 
515f0b067d9SMark Brown 	if (pdata && pdata->dcdc[id])
516e24a04c4SMark Brown 		wm831x_buckv_dvs_init(dcdc, pdata->dcdc[id]->driver_data);
517e24a04c4SMark Brown 
518c172708dSMark Brown 	config.dev = pdev->dev.parent;
519b7ca8788SMark Brown 	if (pdata)
520c172708dSMark Brown 		config.init_data = pdata->dcdc[id];
521c172708dSMark Brown 	config.driver_data = dcdc;
5223d138fccSMark Brown 	config.regmap = wm831x->regmap;
523c172708dSMark Brown 
524c172708dSMark Brown 	dcdc->regulator = regulator_register(&dcdc->desc, &config);
525e4ee831fSMark Brown 	if (IS_ERR(dcdc->regulator)) {
526e4ee831fSMark Brown 		ret = PTR_ERR(dcdc->regulator);
527e4ee831fSMark Brown 		dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
528e4ee831fSMark Brown 			id + 1, ret);
529e4ee831fSMark Brown 		goto err;
530e4ee831fSMark Brown 	}
531e4ee831fSMark Brown 
532cd99758bSMark Brown 	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
533dfda9c27SMark Brown 	ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq,
534dfda9c27SMark Brown 				   IRQF_TRIGGER_RISING, dcdc->name, dcdc);
535e4ee831fSMark Brown 	if (ret != 0) {
536e4ee831fSMark Brown 		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
537e4ee831fSMark Brown 			irq, ret);
538e4ee831fSMark Brown 		goto err_regulator;
539e4ee831fSMark Brown 	}
540e4ee831fSMark Brown 
541cd99758bSMark Brown 	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "HC"));
542dfda9c27SMark Brown 	ret = request_threaded_irq(irq, NULL, wm831x_dcdc_oc_irq,
543dfda9c27SMark Brown 				   IRQF_TRIGGER_RISING, dcdc->name, dcdc);
544e4ee831fSMark Brown 	if (ret != 0) {
545e4ee831fSMark Brown 		dev_err(&pdev->dev, "Failed to request HC IRQ %d: %d\n",
546e4ee831fSMark Brown 			irq, ret);
547e4ee831fSMark Brown 		goto err_uv;
548e4ee831fSMark Brown 	}
549e4ee831fSMark Brown 
550e4ee831fSMark Brown 	platform_set_drvdata(pdev, dcdc);
551e4ee831fSMark Brown 
552e4ee831fSMark Brown 	return 0;
553e4ee831fSMark Brown 
554e4ee831fSMark Brown err_uv:
555cd99758bSMark Brown 	free_irq(wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV")),
556cd99758bSMark Brown 		 dcdc);
557e4ee831fSMark Brown err_regulator:
558e4ee831fSMark Brown 	regulator_unregister(dcdc->regulator);
559e4ee831fSMark Brown err:
560e24a04c4SMark Brown 	if (dcdc->dvs_gpio)
561e24a04c4SMark Brown 		gpio_free(dcdc->dvs_gpio);
562e4ee831fSMark Brown 	return ret;
563e4ee831fSMark Brown }
564e4ee831fSMark Brown 
565e4ee831fSMark Brown static __devexit int wm831x_buckv_remove(struct platform_device *pdev)
566e4ee831fSMark Brown {
567e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
568cd99758bSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
569e4ee831fSMark Brown 
570eb66d565SDmitry Torokhov 	platform_set_drvdata(pdev, NULL);
571eb66d565SDmitry Torokhov 
572cd99758bSMark Brown 	free_irq(wm831x_irq(wm831x, platform_get_irq_byname(pdev, "HC")),
573cd99758bSMark Brown 			    dcdc);
574cd99758bSMark Brown 	free_irq(wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV")),
575cd99758bSMark Brown 			    dcdc);
576e4ee831fSMark Brown 	regulator_unregister(dcdc->regulator);
577e24a04c4SMark Brown 	if (dcdc->dvs_gpio)
578e24a04c4SMark Brown 		gpio_free(dcdc->dvs_gpio);
579e4ee831fSMark Brown 
580e4ee831fSMark Brown 	return 0;
581e4ee831fSMark Brown }
582e4ee831fSMark Brown 
583e4ee831fSMark Brown static struct platform_driver wm831x_buckv_driver = {
584e4ee831fSMark Brown 	.probe = wm831x_buckv_probe,
585e4ee831fSMark Brown 	.remove = __devexit_p(wm831x_buckv_remove),
586e4ee831fSMark Brown 	.driver		= {
587e4ee831fSMark Brown 		.name	= "wm831x-buckv",
588eb66d565SDmitry Torokhov 		.owner	= THIS_MODULE,
589e4ee831fSMark Brown 	},
590e4ee831fSMark Brown };
591e4ee831fSMark Brown 
592e4ee831fSMark Brown /*
593e4ee831fSMark Brown  * BUCKP specifics
594e4ee831fSMark Brown  */
595e4ee831fSMark Brown 
596d580cb5eSAxel Lin static int wm831x_buckp_set_suspend_voltage(struct regulator_dev *rdev, int uV)
597e4ee831fSMark Brown {
598e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
599e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
600e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
601d580cb5eSAxel Lin 	int sel;
602e4ee831fSMark Brown 
603d580cb5eSAxel Lin 	sel = regulator_map_voltage_linear(rdev, uV, uV);
604d580cb5eSAxel Lin 	if (sel < 0)
605d580cb5eSAxel Lin 		return sel;
606d580cb5eSAxel Lin 
607d580cb5eSAxel Lin 	return wm831x_set_bits(wm831x, reg, WM831X_DC3_ON_VSEL_MASK, sel);
608e4ee831fSMark Brown }
609e4ee831fSMark Brown 
610e4ee831fSMark Brown static struct regulator_ops wm831x_buckp_ops = {
611d580cb5eSAxel Lin 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
612817436e7SMark Brown 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
613c70ad9dcSAxel Lin 	.list_voltage = regulator_list_voltage_linear,
614d580cb5eSAxel Lin 	.map_voltage = regulator_map_voltage_linear,
615e4ee831fSMark Brown 	.set_suspend_voltage = wm831x_buckp_set_suspend_voltage,
616e4ee831fSMark Brown 
6173d138fccSMark Brown 	.is_enabled = regulator_is_enabled_regmap,
6183d138fccSMark Brown 	.enable = regulator_enable_regmap,
6193d138fccSMark Brown 	.disable = regulator_disable_regmap,
620e4ee831fSMark Brown 	.get_status = wm831x_dcdc_get_status,
621e4ee831fSMark Brown 	.get_mode = wm831x_dcdc_get_mode,
622e4ee831fSMark Brown 	.set_mode = wm831x_dcdc_set_mode,
623e4ee831fSMark Brown 	.set_suspend_mode = wm831x_dcdc_set_suspend_mode,
624e4ee831fSMark Brown };
625e4ee831fSMark Brown 
626e4ee831fSMark Brown static __devinit int wm831x_buckp_probe(struct platform_device *pdev)
627e4ee831fSMark Brown {
628e4ee831fSMark Brown 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
629e4ee831fSMark Brown 	struct wm831x_pdata *pdata = wm831x->dev->platform_data;
630c172708dSMark Brown 	struct regulator_config config = { };
631137a6354SMark Brown 	int id;
632e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc;
633e4ee831fSMark Brown 	struct resource *res;
634e4ee831fSMark Brown 	int ret, irq;
635e4ee831fSMark Brown 
636137a6354SMark Brown 	if (pdata && pdata->wm831x_num)
637137a6354SMark Brown 		id = (pdata->wm831x_num * 10) + 1;
638137a6354SMark Brown 	else
639137a6354SMark Brown 		id = 0;
640137a6354SMark Brown 	id = pdev->id - id;
641137a6354SMark Brown 
642e4ee831fSMark Brown 	dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
643e4ee831fSMark Brown 
644fded2f4fSMark Brown 	dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc),
645fded2f4fSMark Brown 			    GFP_KERNEL);
646e4ee831fSMark Brown 	if (dcdc == NULL) {
647e4ee831fSMark Brown 		dev_err(&pdev->dev, "Unable to allocate private data\n");
648e4ee831fSMark Brown 		return -ENOMEM;
649e4ee831fSMark Brown 	}
650e4ee831fSMark Brown 
651e4ee831fSMark Brown 	dcdc->wm831x = wm831x;
652e4ee831fSMark Brown 
6535656098eSMark Brown 	res = platform_get_resource(pdev, IORESOURCE_REG, 0);
654e4ee831fSMark Brown 	if (res == NULL) {
6555656098eSMark Brown 		dev_err(&pdev->dev, "No REG resource\n");
656e4ee831fSMark Brown 		ret = -EINVAL;
657e4ee831fSMark Brown 		goto err;
658e4ee831fSMark Brown 	}
659e4ee831fSMark Brown 	dcdc->base = res->start;
660e4ee831fSMark Brown 
661e4ee831fSMark Brown 	snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
662e4ee831fSMark Brown 	dcdc->desc.name = dcdc->name;
66382caa978SMark Brown 
66482caa978SMark Brown 	snprintf(dcdc->supply_name, sizeof(dcdc->supply_name),
66582caa978SMark Brown 		 "DC%dVDD", id + 1);
66682caa978SMark Brown 	dcdc->desc.supply_name = dcdc->supply_name;
66782caa978SMark Brown 
668e4ee831fSMark Brown 	dcdc->desc.id = id;
669e4ee831fSMark Brown 	dcdc->desc.type = REGULATOR_VOLTAGE;
670e4ee831fSMark Brown 	dcdc->desc.n_voltages = WM831X_BUCKP_MAX_SELECTOR + 1;
671e4ee831fSMark Brown 	dcdc->desc.ops = &wm831x_buckp_ops;
672e4ee831fSMark Brown 	dcdc->desc.owner = THIS_MODULE;
673817436e7SMark Brown 	dcdc->desc.vsel_reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
674817436e7SMark Brown 	dcdc->desc.vsel_mask = WM831X_DC3_ON_VSEL_MASK;
6753d138fccSMark Brown 	dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
6763d138fccSMark Brown 	dcdc->desc.enable_mask = 1 << id;
677c70ad9dcSAxel Lin 	dcdc->desc.min_uV = 850000;
678c70ad9dcSAxel Lin 	dcdc->desc.uV_step = 25000;
679e4ee831fSMark Brown 
680c172708dSMark Brown 	config.dev = pdev->dev.parent;
681b7ca8788SMark Brown 	if (pdata)
682c172708dSMark Brown 		config.init_data = pdata->dcdc[id];
683c172708dSMark Brown 	config.driver_data = dcdc;
684817436e7SMark Brown 	config.regmap = wm831x->regmap;
685c172708dSMark Brown 
686c172708dSMark Brown 	dcdc->regulator = regulator_register(&dcdc->desc, &config);
687e4ee831fSMark Brown 	if (IS_ERR(dcdc->regulator)) {
688e4ee831fSMark Brown 		ret = PTR_ERR(dcdc->regulator);
689e4ee831fSMark Brown 		dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
690e4ee831fSMark Brown 			id + 1, ret);
691e4ee831fSMark Brown 		goto err;
692e4ee831fSMark Brown 	}
693e4ee831fSMark Brown 
694cd99758bSMark Brown 	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
695dfda9c27SMark Brown 	ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq,
696dfda9c27SMark Brown 				   IRQF_TRIGGER_RISING,	dcdc->name, dcdc);
697e4ee831fSMark Brown 	if (ret != 0) {
698e4ee831fSMark Brown 		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
699e4ee831fSMark Brown 			irq, ret);
700e4ee831fSMark Brown 		goto err_regulator;
701e4ee831fSMark Brown 	}
702e4ee831fSMark Brown 
703e4ee831fSMark Brown 	platform_set_drvdata(pdev, dcdc);
704e4ee831fSMark Brown 
705e4ee831fSMark Brown 	return 0;
706e4ee831fSMark Brown 
707e4ee831fSMark Brown err_regulator:
708e4ee831fSMark Brown 	regulator_unregister(dcdc->regulator);
709e4ee831fSMark Brown err:
710e4ee831fSMark Brown 	return ret;
711e4ee831fSMark Brown }
712e4ee831fSMark Brown 
713e4ee831fSMark Brown static __devexit int wm831x_buckp_remove(struct platform_device *pdev)
714e4ee831fSMark Brown {
715e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
716e4ee831fSMark Brown 
717eb66d565SDmitry Torokhov 	platform_set_drvdata(pdev, NULL);
718eb66d565SDmitry Torokhov 
719cd99758bSMark Brown 	free_irq(wm831x_irq(dcdc->wm831x, platform_get_irq_byname(pdev, "UV")),
720cd99758bSMark Brown 			    dcdc);
721e4ee831fSMark Brown 	regulator_unregister(dcdc->regulator);
722e4ee831fSMark Brown 
723e4ee831fSMark Brown 	return 0;
724e4ee831fSMark Brown }
725e4ee831fSMark Brown 
726e4ee831fSMark Brown static struct platform_driver wm831x_buckp_driver = {
727e4ee831fSMark Brown 	.probe = wm831x_buckp_probe,
728e4ee831fSMark Brown 	.remove = __devexit_p(wm831x_buckp_remove),
729e4ee831fSMark Brown 	.driver		= {
730e4ee831fSMark Brown 		.name	= "wm831x-buckp",
731eb66d565SDmitry Torokhov 		.owner	= THIS_MODULE,
732e4ee831fSMark Brown 	},
733e4ee831fSMark Brown };
734e4ee831fSMark Brown 
7358267a9baSMark Brown /*
7361304850dSMark Brown  * DCDC boost convertors
7371304850dSMark Brown  */
7381304850dSMark Brown 
7391304850dSMark Brown static int wm831x_boostp_get_status(struct regulator_dev *rdev)
7401304850dSMark Brown {
7411304850dSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
7421304850dSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
7431304850dSMark Brown 	int ret;
7441304850dSMark Brown 
7451304850dSMark Brown 	/* First, check for errors */
7461304850dSMark Brown 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
7471304850dSMark Brown 	if (ret < 0)
7481304850dSMark Brown 		return ret;
7491304850dSMark Brown 
7501304850dSMark Brown 	if (ret & (1 << rdev_get_id(rdev))) {
7511304850dSMark Brown 		dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
7521304850dSMark Brown 			rdev_get_id(rdev) + 1);
7531304850dSMark Brown 		return REGULATOR_STATUS_ERROR;
7541304850dSMark Brown 	}
7551304850dSMark Brown 
7561304850dSMark Brown 	/* Is the regulator on? */
7571304850dSMark Brown 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
7581304850dSMark Brown 	if (ret < 0)
7591304850dSMark Brown 		return ret;
7601304850dSMark Brown 	if (ret & (1 << rdev_get_id(rdev)))
7611304850dSMark Brown 		return REGULATOR_STATUS_ON;
7621304850dSMark Brown 	else
7631304850dSMark Brown 		return REGULATOR_STATUS_OFF;
7641304850dSMark Brown }
7651304850dSMark Brown 
7661304850dSMark Brown static struct regulator_ops wm831x_boostp_ops = {
7671304850dSMark Brown 	.get_status = wm831x_boostp_get_status,
7681304850dSMark Brown 
7693d138fccSMark Brown 	.is_enabled = regulator_is_enabled_regmap,
7703d138fccSMark Brown 	.enable = regulator_enable_regmap,
7713d138fccSMark Brown 	.disable = regulator_disable_regmap,
7721304850dSMark Brown };
7731304850dSMark Brown 
7741304850dSMark Brown static __devinit int wm831x_boostp_probe(struct platform_device *pdev)
7751304850dSMark Brown {
7761304850dSMark Brown 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
7771304850dSMark Brown 	struct wm831x_pdata *pdata = wm831x->dev->platform_data;
778c172708dSMark Brown 	struct regulator_config config = { };
7791304850dSMark Brown 	int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
7801304850dSMark Brown 	struct wm831x_dcdc *dcdc;
7811304850dSMark Brown 	struct resource *res;
7821304850dSMark Brown 	int ret, irq;
7831304850dSMark Brown 
7841304850dSMark Brown 	dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
7851304850dSMark Brown 
7861304850dSMark Brown 	if (pdata == NULL || pdata->dcdc[id] == NULL)
7871304850dSMark Brown 		return -ENODEV;
7881304850dSMark Brown 
7894c60165dSAxel Lin 	dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), GFP_KERNEL);
7901304850dSMark Brown 	if (dcdc == NULL) {
7911304850dSMark Brown 		dev_err(&pdev->dev, "Unable to allocate private data\n");
7921304850dSMark Brown 		return -ENOMEM;
7931304850dSMark Brown 	}
7941304850dSMark Brown 
7951304850dSMark Brown 	dcdc->wm831x = wm831x;
7961304850dSMark Brown 
7975656098eSMark Brown 	res = platform_get_resource(pdev, IORESOURCE_REG, 0);
7981304850dSMark Brown 	if (res == NULL) {
7995656098eSMark Brown 		dev_err(&pdev->dev, "No REG resource\n");
8001304850dSMark Brown 		ret = -EINVAL;
8011304850dSMark Brown 		goto err;
8021304850dSMark Brown 	}
8031304850dSMark Brown 	dcdc->base = res->start;
8041304850dSMark Brown 
8051304850dSMark Brown 	snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
8061304850dSMark Brown 	dcdc->desc.name = dcdc->name;
8071304850dSMark Brown 	dcdc->desc.id = id;
8081304850dSMark Brown 	dcdc->desc.type = REGULATOR_VOLTAGE;
8091304850dSMark Brown 	dcdc->desc.ops = &wm831x_boostp_ops;
8101304850dSMark Brown 	dcdc->desc.owner = THIS_MODULE;
8113d138fccSMark Brown 	dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
8123d138fccSMark Brown 	dcdc->desc.enable_mask = 1 << id;
8131304850dSMark Brown 
814c172708dSMark Brown 	config.dev = pdev->dev.parent;
815f0b067d9SMark Brown 	if (pdata)
816c172708dSMark Brown 		config.init_data = pdata->dcdc[id];
817c172708dSMark Brown 	config.driver_data = dcdc;
8183d138fccSMark Brown 	config.regmap = wm831x->regmap;
819c172708dSMark Brown 
820c172708dSMark Brown 	dcdc->regulator = regulator_register(&dcdc->desc, &config);
8211304850dSMark Brown 	if (IS_ERR(dcdc->regulator)) {
8221304850dSMark Brown 		ret = PTR_ERR(dcdc->regulator);
8231304850dSMark Brown 		dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
8241304850dSMark Brown 			id + 1, ret);
8251304850dSMark Brown 		goto err;
8261304850dSMark Brown 	}
8271304850dSMark Brown 
828cd99758bSMark Brown 	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
829dfda9c27SMark Brown 	ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq,
8301304850dSMark Brown 				   IRQF_TRIGGER_RISING, dcdc->name,
8311304850dSMark Brown 				   dcdc);
8321304850dSMark Brown 	if (ret != 0) {
8331304850dSMark Brown 		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
8341304850dSMark Brown 			irq, ret);
8351304850dSMark Brown 		goto err_regulator;
8361304850dSMark Brown 	}
8371304850dSMark Brown 
8381304850dSMark Brown 	platform_set_drvdata(pdev, dcdc);
8391304850dSMark Brown 
8401304850dSMark Brown 	return 0;
8411304850dSMark Brown 
8421304850dSMark Brown err_regulator:
8431304850dSMark Brown 	regulator_unregister(dcdc->regulator);
8441304850dSMark Brown err:
8451304850dSMark Brown 	return ret;
8461304850dSMark Brown }
8471304850dSMark Brown 
8481304850dSMark Brown static __devexit int wm831x_boostp_remove(struct platform_device *pdev)
8491304850dSMark Brown {
8501304850dSMark Brown 	struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
8511304850dSMark Brown 
852eb66d565SDmitry Torokhov 	platform_set_drvdata(pdev, NULL);
853eb66d565SDmitry Torokhov 
854cd99758bSMark Brown 	free_irq(wm831x_irq(dcdc->wm831x, platform_get_irq_byname(pdev, "UV")),
855cd99758bSMark Brown 		 dcdc);
8561304850dSMark Brown 	regulator_unregister(dcdc->regulator);
8571304850dSMark Brown 
8581304850dSMark Brown 	return 0;
8591304850dSMark Brown }
8601304850dSMark Brown 
8611304850dSMark Brown static struct platform_driver wm831x_boostp_driver = {
8621304850dSMark Brown 	.probe = wm831x_boostp_probe,
8631304850dSMark Brown 	.remove = __devexit_p(wm831x_boostp_remove),
8641304850dSMark Brown 	.driver		= {
8651304850dSMark Brown 		.name	= "wm831x-boostp",
866eb66d565SDmitry Torokhov 		.owner	= THIS_MODULE,
8671304850dSMark Brown 	},
8681304850dSMark Brown };
8691304850dSMark Brown 
8701304850dSMark Brown /*
8718267a9baSMark Brown  * External Power Enable
8728267a9baSMark Brown  *
8738267a9baSMark Brown  * These aren't actually DCDCs but look like them in hardware so share
8748267a9baSMark Brown  * code.
8758267a9baSMark Brown  */
8768267a9baSMark Brown 
8778267a9baSMark Brown #define WM831X_EPE_BASE 6
8788267a9baSMark Brown 
8798267a9baSMark Brown static struct regulator_ops wm831x_epe_ops = {
8803d138fccSMark Brown 	.is_enabled = regulator_is_enabled_regmap,
8813d138fccSMark Brown 	.enable = regulator_enable_regmap,
8823d138fccSMark Brown 	.disable = regulator_disable_regmap,
8838267a9baSMark Brown 	.get_status = wm831x_dcdc_get_status,
8848267a9baSMark Brown };
8858267a9baSMark Brown 
8868267a9baSMark Brown static __devinit int wm831x_epe_probe(struct platform_device *pdev)
8878267a9baSMark Brown {
8888267a9baSMark Brown 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
8898267a9baSMark Brown 	struct wm831x_pdata *pdata = wm831x->dev->platform_data;
890c172708dSMark Brown 	struct regulator_config config = { };
8918267a9baSMark Brown 	int id = pdev->id % ARRAY_SIZE(pdata->epe);
8928267a9baSMark Brown 	struct wm831x_dcdc *dcdc;
8938267a9baSMark Brown 	int ret;
8948267a9baSMark Brown 
8958267a9baSMark Brown 	dev_dbg(&pdev->dev, "Probing EPE%d\n", id + 1);
8968267a9baSMark Brown 
8974c60165dSAxel Lin 	dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), GFP_KERNEL);
8988267a9baSMark Brown 	if (dcdc == NULL) {
8998267a9baSMark Brown 		dev_err(&pdev->dev, "Unable to allocate private data\n");
9008267a9baSMark Brown 		return -ENOMEM;
9018267a9baSMark Brown 	}
9028267a9baSMark Brown 
9038267a9baSMark Brown 	dcdc->wm831x = wm831x;
9048267a9baSMark Brown 
9058267a9baSMark Brown 	/* For current parts this is correct; probably need to revisit
9068267a9baSMark Brown 	 * in future.
9078267a9baSMark Brown 	 */
9088267a9baSMark Brown 	snprintf(dcdc->name, sizeof(dcdc->name), "EPE%d", id + 1);
9098267a9baSMark Brown 	dcdc->desc.name = dcdc->name;
9108267a9baSMark Brown 	dcdc->desc.id = id + WM831X_EPE_BASE; /* Offset in DCDC registers */
9118267a9baSMark Brown 	dcdc->desc.ops = &wm831x_epe_ops;
9128267a9baSMark Brown 	dcdc->desc.type = REGULATOR_VOLTAGE;
9138267a9baSMark Brown 	dcdc->desc.owner = THIS_MODULE;
9143d138fccSMark Brown 	dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
9153d138fccSMark Brown 	dcdc->desc.enable_mask = 1 << dcdc->desc.id;
9168267a9baSMark Brown 
917c172708dSMark Brown 	config.dev = pdev->dev.parent;
918b7ca8788SMark Brown 	if (pdata)
919c172708dSMark Brown 		config.init_data = pdata->epe[id];
920c172708dSMark Brown 	config.driver_data = dcdc;
9213d138fccSMark Brown 	config.regmap = wm831x->regmap;
922c172708dSMark Brown 
923c172708dSMark Brown 	dcdc->regulator = regulator_register(&dcdc->desc, &config);
9248267a9baSMark Brown 	if (IS_ERR(dcdc->regulator)) {
9258267a9baSMark Brown 		ret = PTR_ERR(dcdc->regulator);
9268267a9baSMark Brown 		dev_err(wm831x->dev, "Failed to register EPE%d: %d\n",
9278267a9baSMark Brown 			id + 1, ret);
9288267a9baSMark Brown 		goto err;
9298267a9baSMark Brown 	}
9308267a9baSMark Brown 
9318267a9baSMark Brown 	platform_set_drvdata(pdev, dcdc);
9328267a9baSMark Brown 
9338267a9baSMark Brown 	return 0;
9348267a9baSMark Brown 
9358267a9baSMark Brown err:
9368267a9baSMark Brown 	return ret;
9378267a9baSMark Brown }
9388267a9baSMark Brown 
9398267a9baSMark Brown static __devexit int wm831x_epe_remove(struct platform_device *pdev)
9408267a9baSMark Brown {
9418267a9baSMark Brown 	struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
9428267a9baSMark Brown 
943eb66d565SDmitry Torokhov 	platform_set_drvdata(pdev, NULL);
9448267a9baSMark Brown 	regulator_unregister(dcdc->regulator);
9458267a9baSMark Brown 
9468267a9baSMark Brown 	return 0;
9478267a9baSMark Brown }
9488267a9baSMark Brown 
9498267a9baSMark Brown static struct platform_driver wm831x_epe_driver = {
9508267a9baSMark Brown 	.probe = wm831x_epe_probe,
9518267a9baSMark Brown 	.remove = __devexit_p(wm831x_epe_remove),
9528267a9baSMark Brown 	.driver		= {
9538267a9baSMark Brown 		.name	= "wm831x-epe",
954eb66d565SDmitry Torokhov 		.owner	= THIS_MODULE,
9558267a9baSMark Brown 	},
9568267a9baSMark Brown };
9578267a9baSMark Brown 
958e4ee831fSMark Brown static int __init wm831x_dcdc_init(void)
959e4ee831fSMark Brown {
960e4ee831fSMark Brown 	int ret;
961e4ee831fSMark Brown 	ret = platform_driver_register(&wm831x_buckv_driver);
962e4ee831fSMark Brown 	if (ret != 0)
963e4ee831fSMark Brown 		pr_err("Failed to register WM831x BUCKV driver: %d\n", ret);
964e4ee831fSMark Brown 
965e4ee831fSMark Brown 	ret = platform_driver_register(&wm831x_buckp_driver);
966e4ee831fSMark Brown 	if (ret != 0)
967e4ee831fSMark Brown 		pr_err("Failed to register WM831x BUCKP driver: %d\n", ret);
968e4ee831fSMark Brown 
9691304850dSMark Brown 	ret = platform_driver_register(&wm831x_boostp_driver);
9701304850dSMark Brown 	if (ret != 0)
9711304850dSMark Brown 		pr_err("Failed to register WM831x BOOST driver: %d\n", ret);
9721304850dSMark Brown 
9738267a9baSMark Brown 	ret = platform_driver_register(&wm831x_epe_driver);
9748267a9baSMark Brown 	if (ret != 0)
9758267a9baSMark Brown 		pr_err("Failed to register WM831x EPE driver: %d\n", ret);
9768267a9baSMark Brown 
977e4ee831fSMark Brown 	return 0;
978e4ee831fSMark Brown }
979e4ee831fSMark Brown subsys_initcall(wm831x_dcdc_init);
980e4ee831fSMark Brown 
981e4ee831fSMark Brown static void __exit wm831x_dcdc_exit(void)
982e4ee831fSMark Brown {
9838267a9baSMark Brown 	platform_driver_unregister(&wm831x_epe_driver);
9841304850dSMark Brown 	platform_driver_unregister(&wm831x_boostp_driver);
985e4ee831fSMark Brown 	platform_driver_unregister(&wm831x_buckp_driver);
986e4ee831fSMark Brown 	platform_driver_unregister(&wm831x_buckv_driver);
987e4ee831fSMark Brown }
988e4ee831fSMark Brown module_exit(wm831x_dcdc_exit);
989e4ee831fSMark Brown 
990e4ee831fSMark Brown /* Module information */
991e4ee831fSMark Brown MODULE_AUTHOR("Mark Brown");
992e4ee831fSMark Brown MODULE_DESCRIPTION("WM831x DC-DC convertor driver");
993e4ee831fSMark Brown MODULE_LICENSE("GPL");
994e4ee831fSMark Brown MODULE_ALIAS("platform:wm831x-buckv");
995e4ee831fSMark Brown MODULE_ALIAS("platform:wm831x-buckp");
99624b43150SMark Brown MODULE_ALIAS("platform:wm831x-epe");
997