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
;
27 struct power_supply_desc wall_desc
;
28 struct power_supply_desc usb_desc
;
29 struct power_supply_desc battery_desc
;
32 char battery_name
[20];
36 static int wm831x_power_check_online(struct wm831x
*wm831x
, int supply
,
37 union power_supply_propval
*val
)
41 ret
= wm831x_reg_read(wm831x
, WM831X_SYSTEM_STATUS
);
53 static int wm831x_power_read_voltage(struct wm831x
*wm831x
,
54 enum wm831x_auxadc src
,
55 union power_supply_propval
*val
)
59 ret
= wm831x_auxadc_read_uv(wm831x
, src
);
66 /*********************************************************************
68 *********************************************************************/
69 static int wm831x_wall_get_prop(struct power_supply
*psy
,
70 enum power_supply_property psp
,
71 union power_supply_propval
*val
)
73 struct wm831x_power
*wm831x_power
= dev_get_drvdata(psy
->dev
.parent
);
74 struct wm831x
*wm831x
= wm831x_power
->wm831x
;
78 case POWER_SUPPLY_PROP_ONLINE
:
79 ret
= wm831x_power_check_online(wm831x
, WM831X_PWR_WALL
, val
);
81 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
82 ret
= wm831x_power_read_voltage(wm831x
, WM831X_AUX_WALL
, val
);
92 static enum power_supply_property wm831x_wall_props
[] = {
93 POWER_SUPPLY_PROP_ONLINE
,
94 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
97 /*********************************************************************
99 *********************************************************************/
100 static int wm831x_usb_get_prop(struct power_supply
*psy
,
101 enum power_supply_property psp
,
102 union power_supply_propval
*val
)
104 struct wm831x_power
*wm831x_power
= dev_get_drvdata(psy
->dev
.parent
);
105 struct wm831x
*wm831x
= wm831x_power
->wm831x
;
109 case POWER_SUPPLY_PROP_ONLINE
:
110 ret
= wm831x_power_check_online(wm831x
, WM831X_PWR_USB
, val
);
112 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
113 ret
= wm831x_power_read_voltage(wm831x
, WM831X_AUX_USB
, val
);
123 static enum power_supply_property wm831x_usb_props
[] = {
124 POWER_SUPPLY_PROP_ONLINE
,
125 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
128 /*********************************************************************
130 *********************************************************************/
137 static struct chg_map trickle_ilims
[] = {
138 { 50, 0 << WM831X_CHG_TRKL_ILIM_SHIFT
},
139 { 100, 1 << WM831X_CHG_TRKL_ILIM_SHIFT
},
140 { 150, 2 << WM831X_CHG_TRKL_ILIM_SHIFT
},
141 { 200, 3 << WM831X_CHG_TRKL_ILIM_SHIFT
},
144 static struct chg_map vsels
[] = {
145 { 4050, 0 << WM831X_CHG_VSEL_SHIFT
},
146 { 4100, 1 << WM831X_CHG_VSEL_SHIFT
},
147 { 4150, 2 << WM831X_CHG_VSEL_SHIFT
},
148 { 4200, 3 << WM831X_CHG_VSEL_SHIFT
},
151 static struct chg_map fast_ilims
[] = {
152 { 0, 0 << WM831X_CHG_FAST_ILIM_SHIFT
},
153 { 50, 1 << WM831X_CHG_FAST_ILIM_SHIFT
},
154 { 100, 2 << WM831X_CHG_FAST_ILIM_SHIFT
},
155 { 150, 3 << WM831X_CHG_FAST_ILIM_SHIFT
},
156 { 200, 4 << WM831X_CHG_FAST_ILIM_SHIFT
},
157 { 250, 5 << WM831X_CHG_FAST_ILIM_SHIFT
},
158 { 300, 6 << WM831X_CHG_FAST_ILIM_SHIFT
},
159 { 350, 7 << WM831X_CHG_FAST_ILIM_SHIFT
},
160 { 400, 8 << WM831X_CHG_FAST_ILIM_SHIFT
},
161 { 450, 9 << WM831X_CHG_FAST_ILIM_SHIFT
},
162 { 500, 10 << WM831X_CHG_FAST_ILIM_SHIFT
},
163 { 600, 11 << WM831X_CHG_FAST_ILIM_SHIFT
},
164 { 700, 12 << WM831X_CHG_FAST_ILIM_SHIFT
},
165 { 800, 13 << WM831X_CHG_FAST_ILIM_SHIFT
},
166 { 900, 14 << WM831X_CHG_FAST_ILIM_SHIFT
},
167 { 1000, 15 << WM831X_CHG_FAST_ILIM_SHIFT
},
170 static struct chg_map eoc_iterms
[] = {
171 { 20, 0 << WM831X_CHG_ITERM_SHIFT
},
172 { 30, 1 << WM831X_CHG_ITERM_SHIFT
},
173 { 40, 2 << WM831X_CHG_ITERM_SHIFT
},
174 { 50, 3 << WM831X_CHG_ITERM_SHIFT
},
175 { 60, 4 << WM831X_CHG_ITERM_SHIFT
},
176 { 70, 5 << WM831X_CHG_ITERM_SHIFT
},
177 { 80, 6 << WM831X_CHG_ITERM_SHIFT
},
178 { 90, 7 << WM831X_CHG_ITERM_SHIFT
},
181 static struct chg_map chg_times
[] = {
182 { 60, 0 << WM831X_CHG_TIME_SHIFT
},
183 { 90, 1 << WM831X_CHG_TIME_SHIFT
},
184 { 120, 2 << WM831X_CHG_TIME_SHIFT
},
185 { 150, 3 << WM831X_CHG_TIME_SHIFT
},
186 { 180, 4 << WM831X_CHG_TIME_SHIFT
},
187 { 210, 5 << WM831X_CHG_TIME_SHIFT
},
188 { 240, 6 << WM831X_CHG_TIME_SHIFT
},
189 { 270, 7 << WM831X_CHG_TIME_SHIFT
},
190 { 300, 8 << WM831X_CHG_TIME_SHIFT
},
191 { 330, 9 << WM831X_CHG_TIME_SHIFT
},
192 { 360, 10 << WM831X_CHG_TIME_SHIFT
},
193 { 390, 11 << WM831X_CHG_TIME_SHIFT
},
194 { 420, 12 << WM831X_CHG_TIME_SHIFT
},
195 { 450, 13 << WM831X_CHG_TIME_SHIFT
},
196 { 480, 14 << WM831X_CHG_TIME_SHIFT
},
197 { 510, 15 << WM831X_CHG_TIME_SHIFT
},
200 static void wm831x_battey_apply_config(struct wm831x
*wm831x
,
201 struct chg_map
*map
, int count
, int val
,
202 int *reg
, const char *name
,
207 for (i
= 0; i
< count
; i
++)
208 if (val
== map
[i
].val
)
211 dev_err(wm831x
->dev
, "Invalid %s %d%s\n",
214 *reg
|= map
[i
].reg_val
;
215 dev_dbg(wm831x
->dev
, "Set %s of %d%s\n", name
, val
, units
);
219 static void wm831x_config_battery(struct wm831x
*wm831x
)
221 struct wm831x_pdata
*wm831x_pdata
= wm831x
->dev
->platform_data
;
222 struct wm831x_battery_pdata
*pdata
;
225 if (!wm831x_pdata
|| !wm831x_pdata
->battery
) {
226 dev_warn(wm831x
->dev
,
227 "No battery charger configuration\n");
231 pdata
= wm831x_pdata
->battery
;
236 if (!pdata
->enable
) {
237 dev_info(wm831x
->dev
, "Battery charger disabled\n");
241 reg1
|= WM831X_CHG_ENA
;
243 reg2
|= WM831X_CHG_OFF_MSK
;
244 if (pdata
->fast_enable
)
245 reg1
|= WM831X_CHG_FAST
;
247 wm831x_battey_apply_config(wm831x
, trickle_ilims
,
248 ARRAY_SIZE(trickle_ilims
),
249 pdata
->trickle_ilim
, ®2
,
250 "trickle charge current limit", "mA");
252 wm831x_battey_apply_config(wm831x
, vsels
, ARRAY_SIZE(vsels
),
254 "target voltage", "mV");
256 wm831x_battey_apply_config(wm831x
, fast_ilims
, ARRAY_SIZE(fast_ilims
),
257 pdata
->fast_ilim
, ®2
,
258 "fast charge current limit", "mA");
260 wm831x_battey_apply_config(wm831x
, eoc_iterms
, ARRAY_SIZE(eoc_iterms
),
261 pdata
->eoc_iterm
, ®1
,
262 "end of charge current threshold", "mA");
264 wm831x_battey_apply_config(wm831x
, chg_times
, ARRAY_SIZE(chg_times
),
265 pdata
->timeout
, ®2
,
266 "charger timeout", "min");
268 ret
= wm831x_reg_unlock(wm831x
);
270 dev_err(wm831x
->dev
, "Failed to unlock registers: %d\n", ret
);
274 ret
= wm831x_set_bits(wm831x
, WM831X_CHARGER_CONTROL_1
,
275 WM831X_CHG_ENA_MASK
|
276 WM831X_CHG_FAST_MASK
|
277 WM831X_CHG_ITERM_MASK
,
280 dev_err(wm831x
->dev
, "Failed to set charger control 1: %d\n",
283 ret
= wm831x_set_bits(wm831x
, WM831X_CHARGER_CONTROL_2
,
285 WM831X_CHG_TIME_MASK
|
286 WM831X_CHG_FAST_ILIM_MASK
|
287 WM831X_CHG_TRKL_ILIM_MASK
|
288 WM831X_CHG_VSEL_MASK
,
291 dev_err(wm831x
->dev
, "Failed to set charger control 2: %d\n",
294 wm831x_reg_lock(wm831x
);
297 static int wm831x_bat_check_status(struct wm831x
*wm831x
, int *status
)
301 ret
= wm831x_reg_read(wm831x
, WM831X_SYSTEM_STATUS
);
305 if (ret
& WM831X_PWR_SRC_BATT
) {
306 *status
= POWER_SUPPLY_STATUS_DISCHARGING
;
310 ret
= wm831x_reg_read(wm831x
, WM831X_CHARGER_STATUS
);
314 switch (ret
& WM831X_CHG_STATE_MASK
) {
315 case WM831X_CHG_STATE_OFF
:
316 *status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
318 case WM831X_CHG_STATE_TRICKLE
:
319 case WM831X_CHG_STATE_FAST
:
320 *status
= POWER_SUPPLY_STATUS_CHARGING
;
324 *status
= POWER_SUPPLY_STATUS_UNKNOWN
;
331 static int wm831x_bat_check_type(struct wm831x
*wm831x
, int *type
)
335 ret
= wm831x_reg_read(wm831x
, WM831X_CHARGER_STATUS
);
339 switch (ret
& WM831X_CHG_STATE_MASK
) {
340 case WM831X_CHG_STATE_TRICKLE
:
341 case WM831X_CHG_STATE_TRICKLE_OT
:
342 *type
= POWER_SUPPLY_CHARGE_TYPE_TRICKLE
;
344 case WM831X_CHG_STATE_FAST
:
345 case WM831X_CHG_STATE_FAST_OT
:
346 *type
= POWER_SUPPLY_CHARGE_TYPE_FAST
;
349 *type
= POWER_SUPPLY_CHARGE_TYPE_NONE
;
356 static int wm831x_bat_check_health(struct wm831x
*wm831x
, int *health
)
360 ret
= wm831x_reg_read(wm831x
, WM831X_CHARGER_STATUS
);
364 if (ret
& WM831X_BATT_HOT_STS
) {
365 *health
= POWER_SUPPLY_HEALTH_OVERHEAT
;
369 if (ret
& WM831X_BATT_COLD_STS
) {
370 *health
= POWER_SUPPLY_HEALTH_COLD
;
374 if (ret
& WM831X_BATT_OV_STS
) {
375 *health
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
379 switch (ret
& WM831X_CHG_STATE_MASK
) {
380 case WM831X_CHG_STATE_TRICKLE_OT
:
381 case WM831X_CHG_STATE_FAST_OT
:
382 *health
= POWER_SUPPLY_HEALTH_OVERHEAT
;
384 case WM831X_CHG_STATE_DEFECTIVE
:
385 *health
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
388 *health
= POWER_SUPPLY_HEALTH_GOOD
;
395 static int wm831x_bat_get_prop(struct power_supply
*psy
,
396 enum power_supply_property psp
,
397 union power_supply_propval
*val
)
399 struct wm831x_power
*wm831x_power
= dev_get_drvdata(psy
->dev
.parent
);
400 struct wm831x
*wm831x
= wm831x_power
->wm831x
;
404 case POWER_SUPPLY_PROP_STATUS
:
405 ret
= wm831x_bat_check_status(wm831x
, &val
->intval
);
407 case POWER_SUPPLY_PROP_ONLINE
:
408 ret
= wm831x_power_check_online(wm831x
, WM831X_PWR_SRC_BATT
,
411 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
412 ret
= wm831x_power_read_voltage(wm831x
, WM831X_AUX_BATT
, val
);
414 case POWER_SUPPLY_PROP_HEALTH
:
415 ret
= wm831x_bat_check_health(wm831x
, &val
->intval
);
417 case POWER_SUPPLY_PROP_CHARGE_TYPE
:
418 ret
= wm831x_bat_check_type(wm831x
, &val
->intval
);
428 static enum power_supply_property wm831x_bat_props
[] = {
429 POWER_SUPPLY_PROP_STATUS
,
430 POWER_SUPPLY_PROP_ONLINE
,
431 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
432 POWER_SUPPLY_PROP_HEALTH
,
433 POWER_SUPPLY_PROP_CHARGE_TYPE
,
436 static const char *wm831x_bat_irqs
[] = {
447 static irqreturn_t
wm831x_bat_irq(int irq
, void *data
)
449 struct wm831x_power
*wm831x_power
= data
;
450 struct wm831x
*wm831x
= wm831x_power
->wm831x
;
452 dev_dbg(wm831x
->dev
, "Battery status changed: %d\n", irq
);
454 /* The battery charger is autonomous so we don't need to do
455 * anything except kick user space */
456 if (wm831x_power
->have_battery
)
457 power_supply_changed(wm831x_power
->battery
);
463 /*********************************************************************
465 *********************************************************************/
467 static irqreturn_t
wm831x_syslo_irq(int irq
, void *data
)
469 struct wm831x_power
*wm831x_power
= data
;
470 struct wm831x
*wm831x
= wm831x_power
->wm831x
;
472 /* Not much we can actually *do* but tell people for
473 * posterity, we're probably about to run out of power. */
474 dev_crit(wm831x
->dev
, "SYSVDD under voltage\n");
479 static irqreturn_t
wm831x_pwr_src_irq(int irq
, void *data
)
481 struct wm831x_power
*wm831x_power
= data
;
482 struct wm831x
*wm831x
= wm831x_power
->wm831x
;
484 dev_dbg(wm831x
->dev
, "Power source changed\n");
486 /* Just notify for everything - little harm in overnotifying. */
487 if (wm831x_power
->have_battery
)
488 power_supply_changed(wm831x_power
->battery
);
489 power_supply_changed(wm831x_power
->usb
);
490 power_supply_changed(wm831x_power
->wall
);
495 static int wm831x_power_probe(struct platform_device
*pdev
)
497 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
498 struct wm831x_pdata
*wm831x_pdata
= wm831x
->dev
->platform_data
;
499 struct wm831x_power
*power
;
502 power
= kzalloc(sizeof(struct wm831x_power
), GFP_KERNEL
);
506 power
->wm831x
= wm831x
;
507 platform_set_drvdata(pdev
, power
);
509 if (wm831x_pdata
&& wm831x_pdata
->wm831x_num
) {
510 snprintf(power
->wall_name
, sizeof(power
->wall_name
),
511 "wm831x-wall.%d", wm831x_pdata
->wm831x_num
);
512 snprintf(power
->battery_name
, sizeof(power
->wall_name
),
513 "wm831x-battery.%d", wm831x_pdata
->wm831x_num
);
514 snprintf(power
->usb_name
, sizeof(power
->wall_name
),
515 "wm831x-usb.%d", wm831x_pdata
->wm831x_num
);
517 snprintf(power
->wall_name
, sizeof(power
->wall_name
),
519 snprintf(power
->battery_name
, sizeof(power
->wall_name
),
521 snprintf(power
->usb_name
, sizeof(power
->wall_name
),
525 /* We ignore configuration failures since we can still read back
526 * the status without enabling the charger.
528 wm831x_config_battery(wm831x
);
530 power
->wall_desc
.name
= power
->wall_name
;
531 power
->wall_desc
.type
= POWER_SUPPLY_TYPE_MAINS
;
532 power
->wall_desc
.properties
= wm831x_wall_props
;
533 power
->wall_desc
.num_properties
= ARRAY_SIZE(wm831x_wall_props
);
534 power
->wall_desc
.get_property
= wm831x_wall_get_prop
;
535 power
->wall
= power_supply_register(&pdev
->dev
, &power
->wall_desc
,
537 if (IS_ERR(power
->wall
)) {
538 ret
= PTR_ERR(power
->wall
);
542 power
->usb_desc
.name
= power
->usb_name
,
543 power
->usb_desc
.type
= POWER_SUPPLY_TYPE_USB
;
544 power
->usb_desc
.properties
= wm831x_usb_props
;
545 power
->usb_desc
.num_properties
= ARRAY_SIZE(wm831x_usb_props
);
546 power
->usb_desc
.get_property
= wm831x_usb_get_prop
;
547 power
->usb
= power_supply_register(&pdev
->dev
, &power
->usb_desc
, NULL
);
548 if (IS_ERR(power
->usb
)) {
549 ret
= PTR_ERR(power
->usb
);
553 ret
= wm831x_reg_read(wm831x
, WM831X_CHARGER_CONTROL_1
);
556 power
->have_battery
= ret
& WM831X_CHG_ENA
;
558 if (power
->have_battery
) {
559 power
->battery_desc
.name
= power
->battery_name
;
560 power
->battery_desc
.properties
= wm831x_bat_props
;
561 power
->battery_desc
.num_properties
= ARRAY_SIZE(wm831x_bat_props
);
562 power
->battery_desc
.get_property
= wm831x_bat_get_prop
;
563 power
->battery_desc
.use_for_apm
= 1;
564 power
->battery
= power_supply_register(&pdev
->dev
,
565 &power
->battery_desc
,
567 if (IS_ERR(power
->battery
)) {
568 ret
= PTR_ERR(power
->battery
);
573 irq
= wm831x_irq(wm831x
, platform_get_irq_byname(pdev
, "SYSLO"));
574 ret
= request_threaded_irq(irq
, NULL
, wm831x_syslo_irq
,
575 IRQF_TRIGGER_RISING
, "System power low",
578 dev_err(&pdev
->dev
, "Failed to request SYSLO IRQ %d: %d\n",
583 irq
= wm831x_irq(wm831x
, platform_get_irq_byname(pdev
, "PWR SRC"));
584 ret
= request_threaded_irq(irq
, NULL
, wm831x_pwr_src_irq
,
585 IRQF_TRIGGER_RISING
, "Power source",
588 dev_err(&pdev
->dev
, "Failed to request PWR SRC IRQ %d: %d\n",
593 for (i
= 0; i
< ARRAY_SIZE(wm831x_bat_irqs
); i
++) {
594 irq
= wm831x_irq(wm831x
,
595 platform_get_irq_byname(pdev
,
596 wm831x_bat_irqs
[i
]));
597 ret
= request_threaded_irq(irq
, NULL
, wm831x_bat_irq
,
603 "Failed to request %s IRQ %d: %d\n",
604 wm831x_bat_irqs
[i
], irq
, ret
);
612 for (; i
>= 0; i
--) {
613 irq
= platform_get_irq_byname(pdev
, wm831x_bat_irqs
[i
]);
614 free_irq(irq
, power
);
616 irq
= wm831x_irq(wm831x
, platform_get_irq_byname(pdev
, "PWR SRC"));
617 free_irq(irq
, power
);
619 irq
= wm831x_irq(wm831x
, platform_get_irq_byname(pdev
, "SYSLO"));
620 free_irq(irq
, power
);
622 if (power
->have_battery
)
623 power_supply_unregister(power
->battery
);
625 power_supply_unregister(power
->usb
);
627 power_supply_unregister(power
->wall
);
633 static int wm831x_power_remove(struct platform_device
*pdev
)
635 struct wm831x_power
*wm831x_power
= platform_get_drvdata(pdev
);
636 struct wm831x
*wm831x
= wm831x_power
->wm831x
;
639 for (i
= 0; i
< ARRAY_SIZE(wm831x_bat_irqs
); i
++) {
640 irq
= wm831x_irq(wm831x
,
641 platform_get_irq_byname(pdev
,
642 wm831x_bat_irqs
[i
]));
643 free_irq(irq
, wm831x_power
);
646 irq
= wm831x_irq(wm831x
, platform_get_irq_byname(pdev
, "PWR SRC"));
647 free_irq(irq
, wm831x_power
);
649 irq
= wm831x_irq(wm831x
, platform_get_irq_byname(pdev
, "SYSLO"));
650 free_irq(irq
, wm831x_power
);
652 if (wm831x_power
->have_battery
)
653 power_supply_unregister(wm831x_power
->battery
);
654 power_supply_unregister(wm831x_power
->wall
);
655 power_supply_unregister(wm831x_power
->usb
);
660 static struct platform_driver wm831x_power_driver
= {
661 .probe
= wm831x_power_probe
,
662 .remove
= wm831x_power_remove
,
664 .name
= "wm831x-power",
668 module_platform_driver(wm831x_power_driver
);
670 MODULE_DESCRIPTION("Power supply driver for WM831x PMICs");
671 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
672 MODULE_LICENSE("GPL");
673 MODULE_ALIAS("platform:wm831x-power");