ASoC: Fix typo in late revision WM8994 DAC2R name
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / regulator / wm831x-dcdc.c
blob8b0d2c4bde919fb9815903288acabb1afa9b63eb
1 /*
2 * wm831x-dcdc.c -- DC-DC buck convertor driver for the WM831x series
4 * Copyright 2009 Wolfson Microelectronics PLC.
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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.
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>
22 #include <linux/regulator/machine.h>
23 #include <linux/gpio.h>
24 #include <linux/slab.h>
26 #include <linux/mfd/wm831x/core.h>
27 #include <linux/mfd/wm831x/regulator.h>
28 #include <linux/mfd/wm831x/pdata.h>
30 #define WM831X_BUCKV_MAX_SELECTOR 0x68
31 #define WM831X_BUCKP_MAX_SELECTOR 0x66
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
38 #define WM831X_DCDC_MAX_NAME 6
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
45 #define WM831X_DCDC_DVS_CONTROL 4
48 * Shared
51 struct 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;
57 int dvs_gpio;
58 int dvs_gpio_state;
59 int on_vsel;
60 int dvs_vsel;
63 static int wm831x_dcdc_is_enabled(struct regulator_dev *rdev)
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;
70 reg = wm831x_reg_read(wm831x, WM831X_DCDC_ENABLE);
71 if (reg < 0)
72 return reg;
74 if (reg & mask)
75 return 1;
76 else
77 return 0;
80 static int wm831x_dcdc_enable(struct regulator_dev *rdev)
82 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
83 struct wm831x *wm831x = dcdc->wm831x;
84 int mask = 1 << rdev_get_id(rdev);
86 return wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE, mask, mask);
89 static int wm831x_dcdc_disable(struct regulator_dev *rdev)
91 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
92 struct wm831x *wm831x = dcdc->wm831x;
93 int mask = 1 << rdev_get_id(rdev);
95 return wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE, mask, 0);
98 static unsigned int wm831x_dcdc_get_mode(struct regulator_dev *rdev)
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;
106 val = wm831x_reg_read(wm831x, reg);
107 if (val < 0)
108 return val;
110 val = (val & WM831X_DC1_ON_MODE_MASK) >> WM831X_DC1_ON_MODE_SHIFT;
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();
126 static int wm831x_dcdc_set_mode_int(struct wm831x *wm831x, int reg,
127 unsigned int mode)
129 int val;
131 switch (mode) {
132 case REGULATOR_MODE_FAST:
133 val = WM831X_DCDC_MODE_FAST;
134 break;
135 case REGULATOR_MODE_NORMAL:
136 val = WM831X_DCDC_MODE_NORMAL;
137 break;
138 case REGULATOR_MODE_STANDBY:
139 val = WM831X_DCDC_MODE_STANDBY;
140 break;
141 case REGULATOR_MODE_IDLE:
142 val = WM831X_DCDC_MODE_IDLE;
143 break;
144 default:
145 return -EINVAL;
148 return wm831x_set_bits(wm831x, reg, WM831X_DC1_ON_MODE_MASK,
149 val << WM831X_DC1_ON_MODE_SHIFT);
152 static int wm831x_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
154 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
155 struct wm831x *wm831x = dcdc->wm831x;
156 u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
158 return wm831x_dcdc_set_mode_int(wm831x, reg, mode);
161 static int wm831x_dcdc_set_suspend_mode(struct regulator_dev *rdev,
162 unsigned int mode)
164 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
165 struct wm831x *wm831x = dcdc->wm831x;
166 u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
168 return wm831x_dcdc_set_mode_int(wm831x, reg, mode);
171 static int wm831x_dcdc_get_status(struct regulator_dev *rdev)
173 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
174 struct wm831x *wm831x = dcdc->wm831x;
175 int ret;
177 /* First, check for errors */
178 ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
179 if (ret < 0)
180 return ret;
182 if (ret & (1 << rdev_get_id(rdev))) {
183 dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
184 rdev_get_id(rdev) + 1);
185 return REGULATOR_STATUS_ERROR;
188 /* DCDC1 and DCDC2 can additionally detect high voltage/current */
189 if (rdev_get_id(rdev) < 2) {
190 if (ret & (WM831X_DC1_OV_STS << rdev_get_id(rdev))) {
191 dev_dbg(wm831x->dev, "DCDC%d over voltage\n",
192 rdev_get_id(rdev) + 1);
193 return REGULATOR_STATUS_ERROR;
196 if (ret & (WM831X_DC1_HC_STS << rdev_get_id(rdev))) {
197 dev_dbg(wm831x->dev, "DCDC%d over current\n",
198 rdev_get_id(rdev) + 1);
199 return REGULATOR_STATUS_ERROR;
203 /* Is the regulator on? */
204 ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
205 if (ret < 0)
206 return ret;
207 if (!(ret & (1 << rdev_get_id(rdev))))
208 return REGULATOR_STATUS_OFF;
210 /* TODO: When we handle hardware control modes so we can report the
211 * current mode. */
212 return REGULATOR_STATUS_ON;
215 static irqreturn_t wm831x_dcdc_uv_irq(int irq, void *data)
217 struct wm831x_dcdc *dcdc = data;
219 regulator_notifier_call_chain(dcdc->regulator,
220 REGULATOR_EVENT_UNDER_VOLTAGE,
221 NULL);
223 return IRQ_HANDLED;
226 static irqreturn_t wm831x_dcdc_oc_irq(int irq, void *data)
228 struct wm831x_dcdc *dcdc = data;
230 regulator_notifier_call_chain(dcdc->regulator,
231 REGULATOR_EVENT_OVER_CURRENT,
232 NULL);
234 return IRQ_HANDLED;
238 * BUCKV specifics
241 static int wm831x_buckv_list_voltage(struct regulator_dev *rdev,
242 unsigned selector)
244 if (selector <= 0x8)
245 return 600000;
246 if (selector <= WM831X_BUCKV_MAX_SELECTOR)
247 return 600000 + ((selector - 0x8) * 12500);
248 return -EINVAL;
251 static int wm831x_buckv_select_min_voltage(struct regulator_dev *rdev,
252 int min_uV, int max_uV)
254 u16 vsel;
256 if (min_uV < 600000)
257 vsel = 0;
258 else if (min_uV <= 1800000)
259 vsel = ((min_uV - 600000) / 12500) + 8;
260 else
261 return -EINVAL;
263 if (wm831x_buckv_list_voltage(rdev, vsel) > max_uV)
264 return -EINVAL;
266 return vsel;
269 static int wm831x_buckv_select_max_voltage(struct regulator_dev *rdev,
270 int min_uV, int max_uV)
272 u16 vsel;
274 if (max_uV < 600000 || max_uV > 1800000)
275 return -EINVAL;
277 vsel = ((max_uV - 600000) / 12500) + 8;
279 if (wm831x_buckv_list_voltage(rdev, vsel) < min_uV ||
280 wm831x_buckv_list_voltage(rdev, vsel) < max_uV)
281 return -EINVAL;
283 return vsel;
286 static int wm831x_buckv_set_dvs(struct regulator_dev *rdev, int state)
288 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
290 if (state == dcdc->dvs_gpio_state)
291 return 0;
293 dcdc->dvs_gpio_state = state;
294 gpio_set_value(dcdc->dvs_gpio, state);
296 /* Should wait for DVS state change to be asserted if we have
297 * a GPIO for it, for now assume the device is configured
298 * for the fastest possible transition.
301 return 0;
304 static int wm831x_buckv_set_voltage(struct regulator_dev *rdev,
305 int min_uV, int max_uV, unsigned *selector)
307 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
308 struct wm831x *wm831x = dcdc->wm831x;
309 int on_reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
310 int dvs_reg = dcdc->base + WM831X_DCDC_DVS_CONTROL;
311 int vsel, ret;
313 vsel = wm831x_buckv_select_min_voltage(rdev, min_uV, max_uV);
314 if (vsel < 0)
315 return vsel;
317 *selector = vsel;
319 /* If this value is already set then do a GPIO update if we can */
320 if (dcdc->dvs_gpio && dcdc->on_vsel == vsel)
321 return wm831x_buckv_set_dvs(rdev, 0);
323 if (dcdc->dvs_gpio && dcdc->dvs_vsel == vsel)
324 return wm831x_buckv_set_dvs(rdev, 1);
326 /* Always set the ON status to the minimum voltage */
327 ret = wm831x_set_bits(wm831x, on_reg, WM831X_DC1_ON_VSEL_MASK, vsel);
328 if (ret < 0)
329 return ret;
330 dcdc->on_vsel = vsel;
332 if (!dcdc->dvs_gpio)
333 return ret;
335 /* Kick the voltage transition now */
336 ret = wm831x_buckv_set_dvs(rdev, 0);
337 if (ret < 0)
338 return ret;
340 /* Set the high voltage as the DVS voltage. This is optimised
341 * for CPUfreq usage, most processors will keep the maximum
342 * voltage constant and lower the minimum with the frequency. */
343 vsel = wm831x_buckv_select_max_voltage(rdev, min_uV, max_uV);
344 if (vsel < 0) {
345 /* This should never happen - at worst the same vsel
346 * should be chosen */
347 WARN_ON(vsel < 0);
348 return 0;
351 /* Don't bother if it's the same VSEL we're already using */
352 if (vsel == dcdc->on_vsel)
353 return 0;
355 ret = wm831x_set_bits(wm831x, dvs_reg, WM831X_DC1_DVS_VSEL_MASK, vsel);
356 if (ret == 0)
357 dcdc->dvs_vsel = vsel;
358 else
359 dev_warn(wm831x->dev, "Failed to set DCDC DVS VSEL: %d\n",
360 ret);
362 return 0;
365 static int wm831x_buckv_set_suspend_voltage(struct regulator_dev *rdev,
366 int uV)
368 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
369 struct wm831x *wm831x = dcdc->wm831x;
370 u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
371 int vsel;
373 vsel = wm831x_buckv_select_min_voltage(rdev, uV, uV);
374 if (vsel < 0)
375 return vsel;
377 return wm831x_set_bits(wm831x, reg, WM831X_DC1_SLP_VSEL_MASK, vsel);
380 static int wm831x_buckv_get_voltage_sel(struct regulator_dev *rdev)
382 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
384 if (dcdc->dvs_gpio && dcdc->dvs_gpio_state)
385 return dcdc->dvs_vsel;
386 else
387 return dcdc->on_vsel;
390 /* Current limit options */
391 static u16 wm831x_dcdc_ilim[] = {
392 125, 250, 375, 500, 625, 750, 875, 1000
395 static int wm831x_buckv_set_current_limit(struct regulator_dev *rdev,
396 int min_uA, int max_uA)
398 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
399 struct wm831x *wm831x = dcdc->wm831x;
400 u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2;
401 int i;
403 for (i = 0; i < ARRAY_SIZE(wm831x_dcdc_ilim); i++) {
404 if (max_uA <= wm831x_dcdc_ilim[i])
405 break;
407 if (i == ARRAY_SIZE(wm831x_dcdc_ilim))
408 return -EINVAL;
410 return wm831x_set_bits(wm831x, reg, WM831X_DC1_HC_THR_MASK, i);
413 static int wm831x_buckv_get_current_limit(struct regulator_dev *rdev)
415 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
416 struct wm831x *wm831x = dcdc->wm831x;
417 u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2;
418 int val;
420 val = wm831x_reg_read(wm831x, reg);
421 if (val < 0)
422 return val;
424 return wm831x_dcdc_ilim[val & WM831X_DC1_HC_THR_MASK];
427 static struct regulator_ops wm831x_buckv_ops = {
428 .set_voltage = wm831x_buckv_set_voltage,
429 .get_voltage_sel = wm831x_buckv_get_voltage_sel,
430 .list_voltage = wm831x_buckv_list_voltage,
431 .set_suspend_voltage = wm831x_buckv_set_suspend_voltage,
432 .set_current_limit = wm831x_buckv_set_current_limit,
433 .get_current_limit = wm831x_buckv_get_current_limit,
435 .is_enabled = wm831x_dcdc_is_enabled,
436 .enable = wm831x_dcdc_enable,
437 .disable = wm831x_dcdc_disable,
438 .get_status = wm831x_dcdc_get_status,
439 .get_mode = wm831x_dcdc_get_mode,
440 .set_mode = wm831x_dcdc_set_mode,
441 .set_suspend_mode = wm831x_dcdc_set_suspend_mode,
445 * Set up DVS control. We just log errors since we can still run
446 * (with reduced performance) if we fail.
448 static __devinit void wm831x_buckv_dvs_init(struct wm831x_dcdc *dcdc,
449 struct wm831x_buckv_pdata *pdata)
451 struct wm831x *wm831x = dcdc->wm831x;
452 int ret;
453 u16 ctrl;
455 if (!pdata || !pdata->dvs_gpio)
456 return;
458 switch (pdata->dvs_control_src) {
459 case 1:
460 ctrl = 2 << WM831X_DC1_DVS_SRC_SHIFT;
461 break;
462 case 2:
463 ctrl = 3 << WM831X_DC1_DVS_SRC_SHIFT;
464 break;
465 default:
466 dev_err(wm831x->dev, "Invalid DVS control source %d for %s\n",
467 pdata->dvs_control_src, dcdc->name);
468 return;
471 ret = wm831x_set_bits(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL,
472 WM831X_DC1_DVS_SRC_MASK, ctrl);
473 if (ret < 0) {
474 dev_err(wm831x->dev, "Failed to set %s DVS source: %d\n",
475 dcdc->name, ret);
476 return;
479 ret = gpio_request(pdata->dvs_gpio, "DCDC DVS");
480 if (ret < 0) {
481 dev_err(wm831x->dev, "Failed to get %s DVS GPIO: %d\n",
482 dcdc->name, ret);
483 return;
486 /* gpiolib won't let us read the GPIO status so pick the higher
487 * of the two existing voltages so we take it as platform data.
489 dcdc->dvs_gpio_state = pdata->dvs_init_state;
491 ret = gpio_direction_output(pdata->dvs_gpio, dcdc->dvs_gpio_state);
492 if (ret < 0) {
493 dev_err(wm831x->dev, "Failed to enable %s DVS GPIO: %d\n",
494 dcdc->name, ret);
495 gpio_free(pdata->dvs_gpio);
496 return;
499 dcdc->dvs_gpio = pdata->dvs_gpio;
502 static __devinit int wm831x_buckv_probe(struct platform_device *pdev)
504 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
505 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
506 int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
507 struct wm831x_dcdc *dcdc;
508 struct resource *res;
509 int ret, irq;
511 dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
513 if (pdata == NULL || pdata->dcdc[id] == NULL)
514 return -ENODEV;
516 dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
517 if (dcdc == NULL) {
518 dev_err(&pdev->dev, "Unable to allocate private data\n");
519 return -ENOMEM;
522 dcdc->wm831x = wm831x;
524 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
525 if (res == NULL) {
526 dev_err(&pdev->dev, "No I/O resource\n");
527 ret = -EINVAL;
528 goto err;
530 dcdc->base = res->start;
532 snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
533 dcdc->desc.name = dcdc->name;
534 dcdc->desc.id = id;
535 dcdc->desc.type = REGULATOR_VOLTAGE;
536 dcdc->desc.n_voltages = WM831X_BUCKV_MAX_SELECTOR + 1;
537 dcdc->desc.ops = &wm831x_buckv_ops;
538 dcdc->desc.owner = THIS_MODULE;
540 ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG);
541 if (ret < 0) {
542 dev_err(wm831x->dev, "Failed to read ON VSEL: %d\n", ret);
543 goto err;
545 dcdc->on_vsel = ret & WM831X_DC1_ON_VSEL_MASK;
547 ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG);
548 if (ret < 0) {
549 dev_err(wm831x->dev, "Failed to read DVS VSEL: %d\n", ret);
550 goto err;
552 dcdc->dvs_vsel = ret & WM831X_DC1_DVS_VSEL_MASK;
554 if (pdata->dcdc[id])
555 wm831x_buckv_dvs_init(dcdc, pdata->dcdc[id]->driver_data);
557 dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
558 pdata->dcdc[id], dcdc);
559 if (IS_ERR(dcdc->regulator)) {
560 ret = PTR_ERR(dcdc->regulator);
561 dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
562 id + 1, ret);
563 goto err;
566 irq = platform_get_irq_byname(pdev, "UV");
567 ret = wm831x_request_irq(wm831x, irq, wm831x_dcdc_uv_irq,
568 IRQF_TRIGGER_RISING, dcdc->name,
569 dcdc);
570 if (ret != 0) {
571 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
572 irq, ret);
573 goto err_regulator;
576 irq = platform_get_irq_byname(pdev, "HC");
577 ret = wm831x_request_irq(wm831x, irq, wm831x_dcdc_oc_irq,
578 IRQF_TRIGGER_RISING, dcdc->name,
579 dcdc);
580 if (ret != 0) {
581 dev_err(&pdev->dev, "Failed to request HC IRQ %d: %d\n",
582 irq, ret);
583 goto err_uv;
586 platform_set_drvdata(pdev, dcdc);
588 return 0;
590 err_uv:
591 wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), dcdc);
592 err_regulator:
593 regulator_unregister(dcdc->regulator);
594 err:
595 if (dcdc->dvs_gpio)
596 gpio_free(dcdc->dvs_gpio);
597 kfree(dcdc);
598 return ret;
601 static __devexit int wm831x_buckv_remove(struct platform_device *pdev)
603 struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
604 struct wm831x *wm831x = dcdc->wm831x;
606 platform_set_drvdata(pdev, NULL);
608 wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "HC"), dcdc);
609 wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), dcdc);
610 regulator_unregister(dcdc->regulator);
611 if (dcdc->dvs_gpio)
612 gpio_free(dcdc->dvs_gpio);
613 kfree(dcdc);
615 return 0;
618 static struct platform_driver wm831x_buckv_driver = {
619 .probe = wm831x_buckv_probe,
620 .remove = __devexit_p(wm831x_buckv_remove),
621 .driver = {
622 .name = "wm831x-buckv",
623 .owner = THIS_MODULE,
628 * BUCKP specifics
631 static int wm831x_buckp_list_voltage(struct regulator_dev *rdev,
632 unsigned selector)
634 if (selector <= WM831X_BUCKP_MAX_SELECTOR)
635 return 850000 + (selector * 25000);
636 else
637 return -EINVAL;
640 static int wm831x_buckp_set_voltage_int(struct regulator_dev *rdev, int reg,
641 int min_uV, int max_uV, int *selector)
643 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
644 struct wm831x *wm831x = dcdc->wm831x;
645 u16 vsel;
647 if (min_uV <= 34000000)
648 vsel = (min_uV - 850000) / 25000;
649 else
650 return -EINVAL;
652 if (wm831x_buckp_list_voltage(rdev, vsel) > max_uV)
653 return -EINVAL;
655 *selector = vsel;
657 return wm831x_set_bits(wm831x, reg, WM831X_DC3_ON_VSEL_MASK, vsel);
660 static int wm831x_buckp_set_voltage(struct regulator_dev *rdev,
661 int min_uV, int max_uV,
662 unsigned *selector)
664 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
665 u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
667 return wm831x_buckp_set_voltage_int(rdev, reg, min_uV, max_uV,
668 selector);
671 static int wm831x_buckp_set_suspend_voltage(struct regulator_dev *rdev,
672 int uV)
674 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
675 u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
676 unsigned selector;
678 return wm831x_buckp_set_voltage_int(rdev, reg, uV, uV, &selector);
681 static int wm831x_buckp_get_voltage_sel(struct regulator_dev *rdev)
683 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
684 struct wm831x *wm831x = dcdc->wm831x;
685 u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
686 int val;
688 val = wm831x_reg_read(wm831x, reg);
689 if (val < 0)
690 return val;
692 return val & WM831X_DC3_ON_VSEL_MASK;
695 static struct regulator_ops wm831x_buckp_ops = {
696 .set_voltage = wm831x_buckp_set_voltage,
697 .get_voltage_sel = wm831x_buckp_get_voltage_sel,
698 .list_voltage = wm831x_buckp_list_voltage,
699 .set_suspend_voltage = wm831x_buckp_set_suspend_voltage,
701 .is_enabled = wm831x_dcdc_is_enabled,
702 .enable = wm831x_dcdc_enable,
703 .disable = wm831x_dcdc_disable,
704 .get_status = wm831x_dcdc_get_status,
705 .get_mode = wm831x_dcdc_get_mode,
706 .set_mode = wm831x_dcdc_set_mode,
707 .set_suspend_mode = wm831x_dcdc_set_suspend_mode,
710 static __devinit int wm831x_buckp_probe(struct platform_device *pdev)
712 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
713 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
714 int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
715 struct wm831x_dcdc *dcdc;
716 struct resource *res;
717 int ret, irq;
719 dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
721 if (pdata == NULL || pdata->dcdc[id] == NULL)
722 return -ENODEV;
724 dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
725 if (dcdc == NULL) {
726 dev_err(&pdev->dev, "Unable to allocate private data\n");
727 return -ENOMEM;
730 dcdc->wm831x = wm831x;
732 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
733 if (res == NULL) {
734 dev_err(&pdev->dev, "No I/O resource\n");
735 ret = -EINVAL;
736 goto err;
738 dcdc->base = res->start;
740 snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
741 dcdc->desc.name = dcdc->name;
742 dcdc->desc.id = id;
743 dcdc->desc.type = REGULATOR_VOLTAGE;
744 dcdc->desc.n_voltages = WM831X_BUCKP_MAX_SELECTOR + 1;
745 dcdc->desc.ops = &wm831x_buckp_ops;
746 dcdc->desc.owner = THIS_MODULE;
748 dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
749 pdata->dcdc[id], dcdc);
750 if (IS_ERR(dcdc->regulator)) {
751 ret = PTR_ERR(dcdc->regulator);
752 dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
753 id + 1, ret);
754 goto err;
757 irq = platform_get_irq_byname(pdev, "UV");
758 ret = wm831x_request_irq(wm831x, irq, wm831x_dcdc_uv_irq,
759 IRQF_TRIGGER_RISING, dcdc->name,
760 dcdc);
761 if (ret != 0) {
762 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
763 irq, ret);
764 goto err_regulator;
767 platform_set_drvdata(pdev, dcdc);
769 return 0;
771 err_regulator:
772 regulator_unregister(dcdc->regulator);
773 err:
774 kfree(dcdc);
775 return ret;
778 static __devexit int wm831x_buckp_remove(struct platform_device *pdev)
780 struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
781 struct wm831x *wm831x = dcdc->wm831x;
783 platform_set_drvdata(pdev, NULL);
785 wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), dcdc);
786 regulator_unregister(dcdc->regulator);
787 kfree(dcdc);
789 return 0;
792 static struct platform_driver wm831x_buckp_driver = {
793 .probe = wm831x_buckp_probe,
794 .remove = __devexit_p(wm831x_buckp_remove),
795 .driver = {
796 .name = "wm831x-buckp",
797 .owner = THIS_MODULE,
802 * DCDC boost convertors
805 static int wm831x_boostp_get_status(struct regulator_dev *rdev)
807 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
808 struct wm831x *wm831x = dcdc->wm831x;
809 int ret;
811 /* First, check for errors */
812 ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
813 if (ret < 0)
814 return ret;
816 if (ret & (1 << rdev_get_id(rdev))) {
817 dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
818 rdev_get_id(rdev) + 1);
819 return REGULATOR_STATUS_ERROR;
822 /* Is the regulator on? */
823 ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
824 if (ret < 0)
825 return ret;
826 if (ret & (1 << rdev_get_id(rdev)))
827 return REGULATOR_STATUS_ON;
828 else
829 return REGULATOR_STATUS_OFF;
832 static struct regulator_ops wm831x_boostp_ops = {
833 .get_status = wm831x_boostp_get_status,
835 .is_enabled = wm831x_dcdc_is_enabled,
836 .enable = wm831x_dcdc_enable,
837 .disable = wm831x_dcdc_disable,
840 static __devinit int wm831x_boostp_probe(struct platform_device *pdev)
842 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
843 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
844 int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
845 struct wm831x_dcdc *dcdc;
846 struct resource *res;
847 int ret, irq;
849 dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
851 if (pdata == NULL || pdata->dcdc[id] == NULL)
852 return -ENODEV;
854 dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
855 if (dcdc == NULL) {
856 dev_err(&pdev->dev, "Unable to allocate private data\n");
857 return -ENOMEM;
860 dcdc->wm831x = wm831x;
862 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
863 if (res == NULL) {
864 dev_err(&pdev->dev, "No I/O resource\n");
865 ret = -EINVAL;
866 goto err;
868 dcdc->base = res->start;
870 snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
871 dcdc->desc.name = dcdc->name;
872 dcdc->desc.id = id;
873 dcdc->desc.type = REGULATOR_VOLTAGE;
874 dcdc->desc.ops = &wm831x_boostp_ops;
875 dcdc->desc.owner = THIS_MODULE;
877 dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
878 pdata->dcdc[id], dcdc);
879 if (IS_ERR(dcdc->regulator)) {
880 ret = PTR_ERR(dcdc->regulator);
881 dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
882 id + 1, ret);
883 goto err;
886 irq = platform_get_irq_byname(pdev, "UV");
887 ret = wm831x_request_irq(wm831x, irq, wm831x_dcdc_uv_irq,
888 IRQF_TRIGGER_RISING, dcdc->name,
889 dcdc);
890 if (ret != 0) {
891 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
892 irq, ret);
893 goto err_regulator;
896 platform_set_drvdata(pdev, dcdc);
898 return 0;
900 err_regulator:
901 regulator_unregister(dcdc->regulator);
902 err:
903 kfree(dcdc);
904 return ret;
907 static __devexit int wm831x_boostp_remove(struct platform_device *pdev)
909 struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
910 struct wm831x *wm831x = dcdc->wm831x;
912 platform_set_drvdata(pdev, NULL);
914 wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), dcdc);
915 regulator_unregister(dcdc->regulator);
916 kfree(dcdc);
918 return 0;
921 static struct platform_driver wm831x_boostp_driver = {
922 .probe = wm831x_boostp_probe,
923 .remove = __devexit_p(wm831x_boostp_remove),
924 .driver = {
925 .name = "wm831x-boostp",
926 .owner = THIS_MODULE,
931 * External Power Enable
933 * These aren't actually DCDCs but look like them in hardware so share
934 * code.
937 #define WM831X_EPE_BASE 6
939 static struct regulator_ops wm831x_epe_ops = {
940 .is_enabled = wm831x_dcdc_is_enabled,
941 .enable = wm831x_dcdc_enable,
942 .disable = wm831x_dcdc_disable,
943 .get_status = wm831x_dcdc_get_status,
946 static __devinit int wm831x_epe_probe(struct platform_device *pdev)
948 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
949 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
950 int id = pdev->id % ARRAY_SIZE(pdata->epe);
951 struct wm831x_dcdc *dcdc;
952 int ret;
954 dev_dbg(&pdev->dev, "Probing EPE%d\n", id + 1);
956 if (pdata == NULL || pdata->epe[id] == NULL)
957 return -ENODEV;
959 dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
960 if (dcdc == NULL) {
961 dev_err(&pdev->dev, "Unable to allocate private data\n");
962 return -ENOMEM;
965 dcdc->wm831x = wm831x;
967 /* For current parts this is correct; probably need to revisit
968 * in future.
970 snprintf(dcdc->name, sizeof(dcdc->name), "EPE%d", id + 1);
971 dcdc->desc.name = dcdc->name;
972 dcdc->desc.id = id + WM831X_EPE_BASE; /* Offset in DCDC registers */
973 dcdc->desc.ops = &wm831x_epe_ops;
974 dcdc->desc.type = REGULATOR_VOLTAGE;
975 dcdc->desc.owner = THIS_MODULE;
977 dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
978 pdata->epe[id], dcdc);
979 if (IS_ERR(dcdc->regulator)) {
980 ret = PTR_ERR(dcdc->regulator);
981 dev_err(wm831x->dev, "Failed to register EPE%d: %d\n",
982 id + 1, ret);
983 goto err;
986 platform_set_drvdata(pdev, dcdc);
988 return 0;
990 err:
991 kfree(dcdc);
992 return ret;
995 static __devexit int wm831x_epe_remove(struct platform_device *pdev)
997 struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
999 platform_set_drvdata(pdev, NULL);
1001 regulator_unregister(dcdc->regulator);
1002 kfree(dcdc);
1004 return 0;
1007 static struct platform_driver wm831x_epe_driver = {
1008 .probe = wm831x_epe_probe,
1009 .remove = __devexit_p(wm831x_epe_remove),
1010 .driver = {
1011 .name = "wm831x-epe",
1012 .owner = THIS_MODULE,
1016 static int __init wm831x_dcdc_init(void)
1018 int ret;
1019 ret = platform_driver_register(&wm831x_buckv_driver);
1020 if (ret != 0)
1021 pr_err("Failed to register WM831x BUCKV driver: %d\n", ret);
1023 ret = platform_driver_register(&wm831x_buckp_driver);
1024 if (ret != 0)
1025 pr_err("Failed to register WM831x BUCKP driver: %d\n", ret);
1027 ret = platform_driver_register(&wm831x_boostp_driver);
1028 if (ret != 0)
1029 pr_err("Failed to register WM831x BOOST driver: %d\n", ret);
1031 ret = platform_driver_register(&wm831x_epe_driver);
1032 if (ret != 0)
1033 pr_err("Failed to register WM831x EPE driver: %d\n", ret);
1035 return 0;
1037 subsys_initcall(wm831x_dcdc_init);
1039 static void __exit wm831x_dcdc_exit(void)
1041 platform_driver_unregister(&wm831x_epe_driver);
1042 platform_driver_unregister(&wm831x_boostp_driver);
1043 platform_driver_unregister(&wm831x_buckp_driver);
1044 platform_driver_unregister(&wm831x_buckv_driver);
1046 module_exit(wm831x_dcdc_exit);
1048 /* Module information */
1049 MODULE_AUTHOR("Mark Brown");
1050 MODULE_DESCRIPTION("WM831x DC-DC convertor driver");
1051 MODULE_LICENSE("GPL");
1052 MODULE_ALIAS("platform:wm831x-buckv");
1053 MODULE_ALIAS("platform:wm831x-buckp");