1 /* The industrial I/O core, trigger handling functions
3 * Copyright (c) 2008 Jonathan Cameron
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.
11 #ifndef _IIO_TRIGGER_H_
12 #define _IIO_TRIGGER_H_
19 * struct iio_trigger - industrial I/O trigger device
21 * @id: [INTERN] unique id number
22 * @name: [DRIVER] unique name
23 * @dev: [DRIVER] associated device (if relevant)
24 * @private_data: [DRIVER] device specific data
25 * @list: [INTERN] used in maintenance of global trigger list
26 * @alloc_list: [DRIVER] used for driver specific trigger list
27 * @control_attrs: [DRIVER] sysfs attributes relevant to trigger type
28 * @owner: [DRIVER] used to monitor usage count of the trigger.
29 * @use_count: use count for the trigger
30 * @set_trigger_state: [DRIVER] switch on/off the trigger on demand
31 * @try_reenable: function to reenable the trigger when the
32 * use count is zero (may be NULL)
40 struct list_head list
;
41 struct list_head alloc_list
;
42 const struct attribute_group
*control_attrs
;
46 int (*set_trigger_state
)(struct iio_trigger
*trig
, bool state
);
47 int (*try_reenable
)(struct iio_trigger
*trig
);
49 struct irq_chip subirq_chip
;
52 struct iio_subirq subirqs
[CONFIG_IIO_CONSUMERS_PER_TRIGGER
];
53 unsigned long pool
[BITS_TO_LONGS(CONFIG_IIO_CONSUMERS_PER_TRIGGER
)];
54 struct mutex pool_lock
;
57 static inline struct iio_trigger
*to_iio_trigger(struct device
*d
)
59 return container_of(d
, struct iio_trigger
, dev
);
62 static inline void iio_put_trigger(struct iio_trigger
*trig
)
64 put_device(&trig
->dev
);
65 module_put(trig
->owner
);
68 static inline void iio_get_trigger(struct iio_trigger
*trig
)
70 __module_get(trig
->owner
);
71 get_device(&trig
->dev
);
75 * iio_trigger_read_name() - sysfs access function to get the trigger name
76 * @dev: the system device
77 * @attr: device attributes for the device
78 * @buf: output buffer to store the trigger name
80 ssize_t
iio_trigger_read_name(struct device
*dev
,
81 struct device_attribute
*attr
,
84 #define IIO_TRIGGER_NAME_ATTR DEVICE_ATTR(name, S_IRUGO, \
85 iio_trigger_read_name, \
89 * iio_trigger_register() - register a trigger with the IIO core
90 * @trig_info: trigger to be registered
92 int iio_trigger_register(struct iio_trigger
*trig_info
);
95 * iio_trigger_unregister() - unregister a trigger from the core
96 * @trig_info: trigger to be unregistered
98 void iio_trigger_unregister(struct iio_trigger
*trig_info
);
101 * iio_trigger_attach_poll_func() - add a function pair to be run on trigger
102 * @trig: trigger to which the function pair are being added
103 * @pf: poll function pair
105 int iio_trigger_attach_poll_func(struct iio_trigger
*trig
,
106 struct iio_poll_func
*pf
);
109 * iio_trigger_dettach_poll_func() - remove function pair from those to be
111 * @trig: trigger from which the function is being removed
112 * @pf: poll function pair
114 int iio_trigger_dettach_poll_func(struct iio_trigger
*trig
,
115 struct iio_poll_func
*pf
);
118 * iio_trigger_poll() - called on a trigger occurring
119 * @trig: trigger which occurred
121 * Typically called in relevant hardware interrupt handler.
123 void iio_trigger_poll(struct iio_trigger
*trig
, s64 time
);
124 void iio_trigger_poll_chained(struct iio_trigger
*trig
, s64 time
);
125 void iio_trigger_notify_done(struct iio_trigger
*trig
);
127 irqreturn_t
iio_trigger_generic_data_rdy_poll(int irq
, void *private);
129 static inline int iio_trigger_get_irq(struct iio_trigger
*trig
)
132 mutex_lock(&trig
->pool_lock
);
133 ret
= bitmap_find_free_region(trig
->pool
,
134 CONFIG_IIO_CONSUMERS_PER_TRIGGER
,
136 mutex_unlock(&trig
->pool_lock
);
138 ret
+= trig
->subirq_base
;
143 static inline void iio_trigger_put_irq(struct iio_trigger
*trig
, int irq
)
145 mutex_lock(&trig
->pool_lock
);
146 clear_bit(irq
- trig
->subirq_base
, trig
->pool
);
147 mutex_unlock(&trig
->pool_lock
);
151 * struct iio_poll_func - poll function pair
153 * @private_data: data specific to device (passed into poll func)
154 * @h: the function that is actually run on trigger
155 * @thread: threaded interrupt part
156 * @type: the type of interrupt (basically if oneshot)
157 * @irq: the corresponding irq as allocated from the
159 * @timestamp: some devices need a timestamp grabbed as soon
160 * as possible after the trigger - hence handler
161 * passes it via here.
163 struct iio_poll_func
{
165 irqreturn_t (*h
)(int irq
, void *p
);
166 irqreturn_t (*thread
)(int irq
, void *p
);
173 irqreturn_t
iio_pollfunc_store_time(int irq
, void *p
);
176 * Two functions for common case where all that happens is a pollfunc
177 * is attached and detached from a trigger
179 int iio_triggered_ring_postenable(struct iio_dev
*indio_dev
);
180 int iio_triggered_ring_predisable(struct iio_dev
*indio_dev
);
182 struct iio_trigger
*iio_allocate_trigger(void);
183 struct iio_trigger
*iio_allocate_trigger_named(const char *name
);
184 void iio_free_trigger(struct iio_trigger
*trig
);
186 #endif /* _IIO_TRIGGER_H_ */