From patchwork Sun Jan 10 19:53:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Lindgren X-Patchwork-Id: 360766 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CF43FC433E6 for ; Sun, 10 Jan 2021 19:54:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A477822581 for ; Sun, 10 Jan 2021 19:54:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726608AbhAJTyv (ORCPT ); Sun, 10 Jan 2021 14:54:51 -0500 Received: from muru.com ([72.249.23.125]:42348 "EHLO muru.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726394AbhAJTyu (ORCPT ); Sun, 10 Jan 2021 14:54:50 -0500 Received: from hillo.muru.com (localhost [127.0.0.1]) by muru.com (Postfix) with ESMTP id E266B80E5; Sun, 10 Jan 2021 19:54:09 +0000 (UTC) From: Tony Lindgren To: Sebastian Reichel Cc: linux-pm@vger.kernel.org, linux-omap@vger.kernel.org, Arthur Demchenkov , Carl Philipp Klemm , Merlijn Wajer , Pavel Machek Subject: [PATCH 01/15] power: supply: cpcap-charger: Fix missing power_supply_put() Date: Sun, 10 Jan 2021 21:53:49 +0200 Message-Id: <20210110195403.13758-2-tony@atomide.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210110195403.13758-1-tony@atomide.com> References: <20210110195403.13758-1-tony@atomide.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Fix missing power_supply_put(). Cc: Arthur Demchenkov Cc: Carl Philipp Klemm Cc: Merlijn Wajer Cc: Pavel Machek Fixes: 688ea049233 ("power: supply: cpcap-charger: Allow changing constant charge voltage") Signed-off-by: Tony Lindgren --- drivers/power/supply/cpcap-charger.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/power/supply/cpcap-charger.c b/drivers/power/supply/cpcap-charger.c --- a/drivers/power/supply/cpcap-charger.c +++ b/drivers/power/supply/cpcap-charger.c @@ -302,6 +302,7 @@ cpcap_charger_get_bat_const_charge_voltage(struct cpcap_charger_ddata *ddata) if (!error) voltage = prop.intval; } + power_supply_put(battery); return voltage; } From patchwork Sun Jan 10 19:53:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Lindgren X-Patchwork-Id: 360310 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A4BD0C433E0 for ; Sun, 10 Jan 2021 19:54:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7D1A4225A9 for ; Sun, 10 Jan 2021 19:54:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726635AbhAJTyw (ORCPT ); Sun, 10 Jan 2021 14:54:52 -0500 Received: from muru.com ([72.249.23.125]:42358 "EHLO muru.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726394AbhAJTyw (ORCPT ); Sun, 10 Jan 2021 14:54:52 -0500 Received: from hillo.muru.com (localhost [127.0.0.1]) by muru.com (Postfix) with ESMTP id 8937C810A; Sun, 10 Jan 2021 19:54:11 +0000 (UTC) From: Tony Lindgren To: Sebastian Reichel Cc: linux-pm@vger.kernel.org, linux-omap@vger.kernel.org, Arthur Demchenkov , Carl Philipp Klemm , Merlijn Wajer , Pavel Machek Subject: [PATCH 02/15] power: supply: cpcap-battery: Fix missing power_supply_put() Date: Sun, 10 Jan 2021 21:53:50 +0200 Message-Id: <20210110195403.13758-3-tony@atomide.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210110195403.13758-1-tony@atomide.com> References: <20210110195403.13758-1-tony@atomide.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Fix missing power_supply_put(). Cc: Arthur Demchenkov Cc: Carl Philipp Klemm Cc: Merlijn Wajer Cc: Pavel Machek Fixes: b0134cc14b9 ("power: supply: cpcap-battery: Fix handling of lowered charger voltage") Signed-off-by: Tony Lindgren --- drivers/power/supply/cpcap-battery.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/drivers/power/supply/cpcap-battery.c b/drivers/power/supply/cpcap-battery.c --- a/drivers/power/supply/cpcap-battery.c +++ b/drivers/power/supply/cpcap-battery.c @@ -568,17 +568,21 @@ static int cpcap_battery_update_charger(struct cpcap_battery_ddata *ddata, POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, &prop); if (error) - return error; + goto out_put; /* Allow charger const voltage lower than battery const voltage */ if (const_charge_voltage > prop.intval) - return 0; + goto out_put; val.intval = const_charge_voltage; - return power_supply_set_property(charger, + error = power_supply_set_property(charger, POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, &val); +out_put: + power_supply_put(charger); + + return error; } static int cpcap_battery_set_property(struct power_supply *psy, From patchwork Sun Jan 10 19:53:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Lindgren X-Patchwork-Id: 360765 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 72FAFC433E6 for ; Sun, 10 Jan 2021 19:54:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4234F22581 for ; Sun, 10 Jan 2021 19:54:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726709AbhAJTyy (ORCPT ); Sun, 10 Jan 2021 14:54:54 -0500 Received: from muru.com ([72.249.23.125]:42368 "EHLO muru.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726394AbhAJTyy (ORCPT ); Sun, 10 Jan 2021 14:54:54 -0500 Received: from hillo.muru.com (localhost [127.0.0.1]) by muru.com (Postfix) with ESMTP id 510FF813C; Sun, 10 Jan 2021 19:54:13 +0000 (UTC) From: Tony Lindgren To: Sebastian Reichel Cc: linux-pm@vger.kernel.org, linux-omap@vger.kernel.org, Arthur Demchenkov , Carl Philipp Klemm , Merlijn Wajer , Pavel Machek Subject: [PATCH 03/15] power: supply: cpcap-charger: Fix flakey reboot with charger connected Date: Sun, 10 Jan 2021 21:53:51 +0200 Message-Id: <20210110195403.13758-4-tony@atomide.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210110195403.13758-1-tony@atomide.com> References: <20210110195403.13758-1-tony@atomide.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org If we have a USB charger connected, reboot is flakey and often fails to reboot the device with the charger LED staying on. Let's fix this by implementing .shutdown. Cc: Arthur Demchenkov Cc: Carl Philipp Klemm Cc: Merlijn Wajer Cc: Pavel Machek Signed-off-by: Tony Lindgren --- drivers/power/supply/cpcap-charger.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/drivers/power/supply/cpcap-charger.c b/drivers/power/supply/cpcap-charger.c --- a/drivers/power/supply/cpcap-charger.c +++ b/drivers/power/supply/cpcap-charger.c @@ -886,7 +886,7 @@ static int cpcap_charger_probe(struct platform_device *pdev) return 0; } -static int cpcap_charger_remove(struct platform_device *pdev) +static void cpcap_charger_shutdown(struct platform_device *pdev) { struct cpcap_charger_ddata *ddata = platform_get_drvdata(pdev); int error; @@ -903,6 +903,11 @@ static int cpcap_charger_remove(struct platform_device *pdev) error); cancel_delayed_work_sync(&ddata->vbus_work); cancel_delayed_work_sync(&ddata->detect_work); +} + +static int cpcap_charger_remove(struct platform_device *pdev) +{ + cpcap_charger_shutdown(pdev); return 0; } @@ -913,6 +918,7 @@ static struct platform_driver cpcap_charger_driver = { .name = "cpcap-charger", .of_match_table = of_match_ptr(cpcap_charger_id_table), }, + .shutdown = cpcap_charger_shutdown, .remove = cpcap_charger_remove, }; module_platform_driver(cpcap_charger_driver); From patchwork Sun Jan 10 19:53:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Lindgren X-Patchwork-Id: 360309 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F3290C433E0 for ; Sun, 10 Jan 2021 19:54:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C20B6225A9 for ; Sun, 10 Jan 2021 19:54:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726756AbhAJTy4 (ORCPT ); Sun, 10 Jan 2021 14:54:56 -0500 Received: from muru.com ([72.249.23.125]:42380 "EHLO muru.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726394AbhAJTy4 (ORCPT ); Sun, 10 Jan 2021 14:54:56 -0500 Received: from hillo.muru.com (localhost [127.0.0.1]) by muru.com (Postfix) with ESMTP id 1A41D8140; Sun, 10 Jan 2021 19:54:14 +0000 (UTC) From: Tony Lindgren To: Sebastian Reichel Cc: linux-pm@vger.kernel.org, linux-omap@vger.kernel.org, Arthur Demchenkov , Carl Philipp Klemm , Merlijn Wajer , Pavel Machek Subject: [PATCH 04/15] power: supply: cpcap-charger: Make VBUS already provided debug only Date: Sun, 10 Jan 2021 21:53:52 +0200 Message-Id: <20210110195403.13758-5-tony@atomide.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210110195403.13758-1-tony@atomide.com> References: <20210110195403.13758-1-tony@atomide.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org This should be only shown when debug is enabled. Cc: Arthur Demchenkov Cc: Carl Philipp Klemm Cc: Merlijn Wajer Cc: Pavel Machek Signed-off-by: Tony Lindgren --- drivers/power/supply/cpcap-charger.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/power/supply/cpcap-charger.c b/drivers/power/supply/cpcap-charger.c --- a/drivers/power/supply/cpcap-charger.c +++ b/drivers/power/supply/cpcap-charger.c @@ -434,7 +434,7 @@ static void cpcap_charger_vbus_work(struct work_struct *work) if (ddata->vbus_enabled) { vbus = cpcap_charger_vbus_valid(ddata); if (vbus) { - dev_info(ddata->dev, "VBUS already provided\n"); + dev_dbg(ddata->dev, "VBUS already provided\n"); return; } From patchwork Sun Jan 10 19:53:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Lindgren X-Patchwork-Id: 360764 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 736BCC433E0 for ; Sun, 10 Jan 2021 19:55:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4739822A84 for ; Sun, 10 Jan 2021 19:55:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726602AbhAJTza (ORCPT ); Sun, 10 Jan 2021 14:55:30 -0500 Received: from muru.com ([72.249.23.125]:42498 "EHLO muru.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726447AbhAJTza (ORCPT ); Sun, 10 Jan 2021 14:55:30 -0500 Received: from hillo.muru.com (localhost [127.0.0.1]) by muru.com (Postfix) with ESMTP id B53BE8191; Sun, 10 Jan 2021 19:54:16 +0000 (UTC) From: Tony Lindgren To: Sebastian Reichel Cc: linux-pm@vger.kernel.org, linux-omap@vger.kernel.org, Arthur Demchenkov , Carl Philipp Klemm , Merlijn Wajer , Pavel Machek Subject: [PATCH 05/15] power: supply: cpcap-charger: Use standard enumeration Date: Sun, 10 Jan 2021 21:53:53 +0200 Message-Id: <20210110195403.13758-6-tony@atomide.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210110195403.13758-1-tony@atomide.com> References: <20210110195403.13758-1-tony@atomide.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Just get rid of the custom enumeration if favor of the standard one. Cc: Arthur Demchenkov Cc: Carl Philipp Klemm Cc: Merlijn Wajer Cc: Pavel Machek Signed-off-by: Tony Lindgren --- drivers/power/supply/cpcap-charger.c | 39 ++++++++++++++-------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/drivers/power/supply/cpcap-charger.c b/drivers/power/supply/cpcap-charger.c --- a/drivers/power/supply/cpcap-charger.c +++ b/drivers/power/supply/cpcap-charger.c @@ -120,13 +120,6 @@ enum { CPCAP_CHARGER_IIO_NR, }; -enum { - CPCAP_CHARGER_DISCONNECTED, - CPCAP_CHARGER_DETECTING, - CPCAP_CHARGER_CHARGING, - CPCAP_CHARGER_DONE, -}; - struct cpcap_charger_ddata { struct device *dev; struct regmap *reg; @@ -533,7 +526,7 @@ static void cpcap_charger_update_state(struct cpcap_charger_ddata *ddata, { const char *status; - if (state > CPCAP_CHARGER_DONE) { + if (state > POWER_SUPPLY_STATUS_FULL) { dev_warn(ddata->dev, "unknown state: %i\n", state); return; @@ -542,16 +535,16 @@ static void cpcap_charger_update_state(struct cpcap_charger_ddata *ddata, ddata->state = state; switch (state) { - case CPCAP_CHARGER_DISCONNECTED: + case POWER_SUPPLY_STATUS_DISCHARGING: status = "DISCONNECTED"; break; - case CPCAP_CHARGER_DETECTING: + case POWER_SUPPLY_STATUS_NOT_CHARGING: status = "DETECTING"; break; - case CPCAP_CHARGER_CHARGING: + case POWER_SUPPLY_STATUS_CHARGING: status = "CHARGING"; break; - case CPCAP_CHARGER_DONE: + case POWER_SUPPLY_STATUS_FULL: status = "DONE"; break; default: @@ -616,7 +609,8 @@ static void cpcap_usb_detect(struct work_struct *work) /* Just init the state if a charger is connected with no chrg_det set */ if (!s.chrg_det && s.chrgcurr1 && s.vbusvld) { - cpcap_charger_update_state(ddata, CPCAP_CHARGER_DETECTING); + cpcap_charger_update_state(ddata, + POWER_SUPPLY_STATUS_NOT_CHARGING); return; } @@ -626,7 +620,8 @@ static void cpcap_usb_detect(struct work_struct *work) * charged to 4.35V by Android. Try again in 10 minutes. */ if (cpcap_charger_get_charge_voltage(ddata) > ddata->voltage) { - cpcap_charger_disconnect(ddata, CPCAP_CHARGER_DETECTING, + cpcap_charger_disconnect(ddata, + POWER_SUPPLY_STATUS_NOT_CHARGING, HZ * 60 * 10); return; @@ -634,19 +629,21 @@ static void cpcap_usb_detect(struct work_struct *work) /* Throttle chrgcurr2 interrupt for charger done and retry */ switch (ddata->state) { - case CPCAP_CHARGER_CHARGING: + case POWER_SUPPLY_STATUS_CHARGING: if (s.chrgcurr2) break; if (s.chrgcurr1 && s.vbusvld) { - cpcap_charger_disconnect(ddata, CPCAP_CHARGER_DONE, + cpcap_charger_disconnect(ddata, + POWER_SUPPLY_STATUS_FULL, HZ * 5); return; } break; - case CPCAP_CHARGER_DONE: + case POWER_SUPPLY_STATUS_FULL: if (!s.chrgcurr2) break; - cpcap_charger_disconnect(ddata, CPCAP_CHARGER_DETECTING, + cpcap_charger_disconnect(ddata, + POWER_SUPPLY_STATUS_NOT_CHARGING, HZ * 5); return; default: @@ -669,12 +666,14 @@ static void cpcap_usb_detect(struct work_struct *work) max_current, 0); if (error) goto out_err; - cpcap_charger_update_state(ddata, CPCAP_CHARGER_CHARGING); + cpcap_charger_update_state(ddata, + POWER_SUPPLY_STATUS_CHARGING); } else { error = cpcap_charger_set_state(ddata, 0, 0, 0); if (error) goto out_err; - cpcap_charger_update_state(ddata, CPCAP_CHARGER_DISCONNECTED); + cpcap_charger_update_state(ddata, + POWER_SUPPLY_STATUS_DISCHARGING); } power_supply_changed(ddata->usb); From patchwork Sun Jan 10 19:53:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Lindgren X-Patchwork-Id: 360308 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C4569C433E6 for ; Sun, 10 Jan 2021 19:55:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9B977225A9 for ; Sun, 10 Jan 2021 19:55:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726447AbhAJTza (ORCPT ); Sun, 10 Jan 2021 14:55:30 -0500 Received: from muru.com ([72.249.23.125]:42500 "EHLO muru.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726479AbhAJTza (ORCPT ); Sun, 10 Jan 2021 14:55:30 -0500 Received: from hillo.muru.com (localhost [127.0.0.1]) by muru.com (Postfix) with ESMTP id 82DE581D0; Sun, 10 Jan 2021 19:54:18 +0000 (UTC) From: Tony Lindgren To: Sebastian Reichel Cc: linux-pm@vger.kernel.org, linux-omap@vger.kernel.org, Pavel Machek , Arthur Demchenkov , Carl Philipp Klemm , Merlijn Wajer Subject: [PATCH 06/15] power: supply: cpcap-charger: Limiting charge current on Droid 4 Date: Sun, 10 Jan 2021 21:53:54 +0200 Message-Id: <20210110195403.13758-7-tony@atomide.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210110195403.13758-1-tony@atomide.com> References: <20210110195403.13758-1-tony@atomide.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org From: Pavel Machek Droid 4 has same problem as N900: it is often necessary to manually tweak current draw from USB, for example when using thin charging cable. Cc: Arthur Demchenkov Cc: Carl Philipp Klemm Cc: Merlijn Wajer Cc: Pavel Machek Signed-off-by: Pavel Machek [tony@atomide.com: rebased, cleaned up whitespace issues, updated comments] Signed-off-by: Tony Lindgren --- drivers/power/supply/cpcap-charger.c | 49 ++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/drivers/power/supply/cpcap-charger.c b/drivers/power/supply/cpcap-charger.c --- a/drivers/power/supply/cpcap-charger.c +++ b/drivers/power/supply/cpcap-charger.c @@ -89,6 +89,8 @@ * CPCAP_REG_CRM charge currents. These seem to match MC13783UG.pdf * values in "Table 8-3. Charge Path Regulator Current Limit * Characteristics" for the nominal values. + * + * Except 70mA and 1.596A and unlimited, these are simply 88.7mA / step. */ #define CPCAP_REG_CRM_ICHRG(val) (((val) & 0xf) << 0) #define CPCAP_REG_CRM_ICHRG_0A000 CPCAP_REG_CRM_ICHRG(0x0) @@ -140,6 +142,7 @@ struct cpcap_charger_ddata { int status; int state; int voltage; + int limit_current; }; struct cpcap_interrupt_desc { @@ -166,6 +169,7 @@ static enum power_supply_property cpcap_charger_props[] = { POWER_SUPPLY_PROP_STATUS, POWER_SUPPLY_PROP_ONLINE, POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, + POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, POWER_SUPPLY_PROP_VOLTAGE_NOW, POWER_SUPPLY_PROP_CURRENT_NOW, }; @@ -229,6 +233,9 @@ static int cpcap_charger_get_property(struct power_supply *psy, case POWER_SUPPLY_PROP_STATUS: val->intval = ddata->status; break; + case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: + val->intval = ddata->limit_current; + break; case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: val->intval = ddata->voltage; break; @@ -300,6 +307,26 @@ cpcap_charger_get_bat_const_charge_voltage(struct cpcap_charger_ddata *ddata) return voltage; } +static int cpcap_charger_current_to_regval(int microamp) +{ + int miliamp = microamp / 1000; + int res; + + if (miliamp < 0) + return -EINVAL; + if (miliamp < 70) + return CPCAP_REG_CRM_ICHRG(0x0); + if (miliamp < 177) + return CPCAP_REG_CRM_ICHRG(0x1); + if (miliamp > 1596) + return CPCAP_REG_CRM_ICHRG(0xe); + + res = microamp / 88666; + if (res > 0xd) + res = 0xd; + return CPCAP_REG_CRM_ICHRG(res); +} + static int cpcap_charger_set_property(struct power_supply *psy, enum power_supply_property psp, const union power_supply_propval *val) @@ -308,6 +335,12 @@ static int cpcap_charger_set_property(struct power_supply *psy, int voltage, batvolt; switch (psp) { + case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: + if (cpcap_charger_current_to_regval(val->intval) < 0) + return -EINVAL; + ddata->limit_current = val->intval; + schedule_delayed_work(&ddata->detect_work, 0); + break; case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: voltage = cpcap_charger_match_voltage(val->intval); batvolt = cpcap_charger_get_bat_const_charge_voltage(ddata); @@ -327,6 +360,7 @@ static int cpcap_charger_property_is_writeable(struct power_supply *psy, enum power_supply_property psp) { switch (psp) { + case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: return 1; default: @@ -652,18 +686,20 @@ static void cpcap_usb_detect(struct work_struct *work) if (!ddata->feeding_vbus && cpcap_charger_vbus_valid(ddata) && s.chrgcurr1) { - int max_current; - int vchrg; + int max_current = 532000; + int vchrg, ichrg; if (cpcap_charger_battery_found(ddata)) - max_current = CPCAP_REG_CRM_ICHRG_1A596; - else - max_current = CPCAP_REG_CRM_ICHRG_0A532; + max_current = 1596000; + + if (max_current > ddata->limit_current) + max_current = ddata->limit_current; + ichrg = cpcap_charger_current_to_regval(max_current); vchrg = cpcap_charger_voltage_to_regval(ddata->voltage); error = cpcap_charger_set_state(ddata, CPCAP_REG_CRM_VCHRG(vchrg), - max_current, 0); + ichrg, 0); if (error) goto out_err; cpcap_charger_update_state(ddata, @@ -837,6 +873,7 @@ static int cpcap_charger_probe(struct platform_device *pdev) ddata->dev = &pdev->dev; ddata->voltage = 4200000; + ddata->limit_current = 532000; ddata->reg = dev_get_regmap(ddata->dev->parent, NULL); if (!ddata->reg) From patchwork Sun Jan 10 19:53:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Lindgren X-Patchwork-Id: 360307 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6F850C4332E for ; Sun, 10 Jan 2021 19:55:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 48E01225A9 for ; Sun, 10 Jan 2021 19:55:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726774AbhAJTzc (ORCPT ); Sun, 10 Jan 2021 14:55:32 -0500 Received: from muru.com ([72.249.23.125]:42504 "EHLO muru.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726479AbhAJTzc (ORCPT ); Sun, 10 Jan 2021 14:55:32 -0500 Received: from hillo.muru.com (localhost [127.0.0.1]) by muru.com (Postfix) with ESMTP id 5364388B3; Sun, 10 Jan 2021 19:54:20 +0000 (UTC) From: Tony Lindgren To: Sebastian Reichel Cc: linux-pm@vger.kernel.org, linux-omap@vger.kernel.org, Arthur Demchenkov , Carl Philipp Klemm , Merlijn Wajer , Pavel Machek Subject: [PATCH 07/15] power: supply: cpcap-charger: Drop internal state and use generic stats Date: Sun, 10 Jan 2021 21:53:55 +0200 Message-Id: <20210110195403.13758-8-tony@atomide.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210110195403.13758-1-tony@atomide.com> References: <20210110195403.13758-1-tony@atomide.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org We currently have both state and status, get rid of state and use generic status instead. Cc: Arthur Demchenkov Cc: Carl Philipp Klemm Cc: Merlijn Wajer Cc: Pavel Machek Signed-off-by: Tony Lindgren --- drivers/power/supply/cpcap-charger.c | 81 ++++++++++++++-------------- 1 file changed, 42 insertions(+), 39 deletions(-) diff --git a/drivers/power/supply/cpcap-charger.c b/drivers/power/supply/cpcap-charger.c --- a/drivers/power/supply/cpcap-charger.c +++ b/drivers/power/supply/cpcap-charger.c @@ -140,7 +140,6 @@ struct cpcap_charger_ddata { atomic_t active; int status; - int state; int voltage; int limit_current; }; @@ -386,6 +385,39 @@ static void cpcap_charger_set_inductive_path(struct cpcap_charger_ddata *ddata, gpiod_set_value(ddata->gpio[1], enabled); } +static void cpcap_charger_update_state(struct cpcap_charger_ddata *ddata, + int state) +{ + const char *status; + + if (state > POWER_SUPPLY_STATUS_FULL) { + dev_warn(ddata->dev, "unknown state: %i\n", state); + + return; + } + + ddata->status = state; + + switch (state) { + case POWER_SUPPLY_STATUS_DISCHARGING: + status = "DISCONNECTED"; + break; + case POWER_SUPPLY_STATUS_NOT_CHARGING: + status = "DETECTING"; + break; + case POWER_SUPPLY_STATUS_CHARGING: + status = "CHARGING"; + break; + case POWER_SUPPLY_STATUS_FULL: + status = "DONE"; + break; + default: + return; + } + + dev_dbg(ddata->dev, "state: %s\n", status); +} + static int cpcap_charger_set_state(struct cpcap_charger_ddata *ddata, int max_voltage, int charge_current, int trickle_current) @@ -402,11 +434,13 @@ static int cpcap_charger_set_state(struct cpcap_charger_ddata *ddata, CPCAP_REG_CRM_FET_OVRD | CPCAP_REG_CRM_FET_CTRL); if (error) { - ddata->status = POWER_SUPPLY_STATUS_UNKNOWN; + cpcap_charger_update_state(ddata, + POWER_SUPPLY_STATUS_UNKNOWN); goto out_err; } - ddata->status = POWER_SUPPLY_STATUS_DISCHARGING; + cpcap_charger_update_state(ddata, + POWER_SUPPLY_STATUS_DISCHARGING); return 0; } @@ -419,11 +453,13 @@ static int cpcap_charger_set_state(struct cpcap_charger_ddata *ddata, max_voltage | charge_current); if (error) { - ddata->status = POWER_SUPPLY_STATUS_UNKNOWN; + cpcap_charger_update_state(ddata, + POWER_SUPPLY_STATUS_UNKNOWN); goto out_err; } - ddata->status = POWER_SUPPLY_STATUS_CHARGING; + cpcap_charger_update_state(ddata, + POWER_SUPPLY_STATUS_CHARGING); return 0; @@ -555,39 +591,6 @@ static int cpcap_charger_get_ints_state(struct cpcap_charger_ddata *ddata, return 0; } -static void cpcap_charger_update_state(struct cpcap_charger_ddata *ddata, - int state) -{ - const char *status; - - if (state > POWER_SUPPLY_STATUS_FULL) { - dev_warn(ddata->dev, "unknown state: %i\n", state); - - return; - } - - ddata->state = state; - - switch (state) { - case POWER_SUPPLY_STATUS_DISCHARGING: - status = "DISCONNECTED"; - break; - case POWER_SUPPLY_STATUS_NOT_CHARGING: - status = "DETECTING"; - break; - case POWER_SUPPLY_STATUS_CHARGING: - status = "CHARGING"; - break; - case POWER_SUPPLY_STATUS_FULL: - status = "DONE"; - break; - default: - return; - } - - dev_dbg(ddata->dev, "state: %s\n", status); -} - static int cpcap_charger_voltage_to_regval(int voltage) { int offset; @@ -662,7 +665,7 @@ static void cpcap_usb_detect(struct work_struct *work) } /* Throttle chrgcurr2 interrupt for charger done and retry */ - switch (ddata->state) { + switch (ddata->status) { case POWER_SUPPLY_STATUS_CHARGING: if (s.chrgcurr2) break; From patchwork Sun Jan 10 19:53:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Lindgren X-Patchwork-Id: 360763 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 13F78C4332D for ; Sun, 10 Jan 2021 19:55:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DCFC122A84 for ; Sun, 10 Jan 2021 19:55:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726768AbhAJTzc (ORCPT ); Sun, 10 Jan 2021 14:55:32 -0500 Received: from muru.com ([72.249.23.125]:42502 "EHLO muru.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726686AbhAJTzc (ORCPT ); Sun, 10 Jan 2021 14:55:32 -0500 Received: from hillo.muru.com (localhost [127.0.0.1]) by muru.com (Postfix) with ESMTP id 26D0E88B4; Sun, 10 Jan 2021 19:54:22 +0000 (UTC) From: Tony Lindgren To: Sebastian Reichel Cc: linux-pm@vger.kernel.org, linux-omap@vger.kernel.org, Arthur Demchenkov , Carl Philipp Klemm , Merlijn Wajer , Pavel Machek Subject: [PATCH 08/15] power: supply: cpcap-charger: Simplify things with enable and disable Date: Sun, 10 Jan 2021 21:53:56 +0200 Message-Id: <20210110195403.13758-9-tony@atomide.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210110195403.13758-1-tony@atomide.com> References: <20210110195403.13758-1-tony@atomide.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Let's turn cpcap_charger_set_state() into separate cpcap_charger_enable() and cpcap_charger_disable() to simplify things, and to allow managing status separately. This can be then used for the follow-up patches to make battery full status behave a bit nicer. Cc: Arthur Demchenkov Cc: Carl Philipp Klemm Cc: Merlijn Wajer Cc: Pavel Machek Signed-off-by: Tony Lindgren --- drivers/power/supply/cpcap-charger.c | 81 ++++++++++++++-------------- 1 file changed, 39 insertions(+), 42 deletions(-) diff --git a/drivers/power/supply/cpcap-charger.c b/drivers/power/supply/cpcap-charger.c --- a/drivers/power/supply/cpcap-charger.c +++ b/drivers/power/supply/cpcap-charger.c @@ -418,32 +418,30 @@ static void cpcap_charger_update_state(struct cpcap_charger_ddata *ddata, dev_dbg(ddata->dev, "state: %s\n", status); } -static int cpcap_charger_set_state(struct cpcap_charger_ddata *ddata, - int max_voltage, int charge_current, - int trickle_current) +static int cpcap_charger_disable(struct cpcap_charger_ddata *ddata) { - bool enable; int error; - enable = (charge_current || trickle_current); - dev_dbg(ddata->dev, "%s enable: %i\n", __func__, enable); + error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, 0x3fff, + CPCAP_REG_CRM_FET_OVRD | + CPCAP_REG_CRM_FET_CTRL); + if (error) + dev_err(ddata->dev, "%s failed with %i\n", __func__, error); - if (!enable) { - error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, - 0x3fff, - CPCAP_REG_CRM_FET_OVRD | - CPCAP_REG_CRM_FET_CTRL); - if (error) { - cpcap_charger_update_state(ddata, - POWER_SUPPLY_STATUS_UNKNOWN); - goto out_err; - } + return error; +} - cpcap_charger_update_state(ddata, - POWER_SUPPLY_STATUS_DISCHARGING); +static int cpcap_charger_enable(struct cpcap_charger_ddata *ddata, + int max_voltage, int charge_current, + int trickle_current) +{ + int error; - return 0; - } + if (!max_voltage || !charge_current) + return -EINVAL; + + dev_dbg(ddata->dev, "enable: %i %i %i\n", + max_voltage, charge_current, trickle_current); error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, 0x3fff, CPCAP_REG_CRM_CHRG_LED_EN | @@ -452,19 +450,8 @@ static int cpcap_charger_set_state(struct cpcap_charger_ddata *ddata, CPCAP_REG_CRM_FET_CTRL | max_voltage | charge_current); - if (error) { - cpcap_charger_update_state(ddata, - POWER_SUPPLY_STATUS_UNKNOWN); - goto out_err; - } - - cpcap_charger_update_state(ddata, - POWER_SUPPLY_STATUS_CHARGING); - - return 0; - -out_err: - dev_err(ddata->dev, "%s failed with %i\n", __func__, error); + if (error) + dev_err(ddata->dev, "%s failed with %i\n", __func__, error); return error; } @@ -506,10 +493,13 @@ static void cpcap_charger_vbus_work(struct work_struct *work) cpcap_charger_set_cable_path(ddata, false); cpcap_charger_set_inductive_path(ddata, false); - error = cpcap_charger_set_state(ddata, 0, 0, 0); + error = cpcap_charger_disable(ddata); if (error) goto out_err; + cpcap_charger_update_state(ddata, + POWER_SUPPLY_STATUS_DISCHARGING); + error = regmap_update_bits(ddata->reg, CPCAP_REG_VUSBC, CPCAP_BIT_VBUS_SWITCH, CPCAP_BIT_VBUS_SWITCH); @@ -540,6 +530,7 @@ static void cpcap_charger_vbus_work(struct work_struct *work) return; out_err: + cpcap_charger_update_state(ddata, POWER_SUPPLY_STATUS_UNKNOWN); dev_err(ddata->dev, "%s could not %s vbus: %i\n", __func__, ddata->vbus_enabled ? "enable" : "disable", error); } @@ -622,9 +613,11 @@ static void cpcap_charger_disconnect(struct cpcap_charger_ddata *ddata, { int error; - error = cpcap_charger_set_state(ddata, 0, 0, 0); - if (error) + error = cpcap_charger_disable(ddata); + if (error) { + cpcap_charger_update_state(ddata, POWER_SUPPLY_STATUS_UNKNOWN); return; + } cpcap_charger_update_state(ddata, state); power_supply_changed(ddata->usb); @@ -700,15 +693,15 @@ static void cpcap_usb_detect(struct work_struct *work) ichrg = cpcap_charger_current_to_regval(max_current); vchrg = cpcap_charger_voltage_to_regval(ddata->voltage); - error = cpcap_charger_set_state(ddata, - CPCAP_REG_CRM_VCHRG(vchrg), - ichrg, 0); + error = cpcap_charger_enable(ddata, + CPCAP_REG_CRM_VCHRG(vchrg), + ichrg, 0); if (error) goto out_err; cpcap_charger_update_state(ddata, POWER_SUPPLY_STATUS_CHARGING); } else { - error = cpcap_charger_set_state(ddata, 0, 0, 0); + error = cpcap_charger_disable(ddata); if (error) goto out_err; cpcap_charger_update_state(ddata, @@ -719,6 +712,7 @@ static void cpcap_usb_detect(struct work_struct *work) return; out_err: + cpcap_charger_update_state(ddata, POWER_SUPPLY_STATUS_UNKNOWN); dev_err(ddata->dev, "%s failed with %i\n", __func__, error); } @@ -936,10 +930,13 @@ static void cpcap_charger_shutdown(struct platform_device *pdev) dev_warn(ddata->dev, "could not clear USB comparator: %i\n", error); - error = cpcap_charger_set_state(ddata, 0, 0, 0); - if (error) + error = cpcap_charger_disable(ddata); + if (error) { + cpcap_charger_update_state(ddata, POWER_SUPPLY_STATUS_UNKNOWN); dev_warn(ddata->dev, "could not clear charger: %i\n", error); + } + cpcap_charger_update_state(ddata, POWER_SUPPLY_STATUS_DISCHARGING); cancel_delayed_work_sync(&ddata->vbus_work); cancel_delayed_work_sync(&ddata->detect_work); } From patchwork Sun Jan 10 19:53:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Lindgren X-Patchwork-Id: 360306 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 15A47C4332B for ; Sun, 10 Jan 2021 19:55:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E0A6B22A84 for ; Sun, 10 Jan 2021 19:55:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726829AbhAJTze (ORCPT ); Sun, 10 Jan 2021 14:55:34 -0500 Received: from muru.com ([72.249.23.125]:42506 "EHLO muru.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726479AbhAJTzd (ORCPT ); Sun, 10 Jan 2021 14:55:33 -0500 Received: from hillo.muru.com (localhost [127.0.0.1]) by muru.com (Postfix) with ESMTP id F3F0C88C3; Sun, 10 Jan 2021 19:54:23 +0000 (UTC) From: Tony Lindgren To: Sebastian Reichel Cc: linux-pm@vger.kernel.org, linux-omap@vger.kernel.org, Arthur Demchenkov , Carl Philipp Klemm , Merlijn Wajer , Pavel Machek Subject: [PATCH 09/15] power: supply: cpcap-charger: Provide state updates for battery from charger Date: Sun, 10 Jan 2021 21:53:57 +0200 Message-Id: <20210110195403.13758-10-tony@atomide.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210110195403.13758-1-tony@atomide.com> References: <20210110195403.13758-1-tony@atomide.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org We want to have the battery update it's status when the charge is done, and when the charger is disconnected. Otherwise the battery does not know when it's full unless there's a userspace app polling the battery status. To do this, we add supplied_to handling to cpcap-battery, and implement power_supply_changed() for cpcap-charger. When cpcap-charger calls power_supply_changed(), cpcap-battery will update it's status. Let's also use new_state variable for the POWER_SUPPLY_STATUS_CHARGING case to have unified handling for the switch. Cc: Arthur Demchenkov Cc: Carl Philipp Klemm Cc: Merlijn Wajer Cc: Pavel Machek Signed-off-by: Tony Lindgren --- drivers/power/supply/cpcap-battery.c | 13 +++++++++++ drivers/power/supply/cpcap-charger.c | 34 ++++++++++++++++++++++------ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/drivers/power/supply/cpcap-battery.c b/drivers/power/supply/cpcap-battery.c --- a/drivers/power/supply/cpcap-battery.c +++ b/drivers/power/supply/cpcap-battery.c @@ -416,6 +416,18 @@ static int cpcap_battery_update_status(struct cpcap_battery_ddata *ddata) return 0; } +/* + * Update battery status when cpcap-charger calls power_supply_changed(). + * This allows us to detect battery full condition before the charger + * disconnects. + */ +static void cpcap_battery_external_power_changed(struct power_supply *psy) +{ + union power_supply_propval prop; + + power_supply_get_property(psy, POWER_SUPPLY_PROP_STATUS, &prop); +} + static enum power_supply_property cpcap_battery_props[] = { POWER_SUPPLY_PROP_STATUS, POWER_SUPPLY_PROP_PRESENT, @@ -914,6 +926,7 @@ static int cpcap_battery_probe(struct platform_device *pdev) psy_desc->get_property = cpcap_battery_get_property; psy_desc->set_property = cpcap_battery_set_property; psy_desc->property_is_writeable = cpcap_battery_property_is_writeable; + psy_desc->external_power_changed = cpcap_battery_external_power_changed; psy_cfg.of_node = pdev->dev.of_node; psy_cfg.drv_data = ddata; diff --git a/drivers/power/supply/cpcap-charger.c b/drivers/power/supply/cpcap-charger.c --- a/drivers/power/supply/cpcap-charger.c +++ b/drivers/power/supply/cpcap-charger.c @@ -613,6 +613,16 @@ static void cpcap_charger_disconnect(struct cpcap_charger_ddata *ddata, { int error; + /* Update battery state before disconnecting the charger */ + switch (state) { + case POWER_SUPPLY_STATUS_DISCHARGING: + case POWER_SUPPLY_STATUS_FULL: + power_supply_changed(ddata->usb); + break; + default: + break; + } + error = cpcap_charger_disable(ddata); if (error) { cpcap_charger_update_state(ddata, POWER_SUPPLY_STATUS_UNKNOWN); @@ -628,7 +638,7 @@ static void cpcap_usb_detect(struct work_struct *work) { struct cpcap_charger_ddata *ddata; struct cpcap_charger_ints_state s; - int error; + int error, new_state; ddata = container_of(work, struct cpcap_charger_ddata, detect_work.work); @@ -662,19 +672,23 @@ static void cpcap_usb_detect(struct work_struct *work) case POWER_SUPPLY_STATUS_CHARGING: if (s.chrgcurr2) break; + new_state = POWER_SUPPLY_STATUS_FULL; + if (s.chrgcurr1 && s.vbusvld) { - cpcap_charger_disconnect(ddata, - POWER_SUPPLY_STATUS_FULL, - HZ * 5); + cpcap_charger_disconnect(ddata, new_state, HZ * 5); return; } break; case POWER_SUPPLY_STATUS_FULL: if (!s.chrgcurr2) break; - cpcap_charger_disconnect(ddata, - POWER_SUPPLY_STATUS_NOT_CHARGING, - HZ * 5); + if (s.vbusvld) + new_state = POWER_SUPPLY_STATUS_NOT_CHARGING; + else + new_state = POWER_SUPPLY_STATUS_DISCHARGING; + + cpcap_charger_disconnect(ddata, new_state, HZ * 5); + return; default: break; @@ -832,6 +846,10 @@ static int cpcap_charger_init_iio(struct cpcap_charger_ddata *ddata) return error; } +static char *cpcap_charger_supplied_to[] = { + "battery", +}; + static const struct power_supply_desc cpcap_charger_usb_desc = { .name = "usb", .type = POWER_SUPPLY_TYPE_USB, @@ -889,6 +907,8 @@ static int cpcap_charger_probe(struct platform_device *pdev) psy_cfg.of_node = pdev->dev.of_node; psy_cfg.drv_data = ddata; + psy_cfg.supplied_to = cpcap_charger_supplied_to; + psy_cfg.num_supplicants = ARRAY_SIZE(cpcap_charger_supplied_to), ddata->usb = devm_power_supply_register(ddata->dev, &cpcap_charger_usb_desc, From patchwork Sun Jan 10 19:53:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Lindgren X-Patchwork-Id: 360762 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7162DC43331 for ; Sun, 10 Jan 2021 19:55:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4BBA622A84 for ; Sun, 10 Jan 2021 19:55:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726819AbhAJTzd (ORCPT ); Sun, 10 Jan 2021 14:55:33 -0500 Received: from muru.com ([72.249.23.125]:42508 "EHLO muru.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726787AbhAJTzd (ORCPT ); Sun, 10 Jan 2021 14:55:33 -0500 Received: from hillo.muru.com (localhost [127.0.0.1]) by muru.com (Postfix) with ESMTP id BC42288CC; Sun, 10 Jan 2021 19:54:25 +0000 (UTC) From: Tony Lindgren To: Sebastian Reichel Cc: linux-pm@vger.kernel.org, linux-omap@vger.kernel.org, Arthur Demchenkov , Carl Philipp Klemm , Merlijn Wajer , Pavel Machek Subject: [PATCH 10/15] power: supply: cpcap-battery: Use charger status for battery full detection Date: Sun, 10 Jan 2021 21:53:58 +0200 Message-Id: <20210110195403.13758-11-tony@atomide.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210110195403.13758-1-tony@atomide.com> References: <20210110195403.13758-1-tony@atomide.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org We now get battery full notification from cpcap-charger, so let's use that for battery full status and charger disconnect. Note that any current based battery full detection we have tried earlier is flakey as it won't account for example for CPU load increasing the battery current. Anyways, if current based battery full detection is also still needed, we can reconsider adding it in addition to the charger status based detection. Cc: Arthur Demchenkov Cc: Carl Philipp Klemm Cc: Merlijn Wajer Cc: Pavel Machek Signed-off-by: Tony Lindgren --- drivers/power/supply/cpcap-battery.c | 56 ++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/drivers/power/supply/cpcap-battery.c b/drivers/power/supply/cpcap-battery.c --- a/drivers/power/supply/cpcap-battery.c +++ b/drivers/power/supply/cpcap-battery.c @@ -135,6 +135,7 @@ struct cpcap_battery_ddata { atomic_t active; int status; u16 vendor; + unsigned int is_full:1; }; #define CPCAP_NO_BATTERY -400 @@ -371,15 +372,62 @@ static int cpcap_battery_cc_get_avg_current(struct cpcap_battery_ddata *ddata) return cpcap_battery_cc_to_ua(ddata, sample, acc, offset); } +static int cpcap_battery_get_charger_status(struct cpcap_battery_ddata *ddata, + int *val) +{ + union power_supply_propval prop; + struct power_supply *charger; + int error; + + charger = power_supply_get_by_name("usb"); + if (!charger) + return -ENODEV; + + error = power_supply_get_property(charger, POWER_SUPPLY_PROP_STATUS, + &prop); + if (error) + *val = POWER_SUPPLY_STATUS_UNKNOWN; + else + *val = prop.intval; + + power_supply_put(charger); + + return error; +} + static bool cpcap_battery_full(struct cpcap_battery_ddata *ddata) { struct cpcap_battery_state_data *state = cpcap_battery_latest(ddata); + unsigned int vfull; + int error, val; + + error = cpcap_battery_get_charger_status(ddata, &val); + if (!error) { + switch (val) { + case POWER_SUPPLY_STATUS_DISCHARGING: + dev_dbg(ddata->dev, "charger disconnected\n"); + ddata->is_full = 0; + break; + case POWER_SUPPLY_STATUS_FULL: + dev_dbg(ddata->dev, "charger full status\n"); + ddata->is_full = 1; + break; + default: + break; + } + } + + /* + * The full battery voltage here can be inaccurate, it's used just to + * filter out any trickle charging events. We clear the is_full status + * on charger disconnect above anyways. + */ + vfull = ddata->config.bat.constant_charge_voltage_max_uv - 120000; - if (state->voltage >= - (ddata->config.bat.constant_charge_voltage_max_uv - 18000)) - return true; + if (ddata->is_full && state->voltage < vfull) + ddata->is_full = 0; - return false; + return ddata->is_full; } static int cpcap_battery_update_status(struct cpcap_battery_ddata *ddata) From patchwork Sun Jan 10 19:53:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tony Lindgren X-Patchwork-Id: 360304 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 43B1FC433E9 for ; Sun, 10 Jan 2021 19:55:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1912D225A9 for ; Sun, 10 Jan 2021 19:55:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726868AbhAJTzf (ORCPT ); Sun, 10 Jan 2021 14:55:35 -0500 Received: from muru.com ([72.249.23.125]:42512 "EHLO muru.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726479AbhAJTzf (ORCPT ); Sun, 10 Jan 2021 14:55:35 -0500 Received: from hillo.muru.com (localhost [127.0.0.1]) by muru.com (Postfix) with ESMTP id 99F4188D7; Sun, 10 Jan 2021 19:54:27 +0000 (UTC) From: Tony Lindgren To: Sebastian Reichel Cc: linux-pm@vger.kernel.org, linux-omap@vger.kernel.org, Arthur Demchenkov , Carl Philipp Klemm , Merlijn Wajer , Pavel Machek Subject: [PATCH 11/15] power: supply: cpcap-battery: Add charge_full property Date: Sun, 10 Jan 2021 21:53:59 +0200 Message-Id: <20210110195403.13758-12-tony@atomide.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210110195403.13758-1-tony@atomide.com> References: <20210110195403.13758-1-tony@atomide.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org From: Arthur Demchenkov Add charge_full property and let user update it. This is needed for capacity reporting in the following patches. Cc: Arthur Demchenkov Cc: Carl Philipp Klemm Cc: Merlijn Wajer Cc: Pavel Machek Signed-off-by: Arthur Demchenkov [tony@atomide.com: updated comments] Signed-off-by: Tony Lindgren --- drivers/power/supply/cpcap-battery.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/drivers/power/supply/cpcap-battery.c b/drivers/power/supply/cpcap-battery.c --- a/drivers/power/supply/cpcap-battery.c +++ b/drivers/power/supply/cpcap-battery.c @@ -133,6 +133,7 @@ struct cpcap_battery_ddata { struct cpcap_battery_state_data state[CPCAP_BATTERY_STATE_NR]; u32 cc_lsb; /* μAms per LSB */ atomic_t active; + int charge_full; int status; u16 vendor; unsigned int is_full:1; @@ -486,6 +487,7 @@ static enum power_supply_property cpcap_battery_props[] = { POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, POWER_SUPPLY_PROP_CURRENT_AVG, POWER_SUPPLY_PROP_CURRENT_NOW, + POWER_SUPPLY_PROP_CHARGE_FULL, POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, POWER_SUPPLY_PROP_CHARGE_COUNTER, POWER_SUPPLY_PROP_POWER_NOW, @@ -594,6 +596,11 @@ static int cpcap_battery_get_property(struct power_supply *psy, else val->intval = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; break; + case POWER_SUPPLY_PROP_CHARGE_FULL: + if (!ddata->charge_full) + return -ENODATA; + val->intval = ddata->charge_full; + break; case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: val->intval = ddata->config.info.charge_full_design; break; @@ -661,6 +668,15 @@ static int cpcap_battery_set_property(struct power_supply *psy, ddata->config.bat.constant_charge_voltage_max_uv = val->intval; return cpcap_battery_update_charger(ddata, val->intval); + case POWER_SUPPLY_PROP_CHARGE_FULL: + if (val->intval < 0) + return -EINVAL; + if (val->intval > ddata->config.info.charge_full_design) + return -EINVAL; + + ddata->charge_full = val->intval; + + return 0; default: return -EINVAL; } @@ -673,6 +689,7 @@ static int cpcap_battery_property_is_writeable(struct power_supply *psy, { switch (psp) { case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: + case POWER_SUPPLY_PROP_CHARGE_FULL: return 1; default: return 0; From patchwork Sun Jan 10 19:54:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Lindgren X-Patchwork-Id: 360761 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 056D3C4332D for ; Sun, 10 Jan 2021 19:55:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C5466225A9 for ; Sun, 10 Jan 2021 19:55:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726479AbhAJTzg (ORCPT ); Sun, 10 Jan 2021 14:55:36 -0500 Received: from muru.com ([72.249.23.125]:42510 "EHLO muru.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726841AbhAJTzf (ORCPT ); Sun, 10 Jan 2021 14:55:35 -0500 Received: from hillo.muru.com (localhost [127.0.0.1]) by muru.com (Postfix) with ESMTP id 66FE588E0; Sun, 10 Jan 2021 19:54:29 +0000 (UTC) From: Tony Lindgren To: Sebastian Reichel Cc: linux-pm@vger.kernel.org, linux-omap@vger.kernel.org, Arthur Demchenkov , Carl Philipp Klemm , Merlijn Wajer , Pavel Machek Subject: [PATCH 12/15] power: supply: cpcap-battery: Keep track of state for capacity reporting Date: Sun, 10 Jan 2021 21:54:00 +0200 Message-Id: <20210110195403.13758-13-tony@atomide.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210110195403.13758-1-tony@atomide.com> References: <20210110195403.13758-1-tony@atomide.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org From: Arthur Demchenkov Save the battery empty and full states so we can use those to estimate the battery capacity in the following patches. If the user provides us with charge_full value (which it could save in a permanent storage between reboots), initialize low and high counter_uah with calculated values. If we hit battery low once, we should stick on reporting it until the charger is connected. This way low->counter_uah will be updated properly, and that will allow us to get more accurate charge_full value. Based on an earlier patch by Tony Lindgren with charge_full usage and other improvments done by Arthur Demchenkov. Cc: Arthur Demchenkov Cc: Carl Philipp Klemm Cc: Merlijn Wajer Cc: Pavel Machek Co-developed-by: Tony Lindgren Signed-off-by: Arthur Demchenkov [tony@atomide.com: combined earlier patches, updated comments] Signed-off-by: Tony Lindgren --- drivers/power/supply/cpcap-battery.c | 56 +++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/drivers/power/supply/cpcap-battery.c b/drivers/power/supply/cpcap-battery.c --- a/drivers/power/supply/cpcap-battery.c +++ b/drivers/power/supply/cpcap-battery.c @@ -111,6 +111,8 @@ struct cpcap_coulomb_counter_data { enum cpcap_battery_state { CPCAP_BATTERY_STATE_PREVIOUS, CPCAP_BATTERY_STATE_LATEST, + CPCAP_BATTERY_STATE_EMPTY, + CPCAP_BATTERY_STATE_FULL, CPCAP_BATTERY_STATE_NR, }; @@ -166,6 +168,18 @@ cpcap_battery_previous(struct cpcap_battery_ddata *ddata) return cpcap_battery_get_state(ddata, CPCAP_BATTERY_STATE_PREVIOUS); } +static struct cpcap_battery_state_data * +cpcap_battery_get_empty(struct cpcap_battery_ddata *ddata) +{ + return cpcap_battery_get_state(ddata, CPCAP_BATTERY_STATE_EMPTY); +} + +static struct cpcap_battery_state_data * +cpcap_battery_get_full(struct cpcap_battery_ddata *ddata) +{ + return cpcap_battery_get_state(ddata, CPCAP_BATTERY_STATE_FULL); +} + static int cpcap_charger_battery_temperature(struct cpcap_battery_ddata *ddata, int *value) { @@ -431,9 +445,23 @@ static bool cpcap_battery_full(struct cpcap_battery_ddata *ddata) return ddata->is_full; } +static bool cpcap_battery_low(struct cpcap_battery_ddata *ddata) +{ + struct cpcap_battery_state_data *state = cpcap_battery_latest(ddata); + static bool is_low; + + if (state->current_ua > 0 && (state->voltage <= 3350000 || is_low)) + is_low = true; + else + is_low = false; + + return is_low; +} + static int cpcap_battery_update_status(struct cpcap_battery_ddata *ddata) { - struct cpcap_battery_state_data state, *latest, *previous; + struct cpcap_battery_state_data state, *latest, *previous, + *empty, *full; ktime_t now; int error; @@ -462,6 +490,32 @@ static int cpcap_battery_update_status(struct cpcap_battery_ddata *ddata) memcpy(previous, latest, sizeof(*previous)); memcpy(latest, &state, sizeof(*latest)); + if (cpcap_battery_full(ddata)) { + full = cpcap_battery_get_full(ddata); + memcpy(full, latest, sizeof(*full)); + + empty = cpcap_battery_get_empty(ddata); + if (empty->voltage && empty->voltage != -1) { + empty->voltage = -1; + ddata->charge_full = + empty->counter_uah - full->counter_uah; + } else if (ddata->charge_full) { + empty->voltage = -1; + empty->counter_uah = + full->counter_uah + ddata->charge_full; + } + } else if (cpcap_battery_low(ddata)) { + empty = cpcap_battery_get_empty(ddata); + memcpy(empty, latest, sizeof(*empty)); + + full = cpcap_battery_get_full(ddata); + if (full->voltage) { + full->voltage = 0; + ddata->charge_full = + empty->counter_uah - full->counter_uah; + } + } + return 0; } From patchwork Sun Jan 10 19:54:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Lindgren X-Patchwork-Id: 360305 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 67B3EC43217 for ; Sun, 10 Jan 2021 19:55:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 28F8E225A9 for ; Sun, 10 Jan 2021 19:55:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726897AbhAJTzh (ORCPT ); Sun, 10 Jan 2021 14:55:37 -0500 Received: from muru.com ([72.249.23.125]:42516 "EHLO muru.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726841AbhAJTzg (ORCPT ); Sun, 10 Jan 2021 14:55:36 -0500 Received: from hillo.muru.com (localhost [127.0.0.1]) by muru.com (Postfix) with ESMTP id 39B608931; Sun, 10 Jan 2021 19:54:31 +0000 (UTC) From: Tony Lindgren To: Sebastian Reichel Cc: linux-pm@vger.kernel.org, linux-omap@vger.kernel.org, Arthur Demchenkov , Carl Philipp Klemm , Merlijn Wajer , Pavel Machek Subject: [PATCH 13/15] power: supply: cpcap-battery: Implement capacity reporting Date: Sun, 10 Jan 2021 21:54:01 +0200 Message-Id: <20210110195403.13758-14-tony@atomide.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210110195403.13758-1-tony@atomide.com> References: <20210110195403.13758-1-tony@atomide.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org From: Arthur Demchenkov Calculate percentage using charge_full value provided. Cc: Arthur Demchenkov Cc: Carl Philipp Klemm Cc: Merlijn Wajer Cc: Pavel Machek Signed-off-by: Arthur Demchenkov [tony@atomide.com: updated to apply after dropping my earlier patch] Signed-off-by: Tony Lindgren --- drivers/power/supply/cpcap-battery.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/drivers/power/supply/cpcap-battery.c b/drivers/power/supply/cpcap-battery.c --- a/drivers/power/supply/cpcap-battery.c +++ b/drivers/power/supply/cpcap-battery.c @@ -546,6 +546,7 @@ static enum power_supply_property cpcap_battery_props[] = { POWER_SUPPLY_PROP_CHARGE_COUNTER, POWER_SUPPLY_PROP_POWER_NOW, POWER_SUPPLY_PROP_POWER_AVG, + POWER_SUPPLY_PROP_CAPACITY, POWER_SUPPLY_PROP_CAPACITY_LEVEL, POWER_SUPPLY_PROP_SCOPE, POWER_SUPPLY_PROP_TEMP, @@ -556,7 +557,7 @@ static int cpcap_battery_get_property(struct power_supply *psy, union power_supply_propval *val) { struct cpcap_battery_ddata *ddata = power_supply_get_drvdata(psy); - struct cpcap_battery_state_data *latest, *previous; + struct cpcap_battery_state_data *latest, *previous, *empty; u32 sample; s32 accumulator; int cached; @@ -636,6 +637,16 @@ static int cpcap_battery_get_property(struct power_supply *psy, tmp *= ((latest->voltage + previous->voltage) / 20000); val->intval = div64_s64(tmp, 100); break; + case POWER_SUPPLY_PROP_CAPACITY: + empty = cpcap_battery_get_empty(ddata); + if (!empty->voltage || !ddata->charge_full) + return -ENODATA; + /* (ddata->charge_full / 200) is needed for rounding */ + val->intval = empty->counter_uah - latest->counter_uah + + ddata->charge_full / 200; + val->intval = clamp(val->intval, 0, ddata->charge_full); + val->intval = val->intval * 100 / ddata->charge_full; + break; case POWER_SUPPLY_PROP_CAPACITY_LEVEL: if (cpcap_battery_full(ddata)) val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL; From patchwork Sun Jan 10 19:54:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Lindgren X-Patchwork-Id: 360760 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0D230C43333 for ; Sun, 10 Jan 2021 19:55:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CDC8322A84 for ; Sun, 10 Jan 2021 19:55:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726884AbhAJTzh (ORCPT ); Sun, 10 Jan 2021 14:55:37 -0500 Received: from muru.com ([72.249.23.125]:42514 "EHLO muru.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726787AbhAJTzg (ORCPT ); Sun, 10 Jan 2021 14:55:36 -0500 Received: from hillo.muru.com (localhost [127.0.0.1]) by muru.com (Postfix) with ESMTP id E6BA489DA; Sun, 10 Jan 2021 19:54:32 +0000 (UTC) From: Tony Lindgren To: Sebastian Reichel Cc: linux-pm@vger.kernel.org, linux-omap@vger.kernel.org, Arthur Demchenkov , Carl Philipp Klemm , Merlijn Wajer , Pavel Machek Subject: [PATCH 14/15] power: supply: cpcap-battery: Add charge_now property Date: Sun, 10 Jan 2021 21:54:02 +0200 Message-Id: <20210110195403.13758-15-tony@atomide.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210110195403.13758-1-tony@atomide.com> References: <20210110195403.13758-1-tony@atomide.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org From: Arthur Demchenkov Add charge_now property for capacity reporting. Cc: Arthur Demchenkov Cc: Carl Philipp Klemm Cc: Merlijn Wajer Cc: Pavel Machek Signed-off-by: Arthur Demchenkov [tony@atomide.com: updated to apply for naming changes] Signed-off-by: Tony Lindgren --- drivers/power/supply/cpcap-battery.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/drivers/power/supply/cpcap-battery.c b/drivers/power/supply/cpcap-battery.c --- a/drivers/power/supply/cpcap-battery.c +++ b/drivers/power/supply/cpcap-battery.c @@ -542,6 +542,7 @@ static enum power_supply_property cpcap_battery_props[] = { POWER_SUPPLY_PROP_CURRENT_AVG, POWER_SUPPLY_PROP_CURRENT_NOW, POWER_SUPPLY_PROP_CHARGE_FULL, + POWER_SUPPLY_PROP_CHARGE_NOW, POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, POWER_SUPPLY_PROP_CHARGE_COUNTER, POWER_SUPPLY_PROP_POWER_NOW, @@ -661,6 +662,16 @@ static int cpcap_battery_get_property(struct power_supply *psy, else val->intval = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; break; + case POWER_SUPPLY_PROP_CHARGE_NOW: + empty = cpcap_battery_get_empty(ddata); + if (!empty->voltage) + return -ENODATA; + val->intval = empty->counter_uah - latest->counter_uah; + if (val->intval < 0) + val->intval = 0; + else if (ddata->charge_full && ddata->charge_full < val->intval) + val->intval = ddata->charge_full; + break; case POWER_SUPPLY_PROP_CHARGE_FULL: if (!ddata->charge_full) return -ENODATA; From patchwork Sun Jan 10 19:54:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Lindgren X-Patchwork-Id: 360759 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3AA3DC433DB for ; Sun, 10 Jan 2021 19:55:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1554A22B51 for ; Sun, 10 Jan 2021 19:55:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726906AbhAJTzq (ORCPT ); Sun, 10 Jan 2021 14:55:46 -0500 Received: from muru.com ([72.249.23.125]:42498 "EHLO muru.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726841AbhAJTzq (ORCPT ); Sun, 10 Jan 2021 14:55:46 -0500 Received: from hillo.muru.com (localhost [127.0.0.1]) by muru.com (Postfix) with ESMTP id B5C5489E4; Sun, 10 Jan 2021 19:54:34 +0000 (UTC) From: Tony Lindgren To: Sebastian Reichel Cc: linux-pm@vger.kernel.org, linux-omap@vger.kernel.org, Pavel Machek , Arthur Demchenkov , Carl Philipp Klemm , Merlijn Wajer Subject: [PATCH 15/15] power: supply: cpcap-battery: Fix typo Date: Sun, 10 Jan 2021 21:54:03 +0200 Message-Id: <20210110195403.13758-16-tony@atomide.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210110195403.13758-1-tony@atomide.com> References: <20210110195403.13758-1-tony@atomide.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org From: Pavel Machek Fix typo Cc: Arthur Demchenkov Cc: Carl Philipp Klemm Cc: Merlijn Wajer Cc: Pavel Machek Signed-off-by: Pavel Machek [tony@atomide.com: separated out from charger changes] Signed-off-by: Tony Lindgren --- drivers/power/supply/cpcap-battery.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/power/supply/cpcap-battery.c b/drivers/power/supply/cpcap-battery.c --- a/drivers/power/supply/cpcap-battery.c +++ b/drivers/power/supply/cpcap-battery.c @@ -3,7 +3,7 @@ * * Copyright (C) 2017 Tony Lindgren * - * Some parts of the code based on earlie Motorola mapphone Linux kernel + * Some parts of the code based on earlier Motorola mapphone Linux kernel * drivers: * * Copyright (C) 2009-2010 Motorola, Inc.