From patchwork Thu Aug 22 09:19:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen-Yu Tsai X-Patchwork-Id: 13773091 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 5EAF7C3DA4A for ; Thu, 22 Aug 2024 09:22:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=qJpiCpdFfWAtf8uPPEuTBRfORRdyXl0KF9nHABZqeeQ=; b=GLPQVZiSjvN0LcZ7nSzQZOqgSM /2gsfHIdeAeQiH3g5Yt8rq0rEJZADUORjwzzuvmsHNUivc62+l7lgAVdHHrGKr3rc+lksnZ8xNg9Z 0CJMlI3F2Q4f6HljDve/JYeykRtHw9QvmWyvlCI4aqmjhq/ijV38FERZHaIsQsWrcifNQN3CVat+Q sxPMqEGmT32JzCcJW9VoDJM9Mf8WDHBPBQvWpTMGRg7Y13ojmq0T17X+kokvBb4Z1jhBO+52d/UuC X81z+DR0k3S0BJ69FOTMULfByWg2PNqzTIZPKBGlKpvTuqeUBV++kqwZv15k2BSpgDLMrccwkolGH vfrc7YGg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh40z-0000000CCck-21R7; Thu, 22 Aug 2024 09:21:57 +0000 Received: from mail-pf1-x433.google.com ([2607:f8b0:4864:20::433]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh3zV-0000000CC7R-2wnS for linux-arm-kernel@lists.infradead.org; Thu, 22 Aug 2024 09:20:27 +0000 Received: by mail-pf1-x433.google.com with SMTP id d2e1a72fcca58-713edc53429so467894b3a.2 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=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=qJpiCpdFfWAtf8uPPEuTBRfORRdyXl0KF9nHABZqeeQ=; b=NBVZqQAUQFeADrS8oQZv/6kVdy63Norf/niOHs3xNr7tCcztdyhJpQQXCBj91fb9wK JEWuHBvYuSGSohc9HIRdEC8maPdZMq8cGZtvNLTggtg1y2HPZd2XTJZbgCt7YJDjmRAh Fr+u/krqzKqnHUMmD0XCsjzl2E9PxpG1I2wVY= 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=qfHvaZEqgvy4FEWb0Aib9xX+dpal4QeN2dK7L69p+dLkc/53F38iwq5J1Pon1zdFa3 84C+ZbDpioWED7rJUbhCpc5mutX0VDKdRScrb6FkxYmIYqPuJDoyq47Y75FGl4JfNOXE 7cOt+ttrryCmhaNehein4AJ8Ur3prd2i4Z3PfZNoN7FtOz7TElHOhhNg0TaTVBIVbuXq gm4hwh4xGGB0p3YvEpQlz6pxPkXkQncCHOZ6EfGHep2w2C+4k8hGoU/eW2jLrH2HbK1d gvp1UPN/Qhv3/kFgTd4iiq6/FCL/HXxqyz8ow8wEMjnZeyLl2kX8fX0ot9aDHWHBlcty V9pA== X-Forwarded-Encrypted: i=1; AJvYcCXMfe+LcTJiy5C+IewqWRv/p6AcBD1oOOB2wseSJDkXWpe5RqZYHlUs5PUwMIEn2dywCu9VWezdJbbY0z214wkO@lists.infradead.org X-Gm-Message-State: AOJu0YxOFZoh6eH5mTXkF9X5EcNgjfH6sEoSXYzsYJOvMIQ3tJURyUjK cgfM20lEb12X5H8yZ0Vu1qIHlZ699VHmr1KQZy3sFLHgjkirSzBqpeZbR15KiQ== 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> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240822_022025_859966_340D6FC1 X-CRM114-Status: GOOD ( 18.05 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add a helper function to add string property updates to an OF changeset. This is similar to of_changeset_add_prop_string(), but instead of adding the property (and failing if it exists), it will update the property. This shall be used later in the DT hardware prober. Signed-off-by: Chen-Yu Tsai 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 = __of_prop_dup(pp, GFP_KERNEL); + if (!new_pp) + return -ENOMEM; + + ret = of_changeset_update_property(ocs, np, new_pp); + if (ret) + __of_prop_free(new_pp); + + return ret; +} + +/** + * of_changeset_update_prop_string - Add a string property update to a changeset + * + * @ocs: changeset pointer + * @np: device node pointer + * @prop_name: name of the property to be updated + * @str: pointer to null terminated string + * + * Create a string property to be updated and add it to a changeset. + * + * Return: 0 on success, a negative error value in case of an error. + */ +int of_changeset_update_prop_string(struct of_changeset *ocs, + struct device_node *np, + const char *prop_name, const char *str) +{ + struct property prop = { + .name = (char *)prop_name, + .length = strlen(str) + 1, + .value = (void *)str, + }; + + return of_changeset_update_prop_helper(ocs, np, &prop); +} +EXPORT_SYMBOL_GPL(of_changeset_update_prop_string); diff --git a/include/linux/of.h b/include/linux/of.h index 85b60ac9eec5..046283be1cd3 100644 --- a/include/linux/of.h +++ b/include/linux/of.h @@ -1651,6 +1651,10 @@ static inline int of_changeset_add_prop_u32(struct of_changeset *ocs, return of_changeset_add_prop_u32_array(ocs, np, prop_name, &val, 1); } +int of_changeset_update_prop_string(struct of_changeset *ocs, + struct device_node *np, + const char *prop_name, const char *str); + int of_changeset_add_prop_bool(struct of_changeset *ocs, struct device_node *np, const char *prop_name); From patchwork Thu Aug 22 09:19:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen-Yu Tsai X-Patchwork-Id: 13773092 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8F2E0C5320E for ; Thu, 22 Aug 2024 09:22:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=IeQTJP2FGJKC+q0TaK5nRU8de0bpWdLOhuHTxIn3w+o=; b=CTJkrG4XsaCgAaJbY5WORLD5O4 Z2+zdXhXv//rS5+cMZHFbl6jgcKQRFzH17lvha+Ut/txIvGbwk0IOK1GFBepmZj0dfb+u4hjNt0sk l9GoNobxfMxmYJFf7y+5KOIfMye3zecqMqaiE64uCd1RuVfUD9YtfjbK8j+VhILb/Qg+LRZ7YzWud H0b7WovTUqIsgZDErmQ1kq/QtTdt5obEGfGNKV2V/UfbUsbnAEPqrOJxfSpX08xuCwTGGQDRkb/Zr hl+7shtE5PQ8kBAbASH6AuJO4i8EFMNc2WTdm3pSSmWOBx7WmnDTZyrcuc4UAc9ZHgZcnLgH45cYX JbP2sSTQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh41j-0000000CCqR-2Gnc; Thu, 22 Aug 2024 09:22:43 +0000 Received: from mail-pf1-x436.google.com ([2607:f8b0:4864:20::436]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh3zZ-0000000CC9S-0yP4 for linux-arm-kernel@lists.infradead.org; Thu, 22 Aug 2024 09:20:30 +0000 Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-7142448aaf9so403275b3a.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=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=IeQTJP2FGJKC+q0TaK5nRU8de0bpWdLOhuHTxIn3w+o=; b=gFciJYL3/mWHf267xlzbx8uezMNuLoyvcOkyjcTo8UmQU487kPZSUpn5H5Tu2LcZol 722LS+gF+lCkeGJK2d0yQSNGlRG4zL5kSNIlfAkas0KSjzeZi16j+o6ngi9I2RSUu/Qk m5hMpdKod8A5Tk2eqGLSpZlb7VRpc0D3rumJE= 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=GGZRfNN4KMA6o5sBBp11+gLlRCu00lh5k2MIjCsW/KFqxuOCZIxOkQk/7vca+OCcfG s88X/eYaCs+Y4loqMeBiAWGC8Dp/q/gS/1TGgOFPeMmkKbol5rC4nnMt9tzgEB//qXBv MP3yvPyJUZE2/U26NxHkJ85f3L3bOElC2G7lUdfbWbYepxFwLd+3SzryKl90PcyRIOth feVQAfYMH5s3ve0vJRC0OjSxNEvJ0ojE59ps+BUmBKgL6+FrV0RIfJT4tsDp9gE11NF/ 4WB60A4bKwhK5MsJVcI9gKllYygxDaBVf426NQGN0wnj5w9JaJtZOlxGMaAYdsa3Yq8g mCsg== X-Forwarded-Encrypted: i=1; AJvYcCUYN6Go7OeTPHCuDKT+IJl41bYXKDXjrsaskbWFxyFy+YuBAGk0nNdj/mrqKGpzfwYM4XLqK03OyTTniaHiB+uo@lists.infradead.org X-Gm-Message-State: AOJu0Ywi7kt3M9r0dL/Rnez9GSYFBZ0GCT9H8AFoxqj8w8OASnZQfpAM RZHoKG4V27deXBbCRrFqAm3GQj8IuNDnCojsg6Bb8vgCeJbHUatTV2RqBxPL1Q== 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> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240822_022029_332574_BBD3BBBB X-CRM114-Status: GOOD ( 26.37 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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); } -/** - * 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 name. - * returns the device node corresponding to the regulator if found, else - * returns NULL. - */ -static struct device_node *of_get_child_regulator(struct device_node *parent, - const char *prop_name) -{ - struct device_node *regnode = NULL; - struct device_node *child = NULL; - - for_each_child_of_node(parent, child) { - regnode = of_parse_phandle(child, prop_name, 0); - - if (!regnode) { - regnode = 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 = 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 = of_parse_phandle(dev->of_node, prop_name, 0); - - if (!regnode) { - regnode = 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(struct device *dev, const char *supply) { struct regulator_dev *r = NULL; - struct device_node *node; struct regulator_map *map; const char *devname = NULL; @@ -2017,19 +1950,11 @@ static struct regulator_dev *regulator_dev_lookup(struct device *dev, /* first do a dt based lookup */ if (dev && dev->of_node) { - node = of_get_regulator(dev, supply); - if (node) { - r = of_find_regulator_by_node(node); - of_node_put(node); - if (r) - return r; - - /* - * We have a node, but there is no device. - * assume it has not registered yet. - */ - return ERR_PTR(-EPROBE_DEFER); - } + r = of_regulator_dev_lookup(dev, supply); + if (!IS_ERR(r)) + return r; + if (PTR_ERR(r) == -EPROBE_DEFER) + return r; } /* 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 device *dev) } #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); #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; } static inline struct regulator_init_data * diff --git a/drivers/regulator/of_regulator.c b/drivers/regulator/of_regulator.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; } -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 name. + * returns the device node corresponding to the regulator if found, else + * returns NULL. + */ +static struct device_node *of_get_child_regulator(struct device_node *parent, + const char *prop_name) +{ + struct device_node *regnode = NULL; + struct device_node *child = NULL; + + for_each_child_of_node(parent, child) { + regnode = of_parse_phandle(child, prop_name, 0); + + if (!regnode) { + regnode = 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 = 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 = of_parse_phandle(dev->of_node, prop_name, 0); + + if (!regnode) { + regnode = 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; @@ -559,6 +625,40 @@ struct regulator_dev *of_find_regulator_by_node(struct device_node *np) return dev ? dev_to_rdev(dev) : NULL; } +/** of_regulator_dev_lookup - lookup a regulator device with device tree only + * @dev: Device pointer for regulator supply lookup. + * @supply: Supply name or regulator ID. + * + * If successful, returns a struct regulator_dev that corresponds to the name + * @supply and with the embedded struct device refcount incremented by one. + * The refcount must be dropped by calling put_device(). + * On failure one of the following ERR-PTR-encoded values is returned: + * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed + * 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 = of_get_regulator(dev, supply); + if (node) { + r = 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. */ From patchwork Thu Aug 22 09:19:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen-Yu Tsai X-Patchwork-Id: 13773093 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7478AC3DA4A for ; Thu, 22 Aug 2024 09:23:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=dQ/ais/DnW2DhkO42fRSNxxKCNt0O583PTfpQXJv2Js=; b=pK5uLj8g5bhMB8qbz8DUEAvTZe 2Ldc9kyhxnRTk58b4GfYFaW4pdPsAra5A+inr0CoVfn83PINdgAyRsdA4+/mO+WFeg/CxqVMSzdpA BkHfPskgNHER3sT4FTePRoUjTqs6BEHuqMGEUzxzNjGx6GZQyM/HXbV330oifRD4wHEsnQTgyWeIc n/H8MvxNDelhooipo5/W46mnkG/wxEFs0oWpf6eJ0gwHJiInB1AihuhL5M+r+B1PNlRKhG4ypkQ5i vdxjyVB3AhrgG6c8r2FgXm57gMC8Xkh4/dE5Ke0GRVgMyVhVrlIK4gPe2O3BC83SdFRGooC6pnKjI 9AfcHSEg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh42R-0000000CD36-1Cgr; Thu, 22 Aug 2024 09:23:27 +0000 Received: from mail-pg1-x52d.google.com ([2607:f8b0:4864:20::52d]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh3zc-0000000CCAt-3AgP for linux-arm-kernel@lists.infradead.org; Thu, 22 Aug 2024 09:20:34 +0000 Received: by mail-pg1-x52d.google.com with SMTP id 41be03b00d2f7-7b0c9bbddb4so415906a12.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=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=dQ/ais/DnW2DhkO42fRSNxxKCNt0O583PTfpQXJv2Js=; b=gr+KiuNYnEI60KfZqAS8J2I6viN5WnCjnZ/B/R/gkYiSeiXWCfCJjZog8SQAdsaE2l QRoQg4eYwevhhZD5yK8hhJojP4jKw9Z/TAa1t0Csv0JFuZyCmxCt3mlTKxhnITACs/pe luQs690HG6VHS/iG/HUGZznK4SxVQmWuF5cLQ= 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=uVgcdOTJDM4v/jqbgFvXZcbsP9QZqZ/HNK+AerPetnJZ9W7fW5K0dLq8GlTe9L6jm4 cUyk0cYn8Ih4PEd3rH4ZugLcBI0a1CkhkyUrEwyD6SZdmUjoZKJGloXsRDTagKJtEQRJ 39zTKmuqnNnxzJurlD79/KDugCuRRQIdKaxR2oF3oVFezi4s1x5MroYMkR6hKnBO+aMC 2BDlIHv39mHW7YhlU2MeiQSI3rJtlA7txMcJxjHTe2WeIvnm8e/5fDvSwwoh+Y/7F04W vnUsw9n2C0qjrAD/vIeDQtYoxE7ub7HwZrYDY+QDBOU3ptaHsyrMIJthJPu7xBbaO5mr s3fA== X-Forwarded-Encrypted: i=1; AJvYcCUDJz6Asw6B6597QcBhU88c7jUCieK8HGCV8I4qU1ZqEbPwk6Z0mgxIMRFbcm01bPVRx0b810LRtQNtv9UueFmf@lists.infradead.org X-Gm-Message-State: AOJu0Ywj9O77yLZWYOdwC1LPWa3uJKpbI7BPUER/5hZtdj8Z0pqZQT2j aQVweFdL5ongkfTdU/8XnxoExDEi0gdbEAcGseP1DlRYiNnMS/3jOzMQ2+zsNw== 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> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240822_022032_824803_F06B7B09 X-CRM114-Status: GOOD ( 19.60 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org _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 regulator_dev *rdev) return ret; } -/* 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 >= MAX_GET_TYPE) { dev_err(dev, "invalid type %d in %s\n", get_type, __func__); - return ERR_PTR(-EINVAL); + return -EINVAL; } if (id == NULL) { dev_err(dev, "regulator request with no identifier\n"); - return ERR_PTR(-EINVAL); + return -EINVAL; } - rdev = 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 request + * + * 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 = PTR_ERR(rdev); @@ -2228,6 +2245,21 @@ struct regulator *_regulator_get(struct device *dev, const char *id, return regulator; } +/* 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 = _regulator_get_common_check(dev, id, get_type); + if (ret) + return ERR_PTR(ret); + + rdev = 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 }; +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, From patchwork Thu Aug 22 09:19:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen-Yu Tsai X-Patchwork-Id: 13773094 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3B59CC5320E for ; Thu, 22 Aug 2024 09:24:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=BDc0gYFzE7XGjhJ2AhN6DupFbhnHtplhsjKD3gx9Rh0=; b=mvxklf3cZRMvgVrAQHKhwG02Xz umig8qcEPEb1dAvaL+0X9sCMdKX4pe06x2eCrVn2gEpnkulGOvp1fPTjGv33Pa9gKiYQqS1CcPVZ0 KTrKdcSO+7OmcAaqSGXS8+XJkC0AOz2xX8zNZ7rIQ6bMPPcmC87474j65NwvRC/epQvfrIdgMp47u B6Qy3/8V/DKuHXnVzkyeA5qyi1Gn3KFXHQxoV/sS4Ft2kHn7/aPQ8h22SeBqZHjio1cji8jZ3O4CE BFMnsiI+On98vqTiyNlcaje0kTlG9FS35NNDtP+tdmfKy4wtPDdo5ZwXCAhE0243BdqCcIUOh+WqN MJ9KDK2Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh439-0000000CDIQ-2NUl; Thu, 22 Aug 2024 09:24:11 +0000 Received: from mail-pf1-x430.google.com ([2607:f8b0:4864:20::430]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh3zg-0000000CCCL-2Zpj for linux-arm-kernel@lists.infradead.org; Thu, 22 Aug 2024 09:20:39 +0000 Received: by mail-pf1-x430.google.com with SMTP id d2e1a72fcca58-7142448aaf9so403372b3a.1 for ; Thu, 22 Aug 2024 02:20:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1724318435; x=1724923235; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=BDc0gYFzE7XGjhJ2AhN6DupFbhnHtplhsjKD3gx9Rh0=; b=TAgFQJlqgVPpkVCDSyjj45l5xSOrUjwCdiypbV8UFVwjj0VLLx3flglb66GmSKGChr GqngwKlQsLbmCDH3OQkRXsWlVK7W40bqx81tDyZR3rMk625nKdn1QXweGuXuH67aTnoG 530R/IRsRL/KUegc8RzxuYwUJkicnOTRBd/eY= 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=nvGJGyz3buMPA/wCVNbJBEfroqStFTtD2CuQA/MKkHUA3UERigICCzN7FyPbpD9jUT AAMzd+YaTFNbJz/4r2C6Wq0Wp0QpaNs8OR/B04jDNJh1nt2S3+nbocwshWpN6THGoZ6i 1C7Do+ykg8le2xUylSb2qb07czLJ98nJt4imMd7mF50DTwCoeq1K0H9tdY5FKBWVPOXi iR/LS9GIhJwJxtaJCEkipQ0wn2Tcp80s3eNOdcRNDTRd0kveDZVEEryn2hDYXU9Hisvp EN/IwSE0R9WKOVPrF7GPpSt6fdR46RjBSLyj03no8LunpIeFf3J5mlBVMQ7AIKhS6bxp PbqA== X-Forwarded-Encrypted: i=1; AJvYcCXUvRK2UXxJ6LoLGxcTvoHWt6TOgafAS4GcwcD0YIOIx5yqa6JyVXRNyJm2sfiJSLocx6Rj+rvIWfD6eFloxBnm@lists.infradead.org X-Gm-Message-State: AOJu0YynJ0bupD5QqhVxny4QmI7KckeYC0/q41EWz/2ZpqUv7Dq9nt3J UTBs3/EKP6A5kGq0vuEEnzTw3xH0HeYKp7X8q3k0d/n93U9JFISS8UEVcH5W9A== 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> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240822_022036_783477_647DF9C5 X-CRM114-Status: GOOD ( 27.66 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The to-be-introduced I2C component prober needs to enable regulator supplies (and toggle GPIO pins) for the various components it intends to probe. To support this, a new "pure DT lookup" method for getting regulator supplies is needed, since the device normally requesting the supply won't get created until after the component is probed to be available. 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 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(struct device *dev, /* first do a dt based lookup */ if (dev && dev->of_node) { - r = of_regulator_dev_lookup(dev, supply); + r = of_regulator_dev_lookup(dev, dev->of_node, supply); if (!IS_ERR(r)) return r; if (PTR_ERR(r) == -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 device *dev) #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); #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_regulator.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(struct device_node *parent, /** * of_get_regulator - get a regulator device node based on supply name - * @dev: Device pointer for the consumer (of regulator) device + * @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 device_node *node, + const char *supply) { struct device_node *regnode = NULL; char prop_name[64]; /* 64 is max size of property name */ - dev_dbg(dev, "Looking up %s-supply from device tree\n", supply); + dev_dbg(dev, "Looking up %s-supply from device node %pOF\n", supply, node); snprintf(prop_name, 64, "%s-supply", supply); - regnode = of_parse_phandle(dev->of_node, prop_name, 0); + regnode = of_parse_phandle(node, prop_name, 0); if (!regnode) { regnode = of_get_child_regulator(dev->of_node, prop_name); @@ -626,7 +628,8 @@ static struct regulator_dev *of_find_regulator_by_node(struct device_node *np) } /** of_regulator_dev_lookup - lookup a regulator device with device tree only - * @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 name @@ -636,13 +639,13 @@ static struct regulator_dev *of_find_regulator_by_node(struct device_node *np) * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed * in the future. */ -struct regulator_dev *of_regulator_dev_lookup(struct device *dev, +struct regulator_dev *of_regulator_dev_lookup(struct device *dev, struct device_node *np, const char *supply) { struct regulator_dev *r; struct device_node *node; - node = of_get_regulator(dev, supply); + node = of_get_regulator(dev, np, supply); if (node) { r = of_find_regulator_by_node(node); of_node_put(node); @@ -659,6 +662,41 @@ struct regulator_dev *of_regulator_dev_lookup(struct device *dev, return ERR_PTR(-ENODEV); } +static struct regulator *_of_regulator_get(struct device *dev, struct device_node *node, + const char *id, enum regulator_get_type get_type) +{ + struct regulator_dev *r; + int ret; + + ret = _regulator_get_common_check(dev, id, get_type); + if (ret) + return ERR_PTR(ret); + + r = 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 lookup + * @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, struct device_node *np, } else { memcpy(name, prop->name, i); name[i] = '\0'; - tmp = regulator_get(dev, name); + tmp = of_regulator_get_optional(dev, np, name); if (IS_ERR(tmp)) { ret = PTR_ERR(tmp); goto error; From patchwork Thu Aug 22 09:19:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen-Yu Tsai X-Patchwork-Id: 13773095 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2708BC5320E for ; Thu, 22 Aug 2024 09:25:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=OGdOo23OjShZKKUPhP64Sm0t2nst66j12GqHlKAMLTI=; b=seilOn1Zq8bjXEUbtVoQAy9TLL l9aK0jjsvU7wFxCwNaJUdc4hVbin5n/XPezes3sNGSQaKr30mCiwjqCKKD8+7tZKuhfJra63lHNql 8MWq912lPjoau1wjq/R35MyIhXAKjHnClFeFoF8jZtmPZ7jj7testhQfVupPg97K9XNZ1bRRz9IF/ kERCgkx66+98urWQsw1IKUw2wglRrTr8UaBrQjNVq1hIzqzNXcHciuJPyJshny47gwbVDwpF62WO6 ELfVVtNRgjd/KUqf1yxqeXNs5jhtbP2ZVyaxYucdV+ieOvyhY7Pj7gAM1r7oBKERmsNyvWlkUX/tZ wCzikPYQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh43s-0000000CDXb-2t9j; Thu, 22 Aug 2024 09:24:56 +0000 Received: from mail-oo1-xc2b.google.com ([2607:f8b0:4864:20::c2b]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh3zk-0000000CCE9-1Jya for linux-arm-kernel@lists.infradead.org; Thu, 22 Aug 2024 09:20:41 +0000 Received: by mail-oo1-xc2b.google.com with SMTP id 006d021491bc7-5d5bb03fe42so350634eaf.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=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=OGdOo23OjShZKKUPhP64Sm0t2nst66j12GqHlKAMLTI=; b=EqlqYN0yIjMMgOGbJ4IklhMZLKLU3b2nUicuMrcYFOr3VsR57qHeiTbOS9TK/JSzp2 fZ0ZT1m6G0+T3ayeZkV3YYtyt3jJJx2FZYFGMFW3Twvv73nORVgO7IefVhvbpVwXhmVb xBtugEmyq5D1XEFA5+sL0NKUWBikWvBcUJKTA= 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=R9etCYszjGqoulAg/Q8Bgy7PQf3VPqHo23VJpc/Vt4I6ZsdfWLKdLANjg8kemNpboS 6fHHqdjUc7P0IHvbJ6lP6hcNwzacd6ODDZwM2l786oYgQQUPTyPWeiY01DqTCOUYionY X58nXlFdvW3KBnNgbolScC4qXUnyoV8yl/znSqSfeiRFwy2e8WqvgoW0u0+lnHc3oblQ UhWpSvB3L7C6BkO/B1tnGGQzDef9bSRaEzpfeSPXk7QRCv75+bqyzgszPiM6pAloEAFv qm2rJf8gIhWv2+Yh1bHC/fnC0ht6QRhERrA59fQfSUP7Jlv5Ymzst+SJJ507a6bsanQt FOVg== X-Forwarded-Encrypted: i=1; AJvYcCVQwOMOQhTXKVaE1uA15yc7LbwpZQ6K7cB062FTD35zdObk3KLMVKNN1AMaO7RDFLtwTwqVkhh6Q+E/WPn6uTdA@lists.infradead.org X-Gm-Message-State: AOJu0YwUHLuhuBsZyKGqjZEurnlpJ05IdDkQAq9EIPmt27KOfmfwGek5 NAfTO6aqMZGubWiSOv2rfMWNFCO395fdqZU7ti66gzCffDq9xnHwjqMuzdsmQw== 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> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240822_022040_381321_9586E14D X-CRM114-Status: GOOD ( 23.28 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The 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 fwnode_handle *fwnode, } EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index); +/** + * gpio_property_name_length - Returns the GPIO name length from a property name + * @str: string to check + * + * This function checks if the given name matches the GPIO property patterns, 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 = 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 = gpio_suffixes; *p; p++) + if (!strcmp(str, *p)) + return 0; + + for (size_t i = len - 4; i > 0; i--) { + /* find right-most '-' and check if remainder matches suffix */ + if (str[i] != '-') + continue; + + for (const char *const *p = 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 / function * 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 { #ifdef CONFIG_GPIOLIB +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); From patchwork Thu Aug 22 09:19:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen-Yu Tsai X-Patchwork-Id: 13773119 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 997A9C5320E for ; Thu, 22 Aug 2024 09:25:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=cxRueDbPxY1fgYOETHJnt8bZkNktfZ7UPvnhXd9unUQ=; b=ORKVNshr7Q1okwLwi9+jQTxRGZ U/ouq2Nt1Yp5+HUgEn7eswaU8oF+HsyKxR/9h8C7qpQBcvHu58FCg2d23U+pvIEvxQBnNq/GOu62k vOiaZMqeXZ70Yf4miMONlZPF32o5fqOV8nq90bQfC1vZrgkPvYv5ZqL+NKpeu5d+9LETNTev6XNlD OaqhofZC/gI3MjQxAmXY2O6V2Py7Pj8h11cpySocI7jWie0FUQveSqrRDQ4vo78SJ26wGjGVwxgrb upiCnXjzAniz0mOHtcZP1tltDSypxAiVuWNJ51yPGUZyG86LymUdXMTHJgNK63cjMhkv43Tx7lZba phmMbG3Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh44e-0000000CDpT-0eZS; Thu, 22 Aug 2024 09:25:44 +0000 Received: from mail-pf1-x42a.google.com ([2607:f8b0:4864:20::42a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh3zn-0000000CCFk-2tkk for linux-arm-kernel@lists.infradead.org; Thu, 22 Aug 2024 09:20:45 +0000 Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-714114be925so498534b3a.2 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=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=cxRueDbPxY1fgYOETHJnt8bZkNktfZ7UPvnhXd9unUQ=; b=M1KglKmM0+2g764kI6I5eB7EgR1wo57i+zwaNMXxkiOxaXNM/EzivNyr/Ns3yhMqot dWdVwnF6xEKqeNxTJY4Y4oZC6C3Whak4cA2q1FtsfWdyDIj3313Ri5bLVFPydVAuqJ65 W5ag+Tr9vLBbq+eJtRJVWYDbo7Bi+qJK/8IXA= 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=A+Ax/U3uEOFfL+3KNH45t4p8VMdjq3Pt86HCOdIgTOIJ5xLhsMgiMbU9gfAwzgRnHx zzwXoKHxxVySJhIrUOJ6FVZtrx5kNz6q/5fUqJNk/XC4LTF8dJrF0dn5rr6Xg7XnaSO/ QA7NMcseKIzVFNKOTgO8tduQkr++QpFj/aOGauRRorVwAFVVl+rdmoduFoK4U6Knntf6 qBmdV6o3jIEP+N9YONiE7KnrtRbCx1je7hVroRgZthjgWOWGackhXKeWhLVuGs11ibcV avBh3eiHvJNounmjO+E2zx8mGDS8qilC7rW/GjmCYvvH8jiRMza1+ilbRksj9kHXBgcW caMQ== X-Forwarded-Encrypted: i=1; AJvYcCUL+jxy12o3d9kTRk2+uBXiQ8t/onkQlOHtF8fabXPM/3n3z16J7UVcgGCWoXwmqU6MSFozgDb53S3Mu5DK2m/b@lists.infradead.org X-Gm-Message-State: AOJu0YxDugPY4pN8ZscUpGmLEKFpbnPgjT6DN89KhbWfvVyKvo7BokVv 90MORaD+hKQPQHB/ZnY1KZNPduhq0hVFxlteF4/hN+TmM0hQtFWGXpcXNzd4vA== 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> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240822_022043_769881_7D377C94 X-CRM114-Status: GOOD ( 41.74 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Some devices are designed and manufactured with some components having multiple drop-in replacement options. These components are often connected to the mainboard via ribbon cables, having the same signals and pin assignments across all options. These may include the display panel and touchscreen on laptops and tablets, and the trackpad on laptops. Sometimes which component option is used in a particular device can be detected by some firmware provided identifier, other times that information is not available, and the kernel has to try to probe each device. This change attempts to make the "probe each device" case cleaner. The current approach is to have all options added and enabled in the device tree. The kernel would then bind each device and run each driver's probe function. This works, but has been broken before due to the introduction of asynchronous probing, causing multiple instances requesting "shared" resources, such as pinmuxes, GPIO pins, interrupt lines, at the same time, with only one instance succeeding. Work arounds for these include moving the pinmux to the parent I2C controller, using GPIO hogs or pinmux settings to keep the GPIO pins in some fixed configuration, and requesting the interrupt line very late. Such configurations can be seen on the MT8183 Krane Chromebook tablets, and the Qualcomm sc8280xp-based Lenovo Thinkpad 13S. Instead of this delicate dance between drivers and device tree quirks, this change introduces a simple I2C component probe. function For a given class of devices on the same I2C bus, it will go through all of them, doing a simple I2C read transfer and see which one of them responds. It will then enable the device that responds. This requires some minor modifications in the existing device tree. The status for all the device nodes for the component options must be set to "failed-needs-probe". This makes it clear that some mechanism is needed to enable one of them, and also prevents the prober and device drivers running at the same time. Signed-off-by: Chen-Yu Tsai --- Changes since 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 = 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 := i2c-core-base.o i2c-core-smbus.o i2c-core-$(CONFIG_ACPI) += i2c-core-acpi.o i2c-core-$(CONFIG_I2C_SLAVE) += i2c-core-slave.o i2c-core-$(CONFIG_OF) += i2c-core-of.o +i2c-core-$(CONFIG_OF_DYNAMIC) += i2c-core-of-prober.o obj-$(CONFIG_I2C_SMBUS) += i2c-smbus.o obj-$(CONFIG_I2C_CHARDEV) += i2c-dev.o diff --git a/drivers/i2c/i2c-core-of-prober.c b/drivers/i2c/i2c-core-of-prober.c new file mode 100644 index 000000000000..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 each + * driver separately try and probe its device while fighting over shared + * resources, they can be marked as "fail-needs-probe" and have a prober + * figure out which one is actually used beforehand. + * + * This prober assumes such drop-in parts are on the same I2C bus, have + * non-conflicting addresses, and can be directly probed by seeing which + * address responds. + * + * TODO: + * - Support handling common regulators and GPIOs. + * - Support I2C muxes + */ + +static struct device_node *i2c_of_probe_get_i2c_node(struct device *dev, const char *type) +{ + struct device_node *node __free(device_node) = of_find_node_by_name(NULL, 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) = 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", type); + + 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) = kzalloc(sizeof(*ocs), GFP_KERNEL); + if (!ocs) + return -ENOMEM; + + of_changeset_init(ocs); + ret = of_changeset_update_prop_string(ocs, node, "status", "okay"); + if (ret) + return ret; + + ret = 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 = 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 function + * can return -EPROBE_DEFER if the I2C adapter is unavailable. + * Return: 0 on success or no-op, error code otherwise. + * A no-op can happen when it seems like the device tree already + * has components of the type to be probed already enabled. This + * can happen when the device tree had not been updated to mark + * the status of the to-be-probed components as "fail". Or this + * function was already run with the same parameters and succeeded + * in enabling a component. The latter could happen if the user + * had multiple types of components to probe, and one of them down + * the list caused a deferred probe. This is expected behavior. + */ +int i2c_of_probe_component(struct device *dev, const char *type) +{ + struct i2c_adapter *i2c; + int ret; + + struct device_node *i2c_node __free(device_node) = 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 = of_get_i2c_adapter_by_node(i2c_node); + if (!i2c) + return dev_err_probe(dev, -EPROBE_DEFER, "Couldn't get I2C adapter\n"); + + ret = 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, &data) < 0) + continue; + + /* Found a device that is responding */ + ret = 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); +#if IS_ENABLED(CONFIG_OF_DYNAMIC) +int i2c_of_probe_component(struct device *dev, const char *type); +#endif + #else static inline struct i2c_client *of_find_i2c_device_by_node(struct device_node *node) From patchwork Thu Aug 22 09:20:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen-Yu Tsai X-Patchwork-Id: 13773120 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7280DC5320E for ; Thu, 22 Aug 2024 09:26:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=nK7RVXbUa73K0O9XTM3cCy4ZRtIeA4akoAuNuVfnhl4=; b=lRFgjnYLZYm6PSng1udyE5qLYV D1AC1dT8P+dGNX7T4c/A/CvFaMhionUseAArasoDGc4S1rC0ouxaECqvp6ah3x+Z5+XRZ0aIs/ct7 oPs7Oh/DBiYgZ4Cmo+13Zyml1kuGByPZz9khj+1kv79+PtO0Emr7fCDRYMl6HXzHr7ZPzmLOJQ5Us 5iiDsM5/hoqUtnLcihRa6GYNqEZc6oN1gkn35t40qIE/+X65VIBjv8f40vqDwOezIlGuVfn07PL7/ ya5te+OIPp7b+Xj+EYCMm0cx6TogmqvKYjunpFnsk+rsIzpAM1GFp8tsdCU+R+qVqWqb5SkSmMJGY L4snL4zg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh45L-0000000CDur-1DBG; Thu, 22 Aug 2024 09:26:27 +0000 Received: from mail-pf1-x433.google.com ([2607:f8b0:4864:20::433]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh3zq-0000000CCHB-44KE for linux-arm-kernel@lists.infradead.org; Thu, 22 Aug 2024 09:20:48 +0000 Received: by mail-pf1-x433.google.com with SMTP id d2e1a72fcca58-7142448aaf9so403472b3a.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=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=nK7RVXbUa73K0O9XTM3cCy4ZRtIeA4akoAuNuVfnhl4=; b=bmyoAbdxWATVF/+GBe3wwoj8/0zKxrEXDQI8AhHYXJU/FXeVZc/ttEaxu1Fpw45Ebq UpZr3PuxGaz2rwU8vSEG82AhFZLGeE96ewp43idqcHzupBGjEZWdNQfEHE3KiOrX2gdS XrLTq/SEfOes3MdkAe38QWV6/ZEXkousHjfCc= 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=sskOIdiUuPFdKBr6RFzIrb4hF2nyxdpHEpU6LITko1f6HtqHBERo2UQNA3Ei3O/Ta3 ywaCrPHAU6NqH7CaXlmZqLIYC0TqvBHCFaNxHZ5iy5r7/Ax7rPhruyz1aiL+6N/tS0YQ a9duY9+4MBbtFdaTu0n/3DOA53QdrIi3coa+FVcLKimc9unLYqkCLsXxaNboR0uPB4eV N4xKdHCH3l70rQJUCs1irjwlGRjZg13UCCWywD2Wmvf5g1wXjFI+WpB4GzucOns0THYo w7MNAWBfFEHZxt0Ln5MJUqkX5k8iLOeDfLZf9swt2VPQRJylYnwsXoDBtrrAegJ7v5RE i7HQ== X-Forwarded-Encrypted: i=1; AJvYcCWdwx/JOTv5VphQxlNqu5iOaJ5/UBYUn+9ckmf8kukuzG3SJAWqJrB7fZGxBRbY/HWp5uU4+mpI89W751FbU8WM@lists.infradead.org X-Gm-Message-State: AOJu0Yx5A6m6PJp7BfW0p3oh1bBz+7n563cb95m2LRyEU6ov7q9MsOpx ZjvfkcgDjRgFiCpXyGYuQvB+gqb7rfNrmy28hBSoRBJ28r4qZHXqSVclDZreuQ== 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> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240822_022047_039334_7F643DA0 X-CRM114-Status: GOOD ( 25.35 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org This adds 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-prober.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 @@ */ #include +#include #include #include #include #include #include #include +#include #include /* @@ -27,10 +29,93 @@ * address responds. * * TODO: - * - Support handling common regulators and GPIOs. + * - Support handling common GPIOs. * - Support I2C muxes */ +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_node *node, + struct i2c_of_probe_data *data) +{ + struct regulator_bulk_data *tmp, *new_regulators; + int ret; + + ret = of_regulator_bulk_get_all(dev, node, &tmp); + if (ret <= 0) + return ret; + + if (!data->regulators) { + data->regulators = tmp; + data->regulators_num = ret; + return ret; + }; + + new_regulators = krealloc(data->regulators, + sizeof(*tmp) * (data->regulators_num + ret), + GFP_KERNEL); + if (!new_regulators) { + regulator_bulk_free(ret, tmp); + return -ENOMEM; + } + + data->regulators = new_regulators; + + for (unsigned int i = 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 *node, + struct i2c_of_probe_data *data) +{ + struct property *prop; + int ret; + + ret = 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 = 0; + + dev_dbg(dev, "Enabling regulator supplies\n"); + + ret = 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, const char *type) { struct device_node *node __free(device_node) = of_find_node_by_name(NULL, 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 = {0}; int ret; struct device_node *i2c_node __free(device_node) = i2c_of_probe_get_i2c_node(dev, type); @@ -133,6 +219,30 @@ int i2c_of_probe_component(struct device *dev, const char *type) if (!i2c) return dev_err_probe(dev, -EPROBE_DEFER, "Couldn't get I2C adapter\n"); + /* Grab resources */ + for_each_child_of_node_scoped(i2c_node, node) { + u32 addr; + + if (!of_node_name_prefix(node, type)) + continue; + if (of_property_read_u32(node, "reg", &addr)) + continue; + + dev_dbg(dev, "Requesting resources for %pOF\n", node); + ret = i2c_of_probe_get_res(dev, node, &probe_data); + if (ret) + return ret; + } + + dev_dbg(dev, "Resources: # of regulator supplies = %d\n", probe_data.regulators_num); + + /* Enable resources */ + ret = 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 = 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 char *type) break; } + i2c_of_probe_disable_regulators(&probe_data); + i2c_of_probe_free_res(&probe_data); i2c_put_adapter(i2c); return ret; From patchwork Thu Aug 22 09:20:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen-Yu Tsai X-Patchwork-Id: 13773121 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 75038C3DA4A for ; Thu, 22 Aug 2024 09:27:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=oIuVa6xe5T9fPXAC3Ljp+tme57B/5X5NOOiRmoSNKDs=; b=q/74zJUH7T/vYVQ4SeV8oSNtkZ fwutq9V2ZOxGEstn7eZzzaQ+4V3FxDnZ3fzaaU05o45R0jCEF+It8em9HxpqHMXl3FRMA8O/6k9pf PzHtF2GZt0FZ5+s01nmm7nDp0jpYPXglzY1YGbzSdIW2mcGZAUKrF/zTqWn/jtofBGgaXwF6aGWg+ /7/W6IN1AMGY2dkoM2hcAicockM1Hkhi3APK0tul0QKaq34/EODCBcmR5XAhYCVOqdcsswjK4OlWv +FLki5dt66dLQIH36By1Q+lPEU4dfE15JTDhomho2wPv+j0SDC3MkdWkFtIMojMnDtgXyNDoFy1Ba VWdSH+oQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh464-0000000CE9B-0hp2; Thu, 22 Aug 2024 09:27:12 +0000 Received: from mail-oo1-xc36.google.com ([2607:f8b0:4864:20::c36]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh3zv-0000000CCIr-0EiN for linux-arm-kernel@lists.infradead.org; Thu, 22 Aug 2024 09:20:53 +0000 Received: by mail-oo1-xc36.google.com with SMTP id 006d021491bc7-5da686531d3so374189eaf.3 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=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=oIuVa6xe5T9fPXAC3Ljp+tme57B/5X5NOOiRmoSNKDs=; b=SJdQ04XXrU6+gzhAFzEShvXIIhnJRuxNnZo92PCrE6FMbqJgqYTd0+hfAEwc8+l7sy qmacaeQz92rXHUGbTdggMDs3HzDZeshs3vynkFjTAS4/KB7sFdIpEPv3rPVzK20bIMPN Q6vv8h5NtUtzM1yg6YVaq9iA7v6szGLtQl7v4= 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=CEKAUx31d8jvASV1kisGfEnQSfaeGCz9sOdLhsNKPUZg61Qb5Q2Y5hMQrdw/QXF+BQ psc9qqGpj51QYRrm0uaur6ymtNJ+Ey23DfFOXKEew0tvxYFaN2JoqXhYd2gnTc3PqjRo b7SUX4n5FwwlaKEcg/sKFt7qnw+dgvgalwBMJ4//3JbuvxcNopssOH9ua62gPr1fNGVd iXvCx1vatve9OHvdjZ0c+7M3yPW7srtPaTYahNcDXS9ODQnGJFmrT6U1zq1UGp5dy+ov Me6P2AkbZuhyhGFa9Bl2SgznyW9EglKuevN/H9kxQsDI1Xakk7+kgpxs31lX3bEXljdS ZySg== X-Forwarded-Encrypted: i=1; AJvYcCVxYtMLxvU1hP99UNpkwmcdfThgITYrGVwGPi5GUg//81dgEqJGexvV7ww/P8fiv6ZATNkLQ1AIazdNbWOO0dlm@lists.infradead.org X-Gm-Message-State: AOJu0YxUyIlPVodUUId9yXLdF5oZfF7UB8vYMzulBW6uv3PkgIq/M6EY 7jnn8w0btLVu4uduXoSRnNjWlW92VH4+Xbwrxr0ZGL/N9GsYHrTYs4YUUfXfuA== 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> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240822_022051_108075_1623301D X-CRM114-Status: GOOD ( 31.94 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org This adds GPIO 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-prober.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 */ 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; } +/* + * 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 property *prop, + struct i2c_of_probe_data *data) +{ + struct fwnode_handle *fwnode = 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 = NULL; + size_t new_size; + int len; + + len = gpio_property_name_length(prop->name); + if (len < 0) + return 0; + + if (len >= 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 = con; + } + + /* + * GPIO descriptors are not reference counted. GPIOD_FLAGS_BIT_NONEXCLUSIVE + * can't differentiate between GPIOs shared between devices to be probed and + * other devices (which is incorrect). If the initial request fails with + * -EBUSY, retry with GPIOD_FLAGS_BIT_NONEXCLUSIVE and see if it matches + * any existing ones. + */ + gpiod = fwnode_gpiod_get_index(fwnode, con_id, 0, GPIOD_ASIS, "i2c-of-prober"); + if (IS_ERR(gpiod)) { + int i; + + if (PTR_ERR(gpiod) != -EBUSY || !data->gpiods) + return PTR_ERR(gpiod); + + gpiod = fwnode_gpiod_get_index(fwnode, con_id, 0, + GPIOD_ASIS | GPIOD_FLAGS_BIT_NONEXCLUSIVE, + "i2c-of-prober"); + for (i = 0; i < data->gpiods->ndescs; i++) + if (gpiod == data->gpiods->desc[i]) + return 1; + + return -EBUSY; + } + + new_size = struct_size(gpiods, desc, data->gpiods ? data->gpiods->ndescs + 1 : 1); + gpiods = krealloc(data->gpiods, new_size, GFP_KERNEL); + if (!gpiods) { + gpiod_put(gpiod); + return -ENOMEM; + } + + data->gpiods = gpiods; + data->gpiods->desc[data->gpiods->ndescs++] = 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 = 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); } @@ -88,6 +169,18 @@ static int i2c_of_probe_get_res(struct device *dev, struct device_node *node, goto err_cleanup; } + for_each_property_of_node(node, prop) { + dev_dbg(dev, "Trying property %pOF/%s\n", node, prop->name); + + /* GPIOs */ + ret = i2c_of_probe_get_gpiod(node, prop, data); + if (ret < 0) { + dev_err_probe(dev, ret, "Failed to get GPIO from %pOF/%s\n", + node, prop->name); + goto err_cleanup; + } + } + return 0; err_cleanup: @@ -98,6 +191,7 @@ static int i2c_of_probe_get_res(struct device *dev, struct device_node *node, static int i2c_of_probe_enable_res(struct device *dev, struct i2c_of_probe_data *data) { int ret = 0; + int gpio_i; dev_dbg(dev, "Enabling regulator supplies\n"); @@ -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); + if (!data->gpiods) + return 0; + + for (gpio_i = 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 = 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 >= 0; gpio_i--) + gpiod_set_raw_value_cansleep(data->gpiods->desc[gpio_i], 0); + regulator_bulk_disable(data->regulators_num, data->regulators); + + return ret; } 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 char *type) return ret; } - dev_dbg(dev, "Resources: # of regulator supplies = %d\n", probe_data.regulators_num); + dev_dbg(dev, "Resources: # of GPIOs = %d, # of regulator supplies = %d\n", + probe_data.gpiods ? probe_data.gpiods->ndescs : 0, + probe_data.regulators_num); /* Enable resources */ ret = i2c_of_probe_enable_res(dev, &probe_data); @@ -256,6 +377,7 @@ int i2c_of_probe_component(struct device *dev, const char *type) continue; /* Found a device that is responding */ + i2c_of_probe_free_gpios(&probe_data); ret = i2c_of_probe_enable_node(dev, node); break; } From patchwork Thu Aug 22 09:20:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen-Yu Tsai X-Patchwork-Id: 13773123 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E8A82C3DA4A for ; Thu, 22 Aug 2024 09:29:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=eQmK8T4bq0VH3g9O2efQWUIbRipH0PsfI9tyjRvqgic=; b=fnAHxN/A1GXtzN+aU3JlOQWJrP c+jbwH81MyQJgczeL+er3JMj4WYnakL/1oBKbH4rvIvJ146tDRAwqSfWcGjILRyih1Azi9K9rXW30 4tbFfL5J5270JzvH0I7LSzgiSbNcA4ByVqMboI33763aOfH/UbVh92gF8QjiIVDWCpYusNn3DWdWh zNGd86OQoFsobhmMpdGpUAUuDL74xnVvgdrsBoXXrTv0+akjdEyqale1R3VbkFo9mGP3+CDYKXN2L iGGWkVP4TinDis7ULBW3ahlvIe/zdEHkcZWLp9VkaQ1NdGDEmPDFn6YyAI/sCE046QAqzxbWr3GI8 H7BDXdQQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh48S-0000000CElq-1XvR; Thu, 22 Aug 2024 09:29:40 +0000 Received: from mail-pf1-x433.google.com ([2607:f8b0:4864:20::433]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh3zy-0000000CCKH-0H4w for linux-arm-kernel@lists.infradead.org; Thu, 22 Aug 2024 09:20:55 +0000 Received: by mail-pf1-x433.google.com with SMTP id d2e1a72fcca58-714263cb074so514949b3a.1 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=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=eQmK8T4bq0VH3g9O2efQWUIbRipH0PsfI9tyjRvqgic=; b=fWmNjnALocddIczpR73VoUFwk7K4BrqC5ADO61J75NKoJlw+xyKDUSkK9WD+Sl1920 eWDnmA9TCnXsYLtyk+taeOtEcF3Xeaw7Yy7jaw28VVlHWIJFksw/pW29i1sIMc1aHzsQ b6G2zuQ/BTezWiQZ/rbpculp1jW+t4FrHauQw= 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=PhMLCN6BW+0EQDPjjOCfSM4QwFXkjzleU2UAVv18Uba72OYHTL36uyNvnYiVy1QXiX lGpe6QNWTL1DbnIoYpgtIOh+WXo55BjxuxuZcZAUUMazqv9ztMflm1PPktZbrrsRRNs+ Sk3Lf0RPO2tIg1b9BWm327vGKDCYigvQI2ki7geBwcb4uMh0TKXVw/qTFjvycvvR0hJM p+UO35mfbFYUTMtBRzEwls7zGf0BvIBhjBrzriBqnupO+Ow60QuLLcZ1cQk92OJgA0Wd IcRw2Q8uvp4ogEpov8kbDd13Hw9FTChTdck7R9a07UZfzE71LCERWU38SDGTlqBReHs1 isLw== X-Forwarded-Encrypted: i=1; AJvYcCUzrHS1ylD+xkhmXnLNuqZ9wmuUw40zyXWI91fKobybxeJFZ/BcqAP05uMd73Yvxa5nK0D8CP7/o2QZ5PqPF+T9@lists.infradead.org X-Gm-Message-State: AOJu0YwjLOKSCWhhu4+1w9hLMNDw/7ce/rr5Xl3QEgfZwcJR/Vwvpsob FweRjLBlU7qHOiroaDw6OsK96LjcVz83KYvo43G0YLIVwuvZVKlJslZ0xF1zkw== 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> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240822_022054_139574_5788F2F1 X-CRM114-Status: GOOD ( 40.47 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Some devices are designed and manufactured with some components having multiple drop-in replacement options. These components are often connected to the mainboard via ribbon cables, having the same signals and pin assignments across all options. These may include the display panel and touchscreen on laptops and tablets, and the trackpad on laptops. Sometimes which component option is used in a particular device can be detected by some firmware provided identifier, other times that information is not available, and the kernel has to try to probe each device. This change attempts to make the "probe each device" case cleaner. The current approach is to have all options added and enabled in the device tree. The kernel would then bind each device and run each driver's probe function. This works, but has been broken before due to the introduction of asynchronous probing, causing multiple instances requesting "shared" resources, such as pinmuxes, GPIO pins, interrupt lines, at the same time, with only one instance succeeding. Work arounds for these include moving the pinmux to the parent I2C controller, using GPIO hogs or pinmux settings to keep the GPIO pins in some fixed configuration, and requesting the interrupt line very late. Such configurations can be seen on the MT8183 Krane Chromebook tablets, and the Qualcomm sc8280xp-based Lenovo Thinkpad 13S. Instead of this delicate dance between drivers and device tree quirks, this change introduces a simple I2C component prober. For any given class of devices on the same I2C bus, it will go through all of them, doing a simple I2C read transfer and see which one of them responds. It will then enable the device that responds. This requires some minor modifications in the existing device tree. The status for all the device nodes for the component options must be set to "failed-needs-probe". This makes it clear that some mechanism is needed to enable one of them, and also prevents the prober and device drivers running at the same time. Signed-off-by: Chen-Yu Tsai --- Changes since 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 = 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/Kconfig 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. +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/Makefile index 2dcc6ccc2302..21a9d5047053 100644 --- a/drivers/platform/chrome/Makefile +++ b/drivers/platform/chrome/Makefile @@ -8,6 +8,7 @@ obj-$(CONFIG_CHROMEOS_ACPI) += chromeos_acpi.o obj-$(CONFIG_CHROMEOS_LAPTOP) += chromeos_laptop.o obj-$(CONFIG_CHROMEOS_PRIVACY_SCREEN) += chromeos_privacy_screen.o obj-$(CONFIG_CHROMEOS_PSTORE) += chromeos_pstore.o +obj-$(CONFIG_CHROMEOS_OF_HW_PROBER) += chromeos_of_hw_prober.o obj-$(CONFIG_CHROMEOS_TBMC) += chromeos_tbmc.o obj-$(CONFIG_CROS_EC) += cros_ec.o obj-$(CONFIG_CROS_EC_I2C) += cros_ec_i2c.o diff --git a/drivers/platform/chrome/chromeos_of_hw_prober.c b/drivers/platform/chrome/chromeos_of_hw_prober.c new file mode 100644 index 000000000000..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 *data) +{ + const char *type = data; + + return i2c_of_probe_component(dev, type); +} + +static const struct hw_prober_entry hw_prober_platforms[] = { + { .compatible = "google,hana", .prober = chromeos_i2c_component_prober, .data = "touchscreen" }, + { .compatible = "google,hana", .prober = chromeos_i2c_component_prober, .data = "trackpad" }, +}; + +static int chromeos_of_hw_prober_probe(struct platform_device *pdev) +{ + for (size_t i = 0; i < ARRAY_SIZE(hw_prober_platforms); i++) { + int ret; + + if (!of_machine_is_compatible(hw_prober_platforms[i].compatible)) + continue; + + ret = hw_prober_platforms[i].prober(&pdev->dev, hw_prober_platforms[i].data); + /* Ignore unrecoverable errors and keep going through other probers */ + if (ret == -EPROBE_DEFER) + return ret; + } + + return 0; +} + +static struct platform_driver chromeos_of_hw_prober_driver = { + .probe = chromeos_of_hw_prober_probe, + .driver = { + .name = DRV_NAME, + }, +}; + +static struct platform_device *chromeos_of_hw_prober_pdev; + +static int chromeos_of_hw_prober_driver_init(void) +{ + size_t i; + int ret; + + for (i = 0; i < ARRAY_SIZE(hw_prober_platforms); i++) + if (of_machine_is_compatible(hw_prober_platforms[i].compatible)) + break; + if (i == ARRAY_SIZE(hw_prober_platforms)) + return -ENODEV; + + ret = platform_driver_register(&chromeos_of_hw_prober_driver); + if (ret) + return ret; + + chromeos_of_hw_prober_pdev = + platform_device_register_simple(DRV_NAME, PLATFORM_DEVID_NONE, NULL, 0); + if (IS_ERR(chromeos_of_hw_prober_pdev)) + goto err; + + return 0; + +err: + platform_driver_unregister(&chromeos_of_hw_prober_driver); + + return PTR_ERR(chromeos_of_hw_prober_pdev); +} +module_init(chromeos_of_hw_prober_driver_init); + +static void chromeos_of_hw_prober_driver_exit(void) +{ + platform_device_unregister(chromeos_of_hw_prober_pdev); + platform_driver_unregister(&chromeos_of_hw_prober_driver); +} +module_exit(chromeos_of_hw_prober_driver_exit); + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("ChromeOS device tree hardware prober"); From patchwork Thu Aug 22 09:20:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen-Yu Tsai X-Patchwork-Id: 13773122 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 11661C3DA4A for ; Thu, 22 Aug 2024 09:29:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=b0ZXsCUO90vlD45ZJO1I+QT/1oidv8iaRsna2l2Dubk=; b=IWY9ert2NB7EhBvtnhyh8qznTj gsIr1xOe79RZZgg5RU+JiL0qoZ7gMr4+BOHOg57Cdp71pJy5ezBtBS0si1XRuIRf0N8Wi2jZFuxWZ 67tGvlmNZh5cx1kQSrINraVAFNaSY47yKJIpmzegnl4U0Y1OX8x1vY4OOLARHOrMy9cEonMIpMi21 56qdQ3gU4oEaYvhJQMuGGkc4q4rKZXRr5E2heemqeepMxmLtQeagPlxzbQPX6jekp6dCJwH/Fpmc9 WPYvK39PpDcfm6R/XYgFwcpm2FHlMu27P1xW2qWHQXixOS6QtifEFmXMYPEMeR0lUXp0Dvi/LvyaH 3MZONAoA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh47k-0000000CEab-0cUV; Thu, 22 Aug 2024 09:28:56 +0000 Received: from mail-pf1-x42a.google.com ([2607:f8b0:4864:20::42a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh401-0000000CCM3-3Bpz for linux-arm-kernel@lists.infradead.org; Thu, 22 Aug 2024 09:20:59 +0000 Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-712603f7ba5so516681b3a.3 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=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=b0ZXsCUO90vlD45ZJO1I+QT/1oidv8iaRsna2l2Dubk=; b=RdIvhLFqJPF1SPTkNNAyUJzNVHa29xpvQSbDG/4PaagEnfgRHrJmRAthiCiofr1SlK +MyL8siS7/MEgrkYwxAQTcMEzqWtrNMVLtj0qZKSTRsTjsoH1X6u3TtCxBeGjtUyXjfK 5ith9xfFSHm6smsccHeafrIyWVA1zDAvOMBFA= 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=KgKRu7hlEDIcALHqByRJWklp9555EQb68MRznPKVvElEzIn8EhHQMjzAXuea9lbIOG 0tPhPauiIdQWT6aHkhW1zO7E/dhNq9sedw5FjLXrSC3+GPYQn5d4mKsHG3NxK6dru8zn Eiip7R0RBbiYThzcr1uTevUCsWkrlSbCHOuL/mJaVtuSzEzcvOFMqNZCUQrgOBXKhDy0 NQWUf0SyzjrxBgrT6K+etzw5RwhLW2bxzUihG8ihjadqqifW1OWBZrttnGs1xIG1U6f7 Une5RQvf1zXDzamOfT369D+JzGpTO68b9iddPmJIipKrMZLO/VGlrouNO5sn/vJyz/6K lP6w== X-Forwarded-Encrypted: i=1; AJvYcCWmpHPo2QAZ0ir1UKcsmbqspGYxtirX6BO+/1KfvRiRbhWGMi/N37LwvLaIt7B3tlJbxvVwdD7/YLkn6PTf/NXQ@lists.infradead.org X-Gm-Message-State: AOJu0YxN0JToBR4nE/3VrWKaHod8GvOvgnR2XSdqWTIj8cJREkS1Aox8 Ckpb6SBuW5e4Dnm30oqXJtM6bNZyi3pY0RSBQzipdljBm/xamo00buvKk4RrZw== 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> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240822_022057_958421_AA18F5E0 X-CRM114-Status: GOOD ( 16.19 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Instead of having them all available, mark them all as "fail-needs-probe" and have the implementation try to probe which one is present. Also remove the shared resource workaround by moving the pinctrl entry for the trackpad interrupt line back into the individual trackpad nodes. Signed-off-by: Chen-Yu Tsai --- Changes since v4: - Rebased Changes since v3: - Also remove second source workaround, i.e. move the interrupt line pinctrl entry from the i2c node back to the components. Changes since v2: - Drop class from status --- arch/arm64/boot/dts/mediatek/mt8173-elm-hana.dtsi | 13 +++++++++++++ arch/arm64/boot/dts/mediatek/mt8173-elm.dtsi | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/arch/arm64/boot/dts/mediatek/mt8173-elm-hana.dtsi b/arch/arm64/boot/dts/mediatek/mt8173-elm-hana.dtsi index 8d1cbc92bce3..251e084bf7de 100644 --- a/arch/arm64/boot/dts/mediatek/mt8173-elm-hana.dtsi +++ b/arch/arm64/boot/dts/mediatek/mt8173-elm-hana.dtsi @@ -14,6 +14,7 @@ touchscreen2: touchscreen@34 { compatible = "melfas,mip4_ts"; reg = <0x34>; interrupts-extended = <&pio 88 IRQ_TYPE_LEVEL_LOW>; + status = "fail-needs-probe"; }; /* @@ -26,6 +27,7 @@ touchscreen3: touchscreen@20 { reg = <0x20>; hid-descr-addr = <0x0020>; interrupts-extended = <&pio 88 IRQ_TYPE_LEVEL_LOW>; + status = "fail-needs-probe"; }; /* Lenovo Ideapad C330 uses G2Touch touchscreen as a 2nd source touchscreen */ @@ -47,9 +49,12 @@ &i2c4 { trackpad2: trackpad@2c { compatible = "hid-over-i2c"; interrupts-extended = <&pio 117 IRQ_TYPE_LEVEL_LOW>; + pinctrl-names = "default"; + pinctrl-0 = <&trackpad_irq>; reg = <0x2c>; hid-descr-addr = <0x0020>; wakeup-source; + status = "fail-needs-probe"; }; }; @@ -74,3 +79,11 @@ pins_wp { }; }; }; + +&touchscreen { + status = "fail-needs-probe"; +}; + +&trackpad { + status = "fail-needs-probe"; +}; diff --git a/arch/arm64/boot/dts/mediatek/mt8173-elm.dtsi b/arch/arm64/boot/dts/mediatek/mt8173-elm.dtsi index b4d85147b77b..eee64461421f 100644 --- a/arch/arm64/boot/dts/mediatek/mt8173-elm.dtsi +++ b/arch/arm64/boot/dts/mediatek/mt8173-elm.dtsi @@ -358,12 +358,12 @@ touchscreen: touchscreen@10 { &i2c4 { clock-frequency = <400000>; status = "okay"; - pinctrl-names = "default"; - pinctrl-0 = <&trackpad_irq>; trackpad: trackpad@15 { compatible = "elan,ekth3000"; interrupts-extended = <&pio 117 IRQ_TYPE_LEVEL_LOW>; + pinctrl-names = "default"; + pinctrl-0 = <&trackpad_irq>; reg = <0x15>; vcc-supply = <&mt6397_vgp6_reg>; wakeup-source;