blob: a0982e80985198004ae990218c38718d8616eec6 [file] [log] [blame]
Mark Browne4ee8312009-07-28 15:21:49 +01001/*
2 * wm831x-dcdc.c -- DC-DC buck convertor driver for the WM831x series
3 *
4 * Copyright 2009 Wolfson Microelectronics PLC.
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 */
13
14#include <linux/module.h>
15#include <linux/moduleparam.h>
16#include <linux/init.h>
17#include <linux/bitops.h>
18#include <linux/err.h>
19#include <linux/i2c.h>
20#include <linux/platform_device.h>
21#include <linux/regulator/driver.h>
Mark Browne24a04c2009-09-22 08:47:11 -070022#include <linux/regulator/machine.h>
23#include <linux/gpio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Mark Browne4ee8312009-07-28 15:21:49 +010025
26#include <linux/mfd/wm831x/core.h>
27#include <linux/mfd/wm831x/regulator.h>
28#include <linux/mfd/wm831x/pdata.h>
29
30#define WM831X_BUCKV_MAX_SELECTOR 0x68
31#define WM831X_BUCKP_MAX_SELECTOR 0x66
32
33#define WM831X_DCDC_MODE_FAST 0
34#define WM831X_DCDC_MODE_NORMAL 1
35#define WM831X_DCDC_MODE_IDLE 2
36#define WM831X_DCDC_MODE_STANDBY 3
37
38#define WM831X_DCDC_MAX_NAME 6
39
40/* Register offsets in control block */
41#define WM831X_DCDC_CONTROL_1 0
42#define WM831X_DCDC_CONTROL_2 1
43#define WM831X_DCDC_ON_CONFIG 2
44#define WM831X_DCDC_SLEEP_CONTROL 3
Mark Browne24a04c2009-09-22 08:47:11 -070045#define WM831X_DCDC_DVS_CONTROL 4
Mark Browne4ee8312009-07-28 15:21:49 +010046
47/*
48 * Shared
49 */
50
51struct wm831x_dcdc {
52 char name[WM831X_DCDC_MAX_NAME];
53 struct regulator_desc desc;
54 int base;
55 struct wm831x *wm831x;
56 struct regulator_dev *regulator;
Mark Browne24a04c2009-09-22 08:47:11 -070057 int dvs_gpio;
58 int dvs_gpio_state;
59 int on_vsel;
60 int dvs_vsel;
Mark Browne4ee8312009-07-28 15:21:49 +010061};
62
63static int wm831x_dcdc_is_enabled(struct regulator_dev *rdev)
64{
65 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
66 struct wm831x *wm831x = dcdc->wm831x;
67 int mask = 1 << rdev_get_id(rdev);
68 int reg;
69
70 reg = wm831x_reg_read(wm831x, WM831X_DCDC_ENABLE);
71 if (reg < 0)
72 return reg;
73
74 if (reg & mask)
75 return 1;
76 else
77 return 0;
78}
79
80static int wm831x_dcdc_enable(struct regulator_dev *rdev)
81{
82 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
83 struct wm831x *wm831x = dcdc->wm831x;
84 int mask = 1 << rdev_get_id(rdev);
85
86 return wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE, mask, mask);
87}
88
89static int wm831x_dcdc_disable(struct regulator_dev *rdev)
90{
91 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
92 struct wm831x *wm831x = dcdc->wm831x;
93 int mask = 1 << rdev_get_id(rdev);
94
95 return wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE, mask, 0);
96}
97
98static unsigned int wm831x_dcdc_get_mode(struct regulator_dev *rdev)
99
100{
101 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
102 struct wm831x *wm831x = dcdc->wm831x;
103 u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
104 int val;
105
106 val = wm831x_reg_read(wm831x, reg);
107 if (val < 0)
108 return val;
109
110 val = (val & WM831X_DC1_ON_MODE_MASK) >> WM831X_DC1_ON_MODE_SHIFT;
111
112 switch (val) {
113 case WM831X_DCDC_MODE_FAST:
114 return REGULATOR_MODE_FAST;
115 case WM831X_DCDC_MODE_NORMAL:
116 return REGULATOR_MODE_NORMAL;
117 case WM831X_DCDC_MODE_STANDBY:
118 return REGULATOR_MODE_STANDBY;
119 case WM831X_DCDC_MODE_IDLE:
120 return REGULATOR_MODE_IDLE;
121 default:
122 BUG();
Mark Brown9ee291a2011-02-02 20:17:22 +0000123 return -EINVAL;
Mark Browne4ee8312009-07-28 15:21:49 +0100124 }
125}
126
127static int wm831x_dcdc_set_mode_int(struct wm831x *wm831x, int reg,
128 unsigned int mode)
129{
130 int val;
131
132 switch (mode) {
133 case REGULATOR_MODE_FAST:
134 val = WM831X_DCDC_MODE_FAST;
135 break;
136 case REGULATOR_MODE_NORMAL:
137 val = WM831X_DCDC_MODE_NORMAL;
138 break;
139 case REGULATOR_MODE_STANDBY:
140 val = WM831X_DCDC_MODE_STANDBY;
141 break;
142 case REGULATOR_MODE_IDLE:
143 val = WM831X_DCDC_MODE_IDLE;
144 break;
145 default:
146 return -EINVAL;
147 }
148
149 return wm831x_set_bits(wm831x, reg, WM831X_DC1_ON_MODE_MASK,
150 val << WM831X_DC1_ON_MODE_SHIFT);
151}
152
153static int wm831x_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
154{
155 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
156 struct wm831x *wm831x = dcdc->wm831x;
157 u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
158
159 return wm831x_dcdc_set_mode_int(wm831x, reg, mode);
160}
161
162static int wm831x_dcdc_set_suspend_mode(struct regulator_dev *rdev,
163 unsigned int mode)
164{
165 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
166 struct wm831x *wm831x = dcdc->wm831x;
167 u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
168
169 return wm831x_dcdc_set_mode_int(wm831x, reg, mode);
170}
171
172static int wm831x_dcdc_get_status(struct regulator_dev *rdev)
173{
174 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
175 struct wm831x *wm831x = dcdc->wm831x;
176 int ret;
177
178 /* First, check for errors */
179 ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
180 if (ret < 0)
181 return ret;
182
183 if (ret & (1 << rdev_get_id(rdev))) {
184 dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
185 rdev_get_id(rdev) + 1);
186 return REGULATOR_STATUS_ERROR;
187 }
188
189 /* DCDC1 and DCDC2 can additionally detect high voltage/current */
190 if (rdev_get_id(rdev) < 2) {
191 if (ret & (WM831X_DC1_OV_STS << rdev_get_id(rdev))) {
192 dev_dbg(wm831x->dev, "DCDC%d over voltage\n",
193 rdev_get_id(rdev) + 1);
194 return REGULATOR_STATUS_ERROR;
195 }
196
197 if (ret & (WM831X_DC1_HC_STS << rdev_get_id(rdev))) {
198 dev_dbg(wm831x->dev, "DCDC%d over current\n",
199 rdev_get_id(rdev) + 1);
200 return REGULATOR_STATUS_ERROR;
201 }
202 }
203
204 /* Is the regulator on? */
205 ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
206 if (ret < 0)
207 return ret;
208 if (!(ret & (1 << rdev_get_id(rdev))))
209 return REGULATOR_STATUS_OFF;
210
211 /* TODO: When we handle hardware control modes so we can report the
212 * current mode. */
213 return REGULATOR_STATUS_ON;
214}
215
216static irqreturn_t wm831x_dcdc_uv_irq(int irq, void *data)
217{
218 struct wm831x_dcdc *dcdc = data;
219
220 regulator_notifier_call_chain(dcdc->regulator,
221 REGULATOR_EVENT_UNDER_VOLTAGE,
222 NULL);
223
224 return IRQ_HANDLED;
225}
226
227static irqreturn_t wm831x_dcdc_oc_irq(int irq, void *data)
228{
229 struct wm831x_dcdc *dcdc = data;
230
231 regulator_notifier_call_chain(dcdc->regulator,
232 REGULATOR_EVENT_OVER_CURRENT,
233 NULL);
234
235 return IRQ_HANDLED;
236}
237
238/*
239 * BUCKV specifics
240 */
241
242static int wm831x_buckv_list_voltage(struct regulator_dev *rdev,
243 unsigned selector)
244{
245 if (selector <= 0x8)
246 return 600000;
247 if (selector <= WM831X_BUCKV_MAX_SELECTOR)
248 return 600000 + ((selector - 0x8) * 12500);
249 return -EINVAL;
250}
251
Mark Browne24a04c2009-09-22 08:47:11 -0700252static int wm831x_buckv_select_min_voltage(struct regulator_dev *rdev,
253 int min_uV, int max_uV)
Mark Browne4ee8312009-07-28 15:21:49 +0100254{
Mark Browne4ee8312009-07-28 15:21:49 +0100255 u16 vsel;
256
257 if (min_uV < 600000)
258 vsel = 0;
259 else if (min_uV <= 1800000)
260 vsel = ((min_uV - 600000) / 12500) + 8;
261 else
262 return -EINVAL;
263
264 if (wm831x_buckv_list_voltage(rdev, vsel) > max_uV)
265 return -EINVAL;
266
Mark Browne24a04c2009-09-22 08:47:11 -0700267 return vsel;
268}
269
270static int wm831x_buckv_select_max_voltage(struct regulator_dev *rdev,
271 int min_uV, int max_uV)
272{
273 u16 vsel;
274
275 if (max_uV < 600000 || max_uV > 1800000)
276 return -EINVAL;
277
278 vsel = ((max_uV - 600000) / 12500) + 8;
279
280 if (wm831x_buckv_list_voltage(rdev, vsel) < min_uV ||
281 wm831x_buckv_list_voltage(rdev, vsel) < max_uV)
282 return -EINVAL;
283
284 return vsel;
285}
286
287static int wm831x_buckv_set_dvs(struct regulator_dev *rdev, int state)
288{
289 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
290
291 if (state == dcdc->dvs_gpio_state)
292 return 0;
293
294 dcdc->dvs_gpio_state = state;
295 gpio_set_value(dcdc->dvs_gpio, state);
296
297 /* Should wait for DVS state change to be asserted if we have
298 * a GPIO for it, for now assume the device is configured
299 * for the fastest possible transition.
300 */
301
302 return 0;
Mark Browne4ee8312009-07-28 15:21:49 +0100303}
304
305static int wm831x_buckv_set_voltage(struct regulator_dev *rdev,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000306 int min_uV, int max_uV, unsigned *selector)
Mark Browne4ee8312009-07-28 15:21:49 +0100307{
308 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
Mark Browne24a04c2009-09-22 08:47:11 -0700309 struct wm831x *wm831x = dcdc->wm831x;
310 int on_reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
311 int dvs_reg = dcdc->base + WM831X_DCDC_DVS_CONTROL;
312 int vsel, ret;
Mark Browne4ee8312009-07-28 15:21:49 +0100313
Mark Browne24a04c2009-09-22 08:47:11 -0700314 vsel = wm831x_buckv_select_min_voltage(rdev, min_uV, max_uV);
315 if (vsel < 0)
316 return vsel;
317
Mark Brown3a93f2a2010-11-10 14:38:29 +0000318 *selector = vsel;
319
Mark Browne24a04c2009-09-22 08:47:11 -0700320 /* If this value is already set then do a GPIO update if we can */
321 if (dcdc->dvs_gpio && dcdc->on_vsel == vsel)
322 return wm831x_buckv_set_dvs(rdev, 0);
323
324 if (dcdc->dvs_gpio && dcdc->dvs_vsel == vsel)
325 return wm831x_buckv_set_dvs(rdev, 1);
326
327 /* Always set the ON status to the minimum voltage */
328 ret = wm831x_set_bits(wm831x, on_reg, WM831X_DC1_ON_VSEL_MASK, vsel);
329 if (ret < 0)
330 return ret;
331 dcdc->on_vsel = vsel;
332
333 if (!dcdc->dvs_gpio)
334 return ret;
335
336 /* Kick the voltage transition now */
337 ret = wm831x_buckv_set_dvs(rdev, 0);
338 if (ret < 0)
339 return ret;
340
341 /* Set the high voltage as the DVS voltage. This is optimised
342 * for CPUfreq usage, most processors will keep the maximum
343 * voltage constant and lower the minimum with the frequency. */
344 vsel = wm831x_buckv_select_max_voltage(rdev, min_uV, max_uV);
345 if (vsel < 0) {
346 /* This should never happen - at worst the same vsel
347 * should be chosen */
348 WARN_ON(vsel < 0);
349 return 0;
350 }
351
352 /* Don't bother if it's the same VSEL we're already using */
353 if (vsel == dcdc->on_vsel)
354 return 0;
355
356 ret = wm831x_set_bits(wm831x, dvs_reg, WM831X_DC1_DVS_VSEL_MASK, vsel);
357 if (ret == 0)
358 dcdc->dvs_vsel = vsel;
359 else
360 dev_warn(wm831x->dev, "Failed to set DCDC DVS VSEL: %d\n",
361 ret);
362
363 return 0;
Mark Browne4ee8312009-07-28 15:21:49 +0100364}
365
366static int wm831x_buckv_set_suspend_voltage(struct regulator_dev *rdev,
Mark Browne24a04c2009-09-22 08:47:11 -0700367 int uV)
Mark Browne4ee8312009-07-28 15:21:49 +0100368{
369 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
Mark Browne24a04c2009-09-22 08:47:11 -0700370 struct wm831x *wm831x = dcdc->wm831x;
Mark Browne4ee8312009-07-28 15:21:49 +0100371 u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
Mark Browne24a04c2009-09-22 08:47:11 -0700372 int vsel;
Mark Browne4ee8312009-07-28 15:21:49 +0100373
Mark Browne24a04c2009-09-22 08:47:11 -0700374 vsel = wm831x_buckv_select_min_voltage(rdev, uV, uV);
375 if (vsel < 0)
376 return vsel;
377
378 return wm831x_set_bits(wm831x, reg, WM831X_DC1_SLP_VSEL_MASK, vsel);
Mark Browne4ee8312009-07-28 15:21:49 +0100379}
380
Mark Brownafb8bb82010-12-10 17:28:08 +0000381static int wm831x_buckv_get_voltage_sel(struct regulator_dev *rdev)
Mark Browne4ee8312009-07-28 15:21:49 +0100382{
383 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
Mark Browne4ee8312009-07-28 15:21:49 +0100384
Mark Browne24a04c2009-09-22 08:47:11 -0700385 if (dcdc->dvs_gpio && dcdc->dvs_gpio_state)
Mark Brownafb8bb82010-12-10 17:28:08 +0000386 return dcdc->dvs_vsel;
Mark Browne24a04c2009-09-22 08:47:11 -0700387 else
Mark Brownafb8bb82010-12-10 17:28:08 +0000388 return dcdc->on_vsel;
Mark Browne4ee8312009-07-28 15:21:49 +0100389}
390
391/* Current limit options */
392static u16 wm831x_dcdc_ilim[] = {
393 125, 250, 375, 500, 625, 750, 875, 1000
394};
395
396static int wm831x_buckv_set_current_limit(struct regulator_dev *rdev,
397 int min_uA, int max_uA)
398{
399 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
400 struct wm831x *wm831x = dcdc->wm831x;
401 u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2;
402 int i;
403
404 for (i = 0; i < ARRAY_SIZE(wm831x_dcdc_ilim); i++) {
405 if (max_uA <= wm831x_dcdc_ilim[i])
406 break;
407 }
408 if (i == ARRAY_SIZE(wm831x_dcdc_ilim))
409 return -EINVAL;
410
411 return wm831x_set_bits(wm831x, reg, WM831X_DC1_HC_THR_MASK, i);
412}
413
414static int wm831x_buckv_get_current_limit(struct regulator_dev *rdev)
415{
416 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
417 struct wm831x *wm831x = dcdc->wm831x;
418 u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2;
419 int val;
420
421 val = wm831x_reg_read(wm831x, reg);
422 if (val < 0)
423 return val;
424
425 return wm831x_dcdc_ilim[val & WM831X_DC1_HC_THR_MASK];
426}
427
428static struct regulator_ops wm831x_buckv_ops = {
429 .set_voltage = wm831x_buckv_set_voltage,
Mark Brownafb8bb82010-12-10 17:28:08 +0000430 .get_voltage_sel = wm831x_buckv_get_voltage_sel,
Mark Browne4ee8312009-07-28 15:21:49 +0100431 .list_voltage = wm831x_buckv_list_voltage,
432 .set_suspend_voltage = wm831x_buckv_set_suspend_voltage,
433 .set_current_limit = wm831x_buckv_set_current_limit,
434 .get_current_limit = wm831x_buckv_get_current_limit,
435
436 .is_enabled = wm831x_dcdc_is_enabled,
437 .enable = wm831x_dcdc_enable,
438 .disable = wm831x_dcdc_disable,
439 .get_status = wm831x_dcdc_get_status,
440 .get_mode = wm831x_dcdc_get_mode,
441 .set_mode = wm831x_dcdc_set_mode,
442 .set_suspend_mode = wm831x_dcdc_set_suspend_mode,
443};
444
Mark Browne24a04c2009-09-22 08:47:11 -0700445/*
446 * Set up DVS control. We just log errors since we can still run
447 * (with reduced performance) if we fail.
448 */
449static __devinit void wm831x_buckv_dvs_init(struct wm831x_dcdc *dcdc,
450 struct wm831x_buckv_pdata *pdata)
451{
452 struct wm831x *wm831x = dcdc->wm831x;
453 int ret;
454 u16 ctrl;
455
456 if (!pdata || !pdata->dvs_gpio)
457 return;
458
459 switch (pdata->dvs_control_src) {
460 case 1:
461 ctrl = 2 << WM831X_DC1_DVS_SRC_SHIFT;
462 break;
463 case 2:
464 ctrl = 3 << WM831X_DC1_DVS_SRC_SHIFT;
465 break;
466 default:
467 dev_err(wm831x->dev, "Invalid DVS control source %d for %s\n",
468 pdata->dvs_control_src, dcdc->name);
469 return;
470 }
471
472 ret = wm831x_set_bits(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL,
473 WM831X_DC1_DVS_SRC_MASK, ctrl);
474 if (ret < 0) {
475 dev_err(wm831x->dev, "Failed to set %s DVS source: %d\n",
476 dcdc->name, ret);
477 return;
478 }
479
480 ret = gpio_request(pdata->dvs_gpio, "DCDC DVS");
481 if (ret < 0) {
482 dev_err(wm831x->dev, "Failed to get %s DVS GPIO: %d\n",
483 dcdc->name, ret);
484 return;
485 }
486
487 /* gpiolib won't let us read the GPIO status so pick the higher
488 * of the two existing voltages so we take it as platform data.
489 */
490 dcdc->dvs_gpio_state = pdata->dvs_init_state;
491
492 ret = gpio_direction_output(pdata->dvs_gpio, dcdc->dvs_gpio_state);
493 if (ret < 0) {
494 dev_err(wm831x->dev, "Failed to enable %s DVS GPIO: %d\n",
495 dcdc->name, ret);
496 gpio_free(pdata->dvs_gpio);
497 return;
498 }
499
500 dcdc->dvs_gpio = pdata->dvs_gpio;
501}
502
Mark Browne4ee8312009-07-28 15:21:49 +0100503static __devinit int wm831x_buckv_probe(struct platform_device *pdev)
504{
505 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
506 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
507 int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
508 struct wm831x_dcdc *dcdc;
509 struct resource *res;
510 int ret, irq;
511
512 dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
513
514 if (pdata == NULL || pdata->dcdc[id] == NULL)
515 return -ENODEV;
516
517 dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
518 if (dcdc == NULL) {
519 dev_err(&pdev->dev, "Unable to allocate private data\n");
520 return -ENOMEM;
521 }
522
523 dcdc->wm831x = wm831x;
524
525 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
526 if (res == NULL) {
527 dev_err(&pdev->dev, "No I/O resource\n");
528 ret = -EINVAL;
529 goto err;
530 }
531 dcdc->base = res->start;
532
533 snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
534 dcdc->desc.name = dcdc->name;
535 dcdc->desc.id = id;
536 dcdc->desc.type = REGULATOR_VOLTAGE;
537 dcdc->desc.n_voltages = WM831X_BUCKV_MAX_SELECTOR + 1;
538 dcdc->desc.ops = &wm831x_buckv_ops;
539 dcdc->desc.owner = THIS_MODULE;
540
Mark Browne24a04c2009-09-22 08:47:11 -0700541 ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG);
542 if (ret < 0) {
543 dev_err(wm831x->dev, "Failed to read ON VSEL: %d\n", ret);
544 goto err;
545 }
546 dcdc->on_vsel = ret & WM831X_DC1_ON_VSEL_MASK;
547
548 ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG);
549 if (ret < 0) {
550 dev_err(wm831x->dev, "Failed to read DVS VSEL: %d\n", ret);
551 goto err;
552 }
553 dcdc->dvs_vsel = ret & WM831X_DC1_DVS_VSEL_MASK;
554
555 if (pdata->dcdc[id])
556 wm831x_buckv_dvs_init(dcdc, pdata->dcdc[id]->driver_data);
557
Mark Browne4ee8312009-07-28 15:21:49 +0100558 dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
559 pdata->dcdc[id], dcdc);
560 if (IS_ERR(dcdc->regulator)) {
561 ret = PTR_ERR(dcdc->regulator);
562 dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
563 id + 1, ret);
564 goto err;
565 }
566
567 irq = platform_get_irq_byname(pdev, "UV");
Mark Browndfda9c22011-03-01 16:50:43 +0000568 ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq,
569 IRQF_TRIGGER_RISING, dcdc->name, dcdc);
Mark Browne4ee8312009-07-28 15:21:49 +0100570 if (ret != 0) {
571 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
572 irq, ret);
573 goto err_regulator;
574 }
575
576 irq = platform_get_irq_byname(pdev, "HC");
Mark Browndfda9c22011-03-01 16:50:43 +0000577 ret = request_threaded_irq(irq, NULL, wm831x_dcdc_oc_irq,
578 IRQF_TRIGGER_RISING, dcdc->name, dcdc);
Mark Browne4ee8312009-07-28 15:21:49 +0100579 if (ret != 0) {
580 dev_err(&pdev->dev, "Failed to request HC IRQ %d: %d\n",
581 irq, ret);
582 goto err_uv;
583 }
584
585 platform_set_drvdata(pdev, dcdc);
586
587 return 0;
588
589err_uv:
Mark Browndfda9c22011-03-01 16:50:43 +0000590 free_irq(platform_get_irq_byname(pdev, "UV"), dcdc);
Mark Browne4ee8312009-07-28 15:21:49 +0100591err_regulator:
592 regulator_unregister(dcdc->regulator);
593err:
Mark Browne24a04c2009-09-22 08:47:11 -0700594 if (dcdc->dvs_gpio)
595 gpio_free(dcdc->dvs_gpio);
Mark Browne4ee8312009-07-28 15:21:49 +0100596 kfree(dcdc);
597 return ret;
598}
599
600static __devexit int wm831x_buckv_remove(struct platform_device *pdev)
601{
602 struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
Mark Browne4ee8312009-07-28 15:21:49 +0100603
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800604 platform_set_drvdata(pdev, NULL);
605
Mark Brown69952362011-03-18 12:43:35 +0000606 free_irq(platform_get_irq_byname(pdev, "HC"), dcdc);
607 free_irq(platform_get_irq_byname(pdev, "UV"), dcdc);
Mark Browne4ee8312009-07-28 15:21:49 +0100608 regulator_unregister(dcdc->regulator);
Mark Browne24a04c2009-09-22 08:47:11 -0700609 if (dcdc->dvs_gpio)
610 gpio_free(dcdc->dvs_gpio);
Mark Browne4ee8312009-07-28 15:21:49 +0100611 kfree(dcdc);
612
613 return 0;
614}
615
616static struct platform_driver wm831x_buckv_driver = {
617 .probe = wm831x_buckv_probe,
618 .remove = __devexit_p(wm831x_buckv_remove),
619 .driver = {
620 .name = "wm831x-buckv",
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800621 .owner = THIS_MODULE,
Mark Browne4ee8312009-07-28 15:21:49 +0100622 },
623};
624
625/*
626 * BUCKP specifics
627 */
628
629static int wm831x_buckp_list_voltage(struct regulator_dev *rdev,
630 unsigned selector)
631{
632 if (selector <= WM831X_BUCKP_MAX_SELECTOR)
633 return 850000 + (selector * 25000);
634 else
635 return -EINVAL;
636}
637
638static int wm831x_buckp_set_voltage_int(struct regulator_dev *rdev, int reg,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000639 int min_uV, int max_uV, int *selector)
Mark Browne4ee8312009-07-28 15:21:49 +0100640{
641 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
642 struct wm831x *wm831x = dcdc->wm831x;
643 u16 vsel;
644
645 if (min_uV <= 34000000)
646 vsel = (min_uV - 850000) / 25000;
647 else
648 return -EINVAL;
649
650 if (wm831x_buckp_list_voltage(rdev, vsel) > max_uV)
651 return -EINVAL;
652
Mark Brown3a93f2a2010-11-10 14:38:29 +0000653 *selector = vsel;
654
Mark Browne4ee8312009-07-28 15:21:49 +0100655 return wm831x_set_bits(wm831x, reg, WM831X_DC3_ON_VSEL_MASK, vsel);
656}
657
658static int wm831x_buckp_set_voltage(struct regulator_dev *rdev,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000659 int min_uV, int max_uV,
660 unsigned *selector)
Mark Browne4ee8312009-07-28 15:21:49 +0100661{
662 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
663 u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
664
Mark Brown3a93f2a2010-11-10 14:38:29 +0000665 return wm831x_buckp_set_voltage_int(rdev, reg, min_uV, max_uV,
666 selector);
Mark Browne4ee8312009-07-28 15:21:49 +0100667}
668
669static int wm831x_buckp_set_suspend_voltage(struct regulator_dev *rdev,
670 int uV)
671{
672 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
673 u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
Mark Brown3a93f2a2010-11-10 14:38:29 +0000674 unsigned selector;
Mark Browne4ee8312009-07-28 15:21:49 +0100675
Mark Brown3a93f2a2010-11-10 14:38:29 +0000676 return wm831x_buckp_set_voltage_int(rdev, reg, uV, uV, &selector);
Mark Browne4ee8312009-07-28 15:21:49 +0100677}
678
Mark Brownafb8bb82010-12-10 17:28:08 +0000679static int wm831x_buckp_get_voltage_sel(struct regulator_dev *rdev)
Mark Browne4ee8312009-07-28 15:21:49 +0100680{
681 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
682 struct wm831x *wm831x = dcdc->wm831x;
683 u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
684 int val;
685
686 val = wm831x_reg_read(wm831x, reg);
687 if (val < 0)
688 return val;
689
Mark Brownafb8bb82010-12-10 17:28:08 +0000690 return val & WM831X_DC3_ON_VSEL_MASK;
Mark Browne4ee8312009-07-28 15:21:49 +0100691}
692
693static struct regulator_ops wm831x_buckp_ops = {
694 .set_voltage = wm831x_buckp_set_voltage,
Mark Brownafb8bb82010-12-10 17:28:08 +0000695 .get_voltage_sel = wm831x_buckp_get_voltage_sel,
Mark Browne4ee8312009-07-28 15:21:49 +0100696 .list_voltage = wm831x_buckp_list_voltage,
697 .set_suspend_voltage = wm831x_buckp_set_suspend_voltage,
698
699 .is_enabled = wm831x_dcdc_is_enabled,
700 .enable = wm831x_dcdc_enable,
701 .disable = wm831x_dcdc_disable,
702 .get_status = wm831x_dcdc_get_status,
703 .get_mode = wm831x_dcdc_get_mode,
704 .set_mode = wm831x_dcdc_set_mode,
705 .set_suspend_mode = wm831x_dcdc_set_suspend_mode,
706};
707
708static __devinit int wm831x_buckp_probe(struct platform_device *pdev)
709{
710 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
711 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
712 int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
713 struct wm831x_dcdc *dcdc;
714 struct resource *res;
715 int ret, irq;
716
717 dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
718
719 if (pdata == NULL || pdata->dcdc[id] == NULL)
720 return -ENODEV;
721
722 dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
723 if (dcdc == NULL) {
724 dev_err(&pdev->dev, "Unable to allocate private data\n");
725 return -ENOMEM;
726 }
727
728 dcdc->wm831x = wm831x;
729
730 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
731 if (res == NULL) {
732 dev_err(&pdev->dev, "No I/O resource\n");
733 ret = -EINVAL;
734 goto err;
735 }
736 dcdc->base = res->start;
737
738 snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
739 dcdc->desc.name = dcdc->name;
740 dcdc->desc.id = id;
741 dcdc->desc.type = REGULATOR_VOLTAGE;
742 dcdc->desc.n_voltages = WM831X_BUCKP_MAX_SELECTOR + 1;
743 dcdc->desc.ops = &wm831x_buckp_ops;
744 dcdc->desc.owner = THIS_MODULE;
745
746 dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
747 pdata->dcdc[id], dcdc);
748 if (IS_ERR(dcdc->regulator)) {
749 ret = PTR_ERR(dcdc->regulator);
750 dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
751 id + 1, ret);
752 goto err;
753 }
754
755 irq = platform_get_irq_byname(pdev, "UV");
Mark Browndfda9c22011-03-01 16:50:43 +0000756 ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq,
757 IRQF_TRIGGER_RISING, dcdc->name, dcdc);
Mark Browne4ee8312009-07-28 15:21:49 +0100758 if (ret != 0) {
759 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
760 irq, ret);
761 goto err_regulator;
762 }
763
764 platform_set_drvdata(pdev, dcdc);
765
766 return 0;
767
768err_regulator:
769 regulator_unregister(dcdc->regulator);
770err:
771 kfree(dcdc);
772 return ret;
773}
774
775static __devexit int wm831x_buckp_remove(struct platform_device *pdev)
776{
777 struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
Mark Browne4ee8312009-07-28 15:21:49 +0100778
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800779 platform_set_drvdata(pdev, NULL);
780
Mark Brown69952362011-03-18 12:43:35 +0000781 free_irq(platform_get_irq_byname(pdev, "UV"), dcdc);
Mark Browne4ee8312009-07-28 15:21:49 +0100782 regulator_unregister(dcdc->regulator);
783 kfree(dcdc);
784
785 return 0;
786}
787
788static struct platform_driver wm831x_buckp_driver = {
789 .probe = wm831x_buckp_probe,
790 .remove = __devexit_p(wm831x_buckp_remove),
791 .driver = {
792 .name = "wm831x-buckp",
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800793 .owner = THIS_MODULE,
Mark Browne4ee8312009-07-28 15:21:49 +0100794 },
795};
796
Mark Brown8267a9b2009-07-28 15:22:23 +0100797/*
Mark Brown13048502009-07-28 15:23:16 +0100798 * DCDC boost convertors
799 */
800
801static int wm831x_boostp_get_status(struct regulator_dev *rdev)
802{
803 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
804 struct wm831x *wm831x = dcdc->wm831x;
805 int ret;
806
807 /* First, check for errors */
808 ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
809 if (ret < 0)
810 return ret;
811
812 if (ret & (1 << rdev_get_id(rdev))) {
813 dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
814 rdev_get_id(rdev) + 1);
815 return REGULATOR_STATUS_ERROR;
816 }
817
818 /* Is the regulator on? */
819 ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
820 if (ret < 0)
821 return ret;
822 if (ret & (1 << rdev_get_id(rdev)))
823 return REGULATOR_STATUS_ON;
824 else
825 return REGULATOR_STATUS_OFF;
826}
827
828static struct regulator_ops wm831x_boostp_ops = {
829 .get_status = wm831x_boostp_get_status,
830
831 .is_enabled = wm831x_dcdc_is_enabled,
832 .enable = wm831x_dcdc_enable,
833 .disable = wm831x_dcdc_disable,
834};
835
836static __devinit int wm831x_boostp_probe(struct platform_device *pdev)
837{
838 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
839 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
840 int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
841 struct wm831x_dcdc *dcdc;
842 struct resource *res;
843 int ret, irq;
844
845 dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
846
847 if (pdata == NULL || pdata->dcdc[id] == NULL)
848 return -ENODEV;
849
850 dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
851 if (dcdc == NULL) {
852 dev_err(&pdev->dev, "Unable to allocate private data\n");
853 return -ENOMEM;
854 }
855
856 dcdc->wm831x = wm831x;
857
858 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
859 if (res == NULL) {
860 dev_err(&pdev->dev, "No I/O resource\n");
861 ret = -EINVAL;
862 goto err;
863 }
864 dcdc->base = res->start;
865
866 snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
867 dcdc->desc.name = dcdc->name;
868 dcdc->desc.id = id;
869 dcdc->desc.type = REGULATOR_VOLTAGE;
870 dcdc->desc.ops = &wm831x_boostp_ops;
871 dcdc->desc.owner = THIS_MODULE;
872
873 dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
874 pdata->dcdc[id], dcdc);
875 if (IS_ERR(dcdc->regulator)) {
876 ret = PTR_ERR(dcdc->regulator);
877 dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
878 id + 1, ret);
879 goto err;
880 }
881
882 irq = platform_get_irq_byname(pdev, "UV");
Mark Browndfda9c22011-03-01 16:50:43 +0000883 ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq,
884 IRQF_TRIGGER_RISING, dcdc->name,
885 dcdc);
Mark Brown13048502009-07-28 15:23:16 +0100886 if (ret != 0) {
887 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
888 irq, ret);
889 goto err_regulator;
890 }
891
892 platform_set_drvdata(pdev, dcdc);
893
894 return 0;
895
896err_regulator:
897 regulator_unregister(dcdc->regulator);
898err:
899 kfree(dcdc);
900 return ret;
901}
902
903static __devexit int wm831x_boostp_remove(struct platform_device *pdev)
904{
905 struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
Mark Brown13048502009-07-28 15:23:16 +0100906
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800907 platform_set_drvdata(pdev, NULL);
908
Mark Browndfda9c22011-03-01 16:50:43 +0000909 free_irq(platform_get_irq_byname(pdev, "UV"), dcdc);
Mark Brown13048502009-07-28 15:23:16 +0100910 regulator_unregister(dcdc->regulator);
911 kfree(dcdc);
912
913 return 0;
914}
915
916static struct platform_driver wm831x_boostp_driver = {
917 .probe = wm831x_boostp_probe,
918 .remove = __devexit_p(wm831x_boostp_remove),
919 .driver = {
920 .name = "wm831x-boostp",
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800921 .owner = THIS_MODULE,
Mark Brown13048502009-07-28 15:23:16 +0100922 },
923};
924
925/*
Mark Brown8267a9b2009-07-28 15:22:23 +0100926 * External Power Enable
927 *
928 * These aren't actually DCDCs but look like them in hardware so share
929 * code.
930 */
931
932#define WM831X_EPE_BASE 6
933
934static struct regulator_ops wm831x_epe_ops = {
935 .is_enabled = wm831x_dcdc_is_enabled,
936 .enable = wm831x_dcdc_enable,
937 .disable = wm831x_dcdc_disable,
938 .get_status = wm831x_dcdc_get_status,
939};
940
941static __devinit int wm831x_epe_probe(struct platform_device *pdev)
942{
943 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
944 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
945 int id = pdev->id % ARRAY_SIZE(pdata->epe);
946 struct wm831x_dcdc *dcdc;
947 int ret;
948
949 dev_dbg(&pdev->dev, "Probing EPE%d\n", id + 1);
950
951 if (pdata == NULL || pdata->epe[id] == NULL)
952 return -ENODEV;
953
954 dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
955 if (dcdc == NULL) {
956 dev_err(&pdev->dev, "Unable to allocate private data\n");
957 return -ENOMEM;
958 }
959
960 dcdc->wm831x = wm831x;
961
962 /* For current parts this is correct; probably need to revisit
963 * in future.
964 */
965 snprintf(dcdc->name, sizeof(dcdc->name), "EPE%d", id + 1);
966 dcdc->desc.name = dcdc->name;
967 dcdc->desc.id = id + WM831X_EPE_BASE; /* Offset in DCDC registers */
968 dcdc->desc.ops = &wm831x_epe_ops;
969 dcdc->desc.type = REGULATOR_VOLTAGE;
970 dcdc->desc.owner = THIS_MODULE;
971
972 dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
973 pdata->epe[id], dcdc);
974 if (IS_ERR(dcdc->regulator)) {
975 ret = PTR_ERR(dcdc->regulator);
976 dev_err(wm831x->dev, "Failed to register EPE%d: %d\n",
977 id + 1, ret);
978 goto err;
979 }
980
981 platform_set_drvdata(pdev, dcdc);
982
983 return 0;
984
985err:
986 kfree(dcdc);
987 return ret;
988}
989
990static __devexit int wm831x_epe_remove(struct platform_device *pdev)
991{
992 struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
993
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800994 platform_set_drvdata(pdev, NULL);
995
Mark Brown8267a9b2009-07-28 15:22:23 +0100996 regulator_unregister(dcdc->regulator);
997 kfree(dcdc);
998
999 return 0;
1000}
1001
1002static struct platform_driver wm831x_epe_driver = {
1003 .probe = wm831x_epe_probe,
1004 .remove = __devexit_p(wm831x_epe_remove),
1005 .driver = {
1006 .name = "wm831x-epe",
Dmitry Torokhoveb66d562010-02-23 23:38:39 -08001007 .owner = THIS_MODULE,
Mark Brown8267a9b2009-07-28 15:22:23 +01001008 },
1009};
1010
Mark Browne4ee8312009-07-28 15:21:49 +01001011static int __init wm831x_dcdc_init(void)
1012{
1013 int ret;
1014 ret = platform_driver_register(&wm831x_buckv_driver);
1015 if (ret != 0)
1016 pr_err("Failed to register WM831x BUCKV driver: %d\n", ret);
1017
1018 ret = platform_driver_register(&wm831x_buckp_driver);
1019 if (ret != 0)
1020 pr_err("Failed to register WM831x BUCKP driver: %d\n", ret);
1021
Mark Brown13048502009-07-28 15:23:16 +01001022 ret = platform_driver_register(&wm831x_boostp_driver);
1023 if (ret != 0)
1024 pr_err("Failed to register WM831x BOOST driver: %d\n", ret);
1025
Mark Brown8267a9b2009-07-28 15:22:23 +01001026 ret = platform_driver_register(&wm831x_epe_driver);
1027 if (ret != 0)
1028 pr_err("Failed to register WM831x EPE driver: %d\n", ret);
1029
Mark Browne4ee8312009-07-28 15:21:49 +01001030 return 0;
1031}
1032subsys_initcall(wm831x_dcdc_init);
1033
1034static void __exit wm831x_dcdc_exit(void)
1035{
Mark Brown8267a9b2009-07-28 15:22:23 +01001036 platform_driver_unregister(&wm831x_epe_driver);
Mark Brown13048502009-07-28 15:23:16 +01001037 platform_driver_unregister(&wm831x_boostp_driver);
Mark Browne4ee8312009-07-28 15:21:49 +01001038 platform_driver_unregister(&wm831x_buckp_driver);
1039 platform_driver_unregister(&wm831x_buckv_driver);
1040}
1041module_exit(wm831x_dcdc_exit);
1042
1043/* Module information */
1044MODULE_AUTHOR("Mark Brown");
1045MODULE_DESCRIPTION("WM831x DC-DC convertor driver");
1046MODULE_LICENSE("GPL");
1047MODULE_ALIAS("platform:wm831x-buckv");
1048MODULE_ALIAS("platform:wm831x-buckp");