trigger.h revision d96d1337e339521a2bd56dc9d51fef140c1a49ee
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 * @pollfunc_list_lock:	[INTERN] protection of the polling function list
28 * @pollfunc_list:	[INTERN] list of functions to run on trigger.
29 * @control_attrs:	[DRIVER] sysfs attributes relevant to trigger type
30 * @owner:		[DRIVER] used to monitor usage count of the trigger.
31 * @use_count:		use count for the trigger
32 * @set_trigger_state:	[DRIVER] switch on/off the trigger on demand
33 * @try_reenable:	function to reenable the trigger when the
34 *			use count is zero (may be NULL)
35 **/
36struct iio_trigger {
37	int				id;
38	const char			*name;
39	struct device			dev;
40
41	void				*private_data;
42	struct list_head		list;
43	struct list_head		alloc_list;
44	spinlock_t			pollfunc_list_lock;
45	struct list_head		pollfunc_list;
46	const struct attribute_group	*control_attrs;
47	struct module			*owner;
48	int use_count;
49
50	int (*set_trigger_state)(struct iio_trigger *trig, bool state);
51	int (*try_reenable)(struct iio_trigger *trig);
52
53	struct irq_chip			subirq_chip;
54	int				subirq_base;
55
56	struct iio_subirq subirqs[CONFIG_IIO_CONSUMERS_PER_TRIGGER];
57	unsigned long pool[BITS_TO_LONGS(CONFIG_IIO_CONSUMERS_PER_TRIGGER)];
58	struct mutex			pool_lock;
59};
60
61static inline struct iio_trigger *to_iio_trigger(struct device *d)
62{
63	return container_of(d, struct iio_trigger, dev);
64};
65
66static inline void iio_put_trigger(struct iio_trigger *trig)
67{
68	put_device(&trig->dev);
69	module_put(trig->owner);
70};
71
72static inline void iio_get_trigger(struct iio_trigger *trig)
73{
74	__module_get(trig->owner);
75	get_device(&trig->dev);
76};
77
78/**
79 * iio_trigger_read_name() - sysfs access function to get the trigger name
80 * @dev: the system device
81 * @attr: device attributes for the device
82 * @buf: output buffer to store the trigger name
83 **/
84ssize_t iio_trigger_read_name(struct device *dev,
85			      struct device_attribute *attr,
86			      char *buf);
87
88#define IIO_TRIGGER_NAME_ATTR DEVICE_ATTR(name, S_IRUGO,		\
89					  iio_trigger_read_name,	\
90					  NULL);
91
92/**
93 * iio_trigger_register() - register a trigger with the IIO core
94 * @trig_info:	trigger to be registered
95 **/
96int iio_trigger_register(struct iio_trigger *trig_info);
97
98/**
99 * iio_trigger_unregister() - unregister a trigger from the core
100 * @trig_info:	trigger to be unregistered
101 **/
102void iio_trigger_unregister(struct iio_trigger *trig_info);
103
104/**
105 * iio_trigger_attach_poll_func() - add a function pair to be run on trigger
106 * @trig:	trigger to which the function pair are being added
107 * @pf:		poll function pair
108 **/
109int iio_trigger_attach_poll_func(struct iio_trigger *trig,
110				 struct iio_poll_func *pf);
111
112/**
113 * iio_trigger_dettach_poll_func() -	remove function pair from those to be
114 *					run on trigger
115 * @trig:	trigger from which the function is being removed
116 * @pf:		poll function pair
117 **/
118int iio_trigger_dettach_poll_func(struct iio_trigger *trig,
119				  struct iio_poll_func *pf);
120
121/**
122 * iio_trigger_poll() - called on a trigger occurring
123 * @trig: trigger which occurred
124 *
125 * Typically called in relevant hardware interrupt handler.
126 **/
127void iio_trigger_poll(struct iio_trigger *trig, s64 time);
128void iio_trigger_notify_done(struct iio_trigger *trig);
129
130static inline int iio_trigger_get_irq(struct iio_trigger *trig)
131{
132	int ret;
133	mutex_lock(&trig->pool_lock);
134	ret = bitmap_find_free_region(trig->pool,
135				      CONFIG_IIO_CONSUMERS_PER_TRIGGER,
136				      ilog2(1));
137	mutex_unlock(&trig->pool_lock);
138	if (ret >= 0)
139		ret += trig->subirq_base;
140
141	return ret;
142};
143
144static inline void iio_trigger_put_irq(struct iio_trigger *trig, int irq)
145{
146	mutex_lock(&trig->pool_lock);
147	clear_bit(irq - trig->subirq_base, trig->pool);
148	mutex_unlock(&trig->pool_lock);
149};
150
151/**
152 * struct iio_poll_func - poll function pair
153 *
154 * @list:			associate this with a triggers pollfunc_list
155 * @private_data:		data specific to device (passed into poll func)
156 * @poll_func_immediate:	function in here is run first. They should be
157 *				extremely lightweight.  Typically used for latch
158 *				control on sensor supporting it.
159 * @poll_func_main:		function in here is run after all immediates.
160 *				Reading from sensor etc typically involves
161 *				scheduling from here.
162 * @h:				the function that is actually run on trigger
163 * @thread:			threaded interrupt part
164 * @type:			the type of interrupt (basically if oneshot)
165 * @irq:			the corresponding irq as allocated from the
166 *				trigger pool
167 * @timestamp:			some devices need a timestamp grabbed as soon
168 *				as possible after the trigger - hence handler
169 *				passes it via here.
170 **/
171struct iio_poll_func {
172	struct				list_head list;
173	void				*private_data;
174	void (*poll_func_immediate)(struct iio_dev *indio_dev);
175	void (*poll_func_main)(struct iio_dev *private_data, s64 time);
176
177	irqreturn_t (*h)(int irq, void *p);
178	irqreturn_t (*thread)(int irq, void *p);
179	int type;
180	char *name;
181	int irq;
182	s64 timestamp;
183};
184
185int iio_alloc_pollfunc(struct iio_dev *indio_dev,
186		       void (*immediate)(struct iio_dev *indio_dev),
187		       void (*main)(struct iio_dev *private_data, s64 time));
188
189irqreturn_t iio_pollfunc_store_time(int irq, void *p);
190
191/*
192 * Two functions for common case where all that happens is a pollfunc
193 * is attached and detached from a trigger
194 */
195int iio_triggered_ring_postenable(struct iio_dev *indio_dev);
196int iio_triggered_ring_predisable(struct iio_dev *indio_dev);
197
198struct iio_trigger *iio_allocate_trigger(void);
199struct iio_trigger *iio_allocate_trigger_named(const char *name);
200void iio_free_trigger(struct iio_trigger *trig);
201
202#endif /* _IIO_TRIGGER_H_ */
203