Triggers

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_register(trig);

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.

Definition

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);
};

Members

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

Description

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

struct iio_trigger

industrial I/O trigger device

Definition

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;
};

Members

ops
[DRIVER] operations structure
id
[INTERN] unique id number
name
[DRIVER] unique name
dev
[DRIVER] associated device (if relevant)
list
[INTERN] used in maintenance of global trigger list
alloc_list
[DRIVER] used for driver specific trigger list
use_count
use count for the trigger
subirq_chip
[INTERN] associate ‘virtual’ irq chip.
subirq_base
[INTERN] base number for irqs provided by trigger.
subirqs
[INTERN] information about the ‘child’ irqs.
pool
[INTERN] bitmap of irqs currently in use.
pool_lock
[INTERN] protection of the irq pool.
attached_own_device
[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

Parameters

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

Description

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

Parameters

struct iio_trigger * trig
IIO trigger structure

Description

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

Parameters

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

unregister a trigger from the core

Parameters

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

Parameters

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

Parameters

struct iio_trigger * trig
trigger which occurred

Description

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

Parameters

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()

Parameters

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

Description

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.

Return

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()

Parameters

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

Description

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()

Parameters

struct device * dev
device this trigger was allocated for
struct iio_trigger * trig_info
trigger to register

Description

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.

Return

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()

Parameters

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

Description

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

Parameters

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

Description

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

Return

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