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>
20 * Provide means of adjusting timer accuracy.
21 * Currently assumes nano seconds.
24 /* Event interface flags */
25 #define IIO_BUSY_BIT_POS 1
30 * iio_get_time_ns() - utility function to get a time stamp for events etc
32 static inline s64
iio_get_time_ns(void)
36 * calls getnstimeofday.
37 * If hrtimers then up to ns accurate, if not microsecond.
39 ktime_get_real_ts(&ts
);
41 return timespec_to_ns(&ts
);
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.
49 * Does reference counting to allow shared handlers.
51 void iio_add_event_to_list(struct iio_event_handler_list
*el
,
52 struct list_head
*head
);
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.
59 * Does reference counting to allow shared handlers.
61 void iio_remove_event_from_list(struct iio_event_handler_list
*el
,
62 struct list_head
*head
);
64 /* Device operating modes */
65 #define INDIO_DIRECT_MODE 0x01
66 #define INDIO_RING_TRIGGERED 0x02
67 #define INDIO_RING_HARDWARE_BUFFER 0x08
69 #define INDIO_ALL_RING_MODES (INDIO_RING_TRIGGERED | INDIO_RING_HARDWARE_BUFFER)
71 /* Vast majority of this is set by the industrialio subsystem on a
72 * call to iio_device_register. */
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
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
93 * @scan_el_attrs: [DRIVER] control of scan elements if that scan mode
94 * control method is used
95 * @scan_count: [INTERN] the number of elements in the current scan mode
96 * @scan_mask: [INTERN] bitmask used in masking scan mode elements
97 * @available_scan_masks: [DRIVER] optional array of allowed bitmasks
98 * @scan_timestamp: [INTERN] does the scan mode include a timestamp
99 * @trig: [INTERN] current device trigger (ring buffer modes)
100 * @pollfunc: [DRIVER] function run on trigger being recieved
108 const struct attribute_group
*attrs
;
109 struct module
*driver_module
;
111 int num_interrupt_lines
;
112 struct iio_interrupt
**interrupts
;
113 struct attribute_group
*event_attrs
;
114 struct attribute_group
*event_conf_attrs
;
116 struct iio_event_interface
*event_interfaces
;
118 struct iio_ring_buffer
*ring
;
121 struct attribute_group
*scan_el_attrs
;
125 u32
*available_scan_masks
;
127 struct iio_trigger
*trig
;
128 struct iio_poll_func
*pollfunc
;
132 * These are mainly provided to allow for a change of implementation if a device
133 * has a large number of scan elements
135 #define IIO_MAX_SCAN_LENGTH 31
137 /* note 0 used as error indicator as it doesn't make sense. */
138 static inline u32
iio_scan_mask_match(u32
*av_masks
, u32 mask
)
141 if (!(~*av_masks
& mask
))
148 static inline int iio_scan_mask_query(struct iio_dev
*dev_info
, int bit
)
152 if (bit
> IIO_MAX_SCAN_LENGTH
)
155 if (!dev_info
->scan_mask
)
158 if (dev_info
->available_scan_masks
)
159 mask
= iio_scan_mask_match(dev_info
->available_scan_masks
,
160 dev_info
->scan_mask
);
162 mask
= dev_info
->scan_mask
;
167 return !!(mask
& (1 << bit
));
170 static inline int iio_scan_mask_set(struct iio_dev
*dev_info
, int bit
)
173 u32 trialmask
= dev_info
->scan_mask
| (1 << bit
);
175 if (bit
> IIO_MAX_SCAN_LENGTH
)
177 if (dev_info
->available_scan_masks
) {
178 mask
= iio_scan_mask_match(dev_info
->available_scan_masks
,
183 dev_info
->scan_mask
= trialmask
;
184 dev_info
->scan_count
++;
189 static inline int iio_scan_mask_clear(struct iio_dev
*dev_info
, int bit
)
191 if (bit
> IIO_MAX_SCAN_LENGTH
)
193 dev_info
->scan_mask
&= ~(1 << bit
);
194 dev_info
->scan_count
--;
199 * iio_scan_mask_count_to_right() - how many scan elements occur before here
200 * @dev_info: the iio_device whose scan mode we are querying
201 * @bit: which number scan element is this
203 static inline int iio_scan_mask_count_to_right(struct iio_dev
*dev_info
,
207 int mask
= (1 << bit
);
208 if (bit
> IIO_MAX_SCAN_LENGTH
)
212 if (mask
& dev_info
->scan_mask
)
220 * iio_device_register() - register a device with the IIO subsystem
221 * @dev_info: Device structure filled by the device driver
223 int iio_device_register(struct iio_dev
*dev_info
);
226 * iio_device_unregister() - unregister a device from the IIO subsystem
227 * @dev_info: Device structure representing the device.
229 void iio_device_unregister(struct iio_dev
*dev_info
);
232 * struct iio_interrupt - wrapper used to allow easy handling of multiple
233 * physical interrupt lines
234 * @dev_info: the iio device for which the is an interrupt line
235 * @line_number: associated line number
236 * @id: idr allocated unique id number
237 * @irq: associate interrupt number
238 * @ev_list: event handler list for associated events
239 * @ev_list_lock: ensure only one access to list at a time
241 struct iio_interrupt
{
242 struct iio_dev
*dev_info
;
246 struct list_head ev_list
;
247 spinlock_t ev_list_lock
;
250 #define to_iio_interrupt(i) container_of(i, struct iio_interrupt, ev_list)
253 * iio_register_interrupt_line() - Tell IIO about interrupt lines
255 * @irq: Typically provided via platform data
256 * @dev_info: IIO device info structure for device
257 * @line_number: Which interrupt line of the device is this?
258 * @type: Interrupt type (e.g. edge triggered etc)
259 * @name: Identifying name.
261 int iio_register_interrupt_line(unsigned int irq
,
262 struct iio_dev
*dev_info
,
267 void iio_unregister_interrupt_line(struct iio_dev
*dev_info
,
273 * iio_push_event() - try to add event to the list for userspace reading
274 * @dev_info: IIO device structure
275 * @ev_line: Which event line (hardware interrupt)
276 * @ev_code: What event
277 * @timestamp: When the event occurred
279 int iio_push_event(struct iio_dev
*dev_info
,
285 * __iio_push_event() - tries to add an event to the list associated with a chrdev
286 * @ev_int: the event interface to which we are pushing the event
287 * @ev_code: the outgoing event code
288 * @timestamp: timestamp of the event
289 * @shared_pointer_p: the shared event pointer
291 int __iio_push_event(struct iio_event_interface
*ev_int
,
294 struct iio_shared_ev_pointer
*
297 * __iio_change_event() - change an event code in case of event escalation
298 * @ev: the event to be changed
299 * @ev_code: new event code
300 * @timestamp: new timestamp
302 void __iio_change_event(struct iio_detected_event_list
*ev
,
307 * iio_setup_ev_int() - configure an event interface (chrdev)
308 * @name: name used for resulting sysfs directory etc.
309 * @ev_int: interface we are configuring
310 * @owner: module that is responsible for registering this ev_int
311 * @dev: device whose ev_int this is
313 int iio_setup_ev_int(struct iio_event_interface
*ev_int
,
315 struct module
*owner
,
318 void iio_free_ev_int(struct iio_event_interface
*ev_int
);
321 * iio_allocate_chrdev() - Allocate a chrdev
322 * @handler: struct that contains relevant file handling for chrdev
323 * @dev_info: iio_dev for which chrdev is being created
325 int iio_allocate_chrdev(struct iio_handler
*handler
, struct iio_dev
*dev_info
);
326 void iio_deallocate_chrdev(struct iio_handler
*handler
);
328 /* Used to distinguish between bipolar and unipolar scan elemenents.
329 * Whilst this may seem obvious, we may well want to change the representation
331 #define IIO_SIGNED(a) -(a)
332 #define IIO_UNSIGNED(a) (a)
334 extern dev_t iio_devt
;
335 extern struct bus_type iio_bus_type
;
338 * iio_put_device() - reference counted deallocation of struct device
339 * @dev: the iio_device containing the device
341 static inline void iio_put_device(struct iio_dev
*dev
)
344 put_device(&dev
->dev
);
348 * to_iio_dev() - get iio_dev for which we have the struct device
349 * @d: the struct device
351 static inline struct iio_dev
*to_iio_dev(struct device
*d
)
353 return container_of(d
, struct iio_dev
, dev
);
357 * iio_dev_get_devdata() - helper function gets device specific data
358 * @d: the iio_dev associated with the device
360 static inline void *iio_dev_get_devdata(struct iio_dev
*d
)
366 * iio_allocate_device() - allocate an iio_dev from a driver
368 struct iio_dev
*iio_allocate_device(void);
371 * iio_free_device() - free an iio_dev from a driver
372 * @dev: the iio_dev associated with the device
374 void iio_free_device(struct iio_dev
*dev
);
377 * iio_put() - internal module reference count reduce
382 * iio_get() - internal module reference count increase
387 * iio_device_get_chrdev_minor() - get an unused minor number
389 int iio_device_get_chrdev_minor(void);
390 void iio_device_free_chrdev_minor(int val
);
393 * iio_ring_enabled() - helper function to test if any form of ring is enabled
394 * @dev_info: IIO device info structure for device
396 static inline bool iio_ring_enabled(struct iio_dev
*dev_info
)
398 return dev_info
->currentmode
399 & (INDIO_RING_TRIGGERED
400 | INDIO_RING_HARDWARE_BUFFER
);
405 int iio_get_new_idr_val(struct idr
*this_idr
);
406 void iio_free_idr_val(struct idr
*this_idr
, int id
);
407 #endif /* _INDUSTRIAL_IO_H_ */