2009-08-11 00:43:53 +08:00
|
|
|
/*
|
|
|
|
* 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 cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2009-08-11 00:43:53 +08:00
|
|
|
|
|
|
|
#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;
|
2015-03-12 15:44:11 +08:00
|
|
|
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;
|
2011-05-31 20:30:11 +08:00
|
|
|
char wall_name[20];
|
|
|
|
char usb_name[20];
|
|
|
|
char battery_name[20];
|
2011-08-16 07:37:42 +08:00
|
|
|
bool have_battery;
|
2009-08-11 00:43:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2015-03-12 15:44:11 +08:00
|
|
|
struct wm831x_power *wm831x_power = dev_get_drvdata(psy->dev.parent);
|
2009-08-11 00:43:53 +08:00
|
|
|
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)
|
|
|
|
{
|
2015-03-12 15:44:11 +08:00
|
|
|
struct wm831x_power *wm831x_power = dev_get_drvdata(psy->dev.parent);
|
2009-08-11 00:43:53 +08:00
|
|
|
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)
|
|
|
|
{
|
2015-03-12 15:44:11 +08:00
|
|
|
struct wm831x_power *wm831x_power = dev_get_drvdata(psy->dev.parent);
|
2009-08-11 00:43:53 +08:00
|
|
|
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 */
|
2011-08-16 07:37:42 +08:00
|
|
|
if (wm831x_power->have_battery)
|
2015-03-12 15:44:11 +08:00
|
|
|
power_supply_changed(wm831x_power->battery);
|
2009-08-11 00:43:53 +08:00
|
|
|
|
|
|
|
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");
|
|
|
|
|
2009-10-01 22:41:06 +08:00
|
|
|
/* Just notify for everything - little harm in overnotifying. */
|
2011-08-16 07:37:42 +08:00
|
|
|
if (wm831x_power->have_battery)
|
2015-03-12 15:44:11 +08:00
|
|
|
power_supply_changed(wm831x_power->battery);
|
|
|
|
power_supply_changed(wm831x_power->usb);
|
|
|
|
power_supply_changed(wm831x_power->wall);
|
2009-08-11 00:43:53 +08:00
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
2012-11-20 02:22:23 +08:00
|
|
|
static int wm831x_power_probe(struct platform_device *pdev)
|
2009-08-11 00:43:53 +08:00
|
|
|
{
|
|
|
|
struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
|
2011-05-31 20:30:11 +08:00
|
|
|
struct wm831x_pdata *wm831x_pdata = wm831x->dev->platform_data;
|
2009-08-11 00:43:53 +08:00
|
|
|
struct wm831x_power *power;
|
|
|
|
int ret, irq, i;
|
|
|
|
|
2015-08-21 06:12:43 +08:00
|
|
|
power = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_power),
|
|
|
|
GFP_KERNEL);
|
2009-08-11 00:43:53 +08:00
|
|
|
if (power == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
power->wm831x = wm831x;
|
|
|
|
platform_set_drvdata(pdev, power);
|
|
|
|
|
2011-05-31 20:30:11 +08:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2009-08-11 00:43:53 +08:00
|
|
|
/* We ignore configuration failures since we can still read back
|
2009-10-01 22:41:06 +08:00
|
|
|
* the status without enabling the charger.
|
2009-08-11 00:43:53 +08:00
|
|
|
*/
|
|
|
|
wm831x_config_battery(wm831x);
|
|
|
|
|
2015-03-12 15:44:11 +08:00
|
|
|
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);
|
2015-08-21 06:12:43 +08:00
|
|
|
goto err;
|
2015-03-12 15:44:11 +08:00
|
|
|
}
|
2009-08-11 00:43:53 +08:00
|
|
|
|
2015-03-12 15:44:11 +08:00
|
|
|
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);
|
2011-08-16 07:37:42 +08:00
|
|
|
goto err_wall;
|
2015-03-12 15:44:11 +08:00
|
|
|
}
|
2011-08-16 07:37:42 +08:00
|
|
|
|
|
|
|
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) {
|
2015-03-12 15:44:11 +08:00
|
|
|
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;
|
|
|
|
}
|
2011-08-16 07:37:42 +08:00
|
|
|
}
|
2009-08-11 00:43:53 +08:00
|
|
|
|
2012-05-15 05:14:24 +08:00
|
|
|
irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "SYSLO"));
|
2010-03-11 02:27:40 +08:00
|
|
|
ret = request_threaded_irq(irq, NULL, wm831x_syslo_irq,
|
2015-09-23 01:00:40 +08:00
|
|
|
IRQF_TRIGGER_RISING | IRQF_ONESHOT, "System power low",
|
2010-03-11 02:27:40 +08:00
|
|
|
power);
|
2009-08-11 00:43:53 +08:00
|
|
|
if (ret != 0) {
|
|
|
|
dev_err(&pdev->dev, "Failed to request SYSLO IRQ %d: %d\n",
|
|
|
|
irq, ret);
|
2011-08-16 07:37:42 +08:00
|
|
|
goto err_battery;
|
2009-08-11 00:43:53 +08:00
|
|
|
}
|
|
|
|
|
2012-05-15 05:14:24 +08:00
|
|
|
irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "PWR SRC"));
|
2010-03-11 02:27:40 +08:00
|
|
|
ret = request_threaded_irq(irq, NULL, wm831x_pwr_src_irq,
|
2015-09-23 01:00:40 +08:00
|
|
|
IRQF_TRIGGER_RISING | IRQF_ONESHOT, "Power source",
|
2010-03-11 02:27:40 +08:00
|
|
|
power);
|
2009-08-11 00:43:53 +08:00
|
|
|
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++) {
|
2012-05-15 05:14:24 +08:00
|
|
|
irq = wm831x_irq(wm831x,
|
|
|
|
platform_get_irq_byname(pdev,
|
|
|
|
wm831x_bat_irqs[i]));
|
2010-03-11 02:27:40 +08:00
|
|
|
ret = request_threaded_irq(irq, NULL, wm831x_bat_irq,
|
2015-09-23 01:00:40 +08:00
|
|
|
IRQF_TRIGGER_RISING | IRQF_ONESHOT,
|
2010-03-11 02:27:40 +08:00
|
|
|
wm831x_bat_irqs[i],
|
|
|
|
power);
|
2009-08-11 00:43:53 +08:00
|
|
|
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:
|
2015-06-14 19:55:18 +08:00
|
|
|
--i;
|
2009-08-11 00:43:53 +08:00
|
|
|
for (; i >= 0; i--) {
|
|
|
|
irq = platform_get_irq_byname(pdev, wm831x_bat_irqs[i]);
|
2010-03-11 02:27:40 +08:00
|
|
|
free_irq(irq, power);
|
2009-08-11 00:43:53 +08:00
|
|
|
}
|
2012-05-15 05:14:24 +08:00
|
|
|
irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "PWR SRC"));
|
2010-03-11 02:27:40 +08:00
|
|
|
free_irq(irq, power);
|
2009-08-11 00:43:53 +08:00
|
|
|
err_syslo:
|
2012-05-15 05:14:24 +08:00
|
|
|
irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "SYSLO"));
|
2010-03-11 02:27:40 +08:00
|
|
|
free_irq(irq, power);
|
2011-08-16 07:37:42 +08:00
|
|
|
err_battery:
|
|
|
|
if (power->have_battery)
|
2015-03-12 15:44:11 +08:00
|
|
|
power_supply_unregister(power->battery);
|
2009-08-11 00:43:53 +08:00
|
|
|
err_usb:
|
2015-03-12 15:44:11 +08:00
|
|
|
power_supply_unregister(power->usb);
|
2009-08-11 00:43:53 +08:00
|
|
|
err_wall:
|
2015-03-12 15:44:11 +08:00
|
|
|
power_supply_unregister(power->wall);
|
2015-08-21 06:12:43 +08:00
|
|
|
err:
|
2009-08-11 00:43:53 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-11-20 02:26:07 +08:00
|
|
|
static int wm831x_power_remove(struct platform_device *pdev)
|
2009-08-11 00:43:53 +08:00
|
|
|
{
|
|
|
|
struct wm831x_power *wm831x_power = platform_get_drvdata(pdev);
|
2012-05-15 05:14:24 +08:00
|
|
|
struct wm831x *wm831x = wm831x_power->wm831x;
|
2009-08-11 00:43:53 +08:00
|
|
|
int irq, i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(wm831x_bat_irqs); i++) {
|
2012-05-15 05:14:24 +08:00
|
|
|
irq = wm831x_irq(wm831x,
|
|
|
|
platform_get_irq_byname(pdev,
|
|
|
|
wm831x_bat_irqs[i]));
|
2010-03-11 02:27:40 +08:00
|
|
|
free_irq(irq, wm831x_power);
|
2009-08-11 00:43:53 +08:00
|
|
|
}
|
|
|
|
|
2012-05-15 05:14:24 +08:00
|
|
|
irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "PWR SRC"));
|
2010-03-11 02:27:40 +08:00
|
|
|
free_irq(irq, wm831x_power);
|
2009-08-11 00:43:53 +08:00
|
|
|
|
2012-05-15 05:14:24 +08:00
|
|
|
irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "SYSLO"));
|
2010-03-11 02:27:40 +08:00
|
|
|
free_irq(irq, wm831x_power);
|
2009-08-11 00:43:53 +08:00
|
|
|
|
2011-08-16 07:37:42 +08:00
|
|
|
if (wm831x_power->have_battery)
|
2015-03-12 15:44:11 +08:00
|
|
|
power_supply_unregister(wm831x_power->battery);
|
|
|
|
power_supply_unregister(wm831x_power->wall);
|
|
|
|
power_supply_unregister(wm831x_power->usb);
|
2009-08-11 00:43:53 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct platform_driver wm831x_power_driver = {
|
|
|
|
.probe = wm831x_power_probe,
|
2012-11-20 02:20:40 +08:00
|
|
|
.remove = wm831x_power_remove,
|
2009-08-11 00:43:53 +08:00
|
|
|
.driver = {
|
|
|
|
.name = "wm831x-power",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2011-11-26 12:01:10 +08:00
|
|
|
module_platform_driver(wm831x_power_driver);
|
2009-08-11 00:43:53 +08:00
|
|
|
|
|
|
|
MODULE_DESCRIPTION("Power supply driver for WM831x PMICs");
|
|
|
|
MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_ALIAS("platform:wm831x-power");
|