2009-06-18 07:26:26 +08:00
|
|
|
/*
|
|
|
|
* I2C client/driver for the Maxim/Dallas DS2782 Stand-Alone Fuel Gauge IC
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 Bluewater Systems Ltd
|
|
|
|
*
|
2011-06-15 12:45:36 +08:00
|
|
|
* Author: Ryan Mallon
|
2009-06-18 07:26:26 +08:00
|
|
|
*
|
2010-04-26 19:05:25 +08:00
|
|
|
* DS2786 added by Yulia Vilensky <vilensky@compulab.co.il>
|
|
|
|
*
|
2013-01-10 17:24:48 +08:00
|
|
|
* UEvent sending added by Evgeny Romanov <romanov@neurosoft.ru>
|
|
|
|
*
|
2009-06-18 07:26:26 +08:00
|
|
|
* 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/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/swab.h>
|
|
|
|
#include <linux/i2c.h>
|
2013-01-10 17:24:48 +08:00
|
|
|
#include <linux/delay.h>
|
2009-06-18 07:26:26 +08:00
|
|
|
#include <linux/idr.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>
|
2010-04-26 19:05:25 +08:00
|
|
|
#include <linux/ds2782_battery.h>
|
2009-06-18 07:26:26 +08:00
|
|
|
|
|
|
|
#define DS2782_REG_RARC 0x06 /* Remaining active relative capacity */
|
|
|
|
|
2010-04-26 19:05:25 +08:00
|
|
|
#define DS278x_REG_VOLT_MSB 0x0c
|
|
|
|
#define DS278x_REG_TEMP_MSB 0x0a
|
|
|
|
#define DS278x_REG_CURRENT_MSB 0x0e
|
2009-06-18 07:26:26 +08:00
|
|
|
|
|
|
|
/* EEPROM Block */
|
|
|
|
#define DS2782_REG_RSNSP 0x69 /* Sense resistor value */
|
|
|
|
|
|
|
|
/* Current unit measurement in uA for a 1 milli-ohm sense resistor */
|
|
|
|
#define DS2782_CURRENT_UNITS 1563
|
|
|
|
|
2010-04-26 19:05:25 +08:00
|
|
|
#define DS2786_REG_RARC 0x02 /* Remaining active relative capacity */
|
|
|
|
|
|
|
|
#define DS2786_CURRENT_UNITS 25
|
|
|
|
|
2013-01-10 17:24:48 +08:00
|
|
|
#define DS278x_DELAY 1000
|
|
|
|
|
2010-04-26 19:05:25 +08:00
|
|
|
struct ds278x_info;
|
|
|
|
|
|
|
|
struct ds278x_battery_ops {
|
2010-05-13 07:54:57 +08:00
|
|
|
int (*get_battery_current)(struct ds278x_info *info, int *current_uA);
|
2010-10-02 05:17:42 +08:00
|
|
|
int (*get_battery_voltage)(struct ds278x_info *info, int *voltage_uV);
|
|
|
|
int (*get_battery_capacity)(struct ds278x_info *info, int *capacity);
|
2010-04-26 19:05:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#define to_ds278x_info(x) container_of(x, struct ds278x_info, battery)
|
2009-06-18 07:26:26 +08:00
|
|
|
|
2010-04-26 19:05:25 +08:00
|
|
|
struct ds278x_info {
|
2009-06-18 07:26:26 +08:00
|
|
|
struct i2c_client *client;
|
|
|
|
struct power_supply battery;
|
2010-04-26 19:05:25 +08:00
|
|
|
struct ds278x_battery_ops *ops;
|
2013-01-10 17:24:48 +08:00
|
|
|
struct delayed_work bat_work;
|
2009-06-18 07:26:26 +08:00
|
|
|
int id;
|
2010-04-26 19:05:25 +08:00
|
|
|
int rsns;
|
2013-01-10 17:24:48 +08:00
|
|
|
int capacity;
|
|
|
|
int status; /* State Of Charge */
|
2009-06-18 07:26:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static DEFINE_IDR(battery_id);
|
|
|
|
static DEFINE_MUTEX(battery_lock);
|
|
|
|
|
2010-04-26 19:05:25 +08:00
|
|
|
static inline int ds278x_read_reg(struct ds278x_info *info, int reg, u8 *val)
|
2009-06-18 07:26:26 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = i2c_smbus_read_byte_data(info->client, reg);
|
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&info->client->dev, "register read failed\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
*val = ret;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-04-26 19:05:25 +08:00
|
|
|
static inline int ds278x_read_reg16(struct ds278x_info *info, int reg_msb,
|
2009-06-18 07:26:26 +08:00
|
|
|
s16 *val)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2012-10-24 15:12:29 +08:00
|
|
|
ret = i2c_smbus_read_word_data(info->client, reg_msb);
|
2009-06-18 07:26:26 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&info->client->dev, "register read failed\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-10-24 15:12:29 +08:00
|
|
|
*val = swab16(ret);
|
2009-06-18 07:26:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-04-26 19:05:25 +08:00
|
|
|
static int ds278x_get_temp(struct ds278x_info *info, int *temp)
|
2009-06-18 07:26:26 +08:00
|
|
|
{
|
|
|
|
s16 raw;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Temperature is measured in units of 0.125 degrees celcius, the
|
|
|
|
* power_supply class measures temperature in tenths of degrees
|
|
|
|
* celsius. The temperature value is stored as a 10 bit number, plus
|
|
|
|
* sign in the upper bits of a 16 bit register.
|
|
|
|
*/
|
2010-04-26 19:05:25 +08:00
|
|
|
err = ds278x_read_reg16(info, DS278x_REG_TEMP_MSB, &raw);
|
2009-06-18 07:26:26 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
*temp = ((raw / 32) * 125) / 100;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-04-26 19:05:25 +08:00
|
|
|
static int ds2782_get_current(struct ds278x_info *info, int *current_uA)
|
2009-06-18 07:26:26 +08:00
|
|
|
{
|
|
|
|
int sense_res;
|
|
|
|
int err;
|
|
|
|
u8 sense_res_raw;
|
|
|
|
s16 raw;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The units of measurement for current are dependent on the value of
|
|
|
|
* the sense resistor.
|
|
|
|
*/
|
2010-04-26 19:05:25 +08:00
|
|
|
err = ds278x_read_reg(info, DS2782_REG_RSNSP, &sense_res_raw);
|
2009-06-18 07:26:26 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
if (sense_res_raw == 0) {
|
|
|
|
dev_err(&info->client->dev, "sense resistor value is 0\n");
|
|
|
|
return -ENXIO;
|
|
|
|
}
|
|
|
|
sense_res = 1000 / sense_res_raw;
|
|
|
|
|
|
|
|
dev_dbg(&info->client->dev, "sense resistor = %d milli-ohms\n",
|
|
|
|
sense_res);
|
2010-04-26 19:05:25 +08:00
|
|
|
err = ds278x_read_reg16(info, DS278x_REG_CURRENT_MSB, &raw);
|
2009-06-18 07:26:26 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
*current_uA = raw * (DS2782_CURRENT_UNITS / sense_res);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-10-02 05:17:42 +08:00
|
|
|
static int ds2782_get_voltage(struct ds278x_info *info, int *voltage_uV)
|
2009-06-18 07:26:26 +08:00
|
|
|
{
|
|
|
|
s16 raw;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Voltage is measured in units of 4.88mV. The voltage is stored as
|
|
|
|
* a 10-bit number plus sign, in the upper bits of a 16-bit register
|
|
|
|
*/
|
2010-04-26 19:05:25 +08:00
|
|
|
err = ds278x_read_reg16(info, DS278x_REG_VOLT_MSB, &raw);
|
2009-06-18 07:26:26 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
2010-10-02 05:17:42 +08:00
|
|
|
*voltage_uV = (raw / 32) * 4800;
|
2009-06-18 07:26:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-04-26 19:05:25 +08:00
|
|
|
static int ds2782_get_capacity(struct ds278x_info *info, int *capacity)
|
2009-06-18 07:26:26 +08:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
u8 raw;
|
|
|
|
|
2010-04-26 19:05:25 +08:00
|
|
|
err = ds278x_read_reg(info, DS2782_REG_RARC, &raw);
|
2009-06-18 07:26:26 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
*capacity = raw;
|
2010-06-15 08:44:59 +08:00
|
|
|
return 0;
|
2009-06-18 07:26:26 +08:00
|
|
|
}
|
|
|
|
|
2010-04-26 19:05:25 +08:00
|
|
|
static int ds2786_get_current(struct ds278x_info *info, int *current_uA)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
s16 raw;
|
|
|
|
|
|
|
|
err = ds278x_read_reg16(info, DS278x_REG_CURRENT_MSB, &raw);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
*current_uA = (raw / 16) * (DS2786_CURRENT_UNITS / info->rsns);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-10-02 05:17:42 +08:00
|
|
|
static int ds2786_get_voltage(struct ds278x_info *info, int *voltage_uV)
|
2010-04-26 19:05:25 +08:00
|
|
|
{
|
|
|
|
s16 raw;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Voltage is measured in units of 1.22mV. The voltage is stored as
|
2014-01-18 01:02:38 +08:00
|
|
|
* a 12-bit number plus sign, in the upper bits of a 16-bit register
|
2010-04-26 19:05:25 +08:00
|
|
|
*/
|
|
|
|
err = ds278x_read_reg16(info, DS278x_REG_VOLT_MSB, &raw);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2010-10-02 05:17:42 +08:00
|
|
|
*voltage_uV = (raw / 8) * 1220;
|
2010-04-26 19:05:25 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ds2786_get_capacity(struct ds278x_info *info, int *capacity)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
u8 raw;
|
|
|
|
|
|
|
|
err = ds278x_read_reg(info, DS2786_REG_RARC, &raw);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
/* Relative capacity is displayed with resolution 0.5 % */
|
|
|
|
*capacity = raw/2 ;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ds278x_get_status(struct ds278x_info *info, int *status)
|
2009-06-18 07:26:26 +08:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
int current_uA;
|
|
|
|
int capacity;
|
|
|
|
|
2010-05-13 07:54:57 +08:00
|
|
|
err = info->ops->get_battery_current(info, ¤t_uA);
|
2009-06-18 07:26:26 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2010-05-13 07:54:57 +08:00
|
|
|
err = info->ops->get_battery_capacity(info, &capacity);
|
2009-06-18 07:26:26 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2013-01-10 17:24:48 +08:00
|
|
|
info->capacity = capacity;
|
|
|
|
|
2009-06-18 07:26:26 +08:00
|
|
|
if (capacity == 100)
|
|
|
|
*status = POWER_SUPPLY_STATUS_FULL;
|
|
|
|
else if (current_uA == 0)
|
|
|
|
*status = POWER_SUPPLY_STATUS_NOT_CHARGING;
|
|
|
|
else if (current_uA < 0)
|
|
|
|
*status = POWER_SUPPLY_STATUS_DISCHARGING;
|
|
|
|
else
|
|
|
|
*status = POWER_SUPPLY_STATUS_CHARGING;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-04-26 19:05:25 +08:00
|
|
|
static int ds278x_battery_get_property(struct power_supply *psy,
|
2009-06-18 07:26:26 +08:00
|
|
|
enum power_supply_property prop,
|
|
|
|
union power_supply_propval *val)
|
|
|
|
{
|
2010-04-26 19:05:25 +08:00
|
|
|
struct ds278x_info *info = to_ds278x_info(psy);
|
2009-06-18 07:26:26 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
switch (prop) {
|
|
|
|
case POWER_SUPPLY_PROP_STATUS:
|
2010-04-26 19:05:25 +08:00
|
|
|
ret = ds278x_get_status(info, &val->intval);
|
2009-06-18 07:26:26 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case POWER_SUPPLY_PROP_CAPACITY:
|
2010-05-13 07:54:57 +08:00
|
|
|
ret = info->ops->get_battery_capacity(info, &val->intval);
|
2009-06-18 07:26:26 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case POWER_SUPPLY_PROP_VOLTAGE_NOW:
|
2010-05-13 07:54:57 +08:00
|
|
|
ret = info->ops->get_battery_voltage(info, &val->intval);
|
2009-06-18 07:26:26 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case POWER_SUPPLY_PROP_CURRENT_NOW:
|
2010-05-13 07:54:57 +08:00
|
|
|
ret = info->ops->get_battery_current(info, &val->intval);
|
2009-06-18 07:26:26 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case POWER_SUPPLY_PROP_TEMP:
|
2010-04-26 19:05:25 +08:00
|
|
|
ret = ds278x_get_temp(info, &val->intval);
|
2009-06-18 07:26:26 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-01-10 17:24:48 +08:00
|
|
|
static void ds278x_bat_update(struct ds278x_info *info)
|
|
|
|
{
|
|
|
|
int old_status = info->status;
|
|
|
|
int old_capacity = info->capacity;
|
|
|
|
|
|
|
|
ds278x_get_status(info, &info->status);
|
|
|
|
|
|
|
|
if ((old_status != info->status) || (old_capacity != info->capacity))
|
|
|
|
power_supply_changed(&info->battery);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ds278x_bat_work(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct ds278x_info *info;
|
|
|
|
|
|
|
|
info = container_of(work, struct ds278x_info, bat_work.work);
|
|
|
|
ds278x_bat_update(info);
|
|
|
|
|
|
|
|
schedule_delayed_work(&info->bat_work, DS278x_DELAY);
|
|
|
|
}
|
|
|
|
|
2010-04-26 19:05:25 +08:00
|
|
|
static enum power_supply_property ds278x_battery_props[] = {
|
2009-06-18 07:26:26 +08:00
|
|
|
POWER_SUPPLY_PROP_STATUS,
|
|
|
|
POWER_SUPPLY_PROP_CAPACITY,
|
|
|
|
POWER_SUPPLY_PROP_VOLTAGE_NOW,
|
|
|
|
POWER_SUPPLY_PROP_CURRENT_NOW,
|
|
|
|
POWER_SUPPLY_PROP_TEMP,
|
|
|
|
};
|
|
|
|
|
2010-04-26 19:05:25 +08:00
|
|
|
static void ds278x_power_supply_init(struct power_supply *battery)
|
2009-06-18 07:26:26 +08:00
|
|
|
{
|
|
|
|
battery->type = POWER_SUPPLY_TYPE_BATTERY;
|
2010-04-26 19:05:25 +08:00
|
|
|
battery->properties = ds278x_battery_props;
|
|
|
|
battery->num_properties = ARRAY_SIZE(ds278x_battery_props);
|
|
|
|
battery->get_property = ds278x_battery_get_property;
|
2009-06-18 07:26:26 +08:00
|
|
|
battery->external_power_changed = NULL;
|
|
|
|
}
|
|
|
|
|
2010-04-26 19:05:25 +08:00
|
|
|
static int ds278x_battery_remove(struct i2c_client *client)
|
2009-06-18 07:26:26 +08:00
|
|
|
{
|
2010-04-26 19:05:25 +08:00
|
|
|
struct ds278x_info *info = i2c_get_clientdata(client);
|
2009-06-18 07:26:26 +08:00
|
|
|
|
|
|
|
power_supply_unregister(&info->battery);
|
|
|
|
kfree(info->battery.name);
|
|
|
|
|
|
|
|
mutex_lock(&battery_lock);
|
|
|
|
idr_remove(&battery_id, info->id);
|
|
|
|
mutex_unlock(&battery_lock);
|
|
|
|
|
2013-01-10 17:24:48 +08:00
|
|
|
cancel_delayed_work(&info->bat_work);
|
|
|
|
|
2009-06-18 07:26:26 +08:00
|
|
|
kfree(info);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-10 21:34:05 +08:00
|
|
|
#ifdef CONFIG_PM_SLEEP
|
2013-01-10 17:24:48 +08:00
|
|
|
|
2013-03-10 21:34:05 +08:00
|
|
|
static int ds278x_suspend(struct device *dev)
|
2013-01-10 17:24:48 +08:00
|
|
|
{
|
2013-03-10 21:34:05 +08:00
|
|
|
struct i2c_client *client = to_i2c_client(dev);
|
2013-01-10 17:24:48 +08:00
|
|
|
struct ds278x_info *info = i2c_get_clientdata(client);
|
|
|
|
|
|
|
|
cancel_delayed_work(&info->bat_work);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-10 21:34:05 +08:00
|
|
|
static int ds278x_resume(struct device *dev)
|
2013-01-10 17:24:48 +08:00
|
|
|
{
|
2013-03-10 21:34:05 +08:00
|
|
|
struct i2c_client *client = to_i2c_client(dev);
|
2013-01-10 17:24:48 +08:00
|
|
|
struct ds278x_info *info = i2c_get_clientdata(client);
|
|
|
|
|
|
|
|
schedule_delayed_work(&info->bat_work, DS278x_DELAY);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-10 21:34:05 +08:00
|
|
|
static SIMPLE_DEV_PM_OPS(ds278x_battery_pm_ops, ds278x_suspend, ds278x_resume);
|
|
|
|
#define DS278X_BATTERY_PM_OPS (&ds278x_battery_pm_ops)
|
2013-01-10 17:24:48 +08:00
|
|
|
|
2013-03-10 21:34:05 +08:00
|
|
|
#else
|
|
|
|
#define DS278X_BATTERY_PM_OPS NULL
|
|
|
|
#endif /* CONFIG_PM_SLEEP */
|
2013-01-10 17:24:48 +08:00
|
|
|
|
2010-04-27 02:10:52 +08:00
|
|
|
enum ds278x_num_id {
|
|
|
|
DS2782 = 0,
|
|
|
|
DS2786,
|
|
|
|
};
|
|
|
|
|
2010-04-26 19:05:25 +08:00
|
|
|
static struct ds278x_battery_ops ds278x_ops[] = {
|
2010-04-27 02:10:52 +08:00
|
|
|
[DS2782] = {
|
2010-05-13 07:54:57 +08:00
|
|
|
.get_battery_current = ds2782_get_current,
|
|
|
|
.get_battery_voltage = ds2782_get_voltage,
|
|
|
|
.get_battery_capacity = ds2782_get_capacity,
|
2010-04-26 19:05:25 +08:00
|
|
|
},
|
2010-04-27 02:10:52 +08:00
|
|
|
[DS2786] = {
|
2010-05-13 07:54:57 +08:00
|
|
|
.get_battery_current = ds2786_get_current,
|
|
|
|
.get_battery_voltage = ds2786_get_voltage,
|
|
|
|
.get_battery_capacity = ds2786_get_capacity,
|
2010-04-26 19:05:25 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static int ds278x_battery_probe(struct i2c_client *client,
|
2009-06-18 07:26:26 +08:00
|
|
|
const struct i2c_device_id *id)
|
|
|
|
{
|
2010-04-26 19:05:25 +08:00
|
|
|
struct ds278x_platform_data *pdata = client->dev.platform_data;
|
|
|
|
struct ds278x_info *info;
|
2009-06-18 07:26:26 +08:00
|
|
|
int ret;
|
|
|
|
int num;
|
|
|
|
|
2010-04-26 19:05:25 +08:00
|
|
|
/*
|
|
|
|
* ds2786 should have the sense resistor value set
|
|
|
|
* in the platform data
|
|
|
|
*/
|
2010-04-27 02:10:52 +08:00
|
|
|
if (id->driver_data == DS2786 && !pdata) {
|
2010-04-26 19:05:25 +08:00
|
|
|
dev_err(&client->dev, "missing platform data for ds2786\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2009-06-18 07:26:26 +08:00
|
|
|
/* Get an ID for this battery */
|
|
|
|
mutex_lock(&battery_lock);
|
2013-02-28 09:04:37 +08:00
|
|
|
ret = idr_alloc(&battery_id, client, 0, 0, GFP_KERNEL);
|
2009-06-18 07:26:26 +08:00
|
|
|
mutex_unlock(&battery_lock);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail_id;
|
2013-02-28 09:04:37 +08:00
|
|
|
num = ret;
|
2009-06-18 07:26:26 +08:00
|
|
|
|
|
|
|
info = kzalloc(sizeof(*info), GFP_KERNEL);
|
|
|
|
if (!info) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto fail_info;
|
|
|
|
}
|
|
|
|
|
2010-04-26 19:05:25 +08:00
|
|
|
info->battery.name = kasprintf(GFP_KERNEL, "%s-%d", client->name, num);
|
2009-06-18 07:26:26 +08:00
|
|
|
if (!info->battery.name) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto fail_name;
|
|
|
|
}
|
|
|
|
|
2010-04-27 02:10:52 +08:00
|
|
|
if (id->driver_data == DS2786)
|
2010-04-26 19:05:25 +08:00
|
|
|
info->rsns = pdata->rsns;
|
|
|
|
|
2009-06-18 07:26:26 +08:00
|
|
|
i2c_set_clientdata(client, info);
|
|
|
|
info->client = client;
|
2010-04-26 19:05:25 +08:00
|
|
|
info->id = num;
|
|
|
|
info->ops = &ds278x_ops[id->driver_data];
|
|
|
|
ds278x_power_supply_init(&info->battery);
|
2009-06-18 07:26:26 +08:00
|
|
|
|
2013-01-10 17:24:48 +08:00
|
|
|
info->capacity = 100;
|
|
|
|
info->status = POWER_SUPPLY_STATUS_FULL;
|
|
|
|
|
|
|
|
INIT_DELAYED_WORK(&info->bat_work, ds278x_bat_work);
|
|
|
|
|
2009-06-18 07:26:26 +08:00
|
|
|
ret = power_supply_register(&client->dev, &info->battery);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(&client->dev, "failed to register battery\n");
|
|
|
|
goto fail_register;
|
2013-01-10 17:24:48 +08:00
|
|
|
} else {
|
|
|
|
schedule_delayed_work(&info->bat_work, DS278x_DELAY);
|
2009-06-18 07:26:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail_register:
|
|
|
|
kfree(info->battery.name);
|
|
|
|
fail_name:
|
|
|
|
kfree(info);
|
|
|
|
fail_info:
|
|
|
|
mutex_lock(&battery_lock);
|
|
|
|
idr_remove(&battery_id, num);
|
|
|
|
mutex_unlock(&battery_lock);
|
|
|
|
fail_id:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-04-26 19:05:25 +08:00
|
|
|
static const struct i2c_device_id ds278x_id[] = {
|
2010-04-27 02:10:52 +08:00
|
|
|
{"ds2782", DS2782},
|
|
|
|
{"ds2786", DS2786},
|
2009-06-18 07:26:26 +08:00
|
|
|
{},
|
|
|
|
};
|
2011-03-01 17:23:34 +08:00
|
|
|
MODULE_DEVICE_TABLE(i2c, ds278x_id);
|
2009-06-18 07:26:26 +08:00
|
|
|
|
2010-04-26 19:05:25 +08:00
|
|
|
static struct i2c_driver ds278x_battery_driver = {
|
2009-06-18 07:26:26 +08:00
|
|
|
.driver = {
|
|
|
|
.name = "ds2782-battery",
|
2013-03-10 21:34:05 +08:00
|
|
|
.pm = DS278X_BATTERY_PM_OPS,
|
2009-06-18 07:26:26 +08:00
|
|
|
},
|
2010-04-26 19:05:25 +08:00
|
|
|
.probe = ds278x_battery_probe,
|
|
|
|
.remove = ds278x_battery_remove,
|
|
|
|
.id_table = ds278x_id,
|
2009-06-18 07:26:26 +08:00
|
|
|
};
|
2012-01-21 14:42:54 +08:00
|
|
|
module_i2c_driver(ds278x_battery_driver);
|
2009-06-18 07:26:26 +08:00
|
|
|
|
2011-06-15 12:45:36 +08:00
|
|
|
MODULE_AUTHOR("Ryan Mallon");
|
2009-06-18 07:26:26 +08:00
|
|
|
MODULE_DESCRIPTION("Maxim/Dallas DS2782 Stand-Alone Fuel Gauage IC driver");
|
|
|
|
MODULE_LICENSE("GPL");
|