From nobody Thu Sep 19 19:24:09 2024 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) (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 4DFCE17DFF7 for ; Thu, 22 Aug 2024 09:20:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318426; cv=none; b=UkLWrZxpnBBY8iuuD01h271s9JWrHSeqw7d9xtgQ+S3g+W2qiqPSJoCyDoEVkkhrrnwHaQuyafdnB45mzGEUarHKxkwlLMdtFvXYorYvh8Cg2e0e5W5IxhnwQgzB697/AXNHkbDTzDi/ylZVKZkqseQVXaPTl1PjyzkricO8GGU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318426; c=relaxed/simple; bh=UpiWOo/TltGJssQsJUrLPTTkx2+aS45gddRc0ilmepo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oud4KXITqF6YN4zzcTUStKYaY9kLz7WbJJYofzcgYOt82AEz7TJOBtOrOCMmTxRJofhlMTd65TBc/ARC+d8UhXgAqDKOQJdRQwwtUKtIfdHL9t+a2Vu7XLItlqXEmo0NLt1eqCPjuYDE9QVJjajBfP5gD1BY5ZWoRQVAO1N4F14= 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=P+hiKvZ7; arc=none smtp.client-ip=209.85.210.175 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="P+hiKvZ7" Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-7143165f23fso467391b3a.1 for ; Thu, 22 Aug 2024 02:20:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1724318425; x=1724923225; 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=qJpiCpdFfWAtf8uPPEuTBRfORRdyXl0KF9nHABZqeeQ=; b=P+hiKvZ7sxI+v41o7cHmXNf9yUQYVIAYRy1FyPocKlLnpT7NUYjhEGDKbMX1KFpIQl PMRpe/nb1ceSULkzkObjpT51ZLwXcHRiOaViYBH47zkTSGMs9r+XTwSUPuhXuuAX+Gig 0AGadylDng7CKkGHAgVCOjGNdTqfKkpEy8GCI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724318425; x=1724923225; 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=qJpiCpdFfWAtf8uPPEuTBRfORRdyXl0KF9nHABZqeeQ=; b=L7yn8lf6lnVJLApyVyaMnVqw1CVRNxJsAZRohDEKb4Au0GQYk+TI/Xb0XkGOxgvXYN elQwN/UzyjGeLY6n5wMNkGgKX5JNXIsWv0VLBAF+0Bm2Q2o0AptC6sWmw6/FG6+AvWD3 qrhUU57eoB4ToGi+4FRlyvplJHPOC3Lol4T5GyQOHzufyfh7z6EHh8pOxeZNMUwaH1XY ENGd4OI2BKFUlsaVRGHUUoauVj9SfnuIhvun+X7QRH1uavORn3aGspPpWSOXtnspCdLM OOT5KLNW8EWegsqaHnoM0TYElFcZn39OkneEgfTGFGi7A196ZQpHE1dfPkM7EYDwnCWM slVA== X-Forwarded-Encrypted: i=1; AJvYcCU2DIhgTY6Kt/PC2Vd/zfHojlTS1cjv3pt11V4U5Nsh2HvBQnJXht0K12sxleIG56lmXhD3Ed/gXnNCHTQ=@vger.kernel.org X-Gm-Message-State: AOJu0YwrxhOb9gGb0gJYn+rVs/vy0JHIG8oRaCsgq/pf7RR3jTV1Kc9M AQ2TQq1t+OpNqEW+iy34G6g+6+QdIIw0zx8IyCK7Pdh1O0DJV1zEdU7NampfPA== X-Google-Smtp-Source: AGHT+IF3q7kEnTwTdOeElAbMIG4ut51tj+GRpPErjT+Nd/r+ne8//cdW649YGpsOCjtM4GbM7nHD1A== X-Received: by 2002:a05:6a00:2288:b0:70d:2a4d:2edc with SMTP id d2e1a72fcca58-7142353f2bdmr5844551b3a.20.1724318424602; Thu, 22 Aug 2024 02:20:24 -0700 (PDT) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:8470:6a67:8877:ce2c]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71434335e69sm951398b3a.194.2024.08.22.02.20.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2024 02:20:24 -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 v5 01/10] of: dynamic: Add of_changeset_update_prop_string Date: Thu, 22 Aug 2024 17:19:54 +0800 Message-ID: <20240822092006.3134096-2-wenst@chromium.org> X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog In-Reply-To: <20240822092006.3134096-1-wenst@chromium.org> References: <20240822092006.3134096-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 Reviewed-by: Rob Herring (Arm) --- Changes since v4: - Use modern designated initializer for |prop| 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..daa69d160a78 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 =3D { + .name =3D (char *)prop_name, + .length =3D strlen(str) + 1, + .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.184.g6999bdac58-goog From nobody Thu Sep 19 19:24:09 2024 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) (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 07B3818133F for ; Thu, 22 Aug 2024 09:20:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318430; cv=none; b=KJVhL+TmP/ljwCHf8CqCj2DHszW/IfuryhT5NlagnutQkuwOZ/eVLtwV3peyf9odX3nGdBEQ6Jtb14g/OITh3XaqebTG5aZisEGJY+uPBaOW7Q4qsbKiqlfoctq2Vc/nYlIlmObN0w9+HQbmk5aWNMcu4GPETAmb9I0ElK69c00= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318430; c=relaxed/simple; bh=ELziOpfut8Y9y2QpRYtBASCpGbA0kgK45JJaJ9cGJa0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rWDzvuMtL6gkRYeFOxO/7aDrzUaYww8/MGUskSv2xvrX9u7cfLxiu7txUMqES8jDtuG5gCyj5WghvH3gdWVUFlmkWfDVycb+8+tJSTQCX9bxgnp81LeIutHLyUpIjheKFkZ8T/J6DWS+efCHzo1cEkE3ZFIDehql2X/KVS8cFtc= 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=XCvEyO1k; arc=none smtp.client-ip=209.85.210.172 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="XCvEyO1k" Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-7142448aaf9so403278b3a.1 for ; Thu, 22 Aug 2024 02:20:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1724318428; x=1724923228; 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=IeQTJP2FGJKC+q0TaK5nRU8de0bpWdLOhuHTxIn3w+o=; b=XCvEyO1k0ibjsaSyf8dxzO01+Ky8ND2Ns9P0ab6c608KB6/g4neRfRmIxE/zgZq6zp jNsiiQ7tNVkgbRkYJqg9c9K8Prh0zBzAOUAbknAznEQ/u2e32FzRc1e7vch2PTZcI7Rc TWgD117GPx8qW2LgNlLEuwBGrRLUJsOzXMqYo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724318428; x=1724923228; 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=IeQTJP2FGJKC+q0TaK5nRU8de0bpWdLOhuHTxIn3w+o=; b=TQJABuZZEb5Sp4WygtOJzEDdX40P9dn3BZzFf2UYYnxc2YImmvA8bXA5LJJh2X80dN 66o+0fD7568rmzm4NVmIaSpn9B3NG9af7PDxr8zQB8HNR9JhxmhA0AlvDNZZseyWMLcz +0TRX3lP4uCVheZk+DofDScRSMvOk8jtASqm1oX/ge1BZXdfOXOg6bts5sgPsUrbJLpR S/+ETcQUqdgD/Mlb5Fyh6YcLtxnHIud36GsWULRvjDLiqOSBX/8kvYlT5fiDA3qm+G5i yRw6z9zX34F+LKcCQLWBnjfZUAejkKXYLPfDgtj4ZBitSFZEMxNZr8rKzQBjYZ/uaWPm rgnQ== X-Forwarded-Encrypted: i=1; AJvYcCUlo2yqFXauNi+DI0kGVu5KYjM2lyKDGWGRKGb4JCL+0R7F6QLu8Vs9SqxlDu63jiNUC+kQLMgH1CfTesY=@vger.kernel.org X-Gm-Message-State: AOJu0Yx6pUCdUtOCYHvd+1VUmzMGYr/nJ0I62Y6IZb7rGh/kFof12AxR zj4K/IhxlLHEPm35CKPy+OdHg9RJ6ZApSNbYdLuRZkPeqxWUj3Cox/jYichQ3w== X-Google-Smtp-Source: AGHT+IH60pf2USyhbGl6hcFS70WibHe7v+VnRzo4raEis9sQGEqleOsBie2zhiyldOuUYRp+I/niuQ== X-Received: by 2002:a05:6a21:c94:b0:1ca:ccd0:1bdb with SMTP id adf61e73a8af0-1cae524ea12mr3283455637.20.1724318428213; Thu, 22 Aug 2024 02:20:28 -0700 (PDT) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:8470:6a67:8877:ce2c]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71434335e69sm951398b3a.194.2024.08.22.02.20.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2024 02:20:27 -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 v5 02/10] regulator: Move OF-specific regulator lookup code to of_regulator.c Date: Thu, 22 Aug 2024 17:19:55 +0800 Message-ID: <20240822092006.3134096-3-wenst@chromium.org> X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog In-Reply-To: <20240822092006.3134096-1-wenst@chromium.org> References: <20240822092006.3134096-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" There's still a bit of OF-specific code in the regulator device lookup function. Move those bits of code over to of_regulator.c, and create a new function of_regulator_dev_lookup() to encapsulate the code moved out of regulator_dev_lookup(). Also mark of_find_regulator_by_node() as static, since there are no other users in other compile units. There are no functional changes. Signed-off-by: Chen-Yu Tsai --- Changes since v4: - New patch --- drivers/regulator/core.c | 85 ++------------------------ drivers/regulator/internal.h | 9 +-- drivers/regulator/of_regulator.c | 102 ++++++++++++++++++++++++++++++- 3 files changed, 111 insertions(+), 85 deletions(-) diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c index 9029de5395ee..361309fcae57 100644 --- a/drivers/regulator/core.c +++ b/drivers/regulator/core.c @@ -419,72 +419,6 @@ static void regulator_lock_dependent(struct regulator_= dev *rdev, mutex_unlock(®ulator_list_mutex); } =20 -/** - * of_get_child_regulator - get a child regulator device node - * based on supply name - * @parent: Parent device node - * @prop_name: Combination regulator supply name and "-supply" - * - * Traverse all child nodes. - * Extract the child regulator device node corresponding to the supply nam= e. - * returns the device node corresponding to the regulator if found, else - * returns NULL. - */ -static struct device_node *of_get_child_regulator(struct device_node *pare= nt, - const char *prop_name) -{ - struct device_node *regnode =3D NULL; - struct device_node *child =3D NULL; - - for_each_child_of_node(parent, child) { - regnode =3D of_parse_phandle(child, prop_name, 0); - - if (!regnode) { - regnode =3D of_get_child_regulator(child, prop_name); - if (regnode) - goto err_node_put; - } else { - goto err_node_put; - } - } - return NULL; - -err_node_put: - of_node_put(child); - return regnode; -} - -/** - * of_get_regulator - get a regulator device node based on supply name - * @dev: Device pointer for the consumer (of regulator) device - * @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) -{ - struct device_node *regnode =3D NULL; - char prop_name[64]; /* 64 is max size of property name */ - - dev_dbg(dev, "Looking up %s-supply from device tree\n", supply); - - snprintf(prop_name, 64, "%s-supply", supply); - regnode =3D of_parse_phandle(dev->of_node, prop_name, 0); - - if (!regnode) { - regnode =3D of_get_child_regulator(dev->of_node, prop_name); - if (regnode) - return regnode; - - dev_dbg(dev, "Looking up %s property in node %pOF failed\n", - prop_name, dev->of_node); - return NULL; - } - return regnode; -} - /* Platform voltage constraint check */ int regulator_check_voltage(struct regulator_dev *rdev, int *min_uV, int *max_uV) @@ -2009,7 +1943,6 @@ static struct regulator_dev *regulator_dev_lookup(str= uct device *dev, const char *supply) { struct regulator_dev *r =3D NULL; - struct device_node *node; struct regulator_map *map; const char *devname =3D NULL; =20 @@ -2017,19 +1950,11 @@ static struct regulator_dev *regulator_dev_lookup(s= truct device *dev, =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 (r) - return r; - - /* - * We have a node, but there is no device. - * assume it has not registered yet. - */ - return ERR_PTR(-EPROBE_DEFER); - } + r =3D of_regulator_dev_lookup(dev, supply); + if (!IS_ERR(r)) + return r; + if (PTR_ERR(r) =3D=3D -EPROBE_DEFER) + return r; } =20 /* if not found, try doing it non-dt way */ diff --git a/drivers/regulator/internal.h b/drivers/regulator/internal.h index 77a502141089..ffeaef22169e 100644 --- a/drivers/regulator/internal.h +++ b/drivers/regulator/internal.h @@ -66,7 +66,8 @@ static inline struct regulator_dev *dev_to_rdev(struct de= vice *dev) } =20 #ifdef CONFIG_OF -struct regulator_dev *of_find_regulator_by_node(struct device_node *np); +struct regulator_dev *of_regulator_dev_lookup(struct device *dev, + const char *supply); struct regulator_init_data *regulator_of_get_init_data(struct device *dev, const struct regulator_desc *desc, struct regulator_config *config, @@ -80,10 +81,10 @@ int of_get_n_coupled(struct regulator_dev *rdev); bool of_check_coupling_data(struct regulator_dev *rdev); =20 #else -static inline struct regulator_dev * -of_find_regulator_by_node(struct device_node *np) +static inline struct regulator_dev *of_regulator_dev_lookup(struct device = *dev, + const char *supply) { - return NULL; + return -ENODEV; } =20 static inline struct regulator_init_data * diff --git a/drivers/regulator/of_regulator.c b/drivers/regulator/of_regula= tor.c index d557f7b1ec7c..d924f5c1de59 100644 --- a/drivers/regulator/of_regulator.c +++ b/drivers/regulator/of_regulator.c @@ -550,7 +550,73 @@ struct regulator_init_data *regulator_of_get_init_data= (struct device *dev, return NULL; } =20 -struct regulator_dev *of_find_regulator_by_node(struct device_node *np) +/** + * of_get_child_regulator - get a child regulator device node + * based on supply name + * @parent: Parent device node + * @prop_name: Combination regulator supply name and "-supply" + * + * Traverse all child nodes. + * Extract the child regulator device node corresponding to the supply nam= e. + * returns the device node corresponding to the regulator if found, else + * returns NULL. + */ +static struct device_node *of_get_child_regulator(struct device_node *pare= nt, + const char *prop_name) +{ + struct device_node *regnode =3D NULL; + struct device_node *child =3D NULL; + + for_each_child_of_node(parent, child) { + regnode =3D of_parse_phandle(child, prop_name, 0); + + if (!regnode) { + regnode =3D of_get_child_regulator(child, prop_name); + if (regnode) + goto err_node_put; + } else { + goto err_node_put; + } + } + return NULL; + +err_node_put: + of_node_put(child); + return regnode; +} + +/** + * of_get_regulator - get a regulator device node based on supply name + * @dev: Device pointer for the consumer (of regulator) device + * @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) +{ + struct device_node *regnode =3D NULL; + char prop_name[64]; /* 64 is max size of property name */ + + dev_dbg(dev, "Looking up %s-supply from device tree\n", supply); + + snprintf(prop_name, 64, "%s-supply", supply); + regnode =3D of_parse_phandle(dev->of_node, prop_name, 0); + + if (!regnode) { + regnode =3D of_get_child_regulator(dev->of_node, prop_name); + if (regnode) + return regnode; + + dev_dbg(dev, "Looking up %s property in node %pOF failed\n", + prop_name, dev->of_node); + return NULL; + } + return regnode; +} + +static struct regulator_dev *of_find_regulator_by_node(struct device_node = *np) { struct device *dev; =20 @@ -559,6 +625,40 @@ struct regulator_dev *of_find_regulator_by_node(struct= device_node *np) return dev ? dev_to_rdev(dev) : NULL; } =20 +/** of_regulator_dev_lookup - lookup a regulator device with device tree o= nly + * @dev: Device pointer for regulator supply lookup. + * @supply: Supply name or regulator ID. + * + * 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(). + * On failure one of the following ERR-PTR-encoded values is returned: + * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succ= eed + * in the future. + */ +struct regulator_dev *of_regulator_dev_lookup(struct device *dev, + const char *supply) +{ + struct regulator_dev *r; + struct device_node *node; + + node =3D of_get_regulator(dev, supply); + if (node) { + r =3D of_find_regulator_by_node(node); + of_node_put(node); + if (r) + return r; + + /* + * We have a node, but there is no device. + * assume it has not registered yet. + */ + return ERR_PTR(-EPROBE_DEFER); + } + + return ERR_PTR(-ENODEV); +} + /* * Returns number of regulators coupled with rdev. */ --=20 2.46.0.184.g6999bdac58-goog From nobody Thu Sep 19 19:24:09 2024 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (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 7D66F183CAA for ; Thu, 22 Aug 2024 09:20:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318434; cv=none; b=nGhI5q+gnINHpiiAaDJ5BIcZbXAOeWOQPVKU7/bFHjbW5ZkWdxSf/7htwANsqxV7emxiSEGFbaPlQj51MfD8hYsXNxtctST9NiJtTNQQVs7GXD1wPh729h+EZzHchlN4ZfQuclEZlZA50bYCwcPaHiHeK42vYALSzzfGZg4CkIA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318434; c=relaxed/simple; bh=F4P2nmGS6A9jyMX3gZMS4FBUKJs6ahBUb0pWZbTHLOA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dqyT6hAf7dUDfIIhcdKPz5DGcRc5BPC1bXE8S/u13OYqtsTP+b2yJajQqZrSAamZkiPH7QdUSjUnRVtOxKH7yMSDENe03de+OXJGO+BUtuqLfzt0l2Mmy5hn7G2h5x6Z4K3GputGlkXGwXtw6rtG+exppwhqMkd4JkKWZMHKA/8= 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=D3bNgzNz; arc=none smtp.client-ip=209.85.210.180 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="D3bNgzNz" Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-712603f7ba5so516424b3a.3 for ; Thu, 22 Aug 2024 02:20:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1724318432; x=1724923232; 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=dQ/ais/DnW2DhkO42fRSNxxKCNt0O583PTfpQXJv2Js=; b=D3bNgzNzAG+WeGXfqA9KQXemJQsFfydskP0dW0Xzx2f+K+b3fSXM3u/dphwUnivZsf MogL86NL5kXlIyWcZyE2PoNbj8cFAmRrRH6XgTmTbdEVKfoyPfjMcZT6sSkyCMrxVj7P kTIhEK/DkD3becfGZEdV8QbVCoaE3vr0Oa/yc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724318432; x=1724923232; 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=dQ/ais/DnW2DhkO42fRSNxxKCNt0O583PTfpQXJv2Js=; b=Zbg5ll7M0YEtK3bubK4SKR/rYWjLF6KDuJo0pf+PcsiCjHELD7VlNiLnJD3k1TDRf/ J42lA8jQ7UaFWOwDMADNBRqECTIbxaWtAZzUmFnxWwVE4Xk2a+6vnPrKXQbknpRFBJpH ZRq8e8FqqMlrg4BJOyiiHG3eXGH/LqSkwfkgXoxSpKFjB2V3+NNsxtVxfxBn5sITR8Sx yczAaCgdEGIz0xtltDye2EcGM+hYHcZFqdLoBg03POjjUY+CdaWDcbQt/9tzPA6mdSWk 2cTcCFXYTzjgJGOas3mCjfYWM+KPtVLwQPl3Eftxue1Y+P9E/cQnE5ZgnLlijoLfmoUp Ir9w== X-Forwarded-Encrypted: i=1; AJvYcCWxQ7yINPD39AH+qHtqc2wMOt3R/DbQYYtSXvVrqcrKXrZSzOVvBloFLR9VTP97LXe5ayxC13ud38+fC6E=@vger.kernel.org X-Gm-Message-State: AOJu0Yyn55fhg2f3gHUDq8OFf7R8Rx/UTRiMx6viG36duH2qObphFoTR WGcqpJEU+B+gjMS7JI8BOtCLRljcT/yLC/6BPlf9x6rqOPfZsTjV+PMsVUOoxw== X-Google-Smtp-Source: AGHT+IHNX1mTbdGkDuNm269HAAWFLZMuunhBrLdO555YXJUIWEo+jUShOHk7j43VZg4OPf7/Roe+/g== X-Received: by 2002:a05:6a21:398c:b0:1c6:f9ea:f2df with SMTP id adf61e73a8af0-1cad7f89598mr5999512637.12.1724318431795; Thu, 22 Aug 2024 02:20:31 -0700 (PDT) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:8470:6a67:8877:ce2c]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71434335e69sm951398b3a.194.2024.08.22.02.20.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2024 02:20:31 -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 v5 03/10] regulator: Split up _regulator_get() Date: Thu, 22 Aug 2024 17:19:56 +0800 Message-ID: <20240822092006.3134096-4-wenst@chromium.org> X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog In-Reply-To: <20240822092006.3134096-1-wenst@chromium.org> References: <20240822092006.3134096-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" _regulator_get() contains a lot of common code doing checks prior to the regulator lookup and housekeeping work after the lookup. Almost all the code could be shared with a OF-specific variant of _regulator_get(). Split out the common parts so that they can be reused. The OF-specific version of _regulator_get() will be added in a subsequent patch. No functional changes were made. Signed-off-by: Chen-Yu Tsai --- Changes since v4: - New patch --- drivers/regulator/core.c | 54 ++++++++++++++++++++++++++++-------- drivers/regulator/internal.h | 4 +++ 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c index 361309fcae57..4d1c640cc030 100644 --- a/drivers/regulator/core.c +++ b/drivers/regulator/core.c @@ -2093,26 +2093,43 @@ static int regulator_resolve_supply(struct regulato= r_dev *rdev) return ret; } =20 -/* Internal regulator request function */ -struct regulator *_regulator_get(struct device *dev, const char *id, - enum regulator_get_type get_type) +/* common pre-checks for regulator requests */ +int _regulator_get_common_check(struct device *dev, const char *id, + enum regulator_get_type get_type) { - struct regulator_dev *rdev; - struct regulator *regulator; - struct device_link *link; - int ret; - if (get_type >=3D MAX_GET_TYPE) { dev_err(dev, "invalid type %d in %s\n", get_type, __func__); - return ERR_PTR(-EINVAL); + return -EINVAL; } =20 if (id =3D=3D NULL) { dev_err(dev, "regulator request with no identifier\n"); - return ERR_PTR(-EINVAL); + return -EINVAL; } =20 - rdev =3D regulator_dev_lookup(dev, id); + return 0; +} + +/** + * _regulator_get_common - Common code for regulator requests + * @rdev: regulator device pointer as returned by *regulator_dev_lookup() + * Its reference count is expected to have been incremented. + * @dev: device used for dev_printk messages + * @id: Supply name or regulator ID + * @get_type: enum regulator_get_type value corresponding to type of reque= st + * + * Returns a struct regulator corresponding to @rdev, + * or IS_ERR() condition containing errno. + * + * This function should be chained with *regulator_dev_lookup() functions. + */ +struct regulator *_regulator_get_common(struct regulator_dev *rdev, struct= device *dev, + const char *id, enum regulator_get_type get_type) +{ + struct regulator *regulator; + struct device_link *link; + int ret; + if (IS_ERR(rdev)) { ret =3D PTR_ERR(rdev); =20 @@ -2228,6 +2245,21 @@ struct regulator *_regulator_get(struct device *dev,= const char *id, return regulator; } =20 +/* Internal regulator request function */ +struct regulator *_regulator_get(struct device *dev, const char *id, + enum regulator_get_type get_type) +{ + struct regulator_dev *rdev; + int ret; + + ret =3D _regulator_get_common_check(dev, id, get_type); + if (ret) + return ERR_PTR(ret); + + rdev =3D regulator_dev_lookup(dev, id); + return _regulator_get_common(rdev, dev, id, get_type); +} + /** * regulator_get - lookup and obtain a reference to a regulator. * @dev: device for regulator "consumer" diff --git a/drivers/regulator/internal.h b/drivers/regulator/internal.h index ffeaef22169e..3718459fb0c5 100644 --- a/drivers/regulator/internal.h +++ b/drivers/regulator/internal.h @@ -121,6 +121,10 @@ enum regulator_get_type { MAX_GET_TYPE }; =20 +int _regulator_get_common_check(struct device *dev, const char *id, + enum regulator_get_type get_type); +struct regulator *_regulator_get_common(struct regulator_dev *rdev, struct= device *dev, + const char *id, enum regulator_get_type get_type); struct regulator *_regulator_get(struct device *dev, const char *id, enum regulator_get_type get_type); int _regulator_bulk_get(struct device *dev, int num_consumers, --=20 2.46.0.184.g6999bdac58-goog From nobody Thu Sep 19 19:24:09 2024 Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) (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 1A269183CCB for ; Thu, 22 Aug 2024 09:20:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318437; cv=none; b=Fr/yLYvaHwiLyokzY0AmSxb0IOywy/555tiUVNRbMGvd9SX2r73WfX6seumBisIgD36sDExP20ToYKNMr6xVPQFj/bk0MzFLrRPt+10NX9C/9cPbjhn7S00gKRx2/hAEwhfJDeb7pKRg+jlMqmtrW5umOISCgxJvF2tEKlwh2fk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318437; c=relaxed/simple; bh=eqNJQZhyT5lQYyjR+1n24rDLOldGDkXVEUqXxRauVRY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LMP/I0MWhrZ0QX92ckY3xuC2xu0nnVh6gu/uVDLH5z2ITnRNENWrlOXxr9sNgJ+TeUB8TUUYJUTibafI8Fn2Ytsv8h+z/fQxYxPs/PY1byWEGcOEHFMG/FPj3Wj1oVCzTzHOI6pvrl5rHqjKN2CLOYL7Cs6KjbWaLsUKxCLMlow= 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=BEIB5HSP; arc=none smtp.client-ip=209.85.210.181 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="BEIB5HSP" Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-714261089c1so400985b3a.0 for ; Thu, 22 Aug 2024 02:20:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1724318435; x=1724923235; 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=BDc0gYFzE7XGjhJ2AhN6DupFbhnHtplhsjKD3gx9Rh0=; b=BEIB5HSPmetmvorlFZgKmd3yxuKIXQrMgl67vCLj1lVAe/3KC+kP9Ncthxht0YqAua XG5AAtlFha0K/ehaFocDHM2egv6pE89Kk4Fhfs1AjDcNfaTP4yBziH1JP6RqbYep9N8i fWD15TRdtQQpe21BiWPCv74O3+8ZCf2h9xQCY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724318435; x=1724923235; 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=BDc0gYFzE7XGjhJ2AhN6DupFbhnHtplhsjKD3gx9Rh0=; b=Cxi9iNFNt13JLpIIYC9ISfwLcWqZeZnQWvmiTOCMvjdexDjUBvNunBnzrV8mc5uDkW oqizsCaWz2+Qlw+2/HYybFJC65oIN0IaREJWbEN/QGn55a9DrhNNnWr4PLbE2nCGq7cr NM/Qp8Yaa2qLXubkdCXIlUccGvQiuTjnl7skLfCPW8yLl/Orw556EtA8it+3185Dmdzf 9FoeOqK7M6mEN157Ywq8uhL8gPAc/9MbgCWNlDLGFaNwHAS1ammDnQNUHp6Z5F/+PitG ZMUXLK4rKIsKnou8tNQ6GoQMg/5kXjChocWuFI5OyDpJucqch5nZ35J7C3G+Tc4vfMW/ hI9Q== X-Forwarded-Encrypted: i=1; AJvYcCUBslzJL+yVe0Glo2nwNayQU7hpqZrWrdlD9VkUAQX1tQ0FuPtbP2rdFQ/vXf7DtiO4unmhcEv+mJh3ZYc=@vger.kernel.org X-Gm-Message-State: AOJu0YxRYWkZdlKO+q76ypLPKQVEC7zvHKWz4U+T5znLc/M9bwJmJsU7 C1Bfryd2im1EPC4rvSHXDcJlG2VSIYqQ28ZCQCZi/Xy9Tis8iWwtq0Ri8Cm+iQ== X-Google-Smtp-Source: AGHT+IGFjvk3XcfXwOVUDAxxMj2xSqe1AU1grF2L8qM1UCDTpX9001BmMqg32GNyYPTfbXCI1q3+QQ== X-Received: by 2002:a05:6a21:8804:b0:1c0:ef24:4124 with SMTP id adf61e73a8af0-1cae51ad139mr3146210637.6.1724318435415; Thu, 22 Aug 2024 02:20:35 -0700 (PDT) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:8470:6a67:8877:ce2c]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71434335e69sm951398b3a.194.2024.08.22.02.20.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2024 02:20:35 -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 v5 04/10] regulator: Do pure DT regulator lookup in of_regulator_bulk_get_all() Date: Thu, 22 Aug 2024 17:19:57 +0800 Message-ID: <20240822092006.3134096-5-wenst@chromium.org> X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog In-Reply-To: <20240822092006.3134096-1-wenst@chromium.org> References: <20240822092006.3134096-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. Convert the existing of_regulator_bulk_get_all() for this purpose. This function has no in-tree users, as the original patch [1] that used it was never landed. This patch changes the function ABI, but it is straightforward to convert users. The underlying code that supports the existing regulator_get*() functions has been reworked in previous patches to support this specific case. An internal OF-specific version of regulator_get(), of_regulator_get_optional(), is added for this. [1] https://lore.kernel.org/all/20231220203537.83479-2-jernej.skrabec@gmail= .com/ Signed-off-by: Chen-Yu Tsai --- Changes since v4: - Was "regulator: Add regulator_of_get_optional() for pure DT" in v4 - Restore platform-agnostic regulator consumer code to original state - Move OF-specific regulator code to of_regulator.c (separate patch) - Split _regulator_get() into three parts for reuse (separate patch) - Add OF-specific _of_regulator_get() function - Rename regulator_of_get_optional() to of_regulator_get_optional() for consistency - Make of_regulator_get_optional() static, as it is only used internally - Convert of_regulator_bulk_get_all() Changes since v3: - New patch --- drivers/regulator/core.c | 2 +- drivers/regulator/internal.h | 2 ++ drivers/regulator/of_regulator.c | 54 +++++++++++++++++++++++++++----- 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c index 4d1c640cc030..d70302e30731 100644 --- a/drivers/regulator/core.c +++ b/drivers/regulator/core.c @@ -1950,7 +1950,7 @@ static struct regulator_dev *regulator_dev_lookup(str= uct device *dev, =20 /* first do a dt based lookup */ if (dev && dev->of_node) { - r =3D of_regulator_dev_lookup(dev, supply); + r =3D of_regulator_dev_lookup(dev, dev->of_node, supply); if (!IS_ERR(r)) return r; if (PTR_ERR(r) =3D=3D -EPROBE_DEFER) diff --git a/drivers/regulator/internal.h b/drivers/regulator/internal.h index 3718459fb0c5..44680ce3587b 100644 --- a/drivers/regulator/internal.h +++ b/drivers/regulator/internal.h @@ -67,6 +67,7 @@ static inline struct regulator_dev *dev_to_rdev(struct de= vice *dev) =20 #ifdef CONFIG_OF struct regulator_dev *of_regulator_dev_lookup(struct device *dev, + struct device_node *np, const char *supply); struct regulator_init_data *regulator_of_get_init_data(struct device *dev, const struct regulator_desc *desc, @@ -82,6 +83,7 @@ bool of_check_coupling_data(struct regulator_dev *rdev); =20 #else static inline struct regulator_dev *of_regulator_dev_lookup(struct device = *dev, + struct device_node *np, const char *supply) { return -ENODEV; diff --git a/drivers/regulator/of_regulator.c b/drivers/regulator/of_regula= tor.c index d924f5c1de59..67a5882a917e 100644 --- a/drivers/regulator/of_regulator.c +++ b/drivers/regulator/of_regulator.c @@ -587,22 +587,24 @@ 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 + * @dev: Device pointer for dev_printk messages + * @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 *dev, struct dev= ice_node *node, + const 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); + dev_dbg(dev, "Looking up %s-supply from device node %pOF\n", supply, node= ); =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); @@ -626,7 +628,8 @@ static struct regulator_dev *of_find_regulator_by_node(= struct device_node *np) } =20 /** of_regulator_dev_lookup - lookup a regulator device with device tree o= nly - * @dev: Device pointer for regulator supply lookup. + * @dev: Device pointer for dev_printk messages. + * @node: Device node pointer for regulator supply lookup. * @supply: Supply name or regulator ID. * * If successful, returns a struct regulator_dev that corresponds to the n= ame @@ -636,13 +639,13 @@ static struct regulator_dev *of_find_regulator_by_nod= e(struct device_node *np) * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succ= eed * in the future. */ -struct regulator_dev *of_regulator_dev_lookup(struct device *dev, +struct regulator_dev *of_regulator_dev_lookup(struct device *dev, struct d= evice_node *np, const char *supply) { struct regulator_dev *r; struct device_node *node; =20 - node =3D of_get_regulator(dev, supply); + node =3D of_get_regulator(dev, np, supply); if (node) { r =3D of_find_regulator_by_node(node); of_node_put(node); @@ -659,6 +662,41 @@ struct regulator_dev *of_regulator_dev_lookup(struct d= evice *dev, return ERR_PTR(-ENODEV); } =20 +static struct regulator *_of_regulator_get(struct device *dev, struct devi= ce_node *node, + const char *id, enum regulator_get_type get_type) +{ + struct regulator_dev *r; + int ret; + + ret =3D _regulator_get_common_check(dev, id, get_type); + if (ret) + return ERR_PTR(ret); + + r =3D of_regulator_dev_lookup(dev, node, id); + return _regulator_get_common(r, dev, id, get_type); +} + +/** + * of_regulator_get_optional - get optional regulator via device tree look= up + * @dev: device used for dev_printk messages + * @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(). + */ +static struct regulator *of_regulator_get_optional(struct device *dev, + struct device_node *node, + const char *id) +{ + return _of_regulator_get(NULL, node, id, OPTIONAL_GET); +} + /* * Returns number of regulators coupled with rdev. */ @@ -875,7 +913,7 @@ int of_regulator_bulk_get_all(struct device *dev, struc= t device_node *np, } else { memcpy(name, prop->name, i); name[i] =3D '\0'; - tmp =3D regulator_get(dev, name); + tmp =3D of_regulator_get_optional(dev, np, name); if (IS_ERR(tmp)) { ret =3D PTR_ERR(tmp); goto error; --=20 2.46.0.184.g6999bdac58-goog From nobody Thu Sep 19 19:24:09 2024 Received: from mail-oo1-f42.google.com (mail-oo1-f42.google.com [209.85.161.42]) (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 12AA1185923 for ; Thu, 22 Aug 2024 09:20:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318441; cv=none; b=k+RA4ynQSd7NfAEG4s9qzpY5N7KRTITGwjxCnSQckDeIPzudzXhNI10Vrn5uxrPiJHjrl1DGKh/zWOwpFJDq0tbLjZ/bw6+f0LJ105zmbGEi6uR55D3oHoUqlK/fpIVAxUIFpCF/w3mEVC/567qMIt/YFddBkC+jp2l2/eEF5O0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318441; c=relaxed/simple; bh=gTgPY7UWqR1vyoGV1ZZgVjMMPO2UUtQdJrOqh6+4QXA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ufckBBKjBbuS/AJjAqn/pGdc3y2574Aw6o9OdkkedaCUbHYAniV5OjhkjcBtt4CCVx7Q1teb2aMi4kngAaomiXNZsVmESVmOA6GVu8ER0KPJIjSlqrTacyhMfg/lCq+xS+Nq+v2iFiMR8uoM90lEo8jCCCEEQWdGJis4NjixeNU= 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=Ioz3Hmpt; arc=none smtp.client-ip=209.85.161.42 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="Ioz3Hmpt" Received: by mail-oo1-f42.google.com with SMTP id 006d021491bc7-5d5bb03fe42so350636eaf.3 for ; Thu, 22 Aug 2024 02:20:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1724318439; x=1724923239; 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=OGdOo23OjShZKKUPhP64Sm0t2nst66j12GqHlKAMLTI=; b=Ioz3Hmpt8bCGpwu8UUbaIZtxnwiQj7ErDtRA6qcjKoUyBc9Rk38KPG9nNqLcB1+Dgx ET2O3tZsPkoilopB7weaOA51RE6+7UMfTuLF6vjPlvRq0RGif5ClUVe+8kzmkeDGflcz BnUsJSzC0BWNM/pXLCGbVdpM2ELKLLIsB3Y2A= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724318439; x=1724923239; 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=OGdOo23OjShZKKUPhP64Sm0t2nst66j12GqHlKAMLTI=; b=Qn8LKSas+r0qe3QzEIW+5IsPjr2fu9bM1IzDeRq8QybYB+3SQvp0GhLf1KP8iuOpiU sF7+TM3Bjo8NUxwTFERQmiuHOOjH+BUlb4M54JfnYd/4xIcNWltYudidNWjtBvvssvWJ 2PQZprxpQoWpdn8fq0+UtCR+Ff7YR9iXoPpKQCEeTswQHhCHS7nfudgPenQ0yGaFGr+f fjtH7RhZAHySmD+QiMYbBJ8P8z8wdYIaqwwt4Hn4TfW6o6uCvtocAqBxbs0DTdMw3HKW YBHtDww03XberthZYeaiHyd7zxbvytjuVJ8kFn7NyIhNGlDvsz1zr2tZGSHbuKDky3rS D+BA== X-Forwarded-Encrypted: i=1; AJvYcCWPEtWSF95Clm8hfc4ZxQUBkzVCWETpzyYaj+B1MlVOz3bfGLG57jsoTKT5XhRU7Vj/sIZcuhUeCQpSdNM=@vger.kernel.org X-Gm-Message-State: AOJu0YwRTmiS8BmNLfv7bxb01ftuVd6Y/ko4Ye/CENp4Khp7xYqRhk5p G6h2ISEz6VZhNHkHA+OtZLOeRxxLukBWV2BBbnR0GOpmPC35LZfZjOWVw7U19Q== X-Google-Smtp-Source: AGHT+IGg4vXGxgKXD8IY1cy882/n6HazEz1G403uPDeFfsv+SBpWVZIvn3E7WqFDu7Zn4wJDa5xJAA== X-Received: by 2002:a05:6358:2624:b0:1af:3b39:8183 with SMTP id e5c5f4694b2df-1b59fce12c4mr581766055d.20.1724318439044; Thu, 22 Aug 2024 02:20:39 -0700 (PDT) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:8470:6a67:8877:ce2c]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71434335e69sm951398b3a.194.2024.08.22.02.20.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2024 02:20:38 -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 v5 05/10] gpiolib: Add gpio_property_name_length() Date: Thu, 22 Aug 2024 17:19:58 +0800 Message-ID: <20240822092006.3134096-6-wenst@chromium.org> X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog In-Reply-To: <20240822092006.3134096-1-wenst@chromium.org> References: <20240822092006.3134096-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 I2C device tree component prober needs to get and toggle GPIO lines for the components it intends to probe. These components may not use the same name for their GPIO lines, so the prober must go through the device tree, check each property to see it is a GPIO property, and get the GPIO line. Instead of duplicating the GPIO suffixes, or exporting them to the prober to do pattern matching, simply add and export a new function that does the pattern matching and returns the length of the GPIO name. The caller can then use that to copy out the name if it needs to. Signed-off-by: Chen-Yu Tsai --- Changes since v4: - new patch Depends on commit 4b91188dced8 ("gpiolib: Replace gpio_suffix_count with NULL-terminated array"). --- drivers/gpio/gpiolib.c | 44 +++++++++++++++++++++++++++++++++++ include/linux/gpio/consumer.h | 2 ++ 2 files changed, 46 insertions(+) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 3903d0a75304..e4228ef6f131 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -4295,6 +4295,50 @@ struct gpio_desc *fwnode_gpiod_get_index(struct fwno= de_handle *fwnode, } EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index); =20 +/** + * gpio_property_name_length - Returns the GPIO name length from a propert= y name + * @str: string to check + * + * This function checks if the given name matches the GPIO property patter= ns, and + * returns the length of the name of the GPIO. The pattern is "*-" + * or just "". + * + * Returns: + * The length of the string before '-' if it matches "*-", or + * 0 if no name part, just the suffix, or + * -EINVAL if the string doesn't match the pattern. + */ +int gpio_property_name_length(const char *str) +{ + size_t len; + + len =3D strlen(str); + + /* string need to be at minimum len(gpio) */ + if (len < 4) + return -EINVAL; + + /* Check for no-name case: "gpio" / "gpios" */ + for (const char *const *p =3D gpio_suffixes; *p; p++) + if (!strcmp(str, *p)) + return 0; + + for (size_t i =3D len - 4; i > 0; i--) { + /* find right-most '-' and check if remainder matches suffix */ + if (str[i] !=3D '-') + continue; + + for (const char *const *p =3D gpio_suffixes; *p; p++) + if (!strcmp(str + i + 1, *p)) + return i; + + return -EINVAL; + } + + return -EINVAL; +} +EXPORT_SYMBOL_GPL(gpio_property_name_length); + /** * gpiod_count - return the number of GPIOs associated with a device / fun= ction * or -ENOENT if no GPIO has been assigned to the requested function diff --git a/include/linux/gpio/consumer.h b/include/linux/gpio/consumer.h index db2dfbae8edb..ce3a5f86a037 100644 --- a/include/linux/gpio/consumer.h +++ b/include/linux/gpio/consumer.h @@ -56,6 +56,8 @@ enum gpiod_flags { =20 #ifdef CONFIG_GPIOLIB =20 +int gpio_property_name_length(const char *name); + /* Return the number of GPIOs associated with a device / function */ int gpiod_count(struct device *dev, const char *con_id); =20 --=20 2.46.0.184.g6999bdac58-goog From nobody Thu Sep 19 19:24:09 2024 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.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 61B74185949 for ; Thu, 22 Aug 2024 09:20:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318445; cv=none; b=TjTgz1JI2pkTAxDZgozJ2KsddpWO96apyoEFy1qixg5iIIop1kQgrr8Om/vIoHECQ2GZs9BWodJyp0Tw+rCe9NVYGDSGsU3ppxVhvcxJT5hMuTPY8K51tv6MgXdKxMwHf7oHQIZUguGggEFcqS7dwZ5O7HvK4UgBIe05nFlSQPI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318445; c=relaxed/simple; bh=Wgu1A4d18OeFbkPMAo4yiGBwGlPss71+5Ioi+fHta+w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Vqu7q1ROPqpurZdyM0mhh1Vk2a01Pk0Rd6xZFgW72xns9G6aRJe6F0N+NXq4Q4KYFYFQuozSnoMqI5N0NaJboYY+PvD5CJQBk5FaQKPWbaSpy4+n8J/YkySE5YR0o4FnArpY/7F1Cl1DkqQDBmzR6vmCqgv/i4Qtpx9C9l5nNWc= 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=aJAEW0lU; arc=none smtp.client-ip=209.85.210.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="aJAEW0lU" Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-71431524f33so519713b3a.1 for ; Thu, 22 Aug 2024 02:20:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1724318443; x=1724923243; 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=cxRueDbPxY1fgYOETHJnt8bZkNktfZ7UPvnhXd9unUQ=; b=aJAEW0lUNkpcpVRdXpv2ou8QTk/FiaF4b4smdMYXfZ36swlqd1iXPVFgXat49t6b5B +oW0jmp1Kgjub20a4kBPU9+A5vz5uT/tumFftfVpUY6IEG0magIBqqqXV3VuNa1PHxV3 qlfGhmcgPSSVptZ4PH1llyX0V4Yi1VNWdKGGs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724318443; x=1724923243; 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=cxRueDbPxY1fgYOETHJnt8bZkNktfZ7UPvnhXd9unUQ=; b=FWC6Fh9ZTEeXuRvsbLCcuHrnP/Wj7KIrCN24vmIVZcp/Pov4RhtFFqvHF9g1l0IkOt jc4VBAw33uPyDdIhRz8urET0MowTRdgrQ9uZww9eEXREqUpuLDcGSzvETlXWTScdGBnR eRo2z/mUmLYpYvuE01g12sre1w3MKZ3AaFOkSIxlcX3w8pUvMtRvBzJBJ17cmzwlkUka SPdmwXtSseDYmQ+y1xAOvxT0itoBJCTOEompV//RmgBipo4W1uADMAU2bkK6I4lTZhPx E79QwtAhB1F+IizRW+/Jg4myl9GOqj5hvx6EhM/sFhCmBO/umo+doBcRZsos+bjETHqU mmNQ== X-Forwarded-Encrypted: i=1; AJvYcCUqL2UtIVBMGJJJPrEwmE1TSFUYs59cSmkJMMhoQsRPD9K2QJzR4GWX6aEGTI/uGs2dSw7pyMofhYxOM9w=@vger.kernel.org X-Gm-Message-State: AOJu0Yw8bT4sAPKRcq9z/ZMGBCwIRdzvJ0A0yOf95ojgW/4UjGCvY9uh DnjHaYUVjOaHWdcHOMc3qs0WvBdtUR20SO1M/7AN4ZJ5ZvU7rfXHCov0Ps5HTQ== X-Google-Smtp-Source: AGHT+IFtRkYpkCzlZNxo5hRLezdqYzrhH87tUkIsFl9WPCXve7xcF75QepgPZpqioalAlfO4Fwqhgw== X-Received: by 2002:a05:6a20:9f4f:b0:1c4:8dc0:84f0 with SMTP id adf61e73a8af0-1cad814457dmr6336351637.43.1724318442623; Thu, 22 Aug 2024 02:20:42 -0700 (PDT) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:8470:6a67:8877:ce2c]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71434335e69sm951398b3a.194.2024.08.22.02.20.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2024 02:20:42 -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 v5 06/10] i2c: Introduce OF component probe function Date: Thu, 22 Aug 2024 17:19:59 +0800 Message-ID: <20240822092006.3134096-7-wenst@chromium.org> X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog In-Reply-To: <20240822092006.3134096-1-wenst@chromium.org> References: <20240822092006.3134096-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 v4: - Split code into helper functions - Use scoped helpers and __free() to reduce error path Changes since v3: - Complete kernel-doc - Return different error if I2C controller is disabled - Expand comment to explain assumptions and constraints - Split for-loop finding target node and operations on target node - Add missing i2c_put_adapter() - Move prober code to separate file Rob also asked why there was a limitation of "exactly one touchscreen will be enabled across the whole tree". The use case this prober currently targets is a component on consumer electronics (tablet or laptop) being swapped out due to cost or supply reasons. Designs with multiple components of the same type are pretty rare. The way the next patch is written also assumes this for efficiency reasons. Changes since v2: - New patch split out from "of: Introduce hardware prober driver" - Addressed Rob's comments - Move i2c prober to i2c subsystem - Use of_node_is_available() to check if node is enabled. - Use OF changeset API to update status property - Addressed Andy's comments - Probe function now accepts "struct device *dev" instead to reduce line length and dereferences - Move "ret =3D 0" to just before for_each_child_of_node(i2c_node, node) --- drivers/i2c/Makefile | 1 + drivers/i2c/i2c-core-of-prober.c | 157 +++++++++++++++++++++++++++++++ include/linux/i2c.h | 4 + 3 files changed, 162 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..bb7b231201b0 --- /dev/null +++ b/drivers/i2c/i2c-core-of-prober.c @@ -0,0 +1,157 @@ +// 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 +#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 + */ + +static struct device_node *i2c_of_probe_get_i2c_node(struct device *dev, c= onst char *type) +{ + struct device_node *node __free(device_node) =3D of_find_node_by_name(NUL= L, type); + if (!node) + return dev_err_ptr_probe(dev, -ENODEV, "Could not find %s device node\n"= , type); + + struct device_node *i2c_node __free(device_node) =3D of_get_parent(node); + if (!of_node_name_eq(i2c_node, "i2c")) + return dev_err_ptr_probe(dev, -EINVAL, "%s device isn't on I2C bus\n", t= ype); + + if (!of_device_is_available(i2c_node)) + return dev_err_ptr_probe(dev, -ENODEV, "I2C controller not available\n"); + + return no_free_ptr(i2c_node); +} + +static int i2c_of_probe_enable_node(struct device *dev, struct device_node= *node) +{ + int ret; + + dev_info(dev, "Enabling %pOF\n", node); + + struct of_changeset *ocs __free(kfree) =3D kzalloc(sizeof(*ocs), GFP_KERN= EL); + if (!ocs) + return -ENOMEM; + + of_changeset_init(ocs); + ret =3D of_changeset_update_prop_string(ocs, node, "status", "okay"); + if (ret) + return ret; + + ret =3D of_changeset_apply(ocs); + if (!ret) { + /* + * ocs is intentionally kept around as it needs to + * exist as long as the change is applied. + */ + void *ptr __always_unused =3D no_free_ptr(ocs); + } else { + /* ocs needs to be explicitly cleaned up before being freed. */ + of_changeset_destroy(ocs); + } + + return ret; +} + +/** + * i2c_of_probe_component() - probe for devices of "type" on the same i2c = bus + * @dev: &struct device of the caller, only used for dev_* printk messages + * @type: a string to match the device node name prefix to probe for + * + * Probe for possible I2C components of the same "type" on the same I2C bus + * that have their status marked as "fail". + * + * Assumes that across the entire device tree the only instances of nodes + * prefixed with "type" are the ones that need handling for second source + * components. In other words, if type is "touchscreen", then all device + * nodes named "touchscreen*" are the ones that need probing. There cannot + * be another "touchscreen" node that is already enabled. + * + * Assumes that for each "type" of component, only one actually exists. In + * other words, only one matching and existing device will be enabled. + * + * Context: Process context only. Does non-atomic I2C transfers. + * Should only be used from a driver probe function, as the 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 i2c_adapter *i2c; + int ret; + + struct device_node *i2c_node __free(device_node) =3D i2c_of_probe_get_i2c= _node(dev, type); + if (IS_ERR(i2c_node)) + return PTR_ERR(i2c_node); + + for_each_child_of_node_scoped(i2c_node, node) { + if (!of_node_name_prefix(node, type)) + continue; + if (!of_device_is_available(node)) + continue; + + /* + * Device tree has component already enabled. Either the + * device tree isn't supported or we already probed once. + */ + return 0; + } + + i2c =3D of_get_i2c_adapter_by_node(i2c_node); + if (!i2c) + return dev_err_probe(dev, -EPROBE_DEFER, "Couldn't get I2C adapter\n"); + + ret =3D 0; + for_each_child_of_node_scoped(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; + + /* Found a device that is responding */ + ret =3D i2c_of_probe_enable_node(dev, node); + break; + } + + i2c_put_adapter(i2c); + + return ret; +} +EXPORT_SYMBOL_GPL(i2c_of_probe_component); diff --git a/include/linux/i2c.h b/include/linux/i2c.h index 377def497298..4c48ea93ec49 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.184.g6999bdac58-goog From nobody Thu Sep 19 19:24:09 2024 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.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 101AF185B44 for ; Thu, 22 Aug 2024 09:20:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318448; cv=none; b=os56pQqK2mh3SpAZ37Ub4XKTvNIij3Ms9yk3p7KuJxjeZhZAlTTlgzp4/iwhdyT3rU4EiroMiY3Xw28FKbxu6Tp+b5kGRXtGA8B/WwGyvwFFlgZz/+GRdLgvuNLC/t8xEzu85oEOv2S6FpyIttS9g35Sq3VYsP/atwIzpGcw4ZM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318448; c=relaxed/simple; bh=piTyNsw3eHoDVnCDv3Hd5LIe3UIXKM9fGlm4l0G6M4I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bzRP9Gbx/Jghn/wpNvnotBuoO8Cm2JRTRcxV5RmBIiCFa0X41c/B20c7Jd8CSYAhWw72BxiqMbo/H5ZF8Ef1XN4MkYxit0nFP3gtAaijOS/CRuQE4AhiiekjRkwY58HGXwetsHFqvhqSF2mgWg4iVy8Jcds9z4XAWXX6ZBr7RiE= 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=b0aCzI8f; arc=none smtp.client-ip=209.85.210.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="b0aCzI8f" Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-7142448aaf9so403474b3a.1 for ; Thu, 22 Aug 2024 02:20:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1724318446; x=1724923246; 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=nK7RVXbUa73K0O9XTM3cCy4ZRtIeA4akoAuNuVfnhl4=; b=b0aCzI8fUfKtdObudEFySP2PcchUWQ183cYvuZRGU/xJUlS8dzR5AGAkhPlIjDJ6mu 1qvJ6o6BLcYcg4pgNqn35fWhvDOX7Gz+f3zB1u5/JZoTkXw0T8o0cmyYXR3GYbfaD84S J9xs/4a8abXHwJMnC+OXbkdzGyXVdG0kv6YcM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724318446; x=1724923246; 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=nK7RVXbUa73K0O9XTM3cCy4ZRtIeA4akoAuNuVfnhl4=; b=diT+7v9uFpSI1h+QOa5nXsFXqYst3IuTXwUDkp/K0G6cJTOHe8EF6YgHo95oxknXzk 1X5FgNWe4N8GzxViWVaqMSGTftnC1mx/7mVAtkIFiB6aKnkEVTrbbulZ8M8E/c7xyS/N YvqLf4+8JXsczd0rW9iCyTFyUjideX4Z70d/WUagQ6HblTcHmuRd3lwI5jLwgmQHipkc CrQMBajWhfKQPJnru/pn7vS89uOkSXIqV9SLIU2y/KlQVFqslK+XXDXI5/dQQHayPqqf 2F92vJYkQTb7Rsn9rxShsNXE8nyF8aRbrQqjnCxrLJrAzGGisSp1uCh6VgNSPrDb7ssY ZP2A== X-Forwarded-Encrypted: i=1; AJvYcCV8htiIEPozWPXm6WkwaQhKWeOP6ThoZMczC3OhALunY9hc8JU1uqj6+/1APHfejOS1I1yxzVyTZBJtS+U=@vger.kernel.org X-Gm-Message-State: AOJu0Yz+OfKKF3bB1ThBwOSZCWIAuiRbWUqtY9NREjLryHVj/Z+255C2 XB2kajhajk48n+pPbfWDkAmVvlicugTYIoVdN5kLmxhtdl9e7w9kQ+Bk8rxDFA== X-Google-Smtp-Source: AGHT+IFZrknrh60AP+xUNW7G6X8rXlnZ0nYSqqA2Jq4MMQC2PUO1ifpAUkr7X0okzBKnbPbPM0MW6w== X-Received: by 2002:a05:6a00:2d81:b0:70e:cee8:264a with SMTP id d2e1a72fcca58-7143170e827mr4568474b3a.1.1724318446207; Thu, 22 Aug 2024 02:20:46 -0700 (PDT) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:8470:6a67:8877:ce2c]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71434335e69sm951398b3a.194.2024.08.22.02.20.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2024 02:20:45 -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 v5 07/10] i2c: of-prober: Add regulator support Date: Thu, 22 Aug 2024 17:20:00 +0800 Message-ID: <20240822092006.3134096-8-wenst@chromium.org> X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog In-Reply-To: <20240822092006.3134096-1-wenst@chromium.org> References: <20240822092006.3134096-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 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. GPIOs will be handled in the next patch. 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 v4: - Split out GPIO handling to separate patch - Rewrote using of_regulator_bulk_get_all() - Replaced "regulators" with "regulator supplies" in debug messages 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 | 114 ++++++++++++++++++++++++++++++- 1 file changed, 113 insertions(+), 1 deletion(-) diff --git a/drivers/i2c/i2c-core-of-prober.c b/drivers/i2c/i2c-core-of-pro= ber.c index bb7b231201b0..32184cfd10f6 100644 --- a/drivers/i2c/i2c-core-of-prober.c +++ b/drivers/i2c/i2c-core-of-prober.c @@ -6,12 +6,14 @@ */ =20 #include +#include #include #include #include #include #include #include +#include #include =20 /* @@ -27,10 +29,93 @@ * address responds. * * TODO: - * - Support handling common regulators and GPIOs. + * - Support handling common GPIOs. * - Support I2C muxes */ =20 +struct i2c_of_probe_data { + struct regulator_bulk_data *regulators; + unsigned int regulators_num; +}; + +/* Returns number of regulator supplies found for node, or error. */ +static int i2c_of_probe_get_regulator(struct device *dev, struct device_no= de *node, + struct i2c_of_probe_data *data) +{ + struct regulator_bulk_data *tmp, *new_regulators; + int ret; + + ret =3D of_regulator_bulk_get_all(dev, node, &tmp); + if (ret <=3D 0) + return ret; + + if (!data->regulators) { + data->regulators =3D tmp; + data->regulators_num =3D ret; + return ret; + }; + + new_regulators =3D krealloc(data->regulators, + sizeof(*tmp) * (data->regulators_num + ret), + GFP_KERNEL); + if (!new_regulators) { + regulator_bulk_free(ret, tmp); + return -ENOMEM; + } + + data->regulators =3D new_regulators; + + for (unsigned int i =3D 0; i < ret; i++) + memcpy(&data->regulators[data->regulators_num++], &tmp[i], sizeof(*tmp)); + + return ret; +} + +static void i2c_of_probe_free_res(struct i2c_of_probe_data *data) +{ + regulator_bulk_free(data->regulators_num, data->regulators); +} + +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; + + ret =3D i2c_of_probe_get_regulator(dev, node, data); + if (ret < 0) { + dev_err_probe(dev, ret, "Failed to get regulator supplies from %pOF\n", = node); + 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; + + dev_dbg(dev, "Enabling regulator supplies\n"); + + ret =3D regulator_bulk_enable(data->regulators_num, data->regulators); + if (ret) + return ret; + + /* largest post-power-on pre-reset-deassert delay seen among drivers */ + msleep(500); + + return 0; +} + +static void i2c_of_probe_disable_regulators(struct i2c_of_probe_data *data) +{ + regulator_bulk_disable(data->regulators_num, data->regulators); +} + static struct device_node *i2c_of_probe_get_i2c_node(struct device *dev, c= onst char *type) { struct device_node *node __free(device_node) =3D of_find_node_by_name(NUL= L, type); @@ -110,6 +195,7 @@ static int i2c_of_probe_enable_node(struct device *dev,= struct device_node *node int i2c_of_probe_component(struct device *dev, const char *type) { struct i2c_adapter *i2c; + struct i2c_of_probe_data probe_data =3D {0}; int ret; =20 struct device_node *i2c_node __free(device_node) =3D i2c_of_probe_get_i2c= _node(dev, type); @@ -133,6 +219,30 @@ int i2c_of_probe_component(struct device *dev, const c= har *type) if (!i2c) 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, &probe_data); + if (ret) + return ret; + } + + dev_dbg(dev, "Resources: # of regulator supplies =3D %d\n", probe_data.re= gulators_num); + + /* Enable resources */ + ret =3D i2c_of_probe_enable_res(dev, &probe_data); + if (ret) { + i2c_of_probe_free_res(&probe_data); + return dev_err_probe(dev, ret, "Failed to enable resources\n"); + } + ret =3D 0; for_each_child_of_node_scoped(i2c_node, node) { union i2c_smbus_data data; @@ -150,6 +260,8 @@ int i2c_of_probe_component(struct device *dev, const ch= ar *type) break; } =20 + i2c_of_probe_disable_regulators(&probe_data); + i2c_of_probe_free_res(&probe_data); i2c_put_adapter(i2c); =20 return ret; --=20 2.46.0.184.g6999bdac58-goog From nobody Thu Sep 19 19:24:09 2024 Received: from mail-oo1-f48.google.com (mail-oo1-f48.google.com [209.85.161.48]) (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 AD93F185949 for ; Thu, 22 Aug 2024 09:20:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318452; cv=none; b=qPHNXOaloxXw0NZBkoBBXD2PDQrTvcVbdfj7qQBsO3xm/3VQs66grm92Vay0q69MAJX+nqdWLPyAeWZ6AyKju9Qz5vQ6DdVa4ME2mkEYY5pOwALp7scoLb9R1IJPkIFRUuKvmXW9A1tM+roqGhNcEvM3nepOCUdG87hMo61Y7DY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318452; c=relaxed/simple; bh=7PXjrfDF7BHqqcZZ2pniX9XcCoIrPJyShubjWsTVSL0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YT4HOIqko7FF6e/USsKfg0UoG3smhpniNaFm/jhhKNEtC5tLzQLdYwL6wz39eJtV/uYJGXHQrJr2n8+/oQwdIL5x4uv/4Fc0js46UWvWE7ywXfgRdvw13tTvwB1VsSOHvc9z1gxzY21kv/H67rHC1SCjVOOAuajVl9mYBedWcQQ= 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=E7hUJViD; arc=none smtp.client-ip=209.85.161.48 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="E7hUJViD" Received: by mail-oo1-f48.google.com with SMTP id 006d021491bc7-5d5ed6f51cfso382567eaf.0 for ; Thu, 22 Aug 2024 02:20:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1724318450; x=1724923250; 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=oIuVa6xe5T9fPXAC3Ljp+tme57B/5X5NOOiRmoSNKDs=; b=E7hUJViD+HmX0CYsgTOuwYI7w6pVZ/Vasyorf3znHakonQgbIfNKSjk3+mwWj8GM66 UmhsG9pxdZlLdtwuyGIX7BHFeohxDYMAlHTOxJ0+6+jQx7Ro3fIoWg/ppUriAoLhpsR5 EZx02Ha8fUFmJwiAgvnfkNYeHP94XtiWjGhNw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724318450; x=1724923250; 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=oIuVa6xe5T9fPXAC3Ljp+tme57B/5X5NOOiRmoSNKDs=; b=rUh1Dk1Qt/DBjGrHBBQZexdG1hyJTRmcHin/FwtjIFfN3UDW1KnfMZzIg1YxZOG0wF 0BGVHHKyN9tWuIyirob04M2kq5TE7SN9dJi2jTE1ayexWz2yZQnHoCwIC/LgJXLFiWf9 cuJzrNUOFWKa0u90CECwm50tO7Ifb55dKxg9ph86k5HYT1BNYVbWAbiscMgiRoUdOnAn cxeYMXWlK8UgtIBwatx3Ev48+PgpdLoBx94x0oX/3rq9IOBQEREb6Q9DsifL9mKQTxLM uXnJWjSR2HRwPjW0ybmpkqCNGL5EauYugrsCFVU0IB+eGisYo9PeSZIn8OwF5Pq88ZQV z3eg== X-Forwarded-Encrypted: i=1; AJvYcCXxOt5lB+dyMLcttLWOUgtKGkbeOhB2nIo/FVqKL8p81os1G+KsGp0XLW53phokVtIbaFgtsJNxbPjnUi0=@vger.kernel.org X-Gm-Message-State: AOJu0Yw6iktAZbkR0AefWw3DCulS1G4/gKXQe22wd9ejmEqijev2n5VS eJl3AI3vPxhCjz/8ES7y3sMWQ73XU3j7Hzux3hir8QqqnKq4SA6uOlfPQjOKSw== X-Google-Smtp-Source: AGHT+IE7GKAi5GcWfleItjZNggbXuAhjFv7VDxVtEAzSHPTiX6tW2gRy6Kxe6kC20IJZDqjxfuUcSg== X-Received: by 2002:a05:6870:7a07:b0:260:fc49:3e96 with SMTP id 586e51a60fabf-273cff98fcbmr1222495fac.46.1724318449803; Thu, 22 Aug 2024 02:20:49 -0700 (PDT) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:8470:6a67:8877:ce2c]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71434335e69sm951398b3a.194.2024.08.22.02.20.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2024 02:20:49 -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 v5 08/10] i2c: of-prober: Add GPIO support Date: Thu, 22 Aug 2024 17:20:01 +0800 Message-ID: <20240822092006.3134096-9-wenst@chromium.org> X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog In-Reply-To: <20240822092006.3134096-1-wenst@chromium.org> References: <20240822092006.3134096-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 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. regulator support was added in the previous patch. 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 v4: - Split out from previous patch - Moved GPIO property name check to common function in gpiolib.c in new patch - Moved i2c_of_probe_free_gpios() into for_each_child_of_node_scoped() - Rewrote in gpiod_*_array-esque fashion --- drivers/i2c/i2c-core-of-prober.c | 126 ++++++++++++++++++++++++++++++- 1 file changed, 124 insertions(+), 2 deletions(-) diff --git a/drivers/i2c/i2c-core-of-prober.c b/drivers/i2c/i2c-core-of-pro= ber.c index 32184cfd10f6..046e6605053c 100644 --- a/drivers/i2c/i2c-core-of-prober.c +++ b/drivers/i2c/i2c-core-of-prober.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -29,11 +30,11 @@ * address responds. * * TODO: - * - Support handling common GPIOs. * - Support I2C muxes */ =20 struct i2c_of_probe_data { + struct gpio_descs *gpiods; struct regulator_bulk_data *regulators; unsigned int regulators_num; }; @@ -71,8 +72,88 @@ static int i2c_of_probe_get_regulator(struct device *dev= , struct device_node *no return ret; } =20 +/* + * Returns 1 if property is GPIO and GPIO successfully requested, + * 0 if not a GPIO property, or error if request for GPIO failed. + */ +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_descs *gpiods; + struct gpio_desc *gpiod; + char con[32]; /* 32 is max size of property name */ + char *con_id =3D NULL; + size_t new_size; + int len; + + len =3D gpio_property_name_length(prop->name); + if (len < 0) + return 0; + + if (len >=3D sizeof(con) - 1) { + pr_err("%pOF: length of GPIO name \"%s\" exceeds current limit\n", + node, prop->name); + return -EINVAL; + } + + if (len > 0) { + strscpy(con, prop->name, len + 1); + con_id =3D con; + } + + /* + * 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). If the initial request fails with + * -EBUSY, retry with GPIOD_FLAGS_BIT_NONEXCLUSIVE and see if it matches + * any existing ones. + */ + gpiod =3D fwnode_gpiod_get_index(fwnode, con_id, 0, GPIOD_ASIS, "i2c-of-p= rober"); + if (IS_ERR(gpiod)) { + int i; + + if (PTR_ERR(gpiod) !=3D -EBUSY || !data->gpiods) + return PTR_ERR(gpiod); + + gpiod =3D fwnode_gpiod_get_index(fwnode, con_id, 0, + GPIOD_ASIS | GPIOD_FLAGS_BIT_NONEXCLUSIVE, + "i2c-of-prober"); + for (i =3D 0; i < data->gpiods->ndescs; i++) + if (gpiod =3D=3D data->gpiods->desc[i]) + return 1; + + return -EBUSY; + } + + new_size =3D struct_size(gpiods, desc, data->gpiods ? data->gpiods->ndesc= s + 1 : 1); + gpiods =3D krealloc(data->gpiods, new_size, GFP_KERNEL); + if (!gpiods) { + gpiod_put(gpiod); + return -ENOMEM; + } + + data->gpiods =3D gpiods; + data->gpiods->desc[data->gpiods->ndescs++] =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) +{ + if (data->gpiods) + gpiod_put_array(data->gpiods); + data->gpiods =3D NULL; +} + static void i2c_of_probe_free_res(struct i2c_of_probe_data *data) { + i2c_of_probe_free_gpios(data); regulator_bulk_free(data->regulators_num, data->regulators); } =20 @@ -88,6 +169,18 @@ static int i2c_of_probe_get_res(struct device *dev, str= uct device_node *node, goto err_cleanup; } =20 + for_each_property_of_node(node, prop) { + dev_dbg(dev, "Trying property %pOF/%s\n", node, prop->name); + + /* 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; =20 err_cleanup: @@ -98,6 +191,7 @@ static int i2c_of_probe_get_res(struct device *dev, stru= ct device_node *node, static int i2c_of_probe_enable_res(struct device *dev, struct i2c_of_probe= _data *data) { int ret =3D 0; + int gpio_i; =20 dev_dbg(dev, "Enabling regulator supplies\n"); =20 @@ -108,7 +202,32 @@ static int i2c_of_probe_enable_res(struct device *dev,= struct i2c_of_probe_data /* largest post-power-on pre-reset-deassert delay seen among drivers */ msleep(500); =20 + if (!data->gpiods) + return 0; + + for (gpio_i =3D 0; gpio_i < data->gpiods->ndescs; 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->desc[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->desc[gpio_i], 0); + regulator_bulk_disable(data->regulators_num, data->regulators); + + return ret; } =20 static void i2c_of_probe_disable_regulators(struct i2c_of_probe_data *data) @@ -234,7 +353,9 @@ int i2c_of_probe_component(struct device *dev, const ch= ar *type) return ret; } =20 - dev_dbg(dev, "Resources: # of regulator supplies =3D %d\n", probe_data.re= gulators_num); + dev_dbg(dev, "Resources: # of GPIOs =3D %d, # of regulator supplies =3D %= d\n", + probe_data.gpiods ? probe_data.gpiods->ndescs : 0, + probe_data.regulators_num); =20 /* Enable resources */ ret =3D i2c_of_probe_enable_res(dev, &probe_data); @@ -256,6 +377,7 @@ int i2c_of_probe_component(struct device *dev, const ch= ar *type) continue; =20 /* Found a device that is responding */ + i2c_of_probe_free_gpios(&probe_data); ret =3D i2c_of_probe_enable_node(dev, node); break; } --=20 2.46.0.184.g6999bdac58-goog From nobody Thu Sep 19 19:24:09 2024 Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) (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 2C9AF185B65 for ; Thu, 22 Aug 2024 09:20:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318455; cv=none; b=pD3AtT1jqJw2RiCdf3OYdbWuhTWisUyLeHR4Q8pMQD+cpnMul2pJ/ODPxQDb1r5PTGjfStqegusDPvEB9j4GsS9LG1JHBJSMFoGqKjjC5NoGpqUdAYdPvdjHJjc1iR46nugu+/POcvnHUzbljDIsZarsO68daJd3BvWp43oUTMY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318455; c=relaxed/simple; bh=yw39hontu2rPsw2Dfc+BlHwqRCTi/IleBZWbIffDIlg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iX9dFknT6tV1eelmrXike0Pw4YxdowJeefADe/zqpZTKLSJTNJErrIUzd7MI/jHu4mQTrA1J00DA1EIaHOrFYaMEuE/u5mqlGbpKJ8SgyJvSMmGB2qIXnA4D7msGynv6RJXEuP1n9jyQifJD0DZuWOcyjJSRN3VOFLL/VhTl/4o= 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=FyhsGKCc; arc=none smtp.client-ip=209.85.210.170 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="FyhsGKCc" Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-7142e4dddbfso508102b3a.0 for ; Thu, 22 Aug 2024 02:20:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1724318453; x=1724923253; 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=eQmK8T4bq0VH3g9O2efQWUIbRipH0PsfI9tyjRvqgic=; b=FyhsGKCcDsgGTniFDBB4K2RlidUSGRH+85KVFRmAL9EVHTEWvWlIil+17ZRs38FRa+ NCq4wqqYqKmaY+ULJP7xWK5PY22z1dAJdgTQDjbH5KS9dcG2gXVzhZyN4PWEXJV4wQVu uO5WfoQGZ4+89dUAbIvgmnIxT0COngZQbaQEo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724318453; x=1724923253; 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=eQmK8T4bq0VH3g9O2efQWUIbRipH0PsfI9tyjRvqgic=; b=sEpErtsn5OyySC9aGpf44NPqCKU3I4ZnI0+7x6lW8JhDQizhVZQNr6xzxuICuu2fOK aHSc9Pv4FG5L0W9N0lp6fT+ObOhIbFG+HFcXWIns+Cx1BaAY1Di/Hru5+PvPGSQpjX1x 1ctwClEhDL0tewGvRQe6w5FU7vTSN0mIdBnILJtQU9dRT3HpAWDx0oK+AbA8jiCgJ0qE WYIxMuI2G1RsylWE6YDWddpNnDe5gjQLCG9WWnmnBZ4iQ3SsCXRdK8sv+0JxcYo4nSiw +SJVzU45+9S2SOe/DvGG68DomarD/BYHWgcKV6aFa6cLeJJKqUtuRfGUFx3aJIYP4MNf 3PAw== X-Forwarded-Encrypted: i=1; AJvYcCVffV1kmomuCR+NzCtXny8YySoEMlKx2yq9zt8dHO8gFCkUmjc4ZgBdFg57rxzBM+ejKcU/QAWjP/7mYEo=@vger.kernel.org X-Gm-Message-State: AOJu0Yyj9NhMzrzloZsGJVRrde+1D+kcTms4sKbPPq9fYkMfLeZoi9PG F92Bas/opdVRWcAhAiTFsA8Erkqj7GVm+Jo0Bobt++MoQCHNdfQS52bAV1sbkg== X-Google-Smtp-Source: AGHT+IH/kwtT/Qw1863OxoM8Pwh9F7kQ2eVlIqLy6rBTQ85cFpmTCAX1lJy/jdi51ORwhwbRPiou2Q== X-Received: by 2002:a05:6a00:6f29:b0:70e:98e2:fdb5 with SMTP id d2e1a72fcca58-7143676376dmr1111472b3a.29.1724318453372; Thu, 22 Aug 2024 02:20:53 -0700 (PDT) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:8470:6a67:8877:ce2c]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71434335e69sm951398b3a.194.2024.08.22.02.20.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2024 02:20:53 -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 v5 09/10] platform/chrome: Introduce device tree hardware prober Date: Thu, 22 Aug 2024 17:20:02 +0800 Message-ID: <20240822092006.3134096-10-wenst@chromium.org> X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog In-Reply-To: <20240822092006.3134096-1-wenst@chromium.org> References: <20240822092006.3134096-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 v4: - Fix Kconfig dependency - Update copyright year - Drop "linux/of.h" header - Include "linux/errno.h" - Move |int ret| declaration to top of block - Return -ENODEV on no match instead of 0 - Unregister platform driver and device unconditionally after previous change 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 | 104 ++++++++++++++++++ 3 files changed, 116 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..b7dbaf77b6db 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 + 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..acf643a9eebe --- /dev/null +++ b/drivers/platform/chrome/chromeos_of_hw_prober.c @@ -0,0 +1,104 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * ChromeOS Device Tree Hardware Prober + * + * Copyright (c) 2024 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++) { + int ret; + + if (!of_machine_is_compatible(hw_prober_platforms[i].compatible)) + continue; + + 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 -ENODEV; + + 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) +{ + 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.184.g6999bdac58-goog From nobody Thu Sep 19 19:24:09 2024 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) (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 8937D185B7E for ; Thu, 22 Aug 2024 09:20:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318459; cv=none; b=JiUhcuUx+qZXng4cbHtu5j5ASenA1EJAgiWSbPDu2fzQzCZcIQ74S44sSVzm+sfhtBAyDmCN6tRcyZCiGi/OB2KE2xgqqsCRBeHc/orPaC1Ny72174mrU/zjBEKOR/qS5HmYJS+SFT7OUWv6eL4J7ucLkdrC7dHb76Yh4M9oEtg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318459; c=relaxed/simple; bh=KPKZx+o2UwLiNaiDemeY/MCCfCJXoZ6VeunQ3tP7UxE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PXkMJh9nTzwGEXveJ10v7cCwhqwrAVWR7gvzk7GvxFEfqC0CHawZ6yUsrK+XNDSN/ejYfs2/EEZ47id6l62WVMOgMyP+aoz1th059OPsZechyQLea1rIOw0FWOHnYoa/YwCJOGKVRLr5L5cfsPP51/mWQmNAI34efORQWZKxgks= 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=n0CPqB4X; arc=none smtp.client-ip=209.85.210.177 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="n0CPqB4X" Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-7141b04e7b5so362171b3a.2 for ; Thu, 22 Aug 2024 02:20:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1724318457; x=1724923257; 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=b0ZXsCUO90vlD45ZJO1I+QT/1oidv8iaRsna2l2Dubk=; b=n0CPqB4Xuvhg7W13cy97dJnxh0HwAyaWacv3xLpLC2/r5kr5P1l5iLCavXUhIIEB1I twt9Nao6GLLHerKxIYfFzFUneIdbZX+Ut5Lqc+yS18po3ldEKV/e9jEe9z55HqV4fbVp UmBU7pr8ttSOrBwUXW+tRlj/inWDn+cWUHRpU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724318457; x=1724923257; 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=b0ZXsCUO90vlD45ZJO1I+QT/1oidv8iaRsna2l2Dubk=; b=ID7NG3xSOvU04/t4YodBchPPmDE9iWAyiTycT8nGGyLYGET6IepirITgKwpz1W7QnY o9FDjZC0g7nxi2Ribi/hMLcvfWGy7Senkn4m1kqhG3XwiLfnp2TEqG4o8QZjgNJrFwGc drH6B2zKA+MydoTxzfOVa30bbwRkJjgGL8WTks9aW3FZVo8YRvo07tlb+OENV+y8bJvX CUdJ1EkbQ+j66ZnaWEG9mZV5b6gsyKl95tVvkZCIWgfmNKiJaOH4pbyrxzg09Ry2heI+ 0Fa3fS/zBkGUHEk63HTZa+FjDU2XljhiQzbrfn5uBt9SOMs4z+hdxhiacGrorcJFytT+ oJ7g== X-Forwarded-Encrypted: i=1; AJvYcCXiKI2W+aWBa7xT7jJb/ya2NhxHaECdTSu+42q5KgDwZV1nU7pGcwSOYLsAZkXmurc/Jhpug6gRqIzx48k=@vger.kernel.org X-Gm-Message-State: AOJu0YwsYfQ62HD0zukAt2+P1IAaOBKDBmHl6yPF6Lqm4pRhtj5/nycS eYqbuTkdLU8vF5EXZRMztPe/xx4YWysqBfFYtoQUQAziunIAYcIWr38HZMoBVg== X-Google-Smtp-Source: AGHT+IGopbbojtmE4fS/IUOg0cpO0JEZHF2VnB3L6eDK3aI3tJHchVj0lRN1Md7MFI5PPKTGcEf3AQ== X-Received: by 2002:a05:6a20:2d26:b0:1c6:fb07:381e with SMTP id adf61e73a8af0-1cad8180a2amr6660606637.44.1724318456919; Thu, 22 Aug 2024 02:20:56 -0700 (PDT) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:8470:6a67:8877:ce2c]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71434335e69sm951398b3a.194.2024.08.22.02.20.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2024 02:20:56 -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 v5 10/10] arm64: dts: mediatek: mt8173-elm-hana: Mark touchscreens and trackpads as fail Date: Thu, 22 Aug 2024 17:20:03 +0800 Message-ID: <20240822092006.3134096-11-wenst@chromium.org> X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog In-Reply-To: <20240822092006.3134096-1-wenst@chromium.org> References: <20240822092006.3134096-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.184.g6999bdac58-goog