Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 1 | /* The industrial I/O core, trigger handling functions |
| 2 | * |
| 3 | * Copyright (c) 2008 Jonathan Cameron |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 as published by |
| 7 | * the Free Software Foundation. |
| 8 | */ |
| 9 | #ifndef _IIO_TRIGGER_H_ |
| 10 | #define _IIO_TRIGGER_H_ |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 11 | |
| 12 | /** |
| 13 | * struct iio_trigger - industrial I/O trigger device |
| 14 | * |
| 15 | * @id: [INTERN] unique id number |
| 16 | * @name: [DRIVER] unique name |
| 17 | * @dev: [DRIVER] associated device (if relevant) |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 18 | * @private_data: [DRIVER] device specific data |
| 19 | * @list: [INTERN] used in maintenance of global trigger list |
| 20 | * @alloc_list: [DRIVER] used for driver specific trigger list |
Randy Dunlap | 4c57260 | 2009-10-04 19:34:02 -0700 | [diff] [blame] | 21 | * @pollfunc_list_lock: [INTERN] protection of the polling function list |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 22 | * @pollfunc_list: [INTERN] list of functions to run on trigger. |
| 23 | * @control_attrs: [DRIVER] sysfs attributes relevant to trigger type |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 24 | * @owner: [DRIVER] used to monitor usage count of the trigger. |
Randy Dunlap | 4c57260 | 2009-10-04 19:34:02 -0700 | [diff] [blame] | 25 | * @use_count: use count for the trigger |
| 26 | * @set_trigger_state: [DRIVER] switch on/off the trigger on demand |
| 27 | * @try_reenable: function to reenable the trigger when the |
| 28 | * use count is zero (may be NULL) |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 29 | **/ |
| 30 | struct iio_trigger { |
| 31 | int id; |
| 32 | const char *name; |
| 33 | struct device dev; |
| 34 | |
| 35 | void *private_data; |
| 36 | struct list_head list; |
| 37 | struct list_head alloc_list; |
| 38 | spinlock_t pollfunc_list_lock; |
| 39 | struct list_head pollfunc_list; |
| 40 | const struct attribute_group *control_attrs; |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 41 | struct module *owner; |
| 42 | int use_count; |
| 43 | |
| 44 | int (*set_trigger_state)(struct iio_trigger *trig, bool state); |
| 45 | int (*try_reenable)(struct iio_trigger *trig); |
| 46 | }; |
| 47 | |
| 48 | static inline struct iio_trigger *to_iio_trigger(struct device *d) |
| 49 | { |
| 50 | return container_of(d, struct iio_trigger, dev); |
| 51 | }; |
| 52 | |
| 53 | static inline void iio_put_trigger(struct iio_trigger *trig) |
| 54 | { |
| 55 | put_device(&trig->dev); |
| 56 | module_put(trig->owner); |
| 57 | }; |
| 58 | |
| 59 | static inline void iio_get_trigger(struct iio_trigger *trig) |
| 60 | { |
| 61 | __module_get(trig->owner); |
| 62 | get_device(&trig->dev); |
| 63 | }; |
| 64 | |
| 65 | /** |
| 66 | * iio_trigger_read_name() - sysfs access function to get the trigger name |
Randy Dunlap | 4c57260 | 2009-10-04 19:34:02 -0700 | [diff] [blame] | 67 | * @dev: the system device |
| 68 | * @attr: device attributes for the device |
| 69 | * @buf: output buffer to store the trigger name |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 70 | **/ |
| 71 | ssize_t iio_trigger_read_name(struct device *dev, |
| 72 | struct device_attribute *attr, |
| 73 | char *buf); |
| 74 | |
| 75 | #define IIO_TRIGGER_NAME_ATTR DEVICE_ATTR(name, S_IRUGO, \ |
| 76 | iio_trigger_read_name, \ |
| 77 | NULL); |
| 78 | |
| 79 | /** |
| 80 | * iio_trigger_find_by_name() - search global trigger list |
Randy Dunlap | 4c57260 | 2009-10-04 19:34:02 -0700 | [diff] [blame] | 81 | * @name: trigger name to search for |
| 82 | * @len: trigger name string length to compare |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 83 | **/ |
| 84 | struct iio_trigger *iio_trigger_find_by_name(const char *name, size_t len); |
| 85 | |
| 86 | /** |
| 87 | * iio_trigger_register() - register a trigger with the IIO core |
| 88 | * @trig_info: trigger to be registered |
| 89 | **/ |
| 90 | int iio_trigger_register(struct iio_trigger *trig_info); |
| 91 | |
| 92 | /** |
| 93 | * iio_trigger_unregister() - unregister a trigger from the core |
Randy Dunlap | 4c57260 | 2009-10-04 19:34:02 -0700 | [diff] [blame] | 94 | * @trig_info: trigger to be unregistered |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 95 | **/ |
| 96 | void iio_trigger_unregister(struct iio_trigger *trig_info); |
| 97 | |
| 98 | /** |
| 99 | * iio_trigger_attach_poll_func() - add a function pair to be run on trigger |
| 100 | * @trig: trigger to which the function pair are being added |
Randy Dunlap | 4c57260 | 2009-10-04 19:34:02 -0700 | [diff] [blame] | 101 | * @pf: poll function pair |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 102 | **/ |
| 103 | int iio_trigger_attach_poll_func(struct iio_trigger *trig, |
| 104 | struct iio_poll_func *pf); |
| 105 | |
| 106 | /** |
| 107 | * iio_trigger_dettach_poll_func() - remove function pair from those to be |
Randy Dunlap | 4c57260 | 2009-10-04 19:34:02 -0700 | [diff] [blame] | 108 | * run on trigger |
| 109 | * @trig: trigger from which the function is being removed |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 110 | * @pf: poll function pair |
| 111 | **/ |
| 112 | int iio_trigger_dettach_poll_func(struct iio_trigger *trig, |
| 113 | struct iio_poll_func *pf); |
| 114 | |
| 115 | /** |
Randy Dunlap | 4c57260 | 2009-10-04 19:34:02 -0700 | [diff] [blame] | 116 | * iio_trigger_poll() - called on a trigger occurring |
| 117 | * @trig: trigger which occurred |
| 118 | * |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 119 | * Typically called in relevant hardware interrupt handler. |
| 120 | **/ |
Jonathan Cameron | 7b2c33b | 2010-07-11 16:39:11 +0100 | [diff] [blame] | 121 | void iio_trigger_poll(struct iio_trigger *trig, s64 time); |
Randy Dunlap | 4c57260 | 2009-10-04 19:34:02 -0700 | [diff] [blame] | 122 | void iio_trigger_notify_done(struct iio_trigger *trig); |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * struct iio_poll_func - poll function pair |
| 126 | * |
| 127 | * @list: associate this with a triggers pollfunc_list |
| 128 | * @private_data: data specific to device (passed into poll func) |
| 129 | * @poll_func_immediate: function in here is run first. They should be |
| 130 | * extremely lightweight. Typically used for latch |
| 131 | * control on sensor supporting it. |
| 132 | * @poll_func_main: function in here is run after all immediates. |
| 133 | * Reading from sensor etc typically involves |
Randy Dunlap | 4c57260 | 2009-10-04 19:34:02 -0700 | [diff] [blame] | 134 | * scheduling from here. |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 135 | * |
Randy Dunlap | 4c57260 | 2009-10-04 19:34:02 -0700 | [diff] [blame] | 136 | * The two stage approach used here is only important when multiple sensors are |
| 137 | * being triggered by a single trigger. This really comes into its own with |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 138 | * simultaneous sampling devices where a simple latch command can be used to |
| 139 | * make the device store the values on all inputs. |
| 140 | **/ |
| 141 | struct iio_poll_func { |
| 142 | struct list_head list; |
| 143 | void *private_data; |
| 144 | void (*poll_func_immediate)(struct iio_dev *indio_dev); |
Jonathan Cameron | 7b2c33b | 2010-07-11 16:39:11 +0100 | [diff] [blame] | 145 | void (*poll_func_main)(struct iio_dev *private_data, s64 time); |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 146 | |
| 147 | }; |
| 148 | |
Jonathan Cameron | 1574409 | 2010-07-11 16:39:09 +0100 | [diff] [blame] | 149 | int iio_alloc_pollfunc(struct iio_dev *indio_dev, |
| 150 | void (*immediate)(struct iio_dev *indio_dev), |
Jonathan Cameron | 7b2c33b | 2010-07-11 16:39:11 +0100 | [diff] [blame] | 151 | void (*main)(struct iio_dev *private_data, s64 time)); |
Jonathan Cameron | 1574409 | 2010-07-11 16:39:09 +0100 | [diff] [blame] | 152 | |
Jonathan Cameron | c3db00c | 2010-07-11 16:39:10 +0100 | [diff] [blame] | 153 | /* |
| 154 | * Two functions for common case where all that happens is a pollfunc |
Jonathan Cameron | 04b7081 | 2010-09-04 17:54:50 +0100 | [diff] [blame] | 155 | * is attached and detached from a trigger |
Jonathan Cameron | c3db00c | 2010-07-11 16:39:10 +0100 | [diff] [blame] | 156 | */ |
| 157 | int iio_triggered_ring_postenable(struct iio_dev *indio_dev); |
| 158 | int iio_triggered_ring_predisable(struct iio_dev *indio_dev); |
| 159 | |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 160 | struct iio_trigger *iio_allocate_trigger(void); |
| 161 | |
| 162 | void iio_free_trigger(struct iio_trigger *trig); |
| 163 | |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 164 | #endif /* _IIO_TRIGGER_H_ */ |