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
, unsigned *selector
)
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
);
319 /* If this value is already set then do a GPIO update if we can */
320 if (dcdc
->dvs_gpio
&& dcdc
->on_vsel
== vsel
)
321 return wm831x_buckv_set_dvs(rdev
, 0);
323 if (dcdc
->dvs_gpio
&& dcdc
->dvs_vsel
== vsel
)
324 return wm831x_buckv_set_dvs(rdev
, 1);
326 /* Always set the ON status to the minimum voltage */
327 ret
= wm831x_set_bits(wm831x
, on_reg
, WM831X_DC1_ON_VSEL_MASK
, vsel
);
330 dcdc
->on_vsel
= vsel
;
335 /* Kick the voltage transition now */
336 ret
= wm831x_buckv_set_dvs(rdev
, 0);
340 /* Set the high voltage as the DVS voltage. This is optimised
341 * for CPUfreq usage, most processors will keep the maximum
342 * voltage constant and lower the minimum with the frequency. */
343 vsel
= wm831x_buckv_select_max_voltage(rdev
, min_uV
, max_uV
);
345 /* This should never happen - at worst the same vsel
346 * should be chosen */
351 /* Don't bother if it's the same VSEL we're already using */
352 if (vsel
== dcdc
->on_vsel
)
355 ret
= wm831x_set_bits(wm831x
, dvs_reg
, WM831X_DC1_DVS_VSEL_MASK
, vsel
);
357 dcdc
->dvs_vsel
= vsel
;
359 dev_warn(wm831x
->dev
, "Failed to set DCDC DVS VSEL: %d\n",
365 static int wm831x_buckv_set_suspend_voltage(struct regulator_dev
*rdev
,
368 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
369 struct wm831x
*wm831x
= dcdc
->wm831x
;
370 u16 reg
= dcdc
->base
+ WM831X_DCDC_SLEEP_CONTROL
;
373 vsel
= wm831x_buckv_select_min_voltage(rdev
, uV
, uV
);
377 return wm831x_set_bits(wm831x
, reg
, WM831X_DC1_SLP_VSEL_MASK
, vsel
);
380 static int wm831x_buckv_get_voltage_sel(struct regulator_dev
*rdev
)
382 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
384 if (dcdc
->dvs_gpio
&& dcdc
->dvs_gpio_state
)
385 return dcdc
->dvs_vsel
;
387 return dcdc
->on_vsel
;
390 /* Current limit options */
391 static u16 wm831x_dcdc_ilim
[] = {
392 125, 250, 375, 500, 625, 750, 875, 1000
395 static int wm831x_buckv_set_current_limit(struct regulator_dev
*rdev
,
396 int min_uA
, int max_uA
)
398 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
399 struct wm831x
*wm831x
= dcdc
->wm831x
;
400 u16 reg
= dcdc
->base
+ WM831X_DCDC_CONTROL_2
;
403 for (i
= 0; i
< ARRAY_SIZE(wm831x_dcdc_ilim
); i
++) {
404 if (max_uA
<= wm831x_dcdc_ilim
[i
])
407 if (i
== ARRAY_SIZE(wm831x_dcdc_ilim
))
410 return wm831x_set_bits(wm831x
, reg
, WM831X_DC1_HC_THR_MASK
, i
);
413 static int wm831x_buckv_get_current_limit(struct regulator_dev
*rdev
)
415 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
416 struct wm831x
*wm831x
= dcdc
->wm831x
;
417 u16 reg
= dcdc
->base
+ WM831X_DCDC_CONTROL_2
;
420 val
= wm831x_reg_read(wm831x
, reg
);
424 return wm831x_dcdc_ilim
[val
& WM831X_DC1_HC_THR_MASK
];
427 static struct regulator_ops wm831x_buckv_ops
= {
428 .set_voltage
= wm831x_buckv_set_voltage
,
429 .get_voltage_sel
= wm831x_buckv_get_voltage_sel
,
430 .list_voltage
= wm831x_buckv_list_voltage
,
431 .set_suspend_voltage
= wm831x_buckv_set_suspend_voltage
,
432 .set_current_limit
= wm831x_buckv_set_current_limit
,
433 .get_current_limit
= wm831x_buckv_get_current_limit
,
435 .is_enabled
= wm831x_dcdc_is_enabled
,
436 .enable
= wm831x_dcdc_enable
,
437 .disable
= wm831x_dcdc_disable
,
438 .get_status
= wm831x_dcdc_get_status
,
439 .get_mode
= wm831x_dcdc_get_mode
,
440 .set_mode
= wm831x_dcdc_set_mode
,
441 .set_suspend_mode
= wm831x_dcdc_set_suspend_mode
,
445 * Set up DVS control. We just log errors since we can still run
446 * (with reduced performance) if we fail.
448 static __devinit
void wm831x_buckv_dvs_init(struct wm831x_dcdc
*dcdc
,
449 struct wm831x_buckv_pdata
*pdata
)
451 struct wm831x
*wm831x
= dcdc
->wm831x
;
455 if (!pdata
|| !pdata
->dvs_gpio
)
458 switch (pdata
->dvs_control_src
) {
460 ctrl
= 2 << WM831X_DC1_DVS_SRC_SHIFT
;
463 ctrl
= 3 << WM831X_DC1_DVS_SRC_SHIFT
;
466 dev_err(wm831x
->dev
, "Invalid DVS control source %d for %s\n",
467 pdata
->dvs_control_src
, dcdc
->name
);
471 ret
= wm831x_set_bits(wm831x
, dcdc
->base
+ WM831X_DCDC_DVS_CONTROL
,
472 WM831X_DC1_DVS_SRC_MASK
, ctrl
);
474 dev_err(wm831x
->dev
, "Failed to set %s DVS source: %d\n",
479 ret
= gpio_request(pdata
->dvs_gpio
, "DCDC DVS");
481 dev_err(wm831x
->dev
, "Failed to get %s DVS GPIO: %d\n",
486 /* gpiolib won't let us read the GPIO status so pick the higher
487 * of the two existing voltages so we take it as platform data.
489 dcdc
->dvs_gpio_state
= pdata
->dvs_init_state
;
491 ret
= gpio_direction_output(pdata
->dvs_gpio
, dcdc
->dvs_gpio_state
);
493 dev_err(wm831x
->dev
, "Failed to enable %s DVS GPIO: %d\n",
495 gpio_free(pdata
->dvs_gpio
);
499 dcdc
->dvs_gpio
= pdata
->dvs_gpio
;
502 static __devinit
int wm831x_buckv_probe(struct platform_device
*pdev
)
504 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
505 struct wm831x_pdata
*pdata
= wm831x
->dev
->platform_data
;
506 int id
= pdev
->id
% ARRAY_SIZE(pdata
->dcdc
);
507 struct wm831x_dcdc
*dcdc
;
508 struct resource
*res
;
511 dev_dbg(&pdev
->dev
, "Probing DCDC%d\n", id
+ 1);
513 if (pdata
== NULL
|| pdata
->dcdc
[id
] == NULL
)
516 dcdc
= kzalloc(sizeof(struct wm831x_dcdc
), GFP_KERNEL
);
518 dev_err(&pdev
->dev
, "Unable to allocate private data\n");
522 dcdc
->wm831x
= wm831x
;
524 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
526 dev_err(&pdev
->dev
, "No I/O resource\n");
530 dcdc
->base
= res
->start
;
532 snprintf(dcdc
->name
, sizeof(dcdc
->name
), "DCDC%d", id
+ 1);
533 dcdc
->desc
.name
= dcdc
->name
;
535 dcdc
->desc
.type
= REGULATOR_VOLTAGE
;
536 dcdc
->desc
.n_voltages
= WM831X_BUCKV_MAX_SELECTOR
+ 1;
537 dcdc
->desc
.ops
= &wm831x_buckv_ops
;
538 dcdc
->desc
.owner
= THIS_MODULE
;
540 ret
= wm831x_reg_read(wm831x
, dcdc
->base
+ WM831X_DCDC_ON_CONFIG
);
542 dev_err(wm831x
->dev
, "Failed to read ON VSEL: %d\n", ret
);
545 dcdc
->on_vsel
= ret
& WM831X_DC1_ON_VSEL_MASK
;
547 ret
= wm831x_reg_read(wm831x
, dcdc
->base
+ WM831X_DCDC_ON_CONFIG
);
549 dev_err(wm831x
->dev
, "Failed to read DVS VSEL: %d\n", ret
);
552 dcdc
->dvs_vsel
= ret
& WM831X_DC1_DVS_VSEL_MASK
;
555 wm831x_buckv_dvs_init(dcdc
, pdata
->dcdc
[id
]->driver_data
);
557 dcdc
->regulator
= regulator_register(&dcdc
->desc
, &pdev
->dev
,
558 pdata
->dcdc
[id
], dcdc
);
559 if (IS_ERR(dcdc
->regulator
)) {
560 ret
= PTR_ERR(dcdc
->regulator
);
561 dev_err(wm831x
->dev
, "Failed to register DCDC%d: %d\n",
566 irq
= platform_get_irq_byname(pdev
, "UV");
567 ret
= wm831x_request_irq(wm831x
, irq
, wm831x_dcdc_uv_irq
,
568 IRQF_TRIGGER_RISING
, dcdc
->name
,
571 dev_err(&pdev
->dev
, "Failed to request UV IRQ %d: %d\n",
576 irq
= platform_get_irq_byname(pdev
, "HC");
577 ret
= wm831x_request_irq(wm831x
, irq
, wm831x_dcdc_oc_irq
,
578 IRQF_TRIGGER_RISING
, dcdc
->name
,
581 dev_err(&pdev
->dev
, "Failed to request HC IRQ %d: %d\n",
586 platform_set_drvdata(pdev
, dcdc
);
591 wm831x_free_irq(wm831x
, platform_get_irq_byname(pdev
, "UV"), dcdc
);
593 regulator_unregister(dcdc
->regulator
);
596 gpio_free(dcdc
->dvs_gpio
);
601 static __devexit
int wm831x_buckv_remove(struct platform_device
*pdev
)
603 struct wm831x_dcdc
*dcdc
= platform_get_drvdata(pdev
);
604 struct wm831x
*wm831x
= dcdc
->wm831x
;
606 platform_set_drvdata(pdev
, NULL
);
608 wm831x_free_irq(wm831x
, platform_get_irq_byname(pdev
, "HC"), dcdc
);
609 wm831x_free_irq(wm831x
, platform_get_irq_byname(pdev
, "UV"), dcdc
);
610 regulator_unregister(dcdc
->regulator
);
612 gpio_free(dcdc
->dvs_gpio
);
618 static struct platform_driver wm831x_buckv_driver
= {
619 .probe
= wm831x_buckv_probe
,
620 .remove
= __devexit_p(wm831x_buckv_remove
),
622 .name
= "wm831x-buckv",
623 .owner
= THIS_MODULE
,
631 static int wm831x_buckp_list_voltage(struct regulator_dev
*rdev
,
634 if (selector
<= WM831X_BUCKP_MAX_SELECTOR
)
635 return 850000 + (selector
* 25000);
640 static int wm831x_buckp_set_voltage_int(struct regulator_dev
*rdev
, int reg
,
641 int min_uV
, int max_uV
, int *selector
)
643 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
644 struct wm831x
*wm831x
= dcdc
->wm831x
;
647 if (min_uV
<= 34000000)
648 vsel
= (min_uV
- 850000) / 25000;
652 if (wm831x_buckp_list_voltage(rdev
, vsel
) > max_uV
)
657 return wm831x_set_bits(wm831x
, reg
, WM831X_DC3_ON_VSEL_MASK
, vsel
);
660 static int wm831x_buckp_set_voltage(struct regulator_dev
*rdev
,
661 int min_uV
, int max_uV
,
664 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
665 u16 reg
= dcdc
->base
+ WM831X_DCDC_ON_CONFIG
;
667 return wm831x_buckp_set_voltage_int(rdev
, reg
, min_uV
, max_uV
,
671 static int wm831x_buckp_set_suspend_voltage(struct regulator_dev
*rdev
,
674 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
675 u16 reg
= dcdc
->base
+ WM831X_DCDC_SLEEP_CONTROL
;
678 return wm831x_buckp_set_voltage_int(rdev
, reg
, uV
, uV
, &selector
);
681 static int wm831x_buckp_get_voltage_sel(struct regulator_dev
*rdev
)
683 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
684 struct wm831x
*wm831x
= dcdc
->wm831x
;
685 u16 reg
= dcdc
->base
+ WM831X_DCDC_ON_CONFIG
;
688 val
= wm831x_reg_read(wm831x
, reg
);
692 return val
& WM831X_DC3_ON_VSEL_MASK
;
695 static struct regulator_ops wm831x_buckp_ops
= {
696 .set_voltage
= wm831x_buckp_set_voltage
,
697 .get_voltage_sel
= wm831x_buckp_get_voltage_sel
,
698 .list_voltage
= wm831x_buckp_list_voltage
,
699 .set_suspend_voltage
= wm831x_buckp_set_suspend_voltage
,
701 .is_enabled
= wm831x_dcdc_is_enabled
,
702 .enable
= wm831x_dcdc_enable
,
703 .disable
= wm831x_dcdc_disable
,
704 .get_status
= wm831x_dcdc_get_status
,
705 .get_mode
= wm831x_dcdc_get_mode
,
706 .set_mode
= wm831x_dcdc_set_mode
,
707 .set_suspend_mode
= wm831x_dcdc_set_suspend_mode
,
710 static __devinit
int wm831x_buckp_probe(struct platform_device
*pdev
)
712 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
713 struct wm831x_pdata
*pdata
= wm831x
->dev
->platform_data
;
714 int id
= pdev
->id
% ARRAY_SIZE(pdata
->dcdc
);
715 struct wm831x_dcdc
*dcdc
;
716 struct resource
*res
;
719 dev_dbg(&pdev
->dev
, "Probing DCDC%d\n", id
+ 1);
721 if (pdata
== NULL
|| pdata
->dcdc
[id
] == NULL
)
724 dcdc
= kzalloc(sizeof(struct wm831x_dcdc
), GFP_KERNEL
);
726 dev_err(&pdev
->dev
, "Unable to allocate private data\n");
730 dcdc
->wm831x
= wm831x
;
732 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
734 dev_err(&pdev
->dev
, "No I/O resource\n");
738 dcdc
->base
= res
->start
;
740 snprintf(dcdc
->name
, sizeof(dcdc
->name
), "DCDC%d", id
+ 1);
741 dcdc
->desc
.name
= dcdc
->name
;
743 dcdc
->desc
.type
= REGULATOR_VOLTAGE
;
744 dcdc
->desc
.n_voltages
= WM831X_BUCKP_MAX_SELECTOR
+ 1;
745 dcdc
->desc
.ops
= &wm831x_buckp_ops
;
746 dcdc
->desc
.owner
= THIS_MODULE
;
748 dcdc
->regulator
= regulator_register(&dcdc
->desc
, &pdev
->dev
,
749 pdata
->dcdc
[id
], dcdc
);
750 if (IS_ERR(dcdc
->regulator
)) {
751 ret
= PTR_ERR(dcdc
->regulator
);
752 dev_err(wm831x
->dev
, "Failed to register DCDC%d: %d\n",
757 irq
= platform_get_irq_byname(pdev
, "UV");
758 ret
= wm831x_request_irq(wm831x
, irq
, wm831x_dcdc_uv_irq
,
759 IRQF_TRIGGER_RISING
, dcdc
->name
,
762 dev_err(&pdev
->dev
, "Failed to request UV IRQ %d: %d\n",
767 platform_set_drvdata(pdev
, dcdc
);
772 regulator_unregister(dcdc
->regulator
);
778 static __devexit
int wm831x_buckp_remove(struct platform_device
*pdev
)
780 struct wm831x_dcdc
*dcdc
= platform_get_drvdata(pdev
);
781 struct wm831x
*wm831x
= dcdc
->wm831x
;
783 platform_set_drvdata(pdev
, NULL
);
785 wm831x_free_irq(wm831x
, platform_get_irq_byname(pdev
, "UV"), dcdc
);
786 regulator_unregister(dcdc
->regulator
);
792 static struct platform_driver wm831x_buckp_driver
= {
793 .probe
= wm831x_buckp_probe
,
794 .remove
= __devexit_p(wm831x_buckp_remove
),
796 .name
= "wm831x-buckp",
797 .owner
= THIS_MODULE
,
802 * DCDC boost convertors
805 static int wm831x_boostp_get_status(struct regulator_dev
*rdev
)
807 struct wm831x_dcdc
*dcdc
= rdev_get_drvdata(rdev
);
808 struct wm831x
*wm831x
= dcdc
->wm831x
;
811 /* First, check for errors */
812 ret
= wm831x_reg_read(wm831x
, WM831X_DCDC_UV_STATUS
);
816 if (ret
& (1 << rdev_get_id(rdev
))) {
817 dev_dbg(wm831x
->dev
, "DCDC%d under voltage\n",
818 rdev_get_id(rdev
) + 1);
819 return REGULATOR_STATUS_ERROR
;
822 /* Is the regulator on? */
823 ret
= wm831x_reg_read(wm831x
, WM831X_DCDC_STATUS
);
826 if (ret
& (1 << rdev_get_id(rdev
)))
827 return REGULATOR_STATUS_ON
;
829 return REGULATOR_STATUS_OFF
;
832 static struct regulator_ops wm831x_boostp_ops
= {
833 .get_status
= wm831x_boostp_get_status
,
835 .is_enabled
= wm831x_dcdc_is_enabled
,
836 .enable
= wm831x_dcdc_enable
,
837 .disable
= wm831x_dcdc_disable
,
840 static __devinit
int wm831x_boostp_probe(struct platform_device
*pdev
)
842 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
843 struct wm831x_pdata
*pdata
= wm831x
->dev
->platform_data
;
844 int id
= pdev
->id
% ARRAY_SIZE(pdata
->dcdc
);
845 struct wm831x_dcdc
*dcdc
;
846 struct resource
*res
;
849 dev_dbg(&pdev
->dev
, "Probing DCDC%d\n", id
+ 1);
851 if (pdata
== NULL
|| pdata
->dcdc
[id
] == NULL
)
854 dcdc
= kzalloc(sizeof(struct wm831x_dcdc
), GFP_KERNEL
);
856 dev_err(&pdev
->dev
, "Unable to allocate private data\n");
860 dcdc
->wm831x
= wm831x
;
862 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
864 dev_err(&pdev
->dev
, "No I/O resource\n");
868 dcdc
->base
= res
->start
;
870 snprintf(dcdc
->name
, sizeof(dcdc
->name
), "DCDC%d", id
+ 1);
871 dcdc
->desc
.name
= dcdc
->name
;
873 dcdc
->desc
.type
= REGULATOR_VOLTAGE
;
874 dcdc
->desc
.ops
= &wm831x_boostp_ops
;
875 dcdc
->desc
.owner
= THIS_MODULE
;
877 dcdc
->regulator
= regulator_register(&dcdc
->desc
, &pdev
->dev
,
878 pdata
->dcdc
[id
], dcdc
);
879 if (IS_ERR(dcdc
->regulator
)) {
880 ret
= PTR_ERR(dcdc
->regulator
);
881 dev_err(wm831x
->dev
, "Failed to register DCDC%d: %d\n",
886 irq
= platform_get_irq_byname(pdev
, "UV");
887 ret
= wm831x_request_irq(wm831x
, irq
, wm831x_dcdc_uv_irq
,
888 IRQF_TRIGGER_RISING
, dcdc
->name
,
891 dev_err(&pdev
->dev
, "Failed to request UV IRQ %d: %d\n",
896 platform_set_drvdata(pdev
, dcdc
);
901 regulator_unregister(dcdc
->regulator
);
907 static __devexit
int wm831x_boostp_remove(struct platform_device
*pdev
)
909 struct wm831x_dcdc
*dcdc
= platform_get_drvdata(pdev
);
910 struct wm831x
*wm831x
= dcdc
->wm831x
;
912 platform_set_drvdata(pdev
, NULL
);
914 wm831x_free_irq(wm831x
, platform_get_irq_byname(pdev
, "UV"), dcdc
);
915 regulator_unregister(dcdc
->regulator
);
921 static struct platform_driver wm831x_boostp_driver
= {
922 .probe
= wm831x_boostp_probe
,
923 .remove
= __devexit_p(wm831x_boostp_remove
),
925 .name
= "wm831x-boostp",
926 .owner
= THIS_MODULE
,
931 * External Power Enable
933 * These aren't actually DCDCs but look like them in hardware so share
937 #define WM831X_EPE_BASE 6
939 static struct regulator_ops wm831x_epe_ops
= {
940 .is_enabled
= wm831x_dcdc_is_enabled
,
941 .enable
= wm831x_dcdc_enable
,
942 .disable
= wm831x_dcdc_disable
,
943 .get_status
= wm831x_dcdc_get_status
,
946 static __devinit
int wm831x_epe_probe(struct platform_device
*pdev
)
948 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
949 struct wm831x_pdata
*pdata
= wm831x
->dev
->platform_data
;
950 int id
= pdev
->id
% ARRAY_SIZE(pdata
->epe
);
951 struct wm831x_dcdc
*dcdc
;
954 dev_dbg(&pdev
->dev
, "Probing EPE%d\n", id
+ 1);
956 if (pdata
== NULL
|| pdata
->epe
[id
] == NULL
)
959 dcdc
= kzalloc(sizeof(struct wm831x_dcdc
), GFP_KERNEL
);
961 dev_err(&pdev
->dev
, "Unable to allocate private data\n");
965 dcdc
->wm831x
= wm831x
;
967 /* For current parts this is correct; probably need to revisit
970 snprintf(dcdc
->name
, sizeof(dcdc
->name
), "EPE%d", id
+ 1);
971 dcdc
->desc
.name
= dcdc
->name
;
972 dcdc
->desc
.id
= id
+ WM831X_EPE_BASE
; /* Offset in DCDC registers */
973 dcdc
->desc
.ops
= &wm831x_epe_ops
;
974 dcdc
->desc
.type
= REGULATOR_VOLTAGE
;
975 dcdc
->desc
.owner
= THIS_MODULE
;
977 dcdc
->regulator
= regulator_register(&dcdc
->desc
, &pdev
->dev
,
978 pdata
->epe
[id
], dcdc
);
979 if (IS_ERR(dcdc
->regulator
)) {
980 ret
= PTR_ERR(dcdc
->regulator
);
981 dev_err(wm831x
->dev
, "Failed to register EPE%d: %d\n",
986 platform_set_drvdata(pdev
, dcdc
);
995 static __devexit
int wm831x_epe_remove(struct platform_device
*pdev
)
997 struct wm831x_dcdc
*dcdc
= platform_get_drvdata(pdev
);
999 platform_set_drvdata(pdev
, NULL
);
1001 regulator_unregister(dcdc
->regulator
);
1007 static struct platform_driver wm831x_epe_driver
= {
1008 .probe
= wm831x_epe_probe
,
1009 .remove
= __devexit_p(wm831x_epe_remove
),
1011 .name
= "wm831x-epe",
1012 .owner
= THIS_MODULE
,
1016 static int __init
wm831x_dcdc_init(void)
1019 ret
= platform_driver_register(&wm831x_buckv_driver
);
1021 pr_err("Failed to register WM831x BUCKV driver: %d\n", ret
);
1023 ret
= platform_driver_register(&wm831x_buckp_driver
);
1025 pr_err("Failed to register WM831x BUCKP driver: %d\n", ret
);
1027 ret
= platform_driver_register(&wm831x_boostp_driver
);
1029 pr_err("Failed to register WM831x BOOST driver: %d\n", ret
);
1031 ret
= platform_driver_register(&wm831x_epe_driver
);
1033 pr_err("Failed to register WM831x EPE driver: %d\n", ret
);
1037 subsys_initcall(wm831x_dcdc_init
);
1039 static void __exit
wm831x_dcdc_exit(void)
1041 platform_driver_unregister(&wm831x_epe_driver
);
1042 platform_driver_unregister(&wm831x_boostp_driver
);
1043 platform_driver_unregister(&wm831x_buckp_driver
);
1044 platform_driver_unregister(&wm831x_buckv_driver
);
1046 module_exit(wm831x_dcdc_exit
);
1048 /* Module information */
1049 MODULE_AUTHOR("Mark Brown");
1050 MODULE_DESCRIPTION("WM831x DC-DC convertor driver");
1051 MODULE_LICENSE("GPL");
1052 MODULE_ALIAS("platform:wm831x-buckv");
1053 MODULE_ALIAS("platform:wm831x-buckp");