Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / staging / iio / iio.h
blob753f769736ef3f5fe1f0101732f9a5a4d10a644f
2 /* The industrial I/O core
4 * Copyright (c) 2008 Jonathan Cameron
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 */
11 #ifndef _INDUSTRIAL_IO_H_
12 #define _INDUSTRIAL_IO_H_
14 #include <linux/device.h>
15 #include <linux/cdev.h>
16 #include <linux/irq.h>
17 #include "sysfs.h"
18 #include "chrdev.h"
20 /* IIO TODO LIST */
22 * Provide means of adjusting timer accuracy.
23 * Currently assumes nano seconds.
26 enum iio_chan_type {
27 /* real channel types */
28 IIO_IN,
29 IIO_OUT,
30 IIO_CURRENT,
31 IIO_POWER,
32 IIO_ACCEL,
33 IIO_IN_DIFF,
34 IIO_GYRO,
35 IIO_MAGN,
36 IIO_LIGHT,
37 IIO_INTENSITY,
38 IIO_PROXIMITY,
39 IIO_TEMP,
40 IIO_INCLI,
41 IIO_ROT,
42 IIO_ANGL,
43 IIO_TIMESTAMP,
46 #define IIO_MOD_X 0
47 #define IIO_MOD_LIGHT_BOTH 0
48 #define IIO_MOD_Y 1
49 #define IIO_MOD_LIGHT_IR 1
50 #define IIO_MOD_Z 2
51 #define IIO_MOD_X_AND_Y 3
52 #define IIO_MOD_X_ANX_Z 4
53 #define IIO_MOD_Y_AND_Z 5
54 #define IIO_MOD_X_AND_Y_AND_Z 6
55 #define IIO_MOD_X_OR_Y 7
56 #define IIO_MOD_X_OR_Z 8
57 #define IIO_MOD_Y_OR_Z 9
58 #define IIO_MOD_X_OR_Y_OR_Z 10
60 /* Could add the raw attributes as well - allowing buffer only devices */
61 enum iio_chan_info_enum {
62 IIO_CHAN_INFO_SCALE_SHARED,
63 IIO_CHAN_INFO_SCALE_SEPARATE,
64 IIO_CHAN_INFO_OFFSET_SHARED,
65 IIO_CHAN_INFO_OFFSET_SEPARATE,
66 IIO_CHAN_INFO_CALIBSCALE_SHARED,
67 IIO_CHAN_INFO_CALIBSCALE_SEPARATE,
68 IIO_CHAN_INFO_CALIBBIAS_SHARED,
69 IIO_CHAN_INFO_CALIBBIAS_SEPARATE,
70 IIO_CHAN_INFO_PEAK_SHARED,
71 IIO_CHAN_INFO_PEAK_SEPARATE,
72 IIO_CHAN_INFO_PEAK_SCALE_SHARED,
73 IIO_CHAN_INFO_PEAK_SCALE_SEPARATE,
74 IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW_SHARED,
75 IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW_SEPARATE,
78 /**
79 * struct iio_chan_spec - specification of a single channel
80 * @type: What type of measurement is the channel making.
81 * @channel: What number or name do we wish to asign the channel.
82 * @channel2: If there is a second number for a differential
83 * channel then this is it. If modified is set then the
84 * value here specifies the modifier.
85 * @address: Driver specific identifier.
86 * @scan_index: Monotonic index to give ordering in scans when read
87 * from a buffer.
88 * @scan_type: Sign: 's' or 'u' to specify signed or unsigned
89 * realbits: Number of valid bits of data
90 * storage_bits: Realbits + padding
91 * shift: Shift right by this before masking out
92 * realbits.
93 * @info_mask: What information is to be exported about this channel.
94 * This includes calibbias, scale etc.
95 * @event_mask: What events can this channel produce.
96 * @extend_name: Allows labeling of channel attributes with an
97 * informative name. Note this has no effect codes etc,
98 * unlike modifiers.
99 * @processed_val: Flag to specify the data access attribute should be
100 * *_input rather than *_raw.
101 * @modified: Does a modifier apply to this channel. What these are
102 * depends on the channel type. Modifier is set in
103 * channel2. Examples are IIO_MOD_X for axial sensors about
104 * the 'x' axis.
105 * @indexed: Specify the channel has a numerical index. If not,
106 * the value in channel will be suppressed for attribute
107 * but not for event codes. Typically set it to 0 when
108 * the index is false.
110 struct iio_chan_spec {
111 enum iio_chan_type type;
112 int channel;
113 int channel2;
114 unsigned long address;
115 int scan_index;
116 struct {
117 char sign;
118 u8 realbits;
119 u8 storagebits;
120 u8 shift;
121 } scan_type;
122 long info_mask;
123 long event_mask;
124 char *extend_name;
125 unsigned processed_val:1;
126 unsigned modified:1;
127 unsigned indexed:1;
130 #define IIO_ST(si, rb, sb, sh) \
131 { .sign = si, .realbits = rb, .storagebits = sb, .shift = sh }
133 #define IIO_CHAN(_type, _mod, _indexed, _proc, _name, _chan, _chan2, \
134 _inf_mask, _address, _si, _stype, _event_mask) \
135 { .type = _type, \
136 .modified = _mod, \
137 .indexed = _indexed, \
138 .processed_val = _proc, \
139 .extend_name = _name, \
140 .channel = _chan, \
141 .channel2 = _chan2, \
142 .info_mask = _inf_mask, \
143 .address = _address, \
144 .scan_index = _si, \
145 .scan_type = _stype, \
146 .event_mask = _event_mask }
148 #define IIO_CHAN_SOFT_TIMESTAMP(_si) \
149 { .type = IIO_TIMESTAMP, .channel = -1, \
150 .scan_index = _si, .scan_type = IIO_ST('s', 64, 64, 0) }
153 * iio_get_time_ns() - utility function to get a time stamp for events etc
155 static inline s64 iio_get_time_ns(void)
157 struct timespec ts;
159 * calls getnstimeofday.
160 * If hrtimers then up to ns accurate, if not microsecond.
162 ktime_get_real_ts(&ts);
164 return timespec_to_ns(&ts);
167 /* Device operating modes */
168 #define INDIO_DIRECT_MODE 0x01
169 #define INDIO_RING_TRIGGERED 0x02
170 #define INDIO_RING_HARDWARE_BUFFER 0x08
172 #define INDIO_ALL_RING_MODES (INDIO_RING_TRIGGERED | INDIO_RING_HARDWARE_BUFFER)
174 /* Vast majority of this is set by the industrialio subsystem on a
175 * call to iio_device_register. */
176 #define IIO_VAL_INT 1
177 #define IIO_VAL_INT_PLUS_MICRO 2
178 #define IIO_VAL_INT_PLUS_NANO 3
180 struct iio_trigger; /* forward declaration */
183 * struct iio_info - constant information about device
184 * @driver_module: module structure used to ensure correct
185 * ownership of chrdevs etc
186 * @num_interrupt_lines:number of physical interrupt lines from device
187 * @event_attrs: event control attributes
188 * @attrs: general purpose device attributes
189 * @read_raw: function to request a value from the device.
190 * mask specifies which value. Note 0 means a reading of
191 * the channel in question. Return value will specify the
192 * type of value returned by the device. val and val2 will
193 * contain the elements making up the returned value.
194 * @write_raw: function to write a value to the device.
195 * Parameters are the same as for read_raw.
196 * @write_raw_get_fmt: callback function to query the expected
197 * format/precision. If not set by the driver, write_raw
198 * returns IIO_VAL_INT_PLUS_MICRO.
199 * @read_event_config: find out if the event is enabled.
200 * @write_event_config: set if the event is enabled.
201 * @read_event_value: read a value associated with the event. Meaning
202 * is event dependant. event_code specifies which event.
203 * @write_event_value: write the value associate with the event.
204 * Meaning is event dependent.
205 * @validate_trigger: function to validate the trigger when the
206 * current trigger gets changed.
208 struct iio_info {
209 struct module *driver_module;
210 int num_interrupt_lines;
211 struct attribute_group *event_attrs;
212 const struct attribute_group *attrs;
214 int (*read_raw)(struct iio_dev *indio_dev,
215 struct iio_chan_spec const *chan,
216 int *val,
217 int *val2,
218 long mask);
220 int (*write_raw)(struct iio_dev *indio_dev,
221 struct iio_chan_spec const *chan,
222 int val,
223 int val2,
224 long mask);
226 int (*write_raw_get_fmt)(struct iio_dev *indio_dev,
227 struct iio_chan_spec const *chan,
228 long mask);
230 int (*read_event_config)(struct iio_dev *indio_dev,
231 int event_code);
233 int (*write_event_config)(struct iio_dev *indio_dev,
234 int event_code,
235 int state);
237 int (*read_event_value)(struct iio_dev *indio_dev,
238 int event_code,
239 int *val);
240 int (*write_event_value)(struct iio_dev *indio_dev,
241 int event_code,
242 int val);
243 int (*validate_trigger)(struct iio_dev *indio_dev,
244 struct iio_trigger *trig);
249 * struct iio_dev - industrial I/O device
250 * @id: [INTERN] used to identify device internally
251 * @modes: [DRIVER] operating modes supported by device
252 * @currentmode: [DRIVER] current operating mode
253 * @dev: [DRIVER] device structure, should be assigned a parent
254 * and owner
255 * @event_interfaces: [INTERN] event chrdevs associated with interrupt lines
256 * @ring: [DRIVER] any ring buffer present
257 * @mlock: [INTERN] lock used to prevent simultaneous device state
258 * changes
259 * @available_scan_masks: [DRIVER] optional array of allowed bitmasks
260 * @trig: [INTERN] current device trigger (ring buffer modes)
261 * @pollfunc: [DRIVER] function run on trigger being received
262 * @channels: [DRIVER] channel specification structure table
263 * @num_channels: [DRIVER] number of chanels specified in @channels.
264 * @channel_attr_list: [INTERN] keep track of automatically created channel
265 * attributes.
266 * @name: [DRIVER] name of the device.
268 struct iio_dev {
269 int id;
271 int modes;
272 int currentmode;
273 struct device dev;
275 struct iio_event_interface *event_interfaces;
277 struct iio_ring_buffer *ring;
278 struct mutex mlock;
280 u32 *available_scan_masks;
281 struct iio_trigger *trig;
282 struct iio_poll_func *pollfunc;
284 struct iio_chan_spec const *channels;
285 int num_channels;
287 struct list_head channel_attr_list;
288 const char *name;
289 const struct iio_info *info;
293 * iio_device_register() - register a device with the IIO subsystem
294 * @dev_info: Device structure filled by the device driver
296 int iio_device_register(struct iio_dev *dev_info);
299 * iio_device_unregister() - unregister a device from the IIO subsystem
300 * @dev_info: Device structure representing the device.
302 void iio_device_unregister(struct iio_dev *dev_info);
305 * iio_push_event() - try to add event to the list for userspace reading
306 * @dev_info: IIO device structure
307 * @ev_line: Which event line (hardware interrupt)
308 * @ev_code: What event
309 * @timestamp: When the event occurred
311 int iio_push_event(struct iio_dev *dev_info,
312 int ev_line,
313 int ev_code,
314 s64 timestamp);
316 extern struct bus_type iio_bus_type;
319 * iio_put_device() - reference counted deallocation of struct device
320 * @dev: the iio_device containing the device
322 static inline void iio_put_device(struct iio_dev *dev)
324 if (dev)
325 put_device(&dev->dev);
328 /* Can we make this smaller? */
329 #define IIO_ALIGN L1_CACHE_BYTES
331 * iio_allocate_device() - allocate an iio_dev from a driver
332 * @sizeof_priv: Space to allocate for private structure.
334 struct iio_dev *iio_allocate_device(int sizeof_priv);
336 static inline void *iio_priv(const struct iio_dev *dev)
338 return (char *)dev + ALIGN(sizeof(struct iio_dev), IIO_ALIGN);
341 static inline struct iio_dev *iio_priv_to_dev(void *priv)
343 return (struct iio_dev *)((char *)priv -
344 ALIGN(sizeof(struct iio_dev), IIO_ALIGN));
348 * iio_free_device() - free an iio_dev from a driver
349 * @dev: the iio_dev associated with the device
351 void iio_free_device(struct iio_dev *dev);
354 * iio_ring_enabled() - helper function to test if any form of ring is enabled
355 * @dev_info: IIO device info structure for device
357 static inline bool iio_ring_enabled(struct iio_dev *dev_info)
359 return dev_info->currentmode
360 & (INDIO_RING_TRIGGERED
361 | INDIO_RING_HARDWARE_BUFFER);
364 #endif /* _INDUSTRIAL_IO_H_ */