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
= devm_kzalloc(&pdev
->dev
, sizeof(struct wm831x_power
),
507 power
->wm831x
= wm831x
;
508 platform_set_drvdata(pdev
, power
);
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 power
->wall_desc
.name
= power
->wall_name
;
532 power
->wall_desc
.type
= POWER_SUPPLY_TYPE_MAINS
;
533 power
->wall_desc
.properties
= wm831x_wall_props
;
534 power
->wall_desc
.num_properties
= ARRAY_SIZE(wm831x_wall_props
);
535 power
->wall_desc
.get_property
= wm831x_wall_get_prop
;
536 power
->wall
= power_supply_register(&pdev
->dev
, &power
->wall_desc
,
538 if (IS_ERR(power
->wall
)) {
539 ret
= PTR_ERR(power
->wall
);
543 power
->usb_desc
.name
= power
->usb_name
,
544 power
->usb_desc
.type
= POWER_SUPPLY_TYPE_USB
;
545 power
->usb_desc
.properties
= wm831x_usb_props
;
546 power
->usb_desc
.num_properties
= ARRAY_SIZE(wm831x_usb_props
);
547 power
->usb_desc
.get_property
= wm831x_usb_get_prop
;
548 power
->usb
= power_supply_register(&pdev
->dev
, &power
->usb_desc
, NULL
);
549 if (IS_ERR(power
->usb
)) {
550 ret
= PTR_ERR(power
->usb
);
554 ret
= wm831x_reg_read(wm831x
, WM831X_CHARGER_CONTROL_1
);
557 power
->have_battery
= ret
& WM831X_CHG_ENA
;
559 if (power
->have_battery
) {
560 power
->battery_desc
.name
= power
->battery_name
;
561 power
->battery_desc
.properties
= wm831x_bat_props
;
562 power
->battery_desc
.num_properties
= ARRAY_SIZE(wm831x_bat_props
);
563 power
->battery_desc
.get_property
= wm831x_bat_get_prop
;
564 power
->battery_desc
.use_for_apm
= 1;
565 power
->battery
= power_supply_register(&pdev
->dev
,
566 &power
->battery_desc
,
568 if (IS_ERR(power
->battery
)) {
569 ret
= PTR_ERR(power
->battery
);
574 irq
= wm831x_irq(wm831x
, platform_get_irq_byname(pdev
, "SYSLO"));
575 ret
= request_threaded_irq(irq
, NULL
, wm831x_syslo_irq
,
576 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
, "System power low",
579 dev_err(&pdev
->dev
, "Failed to request SYSLO IRQ %d: %d\n",
584 irq
= wm831x_irq(wm831x
, platform_get_irq_byname(pdev
, "PWR SRC"));
585 ret
= request_threaded_irq(irq
, NULL
, wm831x_pwr_src_irq
,
586 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
, "Power source",
589 dev_err(&pdev
->dev
, "Failed to request PWR SRC IRQ %d: %d\n",
594 for (i
= 0; i
< ARRAY_SIZE(wm831x_bat_irqs
); i
++) {
595 irq
= wm831x_irq(wm831x
,
596 platform_get_irq_byname(pdev
,
597 wm831x_bat_irqs
[i
]));
598 ret
= request_threaded_irq(irq
, NULL
, wm831x_bat_irq
,
599 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
604 "Failed to request %s IRQ %d: %d\n",
605 wm831x_bat_irqs
[i
], irq
, ret
);
614 for (; i
>= 0; i
--) {
615 irq
= platform_get_irq_byname(pdev
, wm831x_bat_irqs
[i
]);
616 free_irq(irq
, power
);
618 irq
= wm831x_irq(wm831x
, platform_get_irq_byname(pdev
, "PWR SRC"));
619 free_irq(irq
, power
);
621 irq
= wm831x_irq(wm831x
, platform_get_irq_byname(pdev
, "SYSLO"));
622 free_irq(irq
, power
);
624 if (power
->have_battery
)
625 power_supply_unregister(power
->battery
);
627 power_supply_unregister(power
->usb
);
629 power_supply_unregister(power
->wall
);
634 static int wm831x_power_remove(struct platform_device
*pdev
)
636 struct wm831x_power
*wm831x_power
= platform_get_drvdata(pdev
);
637 struct wm831x
*wm831x
= wm831x_power
->wm831x
;
640 for (i
= 0; i
< ARRAY_SIZE(wm831x_bat_irqs
); i
++) {
641 irq
= wm831x_irq(wm831x
,
642 platform_get_irq_byname(pdev
,
643 wm831x_bat_irqs
[i
]));
644 free_irq(irq
, wm831x_power
);
647 irq
= wm831x_irq(wm831x
, platform_get_irq_byname(pdev
, "PWR SRC"));
648 free_irq(irq
, wm831x_power
);
650 irq
= wm831x_irq(wm831x
, platform_get_irq_byname(pdev
, "SYSLO"));
651 free_irq(irq
, wm831x_power
);
653 if (wm831x_power
->have_battery
)
654 power_supply_unregister(wm831x_power
->battery
);
655 power_supply_unregister(wm831x_power
->wall
);
656 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");