162306a36Sopenharmony_ci======== 262306a36Sopenharmony_ciTriggers 362306a36Sopenharmony_ci======== 462306a36Sopenharmony_ci 562306a36Sopenharmony_ci* struct iio_trigger — industrial I/O trigger device 662306a36Sopenharmony_ci* :c:func:`devm_iio_trigger_alloc` — Resource-managed iio_trigger_alloc 762306a36Sopenharmony_ci* :c:func:`devm_iio_trigger_register` — Resource-managed iio_trigger_register 862306a36Sopenharmony_ci iio_trigger_unregister 962306a36Sopenharmony_ci* :c:func:`iio_trigger_validate_own_device` — Check if a trigger and IIO 1062306a36Sopenharmony_ci device belong to the same device 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_ciIn many situations it is useful for a driver to be able to capture data based 1362306a36Sopenharmony_cion some external event (trigger) as opposed to periodically polling for data. 1462306a36Sopenharmony_ciAn IIO trigger can be provided by a device driver that also has an IIO device 1562306a36Sopenharmony_cibased on hardware generated events (e.g. data ready or threshold exceeded) or 1662306a36Sopenharmony_ciprovided by a separate driver from an independent interrupt source (e.g. GPIO 1762306a36Sopenharmony_ciline connected to some external system, timer interrupt or user space writing 1862306a36Sopenharmony_cia specific file in sysfs). A trigger may initiate data capture for a number of 1962306a36Sopenharmony_cisensors and also it may be completely unrelated to the sensor itself. 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ciIIO trigger sysfs interface 2262306a36Sopenharmony_ci=========================== 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ciThere are two locations in sysfs related to triggers: 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_ci* :file:`/sys/bus/iio/devices/trigger{Y}/*`, this file is created once an 2762306a36Sopenharmony_ci IIO trigger is registered with the IIO core and corresponds to trigger 2862306a36Sopenharmony_ci with index Y. 2962306a36Sopenharmony_ci Because triggers can be very different depending on type there are few 3062306a36Sopenharmony_ci standard attributes that we can describe here: 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_ci * :file:`name`, trigger name that can be later used for association with a 3362306a36Sopenharmony_ci device. 3462306a36Sopenharmony_ci * :file:`sampling_frequency`, some timer based triggers use this attribute to 3562306a36Sopenharmony_ci specify the frequency for trigger calls. 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ci* :file:`/sys/bus/iio/devices/iio:device{X}/trigger/*`, this directory is 3862306a36Sopenharmony_ci created once the device supports a triggered buffer. We can associate a 3962306a36Sopenharmony_ci trigger with our device by writing the trigger's name in the 4062306a36Sopenharmony_ci :file:`current_trigger` file. 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_ciIIO trigger setup 4362306a36Sopenharmony_ci================= 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ciLet's see a simple example of how to setup a trigger to be used by a driver:: 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ci struct iio_trigger_ops trigger_ops = { 4862306a36Sopenharmony_ci .set_trigger_state = sample_trigger_state, 4962306a36Sopenharmony_ci .validate_device = sample_validate_device, 5062306a36Sopenharmony_ci } 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ci struct iio_trigger *trig; 5362306a36Sopenharmony_ci 5462306a36Sopenharmony_ci /* first, allocate memory for our trigger */ 5562306a36Sopenharmony_ci trig = iio_trigger_alloc(dev, "trig-%s-%d", name, idx); 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_ci /* setup trigger operations field */ 5862306a36Sopenharmony_ci trig->ops = &trigger_ops; 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_ci /* now register the trigger with the IIO core */ 6162306a36Sopenharmony_ci iio_trigger_register(trig); 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_ciIIO trigger ops 6462306a36Sopenharmony_ci=============== 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ci* struct iio_trigger_ops — operations structure for an iio_trigger. 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ciNotice that a trigger has a set of operations attached: 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ci* :file:`set_trigger_state`, switch the trigger on/off on demand. 7162306a36Sopenharmony_ci* :file:`validate_device`, function to validate the device when the current 7262306a36Sopenharmony_ci trigger gets changed. 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ciMore details 7562306a36Sopenharmony_ci============ 7662306a36Sopenharmony_ci.. kernel-doc:: include/linux/iio/trigger.h 7762306a36Sopenharmony_ci.. kernel-doc:: drivers/iio/industrialio-trigger.c 7862306a36Sopenharmony_ci :export: 79