linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Mario Limonciello <mario.limonciello@amd.com>
To: <heikki.krogerus@linux.intel.com>, <rafael@kernel.org>,
	<ajayg@nvidia.com>, <andriy.shevchenko@linux.intel.com>
Cc: <linux-i2c@vger.kernel.org>, <linux-pm@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>, <linux-usb@vger.kernel.org>,
	<Evan.Quan@amd.com>, <Lijo.Lazar@amd.com>,
	<Sanket.Goswami@amd.com>,
	"Mario Limonciello" <mario.limonciello@amd.com>
Subject: [PATCH 1/2] power: supply: Use the scope of power supplies to tell if power is system supplied
Date: Tue, 16 May 2023 13:25:40 -0500	[thread overview]
Message-ID: <20230516182541.5836-2-mario.limonciello@amd.com> (raw)
In-Reply-To: <20230516182541.5836-1-mario.limonciello@amd.com>

The logic used for power_supply_is_system_supplied() counts all power
supplies and:
* If no power supplies found assumes AC
* If non-battery power supplies found uses online to determine AC/DC.
  - If any are onlined, assumes AC
  - Othewise DC.

This logic makes sense for desktop systems that don't export an ACPI
battery, but it fails once you include a dGPU that provides a UCSI
power supply on a desktop system without any other power supplies.

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 <Evan.Quan@amd.com>
Suggested-by: Lijo Lazar <Lijo.Lazar@amd.com>
Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
---
 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 ab986dbace16..d57f420ba8c3 100644
--- a/drivers/power/supply/power_supply_core.c
+++ b/drivers/power/supply/power_supply_core.c
@@ -348,6 +348,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,
@@ -366,8 +370,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.34.1


  reply	other threads:[~2023-05-16 18:49 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-16 18:25 [PATCH 0/2] Adjust logic for power_supply_is_system_supplied() Mario Limonciello
2023-05-16 18:25 ` Mario Limonciello [this message]
2023-05-16 20:41   ` [PATCH 1/2] power: supply: Use the scope of power supplies to tell if power is system supplied Sebastian Reichel
2023-05-16 20:44     ` Limonciello, Mario
2023-05-16 21:54       ` Sebastian Reichel
2023-05-16 18:25 ` [PATCH 2/2] usb: typec: ucsi: Don't create power supplies for dGPUs Mario Limonciello
2023-05-17 17:14   ` Andy Shevchenko
2023-05-17 18:58     ` Greg KH
2023-05-17 19:14       ` Andy Shevchenko
2023-05-18 15:31         ` Limonciello, Mario
2023-05-17  1:22 ` [PATCH 0/2] Adjust logic for power_supply_is_system_supplied() Quan, Evan
2023-05-17 17:15   ` andriy.shevchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230516182541.5836-2-mario.limonciello@amd.com \
    --to=mario.limonciello@amd.com \
    --cc=Evan.Quan@amd.com \
    --cc=Lijo.Lazar@amd.com \
    --cc=Sanket.Goswami@amd.com \
    --cc=ajayg@nvidia.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=heikki.krogerus@linux.intel.com \
    --cc=linux-i2c@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=rafael@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).