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
52 char name
[WM831X_DCDC_MAX_NAME
];
53 struct regulator_desc desc
;
55 struct wm831x
*wm831x
;
56 struct regulator_dev
*regulator
;
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
);
70 reg
= wm831x_reg_read(wm831x
, WM831X_DCDC_ENABLE
);
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
;
106 val
= wm831x_reg_read(wm831x
, reg
);
110 val
= (val
& WM831X_DC1_ON_MODE_MASK
) >> WM831X_DC1_ON_MODE_SHIFT
;
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
;
127 static int wm831x_dcdc_set_mode_int(struct wm831x
*wm831x
, int reg
,
133 case REGULATOR_MODE_FAST
:
134 val
= WM831X_DCDC_MODE_FAST
;
136 case REGULATOR_MODE_NORMAL
:
137 val
= WM831X_DCDC_MODE_NORMAL
;
139 case REGULATOR_MODE_STANDBY
:
140 val
= WM831X_DCDC_MODE_STANDBY
;
142 case REGULATOR_MODE_IDLE
:
143 val
= WM831X_DCDC_MODE_IDLE
;
149 return wm831x_set_bits(wm831x
, reg
, WM831X_DC1_ON_MODE_MASK
,
150 val
<< WM831X_DC1_ON_MODE_SHIFT
);
153 static int wm831x_dcdc_set_mode(struct regulator_dev
*rdev
, unsigned int mode
)
155 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
156 struct wm831x
*wm831x
= dcdc
->wm831x
;
157 u16 reg
= dcdc
->base
+ WM831X_DCDC_ON_CONFIG
;
159 return wm831x_dcdc_set_mode_int(wm831x
, reg
, mode
);
162 static int wm831x_dcdc_set_suspend_mode(struct regulator_dev
*rdev
,
165 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
166 struct wm831x
*wm831x
= dcdc
->wm831x
;
167 u16 reg
= dcdc
->base
+ WM831X_DCDC_SLEEP_CONTROL
;
169 return wm831x_dcdc_set_mode_int(wm831x
, reg
, mode
);
172 static int wm831x_dcdc_get_status(struct regulator_dev
*rdev
)
174 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
175 struct wm831x
*wm831x
= dcdc
->wm831x
;
178 /* First, check for errors */
179 ret
= wm831x_reg_read(wm831x
, WM831X_DCDC_UV_STATUS
);
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
;
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
;
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
;
204 /* Is the regulator on? */
205 ret
= wm831x_reg_read(wm831x
, WM831X_DCDC_STATUS
);
208 if (!(ret
& (1 << rdev_get_id(rdev
))))
209 return REGULATOR_STATUS_OFF
;
211 /* TODO: When we handle hardware control modes so we can report the
213 return REGULATOR_STATUS_ON
;
216 static irqreturn_t
wm831x_dcdc_uv_irq(int irq
, void *data
)
218 struct wm831x_dcdc
*dcdc
= data
;
220 regulator_notifier_call_chain(dcdc
->regulator
,
221 REGULATOR_EVENT_UNDER_VOLTAGE
,
227 static irqreturn_t
wm831x_dcdc_oc_irq(int irq
, void *data
)
229 struct wm831x_dcdc
*dcdc
= data
;
231 regulator_notifier_call_chain(dcdc
->regulator
,
232 REGULATOR_EVENT_OVER_CURRENT
,
242 static int wm831x_buckv_list_voltage(struct regulator_dev
*rdev
,
247 if (selector
<= WM831X_BUCKV_MAX_SELECTOR
)
248 return 600000 + ((selector
- 0x8) * 12500);
252 static int wm831x_buckv_select_min_voltage(struct regulator_dev
*rdev
,
253 int min_uV
, int max_uV
)
259 else if (min_uV
<= 1800000)
260 vsel
= ((min_uV
- 600000) / 12500) + 8;
264 if (wm831x_buckv_list_voltage(rdev
, vsel
) > max_uV
)
270 static int wm831x_buckv_select_max_voltage(struct regulator_dev
*rdev
,
271 int min_uV
, int max_uV
)
275 if (max_uV
< 600000 || max_uV
> 1800000)
278 vsel
= ((max_uV
- 600000) / 12500) + 8;
280 if (wm831x_buckv_list_voltage(rdev
, vsel
) < min_uV
||
281 wm831x_buckv_list_voltage(rdev
, vsel
) < max_uV
)
287 static int wm831x_buckv_set_dvs(struct regulator_dev
*rdev
, int state
)
289 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
291 if (state
== dcdc
->dvs_gpio_state
)
294 dcdc
->dvs_gpio_state
= state
;
295 gpio_set_value(dcdc
->dvs_gpio
, state
);
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.
305 static int wm831x_buckv_set_voltage(struct regulator_dev
*rdev
,
306 int min_uV
, int max_uV
, unsigned *selector
)
308 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
309 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
;
314 vsel
= wm831x_buckv_select_min_voltage(rdev
, min_uV
, max_uV
);
320 /* 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);
324 if (dcdc
->dvs_gpio
&& dcdc
->dvs_vsel
== vsel
)
325 return wm831x_buckv_set_dvs(rdev
, 1);
327 /* Always set the ON status to the minimum voltage */
328 ret
= wm831x_set_bits(wm831x
, on_reg
, WM831X_DC1_ON_VSEL_MASK
, vsel
);
331 dcdc
->on_vsel
= vsel
;
336 /* Kick the voltage transition now */
337 ret
= wm831x_buckv_set_dvs(rdev
, 0);
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
);
346 /* This should never happen - at worst the same vsel
347 * should be chosen */
352 /* Don't bother if it's the same VSEL we're already using */
353 if (vsel
== dcdc
->on_vsel
)
356 ret
= wm831x_set_bits(wm831x
, dvs_reg
, WM831X_DC1_DVS_VSEL_MASK
, vsel
);
358 dcdc
->dvs_vsel
= vsel
;
360 dev_warn(wm831x
->dev
, "Failed to set DCDC DVS VSEL: %d\n",
366 static int wm831x_buckv_set_suspend_voltage(struct regulator_dev
*rdev
,
369 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
370 struct wm831x
*wm831x
= dcdc
->wm831x
;
371 u16 reg
= dcdc
->base
+ WM831X_DCDC_SLEEP_CONTROL
;
374 vsel
= wm831x_buckv_select_min_voltage(rdev
, uV
, uV
);
378 return wm831x_set_bits(wm831x
, reg
, WM831X_DC1_SLP_VSEL_MASK
, vsel
);
381 static int wm831x_buckv_get_voltage_sel(struct regulator_dev
*rdev
)
383 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
385 if (dcdc
->dvs_gpio
&& dcdc
->dvs_gpio_state
)
386 return dcdc
->dvs_vsel
;
388 return dcdc
->on_vsel
;
391 /* Current limit options */
392 static u16 wm831x_dcdc_ilim
[] = {
393 125, 250, 375, 500, 625, 750, 875, 1000
396 static int wm831x_buckv_set_current_limit(struct regulator_dev
*rdev
,
397 int min_uA
, int max_uA
)
399 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
400 struct wm831x
*wm831x
= dcdc
->wm831x
;
401 u16 reg
= dcdc
->base
+ WM831X_DCDC_CONTROL_2
;
404 for (i
= 0; i
< ARRAY_SIZE(wm831x_dcdc_ilim
); i
++) {
405 if (max_uA
<= wm831x_dcdc_ilim
[i
])
408 if (i
== ARRAY_SIZE(wm831x_dcdc_ilim
))
411 return wm831x_set_bits(wm831x
, reg
, WM831X_DC1_HC_THR_MASK
, i
);
414 static int wm831x_buckv_get_current_limit(struct regulator_dev
*rdev
)
416 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
417 struct wm831x
*wm831x
= dcdc
->wm831x
;
418 u16 reg
= dcdc
->base
+ WM831X_DCDC_CONTROL_2
;
421 val
= wm831x_reg_read(wm831x
, reg
);
425 return wm831x_dcdc_ilim
[val
& WM831X_DC1_HC_THR_MASK
];
428 static struct regulator_ops wm831x_buckv_ops
= {
429 .set_voltage
= wm831x_buckv_set_voltage
,
430 .get_voltage_sel
= wm831x_buckv_get_voltage_sel
,
431 .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
,
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
,
446 * Set up DVS control. We just log errors since we can still run
447 * (with reduced performance) if we fail.
449 static __devinit
void wm831x_buckv_dvs_init(struct wm831x_dcdc
*dcdc
,
450 struct wm831x_buckv_pdata
*pdata
)
452 struct wm831x
*wm831x
= dcdc
->wm831x
;
456 if (!pdata
|| !pdata
->dvs_gpio
)
459 switch (pdata
->dvs_control_src
) {
461 ctrl
= 2 << WM831X_DC1_DVS_SRC_SHIFT
;
464 ctrl
= 3 << WM831X_DC1_DVS_SRC_SHIFT
;
467 dev_err(wm831x
->dev
, "Invalid DVS control source %d for %s\n",
468 pdata
->dvs_control_src
, dcdc
->name
);
472 ret
= wm831x_set_bits(wm831x
, dcdc
->base
+ WM831X_DCDC_DVS_CONTROL
,
473 WM831X_DC1_DVS_SRC_MASK
, ctrl
);
475 dev_err(wm831x
->dev
, "Failed to set %s DVS source: %d\n",
480 ret
= gpio_request(pdata
->dvs_gpio
, "DCDC DVS");
482 dev_err(wm831x
->dev
, "Failed to get %s DVS GPIO: %d\n",
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.
490 dcdc
->dvs_gpio_state
= pdata
->dvs_init_state
;
492 ret
= gpio_direction_output(pdata
->dvs_gpio
, dcdc
->dvs_gpio_state
);
494 dev_err(wm831x
->dev
, "Failed to enable %s DVS GPIO: %d\n",
496 gpio_free(pdata
->dvs_gpio
);
500 dcdc
->dvs_gpio
= pdata
->dvs_gpio
;
503 static __devinit
int wm831x_buckv_probe(struct platform_device
*pdev
)
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
;
512 dev_dbg(&pdev
->dev
, "Probing DCDC%d\n", id
+ 1);
514 if (pdata
== NULL
|| pdata
->dcdc
[id
] == NULL
)
517 dcdc
= kzalloc(sizeof(struct wm831x_dcdc
), GFP_KERNEL
);
519 dev_err(&pdev
->dev
, "Unable to allocate private data\n");
523 dcdc
->wm831x
= wm831x
;
525 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
527 dev_err(&pdev
->dev
, "No I/O resource\n");
531 dcdc
->base
= res
->start
;
533 snprintf(dcdc
->name
, sizeof(dcdc
->name
), "DCDC%d", id
+ 1);
534 dcdc
->desc
.name
= dcdc
->name
;
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
;
541 ret
= wm831x_reg_read(wm831x
, dcdc
->base
+ WM831X_DCDC_ON_CONFIG
);
543 dev_err(wm831x
->dev
, "Failed to read ON VSEL: %d\n", ret
);
546 dcdc
->on_vsel
= ret
& WM831X_DC1_ON_VSEL_MASK
;
548 ret
= wm831x_reg_read(wm831x
, dcdc
->base
+ WM831X_DCDC_ON_CONFIG
);
550 dev_err(wm831x
->dev
, "Failed to read DVS VSEL: %d\n", ret
);
553 dcdc
->dvs_vsel
= ret
& WM831X_DC1_DVS_VSEL_MASK
;
556 wm831x_buckv_dvs_init(dcdc
, pdata
->dcdc
[id
]->driver_data
);
558 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",
567 irq
= platform_get_irq_byname(pdev
, "UV");
568 ret
= request_threaded_irq(irq
, NULL
, wm831x_dcdc_uv_irq
,
569 IRQF_TRIGGER_RISING
, dcdc
->name
, dcdc
);
571 dev_err(&pdev
->dev
, "Failed to request UV IRQ %d: %d\n",
576 irq
= platform_get_irq_byname(pdev
, "HC");
577 ret
= request_threaded_irq(irq
, NULL
, wm831x_dcdc_oc_irq
,
578 IRQF_TRIGGER_RISING
, dcdc
->name
, dcdc
);
580 dev_err(&pdev
->dev
, "Failed to request HC IRQ %d: %d\n",
585 platform_set_drvdata(pdev
, dcdc
);
590 free_irq(platform_get_irq_byname(pdev
, "UV"), dcdc
);
592 regulator_unregister(dcdc
->regulator
);
595 gpio_free(dcdc
->dvs_gpio
);
600 static __devexit
int wm831x_buckv_remove(struct platform_device
*pdev
)
602 struct wm831x_dcdc
*dcdc
= platform_get_drvdata(pdev
);
604 platform_set_drvdata(pdev
, NULL
);
606 free_irq(platform_get_irq_byname(pdev
, "HC"), dcdc
);
607 free_irq(platform_get_irq_byname(pdev
, "UV"), dcdc
);
608 regulator_unregister(dcdc
->regulator
);
610 gpio_free(dcdc
->dvs_gpio
);
616 static struct platform_driver wm831x_buckv_driver
= {
617 .probe
= wm831x_buckv_probe
,
618 .remove
= __devexit_p(wm831x_buckv_remove
),
620 .name
= "wm831x-buckv",
621 .owner
= THIS_MODULE
,
629 static int wm831x_buckp_list_voltage(struct regulator_dev
*rdev
,
632 if (selector
<= WM831X_BUCKP_MAX_SELECTOR
)
633 return 850000 + (selector
* 25000);
638 static int wm831x_buckp_set_voltage_int(struct regulator_dev
*rdev
, int reg
,
639 int min_uV
, int max_uV
, int *selector
)
641 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
642 struct wm831x
*wm831x
= dcdc
->wm831x
;
645 if (min_uV
<= 34000000)
646 vsel
= (min_uV
- 850000) / 25000;
650 if (wm831x_buckp_list_voltage(rdev
, vsel
) > max_uV
)
655 return wm831x_set_bits(wm831x
, reg
, WM831X_DC3_ON_VSEL_MASK
, vsel
);
658 static int wm831x_buckp_set_voltage(struct regulator_dev
*rdev
,
659 int min_uV
, int max_uV
,
662 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
663 u16 reg
= dcdc
->base
+ WM831X_DCDC_ON_CONFIG
;
665 return wm831x_buckp_set_voltage_int(rdev
, reg
, min_uV
, max_uV
,
669 static int wm831x_buckp_set_suspend_voltage(struct regulator_dev
*rdev
,
672 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
673 u16 reg
= dcdc
->base
+ WM831X_DCDC_SLEEP_CONTROL
;
676 return wm831x_buckp_set_voltage_int(rdev
, reg
, uV
, uV
, &selector
);
679 static int wm831x_buckp_get_voltage_sel(struct regulator_dev
*rdev
)
681 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
682 struct wm831x
*wm831x
= dcdc
->wm831x
;
683 u16 reg
= dcdc
->base
+ WM831X_DCDC_ON_CONFIG
;
686 val
= wm831x_reg_read(wm831x
, reg
);
690 return val
& WM831X_DC3_ON_VSEL_MASK
;
693 static struct regulator_ops wm831x_buckp_ops
= {
694 .set_voltage
= wm831x_buckp_set_voltage
,
695 .get_voltage_sel
= wm831x_buckp_get_voltage_sel
,
696 .list_voltage
= wm831x_buckp_list_voltage
,
697 .set_suspend_voltage
= wm831x_buckp_set_suspend_voltage
,
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
,
708 static __devinit
int wm831x_buckp_probe(struct platform_device
*pdev
)
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
;
717 dev_dbg(&pdev
->dev
, "Probing DCDC%d\n", id
+ 1);
719 if (pdata
== NULL
|| pdata
->dcdc
[id
] == NULL
)
722 dcdc
= kzalloc(sizeof(struct wm831x_dcdc
), GFP_KERNEL
);
724 dev_err(&pdev
->dev
, "Unable to allocate private data\n");
728 dcdc
->wm831x
= wm831x
;
730 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
732 dev_err(&pdev
->dev
, "No I/O resource\n");
736 dcdc
->base
= res
->start
;
738 snprintf(dcdc
->name
, sizeof(dcdc
->name
), "DCDC%d", id
+ 1);
739 dcdc
->desc
.name
= dcdc
->name
;
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
;
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",
755 irq
= platform_get_irq_byname(pdev
, "UV");
756 ret
= request_threaded_irq(irq
, NULL
, wm831x_dcdc_uv_irq
,
757 IRQF_TRIGGER_RISING
, dcdc
->name
, dcdc
);
759 dev_err(&pdev
->dev
, "Failed to request UV IRQ %d: %d\n",
764 platform_set_drvdata(pdev
, dcdc
);
769 regulator_unregister(dcdc
->regulator
);
775 static __devexit
int wm831x_buckp_remove(struct platform_device
*pdev
)
777 struct wm831x_dcdc
*dcdc
= platform_get_drvdata(pdev
);
779 platform_set_drvdata(pdev
, NULL
);
781 free_irq(platform_get_irq_byname(pdev
, "UV"), dcdc
);
782 regulator_unregister(dcdc
->regulator
);
788 static struct platform_driver wm831x_buckp_driver
= {
789 .probe
= wm831x_buckp_probe
,
790 .remove
= __devexit_p(wm831x_buckp_remove
),
792 .name
= "wm831x-buckp",
793 .owner
= THIS_MODULE
,
798 * DCDC boost convertors
801 static int wm831x_boostp_get_status(struct regulator_dev
*rdev
)
803 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
804 struct wm831x
*wm831x
= dcdc
->wm831x
;
807 /* First, check for errors */
808 ret
= wm831x_reg_read(wm831x
, WM831X_DCDC_UV_STATUS
);
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
;
818 /* Is the regulator on? */
819 ret
= wm831x_reg_read(wm831x
, WM831X_DCDC_STATUS
);
822 if (ret
& (1 << rdev_get_id(rdev
)))
823 return REGULATOR_STATUS_ON
;
825 return REGULATOR_STATUS_OFF
;
828 static struct regulator_ops wm831x_boostp_ops
= {
829 .get_status
= wm831x_boostp_get_status
,
831 .is_enabled
= wm831x_dcdc_is_enabled
,
832 .enable
= wm831x_dcdc_enable
,
833 .disable
= wm831x_dcdc_disable
,
836 static __devinit
int wm831x_boostp_probe(struct platform_device
*pdev
)
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
;
845 dev_dbg(&pdev
->dev
, "Probing DCDC%d\n", id
+ 1);
847 if (pdata
== NULL
|| pdata
->dcdc
[id
] == NULL
)
850 dcdc
= kzalloc(sizeof(struct wm831x_dcdc
), GFP_KERNEL
);
852 dev_err(&pdev
->dev
, "Unable to allocate private data\n");
856 dcdc
->wm831x
= wm831x
;
858 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
860 dev_err(&pdev
->dev
, "No I/O resource\n");
864 dcdc
->base
= res
->start
;
866 snprintf(dcdc
->name
, sizeof(dcdc
->name
), "DCDC%d", id
+ 1);
867 dcdc
->desc
.name
= dcdc
->name
;
869 dcdc
->desc
.type
= REGULATOR_VOLTAGE
;
870 dcdc
->desc
.ops
= &wm831x_boostp_ops
;
871 dcdc
->desc
.owner
= THIS_MODULE
;
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",
882 irq
= platform_get_irq_byname(pdev
, "UV");
883 ret
= request_threaded_irq(irq
, NULL
, wm831x_dcdc_uv_irq
,
884 IRQF_TRIGGER_RISING
, dcdc
->name
,
887 dev_err(&pdev
->dev
, "Failed to request UV IRQ %d: %d\n",
892 platform_set_drvdata(pdev
, dcdc
);
897 regulator_unregister(dcdc
->regulator
);
903 static __devexit
int wm831x_boostp_remove(struct platform_device
*pdev
)
905 struct wm831x_dcdc
*dcdc
= platform_get_drvdata(pdev
);
907 platform_set_drvdata(pdev
, NULL
);
909 free_irq(platform_get_irq_byname(pdev
, "UV"), dcdc
);
910 regulator_unregister(dcdc
->regulator
);
916 static struct platform_driver wm831x_boostp_driver
= {
917 .probe
= wm831x_boostp_probe
,
918 .remove
= __devexit_p(wm831x_boostp_remove
),
920 .name
= "wm831x-boostp",
921 .owner
= THIS_MODULE
,
926 * External Power Enable
928 * These aren't actually DCDCs but look like them in hardware so share
932 #define WM831X_EPE_BASE 6
934 static 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
,
941 static __devinit
int wm831x_epe_probe(struct platform_device
*pdev
)
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
;
949 dev_dbg(&pdev
->dev
, "Probing EPE%d\n", id
+ 1);
951 if (pdata
== NULL
|| pdata
->epe
[id
] == NULL
)
954 dcdc
= kzalloc(sizeof(struct wm831x_dcdc
), GFP_KERNEL
);
956 dev_err(&pdev
->dev
, "Unable to allocate private data\n");
960 dcdc
->wm831x
= wm831x
;
962 /* For current parts this is correct; probably need to revisit
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
;
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",
981 platform_set_drvdata(pdev
, dcdc
);
990 static __devexit
int wm831x_epe_remove(struct platform_device
*pdev
)
992 struct wm831x_dcdc
*dcdc
= platform_get_drvdata(pdev
);
994 platform_set_drvdata(pdev
, NULL
);
996 regulator_unregister(dcdc
->regulator
);
1002 static struct platform_driver wm831x_epe_driver
= {
1003 .probe
= wm831x_epe_probe
,
1004 .remove
= __devexit_p(wm831x_epe_remove
),
1006 .name
= "wm831x-epe",
1007 .owner
= THIS_MODULE
,
1011 static int __init
wm831x_dcdc_init(void)
1014 ret
= platform_driver_register(&wm831x_buckv_driver
);
1016 pr_err("Failed to register WM831x BUCKV driver: %d\n", ret
);
1018 ret
= platform_driver_register(&wm831x_buckp_driver
);
1020 pr_err("Failed to register WM831x BUCKP driver: %d\n", ret
);
1022 ret
= platform_driver_register(&wm831x_boostp_driver
);
1024 pr_err("Failed to register WM831x BOOST driver: %d\n", ret
);
1026 ret
= platform_driver_register(&wm831x_epe_driver
);
1028 pr_err("Failed to register WM831x EPE driver: %d\n", ret
);
1032 subsys_initcall(wm831x_dcdc_init
);
1034 static void __exit
wm831x_dcdc_exit(void)
1036 platform_driver_unregister(&wm831x_epe_driver
);
1037 platform_driver_unregister(&wm831x_boostp_driver
);
1038 platform_driver_unregister(&wm831x_buckp_driver
);
1039 platform_driver_unregister(&wm831x_buckv_driver
);
1041 module_exit(wm831x_dcdc_exit
);
1043 /* Module information */
1044 MODULE_AUTHOR("Mark Brown");
1045 MODULE_DESCRIPTION("WM831x DC-DC convertor driver");
1046 MODULE_LICENSE("GPL");
1047 MODULE_ALIAS("platform:wm831x-buckv");
1048 MODULE_ALIAS("platform:wm831x-buckp");