MIPS: Alchemy: DB1200: Remove custom wait implementation
[linux-2.6/linux-mips.git] / drivers / regulator / wm831x-dcdc.c
blobdbfaf5945e48a683c20cd87ff185dfc816b789e7
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)
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 /* If this value is already set then do a GPIO update if we can */
318 if (dcdc->dvs_gpio && dcdc->on_vsel == vsel)
319 return wm831x_buckv_set_dvs(rdev, 0);
321 if (dcdc->dvs_gpio && dcdc->dvs_vsel == vsel)
322 return wm831x_buckv_set_dvs(rdev, 1);
324 /* Always set the ON status to the minimum voltage */
325 ret = wm831x_set_bits(wm831x, on_reg, WM831X_DC1_ON_VSEL_MASK, vsel);
326 if (ret < 0)
327 return ret;
328 dcdc->on_vsel = vsel;
330 if (!dcdc->dvs_gpio)
331 return ret;
333 /* Kick the voltage transition now */
334 ret = wm831x_buckv_set_dvs(rdev, 0);
335 if (ret < 0)
336 return ret;
338 /* Set the high voltage as the DVS voltage. This is optimised
339 * for CPUfreq usage, most processors will keep the maximum
340 * voltage constant and lower the minimum with the frequency. */
341 vsel = wm831x_buckv_select_max_voltage(rdev, min_uV, max_uV);
342 if (vsel < 0) {
343 /* This should never happen - at worst the same vsel
344 * should be chosen */
345 WARN_ON(vsel < 0);
346 return 0;
349 /* Don't bother if it's the same VSEL we're already using */
350 if (vsel == dcdc->on_vsel)
351 return 0;
353 ret = wm831x_set_bits(wm831x, dvs_reg, WM831X_DC1_DVS_VSEL_MASK, vsel);
354 if (ret == 0)
355 dcdc->dvs_vsel = vsel;
356 else
357 dev_warn(wm831x->dev, "Failed to set DCDC DVS VSEL: %d\n",
358 ret);
360 return 0;
363 static int wm831x_buckv_set_suspend_voltage(struct regulator_dev *rdev,
364 int uV)
366 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
367 struct wm831x *wm831x = dcdc->wm831x;
368 u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
369 int vsel;
371 vsel = wm831x_buckv_select_min_voltage(rdev, uV, uV);
372 if (vsel < 0)
373 return vsel;
375 return wm831x_set_bits(wm831x, reg, WM831X_DC1_SLP_VSEL_MASK, vsel);
378 static int wm831x_buckv_get_voltage(struct regulator_dev *rdev)
380 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
382 if (dcdc->dvs_gpio && dcdc->dvs_gpio_state)
383 return wm831x_buckv_list_voltage(rdev, dcdc->dvs_vsel);
384 else
385 return wm831x_buckv_list_voltage(rdev, dcdc->on_vsel);
388 /* Current limit options */
389 static u16 wm831x_dcdc_ilim[] = {
390 125, 250, 375, 500, 625, 750, 875, 1000
393 static int wm831x_buckv_set_current_limit(struct regulator_dev *rdev,
394 int min_uA, int max_uA)
396 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
397 struct wm831x *wm831x = dcdc->wm831x;
398 u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2;
399 int i;
401 for (i = 0; i < ARRAY_SIZE(wm831x_dcdc_ilim); i++) {
402 if (max_uA <= wm831x_dcdc_ilim[i])
403 break;
405 if (i == ARRAY_SIZE(wm831x_dcdc_ilim))
406 return -EINVAL;
408 return wm831x_set_bits(wm831x, reg, WM831X_DC1_HC_THR_MASK, i);
411 static int wm831x_buckv_get_current_limit(struct regulator_dev *rdev)
413 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
414 struct wm831x *wm831x = dcdc->wm831x;
415 u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2;
416 int val;
418 val = wm831x_reg_read(wm831x, reg);
419 if (val < 0)
420 return val;
422 return wm831x_dcdc_ilim[val & WM831X_DC1_HC_THR_MASK];
425 static struct regulator_ops wm831x_buckv_ops = {
426 .set_voltage = wm831x_buckv_set_voltage,
427 .get_voltage = wm831x_buckv_get_voltage,
428 .list_voltage = wm831x_buckv_list_voltage,
429 .set_suspend_voltage = wm831x_buckv_set_suspend_voltage,
430 .set_current_limit = wm831x_buckv_set_current_limit,
431 .get_current_limit = wm831x_buckv_get_current_limit,
433 .is_enabled = wm831x_dcdc_is_enabled,
434 .enable = wm831x_dcdc_enable,
435 .disable = wm831x_dcdc_disable,
436 .get_status = wm831x_dcdc_get_status,
437 .get_mode = wm831x_dcdc_get_mode,
438 .set_mode = wm831x_dcdc_set_mode,
439 .set_suspend_mode = wm831x_dcdc_set_suspend_mode,
443 * Set up DVS control. We just log errors since we can still run
444 * (with reduced performance) if we fail.
446 static __devinit void wm831x_buckv_dvs_init(struct wm831x_dcdc *dcdc,
447 struct wm831x_buckv_pdata *pdata)
449 struct wm831x *wm831x = dcdc->wm831x;
450 int ret;
451 u16 ctrl;
453 if (!pdata || !pdata->dvs_gpio)
454 return;
456 switch (pdata->dvs_control_src) {
457 case 1:
458 ctrl = 2 << WM831X_DC1_DVS_SRC_SHIFT;
459 break;
460 case 2:
461 ctrl = 3 << WM831X_DC1_DVS_SRC_SHIFT;
462 break;
463 default:
464 dev_err(wm831x->dev, "Invalid DVS control source %d for %s\n",
465 pdata->dvs_control_src, dcdc->name);
466 return;
469 ret = wm831x_set_bits(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL,
470 WM831X_DC1_DVS_SRC_MASK, ctrl);
471 if (ret < 0) {
472 dev_err(wm831x->dev, "Failed to set %s DVS source: %d\n",
473 dcdc->name, ret);
474 return;
477 ret = gpio_request(pdata->dvs_gpio, "DCDC DVS");
478 if (ret < 0) {
479 dev_err(wm831x->dev, "Failed to get %s DVS GPIO: %d\n",
480 dcdc->name, ret);
481 return;
484 /* gpiolib won't let us read the GPIO status so pick the higher
485 * of the two existing voltages so we take it as platform data.
487 dcdc->dvs_gpio_state = pdata->dvs_init_state;
489 ret = gpio_direction_output(pdata->dvs_gpio, dcdc->dvs_gpio_state);
490 if (ret < 0) {
491 dev_err(wm831x->dev, "Failed to enable %s DVS GPIO: %d\n",
492 dcdc->name, ret);
493 gpio_free(pdata->dvs_gpio);
494 return;
497 dcdc->dvs_gpio = pdata->dvs_gpio;
500 static __devinit int wm831x_buckv_probe(struct platform_device *pdev)
502 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
503 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
504 int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
505 struct wm831x_dcdc *dcdc;
506 struct resource *res;
507 int ret, irq;
509 dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
511 if (pdata == NULL || pdata->dcdc[id] == NULL)
512 return -ENODEV;
514 dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
515 if (dcdc == NULL) {
516 dev_err(&pdev->dev, "Unable to allocate private data\n");
517 return -ENOMEM;
520 dcdc->wm831x = wm831x;
522 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
523 if (res == NULL) {
524 dev_err(&pdev->dev, "No I/O resource\n");
525 ret = -EINVAL;
526 goto err;
528 dcdc->base = res->start;
530 snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
531 dcdc->desc.name = dcdc->name;
532 dcdc->desc.id = id;
533 dcdc->desc.type = REGULATOR_VOLTAGE;
534 dcdc->desc.n_voltages = WM831X_BUCKV_MAX_SELECTOR + 1;
535 dcdc->desc.ops = &wm831x_buckv_ops;
536 dcdc->desc.owner = THIS_MODULE;
538 ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG);
539 if (ret < 0) {
540 dev_err(wm831x->dev, "Failed to read ON VSEL: %d\n", ret);
541 goto err;
543 dcdc->on_vsel = ret & WM831X_DC1_ON_VSEL_MASK;
545 ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG);
546 if (ret < 0) {
547 dev_err(wm831x->dev, "Failed to read DVS VSEL: %d\n", ret);
548 goto err;
550 dcdc->dvs_vsel = ret & WM831X_DC1_DVS_VSEL_MASK;
552 if (pdata->dcdc[id])
553 wm831x_buckv_dvs_init(dcdc, pdata->dcdc[id]->driver_data);
555 dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
556 pdata->dcdc[id], dcdc);
557 if (IS_ERR(dcdc->regulator)) {
558 ret = PTR_ERR(dcdc->regulator);
559 dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
560 id + 1, ret);
561 goto err;
564 irq = platform_get_irq_byname(pdev, "UV");
565 ret = wm831x_request_irq(wm831x, irq, wm831x_dcdc_uv_irq,
566 IRQF_TRIGGER_RISING, dcdc->name,
567 dcdc);
568 if (ret != 0) {
569 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
570 irq, ret);
571 goto err_regulator;
574 irq = platform_get_irq_byname(pdev, "HC");
575 ret = wm831x_request_irq(wm831x, irq, wm831x_dcdc_oc_irq,
576 IRQF_TRIGGER_RISING, dcdc->name,
577 dcdc);
578 if (ret != 0) {
579 dev_err(&pdev->dev, "Failed to request HC IRQ %d: %d\n",
580 irq, ret);
581 goto err_uv;
584 platform_set_drvdata(pdev, dcdc);
586 return 0;
588 err_uv:
589 wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), dcdc);
590 err_regulator:
591 regulator_unregister(dcdc->regulator);
592 err:
593 if (dcdc->dvs_gpio)
594 gpio_free(dcdc->dvs_gpio);
595 kfree(dcdc);
596 return ret;
599 static __devexit int wm831x_buckv_remove(struct platform_device *pdev)
601 struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
602 struct wm831x *wm831x = dcdc->wm831x;
604 platform_set_drvdata(pdev, NULL);
606 wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "HC"), dcdc);
607 wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), dcdc);
608 regulator_unregister(dcdc->regulator);
609 if (dcdc->dvs_gpio)
610 gpio_free(dcdc->dvs_gpio);
611 kfree(dcdc);
613 return 0;
616 static struct platform_driver wm831x_buckv_driver = {
617 .probe = wm831x_buckv_probe,
618 .remove = __devexit_p(wm831x_buckv_remove),
619 .driver = {
620 .name = "wm831x-buckv",
621 .owner = THIS_MODULE,
626 * BUCKP specifics
629 static int wm831x_buckp_list_voltage(struct regulator_dev *rdev,
630 unsigned selector)
632 if (selector <= WM831X_BUCKP_MAX_SELECTOR)
633 return 850000 + (selector * 25000);
634 else
635 return -EINVAL;
638 static int wm831x_buckp_set_voltage_int(struct regulator_dev *rdev, int reg,
639 int min_uV, int max_uV)
641 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
642 struct wm831x *wm831x = dcdc->wm831x;
643 u16 vsel;
645 if (min_uV <= 34000000)
646 vsel = (min_uV - 850000) / 25000;
647 else
648 return -EINVAL;
650 if (wm831x_buckp_list_voltage(rdev, vsel) > max_uV)
651 return -EINVAL;
653 return wm831x_set_bits(wm831x, reg, WM831X_DC3_ON_VSEL_MASK, vsel);
656 static int wm831x_buckp_set_voltage(struct regulator_dev *rdev,
657 int min_uV, int max_uV)
659 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
660 u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
662 return wm831x_buckp_set_voltage_int(rdev, reg, min_uV, max_uV);
665 static int wm831x_buckp_set_suspend_voltage(struct regulator_dev *rdev,
666 int uV)
668 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
669 u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
671 return wm831x_buckp_set_voltage_int(rdev, reg, uV, uV);
674 static int wm831x_buckp_get_voltage(struct regulator_dev *rdev)
676 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
677 struct wm831x *wm831x = dcdc->wm831x;
678 u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
679 int val;
681 val = wm831x_reg_read(wm831x, reg);
682 if (val < 0)
683 return val;
685 return wm831x_buckp_list_voltage(rdev, val & WM831X_DC3_ON_VSEL_MASK);
688 static struct regulator_ops wm831x_buckp_ops = {
689 .set_voltage = wm831x_buckp_set_voltage,
690 .get_voltage = wm831x_buckp_get_voltage,
691 .list_voltage = wm831x_buckp_list_voltage,
692 .set_suspend_voltage = wm831x_buckp_set_suspend_voltage,
694 .is_enabled = wm831x_dcdc_is_enabled,
695 .enable = wm831x_dcdc_enable,
696 .disable = wm831x_dcdc_disable,
697 .get_status = wm831x_dcdc_get_status,
698 .get_mode = wm831x_dcdc_get_mode,
699 .set_mode = wm831x_dcdc_set_mode,
700 .set_suspend_mode = wm831x_dcdc_set_suspend_mode,
703 static __devinit int wm831x_buckp_probe(struct platform_device *pdev)
705 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
706 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
707 int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
708 struct wm831x_dcdc *dcdc;
709 struct resource *res;
710 int ret, irq;
712 dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
714 if (pdata == NULL || pdata->dcdc[id] == NULL)
715 return -ENODEV;
717 dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
718 if (dcdc == NULL) {
719 dev_err(&pdev->dev, "Unable to allocate private data\n");
720 return -ENOMEM;
723 dcdc->wm831x = wm831x;
725 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
726 if (res == NULL) {
727 dev_err(&pdev->dev, "No I/O resource\n");
728 ret = -EINVAL;
729 goto err;
731 dcdc->base = res->start;
733 snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
734 dcdc->desc.name = dcdc->name;
735 dcdc->desc.id = id;
736 dcdc->desc.type = REGULATOR_VOLTAGE;
737 dcdc->desc.n_voltages = WM831X_BUCKP_MAX_SELECTOR + 1;
738 dcdc->desc.ops = &wm831x_buckp_ops;
739 dcdc->desc.owner = THIS_MODULE;
741 dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
742 pdata->dcdc[id], dcdc);
743 if (IS_ERR(dcdc->regulator)) {
744 ret = PTR_ERR(dcdc->regulator);
745 dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
746 id + 1, ret);
747 goto err;
750 irq = platform_get_irq_byname(pdev, "UV");
751 ret = wm831x_request_irq(wm831x, irq, wm831x_dcdc_uv_irq,
752 IRQF_TRIGGER_RISING, dcdc->name,
753 dcdc);
754 if (ret != 0) {
755 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
756 irq, ret);
757 goto err_regulator;
760 platform_set_drvdata(pdev, dcdc);
762 return 0;
764 err_regulator:
765 regulator_unregister(dcdc->regulator);
766 err:
767 kfree(dcdc);
768 return ret;
771 static __devexit int wm831x_buckp_remove(struct platform_device *pdev)
773 struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
774 struct wm831x *wm831x = dcdc->wm831x;
776 platform_set_drvdata(pdev, NULL);
778 wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), dcdc);
779 regulator_unregister(dcdc->regulator);
780 kfree(dcdc);
782 return 0;
785 static struct platform_driver wm831x_buckp_driver = {
786 .probe = wm831x_buckp_probe,
787 .remove = __devexit_p(wm831x_buckp_remove),
788 .driver = {
789 .name = "wm831x-buckp",
790 .owner = THIS_MODULE,
795 * DCDC boost convertors
798 static int wm831x_boostp_get_status(struct regulator_dev *rdev)
800 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
801 struct wm831x *wm831x = dcdc->wm831x;
802 int ret;
804 /* First, check for errors */
805 ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
806 if (ret < 0)
807 return ret;
809 if (ret & (1 << rdev_get_id(rdev))) {
810 dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
811 rdev_get_id(rdev) + 1);
812 return REGULATOR_STATUS_ERROR;
815 /* Is the regulator on? */
816 ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
817 if (ret < 0)
818 return ret;
819 if (ret & (1 << rdev_get_id(rdev)))
820 return REGULATOR_STATUS_ON;
821 else
822 return REGULATOR_STATUS_OFF;
825 static struct regulator_ops wm831x_boostp_ops = {
826 .get_status = wm831x_boostp_get_status,
828 .is_enabled = wm831x_dcdc_is_enabled,
829 .enable = wm831x_dcdc_enable,
830 .disable = wm831x_dcdc_disable,
833 static __devinit int wm831x_boostp_probe(struct platform_device *pdev)
835 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
836 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
837 int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
838 struct wm831x_dcdc *dcdc;
839 struct resource *res;
840 int ret, irq;
842 dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
844 if (pdata == NULL || pdata->dcdc[id] == NULL)
845 return -ENODEV;
847 dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
848 if (dcdc == NULL) {
849 dev_err(&pdev->dev, "Unable to allocate private data\n");
850 return -ENOMEM;
853 dcdc->wm831x = wm831x;
855 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
856 if (res == NULL) {
857 dev_err(&pdev->dev, "No I/O resource\n");
858 ret = -EINVAL;
859 goto err;
861 dcdc->base = res->start;
863 snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
864 dcdc->desc.name = dcdc->name;
865 dcdc->desc.id = id;
866 dcdc->desc.type = REGULATOR_VOLTAGE;
867 dcdc->desc.ops = &wm831x_boostp_ops;
868 dcdc->desc.owner = THIS_MODULE;
870 dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
871 pdata->dcdc[id], dcdc);
872 if (IS_ERR(dcdc->regulator)) {
873 ret = PTR_ERR(dcdc->regulator);
874 dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
875 id + 1, ret);
876 goto err;
879 irq = platform_get_irq_byname(pdev, "UV");
880 ret = wm831x_request_irq(wm831x, irq, wm831x_dcdc_uv_irq,
881 IRQF_TRIGGER_RISING, dcdc->name,
882 dcdc);
883 if (ret != 0) {
884 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
885 irq, ret);
886 goto err_regulator;
889 platform_set_drvdata(pdev, dcdc);
891 return 0;
893 err_regulator:
894 regulator_unregister(dcdc->regulator);
895 err:
896 kfree(dcdc);
897 return ret;
900 static __devexit int wm831x_boostp_remove(struct platform_device *pdev)
902 struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
903 struct wm831x *wm831x = dcdc->wm831x;
905 platform_set_drvdata(pdev, NULL);
907 wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), dcdc);
908 regulator_unregister(dcdc->regulator);
909 kfree(dcdc);
911 return 0;
914 static struct platform_driver wm831x_boostp_driver = {
915 .probe = wm831x_boostp_probe,
916 .remove = __devexit_p(wm831x_boostp_remove),
917 .driver = {
918 .name = "wm831x-boostp",
919 .owner = THIS_MODULE,
924 * External Power Enable
926 * These aren't actually DCDCs but look like them in hardware so share
927 * code.
930 #define WM831X_EPE_BASE 6
932 static struct regulator_ops wm831x_epe_ops = {
933 .is_enabled = wm831x_dcdc_is_enabled,
934 .enable = wm831x_dcdc_enable,
935 .disable = wm831x_dcdc_disable,
936 .get_status = wm831x_dcdc_get_status,
939 static __devinit int wm831x_epe_probe(struct platform_device *pdev)
941 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
942 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
943 int id = pdev->id % ARRAY_SIZE(pdata->epe);
944 struct wm831x_dcdc *dcdc;
945 int ret;
947 dev_dbg(&pdev->dev, "Probing EPE%d\n", id + 1);
949 if (pdata == NULL || pdata->epe[id] == NULL)
950 return -ENODEV;
952 dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
953 if (dcdc == NULL) {
954 dev_err(&pdev->dev, "Unable to allocate private data\n");
955 return -ENOMEM;
958 dcdc->wm831x = wm831x;
960 /* For current parts this is correct; probably need to revisit
961 * in future.
963 snprintf(dcdc->name, sizeof(dcdc->name), "EPE%d", id + 1);
964 dcdc->desc.name = dcdc->name;
965 dcdc->desc.id = id + WM831X_EPE_BASE; /* Offset in DCDC registers */
966 dcdc->desc.ops = &wm831x_epe_ops;
967 dcdc->desc.type = REGULATOR_VOLTAGE;
968 dcdc->desc.owner = THIS_MODULE;
970 dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
971 pdata->epe[id], dcdc);
972 if (IS_ERR(dcdc->regulator)) {
973 ret = PTR_ERR(dcdc->regulator);
974 dev_err(wm831x->dev, "Failed to register EPE%d: %d\n",
975 id + 1, ret);
976 goto err;
979 platform_set_drvdata(pdev, dcdc);
981 return 0;
983 err:
984 kfree(dcdc);
985 return ret;
988 static __devexit int wm831x_epe_remove(struct platform_device *pdev)
990 struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
992 platform_set_drvdata(pdev, NULL);
994 regulator_unregister(dcdc->regulator);
995 kfree(dcdc);
997 return 0;
1000 static struct platform_driver wm831x_epe_driver = {
1001 .probe = wm831x_epe_probe,
1002 .remove = __devexit_p(wm831x_epe_remove),
1003 .driver = {
1004 .name = "wm831x-epe",
1005 .owner = THIS_MODULE,
1009 static int __init wm831x_dcdc_init(void)
1011 int ret;
1012 ret = platform_driver_register(&wm831x_buckv_driver);
1013 if (ret != 0)
1014 pr_err("Failed to register WM831x BUCKV driver: %d\n", ret);
1016 ret = platform_driver_register(&wm831x_buckp_driver);
1017 if (ret != 0)
1018 pr_err("Failed to register WM831x BUCKP driver: %d\n", ret);
1020 ret = platform_driver_register(&wm831x_boostp_driver);
1021 if (ret != 0)
1022 pr_err("Failed to register WM831x BOOST driver: %d\n", ret);
1024 ret = platform_driver_register(&wm831x_epe_driver);
1025 if (ret != 0)
1026 pr_err("Failed to register WM831x EPE driver: %d\n", ret);
1028 return 0;
1030 subsys_initcall(wm831x_dcdc_init);
1032 static void __exit wm831x_dcdc_exit(void)
1034 platform_driver_unregister(&wm831x_epe_driver);
1035 platform_driver_unregister(&wm831x_boostp_driver);
1036 platform_driver_unregister(&wm831x_buckp_driver);
1037 platform_driver_unregister(&wm831x_buckv_driver);
1039 module_exit(wm831x_dcdc_exit);
1041 /* Module information */
1042 MODULE_AUTHOR("Mark Brown");
1043 MODULE_DESCRIPTION("WM831x DC-DC convertor driver");
1044 MODULE_LICENSE("GPL");
1045 MODULE_ALIAS("platform:wm831x-buckv");
1046 MODULE_ALIAS("platform:wm831x-buckp");