Triggers
-
struct iio_trigger — industrial I/O trigger device
-
devm_iio_trigger_alloc()— Resource-managed iio_trigger_alloc -
devm_iio_trigger_register()— Resource-managed iio_trigger_register iio_trigger_unregister -
iio_trigger_validate_own_device()— Check if a trigger and IIO device belong to the same device
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/trigger{Y}/*, 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:device{X}/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 thecurrent_triggerfile.
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 #
- struct iio_trigger_ops — operations structure for an iio_trigger.
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
;
Members
set_trigger_state: switch on/off the trigger on demandreenable: 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
;
Members
ops: [DRIVER] operations structureowner: [INTERN] owner of this driver moduleid: [INTERN] unique id numbername: [DRIVER] unique namedev: [DRIVER] associated device (if relevant)list: [INTERN] used in maintenance of global trigger listalloc_list: [DRIVER] used for driver specific trigger listuse_count: [INTERN] 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.reenable_work: [INTERN] work item used to ensure reenable can sleep.
iio_trigger_set_drvdata() #
void Set trigger driver data
Parameters
struct iio_trigger *trig: IIO trigger structurevoid *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().
iio_trigger_get_drvdata() #
void * Get trigger driver data
Parameters
struct iio_trigger *trig: IIO trigger structure
Description
Returns the data previously set with iio_trigger_set_drvdata()
iio_trigger_register() #
int register a trigger with the IIO core
Parameters
struct iio_trigger *trig_info: trigger to be registered
iio_trigger_unregister() #
void unregister a trigger from the core
Parameters
struct iio_trigger *trig_info: trigger to be unregistered
iio_trigger_set_immutable() #
int set an immutable trigger on destination
Parameters
struct iio_dev *indio_dev: IIO device structure containing the devicestruct iio_trigger *trig: trigger to assign to device
iio_trigger_using_own() #
bool tells us if we use our own HW trigger ourselves
Parameters
struct iio_dev *indio_dev: device to check
iio_trigger_poll() #
void Call the IRQ trigger handler of the consumers
Parameters
struct iio_trigger *trig: trigger which occurred
Description
This function should only be called from a hard IRQ context.
iio_trigger_poll_nested() #
void Call the threaded trigger handler of the consumers
Parameters
struct iio_trigger *trig: trigger which occurred
Description
This function should only be called from a kernel thread context.
__iio_trigger_alloc() #
struct iio_trigger * Allocate a trigger
Parameters
struct device *parent: Device to allocate iio_trigger forstruct module *this_mod: module allocating the triggerconst 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
Return
Pointer to allocated iio_trigger on success, NULL on failure.
__devm_iio_trigger_alloc() #
struct iio_trigger * Resource-managed iio_trigger_alloc() Managed iio_trigger_alloc. iio_trigger allocated with this function is automatically freed on driver detach.
Parameters
struct device *parent: Device to allocate iio_trigger forstruct module *this_mod: module allocating the triggerconst 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
Return
Pointer to allocated iio_trigger on success, NULL on failure.
devm_iio_trigger_register() #
int Resource-managed iio_trigger_register()
Parameters
struct device *dev: device this trigger was allocated forstruct 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.
Return
0 on success, negative error number on failure.
iio_validate_own_trigger() #
int Check if a trigger and IIO device belong to the same device
Parameters
struct iio_dev *idev: the IIO device to checkstruct iio_trigger *trig: the IIO trigger to check
Description
This function can be used as the validate_trigger 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.
iio_trigger_validate_own_device() #
int Check if a trigger and IIO device belong to the same device
Parameters
struct iio_trigger *trig: The IIO trigger to checkstruct 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.