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 * @scan_timestamp: [INTERN] does the scan mode include a timestamp
100 * @trig: [INTERN] current device trigger (ring buffer modes)
101 * @pollfunc: [DRIVER] function run on trigger being recieved
109 const struct attribute_group
*attrs
;
110 struct module
*driver_module
;
112 int num_interrupt_lines
;
113 struct iio_interrupt
**interrupts
;
114 struct attribute_group
*event_attrs
;
115 struct attribute_group
*event_conf_attrs
;
117 struct iio_event_interface
*event_interfaces
;
119 struct iio_ring_buffer
*ring
;
122 struct attribute_group
*scan_el_attrs
;
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 15
137 static inline int iio_scan_mask_query(struct iio_dev
*dev_info
, int bit
)
139 if (bit
> IIO_MAX_SCAN_LENGTH
)
142 return !!(dev_info
->scan_mask
& (1 << bit
));
145 static inline int iio_scan_mask_set(struct iio_dev
*dev_info
, int bit
)
147 if (bit
> IIO_MAX_SCAN_LENGTH
)
149 dev_info
->scan_mask
|= (1 << bit
);
150 dev_info
->scan_count
++;
154 static inline int iio_scan_mask_clear(struct iio_dev
*dev_info
, int bit
)
156 if (bit
> IIO_MAX_SCAN_LENGTH
)
158 dev_info
->scan_mask
&= ~(1 << bit
);
159 dev_info
->scan_count
--;
164 * iio_scan_mask_count_to_right() - how many scan elements occur before here
165 * @dev_info: the iio_device whose scan mode we are querying
166 * @bit: which number scan element is this
168 static inline int iio_scan_mask_count_to_right(struct iio_dev
*dev_info
,
172 int mask
= (1 << bit
);
173 if (bit
> IIO_MAX_SCAN_LENGTH
)
177 if (mask
& dev_info
->scan_mask
)
185 * iio_device_register() - register a device with the IIO subsystem
186 * @dev_info: Device structure filled by the device driver
188 int iio_device_register(struct iio_dev
*dev_info
);
191 * iio_device_unregister() - unregister a device from the IIO subsystem
192 * @dev_info: Device structure representing the device.
194 void iio_device_unregister(struct iio_dev
*dev_info
);
197 * struct iio_interrupt - wrapper used to allow easy handling of multiple
198 * physical interrupt lines
199 * @dev_info: the iio device for which the is an interrupt line
200 * @line_number: associated line number
201 * @id: idr allocated unique id number
202 * @irq: associate interrupt number
203 * @ev_list: event handler list for associated events
204 * @ev_list_lock: ensure only one access to list at a time
206 struct iio_interrupt
{
207 struct iio_dev
*dev_info
;
211 struct list_head ev_list
;
212 spinlock_t ev_list_lock
;
215 #define to_iio_interrupt(i) container_of(i, struct iio_interrupt, ev_list)
218 * iio_register_interrupt_line() - Tell IIO about interrupt lines
220 * @irq: Typically provided via platform data
221 * @dev_info: IIO device info structure for device
222 * @line_number: Which interrupt line of the device is this?
223 * @type: Interrupt type (e.g. edge triggered etc)
224 * @name: Identifying name.
226 int iio_register_interrupt_line(unsigned int irq
,
227 struct iio_dev
*dev_info
,
232 void iio_unregister_interrupt_line(struct iio_dev
*dev_info
,
238 * iio_push_event() - try to add event to the list for userspace reading
239 * @dev_info: IIO device structure
240 * @ev_line: Which event line (hardware interrupt)
241 * @ev_code: What event
242 * @timestamp: When the event occured
244 int iio_push_event(struct iio_dev
*dev_info
,
250 * struct iio_work_cont - container for when singleton handler case matters
251 * @ws: [DEVICE]work_struct when not only possible event
252 * @ws_nocheck: [DEVICE]work_struct when only possible event
253 * @address: [DEVICE]associated register address
254 * @mask: [DEVICE]associated mask for identifying event source
255 * @st: [DEVICE]device specific state information
257 struct iio_work_cont
{
258 struct work_struct ws
;
259 struct work_struct ws_nocheck
;
265 #define to_iio_work_cont_check(_ws) \
266 container_of(_ws, struct iio_work_cont, ws)
268 #define to_iio_work_cont_no_check(_ws) \
269 container_of(_ws, struct iio_work_cont, ws_nocheck)
272 * iio_init_work_cont() - intiialize the elements of a work container
273 * @cont: the work container
274 * @_checkfunc: function called when there are multiple possible int sources
275 * @_nocheckfunc: function for when there is only one int source
276 * @_add: driver dependant, typically a register address
277 * @_mask: driver dependant, typically a bit mask for a register
278 * @_st: driver dependant, typically pointer to a device state structure
281 iio_init_work_cont(struct iio_work_cont
*cont
,
282 void (*_checkfunc
)(struct work_struct
*),
283 void (*_nocheckfunc
)(struct work_struct
*),
284 int _add
, int _mask
, void *_st
)
286 INIT_WORK(&(cont
)->ws
, _checkfunc
);
287 INIT_WORK(&(cont
)->ws_nocheck
, _nocheckfunc
);
288 cont
->address
= _add
;
293 * __iio_push_event() tries to add an event to the list associated with a chrdev
294 * @ev_int: the event interface to which we are pushing the event
295 * @ev_code: the outgoing event code
296 * @timestamp: timestamp of the event
297 * @shared_pointer_p: the shared event pointer
299 int __iio_push_event(struct iio_event_interface
*ev_int
,
302 struct iio_shared_ev_pointer
*
305 * __iio_change_event() change an event code in case of event escallation
306 * @ev: the evnet to be changed
307 * @ev_code: new event code
308 * @timestamp: new timestamp
310 void __iio_change_event(struct iio_detected_event_list
*ev
,
315 * iio_setup_ev_int() Configure an event interface (chrdev)
316 * @name: name used for resulting sysfs directory etc.
317 * @ev_int: interface we are configuring
318 * @owner: module that is responsible for registering this ev_int
319 * @dev: device whose ev_int this is
321 int iio_setup_ev_int(struct iio_event_interface
*ev_int
,
323 struct module
*owner
,
326 void iio_free_ev_int(struct iio_event_interface
*ev_int
);
329 * iio_allocate_chrdev() - Allocate a chrdev
330 * @handler: struct that contains relevant file handling for chrdev
331 * @dev_info: iio_dev for which chrdev is being created
333 int iio_allocate_chrdev(struct iio_handler
*handler
, struct iio_dev
*dev_info
);
334 void iio_deallocate_chrdev(struct iio_handler
*handler
);
336 /* Used to distinguish between bipolar and unipolar scan elemenents.
337 * Whilst this may seem obvious, we may well want to change the representation
339 #define IIO_SIGNED(a) -(a)
340 #define IIO_UNSIGNED(a) (a)
342 extern dev_t iio_devt
;
343 extern struct class iio_class
;
346 * iio_put_device() - reference counted deallocated of struct device
347 * @dev: the iio_device containing the device
349 static inline void iio_put_device(struct iio_dev
*dev
)
352 put_device(&dev
->dev
);
356 * to_iio_dev() - get iio_dev for which we have have the struct device
357 * @d: the struct device
359 static inline struct iio_dev
*to_iio_dev(struct device
*d
)
361 return container_of(d
, struct iio_dev
, dev
);
365 * iio_dev_get_devdata() - helper function gets device specific data
366 * @d: the iio_dev associated with the device
368 static inline void *iio_dev_get_devdata(struct iio_dev
*d
)
374 * iio_allocate_device() - allocate an iio_dev from a driver
376 struct iio_dev
*iio_allocate_device(void);
379 * iio_free_device() - free an iio_dev from a driver
381 void iio_free_device(struct iio_dev
*dev
);
384 * iio_put() - internal module reference count reduce
389 * iio_get() - internal module reference count increase
393 /* Ring buffer related */
394 int iio_device_get_chrdev_minor(void);
395 void iio_device_free_chrdev_minor(int val
);
398 * iio_ring_enabled() helper function to test if any form of ring enabled
400 static inline bool iio_ring_enabled(struct iio_dev
*dev_info
)
402 return dev_info
->currentmode
403 & (INDIO_RING_TRIGGERED
404 | INDIO_RING_HARDWARE_BUFFER
);
409 int iio_get_new_idr_val(struct idr
*this_idr
);
410 void iio_free_idr_val(struct idr
*this_idr
, int id
);
411 #endif /* _INDUSTRIAL_IO_H_ */