staging:iio:trigger sysfs userspace trigger rework.
[linux-2.6/next.git] / drivers / staging / iio / trigger.h
blob5efa0d50b72067cd2f9fee8b9b90c58442c85084
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.
8 */
9 #include <linux/irq.h>
11 #ifndef _IIO_TRIGGER_H_
12 #define _IIO_TRIGGER_H_
14 struct iio_subirq {
15 bool enabled;
18 /**
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)
33 **/
34 struct iio_trigger {
35 int id;
36 const char *name;
37 struct device dev;
39 void *private_data;
40 struct list_head list;
41 struct list_head alloc_list;
42 const struct attribute_group *control_attrs;
43 struct module *owner;
44 int use_count;
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;
50 int subirq_base;
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);
74 /**
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
79 **/
80 ssize_t iio_trigger_read_name(struct device *dev,
81 struct device_attribute *attr,
82 char *buf);
84 #define IIO_TRIGGER_NAME_ATTR DEVICE_ATTR(name, S_IRUGO, \
85 iio_trigger_read_name, \
86 NULL);
88 /**
89 * iio_trigger_register() - register a trigger with the IIO core
90 * @trig_info: trigger to be registered
91 **/
92 int iio_trigger_register(struct iio_trigger *trig_info);
94 /**
95 * iio_trigger_unregister() - unregister a trigger from the core
96 * @trig_info: trigger to be unregistered
97 **/
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
110 * run on trigger
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)
131 int ret;
132 mutex_lock(&trig->pool_lock);
133 ret = bitmap_find_free_region(trig->pool,
134 CONFIG_IIO_CONSUMERS_PER_TRIGGER,
135 ilog2(1));
136 mutex_unlock(&trig->pool_lock);
137 if (ret >= 0)
138 ret += trig->subirq_base;
140 return ret;
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
158 * trigger pool
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 {
164 void *private_data;
165 irqreturn_t (*h)(int irq, void *p);
166 irqreturn_t (*thread)(int irq, void *p);
167 int type;
168 char *name;
169 int irq;
170 s64 timestamp;
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_ */