1 /* The industrial I/O core
3 * Copyright (c) 2008 Jonathan Cameron
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.
10 #ifndef _INDUSTRIAL_IO_H_
11 #define _INDUSTRIAL_IO_H_
13 #include <linux/device.h>
14 #include <linux/cdev.h>
19 /* Static device specific elements (conversion factors etc)
20 * should be exported via sysfs
22 * Provide means of adjusting timer accuracy.
23 * Currently assumes nano seconds.
26 /* Event interface flags */
27 #define IIO_BUSY_BIT_POS 1
32 * iio_get_time_ns() - utility function to get a time stamp for events etc
34 static inline s64 iio_get_time_ns(void)
38 * calls getnstimeofday.
39 * If hrtimers then up to ns accurate, if not microsecond.
41 ktime_get_real_ts(&ts);
43 return timespec_to_ns(&ts);
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.
51 * Does reference counting to allow shared handlers.
53 void iio_add_event_to_list(struct iio_event_handler_list *el,
54 struct list_head *head);
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.
61 * Does reference counting to allow shared handlers.
63 void iio_remove_event_from_list(struct iio_event_handler_list *el,
64 struct list_head *head);
66 /* Device operating modes */
67 #define INDIO_DIRECT_MODE 0x01
68 #define INDIO_RING_TRIGGERED 0x02
69 #define INDIO_RING_HARDWARE_BUFFER 0x08
71 #define INDIO_ALL_RING_MODES (INDIO_RING_TRIGGERED | INDIO_RING_HARDWARE_BUFFER)
73 /* Vast majority of this is set by the industrialio subsystem on a
74 * call to iio_device_register. */
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
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
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
110 const struct attribute_group *attrs;
111 struct module *driver_module;
113 int num_interrupt_lines;
114 struct iio_interrupt **interrupts;
115 struct attribute_group *event_attrs;
116 struct attribute_group *event_conf_attrs;
118 struct iio_event_interface *event_interfaces;
120 struct iio_ring_buffer *ring;
123 struct attribute_group *scan_el_attrs;
127 u32 *available_scan_masks;
129 struct iio_trigger *trig;
130 struct iio_poll_func *pollfunc;
134 * These are mainly provided to allow for a change of implementation if a device
135 * has a large number of scan elements
137 #define IIO_MAX_SCAN_LENGTH 31
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)
143 if (!(~*av_masks & mask))
150 static inline int iio_scan_mask_query(struct iio_dev *dev_info, int bit)
154 if (bit > IIO_MAX_SCAN_LENGTH)
157 if (!dev_info->scan_mask)
160 if (dev_info->available_scan_masks)
161 mask = iio_scan_mask_match(dev_info->available_scan_masks,
162 dev_info->scan_mask);
164 mask = dev_info->scan_mask;
169 return !!(mask & (1 << bit));
172 static inline int iio_scan_mask_set(struct iio_dev *dev_info, int bit)
175 u32 trialmask = dev_info->scan_mask | (1 << bit);
177 if (bit > IIO_MAX_SCAN_LENGTH)
179 if (dev_info->available_scan_masks) {
180 mask = iio_scan_mask_match(dev_info->available_scan_masks,
185 dev_info->scan_mask = trialmask;
186 dev_info->scan_count++;
191 static inline int iio_scan_mask_clear(struct iio_dev *dev_info, int bit)
193 if (bit > IIO_MAX_SCAN_LENGTH)
195 dev_info->scan_mask &= ~(1 << bit);
196 dev_info->scan_count--;
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
205 static inline int iio_scan_mask_count_to_right(struct iio_dev *dev_info,
209 int mask = (1 << bit);
210 if (bit > IIO_MAX_SCAN_LENGTH)
214 if (mask & dev_info->scan_mask)
222 * iio_device_register() - register a device with the IIO subsystem
223 * @dev_info: Device structure filled by the device driver
225 int iio_device_register(struct iio_dev *dev_info);
228 * iio_device_unregister() - unregister a device from the IIO subsystem
229 * @dev_info: Device structure representing the device.
231 void iio_device_unregister(struct iio_dev *dev_info);
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
243 struct iio_interrupt {
244 struct iio_dev *dev_info;
248 struct list_head ev_list;
249 spinlock_t ev_list_lock;
252 #define to_iio_interrupt(i) container_of(i, struct iio_interrupt, ev_list)
255 * iio_register_interrupt_line() - Tell IIO about interrupt lines
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.
263 int iio_register_interrupt_line(unsigned int irq,
264 struct iio_dev *dev_info,
269 void iio_unregister_interrupt_line(struct iio_dev *dev_info,
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
281 int iio_push_event(struct iio_dev *dev_info,
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
294 struct iio_work_cont {
295 struct work_struct ws;
296 struct work_struct ws_nocheck;
302 #define to_iio_work_cont_check(_ws) \
303 container_of(_ws, struct iio_work_cont, ws)
305 #define to_iio_work_cont_no_check(_ws) \
306 container_of(_ws, struct iio_work_cont, ws_nocheck)
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
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)
323 INIT_WORK(&(cont)->ws, _checkfunc);
324 INIT_WORK(&(cont)->ws_nocheck, _nocheckfunc);
325 cont->address = _add;
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
336 int __iio_push_event(struct iio_event_interface *ev_int,
339 struct iio_shared_ev_pointer*
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
347 void __iio_change_event(struct iio_detected_event_list *ev,
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
358 int iio_setup_ev_int(struct iio_event_interface *ev_int,
360 struct module *owner,
363 void iio_free_ev_int(struct iio_event_interface *ev_int);
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
370 int iio_allocate_chrdev(struct iio_handler *handler, struct iio_dev *dev_info);
371 void iio_deallocate_chrdev(struct iio_handler *handler);
373 /* Used to distinguish between bipolar and unipolar scan elemenents.
374 * Whilst this may seem obvious, we may well want to change the representation
376 #define IIO_SIGNED(a) -(a)
377 #define IIO_UNSIGNED(a) (a)
379 extern dev_t iio_devt;
380 extern struct bus_type iio_bus_type;
383 * iio_put_device() - reference counted deallocation of struct device
384 * @dev: the iio_device containing the device
386 static inline void iio_put_device(struct iio_dev *dev)
389 put_device(&dev->dev);
393 * to_iio_dev() - get iio_dev for which we have the struct device
394 * @d: the struct device
396 static inline struct iio_dev *to_iio_dev(struct device *d)
398 return container_of(d, struct iio_dev, dev);
402 * iio_dev_get_devdata() - helper function gets device specific data
403 * @d: the iio_dev associated with the device
405 static inline void *iio_dev_get_devdata(struct iio_dev *d)
411 * iio_allocate_device() - allocate an iio_dev from a driver
413 struct iio_dev *iio_allocate_device(void);
416 * iio_free_device() - free an iio_dev from a driver
417 * @dev: the iio_dev associated with the device
419 void iio_free_device(struct iio_dev *dev);
422 * iio_put() - internal module reference count reduce
427 * iio_get() - internal module reference count increase
431 /* Ring buffer related */
432 int iio_device_get_chrdev_minor(void);
433 void iio_device_free_chrdev_minor(int val);
436 * iio_ring_enabled() - helper function to test if any form of ring is enabled
437 * @dev_info: IIO device info structure for device
439 static inline bool iio_ring_enabled(struct iio_dev *dev_info)
441 return dev_info->currentmode
442 & (INDIO_RING_TRIGGERED
443 | INDIO_RING_HARDWARE_BUFFER);
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_ */