rk3399主控+bq4050充电管理芯片,驱动,配置

主控:rk3399

系统:android7.1

内核版本:4.4.126

dts配置:

&i2c4 {
    status = "okay";
    clock-frequency = <80000>;
    bq3060batter:battery@0b {
            compatible = "bq4050";
            reg = <0x0b>;
    };
};

内核驱动:

bq4050_battery.c

/*

 * BQ4050 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/BQ4050

 */

#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 BQ4050_MANUFACTURER "Texas Instruments"

/* BQ4050 regs */

//#define BQ4050_REG_AtRateTimeToFull 0x05

//#define BQ4050_REG_AtRateTimeToEmpty 0x06

#define BQ4050_REG_Temperature 0x08

#define BQ4050_REG_Voltage 0x09

#define BQ4050_REG_Current 0x0A

#define BQ4050_REG_AverageCurrent 0x0B

#define BQ4050_REG_RelativeOfCharge 0x0D

#define BQ4050_REG_RemainingCapacity 0x0F

#define BQ4050_REG_FullChargeCapacity 0x10

#define BQ4050_REG_RunTimeToEmpty 0x11

#define BQ4050_REG_AverageTimeToEmpty 0x12

#define BQ4050_REG_AverageTimeToFull 0x13

#define BQ4050_REG_BatteryStatus 0x16

#define BQ4050_REG_CycleCount 0x17

#define BQ4050_REG_DesignCapacity 0x18

#define BQ4050_REG_DesignVoltage 0x19

#define BQ4050_REG_SerialNumber 0x1C

/* BQ27XXX Flags */

#define BQ4050_FLAG_EC (BIT(0)|BIT(1)|BIT(2)|BIT(3))

#define BQ4050_FLAG_FD BIT(4)

#define BQ4050_FLAG_FC BIT(5)

#define BQ4050_FLAG_DSG BIT(6)

#define BQ4050_FLAG_INIT BIT(7)

#define BQ4050_FLAG_TDA BIT(11)

#define BQ4050_FLAG_OTA BIT(12)

#define BQ4050_FLAG_TCA BIT(14)

#define BQ4050_FLAG_OCA BIT(15)

struct bq4050_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 bq4050_device_info {

struct device *dev;

int id;

struct bq4050_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 bq4050_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 BQ4050 devices

 */

static int bq4050_battery_i2c_read(struct bq4050_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;

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);

return ret;

}

static inline int bq4050_read(struct bq4050_device_info *di, int reg_index)

{

/* Reports EINVAL for invalid/missing registers */

if (!di)

return -EINVAL;

return bq4050_battery_i2c_read(di, reg_index);

}

/*

 * Return the battery State-of-Charge

 * Or < 0 if something fails.

 */

static int bq4050_battery_read_soc(struct bq4050_device_info *di)

{

int soc;

soc = bq4050_read(di, BQ4050_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 bq4050_battery_read_charge(struct bq4050_device_info *di, u8 reg)

{

int charge;

charge = bq4050_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 bq4050_battery_read_nac(struct bq4050_device_info *di)

{

return bq4050_battery_read_charge(di, BQ4050_REG_RemainingCapacity);

}

/*

 * Return the battery Full Charge Capacity in µAh

 * Or < 0 if something fails.

 */

static inline int bq4050_battery_read_fcc(struct bq4050_device_info *di)

{

return bq4050_battery_read_charge(di, BQ4050_REG_FullChargeCapacity);

}

/*

 * Return the Design Capacity in µAh

 * Or < 0 if something fails.

 */

static int bq4050_battery_read_dcap(struct bq4050_device_info *di)

{

int dcap;

dcap = bq4050_read(di, BQ4050_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 bq4050_battery_read_temperature(struct bq4050_device_info *di)

{

int temp;

temp = bq4050_read(di, BQ4050_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 bq4050_battery_read_cyct(struct bq4050_device_info *di)

{

int cyct;

cyct = bq4050_read(di, BQ4050_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 bq4050_battery_read_time(struct bq4050_device_info *di, u8 reg)

{

int tval;

tval = bq4050_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 bq4050_battery_read_health(struct bq4050_device_info *di)

{

int flags;

//flags = bq4050_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 & BQ4050_FLAG_OTA)

return POWER_SUPPLY_HEALTH_OVERHEAT;

if (flags & BQ4050_FLAG_EC)

return POWER_SUPPLY_HEALTH_DEAD;

if (!(flags & BQ4050_FLAG_INIT))

return POWER_SUPPLY_HEALTH_UNKNOWN;

return POWER_SUPPLY_HEALTH_GOOD;

}

static void bq4050_battery_update(struct bq4050_device_info *di)

{

struct bq4050_reg_cache cache = {0, };

cache.flags = bq4050_read(di, BQ4050_REG_BatteryStatus);

if ((cache.flags & BQ4050_FLAG_EC) == BQ4050_FLAG_EC)

cache.flags = -1; /* read error */

if (cache.flags >= 0) {

cache.temperature = bq4050_battery_read_temperature(di);

cache.time_to_empty = bq4050_battery_read_time(di, BQ4050_REG_RunTimeToEmpty);

cache.time_to_empty_avg = bq4050_battery_read_time(di, BQ4050_REG_AverageTimeToEmpty);

//cache.time_to_full = bq4050_battery_read_time(di, BQ4050_REG_AtRateTimeToFull);

cache.time_to_full_avg = bq4050_battery_read_time(di, BQ4050_REG_AverageTimeToFull);

cache.charge_full = bq4050_battery_read_fcc(di);

cache.capacity = bq4050_battery_read_soc(di);

cache.health = bq4050_battery_read_health(di);

cache.cycle_count = bq4050_battery_read_cyct(di);

/* We only have to read charge design full once */

if (di->charge_design_full <= 0)

di->charge_design_full = bq4050_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 bq4050_battery_poll(struct work_struct *work)

{

struct bq4050_device_info *di =

container_of(work, struct bq4050_device_info,

    work.work);

bq4050_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 bq4050_battery_current(struct bq4050_device_info *di,

  union power_supply_propval *val)

{

int curr;

curr = bq4050_read(di, BQ4050_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 bq4050_battery_status(struct bq4050_device_info *di,

 union power_supply_propval *val)

{

int status;

if (di->cache.flags & BQ4050_FLAG_FC)

status = POWER_SUPPLY_STATUS_FULL;

else if (!(di->cache.flags & BQ4050_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 bq4050_battery_voltage(struct bq4050_device_info *di,

  union power_supply_propval *val)

{

int volt;

volt = bq4050_read(di, BQ4050_REG_Voltage);

if (volt < 0) {

dev_err(di->dev, "error reading voltage\n");

return volt;

}

val->intval = volt;

return 0;

}

static int bq4050_simple_value(int value,

union power_supply_propval *val)

{

if (value < 0)

return value;

val->intval = value;

return 0;

}

static int bq4050_battery_get_property(struct power_supply *psy,

enum power_supply_property psp,

union power_supply_propval *val)

{

int ret = 0;

struct bq4050_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);

bq4050_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 = bq4050_battery_status(di, val);

break;

case POWER_SUPPLY_PROP_VOLTAGE_NOW:

ret = bq4050_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 = bq4050_battery_current(di, val);

break;

case POWER_SUPPLY_PROP_CAPACITY:

ret = bq4050_simple_value(di->cache.capacity, val);

break;

case POWER_SUPPLY_PROP_TEMP:

ret = bq4050_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 = bq4050_simple_value(di->cache.time_to_empty, val);

break;

case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:

ret = bq4050_simple_value(di->cache.time_to_empty_avg, val);

break;

case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:

ret = bq4050_simple_value(di->cache.time_to_full, val);

break;

case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:

ret = bq4050_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 = bq4050_simple_value(bq4050_battery_read_nac(di), val);

break;

case POWER_SUPPLY_PROP_CHARGE_FULL:

ret = bq4050_simple_value(di->cache.charge_full, val);

break;

case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:

ret = bq4050_simple_value(di->charge_design_full, val);

break;

case POWER_SUPPLY_PROP_CYCLE_COUNT:

ret = bq4050_simple_value(di->cache.cycle_count, val);

break;

case POWER_SUPPLY_PROP_HEALTH:

ret = bq4050_simple_value(di->cache.health, val);

break;

case POWER_SUPPLY_PROP_MANUFACTURER:

val->strval = BQ4050_MANUFACTURER;

break;

default:

return -EINVAL;

}

return ret;

}

static void bq4050_external_power_changed(struct power_supply *psy)

{

struct bq4050_device_info *di = power_supply_get_drvdata(psy);

cancel_delayed_work_sync(&di->work);

schedule_delayed_work(&di->work, 0);

}

static int __maybe_unused bq4050_powersupply_init(struct bq4050_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 = bq4050_battery_props;

psy_desc->num_properties = ARRAY_SIZE(bq4050_battery_props);

psy_desc->get_property = bq4050_battery_get_property;

psy_desc->external_power_changed = bq4050_external_power_changed;

INIT_DELAYED_WORK(&di->work, bq4050_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);

bq4050_battery_update(di);

return 0;

}

static void __maybe_unused bq4050_powersupply_unregister(struct bq4050_device_info *di)

{

/*

* power_supply_unregister call bq4050_battery_get_property which

* call bq4050_battery_poll.

* Make sure that bq4050_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 bq4050_battery_i2c_probe(struct i2c_client *client,

    const struct i2c_device_id *id)

{

char *name;

struct bq4050_device_info *di;

int num;

int retval = 0;

/* 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->id = num;

di->dev = &client->dev;

//di->chip = id->driver_data;

retval = bq4050_powersupply_init(di, name);

if (retval)

goto batt_failed;

/* Schedule a polling after about 1 min */

schedule_delayed_work(&di->work, 60 * HZ);

i2c_set_clientdata(client, di);

return 0;

batt_failed:

mutex_lock(&battery_mutex);

idr_remove(&battery_id, num);

mutex_unlock(&battery_mutex);

return retval;

}

static int bq4050_battery_i2c_remove(struct i2c_client *client)

{

struct bq4050_device_info *di = i2c_get_clientdata(client);

bq4050_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 bq4050_id[] = {

{ "bq4050", 1 },

{},

};

MODULE_DEVICE_TABLE(i2c, bq4050_id);

static struct i2c_driver bq4050_battery_i2c_driver = {

.driver = {

.name = "bq4050-battery",

},

.probe = bq4050_battery_i2c_probe,

.remove = bq4050_battery_i2c_remove,

.id_table = bq4050_id,

};

static inline int bq4050_battery_i2c_init(void)

{

int ret = i2c_add_driver(&bq4050_battery_i2c_driver);

if (ret)

pr_err("Unable to register BQ4050 i2c driver\n");

return ret;

}

static inline void bq4050_battery_i2c_exit(void)

{

i2c_del_driver(&bq4050_battery_i2c_driver);

}

/*

 * Module stuff

 */

static int __init bq4050_battery_init(void)

{

int ret;

ret = bq4050_battery_i2c_init();

if (ret)

return ret;

return ret;

}

module_init(bq4050_battery_init);

static void __exit bq4050_battery_exit(void)

{

bq4050_battery_i2c_exit();

}

module_exit(bq4050_battery_exit);

MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");

MODULE_DESCRIPTION("BQ4050 battery monitor driver");

MODULE_LICENSE("GPL");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值