Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp106798ybi; Thu, 1 Aug 2019 15:32:29 -0700 (PDT) X-Google-Smtp-Source: APXvYqwMzvrSAdYmZG5e2boDfefFYOpf5lOTEcEluXUX8N+eBqZLdzg/RUjNEyD0N4VtggOCnueH X-Received: by 2002:a17:90a:c20e:: with SMTP id e14mr1167782pjt.0.1564698749558; Thu, 01 Aug 2019 15:32:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564698749; cv=none; d=google.com; s=arc-20160816; b=t1jCHy+GYDE/RdSJ13hYnqSFQGALhUHvec+TeshMp4WavyV9MD+o4KxiMI7JtC35wU ymLLBZDDiF+LcS4F27efWcYbLVZ7rCCIXFEdykyxuu9aHuJ14qOOfNwiiqw22UXNpapg U55zIg597Kt9CmxEPvy5SaXJcS6FGIMfszTQ/SMqhFwIurIsjXBBabkqss3VkTMLl3U+ iJCyLigmc6V2AVRUaiuYsvz+cSxbKfyeks6XdnrzOj78zFq3y759usCakCurQ43cb8wT yr7vZx2wnVe1FoYwoJkGbD5qbaSBbsNWMLmCDJBIsENTXWQgxEMCAQ8fwb+At7UHt0F7 asMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=WUCcMouh0WO52+e0adQwOKzErR3ljNy4cs3bF3C1u1k=; b=TdcjlMAHdqAalRowXCIH22VWR38kKhuQF9ChgVqFJMq2i3T917WJFAeADKEFC4876Y WqeW2JbTBq/qKwE8C/QxWPMAkOECY+APjvzzX+68wNUZwFpi2kHsonoy8kAEjaMrxqOY gG+MHxWRy5PH5s6Twy5ge8R/4144AZOP+8bCMEpoBqMdSiZ2i/AxjeTf078NDgoidl1i gilP5I7oVz4qw7xSCzJQOq56HF59VisViqxZ/Eh963rFaNf0GJWy44V5aXc7Jtjop0kE T093eZOVV3k+IrcMP1JhjbAdm/RBockSAgn8i0t2dj64EW3mBNeHEh1Wcj65NaWqkESp 9FFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=izCAK6tb; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q2si34627378plh.59.2019.08.01.15.32.12; Thu, 01 Aug 2019 15:32:29 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=izCAK6tb; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729823AbfHASef (ORCPT + 99 others); Thu, 1 Aug 2019 14:34:35 -0400 Received: from mail-oi1-f195.google.com ([209.85.167.195]:37235 "EHLO mail-oi1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727218AbfHASef (ORCPT ); Thu, 1 Aug 2019 14:34:35 -0400 Received: by mail-oi1-f195.google.com with SMTP id t76so54784950oih.4 for ; Thu, 01 Aug 2019 11:34:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=WUCcMouh0WO52+e0adQwOKzErR3ljNy4cs3bF3C1u1k=; b=izCAK6tbIWTL6XLXegvw14s6+xaXPO8v5Qdl7Tf1/WnGZ819VyecArGftwnoelv8lA 5HGC4Xg4T4BzYB1KwMNYQ5mMLMXD0TD+NpT8s7QD1lxMihjVPfS36nyccDcWAM3TTmyg rdx7BsR+PqSEEEsa+tx0jCqrcEExhIRu4uBNg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=WUCcMouh0WO52+e0adQwOKzErR3ljNy4cs3bF3C1u1k=; b=GS9CY/AkJCYb0gNT75nykdhn7zsw1y9qidt5xIcS+x1Yb6hix6eeLQiIwfq70dJbGX 4gKEHdusdbgojKXk8WvJAhsLgQaKvc4nrf8p0/bbCMsz/qDna3HfZbxFB10etoItkWkr SoYN+zMUIg2pu15oOTueMoNFKxxvYVGsR0ZajtzMXrD+qW7+DKyD5nEu/hqztcQO3b84 F2DW79eyoU2U0+1DSXD3Xjep1P4gF0Ys3WfsJ27UpgHcVXhyFHutomLkgJjFiz7QqYm3 LRYs1ggc5cv93qMEpUWIkZ9Ot/vasr7xKMixExQ8jc+yoZ21O3ldhTML9uzsQsUznk7H AXqg== X-Gm-Message-State: APjAAAXuvwB+OXL+ycfUeBAW+M0uogyh+rwyp04LrZHjjdLkZtXa8CQQ h+28X/ekvUcKLRlRvnAvP/DBH1fduPM= X-Received: by 2002:aca:ecc1:: with SMTP id k184mr138002oih.82.1564684473164; Thu, 01 Aug 2019 11:34:33 -0700 (PDT) Received: from mail-oi1-f179.google.com (mail-oi1-f179.google.com. [209.85.167.179]) by smtp.gmail.com with ESMTPSA id o18sm27538092ote.63.2019.08.01.11.34.30 for (version=TLS1_3 cipher=AEAD-AES128-GCM-SHA256 bits=128/128); Thu, 01 Aug 2019 11:34:31 -0700 (PDT) Received: by mail-oi1-f179.google.com with SMTP id w196so33417218oie.7 for ; Thu, 01 Aug 2019 11:34:30 -0700 (PDT) X-Received: by 2002:aca:488e:: with SMTP id v136mr119143oia.49.1564684470454; Thu, 01 Aug 2019 11:34:30 -0700 (PDT) MIME-Version: 1.0 References: <1564452025-12673-1-git-send-email-rtresidd@electromag.com.au> In-Reply-To: <1564452025-12673-1-git-send-email-rtresidd@electromag.com.au> From: Nick Crews Date: Thu, 1 Aug 2019 12:34:18 -0600 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v4 1/1] power/supply/sbs-battery: Fix confusing battery status when idle or empty To: Richard Tresidder Cc: Sebastian Reichel , Enric Balletbo i Serra , andrew.smirnov@gmail.com, Guenter Roeck , david@lechnology.com, Thomas Gleixner , Kate Stewart , Greg Kroah-Hartman , rfontana@redhat.com, allison@lohutok.net, Baolin Wang , linux-pm@vger.kernel.org, linux-kernel Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Thanks Richard, I still would like some more opinions on this changing the userspace experience, but LGTM otherwise. Reviewed-by: Nick Crews On Mon, Jul 29, 2019 at 8:00 PM Richard Tresidder wrote: > > When a battery or batteries in a system are in parallel then one or more > may not be providing any current to the system. > This fixes an incorrect status indication of FULL for the battery simply > because it wasn't discharging at that point in time. > The battery will now be flagged as NOT CHARGING. > Have also added the additional check for the battery FULL DISCHARGED flag > which will now flag a status of EMPTY. > > Signed-off-by: Richard Tresidder > --- > > Notes: > power/supply/sbs-battery: Fix confusing battery status when idle or empty > > When a battery or batteries in a system are in parallel then one or more > may not be providing any current to the system. > This fixes an incorrect status indication of FULL for the battery simply > because it wasn't discharging at that point in time. > The battery will now be flagged as NOT CHARGING. > Have also added the additional check for the battery FULL DISCHARGED flag > which will now flag a status of EMPTY. > > v2: Missed a later merge that should have been included in original patch > v3: Refactor the sbs_status_correct function to capture all the states for > normal operation rather than being spread across multile functions. > v4: Remove unnecessary brackets, rename sbs_status_correct to > sbs_correct_battery_status > > drivers/power/supply/power_supply_sysfs.c | 2 +- > drivers/power/supply/sbs-battery.c | 46 ++++++++++++------------------- > include/linux/power_supply.h | 1 + > 3 files changed, 19 insertions(+), 30 deletions(-) > > diff --git a/drivers/power/supply/power_supply_sysfs.c b/drivers/power/supply/power_supply_sysfs.c > index f37ad4e..305e833 100644 > --- a/drivers/power/supply/power_supply_sysfs.c > +++ b/drivers/power/supply/power_supply_sysfs.c > @@ -51,7 +51,7 @@ > }; > > static const char * const power_supply_status_text[] = { > - "Unknown", "Charging", "Discharging", "Not charging", "Full" > + "Unknown", "Charging", "Discharging", "Not charging", "Full", "Empty" > }; > > static const char * const power_supply_charge_type_text[] = { > diff --git a/drivers/power/supply/sbs-battery.c b/drivers/power/supply/sbs-battery.c > index 048d205..3ed70d4 100644 > --- a/drivers/power/supply/sbs-battery.c > +++ b/drivers/power/supply/sbs-battery.c > @@ -283,7 +283,7 @@ static int sbs_write_word_data(struct i2c_client *client, u8 address, > return 0; > } > > -static int sbs_status_correct(struct i2c_client *client, int *intval) > +static int sbs_correct_battery_status(struct i2c_client *client, int *status) > { > int ret; > > @@ -293,16 +293,18 @@ static int sbs_status_correct(struct i2c_client *client, int *intval) > > ret = (s16)ret; > > - /* Not drawing current means full (cannot be not charging) */ > - if (ret == 0) > - *intval = POWER_SUPPLY_STATUS_FULL; > - > - if (*intval == POWER_SUPPLY_STATUS_FULL) { > - /* Drawing or providing current when full */ > - if (ret > 0) > - *intval = POWER_SUPPLY_STATUS_CHARGING; > - else if (ret < 0) > - *intval = POWER_SUPPLY_STATUS_DISCHARGING; > + if (ret > 0) > + *status = POWER_SUPPLY_STATUS_CHARGING; > + else if (ret < 0) > + *status = POWER_SUPPLY_STATUS_DISCHARGING; > + else { > + /* Current is 0, so how full is the battery? */ > + if (*status & BATTERY_FULL_CHARGED) > + *status = POWER_SUPPLY_STATUS_FULL; > + else if (*status & BATTERY_FULL_DISCHARGED) > + *status = POWER_SUPPLY_STATUS_EMPTY; > + else > + *status = POWER_SUPPLY_STATUS_NOT_CHARGING; > } > > return 0; > @@ -421,14 +423,9 @@ static int sbs_get_battery_property(struct i2c_client *client, > return 0; > } > > - if (ret & BATTERY_FULL_CHARGED) > - val->intval = POWER_SUPPLY_STATUS_FULL; > - else if (ret & BATTERY_DISCHARGING) > - val->intval = POWER_SUPPLY_STATUS_DISCHARGING; > - else > - val->intval = POWER_SUPPLY_STATUS_CHARGING; > - > - sbs_status_correct(client, &val->intval); > + ret = sbs_correct_battery_status(client, &val->intval); > + if (ret < 0) > + return ret; > > if (chip->poll_time == 0) > chip->last_state = val->intval; > @@ -773,20 +770,11 @@ static void sbs_delayed_work(struct work_struct *work) > > ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr); > /* if the read failed, give up on this work */ > - if (ret < 0) { > + if (ret < 0 || sbs_correct_battery_status(chip->client, &ret) < 0) { > chip->poll_time = 0; > return; > } > > - if (ret & BATTERY_FULL_CHARGED) > - ret = POWER_SUPPLY_STATUS_FULL; > - else if (ret & BATTERY_DISCHARGING) > - ret = POWER_SUPPLY_STATUS_DISCHARGING; > - else > - ret = POWER_SUPPLY_STATUS_CHARGING; > - > - sbs_status_correct(chip->client, &ret); > - > if (chip->last_state != ret) { > chip->poll_time = 0; > power_supply_changed(chip->power_supply); > diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h > index 28413f7..8fb10ec 100644 > --- a/include/linux/power_supply.h > +++ b/include/linux/power_supply.h > @@ -37,6 +37,7 @@ enum { > POWER_SUPPLY_STATUS_DISCHARGING, > POWER_SUPPLY_STATUS_NOT_CHARGING, > POWER_SUPPLY_STATUS_FULL, > + POWER_SUPPLY_STATUS_EMPTY, > }; > > /* What algorithm is the charger using? */ > -- > 1.8.3.1 >