Skip to content

Commit ccce440

Browse files
Liam Brecksre
authored andcommitted
power: supply: bq27xxx: Add power_supply_battery_info support
Previously there was no way to configure these chips in the event that the defaults didn't match the battery in question. For chips with RAM data memory (and also those with flash/NVM data memory if CONFIG_BATTERY_BQ27XXX_DT_UPDATES_NVM is defined and the user has not set module param dt_monitored_battery_updates_nvm=0) we now call power_supply_get_battery_info(), check its values, and write battery properties to chip data memory if there is a dm_regs table for the chip. Signed-off-by: Matt Ranostay <matt@ranostay.consulting> Signed-off-by: Liam Breck <kernel@networkimprov.net> Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.co.uk>
1 parent 0670c9b commit ccce440

3 files changed

Lines changed: 216 additions & 1 deletion

File tree

‎drivers/power/supply/Kconfig‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,17 @@ config BATTERY_BQ27XXX_I2C
198198
Say Y here to enable support for batteries with BQ27xxx chips
199199
connected over an I2C bus.
200200

201+
config BATTERY_BQ27XXX_DT_UPDATES_NVM
202+
bool "BQ27xxx support for update of NVM/flash data memory"
203+
depends on BATTERY_BQ27XXX_I2C
204+
help
205+
Say Y here to enable devicetree monitored-battery config to update
206+
NVM/flash data memory. Only enable this option for devices with a
207+
fuel gauge mounted on the circuit board, and a battery that cannot
208+
easily be replaced with one of a different type. Not for
209+
general-purpose kernels, as this can cause misconfiguration of a
210+
smart battery with embedded NVM/flash.
211+
201212
config BATTERY_DA9030
202213
tristate "DA9030 battery driver"
203214
depends on PMIC_DA903X

‎drivers/power/supply/bq27xxx_battery.c‎

Lines changed: 203 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,13 @@ static LIST_HEAD(bq27xxx_battery_devices);
805805

806806
#define BQ27XXX_DM_SZ 32
807807

808+
struct bq27xxx_dm_reg {
809+
u8 subclass_id;
810+
u8 offset;
811+
u8 bytes;
812+
u16 min, max;
813+
};
814+
808815
/**
809816
* struct bq27xxx_dm_buf - chip data memory buffer
810817
* @class: data memory subclass_id
@@ -822,6 +829,44 @@ struct bq27xxx_dm_buf {
822829
bool has_data, dirty;
823830
};
824831

832+
#define BQ27XXX_DM_BUF(di, i) { \
833+
.class = (di)->dm_regs[i].subclass_id, \
834+
.block = (di)->dm_regs[i].offset / BQ27XXX_DM_SZ, \
835+
}
836+
837+
static inline u16 *bq27xxx_dm_reg_ptr(struct bq27xxx_dm_buf *buf,
838+
struct bq27xxx_dm_reg *reg)
839+
{
840+
if (buf->class == reg->subclass_id &&
841+
buf->block == reg->offset / BQ27XXX_DM_SZ)
842+
return (u16 *) (buf->data + reg->offset % BQ27XXX_DM_SZ);
843+
844+
return NULL;
845+
}
846+
847+
enum bq27xxx_dm_reg_id {
848+
BQ27XXX_DM_DESIGN_CAPACITY = 0,
849+
BQ27XXX_DM_DESIGN_ENERGY,
850+
BQ27XXX_DM_TERMINATE_VOLTAGE,
851+
};
852+
853+
static const char * const bq27xxx_dm_reg_name[] = {
854+
[BQ27XXX_DM_DESIGN_CAPACITY] = "design-capacity",
855+
[BQ27XXX_DM_DESIGN_ENERGY] = "design-energy",
856+
[BQ27XXX_DM_TERMINATE_VOLTAGE] = "terminate-voltage",
857+
};
858+
859+
860+
static bool bq27xxx_dt_to_nvm = true;
861+
module_param_named(dt_monitored_battery_updates_nvm, bq27xxx_dt_to_nvm, bool, 0444);
862+
MODULE_PARM_DESC(dt_monitored_battery_updates_nvm,
863+
"Devicetree monitored-battery config updates data memory on NVM/flash chips.\n"
864+
"Users must set this =0 when installing a different type of battery!\n"
865+
"Default is =1."
866+
#ifndef CONFIG_BATTERY_BQ27XXX_DT_UPDATES_NVM
867+
"\nSetting this affects future kernel updates, not the current configuration."
868+
#endif
869+
);
825870

826871
static int poll_interval_param_set(const char *val, const struct kernel_param *kp)
827872
{
@@ -1019,6 +1064,55 @@ static int bq27xxx_battery_read_dm_block(struct bq27xxx_device_info *di,
10191064
return ret;
10201065
}
10211066

1067+
static void bq27xxx_battery_update_dm_block(struct bq27xxx_device_info *di,
1068+
struct bq27xxx_dm_buf *buf,
1069+
enum bq27xxx_dm_reg_id reg_id,
1070+
unsigned int val)
1071+
{
1072+
struct bq27xxx_dm_reg *reg = &di->dm_regs[reg_id];
1073+
const char *str = bq27xxx_dm_reg_name[reg_id];
1074+
u16 *prev = bq27xxx_dm_reg_ptr(buf, reg);
1075+
1076+
if (prev == NULL) {
1077+
dev_warn(di->dev, "buffer does not match %s dm spec\n", str);
1078+
return;
1079+
}
1080+
1081+
if (reg->bytes != 2) {
1082+
dev_warn(di->dev, "%s dm spec has unsupported byte size\n", str);
1083+
return;
1084+
}
1085+
1086+
if (!buf->has_data)
1087+
return;
1088+
1089+
if (be16_to_cpup(prev) == val) {
1090+
dev_info(di->dev, "%s has %u\n", str, val);
1091+
return;
1092+
}
1093+
1094+
#ifdef CONFIG_BATTERY_BQ27XXX_DT_UPDATES_NVM
1095+
if (!di->ram_chip && !bq27xxx_dt_to_nvm) {
1096+
#else
1097+
if (!di->ram_chip) {
1098+
#endif
1099+
/* devicetree and NVM differ; defer to NVM */
1100+
dev_warn(di->dev, "%s has %u; update to %u disallowed "
1101+
#ifdef CONFIG_BATTERY_BQ27XXX_DT_UPDATES_NVM
1102+
"by dt_monitored_battery_updates_nvm=0"
1103+
#else
1104+
"for flash/NVM data memory"
1105+
#endif
1106+
"\n", str, be16_to_cpup(prev), val);
1107+
return;
1108+
}
1109+
1110+
dev_info(di->dev, "update %s to %u\n", str, val);
1111+
1112+
*prev = cpu_to_be16(val);
1113+
buf->dirty = true;
1114+
}
1115+
10221116
static int bq27xxx_battery_cfgupdate_priv(struct bq27xxx_device_info *di, bool active)
10231117
{
10241118
const int limit = 100;
@@ -1129,6 +1223,103 @@ static int bq27xxx_battery_write_dm_block(struct bq27xxx_device_info *di,
11291223
return ret;
11301224
}
11311225

1226+
static void bq27xxx_battery_set_config(struct bq27xxx_device_info *di,
1227+
struct power_supply_battery_info *info)
1228+
{
1229+
struct bq27xxx_dm_buf bd = BQ27XXX_DM_BUF(di, BQ27XXX_DM_DESIGN_CAPACITY);
1230+
struct bq27xxx_dm_buf bt = BQ27XXX_DM_BUF(di, BQ27XXX_DM_TERMINATE_VOLTAGE);
1231+
bool updated;
1232+
1233+
if (bq27xxx_battery_unseal(di) < 0)
1234+
return;
1235+
1236+
if (info->charge_full_design_uah != -EINVAL &&
1237+
info->energy_full_design_uwh != -EINVAL) {
1238+
bq27xxx_battery_read_dm_block(di, &bd);
1239+
/* assume design energy & capacity are in same block */
1240+
bq27xxx_battery_update_dm_block(di, &bd,
1241+
BQ27XXX_DM_DESIGN_CAPACITY,
1242+
info->charge_full_design_uah / 1000);
1243+
bq27xxx_battery_update_dm_block(di, &bd,
1244+
BQ27XXX_DM_DESIGN_ENERGY,
1245+
info->energy_full_design_uwh / 1000);
1246+
}
1247+
1248+
if (info->voltage_min_design_uv != -EINVAL) {
1249+
bool same = bd.class == bt.class && bd.block == bt.block;
1250+
if (!same)
1251+
bq27xxx_battery_read_dm_block(di, &bt);
1252+
bq27xxx_battery_update_dm_block(di, same ? &bd : &bt,
1253+
BQ27XXX_DM_TERMINATE_VOLTAGE,
1254+
info->voltage_min_design_uv / 1000);
1255+
}
1256+
1257+
updated = bd.dirty || bt.dirty;
1258+
1259+
bq27xxx_battery_write_dm_block(di, &bd);
1260+
bq27xxx_battery_write_dm_block(di, &bt);
1261+
1262+
bq27xxx_battery_seal(di);
1263+
1264+
if (updated && di->chip != BQ27421) { /* not a cfgupdate chip, so reset */
1265+
bq27xxx_write(di, BQ27XXX_REG_CTRL, BQ27XXX_RESET, false);
1266+
BQ27XXX_MSLEEP(300); /* reset time is not documented */
1267+
}
1268+
/* assume bq27xxx_battery_update() is called hereafter */
1269+
}
1270+
1271+
static void bq27xxx_battery_settings(struct bq27xxx_device_info *di)
1272+
{
1273+
struct power_supply_battery_info info = {};
1274+
unsigned int min, max;
1275+
1276+
if (power_supply_get_battery_info(di->bat, &info) < 0)
1277+
return;
1278+
1279+
if (!di->dm_regs) {
1280+
dev_warn(di->dev, "data memory update not supported for chip\n");
1281+
return;
1282+
}
1283+
1284+
if (info.energy_full_design_uwh != info.charge_full_design_uah) {
1285+
if (info.energy_full_design_uwh == -EINVAL)
1286+
dev_warn(di->dev, "missing battery:energy-full-design-microwatt-hours\n");
1287+
else if (info.charge_full_design_uah == -EINVAL)
1288+
dev_warn(di->dev, "missing battery:charge-full-design-microamp-hours\n");
1289+
}
1290+
1291+
/* assume min == 0 */
1292+
max = di->dm_regs[BQ27XXX_DM_DESIGN_ENERGY].max;
1293+
if (info.energy_full_design_uwh > max * 1000) {
1294+
dev_err(di->dev, "invalid battery:energy-full-design-microwatt-hours %d\n",
1295+
info.energy_full_design_uwh);
1296+
info.energy_full_design_uwh = -EINVAL;
1297+
}
1298+
1299+
/* assume min == 0 */
1300+
max = di->dm_regs[BQ27XXX_DM_DESIGN_CAPACITY].max;
1301+
if (info.charge_full_design_uah > max * 1000) {
1302+
dev_err(di->dev, "invalid battery:charge-full-design-microamp-hours %d\n",
1303+
info.charge_full_design_uah);
1304+
info.charge_full_design_uah = -EINVAL;
1305+
}
1306+
1307+
min = di->dm_regs[BQ27XXX_DM_TERMINATE_VOLTAGE].min;
1308+
max = di->dm_regs[BQ27XXX_DM_TERMINATE_VOLTAGE].max;
1309+
if ((info.voltage_min_design_uv < min * 1000 ||
1310+
info.voltage_min_design_uv > max * 1000) &&
1311+
info.voltage_min_design_uv != -EINVAL) {
1312+
dev_err(di->dev, "invalid battery:voltage-min-design-microvolt %d\n",
1313+
info.voltage_min_design_uv);
1314+
info.voltage_min_design_uv = -EINVAL;
1315+
}
1316+
1317+
if ((info.energy_full_design_uwh != -EINVAL &&
1318+
info.charge_full_design_uah != -EINVAL) ||
1319+
info.voltage_min_design_uv != -EINVAL)
1320+
bq27xxx_battery_set_config(di, &info);
1321+
}
1322+
11321323
/*
11331324
* Return the battery State-of-Charge
11341325
* Or < 0 if something fails.
@@ -1646,6 +1837,13 @@ static int bq27xxx_battery_get_property(struct power_supply *psy,
16461837
case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
16471838
ret = bq27xxx_simple_value(di->charge_design_full, val);
16481839
break;
1840+
/*
1841+
* TODO: Implement these to make registers set from
1842+
* power_supply_battery_info visible in sysfs.
1843+
*/
1844+
case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
1845+
case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
1846+
return -EINVAL;
16491847
case POWER_SUPPLY_PROP_CYCLE_COUNT:
16501848
ret = bq27xxx_simple_value(di->cache.cycle_count, val);
16511849
break;
@@ -1679,7 +1877,10 @@ static void bq27xxx_external_power_changed(struct power_supply *psy)
16791877
int bq27xxx_battery_setup(struct bq27xxx_device_info *di)
16801878
{
16811879
struct power_supply_desc *psy_desc;
1682-
struct power_supply_config psy_cfg = { .drv_data = di, };
1880+
struct power_supply_config psy_cfg = {
1881+
.of_node = di->dev->of_node,
1882+
.drv_data = di,
1883+
};
16831884

16841885
INIT_DELAYED_WORK(&di->work, bq27xxx_battery_poll);
16851886
mutex_init(&di->lock);
@@ -1704,6 +1905,7 @@ int bq27xxx_battery_setup(struct bq27xxx_device_info *di)
17041905

17051906
dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
17061907

1908+
bq27xxx_battery_settings(di);
17071909
bq27xxx_battery_update(di);
17081910

17091911
mutex_lock(&bq27xxx_list_lock);

‎include/linux/power/bq27xxx_battery.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ struct bq27xxx_device_info {
6363
struct device *dev;
6464
int id;
6565
enum bq27xxx_chip chip;
66+
bool ram_chip;
6667
const char *name;
68+
struct bq27xxx_dm_reg *dm_regs;
6769
u32 unseal_key;
6870
struct bq27xxx_access_methods bus;
6971
struct bq27xxx_reg_cache cache;

0 commit comments

Comments
 (0)