From: Rob Herring <robh@kernel.org>
To: Chen-Yu Tsai <wenst@chromium.org>
Cc: Saravana Kannan <saravanak@google.com>,
Matthias Brugger <matthias.bgg@gmail.com>,
AngeloGioacchino Del Regno
<angelogioacchino.delregno@collabora.com>,
Wolfram Sang <wsa@kernel.org>, Benson Leung <bleung@chromium.org>,
Tzung-Bi Shih <tzungbi@kernel.org>,
Mark Brown <broonie@kernel.org>,
Liam Girdwood <lgirdwood@gmail.com>,
chrome-platform@lists.linux.dev, devicetree@vger.kernel.org,
linux-arm-kernel@lists.infradead.org,
linux-mediatek@lists.infradead.org, linux-kernel@vger.kernel.org,
Douglas Anderson <dianders@chromium.org>,
Johan Hovold <johan@kernel.org>, Jiri Kosina <jikos@kernel.org>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
linux-i2c@vger.kernel.org
Subject: Re: [PATCH v6 08/12] i2c: Introduce OF component probe function
Date: Wed, 4 Sep 2024 10:37:46 -0500 [thread overview]
Message-ID: <20240904153746.GA2610341-robh@kernel.org> (raw)
In-Reply-To: <20240904090016.2841572-9-wenst@chromium.org>
On Wed, Sep 04, 2024 at 05:00:10PM +0800, Chen-Yu Tsai wrote:
> 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.
>
> Signed-off-by: Chen-Yu Tsai <wenst@chromium.org>
> ---
> Changes since v5:
> - Fixed indent in Makefile
> - Split regulator and GPIO TODO items
> - Reversed final conditional in i2c_of_probe_enable_node()
>
> 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 | 158 +++++++++++++++++++++++++++++++
> include/linux/i2c.h | 4 +
> 3 files changed, 163 insertions(+)
> create mode 100644 drivers/i2c/i2c-core-of-prober.c
>
> diff --git a/drivers/i2c/Makefile b/drivers/i2c/Makefile
> index f12d6b10a85e..c539cdc1e305 100644
> --- a/drivers/i2c/Makefile
> +++ b/drivers/i2c/Makefile
> @@ -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
> diff --git a/drivers/i2c/i2c-core-of-prober.c b/drivers/i2c/i2c-core-of-prober.c
> new file mode 100644
> index 000000000000..64d7631f4885
> --- /dev/null
> +++ b/drivers/i2c/i2c-core-of-prober.c
> @@ -0,0 +1,158 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Linux I2C core OF component prober code
> + *
> + * Copyright (C) 2024 Google LLC
> + */
> +
> +#include <linux/cleanup.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/slab.h>
> +
> +/*
> + * Some devices, such as Google Hana Chromebooks, are produced by multiple
> + * vendors each using their preferred components. Such components are all
> + * in the device tree. Instead of having all of them enabled and having each
> + * driver separately try and probe its device while fighting over shared
> + * resources, they can be marked as "fail-needs-probe" and have a prober
> + * figure out which one is actually used beforehand.
> + *
> + * This prober assumes such drop-in parts are on the same I2C bus, have
> + * non-conflicting addresses, and can be directly probed by seeing which
> + * address responds.
> + *
> + * TODO:
> + * - Support handling common regulators.
> + * - Support handling common GPIOs.
> + * - Support I2C muxes
> + */
> +
> +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);
> + if (!node)
> + return dev_err_ptr_probe(dev, -ENODEV, "Could not find %s device node\n", type);
> +
> + struct device_node *i2c_node __free(device_node) = of_get_parent(node);
> + if (!of_node_name_eq(i2c_node, "i2c"))
> + return dev_err_ptr_probe(dev, -EINVAL, "%s device isn't on I2C bus\n", type);
> +
> + if (!of_device_is_available(i2c_node))
> + return dev_err_ptr_probe(dev, -ENODEV, "I2C controller not available\n");
> +
> + return no_free_ptr(i2c_node);
> +}
> +
> +static int i2c_of_probe_enable_node(struct device *dev, struct device_node *node)
> +{
> + int ret;
> +
> + dev_info(dev, "Enabling %pOF\n", node);
> +
> + struct of_changeset *ocs __free(kfree) = kzalloc(sizeof(*ocs), GFP_KERNEL);
> + if (!ocs)
> + return -ENOMEM;
> +
> + of_changeset_init(ocs);
> + ret = of_changeset_update_prop_string(ocs, node, "status", "okay");
> + if (ret)
> + return ret;
> +
> + ret = of_changeset_apply(ocs);
> + if (ret) {
> + /* ocs needs to be explicitly cleaned up before being freed. */
> + of_changeset_destroy(ocs);
> + } else {
> + /*
> + * 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);
> + }
> +
> + return ret;
> +}
> +
> +/**
> + * i2c_of_probe_component() - probe for devices of "type" on the same i2c bus
> + * @dev: &struct device of the caller, only used for dev_* printk messages
> + * @type: a string to match the device node name prefix to probe for
> + *
> + * Probe for possible I2C components of the same "type" on the same I2C bus
> + * that have their status marked as "fail".
> + *
> + * Assumes that across the entire device tree the only instances of nodes
> + * prefixed with "type" are the ones that need handling for second source
> + * components. In other words, if type is "touchscreen", then all device
> + * nodes named "touchscreen*" are the ones that need probing. There cannot
> + * be another "touchscreen" node that is already enabled.
> + *
> + * Assumes that for each "type" of component, only one actually exists. In
> + * other words, only one matching and existing device will be enabled.
> + *
> + * Context: Process context only. Does non-atomic I2C transfers.
> + * Should only be used from a driver probe function, as the function
> + * can return -EPROBE_DEFER if the I2C adapter is unavailable.
> + * Return: 0 on success or no-op, error code otherwise.
> + * A no-op can happen when it seems like the device tree already
> + * has components of the type to be probed already enabled. This
> + * can happen when the device tree had not been updated to mark
> + * the status of the to-be-probed components as "fail". Or this
> + * function was already run with the same parameters and succeeded
> + * in enabling a component. The latter could happen if the user
> + * had multiple types of components to probe, and one of them down
> + * the list caused a deferred probe. This is expected behavior.
> + */
> +int i2c_of_probe_component(struct device *dev, const char *type)
> +{
> + struct i2c_adapter *i2c;
> + int ret;
> +
> + struct device_node *i2c_node __free(device_node) = i2c_of_probe_get_i2c_node(dev, type);
> + if (IS_ERR(i2c_node))
> + return PTR_ERR(i2c_node);
> +
> + for_each_child_of_node_scoped(i2c_node, node) {
for_each_available_child_of_node_scoped()
> + if (!of_node_name_prefix(node, type))
> + continue;
> + if (!of_device_is_available(node))
> + continue;
> +
> + /*
> + * Device tree has component already enabled. Either the
> + * device tree isn't supported or we already probed once.
> + */
> + return 0;
> + }
next prev parent reply other threads:[~2024-09-04 15:39 UTC|newest]
Thread overview: 47+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-04 9:00 [PATCH v6 00/12] platform/chrome: Introduce DT hardware prober Chen-Yu Tsai
2024-09-04 9:00 ` [PATCH v6 01/12] of: dynamic: Add of_changeset_update_prop_string Chen-Yu Tsai
2024-09-04 9:00 ` [PATCH v6 02/12] of: base: Add for_each_child_of_node_with_prefix_scoped() Chen-Yu Tsai
2024-09-04 13:03 ` Andy Shevchenko
2024-09-04 13:43 ` Rob Herring
2024-09-04 9:00 ` [PATCH v6 03/12] regulator: Move OF-specific regulator lookup code to of_regulator.c Chen-Yu Tsai
2024-09-04 13:09 ` Andy Shevchenko
2024-09-04 13:14 ` Andy Shevchenko
2024-09-05 8:11 ` Chen-Yu Tsai
2024-09-05 8:29 ` Andy Shevchenko
2024-09-04 9:00 ` [PATCH v6 04/12] regulator: Split up _regulator_get() Chen-Yu Tsai
2024-09-04 9:00 ` [PATCH v6 05/12] regulator: Do pure DT regulator lookup in of_regulator_bulk_get_all() Chen-Yu Tsai
2024-09-04 13:13 ` Andy Shevchenko
2024-09-09 2:39 ` Chen-Yu Tsai
2024-09-04 9:00 ` [PATCH v6 06/12] gpiolib: Add gpio_get_property_name_length() Chen-Yu Tsai
2024-09-04 13:40 ` Andy Shevchenko
2024-09-09 2:45 ` Chen-Yu Tsai
2024-09-11 7:37 ` Chen-Yu Tsai
2024-09-11 14:39 ` Andy Shevchenko
2024-09-04 9:00 ` [PATCH v6 07/12] i2c: core: Remove extra space in Makefile Chen-Yu Tsai
2024-09-04 13:41 ` Andy Shevchenko
2024-09-04 9:00 ` [PATCH v6 08/12] i2c: Introduce OF component probe function Chen-Yu Tsai
2024-09-04 13:47 ` Andy Shevchenko
2024-09-09 3:02 ` Chen-Yu Tsai
2024-09-04 15:37 ` Rob Herring [this message]
2024-09-04 9:00 ` [PATCH v6 09/12] i2c: of-prober: Add regulator support Chen-Yu Tsai
2024-09-04 13:53 ` Andy Shevchenko
2024-09-04 22:57 ` Doug Anderson
2024-09-05 15:10 ` Chen-Yu Tsai
2024-09-05 18:14 ` Doug Anderson
2024-09-05 18:42 ` Mark Brown
2024-09-05 19:13 ` Andy Shevchenko
2024-09-06 3:45 ` Chen-Yu Tsai
2024-09-11 0:30 ` Doug Anderson
2024-09-11 6:12 ` Chen-Yu Tsai
2024-09-11 14:38 ` Andy Shevchenko
2024-09-11 14:49 ` Mark Brown
2024-09-04 9:00 ` [PATCH v6 10/12] i2c: of-prober: Add GPIO support Chen-Yu Tsai
2024-09-04 14:04 ` Andy Shevchenko
2024-09-04 9:00 ` [PATCH v6 11/12] platform/chrome: Introduce device tree hardware prober Chen-Yu Tsai
2024-09-04 10:08 ` Tzung-Bi Shih
2024-09-05 3:52 ` Chen-Yu Tsai
2024-09-05 4:34 ` Tzung-Bi Shih
2024-09-04 9:00 ` [PATCH v6 12/12] arm64: dts: mediatek: mt8173-elm-hana: Mark touchscreens and trackpads as fail Chen-Yu Tsai
2024-09-04 17:19 ` (subset) [PATCH v6 00/12] platform/chrome: Introduce DT hardware prober Mark Brown
2024-09-07 16:58 ` Wolfram Sang
2024-09-09 3:24 ` Chen-Yu Tsai
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240904153746.GA2610341-robh@kernel.org \
--to=robh@kernel.org \
--cc=andriy.shevchenko@linux.intel.com \
--cc=angelogioacchino.delregno@collabora.com \
--cc=bleung@chromium.org \
--cc=broonie@kernel.org \
--cc=chrome-platform@lists.linux.dev \
--cc=devicetree@vger.kernel.org \
--cc=dianders@chromium.org \
--cc=jikos@kernel.org \
--cc=johan@kernel.org \
--cc=lgirdwood@gmail.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-i2c@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mediatek@lists.infradead.org \
--cc=matthias.bgg@gmail.com \
--cc=saravanak@google.com \
--cc=tzungbi@kernel.org \
--cc=wenst@chromium.org \
--cc=wsa@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).