From patchwork Thu Aug 8 09:59:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen-Yu Tsai X-Patchwork-Id: 13757187 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E473FC3DA4A for ; Thu, 8 Aug 2024 10:01:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=lnHNs8RGUpugUdHA2OImjbnnj6+rNsZeKGSTnE9NGbI=; b=lHHEtjs8lhtDzYSSvt43fmKH/h 1+fnyRGQ35/XYxJE+nHzyiiUdNcV7BVN1ysWZV8LAutMgNMSztR1E0zP7oNOUA739xzqESCHqhm1M AznaHHIhgxLR7JBb8lnZgMVRULBXz2AAuxXdbsgwBfECDfUPyknK+Wl4/asGVpp8chZv8YixMGnRX mckXMqhdQ8FunxNvE2csyxfDPu+ed1q5JWP4yBZ2X5cIKAxihQ1FY7nq2ljqgiZV31MIKXp8CWi/U ZqUV/6o1LN5RlHjExMFMDeVG8CgQsWmTDc1591obt2ZNkqEp2fus1pmSjLnFD/GR8KmbVTq2FSv57 sdIxK75g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbzx1-00000007oKg-3yZK; Thu, 08 Aug 2024 10:00:55 +0000 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbzvw-00000007o0H-31Ga for linux-arm-kernel@lists.infradead.org; Thu, 08 Aug 2024 09:59:51 +0000 Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-1fb3b7d0d56so5331655ad.1 for ; Thu, 08 Aug 2024 02:59:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1723111187; x=1723715987; darn=lists.infradead.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=lnHNs8RGUpugUdHA2OImjbnnj6+rNsZeKGSTnE9NGbI=; b=QAQ/dGFcZJU1Xzz8NQlG5U+wVEG526Zu41a5m6zgRV3JEp1DEHS27La36P3nALy6hK NBhTHF4c9o3dNQfEMjnABPXy0mPin9pLnRZs/KuyCOdsndYKG+NeyzGV7GzzsjLADjKS /u5O/237QXHA30gI6mfhhBl90SHvYDCILf+kc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723111187; x=1723715987; 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=lnHNs8RGUpugUdHA2OImjbnnj6+rNsZeKGSTnE9NGbI=; b=VTiOfje4zscjFOUc/s70rnG/J/WqK7rsGv+XHdYCfJMNyk50JnrWdNXpge6of6JvPA sEsQr0cwomW2+zvoH5T+wEh2fqKNrlD8cagRbO+/Yu8wtvYDyJs4DJzywt+zV0fc8v6k P17nCmkjORxVZcK9Uw8NkqFG8GuGbnqmW+3sfbJduypOBd/UMaqcscXG/C+1JV0IsAf2 jGNCArvvSVWclZZ8FVsZ3udGV7aJmvXgDnyZu+fx+4qbVUj/UbNMV7koYCMnBFeai1ji z7GYJMS+KN7d1tNcqQrHdtLqI1h34MUrgYogggJEM2gBXc31RrgaqNqaMO3Y58hWxFQ6 ZM7A== X-Forwarded-Encrypted: i=1; AJvYcCXYlQPyxCCOZy7wqiYiqzUK1A7Y2mRymbpQWgRBD7KDtzPRS2Mz/2GavKxWEiAj1LAQgTm7hz3nsP5d0WmFOe8BnAqtQ0IohvJGHpi6OmuHOKv1P8s= X-Gm-Message-State: AOJu0YzRAK06BUV55dlVTGpWBZLJUFmxmcOkys0QjAKDnwIm1isIwkur bIHSj1QbT8MJJcpywHVLylg99DjMyh/SyPMU9YME70W/gZLLQNgmZqpcEF40/A== X-Google-Smtp-Source: AGHT+IFIftWwmlbWRkGBzfDMk0j7k5O8rm8KVgSziXqkMdFzRmZhlp6LCbkz+1PDSOvY9LkVZKP1dQ== X-Received: by 2002:a17:902:ec8c:b0:1fd:9c2d:2f27 with SMTP id d9443c01a7336-2009524a0edmr15321085ad.24.1723111187412; Thu, 08 Aug 2024 02:59:47 -0700 (PDT) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:8b53:87e6:914:a00d]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59297707sm120784985ad.254.2024.08.08.02.59.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Aug 2024 02:59:47 -0700 (PDT) From: Chen-Yu Tsai To: Rob Herring , Saravana Kannan , Matthias Brugger , AngeloGioacchino Del Regno , Wolfram Sang , Benson Leung , Tzung-Bi Shih , Mark Brown , Liam Girdwood Cc: Chen-Yu Tsai , 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 , Johan Hovold , Jiri Kosina , Andy Shevchenko , linux-i2c@vger.kernel.org Subject: [PATCH v4 1/6] of: dynamic: Add of_changeset_update_prop_string Date: Thu, 8 Aug 2024 17:59:24 +0800 Message-ID: <20240808095931.2649657-2-wenst@chromium.org> X-Mailer: git-send-email 2.46.0.rc2.264.g509ed76dc8-goog In-Reply-To: <20240808095931.2649657-1-wenst@chromium.org> References: <20240808095931.2649657-1-wenst@chromium.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240808_025948_791510_6F229B93 X-CRM114-Status: GOOD ( 17.34 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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. Signed-off-by: Chen-Yu Tsai --- 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(+) diff --git a/drivers/of/dynamic.c b/drivers/of/dynamic.c index 110104a936d9..dfd1358d59d5 100644 --- a/drivers/of/dynamic.c +++ b/drivers/of/dynamic.c @@ -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; + + prop.name = (char *)prop_name; + prop.length = strlen(str) + 1; + prop.value = (void *)str; + + return of_changeset_update_prop_helper(ocs, np, &prop); +} +EXPORT_SYMBOL_GPL(of_changeset_update_prop_string); diff --git a/include/linux/of.h b/include/linux/of.h index 85b60ac9eec5..046283be1cd3 100644 --- a/include/linux/of.h +++ b/include/linux/of.h @@ -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); From patchwork Thu Aug 8 09:59:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen-Yu Tsai X-Patchwork-Id: 13757188 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 810FCC3DA4A for ; Thu, 8 Aug 2024 10:01:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=wR1Udf/FWl7py4H5RQqWMXM0U9PswySd1hls6tNxB7g=; b=K272JYu9VgcjvGQ+ttXg2IcLDQ TnbEz3RW9TL2agPnGXAElHfQUfD0AFGoct+FXe6yvnrLiL1OBfwrpbOn1uaWepOkNQYU6ozlMPiWk hXhFUXZMdAT2SPCoCvZGHw7mWdn/8rHJVcntXCjZ/ciFoDEuXanmMyrptNUiLli/KoY4gfZo31SWg IWQdj6w+/EiKB20p51HhBQ04Q27Fa4jDyliSdhxiv4TSrAglXRFsJ+oLC/V4+isxT+eyy8Riw6DPd Ed0BMLA/xjY926xl+iWPO6Io6ixBo540mUolzBb24CIjAMi7nMvniPCtBWq+ly3nxuz1orGt2jPIB ec1qbkvg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbzxa-00000007oWR-2HEl; Thu, 08 Aug 2024 10:01:30 +0000 Received: from mail-pl1-x62a.google.com ([2607:f8b0:4864:20::62a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbzw0-00000007o1Z-0qtR for linux-arm-kernel@lists.infradead.org; Thu, 08 Aug 2024 09:59:53 +0000 Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-1fee6435a34so7385765ad.0 for ; Thu, 08 Aug 2024 02:59:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1723111191; x=1723715991; darn=lists.infradead.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=wR1Udf/FWl7py4H5RQqWMXM0U9PswySd1hls6tNxB7g=; b=MphKt03gOtnaSH0XTncSAbxGxD38EQ6fIf9zV8tkNPb4hBmlsB/GiJAMBD31JHNdSD p9YOjc9VNmGqplOGw9oD2PNALdncmfIZvpKXZHe0UdUUMHk9VDjjoIhZSRznxIuYiYlI Ow9X3hkq4KwoBSjN8FT7NZe7YcK9magsuamfo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723111191; x=1723715991; 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=wR1Udf/FWl7py4H5RQqWMXM0U9PswySd1hls6tNxB7g=; b=hiLP/gNiOwmPlVW3jZUc6OyF3RVRsT6IKGs0zI53T/v1C+3YRvwluyggjpw2sH0NtE Y6f/MGJLQx27S53f83txO6FXSH2Pc18hCtqI5v44dhHSnwL18uCOGCDkfbQRyqq3VLxe cqRQQUg9M9iTjPmtIa+nL/lyUnEFm7j1SCPmHu9lZvxqzixRNVpZUqs7/xYLlfsxLXbY cckP5tDfwyVCgnBDuYzPEUp1Tw/z4Slb1npzn14ylSLZpJzdtZMO5lJTZd1yiWhrgbLm 0ERR29YmNi399Y1v2B9DxKkhPcV8IALGIxtdEQK+iNQF/2gwrWPUg7fvlDFb7TiBhM+5 3mKw== X-Forwarded-Encrypted: i=1; AJvYcCW/4EEtzo3tca/3uVkZto4lY8Frij3q2bSn3SUR/yZPJS+NLljegmMrG2Zquar76ITOntX9Tsx0JJEj8zXWWrFP07agDQN27i3iCVkYS5YQZm+icT8= X-Gm-Message-State: AOJu0YwRHVV4Y0omKw9yY+ksMcA+plnoRKXAuW1ycXgKQtRMCCb5griG Izr1EDc5o3CZmA3u1kBeZe++zR59T4DnyLIBoNQSv5LE/4oopUgs7q2s6FPOEg== X-Google-Smtp-Source: AGHT+IHWAl+hZjv9nSvJy1SKpxCjwWYJJknIPCWUxWfBsX+zsM0IhYTrybxxtL2FsiZEr+fCR0sgcA== X-Received: by 2002:a17:903:40c5:b0:1fd:d55b:df26 with SMTP id d9443c01a7336-200952e9a8bmr13508545ad.61.1723111190903; Thu, 08 Aug 2024 02:59:50 -0700 (PDT) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:8b53:87e6:914:a00d]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59297707sm120784985ad.254.2024.08.08.02.59.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Aug 2024 02:59:50 -0700 (PDT) From: Chen-Yu Tsai To: Rob Herring , Saravana Kannan , Matthias Brugger , AngeloGioacchino Del Regno , Wolfram Sang , Benson Leung , Tzung-Bi Shih , Mark Brown , Liam Girdwood Cc: Chen-Yu Tsai , 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 , Johan Hovold , Jiri Kosina , Andy Shevchenko , linux-i2c@vger.kernel.org Subject: [PATCH v4 2/6] regulator: Add regulator_of_get_optional() for pure DT regulator lookup Date: Thu, 8 Aug 2024 17:59:25 +0800 Message-ID: <20240808095931.2649657-3-wenst@chromium.org> X-Mailer: git-send-email 2.46.0.rc2.264.g509ed76dc8-goog In-Reply-To: <20240808095931.2649657-1-wenst@chromium.org> References: <20240808095931.2649657-1-wenst@chromium.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240808_025952_255173_604DD8BF X-CRM114-Status: GOOD ( 29.31 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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. This adds a new regulator_of_get_optional() for this purpose. The underlying code that support the existing regulator_get*() functions are extended to support this specific case. Signed-off-by: Chen-Yu Tsai --- Changes since v3: - New patch --- drivers/regulator/core.c | 81 ++++++++++++++++++++++-------- drivers/regulator/devres.c | 2 +- drivers/regulator/internal.h | 2 +- include/linux/regulator/consumer.h | 8 +++ 4 files changed, 69 insertions(+), 24 deletions(-) diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c index 7674b7f2df14..ba4542d76642 100644 --- a/drivers/regulator/core.c +++ b/drivers/regulator/core.c @@ -456,30 +456,29 @@ static struct device_node *of_get_child_regulator(struct device_node *parent, /** * of_get_regulator - get a regulator device node based on supply name - * @dev: Device pointer for the consumer (of regulator) device + * @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. */ -static struct device_node *of_get_regulator(struct device *dev, const char *supply) +static struct device_node *of_get_regulator(struct device_node *node, 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); + pr_debug("%pOF: Looking up %s-supply from device tree\n", node, supply); snprintf(prop_name, 64, "%s-supply", supply); - regnode = of_parse_phandle(dev->of_node, prop_name, 0); + regnode = of_parse_phandle(node, prop_name, 0); if (!regnode) { - regnode = of_get_child_regulator(dev->of_node, prop_name); + regnode = of_get_child_regulator(node, prop_name); if (regnode) return regnode; - dev_dbg(dev, "Looking up %s property in node %pOF failed\n", - prop_name, dev->of_node); + pr_debug("%pOF: Looking up %s property failed\n", node, prop_name); return NULL; } return regnode; @@ -1996,8 +1995,14 @@ static struct regulator_dev *regulator_lookup_by_name(const char *name) /** * regulator_dev_lookup - lookup a regulator device. * @dev: device for regulator "consumer". + * @node: device node for regulator supply lookup. + * Falls back to dev->of_node if NULL. * @supply: Supply name or regulator ID. * + * If dev is NULL and node is not NULL, a pure device tree lookup is assumed. + * That is, it will not use supply aliases and end after DT based lookup is + * done. + * * 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(). @@ -2006,21 +2011,30 @@ static struct regulator_dev *regulator_lookup_by_name(const char *name) * in the future. */ static struct regulator_dev *regulator_dev_lookup(struct device *dev, + struct device_node *node, const char *supply) { struct regulator_dev *r = NULL; - struct device_node *node; + struct device_node *regulator_node; struct regulator_map *map; const char *devname = NULL; + bool pure_dt_lookup = false; + + pure_dt_lookup = (node && !dev); - regulator_supply_alias(&dev, &supply); + /* Pure DT lookup should use given supply name directly */ + if (!pure_dt_lookup) + regulator_supply_alias(&dev, &supply); + + if (!node && dev && dev->of_node) + node = dev->of_node; /* 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 (node) { + regulator_node = of_get_regulator(node, supply); + if (regulator_node) { + r = of_find_regulator_by_node(regulator_node); + of_node_put(regulator_node); if (r) return r; @@ -2032,6 +2046,10 @@ static struct regulator_dev *regulator_dev_lookup(struct device *dev, } } + /* Pure DT lookup stops here. */ + if (pure_dt_lookup) + return ERR_PTR(-ENODEV); + /* if not found, try doing it non-dt way */ if (dev) devname = dev_name(dev); @@ -2076,7 +2094,7 @@ static int regulator_resolve_supply(struct regulator_dev *rdev) if (rdev->supply) return 0; - r = regulator_dev_lookup(dev, rdev->supply_name); + r = regulator_dev_lookup(dev, NULL, rdev->supply_name); if (IS_ERR(r)) { ret = PTR_ERR(r); @@ -2169,8 +2187,8 @@ static int regulator_resolve_supply(struct regulator_dev *rdev) } /* Internal regulator request function */ -struct regulator *_regulator_get(struct device *dev, const char *id, - enum regulator_get_type get_type) +struct regulator *_regulator_get(struct device *dev, struct device_node *node, + const char *id, enum regulator_get_type get_type) { struct regulator_dev *rdev; struct regulator *regulator; @@ -2187,7 +2205,7 @@ struct regulator *_regulator_get(struct device *dev, const char *id, return ERR_PTR(-EINVAL); } - rdev = regulator_dev_lookup(dev, id); + rdev = regulator_dev_lookup(dev, node, id); if (IS_ERR(rdev)) { ret = PTR_ERR(rdev); @@ -2318,7 +2336,7 @@ struct regulator *_regulator_get(struct device *dev, const char *id, */ struct regulator *regulator_get(struct device *dev, const char *id) { - return _regulator_get(dev, id, NORMAL_GET); + return _regulator_get(dev, NULL, id, NORMAL_GET); } EXPORT_SYMBOL_GPL(regulator_get); @@ -2345,7 +2363,7 @@ EXPORT_SYMBOL_GPL(regulator_get); */ struct regulator *regulator_get_exclusive(struct device *dev, const char *id) { - return _regulator_get(dev, id, EXCLUSIVE_GET); + return _regulator_get(dev, NULL, id, EXCLUSIVE_GET); } EXPORT_SYMBOL_GPL(regulator_get_exclusive); @@ -2371,10 +2389,29 @@ EXPORT_SYMBOL_GPL(regulator_get_exclusive); */ struct regulator *regulator_get_optional(struct device *dev, const char *id) { - return _regulator_get(dev, id, OPTIONAL_GET); + return _regulator_get(dev, NULL, id, OPTIONAL_GET); } EXPORT_SYMBOL_GPL(regulator_get_optional); +/** + * regulator_of_get_optional - get optional regulator via device tree lookup + * @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(). + */ +struct regulator *regulator_of_get_optional(struct device_node *node, const char *id) +{ + return _regulator_get(NULL, node, id, OPTIONAL_GET); +} +EXPORT_SYMBOL_GPL(regulator_of_get_optional); + static void destroy_regulator(struct regulator *regulator) { struct regulator_dev *rdev = regulator->rdev; @@ -4928,7 +4965,7 @@ int _regulator_bulk_get(struct device *dev, int num_consumers, consumers[i].consumer = NULL; for (i = 0; i < num_consumers; i++) { - consumers[i].consumer = _regulator_get(dev, + consumers[i].consumer = _regulator_get(dev, NULL, consumers[i].supply, get_type); if (IS_ERR(consumers[i].consumer)) { ret = dev_err_probe(dev, PTR_ERR(consumers[i].consumer), diff --git a/drivers/regulator/devres.c b/drivers/regulator/devres.c index 7111c46e9de1..7d9ea8232959 100644 --- a/drivers/regulator/devres.c +++ b/drivers/regulator/devres.c @@ -28,7 +28,7 @@ static struct regulator *_devm_regulator_get(struct device *dev, const char *id, if (!ptr) return ERR_PTR(-ENOMEM); - regulator = _regulator_get(dev, id, get_type); + regulator = _regulator_get(dev, NULL, id, get_type); if (!IS_ERR(regulator)) { *ptr = regulator; devres_add(dev, ptr); diff --git a/drivers/regulator/internal.h b/drivers/regulator/internal.h index 77a502141089..51eb552cba01 100644 --- a/drivers/regulator/internal.h +++ b/drivers/regulator/internal.h @@ -120,7 +120,7 @@ enum regulator_get_type { MAX_GET_TYPE }; -struct regulator *_regulator_get(struct device *dev, const char *id, +struct regulator *_regulator_get(struct device *dev, struct device_node *node, const char *id, enum regulator_get_type get_type); int _regulator_bulk_get(struct device *dev, int num_consumers, struct regulator_bulk_data *consumers, enum regulator_get_type get_type); diff --git a/include/linux/regulator/consumer.h b/include/linux/regulator/consumer.h index d986ec13092e..76826d0d99f1 100644 --- a/include/linux/regulator/consumer.h +++ b/include/linux/regulator/consumer.h @@ -168,6 +168,8 @@ int devm_regulator_get_enable_read_voltage(struct device *dev, const char *id); void regulator_put(struct regulator *regulator); void devm_regulator_put(struct regulator *regulator); +struct regulator *__must_check regulator_of_get_optional(struct device_node *node, const char *id); + int regulator_register_supply_alias(struct device *dev, const char *id, struct device *alias_dev, const char *alias_id); @@ -358,6 +360,12 @@ static inline void devm_regulator_put(struct regulator *regulator) { } +static inline struct regulator *__must_check +regulator_of_get_optional(struct device_node *node, const char *id) +{ + return ERR_PTR(-ENODEV); +} + static inline void devm_regulator_bulk_put(struct regulator_bulk_data *consumers) { } From patchwork Thu Aug 8 09:59:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen-Yu Tsai X-Patchwork-Id: 13757189 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id F177EC3DA4A for ; Thu, 8 Aug 2024 10:02:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=8qgCZ39zaMZ1pHgoNfFZBo55h8VlLpIWFFJKzWV3ijA=; b=S5IBRfH0quGCpdB0zUJYRSyWke zSg1VYSoQvEkLUvsuPAvcVqFBLN4bmm4J4vyo0IO8ctdrAuY30S+R2/2aS36/mrvuJbDgRRKJB+Q3 J4/Un0iwmESYV0EgWWmZkpgV6fm3KFXni4ILDqO6xMebB83lfvrPsblqJDN+b5pgEG/8QKv97Nd7U h4o8LVw5zzPtiTK/pcdxuVCEmknqXep6iqCgp6YHefjnHhkmC1VquhpbGw+gRjLCXD5sIu/bMHKpF nz9w/TBr2sASfKU9mLIb2WdmfRSJwxG5WkASqrTtQvruGMzrhFbX+ivVIUHfqiYaHyOD1ftlIDivC 9Xv6vqyQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbzy7-00000007ogL-0Pso; Thu, 08 Aug 2024 10:02:03 +0000 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbzw3-00000007o2c-3pEq for linux-arm-kernel@lists.infradead.org; Thu, 08 Aug 2024 09:59:57 +0000 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-1fd6ed7688cso8193145ad.3 for ; Thu, 08 Aug 2024 02:59:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1723111195; x=1723715995; darn=lists.infradead.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=8qgCZ39zaMZ1pHgoNfFZBo55h8VlLpIWFFJKzWV3ijA=; b=O+Fq2KM5bIzmMmztkqGgHe0YvZQAt14lLxBxhqDlOAuRuypDOvbDEH3nuSwi4NEi8F 3dIuiY3MCAT0E+mLxRCadqrgq+Uwz7VoNy5BXC6V0IehJemuYHaV+Y2De64UpGi8RqKS C3Of5ukPvQhHxVpK7TFgVq0LiUnI97l6J9hj4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723111195; x=1723715995; 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=8qgCZ39zaMZ1pHgoNfFZBo55h8VlLpIWFFJKzWV3ijA=; b=kAkmI+aq96qT07scpItwb0rxIKqNVIwwQDHo2YSoj9uIb59GSvFucUeVHojaOJiRjp COuWTmRDF6IGsAVfSDorBqQ/zZp+iXvHvqk+/78ABxjVQk4OxMClfnVzkR6L+Z9aNqEX 1T+RyR8S2AtWF5TDk+iP4OtXsYoH2Vae3A+lKPzGbczcSt0b02HZWGSlLJY/MaUpxv4h 3YvDo3Xc+lJ2iMCptIpw5qkDL+CFEGhHlPO5Lr3H4JlmhiG+cxmeICa0VD1kAW6GWjNP uDf9jSxcRzjr4GzNHwBOBHBJNWattHUFYNLJ7aZ+h8sn53nBrmNf+1/lhl2V0Bd/JkpQ edZQ== X-Forwarded-Encrypted: i=1; AJvYcCVYEY/Lk0+A2YkTavg18eCaEBtWdN7Yy4+twIG1/9cCYfHhAKDxzf0ViqM4Dodyt40tGeYfFGSwP5nFaj/bjmkCqXpjYZsbr+n8vUQGhQWEgYimVt0= X-Gm-Message-State: AOJu0YzQ9ADfDnp1UaTTSyhYeQROlASuW+lBLZOsVFSTYULMfc0xdcUo Ac11gW6ZpdnkWPKnlWjJJUJ92hxtCTMpoDsjiFfMQx247ej8amN4dwXJSIwR8A== X-Google-Smtp-Source: AGHT+IEKKGr+vmamMIgjjXNkXa4rHYEBsgcb5kjNPWWGBvxBXx4ElZWMNIirkJNIPkRsY8kGV6DcSg== X-Received: by 2002:a17:903:8ce:b0:1fb:7530:30be with SMTP id d9443c01a7336-2009527d432mr18198885ad.38.1723111194535; Thu, 08 Aug 2024 02:59:54 -0700 (PDT) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:8b53:87e6:914:a00d]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59297707sm120784985ad.254.2024.08.08.02.59.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Aug 2024 02:59:54 -0700 (PDT) From: Chen-Yu Tsai To: Rob Herring , Saravana Kannan , Matthias Brugger , AngeloGioacchino Del Regno , Wolfram Sang , Benson Leung , Tzung-Bi Shih , Mark Brown , Liam Girdwood Cc: Chen-Yu Tsai , 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 , Johan Hovold , Jiri Kosina , Andy Shevchenko , linux-i2c@vger.kernel.org Subject: [PATCH v4 3/6] i2c: Introduce OF component probe function Date: Thu, 8 Aug 2024 17:59:26 +0800 Message-ID: <20240808095931.2649657-4-wenst@chromium.org> X-Mailer: git-send-email 2.46.0.rc2.264.g509ed76dc8-goog In-Reply-To: <20240808095931.2649657-1-wenst@chromium.org> References: <20240808095931.2649657-1-wenst@chromium.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240808_025955_988444_D5604E39 X-CRM114-Status: GOOD ( 41.16 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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 --- 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 | 155 +++++++++++++++++++++++++++++++ include/linux/i2c.h | 4 + 3 files changed, 160 insertions(+) create mode 100644 drivers/i2c/i2c-core-of-prober.c diff --git a/drivers/i2c/Makefile b/drivers/i2c/Makefile index 3f71ce4711e3..0c543c742895 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..08aa736cc7a9 --- /dev/null +++ b/drivers/i2c/i2c-core-of-prober.c @@ -0,0 +1,155 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Linux I2C core OF component prober code + * + * Copyright (C) 2024 Google LLC + */ + +#include +#include +#include +#include +#include +#include + +/* + * 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 and GPIOs. + * - Support I2C muxes + */ + +/** + * 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 device_node *node, *i2c_node; + struct i2c_adapter *i2c; + struct of_changeset *ocs = NULL; + int ret; + + node = of_find_node_by_name(NULL, type); + if (!node) + return dev_err_probe(dev, -ENODEV, "Could not find %s device node\n", type); + + i2c_node = of_get_next_parent(node); + if (!of_node_name_eq(i2c_node, "i2c")) { + of_node_put(i2c_node); + return dev_err_probe(dev, -EINVAL, "%s device isn't on I2C bus\n", type); + } + + if (!of_device_is_available(i2c_node)) { + of_node_put(i2c_node); + return dev_err_probe(dev, -ENODEV, "I2C controller not available\n"); + } + + for_each_child_of_node(i2c_node, node) { + if (!of_node_name_prefix(node, type)) + continue; + if (of_device_is_available(node)) { + /* + * Device tree has component already enabled. Either the + * device tree isn't supported or we already probed once. + */ + 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(dev, -EPROBE_DEFER, "Couldn't get I2C adapter\n"); + } + + ret = 0; + for_each_child_of_node(i2c_node, node) { + union i2c_smbus_data data; + u32 addr; + + if (!of_node_name_prefix(node, type)) + 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; + + break; + } + + /* Found a device that is responding */ + if (node) { + dev_info(dev, "Enabling %pOF\n", node); + + ocs = kzalloc(sizeof(*ocs), GFP_KERNEL); + if (!ocs) { + ret = -ENOMEM; + goto err_put_node; + } + + of_changeset_init(ocs); + ret = of_changeset_update_prop_string(ocs, node, "status", "okay"); + if (ret) + goto err_free_ocs; + ret = of_changeset_apply(ocs); + if (ret) + goto err_destroy_ocs; + + of_node_put(node); + } + + i2c_put_adapter(i2c); + of_node_put(i2c_node); + + return 0; + +err_destroy_ocs: + of_changeset_destroy(ocs); +err_free_ocs: + kfree(ocs); +err_put_node: + of_node_put(node); + i2c_put_adapter(i2c); + of_node_put(i2c_node); + return ret; +} +EXPORT_SYMBOL_GPL(i2c_of_probe_component); diff --git a/include/linux/i2c.h b/include/linux/i2c.h index 7eedd0c662da..0415d5d0f6ac 100644 --- a/include/linux/i2c.h +++ b/include/linux/i2c.h @@ -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) From patchwork Thu Aug 8 09:59:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen-Yu Tsai X-Patchwork-Id: 13757190 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3CFFFC52D71 for ; Thu, 8 Aug 2024 10:02:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=BMQzDBFwtvtL+uRpCqzQYWzmcXr00tCiZCE6wRprzyk=; b=x064xqlSKQJcdNcyug/oLq+tEA qPYGasr7CqFvou0MtLWgSAj4eKwn5ZZFD3CNQ2QOfBkhqBkqi9flYUJOZg5MOfM1ftY73d3SYJp9Z 7WNtiCMGfLf5TeSIYmPYhu2v0SautifJ+QcfKPe6DRqcmq794itaC0gBwhyQBLLWnqaRVPA6Ufv0L chjioMl5G8Q4Z/yh0kGZ/mVn9pQGWO527Kkm0AY+lTWIC0H2ffeTN1z7tzOFG4GeKmt4m78wK4wnQ rwE5PniQL/6d5JkabcSEbT48GCw9xH2vv5qYOKHVMstTenPrGtCHSfFr3KGje/VRwew6ZU/W7VZAo pnnSe7Dw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbzyp-00000007owV-0nxa; Thu, 08 Aug 2024 10:02:47 +0000 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbzw7-00000007o4A-1cdz for linux-arm-kernel@lists.infradead.org; Thu, 08 Aug 2024 10:00:01 +0000 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-1fda7fa60a9so7795375ad.3 for ; Thu, 08 Aug 2024 02:59:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1723111198; x=1723715998; darn=lists.infradead.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=BMQzDBFwtvtL+uRpCqzQYWzmcXr00tCiZCE6wRprzyk=; b=P5N6tCpvZCJ5JIeslwQYwwqc3Z4omD1un6IoJBnzjPOfWo0CbKvtJW6N8JaGNLaCbb ORM8N7H2pT4zCU54DAPxnLnNATltBV7q78SuoxSDDKNUpyFag4mRo2qsheV46k4b+brl OeY+J59wsuC1yc+KKsGo/vFW8prAyv1HDmaTw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723111198; x=1723715998; 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=BMQzDBFwtvtL+uRpCqzQYWzmcXr00tCiZCE6wRprzyk=; b=bgDRbv8MZlJte4r7KuQkY8eHjhdQM+A7Zr0scoFSUGp3UQy+MJkCcruj3YGaQmZKK/ 5DjZsQz1kBca9kvS88VWsqDs90cPl1f/8zIQVohv9BIMb6/NrQAKEhZ8JzhLOUbJlaqW 8VCEV3UYaoN5hBEWlpBRqvY7yBUt4YxqxVNjc6/OhiBqeChDdfEHWYX23mcwUmg9WQUJ 3skAtDdr+U7T4IffJrB3az9/KoXvy5xRKhhf0/aaKWKkU2OtL0j5DHhfm7rVNsfbBPyo syMwUhZRxMths55QYJaR9wD9EgY+X3JeQnzJ0F86QN+Ig1VZnD0q/WdwCH0F7Ne7eGo6 0btA== X-Forwarded-Encrypted: i=1; AJvYcCVvttifY0mSoKD/OX7TXfWEIm95sPBvFDjJ/BCdxih07Q5Hb4RO5O6rnef2vieHXoONWVr+5VWWVXhqXQFjskAy77sKWNhRMpet05+PhYU9COCTGHU= X-Gm-Message-State: AOJu0YyS1GzCohWosq5GUBmlQVrN/uP2LK1oACf5vT11FpPHoO9v6gQt 7aBqAkFn8bM/t2r6cCRuP/bAqrN3Gs9vmk6d0sAJSYlZZxSD8OVA/tRwPrj7Ug== X-Google-Smtp-Source: AGHT+IFpIGcwhRChfmS6FgNQKtKkh03/uSPOBA07xstWnAMz5Rv8gBIXh5/7TokHUzCHGoOUlZXHWw== X-Received: by 2002:a17:903:2288:b0:1fa:2b89:f549 with SMTP id d9443c01a7336-20095224c09mr17452045ad.10.1723111198191; Thu, 08 Aug 2024 02:59:58 -0700 (PDT) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:8b53:87e6:914:a00d]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59297707sm120784985ad.254.2024.08.08.02.59.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Aug 2024 02:59:57 -0700 (PDT) From: Chen-Yu Tsai To: Rob Herring , Saravana Kannan , Matthias Brugger , AngeloGioacchino Del Regno , Wolfram Sang , Benson Leung , Tzung-Bi Shih , Mark Brown , Liam Girdwood Cc: Chen-Yu Tsai , 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 , Johan Hovold , Jiri Kosina , Andy Shevchenko , linux-i2c@vger.kernel.org Subject: [PATCH v4 4/6] i2c: of-prober: Add GPIO and regulator support Date: Thu, 8 Aug 2024 17:59:27 +0800 Message-ID: <20240808095931.2649657-5-wenst@chromium.org> X-Mailer: git-send-email 2.46.0.rc2.264.g509ed76dc8-goog In-Reply-To: <20240808095931.2649657-1-wenst@chromium.org> References: <20240808095931.2649657-1-wenst@chromium.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240808_025959_440676_9A8D140C X-CRM114-Status: GOOD ( 32.67 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org This adds GPIO and 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. 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. Signed-off-by: Chen-Yu Tsai --- 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 | 272 ++++++++++++++++++++++++++++++- 1 file changed, 271 insertions(+), 1 deletion(-) diff --git a/drivers/i2c/i2c-core-of-prober.c b/drivers/i2c/i2c-core-of-prober.c index 08aa736cc7a9..76d26768e12c 100644 --- a/drivers/i2c/i2c-core-of-prober.c +++ b/drivers/i2c/i2c-core-of-prober.c @@ -5,11 +5,14 @@ * Copyright (C) 2024 Google LLC */ +#include #include #include +#include #include #include #include +#include #include /* @@ -25,10 +28,242 @@ * address responds. * * TODO: - * - Support handling common regulators and GPIOs. * - Support I2C muxes */ +/* + * While 8 seems like a small number, especially when probing many component + * options, in practice all the options will have the same resources. The + * code getting the resources below does deduplication to avoid conflicts. + */ +#define RESOURCE_MAX 8 + +struct i2c_of_probe_data { + struct of_phandle_args gpio_phandles[RESOURCE_MAX]; + unsigned int gpio_phandles_num; + struct gpio_desc *gpiods[RESOURCE_MAX]; + unsigned int gpiods_num; + struct regulator *regulators[RESOURCE_MAX]; + unsigned int regulators_num; +}; + +#define REGULATOR_SUFFIX "-supply" + +/* Returns 1 if regulator found for property, 0 if not, or error. */ +static int i2c_of_probe_get_regulator(struct device_node *node, struct property *prop, + struct i2c_of_probe_data *data) +{ + struct regulator *regulator = NULL; + char con[32]; /* 32 is max size of property name */ + char *p; + + p = strstr(prop->name, REGULATOR_SUFFIX); + if (!p) + return 0; + + if (strcmp(p, REGULATOR_SUFFIX)) + return 0; + + strscpy(con, prop->name, p - prop->name + 1); + regulator = regulator_of_get_optional(node, con); + /* DT lookup should never return -ENODEV */ + if (IS_ERR(regulator)) + return PTR_ERR(regulator); + + for (int i = 0; i < data->regulators_num; i++) + if (regulator_is_equal(regulator, data->regulators[i])) { + regulator_put(regulator); + regulator = NULL; + break; + } + + if (!regulator) + return 1; + + if (data->regulators_num == ARRAY_SIZE(data->regulators)) { + regulator_put(regulator); + return -ENOMEM; + } + + data->regulators[data->regulators_num++] = regulator; + + return 1; +} + +#define GPIO_SUFFIX "-gpio" + +/* Returns 1 if GPIO found for property, 0 if not, or error. */ +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_desc *gpiod = NULL; + char con[32]; /* 32 is max size of property name */ + char *con_id = NULL; + char *p; + struct of_phandle_args phargs; + int ret; + bool duplicate_found; + + p = strstr(prop->name, GPIO_SUFFIX); + if (p) { + strscpy(con, prop->name, p - prop->name + 1); + con_id = con; + } else if (strcmp(prop->name, "gpio") && strcmp(prop->name, "gpios")) { + return 0; + } + + ret = of_parse_phandle_with_args_map(node, prop->name, "gpio", 0, &phargs); + if (ret) + return ret; + + /* + * 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). Instead we check the parsed phandle + * for duplicates. Ignore the flags (the last arg) in this case. + */ + phargs.args[phargs.args_count - 1] = 0; + duplicate_found = false; + for (int i = 0; i < data->gpio_phandles_num; i++) + if (of_phandle_args_equal(&phargs, &data->gpio_phandles[i])) { + duplicate_found = true; + break; + } + + if (duplicate_found) { + of_node_put(phargs.np); + return 1; + } + + gpiod = fwnode_gpiod_get_index(fwnode, con_id, 0, GPIOD_ASIS, "i2c-of-prober"); + if (IS_ERR(gpiod)) { + of_node_put(phargs.np); + return PTR_ERR(gpiod); + } + + if (data->gpiods_num == ARRAY_SIZE(data->gpiods)) { + of_node_put(phargs.np); + gpiod_put(gpiod); + return -ENOMEM; + } + + memcpy(&data->gpio_phandles[data->gpio_phandles_num++], &phargs, sizeof(phargs)); + data->gpiods[data->gpiods_num++] = 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) +{ + for (int i = data->gpio_phandles_num - 1; i >= 0; i--) + of_node_put(data->gpio_phandles[i].np); + data->gpio_phandles_num = 0; + + for (int i = data->gpiods_num - 1; i >= 0; i--) + gpiod_put(data->gpiods[i]); + data->gpiods_num = 0; +} + +static void i2c_of_probe_free_res(struct i2c_of_probe_data *data) +{ + i2c_of_probe_free_gpios(data); + + for (int i = data->regulators_num; i >= 0; i--) + regulator_put(data->regulators[i]); + data->regulators_num = 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; + + for_each_property_of_node(node, prop) { + dev_dbg(dev, "Trying property %pOF/%s\n", node, prop->name); + + /* regulator supplies */ + ret = i2c_of_probe_get_regulator(node, prop, data); + if (ret > 0) + continue; + if (ret < 0) { + dev_err_probe(dev, ret, "Failed to get regulator supply from %pOF/%s\n", + node, prop->name); + goto err_cleanup; + } + + /* 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: + 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; + int reg_i, gpio_i; + + dev_dbg(dev, "Enabling resources\n"); + + for (reg_i = 0; reg_i < data->regulators_num; reg_i++) { + dev_dbg(dev, "Enabling regulator %d\n", reg_i); + ret = regulator_enable(data->regulators[reg_i]); + if (ret) + goto disable_regulators; + } + + /* largest post-power-on pre-reset-deassert delay seen among drivers */ + msleep(500); + + for (gpio_i = 0; gpio_i < data->gpiods_num; 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[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[gpio_i], 0); +disable_regulators: + for (reg_i--; reg_i >= 0; reg_i--) + regulator_disable(data->regulators[reg_i]); + + return ret; +} + +static void i2c_of_probe_disable_regulators(struct i2c_of_probe_data *data) +{ + for (int i = data->regulators_num - 1; i >= 0; i--) + regulator_disable(data->regulators[i]); +} + /** * 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 @@ -64,6 +299,7 @@ int i2c_of_probe_component(struct device *dev, const char *type) struct device_node *node, *i2c_node; struct i2c_adapter *i2c; struct of_changeset *ocs = NULL; + struct i2c_of_probe_data data = {0}; int ret; node = of_find_node_by_name(NULL, type); @@ -101,6 +337,34 @@ int i2c_of_probe_component(struct device *dev, const char *type) 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, &data); + if (ret) { + of_node_put(i2c_node); + return ret; + } + } + + dev_dbg(dev, "Resources: # of GPIOs = %d, # of regulator supplies = %d\n", + data.gpiods_num, data.regulators_num); + + /* Enable resources */ + ret = i2c_of_probe_enable_res(dev, &data); + if (ret) { + i2c_of_probe_free_res(&data); + of_node_put(i2c_node); + return dev_err_probe(dev, ret, "Failed to enable resources\n"); + } + ret = 0; for_each_child_of_node(i2c_node, node) { union i2c_smbus_data data; @@ -116,6 +380,8 @@ int i2c_of_probe_component(struct device *dev, const char *type) break; } + i2c_of_probe_free_gpios(&data); + /* Found a device that is responding */ if (node) { dev_info(dev, "Enabling %pOF\n", node); @@ -137,6 +403,8 @@ int i2c_of_probe_component(struct device *dev, const char *type) of_node_put(node); } + i2c_of_probe_disable_regulators(&data); + i2c_of_probe_free_res(&data); i2c_put_adapter(i2c); of_node_put(i2c_node); @@ -148,6 +416,8 @@ int i2c_of_probe_component(struct device *dev, const char *type) kfree(ocs); err_put_node: of_node_put(node); + i2c_of_probe_disable_regulators(&data); + i2c_of_probe_free_res(&data); i2c_put_adapter(i2c); of_node_put(i2c_node); return ret; From patchwork Thu Aug 8 09:59:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen-Yu Tsai X-Patchwork-Id: 13757191 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 41014C52D71 for ; Thu, 8 Aug 2024 10:03:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=IrJXguIlB/7qdbaZON+hnRiNa98pqQA+T1VHHt60EcI=; b=LGczzsV+8ks1ulu4frC5fWQIc2 /d2tclJbloSvpHrMD7Q6lyY/ggyY9L/w4l7TpPHqSoN9v/kNz7uM8k9H5YtLso6XgiCs5ZiVgFKzp NYKacpZ/mzQ+hvzBYDvmeveqOGmLIIUO+i0BTieH+JAHeVZ2+X/vng5gGXnf+2aNAPNO/8i300qyZ xTgJOiavwaoJ2KDkLX2jT8i+09MDabybIM3htgpBjrTU92a7DbRWrRYPXncqq58soOtZP2SqVod2p PzvpSX+Ae1bT6Ho+71hHWn+ZfJSK8UCpq1L2GvAnAue/hhq6eATYwlqFB0qeberTjjDbR1UZv39nZ PSZxK2Cg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbzzM-00000007p5o-0pnr; Thu, 08 Aug 2024 10:03:20 +0000 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbzwB-00000007o5A-1EU3 for linux-arm-kernel@lists.infradead.org; Thu, 08 Aug 2024 10:00:06 +0000 Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-1fc6ee64512so7411835ad.0 for ; Thu, 08 Aug 2024 03:00:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1723111202; x=1723716002; darn=lists.infradead.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=IrJXguIlB/7qdbaZON+hnRiNa98pqQA+T1VHHt60EcI=; b=Qy6SYL05XG06r36NXjx91HVa6EmQ11NC3yvn9NcapjlCE4xd+yIs/UoUmtik8r0sCP CE1BG2c4Bc++ewFx/qIjZS8KVEHql184VKqY/5bPwnlcdiJty/klVP6GMgcIlIFz4OPx +fu+UFIPAXP5ghEUGFYJ7b3GwGT76nlp9hEyE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723111202; x=1723716002; 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=IrJXguIlB/7qdbaZON+hnRiNa98pqQA+T1VHHt60EcI=; b=PlF8naMpEdm2HRTmuqzsz6RlUfsz9AhLedFV+J1B+SH5rt9C0nN71dwxG0wTLaRVJ+ h8mKeBeaoYL+HLj9DVxfQHPaYhh7dlwPVp1wr7YekE6BKHxgz/wW5JoPHHaMqNAPYOGq aUTHLJqJAMDFxbLr5Kk6WEFQz6VEFfQiXUt+RuCrJyoxJ+9wmWmtVeNmyJEzSoBzTIaj eKK3zRtq88fhuzcCtdBr67B9R6rjzPtWObkhFbUw3VvstrIGROF+E5ifjKsX38hgEMep 33iZXLP+Mg1SUr7yyAisj83TO23jhpcEBC5RvtCX+5UxhetGUjOXRykFGa22DVQ+9xbK jBnA== X-Forwarded-Encrypted: i=1; AJvYcCUaNCShA6DmxSnYGpUEbelNxhX47uSMFLlkrNbH5Sx9/X7GbeJnWOJs5tXRIrefWXt6ymf6jzyAftQVMGjqTckB@lists.infradead.org X-Gm-Message-State: AOJu0Yy1tkaHXNLuuxjF26WBNYVI4rxJG33u89vUfpaJQvR5dogAf+DD kcnzhlGVZpx5kA5nuhTnb7HD1d+8f37cdB7isK92oydxQqcWz3g71SMk527W6g== X-Google-Smtp-Source: AGHT+IHGkeRBSVxrUTgdzlEanlCyL8c8BBRuZLzJUruiS+q3sQlgMYribQJMMsB438TZAjNNOrIc3A== X-Received: by 2002:a17:903:2406:b0:1fd:9105:7dd3 with SMTP id d9443c01a7336-20095311b9emr13251665ad.64.1723111201671; Thu, 08 Aug 2024 03:00:01 -0700 (PDT) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:8b53:87e6:914:a00d]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59297707sm120784985ad.254.2024.08.08.02.59.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Aug 2024 03:00:01 -0700 (PDT) From: Chen-Yu Tsai To: Rob Herring , Saravana Kannan , Matthias Brugger , AngeloGioacchino Del Regno , Wolfram Sang , Benson Leung , Tzung-Bi Shih , Mark Brown , Liam Girdwood Cc: Chen-Yu Tsai , 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 , Johan Hovold , Jiri Kosina , Andy Shevchenko , linux-i2c@vger.kernel.org Subject: [PATCH v4 5/6] platform/chrome: Introduce device tree hardware prober Date: Thu, 8 Aug 2024 17:59:28 +0800 Message-ID: <20240808095931.2649657-6-wenst@chromium.org> X-Mailer: git-send-email 2.46.0.rc2.264.g509ed76dc8-goog In-Reply-To: <20240808095931.2649657-1-wenst@chromium.org> References: <20240808095931.2649657-1-wenst@chromium.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240808_030003_742330_6525A439 X-CRM114-Status: GOOD ( 40.13 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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. Signed-off-by: Chen-Yu Tsai --- 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 | 107 ++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 drivers/platform/chrome/chromeos_of_hw_prober.c diff --git a/drivers/platform/chrome/Kconfig b/drivers/platform/chrome/Kconfig index 7dbeb786352a..acc740bd8bd9 100644 --- a/drivers/platform/chrome/Kconfig +++ b/drivers/platform/chrome/Kconfig @@ -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 || !I2C # cannot be built-in for modular 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 diff --git a/drivers/platform/chrome/Makefile b/drivers/platform/chrome/Makefile index 2dcc6ccc2302..21a9d5047053 100644 --- a/drivers/platform/chrome/Makefile +++ b/drivers/platform/chrome/Makefile @@ -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 diff --git a/drivers/platform/chrome/chromeos_of_hw_prober.c b/drivers/platform/chrome/chromeos_of_hw_prober.c new file mode 100644 index 000000000000..03bde52f9092 --- /dev/null +++ b/drivers/platform/chrome/chromeos_of_hw_prober.c @@ -0,0 +1,107 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * ChromeOS Device Tree Hardware Prober + * + * Copyright (c) 2023 Google LLC + */ + +#include +#include +#include +#include +#include + +#define DRV_NAME "chromeos_of_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 device *dev, const void *data); + const void *data; +}; + +static int chromeos_i2c_component_prober(struct device *dev, const void *data) +{ + const char *type = data; + + return i2c_of_probe_component(dev, type); +} + +static const struct hw_prober_entry hw_prober_platforms[] = { + { .compatible = "google,hana", .prober = chromeos_i2c_component_prober, .data = "touchscreen" }, + { .compatible = "google,hana", .prober = chromeos_i2c_component_prober, .data = "trackpad" }, +}; + +static int chromeos_of_hw_prober_probe(struct platform_device *pdev) +{ + for (size_t i = 0; i < ARRAY_SIZE(hw_prober_platforms); i++) { + if (!of_machine_is_compatible(hw_prober_platforms[i].compatible)) + continue; + + int ret; + + ret = hw_prober_platforms[i].prober(&pdev->dev, hw_prober_platforms[i].data); + /* Ignore unrecoverable errors and keep going through other probers */ + if (ret == -EPROBE_DEFER) + return ret; + } + + return 0; +} + +static struct platform_driver chromeos_of_hw_prober_driver = { + .probe = chromeos_of_hw_prober_probe, + .driver = { + .name = DRV_NAME, + }, +}; + +static struct platform_device *chromeos_of_hw_prober_pdev; + +static int chromeos_of_hw_prober_driver_init(void) +{ + size_t i; + int ret; + + for (i = 0; i < ARRAY_SIZE(hw_prober_platforms); i++) + if (of_machine_is_compatible(hw_prober_platforms[i].compatible)) + break; + if (i == ARRAY_SIZE(hw_prober_platforms)) + return 0; + + ret = platform_driver_register(&chromeos_of_hw_prober_driver); + if (ret) + return ret; + + chromeos_of_hw_prober_pdev = + platform_device_register_simple(DRV_NAME, PLATFORM_DEVID_NONE, NULL, 0); + if (IS_ERR(chromeos_of_hw_prober_pdev)) + goto err; + + return 0; + +err: + platform_driver_unregister(&chromeos_of_hw_prober_driver); + + return PTR_ERR(chromeos_of_hw_prober_pdev); +} +module_init(chromeos_of_hw_prober_driver_init); + +static void chromeos_of_hw_prober_driver_exit(void) +{ + if (!chromeos_of_hw_prober_pdev) + return; + + platform_device_unregister(chromeos_of_hw_prober_pdev); + platform_driver_unregister(&chromeos_of_hw_prober_driver); +} +module_exit(chromeos_of_hw_prober_driver_exit); + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("ChromeOS device tree hardware prober"); From patchwork Thu Aug 8 09:59:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen-Yu Tsai X-Patchwork-Id: 13757192 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C2BC1C3DA4A for ; Thu, 8 Aug 2024 10:04:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=/9/QWNKXSA4i4sxpfMzvxA3Z99pgqscZPbDBz2zlFLI=; b=N/9UEEk9fpsCFf+4Q057qm9VSG VOu7KxkHJfcE4pn6xbUQmw7HokD4m/Yp0/BzRaz5/zk1tTORM1sBcTq3YCfDh90eVJTDLjGt7Vntp 5brHSTHVgMrzSgGXtx2cZo0bmiUMW5L6bTw6ofrQCvCzj0jBWoes6UcQ5lA26OLxS7zS+pGwlso4n VEwzhg7qDT1oDkSUZdlGHVSISw3dnw+NggP8CwqyNJhpCQfFM6VL2Cf50oFvYrrhSqIPKIbWhjmnm RxWFnKbCoY25pG9wa28wAtuYkKY108Bk2VHC/sU2Nj8cwQVaByc5Z0tI+MESEOeNMQUlVH8SyUMmn N2EPEPtg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbzzr-00000007pC9-3icU; Thu, 08 Aug 2024 10:03:51 +0000 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbzwE-00000007o7M-2mXi for linux-arm-kernel@lists.infradead.org; Thu, 08 Aug 2024 10:00:08 +0000 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1fd90c2fc68so7481905ad.1 for ; Thu, 08 Aug 2024 03:00:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1723111205; x=1723716005; darn=lists.infradead.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=/9/QWNKXSA4i4sxpfMzvxA3Z99pgqscZPbDBz2zlFLI=; b=IiCM8EkEdisS0C1wXJAaGk3TGC6mNkxWebnuM1EXQRHMkOVksd1DG2BYeQEy0t4TKn LsGOok+OrIbChaS/sd8rvhif3jAOpUWUOkgC7V8EBr5G8kkREpPJ4ZUxgyek4BCt5W93 iXxDScejK4RToXd4Tp/v1KpsR43W+EYLRdVvg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723111205; x=1723716005; 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=/9/QWNKXSA4i4sxpfMzvxA3Z99pgqscZPbDBz2zlFLI=; b=Tg+sodOwl3U6+S9mTpRZcwBNdTztXtqrOEYvJMCGXsXNQlyatBqp61ViR+HsDdsMfV jQhll4lf9uUI3lRFL/6OBcGI3Oze384f3fmmn6KP48Uj5ioGYu/CfLcH8wsyCAGMFQp7 Upp0MUdjvbSfykcwBMKSK5MkCdt75LWt+GrPY30pBeqaEDqkkHLcb3+M3JW21lWpeGHR fVkxjXezseWfRJVk8/Sj82+yHiLjbA+BfOs+ZVuy9Zgr4TCmJFvD7rBe+hWDOH1aszJe qjnWLgIR/KNadeCUVD90xHvZa8YkPvxO1Wv4aOuvU2AqrZhKkPsflo7Z7dHf0wEutFAh KH1g== X-Forwarded-Encrypted: i=1; AJvYcCW6nfROnu0O1/yjyekDk4rJrCCuS8uBlZvDdldcCQgCuaKHoIrmJ3EvXHkB3kOiZk1H5CWL5RW6xUqn94Omd8tQN6IYK2QP7HdqqNL3Gh1RIc422lo= X-Gm-Message-State: AOJu0YyLqz+wfSFvpY6Ts9mKXWYQJE3Ge1dxaPuN+/NHnvWdpR1gOymn 8YJOV/F0WEs0iudvqb9E1w+e9+hB7KR+sRf7nrL9N1V4Sz6onNjzHzZmrv3cSA== X-Google-Smtp-Source: AGHT+IFHYCH6ZPttGWJyX5orsjVgIQjNRW8T6hIcAdANhk3iA3ouOxz6UxT/mqu55va9bKkOzlc4Ag== X-Received: by 2002:a17:902:ce81:b0:1fd:7724:c783 with SMTP id d9443c01a7336-200952654a3mr16237775ad.36.1723111205352; Thu, 08 Aug 2024 03:00:05 -0700 (PDT) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:8b53:87e6:914:a00d]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59297707sm120784985ad.254.2024.08.08.03.00.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Aug 2024 03:00:04 -0700 (PDT) From: Chen-Yu Tsai To: Rob Herring , Saravana Kannan , Matthias Brugger , AngeloGioacchino Del Regno , Wolfram Sang , Benson Leung , Tzung-Bi Shih , Mark Brown , Liam Girdwood Cc: Chen-Yu Tsai , 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 , Johan Hovold , Jiri Kosina , Andy Shevchenko , linux-i2c@vger.kernel.org Subject: [PATCH v4 6/6] arm64: dts: mediatek: mt8173-elm-hana: Mark touchscreens and trackpads as fail Date: Thu, 8 Aug 2024 17:59:29 +0800 Message-ID: <20240808095931.2649657-7-wenst@chromium.org> X-Mailer: git-send-email 2.46.0.rc2.264.g509ed76dc8-goog In-Reply-To: <20240808095931.2649657-1-wenst@chromium.org> References: <20240808095931.2649657-1-wenst@chromium.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240808_030006_794964_E62307C5 X-CRM114-Status: GOOD ( 16.12 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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. Signed-off-by: Chen-Yu Tsai --- 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(-) diff --git a/arch/arm64/boot/dts/mediatek/mt8173-elm-hana.dtsi b/arch/arm64/boot/dts/mediatek/mt8173-elm-hana.dtsi index 8d1cbc92bce3..251e084bf7de 100644 --- a/arch/arm64/boot/dts/mediatek/mt8173-elm-hana.dtsi +++ b/arch/arm64/boot/dts/mediatek/mt8173-elm-hana.dtsi @@ -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"; +}; diff --git a/arch/arm64/boot/dts/mediatek/mt8173-elm.dtsi b/arch/arm64/boot/dts/mediatek/mt8173-elm.dtsi index b4d85147b77b..eee64461421f 100644 --- a/arch/arm64/boot/dts/mediatek/mt8173-elm.dtsi +++ b/arch/arm64/boot/dts/mediatek/mt8173-elm.dtsi @@ -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;