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_set_dvs(struct regulator_dev
*rdev
, int state
)
272 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
274 if (state
== dcdc
->dvs_gpio_state
)
277 dcdc
->dvs_gpio_state
= state
;
278 gpio_set_value(dcdc
->dvs_gpio
, state
);
280 /* Should wait for DVS state change to be asserted if we have
281 * a GPIO for it, for now assume the device is configured
282 * for the fastest possible transition.
288 static int wm831x_buckv_set_voltage(struct regulator_dev
*rdev
,
289 int min_uV
, int max_uV
, unsigned *selector
)
291 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
292 struct wm831x
*wm831x
= dcdc
->wm831x
;
293 int on_reg
= dcdc
->base
+ WM831X_DCDC_ON_CONFIG
;
294 int dvs_reg
= dcdc
->base
+ WM831X_DCDC_DVS_CONTROL
;
297 vsel
= wm831x_buckv_select_min_voltage(rdev
, min_uV
, max_uV
);
303 /* If this value is already set then do a GPIO update if we can */
304 if (dcdc
->dvs_gpio
&& dcdc
->on_vsel
== vsel
)
305 return wm831x_buckv_set_dvs(rdev
, 0);
307 if (dcdc
->dvs_gpio
&& dcdc
->dvs_vsel
== vsel
)
308 return wm831x_buckv_set_dvs(rdev
, 1);
310 /* Always set the ON status to the minimum voltage */
311 ret
= wm831x_set_bits(wm831x
, on_reg
, WM831X_DC1_ON_VSEL_MASK
, vsel
);
314 dcdc
->on_vsel
= vsel
;
319 /* Kick the voltage transition now */
320 ret
= wm831x_buckv_set_dvs(rdev
, 0);
325 * If this VSEL is higher than the last one we've seen then
326 * remember it as the DVS VSEL. This is optimised for CPUfreq
327 * usage where we want to get to the highest voltage very
330 if (vsel
> dcdc
->dvs_vsel
) {
331 ret
= wm831x_set_bits(wm831x
, dvs_reg
,
332 WM831X_DC1_DVS_VSEL_MASK
,
335 dcdc
->dvs_vsel
= vsel
;
337 dev_warn(wm831x
->dev
,
338 "Failed to set DCDC DVS VSEL: %d\n", ret
);
344 static int wm831x_buckv_set_suspend_voltage(struct regulator_dev
*rdev
,
347 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
348 struct wm831x
*wm831x
= dcdc
->wm831x
;
349 u16 reg
= dcdc
->base
+ WM831X_DCDC_SLEEP_CONTROL
;
352 vsel
= wm831x_buckv_select_min_voltage(rdev
, uV
, uV
);
356 return wm831x_set_bits(wm831x
, reg
, WM831X_DC1_SLP_VSEL_MASK
, vsel
);
359 static int wm831x_buckv_get_voltage_sel(struct regulator_dev
*rdev
)
361 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
363 if (dcdc
->dvs_gpio
&& dcdc
->dvs_gpio_state
)
364 return dcdc
->dvs_vsel
;
366 return dcdc
->on_vsel
;
369 /* Current limit options */
370 static u16 wm831x_dcdc_ilim
[] = {
371 125, 250, 375, 500, 625, 750, 875, 1000
374 static int wm831x_buckv_set_current_limit(struct regulator_dev
*rdev
,
375 int min_uA
, int max_uA
)
377 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
378 struct wm831x
*wm831x
= dcdc
->wm831x
;
379 u16 reg
= dcdc
->base
+ WM831X_DCDC_CONTROL_2
;
382 for (i
= 0; i
< ARRAY_SIZE(wm831x_dcdc_ilim
); i
++) {
383 if (max_uA
<= wm831x_dcdc_ilim
[i
])
386 if (i
== ARRAY_SIZE(wm831x_dcdc_ilim
))
389 return wm831x_set_bits(wm831x
, reg
, WM831X_DC1_HC_THR_MASK
, i
);
392 static int wm831x_buckv_get_current_limit(struct regulator_dev
*rdev
)
394 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
395 struct wm831x
*wm831x
= dcdc
->wm831x
;
396 u16 reg
= dcdc
->base
+ WM831X_DCDC_CONTROL_2
;
399 val
= wm831x_reg_read(wm831x
, reg
);
403 return wm831x_dcdc_ilim
[val
& WM831X_DC1_HC_THR_MASK
];
406 static struct regulator_ops wm831x_buckv_ops
= {
407 .set_voltage
= wm831x_buckv_set_voltage
,
408 .get_voltage_sel
= wm831x_buckv_get_voltage_sel
,
409 .list_voltage
= wm831x_buckv_list_voltage
,
410 .set_suspend_voltage
= wm831x_buckv_set_suspend_voltage
,
411 .set_current_limit
= wm831x_buckv_set_current_limit
,
412 .get_current_limit
= wm831x_buckv_get_current_limit
,
414 .is_enabled
= wm831x_dcdc_is_enabled
,
415 .enable
= wm831x_dcdc_enable
,
416 .disable
= wm831x_dcdc_disable
,
417 .get_status
= wm831x_dcdc_get_status
,
418 .get_mode
= wm831x_dcdc_get_mode
,
419 .set_mode
= wm831x_dcdc_set_mode
,
420 .set_suspend_mode
= wm831x_dcdc_set_suspend_mode
,
424 * Set up DVS control. We just log errors since we can still run
425 * (with reduced performance) if we fail.
427 static __devinit
void wm831x_buckv_dvs_init(struct wm831x_dcdc
*dcdc
,
428 struct wm831x_buckv_pdata
*pdata
)
430 struct wm831x
*wm831x
= dcdc
->wm831x
;
434 if (!pdata
|| !pdata
->dvs_gpio
)
437 ret
= gpio_request(pdata
->dvs_gpio
, "DCDC DVS");
439 dev_err(wm831x
->dev
, "Failed to get %s DVS GPIO: %d\n",
444 /* gpiolib won't let us read the GPIO status so pick the higher
445 * of the two existing voltages so we take it as platform data.
447 dcdc
->dvs_gpio_state
= pdata
->dvs_init_state
;
449 ret
= gpio_direction_output(pdata
->dvs_gpio
, dcdc
->dvs_gpio_state
);
451 dev_err(wm831x
->dev
, "Failed to enable %s DVS GPIO: %d\n",
453 gpio_free(pdata
->dvs_gpio
);
457 dcdc
->dvs_gpio
= 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 /* If DVS_VSEL is set to the minimum value then raise it to ON_VSEL
473 * to make bootstrapping a bit smoother.
475 if (!dcdc
->dvs_vsel
) {
476 ret
= wm831x_set_bits(wm831x
,
477 dcdc
->base
+ WM831X_DCDC_DVS_CONTROL
,
478 WM831X_DC1_DVS_VSEL_MASK
, dcdc
->on_vsel
);
480 dcdc
->dvs_vsel
= dcdc
->on_vsel
;
482 dev_warn(wm831x
->dev
, "Failed to set DVS_VSEL: %d\n",
486 ret
= wm831x_set_bits(wm831x
, dcdc
->base
+ WM831X_DCDC_DVS_CONTROL
,
487 WM831X_DC1_DVS_SRC_MASK
, ctrl
);
489 dev_err(wm831x
->dev
, "Failed to set %s DVS source: %d\n",
494 static __devinit
int wm831x_buckv_probe(struct platform_device
*pdev
)
496 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
497 struct wm831x_pdata
*pdata
= wm831x
->dev
->platform_data
;
499 struct wm831x_dcdc
*dcdc
;
500 struct resource
*res
;
503 if (pdata
&& pdata
->wm831x_num
)
504 id
= (pdata
->wm831x_num
* 10) + 1;
509 dev_dbg(&pdev
->dev
, "Probing DCDC%d\n", id
+ 1);
511 if (pdata
== NULL
|| pdata
->dcdc
[id
] == NULL
)
514 dcdc
= devm_kzalloc(&pdev
->dev
, sizeof(struct wm831x_dcdc
),
517 dev_err(&pdev
->dev
, "Unable to allocate private data\n");
521 dcdc
->wm831x
= wm831x
;
523 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
525 dev_err(&pdev
->dev
, "No I/O resource\n");
529 dcdc
->base
= res
->start
;
531 snprintf(dcdc
->name
, sizeof(dcdc
->name
), "DCDC%d", id
+ 1);
532 dcdc
->desc
.name
= dcdc
->name
;
534 dcdc
->desc
.type
= REGULATOR_VOLTAGE
;
535 dcdc
->desc
.n_voltages
= WM831X_BUCKV_MAX_SELECTOR
+ 1;
536 dcdc
->desc
.ops
= &wm831x_buckv_ops
;
537 dcdc
->desc
.owner
= THIS_MODULE
;
539 ret
= wm831x_reg_read(wm831x
, dcdc
->base
+ WM831X_DCDC_ON_CONFIG
);
541 dev_err(wm831x
->dev
, "Failed to read ON VSEL: %d\n", ret
);
544 dcdc
->on_vsel
= ret
& WM831X_DC1_ON_VSEL_MASK
;
546 ret
= wm831x_reg_read(wm831x
, dcdc
->base
+ WM831X_DCDC_DVS_CONTROL
);
548 dev_err(wm831x
->dev
, "Failed to read DVS VSEL: %d\n", ret
);
551 dcdc
->dvs_vsel
= ret
& WM831X_DC1_DVS_VSEL_MASK
;
554 wm831x_buckv_dvs_init(dcdc
, pdata
->dcdc
[id
]->driver_data
);
556 dcdc
->regulator
= regulator_register(&dcdc
->desc
, &pdev
->dev
,
557 pdata
->dcdc
[id
], dcdc
, NULL
);
558 if (IS_ERR(dcdc
->regulator
)) {
559 ret
= PTR_ERR(dcdc
->regulator
);
560 dev_err(wm831x
->dev
, "Failed to register DCDC%d: %d\n",
565 irq
= platform_get_irq_byname(pdev
, "UV");
566 ret
= request_threaded_irq(irq
, NULL
, wm831x_dcdc_uv_irq
,
567 IRQF_TRIGGER_RISING
, dcdc
->name
, dcdc
);
569 dev_err(&pdev
->dev
, "Failed to request UV IRQ %d: %d\n",
574 irq
= platform_get_irq_byname(pdev
, "HC");
575 ret
= request_threaded_irq(irq
, NULL
, wm831x_dcdc_oc_irq
,
576 IRQF_TRIGGER_RISING
, dcdc
->name
, dcdc
);
578 dev_err(&pdev
->dev
, "Failed to request HC IRQ %d: %d\n",
583 platform_set_drvdata(pdev
, dcdc
);
588 free_irq(platform_get_irq_byname(pdev
, "UV"), dcdc
);
590 regulator_unregister(dcdc
->regulator
);
593 gpio_free(dcdc
->dvs_gpio
);
597 static __devexit
int wm831x_buckv_remove(struct platform_device
*pdev
)
599 struct wm831x_dcdc
*dcdc
= platform_get_drvdata(pdev
);
601 platform_set_drvdata(pdev
, NULL
);
603 free_irq(platform_get_irq_byname(pdev
, "HC"), dcdc
);
604 free_irq(platform_get_irq_byname(pdev
, "UV"), dcdc
);
605 regulator_unregister(dcdc
->regulator
);
607 gpio_free(dcdc
->dvs_gpio
);
612 static struct platform_driver wm831x_buckv_driver
= {
613 .probe
= wm831x_buckv_probe
,
614 .remove
= __devexit_p(wm831x_buckv_remove
),
616 .name
= "wm831x-buckv",
617 .owner
= THIS_MODULE
,
625 static int wm831x_buckp_list_voltage(struct regulator_dev
*rdev
,
628 if (selector
<= WM831X_BUCKP_MAX_SELECTOR
)
629 return 850000 + (selector
* 25000);
634 static int wm831x_buckp_set_voltage_int(struct regulator_dev
*rdev
, int reg
,
635 int min_uV
, int max_uV
, int *selector
)
637 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
638 struct wm831x
*wm831x
= dcdc
->wm831x
;
641 if (min_uV
<= 34000000)
642 vsel
= (min_uV
- 850000) / 25000;
646 if (wm831x_buckp_list_voltage(rdev
, vsel
) > max_uV
)
651 return wm831x_set_bits(wm831x
, reg
, WM831X_DC3_ON_VSEL_MASK
, vsel
);
654 static int wm831x_buckp_set_voltage(struct regulator_dev
*rdev
,
655 int min_uV
, int max_uV
,
658 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
659 u16 reg
= dcdc
->base
+ WM831X_DCDC_ON_CONFIG
;
661 return wm831x_buckp_set_voltage_int(rdev
, reg
, min_uV
, max_uV
,
665 static int wm831x_buckp_set_suspend_voltage(struct regulator_dev
*rdev
,
668 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
669 u16 reg
= dcdc
->base
+ WM831X_DCDC_SLEEP_CONTROL
;
672 return wm831x_buckp_set_voltage_int(rdev
, reg
, uV
, uV
, &selector
);
675 static int wm831x_buckp_get_voltage_sel(struct regulator_dev
*rdev
)
677 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
678 struct wm831x
*wm831x
= dcdc
->wm831x
;
679 u16 reg
= dcdc
->base
+ WM831X_DCDC_ON_CONFIG
;
682 val
= wm831x_reg_read(wm831x
, reg
);
686 return val
& WM831X_DC3_ON_VSEL_MASK
;
689 static struct regulator_ops wm831x_buckp_ops
= {
690 .set_voltage
= wm831x_buckp_set_voltage
,
691 .get_voltage_sel
= wm831x_buckp_get_voltage_sel
,
692 .list_voltage
= wm831x_buckp_list_voltage
,
693 .set_suspend_voltage
= wm831x_buckp_set_suspend_voltage
,
695 .is_enabled
= wm831x_dcdc_is_enabled
,
696 .enable
= wm831x_dcdc_enable
,
697 .disable
= wm831x_dcdc_disable
,
698 .get_status
= wm831x_dcdc_get_status
,
699 .get_mode
= wm831x_dcdc_get_mode
,
700 .set_mode
= wm831x_dcdc_set_mode
,
701 .set_suspend_mode
= wm831x_dcdc_set_suspend_mode
,
704 static __devinit
int wm831x_buckp_probe(struct platform_device
*pdev
)
706 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
707 struct wm831x_pdata
*pdata
= wm831x
->dev
->platform_data
;
709 struct wm831x_dcdc
*dcdc
;
710 struct resource
*res
;
713 if (pdata
&& pdata
->wm831x_num
)
714 id
= (pdata
->wm831x_num
* 10) + 1;
719 dev_dbg(&pdev
->dev
, "Probing DCDC%d\n", id
+ 1);
721 if (pdata
== NULL
|| pdata
->dcdc
[id
] == NULL
)
724 dcdc
= devm_kzalloc(&pdev
->dev
, sizeof(struct wm831x_dcdc
),
727 dev_err(&pdev
->dev
, "Unable to allocate private data\n");
731 dcdc
->wm831x
= wm831x
;
733 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
735 dev_err(&pdev
->dev
, "No I/O resource\n");
739 dcdc
->base
= res
->start
;
741 snprintf(dcdc
->name
, sizeof(dcdc
->name
), "DCDC%d", id
+ 1);
742 dcdc
->desc
.name
= dcdc
->name
;
744 dcdc
->desc
.type
= REGULATOR_VOLTAGE
;
745 dcdc
->desc
.n_voltages
= WM831X_BUCKP_MAX_SELECTOR
+ 1;
746 dcdc
->desc
.ops
= &wm831x_buckp_ops
;
747 dcdc
->desc
.owner
= THIS_MODULE
;
749 dcdc
->regulator
= regulator_register(&dcdc
->desc
, &pdev
->dev
,
750 pdata
->dcdc
[id
], dcdc
, NULL
);
751 if (IS_ERR(dcdc
->regulator
)) {
752 ret
= PTR_ERR(dcdc
->regulator
);
753 dev_err(wm831x
->dev
, "Failed to register DCDC%d: %d\n",
758 irq
= platform_get_irq_byname(pdev
, "UV");
759 ret
= request_threaded_irq(irq
, NULL
, wm831x_dcdc_uv_irq
,
760 IRQF_TRIGGER_RISING
, dcdc
->name
, dcdc
);
762 dev_err(&pdev
->dev
, "Failed to request UV IRQ %d: %d\n",
767 platform_set_drvdata(pdev
, dcdc
);
772 regulator_unregister(dcdc
->regulator
);
777 static __devexit
int wm831x_buckp_remove(struct platform_device
*pdev
)
779 struct wm831x_dcdc
*dcdc
= platform_get_drvdata(pdev
);
781 platform_set_drvdata(pdev
, NULL
);
783 free_irq(platform_get_irq_byname(pdev
, "UV"), dcdc
);
784 regulator_unregister(dcdc
->regulator
);
789 static struct platform_driver wm831x_buckp_driver
= {
790 .probe
= wm831x_buckp_probe
,
791 .remove
= __devexit_p(wm831x_buckp_remove
),
793 .name
= "wm831x-buckp",
794 .owner
= THIS_MODULE
,
799 * DCDC boost convertors
802 static int wm831x_boostp_get_status(struct regulator_dev
*rdev
)
804 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
805 struct wm831x
*wm831x
= dcdc
->wm831x
;
808 /* First, check for errors */
809 ret
= wm831x_reg_read(wm831x
, WM831X_DCDC_UV_STATUS
);
813 if (ret
& (1 << rdev_get_id(rdev
))) {
814 dev_dbg(wm831x
->dev
, "DCDC%d under voltage\n",
815 rdev_get_id(rdev
) + 1);
816 return REGULATOR_STATUS_ERROR
;
819 /* Is the regulator on? */
820 ret
= wm831x_reg_read(wm831x
, WM831X_DCDC_STATUS
);
823 if (ret
& (1 << rdev_get_id(rdev
)))
824 return REGULATOR_STATUS_ON
;
826 return REGULATOR_STATUS_OFF
;
829 static struct regulator_ops wm831x_boostp_ops
= {
830 .get_status
= wm831x_boostp_get_status
,
832 .is_enabled
= wm831x_dcdc_is_enabled
,
833 .enable
= wm831x_dcdc_enable
,
834 .disable
= wm831x_dcdc_disable
,
837 static __devinit
int wm831x_boostp_probe(struct platform_device
*pdev
)
839 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
840 struct wm831x_pdata
*pdata
= wm831x
->dev
->platform_data
;
841 int id
= pdev
->id
% ARRAY_SIZE(pdata
->dcdc
);
842 struct wm831x_dcdc
*dcdc
;
843 struct resource
*res
;
846 dev_dbg(&pdev
->dev
, "Probing DCDC%d\n", id
+ 1);
848 if (pdata
== NULL
|| pdata
->dcdc
[id
] == NULL
)
851 dcdc
= kzalloc(sizeof(struct wm831x_dcdc
), GFP_KERNEL
);
853 dev_err(&pdev
->dev
, "Unable to allocate private data\n");
857 dcdc
->wm831x
= wm831x
;
859 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
861 dev_err(&pdev
->dev
, "No I/O resource\n");
865 dcdc
->base
= res
->start
;
867 snprintf(dcdc
->name
, sizeof(dcdc
->name
), "DCDC%d", id
+ 1);
868 dcdc
->desc
.name
= dcdc
->name
;
870 dcdc
->desc
.type
= REGULATOR_VOLTAGE
;
871 dcdc
->desc
.ops
= &wm831x_boostp_ops
;
872 dcdc
->desc
.owner
= THIS_MODULE
;
874 dcdc
->regulator
= regulator_register(&dcdc
->desc
, &pdev
->dev
,
875 pdata
->dcdc
[id
], dcdc
, NULL
);
876 if (IS_ERR(dcdc
->regulator
)) {
877 ret
= PTR_ERR(dcdc
->regulator
);
878 dev_err(wm831x
->dev
, "Failed to register DCDC%d: %d\n",
883 irq
= platform_get_irq_byname(pdev
, "UV");
884 ret
= request_threaded_irq(irq
, NULL
, wm831x_dcdc_uv_irq
,
885 IRQF_TRIGGER_RISING
, dcdc
->name
,
888 dev_err(&pdev
->dev
, "Failed to request UV IRQ %d: %d\n",
893 platform_set_drvdata(pdev
, dcdc
);
898 regulator_unregister(dcdc
->regulator
);
904 static __devexit
int wm831x_boostp_remove(struct platform_device
*pdev
)
906 struct wm831x_dcdc
*dcdc
= platform_get_drvdata(pdev
);
908 platform_set_drvdata(pdev
, NULL
);
910 free_irq(platform_get_irq_byname(pdev
, "UV"), dcdc
);
911 regulator_unregister(dcdc
->regulator
);
917 static struct platform_driver wm831x_boostp_driver
= {
918 .probe
= wm831x_boostp_probe
,
919 .remove
= __devexit_p(wm831x_boostp_remove
),
921 .name
= "wm831x-boostp",
922 .owner
= THIS_MODULE
,
927 * External Power Enable
929 * These aren't actually DCDCs but look like them in hardware so share
933 #define WM831X_EPE_BASE 6
935 static struct regulator_ops wm831x_epe_ops
= {
936 .is_enabled
= wm831x_dcdc_is_enabled
,
937 .enable
= wm831x_dcdc_enable
,
938 .disable
= wm831x_dcdc_disable
,
939 .get_status
= wm831x_dcdc_get_status
,
942 static __devinit
int wm831x_epe_probe(struct platform_device
*pdev
)
944 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
945 struct wm831x_pdata
*pdata
= wm831x
->dev
->platform_data
;
946 int id
= pdev
->id
% ARRAY_SIZE(pdata
->epe
);
947 struct wm831x_dcdc
*dcdc
;
950 dev_dbg(&pdev
->dev
, "Probing EPE%d\n", id
+ 1);
952 if (pdata
== NULL
|| pdata
->epe
[id
] == NULL
)
955 dcdc
= kzalloc(sizeof(struct wm831x_dcdc
), GFP_KERNEL
);
957 dev_err(&pdev
->dev
, "Unable to allocate private data\n");
961 dcdc
->wm831x
= wm831x
;
963 /* For current parts this is correct; probably need to revisit
966 snprintf(dcdc
->name
, sizeof(dcdc
->name
), "EPE%d", id
+ 1);
967 dcdc
->desc
.name
= dcdc
->name
;
968 dcdc
->desc
.id
= id
+ WM831X_EPE_BASE
; /* Offset in DCDC registers */
969 dcdc
->desc
.ops
= &wm831x_epe_ops
;
970 dcdc
->desc
.type
= REGULATOR_VOLTAGE
;
971 dcdc
->desc
.owner
= THIS_MODULE
;
973 dcdc
->regulator
= regulator_register(&dcdc
->desc
, &pdev
->dev
,
974 pdata
->epe
[id
], dcdc
, NULL
);
975 if (IS_ERR(dcdc
->regulator
)) {
976 ret
= PTR_ERR(dcdc
->regulator
);
977 dev_err(wm831x
->dev
, "Failed to register EPE%d: %d\n",
982 platform_set_drvdata(pdev
, dcdc
);
991 static __devexit
int wm831x_epe_remove(struct platform_device
*pdev
)
993 struct wm831x_dcdc
*dcdc
= platform_get_drvdata(pdev
);
995 platform_set_drvdata(pdev
, NULL
);
997 regulator_unregister(dcdc
->regulator
);
1003 static struct platform_driver wm831x_epe_driver
= {
1004 .probe
= wm831x_epe_probe
,
1005 .remove
= __devexit_p(wm831x_epe_remove
),
1007 .name
= "wm831x-epe",
1008 .owner
= THIS_MODULE
,
1012 static int __init
wm831x_dcdc_init(void)
1015 ret
= platform_driver_register(&wm831x_buckv_driver
);
1017 pr_err("Failed to register WM831x BUCKV driver: %d\n", ret
);
1019 ret
= platform_driver_register(&wm831x_buckp_driver
);
1021 pr_err("Failed to register WM831x BUCKP driver: %d\n", ret
);
1023 ret
= platform_driver_register(&wm831x_boostp_driver
);
1025 pr_err("Failed to register WM831x BOOST driver: %d\n", ret
);
1027 ret
= platform_driver_register(&wm831x_epe_driver
);
1029 pr_err("Failed to register WM831x EPE driver: %d\n", ret
);
1033 subsys_initcall(wm831x_dcdc_init
);
1035 static void __exit
wm831x_dcdc_exit(void)
1037 platform_driver_unregister(&wm831x_epe_driver
);
1038 platform_driver_unregister(&wm831x_boostp_driver
);
1039 platform_driver_unregister(&wm831x_buckp_driver
);
1040 platform_driver_unregister(&wm831x_buckv_driver
);
1042 module_exit(wm831x_dcdc_exit
);
1044 /* Module information */
1045 MODULE_AUTHOR("Mark Brown");
1046 MODULE_DESCRIPTION("WM831x DC-DC convertor driver");
1047 MODULE_LICENSE("GPL");
1048 MODULE_ALIAS("platform:wm831x-buckv");
1049 MODULE_ALIAS("platform:wm831x-buckp");
1050 MODULE_ALIAS("platform:wm831x-epe");