Message ID | 20231109100606.1245545-3-wenst@chromium.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b129:0:b0:403:3b70:6f57 with SMTP id q9csp332859vqs; Thu, 9 Nov 2023 02:08:23 -0800 (PST) X-Google-Smtp-Source: AGHT+IHJtKB0OzcdCpr88WERA8SM9peGPUMS9BPcukZSSr/JSJoom65cmAyJfxc/9Wo/ypXoPvpC X-Received: by 2002:a17:90b:3ec4:b0:281:20a0:dae3 with SMTP id rm4-20020a17090b3ec400b0028120a0dae3mr920553pjb.40.1699524503693; Thu, 09 Nov 2023 02:08:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699524503; cv=none; d=google.com; s=arc-20160816; b=zlASEDXHxhY/3NFNkclZcLCfh0MK6MjmYHkxy+KDv9dHVsWmJ2UDKTV7RhAgESFxrS nIOSYw185FVWpCtkauobGahZfU4kmwSZLcTV75ABGHtznzj9BZ0yLWdLdf3Eu/P6A9Q5 cuDrmgeZfnB4c/tTzGnyPJTgWfthIDYbcdbwatnNHfMy/AL6SNWJqQ+BxJHXGcKi0wcn Y2dRYTZGxkRddpnhCh0WdOHOPaQqyr6+WCCmJpZrpy24plZ8WPbS76/wd1VTddUUoy1e Z3WeIMmJiGerXopeHvvG/9xjjIa/j0yn44h7SBUxFWiOrRKj+X+IjOdBWLRSrK2ZeCgC OZ9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=t5ye6xQE9knRoa1If0NJpRXCWT7E3YY80pCdkZR9RUU=; fh=9RjyB+hDNs618LdaNPm846thAWpv+UOF4Pqs3eQuhzI=; b=eygkfS8jfhJhQG3ZK1Wo3AFGlxi1YL6b2TXwc4TN/MR/LB/lMOvecF46/mqbaA6oCr kOUoz1vYlR9FBcdiXjygHxT4bGmE0QPZ1iRYxQ3fcQZ+g3mw45sac9NCXuwC0dhNvFQV K/eS/OzAfpmtLaIIlwFhVtpczZX4wz8/XQx7rIGKwzfF+4j3BAqmqDN6v9zhke8igpth tWIJI6AzNT1NSQYV+0Jm830ZPgiZyVSZJX5BzqDowXyOzl/pdli15PYTpI2UqhFgEZtC NiLBeUrqP3w2TjiZ+KVdjyiuTSTDRrj+72gxfs9g+t6zmz0cTfUOylvbjUPwK6hqcm5h xqTg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=hZBMy4UD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id il7-20020a17090b164700b002805a422743si1430211pjb.12.2023.11.09.02.08.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 02:08:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=hZBMy4UD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 68AE5838912F; Thu, 9 Nov 2023 02:08:08 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233305AbjKIKHG (ORCPT <rfc822;jaysivo@gmail.com> + 32 others); Thu, 9 Nov 2023 05:07:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51498 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232741AbjKIKG5 (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 9 Nov 2023 05:06:57 -0500 Received: from mail-pg1-x535.google.com (mail-pg1-x535.google.com [IPv6:2607:f8b0:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5BC032D76 for <linux-kernel@vger.kernel.org>; Thu, 9 Nov 2023 02:06:50 -0800 (PST) Received: by mail-pg1-x535.google.com with SMTP id 41be03b00d2f7-5bdc185c449so544923a12.0 for <linux-kernel@vger.kernel.org>; Thu, 09 Nov 2023 02:06:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1699524410; x=1700129210; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=t5ye6xQE9knRoa1If0NJpRXCWT7E3YY80pCdkZR9RUU=; b=hZBMy4UDuygUobGSPbCJdhb96NSnsSTjP2nF6QnsG8V12c3KKo1pIJGq9xtZ7CzMzY sphs0p80O7vxqDIuBLcl+hRtdFJL03cgV2gegaHWB213UJhwgOSwZaUZi1lFIzORVff0 QZyTC2jQNVjSP1hJ5OG/14Q6vcmTeKlec3gqc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699524410; x=1700129210; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=t5ye6xQE9knRoa1If0NJpRXCWT7E3YY80pCdkZR9RUU=; b=o/Z3Av/unfTUzBv/EXwSIhU2ck4pF1CbWc8iGD/Tnc23y9y1u/EnbautJgmEsGUMoj Q7n5baLdRKHNT82IP20xs1lapHF+5zW19DknT+fymZhq/tkkfcf80RTJVZBthgf0kC5x xXp/ErQgkAPNzV7kwb2lVr09y5L9Ygb+KFSvitIcKrE1L8dJJ45j57GgXjaaA7Wm6iYS f9PgyBJ+eooL3HWnLRP35ZTlCkxssQGwHi8cc81+QyQkT1LcTHOi550QHt23HG4Uf2QA 7Vku+knwk+Q/pVXWwlEkigHh3s3rBIaAfvLHAnfUTzQ/1KkWXDmd9RNBPTNhpV3QSjuJ HIxw== X-Gm-Message-State: AOJu0Yy3Lcgw0poXg+dHNkDxPnaABPuvWny0JTYlcC0kpD2TZjqVCKk3 TxgNGcEnHfcrGMIMIEv4ixWn9w== X-Received: by 2002:a17:902:8c83:b0:1cc:2c45:757a with SMTP id t3-20020a1709028c8300b001cc2c45757amr4475248plo.10.1699524409702; Thu, 09 Nov 2023 02:06:49 -0800 (PST) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:6f57:d4c:468c:5daf]) by smtp.gmail.com with ESMTPSA id c13-20020a170902d48d00b001c60ba709b7sm3127511plg.125.2023.11.09.02.06.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 02:06:49 -0800 (PST) From: Chen-Yu Tsai <wenst@chromium.org> To: Rob Herring <robh+dt@kernel.org>, Frank Rowand <frowand.list@gmail.com>, Krzysztof Kozlowski <krzysztof.kozlowski+dt@linaro.org>, Conor Dooley <conor+dt@kernel.org>, Matthias Brugger <matthias.bgg@gmail.com>, AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com> Cc: Hsin-Yi Wang <hsinyi@chromium.org>, Dmitry Torokhov <dmitry.torokhov@gmail.com>, andriy.shevchenko@linux.intel.com, Jiri Kosina <jikos@kernel.org>, linus.walleij@linaro.org, broonie@kernel.org, gregkh@linuxfoundation.org, hdegoede@redhat.com, james.clark@arm.com, james@equiv.tech, keescook@chromium.org, petr.tesarik.ext@huawei.com, rafael@kernel.org, tglx@linutronix.de, Jeff LaBundy <jeff@labundy.com>, linux-input@vger.kernel.org, Chen-Yu Tsai <wenst@chromium.org>, 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> Subject: [RFC PATCH v2 2/7] of: Introduce hardware prober driver Date: Thu, 9 Nov 2023 18:05:59 +0800 Message-ID: <20231109100606.1245545-3-wenst@chromium.org> X-Mailer: git-send-email 2.42.0.869.gea05f2083d-goog In-Reply-To: <20231109100606.1245545-1-wenst@chromium.org> References: <20231109100606.1245545-1-wenst@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Thu, 09 Nov 2023 02:08:08 -0800 (PST) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782080605869416337 X-GMAIL-MSGID: 1782080605869416337 |
Series |
of: Introduce hardware prober driver
|
|
Commit Message
Chen-Yu Tsai
Nov. 9, 2023, 10:05 a.m. UTC
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-xxx". 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>
---
drivers/of/Kconfig | 13 ++++
drivers/of/Makefile | 1 +
drivers/of/hw_prober.c | 154 +++++++++++++++++++++++++++++++++++++++++
3 files changed, 168 insertions(+)
create mode 100644 drivers/of/hw_prober.c
Comments
On Thu, Nov 9, 2023 at 4:06 AM Chen-Yu Tsai <wenst@chromium.org> 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 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-xxx". 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> > --- > drivers/of/Kconfig | 13 ++++ > drivers/of/Makefile | 1 + > drivers/of/hw_prober.c | 154 +++++++++++++++++++++++++++++++++++++++++ Not sure about having this in drivers/of/, but fine for now... Really, the I2C bus stuff should be in the I2C core with the rest of the code that knows how to parse I2C bus nodes. > 3 files changed, 168 insertions(+) > create mode 100644 drivers/of/hw_prober.c > > diff --git a/drivers/of/Kconfig b/drivers/of/Kconfig > index da9826accb1b..269d20d51936 100644 > --- a/drivers/of/Kconfig > +++ b/drivers/of/Kconfig > @@ -102,4 +102,17 @@ config OF_OVERLAY > config OF_NUMA > bool > > +config HW_PROBER > + bool "Hardware Prober driver" > + select I2C You should not select I2C, but enable/disable I2C functionality based on it being enabled. > + select OF_DYNAMIC > + help > + Some devices will have multiple drop-in options for one component. > + In many cases the different options are indistinguishable by the > + kernel without actually probing each possible option. > + > + This driver is meant to handle the probing of such components, and > + update the running device tree such that the correct variant is > + made available. > + > endif # OF > diff --git a/drivers/of/Makefile b/drivers/of/Makefile > index eff624854575..ed3875cdc554 100644 > --- a/drivers/of/Makefile > +++ b/drivers/of/Makefile > @@ -12,6 +12,7 @@ obj-$(CONFIG_OF_RESERVED_MEM) += of_reserved_mem.o > obj-$(CONFIG_OF_RESOLVE) += resolver.o > obj-$(CONFIG_OF_OVERLAY) += overlay.o > obj-$(CONFIG_OF_NUMA) += of_numa.o > +obj-$(CONFIG_HW_PROBER) += hw_prober.o > > ifdef CONFIG_KEXEC_FILE > ifdef CONFIG_OF_FLATTREE > diff --git a/drivers/of/hw_prober.c b/drivers/of/hw_prober.c > new file mode 100644 > index 000000000000..442da6eff896 > --- /dev/null > +++ b/drivers/of/hw_prober.c > @@ -0,0 +1,154 @@ > +// SPDX-License-Identifier: GPL-2.0-only > +/* > + * hw_prober.c - Hardware prober driver > + * > + * Copyright (c) 2023 Google LLC > + */ > + > +#include <linux/array_size.h> > +#include <linux/i2c.h> > +#include <linux/of.h> > +#include <linux/platform_device.h> > + > +#define DRV_NAME "hw_prober" > + > +/** > + * struct hw_prober_entry - Holds an entry for the hardware prober > + * > + * @compatible: compatible string to match against the machine > + * @prober: prober function to call when machine matches > + * @data: extra data for the prober function > + */ > +struct hw_prober_entry { > + const char *compatible; > + int (*prober)(struct platform_device *pdev, const void *data); > + const void *data; > +}; > + > +/* > + * Some devices, such as Google Hana Chromebooks, are produced by multiple > + * vendors each using their preferred components. This prober assumes such > + * drop-in parts are on dedicated I2C busses, have non-conflicting addresses, > + * and can be directly probed by seeing which address responds without needing > + * regulators or GPIOs being enabled or toggled. > + */ > +static int i2c_component_prober(struct platform_device *pdev, const void *data) > +{ > + const char *node_name = data; > + struct device_node *node, *i2c_node; > + struct i2c_adapter *i2c; > + int ret = 0; > + > + node = of_find_node_by_name(NULL, node_name); > + if (!node) > + return dev_err_probe(&pdev->dev, -ENODEV, "Could not find %s device node\n", > + node_name); > + > + i2c_node = of_get_next_parent(node); > + if (strcmp(i2c_node->name, "i2c")) { We have functions for comparing node names, use them and don't access ->name directly. > + of_node_put(i2c_node); > + return dev_err_probe(&pdev->dev, -EINVAL, "%s device isn't on I2C bus\n", > + node_name); > + } > + > + for_each_child_of_node(i2c_node, node) { > + if (!of_node_name_prefix(node, node_name)) > + continue; > + if (!of_device_is_fail(node)) { > + /* device tree has component already enabled */ This isn't quite right if there's a disabled device. To check 'is enabled', you just need to use of_device_is_available(). > + of_node_put(node); > + of_node_put(i2c_node); > + return 0; > + } > + } > + > + i2c = of_get_i2c_adapter_by_node(i2c_node); > + if (!i2c) { > + of_node_put(i2c_node); > + return dev_err_probe(&pdev->dev, -EPROBE_DEFER, "Couldn't get I2C adapter\n"); > + } > + > + for_each_child_of_node(i2c_node, node) { The I2C core will walk the devices too. Perhaps if that saves off a list of failed devices, then we don't need to walk the nodes again. > + struct property *prop; > + union i2c_smbus_data data; > + u32 addr; > + > + if (!of_node_name_prefix(node, node_name)) > + continue; > + if (of_property_read_u32(node, "reg", &addr)) > + continue; > + if (i2c_smbus_xfer(i2c, addr, 0, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data) < 0) > + continue; > + > + dev_info(&pdev->dev, "Enabling %pOF\n", node); > + > + prop = kzalloc(sizeof(*prop), GFP_KERNEL); > + if (!prop) { > + ret = -ENOMEM; > + of_node_put(node); > + break; > + } > + > + prop->name = "status"; > + prop->length = 5; > + prop->value = "okay"; > + > + /* Found a device that is responding */ > + ret = of_update_property(node, prop); Use the changeset API instead and make an update flavor of of_changeset_add_prop_string(). > + if (ret) > + kfree(prop); > + > + of_node_put(node); > + break; > + } > + > + i2c_put_adapter(i2c); > + of_node_put(i2c_node); > + > + return ret; > +} > + > +static const struct hw_prober_entry hw_prober_platforms[] = { > + { .compatible = "google,hana", .prober = i2c_component_prober, .data = "touchscreen" }, > + { .compatible = "google,hana", .prober = i2c_component_prober, .data = "trackpad" }, Not generic code. Needs to be somewhere else. > +}; > + > +static int hw_prober_probe(struct platform_device *pdev) > +{ > + for (int i = 0; i < ARRAY_SIZE(hw_prober_platforms); i++) > + if (of_machine_is_compatible(hw_prober_platforms[i].compatible)) { > + int ret; > + > + ret = hw_prober_platforms[i].prober(pdev, hw_prober_platforms[i].data); > + if (ret) > + return ret; > + } > + > + return 0; > +} > + > +static struct platform_driver hw_prober_driver = { > + .probe = hw_prober_probe, > + .driver = { > + .name = DRV_NAME, > + }, > +}; > + > +static int __init hw_prober_driver_init(void) > +{ > + struct platform_device *pdev; > + int ret; > + > + ret = platform_driver_register(&hw_prober_driver); > + if (ret) > + return ret; > + > + pdev = platform_device_register_simple(DRV_NAME, -1, NULL, 0); This should be dependent on platforms that need it, not everyone. IOW, this is where checking for "google,hana" belongs. Rob
On Thu, Nov 09, 2023 at 06:05:59PM +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 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-xxx". 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. ... > +config HW_PROBER config OF_HW_PROBER // or anything with explicit OF Don't give a false impression that this is something that may works without OF support. ... > + bool "Hardware Prober driver" Ditto. ... > +/* > + * hw_prober.c - Hardware prober driver Do not include filename into the file itself. > + * > + * Copyright (c) 2023 Google LLC > + */ ... > + node = of_find_node_by_name(NULL, node_name); > + if (!node) > + return dev_err_probe(&pdev->dev, -ENODEV, "Could not find %s device node\n", > + node_name); With struct device *dev = &pdev->dev; this and other lines can be made neater. ... For better maintenance it's good to have ret assignment be placed here ret = 0; > + for_each_child_of_node(i2c_node, node) { > + struct property *prop; > + union i2c_smbus_data data; > + u32 addr; > + > + if (!of_node_name_prefix(node, node_name)) > + continue; > + if (of_property_read_u32(node, "reg", &addr)) > + continue; > + if (i2c_smbus_xfer(i2c, addr, 0, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data) < 0) > + continue; > + > + dev_info(&pdev->dev, "Enabling %pOF\n", node); > + > + prop = kzalloc(sizeof(*prop), GFP_KERNEL); > + if (!prop) { > + ret = -ENOMEM; > + of_node_put(node); > + break; > + } > + > + prop->name = "status"; > + prop->length = 5; > + prop->value = "okay"; > + > + /* Found a device that is responding */ > + ret = of_update_property(node, prop); > + if (ret) > + kfree(prop); > + > + of_node_put(node); > + break; > + } ... > +static const struct hw_prober_entry hw_prober_platforms[] = { > + { .compatible = "google,hana", .prober = i2c_component_prober, .data = "touchscreen" }, > + { .compatible = "google,hana", .prober = i2c_component_prober, .data = "trackpad" }, > +}; Why can't OF ID table be used for this? ... > + for (int i = 0; i < ARRAY_SIZE(hw_prober_platforms); i++) unsigned? > + if (of_machine_is_compatible(hw_prober_platforms[i].compatible)) { > + int ret; > + > + ret = hw_prober_platforms[i].prober(pdev, hw_prober_platforms[i].data); > + if (ret) > + return ret; > + } ... > + pdev = platform_device_register_simple(DRV_NAME, -1, NULL, 0); -1 is defined in the header, use that definition. > + if (!IS_ERR(pdev)) > + return 0; > + > + platform_driver_unregister(&hw_prober_driver); > + > + return PTR_ERR(pdev); Can you use standard pattern, i.e. checking for the _error_ condition?
On Fri, Nov 10, 2023 at 1:54 AM Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote: > > On Thu, Nov 09, 2023 at 06:05:59PM +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 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-xxx". 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. > > ... > > > +config HW_PROBER > > config OF_HW_PROBER // or anything with explicit OF > > Don't give a false impression that this is something that may works without > OF support. Ack. > ... > > > + bool "Hardware Prober driver" > > Ditto. Ack. > ... > > > +/* > > + * hw_prober.c - Hardware prober driver > > Do not include filename into the file itself. Ack. > > + * > > + * Copyright (c) 2023 Google LLC > > + */ > > ... > > > + node = of_find_node_by_name(NULL, node_name); > > + if (!node) > > + return dev_err_probe(&pdev->dev, -ENODEV, "Could not find %s device node\n", > > + node_name); > > With > > struct device *dev = &pdev->dev; > > this and other lines can be made neater. Ack. > ... > > > For better maintenance it's good to have ret assignment be placed here > > ret = 0; Ack. > > + for_each_child_of_node(i2c_node, node) { > > + struct property *prop; > > + union i2c_smbus_data data; > > + u32 addr; > > + > > + if (!of_node_name_prefix(node, node_name)) > > + continue; > > + if (of_property_read_u32(node, "reg", &addr)) > > + continue; > > + if (i2c_smbus_xfer(i2c, addr, 0, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data) < 0) > > + continue; > > + > > + dev_info(&pdev->dev, "Enabling %pOF\n", node); > > + > > + prop = kzalloc(sizeof(*prop), GFP_KERNEL); > > + if (!prop) { > > + ret = -ENOMEM; > > + of_node_put(node); > > + break; > > + } > > + > > + prop->name = "status"; > > + prop->length = 5; > > + prop->value = "okay"; > > + > > + /* Found a device that is responding */ > > + ret = of_update_property(node, prop); > > + if (ret) > > + kfree(prop); > > + > > + of_node_put(node); > > + break; > > + } > > ... > > > +static const struct hw_prober_entry hw_prober_platforms[] = { > > + { .compatible = "google,hana", .prober = i2c_component_prober, .data = "touchscreen" }, > > + { .compatible = "google,hana", .prober = i2c_component_prober, .data = "trackpad" }, > > +}; > > Why can't OF ID table be used for this? My intent was to have this accept a probe function, which may take an extra data argument. So either a new structure like the one here, or use OF ID table, and then another layer with a struct holding the prober and extra data pointer. I'm guessing this will change since Rob thinks the next patch that adds a different prober doesn't belong here. > ... > > > + for (int i = 0; i < ARRAY_SIZE(hw_prober_platforms); i++) > > unsigned? Ack. > > + if (of_machine_is_compatible(hw_prober_platforms[i].compatible)) { > > + int ret; > > + > > + ret = hw_prober_platforms[i].prober(pdev, hw_prober_platforms[i].data); > > + if (ret) > > + return ret; > > + } > > ... > > > + pdev = platform_device_register_simple(DRV_NAME, -1, NULL, 0); > > -1 is defined in the header, use that definition. Ack. > > + if (!IS_ERR(pdev)) > > + return 0; > > + > > + platform_driver_unregister(&hw_prober_driver); > > + > > + return PTR_ERR(pdev); > > Can you use standard pattern, i.e. checking for the _error_ condition? Ack. Thanks ChenYu
On Thu, Nov 9, 2023 at 11:14 PM Rob Herring <robh+dt@kernel.org> wrote: > > On Thu, Nov 9, 2023 at 4:06 AM Chen-Yu Tsai <wenst@chromium.org> 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 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-xxx". 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> > > --- > > drivers/of/Kconfig | 13 ++++ > > drivers/of/Makefile | 1 + > > drivers/of/hw_prober.c | 154 +++++++++++++++++++++++++++++++++++++++++ > > Not sure about having this in drivers/of/, but fine for now... Really, > the I2C bus stuff should be in the I2C core with the rest of the code > that knows how to parse I2C bus nodes. I think we can move the majority of this patch into the I2C core and export just the one prober function. The remainder and the other SKU ID based prober could be moved to drivers/platform/chrome. Not sure how we want to place probers for other platforms though? > > 3 files changed, 168 insertions(+) > > create mode 100644 drivers/of/hw_prober.c > > > > diff --git a/drivers/of/Kconfig b/drivers/of/Kconfig > > index da9826accb1b..269d20d51936 100644 > > --- a/drivers/of/Kconfig > > +++ b/drivers/of/Kconfig > > @@ -102,4 +102,17 @@ config OF_OVERLAY > > config OF_NUMA > > bool > > > > +config HW_PROBER > > + bool "Hardware Prober driver" > > + select I2C > > You should not select I2C, but enable/disable I2C functionality based > on it being enabled. Ack. > > + select OF_DYNAMIC > > + help > > + Some devices will have multiple drop-in options for one component. > > + In many cases the different options are indistinguishable by the > > + kernel without actually probing each possible option. > > + > > + This driver is meant to handle the probing of such components, and > > + update the running device tree such that the correct variant is > > + made available. > > + > > endif # OF > > diff --git a/drivers/of/Makefile b/drivers/of/Makefile > > index eff624854575..ed3875cdc554 100644 > > --- a/drivers/of/Makefile > > +++ b/drivers/of/Makefile > > @@ -12,6 +12,7 @@ obj-$(CONFIG_OF_RESERVED_MEM) += of_reserved_mem.o > > obj-$(CONFIG_OF_RESOLVE) += resolver.o > > obj-$(CONFIG_OF_OVERLAY) += overlay.o > > obj-$(CONFIG_OF_NUMA) += of_numa.o > > +obj-$(CONFIG_HW_PROBER) += hw_prober.o > > > > ifdef CONFIG_KEXEC_FILE > > ifdef CONFIG_OF_FLATTREE > > diff --git a/drivers/of/hw_prober.c b/drivers/of/hw_prober.c > > new file mode 100644 > > index 000000000000..442da6eff896 > > --- /dev/null > > +++ b/drivers/of/hw_prober.c > > @@ -0,0 +1,154 @@ > > +// SPDX-License-Identifier: GPL-2.0-only > > +/* > > + * hw_prober.c - Hardware prober driver > > + * > > + * Copyright (c) 2023 Google LLC > > + */ > > + > > +#include <linux/array_size.h> > > +#include <linux/i2c.h> > > +#include <linux/of.h> > > +#include <linux/platform_device.h> > > + > > +#define DRV_NAME "hw_prober" > > + > > +/** > > + * struct hw_prober_entry - Holds an entry for the hardware prober > > + * > > + * @compatible: compatible string to match against the machine > > + * @prober: prober function to call when machine matches > > + * @data: extra data for the prober function > > + */ > > +struct hw_prober_entry { > > + const char *compatible; > > + int (*prober)(struct platform_device *pdev, const void *data); > > + const void *data; > > +}; > > + > > +/* > > + * Some devices, such as Google Hana Chromebooks, are produced by multiple > > + * vendors each using their preferred components. This prober assumes such > > + * drop-in parts are on dedicated I2C busses, have non-conflicting addresses, > > + * and can be directly probed by seeing which address responds without needing > > + * regulators or GPIOs being enabled or toggled. > > + */ > > +static int i2c_component_prober(struct platform_device *pdev, const void *data) > > +{ > > + const char *node_name = data; > > + struct device_node *node, *i2c_node; > > + struct i2c_adapter *i2c; > > + int ret = 0; > > + > > + node = of_find_node_by_name(NULL, node_name); > > + if (!node) > > + return dev_err_probe(&pdev->dev, -ENODEV, "Could not find %s device node\n", > > + node_name); > > + > > + i2c_node = of_get_next_parent(node); > > + if (strcmp(i2c_node->name, "i2c")) { > > We have functions for comparing node names, use them and don't access > ->name directly. Ack. > > + of_node_put(i2c_node); > > + return dev_err_probe(&pdev->dev, -EINVAL, "%s device isn't on I2C bus\n", > > + node_name); > > + } > > + > > + for_each_child_of_node(i2c_node, node) { > > + if (!of_node_name_prefix(node, node_name)) > > + continue; > > + if (!of_device_is_fail(node)) { > > + /* device tree has component already enabled */ > > This isn't quite right if there's a disabled device. To check 'is > enabled', you just need to use of_device_is_available(). I wanted to distinguish between explicitly disabled, i.e. components known to not exist on some specific variant, vs fail or components that need probing. I suppose this is overengineered since the previous of_node_name_prefix() already restricts the check to the class of components we are interested in. > > + of_node_put(node); > > + of_node_put(i2c_node); > > + return 0; > > + } > > + } > > + > > + i2c = of_get_i2c_adapter_by_node(i2c_node); > > + if (!i2c) { > > + of_node_put(i2c_node); > > + return dev_err_probe(&pdev->dev, -EPROBE_DEFER, "Couldn't get I2C adapter\n"); > > + } > > + > > + for_each_child_of_node(i2c_node, node) { > > The I2C core will walk the devices too. Perhaps if that saves off a > list of failed devices, then we don't need to walk the nodes again. I will look into it. > > + struct property *prop; > > + union i2c_smbus_data data; > > + u32 addr; > > + > > + if (!of_node_name_prefix(node, node_name)) > > + continue; > > + if (of_property_read_u32(node, "reg", &addr)) > > + continue; > > + if (i2c_smbus_xfer(i2c, addr, 0, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data) < 0) > > + continue; > > + > > + dev_info(&pdev->dev, "Enabling %pOF\n", node); > > + > > + prop = kzalloc(sizeof(*prop), GFP_KERNEL); > > + if (!prop) { > > + ret = -ENOMEM; > > + of_node_put(node); > > + break; > > + } > > + > > + prop->name = "status"; > > + prop->length = 5; > > + prop->value = "okay"; > > + > > + /* Found a device that is responding */ > > + ret = of_update_property(node, prop); > > Use the changeset API instead and make an update flavor of > of_changeset_add_prop_string(). Ack. However I don't really like the API, as there is no easy way to free the allocated property if of_changeset_apply fails and we want to clean up. > > + if (ret) > > + kfree(prop); > > + > > + of_node_put(node); > > + break; > > + } > > + > > + i2c_put_adapter(i2c); > > + of_node_put(i2c_node); > > + > > + return ret; > > +} > > + > > +static const struct hw_prober_entry hw_prober_platforms[] = { > > + { .compatible = "google,hana", .prober = i2c_component_prober, .data = "touchscreen" }, > > + { .compatible = "google,hana", .prober = i2c_component_prober, .data = "trackpad" }, > > Not generic code. Needs to be somewhere else. OK. How about drivers/platform/chrome/ for the non-generic bits and the other prober patch? > > +}; > > + > > +static int hw_prober_probe(struct platform_device *pdev) > > +{ > > + for (int i = 0; i < ARRAY_SIZE(hw_prober_platforms); i++) > > + if (of_machine_is_compatible(hw_prober_platforms[i].compatible)) { > > + int ret; > > + > > + ret = hw_prober_platforms[i].prober(pdev, hw_prober_platforms[i].data); > > + if (ret) > > + return ret; > > + } > > + > > + return 0; > > +} > > + > > +static struct platform_driver hw_prober_driver = { > > + .probe = hw_prober_probe, > > + .driver = { > > + .name = DRV_NAME, > > + }, > > +}; > > + > > +static int __init hw_prober_driver_init(void) > > +{ > > + struct platform_device *pdev; > > + int ret; > > + > > + ret = platform_driver_register(&hw_prober_driver); > > + if (ret) > > + return ret; > > + > > + pdev = platform_device_register_simple(DRV_NAME, -1, NULL, 0); > > This should be dependent on platforms that need it, not everyone. IOW, > this is where checking for "google,hana" belongs. Ack. Thanks ChenYu
diff --git a/drivers/of/Kconfig b/drivers/of/Kconfig index da9826accb1b..269d20d51936 100644 --- a/drivers/of/Kconfig +++ b/drivers/of/Kconfig @@ -102,4 +102,17 @@ config OF_OVERLAY config OF_NUMA bool +config HW_PROBER + bool "Hardware Prober driver" + select I2C + select OF_DYNAMIC + help + Some devices will have multiple drop-in options for one component. + In many cases the different options are indistinguishable by the + kernel without actually probing each possible option. + + This driver is meant to handle the probing of such components, and + update the running device tree such that the correct variant is + made available. + endif # OF diff --git a/drivers/of/Makefile b/drivers/of/Makefile index eff624854575..ed3875cdc554 100644 --- a/drivers/of/Makefile +++ b/drivers/of/Makefile @@ -12,6 +12,7 @@ obj-$(CONFIG_OF_RESERVED_MEM) += of_reserved_mem.o obj-$(CONFIG_OF_RESOLVE) += resolver.o obj-$(CONFIG_OF_OVERLAY) += overlay.o obj-$(CONFIG_OF_NUMA) += of_numa.o +obj-$(CONFIG_HW_PROBER) += hw_prober.o ifdef CONFIG_KEXEC_FILE ifdef CONFIG_OF_FLATTREE diff --git a/drivers/of/hw_prober.c b/drivers/of/hw_prober.c new file mode 100644 index 000000000000..442da6eff896 --- /dev/null +++ b/drivers/of/hw_prober.c @@ -0,0 +1,154 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * hw_prober.c - Hardware prober driver + * + * Copyright (c) 2023 Google LLC + */ + +#include <linux/array_size.h> +#include <linux/i2c.h> +#include <linux/of.h> +#include <linux/platform_device.h> + +#define DRV_NAME "hw_prober" + +/** + * struct hw_prober_entry - Holds an entry for the hardware prober + * + * @compatible: compatible string to match against the machine + * @prober: prober function to call when machine matches + * @data: extra data for the prober function + */ +struct hw_prober_entry { + const char *compatible; + int (*prober)(struct platform_device *pdev, const void *data); + const void *data; +}; + +/* + * Some devices, such as Google Hana Chromebooks, are produced by multiple + * vendors each using their preferred components. This prober assumes such + * drop-in parts are on dedicated I2C busses, have non-conflicting addresses, + * and can be directly probed by seeing which address responds without needing + * regulators or GPIOs being enabled or toggled. + */ +static int i2c_component_prober(struct platform_device *pdev, const void *data) +{ + const char *node_name = data; + struct device_node *node, *i2c_node; + struct i2c_adapter *i2c; + int ret = 0; + + node = of_find_node_by_name(NULL, node_name); + if (!node) + return dev_err_probe(&pdev->dev, -ENODEV, "Could not find %s device node\n", + node_name); + + i2c_node = of_get_next_parent(node); + if (strcmp(i2c_node->name, "i2c")) { + of_node_put(i2c_node); + return dev_err_probe(&pdev->dev, -EINVAL, "%s device isn't on I2C bus\n", + node_name); + } + + for_each_child_of_node(i2c_node, node) { + if (!of_node_name_prefix(node, node_name)) + continue; + if (!of_device_is_fail(node)) { + /* device tree has component already enabled */ + of_node_put(node); + of_node_put(i2c_node); + return 0; + } + } + + i2c = of_get_i2c_adapter_by_node(i2c_node); + if (!i2c) { + of_node_put(i2c_node); + return dev_err_probe(&pdev->dev, -EPROBE_DEFER, "Couldn't get I2C adapter\n"); + } + + for_each_child_of_node(i2c_node, node) { + struct property *prop; + union i2c_smbus_data data; + u32 addr; + + if (!of_node_name_prefix(node, node_name)) + continue; + if (of_property_read_u32(node, "reg", &addr)) + continue; + if (i2c_smbus_xfer(i2c, addr, 0, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data) < 0) + continue; + + dev_info(&pdev->dev, "Enabling %pOF\n", node); + + prop = kzalloc(sizeof(*prop), GFP_KERNEL); + if (!prop) { + ret = -ENOMEM; + of_node_put(node); + break; + } + + prop->name = "status"; + prop->length = 5; + prop->value = "okay"; + + /* Found a device that is responding */ + ret = of_update_property(node, prop); + if (ret) + kfree(prop); + + of_node_put(node); + break; + } + + i2c_put_adapter(i2c); + of_node_put(i2c_node); + + return ret; +} + +static const struct hw_prober_entry hw_prober_platforms[] = { + { .compatible = "google,hana", .prober = i2c_component_prober, .data = "touchscreen" }, + { .compatible = "google,hana", .prober = i2c_component_prober, .data = "trackpad" }, +}; + +static int hw_prober_probe(struct platform_device *pdev) +{ + for (int i = 0; i < ARRAY_SIZE(hw_prober_platforms); i++) + if (of_machine_is_compatible(hw_prober_platforms[i].compatible)) { + int ret; + + ret = hw_prober_platforms[i].prober(pdev, hw_prober_platforms[i].data); + if (ret) + return ret; + } + + return 0; +} + +static struct platform_driver hw_prober_driver = { + .probe = hw_prober_probe, + .driver = { + .name = DRV_NAME, + }, +}; + +static int __init hw_prober_driver_init(void) +{ + struct platform_device *pdev; + int ret; + + ret = platform_driver_register(&hw_prober_driver); + if (ret) + return ret; + + pdev = platform_device_register_simple(DRV_NAME, -1, NULL, 0); + if (!IS_ERR(pdev)) + return 0; + + platform_driver_unregister(&hw_prober_driver); + + return PTR_ERR(pdev); +} +device_initcall(hw_prober_driver_init);