[PATCH 4/4] watchdog: factor out device registration into common class code
Ahmad Fatoum
a.fatoum at pengutronix.de
Tue Jul 16 04:57:11 PDT 2024
Watchdog devices are called wdog[0-...], unless a DT alias is available
and no device had been registered with that name.
This is a useful scheme for other devices as well, so move it to a
generic place for reuse.
Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
drivers/base/class.c | 38 ++++++++++++++++++++++++++++++++++++++
drivers/watchdog/wd_core.c | 25 +++----------------------
include/device.h | 4 ++++
3 files changed, 45 insertions(+), 22 deletions(-)
diff --git a/drivers/base/class.c b/drivers/base/class.c
index 5fbbae0ff025..d0765e496f9e 100644
--- a/drivers/base/class.c
+++ b/drivers/base/class.c
@@ -17,6 +17,44 @@ int class_add_device(struct class *class, struct device *dev)
return 0;
}
+static int __class_register_device(struct device *class_dev, const char *name, int id)
+{
+ class_dev->id = id;
+ dev_set_name(class_dev, name);
+
+ return register_device(class_dev);
+}
+
+int class_register_device(struct class *class,
+ struct device *class_dev,
+ const char *name)
+{
+ struct device *parent = class_dev->parent;
+ const char *alias = NULL;
+ int ret = 0;
+
+ if (dev_of_node(parent))
+ alias = of_alias_get(parent->of_node);
+
+ if (alias)
+ ret = __class_register_device(class_dev, alias, DEVICE_ID_SINGLE);
+
+ if (!alias || ret)
+ ret = __class_register_device(class_dev, name, DEVICE_ID_DYNAMIC);
+
+ if (ret)
+ return ret;
+
+ class_add_device(class, class_dev);
+ return 0;
+}
+
+void class_unregister_device(struct device *class_dev)
+{
+ list_del(&class_dev->class_list);
+ unregister_device(class_dev);
+}
+
void class_remove_device(struct class *class, struct device *dev)
{
list_del(&dev->class_list);
diff --git a/drivers/watchdog/wd_core.c b/drivers/watchdog/wd_core.c
index 44f2b6da8cf2..adb4c639097e 100644
--- a/drivers/watchdog/wd_core.c
+++ b/drivers/watchdog/wd_core.c
@@ -119,15 +119,6 @@ static void watchdog_register_poller(struct watchdog *wd)
NULL, &wd->poller_enable, wd);
}
-static int watchdog_register_dev(struct watchdog *wd, const char *name, int id)
-{
- wd->dev.parent = wd->hwdev;
- wd->dev.id = id;
- dev_set_name(&wd->dev, name);
-
- return register_device(&wd->dev);
-}
-
/**
* dev_get_watchdog_priority() - get a device's desired watchdog priority
* @dev: The device, which device_node to read the property from
@@ -187,18 +178,10 @@ static struct restart_handler restart_handler = {
int watchdog_register(struct watchdog *wd)
{
- const char *alias = NULL;
- int ret = 0;
-
- if (wd->hwdev)
- alias = of_alias_get(wd->hwdev->of_node);
-
- if (alias)
- ret = watchdog_register_dev(wd, alias, DEVICE_ID_SINGLE);
-
- if (!alias || ret)
- ret = watchdog_register_dev(wd, "wdog", DEVICE_ID_DYNAMIC);
+ int ret;
+ wd->dev.parent = wd->hwdev;
+ ret = class_register_device(&watchdog_class, &wd->dev, "wdog");
if (ret)
return ret;
@@ -239,8 +222,6 @@ int watchdog_register(struct watchdog *wd)
dev_warn(&wd->dev, "failed to register restart handler\n");
}
- class_add_device(&watchdog_class, &wd->dev);
-
pr_debug("registering watchdog %s with priority %d\n", watchdog_name(wd),
wd->priority);
diff --git a/include/device.h b/include/device.h
index 4c76da2f82dd..5d3923786167 100644
--- a/include/device.h
+++ b/include/device.h
@@ -128,6 +128,10 @@ void class_register(struct class *class);
int class_add_device(struct class *class, struct device *dev);
void class_remove_device(struct class *class, struct device *dev);
+int class_register_device(struct class *class, struct device *class_dev,
+ const char *name);
+void class_unregister_device(struct device *class_dev);
+
extern struct list_head class_list;
#define class_for_each_device(class, dev) list_for_each_entry((dev), &(class)->devices, class_list)
#define class_for_each(class) list_for_each_entry((class), &class_list, list)
--
2.39.2
More information about the barebox
mailing list