[PATCH v2] mfd: cros_ec_dev: Only register PCHG device if present

From: Stephen Boyd
Date: Mon Apr 18 2022 - 20:05:34 EST


Don't create a device for the peripheral charger (PCHG) if there aren't
any peripheral charger ports. This removes a device on most ChromeOS
systems, because the peripheral charger functionality isn't always
present.

Cc: Prashant Malani <pmalani@xxxxxxxxxxxx>
Cc: Tzung-Bi Shih <tzungbi@xxxxxxxxxx>
Cc: Daisuke Nojiri <dnojiri@xxxxxxxxxxxx>
Cc: Benson Leung <bleung@xxxxxxxxxxxx>
Cc: Guenter Roeck <groeck@xxxxxxxxxxxx>
Cc: <chrome-platform@xxxxxxxxxxxxxxx>
Signed-off-by: Stephen Boyd <swboyd@xxxxxxxxxxxx>
---

Changes from v1 (https://lore.kernel.org/r/20220415003253.1973106-1-swboyd@xxxxxxxxxxxx):
* Use cros_ec_command()
* Drop the other patches that aren't needed anymore

drivers/mfd/cros_ec_dev.c | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)

diff --git a/drivers/mfd/cros_ec_dev.c b/drivers/mfd/cros_ec_dev.c
index 546feef851ab..596731caf407 100644
--- a/drivers/mfd/cros_ec_dev.c
+++ b/drivers/mfd/cros_ec_dev.c
@@ -114,6 +114,9 @@ static const struct mfd_cell cros_ec_platform_cells[] = {
{ .name = "cros-ec-chardev", },
{ .name = "cros-ec-debugfs", },
{ .name = "cros-ec-sysfs", },
+};
+
+static const struct mfd_cell cros_ec_pchg_cells[] = {
{ .name = "cros-ec-pchg", },
};

@@ -137,6 +140,7 @@ static int ec_device_probe(struct platform_device *pdev)
struct device *dev = &pdev->dev;
struct cros_ec_platform *ec_platform = dev_get_platdata(dev);
struct cros_ec_dev *ec = kzalloc(sizeof(*ec), GFP_KERNEL);
+ struct ec_response_pchg_count pchg_count;
int i;

if (!ec)
@@ -242,6 +246,21 @@ static int ec_device_probe(struct platform_device *pdev)
}
}

+ /*
+ * The PCHG device cannot be detected by sending EC_FEATURE_GET_CMD, but
+ * it can be detected by querying the number of peripheral chargers.
+ */
+ retval = cros_ec_command(ec->ec_dev, 0, EC_CMD_PCHG_COUNT, NULL, 0,
+ &pchg_count, sizeof(pchg_count));
+ if (retval >= 0 && pchg_count.port_count) {
+ retval = mfd_add_hotplug_devices(ec->dev,
+ cros_ec_pchg_cells,
+ ARRAY_SIZE(cros_ec_pchg_cells));
+ if (retval)
+ dev_warn(ec->dev, "failed to add pchg: %d\n",
+ retval);
+ }
+
/*
* The following subdevices cannot be detected by sending the
* EC_FEATURE_GET_CMD to the Embedded Controller device.

base-commit: 3123109284176b1532874591f7c81f3837bbdc17
--
https://chromeos.dev