Lines Matching +full:precharge +full:- +full:current +full:- +full:microamp

1 // SPDX-License-Identifier: GPL-2.0-only
19 #include <linux/extcon-provider.h>
32 #define BQ24190_REG_POC 0x01 /* Power-On Configuration */
50 #define BQ24190_REG_CCC 0x02 /* Charge Current Control */
57 #define BQ24190_REG_PCTCC 0x03 /* Pre-charge/Termination Current Cntl */
152 * reads return the current value. In order to return the fault status
190 * The tables below provide a 2-way mapping for the value that goes in
191 * the register field and the real-world value that it represents.
193 * number at that index in the array is the real-world value that it
233 * 'val'. The index range returned is 0 to 'tbl_size' - 1. Assumes that
245 return i - 1; in bq24190_find_idx()
254 ret = i2c_smbus_read_byte_data(bdi->client, reg); in bq24190_read()
264 return i2c_smbus_write_byte_data(bdi->client, reg, data); in bq24190_write()
312 v = (v >= tbl_size) ? (tbl_size - 1) : v; in bq24190_get_field_val()
366 /* On i386 ptrace-abi.h defines SS that breaks the macro calls below. */
454 info = bq24190_sysfs_field_lookup(attr->attr.name); in bq24190_sysfs_show()
456 return -EINVAL; in bq24190_sysfs_show()
458 ret = pm_runtime_resume_and_get(bdi->dev); in bq24190_sysfs_show()
462 ret = bq24190_read_mask(bdi, info->reg, info->mask, info->shift, &v); in bq24190_sysfs_show()
468 pm_runtime_mark_last_busy(bdi->dev); in bq24190_sysfs_show()
469 pm_runtime_put_autosuspend(bdi->dev); in bq24190_sysfs_show()
483 info = bq24190_sysfs_field_lookup(attr->attr.name); in bq24190_sysfs_store()
485 return -EINVAL; in bq24190_sysfs_store()
491 ret = pm_runtime_resume_and_get(bdi->dev); in bq24190_sysfs_store()
495 ret = bq24190_write_mask(bdi, info->reg, info->mask, info->shift, v); in bq24190_sysfs_store()
499 pm_runtime_mark_last_busy(bdi->dev); in bq24190_sysfs_store()
500 pm_runtime_put_autosuspend(bdi->dev); in bq24190_sysfs_store()
508 union power_supply_propval val = { .intval = bdi->charge_type }; in bq24190_set_otg_vbus()
511 ret = pm_runtime_resume_and_get(bdi->dev); in bq24190_set_otg_vbus()
513 dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", ret); in bq24190_set_otg_vbus()
517 bdi->otg_vbus_enabled = enable; in bq24190_set_otg_vbus()
526 pm_runtime_mark_last_busy(bdi->dev); in bq24190_set_otg_vbus()
527 pm_runtime_put_autosuspend(bdi->dev); in bq24190_set_otg_vbus()
549 ret = pm_runtime_resume_and_get(bdi->dev); in bq24190_vbus_is_enabled()
551 dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", ret); in bq24190_vbus_is_enabled()
559 pm_runtime_mark_last_busy(bdi->dev); in bq24190_vbus_is_enabled()
560 pm_runtime_put_autosuspend(bdi->dev); in bq24190_vbus_is_enabled()
565 bdi->otg_vbus_enabled = (val == BQ24190_REG_POC_CHG_CONFIG_OTG || in bq24190_vbus_is_enabled()
567 return bdi->otg_vbus_enabled; in bq24190_vbus_is_enabled()
578 .of_match = "usb-otg-vbus",
594 struct bq24190_platform_data *pdata = bdi->dev->platform_data; in bq24190_register_vbus_regulator()
599 cfg.dev = bdi->dev; in bq24190_register_vbus_regulator()
600 if (pdata && pdata->regulator_init_data) in bq24190_register_vbus_regulator()
601 cfg.init_data = pdata->regulator_init_data; in bq24190_register_vbus_regulator()
605 reg = devm_regulator_register(bdi->dev, &bq24190_vbus_desc, &cfg); in bq24190_register_vbus_regulator()
608 dev_err(bdi->dev, "Can't register regulator: %d\n", ret); in bq24190_register_vbus_regulator()
629 bdi->watchdog = ((v & BQ24190_REG_CTTC_WATCHDOG_MASK) >> in bq24190_set_config()
646 if (bdi->sys_min) { in bq24190_set_config()
647 v = bdi->sys_min / 100 - 30; // manual section 9.5.1.2, table 9 in bq24190_set_config()
656 if (bdi->iprechg) { in bq24190_set_config()
657 v = bdi->iprechg / 128 - 1; // manual section 9.5.1.4, table 11 in bq24190_set_config()
666 if (bdi->iterm) { in bq24190_set_config()
667 v = bdi->iterm / 128 - 1; // manual section 9.5.1.4, table 11 in bq24190_set_config()
676 if (bdi->ichg) { in bq24190_set_config()
682 bdi->ichg); in bq24190_set_config()
687 if (bdi->vreg) { in bq24190_set_config()
693 bdi->vreg); in bq24190_set_config()
709 * { PROPERTY_ENTRY_BOOL("disable-reset"), ... }; in bq24190_register_reset()
716 if (device_property_read_bool(bdi->dev, "disable-reset")) in bq24190_register_reset()
740 } while (--limit); in bq24190_register_reset()
742 return -EIO; in bq24190_register_reset()
775 val->intval = type; in bq24190_charger_get_charge_type()
789 * termination current so it recommends turning off the termination in bq24190_charger_set_charge_type()
796 switch (val->intval) { in bq24190_charger_set_charge_type()
811 return -EINVAL; in bq24190_charger_set_charge_type()
814 bdi->charge_type = val->intval; in bq24190_charger_set_charge_type()
817 * the charge-type until its gets disabled. in bq24190_charger_set_charge_type()
819 if (bdi->otg_vbus_enabled) in bq24190_charger_set_charge_type()
849 mutex_lock(&bdi->f_reg_lock); in bq24190_charger_get_health()
850 v = bdi->f_reg; in bq24190_charger_get_health()
851 mutex_unlock(&bdi->f_reg_lock); in bq24190_charger_get_health()
874 * This could be over-voltage or under-voltage in bq24190_charger_get_health()
877 * when its really under-voltage, just return in bq24190_charger_get_health()
889 health = -1; in bq24190_charger_get_health()
893 * This could be over-current or over-voltage but there's in bq24190_charger_get_health()
896 * even if it was over-current. in bq24190_charger_get_health()
903 val->intval = health; in bq24190_charger_get_health()
926 val->intval = pg_stat && !batfet_disable; in bq24190_charger_get_online()
976 val->intval = ++v * 128 * 1000; in bq24190_charger_get_precharge()
992 val->intval = ++v * 128 * 1000; in bq24190_charger_get_charge_term()
1015 /* If FORCE_20PCT is enabled, then current is 20% of ICHG value */ in bq24190_charger_get_current()
1019 val->intval = curr; in bq24190_charger_get_current()
1027 int ret, curr = val->intval; in bq24190_charger_set_current()
1039 if (curr > bdi->ichg_max) in bq24190_charger_set_current()
1040 return -EINVAL; in bq24190_charger_set_current()
1049 bdi->ichg = curr; in bq24190_charger_set_current()
1066 val->intval = voltage; in bq24190_charger_get_voltage()
1075 if (val->intval > bdi->vreg_max) in bq24190_charger_set_voltage()
1076 return -EINVAL; in bq24190_charger_set_voltage()
1081 ARRAY_SIZE(bq24190_cvc_vreg_values), val->intval); in bq24190_charger_set_voltage()
1085 bdi->vreg = val->intval; in bq24190_charger_set_voltage()
1103 val->intval = iinlimit; in bq24190_charger_get_iinlimit()
1114 ARRAY_SIZE(bq24190_isc_iinlim_values), val->intval); in bq24190_charger_set_iinlimit()
1123 dev_dbg(bdi->dev, "prop: %d\n", psp); in bq24190_charger_get_property()
1125 ret = pm_runtime_resume_and_get(bdi->dev); in bq24190_charger_get_property()
1155 val->intval = bdi->ichg_max; in bq24190_charger_get_property()
1162 val->intval = bdi->vreg_max; in bq24190_charger_get_property()
1169 val->intval = POWER_SUPPLY_SCOPE_SYSTEM; in bq24190_charger_get_property()
1173 val->strval = bdi->model_name; in bq24190_charger_get_property()
1177 val->strval = BQ24190_MANUFACTURER; in bq24190_charger_get_property()
1181 ret = -ENODATA; in bq24190_charger_get_property()
1184 pm_runtime_mark_last_busy(bdi->dev); in bq24190_charger_get_property()
1185 pm_runtime_put_autosuspend(bdi->dev); in bq24190_charger_get_property()
1197 dev_dbg(bdi->dev, "prop: %d\n", psp); in bq24190_charger_set_property()
1199 ret = pm_runtime_resume_and_get(bdi->dev); in bq24190_charger_set_property()
1223 ret = -EINVAL; in bq24190_charger_set_property()
1226 pm_runtime_mark_last_busy(bdi->dev); in bq24190_charger_set_property()
1227 pm_runtime_put_autosuspend(bdi->dev); in bq24190_charger_set_property()
1256 ret = power_supply_get_property_from_supplier(bdi->charger, in bq24190_input_current_limit_work()
1262 bq24190_charger_set_property(bdi->charger, in bq24190_input_current_limit_work()
1267 /* Sync the input-current-limit with our parent supply (if we have one) */
1273 * The Power-Good detection may take up to 220ms, sometimes in bq24190_charger_external_power_changed()
1277 * too low default 500mA iinlim. Delay setting the input-current-limit in bq24190_charger_external_power_changed()
1280 queue_delayed_work(system_wq, &bdi->input_current_limit_work, in bq24190_charger_external_power_changed()
1303 "main-battery",
1307 .name = "bq24190-charger",
1325 mutex_lock(&bdi->f_reg_lock); in bq24190_battery_get_status()
1326 chrg_fault = bdi->f_reg; in bq24190_battery_get_status()
1327 mutex_unlock(&bdi->f_reg_lock); in bq24190_battery_get_status()
1338 * - there is no good power source; in bq24190_battery_get_status()
1339 * - there is a charge fault. in bq24190_battery_get_status()
1353 case 0x1: /* Pre-charge */ in bq24190_battery_get_status()
1361 ret = -EIO; in bq24190_battery_get_status()
1366 val->intval = status; in bq24190_battery_get_status()
1377 mutex_lock(&bdi->f_reg_lock); in bq24190_battery_get_health()
1378 v = bdi->f_reg; in bq24190_battery_get_health()
1379 mutex_unlock(&bdi->f_reg_lock); in bq24190_battery_get_health()
1406 val->intval = health; in bq24190_battery_get_health()
1422 val->intval = !batfet_disable; in bq24190_battery_get_online()
1431 BQ24190_REG_MOC_BATFET_DISABLE_SHIFT, !val->intval); in bq24190_battery_set_online()
1447 val->intval = temp; in bq24190_battery_get_temp_alert_max()
1458 ARRAY_SIZE(bq24190_ictrc_treg_values), val->intval); in bq24190_battery_set_temp_alert_max()
1467 dev_warn(bdi->dev, "warning: /sys/class/power_supply/bq24190-battery is deprecated\n"); in bq24190_battery_get_property()
1468 dev_dbg(bdi->dev, "prop: %d\n", psp); in bq24190_battery_get_property()
1470 ret = pm_runtime_resume_and_get(bdi->dev); in bq24190_battery_get_property()
1485 /* Could be Li-on or Li-polymer but no way to tell which */ in bq24190_battery_get_property()
1486 val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN; in bq24190_battery_get_property()
1493 val->intval = POWER_SUPPLY_SCOPE_SYSTEM; in bq24190_battery_get_property()
1497 ret = -ENODATA; in bq24190_battery_get_property()
1500 pm_runtime_mark_last_busy(bdi->dev); in bq24190_battery_get_property()
1501 pm_runtime_put_autosuspend(bdi->dev); in bq24190_battery_get_property()
1513 dev_warn(bdi->dev, "warning: /sys/class/power_supply/bq24190-battery is deprecated\n"); in bq24190_battery_set_property()
1514 dev_dbg(bdi->dev, "prop: %d\n", psp); in bq24190_battery_set_property()
1516 ret = pm_runtime_resume_and_get(bdi->dev); in bq24190_battery_set_property()
1528 ret = -EINVAL; in bq24190_battery_set_property()
1531 pm_runtime_mark_last_busy(bdi->dev); in bq24190_battery_set_property()
1532 pm_runtime_put_autosuspend(bdi->dev); in bq24190_battery_set_property()
1564 .name = "bq24190-battery",
1579 ret = extcon_set_state_sync(bdi->edev, EXTCON_USB, otg_enabled); in bq24190_configure_usb_otg()
1581 dev_err(bdi->dev, "Can't set extcon state to %d: %d\n", in bq24190_configure_usb_otg()
1598 dev_err(bdi->dev, "Can't read SS reg: %d\n", ret); in bq24190_check_status()
1606 dev_err(bdi->dev, "Can't read F reg: %d\n", ret); in bq24190_check_status()
1616 if (f_reg != bdi->f_reg) { in bq24190_check_status()
1617 dev_warn(bdi->dev, in bq24190_check_status()
1624 mutex_lock(&bdi->f_reg_lock); in bq24190_check_status()
1625 if ((bdi->f_reg & battery_mask_f) != (f_reg & battery_mask_f)) in bq24190_check_status()
1627 if ((bdi->f_reg & ~battery_mask_f) != (f_reg & ~battery_mask_f)) in bq24190_check_status()
1629 bdi->f_reg = f_reg; in bq24190_check_status()
1630 mutex_unlock(&bdi->f_reg_lock); in bq24190_check_status()
1633 if (ss_reg != bdi->ss_reg) { in bq24190_check_status()
1635 * The device is in host mode so when PG_STAT goes from 1->0 in bq24190_check_status()
1638 if ((bdi->ss_reg & BQ24190_REG_SS_PG_STAT_MASK) && in bq24190_check_status()
1645 dev_err(bdi->dev, "Can't access ISC reg: %d\n", in bq24190_check_status()
1649 if ((bdi->ss_reg & battery_mask_ss) != (ss_reg & battery_mask_ss)) in bq24190_check_status()
1651 if ((bdi->ss_reg & ~battery_mask_ss) != (ss_reg & ~battery_mask_ss)) in bq24190_check_status()
1653 bdi->ss_reg = ss_reg; in bq24190_check_status()
1657 power_supply_changed(bdi->charger); in bq24190_check_status()
1660 if (alert_battery && bdi->battery) in bq24190_check_status()
1661 power_supply_changed(bdi->battery); in bq24190_check_status()
1663 dev_dbg(bdi->dev, "ss_reg: 0x%02x, f_reg: 0x%02x\n", ss_reg, f_reg); in bq24190_check_status()
1671 bdi->irq_event = true; in bq24190_irq_handler_thread()
1672 error = pm_runtime_resume_and_get(bdi->dev); in bq24190_irq_handler_thread()
1674 dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", error); in bq24190_irq_handler_thread()
1678 pm_runtime_mark_last_busy(bdi->dev); in bq24190_irq_handler_thread()
1679 pm_runtime_put_autosuspend(bdi->dev); in bq24190_irq_handler_thread()
1680 bdi->irq_event = false; in bq24190_irq_handler_thread()
1704 dev_err(bdi->dev, "Error unknown model: 0x%02x\n", v); in bq24190_hw_init()
1705 return -ENODEV; in bq24190_hw_init()
1716 return bq24190_read(bdi, BQ24190_REG_SS, &bdi->ss_reg); in bq24190_hw_init()
1721 const char * const s = "ti,system-minimum-microvolt"; in bq24190_get_config()
1725 idx = ARRAY_SIZE(bq24190_ccc_ichg_values) - 1; in bq24190_get_config()
1726 bdi->ichg_max = bq24190_ccc_ichg_values[idx]; in bq24190_get_config()
1728 idx = ARRAY_SIZE(bq24190_cvc_vreg_values) - 1; in bq24190_get_config()
1729 bdi->vreg_max = bq24190_cvc_vreg_values[idx]; in bq24190_get_config()
1731 if (device_property_read_u32(bdi->dev, s, &v) == 0) { in bq24190_get_config()
1735 bdi->sys_min = v; in bq24190_get_config()
1737 dev_warn(bdi->dev, "invalid value for %s: %u\n", s, v); in bq24190_get_config()
1740 if (!power_supply_get_battery_info(bdi->charger, &info)) { in bq24190_get_config()
1741 v = info->precharge_current_ua / 1000; in bq24190_get_config()
1744 bdi->iprechg = v; in bq24190_get_config()
1746 dev_warn(bdi->dev, "invalid value for battery:precharge-current-microamp: %d\n", in bq24190_get_config()
1749 v = info->charge_term_current_ua / 1000; in bq24190_get_config()
1752 bdi->iterm = v; in bq24190_get_config()
1754 dev_warn(bdi->dev, "invalid value for battery:charge-term-current-microamp: %d\n", in bq24190_get_config()
1758 v = info->constant_charge_current_max_ua; in bq24190_get_config()
1759 if (v >= bq24190_ccc_ichg_values[0] && v <= bdi->ichg_max) in bq24190_get_config()
1760 bdi->ichg = bdi->ichg_max = v; in bq24190_get_config()
1762 v = info->constant_charge_voltage_max_uv; in bq24190_get_config()
1763 if (v >= bq24190_cvc_vreg_values[0] && v <= bdi->vreg_max) in bq24190_get_config()
1764 bdi->vreg = bdi->vreg_max = v; in bq24190_get_config()
1773 struct i2c_adapter *adapter = client->adapter; in bq24190_probe()
1774 struct device *dev = &client->dev; in bq24190_probe()
1781 return -ENODEV; in bq24190_probe()
1787 return -ENOMEM; in bq24190_probe()
1790 bdi->client = client; in bq24190_probe()
1791 bdi->dev = dev; in bq24190_probe()
1792 strncpy(bdi->model_name, id->name, I2C_NAME_SIZE); in bq24190_probe()
1793 mutex_init(&bdi->f_reg_lock); in bq24190_probe()
1794 bdi->charge_type = POWER_SUPPLY_CHARGE_TYPE_FAST; in bq24190_probe()
1795 bdi->f_reg = 0; in bq24190_probe()
1796 bdi->ss_reg = BQ24190_REG_SS_VBUS_STAT_MASK; /* impossible state */ in bq24190_probe()
1797 INIT_DELAYED_WORK(&bdi->input_current_limit_work, in bq24190_probe()
1802 if (client->irq <= 0) { in bq24190_probe()
1804 return -EINVAL; in bq24190_probe()
1807 bdi->edev = devm_extcon_dev_allocate(dev, bq24190_usb_extcon_cable); in bq24190_probe()
1808 if (IS_ERR(bdi->edev)) in bq24190_probe()
1809 return PTR_ERR(bdi->edev); in bq24190_probe()
1811 ret = devm_extcon_dev_register(dev, bdi->edev); in bq24190_probe()
1830 charger_cfg.of_node = dev->of_node; in bq24190_probe()
1833 bdi->charger = power_supply_register(dev, &bq24190_charger_desc, in bq24190_probe()
1835 if (IS_ERR(bdi->charger)) { in bq24190_probe()
1837 ret = PTR_ERR(bdi->charger); in bq24190_probe()
1843 if (!device_property_read_bool(dev, "omit-battery-class")) { in bq24190_probe()
1845 bdi->battery = power_supply_register(dev, &bq24190_battery_desc, in bq24190_probe()
1847 if (IS_ERR(bdi->battery)) { in bq24190_probe()
1849 ret = PTR_ERR(bdi->battery); in bq24190_probe()
1866 ret = bq24190_configure_usb_otg(bdi, bdi->ss_reg); in bq24190_probe()
1870 bdi->initialized = true; in bq24190_probe()
1872 ret = devm_request_threaded_irq(dev, client->irq, NULL, in bq24190_probe()
1875 "bq24190-charger", bdi); in bq24190_probe()
1885 enable_irq_wake(client->irq); in bq24190_probe()
1893 if (!IS_ERR_OR_NULL(bdi->battery)) in bq24190_probe()
1894 power_supply_unregister(bdi->battery); in bq24190_probe()
1895 power_supply_unregister(bdi->charger); in bq24190_probe()
1909 error = pm_runtime_resume_and_get(bdi->dev); in bq24190_remove()
1911 dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", error); in bq24190_remove()
1914 if (bdi->battery) in bq24190_remove()
1915 power_supply_unregister(bdi->battery); in bq24190_remove()
1916 power_supply_unregister(bdi->charger); in bq24190_remove()
1918 pm_runtime_put_sync(bdi->dev); in bq24190_remove()
1919 pm_runtime_dont_use_autosuspend(bdi->dev); in bq24190_remove()
1920 pm_runtime_disable(bdi->dev); in bq24190_remove()
1936 if (!bdi->initialized) in bq24190_runtime_suspend()
1939 dev_dbg(bdi->dev, "%s\n", __func__); in bq24190_runtime_suspend()
1949 if (!bdi->initialized) in bq24190_runtime_resume()
1952 if (!bdi->irq_event) { in bq24190_runtime_resume()
1953 dev_dbg(bdi->dev, "checking events on possible wakeirq\n"); in bq24190_runtime_resume()
1966 error = pm_runtime_resume_and_get(bdi->dev); in bq24190_pm_suspend()
1968 dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", error); in bq24190_pm_suspend()
1973 pm_runtime_mark_last_busy(bdi->dev); in bq24190_pm_suspend()
1974 pm_runtime_put_autosuspend(bdi->dev); in bq24190_pm_suspend()
1986 bdi->f_reg = 0; in bq24190_pm_resume()
1987 bdi->ss_reg = BQ24190_REG_SS_VBUS_STAT_MASK; /* impossible state */ in bq24190_pm_resume()
1989 error = pm_runtime_resume_and_get(bdi->dev); in bq24190_pm_resume()
1991 dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", error); in bq24190_pm_resume()
1995 bq24190_read(bdi, BQ24190_REG_SS, &bdi->ss_reg); in bq24190_pm_resume()
1998 pm_runtime_mark_last_busy(bdi->dev); in bq24190_pm_resume()
1999 pm_runtime_put_autosuspend(bdi->dev); in bq24190_pm_resume()
2003 power_supply_changed(bdi->charger); in bq24190_pm_resume()
2004 if (bdi->battery) in bq24190_pm_resume()
2005 power_supply_changed(bdi->battery); in bq24190_pm_resume()
2040 .name = "bq24190-charger",