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 * @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 recieved
103 const struct attribute_group
*attrs
;
104 struct module
*driver_module
;
106 int num_interrupt_lines
;
107 struct iio_interrupt
**interrupts
;
108 struct attribute_group
*event_attrs
;
109 struct attribute_group
*event_conf_attrs
;
111 struct iio_event_interface
*event_interfaces
;
113 struct iio_ring_buffer
*ring
;
116 u32
*available_scan_masks
;
117 struct iio_trigger
*trig
;
118 struct iio_poll_func
*pollfunc
;
122 * iio_device_register() - register a device with the IIO subsystem
123 * @dev_info: Device structure filled by the device driver
125 int iio_device_register(struct iio_dev
*dev_info
);
128 * iio_device_unregister() - unregister a device from the IIO subsystem
129 * @dev_info: Device structure representing the device.
131 void iio_device_unregister(struct iio_dev
*dev_info
);
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
143 struct iio_interrupt
{
144 struct iio_dev
*dev_info
;
148 struct list_head ev_list
;
149 spinlock_t ev_list_lock
;
152 #define to_iio_interrupt(i) container_of(i, struct iio_interrupt, ev_list)
155 * iio_register_interrupt_line() - Tell IIO about interrupt lines
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.
163 int iio_register_interrupt_line(unsigned int irq
,
164 struct iio_dev
*dev_info
,
169 void iio_unregister_interrupt_line(struct iio_dev
*dev_info
,
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
181 int iio_push_event(struct iio_dev
*dev_info
,
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
193 int __iio_push_event(struct iio_event_interface
*ev_int
,
196 struct iio_shared_ev_pointer
*
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
204 void __iio_change_event(struct iio_detected_event_list
*ev
,
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
215 int iio_setup_ev_int(struct iio_event_interface
*ev_int
,
217 struct module
*owner
,
220 void iio_free_ev_int(struct iio_event_interface
*ev_int
);
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
227 int iio_allocate_chrdev(struct iio_handler
*handler
, struct iio_dev
*dev_info
);
228 void iio_deallocate_chrdev(struct iio_handler
*handler
);
230 /* Used to distinguish between bipolar and unipolar scan elemenents.
231 * Whilst this may seem obvious, we may well want to change the representation
233 #define IIO_SIGNED(a) -(a)
234 #define IIO_UNSIGNED(a) (a)
236 extern dev_t iio_devt
;
237 extern struct bus_type iio_bus_type
;
240 * iio_put_device() - reference counted deallocation of struct device
241 * @dev: the iio_device containing the device
243 static inline void iio_put_device(struct iio_dev
*dev
)
246 put_device(&dev
->dev
);
250 * to_iio_dev() - get iio_dev for which we have the struct device
251 * @d: the struct device
253 static inline struct iio_dev
*to_iio_dev(struct device
*d
)
255 return container_of(d
, struct iio_dev
, dev
);
259 * iio_dev_get_devdata() - helper function gets device specific data
260 * @d: the iio_dev associated with the device
262 static inline void *iio_dev_get_devdata(struct iio_dev
*d
)
268 * iio_allocate_device() - allocate an iio_dev from a driver
270 struct iio_dev
*iio_allocate_device(void);
273 * iio_free_device() - free an iio_dev from a driver
274 * @dev: the iio_dev associated with the device
276 void iio_free_device(struct iio_dev
*dev
);
279 * iio_put() - internal module reference count reduce
284 * iio_get() - internal module reference count increase
289 * iio_device_get_chrdev_minor() - get an unused minor number
291 int iio_device_get_chrdev_minor(void);
292 void iio_device_free_chrdev_minor(int val
);
295 * iio_ring_enabled() - helper function to test if any form of ring is enabled
296 * @dev_info: IIO device info structure for device
298 static inline bool iio_ring_enabled(struct iio_dev
*dev_info
)
300 return dev_info
->currentmode
301 & (INDIO_RING_TRIGGERED
302 | INDIO_RING_HARDWARE_BUFFER
);
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_ */