Skip to contentSkip to conversation

platform/chrome: Introduce DT hardware prober

PATCH Superseded Chen-Yu Tsai 4 participants 36 messages Aug 22, 2024 – Aug 26, 2024

Subsystems (10)

v5 of 11 Superseded Latest is v11 →

lore
Share & tools
lore

Chen-Yu Tsai

author maintainer
#0 lore raw

v5 cover platform/chrome: Introduce DT hardware prober

Hi everyone,

This is v5 of my "of: Introduce hardware prober driver" [1] series. v5 mainly addresses comments from Andy.

v2 continued Doug's "of: device: Support 2nd sources of probeable but undiscoverable devices" [2] series, but follows the scheme suggested by Rob, marking all second source component device nodes as "fail-needs-probe", and having a hardware prober driver enable the one of them.

Changes since v4:
- Link to v4:
https://lore.kernel.org/all/[email protected]/
- Patch 1 "of: dynamic: Add of_changeset_update_prop_string"
- Use modern designated initializer for |prop|
- Patch 2 "regulator: Move OF-specific regulator lookup code to of_regulator.c"
- New patch only moving code
- Patch 3 "regulator: Split up _regulator_get()"
- New patch splitting existing code into smaller functions
- Patch 4 "regulator: Do pure DT regulator lookup in of_regulator_bulk_get_all()"
- Was "regulator: Add regulator_of_get_optional() for pure DT" in v4
- Add OF-specific _of_regulator_get() function
- Rename regulator_of_get_optional() to of_regulator_get_optional() for consistency
- Make of_regulator_get_optional() static, as it is only used internally
- Convert of_regulator_bulk_get_all()
- Patch 5 "gpiolib: Add gpio_property_name_length()"
- New patch, split out from patch 7/8
- Patch 6 "i2c: Introduce OF component probe function"
- Split code into helper functions
- Use scoped helpers and __free() to reduce error path
- Patch 7 "i2c: of-prober: Add regulator support"
- Split out GPIO handling as patch 8
- Rewrote using of_regulator_bulk_get_all()
- Replaced "regulators" with "regulator supplies" in debug messages
- Patch 8 "i2c: of-prober: Add GPIO support"
- New patch split out from patch 7
- Moved GPIO property name check to common function in gpiolib.c in new patch
- Moved i2c_of_probe_free_gpios() into for_each_child_of_node_scoped()
- Rewrote in gpiod_*_array-esque fashion
- Patch 9 "platform/chrome: Introduce device tree hardware prober"
- Fix Kconfig dependency
- Update copyright year
- Drop "linux/of.h" header
- Include "linux/errno.h"
- Move |int ret| declaration to top of block
- Return -ENODEV on no match instead of 0
- Unregister platform driver and device unconditionally after previous change
- Patch 10 "arm64: dts: mediatek: mt8173-elm-hana: Mark touchscreens and trackpads as fail"
- Rebased

Changes since v3:
- Patch 1 "of: dynamic: Add of_changeset_update_prop_string"
- Use new __of_prop_free() helper (helper was added by Rob in 2024/04)
- Add new line before header declaration
- Patch 2 "regulator: Add regulator_of_get_optional() for pure DT"
- New patch
- Patch 3 "i2c: Introduce OF component probe function"
- Complete kernel-doc
- Return different error if I2C controller is disabled
- Expand comment to explain assumptions and constraints
- Split for-loop finding target node and operations on target node
- Add missing i2c_put_adapter()
- Move prober code to separate file
- Patch 4 "i2c: of-prober: Add GPIO and regulator support"
- New patch
- Patch 5 "platform/chrome: Introduce device tree hardware prober"
- Include linux/init.h
- Rewrite for loop in driver probe function as suggested by Andy
- Make prober driver buildable as module
- Ignore prober errors other than probe deferral
- Patch 6 "arm64: dts: mediatek: mt8173-elm-hana: Mark touchscreens and trackpads as fail"
- Rebased

Patch "arm64: dts: mediatek: mt8173-elm-hana: Add G2touch G7500 touchscreen" was merged separately and thus removed from this series.

Changes since v2:
- Added of_changeset_update_prop_string()
- Moved generic I2C code to the I2C core
- Moved remaining platform specific code to platform/chrome/
- Switched to of_node_is_available() to check if node is enabled.
- Switched to OF changeset API to update status property
- I2C probe helper function now accepts "struct device *dev" instead to reduce line length and dereferences
- Moved "ret = 0" to just before for_each_child_of_node(i2c_node, node)
- Depend on rather than select CONFIG_I2C
- Copied machine check to driver init function
- Explicitly mentioned "device tree" or OF in driver name, description and Kconfig symbol
- Dropped filename from inside the file
- Made loop variable size_t (instead of unsigned int as Andy asked)
- Switched to PLATFORM_DEVID_NONE instead of raw -1
- Switched to standard goto error path pattern in hw_prober_driver_init()
- Dropped device class from status property

Patches removed from v3 and saved for later:
- of: base: Add of_device_is_fail
- of: hw_prober: Support Chromebook SKU ID based component selection
- dt-bindings: arm: mediatek: Remove SKU specific compatibles for Google Krane
- arm64: dts: mediatek: mt8183-kukui: Merge Krane device trees

For the I2C component (touchscreens and trackpads) case from the original series, the hardware prober driver finds the particular class of device in the device tree, gets its parent I2C adapter, and tries to initiate a simple I2C read for each device under that I2C bus. When it finds one that responds, it considers that one present, marks it as "okay", and returns, letting the driver core actually probe the device.

This works fine in most cases since these components are connected via a ribbon cable and always have the same resources. The prober will also grab these resources and enable them.

The other case, selecting a display panel to use based on the SKU ID from the firmware, hit a bit of an issue with fixing the OF graph. It has been left out since v3.

Patch 1 adds of_changeset_update_prop_string(), as requested by Rob.

Patches 2 through 4 reorganize the OF-specific regulator core code and reworks the existing of_regulator_bulk_get_all() function to look up regulator supplies solely using device tree nodes.

Patch 5 adds a function to the GPIO library that checks whether a given string (property name) matches the GPIO property pattern, and if it does, returns the length of the GPIO name.

Patch 6 implements probing the I2C bus for presence of components as a helper function in the I2C core.

Patch 7 implements regulator supply support for the I2C component prober.

Patch 8 implements GPIO support for the I2C component prober.

Patch 9 adds a ChromeOS specific DT hardware prober. This initial version targets the Hana Chromebooks, probing its I2C trackpads and touchscreens.

Patch 10 modifies the Hana device tree and marks the touchscreens and trackpads as "fail-needs-probe", ready for the driver to probe.

The patch and build time dependencies for this series is now quite complicated:

of_regulator.c cleanups [1] -> regulator patches here ----

							   |
							   v
  gpio patch in -next [2] -> gpiolib patch here  -----> i2c of-prober --
								       |
                      platform/chrome device tree hardware prober <----- 

The regulator patches in this series depend on other cleanup patches [1] I sent earlier. The gpiolib patch depends on a commit in -next [2] changing the GPIO suffixes array. Patches 6 through 8 introducting i2c of-prober depend on the first 5 patches. Patch 9, The chrome prober, depends on patches 6 through 8.

I think it might be easier if the respective maintainers take the first five patches for -rc1, and patches 6 through 9 go through either the i2c or chrome trees either very late in the merge window or right after it. Patch 10 can go in only after everything else is in. This should be better than having an immutable branch on top of some commit in -next for three other trees to consume.

Thanks
ChenYu

[1] https://lore.kernel.org/all/[email protected]/
[2] commit 4b91188dced8 ("gpiolib: Replace gpio_suffix_count with NULL-terminated array")

Chen-Yu Tsai (10):
of: dynamic: Add of_changeset_update_prop_string
regulator: Move OF-specific regulator lookup code to of_regulator.c
regulator: Split up _regulator_get()
regulator: Do pure DT regulator lookup in of_regulator_bulk_get_all()
gpiolib: Add gpio_property_name_length()
i2c: Introduce OF component probe function
i2c: of-prober: Add regulator support
i2c: of-prober: Add GPIO support
platform/chrome: Introduce device tree hardware prober
arm64: dts: mediatek: mt8173-elm-hana: Mark touchscreens and trackpads
as fail

 .../boot/dts/mediatek/mt8173-elm-hana.dtsi    |  13 +
 arch/arm64/boot/dts/mediatek/mt8173-elm.dtsi  |   4 +-
 drivers/gpio/gpiolib.c                        |  44 ++
 drivers/i2c/Makefile                          |   1 +
 drivers/i2c/i2c-core-of-prober.c              | 391 ++++++++++++++++++
 drivers/of/dynamic.c                          |  44 ++
 drivers/platform/chrome/Kconfig               |  11 +
 drivers/platform/chrome/Makefile              |   1 +
 .../platform/chrome/chromeos_of_hw_prober.c   | 104 +++++
 drivers/regulator/core.c                      | 139 +++----
 drivers/regulator/internal.h                  |  15 +-
 drivers/regulator/of_regulator.c              | 142 ++++++-
 include/linux/gpio/consumer.h                 |   2 +
 include/linux/i2c.h                           |   4 +
 include/linux/of.h                            |   4 +
 15 files changed, 820 insertions(+), 99 deletions(-)
 create mode 100644 drivers/i2c/i2c-core-of-prober.c
 create mode 100644 drivers/platform/chrome/chromeos_of_hw_prober.c
Signature · 1 line
2.46.0.184.g6999bdac58-goog
author maintainer
#1 lore raw

PATCH v5 1/10 of: dynamic: Add of_changeset_update_prop_string

Add a helper function to add string property updates to an OF changeset. This is similar to of_changeset_add_prop_string(), but instead of adding the property (and failing if it exists), it will update the property.

This shall be used later in the DT hardware prober.

---
Changes since v4:
- Use modern designated initializer for |prop|

Changes since v3:
- Use new __of_prop_free() helper
- Add new line before header declaration

Changes since v2:
- New patch added in v3

---
 drivers/of/dynamic.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 include/linux/of.h   |  4 ++++
 2 files changed, 48 insertions(+)
drivers/of/dynamic.c +44 -0
index 110104a936d9..daa69d160a78 100644
@@ -1072,3 +1072,47 @@ int of_changeset_add_prop_bool(struct of_changeset *ocs, struct device_node *np,
 	return of_changeset_add_prop_helper(ocs, np, &prop);
 }
 EXPORT_SYMBOL_GPL(of_changeset_add_prop_bool);
+
+static int of_changeset_update_prop_helper(struct of_changeset *ocs,
+					   struct device_node *np,
+					   const struct property *pp)
+{
+	struct property *new_pp;
+	int ret;
+
+	new_pp = __of_prop_dup(pp, GFP_KERNEL);
+	if (!new_pp)
+		return -ENOMEM;
+
+	ret = of_changeset_update_property(ocs, np, new_pp);
+	if (ret)
+		__of_prop_free(new_pp);
+
+	return ret;
+}
+
+/**
+ * of_changeset_update_prop_string - Add a string property update to a changeset
+ *
+ * @ocs:	changeset pointer
+ * @np:		device node pointer
+ * @prop_name:	name of the property to be updated
+ * @str:	pointer to null terminated string
+ *
+ * Create a string property to be updated and add it to a changeset.
+ *
+ * Return: 0 on success, a negative error value in case of an error.
+ */
+int of_changeset_update_prop_string(struct of_changeset *ocs,
+				    struct device_node *np,
+				    const char *prop_name, const char *str)
+{
+	struct property prop = {
+		.name = (char *)prop_name,
+		.length = strlen(str) + 1,
+		.value = (void *)str,
+	};
+
+	return of_changeset_update_prop_helper(ocs, np, &prop);
+}
+EXPORT_SYMBOL_GPL(of_changeset_update_prop_string);
include/linux/of.h +4 -0
index 85b60ac9eec5..046283be1cd3 100644
@@ -1651,6 +1651,10 @@ static inline int of_changeset_add_prop_u32(struct of_changeset *ocs,
 	return of_changeset_add_prop_u32_array(ocs, np, prop_name, &val, 1);
 }
 
+int of_changeset_update_prop_string(struct of_changeset *ocs,
+				    struct device_node *np,
+				    const char *prop_name, const char *str);
+
 int of_changeset_add_prop_bool(struct of_changeset *ocs, struct device_node *np,
 			       const char *prop_name);
 
Signature · 1 line
2.46.0.184.g6999bdac58-goog
maintainer Reviewed-by on v5 1/10
#2 lore raw

Re: [PATCH v5 01/10] of: dynamic: Add of_changeset_update_prop_string

Quoted 16 lines · Chen-Yu Tsai

Add a helper function to add string property updates to an OF changeset. This is similar to of_changeset_add_prop_string(), but instead of adding the property (and failing if it exists), it will update the property.

This shall be used later in the DT hardware prober.

---
Changes since v4:
- Use modern designated initializer for |prop|

Changes since v3:
- Use new __of_prop_free() helper
- Add new line before header declaration

Quoted 6 lines

Changes since v2:
- New patch added in v3

---
 drivers/of/dynamic.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 include/linux/of.h   |  4 ++++
 2 files changed, 48 insertions(+)
author maintainer
#3 lore raw

PATCH v5 2/10 regulator: Move OF-specific regulator lookup code to of_regulator.c

In reply to Chen-Yu Tsai

There's still a bit of OF-specific code in the regulator device lookup function.

Move those bits of code over to of_regulator.c, and create a new function of_regulator_dev_lookup() to encapsulate the code moved out of regulator_dev_lookup().

Also mark of_find_regulator_by_node() as static, since there are no other users in other compile units.

There are no functional changes.

---
Changes since v4:
- New patch

---
 drivers/regulator/core.c         |  85 ++------------------------
 drivers/regulator/internal.h     |   9 +--
 drivers/regulator/of_regulator.c | 102 ++++++++++++++++++++++++++++++-
 3 files changed, 111 insertions(+), 85 deletions(-)
drivers/regulator/core.c +5 -80
index 9029de5395ee..361309fcae57 100644
@@ -419,72 +419,6 @@ static void regulator_lock_dependent(struct regulator_dev *rdev,
 	mutex_unlock(&regulator_list_mutex);
 }
 
-/**
- * of_get_child_regulator - get a child regulator device node
- * based on supply name
- * @parent: Parent device node
- * @prop_name: Combination regulator supply name and "-supply"
- *
- * Traverse all child nodes.
- * Extract the child regulator device node corresponding to the supply name.
- * returns the device node corresponding to the regulator if found, else
- * returns NULL.
- */
-static struct device_node *of_get_child_regulator(struct device_node *parent,
-						  const char *prop_name)
-{
-	struct device_node *regnode = NULL;
-	struct device_node *child = NULL;
-
-	for_each_child_of_node(parent, child) {
-		regnode = of_parse_phandle(child, prop_name, 0);
-
-		if (!regnode) {
-			regnode = of_get_child_regulator(child, prop_name);
-			if (regnode)
-				goto err_node_put;
-		} else {
-			goto err_node_put;
-		}
-	}
-	return NULL;
-
-err_node_put:
-	of_node_put(child);
-	return regnode;
-}
-
-/**
- * of_get_regulator - get a regulator device node based on supply name
- * @dev: Device pointer for the consumer (of regulator) device
- * @supply: regulator supply name
- *
- * Extract the regulator device node corresponding to the supply name.
- * returns the device node corresponding to the regulator if found, else
- * returns NULL.
- */
-static struct device_node *of_get_regulator(struct device *dev, const char *supply)
-{
-	struct device_node *regnode = NULL;
-	char prop_name[64]; /* 64 is max size of property name */
-
-	dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
-
-	snprintf(prop_name, 64, "%s-supply", supply);
-	regnode = of_parse_phandle(dev->of_node, prop_name, 0);
-
-	if (!regnode) {
-		regnode = of_get_child_regulator(dev->of_node, prop_name);
-		if (regnode)
-			return regnode;
-
-		dev_dbg(dev, "Looking up %s property in node %pOF failed\n",
-				prop_name, dev->of_node);
-		return NULL;
-	}
-	return regnode;
-}
-
 /* Platform voltage constraint check */
 int regulator_check_voltage(struct regulator_dev *rdev,
 			    int *min_uV, int *max_uV)
@@ -2009,7 +1943,6 @@ static struct regulator_dev *regulator_dev_lookup(struct device *dev,
 						  const char *supply)
 {
 	struct regulator_dev *r = NULL;
-	struct device_node *node;
 	struct regulator_map *map;
 	const char *devname = NULL;
 
@@ -2017,19 +1950,11 @@ static struct regulator_dev *regulator_dev_lookup(struct device *dev,
 
 	/* first do a dt based lookup */
 	if (dev && dev->of_node) {
-		node = of_get_regulator(dev, supply);
-		if (node) {
-			r = of_find_regulator_by_node(node);
-			of_node_put(node);
-			if (r)
-				return r;
-
-			/*
-			 * We have a node, but there is no device.
-			 * assume it has not registered yet.
-			 */
-			return ERR_PTR(-EPROBE_DEFER);
-		}
+		r = of_regulator_dev_lookup(dev, supply);
+		if (!IS_ERR(r))
+			return r;
+		if (PTR_ERR(r) == -EPROBE_DEFER)
+			return r;
 	}
 
 	/* if not found, try doing it non-dt way */
drivers/regulator/internal.h +5 -4
index 77a502141089..ffeaef22169e 100644
@@ -66,7 +66,8 @@ static inline struct regulator_dev *dev_to_rdev(struct device *dev)
 }
 
 #ifdef CONFIG_OF
-struct regulator_dev *of_find_regulator_by_node(struct device_node *np);
+struct regulator_dev *of_regulator_dev_lookup(struct device *dev,
+					      const char *supply);
 struct regulator_init_data *regulator_of_get_init_data(struct device *dev,
 			         const struct regulator_desc *desc,
 				 struct regulator_config *config,
@@ -80,10 +81,10 @@ int of_get_n_coupled(struct regulator_dev *rdev);
 bool of_check_coupling_data(struct regulator_dev *rdev);
 
 #else
-static inline struct regulator_dev *
-of_find_regulator_by_node(struct device_node *np)
+static inline struct regulator_dev *of_regulator_dev_lookup(struct device *dev,
+							    const char *supply)
 {
-	return NULL;
+	return -ENODEV;
 }
 
 static inline struct regulator_init_data *
drivers/regulator/of_regulator.c +101 -1

114 lines not on this page · raw message

Signature · 1 line
2.46.0.184.g6999bdac58-goog
#4 lore raw

Re: [PATCH v5 02/10] regulator: Move OF-specific regulator lookup code to of_regulator.c

Quoted 11 lines · Chen-Yu Tsai

There's still a bit of OF-specific code in the regulator device lookup function.

Move those bits of code over to of_regulator.c, and create a new function of_regulator_dev_lookup() to encapsulate the code moved out of regulator_dev_lookup().

Also mark of_find_regulator_by_node() as static, since there are no other users in other compile units.

There are no functional changes.

...

Quoted 10 lines

+/**
+ * of_get_child_regulator - get a child regulator device node
+ * based on supply name
+ * @parent: Parent device node
+ * @prop_name: Combination regulator supply name and "-supply"
+ *
+ * Traverse all child nodes.
+ * Extract the child regulator device node corresponding to the supply name.
+ * returns the device node corresponding to the regulator if found, else
+ * returns NULL.

At the same time you may fix kernel-doc warnings (no "Return" section) in these three (on your wish you may fix others in a separate change, but it's not related to this series).

Quoted 1 line

+ */

...

Quoted 1 line

+/** of_regulator_dev_lookup - lookup a regulator device with device tree only

Something went wrong with the indentation.

Quoted 10 lines

+ * @dev: Device pointer for regulator supply lookup.
+ * @supply: Supply name or regulator ID.
+ *
+ * If successful, returns a struct regulator_dev that corresponds to the name
+ * @supply and with the embedded struct device refcount incremented by one.
+ * The refcount must be dropped by calling put_device().
+ * On failure one of the following ERR-PTR-encoded values is returned:
+ * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
+ * in the future.
+ */

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#5 lore raw

Re: [PATCH v5 02/10] regulator: Move OF-specific regulator lookup code to of_regulator.c

Quoted 27 lines · Andy Shevchenko
Quoted 11 lines · Chen-Yu Tsai

There's still a bit of OF-specific code in the regulator device lookup function.

Move those bits of code over to of_regulator.c, and create a new function of_regulator_dev_lookup() to encapsulate the code moved out of regulator_dev_lookup().

Also mark of_find_regulator_by_node() as static, since there are no other users in other compile units.

There are no functional changes.

...

Quoted 10 lines

+/**
+ * of_get_child_regulator - get a child regulator device node
+ * based on supply name
+ * @parent: Parent device node
+ * @prop_name: Combination regulator supply name and "-supply"
+ *
+ * Traverse all child nodes.
+ * Extract the child regulator device node corresponding to the supply name.
+ * returns the device node corresponding to the regulator if found, else
+ * returns NULL.

Quoted 3 lines

At the same time you may fix kernel-doc warnings (no "Return" section) in these three (on your wish you may fix others in a separate change, but it's not related to this series).

As you said some other functions are missing it as well, so I'll do a patch separate from this series to fix them all.

Quoted 7 lines
Quoted 1 line

+ */

...

Quoted 1 line

+/** of_regulator_dev_lookup - lookup a regulator device with device tree only

Something went wrong with the indentation.

Will fix, and also add a "Return" section.

Thanks
ChenYu

Quoted 16 lines
Quoted 10 lines

+ * @dev: Device pointer for regulator supply lookup.
+ * @supply: Supply name or regulator ID.
+ *
+ * If successful, returns a struct regulator_dev that corresponds to the name
+ * @supply and with the embedded struct device refcount incremented by one.
+ * The refcount must be dropped by calling put_device().
+ * On failure one of the following ERR-PTR-encoded values is returned:
+ * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
+ * in the future.
+ */

--
With Best Regards,
Andy Shevchenko

#6 lore raw

Re: [PATCH v5 02/10] regulator: Move OF-specific regulator lookup code to of_regulator.c

Quoted 3 lines · Chen-Yu Tsai
Quoted 1 line · Andy Shevchenko

On Thu, Aug 22, 2024 at 05:19:55PM +0800, Chen-Yu Tsai wrote:

...

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

+/**
+ * of_get_child_regulator - get a child regulator device node
+ * based on supply name
+ * @parent: Parent device node
+ * @prop_name: Combination regulator supply name and "-supply"
+ *
+ * Traverse all child nodes.
+ * Extract the child regulator device node corresponding to the supply name.
+ * returns the device node corresponding to the regulator if found, else
+ * returns NULL.

Quoted 6 lines
Quoted 3 lines

At the same time you may fix kernel-doc warnings (no "Return" section) in these three (on your wish you may fix others in a separate change, but it's not related to this series).

As you said some other functions are missing it as well, so I'll do a patch separate from this series to fix them all.

But you need to fix them in this patch series. We do not add patches with known issues, which are really easy to fix beforehand.

(And below seems you indirectly agrees on that)

Quoted 1 line
Quoted 1 line
Quoted 1 line

+ */

...

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

+/** of_regulator_dev_lookup - lookup a regulator device with device tree only

Something went wrong with the indentation.

Will fix, and also add a "Return" section.

Thank you!

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

+ * @dev: Device pointer for regulator supply lookup.
+ * @supply: Supply name or regulator ID.
+ *
+ * If successful, returns a struct regulator_dev that corresponds to the name
+ * @supply and with the embedded struct device refcount incremented by one.
+ * The refcount must be dropped by calling put_device().
+ * On failure one of the following ERR-PTR-encoded values is returned:
+ * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
+ * in the future.
+ */

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#7 lore raw

Re: [PATCH v5 02/10] regulator: Move OF-specific regulator lookup code to of_regulator.c

Quoted 23 lines · Andy Shevchenko
Quoted 3 lines · Chen-Yu Tsai
Quoted 1 line · Andy Shevchenko

On Thu, Aug 22, 2024 at 05:19:55PM +0800, Chen-Yu Tsai wrote:

...

Quoted 15 lines
Quoted 14 lines
Quoted 10 lines

+/**
+ * of_get_child_regulator - get a child regulator device node
+ * based on supply name
+ * @parent: Parent device node
+ * @prop_name: Combination regulator supply name and "-supply"
+ *
+ * Traverse all child nodes.
+ * Extract the child regulator device node corresponding to the supply name.
+ * returns the device node corresponding to the regulator if found, else
+ * returns NULL.

At the same time you may fix kernel-doc warnings (no "Return" section) in these three (on your wish you may fix others in a separate change, but it's not related to this series).

Quoted 5 lines
Quoted 2 lines

As you said some other functions are missing it as well, so I'll do a patch separate from this series to fix them all.

But you need to fix them in this patch series. We do not add patches with known issues, which are really easy to fix beforehand.

I'd say that's a preexisting thing, and code movement shouldn't change it. But, yeah, I can send another patch fixing up all the kerneldoc issues in both files.

ChenYu

Quoted 30 lines

(And below seems you indirectly agrees on that)

Quoted 1 line
Quoted 1 line
Quoted 1 line

+ */

...

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

+/** of_regulator_dev_lookup - lookup a regulator device with device tree only

Something went wrong with the indentation.

Will fix, and also add a "Return" section.

Thank you!

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

+ * @dev: Device pointer for regulator supply lookup.
+ * @supply: Supply name or regulator ID.
+ *
+ * If successful, returns a struct regulator_dev that corresponds to the name
+ * @supply and with the embedded struct device refcount incremented by one.
+ * The refcount must be dropped by calling put_device().
+ * On failure one of the following ERR-PTR-encoded values is returned:
+ * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
+ * in the future.
+ */

--
With Best Regards,
Andy Shevchenko

#8 lore raw

Re: [PATCH v5 02/10] regulator: Move OF-specific regulator lookup code to of_regulator.c

In reply to Chen-Yu Tsai
kernel test robot noticed the following build warnings:

Hi Chen-Yu,

kernel test robot noticed the following build warnings:

[auto build test WARNING on broonie-regulator/for-next]
[also build test WARNING on robh/for-next wsa/i2c/for-next linus/master v6.11-rc5 next-20240823] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Chen-Yu-Tsai/of-dynamic-Add-of_changeset_update_prop_string/20240826-105737
base:   https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regulator.git for-next
patch link:    https://lore.kernel.org/r/20240822092006.3134096-3-wenst%40chromium.org
patch subject: [PATCH v5 02/10] regulator: Move OF-specific regulator lookup code to of_regulator.c
config: i386-randconfig-005-20240826 (https://download.01.org/0day-ci/archive/20240826/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240826/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

In file included from drivers/regulator/helpers.c:17:
drivers/regulator/internal.h: In function 'of_regulator_dev_lookup':

>> drivers/regulator/internal.h:87:16: warning: returning 'int' from a function with return type 'struct regulator_dev *' makes pointer from integer without a cast [-Wint-conversion]
      87 |         return -ENODEV;
         |                ^

vim +87 drivers/regulator/internal.h

    82	
    83	#else
    84	static inline struct regulator_dev *of_regulator_dev_lookup(struct device *dev,
    85								    const char *supply)
    86	{
  > 87		return -ENODEV;
    88	}
    89	
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#9 lore raw

Re: [PATCH v5 02/10] regulator: Move OF-specific regulator lookup code to of_regulator.c

In reply to Chen-Yu Tsai
kernel test robot noticed the following build errors:

Hi Chen-Yu,

kernel test robot noticed the following build errors:

[auto build test ERROR on broonie-regulator/for-next]
[also build test ERROR on robh/for-next wsa/i2c/for-next linus/master v6.11-rc5 next-20240823] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Chen-Yu-Tsai/of-dynamic-Add-of_changeset_update_prop_string/20240826-105737
base:   https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regulator.git for-next
patch link:    https://lore.kernel.org/r/20240822092006.3134096-3-wenst%40chromium.org
patch subject: [PATCH v5 02/10] regulator: Move OF-specific regulator lookup code to of_regulator.c
config: parisc-randconfig-002-20240826 (https://download.01.org/0day-ci/archive/20240826/[email protected]/config)
compiler: hppa-linux-gcc (GCC) 14.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240826/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from drivers/regulator/helpers.c:17:
drivers/regulator/internal.h: In function 'of_regulator_dev_lookup':

>> drivers/regulator/internal.h:87:16: error: returning 'int' from a function with return type 'struct regulator_dev *' makes pointer from integer without a cast [-Wint-conversion]
      87 |         return -ENODEV;
         |                ^

vim +87 drivers/regulator/internal.h

    82	
    83	#else
    84	static inline struct regulator_dev *of_regulator_dev_lookup(struct device *dev,
    85								    const char *supply)
    86	{
  > 87		return -ENODEV;
    88	}
    89	
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#10 lore raw

Re: [PATCH v5 02/10] regulator: Move OF-specific regulator lookup code to of_regulator.c

In reply to Chen-Yu Tsai
kernel test robot noticed the following build errors:

Hi Chen-Yu,

kernel test robot noticed the following build errors:

[auto build test ERROR on broonie-regulator/for-next]
[also build test ERROR on robh/for-next wsa/i2c/for-next linus/master v6.11-rc5 next-20240826] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Chen-Yu-Tsai/of-dynamic-Add-of_changeset_update_prop_string/20240826-105737
base:   https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regulator.git for-next
patch link:    https://lore.kernel.org/r/20240822092006.3134096-3-wenst%40chromium.org
patch subject: [PATCH v5 02/10] regulator: Move OF-specific regulator lookup code to of_regulator.c
config: i386-buildonly-randconfig-003-20240826 (https://download.01.org/0day-ci/archive/20240826/[email protected]/config)
compiler: clang version 18.1.5 (https://github.com/llvm/llvm-project 617a15a9eac96088ae5e9134248d8236e34b91b1)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240826/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from drivers/regulator/devres.c:15:

>> drivers/regulator/internal.h:87:9: error: incompatible integer to pointer conversion returning 'int' from a function with result type 'struct regulator_dev *' [-Wint-conversion]
      87 |         return -ENODEV;
         |                ^~~~~~~

1 error generated.

vim +87 drivers/regulator/internal.h

    82	
    83	#else
    84	static inline struct regulator_dev *of_regulator_dev_lookup(struct device *dev,
    85								    const char *supply)
    86	{
  > 87		return -ENODEV;
    88	}
    89	
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author maintainer
#11 lore raw

PATCH v5 3/10 regulator: Split up _regulator_get()

In reply to Chen-Yu Tsai

_regulator_get() contains a lot of common code doing checks prior to the regulator lookup and housekeeping work after the lookup. Almost all the code could be shared with a OF-specific variant of _regulator_get().

Split out the common parts so that they can be reused. The OF-specific version of _regulator_get() will be added in a subsequent patch. No functional changes were made.

---
Changes since v4:
- New patch

---
 drivers/regulator/core.c     | 54 ++++++++++++++++++++++++++++--------
 drivers/regulator/internal.h |  4 +++
 2 files changed, 47 insertions(+), 11 deletions(-)
drivers/regulator/core.c +43 -11
index 361309fcae57..4d1c640cc030 100644
@@ -2093,26 +2093,43 @@ static int regulator_resolve_supply(struct regulator_dev *rdev)
 	return ret;
 }
 
-/* Internal regulator request function */
-struct regulator *_regulator_get(struct device *dev, const char *id,
-				 enum regulator_get_type get_type)
+/* common pre-checks for regulator requests */
+int _regulator_get_common_check(struct device *dev, const char *id,
+				enum regulator_get_type get_type)
 {
-	struct regulator_dev *rdev;
-	struct regulator *regulator;
-	struct device_link *link;
-	int ret;
-
 	if (get_type >= MAX_GET_TYPE) {
 		dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
-		return ERR_PTR(-EINVAL);
+		return -EINVAL;
 	}
 
 	if (id == NULL) {
 		dev_err(dev, "regulator request with no identifier\n");
-		return ERR_PTR(-EINVAL);
+		return -EINVAL;
 	}
 
-	rdev = regulator_dev_lookup(dev, id);
+	return 0;
+}
+
+/**
+ * _regulator_get_common - Common code for regulator requests
+ * @rdev: regulator device pointer as returned by *regulator_dev_lookup()
+ *       Its reference count is expected to have been incremented.
+ * @dev: device used for dev_printk messages
+ * @id: Supply name or regulator ID
+ * @get_type: enum regulator_get_type value corresponding to type of request
+ *
+ * Returns a struct regulator corresponding to @rdev,
+ * or IS_ERR() condition containing errno.
+ *
+ * This function should be chained with *regulator_dev_lookup() functions.
+ */
+struct regulator *_regulator_get_common(struct regulator_dev *rdev, struct device *dev,
+					const char *id, enum regulator_get_type get_type)
+{
+	struct regulator *regulator;
+	struct device_link *link;
+	int ret;
+
 	if (IS_ERR(rdev)) {
 		ret = PTR_ERR(rdev);
 
@@ -2228,6 +2245,21 @@ struct regulator *_regulator_get(struct device *dev, const char *id,
 	return regulator;
 }
 
+/* Internal regulator request function */
+struct regulator *_regulator_get(struct device *dev, const char *id,
+				 enum regulator_get_type get_type)
+{
+	struct regulator_dev *rdev;
+	int ret;
+
+	ret = _regulator_get_common_check(dev, id, get_type);
+	if (ret)
+		return ERR_PTR(ret);
+
+	rdev = regulator_dev_lookup(dev, id);
+	return _regulator_get_common(rdev, dev, id, get_type);
+}
+
 /**
  * regulator_get - lookup and obtain a reference to a regulator.
  * @dev: device for regulator "consumer"
drivers/regulator/internal.h +4 -0
index ffeaef22169e..3718459fb0c5 100644
@@ -121,6 +121,10 @@ enum regulator_get_type {
 	MAX_GET_TYPE
 };
 
+int _regulator_get_common_check(struct device *dev, const char *id,
+				enum regulator_get_type get_type);
+struct regulator *_regulator_get_common(struct regulator_dev *rdev, struct device *dev,
+					const char *id, enum regulator_get_type get_type);
 struct regulator *_regulator_get(struct device *dev, const char *id,
 				 enum regulator_get_type get_type);
 int _regulator_bulk_get(struct device *dev, int num_consumers,
Signature · 1 line
2.46.0.184.g6999bdac58-goog
#12 lore raw

Re: [PATCH v5 03/10] regulator: Split up _regulator_get()

Quoted 8 lines · Chen-Yu Tsai

_regulator_get() contains a lot of common code doing checks prior to the regulator lookup and housekeeping work after the lookup. Almost all the code could be shared with a OF-specific variant of _regulator_get().

Split out the common parts so that they can be reused. The OF-specific version of _regulator_get() will be added in a subsequent patch. No functional changes were made.

Quoted 12 lines

+/**
+ * _regulator_get_common - Common code for regulator requests
+ * @rdev: regulator device pointer as returned by *regulator_dev_lookup()
+ * Its reference count is expected to have been incremented.
+ * @dev: device used for dev_printk messages
+ * @id: Supply name or regulator ID
+ * @get_type: enum regulator_get_type value corresponding to type of request
+ *
+ * Returns a struct regulator corresponding to @rdev,
+ * or IS_ERR() condition containing errno.
+ *
+ * This function should be chained with *regulator_dev_lookup() functions.

kernel-doc will warn here: No "Return" section.

Quoted 1 line

+ */

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#13 lore raw

Re: [PATCH v5 03/10] regulator: Split up _regulator_get()

Quoted 23 lines · Andy Shevchenko
Quoted 8 lines · Chen-Yu Tsai

_regulator_get() contains a lot of common code doing checks prior to the regulator lookup and housekeeping work after the lookup. Almost all the code could be shared with a OF-specific variant of _regulator_get().

Split out the common parts so that they can be reused. The OF-specific version of _regulator_get() will be added in a subsequent patch. No functional changes were made.

Quoted 12 lines

+/**
+ * _regulator_get_common - Common code for regulator requests
+ * @rdev: regulator device pointer as returned by *regulator_dev_lookup()
+ * Its reference count is expected to have been incremented.
+ * @dev: device used for dev_printk messages
+ * @id: Supply name or regulator ID
+ * @get_type: enum regulator_get_type value corresponding to type of request
+ *
+ * Returns a struct regulator corresponding to @rdev,
+ * or IS_ERR() condition containing errno.
+ *
+ * This function should be chained with *regulator_dev_lookup() functions.

Quoted 1 line

kernel-doc will warn here: No "Return" section.

Will fix.

ChenYu

Quoted 7 lines
Quoted 1 line

+ */

--
With Best Regards,
Andy Shevchenko

author maintainer
#14 lore raw

PATCH v5 4/10 regulator: Do pure DT regulator lookup in of_regulator_bulk_get_all()

In reply to Chen-Yu Tsai

The to-be-introduced I2C component prober needs to enable regulator supplies (and toggle GPIO pins) for the various components it intends to probe. To support this, a new "pure DT lookup" method for getting regulator supplies is needed, since the device normally requesting the supply won't get created until after the component is probed to be available.

Convert the existing of_regulator_bulk_get_all() for this purpose. This function has no in-tree users, as the original patch [1] that used it was never landed. This patch changes the function ABI, but it is straightforward to convert users.

The underlying code that supports the existing regulator_get*() functions has been reworked in previous patches to support this specific case. An internal OF-specific version of regulator_get(), of_regulator_get_optional(), is added for this.

[1] https://lore.kernel.org/all/[email protected]/

---
Changes since v4:
- Was "regulator: Add regulator_of_get_optional() for pure DT" in v4
- Restore platform-agnostic regulator consumer code to original state
- Move OF-specific regulator code to of_regulator.c (separate patch)
- Split _regulator_get() into three parts for reuse (separate patch)
- Add OF-specific _of_regulator_get() function
- Rename regulator_of_get_optional() to of_regulator_get_optional() for consistency
- Make of_regulator_get_optional() static, as it is only used internally
- Convert of_regulator_bulk_get_all()
Changes since v3:
- New patch

---
 drivers/regulator/core.c         |  2 +-
 drivers/regulator/internal.h     |  2 ++
 drivers/regulator/of_regulator.c | 54 +++++++++++++++++++++++++++-----
 3 files changed, 49 insertions(+), 9 deletions(-)
drivers/regulator/core.c +1 -1
index 4d1c640cc030..d70302e30731 100644
@@ -1950,7 +1950,7 @@ static struct regulator_dev *regulator_dev_lookup(struct device *dev,
 
 	/* first do a dt based lookup */
 	if (dev && dev->of_node) {
-		r = of_regulator_dev_lookup(dev, supply);
+		r = of_regulator_dev_lookup(dev, dev->of_node, supply);
 		if (!IS_ERR(r))
 			return r;
 		if (PTR_ERR(r) == -EPROBE_DEFER)
drivers/regulator/internal.h +2 -0
index 3718459fb0c5..44680ce3587b 100644
@@ -67,6 +67,7 @@ static inline struct regulator_dev *dev_to_rdev(struct device *dev)
 
 #ifdef CONFIG_OF
 struct regulator_dev *of_regulator_dev_lookup(struct device *dev,
+					      struct device_node *np,
 					      const char *supply);
 struct regulator_init_data *regulator_of_get_init_data(struct device *dev,
 			         const struct regulator_desc *desc,
@@ -82,6 +83,7 @@ bool of_check_coupling_data(struct regulator_dev *rdev);
 
 #else
 static inline struct regulator_dev *of_regulator_dev_lookup(struct device *dev,
+							    struct device_node *np,
 							    const char *supply)
 {
 	return -ENODEV;
drivers/regulator/of_regulator.c +46 -8

101 lines not on this page · raw message

Signature · 1 line
2.46.0.184.g6999bdac58-goog
#15 lore raw

Re: [PATCH v5 04/10] regulator: Do pure DT regulator lookup in of_regulator_bulk_get_all()

Quoted 12 lines · Chen-Yu Tsai

The to-be-introduced I2C component prober needs to enable regulator supplies (and toggle GPIO pins) for the various components it intends to probe. To support this, a new "pure DT lookup" method for getting regulator supplies is needed, since the device normally requesting the supply won't get created until after the component is probed to be available.

Convert the existing of_regulator_bulk_get_all() for this purpose. This function has no in-tree users, as the original patch [1] that used it was never landed. This patch changes the function ABI, but it is straightforward to convert users.

Quoted 6 lines

The underlying code that supports the existing regulator_get*() functions has been reworked in previous patches to support this specific case. An internal OF-specific version of regulator_get(), of_regulator_get_optional(), is added for this.

[1] https://lore.kernel.org/all/[email protected]/

Make it Link tag

...

drivers/regulator/core.c static struct regulator_dev *regulator_dev_lookup(struct device *dev, · line 1952

Quoted 2 lines
drivers/regulator/core.c +0 -0
 	/* first do a dt based lookup */
 	if (dev && dev->of_node) {
	if (dev_of_node())

drivers/regulator/core.c static struct regulator_dev *regulator_dev_lookup(struct device *dev, · line 1953

Quoted 2 lines
drivers/regulator/core.c +1 -1
-		r = of_regulator_dev_lookup(dev, supply);
+		r = of_regulator_dev_lookup(dev, dev->of_node, supply);
	dev_of_node()
Quoted 3 lines
drivers/regulator/core.c +0 -0
 		if (!IS_ERR(r))
 			return r;
 		if (PTR_ERR(r) == -EPROBE_DEFER)

...

Quoted 4 lines

/**
* of_get_regulator - get a regulator device node based on supply name
- * @dev: Device pointer for the consumer (of regulator) device
+ * @dev: Device pointer for dev_printk messages

dev_printk()

Quoted 7 lines

+ * @node: Device node pointer for supply property lookup
* @supply: regulator supply name
*
* Extract the regulator device node corresponding to the supply name.
* returns the device node corresponding to the regulator if found, else
* returns NULL.
*/

...

Quoted 3 lines

/** of_regulator_dev_lookup - lookup a regulator device with device tree only
- * @dev: Device pointer for regulator supply lookup.
+ * @dev: Device pointer for dev_printk messages.

Ditto.

Quoted 8 lines

+ * @node: Device node pointer for regulator supply lookup.
* @supply: Supply name or regulator ID.
*
* If successful, returns a struct regulator_dev that corresponds to the name

drivers/regulator/of_regulator.c +0 -0
@@ -636,13 +639,13 @@ static struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
  * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
  * in the future.
  */

...

Quoted 3 lines

+/**
+ * of_regulator_get_optional - get optional regulator via device tree lookup
+ * @dev: device used for dev_printk messages

Ditto.

Quoted 10 lines

+ * @node: device node for regulator "consumer"
+ * @id: Supply name
+ *
+ * Returns a struct regulator corresponding to the regulator producer,
+ * or IS_ERR() condition containing errno.
+ *
+ * This is intended for use by consumers that want to get a regulator
+ * supply directly from a device node, and can and want to deal with
+ * absence of such supplies. This will _not_ consider supply aliases.
+ * See regulator_dev_lookup().

Fix kernel-doc warning.

Quoted 1 line

+ */

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#16 lore raw

Re: [PATCH v5 04/10] regulator: Do pure DT regulator lookup in of_regulator_bulk_get_all()

Quoted 26 lines · Andy Shevchenko
Quoted 12 lines · Chen-Yu Tsai

The to-be-introduced I2C component prober needs to enable regulator supplies (and toggle GPIO pins) for the various components it intends to probe. To support this, a new "pure DT lookup" method for getting regulator supplies is needed, since the device normally requesting the supply won't get created until after the component is probed to be available.

Convert the existing of_regulator_bulk_get_all() for this purpose. This function has no in-tree users, as the original patch [1] that used it was never landed. This patch changes the function ABI, but it is straightforward to convert users.

Quoted 6 lines

The underlying code that supports the existing regulator_get*() functions has been reworked in previous patches to support this specific case. An internal OF-specific version of regulator_get(), of_regulator_get_optional(), is added for this.

[1] https://lore.kernel.org/all/[email protected]/

Make it Link tag

Quoted 7 lines

...

Quoted 2 lines
      /* first do a dt based lookup */
      if (dev && dev->of_node) {
        if (dev_of_node())

Not part of the original change, but I don't see why not.

Quoted 4 lines

drivers/regulator/core.c static struct regulator_dev *regulator_dev_lookup(struct device *dev, · line 1953

Quoted 2 lines
drivers/regulator/core.c +1 -1
-             r = of_regulator_dev_lookup(dev, supply);
+             r = of_regulator_dev_lookup(dev, dev->of_node, supply);
        dev_of_node()

Ack.

Quoted 4 lines
Quoted 3 lines
              if (!IS_ERR(r))
                      return r;
              if (PTR_ERR(r) == -EPROBE_DEFER)
Quoted 8 lines

...

Quoted 4 lines

/**
* of_get_regulator - get a regulator device node based on supply name
- * @dev: Device pointer for the consumer (of regulator) device
+ * @dev: Device pointer for dev_printk messages

dev_printk()

Ack.

Quoted 8 lines
Quoted 7 lines

+ * @node: Device node pointer for supply property lookup
* @supply: regulator supply name
*
* Extract the regulator device node corresponding to the supply name.
* returns the device node corresponding to the regulator if found, else
* returns NULL.
*/

Quoted 7 lines

...

Quoted 3 lines

/** of_regulator_dev_lookup - lookup a regulator device with device tree only
- * @dev: Device pointer for regulator supply lookup.
+ * @dev: Device pointer for dev_printk messages.

Ditto.

Ack.

Quoted 11 lines
Quoted 8 lines

+ * @node: Device node pointer for regulator supply lookup.
* @supply: Supply name or regulator ID.
*
* If successful, returns a struct regulator_dev that corresponds to the name

drivers/regulator/of_regulator.c +0 -0
@@ -636,13 +639,13 @@ static struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
  * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
  * in the future.
  */

...

Quoted 5 lines
Quoted 3 lines

+/**
+ * of_regulator_get_optional - get optional regulator via device tree lookup
+ * @dev: device used for dev_printk messages

Ditto.

Ack.

Quoted 11 lines
Quoted 10 lines

+ * @node: device node for regulator "consumer"
+ * @id: Supply name
+ *
+ * Returns a struct regulator corresponding to the regulator producer,
+ * or IS_ERR() condition containing errno.
+ *
+ * This is intended for use by consumers that want to get a regulator
+ * supply directly from a device node, and can and want to deal with
+ * absence of such supplies. This will _not_ consider supply aliases.
+ * See regulator_dev_lookup().

Quoted 1 line

Fix kernel-doc warning.

Ack.

Thanks
ChenYu

Quoted 7 lines
Quoted 1 line

+ */

--
With Best Regards,
Andy Shevchenko

author maintainer
#17 lore raw

PATCH v5 5/10 gpiolib: Add gpio_property_name_length()

In reply to Chen-Yu Tsai

The I2C device tree component prober needs to get and toggle GPIO lines for the components it intends to probe. These components may not use the same name for their GPIO lines, so the prober must go through the device tree, check each property to see it is a GPIO property, and get the GPIO line.

Instead of duplicating the GPIO suffixes, or exporting them to the prober to do pattern matching, simply add and export a new function that does the pattern matching and returns the length of the GPIO name. The caller can then use that to copy out the name if it needs to.

---
Changes since v4:
- new patch

Depends on commit 4b91188dced8 ("gpiolib: Replace gpio_suffix_count with NULL-terminated array").

---
 drivers/gpio/gpiolib.c        | 44 +++++++++++++++++++++++++++++++++++
 include/linux/gpio/consumer.h |  2 ++
 2 files changed, 46 insertions(+)
drivers/gpio/gpiolib.c +44 -0
index 3903d0a75304..e4228ef6f131 100644
@@ -4295,6 +4295,50 @@ struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
 }
 EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index);
 
+/**
+ * gpio_property_name_length - Returns the GPIO name length from a property name
+ * @str:	string to check
+ *
+ * This function checks if the given name matches the GPIO property patterns, and
+ * returns the length of the name of the GPIO. The pattern is "*-<GPIO suffix>"
+ * or just "<GPIO suffix>".
+ *
+ * Returns:
+ * The length of the string before '-' if it matches "*-<GPIO suffix>", or
+ * 0 if no name part, just the suffix, or
+ * -EINVAL if the string doesn't match the pattern.
+ */
+int gpio_property_name_length(const char *str)
+{
+	size_t len;
+
+	len = strlen(str);
+
+	/* string need to be at minimum len(gpio) */
+	if (len < 4)
+		return -EINVAL;
+
+	/* Check for no-name case: "gpio" / "gpios" */
+	for (const char *const *p = gpio_suffixes; *p; p++)
+		if (!strcmp(str, *p))
+			return 0;
+
+	for (size_t i = len - 4; i > 0; i--) {
+		/* find right-most '-' and check if remainder matches suffix */
+		if (str[i] != '-')
+			continue;
+
+		for (const char *const *p = gpio_suffixes; *p; p++)
+			if (!strcmp(str + i + 1, *p))
+				return i;
+
+		return -EINVAL;
+	}
+
+	return -EINVAL;
+}
+EXPORT_SYMBOL_GPL(gpio_property_name_length);
+
 /**
  * gpiod_count - return the number of GPIOs associated with a device / function
  *		or -ENOENT if no GPIO has been assigned to the requested function
include/linux/gpio/consumer.h +2 -0
index db2dfbae8edb..ce3a5f86a037 100644
@@ -56,6 +56,8 @@ enum gpiod_flags {
 
 #ifdef CONFIG_GPIOLIB
 
+int gpio_property_name_length(const char *name);
+
 /* Return the number of GPIOs associated with a device / function */
 int gpiod_count(struct device *dev, const char *con_id);
 
Signature · 1 line
2.46.0.184.g6999bdac58-goog
#18 lore raw

Re: [PATCH v5 05/10] gpiolib: Add gpio_property_name_length()

Quoted 10 lines · Chen-Yu Tsai

The I2C device tree component prober needs to get and toggle GPIO lines for the components it intends to probe. These components may not use the same name for their GPIO lines, so the prober must go through the device tree, check each property to see it is a GPIO property, and get the GPIO line.

Instead of duplicating the GPIO suffixes, or exporting them to the prober to do pattern matching, simply add and export a new function that does the pattern matching and returns the length of the GPIO name. The caller can then use that to copy out the name if it needs to.

...

Quoted 3 lines

+/**
+ * gpio_property_name_length - Returns the GPIO name length from a property name
+ * @str: string to check

It's property name, so, I would name this 'propname'.

Quoted 6 lines

+ * This function checks if the given name matches the GPIO property patterns, and
+ * returns the length of the name of the GPIO. The pattern is "*-<GPIO suffix>"
+ * or just "<GPIO suffix>".
+ *
+ * Returns:
+ * The length of the string before '-' if it matches "*-<GPIO suffix>", or

What about "x-y-gpios"? It's unclear what will be the behaviour.

Quoted 4 lines

+ * 0 if no name part, just the suffix, or
+ * -EINVAL if the string doesn't match the pattern.
+ */
+int gpio_property_name_length(const char *str)

gpio_get_... ?

drivers/gpio/gpiolib.c struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode, · line 4315

Quoted 4 lines
drivers/gpio/gpiolib.c +4 -0
+{
+	size_t len;
+
+	len = strlen(str);

If it has a thousands characters...?

drivers/gpio/gpiolib.c struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode, · line 4319

Quoted 3 lines
drivers/gpio/gpiolib.c +3 -0
+	/* string need to be at minimum len(gpio) */
+	if (len < 4)
+		return -EINVAL;

Do we really need it here? See below as well.

Quoted 6 lines
drivers/gpio/gpiolib.c +5 -0
+	/* Check for no-name case: "gpio" / "gpios" */
+	for (const char *const *p = gpio_suffixes; *p; p++)
+		if (!strcmp(str, *p))
+			return 0;
 
+	for (size_t i = len - 4; i > 0; i--) {

drivers/gpio/gpiolib.c struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode, · line 4336

Quoted 10 lines
drivers/gpio/gpiolib.c +10 -0
+		/* find right-most '-' and check if remainder matches suffix */
+		if (str[i] != '-')
+			continue;
+
+		for (const char *const *p = gpio_suffixes; *p; p++)
+			if (!strcmp(str + i + 1, *p))
+				return i;
+
+		return -EINVAL;
+	}

This can be combined with the above

	for (const char *const *p = gpio_suffixes; *p; p++) {
		/*
		 * Find right-most '-' and check if remainder matches suffix.
		 * If no separator found, check for no-name cases.
		 */
		dash = strrchr(propname, '-');
		if (!strcmp(dash ? dash + 1 : propname, *p))
			return i;
	}
Quoted 2 lines
(quoted diff) +2 -0
+	return -EINVAL;
+}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#19 lore raw

Re: [PATCH v5 05/10] gpiolib: Add gpio_property_name_length()

Quoted 13 lines · Andy Shevchenko
Quoted 10 lines · Chen-Yu Tsai

The I2C device tree component prober needs to get and toggle GPIO lines for the components it intends to probe. These components may not use the same name for their GPIO lines, so the prober must go through the device tree, check each property to see it is a GPIO property, and get the GPIO line.

Instead of duplicating the GPIO suffixes, or exporting them to the prober to do pattern matching, simply add and export a new function that does the pattern matching and returns the length of the GPIO name. The caller can then use that to copy out the name if it needs to.

Quoted 7 lines

...

Quoted 3 lines

+/**
+ * gpio_property_name_length - Returns the GPIO name length from a property name
+ * @str: string to check

It's property name, so, I would name this 'propname'.

Ack.

Quoted 8 lines
Quoted 6 lines

+ * This function checks if the given name matches the GPIO property patterns, and
+ * returns the length of the name of the GPIO. The pattern is "*-<GPIO suffix>"
+ * or just "<GPIO suffix>".
+ *
+ * Returns:
+ * The length of the string before '-' if it matches "*-<GPIO suffix>", or

What about "x-y-gpios"? It's unclear what will be the behaviour.

I thought it was implied that the '-' mentioned here is the one before the suffix. I made it more explicit.

Quoted 6 lines
Quoted 4 lines

+ * 0 if no name part, just the suffix, or
+ * -EINVAL if the string doesn't match the pattern.
+ */
+int gpio_property_name_length(const char *str)

gpio_get_... ?

Ack.

Quoted 6 lines

drivers/gpio/gpiolib.c struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode, · line 4315

Quoted 4 lines
drivers/gpio/gpiolib.c +4 -0
+{
+     size_t len;
+
+     len = strlen(str);

If it has a thousands characters...?

Shouldn't matter much? I suppose using strrchr() as you suggested requires one less pass.

Quoted 25 lines

drivers/gpio/gpiolib.c struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode, · line 4319

Quoted 3 lines
drivers/gpio/gpiolib.c +3 -0
+     /* string need to be at minimum len(gpio) */
+     if (len < 4)
+             return -EINVAL;

Do we really need it here? See below as well.

Quoted 6 lines
drivers/gpio/gpiolib.c +5 -0
+     /* Check for no-name case: "gpio" / "gpios" */
+     for (const char *const *p = gpio_suffixes; *p; p++)
+             if (!strcmp(str, *p))
+                     return 0;
 
+     for (size_t i = len - 4; i > 0; i--) {

drivers/gpio/gpiolib.c struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode, · line 4336

Quoted 10 lines
drivers/gpio/gpiolib.c +10 -0
+             /* find right-most '-' and check if remainder matches suffix */
+             if (str[i] != '-')
+                     continue;
+
+             for (const char *const *p = gpio_suffixes; *p; p++)
+                     if (!strcmp(str + i + 1, *p))
+                             return i;
+
+             return -EINVAL;
+     }

This can be combined with the above

Quoted 6 lines
        for (const char *const *p = gpio_suffixes; *p; p++) {
                /*
                 * Find right-most '-' and check if remainder matches suffix.
                 * If no separator found, check for no-name cases.
                 */
                dash = strrchr(propname, '-');

I believe this line could be moved out of the for-loop. Otherwise it looks much more concise compared to my version. I'll omit the comment though, as it is just rehashing the kerneldoc description, and now that the function is so short, it shouldn't be hard to read.

I'll add you as "Suggested-by".

Thanks
ChenYu

Quoted 12 lines
                if (!strcmp(dash ? dash + 1 : propname, *p))
                        return i;
        }

Review

Quoted 2 lines
(quoted diff) +2 -0
+     return -EINVAL;
+}

--
With Best Regards,
Andy Shevchenko

#20 lore raw

Re: [PATCH v5 05/10] gpiolib: Add gpio_property_name_length()

Quoted 3 lines · Chen-Yu Tsai
Quoted 1 line · Andy Shevchenko

On Thu, Aug 22, 2024 at 05:19:58PM +0800, Chen-Yu Tsai wrote:

...

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

+ len = strlen(str);

If it has a thousands characters...?

Shouldn't matter much? I suppose using strrchr() as you suggested requires one less pass.

Yes, this is the point.

...

Quoted 9 lines
Quoted 8 lines

This can be combined with the above

        for (const char *const *p = gpio_suffixes; *p; p++) {
                /*
                 * Find right-most '-' and check if remainder matches suffix.
                 * If no separator found, check for no-name cases.
                 */
                dash = strrchr(propname, '-');
Quoted 4 lines

I believe this line could be moved out of the for-loop. Otherwise it looks much more concise compared to my version. I'll omit the comment though, as it is just rehashing the kerneldoc description, and now that the function is so short, it shouldn't be hard to read.

Agree. And I put comment inside the loop, while it should be outside. But then, as you said, the function becomes so little that kernel-doc above does the job, hence no comment in the code needed.

Quoted 1 line

I'll add you as "Suggested-by".

Fine with me.

Quoted 3 lines
Quoted 3 lines
                if (!strcmp(dash ? dash + 1 : propname, *p))
                        return i;
        }
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#21 lore raw

PATCH v5 6/10 i2c: Introduce OF component probe function

In reply to Chen-Yu Tsai

Some devices are designed and manufactured with some components having multiple drop-in replacement options. These components are often connected to the mainboard via ribbon cables, having the same signals and pin assignments across all options. These may include the display panel and touchscreen on laptops and tablets, and the trackpad on laptops. Sometimes which component option is used in a particular device can be detected by some firmware provided identifier, other times that information is not available, and the kernel has to try to probe each device.

This change attempts to make the "probe each device" case cleaner. The current approach is to have all options added and enabled in the device tree. The kernel would then bind each device and run each driver's probe function. This works, but has been broken before due to the introduction of asynchronous probing, causing multiple instances requesting "shared" resources, such as pinmuxes, GPIO pins, interrupt lines, at the same time, with only one instance succeeding. Work arounds for these include moving the pinmux to the parent I2C controller, using GPIO hogs or pinmux settings to keep the GPIO pins in some fixed configuration, and requesting the interrupt line very late. Such configurations can be seen on the MT8183 Krane Chromebook tablets, and the Qualcomm sc8280xp-based Lenovo Thinkpad 13S.

Instead of this delicate dance between drivers and device tree quirks, this change introduces a simple I2C component probe. function For a given class of devices on the same I2C bus, it will go through all of them, doing a simple I2C read transfer and see which one of them responds. It will then enable the device that responds.

This requires some minor modifications in the existing device tree. The status for all the device nodes for the component options must be set to "failed-needs-probe". This makes it clear that some mechanism is needed to enable one of them, and also prevents the prober and device drivers running at the same time.

---
Changes since v4:
- Split code into helper functions
- Use scoped helpers and __free() to reduce error path

Changes since v3:
- Complete kernel-doc
- Return different error if I2C controller is disabled
- Expand comment to explain assumptions and constraints
- Split for-loop finding target node and operations on target node
- Add missing i2c_put_adapter()
- Move prober code to separate file

Rob also asked why there was a limitation of "exactly one touchscreen will be enabled across the whole tree".

The use case this prober currently targets is a component on consumer electronics (tablet or laptop) being swapped out due to cost or supply reasons. Designs with multiple components of the same type are pretty rare. The way the next patch is written also assumes this for efficiency reasons.

Changes since v2:
- New patch split out from "of: Introduce hardware prober driver"
- Addressed Rob's comments
- Move i2c prober to i2c subsystem
- Use of_node_is_available() to check if node is enabled.
- Use OF changeset API to update status property
- Addressed Andy's comments
- Probe function now accepts "struct device *dev" instead to reduce line length and dereferences
- Move "ret = 0" to just before for_each_child_of_node(i2c_node, node)

---
 drivers/i2c/Makefile             |   1 +
 drivers/i2c/i2c-core-of-prober.c | 157 +++++++++++++++++++++++++++++++
 include/linux/i2c.h              |   4 +
 3 files changed, 162 insertions(+)
 create mode 100644 drivers/i2c/i2c-core-of-prober.c
drivers/i2c/Makefile +1 -0
index 3f71ce4711e3..0c543c742895 100644
@@ -9,6 +9,7 @@ i2c-core-objs 			:= i2c-core-base.o i2c-core-smbus.o
 i2c-core-$(CONFIG_ACPI)		+= i2c-core-acpi.o
 i2c-core-$(CONFIG_I2C_SLAVE) 	+= i2c-core-slave.o
 i2c-core-$(CONFIG_OF) 		+= i2c-core-of.o
+i2c-core-$(CONFIG_OF_DYNAMIC) 	+= i2c-core-of-prober.o
 
 obj-$(CONFIG_I2C_SMBUS)		+= i2c-smbus.o
 obj-$(CONFIG_I2C_CHARDEV)	+= i2c-dev.o
drivers/i2c/i2c-core-of-prober.c new file +157 -0

157 lines not on this page · raw message

include/linux/i2c.h +4 -0
index 377def497298..4c48ea93ec49 100644
@@ -1030,6 +1030,10 @@ const struct of_device_id
 int of_i2c_get_board_info(struct device *dev, struct device_node *node,
 			  struct i2c_board_info *info);
 
+#if IS_ENABLED(CONFIG_OF_DYNAMIC)
+int i2c_of_probe_component(struct device *dev, const char *type);
+#endif
+
 #else
 
 static inline struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
Signature · 1 line
2.46.0.184.g6999bdac58-goog
#22 lore raw

Re: [PATCH v5 06/10] i2c: Introduce OF component probe function

Quoted 34 lines · Chen-Yu Tsai

Some devices are designed and manufactured with some components having multiple drop-in replacement options. These components are often connected to the mainboard via ribbon cables, having the same signals and pin assignments across all options. These may include the display panel and touchscreen on laptops and tablets, and the trackpad on laptops. Sometimes which component option is used in a particular device can be detected by some firmware provided identifier, other times that information is not available, and the kernel has to try to probe each device.

This change attempts to make the "probe each device" case cleaner. The current approach is to have all options added and enabled in the device tree. The kernel would then bind each device and run each driver's probe function. This works, but has been broken before due to the introduction of asynchronous probing, causing multiple instances requesting "shared" resources, such as pinmuxes, GPIO pins, interrupt lines, at the same time, with only one instance succeeding. Work arounds for these include moving the pinmux to the parent I2C controller, using GPIO hogs or pinmux settings to keep the GPIO pins in some fixed configuration, and requesting the interrupt line very late. Such configurations can be seen on the MT8183 Krane Chromebook tablets, and the Qualcomm sc8280xp-based Lenovo Thinkpad 13S.

Instead of this delicate dance between drivers and device tree quirks, this change introduces a simple I2C component probe. function For a given class of devices on the same I2C bus, it will go through all of them, doing a simple I2C read transfer and see which one of them responds. It will then enable the device that responds.

This requires some minor modifications in the existing device tree. The status for all the device nodes for the component options must be set to "failed-needs-probe". This makes it clear that some mechanism is needed to enable one of them, and also prevents the prober and device drivers running at the same time.

...

drivers/i2c/Makefile i2c-core-objs := i2c-core-base.o i2c-core-smbus.o · line 12

Quoted 7 lines
drivers/i2c/Makefile +1 -0
@@ -9,6 +9,7 @@ i2c-core-objs 			:= i2c-core-base.o i2c-core-smbus.o
 i2c-core-$(CONFIG_ACPI)		+= i2c-core-acpi.o
 i2c-core-$(CONFIG_I2C_SLAVE) 	+= i2c-core-slave.o
 i2c-core-$(CONFIG_OF) 		+= i2c-core-of.o
+i2c-core-$(CONFIG_OF_DYNAMIC) 	+= i2c-core-of-prober.o

Seems like all the above (except ACPI) have the same issue, i.e. TABs/spaces mixture.

...

drivers/i2c/Makefile i2c-core-objs := i2c-core-base.o i2c-core-smbus.o · line 14

Quoted 2 lines
drivers/i2c/Makefile +2 -0
+	ret = of_changeset_apply(ocs);
+	if (!ret) {

Why not positive conditional?

Quoted 9 lines
drivers/i2c/Makefile +9 -0
+		/*
+		 * ocs is intentionally kept around as it needs to
+		 * exist as long as the change is applied.
+		 */
+		void *ptr __always_unused = no_free_ptr(ocs);
+	} else {
+		/* ocs needs to be explicitly cleaned up before being freed. */
+		of_changeset_destroy(ocs);
+	}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#23 lore raw

Re: [PATCH v5 06/10] i2c: Introduce OF component probe function

Quoted 39 lines · Andy Shevchenko
Quoted 34 lines · Chen-Yu Tsai

Some devices are designed and manufactured with some components having multiple drop-in replacement options. These components are often connected to the mainboard via ribbon cables, having the same signals and pin assignments across all options. These may include the display panel and touchscreen on laptops and tablets, and the trackpad on laptops. Sometimes which component option is used in a particular device can be detected by some firmware provided identifier, other times that information is not available, and the kernel has to try to probe each device.

This change attempts to make the "probe each device" case cleaner. The current approach is to have all options added and enabled in the device tree. The kernel would then bind each device and run each driver's probe function. This works, but has been broken before due to the introduction of asynchronous probing, causing multiple instances requesting "shared" resources, such as pinmuxes, GPIO pins, interrupt lines, at the same time, with only one instance succeeding. Work arounds for these include moving the pinmux to the parent I2C controller, using GPIO hogs or pinmux settings to keep the GPIO pins in some fixed configuration, and requesting the interrupt line very late. Such configurations can be seen on the MT8183 Krane Chromebook tablets, and the Qualcomm sc8280xp-based Lenovo Thinkpad 13S.

Instead of this delicate dance between drivers and device tree quirks, this change introduces a simple I2C component probe. function For a given class of devices on the same I2C bus, it will go through all of them, doing a simple I2C read transfer and see which one of them responds. It will then enable the device that responds.

This requires some minor modifications in the existing device tree. The status for all the device nodes for the component options must be set to "failed-needs-probe". This makes it clear that some mechanism is needed to enable one of them, and also prevents the prober and device drivers running at the same time.

...

Quoted 10 lines

drivers/i2c/Makefile i2c-core-objs := i2c-core-base.o i2c-core-smbus.o · line 12

Quoted 7 lines
drivers/i2c/Makefile +1 -0
@@ -9,6 +9,7 @@ i2c-core-objs                         := i2c-core-base.o i2c-core-smbus.o
 i2c-core-$(CONFIG_ACPI)              += i2c-core-acpi.o
 i2c-core-$(CONFIG_I2C_SLAVE)         += i2c-core-slave.o
 i2c-core-$(CONFIG_OF)                += i2c-core-of.o
+i2c-core-$(CONFIG_OF_DYNAMIC)        += i2c-core-of-prober.o

Seems like all the above (except ACPI) have the same issue, i.e. TABs/spaces mixture.

I was lazy and just copied the previous line. I'll fix the new line for now.

Quoted 6 lines

...

drivers/i2c/Makefile

Quoted 2 lines
drivers/i2c/Makefile +2 -0
+     ret = of_changeset_apply(ocs);
+     if (!ret) {

Why not positive conditional?

No real reason. I suppose having the error condition come first is more common. Not sure if it makes any difference in this case though?

Thanks
ChenYu

Quoted 15 lines

drivers/i2c/Makefile

Quoted 9 lines
drivers/i2c/Makefile +9 -0
+             /*
+              * ocs is intentionally kept around as it needs to
+              * exist as long as the change is applied.
+              */
+             void *ptr __always_unused = no_free_ptr(ocs);
+     } else {
+             /* ocs needs to be explicitly cleaned up before being freed. */
+             of_changeset_destroy(ocs);
+     }

--
With Best Regards,
Andy Shevchenko

#24 lore raw

Re: [PATCH v5 06/10] i2c: Introduce OF component probe function

Quoted 3 lines · Chen-Yu Tsai
Quoted 1 line · Andy Shevchenko

On Thu, Aug 22, 2024 at 05:19:59PM +0800, Chen-Yu Tsai wrote:

...

Quoted 7 lines
Quoted 4 lines

drivers/i2c/i2c-core-of-prober.c line 66

Quoted 2 lines
drivers/i2c/i2c-core-of-prober.c new file +2 -0
+     ret = of_changeset_apply(ocs);
+     if (!ret) {

Why not positive conditional?

No real reason. I suppose having the error condition come first is more common.

Yes, when you have something like

	if (err) {
		...
		return err;
	} else {
		...
	}

But you don't. That's why I commented on this.

Quoted 1 line

Not sure if it makes any difference in this case though?

! is hard to read by a human being, easy to make a mistake in the brain of reader and with inverted logic the code reading becomes harder. So, it's pure about cognitive function.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
drivers/i2c/i2c-core-of-prober.c new file +9 -0
+             /*
+              * ocs is intentionally kept around as it needs to
+              * exist as long as the change is applied.
+              */
+             void *ptr __always_unused = no_free_ptr(ocs);
+     } else {
+             /* ocs needs to be explicitly cleaned up before being freed. */
+             of_changeset_destroy(ocs);
+     }
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#25 lore raw

PATCH v5 7/10 i2c: of-prober: Add regulator support

In reply to Chen-Yu Tsai

This adds regulator management to the I2C OF component prober. Components that the prober intends to probe likely require their regulator supplies be enabled, and GPIOs be toggled to enable them or bring them out of reset before they will respond to probe attempts. GPIOs will be handled in the next patch.

Without specific knowledge of each component's resource names or power sequencing requirements, the prober can only enable the regulator supplies all at once, and toggle the GPIOs all at once. Luckily, reset pins tend to be active low, while enable pins tend to be active high, so setting the raw status of all GPIO pins to high should work. The wait time before and after resources are enabled are collected from existing drivers and device trees.

The prober collects resources from all possible components and enables them together, instead of enabling resources and probing each component one by one. The latter approach does not provide any boot time benefits over simply enabling each component and letting each driver probe sequentially.

The prober will also deduplicate the resources, since on a component swap out or co-layout design, the resources are always the same. While duplicate regulator supplies won't cause much issue, shared GPIOs don't work reliably, especially with other drivers. For the same reason, the prober will release the GPIOs before the successfully probed component is actually enabled.

---
Changes since v4:
- Split out GPIO handling to separate patch
- Rewrote using of_regulator_bulk_get_all()
- Replaced "regulators" with "regulator supplies" in debug messages

Changes since v3:
- New patch

This change is kept as a separate patch for now since the changes are quite numerous.

---
 drivers/i2c/i2c-core-of-prober.c | 114 ++++++++++++++++++++++++++++++-
 1 file changed, 113 insertions(+), 1 deletion(-)
drivers/i2c/i2c-core-of-prober.c +113 -1
index bb7b231201b0..32184cfd10f6 100644
@@ -6,12 +6,14 @@
  */
 
 #include <linux/cleanup.h>
+#include <linux/delay.h>
 #include <linux/device.h>
 #include <linux/dev_printk.h>
 #include <linux/err.h>
 #include <linux/i2c.h>
 #include <linux/module.h>
 #include <linux/of.h>
+#include <linux/regulator/consumer.h>
 #include <linux/slab.h>
 
 /*
@@ -27,10 +29,93 @@
  * address responds.
  *
  * TODO:
- * - Support handling common regulators and GPIOs.
+ * - Support handling common GPIOs.
  * - Support I2C muxes
  */
 
+struct i2c_of_probe_data {
+	struct regulator_bulk_data *regulators;
+	unsigned int regulators_num;
+};
+
+/* Returns number of regulator supplies found for node, or error. */
+static int i2c_of_probe_get_regulator(struct device *dev, struct device_node *node,
+				      struct i2c_of_probe_data *data)
+{
+	struct regulator_bulk_data *tmp, *new_regulators;
+	int ret;
+
+	ret = of_regulator_bulk_get_all(dev, node, &tmp);
+	if (ret <= 0)
+		return ret;
+
+	if (!data->regulators) {
+		data->regulators = tmp;
+		data->regulators_num = ret;
+		return ret;
+	};
+
+	new_regulators = krealloc(data->regulators,
+				  sizeof(*tmp) * (data->regulators_num + ret),
+				  GFP_KERNEL);
+	if (!new_regulators) {
+		regulator_bulk_free(ret, tmp);
+		return -ENOMEM;
+	}
+
+	data->regulators = new_regulators;
+
+	for (unsigned int i = 0; i < ret; i++)
+		memcpy(&data->regulators[data->regulators_num++], &tmp[i], sizeof(*tmp));
+
+	return ret;
+}
+
+static void i2c_of_probe_free_res(struct i2c_of_probe_data *data)
+{
+	regulator_bulk_free(data->regulators_num, data->regulators);
+}
+
+static int i2c_of_probe_get_res(struct device *dev, struct device_node *node,
+				struct i2c_of_probe_data *data)
+{
+	struct property *prop;
+	int ret;
+
+	ret = i2c_of_probe_get_regulator(dev, node, data);
+	if (ret < 0) {
+		dev_err_probe(dev, ret, "Failed to get regulator supplies from %pOF\n", node);
+		goto err_cleanup;
+	}
+
+	return 0;
+
+err_cleanup:
+	i2c_of_probe_free_res(data);
+	return ret;
+}
+
+static int i2c_of_probe_enable_res(struct device *dev, struct i2c_of_probe_data *data)
+{
+	int ret = 0;
+
+	dev_dbg(dev, "Enabling regulator supplies\n");
+
+	ret = regulator_bulk_enable(data->regulators_num, data->regulators);
+	if (ret)
+		return ret;
+
+	/* largest post-power-on pre-reset-deassert delay seen among drivers */
+	msleep(500);
+
+	return 0;
+}
+
+static void i2c_of_probe_disable_regulators(struct i2c_of_probe_data *data)
+{
+	regulator_bulk_disable(data->regulators_num, data->regulators);
+}
+
 static struct device_node *i2c_of_probe_get_i2c_node(struct device *dev, const char *type)
 {
 	struct device_node *node __free(device_node) = of_find_node_by_name(NULL, type);
@@ -110,6 +195,7 @@ static int i2c_of_probe_enable_node(struct device *dev, struct device_node *node
 int i2c_of_probe_component(struct device *dev, const char *type)
 {
 	struct i2c_adapter *i2c;
+	struct i2c_of_probe_data probe_data = {0};
 	int ret;
 
 	struct device_node *i2c_node __free(device_node) = i2c_of_probe_get_i2c_node(dev, type);
@@ -133,6 +219,30 @@ int i2c_of_probe_component(struct device *dev, const char *type)
 	if (!i2c)
 		return dev_err_probe(dev, -EPROBE_DEFER, "Couldn't get I2C adapter\n");
 
+	/* Grab resources */
+	for_each_child_of_node_scoped(i2c_node, node) {
+		u32 addr;
+
+		if (!of_node_name_prefix(node, type))
+			continue;
+		if (of_property_read_u32(node, "reg", &addr))
+			continue;
+
+		dev_dbg(dev, "Requesting resources for %pOF\n", node);
+		ret = i2c_of_probe_get_res(dev, node, &probe_data);
+		if (ret)
+			return ret;
+	}
+
+	dev_dbg(dev, "Resources: # of regulator supplies = %d\n", probe_data.regulators_num);
+
+	/* Enable resources */
+	ret = i2c_of_probe_enable_res(dev, &probe_data);
+	if (ret) {
+		i2c_of_probe_free_res(&probe_data);
+		return dev_err_probe(dev, ret, "Failed to enable resources\n");
+	}
+
 	ret = 0;
 	for_each_child_of_node_scoped(i2c_node, node) {
 		union i2c_smbus_data data;
@@ -150,6 +260,8 @@ int i2c_of_probe_component(struct device *dev, const char *type)
 		break;
 	}
 
+	i2c_of_probe_disable_regulators(&probe_data);
+	i2c_of_probe_free_res(&probe_data);
 	i2c_put_adapter(i2c);
 
 	return ret;
Signature · 1 line
2.46.0.184.g6999bdac58-goog
#26 lore raw

Re: [PATCH v5 07/10] i2c: of-prober: Add regulator support

Quoted 26 lines · Chen-Yu Tsai

This adds regulator management to the I2C OF component prober. Components that the prober intends to probe likely require their regulator supplies be enabled, and GPIOs be toggled to enable them or bring them out of reset before they will respond to probe attempts. GPIOs will be handled in the next patch.

Without specific knowledge of each component's resource names or power sequencing requirements, the prober can only enable the regulator supplies all at once, and toggle the GPIOs all at once. Luckily, reset pins tend to be active low, while enable pins tend to be active high, so setting the raw status of all GPIO pins to high should work. The wait time before and after resources are enabled are collected from existing drivers and device trees.

The prober collects resources from all possible components and enables them together, instead of enabling resources and probing each component one by one. The latter approach does not provide any boot time benefits over simply enabling each component and letting each driver probe sequentially.

The prober will also deduplicate the resources, since on a component swap out or co-layout design, the resources are always the same. While duplicate regulator supplies won't cause much issue, shared GPIOs don't work reliably, especially with other drivers. For the same reason, the prober will release the GPIOs before the successfully probed component is actually enabled.

...

Quoted 7 lines

/*

* address responds.
*
* TODO:
- * - Support handling common regulators and GPIOs.
+ * - Support handling common GPIOs.

You can split this to two lines in the first place and have less churn in this patch and the other one.

Quoted 2 lines

* - Support I2C muxes
*/

..

drivers/i2c/i2c-core-of-prober.c line 50

Quoted 10 lines
drivers/i2c/i2c-core-of-prober.c +10 -0
+/* Returns number of regulator supplies found for node, or error. */
+static int i2c_of_probe_get_regulator(struct device *dev, struct device_node *node,
+				      struct i2c_of_probe_data *data)
+{
+	struct regulator_bulk_data *tmp, *new_regulators;
+	int ret;
+
+	ret = of_regulator_bulk_get_all(dev, node, &tmp);
+	if (ret <= 0)
+		return ret;

I would split this and explain 0 case.

drivers/i2c/i2c-core-of-prober.c line 59

Quoted 8 lines
drivers/i2c/i2c-core-of-prober.c +8 -0
+	if (!data->regulators) {
+		data->regulators = tmp;
+		data->regulators_num = ret;
+		return ret;
+	};
+
+	new_regulators = krealloc(data->regulators,
+				  sizeof(*tmp) * (data->regulators_num + ret),

krealloc_array()

drivers/i2c/i2c-core-of-prober.c line 69

Quoted 10 lines
drivers/i2c/i2c-core-of-prober.c +9 -0
+				  GFP_KERNEL);
+	if (!new_regulators) {
+		regulator_bulk_free(ret, tmp);
+		return -ENOMEM;
+	}
+
+	data->regulators = new_regulators;
 
+	for (unsigned int i = 0; i < ret; i++)
+		memcpy(&data->regulators[data->regulators_num++], &tmp[i], sizeof(*tmp));

Seems like copying array to array, no? If so, can't be done in a single memcpy() call?

Quoted 2 lines
(quoted diff) +2 -0
+	return ret;
+}

...

Quoted 8 lines
drivers/i2c/i2c-core-of-prober.c +8 -0
+static int i2c_of_probe_get_res(struct device *dev, struct device_node *node,
+				struct i2c_of_probe_data *data)
+{
+	struct property *prop;
+	int ret;
+
+	ret = i2c_of_probe_get_regulator(dev, node, data);
+	if (ret < 0) {

drivers/i2c/i2c-core-of-prober.c line 96

Quoted 10 lines
drivers/i2c/i2c-core-of-prober.c +10 -0
+		dev_err_probe(dev, ret, "Failed to get regulator supplies from %pOF\n", node);
+		goto err_cleanup;
+	}
+
+	return 0;
+
+err_cleanup:
+	i2c_of_probe_free_res(data);
+	return ret;
+}

Hmm... why not

static int i2c_of_probe_get_res(struct device *dev, struct device_node *node,
struct i2c_of_probe_data *data)

{
	struct property *prop;
	int ret;

	ret = i2c_of_probe_get_regulator(dev, node, data);
	if (ret < 0) {
		i2c_of_probe_free_res(data);
		return dev_err_probe(dev, ret, "Failed to get regulator supplies from %pOF\n", node);
	}

	return 0;
}

...

drivers/i2c/i2c-core-of-prober.c line 100

Quoted 3 lines
drivers/i2c/i2c-core-of-prober.c +3 -0
+static int i2c_of_probe_enable_res(struct device *dev, struct i2c_of_probe_data *data)
+{
+	int ret = 0;

Redundant assignment.

drivers/i2c/i2c-core-of-prober.c line 109

Quoted 8 lines
drivers/i2c/i2c-core-of-prober.c +8 -0
+	dev_dbg(dev, "Enabling regulator supplies\n");
+
+	ret = regulator_bulk_enable(data->regulators_num, data->regulators);
+	if (ret)
+		return ret;
+
+	/* largest post-power-on pre-reset-deassert delay seen among drivers */
+	msleep(500);

How would we monitor if any [new] driver wants to use bigger timeout?

Quoted 2 lines
(quoted diff) +2 -0
+	return 0;
+}

...

drivers/i2c/i2c-core-of-prober.c static int i2c_of_probe_enable_node(struct device *dev, struct device_node *node · line 198

Quoted 2 lines
drivers/i2c/i2c-core-of-prober.c +1 -0
 	struct i2c_adapter *i2c;
+	struct i2c_of_probe_data probe_data = {0};

Reversed xmas tree order?

'0' is not needed.

...

drivers/i2c/i2c-core-of-prober.c int i2c_of_probe_component(struct device *dev, const char *type) · line 227

Quoted 6 lines
drivers/i2c/i2c-core-of-prober.c +6 -0
+	/* Grab resources */
+	for_each_child_of_node_scoped(i2c_node, node) {
+		u32 addr;
+
+		if (!of_node_name_prefix(node, type))
+			continue;

Is it third or fourth copy of this code? At some point you probably want

#define for_each_child_of_node_with_prefix_scoped()
	for_each_if(...)

(or equivalent)

Quoted 8 lines
drivers/i2c/i2c-core-of-prober.c +8 -0
+		if (of_property_read_u32(node, "reg", &addr))
+			continue;
+
+		dev_dbg(dev, "Requesting resources for %pOF\n", node);
+		ret = i2c_of_probe_get_res(dev, node, &probe_data);
+		if (ret)
+			return ret;
+	}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#27 lore raw

Re: [PATCH v5 07/10] i2c: of-prober: Add regulator support

Quoted 33 lines · Andy Shevchenko
Quoted 26 lines · Chen-Yu Tsai

This adds regulator management to the I2C OF component prober. Components that the prober intends to probe likely require their regulator supplies be enabled, and GPIOs be toggled to enable them or bring them out of reset before they will respond to probe attempts. GPIOs will be handled in the next patch.

Without specific knowledge of each component's resource names or power sequencing requirements, the prober can only enable the regulator supplies all at once, and toggle the GPIOs all at once. Luckily, reset pins tend to be active low, while enable pins tend to be active high, so setting the raw status of all GPIO pins to high should work. The wait time before and after resources are enabled are collected from existing drivers and device trees.

The prober collects resources from all possible components and enables them together, instead of enabling resources and probing each component one by one. The latter approach does not provide any boot time benefits over simply enabling each component and letting each driver probe sequentially.

The prober will also deduplicate the resources, since on a component swap out or co-layout design, the resources are always the same. While duplicate regulator supplies won't cause much issue, shared GPIOs don't work reliably, especially with other drivers. For the same reason, the prober will release the GPIOs before the successfully probed component is actually enabled.

...

Quoted 1 line

/*

Quoted 8 lines
Quoted 5 lines

* address responds.
*
* TODO:
- * - Support handling common regulators and GPIOs.
+ * - Support handling common GPIOs.

You can split this to two lines in the first place and have less churn in this patch and the other one.

Ack.

Quoted 5 lines
Quoted 2 lines

* - Support I2C muxes
*/

..

Quoted 12 lines

drivers/i2c/i2c-core-of-prober.c line 50

Quoted 10 lines
drivers/i2c/i2c-core-of-prober.c +10 -0
+/* Returns number of regulator supplies found for node, or error. */
+static int i2c_of_probe_get_regulator(struct device *dev, struct device_node *node,
+                                   struct i2c_of_probe_data *data)
+{
+     struct regulator_bulk_data *tmp, *new_regulators;
+     int ret;
+
+     ret = of_regulator_bulk_get_all(dev, node, &tmp);
+     if (ret <= 0)
+             return ret;

I would split this and explain 0 case.

Ack.

Quoted 10 lines

drivers/i2c/i2c-core-of-prober.c line 59

Quoted 8 lines
drivers/i2c/i2c-core-of-prober.c +8 -0
+     if (!data->regulators) {
+             data->regulators = tmp;
+             data->regulators_num = ret;
+             return ret;
+     };
+
+     new_regulators = krealloc(data->regulators,
+                               sizeof(*tmp) * (data->regulators_num + ret),

krealloc_array()

Ack. Somehow I didn't find this function while I was rewriting the code.

Quoted 8 lines
Quoted 7 lines
drivers/i2c/i2c-core-of-prober.c +7 -0
+                               GFP_KERNEL);
+     if (!new_regulators) {
+             regulator_bulk_free(ret, tmp);
+             return -ENOMEM;
+     }
+
+     data->regulators = new_regulators;
Quoted 4 lines

drivers/i2c/i2c-core-of-prober.c line 69

Quoted 2 lines
drivers/i2c/i2c-core-of-prober.c +2 -0
+     for (unsigned int i = 0; i < ret; i++)
+             memcpy(&data->regulators[data->regulators_num++], &tmp[i], sizeof(*tmp));

Seems like copying array to array, no? If so, can't be done in a single memcpy() call?

Ack.

Quoted 41 lines
Quoted 2 lines
(quoted diff) +2 -0
+     return ret;
+}

...

Quoted 8 lines
drivers/i2c/i2c-core-of-prober.c +8 -0
+static int i2c_of_probe_get_res(struct device *dev, struct device_node *node,
+                             struct i2c_of_probe_data *data)
+{
+     struct property *prop;
+     int ret;
+
+     ret = i2c_of_probe_get_regulator(dev, node, data);
+     if (ret < 0) {

drivers/i2c/i2c-core-of-prober.c line 96

Quoted 10 lines
drivers/i2c/i2c-core-of-prober.c +10 -0
+             dev_err_probe(dev, ret, "Failed to get regulator supplies from %pOF\n", node);
+             goto err_cleanup;
+     }
+
+     return 0;
+
+err_cleanup:
+     i2c_of_probe_free_res(data);
+     return ret;
+}

Hmm... why not

static int i2c_of_probe_get_res(struct device *dev, struct device_node *node,
struct i2c_of_probe_data *data)

{
        struct property *prop;
        int ret;

        ret = i2c_of_probe_get_regulator(dev, node, data);
        if (ret < 0) {
                i2c_of_probe_free_res(data);
                return dev_err_probe(dev, ret, "Failed to get regulator supplies from %pOF\n", node);
        }

        return 0;
}
Quoted 1 line

...

That would be more churn in the next patch, which introduces another error condition requiring the same cleanup.

Quoted 5 lines

drivers/i2c/i2c-core-of-prober.c line 100

Quoted 3 lines
drivers/i2c/i2c-core-of-prober.c +3 -0
+static int i2c_of_probe_enable_res(struct device *dev, struct i2c_of_probe_data *data)
+{
+     int ret = 0;

Redundant assignment.

Ack.

Quoted 10 lines

drivers/i2c/i2c-core-of-prober.c line 109

Quoted 8 lines
drivers/i2c/i2c-core-of-prober.c +8 -0
+     dev_dbg(dev, "Enabling regulator supplies\n");
+
+     ret = regulator_bulk_enable(data->regulators_num, data->regulators);
+     if (ret)
+             return ret;
+
+     /* largest post-power-on pre-reset-deassert delay seen among drivers */
+     msleep(500);

How would we monitor if any [new] driver wants to use bigger timeout?

The assumption is that the person doing the integration should test for this. This prober doesn't get called everywhere. It needs a driver to call it, and that driver is written by someone for some specific platform. Maybe I should explicitly spell that out in the function description? Or even make it a parameter?

Also, having an arbitrarily large number here doesn't help platforms that want to minimize boot time. On that front I'm also thinking about whether it is possible to do a handover to the actual driver so that the latter doesn't have to go through the whole power sequence again.

Quoted 9 lines
Quoted 2 lines
(quoted diff) +2 -0
+     return 0;
+}

...

Quoted 2 lines
      struct i2c_adapter *i2c;

+ struct i2c_of_probe_data probe_data = {0};

Reversed xmas tree order?

OK...

Quoted 1 line

'0' is not needed.

Ack.

Quoted 9 lines

...

drivers/i2c/i2c-core-of-prober.c int i2c_of_probe_component(struct device *dev, const char *type) · line 227

Quoted 6 lines
drivers/i2c/i2c-core-of-prober.c +6 -0
+     /* Grab resources */
+     for_each_child_of_node_scoped(i2c_node, node) {
+             u32 addr;
+
+             if (!of_node_name_prefix(node, type))
+                     continue;
Quoted 6 lines

Is it third or fourth copy of this code? At some point you probably want

#define for_each_child_of_node_with_prefix_scoped()
        for_each_if(...)

(or equivalent)

Ack.

Thank you for the review.

ChenYu

Quoted 14 lines

drivers/i2c/i2c-core-of-prober.c int i2c_of_probe_component(struct device *dev, const char *type) · line 235

Quoted 8 lines
drivers/i2c/i2c-core-of-prober.c +8 -0
+             if (of_property_read_u32(node, "reg", &addr))
+                     continue;
+
+             dev_dbg(dev, "Requesting resources for %pOF\n", node);
+             ret = i2c_of_probe_get_res(dev, node, &probe_data);
+             if (ret)
+                     return ret;
+     }

--
With Best Regards,
Andy Shevchenko

#28 lore raw

Re: [PATCH v5 07/10] i2c: of-prober: Add regulator support

Quoted 3 lines · Chen-Yu Tsai
Quoted 1 line · Andy Shevchenko

On Thu, Aug 22, 2024 at 05:20:00PM +0800, Chen-Yu Tsai wrote:

...

Quoted 14 lines
Quoted 14 lines

Hmm... why not

static int i2c_of_probe_get_res(struct device *dev, struct device_node *node,
struct i2c_of_probe_data *data)

{
        struct property *prop;
        int ret;

        ret = i2c_of_probe_get_regulator(dev, node, data);
        if (ret < 0) {
                i2c_of_probe_free_res(data);
                return dev_err_probe(dev, ret, "Failed to get regulator supplies from %pOF\n", node);
        }
Quoted 5 lines
Quoted 2 lines
        return 0;
}

That would be more churn in the next patch, which introduces another error condition requiring the same cleanup.

OK!

...

Quoted 11 lines
Quoted 4 lines

drivers/i2c/i2c-core-of-prober.c line 109

Quoted 2 lines
drivers/i2c/i2c-core-of-prober.c +2 -0
+     /* largest post-power-on pre-reset-deassert delay seen among drivers */
+     msleep(500);

How would we monitor if any [new] driver wants to use bigger timeout?

The assumption is that the person doing the integration should test for this. This prober doesn't get called everywhere. It needs a driver to call it, and that driver is written by someone for some specific platform. Maybe I should explicitly spell that out in the function description? Or even make it a parameter?

Quoted 4 lines

Also, having an arbitrarily large number here doesn't help platforms that want to minimize boot time. On that front I'm also thinking about whether it is possible to do a handover to the actual driver so that the latter doesn't have to go through the whole power sequence again.

Yeah, I think the best effort is to have a parameter.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#29 lore raw

PATCH v5 8/10 i2c: of-prober: Add GPIO support

In reply to Chen-Yu Tsai

This adds GPIO management to the I2C OF component prober. Components that the prober intends to probe likely require their regulator supplies be enabled, and GPIOs be toggled to enable them or bring them out of reset before they will respond to probe attempts. regulator support was added in the previous patch.

Without specific knowledge of each component's resource names or power sequencing requirements, the prober can only enable the regulator supplies all at once, and toggle the GPIOs all at once. Luckily, reset pins tend to be active low, while enable pins tend to be active high, so setting the raw status of all GPIO pins to high should work. The wait time before and after resources are enabled are collected from existing drivers and device trees.

The prober collects resources from all possible components and enables them together, instead of enabling resources and probing each component one by one. The latter approach does not provide any boot time benefits over simply enabling each component and letting each driver probe sequentially.

The prober will also deduplicate the resources, since on a component swap out or co-layout design, the resources are always the same. While duplicate regulator supplies won't cause much issue, shared GPIOs don't work reliably, especially with other drivers. For the same reason, the prober will release the GPIOs before the successfully probed component is actually enabled.

---
Changes since v4:
- Split out from previous patch
- Moved GPIO property name check to common function in gpiolib.c in new patch
- Moved i2c_of_probe_free_gpios() into for_each_child_of_node_scoped()
- Rewrote in gpiod_*_array-esque fashion

---
 drivers/i2c/i2c-core-of-prober.c | 126 ++++++++++++++++++++++++++++++-
 1 file changed, 124 insertions(+), 2 deletions(-)
drivers/i2c/i2c-core-of-prober.c +124 -2
index 32184cfd10f6..046e6605053c 100644
@@ -10,6 +10,7 @@
 #include <linux/device.h>
 #include <linux/dev_printk.h>
 #include <linux/err.h>
+#include <linux/gpio/consumer.h>
 #include <linux/i2c.h>
 #include <linux/module.h>
 #include <linux/of.h>
@@ -29,11 +30,11 @@
  * address responds.
  *
  * TODO:
- * - Support handling common GPIOs.
  * - Support I2C muxes
  */
 
 struct i2c_of_probe_data {
+	struct gpio_descs *gpiods;
 	struct regulator_bulk_data *regulators;
 	unsigned int regulators_num;
 };
@@ -71,8 +72,88 @@ static int i2c_of_probe_get_regulator(struct device *dev, struct device_node *no
 	return ret;
 }
 
+/*
+ * Returns 1 if property is GPIO and GPIO successfully requested,
+ * 0 if not a GPIO property, or error if request for GPIO failed.
+ */
+static int i2c_of_probe_get_gpiod(struct device_node *node, struct property *prop,
+				  struct i2c_of_probe_data *data)
+{
+	struct fwnode_handle *fwnode = of_fwnode_handle(node);
+	struct gpio_descs *gpiods;
+	struct gpio_desc *gpiod;
+	char con[32]; /* 32 is max size of property name */
+	char *con_id = NULL;
+	size_t new_size;
+	int len;
+
+	len = gpio_property_name_length(prop->name);
+	if (len < 0)
+		return 0;
+
+	if (len >= sizeof(con) - 1) {
+		pr_err("%pOF: length of GPIO name \"%s\" exceeds current limit\n",
+		       node, prop->name);
+		return -EINVAL;
+	}
+
+	if (len > 0) {
+		strscpy(con, prop->name, len + 1);
+		con_id = con;
+	}
+
+	/*
+	 * GPIO descriptors are not reference counted. GPIOD_FLAGS_BIT_NONEXCLUSIVE
+	 * can't differentiate between GPIOs shared between devices to be probed and
+	 * other devices (which is incorrect). If the initial request fails with
+	 * -EBUSY, retry with GPIOD_FLAGS_BIT_NONEXCLUSIVE and see if it matches
+	 * any existing ones.
+	 */
+	gpiod = fwnode_gpiod_get_index(fwnode, con_id, 0, GPIOD_ASIS, "i2c-of-prober");
+	if (IS_ERR(gpiod)) {
+		int i;
+
+		if (PTR_ERR(gpiod) != -EBUSY || !data->gpiods)
+			return PTR_ERR(gpiod);
+
+		gpiod = fwnode_gpiod_get_index(fwnode, con_id, 0,
+					       GPIOD_ASIS | GPIOD_FLAGS_BIT_NONEXCLUSIVE,
+					       "i2c-of-prober");
+		for (i = 0; i < data->gpiods->ndescs; i++)
+			if (gpiod == data->gpiods->desc[i])
+				return 1;
+
+		return -EBUSY;
+	}
+
+	new_size = struct_size(gpiods, desc, data->gpiods ? data->gpiods->ndescs + 1 : 1);
+	gpiods = krealloc(data->gpiods, new_size, GFP_KERNEL);
+	if (!gpiods) {
+		gpiod_put(gpiod);
+		return -ENOMEM;
+	}
+
+	data->gpiods = gpiods;
+	data->gpiods->desc[data->gpiods->ndescs++] = gpiod;
+
+	return 1;
+}
+
+/*
+ * This is split into two functions because in the normal flow the GPIOs
+ * have to be released before the actual driver probes so that the latter
+ * can acquire them.
+ */
+static void i2c_of_probe_free_gpios(struct i2c_of_probe_data *data)
+{
+	if (data->gpiods)
+		gpiod_put_array(data->gpiods);
+	data->gpiods = NULL;
+}
+
 static void i2c_of_probe_free_res(struct i2c_of_probe_data *data)
 {
+	i2c_of_probe_free_gpios(data);
 	regulator_bulk_free(data->regulators_num, data->regulators);
 }
 
@@ -88,6 +169,18 @@ static int i2c_of_probe_get_res(struct device *dev, struct device_node *node,
 		goto err_cleanup;
 	}
 
+	for_each_property_of_node(node, prop) {
+		dev_dbg(dev, "Trying property %pOF/%s\n", node, prop->name);
+
+		/* GPIOs */
+		ret = i2c_of_probe_get_gpiod(node, prop, data);
+		if (ret < 0) {
+			dev_err_probe(dev, ret, "Failed to get GPIO from %pOF/%s\n",
+				      node, prop->name);
+			goto err_cleanup;
+		}
+	}
+
 	return 0;
 
 err_cleanup:
@@ -98,6 +191,7 @@ static int i2c_of_probe_get_res(struct device *dev, struct device_node *node,
 static int i2c_of_probe_enable_res(struct device *dev, struct i2c_of_probe_data *data)
 {
 	int ret = 0;
+	int gpio_i;
 
 	dev_dbg(dev, "Enabling regulator supplies\n");
 
@@ -108,7 +202,32 @@ static int i2c_of_probe_enable_res(struct device *dev, struct i2c_of_probe_data
 	/* largest post-power-on pre-reset-deassert delay seen among drivers */
 	msleep(500);
 
+	if (!data->gpiods)
+		return 0;
+
+	for (gpio_i = 0; gpio_i < data->gpiods->ndescs; gpio_i++) {
+		/*
+		 * reset GPIOs normally have opposite polarity compared to
+		 * enable GPIOs. Instead of parsing the flags again, simply
+		 * set the raw value to high.
+		 */
+		dev_dbg(dev, "Setting GPIO %d\n", gpio_i);
+		ret = gpiod_direction_output_raw(data->gpiods->desc[gpio_i], 1);
+		if (ret)
+			goto disable_gpios;
+	}
+
+	/* largest post-reset-deassert delay seen in tree for Elan I2C HID */
+	msleep(300);
+
 	return 0;
+
+disable_gpios:
+	for (gpio_i--; gpio_i >= 0; gpio_i--)
+		gpiod_set_raw_value_cansleep(data->gpiods->desc[gpio_i], 0);
+	regulator_bulk_disable(data->regulators_num, data->regulators);
+
+	return ret;
 }
 
 static void i2c_of_probe_disable_regulators(struct i2c_of_probe_data *data)
@@ -234,7 +353,9 @@ int i2c_of_probe_component(struct device *dev, const char *type)
 			return ret;
 	}
 
-	dev_dbg(dev, "Resources: # of regulator supplies = %d\n", probe_data.regulators_num);
+	dev_dbg(dev, "Resources: # of GPIOs = %d, # of regulator supplies = %d\n",
+		probe_data.gpiods ? probe_data.gpiods->ndescs : 0,
+		probe_data.regulators_num);
 
 	/* Enable resources */
 	ret = i2c_of_probe_enable_res(dev, &probe_data);
@@ -256,6 +377,7 @@ int i2c_of_probe_component(struct device *dev, const char *type)
 			continue;
 
 		/* Found a device that is responding */
+		i2c_of_probe_free_gpios(&probe_data);
 		ret = i2c_of_probe_enable_node(dev, node);
 		break;
 	}
Signature · 1 line
2.46.0.184.g6999bdac58-goog
#30 lore raw

Re: [PATCH v5 08/10] i2c: of-prober: Add GPIO support

Quoted 26 lines · Chen-Yu Tsai

This adds GPIO management to the I2C OF component prober. Components that the prober intends to probe likely require their regulator supplies be enabled, and GPIOs be toggled to enable them or bring them out of reset before they will respond to probe attempts. regulator support was added in the previous patch.

Without specific knowledge of each component's resource names or power sequencing requirements, the prober can only enable the regulator supplies all at once, and toggle the GPIOs all at once. Luckily, reset pins tend to be active low, while enable pins tend to be active high, so setting the raw status of all GPIO pins to high should work. The wait time before and after resources are enabled are collected from existing drivers and device trees.

The prober collects resources from all possible components and enables them together, instead of enabling resources and probing each component one by one. The latter approach does not provide any boot time benefits over simply enabling each component and letting each driver probe sequentially.

The prober will also deduplicate the resources, since on a component swap out or co-layout design, the resources are always the same. While duplicate regulator supplies won't cause much issue, shared GPIOs don't work reliably, especially with other drivers. For the same reason, the prober will release the GPIOs before the successfully probed component is actually enabled.

...

drivers/i2c/i2c-core-of-prober.c static int i2c_of_probe_get_regulator(struct device *dev, struct device_node *no · line 85

Quoted 4 lines
drivers/i2c/i2c-core-of-prober.c +4 -0
+	struct fwnode_handle *fwnode = of_fwnode_handle(node);
+	struct gpio_descs *gpiods;
+	struct gpio_desc *gpiod;
+	char con[32]; /* 32 is max size of property name */

Use 'propname' to be aligned with GPIO library usages.

Quoted 3 lines
drivers/i2c/i2c-core-of-prober.c +3 -0
+	char *con_id = NULL;
+	size_t new_size;
+	int len;

...

drivers/i2c/i2c-core-of-prober.c static int i2c_of_probe_get_regulator(struct device *dev, struct device_node *no · line 94

Quoted 1 line
drivers/i2c/i2c-core-of-prober.c +1 -0
+	if (len >= sizeof(con) - 1) {

This can be transformed to check the returned value from strscpy().

drivers/i2c/i2c-core-of-prober.c static int i2c_of_probe_get_regulator(struct device *dev, struct device_node *no · line 101

Quoted 7 lines
drivers/i2c/i2c-core-of-prober.c +7 -0
+		pr_err("%pOF: length of GPIO name \"%s\" exceeds current limit\n",
+		       node, prop->name);
+		return -EINVAL;
+	}
+
+	if (len > 0) {
+		strscpy(con, prop->name, len + 1);

The correct (robust) call is with destination size. Which means here that you may use 2-argument strscpy().

Quoted 2 lines
(quoted diff) +2 -0
+		con_id = con;
+	}

...

drivers/i2c/i2c-core-of-prober.c static int i2c_of_probe_enable_res(struct device *dev, struct i2c_of_probe_data · line 206

Quoted 2 lines
drivers/i2c/i2c-core-of-prober.c +2 -0
+	if (!data->gpiods)
+		return 0;

If it comes a new code (something else besides GPIOs and regulators) this will be a (small) impediment. Better to have a helper for each case and do

	ret = ..._gpiods();
	if (ret)
		...

Same for regulators and anything else in the future, if any.

drivers/i2c/i2c-core-of-prober.c static int i2c_of_probe_enable_res(struct device *dev, struct i2c_of_probe_data · line 210

Quoted 2 lines
drivers/i2c/i2c-core-of-prober.c +2 -0
+		/*
+		 * reset GPIOs normally have opposite polarity compared to

"reset"

drivers/i2c/i2c-core-of-prober.c static int i2c_of_probe_enable_res(struct device *dev, struct i2c_of_probe_data · line 211

Quoted 1 line
drivers/i2c/i2c-core-of-prober.c +1 -0
+		 * enable GPIOs. Instead of parsing the flags again, simply

"enable"

drivers/i2c/i2c-core-of-prober.c static int i2c_of_probe_enable_res(struct device *dev, struct i2c_of_probe_data · line 212

Quoted 1 line
drivers/i2c/i2c-core-of-prober.c +1 -0
+		 * set the raw value to high.

This is quite a fragile assumption. Yes, it would work in 98% cases, but will break if it's not true somewhere else.

Quoted 1 line
(quoted diff) +1 -0
+		 */

...

drivers/i2c/i2c-core-of-prober.c static int i2c_of_probe_enable_res(struct device *dev, struct i2c_of_probe_data · line 221

Quoted 2 lines
drivers/i2c/i2c-core-of-prober.c +2 -0
+	/* largest post-reset-deassert delay seen in tree for Elan I2C HID */
+	msleep(300);

Same Q, how do you monitor _all_ the drivers?

...

drivers/i2c/i2c-core-of-prober.c static int i2c_of_probe_enable_res(struct device *dev, struct i2c_of_probe_data · line 227

Quoted 3 lines
drivers/i2c/i2c-core-of-prober.c +3 -0
+disable_gpios:
+	for (gpio_i--; gpio_i >= 0; gpio_i--)
+		gpiod_set_raw_value_cansleep(data->gpiods->desc[gpio_i], 0);

Can't you call the _array() variant here?

...

drivers/i2c/i2c-core-of-prober.c int i2c_of_probe_component(struct device *dev, const char *type) · line 358

Quoted 4 lines
drivers/i2c/i2c-core-of-prober.c +3 -1
-	dev_dbg(dev, "Resources: # of regulator supplies = %d\n", probe_data.regulators_num);
+	dev_dbg(dev, "Resources: # of GPIOs = %d, # of regulator supplies = %d\n",
+		probe_data.gpiods ? probe_data.gpiods->ndescs : 0,
+		probe_data.regulators_num);

I would issue one message per class of the devices (GPIOs, regulators, ...)

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#31 lore raw

Re: [PATCH v5 08/10] i2c: of-prober: Add GPIO support

Quoted 29 lines · Andy Shevchenko
Quoted 20 lines · Chen-Yu Tsai

This adds GPIO management to the I2C OF component prober. Components that the prober intends to probe likely require their regulator supplies be enabled, and GPIOs be toggled to enable them or bring them out of reset before they will respond to probe attempts. regulator support was added in the previous patch.

Without specific knowledge of each component's resource names or power sequencing requirements, the prober can only enable the regulator supplies all at once, and toggle the GPIOs all at once. Luckily, reset pins tend to be active low, while enable pins tend to be active high, so setting the raw status of all GPIO pins to high should work. The wait time before and after resources are enabled are collected from existing drivers and device trees.

The prober collects resources from all possible components and enables them together, instead of enabling resources and probing each component one by one. The latter approach does not provide any boot time benefits over simply enabling each component and letting each driver probe sequentially.

Quoted 6 lines

The prober will also deduplicate the resources, since on a component swap out or co-layout design, the resources are always the same. While duplicate regulator supplies won't cause much issue, shared GPIOs don't work reliably, especially with other drivers. For the same reason, the prober will release the GPIOs before the successfully probed component is actually enabled.

Quoted 8 lines

...

drivers/i2c/i2c-core-of-prober.c static int i2c_of_probe_get_regulator(struct device *dev, struct device_node *no · line 85

Quoted 4 lines
drivers/i2c/i2c-core-of-prober.c +4 -0
+     struct fwnode_handle *fwnode = of_fwnode_handle(node);
+     struct gpio_descs *gpiods;
+     struct gpio_desc *gpiod;
+     char con[32]; /* 32 is max size of property name */

Use 'propname' to be aligned with GPIO library usages.

Ack.

Quoted 9 lines
Quoted 3 lines
drivers/i2c/i2c-core-of-prober.c +3 -0
+     char *con_id = NULL;
+     size_t new_size;
+     int len;

...

Quoted 1 line

+ if (len >= sizeof(con) - 1) {

This can be transformed to check the returned value from strscpy().

Ack.

Quoted 10 lines

drivers/i2c/i2c-core-of-prober.c static int i2c_of_probe_get_regulator(struct device *dev, struct device_node *no · line 101

Quoted 7 lines
drivers/i2c/i2c-core-of-prober.c +7 -0
+             pr_err("%pOF: length of GPIO name \"%s\" exceeds current limit\n",
+                    node, prop->name);
+             return -EINVAL;
+     }
+
+     if (len > 0) {
+             strscpy(con, prop->name, len + 1);

The correct (robust) call is with destination size. Which means here that you may use 2-argument strscpy().

Ack.

Quoted 8 lines
Quoted 2 lines
(quoted diff) +2 -0
+             con_id = con;
+     }

...

drivers/i2c/i2c-core-of-prober.c static int i2c_of_probe_enable_res(struct device *dev, struct i2c_of_probe_data · line 206

Quoted 2 lines
drivers/i2c/i2c-core-of-prober.c +2 -0
+     if (!data->gpiods)
+             return 0;
Quoted 7 lines

If it comes a new code (something else besides GPIOs and regulators) this will be a (small) impediment. Better to have a helper for each case and do

        ret = ..._gpiods();
        if (ret)
                ...

Same for regulators and anything else in the future, if any.

I'm not sure I follow. Do you mean wrap each individual type in a wrapper and call those here, like the following?

    i2c_of_probe_enable_res(...)
    {
        ret = i2c_of_probe_enable_regulators(...)
        if (ret)
              return ret;

        ret = i2c_of_probe_enable_gpios(...)
        if (ret)
              goto error_disable_regulators;

        ...
    }
Quoted 5 lines

drivers/i2c/i2c-core-of-prober.c static int i2c_of_probe_enable_res(struct device *dev, struct i2c_of_probe_data · line 210

Quoted 2 lines
drivers/i2c/i2c-core-of-prober.c +2 -0
+             /*
+              * reset GPIOs normally have opposite polarity compared to

"reset"

Quoted 8 lines
Quoted 1 line

+ * enable GPIOs. Instead of parsing the flags again, simply

"enable"

Quoted 1 line

+ * set the raw value to high.

This is quite a fragile assumption. Yes, it would work in 98% cases, but will break if it's not true somewhere else.

Well, this seems to be the de facto standard. Or it would have to remember what each GPIO descriptor's name is, and try to classify those into either "enable" or "reset", and set their respective logical values to 1 or 0. And then you run into a peripheral with a broken binding that has its "reset" GPIO inverted, i.e. it's driver behavior needs to follow the "enable" GPIO style. The class of devices this prober targets are consumer electronics (laptops, tablets, phones) that at least have gone through some component selection where the options won't have conflicting requirements.

And if the polarities of the possible components don't line up, then this probe structure can't really do anything. One would need something that power sequences each component separately and probes it. I would really like to avoid that if possible, as it makes the boot time (to peripheral available) dependent on which component you have and how far down the list it is. We have Chromebooks that have 4 touchscreen components introduced over the years. In that case something more like Doug's original proposal would work better: something that forces mutual exclusivity among a class of devices.

Quoted 8 lines
Quoted 1 line

+ */

...

drivers/i2c/i2c-core-of-prober.c static int i2c_of_probe_enable_res(struct device *dev, struct i2c_of_probe_data · line 221

Quoted 2 lines
drivers/i2c/i2c-core-of-prober.c +2 -0
+     /* largest post-reset-deassert delay seen in tree for Elan I2C HID */
+     msleep(300);

Same Q, how do you monitor _all_ the drivers?

Discussion in the previous patch.

Quoted 7 lines

...

drivers/i2c/i2c-core-of-prober.c static int i2c_of_probe_enable_res(struct device *dev, struct i2c_of_probe_data · line 227

Quoted 3 lines
drivers/i2c/i2c-core-of-prober.c +3 -0
+disable_gpios:
+     for (gpio_i--; gpio_i >= 0; gpio_i--)
+             gpiod_set_raw_value_cansleep(data->gpiods->desc[gpio_i], 0);

Can't you call the _array() variant here?

I thought that without |struct gpio_array| the _array() variant wouldn't help, i.e. it would still be a loop internally. Looks like I was wrong.

Quoted 8 lines

...

drivers/i2c/i2c-core-of-prober.c int i2c_of_probe_component(struct device *dev, const char *type) · line 358

Quoted 4 lines
drivers/i2c/i2c-core-of-prober.c +3 -1
-     dev_dbg(dev, "Resources: # of regulator supplies = %d\n", probe_data.regulators_num);
+     dev_dbg(dev, "Resources: # of GPIOs = %d, # of regulator supplies = %d\n",
+             probe_data.gpiods ? probe_data.gpiods->ndescs : 0,
+             probe_data.regulators_num);

I would issue one message per class of the devices (GPIOs, regulators, ...)

Ack.

Thank you for the review.
ChenYu

#32 lore raw

Re: [PATCH v5 08/10] i2c: of-prober: Add GPIO support

Quoted 3 lines · Chen-Yu Tsai
Quoted 1 line · Andy Shevchenko

On Thu, Aug 22, 2024 at 05:20:01PM +0800, Chen-Yu Tsai wrote:

...

Quoted 15 lines
Quoted 11 lines

drivers/i2c/i2c-core-of-prober.c static int i2c_of_probe_enable_res(struct device *dev, struct i2c_of_probe_data · line 206

Quoted 2 lines
drivers/i2c/i2c-core-of-prober.c +2 -0
+     if (!data->gpiods)
+             return 0;

If it comes a new code (something else besides GPIOs and regulators) this will be a (small) impediment. Better to have a helper for each case and do

        ret = ..._gpiods();
        if (ret)
                ...

Same for regulators and anything else in the future, if any.

I'm not sure I follow. Do you mean wrap each individual type in a wrapper and call those here, like the following?

Quoted 12 lines
    i2c_of_probe_enable_res(...)
    {
        ret = i2c_of_probe_enable_regulators(...)
        if (ret)
              return ret;

        ret = i2c_of_probe_enable_gpios(...)
        if (ret)
              goto error_disable_regulators;

        ...
    }

Yes.

...

Quoted 14 lines
Quoted 5 lines

drivers/i2c/i2c-core-of-prober.c static int i2c_of_probe_enable_res(struct device *dev, struct i2c_of_probe_data · line 210

Quoted 2 lines
drivers/i2c/i2c-core-of-prober.c +2 -0
+             /*
+              * reset GPIOs normally have opposite polarity compared to

"reset"

Quoted 8 lines
Quoted 1 line

+ * enable GPIOs. Instead of parsing the flags again, simply

"enable"

Quoted 1 line

+ * set the raw value to high.

This is quite a fragile assumption. Yes, it would work in 98% cases, but will break if it's not true somewhere else.

Quoted 9 lines

Well, this seems to be the de facto standard. Or it would have to remember what each GPIO descriptor's name is, and try to classify those into either "enable" or "reset", and set their respective logical values to 1 or 0. And then you run into a peripheral with a broken binding that has its "reset" GPIO inverted, i.e. it's driver behavior needs to follow the "enable" GPIO style. The class of devices this prober targets are consumer electronics (laptops, tablets, phones) that at least have gone through some component selection where the options won't have conflicting requirements.

I'm talking from real life example(s) :-)

Recently I looked at the OV7251 sensor driver that expects "enable" GPIO while all users supply "reset"-as-"enable" with the exact trouble I described. Yet it's pure software / ABI issue in that case, but who knows what PCB engineers may come up with.

Quoted 9 lines

And if the polarities of the possible components don't line up, then this probe structure can't really do anything. One would need something that power sequences each component separately and probes it. I would really like to avoid that if possible, as it makes the boot time (to peripheral available) dependent on which component you have and how far down the list it is. We have Chromebooks that have 4 touchscreen components introduced over the years. In that case something more like Doug's original proposal would work better: something that forces mutual exclusivity among a class of devices.

Maybe. I just pointed out the potential problem.

Quoted 1 line
Quoted 1 line
Quoted 1 line

+ */

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#33 lore raw

Re: [PATCH v5 08/10] i2c: of-prober: Add GPIO support

Quoted 64 lines · Andy Shevchenko
Quoted 3 lines · Chen-Yu Tsai
Quoted 1 line · Andy Shevchenko

On Thu, Aug 22, 2024 at 05:20:01PM +0800, Chen-Yu Tsai wrote:

...

Quoted 15 lines
Quoted 11 lines

drivers/i2c/i2c-core-of-prober.c static int i2c_of_probe_enable_res(struct device *dev, struct i2c_of_probe_data · line 206

Quoted 2 lines
drivers/i2c/i2c-core-of-prober.c +2 -0
+     if (!data->gpiods)
+             return 0;

If it comes a new code (something else besides GPIOs and regulators) this will be a (small) impediment. Better to have a helper for each case and do

        ret = ..._gpiods();
        if (ret)
                ...

Same for regulators and anything else in the future, if any.

I'm not sure I follow. Do you mean wrap each individual type in a wrapper and call those here, like the following?

Quoted 12 lines
    i2c_of_probe_enable_res(...)
    {
        ret = i2c_of_probe_enable_regulators(...)
        if (ret)
              return ret;

        ret = i2c_of_probe_enable_gpios(...)
        if (ret)
              goto error_disable_regulators;

        ...
    }

Yes.

...

Quoted 14 lines
Quoted 5 lines

drivers/i2c/i2c-core-of-prober.c static int i2c_of_probe_enable_res(struct device *dev, struct i2c_of_probe_data · line 210

Quoted 2 lines
drivers/i2c/i2c-core-of-prober.c +2 -0
+             /*
+              * reset GPIOs normally have opposite polarity compared to

"reset"

Quoted 8 lines
Quoted 1 line

+ * enable GPIOs. Instead of parsing the flags again, simply

"enable"

Quoted 1 line

+ * set the raw value to high.

This is quite a fragile assumption. Yes, it would work in 98% cases, but will break if it's not true somewhere else.

Quoted 9 lines

Well, this seems to be the de facto standard. Or it would have to remember what each GPIO descriptor's name is, and try to classify those into either "enable" or "reset", and set their respective logical values to 1 or 0. And then you run into a peripheral with a broken binding that has its "reset" GPIO inverted, i.e. it's driver behavior needs to follow the "enable" GPIO style. The class of devices this prober targets are consumer electronics (laptops, tablets, phones) that at least have gone through some component selection where the options won't have conflicting requirements.

Quoted 6 lines

I'm talking from real life example(s) :-)

Recently I looked at the OV7251 sensor driver that expects "enable" GPIO while all users supply "reset"-as-"enable" with the exact trouble I described. Yet it's pure software / ABI issue in that case, but who knows what PCB engineers may come up with.

For the OV7251 case specifically, the current approach works fine, as the polarity is the same: high electric level for a working chip.

But now that you mention it, camera sensors are exactly the case I had in mind, though not the same chip. Some of the OmniVision and GalaxyCore sensors have a "shutdown" pin that is active high, i.e. high electric level shuts down the chip.

So I guess the alternative would be to remember each GPIO's name, and do the appropriate thing based on the name, and also set the logical value, not the raw value. If it says "shutdown" or "reset", set the logical value to 0; if it says "enable", set it to 1. And hopefully we don't run into a binding which has "shutdown" but is actually "enable" ...

I don't have this case and I kind of want to leave it as a TODO though. I feel like the scope of the series is expanding ever so slightly.

ChenYu

Quoted 19 lines
Quoted 9 lines

And if the polarities of the possible components don't line up, then this probe structure can't really do anything. One would need something that power sequences each component separately and probes it. I would really like to avoid that if possible, as it makes the boot time (to peripheral available) dependent on which component you have and how far down the list it is. We have Chromebooks that have 4 touchscreen components introduced over the years. In that case something more like Doug's original proposal would work better: something that forces mutual exclusivity among a class of devices.

Maybe. I just pointed out the potential problem.

Quoted 1 line
Quoted 1 line
Quoted 1 line

+ */

--
With Best Regards,
Andy Shevchenko

author maintainer
#34 lore raw

PATCH v5 9/10 platform/chrome: Introduce device tree hardware prober

In reply to Chen-Yu Tsai

Some devices are designed and manufactured with some components having multiple drop-in replacement options. These components are often connected to the mainboard via ribbon cables, having the same signals and pin assignments across all options. These may include the display panel and touchscreen on laptops and tablets, and the trackpad on laptops. Sometimes which component option is used in a particular device can be detected by some firmware provided identifier, other times that information is not available, and the kernel has to try to probe each device.

This change attempts to make the "probe each device" case cleaner. The current approach is to have all options added and enabled in the device tree. The kernel would then bind each device and run each driver's probe function. This works, but has been broken before due to the introduction of asynchronous probing, causing multiple instances requesting "shared" resources, such as pinmuxes, GPIO pins, interrupt lines, at the same time, with only one instance succeeding. Work arounds for these include moving the pinmux to the parent I2C controller, using GPIO hogs or pinmux settings to keep the GPIO pins in some fixed configuration, and requesting the interrupt line very late. Such configurations can be seen on the MT8183 Krane Chromebook tablets, and the Qualcomm sc8280xp-based Lenovo Thinkpad 13S.

Instead of this delicate dance between drivers and device tree quirks, this change introduces a simple I2C component prober. For any given class of devices on the same I2C bus, it will go through all of them, doing a simple I2C read transfer and see which one of them responds. It will then enable the device that responds.

This requires some minor modifications in the existing device tree. The status for all the device nodes for the component options must be set to "failed-needs-probe". This makes it clear that some mechanism is needed to enable one of them, and also prevents the prober and device drivers running at the same time.

---
Changes since v4:
- Fix Kconfig dependency
- Update copyright year
- Drop "linux/of.h" header
- Include "linux/errno.h"
- Move |int ret| declaration to top of block
- Return -ENODEV on no match instead of 0
- Unregister platform driver and device unconditionally after previous change

Changes since v3:
- Include linux/init.h
- Rewrite for loop in driver probe function as suggested by Andy
- Make prober driver buildable as module
- Ignore prober errors other than probe deferral

Changes since v2:
- Addressed Rob's comments
- Move remaining driver code to drivers/platform/chrome/
- Depend on rather than select CONFIG_I2C
- Copy machine check to driver init function
- Addressed Andy's comments
- Explicitly mention "device tree" or OF in driver name, description and Kconfig symbol
- Drop filename from inside the file
- Switch to passing "struct device *" to shorten lines
- Move "ret = 0" to just before for_each_child_of_node(i2c_node, node)
- Make loop variable size_t (instead of unsigned int as Andy asked)
- Use PLATFORM_DEVID_NONE instead of raw -1
- Use standard goto error path pattern in hw_prober_driver_init()

- Changes since v1:
- New patch

---
 drivers/platform/chrome/Kconfig               |  11 ++
 drivers/platform/chrome/Makefile              |   1 +
 .../platform/chrome/chromeos_of_hw_prober.c   | 104 ++++++++++++++++++
 3 files changed, 116 insertions(+)
 create mode 100644 drivers/platform/chrome/chromeos_of_hw_prober.c
drivers/platform/chrome/Kconfig +11 -0
index 7dbeb786352a..b7dbaf77b6db 100644
@@ -61,6 +61,17 @@ config CHROMEOS_TBMC
 	  To compile this driver as a module, choose M here: the
 	  module will be called chromeos_tbmc.
 
+config CHROMEOS_OF_HW_PROBER
+	tristate "ChromeOS Device Tree Hardware Prober"
+	depends on OF
+	depends on I2C
+	select OF_DYNAMIC
+	default OF
+	help
+	  This option enables the device tree hardware prober for ChromeOS
+	  devices. The driver will probe the correct component variant in
+	  devices that have multiple drop-in options for one component.
+
 config CROS_EC
 	tristate "ChromeOS Embedded Controller"
 	select CROS_EC_PROTO
drivers/platform/chrome/Makefile +1 -0
index 2dcc6ccc2302..21a9d5047053 100644
@@ -8,6 +8,7 @@ obj-$(CONFIG_CHROMEOS_ACPI)		+= chromeos_acpi.o
 obj-$(CONFIG_CHROMEOS_LAPTOP)		+= chromeos_laptop.o
 obj-$(CONFIG_CHROMEOS_PRIVACY_SCREEN)	+= chromeos_privacy_screen.o
 obj-$(CONFIG_CHROMEOS_PSTORE)		+= chromeos_pstore.o
+obj-$(CONFIG_CHROMEOS_OF_HW_PROBER)	+= chromeos_of_hw_prober.o
 obj-$(CONFIG_CHROMEOS_TBMC)		+= chromeos_tbmc.o
 obj-$(CONFIG_CROS_EC)			+= cros_ec.o
 obj-$(CONFIG_CROS_EC_I2C)		+= cros_ec_i2c.o
drivers/platform/chrome/chromeos_of_hw_prober.c new file +104 -0

104 lines not on this page · raw message

Signature · 1 line
2.46.0.184.g6999bdac58-goog
author maintainer
#35 lore raw

PATCH v5 10/10 arm64: dts: mediatek: mt8173-elm-hana: Mark touchscreens and trackpads as fail

In reply to Chen-Yu Tsai

Instead of having them all available, mark them all as "fail-needs-probe" and have the implementation try to probe which one is present.

Also remove the shared resource workaround by moving the pinctrl entry for the trackpad interrupt line back into the individual trackpad nodes.

---
Changes since v4:
- Rebased

Changes since v3:
- Also remove second source workaround, i.e. move the interrupt line pinctrl entry from the i2c node back to the components.

Changes since v2:
- Drop class from status

---
 arch/arm64/boot/dts/mediatek/mt8173-elm-hana.dtsi | 13 +++++++++++++
 arch/arm64/boot/dts/mediatek/mt8173-elm.dtsi      |  4 ++--
 2 files changed, 15 insertions(+), 2 deletions(-)
arch/arm64/boot/dts/mediatek/mt8173-elm-hana.dtsi +13 -0
index 8d1cbc92bce3..251e084bf7de 100644
@@ -14,6 +14,7 @@ touchscreen2: touchscreen@34 {
 		compatible = "melfas,mip4_ts";
 		reg = <0x34>;
 		interrupts-extended = <&pio 88 IRQ_TYPE_LEVEL_LOW>;
+		status = "fail-needs-probe";
 	};
 
 	/*
@@ -26,6 +27,7 @@ touchscreen3: touchscreen@20 {
 		reg = <0x20>;
 		hid-descr-addr = <0x0020>;
 		interrupts-extended = <&pio 88 IRQ_TYPE_LEVEL_LOW>;
+		status = "fail-needs-probe";
 	};
 
 	/* Lenovo Ideapad C330 uses G2Touch touchscreen as a 2nd source touchscreen */
@@ -47,9 +49,12 @@ &i2c4 {
 	trackpad2: trackpad@2c {
 		compatible = "hid-over-i2c";
 		interrupts-extended = <&pio 117 IRQ_TYPE_LEVEL_LOW>;
+		pinctrl-names = "default";
+		pinctrl-0 = <&trackpad_irq>;
 		reg = <0x2c>;
 		hid-descr-addr = <0x0020>;
 		wakeup-source;
+		status = "fail-needs-probe";
 	};
 };
 
@@ -74,3 +79,11 @@ pins_wp {
 		};
 	};
 };
+
+&touchscreen {
+	status = "fail-needs-probe";
+};
+
+&trackpad {
+	status = "fail-needs-probe";
+};
arch/arm64/boot/dts/mediatek/mt8173-elm.dtsi +2 -2
index b4d85147b77b..eee64461421f 100644
@@ -358,12 +358,12 @@ touchscreen: touchscreen@10 {
 &i2c4 {
 	clock-frequency = <400000>;
 	status = "okay";
-	pinctrl-names = "default";
-	pinctrl-0 = <&trackpad_irq>;
 
 	trackpad: trackpad@15 {
 		compatible = "elan,ekth3000";
 		interrupts-extended = <&pio 117 IRQ_TYPE_LEVEL_LOW>;
+		pinctrl-names = "default";
+		pinctrl-0 = <&trackpad_irq>;
 		reg = <0x15>;
 		vcc-supply = <&mt6397_vgp6_reg>;
 		wakeup-source;
Signature · 1 line
2.46.0.184.g6999bdac58-goog