From patchwork Thu Mar 13 07:18:00 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matti Vaittinen X-Patchwork-Id: 14014488 Received: from mail-lj1-f178.google.com (mail-lj1-f178.google.com [209.85.208.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 31A5B1FC102; Thu, 13 Mar 2025 07:18:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741850292; cv=none; b=qNnScgHGMafg+1wN2pfDJms+HMhFGxLXw7astKoDViekbwATKndA2gO+ERuz4rC4ef7cFCQvLlgCjw52QrU2F7UwNhqjB7JIhtNSmbcOsCWRYL8qv/KkLnTry0O7PL9gqXns0LldjPiDPoAT/a8JU1tujcjeNiyVoQaTaDA4hsk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741850292; c=relaxed/simple; bh=f8ZnW3oO3kWzj9xDjSgBVqOoaNahbzAF5dOy2Fh++kE=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=oVorPt4uc3x1c08OM6oBTjNEcBSr2UO1F4Xs1BPCC3nQX7QVQTbjleldMw3OU7AZ5nQfnWmXGs7amE1aGCsoqWi5nUG5r51eVyj2R4YwPLEQ/rvSO96m9mRFc1QE2KD4MhHiCo9m5KAW5nNrBLP+l10tQtPLtbAql6WVGCu4Us4= 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=Zfb6fPLd; arc=none smtp.client-ip=209.85.208.178 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="Zfb6fPLd" Received: by mail-lj1-f178.google.com with SMTP id 38308e7fff4ca-30bf8f5dde5so6069751fa.2; Thu, 13 Mar 2025 00:18:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1741850288; x=1742455088; 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=S6OQ0UScw9oWVqgjbWQLTmLI2FDlmbWiio4NLFE3cNQ=; b=Zfb6fPLdyWsgy1t5Y/eO1r5/A06bq2hp8VOMJIU5fmNwt/B8Y8SeVl/9sQMqqd1zj0 VHDfDfkjxxZDxgvXzazHml91PErt1jn8HSDEpaqP/Wu8M5hXV9EVD7NZKLXp0oSdBab3 3R/ahYxMeSf6Zjwgln2uD3DX4Zd/pcl3tdFCR0MQPVFoYoZrMqtYNz/3ifLUNPtDEE7H bteRFVnT8QfeLE8nEltIHgj2umFlV91h8By9EiWm5wS4ooP4tstKkdS9TAWnBDZDo9xo /xUNJtR0uFQw8Rw3LnE7gawSbMl2S4jPrKOB6zD4YhECBxOoPrdkhvuG0v7VsvDbdCSK hj4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741850288; x=1742455088; 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=S6OQ0UScw9oWVqgjbWQLTmLI2FDlmbWiio4NLFE3cNQ=; b=tzLAe3OW7xZQ8P6KHicmLhB0/cPei8msdiOqySh5mgJzCBN9EUjUU8N0fAf7JiRT09 h/EbaOLc58OX32m5zmd+YZk68nlGu3yaiKlNGDB908p3Jex6nknwC756UrSMYLlk/IAd o8n2w3zkG441JJ/f8mCBFKemSbugI9u2CnSRabWUNsAXBgm2dr2p91A8HPbHrvITgN8r BTLaTIP91uKB3J5nhHlUHqjlb5M71q5CsZ4ctwGpRcE+5T27uO0FFTBdDeHim/rPat74 SyhOgLF9zPwofftBF1N43oO6wBTxmLqvRaTEFDHzm6Zq1+tQVRf1lxv+w34vWxKCRK9v xDNA== X-Forwarded-Encrypted: i=1; AJvYcCUdkldFZTMi99oZoa8BNn5u8isniCS2PwsLQMYLhF5XmwjVMiykb3RTilHSK0X1Y8Kp1ZAE1BHojPIS@vger.kernel.org, AJvYcCUsNu88Q407USJEz9E0680I7sUZjlKmoykPLn82oEEGOSN1LxBx72EjF39bD8rfKMIjenh2P5uzbQ2pGZXH@vger.kernel.org, AJvYcCWOrQy7APWxJMyFW3XSNKtwMyCH0FLeV+gzMNyFuGa2dzJS6D55xRXfN7bVR0FrjXdnMlwF9tSk@vger.kernel.org, AJvYcCWiivILHNU8zeS0bDwS7OiqlcSqqBRqEW2/RyXoyRiqm9Lf9/iVSfFBWNPK+VqFIGrdAP3eXj4JuDAGgQ==@vger.kernel.org, AJvYcCX+azUwH93afWtLXG9nKsBSac7CI5VofXysAo+61HqCykWMc/+0vlVIInl7KTftM6Jhwiit78Z3++KV@vger.kernel.org X-Gm-Message-State: AOJu0Yw64XQeeDrxt3ytSp1VgVYwTmRyaryGT6GKtmlY+GhDWy/90R0P KNyL8QpihnKiKUtJ16Dam/Z+fYqwDQ88+MvwLcjeMpfrpjFVKeWe X-Gm-Gg: ASbGncv8SlMMltFNNhJN8DDFgYHU56z9L+R15QYgzliObLny+qon8UQEaWtV7YoSiZs zARsMY3V+RkS1is2o/KwhiN3vGeHW2EBjiLKlTazBPLFwRSIoUUBI9QCE7BQr79tzZAgHLNwwAI pyalNydcRSpsckR/8zfcMiXWdFzzfBbLhf2cV8l4KzYa/ogDUceQQ16W5a9QQOiTXunh5l0KyUE GSKYtpFUKw9HhWVMpiqkTQ2TXogdMf1OMhZ2pHkfW9IDVjYLRSybC+de4gaq98+XEp5Ble1BPT2 lL0a9/LtJ1LSeDqBC3hn/TiGEDqRirt78Kl/o+/QpSJfTTgLzBw= X-Google-Smtp-Source: AGHT+IHzZv16Eu3w2zLkjkG+6Z49HMA0c9ugl4Tm5n7/tbL2xQpVPA78kgBBEziTb7/bu9L9u95PWQ== X-Received: by 2002:a05:6512:3da3:b0:545:8a1:536e with SMTP id 2adb3069b0e04-54990e2be7bmr8687493e87.6.1741850288124; Thu, 13 Mar 2025 00:18:08 -0700 (PDT) Received: from mva-rohm ([2a10:a5c0:800d:dd00:8fdf:935a:2c85:d703]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-549ba885982sm117495e87.192.2025.03.13.00.18.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Mar 2025 00:18:05 -0700 (PDT) Date: Thu, 13 Mar 2025 09:18:00 +0200 From: Matti Vaittinen To: Matti Vaittinen , Matti Vaittinen Cc: Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Andy Shevchenko , Daniel Scally , Heikki Krogerus , Sakari Ailus , Greg Kroah-Hartman , "Rafael J. Wysocki" , Danilo Krummrich , Matti Vaittinen , Claudiu Manoil , Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-acpi@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH v7 02/10] property: Add functions to iterate named child Message-ID: References: Precedence: bulk X-Mailing-List: netdev@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 --- Revision history: 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 | 24 ++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/drivers/base/property.c b/drivers/base/property.c index c1392743df9c..83fa0d0ee1f5 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.:: + * device_get_named_child_node_count(dev, "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..7411dfdaadc0 100644 --- a/include/linux/property.h +++ b/include/linux/property.h @@ -167,10 +167,18 @@ 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)) +#define fwnode_for_each_available_named_child_node(fwnode, child, name) \ + fwnode_for_each_available_child_node(fwnode, child) \ + if (!fwnode_name_eq(child, name)) { } else + struct fwnode_handle *device_get_next_child_node(const struct device *dev, struct fwnode_handle *child); @@ -178,11 +186,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 +226,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) {