[PATCH v2 04/11] driver core: platform: Introduce platform_device_add_with_name()

From: Théo Lebrun
Date: Fri May 03 2024 - 10:23:12 EST


Public function platform_device_add() does three things:
- Name device using pdev->name and pdev->id;
- Insert resources into resource tree;
- device_add().

This implies device name must be based upon pdev->name. Add
platform_device_add_with_name() to allow caller to pick device name.
pdev->name cannot be picked freely: it might be used to match platform
driver id_table or name. This applies to MFD cells for example.

In header file, avoid breakage by aliasing platform_device_add(pdev) to
platform_device_add_with_name(pdev, NULL).

Signed-off-by: Théo Lebrun <theo.lebrun@xxxxxxxxxxx>
---
drivers/base/platform.c | 17 +++++++++++------
include/linux/platform_device.h | 12 +++++++++++-
2 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/drivers/base/platform.c b/drivers/base/platform.c
index 10c577963418..dce282cb2e50 100644
--- a/drivers/base/platform.c
+++ b/drivers/base/platform.c
@@ -648,13 +648,15 @@ int platform_device_add_data(struct platform_device *pdev, const void *data,
EXPORT_SYMBOL_GPL(platform_device_add_data);

/**
- * platform_device_add - add a platform device to device hierarchy
+ * platform_device_add_with_name - add a platform device to device hierarchy
* @pdev: platform device we're adding
+ * @devname: optional device name to assign, fallback to platform device name
*
* This is part 2 of platform_device_register(), though may be called
* separately _iff_ pdev was allocated by platform_device_alloc().
*/
-int platform_device_add(struct platform_device *pdev)
+int platform_device_add_with_name(struct platform_device *pdev,
+ const char *devname)
{
struct device *dev = &pdev->dev;
u32 i;
@@ -665,12 +667,15 @@ int platform_device_add(struct platform_device *pdev)

dev->bus = &platform_bus_type;

+ if (!devname)
+ devname = pdev->name;
+
switch (pdev->id) {
default:
- dev_set_name(dev, "%s.%d", pdev->name, pdev->id);
+ dev_set_name(dev, "%s.%d", devname, pdev->id);
break;
case PLATFORM_DEVID_NONE:
- dev_set_name(dev, "%s", pdev->name);
+ dev_set_name(dev, "%s", devname);
break;
case PLATFORM_DEVID_AUTO:
/*
@@ -683,7 +688,7 @@ int platform_device_add(struct platform_device *pdev)
return ret;
pdev->id = ret;
pdev->id_auto = true;
- dev_set_name(dev, "%s.%d.auto", pdev->name, pdev->id);
+ dev_set_name(dev, "%s.%d.auto", devname, pdev->id);
break;
}

@@ -733,7 +738,7 @@ int platform_device_add(struct platform_device *pdev)

return ret;
}
-EXPORT_SYMBOL_GPL(platform_device_add);
+EXPORT_SYMBOL_GPL(platform_device_add_with_name);

/**
* platform_device_del - remove a platform-level device
diff --git a/include/linux/platform_device.h b/include/linux/platform_device.h
index 7a41c72c1959..bc1463c83f69 100644
--- a/include/linux/platform_device.h
+++ b/include/linux/platform_device.h
@@ -229,10 +229,20 @@ 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);
-extern int platform_device_add(struct platform_device *pdev);
+extern int platform_device_add_with_name(struct platform_device *pdev,
+ const char *devname);
extern void platform_device_del(struct platform_device *pdev);
extern void platform_device_put(struct platform_device *pdev);

+/**
+ * platform_device_add - add a platform device to device hierarchy
+ * @pdev: platform device we're adding
+ */
+static inline int platform_device_add(struct platform_device *pdev)
+{
+ return platform_device_add_with_name(pdev, NULL);
+}
+
struct platform_driver {
int (*probe)(struct platform_device *);


--
2.45.0