2 * PMU driver for Wolfson Microelectronics wm831x PMICs
4 * Copyright 2009 Wolfson Microelectronics PLC.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/module.h>
12 #include <linux/err.h>
13 #include <linux/platform_device.h>
14 #include <linux/power_supply.h>
16 #include <linux/mfd/wm831x/core.h>
17 #include <linux/mfd/wm831x/auxadc.h>
18 #include <linux/mfd/wm831x/pmu.h>
19 #include <linux/mfd/wm831x/pdata.h>
22 struct wm831x
*wm831x
;
23 struct power_supply wall
;
24 struct power_supply usb
;
25 struct power_supply battery
;
28 static int wm831x_power_check_online(struct wm831x
*wm831x
, int supply
,
29 union power_supply_propval
*val
)
33 ret
= wm831x_reg_read(wm831x
, WM831X_SYSTEM_STATUS
);
45 static int wm831x_power_read_voltage(struct wm831x
*wm831x
,
46 enum wm831x_auxadc src
,
47 union power_supply_propval
*val
)
51 ret
= wm831x_auxadc_read_uv(wm831x
, src
);
58 /*********************************************************************
60 *********************************************************************/
61 static int wm831x_wall_get_prop(struct power_supply
*psy
,
62 enum power_supply_property psp
,
63 union power_supply_propval
*val
)
65 struct wm831x_power
*wm831x_power
= dev_get_drvdata(psy
->dev
->parent
);
66 struct wm831x
*wm831x
= wm831x_power
->wm831x
;
70 case POWER_SUPPLY_PROP_ONLINE
:
71 ret
= wm831x_power_check_online(wm831x
, WM831X_PWR_WALL
, val
);
73 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
74 ret
= wm831x_power_read_voltage(wm831x
, WM831X_AUX_WALL
, val
);
84 static enum power_supply_property wm831x_wall_props
[] = {
85 POWER_SUPPLY_PROP_ONLINE
,
86 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
89 /*********************************************************************
91 *********************************************************************/
92 static int wm831x_usb_get_prop(struct power_supply
*psy
,
93 enum power_supply_property psp
,
94 union power_supply_propval
*val
)
96 struct wm831x_power
*wm831x_power
= dev_get_drvdata(psy
->dev
->parent
);
97 struct wm831x
*wm831x
= wm831x_power
->wm831x
;
101 case POWER_SUPPLY_PROP_ONLINE
:
102 ret
= wm831x_power_check_online(wm831x
, WM831X_PWR_USB
, val
);
104 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
105 ret
= wm831x_power_read_voltage(wm831x
, WM831X_AUX_USB
, val
);
115 static enum power_supply_property wm831x_usb_props
[] = {
116 POWER_SUPPLY_PROP_ONLINE
,
117 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
120 /*********************************************************************
122 *********************************************************************/
129 static struct chg_map trickle_ilims
[] = {
130 { 50, 0 << WM831X_CHG_TRKL_ILIM_SHIFT
},
131 { 100, 1 << WM831X_CHG_TRKL_ILIM_SHIFT
},
132 { 150, 2 << WM831X_CHG_TRKL_ILIM_SHIFT
},
133 { 200, 3 << WM831X_CHG_TRKL_ILIM_SHIFT
},
136 static struct chg_map vsels
[] = {
137 { 4050, 0 << WM831X_CHG_VSEL_SHIFT
},
138 { 4100, 1 << WM831X_CHG_VSEL_SHIFT
},
139 { 4150, 2 << WM831X_CHG_VSEL_SHIFT
},
140 { 4200, 3 << WM831X_CHG_VSEL_SHIFT
},
143 static struct chg_map fast_ilims
[] = {
144 { 0, 0 << WM831X_CHG_FAST_ILIM_SHIFT
},
145 { 50, 1 << WM831X_CHG_FAST_ILIM_SHIFT
},
146 { 100, 2 << WM831X_CHG_FAST_ILIM_SHIFT
},
147 { 150, 3 << WM831X_CHG_FAST_ILIM_SHIFT
},
148 { 200, 4 << WM831X_CHG_FAST_ILIM_SHIFT
},
149 { 250, 5 << WM831X_CHG_FAST_ILIM_SHIFT
},
150 { 300, 6 << WM831X_CHG_FAST_ILIM_SHIFT
},
151 { 350, 7 << WM831X_CHG_FAST_ILIM_SHIFT
},
152 { 400, 8 << WM831X_CHG_FAST_ILIM_SHIFT
},
153 { 450, 9 << WM831X_CHG_FAST_ILIM_SHIFT
},
154 { 500, 10 << WM831X_CHG_FAST_ILIM_SHIFT
},
155 { 600, 11 << WM831X_CHG_FAST_ILIM_SHIFT
},
156 { 700, 12 << WM831X_CHG_FAST_ILIM_SHIFT
},
157 { 800, 13 << WM831X_CHG_FAST_ILIM_SHIFT
},
158 { 900, 14 << WM831X_CHG_FAST_ILIM_SHIFT
},
159 { 1000, 15 << WM831X_CHG_FAST_ILIM_SHIFT
},
162 static struct chg_map eoc_iterms
[] = {
163 { 20, 0 << WM831X_CHG_ITERM_SHIFT
},
164 { 30, 1 << WM831X_CHG_ITERM_SHIFT
},
165 { 40, 2 << WM831X_CHG_ITERM_SHIFT
},
166 { 50, 3 << WM831X_CHG_ITERM_SHIFT
},
167 { 60, 4 << WM831X_CHG_ITERM_SHIFT
},
168 { 70, 5 << WM831X_CHG_ITERM_SHIFT
},
169 { 80, 6 << WM831X_CHG_ITERM_SHIFT
},
170 { 90, 7 << WM831X_CHG_ITERM_SHIFT
},
173 static struct chg_map chg_times
[] = {
174 { 60, 0 << WM831X_CHG_TIME_SHIFT
},
175 { 90, 1 << WM831X_CHG_TIME_SHIFT
},
176 { 120, 2 << WM831X_CHG_TIME_SHIFT
},
177 { 150, 3 << WM831X_CHG_TIME_SHIFT
},
178 { 180, 4 << WM831X_CHG_TIME_SHIFT
},
179 { 210, 5 << WM831X_CHG_TIME_SHIFT
},
180 { 240, 6 << WM831X_CHG_TIME_SHIFT
},
181 { 270, 7 << WM831X_CHG_TIME_SHIFT
},
182 { 300, 8 << WM831X_CHG_TIME_SHIFT
},
183 { 330, 9 << WM831X_CHG_TIME_SHIFT
},
184 { 360, 10 << WM831X_CHG_TIME_SHIFT
},
185 { 390, 11 << WM831X_CHG_TIME_SHIFT
},
186 { 420, 12 << WM831X_CHG_TIME_SHIFT
},
187 { 450, 13 << WM831X_CHG_TIME_SHIFT
},
188 { 480, 14 << WM831X_CHG_TIME_SHIFT
},
189 { 510, 15 << WM831X_CHG_TIME_SHIFT
},
192 static void wm831x_battey_apply_config(struct wm831x
*wm831x
,
193 struct chg_map
*map
, int count
, int val
,
194 int *reg
, const char *name
,
199 for (i
= 0; i
< count
; i
++)
200 if (val
== map
[i
].val
)
203 dev_err(wm831x
->dev
, "Invalid %s %d%s\n",
206 *reg
|= map
[i
].reg_val
;
207 dev_dbg(wm831x
->dev
, "Set %s of %d%s\n", name
, val
, units
);
211 static void wm831x_config_battery(struct wm831x
*wm831x
)
213 struct wm831x_pdata
*wm831x_pdata
= wm831x
->dev
->platform_data
;
214 struct wm831x_battery_pdata
*pdata
;
217 if (!wm831x_pdata
|| !wm831x_pdata
->battery
) {
218 dev_warn(wm831x
->dev
,
219 "No battery charger configuration\n");
223 pdata
= wm831x_pdata
->battery
;
228 if (!pdata
->enable
) {
229 dev_info(wm831x
->dev
, "Battery charger disabled\n");
233 reg1
|= WM831X_CHG_ENA
;
235 reg2
|= WM831X_CHG_OFF_MSK
;
236 if (pdata
->fast_enable
)
237 reg1
|= WM831X_CHG_FAST
;
239 wm831x_battey_apply_config(wm831x
, trickle_ilims
,
240 ARRAY_SIZE(trickle_ilims
),
241 pdata
->trickle_ilim
, ®2
,
242 "trickle charge current limit", "mA");
244 wm831x_battey_apply_config(wm831x
, vsels
, ARRAY_SIZE(vsels
),
246 "target voltage", "mV");
248 wm831x_battey_apply_config(wm831x
, fast_ilims
, ARRAY_SIZE(fast_ilims
),
249 pdata
->fast_ilim
, ®2
,
250 "fast charge current limit", "mA");
252 wm831x_battey_apply_config(wm831x
, eoc_iterms
, ARRAY_SIZE(eoc_iterms
),
253 pdata
->eoc_iterm
, ®1
,
254 "end of charge current threshold", "mA");
256 wm831x_battey_apply_config(wm831x
, chg_times
, ARRAY_SIZE(chg_times
),
257 pdata
->timeout
, ®2
,
258 "charger timeout", "min");
260 ret
= wm831x_reg_unlock(wm831x
);
262 dev_err(wm831x
->dev
, "Failed to unlock registers: %d\n", ret
);
266 ret
= wm831x_set_bits(wm831x
, WM831X_CHARGER_CONTROL_1
,
267 WM831X_CHG_ENA_MASK
|
268 WM831X_CHG_FAST_MASK
|
269 WM831X_CHG_ITERM_MASK
|
270 WM831X_CHG_ITERM_MASK
,
273 dev_err(wm831x
->dev
, "Failed to set charger control 1: %d\n",
276 ret
= wm831x_set_bits(wm831x
, WM831X_CHARGER_CONTROL_2
,
278 WM831X_CHG_TIME_MASK
|
279 WM831X_CHG_FAST_ILIM_MASK
|
280 WM831X_CHG_TRKL_ILIM_MASK
|
281 WM831X_CHG_VSEL_MASK
,
284 dev_err(wm831x
->dev
, "Failed to set charger control 2: %d\n",
287 wm831x_reg_lock(wm831x
);
290 static int wm831x_bat_check_status(struct wm831x
*wm831x
, int *status
)
294 ret
= wm831x_reg_read(wm831x
, WM831X_SYSTEM_STATUS
);
298 if (ret
& WM831X_PWR_SRC_BATT
) {
299 *status
= POWER_SUPPLY_STATUS_DISCHARGING
;
303 ret
= wm831x_reg_read(wm831x
, WM831X_CHARGER_STATUS
);
307 switch (ret
& WM831X_CHG_STATE_MASK
) {
308 case WM831X_CHG_STATE_OFF
:
309 *status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
311 case WM831X_CHG_STATE_TRICKLE
:
312 case WM831X_CHG_STATE_FAST
:
313 *status
= POWER_SUPPLY_STATUS_CHARGING
;
317 *status
= POWER_SUPPLY_STATUS_UNKNOWN
;
324 static int wm831x_bat_check_type(struct wm831x
*wm831x
, int *type
)
328 ret
= wm831x_reg_read(wm831x
, WM831X_CHARGER_STATUS
);
332 switch (ret
& WM831X_CHG_STATE_MASK
) {
333 case WM831X_CHG_STATE_TRICKLE
:
334 case WM831X_CHG_STATE_TRICKLE_OT
:
335 *type
= POWER_SUPPLY_CHARGE_TYPE_TRICKLE
;
337 case WM831X_CHG_STATE_FAST
:
338 case WM831X_CHG_STATE_FAST_OT
:
339 *type
= POWER_SUPPLY_CHARGE_TYPE_FAST
;
342 *type
= POWER_SUPPLY_CHARGE_TYPE_NONE
;
349 static int wm831x_bat_check_health(struct wm831x
*wm831x
, int *health
)
353 ret
= wm831x_reg_read(wm831x
, WM831X_CHARGER_STATUS
);
357 if (ret
& WM831X_BATT_HOT_STS
) {
358 *health
= POWER_SUPPLY_HEALTH_OVERHEAT
;
362 if (ret
& WM831X_BATT_COLD_STS
) {
363 *health
= POWER_SUPPLY_HEALTH_COLD
;
367 if (ret
& WM831X_BATT_OV_STS
) {
368 *health
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
372 switch (ret
& WM831X_CHG_STATE_MASK
) {
373 case WM831X_CHG_STATE_TRICKLE_OT
:
374 case WM831X_CHG_STATE_FAST_OT
:
375 *health
= POWER_SUPPLY_HEALTH_OVERHEAT
;
377 case WM831X_CHG_STATE_DEFECTIVE
:
378 *health
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
381 *health
= POWER_SUPPLY_HEALTH_GOOD
;
388 static int wm831x_bat_get_prop(struct power_supply
*psy
,
389 enum power_supply_property psp
,
390 union power_supply_propval
*val
)
392 struct wm831x_power
*wm831x_power
= dev_get_drvdata(psy
->dev
->parent
);
393 struct wm831x
*wm831x
= wm831x_power
->wm831x
;
397 case POWER_SUPPLY_PROP_STATUS
:
398 ret
= wm831x_bat_check_status(wm831x
, &val
->intval
);
400 case POWER_SUPPLY_PROP_ONLINE
:
401 ret
= wm831x_power_check_online(wm831x
, WM831X_PWR_SRC_BATT
,
404 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
405 ret
= wm831x_power_read_voltage(wm831x
, WM831X_AUX_BATT
, val
);
407 case POWER_SUPPLY_PROP_HEALTH
:
408 ret
= wm831x_bat_check_health(wm831x
, &val
->intval
);
410 case POWER_SUPPLY_PROP_CHARGE_TYPE
:
411 ret
= wm831x_bat_check_type(wm831x
, &val
->intval
);
421 static enum power_supply_property wm831x_bat_props
[] = {
422 POWER_SUPPLY_PROP_STATUS
,
423 POWER_SUPPLY_PROP_ONLINE
,
424 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
425 POWER_SUPPLY_PROP_HEALTH
,
426 POWER_SUPPLY_PROP_CHARGE_TYPE
,
429 static const char *wm831x_bat_irqs
[] = {
440 static irqreturn_t
wm831x_bat_irq(int irq
, void *data
)
442 struct wm831x_power
*wm831x_power
= data
;
443 struct wm831x
*wm831x
= wm831x_power
->wm831x
;
445 dev_dbg(wm831x
->dev
, "Battery status changed: %d\n", irq
);
447 /* The battery charger is autonomous so we don't need to do
448 * anything except kick user space */
449 power_supply_changed(&wm831x_power
->battery
);
455 /*********************************************************************
457 *********************************************************************/
459 static irqreturn_t
wm831x_syslo_irq(int irq
, void *data
)
461 struct wm831x_power
*wm831x_power
= data
;
462 struct wm831x
*wm831x
= wm831x_power
->wm831x
;
464 /* Not much we can actually *do* but tell people for
465 * posterity, we're probably about to run out of power. */
466 dev_crit(wm831x
->dev
, "SYSVDD under voltage\n");
471 static irqreturn_t
wm831x_pwr_src_irq(int irq
, void *data
)
473 struct wm831x_power
*wm831x_power
= data
;
474 struct wm831x
*wm831x
= wm831x_power
->wm831x
;
476 dev_dbg(wm831x
->dev
, "Power source changed\n");
478 /* Just notify for everything - little harm in overnotifying. */
479 power_supply_changed(&wm831x_power
->battery
);
480 power_supply_changed(&wm831x_power
->usb
);
481 power_supply_changed(&wm831x_power
->wall
);
486 static __devinit
int wm831x_power_probe(struct platform_device
*pdev
)
488 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
489 struct wm831x_power
*power
;
490 struct power_supply
*usb
;
491 struct power_supply
*battery
;
492 struct power_supply
*wall
;
495 power
= kzalloc(sizeof(struct wm831x_power
), GFP_KERNEL
);
499 power
->wm831x
= wm831x
;
500 platform_set_drvdata(pdev
, power
);
503 battery
= &power
->battery
;
506 /* We ignore configuration failures since we can still read back
507 * the status without enabling the charger.
509 wm831x_config_battery(wm831x
);
511 wall
->name
= "wm831x-wall";
512 wall
->type
= POWER_SUPPLY_TYPE_MAINS
;
513 wall
->properties
= wm831x_wall_props
;
514 wall
->num_properties
= ARRAY_SIZE(wm831x_wall_props
);
515 wall
->get_property
= wm831x_wall_get_prop
;
516 ret
= power_supply_register(&pdev
->dev
, wall
);
520 battery
->name
= "wm831x-battery";
521 battery
->properties
= wm831x_bat_props
;
522 battery
->num_properties
= ARRAY_SIZE(wm831x_bat_props
);
523 battery
->get_property
= wm831x_bat_get_prop
;
524 battery
->use_for_apm
= 1;
525 ret
= power_supply_register(&pdev
->dev
, battery
);
529 usb
->name
= "wm831x-usb",
530 usb
->type
= POWER_SUPPLY_TYPE_USB
;
531 usb
->properties
= wm831x_usb_props
;
532 usb
->num_properties
= ARRAY_SIZE(wm831x_usb_props
);
533 usb
->get_property
= wm831x_usb_get_prop
;
534 ret
= power_supply_register(&pdev
->dev
, usb
);
538 irq
= platform_get_irq_byname(pdev
, "SYSLO");
539 ret
= wm831x_request_irq(wm831x
, irq
, wm831x_syslo_irq
,
540 IRQF_TRIGGER_RISING
, "SYSLO",
543 dev_err(&pdev
->dev
, "Failed to request SYSLO IRQ %d: %d\n",
548 irq
= platform_get_irq_byname(pdev
, "PWR SRC");
549 ret
= wm831x_request_irq(wm831x
, irq
, wm831x_pwr_src_irq
,
550 IRQF_TRIGGER_RISING
, "Power source",
553 dev_err(&pdev
->dev
, "Failed to request PWR SRC IRQ %d: %d\n",
558 for (i
= 0; i
< ARRAY_SIZE(wm831x_bat_irqs
); i
++) {
559 irq
= platform_get_irq_byname(pdev
, wm831x_bat_irqs
[i
]);
560 ret
= wm831x_request_irq(wm831x
, irq
, wm831x_bat_irq
,
566 "Failed to request %s IRQ %d: %d\n",
567 wm831x_bat_irqs
[i
], irq
, ret
);
575 for (; i
>= 0; i
--) {
576 irq
= platform_get_irq_byname(pdev
, wm831x_bat_irqs
[i
]);
577 wm831x_free_irq(wm831x
, irq
, power
);
579 irq
= platform_get_irq_byname(pdev
, "PWR SRC");
580 wm831x_free_irq(wm831x
, irq
, power
);
582 irq
= platform_get_irq_byname(pdev
, "SYSLO");
583 wm831x_free_irq(wm831x
, irq
, power
);
585 power_supply_unregister(usb
);
587 power_supply_unregister(battery
);
589 power_supply_unregister(wall
);
595 static __devexit
int wm831x_power_remove(struct platform_device
*pdev
)
597 struct wm831x_power
*wm831x_power
= platform_get_drvdata(pdev
);
598 struct wm831x
*wm831x
= wm831x_power
->wm831x
;
601 for (i
= 0; i
< ARRAY_SIZE(wm831x_bat_irqs
); i
++) {
602 irq
= platform_get_irq_byname(pdev
, wm831x_bat_irqs
[i
]);
603 wm831x_free_irq(wm831x
, irq
, wm831x_power
);
606 irq
= platform_get_irq_byname(pdev
, "PWR SRC");
607 wm831x_free_irq(wm831x
, irq
, wm831x_power
);
609 irq
= platform_get_irq_byname(pdev
, "SYSLO");
610 wm831x_free_irq(wm831x
, irq
, wm831x_power
);
612 power_supply_unregister(&wm831x_power
->battery
);
613 power_supply_unregister(&wm831x_power
->wall
);
614 power_supply_unregister(&wm831x_power
->usb
);
618 static struct platform_driver wm831x_power_driver
= {
619 .probe
= wm831x_power_probe
,
620 .remove
= __devexit_p(wm831x_power_remove
),
622 .name
= "wm831x-power",
626 static int __init
wm831x_power_init(void)
628 return platform_driver_register(&wm831x_power_driver
);
630 module_init(wm831x_power_init
);
632 static void __exit
wm831x_power_exit(void)
634 platform_driver_unregister(&wm831x_power_driver
);
636 module_exit(wm831x_power_exit
);
638 MODULE_DESCRIPTION("Power supply driver for WM831x PMICs");
639 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
640 MODULE_LICENSE("GPL");
641 MODULE_ALIAS("platform:wm831x-power");