trigger.h revision 59c85e82c2e7a672cb4342dc5ccf9df8a3a14f73
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#include <linux/irq.h>
10
11#ifndef _IIO_TRIGGER_H_
12#define _IIO_TRIGGER_H_
13
14struct iio_subirq {
15	bool enabled;
16};
17
18/**
19 * struct iio_trigger - industrial I/O trigger device
20 *
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 * @owner:		[DRIVER] used to monitor usage count of the trigger.
28 * @use_count:		use count for the trigger
29 * @set_trigger_state:	[DRIVER] switch on/off the trigger on demand
30 * @try_reenable:	function to reenable the trigger when the
31 *			use count is zero (may be NULL)
32 * @subirq_chip:	[INTERN] associate 'virtual' irq chip.
33 * @subirq_base:	[INTERN] base number for irqs provided by trigger.
34 * @subirqs:		[INTERN] information about the 'child' irqs.
35 * @pool:		[INTERN] bitmap of irqs currently in use.
36 * @pool_lock:		[INTERN] protection of the irq pool.
37 **/
38struct iio_trigger {
39	int				id;
40	const char			*name;
41	struct device			dev;
42
43	void				*private_data;
44	struct list_head		list;
45	struct list_head		alloc_list;
46	struct module			*owner;
47	int use_count;
48
49	int (*set_trigger_state)(struct iio_trigger *trig, bool state);
50	int (*try_reenable)(struct iio_trigger *trig);
51
52	struct irq_chip			subirq_chip;
53	int				subirq_base;
54
55	struct iio_subirq subirqs[CONFIG_IIO_CONSUMERS_PER_TRIGGER];
56	unsigned long pool[BITS_TO_LONGS(CONFIG_IIO_CONSUMERS_PER_TRIGGER)];
57	struct mutex			pool_lock;
58};
59
60static inline struct iio_trigger *to_iio_trigger(struct device *d)
61{
62	return container_of(d, struct iio_trigger, dev);
63};
64
65static inline void iio_put_trigger(struct iio_trigger *trig)
66{
67	put_device(&trig->dev);
68	module_put(trig->owner);
69};
70
71static inline void iio_get_trigger(struct iio_trigger *trig)
72{
73	__module_get(trig->owner);
74	get_device(&trig->dev);
75};
76
77/**
78 * iio_trigger_register() - register a trigger with the IIO core
79 * @trig_info:	trigger to be registered
80 **/
81int iio_trigger_register(struct iio_trigger *trig_info);
82
83/**
84 * iio_trigger_unregister() - unregister a trigger from the core
85 * @trig_info:	trigger to be unregistered
86 **/
87void iio_trigger_unregister(struct iio_trigger *trig_info);
88
89/**
90 * iio_trigger_attach_poll_func() - add a function pair to be run on trigger
91 * @trig:	trigger to which the function pair are being added
92 * @pf:		poll function pair
93 **/
94int iio_trigger_attach_poll_func(struct iio_trigger *trig,
95				 struct iio_poll_func *pf);
96
97/**
98 * iio_trigger_dettach_poll_func() -	remove function pair from those to be
99 *					run on trigger
100 * @trig:	trigger from which the function is being removed
101 * @pf:		poll function pair
102 **/
103int iio_trigger_dettach_poll_func(struct iio_trigger *trig,
104				  struct iio_poll_func *pf);
105
106/**
107 * iio_trigger_poll() - called on a trigger occurring
108 * @trig: trigger which occurred
109 *
110 * Typically called in relevant hardware interrupt handler.
111 **/
112void iio_trigger_poll(struct iio_trigger *trig, s64 time);
113void iio_trigger_poll_chained(struct iio_trigger *trig, s64 time);
114void iio_trigger_notify_done(struct iio_trigger *trig);
115
116irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private);
117
118static inline int iio_trigger_get_irq(struct iio_trigger *trig)
119{
120	int ret;
121	mutex_lock(&trig->pool_lock);
122	ret = bitmap_find_free_region(trig->pool,
123				      CONFIG_IIO_CONSUMERS_PER_TRIGGER,
124				      ilog2(1));
125	mutex_unlock(&trig->pool_lock);
126	if (ret >= 0)
127		ret += trig->subirq_base;
128
129	return ret;
130};
131
132static inline void iio_trigger_put_irq(struct iio_trigger *trig, int irq)
133{
134	mutex_lock(&trig->pool_lock);
135	clear_bit(irq - trig->subirq_base, trig->pool);
136	mutex_unlock(&trig->pool_lock);
137};
138
139/**
140 * struct iio_poll_func - poll function pair
141 *
142 * @private_data:		data specific to device (passed into poll func)
143 * @h:				the function that is actually run on trigger
144 * @thread:			threaded interrupt part
145 * @type:			the type of interrupt (basically if oneshot)
146 * @name:			name used to identify the trigger consumer.
147 * @irq:			the corresponding irq as allocated from the
148 *				trigger pool
149 * @timestamp:			some devices need a timestamp grabbed as soon
150 *				as possible after the trigger - hence handler
151 *				passes it via here.
152 **/
153struct iio_poll_func {
154	void				*private_data;
155	irqreturn_t (*h)(int irq, void *p);
156	irqreturn_t (*thread)(int irq, void *p);
157	int type;
158	char *name;
159	int irq;
160	s64 timestamp;
161};
162
163irqreturn_t iio_pollfunc_store_time(int irq, void *p);
164
165/*
166 * Two functions for common case where all that happens is a pollfunc
167 * is attached and detached from a trigger
168 */
169int iio_triggered_ring_postenable(struct iio_dev *indio_dev);
170int iio_triggered_ring_predisable(struct iio_dev *indio_dev);
171
172struct iio_trigger *iio_allocate_trigger(const char *fmt, ...)
173	__attribute__((format(printf, 1, 2)));
174void iio_free_trigger(struct iio_trigger *trig);
175
176#endif /* _IIO_TRIGGER_H_ */
177