Lines Matching +full:charge +full:- +full:current +full:- +full:limit +full:- +full:mapping
1 // SPDX-License-Identifier: GPL-2.0
7 * Copyright (C) 2010-2011 Lars-Peter Clausen <lars@metafoo.de>
19 * https://www.ti.com/product/bq27510-g1
20 * https://www.ti.com/product/bq27510-g2
21 * https://www.ti.com/product/bq27510-g3
22 * https://www.ti.com/product/bq27520-g1
23 * https://www.ti.com/product/bq27520-g2
24 * https://www.ti.com/product/bq27520-g3
25 * https://www.ti.com/product/bq27520-g4
26 * https://www.ti.com/product/bq27530-g1
27 * https://www.ti.com/product/bq27531-g1
28 * https://www.ti.com/product/bq27541-g1
29 * https://www.ti.com/product/bq27542-g1
30 * https://www.ti.com/product/bq27546-g1
31 * https://www.ti.com/product/bq27742-g1
32 * https://www.ti.com/product/bq27545-g1
33 * https://www.ti.com/product/bq27421-g1
34 * https://www.ti.com/product/bq27425-g1
36 * https://www.ti.com/product/bq27411-g1
37 * https://www.ti.com/product/bq27441-g1
38 * https://www.ti.com/product/bq27621-g1
41 * https://www.ti.com/product/bq34z100-g1
62 #define BQ27XXX_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
63 #define BQ27XXX_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
72 #define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */
73 #define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */
76 #define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
96 * bq27xxx_reg_index - Register names
98 * These are indexes into a device's register mapping array.
106 BQ27XXX_REG_AI, /* Average Current */
108 BQ27XXX_REG_TTE, /* Time-to-Empty */
109 BQ27XXX_REG_TTF, /* Time-to-Full */
110 BQ27XXX_REG_TTES, /* Time-to-Empty Standby */
111 BQ27XXX_REG_TTECP, /* Time-to-Empty at Constant Power */
113 BQ27XXX_REG_FCC, /* Full Charge Capacity */
116 BQ27XXX_REG_SOC, /* State-of-Charge */
961 * struct bq27xxx_dm_buf - chip data memory buffer
978 .class = (di)->dm_regs[i].subclass_id, \
979 .block = (di)->dm_regs[i].offset / BQ27XXX_DM_SZ, \
985 if (buf->class == reg->subclass_id && in bq27xxx_dm_reg_ptr()
986 buf->block == reg->offset / BQ27XXX_DM_SZ) in bq27xxx_dm_reg_ptr()
987 return (u16 *) (buf->data + reg->offset % BQ27XXX_DM_SZ); in bq27xxx_dm_reg_ptr()
993 [BQ27XXX_DM_DESIGN_CAPACITY] = "design-capacity",
994 [BQ27XXX_DM_DESIGN_ENERGY] = "design-energy",
995 [BQ27XXX_DM_TERMINATE_VOLTAGE] = "terminate-voltage",
1002 "Devicetree monitored-battery config updates data memory on NVM/flash chips.\n"
1006 "\nSetting this affects future kernel updates, not the current configuration."
1013 unsigned int prev_val = *(unsigned int *) kp->arg; in poll_interval_param_set()
1017 if (ret < 0 || prev_val == *(unsigned int *) kp->arg) in poll_interval_param_set()
1022 cancel_delayed_work_sync(&di->work); in poll_interval_param_set()
1023 schedule_delayed_work(&di->work, 0); in poll_interval_param_set()
1038 "battery poll interval in seconds - 0 disables polling");
1049 if (!di || di->regs[reg_index] == INVALID_REG_ADDR) in bq27xxx_read()
1050 return -EINVAL; in bq27xxx_read()
1052 ret = di->bus.read(di, di->regs[reg_index], single); in bq27xxx_read()
1054 dev_dbg(di->dev, "failed to read register 0x%02x (index %d)\n", in bq27xxx_read()
1055 di->regs[reg_index], reg_index); in bq27xxx_read()
1065 if (!di || di->regs[reg_index] == INVALID_REG_ADDR) in bq27xxx_write()
1066 return -EINVAL; in bq27xxx_write()
1068 if (!di->bus.write) in bq27xxx_write()
1069 return -EPERM; in bq27xxx_write()
1071 ret = di->bus.write(di, di->regs[reg_index], value, single); in bq27xxx_write()
1073 dev_dbg(di->dev, "failed to write register 0x%02x (index %d)\n", in bq27xxx_write()
1074 di->regs[reg_index], reg_index); in bq27xxx_write()
1084 if (!di || di->regs[reg_index] == INVALID_REG_ADDR) in bq27xxx_read_block()
1085 return -EINVAL; in bq27xxx_read_block()
1087 if (!di->bus.read_bulk) in bq27xxx_read_block()
1088 return -EPERM; in bq27xxx_read_block()
1090 ret = di->bus.read_bulk(di, di->regs[reg_index], data, len); in bq27xxx_read_block()
1092 dev_dbg(di->dev, "failed to read_bulk register 0x%02x (index %d)\n", in bq27xxx_read_block()
1093 di->regs[reg_index], reg_index); in bq27xxx_read_block()
1103 if (!di || di->regs[reg_index] == INVALID_REG_ADDR) in bq27xxx_write_block()
1104 return -EINVAL; in bq27xxx_write_block()
1106 if (!di->bus.write_bulk) in bq27xxx_write_block()
1107 return -EPERM; in bq27xxx_write_block()
1109 ret = di->bus.write_bulk(di, di->regs[reg_index], data, len); in bq27xxx_write_block()
1111 dev_dbg(di->dev, "failed to write_bulk register 0x%02x (index %d)\n", in bq27xxx_write_block()
1112 di->regs[reg_index], reg_index); in bq27xxx_write_block()
1123 dev_err(di->dev, "bus error on seal: %d\n", ret); in bq27xxx_battery_seal()
1134 if (di->unseal_key == 0) { in bq27xxx_battery_unseal()
1135 dev_err(di->dev, "unseal failed due to missing key\n"); in bq27xxx_battery_unseal()
1136 return -EINVAL; in bq27xxx_battery_unseal()
1139 ret = bq27xxx_write(di, BQ27XXX_REG_CTRL, (u16)(di->unseal_key >> 16), false); in bq27xxx_battery_unseal()
1143 ret = bq27xxx_write(di, BQ27XXX_REG_CTRL, (u16)di->unseal_key, false); in bq27xxx_battery_unseal()
1150 dev_err(di->dev, "bus error on unseal: %d\n", ret); in bq27xxx_battery_unseal()
1160 sum += buf->data[i]; in bq27xxx_battery_checksum_dm_block()
1163 return 0xff - sum; in bq27xxx_battery_checksum_dm_block()
1171 buf->has_data = false; in bq27xxx_battery_read_dm_block()
1173 ret = bq27xxx_write(di, BQ27XXX_DM_CLASS, buf->class, true); in bq27xxx_battery_read_dm_block()
1177 ret = bq27xxx_write(di, BQ27XXX_DM_BLOCK, buf->block, true); in bq27xxx_battery_read_dm_block()
1183 ret = bq27xxx_read_block(di, BQ27XXX_DM_DATA, buf->data, BQ27XXX_DM_SZ); in bq27xxx_battery_read_dm_block()
1192 ret = -EINVAL; in bq27xxx_battery_read_dm_block()
1196 buf->has_data = true; in bq27xxx_battery_read_dm_block()
1197 buf->dirty = false; in bq27xxx_battery_read_dm_block()
1202 dev_err(di->dev, "bus error reading chip memory: %d\n", ret); in bq27xxx_battery_read_dm_block()
1211 struct bq27xxx_dm_reg *reg = &di->dm_regs[reg_id];
1216 dev_warn(di->dev, "buffer does not match %s dm spec\n", str);
1220 if (reg->bytes != 2) {
1221 dev_warn(di->dev, "%s dm spec has unsupported byte size\n", str);
1225 if (!buf->has_data)
1229 dev_info(di->dev, "%s has %u\n", str, val);
1234 if (!(di->opts & BQ27XXX_O_RAM) && !bq27xxx_dt_to_nvm) {
1236 if (!(di->opts & BQ27XXX_O_RAM)) {
1239 dev_warn(di->dev, "%s has %u; update to %u disallowed "
1249 dev_info(di->dev, "update %s to %u\n", str, val);
1252 buf->dirty = true;
1257 const int limit = 100; local
1259 int ret, try = limit;
1270 } while (!!(ret & BQ27XXX_FLAG_CFGUP) != active && --try);
1272 if (!try && di->chip != BQ27425) { // 425 has a bug
1273 dev_err(di->dev, "timed out waiting for cfgupdate flag %d\n", active);
1274 return -EINVAL;
1277 if (limit - try > 3)
1278 dev_warn(di->dev, "cfgupdate %d, retries %d\n", active, limit - try);
1286 if (ret < 0 && ret != -EINVAL)
1287 dev_err(di->dev, "bus error on set_cfgupdate: %d\n", ret);
1295 if (ret < 0 && ret != -EINVAL)
1296 dev_err(di->dev, "bus error on soft_reset: %d\n", ret);
1304 bool cfgup = di->opts & BQ27XXX_O_CFGUP;
1307 if (!buf->dirty)
1320 ret = bq27xxx_write(di, BQ27XXX_DM_CLASS, buf->class, true);
1324 ret = bq27xxx_write(di, BQ27XXX_DM_BLOCK, buf->block, true);
1330 ret = bq27xxx_write_block(di, BQ27XXX_DM_DATA, buf->data, BQ27XXX_DM_SZ);
1353 buf->dirty = false;
1361 dev_err(di->dev, "bus error writing chip memory: %d\n", ret);
1375 if (info->charge_full_design_uah != -EINVAL &&
1376 info->energy_full_design_uwh != -EINVAL) {
1381 info->charge_full_design_uah / 1000);
1384 info->energy_full_design_uwh / 1000);
1387 if (info->voltage_min_design_uv != -EINVAL) {
1393 info->voltage_min_design_uv / 1000);
1403 if (updated && !(di->opts & BQ27XXX_O_CFGUP)) {
1415 if (power_supply_get_battery_info(di->bat, &info) < 0)
1418 if (!di->dm_regs) {
1419 dev_warn(di->dev, "data memory update not supported for chip\n");
1424 if (info.energy_full_design_uwh == -EINVAL)
1425 dev_warn(di->dev, "missing battery:energy-full-design-microwatt-hours\n");
1426 else if (info.charge_full_design_uah == -EINVAL)
1427 dev_warn(di->dev, "missing battery:charge-full-design-microamp-hours\n");
1431 max = di->dm_regs[BQ27XXX_DM_DESIGN_ENERGY].max;
1433 dev_err(di->dev, "invalid battery:energy-full-design-microwatt-hours %d\n",
1435 info.energy_full_design_uwh = -EINVAL;
1439 max = di->dm_regs[BQ27XXX_DM_DESIGN_CAPACITY].max;
1441 dev_err(di->dev, "invalid battery:charge-full-design-microamp-hours %d\n",
1443 info.charge_full_design_uah = -EINVAL;
1446 min = di->dm_regs[BQ27XXX_DM_TERMINATE_VOLTAGE].min;
1447 max = di->dm_regs[BQ27XXX_DM_TERMINATE_VOLTAGE].max;
1450 info.voltage_min_design_uv != -EINVAL) {
1451 dev_err(di->dev, "invalid battery:voltage-min-design-microvolt %d\n",
1453 info.voltage_min_design_uv = -EINVAL;
1456 if ((info.energy_full_design_uwh != -EINVAL &&
1457 info.charge_full_design_uah != -EINVAL) ||
1458 info.voltage_min_design_uv != -EINVAL)
1463 * Return the battery State-of-Charge
1470 if (di->opts & BQ27XXX_O_SOC_SI)
1476 dev_dbg(di->dev, "error reading State-of-Charge\n");
1482 * Return a battery charge value in µAh
1487 int charge; local
1489 charge = bq27xxx_read(di, reg, false);
1490 if (charge < 0) {
1491 dev_dbg(di->dev, "error reading charge register %02x: %d\n",
1492 reg, charge);
1493 return charge;
1496 if (di->opts & BQ27XXX_O_ZERO)
1497 charge *= BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
1499 charge *= 1000;
1501 return charge;
1512 if (di->opts & BQ27XXX_O_ZERO) {
1515 return -ENODATA;
1522 * Return the battery Full Charge Capacity in µAh
1538 if (di->opts & BQ27XXX_O_ZERO)
1544 dev_dbg(di->dev, "error reading initial last measured discharge\n");
1548 if (di->opts & BQ27XXX_O_ZERO)
1566 dev_dbg(di->dev, "error reading available energy\n");
1570 if (di->opts & BQ27XXX_O_ZERO)
1588 dev_err(di->dev, "error reading temperature\n");
1592 if (di->opts & BQ27XXX_O_ZERO)
1608 dev_err(di->dev, "error reading cycle count total\n");
1623 dev_dbg(di->dev, "error reading time register %02x: %d\n",
1629 return -ENODATA;
1644 dev_err(di->dev, "error reading average power register %02x: %d\n",
1649 if (di->opts & BQ27XXX_O_ZERO)
1660 if (di->opts & BQ27XXX_O_OTDC)
1662 if (di->opts & BQ27XXX_O_UTOT)
1673 if (di->opts & BQ27XXX_O_UTOT)
1680 * Returns true if a low state of charge condition is detected
1684 if (di->opts & BQ27XXX_O_ZERO)
1686 else if (di->opts & BQ27Z561_O_BITS)
1695 if (unlikely(bq27xxx_battery_overtemp(di, di->cache.flags)))
1697 if (unlikely(bq27xxx_battery_undertemp(di, di->cache.flags)))
1699 if (unlikely(bq27xxx_battery_dead(di, di->cache.flags)))
1708 bool has_ci_flag = di->opts & BQ27XXX_O_HAS_CI;
1709 bool has_singe_flag = di->opts & BQ27XXX_O_ZERO;
1713 cache.flags = -1; /* read error */
1717 dev_info_once(di->dev, "battery is not calibrated! ignoring capacity values\n");
1718 cache.capacity = -ENODATA;
1719 cache.energy = -ENODATA;
1720 cache.time_to_empty = -ENODATA;
1721 cache.time_to_empty_avg = -ENODATA;
1722 cache.time_to_full = -ENODATA;
1723 cache.charge_full = -ENODATA;
1724 cache.health = -ENODATA;
1726 if (di->regs[BQ27XXX_REG_TTE] != INVALID_REG_ADDR)
1728 if (di->regs[BQ27XXX_REG_TTECP] != INVALID_REG_ADDR)
1730 if (di->regs[BQ27XXX_REG_TTF] != INVALID_REG_ADDR)
1735 if (di->regs[BQ27XXX_REG_AE] != INVALID_REG_ADDR)
1737 di->cache.flags = cache.flags;
1740 if (di->regs[BQ27XXX_REG_CYCT] != INVALID_REG_ADDR)
1742 if (di->regs[BQ27XXX_REG_AP] != INVALID_REG_ADDR)
1745 /* We only have to read charge design full once */
1746 if (di->charge_design_full <= 0)
1747 di->charge_design_full = bq27xxx_battery_read_dcap(di);
1750 if ((di->cache.capacity != cache.capacity) ||
1751 (di->cache.flags != cache.flags))
1752 power_supply_changed(di->bat);
1754 if (memcmp(&di->cache, &cache, sizeof(cache)) != 0)
1755 di->cache = cache;
1757 di->last_update = jiffies;
1770 schedule_delayed_work(&di->work, poll_interval * HZ);
1774 * Return the battery average current in µA
1775 * Note that current can be negative signed as well
1786 dev_err(di->dev, "error reading current\n");
1790 if (di->opts & BQ27XXX_O_ZERO) {
1793 dev_dbg(di->dev, "negative current!\n");
1794 curr = -curr;
1797 val->intval = curr * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
1800 val->intval = (int)((s16)curr) * 1000;
1811 if (di->opts & BQ27XXX_O_ZERO) {
1812 if (di->cache.flags & BQ27000_FLAG_FC)
1814 else if (di->cache.flags & BQ27000_FLAG_CHGS)
1818 } else if (di->opts & BQ27Z561_O_BITS) {
1819 if (di->cache.flags & BQ27Z561_FLAG_FC)
1821 else if (di->cache.flags & BQ27Z561_FLAG_DIS_CH)
1826 if (di->cache.flags & BQ27XXX_FLAG_FC)
1828 else if (di->cache.flags & BQ27XXX_FLAG_DSC)
1835 (power_supply_am_i_supplied(di->bat) > 0))
1838 val->intval = status;
1848 if (di->opts & BQ27XXX_O_ZERO) {
1849 if (di->cache.flags & BQ27000_FLAG_FC)
1851 else if (di->cache.flags & BQ27000_FLAG_EDV1)
1853 else if (di->cache.flags & BQ27000_FLAG_EDVF)
1857 } else if (di->opts & BQ27Z561_O_BITS) {
1858 if (di->cache.flags & BQ27Z561_FLAG_FC)
1860 else if (di->cache.flags & BQ27Z561_FLAG_FDC)
1865 if (di->cache.flags & BQ27XXX_FLAG_FC)
1867 else if (di->cache.flags & BQ27XXX_FLAG_SOC1)
1869 else if (di->cache.flags & BQ27XXX_FLAG_SOCF)
1875 val->intval = level;
1891 dev_err(di->dev, "error reading voltage\n");
1895 val->intval = volt * 1000;
1906 val->intval = value;
1918 mutex_lock(&di->lock);
1919 if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
1920 cancel_delayed_work_sync(&di->work);
1921 bq27xxx_battery_poll(&di->work.work);
1923 mutex_unlock(&di->lock);
1925 if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
1926 return -ENODEV;
1936 val->intval = di->cache.flags < 0 ? 0 : 1;
1942 ret = bq27xxx_simple_value(di->cache.capacity, val);
1948 ret = bq27xxx_simple_value(di->cache.temperature, val);
1950 val->intval -= 2731; /* convert decidegree k to c */
1953 ret = bq27xxx_simple_value(di->cache.time_to_empty, val);
1956 ret = bq27xxx_simple_value(di->cache.time_to_empty_avg, val);
1959 ret = bq27xxx_simple_value(di->cache.time_to_full, val);
1962 if (di->opts & BQ27XXX_O_MUL_CHEM)
1963 val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
1965 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
1971 ret = bq27xxx_simple_value(di->cache.charge_full, val);
1974 ret = bq27xxx_simple_value(di->charge_design_full, val);
1982 return -EINVAL;
1984 ret = bq27xxx_simple_value(di->cache.cycle_count, val);
1987 ret = bq27xxx_simple_value(di->cache.energy, val);
1990 ret = bq27xxx_simple_value(di->cache.power_avg, val);
1993 ret = bq27xxx_simple_value(di->cache.health, val);
1996 val->strval = BQ27XXX_MANUFACTURER;
1999 return -EINVAL;
2009 cancel_delayed_work_sync(&di->work);
2010 schedule_delayed_work(&di->work, 0);
2017 .of_node = di->dev->of_node,
2021 INIT_DELAYED_WORK(&di->work, bq27xxx_battery_poll);
2022 mutex_init(&di->lock);
2024 di->regs = bq27xxx_chip_data[di->chip].regs;
2025 di->unseal_key = bq27xxx_chip_data[di->chip].unseal_key;
2026 di->dm_regs = bq27xxx_chip_data[di->chip].dm_regs;
2027 di->opts = bq27xxx_chip_data[di->chip].opts;
2029 psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL);
2031 return -ENOMEM;
2033 psy_desc->name = di->name;
2034 psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
2035 psy_desc->properties = bq27xxx_chip_data[di->chip].props;
2036 psy_desc->num_properties = bq27xxx_chip_data[di->chip].props_size;
2037 psy_desc->get_property = bq27xxx_battery_get_property;
2038 psy_desc->external_power_changed = bq27xxx_external_power_changed;
2040 di->bat = power_supply_register_no_ws(di->dev, psy_desc, &psy_cfg);
2041 if (IS_ERR(di->bat))
2042 return dev_err_probe(di->dev, PTR_ERR(di->bat),
2049 list_add(&di->list, &bq27xxx_battery_devices);
2066 cancel_delayed_work_sync(&di->work);
2068 power_supply_unregister(di->bat);
2071 list_del(&di->list);
2074 mutex_destroy(&di->lock);