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.
There are two locations in sysfs related to triggers:
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);
Notice that a trigger has a set of operations attached:
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
Description
This is typically static const within a driver and shared by instances of a given device.
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[CONFIG_IIO_CONSUMERS_PER_TRIGGER];
unsigned long pool[BITS_TO_LONGS(CONFIG_IIO_CONSUMERS_PER_TRIGGER)];
struct mutex pool_lock;
bool attached_own_device;
};
Members
Set trigger driver data
Parameters
Description
Allows to attach an arbitrary pointer to an IIO trigger, which can later be retrieved by iio_trigger_get_drvdata().
Get trigger driver data
Parameters
Description
Returns the data previously set with iio_trigger_set_drvdata()
register a trigger with the IIO core
Parameters
unregister a trigger from the core
Parameters
set an immutable trigger on destination
Parameters
Description
indio_dev - IIO device structure containing the device trig - trigger to assign to device
called on a trigger occurring
Parameters
Description
Typically called in relevant hardware interrupt handler.
tells us if we use our own HW trigger ourselves
Parameters
Resource-managed iio_trigger_alloc()
Parameters
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.
Resource-managed iio_trigger_free()
Parameters
Description
Free iio_trigger allocated with devm_iio_trigger_alloc().
Resource-managed iio_trigger_register()
Parameters
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.
Resource-managed iio_trigger_unregister()
Parameters
Description
Unregister trigger registered with devm_iio_trigger_register().
Check if a trigger and IIO device belong to the same device
Parameters
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.