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>
15 #include <linux/slab.h>
17 #include <linux/mfd/wm831x/core.h>
18 #include <linux/mfd/wm831x/auxadc.h>
19 #include <linux/mfd/wm831x/pmu.h>
20 #include <linux/mfd/wm831x/pdata.h>
23 struct wm831x
*wm831x
;
24 struct power_supply wall
;
25 struct power_supply usb
;
26 struct power_supply battery
;
29 char battery_name
[20];
32 static int wm831x_power_check_online(struct wm831x
*wm831x
, int supply
,
33 union power_supply_propval
*val
)
37 ret
= wm831x_reg_read(wm831x
, WM831X_SYSTEM_STATUS
);
49 static int wm831x_power_read_voltage(struct wm831x
*wm831x
,
50 enum wm831x_auxadc src
,
51 union power_supply_propval
*val
)
55 ret
= wm831x_auxadc_read_uv(wm831x
, src
);
62 /*********************************************************************
64 *********************************************************************/
65 static int wm831x_wall_get_prop(struct power_supply
*psy
,
66 enum power_supply_property psp
,
67 union power_supply_propval
*val
)
69 struct wm831x_power
*wm831x_power
= dev_get_drvdata(psy
->dev
->parent
);
70 struct wm831x
*wm831x
= wm831x_power
->wm831x
;
74 case POWER_SUPPLY_PROP_ONLINE
:
75 ret
= wm831x_power_check_online(wm831x
, WM831X_PWR_WALL
, val
);
77 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
78 ret
= wm831x_power_read_voltage(wm831x
, WM831X_AUX_WALL
, val
);
88 static enum power_supply_property wm831x_wall_props
[] = {
89 POWER_SUPPLY_PROP_ONLINE
,
90 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
93 /*********************************************************************
95 *********************************************************************/
96 static int wm831x_usb_get_prop(struct power_supply
*psy
,
97 enum power_supply_property psp
,
98 union power_supply_propval
*val
)
100 struct wm831x_power
*wm831x_power
= dev_get_drvdata(psy
->dev
->parent
);
101 struct wm831x
*wm831x
= wm831x_power
->wm831x
;
105 case POWER_SUPPLY_PROP_ONLINE
:
106 ret
= wm831x_power_check_online(wm831x
, WM831X_PWR_USB
, val
);
108 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
109 ret
= wm831x_power_read_voltage(wm831x
, WM831X_AUX_USB
, val
);
119 static enum power_supply_property wm831x_usb_props
[] = {
120 POWER_SUPPLY_PROP_ONLINE
,
121 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
124 /*********************************************************************
126 *********************************************************************/
133 static struct chg_map trickle_ilims
[] = {
134 { 50, 0 << WM831X_CHG_TRKL_ILIM_SHIFT
},
135 { 100, 1 << WM831X_CHG_TRKL_ILIM_SHIFT
},
136 { 150, 2 << WM831X_CHG_TRKL_ILIM_SHIFT
},
137 { 200, 3 << WM831X_CHG_TRKL_ILIM_SHIFT
},
140 static struct chg_map vsels
[] = {
141 { 4050, 0 << WM831X_CHG_VSEL_SHIFT
},
142 { 4100, 1 << WM831X_CHG_VSEL_SHIFT
},
143 { 4150, 2 << WM831X_CHG_VSEL_SHIFT
},
144 { 4200, 3 << WM831X_CHG_VSEL_SHIFT
},
147 static struct chg_map fast_ilims
[] = {
148 { 0, 0 << WM831X_CHG_FAST_ILIM_SHIFT
},
149 { 50, 1 << WM831X_CHG_FAST_ILIM_SHIFT
},
150 { 100, 2 << WM831X_CHG_FAST_ILIM_SHIFT
},
151 { 150, 3 << WM831X_CHG_FAST_ILIM_SHIFT
},
152 { 200, 4 << WM831X_CHG_FAST_ILIM_SHIFT
},
153 { 250, 5 << WM831X_CHG_FAST_ILIM_SHIFT
},
154 { 300, 6 << WM831X_CHG_FAST_ILIM_SHIFT
},
155 { 350, 7 << WM831X_CHG_FAST_ILIM_SHIFT
},
156 { 400, 8 << WM831X_CHG_FAST_ILIM_SHIFT
},
157 { 450, 9 << WM831X_CHG_FAST_ILIM_SHIFT
},
158 { 500, 10 << WM831X_CHG_FAST_ILIM_SHIFT
},
159 { 600, 11 << WM831X_CHG_FAST_ILIM_SHIFT
},
160 { 700, 12 << WM831X_CHG_FAST_ILIM_SHIFT
},
161 { 800, 13 << WM831X_CHG_FAST_ILIM_SHIFT
},
162 { 900, 14 << WM831X_CHG_FAST_ILIM_SHIFT
},
163 { 1000, 15 << WM831X_CHG_FAST_ILIM_SHIFT
},
166 static struct chg_map eoc_iterms
[] = {
167 { 20, 0 << WM831X_CHG_ITERM_SHIFT
},
168 { 30, 1 << WM831X_CHG_ITERM_SHIFT
},
169 { 40, 2 << WM831X_CHG_ITERM_SHIFT
},
170 { 50, 3 << WM831X_CHG_ITERM_SHIFT
},
171 { 60, 4 << WM831X_CHG_ITERM_SHIFT
},
172 { 70, 5 << WM831X_CHG_ITERM_SHIFT
},
173 { 80, 6 << WM831X_CHG_ITERM_SHIFT
},
174 { 90, 7 << WM831X_CHG_ITERM_SHIFT
},
177 static struct chg_map chg_times
[] = {
178 { 60, 0 << WM831X_CHG_TIME_SHIFT
},
179 { 90, 1 << WM831X_CHG_TIME_SHIFT
},
180 { 120, 2 << WM831X_CHG_TIME_SHIFT
},
181 { 150, 3 << WM831X_CHG_TIME_SHIFT
},
182 { 180, 4 << WM831X_CHG_TIME_SHIFT
},
183 { 210, 5 << WM831X_CHG_TIME_SHIFT
},
184 { 240, 6 << WM831X_CHG_TIME_SHIFT
},
185 { 270, 7 << WM831X_CHG_TIME_SHIFT
},
186 { 300, 8 << WM831X_CHG_TIME_SHIFT
},
187 { 330, 9 << WM831X_CHG_TIME_SHIFT
},
188 { 360, 10 << WM831X_CHG_TIME_SHIFT
},
189 { 390, 11 << WM831X_CHG_TIME_SHIFT
},
190 { 420, 12 << WM831X_CHG_TIME_SHIFT
},
191 { 450, 13 << WM831X_CHG_TIME_SHIFT
},
192 { 480, 14 << WM831X_CHG_TIME_SHIFT
},
193 { 510, 15 << WM831X_CHG_TIME_SHIFT
},
196 static void wm831x_battey_apply_config(struct wm831x
*wm831x
,
197 struct chg_map
*map
, int count
, int val
,
198 int *reg
, const char *name
,
203 for (i
= 0; i
< count
; i
++)
204 if (val
== map
[i
].val
)
207 dev_err(wm831x
->dev
, "Invalid %s %d%s\n",
210 *reg
|= map
[i
].reg_val
;
211 dev_dbg(wm831x
->dev
, "Set %s of %d%s\n", name
, val
, units
);
215 static void wm831x_config_battery(struct wm831x
*wm831x
)
217 struct wm831x_pdata
*wm831x_pdata
= wm831x
->dev
->platform_data
;
218 struct wm831x_battery_pdata
*pdata
;
221 if (!wm831x_pdata
|| !wm831x_pdata
->battery
) {
222 dev_warn(wm831x
->dev
,
223 "No battery charger configuration\n");
227 pdata
= wm831x_pdata
->battery
;
232 if (!pdata
->enable
) {
233 dev_info(wm831x
->dev
, "Battery charger disabled\n");
237 reg1
|= WM831X_CHG_ENA
;
239 reg2
|= WM831X_CHG_OFF_MSK
;
240 if (pdata
->fast_enable
)
241 reg1
|= WM831X_CHG_FAST
;
243 wm831x_battey_apply_config(wm831x
, trickle_ilims
,
244 ARRAY_SIZE(trickle_ilims
),
245 pdata
->trickle_ilim
, ®2
,
246 "trickle charge current limit", "mA");
248 wm831x_battey_apply_config(wm831x
, vsels
, ARRAY_SIZE(vsels
),
250 "target voltage", "mV");
252 wm831x_battey_apply_config(wm831x
, fast_ilims
, ARRAY_SIZE(fast_ilims
),
253 pdata
->fast_ilim
, ®2
,
254 "fast charge current limit", "mA");
256 wm831x_battey_apply_config(wm831x
, eoc_iterms
, ARRAY_SIZE(eoc_iterms
),
257 pdata
->eoc_iterm
, ®1
,
258 "end of charge current threshold", "mA");
260 wm831x_battey_apply_config(wm831x
, chg_times
, ARRAY_SIZE(chg_times
),
261 pdata
->timeout
, ®2
,
262 "charger timeout", "min");
264 ret
= wm831x_reg_unlock(wm831x
);
266 dev_err(wm831x
->dev
, "Failed to unlock registers: %d\n", ret
);
270 ret
= wm831x_set_bits(wm831x
, WM831X_CHARGER_CONTROL_1
,
271 WM831X_CHG_ENA_MASK
|
272 WM831X_CHG_FAST_MASK
|
273 WM831X_CHG_ITERM_MASK
,
276 dev_err(wm831x
->dev
, "Failed to set charger control 1: %d\n",
279 ret
= wm831x_set_bits(wm831x
, WM831X_CHARGER_CONTROL_2
,
281 WM831X_CHG_TIME_MASK
|
282 WM831X_CHG_FAST_ILIM_MASK
|
283 WM831X_CHG_TRKL_ILIM_MASK
|
284 WM831X_CHG_VSEL_MASK
,
287 dev_err(wm831x
->dev
, "Failed to set charger control 2: %d\n",
290 wm831x_reg_lock(wm831x
);
293 static int wm831x_bat_check_status(struct wm831x
*wm831x
, int *status
)
297 ret
= wm831x_reg_read(wm831x
, WM831X_SYSTEM_STATUS
);
301 if (ret
& WM831X_PWR_SRC_BATT
) {
302 *status
= POWER_SUPPLY_STATUS_DISCHARGING
;
306 ret
= wm831x_reg_read(wm831x
, WM831X_CHARGER_STATUS
);
310 switch (ret
& WM831X_CHG_STATE_MASK
) {
311 case WM831X_CHG_STATE_OFF
:
312 *status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
314 case WM831X_CHG_STATE_TRICKLE
:
315 case WM831X_CHG_STATE_FAST
:
316 *status
= POWER_SUPPLY_STATUS_CHARGING
;
320 *status
= POWER_SUPPLY_STATUS_UNKNOWN
;
327 static int wm831x_bat_check_type(struct wm831x
*wm831x
, int *type
)
331 ret
= wm831x_reg_read(wm831x
, WM831X_CHARGER_STATUS
);
335 switch (ret
& WM831X_CHG_STATE_MASK
) {
336 case WM831X_CHG_STATE_TRICKLE
:
337 case WM831X_CHG_STATE_TRICKLE_OT
:
338 *type
= POWER_SUPPLY_CHARGE_TYPE_TRICKLE
;
340 case WM831X_CHG_STATE_FAST
:
341 case WM831X_CHG_STATE_FAST_OT
:
342 *type
= POWER_SUPPLY_CHARGE_TYPE_FAST
;
345 *type
= POWER_SUPPLY_CHARGE_TYPE_NONE
;
352 static int wm831x_bat_check_health(struct wm831x
*wm831x
, int *health
)
356 ret
= wm831x_reg_read(wm831x
, WM831X_CHARGER_STATUS
);
360 if (ret
& WM831X_BATT_HOT_STS
) {
361 *health
= POWER_SUPPLY_HEALTH_OVERHEAT
;
365 if (ret
& WM831X_BATT_COLD_STS
) {
366 *health
= POWER_SUPPLY_HEALTH_COLD
;
370 if (ret
& WM831X_BATT_OV_STS
) {
371 *health
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
375 switch (ret
& WM831X_CHG_STATE_MASK
) {
376 case WM831X_CHG_STATE_TRICKLE_OT
:
377 case WM831X_CHG_STATE_FAST_OT
:
378 *health
= POWER_SUPPLY_HEALTH_OVERHEAT
;
380 case WM831X_CHG_STATE_DEFECTIVE
:
381 *health
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
384 *health
= POWER_SUPPLY_HEALTH_GOOD
;
391 static int wm831x_bat_get_prop(struct power_supply
*psy
,
392 enum power_supply_property psp
,
393 union power_supply_propval
*val
)
395 struct wm831x_power
*wm831x_power
= dev_get_drvdata(psy
->dev
->parent
);
396 struct wm831x
*wm831x
= wm831x_power
->wm831x
;
400 case POWER_SUPPLY_PROP_STATUS
:
401 ret
= wm831x_bat_check_status(wm831x
, &val
->intval
);
403 case POWER_SUPPLY_PROP_ONLINE
:
404 ret
= wm831x_power_check_online(wm831x
, WM831X_PWR_SRC_BATT
,
407 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
408 ret
= wm831x_power_read_voltage(wm831x
, WM831X_AUX_BATT
, val
);
410 case POWER_SUPPLY_PROP_HEALTH
:
411 ret
= wm831x_bat_check_health(wm831x
, &val
->intval
);
413 case POWER_SUPPLY_PROP_CHARGE_TYPE
:
414 ret
= wm831x_bat_check_type(wm831x
, &val
->intval
);
424 static enum power_supply_property wm831x_bat_props
[] = {
425 POWER_SUPPLY_PROP_STATUS
,
426 POWER_SUPPLY_PROP_ONLINE
,
427 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
428 POWER_SUPPLY_PROP_HEALTH
,
429 POWER_SUPPLY_PROP_CHARGE_TYPE
,
432 static const char *wm831x_bat_irqs
[] = {
443 static irqreturn_t
wm831x_bat_irq(int irq
, void *data
)
445 struct wm831x_power
*wm831x_power
= data
;
446 struct wm831x
*wm831x
= wm831x_power
->wm831x
;
448 dev_dbg(wm831x
->dev
, "Battery status changed: %d\n", irq
);
450 /* The battery charger is autonomous so we don't need to do
451 * anything except kick user space */
452 power_supply_changed(&wm831x_power
->battery
);
458 /*********************************************************************
460 *********************************************************************/
462 static irqreturn_t
wm831x_syslo_irq(int irq
, void *data
)
464 struct wm831x_power
*wm831x_power
= data
;
465 struct wm831x
*wm831x
= wm831x_power
->wm831x
;
467 /* Not much we can actually *do* but tell people for
468 * posterity, we're probably about to run out of power. */
469 dev_crit(wm831x
->dev
, "SYSVDD under voltage\n");
474 static irqreturn_t
wm831x_pwr_src_irq(int irq
, void *data
)
476 struct wm831x_power
*wm831x_power
= data
;
477 struct wm831x
*wm831x
= wm831x_power
->wm831x
;
479 dev_dbg(wm831x
->dev
, "Power source changed\n");
481 /* Just notify for everything - little harm in overnotifying. */
482 power_supply_changed(&wm831x_power
->battery
);
483 power_supply_changed(&wm831x_power
->usb
);
484 power_supply_changed(&wm831x_power
->wall
);
489 static __devinit
int wm831x_power_probe(struct platform_device
*pdev
)
491 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
492 struct wm831x_pdata
*wm831x_pdata
= wm831x
->dev
->platform_data
;
493 struct wm831x_power
*power
;
494 struct power_supply
*usb
;
495 struct power_supply
*battery
;
496 struct power_supply
*wall
;
499 power
= kzalloc(sizeof(struct wm831x_power
), GFP_KERNEL
);
503 power
->wm831x
= wm831x
;
504 platform_set_drvdata(pdev
, power
);
507 battery
= &power
->battery
;
510 if (wm831x_pdata
&& wm831x_pdata
->wm831x_num
) {
511 snprintf(power
->wall_name
, sizeof(power
->wall_name
),
512 "wm831x-wall.%d", wm831x_pdata
->wm831x_num
);
513 snprintf(power
->battery_name
, sizeof(power
->wall_name
),
514 "wm831x-battery.%d", wm831x_pdata
->wm831x_num
);
515 snprintf(power
->usb_name
, sizeof(power
->wall_name
),
516 "wm831x-usb.%d", wm831x_pdata
->wm831x_num
);
518 snprintf(power
->wall_name
, sizeof(power
->wall_name
),
520 snprintf(power
->battery_name
, sizeof(power
->wall_name
),
522 snprintf(power
->usb_name
, sizeof(power
->wall_name
),
526 /* We ignore configuration failures since we can still read back
527 * the status without enabling the charger.
529 wm831x_config_battery(wm831x
);
531 wall
->name
= power
->wall_name
;
532 wall
->type
= POWER_SUPPLY_TYPE_MAINS
;
533 wall
->properties
= wm831x_wall_props
;
534 wall
->num_properties
= ARRAY_SIZE(wm831x_wall_props
);
535 wall
->get_property
= wm831x_wall_get_prop
;
536 ret
= power_supply_register(&pdev
->dev
, wall
);
540 battery
->name
= power
->battery_name
;
541 battery
->properties
= wm831x_bat_props
;
542 battery
->num_properties
= ARRAY_SIZE(wm831x_bat_props
);
543 battery
->get_property
= wm831x_bat_get_prop
;
544 battery
->use_for_apm
= 1;
545 ret
= power_supply_register(&pdev
->dev
, battery
);
549 usb
->name
= power
->usb_name
,
550 usb
->type
= POWER_SUPPLY_TYPE_USB
;
551 usb
->properties
= wm831x_usb_props
;
552 usb
->num_properties
= ARRAY_SIZE(wm831x_usb_props
);
553 usb
->get_property
= wm831x_usb_get_prop
;
554 ret
= power_supply_register(&pdev
->dev
, usb
);
558 irq
= platform_get_irq_byname(pdev
, "SYSLO");
559 ret
= request_threaded_irq(irq
, NULL
, wm831x_syslo_irq
,
560 IRQF_TRIGGER_RISING
, "System power low",
563 dev_err(&pdev
->dev
, "Failed to request SYSLO IRQ %d: %d\n",
568 irq
= platform_get_irq_byname(pdev
, "PWR SRC");
569 ret
= request_threaded_irq(irq
, NULL
, wm831x_pwr_src_irq
,
570 IRQF_TRIGGER_RISING
, "Power source",
573 dev_err(&pdev
->dev
, "Failed to request PWR SRC IRQ %d: %d\n",
578 for (i
= 0; i
< ARRAY_SIZE(wm831x_bat_irqs
); i
++) {
579 irq
= platform_get_irq_byname(pdev
, wm831x_bat_irqs
[i
]);
580 ret
= request_threaded_irq(irq
, NULL
, wm831x_bat_irq
,
586 "Failed to request %s IRQ %d: %d\n",
587 wm831x_bat_irqs
[i
], irq
, ret
);
595 for (; i
>= 0; i
--) {
596 irq
= platform_get_irq_byname(pdev
, wm831x_bat_irqs
[i
]);
597 free_irq(irq
, power
);
599 irq
= platform_get_irq_byname(pdev
, "PWR SRC");
600 free_irq(irq
, power
);
602 irq
= platform_get_irq_byname(pdev
, "SYSLO");
603 free_irq(irq
, power
);
605 power_supply_unregister(usb
);
607 power_supply_unregister(battery
);
609 power_supply_unregister(wall
);
615 static __devexit
int wm831x_power_remove(struct platform_device
*pdev
)
617 struct wm831x_power
*wm831x_power
= platform_get_drvdata(pdev
);
620 for (i
= 0; i
< ARRAY_SIZE(wm831x_bat_irqs
); i
++) {
621 irq
= platform_get_irq_byname(pdev
, wm831x_bat_irqs
[i
]);
622 free_irq(irq
, wm831x_power
);
625 irq
= platform_get_irq_byname(pdev
, "PWR SRC");
626 free_irq(irq
, wm831x_power
);
628 irq
= platform_get_irq_byname(pdev
, "SYSLO");
629 free_irq(irq
, wm831x_power
);
631 power_supply_unregister(&wm831x_power
->battery
);
632 power_supply_unregister(&wm831x_power
->wall
);
633 power_supply_unregister(&wm831x_power
->usb
);
638 static struct platform_driver wm831x_power_driver
= {
639 .probe
= wm831x_power_probe
,
640 .remove
= __devexit_p(wm831x_power_remove
),
642 .name
= "wm831x-power",
646 static int __init
wm831x_power_init(void)
648 return platform_driver_register(&wm831x_power_driver
);
650 module_init(wm831x_power_init
);
652 static void __exit
wm831x_power_exit(void)
654 platform_driver_unregister(&wm831x_power_driver
);
656 module_exit(wm831x_power_exit
);
658 MODULE_DESCRIPTION("Power supply driver for WM831x PMICs");
659 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
660 MODULE_LICENSE("GPL");
661 MODULE_ALIAS("platform:wm831x-power");