In many situations it is useful for a driver to be able to capture data based on some external event (trigger) as opposed to periodically polling for data. An IIO trigger can be provided by a device driver that also has an IIO device based on hardware generated events (e.g. data ready or threshold exceeded) or provided by a separate driver from an independent interrupt source (e.g. GPIO line connected to some external system, timer interrupt or user space writing a specific file in sysfs). A trigger may initiate data capture for a number of sensors and also it may be completely unrelated to the sensor itself.

IIO trigger sysfs interface

There are two locations in sysfs related to triggers:

  • /sys/bus/iio/devices/triggerY/*, this file is created once an IIO trigger is registered with the IIO core and corresponds to trigger with index Y. Because triggers can be very different depending on type there are few standard attributes that we can describe here:
    • name, trigger name that can be later used for association with a device.
    • sampling_frequency, some timer based triggers use this attribute to specify the frequency for trigger calls.
  • /sys/bus/iio/devices/iio:deviceX/trigger/*, this directory is created once the device supports a triggered buffer. We can associate a trigger with our device by writing the trigger’s name in the current_trigger file.

IIO trigger setup

Let’s see a simple example of how to setup a trigger to be used by a driver:

struct iio_trigger_ops trigger_ops = {
    .set_trigger_state = sample_trigger_state,
    .validate_device = sample_validate_device,

struct iio_trigger *trig;

/* first, allocate memory for our trigger */
trig = iio_trigger_alloc(dev, "trig-%s-%d", name, idx);

/* setup trigger operations field */
trig->ops = &trigger_ops;

/* now register the trigger with the IIO core */

IIO trigger ops

Notice that a trigger has a set of operations attached:

  • set_trigger_state, switch the trigger on/off on demand.
  • validate_device, function to validate the device when the current trigger gets changed.

More details

struct iio_trigger_ops

operations structure for an iio_trigger.


struct iio_trigger_ops {
  struct module * owner;
  int (* set_trigger_state) (struct iio_trigger *trig, bool state);
  int (* try_reenable) (struct iio_trigger *trig);
  int (* validate_device) (struct iio_trigger *trig,struct iio_dev *indio_dev);


used to monitor usage count of the trigger.
switch on/off the trigger on demand
function to reenable the trigger when the use count is zero (may be NULL)
function to validate the device when the current trigger gets changed.


This is typically static const within a driver and shared by instances of a given device.

struct iio_trigger

industrial I/O trigger device


struct iio_trigger {
  const struct iio_trigger_ops * ops;
  int id;
  const char * name;
  struct device dev;
  struct list_head list;
  struct list_head alloc_list;
  atomic_t use_count;
  struct irq_chip subirq_chip;
  int subirq_base;
  struct iio_subirq subirqs;
  unsigned long pool;
  struct mutex pool_lock;
  bool attached_own_device;


[DRIVER] operations structure
[INTERN] unique id number
[DRIVER] unique name
[DRIVER] associated device (if relevant)
[INTERN] used in maintenance of global trigger list
[DRIVER] used for driver specific trigger list
use count for the trigger
[INTERN] associate ‘virtual’ irq chip.
[INTERN] base number for irqs provided by trigger.
[INTERN] information about the ‘child’ irqs.
[INTERN] bitmap of irqs currently in use.
[INTERN] protection of the irq pool.
[INTERN] if we are using our own device as trigger, i.e. if we registered a poll function to the same device as the one providing the trigger.
void iio_trigger_set_drvdata(struct iio_trigger * trig, void * data)

Set trigger driver data


struct iio_trigger * trig
IIO trigger structure
void * data
Driver specific data


Allows to attach an arbitrary pointer to an IIO trigger, which can later be retrieved by iio_trigger_get_drvdata().

void * iio_trigger_get_drvdata(struct iio_trigger * trig)

Get trigger driver data


struct iio_trigger * trig
IIO trigger structure


Returns the data previously set with iio_trigger_set_drvdata()

int iio_trigger_register(struct iio_trigger * trig_info)

register a trigger with the IIO core


struct iio_trigger * trig_info
trigger to be registered
void iio_trigger_unregister(struct iio_trigger * trig_info)

unregister a trigger from the core


struct iio_trigger * trig_info
trigger to be unregistered
int iio_trigger_set_immutable(struct iio_dev * indio_dev, struct iio_trigger * trig)

set an immutable trigger on destination


struct iio_dev * indio_dev
IIO device structure containing the device
struct iio_trigger * trig
trigger to assign to device
void iio_trigger_poll(struct iio_trigger * trig)

called on a trigger occurring


struct iio_trigger * trig
trigger which occurred


Typically called in relevant hardware interrupt handler.

bool iio_trigger_using_own(struct iio_dev * indio_dev)

tells us if we use our own HW trigger ourselves


struct iio_dev * indio_dev
device to check
struct iio_trigger * devm_iio_trigger_alloc(struct device * dev, const char * fmt, ...)

Resource-managed iio_trigger_alloc()


struct device * dev
Device to allocate iio_trigger for
const char * fmt
trigger name format. If it includes format specifiers, the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers.
variable arguments


Managed iio_trigger_alloc. iio_trigger allocated with this function is automatically freed on driver detach.

If an iio_trigger allocated with this function needs to be freed separately, devm_iio_trigger_free() must be used.


Pointer to allocated iio_trigger on success, NULL on failure.

void devm_iio_trigger_free(struct device * dev, struct iio_trigger * iio_trig)

Resource-managed iio_trigger_free()


struct device * dev
Device this iio_dev belongs to
struct iio_trigger * iio_trig
the iio_trigger associated with the device


Free iio_trigger allocated with devm_iio_trigger_alloc().

int devm_iio_trigger_register(struct device * dev, struct iio_trigger * trig_info)

Resource-managed iio_trigger_register()


struct device * dev
device this trigger was allocated for
struct iio_trigger * 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.


0 on success, negative error number on failure.

void devm_iio_trigger_unregister(struct device * dev, struct iio_trigger * trig_info)

Resource-managed iio_trigger_unregister()


struct device * dev
device this iio_trigger belongs to
struct iio_trigger * trig_info
the trigger associated with the device


Unregister trigger registered with devm_iio_trigger_register().

int iio_trigger_validate_own_device(struct iio_trigger * trig, struct iio_dev * indio_dev)

Check if a trigger and IIO device belong to the same device


struct iio_trigger * trig
The IIO trigger to check
struct iio_dev * indio_dev
the IIO device to check


This function can be used as the validate_device callback for triggers that can only be attached to their own device.


0 if both the trigger and the IIO device belong to the same device, -EINVAL otherwise.