mirror of
https://github.com/AuxXxilium/linux_dsm_epyc7002.git
synced 2024-12-25 21:45:32 +07:00
297d716f62
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>
674 lines
18 KiB
C
674 lines
18 KiB
C
/*
|
|
* PMU driver for Wolfson Microelectronics wm831x PMICs
|
|
*
|
|
* Copyright 2009 Wolfson Microelectronics PLC.
|
|
*
|
|
* 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/slab.h>
|
|
|
|
#include <linux/mfd/wm831x/core.h>
|
|
#include <linux/mfd/wm831x/auxadc.h>
|
|
#include <linux/mfd/wm831x/pmu.h>
|
|
#include <linux/mfd/wm831x/pdata.h>
|
|
|
|
struct wm831x_power {
|
|
struct wm831x *wm831x;
|
|
struct power_supply *wall;
|
|
struct power_supply *usb;
|
|
struct power_supply *battery;
|
|
struct power_supply_desc wall_desc;
|
|
struct power_supply_desc usb_desc;
|
|
struct power_supply_desc battery_desc;
|
|
char wall_name[20];
|
|
char usb_name[20];
|
|
char battery_name[20];
|
|
bool have_battery;
|
|
};
|
|
|
|
static int wm831x_power_check_online(struct wm831x *wm831x, int supply,
|
|
union power_supply_propval *val)
|
|
{
|
|
int ret;
|
|
|
|
ret = wm831x_reg_read(wm831x, WM831X_SYSTEM_STATUS);
|
|
if (ret < 0)
|
|
return ret;
|
|
|
|
if (ret & supply)
|
|
val->intval = 1;
|
|
else
|
|
val->intval = 0;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int wm831x_power_read_voltage(struct wm831x *wm831x,
|
|
enum wm831x_auxadc src,
|
|
union power_supply_propval *val)
|
|
{
|
|
int ret;
|
|
|
|
ret = wm831x_auxadc_read_uv(wm831x, src);
|
|
if (ret >= 0)
|
|
val->intval = ret;
|
|
|
|
return ret;
|
|
}
|
|
|
|
/*********************************************************************
|
|
* WALL Power
|
|
*********************************************************************/
|
|
static int wm831x_wall_get_prop(struct power_supply *psy,
|
|
enum power_supply_property psp,
|
|
union power_supply_propval *val)
|
|
{
|
|
struct wm831x_power *wm831x_power = dev_get_drvdata(psy->dev.parent);
|
|
struct wm831x *wm831x = wm831x_power->wm831x;
|
|
int ret = 0;
|
|
|
|
switch (psp) {
|
|
case POWER_SUPPLY_PROP_ONLINE:
|
|
ret = wm831x_power_check_online(wm831x, WM831X_PWR_WALL, val);
|
|
break;
|
|
case POWER_SUPPLY_PROP_VOLTAGE_NOW:
|
|
ret = wm831x_power_read_voltage(wm831x, WM831X_AUX_WALL, val);
|
|
break;
|
|
default:
|
|
ret = -EINVAL;
|
|
break;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
static enum power_supply_property wm831x_wall_props[] = {
|
|
POWER_SUPPLY_PROP_ONLINE,
|
|
POWER_SUPPLY_PROP_VOLTAGE_NOW,
|
|
};
|
|
|
|
/*********************************************************************
|
|
* USB Power
|
|
*********************************************************************/
|
|
static int wm831x_usb_get_prop(struct power_supply *psy,
|
|
enum power_supply_property psp,
|
|
union power_supply_propval *val)
|
|
{
|
|
struct wm831x_power *wm831x_power = dev_get_drvdata(psy->dev.parent);
|
|
struct wm831x *wm831x = wm831x_power->wm831x;
|
|
int ret = 0;
|
|
|
|
switch (psp) {
|
|
case POWER_SUPPLY_PROP_ONLINE:
|
|
ret = wm831x_power_check_online(wm831x, WM831X_PWR_USB, val);
|
|
break;
|
|
case POWER_SUPPLY_PROP_VOLTAGE_NOW:
|
|
ret = wm831x_power_read_voltage(wm831x, WM831X_AUX_USB, val);
|
|
break;
|
|
default:
|
|
ret = -EINVAL;
|
|
break;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
static enum power_supply_property wm831x_usb_props[] = {
|
|
POWER_SUPPLY_PROP_ONLINE,
|
|
POWER_SUPPLY_PROP_VOLTAGE_NOW,
|
|
};
|
|
|
|
/*********************************************************************
|
|
* Battery properties
|
|
*********************************************************************/
|
|
|
|
struct chg_map {
|
|
int val;
|
|
int reg_val;
|
|
};
|
|
|
|
static struct chg_map trickle_ilims[] = {
|
|
{ 50, 0 << WM831X_CHG_TRKL_ILIM_SHIFT },
|
|
{ 100, 1 << WM831X_CHG_TRKL_ILIM_SHIFT },
|
|
{ 150, 2 << WM831X_CHG_TRKL_ILIM_SHIFT },
|
|
{ 200, 3 << WM831X_CHG_TRKL_ILIM_SHIFT },
|
|
};
|
|
|
|
static struct chg_map vsels[] = {
|
|
{ 4050, 0 << WM831X_CHG_VSEL_SHIFT },
|
|
{ 4100, 1 << WM831X_CHG_VSEL_SHIFT },
|
|
{ 4150, 2 << WM831X_CHG_VSEL_SHIFT },
|
|
{ 4200, 3 << WM831X_CHG_VSEL_SHIFT },
|
|
};
|
|
|
|
static struct chg_map fast_ilims[] = {
|
|
{ 0, 0 << WM831X_CHG_FAST_ILIM_SHIFT },
|
|
{ 50, 1 << WM831X_CHG_FAST_ILIM_SHIFT },
|
|
{ 100, 2 << WM831X_CHG_FAST_ILIM_SHIFT },
|
|
{ 150, 3 << WM831X_CHG_FAST_ILIM_SHIFT },
|
|
{ 200, 4 << WM831X_CHG_FAST_ILIM_SHIFT },
|
|
{ 250, 5 << WM831X_CHG_FAST_ILIM_SHIFT },
|
|
{ 300, 6 << WM831X_CHG_FAST_ILIM_SHIFT },
|
|
{ 350, 7 << WM831X_CHG_FAST_ILIM_SHIFT },
|
|
{ 400, 8 << WM831X_CHG_FAST_ILIM_SHIFT },
|
|
{ 450, 9 << WM831X_CHG_FAST_ILIM_SHIFT },
|
|
{ 500, 10 << WM831X_CHG_FAST_ILIM_SHIFT },
|
|
{ 600, 11 << WM831X_CHG_FAST_ILIM_SHIFT },
|
|
{ 700, 12 << WM831X_CHG_FAST_ILIM_SHIFT },
|
|
{ 800, 13 << WM831X_CHG_FAST_ILIM_SHIFT },
|
|
{ 900, 14 << WM831X_CHG_FAST_ILIM_SHIFT },
|
|
{ 1000, 15 << WM831X_CHG_FAST_ILIM_SHIFT },
|
|
};
|
|
|
|
static struct chg_map eoc_iterms[] = {
|
|
{ 20, 0 << WM831X_CHG_ITERM_SHIFT },
|
|
{ 30, 1 << WM831X_CHG_ITERM_SHIFT },
|
|
{ 40, 2 << WM831X_CHG_ITERM_SHIFT },
|
|
{ 50, 3 << WM831X_CHG_ITERM_SHIFT },
|
|
{ 60, 4 << WM831X_CHG_ITERM_SHIFT },
|
|
{ 70, 5 << WM831X_CHG_ITERM_SHIFT },
|
|
{ 80, 6 << WM831X_CHG_ITERM_SHIFT },
|
|
{ 90, 7 << WM831X_CHG_ITERM_SHIFT },
|
|
};
|
|
|
|
static struct chg_map chg_times[] = {
|
|
{ 60, 0 << WM831X_CHG_TIME_SHIFT },
|
|
{ 90, 1 << WM831X_CHG_TIME_SHIFT },
|
|
{ 120, 2 << WM831X_CHG_TIME_SHIFT },
|
|
{ 150, 3 << WM831X_CHG_TIME_SHIFT },
|
|
{ 180, 4 << WM831X_CHG_TIME_SHIFT },
|
|
{ 210, 5 << WM831X_CHG_TIME_SHIFT },
|
|
{ 240, 6 << WM831X_CHG_TIME_SHIFT },
|
|
{ 270, 7 << WM831X_CHG_TIME_SHIFT },
|
|
{ 300, 8 << WM831X_CHG_TIME_SHIFT },
|
|
{ 330, 9 << WM831X_CHG_TIME_SHIFT },
|
|
{ 360, 10 << WM831X_CHG_TIME_SHIFT },
|
|
{ 390, 11 << WM831X_CHG_TIME_SHIFT },
|
|
{ 420, 12 << WM831X_CHG_TIME_SHIFT },
|
|
{ 450, 13 << WM831X_CHG_TIME_SHIFT },
|
|
{ 480, 14 << WM831X_CHG_TIME_SHIFT },
|
|
{ 510, 15 << WM831X_CHG_TIME_SHIFT },
|
|
};
|
|
|
|
static void wm831x_battey_apply_config(struct wm831x *wm831x,
|
|
struct chg_map *map, int count, int val,
|
|
int *reg, const char *name,
|
|
const char *units)
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < count; i++)
|
|
if (val == map[i].val)
|
|
break;
|
|
if (i == count) {
|
|
dev_err(wm831x->dev, "Invalid %s %d%s\n",
|
|
name, val, units);
|
|
} else {
|
|
*reg |= map[i].reg_val;
|
|
dev_dbg(wm831x->dev, "Set %s of %d%s\n", name, val, units);
|
|
}
|
|
}
|
|
|
|
static void wm831x_config_battery(struct wm831x *wm831x)
|
|
{
|
|
struct wm831x_pdata *wm831x_pdata = wm831x->dev->platform_data;
|
|
struct wm831x_battery_pdata *pdata;
|
|
int ret, reg1, reg2;
|
|
|
|
if (!wm831x_pdata || !wm831x_pdata->battery) {
|
|
dev_warn(wm831x->dev,
|
|
"No battery charger configuration\n");
|
|
return;
|
|
}
|
|
|
|
pdata = wm831x_pdata->battery;
|
|
|
|
reg1 = 0;
|
|
reg2 = 0;
|
|
|
|
if (!pdata->enable) {
|
|
dev_info(wm831x->dev, "Battery charger disabled\n");
|
|
return;
|
|
}
|
|
|
|
reg1 |= WM831X_CHG_ENA;
|
|
if (pdata->off_mask)
|
|
reg2 |= WM831X_CHG_OFF_MSK;
|
|
if (pdata->fast_enable)
|
|
reg1 |= WM831X_CHG_FAST;
|
|
|
|
wm831x_battey_apply_config(wm831x, trickle_ilims,
|
|
ARRAY_SIZE(trickle_ilims),
|
|
pdata->trickle_ilim, ®2,
|
|
"trickle charge current limit", "mA");
|
|
|
|
wm831x_battey_apply_config(wm831x, vsels, ARRAY_SIZE(vsels),
|
|
pdata->vsel, ®2,
|
|
"target voltage", "mV");
|
|
|
|
wm831x_battey_apply_config(wm831x, fast_ilims, ARRAY_SIZE(fast_ilims),
|
|
pdata->fast_ilim, ®2,
|
|
"fast charge current limit", "mA");
|
|
|
|
wm831x_battey_apply_config(wm831x, eoc_iterms, ARRAY_SIZE(eoc_iterms),
|
|
pdata->eoc_iterm, ®1,
|
|
"end of charge current threshold", "mA");
|
|
|
|
wm831x_battey_apply_config(wm831x, chg_times, ARRAY_SIZE(chg_times),
|
|
pdata->timeout, ®2,
|
|
"charger timeout", "min");
|
|
|
|
ret = wm831x_reg_unlock(wm831x);
|
|
if (ret != 0) {
|
|
dev_err(wm831x->dev, "Failed to unlock registers: %d\n", ret);
|
|
return;
|
|
}
|
|
|
|
ret = wm831x_set_bits(wm831x, WM831X_CHARGER_CONTROL_1,
|
|
WM831X_CHG_ENA_MASK |
|
|
WM831X_CHG_FAST_MASK |
|
|
WM831X_CHG_ITERM_MASK,
|
|
reg1);
|
|
if (ret != 0)
|
|
dev_err(wm831x->dev, "Failed to set charger control 1: %d\n",
|
|
ret);
|
|
|
|
ret = wm831x_set_bits(wm831x, WM831X_CHARGER_CONTROL_2,
|
|
WM831X_CHG_OFF_MSK |
|
|
WM831X_CHG_TIME_MASK |
|
|
WM831X_CHG_FAST_ILIM_MASK |
|
|
WM831X_CHG_TRKL_ILIM_MASK |
|
|
WM831X_CHG_VSEL_MASK,
|
|
reg2);
|
|
if (ret != 0)
|
|
dev_err(wm831x->dev, "Failed to set charger control 2: %d\n",
|
|
ret);
|
|
|
|
wm831x_reg_lock(wm831x);
|
|
}
|
|
|
|
static int wm831x_bat_check_status(struct wm831x *wm831x, int *status)
|
|
{
|
|
int ret;
|
|
|
|
ret = wm831x_reg_read(wm831x, WM831X_SYSTEM_STATUS);
|
|
if (ret < 0)
|
|
return ret;
|
|
|
|
if (ret & WM831X_PWR_SRC_BATT) {
|
|
*status = POWER_SUPPLY_STATUS_DISCHARGING;
|
|
return 0;
|
|
}
|
|
|
|
ret = wm831x_reg_read(wm831x, WM831X_CHARGER_STATUS);
|
|
if (ret < 0)
|
|
return ret;
|
|
|
|
switch (ret & WM831X_CHG_STATE_MASK) {
|
|
case WM831X_CHG_STATE_OFF:
|
|
*status = POWER_SUPPLY_STATUS_NOT_CHARGING;
|
|
break;
|
|
case WM831X_CHG_STATE_TRICKLE:
|
|
case WM831X_CHG_STATE_FAST:
|
|
*status = POWER_SUPPLY_STATUS_CHARGING;
|
|
break;
|
|
|
|
default:
|
|
*status = POWER_SUPPLY_STATUS_UNKNOWN;
|
|
break;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int wm831x_bat_check_type(struct wm831x *wm831x, int *type)
|
|
{
|
|
int ret;
|
|
|
|
ret = wm831x_reg_read(wm831x, WM831X_CHARGER_STATUS);
|
|
if (ret < 0)
|
|
return ret;
|
|
|
|
switch (ret & WM831X_CHG_STATE_MASK) {
|
|
case WM831X_CHG_STATE_TRICKLE:
|
|
case WM831X_CHG_STATE_TRICKLE_OT:
|
|
*type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
|
|
break;
|
|
case WM831X_CHG_STATE_FAST:
|
|
case WM831X_CHG_STATE_FAST_OT:
|
|
*type = POWER_SUPPLY_CHARGE_TYPE_FAST;
|
|
break;
|
|
default:
|
|
*type = POWER_SUPPLY_CHARGE_TYPE_NONE;
|
|
break;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int wm831x_bat_check_health(struct wm831x *wm831x, int *health)
|
|
{
|
|
int ret;
|
|
|
|
ret = wm831x_reg_read(wm831x, WM831X_CHARGER_STATUS);
|
|
if (ret < 0)
|
|
return ret;
|
|
|
|
if (ret & WM831X_BATT_HOT_STS) {
|
|
*health = POWER_SUPPLY_HEALTH_OVERHEAT;
|
|
return 0;
|
|
}
|
|
|
|
if (ret & WM831X_BATT_COLD_STS) {
|
|
*health = POWER_SUPPLY_HEALTH_COLD;
|
|
return 0;
|
|
}
|
|
|
|
if (ret & WM831X_BATT_OV_STS) {
|
|
*health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
|
|
return 0;
|
|
}
|
|
|
|
switch (ret & WM831X_CHG_STATE_MASK) {
|
|
case WM831X_CHG_STATE_TRICKLE_OT:
|
|
case WM831X_CHG_STATE_FAST_OT:
|
|
*health = POWER_SUPPLY_HEALTH_OVERHEAT;
|
|
break;
|
|
case WM831X_CHG_STATE_DEFECTIVE:
|
|
*health = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
|
|
break;
|
|
default:
|
|
*health = POWER_SUPPLY_HEALTH_GOOD;
|
|
break;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int wm831x_bat_get_prop(struct power_supply *psy,
|
|
enum power_supply_property psp,
|
|
union power_supply_propval *val)
|
|
{
|
|
struct wm831x_power *wm831x_power = dev_get_drvdata(psy->dev.parent);
|
|
struct wm831x *wm831x = wm831x_power->wm831x;
|
|
int ret = 0;
|
|
|
|
switch (psp) {
|
|
case POWER_SUPPLY_PROP_STATUS:
|
|
ret = wm831x_bat_check_status(wm831x, &val->intval);
|
|
break;
|
|
case POWER_SUPPLY_PROP_ONLINE:
|
|
ret = wm831x_power_check_online(wm831x, WM831X_PWR_SRC_BATT,
|
|
val);
|
|
break;
|
|
case POWER_SUPPLY_PROP_VOLTAGE_NOW:
|
|
ret = wm831x_power_read_voltage(wm831x, WM831X_AUX_BATT, val);
|
|
break;
|
|
case POWER_SUPPLY_PROP_HEALTH:
|
|
ret = wm831x_bat_check_health(wm831x, &val->intval);
|
|
break;
|
|
case POWER_SUPPLY_PROP_CHARGE_TYPE:
|
|
ret = wm831x_bat_check_type(wm831x, &val->intval);
|
|
break;
|
|
default:
|
|
ret = -EINVAL;
|
|
break;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
static enum power_supply_property wm831x_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 char *wm831x_bat_irqs[] = {
|
|
"BATT HOT",
|
|
"BATT COLD",
|
|
"BATT FAIL",
|
|
"OV",
|
|
"END",
|
|
"TO",
|
|
"MODE",
|
|
"START",
|
|
};
|
|
|
|
static irqreturn_t wm831x_bat_irq(int irq, void *data)
|
|
{
|
|
struct wm831x_power *wm831x_power = data;
|
|
struct wm831x *wm831x = wm831x_power->wm831x;
|
|
|
|
dev_dbg(wm831x->dev, "Battery status changed: %d\n", irq);
|
|
|
|
/* The battery charger is autonomous so we don't need to do
|
|
* anything except kick user space */
|
|
if (wm831x_power->have_battery)
|
|
power_supply_changed(wm831x_power->battery);
|
|
|
|
return IRQ_HANDLED;
|
|
}
|
|
|
|
|
|
/*********************************************************************
|
|
* Initialisation
|
|
*********************************************************************/
|
|
|
|
static irqreturn_t wm831x_syslo_irq(int irq, void *data)
|
|
{
|
|
struct wm831x_power *wm831x_power = data;
|
|
struct wm831x *wm831x = wm831x_power->wm831x;
|
|
|
|
/* Not much we can actually *do* but tell people for
|
|
* posterity, we're probably about to run out of power. */
|
|
dev_crit(wm831x->dev, "SYSVDD under voltage\n");
|
|
|
|
return IRQ_HANDLED;
|
|
}
|
|
|
|
static irqreturn_t wm831x_pwr_src_irq(int irq, void *data)
|
|
{
|
|
struct wm831x_power *wm831x_power = data;
|
|
struct wm831x *wm831x = wm831x_power->wm831x;
|
|
|
|
dev_dbg(wm831x->dev, "Power source changed\n");
|
|
|
|
/* Just notify for everything - little harm in overnotifying. */
|
|
if (wm831x_power->have_battery)
|
|
power_supply_changed(wm831x_power->battery);
|
|
power_supply_changed(wm831x_power->usb);
|
|
power_supply_changed(wm831x_power->wall);
|
|
|
|
return IRQ_HANDLED;
|
|
}
|
|
|
|
static int wm831x_power_probe(struct platform_device *pdev)
|
|
{
|
|
struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
|
|
struct wm831x_pdata *wm831x_pdata = wm831x->dev->platform_data;
|
|
struct wm831x_power *power;
|
|
int ret, irq, i;
|
|
|
|
power = kzalloc(sizeof(struct wm831x_power), GFP_KERNEL);
|
|
if (power == NULL)
|
|
return -ENOMEM;
|
|
|
|
power->wm831x = wm831x;
|
|
platform_set_drvdata(pdev, power);
|
|
|
|
if (wm831x_pdata && wm831x_pdata->wm831x_num) {
|
|
snprintf(power->wall_name, sizeof(power->wall_name),
|
|
"wm831x-wall.%d", wm831x_pdata->wm831x_num);
|
|
snprintf(power->battery_name, sizeof(power->wall_name),
|
|
"wm831x-battery.%d", wm831x_pdata->wm831x_num);
|
|
snprintf(power->usb_name, sizeof(power->wall_name),
|
|
"wm831x-usb.%d", wm831x_pdata->wm831x_num);
|
|
} else {
|
|
snprintf(power->wall_name, sizeof(power->wall_name),
|
|
"wm831x-wall");
|
|
snprintf(power->battery_name, sizeof(power->wall_name),
|
|
"wm831x-battery");
|
|
snprintf(power->usb_name, sizeof(power->wall_name),
|
|
"wm831x-usb");
|
|
}
|
|
|
|
/* We ignore configuration failures since we can still read back
|
|
* the status without enabling the charger.
|
|
*/
|
|
wm831x_config_battery(wm831x);
|
|
|
|
power->wall_desc.name = power->wall_name;
|
|
power->wall_desc.type = POWER_SUPPLY_TYPE_MAINS;
|
|
power->wall_desc.properties = wm831x_wall_props;
|
|
power->wall_desc.num_properties = ARRAY_SIZE(wm831x_wall_props);
|
|
power->wall_desc.get_property = wm831x_wall_get_prop;
|
|
power->wall = power_supply_register(&pdev->dev, &power->wall_desc,
|
|
NULL);
|
|
if (IS_ERR(power->wall)) {
|
|
ret = PTR_ERR(power->wall);
|
|
goto err_kmalloc;
|
|
}
|
|
|
|
power->usb_desc.name = power->usb_name,
|
|
power->usb_desc.type = POWER_SUPPLY_TYPE_USB;
|
|
power->usb_desc.properties = wm831x_usb_props;
|
|
power->usb_desc.num_properties = ARRAY_SIZE(wm831x_usb_props);
|
|
power->usb_desc.get_property = wm831x_usb_get_prop;
|
|
power->usb = power_supply_register(&pdev->dev, &power->usb_desc, NULL);
|
|
if (IS_ERR(power->usb)) {
|
|
ret = PTR_ERR(power->usb);
|
|
goto err_wall;
|
|
}
|
|
|
|
ret = wm831x_reg_read(wm831x, WM831X_CHARGER_CONTROL_1);
|
|
if (ret < 0)
|
|
goto err_wall;
|
|
power->have_battery = ret & WM831X_CHG_ENA;
|
|
|
|
if (power->have_battery) {
|
|
power->battery_desc.name = power->battery_name;
|
|
power->battery_desc.properties = wm831x_bat_props;
|
|
power->battery_desc.num_properties = ARRAY_SIZE(wm831x_bat_props);
|
|
power->battery_desc.get_property = wm831x_bat_get_prop;
|
|
power->battery_desc.use_for_apm = 1;
|
|
power->battery = power_supply_register(&pdev->dev,
|
|
&power->battery_desc,
|
|
NULL);
|
|
if (IS_ERR(power->battery)) {
|
|
ret = PTR_ERR(power->battery);
|
|
goto err_usb;
|
|
}
|
|
}
|
|
|
|
irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "SYSLO"));
|
|
ret = request_threaded_irq(irq, NULL, wm831x_syslo_irq,
|
|
IRQF_TRIGGER_RISING, "System power low",
|
|
power);
|
|
if (ret != 0) {
|
|
dev_err(&pdev->dev, "Failed to request SYSLO IRQ %d: %d\n",
|
|
irq, ret);
|
|
goto err_battery;
|
|
}
|
|
|
|
irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "PWR SRC"));
|
|
ret = request_threaded_irq(irq, NULL, wm831x_pwr_src_irq,
|
|
IRQF_TRIGGER_RISING, "Power source",
|
|
power);
|
|
if (ret != 0) {
|
|
dev_err(&pdev->dev, "Failed to request PWR SRC IRQ %d: %d\n",
|
|
irq, ret);
|
|
goto err_syslo;
|
|
}
|
|
|
|
for (i = 0; i < ARRAY_SIZE(wm831x_bat_irqs); i++) {
|
|
irq = wm831x_irq(wm831x,
|
|
platform_get_irq_byname(pdev,
|
|
wm831x_bat_irqs[i]));
|
|
ret = request_threaded_irq(irq, NULL, wm831x_bat_irq,
|
|
IRQF_TRIGGER_RISING,
|
|
wm831x_bat_irqs[i],
|
|
power);
|
|
if (ret != 0) {
|
|
dev_err(&pdev->dev,
|
|
"Failed to request %s IRQ %d: %d\n",
|
|
wm831x_bat_irqs[i], irq, ret);
|
|
goto err_bat_irq;
|
|
}
|
|
}
|
|
|
|
return ret;
|
|
|
|
err_bat_irq:
|
|
for (; i >= 0; i--) {
|
|
irq = platform_get_irq_byname(pdev, wm831x_bat_irqs[i]);
|
|
free_irq(irq, power);
|
|
}
|
|
irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "PWR SRC"));
|
|
free_irq(irq, power);
|
|
err_syslo:
|
|
irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "SYSLO"));
|
|
free_irq(irq, power);
|
|
err_battery:
|
|
if (power->have_battery)
|
|
power_supply_unregister(power->battery);
|
|
err_usb:
|
|
power_supply_unregister(power->usb);
|
|
err_wall:
|
|
power_supply_unregister(power->wall);
|
|
err_kmalloc:
|
|
kfree(power);
|
|
return ret;
|
|
}
|
|
|
|
static int wm831x_power_remove(struct platform_device *pdev)
|
|
{
|
|
struct wm831x_power *wm831x_power = platform_get_drvdata(pdev);
|
|
struct wm831x *wm831x = wm831x_power->wm831x;
|
|
int irq, i;
|
|
|
|
for (i = 0; i < ARRAY_SIZE(wm831x_bat_irqs); i++) {
|
|
irq = wm831x_irq(wm831x,
|
|
platform_get_irq_byname(pdev,
|
|
wm831x_bat_irqs[i]));
|
|
free_irq(irq, wm831x_power);
|
|
}
|
|
|
|
irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "PWR SRC"));
|
|
free_irq(irq, wm831x_power);
|
|
|
|
irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "SYSLO"));
|
|
free_irq(irq, wm831x_power);
|
|
|
|
if (wm831x_power->have_battery)
|
|
power_supply_unregister(wm831x_power->battery);
|
|
power_supply_unregister(wm831x_power->wall);
|
|
power_supply_unregister(wm831x_power->usb);
|
|
kfree(wm831x_power);
|
|
return 0;
|
|
}
|
|
|
|
static struct platform_driver wm831x_power_driver = {
|
|
.probe = wm831x_power_probe,
|
|
.remove = wm831x_power_remove,
|
|
.driver = {
|
|
.name = "wm831x-power",
|
|
},
|
|
};
|
|
|
|
module_platform_driver(wm831x_power_driver);
|
|
|
|
MODULE_DESCRIPTION("Power supply driver for WM831x PMICs");
|
|
MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
|
|
MODULE_LICENSE("GPL");
|
|
MODULE_ALIAS("platform:wm831x-power");
|