From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 47697C7EE2E for ; Wed, 31 May 2023 14:05:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237361AbjEaOFJ (ORCPT ); Wed, 31 May 2023 10:05:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47628 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237561AbjEaOEw (ORCPT ); Wed, 31 May 2023 10:04:52 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2874E1734; Wed, 31 May 2023 06:59:03 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 96B6862C18; Wed, 31 May 2023 13:46:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 09340C433D2; Wed, 31 May 2023 13:46:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685540776; bh=IMSxW+BwXyy3mU+PMSOaQL+UkXZIvUYyfiSk3WvHIoA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=brq9J2sc/aaVIyFiprFg8FeL+2ZAXsvc7OQytQZ1U7/N5xSQ6rsXuPMtXFUWj6srP IwDZeSSEdk+ucro1dBSn2kOmH/W9lM7XCvDrym7fOK//JfYvJQ+8y4ejOsLsw/9uFr LCs0bNTurXBgYJpDGm0yCsMTjDZUX7Cvz5Q2LLsYBdMjD5BkEKZEEBzIIWSlroLYIw 145bBORk3LHevaBi2+SVOrEl6FDbQcm74TveR321qvuJYYTlA/QHr1KV0jfZNJRDwR uPIHzeLVgfAviwGh4f5CJHmpEe8qNBoPtSle2SerUSv4YsGEo+oU+zXnGq3OxGWtoy ZNwghDcUGy2OQ== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Mario Limonciello , Evan Quan , Lijo Lazar , Sebastian Reichel , Sasha Levin , sre@kernel.org, linux-pm@vger.kernel.org Subject: [PATCH AUTOSEL 4.14 06/10] power: supply: Fix logic checking if system is running from battery Date: Wed, 31 May 2023 09:46:02 -0400 Message-Id: <20230531134606.3385210-6-sashal@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230531134606.3385210-1-sashal@kernel.org> References: <20230531134606.3385210-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Mario Limonciello [ Upstream commit 95339f40a8b652b5b1773def31e63fc53c26378a ] The logic used for power_supply_is_system_supplied() counts all power supplies and assumes that the system is running from AC if there is either a non-battery power-supply reporting to be online or if no power-supplies exist at all. The second rule is for desktop systems, that don't have any battery/charger devices. These systems will incorrectly report to be powered from battery once a device scope power-supply is registered (e.g. a HID device), since these power-supplies increase the counter. Apart from HID devices, recent dGPUs provide UCSI power supplies on a desktop systems. The dGPU by default doesn't have anything plugged in so it's 'offline'. This makes power_supply_is_system_supplied() return 0 with a count of 1 meaning all drivers that use this get a wrong judgement. To fix this case adjust the logic to also examine the scope of the power supply. If the power supply is deemed a device power supply, then don't count it. Cc: Evan Quan Suggested-by: Lijo Lazar Signed-off-by: Mario Limonciello Signed-off-by: Sebastian Reichel Signed-off-by: Sasha Levin --- drivers/power/supply/power_supply_core.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c index 409ecff1a51a7..67766b500325f 100644 --- a/drivers/power/supply/power_supply_core.c +++ b/drivers/power/supply/power_supply_core.c @@ -349,6 +349,10 @@ static int __power_supply_is_system_supplied(struct device *dev, void *data) struct power_supply *psy = dev_get_drvdata(dev); unsigned int *count = data; + if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_SCOPE, &ret)) + if (ret.intval == POWER_SUPPLY_SCOPE_DEVICE) + return 0; + (*count)++; if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY) if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE, @@ -367,8 +371,8 @@ int power_supply_is_system_supplied(void) __power_supply_is_system_supplied); /* - * If no power class device was found at all, most probably we are - * running on a desktop system, so assume we are on mains power. + * If no system scope power class device was found at all, most probably we + * are running on a desktop system, so assume we are on mains power. */ if (count == 0) return 1; -- 2.39.2