Subject: [PATCH 01/30] include: linux: platform_device: more helpers for declaring platform drivers

From: Enrico Weigelt <[email protected]>

Add more helper macros for trivial driver init cases, similar to the
already existing module_platform_driver()+friends - now for those which
are initialized at other stages. Lots of drivers couldn't use the existing
macros, as they need to be called at different init stages, eg. subsys,
postcore, arch.

This helps to further reduce driver init boilerplate.

Signed-off-by: Enrico Weigelt <[email protected]>
---
include/linux/platform_device.h | 51 +++++++++++++++++++++++++++++++++++++++++
1 file changed, 51 insertions(+)

diff --git a/include/linux/platform_device.h b/include/linux/platform_device.h
index beb25f2..5f3a967 100644
--- a/include/linux/platform_device.h
+++ b/include/linux/platform_device.h
@@ -259,6 +259,57 @@ static inline void platform_set_drvdata(struct platform_device *pdev,
} \
module_exit(__platform_driver##_exit);

+/* postcore_platform_driver() - Helper macro for drivers that don't do
+ * anything special in module init/exit. This eliminates a lot of
+ * boilerplate. Each module may only use this macro once, and
+ * calling it replaces postcore_initcall() and module_exit()
+ */
+#define postcore_platform_driver(__platform_driver) \
+static int __init __platform_driver##_init(void) \
+{ \
+ return platform_driver_register(&(__platform_driver)); \
+} \
+postcore_initcall(__platform_driver##_init); \
+static void __exit __platform_driver##_exit(void) \
+{ \
+ platform_driver_unregister(&(__platform_driver)); \
+} \
+module_exit(__platform_driver##_exit);
+
+/* subsys_platform_driver() - Helper macro for drivers that don't do
+ * anything special in module init/exit. This eliminates a lot of
+ * boilerplate. Each module may only use this macro once, and
+ * calling it replaces subsys_initcall() and module_exit()
+ */
+#define subsys_platform_driver(__platform_driver) \
+static int __init __platform_driver##_init(void) \
+{ \
+ return platform_driver_register(&(__platform_driver)); \
+} \
+subsys_initcall(__platform_driver##_init); \
+static void __exit __platform_driver##_exit(void) \
+{ \
+ platform_driver_unregister(&(__platform_driver)); \
+} \
+module_exit(__platform_driver##_exit);
+
+/* arch_platform_driver() - Helper macro for drivers that don't do
+ * anything special in module init/exit. This eliminates a lot of
+ * boilerplate. Each module may only use this macro once, and
+ * calling it replaces arch_initcall() and module_exit()
+ */
+#define arch_platform_driver(__platform_driver) \
+static int __init __platform_driver##_init(void) \
+{ \
+ return platform_driver_register(&(__platform_driver)); \
+} \
+arch_initcall(__platform_driver##_init); \
+static void __exit __platform_driver##_exit(void) \
+{ \
+ platform_driver_unregister(&(__platform_driver)); \
+} \
+module_exit(__platform_driver##_exit);
+
/* builtin_platform_driver_probe() - Helper macro for drivers that don't do
* anything special in device init. This eliminates some boilerplate. Each
* driver may only use this macro once, and using it replaces device_initcall.
--
1.9.1


Subject: [PATCH 18/30] drivers: gpio: wm831x: use subsys_platform_driver()

From: Enrico Weigelt <[email protected]>

Reduce driver init boilerplate by using the new
subsys_platform_driver() macro.

Signed-off-by: Enrico Weigelt <[email protected]>
---
drivers/gpio/gpio-wm831x.c | 13 +------------
1 file changed, 1 insertion(+), 12 deletions(-)

diff --git a/drivers/gpio/gpio-wm831x.c b/drivers/gpio/gpio-wm831x.c
index a3a32a7..324f811 100644
--- a/drivers/gpio/gpio-wm831x.c
+++ b/drivers/gpio/gpio-wm831x.c
@@ -296,18 +296,7 @@ static int wm831x_gpio_probe(struct platform_device *pdev)
.driver.name = "wm831x-gpio",
.probe = wm831x_gpio_probe,
};
-
-static int __init wm831x_gpio_init(void)
-{
- return platform_driver_register(&wm831x_gpio_driver);
-}
-subsys_initcall(wm831x_gpio_init);
-
-static void __exit wm831x_gpio_exit(void)
-{
- platform_driver_unregister(&wm831x_gpio_driver);
-}
-module_exit(wm831x_gpio_exit);
+subsys_platform_driver(wm831x_gpio_driver);

MODULE_AUTHOR("Mark Brown <[email protected]>");
MODULE_DESCRIPTION("GPIO interface for WM831x PMICs");
--
1.9.1

Subject: [PATCH 15/30] drivers: gpio: tps6586x: use subsys_platform_driver()

From: Enrico Weigelt <[email protected]>

Reduce driver init boilerplate by using the new
subsys_platform_driver() macro.

Signed-off-by: Enrico Weigelt <[email protected]>
---
drivers/gpio/gpio-tps6586x.c | 7 +------
1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/drivers/gpio/gpio-tps6586x.c b/drivers/gpio/gpio-tps6586x.c
index 9b6cc74..8f75718 100644
--- a/drivers/gpio/gpio-tps6586x.c
+++ b/drivers/gpio/gpio-tps6586x.c
@@ -122,9 +122,4 @@ static int tps6586x_gpio_probe(struct platform_device *pdev)
.driver.name = "tps6586x-gpio",
.probe = tps6586x_gpio_probe,
};
-
-static int __init tps6586x_gpio_init(void)
-{
- return platform_driver_register(&tps6586x_gpio_driver);
-}
-subsys_initcall(tps6586x_gpio_init);
+subsys_platform_driver(tps6586x_gpio_driver);
--
1.9.1

Subject: [PATCH 10/30] drivers: gpio: spear-spics: use subsys_platform_driver()

From: Enrico Weigelt <[email protected]>

Reduce driver init boilerplate by using the new
subsys_platform_driver() macro.

Signed-off-by: Enrico Weigelt <[email protected]>
---
drivers/gpio/gpio-spear-spics.c | 7 +------
1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/drivers/gpio/gpio-spear-spics.c b/drivers/gpio/gpio-spear-spics.c
index 6eca531..efb99f0 100644
--- a/drivers/gpio/gpio-spear-spics.c
+++ b/drivers/gpio/gpio-spear-spics.c
@@ -189,9 +189,4 @@ static int spics_gpio_probe(struct platform_device *pdev)
.of_match_table = spics_gpio_of_match,
},
};
-
-static int __init spics_gpio_init(void)
-{
- return platform_driver_register(&spics_gpio_driver);
-}
-subsys_initcall(spics_gpio_init);
+subsys_platform_driver(spics_gpio_driver);
--
1.9.1

2019-06-18 08:58:03

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH 01/30] include: linux: platform_device: more helpers for declaring platform drivers

On Mon, Jun 17, 2019 at 08:40:42PM +0200, Enrico Weigelt, metux IT consult wrote:
> From: Enrico Weigelt <[email protected]>
>
> Add more helper macros for trivial driver init cases, similar to the
> already existing module_platform_driver()+friends - now for those which
> are initialized at other stages. Lots of drivers couldn't use the existing
> macros, as they need to be called at different init stages, eg. subsys,
> postcore, arch.
>
> This helps to further reduce driver init boilerplate.

> +/* postcore_platform_driver() - Helper macro for drivers that don't do
> + * anything special in module init/exit. This eliminates a lot of
> + * boilerplate. Each module may only use this macro once, and
> + * calling it replaces postcore_initcall() and module_exit()
> + */

Perhaps you meant kernel-doc format?

--
With Best Regards,
Andy Shevchenko


2019-06-25 11:46:12

by Linus Walleij

[permalink] [raw]
Subject: Re: [PATCH 01/30] include: linux: platform_device: more helpers for declaring platform drivers

On Mon, Jun 17, 2019 at 8:41 PM Enrico Weigelt, metux IT consult
<[email protected]> wrote:

> From: Enrico Weigelt <[email protected]>
>
> Add more helper macros for trivial driver init cases, similar to the
> already existing module_platform_driver()+friends - now for those which
> are initialized at other stages. Lots of drivers couldn't use the existing
> macros, as they need to be called at different init stages, eg. subsys,
> postcore, arch.
>
> This helps to further reduce driver init boilerplate.
>
> Signed-off-by: Enrico Weigelt <[email protected]>

You need to send this to Greg as device core maintainer.
Possibly to Rafael as well, he did a very intersting rework
on device dependencies with device links.

While in general I agree that this diets down a lot of duplicate
code that we have done the same way over and over, there
is the issue that we don't want any drivers to do this mockery
and instead use deferred probe and ultimately just probe in the
right order.

I think device links were supposed to fix this up, but it indeed
assumes that you know of these dependencies before you
start probing the first driver, and often you do not, unless the
hardware description explicitly encodes that. And that is one
big problem.

If we should do this, device core changes must be merged or
explicitly ACKed first.

Yours,
Linus Walleij

2019-06-26 06:14:42

by Uwe Kleine-König

[permalink] [raw]
Subject: Re: [PATCH 01/30] include: linux: platform_device: more helpers for declaring platform drivers

Hello,

On Mon, Jun 17, 2019 at 08:40:42PM +0200, Enrico Weigelt, metux IT consult wrote:
> From: Enrico Weigelt <[email protected]>
>
> Add more helper macros for trivial driver init cases, similar to the
> already existing module_platform_driver()+friends - now for those which
> are initialized at other stages. Lots of drivers couldn't use the existing
> macros, as they need to be called at different init stages, eg. subsys,
> postcore, arch.
>
> This helps to further reduce driver init boilerplate.
>
> Signed-off-by: Enrico Weigelt <[email protected]>
> ---
> include/linux/platform_device.h | 51 +++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 51 insertions(+)
>
> diff --git a/include/linux/platform_device.h b/include/linux/platform_device.h
> index beb25f2..5f3a967 100644
> --- a/include/linux/platform_device.h
> +++ b/include/linux/platform_device.h
> @@ -259,6 +259,57 @@ static inline void platform_set_drvdata(struct platform_device *pdev,
> } \
> module_exit(__platform_driver##_exit);
>
> +/* postcore_platform_driver() - Helper macro for drivers that don't do
> + * anything special in module init/exit. This eliminates a lot of
> + * boilerplate. Each module may only use this macro once, and
> + * calling it replaces postcore_initcall() and module_exit()
> + */
> +#define postcore_platform_driver(__platform_driver) \
> +static int __init __platform_driver##_init(void) \
> +{ \
> + return platform_driver_register(&(__platform_driver)); \
> +} \
> +postcore_initcall(__platform_driver##_init); \
> +static void __exit __platform_driver##_exit(void) \
> +{ \
> + platform_driver_unregister(&(__platform_driver)); \
> +} \
> +module_exit(__platform_driver##_exit);
> +
> +/* subsys_platform_driver() - Helper macro for drivers that don't do
> + * anything special in module init/exit. This eliminates a lot of
> + * boilerplate. Each module may only use this macro once, and
> + * calling it replaces subsys_initcall() and module_exit()
> + */
> +#define subsys_platform_driver(__platform_driver) \
> +static int __init __platform_driver##_init(void) \
> +{ \
> + return platform_driver_register(&(__platform_driver)); \
> +} \
> +subsys_initcall(__platform_driver##_init); \
> +static void __exit __platform_driver##_exit(void) \
> +{ \
> + platform_driver_unregister(&(__platform_driver)); \
> +} \
> +module_exit(__platform_driver##_exit);

Would it make sense to do something like:

#define __module_platform_driver(__platform_driver, __initlvl) \
static int __init __platform_driver##_init(void) \
{ \
return platform_driver_register(&(__platform_driver)); \
} \
__initlvl ## _initcall(__platform_driver##_init); \
static void __exit __platform_driver##_exit(void) \
{ \
platform_driver_unregister(&(__platform_driver)); \
} \
module_exit(__platform_driver##_exit);

#define postcore_platform_driver(__platform_driver) __module_platform_driver(__platform_driver, postcore)
#define subsys_platform_driver(__platform_driver) __module_platform_driver(__platform_driver, subsys)
...

Which would be more compact and makes the difference between these
macros a bit more obvious.

Best regards
Uwe

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | http://www.pengutronix.de/ |

Subject: Re: [PATCH 01/30] include: linux: platform_device: more helpers for declaring platform drivers

On 26.06.19 08:14, Uwe Kleine-König wrote:
Hi,

> Would it make sense to do something like:
>
> #define __module_platform_driver(__platform_driver, __initlvl) \
> static int __init __platform_driver##_init(void) \
> { \
> return platform_driver_register(&(__platform_driver)); \
> } \
> __initlvl ## _initcall(__platform_driver##_init); \
> static void __exit __platform_driver##_exit(void) \
> { \
> platform_driver_unregister(&(__platform_driver)); \
> } \
> module_exit(__platform_driver##_exit);
>
> #define postcore_platform_driver(__platform_driver)
__module_platform_driver(__platform_driver, postcore)
> #define subsys_platform_driver(__platform_driver)
__module_platform_driver(__platform_driver, subsys)
> ...
>
> Which would be more compact and makes the difference between these
> macros a bit more obvious.
yeah, could do that, but not sure whether it's really good for
readability when we have so many nested macros :p

OTOH, I didn't want to touch the existing macros for now, just trim down
the actual init boilerplate, postponing further compactions for later.

--mtx

--
Enrico Weigelt, metux IT consult
Free software and Linux embedded engineering
[email protected] -- +49-151-27565287