mfd: Add resource managed APIs for mfd_add_devices
authorLaxman Dewangan <ldewangan@nvidia.com>
Thu, 7 Apr 2016 18:42:55 +0000 (00:12 +0530)
committerLee Jones <lee.jones@linaro.org>
Tue, 19 Apr 2016 06:54:19 +0000 (07:54 +0100)
Add resource managed API devm_mfd_add_devices() for the mfd_add_devices().

This helps in reducing code in error path as it is not required
to call mfd_remove_devices() explicitly to remove all child-devices.
In some cases, it also helps not to implement .remove() callback
which get called during driver unbind.

Signed-off-by: Laxman Dewangan <ldewangan@nvidia.com>
Signed-off-by: Lee Jones <lee.jones@linaro.org>
drivers/mfd/mfd-core.c
include/linux/mfd/core.h

index 409da01effcd6119a34dff45040bf112dc091012..4b4c1d4f32805a93e2f842e790f3332095497dda 100644 (file)
@@ -334,6 +334,44 @@ void mfd_remove_devices(struct device *parent)
 }
 EXPORT_SYMBOL(mfd_remove_devices);
 
+static void devm_mfd_dev_release(struct device *dev, void *res)
+{
+       mfd_remove_devices(dev);
+}
+
+/**
+ * devm_mfd_add_devices - Resource managed version of mfd_add_devices()
+ *
+ * Returns 0 on success or an appropriate negative error number on failure.
+ * All child-devices of the MFD will automatically be removed when it gets
+ * unbinded.
+ */
+int devm_mfd_add_devices(struct device *dev, int id,
+                        const struct mfd_cell *cells, int n_devs,
+                        struct resource *mem_base,
+                        int irq_base, struct irq_domain *domain)
+{
+       struct device **ptr;
+       int ret;
+
+       ptr = devres_alloc(devm_mfd_dev_release, sizeof(*ptr), GFP_KERNEL);
+       if (!ptr)
+               return -ENOMEM;
+
+       ret = mfd_add_devices(dev, id, cells, n_devs, mem_base,
+                             irq_base, domain);
+       if (ret < 0) {
+               devres_free(ptr);
+               return ret;
+       }
+
+       *ptr = dev;
+       devres_add(dev, ptr);
+
+       return ret;
+}
+EXPORT_SYMBOL(devm_mfd_add_devices);
+
 int mfd_clone_cell(const char *cell, const char **clones, size_t n_clones)
 {
        struct mfd_cell cell_entry;
index bc6f7e00fb3de33438759c6c88184b7385dc86b7..4a0268afe546775bbbaad8dd334a6608f58bcac2 100644 (file)
@@ -131,4 +131,8 @@ static inline int mfd_add_hotplug_devices(struct device *parent,
 
 extern void mfd_remove_devices(struct device *parent);
 
+extern int devm_mfd_add_devices(struct device *dev, int id,
+                               const struct mfd_cell *cells, int n_devs,
+                               struct resource *mem_base,
+                               int irq_base, struct irq_domain *irq_domain);
 #endif