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 int wm831x_dcdc_is_enabled(struct regulator_dev *rdev)
65e4ee831fSMark Brown {
66e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
67e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
68e4ee831fSMark Brown 	int mask = 1 << rdev_get_id(rdev);
69e4ee831fSMark Brown 	int reg;
70e4ee831fSMark Brown 
71e4ee831fSMark Brown 	reg = wm831x_reg_read(wm831x, WM831X_DCDC_ENABLE);
72e4ee831fSMark Brown 	if (reg < 0)
73e4ee831fSMark Brown 		return reg;
74e4ee831fSMark Brown 
75e4ee831fSMark Brown 	if (reg & mask)
76e4ee831fSMark Brown 		return 1;
77e4ee831fSMark Brown 	else
78e4ee831fSMark Brown 		return 0;
79e4ee831fSMark Brown }
80e4ee831fSMark Brown 
81e4ee831fSMark Brown static int wm831x_dcdc_enable(struct regulator_dev *rdev)
82e4ee831fSMark Brown {
83e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
84e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
85e4ee831fSMark Brown 	int mask = 1 << rdev_get_id(rdev);
86e4ee831fSMark Brown 
87e4ee831fSMark Brown 	return wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE, mask, mask);
88e4ee831fSMark Brown }
89e4ee831fSMark Brown 
90e4ee831fSMark Brown static int wm831x_dcdc_disable(struct regulator_dev *rdev)
91e4ee831fSMark Brown {
92e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
93e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
94e4ee831fSMark Brown 	int mask = 1 << rdev_get_id(rdev);
95e4ee831fSMark Brown 
96e4ee831fSMark Brown 	return wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE, mask, 0);
97e4ee831fSMark Brown }
98e4ee831fSMark Brown 
99e4ee831fSMark Brown static unsigned int wm831x_dcdc_get_mode(struct regulator_dev *rdev)
100e4ee831fSMark Brown 
101e4ee831fSMark Brown {
102e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
103e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
104e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
105e4ee831fSMark Brown 	int val;
106e4ee831fSMark Brown 
107e4ee831fSMark Brown 	val = wm831x_reg_read(wm831x, reg);
108e4ee831fSMark Brown 	if (val < 0)
109e4ee831fSMark Brown 		return val;
110e4ee831fSMark Brown 
111e4ee831fSMark Brown 	val = (val & WM831X_DC1_ON_MODE_MASK) >> WM831X_DC1_ON_MODE_SHIFT;
112e4ee831fSMark Brown 
113e4ee831fSMark Brown 	switch (val) {
114e4ee831fSMark Brown 	case WM831X_DCDC_MODE_FAST:
115e4ee831fSMark Brown 		return REGULATOR_MODE_FAST;
116e4ee831fSMark Brown 	case WM831X_DCDC_MODE_NORMAL:
117e4ee831fSMark Brown 		return REGULATOR_MODE_NORMAL;
118e4ee831fSMark Brown 	case WM831X_DCDC_MODE_STANDBY:
119e4ee831fSMark Brown 		return REGULATOR_MODE_STANDBY;
120e4ee831fSMark Brown 	case WM831X_DCDC_MODE_IDLE:
121e4ee831fSMark Brown 		return REGULATOR_MODE_IDLE;
122e4ee831fSMark Brown 	default:
123e4ee831fSMark Brown 		BUG();
1249ee291a4SMark Brown 		return -EINVAL;
125e4ee831fSMark Brown 	}
126e4ee831fSMark Brown }
127e4ee831fSMark Brown 
128e4ee831fSMark Brown static int wm831x_dcdc_set_mode_int(struct wm831x *wm831x, int reg,
129e4ee831fSMark Brown 				    unsigned int mode)
130e4ee831fSMark Brown {
131e4ee831fSMark Brown 	int val;
132e4ee831fSMark Brown 
133e4ee831fSMark Brown 	switch (mode) {
134e4ee831fSMark Brown 	case REGULATOR_MODE_FAST:
135e4ee831fSMark Brown 		val = WM831X_DCDC_MODE_FAST;
136e4ee831fSMark Brown 		break;
137e4ee831fSMark Brown 	case REGULATOR_MODE_NORMAL:
138e4ee831fSMark Brown 		val = WM831X_DCDC_MODE_NORMAL;
139e4ee831fSMark Brown 		break;
140e4ee831fSMark Brown 	case REGULATOR_MODE_STANDBY:
141e4ee831fSMark Brown 		val = WM831X_DCDC_MODE_STANDBY;
142e4ee831fSMark Brown 		break;
143e4ee831fSMark Brown 	case REGULATOR_MODE_IDLE:
144e4ee831fSMark Brown 		val = WM831X_DCDC_MODE_IDLE;
145e4ee831fSMark Brown 		break;
146e4ee831fSMark Brown 	default:
147e4ee831fSMark Brown 		return -EINVAL;
148e4ee831fSMark Brown 	}
149e4ee831fSMark Brown 
150e4ee831fSMark Brown 	return wm831x_set_bits(wm831x, reg, WM831X_DC1_ON_MODE_MASK,
151e4ee831fSMark Brown 			       val << WM831X_DC1_ON_MODE_SHIFT);
152e4ee831fSMark Brown }
153e4ee831fSMark Brown 
154e4ee831fSMark Brown static int wm831x_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
155e4ee831fSMark Brown {
156e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
157e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
158e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
159e4ee831fSMark Brown 
160e4ee831fSMark Brown 	return wm831x_dcdc_set_mode_int(wm831x, reg, mode);
161e4ee831fSMark Brown }
162e4ee831fSMark Brown 
163e4ee831fSMark Brown static int wm831x_dcdc_set_suspend_mode(struct regulator_dev *rdev,
164e4ee831fSMark Brown 					unsigned int mode)
165e4ee831fSMark Brown {
166e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
167e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
168e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
169e4ee831fSMark Brown 
170e4ee831fSMark Brown 	return wm831x_dcdc_set_mode_int(wm831x, reg, mode);
171e4ee831fSMark Brown }
172e4ee831fSMark Brown 
173e4ee831fSMark Brown static int wm831x_dcdc_get_status(struct regulator_dev *rdev)
174e4ee831fSMark Brown {
175e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
176e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
177e4ee831fSMark Brown 	int ret;
178e4ee831fSMark Brown 
179e4ee831fSMark Brown 	/* First, check for errors */
180e4ee831fSMark Brown 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
181e4ee831fSMark Brown 	if (ret < 0)
182e4ee831fSMark Brown 		return ret;
183e4ee831fSMark Brown 
184e4ee831fSMark Brown 	if (ret & (1 << rdev_get_id(rdev))) {
185e4ee831fSMark Brown 		dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
186e4ee831fSMark Brown 			rdev_get_id(rdev) + 1);
187e4ee831fSMark Brown 		return REGULATOR_STATUS_ERROR;
188e4ee831fSMark Brown 	}
189e4ee831fSMark Brown 
190e4ee831fSMark Brown 	/* DCDC1 and DCDC2 can additionally detect high voltage/current */
191e4ee831fSMark Brown 	if (rdev_get_id(rdev) < 2) {
192e4ee831fSMark Brown 		if (ret & (WM831X_DC1_OV_STS << rdev_get_id(rdev))) {
193e4ee831fSMark Brown 			dev_dbg(wm831x->dev, "DCDC%d over voltage\n",
194e4ee831fSMark Brown 				rdev_get_id(rdev) + 1);
195e4ee831fSMark Brown 			return REGULATOR_STATUS_ERROR;
196e4ee831fSMark Brown 		}
197e4ee831fSMark Brown 
198e4ee831fSMark Brown 		if (ret & (WM831X_DC1_HC_STS << rdev_get_id(rdev))) {
199e4ee831fSMark Brown 			dev_dbg(wm831x->dev, "DCDC%d over current\n",
200e4ee831fSMark Brown 				rdev_get_id(rdev) + 1);
201e4ee831fSMark Brown 			return REGULATOR_STATUS_ERROR;
202e4ee831fSMark Brown 		}
203e4ee831fSMark Brown 	}
204e4ee831fSMark Brown 
205e4ee831fSMark Brown 	/* Is the regulator on? */
206e4ee831fSMark Brown 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
207e4ee831fSMark Brown 	if (ret < 0)
208e4ee831fSMark Brown 		return ret;
209e4ee831fSMark Brown 	if (!(ret & (1 << rdev_get_id(rdev))))
210e4ee831fSMark Brown 		return REGULATOR_STATUS_OFF;
211e4ee831fSMark Brown 
212e4ee831fSMark Brown 	/* TODO: When we handle hardware control modes so we can report the
213e4ee831fSMark Brown 	 * current mode. */
214e4ee831fSMark Brown 	return REGULATOR_STATUS_ON;
215e4ee831fSMark Brown }
216e4ee831fSMark Brown 
217e4ee831fSMark Brown static irqreturn_t wm831x_dcdc_uv_irq(int irq, void *data)
218e4ee831fSMark Brown {
219e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = data;
220e4ee831fSMark Brown 
221e4ee831fSMark Brown 	regulator_notifier_call_chain(dcdc->regulator,
222e4ee831fSMark Brown 				      REGULATOR_EVENT_UNDER_VOLTAGE,
223e4ee831fSMark Brown 				      NULL);
224e4ee831fSMark Brown 
225e4ee831fSMark Brown 	return IRQ_HANDLED;
226e4ee831fSMark Brown }
227e4ee831fSMark Brown 
228e4ee831fSMark Brown static irqreturn_t wm831x_dcdc_oc_irq(int irq, void *data)
229e4ee831fSMark Brown {
230e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = data;
231e4ee831fSMark Brown 
232e4ee831fSMark Brown 	regulator_notifier_call_chain(dcdc->regulator,
233e4ee831fSMark Brown 				      REGULATOR_EVENT_OVER_CURRENT,
234e4ee831fSMark Brown 				      NULL);
235e4ee831fSMark Brown 
236e4ee831fSMark Brown 	return IRQ_HANDLED;
237e4ee831fSMark Brown }
238e4ee831fSMark Brown 
239e4ee831fSMark Brown /*
240e4ee831fSMark Brown  * BUCKV specifics
241e4ee831fSMark Brown  */
242e4ee831fSMark Brown 
243e4ee831fSMark Brown static int wm831x_buckv_list_voltage(struct regulator_dev *rdev,
244e4ee831fSMark Brown 				      unsigned selector)
245e4ee831fSMark Brown {
246e4ee831fSMark Brown 	if (selector <= 0x8)
247e4ee831fSMark Brown 		return 600000;
248e4ee831fSMark Brown 	if (selector <= WM831X_BUCKV_MAX_SELECTOR)
249e4ee831fSMark Brown 		return 600000 + ((selector - 0x8) * 12500);
250e4ee831fSMark Brown 	return -EINVAL;
251e4ee831fSMark Brown }
252e4ee831fSMark Brown 
253e24a04c4SMark Brown static int wm831x_buckv_select_min_voltage(struct regulator_dev *rdev,
254e4ee831fSMark Brown 					   int min_uV, int max_uV)
255e4ee831fSMark Brown {
256e4ee831fSMark Brown 	u16 vsel;
257e4ee831fSMark Brown 
258e4ee831fSMark Brown 	if (min_uV < 600000)
259e4ee831fSMark Brown 		vsel = 0;
260e4ee831fSMark Brown 	else if (min_uV <= 1800000)
261e4ee831fSMark Brown 		vsel = ((min_uV - 600000) / 12500) + 8;
262e4ee831fSMark Brown 	else
263e4ee831fSMark Brown 		return -EINVAL;
264e4ee831fSMark Brown 
265e4ee831fSMark Brown 	if (wm831x_buckv_list_voltage(rdev, vsel) > max_uV)
266e4ee831fSMark Brown 		return -EINVAL;
267e4ee831fSMark Brown 
268e24a04c4SMark Brown 	return vsel;
269e24a04c4SMark Brown }
270e24a04c4SMark Brown 
271e24a04c4SMark Brown static int wm831x_buckv_set_dvs(struct regulator_dev *rdev, int state)
272e24a04c4SMark Brown {
273e24a04c4SMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
274e24a04c4SMark Brown 
275e24a04c4SMark Brown 	if (state == dcdc->dvs_gpio_state)
276e24a04c4SMark Brown 		return 0;
277e24a04c4SMark Brown 
278e24a04c4SMark Brown 	dcdc->dvs_gpio_state = state;
279e24a04c4SMark Brown 	gpio_set_value(dcdc->dvs_gpio, state);
280e24a04c4SMark Brown 
281e24a04c4SMark Brown 	/* Should wait for DVS state change to be asserted if we have
282e24a04c4SMark Brown 	 * a GPIO for it, for now assume the device is configured
283e24a04c4SMark Brown 	 * for the fastest possible transition.
284e24a04c4SMark Brown 	 */
285e24a04c4SMark Brown 
286e24a04c4SMark Brown 	return 0;
287e4ee831fSMark Brown }
288e4ee831fSMark Brown 
289e4ee831fSMark Brown static int wm831x_buckv_set_voltage(struct regulator_dev *rdev,
2903a93f2a9SMark Brown 				    int min_uV, int max_uV, unsigned *selector)
291e4ee831fSMark Brown {
292e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
293e24a04c4SMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
294e24a04c4SMark Brown 	int on_reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
295e24a04c4SMark Brown 	int dvs_reg = dcdc->base + WM831X_DCDC_DVS_CONTROL;
296e24a04c4SMark Brown 	int vsel, ret;
297e4ee831fSMark Brown 
298e24a04c4SMark Brown 	vsel = wm831x_buckv_select_min_voltage(rdev, min_uV, max_uV);
299e24a04c4SMark Brown 	if (vsel < 0)
300e24a04c4SMark Brown 		return vsel;
301e24a04c4SMark Brown 
3023a93f2a9SMark Brown 	*selector = vsel;
3033a93f2a9SMark Brown 
304e24a04c4SMark Brown 	/* If this value is already set then do a GPIO update if we can */
305e24a04c4SMark Brown 	if (dcdc->dvs_gpio && dcdc->on_vsel == vsel)
306e24a04c4SMark Brown 		return wm831x_buckv_set_dvs(rdev, 0);
307e24a04c4SMark Brown 
308e24a04c4SMark Brown 	if (dcdc->dvs_gpio && dcdc->dvs_vsel == vsel)
309e24a04c4SMark Brown 		return wm831x_buckv_set_dvs(rdev, 1);
310e24a04c4SMark Brown 
311e24a04c4SMark Brown 	/* Always set the ON status to the minimum voltage */
312e24a04c4SMark Brown 	ret = wm831x_set_bits(wm831x, on_reg, WM831X_DC1_ON_VSEL_MASK, vsel);
313e24a04c4SMark Brown 	if (ret < 0)
314e24a04c4SMark Brown 		return ret;
315e24a04c4SMark Brown 	dcdc->on_vsel = vsel;
316e24a04c4SMark Brown 
317e24a04c4SMark Brown 	if (!dcdc->dvs_gpio)
318e24a04c4SMark Brown 		return ret;
319e24a04c4SMark Brown 
320e24a04c4SMark Brown 	/* Kick the voltage transition now */
321e24a04c4SMark Brown 	ret = wm831x_buckv_set_dvs(rdev, 0);
322e24a04c4SMark Brown 	if (ret < 0)
323e24a04c4SMark Brown 		return ret;
324e24a04c4SMark Brown 
32588cda60eSMark Brown 	/*
32688cda60eSMark Brown 	 * If this VSEL is higher than the last one we've seen then
32788cda60eSMark Brown 	 * remember it as the DVS VSEL.  This is optimised for CPUfreq
32888cda60eSMark Brown 	 * usage where we want to get to the highest voltage very
32988cda60eSMark Brown 	 * quickly.
33088cda60eSMark Brown 	 */
33188cda60eSMark Brown 	if (vsel > dcdc->dvs_vsel) {
33288cda60eSMark Brown 		ret = wm831x_set_bits(wm831x, dvs_reg,
33388cda60eSMark Brown 				      WM831X_DC1_DVS_VSEL_MASK,
33488cda60eSMark Brown 				      dcdc->dvs_vsel);
335e24a04c4SMark Brown 		if (ret == 0)
336e24a04c4SMark Brown 			dcdc->dvs_vsel = vsel;
337e24a04c4SMark Brown 		else
33888cda60eSMark Brown 			dev_warn(wm831x->dev,
33988cda60eSMark Brown 				 "Failed to set DCDC DVS VSEL: %d\n", ret);
34088cda60eSMark Brown 	}
341e24a04c4SMark Brown 
342e24a04c4SMark Brown 	return 0;
343e4ee831fSMark Brown }
344e4ee831fSMark Brown 
345e4ee831fSMark Brown static int wm831x_buckv_set_suspend_voltage(struct regulator_dev *rdev,
346e4ee831fSMark Brown 					    int uV)
347e4ee831fSMark Brown {
348e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
349e24a04c4SMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
350e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
351e24a04c4SMark Brown 	int vsel;
352e4ee831fSMark Brown 
353e24a04c4SMark Brown 	vsel = wm831x_buckv_select_min_voltage(rdev, uV, uV);
354e24a04c4SMark Brown 	if (vsel < 0)
355e24a04c4SMark Brown 		return vsel;
356e24a04c4SMark Brown 
357e24a04c4SMark Brown 	return wm831x_set_bits(wm831x, reg, WM831X_DC1_SLP_VSEL_MASK, vsel);
358e4ee831fSMark Brown }
359e4ee831fSMark Brown 
360afb8bb80SMark Brown static int wm831x_buckv_get_voltage_sel(struct regulator_dev *rdev)
361e4ee831fSMark Brown {
362e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
363e4ee831fSMark Brown 
364e24a04c4SMark Brown 	if (dcdc->dvs_gpio && dcdc->dvs_gpio_state)
365afb8bb80SMark Brown 		return dcdc->dvs_vsel;
366e24a04c4SMark Brown 	else
367afb8bb80SMark Brown 		return dcdc->on_vsel;
368e4ee831fSMark Brown }
369e4ee831fSMark Brown 
370e4ee831fSMark Brown /* Current limit options */
371e4ee831fSMark Brown static u16 wm831x_dcdc_ilim[] = {
372e4ee831fSMark Brown 	125, 250, 375, 500, 625, 750, 875, 1000
373e4ee831fSMark Brown };
374e4ee831fSMark Brown 
375e4ee831fSMark Brown static int wm831x_buckv_set_current_limit(struct regulator_dev *rdev,
376e4ee831fSMark Brown 					   int min_uA, int max_uA)
377e4ee831fSMark Brown {
378e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
379e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
380e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2;
381e4ee831fSMark Brown 	int i;
382e4ee831fSMark Brown 
383e4ee831fSMark Brown 	for (i = 0; i < ARRAY_SIZE(wm831x_dcdc_ilim); i++) {
384a171e782SAxel Lin 		if ((min_uA <= wm831x_dcdc_ilim[i]) &&
385a171e782SAxel Lin 		    (wm831x_dcdc_ilim[i] <= max_uA))
386e4ee831fSMark Brown 			break;
387e4ee831fSMark Brown 	}
388e4ee831fSMark Brown 	if (i == ARRAY_SIZE(wm831x_dcdc_ilim))
389e4ee831fSMark Brown 		return -EINVAL;
390e4ee831fSMark Brown 
39109bf14b9SAxel Lin 	return wm831x_set_bits(wm831x, reg, WM831X_DC1_HC_THR_MASK,
39209bf14b9SAxel Lin 			       i << WM831X_DC1_HC_THR_SHIFT);
393e4ee831fSMark Brown }
394e4ee831fSMark Brown 
395e4ee831fSMark Brown static int wm831x_buckv_get_current_limit(struct regulator_dev *rdev)
396e4ee831fSMark Brown {
397e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
398e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
399e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2;
400e4ee831fSMark Brown 	int val;
401e4ee831fSMark Brown 
402e4ee831fSMark Brown 	val = wm831x_reg_read(wm831x, reg);
403e4ee831fSMark Brown 	if (val < 0)
404e4ee831fSMark Brown 		return val;
405e4ee831fSMark Brown 
40609bf14b9SAxel Lin 	val = (val & WM831X_DC1_HC_THR_MASK) >> WM831X_DC1_HC_THR_SHIFT;
40709bf14b9SAxel Lin 	return wm831x_dcdc_ilim[val];
408e4ee831fSMark Brown }
409e4ee831fSMark Brown 
410e4ee831fSMark Brown static struct regulator_ops wm831x_buckv_ops = {
411e4ee831fSMark Brown 	.set_voltage = wm831x_buckv_set_voltage,
412afb8bb80SMark Brown 	.get_voltage_sel = wm831x_buckv_get_voltage_sel,
413e4ee831fSMark Brown 	.list_voltage = wm831x_buckv_list_voltage,
414e4ee831fSMark Brown 	.set_suspend_voltage = wm831x_buckv_set_suspend_voltage,
415e4ee831fSMark Brown 	.set_current_limit = wm831x_buckv_set_current_limit,
416e4ee831fSMark Brown 	.get_current_limit = wm831x_buckv_get_current_limit,
417e4ee831fSMark Brown 
418e4ee831fSMark Brown 	.is_enabled = wm831x_dcdc_is_enabled,
419e4ee831fSMark Brown 	.enable = wm831x_dcdc_enable,
420e4ee831fSMark Brown 	.disable = wm831x_dcdc_disable,
421e4ee831fSMark Brown 	.get_status = wm831x_dcdc_get_status,
422e4ee831fSMark Brown 	.get_mode = wm831x_dcdc_get_mode,
423e4ee831fSMark Brown 	.set_mode = wm831x_dcdc_set_mode,
424e4ee831fSMark Brown 	.set_suspend_mode = wm831x_dcdc_set_suspend_mode,
425e4ee831fSMark Brown };
426e4ee831fSMark Brown 
427e24a04c4SMark Brown /*
428e24a04c4SMark Brown  * Set up DVS control.  We just log errors since we can still run
429e24a04c4SMark Brown  * (with reduced performance) if we fail.
430e24a04c4SMark Brown  */
431e24a04c4SMark Brown static __devinit void wm831x_buckv_dvs_init(struct wm831x_dcdc *dcdc,
432e24a04c4SMark Brown 					    struct wm831x_buckv_pdata *pdata)
433e24a04c4SMark Brown {
434e24a04c4SMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
435e24a04c4SMark Brown 	int ret;
436e24a04c4SMark Brown 	u16 ctrl;
437e24a04c4SMark Brown 
438e24a04c4SMark Brown 	if (!pdata || !pdata->dvs_gpio)
439e24a04c4SMark Brown 		return;
440e24a04c4SMark Brown 
441e24a04c4SMark Brown 	ret = gpio_request(pdata->dvs_gpio, "DCDC DVS");
442e24a04c4SMark Brown 	if (ret < 0) {
443e24a04c4SMark Brown 		dev_err(wm831x->dev, "Failed to get %s DVS GPIO: %d\n",
444e24a04c4SMark Brown 			dcdc->name, ret);
445e24a04c4SMark Brown 		return;
446e24a04c4SMark Brown 	}
447e24a04c4SMark Brown 
448e24a04c4SMark Brown 	/* gpiolib won't let us read the GPIO status so pick the higher
449e24a04c4SMark Brown 	 * of the two existing voltages so we take it as platform data.
450e24a04c4SMark Brown 	 */
451e24a04c4SMark Brown 	dcdc->dvs_gpio_state = pdata->dvs_init_state;
452e24a04c4SMark Brown 
453e24a04c4SMark Brown 	ret = gpio_direction_output(pdata->dvs_gpio, dcdc->dvs_gpio_state);
454e24a04c4SMark Brown 	if (ret < 0) {
455e24a04c4SMark Brown 		dev_err(wm831x->dev, "Failed to enable %s DVS GPIO: %d\n",
456e24a04c4SMark Brown 			dcdc->name, ret);
457e24a04c4SMark Brown 		gpio_free(pdata->dvs_gpio);
458e24a04c4SMark Brown 		return;
459e24a04c4SMark Brown 	}
460e24a04c4SMark Brown 
461e24a04c4SMark Brown 	dcdc->dvs_gpio = pdata->dvs_gpio;
462b47ba9fdSMark Brown 
463b47ba9fdSMark Brown 	switch (pdata->dvs_control_src) {
464b47ba9fdSMark Brown 	case 1:
465b47ba9fdSMark Brown 		ctrl = 2 << WM831X_DC1_DVS_SRC_SHIFT;
466b47ba9fdSMark Brown 		break;
467b47ba9fdSMark Brown 	case 2:
468b47ba9fdSMark Brown 		ctrl = 3 << WM831X_DC1_DVS_SRC_SHIFT;
469b47ba9fdSMark Brown 		break;
470b47ba9fdSMark Brown 	default:
471b47ba9fdSMark Brown 		dev_err(wm831x->dev, "Invalid DVS control source %d for %s\n",
472b47ba9fdSMark Brown 			pdata->dvs_control_src, dcdc->name);
473b47ba9fdSMark Brown 		return;
474b47ba9fdSMark Brown 	}
475b47ba9fdSMark Brown 
476c439b8f4SMark Brown 	/* If DVS_VSEL is set to the minimum value then raise it to ON_VSEL
477c439b8f4SMark Brown 	 * to make bootstrapping a bit smoother.
478c439b8f4SMark Brown 	 */
479c439b8f4SMark Brown 	if (!dcdc->dvs_vsel) {
480c439b8f4SMark Brown 		ret = wm831x_set_bits(wm831x,
481c439b8f4SMark Brown 				      dcdc->base + WM831X_DCDC_DVS_CONTROL,
482c439b8f4SMark Brown 				      WM831X_DC1_DVS_VSEL_MASK, dcdc->on_vsel);
483c439b8f4SMark Brown 		if (ret == 0)
484c439b8f4SMark Brown 			dcdc->dvs_vsel = dcdc->on_vsel;
485c439b8f4SMark Brown 		else
486c439b8f4SMark Brown 			dev_warn(wm831x->dev, "Failed to set DVS_VSEL: %d\n",
487c439b8f4SMark Brown 				 ret);
488c439b8f4SMark Brown 	}
489c439b8f4SMark Brown 
490b47ba9fdSMark Brown 	ret = wm831x_set_bits(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL,
491b47ba9fdSMark Brown 			      WM831X_DC1_DVS_SRC_MASK, ctrl);
492b47ba9fdSMark Brown 	if (ret < 0) {
493b47ba9fdSMark Brown 		dev_err(wm831x->dev, "Failed to set %s DVS source: %d\n",
494b47ba9fdSMark Brown 			dcdc->name, ret);
495b47ba9fdSMark Brown 	}
496e24a04c4SMark Brown }
497e24a04c4SMark Brown 
498e4ee831fSMark Brown static __devinit int wm831x_buckv_probe(struct platform_device *pdev)
499e4ee831fSMark Brown {
500e4ee831fSMark Brown 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
501e4ee831fSMark Brown 	struct wm831x_pdata *pdata = wm831x->dev->platform_data;
502c172708dSMark Brown 	struct regulator_config config = { };
503137a6354SMark Brown 	int id;
504e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc;
505e4ee831fSMark Brown 	struct resource *res;
506e4ee831fSMark Brown 	int ret, irq;
507e4ee831fSMark Brown 
508137a6354SMark Brown 	if (pdata && pdata->wm831x_num)
509137a6354SMark Brown 		id = (pdata->wm831x_num * 10) + 1;
510137a6354SMark Brown 	else
511137a6354SMark Brown 		id = 0;
512137a6354SMark Brown 	id = pdev->id - id;
513137a6354SMark Brown 
514e4ee831fSMark Brown 	dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
515e4ee831fSMark Brown 
516e4ee831fSMark Brown 	if (pdata == NULL || pdata->dcdc[id] == NULL)
517e4ee831fSMark Brown 		return -ENODEV;
518e4ee831fSMark Brown 
519fded2f4fSMark Brown 	dcdc = devm_kzalloc(&pdev->dev,  sizeof(struct wm831x_dcdc),
520fded2f4fSMark Brown 			    GFP_KERNEL);
521e4ee831fSMark Brown 	if (dcdc == NULL) {
522e4ee831fSMark Brown 		dev_err(&pdev->dev, "Unable to allocate private data\n");
523e4ee831fSMark Brown 		return -ENOMEM;
524e4ee831fSMark Brown 	}
525e4ee831fSMark Brown 
526e4ee831fSMark Brown 	dcdc->wm831x = wm831x;
527e4ee831fSMark Brown 
528e4ee831fSMark Brown 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
529e4ee831fSMark Brown 	if (res == NULL) {
530e4ee831fSMark Brown 		dev_err(&pdev->dev, "No I/O resource\n");
531e4ee831fSMark Brown 		ret = -EINVAL;
532e4ee831fSMark Brown 		goto err;
533e4ee831fSMark Brown 	}
534e4ee831fSMark Brown 	dcdc->base = res->start;
535e4ee831fSMark Brown 
536e4ee831fSMark Brown 	snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
537e4ee831fSMark Brown 	dcdc->desc.name = dcdc->name;
53882caa978SMark Brown 
53982caa978SMark Brown 	snprintf(dcdc->supply_name, sizeof(dcdc->supply_name),
54082caa978SMark Brown 		 "DC%dVDD", id + 1);
54182caa978SMark Brown 	dcdc->desc.supply_name = dcdc->supply_name;
54282caa978SMark Brown 
543e4ee831fSMark Brown 	dcdc->desc.id = id;
544e4ee831fSMark Brown 	dcdc->desc.type = REGULATOR_VOLTAGE;
545e4ee831fSMark Brown 	dcdc->desc.n_voltages = WM831X_BUCKV_MAX_SELECTOR + 1;
546e4ee831fSMark Brown 	dcdc->desc.ops = &wm831x_buckv_ops;
547e4ee831fSMark Brown 	dcdc->desc.owner = THIS_MODULE;
548e4ee831fSMark Brown 
549e24a04c4SMark Brown 	ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG);
550e24a04c4SMark Brown 	if (ret < 0) {
551e24a04c4SMark Brown 		dev_err(wm831x->dev, "Failed to read ON VSEL: %d\n", ret);
552e24a04c4SMark Brown 		goto err;
553e24a04c4SMark Brown 	}
554e24a04c4SMark Brown 	dcdc->on_vsel = ret & WM831X_DC1_ON_VSEL_MASK;
555e24a04c4SMark Brown 
556a1b81dd3SMark Brown 	ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL);
557e24a04c4SMark Brown 	if (ret < 0) {
558e24a04c4SMark Brown 		dev_err(wm831x->dev, "Failed to read DVS VSEL: %d\n", ret);
559e24a04c4SMark Brown 		goto err;
560e24a04c4SMark Brown 	}
561e24a04c4SMark Brown 	dcdc->dvs_vsel = ret & WM831X_DC1_DVS_VSEL_MASK;
562e24a04c4SMark Brown 
563e24a04c4SMark Brown 	if (pdata->dcdc[id])
564e24a04c4SMark Brown 		wm831x_buckv_dvs_init(dcdc, pdata->dcdc[id]->driver_data);
565e24a04c4SMark Brown 
566c172708dSMark Brown 	config.dev = pdev->dev.parent;
567c172708dSMark Brown 	config.init_data = pdata->dcdc[id];
568c172708dSMark Brown 	config.driver_data = dcdc;
569c172708dSMark Brown 
570c172708dSMark Brown 	dcdc->regulator = regulator_register(&dcdc->desc, &config);
571e4ee831fSMark Brown 	if (IS_ERR(dcdc->regulator)) {
572e4ee831fSMark Brown 		ret = PTR_ERR(dcdc->regulator);
573e4ee831fSMark Brown 		dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
574e4ee831fSMark Brown 			id + 1, ret);
575e4ee831fSMark Brown 		goto err;
576e4ee831fSMark Brown 	}
577e4ee831fSMark Brown 
578e4ee831fSMark Brown 	irq = platform_get_irq_byname(pdev, "UV");
579dfda9c27SMark Brown 	ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq,
580dfda9c27SMark Brown 				   IRQF_TRIGGER_RISING, dcdc->name, dcdc);
581e4ee831fSMark Brown 	if (ret != 0) {
582e4ee831fSMark Brown 		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
583e4ee831fSMark Brown 			irq, ret);
584e4ee831fSMark Brown 		goto err_regulator;
585e4ee831fSMark Brown 	}
586e4ee831fSMark Brown 
587e4ee831fSMark Brown 	irq = platform_get_irq_byname(pdev, "HC");
588dfda9c27SMark Brown 	ret = request_threaded_irq(irq, NULL, wm831x_dcdc_oc_irq,
589dfda9c27SMark Brown 				   IRQF_TRIGGER_RISING, dcdc->name, dcdc);
590e4ee831fSMark Brown 	if (ret != 0) {
591e4ee831fSMark Brown 		dev_err(&pdev->dev, "Failed to request HC IRQ %d: %d\n",
592e4ee831fSMark Brown 			irq, ret);
593e4ee831fSMark Brown 		goto err_uv;
594e4ee831fSMark Brown 	}
595e4ee831fSMark Brown 
596e4ee831fSMark Brown 	platform_set_drvdata(pdev, dcdc);
597e4ee831fSMark Brown 
598e4ee831fSMark Brown 	return 0;
599e4ee831fSMark Brown 
600e4ee831fSMark Brown err_uv:
601dfda9c27SMark Brown 	free_irq(platform_get_irq_byname(pdev, "UV"), dcdc);
602e4ee831fSMark Brown err_regulator:
603e4ee831fSMark Brown 	regulator_unregister(dcdc->regulator);
604e4ee831fSMark Brown err:
605e24a04c4SMark Brown 	if (dcdc->dvs_gpio)
606e24a04c4SMark Brown 		gpio_free(dcdc->dvs_gpio);
607e4ee831fSMark Brown 	return ret;
608e4ee831fSMark Brown }
609e4ee831fSMark Brown 
610e4ee831fSMark Brown static __devexit int wm831x_buckv_remove(struct platform_device *pdev)
611e4ee831fSMark Brown {
612e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
613e4ee831fSMark Brown 
614eb66d565SDmitry Torokhov 	platform_set_drvdata(pdev, NULL);
615eb66d565SDmitry Torokhov 
61669952369SMark Brown 	free_irq(platform_get_irq_byname(pdev, "HC"), dcdc);
61769952369SMark Brown 	free_irq(platform_get_irq_byname(pdev, "UV"), dcdc);
618e4ee831fSMark Brown 	regulator_unregister(dcdc->regulator);
619e24a04c4SMark Brown 	if (dcdc->dvs_gpio)
620e24a04c4SMark Brown 		gpio_free(dcdc->dvs_gpio);
621e4ee831fSMark Brown 
622e4ee831fSMark Brown 	return 0;
623e4ee831fSMark Brown }
624e4ee831fSMark Brown 
625e4ee831fSMark Brown static struct platform_driver wm831x_buckv_driver = {
626e4ee831fSMark Brown 	.probe = wm831x_buckv_probe,
627e4ee831fSMark Brown 	.remove = __devexit_p(wm831x_buckv_remove),
628e4ee831fSMark Brown 	.driver		= {
629e4ee831fSMark Brown 		.name	= "wm831x-buckv",
630eb66d565SDmitry Torokhov 		.owner	= THIS_MODULE,
631e4ee831fSMark Brown 	},
632e4ee831fSMark Brown };
633e4ee831fSMark Brown 
634e4ee831fSMark Brown /*
635e4ee831fSMark Brown  * BUCKP specifics
636e4ee831fSMark Brown  */
637e4ee831fSMark Brown 
638e4ee831fSMark Brown static int wm831x_buckp_list_voltage(struct regulator_dev *rdev,
639e4ee831fSMark Brown 				      unsigned selector)
640e4ee831fSMark Brown {
641e4ee831fSMark Brown 	if (selector <= WM831X_BUCKP_MAX_SELECTOR)
642e4ee831fSMark Brown 		return 850000 + (selector * 25000);
643e4ee831fSMark Brown 	else
644e4ee831fSMark Brown 		return -EINVAL;
645e4ee831fSMark Brown }
646e4ee831fSMark Brown 
647e4ee831fSMark Brown static int wm831x_buckp_set_voltage_int(struct regulator_dev *rdev, int reg,
6483a93f2a9SMark Brown 					int min_uV, int max_uV, int *selector)
649e4ee831fSMark Brown {
650e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
651e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
652e4ee831fSMark Brown 	u16 vsel;
653e4ee831fSMark Brown 
654e4ee831fSMark Brown 	if (min_uV <= 34000000)
655e4ee831fSMark Brown 		vsel = (min_uV - 850000) / 25000;
656e4ee831fSMark Brown 	else
657e4ee831fSMark Brown 		return -EINVAL;
658e4ee831fSMark Brown 
659e4ee831fSMark Brown 	if (wm831x_buckp_list_voltage(rdev, vsel) > max_uV)
660e4ee831fSMark Brown 		return -EINVAL;
661e4ee831fSMark Brown 
6623a93f2a9SMark Brown 	*selector = vsel;
6633a93f2a9SMark Brown 
664e4ee831fSMark Brown 	return wm831x_set_bits(wm831x, reg, WM831X_DC3_ON_VSEL_MASK, vsel);
665e4ee831fSMark Brown }
666e4ee831fSMark Brown 
667e4ee831fSMark Brown static int wm831x_buckp_set_voltage(struct regulator_dev *rdev,
6683a93f2a9SMark Brown 				    int min_uV, int max_uV,
6693a93f2a9SMark Brown 				    unsigned *selector)
670e4ee831fSMark Brown {
671e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
672e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
673e4ee831fSMark Brown 
6743a93f2a9SMark Brown 	return wm831x_buckp_set_voltage_int(rdev, reg, min_uV, max_uV,
6753a93f2a9SMark Brown 					    selector);
676e4ee831fSMark Brown }
677e4ee831fSMark Brown 
678e4ee831fSMark Brown static int wm831x_buckp_set_suspend_voltage(struct regulator_dev *rdev,
679e4ee831fSMark Brown 					    int uV)
680e4ee831fSMark Brown {
681e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
682e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
6833a93f2a9SMark Brown 	unsigned selector;
684e4ee831fSMark Brown 
6853a93f2a9SMark Brown 	return wm831x_buckp_set_voltage_int(rdev, reg, uV, uV, &selector);
686e4ee831fSMark Brown }
687e4ee831fSMark Brown 
688afb8bb80SMark Brown static int wm831x_buckp_get_voltage_sel(struct regulator_dev *rdev)
689e4ee831fSMark Brown {
690e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
691e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
692e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
693e4ee831fSMark Brown 	int val;
694e4ee831fSMark Brown 
695e4ee831fSMark Brown 	val = wm831x_reg_read(wm831x, reg);
696e4ee831fSMark Brown 	if (val < 0)
697e4ee831fSMark Brown 		return val;
698e4ee831fSMark Brown 
699afb8bb80SMark Brown 	return val & WM831X_DC3_ON_VSEL_MASK;
700e4ee831fSMark Brown }
701e4ee831fSMark Brown 
702e4ee831fSMark Brown static struct regulator_ops wm831x_buckp_ops = {
703e4ee831fSMark Brown 	.set_voltage = wm831x_buckp_set_voltage,
704afb8bb80SMark Brown 	.get_voltage_sel = wm831x_buckp_get_voltage_sel,
705e4ee831fSMark Brown 	.list_voltage = wm831x_buckp_list_voltage,
706e4ee831fSMark Brown 	.set_suspend_voltage = wm831x_buckp_set_suspend_voltage,
707e4ee831fSMark Brown 
708e4ee831fSMark Brown 	.is_enabled = wm831x_dcdc_is_enabled,
709e4ee831fSMark Brown 	.enable = wm831x_dcdc_enable,
710e4ee831fSMark Brown 	.disable = wm831x_dcdc_disable,
711e4ee831fSMark Brown 	.get_status = wm831x_dcdc_get_status,
712e4ee831fSMark Brown 	.get_mode = wm831x_dcdc_get_mode,
713e4ee831fSMark Brown 	.set_mode = wm831x_dcdc_set_mode,
714e4ee831fSMark Brown 	.set_suspend_mode = wm831x_dcdc_set_suspend_mode,
715e4ee831fSMark Brown };
716e4ee831fSMark Brown 
717e4ee831fSMark Brown static __devinit int wm831x_buckp_probe(struct platform_device *pdev)
718e4ee831fSMark Brown {
719e4ee831fSMark Brown 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
720e4ee831fSMark Brown 	struct wm831x_pdata *pdata = wm831x->dev->platform_data;
721c172708dSMark Brown 	struct regulator_config config = { };
722137a6354SMark Brown 	int id;
723e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc;
724e4ee831fSMark Brown 	struct resource *res;
725e4ee831fSMark Brown 	int ret, irq;
726e4ee831fSMark Brown 
727137a6354SMark Brown 	if (pdata && pdata->wm831x_num)
728137a6354SMark Brown 		id = (pdata->wm831x_num * 10) + 1;
729137a6354SMark Brown 	else
730137a6354SMark Brown 		id = 0;
731137a6354SMark Brown 	id = pdev->id - id;
732137a6354SMark Brown 
733e4ee831fSMark Brown 	dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
734e4ee831fSMark Brown 
735e4ee831fSMark Brown 	if (pdata == NULL || pdata->dcdc[id] == NULL)
736e4ee831fSMark Brown 		return -ENODEV;
737e4ee831fSMark Brown 
738fded2f4fSMark Brown 	dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc),
739fded2f4fSMark Brown 			    GFP_KERNEL);
740e4ee831fSMark Brown 	if (dcdc == NULL) {
741e4ee831fSMark Brown 		dev_err(&pdev->dev, "Unable to allocate private data\n");
742e4ee831fSMark Brown 		return -ENOMEM;
743e4ee831fSMark Brown 	}
744e4ee831fSMark Brown 
745e4ee831fSMark Brown 	dcdc->wm831x = wm831x;
746e4ee831fSMark Brown 
747e4ee831fSMark Brown 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
748e4ee831fSMark Brown 	if (res == NULL) {
749e4ee831fSMark Brown 		dev_err(&pdev->dev, "No I/O resource\n");
750e4ee831fSMark Brown 		ret = -EINVAL;
751e4ee831fSMark Brown 		goto err;
752e4ee831fSMark Brown 	}
753e4ee831fSMark Brown 	dcdc->base = res->start;
754e4ee831fSMark Brown 
755e4ee831fSMark Brown 	snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
756e4ee831fSMark Brown 	dcdc->desc.name = dcdc->name;
75782caa978SMark Brown 
75882caa978SMark Brown 	snprintf(dcdc->supply_name, sizeof(dcdc->supply_name),
75982caa978SMark Brown 		 "DC%dVDD", id + 1);
76082caa978SMark Brown 	dcdc->desc.supply_name = dcdc->supply_name;
76182caa978SMark Brown 
762e4ee831fSMark Brown 	dcdc->desc.id = id;
763e4ee831fSMark Brown 	dcdc->desc.type = REGULATOR_VOLTAGE;
764e4ee831fSMark Brown 	dcdc->desc.n_voltages = WM831X_BUCKP_MAX_SELECTOR + 1;
765e4ee831fSMark Brown 	dcdc->desc.ops = &wm831x_buckp_ops;
766e4ee831fSMark Brown 	dcdc->desc.owner = THIS_MODULE;
767e4ee831fSMark Brown 
768c172708dSMark Brown 	config.dev = pdev->dev.parent;
769c172708dSMark Brown 	config.init_data = pdata->dcdc[id];
770c172708dSMark Brown 	config.driver_data = dcdc;
771c172708dSMark Brown 
772c172708dSMark Brown 	dcdc->regulator = regulator_register(&dcdc->desc, &config);
773e4ee831fSMark Brown 	if (IS_ERR(dcdc->regulator)) {
774e4ee831fSMark Brown 		ret = PTR_ERR(dcdc->regulator);
775e4ee831fSMark Brown 		dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
776e4ee831fSMark Brown 			id + 1, ret);
777e4ee831fSMark Brown 		goto err;
778e4ee831fSMark Brown 	}
779e4ee831fSMark Brown 
780e4ee831fSMark Brown 	irq = platform_get_irq_byname(pdev, "UV");
781dfda9c27SMark Brown 	ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq,
782dfda9c27SMark Brown 				   IRQF_TRIGGER_RISING,	dcdc->name, dcdc);
783e4ee831fSMark Brown 	if (ret != 0) {
784e4ee831fSMark Brown 		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
785e4ee831fSMark Brown 			irq, ret);
786e4ee831fSMark Brown 		goto err_regulator;
787e4ee831fSMark Brown 	}
788e4ee831fSMark Brown 
789e4ee831fSMark Brown 	platform_set_drvdata(pdev, dcdc);
790e4ee831fSMark Brown 
791e4ee831fSMark Brown 	return 0;
792e4ee831fSMark Brown 
793e4ee831fSMark Brown err_regulator:
794e4ee831fSMark Brown 	regulator_unregister(dcdc->regulator);
795e4ee831fSMark Brown err:
796e4ee831fSMark Brown 	return ret;
797e4ee831fSMark Brown }
798e4ee831fSMark Brown 
799e4ee831fSMark Brown static __devexit int wm831x_buckp_remove(struct platform_device *pdev)
800e4ee831fSMark Brown {
801e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
802e4ee831fSMark Brown 
803eb66d565SDmitry Torokhov 	platform_set_drvdata(pdev, NULL);
804eb66d565SDmitry Torokhov 
80569952369SMark Brown 	free_irq(platform_get_irq_byname(pdev, "UV"), dcdc);
806e4ee831fSMark Brown 	regulator_unregister(dcdc->regulator);
807e4ee831fSMark Brown 
808e4ee831fSMark Brown 	return 0;
809e4ee831fSMark Brown }
810e4ee831fSMark Brown 
811e4ee831fSMark Brown static struct platform_driver wm831x_buckp_driver = {
812e4ee831fSMark Brown 	.probe = wm831x_buckp_probe,
813e4ee831fSMark Brown 	.remove = __devexit_p(wm831x_buckp_remove),
814e4ee831fSMark Brown 	.driver		= {
815e4ee831fSMark Brown 		.name	= "wm831x-buckp",
816eb66d565SDmitry Torokhov 		.owner	= THIS_MODULE,
817e4ee831fSMark Brown 	},
818e4ee831fSMark Brown };
819e4ee831fSMark Brown 
8208267a9baSMark Brown /*
8211304850dSMark Brown  * DCDC boost convertors
8221304850dSMark Brown  */
8231304850dSMark Brown 
8241304850dSMark Brown static int wm831x_boostp_get_status(struct regulator_dev *rdev)
8251304850dSMark Brown {
8261304850dSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
8271304850dSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
8281304850dSMark Brown 	int ret;
8291304850dSMark Brown 
8301304850dSMark Brown 	/* First, check for errors */
8311304850dSMark Brown 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
8321304850dSMark Brown 	if (ret < 0)
8331304850dSMark Brown 		return ret;
8341304850dSMark Brown 
8351304850dSMark Brown 	if (ret & (1 << rdev_get_id(rdev))) {
8361304850dSMark Brown 		dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
8371304850dSMark Brown 			rdev_get_id(rdev) + 1);
8381304850dSMark Brown 		return REGULATOR_STATUS_ERROR;
8391304850dSMark Brown 	}
8401304850dSMark Brown 
8411304850dSMark Brown 	/* Is the regulator on? */
8421304850dSMark Brown 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
8431304850dSMark Brown 	if (ret < 0)
8441304850dSMark Brown 		return ret;
8451304850dSMark Brown 	if (ret & (1 << rdev_get_id(rdev)))
8461304850dSMark Brown 		return REGULATOR_STATUS_ON;
8471304850dSMark Brown 	else
8481304850dSMark Brown 		return REGULATOR_STATUS_OFF;
8491304850dSMark Brown }
8501304850dSMark Brown 
8511304850dSMark Brown static struct regulator_ops wm831x_boostp_ops = {
8521304850dSMark Brown 	.get_status = wm831x_boostp_get_status,
8531304850dSMark Brown 
8541304850dSMark Brown 	.is_enabled = wm831x_dcdc_is_enabled,
8551304850dSMark Brown 	.enable = wm831x_dcdc_enable,
8561304850dSMark Brown 	.disable = wm831x_dcdc_disable,
8571304850dSMark Brown };
8581304850dSMark Brown 
8591304850dSMark Brown static __devinit int wm831x_boostp_probe(struct platform_device *pdev)
8601304850dSMark Brown {
8611304850dSMark Brown 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
8621304850dSMark Brown 	struct wm831x_pdata *pdata = wm831x->dev->platform_data;
863c172708dSMark Brown 	struct regulator_config config = { };
8641304850dSMark Brown 	int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
8651304850dSMark Brown 	struct wm831x_dcdc *dcdc;
8661304850dSMark Brown 	struct resource *res;
8671304850dSMark Brown 	int ret, irq;
8681304850dSMark Brown 
8691304850dSMark Brown 	dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
8701304850dSMark Brown 
8711304850dSMark Brown 	if (pdata == NULL || pdata->dcdc[id] == NULL)
8721304850dSMark Brown 		return -ENODEV;
8731304850dSMark Brown 
8744c60165dSAxel Lin 	dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), GFP_KERNEL);
8751304850dSMark Brown 	if (dcdc == NULL) {
8761304850dSMark Brown 		dev_err(&pdev->dev, "Unable to allocate private data\n");
8771304850dSMark Brown 		return -ENOMEM;
8781304850dSMark Brown 	}
8791304850dSMark Brown 
8801304850dSMark Brown 	dcdc->wm831x = wm831x;
8811304850dSMark Brown 
8821304850dSMark Brown 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
8831304850dSMark Brown 	if (res == NULL) {
8841304850dSMark Brown 		dev_err(&pdev->dev, "No I/O resource\n");
8851304850dSMark Brown 		ret = -EINVAL;
8861304850dSMark Brown 		goto err;
8871304850dSMark Brown 	}
8881304850dSMark Brown 	dcdc->base = res->start;
8891304850dSMark Brown 
8901304850dSMark Brown 	snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
8911304850dSMark Brown 	dcdc->desc.name = dcdc->name;
8921304850dSMark Brown 	dcdc->desc.id = id;
8931304850dSMark Brown 	dcdc->desc.type = REGULATOR_VOLTAGE;
8941304850dSMark Brown 	dcdc->desc.ops = &wm831x_boostp_ops;
8951304850dSMark Brown 	dcdc->desc.owner = THIS_MODULE;
8961304850dSMark Brown 
897c172708dSMark Brown 	config.dev = pdev->dev.parent;
898c172708dSMark Brown 	config.init_data = pdata->dcdc[id];
899c172708dSMark Brown 	config.driver_data = dcdc;
900c172708dSMark Brown 
901c172708dSMark Brown 	dcdc->regulator = regulator_register(&dcdc->desc, &config);
9021304850dSMark Brown 	if (IS_ERR(dcdc->regulator)) {
9031304850dSMark Brown 		ret = PTR_ERR(dcdc->regulator);
9041304850dSMark Brown 		dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
9051304850dSMark Brown 			id + 1, ret);
9061304850dSMark Brown 		goto err;
9071304850dSMark Brown 	}
9081304850dSMark Brown 
9091304850dSMark Brown 	irq = platform_get_irq_byname(pdev, "UV");
910dfda9c27SMark Brown 	ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq,
9111304850dSMark Brown 				   IRQF_TRIGGER_RISING, dcdc->name,
9121304850dSMark Brown 				   dcdc);
9131304850dSMark Brown 	if (ret != 0) {
9141304850dSMark Brown 		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
9151304850dSMark Brown 			irq, ret);
9161304850dSMark Brown 		goto err_regulator;
9171304850dSMark Brown 	}
9181304850dSMark Brown 
9191304850dSMark Brown 	platform_set_drvdata(pdev, dcdc);
9201304850dSMark Brown 
9211304850dSMark Brown 	return 0;
9221304850dSMark Brown 
9231304850dSMark Brown err_regulator:
9241304850dSMark Brown 	regulator_unregister(dcdc->regulator);
9251304850dSMark Brown err:
9261304850dSMark Brown 	return ret;
9271304850dSMark Brown }
9281304850dSMark Brown 
9291304850dSMark Brown static __devexit int wm831x_boostp_remove(struct platform_device *pdev)
9301304850dSMark Brown {
9311304850dSMark Brown 	struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
9321304850dSMark Brown 
933eb66d565SDmitry Torokhov 	platform_set_drvdata(pdev, NULL);
934eb66d565SDmitry Torokhov 
935dfda9c27SMark Brown 	free_irq(platform_get_irq_byname(pdev, "UV"), dcdc);
9361304850dSMark Brown 	regulator_unregister(dcdc->regulator);
9371304850dSMark Brown 
9381304850dSMark Brown 	return 0;
9391304850dSMark Brown }
9401304850dSMark Brown 
9411304850dSMark Brown static struct platform_driver wm831x_boostp_driver = {
9421304850dSMark Brown 	.probe = wm831x_boostp_probe,
9431304850dSMark Brown 	.remove = __devexit_p(wm831x_boostp_remove),
9441304850dSMark Brown 	.driver		= {
9451304850dSMark Brown 		.name	= "wm831x-boostp",
946eb66d565SDmitry Torokhov 		.owner	= THIS_MODULE,
9471304850dSMark Brown 	},
9481304850dSMark Brown };
9491304850dSMark Brown 
9501304850dSMark Brown /*
9518267a9baSMark Brown  * External Power Enable
9528267a9baSMark Brown  *
9538267a9baSMark Brown  * These aren't actually DCDCs but look like them in hardware so share
9548267a9baSMark Brown  * code.
9558267a9baSMark Brown  */
9568267a9baSMark Brown 
9578267a9baSMark Brown #define WM831X_EPE_BASE 6
9588267a9baSMark Brown 
9598267a9baSMark Brown static struct regulator_ops wm831x_epe_ops = {
9608267a9baSMark Brown 	.is_enabled = wm831x_dcdc_is_enabled,
9618267a9baSMark Brown 	.enable = wm831x_dcdc_enable,
9628267a9baSMark Brown 	.disable = wm831x_dcdc_disable,
9638267a9baSMark Brown 	.get_status = wm831x_dcdc_get_status,
9648267a9baSMark Brown };
9658267a9baSMark Brown 
9668267a9baSMark Brown static __devinit int wm831x_epe_probe(struct platform_device *pdev)
9678267a9baSMark Brown {
9688267a9baSMark Brown 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
9698267a9baSMark Brown 	struct wm831x_pdata *pdata = wm831x->dev->platform_data;
970c172708dSMark Brown 	struct regulator_config config = { };
9718267a9baSMark Brown 	int id = pdev->id % ARRAY_SIZE(pdata->epe);
9728267a9baSMark Brown 	struct wm831x_dcdc *dcdc;
9738267a9baSMark Brown 	int ret;
9748267a9baSMark Brown 
9758267a9baSMark Brown 	dev_dbg(&pdev->dev, "Probing EPE%d\n", id + 1);
9768267a9baSMark Brown 
9778267a9baSMark Brown 	if (pdata == NULL || pdata->epe[id] == NULL)
9788267a9baSMark Brown 		return -ENODEV;
9798267a9baSMark Brown 
9804c60165dSAxel Lin 	dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), GFP_KERNEL);
9818267a9baSMark Brown 	if (dcdc == NULL) {
9828267a9baSMark Brown 		dev_err(&pdev->dev, "Unable to allocate private data\n");
9838267a9baSMark Brown 		return -ENOMEM;
9848267a9baSMark Brown 	}
9858267a9baSMark Brown 
9868267a9baSMark Brown 	dcdc->wm831x = wm831x;
9878267a9baSMark Brown 
9888267a9baSMark Brown 	/* For current parts this is correct; probably need to revisit
9898267a9baSMark Brown 	 * in future.
9908267a9baSMark Brown 	 */
9918267a9baSMark Brown 	snprintf(dcdc->name, sizeof(dcdc->name), "EPE%d", id + 1);
9928267a9baSMark Brown 	dcdc->desc.name = dcdc->name;
9938267a9baSMark Brown 	dcdc->desc.id = id + WM831X_EPE_BASE; /* Offset in DCDC registers */
9948267a9baSMark Brown 	dcdc->desc.ops = &wm831x_epe_ops;
9958267a9baSMark Brown 	dcdc->desc.type = REGULATOR_VOLTAGE;
9968267a9baSMark Brown 	dcdc->desc.owner = THIS_MODULE;
9978267a9baSMark Brown 
998c172708dSMark Brown 	config.dev = pdev->dev.parent;
999c172708dSMark Brown 	config.init_data = pdata->epe[id];
1000c172708dSMark Brown 	config.driver_data = dcdc;
1001c172708dSMark Brown 
1002c172708dSMark Brown 	dcdc->regulator = regulator_register(&dcdc->desc, &config);
10038267a9baSMark Brown 	if (IS_ERR(dcdc->regulator)) {
10048267a9baSMark Brown 		ret = PTR_ERR(dcdc->regulator);
10058267a9baSMark Brown 		dev_err(wm831x->dev, "Failed to register EPE%d: %d\n",
10068267a9baSMark Brown 			id + 1, ret);
10078267a9baSMark Brown 		goto err;
10088267a9baSMark Brown 	}
10098267a9baSMark Brown 
10108267a9baSMark Brown 	platform_set_drvdata(pdev, dcdc);
10118267a9baSMark Brown 
10128267a9baSMark Brown 	return 0;
10138267a9baSMark Brown 
10148267a9baSMark Brown err:
10158267a9baSMark Brown 	return ret;
10168267a9baSMark Brown }
10178267a9baSMark Brown 
10188267a9baSMark Brown static __devexit int wm831x_epe_remove(struct platform_device *pdev)
10198267a9baSMark Brown {
10208267a9baSMark Brown 	struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
10218267a9baSMark Brown 
1022eb66d565SDmitry Torokhov 	platform_set_drvdata(pdev, NULL);
10238267a9baSMark Brown 	regulator_unregister(dcdc->regulator);
10248267a9baSMark Brown 
10258267a9baSMark Brown 	return 0;
10268267a9baSMark Brown }
10278267a9baSMark Brown 
10288267a9baSMark Brown static struct platform_driver wm831x_epe_driver = {
10298267a9baSMark Brown 	.probe = wm831x_epe_probe,
10308267a9baSMark Brown 	.remove = __devexit_p(wm831x_epe_remove),
10318267a9baSMark Brown 	.driver		= {
10328267a9baSMark Brown 		.name	= "wm831x-epe",
1033eb66d565SDmitry Torokhov 		.owner	= THIS_MODULE,
10348267a9baSMark Brown 	},
10358267a9baSMark Brown };
10368267a9baSMark Brown 
1037e4ee831fSMark Brown static int __init wm831x_dcdc_init(void)
1038e4ee831fSMark Brown {
1039e4ee831fSMark Brown 	int ret;
1040e4ee831fSMark Brown 	ret = platform_driver_register(&wm831x_buckv_driver);
1041e4ee831fSMark Brown 	if (ret != 0)
1042e4ee831fSMark Brown 		pr_err("Failed to register WM831x BUCKV driver: %d\n", ret);
1043e4ee831fSMark Brown 
1044e4ee831fSMark Brown 	ret = platform_driver_register(&wm831x_buckp_driver);
1045e4ee831fSMark Brown 	if (ret != 0)
1046e4ee831fSMark Brown 		pr_err("Failed to register WM831x BUCKP driver: %d\n", ret);
1047e4ee831fSMark Brown 
10481304850dSMark Brown 	ret = platform_driver_register(&wm831x_boostp_driver);
10491304850dSMark Brown 	if (ret != 0)
10501304850dSMark Brown 		pr_err("Failed to register WM831x BOOST driver: %d\n", ret);
10511304850dSMark Brown 
10528267a9baSMark Brown 	ret = platform_driver_register(&wm831x_epe_driver);
10538267a9baSMark Brown 	if (ret != 0)
10548267a9baSMark Brown 		pr_err("Failed to register WM831x EPE driver: %d\n", ret);
10558267a9baSMark Brown 
1056e4ee831fSMark Brown 	return 0;
1057e4ee831fSMark Brown }
1058e4ee831fSMark Brown subsys_initcall(wm831x_dcdc_init);
1059e4ee831fSMark Brown 
1060e4ee831fSMark Brown static void __exit wm831x_dcdc_exit(void)
1061e4ee831fSMark Brown {
10628267a9baSMark Brown 	platform_driver_unregister(&wm831x_epe_driver);
10631304850dSMark Brown 	platform_driver_unregister(&wm831x_boostp_driver);
1064e4ee831fSMark Brown 	platform_driver_unregister(&wm831x_buckp_driver);
1065e4ee831fSMark Brown 	platform_driver_unregister(&wm831x_buckv_driver);
1066e4ee831fSMark Brown }
1067e4ee831fSMark Brown module_exit(wm831x_dcdc_exit);
1068e4ee831fSMark Brown 
1069e4ee831fSMark Brown /* Module information */
1070e4ee831fSMark Brown MODULE_AUTHOR("Mark Brown");
1071e4ee831fSMark Brown MODULE_DESCRIPTION("WM831x DC-DC convertor driver");
1072e4ee831fSMark Brown MODULE_LICENSE("GPL");
1073e4ee831fSMark Brown MODULE_ALIAS("platform:wm831x-buckv");
1074e4ee831fSMark Brown MODULE_ALIAS("platform:wm831x-buckp");
107524b43150SMark Brown MODULE_ALIAS("platform:wm831x-epe");
1076