mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] mfd: implement mfd_add_devices
@ 2022-01-14  8:47 Ahmad Fatoum
  2022-01-17  9:29 ` Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Ahmad Fatoum @ 2022-01-14  8:47 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

This makes it easier to port Linux drivers like PMICs, where a device
tree probed MFD node exists and the driver matching against it then
registers a number of MFD cell devices, which don't have their own
DT compatibles.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/base/resource.c  |  2 +-
 drivers/mfd/Makefile     |  2 ++
 drivers/mfd/core.c       | 25 +++++++++++++++++++++++++
 include/driver.h         |  2 +-
 include/linux/mfd/core.h | 23 +++++++++++++++++++++++
 5 files changed, 52 insertions(+), 2 deletions(-)
 create mode 100644 drivers/mfd/core.c
 create mode 100644 include/linux/mfd/core.h

diff --git a/drivers/base/resource.c b/drivers/base/resource.c
index d0d39620772e..0134456ffac2 100644
--- a/drivers/base/resource.c
+++ b/drivers/base/resource.c
@@ -20,7 +20,7 @@ struct device_d *device_alloc(const char *devname, int id)
 	return dev;
 }
 
-int device_add_data(struct device_d *dev, void *data, size_t size)
+int device_add_data(struct device_d *dev, const void *data, size_t size)
 {
 	free(dev->platform_data);
 
diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile
index 2bcf90078ab4..a00119a1d5f4 100644
--- a/drivers/mfd/Makefile
+++ b/drivers/mfd/Makefile
@@ -1,4 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-only
+obj-y				+= core.o
+
 obj-$(CONFIG_MFD_ACT8846)	+= act8846.o
 obj-$(CONFIG_MFD_DA9053)	+= da9053.o
 obj-$(CONFIG_MFD_DA9063)	+= da9063.o
diff --git a/drivers/mfd/core.c b/drivers/mfd/core.c
new file mode 100644
index 000000000000..fa6aa1c74a18
--- /dev/null
+++ b/drivers/mfd/core.c
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <linux/mfd/core.h>
+#include <driver.h>
+
+int mfd_add_devices(struct device_d *parent, const struct mfd_cell *cells, int n_devs)
+{
+	struct device_d *dev;
+	int ret, i;
+
+	for (i = 0; i < n_devs; i++) {
+		dev = device_alloc(cells[i].name, DEVICE_ID_DYNAMIC);
+		dev->parent = parent;
+
+		ret = platform_device_register(dev);
+		if (ret)
+			return ret;
+
+		ret = device_add_data(dev, &cells[i], sizeof(cells[i]));
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
diff --git a/include/driver.h b/include/driver.h
index 4f6d40e17c14..513b9ea794e6 100644
--- a/include/driver.h
+++ b/include/driver.h
@@ -234,7 +234,7 @@ int device_add_resources(struct device_d *dev, const struct resource *res, int n
 int device_add_resource(struct device_d *dev, const char *resname,
 		resource_size_t start, resource_size_t size, unsigned int flags);
 
-int device_add_data(struct device_d *dev, void *data, size_t size);
+int device_add_data(struct device_d *dev, const void *data, size_t size);
 
 /*
  * register a generic device
diff --git a/include/linux/mfd/core.h b/include/linux/mfd/core.h
new file mode 100644
index 000000000000..2b3b51c69e5f
--- /dev/null
+++ b/include/linux/mfd/core.h
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2006 Ian Molton
+ * Copyright (c) 2007 Dmitry Baryshkov
+ */
+
+#ifndef MFD_CORE_H
+#define MFD_CORE_H
+
+struct device_d;
+
+/*
+ * This struct describes the MFD part ("cell").
+ * After registration the copy of this structure will become the platform data
+ * of the resulting device_d
+ */
+struct mfd_cell {
+	const char		*name;
+};
+
+int mfd_add_devices(struct device_d *parent, const struct mfd_cell *cells, int n_devs);
+
+#endif
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] mfd: implement mfd_add_devices
  2022-01-14  8:47 [PATCH] mfd: implement mfd_add_devices Ahmad Fatoum
@ 2022-01-17  9:29 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2022-01-17  9:29 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

On Fri, Jan 14, 2022 at 09:47:28AM +0100, Ahmad Fatoum wrote:
> This makes it easier to port Linux drivers like PMICs, where a device
> tree probed MFD node exists and the driver matching against it then
> registers a number of MFD cell devices, which don't have their own
> DT compatibles.
> 
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
>  drivers/base/resource.c  |  2 +-
>  drivers/mfd/Makefile     |  2 ++
>  drivers/mfd/core.c       | 25 +++++++++++++++++++++++++
>  include/driver.h         |  2 +-
>  include/linux/mfd/core.h | 23 +++++++++++++++++++++++
>  5 files changed, 52 insertions(+), 2 deletions(-)
>  create mode 100644 drivers/mfd/core.c
>  create mode 100644 include/linux/mfd/core.h
> 
> diff --git a/drivers/base/resource.c b/drivers/base/resource.c
> index d0d39620772e..0134456ffac2 100644
> --- a/drivers/base/resource.c
> +++ b/drivers/base/resource.c
> @@ -20,7 +20,7 @@ struct device_d *device_alloc(const char *devname, int id)
>  	return dev;
>  }
>  
> -int device_add_data(struct device_d *dev, void *data, size_t size)
> +int device_add_data(struct device_d *dev, const void *data, size_t size)
>  {
>  	free(dev->platform_data);
>  
> diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile
> index 2bcf90078ab4..a00119a1d5f4 100644
> --- a/drivers/mfd/Makefile
> +++ b/drivers/mfd/Makefile
> @@ -1,4 +1,6 @@
>  # SPDX-License-Identifier: GPL-2.0-only
> +obj-y				+= core.o
> +
>  obj-$(CONFIG_MFD_ACT8846)	+= act8846.o
>  obj-$(CONFIG_MFD_DA9053)	+= da9053.o
>  obj-$(CONFIG_MFD_DA9063)	+= da9063.o
> diff --git a/drivers/mfd/core.c b/drivers/mfd/core.c
> new file mode 100644
> index 000000000000..fa6aa1c74a18
> --- /dev/null
> +++ b/drivers/mfd/core.c
> @@ -0,0 +1,25 @@
> +/* SPDX-License-Identifier: GPL-2.0-only */
> +
> +#include <linux/mfd/core.h>
> +#include <driver.h>
> +
> +int mfd_add_devices(struct device_d *parent, const struct mfd_cell *cells, int n_devs)
> +{
> +	struct device_d *dev;
> +	int ret, i;
> +
> +	for (i = 0; i < n_devs; i++) {
> +		dev = device_alloc(cells[i].name, DEVICE_ID_DYNAMIC);
> +		dev->parent = parent;
> +
> +		ret = platform_device_register(dev);
> +		if (ret)
> +			return ret;
> +
> +		ret = device_add_data(dev, &cells[i], sizeof(cells[i]));
> +		if (ret)
> +			return ret;

Calling platform_device_register() might result in probing the device.
You should call device_add_data() before registering the device.

Sascha

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-01-17  9:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-14  8:47 [PATCH] mfd: implement mfd_add_devices Ahmad Fatoum
2022-01-17  9:29 ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox