Lines Matching +full:charger +full:- +full:thermistor

1 // SPDX-License-Identifier: GPL-2.0
2 // BQ256XX Battery Charger Driver
3 // Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com/
153 * struct bq256xx_init_data -
177 * struct bq256xx_state -
209 * struct bq256xx_device -
213 * @charger: power supply registered for the charger
233 struct power_supply *charger; member
253 * struct bq256xx_chip_info -
342 return i - 1; in bq256xx_array_parse()
344 if (val >= array[array_size - 1]) in bq256xx_array_parse()
345 return array_size - 1; in bq256xx_array_parse()
351 if (val > array[i - 1] && val < array[i]) { in bq256xx_array_parse()
353 return i - 1; in bq256xx_array_parse()
358 return -EINVAL; in bq256xx_array_parse()
367 bq->usb_event = val; in bq256xx_usb_notifier()
368 queue_work(system_power_efficient_wq, &bq->usb_work); in bq256xx_usb_notifier()
378 switch (bq->usb_event) { in bq256xx_usb_work()
382 power_supply_changed(bq->charger); in bq256xx_usb_work()
385 dev_err(bq->dev, "Error switching to charger mode.\n"); in bq256xx_usb_work()
434 ret = regmap_read(bq->regmap, BQ256XX_CHARGER_STATUS_0, in bq256xx_get_state()
439 ret = regmap_read(bq->regmap, BQ256XX_CHARGER_STATUS_1, in bq256xx_get_state()
444 state->vbus_stat = charger_status_0 & BQ256XX_VBUS_STAT_MASK; in bq256xx_get_state()
445 state->chrg_stat = charger_status_0 & BQ256XX_CHRG_STAT_MASK; in bq256xx_get_state()
446 state->online = charger_status_0 & BQ256XX_PG_STAT_MASK; in bq256xx_get_state()
448 state->wdt_fault = charger_status_1 & BQ256XX_WDT_FAULT_MASK; in bq256xx_get_state()
449 state->bat_fault = charger_status_1 & BQ256XX_BAT_FAULT_MASK; in bq256xx_get_state()
450 state->chrg_fault = charger_status_1 & BQ256XX_CHRG_FAULT_MASK; in bq256xx_get_state()
451 state->ntc_fault = charger_status_1 & BQ256XX_NTC_FAULT_MASK; in bq256xx_get_state()
469 return -EINVAL; in bq256xx_set_charge_type()
472 return regmap_update_bits(bq->regmap, BQ256XX_CHARGER_CONTROL_0, in bq256xx_set_charge_type()
483 ret = regmap_read(bq->regmap, BQ256XX_CHARGE_CURRENT_LIMIT, in bq256xx_get_ichg_curr()
499 ret = regmap_read(bq->regmap, BQ256XX_CHARGE_CURRENT_LIMIT, in bq25618_619_get_ichg_curr()
509 return bq25618_619_ichg_values[ichg_reg_code - BQ25618_ICHG_THRESH]; in bq25618_619_get_ichg_curr()
515 int ichg_max = bq->init_data.ichg_max; in bq256xx_set_ichg_curr()
520 return regmap_update_bits(bq->regmap, BQ256XX_CHARGE_CURRENT_LIMIT, in bq256xx_set_ichg_curr()
528 int ichg_max = bq->init_data.ichg_max; in bq25618_619_set_ichg_curr()
539 return regmap_update_bits(bq->regmap, BQ256XX_CHARGE_CURRENT_LIMIT, in bq25618_619_set_ichg_curr()
549 ret = regmap_read(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT, in bq25618_619_get_chrg_volt()
559 return ((vbatreg_reg_code - BQ2561X_VBATREG_THRESH) * in bq25618_619_get_chrg_volt()
572 ret = regmap_read(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT, in bq25611d_get_chrg_volt()
581 return ((vbatreg_reg_code - BQ2561X_VBATREG_THRESH) * in bq25611d_get_chrg_volt()
594 ret = regmap_read(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT, in bq2560x_get_chrg_volt()
612 ret = regmap_read(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT, in bq25601d_get_chrg_volt()
628 int vbatreg_max = bq->init_data.vbatreg_max; in bq25618_619_set_chrg_volt()
633 vbatreg_reg_code = ((vbatreg - in bq25618_619_set_chrg_volt()
641 return regmap_update_bits(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT, in bq25618_619_set_chrg_volt()
650 int vbatreg_max = bq->init_data.vbatreg_max; in bq25611d_set_chrg_volt()
655 vbatreg_reg_code = ((vbatreg - in bq25611d_set_chrg_volt()
663 return regmap_update_bits(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT, in bq25611d_set_chrg_volt()
671 int vbatreg_max = bq->init_data.vbatreg_max; in bq2560x_set_chrg_volt()
675 vbatreg_reg_code = (vbatreg - BQ2560X_VBATREG_OFFSET_uV) / in bq2560x_set_chrg_volt()
678 return regmap_update_bits(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT, in bq2560x_set_chrg_volt()
686 int vbatreg_max = bq->init_data.vbatreg_max; in bq25601d_set_chrg_volt()
690 vbatreg_reg_code = (vbatreg - BQ25601D_VBATREG_OFFSET_uV) / in bq25601d_set_chrg_volt()
693 return regmap_update_bits(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT, in bq25601d_set_chrg_volt()
700 return regmap_update_bits(bq->regmap, BQ256XX_INPUT_CURRENT_LIMIT, in bq256xx_set_ts_ignore()
710 ret = regmap_read(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM, in bq256xx_get_prechrg_curr()
729 iprechg_reg_code = ((iprechg - BQ256XX_IPRECHG_OFFSET_uA) / in bq256xx_set_prechrg_curr()
732 return regmap_update_bits(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM, in bq256xx_set_prechrg_curr()
742 ret = regmap_read(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM, in bq25618_619_get_prechrg_curr()
761 iprechg_reg_code = ((iprechg - BQ25618_IPRECHG_OFFSET_uA) / in bq25618_619_set_prechrg_curr()
764 return regmap_update_bits(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM, in bq25618_619_set_prechrg_curr()
774 ret = regmap_read(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM, in bq256xx_get_term_curr()
791 iterm_reg_code = (iterm - BQ256XX_ITERM_OFFSET_uA) / in bq256xx_set_term_curr()
794 return regmap_update_bits(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM, in bq256xx_set_term_curr()
804 ret = regmap_read(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM, in bq25618_619_get_term_curr()
821 iterm_reg_code = (iterm - BQ25618_ITERM_OFFSET_uA) / in bq25618_619_set_term_curr()
824 return regmap_update_bits(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM, in bq25618_619_set_term_curr()
834 ret = regmap_read(bq->regmap, BQ256XX_CHARGER_CONTROL_2, in bq256xx_get_input_volt_lim()
851 vindpm_reg_code = (vindpm - BQ256XX_VINDPM_OFFSET_uV) / in bq256xx_set_input_volt_lim()
854 return regmap_update_bits(bq->regmap, BQ256XX_CHARGER_CONTROL_2, in bq256xx_set_input_volt_lim()
864 ret = regmap_read(bq->regmap, BQ256XX_INPUT_CURRENT_LIMIT, in bq256xx_get_input_curr_lim()
881 iindpm_reg_code = (iindpm - BQ256XX_IINDPM_OFFSET_uA) / in bq256xx_set_input_curr_lim()
884 return regmap_update_bits(bq->regmap, BQ256XX_INPUT_CURRENT_LIMIT, in bq256xx_set_input_curr_lim()
892 regmap_update_bits(bq->regmap, BQ256XX_PART_INFORMATION, in bq256xx_charger_reset()
895 if (!IS_ERR_OR_NULL(bq->usb2_phy)) in bq256xx_charger_reset()
896 usb_unregister_notifier(bq->usb2_phy, &bq->usb_nb); in bq256xx_charger_reset()
898 if (!IS_ERR_OR_NULL(bq->usb3_phy)) in bq256xx_charger_reset()
899 usb_unregister_notifier(bq->usb3_phy, &bq->usb_nb); in bq256xx_charger_reset()
907 int ret = -EINVAL; in bq256xx_set_charger_property()
911 ret = bq->chip_info->bq256xx_set_iindpm(bq, val->intval); in bq256xx_set_charger_property()
920 ret = bq->chip_info->bq256xx_set_vbatreg(bq, val->intval); in bq256xx_set_charger_property()
926 ret = bq->chip_info->bq256xx_set_ichg(bq, val->intval); in bq256xx_set_charger_property()
932 ret = bq->chip_info->bq256xx_set_iprechg(bq, val->intval); in bq256xx_set_charger_property()
938 ret = bq->chip_info->bq256xx_set_iterm(bq, val->intval); in bq256xx_set_charger_property()
944 ret = bq->chip_info->bq256xx_set_vindpm(bq, val->intval); in bq256xx_set_charger_property()
950 ret = bq->chip_info->bq256xx_set_charge_type(bq, val->intval); in bq256xx_set_charger_property()
971 val->intval = bq->init_data.ichg_max; in bq256xx_get_battery_property()
975 val->intval = bq->init_data.vbatreg_max; in bq256xx_get_battery_property()
979 return -EINVAL; in bq256xx_get_battery_property()
993 mutex_lock(&bq->lock); in bq256xx_get_charger_property()
995 mutex_unlock(&bq->lock); in bq256xx_get_charger_property()
1003 val->intval = POWER_SUPPLY_STATUS_DISCHARGING; in bq256xx_get_charger_property()
1005 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; in bq256xx_get_charger_property()
1007 val->intval = POWER_SUPPLY_STATUS_FULL; in bq256xx_get_charger_property()
1009 val->intval = POWER_SUPPLY_STATUS_CHARGING; in bq256xx_get_charger_property()
1013 val->intval = POWER_SUPPLY_HEALTH_UNKNOWN; in bq256xx_get_charger_property()
1015 val->intval = in bq256xx_get_charger_property()
1018 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE; in bq256xx_get_charger_property()
1022 val->intval = in bq256xx_get_charger_property()
1026 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; in bq256xx_get_charger_property()
1029 val->intval = in bq256xx_get_charger_property()
1038 val->intval = POWER_SUPPLY_HEALTH_WARM; in bq256xx_get_charger_property()
1041 val->intval = POWER_SUPPLY_HEALTH_COOL; in bq256xx_get_charger_property()
1044 val->intval = POWER_SUPPLY_HEALTH_COLD; in bq256xx_get_charger_property()
1047 val->intval = POWER_SUPPLY_HEALTH_HOT; in bq256xx_get_charger_property()
1050 val->intval = POWER_SUPPLY_HEALTH_GOOD; in bq256xx_get_charger_property()
1057 if (bq->chip_info->has_usb_detect) { in bq256xx_get_charger_property()
1060 val->intval = POWER_SUPPLY_USB_TYPE_SDP; in bq256xx_get_charger_property()
1063 val->intval = POWER_SUPPLY_USB_TYPE_CDP; in bq256xx_get_charger_property()
1066 val->intval = POWER_SUPPLY_USB_TYPE_DCP; in bq256xx_get_charger_property()
1069 val->intval = POWER_SUPPLY_USB_TYPE_ACA; in bq256xx_get_charger_property()
1072 val->intval = POWER_SUPPLY_USB_TYPE_UNKNOWN; in bq256xx_get_charger_property()
1078 val->intval = POWER_SUPPLY_USB_TYPE_SDP; in bq256xx_get_charger_property()
1081 val->intval = POWER_SUPPLY_USB_TYPE_ACA; in bq256xx_get_charger_property()
1084 val->intval = POWER_SUPPLY_USB_TYPE_UNKNOWN; in bq256xx_get_charger_property()
1093 val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE; in bq256xx_get_charger_property()
1096 val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE; in bq256xx_get_charger_property()
1099 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST; in bq256xx_get_charger_property()
1102 val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE; in bq256xx_get_charger_property()
1105 val->intval = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN; in bq256xx_get_charger_property()
1110 val->strval = BQ256XX_MANUFACTURER; in bq256xx_get_charger_property()
1114 val->strval = bq->model_name; in bq256xx_get_charger_property()
1118 val->intval = state.online; in bq256xx_get_charger_property()
1122 ret = bq->chip_info->bq256xx_get_vindpm(bq); in bq256xx_get_charger_property()
1125 val->intval = ret; in bq256xx_get_charger_property()
1129 ret = bq->chip_info->bq256xx_get_iindpm(bq); in bq256xx_get_charger_property()
1132 val->intval = ret; in bq256xx_get_charger_property()
1136 ret = bq->chip_info->bq256xx_get_vbatreg(bq); in bq256xx_get_charger_property()
1139 val->intval = ret; in bq256xx_get_charger_property()
1143 ret = bq->chip_info->bq256xx_get_ichg(bq); in bq256xx_get_charger_property()
1146 val->intval = ret; in bq256xx_get_charger_property()
1150 ret = bq->chip_info->bq256xx_get_iprechg(bq); in bq256xx_get_charger_property()
1153 val->intval = ret; in bq256xx_get_charger_property()
1157 ret = bq->chip_info->bq256xx_get_iterm(bq); in bq256xx_get_charger_property()
1160 val->intval = ret; in bq256xx_get_charger_property()
1164 return -EINVAL; in bq256xx_get_charger_property()
1175 mutex_lock(&bq->lock); in bq256xx_state_changed()
1176 old_state = bq->state; in bq256xx_state_changed()
1177 mutex_unlock(&bq->lock); in bq256xx_state_changed()
1195 mutex_lock(&bq->lock); in bq256xx_irq_handler_thread()
1196 bq->state = state; in bq256xx_irq_handler_thread()
1197 mutex_unlock(&bq->lock); in bq256xx_irq_handler_thread()
1199 power_supply_changed(bq->charger); in bq256xx_irq_handler_thread()
1245 .name = "bq256xx-charger",
1260 .name = "bq256xx-battery",
1542 bq->charger = devm_power_supply_register(bq->dev, in bq256xx_power_supply_init()
1545 if (IS_ERR(bq->charger)) { in bq256xx_power_supply_init()
1546 dev_err(dev, "power supply register charger failed\n"); in bq256xx_power_supply_init()
1547 return PTR_ERR(bq->charger); in bq256xx_power_supply_init()
1550 bq->battery = devm_power_supply_register(bq->dev, in bq256xx_power_supply_init()
1553 if (IS_ERR(bq->battery)) { in bq256xx_power_supply_init()
1555 return PTR_ERR(bq->battery); in bq256xx_power_supply_init()
1568 if (bq->watchdog_timer == bq256xx_watchdog_time[i]) { in bq256xx_hw_init()
1573 bq->watchdog_timer > bq256xx_watchdog_time[i] && in bq256xx_hw_init()
1574 bq->watchdog_timer < bq256xx_watchdog_time[i + 1]) in bq256xx_hw_init()
1577 ret = regmap_update_bits(bq->regmap, BQ256XX_CHARGER_CONTROL_1, in bq256xx_hw_init()
1583 ret = power_supply_get_battery_info(bq->charger, &bat_info); in bq256xx_hw_init()
1584 if (ret == -ENOMEM) in bq256xx_hw_init()
1588 dev_warn(bq->dev, "battery info missing, default values will be applied\n"); in bq256xx_hw_init()
1590 bat_info->constant_charge_current_max_ua = in bq256xx_hw_init()
1591 bq->chip_info->bq256xx_def_ichg; in bq256xx_hw_init()
1593 bat_info->constant_charge_voltage_max_uv = in bq256xx_hw_init()
1594 bq->chip_info->bq256xx_def_vbatreg; in bq256xx_hw_init()
1596 bat_info->precharge_current_ua = in bq256xx_hw_init()
1597 bq->chip_info->bq256xx_def_iprechg; in bq256xx_hw_init()
1599 bat_info->charge_term_current_ua = in bq256xx_hw_init()
1600 bq->chip_info->bq256xx_def_iterm; in bq256xx_hw_init()
1602 bq->init_data.ichg_max = in bq256xx_hw_init()
1603 bq->chip_info->bq256xx_max_ichg; in bq256xx_hw_init()
1605 bq->init_data.vbatreg_max = in bq256xx_hw_init()
1606 bq->chip_info->bq256xx_max_vbatreg; in bq256xx_hw_init()
1608 bq->init_data.ichg_max = in bq256xx_hw_init()
1609 bat_info->constant_charge_current_max_ua; in bq256xx_hw_init()
1611 bq->init_data.vbatreg_max = in bq256xx_hw_init()
1612 bat_info->constant_charge_voltage_max_uv; in bq256xx_hw_init()
1615 ret = bq->chip_info->bq256xx_set_vindpm(bq, bq->init_data.vindpm); in bq256xx_hw_init()
1619 ret = bq->chip_info->bq256xx_set_iindpm(bq, bq->init_data.iindpm); in bq256xx_hw_init()
1623 ret = bq->chip_info->bq256xx_set_ichg(bq, in bq256xx_hw_init()
1624 bq->chip_info->bq256xx_def_ichg); in bq256xx_hw_init()
1628 ret = bq->chip_info->bq256xx_set_iprechg(bq, in bq256xx_hw_init()
1629 bat_info->precharge_current_ua); in bq256xx_hw_init()
1633 ret = bq->chip_info->bq256xx_set_vbatreg(bq, in bq256xx_hw_init()
1634 bq->chip_info->bq256xx_def_vbatreg); in bq256xx_hw_init()
1638 ret = bq->chip_info->bq256xx_set_iterm(bq, in bq256xx_hw_init()
1639 bat_info->charge_term_current_ua); in bq256xx_hw_init()
1643 if (bq->chip_info->bq256xx_set_ts_ignore) { in bq256xx_hw_init()
1644 ret = bq->chip_info->bq256xx_set_ts_ignore(bq, bq->init_data.ts_ignore); in bq256xx_hw_init()
1649 power_supply_put_battery_info(bq->charger, bat_info); in bq256xx_hw_init()
1659 psy_cfg->drv_data = bq; in bq256xx_parse_dt()
1660 psy_cfg->of_node = dev->of_node; in bq256xx_parse_dt()
1662 ret = device_property_read_u32(bq->dev, "ti,watchdog-timeout-ms", in bq256xx_parse_dt()
1663 &bq->watchdog_timer); in bq256xx_parse_dt()
1665 bq->watchdog_timer = BQ256XX_WATCHDOG_DIS; in bq256xx_parse_dt()
1667 if (bq->watchdog_timer > BQ256XX_WATCHDOG_MAX || in bq256xx_parse_dt()
1668 bq->watchdog_timer < BQ256XX_WATCHDOG_DIS) in bq256xx_parse_dt()
1669 return -EINVAL; in bq256xx_parse_dt()
1671 ret = device_property_read_u32(bq->dev, in bq256xx_parse_dt()
1672 "input-voltage-limit-microvolt", in bq256xx_parse_dt()
1673 &bq->init_data.vindpm); in bq256xx_parse_dt()
1675 bq->init_data.vindpm = bq->chip_info->bq256xx_def_vindpm; in bq256xx_parse_dt()
1677 ret = device_property_read_u32(bq->dev, in bq256xx_parse_dt()
1678 "input-current-limit-microamp", in bq256xx_parse_dt()
1679 &bq->init_data.iindpm); in bq256xx_parse_dt()
1681 bq->init_data.iindpm = bq->chip_info->bq256xx_def_iindpm; in bq256xx_parse_dt()
1683 bq->init_data.ts_ignore = device_property_read_bool(bq->dev, "ti,no-thermistor"); in bq256xx_parse_dt()
1691 struct device *dev = &client->dev; in bq256xx_probe()
1699 return -ENOMEM; in bq256xx_probe()
1701 bq->client = client; in bq256xx_probe()
1702 bq->dev = dev; in bq256xx_probe()
1703 bq->chip_info = i2c_get_match_data(client); in bq256xx_probe()
1705 mutex_init(&bq->lock); in bq256xx_probe()
1707 strscpy(bq->model_name, id->name, sizeof(bq->model_name)); in bq256xx_probe()
1709 bq->regmap = devm_regmap_init_i2c(client, in bq256xx_probe()
1710 bq->chip_info->bq256xx_regmap_config); in bq256xx_probe()
1712 if (IS_ERR(bq->regmap)) { in bq256xx_probe()
1714 return PTR_ERR(bq->regmap); in bq256xx_probe()
1730 bq->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2); in bq256xx_probe()
1731 if (!IS_ERR_OR_NULL(bq->usb2_phy)) { in bq256xx_probe()
1732 INIT_WORK(&bq->usb_work, bq256xx_usb_work); in bq256xx_probe()
1733 bq->usb_nb.notifier_call = bq256xx_usb_notifier; in bq256xx_probe()
1734 usb_register_notifier(bq->usb2_phy, &bq->usb_nb); in bq256xx_probe()
1737 bq->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3); in bq256xx_probe()
1738 if (!IS_ERR_OR_NULL(bq->usb3_phy)) { in bq256xx_probe()
1739 INIT_WORK(&bq->usb_work, bq256xx_usb_work); in bq256xx_probe()
1740 bq->usb_nb.notifier_call = bq256xx_usb_notifier; in bq256xx_probe()
1741 usb_register_notifier(bq->usb3_phy, &bq->usb_nb); in bq256xx_probe()
1744 if (client->irq) { in bq256xx_probe()
1745 ret = devm_request_threaded_irq(dev, client->irq, NULL, in bq256xx_probe()
1749 dev_name(&client->dev), bq); in bq256xx_probe()
1809 .name = "bq256xx-charger",
1818 MODULE_AUTHOR("Ricardo Rivera-Matos <r-rivera-matos@ti.com>");
1819 MODULE_DESCRIPTION("bq256xx charger driver");