On Mon, Dec 09, 2024 at 02:03:33PM +0100, Herve Codina wrote: > 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 <herve.codina@xxxxxxxxxxx> > --- > drivers/base/core.c | 54 ++++++++++++++++++++++++++++++++++++++++++ > include/linux/device.h | 2 ++ > 2 files changed, 56 insertions(+) > > diff --git a/drivers/base/core.c b/drivers/base/core.c > index 8b056306f04e..81e5465aa746 100644 > --- a/drivers/base/core.c > +++ b/drivers/base/core.c > @@ -5216,6 +5216,60 @@ 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) Why is this void? > +{ > + if (!of_node) > + return; > + > + dev = get_device(dev); > + if (!dev) > + return; > + > + if (dev->of_node) { > + dev_warn(dev, "Cannot replace node %pOF with %pOF\n", > + dev->of_node, of_node); Why not return an error too? Otherwise you can never know if this worked or not. thanks, greg k-h