1 // SPDX-License-Identifier: GPL-2.0-only
3 * Battery driver for wm8350 PMIC
5 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
7 * Based on OLPC Battery Driver
9 * Copyright 2006 David Woodhouse <dwmw2@infradead.org>
12 #include <linux/module.h>
13 #include <linux/err.h>
14 #include <linux/platform_device.h>
15 #include <linux/power_supply.h>
16 #include <linux/mfd/wm8350/supply.h>
17 #include <linux/mfd/wm8350/core.h>
18 #include <linux/mfd/wm8350/comparator.h>
20 static int wm8350_read_battery_uvolts(struct wm8350
*wm8350
)
22 return wm8350_read_auxadc(wm8350
, WM8350_AUXADC_BATT
, 0, 0)
26 static int wm8350_read_line_uvolts(struct wm8350
*wm8350
)
28 return wm8350_read_auxadc(wm8350
, WM8350_AUXADC_LINE
, 0, 0)
32 static int wm8350_read_usb_uvolts(struct wm8350
*wm8350
)
34 return wm8350_read_auxadc(wm8350
, WM8350_AUXADC_USB
, 0, 0)
38 #define WM8350_BATT_SUPPLY 1
39 #define WM8350_USB_SUPPLY 2
40 #define WM8350_LINE_SUPPLY 4
42 static inline int wm8350_charge_time_min(struct wm8350
*wm8350
, int min
)
44 if (!wm8350
->power
.rev_g_coeff
)
45 return (((min
- 30) / 15) & 0xf) << 8;
47 return (((min
- 30) / 30) & 0xf) << 8;
50 static int wm8350_get_supplies(struct wm8350
*wm8350
)
55 sm
= wm8350_reg_read(wm8350
, WM8350_STATE_MACHINE_STATUS
);
56 ov
= wm8350_reg_read(wm8350
, WM8350_MISC_OVERRIDES
);
57 co
= wm8350_reg_read(wm8350
, WM8350_COMPARATOR_OVERRIDES
);
58 chrg
= wm8350_reg_read(wm8350
, WM8350_BATTERY_CHARGER_CONTROL_2
);
61 sm
= (sm
& WM8350_USB_SM_MASK
) >> WM8350_USB_SM_SHIFT
;
64 chrg
&= WM8350_CHG_ISEL_MASK
;
66 /* If the USB state machine is active then we're using that with or
67 * without battery, otherwise check for wall supply */
68 if (((sm
== WM8350_USB_SM_100_SLV
) ||
69 (sm
== WM8350_USB_SM_500_SLV
) ||
70 (sm
== WM8350_USB_SM_STDBY_SLV
))
71 && !(ov
& WM8350_USB_LIMIT_OVRDE
))
72 supplies
= WM8350_USB_SUPPLY
;
73 else if (((sm
== WM8350_USB_SM_100_SLV
) ||
74 (sm
== WM8350_USB_SM_500_SLV
) ||
75 (sm
== WM8350_USB_SM_STDBY_SLV
))
76 && (ov
& WM8350_USB_LIMIT_OVRDE
) && (chrg
== 0))
77 supplies
= WM8350_USB_SUPPLY
| WM8350_BATT_SUPPLY
;
78 else if (co
& WM8350_WALL_FB_OVRDE
)
79 supplies
= WM8350_LINE_SUPPLY
;
81 supplies
= WM8350_BATT_SUPPLY
;
86 static int wm8350_charger_config(struct wm8350
*wm8350
,
87 struct wm8350_charger_policy
*policy
)
89 u16 reg
, eoc_mA
, fast_limit_mA
;
93 "No charger policy, charger not configured.\n");
97 /* make sure USB fast charge current is not > 500mA */
98 if (policy
->fast_limit_USB_mA
> 500) {
99 dev_err(wm8350
->dev
, "USB fast charge > 500mA\n");
103 eoc_mA
= WM8350_CHG_EOC_mA(policy
->eoc_mA
);
105 wm8350_reg_unlock(wm8350
);
107 reg
= wm8350_reg_read(wm8350
, WM8350_BATTERY_CHARGER_CONTROL_1
)
108 & WM8350_CHG_ENA_R168
;
109 wm8350_reg_write(wm8350
, WM8350_BATTERY_CHARGER_CONTROL_1
,
110 reg
| eoc_mA
| policy
->trickle_start_mV
|
111 WM8350_CHG_TRICKLE_TEMP_CHOKE
|
112 WM8350_CHG_TRICKLE_USB_CHOKE
|
113 WM8350_CHG_FAST_USB_THROTTLE
);
115 if (wm8350_get_supplies(wm8350
) & WM8350_USB_SUPPLY
) {
117 WM8350_CHG_FAST_LIMIT_mA(policy
->fast_limit_USB_mA
);
118 wm8350_reg_write(wm8350
, WM8350_BATTERY_CHARGER_CONTROL_2
,
119 policy
->charge_mV
| policy
->trickle_charge_USB_mA
|
120 fast_limit_mA
| wm8350_charge_time_min(wm8350
,
121 policy
->charge_timeout
));
125 WM8350_CHG_FAST_LIMIT_mA(policy
->fast_limit_mA
);
126 wm8350_reg_write(wm8350
, WM8350_BATTERY_CHARGER_CONTROL_2
,
127 policy
->charge_mV
| policy
->trickle_charge_mA
|
128 fast_limit_mA
| wm8350_charge_time_min(wm8350
,
129 policy
->charge_timeout
));
132 wm8350_reg_lock(wm8350
);
136 static int wm8350_batt_status(struct wm8350
*wm8350
)
140 state
= wm8350_reg_read(wm8350
, WM8350_BATTERY_CHARGER_CONTROL_2
);
141 state
&= WM8350_CHG_STS_MASK
;
144 case WM8350_CHG_STS_OFF
:
145 return POWER_SUPPLY_STATUS_DISCHARGING
;
147 case WM8350_CHG_STS_TRICKLE
:
148 case WM8350_CHG_STS_FAST
:
149 return POWER_SUPPLY_STATUS_CHARGING
;
152 return POWER_SUPPLY_STATUS_UNKNOWN
;
156 static ssize_t
charger_state_show(struct device
*dev
,
157 struct device_attribute
*attr
, char *buf
)
159 struct wm8350
*wm8350
= dev_get_drvdata(dev
);
163 state
= wm8350_reg_read(wm8350
, WM8350_BATTERY_CHARGER_CONTROL_2
) &
166 case WM8350_CHG_STS_OFF
:
167 charge
= "Charger Off";
169 case WM8350_CHG_STS_TRICKLE
:
170 charge
= "Trickle Charging";
172 case WM8350_CHG_STS_FAST
:
173 charge
= "Fast Charging";
179 return sprintf(buf
, "%s\n", charge
);
182 static DEVICE_ATTR_RO(charger_state
);
184 static irqreturn_t
wm8350_charger_handler(int irq
, void *data
)
186 struct wm8350
*wm8350
= data
;
187 struct wm8350_power
*power
= &wm8350
->power
;
188 struct wm8350_charger_policy
*policy
= power
->policy
;
190 switch (irq
- wm8350
->irq_base
) {
191 case WM8350_IRQ_CHG_BAT_FAIL
:
192 dev_err(wm8350
->dev
, "battery failed\n");
194 case WM8350_IRQ_CHG_TO
:
195 dev_err(wm8350
->dev
, "charger timeout\n");
196 power_supply_changed(power
->battery
);
199 case WM8350_IRQ_CHG_BAT_HOT
:
200 case WM8350_IRQ_CHG_BAT_COLD
:
201 case WM8350_IRQ_CHG_START
:
202 case WM8350_IRQ_CHG_END
:
203 power_supply_changed(power
->battery
);
206 case WM8350_IRQ_CHG_FAST_RDY
:
207 dev_dbg(wm8350
->dev
, "fast charger ready\n");
208 wm8350_charger_config(wm8350
, policy
);
209 wm8350_reg_unlock(wm8350
);
210 wm8350_set_bits(wm8350
, WM8350_BATTERY_CHARGER_CONTROL_1
,
212 wm8350_reg_lock(wm8350
);
215 case WM8350_IRQ_CHG_VBATT_LT_3P9
:
216 dev_warn(wm8350
->dev
, "battery < 3.9V\n");
218 case WM8350_IRQ_CHG_VBATT_LT_3P1
:
219 dev_warn(wm8350
->dev
, "battery < 3.1V\n");
221 case WM8350_IRQ_CHG_VBATT_LT_2P85
:
222 dev_warn(wm8350
->dev
, "battery < 2.85V\n");
225 /* Supply change. We will overnotify but it should do
227 case WM8350_IRQ_EXT_USB_FB
:
228 case WM8350_IRQ_EXT_WALL_FB
:
229 wm8350_charger_config(wm8350
, policy
);
231 case WM8350_IRQ_EXT_BAT_FB
:
232 power_supply_changed(power
->battery
);
233 power_supply_changed(power
->usb
);
234 power_supply_changed(power
->ac
);
238 dev_err(wm8350
->dev
, "Unknown interrupt %d\n", irq
);
244 /*********************************************************************
246 *********************************************************************/
247 static int wm8350_ac_get_prop(struct power_supply
*psy
,
248 enum power_supply_property psp
,
249 union power_supply_propval
*val
)
251 struct wm8350
*wm8350
= dev_get_drvdata(psy
->dev
.parent
);
255 case POWER_SUPPLY_PROP_ONLINE
:
256 val
->intval
= !!(wm8350_get_supplies(wm8350
) &
259 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
260 val
->intval
= wm8350_read_line_uvolts(wm8350
);
269 static enum power_supply_property wm8350_ac_props
[] = {
270 POWER_SUPPLY_PROP_ONLINE
,
271 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
274 /*********************************************************************
276 *********************************************************************/
277 static int wm8350_usb_get_prop(struct power_supply
*psy
,
278 enum power_supply_property psp
,
279 union power_supply_propval
*val
)
281 struct wm8350
*wm8350
= dev_get_drvdata(psy
->dev
.parent
);
285 case POWER_SUPPLY_PROP_ONLINE
:
286 val
->intval
= !!(wm8350_get_supplies(wm8350
) &
289 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
290 val
->intval
= wm8350_read_usb_uvolts(wm8350
);
299 static enum power_supply_property wm8350_usb_props
[] = {
300 POWER_SUPPLY_PROP_ONLINE
,
301 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
304 /*********************************************************************
306 *********************************************************************/
308 static int wm8350_bat_check_health(struct wm8350
*wm8350
)
312 if (wm8350_read_battery_uvolts(wm8350
) < 2850000)
313 return POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
315 reg
= wm8350_reg_read(wm8350
, WM8350_CHARGER_OVERRIDES
);
316 if (reg
& WM8350_CHG_BATT_HOT_OVRDE
)
317 return POWER_SUPPLY_HEALTH_OVERHEAT
;
319 if (reg
& WM8350_CHG_BATT_COLD_OVRDE
)
320 return POWER_SUPPLY_HEALTH_COLD
;
322 return POWER_SUPPLY_HEALTH_GOOD
;
325 static int wm8350_bat_get_charge_type(struct wm8350
*wm8350
)
329 state
= wm8350_reg_read(wm8350
, WM8350_BATTERY_CHARGER_CONTROL_2
) &
332 case WM8350_CHG_STS_OFF
:
333 return POWER_SUPPLY_CHARGE_TYPE_NONE
;
334 case WM8350_CHG_STS_TRICKLE
:
335 return POWER_SUPPLY_CHARGE_TYPE_TRICKLE
;
336 case WM8350_CHG_STS_FAST
:
337 return POWER_SUPPLY_CHARGE_TYPE_FAST
;
339 return POWER_SUPPLY_CHARGE_TYPE_UNKNOWN
;
343 static int wm8350_bat_get_property(struct power_supply
*psy
,
344 enum power_supply_property psp
,
345 union power_supply_propval
*val
)
347 struct wm8350
*wm8350
= dev_get_drvdata(psy
->dev
.parent
);
351 case POWER_SUPPLY_PROP_STATUS
:
352 val
->intval
= wm8350_batt_status(wm8350
);
354 case POWER_SUPPLY_PROP_ONLINE
:
355 val
->intval
= !!(wm8350_get_supplies(wm8350
) &
358 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
359 val
->intval
= wm8350_read_battery_uvolts(wm8350
);
361 case POWER_SUPPLY_PROP_HEALTH
:
362 val
->intval
= wm8350_bat_check_health(wm8350
);
364 case POWER_SUPPLY_PROP_CHARGE_TYPE
:
365 val
->intval
= wm8350_bat_get_charge_type(wm8350
);
375 static enum power_supply_property wm8350_bat_props
[] = {
376 POWER_SUPPLY_PROP_STATUS
,
377 POWER_SUPPLY_PROP_ONLINE
,
378 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
379 POWER_SUPPLY_PROP_HEALTH
,
380 POWER_SUPPLY_PROP_CHARGE_TYPE
,
383 static const struct power_supply_desc wm8350_ac_desc
= {
385 .type
= POWER_SUPPLY_TYPE_MAINS
,
386 .properties
= wm8350_ac_props
,
387 .num_properties
= ARRAY_SIZE(wm8350_ac_props
),
388 .get_property
= wm8350_ac_get_prop
,
391 static const struct power_supply_desc wm8350_battery_desc
= {
392 .name
= "wm8350-battery",
393 .properties
= wm8350_bat_props
,
394 .num_properties
= ARRAY_SIZE(wm8350_bat_props
),
395 .get_property
= wm8350_bat_get_property
,
399 static const struct power_supply_desc wm8350_usb_desc
= {
400 .name
= "wm8350-usb",
401 .type
= POWER_SUPPLY_TYPE_USB
,
402 .properties
= wm8350_usb_props
,
403 .num_properties
= ARRAY_SIZE(wm8350_usb_props
),
404 .get_property
= wm8350_usb_get_prop
,
407 /*********************************************************************
409 *********************************************************************/
411 static void wm8350_init_charger(struct wm8350
*wm8350
)
413 /* register our interest in charger events */
414 wm8350_register_irq(wm8350
, WM8350_IRQ_CHG_BAT_HOT
,
415 wm8350_charger_handler
, 0, "Battery hot", wm8350
);
416 wm8350_register_irq(wm8350
, WM8350_IRQ_CHG_BAT_COLD
,
417 wm8350_charger_handler
, 0, "Battery cold", wm8350
);
418 wm8350_register_irq(wm8350
, WM8350_IRQ_CHG_BAT_FAIL
,
419 wm8350_charger_handler
, 0, "Battery fail", wm8350
);
420 wm8350_register_irq(wm8350
, WM8350_IRQ_CHG_TO
,
421 wm8350_charger_handler
, 0,
422 "Charger timeout", wm8350
);
423 wm8350_register_irq(wm8350
, WM8350_IRQ_CHG_END
,
424 wm8350_charger_handler
, 0,
425 "Charge end", wm8350
);
426 wm8350_register_irq(wm8350
, WM8350_IRQ_CHG_START
,
427 wm8350_charger_handler
, 0,
428 "Charge start", wm8350
);
429 wm8350_register_irq(wm8350
, WM8350_IRQ_CHG_FAST_RDY
,
430 wm8350_charger_handler
, 0,
431 "Fast charge ready", wm8350
);
432 wm8350_register_irq(wm8350
, WM8350_IRQ_CHG_VBATT_LT_3P9
,
433 wm8350_charger_handler
, 0,
434 "Battery <3.9V", wm8350
);
435 wm8350_register_irq(wm8350
, WM8350_IRQ_CHG_VBATT_LT_3P1
,
436 wm8350_charger_handler
, 0,
437 "Battery <3.1V", wm8350
);
438 wm8350_register_irq(wm8350
, WM8350_IRQ_CHG_VBATT_LT_2P85
,
439 wm8350_charger_handler
, 0,
440 "Battery <2.85V", wm8350
);
442 /* and supply change events */
443 wm8350_register_irq(wm8350
, WM8350_IRQ_EXT_USB_FB
,
444 wm8350_charger_handler
, 0, "USB", wm8350
);
445 wm8350_register_irq(wm8350
, WM8350_IRQ_EXT_WALL_FB
,
446 wm8350_charger_handler
, 0, "Wall", wm8350
);
447 wm8350_register_irq(wm8350
, WM8350_IRQ_EXT_BAT_FB
,
448 wm8350_charger_handler
, 0, "Battery", wm8350
);
451 static void free_charger_irq(struct wm8350
*wm8350
)
453 wm8350_free_irq(wm8350
, WM8350_IRQ_CHG_BAT_HOT
, wm8350
);
454 wm8350_free_irq(wm8350
, WM8350_IRQ_CHG_BAT_COLD
, wm8350
);
455 wm8350_free_irq(wm8350
, WM8350_IRQ_CHG_BAT_FAIL
, wm8350
);
456 wm8350_free_irq(wm8350
, WM8350_IRQ_CHG_TO
, wm8350
);
457 wm8350_free_irq(wm8350
, WM8350_IRQ_CHG_END
, wm8350
);
458 wm8350_free_irq(wm8350
, WM8350_IRQ_CHG_START
, wm8350
);
459 wm8350_free_irq(wm8350
, WM8350_IRQ_CHG_VBATT_LT_3P9
, wm8350
);
460 wm8350_free_irq(wm8350
, WM8350_IRQ_CHG_VBATT_LT_3P1
, wm8350
);
461 wm8350_free_irq(wm8350
, WM8350_IRQ_CHG_VBATT_LT_2P85
, wm8350
);
462 wm8350_free_irq(wm8350
, WM8350_IRQ_EXT_USB_FB
, wm8350
);
463 wm8350_free_irq(wm8350
, WM8350_IRQ_EXT_WALL_FB
, wm8350
);
464 wm8350_free_irq(wm8350
, WM8350_IRQ_EXT_BAT_FB
, wm8350
);
467 static int wm8350_power_probe(struct platform_device
*pdev
)
469 struct wm8350
*wm8350
= platform_get_drvdata(pdev
);
470 struct wm8350_power
*power
= &wm8350
->power
;
471 struct wm8350_charger_policy
*policy
= power
->policy
;
474 power
->ac
= power_supply_register(&pdev
->dev
, &wm8350_ac_desc
, NULL
);
475 if (IS_ERR(power
->ac
))
476 return PTR_ERR(power
->ac
);
478 power
->battery
= power_supply_register(&pdev
->dev
, &wm8350_battery_desc
,
480 if (IS_ERR(power
->battery
)) {
481 ret
= PTR_ERR(power
->battery
);
485 power
->usb
= power_supply_register(&pdev
->dev
, &wm8350_usb_desc
, NULL
);
486 if (IS_ERR(power
->usb
)) {
487 ret
= PTR_ERR(power
->usb
);
491 ret
= device_create_file(&pdev
->dev
, &dev_attr_charger_state
);
493 dev_warn(wm8350
->dev
, "failed to add charge sysfs: %d\n", ret
);
496 wm8350_init_charger(wm8350
);
497 if (wm8350_charger_config(wm8350
, policy
) == 0) {
498 wm8350_reg_unlock(wm8350
);
499 wm8350_set_bits(wm8350
, WM8350_POWER_MGMT_5
, WM8350_CHG_ENA
);
500 wm8350_reg_lock(wm8350
);
506 power_supply_unregister(power
->battery
);
508 power_supply_unregister(power
->ac
);
513 static int wm8350_power_remove(struct platform_device
*pdev
)
515 struct wm8350
*wm8350
= platform_get_drvdata(pdev
);
516 struct wm8350_power
*power
= &wm8350
->power
;
518 free_charger_irq(wm8350
);
519 device_remove_file(&pdev
->dev
, &dev_attr_charger_state
);
520 power_supply_unregister(power
->battery
);
521 power_supply_unregister(power
->ac
);
522 power_supply_unregister(power
->usb
);
526 static struct platform_driver wm8350_power_driver
= {
527 .probe
= wm8350_power_probe
,
528 .remove
= wm8350_power_remove
,
530 .name
= "wm8350-power",
534 module_platform_driver(wm8350_power_driver
);
536 MODULE_LICENSE("GPL");
537 MODULE_DESCRIPTION("Power supply driver for WM8350");
538 MODULE_ALIAS("platform:wm8350-power");