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
;
126 static int wm831x_dcdc_set_mode_int(struct wm831x
*wm831x
, int reg
,
132 case REGULATOR_MODE_FAST
:
133 val
= WM831X_DCDC_MODE_FAST
;
135 case REGULATOR_MODE_NORMAL
:
136 val
= WM831X_DCDC_MODE_NORMAL
;
138 case REGULATOR_MODE_STANDBY
:
139 val
= WM831X_DCDC_MODE_STANDBY
;
141 case REGULATOR_MODE_IDLE
:
142 val
= WM831X_DCDC_MODE_IDLE
;
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
,
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
;
177 /* First, check for errors */
178 ret
= wm831x_reg_read(wm831x
, WM831X_DCDC_UV_STATUS
);
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
);
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
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
,
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
,
241 static int wm831x_buckv_list_voltage(struct regulator_dev
*rdev
,
246 if (selector
<= WM831X_BUCKV_MAX_SELECTOR
)
247 return 600000 + ((selector
- 0x8) * 12500);
251 static int wm831x_buckv_select_min_voltage(struct regulator_dev
*rdev
,
252 int min_uV
, int max_uV
)
258 else if (min_uV
<= 1800000)
259 vsel
= ((min_uV
- 600000) / 12500) + 8;
263 if (wm831x_buckv_list_voltage(rdev
, vsel
) > max_uV
)
269 static int wm831x_buckv_select_max_voltage(struct regulator_dev
*rdev
,
270 int min_uV
, int max_uV
)
274 if (max_uV
< 600000 || max_uV
> 1800000)
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
)
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
)
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.
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
;
313 vsel
= wm831x_buckv_select_min_voltage(rdev
, min_uV
, max_uV
);
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
);
328 dcdc
->on_vsel
= vsel
;
333 /* Kick the voltage transition now */
334 ret
= wm831x_buckv_set_dvs(rdev
, 0);
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
);
343 /* This should never happen - at worst the same vsel
344 * should be chosen */
349 /* Don't bother if it's the same VSEL we're already using */
350 if (vsel
== dcdc
->on_vsel
)
353 ret
= wm831x_set_bits(wm831x
, dvs_reg
, WM831X_DC1_DVS_VSEL_MASK
, vsel
);
355 dcdc
->dvs_vsel
= vsel
;
357 dev_warn(wm831x
->dev
, "Failed to set DCDC DVS VSEL: %d\n",
363 static int wm831x_buckv_set_suspend_voltage(struct regulator_dev
*rdev
,
366 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
367 struct wm831x
*wm831x
= dcdc
->wm831x
;
368 u16 reg
= dcdc
->base
+ WM831X_DCDC_SLEEP_CONTROL
;
371 vsel
= wm831x_buckv_select_min_voltage(rdev
, uV
, uV
);
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
);
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
;
401 for (i
= 0; i
< ARRAY_SIZE(wm831x_dcdc_ilim
); i
++) {
402 if (max_uA
<= wm831x_dcdc_ilim
[i
])
405 if (i
== ARRAY_SIZE(wm831x_dcdc_ilim
))
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
;
418 val
= wm831x_reg_read(wm831x
, reg
);
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
;
453 if (!pdata
|| !pdata
->dvs_gpio
)
456 switch (pdata
->dvs_control_src
) {
458 ctrl
= 2 << WM831X_DC1_DVS_SRC_SHIFT
;
461 ctrl
= 3 << WM831X_DC1_DVS_SRC_SHIFT
;
464 dev_err(wm831x
->dev
, "Invalid DVS control source %d for %s\n",
465 pdata
->dvs_control_src
, dcdc
->name
);
469 ret
= wm831x_set_bits(wm831x
, dcdc
->base
+ WM831X_DCDC_DVS_CONTROL
,
470 WM831X_DC1_DVS_SRC_MASK
, ctrl
);
472 dev_err(wm831x
->dev
, "Failed to set %s DVS source: %d\n",
477 ret
= gpio_request(pdata
->dvs_gpio
, "DCDC DVS");
479 dev_err(wm831x
->dev
, "Failed to get %s DVS GPIO: %d\n",
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
);
491 dev_err(wm831x
->dev
, "Failed to enable %s DVS GPIO: %d\n",
493 gpio_free(pdata
->dvs_gpio
);
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
;
509 dev_dbg(&pdev
->dev
, "Probing DCDC%d\n", id
+ 1);
511 if (pdata
== NULL
|| pdata
->dcdc
[id
] == NULL
)
514 dcdc
= kzalloc(sizeof(struct wm831x_dcdc
), GFP_KERNEL
);
516 dev_err(&pdev
->dev
, "Unable to allocate private data\n");
520 dcdc
->wm831x
= wm831x
;
522 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
524 dev_err(&pdev
->dev
, "No I/O resource\n");
528 dcdc
->base
= res
->start
;
530 snprintf(dcdc
->name
, sizeof(dcdc
->name
), "DCDC%d", id
+ 1);
531 dcdc
->desc
.name
= dcdc
->name
;
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
);
540 dev_err(wm831x
->dev
, "Failed to read ON VSEL: %d\n", ret
);
543 dcdc
->on_vsel
= ret
& WM831X_DC1_ON_VSEL_MASK
;
545 ret
= wm831x_reg_read(wm831x
, dcdc
->base
+ WM831X_DCDC_ON_CONFIG
);
547 dev_err(wm831x
->dev
, "Failed to read DVS VSEL: %d\n", ret
);
550 dcdc
->dvs_vsel
= ret
& WM831X_DC1_DVS_VSEL_MASK
;
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",
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
,
569 dev_err(&pdev
->dev
, "Failed to request UV IRQ %d: %d\n",
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
,
579 dev_err(&pdev
->dev
, "Failed to request HC IRQ %d: %d\n",
584 platform_set_drvdata(pdev
, dcdc
);
589 wm831x_free_irq(wm831x
, platform_get_irq_byname(pdev
, "UV"), dcdc
);
591 regulator_unregister(dcdc
->regulator
);
594 gpio_free(dcdc
->dvs_gpio
);
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
);
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
)
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
)
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
,
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
;
681 val
= wm831x_reg_read(wm831x
, reg
);
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
;
712 dev_dbg(&pdev
->dev
, "Probing DCDC%d\n", id
+ 1);
714 if (pdata
== NULL
|| pdata
->dcdc
[id
] == NULL
)
717 dcdc
= kzalloc(sizeof(struct wm831x_dcdc
), GFP_KERNEL
);
719 dev_err(&pdev
->dev
, "Unable to allocate private data\n");
723 dcdc
->wm831x
= wm831x
;
725 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
727 dev_err(&pdev
->dev
, "No I/O resource\n");
731 dcdc
->base
= res
->start
;
733 snprintf(dcdc
->name
, sizeof(dcdc
->name
), "DCDC%d", id
+ 1);
734 dcdc
->desc
.name
= dcdc
->name
;
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",
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
,
755 dev_err(&pdev
->dev
, "Failed to request UV IRQ %d: %d\n",
760 platform_set_drvdata(pdev
, dcdc
);
765 regulator_unregister(dcdc
->regulator
);
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
);
785 static struct platform_driver wm831x_buckp_driver
= {
786 .probe
= wm831x_buckp_probe
,
787 .remove
= __devexit_p(wm831x_buckp_remove
),
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
;
804 /* First, check for errors */
805 ret
= wm831x_reg_read(wm831x
, WM831X_DCDC_UV_STATUS
);
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
);
819 if (ret
& (1 << rdev_get_id(rdev
)))
820 return REGULATOR_STATUS_ON
;
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
;
842 dev_dbg(&pdev
->dev
, "Probing DCDC%d\n", id
+ 1);
844 if (pdata
== NULL
|| pdata
->dcdc
[id
] == NULL
)
847 dcdc
= kzalloc(sizeof(struct wm831x_dcdc
), GFP_KERNEL
);
849 dev_err(&pdev
->dev
, "Unable to allocate private data\n");
853 dcdc
->wm831x
= wm831x
;
855 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
857 dev_err(&pdev
->dev
, "No I/O resource\n");
861 dcdc
->base
= res
->start
;
863 snprintf(dcdc
->name
, sizeof(dcdc
->name
), "DCDC%d", id
+ 1);
864 dcdc
->desc
.name
= dcdc
->name
;
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",
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
,
884 dev_err(&pdev
->dev
, "Failed to request UV IRQ %d: %d\n",
889 platform_set_drvdata(pdev
, dcdc
);
894 regulator_unregister(dcdc
->regulator
);
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
);
914 static struct platform_driver wm831x_boostp_driver
= {
915 .probe
= wm831x_boostp_probe
,
916 .remove
= __devexit_p(wm831x_boostp_remove
),
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
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
;
947 dev_dbg(&pdev
->dev
, "Probing EPE%d\n", id
+ 1);
949 if (pdata
== NULL
|| pdata
->epe
[id
] == NULL
)
952 dcdc
= kzalloc(sizeof(struct wm831x_dcdc
), GFP_KERNEL
);
954 dev_err(&pdev
->dev
, "Unable to allocate private data\n");
958 dcdc
->wm831x
= wm831x
;
960 /* For current parts this is correct; probably need to revisit
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",
979 platform_set_drvdata(pdev
, dcdc
);
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
);
1000 static struct platform_driver wm831x_epe_driver
= {
1001 .probe
= wm831x_epe_probe
,
1002 .remove
= __devexit_p(wm831x_epe_remove
),
1004 .name
= "wm831x-epe",
1005 .owner
= THIS_MODULE
,
1009 static int __init
wm831x_dcdc_init(void)
1012 ret
= platform_driver_register(&wm831x_buckv_driver
);
1014 pr_err("Failed to register WM831x BUCKV driver: %d\n", ret
);
1016 ret
= platform_driver_register(&wm831x_buckp_driver
);
1018 pr_err("Failed to register WM831x BUCKP driver: %d\n", ret
);
1020 ret
= platform_driver_register(&wm831x_boostp_driver
);
1022 pr_err("Failed to register WM831x BOOST driver: %d\n", ret
);
1024 ret
= platform_driver_register(&wm831x_epe_driver
);
1026 pr_err("Failed to register WM831x EPE driver: %d\n", ret
);
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");