Lines Matching +full:battery +full:- +full:powered

1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * TWL4030/TPS65950 BCI (Battery Charger Interface) driver
71 #define TWL4030_ICHGHIGH BIT(2) /* Battery charge current high */
72 #define TWL4030_ICHGLOW BIT(3) /* Battery cc. low / FSM state change */
73 #define TWL4030_ICHGEOC BIT(4) /* Battery current end-of-charge */
74 #define TWL4030_TBATOR2 BIT(5) /* Battery temperature out of range 2 */
75 #define TWL4030_TBATOR1 BIT(6) /* Battery temperature out of range 1 */
76 #define TWL4030_BATSTS BIT(7) /* Battery status */
207 * CGAIN == 0: ICHG = (BCIICHG * 1.7) / (2^10 - 1) - 0.85
208 * CGAIN == 1: ICHG = (BCIICHG * 3.4) / (2^10 - 1) - 1.7
210 * CGAIN == 0: val * 1.6618 - 0.85 * 1000
211 * CGAIN == 1: (val * 1.6618 - 0.85 * 1000) * 2
219 return (regval * 16618 - 8500 * 1000) / 5; in regval2ua()
221 return (regval * 16618 - 8500 * 1000) / 10; in regval2ua()
252 if (ac_available(bci->channel_vac)) { in twl4030_charger_update_current()
253 cur = bci->ac_cur; in twl4030_charger_update_current()
254 bci->ac_is_active = true; in twl4030_charger_update_current()
256 cur = bci->usb_cur; in twl4030_charger_update_current()
257 bci->ac_is_active = false; in twl4030_charger_update_current()
258 if (cur > bci->usb_cur_target) { in twl4030_charger_update_current()
259 cur = bci->usb_cur_target; in twl4030_charger_update_current()
260 bci->usb_cur = cur; in twl4030_charger_update_current()
262 if (cur < bci->usb_cur_target) in twl4030_charger_update_current()
263 schedule_delayed_work(&bci->current_worker, USB_CUR_DELAY); in twl4030_charger_update_current()
267 if (bci->ichg_eoc >= 200000) in twl4030_charger_update_current()
269 if (bci->ichg_lo >= 400000) in twl4030_charger_update_current()
271 if (bci->ichg_hi >= 820000) in twl4030_charger_update_current()
296 reg = ua2regval(bci->ichg_eoc, cgain); in twl4030_charger_update_current()
308 reg = ua2regval(bci->ichg_lo, cgain); in twl4030_charger_update_current()
330 reg = ua2regval(bci->ichg_hi, cgain); in twl4030_charger_update_current()
389 /* Flip CGAIN and re-enable charging */ in twl4030_charger_update_current()
415 dev_dbg(bci->dev, "v=%d cur=%d limit=%d target=%d\n", v, curr, in twl4030_current_worker()
416 bci->usb_cur, bci->usb_cur_target); in twl4030_current_worker()
420 if (bci->usb_cur >= USB_CUR_STEP) in twl4030_current_worker()
421 bci->usb_cur -= USB_CUR_STEP; in twl4030_current_worker()
422 bci->usb_cur_target = bci->usb_cur; in twl4030_current_worker()
423 } else if (bci->usb_cur >= bci->usb_cur_target || in twl4030_current_worker()
424 bci->usb_cur + USB_CUR_STEP > USB_MAX_CURRENT) { in twl4030_current_worker()
425 /* Reached target and voltage is OK - stop */ in twl4030_current_worker()
428 bci->usb_cur += USB_CUR_STEP; in twl4030_current_worker()
429 schedule_delayed_work(&bci->current_worker, USB_CUR_DELAY); in twl4030_current_worker()
442 if (bci->usb_mode == CHARGE_OFF) in twl4030_charger_enable_usb()
444 if (enable && !IS_ERR_OR_NULL(bci->transceiver)) { in twl4030_charger_enable_usb()
448 /* Need to keep phy powered */ in twl4030_charger_enable_usb()
449 if (!bci->usb_enabled) { in twl4030_charger_enable_usb()
450 pm_runtime_get_sync(bci->transceiver->dev); in twl4030_charger_enable_usb()
451 bci->usb_enabled = 1; in twl4030_charger_enable_usb()
454 if (bci->usb_mode == CHARGE_AUTO) { in twl4030_charger_enable_usb()
462 dev_err(bci->dev, in twl4030_charger_enable_usb()
474 if (bci->usb_mode == CHARGE_LINEAR) { in twl4030_charger_enable_usb()
481 dev_err(bci->dev, in twl4030_charger_enable_usb()
502 /* ICHGEOCEN - end-of-charge monitor (current < 80mA) in twl4030_charger_enable_usb()
504 * ICHGLOWEN - current level monitor (charge continues) in twl4030_charger_enable_usb()
505 * don't monitor over-current or heat save in twl4030_charger_enable_usb()
514 if (bci->usb_enabled) { in twl4030_charger_enable_usb()
515 pm_runtime_mark_last_busy(bci->transceiver->dev); in twl4030_charger_enable_usb()
516 pm_runtime_put_autosuspend(bci->transceiver->dev); in twl4030_charger_enable_usb()
517 bci->usb_enabled = 0; in twl4030_charger_enable_usb()
519 bci->usb_cur = 0; in twl4030_charger_enable_usb()
532 if (bci->ac_mode == CHARGE_OFF) in twl4030_charger_enable_ac()
544 * Enable/Disable charging of Backup Battery.
553 /* disable charging of backup battery */ in twl4030_charger_enable_backup()
593 dev_dbg(bci->dev, "CHG_PRES irq\n"); in twl4030_charger_interrupt()
595 bci->ac_cur = 500000; in twl4030_charger_interrupt()
597 power_supply_changed(bci->ac); in twl4030_charger_interrupt()
598 power_supply_changed(bci->usb); in twl4030_charger_interrupt()
622 dev_dbg(bci->dev, "BCI irq %02x %02x\n", irqs2, irqs1); in twl4030_bci_interrupt()
626 power_supply_changed(bci->ac); in twl4030_bci_interrupt()
627 power_supply_changed(bci->usb); in twl4030_bci_interrupt()
633 dev_warn(bci->dev, "battery temperature out of range\n"); in twl4030_bci_interrupt()
636 dev_crit(bci->dev, "battery disconnected\n"); in twl4030_bci_interrupt()
639 dev_crit(bci->dev, "VBAT overvoltage\n"); in twl4030_bci_interrupt()
642 dev_crit(bci->dev, "VBUS overvoltage\n"); in twl4030_bci_interrupt()
645 dev_crit(bci->dev, "Ac charger overvoltage\n"); in twl4030_bci_interrupt()
654 switch (bci->event) { in twl4030_bci_usb_work()
670 dev_dbg(bci->dev, "OTG notify %lu\n", val); in twl4030_bci_usb_ncb()
674 bci->usb_cur_target = 500000; in twl4030_bci_usb_ncb()
676 bci->usb_cur_target = 100000; in twl4030_bci_usb_ncb()
678 bci->event = val; in twl4030_bci_usb_ncb()
679 schedule_work(&bci->work); in twl4030_bci_usb_ncb()
691 struct twl4030_bci *bci = dev_get_drvdata(dev->parent); in twl4030_bci_mode_store()
699 if (dev == &bci->ac->dev) { in twl4030_bci_mode_store()
701 return -EINVAL; in twl4030_bci_mode_store()
703 bci->ac_mode = mode; in twl4030_bci_mode_store()
707 bci->usb_mode = mode; in twl4030_bci_mode_store()
720 struct twl4030_bci *bci = dev_get_drvdata(dev->parent); in twl4030_bci_mode_show()
723 int mode = bci->usb_mode; in twl4030_bci_mode_show()
725 if (dev == &bci->ac->dev) in twl4030_bci_mode_show()
726 mode = bci->ac_mode; in twl4030_bci_mode_show()
733 buf[len-1] = '\n'; in twl4030_bci_mode_show()
767 dev_err(bci->dev, "error reading BCIMSTATEC\n"); in twl4030bci_state()
771 dev_dbg(bci->dev, "state: %02x\n", state); in twl4030bci_state()
792 struct twl4030_bci *bci = dev_get_drvdata(psy->dev.parent); in twl4030_bci_get_property()
801 if (psy->desc->type == POWER_SUPPLY_TYPE_USB) in twl4030_bci_get_property()
810 if (psy->desc->type == POWER_SUPPLY_TYPE_USB) in twl4030_bci_get_property()
822 val->intval = twl4030_bci_state_to_status(state); in twl4030_bci_get_property()
824 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; in twl4030_bci_get_property()
829 return -ENODATA; in twl4030_bci_get_property()
830 if (psy->desc->type == POWER_SUPPLY_TYPE_USB) { in twl4030_bci_get_property()
835 val->intval = ret * 6843; in twl4030_bci_get_property()
841 val->intval = ret * 9775; in twl4030_bci_get_property()
846 return -ENODATA; in twl4030_bci_get_property()
851 val->intval = ret; in twl4030_bci_get_property()
854 val->intval = is_charging && in twl4030_bci_get_property()
859 val->intval = -1; in twl4030_bci_get_property()
860 if (psy->desc->type != POWER_SUPPLY_TYPE_USB) { in twl4030_bci_get_property()
861 if (!bci->ac_is_active) in twl4030_bci_get_property()
862 val->intval = bci->ac_cur; in twl4030_bci_get_property()
864 if (bci->ac_is_active) in twl4030_bci_get_property()
865 val->intval = bci->usb_cur_target; in twl4030_bci_get_property()
867 if (val->intval < 0) { in twl4030_bci_get_property()
870 val->intval = twl4030bci_read_adc_val(TWL4030_BCIIREF1); in twl4030_bci_get_property()
871 if (val->intval < 0) in twl4030_bci_get_property()
872 return val->intval; in twl4030_bci_get_property()
876 val->intval = regval2ua(val->intval, bcictl1 & in twl4030_bci_get_property()
881 return -EINVAL; in twl4030_bci_get_property()
891 struct twl4030_bci *bci = dev_get_drvdata(psy->dev.parent); in twl4030_bci_set_property()
895 if (psy->desc->type == POWER_SUPPLY_TYPE_USB) in twl4030_bci_set_property()
896 bci->usb_cur_target = val->intval; in twl4030_bci_set_property()
898 bci->ac_cur = val->intval; in twl4030_bci_set_property()
902 return -EINVAL; in twl4030_bci_set_property()
931 struct device_node *np = dev->of_node; in twl4030_bci_parse_dt()
941 if (of_property_read_u32(np, "ti,bb-uvolt", &num) == 0) in twl4030_bci_parse_dt()
942 pdata->bb_uvolt = num; in twl4030_bci_parse_dt()
943 if (of_property_read_u32(np, "ti,bb-uamp", &num) == 0) in twl4030_bci_parse_dt()
944 pdata->bb_uamp = num; in twl4030_bci_parse_dt()
978 const struct twl4030_bci_platform_data *pdata = pdev->dev.platform_data; in twl4030_bci_probe()
982 bci = devm_kzalloc(&pdev->dev, sizeof(*bci), GFP_KERNEL); in twl4030_bci_probe()
984 return -ENOMEM; in twl4030_bci_probe()
987 pdata = twl4030_bci_parse_dt(&pdev->dev); in twl4030_bci_probe()
989 bci->ichg_eoc = 80100; /* Stop charging when current drops to here */ in twl4030_bci_probe()
990 bci->ichg_lo = 241000; /* Low threshold */ in twl4030_bci_probe()
991 bci->ichg_hi = 500000; /* High threshold */ in twl4030_bci_probe()
992 bci->ac_cur = 500000; /* 500mA */ in twl4030_bci_probe()
994 bci->usb_cur_target = 500000; /* 500mA */ in twl4030_bci_probe()
996 bci->usb_cur_target = 100000; /* 100mA */ in twl4030_bci_probe()
997 bci->usb_mode = CHARGE_AUTO; in twl4030_bci_probe()
998 bci->ac_mode = CHARGE_AUTO; in twl4030_bci_probe()
1000 bci->dev = &pdev->dev; in twl4030_bci_probe()
1001 bci->irq_chg = platform_get_irq(pdev, 0); in twl4030_bci_probe()
1002 bci->irq_bci = platform_get_irq(pdev, 1); in twl4030_bci_probe()
1006 INIT_WORK(&bci->work, twl4030_bci_usb_work); in twl4030_bci_probe()
1007 INIT_DELAYED_WORK(&bci->current_worker, twl4030_current_worker); in twl4030_bci_probe()
1009 bci->channel_vac = devm_iio_channel_get(&pdev->dev, "vac"); in twl4030_bci_probe()
1010 if (IS_ERR(bci->channel_vac)) { in twl4030_bci_probe()
1011 ret = PTR_ERR(bci->channel_vac); in twl4030_bci_probe()
1012 if (ret == -EPROBE_DEFER) in twl4030_bci_probe()
1014 dev_warn(&pdev->dev, "could not request vac iio channel (%d)", in twl4030_bci_probe()
1016 bci->channel_vac = NULL; in twl4030_bci_probe()
1019 if (bci->dev->of_node) { in twl4030_bci_probe()
1022 phynode = of_get_compatible_child(bci->dev->of_node->parent, in twl4030_bci_probe()
1023 "ti,twl4030-usb"); in twl4030_bci_probe()
1025 bci->usb_nb.notifier_call = twl4030_bci_usb_ncb; in twl4030_bci_probe()
1026 bci->transceiver = devm_usb_get_phy_by_node( in twl4030_bci_probe()
1027 bci->dev, phynode, &bci->usb_nb); in twl4030_bci_probe()
1029 if (IS_ERR(bci->transceiver)) { in twl4030_bci_probe()
1030 ret = PTR_ERR(bci->transceiver); in twl4030_bci_probe()
1031 if (ret == -EPROBE_DEFER) in twl4030_bci_probe()
1033 dev_warn(&pdev->dev, "could not request transceiver (%d)", in twl4030_bci_probe()
1035 bci->transceiver = NULL; in twl4030_bci_probe()
1040 bci->ac = devm_power_supply_register(&pdev->dev, &twl4030_bci_ac_desc, in twl4030_bci_probe()
1042 if (IS_ERR(bci->ac)) { in twl4030_bci_probe()
1043 ret = PTR_ERR(bci->ac); in twl4030_bci_probe()
1044 dev_err(&pdev->dev, "failed to register ac: %d\n", ret); in twl4030_bci_probe()
1048 bci->usb = devm_power_supply_register(&pdev->dev, &twl4030_bci_usb_desc, in twl4030_bci_probe()
1050 if (IS_ERR(bci->usb)) { in twl4030_bci_probe()
1051 ret = PTR_ERR(bci->usb); in twl4030_bci_probe()
1052 dev_err(&pdev->dev, "failed to register usb: %d\n", ret); in twl4030_bci_probe()
1056 ret = devm_request_threaded_irq(&pdev->dev, bci->irq_chg, NULL, in twl4030_bci_probe()
1057 twl4030_charger_interrupt, IRQF_ONESHOT, pdev->name, in twl4030_bci_probe()
1060 dev_err(&pdev->dev, "could not request irq %d, status %d\n", in twl4030_bci_probe()
1061 bci->irq_chg, ret); in twl4030_bci_probe()
1065 ret = devm_request_threaded_irq(&pdev->dev, bci->irq_bci, NULL, in twl4030_bci_probe()
1066 twl4030_bci_interrupt, IRQF_ONESHOT, pdev->name, bci); in twl4030_bci_probe()
1068 dev_err(&pdev->dev, "could not request irq %d, status %d\n", in twl4030_bci_probe()
1069 bci->irq_bci, ret); in twl4030_bci_probe()
1079 dev_err(&pdev->dev, "failed to unmask interrupts: %d\n", ret); in twl4030_bci_probe()
1087 dev_warn(&pdev->dev, "failed to unmask interrupts: %d\n", ret); in twl4030_bci_probe()
1090 if (device_create_file(&bci->usb->dev, &dev_attr_mode)) in twl4030_bci_probe()
1091 dev_warn(&pdev->dev, "could not create sysfs file\n"); in twl4030_bci_probe()
1092 if (device_create_file(&bci->ac->dev, &dev_attr_mode)) in twl4030_bci_probe()
1093 dev_warn(&pdev->dev, "could not create sysfs file\n"); in twl4030_bci_probe()
1096 if (!IS_ERR_OR_NULL(bci->transceiver)) in twl4030_bci_probe()
1097 twl4030_bci_usb_ncb(&bci->usb_nb, in twl4030_bci_probe()
1098 bci->transceiver->last_event, in twl4030_bci_probe()
1103 twl4030_charger_enable_backup(pdata->bb_uvolt, in twl4030_bci_probe()
1104 pdata->bb_uamp); in twl4030_bci_probe()
1119 device_remove_file(&bci->usb->dev, &dev_attr_mode); in twl4030_bci_remove()
1120 device_remove_file(&bci->ac->dev, &dev_attr_mode); in twl4030_bci_remove()
1129 {.compatible = "ti,twl4030-bci", },
1145 MODULE_DESCRIPTION("TWL4030 Battery Charger Interface driver");