]> git.karo-electronics.de Git - linux-beck.git/commitdiff
iio:trigger: add resource managed (un)register
authorGregor Boirie <gregor.boirie@parrot.com>
Fri, 2 Sep 2016 18:47:54 +0000 (20:47 +0200)
committerJonathan Cameron <jic23@kernel.org>
Sun, 4 Sep 2016 14:42:37 +0000 (15:42 +0100)
Add resource managed devm_iio_trigger_register() and
devm_iio_triger_unregister() to automatically clean up registered triggers
allocated by IIO drivers, thus leading to simplified IIO drivers code.

Signed-off-by: Gregor Boirie <gregor.boirie@parrot.com>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>
Documentation/driver-model/devres.txt
drivers/iio/industrialio-trigger.c
include/linux/iio/trigger.h

index b0d775d28e97bebe4875385530a26f0335f90b29..6a2138a7c3ff65301ff1a4282834f13f420f0949 100644 (file)
@@ -268,6 +268,8 @@ IIO
   devm_iio_kfifo_free()
   devm_iio_trigger_alloc()
   devm_iio_trigger_free()
+  devm_iio_trigger_register()
+  devm_iio_trigger_unregister()
   devm_iio_channel_get()
   devm_iio_channel_release()
   devm_iio_channel_get_all()
index 3dde81e57e97d9e7f1e8962ba9f9882150b250fd..ba584b5e71f43dc2deca0714dde14069e7944d3d 100644 (file)
@@ -642,6 +642,65 @@ void devm_iio_trigger_free(struct device *dev, struct iio_trigger *iio_trig)
 }
 EXPORT_SYMBOL_GPL(devm_iio_trigger_free);
 
+static void devm_iio_trigger_unreg(struct device *dev, void *res)
+{
+       iio_trigger_unregister(*(struct iio_trigger **)res);
+}
+
+/**
+ * devm_iio_trigger_register - Resource-managed iio_trigger_register()
+ * @dev:       device this trigger was allocated for
+ * @trig_info: trigger to register
+ *
+ * Managed iio_trigger_register().  The IIO trigger registered with this
+ * function is automatically unregistered on driver detach. This function
+ * calls iio_trigger_register() internally. Refer to that function for more
+ * information.
+ *
+ * If an iio_trigger registered with this function needs to be unregistered
+ * separately, devm_iio_trigger_unregister() must be used.
+ *
+ * RETURNS:
+ * 0 on success, negative error number on failure.
+ */
+int devm_iio_trigger_register(struct device *dev, struct iio_trigger *trig_info)
+{
+       struct iio_trigger **ptr;
+       int ret;
+
+       ptr = devres_alloc(devm_iio_trigger_unreg, sizeof(*ptr), GFP_KERNEL);
+       if (!ptr)
+               return -ENOMEM;
+
+       *ptr = trig_info;
+       ret = iio_trigger_register(trig_info);
+       if (!ret)
+               devres_add(dev, ptr);
+       else
+               devres_free(ptr);
+
+       return ret;
+}
+EXPORT_SYMBOL_GPL(devm_iio_trigger_register);
+
+/**
+ * devm_iio_trigger_unregister - Resource-managed iio_trigger_unregister()
+ * @dev:       device this iio_trigger belongs to
+ * @trig_info: the trigger associated with the device
+ *
+ * Unregister trigger registered with devm_iio_trigger_register().
+ */
+void devm_iio_trigger_unregister(struct device *dev,
+                                struct iio_trigger *trig_info)
+{
+       int rc;
+
+       rc = devres_release(dev, devm_iio_trigger_unreg, devm_iio_trigger_match,
+                           trig_info);
+       WARN_ON(rc);
+}
+EXPORT_SYMBOL_GPL(devm_iio_trigger_unregister);
+
 void iio_device_register_trigger_consumer(struct iio_dev *indio_dev)
 {
        indio_dev->groups[indio_dev->groupcounter++] =
index a122bdd4076cad73cbf6e19283b41401fde92c70..f0890a5abf13dc024f9cc91248bf6550b2768baf 100644 (file)
@@ -125,12 +125,18 @@ static inline void *iio_trigger_get_drvdata(struct iio_trigger *trig)
  **/
 int iio_trigger_register(struct iio_trigger *trig_info);
 
+int devm_iio_trigger_register(struct device *dev,
+                             struct iio_trigger *trig_info);
+
 /**
  * iio_trigger_unregister() - unregister a trigger from the core
  * @trig_info: trigger to be unregistered
  **/
 void iio_trigger_unregister(struct iio_trigger *trig_info);
 
+void devm_iio_trigger_unregister(struct device *dev,
+                                struct iio_trigger *trig_info);
+
 /**
  * iio_trigger_set_immutable() - set an immutable trigger on destination
  *