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: 13757172 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 B0729C3DA4A for ; Thu, 8 Aug 2024 10:00:24 +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=qnI6KSkOG6MkQXb/KUs/m4DxHJ Zen9oF1C/kHnXJXeroDZ0PkMjgaA7uKWe5R1mjDAI98F4gcUK/bbyFQBI7a6evzqR1L5CjLIdbj3l qEkG8hcoRgDKWWlpJKOUQHXvygTtg2R65qApIzCLDtrgcWxw9FhWwtOUru88OfyEH+2+b+les7KEm +XXrDhhFt4rfLJUjYlM0av87ftsnmpkboVGscejeMSLnRuB3/GEBP+zmeoQvlebW6P6B3EpKFCkAE hLweqHyjaQJvrEYXML59s2qulmJWMslHTwcUR/ZNNIeLmoKKyFosjl0mHrStmU8Ah8e3LWCIEZ083 GTkwRkaw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbzwV-00000007oBJ-2bht; Thu, 08 Aug 2024 10:00:23 +0000 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbzvw-00000007o0G-31dE for linux-mediatek@lists.infradead.org; Thu, 08 Aug 2024 09:59:50 +0000 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-1fc6ee64512so7410175ad.0 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=Jjyc5kamQOe6fv0YdETQ0GkV60VA4VdlYxS22DxicbBql+20bjf462Iuiar3y60XGA NWM+zsOyI67P/KxNZD+6Dwx1vqOpWABlwtruArjlNbntkZUSCmN1dFb9QPDZmmemX45p IdnHSiG6pn74OKq+qqkWjW1LvWKRwzDnNliVLpS7Wpd55g0tey05w3Hxj4JvRPryIS4i MfHOdB2SRl1YWFGf4pxeolzNfE3DIf7PzVsLyb+3RmKfKAoBFpRSXpCjlN7LB12vpbUb OmbBRmR9Q8COkmHDW3bZuZrfjM1mHWScw4N8EvKPx+TAsEYzpE4iHE5uZk0I9QTYcx+t 7ipw== X-Forwarded-Encrypted: i=1; AJvYcCULYHgpsFYVuVPFo5T3lUwb99ehaUoPMuULPfBiIzJtRXrV//x9xqIH41nKFq7KVCdlgDKCXgrle7JUeOa5+DMXOwCGiAc3I7dJeow+tRuQE2H7 X-Gm-Message-State: AOJu0Yxmp/1pXGd2bg5l5KQol4810xzj1c64E1k3qolN71vNvO7fwg5R zUw4twFuuYav47v0GlOc7G1k1ZKTMUgK6VoCe8Y/8RR1+C/hIyO9bNePCX+fvA== 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_791865_88064690 X-CRM114-Status: GOOD ( 16.08 ) X-BeenThere: linux-mediatek@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-mediatek" Errors-To: linux-mediatek-bounces+linux-mediatek=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: 13757174 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 E703EC3DA4A for ; Thu, 8 Aug 2024 10:00:56 +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=hGGGX1Gi38Yd2BnFYeswB3YiKr Ao+D9yW8LtVm6s/zV0/vmI3ScD2NPCgYYJlEQp/ANa77KjvT2faZPzQqZ9AB8L9HV63xZmjTwNKpj l/Awk6e1VkdxSzKRd3UQ7M5d1ySPPP3y4KMkWn1yf+tgnO6HIqFAcRFZvHLgbVqo4CqBn5vcSwlYu HtScNgm/3FCpCHWTyuNNLR/23ZDz6td2CW0zA8uvqVBmDtv5mbkSl15citEjEkeGBgoXvktkNbyNz 9JVSiVSNU1fUNWxiYe/18yadeJbeQf+QbDcV6ebt1nO0n57l5ekvpWfV8LXSVI00Obyk2vSO+yP13 O+gQ4t1A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbzx2-00000007oL6-2bh8; Thu, 08 Aug 2024 10:00:56 +0000 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbzvz-00000007o1Y-4927 for linux-mediatek@lists.infradead.org; Thu, 08 Aug 2024 09:59:53 +0000 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-1fd90c2fc68so7480015ad.1 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=ciGCupch5KJtUoXyRl1Z+t7XrJ+hKoMphljbvLZv7HUdTQGaEaEl67TWgK9AQmTjaE RN7IGgZNMLYmxuGMciKVBpK2EDRVuTl7LLSoPsSLqTObcQkcbxIVmVCiJVOq9MvRX8li pL6IR8OxseukzWiKp2KclixoYn0IRHrayG5VGH+/MNn8uIF3Hm5UwN+1e5Kv1NwlrolP eAQLqMcqnBcfGnhBvCbXg6irPVWyQSgwfGh6daW1ZDV0SnFYEUu3Mb2+oXjul4yWJunB hlYp0t8eEuwugtJb29zvxKpHmRzoNGAHveybJUNryilNWNckV7++e8RudpNixbDXFsn7 kSqA== X-Forwarded-Encrypted: i=1; AJvYcCXhBXu16jhw5F36QXCd1y39LLsAwArZ8a6uR9RnCm8ieCugTLe2fs00q2CV1655XfLA5sy0ZrkoNb4Oa6nhzCrfq0yvpqPSqSqywuQ/ndyEFYJs X-Gm-Message-State: AOJu0Yy3E7yHUi6qxYYvMV1c0woAWnjY8qY5kb5uC7ukn7qdrctFO3o5 haGD6xHUbJlGxEJaEcVJFwg8VdTS5Di/ClRqkc+d2/16Ii8/Ewp+kNUGNIgeZQ== 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_055569_02C5CF50 X-CRM114-Status: GOOD ( 28.04 ) X-BeenThere: linux-mediatek@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-mediatek" Errors-To: linux-mediatek-bounces+linux-mediatek=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: 13757175 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 20518C52D71 for ; Thu, 8 Aug 2024 10:02:04 +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=xUslAmN4E6U/E5RepqOXUlkWFC yBFLvCPkxNjm8uBP8ei3YQ95K2d8uNkLgoAQPDopQqxQdPchQKGTp/12p9KbrnkHe/+lmDtlEKEZz PGZ3gwOEVV+aZIGRBMQip0V/tL9NlOselztaKL5lFzk55+0Y8FjFoTznZGAuAVukprDWap1O9dz5G QYoFKh7HzUSC0cM5gYVL21D+M9iWTLoPLBRtUzdTym0kVKC3KFPyeN2J6f/YBe3lXWW/Xnbs7j67D UAWsHM/jOCne++kyEZdd6scb3676lHcTRC8hCVbyWuCeNifmxjiWY+/sP9IpqhlxnWjD+A763EfxL cb5Vonng==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbzy7-00000007ogf-3Jzo; Thu, 08 Aug 2024 10:02:03 +0000 Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbzw3-00000007o2b-3pP1 for linux-mediatek@lists.infradead.org; Thu, 08 Aug 2024 09:59:57 +0000 Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-1fd90c2fc68so7480595ad.1 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=UM1cOTLI8fq0YX+Fpiv17FZUZSId1ugz0ZgGWjtQ0cT4YwvI8FO9fJ748lErhRdZDl E1WsiLMmK1agHn8xLmdo8kj9G4CbO1Ql1qV0bRI9NdI7490dkFGQVmOd+kD4gn/oX8WX Qq3kA8yehwAA5YlmQEKpPb4j+i12ALvl6eV1GIG2+Yc4iVblHhoNXeLe3qXsf72RWnuP uhXdhFdkEL20ODz96GaowaBYQwIP3jc+oAu2NP5y3rAPaYiNqjQD+IobObbA+SciTVFv L64u83CnIjoN6KzKYLfkbk42sX4kTDUhmrWZ6IMKrpVnF89X/hIBcwFqH20a9FHLz6Yq F6FA== X-Forwarded-Encrypted: i=1; AJvYcCXoleo5w9/kFWgAE1U7q77FHUTot/zRgcLwCCBwyhtUW1c/Xc0JUfJVn/ehJMxOQWSpjYzregczCKofeBJ3ffP8DgbDJXagsC9SD8t2AlIgvXPt X-Gm-Message-State: AOJu0Yy5ajBvEZ0hSnkf24DcivUGiR/V7dMd/KRUNcTX2mvi52zQw5h3 guKdbuaHrwzbPDviBCXQ5fYuWY8DCc5zrMngSDFxjqhFfqfmQFM/key6mox+BA== 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_025956_067914_2ABB6551 X-CRM114-Status: GOOD ( 40.00 ) X-BeenThere: linux-mediatek@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-mediatek" Errors-To: linux-mediatek-bounces+linux-mediatek=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: 13757176 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 9A365C3DA4A for ; Thu, 8 Aug 2024 10:02:04 +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=YyaxG63T0HzdfRFMVi7CqoS/kp /sLWaKnnPgiOFqgSan71D2cafmpvsG+ZTg6/LFnwZvpNRAN0g8tzTjAY/MrfrzKoNaI+sQYcZiwzc Vy1K5jcr2cNENJQIAoT2p+3aOYt25FfDMwM1c/+fnXXfMCI1SeGcjw1VsDtjLss1pVRtLQQGjQmSN 7NgE2L6osds1G/fzqwz3YJG1uvVX2l3Zzhq3jfBjn2NjXmo80F0uFe8for9zA+rUxaz7zL9NRhvIo 0szWhS6KK6P156w4znYafqsklSJpA4rbrd1PdqQ1jjSzG/uLuF9lYTH9TA6ciCGe/UOnqPHpms0bg pezZdBAQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbzy8-00000007ogz-0Uqv; Thu, 08 Aug 2024 10:02:04 +0000 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbzw7-00000007o4B-1Ibq for linux-mediatek@lists.infradead.org; Thu, 08 Aug 2024 10:00:00 +0000 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-1fd78c165eeso6641335ad.2 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=WMyjWdJqr/2u3jL4hzCORzN0xcIBvur6866h/rTRGPLeCMIjTzlrVF8zuDVYr98Uwk P5fDizRhE7M56hkmcCsPLdwXktlN79GqPQuzYMLUFnPnuDR3Rp7DZ3vKhxgaN62zf9Zc p9TQ+rIArNekzgHTi1SbAh2yLjhyOUFzLOOJu7lv2UJJ+dsr46Vn7V6r1vg8GGkXWiZQ sfoHZURNwiSVSh9w9t3Ewe5f5IABLjY+V73eAc2/1efavbpnavgIBB4iW7i2mUbCE4WN z0kA25qlpI7Ctf12aNI6ReQPgOapg5brOz9EeYAVzOz/lKk33gx6GLuN9lHK39m8A8uj dEEA== X-Forwarded-Encrypted: i=1; AJvYcCX/Fo6+HdCL0bi4BXBZUs7H5Dn+FAj0ZoofUtaR1vVo5NyHn85V9rHZPYfepSnK8J8aLQ/b/SUD0OwKPjZElOCnRFzgbMFwlSXZBAeyVwJX0owo X-Gm-Message-State: AOJu0YxvqpDnRj3iCucKzrH7lmN9W4ZveERoTiyxR8PNb+uOB8C7ThRg G+KmARbjASo/nqb1ojOSYBTiON/sLCYYlr+SwWmzTsNF/mau8G8A1bvDMlgdaQ== 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_385336_73BD404A X-CRM114-Status: GOOD ( 31.41 ) X-BeenThere: linux-mediatek@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-mediatek" Errors-To: linux-mediatek-bounces+linux-mediatek=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: 13757177 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 1566EC3DA4A for ; Thu, 8 Aug 2024 10:02:48 +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=ZJj7tI0ZT6yl5NtBAUvaZn5vZT I+wH3eRjgajaL6DhsO0dLF547jVuTEEIHxwqhgObvy6GD2nuqtccA7elW8PIWNwG8G45LAvVAiQ5I 6Tw+YTMszV1CaV1nz67vXlOT49NWILRcvW24/I4rJwf80zD+mGVUCQnEfICJ1PWYPFvy6mqzX2s/3 HhDTJOX4GGoXp6pSdpakpwLbI0TzXPqXv+3gQJZZImNBOeOBmxk7xnAvjpcw+Q7VujFD8npRYMEZP ojDwtpypLSPui5Xc2jHZFcqkl646tK/MZych5Bhxm2Ge4kDgVC7UHf44VqgsNUArtUPvdQuph8sV+ SanhhD4w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbzyp-00000007owr-3C9t; Thu, 08 Aug 2024 10:02:47 +0000 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbzwB-00000007o5B-0iau for linux-mediatek@lists.infradead.org; Thu, 08 Aug 2024 10:00:06 +0000 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1fc49c0aaffso7232815ad.3 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=LWAX8j0lXkapdT94N/G/w/yhmrq0C34qSJDJLYt39zip6/5O+xnbI7CUA/qUgCZtDe uddWYJDJtwBOTI8Mz1oYDIQqSCiPn6wDdAxLyIBl4+KJs3JCoMV+vwAki21rb4nkakH0 un1q78w2OP1muStmx9cBAAAUgtJGJCOpA68UP8YdScaU3tJRgtMkXvRYha7KQcuRrTxY GzFcI7Yb1YstIn24A5qnXz+A75mvDgyGbTrO8sq9yIZGgCarZbUDU4WxvYzQo95uMyAg Rg7SSwOooUeJEBe1dTH1unDTDuCjD56rmGxSYfVemlpdCz9mfaJ8QL8JqYA3X6HoLMDy krNg== X-Forwarded-Encrypted: i=1; AJvYcCX6Kwur3kD4rVUVJG0olbsoFwg0PJBlLOJvoxLi7hQDfcZAVXd3hxev1/+5daFyVErhcscjMvCp+aC5yBahGg==@lists.infradead.org X-Gm-Message-State: AOJu0YzOZixTOUSInZ58QW69BL8k8IFRtgSE6V5f9GU0z9XQOi7G54w8 qTZdqzNQZsNVMUifEMpdsJU4FzLK6s0J3X6INHIb5MepycSFzrmI2xHgJrv9gQ== 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_689643_01ABD9AE X-CRM114-Status: GOOD ( 38.87 ) X-BeenThere: linux-mediatek@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-mediatek" Errors-To: linux-mediatek-bounces+linux-mediatek=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: 13757178 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 14629C3DA4A for ; Thu, 8 Aug 2024 10:03:21 +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=FT09iI0mrV3Eo3L/bIZQ3l8Nua AhgqmsJGTpCW0r15rpn4lkLQL5F1cEgBojgbrGoYG3mEj4dLNyM6figAROTBjuA6TZ9du5KChRGuD M9AAWkELqKpfxKM4ov3ImbdDCFqDrRz7GM82lXYs6TqdrLmjL8IJQiVQPvuGCyD/NtTgLUYd+HOSO aQn04ED9ai4uOvbnxuW7iAMvlFOXiMiVS04qyrqfZWD8opM6P7dVyqm/0OnE69cYW6ZlbKkV1oB6Z u+0fBDRNHHwvEpS9gfJMdKUSbLzcxnajBzeJZ7cI7vRnxPst1z3yNr0k2Ckxy/QoG97kdogrFl3T4 xFmSc4BA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbzzM-00000007p67-3COC; Thu, 08 Aug 2024 10:03:20 +0000 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbzwE-00000007o7L-0NQT for linux-mediatek@lists.infradead.org; Thu, 08 Aug 2024 10:00:07 +0000 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-1fd6ed7688cso8194445ad.3 for ; Thu, 08 Aug 2024 03:00:05 -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=p9pUN0RXAq67WRd2Q5ON9Rbbf4eBYrVcgzuwn8JDUfyUwmWkeesO31++ZtZopciT+w hl+xTLg0TIqUA9bp82Srj9qefHR+0Z1Bbt0hP9+QMPYyyiuvVePlHo1ZAHfb0MHIMoGF Unn0Y6jOdYXlJDGfNzzUIgRP/FX6mn3WTf2Dba1t4uNlolICiQyFubYEUYqM8SlSJzo2 A4Qf5mmTum3EqSbv67+0+3o6W86FnD3st8SIIZlxJ+MHfm4IR6aIX0i1mB9Yb+USzJTf eTYXExTSUZv/jvllWPZwLDW2gvHirPdfL5LgXB2Djoeh7PUvTqIfqsI4V3eW13d/P1M+ 2iVQ== X-Forwarded-Encrypted: i=1; AJvYcCVVg9zp9Fh7aS2Fo8fObwagwUxB5Ya8lds5ArGSAppqb4K9SaSB3ukBxRp+3NWvu/si634WI9TctI1y+rK26ja7vLJSmYhNnyE4mEwHc/V1V5XT X-Gm-Message-State: AOJu0YxyNNHxRCOKJxv6p6F/bV+DtMba8UPYXsVVV8K6Ow1begNLbmNx bsMndQbmViiSEPgyVeGtG5Bi1HlLOK/mmP73z1Jjt7UMz52e7Drnb36FGy2VPA== 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_293934_E03EA503 X-CRM114-Status: GOOD ( 14.86 ) X-BeenThere: linux-mediatek@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-mediatek" Errors-To: linux-mediatek-bounces+linux-mediatek=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;