linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Hans de Goede <hdegoede@redhat.com>
To: "Rafael J . Wysocki" <rjw@rjwysocki.net>,
	Mika Westerberg <mika.westerberg@linux.intel.com>,
	Mark Gross <markgross@kernel.org>,
	Andy Shevchenko <andy@infradead.org>,
	Wolfram Sang <wsa@the-dreams.de>,
	Sebastian Reichel <sre@kernel.org>,
	MyungJoo Ham <myungjoo.ham@samsung.com>,
	Chanwoo Choi <cw00.choi@samsung.com>,
	Ard Biesheuvel <ardb@kernel.org>
Cc: Hans de Goede <hdegoede@redhat.com>, Len Brown <lenb@kernel.org>,
	linux-acpi@vger.kernel.org, Yauhen Kharuzhy <jekhor@gmail.com>,
	Tsuchiya Yuto <kitakar@gmail.com>,
	platform-driver-x86@vger.kernel.org, linux-i2c@vger.kernel.org,
	linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-efi@vger.kernel.org
Subject: [PATCH v2 01/20] power: supply: core: Refactor power_supply_set_input_current_limit_from_supplier()
Date: Sun, 14 Nov 2021 18:03:16 +0100	[thread overview]
Message-ID: <20211114170335.66994-2-hdegoede@redhat.com> (raw)
In-Reply-To: <20211114170335.66994-1-hdegoede@redhat.com>

Some (USB) charger ICs have variants with USB D+ and D- pins to do their
own builtin charger-type detection, like e.g. the bq24190 and bq25890 and
also variants which lack this functionality, e.g. the bq24192 and bq25892.

In case the charger-type; and thus the input-current-limit detection is
done outside the charger IC then we need some way to communicate this to
the charger IC. In the past extcon was used for this, but if the external
detection does e.g. full USB PD negotiation then the extcon cable-types do
not convey enough information.

For these setups it was decided to model the external charging "brick"
and the parameters negotiated with it as a power_supply class-device
itself; and power_supply_set_input_current_limit_from_supplier() was
introduced to allow drivers to get the input-current-limit this way.

But in some cases psy drivers may want to know other properties, e.g. the
bq25892 can do "quick-charge" negotiation by pulsing its current draw,
but this should only be done if the usb_type psy-property of its supplier
is set to DCP (and device-properties indicate the board allows higher
voltages).

Instead of adding extra helper functions for each property which
a psy-driver wants to query from its supplier, refactor
power_supply_set_input_current_limit_from_supplier() into a
more generic power_supply_get_property_from_supplier() function.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/power/supply/bq24190_charger.c   | 10 ++++-
 drivers/power/supply/power_supply_core.c | 57 +++++++++++++-----------
 include/linux/power_supply.h             |  5 ++-
 3 files changed, 42 insertions(+), 30 deletions(-)

diff --git a/drivers/power/supply/bq24190_charger.c b/drivers/power/supply/bq24190_charger.c
index 35ff0c8fe96f..0c3d5caaef0c 100644
--- a/drivers/power/supply/bq24190_charger.c
+++ b/drivers/power/supply/bq24190_charger.c
@@ -1206,8 +1206,16 @@ static void bq24190_input_current_limit_work(struct work_struct *work)
 	struct bq24190_dev_info *bdi =
 		container_of(work, struct bq24190_dev_info,
 			     input_current_limit_work.work);
+	union power_supply_propval val;
+	int ret;
 
-	power_supply_set_input_current_limit_from_supplier(bdi->charger);
+	ret = power_supply_get_property_from_supplier(bdi->charger,
+						      POWER_SUPPLY_PROP_CURRENT_MAX,
+						      &val);
+	if (ret == 0)
+		bq24190_charger_set_property(bdi->charger,
+					     POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
+					     &val);
 }
 
 /* Sync the input-current-limit with our parent supply (if we have one) */
diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c
index fc12a4f407f4..fb956a9a1827 100644
--- a/drivers/power/supply/power_supply_core.c
+++ b/drivers/power/supply/power_supply_core.c
@@ -375,46 +375,49 @@ int power_supply_is_system_supplied(void)
 }
 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
 
-static int __power_supply_get_supplier_max_current(struct device *dev,
-						   void *data)
+struct psy_get_supplier_prop_data {
+	struct power_supply *psy;
+	enum power_supply_property psp;
+	union power_supply_propval *val;
+};
+
+static int __power_supply_get_supplier_property(struct device *dev, void *_data)
 {
-	union power_supply_propval ret = {0,};
 	struct power_supply *epsy = dev_get_drvdata(dev);
-	struct power_supply *psy = data;
+	struct psy_get_supplier_prop_data *data = _data;
 
-	if (__power_supply_is_supplied_by(epsy, psy))
-		if (!epsy->desc->get_property(epsy,
-					      POWER_SUPPLY_PROP_CURRENT_MAX,
-					      &ret))
-			return ret.intval;
+	if (__power_supply_is_supplied_by(epsy, data->psy))
+		if (!epsy->desc->get_property(epsy, data->psp, data->val))
+			return 1; /* Success */
 
-	return 0;
+	return 0; /* Continue iterating */
 }
 
-int power_supply_set_input_current_limit_from_supplier(struct power_supply *psy)
+int power_supply_get_property_from_supplier(struct power_supply *psy,
+					    enum power_supply_property psp,
+					    union power_supply_propval *val)
 {
-	union power_supply_propval val = {0,};
-	int curr;
-
-	if (!psy->desc->set_property)
-		return -EINVAL;
+	struct psy_get_supplier_prop_data data = {
+		.psy = psy,
+		.psp = psp,
+		.val = val,
+	};
+	int ret;
 
 	/*
 	 * This function is not intended for use with a supply with multiple
-	 * suppliers, we simply pick the first supply to report a non 0
-	 * max-current.
+	 * suppliers, we simply pick the first supply to report the psp.
 	 */
-	curr = class_for_each_device(power_supply_class, NULL, psy,
-				      __power_supply_get_supplier_max_current);
-	if (curr <= 0)
-		return (curr == 0) ? -ENODEV : curr;
-
-	val.intval = curr;
+	ret = class_for_each_device(power_supply_class, NULL, &data,
+				    __power_supply_get_supplier_property);
+	if (ret < 0)
+		return ret;
+	if (ret == 0)
+		return -ENODEV;
 
-	return psy->desc->set_property(psy,
-				POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val);
+	return 0;
 }
-EXPORT_SYMBOL_GPL(power_supply_set_input_current_limit_from_supplier);
+EXPORT_SYMBOL_GPL(power_supply_get_property_from_supplier);
 
 int power_supply_set_battery_charged(struct power_supply *psy)
 {
diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h
index 9ca1f120a211..0735b8963e0a 100644
--- a/include/linux/power_supply.h
+++ b/include/linux/power_supply.h
@@ -420,8 +420,9 @@ power_supply_temp2resist_simple(struct power_supply_resistance_temp_table *table
 				int table_len, int temp);
 extern void power_supply_changed(struct power_supply *psy);
 extern int power_supply_am_i_supplied(struct power_supply *psy);
-extern int power_supply_set_input_current_limit_from_supplier(
-					 struct power_supply *psy);
+int power_supply_get_property_from_supplier(struct power_supply *psy,
+					    enum power_supply_property psp,
+					    union power_supply_propval *val);
 extern int power_supply_set_battery_charged(struct power_supply *psy);
 
 #ifdef CONFIG_POWER_SUPPLY
-- 
2.31.1


  reply	other threads:[~2021-11-14 17:04 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-14 17:03 [PATCH v2 00/20] power-suppy/i2c/extcon: Fix charger setup on Xiaomi Mi Pad 2 and Lenovo Yogabook Hans de Goede
2021-11-14 17:03 ` Hans de Goede [this message]
2021-11-14 17:03 ` [PATCH v2 02/20] power: supply: bq25890: Fix ADC continuous conversion setting when charging Hans de Goede
2021-11-14 17:03 ` [PATCH v2 03/20] power: supply: bq25890: Rename IILIM field to IINLIM Hans de Goede
2021-11-14 17:03 ` [PATCH v2 04/20] power: supply: bq25890: Reduce reported CONSTANT_CHARGE_CURRENT_MAX for low temperatures Hans de Goede
2021-11-16  9:43   ` Hans de Goede
2021-11-14 17:03 ` [PATCH v2 05/20] power: supply: bq25890: Add a bq25890_rw_init_data() helper Hans de Goede
2021-11-14 17:03 ` [PATCH v2 06/20] power: supply: bq25890: Add support to skip reset at probe() / remove() Hans de Goede
2021-11-14 17:03 ` [PATCH v2 07/20] power: supply: bq25890: Add support to read back the settings from the chip Hans de Goede
2021-11-14 17:03 ` [PATCH v2 08/20] power: supply: bq25890: Enable charging on boards where we skip reset Hans de Goede
2021-11-14 17:03 ` [PATCH v2 09/20] power: supply: bq25890: Drop dev->platform_data == NULL check Hans de Goede
2021-11-16 11:02   ` Andy Shevchenko
2021-11-14 17:03 ` [PATCH v2 10/20] power: supply: bq25890: Add bq25890_set_otg_cfg() helper Hans de Goede
2021-11-15 10:11   ` Yauhen Kharuzhy
2021-11-16  9:33     ` Hans de Goede
     [not found]       ` <CAKWEGV7WVsZK=890UG=t3dhqCuoD-6N44DPMzk-_8TSPBm4_Dg@mail.gmail.com>
2021-11-28 15:02         ` Hans de Goede
2021-11-28 19:46           ` Yauhen Kharuzhy
2021-11-14 17:03 ` [PATCH v2 11/20] power: supply: bq25890: Add support for registering the Vbus boost converter as a regulator Hans de Goede
2021-11-16 11:05   ` Andy Shevchenko
2021-11-14 17:03 ` [PATCH v2 12/20] power: supply: bq25890: On the bq25892 set the IINLIM based on external charger detection Hans de Goede
2021-11-14 17:03 ` [PATCH v2 13/20] power: supply: bq25890: Support higher charging voltages through Pump Express+ protocol Hans de Goede
2021-11-16 11:14   ` Andy Shevchenko
2021-11-16 11:40     ` Hans de Goede
2021-11-14 17:03 ` [PATCH v2 14/20] mfd: intel_soc_pmic_chtwc: Add intel_cht_wc_get_model() helper function Hans de Goede
2021-11-16 11:18   ` Andy Shevchenko
2021-11-16 11:43     ` Hans de Goede
2021-11-28  9:53     ` Hans de Goede
2021-11-14 17:03 ` [PATCH v2 15/20] i2c: cht-wc: Make charger i2c-client instantiation board/device-model specific Hans de Goede
2021-11-16 11:20   ` Andy Shevchenko
2021-11-16 11:44     ` Hans de Goede
2021-11-23  8:24   ` Wolfram Sang
2021-11-28 14:04     ` Hans de Goede
2021-11-14 17:03 ` [PATCH v2 16/20] extcon: intel-cht-wc: Use new intel_cht_wc_get_model() helper Hans de Goede
2021-11-17  6:47   ` Chanwoo Choi
2021-11-17 22:28     ` Hans de Goede
2021-11-14 17:03 ` [PATCH v2 17/20] extcon: intel-cht-wc: Support devs with Micro-B / USB-2 only Type-C connectors Hans de Goede
2021-11-16 11:28   ` Andy Shevchenko
2021-11-16 11:31     ` Andy Shevchenko
2021-11-16 11:32       ` Andy Shevchenko
2021-11-16 11:51     ` Hans de Goede
2021-11-14 17:03 ` [PATCH v2 18/20] extcon: intel-cht-wc: Refactor cht_wc_extcon_get_charger() Hans de Goede
2021-11-17  7:15   ` Chanwoo Choi
2021-11-17 22:30     ` Hans de Goede
2021-11-19 15:44       ` Chanwoo Choi
2021-11-28 14:17         ` Hans de Goede
2021-11-14 17:03 ` [PATCH v2 19/20] extcon: intel-cht-wc: Add support for registering a power_supply class-device Hans de Goede
2021-11-16 15:19   ` Andy Shevchenko
2021-11-14 17:03 ` [PATCH v2 20/20] extcon: intel-cht-wc: Report RID_A for ACA adapters Hans de Goede
2021-11-16 11:00 ` [PATCH v2 00/20] power-suppy/i2c/extcon: Fix charger setup on Xiaomi Mi Pad 2 and Lenovo Yogabook Andy Shevchenko
2021-11-16 11:05   ` Hans de Goede
2021-11-17 18:56 ` Sebastian Reichel

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=20211114170335.66994-2-hdegoede@redhat.com \
    --to=hdegoede@redhat.com \
    --cc=andy@infradead.org \
    --cc=ardb@kernel.org \
    --cc=cw00.choi@samsung.com \
    --cc=jekhor@gmail.com \
    --cc=kitakar@gmail.com \
    --cc=lenb@kernel.org \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-efi@vger.kernel.org \
    --cc=linux-i2c@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=markgross@kernel.org \
    --cc=mika.westerberg@linux.intel.com \
    --cc=myungjoo.ham@samsung.com \
    --cc=platform-driver-x86@vger.kernel.org \
    --cc=rjw@rjwysocki.net \
    --cc=sre@kernel.org \
    --cc=wsa@the-dreams.de \
    /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).