From patchwork Mon Dec 2 13:15:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herve Codina X-Patchwork-Id: 13890599 X-Patchwork-Delegate: bhelgaas@google.com Received: from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net [217.70.183.198]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4D8B21FE457; Mon, 2 Dec 2024 13:15:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.198 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733145355; cv=none; b=bhvC8HXH00tZc2MYvT6CTkNUJ8wXvtO8HRwyc7Y3v2PVpW7IxIRlsca6sxwrGry6bqZkUcmfn8wcj6y/qOxT9740E56N0TEeumYGESPYqfEXthm5D9O4m+MtWpTT5HbQjRJ03JKQyYD67AmtWhQH3S+MYW1RCV1wCOUxQNV5Hdg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733145355; c=relaxed/simple; bh=MXo9qknpg/+O6aSt5D4boZNtCfs9E34s+VnJp5uU0F4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iJ0Zc0H6+xnPyMOysysdbfMbTtL0vyXpXXT0IFnldJUx2DJ0fqQE6gqWnN84alhGpDTeUXXFQOeUOTZ21kFjE2vyboZG+KADf8H4/NOTxwoGfbjGGCfLrjliePFfDqlOea9Ibdq+921OsWzaYb8p0yp64H6MvMSqmulj4gvxi9E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=mHr36Ocs; arc=none smtp.client-ip=217.70.183.198 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="mHr36Ocs" Received: by mail.gandi.net (Postfix) with ESMTPA id 182C6C0002; Mon, 2 Dec 2024 13:15:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1733145345; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ohmmf1f1Tk0D6JeMPzj0EkbnDhwhxLtjoCBBfJqa1U4=; b=mHr36OcsfTU27NHmM/RhKbgD5xskIe0akyUqRTkGMyH5sNUVW5hMRBdE9OWJzj9Uw+k4te k7FGtry/e1XO3KYMAKxEyY+NZtZ0AKE8kKo/IZ2IOOxjApCT0qm7uLUdUk0CVVMkSL3J4M JSGnRhX5RoeiBMu8R6txjssfyeAr2bdz9NZ/hCSRgEHFJeSYHhNzvwLQnpCYHkJz4mGYVB ebtbj8dawnIrbmukJXIRvCO4ybVqut5cLPh+pyIAIs7KNq7sofG0d2m16fqvy/nQeENXo2 BRaMKzUilhS//zydImJWGUqLBYtW48DjxNhRHys5b6afYFZai0ldBJ0uUwBxIQ== From: Herve Codina To: Greg Kroah-Hartman , "Rafael J. Wysocki" , Rob Herring , Saravana Kannan , Bjorn Helgaas , Lizhi Hou Cc: linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-pci@vger.kernel.org, Allan Nielsen , Horatiu Vultur , Steen Hegelund , Thomas Petazzoni , Herve Codina Subject: [PATCH v4 1/6] driver core: Introduce device_{add,remove}_of_node() Date: Mon, 2 Dec 2024 14:15:13 +0100 Message-ID: <20241202131522.142268-2-herve.codina@bootlin.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241202131522.142268-1-herve.codina@bootlin.com> References: <20241202131522.142268-1-herve.codina@bootlin.com> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-GND-Sasl: herve.codina@bootlin.com An of_node can be set to a device using device_set_node(). This function cannot prevent any of_node and/or fwnode overwrites. When adding an of_node on an already present device, the following operations need to be done: - Attach the of_node if no of_node were already attached - Attach the of_node as a fwnode if no fwnode were already attached This is the purpose of device_add_of_node(). device_remove_of_node() reverts the operations done by device_add_of_node(). Signed-off-by: Herve Codina --- drivers/base/core.c | 52 ++++++++++++++++++++++++++++++++++++++++++ include/linux/device.h | 2 ++ 2 files changed, 54 insertions(+) diff --git a/drivers/base/core.c b/drivers/base/core.c index 8b056306f04e..3953c5ab7316 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -5216,6 +5216,58 @@ void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode) } EXPORT_SYMBOL_GPL(set_secondary_fwnode); +/** + * device_remove_of_node - Remove an of_node from a device + * @dev: device whose device-tree node is being removed + */ +void device_remove_of_node(struct device *dev) +{ + dev = get_device(dev); + if (!dev) + return; + + if (!dev->of_node) + goto end; + + if (dev->fwnode == of_fwnode_handle(dev->of_node)) + dev->fwnode = NULL; + + of_node_put(dev->of_node); + dev->of_node = NULL; + +end: + put_device(dev); +} +EXPORT_SYMBOL_GPL(device_remove_of_node); + +/** + * device_add_of_node - Add an of_node to an existing device + * @dev: device whose device-tree node is being added + * @of_node: of_node to add + */ +void device_add_of_node(struct device *dev, struct device_node *of_node) +{ + if (!of_node) + return; + + dev = get_device(dev); + if (!dev) + return; + + if (WARN(dev->of_node, "%s: Cannot replace node %pOF with %pOF\n", + dev_name(dev), dev->of_node, of_node)) + goto end; + + dev->of_node = of_node_get(of_node); + + if (!dev->fwnode) + dev->fwnode = of_fwnode_handle(of_node); + +end: + put_device(dev); +} +EXPORT_SYMBOL_GPL(device_add_of_node); + /** * device_set_of_node_from_dev - reuse device-tree node of another device * @dev: device whose device-tree node is being set diff --git a/include/linux/device.h b/include/linux/device.h index 667cb6db9019..ef4c0f3c41cd 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -1149,6 +1149,8 @@ int device_online(struct device *dev); void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode); void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode); void device_set_node(struct device *dev, struct fwnode_handle *fwnode); +void device_add_of_node(struct device *dev, struct device_node *of_node); +void device_remove_of_node(struct device *dev); void device_set_of_node_from_dev(struct device *dev, const struct device *dev2); static inline struct device_node *dev_of_node(struct device *dev)