Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp1146713pxb; Wed, 6 Apr 2022 09:48:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzOVGDTTBe+DI8802GOAwb88jxVqVVTTgDk2x8W2XcG4NPeevNP99YyrI9lJoF4nJpicwbC X-Received: by 2002:a63:451b:0:b0:380:b9f8:13f4 with SMTP id s27-20020a63451b000000b00380b9f813f4mr7869153pga.367.1649263713351; Wed, 06 Apr 2022 09:48:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649263713; cv=none; d=google.com; s=arc-20160816; b=xTCgqMq/Jtu65Epce0tNzg14Z6+t/uJs6SbYjCHSHMkXKfe6thLP6kMRFg7KRYY5/m wBMvu6PXIltjVgCb3qnk6D9ZJAwrwarLrFRPFNCsjqGBbJ9qHSZhFSza/pZDrpwmhKBt 42HxYI6NFP9C5mBBN/Gr5OLSrrY84HSCjUsvWRDExFSDtuspsOKLRlYbEENkZk2Su7gP pnIxV9+gBZaMseYU6iaAtWOb27SQ738PLgrKSh3bXERQxR9AZPAPuoIvFQrsyqU0DhRL C956QgAvi7wUMbAPBls/GP5vdUE53j2C/5/qbaGetVEsXbSjVrCXUvJ3aAGoNzY4ck0o qh4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=AU410nweQjHX7S47xL6Ks3B+3T6eGPaZuil3GUjgm3M=; b=p+UxxCCSsL22p4wYqgTrV2PSmiyob6jiKnBwHsteQtqnEhOhAAU2IgvVZ9RZ1Sn/zZ gg3LIKEFa9yc0Ml5zh3tTTu6XpOgEaOXPjV5K5xvAJTCDOdLnFmLwB5Nqq6i+i1MF9uq okjjl+k9zcjYHfN7J1aMZucvF6z/JgeMvqKRtS6howpRhO7A/6ycRFQNPc9nH5gwDG1+ vLUYr3iGCmz1SLnDlLO5yBNGdCCOt3mYNU/gosWCAVJ6xc7Od9/CDlt1r0CHFZvtINmt zRPwHwefZQXY8afQQ5Jg5mH/DsOoXFo1FdAm19yc+OoURLKuDiKc+2o+KdsuDbyOJwIG idKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=cPJE0XNv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id n189-20020a6227c6000000b004faa6d525ddsi15287824pfn.269.2022.04.06.09.48.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 09:48:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=cPJE0XNv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 9EB68282564; Wed, 6 Apr 2022 09:22:30 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237329AbiDFQYV (ORCPT + 99 others); Wed, 6 Apr 2022 12:24:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52000 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237641AbiDFQX3 (ORCPT ); Wed, 6 Apr 2022 12:23:29 -0400 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 341AF3C8D6A; Wed, 6 Apr 2022 06:46:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1649252795; x=1680788795; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Ly37bf7KE1/2e2jYQWJyGiJWGtDdYuEz5soj2EbR73k=; b=cPJE0XNvEiehtOo4xJ4VgzTbmDjbCma9rGl2XgVXg3BYYViPvBWu0IF2 oUVu0D1Gsjw4mBL21E+5PhNvT+NX9BIMgFi5w7XJdCY/o0j5CSRr21iLl C0BKfCqsXJvQN7brp0RmUH9KqJpE998XbhvIZmoMXPYCpae7Qj5oisldb TeptIGpmJ4bq7wdd06oMSQPkBCUdf687pCFRmVah+jjwx65vQiiixtGBX hAKefV8E9LdqCWjRyevxOp4akwx1HqEvufmAspBDprrWj7YaRxi6nmqV5 mg+0lNrCfzmARlsK/mxn9iC5G12iVY/XVZcClo+nvvC3UWLhZQJHHG6BQ g==; X-IronPort-AV: E=McAfee;i="6200,9189,10308"; a="259873395" X-IronPort-AV: E=Sophos;i="5.90,239,1643702400"; d="scan'208";a="259873395" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Apr 2022 06:09:57 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.90,239,1643702400"; d="scan'208";a="556951803" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga007.fm.intel.com with ESMTP; 06 Apr 2022 06:09:54 -0700 Received: by black.fi.intel.com (Postfix, from userid 1003) id 6DDA212B; Wed, 6 Apr 2022 16:05:55 +0300 (EEST) From: Andy Shevchenko To: Andy Shevchenko , linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Daniel Scally , Heikki Krogerus , Sakari Ailus , Greg Kroah-Hartman , "Rafael J. Wysocki" , Len Brown , Michael Walle Subject: [PATCH v5 2/4] device property: Introduce fwnode_for_each_parent_node() Date: Wed, 6 Apr 2022 16:05:50 +0300 Message-Id: <20220406130552.30930-2-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220406130552.30930-1-andriy.shevchenko@linux.intel.com> References: <20220406130552.30930-1-andriy.shevchenko@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In a few cases the functionality of fwnode_for_each_parent_node() is already in use. Introduce a common helper macro for it. It may be used by others as well in the future. Signed-off-by: Andy Shevchenko --- v5: new patch drivers/base/property.c | 56 +++++++++++++++++++++------------------- include/linux/property.h | 9 +++++-- 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/drivers/base/property.c b/drivers/base/property.c index 3376d3971a93..bca9a28ce271 100644 --- a/drivers/base/property.c +++ b/drivers/base/property.c @@ -596,17 +596,17 @@ EXPORT_SYMBOL_GPL(fwnode_get_next_parent); */ struct device *fwnode_get_next_parent_dev(struct fwnode_handle *fwnode) { + struct fwnode_handle *parent; struct device *dev; - fwnode_handle_get(fwnode); - do { - fwnode = fwnode_get_next_parent(fwnode); - if (!fwnode) - return NULL; + fwnode_for_each_parent_node(fwnode, parent) { dev = get_dev_from_fwnode(fwnode); - } while (!dev); - fwnode_handle_put(fwnode); - return dev; + if (dev) { + fwnode_handle_put(parent); + return dev; + } + } + return NULL; } /** @@ -617,13 +617,11 @@ struct device *fwnode_get_next_parent_dev(struct fwnode_handle *fwnode) */ unsigned int fwnode_count_parents(const struct fwnode_handle *fwnode) { - struct fwnode_handle *__fwnode; - unsigned int count; - - __fwnode = fwnode_get_parent(fwnode); + struct fwnode_handle *parent; + unsigned int count = 0; - for (count = 0; __fwnode; count++) - __fwnode = fwnode_get_next_parent(__fwnode); + fwnode_for_each_parent_node(fwnode, parent) + count++; return count; } @@ -644,15 +642,16 @@ EXPORT_SYMBOL_GPL(fwnode_count_parents); struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwnode, unsigned int depth) { - fwnode_handle_get(fwnode); + struct fwnode_handle *parent; - do { - if (depth-- == 0) - break; - fwnode = fwnode_get_next_parent(fwnode); - } while (fwnode); + if (depth == 0) + return fwnode_handle_get(fwnode); - return fwnode; + fwnode_for_each_parent_node(fwnode, parent) { + if (--depth == 0) + return parent; + } + return NULL; } EXPORT_SYMBOL_GPL(fwnode_get_nth_parent); @@ -669,17 +668,20 @@ EXPORT_SYMBOL_GPL(fwnode_get_nth_parent); bool fwnode_is_ancestor_of(struct fwnode_handle *test_ancestor, struct fwnode_handle *test_child) { + struct fwnode_handle *parent; + if (IS_ERR_OR_NULL(test_ancestor)) return false; - fwnode_handle_get(test_child); - do { - if (test_child == test_ancestor) { - fwnode_handle_put(test_child); + if (test_child == test_ancestor) + return true; + + fwnode_for_each_parent_node(test_child, parent) { + if (parent == test_ancestor) { + fwnode_handle_put(parent); return true; } - test_child = fwnode_get_next_parent(test_child); - } while (test_child); + } return false; } diff --git a/include/linux/property.h b/include/linux/property.h index 4cd4b326941f..15d6863ae962 100644 --- a/include/linux/property.h +++ b/include/linux/property.h @@ -83,9 +83,14 @@ struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode, const char *fwnode_get_name(const struct fwnode_handle *fwnode); const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode); + struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode); -struct fwnode_handle *fwnode_get_next_parent( - struct fwnode_handle *fwnode); +struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode); + +#define fwnode_for_each_parent_node(fwnode, parent) \ + for (parent = fwnode_get_parent(fwnode); parent; \ + parent = fwnode_get_next_parent(parent)) + struct device *fwnode_get_next_parent_dev(struct fwnode_handle *fwnode); unsigned int fwnode_count_parents(const struct fwnode_handle *fwn); struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwn, -- 2.35.1