• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/components/opensource/linux/linux-2.6.36/drivers/staging/iio/
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/*
20 * Provide means of adjusting timer accuracy.
21 * Currently assumes nano seconds.
22 */
23
24/* Event interface flags */
25#define IIO_BUSY_BIT_POS 1
26
27struct iio_dev;
28
29/**
30 * iio_get_time_ns() - utility function to get a time stamp for events etc
31 **/
32static inline s64 iio_get_time_ns(void)
33{
34	struct timespec ts;
35	/*
36	 * calls getnstimeofday.
37	 * If hrtimers then up to ns accurate, if not microsecond.
38	 */
39	ktime_get_real_ts(&ts);
40
41	return timespec_to_ns(&ts);
42}
43
44/**
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.
48 *
49 * Does reference counting to allow shared handlers.
50 **/
51void iio_add_event_to_list(struct iio_event_handler_list *el,
52			   struct list_head *head);
53
54/**
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.
58 *
59 * Does reference counting to allow shared handlers.
60 **/
61void iio_remove_event_from_list(struct iio_event_handler_list *el,
62				struct list_head *head);
63
64/* Device operating modes */
65#define INDIO_DIRECT_MODE		0x01
66#define INDIO_RING_TRIGGERED		0x02
67#define INDIO_RING_HARDWARE_BUFFER	0x08
68
69#define INDIO_ALL_RING_MODES (INDIO_RING_TRIGGERED | INDIO_RING_HARDWARE_BUFFER)
70
71/* Vast majority of this is set by the industrialio subsystem on a
72 * call to iio_device_register. */
73
74/**
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
81 *			and owner
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
92 *			changes
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
101 **/
102struct iio_dev {
103	int				id;
104	void				*dev_data;
105	int				modes;
106	int				currentmode;
107	struct device			dev;
108	const struct attribute_group	*attrs;
109	struct module			*driver_module;
110
111	int				num_interrupt_lines;
112	struct iio_interrupt		**interrupts;
113	struct attribute_group		*event_attrs;
114	struct attribute_group		*event_conf_attrs;
115
116	struct iio_event_interface	*event_interfaces;
117
118	struct iio_ring_buffer		*ring;
119	struct mutex			mlock;
120
121	struct attribute_group		*scan_el_attrs;
122	int				scan_count;
123
124	u32				scan_mask;
125	u32				*available_scan_masks;
126	bool				scan_timestamp;
127	struct iio_trigger		*trig;
128	struct iio_poll_func		*pollfunc;
129};
130
131/*
132 * These are mainly provided to allow for a change of implementation if a device
133 * has a large number of scan elements
134 */
135#define IIO_MAX_SCAN_LENGTH 31
136
137/* note 0 used as error indicator as it doesn't make sense. */
138static inline u32 iio_scan_mask_match(u32 *av_masks, u32 mask)
139{
140	while (*av_masks) {
141		if (!(~*av_masks & mask))
142			return *av_masks;
143		av_masks++;
144	}
145	return 0;
146}
147
148static inline int iio_scan_mask_query(struct iio_dev *dev_info, int bit)
149{
150	u32 mask;
151
152	if (bit > IIO_MAX_SCAN_LENGTH)
153		return -EINVAL;
154
155	if (!dev_info->scan_mask)
156		return 0;
157
158	if (dev_info->available_scan_masks)
159		mask = iio_scan_mask_match(dev_info->available_scan_masks,
160					dev_info->scan_mask);
161	else
162		mask = dev_info->scan_mask;
163
164	if (!mask)
165		return -EINVAL;
166
167	return !!(mask & (1 << bit));
168};
169
170static inline int iio_scan_mask_set(struct iio_dev *dev_info, int bit)
171{
172	u32 mask;
173	u32 trialmask = dev_info->scan_mask | (1 << bit);
174
175	if (bit > IIO_MAX_SCAN_LENGTH)
176		return -EINVAL;
177	if (dev_info->available_scan_masks) {
178		mask = iio_scan_mask_match(dev_info->available_scan_masks,
179					trialmask);
180		if (!mask)
181			return -EINVAL;
182	}
183	dev_info->scan_mask = trialmask;
184	dev_info->scan_count++;
185
186	return 0;
187};
188
189static inline int iio_scan_mask_clear(struct iio_dev *dev_info, int bit)
190{
191	if (bit > IIO_MAX_SCAN_LENGTH)
192		return -EINVAL;
193	dev_info->scan_mask &= ~(1 << bit);
194	dev_info->scan_count--;
195	return 0;
196};
197
198/**
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
202 **/
203static inline int iio_scan_mask_count_to_right(struct iio_dev *dev_info,
204						int bit)
205{
206	int count = 0;
207	int mask = (1 << bit);
208	if (bit > IIO_MAX_SCAN_LENGTH)
209		return -EINVAL;
210	while (mask) {
211		mask >>= 1;
212		if (mask & dev_info->scan_mask)
213			count++;
214	}
215
216	return count;
217}
218
219/**
220 * iio_device_register() - register a device with the IIO subsystem
221 * @dev_info:		Device structure filled by the device driver
222 **/
223int iio_device_register(struct iio_dev *dev_info);
224
225/**
226 * iio_device_unregister() - unregister a device from the IIO subsystem
227 * @dev_info:		Device structure representing the device.
228 **/
229void iio_device_unregister(struct iio_dev *dev_info);
230
231/**
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
240 **/
241struct iio_interrupt {
242	struct iio_dev			*dev_info;
243	int				line_number;
244	int				id;
245	int				irq;
246	struct list_head		ev_list;
247	spinlock_t			ev_list_lock;
248};
249
250#define to_iio_interrupt(i) container_of(i, struct iio_interrupt, ev_list)
251
252/**
253 * iio_register_interrupt_line() - Tell IIO about interrupt lines
254 *
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.
260 **/
261int iio_register_interrupt_line(unsigned int			irq,
262				struct iio_dev			*dev_info,
263				int				line_number,
264				unsigned long			type,
265				const char			*name);
266
267void iio_unregister_interrupt_line(struct iio_dev *dev_info,
268				   int line_number);
269
270
271
272/**
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
278 **/
279int iio_push_event(struct iio_dev *dev_info,
280		  int ev_line,
281		  int ev_code,
282		  s64 timestamp);
283
284/**
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
290 **/
291int __iio_push_event(struct iio_event_interface *ev_int,
292		    int ev_code,
293		    s64 timestamp,
294		    struct iio_shared_ev_pointer*
295		    shared_pointer_p);
296/**
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
301 **/
302void __iio_change_event(struct iio_detected_event_list *ev,
303			int ev_code,
304			s64 timestamp);
305
306/**
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
312 **/
313int iio_setup_ev_int(struct iio_event_interface *ev_int,
314		     const char *name,
315		     struct module *owner,
316		     struct device *dev);
317
318void iio_free_ev_int(struct iio_event_interface *ev_int);
319
320/**
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
324 **/
325int iio_allocate_chrdev(struct iio_handler *handler, struct iio_dev *dev_info);
326void iio_deallocate_chrdev(struct iio_handler *handler);
327
328/* Used to distinguish between bipolar and unipolar scan elemenents.
329 * Whilst this may seem obvious, we may well want to change the representation
330 * in the future!*/
331#define IIO_SIGNED(a) -(a)
332#define IIO_UNSIGNED(a) (a)
333
334extern dev_t iio_devt;
335extern struct bus_type iio_bus_type;
336
337/**
338 * iio_put_device() - reference counted deallocation of struct device
339 * @dev: the iio_device containing the device
340 **/
341static inline void iio_put_device(struct iio_dev *dev)
342{
343	if (dev)
344		put_device(&dev->dev);
345};
346
347/**
348 * to_iio_dev() - get iio_dev for which we have the struct device
349 * @d: the struct device
350 **/
351static inline struct iio_dev *to_iio_dev(struct device *d)
352{
353	return container_of(d, struct iio_dev, dev);
354};
355
356/**
357 * iio_dev_get_devdata() - helper function gets device specific data
358 * @d: the iio_dev associated with the device
359 **/
360static inline void *iio_dev_get_devdata(struct iio_dev *d)
361{
362	return d->dev_data;
363}
364
365/**
366 * iio_allocate_device() - allocate an iio_dev from a driver
367 **/
368struct iio_dev *iio_allocate_device(void);
369
370/**
371 * iio_free_device() - free an iio_dev from a driver
372 * @dev: the iio_dev associated with the device
373 **/
374void iio_free_device(struct iio_dev *dev);
375
376/**
377 * iio_put() - internal module reference count reduce
378 **/
379void iio_put(void);
380
381/**
382 * iio_get() - internal module reference count increase
383 **/
384void iio_get(void);
385
386/**
387 * iio_device_get_chrdev_minor() - get an unused minor number
388 **/
389int iio_device_get_chrdev_minor(void);
390void iio_device_free_chrdev_minor(int val);
391
392/**
393 * iio_ring_enabled() - helper function to test if any form of ring is enabled
394 * @dev_info:		IIO device info structure for device
395 **/
396static inline bool iio_ring_enabled(struct iio_dev *dev_info)
397{
398	return dev_info->currentmode
399		& (INDIO_RING_TRIGGERED
400		   | INDIO_RING_HARDWARE_BUFFER);
401};
402
403struct idr;
404
405int iio_get_new_idr_val(struct idr *this_idr);
406void iio_free_idr_val(struct idr *this_idr, int id);
407#endif /* _INDUSTRIAL_IO_H_ */
408