Merge branch 'misc' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild-2.6
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / iio / iio.h
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 /* Static device specific elements (conversion factors etc)
20 * should be exported via sysfs
21 *
22 * Provide means of adjusting timer accuracy.
23 * Currently assumes nano seconds.
24 */
25
26 /* Event interface flags */
27 #define IIO_BUSY_BIT_POS 1
28
29 struct iio_dev;
30
31 /**
32 * iio_get_time_ns() - utility function to get a time stamp for events etc
33 **/
34 static inline s64 iio_get_time_ns(void)
35 {
36 struct timespec ts;
37 /*
38 * calls getnstimeofday.
39 * If hrtimers then up to ns accurate, if not microsecond.
40 */
41 ktime_get_real_ts(&ts);
42
43 return timespec_to_ns(&ts);
44 }
45
46 /**
47 * iio_add_event_to_list() - Wraps adding to event lists
48 * @el: the list element of the event to be handled.
49 * @head: the list associated with the event handler being used.
50 *
51 * Does reference counting to allow shared handlers.
52 **/
53 void iio_add_event_to_list(struct iio_event_handler_list *el,
54 struct list_head *head);
55
56 /**
57 * iio_remove_event_from_list() - Wraps removing from event list
58 * @el: element to be removed
59 * @head: associate list head for the interrupt handler.
60 *
61 * Does reference counting to allow shared handlers.
62 **/
63 void iio_remove_event_from_list(struct iio_event_handler_list *el,
64 struct list_head *head);
65
66 /* Device operating modes */
67 #define INDIO_DIRECT_MODE 0x01
68 #define INDIO_RING_TRIGGERED 0x02
69 #define INDIO_RING_HARDWARE_BUFFER 0x08
70
71 #define INDIO_ALL_RING_MODES (INDIO_RING_TRIGGERED | INDIO_RING_HARDWARE_BUFFER)
72
73 /* Vast majority of this is set by the industrialio subsystem on a
74 * call to iio_device_register. */
75
76 /**
77 * struct iio_dev - industrial I/O device
78 * @id: [INTERN] used to identify device internally
79 * @dev_data: [DRIVER] device specific data
80 * @modes: [DRIVER] operating modes supported by device
81 * @currentmode: [DRIVER] current operating mode
82 * @dev: [DRIVER] device structure, should be assigned a parent
83 * and owner
84 * @attrs: [DRIVER] general purpose device attributes
85 * @driver_module: [DRIVER] module structure used to ensure correct
86 * ownership of chrdevs etc
87 * @num_interrupt_lines:[DRIVER] number of physical interrupt lines from device
88 * @interrupts: [INTERN] interrupt line specific event lists etc
89 * @event_attrs: [DRIVER] event control attributes
90 * @event_conf_attrs: [DRIVER] event configuration attributes
91 * @event_interfaces: [INTERN] event chrdevs associated with interrupt lines
92 * @ring: [DRIVER] any ring buffer present
93 * @mlock: [INTERN] lock used to prevent simultaneous device state
94 * changes
95 * @scan_el_attrs: [DRIVER] control of scan elements if that scan mode
96 * control method is used
97 * @scan_count: [INTERN] the number of elements in the current scan mode
98 * @scan_mask: [INTERN] bitmask used in masking scan mode elements
99 * @available_scan_masks: [DRIVER] optional array of allowed bitmasks
100 * @scan_timestamp: [INTERN] does the scan mode include a timestamp
101 * @trig: [INTERN] current device trigger (ring buffer modes)
102 * @pollfunc: [DRIVER] function run on trigger being recieved
103 **/
104 struct iio_dev {
105 int id;
106 void *dev_data;
107 int modes;
108 int currentmode;
109 struct device dev;
110 const struct attribute_group *attrs;
111 struct module *driver_module;
112
113 int num_interrupt_lines;
114 struct iio_interrupt **interrupts;
115 struct attribute_group *event_attrs;
116 struct attribute_group *event_conf_attrs;
117
118 struct iio_event_interface *event_interfaces;
119
120 struct iio_ring_buffer *ring;
121 struct mutex mlock;
122
123 struct attribute_group *scan_el_attrs;
124 int scan_count;
125
126 u32 scan_mask;
127 u32 *available_scan_masks;
128 bool scan_timestamp;
129 struct iio_trigger *trig;
130 struct iio_poll_func *pollfunc;
131 };
132
133 /*
134 * These are mainly provided to allow for a change of implementation if a device
135 * has a large number of scan elements
136 */
137 #define IIO_MAX_SCAN_LENGTH 31
138
139 /* note 0 used as error indicator as it doesn't make sense. */
140 static inline u32 iio_scan_mask_match(u32 *av_masks, u32 mask)
141 {
142 while (*av_masks) {
143 if (!(~*av_masks & mask))
144 return *av_masks;
145 av_masks++;
146 }
147 return 0;
148 }
149
150 static inline int iio_scan_mask_query(struct iio_dev *dev_info, int bit)
151 {
152 u32 mask;
153
154 if (bit > IIO_MAX_SCAN_LENGTH)
155 return -EINVAL;
156
157 if (!dev_info->scan_mask)
158 return 0;
159
160 if (dev_info->available_scan_masks)
161 mask = iio_scan_mask_match(dev_info->available_scan_masks,
162 dev_info->scan_mask);
163 else
164 mask = dev_info->scan_mask;
165
166 if (!mask)
167 return -EINVAL;
168
169 return !!(mask & (1 << bit));
170 };
171
172 static inline int iio_scan_mask_set(struct iio_dev *dev_info, int bit)
173 {
174 u32 mask;
175 u32 trialmask = dev_info->scan_mask | (1 << bit);
176
177 if (bit > IIO_MAX_SCAN_LENGTH)
178 return -EINVAL;
179 if (dev_info->available_scan_masks) {
180 mask = iio_scan_mask_match(dev_info->available_scan_masks,
181 trialmask);
182 if (!mask)
183 return -EINVAL;
184 }
185 dev_info->scan_mask = trialmask;
186 dev_info->scan_count++;
187
188 return 0;
189 };
190
191 static inline int iio_scan_mask_clear(struct iio_dev *dev_info, int bit)
192 {
193 if (bit > IIO_MAX_SCAN_LENGTH)
194 return -EINVAL;
195 dev_info->scan_mask &= ~(1 << bit);
196 dev_info->scan_count--;
197 return 0;
198 };
199
200 /**
201 * iio_scan_mask_count_to_right() - how many scan elements occur before here
202 * @dev_info: the iio_device whose scan mode we are querying
203 * @bit: which number scan element is this
204 **/
205 static inline int iio_scan_mask_count_to_right(struct iio_dev *dev_info,
206 int bit)
207 {
208 int count = 0;
209 int mask = (1 << bit);
210 if (bit > IIO_MAX_SCAN_LENGTH)
211 return -EINVAL;
212 while (mask) {
213 mask >>= 1;
214 if (mask & dev_info->scan_mask)
215 count++;
216 }
217
218 return count;
219 }
220
221 /**
222 * iio_device_register() - register a device with the IIO subsystem
223 * @dev_info: Device structure filled by the device driver
224 **/
225 int iio_device_register(struct iio_dev *dev_info);
226
227 /**
228 * iio_device_unregister() - unregister a device from the IIO subsystem
229 * @dev_info: Device structure representing the device.
230 **/
231 void iio_device_unregister(struct iio_dev *dev_info);
232
233 /**
234 * struct iio_interrupt - wrapper used to allow easy handling of multiple
235 * physical interrupt lines
236 * @dev_info: the iio device for which the is an interrupt line
237 * @line_number: associated line number
238 * @id: idr allocated unique id number
239 * @irq: associate interrupt number
240 * @ev_list: event handler list for associated events
241 * @ev_list_lock: ensure only one access to list at a time
242 **/
243 struct iio_interrupt {
244 struct iio_dev *dev_info;
245 int line_number;
246 int id;
247 int irq;
248 struct list_head ev_list;
249 spinlock_t ev_list_lock;
250 };
251
252 #define to_iio_interrupt(i) container_of(i, struct iio_interrupt, ev_list)
253
254 /**
255 * iio_register_interrupt_line() - Tell IIO about interrupt lines
256 *
257 * @irq: Typically provided via platform data
258 * @dev_info: IIO device info structure for device
259 * @line_number: Which interrupt line of the device is this?
260 * @type: Interrupt type (e.g. edge triggered etc)
261 * @name: Identifying name.
262 **/
263 int iio_register_interrupt_line(unsigned int irq,
264 struct iio_dev *dev_info,
265 int line_number,
266 unsigned long type,
267 const char *name);
268
269 void iio_unregister_interrupt_line(struct iio_dev *dev_info,
270 int line_number);
271
272
273
274 /**
275 * iio_push_event() - try to add event to the list for userspace reading
276 * @dev_info: IIO device structure
277 * @ev_line: Which event line (hardware interrupt)
278 * @ev_code: What event
279 * @timestamp: When the event occurred
280 **/
281 int iio_push_event(struct iio_dev *dev_info,
282 int ev_line,
283 int ev_code,
284 s64 timestamp);
285
286 /**
287 * struct iio_work_cont - container for when singleton handler case matters
288 * @ws: [DEVICE] work_struct when not only possible event
289 * @ws_nocheck: [DEVICE] work_struct when only possible event
290 * @address: [DEVICE] associated register address
291 * @mask: [DEVICE] associated mask for identifying event source
292 * @st: [DEVICE] device specific state information
293 **/
294 struct iio_work_cont {
295 struct work_struct ws;
296 struct work_struct ws_nocheck;
297 int address;
298 int mask;
299 void *st;
300 };
301
302 #define to_iio_work_cont_check(_ws) \
303 container_of(_ws, struct iio_work_cont, ws)
304
305 #define to_iio_work_cont_no_check(_ws) \
306 container_of(_ws, struct iio_work_cont, ws_nocheck)
307
308 /**
309 * iio_init_work_cont() - intiialize the elements of a work container
310 * @cont: the work container
311 * @_checkfunc: function called when there are multiple possible int sources
312 * @_nocheckfunc: function for when there is only one int source
313 * @_add: driver dependent, typically a register address
314 * @_mask: driver dependent, typically a bit mask for a register
315 * @_st: driver dependent, typically pointer to a device state structure
316 **/
317 static inline void
318 iio_init_work_cont(struct iio_work_cont *cont,
319 void (*_checkfunc)(struct work_struct *),
320 void (*_nocheckfunc)(struct work_struct *),
321 int _add, int _mask, void *_st)
322 {
323 INIT_WORK(&(cont)->ws, _checkfunc);
324 INIT_WORK(&(cont)->ws_nocheck, _nocheckfunc);
325 cont->address = _add;
326 cont->mask = _mask;
327 cont->st = _st;
328 }
329 /**
330 * __iio_push_event() - tries to add an event to the list associated with a chrdev
331 * @ev_int: the event interface to which we are pushing the event
332 * @ev_code: the outgoing event code
333 * @timestamp: timestamp of the event
334 * @shared_pointer_p: the shared event pointer
335 **/
336 int __iio_push_event(struct iio_event_interface *ev_int,
337 int ev_code,
338 s64 timestamp,
339 struct iio_shared_ev_pointer*
340 shared_pointer_p);
341 /**
342 * __iio_change_event() - change an event code in case of event escalation
343 * @ev: the event to be changed
344 * @ev_code: new event code
345 * @timestamp: new timestamp
346 **/
347 void __iio_change_event(struct iio_detected_event_list *ev,
348 int ev_code,
349 s64 timestamp);
350
351 /**
352 * iio_setup_ev_int() - configure an event interface (chrdev)
353 * @name: name used for resulting sysfs directory etc.
354 * @ev_int: interface we are configuring
355 * @owner: module that is responsible for registering this ev_int
356 * @dev: device whose ev_int this is
357 **/
358 int iio_setup_ev_int(struct iio_event_interface *ev_int,
359 const char *name,
360 struct module *owner,
361 struct device *dev);
362
363 void iio_free_ev_int(struct iio_event_interface *ev_int);
364
365 /**
366 * iio_allocate_chrdev() - Allocate a chrdev
367 * @handler: struct that contains relevant file handling for chrdev
368 * @dev_info: iio_dev for which chrdev is being created
369 **/
370 int iio_allocate_chrdev(struct iio_handler *handler, struct iio_dev *dev_info);
371 void iio_deallocate_chrdev(struct iio_handler *handler);
372
373 /* Used to distinguish between bipolar and unipolar scan elemenents.
374 * Whilst this may seem obvious, we may well want to change the representation
375 * in the future!*/
376 #define IIO_SIGNED(a) -(a)
377 #define IIO_UNSIGNED(a) (a)
378
379 extern dev_t iio_devt;
380 extern struct bus_type iio_bus_type;
381
382 /**
383 * iio_put_device() - reference counted deallocation of struct device
384 * @dev: the iio_device containing the device
385 **/
386 static inline void iio_put_device(struct iio_dev *dev)
387 {
388 if (dev)
389 put_device(&dev->dev);
390 };
391
392 /**
393 * to_iio_dev() - get iio_dev for which we have the struct device
394 * @d: the struct device
395 **/
396 static inline struct iio_dev *to_iio_dev(struct device *d)
397 {
398 return container_of(d, struct iio_dev, dev);
399 };
400
401 /**
402 * iio_dev_get_devdata() - helper function gets device specific data
403 * @d: the iio_dev associated with the device
404 **/
405 static inline void *iio_dev_get_devdata(struct iio_dev *d)
406 {
407 return d->dev_data;
408 }
409
410 /**
411 * iio_allocate_device() - allocate an iio_dev from a driver
412 **/
413 struct iio_dev *iio_allocate_device(void);
414
415 /**
416 * iio_free_device() - free an iio_dev from a driver
417 * @dev: the iio_dev associated with the device
418 **/
419 void iio_free_device(struct iio_dev *dev);
420
421 /**
422 * iio_put() - internal module reference count reduce
423 **/
424 void iio_put(void);
425
426 /**
427 * iio_get() - internal module reference count increase
428 **/
429 void iio_get(void);
430
431 /* Ring buffer related */
432 int iio_device_get_chrdev_minor(void);
433 void iio_device_free_chrdev_minor(int val);
434
435 /**
436 * iio_ring_enabled() - helper function to test if any form of ring is enabled
437 * @dev_info: IIO device info structure for device
438 **/
439 static inline bool iio_ring_enabled(struct iio_dev *dev_info)
440 {
441 return dev_info->currentmode
442 & (INDIO_RING_TRIGGERED
443 | INDIO_RING_HARDWARE_BUFFER);
444 };
445
446 struct idr;
447
448 int iio_get_new_idr_val(struct idr *this_idr);
449 void iio_free_idr_val(struct idr *this_idr, int id);
450 #endif /* _INDUSTRIAL_IO_H_ */