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];
33 static int wm831x_power_check_online(struct wm831x
*wm831x
, int supply
,
34 union power_supply_propval
*val
)
38 ret
= wm831x_reg_read(wm831x
, WM831X_SYSTEM_STATUS
);
50 static int wm831x_power_read_voltage(struct wm831x
*wm831x
,
51 enum wm831x_auxadc src
,
52 union power_supply_propval
*val
)
56 ret
= wm831x_auxadc_read_uv(wm831x
, src
);
63 /*********************************************************************
65 *********************************************************************/
66 static int wm831x_wall_get_prop(struct power_supply
*psy
,
67 enum power_supply_property psp
,
68 union power_supply_propval
*val
)
70 struct wm831x_power
*wm831x_power
= dev_get_drvdata(psy
->dev
->parent
);
71 struct wm831x
*wm831x
= wm831x_power
->wm831x
;
75 case POWER_SUPPLY_PROP_ONLINE
:
76 ret
= wm831x_power_check_online(wm831x
, WM831X_PWR_WALL
, val
);
78 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
79 ret
= wm831x_power_read_voltage(wm831x
, WM831X_AUX_WALL
, val
);
89 static enum power_supply_property wm831x_wall_props
[] = {
90 POWER_SUPPLY_PROP_ONLINE
,
91 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
94 /*********************************************************************
96 *********************************************************************/
97 static int wm831x_usb_get_prop(struct power_supply
*psy
,
98 enum power_supply_property psp
,
99 union power_supply_propval
*val
)
101 struct wm831x_power
*wm831x_power
= dev_get_drvdata(psy
->dev
->parent
);
102 struct wm831x
*wm831x
= wm831x_power
->wm831x
;
106 case POWER_SUPPLY_PROP_ONLINE
:
107 ret
= wm831x_power_check_online(wm831x
, WM831X_PWR_USB
, val
);
109 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
110 ret
= wm831x_power_read_voltage(wm831x
, WM831X_AUX_USB
, val
);
120 static enum power_supply_property wm831x_usb_props
[] = {
121 POWER_SUPPLY_PROP_ONLINE
,
122 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
125 /*********************************************************************
127 *********************************************************************/
134 static struct chg_map trickle_ilims
[] = {
135 { 50, 0 << WM831X_CHG_TRKL_ILIM_SHIFT
},
136 { 100, 1 << WM831X_CHG_TRKL_ILIM_SHIFT
},
137 { 150, 2 << WM831X_CHG_TRKL_ILIM_SHIFT
},
138 { 200, 3 << WM831X_CHG_TRKL_ILIM_SHIFT
},
141 static struct chg_map vsels
[] = {
142 { 4050, 0 << WM831X_CHG_VSEL_SHIFT
},
143 { 4100, 1 << WM831X_CHG_VSEL_SHIFT
},
144 { 4150, 2 << WM831X_CHG_VSEL_SHIFT
},
145 { 4200, 3 << WM831X_CHG_VSEL_SHIFT
},
148 static struct chg_map fast_ilims
[] = {
149 { 0, 0 << WM831X_CHG_FAST_ILIM_SHIFT
},
150 { 50, 1 << WM831X_CHG_FAST_ILIM_SHIFT
},
151 { 100, 2 << WM831X_CHG_FAST_ILIM_SHIFT
},
152 { 150, 3 << WM831X_CHG_FAST_ILIM_SHIFT
},
153 { 200, 4 << WM831X_CHG_FAST_ILIM_SHIFT
},
154 { 250, 5 << WM831X_CHG_FAST_ILIM_SHIFT
},
155 { 300, 6 << WM831X_CHG_FAST_ILIM_SHIFT
},
156 { 350, 7 << WM831X_CHG_FAST_ILIM_SHIFT
},
157 { 400, 8 << WM831X_CHG_FAST_ILIM_SHIFT
},
158 { 450, 9 << WM831X_CHG_FAST_ILIM_SHIFT
},
159 { 500, 10 << WM831X_CHG_FAST_ILIM_SHIFT
},
160 { 600, 11 << WM831X_CHG_FAST_ILIM_SHIFT
},
161 { 700, 12 << WM831X_CHG_FAST_ILIM_SHIFT
},
162 { 800, 13 << WM831X_CHG_FAST_ILIM_SHIFT
},
163 { 900, 14 << WM831X_CHG_FAST_ILIM_SHIFT
},
164 { 1000, 15 << WM831X_CHG_FAST_ILIM_SHIFT
},
167 static struct chg_map eoc_iterms
[] = {
168 { 20, 0 << WM831X_CHG_ITERM_SHIFT
},
169 { 30, 1 << WM831X_CHG_ITERM_SHIFT
},
170 { 40, 2 << WM831X_CHG_ITERM_SHIFT
},
171 { 50, 3 << WM831X_CHG_ITERM_SHIFT
},
172 { 60, 4 << WM831X_CHG_ITERM_SHIFT
},
173 { 70, 5 << WM831X_CHG_ITERM_SHIFT
},
174 { 80, 6 << WM831X_CHG_ITERM_SHIFT
},
175 { 90, 7 << WM831X_CHG_ITERM_SHIFT
},
178 static struct chg_map chg_times
[] = {
179 { 60, 0 << WM831X_CHG_TIME_SHIFT
},
180 { 90, 1 << WM831X_CHG_TIME_SHIFT
},
181 { 120, 2 << WM831X_CHG_TIME_SHIFT
},
182 { 150, 3 << WM831X_CHG_TIME_SHIFT
},
183 { 180, 4 << WM831X_CHG_TIME_SHIFT
},
184 { 210, 5 << WM831X_CHG_TIME_SHIFT
},
185 { 240, 6 << WM831X_CHG_TIME_SHIFT
},
186 { 270, 7 << WM831X_CHG_TIME_SHIFT
},
187 { 300, 8 << WM831X_CHG_TIME_SHIFT
},
188 { 330, 9 << WM831X_CHG_TIME_SHIFT
},
189 { 360, 10 << WM831X_CHG_TIME_SHIFT
},
190 { 390, 11 << WM831X_CHG_TIME_SHIFT
},
191 { 420, 12 << WM831X_CHG_TIME_SHIFT
},
192 { 450, 13 << WM831X_CHG_TIME_SHIFT
},
193 { 480, 14 << WM831X_CHG_TIME_SHIFT
},
194 { 510, 15 << WM831X_CHG_TIME_SHIFT
},
197 static void wm831x_battey_apply_config(struct wm831x
*wm831x
,
198 struct chg_map
*map
, int count
, int val
,
199 int *reg
, const char *name
,
204 for (i
= 0; i
< count
; i
++)
205 if (val
== map
[i
].val
)
208 dev_err(wm831x
->dev
, "Invalid %s %d%s\n",
211 *reg
|= map
[i
].reg_val
;
212 dev_dbg(wm831x
->dev
, "Set %s of %d%s\n", name
, val
, units
);
216 static void wm831x_config_battery(struct wm831x
*wm831x
)
218 struct wm831x_pdata
*wm831x_pdata
= wm831x
->dev
->platform_data
;
219 struct wm831x_battery_pdata
*pdata
;
222 if (!wm831x_pdata
|| !wm831x_pdata
->battery
) {
223 dev_warn(wm831x
->dev
,
224 "No battery charger configuration\n");
228 pdata
= wm831x_pdata
->battery
;
233 if (!pdata
->enable
) {
234 dev_info(wm831x
->dev
, "Battery charger disabled\n");
238 reg1
|= WM831X_CHG_ENA
;
240 reg2
|= WM831X_CHG_OFF_MSK
;
241 if (pdata
->fast_enable
)
242 reg1
|= WM831X_CHG_FAST
;
244 wm831x_battey_apply_config(wm831x
, trickle_ilims
,
245 ARRAY_SIZE(trickle_ilims
),
246 pdata
->trickle_ilim
, ®2
,
247 "trickle charge current limit", "mA");
249 wm831x_battey_apply_config(wm831x
, vsels
, ARRAY_SIZE(vsels
),
251 "target voltage", "mV");
253 wm831x_battey_apply_config(wm831x
, fast_ilims
, ARRAY_SIZE(fast_ilims
),
254 pdata
->fast_ilim
, ®2
,
255 "fast charge current limit", "mA");
257 wm831x_battey_apply_config(wm831x
, eoc_iterms
, ARRAY_SIZE(eoc_iterms
),
258 pdata
->eoc_iterm
, ®1
,
259 "end of charge current threshold", "mA");
261 wm831x_battey_apply_config(wm831x
, chg_times
, ARRAY_SIZE(chg_times
),
262 pdata
->timeout
, ®2
,
263 "charger timeout", "min");
265 ret
= wm831x_reg_unlock(wm831x
);
267 dev_err(wm831x
->dev
, "Failed to unlock registers: %d\n", ret
);
271 ret
= wm831x_set_bits(wm831x
, WM831X_CHARGER_CONTROL_1
,
272 WM831X_CHG_ENA_MASK
|
273 WM831X_CHG_FAST_MASK
|
274 WM831X_CHG_ITERM_MASK
,
277 dev_err(wm831x
->dev
, "Failed to set charger control 1: %d\n",
280 ret
= wm831x_set_bits(wm831x
, WM831X_CHARGER_CONTROL_2
,
282 WM831X_CHG_TIME_MASK
|
283 WM831X_CHG_FAST_ILIM_MASK
|
284 WM831X_CHG_TRKL_ILIM_MASK
|
285 WM831X_CHG_VSEL_MASK
,
288 dev_err(wm831x
->dev
, "Failed to set charger control 2: %d\n",
291 wm831x_reg_lock(wm831x
);
294 static int wm831x_bat_check_status(struct wm831x
*wm831x
, int *status
)
298 ret
= wm831x_reg_read(wm831x
, WM831X_SYSTEM_STATUS
);
302 if (ret
& WM831X_PWR_SRC_BATT
) {
303 *status
= POWER_SUPPLY_STATUS_DISCHARGING
;
307 ret
= wm831x_reg_read(wm831x
, WM831X_CHARGER_STATUS
);
311 switch (ret
& WM831X_CHG_STATE_MASK
) {
312 case WM831X_CHG_STATE_OFF
:
313 *status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
315 case WM831X_CHG_STATE_TRICKLE
:
316 case WM831X_CHG_STATE_FAST
:
317 *status
= POWER_SUPPLY_STATUS_CHARGING
;
321 *status
= POWER_SUPPLY_STATUS_UNKNOWN
;
328 static int wm831x_bat_check_type(struct wm831x
*wm831x
, int *type
)
332 ret
= wm831x_reg_read(wm831x
, WM831X_CHARGER_STATUS
);
336 switch (ret
& WM831X_CHG_STATE_MASK
) {
337 case WM831X_CHG_STATE_TRICKLE
:
338 case WM831X_CHG_STATE_TRICKLE_OT
:
339 *type
= POWER_SUPPLY_CHARGE_TYPE_TRICKLE
;
341 case WM831X_CHG_STATE_FAST
:
342 case WM831X_CHG_STATE_FAST_OT
:
343 *type
= POWER_SUPPLY_CHARGE_TYPE_FAST
;
346 *type
= POWER_SUPPLY_CHARGE_TYPE_NONE
;
353 static int wm831x_bat_check_health(struct wm831x
*wm831x
, int *health
)
357 ret
= wm831x_reg_read(wm831x
, WM831X_CHARGER_STATUS
);
361 if (ret
& WM831X_BATT_HOT_STS
) {
362 *health
= POWER_SUPPLY_HEALTH_OVERHEAT
;
366 if (ret
& WM831X_BATT_COLD_STS
) {
367 *health
= POWER_SUPPLY_HEALTH_COLD
;
371 if (ret
& WM831X_BATT_OV_STS
) {
372 *health
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
376 switch (ret
& WM831X_CHG_STATE_MASK
) {
377 case WM831X_CHG_STATE_TRICKLE_OT
:
378 case WM831X_CHG_STATE_FAST_OT
:
379 *health
= POWER_SUPPLY_HEALTH_OVERHEAT
;
381 case WM831X_CHG_STATE_DEFECTIVE
:
382 *health
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
385 *health
= POWER_SUPPLY_HEALTH_GOOD
;
392 static int wm831x_bat_get_prop(struct power_supply
*psy
,
393 enum power_supply_property psp
,
394 union power_supply_propval
*val
)
396 struct wm831x_power
*wm831x_power
= dev_get_drvdata(psy
->dev
->parent
);
397 struct wm831x
*wm831x
= wm831x_power
->wm831x
;
401 case POWER_SUPPLY_PROP_STATUS
:
402 ret
= wm831x_bat_check_status(wm831x
, &val
->intval
);
404 case POWER_SUPPLY_PROP_ONLINE
:
405 ret
= wm831x_power_check_online(wm831x
, WM831X_PWR_SRC_BATT
,
408 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
409 ret
= wm831x_power_read_voltage(wm831x
, WM831X_AUX_BATT
, val
);
411 case POWER_SUPPLY_PROP_HEALTH
:
412 ret
= wm831x_bat_check_health(wm831x
, &val
->intval
);
414 case POWER_SUPPLY_PROP_CHARGE_TYPE
:
415 ret
= wm831x_bat_check_type(wm831x
, &val
->intval
);
425 static enum power_supply_property wm831x_bat_props
[] = {
426 POWER_SUPPLY_PROP_STATUS
,
427 POWER_SUPPLY_PROP_ONLINE
,
428 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
429 POWER_SUPPLY_PROP_HEALTH
,
430 POWER_SUPPLY_PROP_CHARGE_TYPE
,
433 static const char *wm831x_bat_irqs
[] = {
444 static irqreturn_t
wm831x_bat_irq(int irq
, void *data
)
446 struct wm831x_power
*wm831x_power
= data
;
447 struct wm831x
*wm831x
= wm831x_power
->wm831x
;
449 dev_dbg(wm831x
->dev
, "Battery status changed: %d\n", irq
);
451 /* The battery charger is autonomous so we don't need to do
452 * anything except kick user space */
453 if (wm831x_power
->have_battery
)
454 power_supply_changed(&wm831x_power
->battery
);
460 /*********************************************************************
462 *********************************************************************/
464 static irqreturn_t
wm831x_syslo_irq(int irq
, void *data
)
466 struct wm831x_power
*wm831x_power
= data
;
467 struct wm831x
*wm831x
= wm831x_power
->wm831x
;
469 /* Not much we can actually *do* but tell people for
470 * posterity, we're probably about to run out of power. */
471 dev_crit(wm831x
->dev
, "SYSVDD under voltage\n");
476 static irqreturn_t
wm831x_pwr_src_irq(int irq
, void *data
)
478 struct wm831x_power
*wm831x_power
= data
;
479 struct wm831x
*wm831x
= wm831x_power
->wm831x
;
481 dev_dbg(wm831x
->dev
, "Power source changed\n");
483 /* Just notify for everything - little harm in overnotifying. */
484 if (wm831x_power
->have_battery
)
485 power_supply_changed(&wm831x_power
->battery
);
486 power_supply_changed(&wm831x_power
->usb
);
487 power_supply_changed(&wm831x_power
->wall
);
492 static __devinit
int wm831x_power_probe(struct platform_device
*pdev
)
494 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
495 struct wm831x_pdata
*wm831x_pdata
= wm831x
->dev
->platform_data
;
496 struct wm831x_power
*power
;
497 struct power_supply
*usb
;
498 struct power_supply
*battery
;
499 struct power_supply
*wall
;
502 power
= kzalloc(sizeof(struct wm831x_power
), GFP_KERNEL
);
506 power
->wm831x
= wm831x
;
507 platform_set_drvdata(pdev
, power
);
510 battery
= &power
->battery
;
513 if (wm831x_pdata
&& wm831x_pdata
->wm831x_num
) {
514 snprintf(power
->wall_name
, sizeof(power
->wall_name
),
515 "wm831x-wall.%d", wm831x_pdata
->wm831x_num
);
516 snprintf(power
->battery_name
, sizeof(power
->wall_name
),
517 "wm831x-battery.%d", wm831x_pdata
->wm831x_num
);
518 snprintf(power
->usb_name
, sizeof(power
->wall_name
),
519 "wm831x-usb.%d", wm831x_pdata
->wm831x_num
);
521 snprintf(power
->wall_name
, sizeof(power
->wall_name
),
523 snprintf(power
->battery_name
, sizeof(power
->wall_name
),
525 snprintf(power
->usb_name
, sizeof(power
->wall_name
),
529 /* We ignore configuration failures since we can still read back
530 * the status without enabling the charger.
532 wm831x_config_battery(wm831x
);
534 wall
->name
= power
->wall_name
;
535 wall
->type
= POWER_SUPPLY_TYPE_MAINS
;
536 wall
->properties
= wm831x_wall_props
;
537 wall
->num_properties
= ARRAY_SIZE(wm831x_wall_props
);
538 wall
->get_property
= wm831x_wall_get_prop
;
539 ret
= power_supply_register(&pdev
->dev
, wall
);
543 usb
->name
= power
->usb_name
,
544 usb
->type
= POWER_SUPPLY_TYPE_USB
;
545 usb
->properties
= wm831x_usb_props
;
546 usb
->num_properties
= ARRAY_SIZE(wm831x_usb_props
);
547 usb
->get_property
= wm831x_usb_get_prop
;
548 ret
= power_supply_register(&pdev
->dev
, usb
);
552 ret
= wm831x_reg_read(wm831x
, WM831X_CHARGER_CONTROL_1
);
555 power
->have_battery
= ret
& WM831X_CHG_ENA
;
557 if (power
->have_battery
) {
558 battery
->name
= power
->battery_name
;
559 battery
->properties
= wm831x_bat_props
;
560 battery
->num_properties
= ARRAY_SIZE(wm831x_bat_props
);
561 battery
->get_property
= wm831x_bat_get_prop
;
562 battery
->use_for_apm
= 1;
563 ret
= power_supply_register(&pdev
->dev
, battery
);
568 irq
= platform_get_irq_byname(pdev
, "SYSLO");
569 ret
= request_threaded_irq(irq
, NULL
, wm831x_syslo_irq
,
570 IRQF_TRIGGER_RISING
, "System power low",
573 dev_err(&pdev
->dev
, "Failed to request SYSLO IRQ %d: %d\n",
578 irq
= platform_get_irq_byname(pdev
, "PWR SRC");
579 ret
= request_threaded_irq(irq
, NULL
, wm831x_pwr_src_irq
,
580 IRQF_TRIGGER_RISING
, "Power source",
583 dev_err(&pdev
->dev
, "Failed to request PWR SRC IRQ %d: %d\n",
588 for (i
= 0; i
< ARRAY_SIZE(wm831x_bat_irqs
); i
++) {
589 irq
= platform_get_irq_byname(pdev
, wm831x_bat_irqs
[i
]);
590 ret
= request_threaded_irq(irq
, NULL
, wm831x_bat_irq
,
596 "Failed to request %s IRQ %d: %d\n",
597 wm831x_bat_irqs
[i
], irq
, ret
);
605 for (; i
>= 0; i
--) {
606 irq
= platform_get_irq_byname(pdev
, wm831x_bat_irqs
[i
]);
607 free_irq(irq
, power
);
609 irq
= platform_get_irq_byname(pdev
, "PWR SRC");
610 free_irq(irq
, power
);
612 irq
= platform_get_irq_byname(pdev
, "SYSLO");
613 free_irq(irq
, power
);
615 if (power
->have_battery
)
616 power_supply_unregister(battery
);
618 power_supply_unregister(usb
);
620 power_supply_unregister(wall
);
626 static __devexit
int wm831x_power_remove(struct platform_device
*pdev
)
628 struct wm831x_power
*wm831x_power
= platform_get_drvdata(pdev
);
631 for (i
= 0; i
< ARRAY_SIZE(wm831x_bat_irqs
); i
++) {
632 irq
= platform_get_irq_byname(pdev
, wm831x_bat_irqs
[i
]);
633 free_irq(irq
, wm831x_power
);
636 irq
= platform_get_irq_byname(pdev
, "PWR SRC");
637 free_irq(irq
, wm831x_power
);
639 irq
= platform_get_irq_byname(pdev
, "SYSLO");
640 free_irq(irq
, wm831x_power
);
642 if (wm831x_power
->have_battery
)
643 power_supply_unregister(&wm831x_power
->battery
);
644 power_supply_unregister(&wm831x_power
->wall
);
645 power_supply_unregister(&wm831x_power
->usb
);
650 static struct platform_driver wm831x_power_driver
= {
651 .probe
= wm831x_power_probe
,
652 .remove
= __devexit_p(wm831x_power_remove
),
654 .name
= "wm831x-power",
658 module_platform_driver(wm831x_power_driver
);
660 MODULE_DESCRIPTION("Power supply driver for WM831x PMICs");
661 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
662 MODULE_LICENSE("GPL");
663 MODULE_ALIAS("platform:wm831x-power");