Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp2423660pxb; Tue, 23 Feb 2021 06:49:25 -0800 (PST) X-Google-Smtp-Source: ABdhPJyREj4yijnwohviWjNZEEx/7Scud5B36dI2yBwiK4DuG841feNWldFoXf08+ixbYIHc26z6 X-Received: by 2002:a17:906:b0d8:: with SMTP id bk24mr26928300ejb.252.1614091765567; Tue, 23 Feb 2021 06:49:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614091765; cv=none; d=google.com; s=arc-20160816; b=vmPBM6Ibed1nudrM7Q9e7Xh+MFhpq7ARpuUvBeG+T63uCsu4pnnYbOuZTI5T8zX3by s1b8MDyXjLvisAL+DO5UEVHebeXu4PYxzA5a+SHy4/Fk5K6guxq0W9uBGgZ/uvaOnW5E MYq3KJiNb+5PSto8DtQEL5lr++zwn95iXt2GdcNXviNYEpm7WkXOKM0fHWnFhSRtXqOZ 3mvujFHIUdpO8WUopbRod20yY4arI6nX5hlpwbB+sP5LR8MhqYR0Jdvom5a0VViqApZL /DG2F8VtEcGHyMDykZMHf8v1tgwal393td6u87zpwTx9AtPT8yNRRieYpK4Wk3hsWhuI je4Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :ironport-sdr:dkim-signature:ironport-sdr:dkim-signature; bh=6kfVrHptT2m4Z2h538qhv6uyJzGYhE1c2XuBDP1felc=; b=LnF5xG3dd+9OAiHjqHAQ70TLQ3QBGPYwynJfyO630GN0+205L/32c4iQ73xjfHzz+g n2gRRywoP9NjeK74wBi7cdoIBzZhVIHAcPQ/ovpZdHD0K6H/20majI1sDb/5Kjc4ILRL 9R08E9zXO9EPTvfe95ZX9vGD3E7EeeWxGnrkSWW6mKfR2BTman1kxZqpMmIB7tmx2MaX +p3//v+3spHsKEodhXH4Miw672hWwt3k3WKq7LrY7tpdJzAGSkX8rnBSHyxHrmMnsWKB GIdx8onP8HDEF2Ytehj9ah0+aeNbAyGVMWp94pVXj1WlKoyf4ixQVPa2g4h8/yG01r4f zJLg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@tq-group.com header.s=key1 header.b=l+iBYJcl; dkim=pass header.i=@tq-group.com header.s=key1 header.b=QrhqqqWE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=tq-group.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b14si13247457ede.90.2021.02.23.06.49.01; Tue, 23 Feb 2021 06:49:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@tq-group.com header.s=key1 header.b=l+iBYJcl; dkim=pass header.i=@tq-group.com header.s=key1 header.b=QrhqqqWE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=tq-group.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232993AbhBWOWv (ORCPT + 99 others); Tue, 23 Feb 2021 09:22:51 -0500 Received: from mx1.tq-group.com ([93.104.207.81]:59661 "EHLO mx1.tq-group.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229849AbhBWOW3 (ORCPT ); Tue, 23 Feb 2021 09:22:29 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tq-group.com; i=@tq-group.com; q=dns/txt; s=key1; t=1614090148; x=1645626148; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=6kfVrHptT2m4Z2h538qhv6uyJzGYhE1c2XuBDP1felc=; b=l+iBYJclAT+JIzTUmkMKq06o/eQ5FsfeHsIg9yHyXZk4ratcEUWVkL8H J5llvBaPQsocCVheaxTliOTNO3WfmRHXgTGNz56lk4VCu7xhdfKKTNLQ3 c6aFr1cLGfKq8rwn/6M83Jkyb22QVGTrQA3o8o5r7YN1xA8wKbw8FPr5L HdgY4K633WBmbg1GfMl2amByz5VBGsxuVlN38HVZkep10LN1M+Dhnf9YO KXl6j1FuhGrDv5NbJsJhxKxlCZctki0lGBUGG37wlADAjsILcS2Nz0T/N lLYBdqWSaVAtfLeDrtqJ3COSlt7nPirMeOdc3qKzqni9A90PQXzywy3Ad Q==; IronPort-SDR: M1/PvlMBh2gMIuHLNvgvBF4ASJM0I14XYA/LWQbG7SJrt9ET9klj0RaEP6vOqjk8lHh3tq2q/l +CwWwI/+o6Q8stqHl9Rne2NxyvcjXI4Ehw+VrdZuhXMXEz9lzg61QOgpiQ3rrUxy36lR5WErYh BBuV4mVgiogma0MHG5d7hUqE3rkHceMIjQLUPu8G3YYsPORh/0KfQfkMonUE8VkmgUJ6gTBcuk eZnzwzFRvavGCSwxxk3rBzgL3KR9ZHn9HRQEBNsjV/6dsFcm9B5/ZQ840bTM6UQoEgOaK8dcYb eQc= X-IronPort-AV: E=Sophos;i="5.81,200,1610406000"; d="scan'208";a="16125542" Received: from unknown (HELO tq-pgp-pr1.tq-net.de) ([192.168.6.15]) by mx1-pgp.tq-group.com with ESMTP; 23 Feb 2021 15:12:05 +0100 Received: from mx1.tq-group.com ([192.168.6.7]) by tq-pgp-pr1.tq-net.de (PGP Universal service); Tue, 23 Feb 2021 15:12:05 +0100 X-PGP-Universal: processed; by tq-pgp-pr1.tq-net.de on Tue, 23 Feb 2021 15:12:05 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tq-group.com; i=@tq-group.com; q=dns/txt; s=key1; t=1614089525; x=1645625525; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=6kfVrHptT2m4Z2h538qhv6uyJzGYhE1c2XuBDP1felc=; b=QrhqqqWE7afLQWvwVQFWnkXjSrer63JCKrkhdAcxMf2165gIDta/6v3K grPCbpDGDmSKWk5ebSWlk58pRHCwRRoZbXBWMp1XsjQdMd6Zn/BSgyK8q fob1ZQOO/w4JmaxWNsS089eGUKFQRiP7Oj77/1utclhQMaQ2AhDWPyHHo 2XWOXKAThlbQilOMSKXdqcSFr+HgtDey0MT7GvlLYxu1KNoeoTxGQ2Tnz Vw5tQgwn5V+zHDx2yJ7LBmfh6u87X2TLjLFnuh4bbaUT9cU4KEi+Yvjc2 fDA8pkbXSwS1++bbHdYMmOzVEeemJbreRx4ewT77sSOFUMTx1BxamlOJ6 Q==; IronPort-SDR: Bq8RpNSgEWqrQsHFBFtqK6bAl87zBUprtSqW7+Xi5LLkfouFRtLMYtVHpeXUY+oywJaPqsK6yz GmMLMQy/Q99lMmlMhnNOzakII4xHbDbl4ZVsMHfLIGC7ibSdnbug3k1FKSJxtHvXyLvWy5eNdT 1VZBMqug+m+xfzyfdJifmlb+d28zC/jMeiXa3dAl1LzRp9xi8EF9TIB+7fM2K5E7TA5hjBYL0F 9Ghpw4GM59Pjy1fiXls7BF21e11PwtPjUC1DCKy/VbRJNK4FVQkYsVdwQPsNeF4O9noiT9+WAk 2EQ= X-IronPort-AV: E=Sophos;i="5.81,200,1610406000"; d="scan'208";a="16125541" Received: from vtuxmail01.tq-net.de ([10.115.0.20]) by mx1.tq-group.com with ESMTP; 23 Feb 2021 15:12:05 +0100 Received: from schifferm-ubuntu4.tq-net.de (schifferm-ubuntu4.tq-net.de [10.121.48.12]) by vtuxmail01.tq-net.de (Postfix) with ESMTPA id E1718280075; Tue, 23 Feb 2021 15:12:04 +0100 (CET) X-CheckPoint: {60350D2A-41-8E0BEC96-E002885D} X-MAIL-CPID: B1860B43A38DFC429C7436A9C70A0ED0_4 X-Control-Analysis: str=0001.0A782F1B.60350D35.003C,ss=1,re=0.000,recu=0.000,reip=0.000,cl=1,cld=1,fgs=0 From: Matthias Schiffer To: Dan Murphy , =?UTF-8?q?Pali=20Roh=C3=A1r?= , Sebastian Reichel Cc: Andreas Kemnade , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Matthias Schiffer Subject: [PATCH 2/3] power: supply: bq27xxx: fix power_avg Date: Tue, 23 Feb 2021 15:11:21 +0100 Message-Id: <20210223141122.9574-2-matthias.schiffer@ew.tq-group.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210223141122.9574-1-matthias.schiffer@ew.tq-group.com> References: <20210223141122.9574-1-matthias.schiffer@ew.tq-group.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On all newer bq27xxx ICs, the AveragePower register contains a signed value; in addition to handling the raw value as unsigned, the driver code also didn't convert it to µW as expected. At least for the BQ28Z610, the reference manual incorrectly states that the value is in units of 1mA and not 10mA. I have no way of knowing whether the manuals of other supported ICs contain the same error, or if there are models that actually use 1mA. At least, the new code shouldn't be *less* correct than the old version for any device. power_avg is removed from the cache structure, se we don't have to extend it to store both a signed value and an error code. Always getting an up-to-date value may be desirable anyways, as it avoids inconsistent current and power readings when switching between charging and discharging. Signed-off-by: Matthias Schiffer --- drivers/power/supply/bq27xxx_battery.c | 51 ++++++++++++++------------ include/linux/power/bq27xxx_battery.h | 1 - 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/drivers/power/supply/bq27xxx_battery.c b/drivers/power/supply/bq27xxx_battery.c index cb6ebd2f905e..20e1dc8a87cf 100644 --- a/drivers/power/supply/bq27xxx_battery.c +++ b/drivers/power/supply/bq27xxx_battery.c @@ -1661,27 +1661,6 @@ static int bq27xxx_battery_read_time(struct bq27xxx_device_info *di, u8 reg) return tval * 60; } -/* - * Read an average power register. - * Return < 0 if something fails. - */ -static int bq27xxx_battery_read_pwr_avg(struct bq27xxx_device_info *di) -{ - int tval; - - tval = bq27xxx_read(di, BQ27XXX_REG_AP, false); - if (tval < 0) { - dev_err(di->dev, "error reading average power register %02x: %d\n", - BQ27XXX_REG_AP, tval); - return tval; - } - - if (di->opts & BQ27XXX_O_ZERO) - return (tval * BQ27XXX_POWER_CONSTANT) / BQ27XXX_RS; - else - return tval; -} - /* * Returns true if a battery over temperature condition is detected */ @@ -1769,8 +1748,6 @@ void bq27xxx_battery_update(struct bq27xxx_device_info *di) } if (di->regs[BQ27XXX_REG_CYCT] != INVALID_REG_ADDR) cache.cycle_count = bq27xxx_battery_read_cyct(di); - if (di->regs[BQ27XXX_REG_AP] != INVALID_REG_ADDR) - cache.power_avg = bq27xxx_battery_read_pwr_avg(di); /* We only have to read charge design full once */ if (di->charge_design_full <= 0) @@ -1833,6 +1810,32 @@ static int bq27xxx_battery_current(struct bq27xxx_device_info *di, return 0; } +/* + * Get the average power in µW + * Return < 0 if something fails. + */ +static int bq27xxx_battery_pwr_avg(struct bq27xxx_device_info *di, + union power_supply_propval *val) +{ + int power; + + power = bq27xxx_read(di, BQ27XXX_REG_AP, false); + if (power < 0) { + dev_err(di->dev, + "error reading average power register %02x: %d\n", + BQ27XXX_REG_AP, power); + return power; + } + + if (di->opts & BQ27XXX_O_ZERO) + val->intval = (power * BQ27XXX_POWER_CONSTANT) / BQ27XXX_RS; + else + /* Other gauges return a signed value in units of 10mW */ + val->intval = (int)((s16)power) * 10000; + + return 0; +} + static int bq27xxx_battery_status(struct bq27xxx_device_info *di, union power_supply_propval *val) { @@ -2020,7 +2023,7 @@ static int bq27xxx_battery_get_property(struct power_supply *psy, ret = bq27xxx_simple_value(di->cache.energy, val); break; case POWER_SUPPLY_PROP_POWER_AVG: - ret = bq27xxx_simple_value(di->cache.power_avg, val); + ret = bq27xxx_battery_pwr_avg(di, val); break; case POWER_SUPPLY_PROP_HEALTH: ret = bq27xxx_simple_value(di->cache.health, val); diff --git a/include/linux/power/bq27xxx_battery.h b/include/linux/power/bq27xxx_battery.h index 111a40d0d3d5..8d5f4f40fb41 100644 --- a/include/linux/power/bq27xxx_battery.h +++ b/include/linux/power/bq27xxx_battery.h @@ -53,7 +53,6 @@ struct bq27xxx_reg_cache { int capacity; int energy; int flags; - int power_avg; int health; }; -- 2.17.1