mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 4/4] watchdog: factor out device registration into common class code
Date: Tue, 16 Jul 2024 13:57:11 +0200	[thread overview]
Message-ID: <20240716115711.1956907-4-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20240716115711.1956907-1-a.fatoum@pengutronix.de>

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@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




  parent reply	other threads:[~2024-07-16 11:57 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-16 11:57 [PATCH 1/4] init: mark initcalls const Ahmad Fatoum
2024-07-16 11:57 ` [PATCH 2/4] base: use initcalls instead of linker lists to register classes Ahmad Fatoum
2024-07-16 11:57 ` [PATCH 3/4] watchdog: remove needless error checking for device parameter Ahmad Fatoum
2024-07-16 11:57 ` Ahmad Fatoum [this message]
2024-07-19  6:26 ` [PATCH 1/4] init: mark initcalls const Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240716115711.1956907-4-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox