From nobody Wed Dec 17 07:08:02 2025 Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05on2057.outbound.protection.outlook.com [40.107.22.57]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BDCE424EF61; Wed, 19 Mar 2025 09:42:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.22.57 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742377323; cv=fail; b=EMebJ0PtSRSRS8tU8hOpSCmJb2VGxNehgWOTkebl6EkSRC7kTnhxTn67Bm1LmOQrk2IJlSOSi82tWZmU2y9pF06gE9+wdGWJzGCv0OrMwFgKw95JsOS+q1PUHCWdyljO/qj150VSIyQk+K5yJEQZG147JAlLdmcbiU2t5qljX4w= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742377323; c=relaxed/simple; bh=uY5J503eyEUQCcdAp2EONXc6CJG5biszUwbp15im0qA=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=YqJl7r3L2p94MQzAI/6CojaCDd/v/NnxuN+JYJLioVGz6iK2eysG8MYDMRXTtPxSslxvVvYP4hlg4GLaCDCoU5xlomaft486bB2KQeqNVsMOU9ibe5DIjoEcB7jfxa8IkoiisM44aqSkixP/ukUFa6YUozqAoQY1549Z4+8OfYk= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=axis.com; spf=pass smtp.mailfrom=axis.com; dkim=pass (1024-bit key) header.d=axis.com header.i=@axis.com header.b=fWMfIa6c; arc=fail smtp.client-ip=40.107.22.57 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=axis.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=axis.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=axis.com header.i=@axis.com header.b="fWMfIa6c" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=aMEHsmH3fszw9ZpHwO9eux2X2vbzzyv/SaqvsekZFZmIodR/YZfpmzJLqtD69vAggCHPrXfoKkwHkkF5rTYzz7oBiVBEnCbaiLvASqypBv01gWyxI/STDSrxVVuDRwphhX830jqlM2YUCX0N8pjl3itdAKkYsOkDHrbO+5/EmSlOUJd3wTAtbIXEMVQ7AaG20Vyv0wF6ctrQdGo/pU4vTF91WadAtKCIMhqdnOhfOGedmRYikVVanomT/eJGhO6BB7KH3U6V4RL/A/UM57jgSL6TJVf07REehg+DnW6q4aXLIlYoL/+DG1jzziPiuOWSuO2to5qA7cdEu2W/+s0jUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=T5zUzb97NnKYgEgINsow+xvzBgT8hAFSvKm+i/vOCdo=; b=PObbbo5lr952cecjfiAHrEIh1hldaNdNxGjKJSJLfFSG3oKMb2+LWCCoojnvni0jLJ4Y+HQeoYPgHah4bjjktcKqZx6Cdq/CtcxQDwEve/trbGyJOfc5ZxqstdYNM0nZenIYbCWw2QGfJskdPuVpjZDUoY2yX7uii3JjXtVT5migKjkY6CYLsz1A+HUByxe5hSTieM3CybmhcjtNSZvTKxegrNUhiGfKvRTfBXKyLsuDB3qWJlMApWR73g1hZuVwvSGboa7FVOyf8nVGXfbhjhglwxW4p0Ob+I/mlmEMesG6wKO0QpwRmPxGpOiGOnsU6/Vp7Qq4kwV6Nw57wpQKmg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 195.60.68.100) smtp.rcpttodomain=kernel.org smtp.mailfrom=axis.com; dmarc=pass (p=none sp=none pct=100) action=none header.from=axis.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axis.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=T5zUzb97NnKYgEgINsow+xvzBgT8hAFSvKm+i/vOCdo=; b=fWMfIa6cIexjgvvRK4qrk+RhWicQYnAoHjLeMpvog0cTeZAhgMmryQh8GFl8eSwdMgahzk8lLqQo8dbaD5NCKI7dQj0qcBZob79QlFNzDSib3ORYNfyjDWLR88hT8gq6ygRePVx6fpJmz7PbtkiIw6dC4qEGTBrAheM9g7BtG4s= Received: from DB7PR02CA0020.eurprd02.prod.outlook.com (2603:10a6:10:52::33) by AM9PR02MB6948.eurprd02.prod.outlook.com (2603:10a6:20b:262::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8534.33; Wed, 19 Mar 2025 09:41:56 +0000 Received: from DU2PEPF00028D00.eurprd03.prod.outlook.com (2603:10a6:10:52:cafe::dc) by DB7PR02CA0020.outlook.office365.com (2603:10a6:10:52::33) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8534.34 via Frontend Transport; Wed, 19 Mar 2025 09:41:56 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 195.60.68.100) smtp.mailfrom=axis.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=axis.com; Received-SPF: Pass (protection.outlook.com: domain of axis.com designates 195.60.68.100 as permitted sender) receiver=protection.outlook.com; client-ip=195.60.68.100; helo=mail.axis.com; pr=C Received: from mail.axis.com (195.60.68.100) by DU2PEPF00028D00.mail.protection.outlook.com (10.167.242.184) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8534.20 via Frontend Transport; Wed, 19 Mar 2025 09:41:56 +0000 Received: from se-mail01w.axis.com (10.20.40.7) by se-mail01w.axis.com (10.20.40.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.44; Wed, 19 Mar 2025 10:41:54 +0100 Received: from se-intmail02x.se.axis.com (10.4.0.28) by se-mail01w.axis.com (10.20.40.7) with Microsoft SMTP Server id 15.1.2507.44 via Frontend Transport; Wed, 19 Mar 2025 10:41:54 +0100 Received: from pc4cv4203v8f.sh.cn.axis.com (pc4cv4203v8f.sh.cn.axis.com [192.168.77.92]) by se-intmail02x.se.axis.com (Postfix) with ESMTP id AD39E129; Wed, 19 Mar 2025 10:41:54 +0100 (CET) Received: by pc4cv4203v8f.sh.cn.axis.com (Postfix, from userid 12715) id 12983D6866; Wed, 19 Mar 2025 17:41:53 +0800 (CST) From: Jerry Lv To: =?utf-8?q?Pali_Roh=C3=A1r?= , Sebastian Reichel CC: , , , Subject: [PATCH v3] power: supply: bq27xxx: Retrieve again when busy Date: Wed, 19 Mar 2025 17:41:28 +0800 Message-ID: <20250319-foo-fix-v2-1-ff4cb232ef42@axis.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" X-Mailer: b4 0.14.1 Content-Transfer-Encoding: quoted-printable X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DU2PEPF00028D00:EE_|AM9PR02MB6948:EE_ X-MS-Office365-Filtering-Correlation-Id: 6bf10ee9-3039-4ad4-aa60-08dd66ca49cc X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|82310400026|36860700013|1800799024|13003099007; X-Microsoft-Antispam-Message-Info: =?utf-8?B?dHIrcmsrZHF5bnF2a2sycnQ5QTZveUR1QVRzbmQyV05D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8?B?Q3dWeHNiVDIxczJBRWVxSzhNTGFtKzNRTGhoQnRZcE9pTlZtYXREK2RSVGVP?= =?utf-8?B?UjhaVXE5NW43dEwzRW42MWo0V1owRlhlanA1K0hwZktKcDcyL2VjY0lIVkxD?= =?utf-8?B?U2UrWHliN1lHSUN0NFg0bTVLV0Fpa0pMWlFjQzdFU0JKNXk0U0VmVlhHYTRL?= =?utf-8?Q?YJnWOjAGlqztaBl6a141wKw4FW3Mv8=3D?= X-Forefront-Antispam-Report: CIP:195.60.68.100;CTRY:SE;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.axis.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(376014)(82310400026)(36860700013)(1800799024)(13003099007);DIR:OUT;SFP:1101; X-OriginatorOrg: axis.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Mar 2025 09:41:56.3430 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6bf10ee9-3039-4ad4-aa60-08dd66ca49cc X-MS-Exchange-CrossTenant-Id: 78703d3c-b907-432f-b066-88f7af9ca3af X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=78703d3c-b907-432f-b066-88f7af9ca3af;Ip=[195.60.68.100];Helo=[mail.axis.com] X-MS-Exchange-CrossTenant-AuthSource: DU2PEPF00028D00.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM9PR02MB6948 Multiple applications may access the battery gauge at the same time, so the gauge may be busy and EBUSY will be returned. The driver will set a flag to record the EBUSY state, and this flag will be kept until the next periodic update. When this flag is set, bq27xxx_battery_get_property() will just return ENODEV until the flag is updated. Even if the gauge was busy during the last accessing attempt, returning ENODEV is not ideal, and can cause confusion in the applications layer. Instead, retry accessing the I2C to update the flag is as expected, for the gauge typically recovers from busy state within a few milliseconds. If still failed to access the gauge, the real error code would be returned instead of ENODEV (as suggested by Pali Roh=C3=A1r). Signed-off-by: Jerry Lv Reviewed-by: Pali Roh=C3=A1r --- Changes in v3: - Move I2C retry logic to bq27xxx_battery_i2c_read() - Link to v2: https://lore.kernel.org/all/20241029-foo-fix-v1-1-1dbfed72d02= 3@axis.com Changes in v2: - Retry up to 3 times when gauge is busy - return the real error code when fail to access the device - Link to v1: https://lore.kernel.org/all/20240913-foo-fix2-v1-1-a0f499404f= 3a@axis.com --- drivers/power/supply/bq27xxx_battery.c | 2 +- drivers/power/supply/bq27xxx_battery_i2c.c | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/drivers/power/supply/bq27xxx_battery.c b/drivers/power/supply/= bq27xxx_battery.c index 750fda543308..99631ab46e8e 100644 --- a/drivers/power/supply/bq27xxx_battery.c +++ b/drivers/power/supply/bq27xxx_battery.c @@ -2030,7 +2030,7 @@ static int bq27xxx_battery_get_property(struct power_= supply *psy, mutex_unlock(&di->lock); =20 if (psp !=3D POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0) - return -ENODEV; + return di->cache.flags; =20 switch (psp) { case POWER_SUPPLY_PROP_STATUS: diff --git a/drivers/power/supply/bq27xxx_battery_i2c.c b/drivers/power/sup= ply/bq27xxx_battery_i2c.c index ba0d22d90429..78cccea4d419 100644 --- a/drivers/power/supply/bq27xxx_battery_i2c.c +++ b/drivers/power/supply/bq27xxx_battery_i2c.c @@ -6,6 +6,7 @@ * Andrew F. Davis */ =20 +#include #include #include #include @@ -27,10 +28,15 @@ static irqreturn_t bq27xxx_battery_irq_handler_thread(i= nt irq, void *data) static int bq27xxx_battery_i2c_read(struct bq27xxx_device_info *di, u8 reg, bool single) { +#define MAX_RETRY 3 +#define I2C_DELAY_MIN 10000 +#define I2C_DELAY_MAX 11000 + struct i2c_client *client =3D to_i2c_client(di->dev); struct i2c_msg msg[2]; u8 data[2]; int ret; + int retry =3D 0; =20 if (!client->adapter) return -ENODEV; @@ -47,7 +53,14 @@ static int bq27xxx_battery_i2c_read(struct bq27xxx_devic= e_info *di, u8 reg, else msg[1].len =3D 2; =20 - ret =3D i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)); + do { + ret =3D i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)); + if (ret =3D=3D -EBUSY && retry < MAX_RETRY) { + retry++; + /* sleep 10 miliseconds when busy */ + usleep_range(I2C_DELAY_MIN, I2C_DELAY_MAX); + } + } while (ret =3D=3D -EBUSY && retry < MAX_RETRY); if (ret < 0) return ret; =20 --- base-commit: 42f7652d3eb527d03665b09edac47f85fb600924 change-id: 20241008-foo-fix-b2244cbe6dce Best regards, --=20 Jerry Lv