From nobody Thu Sep 19 23:14:39 2024 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4C369189F52 for ; Thu, 8 Aug 2024 09:59:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723111189; cv=none; b=HNHx+jXAP8TibIyBUilvV6E3jJL9LkxN7sn1kHsiE1UUE/T6s0lf5wyDfSa0gpThKbluUyRth/9ZoYsvdwfwlgHxkpVgNi64DtrhzJK7UPxY4cdeDZUQ09QJIh5T/3SVXcbSrk1o6U/Kl545zGhmUhMDAsjFYoHzI+ZoGfFXhio= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723111189; c=relaxed/simple; bh=H0FOBfwEBWtywgtjSA1F4rTZQ5wzYjAwhkmuhYnKTxs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Zwg1wPW1Pzck0mrTdUSc9Uu7XpkPHdFx/cIolLvjKnht24WNPmSvbt1NuKK7rSuiO2GsqHnqKpM3JPsNE1WKy9pjtWnPXZZe1237N7Nlp7S/x47gFNHip+ENvDje9g23xcDzIysaiABV52oxay+zrgm/mu2f7LVAWuyAHbHcnfo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=Tt4CwuI3; arc=none smtp.client-ip=209.85.214.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="Tt4CwuI3" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1fc587361b6so7787505ad.2 for ; Thu, 08 Aug 2024 02:59:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1723111187; x=1723715987; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=lnHNs8RGUpugUdHA2OImjbnnj6+rNsZeKGSTnE9NGbI=; b=Tt4CwuI3e8oN6dUEd+n31G+e7XjDnOQpWPt8+RZFq7ifVeVuXJovOlSoXwLBreNhSy lfy7WZup8Ouyfz5nMB8epJAGwTzqEHzLgqEUHFu/DAMtjrrl7RUVgls2if1XAtxoAFn8 HPCSN3+Wd2IVAIwMSSO+b7X0O2sbWsrns/EhM= 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=BvYKASzASYJnb7G/Eht6jfVjn2DDFvGjvK+tdq63LdCCDM2fPyiEomqg7sZhSGBj4E sfwAmQZq5KAIVXvta0/mfwXq5kUShfW7Fu2WYxzUOcYhZTfB2wqPIzyRGbA8j958dT86 pXSVmuGjRpgGze2jzZ2Am0Z7QILM1CGs2374IEa2dN9TwXqUBBQAe17hzo1/9nQPuuZd y4qESIQcCTIPdCCJGvbbDEHrt0XM5DaB14tpCyVqhx96a8l57KkHIQwuRMChLAZ3hKFo sb7OJaegFoRTpEqR3urwbnS11vG7w0RCa6ivlhvH+loJ0ceSFWxR0PzJXFbpHCDOrTlq c7IQ== X-Forwarded-Encrypted: i=1; AJvYcCW9WF2XyS7mVbgWP6QfRsQv+Mxw8OZc2/ZbK1rbSCpTa8UBWMPJKxeGamNaJV0+/k64xgrYDivCRR1p2pOTDVBGzRS/9AfaGKpR4k7M X-Gm-Message-State: AOJu0YyRcvmUNHn3eqYyqX2o5tZDUagyKKNxajg2humuJt4qF7Am9Ktw Qiv1lrRFmbHwjNDLIbKklwubhZ/4vhOFwImE8lThckgxjePH2fZCwsFXJ7V/gg== 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 =3D __of_prop_dup(pp, GFP_KERNEL); + if (!new_pp) + return -ENOMEM; + + ret =3D 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 cha= ngeset + * + * @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 =3D (char *)prop_name; + prop.length =3D strlen(str) + 1; + prop.value =3D (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 o= f_changeset *ocs, return of_changeset_add_prop_u32_array(ocs, np, prop_name, &val, 1); } =20 +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_nod= e *np, const char *prop_name); =20 --=20 2.46.0.rc2.264.g509ed76dc8-goog From nobody Thu Sep 19 23:14:39 2024 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AD44018A6C4 for ; Thu, 8 Aug 2024 09:59:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723111193; cv=none; b=r7LDA1sY7u0C8FGsbdHaq8KQUfWqF9+aKxCOjWrTlMplURevR/uGUoSBooon7l/Nd8H2r6WbjxTF4dgra8w9HMg/j2ZVDhyUyrYsa2S/bcEkyi342AOO6FcpxF6dORvfKK+XLe4if//oWpp88MmEn5DxL2IsVKMN3uw0rm33/VY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723111193; c=relaxed/simple; bh=/IAlJZ4lOSvaLE9tnhzTQ2epkpQjqOzpH5OlWZnuSA0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YKxpTlHZ4GNgwX/QHEv6CO09eIWdPqGe8/7gbo6CNdXqhq9Agqhw8xNw8qAqSUJ+9XXZrrUPDDVf6E5WjD4vmdi7KvkULJQFb54H04c9jz5Pm8Crx2RzrjD1p0B0RdUAktz5kGVWIABP2+XrsaULuZvfVeHLytBR2NOHtVnQzWA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=SJ/Go+eF; arc=none smtp.client-ip=209.85.214.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="SJ/Go+eF" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-1fd90c2fc68so7480005ad.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=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=wR1Udf/FWl7py4H5RQqWMXM0U9PswySd1hls6tNxB7g=; b=SJ/Go+eFNfHALffJwWjYdhhbrGugna+yo0Xdy6wAU8QnFJCJCscVlrRemU606x+mY2 vTo6r2OlssSVThhC62Rfl1HkhrWXu8e4hmPn7RW0D/mQkZYZTTEBvYk/vLAUQsd1YCWZ RNqvCHk5V1yqgc11aV5xLp/isWYE52205xQ3c= 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=dIo5YYIFWMur+5xslYOAaDYOXMoHrxU0Z81ezW73VP4wonKDtS8RvT8Sy+2CbkUz9w RBMERQom+oaMw7hG8eKD/kkQZhNIZ29Tkl7FeLIXj2iKbkAc3AMl2IIkP0dTCHl3VbEQ gGh2BVP6bE8bb2vivBHgYZx5MpZISKD4C25ahfpCpWPkfNVS1q+tzfJZFapf5+oj2FtW d4H5Sw3GCVvEvICn8MndsqTmLD0hKA/GkO2IVEsUTEsl5WK1X9j/oZq/UOCjj+M9NalM XafaMFEjaYHuAj65suG07GmMr0O0S7Th7BiflK8j/ZRzhHBHJWrWm4AIMx/WKdT7ijAm /BSQ== X-Forwarded-Encrypted: i=1; AJvYcCWd6GBq+y9XLx0VFtHvb7HG02OXnpQ3JEmnYzW1qig7gtiH8HwZKyE9xYD2UH+zmsE7TiUYwSDAMePV23pGYRmetfkLlEqio08+bGnV X-Gm-Message-State: AOJu0Yz1GA4tkBftn235/dAAjxydt9YwAddVW4Nw10QbVhTt4WH8HXY8 uydWX4xLmfK6ThpBMCfc+QJcclNDmSP/S6nYqgs/LH9EWbgQiWshRz6iEnwG8g== 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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(str= uct device_node *parent, =20 /** * 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, cons= t char *supply) { struct device_node *regnode =3D NULL; char prop_name[64]; /* 64 is max size of property name */ =20 - 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); =20 snprintf(prop_name, 64, "%s-supply", supply); - regnode =3D of_parse_phandle(dev->of_node, prop_name, 0); + regnode =3D of_parse_phandle(node, prop_name, 0); =20 if (!regnode) { - regnode =3D of_get_child_regulator(dev->of_node, prop_name); + regnode =3D of_get_child_regulator(node, prop_name); if (regnode) return regnode; =20 - 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_nam= e(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 assum= ed. + * 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 n= ame * @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_na= me(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 =3D NULL; - struct device_node *node; + struct device_node *regulator_node; struct regulator_map *map; const char *devname =3D NULL; + bool pure_dt_lookup =3D false; + + pure_dt_lookup =3D (node && !dev); =20 - 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 =3D dev->of_node; =20 /* first do a dt based lookup */ - if (dev && dev->of_node) { - node =3D of_get_regulator(dev, supply); - if (node) { - r =3D of_find_regulator_by_node(node); - of_node_put(node); + if (node) { + regulator_node =3D of_get_regulator(node, supply); + if (regulator_node) { + r =3D of_find_regulator_by_node(regulator_node); + of_node_put(regulator_node); if (r) return r; =20 @@ -2032,6 +2046,10 @@ static struct regulator_dev *regulator_dev_lookup(st= ruct device *dev, } } =20 + /* 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 =3D dev_name(dev); @@ -2076,7 +2094,7 @@ static int regulator_resolve_supply(struct regulator_= dev *rdev) if (rdev->supply) return 0; =20 - r =3D regulator_dev_lookup(dev, rdev->supply_name); + r =3D regulator_dev_lookup(dev, NULL, rdev->supply_name); if (IS_ERR(r)) { ret =3D PTR_ERR(r); =20 @@ -2169,8 +2187,8 @@ static int regulator_resolve_supply(struct regulator_= dev *rdev) } =20 /* 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 *n= ode, + 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); } =20 - rdev =3D regulator_dev_lookup(dev, id); + rdev =3D regulator_dev_lookup(dev, node, id); if (IS_ERR(rdev)) { ret =3D PTR_ERR(rdev); =20 @@ -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); =20 @@ -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); =20 @@ -2371,10 +2389,29 @@ EXPORT_SYMBOL_GPL(regulator_get_exclusive); */ struct regulator *regulator_get_optional(struct device *dev, const char *i= d) { - return _regulator_get(dev, id, OPTIONAL_GET); + return _regulator_get(dev, NULL, id, OPTIONAL_GET); } EXPORT_SYMBOL_GPL(regulator_get_optional); =20 +/** + * regulator_of_get_optional - get optional regulator via device tree look= up + * @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, cons= t 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 =3D regulator->rdev; @@ -4928,7 +4965,7 @@ int _regulator_bulk_get(struct device *dev, int num_c= onsumers, consumers[i].consumer =3D NULL; =20 for (i =3D 0; i < num_consumers; i++) { - consumers[i].consumer =3D _regulator_get(dev, + consumers[i].consumer =3D _regulator_get(dev, NULL, consumers[i].supply, get_type); if (IS_ERR(consumers[i].consumer)) { ret =3D 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 devic= e *dev, const char *id, if (!ptr) return ERR_PTR(-ENOMEM); =20 - regulator =3D _regulator_get(dev, id, get_type); + regulator =3D _regulator_get(dev, NULL, id, get_type); if (!IS_ERR(regulator)) { *ptr =3D 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 }; =20 -struct regulator *_regulator_get(struct device *dev, const char *id, +struct regulator *_regulator_get(struct device *dev, struct device_node *n= ode, 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/c= onsumer.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 devic= e *dev, const char *id); void regulator_put(struct regulator *regulator); void devm_regulator_put(struct regulator *regulator); =20 +struct regulator *__must_check regulator_of_get_optional(struct device_nod= e *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) { } =20 +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 *con= sumers) { } --=20 2.46.0.rc2.264.g509ed76dc8-goog From nobody Thu Sep 19 23:14:39 2024 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 63EF718A92A for ; Thu, 8 Aug 2024 09:59:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723111197; cv=none; b=eQGT75f+EYD2mUt+d3wNv9/Li2q8Fs8oLxvCXJ+KaT7B0pdAyc6yRWWDqAaEH4hi/f8+LGCLx+XbUcldBJfA5I7quitCp41uOwDa6mjAmLSZY9TOglOpHiG332Tm9bYVtHdYNayT4UVYGnPdv+Mgvb5O24HxB+2VNGWEmGWQAZk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723111197; c=relaxed/simple; bh=py16IRx3ZkhAud/YioWOf8kFrqTw6bFMSUXkOiTxpPw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XR7++YbuKdYafDZzMGNOQ+crC7D2AJH7YD2rRIVWYZsNeU8oFgd6cfPPWmjjvKXlMjfo2PKtDs2ScceYbBIIbMgWGpJbO8YQ4axuSF4NmgVxZGnsOT2A89UOfnVtYI+NUPBqgZLD6nRf/jkvAvmjZ/HJBs1KZ7pfpxUNNl1bzis= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=KcyJpvoI; arc=none smtp.client-ip=209.85.214.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="KcyJpvoI" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-1fee6435a34so7386225ad.0 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=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=8qgCZ39zaMZ1pHgoNfFZBo55h8VlLpIWFFJKzWV3ijA=; b=KcyJpvoILBTgeup5ng6u1qrB1vgIHMQWppO+zMx8HyQvsHqCPDeJJ3dAQ+rJLoErpU lANO26nciRLAfJvLa1bNuwxENt0rye1Dyite/GJ61RtgKTMyt6Rq5r5XK9MG8zwiG2rk gLKD0RZFmPfnPYsGqxLqStWKxDPMz6R/LBRUI= 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=PkBoZBCA5QYRrKdamz5aZPGEIE1lpDDew2PX4j87hbvw0yOS8xEXUwsNHwbglEPNKc BUbtvSXBPyd7QtOCEKPb0NbxxC+pqN6hTFMUewDnhEzzethPThN4T/wc84qxEZUGexG5 MfLXlyYjmjY4sJUG3v2oSqOPa4dPi450QYMarv5yEcqid2X41lgfYMeWbj73JoEp2vkw w1jiXG7VSnAQCfZdYPRyFB/QDflmKcNWo4Fk2+C2ekq89ghznrlwIbwYxOh9WGvWVDQS IWOFAFAmIpHfOVcW+6ZI0Hkrn8GC73l3TVgR5ch13FHEyYZ4hWWtkHpCcJ17lwGwK+/r BbOA== X-Forwarded-Encrypted: i=1; AJvYcCUuztiwlgmURqy+AGZK7dgrXkk5QoTo1fHjR3UzBFxKvGzb7JYwKzUGdgJZpk60z2un+gYHUiu6MLDr3SfVwcLNCeFec0GgR7kMSNWz X-Gm-Message-State: AOJu0YyMqEeF4oVdz9jCCGkvFRK9d7tZ/VXPNz9cvhIaJ6swllJvAMaG 6RNmL7Md9Ig8LaeVelVgkpJk2XEdS/jq3cTnBsWjmo90fT3QjI1ywEEku2e72g== 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 =3D 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 :=3D i2c-core-base.o i2c-core-smbus.o i2c-core-$(CONFIG_ACPI) +=3D i2c-core-acpi.o i2c-core-$(CONFIG_I2C_SLAVE) +=3D i2c-core-slave.o i2c-core-$(CONFIG_OF) +=3D i2c-core-of.o +i2c-core-$(CONFIG_OF_DYNAMIC) +=3D i2c-core-of-prober.o =20 obj-$(CONFIG_I2C_SMBUS) +=3D i2c-smbus.o obj-$(CONFIG_I2C_CHARDEV) +=3D i2c-dev.o diff --git a/drivers/i2c/i2c-core-of-prober.c b/drivers/i2c/i2c-core-of-pro= ber.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 ea= ch + * 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 funct= ion + * 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 =3D NULL; + int ret; + + node =3D of_find_node_by_name(NULL, type); + if (!node) + return dev_err_probe(dev, -ENODEV, "Could not find %s device node\n", ty= pe); + + i2c_node =3D 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 =3D 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 =3D 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, &dat= a) < 0) + continue; + + break; + } + + /* Found a device that is responding */ + if (node) { + dev_info(dev, "Enabling %pOF\n", node); + + ocs =3D kzalloc(sizeof(*ocs), GFP_KERNEL); + if (!ocs) { + ret =3D -ENOMEM; + goto err_put_node; + } + + of_changeset_init(ocs); + ret =3D of_changeset_update_prop_string(ocs, node, "status", "okay"); + if (ret) + goto err_free_ocs; + ret =3D 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); =20 +#if IS_ENABLED(CONFIG_OF_DYNAMIC) +int i2c_of_probe_component(struct device *dev, const char *type); +#endif + #else =20 static inline struct i2c_client *of_find_i2c_device_by_node(struct device_= node *node) --=20 2.46.0.rc2.264.g509ed76dc8-goog From nobody Thu Sep 19 23:14:40 2024 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E8E0118A94A for ; Thu, 8 Aug 2024 09:59:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723111200; cv=none; b=NQSuPPZ1AXUTz8nZWsmfLdwRQECn6QqEjlUqw1c1ljfhJ81ZM2ATil+a1N+tCZroRo51UfAc/8emLTI5dibtSyQHYBDsBqml1bSEoDRPeJ9zXCtUCCJdPFIDNG7JU9FZbE1zsrtUiuKUiT1DEcqYAOTycrri+U+dMi2XE8BBQMU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723111200; c=relaxed/simple; bh=3PK3nldhA/CKj5NnjT2jjGHNA0GDUckfr9mIXzTKNgE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RyIvAUNKrMTvFEUwRRg7JbE/3hPn4CC5iYyOnnuulWVDVOnh7MxxFVfb9kpyebMJerFnh8dUT0tQs2pyi3WNDkey6DRXqLFsiu1qwIZ8wRdHoFZLeR5lpU8KFoWEr02qWCAWb437hEnOpRcCfHn9Q77e3JQPmVXytryhc4r2g1U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=UXXhKrDg; arc=none smtp.client-ip=209.85.214.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="UXXhKrDg" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1fc5549788eso7849965ad.1 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=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=BMQzDBFwtvtL+uRpCqzQYWzmcXr00tCiZCE6wRprzyk=; b=UXXhKrDgvAIjyY6mHenKM5Ptq/1RPbGEsj49clVysPZTYI4FqyxnbR4j1vW8s7ZzvJ JgtBUCa9fzF/YhZ2RoBaXHeKPMSCnxkka/QCYnUId7+tUtew+sSeurMiRw63YbYtDPxb erCeujaN4199Uimk8HCCKPFs3jtIbYGgc8Bw4= 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=nqJxGj25JibXZVMigmRde4JgJBk0NyRFgEZhcXZSr5rGNSOjuQzdt2M77bAJs8d7QJ TILDZ1a69INJoMJTJ6CA2lfbSSqQUaoGYWl2O2x8nXeJxoc9FIC4jLlVhkbdt8HVEG9K xlAV6WzNbdvghCcvGvSUf0kG+7+8qubd26Bm5MknsCRGj/ucHhO/dKS2zf0re7Yx5LK9 uq+AhujBgMAcxnW0agMz5euVTe3ZW+4kw+nJrzQu4fec5+YT1RiTM4om6fv4BrqPpfAt L/uhMROwXUr8ATYDelaMqasqs68QgUWN56S+6IxwI5yXCoNHJby+bIPjnGKtlQyEKmNj D3jw== X-Forwarded-Encrypted: i=1; AJvYcCXjS73TnpeUowIJhW3SxlP03tPFPIAoGSrwaK7ckVzC7BHYP85UCdrSoy6oNN63ycQWb5TLWzEJE/3bt5aCNHddJ5btpvrlIUddZ7/p X-Gm-Message-State: AOJu0YzObhuxdCWfArCtwTT0nYf+d2+npGp2QdPmLDsLXA1fEenTN1jJ fRGCYTxdT+4Jv4cS1ljtWpqtUtPv2NWVgScAAWT9zmWiIZX8L27ZpoKgD/qqpw== 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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-pro= ber.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 */ =20 +#include #include #include +#include #include #include #include +#include #include =20 /* @@ -25,10 +28,242 @@ * address responds. * * TODO: - * - Support handling common regulators and GPIOs. * - Support I2C muxes */ =20 +/* + * While 8 seems like a small number, especially when probing many compone= nt + * 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 pro= perty *prop, + struct i2c_of_probe_data *data) +{ + struct regulator *regulator =3D NULL; + char con[32]; /* 32 is max size of property name */ + char *p; + + p =3D 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 =3D regulator_of_get_optional(node, con); + /* DT lookup should never return -ENODEV */ + if (IS_ERR(regulator)) + return PTR_ERR(regulator); + + for (int i =3D 0; i < data->regulators_num; i++) + if (regulator_is_equal(regulator, data->regulators[i])) { + regulator_put(regulator); + regulator =3D NULL; + break; + } + + if (!regulator) + return 1; + + if (data->regulators_num =3D=3D ARRAY_SIZE(data->regulators)) { + regulator_put(regulator); + return -ENOMEM; + } + + data->regulators[data->regulators_num++] =3D 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 propert= y *prop, + struct i2c_of_probe_data *data) +{ + struct fwnode_handle *fwnode =3D of_fwnode_handle(node); + struct gpio_desc *gpiod =3D NULL; + char con[32]; /* 32 is max size of property name */ + char *con_id =3D NULL; + char *p; + struct of_phandle_args phargs; + int ret; + bool duplicate_found; + + p =3D strstr(prop->name, GPIO_SUFFIX); + if (p) { + strscpy(con, prop->name, p - prop->name + 1); + con_id =3D con; + } else if (strcmp(prop->name, "gpio") && strcmp(prop->name, "gpios")) { + return 0; + } + + ret =3D of_parse_phandle_with_args_map(node, prop->name, "gpio", 0, &phar= gs); + if (ret) + return ret; + + /* + * GPIO descriptors are not reference counted. GPIOD_FLAGS_BIT_NONEXCLUSI= VE + * 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] =3D 0; + duplicate_found =3D false; + for (int i =3D 0; i < data->gpio_phandles_num; i++) + if (of_phandle_args_equal(&phargs, &data->gpio_phandles[i])) { + duplicate_found =3D true; + break; + } + + if (duplicate_found) { + of_node_put(phargs.np); + return 1; + } + + gpiod =3D fwnode_gpiod_get_index(fwnode, con_id, 0, GPIOD_ASIS, "i2c-of-p= rober"); + if (IS_ERR(gpiod)) { + of_node_put(phargs.np); + return PTR_ERR(gpiod); + } + + if (data->gpiods_num =3D=3D ARRAY_SIZE(data->gpiods)) { + of_node_put(phargs.np); + gpiod_put(gpiod); + return -ENOMEM; + } + + memcpy(&data->gpio_phandles[data->gpio_phandles_num++], &phargs, sizeof(p= hargs)); + data->gpiods[data->gpiods_num++] =3D 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 =3D data->gpio_phandles_num - 1; i >=3D 0; i--) + of_node_put(data->gpio_phandles[i].np); + data->gpio_phandles_num =3D 0; + + for (int i =3D data->gpiods_num - 1; i >=3D 0; i--) + gpiod_put(data->gpiods[i]); + data->gpiods_num =3D 0; +} + +static void i2c_of_probe_free_res(struct i2c_of_probe_data *data) +{ + i2c_of_probe_free_gpios(data); + + for (int i =3D data->regulators_num; i >=3D 0; i--) + regulator_put(data->regulators[i]); + data->regulators_num =3D 0; +} + +static int i2c_of_probe_get_res(struct device *dev, struct device_node *no= de, + 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 =3D 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 =3D 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 =3D 0; + int reg_i, gpio_i; + + dev_dbg(dev, "Enabling resources\n"); + + for (reg_i =3D 0; reg_i < data->regulators_num; reg_i++) { + dev_dbg(dev, "Enabling regulator %d\n", reg_i); + ret =3D 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 =3D 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 =3D 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 >=3D 0; gpio_i--) + gpiod_set_raw_value_cansleep(data->gpiods[gpio_i], 0); +disable_regulators: + for (reg_i--; reg_i >=3D 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 =3D data->regulators_num - 1; i >=3D 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 cha= r *type) struct device_node *node, *i2c_node; struct i2c_adapter *i2c; struct of_changeset *ocs =3D NULL; + struct i2c_of_probe_data data =3D {0}; int ret; =20 node =3D of_find_node_by_name(NULL, type); @@ -101,6 +337,34 @@ int i2c_of_probe_component(struct device *dev, const c= har *type) return dev_err_probe(dev, -EPROBE_DEFER, "Couldn't get I2C adapter\n"); } =20 + /* 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 =3D i2c_of_probe_get_res(dev, node, &data); + if (ret) { + of_node_put(i2c_node); + return ret; + } + } + + dev_dbg(dev, "Resources: # of GPIOs =3D %d, # of regulator supplies =3D %= d\n", + data.gpiods_num, data.regulators_num); + + /* Enable resources */ + ret =3D 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 =3D 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 ch= ar *type) break; } =20 + 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 ch= ar *type) of_node_put(node); } =20 + i2c_of_probe_disable_regulators(&data); + i2c_of_probe_free_res(&data); i2c_put_adapter(i2c); of_node_put(i2c_node); =20 @@ -148,6 +416,8 @@ int i2c_of_probe_component(struct device *dev, const ch= ar *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; --=20 2.46.0.rc2.264.g509ed76dc8-goog From nobody Thu Sep 19 23:14:40 2024 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7772218B474 for ; Thu, 8 Aug 2024 10:00:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723111204; cv=none; b=qk5tEvHlG3qUzvsZTVfllnXByn5eLrgMs1YVb95ktyEC4sAY7qYiAUtufU4ZfYUfaLJXBSZDny7OF+MMQu57dlI9D4t2xWsIKiPTXF3KnGEMTtdE0ubIA1ol3v6D3AlzzCqOl8RvlWZPE9Vzhf/MeEt7TkgNj1r0KQHS01WmhIQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723111204; c=relaxed/simple; bh=KrrCRbhQREYKe5Xaaft5bhjYQwckG15dKwXrCeDx3eA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dwuLsTTbzpnGgSJHMMcwp3lPO0uu97ktzHlol8qanJgKhPwM55+wANawgSI7rwHqQHu45V+d6mYUTvtGL8LCgJUtk3U3Z18HjRF5bI4lnzMaanCdAKmN1AmaeJqqPz2F3x5VWsYxsouJewtux9bA094kitqfIFf87cX22ciyjhU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=CE79LiOe; arc=none smtp.client-ip=209.85.214.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="CE79LiOe" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-1fc6ee64512so7411845ad.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=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=IrJXguIlB/7qdbaZON+hnRiNa98pqQA+T1VHHt60EcI=; b=CE79LiOedI5tPOrUnj1H6tonnbL9+ynlUviMqfLM8aRPOlsRMJvjJBiuLTDsiQiz54 PAfC/M83gk89ZypEDtGYDtutn47tU/w/l6fpeUKQGYD8kCY/YWicY9w7/mMASYF1Oqwf kq4lBv7P8LWWYTJPTPAnhwB1cOJqap0yzZFjQ= 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=fd3fjNN038TUczN6DJMQOG5VEeEwSbUR8MJoAjHTnvzV2a+CYLUeOya05wv1OC+pce 5Iv9pFGoIYH0/Pq1w3ZVon2IEEwOk8UeTR2GQ+sv/6HP90FdmRzu+cHV63oNC2hylXB5 LQKXKjyg40bimyRijL/mV166yQYALtDM7IoVZCRxr3hcklajAynGykDSURIldoOOIatm eCMQay/irmrnW+QJXorYIG9CUzh2Ul0a+AdqpgT8nRnEAhwgHCW3PUMOSrA8ofVaxlUQ 5/yDCftbpmIs4yYZgXDdp+d3PhSXUCbqr1sunMNmmhser3F5t071GXNrmWGQ56m/5jE2 WXFg== X-Forwarded-Encrypted: i=1; AJvYcCXH4jU+pKzkkVOjwoR75LsgnpDkG+HNSip7QagE4tpMVDxwlsZ+M+HLy27kdwjMau/oCJD8z3UGldhe+ZY=@vger.kernel.org X-Gm-Message-State: AOJu0Yw66wNRpWWd2yCi9Spg1V+qdp14X3lOa2lhSD+SZi8BPZSdlKJW qPNag2OO+/5BdORmEeLx4mc7De1MPj38dB1DSYbzsuK8gUkZqx1siK3u2o8Tcw== 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 =3D 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/Kcon= fig 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. =20 +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/Mak= efile index 2dcc6ccc2302..21a9d5047053 100644 --- a/drivers/platform/chrome/Makefile +++ b/drivers/platform/chrome/Makefile @@ -8,6 +8,7 @@ obj-$(CONFIG_CHROMEOS_ACPI) +=3D chromeos_acpi.o obj-$(CONFIG_CHROMEOS_LAPTOP) +=3D chromeos_laptop.o obj-$(CONFIG_CHROMEOS_PRIVACY_SCREEN) +=3D chromeos_privacy_screen.o obj-$(CONFIG_CHROMEOS_PSTORE) +=3D chromeos_pstore.o +obj-$(CONFIG_CHROMEOS_OF_HW_PROBER) +=3D chromeos_of_hw_prober.o obj-$(CONFIG_CHROMEOS_TBMC) +=3D chromeos_tbmc.o obj-$(CONFIG_CROS_EC) +=3D cros_ec.o obj-$(CONFIG_CROS_EC_I2C) +=3D cros_ec_i2c.o diff --git a/drivers/platform/chrome/chromeos_of_hw_prober.c b/drivers/plat= form/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 *d= ata) +{ + const char *type =3D data; + + return i2c_of_probe_component(dev, type); +} + +static const struct hw_prober_entry hw_prober_platforms[] =3D { + { .compatible =3D "google,hana", .prober =3D chromeos_i2c_component_probe= r, .data =3D "touchscreen" }, + { .compatible =3D "google,hana", .prober =3D chromeos_i2c_component_probe= r, .data =3D "trackpad" }, +}; + +static int chromeos_of_hw_prober_probe(struct platform_device *pdev) +{ + for (size_t i =3D 0; i < ARRAY_SIZE(hw_prober_platforms); i++) { + if (!of_machine_is_compatible(hw_prober_platforms[i].compatible)) + continue; + + int ret; + + ret =3D hw_prober_platforms[i].prober(&pdev->dev, hw_prober_platforms[i]= .data); + /* Ignore unrecoverable errors and keep going through other probers */ + if (ret =3D=3D -EPROBE_DEFER) + return ret; + } + + return 0; +} + +static struct platform_driver chromeos_of_hw_prober_driver =3D { + .probe =3D chromeos_of_hw_prober_probe, + .driver =3D { + .name =3D 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 =3D 0; i < ARRAY_SIZE(hw_prober_platforms); i++) + if (of_machine_is_compatible(hw_prober_platforms[i].compatible)) + break; + if (i =3D=3D ARRAY_SIZE(hw_prober_platforms)) + return 0; + + ret =3D platform_driver_register(&chromeos_of_hw_prober_driver); + if (ret) + return ret; + + chromeos_of_hw_prober_pdev =3D + 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"); --=20 2.46.0.rc2.264.g509ed76dc8-goog From nobody Thu Sep 19 23:14:40 2024 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 432B618B484 for ; Thu, 8 Aug 2024 10:00:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723111207; cv=none; b=XyYvgnlnLLSso2RdTOABx12mxu+8BSlRPmBFvgfr7I7AaZtNnCbamVmXyCAVICn3mYXY7uizv9pBlgnPUTYgyfQqcgjGDI7z/f0czj/ZyZ1JWX6i382F+zaf3Th4k8ZUNv9QxTKkgjAqqss0Ct5g2fe5GgI0Pqygw7rG3QTGxyI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723111207; c=relaxed/simple; bh=y4je3VWsBqj5qjHQT037MyYHsD/meXwgpUrJ0PrYeP8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GR4aL6ryy353aC2uvekKGHsrduwMpruVALfSZHVd9R62lwXBw8G2ssIiJYmor9fUsWqasbQEJlTMkOUE3581D1JUPECOxVcUxEDOlpLXT/NpalwqF2lU0BliVUs1whrtxZSxwI1dPvZGUkeVbtCvLxB62wBVmp10EMyLfU/PlUg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=KDge5HAU; arc=none smtp.client-ip=209.85.214.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="KDge5HAU" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1fee6435a34so7387275ad.0 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=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/9/QWNKXSA4i4sxpfMzvxA3Z99pgqscZPbDBz2zlFLI=; b=KDge5HAULCxwG1dAkD5PR47SQ3x9WxzP+e2kF3BHRZJRhI/frnnVoafDpZy0Iu/59w ngscPETIf0D/AFoOVUz3HBVDx0bnEIT6w+DRkucTiDiq6QCxMtoSQydkuRRyhWs7Z35B /y9SBvOvj33GyEREu3RDnitwWTo30tMqErr6k= 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=kaB2RBYgryCad/DXzY4E3C6O4SorDcf2b1NHkabV/Gf2ExToO4CCRefH57Yeuzajii NggK2KtZPNeTO1CKtFmQqMnngRRKI9S7QDgfrTQbCRtb7rKP2Ku6ZJpskQCeIKnO49fX tofPBjRKjqwXXQMrW+G/SrINIGK4uIk7+7AMl10IQ6P92LCLW1L6tEaajKLE8t6bNozy ATuKSzRhj/gbk7OwaOQU4LVk79Yh9JvNV8wBCvR89yglZjMdojELNIpAwk5hXW/VOg0V B7EFtfT8niWyWQ3ee+iCWZfaQ9vpWhQn7/7A8u+KhL1aqTZsS9wi4fspm8btJiK+/CFv kk9g== X-Forwarded-Encrypted: i=1; AJvYcCX566GqxBI3sqmQaByjenkNF0BNGTWJdZ8hkL3eb5ttXV4bn0DLvWsQFNnCLhb6Ha8NoaDmgWomNAkEOaKWvAx49IQ1JESC3GnpFgUU X-Gm-Message-State: AOJu0YxxJZoWrggSGEWhQBDnfBn54KpavxgLw4JJ5KdcebCCDCfVffOe jVjTLiJC6KDikvSXRXnC/icrCruTa9i1bT0f1N5YQdOiKpiZ60NKQO7pt8Tpkg== 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 =3D "melfas,mip4_ts"; reg =3D <0x34>; interrupts-extended =3D <&pio 88 IRQ_TYPE_LEVEL_LOW>; + status =3D "fail-needs-probe"; }; =20 /* @@ -26,6 +27,7 @@ touchscreen3: touchscreen@20 { reg =3D <0x20>; hid-descr-addr =3D <0x0020>; interrupts-extended =3D <&pio 88 IRQ_TYPE_LEVEL_LOW>; + status =3D "fail-needs-probe"; }; =20 /* Lenovo Ideapad C330 uses G2Touch touchscreen as a 2nd source touchscre= en */ @@ -47,9 +49,12 @@ &i2c4 { trackpad2: trackpad@2c { compatible =3D "hid-over-i2c"; interrupts-extended =3D <&pio 117 IRQ_TYPE_LEVEL_LOW>; + pinctrl-names =3D "default"; + pinctrl-0 =3D <&trackpad_irq>; reg =3D <0x2c>; hid-descr-addr =3D <0x0020>; wakeup-source; + status =3D "fail-needs-probe"; }; }; =20 @@ -74,3 +79,11 @@ pins_wp { }; }; }; + +&touchscreen { + status =3D "fail-needs-probe"; +}; + +&trackpad { + status =3D "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 =3D <400000>; status =3D "okay"; - pinctrl-names =3D "default"; - pinctrl-0 =3D <&trackpad_irq>; =20 trackpad: trackpad@15 { compatible =3D "elan,ekth3000"; interrupts-extended =3D <&pio 117 IRQ_TYPE_LEVEL_LOW>; + pinctrl-names =3D "default"; + pinctrl-0 =3D <&trackpad_irq>; reg =3D <0x15>; vcc-supply =3D <&mt6397_vgp6_reg>; wakeup-source; --=20 2.46.0.rc2.264.g509ed76dc8-goog