From patchwork Thu Mar 20 08:21:02 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matti Vaittinen X-Patchwork-Id: 14023500 X-Patchwork-Delegate: geert@linux-m68k.org Received: from mail-lf1-f50.google.com (mail-lf1-f50.google.com [209.85.167.50]) (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 E9C252135A1; Thu, 20 Mar 2025 08:21:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742458873; cv=none; b=uwsiuXGc16EnWh4mghAclzRfh8k0gQlMAh3ew8jVTiaYyfDbkFPnRwgv3npWOiIeBGeicgvhXd0tkiXi8gdnAgzYj8KeVcObkjvEZUUppSDqqytkFWV1ba2Nfuv4TSC5UXaio0T86s3jcTyXAsHG37A3xkoPYgTnaJrr8E5B/kQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742458873; c=relaxed/simple; bh=9RDEkfUAtOtIDvk2ej8pElhGQ26MOiuQJ/PWR+OiWEk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=saQd4jNIOnn4jq3i9oJszmGMOEEfdFoPPgcMmWl4c4IANtakaDAolV9PCpCxLEiAgUkmnvPB+gflSnGgXReiEjzUVZx9F7S9cmiAX4y7ZN604KjvwMxP+OMltTYeoybWPK/KfVnSWarLF/NhN/PW4uGFzgLUhiC/XjQKVl7Gvs0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=NgvVMNOW; arc=none smtp.client-ip=209.85.167.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="NgvVMNOW" Received: by mail-lf1-f50.google.com with SMTP id 2adb3069b0e04-543d8badc30so652221e87.0; Thu, 20 Mar 2025 01:21:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742458870; x=1743063670; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=o8YCQKSCMbcpjqvqGVAud+bsNcc/Isbjy4a8q5O0R/8=; b=NgvVMNOWrK2s8/dPr5JqlbGGlig+dLY4/cXervoZ+A9c7rj6/iF3shIwfFcqzbPTKL H6JKgeJuiolVWweTRZV1+tMfPiMmRhjds0+860i6Iwyi3YTjM4lOa+6qRMhdcFdbJHRA i7N5VjSKIZtc2epUpq2ijw+OvVBqV83Wf3hdMDH9/liAFUluukxmXvjHEkO2dXZb4MzW 7f92dQE0flACrneey0ToLtJ54mEj7DJ2bpB0vT0gfQD7ovE7T/ApC4zXHrGT0oulPau5 8x+aDLyBMyeyySyGh0wuxHeyFmbN7zrbIhrL3WuftCejxb1V2J/Xi9PBiLuFn/5js3uC 46Uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742458870; x=1743063670; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=o8YCQKSCMbcpjqvqGVAud+bsNcc/Isbjy4a8q5O0R/8=; b=OBJwIPCMd5aUZOwQZN2NqgK3uTGcrvcnirEZ7SDwjH8iS27pZr+G9p0fMLMTnbRwPK i+NCnc1EI47Yo/ssTGBWFtWLv1DUqpQOk98LaSbb8l3r/HMerQ9h6utRsxANqRxpi2sz ZDPyVWkYHnilZCNrSmnahW3Efx0/dJ9mreGzMn2gXz79dmUj/HNzbQ8EkPq4nYMlRtzI +N2SAKfsigXT7dkSqzCS63/f42fcCHjqXUmeTQzsoyHzHjfwoIQPlinfbRYAvoVAKRUk rAG+c2yy5u32IGXX9JgZBrjc3UoZDWW5R2oIwhBKsyemgMpsi9AKaaIauftjNYKAW2jw kKTw== X-Forwarded-Encrypted: i=1; AJvYcCW67GvEEiPeHdfOSfQ/fG4az4r0wonIoe+gVgXnZWdmH5WnRGF6uFMpwLHqR9YZt0riR/ENoZH0+Uqz@vger.kernel.org, AJvYcCWMx1rSV/7bdzlnt+swyU8J2GlN5QkoET+vW2Jyhd4ArzINSx8rilHsmynQu5jRiSLgYQ8ZbqHXgOmEVA==@vger.kernel.org, AJvYcCXUSP8NPO3keFS6O9guBcW1I1MThUmbvtHPOss+/f+GRG38meTlc7Z//8vpaPQLtNekYVdhj14f88zOx0v6Y/vQQ2E=@vger.kernel.org, AJvYcCXqTYIu7rs2DhDsHRE0YH199biAePa9mtzLL1EIvr+6y8HHi1h3TENGJehTygDVJ5XcS81PAGfbsuQv@vger.kernel.org, AJvYcCXtlsdTG2wz/sTAECrVIZOfnkSrlDHfG2Gd0wU7Aixbe46jtvEkHtRNWZtueZGcuAXReOezgQEI4/PnqYrQ@vger.kernel.org X-Gm-Message-State: AOJu0YxSdKJlPdrNU13PmfVzXFlxjQ45QXBc5MKuDjsvbjADBJwbA0Tq 0l768xO6BVaUyb2SQuxD4XJcBb8R0eWFq0jDN4Fa/82gp+mBNzQh X-Gm-Gg: ASbGnctlh1XMufV2naLg+hJZe6FridNICFwkAr76+6LrVZ7OabB4TP9qobN2ur9zvBg isTwCbl3mwb4Qv/M0/9wcAj3qUjL5l7Q0DtREpnFqbVTlpFpoa4592oIlq/9XWGONYhNo9mnI5j R5+0Weimd6TRab+gt1w1iW/x+1+IQ9+NFWtV6s7/ijkrlEkwOmvmaPERRj1qya/hX3NeApu13Cu n80dRJ4YZOHP04+UNaaERfHiel91ZjfpPDUjoJku/9yJCLcgp2fqhOgzN7IS0FBwkRPFsQhJrn3 D3vKiA96h9//2iKHlF3eDJNXTb5Ml0sF8o65//hVN2uzksSw8pY= X-Google-Smtp-Source: AGHT+IEtQcDT1hKcr/hyPc4GQAHaaV6Ce4tKlVEIYYQ9CJsshWPNPgEWDxvASx6328HXCmwcR69h0Q== X-Received: by 2002:a05:6512:ea5:b0:549:8d16:7267 with SMTP id 2adb3069b0e04-54acb1a3327mr1879960e87.10.1742458869558; Thu, 20 Mar 2025 01:21:09 -0700 (PDT) Received: from mva-rohm ([2a10:a5c0:800d:dd00:8fdf:935a:2c85:d703]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-549ba88536csm2134374e87.177.2025.03.20.01.21.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Mar 2025 01:21:07 -0700 (PDT) Date: Thu, 20 Mar 2025 10:21:02 +0200 From: Matti Vaittinen To: Matti Vaittinen , Matti Vaittinen Cc: Jonathan Cameron , Lars-Peter Clausen , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Andy Shevchenko , Daniel Scally , Heikki Krogerus , Sakari Ailus , Greg Kroah-Hartman , "Rafael J. Wysocki" , Danilo Krummrich , Matti Vaittinen , Lad Prabhakar , Chen-Yu Tsai , Jernej Skrabec , Samuel Holland , Nuno Sa , David Lechner , Javier Carrasco , Guillaume Stols , Olivier Moysan , Dumitru Ceclan , Trevor Gamblin , Matteo Martelli , Alisa-Dariana Roman , AngeloGioacchino Del Regno , =?iso-8859-1?q?Jo=E3o_Paulo_Gon=E7alves?= , Ramona Alexandra Nechita , linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-acpi@vger.kernel.org, linux-renesas-soc@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev Subject: [PATCH v9 2/8] property: Add functions to iterate named child Message-ID: References: Precedence: bulk X-Mailing-List: linux-renesas-soc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: There are a few use-cases where child nodes with a specific name need to be parsed. Code like: fwnode_for_each_child_node() if (fwnode_name_eq()) ... can be found from a various drivers/subsystems. Adding a macro for this can simplify things a bit. In a few cases the data from the found nodes is later added to an array, which is allocated based on the number of found nodes. One example of such use is the IIO subsystem's ADC channel nodes, where the relevant nodes are named as channel[@N]. Add helpers for iterating and counting device's sub-nodes with certain name instead of open-coding this in every user. Suggested-by: Jonathan Cameron Signed-off-by: Matti Vaittinen Reviewed-by: Andy Shevchenko Reviewed-by: Marcelo Schmitt --- Revision history: v8 => v9: - Drop the fwnode_for_each_available_named_child_node() as suggested by Sakari during v8 review: https://lore.kernel.org/all/Z9mQPJwnKAkPHriT@kekkonen.localdomain/ v7 => v8: - Fix the example in fwnode_get_named_child_node_count() documentation to use the fwnode_get_named_child_node_count() and not the device_get_named_child_node_count() - Fix the rest of the new macro's indentiations v6 => v7: - Improve kerneldoc - Inline device_get_named_child_node_count() and change it to call fwnode_get_named_child_node_count() inside - Fix indentiation of the new macros v5 => v6: - Add helpers to also iterate through the nodes. v4 => v5: - Use given name instead of string 'channel' when counting the nodes - Add also fwnode_get_child_node_count_named() as suggested by Rob. v3 => v4: - New patch as suggested by Jonathan, see discussion in: https://lore.kernel.org/lkml/20250223161338.5c896280@jic23-huawei/ --- drivers/base/property.c | 27 +++++++++++++++++++++++++++ include/linux/property.h | 20 ++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/drivers/base/property.c b/drivers/base/property.c index c1392743df9c..f42f32ff45fc 100644 --- a/drivers/base/property.c +++ b/drivers/base/property.c @@ -945,6 +945,33 @@ unsigned int device_get_child_node_count(const struct device *dev) } EXPORT_SYMBOL_GPL(device_get_child_node_count); +/** + * fwnode_get_named_child_node_count - number of child nodes with given name + * @fwnode: Node which child nodes are counted. + * @name: String to match child node name against. + * + * Scan child nodes and count all the nodes with a specific name. Potential + * 'number' -ending after the 'at sign' for scanned names is ignored. + * E.g.:: + * fwnode_get_named_child_node_count(fwnode, "channel"); + * would match all the nodes:: + * channel { }, channel@0 {}, channel@0xabba {}... + * + * Return: the number of child nodes with a matching name for a given device. + */ +unsigned int fwnode_get_named_child_node_count(const struct fwnode_handle *fwnode, + const char *name) +{ + struct fwnode_handle *child; + unsigned int count = 0; + + fwnode_for_each_named_child_node(fwnode, child, name) + count++; + + return count; +} +EXPORT_SYMBOL_GPL(fwnode_get_named_child_node_count); + bool device_dma_supported(const struct device *dev) { return fwnode_call_bool_op(dev_fwnode(dev), device_dma_supported); diff --git a/include/linux/property.h b/include/linux/property.h index e214ecd241eb..3e83babac0b0 100644 --- a/include/linux/property.h +++ b/include/linux/property.h @@ -167,6 +167,10 @@ struct fwnode_handle *fwnode_get_next_available_child_node( for (child = fwnode_get_next_child_node(fwnode, NULL); child; \ child = fwnode_get_next_child_node(fwnode, child)) +#define fwnode_for_each_named_child_node(fwnode, child, name) \ + fwnode_for_each_child_node(fwnode, child) \ + if (!fwnode_name_eq(child, name)) { } else + #define fwnode_for_each_available_child_node(fwnode, child) \ for (child = fwnode_get_next_available_child_node(fwnode, NULL); child;\ child = fwnode_get_next_available_child_node(fwnode, child)) @@ -178,11 +182,19 @@ struct fwnode_handle *device_get_next_child_node(const struct device *dev, for (child = device_get_next_child_node(dev, NULL); child; \ child = device_get_next_child_node(dev, child)) +#define device_for_each_named_child_node(dev, child, name) \ + device_for_each_child_node(dev, child) \ + if (!fwnode_name_eq(child, name)) { } else + #define device_for_each_child_node_scoped(dev, child) \ for (struct fwnode_handle *child __free(fwnode_handle) = \ device_get_next_child_node(dev, NULL); \ child; child = device_get_next_child_node(dev, child)) +#define device_for_each_named_child_node_scoped(dev, child, name) \ + device_for_each_child_node_scoped(dev, child) \ + if (!fwnode_name_eq(child, name)) { } else + struct fwnode_handle *fwnode_get_named_child_node(const struct fwnode_handle *fwnode, const char *childname); struct fwnode_handle *device_get_named_child_node(const struct device *dev, @@ -210,6 +222,14 @@ int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name); unsigned int device_get_child_node_count(const struct device *dev); +unsigned int fwnode_get_named_child_node_count(const struct fwnode_handle *fwnode, + const char *name); +static inline unsigned int device_get_named_child_node_count(const struct device *dev, + const char *name) +{ + return fwnode_get_named_child_node_count(dev_fwnode(dev), name); +} + static inline int device_property_read_u8(const struct device *dev, const char *propname, u8 *val) {