1e4ee831fSMark Brown /*
2e4ee831fSMark Brown  * wm831x-dcdc.c  --  DC-DC buck convertor driver for the WM831x series
3e4ee831fSMark Brown  *
4e4ee831fSMark Brown  * Copyright 2009 Wolfson Microelectronics PLC.
5e4ee831fSMark Brown  *
6e4ee831fSMark Brown  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7e4ee831fSMark Brown  *
8e4ee831fSMark Brown  *  This program is free software; you can redistribute  it and/or modify it
9e4ee831fSMark Brown  *  under  the terms of  the GNU General  Public License as published by the
10e4ee831fSMark Brown  *  Free Software Foundation;  either version 2 of the  License, or (at your
11e4ee831fSMark Brown  *  option) any later version.
12e4ee831fSMark Brown  */
13e4ee831fSMark Brown 
14e4ee831fSMark Brown #include <linux/module.h>
15e4ee831fSMark Brown #include <linux/moduleparam.h>
16e4ee831fSMark Brown #include <linux/init.h>
17e4ee831fSMark Brown #include <linux/bitops.h>
18e4ee831fSMark Brown #include <linux/err.h>
19e4ee831fSMark Brown #include <linux/i2c.h>
20e4ee831fSMark Brown #include <linux/platform_device.h>
21e4ee831fSMark Brown #include <linux/regulator/driver.h>
22e24a04c4SMark Brown #include <linux/regulator/machine.h>
23e24a04c4SMark Brown #include <linux/gpio.h>
245a0e3ad6STejun Heo #include <linux/slab.h>
25e4ee831fSMark Brown 
26e4ee831fSMark Brown #include <linux/mfd/wm831x/core.h>
27e4ee831fSMark Brown #include <linux/mfd/wm831x/regulator.h>
28e4ee831fSMark Brown #include <linux/mfd/wm831x/pdata.h>
29e4ee831fSMark Brown 
30e4ee831fSMark Brown #define WM831X_BUCKV_MAX_SELECTOR 0x68
31e4ee831fSMark Brown #define WM831X_BUCKP_MAX_SELECTOR 0x66
32e4ee831fSMark Brown 
33e4ee831fSMark Brown #define WM831X_DCDC_MODE_FAST    0
34e4ee831fSMark Brown #define WM831X_DCDC_MODE_NORMAL  1
35e4ee831fSMark Brown #define WM831X_DCDC_MODE_IDLE    2
36e4ee831fSMark Brown #define WM831X_DCDC_MODE_STANDBY 3
37e4ee831fSMark Brown 
38e4ee831fSMark Brown #define WM831X_DCDC_MAX_NAME 6
39e4ee831fSMark Brown 
40e4ee831fSMark Brown /* Register offsets in control block */
41e4ee831fSMark Brown #define WM831X_DCDC_CONTROL_1     0
42e4ee831fSMark Brown #define WM831X_DCDC_CONTROL_2     1
43e4ee831fSMark Brown #define WM831X_DCDC_ON_CONFIG     2
44e4ee831fSMark Brown #define WM831X_DCDC_SLEEP_CONTROL 3
45e24a04c4SMark Brown #define WM831X_DCDC_DVS_CONTROL   4
46e4ee831fSMark Brown 
47e4ee831fSMark Brown /*
48e4ee831fSMark Brown  * Shared
49e4ee831fSMark Brown  */
50e4ee831fSMark Brown 
51e4ee831fSMark Brown struct wm831x_dcdc {
52e4ee831fSMark Brown 	char name[WM831X_DCDC_MAX_NAME];
53e4ee831fSMark Brown 	struct regulator_desc desc;
54e4ee831fSMark Brown 	int base;
55e4ee831fSMark Brown 	struct wm831x *wm831x;
56e4ee831fSMark Brown 	struct regulator_dev *regulator;
57e24a04c4SMark Brown 	int dvs_gpio;
58e24a04c4SMark Brown 	int dvs_gpio_state;
59e24a04c4SMark Brown 	int on_vsel;
60e24a04c4SMark Brown 	int dvs_vsel;
61e4ee831fSMark Brown };
62e4ee831fSMark Brown 
63e4ee831fSMark Brown static int wm831x_dcdc_is_enabled(struct regulator_dev *rdev)
64e4ee831fSMark Brown {
65e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
66e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
67e4ee831fSMark Brown 	int mask = 1 << rdev_get_id(rdev);
68e4ee831fSMark Brown 	int reg;
69e4ee831fSMark Brown 
70e4ee831fSMark Brown 	reg = wm831x_reg_read(wm831x, WM831X_DCDC_ENABLE);
71e4ee831fSMark Brown 	if (reg < 0)
72e4ee831fSMark Brown 		return reg;
73e4ee831fSMark Brown 
74e4ee831fSMark Brown 	if (reg & mask)
75e4ee831fSMark Brown 		return 1;
76e4ee831fSMark Brown 	else
77e4ee831fSMark Brown 		return 0;
78e4ee831fSMark Brown }
79e4ee831fSMark Brown 
80e4ee831fSMark Brown static int wm831x_dcdc_enable(struct regulator_dev *rdev)
81e4ee831fSMark Brown {
82e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
83e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
84e4ee831fSMark Brown 	int mask = 1 << rdev_get_id(rdev);
85e4ee831fSMark Brown 
86e4ee831fSMark Brown 	return wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE, mask, mask);
87e4ee831fSMark Brown }
88e4ee831fSMark Brown 
89e4ee831fSMark Brown static int wm831x_dcdc_disable(struct regulator_dev *rdev)
90e4ee831fSMark Brown {
91e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
92e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
93e4ee831fSMark Brown 	int mask = 1 << rdev_get_id(rdev);
94e4ee831fSMark Brown 
95e4ee831fSMark Brown 	return wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE, mask, 0);
96e4ee831fSMark Brown }
97e4ee831fSMark Brown 
98e4ee831fSMark Brown static unsigned int wm831x_dcdc_get_mode(struct regulator_dev *rdev)
99e4ee831fSMark Brown 
100e4ee831fSMark Brown {
101e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
102e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
103e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
104e4ee831fSMark Brown 	int val;
105e4ee831fSMark Brown 
106e4ee831fSMark Brown 	val = wm831x_reg_read(wm831x, reg);
107e4ee831fSMark Brown 	if (val < 0)
108e4ee831fSMark Brown 		return val;
109e4ee831fSMark Brown 
110e4ee831fSMark Brown 	val = (val & WM831X_DC1_ON_MODE_MASK) >> WM831X_DC1_ON_MODE_SHIFT;
111e4ee831fSMark Brown 
112e4ee831fSMark Brown 	switch (val) {
113e4ee831fSMark Brown 	case WM831X_DCDC_MODE_FAST:
114e4ee831fSMark Brown 		return REGULATOR_MODE_FAST;
115e4ee831fSMark Brown 	case WM831X_DCDC_MODE_NORMAL:
116e4ee831fSMark Brown 		return REGULATOR_MODE_NORMAL;
117e4ee831fSMark Brown 	case WM831X_DCDC_MODE_STANDBY:
118e4ee831fSMark Brown 		return REGULATOR_MODE_STANDBY;
119e4ee831fSMark Brown 	case WM831X_DCDC_MODE_IDLE:
120e4ee831fSMark Brown 		return REGULATOR_MODE_IDLE;
121e4ee831fSMark Brown 	default:
122e4ee831fSMark Brown 		BUG();
1239ee291a4SMark Brown 		return -EINVAL;
124e4ee831fSMark Brown 	}
125e4ee831fSMark Brown }
126e4ee831fSMark Brown 
127e4ee831fSMark Brown static int wm831x_dcdc_set_mode_int(struct wm831x *wm831x, int reg,
128e4ee831fSMark Brown 				    unsigned int mode)
129e4ee831fSMark Brown {
130e4ee831fSMark Brown 	int val;
131e4ee831fSMark Brown 
132e4ee831fSMark Brown 	switch (mode) {
133e4ee831fSMark Brown 	case REGULATOR_MODE_FAST:
134e4ee831fSMark Brown 		val = WM831X_DCDC_MODE_FAST;
135e4ee831fSMark Brown 		break;
136e4ee831fSMark Brown 	case REGULATOR_MODE_NORMAL:
137e4ee831fSMark Brown 		val = WM831X_DCDC_MODE_NORMAL;
138e4ee831fSMark Brown 		break;
139e4ee831fSMark Brown 	case REGULATOR_MODE_STANDBY:
140e4ee831fSMark Brown 		val = WM831X_DCDC_MODE_STANDBY;
141e4ee831fSMark Brown 		break;
142e4ee831fSMark Brown 	case REGULATOR_MODE_IDLE:
143e4ee831fSMark Brown 		val = WM831X_DCDC_MODE_IDLE;
144e4ee831fSMark Brown 		break;
145e4ee831fSMark Brown 	default:
146e4ee831fSMark Brown 		return -EINVAL;
147e4ee831fSMark Brown 	}
148e4ee831fSMark Brown 
149e4ee831fSMark Brown 	return wm831x_set_bits(wm831x, reg, WM831X_DC1_ON_MODE_MASK,
150e4ee831fSMark Brown 			       val << WM831X_DC1_ON_MODE_SHIFT);
151e4ee831fSMark Brown }
152e4ee831fSMark Brown 
153e4ee831fSMark Brown static int wm831x_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
154e4ee831fSMark Brown {
155e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
156e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
157e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
158e4ee831fSMark Brown 
159e4ee831fSMark Brown 	return wm831x_dcdc_set_mode_int(wm831x, reg, mode);
160e4ee831fSMark Brown }
161e4ee831fSMark Brown 
162e4ee831fSMark Brown static int wm831x_dcdc_set_suspend_mode(struct regulator_dev *rdev,
163e4ee831fSMark Brown 					unsigned int mode)
164e4ee831fSMark Brown {
165e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
166e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
167e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
168e4ee831fSMark Brown 
169e4ee831fSMark Brown 	return wm831x_dcdc_set_mode_int(wm831x, reg, mode);
170e4ee831fSMark Brown }
171e4ee831fSMark Brown 
172e4ee831fSMark Brown static int wm831x_dcdc_get_status(struct regulator_dev *rdev)
173e4ee831fSMark Brown {
174e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
175e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
176e4ee831fSMark Brown 	int ret;
177e4ee831fSMark Brown 
178e4ee831fSMark Brown 	/* First, check for errors */
179e4ee831fSMark Brown 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
180e4ee831fSMark Brown 	if (ret < 0)
181e4ee831fSMark Brown 		return ret;
182e4ee831fSMark Brown 
183e4ee831fSMark Brown 	if (ret & (1 << rdev_get_id(rdev))) {
184e4ee831fSMark Brown 		dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
185e4ee831fSMark Brown 			rdev_get_id(rdev) + 1);
186e4ee831fSMark Brown 		return REGULATOR_STATUS_ERROR;
187e4ee831fSMark Brown 	}
188e4ee831fSMark Brown 
189e4ee831fSMark Brown 	/* DCDC1 and DCDC2 can additionally detect high voltage/current */
190e4ee831fSMark Brown 	if (rdev_get_id(rdev) < 2) {
191e4ee831fSMark Brown 		if (ret & (WM831X_DC1_OV_STS << rdev_get_id(rdev))) {
192e4ee831fSMark Brown 			dev_dbg(wm831x->dev, "DCDC%d over voltage\n",
193e4ee831fSMark Brown 				rdev_get_id(rdev) + 1);
194e4ee831fSMark Brown 			return REGULATOR_STATUS_ERROR;
195e4ee831fSMark Brown 		}
196e4ee831fSMark Brown 
197e4ee831fSMark Brown 		if (ret & (WM831X_DC1_HC_STS << rdev_get_id(rdev))) {
198e4ee831fSMark Brown 			dev_dbg(wm831x->dev, "DCDC%d over current\n",
199e4ee831fSMark Brown 				rdev_get_id(rdev) + 1);
200e4ee831fSMark Brown 			return REGULATOR_STATUS_ERROR;
201e4ee831fSMark Brown 		}
202e4ee831fSMark Brown 	}
203e4ee831fSMark Brown 
204e4ee831fSMark Brown 	/* Is the regulator on? */
205e4ee831fSMark Brown 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
206e4ee831fSMark Brown 	if (ret < 0)
207e4ee831fSMark Brown 		return ret;
208e4ee831fSMark Brown 	if (!(ret & (1 << rdev_get_id(rdev))))
209e4ee831fSMark Brown 		return REGULATOR_STATUS_OFF;
210e4ee831fSMark Brown 
211e4ee831fSMark Brown 	/* TODO: When we handle hardware control modes so we can report the
212e4ee831fSMark Brown 	 * current mode. */
213e4ee831fSMark Brown 	return REGULATOR_STATUS_ON;
214e4ee831fSMark Brown }
215e4ee831fSMark Brown 
216e4ee831fSMark Brown static irqreturn_t wm831x_dcdc_uv_irq(int irq, void *data)
217e4ee831fSMark Brown {
218e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = data;
219e4ee831fSMark Brown 
220e4ee831fSMark Brown 	regulator_notifier_call_chain(dcdc->regulator,
221e4ee831fSMark Brown 				      REGULATOR_EVENT_UNDER_VOLTAGE,
222e4ee831fSMark Brown 				      NULL);
223e4ee831fSMark Brown 
224e4ee831fSMark Brown 	return IRQ_HANDLED;
225e4ee831fSMark Brown }
226e4ee831fSMark Brown 
227e4ee831fSMark Brown static irqreturn_t wm831x_dcdc_oc_irq(int irq, void *data)
228e4ee831fSMark Brown {
229e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = data;
230e4ee831fSMark Brown 
231e4ee831fSMark Brown 	regulator_notifier_call_chain(dcdc->regulator,
232e4ee831fSMark Brown 				      REGULATOR_EVENT_OVER_CURRENT,
233e4ee831fSMark Brown 				      NULL);
234e4ee831fSMark Brown 
235e4ee831fSMark Brown 	return IRQ_HANDLED;
236e4ee831fSMark Brown }
237e4ee831fSMark Brown 
238e4ee831fSMark Brown /*
239e4ee831fSMark Brown  * BUCKV specifics
240e4ee831fSMark Brown  */
241e4ee831fSMark Brown 
242e4ee831fSMark Brown static int wm831x_buckv_list_voltage(struct regulator_dev *rdev,
243e4ee831fSMark Brown 				      unsigned selector)
244e4ee831fSMark Brown {
245e4ee831fSMark Brown 	if (selector <= 0x8)
246e4ee831fSMark Brown 		return 600000;
247e4ee831fSMark Brown 	if (selector <= WM831X_BUCKV_MAX_SELECTOR)
248e4ee831fSMark Brown 		return 600000 + ((selector - 0x8) * 12500);
249e4ee831fSMark Brown 	return -EINVAL;
250e4ee831fSMark Brown }
251e4ee831fSMark Brown 
252e24a04c4SMark Brown static int wm831x_buckv_select_min_voltage(struct regulator_dev *rdev,
253e4ee831fSMark Brown 					   int min_uV, int max_uV)
254e4ee831fSMark Brown {
255e4ee831fSMark Brown 	u16 vsel;
256e4ee831fSMark Brown 
257e4ee831fSMark Brown 	if (min_uV < 600000)
258e4ee831fSMark Brown 		vsel = 0;
259e4ee831fSMark Brown 	else if (min_uV <= 1800000)
260e4ee831fSMark Brown 		vsel = ((min_uV - 600000) / 12500) + 8;
261e4ee831fSMark Brown 	else
262e4ee831fSMark Brown 		return -EINVAL;
263e4ee831fSMark Brown 
264e4ee831fSMark Brown 	if (wm831x_buckv_list_voltage(rdev, vsel) > max_uV)
265e4ee831fSMark Brown 		return -EINVAL;
266e4ee831fSMark Brown 
267e24a04c4SMark Brown 	return vsel;
268e24a04c4SMark Brown }
269e24a04c4SMark Brown 
270e24a04c4SMark Brown static int wm831x_buckv_set_dvs(struct regulator_dev *rdev, int state)
271e24a04c4SMark Brown {
272e24a04c4SMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
273e24a04c4SMark Brown 
274e24a04c4SMark Brown 	if (state == dcdc->dvs_gpio_state)
275e24a04c4SMark Brown 		return 0;
276e24a04c4SMark Brown 
277e24a04c4SMark Brown 	dcdc->dvs_gpio_state = state;
278e24a04c4SMark Brown 	gpio_set_value(dcdc->dvs_gpio, state);
279e24a04c4SMark Brown 
280e24a04c4SMark Brown 	/* Should wait for DVS state change to be asserted if we have
281e24a04c4SMark Brown 	 * a GPIO for it, for now assume the device is configured
282e24a04c4SMark Brown 	 * for the fastest possible transition.
283e24a04c4SMark Brown 	 */
284e24a04c4SMark Brown 
285e24a04c4SMark Brown 	return 0;
286e4ee831fSMark Brown }
287e4ee831fSMark Brown 
288e4ee831fSMark Brown static int wm831x_buckv_set_voltage(struct regulator_dev *rdev,
2893a93f2a9SMark Brown 				    int min_uV, int max_uV, unsigned *selector)
290e4ee831fSMark Brown {
291e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
292e24a04c4SMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
293e24a04c4SMark Brown 	int on_reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
294e24a04c4SMark Brown 	int dvs_reg = dcdc->base + WM831X_DCDC_DVS_CONTROL;
295e24a04c4SMark Brown 	int vsel, ret;
296e4ee831fSMark Brown 
297e24a04c4SMark Brown 	vsel = wm831x_buckv_select_min_voltage(rdev, min_uV, max_uV);
298e24a04c4SMark Brown 	if (vsel < 0)
299e24a04c4SMark Brown 		return vsel;
300e24a04c4SMark Brown 
3013a93f2a9SMark Brown 	*selector = vsel;
3023a93f2a9SMark Brown 
303e24a04c4SMark Brown 	/* If this value is already set then do a GPIO update if we can */
304e24a04c4SMark Brown 	if (dcdc->dvs_gpio && dcdc->on_vsel == vsel)
305e24a04c4SMark Brown 		return wm831x_buckv_set_dvs(rdev, 0);
306e24a04c4SMark Brown 
307e24a04c4SMark Brown 	if (dcdc->dvs_gpio && dcdc->dvs_vsel == vsel)
308e24a04c4SMark Brown 		return wm831x_buckv_set_dvs(rdev, 1);
309e24a04c4SMark Brown 
310e24a04c4SMark Brown 	/* Always set the ON status to the minimum voltage */
311e24a04c4SMark Brown 	ret = wm831x_set_bits(wm831x, on_reg, WM831X_DC1_ON_VSEL_MASK, vsel);
312e24a04c4SMark Brown 	if (ret < 0)
313e24a04c4SMark Brown 		return ret;
314e24a04c4SMark Brown 	dcdc->on_vsel = vsel;
315e24a04c4SMark Brown 
316e24a04c4SMark Brown 	if (!dcdc->dvs_gpio)
317e24a04c4SMark Brown 		return ret;
318e24a04c4SMark Brown 
319e24a04c4SMark Brown 	/* Kick the voltage transition now */
320e24a04c4SMark Brown 	ret = wm831x_buckv_set_dvs(rdev, 0);
321e24a04c4SMark Brown 	if (ret < 0)
322e24a04c4SMark Brown 		return ret;
323e24a04c4SMark Brown 
32488cda60eSMark Brown 	/*
32588cda60eSMark Brown 	 * If this VSEL is higher than the last one we've seen then
32688cda60eSMark Brown 	 * remember it as the DVS VSEL.  This is optimised for CPUfreq
32788cda60eSMark Brown 	 * usage where we want to get to the highest voltage very
32888cda60eSMark Brown 	 * quickly.
32988cda60eSMark Brown 	 */
33088cda60eSMark Brown 	if (vsel > dcdc->dvs_vsel) {
33188cda60eSMark Brown 		ret = wm831x_set_bits(wm831x, dvs_reg,
33288cda60eSMark Brown 				      WM831X_DC1_DVS_VSEL_MASK,
33388cda60eSMark Brown 				      dcdc->dvs_vsel);
334e24a04c4SMark Brown 		if (ret == 0)
335e24a04c4SMark Brown 			dcdc->dvs_vsel = vsel;
336e24a04c4SMark Brown 		else
33788cda60eSMark Brown 			dev_warn(wm831x->dev,
33888cda60eSMark Brown 				 "Failed to set DCDC DVS VSEL: %d\n", ret);
33988cda60eSMark Brown 	}
340e24a04c4SMark Brown 
341e24a04c4SMark Brown 	return 0;
342e4ee831fSMark Brown }
343e4ee831fSMark Brown 
344e4ee831fSMark Brown static int wm831x_buckv_set_suspend_voltage(struct regulator_dev *rdev,
345e4ee831fSMark Brown 					    int uV)
346e4ee831fSMark Brown {
347e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
348e24a04c4SMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
349e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
350e24a04c4SMark Brown 	int vsel;
351e4ee831fSMark Brown 
352e24a04c4SMark Brown 	vsel = wm831x_buckv_select_min_voltage(rdev, uV, uV);
353e24a04c4SMark Brown 	if (vsel < 0)
354e24a04c4SMark Brown 		return vsel;
355e24a04c4SMark Brown 
356e24a04c4SMark Brown 	return wm831x_set_bits(wm831x, reg, WM831X_DC1_SLP_VSEL_MASK, vsel);
357e4ee831fSMark Brown }
358e4ee831fSMark Brown 
359afb8bb80SMark Brown static int wm831x_buckv_get_voltage_sel(struct regulator_dev *rdev)
360e4ee831fSMark Brown {
361e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
362e4ee831fSMark Brown 
363e24a04c4SMark Brown 	if (dcdc->dvs_gpio && dcdc->dvs_gpio_state)
364afb8bb80SMark Brown 		return dcdc->dvs_vsel;
365e24a04c4SMark Brown 	else
366afb8bb80SMark Brown 		return dcdc->on_vsel;
367e4ee831fSMark Brown }
368e4ee831fSMark Brown 
369e4ee831fSMark Brown /* Current limit options */
370e4ee831fSMark Brown static u16 wm831x_dcdc_ilim[] = {
371e4ee831fSMark Brown 	125, 250, 375, 500, 625, 750, 875, 1000
372e4ee831fSMark Brown };
373e4ee831fSMark Brown 
374e4ee831fSMark Brown static int wm831x_buckv_set_current_limit(struct regulator_dev *rdev,
375e4ee831fSMark Brown 					   int min_uA, int max_uA)
376e4ee831fSMark Brown {
377e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
378e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
379e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2;
380e4ee831fSMark Brown 	int i;
381e4ee831fSMark Brown 
382e4ee831fSMark Brown 	for (i = 0; i < ARRAY_SIZE(wm831x_dcdc_ilim); i++) {
383a171e782SAxel Lin 		if ((min_uA <= wm831x_dcdc_ilim[i]) &&
384a171e782SAxel Lin 		    (wm831x_dcdc_ilim[i] <= max_uA))
385e4ee831fSMark Brown 			break;
386e4ee831fSMark Brown 	}
387e4ee831fSMark Brown 	if (i == ARRAY_SIZE(wm831x_dcdc_ilim))
388e4ee831fSMark Brown 		return -EINVAL;
389e4ee831fSMark Brown 
39009bf14b9SAxel Lin 	return wm831x_set_bits(wm831x, reg, WM831X_DC1_HC_THR_MASK,
39109bf14b9SAxel Lin 			       i << WM831X_DC1_HC_THR_SHIFT);
392e4ee831fSMark Brown }
393e4ee831fSMark Brown 
394e4ee831fSMark Brown static int wm831x_buckv_get_current_limit(struct regulator_dev *rdev)
395e4ee831fSMark Brown {
396e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
397e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
398e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2;
399e4ee831fSMark Brown 	int val;
400e4ee831fSMark Brown 
401e4ee831fSMark Brown 	val = wm831x_reg_read(wm831x, reg);
402e4ee831fSMark Brown 	if (val < 0)
403e4ee831fSMark Brown 		return val;
404e4ee831fSMark Brown 
40509bf14b9SAxel Lin 	val = (val & WM831X_DC1_HC_THR_MASK) >> WM831X_DC1_HC_THR_SHIFT;
40609bf14b9SAxel Lin 	return wm831x_dcdc_ilim[val];
407e4ee831fSMark Brown }
408e4ee831fSMark Brown 
409e4ee831fSMark Brown static struct regulator_ops wm831x_buckv_ops = {
410e4ee831fSMark Brown 	.set_voltage = wm831x_buckv_set_voltage,
411afb8bb80SMark Brown 	.get_voltage_sel = wm831x_buckv_get_voltage_sel,
412e4ee831fSMark Brown 	.list_voltage = wm831x_buckv_list_voltage,
413e4ee831fSMark Brown 	.set_suspend_voltage = wm831x_buckv_set_suspend_voltage,
414e4ee831fSMark Brown 	.set_current_limit = wm831x_buckv_set_current_limit,
415e4ee831fSMark Brown 	.get_current_limit = wm831x_buckv_get_current_limit,
416e4ee831fSMark Brown 
417e4ee831fSMark Brown 	.is_enabled = wm831x_dcdc_is_enabled,
418e4ee831fSMark Brown 	.enable = wm831x_dcdc_enable,
419e4ee831fSMark Brown 	.disable = wm831x_dcdc_disable,
420e4ee831fSMark Brown 	.get_status = wm831x_dcdc_get_status,
421e4ee831fSMark Brown 	.get_mode = wm831x_dcdc_get_mode,
422e4ee831fSMark Brown 	.set_mode = wm831x_dcdc_set_mode,
423e4ee831fSMark Brown 	.set_suspend_mode = wm831x_dcdc_set_suspend_mode,
424e4ee831fSMark Brown };
425e4ee831fSMark Brown 
426e24a04c4SMark Brown /*
427e24a04c4SMark Brown  * Set up DVS control.  We just log errors since we can still run
428e24a04c4SMark Brown  * (with reduced performance) if we fail.
429e24a04c4SMark Brown  */
430e24a04c4SMark Brown static __devinit void wm831x_buckv_dvs_init(struct wm831x_dcdc *dcdc,
431e24a04c4SMark Brown 					    struct wm831x_buckv_pdata *pdata)
432e24a04c4SMark Brown {
433e24a04c4SMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
434e24a04c4SMark Brown 	int ret;
435e24a04c4SMark Brown 	u16 ctrl;
436e24a04c4SMark Brown 
437e24a04c4SMark Brown 	if (!pdata || !pdata->dvs_gpio)
438e24a04c4SMark Brown 		return;
439e24a04c4SMark Brown 
440e24a04c4SMark Brown 	ret = gpio_request(pdata->dvs_gpio, "DCDC DVS");
441e24a04c4SMark Brown 	if (ret < 0) {
442e24a04c4SMark Brown 		dev_err(wm831x->dev, "Failed to get %s DVS GPIO: %d\n",
443e24a04c4SMark Brown 			dcdc->name, ret);
444e24a04c4SMark Brown 		return;
445e24a04c4SMark Brown 	}
446e24a04c4SMark Brown 
447e24a04c4SMark Brown 	/* gpiolib won't let us read the GPIO status so pick the higher
448e24a04c4SMark Brown 	 * of the two existing voltages so we take it as platform data.
449e24a04c4SMark Brown 	 */
450e24a04c4SMark Brown 	dcdc->dvs_gpio_state = pdata->dvs_init_state;
451e24a04c4SMark Brown 
452e24a04c4SMark Brown 	ret = gpio_direction_output(pdata->dvs_gpio, dcdc->dvs_gpio_state);
453e24a04c4SMark Brown 	if (ret < 0) {
454e24a04c4SMark Brown 		dev_err(wm831x->dev, "Failed to enable %s DVS GPIO: %d\n",
455e24a04c4SMark Brown 			dcdc->name, ret);
456e24a04c4SMark Brown 		gpio_free(pdata->dvs_gpio);
457e24a04c4SMark Brown 		return;
458e24a04c4SMark Brown 	}
459e24a04c4SMark Brown 
460e24a04c4SMark Brown 	dcdc->dvs_gpio = pdata->dvs_gpio;
461b47ba9fdSMark Brown 
462b47ba9fdSMark Brown 	switch (pdata->dvs_control_src) {
463b47ba9fdSMark Brown 	case 1:
464b47ba9fdSMark Brown 		ctrl = 2 << WM831X_DC1_DVS_SRC_SHIFT;
465b47ba9fdSMark Brown 		break;
466b47ba9fdSMark Brown 	case 2:
467b47ba9fdSMark Brown 		ctrl = 3 << WM831X_DC1_DVS_SRC_SHIFT;
468b47ba9fdSMark Brown 		break;
469b47ba9fdSMark Brown 	default:
470b47ba9fdSMark Brown 		dev_err(wm831x->dev, "Invalid DVS control source %d for %s\n",
471b47ba9fdSMark Brown 			pdata->dvs_control_src, dcdc->name);
472b47ba9fdSMark Brown 		return;
473b47ba9fdSMark Brown 	}
474b47ba9fdSMark Brown 
475c439b8f4SMark Brown 	/* If DVS_VSEL is set to the minimum value then raise it to ON_VSEL
476c439b8f4SMark Brown 	 * to make bootstrapping a bit smoother.
477c439b8f4SMark Brown 	 */
478c439b8f4SMark Brown 	if (!dcdc->dvs_vsel) {
479c439b8f4SMark Brown 		ret = wm831x_set_bits(wm831x,
480c439b8f4SMark Brown 				      dcdc->base + WM831X_DCDC_DVS_CONTROL,
481c439b8f4SMark Brown 				      WM831X_DC1_DVS_VSEL_MASK, dcdc->on_vsel);
482c439b8f4SMark Brown 		if (ret == 0)
483c439b8f4SMark Brown 			dcdc->dvs_vsel = dcdc->on_vsel;
484c439b8f4SMark Brown 		else
485c439b8f4SMark Brown 			dev_warn(wm831x->dev, "Failed to set DVS_VSEL: %d\n",
486c439b8f4SMark Brown 				 ret);
487c439b8f4SMark Brown 	}
488c439b8f4SMark Brown 
489b47ba9fdSMark Brown 	ret = wm831x_set_bits(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL,
490b47ba9fdSMark Brown 			      WM831X_DC1_DVS_SRC_MASK, ctrl);
491b47ba9fdSMark Brown 	if (ret < 0) {
492b47ba9fdSMark Brown 		dev_err(wm831x->dev, "Failed to set %s DVS source: %d\n",
493b47ba9fdSMark Brown 			dcdc->name, ret);
494b47ba9fdSMark Brown 	}
495e24a04c4SMark Brown }
496e24a04c4SMark Brown 
497e4ee831fSMark Brown static __devinit int wm831x_buckv_probe(struct platform_device *pdev)
498e4ee831fSMark Brown {
499e4ee831fSMark Brown 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
500e4ee831fSMark Brown 	struct wm831x_pdata *pdata = wm831x->dev->platform_data;
501c172708dSMark Brown 	struct regulator_config config = { };
502137a6354SMark Brown 	int id;
503e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc;
504e4ee831fSMark Brown 	struct resource *res;
505e4ee831fSMark Brown 	int ret, irq;
506e4ee831fSMark Brown 
507137a6354SMark Brown 	if (pdata && pdata->wm831x_num)
508137a6354SMark Brown 		id = (pdata->wm831x_num * 10) + 1;
509137a6354SMark Brown 	else
510137a6354SMark Brown 		id = 0;
511137a6354SMark Brown 	id = pdev->id - id;
512137a6354SMark Brown 
513e4ee831fSMark Brown 	dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
514e4ee831fSMark Brown 
515e4ee831fSMark Brown 	if (pdata == NULL || pdata->dcdc[id] == NULL)
516e4ee831fSMark Brown 		return -ENODEV;
517e4ee831fSMark Brown 
518fded2f4fSMark Brown 	dcdc = devm_kzalloc(&pdev->dev,  sizeof(struct wm831x_dcdc),
519fded2f4fSMark Brown 			    GFP_KERNEL);
520e4ee831fSMark Brown 	if (dcdc == NULL) {
521e4ee831fSMark Brown 		dev_err(&pdev->dev, "Unable to allocate private data\n");
522e4ee831fSMark Brown 		return -ENOMEM;
523e4ee831fSMark Brown 	}
524e4ee831fSMark Brown 
525e4ee831fSMark Brown 	dcdc->wm831x = wm831x;
526e4ee831fSMark Brown 
527e4ee831fSMark Brown 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
528e4ee831fSMark Brown 	if (res == NULL) {
529e4ee831fSMark Brown 		dev_err(&pdev->dev, "No I/O resource\n");
530e4ee831fSMark Brown 		ret = -EINVAL;
531e4ee831fSMark Brown 		goto err;
532e4ee831fSMark Brown 	}
533e4ee831fSMark Brown 	dcdc->base = res->start;
534e4ee831fSMark Brown 
535e4ee831fSMark Brown 	snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
536e4ee831fSMark Brown 	dcdc->desc.name = dcdc->name;
537e4ee831fSMark Brown 	dcdc->desc.id = id;
538e4ee831fSMark Brown 	dcdc->desc.type = REGULATOR_VOLTAGE;
539e4ee831fSMark Brown 	dcdc->desc.n_voltages = WM831X_BUCKV_MAX_SELECTOR + 1;
540e4ee831fSMark Brown 	dcdc->desc.ops = &wm831x_buckv_ops;
541e4ee831fSMark Brown 	dcdc->desc.owner = THIS_MODULE;
542e4ee831fSMark Brown 
543e24a04c4SMark Brown 	ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG);
544e24a04c4SMark Brown 	if (ret < 0) {
545e24a04c4SMark Brown 		dev_err(wm831x->dev, "Failed to read ON VSEL: %d\n", ret);
546e24a04c4SMark Brown 		goto err;
547e24a04c4SMark Brown 	}
548e24a04c4SMark Brown 	dcdc->on_vsel = ret & WM831X_DC1_ON_VSEL_MASK;
549e24a04c4SMark Brown 
550a1b81dd3SMark Brown 	ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL);
551e24a04c4SMark Brown 	if (ret < 0) {
552e24a04c4SMark Brown 		dev_err(wm831x->dev, "Failed to read DVS VSEL: %d\n", ret);
553e24a04c4SMark Brown 		goto err;
554e24a04c4SMark Brown 	}
555e24a04c4SMark Brown 	dcdc->dvs_vsel = ret & WM831X_DC1_DVS_VSEL_MASK;
556e24a04c4SMark Brown 
557e24a04c4SMark Brown 	if (pdata->dcdc[id])
558e24a04c4SMark Brown 		wm831x_buckv_dvs_init(dcdc, pdata->dcdc[id]->driver_data);
559e24a04c4SMark Brown 
560c172708dSMark Brown 	config.dev = pdev->dev.parent;
561c172708dSMark Brown 	config.init_data = pdata->dcdc[id];
562c172708dSMark Brown 	config.driver_data = dcdc;
563c172708dSMark Brown 
564c172708dSMark Brown 	dcdc->regulator = regulator_register(&dcdc->desc, &config);
565e4ee831fSMark Brown 	if (IS_ERR(dcdc->regulator)) {
566e4ee831fSMark Brown 		ret = PTR_ERR(dcdc->regulator);
567e4ee831fSMark Brown 		dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
568e4ee831fSMark Brown 			id + 1, ret);
569e4ee831fSMark Brown 		goto err;
570e4ee831fSMark Brown 	}
571e4ee831fSMark Brown 
572e4ee831fSMark Brown 	irq = platform_get_irq_byname(pdev, "UV");
573dfda9c27SMark Brown 	ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq,
574dfda9c27SMark Brown 				   IRQF_TRIGGER_RISING, dcdc->name, dcdc);
575e4ee831fSMark Brown 	if (ret != 0) {
576e4ee831fSMark Brown 		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
577e4ee831fSMark Brown 			irq, ret);
578e4ee831fSMark Brown 		goto err_regulator;
579e4ee831fSMark Brown 	}
580e4ee831fSMark Brown 
581e4ee831fSMark Brown 	irq = platform_get_irq_byname(pdev, "HC");
582dfda9c27SMark Brown 	ret = request_threaded_irq(irq, NULL, wm831x_dcdc_oc_irq,
583dfda9c27SMark Brown 				   IRQF_TRIGGER_RISING, dcdc->name, dcdc);
584e4ee831fSMark Brown 	if (ret != 0) {
585e4ee831fSMark Brown 		dev_err(&pdev->dev, "Failed to request HC IRQ %d: %d\n",
586e4ee831fSMark Brown 			irq, ret);
587e4ee831fSMark Brown 		goto err_uv;
588e4ee831fSMark Brown 	}
589e4ee831fSMark Brown 
590e4ee831fSMark Brown 	platform_set_drvdata(pdev, dcdc);
591e4ee831fSMark Brown 
592e4ee831fSMark Brown 	return 0;
593e4ee831fSMark Brown 
594e4ee831fSMark Brown err_uv:
595dfda9c27SMark Brown 	free_irq(platform_get_irq_byname(pdev, "UV"), dcdc);
596e4ee831fSMark Brown err_regulator:
597e4ee831fSMark Brown 	regulator_unregister(dcdc->regulator);
598e4ee831fSMark Brown err:
599e24a04c4SMark Brown 	if (dcdc->dvs_gpio)
600e24a04c4SMark Brown 		gpio_free(dcdc->dvs_gpio);
601e4ee831fSMark Brown 	return ret;
602e4ee831fSMark Brown }
603e4ee831fSMark Brown 
604e4ee831fSMark Brown static __devexit int wm831x_buckv_remove(struct platform_device *pdev)
605e4ee831fSMark Brown {
606e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
607e4ee831fSMark Brown 
608eb66d565SDmitry Torokhov 	platform_set_drvdata(pdev, NULL);
609eb66d565SDmitry Torokhov 
61069952369SMark Brown 	free_irq(platform_get_irq_byname(pdev, "HC"), dcdc);
61169952369SMark Brown 	free_irq(platform_get_irq_byname(pdev, "UV"), dcdc);
612e4ee831fSMark Brown 	regulator_unregister(dcdc->regulator);
613e24a04c4SMark Brown 	if (dcdc->dvs_gpio)
614e24a04c4SMark Brown 		gpio_free(dcdc->dvs_gpio);
615e4ee831fSMark Brown 
616e4ee831fSMark Brown 	return 0;
617e4ee831fSMark Brown }
618e4ee831fSMark Brown 
619e4ee831fSMark Brown static struct platform_driver wm831x_buckv_driver = {
620e4ee831fSMark Brown 	.probe = wm831x_buckv_probe,
621e4ee831fSMark Brown 	.remove = __devexit_p(wm831x_buckv_remove),
622e4ee831fSMark Brown 	.driver		= {
623e4ee831fSMark Brown 		.name	= "wm831x-buckv",
624eb66d565SDmitry Torokhov 		.owner	= THIS_MODULE,
625e4ee831fSMark Brown 	},
626e4ee831fSMark Brown };
627e4ee831fSMark Brown 
628e4ee831fSMark Brown /*
629e4ee831fSMark Brown  * BUCKP specifics
630e4ee831fSMark Brown  */
631e4ee831fSMark Brown 
632e4ee831fSMark Brown static int wm831x_buckp_list_voltage(struct regulator_dev *rdev,
633e4ee831fSMark Brown 				      unsigned selector)
634e4ee831fSMark Brown {
635e4ee831fSMark Brown 	if (selector <= WM831X_BUCKP_MAX_SELECTOR)
636e4ee831fSMark Brown 		return 850000 + (selector * 25000);
637e4ee831fSMark Brown 	else
638e4ee831fSMark Brown 		return -EINVAL;
639e4ee831fSMark Brown }
640e4ee831fSMark Brown 
641e4ee831fSMark Brown static int wm831x_buckp_set_voltage_int(struct regulator_dev *rdev, int reg,
6423a93f2a9SMark Brown 					int min_uV, int max_uV, int *selector)
643e4ee831fSMark Brown {
644e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
645e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
646e4ee831fSMark Brown 	u16 vsel;
647e4ee831fSMark Brown 
648e4ee831fSMark Brown 	if (min_uV <= 34000000)
649e4ee831fSMark Brown 		vsel = (min_uV - 850000) / 25000;
650e4ee831fSMark Brown 	else
651e4ee831fSMark Brown 		return -EINVAL;
652e4ee831fSMark Brown 
653e4ee831fSMark Brown 	if (wm831x_buckp_list_voltage(rdev, vsel) > max_uV)
654e4ee831fSMark Brown 		return -EINVAL;
655e4ee831fSMark Brown 
6563a93f2a9SMark Brown 	*selector = vsel;
6573a93f2a9SMark Brown 
658e4ee831fSMark Brown 	return wm831x_set_bits(wm831x, reg, WM831X_DC3_ON_VSEL_MASK, vsel);
659e4ee831fSMark Brown }
660e4ee831fSMark Brown 
661e4ee831fSMark Brown static int wm831x_buckp_set_voltage(struct regulator_dev *rdev,
6623a93f2a9SMark Brown 				    int min_uV, int max_uV,
6633a93f2a9SMark Brown 				    unsigned *selector)
664e4ee831fSMark Brown {
665e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
666e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
667e4ee831fSMark Brown 
6683a93f2a9SMark Brown 	return wm831x_buckp_set_voltage_int(rdev, reg, min_uV, max_uV,
6693a93f2a9SMark Brown 					    selector);
670e4ee831fSMark Brown }
671e4ee831fSMark Brown 
672e4ee831fSMark Brown static int wm831x_buckp_set_suspend_voltage(struct regulator_dev *rdev,
673e4ee831fSMark Brown 					    int uV)
674e4ee831fSMark Brown {
675e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
676e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
6773a93f2a9SMark Brown 	unsigned selector;
678e4ee831fSMark Brown 
6793a93f2a9SMark Brown 	return wm831x_buckp_set_voltage_int(rdev, reg, uV, uV, &selector);
680e4ee831fSMark Brown }
681e4ee831fSMark Brown 
682e4ee831fSMark Brown static struct regulator_ops wm831x_buckp_ops = {
683e4ee831fSMark Brown 	.set_voltage = wm831x_buckp_set_voltage,
684817436e7SMark Brown 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
685e4ee831fSMark Brown 	.list_voltage = wm831x_buckp_list_voltage,
686e4ee831fSMark Brown 	.set_suspend_voltage = wm831x_buckp_set_suspend_voltage,
687e4ee831fSMark Brown 
688e4ee831fSMark Brown 	.is_enabled = wm831x_dcdc_is_enabled,
689e4ee831fSMark Brown 	.enable = wm831x_dcdc_enable,
690e4ee831fSMark Brown 	.disable = wm831x_dcdc_disable,
691e4ee831fSMark Brown 	.get_status = wm831x_dcdc_get_status,
692e4ee831fSMark Brown 	.get_mode = wm831x_dcdc_get_mode,
693e4ee831fSMark Brown 	.set_mode = wm831x_dcdc_set_mode,
694e4ee831fSMark Brown 	.set_suspend_mode = wm831x_dcdc_set_suspend_mode,
695e4ee831fSMark Brown };
696e4ee831fSMark Brown 
697e4ee831fSMark Brown static __devinit int wm831x_buckp_probe(struct platform_device *pdev)
698e4ee831fSMark Brown {
699e4ee831fSMark Brown 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
700e4ee831fSMark Brown 	struct wm831x_pdata *pdata = wm831x->dev->platform_data;
701c172708dSMark Brown 	struct regulator_config config = { };
702137a6354SMark Brown 	int id;
703e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc;
704e4ee831fSMark Brown 	struct resource *res;
705e4ee831fSMark Brown 	int ret, irq;
706e4ee831fSMark Brown 
707137a6354SMark Brown 	if (pdata && pdata->wm831x_num)
708137a6354SMark Brown 		id = (pdata->wm831x_num * 10) + 1;
709137a6354SMark Brown 	else
710137a6354SMark Brown 		id = 0;
711137a6354SMark Brown 	id = pdev->id - id;
712137a6354SMark Brown 
713e4ee831fSMark Brown 	dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
714e4ee831fSMark Brown 
715e4ee831fSMark Brown 	if (pdata == NULL || pdata->dcdc[id] == NULL)
716e4ee831fSMark Brown 		return -ENODEV;
717e4ee831fSMark Brown 
718fded2f4fSMark Brown 	dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc),
719fded2f4fSMark Brown 			    GFP_KERNEL);
720e4ee831fSMark Brown 	if (dcdc == NULL) {
721e4ee831fSMark Brown 		dev_err(&pdev->dev, "Unable to allocate private data\n");
722e4ee831fSMark Brown 		return -ENOMEM;
723e4ee831fSMark Brown 	}
724e4ee831fSMark Brown 
725e4ee831fSMark Brown 	dcdc->wm831x = wm831x;
726e4ee831fSMark Brown 
727e4ee831fSMark Brown 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
728e4ee831fSMark Brown 	if (res == NULL) {
729e4ee831fSMark Brown 		dev_err(&pdev->dev, "No I/O resource\n");
730e4ee831fSMark Brown 		ret = -EINVAL;
731e4ee831fSMark Brown 		goto err;
732e4ee831fSMark Brown 	}
733e4ee831fSMark Brown 	dcdc->base = res->start;
734e4ee831fSMark Brown 
735e4ee831fSMark Brown 	snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
736e4ee831fSMark Brown 	dcdc->desc.name = dcdc->name;
737e4ee831fSMark Brown 	dcdc->desc.id = id;
738e4ee831fSMark Brown 	dcdc->desc.type = REGULATOR_VOLTAGE;
739e4ee831fSMark Brown 	dcdc->desc.n_voltages = WM831X_BUCKP_MAX_SELECTOR + 1;
740e4ee831fSMark Brown 	dcdc->desc.ops = &wm831x_buckp_ops;
741e4ee831fSMark Brown 	dcdc->desc.owner = THIS_MODULE;
742817436e7SMark Brown 	dcdc->desc.vsel_reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
743817436e7SMark Brown 	dcdc->desc.vsel_mask = WM831X_DC3_ON_VSEL_MASK;
744e4ee831fSMark Brown 
745c172708dSMark Brown 	config.dev = pdev->dev.parent;
746c172708dSMark Brown 	config.init_data = pdata->dcdc[id];
747c172708dSMark Brown 	config.driver_data = dcdc;
748817436e7SMark Brown 	config.regmap = wm831x->regmap;
749c172708dSMark Brown 
750c172708dSMark Brown 	dcdc->regulator = regulator_register(&dcdc->desc, &config);
751e4ee831fSMark Brown 	if (IS_ERR(dcdc->regulator)) {
752e4ee831fSMark Brown 		ret = PTR_ERR(dcdc->regulator);
753e4ee831fSMark Brown 		dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
754e4ee831fSMark Brown 			id + 1, ret);
755e4ee831fSMark Brown 		goto err;
756e4ee831fSMark Brown 	}
757e4ee831fSMark Brown 
758e4ee831fSMark Brown 	irq = platform_get_irq_byname(pdev, "UV");
759dfda9c27SMark Brown 	ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq,
760dfda9c27SMark Brown 				   IRQF_TRIGGER_RISING,	dcdc->name, 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 	return ret;
775e4ee831fSMark Brown }
776e4ee831fSMark Brown 
777e4ee831fSMark Brown static __devexit int wm831x_buckp_remove(struct platform_device *pdev)
778e4ee831fSMark Brown {
779e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
780e4ee831fSMark Brown 
781eb66d565SDmitry Torokhov 	platform_set_drvdata(pdev, NULL);
782eb66d565SDmitry Torokhov 
78369952369SMark Brown 	free_irq(platform_get_irq_byname(pdev, "UV"), dcdc);
784e4ee831fSMark Brown 	regulator_unregister(dcdc->regulator);
785e4ee831fSMark Brown 
786e4ee831fSMark Brown 	return 0;
787e4ee831fSMark Brown }
788e4ee831fSMark Brown 
789e4ee831fSMark Brown static struct platform_driver wm831x_buckp_driver = {
790e4ee831fSMark Brown 	.probe = wm831x_buckp_probe,
791e4ee831fSMark Brown 	.remove = __devexit_p(wm831x_buckp_remove),
792e4ee831fSMark Brown 	.driver		= {
793e4ee831fSMark Brown 		.name	= "wm831x-buckp",
794eb66d565SDmitry Torokhov 		.owner	= THIS_MODULE,
795e4ee831fSMark Brown 	},
796e4ee831fSMark Brown };
797e4ee831fSMark Brown 
7988267a9baSMark Brown /*
7991304850dSMark Brown  * DCDC boost convertors
8001304850dSMark Brown  */
8011304850dSMark Brown 
8021304850dSMark Brown static int wm831x_boostp_get_status(struct regulator_dev *rdev)
8031304850dSMark Brown {
8041304850dSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
8051304850dSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
8061304850dSMark Brown 	int ret;
8071304850dSMark Brown 
8081304850dSMark Brown 	/* First, check for errors */
8091304850dSMark Brown 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
8101304850dSMark Brown 	if (ret < 0)
8111304850dSMark Brown 		return ret;
8121304850dSMark Brown 
8131304850dSMark Brown 	if (ret & (1 << rdev_get_id(rdev))) {
8141304850dSMark Brown 		dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
8151304850dSMark Brown 			rdev_get_id(rdev) + 1);
8161304850dSMark Brown 		return REGULATOR_STATUS_ERROR;
8171304850dSMark Brown 	}
8181304850dSMark Brown 
8191304850dSMark Brown 	/* Is the regulator on? */
8201304850dSMark Brown 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
8211304850dSMark Brown 	if (ret < 0)
8221304850dSMark Brown 		return ret;
8231304850dSMark Brown 	if (ret & (1 << rdev_get_id(rdev)))
8241304850dSMark Brown 		return REGULATOR_STATUS_ON;
8251304850dSMark Brown 	else
8261304850dSMark Brown 		return REGULATOR_STATUS_OFF;
8271304850dSMark Brown }
8281304850dSMark Brown 
8291304850dSMark Brown static struct regulator_ops wm831x_boostp_ops = {
8301304850dSMark Brown 	.get_status = wm831x_boostp_get_status,
8311304850dSMark Brown 
8321304850dSMark Brown 	.is_enabled = wm831x_dcdc_is_enabled,
8331304850dSMark Brown 	.enable = wm831x_dcdc_enable,
8341304850dSMark Brown 	.disable = wm831x_dcdc_disable,
8351304850dSMark Brown };
8361304850dSMark Brown 
8371304850dSMark Brown static __devinit int wm831x_boostp_probe(struct platform_device *pdev)
8381304850dSMark Brown {
8391304850dSMark Brown 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
8401304850dSMark Brown 	struct wm831x_pdata *pdata = wm831x->dev->platform_data;
841c172708dSMark Brown 	struct regulator_config config = { };
8421304850dSMark Brown 	int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
8431304850dSMark Brown 	struct wm831x_dcdc *dcdc;
8441304850dSMark Brown 	struct resource *res;
8451304850dSMark Brown 	int ret, irq;
8461304850dSMark Brown 
8471304850dSMark Brown 	dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
8481304850dSMark Brown 
8491304850dSMark Brown 	if (pdata == NULL || pdata->dcdc[id] == NULL)
8501304850dSMark Brown 		return -ENODEV;
8511304850dSMark Brown 
8524c60165dSAxel Lin 	dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), GFP_KERNEL);
8531304850dSMark Brown 	if (dcdc == NULL) {
8541304850dSMark Brown 		dev_err(&pdev->dev, "Unable to allocate private data\n");
8551304850dSMark Brown 		return -ENOMEM;
8561304850dSMark Brown 	}
8571304850dSMark Brown 
8581304850dSMark Brown 	dcdc->wm831x = wm831x;
8591304850dSMark Brown 
8601304850dSMark Brown 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
8611304850dSMark Brown 	if (res == NULL) {
8621304850dSMark Brown 		dev_err(&pdev->dev, "No I/O resource\n");
8631304850dSMark Brown 		ret = -EINVAL;
8641304850dSMark Brown 		goto err;
8651304850dSMark Brown 	}
8661304850dSMark Brown 	dcdc->base = res->start;
8671304850dSMark Brown 
8681304850dSMark Brown 	snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
8691304850dSMark Brown 	dcdc->desc.name = dcdc->name;
8701304850dSMark Brown 	dcdc->desc.id = id;
8711304850dSMark Brown 	dcdc->desc.type = REGULATOR_VOLTAGE;
8721304850dSMark Brown 	dcdc->desc.ops = &wm831x_boostp_ops;
8731304850dSMark Brown 	dcdc->desc.owner = THIS_MODULE;
8741304850dSMark Brown 
875c172708dSMark Brown 	config.dev = pdev->dev.parent;
876c172708dSMark Brown 	config.init_data = pdata->dcdc[id];
877c172708dSMark Brown 	config.driver_data = dcdc;
878c172708dSMark Brown 
879c172708dSMark Brown 	dcdc->regulator = regulator_register(&dcdc->desc, &config);
8801304850dSMark Brown 	if (IS_ERR(dcdc->regulator)) {
8811304850dSMark Brown 		ret = PTR_ERR(dcdc->regulator);
8821304850dSMark Brown 		dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
8831304850dSMark Brown 			id + 1, ret);
8841304850dSMark Brown 		goto err;
8851304850dSMark Brown 	}
8861304850dSMark Brown 
8871304850dSMark Brown 	irq = platform_get_irq_byname(pdev, "UV");
888dfda9c27SMark Brown 	ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq,
8891304850dSMark Brown 				   IRQF_TRIGGER_RISING, dcdc->name,
8901304850dSMark Brown 				   dcdc);
8911304850dSMark Brown 	if (ret != 0) {
8921304850dSMark Brown 		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
8931304850dSMark Brown 			irq, ret);
8941304850dSMark Brown 		goto err_regulator;
8951304850dSMark Brown 	}
8961304850dSMark Brown 
8971304850dSMark Brown 	platform_set_drvdata(pdev, dcdc);
8981304850dSMark Brown 
8991304850dSMark Brown 	return 0;
9001304850dSMark Brown 
9011304850dSMark Brown err_regulator:
9021304850dSMark Brown 	regulator_unregister(dcdc->regulator);
9031304850dSMark Brown err:
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 
911eb66d565SDmitry Torokhov 	platform_set_drvdata(pdev, NULL);
912eb66d565SDmitry Torokhov 
913dfda9c27SMark Brown 	free_irq(platform_get_irq_byname(pdev, "UV"), dcdc);
9141304850dSMark Brown 	regulator_unregister(dcdc->regulator);
9151304850dSMark Brown 
9161304850dSMark Brown 	return 0;
9171304850dSMark Brown }
9181304850dSMark Brown 
9191304850dSMark Brown static struct platform_driver wm831x_boostp_driver = {
9201304850dSMark Brown 	.probe = wm831x_boostp_probe,
9211304850dSMark Brown 	.remove = __devexit_p(wm831x_boostp_remove),
9221304850dSMark Brown 	.driver		= {
9231304850dSMark Brown 		.name	= "wm831x-boostp",
924eb66d565SDmitry Torokhov 		.owner	= THIS_MODULE,
9251304850dSMark Brown 	},
9261304850dSMark Brown };
9271304850dSMark Brown 
9281304850dSMark Brown /*
9298267a9baSMark Brown  * External Power Enable
9308267a9baSMark Brown  *
9318267a9baSMark Brown  * These aren't actually DCDCs but look like them in hardware so share
9328267a9baSMark Brown  * code.
9338267a9baSMark Brown  */
9348267a9baSMark Brown 
9358267a9baSMark Brown #define WM831X_EPE_BASE 6
9368267a9baSMark Brown 
9378267a9baSMark Brown static struct regulator_ops wm831x_epe_ops = {
9388267a9baSMark Brown 	.is_enabled = wm831x_dcdc_is_enabled,
9398267a9baSMark Brown 	.enable = wm831x_dcdc_enable,
9408267a9baSMark Brown 	.disable = wm831x_dcdc_disable,
9418267a9baSMark Brown 	.get_status = wm831x_dcdc_get_status,
9428267a9baSMark Brown };
9438267a9baSMark Brown 
9448267a9baSMark Brown static __devinit int wm831x_epe_probe(struct platform_device *pdev)
9458267a9baSMark Brown {
9468267a9baSMark Brown 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
9478267a9baSMark Brown 	struct wm831x_pdata *pdata = wm831x->dev->platform_data;
948c172708dSMark Brown 	struct regulator_config config = { };
9498267a9baSMark Brown 	int id = pdev->id % ARRAY_SIZE(pdata->epe);
9508267a9baSMark Brown 	struct wm831x_dcdc *dcdc;
9518267a9baSMark Brown 	int ret;
9528267a9baSMark Brown 
9538267a9baSMark Brown 	dev_dbg(&pdev->dev, "Probing EPE%d\n", id + 1);
9548267a9baSMark Brown 
9558267a9baSMark Brown 	if (pdata == NULL || pdata->epe[id] == NULL)
9568267a9baSMark Brown 		return -ENODEV;
9578267a9baSMark Brown 
9584c60165dSAxel Lin 	dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), GFP_KERNEL);
9598267a9baSMark Brown 	if (dcdc == NULL) {
9608267a9baSMark Brown 		dev_err(&pdev->dev, "Unable to allocate private data\n");
9618267a9baSMark Brown 		return -ENOMEM;
9628267a9baSMark Brown 	}
9638267a9baSMark Brown 
9648267a9baSMark Brown 	dcdc->wm831x = wm831x;
9658267a9baSMark Brown 
9668267a9baSMark Brown 	/* For current parts this is correct; probably need to revisit
9678267a9baSMark Brown 	 * in future.
9688267a9baSMark Brown 	 */
9698267a9baSMark Brown 	snprintf(dcdc->name, sizeof(dcdc->name), "EPE%d", id + 1);
9708267a9baSMark Brown 	dcdc->desc.name = dcdc->name;
9718267a9baSMark Brown 	dcdc->desc.id = id + WM831X_EPE_BASE; /* Offset in DCDC registers */
9728267a9baSMark Brown 	dcdc->desc.ops = &wm831x_epe_ops;
9738267a9baSMark Brown 	dcdc->desc.type = REGULATOR_VOLTAGE;
9748267a9baSMark Brown 	dcdc->desc.owner = THIS_MODULE;
9758267a9baSMark Brown 
976c172708dSMark Brown 	config.dev = pdev->dev.parent;
977c172708dSMark Brown 	config.init_data = pdata->epe[id];
978c172708dSMark Brown 	config.driver_data = dcdc;
979c172708dSMark Brown 
980c172708dSMark Brown 	dcdc->regulator = regulator_register(&dcdc->desc, &config);
9818267a9baSMark Brown 	if (IS_ERR(dcdc->regulator)) {
9828267a9baSMark Brown 		ret = PTR_ERR(dcdc->regulator);
9838267a9baSMark Brown 		dev_err(wm831x->dev, "Failed to register EPE%d: %d\n",
9848267a9baSMark Brown 			id + 1, ret);
9858267a9baSMark Brown 		goto err;
9868267a9baSMark Brown 	}
9878267a9baSMark Brown 
9888267a9baSMark Brown 	platform_set_drvdata(pdev, dcdc);
9898267a9baSMark Brown 
9908267a9baSMark Brown 	return 0;
9918267a9baSMark Brown 
9928267a9baSMark Brown err:
9938267a9baSMark Brown 	return ret;
9948267a9baSMark Brown }
9958267a9baSMark Brown 
9968267a9baSMark Brown static __devexit int wm831x_epe_remove(struct platform_device *pdev)
9978267a9baSMark Brown {
9988267a9baSMark Brown 	struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
9998267a9baSMark Brown 
1000eb66d565SDmitry Torokhov 	platform_set_drvdata(pdev, NULL);
10018267a9baSMark Brown 	regulator_unregister(dcdc->regulator);
10028267a9baSMark Brown 
10038267a9baSMark Brown 	return 0;
10048267a9baSMark Brown }
10058267a9baSMark Brown 
10068267a9baSMark Brown static struct platform_driver wm831x_epe_driver = {
10078267a9baSMark Brown 	.probe = wm831x_epe_probe,
10088267a9baSMark Brown 	.remove = __devexit_p(wm831x_epe_remove),
10098267a9baSMark Brown 	.driver		= {
10108267a9baSMark Brown 		.name	= "wm831x-epe",
1011eb66d565SDmitry Torokhov 		.owner	= THIS_MODULE,
10128267a9baSMark Brown 	},
10138267a9baSMark Brown };
10148267a9baSMark Brown 
1015e4ee831fSMark Brown static int __init wm831x_dcdc_init(void)
1016e4ee831fSMark Brown {
1017e4ee831fSMark Brown 	int ret;
1018e4ee831fSMark Brown 	ret = platform_driver_register(&wm831x_buckv_driver);
1019e4ee831fSMark Brown 	if (ret != 0)
1020e4ee831fSMark Brown 		pr_err("Failed to register WM831x BUCKV driver: %d\n", ret);
1021e4ee831fSMark Brown 
1022e4ee831fSMark Brown 	ret = platform_driver_register(&wm831x_buckp_driver);
1023e4ee831fSMark Brown 	if (ret != 0)
1024e4ee831fSMark Brown 		pr_err("Failed to register WM831x BUCKP driver: %d\n", ret);
1025e4ee831fSMark Brown 
10261304850dSMark Brown 	ret = platform_driver_register(&wm831x_boostp_driver);
10271304850dSMark Brown 	if (ret != 0)
10281304850dSMark Brown 		pr_err("Failed to register WM831x BOOST driver: %d\n", ret);
10291304850dSMark Brown 
10308267a9baSMark Brown 	ret = platform_driver_register(&wm831x_epe_driver);
10318267a9baSMark Brown 	if (ret != 0)
10328267a9baSMark Brown 		pr_err("Failed to register WM831x EPE driver: %d\n", ret);
10338267a9baSMark Brown 
1034e4ee831fSMark Brown 	return 0;
1035e4ee831fSMark Brown }
1036e4ee831fSMark Brown subsys_initcall(wm831x_dcdc_init);
1037e4ee831fSMark Brown 
1038e4ee831fSMark Brown static void __exit wm831x_dcdc_exit(void)
1039e4ee831fSMark Brown {
10408267a9baSMark Brown 	platform_driver_unregister(&wm831x_epe_driver);
10411304850dSMark Brown 	platform_driver_unregister(&wm831x_boostp_driver);
1042e4ee831fSMark Brown 	platform_driver_unregister(&wm831x_buckp_driver);
1043e4ee831fSMark Brown 	platform_driver_unregister(&wm831x_buckv_driver);
1044e4ee831fSMark Brown }
1045e4ee831fSMark Brown module_exit(wm831x_dcdc_exit);
1046e4ee831fSMark Brown 
1047e4ee831fSMark Brown /* Module information */
1048e4ee831fSMark Brown MODULE_AUTHOR("Mark Brown");
1049e4ee831fSMark Brown MODULE_DESCRIPTION("WM831x DC-DC convertor driver");
1050e4ee831fSMark Brown MODULE_LICENSE("GPL");
1051e4ee831fSMark Brown MODULE_ALIAS("platform:wm831x-buckv");
1052e4ee831fSMark Brown MODULE_ALIAS("platform:wm831x-buckp");
105324b43150SMark Brown MODULE_ALIAS("platform:wm831x-epe");
1054