2010-09-06 00:31:23 +02:00
|
|
|
/*
|
2011-12-06 02:50:46 +01:00
|
|
|
* Gas Gauge driver for SBS Compliant Batteries
|
2010-09-06 00:31:23 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 2010, NVIDIA Corporation.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/power_supply.h>
|
|
|
|
#include <linux/i2c.h>
|
|
|
|
#include <linux/slab.h>
|
2011-03-01 01:55:28 +01:00
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/gpio.h>
|
2013-03-14 11:19:46 +01:00
|
|
|
#include <linux/of.h>
|
2011-03-01 01:55:28 +01:00
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
#include <linux/power/sbs-battery.h>
|
2010-09-06 00:31:23 +02:00
|
|
|
|
|
|
|
enum {
|
|
|
|
REG_MANUFACTURER_DATA,
|
|
|
|
REG_TEMPERATURE,
|
|
|
|
REG_VOLTAGE,
|
|
|
|
REG_CURRENT,
|
|
|
|
REG_CAPACITY,
|
|
|
|
REG_TIME_TO_EMPTY,
|
|
|
|
REG_TIME_TO_FULL,
|
|
|
|
REG_STATUS,
|
|
|
|
REG_CYCLE_COUNT,
|
2010-09-22 00:33:55 +02:00
|
|
|
REG_SERIAL_NUMBER,
|
|
|
|
REG_REMAINING_CAPACITY,
|
2011-01-25 20:10:06 +01:00
|
|
|
REG_REMAINING_CAPACITY_CHARGE,
|
2010-09-22 00:33:55 +02:00
|
|
|
REG_FULL_CHARGE_CAPACITY,
|
2011-01-25 20:10:06 +01:00
|
|
|
REG_FULL_CHARGE_CAPACITY_CHARGE,
|
2010-09-22 00:33:55 +02:00
|
|
|
REG_DESIGN_CAPACITY,
|
2011-01-25 20:10:06 +01:00
|
|
|
REG_DESIGN_CAPACITY_CHARGE,
|
2010-09-22 00:33:55 +02:00
|
|
|
REG_DESIGN_VOLTAGE,
|
2010-09-06 00:31:23 +02:00
|
|
|
};
|
|
|
|
|
2011-01-25 20:10:06 +01:00
|
|
|
/* Battery Mode defines */
|
|
|
|
#define BATTERY_MODE_OFFSET 0x03
|
|
|
|
#define BATTERY_MODE_MASK 0x8000
|
2011-12-06 02:50:46 +01:00
|
|
|
enum sbs_battery_mode {
|
2011-01-25 20:10:06 +01:00
|
|
|
BATTERY_MODE_AMPS,
|
|
|
|
BATTERY_MODE_WATTS
|
|
|
|
};
|
|
|
|
|
2010-09-06 00:31:23 +02:00
|
|
|
/* manufacturer access defines */
|
|
|
|
#define MANUFACTURER_ACCESS_STATUS 0x0006
|
|
|
|
#define MANUFACTURER_ACCESS_SLEEP 0x0011
|
|
|
|
|
|
|
|
/* battery status value bits */
|
2010-09-22 00:33:55 +02:00
|
|
|
#define BATTERY_DISCHARGING 0x40
|
2010-09-06 00:31:23 +02:00
|
|
|
#define BATTERY_FULL_CHARGED 0x20
|
|
|
|
#define BATTERY_FULL_DISCHARGED 0x10
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
#define SBS_DATA(_psp, _addr, _min_value, _max_value) { \
|
2010-09-06 00:31:23 +02:00
|
|
|
.psp = _psp, \
|
|
|
|
.addr = _addr, \
|
|
|
|
.min_value = _min_value, \
|
|
|
|
.max_value = _max_value, \
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
static const struct chip_data {
|
2010-09-06 00:31:23 +02:00
|
|
|
enum power_supply_property psp;
|
|
|
|
u8 addr;
|
|
|
|
int min_value;
|
|
|
|
int max_value;
|
2011-12-06 02:50:46 +01:00
|
|
|
} sbs_data[] = {
|
2010-09-06 00:31:23 +02:00
|
|
|
[REG_MANUFACTURER_DATA] =
|
2011-12-06 02:50:46 +01:00
|
|
|
SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535),
|
2010-09-06 00:31:23 +02:00
|
|
|
[REG_TEMPERATURE] =
|
2011-12-06 02:50:46 +01:00
|
|
|
SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535),
|
2010-09-06 00:31:23 +02:00
|
|
|
[REG_VOLTAGE] =
|
2011-12-06 02:50:46 +01:00
|
|
|
SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000),
|
2010-09-06 00:31:23 +02:00
|
|
|
[REG_CURRENT] =
|
2011-12-06 02:50:46 +01:00
|
|
|
SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767),
|
2010-09-06 00:31:23 +02:00
|
|
|
[REG_CAPACITY] =
|
2012-04-25 08:59:03 +02:00
|
|
|
SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0D, 0, 100),
|
2010-09-22 00:33:55 +02:00
|
|
|
[REG_REMAINING_CAPACITY] =
|
2011-12-06 02:50:46 +01:00
|
|
|
SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535),
|
2011-01-25 20:10:06 +01:00
|
|
|
[REG_REMAINING_CAPACITY_CHARGE] =
|
2011-12-06 02:50:46 +01:00
|
|
|
SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535),
|
2010-09-22 00:33:55 +02:00
|
|
|
[REG_FULL_CHARGE_CAPACITY] =
|
2011-12-06 02:50:46 +01:00
|
|
|
SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535),
|
2011-01-25 20:10:06 +01:00
|
|
|
[REG_FULL_CHARGE_CAPACITY_CHARGE] =
|
2011-12-06 02:50:46 +01:00
|
|
|
SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535),
|
2010-09-06 00:31:23 +02:00
|
|
|
[REG_TIME_TO_EMPTY] =
|
2011-12-06 02:50:46 +01:00
|
|
|
SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535),
|
2010-09-06 00:31:23 +02:00
|
|
|
[REG_TIME_TO_FULL] =
|
2011-12-06 02:50:46 +01:00
|
|
|
SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535),
|
2010-09-06 00:31:23 +02:00
|
|
|
[REG_STATUS] =
|
2011-12-06 02:50:46 +01:00
|
|
|
SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535),
|
2010-09-06 00:31:23 +02:00
|
|
|
[REG_CYCLE_COUNT] =
|
2011-12-06 02:50:46 +01:00
|
|
|
SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535),
|
2010-09-22 00:33:55 +02:00
|
|
|
[REG_DESIGN_CAPACITY] =
|
2011-12-06 02:50:46 +01:00
|
|
|
SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535),
|
2011-01-25 20:10:06 +01:00
|
|
|
[REG_DESIGN_CAPACITY_CHARGE] =
|
2011-12-06 02:50:46 +01:00
|
|
|
SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535),
|
2010-09-22 00:33:55 +02:00
|
|
|
[REG_DESIGN_VOLTAGE] =
|
2011-12-06 02:50:46 +01:00
|
|
|
SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535),
|
2010-09-06 00:31:23 +02:00
|
|
|
[REG_SERIAL_NUMBER] =
|
2011-12-06 02:50:46 +01:00
|
|
|
SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535),
|
2010-09-06 00:31:23 +02:00
|
|
|
};
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
static enum power_supply_property sbs_properties[] = {
|
2010-09-06 00:31:23 +02:00
|
|
|
POWER_SUPPLY_PROP_STATUS,
|
|
|
|
POWER_SUPPLY_PROP_HEALTH,
|
|
|
|
POWER_SUPPLY_PROP_PRESENT,
|
|
|
|
POWER_SUPPLY_PROP_TECHNOLOGY,
|
|
|
|
POWER_SUPPLY_PROP_CYCLE_COUNT,
|
|
|
|
POWER_SUPPLY_PROP_VOLTAGE_NOW,
|
|
|
|
POWER_SUPPLY_PROP_CURRENT_NOW,
|
|
|
|
POWER_SUPPLY_PROP_CAPACITY,
|
|
|
|
POWER_SUPPLY_PROP_TEMP,
|
|
|
|
POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
|
|
|
|
POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
|
|
|
|
POWER_SUPPLY_PROP_SERIAL_NUMBER,
|
2010-09-22 00:33:55 +02:00
|
|
|
POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
|
|
|
|
POWER_SUPPLY_PROP_ENERGY_NOW,
|
|
|
|
POWER_SUPPLY_PROP_ENERGY_FULL,
|
|
|
|
POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
|
2011-01-25 20:10:06 +01:00
|
|
|
POWER_SUPPLY_PROP_CHARGE_NOW,
|
|
|
|
POWER_SUPPLY_PROP_CHARGE_FULL,
|
|
|
|
POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
|
2010-09-06 00:31:23 +02:00
|
|
|
};
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
struct sbs_info {
|
2011-03-01 01:55:28 +01:00
|
|
|
struct i2c_client *client;
|
|
|
|
struct power_supply power_supply;
|
2011-12-06 02:50:46 +01:00
|
|
|
struct sbs_platform_data *pdata;
|
2011-03-01 01:55:28 +01:00
|
|
|
bool is_present;
|
|
|
|
bool gpio_detect;
|
|
|
|
bool enable_detection;
|
|
|
|
int irq;
|
2011-05-24 21:05:59 +02:00
|
|
|
int last_state;
|
|
|
|
int poll_time;
|
|
|
|
struct delayed_work work;
|
|
|
|
int ignore_changes;
|
2010-09-06 00:31:23 +02:00
|
|
|
};
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
static int sbs_read_word_data(struct i2c_client *client, u8 address)
|
2010-09-22 00:33:55 +02:00
|
|
|
{
|
2011-12-06 02:50:46 +01:00
|
|
|
struct sbs_info *chip = i2c_get_clientdata(client);
|
2011-03-01 01:55:29 +01:00
|
|
|
s32 ret = 0;
|
|
|
|
int retries = 1;
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
if (chip->pdata)
|
|
|
|
retries = max(chip->pdata->i2c_retry_count + 1, 1);
|
2011-03-01 01:55:29 +01:00
|
|
|
|
|
|
|
while (retries > 0) {
|
|
|
|
ret = i2c_smbus_read_word_data(client, address);
|
|
|
|
if (ret >= 0)
|
|
|
|
break;
|
|
|
|
retries--;
|
|
|
|
}
|
2010-09-22 00:33:55 +02:00
|
|
|
|
|
|
|
if (ret < 0) {
|
bq20z75: Fix issues with present and suspend
There are a few issues found around the battery not being present. If the
battery isn't present, then a few undesirable things happen. The first was
excessive reporting of failed properties. This was fixed by instead
returning ENODATA for all properties other than PRESENT if the battery
isn't present. That way the callers can identify the difference between a
failure and the battery not being there.
The next issue was in the suspend logic. It was found that if the battery
wasn't present, then it would return a failure, preventing the system from
going into suspend. If there is no battery present, the io is expected to
fail, so in that case, we shouldn't return the failure and just
acknowledge that it was expected.
I also found that when a gpio was used, i didn't maintain the internal
is_present state properly. I added a set of that to fix that.
Lastly, the code to see io's fail and figure out that the battery isn't
present when not using a gpio had a problem. In that code, it looked for
the read to fail and if it did, then handled it. The problem is that in
function to get the property, it first writes a value and that write can
fail, causing the code to never reach the logic after the read. Fix is
to move the logic till after the write.
Signed-off-by: Rhyland Klein <rklein@nvidia.com>
Signed-off-by: Anton Vorontsov <cbouatmailru@gmail.com>
2011-03-10 01:18:02 +01:00
|
|
|
dev_dbg(&client->dev,
|
2010-09-22 00:33:55 +02:00
|
|
|
"%s: i2c read at address 0x%x failed\n",
|
|
|
|
__func__, address);
|
|
|
|
return ret;
|
|
|
|
}
|
2011-03-01 01:55:29 +01:00
|
|
|
|
2010-09-22 00:33:55 +02:00
|
|
|
return le16_to_cpu(ret);
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
static int sbs_write_word_data(struct i2c_client *client, u8 address,
|
2010-09-22 00:33:55 +02:00
|
|
|
u16 value)
|
|
|
|
{
|
2011-12-06 02:50:46 +01:00
|
|
|
struct sbs_info *chip = i2c_get_clientdata(client);
|
2011-03-01 01:55:29 +01:00
|
|
|
s32 ret = 0;
|
|
|
|
int retries = 1;
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
if (chip->pdata)
|
|
|
|
retries = max(chip->pdata->i2c_retry_count + 1, 1);
|
2011-03-01 01:55:29 +01:00
|
|
|
|
|
|
|
while (retries > 0) {
|
|
|
|
ret = i2c_smbus_write_word_data(client, address,
|
|
|
|
le16_to_cpu(value));
|
|
|
|
if (ret >= 0)
|
|
|
|
break;
|
|
|
|
retries--;
|
|
|
|
}
|
2010-09-22 00:33:55 +02:00
|
|
|
|
|
|
|
if (ret < 0) {
|
bq20z75: Fix issues with present and suspend
There are a few issues found around the battery not being present. If the
battery isn't present, then a few undesirable things happen. The first was
excessive reporting of failed properties. This was fixed by instead
returning ENODATA for all properties other than PRESENT if the battery
isn't present. That way the callers can identify the difference between a
failure and the battery not being there.
The next issue was in the suspend logic. It was found that if the battery
wasn't present, then it would return a failure, preventing the system from
going into suspend. If there is no battery present, the io is expected to
fail, so in that case, we shouldn't return the failure and just
acknowledge that it was expected.
I also found that when a gpio was used, i didn't maintain the internal
is_present state properly. I added a set of that to fix that.
Lastly, the code to see io's fail and figure out that the battery isn't
present when not using a gpio had a problem. In that code, it looked for
the read to fail and if it did, then handled it. The problem is that in
function to get the property, it first writes a value and that write can
fail, causing the code to never reach the logic after the read. Fix is
to move the logic till after the write.
Signed-off-by: Rhyland Klein <rklein@nvidia.com>
Signed-off-by: Anton Vorontsov <cbouatmailru@gmail.com>
2011-03-10 01:18:02 +01:00
|
|
|
dev_dbg(&client->dev,
|
2010-09-22 00:33:55 +02:00
|
|
|
"%s: i2c write to address 0x%x failed\n",
|
|
|
|
__func__, address);
|
|
|
|
return ret;
|
|
|
|
}
|
2011-03-01 01:55:29 +01:00
|
|
|
|
2010-09-22 00:33:55 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
static int sbs_get_battery_presence_and_health(
|
2010-09-06 00:31:23 +02:00
|
|
|
struct i2c_client *client, enum power_supply_property psp,
|
|
|
|
union power_supply_propval *val)
|
|
|
|
{
|
|
|
|
s32 ret;
|
2011-12-06 02:50:46 +01:00
|
|
|
struct sbs_info *chip = i2c_get_clientdata(client);
|
2011-03-01 01:55:28 +01:00
|
|
|
|
|
|
|
if (psp == POWER_SUPPLY_PROP_PRESENT &&
|
2011-12-06 02:50:46 +01:00
|
|
|
chip->gpio_detect) {
|
|
|
|
ret = gpio_get_value(chip->pdata->battery_detect);
|
|
|
|
if (ret == chip->pdata->battery_detect_present)
|
2011-03-01 01:55:28 +01:00
|
|
|
val->intval = 1;
|
|
|
|
else
|
|
|
|
val->intval = 0;
|
2011-12-06 02:50:46 +01:00
|
|
|
chip->is_present = val->intval;
|
2011-03-01 01:55:28 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2010-09-06 00:31:23 +02:00
|
|
|
|
|
|
|
/* Write to ManufacturerAccess with
|
|
|
|
* ManufacturerAccess command and then
|
|
|
|
* read the status */
|
2011-12-06 02:50:46 +01:00
|
|
|
ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
|
|
|
|
MANUFACTURER_ACCESS_STATUS);
|
2011-03-01 01:55:28 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
if (psp == POWER_SUPPLY_PROP_PRESENT)
|
|
|
|
val->intval = 0; /* battery removed */
|
2010-09-22 00:33:55 +02:00
|
|
|
return ret;
|
2011-03-01 01:55:28 +01:00
|
|
|
}
|
2010-09-06 00:31:23 +02:00
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr);
|
bq20z75: Fix issues with present and suspend
There are a few issues found around the battery not being present. If the
battery isn't present, then a few undesirable things happen. The first was
excessive reporting of failed properties. This was fixed by instead
returning ENODATA for all properties other than PRESENT if the battery
isn't present. That way the callers can identify the difference between a
failure and the battery not being there.
The next issue was in the suspend logic. It was found that if the battery
wasn't present, then it would return a failure, preventing the system from
going into suspend. If there is no battery present, the io is expected to
fail, so in that case, we shouldn't return the failure and just
acknowledge that it was expected.
I also found that when a gpio was used, i didn't maintain the internal
is_present state properly. I added a set of that to fix that.
Lastly, the code to see io's fail and figure out that the battery isn't
present when not using a gpio had a problem. In that code, it looked for
the read to fail and if it did, then handled it. The problem is that in
function to get the property, it first writes a value and that write can
fail, causing the code to never reach the logic after the read. Fix is
to move the logic till after the write.
Signed-off-by: Rhyland Klein <rklein@nvidia.com>
Signed-off-by: Anton Vorontsov <cbouatmailru@gmail.com>
2011-03-10 01:18:02 +01:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value ||
|
|
|
|
ret > sbs_data[REG_MANUFACTURER_DATA].max_value) {
|
2010-09-06 00:31:23 +02:00
|
|
|
val->intval = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Mask the upper nibble of 2nd byte and
|
|
|
|
* lower byte of response then
|
|
|
|
* shift the result by 8 to get status*/
|
|
|
|
ret &= 0x0F00;
|
|
|
|
ret >>= 8;
|
|
|
|
if (psp == POWER_SUPPLY_PROP_PRESENT) {
|
|
|
|
if (ret == 0x0F)
|
|
|
|
/* battery removed */
|
|
|
|
val->intval = 0;
|
|
|
|
else
|
|
|
|
val->intval = 1;
|
|
|
|
} else if (psp == POWER_SUPPLY_PROP_HEALTH) {
|
|
|
|
if (ret == 0x09)
|
|
|
|
val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
|
|
|
|
else if (ret == 0x0B)
|
|
|
|
val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
|
|
|
|
else if (ret == 0x0C)
|
|
|
|
val->intval = POWER_SUPPLY_HEALTH_DEAD;
|
|
|
|
else
|
|
|
|
val->intval = POWER_SUPPLY_HEALTH_GOOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
static int sbs_get_battery_property(struct i2c_client *client,
|
2010-09-06 00:31:23 +02:00
|
|
|
int reg_offset, enum power_supply_property psp,
|
|
|
|
union power_supply_propval *val)
|
|
|
|
{
|
2011-12-06 02:50:46 +01:00
|
|
|
struct sbs_info *chip = i2c_get_clientdata(client);
|
2010-09-06 00:31:23 +02:00
|
|
|
s32 ret;
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
|
2010-09-22 00:33:55 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* returned values are 16 bit */
|
2011-12-06 02:50:46 +01:00
|
|
|
if (sbs_data[reg_offset].min_value < 0)
|
2010-09-22 00:33:55 +02:00
|
|
|
ret = (s16)ret;
|
2010-09-06 00:31:23 +02:00
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
if (ret >= sbs_data[reg_offset].min_value &&
|
|
|
|
ret <= sbs_data[reg_offset].max_value) {
|
2010-09-06 00:31:23 +02:00
|
|
|
val->intval = ret;
|
2011-05-24 21:05:59 +02:00
|
|
|
if (psp != POWER_SUPPLY_PROP_STATUS)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (ret & BATTERY_FULL_CHARGED)
|
|
|
|
val->intval = POWER_SUPPLY_STATUS_FULL;
|
|
|
|
else if (ret & BATTERY_FULL_DISCHARGED)
|
|
|
|
val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
|
|
|
|
else if (ret & BATTERY_DISCHARGING)
|
|
|
|
val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
|
|
|
|
else
|
|
|
|
val->intval = POWER_SUPPLY_STATUS_CHARGING;
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
if (chip->poll_time == 0)
|
|
|
|
chip->last_state = val->intval;
|
|
|
|
else if (chip->last_state != val->intval) {
|
|
|
|
cancel_delayed_work_sync(&chip->work);
|
|
|
|
power_supply_changed(&chip->power_supply);
|
|
|
|
chip->poll_time = 0;
|
2010-09-06 00:31:23 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (psp == POWER_SUPPLY_PROP_STATUS)
|
|
|
|
val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
|
|
|
|
else
|
|
|
|
val->intval = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
static void sbs_unit_adjustment(struct i2c_client *client,
|
2010-09-22 00:33:55 +02:00
|
|
|
enum power_supply_property psp, union power_supply_propval *val)
|
|
|
|
{
|
|
|
|
#define BASE_UNIT_CONVERSION 1000
|
|
|
|
#define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION)
|
2011-02-28 02:41:48 +01:00
|
|
|
#define TIME_UNIT_CONVERSION 60
|
|
|
|
#define TEMP_KELVIN_TO_CELSIUS 2731
|
2010-09-22 00:33:55 +02:00
|
|
|
switch (psp) {
|
|
|
|
case POWER_SUPPLY_PROP_ENERGY_NOW:
|
|
|
|
case POWER_SUPPLY_PROP_ENERGY_FULL:
|
|
|
|
case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
|
2011-12-06 02:50:46 +01:00
|
|
|
/* sbs provides energy in units of 10mWh.
|
2011-02-28 02:41:48 +01:00
|
|
|
* Convert to µWh
|
|
|
|
*/
|
2010-09-22 00:33:55 +02:00
|
|
|
val->intval *= BATTERY_MODE_CAP_MULT_WATT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case POWER_SUPPLY_PROP_VOLTAGE_NOW:
|
|
|
|
case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
|
|
|
|
case POWER_SUPPLY_PROP_CURRENT_NOW:
|
2011-01-25 20:10:06 +01:00
|
|
|
case POWER_SUPPLY_PROP_CHARGE_NOW:
|
|
|
|
case POWER_SUPPLY_PROP_CHARGE_FULL:
|
|
|
|
case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
|
2010-09-22 00:33:55 +02:00
|
|
|
val->intval *= BASE_UNIT_CONVERSION;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case POWER_SUPPLY_PROP_TEMP:
|
2011-12-06 02:50:46 +01:00
|
|
|
/* sbs provides battery temperature in 0.1K
|
2011-02-28 02:41:48 +01:00
|
|
|
* so convert it to 0.1°C
|
|
|
|
*/
|
|
|
|
val->intval -= TEMP_KELVIN_TO_CELSIUS;
|
2010-09-22 00:33:55 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
|
|
|
|
case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
|
2011-12-06 02:50:46 +01:00
|
|
|
/* sbs provides time to empty and time to full in minutes.
|
2011-02-28 02:41:48 +01:00
|
|
|
* Convert to seconds
|
|
|
|
*/
|
2010-09-22 00:33:55 +02:00
|
|
|
val->intval *= TIME_UNIT_CONVERSION;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
dev_dbg(&client->dev,
|
|
|
|
"%s: no need for unit conversion %d\n", __func__, psp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
static enum sbs_battery_mode sbs_set_battery_mode(struct i2c_client *client,
|
|
|
|
enum sbs_battery_mode mode)
|
2011-01-25 20:10:06 +01:00
|
|
|
{
|
|
|
|
int ret, original_val;
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET);
|
2011-01-25 20:10:06 +01:00
|
|
|
if (original_val < 0)
|
|
|
|
return original_val;
|
|
|
|
|
|
|
|
if ((original_val & BATTERY_MODE_MASK) == mode)
|
|
|
|
return mode;
|
|
|
|
|
|
|
|
if (mode == BATTERY_MODE_AMPS)
|
|
|
|
ret = original_val & ~BATTERY_MODE_MASK;
|
|
|
|
else
|
|
|
|
ret = original_val | BATTERY_MODE_MASK;
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret);
|
2011-01-25 20:10:06 +01:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return original_val & BATTERY_MODE_MASK;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
static int sbs_get_battery_capacity(struct i2c_client *client,
|
2010-09-22 00:33:55 +02:00
|
|
|
int reg_offset, enum power_supply_property psp,
|
2010-09-06 00:31:23 +02:00
|
|
|
union power_supply_propval *val)
|
|
|
|
{
|
|
|
|
s32 ret;
|
2011-12-06 02:50:46 +01:00
|
|
|
enum sbs_battery_mode mode = BATTERY_MODE_WATTS;
|
2011-01-25 20:10:06 +01:00
|
|
|
|
|
|
|
if (power_supply_is_amp_property(psp))
|
|
|
|
mode = BATTERY_MODE_AMPS;
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
mode = sbs_set_battery_mode(client, mode);
|
2011-01-25 20:10:06 +01:00
|
|
|
if (mode < 0)
|
|
|
|
return mode;
|
2010-09-06 00:31:23 +02:00
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
|
2010-09-22 00:33:55 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2010-09-06 00:31:23 +02:00
|
|
|
|
2010-09-22 00:33:55 +02:00
|
|
|
if (psp == POWER_SUPPLY_PROP_CAPACITY) {
|
2011-12-06 02:50:46 +01:00
|
|
|
/* sbs spec says that this can be >100 %
|
2010-09-22 00:33:55 +02:00
|
|
|
* even if max value is 100 % */
|
|
|
|
val->intval = min(ret, 100);
|
|
|
|
} else
|
|
|
|
val->intval = ret;
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
ret = sbs_set_battery_mode(client, mode);
|
2011-01-25 20:10:06 +01:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2010-09-22 00:33:55 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
static char sbs_serial[5];
|
|
|
|
static int sbs_get_battery_serial_number(struct i2c_client *client,
|
2010-09-22 00:33:55 +02:00
|
|
|
union power_supply_propval *val)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr);
|
2010-09-22 00:33:55 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
ret = sprintf(sbs_serial, "%04x", ret);
|
|
|
|
val->strval = sbs_serial;
|
2010-09-06 00:31:23 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
static int sbs_get_property_index(struct i2c_client *client,
|
2011-01-25 20:10:06 +01:00
|
|
|
enum power_supply_property psp)
|
|
|
|
{
|
|
|
|
int count;
|
2011-12-06 02:50:46 +01:00
|
|
|
for (count = 0; count < ARRAY_SIZE(sbs_data); count++)
|
|
|
|
if (psp == sbs_data[count].psp)
|
2011-01-25 20:10:06 +01:00
|
|
|
return count;
|
|
|
|
|
|
|
|
dev_warn(&client->dev,
|
|
|
|
"%s: Invalid Property - %d\n", __func__, psp);
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
static int sbs_get_property(struct power_supply *psy,
|
2010-09-06 00:31:23 +02:00
|
|
|
enum power_supply_property psp,
|
|
|
|
union power_supply_propval *val)
|
|
|
|
{
|
2011-03-01 01:55:28 +01:00
|
|
|
int ret = 0;
|
2011-12-06 02:50:46 +01:00
|
|
|
struct sbs_info *chip = container_of(psy,
|
|
|
|
struct sbs_info, power_supply);
|
|
|
|
struct i2c_client *client = chip->client;
|
2010-09-06 00:31:23 +02:00
|
|
|
|
|
|
|
switch (psp) {
|
|
|
|
case POWER_SUPPLY_PROP_PRESENT:
|
|
|
|
case POWER_SUPPLY_PROP_HEALTH:
|
2011-12-06 02:50:46 +01:00
|
|
|
ret = sbs_get_battery_presence_and_health(client, psp, val);
|
bq20z75: Fix issues with present and suspend
There are a few issues found around the battery not being present. If the
battery isn't present, then a few undesirable things happen. The first was
excessive reporting of failed properties. This was fixed by instead
returning ENODATA for all properties other than PRESENT if the battery
isn't present. That way the callers can identify the difference between a
failure and the battery not being there.
The next issue was in the suspend logic. It was found that if the battery
wasn't present, then it would return a failure, preventing the system from
going into suspend. If there is no battery present, the io is expected to
fail, so in that case, we shouldn't return the failure and just
acknowledge that it was expected.
I also found that when a gpio was used, i didn't maintain the internal
is_present state properly. I added a set of that to fix that.
Lastly, the code to see io's fail and figure out that the battery isn't
present when not using a gpio had a problem. In that code, it looked for
the read to fail and if it did, then handled it. The problem is that in
function to get the property, it first writes a value and that write can
fail, causing the code to never reach the logic after the read. Fix is
to move the logic till after the write.
Signed-off-by: Rhyland Klein <rklein@nvidia.com>
Signed-off-by: Anton Vorontsov <cbouatmailru@gmail.com>
2011-03-10 01:18:02 +01:00
|
|
|
if (psp == POWER_SUPPLY_PROP_PRESENT)
|
|
|
|
return 0;
|
2010-09-06 00:31:23 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case POWER_SUPPLY_PROP_TECHNOLOGY:
|
|
|
|
val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
|
2012-05-09 08:30:44 +02:00
|
|
|
goto done; /* don't trigger power_supply_changed()! */
|
2010-09-06 00:31:23 +02:00
|
|
|
|
2010-09-22 00:33:55 +02:00
|
|
|
case POWER_SUPPLY_PROP_ENERGY_NOW:
|
|
|
|
case POWER_SUPPLY_PROP_ENERGY_FULL:
|
|
|
|
case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
|
2011-01-25 20:10:06 +01:00
|
|
|
case POWER_SUPPLY_PROP_CHARGE_NOW:
|
|
|
|
case POWER_SUPPLY_PROP_CHARGE_FULL:
|
|
|
|
case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
|
2010-09-06 00:31:23 +02:00
|
|
|
case POWER_SUPPLY_PROP_CAPACITY:
|
2011-12-06 02:50:46 +01:00
|
|
|
ret = sbs_get_property_index(client, psp);
|
2011-03-01 01:55:28 +01:00
|
|
|
if (ret < 0)
|
|
|
|
break;
|
2010-09-22 00:33:55 +02:00
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
ret = sbs_get_battery_capacity(client, ret, psp, val);
|
2010-09-22 00:33:55 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case POWER_SUPPLY_PROP_SERIAL_NUMBER:
|
2011-12-06 02:50:46 +01:00
|
|
|
ret = sbs_get_battery_serial_number(client, val);
|
2010-09-06 00:31:23 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case POWER_SUPPLY_PROP_STATUS:
|
|
|
|
case POWER_SUPPLY_PROP_CYCLE_COUNT:
|
|
|
|
case POWER_SUPPLY_PROP_VOLTAGE_NOW:
|
|
|
|
case POWER_SUPPLY_PROP_CURRENT_NOW:
|
|
|
|
case POWER_SUPPLY_PROP_TEMP:
|
|
|
|
case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
|
|
|
|
case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
|
2010-09-22 00:33:55 +02:00
|
|
|
case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
|
2011-12-06 02:50:46 +01:00
|
|
|
ret = sbs_get_property_index(client, psp);
|
2011-03-01 01:55:28 +01:00
|
|
|
if (ret < 0)
|
|
|
|
break;
|
2010-09-22 00:33:55 +02:00
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
ret = sbs_get_battery_property(client, ret, psp, val);
|
2010-09-06 00:31:23 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
dev_err(&client->dev,
|
|
|
|
"%s: INVALID property\n", __func__);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
if (!chip->enable_detection)
|
2011-03-01 01:55:28 +01:00
|
|
|
goto done;
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
if (!chip->gpio_detect &&
|
|
|
|
chip->is_present != (ret >= 0)) {
|
|
|
|
chip->is_present = (ret >= 0);
|
|
|
|
power_supply_changed(&chip->power_supply);
|
2011-03-01 01:55:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (!ret) {
|
|
|
|
/* Convert units to match requirements for power supply class */
|
2011-12-06 02:50:46 +01:00
|
|
|
sbs_unit_adjustment(client, psp, val);
|
2011-03-01 01:55:28 +01:00
|
|
|
}
|
2010-09-22 00:33:55 +02:00
|
|
|
|
2010-09-06 00:31:23 +02:00
|
|
|
dev_dbg(&client->dev,
|
bq20z75: Fix issues with present and suspend
There are a few issues found around the battery not being present. If the
battery isn't present, then a few undesirable things happen. The first was
excessive reporting of failed properties. This was fixed by instead
returning ENODATA for all properties other than PRESENT if the battery
isn't present. That way the callers can identify the difference between a
failure and the battery not being there.
The next issue was in the suspend logic. It was found that if the battery
wasn't present, then it would return a failure, preventing the system from
going into suspend. If there is no battery present, the io is expected to
fail, so in that case, we shouldn't return the failure and just
acknowledge that it was expected.
I also found that when a gpio was used, i didn't maintain the internal
is_present state properly. I added a set of that to fix that.
Lastly, the code to see io's fail and figure out that the battery isn't
present when not using a gpio had a problem. In that code, it looked for
the read to fail and if it did, then handled it. The problem is that in
function to get the property, it first writes a value and that write can
fail, causing the code to never reach the logic after the read. Fix is
to move the logic till after the write.
Signed-off-by: Rhyland Klein <rklein@nvidia.com>
Signed-off-by: Anton Vorontsov <cbouatmailru@gmail.com>
2011-03-10 01:18:02 +01:00
|
|
|
"%s: property = %d, value = %x\n", __func__, psp, val->intval);
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
if (ret && chip->is_present)
|
bq20z75: Fix issues with present and suspend
There are a few issues found around the battery not being present. If the
battery isn't present, then a few undesirable things happen. The first was
excessive reporting of failed properties. This was fixed by instead
returning ENODATA for all properties other than PRESENT if the battery
isn't present. That way the callers can identify the difference between a
failure and the battery not being there.
The next issue was in the suspend logic. It was found that if the battery
wasn't present, then it would return a failure, preventing the system from
going into suspend. If there is no battery present, the io is expected to
fail, so in that case, we shouldn't return the failure and just
acknowledge that it was expected.
I also found that when a gpio was used, i didn't maintain the internal
is_present state properly. I added a set of that to fix that.
Lastly, the code to see io's fail and figure out that the battery isn't
present when not using a gpio had a problem. In that code, it looked for
the read to fail and if it did, then handled it. The problem is that in
function to get the property, it first writes a value and that write can
fail, causing the code to never reach the logic after the read. Fix is
to move the logic till after the write.
Signed-off-by: Rhyland Klein <rklein@nvidia.com>
Signed-off-by: Anton Vorontsov <cbouatmailru@gmail.com>
2011-03-10 01:18:02 +01:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* battery not present, so return NODATA for properties */
|
|
|
|
if (ret)
|
|
|
|
return -ENODATA;
|
2010-09-06 00:31:23 +02:00
|
|
|
|
bq20z75: Fix issues with present and suspend
There are a few issues found around the battery not being present. If the
battery isn't present, then a few undesirable things happen. The first was
excessive reporting of failed properties. This was fixed by instead
returning ENODATA for all properties other than PRESENT if the battery
isn't present. That way the callers can identify the difference between a
failure and the battery not being there.
The next issue was in the suspend logic. It was found that if the battery
wasn't present, then it would return a failure, preventing the system from
going into suspend. If there is no battery present, the io is expected to
fail, so in that case, we shouldn't return the failure and just
acknowledge that it was expected.
I also found that when a gpio was used, i didn't maintain the internal
is_present state properly. I added a set of that to fix that.
Lastly, the code to see io's fail and figure out that the battery isn't
present when not using a gpio had a problem. In that code, it looked for
the read to fail and if it did, then handled it. The problem is that in
function to get the property, it first writes a value and that write can
fail, causing the code to never reach the logic after the read. Fix is
to move the logic till after the write.
Signed-off-by: Rhyland Klein <rklein@nvidia.com>
Signed-off-by: Anton Vorontsov <cbouatmailru@gmail.com>
2011-03-10 01:18:02 +01:00
|
|
|
return 0;
|
2010-09-06 00:31:23 +02:00
|
|
|
}
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
static irqreturn_t sbs_irq(int irq, void *devid)
|
2011-03-01 01:55:28 +01:00
|
|
|
{
|
|
|
|
struct power_supply *battery = devid;
|
|
|
|
|
|
|
|
power_supply_changed(battery);
|
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
static void sbs_external_power_changed(struct power_supply *psy)
|
2011-05-24 21:05:59 +02:00
|
|
|
{
|
2011-12-06 02:50:46 +01:00
|
|
|
struct sbs_info *chip;
|
2011-05-24 21:05:59 +02:00
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
chip = container_of(psy, struct sbs_info, power_supply);
|
2011-05-24 21:05:59 +02:00
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
if (chip->ignore_changes > 0) {
|
|
|
|
chip->ignore_changes--;
|
2011-05-24 21:05:59 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cancel outstanding work */
|
2011-12-06 02:50:46 +01:00
|
|
|
cancel_delayed_work_sync(&chip->work);
|
2011-05-24 21:05:59 +02:00
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
schedule_delayed_work(&chip->work, HZ);
|
|
|
|
chip->poll_time = chip->pdata->poll_retry_count;
|
2011-05-24 21:05:59 +02:00
|
|
|
}
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
static void sbs_delayed_work(struct work_struct *work)
|
2011-05-24 21:05:59 +02:00
|
|
|
{
|
2011-12-06 02:50:46 +01:00
|
|
|
struct sbs_info *chip;
|
2011-05-24 21:05:59 +02:00
|
|
|
s32 ret;
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
chip = container_of(work, struct sbs_info, work.work);
|
2011-05-24 21:05:59 +02:00
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr);
|
2011-05-24 21:05:59 +02:00
|
|
|
/* if the read failed, give up on this work */
|
|
|
|
if (ret < 0) {
|
2011-12-06 02:50:46 +01:00
|
|
|
chip->poll_time = 0;
|
2011-05-24 21:05:59 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret & BATTERY_FULL_CHARGED)
|
|
|
|
ret = POWER_SUPPLY_STATUS_FULL;
|
|
|
|
else if (ret & BATTERY_FULL_DISCHARGED)
|
|
|
|
ret = POWER_SUPPLY_STATUS_NOT_CHARGING;
|
|
|
|
else if (ret & BATTERY_DISCHARGING)
|
|
|
|
ret = POWER_SUPPLY_STATUS_DISCHARGING;
|
|
|
|
else
|
|
|
|
ret = POWER_SUPPLY_STATUS_CHARGING;
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
if (chip->last_state != ret) {
|
|
|
|
chip->poll_time = 0;
|
|
|
|
power_supply_changed(&chip->power_supply);
|
2011-05-24 21:05:59 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-12-06 02:50:46 +01:00
|
|
|
if (chip->poll_time > 0) {
|
|
|
|
schedule_delayed_work(&chip->work, HZ);
|
|
|
|
chip->poll_time--;
|
2011-05-24 21:05:59 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-14 22:19:07 +02:00
|
|
|
#if defined(CONFIG_OF)
|
|
|
|
|
|
|
|
#include <linux/of_device.h>
|
|
|
|
#include <linux/of_gpio.h>
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
static const struct of_device_id sbs_dt_ids[] = {
|
|
|
|
{ .compatible = "sbs,sbs-battery" },
|
2011-09-14 22:19:07 +02:00
|
|
|
{ .compatible = "ti,bq20z75" },
|
|
|
|
{ }
|
|
|
|
};
|
2012-01-06 02:45:34 +01:00
|
|
|
MODULE_DEVICE_TABLE(of, sbs_dt_ids);
|
2011-09-14 22:19:07 +02:00
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
static struct sbs_platform_data *sbs_of_populate_pdata(
|
2011-09-14 22:19:07 +02:00
|
|
|
struct i2c_client *client)
|
|
|
|
{
|
|
|
|
struct device_node *of_node = client->dev.of_node;
|
2011-12-06 02:50:46 +01:00
|
|
|
struct sbs_platform_data *pdata = client->dev.platform_data;
|
2011-09-14 22:19:07 +02:00
|
|
|
enum of_gpio_flags gpio_flags;
|
|
|
|
int rc;
|
|
|
|
u32 prop;
|
|
|
|
|
|
|
|
/* verify this driver matches this device */
|
|
|
|
if (!of_node)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* if platform data is set, honor it */
|
|
|
|
if (pdata)
|
|
|
|
return pdata;
|
|
|
|
|
|
|
|
/* first make sure at least one property is set, otherwise
|
|
|
|
* it won't change behavior from running without pdata.
|
|
|
|
*/
|
2011-12-06 02:50:46 +01:00
|
|
|
if (!of_get_property(of_node, "sbs,i2c-retry-count", NULL) &&
|
|
|
|
!of_get_property(of_node, "sbs,poll-retry-count", NULL) &&
|
|
|
|
!of_get_property(of_node, "sbs,battery-detect-gpios", NULL))
|
2011-09-14 22:19:07 +02:00
|
|
|
goto of_out;
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
pdata = devm_kzalloc(&client->dev, sizeof(struct sbs_platform_data),
|
2011-09-14 22:19:07 +02:00
|
|
|
GFP_KERNEL);
|
|
|
|
if (!pdata)
|
|
|
|
goto of_out;
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
rc = of_property_read_u32(of_node, "sbs,i2c-retry-count", &prop);
|
2011-09-14 22:19:07 +02:00
|
|
|
if (!rc)
|
|
|
|
pdata->i2c_retry_count = prop;
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
rc = of_property_read_u32(of_node, "sbs,poll-retry-count", &prop);
|
2011-09-14 22:19:07 +02:00
|
|
|
if (!rc)
|
|
|
|
pdata->poll_retry_count = prop;
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
if (!of_get_property(of_node, "sbs,battery-detect-gpios", NULL)) {
|
2011-09-14 22:19:07 +02:00
|
|
|
pdata->battery_detect = -1;
|
|
|
|
goto of_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
pdata->battery_detect = of_get_named_gpio_flags(of_node,
|
2011-12-06 02:50:46 +01:00
|
|
|
"sbs,battery-detect-gpios", 0, &gpio_flags);
|
2011-09-14 22:19:07 +02:00
|
|
|
|
|
|
|
if (gpio_flags & OF_GPIO_ACTIVE_LOW)
|
|
|
|
pdata->battery_detect_present = 0;
|
|
|
|
else
|
|
|
|
pdata->battery_detect_present = 1;
|
|
|
|
|
|
|
|
of_out:
|
|
|
|
return pdata;
|
|
|
|
}
|
|
|
|
#else
|
2011-12-06 02:50:46 +01:00
|
|
|
static struct sbs_platform_data *sbs_of_populate_pdata(
|
2011-09-14 22:19:07 +02:00
|
|
|
struct i2c_client *client)
|
|
|
|
{
|
|
|
|
return client->dev.platform_data;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-11-19 19:22:23 +01:00
|
|
|
static int sbs_probe(struct i2c_client *client,
|
2010-09-06 00:31:23 +02:00
|
|
|
const struct i2c_device_id *id)
|
|
|
|
{
|
2011-12-06 02:50:46 +01:00
|
|
|
struct sbs_info *chip;
|
|
|
|
struct sbs_platform_data *pdata = client->dev.platform_data;
|
2010-09-06 00:31:23 +02:00
|
|
|
int rc;
|
2011-03-01 01:55:28 +01:00
|
|
|
int irq;
|
2011-12-06 02:50:49 +01:00
|
|
|
char *name;
|
2010-09-06 00:31:23 +02:00
|
|
|
|
2011-12-06 02:50:49 +01:00
|
|
|
name = kasprintf(GFP_KERNEL, "sbs-%s", dev_name(&client->dev));
|
|
|
|
if (!name) {
|
|
|
|
dev_err(&client->dev, "Failed to allocate device name\n");
|
2010-09-06 00:31:23 +02:00
|
|
|
return -ENOMEM;
|
2011-12-06 02:50:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
chip = kzalloc(sizeof(struct sbs_info), GFP_KERNEL);
|
|
|
|
if (!chip) {
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto exit_free_name;
|
|
|
|
}
|
2010-09-06 00:31:23 +02:00
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
chip->client = client;
|
|
|
|
chip->enable_detection = false;
|
|
|
|
chip->gpio_detect = false;
|
2011-12-06 02:50:49 +01:00
|
|
|
chip->power_supply.name = name;
|
2011-12-06 02:50:46 +01:00
|
|
|
chip->power_supply.type = POWER_SUPPLY_TYPE_BATTERY;
|
|
|
|
chip->power_supply.properties = sbs_properties;
|
|
|
|
chip->power_supply.num_properties = ARRAY_SIZE(sbs_properties);
|
|
|
|
chip->power_supply.get_property = sbs_get_property;
|
2011-05-24 21:05:59 +02:00
|
|
|
/* ignore first notification of external change, it is generated
|
|
|
|
* from the power_supply_register call back
|
|
|
|
*/
|
2011-12-06 02:50:46 +01:00
|
|
|
chip->ignore_changes = 1;
|
|
|
|
chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
|
|
|
|
chip->power_supply.external_power_changed = sbs_external_power_changed;
|
2010-09-06 00:31:23 +02:00
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
pdata = sbs_of_populate_pdata(client);
|
2011-09-14 22:19:07 +02:00
|
|
|
|
2011-03-01 01:55:28 +01:00
|
|
|
if (pdata) {
|
2011-12-06 02:50:46 +01:00
|
|
|
chip->gpio_detect = gpio_is_valid(pdata->battery_detect);
|
|
|
|
chip->pdata = pdata;
|
2011-03-01 01:55:28 +01:00
|
|
|
}
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
i2c_set_clientdata(client, chip);
|
2010-09-06 00:31:23 +02:00
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
if (!chip->gpio_detect)
|
2011-03-01 01:55:28 +01:00
|
|
|
goto skip_gpio;
|
|
|
|
|
|
|
|
rc = gpio_request(pdata->battery_detect, dev_name(&client->dev));
|
|
|
|
if (rc) {
|
|
|
|
dev_warn(&client->dev, "Failed to request gpio: %d\n", rc);
|
2011-12-06 02:50:46 +01:00
|
|
|
chip->gpio_detect = false;
|
2011-03-01 01:55:28 +01:00
|
|
|
goto skip_gpio;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = gpio_direction_input(pdata->battery_detect);
|
|
|
|
if (rc) {
|
|
|
|
dev_warn(&client->dev, "Failed to get gpio as input: %d\n", rc);
|
|
|
|
gpio_free(pdata->battery_detect);
|
2011-12-06 02:50:46 +01:00
|
|
|
chip->gpio_detect = false;
|
2011-03-01 01:55:28 +01:00
|
|
|
goto skip_gpio;
|
|
|
|
}
|
|
|
|
|
|
|
|
irq = gpio_to_irq(pdata->battery_detect);
|
|
|
|
if (irq <= 0) {
|
|
|
|
dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq);
|
|
|
|
gpio_free(pdata->battery_detect);
|
2011-12-06 02:50:46 +01:00
|
|
|
chip->gpio_detect = false;
|
2011-03-01 01:55:28 +01:00
|
|
|
goto skip_gpio;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
rc = request_irq(irq, sbs_irq,
|
2011-03-01 01:55:28 +01:00
|
|
|
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
|
2011-12-06 02:50:46 +01:00
|
|
|
dev_name(&client->dev), &chip->power_supply);
|
2011-03-01 01:55:28 +01:00
|
|
|
if (rc) {
|
|
|
|
dev_warn(&client->dev, "Failed to request irq: %d\n", rc);
|
|
|
|
gpio_free(pdata->battery_detect);
|
2011-12-06 02:50:46 +01:00
|
|
|
chip->gpio_detect = false;
|
2011-03-01 01:55:28 +01:00
|
|
|
goto skip_gpio;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
chip->irq = irq;
|
2011-03-01 01:55:28 +01:00
|
|
|
|
|
|
|
skip_gpio:
|
2012-09-06 20:32:29 +02:00
|
|
|
/*
|
|
|
|
* Before we register, we need to make sure we can actually talk
|
|
|
|
* to the battery.
|
|
|
|
*/
|
|
|
|
rc = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
|
|
|
|
if (rc < 0) {
|
|
|
|
dev_err(&client->dev, "%s: Failed to get device status\n",
|
|
|
|
__func__);
|
|
|
|
goto exit_psupply;
|
|
|
|
}
|
2011-03-01 01:55:28 +01:00
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
rc = power_supply_register(&client->dev, &chip->power_supply);
|
2010-09-06 00:31:23 +02:00
|
|
|
if (rc) {
|
|
|
|
dev_err(&client->dev,
|
|
|
|
"%s: Failed to register power supply\n", __func__);
|
2011-03-01 01:55:28 +01:00
|
|
|
goto exit_psupply;
|
2010-09-06 00:31:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dev_info(&client->dev,
|
|
|
|
"%s: battery gas gauge device registered\n", client->name);
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
|
2011-05-24 21:05:59 +02:00
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
chip->enable_detection = true;
|
2011-05-24 21:06:50 +02:00
|
|
|
|
2010-09-06 00:31:23 +02:00
|
|
|
return 0;
|
2011-03-01 01:55:28 +01:00
|
|
|
|
|
|
|
exit_psupply:
|
2011-12-06 02:50:46 +01:00
|
|
|
if (chip->irq)
|
|
|
|
free_irq(chip->irq, &chip->power_supply);
|
|
|
|
if (chip->gpio_detect)
|
2011-03-01 01:55:28 +01:00
|
|
|
gpio_free(pdata->battery_detect);
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
kfree(chip);
|
2011-03-01 01:55:28 +01:00
|
|
|
|
2011-12-06 02:50:49 +01:00
|
|
|
exit_free_name:
|
|
|
|
kfree(name);
|
|
|
|
|
2011-03-01 01:55:28 +01:00
|
|
|
return rc;
|
2010-09-06 00:31:23 +02:00
|
|
|
}
|
|
|
|
|
2012-11-19 19:26:07 +01:00
|
|
|
static int sbs_remove(struct i2c_client *client)
|
2010-09-06 00:31:23 +02:00
|
|
|
{
|
2011-12-06 02:50:46 +01:00
|
|
|
struct sbs_info *chip = i2c_get_clientdata(client);
|
2010-09-06 00:31:23 +02:00
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
if (chip->irq)
|
|
|
|
free_irq(chip->irq, &chip->power_supply);
|
|
|
|
if (chip->gpio_detect)
|
|
|
|
gpio_free(chip->pdata->battery_detect);
|
2011-03-01 01:55:28 +01:00
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
power_supply_unregister(&chip->power_supply);
|
2011-05-24 21:05:59 +02:00
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
cancel_delayed_work_sync(&chip->work);
|
2011-05-24 21:05:59 +02:00
|
|
|
|
2011-12-06 02:50:49 +01:00
|
|
|
kfree(chip->power_supply.name);
|
2011-12-06 02:50:46 +01:00
|
|
|
kfree(chip);
|
|
|
|
chip = NULL;
|
2010-09-06 00:31:23 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-10 14:34:07 +01:00
|
|
|
#if defined CONFIG_PM_SLEEP
|
|
|
|
|
|
|
|
static int sbs_suspend(struct device *dev)
|
2010-09-06 00:31:23 +02:00
|
|
|
{
|
2013-03-10 14:34:07 +01:00
|
|
|
struct i2c_client *client = to_i2c_client(dev);
|
2011-12-06 02:50:46 +01:00
|
|
|
struct sbs_info *chip = i2c_get_clientdata(client);
|
2010-09-06 00:31:23 +02:00
|
|
|
s32 ret;
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
if (chip->poll_time > 0)
|
|
|
|
cancel_delayed_work_sync(&chip->work);
|
2011-05-24 21:05:59 +02:00
|
|
|
|
2010-09-06 00:31:23 +02:00
|
|
|
/* write to manufacturer access with sleep command */
|
2011-12-06 02:50:46 +01:00
|
|
|
ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
|
2010-09-06 00:31:23 +02:00
|
|
|
MANUFACTURER_ACCESS_SLEEP);
|
2011-12-06 02:50:46 +01:00
|
|
|
if (chip->is_present && ret < 0)
|
2010-09-22 00:33:55 +02:00
|
|
|
return ret;
|
2010-09-06 00:31:23 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2013-03-10 14:34:07 +01:00
|
|
|
|
|
|
|
static SIMPLE_DEV_PM_OPS(sbs_pm_ops, sbs_suspend, NULL);
|
|
|
|
#define SBS_PM_OPS (&sbs_pm_ops)
|
|
|
|
|
2010-09-06 00:31:23 +02:00
|
|
|
#else
|
2013-03-10 14:34:07 +01:00
|
|
|
#define SBS_PM_OPS NULL
|
2010-09-06 00:31:23 +02:00
|
|
|
#endif
|
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
static const struct i2c_device_id sbs_id[] = {
|
2010-09-06 00:31:23 +02:00
|
|
|
{ "bq20z75", 0 },
|
2011-12-06 02:50:46 +01:00
|
|
|
{ "sbs-battery", 1 },
|
2010-09-06 00:31:23 +02:00
|
|
|
{}
|
|
|
|
};
|
2011-12-06 02:50:46 +01:00
|
|
|
MODULE_DEVICE_TABLE(i2c, sbs_id);
|
|
|
|
|
|
|
|
static struct i2c_driver sbs_battery_driver = {
|
|
|
|
.probe = sbs_probe,
|
2012-11-19 19:20:40 +01:00
|
|
|
.remove = sbs_remove,
|
2011-12-06 02:50:46 +01:00
|
|
|
.id_table = sbs_id,
|
2010-09-06 00:31:23 +02:00
|
|
|
.driver = {
|
2011-12-06 02:50:46 +01:00
|
|
|
.name = "sbs-battery",
|
2013-03-14 11:19:46 +01:00
|
|
|
.of_match_table = of_match_ptr(sbs_dt_ids),
|
2013-03-10 14:34:07 +01:00
|
|
|
.pm = SBS_PM_OPS,
|
2010-09-06 00:31:23 +02:00
|
|
|
},
|
|
|
|
};
|
2012-01-21 07:42:54 +01:00
|
|
|
module_i2c_driver(sbs_battery_driver);
|
2010-09-06 00:31:23 +02:00
|
|
|
|
2011-12-06 02:50:46 +01:00
|
|
|
MODULE_DESCRIPTION("SBS battery monitor driver");
|
2010-09-06 00:31:23 +02:00
|
|
|
MODULE_LICENSE("GPL");
|