1e4ee831fSMark Brown /*
2e4ee831fSMark Brown  * wm831x-dcdc.c  --  DC-DC buck convertor driver for the WM831x series
3e4ee831fSMark Brown  *
4e4ee831fSMark Brown  * Copyright 2009 Wolfson Microelectronics PLC.
5e4ee831fSMark Brown  *
6e4ee831fSMark Brown  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7e4ee831fSMark Brown  *
8e4ee831fSMark Brown  *  This program is free software; you can redistribute  it and/or modify it
9e4ee831fSMark Brown  *  under  the terms of  the GNU General  Public License as published by the
10e4ee831fSMark Brown  *  Free Software Foundation;  either version 2 of the  License, or (at your
11e4ee831fSMark Brown  *  option) any later version.
12e4ee831fSMark Brown  */
13e4ee831fSMark Brown 
14e4ee831fSMark Brown #include <linux/module.h>
15e4ee831fSMark Brown #include <linux/moduleparam.h>
16e4ee831fSMark Brown #include <linux/init.h>
17e4ee831fSMark Brown #include <linux/bitops.h>
18e4ee831fSMark Brown #include <linux/err.h>
19e4ee831fSMark Brown #include <linux/i2c.h>
20e4ee831fSMark Brown #include <linux/platform_device.h>
21e4ee831fSMark Brown #include <linux/regulator/driver.h>
22e24a04c4SMark Brown #include <linux/regulator/machine.h>
23e24a04c4SMark Brown #include <linux/gpio.h>
245a0e3ad6STejun Heo #include <linux/slab.h>
25e4ee831fSMark Brown 
26e4ee831fSMark Brown #include <linux/mfd/wm831x/core.h>
27e4ee831fSMark Brown #include <linux/mfd/wm831x/regulator.h>
28e4ee831fSMark Brown #include <linux/mfd/wm831x/pdata.h>
29e4ee831fSMark Brown 
30e4ee831fSMark Brown #define WM831X_BUCKV_MAX_SELECTOR 0x68
31e4ee831fSMark Brown #define WM831X_BUCKP_MAX_SELECTOR 0x66
32e4ee831fSMark Brown 
33e4ee831fSMark Brown #define WM831X_DCDC_MODE_FAST    0
34e4ee831fSMark Brown #define WM831X_DCDC_MODE_NORMAL  1
35e4ee831fSMark Brown #define WM831X_DCDC_MODE_IDLE    2
36e4ee831fSMark Brown #define WM831X_DCDC_MODE_STANDBY 3
37e4ee831fSMark Brown 
3882caa978SMark Brown #define WM831X_DCDC_MAX_NAME 9
39e4ee831fSMark Brown 
40e4ee831fSMark Brown /* Register offsets in control block */
41e4ee831fSMark Brown #define WM831X_DCDC_CONTROL_1     0
42e4ee831fSMark Brown #define WM831X_DCDC_CONTROL_2     1
43e4ee831fSMark Brown #define WM831X_DCDC_ON_CONFIG     2
44e4ee831fSMark Brown #define WM831X_DCDC_SLEEP_CONTROL 3
45e24a04c4SMark Brown #define WM831X_DCDC_DVS_CONTROL   4
46e4ee831fSMark Brown 
47e4ee831fSMark Brown /*
48e4ee831fSMark Brown  * Shared
49e4ee831fSMark Brown  */
50e4ee831fSMark Brown 
51e4ee831fSMark Brown struct wm831x_dcdc {
52e4ee831fSMark Brown 	char name[WM831X_DCDC_MAX_NAME];
5382caa978SMark Brown 	char supply_name[WM831X_DCDC_MAX_NAME];
54e4ee831fSMark Brown 	struct regulator_desc desc;
55e4ee831fSMark Brown 	int base;
56e4ee831fSMark Brown 	struct wm831x *wm831x;
57e4ee831fSMark Brown 	struct regulator_dev *regulator;
58e24a04c4SMark Brown 	int dvs_gpio;
59e24a04c4SMark Brown 	int dvs_gpio_state;
60e24a04c4SMark Brown 	int on_vsel;
61e24a04c4SMark Brown 	int dvs_vsel;
62e4ee831fSMark Brown };
63e4ee831fSMark Brown 
64e4ee831fSMark Brown static unsigned int wm831x_dcdc_get_mode(struct regulator_dev *rdev)
65e4ee831fSMark Brown 
66e4ee831fSMark Brown {
67e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
68e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
69e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
70e4ee831fSMark Brown 	int val;
71e4ee831fSMark Brown 
72e4ee831fSMark Brown 	val = wm831x_reg_read(wm831x, reg);
73e4ee831fSMark Brown 	if (val < 0)
74e4ee831fSMark Brown 		return val;
75e4ee831fSMark Brown 
76e4ee831fSMark Brown 	val = (val & WM831X_DC1_ON_MODE_MASK) >> WM831X_DC1_ON_MODE_SHIFT;
77e4ee831fSMark Brown 
78e4ee831fSMark Brown 	switch (val) {
79e4ee831fSMark Brown 	case WM831X_DCDC_MODE_FAST:
80e4ee831fSMark Brown 		return REGULATOR_MODE_FAST;
81e4ee831fSMark Brown 	case WM831X_DCDC_MODE_NORMAL:
82e4ee831fSMark Brown 		return REGULATOR_MODE_NORMAL;
83e4ee831fSMark Brown 	case WM831X_DCDC_MODE_STANDBY:
84e4ee831fSMark Brown 		return REGULATOR_MODE_STANDBY;
85e4ee831fSMark Brown 	case WM831X_DCDC_MODE_IDLE:
86e4ee831fSMark Brown 		return REGULATOR_MODE_IDLE;
87e4ee831fSMark Brown 	default:
88e4ee831fSMark Brown 		BUG();
899ee291a4SMark Brown 		return -EINVAL;
90e4ee831fSMark Brown 	}
91e4ee831fSMark Brown }
92e4ee831fSMark Brown 
93e4ee831fSMark Brown static int wm831x_dcdc_set_mode_int(struct wm831x *wm831x, int reg,
94e4ee831fSMark Brown 				    unsigned int mode)
95e4ee831fSMark Brown {
96e4ee831fSMark Brown 	int val;
97e4ee831fSMark Brown 
98e4ee831fSMark Brown 	switch (mode) {
99e4ee831fSMark Brown 	case REGULATOR_MODE_FAST:
100e4ee831fSMark Brown 		val = WM831X_DCDC_MODE_FAST;
101e4ee831fSMark Brown 		break;
102e4ee831fSMark Brown 	case REGULATOR_MODE_NORMAL:
103e4ee831fSMark Brown 		val = WM831X_DCDC_MODE_NORMAL;
104e4ee831fSMark Brown 		break;
105e4ee831fSMark Brown 	case REGULATOR_MODE_STANDBY:
106e4ee831fSMark Brown 		val = WM831X_DCDC_MODE_STANDBY;
107e4ee831fSMark Brown 		break;
108e4ee831fSMark Brown 	case REGULATOR_MODE_IDLE:
109e4ee831fSMark Brown 		val = WM831X_DCDC_MODE_IDLE;
110e4ee831fSMark Brown 		break;
111e4ee831fSMark Brown 	default:
112e4ee831fSMark Brown 		return -EINVAL;
113e4ee831fSMark Brown 	}
114e4ee831fSMark Brown 
115e4ee831fSMark Brown 	return wm831x_set_bits(wm831x, reg, WM831X_DC1_ON_MODE_MASK,
116e4ee831fSMark Brown 			       val << WM831X_DC1_ON_MODE_SHIFT);
117e4ee831fSMark Brown }
118e4ee831fSMark Brown 
119e4ee831fSMark Brown static int wm831x_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
120e4ee831fSMark Brown {
121e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
122e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
123e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
124e4ee831fSMark Brown 
125e4ee831fSMark Brown 	return wm831x_dcdc_set_mode_int(wm831x, reg, mode);
126e4ee831fSMark Brown }
127e4ee831fSMark Brown 
128e4ee831fSMark Brown static int wm831x_dcdc_set_suspend_mode(struct regulator_dev *rdev,
129e4ee831fSMark Brown 					unsigned int mode)
130e4ee831fSMark Brown {
131e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
132e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
133e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
134e4ee831fSMark Brown 
135e4ee831fSMark Brown 	return wm831x_dcdc_set_mode_int(wm831x, reg, mode);
136e4ee831fSMark Brown }
137e4ee831fSMark Brown 
138e4ee831fSMark Brown static int wm831x_dcdc_get_status(struct regulator_dev *rdev)
139e4ee831fSMark Brown {
140e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
141e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
142e4ee831fSMark Brown 	int ret;
143e4ee831fSMark Brown 
144e4ee831fSMark Brown 	/* First, check for errors */
145e4ee831fSMark Brown 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
146e4ee831fSMark Brown 	if (ret < 0)
147e4ee831fSMark Brown 		return ret;
148e4ee831fSMark Brown 
149e4ee831fSMark Brown 	if (ret & (1 << rdev_get_id(rdev))) {
150e4ee831fSMark Brown 		dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
151e4ee831fSMark Brown 			rdev_get_id(rdev) + 1);
152e4ee831fSMark Brown 		return REGULATOR_STATUS_ERROR;
153e4ee831fSMark Brown 	}
154e4ee831fSMark Brown 
155e4ee831fSMark Brown 	/* DCDC1 and DCDC2 can additionally detect high voltage/current */
156e4ee831fSMark Brown 	if (rdev_get_id(rdev) < 2) {
157e4ee831fSMark Brown 		if (ret & (WM831X_DC1_OV_STS << rdev_get_id(rdev))) {
158e4ee831fSMark Brown 			dev_dbg(wm831x->dev, "DCDC%d over voltage\n",
159e4ee831fSMark Brown 				rdev_get_id(rdev) + 1);
160e4ee831fSMark Brown 			return REGULATOR_STATUS_ERROR;
161e4ee831fSMark Brown 		}
162e4ee831fSMark Brown 
163e4ee831fSMark Brown 		if (ret & (WM831X_DC1_HC_STS << rdev_get_id(rdev))) {
164e4ee831fSMark Brown 			dev_dbg(wm831x->dev, "DCDC%d over current\n",
165e4ee831fSMark Brown 				rdev_get_id(rdev) + 1);
166e4ee831fSMark Brown 			return REGULATOR_STATUS_ERROR;
167e4ee831fSMark Brown 		}
168e4ee831fSMark Brown 	}
169e4ee831fSMark Brown 
170e4ee831fSMark Brown 	/* Is the regulator on? */
171e4ee831fSMark Brown 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
172e4ee831fSMark Brown 	if (ret < 0)
173e4ee831fSMark Brown 		return ret;
174e4ee831fSMark Brown 	if (!(ret & (1 << rdev_get_id(rdev))))
175e4ee831fSMark Brown 		return REGULATOR_STATUS_OFF;
176e4ee831fSMark Brown 
177e4ee831fSMark Brown 	/* TODO: When we handle hardware control modes so we can report the
178e4ee831fSMark Brown 	 * current mode. */
179e4ee831fSMark Brown 	return REGULATOR_STATUS_ON;
180e4ee831fSMark Brown }
181e4ee831fSMark Brown 
182e4ee831fSMark Brown static irqreturn_t wm831x_dcdc_uv_irq(int irq, void *data)
183e4ee831fSMark Brown {
184e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = data;
185e4ee831fSMark Brown 
186e4ee831fSMark Brown 	regulator_notifier_call_chain(dcdc->regulator,
187e4ee831fSMark Brown 				      REGULATOR_EVENT_UNDER_VOLTAGE,
188e4ee831fSMark Brown 				      NULL);
189e4ee831fSMark Brown 
190e4ee831fSMark Brown 	return IRQ_HANDLED;
191e4ee831fSMark Brown }
192e4ee831fSMark Brown 
193e4ee831fSMark Brown static irqreturn_t wm831x_dcdc_oc_irq(int irq, void *data)
194e4ee831fSMark Brown {
195e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = data;
196e4ee831fSMark Brown 
197e4ee831fSMark Brown 	regulator_notifier_call_chain(dcdc->regulator,
198e4ee831fSMark Brown 				      REGULATOR_EVENT_OVER_CURRENT,
199e4ee831fSMark Brown 				      NULL);
200e4ee831fSMark Brown 
201e4ee831fSMark Brown 	return IRQ_HANDLED;
202e4ee831fSMark Brown }
203e4ee831fSMark Brown 
204e4ee831fSMark Brown /*
205e4ee831fSMark Brown  * BUCKV specifics
206e4ee831fSMark Brown  */
207e4ee831fSMark Brown 
208e4ee831fSMark Brown static int wm831x_buckv_list_voltage(struct regulator_dev *rdev,
209e4ee831fSMark Brown 				      unsigned selector)
210e4ee831fSMark Brown {
211e4ee831fSMark Brown 	if (selector <= 0x8)
212e4ee831fSMark Brown 		return 600000;
213e4ee831fSMark Brown 	if (selector <= WM831X_BUCKV_MAX_SELECTOR)
214e4ee831fSMark Brown 		return 600000 + ((selector - 0x8) * 12500);
215e4ee831fSMark Brown 	return -EINVAL;
216e4ee831fSMark Brown }
217e4ee831fSMark Brown 
218b5fb77e0SAxel Lin static int wm831x_buckv_map_voltage(struct regulator_dev *rdev,
219e4ee831fSMark Brown 				   int min_uV, int max_uV)
220e4ee831fSMark Brown {
221e4ee831fSMark Brown 	u16 vsel;
222e4ee831fSMark Brown 
223e4ee831fSMark Brown 	if (min_uV < 600000)
224e4ee831fSMark Brown 		vsel = 0;
225e4ee831fSMark Brown 	else if (min_uV <= 1800000)
2261a679930SAxel Lin 		vsel = DIV_ROUND_UP(min_uV - 600000, 12500) + 8;
227e4ee831fSMark Brown 	else
228e4ee831fSMark Brown 		return -EINVAL;
229e4ee831fSMark Brown 
230e4ee831fSMark Brown 	if (wm831x_buckv_list_voltage(rdev, vsel) > max_uV)
231e4ee831fSMark Brown 		return -EINVAL;
232e4ee831fSMark Brown 
233e24a04c4SMark Brown 	return vsel;
234e24a04c4SMark Brown }
235e24a04c4SMark Brown 
236e24a04c4SMark Brown static int wm831x_buckv_set_dvs(struct regulator_dev *rdev, int state)
237e24a04c4SMark Brown {
238e24a04c4SMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
239e24a04c4SMark Brown 
240e24a04c4SMark Brown 	if (state == dcdc->dvs_gpio_state)
241e24a04c4SMark Brown 		return 0;
242e24a04c4SMark Brown 
243e24a04c4SMark Brown 	dcdc->dvs_gpio_state = state;
244e24a04c4SMark Brown 	gpio_set_value(dcdc->dvs_gpio, state);
245e24a04c4SMark Brown 
246e24a04c4SMark Brown 	/* Should wait for DVS state change to be asserted if we have
247e24a04c4SMark Brown 	 * a GPIO for it, for now assume the device is configured
248e24a04c4SMark Brown 	 * for the fastest possible transition.
249e24a04c4SMark Brown 	 */
250e24a04c4SMark Brown 
251e24a04c4SMark Brown 	return 0;
252e4ee831fSMark Brown }
253e4ee831fSMark Brown 
254b5fb77e0SAxel Lin static int wm831x_buckv_set_voltage_sel(struct regulator_dev *rdev,
255b5fb77e0SAxel Lin 					unsigned vsel)
256e4ee831fSMark Brown {
257e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
258e24a04c4SMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
259e24a04c4SMark Brown 	int on_reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
260e24a04c4SMark Brown 	int dvs_reg = dcdc->base + WM831X_DCDC_DVS_CONTROL;
261b5fb77e0SAxel Lin 	int ret;
2623a93f2a9SMark Brown 
263e24a04c4SMark Brown 	/* If this value is already set then do a GPIO update if we can */
264e24a04c4SMark Brown 	if (dcdc->dvs_gpio && dcdc->on_vsel == vsel)
265e24a04c4SMark Brown 		return wm831x_buckv_set_dvs(rdev, 0);
266e24a04c4SMark Brown 
267e24a04c4SMark Brown 	if (dcdc->dvs_gpio && dcdc->dvs_vsel == vsel)
268e24a04c4SMark Brown 		return wm831x_buckv_set_dvs(rdev, 1);
269e24a04c4SMark Brown 
270e24a04c4SMark Brown 	/* Always set the ON status to the minimum voltage */
271e24a04c4SMark Brown 	ret = wm831x_set_bits(wm831x, on_reg, WM831X_DC1_ON_VSEL_MASK, vsel);
272e24a04c4SMark Brown 	if (ret < 0)
273e24a04c4SMark Brown 		return ret;
274e24a04c4SMark Brown 	dcdc->on_vsel = vsel;
275e24a04c4SMark Brown 
276e24a04c4SMark Brown 	if (!dcdc->dvs_gpio)
277e24a04c4SMark Brown 		return ret;
278e24a04c4SMark Brown 
279e24a04c4SMark Brown 	/* Kick the voltage transition now */
280e24a04c4SMark Brown 	ret = wm831x_buckv_set_dvs(rdev, 0);
281e24a04c4SMark Brown 	if (ret < 0)
282e24a04c4SMark Brown 		return ret;
283e24a04c4SMark Brown 
28488cda60eSMark Brown 	/*
28588cda60eSMark Brown 	 * If this VSEL is higher than the last one we've seen then
28688cda60eSMark Brown 	 * remember it as the DVS VSEL.  This is optimised for CPUfreq
28788cda60eSMark Brown 	 * usage where we want to get to the highest voltage very
28888cda60eSMark Brown 	 * quickly.
28988cda60eSMark Brown 	 */
29088cda60eSMark Brown 	if (vsel > dcdc->dvs_vsel) {
29188cda60eSMark Brown 		ret = wm831x_set_bits(wm831x, dvs_reg,
29288cda60eSMark Brown 				      WM831X_DC1_DVS_VSEL_MASK,
29313ae633cSMark Brown 				      vsel);
294e24a04c4SMark Brown 		if (ret == 0)
295e24a04c4SMark Brown 			dcdc->dvs_vsel = vsel;
296e24a04c4SMark Brown 		else
29788cda60eSMark Brown 			dev_warn(wm831x->dev,
29888cda60eSMark Brown 				 "Failed to set DCDC DVS VSEL: %d\n", ret);
29988cda60eSMark Brown 	}
300e24a04c4SMark Brown 
301e24a04c4SMark Brown 	return 0;
302e4ee831fSMark Brown }
303e4ee831fSMark Brown 
304e4ee831fSMark Brown static int wm831x_buckv_set_suspend_voltage(struct regulator_dev *rdev,
305e4ee831fSMark Brown 					    int uV)
306e4ee831fSMark Brown {
307e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
308e24a04c4SMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
309e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
310e24a04c4SMark Brown 	int vsel;
311e4ee831fSMark Brown 
312b5fb77e0SAxel Lin 	vsel = wm831x_buckv_map_voltage(rdev, uV, uV);
313e24a04c4SMark Brown 	if (vsel < 0)
314e24a04c4SMark Brown 		return vsel;
315e24a04c4SMark Brown 
316e24a04c4SMark Brown 	return wm831x_set_bits(wm831x, reg, WM831X_DC1_SLP_VSEL_MASK, vsel);
317e4ee831fSMark Brown }
318e4ee831fSMark Brown 
319afb8bb80SMark Brown static int wm831x_buckv_get_voltage_sel(struct regulator_dev *rdev)
320e4ee831fSMark Brown {
321e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
322e4ee831fSMark Brown 
323e24a04c4SMark Brown 	if (dcdc->dvs_gpio && dcdc->dvs_gpio_state)
324afb8bb80SMark Brown 		return dcdc->dvs_vsel;
325e24a04c4SMark Brown 	else
326afb8bb80SMark Brown 		return dcdc->on_vsel;
327e4ee831fSMark Brown }
328e4ee831fSMark Brown 
329e4ee831fSMark Brown /* Current limit options */
330e4ee831fSMark Brown static u16 wm831x_dcdc_ilim[] = {
331e4ee831fSMark Brown 	125, 250, 375, 500, 625, 750, 875, 1000
332e4ee831fSMark Brown };
333e4ee831fSMark Brown 
334e4ee831fSMark Brown static int wm831x_buckv_set_current_limit(struct regulator_dev *rdev,
335e4ee831fSMark Brown 					   int min_uA, int max_uA)
336e4ee831fSMark Brown {
337e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
338e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
339e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2;
340e4ee831fSMark Brown 	int i;
341e4ee831fSMark Brown 
342422294deSAxel Lin 	for (i = ARRAY_SIZE(wm831x_dcdc_ilim) - 1; i >= 0; i--) {
343a171e782SAxel Lin 		if ((min_uA <= wm831x_dcdc_ilim[i]) &&
344a171e782SAxel Lin 		    (wm831x_dcdc_ilim[i] <= max_uA))
345422294deSAxel Lin 			return wm831x_set_bits(wm831x, reg,
346422294deSAxel Lin 					       WM831X_DC1_HC_THR_MASK,
34709bf14b9SAxel Lin 						i << WM831X_DC1_HC_THR_SHIFT);
348e4ee831fSMark Brown 	}
349e4ee831fSMark Brown 
350422294deSAxel Lin 	return -EINVAL;
351422294deSAxel Lin }
352422294deSAxel Lin 
353e4ee831fSMark Brown static int wm831x_buckv_get_current_limit(struct regulator_dev *rdev)
354e4ee831fSMark Brown {
355e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
356e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
357e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2;
358e4ee831fSMark Brown 	int val;
359e4ee831fSMark Brown 
360e4ee831fSMark Brown 	val = wm831x_reg_read(wm831x, reg);
361e4ee831fSMark Brown 	if (val < 0)
362e4ee831fSMark Brown 		return val;
363e4ee831fSMark Brown 
36409bf14b9SAxel Lin 	val = (val & WM831X_DC1_HC_THR_MASK) >> WM831X_DC1_HC_THR_SHIFT;
36509bf14b9SAxel Lin 	return wm831x_dcdc_ilim[val];
366e4ee831fSMark Brown }
367e4ee831fSMark Brown 
368e4ee831fSMark Brown static struct regulator_ops wm831x_buckv_ops = {
369b5fb77e0SAxel Lin 	.set_voltage_sel = wm831x_buckv_set_voltage_sel,
370afb8bb80SMark Brown 	.get_voltage_sel = wm831x_buckv_get_voltage_sel,
371e4ee831fSMark Brown 	.list_voltage = wm831x_buckv_list_voltage,
372b5fb77e0SAxel Lin 	.map_voltage = wm831x_buckv_map_voltage,
373e4ee831fSMark Brown 	.set_suspend_voltage = wm831x_buckv_set_suspend_voltage,
374e4ee831fSMark Brown 	.set_current_limit = wm831x_buckv_set_current_limit,
375e4ee831fSMark Brown 	.get_current_limit = wm831x_buckv_get_current_limit,
376e4ee831fSMark Brown 
3773d138fccSMark Brown 	.is_enabled = regulator_is_enabled_regmap,
3783d138fccSMark Brown 	.enable = regulator_enable_regmap,
3793d138fccSMark Brown 	.disable = regulator_disable_regmap,
380e4ee831fSMark Brown 	.get_status = wm831x_dcdc_get_status,
381e4ee831fSMark Brown 	.get_mode = wm831x_dcdc_get_mode,
382e4ee831fSMark Brown 	.set_mode = wm831x_dcdc_set_mode,
383e4ee831fSMark Brown 	.set_suspend_mode = wm831x_dcdc_set_suspend_mode,
384e4ee831fSMark Brown };
385e4ee831fSMark Brown 
386e24a04c4SMark Brown /*
387e24a04c4SMark Brown  * Set up DVS control.  We just log errors since we can still run
388e24a04c4SMark Brown  * (with reduced performance) if we fail.
389e24a04c4SMark Brown  */
3901aa20d27SMark Brown static void wm831x_buckv_dvs_init(struct platform_device *pdev,
3911aa20d27SMark Brown 				  struct wm831x_dcdc *dcdc,
392e24a04c4SMark Brown 				  struct wm831x_buckv_pdata *pdata)
393e24a04c4SMark Brown {
394e24a04c4SMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
395e24a04c4SMark Brown 	int ret;
396e24a04c4SMark Brown 	u16 ctrl;
397e24a04c4SMark Brown 
398e24a04c4SMark Brown 	if (!pdata || !pdata->dvs_gpio)
399e24a04c4SMark Brown 		return;
400e24a04c4SMark Brown 
401e24a04c4SMark Brown 	/* gpiolib won't let us read the GPIO status so pick the higher
402e24a04c4SMark Brown 	 * of the two existing voltages so we take it as platform data.
403e24a04c4SMark Brown 	 */
404e24a04c4SMark Brown 	dcdc->dvs_gpio_state = pdata->dvs_init_state;
405e24a04c4SMark Brown 
4061aa20d27SMark Brown 	ret = devm_gpio_request_one(&pdev->dev, pdata->dvs_gpio,
407053fa1b7SMark Brown 				    dcdc->dvs_gpio_state ? GPIOF_INIT_HIGH : 0,
408053fa1b7SMark Brown 				    "DCDC DVS");
409e24a04c4SMark Brown 	if (ret < 0) {
410053fa1b7SMark Brown 		dev_err(wm831x->dev, "Failed to get %s DVS GPIO: %d\n",
411e24a04c4SMark Brown 			dcdc->name, ret);
412e24a04c4SMark Brown 		return;
413e24a04c4SMark Brown 	}
414e24a04c4SMark Brown 
415e24a04c4SMark Brown 	dcdc->dvs_gpio = pdata->dvs_gpio;
416b47ba9fdSMark Brown 
417b47ba9fdSMark Brown 	switch (pdata->dvs_control_src) {
418b47ba9fdSMark Brown 	case 1:
419b47ba9fdSMark Brown 		ctrl = 2 << WM831X_DC1_DVS_SRC_SHIFT;
420b47ba9fdSMark Brown 		break;
421b47ba9fdSMark Brown 	case 2:
422b47ba9fdSMark Brown 		ctrl = 3 << WM831X_DC1_DVS_SRC_SHIFT;
423b47ba9fdSMark Brown 		break;
424b47ba9fdSMark Brown 	default:
425b47ba9fdSMark Brown 		dev_err(wm831x->dev, "Invalid DVS control source %d for %s\n",
426b47ba9fdSMark Brown 			pdata->dvs_control_src, dcdc->name);
427b47ba9fdSMark Brown 		return;
428b47ba9fdSMark Brown 	}
429b47ba9fdSMark Brown 
430c439b8f4SMark Brown 	/* If DVS_VSEL is set to the minimum value then raise it to ON_VSEL
431c439b8f4SMark Brown 	 * to make bootstrapping a bit smoother.
432c439b8f4SMark Brown 	 */
433c439b8f4SMark Brown 	if (!dcdc->dvs_vsel) {
434c439b8f4SMark Brown 		ret = wm831x_set_bits(wm831x,
435c439b8f4SMark Brown 				      dcdc->base + WM831X_DCDC_DVS_CONTROL,
436c439b8f4SMark Brown 				      WM831X_DC1_DVS_VSEL_MASK, dcdc->on_vsel);
437c439b8f4SMark Brown 		if (ret == 0)
438c439b8f4SMark Brown 			dcdc->dvs_vsel = dcdc->on_vsel;
439c439b8f4SMark Brown 		else
440c439b8f4SMark Brown 			dev_warn(wm831x->dev, "Failed to set DVS_VSEL: %d\n",
441c439b8f4SMark Brown 				 ret);
442c439b8f4SMark Brown 	}
443c439b8f4SMark Brown 
444b47ba9fdSMark Brown 	ret = wm831x_set_bits(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL,
445b47ba9fdSMark Brown 			      WM831X_DC1_DVS_SRC_MASK, ctrl);
446b47ba9fdSMark Brown 	if (ret < 0) {
447b47ba9fdSMark Brown 		dev_err(wm831x->dev, "Failed to set %s DVS source: %d\n",
448b47ba9fdSMark Brown 			dcdc->name, ret);
449b47ba9fdSMark Brown 	}
450e24a04c4SMark Brown }
451e24a04c4SMark Brown 
452a5023574SBill Pemberton static int wm831x_buckv_probe(struct platform_device *pdev)
453e4ee831fSMark Brown {
454e4ee831fSMark Brown 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
455dff91d0bSJingoo Han 	struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
456c172708dSMark Brown 	struct regulator_config config = { };
457137a6354SMark Brown 	int id;
458e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc;
459e4ee831fSMark Brown 	struct resource *res;
460e4ee831fSMark Brown 	int ret, irq;
461e4ee831fSMark Brown 
462137a6354SMark Brown 	if (pdata && pdata->wm831x_num)
463137a6354SMark Brown 		id = (pdata->wm831x_num * 10) + 1;
464137a6354SMark Brown 	else
465137a6354SMark Brown 		id = 0;
466137a6354SMark Brown 	id = pdev->id - id;
467137a6354SMark Brown 
468e4ee831fSMark Brown 	dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
469e4ee831fSMark Brown 
470fded2f4fSMark Brown 	dcdc = devm_kzalloc(&pdev->dev,  sizeof(struct wm831x_dcdc),
471fded2f4fSMark Brown 			    GFP_KERNEL);
472e4ee831fSMark Brown 	if (dcdc == NULL) {
473e4ee831fSMark Brown 		dev_err(&pdev->dev, "Unable to allocate private data\n");
474e4ee831fSMark Brown 		return -ENOMEM;
475e4ee831fSMark Brown 	}
476e4ee831fSMark Brown 
477e4ee831fSMark Brown 	dcdc->wm831x = wm831x;
478e4ee831fSMark Brown 
4795656098eSMark Brown 	res = platform_get_resource(pdev, IORESOURCE_REG, 0);
480e4ee831fSMark Brown 	if (res == NULL) {
4815656098eSMark Brown 		dev_err(&pdev->dev, "No REG resource\n");
482e4ee831fSMark Brown 		ret = -EINVAL;
483e4ee831fSMark Brown 		goto err;
484e4ee831fSMark Brown 	}
485e4ee831fSMark Brown 	dcdc->base = res->start;
486e4ee831fSMark Brown 
487e4ee831fSMark Brown 	snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
488e4ee831fSMark Brown 	dcdc->desc.name = dcdc->name;
48982caa978SMark Brown 
49082caa978SMark Brown 	snprintf(dcdc->supply_name, sizeof(dcdc->supply_name),
49182caa978SMark Brown 		 "DC%dVDD", id + 1);
49282caa978SMark Brown 	dcdc->desc.supply_name = dcdc->supply_name;
49382caa978SMark Brown 
494e4ee831fSMark Brown 	dcdc->desc.id = id;
495e4ee831fSMark Brown 	dcdc->desc.type = REGULATOR_VOLTAGE;
496e4ee831fSMark Brown 	dcdc->desc.n_voltages = WM831X_BUCKV_MAX_SELECTOR + 1;
497e4ee831fSMark Brown 	dcdc->desc.ops = &wm831x_buckv_ops;
498e4ee831fSMark Brown 	dcdc->desc.owner = THIS_MODULE;
4993d138fccSMark Brown 	dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
5003d138fccSMark Brown 	dcdc->desc.enable_mask = 1 << id;
501e4ee831fSMark Brown 
502e24a04c4SMark Brown 	ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG);
503e24a04c4SMark Brown 	if (ret < 0) {
504e24a04c4SMark Brown 		dev_err(wm831x->dev, "Failed to read ON VSEL: %d\n", ret);
505e24a04c4SMark Brown 		goto err;
506e24a04c4SMark Brown 	}
507e24a04c4SMark Brown 	dcdc->on_vsel = ret & WM831X_DC1_ON_VSEL_MASK;
508e24a04c4SMark Brown 
509a1b81dd3SMark Brown 	ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL);
510e24a04c4SMark Brown 	if (ret < 0) {
511e24a04c4SMark Brown 		dev_err(wm831x->dev, "Failed to read DVS VSEL: %d\n", ret);
512e24a04c4SMark Brown 		goto err;
513e24a04c4SMark Brown 	}
514e24a04c4SMark Brown 	dcdc->dvs_vsel = ret & WM831X_DC1_DVS_VSEL_MASK;
515e24a04c4SMark Brown 
516f0b067d9SMark Brown 	if (pdata && pdata->dcdc[id])
5171aa20d27SMark Brown 		wm831x_buckv_dvs_init(pdev, dcdc,
5181aa20d27SMark Brown 				      pdata->dcdc[id]->driver_data);
519e24a04c4SMark Brown 
520c172708dSMark Brown 	config.dev = pdev->dev.parent;
521b7ca8788SMark Brown 	if (pdata)
522c172708dSMark Brown 		config.init_data = pdata->dcdc[id];
523c172708dSMark Brown 	config.driver_data = dcdc;
5243d138fccSMark Brown 	config.regmap = wm831x->regmap;
525c172708dSMark Brown 
526c172708dSMark Brown 	dcdc->regulator = regulator_register(&dcdc->desc, &config);
527e4ee831fSMark Brown 	if (IS_ERR(dcdc->regulator)) {
528e4ee831fSMark Brown 		ret = PTR_ERR(dcdc->regulator);
529e4ee831fSMark Brown 		dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
530e4ee831fSMark Brown 			id + 1, ret);
531e4ee831fSMark Brown 		goto err;
532e4ee831fSMark Brown 	}
533e4ee831fSMark Brown 
534cd99758bSMark Brown 	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
535b0c4c0c6SMark Brown 	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
536b0c4c0c6SMark Brown 					wm831x_dcdc_uv_irq,
537dfda9c27SMark Brown 					IRQF_TRIGGER_RISING, dcdc->name, dcdc);
538e4ee831fSMark Brown 	if (ret != 0) {
539e4ee831fSMark Brown 		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
540e4ee831fSMark Brown 			irq, ret);
541e4ee831fSMark Brown 		goto err_regulator;
542e4ee831fSMark Brown 	}
543e4ee831fSMark Brown 
544cd99758bSMark Brown 	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "HC"));
545b0c4c0c6SMark Brown 	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
546b0c4c0c6SMark Brown 					wm831x_dcdc_oc_irq,
547dfda9c27SMark Brown 					IRQF_TRIGGER_RISING, dcdc->name, dcdc);
548e4ee831fSMark Brown 	if (ret != 0) {
549e4ee831fSMark Brown 		dev_err(&pdev->dev, "Failed to request HC IRQ %d: %d\n",
550e4ee831fSMark Brown 			irq, ret);
551b0c4c0c6SMark Brown 		goto err_regulator;
552e4ee831fSMark Brown 	}
553e4ee831fSMark Brown 
554e4ee831fSMark Brown 	platform_set_drvdata(pdev, dcdc);
555e4ee831fSMark Brown 
556e4ee831fSMark Brown 	return 0;
557e4ee831fSMark Brown 
558e4ee831fSMark Brown err_regulator:
559e4ee831fSMark Brown 	regulator_unregister(dcdc->regulator);
560e4ee831fSMark Brown err:
561e4ee831fSMark Brown 	return ret;
562e4ee831fSMark Brown }
563e4ee831fSMark Brown 
5648dc995f5SBill Pemberton static int wm831x_buckv_remove(struct platform_device *pdev)
565e4ee831fSMark Brown {
566e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
567e4ee831fSMark Brown 
568e4ee831fSMark Brown 	regulator_unregister(dcdc->regulator);
569e4ee831fSMark Brown 
570e4ee831fSMark Brown 	return 0;
571e4ee831fSMark Brown }
572e4ee831fSMark Brown 
573e4ee831fSMark Brown static struct platform_driver wm831x_buckv_driver = {
574e4ee831fSMark Brown 	.probe = wm831x_buckv_probe,
5755eb9f2b9SBill Pemberton 	.remove = wm831x_buckv_remove,
576e4ee831fSMark Brown 	.driver		= {
577e4ee831fSMark Brown 		.name	= "wm831x-buckv",
578eb66d565SDmitry Torokhov 		.owner	= THIS_MODULE,
579e4ee831fSMark Brown 	},
580e4ee831fSMark Brown };
581e4ee831fSMark Brown 
582e4ee831fSMark Brown /*
583e4ee831fSMark Brown  * BUCKP specifics
584e4ee831fSMark Brown  */
585e4ee831fSMark Brown 
586d580cb5eSAxel Lin static int wm831x_buckp_set_suspend_voltage(struct regulator_dev *rdev, int uV)
587e4ee831fSMark Brown {
588e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
589e4ee831fSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
590e4ee831fSMark Brown 	u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
591d580cb5eSAxel Lin 	int sel;
592e4ee831fSMark Brown 
593d580cb5eSAxel Lin 	sel = regulator_map_voltage_linear(rdev, uV, uV);
594d580cb5eSAxel Lin 	if (sel < 0)
595d580cb5eSAxel Lin 		return sel;
596d580cb5eSAxel Lin 
597d580cb5eSAxel Lin 	return wm831x_set_bits(wm831x, reg, WM831X_DC3_ON_VSEL_MASK, sel);
598e4ee831fSMark Brown }
599e4ee831fSMark Brown 
600e4ee831fSMark Brown static struct regulator_ops wm831x_buckp_ops = {
601d580cb5eSAxel Lin 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
602817436e7SMark Brown 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
603c70ad9dcSAxel Lin 	.list_voltage = regulator_list_voltage_linear,
604d580cb5eSAxel Lin 	.map_voltage = regulator_map_voltage_linear,
605e4ee831fSMark Brown 	.set_suspend_voltage = wm831x_buckp_set_suspend_voltage,
606e4ee831fSMark Brown 
6073d138fccSMark Brown 	.is_enabled = regulator_is_enabled_regmap,
6083d138fccSMark Brown 	.enable = regulator_enable_regmap,
6093d138fccSMark Brown 	.disable = regulator_disable_regmap,
610e4ee831fSMark Brown 	.get_status = wm831x_dcdc_get_status,
611e4ee831fSMark Brown 	.get_mode = wm831x_dcdc_get_mode,
612e4ee831fSMark Brown 	.set_mode = wm831x_dcdc_set_mode,
613e4ee831fSMark Brown 	.set_suspend_mode = wm831x_dcdc_set_suspend_mode,
614e4ee831fSMark Brown };
615e4ee831fSMark Brown 
616a5023574SBill Pemberton static int wm831x_buckp_probe(struct platform_device *pdev)
617e4ee831fSMark Brown {
618e4ee831fSMark Brown 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
619dff91d0bSJingoo Han 	struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
620c172708dSMark Brown 	struct regulator_config config = { };
621137a6354SMark Brown 	int id;
622e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc;
623e4ee831fSMark Brown 	struct resource *res;
624e4ee831fSMark Brown 	int ret, irq;
625e4ee831fSMark Brown 
626137a6354SMark Brown 	if (pdata && pdata->wm831x_num)
627137a6354SMark Brown 		id = (pdata->wm831x_num * 10) + 1;
628137a6354SMark Brown 	else
629137a6354SMark Brown 		id = 0;
630137a6354SMark Brown 	id = pdev->id - id;
631137a6354SMark Brown 
632e4ee831fSMark Brown 	dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
633e4ee831fSMark Brown 
634fded2f4fSMark Brown 	dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc),
635fded2f4fSMark Brown 			    GFP_KERNEL);
636e4ee831fSMark Brown 	if (dcdc == NULL) {
637e4ee831fSMark Brown 		dev_err(&pdev->dev, "Unable to allocate private data\n");
638e4ee831fSMark Brown 		return -ENOMEM;
639e4ee831fSMark Brown 	}
640e4ee831fSMark Brown 
641e4ee831fSMark Brown 	dcdc->wm831x = wm831x;
642e4ee831fSMark Brown 
6435656098eSMark Brown 	res = platform_get_resource(pdev, IORESOURCE_REG, 0);
644e4ee831fSMark Brown 	if (res == NULL) {
6455656098eSMark Brown 		dev_err(&pdev->dev, "No REG resource\n");
646e4ee831fSMark Brown 		ret = -EINVAL;
647e4ee831fSMark Brown 		goto err;
648e4ee831fSMark Brown 	}
649e4ee831fSMark Brown 	dcdc->base = res->start;
650e4ee831fSMark Brown 
651e4ee831fSMark Brown 	snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
652e4ee831fSMark Brown 	dcdc->desc.name = dcdc->name;
65382caa978SMark Brown 
65482caa978SMark Brown 	snprintf(dcdc->supply_name, sizeof(dcdc->supply_name),
65582caa978SMark Brown 		 "DC%dVDD", id + 1);
65682caa978SMark Brown 	dcdc->desc.supply_name = dcdc->supply_name;
65782caa978SMark Brown 
658e4ee831fSMark Brown 	dcdc->desc.id = id;
659e4ee831fSMark Brown 	dcdc->desc.type = REGULATOR_VOLTAGE;
660e4ee831fSMark Brown 	dcdc->desc.n_voltages = WM831X_BUCKP_MAX_SELECTOR + 1;
661e4ee831fSMark Brown 	dcdc->desc.ops = &wm831x_buckp_ops;
662e4ee831fSMark Brown 	dcdc->desc.owner = THIS_MODULE;
663817436e7SMark Brown 	dcdc->desc.vsel_reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
664817436e7SMark Brown 	dcdc->desc.vsel_mask = WM831X_DC3_ON_VSEL_MASK;
6653d138fccSMark Brown 	dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
6663d138fccSMark Brown 	dcdc->desc.enable_mask = 1 << id;
667c70ad9dcSAxel Lin 	dcdc->desc.min_uV = 850000;
668c70ad9dcSAxel Lin 	dcdc->desc.uV_step = 25000;
669e4ee831fSMark Brown 
670c172708dSMark Brown 	config.dev = pdev->dev.parent;
671b7ca8788SMark Brown 	if (pdata)
672c172708dSMark Brown 		config.init_data = pdata->dcdc[id];
673c172708dSMark Brown 	config.driver_data = dcdc;
674817436e7SMark Brown 	config.regmap = wm831x->regmap;
675c172708dSMark Brown 
676c172708dSMark Brown 	dcdc->regulator = regulator_register(&dcdc->desc, &config);
677e4ee831fSMark Brown 	if (IS_ERR(dcdc->regulator)) {
678e4ee831fSMark Brown 		ret = PTR_ERR(dcdc->regulator);
679e4ee831fSMark Brown 		dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
680e4ee831fSMark Brown 			id + 1, ret);
681e4ee831fSMark Brown 		goto err;
682e4ee831fSMark Brown 	}
683e4ee831fSMark Brown 
684cd99758bSMark Brown 	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
685b0c4c0c6SMark Brown 	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
686b0c4c0c6SMark Brown 					wm831x_dcdc_uv_irq,
687dfda9c27SMark Brown 					IRQF_TRIGGER_RISING, dcdc->name, dcdc);
688e4ee831fSMark Brown 	if (ret != 0) {
689e4ee831fSMark Brown 		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
690e4ee831fSMark Brown 			irq, ret);
691e4ee831fSMark Brown 		goto err_regulator;
692e4ee831fSMark Brown 	}
693e4ee831fSMark Brown 
694e4ee831fSMark Brown 	platform_set_drvdata(pdev, dcdc);
695e4ee831fSMark Brown 
696e4ee831fSMark Brown 	return 0;
697e4ee831fSMark Brown 
698e4ee831fSMark Brown err_regulator:
699e4ee831fSMark Brown 	regulator_unregister(dcdc->regulator);
700e4ee831fSMark Brown err:
701e4ee831fSMark Brown 	return ret;
702e4ee831fSMark Brown }
703e4ee831fSMark Brown 
7048dc995f5SBill Pemberton static int wm831x_buckp_remove(struct platform_device *pdev)
705e4ee831fSMark Brown {
706e4ee831fSMark Brown 	struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
707e4ee831fSMark Brown 
708e4ee831fSMark Brown 	regulator_unregister(dcdc->regulator);
709e4ee831fSMark Brown 
710e4ee831fSMark Brown 	return 0;
711e4ee831fSMark Brown }
712e4ee831fSMark Brown 
713e4ee831fSMark Brown static struct platform_driver wm831x_buckp_driver = {
714e4ee831fSMark Brown 	.probe = wm831x_buckp_probe,
7155eb9f2b9SBill Pemberton 	.remove = wm831x_buckp_remove,
716e4ee831fSMark Brown 	.driver		= {
717e4ee831fSMark Brown 		.name	= "wm831x-buckp",
718eb66d565SDmitry Torokhov 		.owner	= THIS_MODULE,
719e4ee831fSMark Brown 	},
720e4ee831fSMark Brown };
721e4ee831fSMark Brown 
7228267a9baSMark Brown /*
7231304850dSMark Brown  * DCDC boost convertors
7241304850dSMark Brown  */
7251304850dSMark Brown 
7261304850dSMark Brown static int wm831x_boostp_get_status(struct regulator_dev *rdev)
7271304850dSMark Brown {
7281304850dSMark Brown 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
7291304850dSMark Brown 	struct wm831x *wm831x = dcdc->wm831x;
7301304850dSMark Brown 	int ret;
7311304850dSMark Brown 
7321304850dSMark Brown 	/* First, check for errors */
7331304850dSMark Brown 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
7341304850dSMark Brown 	if (ret < 0)
7351304850dSMark Brown 		return ret;
7361304850dSMark Brown 
7371304850dSMark Brown 	if (ret & (1 << rdev_get_id(rdev))) {
7381304850dSMark Brown 		dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
7391304850dSMark Brown 			rdev_get_id(rdev) + 1);
7401304850dSMark Brown 		return REGULATOR_STATUS_ERROR;
7411304850dSMark Brown 	}
7421304850dSMark Brown 
7431304850dSMark Brown 	/* Is the regulator on? */
7441304850dSMark Brown 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
7451304850dSMark Brown 	if (ret < 0)
7461304850dSMark Brown 		return ret;
7471304850dSMark Brown 	if (ret & (1 << rdev_get_id(rdev)))
7481304850dSMark Brown 		return REGULATOR_STATUS_ON;
7491304850dSMark Brown 	else
7501304850dSMark Brown 		return REGULATOR_STATUS_OFF;
7511304850dSMark Brown }
7521304850dSMark Brown 
7531304850dSMark Brown static struct regulator_ops wm831x_boostp_ops = {
7541304850dSMark Brown 	.get_status = wm831x_boostp_get_status,
7551304850dSMark Brown 
7563d138fccSMark Brown 	.is_enabled = regulator_is_enabled_regmap,
7573d138fccSMark Brown 	.enable = regulator_enable_regmap,
7583d138fccSMark Brown 	.disable = regulator_disable_regmap,
7591304850dSMark Brown };
7601304850dSMark Brown 
761a5023574SBill Pemberton static int wm831x_boostp_probe(struct platform_device *pdev)
7621304850dSMark Brown {
7631304850dSMark Brown 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
764dff91d0bSJingoo Han 	struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
765c172708dSMark Brown 	struct regulator_config config = { };
7661304850dSMark Brown 	int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
7671304850dSMark Brown 	struct wm831x_dcdc *dcdc;
7681304850dSMark Brown 	struct resource *res;
7691304850dSMark Brown 	int ret, irq;
7701304850dSMark Brown 
7711304850dSMark Brown 	dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
7721304850dSMark Brown 
7731304850dSMark Brown 	if (pdata == NULL || pdata->dcdc[id] == NULL)
7741304850dSMark Brown 		return -ENODEV;
7751304850dSMark Brown 
7764c60165dSAxel Lin 	dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), GFP_KERNEL);
7771304850dSMark Brown 	if (dcdc == NULL) {
7781304850dSMark Brown 		dev_err(&pdev->dev, "Unable to allocate private data\n");
7791304850dSMark Brown 		return -ENOMEM;
7801304850dSMark Brown 	}
7811304850dSMark Brown 
7821304850dSMark Brown 	dcdc->wm831x = wm831x;
7831304850dSMark Brown 
7845656098eSMark Brown 	res = platform_get_resource(pdev, IORESOURCE_REG, 0);
7851304850dSMark Brown 	if (res == NULL) {
7865656098eSMark Brown 		dev_err(&pdev->dev, "No REG resource\n");
7871304850dSMark Brown 		ret = -EINVAL;
7881304850dSMark Brown 		goto err;
7891304850dSMark Brown 	}
7901304850dSMark Brown 	dcdc->base = res->start;
7911304850dSMark Brown 
7921304850dSMark Brown 	snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
7931304850dSMark Brown 	dcdc->desc.name = dcdc->name;
7941304850dSMark Brown 	dcdc->desc.id = id;
7951304850dSMark Brown 	dcdc->desc.type = REGULATOR_VOLTAGE;
7961304850dSMark Brown 	dcdc->desc.ops = &wm831x_boostp_ops;
7971304850dSMark Brown 	dcdc->desc.owner = THIS_MODULE;
7983d138fccSMark Brown 	dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
7993d138fccSMark Brown 	dcdc->desc.enable_mask = 1 << id;
8001304850dSMark Brown 
801c172708dSMark Brown 	config.dev = pdev->dev.parent;
802f0b067d9SMark Brown 	if (pdata)
803c172708dSMark Brown 		config.init_data = pdata->dcdc[id];
804c172708dSMark Brown 	config.driver_data = dcdc;
8053d138fccSMark Brown 	config.regmap = wm831x->regmap;
806c172708dSMark Brown 
807c172708dSMark Brown 	dcdc->regulator = regulator_register(&dcdc->desc, &config);
8081304850dSMark Brown 	if (IS_ERR(dcdc->regulator)) {
8091304850dSMark Brown 		ret = PTR_ERR(dcdc->regulator);
8101304850dSMark Brown 		dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
8111304850dSMark Brown 			id + 1, ret);
8121304850dSMark Brown 		goto err;
8131304850dSMark Brown 	}
8141304850dSMark Brown 
815cd99758bSMark Brown 	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
816b0c4c0c6SMark Brown 	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
817b0c4c0c6SMark Brown 					wm831x_dcdc_uv_irq,
8181304850dSMark Brown 					IRQF_TRIGGER_RISING, dcdc->name,
8191304850dSMark Brown 					dcdc);
8201304850dSMark Brown 	if (ret != 0) {
8211304850dSMark Brown 		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
8221304850dSMark Brown 			irq, ret);
8231304850dSMark Brown 		goto err_regulator;
8241304850dSMark Brown 	}
8251304850dSMark Brown 
8261304850dSMark Brown 	platform_set_drvdata(pdev, dcdc);
8271304850dSMark Brown 
8281304850dSMark Brown 	return 0;
8291304850dSMark Brown 
8301304850dSMark Brown err_regulator:
8311304850dSMark Brown 	regulator_unregister(dcdc->regulator);
8321304850dSMark Brown err:
8331304850dSMark Brown 	return ret;
8341304850dSMark Brown }
8351304850dSMark Brown 
8368dc995f5SBill Pemberton static int wm831x_boostp_remove(struct platform_device *pdev)
8371304850dSMark Brown {
8381304850dSMark Brown 	struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
8391304850dSMark Brown 
8401304850dSMark Brown 	regulator_unregister(dcdc->regulator);
8411304850dSMark Brown 
8421304850dSMark Brown 	return 0;
8431304850dSMark Brown }
8441304850dSMark Brown 
8451304850dSMark Brown static struct platform_driver wm831x_boostp_driver = {
8461304850dSMark Brown 	.probe = wm831x_boostp_probe,
8475eb9f2b9SBill Pemberton 	.remove = wm831x_boostp_remove,
8481304850dSMark Brown 	.driver		= {
8491304850dSMark Brown 		.name	= "wm831x-boostp",
850eb66d565SDmitry Torokhov 		.owner	= THIS_MODULE,
8511304850dSMark Brown 	},
8521304850dSMark Brown };
8531304850dSMark Brown 
8541304850dSMark Brown /*
8558267a9baSMark Brown  * External Power Enable
8568267a9baSMark Brown  *
8578267a9baSMark Brown  * These aren't actually DCDCs but look like them in hardware so share
8588267a9baSMark Brown  * code.
8598267a9baSMark Brown  */
8608267a9baSMark Brown 
8618267a9baSMark Brown #define WM831X_EPE_BASE 6
8628267a9baSMark Brown 
8638267a9baSMark Brown static struct regulator_ops wm831x_epe_ops = {
8643d138fccSMark Brown 	.is_enabled = regulator_is_enabled_regmap,
8653d138fccSMark Brown 	.enable = regulator_enable_regmap,
8663d138fccSMark Brown 	.disable = regulator_disable_regmap,
8678267a9baSMark Brown 	.get_status = wm831x_dcdc_get_status,
8688267a9baSMark Brown };
8698267a9baSMark Brown 
870a5023574SBill Pemberton static int wm831x_epe_probe(struct platform_device *pdev)
8718267a9baSMark Brown {
8728267a9baSMark Brown 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
873dff91d0bSJingoo Han 	struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
874c172708dSMark Brown 	struct regulator_config config = { };
8758267a9baSMark Brown 	int id = pdev->id % ARRAY_SIZE(pdata->epe);
8768267a9baSMark Brown 	struct wm831x_dcdc *dcdc;
8778267a9baSMark Brown 	int ret;
8788267a9baSMark Brown 
8798267a9baSMark Brown 	dev_dbg(&pdev->dev, "Probing EPE%d\n", id + 1);
8808267a9baSMark Brown 
8814c60165dSAxel Lin 	dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), GFP_KERNEL);
8828267a9baSMark Brown 	if (dcdc == NULL) {
8838267a9baSMark Brown 		dev_err(&pdev->dev, "Unable to allocate private data\n");
8848267a9baSMark Brown 		return -ENOMEM;
8858267a9baSMark Brown 	}
8868267a9baSMark Brown 
8878267a9baSMark Brown 	dcdc->wm831x = wm831x;
8888267a9baSMark Brown 
8898267a9baSMark Brown 	/* For current parts this is correct; probably need to revisit
8908267a9baSMark Brown 	 * in future.
8918267a9baSMark Brown 	 */
8928267a9baSMark Brown 	snprintf(dcdc->name, sizeof(dcdc->name), "EPE%d", id + 1);
8938267a9baSMark Brown 	dcdc->desc.name = dcdc->name;
8948267a9baSMark Brown 	dcdc->desc.id = id + WM831X_EPE_BASE; /* Offset in DCDC registers */
8958267a9baSMark Brown 	dcdc->desc.ops = &wm831x_epe_ops;
8968267a9baSMark Brown 	dcdc->desc.type = REGULATOR_VOLTAGE;
8978267a9baSMark Brown 	dcdc->desc.owner = THIS_MODULE;
8983d138fccSMark Brown 	dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
8993d138fccSMark Brown 	dcdc->desc.enable_mask = 1 << dcdc->desc.id;
9008267a9baSMark Brown 
901c172708dSMark Brown 	config.dev = pdev->dev.parent;
902b7ca8788SMark Brown 	if (pdata)
903c172708dSMark Brown 		config.init_data = pdata->epe[id];
904c172708dSMark Brown 	config.driver_data = dcdc;
9053d138fccSMark Brown 	config.regmap = wm831x->regmap;
906c172708dSMark Brown 
907c172708dSMark Brown 	dcdc->regulator = regulator_register(&dcdc->desc, &config);
9088267a9baSMark Brown 	if (IS_ERR(dcdc->regulator)) {
9098267a9baSMark Brown 		ret = PTR_ERR(dcdc->regulator);
9108267a9baSMark Brown 		dev_err(wm831x->dev, "Failed to register EPE%d: %d\n",
9118267a9baSMark Brown 			id + 1, ret);
9128267a9baSMark Brown 		goto err;
9138267a9baSMark Brown 	}
9148267a9baSMark Brown 
9158267a9baSMark Brown 	platform_set_drvdata(pdev, dcdc);
9168267a9baSMark Brown 
9178267a9baSMark Brown 	return 0;
9188267a9baSMark Brown 
9198267a9baSMark Brown err:
9208267a9baSMark Brown 	return ret;
9218267a9baSMark Brown }
9228267a9baSMark Brown 
9238dc995f5SBill Pemberton static int wm831x_epe_remove(struct platform_device *pdev)
9248267a9baSMark Brown {
9258267a9baSMark Brown 	struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
9268267a9baSMark Brown 
9278267a9baSMark Brown 	regulator_unregister(dcdc->regulator);
9288267a9baSMark Brown 
9298267a9baSMark Brown 	return 0;
9308267a9baSMark Brown }
9318267a9baSMark Brown 
9328267a9baSMark Brown static struct platform_driver wm831x_epe_driver = {
9338267a9baSMark Brown 	.probe = wm831x_epe_probe,
9345eb9f2b9SBill Pemberton 	.remove = wm831x_epe_remove,
9358267a9baSMark Brown 	.driver		= {
9368267a9baSMark Brown 		.name	= "wm831x-epe",
937eb66d565SDmitry Torokhov 		.owner	= THIS_MODULE,
9388267a9baSMark Brown 	},
9398267a9baSMark Brown };
9408267a9baSMark Brown 
941e4ee831fSMark Brown static int __init wm831x_dcdc_init(void)
942e4ee831fSMark Brown {
943e4ee831fSMark Brown 	int ret;
944e4ee831fSMark Brown 	ret = platform_driver_register(&wm831x_buckv_driver);
945e4ee831fSMark Brown 	if (ret != 0)
946e4ee831fSMark Brown 		pr_err("Failed to register WM831x BUCKV driver: %d\n", ret);
947e4ee831fSMark Brown 
948e4ee831fSMark Brown 	ret = platform_driver_register(&wm831x_buckp_driver);
949e4ee831fSMark Brown 	if (ret != 0)
950e4ee831fSMark Brown 		pr_err("Failed to register WM831x BUCKP driver: %d\n", ret);
951e4ee831fSMark Brown 
9521304850dSMark Brown 	ret = platform_driver_register(&wm831x_boostp_driver);
9531304850dSMark Brown 	if (ret != 0)
9541304850dSMark Brown 		pr_err("Failed to register WM831x BOOST driver: %d\n", ret);
9551304850dSMark Brown 
9568267a9baSMark Brown 	ret = platform_driver_register(&wm831x_epe_driver);
9578267a9baSMark Brown 	if (ret != 0)
9588267a9baSMark Brown 		pr_err("Failed to register WM831x EPE driver: %d\n", ret);
9598267a9baSMark Brown 
960e4ee831fSMark Brown 	return 0;
961e4ee831fSMark Brown }
962e4ee831fSMark Brown subsys_initcall(wm831x_dcdc_init);
963e4ee831fSMark Brown 
964e4ee831fSMark Brown static void __exit wm831x_dcdc_exit(void)
965e4ee831fSMark Brown {
9668267a9baSMark Brown 	platform_driver_unregister(&wm831x_epe_driver);
9671304850dSMark Brown 	platform_driver_unregister(&wm831x_boostp_driver);
968e4ee831fSMark Brown 	platform_driver_unregister(&wm831x_buckp_driver);
969e4ee831fSMark Brown 	platform_driver_unregister(&wm831x_buckv_driver);
970e4ee831fSMark Brown }
971e4ee831fSMark Brown module_exit(wm831x_dcdc_exit);
972e4ee831fSMark Brown 
973e4ee831fSMark Brown /* Module information */
974e4ee831fSMark Brown MODULE_AUTHOR("Mark Brown");
975e4ee831fSMark Brown MODULE_DESCRIPTION("WM831x DC-DC convertor driver");
976e4ee831fSMark Brown MODULE_LICENSE("GPL");
977e4ee831fSMark Brown MODULE_ALIAS("platform:wm831x-buckv");
978e4ee831fSMark Brown MODULE_ALIAS("platform:wm831x-buckp");
97943f1f216SAxel Lin MODULE_ALIAS("platform:wm831x-boostp");
98024b43150SMark Brown MODULE_ALIAS("platform:wm831x-epe");
981