mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-08-26 02:39:48 +00:00
![Krzysztof Kozlowski](/assets/img/avatar_default.png)
Change the ownership of power_supply structure from each driver implementing the class to the power supply core. The patch changes power_supply_register() function thus all drivers implementing power supply class are adjusted. Each driver provides the implementation of power supply. However it should not be the owner of power supply class instance because it is exposed by core to other subsystems with power_supply_get_by_name(). These other subsystems have no knowledge when the driver will unregister the power supply. This leads to several issues when driver is unbound - mostly because user of power supply accesses freed memory. Instead let the core own the instance of struct 'power_supply'. Other users of this power supply will still access valid memory because it will be freed when device reference count reaches 0. Currently this means "it will leak" but power_supply_put() call in next patches will solve it. This solves invalid memory references in following race condition scenario: Thread 1: charger manager Thread 2: power supply driver, used by charger manager THREAD 1 (charger manager) THREAD 2 (power supply driver) ========================== ============================== psy = power_supply_get_by_name() Driver unbind, .remove power_supply_unregister() Device fully removed psy->get_property() The 'get_property' call is executed in invalid context because the driver was unbound and struct 'power_supply' memory was freed. This could be observed easily with charger manager driver (here compiled with max17040 fuel gauge): $ cat /sys/devices/virtual/power_supply/cm-battery/capacity & $ echo "1-0036" > /sys/bus/i2c/drivers/max17040/unbind [ 55.725123] Unable to handle kernel NULL pointer dereference at virtual address 00000000 [ 55.732584] pgd = d98d4000 [ 55.734060] [00000000] *pgd=5afa2831, *pte=00000000, *ppte=00000000 [ 55.740318] Internal error: Oops: 80000007 [#1] PREEMPT SMP ARM [ 55.746210] Modules linked in: [ 55.749259] CPU: 1 PID: 2936 Comm: cat Tainted: G W 3.19.0-rc1-next-20141226-00048-gf79f475f3c44-dirty #1496 [ 55.760190] Hardware name: SAMSUNG EXYNOS (Flattened Device Tree) [ 55.766270] task: d9b76f00 ti: daf54000 task.ti: daf54000 [ 55.771647] PC is at 0x0 [ 55.774182] LR is at charger_get_property+0x2f4/0x36c [ 55.779201] pc : [<00000000>] lr : [<c034b0b4>] psr: 60000013 [ 55.779201] sp : daf55e90 ip : 00000003 fp : 00000000 [ 55.790657] r10: 00000000 r9 : c06e2878 r8 : d9b26c68 [ 55.795865] r7 : dad81610 r6 : daec7410 r5 : daf55ebc r4 : 00000000 [ 55.802367] r3 : 00000000 r2 : daf55ebc r1 : 0000002a r0 : d9b26c68 [ 55.808879] Flags: nZCv IRQs on FIQs on Mode SVC_32 ISA ARM Segment user [ 55.815994] Control: 10c5387d Table: 598d406a DAC: 00000015 [ 55.821723] Process cat (pid: 2936, stack limit = 0xdaf54210) [ 55.827451] Stack: (0xdaf55e90 to 0xdaf56000) [ 55.831795] 5e80: 60000013 c01459c4 0000002a c06f8ef8 [ 55.839956] 5ea0: db651000 c06f8ef8 daebac00 c04cb668 daebac08 c0346864 00000000 c01459c4 [ 55.848115] 5ec0: d99eaa80 c06f8ef8 00000fff 00001000 db651000 c027f25c c027f240 d99eaa80 [ 55.856274] 5ee0: d9a06c00 c0146218 daf55f18 00001000 d99eaa80 db4c18c0 00000001 00000001 [ 55.864468] 5f00: daf55f80 c0144c78 c0144c54 c0107f90 00015000 d99eaab0 00000000 00000000 [ 55.872603] 5f20: 000051c7 00000000 db4c18c0 c04a9370 00015000 00001000 daf55f80 00001000 [ 55.880763] 5f40: daf54000 00015000 00000000 c00e53dc db4c18c0 c00e548c 0000000d 00008124 [ 55.888937] 5f60: 00000001 00000000 00000000 db4c18c0 db4c18c0 00001000 00015000 c00e5550 [ 55.897099] 5f80: 00000000 00000000 00001000 00001000 00015000 00000003 00000003 c000f364 [ 55.905239] 5fa0: 00000000 c000f1a0 00001000 00015000 00000003 00015000 00001000 0001333c [ 55.913399] 5fc0: 00001000 00015000 00000003 00000003 00000002 00000000 00000000 00000000 [ 55.921560] 5fe0: 7fffe000 be999850 0000a225 b6f3c19c 60000010 00000003 00000000 00000000 [ 55.929744] [<c034b0b4>] (charger_get_property) from [<c0346864>] (power_supply_show_property+0x48/0x20c) [ 55.939286] [<c0346864>] (power_supply_show_property) from [<c027f25c>] (dev_attr_show+0x1c/0x48) [ 55.948130] [<c027f25c>] (dev_attr_show) from [<c0146218>] (sysfs_kf_seq_show+0x84/0x104) [ 55.956298] [<c0146218>] (sysfs_kf_seq_show) from [<c0144c78>] (kernfs_seq_show+0x24/0x28) [ 55.964536] [<c0144c78>] (kernfs_seq_show) from [<c0107f90>] (seq_read+0x1b0/0x484) [ 55.972172] [<c0107f90>] (seq_read) from [<c00e53dc>] (__vfs_read+0x18/0x4c) [ 55.979188] [<c00e53dc>] (__vfs_read) from [<c00e548c>] (vfs_read+0x7c/0x100) [ 55.986304] [<c00e548c>] (vfs_read) from [<c00e5550>] (SyS_read+0x40/0x8c) [ 55.993164] [<c00e5550>] (SyS_read) from [<c000f1a0>] (ret_fast_syscall+0x0/0x48) [ 56.000626] Code: bad PC value [ 56.011652] ---[ end trace 7b64343fbdae8ef1 ]--- Signed-off-by: Krzysztof Kozlowski <k.kozlowski@samsung.com> Reviewed-by: Bartlomiej Zolnierkiewicz <b.zolnierkie@samsung.com> [for the nvec part] Reviewed-by: Marc Dietrich <marvin24@gmx.de> [for compal-laptop.c] Acked-by: Darren Hart <dvhart@linux.intel.com> [for the mfd part] Acked-by: Lee Jones <lee.jones@linaro.org> [for the hid part] Acked-by: Jiri Kosina <jkosina@suse.cz> [for the acpi part] Acked-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> Signed-off-by: Sebastian Reichel <sre@kernel.org>
540 lines
15 KiB
C
540 lines
15 KiB
C
/*
|
|
* Battery driver for wm8350 PMIC
|
|
*
|
|
* Copyright 2007, 2008 Wolfson Microelectronics PLC.
|
|
*
|
|
* Based on OLPC Battery Driver
|
|
*
|
|
* Copyright 2006 David Woodhouse <dwmw2@infradead.org>
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
* published by the Free Software Foundation.
|
|
*/
|
|
|
|
#include <linux/module.h>
|
|
#include <linux/err.h>
|
|
#include <linux/platform_device.h>
|
|
#include <linux/power_supply.h>
|
|
#include <linux/mfd/wm8350/supply.h>
|
|
#include <linux/mfd/wm8350/core.h>
|
|
#include <linux/mfd/wm8350/comparator.h>
|
|
|
|
static int wm8350_read_battery_uvolts(struct wm8350 *wm8350)
|
|
{
|
|
return wm8350_read_auxadc(wm8350, WM8350_AUXADC_BATT, 0, 0)
|
|
* WM8350_AUX_COEFF;
|
|
}
|
|
|
|
static int wm8350_read_line_uvolts(struct wm8350 *wm8350)
|
|
{
|
|
return wm8350_read_auxadc(wm8350, WM8350_AUXADC_LINE, 0, 0)
|
|
* WM8350_AUX_COEFF;
|
|
}
|
|
|
|
static int wm8350_read_usb_uvolts(struct wm8350 *wm8350)
|
|
{
|
|
return wm8350_read_auxadc(wm8350, WM8350_AUXADC_USB, 0, 0)
|
|
* WM8350_AUX_COEFF;
|
|
}
|
|
|
|
#define WM8350_BATT_SUPPLY 1
|
|
#define WM8350_USB_SUPPLY 2
|
|
#define WM8350_LINE_SUPPLY 4
|
|
|
|
static inline int wm8350_charge_time_min(struct wm8350 *wm8350, int min)
|
|
{
|
|
if (!wm8350->power.rev_g_coeff)
|
|
return (((min - 30) / 15) & 0xf) << 8;
|
|
else
|
|
return (((min - 30) / 30) & 0xf) << 8;
|
|
}
|
|
|
|
static int wm8350_get_supplies(struct wm8350 *wm8350)
|
|
{
|
|
u16 sm, ov, co, chrg;
|
|
int supplies = 0;
|
|
|
|
sm = wm8350_reg_read(wm8350, WM8350_STATE_MACHINE_STATUS);
|
|
ov = wm8350_reg_read(wm8350, WM8350_MISC_OVERRIDES);
|
|
co = wm8350_reg_read(wm8350, WM8350_COMPARATOR_OVERRIDES);
|
|
chrg = wm8350_reg_read(wm8350, WM8350_BATTERY_CHARGER_CONTROL_2);
|
|
|
|
/* USB_SM */
|
|
sm = (sm & WM8350_USB_SM_MASK) >> WM8350_USB_SM_SHIFT;
|
|
|
|
/* CHG_ISEL */
|
|
chrg &= WM8350_CHG_ISEL_MASK;
|
|
|
|
/* If the USB state machine is active then we're using that with or
|
|
* without battery, otherwise check for wall supply */
|
|
if (((sm == WM8350_USB_SM_100_SLV) ||
|
|
(sm == WM8350_USB_SM_500_SLV) ||
|
|
(sm == WM8350_USB_SM_STDBY_SLV))
|
|
&& !(ov & WM8350_USB_LIMIT_OVRDE))
|
|
supplies = WM8350_USB_SUPPLY;
|
|
else if (((sm == WM8350_USB_SM_100_SLV) ||
|
|
(sm == WM8350_USB_SM_500_SLV) ||
|
|
(sm == WM8350_USB_SM_STDBY_SLV))
|
|
&& (ov & WM8350_USB_LIMIT_OVRDE) && (chrg == 0))
|
|
supplies = WM8350_USB_SUPPLY | WM8350_BATT_SUPPLY;
|
|
else if (co & WM8350_WALL_FB_OVRDE)
|
|
supplies = WM8350_LINE_SUPPLY;
|
|
else
|
|
supplies = WM8350_BATT_SUPPLY;
|
|
|
|
return supplies;
|
|
}
|
|
|
|
static int wm8350_charger_config(struct wm8350 *wm8350,
|
|
struct wm8350_charger_policy *policy)
|
|
{
|
|
u16 reg, eoc_mA, fast_limit_mA;
|
|
|
|
if (!policy) {
|
|
dev_warn(wm8350->dev,
|
|
"No charger policy, charger not configured.\n");
|
|
return -EINVAL;
|
|
}
|
|
|
|
/* make sure USB fast charge current is not > 500mA */
|
|
if (policy->fast_limit_USB_mA > 500) {
|
|
dev_err(wm8350->dev, "USB fast charge > 500mA\n");
|
|
return -EINVAL;
|
|
}
|
|
|
|
eoc_mA = WM8350_CHG_EOC_mA(policy->eoc_mA);
|
|
|
|
wm8350_reg_unlock(wm8350);
|
|
|
|
reg = wm8350_reg_read(wm8350, WM8350_BATTERY_CHARGER_CONTROL_1)
|
|
& WM8350_CHG_ENA_R168;
|
|
wm8350_reg_write(wm8350, WM8350_BATTERY_CHARGER_CONTROL_1,
|
|
reg | eoc_mA | policy->trickle_start_mV |
|
|
WM8350_CHG_TRICKLE_TEMP_CHOKE |
|
|
WM8350_CHG_TRICKLE_USB_CHOKE |
|
|
WM8350_CHG_FAST_USB_THROTTLE);
|
|
|
|
if (wm8350_get_supplies(wm8350) & WM8350_USB_SUPPLY) {
|
|
fast_limit_mA =
|
|
WM8350_CHG_FAST_LIMIT_mA(policy->fast_limit_USB_mA);
|
|
wm8350_reg_write(wm8350, WM8350_BATTERY_CHARGER_CONTROL_2,
|
|
policy->charge_mV | policy->trickle_charge_USB_mA |
|
|
fast_limit_mA | wm8350_charge_time_min(wm8350,
|
|
policy->charge_timeout));
|
|
|
|
} else {
|
|
fast_limit_mA =
|
|
WM8350_CHG_FAST_LIMIT_mA(policy->fast_limit_mA);
|
|
wm8350_reg_write(wm8350, WM8350_BATTERY_CHARGER_CONTROL_2,
|
|
policy->charge_mV | policy->trickle_charge_mA |
|
|
fast_limit_mA | wm8350_charge_time_min(wm8350,
|
|
policy->charge_timeout));
|
|
}
|
|
|
|
wm8350_reg_lock(wm8350);
|
|
return 0;
|
|
}
|
|
|
|
static int wm8350_batt_status(struct wm8350 *wm8350)
|
|
{
|
|
u16 state;
|
|
|
|
state = wm8350_reg_read(wm8350, WM8350_BATTERY_CHARGER_CONTROL_2);
|
|
state &= WM8350_CHG_STS_MASK;
|
|
|
|
switch (state) {
|
|
case WM8350_CHG_STS_OFF:
|
|
return POWER_SUPPLY_STATUS_DISCHARGING;
|
|
|
|
case WM8350_CHG_STS_TRICKLE:
|
|
case WM8350_CHG_STS_FAST:
|
|
return POWER_SUPPLY_STATUS_CHARGING;
|
|
|
|
default:
|
|
return POWER_SUPPLY_STATUS_UNKNOWN;
|
|
}
|
|
}
|
|
|
|
static ssize_t charger_state_show(struct device *dev,
|
|
struct device_attribute *attr, char *buf)
|
|
{
|
|
struct wm8350 *wm8350 = dev_get_drvdata(dev);
|
|
char *charge;
|
|
int state;
|
|
|
|
state = wm8350_reg_read(wm8350, WM8350_BATTERY_CHARGER_CONTROL_2) &
|
|
WM8350_CHG_STS_MASK;
|
|
switch (state) {
|
|
case WM8350_CHG_STS_OFF:
|
|
charge = "Charger Off";
|
|
break;
|
|
case WM8350_CHG_STS_TRICKLE:
|
|
charge = "Trickle Charging";
|
|
break;
|
|
case WM8350_CHG_STS_FAST:
|
|
charge = "Fast Charging";
|
|
break;
|
|
default:
|
|
return 0;
|
|
}
|
|
|
|
return sprintf(buf, "%s\n", charge);
|
|
}
|
|
|
|
static DEVICE_ATTR(charger_state, 0444, charger_state_show, NULL);
|
|
|
|
static irqreturn_t wm8350_charger_handler(int irq, void *data)
|
|
{
|
|
struct wm8350 *wm8350 = data;
|
|
struct wm8350_power *power = &wm8350->power;
|
|
struct wm8350_charger_policy *policy = power->policy;
|
|
|
|
switch (irq - wm8350->irq_base) {
|
|
case WM8350_IRQ_CHG_BAT_FAIL:
|
|
dev_err(wm8350->dev, "battery failed\n");
|
|
break;
|
|
case WM8350_IRQ_CHG_TO:
|
|
dev_err(wm8350->dev, "charger timeout\n");
|
|
power_supply_changed(power->battery);
|
|
break;
|
|
|
|
case WM8350_IRQ_CHG_BAT_HOT:
|
|
case WM8350_IRQ_CHG_BAT_COLD:
|
|
case WM8350_IRQ_CHG_START:
|
|
case WM8350_IRQ_CHG_END:
|
|
power_supply_changed(power->battery);
|
|
break;
|
|
|
|
case WM8350_IRQ_CHG_FAST_RDY:
|
|
dev_dbg(wm8350->dev, "fast charger ready\n");
|
|
wm8350_charger_config(wm8350, policy);
|
|
wm8350_reg_unlock(wm8350);
|
|
wm8350_set_bits(wm8350, WM8350_BATTERY_CHARGER_CONTROL_1,
|
|
WM8350_CHG_FAST);
|
|
wm8350_reg_lock(wm8350);
|
|
break;
|
|
|
|
case WM8350_IRQ_CHG_VBATT_LT_3P9:
|
|
dev_warn(wm8350->dev, "battery < 3.9V\n");
|
|
break;
|
|
case WM8350_IRQ_CHG_VBATT_LT_3P1:
|
|
dev_warn(wm8350->dev, "battery < 3.1V\n");
|
|
break;
|
|
case WM8350_IRQ_CHG_VBATT_LT_2P85:
|
|
dev_warn(wm8350->dev, "battery < 2.85V\n");
|
|
break;
|
|
|
|
/* Supply change. We will overnotify but it should do
|
|
* no harm. */
|
|
case WM8350_IRQ_EXT_USB_FB:
|
|
case WM8350_IRQ_EXT_WALL_FB:
|
|
wm8350_charger_config(wm8350, policy);
|
|
case WM8350_IRQ_EXT_BAT_FB: /* Fall through */
|
|
power_supply_changed(power->battery);
|
|
power_supply_changed(power->usb);
|
|
power_supply_changed(power->ac);
|
|
break;
|
|
|
|
default:
|
|
dev_err(wm8350->dev, "Unknown interrupt %d\n", irq);
|
|
}
|
|
|
|
return IRQ_HANDLED;
|
|
}
|
|
|
|
/*********************************************************************
|
|
* AC Power
|
|
*********************************************************************/
|
|
static int wm8350_ac_get_prop(struct power_supply *psy,
|
|
enum power_supply_property psp,
|
|
union power_supply_propval *val)
|
|
{
|
|
struct wm8350 *wm8350 = dev_get_drvdata(psy->dev.parent);
|
|
int ret = 0;
|
|
|
|
switch (psp) {
|
|
case POWER_SUPPLY_PROP_ONLINE:
|
|
val->intval = !!(wm8350_get_supplies(wm8350) &
|
|
WM8350_LINE_SUPPLY);
|
|
break;
|
|
case POWER_SUPPLY_PROP_VOLTAGE_NOW:
|
|
val->intval = wm8350_read_line_uvolts(wm8350);
|
|
break;
|
|
default:
|
|
ret = -EINVAL;
|
|
break;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
static enum power_supply_property wm8350_ac_props[] = {
|
|
POWER_SUPPLY_PROP_ONLINE,
|
|
POWER_SUPPLY_PROP_VOLTAGE_NOW,
|
|
};
|
|
|
|
/*********************************************************************
|
|
* USB Power
|
|
*********************************************************************/
|
|
static int wm8350_usb_get_prop(struct power_supply *psy,
|
|
enum power_supply_property psp,
|
|
union power_supply_propval *val)
|
|
{
|
|
struct wm8350 *wm8350 = dev_get_drvdata(psy->dev.parent);
|
|
int ret = 0;
|
|
|
|
switch (psp) {
|
|
case POWER_SUPPLY_PROP_ONLINE:
|
|
val->intval = !!(wm8350_get_supplies(wm8350) &
|
|
WM8350_USB_SUPPLY);
|
|
break;
|
|
case POWER_SUPPLY_PROP_VOLTAGE_NOW:
|
|
val->intval = wm8350_read_usb_uvolts(wm8350);
|
|
break;
|
|
default:
|
|
ret = -EINVAL;
|
|
break;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
static enum power_supply_property wm8350_usb_props[] = {
|
|
POWER_SUPPLY_PROP_ONLINE,
|
|
POWER_SUPPLY_PROP_VOLTAGE_NOW,
|
|
};
|
|
|
|
/*********************************************************************
|
|
* Battery properties
|
|
*********************************************************************/
|
|
|
|
static int wm8350_bat_check_health(struct wm8350 *wm8350)
|
|
{
|
|
u16 reg;
|
|
|
|
if (wm8350_read_battery_uvolts(wm8350) < 2850000)
|
|
return POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
|
|
|
|
reg = wm8350_reg_read(wm8350, WM8350_CHARGER_OVERRIDES);
|
|
if (reg & WM8350_CHG_BATT_HOT_OVRDE)
|
|
return POWER_SUPPLY_HEALTH_OVERHEAT;
|
|
|
|
if (reg & WM8350_CHG_BATT_COLD_OVRDE)
|
|
return POWER_SUPPLY_HEALTH_COLD;
|
|
|
|
return POWER_SUPPLY_HEALTH_GOOD;
|
|
}
|
|
|
|
static int wm8350_bat_get_charge_type(struct wm8350 *wm8350)
|
|
{
|
|
int state;
|
|
|
|
state = wm8350_reg_read(wm8350, WM8350_BATTERY_CHARGER_CONTROL_2) &
|
|
WM8350_CHG_STS_MASK;
|
|
switch (state) {
|
|
case WM8350_CHG_STS_OFF:
|
|
return POWER_SUPPLY_CHARGE_TYPE_NONE;
|
|
case WM8350_CHG_STS_TRICKLE:
|
|
return POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
|
|
case WM8350_CHG_STS_FAST:
|
|
return POWER_SUPPLY_CHARGE_TYPE_FAST;
|
|
default:
|
|
return POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
|
|
}
|
|
}
|
|
|
|
static int wm8350_bat_get_property(struct power_supply *psy,
|
|
enum power_supply_property psp,
|
|
union power_supply_propval *val)
|
|
{
|
|
struct wm8350 *wm8350 = dev_get_drvdata(psy->dev.parent);
|
|
int ret = 0;
|
|
|
|
switch (psp) {
|
|
case POWER_SUPPLY_PROP_STATUS:
|
|
val->intval = wm8350_batt_status(wm8350);
|
|
break;
|
|
case POWER_SUPPLY_PROP_ONLINE:
|
|
val->intval = !!(wm8350_get_supplies(wm8350) &
|
|
WM8350_BATT_SUPPLY);
|
|
break;
|
|
case POWER_SUPPLY_PROP_VOLTAGE_NOW:
|
|
val->intval = wm8350_read_battery_uvolts(wm8350);
|
|
break;
|
|
case POWER_SUPPLY_PROP_HEALTH:
|
|
val->intval = wm8350_bat_check_health(wm8350);
|
|
break;
|
|
case POWER_SUPPLY_PROP_CHARGE_TYPE:
|
|
val->intval = wm8350_bat_get_charge_type(wm8350);
|
|
break;
|
|
default:
|
|
ret = -EINVAL;
|
|
break;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
static enum power_supply_property wm8350_bat_props[] = {
|
|
POWER_SUPPLY_PROP_STATUS,
|
|
POWER_SUPPLY_PROP_ONLINE,
|
|
POWER_SUPPLY_PROP_VOLTAGE_NOW,
|
|
POWER_SUPPLY_PROP_HEALTH,
|
|
POWER_SUPPLY_PROP_CHARGE_TYPE,
|
|
};
|
|
|
|
static const struct power_supply_desc wm8350_ac_desc = {
|
|
.name = "wm8350-ac",
|
|
.type = POWER_SUPPLY_TYPE_MAINS,
|
|
.properties = wm8350_ac_props,
|
|
.num_properties = ARRAY_SIZE(wm8350_ac_props),
|
|
.get_property = wm8350_ac_get_prop,
|
|
};
|
|
|
|
static const struct power_supply_desc wm8350_battery_desc = {
|
|
.name = "wm8350-battery",
|
|
.properties = wm8350_bat_props,
|
|
.num_properties = ARRAY_SIZE(wm8350_bat_props),
|
|
.get_property = wm8350_bat_get_property,
|
|
.use_for_apm = 1,
|
|
};
|
|
|
|
static const struct power_supply_desc wm8350_usb_desc = {
|
|
.name = "wm8350-usb",
|
|
.type = POWER_SUPPLY_TYPE_USB,
|
|
.properties = wm8350_usb_props,
|
|
.num_properties = ARRAY_SIZE(wm8350_usb_props),
|
|
.get_property = wm8350_usb_get_prop,
|
|
};
|
|
|
|
/*********************************************************************
|
|
* Initialisation
|
|
*********************************************************************/
|
|
|
|
static void wm8350_init_charger(struct wm8350 *wm8350)
|
|
{
|
|
/* register our interest in charger events */
|
|
wm8350_register_irq(wm8350, WM8350_IRQ_CHG_BAT_HOT,
|
|
wm8350_charger_handler, 0, "Battery hot", wm8350);
|
|
wm8350_register_irq(wm8350, WM8350_IRQ_CHG_BAT_COLD,
|
|
wm8350_charger_handler, 0, "Battery cold", wm8350);
|
|
wm8350_register_irq(wm8350, WM8350_IRQ_CHG_BAT_FAIL,
|
|
wm8350_charger_handler, 0, "Battery fail", wm8350);
|
|
wm8350_register_irq(wm8350, WM8350_IRQ_CHG_TO,
|
|
wm8350_charger_handler, 0,
|
|
"Charger timeout", wm8350);
|
|
wm8350_register_irq(wm8350, WM8350_IRQ_CHG_END,
|
|
wm8350_charger_handler, 0,
|
|
"Charge end", wm8350);
|
|
wm8350_register_irq(wm8350, WM8350_IRQ_CHG_START,
|
|
wm8350_charger_handler, 0,
|
|
"Charge start", wm8350);
|
|
wm8350_register_irq(wm8350, WM8350_IRQ_CHG_FAST_RDY,
|
|
wm8350_charger_handler, 0,
|
|
"Fast charge ready", wm8350);
|
|
wm8350_register_irq(wm8350, WM8350_IRQ_CHG_VBATT_LT_3P9,
|
|
wm8350_charger_handler, 0,
|
|
"Battery <3.9V", wm8350);
|
|
wm8350_register_irq(wm8350, WM8350_IRQ_CHG_VBATT_LT_3P1,
|
|
wm8350_charger_handler, 0,
|
|
"Battery <3.1V", wm8350);
|
|
wm8350_register_irq(wm8350, WM8350_IRQ_CHG_VBATT_LT_2P85,
|
|
wm8350_charger_handler, 0,
|
|
"Battery <2.85V", wm8350);
|
|
|
|
/* and supply change events */
|
|
wm8350_register_irq(wm8350, WM8350_IRQ_EXT_USB_FB,
|
|
wm8350_charger_handler, 0, "USB", wm8350);
|
|
wm8350_register_irq(wm8350, WM8350_IRQ_EXT_WALL_FB,
|
|
wm8350_charger_handler, 0, "Wall", wm8350);
|
|
wm8350_register_irq(wm8350, WM8350_IRQ_EXT_BAT_FB,
|
|
wm8350_charger_handler, 0, "Battery", wm8350);
|
|
}
|
|
|
|
static void free_charger_irq(struct wm8350 *wm8350)
|
|
{
|
|
wm8350_free_irq(wm8350, WM8350_IRQ_CHG_BAT_HOT, wm8350);
|
|
wm8350_free_irq(wm8350, WM8350_IRQ_CHG_BAT_COLD, wm8350);
|
|
wm8350_free_irq(wm8350, WM8350_IRQ_CHG_BAT_FAIL, wm8350);
|
|
wm8350_free_irq(wm8350, WM8350_IRQ_CHG_TO, wm8350);
|
|
wm8350_free_irq(wm8350, WM8350_IRQ_CHG_END, wm8350);
|
|
wm8350_free_irq(wm8350, WM8350_IRQ_CHG_START, wm8350);
|
|
wm8350_free_irq(wm8350, WM8350_IRQ_CHG_VBATT_LT_3P9, wm8350);
|
|
wm8350_free_irq(wm8350, WM8350_IRQ_CHG_VBATT_LT_3P1, wm8350);
|
|
wm8350_free_irq(wm8350, WM8350_IRQ_CHG_VBATT_LT_2P85, wm8350);
|
|
wm8350_free_irq(wm8350, WM8350_IRQ_EXT_USB_FB, wm8350);
|
|
wm8350_free_irq(wm8350, WM8350_IRQ_EXT_WALL_FB, wm8350);
|
|
wm8350_free_irq(wm8350, WM8350_IRQ_EXT_BAT_FB, wm8350);
|
|
}
|
|
|
|
static int wm8350_power_probe(struct platform_device *pdev)
|
|
{
|
|
struct wm8350 *wm8350 = platform_get_drvdata(pdev);
|
|
struct wm8350_power *power = &wm8350->power;
|
|
struct wm8350_charger_policy *policy = power->policy;
|
|
int ret;
|
|
|
|
power->ac = power_supply_register(&pdev->dev, &wm8350_ac_desc, NULL);
|
|
if (IS_ERR(power->ac))
|
|
return PTR_ERR(power->ac);
|
|
|
|
power->battery = power_supply_register(&pdev->dev, &wm8350_battery_desc,
|
|
NULL);
|
|
if (IS_ERR(power->battery)) {
|
|
ret = PTR_ERR(power->battery);
|
|
goto battery_failed;
|
|
}
|
|
|
|
power->usb = power_supply_register(&pdev->dev, &wm8350_usb_desc, NULL);
|
|
if (IS_ERR(power->usb)) {
|
|
ret = PTR_ERR(power->usb);
|
|
goto usb_failed;
|
|
}
|
|
|
|
ret = device_create_file(&pdev->dev, &dev_attr_charger_state);
|
|
if (ret < 0)
|
|
dev_warn(wm8350->dev, "failed to add charge sysfs: %d\n", ret);
|
|
ret = 0;
|
|
|
|
wm8350_init_charger(wm8350);
|
|
if (wm8350_charger_config(wm8350, policy) == 0) {
|
|
wm8350_reg_unlock(wm8350);
|
|
wm8350_set_bits(wm8350, WM8350_POWER_MGMT_5, WM8350_CHG_ENA);
|
|
wm8350_reg_lock(wm8350);
|
|
}
|
|
|
|
return ret;
|
|
|
|
usb_failed:
|
|
power_supply_unregister(power->battery);
|
|
battery_failed:
|
|
power_supply_unregister(power->ac);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static int wm8350_power_remove(struct platform_device *pdev)
|
|
{
|
|
struct wm8350 *wm8350 = platform_get_drvdata(pdev);
|
|
struct wm8350_power *power = &wm8350->power;
|
|
|
|
free_charger_irq(wm8350);
|
|
device_remove_file(&pdev->dev, &dev_attr_charger_state);
|
|
power_supply_unregister(power->battery);
|
|
power_supply_unregister(power->ac);
|
|
power_supply_unregister(power->usb);
|
|
return 0;
|
|
}
|
|
|
|
static struct platform_driver wm8350_power_driver = {
|
|
.probe = wm8350_power_probe,
|
|
.remove = wm8350_power_remove,
|
|
.driver = {
|
|
.name = "wm8350-power",
|
|
},
|
|
};
|
|
|
|
module_platform_driver(wm8350_power_driver);
|
|
|
|
MODULE_LICENSE("GPL");
|
|
MODULE_DESCRIPTION("Power supply driver for WM8350");
|
|
MODULE_ALIAS("platform:wm8350-power");
|