Support triggered events.
This is useful for chips that don't have their own interrupt sources.
It allows to use generic/standalone iio triggers for those drivers.
Signed-off-by: Vladimir Barinov <vladimir.barinov@cogentembedded.com>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>
        This value controls the maximum number of consumers that a
        given trigger may handle. Default is 2.
 
+config IIO_TRIGGERED_EVENT
+       tristate
+       select IIO_TRIGGER
+       help
+         Provides helper functions for setting up triggered events.
+
 source "drivers/iio/accel/Kconfig"
 source "drivers/iio/adc/Kconfig"
 source "drivers/iio/amplifiers/Kconfig"
 
 industrialio-$(CONFIG_IIO_BUFFER) += industrialio-buffer.o
 industrialio-$(CONFIG_IIO_TRIGGER) += industrialio-trigger.o
 
+obj-$(CONFIG_IIO_TRIGGERED_EVENT) += industrialio-triggered-event.o
+
 obj-y += accel/
 obj-y += adc/
 obj-y += amplifiers/
 
 static void iio_dev_release(struct device *device)
 {
        struct iio_dev *indio_dev = dev_to_iio_dev(device);
-       if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
+       if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED))
                iio_device_unregister_trigger_consumer(indio_dev);
        iio_device_unregister_eventset(indio_dev);
        iio_device_unregister_sysfs(indio_dev);
                        "Failed to register event set\n");
                goto error_free_sysfs;
        }
-       if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
+       if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED))
                iio_device_register_trigger_consumer(indio_dev);
 
        if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
 
 
        indio_dev->trig = trig;
 
-       if (oldtrig)
+       if (oldtrig) {
+               if (indio_dev->modes & INDIO_EVENT_TRIGGERED)
+                       iio_trigger_detach_poll_func(oldtrig,
+                                                    indio_dev->pollfunc_event);
                iio_trigger_put(oldtrig);
-       if (indio_dev->trig)
+       }
+       if (indio_dev->trig) {
                iio_trigger_get(indio_dev->trig);
+               if (indio_dev->modes & INDIO_EVENT_TRIGGERED)
+                       iio_trigger_attach_poll_func(indio_dev->trig,
+                                                    indio_dev->pollfunc_event);
+       }
 
        return len;
 }
 
--- /dev/null
+/*
+ * Copyright (C) 2015 Cogent Embedded, Inc.
+ *
+ * This program is free software; you can redistribute  it and/or modify it
+ * under  the terms of  the GNU General  Public License as published by the
+ * Free Software Foundation;  either version 2 of the  License, or (at your
+ * option) any later version.
+ */
+
+#include <linux/kernel.h>
+#include <linux/export.h>
+#include <linux/module.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/triggered_event.h>
+#include <linux/iio/trigger_consumer.h>
+
+/**
+ * iio_triggered_event_setup() - Setup pollfunc_event for triggered event
+ * @indio_dev: IIO device structure
+ * @h:         Function which will be used as pollfunc_event top half
+ * @thread:    Function which will be used as pollfunc_event bottom half
+ *
+ * This function combines some common tasks which will normally be performed
+ * when setting up a triggered event. It will allocate the pollfunc_event and
+ * set mode to use it for triggered event.
+ *
+ * Before calling this function the indio_dev structure should already be
+ * completely initialized, but not yet registered. In practice this means that
+ * this function should be called right before iio_device_register().
+ *
+ * To free the resources allocated by this function call
+ * iio_triggered_event_cleanup().
+ */
+int iio_triggered_event_setup(struct iio_dev *indio_dev,
+                             irqreturn_t (*h)(int irq, void *p),
+                             irqreturn_t (*thread)(int irq, void *p))
+{
+       indio_dev->pollfunc_event = iio_alloc_pollfunc(h,
+                                                      thread,
+                                                      IRQF_ONESHOT,
+                                                      indio_dev,
+                                                      "%s_consumer%d",
+                                                      indio_dev->name,
+                                                      indio_dev->id);
+       if (indio_dev->pollfunc_event == NULL)
+               return -ENOMEM;
+
+       /* Flag that events polling is possible */
+       indio_dev->modes |= INDIO_EVENT_TRIGGERED;
+
+       return 0;
+}
+EXPORT_SYMBOL(iio_triggered_event_setup);
+
+/**
+ * iio_triggered_event_cleanup() - Free resources allocated by iio_triggered_event_setup()
+ * @indio_dev: IIO device structure
+ */
+void iio_triggered_event_cleanup(struct iio_dev *indio_dev)
+{
+       indio_dev->modes &= ~INDIO_EVENT_TRIGGERED;
+       iio_dealloc_pollfunc(indio_dev->pollfunc_event);
+}
+EXPORT_SYMBOL(iio_triggered_event_cleanup);
+
+MODULE_AUTHOR("Vladimir Barinov");
+MODULE_DESCRIPTION("IIO helper functions for setting up triggered events");
+MODULE_LICENSE("GPL");
 
 #define INDIO_BUFFER_TRIGGERED         0x02
 #define INDIO_BUFFER_SOFTWARE          0x04
 #define INDIO_BUFFER_HARDWARE          0x08
+#define INDIO_EVENT_TRIGGERED          0x10
 
 #define INDIO_ALL_BUFFER_MODES                                 \
        (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE | INDIO_BUFFER_SOFTWARE)
  * @scan_index_timestamp:[INTERN] cache of the index to the timestamp
  * @trig:              [INTERN] current device trigger (buffer modes)
  * @pollfunc:          [DRIVER] function run on trigger being received
+ * @pollfunc_event:    [DRIVER] function run on events trigger being received
  * @channels:          [DRIVER] channel specification structure table
  * @num_channels:      [DRIVER] number of channels specified in @channels.
  * @channel_attr_list: [INTERN] keep track of automatically created channel
        unsigned                        scan_index_timestamp;
        struct iio_trigger              *trig;
        struct iio_poll_func            *pollfunc;
+       struct iio_poll_func            *pollfunc_event;
 
        struct iio_chan_spec const      *channels;
        int                             num_channels;
 
--- /dev/null
+#ifndef _LINUX_IIO_TRIGGERED_EVENT_H_
+#define _LINUX_IIO_TRIGGERED_EVENT_H_
+
+#include <linux/interrupt.h>
+
+int iio_triggered_event_setup(struct iio_dev *indio_dev,
+       irqreturn_t (*h)(int irq, void *p),
+       irqreturn_t (*thread)(int irq, void *p));
+void iio_triggered_event_cleanup(struct iio_dev *indio_dev);
+
+#endif