xref: /linux/Documentation/driver-api/iio/triggers.rst (revision cbecf716ca618fd44feda6bd9a64a8179d031fc5)
149b2fd6eSJonathan Cameron========
249b2fd6eSJonathan CameronTriggers
349b2fd6eSJonathan Cameron========
449b2fd6eSJonathan Cameron
5*9303c9d5SMauro Carvalho Chehab* struct iio_trigger — industrial I/O trigger device
649b2fd6eSJonathan Cameron* :c:func:`devm_iio_trigger_alloc` — Resource-managed iio_trigger_alloc
749b2fd6eSJonathan Cameron* :c:func:`devm_iio_trigger_register` — Resource-managed iio_trigger_register
849b2fd6eSJonathan Cameron  iio_trigger_unregister
949b2fd6eSJonathan Cameron* :c:func:`iio_trigger_validate_own_device` — Check if a trigger and IIO
1049b2fd6eSJonathan Cameron  device belong to the same device
1149b2fd6eSJonathan Cameron
1249b2fd6eSJonathan CameronIn many situations it is useful for a driver to be able to capture data based
1349b2fd6eSJonathan Cameronon some external event (trigger) as opposed to periodically polling for data.
1449b2fd6eSJonathan CameronAn IIO trigger can be provided by a device driver that also has an IIO device
1549b2fd6eSJonathan Cameronbased on hardware generated events (e.g. data ready or threshold exceeded) or
1649b2fd6eSJonathan Cameronprovided by a separate driver from an independent interrupt source (e.g. GPIO
1749b2fd6eSJonathan Cameronline connected to some external system, timer interrupt or user space writing
1849b2fd6eSJonathan Camerona specific file in sysfs). A trigger may initiate data capture for a number of
1949b2fd6eSJonathan Cameronsensors and also it may be completely unrelated to the sensor itself.
2049b2fd6eSJonathan Cameron
2149b2fd6eSJonathan CameronIIO trigger sysfs interface
2249b2fd6eSJonathan Cameron===========================
2349b2fd6eSJonathan Cameron
2449b2fd6eSJonathan CameronThere are two locations in sysfs related to triggers:
2549b2fd6eSJonathan Cameron
2649b2fd6eSJonathan Cameron* :file:`/sys/bus/iio/devices/trigger{Y}/*`, this file is created once an
2749b2fd6eSJonathan Cameron  IIO trigger is registered with the IIO core and corresponds to trigger
2849b2fd6eSJonathan Cameron  with index Y.
2949b2fd6eSJonathan Cameron  Because triggers can be very different depending on type there are few
3049b2fd6eSJonathan Cameron  standard attributes that we can describe here:
3149b2fd6eSJonathan Cameron
3249b2fd6eSJonathan Cameron  * :file:`name`, trigger name that can be later used for association with a
3349b2fd6eSJonathan Cameron    device.
3449b2fd6eSJonathan Cameron  * :file:`sampling_frequency`, some timer based triggers use this attribute to
3549b2fd6eSJonathan Cameron    specify the frequency for trigger calls.
3649b2fd6eSJonathan Cameron
3749b2fd6eSJonathan Cameron* :file:`/sys/bus/iio/devices/iio:device{X}/trigger/*`, this directory is
3849b2fd6eSJonathan Cameron  created once the device supports a triggered buffer. We can associate a
3949b2fd6eSJonathan Cameron  trigger with our device by writing the trigger's name in the
4049b2fd6eSJonathan Cameron  :file:`current_trigger` file.
4149b2fd6eSJonathan Cameron
4249b2fd6eSJonathan CameronIIO trigger setup
4349b2fd6eSJonathan Cameron=================
4449b2fd6eSJonathan Cameron
4549b2fd6eSJonathan CameronLet's see a simple example of how to setup a trigger to be used by a driver::
4649b2fd6eSJonathan Cameron
4749b2fd6eSJonathan Cameron      struct iio_trigger_ops trigger_ops = {
4849b2fd6eSJonathan Cameron          .set_trigger_state = sample_trigger_state,
4949b2fd6eSJonathan Cameron          .validate_device = sample_validate_device,
5049b2fd6eSJonathan Cameron      }
5149b2fd6eSJonathan Cameron
5249b2fd6eSJonathan Cameron      struct iio_trigger *trig;
5349b2fd6eSJonathan Cameron
5449b2fd6eSJonathan Cameron      /* first, allocate memory for our trigger */
5549b2fd6eSJonathan Cameron      trig = iio_trigger_alloc(dev, "trig-%s-%d", name, idx);
5649b2fd6eSJonathan Cameron
5749b2fd6eSJonathan Cameron      /* setup trigger operations field */
5849b2fd6eSJonathan Cameron      trig->ops = &trigger_ops;
5949b2fd6eSJonathan Cameron
6049b2fd6eSJonathan Cameron      /* now register the trigger with the IIO core */
6149b2fd6eSJonathan Cameron      iio_trigger_register(trig);
6249b2fd6eSJonathan Cameron
6349b2fd6eSJonathan CameronIIO trigger ops
6449b2fd6eSJonathan Cameron===============
6549b2fd6eSJonathan Cameron
66*9303c9d5SMauro Carvalho Chehab* struct iio_trigger_ops — operations structure for an iio_trigger.
6749b2fd6eSJonathan Cameron
6849b2fd6eSJonathan CameronNotice that a trigger has a set of operations attached:
6949b2fd6eSJonathan Cameron
7049b2fd6eSJonathan Cameron* :file:`set_trigger_state`, switch the trigger on/off on demand.
7149b2fd6eSJonathan Cameron* :file:`validate_device`, function to validate the device when the current
7249b2fd6eSJonathan Cameron  trigger gets changed.
7349b2fd6eSJonathan Cameron
7449b2fd6eSJonathan CameronMore details
7549b2fd6eSJonathan Cameron============
7649b2fd6eSJonathan Cameron.. kernel-doc:: include/linux/iio/trigger.h
7749b2fd6eSJonathan Cameron.. kernel-doc:: drivers/iio/industrialio-trigger.c
7849b2fd6eSJonathan Cameron   :export:
79