1 /* The industrial I/O core
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 
10 #ifndef _INDUSTRIAL_IO_H_
11 #define _INDUSTRIAL_IO_H_
12 
13 #include <linux/device.h>
14 #include <linux/cdev.h>
15 #include "sysfs.h"
16 #include "chrdev.h"
17 
18 /* IIO TODO LIST */
19 /*
20  * Provide means of adjusting timer accuracy.
21  * Currently assumes nano seconds.
22  */
23 
24 /* Event interface flags */
25 #define IIO_BUSY_BIT_POS 1
26 
27 struct iio_dev;
28 
29 /**
30  * iio_get_time_ns() - utility function to get a time stamp for events etc
31  **/
iio_get_time_ns(void)32 static inline s64 iio_get_time_ns(void)
33 {
34 	struct timespec ts;
35 	/*
36 	 * calls getnstimeofday.
37 	 * If hrtimers then up to ns accurate, if not microsecond.
38 	 */
39 	ktime_get_real_ts(&ts);
40 
41 	return timespec_to_ns(&ts);
42 }
43 
44 /**
45  * iio_add_event_to_list() - Wraps adding to event lists
46  * @el:		the list element of the event to be handled.
47  * @head:	the list associated with the event handler being used.
48  *
49  * Does reference counting to allow shared handlers.
50  **/
51 void iio_add_event_to_list(struct iio_event_handler_list *el,
52 			   struct list_head *head);
53 
54 /**
55  * iio_remove_event_from_list() - Wraps removing from event list
56  * @el:		element to be removed
57  * @head:	associate list head for the interrupt handler.
58  *
59  * Does reference counting to allow shared handlers.
60  **/
61 void iio_remove_event_from_list(struct iio_event_handler_list *el,
62 				struct list_head *head);
63 
64 /* Device operating modes */
65 #define INDIO_DIRECT_MODE		0x01
66 #define INDIO_RING_TRIGGERED		0x02
67 #define INDIO_RING_HARDWARE_BUFFER	0x08
68 
69 #define INDIO_ALL_RING_MODES (INDIO_RING_TRIGGERED | INDIO_RING_HARDWARE_BUFFER)
70 
71 /* Vast majority of this is set by the industrialio subsystem on a
72  * call to iio_device_register. */
73 
74 /**
75  * struct iio_dev - industrial I/O device
76  * @id:			[INTERN] used to identify device internally
77  * @dev_data:		[DRIVER] device specific data
78  * @modes:		[DRIVER] operating modes supported by device
79  * @currentmode:	[DRIVER] current operating mode
80  * @dev:		[DRIVER] device structure, should be assigned a parent
81  *			and owner
82  * @attrs:		[DRIVER] general purpose device attributes
83  * @driver_module:	[DRIVER] module structure used to ensure correct
84  *			ownership of chrdevs etc
85  * @num_interrupt_lines:[DRIVER] number of physical interrupt lines from device
86  * @interrupts:		[INTERN] interrupt line specific event lists etc
87  * @event_attrs:	[DRIVER] event control attributes
88  * @event_conf_attrs:	[DRIVER] event configuration attributes
89  * @event_interfaces:	[INTERN] event chrdevs associated with interrupt lines
90  * @ring:		[DRIVER] any ring buffer present
91  * @mlock:		[INTERN] lock used to prevent simultaneous device state
92  *			changes
93  * @available_scan_masks: [DRIVER] optional array of allowed bitmasks
94  * @trig:		[INTERN] current device trigger (ring buffer modes)
95  * @pollfunc:		[DRIVER] function run on trigger being received
96  **/
97 struct iio_dev {
98 	int				id;
99 	void				*dev_data;
100 	int				modes;
101 	int				currentmode;
102 	struct device			dev;
103 	const struct attribute_group	*attrs;
104 	struct module			*driver_module;
105 
106 	int				num_interrupt_lines;
107 	struct iio_interrupt		**interrupts;
108 	struct attribute_group		*event_attrs;
109 	struct attribute_group		*event_conf_attrs;
110 
111 	struct iio_event_interface	*event_interfaces;
112 
113 	struct iio_ring_buffer		*ring;
114 	struct mutex			mlock;
115 
116 	u32				*available_scan_masks;
117 	struct iio_trigger		*trig;
118 	struct iio_poll_func		*pollfunc;
119 };
120 
121 /**
122  * iio_device_register() - register a device with the IIO subsystem
123  * @dev_info:		Device structure filled by the device driver
124  **/
125 int iio_device_register(struct iio_dev *dev_info);
126 
127 /**
128  * iio_device_unregister() - unregister a device from the IIO subsystem
129  * @dev_info:		Device structure representing the device.
130  **/
131 void iio_device_unregister(struct iio_dev *dev_info);
132 
133 /**
134  * struct iio_interrupt - wrapper used to allow easy handling of multiple
135  *			physical interrupt lines
136  * @dev_info:		the iio device for which the is an interrupt line
137  * @line_number:	associated line number
138  * @id:			ida allocated unique id number
139  * @irq:		associate interrupt number
140  * @ev_list:		event handler list for associated events
141  * @ev_list_lock:	ensure only one access to list at a time
142  **/
143 struct iio_interrupt {
144 	struct iio_dev			*dev_info;
145 	int				line_number;
146 	int				id;
147 	int				irq;
148 	struct list_head		ev_list;
149 	spinlock_t			ev_list_lock;
150 };
151 
152 #define to_iio_interrupt(i) container_of(i, struct iio_interrupt, ev_list)
153 
154 /**
155  * iio_register_interrupt_line() - Tell IIO about interrupt lines
156  *
157  * @irq:		Typically provided via platform data
158  * @dev_info:		IIO device info structure for device
159  * @line_number:	Which interrupt line of the device is this?
160  * @type:		Interrupt type (e.g. edge triggered etc)
161  * @name:		Identifying name.
162  **/
163 int iio_register_interrupt_line(unsigned int			irq,
164 				struct iio_dev			*dev_info,
165 				int				line_number,
166 				unsigned long			type,
167 				const char			*name);
168 
169 void iio_unregister_interrupt_line(struct iio_dev *dev_info,
170 				   int line_number);
171 
172 
173 
174 /**
175  * iio_push_event() - try to add event to the list for userspace reading
176  * @dev_info:		IIO device structure
177  * @ev_line:		Which event line (hardware interrupt)
178  * @ev_code:		What event
179  * @timestamp:		When the event occurred
180  **/
181 int iio_push_event(struct iio_dev *dev_info,
182 		  int ev_line,
183 		  int ev_code,
184 		  s64 timestamp);
185 
186 /**
187  * __iio_push_event() - tries to add an event to the list associated with a chrdev
188  * @ev_int:		the event interface to which we are pushing the event
189  * @ev_code:		the outgoing event code
190  * @timestamp:		timestamp of the event
191  * @shared_pointer_p:	the shared event pointer
192  **/
193 int __iio_push_event(struct iio_event_interface *ev_int,
194 		    int ev_code,
195 		    s64 timestamp,
196 		    struct iio_shared_ev_pointer*
197 		    shared_pointer_p);
198 /**
199  * __iio_change_event() - change an event code in case of event escalation
200  * @ev:			the event to be changed
201  * @ev_code:		new event code
202  * @timestamp:		new timestamp
203  **/
204 void __iio_change_event(struct iio_detected_event_list *ev,
205 			int ev_code,
206 			s64 timestamp);
207 
208 /**
209  * iio_setup_ev_int() - configure an event interface (chrdev)
210  * @name:		name used for resulting sysfs directory etc.
211  * @ev_int:		interface we are configuring
212  * @owner:		module that is responsible for registering this ev_int
213  * @dev:		device whose ev_int this is
214  **/
215 int iio_setup_ev_int(struct iio_event_interface *ev_int,
216 		     const char *name,
217 		     struct module *owner,
218 		     struct device *dev);
219 
220 void iio_free_ev_int(struct iio_event_interface *ev_int);
221 
222 /**
223  * iio_allocate_chrdev() - Allocate a chrdev
224  * @handler:	struct that contains relevant file handling for chrdev
225  * @dev_info:	iio_dev for which chrdev is being created
226  **/
227 int iio_allocate_chrdev(struct iio_handler *handler, struct iio_dev *dev_info);
228 void iio_deallocate_chrdev(struct iio_handler *handler);
229 
230 /* Used to distinguish between bipolar and unipolar scan elemenents.
231  * Whilst this may seem obvious, we may well want to change the representation
232  * in the future!*/
233 #define IIO_SIGNED(a) -(a)
234 #define IIO_UNSIGNED(a) (a)
235 
236 extern dev_t iio_devt;
237 extern struct bus_type iio_bus_type;
238 
239 /**
240  * iio_put_device() - reference counted deallocation of struct device
241  * @dev: the iio_device containing the device
242  **/
iio_put_device(struct iio_dev * dev)243 static inline void iio_put_device(struct iio_dev *dev)
244 {
245 	if (dev)
246 		put_device(&dev->dev);
247 };
248 
249 /**
250  * to_iio_dev() - get iio_dev for which we have the struct device
251  * @d: the struct device
252  **/
to_iio_dev(struct device * d)253 static inline struct iio_dev *to_iio_dev(struct device *d)
254 {
255 	return container_of(d, struct iio_dev, dev);
256 };
257 
258 /**
259  * iio_dev_get_devdata() - helper function gets device specific data
260  * @d: the iio_dev associated with the device
261  **/
iio_dev_get_devdata(struct iio_dev * d)262 static inline void *iio_dev_get_devdata(struct iio_dev *d)
263 {
264 	return d->dev_data;
265 }
266 
267 /**
268  * iio_allocate_device() - allocate an iio_dev from a driver
269  **/
270 struct iio_dev *iio_allocate_device(void);
271 
272 /**
273  * iio_free_device() - free an iio_dev from a driver
274  * @dev: the iio_dev associated with the device
275  **/
276 void iio_free_device(struct iio_dev *dev);
277 
278 /**
279  * iio_put() - internal module reference count reduce
280  **/
281 void iio_put(void);
282 
283 /**
284  * iio_get() - internal module reference count increase
285  **/
286 void iio_get(void);
287 
288 /**
289  * iio_device_get_chrdev_minor() - get an unused minor number
290  **/
291 int iio_device_get_chrdev_minor(void);
292 void iio_device_free_chrdev_minor(int val);
293 
294 /**
295  * iio_ring_enabled() - helper function to test if any form of ring is enabled
296  * @dev_info:		IIO device info structure for device
297  **/
iio_ring_enabled(struct iio_dev * dev_info)298 static inline bool iio_ring_enabled(struct iio_dev *dev_info)
299 {
300 	return dev_info->currentmode
301 		& (INDIO_RING_TRIGGERED
302 		   | INDIO_RING_HARDWARE_BUFFER);
303 };
304 
305 struct ida;
306 
307 int iio_get_new_ida_val(struct ida *this_ida);
308 void iio_free_ida_val(struct ida *this_ida, int id);
309 #endif /* _INDUSTRIAL_IO_H_ */
310