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 #include <linux/module.h>
11 #include <linux/atomic.h>
12 
13 #ifndef _IIO_TRIGGER_H_
14 #define _IIO_TRIGGER_H_
15 
16 #ifdef CONFIG_IIO_TRIGGER
17 struct iio_subirq {
18 	bool enabled;
19 };
20 
21 struct iio_dev;
22 struct iio_trigger;
23 
24 /**
25  * struct iio_trigger_ops - operations structure for an iio_trigger.
26  * @set_trigger_state:	switch on/off the trigger on demand
27  * @try_reenable:	function to reenable the trigger when the
28  *			use count is zero (may be NULL)
29  * @validate_device:	function to validate the device when the
30  *			current trigger gets changed.
31  *
32  * This is typically static const within a driver and shared by
33  * instances of a given device.
34  **/
35 struct iio_trigger_ops {
36 	int (*set_trigger_state)(struct iio_trigger *trig, bool state);
37 	int (*try_reenable)(struct iio_trigger *trig);
38 	int (*validate_device)(struct iio_trigger *trig,
39 			       struct iio_dev *indio_dev);
40 };
41 
42 
43 /**
44  * struct iio_trigger - industrial I/O trigger device
45  * @ops:		[DRIVER] operations structure
46  * @owner:		[INTERN] owner of this driver module
47  * @id:			[INTERN] unique id number
48  * @name:		[DRIVER] unique name
49  * @dev:		[DRIVER] associated device (if relevant)
50  * @list:		[INTERN] used in maintenance of global trigger list
51  * @alloc_list:		[DRIVER] used for driver specific trigger list
52  * @use_count:		[INTERN] use count for the trigger.
53  * @subirq_chip:	[INTERN] associate 'virtual' irq chip.
54  * @subirq_base:	[INTERN] base number for irqs provided by trigger.
55  * @subirqs:		[INTERN] information about the 'child' irqs.
56  * @pool:		[INTERN] bitmap of irqs currently in use.
57  * @pool_lock:		[INTERN] protection of the irq pool.
58  * @attached_own_device:[INTERN] if we are using our own device as trigger,
59  *			i.e. if we registered a poll function to the same
60  *			device as the one providing the trigger.
61  **/
62 struct iio_trigger {
63 	const struct iio_trigger_ops	*ops;
64 	struct module			*owner;
65 	int				id;
66 	const char			*name;
67 	struct device			dev;
68 
69 	struct list_head		list;
70 	struct list_head		alloc_list;
71 	atomic_t			use_count;
72 
73 	struct irq_chip			subirq_chip;
74 	int				subirq_base;
75 
76 	struct iio_subirq subirqs[CONFIG_IIO_CONSUMERS_PER_TRIGGER];
77 	unsigned long pool[BITS_TO_LONGS(CONFIG_IIO_CONSUMERS_PER_TRIGGER)];
78 	struct mutex			pool_lock;
79 	bool				attached_own_device;
80 };
81 
82 
to_iio_trigger(struct device * d)83 static inline struct iio_trigger *to_iio_trigger(struct device *d)
84 {
85 	return container_of(d, struct iio_trigger, dev);
86 }
87 
iio_trigger_put(struct iio_trigger * trig)88 static inline void iio_trigger_put(struct iio_trigger *trig)
89 {
90 	module_put(trig->owner);
91 	put_device(&trig->dev);
92 }
93 
iio_trigger_get(struct iio_trigger * trig)94 static inline struct iio_trigger *iio_trigger_get(struct iio_trigger *trig)
95 {
96 	get_device(&trig->dev);
97 	__module_get(trig->owner);
98 
99 	return trig;
100 }
101 
102 /**
103  * iio_device_set_drvdata() - Set trigger driver data
104  * @trig: IIO trigger structure
105  * @data: Driver specific data
106  *
107  * Allows to attach an arbitrary pointer to an IIO trigger, which can later be
108  * retrieved by iio_trigger_get_drvdata().
109  */
iio_trigger_set_drvdata(struct iio_trigger * trig,void * data)110 static inline void iio_trigger_set_drvdata(struct iio_trigger *trig, void *data)
111 {
112 	dev_set_drvdata(&trig->dev, data);
113 }
114 
115 /**
116  * iio_trigger_get_drvdata() - Get trigger driver data
117  * @trig: IIO trigger structure
118  *
119  * Returns the data previously set with iio_trigger_set_drvdata()
120  */
iio_trigger_get_drvdata(struct iio_trigger * trig)121 static inline void *iio_trigger_get_drvdata(struct iio_trigger *trig)
122 {
123 	return dev_get_drvdata(&trig->dev);
124 }
125 
126 /**
127  * iio_trigger_register() - register a trigger with the IIO core
128  * @trig_info:	trigger to be registered
129  **/
130 #define iio_trigger_register(trig_info) \
131 	__iio_trigger_register((trig_info), THIS_MODULE)
132 int __iio_trigger_register(struct iio_trigger *trig_info,
133 			   struct module *this_mod);
134 
135 #define devm_iio_trigger_register(dev, trig_info) \
136 	__devm_iio_trigger_register((dev), (trig_info), THIS_MODULE)
137 int __devm_iio_trigger_register(struct device *dev,
138 				struct iio_trigger *trig_info,
139 				struct module *this_mod);
140 
141 /**
142  * iio_trigger_unregister() - unregister a trigger from the core
143  * @trig_info:	trigger to be unregistered
144  **/
145 void iio_trigger_unregister(struct iio_trigger *trig_info);
146 
147 void devm_iio_trigger_unregister(struct device *dev,
148 				 struct iio_trigger *trig_info);
149 
150 /**
151  * iio_trigger_set_immutable() - set an immutable trigger on destination
152  *
153  * @indio_dev: IIO device structure containing the device
154  * @trig: trigger to assign to device
155  *
156  **/
157 int iio_trigger_set_immutable(struct iio_dev *indio_dev, struct iio_trigger *trig);
158 
159 /**
160  * iio_trigger_poll() - called on a trigger occurring
161  * @trig:	trigger which occurred
162  *
163  * Typically called in relevant hardware interrupt handler.
164  **/
165 void iio_trigger_poll(struct iio_trigger *trig);
166 void iio_trigger_poll_chained(struct iio_trigger *trig);
167 
168 irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private);
169 
170 __printf(1, 2) struct iio_trigger *iio_trigger_alloc(const char *fmt, ...);
171 void iio_trigger_free(struct iio_trigger *trig);
172 
173 /**
174  * iio_trigger_using_own() - tells us if we use our own HW trigger ourselves
175  * @indio_dev:  device to check
176  */
177 bool iio_trigger_using_own(struct iio_dev *indio_dev);
178 
179 int iio_trigger_validate_own_device(struct iio_trigger *trig,
180 				     struct iio_dev *indio_dev);
181 
182 #else
183 struct iio_trigger;
184 struct iio_trigger_ops;
185 #endif
186 #endif /* _IIO_TRIGGER_H_ */
187