From nobody Thu Sep 19 10:41:31 2024 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B9A6113C80A for ; Wed, 11 Sep 2024 07:28:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726039704; cv=none; b=oPoHT3fMcOU0fZ2xYlZXowtTQHwmUHbAv+DGfkJtJq+9Ks1xRvn0jl3Ciu5G++8SF5sZReIYoY8/x1euikPGlPBce5q4Q3pjAte8P3nljLRLL2kgSjrHUa9tQG0r251OF68KZP7Qb8Cyw+eIvjinNZsQHzKoTifjSlKfIDiPUFQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726039704; c=relaxed/simple; bh=zj/HJDeowM5ESxF1vN4op15RGlHBPvxyJE3xPNXJ1vg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GO7ZdqLP/Qe5u+dbQGgMwAUE2CUS3NZ50sPnKltSnLxP+KiFR7lAlXzYqD5viRARihNXQdxodcUuF84vYOHR/Tr7cnq1QEyoOhjlAEHTOokklZt4s4HnJLk0f4MzIb7SJLtrH2X7s8IuCsAafVm9fP3n17juWeZCKzYRjJV4k24= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=AJRaz2EC; arc=none smtp.client-ip=209.85.210.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="AJRaz2EC" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-718816be6cbso4562096b3a.1 for ; Wed, 11 Sep 2024 00:28:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1726039702; x=1726644502; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=9XBl4KvAxY2W5qf9J8VaXaQIQeHry8eBV5yNyqGAqXM=; b=AJRaz2ECRrMaSSYChDorTN36boQKfK5SgRqvgFrVS2oIj9ZUZXalqsMEqWU0WcW56d B866SeSDc5StFxcNQKc/Wsr+dNgmPtmXV+S5hPWeJ3eIn96LiVm2fMg5zcCrKZLeSM0+ dqNbDJalScNFR6wNsCme9ZwLD+xvhYe8sHqOY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726039702; x=1726644502; 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=9XBl4KvAxY2W5qf9J8VaXaQIQeHry8eBV5yNyqGAqXM=; b=JUZSmPZEjoyEOvll/YuQqFUsn/zRR1qZjGPdy4bbY9jCTUUMPH7PBJQv+3MCkAqEu1 2Ld/Bp3yXW0kxZmeldHKjDvun3BioznUGthICwtKQt0lja3u9YyYOYyo3eGBHtMnxfgx o6aFajfPQvkQMuEapsJHBUfgRz8ZQ6+Lxzts1cnGV4XQzZE3IS7UahWDQ12FlmcbZ1EN mH6RfTYp9yMBeC94nloZK5616wUzcZHF8Aw7f5VjmdYaA4iDZoHsNHno0yoeXkELskSx 4SocgTCyFxaJbLEqLYJ3n7m5qw7Bu7ZUO2ZztDeOJRaw0aRtqY+ivRPlZpEKFhif+5DE FHcw== X-Forwarded-Encrypted: i=1; AJvYcCUgE/yG+RNfOyfjHzKocrjdZtHGtO582ZMfsYK/06JBn0GzqpUJOHsQVfw4WeYGRt2WSpR8Kg0FmXJ5nzU=@vger.kernel.org X-Gm-Message-State: AOJu0Yz2/5g99U84x+ECa+tjgSotAsclnx1Pb9FZR4qvYbpsAV0O0Xrn RheJC3ynwV1egVIj59TchmcErUYN9ZNyXi/6zX57xDAU1AHDCxXjk1TeZcbFzg== X-Google-Smtp-Source: AGHT+IFjUTc14exhMNLPWpC6tKqxzxp/QnxrLH5ALh3HkEgdTdW2uDtj4xbh/J245j8YHjY14LvqFA== X-Received: by 2002:aa7:8887:0:b0:718:d96d:34d7 with SMTP id d2e1a72fcca58-718e3f9caacmr18512422b3a.3.1726039701972; Wed, 11 Sep 2024 00:28:21 -0700 (PDT) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:8398:fe34:eba2:f301]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71908fe4e7esm2399415b3a.80.2024.09.11.00.28.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Sep 2024 00:28:21 -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 v7 01/10] of: dynamic: Add of_changeset_update_prop_string Date: Wed, 11 Sep 2024 15:27:39 +0800 Message-ID: <20240911072751.365361-2-wenst@chromium.org> X-Mailer: git-send-email 2.46.0.598.g6f2099f65c-goog In-Reply-To: <20240911072751.365361-1-wenst@chromium.org> References: <20240911072751.365361-1-wenst@chromium.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a helper function to add string property updates to an OF changeset. This is similar to of_changeset_add_prop_string(), but instead of adding the property (and failing if it exists), it will update the property. This shall be used later in the DT hardware prober. Signed-off-by: Chen-Yu Tsai Reviewed-by: Rob Herring (Arm) --- Changes since v6: None. Changes since v5: - Collected Rob's tag Changes since v4: - Use modern designated initializer for |prop| Changes since v3: - Use new __of_prop_free() helper - Add new line before header declaration Changes since v2: - New patch added in v3 --- drivers/of/dynamic.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ include/linux/of.h | 4 ++++ 2 files changed, 48 insertions(+) diff --git a/drivers/of/dynamic.c b/drivers/of/dynamic.c index 110104a936d9..daa69d160a78 100644 --- a/drivers/of/dynamic.c +++ b/drivers/of/dynamic.c @@ -1072,3 +1072,47 @@ int of_changeset_add_prop_bool(struct of_changeset *= ocs, struct device_node *np, return of_changeset_add_prop_helper(ocs, np, &prop); } EXPORT_SYMBOL_GPL(of_changeset_add_prop_bool); + +static int of_changeset_update_prop_helper(struct of_changeset *ocs, + struct device_node *np, + const struct property *pp) +{ + struct property *new_pp; + int ret; + + new_pp =3D __of_prop_dup(pp, GFP_KERNEL); + if (!new_pp) + return -ENOMEM; + + ret =3D of_changeset_update_property(ocs, np, new_pp); + if (ret) + __of_prop_free(new_pp); + + return ret; +} + +/** + * of_changeset_update_prop_string - Add a string property update to a cha= ngeset + * + * @ocs: changeset pointer + * @np: device node pointer + * @prop_name: name of the property to be updated + * @str: pointer to null terminated string + * + * Create a string property to be updated and add it to a changeset. + * + * Return: 0 on success, a negative error value in case of an error. + */ +int of_changeset_update_prop_string(struct of_changeset *ocs, + struct device_node *np, + const char *prop_name, const char *str) +{ + struct property prop =3D { + .name =3D (char *)prop_name, + .length =3D strlen(str) + 1, + .value =3D (void *)str, + }; + + return of_changeset_update_prop_helper(ocs, np, &prop); +} +EXPORT_SYMBOL_GPL(of_changeset_update_prop_string); diff --git a/include/linux/of.h b/include/linux/of.h index 85b60ac9eec5..046283be1cd3 100644 --- a/include/linux/of.h +++ b/include/linux/of.h @@ -1651,6 +1651,10 @@ static inline int of_changeset_add_prop_u32(struct o= f_changeset *ocs, return of_changeset_add_prop_u32_array(ocs, np, prop_name, &val, 1); } =20 +int of_changeset_update_prop_string(struct of_changeset *ocs, + struct device_node *np, + const char *prop_name, const char *str); + int of_changeset_add_prop_bool(struct of_changeset *ocs, struct device_nod= e *np, const char *prop_name); =20 --=20 2.46.0.598.g6f2099f65c-goog From nobody Thu Sep 19 10:41:31 2024 Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E673D13D635 for ; Wed, 11 Sep 2024 07:28:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726039707; cv=none; b=ug8neGK0nfTk4DewALcO8GxG94T85/mmOJ7GpyFNx1MUjhBhbo4wA1yncGJAazOvEOqfPtgWdiP1OPs1+UWgjmNvhal+S1fvM/CtQSDQ5+gU9qAUcsJ524iXKTIAKbg4SFFSfBuPEa5uZvO7Admido8seDE16Re88dE45zNdiOo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726039707; c=relaxed/simple; bh=WIfkiNtSKIwyx7GizcoCeKoQku6qSRlQSE16sb4sWRA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FhmWjrngPmJOgodlbn5xy3StdSz3vLrNGQQ/sQIWmzRWc8+htvyuZmuJW6IjZEjYC6H+TdFtAebIQY7UeoXARyL1vPyB9XrhraTXnCUZzgMcgYjU0lxx6CnJhcR+/3EFIC3C+CrVXJn5Cuq0RaJB+cLyVxz0EUpsMiFwEWBHgKA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=JOvuQXKv; arc=none smtp.client-ip=209.85.210.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="JOvuQXKv" Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-718d6ad6050so4036228b3a.0 for ; Wed, 11 Sep 2024 00:28:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1726039705; x=1726644505; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=VHYuF087WkDWw6t01rd+JoBLqqCohsT20qRvxciVpzs=; b=JOvuQXKvQXdA+EWJKiDscgshBbTYFfQFZrP7j5E/nkmRN0mHQKkqeGnvJ51LE6RgwC tfUIGBOKOiAT1zl4G4mZYIbygfss5v2U8YC0YIk0KPTjZBZsrloa9tCApA1Az3m754Ss XprxpzgBZTJmzGKwVPg6omsQmi8zRXArGXRXE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726039705; x=1726644505; 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=VHYuF087WkDWw6t01rd+JoBLqqCohsT20qRvxciVpzs=; b=NWGpdnPkahOHtS7eaa+x7SI83oyU6wokVokXlpb1MMuLr4ldbM6N+jVEy7BvWs0OZs BfLr9QkaKepYJI9g0pdjhoSmK7gcWIscy+r9BhkpZdPCXQACmE3NOYeyugZSlI7cQ8ei PQGzudJznkGbF5U4AHyXONPs7J4cskQ5QqNGacBPITaFAy/fTps+2dVHCZIXVMBNX1qJ AoOBoeMqj/JZIgtD51CQMb2kC9nfCzMs6oCvM+tU1wMOZK6neA81fbX6yoiuysDCBF2D lvAqPjWgrrYGRLmvf9nf56m/vaNSYmtYYIf0WLHxcvP7dlPhksgn+LnBlRfHyLpbV2kg bJFw== X-Forwarded-Encrypted: i=1; AJvYcCU+80acvzwxT13VrrTwBwgrLfst5QgIlrMTFRzJ5vpQchrYwOb1b1UJcK4I3lRe8Dt7051ornC32oMXlsY=@vger.kernel.org X-Gm-Message-State: AOJu0YxsYF7qPhKuj39a+Xk9cRZz8Kn3XRK3ex4Apdwk8+G575bOwYvA xTsKYJyfJTcQgEomPIVbM2pKCth/UbnK1gAqRvOcOSbP+ZuXx6D6rI+PXGQ4iw== X-Google-Smtp-Source: AGHT+IF7NCaFQn5NI21IL8mvEhnYcnrMWrYJBC5MrY8umX2zBuXqZDWtQyZtz6v7uD5SvjxSxtithQ== X-Received: by 2002:a05:6a20:2d0b:b0:1cf:6dd7:fa50 with SMTP id adf61e73a8af0-1cf6dd7fa7cmr114263637.11.1726039705459; Wed, 11 Sep 2024 00:28:25 -0700 (PDT) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:8398:fe34:eba2:f301]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71908fe4e7esm2399415b3a.80.2024.09.11.00.28.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Sep 2024 00:28:25 -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 v7 02/10] of: base: Add for_each_child_of_node_with_prefix() Date: Wed, 11 Sep 2024 15:27:40 +0800 Message-ID: <20240911072751.365361-3-wenst@chromium.org> X-Mailer: git-send-email 2.46.0.598.g6f2099f65c-goog In-Reply-To: <20240911072751.365361-1-wenst@chromium.org> References: <20240911072751.365361-1-wenst@chromium.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" There are cases where drivers would go through child device nodes and operate on only the ones whose node name starts with a given prefix. Provide a helper for these users. This will mainly be used in a subsequent patch that implements a hardware component prober for I2C busses. Signed-off-by: Chen-Yu Tsai Reviewed-by: Rob Herring (Arm) --- Changes since v6: - Changed helper name to "for_each_child_of_node_with_prefix()" Changes since v5: - New patch --- drivers/of/base.c | 35 +++++++++++++++++++++++++++++++++++ include/linux/of.h | 9 +++++++++ 2 files changed, 44 insertions(+) diff --git a/drivers/of/base.c b/drivers/of/base.c index 20603d3c9931..d3c123b3261a 100644 --- a/drivers/of/base.c +++ b/drivers/of/base.c @@ -628,6 +628,41 @@ struct device_node *of_get_next_child(const struct dev= ice_node *node, } EXPORT_SYMBOL(of_get_next_child); =20 +/** + * of_get_next_child_with_prefix - Find the next child node with prefix + * @node: parent node + * @prev: previous child of the parent node, or NULL to get first + * + * This function is like of_get_next_child(), except that it automatically + * skips any nodes whose name doesn't have the given prefix. + * + * Return: A node pointer with refcount incremented, use + * of_node_put() on it when done. + */ +struct device_node *of_get_next_child_with_prefix(const struct device_node= *node, + struct device_node *prev, + const char *prefix) +{ + struct device_node *next; + unsigned long flags; + + if (!node) + return NULL; + + raw_spin_lock_irqsave(&devtree_lock, flags); + next =3D prev ? prev->sibling : node->child; + for (; next; next =3D next->sibling) { + if (!of_node_name_prefix(next, prefix)) + continue; + if (of_node_get(next)) + break; + } + of_node_put(prev); + raw_spin_unlock_irqrestore(&devtree_lock, flags); + return next; +} +EXPORT_SYMBOL(of_get_next_child_with_prefix); + static struct device_node *of_get_next_status_child(const struct device_no= de *node, struct device_node *prev, bool (*checker)(const struct device_node *)) diff --git a/include/linux/of.h b/include/linux/of.h index 046283be1cd3..48cfb39197d6 100644 --- a/include/linux/of.h +++ b/include/linux/of.h @@ -289,6 +289,9 @@ extern struct device_node *of_get_parent(const struct d= evice_node *node); extern struct device_node *of_get_next_parent(struct device_node *node); extern struct device_node *of_get_next_child(const struct device_node *nod= e, struct device_node *prev); +extern struct device_node *of_get_next_child_with_prefix(const struct devi= ce_node *node, + struct device_node *prev, + const char *prefix); extern struct device_node *of_get_next_available_child( const struct device_node *node, struct device_node *prev); extern struct device_node *of_get_next_reserved_child( @@ -1468,6 +1471,12 @@ static inline int of_property_read_s32(const struct = device_node *np, child !=3D NULL; \ child =3D of_get_next_child(parent, child)) =20 +#define for_each_child_of_node_with_prefix(parent, child, prefix) \ + for (struct device_node *child __free(device_node) =3D \ + of_get_next_child_with_prefix(parent, NULL, prefix); \ + child !=3D NULL; \ + child =3D of_get_next_child_with_prefix(parent, child, prefix)) + #define for_each_available_child_of_node(parent, child) \ for (child =3D of_get_next_available_child(parent, NULL); child !=3D NULL= ; \ child =3D of_get_next_available_child(parent, child)) --=20 2.46.0.598.g6f2099f65c-goog From nobody Thu Sep 19 10:41:31 2024 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1C2551442E3 for ; Wed, 11 Sep 2024 07:28:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726039711; cv=none; b=lSg7hk7Xglv53WNd40hEBy+eYewdc9Y1hW757pDzYWnY4969kGhRNbTc5JiomkZYH4VbVbNuhCYLxMOeHOA8OpO2cl2W3FMOXShPo1qu/pi21c05DkwlVlojQ1HADZ5rtyE3hjKUVk4eOuiru5OTiOjXglQXDB33RZhbDXEDYZM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726039711; c=relaxed/simple; bh=GcyKgQsfjkBKNwaAizHmmILs0FYR28hHW2QvCF0Pxwg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Rs7dp2lo/CNCO+nAi2qXZ0r77jEI+P2MtDgczSdKeg28IT57p2nRHWTq97MThnzdPtlvoqGkRMU7n/crqIgOh2frI2HScZZNw5TWTBcvpsD5Pzj5MQBwrAgQxF6osGBhARfjKIMxgLPs19XL3LTbsTMq67NoAxsJJbB8lkk1Z9Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=Z/JS5XU3; arc=none smtp.client-ip=209.85.210.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="Z/JS5XU3" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-718d91eef2eso421089b3a.1 for ; Wed, 11 Sep 2024 00:28:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1726039709; x=1726644509; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Uo9H3mi08AzEIcyFPjnP71nK8rUWm/O6uNXtiPTxfPw=; b=Z/JS5XU38S7Q+WFE6PlkpaNvoF4jMuQ4eIg8ui1HL5h5vASOgRtuZTXAKf40hyZDK9 305hj/QMmvRDDCsJknAOI7AGC++SlKq0AxT5HxeC7DVElVKWk4t33PtMVU7Puag/h+WG +zLoKtSnlUKdBWIZZkisPNdObBUbtFFr5+Ngg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726039709; x=1726644509; 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=Uo9H3mi08AzEIcyFPjnP71nK8rUWm/O6uNXtiPTxfPw=; b=gM1/h6+r9+eE5HNH+pBIdRBnfu2nU08GCBJp2X7KKFycNLeaMFwKoRRSfskRzdLicU ia8NI1/9l4zTo7ScaKV2vPZPM0oL1vSQn7A1O8MiIQyUJNfWiDCdglNBsmnFYl2HzleP ILSFalj8hIMW3HWPWGZZN17TLlL8d6HkA2DZZ1JJ1V/i0AmBU2qE7mI9xR4djt9N2Ici YOKYvKi81Ca9egJvVToFm9NkWnRJUxNc//LLxcWJ+O2LHSMvwQAcedc99uTE2MrpkFuT NK9AXCV6TERVQ+smOAjGV2fcIo/psCiHg022NwHhYgjNoaPHTaZrh2Ni8lMA86dEfgop wjXw== X-Forwarded-Encrypted: i=1; AJvYcCUcW513KztPOCoVrRiY0mnQDgHkbbFYRRG5m3Drjay9LDVWuStj5X06mrUNTrDKgWXYebeQVUTTzRltAXM=@vger.kernel.org X-Gm-Message-State: AOJu0YxZEQ6v57TWgUjB0ROAL3HfjXE87aMYAtdR7QebtCoX1nf5DnDh t5Nr+1G5VCCM2lYzmj4mNXylMsqwMpGP7ZeLKfziq02EPKcvk1FFLrtpxxv9lQ== X-Google-Smtp-Source: AGHT+IH8y/5qgaRg14jmmESJlTP33GJD1mpI9XyvqpkYm264oWkGo2r0XR7RzXDvrRycmczgHdwVfQ== X-Received: by 2002:a05:6a00:6f0c:b0:70d:26cd:9741 with SMTP id d2e1a72fcca58-71907f29d40mr7749073b3a.12.1726039709427; Wed, 11 Sep 2024 00:28:29 -0700 (PDT) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:8398:fe34:eba2:f301]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71908fe4e7esm2399415b3a.80.2024.09.11.00.28.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Sep 2024 00:28:29 -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 v7 03/10] regulator: Split up _regulator_get() Date: Wed, 11 Sep 2024 15:27:41 +0800 Message-ID: <20240911072751.365361-4-wenst@chromium.org> X-Mailer: git-send-email 2.46.0.598.g6f2099f65c-goog In-Reply-To: <20240911072751.365361-1-wenst@chromium.org> References: <20240911072751.365361-1-wenst@chromium.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" _regulator_get() contains a lot of common code doing checks prior to the regulator lookup and housekeeping work after the lookup. Almost all the code could be shared with a OF-specific variant of _regulator_get(). Split out the common parts so that they can be reused. The OF-specific version of _regulator_get() will be added in a subsequent patch. No functional changes were made. Signed-off-by: Chen-Yu Tsai Reviewed-by: Andy Shevchenko --- Changes since v6: None. Changes since v5: - Fixed kerneldoc "Return" section format for _regulator_get_common() - Slightly reworded return value description 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 835a5531d045..d60c86477ac2 100644 --- a/drivers/regulator/core.c +++ b/drivers/regulator/core.c @@ -2103,26 +2103,43 @@ static int regulator_resolve_supply(struct regulato= r_dev *rdev) return ret; } =20 -/* Internal regulator request function */ -struct regulator *_regulator_get(struct device *dev, const char *id, - enum regulator_get_type get_type) +/* common pre-checks for regulator requests */ +int _regulator_get_common_check(struct device *dev, const char *id, + enum regulator_get_type get_type) { - struct regulator_dev *rdev; - struct regulator *regulator; - struct device_link *link; - int ret; - if (get_type >=3D MAX_GET_TYPE) { dev_err(dev, "invalid type %d in %s\n", get_type, __func__); - return ERR_PTR(-EINVAL); + return -EINVAL; } =20 if (id =3D=3D NULL) { dev_err(dev, "regulator request with no identifier\n"); - return ERR_PTR(-EINVAL); + return -EINVAL; } =20 - rdev =3D regulator_dev_lookup(dev, id); + return 0; +} + +/** + * _regulator_get_common - Common code for regulator requests + * @rdev: regulator device pointer as returned by *regulator_dev_lookup() + * Its reference count is expected to have been incremented. + * @dev: device used for dev_printk messages + * @id: Supply name or regulator ID + * @get_type: enum regulator_get_type value corresponding to type of reque= st + * + * Returns: pointer to struct regulator corresponding to @rdev, or ERR_PTR= () + * encoded error. + * + * This function should be chained with *regulator_dev_lookup() functions. + */ +struct regulator *_regulator_get_common(struct regulator_dev *rdev, struct= device *dev, + const char *id, enum regulator_get_type get_type) +{ + struct regulator *regulator; + struct device_link *link; + int ret; + if (IS_ERR(rdev)) { ret =3D PTR_ERR(rdev); =20 @@ -2238,6 +2255,21 @@ struct regulator *_regulator_get(struct device *dev,= const char *id, return regulator; } =20 +/* Internal regulator request function */ +struct regulator *_regulator_get(struct device *dev, const char *id, + enum regulator_get_type get_type) +{ + struct regulator_dev *rdev; + int ret; + + ret =3D _regulator_get_common_check(dev, id, get_type); + if (ret) + return ERR_PTR(ret); + + rdev =3D regulator_dev_lookup(dev, id); + return _regulator_get_common(rdev, dev, id, get_type); +} + /** * regulator_get - lookup and obtain a reference to a regulator. * @dev: device for regulator "consumer" diff --git a/drivers/regulator/internal.h b/drivers/regulator/internal.h index 8e5506c5ee94..5b43f802468d 100644 --- a/drivers/regulator/internal.h +++ b/drivers/regulator/internal.h @@ -121,6 +121,10 @@ enum regulator_get_type { MAX_GET_TYPE }; =20 +int _regulator_get_common_check(struct device *dev, const char *id, + enum regulator_get_type get_type); +struct regulator *_regulator_get_common(struct regulator_dev *rdev, struct= device *dev, + const char *id, enum regulator_get_type get_type); struct regulator *_regulator_get(struct device *dev, const char *id, enum regulator_get_type get_type); int _regulator_bulk_get(struct device *dev, int num_consumers, --=20 2.46.0.598.g6f2099f65c-goog From nobody Thu Sep 19 10:41:31 2024 Received: from mail-oa1-f42.google.com (mail-oa1-f42.google.com [209.85.160.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D293A149E17 for ; Wed, 11 Sep 2024 07:28:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726039716; cv=none; b=S7SaDcCxZysuHE8mKE+29oZKhTsGlr+vOh0NniSZH26UYlIiXDM639E6QsNoVVQltVFKSLMZwQwHj/J24YnpQ2ZzmvIoJerMINw6KLHrmaXAq/lW11k+QXKF4OzY0vIV6QJ6IEzVsE14+Hdmun3Jbv5rsDr6/FbxVlHZNnpC1Ec= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726039716; c=relaxed/simple; bh=WxMROr7huCM27GHQ6nDzxDb5JW0IZzdP8pMTdaNq6Rs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NWxZJAmNIVScUgZNJ5SLM81wl7qBIGAfqy4W4NYuW93Doj7i5q2oB0kMu5t0SGu+kxPChmhitqY+oKcyeDNwueOe2GlGgBYYLRuNceWZazmEeT4HZKfhdSbhgyVATMVw9fLlac63NmPOav4xoEOwJoFBfpDDTgDaQhJH5p+IzUM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=jIO0qvAh; arc=none smtp.client-ip=209.85.160.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="jIO0qvAh" Received: by mail-oa1-f42.google.com with SMTP id 586e51a60fabf-27b88b8a24aso2402369fac.1 for ; Wed, 11 Sep 2024 00:28:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1726039713; x=1726644513; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=usCLS2gw9idy/3IwRZ6LuORHWtJja/i6x5Lk0clI0uw=; b=jIO0qvAh+oNnWD+ArkzqxgmGBKsqv2PWN65ot8qAEgrlkxZ7Imia4ZmEf7gi0YSv+g j82kVoHbcN1YgWyJV0ILqShuhgDld9Tna0ukxKIrMwCCzmlTjDUvu+z6IT0RVF/EOjaK H60de3TttUud8B30jDRfmovmyUvoLP1IH8VS0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726039713; x=1726644513; 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=usCLS2gw9idy/3IwRZ6LuORHWtJja/i6x5Lk0clI0uw=; b=oH0Vu30AiA3gYpar+e3ZsaQ+DxJhmjdrAGD+Bqs1DBq51M+GEHJlCQXaG2ZhmYEjUr XtzgmWdzqBNzPd2A+dkc02fQGyxPHnUC4GzD7pBmrknbNY+QKcIGkBRs3jGMGYnw9GeA 9d02h3dZlmPhIfyvkmKoz9x8bvz7ELBj4IZttcbwCBBGxFMl7id9iAwri4TO9QwAzV6f I8UcuOxemF+3WtRzlZBCM1FCgO7Pp3A3P0JiAUAZt+7Yf4x7g9WLxVrbR3JS3XzFVZWM v7h3RMv5LsVY+KL5XWjualXpkwYEccudG+qVXW+ZgCgq8PgFftzOfs4lcaDkxawmBTxz gL8g== X-Forwarded-Encrypted: i=1; AJvYcCVCQbaGBvMGum9Xx8dcXWtVP2oGf12FNHIPXc+VlV5M948JQ84+rf89zJnstFUouOtirO/MzFroqVvlAXc=@vger.kernel.org X-Gm-Message-State: AOJu0YykLjtfipgVtKN+TTM9tAvaj0SHST4UhNKt4uU46Xs3GYDuIv4H vMcF7cqBu5u2y5hSyeom8UmbPd6KCxyP+rOYdN4P6RjOcEwgDRrZvBja/g/l0w== X-Google-Smtp-Source: AGHT+IFNM7b4JkSSxOxl6LIFw91t1hLIJvdgyEh6OE43L+BvV2ny7clTTpH+bkJj2RTSowhSQE6gdw== X-Received: by 2002:a05:6870:1702:b0:24f:dd11:4486 with SMTP id 586e51a60fabf-27b9dd8110fmr8247849fac.36.1726039712931; Wed, 11 Sep 2024 00:28:32 -0700 (PDT) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:8398:fe34:eba2:f301]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71908fe4e7esm2399415b3a.80.2024.09.11.00.28.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Sep 2024 00:28:32 -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 v7 04/10] regulator: Add of_regulator_get_optional() for pure DT regulator lookup Date: Wed, 11 Sep 2024 15:27:42 +0800 Message-ID: <20240911072751.365361-5-wenst@chromium.org> X-Mailer: git-send-email 2.46.0.598.g6f2099f65c-goog In-Reply-To: <20240911072751.365361-1-wenst@chromium.org> References: <20240911072751.365361-1-wenst@chromium.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The to-be-introduced I2C component prober needs to enable regulator supplies (and toggle GPIO pins) for the various components it intends to probe. To support this, a new "pure DT lookup" method for getting regulator supplies is needed, since the device normally requesting the supply won't get created until after the component is probed to be available. Add a new of_regulator_get_optional() function for this. This mirrors the existing regulator_get_optional() function, but is OF-specific. The underlying code that supports the existing regulator_get*() functions has been reworked in previous patches to support this specific case. Also convert an existing usage of "dev && dev->of_node" to "dev_of_node(dev)". Link: https://lore.kernel.org/all/20231220203537.83479-2-jernej.skrabec@gma= il.com/ [1] Signed-off-by: Chen-Yu Tsai --- Changes since v6: - Changed reference [1] to Link: tag - Rebased on top of commit 401d078eaf2e ("regulator: of: Refactor of_get_*regulator() to decrease indentation") - Exported of_regulator_get_optional() - Changed commit message to focus on "of_regulator_get_optional()" - Dropped change to of_regulator_bulk_get_all() Changes since v5: - Used "dev_of_node(dev)" instead of "dev->of_node" - Replaced "dev_printk" with "dev_printk()" in kerneldoc mentions - Fixed kerneldoc "Return" section format for of_regulator_get_optional() - Fix @np parameter name in of_regulator_dev_lookup() kerneldoc Changes since 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 | 4 +-- drivers/regulator/internal.h | 2 ++ drivers/regulator/of_regulator.c | 51 ++++++++++++++++++++++++++---- include/linux/regulator/consumer.h | 4 +++ 4 files changed, 53 insertions(+), 8 deletions(-) diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c index d60c86477ac2..f94a06ac2109 100644 --- a/drivers/regulator/core.c +++ b/drivers/regulator/core.c @@ -1959,8 +1959,8 @@ static struct regulator_dev *regulator_dev_lookup(str= uct device *dev, regulator_supply_alias(&dev, &supply); =20 /* first do a dt based lookup */ - if (dev && dev->of_node) { - r =3D of_regulator_dev_lookup(dev, supply); + if (dev_of_node(dev)) { + r =3D of_regulator_dev_lookup(dev, dev_of_node(dev), supply); if (!IS_ERR(r)) return r; if (PTR_ERR(r) =3D=3D -EPROBE_DEFER) diff --git a/drivers/regulator/internal.h b/drivers/regulator/internal.h index 5b43f802468d..f62cacbbc729 100644 --- a/drivers/regulator/internal.h +++ b/drivers/regulator/internal.h @@ -67,6 +67,7 @@ static inline struct regulator_dev *dev_to_rdev(struct de= vice *dev) =20 #ifdef CONFIG_OF struct regulator_dev *of_regulator_dev_lookup(struct device *dev, + struct device_node *np, const char *supply); struct regulator_init_data *regulator_of_get_init_data(struct device *dev, const struct regulator_desc *desc, @@ -82,6 +83,7 @@ bool of_check_coupling_data(struct regulator_dev *rdev); =20 #else static inline struct regulator_dev *of_regulator_dev_lookup(struct device = *dev, + struct device_node *np, const char *supply) { return ERR_PTR(-ENODEV); diff --git a/drivers/regulator/of_regulator.c b/drivers/regulator/of_regula= tor.c index 3f490d81abc2..358c3ed791db 100644 --- a/drivers/regulator/of_regulator.c +++ b/drivers/regulator/of_regulator.c @@ -588,7 +588,8 @@ static struct device_node *of_get_child_regulator(struc= t device_node *parent, =20 /** * of_get_regulator - get a regulator device node based on supply name - * @dev: Device pointer for the consumer (of regulator) device + * @dev: Device pointer for dev_printk() messages + * @node: Device node pointer for supply property lookup * @supply: regulator supply name * * Extract the regulator device node corresponding to the supply name. @@ -596,15 +597,16 @@ static struct device_node *of_get_child_regulator(str= uct device_node *parent, * Return: Pointer to the &struct device_node corresponding to the regulat= or * if found, or %NULL if not found. */ -static struct device_node *of_get_regulator(struct device *dev, const char= *supply) +static struct device_node *of_get_regulator(struct device *dev, struct dev= ice_node *node, + const char *supply) { struct device_node *regnode =3D NULL; char prop_name[64]; /* 64 is max size of property name */ =20 - dev_dbg(dev, "Looking up %s-supply from device tree\n", supply); + dev_dbg(dev, "Looking up %s-supply from device node %pOF\n", supply, node= ); =20 snprintf(prop_name, 64, "%s-supply", supply); - regnode =3D of_parse_phandle(dev->of_node, prop_name, 0); + regnode =3D of_parse_phandle(node, prop_name, 0); if (regnode) return regnode; =20 @@ -628,6 +630,7 @@ static struct regulator_dev *of_find_regulator_by_node(= struct device_node *np) /** * of_regulator_dev_lookup - lookup a regulator device with device tree on= ly * @dev: Device pointer for regulator supply lookup. + * @np: Device node pointer for regulator supply lookup. * @supply: Supply name or regulator ID. * * Return: Pointer to the &struct regulator_dev on success, or ERR_PTR() @@ -642,13 +645,13 @@ static struct regulator_dev *of_find_regulator_by_nod= e(struct device_node *np) * * -%ENODEV if lookup fails permanently. * * -%EPROBE_DEFER if lookup could 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 d= evice_node *np, const char *supply) { struct regulator_dev *r; struct device_node *node; =20 - node =3D of_get_regulator(dev, supply); + node =3D of_get_regulator(dev, np, supply); if (node) { r =3D of_find_regulator_by_node(node); of_node_put(node); @@ -665,6 +668,42 @@ struct regulator_dev *of_regulator_dev_lookup(struct d= evice *dev, return ERR_PTR(-ENODEV); } =20 +static struct regulator *_of_regulator_get(struct device *dev, struct devi= ce_node *node, + const char *id, enum regulator_get_type get_type) +{ + struct regulator_dev *r; + int ret; + + ret =3D _regulator_get_common_check(dev, id, get_type); + if (ret) + return ERR_PTR(ret); + + r =3D of_regulator_dev_lookup(dev, node, id); + return _regulator_get_common(r, dev, id, get_type); +} + +/** + * of_regulator_get_optional - get optional regulator via device tree look= up + * @dev: device used for dev_printk() messages + * @node: device node for regulator "consumer" + * @id: Supply name + * + * Return: pointer to struct regulator corresponding to the regulator prod= ucer, + * or PTR_ERR() encoded error number. + * + * This is intended for use by consumers that want to get a regulator + * supply directly from a device node, and can and want to deal with + * absence of such supplies. This will _not_ consider supply aliases. + * See regulator_dev_lookup(). + */ +struct regulator *of_regulator_get_optional(struct device *dev, + struct device_node *node, + const char *id) +{ + return _of_regulator_get(dev, node, id, OPTIONAL_GET); +} +EXPORT_SYMBOL_GPL(of_regulator_get_optional); + /* * Returns number of regulators coupled with rdev. */ diff --git a/include/linux/regulator/consumer.h b/include/linux/regulator/c= onsumer.h index b9ce521910a0..d7325e27a1ce 100644 --- a/include/linux/regulator/consumer.h +++ b/include/linux/regulator/consumer.h @@ -168,6 +168,10 @@ int devm_regulator_get_enable_read_voltage(struct devi= ce *dev, const char *id); void regulator_put(struct regulator *regulator); void devm_regulator_put(struct regulator *regulator); =20 +struct regulator *__must_check of_regulator_get_optional(struct device *de= v, + struct device_node *node, + const char *id); + int regulator_register_supply_alias(struct device *dev, const char *id, struct device *alias_dev, const char *alias_id); --=20 2.46.0.598.g6f2099f65c-goog From nobody Thu Sep 19 10:41:31 2024 Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1145714A0AB for ; Wed, 11 Sep 2024 07:28:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726039718; cv=none; b=D8CungiEbGM0SbZmswH4YypNExAabin77c2jjtt1UqZXaGXHYMbbe+OAAH55PUT8Xg5DAurK+qZXPqxbM/LGNsIRj4MSmGbN5ks/j5f031ygcQcE96RsBxL3tna4+ncaRAFQCHGf7LoRipCaaRvno4tXof3RKz1ZGSlJLOaYQSU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726039718; c=relaxed/simple; bh=XxiTjlQcSLEHrjz0okO6v8gsp7M/kr47QP0mR5JSK08=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QBrEhaUOUBkRUeaFslnaObjX7H/JMA4q1MpRzEu1sOiXiUM3Rke874NIh8cAAaKbwZgqL7KFjlZQj4GPh69ptZKCXMjxZNg5tTMC/YMAjDHl3FuJ5+e0Juj7R5CVsLqjJF4U1MRP0ivO4nXY5u57Upl8dIoyQDaTmJY1wLTYNic= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=AWqja8Do; arc=none smtp.client-ip=209.85.210.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="AWqja8Do" Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-7179069d029so4548207b3a.2 for ; Wed, 11 Sep 2024 00:28:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1726039716; x=1726644516; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=TZj+Rwt2K/givoXdUXg/sARHG7LMQ+JDTIquBzN2oCY=; b=AWqja8Do11n2xY2bO0h7B7OseZ4raQjQ86+PRPLDN7Bb7g3d2kQX/PGqZFi+cRCacC Dam7Wx8/GIrN5M/VGsQxOuDdphRIAMhEKn43gFOVxIRq9GbVjXOGb+/GWJPlvRpqOQw2 81uvN2vw7OIRfE9fzi/SjQuYwdjgin7HOoja4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726039716; x=1726644516; 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=TZj+Rwt2K/givoXdUXg/sARHG7LMQ+JDTIquBzN2oCY=; b=rEyrDV4BCDucmBPmHZ5aH6HgxcQOR/eN/2SuqsZwJVhifjeVW5J2M4Z9TdUOskcFUW /qyYEkpWfAonan7uu5mbYAWM7pBYJJbURdNf+PmLo2amcsEqaiYnvn78StY3qOLsSkme F4IKYksIDo6yrcfZZEodLb2OoJ9BsQs1NZpDHR24rwwCmW3sVQPuDzqf3Dz7JTc0oJZM AzufqGCo2VIK6GhasZlTMzrIZtIKnCNc012yBIu8trdho2tXQtwpaYEVV2ZoBZJB9USo HfVmxvTcXLOLzBxbK8mIN2tMYA7SPtGrJEe3QlGsc13UPJuyqLPjAeTr3RoNsJ0jHSVw Swcg== X-Forwarded-Encrypted: i=1; AJvYcCUmSNCaWrwftZQqoYW8dgOxJ5gIQVgeFCMYRdoj2pg+Qa0mB7RgmF907YovbzytZtrbhtgNCwXxRSJ7qUc=@vger.kernel.org X-Gm-Message-State: AOJu0YwwKECcxpvWKMs5CX6NUjU4Is9bpxAkahpg1FGJ9mVMl3DoSqy5 ea+7yBTZwwatbyf9919ZznpqzOWc/VOD+z2EpbvYY/BFaohQL/CYV6BJkDTBwg== X-Google-Smtp-Source: AGHT+IHedUO15STr+MI+20JywnkGwOWN1412qS2q6DZSwhZSeZ7SKRwPtcyUrKy5SFYgpsHfqp5wRg== X-Received: by 2002:a05:6a20:cd0e:b0:1c0:eba5:e192 with SMTP id adf61e73a8af0-1cf5e0f52bcmr4279897637.27.1726039716399; Wed, 11 Sep 2024 00:28:36 -0700 (PDT) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:8398:fe34:eba2:f301]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71908fe4e7esm2399415b3a.80.2024.09.11.00.28.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Sep 2024 00:28:36 -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 v7 05/10] i2c: core: Remove extra space in Makefile Date: Wed, 11 Sep 2024 15:27:43 +0800 Message-ID: <20240911072751.365361-6-wenst@chromium.org> X-Mailer: git-send-email 2.46.0.598.g6f2099f65c-goog In-Reply-To: <20240911072751.365361-1-wenst@chromium.org> References: <20240911072751.365361-1-wenst@chromium.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Some lines in the Makefile have a space before tabs. Remove those. Reported-by: Andy Shevchenko Closes: https://lore.kernel.org/all/ZsdE0PxKnGRjzChl@smile.fi.intel.com/ Signed-off-by: Chen-Yu Tsai Reviewed-by: Andy Shevchenko Reviewed-by: Andi Shyti --- Changes since v6: - Collected Andy's Reviewed-by Changes since v5: - new patch --- drivers/i2c/Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/i2c/Makefile b/drivers/i2c/Makefile index 3f71ce4711e3..f12d6b10a85e 100644 --- a/drivers/i2c/Makefile +++ b/drivers/i2c/Makefile @@ -5,10 +5,10 @@ =20 obj-$(CONFIG_I2C_BOARDINFO) +=3D i2c-boardinfo.o obj-$(CONFIG_I2C) +=3D i2c-core.o -i2c-core-objs :=3D i2c-core-base.o i2c-core-smbus.o +i2c-core-objs :=3D i2c-core-base.o i2c-core-smbus.o i2c-core-$(CONFIG_ACPI) +=3D i2c-core-acpi.o -i2c-core-$(CONFIG_I2C_SLAVE) +=3D i2c-core-slave.o -i2c-core-$(CONFIG_OF) +=3D i2c-core-of.o +i2c-core-$(CONFIG_I2C_SLAVE) +=3D i2c-core-slave.o +i2c-core-$(CONFIG_OF) +=3D i2c-core-of.o =20 obj-$(CONFIG_I2C_SMBUS) +=3D i2c-smbus.o obj-$(CONFIG_I2C_CHARDEV) +=3D i2c-dev.o --=20 2.46.0.598.g6f2099f65c-goog From nobody Thu Sep 19 10:41:31 2024 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E16A513C67E for ; Wed, 11 Sep 2024 07:28:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726039722; cv=none; b=nPh6f0P55VM0dtXymOlo3jxxh7nEkD3kGb8ayO9gVPr/WKaikTWzPJ0UOW9xpsyAMAFpHUhq7B8AvxyO+Pz6ujqNXdiZqfjxAHh+iF3TwFJsD161ElcMAdEOF8ezKhIpZRaP0royn2YDZ6r4zgAt6aXlG+DT822Wew/2iHXPbzA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726039722; c=relaxed/simple; bh=rkpZP23OkLqpFxIiBdJX7RlIVZude6weDw4hvQOiuKs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=E4ykolBNWI2HDhmAcYXAJ/T+spuVLPvyqG3u4/hRHxMBjOXAkOhL9A1RJceCumwDayC2kwR16kTSUNhEEAt2sSBDpErL1ygb1HshVZ/uyIWHY99dpqKituMzKZjbusRKv4b/nqBSZ+wC4/LpDgjSk4+9J8SWTOAEm25kAPPzEpM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=jpgCg63U; arc=none smtp.client-ip=209.85.210.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="jpgCg63U" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-718da0821cbso4105081b3a.0 for ; Wed, 11 Sep 2024 00:28:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1726039720; x=1726644520; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YuuJ4IMp8f8RUPgIJJofk3lgpK2gw9f2hR5QsihKIF0=; b=jpgCg63UKOkMJMKHPkGfow6H7ds1KKf2VD5W+0bSGXZYiQsIJ7GLinzcTk6pGHadrF OBzhjiDawxpl6Oi25d6jB8wZfNmGDNmG4WSFJUucuWV9eQB7h94cwASPkp3T7RrIdAhX h+Y1DQurIpFkxTn/RK+DUOykS+0XtQnFJm0N4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726039720; x=1726644520; 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=YuuJ4IMp8f8RUPgIJJofk3lgpK2gw9f2hR5QsihKIF0=; b=mYUq+sQYkdr3SIY3A/Mqfi5DzO6lKAsCf5r3+o7GY5Z5yl98GemtWQx7BnJFUw9agX lzZ00S7lnc2svgJkIDnrdRbByGUuK/Z2N3WHcXQKj2TIgADdB4DeguO9xw1jMOu8YmT7 goPjQog9CSF39M4bzHppG4LeavRo9YztMBa8W4PH6U2u1dL3/6oBnKjF5aCDsQqIeFcX kgrXcKjlK+kWBwHE7OzM600xDK6/3pmz2cKopQ8X1wCohrhAznN8S7w29mbfMEUpAhZk A0nFsb7Mqgq1bSCBTCK4UiKPgEDV6v6KJTgWkjFE7lZcx7NowAh97P28ZD1pcjkMMfx2 NzeQ== X-Forwarded-Encrypted: i=1; AJvYcCXydXWKDY22w2390uJ/mTvi3RNdOkd2Wy0+rQEvOF5WJPAwXD6WhIZ2iuj+vsM+hpjxPNKWoA1QiU0K2v8=@vger.kernel.org X-Gm-Message-State: AOJu0YwyL/dE9rrG1aXqGrjO05OoEZxtLzZndkO1PMFET6Oo4shPH0j5 cwjNC5GUjzg689kETbewLxnxQT4vyg+iQqy0ZzoqTVKSeuO1ocSshvGOsNBWBg== X-Google-Smtp-Source: AGHT+IGnls1/wySnShGxcSfF4z8WnLnKNAl5aI9lwWWNHkRT83lWy5txFhO7utXp/CZOwDQ3OoJEgw== X-Received: by 2002:a05:6a00:1a87:b0:717:945e:effd with SMTP id d2e1a72fcca58-718e3f9dd53mr18901401b3a.1.1726039719906; Wed, 11 Sep 2024 00:28:39 -0700 (PDT) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:8398:fe34:eba2:f301]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71908fe4e7esm2399415b3a.80.2024.09.11.00.28.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Sep 2024 00:28:39 -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 v7 06/10] i2c: Introduce OF component probe function Date: Wed, 11 Sep 2024 15:27:44 +0800 Message-ID: <20240911072751.365361-7-wenst@chromium.org> X-Mailer: git-send-email 2.46.0.598.g6f2099f65c-goog In-Reply-To: <20240911072751.365361-1-wenst@chromium.org> References: <20240911072751.365361-1-wenst@chromium.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Some devices are designed and manufactured with some components having multiple drop-in replacement options. These components are often connected to the mainboard via ribbon cables, having the same signals and pin assignments across all options. These may include the display panel and touchscreen on laptops and tablets, and the trackpad on laptops. Sometimes which component option is used in a particular device can be detected by some firmware provided identifier, other times that information is not available, and the kernel has to try to probe each device. This change attempts to make the "probe each device" case cleaner. The current approach is to have all options added and enabled in the device tree. The kernel would then bind each device and run each driver's probe function. This works, but has been broken before due to the introduction of asynchronous probing, causing multiple instances requesting "shared" resources, such as pinmuxes, GPIO pins, interrupt lines, at the same time, with only one instance succeeding. Work arounds for these include moving the pinmux to the parent I2C controller, using GPIO hogs or pinmux settings to keep the GPIO pins in some fixed configuration, and requesting the interrupt line very late. Such configurations can be seen on the MT8183 Krane Chromebook tablets, and the Qualcomm sc8280xp-based Lenovo Thinkpad 13S. Instead of this delicate dance between drivers and device tree quirks, this change introduces a simple I2C component probe. function For a given class of devices on the same I2C bus, it will go through all of them, doing a simple I2C read transfer and see which one of them responds. It will then enable the device that responds. This requires some minor modifications in the existing device tree. The status for all the device nodes for the component options must be set to "failed-needs-probe". This makes it clear that some mechanism is needed to enable one of them, and also prevents the prober and device drivers running at the same time. Signed-off-by: Chen-Yu Tsai --- Changes since v6: - Correctly replaced for_each_child_of_node_scoped() with for_each_child_of_node_with_prefix() - Added namespace for exported symbol - Made the probe function a framework with hooks - Split out a new header file - Added MAINTAINERS entry - Reworded kernel-doc - Dropped usage of __free from i2c_of_probe_component() since error path cleanup is needed anyway Changes since v5: - Fixed indent in Makefile - Split regulator and GPIO TODO items - Reversed final conditional in i2c_of_probe_enable_node() Changes since v4: - Split code into helper functions - Use scoped helpers and __free() to reduce error path Changes since v3: - Complete kernel-doc - Return different error if I2C controller is disabled - Expand comment to explain assumptions and constraints - Split for-loop finding target node and operations on target node - Add missing i2c_put_adapter() - Move prober code to separate file Rob also asked why there was a limitation of "exactly one touchscreen will be enabled across the whole tree". The use case this prober currently targets is a component on consumer electronics (tablet or laptop) being swapped out due to cost or supply reasons. Designs with multiple components of the same type are pretty rare. The way the next patch is written also assumes this for efficiency reasons. Changes since v2: - New patch split out from "of: Introduce hardware prober driver" - Addressed Rob's comments - Move i2c prober to i2c subsystem - Use of_node_is_available() to check if node is enabled. - Use OF changeset API to update status property - Addressed Andy's comments - Probe function now accepts "struct device *dev" instead to reduce line length and dereferences - Move "ret =3D 0" to just before for_each_child_of_node(i2c_node, node) --- MAINTAINERS | 8 ++ drivers/i2c/Makefile | 1 + drivers/i2c/i2c-core-of-prober.c | 195 +++++++++++++++++++++++++++++++ include/linux/i2c-of-prober.h | 73 ++++++++++++ 4 files changed, 277 insertions(+) create mode 100644 drivers/i2c/i2c-core-of-prober.c create mode 100644 include/linux/i2c-of-prober.h diff --git a/MAINTAINERS b/MAINTAINERS index 927f81c12543..d88646380786 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -10645,6 +10645,14 @@ S: Maintained F: Documentation/devicetree/bindings/i2c/marvell,mv64xxx-i2c.yaml F: drivers/i2c/busses/i2c-mv64xxx.c =20 +I2C OF COMPONENT PROBER +M: Chen-Yu Tsai +L: linux-i2c@vger.kernel.org +L: devicetree@vger.kernel.org +S: Maintained +F: drivers/i2c/i2c-core-of-prober.c +F: include/linux-i2c-of-prober.h + I2C OVER PARALLEL PORT M: Jean Delvare L: linux-i2c@vger.kernel.org diff --git a/drivers/i2c/Makefile b/drivers/i2c/Makefile index f12d6b10a85e..c539cdc1e305 100644 --- a/drivers/i2c/Makefile +++ b/drivers/i2c/Makefile @@ -9,6 +9,7 @@ i2c-core-objs :=3D i2c-core-base.o i2c-core-smbus.o i2c-core-$(CONFIG_ACPI) +=3D i2c-core-acpi.o i2c-core-$(CONFIG_I2C_SLAVE) +=3D i2c-core-slave.o i2c-core-$(CONFIG_OF) +=3D i2c-core-of.o +i2c-core-$(CONFIG_OF_DYNAMIC) +=3D i2c-core-of-prober.o =20 obj-$(CONFIG_I2C_SMBUS) +=3D i2c-smbus.o obj-$(CONFIG_I2C_CHARDEV) +=3D i2c-dev.o diff --git a/drivers/i2c/i2c-core-of-prober.c b/drivers/i2c/i2c-core-of-pro= ber.c new file mode 100644 index 000000000000..62ff2f4b6177 --- /dev/null +++ b/drivers/i2c/i2c-core-of-prober.c @@ -0,0 +1,195 @@ +// 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 +#include + +/* + * Some devices, such as Google Hana Chromebooks, are produced by multiple + * vendors each using their preferred components. Such components are all + * in the device tree. Instead of having all of them enabled and having ea= ch + * driver separately try and probe its device while fighting over shared + * resources, they can be marked as "fail-needs-probe" and have a prober + * figure out which one is actually used beforehand. + * + * This prober assumes such drop-in parts are on the same I2C bus, have + * non-conflicting addresses, and can be directly probed by seeing which + * address responds. + * + * TODO: + * - Support handling common regulators. + * - Support handling common GPIOs. + * - Support I2C muxes + */ + +static struct device_node *i2c_of_probe_get_i2c_node(struct device *dev, c= onst char *type) +{ + struct device_node *node __free(device_node) =3D of_find_node_by_name(NUL= L, type); + if (!node) + return dev_err_ptr_probe(dev, -ENODEV, "Could not find %s device node\n"= , type); + + struct device_node *i2c_node __free(device_node) =3D of_get_parent(node); + if (!of_node_name_eq(i2c_node, "i2c")) + return dev_err_ptr_probe(dev, -EINVAL, "%s device isn't on I2C bus\n", t= ype); + + if (!of_device_is_available(i2c_node)) + return dev_err_ptr_probe(dev, -ENODEV, "I2C controller not available\n"); + + return no_free_ptr(i2c_node); +} + +static int i2c_of_probe_enable_node(struct device *dev, struct device_node= *node) +{ + int ret; + + dev_info(dev, "Enabling %pOF\n", node); + + struct of_changeset *ocs __free(kfree) =3D kzalloc(sizeof(*ocs), GFP_KERN= EL); + if (!ocs) + return -ENOMEM; + + of_changeset_init(ocs); + ret =3D of_changeset_update_prop_string(ocs, node, "status", "okay"); + if (ret) + return ret; + + ret =3D of_changeset_apply(ocs); + if (ret) { + /* ocs needs to be explicitly cleaned up before being freed. */ + of_changeset_destroy(ocs); + } else { + /* + * ocs is intentionally kept around as it needs to + * exist as long as the change is applied. + */ + void *ptr __always_unused =3D no_free_ptr(ocs); + } + + return ret; +} + +static const struct i2c_of_probe_ops i2c_of_probe_dummy_ops; + +/** + * i2c_of_probe_component() - probe for devices of "type" on the same i2c = bus + * @dev: Pointer to the &struct device of the caller, only used for dev_pr= intk() messages. + * @cfg: Pointer to the &struct i2c_of_probe_cfg containing callbacks and = other options + * for the prober. + * @ctx: Context data for callbacks. + * + * Probe for possible I2C components of the same "type" (&i2c_of_probe_cfg= ->type) + * on the same I2C bus that have their status marked as "fail". + * + * Assumes that across the entire device tree the only instances of nodes + * prefixed with "type" are the ones that need handling for second source + * components. In other words, if "type" is "touchscreen", then all device + * nodes named "touchscreen*" are the ones that need probing. There cannot + * be another "touchscreen" node that is already enabled. + * + * Assumes that for each "type" of component, only one actually exists. In + * other words, only one matching and existing device will be enabled. + * + * Context: Process context only. Does non-atomic I2C transfers. + * Should only be used from a driver probe function, as the funct= ion + * can return -EPROBE_DEFER if the I2C adapter or other resources + * are 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 struct i2c_of_probe_c= fg *cfg, void *ctx) +{ + const struct i2c_of_probe_ops *ops; + const char *type; + struct device_node *i2c_node; + struct i2c_adapter *i2c; + int ret; + + if (!cfg) + return -EINVAL; + + ops =3D cfg->ops ?: &i2c_of_probe_dummy_ops; + type =3D cfg->type; + + i2c_node =3D i2c_of_probe_get_i2c_node(dev, type); + if (IS_ERR(i2c_node)) + return PTR_ERR(i2c_node); + + for_each_child_of_node_with_prefix(i2c_node, node, type) { + 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. + */ + ret =3D 0; + goto out_put_i2c_node; + } + + i2c =3D of_get_i2c_adapter_by_node(i2c_node); + if (!i2c) { + ret =3D dev_err_probe(dev, -EPROBE_DEFER, "Couldn't get I2C adapter\n"); + goto out_put_i2c_node; + } + + /* Grab resources */ + ret =3D 0; + if (ops->get_resources) + ret =3D ops->get_resources(dev, i2c_node, ctx); + if (ret) + goto out_put_i2c_adapter; + + /* Enable resources */ + if (ops->enable) + ret =3D ops->enable(dev, ctx); + if (ret) + goto out_release_resources; + + ret =3D 0; + for_each_child_of_node_with_prefix(i2c_node, node, type) { + union i2c_smbus_data data; + u32 addr; + + if (of_property_read_u32(node, "reg", &addr)) + continue; + if (i2c_smbus_xfer(i2c, addr, 0, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dat= a) < 0) + continue; + + /* Found a device that is responding */ + if (ops->free_resources_early) + ops->free_resources_early(ctx); + ret =3D i2c_of_probe_enable_node(dev, node); + break; + } + + if (ops->cleanup) + ops->cleanup(dev, ctx); +out_release_resources: + if (ops->free_resources_late) + ops->free_resources_late(ctx); +out_put_i2c_adapter: + i2c_put_adapter(i2c); +out_put_i2c_node: + of_node_put(i2c_node); + + return ret; +} +EXPORT_SYMBOL_NS_GPL(i2c_of_probe_component, I2C_OF_PROBER); diff --git a/include/linux/i2c-of-prober.h b/include/linux/i2c-of-prober.h new file mode 100644 index 000000000000..0f94e7c94310 --- /dev/null +++ b/include/linux/i2c-of-prober.h @@ -0,0 +1,73 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * i2c-of-prober.h - definitions for the Linux I2C OF component prober + * + * Copyright (C) 2024 Google LLC + */ + +#ifndef _LINUX_I2C_OF_PROBER_H +#define _LINUX_I2C_OF_PROBER_H + +#if IS_ENABLED(CONFIG_OF_DYNAMIC) + +struct device; +struct device_node; + +/** + * struct i2c_of_probe_ops - I2C OF component prober callbacks + * + * A set of callbacks to be used by i2c_of_probe_component(). + * + * All callbacks are optional. Callbacks are called only once per run, and= are + * used in the order they are defined in this structure. + * + * All callbacks that have return values shall return %0 on success, + * or a negative error number on failure. + * + * The @dev parameter passed to the callbacks is the same as @dev passed to + * i2c_of_probe_component(). It should only be used for dev_printk() calls + * and nothing else, especially not managed device resource (devres) APIs. + */ +struct i2c_of_probe_ops { + /** @get_resources: Retrieve resources for components. */ + int (*get_resources)(struct device *dev, struct device_node *bus_node, vo= id *data); + + /** @free_resources_early: Release exclusive resources prior to enabling = component. */ + void (*free_resources_early)(void *data); + + /** + * @enable: Enable resources so that the components respond to probes. + * + * Resources should be reverted to their initial state before returning i= f this fails. + */ + int (*enable)(struct device *dev, void *data); + + /** + * @cleanup: Opposite of @enable to balance refcounts after probing. + * + * Can not operate on resources already freed in @free_resources_early. + */ + int (*cleanup)(struct device *dev, void *data); + + /** + * @free_resources_late: Release all resources, including those that woul= d have + * been released by @free_resources_early. + */ + void (*free_resources_late)(void *data); +}; + +/** + * struct i2c_of_probe_cfg - I2C OF component prober configuration + * @ops: Callbacks for the prober to use. + * @type: A string to match the device node name prefix to probe for. + */ +struct i2c_of_probe_cfg { + const struct i2c_of_probe_ops *ops; + const char *type; +}; + +int i2c_of_probe_component(struct device *dev, const struct i2c_of_probe_c= fg *cfg, void *ctx); + +#endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ + +#endif /* _LINUX_I2C_OF_PROBER_H */ --=20 2.46.0.598.g6f2099f65c-goog From nobody Thu Sep 19 10:41:31 2024 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1DC5113D524 for ; Wed, 11 Sep 2024 07:28:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726039725; cv=none; b=KskvSLzCHCj8+15N8MIxnAIv/UIP955/nMmCv9iqwYjY0D+Qgt17WUc3ZbspHDXcx9w4xBBkpZROR4I6vu9hYkheA72HHEBD94cWleSHny1SduXQL4rQ4/wzjP4D82SCE3XCfvXscRdkGkna2Kh8bwomqosTgdJ9MrGXqkDATHw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726039725; c=relaxed/simple; bh=0m7VQ2QI0ApKlNRhuiGh+mRGNYI/bK3s4kf7/NXjOx0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jFvr0EX7Fn0re85OXz5Nz5N0kJSBJ9ExaGyiPA0JunfvjtIFpA0vYS7RyczRmEv817tzVIHK1LvlYYMEJpAXAexqExERWCVRo7hjJB1/fEIJNVYosYKnN7QXsjVr7tneB0DsiEFPr8nJvF+TURNLXP8JVXjfk5RHURsy1aknesU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=jF64Q5iu; arc=none smtp.client-ip=209.85.210.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="jF64Q5iu" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-718d6ad6050so4036455b3a.0 for ; Wed, 11 Sep 2024 00:28:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1726039723; x=1726644523; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=fVhbTxLURsXagkjV6T5t8snpx3JbKq5qtkesVJ+9OU0=; b=jF64Q5iuQL3jflr+BWwhswyENLKkQFBaYYRqi3bgi1WLjVWSitliwl15I5ItTfHaVs PMK9PoXiDpfC0Sih13tTiHB28t39NrTIkYY9eYDpES1Lu8Th/33AozAUQOewWxmerEod l1tzWIP+AXVqnTJmVz8S4TRS0qg8iov8fMiCw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726039723; x=1726644523; 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=fVhbTxLURsXagkjV6T5t8snpx3JbKq5qtkesVJ+9OU0=; b=IF31b93KwGkWKjeCIIJnxdLxiHdmfsdT7LTaM9F5sSBi9j8DXwOVsaFQ0QnHTgM7ON RXsLAdAaOdRHWE04p8IYD/tu2U2HKQguhFysm3g9FT13wGcBWB5huOFKIFHCulXqwSup G2AuIy5O8+Ekn5PSf6mhOoX2BmtQHKFt2a1bF+6qXPdoEoHAeJzyJa5JI4Gwc+mARi2r PX3cI1q6y/VeVmSgKsNaa3VwvDtMkmAa35qRbSbRxxRNcOVRu5HxgrD+xDl66Ubd5quO mxdFEcFq/l+aFNKr2ilPVXlj+UuPK29rwQZYXmOmxszZfnoOVIPUDctszUZxR5gMxWpR S05A== X-Forwarded-Encrypted: i=1; AJvYcCWOh+xj1FHIvvhEPtxu9pKFEVpHxz5ivzJ5FiubWXcysOhUIk6PMEqLcyf27W1yta0XPcO+Qj0XFwTOf7k=@vger.kernel.org X-Gm-Message-State: AOJu0YzeuhaHjbDMRUBUFosuwAo3apbaQBUS7ReiInwuhpdyoBCf3vby S8IdBR/cBMJdBgm+yQkTOutKpCez2M2ZZzmYc6ZU9/K2CgBm1YXE4SzMxFYsgw== X-Google-Smtp-Source: AGHT+IHJHP/sS0c8oI4sQVmWMgfIt7DzJlolRs9q3i3C6TrZ9TAHcWiYmt8L4UpiwKLjhWZ/rnLTPA== X-Received: by 2002:a05:6a20:cf90:b0:1cf:5437:e76f with SMTP id adf61e73a8af0-1cf5e098306mr4519928637.4.1726039723417; Wed, 11 Sep 2024 00:28:43 -0700 (PDT) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:8398:fe34:eba2:f301]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71908fe4e7esm2399415b3a.80.2024.09.11.00.28.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Sep 2024 00:28:43 -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 v7 07/10] i2c: of-prober: Add simple helpers for regulator support Date: Wed, 11 Sep 2024 15:27:45 +0800 Message-ID: <20240911072751.365361-8-wenst@chromium.org> X-Mailer: git-send-email 2.46.0.598.g6f2099f65c-goog In-Reply-To: <20240911072751.365361-1-wenst@chromium.org> References: <20240911072751.365361-1-wenst@chromium.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add helpers to do regulator management for 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. The assumption is that the same class of components to be probed are always connected in the same fashion with the same regulator supply and GPIO. The names may vary due to binding differences, but the physical layout does not change. This set of helpers supports at most one regulator supply. The user must specify the node from which the supply is retrieved. The supply name and the amount of time to wait after the supply is enabled are also given by the user. Signed-off-by: Chen-Yu Tsai --- Changes since v6: - Moved change of of_get_next_child_scoped() to of_get_next_child_with_prefix() to previous patch - Restructured into helpers for the I2C OF component prober - Reduced to only handle one regulator - Commit message updated Changes since v5: - Split of_regulator_bulk_get_all() return value check and explain "ret =3D=3D 0" case - Switched to of_get_next_child_with_prefix_scoped() where applicable - Used krealloc_array() instead of directly calculating size - copy whole regulator array in one memcpy() call - Drop "0" from struct zeroing initializer - Split out regulator helper from i2c_of_probe_enable_res() to keep code cleaner when combined with the next patch - Added options for customizing power sequencing delay - Rename i2c_of_probe_get_regulator() to i2c_of_probe_get_regulators() - Add i2c_of_probe_free_regulator() helper 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 | 169 ++++++++++++++++++++++++++++++- include/linux/i2c-of-prober.h | 48 +++++++++ 2 files changed, 216 insertions(+), 1 deletion(-) diff --git a/drivers/i2c/i2c-core-of-prober.c b/drivers/i2c/i2c-core-of-pro= ber.c index 62ff2f4b6177..1371ea565556 100644 --- a/drivers/i2c/i2c-core-of-prober.c +++ b/drivers/i2c/i2c-core-of-prober.c @@ -6,6 +6,7 @@ */ =20 #include +#include #include #include #include @@ -13,6 +14,7 @@ #include #include #include +#include #include =20 /* @@ -28,7 +30,6 @@ * address responds. * * TODO: - * - Support handling common regulators. * - Support handling common GPIOs. * - Support I2C muxes */ @@ -193,3 +194,169 @@ int i2c_of_probe_component(struct device *dev, const = struct i2c_of_probe_cfg *cf return ret; } EXPORT_SYMBOL_NS_GPL(i2c_of_probe_component, I2C_OF_PROBER); + +static int i2c_of_probe_simple_get_supply(struct device *dev, struct devic= e_node *node, + struct i2c_of_probe_simple_ctx *ctx) +{ + const char *supply_name; + struct regulator *supply; + + /* + * It's entirely possible for the component's device node to not have reg= ulator + * supplies. While it does not make sense from a hardware perspective, the + * supplies could be always on or otherwise not modeled in the device tre= e, but + * the device would still work. + */ + supply_name =3D ctx->opts->supply_name; + if (!supply_name) + return 0; + + supply =3D of_regulator_get_optional(dev, node, supply_name); + if (IS_ERR(supply)) { + return dev_err_probe(dev, PTR_ERR(supply), + "Failed to get regulator supply \"%s\" from %pOF\n", + supply_name, node); + } + + ctx->supply =3D supply; + + return 0; +} + +static void i2c_of_probe_simple_put_supply(struct i2c_of_probe_simple_ctx = *ctx) +{ + regulator_put(ctx->supply); + ctx->supply =3D NULL; +} + +static int i2c_of_probe_simple_enable_regulator(struct device *dev, struct= i2c_of_probe_simple_ctx *ctx) +{ + int ret; + + if (!ctx->supply) + return 0; + + dev_dbg(dev, "Enabling regulator supply \"%s\"\n", ctx->opts->supply_name= ); + + ret =3D regulator_enable(ctx->supply); + if (ret) + return ret; + + msleep(ctx->opts->post_power_on_delay_ms); + + return 0; +} + +static void i2c_of_probe_simple_disable_regulator(struct device *dev, stru= ct i2c_of_probe_simple_ctx *ctx) +{ + if (!ctx->supply) + return; + + dev_dbg(dev, "Disabling regulator supply \"%s\"\n", ctx->opts->supply_nam= e); + + regulator_disable(ctx->supply); +} + +/** + * i2c_of_probe_simple_get_res - Simple helper for I2C OF prober to get re= sources + * @dev: Pointer to the &struct device of the caller, only used for dev_pr= intk() messages + * @bus_node: Pointer to the &struct device_node of the I2C adapter. + * @data: Pointer to &struct i2c_of_probe_simple_ctx helper context. + * + * If &i2c_of_probe_simple_opts->supply_name is given, request the named r= egulator supply. + * + * Return: %0 on success or no-op, or a negative error number on failure. + */ +int i2c_of_probe_simple_get_res(struct device *dev, struct device_node *bu= s_node, void *data) +{ + struct i2c_of_probe_simple_ctx *ctx =3D data; + struct device_node *node; + const char *compat; + int ret; + + dev_dbg(dev, "Requesting resources for components under I2C bus %pOF\n", = bus_node); + + if (!ctx || !ctx->opts) + return -EINVAL; + + compat =3D ctx->opts->res_node_compatible; + if (!compat) + return -EINVAL; + + node =3D of_get_compatible_child(bus_node, compat); + if (!node) + return dev_err_probe(dev, -ENODEV, "No device compatible with \"%s\" fou= nd\n", + compat); + + ret =3D i2c_of_probe_simple_get_supply(dev, node, ctx); + if (ret) + goto out_put_node; + + return 0; + +out_put_node: + of_node_put(node); + return ret; +} +EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_get_res, I2C_OF_PROBER); + +/** + * i2c_of_probe_simple_free_res_late - Simple helper for I2C OF prober to = release all resources. + * @data: Pointer to &struct i2c_of_probe_simple_ctx helper context. + */ +void i2c_of_probe_simple_free_res_late(void *data) +{ + struct i2c_of_probe_simple_ctx *ctx =3D data; + + i2c_of_probe_simple_put_supply(ctx); +} +EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_free_res_late, I2C_OF_PROBER); + +/** + * i2c_of_probe_simple_enable - Enable resources for I2C OF prober simple = helpers + * @dev: Pointer to the &struct device of the caller, only used for dev_pr= intk() messages + * @data: Pointer to &struct i2c_of_probe_simple_ctx helper context. + * + * If a regulator supply was found, enable that regulator. + * + * Return: %0 on success or no-op, or a negative error number on failure. + */ +int i2c_of_probe_simple_enable(struct device *dev, void *data) +{ + struct i2c_of_probe_simple_ctx *ctx =3D data; + int ret; + + ret =3D i2c_of_probe_simple_enable_regulator(dev, ctx); + if (ret) + return ret; + + return 0; +} +EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_enable, I2C_OF_PROBER); + +/** + * i2c_of_probe_simple_cleanup - Clean up resources for I2C OF prober simp= le helpers + * @dev: Pointer to the &struct device of the caller, only used for dev_pr= intk() messages + * @data: Pointer to &struct i2c_of_probe_simple_ctx helper context. + * + * If a regulator supply was found, disable that regulator. + * + * Return: %0 on success or no-op, or a negative error number on failure. + */ +int i2c_of_probe_simple_cleanup(struct device *dev, void *data) +{ + struct i2c_of_probe_simple_ctx *ctx =3D data; + + i2c_of_probe_simple_disable_regulator(dev, ctx); + + return 0; +} +EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_cleanup, I2C_OF_PROBER); + +struct i2c_of_probe_ops i2c_of_probe_simple_ops =3D { + .get_resources =3D i2c_of_probe_simple_get_res, + .enable =3D i2c_of_probe_simple_enable, + .cleanup =3D i2c_of_probe_simple_cleanup, + .free_resources_late =3D i2c_of_probe_simple_free_res_late, +}; +EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_ops, I2C_OF_PROBER); diff --git a/include/linux/i2c-of-prober.h b/include/linux/i2c-of-prober.h index 0f94e7c94310..541451fbf58d 100644 --- a/include/linux/i2c-of-prober.h +++ b/include/linux/i2c-of-prober.h @@ -68,6 +68,54 @@ struct i2c_of_probe_cfg { =20 int i2c_of_probe_component(struct device *dev, const struct i2c_of_probe_c= fg *cfg, void *ctx); =20 +/** + * DOC: I2C OF component prober simple helpers + * + * Components such as trackpads are commonly connected to a devices basebo= ard + * with a 6-pin ribbon cable. That gives at most one voltage supply and one + * GPIO besides the I2C bus, interrupt pin, and common ground. Touchscreen= s, + * while integrated into the display panel's connection, typically have the + * same set of connections. + * + * A simple set of helpers are provided here for use with the I2C OF compo= nent + * prober. This implementation targets such components, allowing for at mo= st + * one regulator supply. + * + * The following helpers are provided: + * * i2c_of_probe_simple_get_res() + * * i2c_of_probe_simple_free_res_late() + * * i2c_of_probe_simple_enable() + * * i2c_of_probe_simple_cleanup() + */ + +/** + * struct i2c_of_probe_simple_opts - Options for simple I2C component prob= er callbacks + * @res_node_compatible: Compatible string of device node to retrieve reso= urces from. + * @supply_name: Name of regulator supply. + * @post_power_on_delay_ms: Delay in ms after regulators are powered on. P= assed to msleep(). + */ +struct i2c_of_probe_simple_opts { + const char *res_node_compatible; + const char *supply_name; + unsigned int post_power_on_delay_ms; +}; + +struct regulator; + +struct i2c_of_probe_simple_ctx { + /* public: provided by user before helpers are used. */ + const struct i2c_of_probe_simple_opts *opts; + /* private: internal fields for helpers. */ + struct regulator *supply; +}; + +int i2c_of_probe_simple_get_res(struct device *dev, struct device_node *bu= s_node, void *data); +void i2c_of_probe_simple_free_res_late(void *data); +int i2c_of_probe_simple_enable(struct device *dev, void *data); +int i2c_of_probe_simple_cleanup(struct device *dev, void *data); + +extern struct i2c_of_probe_ops i2c_of_probe_simple_ops; + #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ =20 #endif /* _LINUX_I2C_OF_PROBER_H */ --=20 2.46.0.598.g6f2099f65c-goog From nobody Thu Sep 19 10:41:31 2024 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D420915445D for ; Wed, 11 Sep 2024 07:28:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726039729; cv=none; b=nTbBPLArbMuiL3SsnVOQYFJMKQJZGGm8v7rnWzcKzBROje/YToULeAGm9oaGqdhxagQi3F18fkQdUpy/bV/pgMsQ+driB7Y/SrbeSdWoaISVfeOtkYuMBdh9uyAEHFmem+04feQWPHIryTTEUY39HuqX2keOLk9JAbktWiz1g7M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726039729; c=relaxed/simple; bh=FSH8K/H/drkMAVejnP3Xi5N5diDIwjBm8OH7GEvV/sE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NmndqCt7du8duivva9WKJKMWOhQNkAT1JK7L5gByKoFNeqU+H5PWhlfoyHwvzIGy7Sn2IZRrv6v8VhtHfFGvyKJ+PmdZF4OyiamzLqoYIRMQ6aCO3qBzlC2vWYpZm+7Sq82T8soi94YboBjbyRy6CbJxOkIlrWBFvmdq6zuAGns= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=Tus4rg+f; arc=none smtp.client-ip=209.85.210.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="Tus4rg+f" Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-718e285544fso2991582b3a.1 for ; Wed, 11 Sep 2024 00:28:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1726039727; x=1726644527; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=RNFQ0+k6gl1kkh3o3uKf0ZEr04CBiZ9w1gp6dXSYFKk=; b=Tus4rg+fWJ1kcN8htSdh6MATbUgA17hN57sxfgQSPY7EtmC6CfCbo5NZ7N3ATrxvXA 7oWPTCWOaEvX9UKHykMMMBX/LVjKoJp0roANw0Dr83l29VgzzxtV/aoTYZ9U7c8JBoKq /esHsWzsROkeZwKGyLgAq7awKzw0uF03eZpgo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726039727; x=1726644527; 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=RNFQ0+k6gl1kkh3o3uKf0ZEr04CBiZ9w1gp6dXSYFKk=; b=rDw7YlivYn+qpTSL4DoJrxv4ePrL0sq4F5kJ04BWpsqOYPh/dq5rsTsvxIV1fyzt7B DvNy+Hs6ME8HsD3brtsJjGexLwY4r18g6skC0Btp91QVrriTEbCnduGYTinUkEubkkpe qKybys6kqWIK4cm0yL5ntdqquuMHBI2M1EbQNc+VgWllhO+64jSLSKtUoEHiHhiy7sH+ cqw6TCSTwJhg23eqiKokb5nwF6Xe3sGhHrjyZx4kajb8nZafBRj+P/RurapxGUKMC2LR EQr4LgAMN01UErQ3/N3/g9/GrrrxYC8hXbJuF/C36QziZmA2IeOhn1ENXHditDfXJaeR XF5Q== X-Forwarded-Encrypted: i=1; AJvYcCWPYCw+01iJL0BwFf2mFo8ljKkkIpcwvBHxVz/xErjtqJfajJde2cNwJfeffwQ628amRqkVS5+fKynzddA=@vger.kernel.org X-Gm-Message-State: AOJu0YxCZ54F+/j4nON52dW+ZLNoHMLJGNQbL/IzD1YugOjsU4UMEjsc YmOROlBrkHPNDiZNu03g6A/RFaz8iKFvXsqsoweAic/776Cbj70hH1zJ5Q0Xtw== X-Google-Smtp-Source: AGHT+IFFL6/Rj4Gwyc1YxeZ/hABvvBsCQgvzC/UjAY926OKBN27PE9HKTGhU9CwzQureNFH/FVgdIQ== X-Received: by 2002:a05:6a00:1254:b0:70e:8070:f9d0 with SMTP id d2e1a72fcca58-718d5e16641mr18401634b3a.9.1726039726903; Wed, 11 Sep 2024 00:28:46 -0700 (PDT) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:8398:fe34:eba2:f301]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71908fe4e7esm2399415b3a.80.2024.09.11.00.28.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Sep 2024 00:28:46 -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 v7 08/10] i2c: of-prober: Add GPIO support to simple helpers Date: Wed, 11 Sep 2024 15:27:46 +0800 Message-ID: <20240911072751.365361-9-wenst@chromium.org> X-Mailer: git-send-email 2.46.0.598.g6f2099f65c-goog In-Reply-To: <20240911072751.365361-1-wenst@chromium.org> References: <20240911072751.365361-1-wenst@chromium.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add GPIO support to the simple helpers for 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 supplies were handled in the previous patch. The assumption is that the same class of components to be probed are always connected in the same fashion with the same regulator supply and GPIO. The names may vary due to binding differences, but the physical layout does not change. This supports at most one GPIO pin. The user must specify the GPIO name, the polarity, and the amount of time to wait after the GPIO is toggled. Devices with more than one GPIO pin likely require specific power sequencing beyond what generic code can easily support. Signed-off-by: Chen-Yu Tsai --- Changes since v6: - Restructured into helpers for the I2C OF component prober - Reduced to only handle one GPIO - Set GPIO to input on (failure) cleanup - Updated commit message Changes since v5: - Renamed "con" to "propname" in i2c_of_probe_get_gpiod() - Copy string first and check return value of strscpy() for overflow in i2c_of_probe_get_gpiod() - Add parenthesis around "enable" and "reset" GPIO names in comments - Split resource count debug message into two separate lines - Split out GPIO helper from i2c_of_probe_enable_res() to keep code cleaner following the previous patch - Adopted options for customizing power sequencing delay following previous patch 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 | 95 +++++++++++++++++++++++++++++++- include/linux/i2c-of-prober.h | 10 ++++ 2 files changed, 104 insertions(+), 1 deletion(-) diff --git a/drivers/i2c/i2c-core-of-prober.c b/drivers/i2c/i2c-core-of-pro= ber.c index 1371ea565556..6794ec749882 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 @@ -30,7 +31,6 @@ * address responds. * * TODO: - * - Support handling common GPIOs. * - Support I2C muxes */ =20 @@ -257,6 +257,64 @@ static void i2c_of_probe_simple_disable_regulator(stru= ct device *dev, struct i2c regulator_disable(ctx->supply); } =20 +static int i2c_of_probe_simple_get_gpiod(struct device *dev, struct device= _node *node, + struct i2c_of_probe_simple_ctx *ctx) +{ + struct fwnode_handle *fwnode =3D of_fwnode_handle(node); + struct gpio_desc *gpiod; + const char *con_id =3D NULL; + + /* NULL signals no GPIO needed */ + if (!ctx->opts->gpio_name) + return 0; + + /* An empty string signals an unnamed GPIO */ + if (strlen(ctx->opts->gpio_name)) + con_id =3D ctx->opts->gpio_name; + + gpiod =3D fwnode_gpiod_get_index(fwnode, con_id, 0, GPIOD_ASIS, "i2c-of-p= rober"); + if (IS_ERR(gpiod)) + return PTR_ERR(gpiod); + + ctx->gpiod =3D gpiod; + + return 0; +} + +static void i2c_of_probe_simple_put_gpiod(struct i2c_of_probe_simple_ctx *= ctx) +{ + gpiod_put(ctx->gpiod); + ctx->gpiod =3D NULL; +} + +static int i2c_of_probe_simple_set_gpio(struct device *dev, struct i2c_of_= probe_simple_ctx *ctx) +{ + int ret; + + if (!ctx->gpiod) + return 0; + + dev_dbg(dev, "Setting GPIO\n"); + + ret =3D gpiod_direction_output_raw(ctx->gpiod, ctx->opts->gpio_high_to_en= able ? 1 : 0); + if (ret) + return ret; + + msleep(ctx->opts->post_reset_deassert_delay_ms); + + return 0; +} + +static void i2c_of_probe_simple_disable_gpio(struct device *dev, struct i2= c_of_probe_simple_ctx *ctx) +{ + if (!ctx->gpiod) + return; + + dev_dbg(dev, "Setting GPIO to input\n"); + + gpiod_direction_input(ctx->gpiod); +} + /** * i2c_of_probe_simple_get_res - Simple helper for I2C OF prober to get re= sources * @dev: Pointer to the &struct device of the caller, only used for dev_pr= intk() messages @@ -264,6 +322,8 @@ static void i2c_of_probe_simple_disable_regulator(struc= t device *dev, struct i2c * @data: Pointer to &struct i2c_of_probe_simple_ctx helper context. * * If &i2c_of_probe_simple_opts->supply_name is given, request the named r= egulator supply. + * If &i2c_of_probe_simple_opts->gpio_name is given, request the named GPI= O. Or if it is + * the empty string, request the unnamed GPIO. * * Return: %0 on success or no-op, or a negative error number on failure. */ @@ -292,14 +352,36 @@ int i2c_of_probe_simple_get_res(struct device *dev, s= truct device_node *bus_node if (ret) goto out_put_node; =20 + ret =3D i2c_of_probe_simple_get_gpiod(dev, node, ctx); + if (ret) + goto out_put_supply; + return 0; =20 +out_put_supply: + i2c_of_probe_simple_put_supply(ctx); out_put_node: of_node_put(node); return ret; } EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_get_res, I2C_OF_PROBER); =20 +/** + * i2c_of_probe_simple_free_res_early - \ + * Simple helper for I2C OF prober to release GPIOs before component is en= abled + * @data: Pointer to &struct i2c_of_probe_simple_ctx helper context. + * + * GPIO descriptors are exclusive and have to be released before the + * actual driver probes so that the latter can acquire them. + */ +void i2c_of_probe_simple_free_res_early(void *data) +{ + struct i2c_of_probe_simple_ctx *ctx =3D data; + + i2c_of_probe_simple_put_gpiod(ctx); +} +EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_free_res_early, I2C_OF_PROBER); + /** * i2c_of_probe_simple_free_res_late - Simple helper for I2C OF prober to = release all resources. * @data: Pointer to &struct i2c_of_probe_simple_ctx helper context. @@ -308,6 +390,7 @@ void i2c_of_probe_simple_free_res_late(void *data) { struct i2c_of_probe_simple_ctx *ctx =3D data; =20 + i2c_of_probe_simple_put_gpiod(ctx); i2c_of_probe_simple_put_supply(ctx); } EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_free_res_late, I2C_OF_PROBER); @@ -330,7 +413,15 @@ int i2c_of_probe_simple_enable(struct device *dev, voi= d *data) if (ret) return ret; =20 + ret =3D i2c_of_probe_simple_set_gpio(dev, ctx); + if (ret) + goto err; + return 0; + +err: + i2c_of_probe_simple_disable_regulator(dev, ctx); + return ret; } EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_enable, I2C_OF_PROBER); =20 @@ -347,6 +438,7 @@ int i2c_of_probe_simple_cleanup(struct device *dev, voi= d *data) { struct i2c_of_probe_simple_ctx *ctx =3D data; =20 + i2c_of_probe_simple_disable_gpio(dev, ctx); i2c_of_probe_simple_disable_regulator(dev, ctx); =20 return 0; @@ -355,6 +447,7 @@ EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_cleanup, I2C_O= F_PROBER); =20 struct i2c_of_probe_ops i2c_of_probe_simple_ops =3D { .get_resources =3D i2c_of_probe_simple_get_res, + .free_resources_early =3D i2c_of_probe_simple_free_res_early, .enable =3D i2c_of_probe_simple_enable, .cleanup =3D i2c_of_probe_simple_cleanup, .free_resources_late =3D i2c_of_probe_simple_free_res_late, diff --git a/include/linux/i2c-of-prober.h b/include/linux/i2c-of-prober.h index 541451fbf58d..c5e241163c94 100644 --- a/include/linux/i2c-of-prober.h +++ b/include/linux/i2c-of-prober.h @@ -83,6 +83,7 @@ int i2c_of_probe_component(struct device *dev, const stru= ct i2c_of_probe_cfg *cf * * The following helpers are provided: * * i2c_of_probe_simple_get_res() + * * i2c_of_probe_simple_free_res_early() * * i2c_of_probe_simple_free_res_late() * * i2c_of_probe_simple_enable() * * i2c_of_probe_simple_cleanup() @@ -92,24 +93,33 @@ int i2c_of_probe_component(struct device *dev, const st= ruct i2c_of_probe_cfg *cf * struct i2c_of_probe_simple_opts - Options for simple I2C component prob= er callbacks * @res_node_compatible: Compatible string of device node to retrieve reso= urces from. * @supply_name: Name of regulator supply. + * @gpio_name: Name of GPIO. * @post_power_on_delay_ms: Delay in ms after regulators are powered on. P= assed to msleep(). + * @post_reset_deassert_delay_ms: Delay in ms after GPIOs are set. Passed = to msleep(). + * @gpio_high_to_enable: %true if GPIO should be set to electrical high to= enable component. */ struct i2c_of_probe_simple_opts { const char *res_node_compatible; const char *supply_name; + const char *gpio_name; unsigned int post_power_on_delay_ms; + unsigned int post_reset_deassert_delay_ms; + bool gpio_high_to_enable; }; =20 struct regulator; +struct gpio_desc; =20 struct i2c_of_probe_simple_ctx { /* public: provided by user before helpers are used. */ const struct i2c_of_probe_simple_opts *opts; /* private: internal fields for helpers. */ struct regulator *supply; + struct gpio_desc *gpiod; }; =20 int i2c_of_probe_simple_get_res(struct device *dev, struct device_node *bu= s_node, void *data); +void i2c_of_probe_simple_free_res_early(void *data); void i2c_of_probe_simple_free_res_late(void *data); int i2c_of_probe_simple_enable(struct device *dev, void *data); int i2c_of_probe_simple_cleanup(struct device *dev, void *data); --=20 2.46.0.598.g6f2099f65c-goog From nobody Thu Sep 19 10:41:31 2024 Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1B44B154C03 for ; Wed, 11 Sep 2024 07:28:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726039732; cv=none; b=K4O6tVPd0lfh0C57IqIxqByXaqM2NPiLst9KpbuKJ6ZgS3uEMKflW7DIZPKaysfCcNXE8kHaQsH1i+95sBxwbMKaBCl2VioAis3h6fs81tJlBR+JYbkpeNJo9BbnH+1rWHQ6C6MOARuuyc+QmMJRJBVx5UhHL2KBfJlJvwRh/1I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726039732; c=relaxed/simple; bh=UnatgJSa9mULbZAmp0Q2lcoBK9WK0uHukrvur3A+0P0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Qj6NtueRhY15q8frp2luMTYUJ8uMF0e5LeCX3al7KzzymPGv+TO8Pt8TRwhp4iEw2qLzc8MSvc5txU7wKkncDzD7sn1V/BCzF6rWhzYW0cOwRZCp166+gfJ7r6ML/XM05TiMlDd32oc644xbk26pDIZGiiHcwaxk8TwHXOi81g4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=dGoqrUX0; arc=none smtp.client-ip=209.85.215.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="dGoqrUX0" Received: by mail-pg1-f179.google.com with SMTP id 41be03b00d2f7-7d916b6a73aso1982114a12.1 for ; Wed, 11 Sep 2024 00:28:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1726039730; x=1726644530; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=fr9yV4HGWVMIrGkx9ydigLiwJCnrmGer3in2e1w4xKk=; b=dGoqrUX0N81e0+gU0Guty/SoJ9JWMMkH+85bu+Jjh9zDhfNjyPK3jmaW5mTGz93xv9 g/WW8e19aDgYgOSipFfKYp3WVKT6CJMr3pt68JGHduuY0mbbUD1QyuhYd3QJtvhi8hc1 73BrgsHIGm48Fb9xfa8o+Mbn/M1RI9no/R5XY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726039730; x=1726644530; 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=fr9yV4HGWVMIrGkx9ydigLiwJCnrmGer3in2e1w4xKk=; b=HX60WVmykcaAj65ndKwLMDs+Xbt2d2Z4gNHSOqP1/GOuqLrh0+Rx3HJAxDSvk0YlUb PsMxWGfBZJtKdcxSAYq9JPhPP6yc3nA3SW78eBEl8LNNhv6ZXT3odZbdafkNej51rGjN aPLvwW1FBbVlRd5DvYlCbZ2lXQtgy/TQE4oryEUuQ382z2oEVAAR7l4weIfXViEQH6eb LHOduXPpipWHvF5/Y5TfIyTU7LaBC5xiwf0Qpiquxg0D00FBHXLnAZdMObS9xrPJeL8/ t0enDEKgPxwt8GNOeRQflC3cApuOv6Tz0XYUJVgVo/Yb2IxcnMwjLNuqgnUuFPitlems +cfQ== X-Forwarded-Encrypted: i=1; AJvYcCXilY+Sf5v/SMtWufUdSeqYKJ11rcsKXfzk5Kv7XSace+DnPJ4oUNRccG5j56rxjzYW+bvwpAdIG87wsUg=@vger.kernel.org X-Gm-Message-State: AOJu0YyR4L2AHyZxh8DksNWQYQI0TMKCi9/9gSgy4Y+WhZvtiiGhJFKC 6Ft0+1Um8+6DD3wPlc3d6TI+bhWjUP0cWF9CGPnCqJB3Ac1U5tFbFqMqkNAljKnUvT3HHSeNOL8 = X-Google-Smtp-Source: AGHT+IEzZ5LA89X7I7aF3pUCsQe4p61WBrpC4obNNheNiGy0SPXWE2Q7sI4QYrZczPaRm66JSqQ2zw== X-Received: by 2002:a05:6a21:4a4b:b0:1cf:2df6:453f with SMTP id adf61e73a8af0-1cf5de0ef4cmr5867206637.0.1726039730361; Wed, 11 Sep 2024 00:28:50 -0700 (PDT) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:8398:fe34:eba2:f301]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71908fe4e7esm2399415b3a.80.2024.09.11.00.28.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Sep 2024 00:28:50 -0700 (PDT) From: Chen-Yu Tsai To: Rob Herring , Saravana Kannan , Matthias Brugger , AngeloGioacchino Del Regno , Wolfram Sang , Benson Leung , Tzung-Bi Shih , Mark Brown , Liam Girdwood Cc: Chen-Yu Tsai , chrome-platform@lists.linux.dev, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, linux-kernel@vger.kernel.org, Douglas Anderson , Johan Hovold , Jiri Kosina , Andy Shevchenko , linux-i2c@vger.kernel.org Subject: [PATCH v7 09/10] platform/chrome: Introduce device tree hardware prober Date: Wed, 11 Sep 2024 15:27:47 +0800 Message-ID: <20240911072751.365361-10-wenst@chromium.org> X-Mailer: git-send-email 2.46.0.598.g6f2099f65c-goog In-Reply-To: <20240911072751.365361-1-wenst@chromium.org> References: <20240911072751.365361-1-wenst@chromium.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Some devices are designed and manufactured with some components having multiple drop-in replacement options. These components are often connected to the mainboard via ribbon cables, having the same signals and pin assignments across all options. These may include the display panel and touchscreen on laptops and tablets, and the trackpad on laptops. Sometimes which component option is used in a particular device can be detected by some firmware provided identifier, other times that information is not available, and the kernel has to try to probe each device. This change attempts to make the "probe each device" case cleaner. The current approach is to have all options added and enabled in the device tree. The kernel would then bind each device and run each driver's probe function. This works, but has been broken before due to the introduction of asynchronous probing, causing multiple instances requesting "shared" resources, such as pinmuxes, GPIO pins, interrupt lines, at the same time, with only one instance succeeding. Work arounds for these include moving the pinmux to the parent I2C controller, using GPIO hogs or pinmux settings to keep the GPIO pins in some fixed configuration, and requesting the interrupt line very late. Such configurations can be seen on the MT8183 Krane Chromebook tablets, and the Qualcomm sc8280xp-based Lenovo Thinkpad 13S. Instead of this delicate dance between drivers and device tree quirks, this change introduces a simple I2C component prober. For any given class of devices on the same I2C bus, it will go through all of them, doing a simple I2C read transfer and see which one of them responds. It will then enable the device that responds. This requires some minor modifications in the existing device tree. The status for all the device nodes for the component options must be set to "failed-needs-probe". This makes it clear that some mechanism is needed to enable one of them, and also prevents the prober and device drivers running at the same time. Signed-off-by: Chen-Yu Tsai Acked-by: Tzung-Bi Shih --- Changes since v6: - Adapted to new I2C OF prober interface - Collected Acked-by Changes since v5: - Adapt to new i2c_of_probe_component() parameters Changes since v4: - Fix Kconfig dependency - Update copyright year - Drop "linux/of.h" header - Include "linux/errno.h" - Move |int ret| declaration to top of block - Return -ENODEV on no match instead of 0 - Unregister platform driver and device unconditionally after previous change Changes since v3: - Include linux/init.h - Rewrite for loop in driver probe function as suggested by Andy - Make prober driver buildable as module - Ignore prober errors other than probe deferral Changes since v2: - Addressed Rob's comments - Move remaining driver code to drivers/platform/chrome/ - Depend on rather than select CONFIG_I2C - Copy machine check to driver init function - Addressed Andy's comments - Explicitly mention "device tree" or OF in driver name, description and Kconfig symbol - Drop filename from inside the file - Switch to passing "struct device *" to shorten lines - Move "ret =3D 0" to just before for_each_child_of_node(i2c_node, node) - Make loop variable size_t (instead of unsigned int as Andy asked) - Use PLATFORM_DEVID_NONE instead of raw -1 - Use standard goto error path pattern in hw_prober_driver_init() - Changes since v1: - New patch --- drivers/platform/chrome/Kconfig | 11 ++ drivers/platform/chrome/Makefile | 1 + .../platform/chrome/chromeos_of_hw_prober.c | 125 ++++++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 drivers/platform/chrome/chromeos_of_hw_prober.c diff --git a/drivers/platform/chrome/Kconfig b/drivers/platform/chrome/Kcon= fig index 7dbeb786352a..b7dbaf77b6db 100644 --- a/drivers/platform/chrome/Kconfig +++ b/drivers/platform/chrome/Kconfig @@ -61,6 +61,17 @@ config CHROMEOS_TBMC To compile this driver as a module, choose M here: the module will be called chromeos_tbmc. =20 +config CHROMEOS_OF_HW_PROBER + tristate "ChromeOS Device Tree Hardware Prober" + depends on OF + depends on I2C + select OF_DYNAMIC + default OF + help + This option enables the device tree hardware prober for ChromeOS + devices. The driver will probe the correct component variant in + devices that have multiple drop-in options for one component. + config CROS_EC tristate "ChromeOS Embedded Controller" select CROS_EC_PROTO diff --git a/drivers/platform/chrome/Makefile b/drivers/platform/chrome/Mak= efile index 2dcc6ccc2302..21a9d5047053 100644 --- a/drivers/platform/chrome/Makefile +++ b/drivers/platform/chrome/Makefile @@ -8,6 +8,7 @@ obj-$(CONFIG_CHROMEOS_ACPI) +=3D chromeos_acpi.o obj-$(CONFIG_CHROMEOS_LAPTOP) +=3D chromeos_laptop.o obj-$(CONFIG_CHROMEOS_PRIVACY_SCREEN) +=3D chromeos_privacy_screen.o obj-$(CONFIG_CHROMEOS_PSTORE) +=3D chromeos_pstore.o +obj-$(CONFIG_CHROMEOS_OF_HW_PROBER) +=3D chromeos_of_hw_prober.o obj-$(CONFIG_CHROMEOS_TBMC) +=3D chromeos_tbmc.o obj-$(CONFIG_CROS_EC) +=3D cros_ec.o obj-$(CONFIG_CROS_EC_I2C) +=3D cros_ec_i2c.o diff --git a/drivers/platform/chrome/chromeos_of_hw_prober.c b/drivers/plat= form/chrome/chromeos_of_hw_prober.c new file mode 100644 index 000000000000..6d9667c40cc7 --- /dev/null +++ b/drivers/platform/chrome/chromeos_of_hw_prober.c @@ -0,0 +1,125 @@ +// 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; +}; + +struct chromeos_i2c_probe_data { + const struct i2c_of_probe_cfg *cfg; + const struct i2c_of_probe_simple_opts *opts; +}; + +static int chromeos_i2c_component_prober(struct device *dev, const void *_= data) +{ + const struct chromeos_i2c_probe_data *data =3D _data; + struct i2c_of_probe_simple_ctx ctx =3D { + .opts =3D data->opts + }; + + return i2c_of_probe_component(dev, data->cfg, &ctx); +} + +static const struct chromeos_i2c_probe_data chromeos_i2c_probe_dumb_touchs= creen =3D { + .cfg =3D &(const struct i2c_of_probe_cfg) { + .type =3D "touchscreen" + } +}; + +static const struct chromeos_i2c_probe_data chromeos_i2c_probe_dumb_trackp= ad =3D { + .cfg =3D &(const struct i2c_of_probe_cfg) { + .type =3D "trackpad" + } +}; + +static const struct hw_prober_entry hw_prober_platforms[] =3D { + { .compatible =3D "google,hana", .prober =3D chromeos_i2c_component_probe= r, .data =3D &chromeos_i2c_probe_dumb_touchscreen }, + { .compatible =3D "google,hana", .prober =3D chromeos_i2c_component_probe= r, .data =3D &chromeos_i2c_probe_dumb_trackpad }, +}; + +static int chromeos_of_hw_prober_probe(struct platform_device *pdev) +{ + for (size_t i =3D 0; i < ARRAY_SIZE(hw_prober_platforms); i++) { + int ret; + + if (!of_machine_is_compatible(hw_prober_platforms[i].compatible)) + continue; + + ret =3D hw_prober_platforms[i].prober(&pdev->dev, hw_prober_platforms[i]= .data); + /* Ignore unrecoverable errors and keep going through other probers */ + if (ret =3D=3D -EPROBE_DEFER) + return ret; + } + + return 0; +} + +static struct platform_driver chromeos_of_hw_prober_driver =3D { + .probe =3D chromeos_of_hw_prober_probe, + .driver =3D { + .name =3D DRV_NAME, + }, +}; + +static struct platform_device *chromeos_of_hw_prober_pdev; + +static int chromeos_of_hw_prober_driver_init(void) +{ + size_t i; + int ret; + + for (i =3D 0; i < ARRAY_SIZE(hw_prober_platforms); i++) + if (of_machine_is_compatible(hw_prober_platforms[i].compatible)) + break; + if (i =3D=3D ARRAY_SIZE(hw_prober_platforms)) + return -ENODEV; + + ret =3D platform_driver_register(&chromeos_of_hw_prober_driver); + if (ret) + return ret; + + chromeos_of_hw_prober_pdev =3D + platform_device_register_simple(DRV_NAME, PLATFORM_DEVID_NONE, NULL, 0); + if (IS_ERR(chromeos_of_hw_prober_pdev)) + goto err; + + return 0; + +err: + platform_driver_unregister(&chromeos_of_hw_prober_driver); + + return PTR_ERR(chromeos_of_hw_prober_pdev); +} +module_init(chromeos_of_hw_prober_driver_init); + +static void chromeos_of_hw_prober_driver_exit(void) +{ + platform_device_unregister(chromeos_of_hw_prober_pdev); + platform_driver_unregister(&chromeos_of_hw_prober_driver); +} +module_exit(chromeos_of_hw_prober_driver_exit); + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("ChromeOS device tree hardware prober"); +MODULE_IMPORT_NS(I2C_OF_PROBER); --=20 2.46.0.598.g6f2099f65c-goog From nobody Thu Sep 19 10:41:31 2024 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A3B1D156242 for ; Wed, 11 Sep 2024 07:28:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726039736; cv=none; b=ctCK+k+fKNXvJcXda35DSFtOSkB2TdG43TjnXA2Ybme1yyPcRCL0oJGJlB0NQrqHmhAQll2bzs5MJvHkiot1UarOQSQajFtyr38gI8Qb2dyYfAoo1giexLC35+JLo7++FHqZ+fVMYON05cKmMMtsPZKHb5Eu8tqppoiq2KsHL+M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726039736; c=relaxed/simple; bh=WH9TQBccmD+rZJ3vfv7UzS/68zcsvwxBedWHmlT4AQQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ANOgWC5iA/BSPz157WyjwjF6U+5CEcN+o0Bt8KqmL8naWkNteHctNj6AZpVSKd6pbi1SHtxBycdVH5aYagE1jmneryhjWxTMQ+BP+aD0KGiORf/OsOmCuKExZzK7So2bu6oj5u55QgpQHx9dYMdZ0X6Tkl05KCgU4yLrBp1B3Gs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=TABH0xkQ; arc=none smtp.client-ip=209.85.210.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="TABH0xkQ" Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-717849c0dcaso5991049b3a.3 for ; Wed, 11 Sep 2024 00:28:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1726039734; x=1726644534; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=nwH4RxuOgmEV/VtpOzbgUmR2GAZBgupxo+TGOE0kG2U=; b=TABH0xkQv6in//IFyVn0lYs0jXtmCKboHoSNTlboJKjsNKBjdHFEmxsDRgSLxAmpJd 55w8UvnZglGTJZ4mCXzisa8U0wmXXuf8ocz+qMMZRGw38Mi1dABJpmfNszoicEhPRw+v q3erTAgniVJwjNmXSRSq+KGPNoIUZpc8rROT0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726039734; x=1726644534; 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=nwH4RxuOgmEV/VtpOzbgUmR2GAZBgupxo+TGOE0kG2U=; b=fYE5hFrayvhQSqy3HK+LehMgbqQBQY33RzCfj56UtYXWFy6bO0K8sWGIjHkO4+Zkef wmva9Dhc3IPW0X987CFVHslXmHxt/ghjiTwGvu9eWGVSsGFDIVzsIMSXAtskjmhzNKJ2 +JPwGbJ0oOlFlyt+nReZBItlOWvgN3H7iLNqfM7qtjYhsncHgIlrZn/yTttuQYjXUWpV aCq1H8JjlrLl0B3+BOWWz6xyA9T79JcfFG/LGQwyxapBYXcm6OKshsvwdt59gT9P9+CI OXFe/o+kyXvpp3pfI0q8QflHgAbmze9U4yBUeu5Pj/oX12q01UttSNI9Y8Oy+0iQd4oP IlDg== X-Forwarded-Encrypted: i=1; AJvYcCXH4dZqdbo7O/IpFoR/PwvRz7XMDXlDj3P4+YIVeQ7c18WKUze1sb0tcVnX+MSHiNScQdNJEtgAJMPen5M=@vger.kernel.org X-Gm-Message-State: AOJu0YwcTGLxbTLK4d3UPo0RJSVotzROBlTaJjUmvyjvo4XPEimM9zyA NCzp/nOnQWiyAlL6ENQWEwHTnWkK87QrmpYc7XySsFWhM6pWHv6JV2pevVyTxA== X-Google-Smtp-Source: AGHT+IE516O5LzHPomwlShsfozQVhv9kW9/2NJZ52HjZuQpukTdS9x9UaAx49lZ9H3HGK77PP1P1/Q== X-Received: by 2002:a05:6a00:4b4c:b0:717:bce8:f8a9 with SMTP id d2e1a72fcca58-718d5dee47amr21431629b3a.1.1726039733865; Wed, 11 Sep 2024 00:28:53 -0700 (PDT) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:8398:fe34:eba2:f301]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71908fe4e7esm2399415b3a.80.2024.09.11.00.28.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Sep 2024 00:28: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 v7 10/10] arm64: dts: mediatek: mt8173-elm-hana: Mark touchscreens and trackpads as fail Date: Wed, 11 Sep 2024 15:27:48 +0800 Message-ID: <20240911072751.365361-11-wenst@chromium.org> X-Mailer: git-send-email 2.46.0.598.g6f2099f65c-goog In-Reply-To: <20240911072751.365361-1-wenst@chromium.org> References: <20240911072751.365361-1-wenst@chromium.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Instead of having them all available, mark them all as "fail-needs-probe" and have the implementation try to probe which one is present. Also remove the shared resource workaround by moving the pinctrl entry for the trackpad interrupt line back into the individual trackpad nodes. Signed-off-by: Chen-Yu Tsai --- Changes since v6: None. Changes since v5: None. Changes since v4: - Rebased Changes since v3: - Also remove second source workaround, i.e. move the interrupt line pinctrl entry from the i2c node back to the components. Changes since v2: - Drop class from status --- arch/arm64/boot/dts/mediatek/mt8173-elm-hana.dtsi | 13 +++++++++++++ arch/arm64/boot/dts/mediatek/mt8173-elm.dtsi | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/arch/arm64/boot/dts/mediatek/mt8173-elm-hana.dtsi b/arch/arm64= /boot/dts/mediatek/mt8173-elm-hana.dtsi index 8d1cbc92bce3..251e084bf7de 100644 --- a/arch/arm64/boot/dts/mediatek/mt8173-elm-hana.dtsi +++ b/arch/arm64/boot/dts/mediatek/mt8173-elm-hana.dtsi @@ -14,6 +14,7 @@ touchscreen2: touchscreen@34 { compatible =3D "melfas,mip4_ts"; reg =3D <0x34>; interrupts-extended =3D <&pio 88 IRQ_TYPE_LEVEL_LOW>; + status =3D "fail-needs-probe"; }; =20 /* @@ -26,6 +27,7 @@ touchscreen3: touchscreen@20 { reg =3D <0x20>; hid-descr-addr =3D <0x0020>; interrupts-extended =3D <&pio 88 IRQ_TYPE_LEVEL_LOW>; + status =3D "fail-needs-probe"; }; =20 /* Lenovo Ideapad C330 uses G2Touch touchscreen as a 2nd source touchscre= en */ @@ -47,9 +49,12 @@ &i2c4 { trackpad2: trackpad@2c { compatible =3D "hid-over-i2c"; interrupts-extended =3D <&pio 117 IRQ_TYPE_LEVEL_LOW>; + pinctrl-names =3D "default"; + pinctrl-0 =3D <&trackpad_irq>; reg =3D <0x2c>; hid-descr-addr =3D <0x0020>; wakeup-source; + status =3D "fail-needs-probe"; }; }; =20 @@ -74,3 +79,11 @@ pins_wp { }; }; }; + +&touchscreen { + status =3D "fail-needs-probe"; +}; + +&trackpad { + status =3D "fail-needs-probe"; +}; diff --git a/arch/arm64/boot/dts/mediatek/mt8173-elm.dtsi b/arch/arm64/boot= /dts/mediatek/mt8173-elm.dtsi index b4d85147b77b..eee64461421f 100644 --- a/arch/arm64/boot/dts/mediatek/mt8173-elm.dtsi +++ b/arch/arm64/boot/dts/mediatek/mt8173-elm.dtsi @@ -358,12 +358,12 @@ touchscreen: touchscreen@10 { &i2c4 { clock-frequency =3D <400000>; status =3D "okay"; - pinctrl-names =3D "default"; - pinctrl-0 =3D <&trackpad_irq>; =20 trackpad: trackpad@15 { compatible =3D "elan,ekth3000"; interrupts-extended =3D <&pio 117 IRQ_TYPE_LEVEL_LOW>; + pinctrl-names =3D "default"; + pinctrl-0 =3D <&trackpad_irq>; reg =3D <0x15>; vcc-supply =3D <&mt6397_vgp6_reg>; wakeup-source; --=20 2.46.0.598.g6f2099f65c-goog