Add linux-next specific files for 20110801
[linux-2.6/next.git] / drivers / staging / iio / trigger.h
blob3c1f92c0ed268c0befb89a4a457bf0069451fc6c
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>
10 #include <linux/module.h>
12 #ifndef _IIO_TRIGGER_H_
13 #define _IIO_TRIGGER_H_
15 struct iio_subirq {
16 bool enabled;
19 /**
20 * struct iio_trigger - industrial I/O trigger device
22 * @id: [INTERN] unique id number
23 * @name: [DRIVER] unique name
24 * @dev: [DRIVER] associated device (if relevant)
25 * @private_data: [DRIVER] device specific data
26 * @list: [INTERN] used in maintenance of global trigger list
27 * @alloc_list: [DRIVER] used for driver specific trigger list
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 * @validate_device: function to validate the device when the
34 * current trigger gets changed.
35 * @subirq_chip: [INTERN] associate 'virtual' irq chip.
36 * @subirq_base: [INTERN] base number for irqs provided by trigger.
37 * @subirqs: [INTERN] information about the 'child' irqs.
38 * @pool: [INTERN] bitmap of irqs currently in use.
39 * @pool_lock: [INTERN] protection of the irq pool.
40 **/
41 struct iio_trigger {
42 int id;
43 const char *name;
44 struct device dev;
46 void *private_data;
47 struct list_head list;
48 struct list_head alloc_list;
49 struct module *owner;
50 int use_count;
52 int (*set_trigger_state)(struct iio_trigger *trig, bool state);
53 int (*try_reenable)(struct iio_trigger *trig);
54 int (*validate_device)(struct iio_trigger *trig,
55 struct iio_dev *indio_dev);
57 struct irq_chip subirq_chip;
58 int subirq_base;
60 struct iio_subirq subirqs[CONFIG_IIO_CONSUMERS_PER_TRIGGER];
61 unsigned long pool[BITS_TO_LONGS(CONFIG_IIO_CONSUMERS_PER_TRIGGER)];
62 struct mutex pool_lock;
65 /**
66 * struct iio_poll_func - poll function pair
68 * @private_data: data specific to device (passed into poll func)
69 * @h: the function that is actually run on trigger
70 * @thread: threaded interrupt part
71 * @type: the type of interrupt (basically if oneshot)
72 * @name: name used to identify the trigger consumer.
73 * @irq: the corresponding irq as allocated from the
74 * trigger pool
75 * @timestamp: some devices need a timestamp grabbed as soon
76 * as possible after the trigger - hence handler
77 * passes it via here.
78 **/
79 struct iio_poll_func {
80 void *private_data;
81 irqreturn_t (*h)(int irq, void *p);
82 irqreturn_t (*thread)(int irq, void *p);
83 int type;
84 char *name;
85 int irq;
86 s64 timestamp;
89 static inline struct iio_trigger *to_iio_trigger(struct device *d)
91 return container_of(d, struct iio_trigger, dev);
94 static inline void iio_put_trigger(struct iio_trigger *trig)
96 put_device(&trig->dev);
97 module_put(trig->owner);
100 static inline void iio_get_trigger(struct iio_trigger *trig)
102 __module_get(trig->owner);
103 get_device(&trig->dev);
107 * iio_trigger_register() - register a trigger with the IIO core
108 * @trig_info: trigger to be registered
110 int iio_trigger_register(struct iio_trigger *trig_info);
113 * iio_trigger_unregister() - unregister a trigger from the core
114 * @trig_info: trigger to be unregistered
116 void iio_trigger_unregister(struct iio_trigger *trig_info);
119 * iio_trigger_attach_poll_func() - add a function pair to be run on trigger
120 * @trig: trigger to which the function pair are being added
121 * @pf: poll function pair
123 int iio_trigger_attach_poll_func(struct iio_trigger *trig,
124 struct iio_poll_func *pf);
127 * iio_trigger_dettach_poll_func() - remove function pair from those to be
128 * run on trigger
129 * @trig: trigger from which the function is being removed
130 * @pf: poll function pair
132 int iio_trigger_dettach_poll_func(struct iio_trigger *trig,
133 struct iio_poll_func *pf);
136 * iio_trigger_poll() - called on a trigger occurring
137 * @trig: trigger which occurred
139 * Typically called in relevant hardware interrupt handler.
141 void iio_trigger_poll(struct iio_trigger *trig, s64 time);
142 void iio_trigger_poll_chained(struct iio_trigger *trig, s64 time);
143 void iio_trigger_notify_done(struct iio_trigger *trig);
145 irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private);
147 static inline int iio_trigger_get_irq(struct iio_trigger *trig)
149 int ret;
150 mutex_lock(&trig->pool_lock);
151 ret = bitmap_find_free_region(trig->pool,
152 CONFIG_IIO_CONSUMERS_PER_TRIGGER,
153 ilog2(1));
154 mutex_unlock(&trig->pool_lock);
155 if (ret >= 0)
156 ret += trig->subirq_base;
158 return ret;
161 static inline void iio_trigger_put_irq(struct iio_trigger *trig, int irq)
163 mutex_lock(&trig->pool_lock);
164 clear_bit(irq - trig->subirq_base, trig->pool);
165 mutex_unlock(&trig->pool_lock);
168 struct iio_poll_func
169 *iio_alloc_pollfunc(irqreturn_t (*h)(int irq, void *p),
170 irqreturn_t (*thread)(int irq, void *p),
171 int type,
172 void *private,
173 const char *fmt,
174 ...);
175 void iio_dealloc_pollfunc(struct iio_poll_func *pf);
176 irqreturn_t iio_pollfunc_store_time(int irq, void *p);
179 * Two functions for common case where all that happens is a pollfunc
180 * is attached and detached from a trigger
182 int iio_triggered_ring_postenable(struct iio_dev *indio_dev);
183 int iio_triggered_ring_predisable(struct iio_dev *indio_dev);
185 struct iio_trigger *iio_allocate_trigger(const char *fmt, ...)
186 __attribute__((format(printf, 1, 2)));
187 void iio_free_trigger(struct iio_trigger *trig);
189 #endif /* _IIO_TRIGGER_H_ */