Lines Matching +full:bat +full:- +full:temp
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 */
97 * bq27xxx_reg_index - Register names
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 */
118 BQ27XXX_REG_SOC, /* State-of-Charge */
1026 * struct bq27xxx_dm_buf - chip data memory buffer
1043 .class = (di)->dm_regs[i].subclass_id, \
1044 .block = (di)->dm_regs[i].offset / BQ27XXX_DM_SZ, \
1050 if (buf->class == reg->subclass_id && in bq27xxx_dm_reg_ptr()
1051 buf->block == reg->offset / BQ27XXX_DM_SZ) in bq27xxx_dm_reg_ptr()
1052 return (__be16 *) (buf->data + reg->offset % BQ27XXX_DM_SZ); in bq27xxx_dm_reg_ptr()
1058 [BQ27XXX_DM_DESIGN_CAPACITY] = "design-capacity",
1059 [BQ27XXX_DM_DESIGN_ENERGY] = "design-energy",
1060 [BQ27XXX_DM_TERMINATE_VOLTAGE] = "terminate-voltage",
1067 "Devicetree monitored-battery config updates data memory on NVM/flash chips.\n"
1078 unsigned int prev_val = *(unsigned int *) kp->arg; in poll_interval_param_set()
1082 if (ret < 0 || prev_val == *(unsigned int *) kp->arg) in poll_interval_param_set()
1087 mod_delayed_work(system_wq, &di->work, 0); in poll_interval_param_set()
1101 "battery poll interval in seconds - 0 disables polling");
1112 if (!di || di->regs[reg_index] == INVALID_REG_ADDR) in bq27xxx_read()
1113 return -EINVAL; in bq27xxx_read()
1115 ret = di->bus.read(di, di->regs[reg_index], single); in bq27xxx_read()
1117 dev_dbg(di->dev, "failed to read register 0x%02x (index %d)\n", in bq27xxx_read()
1118 di->regs[reg_index], reg_index); in bq27xxx_read()
1128 if (!di || di->regs[reg_index] == INVALID_REG_ADDR) in bq27xxx_write()
1129 return -EINVAL; in bq27xxx_write()
1131 if (!di->bus.write) in bq27xxx_write()
1132 return -EPERM; in bq27xxx_write()
1134 ret = di->bus.write(di, di->regs[reg_index], value, single); in bq27xxx_write()
1136 dev_dbg(di->dev, "failed to write register 0x%02x (index %d)\n", in bq27xxx_write()
1137 di->regs[reg_index], reg_index); in bq27xxx_write()
1147 if (!di || di->regs[reg_index] == INVALID_REG_ADDR) in bq27xxx_read_block()
1148 return -EINVAL; in bq27xxx_read_block()
1150 if (!di->bus.read_bulk) in bq27xxx_read_block()
1151 return -EPERM; in bq27xxx_read_block()
1153 ret = di->bus.read_bulk(di, di->regs[reg_index], data, len); in bq27xxx_read_block()
1155 dev_dbg(di->dev, "failed to read_bulk register 0x%02x (index %d)\n", in bq27xxx_read_block()
1156 di->regs[reg_index], reg_index); in bq27xxx_read_block()
1166 if (!di || di->regs[reg_index] == INVALID_REG_ADDR) in bq27xxx_write_block()
1167 return -EINVAL; in bq27xxx_write_block()
1169 if (!di->bus.write_bulk) in bq27xxx_write_block()
1170 return -EPERM; in bq27xxx_write_block()
1172 ret = di->bus.write_bulk(di, di->regs[reg_index], data, len); in bq27xxx_write_block()
1174 dev_dbg(di->dev, "failed to write_bulk register 0x%02x (index %d)\n", in bq27xxx_write_block()
1175 di->regs[reg_index], reg_index); in bq27xxx_write_block()
1186 dev_err(di->dev, "bus error on seal: %d\n", ret); in bq27xxx_battery_seal()
1197 if (di->unseal_key == 0) { in bq27xxx_battery_unseal()
1198 dev_err(di->dev, "unseal failed due to missing key\n"); in bq27xxx_battery_unseal()
1199 return -EINVAL; in bq27xxx_battery_unseal()
1202 ret = bq27xxx_write(di, BQ27XXX_REG_CTRL, (u16)(di->unseal_key >> 16), false); in bq27xxx_battery_unseal()
1206 ret = bq27xxx_write(di, BQ27XXX_REG_CTRL, (u16)di->unseal_key, false); in bq27xxx_battery_unseal()
1213 dev_err(di->dev, "bus error on unseal: %d\n", ret); in bq27xxx_battery_unseal()
1223 sum += buf->data[i]; in bq27xxx_battery_checksum_dm_block()
1226 return 0xff - sum; in bq27xxx_battery_checksum_dm_block()
1234 buf->has_data = false; in bq27xxx_battery_read_dm_block()
1236 ret = bq27xxx_write(di, BQ27XXX_DM_CLASS, buf->class, true); in bq27xxx_battery_read_dm_block()
1240 ret = bq27xxx_write(di, BQ27XXX_DM_BLOCK, buf->block, true); in bq27xxx_battery_read_dm_block()
1246 ret = bq27xxx_read_block(di, BQ27XXX_DM_DATA, buf->data, BQ27XXX_DM_SZ); in bq27xxx_battery_read_dm_block()
1255 ret = -EINVAL; in bq27xxx_battery_read_dm_block()
1259 buf->has_data = true; in bq27xxx_battery_read_dm_block()
1260 buf->dirty = false; in bq27xxx_battery_read_dm_block()
1265 dev_err(di->dev, "bus error reading chip memory: %d\n", ret); in bq27xxx_battery_read_dm_block()
1274 struct bq27xxx_dm_reg *reg = &di->dm_regs[reg_id];
1279 dev_warn(di->dev, "buffer does not match %s dm spec\n", str);
1283 if (reg->bytes != 2) {
1284 dev_warn(di->dev, "%s dm spec has unsupported byte size\n", str);
1288 if (!buf->has_data)
1292 dev_info(di->dev, "%s has %u\n", str, val);
1297 if (!(di->opts & BQ27XXX_O_RAM) && !bq27xxx_dt_to_nvm) {
1299 if (!(di->opts & BQ27XXX_O_RAM)) {
1302 dev_warn(di->dev, "%s has %u; update to %u disallowed "
1312 dev_info(di->dev, "update %s to %u\n", str, val);
1315 buf->dirty = true;
1333 } while (!!(ret & BQ27XXX_FLAG_CFGUP) != active && --try);
1335 if (!try && di->chip != BQ27425) { // 425 has a bug
1336 dev_err(di->dev, "timed out waiting for cfgupdate flag %d\n", active);
1337 return -EINVAL;
1340 if (limit - try > 3)
1341 dev_warn(di->dev, "cfgupdate %d, retries %d\n", active, limit - try);
1349 if (ret < 0 && ret != -EINVAL)
1350 dev_err(di->dev, "bus error on set_cfgupdate: %d\n", ret);
1358 if (ret < 0 && ret != -EINVAL)
1359 dev_err(di->dev, "bus error on soft_reset: %d\n", ret);
1367 bool cfgup = di->opts & BQ27XXX_O_CFGUP;
1370 if (!buf->dirty)
1383 ret = bq27xxx_write(di, BQ27XXX_DM_CLASS, buf->class, true);
1387 ret = bq27xxx_write(di, BQ27XXX_DM_BLOCK, buf->block, true);
1393 ret = bq27xxx_write_block(di, BQ27XXX_DM_DATA, buf->data, BQ27XXX_DM_SZ);
1416 buf->dirty = false;
1424 dev_err(di->dev, "bus error writing chip memory: %d\n", ret);
1438 if (info->charge_full_design_uah != -EINVAL &&
1439 info->energy_full_design_uwh != -EINVAL) {
1444 info->charge_full_design_uah / 1000);
1447 info->energy_full_design_uwh / 1000);
1450 if (info->voltage_min_design_uv != -EINVAL) {
1456 info->voltage_min_design_uv / 1000);
1466 if (updated && !(di->opts & BQ27XXX_O_CFGUP)) {
1478 if (power_supply_get_battery_info(di->bat, &info) < 0)
1481 if (!di->dm_regs) {
1482 dev_warn(di->dev, "data memory update not supported for chip\n");
1486 if (info->energy_full_design_uwh != info->charge_full_design_uah) {
1487 if (info->energy_full_design_uwh == -EINVAL)
1488 dev_warn(di->dev, "missing battery:energy-full-design-microwatt-hours\n");
1489 else if (info->charge_full_design_uah == -EINVAL)
1490 dev_warn(di->dev, "missing battery:charge-full-design-microamp-hours\n");
1494 max = di->dm_regs[BQ27XXX_DM_DESIGN_ENERGY].max;
1495 if (info->energy_full_design_uwh > max * 1000) {
1496 dev_err(di->dev, "invalid battery:energy-full-design-microwatt-hours %d\n",
1497 info->energy_full_design_uwh);
1498 info->energy_full_design_uwh = -EINVAL;
1502 max = di->dm_regs[BQ27XXX_DM_DESIGN_CAPACITY].max;
1503 if (info->charge_full_design_uah > max * 1000) {
1504 dev_err(di->dev, "invalid battery:charge-full-design-microamp-hours %d\n",
1505 info->charge_full_design_uah);
1506 info->charge_full_design_uah = -EINVAL;
1509 min = di->dm_regs[BQ27XXX_DM_TERMINATE_VOLTAGE].min;
1510 max = di->dm_regs[BQ27XXX_DM_TERMINATE_VOLTAGE].max;
1511 if ((info->voltage_min_design_uv < min * 1000 ||
1512 info->voltage_min_design_uv > max * 1000) &&
1513 info->voltage_min_design_uv != -EINVAL) {
1514 dev_err(di->dev, "invalid battery:voltage-min-design-microvolt %d\n",
1515 info->voltage_min_design_uv);
1516 info->voltage_min_design_uv = -EINVAL;
1519 if ((info->energy_full_design_uwh != -EINVAL &&
1520 info->charge_full_design_uah != -EINVAL) ||
1521 info->voltage_min_design_uv != -EINVAL)
1526 * Return the battery State-of-Charge
1533 if (di->opts & BQ27XXX_O_SOC_SI)
1539 dev_dbg(di->dev, "error reading State-of-Charge\n");
1555 dev_dbg(di->dev, "error reading charge register %02x: %d\n",
1560 if (di->opts & BQ27XXX_O_ZERO)
1565 val->intval = charge;
1610 if (di->charge_design_full > 0) {
1611 val->intval = di->charge_design_full;
1615 if (di->opts & BQ27XXX_O_ZERO)
1621 dev_dbg(di->dev, "error reading design capacity\n");
1625 if (di->opts & BQ27XXX_O_ZERO)
1631 di->charge_design_full = dcap;
1633 val->intval = dcap;
1649 dev_dbg(di->dev, "error reading available energy\n");
1653 if (di->opts & BQ27XXX_O_ZERO)
1658 val->intval = ae;
1670 int temp; local
1672 temp = bq27xxx_read(di, BQ27XXX_REG_TEMP, false);
1673 if (temp < 0) {
1674 dev_err(di->dev, "error reading temperature\n");
1675 return temp;
1678 if (di->opts & BQ27XXX_O_ZERO)
1679 temp = 5 * temp / 2;
1682 temp -= 2731;
1684 val->intval = temp;
1700 dev_err(di->dev, "error reading cycle count total\n");
1702 val->intval = cyct;
1718 dev_dbg(di->dev, "error reading time register %02x: %d\n",
1724 return -ENODATA;
1726 val->intval = tval * 60;
1736 if (di->opts & BQ27XXX_O_OTDC)
1738 if (di->opts & BQ27XXX_O_UTOT)
1749 if (di->opts & BQ27XXX_O_UTOT)
1760 if (di->opts & BQ27XXX_O_ZERO)
1762 else if (di->opts & BQ27Z561_O_BITS)
1774 if (di->opts & BQ27XXX_O_HAS_CI)
1786 if (unlikely(bq27xxx_battery_overtemp(di, di->cache.flags)))
1788 else if (unlikely(bq27xxx_battery_undertemp(di, di->cache.flags)))
1790 else if (unlikely(bq27xxx_battery_dead(di, di->cache.flags)))
1792 else if (unlikely(bq27xxx_battery_capacity_inaccurate(di, di->cache.flags)))
1797 val->intval = health;
1804 if (di->opts & BQ27XXX_O_ZERO)
1806 else if (di->opts & BQ27Z561_O_BITS)
1823 bool single_flags = (di->opts & BQ27XXX_O_ZERO);
1829 dev_err(di->dev, "error reading current\n");
1834 flags = cache->flags;
1838 dev_err(di->dev, "error reading flags\n");
1843 if (di->opts & BQ27XXX_O_ZERO) {
1845 dev_dbg(di->dev, "negative current!\n");
1846 curr = -curr;
1856 val_curr->intval = curr;
1860 val_status->intval = POWER_SUPPLY_STATUS_FULL;
1862 val_status->intval = POWER_SUPPLY_STATUS_CHARGING;
1864 val_status->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1866 val_status->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1874 union power_supply_propval status = di->last_status;
1876 bool has_singe_flag = di->opts & BQ27XXX_O_ZERO;
1880 cache.flags = -1; /* read error */
1883 di->cache.flags = cache.flags;
1887 * checked to detect charging <-> discharging status changes.
1889 if (!(di->opts & BQ27XXX_O_ZERO))
1893 if ((di->cache.capacity != cache.capacity) ||
1894 (di->cache.flags != cache.flags) ||
1895 (di->last_status.intval != status.intval)) {
1896 di->last_status.intval = status.intval;
1897 power_supply_changed(di->bat);
1900 if (memcmp(&di->cache, &cache, sizeof(cache)) != 0)
1901 di->cache = cache;
1903 di->last_update = jiffies;
1905 if (!di->removed && poll_interval > 0)
1906 mod_delayed_work(system_wq, &di->work, poll_interval * HZ);
1911 mutex_lock(&di->lock);
1913 mutex_unlock(&di->lock);
1937 dev_err(di->dev,
1943 if (di->opts & BQ27XXX_O_ZERO)
1944 val->intval = (power * BQ27XXX_POWER_CONSTANT) / BQ27XXX_RS;
1947 val->intval = (int)((s16)power) * 10000;
1957 if (di->opts & BQ27XXX_O_ZERO) {
1958 if (di->cache.flags & BQ27000_FLAG_FC)
1960 else if (di->cache.flags & BQ27000_FLAG_EDVF)
1962 else if (di->cache.flags & BQ27000_FLAG_EDV1)
1966 } else if (di->opts & BQ27Z561_O_BITS) {
1967 if (di->cache.flags & BQ27Z561_FLAG_FC)
1969 else if (di->cache.flags & BQ27Z561_FLAG_FDC)
1974 if (di->cache.flags & BQ27XXX_FLAG_FC)
1976 else if (di->cache.flags & BQ27XXX_FLAG_SOCF)
1978 else if (di->cache.flags & BQ27XXX_FLAG_SOC1)
1984 val->intval = level;
2000 dev_err(di->dev, "error reading voltage\n");
2004 val->intval = volt * 1000;
2015 val->intval = value;
2027 mutex_lock(&di->lock);
2028 if (time_is_before_jiffies(di->last_update + 5 * HZ))
2030 mutex_unlock(&di->lock);
2032 if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
2033 return -ENODEV;
2043 val->intval = di->cache.flags < 0 ? 0 : 1;
2049 ret = bq27xxx_simple_value(di->cache.capacity, val);
2067 if (di->opts & BQ27XXX_O_MUL_CHEM)
2068 val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
2070 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
2073 if (di->regs[BQ27XXX_REG_NAC] != INVALID_REG_ADDR)
2090 return -EINVAL;
2104 val->strval = BQ27XXX_MANUFACTURER;
2107 return -EINVAL;
2118 mod_delayed_work(system_wq, &di->work, HZ / 2);
2132 .of_node = di->dev->of_node,
2137 INIT_DELAYED_WORK(&di->work, bq27xxx_battery_poll);
2138 mutex_init(&di->lock);
2139 ret = devm_add_action_or_reset(di->dev, bq27xxx_battery_mutex_destroy,
2140 &di->lock);
2144 di->regs = bq27xxx_chip_data[di->chip].regs;
2145 di->unseal_key = bq27xxx_chip_data[di->chip].unseal_key;
2146 di->dm_regs = bq27xxx_chip_data[di->chip].dm_regs;
2147 di->opts = bq27xxx_chip_data[di->chip].opts;
2149 psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL);
2151 return -ENOMEM;
2153 psy_desc->name = di->name;
2154 psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
2155 psy_desc->properties = bq27xxx_chip_data[di->chip].props;
2156 psy_desc->num_properties = bq27xxx_chip_data[di->chip].props_size;
2157 psy_desc->get_property = bq27xxx_battery_get_property;
2158 psy_desc->external_power_changed = bq27xxx_external_power_changed;
2160 di->bat = devm_power_supply_register_no_ws(di->dev, psy_desc, &psy_cfg);
2161 if (IS_ERR(di->bat))
2162 return dev_err_probe(di->dev, PTR_ERR(di->bat),
2169 list_add(&di->list, &bq27xxx_battery_devices);
2179 list_del(&di->list);
2182 /* Set removed to avoid bq27xxx_battery_update() re-queuing the work */
2183 mutex_lock(&di->lock);
2184 di->removed = true;
2185 mutex_unlock(&di->lock);
2187 cancel_delayed_work_sync(&di->work);
2196 cancel_delayed_work(&di->work);
2204 schedule_delayed_work(&di->work, 0);