主控:rk3399
系统:android7.1
内核版本:4.4.126
dts配置:
&i2c4 {
status = "okay";
clock-frequency = <80000>;
bq3060batter:battery@0b {
compatible = "BQ3060";
reg = <0x0b>;
};
};
内核驱动:
bq3060_battery.c
/*
* BQ3060 battery driver
*
* Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it>
* Copyright (C) 2008 Eurotech S.p.A. <info@eurotech.it>
* Copyright (C) 2010-2011 Lars-Peter Clausen <lars@metafoo.de>
* Copyright (C) 2011 Pali Rohár <pali.rohar@gmail.com>
*
* Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
*
* This package 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.
*
* THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* Datasheets:
* http://www.ti.com/product/BQ3060
*/
#include <linux/device.h>
#include <linux/module.h>
#include <linux/param.h>
#include <linux/jiffies.h>
#include <linux/workqueue.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/power_supply.h>
#include <linux/idr.h>
#include <linux/i2c.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <asm/unaligned.h>
#define DRIVER_VERSION "1.2.0"
#define BQ3060_MANUFACTURER "Texas Instruments"
/* BQ3060 regs */
//#define BQ3060_REG_AtRateTimeToFull 0x05
//#define BQ3060_REG_AtRateTimeToEmpty 0x06
#define BQ3060_REG_Temperature 0x08
#define BQ3060_REG_Voltage 0x09
#define BQ3060_REG_Current 0x0A
#define BQ3060_REG_AverageCurrent 0x0B
#define BQ3060_REG_RelativeOfCharge 0x0D
#define BQ3060_REG_RemainingCapacity 0x0F
#define BQ3060_REG_FullChargeCapacity 0x10
#define BQ3060_REG_RunTimeToEmpty 0x11
#define BQ3060_REG_AverageTimeToEmpty 0x12
#define BQ3060_REG_AverageTimeToFull 0x13
#define BQ3060_REG_ChargingCurrent 0x14
#define BQ3060_REG_ChargingVoltage 0x15
#define BQ3060_REG_BatteryStatus 0x16
#define BQ3060_REG_CycleCount 0x17
#define BQ3060_REG_DesignCapacity 0x18
#define BQ3060_REG_DesignVoltage 0x19
#define BQ3060_REG_SerialNumber 0x1C
/* BQ27XXX Flags */
#define BQ3060_FLAG_EC (BIT(0)|BIT(1)|BIT(2)|BIT(3))
#define BQ3060_FLAG_FD BIT(4)
#define BQ3060_FLAG_FC BIT(5)
#define BQ3060_FLAG_DSG BIT(6)
#define BQ3060_FLAG_INIT BIT(7)
#define BQ3060_FLAG_TDA BIT(11)
#define BQ3060_FLAG_OTA BIT(12)
#define BQ3060_FLAG_TCA BIT(14)
#define BQ3060_FLAG_OCA BIT(15)
struct BQ3060_reg_cache {
int temperature;
int time_to_empty;
int time_to_empty_avg;
int time_to_full;
int time_to_full_avg;
int charge_full;
int cycle_count;
int capacity;
int flags;
int health;
};
struct BQ3060_device_info {
struct i2c_client *client;
struct device *dev;
int id;
struct BQ3060_reg_cache cache;
int charge_design_full;
unsigned long last_update;
struct delayed_work work;
struct power_supply *bat;
struct mutex lock;
};
static enum power_supply_property BQ3060_battery_props[] = {
POWER_SUPPLY_PROP_STATUS,
POWER_SUPPLY_PROP_PRESENT,
POWER_SUPPLY_PROP_VOLTAGE_NOW,
POWER_SUPPLY_PROP_CURRENT_NOW,
POWER_SUPPLY_PROP_CAPACITY,
POWER_SUPPLY_PROP_TEMP,
POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
POWER_SUPPLY_PROP_TECHNOLOGY,
POWER_SUPPLY_PROP_CHARGE_FULL,
POWER_SUPPLY_PROP_CHARGE_NOW,
POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
POWER_SUPPLY_PROP_CYCLE_COUNT,
POWER_SUPPLY_PROP_HEALTH,
POWER_SUPPLY_PROP_MANUFACTURER,
};
static unsigned int poll_interval = 360;
module_param(poll_interval, uint, 0644);
MODULE_PARM_DESC(poll_interval,
"battery poll interval in seconds - 0 disables polling");
/*
* Common code for BQ3060 devices
*/
/*
static int BQ3060_battery_i2c_read(struct BQ3060_device_info *di, u8 reg)
{
struct i2c_client *client = to_i2c_client(di->dev);
struct i2c_msg msg[2];
unsigned char data[2];
int ret;
if (!client->adapter)
return -ENODEV;
printk(">>>>>>>>>>> victor BQ3060_battery_i2c_read start \n");
msg[0].addr = client->addr;
msg[0].flags = 0;
msg[0].buf = ®
msg[0].len = sizeof(reg);
msg[1].addr = client->addr;
msg[1].flags = I2C_M_RD;
msg[1].buf = data;
msg[1].len = 2;
ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
if (ret < 0)
return ret;
ret = get_unaligned_le16(data);
printk(">>>>>>>>>>> victor BQ3060_battery_i2c_read end \n");
return ret;
}
*/
static inline int BQ3060_read(struct BQ3060_device_info *di, int reg_index)
{
int ret;
/* Reports EINVAL for invalid/missing registers */
if (!di)
return -EINVAL;
//printk(">>>>>>>>>>>> victor BQ3060_read \n");
ret = i2c_smbus_read_word_data(di->client, reg_index);//i2c_smbus_read_byte_data(di->client, reg_index);
//ret = i2c_smbus_read_byte_data(di->client, reg_index); //BQ3060_battery_i2c_read(di, reg_index);
if (ret < 0)
return ret;
//*data = ret;
return ret;//BQ3060_battery_i2c_read(di, reg_index);
}
/*
* Return the battery State-of-Charge
* Or < 0 if something fails.
*/
static int BQ3060_battery_read_soc(struct BQ3060_device_info *di)
{
int soc;
soc = BQ3060_read(di, BQ3060_REG_RelativeOfCharge);
if (soc < 0)
dev_dbg(di->dev, "error reading State-of-Charge\n");
return soc;
}
/*
* Return a battery charge value in µAh
* Or < 0 if something fails.
*/
static int BQ3060_battery_read_charge(struct BQ3060_device_info *di, u8 reg)
{
int charge;
charge = BQ3060_read(di, reg);
if (charge < 0) {
dev_dbg(di->dev, "error reading charge register %02x: %d\n",
reg, charge);
return charge;
}
charge *= 1000;
//If BatteryMode()[CAPM] = 1, then the data reports in 10 mWh.
return charge;
}
/*
* Return the battery Nominal available capacity in µAh
* Or < 0 if something fails.
*/
static inline int BQ3060_battery_read_nac(struct BQ3060_device_info *di)
{
return BQ3060_battery_read_charge(di, BQ3060_REG_RemainingCapacity);
}
/*
* Return the battery Full Charge Capacity in µAh
* Or < 0 if something fails.
*/
static inline int BQ3060_battery_read_fcc(struct BQ3060_device_info *di)
{
return BQ3060_battery_read_charge(di, BQ3060_REG_FullChargeCapacity);
}
/*
* Return the Design Capacity in µAh
* Or < 0 if something fails.
*/
static int BQ3060_battery_read_dcap(struct BQ3060_device_info *di)
{
int dcap;
dcap = BQ3060_read(di, BQ3060_REG_DesignCapacity);
if (dcap < 0) {
dev_dbg(di->dev, "error reading initial last measured discharge\n");
return dcap;
}
dcap *= 1000;
//If BatteryMode()[CAPM] = 1, then the data reports in 10 mWh.
return dcap;
}
/*
* Return the battery temperature in tenths of degree Kelvin
* Or < 0 if something fails.
*/
static int BQ3060_battery_read_temperature(struct BQ3060_device_info *di)
{
int temp;
temp = BQ3060_read(di, BQ3060_REG_Temperature);
if (temp < 0) {
dev_err(di->dev, "error reading temperature\n");
return temp;
}
return temp;
}
/*
* Return the battery Cycle count total
* Or < 0 if something fails.
*/
static int BQ3060_battery_read_cyct(struct BQ3060_device_info *di)
{
int cyct;
cyct = BQ3060_read(di, BQ3060_REG_CycleCount);
if (cyct < 0)
dev_err(di->dev, "error reading cycle count total\n");
return cyct;
}
/*
* Read a time register.
* Return < 0 if something fails.
*/
static int BQ3060_battery_read_time(struct BQ3060_device_info *di, u8 reg)
{
int tval;
tval = BQ3060_read(di, reg);
if (tval < 0) {
dev_dbg(di->dev, "error reading time register %02x: %d\n",
reg, tval);
return tval;
}
if (tval == 65535)
return -ENODATA;
return tval * 60;
}
/*
* Read flag register.
* Return < 0 if something fails.
*/
static int BQ3060_battery_read_health(struct BQ3060_device_info *di)
{
int flags;
//flags = BQ3060_read(di, BQ27XXX_REG_FLAGS);
flags = di->cache.flags;
if (flags < 0) {
dev_err(di->dev, "error reading flag register:%d\n", flags);
return flags;
}
/* Unlikely but important to return first */
if (flags & BQ3060_FLAG_OTA)
return POWER_SUPPLY_HEALTH_OVERHEAT;
if (flags & BQ3060_FLAG_EC)
return POWER_SUPPLY_HEALTH_DEAD;
if (!(flags & BQ3060_FLAG_INIT))
return POWER_SUPPLY_HEALTH_UNKNOWN;
return POWER_SUPPLY_HEALTH_GOOD;
}
static void BQ3060_battery_update(struct BQ3060_device_info *di)
{
struct BQ3060_reg_cache cache = {0, };
cache.flags = BQ3060_read(di, BQ3060_REG_BatteryStatus);
//printk(">>>>>>>>>>>> victor BQ3060_battery_update cache.flags = %d\n",cache.flags);
if ((cache.flags & BQ3060_FLAG_EC) == BQ3060_FLAG_EC)
cache.flags = -1; /* read error */
if (cache.flags >= 0) {
//printk(">>>>>>>>>>>> victor BQ3060_battery_update \n");
cache.temperature = BQ3060_battery_read_temperature(di);
cache.time_to_empty = BQ3060_battery_read_time(di, BQ3060_REG_RunTimeToEmpty);
cache.time_to_empty_avg = BQ3060_battery_read_time(di, BQ3060_REG_AverageTimeToEmpty);
//cache.time_to_full = BQ3060_battery_read_time(di, BQ3060_REG_AtRateTimeToFull);
cache.time_to_full_avg = BQ3060_battery_read_time(di, BQ3060_REG_AverageTimeToFull);
cache.charge_full = BQ3060_battery_read_fcc(di);
cache.capacity = BQ3060_battery_read_soc(di);
cache.health = BQ3060_battery_read_health(di);
cache.cycle_count = BQ3060_battery_read_cyct(di);
/* We only have to read charge design full once */
if (di->charge_design_full <= 0)
di->charge_design_full = BQ3060_battery_read_dcap(di);
}
if (di->cache.capacity != cache.capacity)
power_supply_changed(di->bat);
if (memcmp(&di->cache, &cache, sizeof(cache)) != 0)
di->cache = cache;
di->last_update = jiffies;
}
static void BQ3060_battery_poll(struct work_struct *work)
{
struct BQ3060_device_info *di =
container_of(work, struct BQ3060_device_info,
work.work);
BQ3060_battery_update(di);
if (poll_interval > 0) {
/* The timer does not have to be accurate. */
set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
schedule_delayed_work(&di->work, poll_interval * HZ);
}
}
/*
* Return the battery average current in µA
* Note that current can be negative signed as well
* Or 0 if something fails.
*/
static int BQ3060_battery_current(struct BQ3060_device_info *di,
union power_supply_propval *val)
{
int curr;
curr = BQ3060_read(di, BQ3060_REG_Current);
if (curr < 0) {
dev_err(di->dev, "error reading current\n");
return curr;
}
/* Other gauges return signed value */
val->intval = (int)((s16)curr) * 1000;
return 0;
}
static int BQ3060_battery_status(struct BQ3060_device_info *di,
union power_supply_propval *val)
{
int status;
if (di->cache.flags & BQ3060_FLAG_FC)
status = POWER_SUPPLY_STATUS_FULL;
else if (!(di->cache.flags & BQ3060_FLAG_DSG))
status = POWER_SUPPLY_STATUS_CHARGING;
else if (power_supply_am_i_supplied(di->bat))
status = POWER_SUPPLY_STATUS_NOT_CHARGING;
else
status = POWER_SUPPLY_STATUS_DISCHARGING;
val->intval = status;
return 0;
}
/*
* Return the battery Voltage in millivolts
* Or < 0 if something fails.
*/
static int BQ3060_battery_voltage(struct BQ3060_device_info *di,
union power_supply_propval *val)
{
int volt;
volt = BQ3060_read(di, BQ3060_REG_Voltage);
if (volt < 0) {
dev_err(di->dev, "error reading voltage\n");
return volt;
}
val->intval = volt;
return 0;
}
static int BQ3060_simple_value(int value,
union power_supply_propval *val)
{
if (value < 0)
return value;
val->intval = value;
return 0;
}
static int BQ3060_battery_get_property(struct power_supply *psy,
enum power_supply_property psp,
union power_supply_propval *val)
{
int ret = 0;
struct BQ3060_device_info *di = power_supply_get_drvdata(psy);
mutex_lock(&di->lock);
if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
cancel_delayed_work_sync(&di->work);
BQ3060_battery_poll(&di->work.work);
}
mutex_unlock(&di->lock);
if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
return -ENODEV;
switch (psp) {
case POWER_SUPPLY_PROP_STATUS:
ret = BQ3060_battery_status(di, val);
break;
case POWER_SUPPLY_PROP_VOLTAGE_NOW:
ret = BQ3060_battery_voltage(di, val);
break;
case POWER_SUPPLY_PROP_PRESENT:
val->intval = di->cache.flags < 0 ? 0 : 1;
break;
case POWER_SUPPLY_PROP_CURRENT_NOW:
ret = BQ3060_battery_current(di, val);
break;
case POWER_SUPPLY_PROP_CAPACITY:
ret = BQ3060_simple_value(di->cache.capacity, val);
break;
case POWER_SUPPLY_PROP_TEMP:
ret = BQ3060_simple_value(di->cache.temperature, val);
if (ret == 0)
val->intval -= 2731; /* convert decidegree k to c */
break;
case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
ret = BQ3060_simple_value(di->cache.time_to_empty, val);
break;
case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
ret = BQ3060_simple_value(di->cache.time_to_empty_avg, val);
break;
case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
ret = BQ3060_simple_value(di->cache.time_to_full, val);
break;
case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
ret = BQ3060_simple_value(di->cache.time_to_full_avg, val);
break;
case POWER_SUPPLY_PROP_TECHNOLOGY:
val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
break;
case POWER_SUPPLY_PROP_CHARGE_NOW:
ret = BQ3060_simple_value(BQ3060_battery_read_nac(di), val);
break;
case POWER_SUPPLY_PROP_CHARGE_FULL:
ret = BQ3060_simple_value(di->cache.charge_full, val);
break;
case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
ret = BQ3060_simple_value(di->charge_design_full, val);
break;
case POWER_SUPPLY_PROP_CYCLE_COUNT:
ret = BQ3060_simple_value(di->cache.cycle_count, val);
break;
case POWER_SUPPLY_PROP_HEALTH:
ret = BQ3060_simple_value(di->cache.health, val);
break;
case POWER_SUPPLY_PROP_MANUFACTURER:
val->strval = BQ3060_MANUFACTURER;
break;
default:
return -EINVAL;
}
return ret;
}
static void BQ3060_external_power_changed(struct power_supply *psy)
{
struct BQ3060_device_info *di = power_supply_get_drvdata(psy);
cancel_delayed_work_sync(&di->work);
schedule_delayed_work(&di->work, 0);
}
static int __maybe_unused BQ3060_powersupply_init(struct BQ3060_device_info *di,
const char *name)
{
int ret;
struct power_supply_desc *psy_desc;
struct power_supply_config psy_cfg = { .drv_data = di, };
psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL);
if (!psy_desc)
return -ENOMEM;
psy_desc->name = name;
psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
psy_desc->properties = BQ3060_battery_props;
psy_desc->num_properties = ARRAY_SIZE(BQ3060_battery_props);
psy_desc->get_property = BQ3060_battery_get_property;
psy_desc->external_power_changed = BQ3060_external_power_changed;
INIT_DELAYED_WORK(&di->work, BQ3060_battery_poll);
mutex_init(&di->lock);
di->bat = power_supply_register_no_ws(di->dev, psy_desc, &psy_cfg);
if (IS_ERR(di->bat)) {
ret = PTR_ERR(di->bat);
dev_err(di->dev, "failed to register battery: %d\n", ret);
return ret;
}
dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
BQ3060_battery_update(di);
return 0;
}
static void __maybe_unused BQ3060_powersupply_unregister(struct BQ3060_device_info *di)
{
/*
* power_supply_unregister call BQ3060_battery_get_property which
* call BQ3060_battery_poll.
* Make sure that BQ3060_battery_poll will not call
* schedule_delayed_work again after unregister (which cause OOPS).
*/
poll_interval = 0;
cancel_delayed_work_sync(&di->work);
power_supply_unregister(di->bat);
mutex_destroy(&di->lock);
}
/* If the system has several batteries we need a different name for each
* of them...
*/
static DEFINE_IDR(battery_id);
static DEFINE_MUTEX(battery_mutex);
static int BQ3060_battery_i2c_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
char *name;
struct BQ3060_device_info *di;
int num;
int retval = 0;
printk(">>>> victor BQ3060_battery_i2c_probe \n");
/* Get new ID for the new battery device */
mutex_lock(&battery_mutex);
num = idr_alloc(&battery_id, client, 0, 0, GFP_KERNEL);
mutex_unlock(&battery_mutex);
if (num < 0)
return num;
name = devm_kasprintf(&client->dev, GFP_KERNEL, "%s-%d", id->name, num);
if (!name) {
retval = -ENOMEM;
goto batt_failed;
}
di = devm_kzalloc(&client->dev, sizeof(*di), GFP_KERNEL);
if (!di) {
retval = -ENOMEM;
goto batt_failed;
}
di->client = client;
di->id = num;
di->dev = &client->dev;
//di->chip = id->driver_data;
i2c_set_clientdata(client, di);
retval = BQ3060_powersupply_init(di, name);
if (retval)
goto batt_failed;
/* Schedule a polling after about 1 min */
schedule_delayed_work(&di->work, 60 * HZ);
return 0;
batt_failed:
mutex_lock(&battery_mutex);
idr_remove(&battery_id, num);
mutex_unlock(&battery_mutex);
return retval;
}
static int BQ3060_battery_i2c_remove(struct i2c_client *client)
{
struct BQ3060_device_info *di = i2c_get_clientdata(client);
BQ3060_powersupply_unregister(di);
mutex_lock(&battery_mutex);
idr_remove(&battery_id, di->id);
mutex_unlock(&battery_mutex);
return 0;
}
static const struct i2c_device_id BQ3060_id[] = {
{ "BQ3060", 1 },
{},
};
MODULE_DEVICE_TABLE(i2c, BQ3060_id);
static struct i2c_driver BQ3060_battery_i2c_driver = {
.driver = {
.name = "BQ3060-battery",
},
.probe = BQ3060_battery_i2c_probe,
.remove = BQ3060_battery_i2c_remove,
.id_table = BQ3060_id,
};
static inline int BQ3060_battery_i2c_init(void)
{
int ret = i2c_add_driver(&BQ3060_battery_i2c_driver);
if (ret)
pr_err("Unable to register BQ3060 i2c driver\n");
return ret;
}
static inline void BQ3060_battery_i2c_exit(void)
{
i2c_del_driver(&BQ3060_battery_i2c_driver);
}
/*
* Module stuff
*/
static int __init BQ3060_battery_init(void)
{
int ret;
ret = BQ3060_battery_i2c_init();
if (ret)
return ret;
return ret;
}
module_init(BQ3060_battery_init);
static void __exit BQ3060_battery_exit(void)
{
BQ3060_battery_i2c_exit();
}
module_exit(BQ3060_battery_exit);
MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
MODULE_DESCRIPTION("BQ3060 battery monitor driver");
MODULE_LICENSE("GPL");