Lines Matching +full:li +full:- +full:ion
1 // SPDX-License-Identifier: GPL-2.0-only
4 * This driver is for the multi-block Switch-Mode Battery Charger and Boost
6 * integrated, single-cell lithium-ion battery charger.
8 * Sub-components:
9 * - Charger core
10 * - Buck
11 * - DC charge-path
12 * - USB charge-path
13 * - Battery interface
14 * - Boost (not implemented)
15 * - Misc
16 * - HF-Buck
29 #include <linux/extcon-provider.h>
141 return 5600000 + (index - 43) * 200000; in smbb_vin_fn()
188 .name = "qcom,fast-charge-safe-current",
196 .name = "qcom,fast-charge-current-limit",
204 .name = "qcom,dc-current-limit",
211 .name = "qcom,fast-charge-safe-voltage",
219 .name = "qcom,fast-charge-high-threshold-voltage",
227 .name = "qcom,fast-charge-low-threshold-voltage",
234 .name = "qcom,auto-recharge-threshold-voltage",
241 .name = "qcom,minimum-input-voltage",
248 .name = "usb-charge-current-limit",
266 if (val > prop->max || val < prop->min) { in smbb_charger_attr_write()
267 dev_err(chg->dev, "value out of range for %s [%u:%u]\n", in smbb_charger_attr_write()
268 prop->name, prop->min, prop->max); in smbb_charger_attr_write()
269 return -EINVAL; in smbb_charger_attr_write()
272 if (prop->safe_reg) { in smbb_charger_attr_write()
273 rc = regmap_read(chg->regmap, in smbb_charger_attr_write()
274 chg->addr + prop->safe_reg, &wval); in smbb_charger_attr_write()
276 dev_err(chg->dev, in smbb_charger_attr_write()
278 prop->name); in smbb_charger_attr_write()
282 wval = prop->hw_fn(wval); in smbb_charger_attr_write()
285 dev_warn(chg->dev, in smbb_charger_attr_write()
287 prop->name, wval); in smbb_charger_attr_write()
292 wval = smbb_hw_lookup(val, prop->hw_fn); in smbb_charger_attr_write()
294 rc = regmap_write(chg->regmap, chg->addr + prop->reg, wval); in smbb_charger_attr_write()
296 dev_err(chg->dev, "unable to update %s", prop->name); in smbb_charger_attr_write()
299 out = prop->hw_fn(wval); in smbb_charger_attr_write()
301 dev_warn(chg->dev, in smbb_charger_attr_write()
303 prop->name, out); in smbb_charger_attr_write()
306 dev_dbg(chg->dev, "%s <= %d\n", prop->name, out); in smbb_charger_attr_write()
308 chg->attr[which] = out; in smbb_charger_attr_write()
322 rc = regmap_read(chg->regmap, chg->addr + prop->reg, &val); in smbb_charger_attr_read()
324 dev_err(chg->dev, "failed to read %s\n", prop->name); in smbb_charger_attr_read()
327 val = prop->hw_fn(val); in smbb_charger_attr_read()
328 dev_dbg(chg->dev, "%s => %d\n", prop->name, val); in smbb_charger_attr_read()
330 chg->attr[which] = val; in smbb_charger_attr_read()
344 rc = of_property_read_u32(chg->dev->of_node, prop->name, &val); in smbb_charger_attr_parse()
347 if (!rc || !prop->fail_ok) in smbb_charger_attr_parse()
360 dev_err(chg->dev, "failed to read irq line\n"); in smbb_set_line_flag()
364 mutex_lock(&chg->statlock); in smbb_set_line_flag()
366 chg->status |= flag; in smbb_set_line_flag()
368 chg->status &= ~flag; in smbb_set_line_flag()
369 mutex_unlock(&chg->statlock); in smbb_set_line_flag()
371 dev_dbg(chg->dev, "status = %03lx\n", chg->status); in smbb_set_line_flag()
379 extcon_set_state_sync(chg->edev, EXTCON_USB, in smbb_usb_valid_handler()
380 chg->status & STATUS_USBIN_VALID); in smbb_usb_valid_handler()
381 power_supply_changed(chg->usb_psy); in smbb_usb_valid_handler()
391 if (!chg->dc_disabled) in smbb_dc_valid_handler()
392 power_supply_changed(chg->dc_psy); in smbb_dc_valid_handler()
403 rc = regmap_read(chg->regmap, chg->addr + SMBB_BAT_TEMP_STATUS, &val); in smbb_bat_temp_handler()
407 mutex_lock(&chg->statlock); in smbb_bat_temp_handler()
409 chg->status |= STATUS_BAT_OK; in smbb_bat_temp_handler()
411 chg->status &= ~STATUS_BAT_OK; in smbb_bat_temp_handler()
413 chg->status |= STATUS_BAT_HOT; in smbb_bat_temp_handler()
415 mutex_unlock(&chg->statlock); in smbb_bat_temp_handler()
417 power_supply_changed(chg->bat_psy); in smbb_bat_temp_handler()
426 power_supply_changed(chg->bat_psy); in smbb_bat_present_handler()
436 power_supply_changed(chg->bat_psy); in smbb_chg_done_handler()
446 power_supply_changed(chg->bat_psy); in smbb_chg_gone_handler()
447 power_supply_changed(chg->usb_psy); in smbb_chg_gone_handler()
448 if (!chg->dc_disabled) in smbb_chg_gone_handler()
449 power_supply_changed(chg->dc_psy); in smbb_chg_gone_handler()
459 power_supply_changed(chg->bat_psy); in smbb_chg_fast_handler()
469 power_supply_changed(chg->bat_psy); in smbb_chg_trkl_handler()
478 { "chg-done", smbb_chg_done_handler },
479 { "chg-fast", smbb_chg_fast_handler },
480 { "chg-trkl", smbb_chg_trkl_handler },
481 { "bat-temp-ok", smbb_bat_temp_handler },
482 { "bat-present", smbb_bat_present_handler },
483 { "chg-gone", smbb_chg_gone_handler },
484 { "usb-valid", smbb_usb_valid_handler },
485 { "dc-valid", smbb_dc_valid_handler },
497 mutex_lock(&chg->statlock); in smbb_usbin_get_property()
498 val->intval = !(chg->status & STATUS_CHG_GONE) && in smbb_usbin_get_property()
499 (chg->status & STATUS_USBIN_VALID); in smbb_usbin_get_property()
500 mutex_unlock(&chg->statlock); in smbb_usbin_get_property()
503 val->intval = chg->attr[ATTR_USBIN_IMAX]; in smbb_usbin_get_property()
506 val->intval = 2500000; in smbb_usbin_get_property()
509 rc = -EINVAL; in smbb_usbin_get_property()
526 val->intval); in smbb_usbin_set_property()
529 rc = -EINVAL; in smbb_usbin_set_property()
545 mutex_lock(&chg->statlock); in smbb_dcin_get_property()
546 val->intval = !(chg->status & STATUS_CHG_GONE) && in smbb_dcin_get_property()
547 (chg->status & STATUS_DCIN_VALID); in smbb_dcin_get_property()
548 mutex_unlock(&chg->statlock); in smbb_dcin_get_property()
551 val->intval = chg->attr[ATTR_DCIN_IMAX]; in smbb_dcin_get_property()
554 val->intval = 2500000; in smbb_dcin_get_property()
557 rc = -EINVAL; in smbb_dcin_get_property()
574 val->intval); in smbb_dcin_set_property()
577 rc = -EINVAL; in smbb_dcin_set_property()
598 mutex_lock(&chg->statlock); in smbb_battery_get_property()
599 status = chg->status; in smbb_battery_get_property()
600 mutex_unlock(&chg->statlock); in smbb_battery_get_property()
605 val->intval = POWER_SUPPLY_STATUS_DISCHARGING; in smbb_battery_get_property()
607 val->intval = POWER_SUPPLY_STATUS_DISCHARGING; in smbb_battery_get_property()
609 val->intval = POWER_SUPPLY_STATUS_FULL; in smbb_battery_get_property()
611 val->intval = POWER_SUPPLY_STATUS_DISCHARGING; in smbb_battery_get_property()
613 val->intval = POWER_SUPPLY_STATUS_CHARGING; in smbb_battery_get_property()
615 val->intval = POWER_SUPPLY_STATUS_DISCHARGING; in smbb_battery_get_property()
619 val->intval = POWER_SUPPLY_HEALTH_GOOD; in smbb_battery_get_property()
621 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; in smbb_battery_get_property()
623 val->intval = POWER_SUPPLY_HEALTH_COLD; in smbb_battery_get_property()
627 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST; in smbb_battery_get_property()
629 val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE; in smbb_battery_get_property()
631 val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE; in smbb_battery_get_property()
634 val->intval = !!(status & STATUS_BAT_PRESENT); in smbb_battery_get_property()
637 val->intval = chg->attr[ATTR_BAT_IMAX]; in smbb_battery_get_property()
640 val->intval = chg->attr[ATTR_BAT_VMAX]; in smbb_battery_get_property()
643 /* this charger is a single-cell lithium-ion battery charger in smbb_battery_get_property()
647 val->intval = POWER_SUPPLY_TECHNOLOGY_LION; in smbb_battery_get_property()
650 val->intval = 3000000; /* single-cell li-ion low end */ in smbb_battery_get_property()
653 rc = -EINVAL; in smbb_battery_get_property()
669 rc = smbb_charger_attr_write(chg, ATTR_BAT_IMAX, val->intval); in smbb_battery_set_property()
672 rc = smbb_charger_attr_write(chg, ATTR_BAT_VMAX, val->intval); in smbb_battery_set_property()
675 rc = -EINVAL; in smbb_battery_set_property()
756 static char *smbb_bif[] = { "smbb-bif" };
759 .name = "smbb-bif",
769 .name = "smbb-usbin",
779 .name = "smbb-dcin",
793 rc = regmap_update_bits(chg->regmap, chg->addr + SMBB_USB_OTG_CTL, in smbb_chg_otg_enable()
796 dev_err(chg->dev, "failed to update OTG_CTL\n"); in smbb_chg_otg_enable()
805 rc = regmap_update_bits(chg->regmap, chg->addr + SMBB_USB_OTG_CTL, in smbb_chg_otg_disable()
808 dev_err(chg->dev, "failed to update OTG_CTL\n"); in smbb_chg_otg_disable()
818 rc = regmap_read(chg->regmap, chg->addr + SMBB_USB_OTG_CTL, &value); in smbb_chg_otg_is_enabled()
820 dev_err(chg->dev, "failed to read OTG_CTL\n"); in smbb_chg_otg_is_enabled()
840 chg = devm_kzalloc(&pdev->dev, sizeof(*chg), GFP_KERNEL); in smbb_charger_probe()
842 return -ENOMEM; in smbb_charger_probe()
844 chg->dev = &pdev->dev; in smbb_charger_probe()
845 mutex_init(&chg->statlock); in smbb_charger_probe()
847 chg->regmap = dev_get_regmap(pdev->dev.parent, NULL); in smbb_charger_probe()
848 if (!chg->regmap) { in smbb_charger_probe()
849 dev_err(&pdev->dev, "failed to locate regmap\n"); in smbb_charger_probe()
850 return -ENODEV; in smbb_charger_probe()
853 rc = of_property_read_u32(pdev->dev.of_node, "reg", &chg->addr); in smbb_charger_probe()
855 dev_err(&pdev->dev, "missing or invalid 'reg' property\n"); in smbb_charger_probe()
859 rc = regmap_read(chg->regmap, chg->addr + SMBB_MISC_REV2, &chg->revision); in smbb_charger_probe()
861 dev_err(&pdev->dev, "unable to read revision\n"); in smbb_charger_probe()
865 chg->revision += 1; in smbb_charger_probe()
866 if (chg->revision != 1 && chg->revision != 2 && chg->revision != 3) { in smbb_charger_probe()
867 dev_err(&pdev->dev, "v%d hardware not supported\n", chg->revision); in smbb_charger_probe()
868 return -ENODEV; in smbb_charger_probe()
870 dev_info(&pdev->dev, "Initializing SMBB rev %u", chg->revision); in smbb_charger_probe()
872 chg->dc_disabled = of_property_read_bool(pdev->dev.of_node, "qcom,disable-dc"); in smbb_charger_probe()
877 dev_err(&pdev->dev, "failed to parse/apply settings\n"); in smbb_charger_probe()
883 bat_cfg.of_node = pdev->dev.of_node; in smbb_charger_probe()
884 chg->bat_psy = devm_power_supply_register(&pdev->dev, in smbb_charger_probe()
887 if (IS_ERR(chg->bat_psy)) { in smbb_charger_probe()
888 dev_err(&pdev->dev, "failed to register battery\n"); in smbb_charger_probe()
889 return PTR_ERR(chg->bat_psy); in smbb_charger_probe()
895 chg->usb_psy = devm_power_supply_register(&pdev->dev, in smbb_charger_probe()
898 if (IS_ERR(chg->usb_psy)) { in smbb_charger_probe()
899 dev_err(&pdev->dev, "failed to register USB power supply\n"); in smbb_charger_probe()
900 return PTR_ERR(chg->usb_psy); in smbb_charger_probe()
903 chg->edev = devm_extcon_dev_allocate(&pdev->dev, smbb_usb_extcon_cable); in smbb_charger_probe()
904 if (IS_ERR(chg->edev)) { in smbb_charger_probe()
905 dev_err(&pdev->dev, "failed to allocate extcon device\n"); in smbb_charger_probe()
906 return -ENOMEM; in smbb_charger_probe()
909 rc = devm_extcon_dev_register(&pdev->dev, chg->edev); in smbb_charger_probe()
911 dev_err(&pdev->dev, "failed to register extcon device\n"); in smbb_charger_probe()
915 if (!chg->dc_disabled) { in smbb_charger_probe()
919 chg->dc_psy = devm_power_supply_register(&pdev->dev, in smbb_charger_probe()
922 if (IS_ERR(chg->dc_psy)) { in smbb_charger_probe()
923 dev_err(&pdev->dev, "failed to register DC power supply\n"); in smbb_charger_probe()
924 return PTR_ERR(chg->dc_psy); in smbb_charger_probe()
937 rc = devm_request_threaded_irq(&pdev->dev, irq, NULL, in smbb_charger_probe()
941 dev_err(&pdev->dev, "failed to request irq '%s'\n", in smbb_charger_probe()
951 chg->otg_rdesc.id = -1; in smbb_charger_probe()
952 chg->otg_rdesc.name = "otg-vbus"; in smbb_charger_probe()
953 chg->otg_rdesc.ops = &smbb_chg_otg_ops; in smbb_charger_probe()
954 chg->otg_rdesc.owner = THIS_MODULE; in smbb_charger_probe()
955 chg->otg_rdesc.type = REGULATOR_VOLTAGE; in smbb_charger_probe()
956 chg->otg_rdesc.supply_name = "usb-otg-in"; in smbb_charger_probe()
957 chg->otg_rdesc.of_match = "otg-vbus"; in smbb_charger_probe()
959 config.dev = &pdev->dev; in smbb_charger_probe()
962 chg->otg_reg = devm_regulator_register(&pdev->dev, &chg->otg_rdesc, in smbb_charger_probe()
964 if (IS_ERR(chg->otg_reg)) in smbb_charger_probe()
965 return PTR_ERR(chg->otg_reg); in smbb_charger_probe()
967 chg->jeita_ext_temp = of_property_read_bool(pdev->dev.of_node, in smbb_charger_probe()
968 "qcom,jeita-extended-temp-range"); in smbb_charger_probe()
971 rc = regmap_update_bits(chg->regmap, chg->addr + SMBB_BAT_BTC_CTRL, in smbb_charger_probe()
973 chg->jeita_ext_temp ? in smbb_charger_probe()
977 dev_err(&pdev->dev, in smbb_charger_probe()
979 chg->jeita_ext_temp ? "JEITA extended" : "normal"); in smbb_charger_probe()
986 if (r->rev_mask & BIT(chg->revision)) in smbb_charger_probe()
989 rc = regmap_update_bits(chg->regmap, chg->addr + r->offset, in smbb_charger_probe()
990 r->mask, r->value); in smbb_charger_probe()
992 dev_err(&pdev->dev, in smbb_charger_probe()
1009 regmap_update_bits(chg->regmap, chg->addr + SMBB_CHG_CTRL, CTRL_EN, 0); in smbb_charger_remove()
1013 { .compatible = "qcom,pm8226-charger" },
1014 { .compatible = "qcom,pm8941-charger" },
1023 .name = "qcom-smbb",
1029 MODULE_DESCRIPTION("Qualcomm Switch-Mode Battery Charger and Boost driver");