From patchwork Fri Apr 15 20:36:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 12815279 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 33749C433EF for ; Fri, 15 Apr 2022 20:38:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350531AbiDOUlN (ORCPT ); Fri, 15 Apr 2022 16:41:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47518 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232478AbiDOUlN (ORCPT ); Fri, 15 Apr 2022 16:41:13 -0400 Received: from mail-lf1-x134.google.com (mail-lf1-x134.google.com [IPv6:2a00:1450:4864:20::134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D2F0272441 for ; Fri, 15 Apr 2022 13:38:43 -0700 (PDT) Received: by mail-lf1-x134.google.com with SMTP id u19so15581890lff.4 for ; Fri, 15 Apr 2022 13:38:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=EhELGrOOVgPcFGtEJejZf0g2WHh5ZLOtbqA5koG44Zk=; b=HDu27UTTZdHUtpoBv1b3CepLj+1upxM+qaiL2dsqgFof9mEk2bRlhAHGCQfDNcB6a+ 4092ggnSe1P8PMZwVxtgBfO3U1Wnf1NxXdkYP/xb9vZ4UNl/deOQvahLEnZ7iFeNI57O rjuRRG/AenvrSI+l9qrXfihHoWFBnE5ZJ2PyxvA+4+u+4K2ydjTKpjxo6afGZ7lB9tjp TY6DMofa+gr4bvLywZOgph45YKBnq2yE+U2Bd9qrAhDKtjX7uFmh4cXvb6YuywuZV2wb TCcQD8riGj0VJlQCQqujuOoTS6bu8Y3d4IgyJvx68GSk3DVfA+Dw7EZK+5lFRg7NiLuN MvJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=EhELGrOOVgPcFGtEJejZf0g2WHh5ZLOtbqA5koG44Zk=; b=m0ccUXovKtCE9pIg+764h+fzXBZ+HbVTvzS3njnZKyFtdFrYcDTXUtYBquQ3rXe+xU oyMvaHqgvcQxRWGqOwpXet5e0v4RnignG/tLpwte7okCD2aIXp+BkAeneyeS0rDA8K7G 1AL2NHa1z7QyRw2AlVuli1nZf5oR1PGEL4UjYcGtYqnb6T7Z4HuT9soNqlpebvYEKXR+ BoKZVTBqpueCW8oKwXm7pAzm8kJ6ax7AgDxjYzqBvzfXVZlJz6xc9XbQLYXHE+MM1ibM cYL9Age621AhQX0oh5kXfr5lQlfr4oykcKc7CnRqyDtpqFBznS2WUd8Adl6miB33VM2q RqLw== X-Gm-Message-State: AOAM531OumuRCm6/9jvgrElhZd4DPkt4LXGeimw5r2UpCaFhkmqBFwiL sRRZrQMTsF56Gciu3gSM4oAWExhiuOnp0g== X-Google-Smtp-Source: ABdhPJwi0qhXhfOHOcJgoD32uFe8VmPnylacVML1fpXFs/DOS0TtjISzM5TfDUaN0DhWk6FaHkOnMg== X-Received: by 2002:a19:505e:0:b0:46d:12df:d6b3 with SMTP id z30-20020a19505e000000b0046d12dfd6b3mr428121lfj.111.1650055122076; Fri, 15 Apr 2022 13:38:42 -0700 (PDT) Received: from localhost.localdomain (c-fdcc225c.014-348-6c756e10.bbcust.telenor.se. [92.34.204.253]) by smtp.gmail.com with ESMTPSA id r9-20020a2e94c9000000b0024b4c2a1346sm375318ljh.123.2022.04.15.13.38.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Apr 2022 13:38:41 -0700 (PDT) From: Linus Walleij To: Sebastian Reichel , Marcus Cooper Cc: linux-pm@vger.kernel.org, Matti Vaittinen , Linus Walleij Subject: [PATCH 1/2] power: supply: ab8500: Respect charge_restart_voltage_uv Date: Fri, 15 Apr 2022 22:36:37 +0200 Message-Id: <20220415203638.361074-1-linus.walleij@linaro.org> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org The battery info contains a voltage indicating when the voltage is so low that it is time to restart the CC/CV charging. Make the AB8500 respect and prioritize this setting over the hardcoded 95% threshold. Break out the check into its own function and add some safeguards so we do not run into unpredictable side effects. Signed-off-by: Linus Walleij --- drivers/power/supply/ab8500_chargalg.c | 30 +++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/drivers/power/supply/ab8500_chargalg.c b/drivers/power/supply/ab8500_chargalg.c index 94c22fdfe963..b9622eb9fc72 100644 --- a/drivers/power/supply/ab8500_chargalg.c +++ b/drivers/power/supply/ab8500_chargalg.c @@ -1216,6 +1216,34 @@ static void ab8500_chargalg_external_power_changed(struct power_supply *psy) queue_work(di->chargalg_wq, &di->chargalg_work); } +/** + * ab8500_chargalg_time_to_restart() - time to restart CC/CV charging? + * @di: charging algorithm state + * + * This checks if the voltage or capacity of the battery has fallen so + * low that we need to restart the CC/CV charge cycle. + */ +static bool ab8500_chargalg_time_to_restart(struct ab8500_chargalg *di) +{ + struct power_supply_battery_info *bi = di->bm->bi; + + /* Sanity check - these need to have some reasonable values */ + if (!di->batt_data.volt_uv || !di->batt_data.percent) + return false; + + /* Some batteries tell us at which voltage we should restart charging */ + if (bi->charge_restart_voltage_uv > 0) { + if (di->batt_data.volt_uv <= bi->charge_restart_voltage_uv) + return true; + /* Else we restart as we reach a certain capacity */ + } else { + if (di->batt_data.percent <= AB8500_RECHARGE_CAP) + return true; + } + + return false; +} + /** * ab8500_chargalg_algorithm() - Main function for the algorithm * @di: pointer to the ab8500_chargalg structure @@ -1459,7 +1487,7 @@ static void ab8500_chargalg_algorithm(struct ab8500_chargalg *di) fallthrough; case STATE_WAIT_FOR_RECHARGE: - if (di->batt_data.percent <= AB8500_RECHARGE_CAP) + if (ab8500_chargalg_time_to_restart(di)) ab8500_chargalg_state_to(di, STATE_NORMAL_INIT); break; From patchwork Fri Apr 15 20:36:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 12815280 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1C8E6C433F5 for ; Fri, 15 Apr 2022 20:38:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350577AbiDOUlW (ORCPT ); Fri, 15 Apr 2022 16:41:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47562 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232478AbiDOUlW (ORCPT ); Fri, 15 Apr 2022 16:41:22 -0400 Received: from mail-lf1-x133.google.com (mail-lf1-x133.google.com [IPv6:2a00:1450:4864:20::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BFEB8716E8 for ; Fri, 15 Apr 2022 13:38:52 -0700 (PDT) Received: by mail-lf1-x133.google.com with SMTP id k5so15557341lfg.9 for ; Fri, 15 Apr 2022 13:38:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=FKkjAiIszCawKWT8AUIwwswATWKraE4A1R8cDkeebHg=; b=uS/8zWpLHPctXa+DSmJnWO+wTt6g9CPaoJ3Y4ZxwKbGzkIeXLiDNoGUSNqC+coyKhM NvrVv6vSPdX/xL/Orb1CIU3yHbTSa67nVNvayxzJE2UcJDz9QClnQ0QzLAL/XO46MYSs hexmIFhnDZnRrnx5ntwSQzwPhB6oJybbTFP/c4j9fCBjhjbc8Gri2EtJhWh2GYiYWOZk xgZ0l3xqW79P+cdkNobTlo7/LyLVHybDQ5rA4CiskZfBTTvBy3uyRqp8wm9p+INI7eJY Rs/hQcXDeBRNZvadWi6HYHeyMmaaNLXQ6NWN0cwmbWo6l8U5XSf0/37QXq8zXN+aTOmy 0apA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=FKkjAiIszCawKWT8AUIwwswATWKraE4A1R8cDkeebHg=; b=ySqMolhzc5lQ746Eq4QhlrlbPVobETJUvAX0L6cUWyJY5q0dTVCq3F/oCIF7/7VOLj RUW228Y0ZKnIWZR++MikdpK1UeMrCuv9WpH/s95Yd/NnX07DnNzgpVXMeqWagT854TvA Ly+RSBbHdAKJA0wU59s2HYHLo2/TwBN05MkIgW6L4QyU+JWNsNBZxns1XFbswOZdJXSg CCI0xgfVl3iVtGt9E25mroFML51mXdMOououvMF2I4x0KIp8SxWKr/Owp+PCE321/l0d Vmq7eFGxGCfLevHxev+IV8mpkqg51oEXT5a4tWqGRhWqwKxr7sxi+ympfuE2EFZrmXVP lrSg== X-Gm-Message-State: AOAM531YX1BjEGs25kCmwGaNS66UD9bR52lSdCwkwkWPRos968/txN+h aadcINpts6eeQ7ljkXesChc4xQ== X-Google-Smtp-Source: ABdhPJxONdN21L3HPnFy1jWIgrZoipXV7+ND2O9QcosPJhPq7mi0mewmJ+QtPRIjleiPwvFro4iccw== X-Received: by 2002:a05:6512:2308:b0:46b:c4e6:f3fd with SMTP id o8-20020a056512230800b0046bc4e6f3fdmr425651lfu.411.1650055131040; Fri, 15 Apr 2022 13:38:51 -0700 (PDT) Received: from localhost.localdomain (c-fdcc225c.014-348-6c756e10.bbcust.telenor.se. [92.34.204.253]) by smtp.gmail.com with ESMTPSA id r9-20020a2e94c9000000b0024b4c2a1346sm375318ljh.123.2022.04.15.13.38.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Apr 2022 13:38:50 -0700 (PDT) From: Linus Walleij To: Sebastian Reichel , Marcus Cooper Cc: linux-pm@vger.kernel.org, Matti Vaittinen , Linus Walleij Subject: [PATCH 2/2] power: supply: ab8500: Exit maintenance if too low voltage Date: Fri, 15 Apr 2022 22:36:38 +0200 Message-Id: <20220415203638.361074-2-linus.walleij@linaro.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220415203638.361074-1-linus.walleij@linaro.org> References: <20220415203638.361074-1-linus.walleij@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org The maintenance charging is supposedly designed such that the maintenance current compensates for the battery discharge curve, and as the charging progress from CC/CV -> maintenance A -> maintenance B states, we end up on a reasonable voltage to restart ordinary CC/CV charging after the safety timer at the maintenance B state exits. However: old batteries discharge quicker, and in an old battery we might not get to the expiration of the maintenance B timer before the battery is completely depleted and the system powers off with an empty battery. This is hardly the desire of anyone leaving their phone in the charger for a few days! Introduce a second clause in both maintenance states such that we exit the state and return to ordinary CC/CV charging if the voltage drops below charge_restart_voltage_uv or 95% if this is not defined for the battery. Signed-off-by: Linus Walleij Reviewed-by: Matti Vaittinen --- drivers/power/supply/ab8500_chargalg.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/drivers/power/supply/ab8500_chargalg.c b/drivers/power/supply/ab8500_chargalg.c index b9622eb9fc72..1b23b5261881 100644 --- a/drivers/power/supply/ab8500_chargalg.c +++ b/drivers/power/supply/ab8500_chargalg.c @@ -1514,6 +1514,14 @@ static void ab8500_chargalg_algorithm(struct ab8500_chargalg *di) ab8500_chargalg_stop_maintenance_timer(di); ab8500_chargalg_state_to(di, STATE_MAINTENANCE_B_INIT); } + /* + * This happens if the voltage drops too quickly during + * maintenance charging, especially in older batteries. + */ + if (ab8500_chargalg_time_to_restart(di)) { + ab8500_chargalg_state_to(di, STATE_NORMAL_INIT); + dev_info(di->dev, "restarted charging from maintenance state A - battery getting old?\n"); + } break; case STATE_MAINTENANCE_B_INIT: @@ -1538,6 +1546,14 @@ static void ab8500_chargalg_algorithm(struct ab8500_chargalg *di) ab8500_chargalg_stop_maintenance_timer(di); ab8500_chargalg_state_to(di, STATE_NORMAL_INIT); } + /* + * This happens if the voltage drops too quickly during + * maintenance charging, especially in older batteries. + */ + if (ab8500_chargalg_time_to_restart(di)) { + ab8500_chargalg_state_to(di, STATE_NORMAL_INIT); + dev_info(di->dev, "restarted charging from maintenance state B - battery getting old?\n"); + } break; case STATE_TEMP_LOWHIGH_INIT: