Hi Bartosz,

On Mon, 2026-07-06 at 14:44 +0200, Bartosz Golaszewski wrote:
> Encapsulate the reference counting logic for OF nodes assigned to
> platform devices created with platform_device_alloc() in a helper
> function. Make the kerneldoc state that this is the proper interface for
> assigning OF nodes to dynamically allocated platform devices. This will
> allow us to switch to counting the references of the device's firmware
> nodes, not only the OF nodes.
> 
> Reviewed-by: Manuel Ebner <[email protected]>
> Reviewed-by: Andy Shevchenko <[email protected]>
> Signed-off-by: Bartosz Golaszewski <[email protected]>
> ---
>  drivers/base/platform.c         | 18 ++++++++++++++++++
>  include/linux/platform_device.h |  4 ++++
>  2 files changed, 22 insertions(+)
> 
> diff --git a/drivers/base/platform.c b/drivers/base/platform.c
> index 
> fb9120b0bcfe0e7dd9dfc0d29b91e0ad40a01440..3188d5aba5f90622f821c695049cacda030204fb
> 100644
> --- a/drivers/base/platform.c
> +++ b/drivers/base/platform.c
> @@ -693,6 +693,24 @@ int platform_device_add_data(struct platform_device 
> *pdev, const
> void *data,
>  }
>  EXPORT_SYMBOL_GPL(platform_device_add_data);
>  
> +/**
> + * platform_device_set_of_node - assign an OF node to device
> + * @pdev: platform device to add the node for
> + * @np: new device node
> + *
> + * Assign an OF node to this platform device. Internally keep track of the
> + * reference count. Devices created with platform_device_alloc() must use 
> this
> + * function instead of assigning the node manually.

I did some more pondering about this patch and concluded the right place
for a warning would be the lines been read right before or after assigning
a node manually. 
I think this would be in platform_device_alloc() (as I suggested in v2).

BUT my knowledge in C isn't sound. So if it's good as is keep it. If it isn't
add or move the remark.

Either way Thanks and
 Reviewed-by Manuel Ebner.

---
Note:
I removed Mark Brown <[email protected]> from recipients 
because:
“RCPT TO <[email protected]> failed:
<[email protected]>: Recipient address rejected: Domain not 
found”.


> + */
> +void platform_device_set_of_node(struct platform_device *pdev,
> +                              struct device_node *np)
> +{
> +     of_node_put(pdev->dev.of_node);
> +     pdev->dev.of_node = of_node_get(np);
> +     pdev->dev.fwnode = of_fwnode_handle(np);
> +}
> +EXPORT_SYMBOL_GPL(platform_device_set_of_node);
> +
>  /**
>   * platform_device_add - add a platform device to device hierarchy
>   * @pdev: platform device we're adding
> diff --git a/include/linux/platform_device.h b/include/linux/platform_device.h
> index 
> 8c566f09d04efe420d85ffa046f92c44c6d08526..e9f7baceeb4c8269dbc0143c4d8fc9d73ba024ca
> 100644
> --- a/include/linux/platform_device.h
> +++ b/include/linux/platform_device.h
> @@ -19,6 +19,8 @@
>  struct irq_affinity;
>  struct mfd_cell;
>  struct property_entry;
> +struct platform_device_id;
> +struct device_node;
>  
>  struct platform_device {
>       const char      *name;
> @@ -262,6 +264,8 @@ extern int platform_device_add_resources(struct 
> platform_device
> *pdev,
>                                        unsigned int num);
>  extern int platform_device_add_data(struct platform_device *pdev,
>                                   const void *data, size_t size);
> +void platform_device_set_of_node(struct platform_device *pdev,
> +                              struct device_node *np);
>  extern int platform_device_add(struct platform_device *pdev);
>  extern void platform_device_del(struct platform_device *pdev);
>  extern void platform_device_put(struct platform_device *pdev);

Reply via email to