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>
25 #include <linux/mfd/wm831x/core.h>
26 #include <linux/mfd/wm831x/regulator.h>
27 #include <linux/mfd/wm831x/pdata.h>
29 #define WM831X_BUCKV_MAX_SELECTOR 0x68
30 #define WM831X_BUCKP_MAX_SELECTOR 0x66
32 #define WM831X_DCDC_MODE_FAST 0
33 #define WM831X_DCDC_MODE_NORMAL 1
34 #define WM831X_DCDC_MODE_IDLE 2
35 #define WM831X_DCDC_MODE_STANDBY 3
37 #define WM831X_DCDC_MAX_NAME 6
39 /* Register offsets in control block */
40 #define WM831X_DCDC_CONTROL_1 0
41 #define WM831X_DCDC_CONTROL_2 1
42 #define WM831X_DCDC_ON_CONFIG 2
43 #define WM831X_DCDC_SLEEP_CONTROL 3
44 #define WM831X_DCDC_DVS_CONTROL 4
51 char name
[WM831X_DCDC_MAX_NAME
];
52 struct regulator_desc desc
;
54 struct wm831x
*wm831x
;
55 struct regulator_dev
*regulator
;
62 static int wm831x_dcdc_is_enabled(struct regulator_dev
*rdev
)
64 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
65 struct wm831x
*wm831x
= dcdc
->wm831x
;
66 int mask
= 1 << rdev_get_id(rdev
);
69 reg
= wm831x_reg_read(wm831x
, WM831X_DCDC_ENABLE
);
79 static int wm831x_dcdc_enable(struct regulator_dev
*rdev
)
81 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
82 struct wm831x
*wm831x
= dcdc
->wm831x
;
83 int mask
= 1 << rdev_get_id(rdev
);
85 return wm831x_set_bits(wm831x
, WM831X_DCDC_ENABLE
, mask
, mask
);
88 static int wm831x_dcdc_disable(struct regulator_dev
*rdev
)
90 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
91 struct wm831x
*wm831x
= dcdc
->wm831x
;
92 int mask
= 1 << rdev_get_id(rdev
);
94 return wm831x_set_bits(wm831x
, WM831X_DCDC_ENABLE
, mask
, 0);
97 static unsigned int wm831x_dcdc_get_mode(struct regulator_dev
*rdev
)
100 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
101 struct wm831x
*wm831x
= dcdc
->wm831x
;
102 u16 reg
= dcdc
->base
+ WM831X_DCDC_ON_CONFIG
;
105 val
= wm831x_reg_read(wm831x
, reg
);
109 val
= (val
& WM831X_DC1_ON_MODE_MASK
) >> WM831X_DC1_ON_MODE_SHIFT
;
112 case WM831X_DCDC_MODE_FAST
:
113 return REGULATOR_MODE_FAST
;
114 case WM831X_DCDC_MODE_NORMAL
:
115 return REGULATOR_MODE_NORMAL
;
116 case WM831X_DCDC_MODE_STANDBY
:
117 return REGULATOR_MODE_STANDBY
;
118 case WM831X_DCDC_MODE_IDLE
:
119 return REGULATOR_MODE_IDLE
;
125 static int wm831x_dcdc_set_mode_int(struct wm831x
*wm831x
, int reg
,
131 case REGULATOR_MODE_FAST
:
132 val
= WM831X_DCDC_MODE_FAST
;
134 case REGULATOR_MODE_NORMAL
:
135 val
= WM831X_DCDC_MODE_NORMAL
;
137 case REGULATOR_MODE_STANDBY
:
138 val
= WM831X_DCDC_MODE_STANDBY
;
140 case REGULATOR_MODE_IDLE
:
141 val
= WM831X_DCDC_MODE_IDLE
;
147 return wm831x_set_bits(wm831x
, reg
, WM831X_DC1_ON_MODE_MASK
,
148 val
<< WM831X_DC1_ON_MODE_SHIFT
);
151 static int wm831x_dcdc_set_mode(struct regulator_dev
*rdev
, unsigned int mode
)
153 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
154 struct wm831x
*wm831x
= dcdc
->wm831x
;
155 u16 reg
= dcdc
->base
+ WM831X_DCDC_ON_CONFIG
;
157 return wm831x_dcdc_set_mode_int(wm831x
, reg
, mode
);
160 static int wm831x_dcdc_set_suspend_mode(struct regulator_dev
*rdev
,
163 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
164 struct wm831x
*wm831x
= dcdc
->wm831x
;
165 u16 reg
= dcdc
->base
+ WM831X_DCDC_SLEEP_CONTROL
;
167 return wm831x_dcdc_set_mode_int(wm831x
, reg
, mode
);
170 static int wm831x_dcdc_get_status(struct regulator_dev
*rdev
)
172 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
173 struct wm831x
*wm831x
= dcdc
->wm831x
;
176 /* First, check for errors */
177 ret
= wm831x_reg_read(wm831x
, WM831X_DCDC_UV_STATUS
);
181 if (ret
& (1 << rdev_get_id(rdev
))) {
182 dev_dbg(wm831x
->dev
, "DCDC%d under voltage\n",
183 rdev_get_id(rdev
) + 1);
184 return REGULATOR_STATUS_ERROR
;
187 /* DCDC1 and DCDC2 can additionally detect high voltage/current */
188 if (rdev_get_id(rdev
) < 2) {
189 if (ret
& (WM831X_DC1_OV_STS
<< rdev_get_id(rdev
))) {
190 dev_dbg(wm831x
->dev
, "DCDC%d over voltage\n",
191 rdev_get_id(rdev
) + 1);
192 return REGULATOR_STATUS_ERROR
;
195 if (ret
& (WM831X_DC1_HC_STS
<< rdev_get_id(rdev
))) {
196 dev_dbg(wm831x
->dev
, "DCDC%d over current\n",
197 rdev_get_id(rdev
) + 1);
198 return REGULATOR_STATUS_ERROR
;
202 /* Is the regulator on? */
203 ret
= wm831x_reg_read(wm831x
, WM831X_DCDC_STATUS
);
206 if (!(ret
& (1 << rdev_get_id(rdev
))))
207 return REGULATOR_STATUS_OFF
;
209 /* TODO: When we handle hardware control modes so we can report the
211 return REGULATOR_STATUS_ON
;
214 static irqreturn_t
wm831x_dcdc_uv_irq(int irq
, void *data
)
216 struct wm831x_dcdc
*dcdc
= data
;
218 regulator_notifier_call_chain(dcdc
->regulator
,
219 REGULATOR_EVENT_UNDER_VOLTAGE
,
225 static irqreturn_t
wm831x_dcdc_oc_irq(int irq
, void *data
)
227 struct wm831x_dcdc
*dcdc
= data
;
229 regulator_notifier_call_chain(dcdc
->regulator
,
230 REGULATOR_EVENT_OVER_CURRENT
,
240 static int wm831x_buckv_list_voltage(struct regulator_dev
*rdev
,
245 if (selector
<= WM831X_BUCKV_MAX_SELECTOR
)
246 return 600000 + ((selector
- 0x8) * 12500);
250 static int wm831x_buckv_select_min_voltage(struct regulator_dev
*rdev
,
251 int min_uV
, int max_uV
)
257 else if (min_uV
<= 1800000)
258 vsel
= ((min_uV
- 600000) / 12500) + 8;
262 if (wm831x_buckv_list_voltage(rdev
, vsel
) > max_uV
)
268 static int wm831x_buckv_select_max_voltage(struct regulator_dev
*rdev
,
269 int min_uV
, int max_uV
)
273 if (max_uV
< 600000 || max_uV
> 1800000)
276 vsel
= ((max_uV
- 600000) / 12500) + 8;
278 if (wm831x_buckv_list_voltage(rdev
, vsel
) < min_uV
||
279 wm831x_buckv_list_voltage(rdev
, vsel
) < max_uV
)
285 static int wm831x_buckv_set_dvs(struct regulator_dev
*rdev
, int state
)
287 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
289 if (state
== dcdc
->dvs_gpio_state
)
292 dcdc
->dvs_gpio_state
= state
;
293 gpio_set_value(dcdc
->dvs_gpio
, state
);
295 /* Should wait for DVS state change to be asserted if we have
296 * a GPIO for it, for now assume the device is configured
297 * for the fastest possible transition.
303 static int wm831x_buckv_set_voltage(struct regulator_dev
*rdev
,
304 int min_uV
, int max_uV
)
306 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
307 struct wm831x
*wm831x
= dcdc
->wm831x
;
308 int on_reg
= dcdc
->base
+ WM831X_DCDC_ON_CONFIG
;
309 int dvs_reg
= dcdc
->base
+ WM831X_DCDC_DVS_CONTROL
;
312 vsel
= wm831x_buckv_select_min_voltage(rdev
, min_uV
, max_uV
);
316 /* If this value is already set then do a GPIO update if we can */
317 if (dcdc
->dvs_gpio
&& dcdc
->on_vsel
== vsel
)
318 return wm831x_buckv_set_dvs(rdev
, 0);
320 if (dcdc
->dvs_gpio
&& dcdc
->dvs_vsel
== vsel
)
321 return wm831x_buckv_set_dvs(rdev
, 1);
323 /* Always set the ON status to the minimum voltage */
324 ret
= wm831x_set_bits(wm831x
, on_reg
, WM831X_DC1_ON_VSEL_MASK
, vsel
);
327 dcdc
->on_vsel
= vsel
;
332 /* Kick the voltage transition now */
333 ret
= wm831x_buckv_set_dvs(rdev
, 0);
337 /* Set the high voltage as the DVS voltage. This is optimised
338 * for CPUfreq usage, most processors will keep the maximum
339 * voltage constant and lower the minimum with the frequency. */
340 vsel
= wm831x_buckv_select_max_voltage(rdev
, min_uV
, max_uV
);
342 /* This should never happen - at worst the same vsel
343 * should be chosen */
348 /* Don't bother if it's the same VSEL we're already using */
349 if (vsel
== dcdc
->on_vsel
)
352 ret
= wm831x_set_bits(wm831x
, dvs_reg
, WM831X_DC1_DVS_VSEL_MASK
, vsel
);
354 dcdc
->dvs_vsel
= vsel
;
356 dev_warn(wm831x
->dev
, "Failed to set DCDC DVS VSEL: %d\n",
362 static int wm831x_buckv_set_suspend_voltage(struct regulator_dev
*rdev
,
365 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
366 struct wm831x
*wm831x
= dcdc
->wm831x
;
367 u16 reg
= dcdc
->base
+ WM831X_DCDC_SLEEP_CONTROL
;
370 vsel
= wm831x_buckv_select_min_voltage(rdev
, uV
, uV
);
374 return wm831x_set_bits(wm831x
, reg
, WM831X_DC1_SLP_VSEL_MASK
, vsel
);
377 static int wm831x_buckv_get_voltage(struct regulator_dev
*rdev
)
379 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
381 if (dcdc
->dvs_gpio
&& dcdc
->dvs_gpio_state
)
382 return wm831x_buckv_list_voltage(rdev
, dcdc
->dvs_vsel
);
384 return wm831x_buckv_list_voltage(rdev
, dcdc
->on_vsel
);
387 /* Current limit options */
388 static u16 wm831x_dcdc_ilim
[] = {
389 125, 250, 375, 500, 625, 750, 875, 1000
392 static int wm831x_buckv_set_current_limit(struct regulator_dev
*rdev
,
393 int min_uA
, int max_uA
)
395 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
396 struct wm831x
*wm831x
= dcdc
->wm831x
;
397 u16 reg
= dcdc
->base
+ WM831X_DCDC_CONTROL_2
;
400 for (i
= 0; i
< ARRAY_SIZE(wm831x_dcdc_ilim
); i
++) {
401 if (max_uA
<= wm831x_dcdc_ilim
[i
])
404 if (i
== ARRAY_SIZE(wm831x_dcdc_ilim
))
407 return wm831x_set_bits(wm831x
, reg
, WM831X_DC1_HC_THR_MASK
, i
);
410 static int wm831x_buckv_get_current_limit(struct regulator_dev
*rdev
)
412 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
413 struct wm831x
*wm831x
= dcdc
->wm831x
;
414 u16 reg
= dcdc
->base
+ WM831X_DCDC_CONTROL_2
;
417 val
= wm831x_reg_read(wm831x
, reg
);
421 return wm831x_dcdc_ilim
[val
& WM831X_DC1_HC_THR_MASK
];
424 static struct regulator_ops wm831x_buckv_ops
= {
425 .set_voltage
= wm831x_buckv_set_voltage
,
426 .get_voltage
= wm831x_buckv_get_voltage
,
427 .list_voltage
= wm831x_buckv_list_voltage
,
428 .set_suspend_voltage
= wm831x_buckv_set_suspend_voltage
,
429 .set_current_limit
= wm831x_buckv_set_current_limit
,
430 .get_current_limit
= wm831x_buckv_get_current_limit
,
432 .is_enabled
= wm831x_dcdc_is_enabled
,
433 .enable
= wm831x_dcdc_enable
,
434 .disable
= wm831x_dcdc_disable
,
435 .get_status
= wm831x_dcdc_get_status
,
436 .get_mode
= wm831x_dcdc_get_mode
,
437 .set_mode
= wm831x_dcdc_set_mode
,
438 .set_suspend_mode
= wm831x_dcdc_set_suspend_mode
,
442 * Set up DVS control. We just log errors since we can still run
443 * (with reduced performance) if we fail.
445 static __devinit
void wm831x_buckv_dvs_init(struct wm831x_dcdc
*dcdc
,
446 struct wm831x_buckv_pdata
*pdata
)
448 struct wm831x
*wm831x
= dcdc
->wm831x
;
452 if (!pdata
|| !pdata
->dvs_gpio
)
455 switch (pdata
->dvs_control_src
) {
457 ctrl
= 2 << WM831X_DC1_DVS_SRC_SHIFT
;
460 ctrl
= 3 << WM831X_DC1_DVS_SRC_SHIFT
;
463 dev_err(wm831x
->dev
, "Invalid DVS control source %d for %s\n",
464 pdata
->dvs_control_src
, dcdc
->name
);
468 ret
= wm831x_set_bits(wm831x
, dcdc
->base
+ WM831X_DCDC_DVS_CONTROL
,
469 WM831X_DC1_DVS_SRC_MASK
, ctrl
);
471 dev_err(wm831x
->dev
, "Failed to set %s DVS source: %d\n",
476 ret
= gpio_request(pdata
->dvs_gpio
, "DCDC DVS");
478 dev_err(wm831x
->dev
, "Failed to get %s DVS GPIO: %d\n",
483 /* gpiolib won't let us read the GPIO status so pick the higher
484 * of the two existing voltages so we take it as platform data.
486 dcdc
->dvs_gpio_state
= pdata
->dvs_init_state
;
488 ret
= gpio_direction_output(pdata
->dvs_gpio
, dcdc
->dvs_gpio_state
);
490 dev_err(wm831x
->dev
, "Failed to enable %s DVS GPIO: %d\n",
492 gpio_free(pdata
->dvs_gpio
);
496 dcdc
->dvs_gpio
= pdata
->dvs_gpio
;
499 static __devinit
int wm831x_buckv_probe(struct platform_device
*pdev
)
501 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
502 struct wm831x_pdata
*pdata
= wm831x
->dev
->platform_data
;
503 int id
= pdev
->id
% ARRAY_SIZE(pdata
->dcdc
);
504 struct wm831x_dcdc
*dcdc
;
505 struct resource
*res
;
508 dev_dbg(&pdev
->dev
, "Probing DCDC%d\n", id
+ 1);
510 if (pdata
== NULL
|| pdata
->dcdc
[id
] == NULL
)
513 dcdc
= kzalloc(sizeof(struct wm831x_dcdc
), GFP_KERNEL
);
515 dev_err(&pdev
->dev
, "Unable to allocate private data\n");
519 dcdc
->wm831x
= wm831x
;
521 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
523 dev_err(&pdev
->dev
, "No I/O resource\n");
527 dcdc
->base
= res
->start
;
529 snprintf(dcdc
->name
, sizeof(dcdc
->name
), "DCDC%d", id
+ 1);
530 dcdc
->desc
.name
= dcdc
->name
;
532 dcdc
->desc
.type
= REGULATOR_VOLTAGE
;
533 dcdc
->desc
.n_voltages
= WM831X_BUCKV_MAX_SELECTOR
+ 1;
534 dcdc
->desc
.ops
= &wm831x_buckv_ops
;
535 dcdc
->desc
.owner
= THIS_MODULE
;
537 ret
= wm831x_reg_read(wm831x
, dcdc
->base
+ WM831X_DCDC_ON_CONFIG
);
539 dev_err(wm831x
->dev
, "Failed to read ON VSEL: %d\n", ret
);
542 dcdc
->on_vsel
= ret
& WM831X_DC1_ON_VSEL_MASK
;
544 ret
= wm831x_reg_read(wm831x
, dcdc
->base
+ WM831X_DCDC_ON_CONFIG
);
546 dev_err(wm831x
->dev
, "Failed to read DVS VSEL: %d\n", ret
);
549 dcdc
->dvs_vsel
= ret
& WM831X_DC1_DVS_VSEL_MASK
;
552 wm831x_buckv_dvs_init(dcdc
, pdata
->dcdc
[id
]->driver_data
);
554 dcdc
->regulator
= regulator_register(&dcdc
->desc
, &pdev
->dev
,
555 pdata
->dcdc
[id
], dcdc
);
556 if (IS_ERR(dcdc
->regulator
)) {
557 ret
= PTR_ERR(dcdc
->regulator
);
558 dev_err(wm831x
->dev
, "Failed to register DCDC%d: %d\n",
563 irq
= platform_get_irq_byname(pdev
, "UV");
564 ret
= wm831x_request_irq(wm831x
, irq
, wm831x_dcdc_uv_irq
,
565 IRQF_TRIGGER_RISING
, dcdc
->name
,
568 dev_err(&pdev
->dev
, "Failed to request UV IRQ %d: %d\n",
573 irq
= platform_get_irq_byname(pdev
, "HC");
574 ret
= wm831x_request_irq(wm831x
, irq
, wm831x_dcdc_oc_irq
,
575 IRQF_TRIGGER_RISING
, dcdc
->name
,
578 dev_err(&pdev
->dev
, "Failed to request HC IRQ %d: %d\n",
583 platform_set_drvdata(pdev
, dcdc
);
588 wm831x_free_irq(wm831x
, platform_get_irq_byname(pdev
, "UV"), dcdc
);
590 regulator_unregister(dcdc
->regulator
);
593 gpio_free(dcdc
->dvs_gpio
);
598 static __devexit
int wm831x_buckv_remove(struct platform_device
*pdev
)
600 struct wm831x_dcdc
*dcdc
= platform_get_drvdata(pdev
);
601 struct wm831x
*wm831x
= dcdc
->wm831x
;
603 platform_set_drvdata(pdev
, NULL
);
605 wm831x_free_irq(wm831x
, platform_get_irq_byname(pdev
, "HC"), dcdc
);
606 wm831x_free_irq(wm831x
, platform_get_irq_byname(pdev
, "UV"), dcdc
);
607 regulator_unregister(dcdc
->regulator
);
609 gpio_free(dcdc
->dvs_gpio
);
615 static struct platform_driver wm831x_buckv_driver
= {
616 .probe
= wm831x_buckv_probe
,
617 .remove
= __devexit_p(wm831x_buckv_remove
),
619 .name
= "wm831x-buckv",
620 .owner
= THIS_MODULE
,
628 static int wm831x_buckp_list_voltage(struct regulator_dev
*rdev
,
631 if (selector
<= WM831X_BUCKP_MAX_SELECTOR
)
632 return 850000 + (selector
* 25000);
637 static int wm831x_buckp_set_voltage_int(struct regulator_dev
*rdev
, int reg
,
638 int min_uV
, int max_uV
)
640 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
641 struct wm831x
*wm831x
= dcdc
->wm831x
;
644 if (min_uV
<= 34000000)
645 vsel
= (min_uV
- 850000) / 25000;
649 if (wm831x_buckp_list_voltage(rdev
, vsel
) > max_uV
)
652 return wm831x_set_bits(wm831x
, reg
, WM831X_DC3_ON_VSEL_MASK
, vsel
);
655 static int wm831x_buckp_set_voltage(struct regulator_dev
*rdev
,
656 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
);
664 static int wm831x_buckp_set_suspend_voltage(struct regulator_dev
*rdev
,
667 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
668 u16 reg
= dcdc
->base
+ WM831X_DCDC_SLEEP_CONTROL
;
670 return wm831x_buckp_set_voltage_int(rdev
, reg
, uV
, uV
);
673 static int wm831x_buckp_get_voltage(struct regulator_dev
*rdev
)
675 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
676 struct wm831x
*wm831x
= dcdc
->wm831x
;
677 u16 reg
= dcdc
->base
+ WM831X_DCDC_ON_CONFIG
;
680 val
= wm831x_reg_read(wm831x
, reg
);
684 return wm831x_buckp_list_voltage(rdev
, val
& WM831X_DC3_ON_VSEL_MASK
);
687 static struct regulator_ops wm831x_buckp_ops
= {
688 .set_voltage
= wm831x_buckp_set_voltage
,
689 .get_voltage
= wm831x_buckp_get_voltage
,
690 .list_voltage
= wm831x_buckp_list_voltage
,
691 .set_suspend_voltage
= wm831x_buckp_set_suspend_voltage
,
693 .is_enabled
= wm831x_dcdc_is_enabled
,
694 .enable
= wm831x_dcdc_enable
,
695 .disable
= wm831x_dcdc_disable
,
696 .get_status
= wm831x_dcdc_get_status
,
697 .get_mode
= wm831x_dcdc_get_mode
,
698 .set_mode
= wm831x_dcdc_set_mode
,
699 .set_suspend_mode
= wm831x_dcdc_set_suspend_mode
,
702 static __devinit
int wm831x_buckp_probe(struct platform_device
*pdev
)
704 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
705 struct wm831x_pdata
*pdata
= wm831x
->dev
->platform_data
;
706 int id
= pdev
->id
% ARRAY_SIZE(pdata
->dcdc
);
707 struct wm831x_dcdc
*dcdc
;
708 struct resource
*res
;
711 dev_dbg(&pdev
->dev
, "Probing DCDC%d\n", id
+ 1);
713 if (pdata
== NULL
|| pdata
->dcdc
[id
] == NULL
)
716 dcdc
= kzalloc(sizeof(struct wm831x_dcdc
), GFP_KERNEL
);
718 dev_err(&pdev
->dev
, "Unable to allocate private data\n");
722 dcdc
->wm831x
= wm831x
;
724 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
726 dev_err(&pdev
->dev
, "No I/O resource\n");
730 dcdc
->base
= res
->start
;
732 snprintf(dcdc
->name
, sizeof(dcdc
->name
), "DCDC%d", id
+ 1);
733 dcdc
->desc
.name
= dcdc
->name
;
735 dcdc
->desc
.type
= REGULATOR_VOLTAGE
;
736 dcdc
->desc
.n_voltages
= WM831X_BUCKP_MAX_SELECTOR
+ 1;
737 dcdc
->desc
.ops
= &wm831x_buckp_ops
;
738 dcdc
->desc
.owner
= THIS_MODULE
;
740 dcdc
->regulator
= regulator_register(&dcdc
->desc
, &pdev
->dev
,
741 pdata
->dcdc
[id
], dcdc
);
742 if (IS_ERR(dcdc
->regulator
)) {
743 ret
= PTR_ERR(dcdc
->regulator
);
744 dev_err(wm831x
->dev
, "Failed to register DCDC%d: %d\n",
749 irq
= platform_get_irq_byname(pdev
, "UV");
750 ret
= wm831x_request_irq(wm831x
, irq
, wm831x_dcdc_uv_irq
,
751 IRQF_TRIGGER_RISING
, dcdc
->name
,
754 dev_err(&pdev
->dev
, "Failed to request UV IRQ %d: %d\n",
759 platform_set_drvdata(pdev
, dcdc
);
764 regulator_unregister(dcdc
->regulator
);
770 static __devexit
int wm831x_buckp_remove(struct platform_device
*pdev
)
772 struct wm831x_dcdc
*dcdc
= platform_get_drvdata(pdev
);
773 struct wm831x
*wm831x
= dcdc
->wm831x
;
775 platform_set_drvdata(pdev
, NULL
);
777 wm831x_free_irq(wm831x
, platform_get_irq_byname(pdev
, "UV"), dcdc
);
778 regulator_unregister(dcdc
->regulator
);
784 static struct platform_driver wm831x_buckp_driver
= {
785 .probe
= wm831x_buckp_probe
,
786 .remove
= __devexit_p(wm831x_buckp_remove
),
788 .name
= "wm831x-buckp",
789 .owner
= THIS_MODULE
,
794 * DCDC boost convertors
797 static int wm831x_boostp_get_status(struct regulator_dev
*rdev
)
799 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
800 struct wm831x
*wm831x
= dcdc
->wm831x
;
803 /* First, check for errors */
804 ret
= wm831x_reg_read(wm831x
, WM831X_DCDC_UV_STATUS
);
808 if (ret
& (1 << rdev_get_id(rdev
))) {
809 dev_dbg(wm831x
->dev
, "DCDC%d under voltage\n",
810 rdev_get_id(rdev
) + 1);
811 return REGULATOR_STATUS_ERROR
;
814 /* Is the regulator on? */
815 ret
= wm831x_reg_read(wm831x
, WM831X_DCDC_STATUS
);
818 if (ret
& (1 << rdev_get_id(rdev
)))
819 return REGULATOR_STATUS_ON
;
821 return REGULATOR_STATUS_OFF
;
824 static struct regulator_ops wm831x_boostp_ops
= {
825 .get_status
= wm831x_boostp_get_status
,
827 .is_enabled
= wm831x_dcdc_is_enabled
,
828 .enable
= wm831x_dcdc_enable
,
829 .disable
= wm831x_dcdc_disable
,
832 static __devinit
int wm831x_boostp_probe(struct platform_device
*pdev
)
834 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
835 struct wm831x_pdata
*pdata
= wm831x
->dev
->platform_data
;
836 int id
= pdev
->id
% ARRAY_SIZE(pdata
->dcdc
);
837 struct wm831x_dcdc
*dcdc
;
838 struct resource
*res
;
841 dev_dbg(&pdev
->dev
, "Probing DCDC%d\n", id
+ 1);
843 if (pdata
== NULL
|| pdata
->dcdc
[id
] == NULL
)
846 dcdc
= kzalloc(sizeof(struct wm831x_dcdc
), GFP_KERNEL
);
848 dev_err(&pdev
->dev
, "Unable to allocate private data\n");
852 dcdc
->wm831x
= wm831x
;
854 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
856 dev_err(&pdev
->dev
, "No I/O resource\n");
860 dcdc
->base
= res
->start
;
862 snprintf(dcdc
->name
, sizeof(dcdc
->name
), "DCDC%d", id
+ 1);
863 dcdc
->desc
.name
= dcdc
->name
;
865 dcdc
->desc
.type
= REGULATOR_VOLTAGE
;
866 dcdc
->desc
.ops
= &wm831x_boostp_ops
;
867 dcdc
->desc
.owner
= THIS_MODULE
;
869 dcdc
->regulator
= regulator_register(&dcdc
->desc
, &pdev
->dev
,
870 pdata
->dcdc
[id
], dcdc
);
871 if (IS_ERR(dcdc
->regulator
)) {
872 ret
= PTR_ERR(dcdc
->regulator
);
873 dev_err(wm831x
->dev
, "Failed to register DCDC%d: %d\n",
878 irq
= platform_get_irq_byname(pdev
, "UV");
879 ret
= wm831x_request_irq(wm831x
, irq
, wm831x_dcdc_uv_irq
,
880 IRQF_TRIGGER_RISING
, dcdc
->name
,
883 dev_err(&pdev
->dev
, "Failed to request UV IRQ %d: %d\n",
888 platform_set_drvdata(pdev
, dcdc
);
893 regulator_unregister(dcdc
->regulator
);
899 static __devexit
int wm831x_boostp_remove(struct platform_device
*pdev
)
901 struct wm831x_dcdc
*dcdc
= platform_get_drvdata(pdev
);
902 struct wm831x
*wm831x
= dcdc
->wm831x
;
904 platform_set_drvdata(pdev
, NULL
);
906 wm831x_free_irq(wm831x
, platform_get_irq_byname(pdev
, "UV"), dcdc
);
907 regulator_unregister(dcdc
->regulator
);
913 static struct platform_driver wm831x_boostp_driver
= {
914 .probe
= wm831x_boostp_probe
,
915 .remove
= __devexit_p(wm831x_boostp_remove
),
917 .name
= "wm831x-boostp",
918 .owner
= THIS_MODULE
,
923 * External Power Enable
925 * These aren't actually DCDCs but look like them in hardware so share
929 #define WM831X_EPE_BASE 6
931 static struct regulator_ops wm831x_epe_ops
= {
932 .is_enabled
= wm831x_dcdc_is_enabled
,
933 .enable
= wm831x_dcdc_enable
,
934 .disable
= wm831x_dcdc_disable
,
935 .get_status
= wm831x_dcdc_get_status
,
938 static __devinit
int wm831x_epe_probe(struct platform_device
*pdev
)
940 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
941 struct wm831x_pdata
*pdata
= wm831x
->dev
->platform_data
;
942 int id
= pdev
->id
% ARRAY_SIZE(pdata
->epe
);
943 struct wm831x_dcdc
*dcdc
;
946 dev_dbg(&pdev
->dev
, "Probing EPE%d\n", id
+ 1);
948 if (pdata
== NULL
|| pdata
->epe
[id
] == NULL
)
951 dcdc
= kzalloc(sizeof(struct wm831x_dcdc
), GFP_KERNEL
);
953 dev_err(&pdev
->dev
, "Unable to allocate private data\n");
957 dcdc
->wm831x
= wm831x
;
959 /* For current parts this is correct; probably need to revisit
962 snprintf(dcdc
->name
, sizeof(dcdc
->name
), "EPE%d", id
+ 1);
963 dcdc
->desc
.name
= dcdc
->name
;
964 dcdc
->desc
.id
= id
+ WM831X_EPE_BASE
; /* Offset in DCDC registers */
965 dcdc
->desc
.ops
= &wm831x_epe_ops
;
966 dcdc
->desc
.type
= REGULATOR_VOLTAGE
;
967 dcdc
->desc
.owner
= THIS_MODULE
;
969 dcdc
->regulator
= regulator_register(&dcdc
->desc
, &pdev
->dev
,
970 pdata
->epe
[id
], dcdc
);
971 if (IS_ERR(dcdc
->regulator
)) {
972 ret
= PTR_ERR(dcdc
->regulator
);
973 dev_err(wm831x
->dev
, "Failed to register EPE%d: %d\n",
978 platform_set_drvdata(pdev
, dcdc
);
987 static __devexit
int wm831x_epe_remove(struct platform_device
*pdev
)
989 struct wm831x_dcdc
*dcdc
= platform_get_drvdata(pdev
);
991 platform_set_drvdata(pdev
, NULL
);
993 regulator_unregister(dcdc
->regulator
);
999 static struct platform_driver wm831x_epe_driver
= {
1000 .probe
= wm831x_epe_probe
,
1001 .remove
= __devexit_p(wm831x_epe_remove
),
1003 .name
= "wm831x-epe",
1004 .owner
= THIS_MODULE
,
1008 static int __init
wm831x_dcdc_init(void)
1011 ret
= platform_driver_register(&wm831x_buckv_driver
);
1013 pr_err("Failed to register WM831x BUCKV driver: %d\n", ret
);
1015 ret
= platform_driver_register(&wm831x_buckp_driver
);
1017 pr_err("Failed to register WM831x BUCKP driver: %d\n", ret
);
1019 ret
= platform_driver_register(&wm831x_boostp_driver
);
1021 pr_err("Failed to register WM831x BOOST driver: %d\n", ret
);
1023 ret
= platform_driver_register(&wm831x_epe_driver
);
1025 pr_err("Failed to register WM831x EPE driver: %d\n", ret
);
1029 subsys_initcall(wm831x_dcdc_init
);
1031 static void __exit
wm831x_dcdc_exit(void)
1033 platform_driver_unregister(&wm831x_epe_driver
);
1034 platform_driver_unregister(&wm831x_boostp_driver
);
1035 platform_driver_unregister(&wm831x_buckp_driver
);
1036 platform_driver_unregister(&wm831x_buckv_driver
);
1038 module_exit(wm831x_dcdc_exit
);
1040 /* Module information */
1041 MODULE_AUTHOR("Mark Brown");
1042 MODULE_DESCRIPTION("WM831x DC-DC convertor driver");
1043 MODULE_LICENSE("GPL");
1044 MODULE_ALIAS("platform:wm831x-buckv");
1045 MODULE_ALIAS("platform:wm831x-buckp");