2018-08-08 00:21:06 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
//
|
|
|
|
// max17040_battery.c
|
|
|
|
// fuel-gauge systems for lithium-ion (Li+) batteries
|
|
|
|
//
|
|
|
|
// Copyright (C) 2009 Samsung Electronics
|
|
|
|
// Minkyu Kang <mk7.kang@samsung.com>
|
2009-06-05 14:33:04 +08:00
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/mutex.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/i2c.h>
|
|
|
|
#include <linux/delay.h>
|
2019-12-05 23:44:06 +08:00
|
|
|
#include <linux/interrupt.h>
|
2009-06-05 14:33:04 +08:00
|
|
|
#include <linux/power_supply.h>
|
2020-09-22 19:42:34 +08:00
|
|
|
#include <linux/of_device.h>
|
2009-06-05 14:33:04 +08:00
|
|
|
#include <linux/max17040_battery.h>
|
2020-09-22 19:42:32 +08:00
|
|
|
#include <linux/regmap.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-06-05 14:33:04 +08:00
|
|
|
|
2016-09-21 22:17:34 +08:00
|
|
|
#define MAX17040_VCELL 0x02
|
|
|
|
#define MAX17040_SOC 0x04
|
|
|
|
#define MAX17040_MODE 0x06
|
|
|
|
#define MAX17040_VER 0x08
|
2020-09-22 19:42:32 +08:00
|
|
|
#define MAX17040_CONFIG 0x0C
|
2020-09-22 19:42:37 +08:00
|
|
|
#define MAX17040_STATUS 0x1A
|
2016-09-21 22:17:34 +08:00
|
|
|
#define MAX17040_CMD 0xFE
|
|
|
|
|
2009-06-05 14:33:04 +08:00
|
|
|
|
|
|
|
#define MAX17040_DELAY 1000
|
|
|
|
#define MAX17040_BATTERY_FULL 95
|
2020-09-22 19:42:36 +08:00
|
|
|
#define MAX17040_RCOMP_DEFAULT 0x9700
|
2009-06-05 14:33:04 +08:00
|
|
|
|
2020-09-22 19:42:32 +08:00
|
|
|
#define MAX17040_ATHD_MASK 0x3f
|
2020-09-22 19:42:37 +08:00
|
|
|
#define MAX17040_ALSC_MASK 0x40
|
2019-12-05 23:44:09 +08:00
|
|
|
#define MAX17040_ATHD_DEFAULT_POWER_UP 4
|
2020-09-22 19:42:37 +08:00
|
|
|
#define MAX17040_STATUS_HD_MASK 0x1000
|
|
|
|
#define MAX17040_STATUS_SC_MASK 0x2000
|
2020-09-22 19:42:36 +08:00
|
|
|
#define MAX17040_CFG_RCOMP_MASK 0xff00
|
2019-12-05 23:44:09 +08:00
|
|
|
|
2020-09-22 19:42:34 +08:00
|
|
|
enum chip_id {
|
|
|
|
ID_MAX17040,
|
|
|
|
ID_MAX17041,
|
|
|
|
ID_MAX17043,
|
|
|
|
ID_MAX17044,
|
|
|
|
ID_MAX17048,
|
|
|
|
ID_MAX17049,
|
|
|
|
ID_MAX17058,
|
|
|
|
ID_MAX17059,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* values that differ by chip_id */
|
|
|
|
struct chip_data {
|
|
|
|
u16 reset_val;
|
|
|
|
u16 vcell_shift;
|
|
|
|
u16 vcell_mul;
|
|
|
|
u16 vcell_div;
|
|
|
|
u8 has_low_soc_alert;
|
2020-09-22 19:42:36 +08:00
|
|
|
u8 rcomp_bytes;
|
2020-09-22 19:42:37 +08:00
|
|
|
u8 has_soc_alert;
|
2020-09-22 19:42:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct chip_data max17040_family[] = {
|
|
|
|
[ID_MAX17040] = {
|
|
|
|
.reset_val = 0x0054,
|
|
|
|
.vcell_shift = 4,
|
|
|
|
.vcell_mul = 1250,
|
|
|
|
.vcell_div = 1,
|
|
|
|
.has_low_soc_alert = 0,
|
2020-09-22 19:42:36 +08:00
|
|
|
.rcomp_bytes = 2,
|
2020-09-22 19:42:37 +08:00
|
|
|
.has_soc_alert = 0,
|
2020-09-22 19:42:34 +08:00
|
|
|
},
|
|
|
|
[ID_MAX17041] = {
|
|
|
|
.reset_val = 0x0054,
|
|
|
|
.vcell_shift = 4,
|
|
|
|
.vcell_mul = 2500,
|
|
|
|
.vcell_div = 1,
|
|
|
|
.has_low_soc_alert = 0,
|
2020-09-22 19:42:36 +08:00
|
|
|
.rcomp_bytes = 2,
|
2020-09-22 19:42:37 +08:00
|
|
|
.has_soc_alert = 0,
|
2020-09-22 19:42:34 +08:00
|
|
|
},
|
|
|
|
[ID_MAX17043] = {
|
|
|
|
.reset_val = 0x0054,
|
|
|
|
.vcell_shift = 4,
|
|
|
|
.vcell_mul = 1250,
|
|
|
|
.vcell_div = 1,
|
|
|
|
.has_low_soc_alert = 1,
|
2020-09-22 19:42:36 +08:00
|
|
|
.rcomp_bytes = 1,
|
2020-09-22 19:42:37 +08:00
|
|
|
.has_soc_alert = 0,
|
2020-09-22 19:42:34 +08:00
|
|
|
},
|
|
|
|
[ID_MAX17044] = {
|
|
|
|
.reset_val = 0x0054,
|
|
|
|
.vcell_shift = 4,
|
|
|
|
.vcell_mul = 2500,
|
|
|
|
.vcell_div = 1,
|
|
|
|
.has_low_soc_alert = 1,
|
2020-09-22 19:42:36 +08:00
|
|
|
.rcomp_bytes = 1,
|
2020-09-22 19:42:37 +08:00
|
|
|
.has_soc_alert = 0,
|
2020-09-22 19:42:34 +08:00
|
|
|
},
|
|
|
|
[ID_MAX17048] = {
|
|
|
|
.reset_val = 0x5400,
|
|
|
|
.vcell_shift = 0,
|
|
|
|
.vcell_mul = 625,
|
|
|
|
.vcell_div = 8,
|
|
|
|
.has_low_soc_alert = 1,
|
2020-09-22 19:42:36 +08:00
|
|
|
.rcomp_bytes = 1,
|
2020-09-22 19:42:37 +08:00
|
|
|
.has_soc_alert = 1,
|
2020-09-22 19:42:34 +08:00
|
|
|
},
|
|
|
|
[ID_MAX17049] = {
|
|
|
|
.reset_val = 0x5400,
|
|
|
|
.vcell_shift = 0,
|
|
|
|
.vcell_mul = 625,
|
|
|
|
.vcell_div = 4,
|
|
|
|
.has_low_soc_alert = 1,
|
2020-09-22 19:42:36 +08:00
|
|
|
.rcomp_bytes = 1,
|
2020-09-22 19:42:37 +08:00
|
|
|
.has_soc_alert = 1,
|
2020-09-22 19:42:34 +08:00
|
|
|
},
|
|
|
|
[ID_MAX17058] = {
|
|
|
|
.reset_val = 0x5400,
|
|
|
|
.vcell_shift = 0,
|
|
|
|
.vcell_mul = 625,
|
|
|
|
.vcell_div = 8,
|
|
|
|
.has_low_soc_alert = 1,
|
2020-09-22 19:42:36 +08:00
|
|
|
.rcomp_bytes = 1,
|
2020-09-22 19:42:37 +08:00
|
|
|
.has_soc_alert = 0,
|
2020-09-22 19:42:34 +08:00
|
|
|
},
|
|
|
|
[ID_MAX17059] = {
|
|
|
|
.reset_val = 0x5400,
|
|
|
|
.vcell_shift = 0,
|
|
|
|
.vcell_mul = 625,
|
|
|
|
.vcell_div = 4,
|
|
|
|
.has_low_soc_alert = 1,
|
2020-09-22 19:42:36 +08:00
|
|
|
.rcomp_bytes = 1,
|
2020-09-22 19:42:37 +08:00
|
|
|
.has_soc_alert = 0,
|
2020-09-22 19:42:34 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2009-06-05 14:33:04 +08:00
|
|
|
struct max17040_chip {
|
|
|
|
struct i2c_client *client;
|
2020-09-22 19:42:32 +08:00
|
|
|
struct regmap *regmap;
|
2009-06-05 14:33:04 +08:00
|
|
|
struct delayed_work work;
|
2015-03-12 15:44:11 +08:00
|
|
|
struct power_supply *battery;
|
2009-06-05 14:33:04 +08:00
|
|
|
struct max17040_platform_data *pdata;
|
2020-09-22 19:42:34 +08:00
|
|
|
struct chip_data data;
|
2009-06-05 14:33:04 +08:00
|
|
|
|
|
|
|
/* battery capacity */
|
|
|
|
int soc;
|
|
|
|
/* State Of Charge */
|
|
|
|
int status;
|
2019-12-05 23:44:09 +08:00
|
|
|
/* Low alert threshold from 32% to 1% of the State of Charge */
|
|
|
|
u32 low_soc_alert;
|
2020-09-22 19:42:34 +08:00
|
|
|
/* some devices return twice the capacity */
|
|
|
|
bool quirk_double_soc;
|
2020-09-22 19:42:36 +08:00
|
|
|
/* higher 8 bits for 17043+, 16 bits for 17040,41 */
|
|
|
|
u16 rcomp;
|
2009-06-05 14:33:04 +08:00
|
|
|
};
|
|
|
|
|
2020-09-22 19:42:32 +08:00
|
|
|
static int max17040_reset(struct max17040_chip *chip)
|
2009-06-05 14:33:04 +08:00
|
|
|
{
|
2020-09-22 19:42:34 +08:00
|
|
|
return regmap_write(chip->regmap, MAX17040_CMD, chip->data.reset_val);
|
2009-06-05 14:33:04 +08:00
|
|
|
}
|
|
|
|
|
2020-09-22 19:42:32 +08:00
|
|
|
static int max17040_set_low_soc_alert(struct max17040_chip *chip, u32 level)
|
2009-06-05 14:33:04 +08:00
|
|
|
{
|
2020-09-22 19:42:34 +08:00
|
|
|
level = 32 - level * (chip->quirk_double_soc ? 2 : 1);
|
2020-09-22 19:42:32 +08:00
|
|
|
return regmap_update_bits(chip->regmap, MAX17040_CONFIG,
|
|
|
|
MAX17040_ATHD_MASK, level);
|
2019-12-05 23:44:09 +08:00
|
|
|
}
|
|
|
|
|
2020-09-22 19:42:37 +08:00
|
|
|
static int max17040_set_soc_alert(struct max17040_chip *chip, bool enable)
|
|
|
|
{
|
|
|
|
return regmap_update_bits(chip->regmap, MAX17040_CONFIG,
|
|
|
|
MAX17040_ALSC_MASK, enable ? MAX17040_ALSC_MASK : 0);
|
|
|
|
}
|
|
|
|
|
2020-09-22 19:42:36 +08:00
|
|
|
static int max17040_set_rcomp(struct max17040_chip *chip, u16 rcomp)
|
|
|
|
{
|
|
|
|
u16 mask = chip->data.rcomp_bytes == 2 ?
|
|
|
|
0xffff : MAX17040_CFG_RCOMP_MASK;
|
|
|
|
|
|
|
|
return regmap_update_bits(chip->regmap, MAX17040_CONFIG, mask, rcomp);
|
|
|
|
}
|
|
|
|
|
2020-09-22 19:42:34 +08:00
|
|
|
static int max17040_raw_vcell_to_uvolts(struct max17040_chip *chip, u16 vcell)
|
|
|
|
{
|
|
|
|
struct chip_data *d = &chip->data;
|
|
|
|
|
|
|
|
return (vcell >> d->vcell_shift) * d->vcell_mul / d->vcell_div;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-22 19:42:32 +08:00
|
|
|
static int max17040_get_vcell(struct max17040_chip *chip)
|
2009-06-05 14:33:04 +08:00
|
|
|
{
|
2020-09-22 19:42:32 +08:00
|
|
|
u32 vcell;
|
2009-06-05 14:33:04 +08:00
|
|
|
|
2020-09-22 19:42:32 +08:00
|
|
|
regmap_read(chip->regmap, MAX17040_VCELL, &vcell);
|
2009-06-05 14:33:04 +08:00
|
|
|
|
2020-09-22 19:42:34 +08:00
|
|
|
return max17040_raw_vcell_to_uvolts(chip, vcell);
|
2009-06-05 14:33:04 +08:00
|
|
|
}
|
|
|
|
|
2020-09-22 19:42:32 +08:00
|
|
|
static int max17040_get_soc(struct max17040_chip *chip)
|
2009-06-05 14:33:04 +08:00
|
|
|
{
|
2020-09-22 19:42:32 +08:00
|
|
|
u32 soc;
|
2009-06-05 14:33:04 +08:00
|
|
|
|
2020-09-22 19:42:32 +08:00
|
|
|
regmap_read(chip->regmap, MAX17040_SOC, &soc);
|
2009-06-05 14:33:04 +08:00
|
|
|
|
2020-09-22 19:42:34 +08:00
|
|
|
return soc >> (chip->quirk_double_soc ? 9 : 8);
|
2009-06-05 14:33:04 +08:00
|
|
|
}
|
|
|
|
|
2020-09-22 19:42:32 +08:00
|
|
|
static int max17040_get_version(struct max17040_chip *chip)
|
2009-06-05 14:33:04 +08:00
|
|
|
{
|
2020-09-22 19:42:32 +08:00
|
|
|
int ret;
|
|
|
|
u32 version;
|
2009-06-05 14:33:04 +08:00
|
|
|
|
2020-09-22 19:42:32 +08:00
|
|
|
ret = regmap_read(chip->regmap, MAX17040_VER, &version);
|
2009-06-05 14:33:04 +08:00
|
|
|
|
2020-09-22 19:42:32 +08:00
|
|
|
return ret ? ret : version;
|
2009-06-05 14:33:04 +08:00
|
|
|
}
|
|
|
|
|
2020-09-22 19:42:32 +08:00
|
|
|
static int max17040_get_online(struct max17040_chip *chip)
|
2009-06-05 14:33:04 +08:00
|
|
|
{
|
2020-09-22 19:42:32 +08:00
|
|
|
return chip->pdata && chip->pdata->battery_online ?
|
|
|
|
chip->pdata->battery_online() : 1;
|
2009-06-05 14:33:04 +08:00
|
|
|
}
|
|
|
|
|
2020-09-22 19:42:32 +08:00
|
|
|
static int max17040_get_status(struct max17040_chip *chip)
|
2009-06-05 14:33:04 +08:00
|
|
|
{
|
2014-01-30 21:32:45 +08:00
|
|
|
if (!chip->pdata || !chip->pdata->charger_online
|
2020-09-22 19:42:32 +08:00
|
|
|
|| !chip->pdata->charger_enable)
|
|
|
|
return POWER_SUPPLY_STATUS_UNKNOWN;
|
|
|
|
|
|
|
|
if (max17040_get_soc(chip) > MAX17040_BATTERY_FULL)
|
|
|
|
return POWER_SUPPLY_STATUS_FULL;
|
2009-06-05 14:33:04 +08:00
|
|
|
|
2020-09-22 19:42:32 +08:00
|
|
|
if (chip->pdata->charger_online())
|
2009-06-05 14:33:04 +08:00
|
|
|
if (chip->pdata->charger_enable())
|
2020-09-22 19:42:32 +08:00
|
|
|
return POWER_SUPPLY_STATUS_CHARGING;
|
2009-06-05 14:33:04 +08:00
|
|
|
else
|
2020-09-22 19:42:32 +08:00
|
|
|
return POWER_SUPPLY_STATUS_NOT_CHARGING;
|
|
|
|
else
|
|
|
|
return POWER_SUPPLY_STATUS_DISCHARGING;
|
2009-06-05 14:33:04 +08:00
|
|
|
}
|
|
|
|
|
2019-12-05 23:44:09 +08:00
|
|
|
static int max17040_get_of_data(struct max17040_chip *chip)
|
|
|
|
{
|
|
|
|
struct device *dev = &chip->client->dev;
|
2020-09-22 19:42:36 +08:00
|
|
|
struct chip_data *data = &max17040_family[
|
|
|
|
(enum chip_id) of_device_get_match_data(dev)];
|
|
|
|
int rcomp_len;
|
|
|
|
u8 rcomp[2];
|
2019-12-05 23:44:09 +08:00
|
|
|
|
2020-09-22 19:42:34 +08:00
|
|
|
chip->quirk_double_soc = device_property_read_bool(dev,
|
|
|
|
"maxim,double-soc");
|
|
|
|
|
2019-12-05 23:44:09 +08:00
|
|
|
chip->low_soc_alert = MAX17040_ATHD_DEFAULT_POWER_UP;
|
|
|
|
device_property_read_u32(dev,
|
|
|
|
"maxim,alert-low-soc-level",
|
|
|
|
&chip->low_soc_alert);
|
|
|
|
|
2020-09-22 19:42:34 +08:00
|
|
|
if (chip->low_soc_alert <= 0 ||
|
|
|
|
chip->low_soc_alert > (chip->quirk_double_soc ? 16 : 32)) {
|
2020-09-22 19:42:32 +08:00
|
|
|
dev_err(dev, "maxim,alert-low-soc-level out of bounds\n");
|
2019-12-05 23:44:09 +08:00
|
|
|
return -EINVAL;
|
2020-09-22 19:42:32 +08:00
|
|
|
}
|
2019-12-05 23:44:09 +08:00
|
|
|
|
2020-09-22 19:42:36 +08:00
|
|
|
rcomp_len = device_property_count_u8(dev, "maxim,rcomp");
|
|
|
|
chip->rcomp = MAX17040_RCOMP_DEFAULT;
|
|
|
|
if (rcomp_len == data->rcomp_bytes) {
|
|
|
|
device_property_read_u8_array(dev, "maxim,rcomp",
|
|
|
|
rcomp, rcomp_len);
|
|
|
|
chip->rcomp = rcomp_len == 2 ?
|
|
|
|
rcomp[0] << 8 | rcomp[1] :
|
|
|
|
rcomp[0] << 8;
|
|
|
|
} else if (rcomp_len > 0) {
|
|
|
|
dev_err(dev, "maxim,rcomp has incorrect length\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2019-12-05 23:44:09 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-22 19:42:32 +08:00
|
|
|
static void max17040_check_changes(struct max17040_chip *chip)
|
2019-12-05 23:44:06 +08:00
|
|
|
{
|
2020-09-22 19:42:32 +08:00
|
|
|
chip->soc = max17040_get_soc(chip);
|
|
|
|
chip->status = max17040_get_status(chip);
|
2019-12-05 23:44:06 +08:00
|
|
|
}
|
|
|
|
|
2020-09-22 19:42:31 +08:00
|
|
|
static void max17040_queue_work(struct max17040_chip *chip)
|
|
|
|
{
|
|
|
|
queue_delayed_work(system_power_efficient_wq, &chip->work,
|
|
|
|
MAX17040_DELAY);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void max17040_stop_work(void *data)
|
|
|
|
{
|
|
|
|
struct max17040_chip *chip = data;
|
|
|
|
|
|
|
|
cancel_delayed_work_sync(&chip->work);
|
|
|
|
}
|
|
|
|
|
2009-06-05 14:33:04 +08:00
|
|
|
static void max17040_work(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct max17040_chip *chip;
|
2019-12-05 23:44:10 +08:00
|
|
|
int last_soc, last_status;
|
2009-06-05 14:33:04 +08:00
|
|
|
|
|
|
|
chip = container_of(work, struct max17040_chip, work.work);
|
2019-12-05 23:44:10 +08:00
|
|
|
|
|
|
|
/* store SOC and status to check changes */
|
|
|
|
last_soc = chip->soc;
|
|
|
|
last_status = chip->status;
|
2020-09-22 19:42:32 +08:00
|
|
|
max17040_check_changes(chip);
|
2009-06-05 14:33:04 +08:00
|
|
|
|
2019-12-05 23:44:10 +08:00
|
|
|
/* check changes and send uevent */
|
|
|
|
if (last_soc != chip->soc || last_status != chip->status)
|
|
|
|
power_supply_changed(chip->battery);
|
|
|
|
|
2020-09-22 19:42:31 +08:00
|
|
|
max17040_queue_work(chip);
|
2009-06-05 14:33:04 +08:00
|
|
|
}
|
|
|
|
|
2020-09-22 19:42:37 +08:00
|
|
|
/* Returns true if alert cause was SOC change, not low SOC */
|
|
|
|
static bool max17040_handle_soc_alert(struct max17040_chip *chip)
|
|
|
|
{
|
|
|
|
bool ret = true;
|
|
|
|
u32 data;
|
|
|
|
|
|
|
|
regmap_read(chip->regmap, MAX17040_STATUS, &data);
|
|
|
|
|
|
|
|
if (data & MAX17040_STATUS_HD_MASK) {
|
|
|
|
// this alert was caused by low soc
|
|
|
|
ret = false;
|
|
|
|
}
|
|
|
|
if (data & MAX17040_STATUS_SC_MASK) {
|
|
|
|
// soc change bit -- deassert to mark as handled
|
|
|
|
regmap_write(chip->regmap, MAX17040_STATUS,
|
|
|
|
data & ~MAX17040_STATUS_SC_MASK);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-12-05 23:44:06 +08:00
|
|
|
static irqreturn_t max17040_thread_handler(int id, void *dev)
|
|
|
|
{
|
|
|
|
struct max17040_chip *chip = dev;
|
|
|
|
|
2020-09-22 19:42:37 +08:00
|
|
|
if (!(chip->data.has_soc_alert && max17040_handle_soc_alert(chip)))
|
|
|
|
dev_warn(&chip->client->dev, "IRQ: Alert battery low level\n");
|
2020-09-22 19:42:32 +08:00
|
|
|
|
2019-12-05 23:44:06 +08:00
|
|
|
/* read registers */
|
2020-09-22 19:42:32 +08:00
|
|
|
max17040_check_changes(chip);
|
2019-12-05 23:44:06 +08:00
|
|
|
|
|
|
|
/* send uevent */
|
|
|
|
power_supply_changed(chip->battery);
|
|
|
|
|
2019-12-05 23:44:09 +08:00
|
|
|
/* reset alert bit */
|
2020-09-22 19:42:32 +08:00
|
|
|
max17040_set_low_soc_alert(chip, chip->low_soc_alert);
|
2019-12-05 23:44:09 +08:00
|
|
|
|
2019-12-05 23:44:06 +08:00
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int max17040_enable_alert_irq(struct max17040_chip *chip)
|
|
|
|
{
|
|
|
|
struct i2c_client *client = chip->client;
|
|
|
|
unsigned int flags;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
|
|
|
|
ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
|
|
|
|
max17040_thread_handler, flags,
|
|
|
|
chip->battery->desc->name, chip);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-07-06 08:10:03 +08:00
|
|
|
static int max17040_prop_writeable(struct power_supply *psy,
|
|
|
|
enum power_supply_property psp)
|
|
|
|
{
|
|
|
|
switch (psp) {
|
|
|
|
case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int max17040_set_property(struct power_supply *psy,
|
|
|
|
enum power_supply_property psp,
|
|
|
|
const union power_supply_propval *val)
|
|
|
|
{
|
|
|
|
struct max17040_chip *chip = power_supply_get_drvdata(psy);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
switch (psp) {
|
|
|
|
case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
|
2020-09-22 19:42:34 +08:00
|
|
|
/* alert threshold can be programmed from 1% up to 16/32% */
|
|
|
|
if ((val->intval < 1) ||
|
|
|
|
(val->intval > (chip->quirk_double_soc ? 16 : 32))) {
|
2020-07-06 08:10:03 +08:00
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2020-09-22 19:42:32 +08:00
|
|
|
ret = max17040_set_low_soc_alert(chip, val->intval);
|
2020-07-06 08:10:03 +08:00
|
|
|
chip->low_soc_alert = val->intval;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-09-22 19:42:32 +08:00
|
|
|
static int max17040_get_property(struct power_supply *psy,
|
|
|
|
enum power_supply_property psp,
|
|
|
|
union power_supply_propval *val)
|
|
|
|
{
|
|
|
|
struct max17040_chip *chip = power_supply_get_drvdata(psy);
|
|
|
|
|
|
|
|
switch (psp) {
|
|
|
|
case POWER_SUPPLY_PROP_STATUS:
|
|
|
|
val->intval = max17040_get_status(chip);
|
|
|
|
break;
|
|
|
|
case POWER_SUPPLY_PROP_ONLINE:
|
|
|
|
val->intval = max17040_get_online(chip);
|
|
|
|
break;
|
|
|
|
case POWER_SUPPLY_PROP_VOLTAGE_NOW:
|
|
|
|
val->intval = max17040_get_vcell(chip);
|
|
|
|
break;
|
|
|
|
case POWER_SUPPLY_PROP_CAPACITY:
|
|
|
|
val->intval = max17040_get_soc(chip);
|
|
|
|
break;
|
|
|
|
case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
|
|
|
|
val->intval = chip->low_soc_alert;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct regmap_config max17040_regmap = {
|
|
|
|
.reg_bits = 8,
|
|
|
|
.reg_stride = 2,
|
|
|
|
.val_bits = 16,
|
|
|
|
.val_format_endian = REGMAP_ENDIAN_BIG,
|
|
|
|
};
|
|
|
|
|
2009-06-05 14:33:04 +08:00
|
|
|
static enum power_supply_property max17040_battery_props[] = {
|
|
|
|
POWER_SUPPLY_PROP_STATUS,
|
|
|
|
POWER_SUPPLY_PROP_ONLINE,
|
|
|
|
POWER_SUPPLY_PROP_VOLTAGE_NOW,
|
|
|
|
POWER_SUPPLY_PROP_CAPACITY,
|
2020-07-06 08:10:03 +08:00
|
|
|
POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN,
|
2009-06-05 14:33:04 +08:00
|
|
|
};
|
|
|
|
|
2015-03-12 15:44:11 +08:00
|
|
|
static const struct power_supply_desc max17040_battery_desc = {
|
2020-07-06 08:10:03 +08:00
|
|
|
.name = "battery",
|
|
|
|
.type = POWER_SUPPLY_TYPE_BATTERY,
|
|
|
|
.get_property = max17040_get_property,
|
|
|
|
.set_property = max17040_set_property,
|
|
|
|
.property_is_writeable = max17040_prop_writeable,
|
|
|
|
.properties = max17040_battery_props,
|
|
|
|
.num_properties = ARRAY_SIZE(max17040_battery_props),
|
2015-03-12 15:44:11 +08:00
|
|
|
};
|
|
|
|
|
2012-11-20 02:22:23 +08:00
|
|
|
static int max17040_probe(struct i2c_client *client,
|
2009-06-05 14:33:04 +08:00
|
|
|
const struct i2c_device_id *id)
|
|
|
|
{
|
2019-06-08 18:55:59 +08:00
|
|
|
struct i2c_adapter *adapter = client->adapter;
|
2015-03-12 15:44:11 +08:00
|
|
|
struct power_supply_config psy_cfg = {};
|
2009-06-05 14:33:04 +08:00
|
|
|
struct max17040_chip *chip;
|
2020-09-22 19:42:34 +08:00
|
|
|
enum chip_id chip_id;
|
2020-09-22 19:42:37 +08:00
|
|
|
bool enable_irq = false;
|
2019-12-05 23:44:09 +08:00
|
|
|
int ret;
|
2009-06-05 14:33:04 +08:00
|
|
|
|
|
|
|
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
|
|
|
|
return -EIO;
|
|
|
|
|
2013-01-06 12:53:25 +08:00
|
|
|
chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
|
2009-06-05 14:33:04 +08:00
|
|
|
if (!chip)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
chip->client = client;
|
2020-09-22 19:42:32 +08:00
|
|
|
chip->regmap = devm_regmap_init_i2c(client, &max17040_regmap);
|
2009-06-05 14:33:04 +08:00
|
|
|
chip->pdata = client->dev.platform_data;
|
2020-09-22 19:42:34 +08:00
|
|
|
chip_id = (enum chip_id) id->driver_data;
|
|
|
|
if (client->dev.of_node) {
|
|
|
|
ret = max17040_get_of_data(chip);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
chip_id = (enum chip_id) (uintptr_t)
|
|
|
|
of_device_get_match_data(&client->dev);
|
|
|
|
}
|
|
|
|
chip->data = max17040_family[chip_id];
|
2009-06-05 14:33:04 +08:00
|
|
|
|
|
|
|
i2c_set_clientdata(client, chip);
|
2015-03-12 15:44:11 +08:00
|
|
|
psy_cfg.drv_data = chip;
|
2009-06-05 14:33:04 +08:00
|
|
|
|
2020-09-22 19:42:31 +08:00
|
|
|
chip->battery = devm_power_supply_register(&client->dev,
|
2015-03-12 15:44:11 +08:00
|
|
|
&max17040_battery_desc, &psy_cfg);
|
|
|
|
if (IS_ERR(chip->battery)) {
|
2009-06-05 14:33:04 +08:00
|
|
|
dev_err(&client->dev, "failed: power supply register\n");
|
2015-03-12 15:44:11 +08:00
|
|
|
return PTR_ERR(chip->battery);
|
2009-06-05 14:33:04 +08:00
|
|
|
}
|
|
|
|
|
2020-09-22 19:42:32 +08:00
|
|
|
ret = max17040_get_version(chip);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
dev_dbg(&chip->client->dev, "MAX17040 Fuel-Gauge Ver 0x%x\n", ret);
|
|
|
|
|
2020-09-22 19:42:34 +08:00
|
|
|
if (chip_id == ID_MAX17040 || chip_id == ID_MAX17041)
|
|
|
|
max17040_reset(chip);
|
2009-06-05 14:33:04 +08:00
|
|
|
|
2020-09-22 19:42:36 +08:00
|
|
|
max17040_set_rcomp(chip, chip->rcomp);
|
|
|
|
|
2019-12-05 23:44:06 +08:00
|
|
|
/* check interrupt */
|
2020-09-22 19:42:34 +08:00
|
|
|
if (client->irq && chip->data.has_low_soc_alert) {
|
2020-09-22 19:42:32 +08:00
|
|
|
ret = max17040_set_low_soc_alert(chip, chip->low_soc_alert);
|
2019-12-05 23:44:09 +08:00
|
|
|
if (ret) {
|
|
|
|
dev_err(&client->dev,
|
|
|
|
"Failed to set low SOC alert: err %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2019-12-05 23:44:06 +08:00
|
|
|
|
2020-09-22 19:42:37 +08:00
|
|
|
enable_irq = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client->irq && chip->data.has_soc_alert) {
|
|
|
|
ret = max17040_set_soc_alert(chip, 1);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(&client->dev,
|
|
|
|
"Failed to set SOC alert: err %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
enable_irq = true;
|
|
|
|
} else {
|
|
|
|
/* soc alerts negate the need for polling */
|
|
|
|
INIT_DEFERRABLE_WORK(&chip->work, max17040_work);
|
|
|
|
ret = devm_add_action(&client->dev, max17040_stop_work, chip);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
max17040_queue_work(chip);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enable_irq) {
|
2019-12-05 23:44:06 +08:00
|
|
|
ret = max17040_enable_alert_irq(chip);
|
|
|
|
if (ret) {
|
|
|
|
client->irq = 0;
|
|
|
|
dev_warn(&client->dev,
|
|
|
|
"Failed to get IRQ err %d\n", ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-05 14:33:04 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-10 21:34:06 +08:00
|
|
|
#ifdef CONFIG_PM_SLEEP
|
2009-06-05 14:33:04 +08:00
|
|
|
|
2013-03-10 21:34:06 +08:00
|
|
|
static int max17040_suspend(struct device *dev)
|
2009-06-05 14:33:04 +08:00
|
|
|
{
|
2013-03-10 21:34:06 +08:00
|
|
|
struct i2c_client *client = to_i2c_client(dev);
|
2009-06-05 14:33:04 +08:00
|
|
|
struct max17040_chip *chip = i2c_get_clientdata(client);
|
|
|
|
|
2020-09-22 19:42:37 +08:00
|
|
|
if (client->irq && chip->data.has_soc_alert)
|
|
|
|
// disable soc alert to prevent wakeup
|
|
|
|
max17040_set_soc_alert(chip, 0);
|
|
|
|
else
|
|
|
|
cancel_delayed_work(&chip->work);
|
2019-12-05 23:44:06 +08:00
|
|
|
|
2020-01-10 18:05:40 +08:00
|
|
|
if (client->irq && device_may_wakeup(dev))
|
|
|
|
enable_irq_wake(client->irq);
|
2019-12-05 23:44:06 +08:00
|
|
|
|
2009-06-05 14:33:04 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-10 21:34:06 +08:00
|
|
|
static int max17040_resume(struct device *dev)
|
2009-06-05 14:33:04 +08:00
|
|
|
{
|
2013-03-10 21:34:06 +08:00
|
|
|
struct i2c_client *client = to_i2c_client(dev);
|
2009-06-05 14:33:04 +08:00
|
|
|
struct max17040_chip *chip = i2c_get_clientdata(client);
|
|
|
|
|
2020-01-10 18:05:40 +08:00
|
|
|
if (client->irq && device_may_wakeup(dev))
|
|
|
|
disable_irq_wake(client->irq);
|
2019-12-05 23:44:06 +08:00
|
|
|
|
2020-09-22 19:42:37 +08:00
|
|
|
if (client->irq && chip->data.has_soc_alert)
|
|
|
|
max17040_set_soc_alert(chip, 1);
|
|
|
|
else
|
|
|
|
max17040_queue_work(chip);
|
2020-09-22 19:42:31 +08:00
|
|
|
|
2009-06-05 14:33:04 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-10 21:34:06 +08:00
|
|
|
static SIMPLE_DEV_PM_OPS(max17040_pm_ops, max17040_suspend, max17040_resume);
|
|
|
|
#define MAX17040_PM_OPS (&max17040_pm_ops)
|
|
|
|
|
2009-06-05 14:33:04 +08:00
|
|
|
#else
|
|
|
|
|
2013-03-10 21:34:06 +08:00
|
|
|
#define MAX17040_PM_OPS NULL
|
2009-06-05 14:33:04 +08:00
|
|
|
|
2013-03-10 21:34:06 +08:00
|
|
|
#endif /* CONFIG_PM_SLEEP */
|
2009-06-05 14:33:04 +08:00
|
|
|
|
|
|
|
static const struct i2c_device_id max17040_id[] = {
|
2020-09-22 19:42:34 +08:00
|
|
|
{ "max17040", ID_MAX17040 },
|
|
|
|
{ "max17041", ID_MAX17041 },
|
|
|
|
{ "max17043", ID_MAX17043 },
|
|
|
|
{ "max77836-battery", ID_MAX17043 },
|
|
|
|
{ "max17044", ID_MAX17044 },
|
|
|
|
{ "max17048", ID_MAX17048 },
|
|
|
|
{ "max17049", ID_MAX17049 },
|
|
|
|
{ "max17058", ID_MAX17058 },
|
|
|
|
{ "max17059", ID_MAX17059 },
|
|
|
|
{ /* sentinel */ }
|
2009-06-05 14:33:04 +08:00
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(i2c, max17040_id);
|
|
|
|
|
2017-03-15 11:43:49 +08:00
|
|
|
static const struct of_device_id max17040_of_match[] = {
|
2020-09-22 19:42:34 +08:00
|
|
|
{ .compatible = "maxim,max17040", .data = (void *) ID_MAX17040 },
|
|
|
|
{ .compatible = "maxim,max17041", .data = (void *) ID_MAX17041 },
|
|
|
|
{ .compatible = "maxim,max17043", .data = (void *) ID_MAX17043 },
|
|
|
|
{ .compatible = "maxim,max77836-battery", .data = (void *) ID_MAX17043 },
|
|
|
|
{ .compatible = "maxim,max17044", .data = (void *) ID_MAX17044 },
|
|
|
|
{ .compatible = "maxim,max17048", .data = (void *) ID_MAX17048 },
|
|
|
|
{ .compatible = "maxim,max17049", .data = (void *) ID_MAX17049 },
|
|
|
|
{ .compatible = "maxim,max17058", .data = (void *) ID_MAX17058 },
|
|
|
|
{ .compatible = "maxim,max17059", .data = (void *) ID_MAX17059 },
|
|
|
|
{ /* sentinel */ },
|
2017-03-15 11:43:49 +08:00
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, max17040_of_match);
|
|
|
|
|
2009-06-05 14:33:04 +08:00
|
|
|
static struct i2c_driver max17040_i2c_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "max17040",
|
2017-03-15 11:43:49 +08:00
|
|
|
.of_match_table = max17040_of_match,
|
2013-03-10 21:34:06 +08:00
|
|
|
.pm = MAX17040_PM_OPS,
|
2009-06-05 14:33:04 +08:00
|
|
|
},
|
|
|
|
.probe = max17040_probe,
|
|
|
|
.id_table = max17040_id,
|
|
|
|
};
|
2012-01-21 14:42:54 +08:00
|
|
|
module_i2c_driver(max17040_i2c_driver);
|
2009-06-05 14:33:04 +08:00
|
|
|
|
|
|
|
MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>");
|
|
|
|
MODULE_DESCRIPTION("MAX17040 Fuel Gauge");
|
|
|
|
MODULE_LICENSE("GPL");
|