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
63 #define BQ27XXX_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
64 #define BQ27XXX_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
73 #define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */
74 #define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */
77 #define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
97 * bq27xxx_reg_index - Register names
99 * These are indexes into a device's register mapping array.
107 BQ27XXX_REG_AI, /* Average Current */
109 BQ27XXX_REG_TTE, /* Time-to-Empty */
110 BQ27XXX_REG_TTF, /* Time-to-Full */
111 BQ27XXX_REG_TTES, /* Time-to-Empty Standby */
112 BQ27XXX_REG_TTECP, /* Time-to-Empty at Constant Power */
115 BQ27XXX_REG_FCC, /* Full Charge Capacity */
118 BQ27XXX_REG_SOC, /* State-of-Charge */
1059 * struct bq27xxx_dm_buf - chip data memory buffer
1076 .class = (di)->dm_regs[i].subclass_id, \
1077 .block = (di)->dm_regs[i].offset / BQ27XXX_DM_SZ, \
1083 if (buf->class == reg->subclass_id && in bq27xxx_dm_reg_ptr()
1084 buf->block == reg->offset / BQ27XXX_DM_SZ) in bq27xxx_dm_reg_ptr()
1085 return (__be16 *) (buf->data + reg->offset % BQ27XXX_DM_SZ); in bq27xxx_dm_reg_ptr()
1091 [BQ27XXX_DM_DESIGN_CAPACITY] = "design-capacity",
1092 [BQ27XXX_DM_DESIGN_ENERGY] = "design-energy",
1093 [BQ27XXX_DM_TERMINATE_VOLTAGE] = "terminate-voltage",
1100 "Devicetree monitored-battery config updates data memory on NVM/flash chips.\n"
1104 "\nSetting this affects future kernel updates, not the current configuration."
1111 unsigned int prev_val = *(unsigned int *) kp->arg; in poll_interval_param_set()
1115 if (ret < 0 || prev_val == *(unsigned int *) kp->arg) in poll_interval_param_set()
1120 mod_delayed_work(system_wq, &di->work, 0); in poll_interval_param_set()
1134 "battery poll interval in seconds - 0 disables polling");
1145 if (!di || di->regs[reg_index] == INVALID_REG_ADDR) in bq27xxx_read()
1146 return -EINVAL; in bq27xxx_read()
1148 ret = di->bus.read(di, di->regs[reg_index], single); in bq27xxx_read()
1150 dev_dbg(di->dev, "failed to read register 0x%02x (index %d)\n", in bq27xxx_read()
1151 di->regs[reg_index], reg_index); in bq27xxx_read()
1161 if (!di || di->regs[reg_index] == INVALID_REG_ADDR) in bq27xxx_write()
1162 return -EINVAL; in bq27xxx_write()
1164 if (!di->bus.write) in bq27xxx_write()
1165 return -EPERM; in bq27xxx_write()
1167 ret = di->bus.write(di, di->regs[reg_index], value, single); in bq27xxx_write()
1169 dev_dbg(di->dev, "failed to write register 0x%02x (index %d)\n", in bq27xxx_write()
1170 di->regs[reg_index], reg_index); in bq27xxx_write()
1180 if (!di || di->regs[reg_index] == INVALID_REG_ADDR) in bq27xxx_read_block()
1181 return -EINVAL; in bq27xxx_read_block()
1183 if (!di->bus.read_bulk) in bq27xxx_read_block()
1184 return -EPERM; in bq27xxx_read_block()
1186 ret = di->bus.read_bulk(di, di->regs[reg_index], data, len); in bq27xxx_read_block()
1188 dev_dbg(di->dev, "failed to read_bulk register 0x%02x (index %d)\n", in bq27xxx_read_block()
1189 di->regs[reg_index], reg_index); in bq27xxx_read_block()
1199 if (!di || di->regs[reg_index] == INVALID_REG_ADDR) in bq27xxx_write_block()
1200 return -EINVAL; in bq27xxx_write_block()
1202 if (!di->bus.write_bulk) in bq27xxx_write_block()
1203 return -EPERM; in bq27xxx_write_block()
1205 ret = di->bus.write_bulk(di, di->regs[reg_index], data, len); in bq27xxx_write_block()
1207 dev_dbg(di->dev, "failed to write_bulk register 0x%02x (index %d)\n", in bq27xxx_write_block()
1208 di->regs[reg_index], reg_index); in bq27xxx_write_block()
1219 dev_err(di->dev, "bus error on seal: %d\n", ret); in bq27xxx_battery_seal()
1230 if (di->unseal_key == 0) { in bq27xxx_battery_unseal()
1231 dev_err(di->dev, "unseal failed due to missing key\n"); in bq27xxx_battery_unseal()
1232 return -EINVAL; in bq27xxx_battery_unseal()
1235 ret = bq27xxx_write(di, BQ27XXX_REG_CTRL, (u16)(di->unseal_key >> 16), false); in bq27xxx_battery_unseal()
1239 ret = bq27xxx_write(di, BQ27XXX_REG_CTRL, (u16)di->unseal_key, false); in bq27xxx_battery_unseal()
1246 dev_err(di->dev, "bus error on unseal: %d\n", ret); in bq27xxx_battery_unseal()
1256 sum += buf->data[i]; in bq27xxx_battery_checksum_dm_block()
1259 return 0xff - sum; in bq27xxx_battery_checksum_dm_block()
1267 buf->has_data = false; in bq27xxx_battery_read_dm_block()
1269 ret = bq27xxx_write(di, BQ27XXX_DM_CLASS, buf->class, true); in bq27xxx_battery_read_dm_block()
1273 ret = bq27xxx_write(di, BQ27XXX_DM_BLOCK, buf->block, true); in bq27xxx_battery_read_dm_block()
1279 ret = bq27xxx_read_block(di, BQ27XXX_DM_DATA, buf->data, BQ27XXX_DM_SZ); in bq27xxx_battery_read_dm_block()
1288 ret = -EINVAL; in bq27xxx_battery_read_dm_block()
1292 buf->has_data = true; in bq27xxx_battery_read_dm_block()
1293 buf->dirty = false; in bq27xxx_battery_read_dm_block()
1298 dev_err(di->dev, "bus error reading chip memory: %d\n", ret); in bq27xxx_battery_read_dm_block()
1307 struct bq27xxx_dm_reg *reg = &di->dm_regs[reg_id];
1312 dev_warn(di->dev, "buffer does not match %s dm spec\n", str);
1316 if (reg->bytes != 2) {
1317 dev_warn(di->dev, "%s dm spec has unsupported byte size\n", str);
1321 if (!buf->has_data)
1325 dev_info(di->dev, "%s has %u\n", str, val);
1330 if (!(di->opts & BQ27XXX_O_RAM) && !bq27xxx_dt_to_nvm) {
1332 if (!(di->opts & BQ27XXX_O_RAM)) {
1335 dev_warn(di->dev, "%s has %u; update to %u disallowed "
1345 dev_info(di->dev, "update %s to %u\n", str, val);
1348 buf->dirty = true;
1353 const int limit = 100; local
1355 int ret, try = limit;
1366 } while (!!(ret & BQ27XXX_FLAG_CFGUP) != active && --try);
1368 if (!try && di->chip != BQ27425) { // 425 has a bug
1369 dev_err(di->dev, "timed out waiting for cfgupdate flag %d\n", active);
1370 return -EINVAL;
1373 if (limit - try > 3)
1374 dev_warn(di->dev, "cfgupdate %d, retries %d\n", active, limit - try);
1382 if (ret < 0 && ret != -EINVAL)
1383 dev_err(di->dev, "bus error on set_cfgupdate: %d\n", ret);
1391 if (ret < 0 && ret != -EINVAL)
1392 dev_err(di->dev, "bus error on soft_reset: %d\n", ret);
1400 bool cfgup = di->opts & BQ27XXX_O_CFGUP;
1403 if (!buf->dirty)
1416 ret = bq27xxx_write(di, BQ27XXX_DM_CLASS, buf->class, true);
1420 ret = bq27xxx_write(di, BQ27XXX_DM_BLOCK, buf->block, true);
1426 ret = bq27xxx_write_block(di, BQ27XXX_DM_DATA, buf->data, BQ27XXX_DM_SZ);
1449 buf->dirty = false;
1457 dev_err(di->dev, "bus error writing chip memory: %d\n", ret);
1471 if (info->charge_full_design_uah != -EINVAL &&
1472 info->energy_full_design_uwh != -EINVAL) {
1477 info->charge_full_design_uah / 1000);
1480 info->energy_full_design_uwh / 1000);
1483 if (info->voltage_min_design_uv != -EINVAL) {
1489 info->voltage_min_design_uv / 1000);
1499 if (updated && !(di->opts & BQ27XXX_O_CFGUP)) {
1511 if (power_supply_get_battery_info(di->bat, &info) < 0)
1514 if (!di->dm_regs) {
1515 dev_warn(di->dev, "data memory update not supported for chip\n");
1519 if (info->energy_full_design_uwh != info->charge_full_design_uah) {
1520 if (info->energy_full_design_uwh == -EINVAL)
1521 dev_warn(di->dev, "missing battery:energy-full-design-microwatt-hours\n");
1522 else if (info->charge_full_design_uah == -EINVAL)
1523 dev_warn(di->dev, "missing battery:charge-full-design-microamp-hours\n");
1527 max = di->dm_regs[BQ27XXX_DM_DESIGN_ENERGY].max;
1528 if (info->energy_full_design_uwh > max * 1000) {
1529 dev_err(di->dev, "invalid battery:energy-full-design-microwatt-hours %d\n",
1530 info->energy_full_design_uwh);
1531 info->energy_full_design_uwh = -EINVAL;
1535 max = di->dm_regs[BQ27XXX_DM_DESIGN_CAPACITY].max;
1536 if (info->charge_full_design_uah > max * 1000) {
1537 dev_err(di->dev, "invalid battery:charge-full-design-microamp-hours %d\n",
1538 info->charge_full_design_uah);
1539 info->charge_full_design_uah = -EINVAL;
1542 min = di->dm_regs[BQ27XXX_DM_TERMINATE_VOLTAGE].min;
1543 max = di->dm_regs[BQ27XXX_DM_TERMINATE_VOLTAGE].max;
1544 if ((info->voltage_min_design_uv < min * 1000 ||
1545 info->voltage_min_design_uv > max * 1000) &&
1546 info->voltage_min_design_uv != -EINVAL) {
1547 dev_err(di->dev, "invalid battery:voltage-min-design-microvolt %d\n",
1548 info->voltage_min_design_uv);
1549 info->voltage_min_design_uv = -EINVAL;
1552 if ((info->energy_full_design_uwh != -EINVAL &&
1553 info->charge_full_design_uah != -EINVAL) ||
1554 info->voltage_min_design_uv != -EINVAL)
1559 * Return the battery State-of-Charge
1566 if (di->opts & BQ27XXX_O_SOC_SI)
1572 dev_dbg(di->dev, "error reading State-of-Charge\n");
1578 * Return a battery charge value in µAh
1584 int charge; local
1586 charge = bq27xxx_read(di, reg, false);
1587 if (charge < 0) {
1588 dev_dbg(di->dev, "error reading charge register %02x: %d\n",
1589 reg, charge);
1590 return charge;
1593 if (di->opts & BQ27XXX_O_ZERO)
1594 charge *= BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
1596 charge *= 1000;
1598 val->intval = charge;
1624 * Return the battery Full Charge Capacity in µAh
1642 /* We only have to read charge design full once */
1643 if (di->charge_design_full > 0) {
1644 val->intval = di->charge_design_full;
1648 if (di->opts & BQ27XXX_O_ZERO)
1654 dev_dbg(di->dev, "error reading design capacity\n");
1658 if (di->opts & BQ27XXX_O_ZERO)
1664 di->charge_design_full = dcap;
1666 val->intval = dcap;
1682 dev_dbg(di->dev, "error reading available energy\n");
1686 if (di->opts & BQ27XXX_O_ZERO)
1691 val->intval = ae;
1707 dev_err(di->dev, "error reading temperature\n");
1711 if (di->opts & BQ27XXX_O_ZERO)
1715 temp -= 2731;
1717 val->intval = temp;
1733 dev_err(di->dev, "error reading cycle count total\n");
1735 val->intval = cyct;
1751 dev_dbg(di->dev, "error reading time register %02x: %d\n",
1757 return -ENODATA;
1759 val->intval = tval * 60;
1769 if (di->opts & BQ27XXX_O_OTDC)
1771 if (di->opts & BQ27XXX_O_UTOT)
1782 if (di->opts & BQ27XXX_O_UTOT)
1789 * Returns true if a low state of charge condition is detected
1793 if (di->opts & BQ27XXX_O_ZERO)
1795 else if (di->opts & BQ27Z561_O_BITS)
1807 if (di->opts & BQ27XXX_O_HAS_CI)
1819 if (unlikely(bq27xxx_battery_overtemp(di, di->cache.flags)))
1821 else if (unlikely(bq27xxx_battery_undertemp(di, di->cache.flags)))
1823 else if (unlikely(bq27xxx_battery_dead(di, di->cache.flags)))
1825 else if (unlikely(bq27xxx_battery_capacity_inaccurate(di, di->cache.flags)))
1830 val->intval = health;
1837 if (di->opts & BQ27XXX_O_ZERO)
1839 else if (di->opts & BQ27Z561_O_BITS)
1846 * Return the battery average current in µA and the status
1847 * Note that current can be negative signed as well
1856 bool single_flags = (di->opts & BQ27XXX_O_ZERO);
1862 dev_err(di->dev, "error reading current\n");
1867 flags = cache->flags;
1871 dev_err(di->dev, "error reading flags\n");
1876 if (di->opts & BQ27XXX_O_ZERO) {
1878 dev_dbg(di->dev, "negative current!\n");
1879 curr = -curr;
1889 val_curr->intval = curr;
1893 val_status->intval = POWER_SUPPLY_STATUS_FULL;
1895 val_status->intval = POWER_SUPPLY_STATUS_CHARGING;
1897 val_status->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1899 val_status->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1907 union power_supply_propval status = di->last_status;
1909 bool has_singe_flag = di->opts & BQ27XXX_O_ZERO;
1913 cache.flags = -1; /* read error */
1916 di->cache.flags = cache.flags;
1919 * On gauges with signed current reporting the current must be
1920 * checked to detect charging <-> discharging status changes.
1922 if (!(di->opts & BQ27XXX_O_ZERO))
1926 if ((di->cache.capacity != cache.capacity) ||
1927 (di->cache.flags != cache.flags) ||
1928 (di->last_status.intval != status.intval)) {
1929 di->last_status.intval = status.intval;
1930 power_supply_changed(di->bat);
1933 if (memcmp(&di->cache, &cache, sizeof(cache)) != 0)
1934 di->cache = cache;
1936 di->last_update = jiffies;
1938 if (!di->removed && poll_interval > 0)
1939 mod_delayed_work(system_wq, &di->work, poll_interval * HZ);
1944 mutex_lock(&di->lock);
1946 mutex_unlock(&di->lock);
1970 dev_err(di->dev,
1976 if (di->opts & BQ27XXX_O_ZERO)
1977 val->intval = (power * BQ27XXX_POWER_CONSTANT) / BQ27XXX_RS;
1980 val->intval = (int)((s16)power) * 10000;
1990 if (di->opts & BQ27XXX_O_ZERO) {
1991 if (di->cache.flags & BQ27000_FLAG_FC)
1993 else if (di->cache.flags & BQ27000_FLAG_EDVF)
1995 else if (di->cache.flags & BQ27000_FLAG_EDV1)
1999 } else if (di->opts & BQ27Z561_O_BITS) {
2000 if (di->cache.flags & BQ27Z561_FLAG_FC)
2002 else if (di->cache.flags & BQ27Z561_FLAG_FDC)
2007 if (di->cache.flags & BQ27XXX_FLAG_FC)
2009 else if (di->cache.flags & BQ27XXX_FLAG_SOCF)
2011 else if (di->cache.flags & BQ27XXX_FLAG_SOC1)
2017 val->intval = level;
2033 dev_err(di->dev, "error reading voltage\n");
2037 val->intval = volt * 1000;
2048 val->intval = value;
2060 mutex_lock(&di->lock);
2061 if (time_is_before_jiffies(di->last_update + 5 * HZ))
2063 mutex_unlock(&di->lock);
2065 if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
2066 return -ENODEV;
2076 val->intval = di->cache.flags < 0 ? 0 : 1;
2082 ret = bq27xxx_simple_value(di->cache.capacity, val);
2100 if (di->opts & BQ27XXX_O_MUL_CHEM)
2101 val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
2103 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
2106 if (di->regs[BQ27XXX_REG_NAC] != INVALID_REG_ADDR)
2123 return -EINVAL;
2137 val->strval = BQ27XXX_MANUFACTURER;
2140 return -EINVAL;
2151 mod_delayed_work(system_wq, &di->work, HZ / 2);
2165 .of_node = di->dev->of_node,
2171 INIT_DELAYED_WORK(&di->work, bq27xxx_battery_poll);
2172 mutex_init(&di->lock);
2173 ret = devm_add_action_or_reset(di->dev, bq27xxx_battery_mutex_destroy,
2174 &di->lock);
2178 di->regs = bq27xxx_chip_data[di->chip].regs;
2179 di->unseal_key = bq27xxx_chip_data[di->chip].unseal_key;
2180 di->dm_regs = bq27xxx_chip_data[di->chip].dm_regs;
2181 di->opts = bq27xxx_chip_data[di->chip].opts;
2183 psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL);
2185 return -ENOMEM;
2187 psy_desc->name = di->name;
2188 psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
2189 psy_desc->properties = bq27xxx_chip_data[di->chip].props;
2190 psy_desc->num_properties = bq27xxx_chip_data[di->chip].props_size;
2191 psy_desc->get_property = bq27xxx_battery_get_property;
2192 psy_desc->external_power_changed = bq27xxx_external_power_changed;
2194 di->bat = devm_power_supply_register(di->dev, psy_desc, &psy_cfg);
2195 if (IS_ERR(di->bat))
2196 return dev_err_probe(di->dev, PTR_ERR(di->bat),
2203 list_add(&di->list, &bq27xxx_battery_devices);
2213 list_del(&di->list);
2216 /* Set removed to avoid bq27xxx_battery_update() re-queuing the work */
2217 mutex_lock(&di->lock);
2218 di->removed = true;
2219 mutex_unlock(&di->lock);
2221 cancel_delayed_work_sync(&di->work);
2230 cancel_delayed_work(&di->work);
2238 schedule_delayed_work(&di->work, 0);