Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / staging / iio / iio.h
blobbe6ced31f65e6dd057c47419799432619d69aceb
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 */
10 #ifndef _INDUSTRIAL_IO_H_
11 #define _INDUSTRIAL_IO_H_
13 #include <linux/device.h>
14 #include <linux/cdev.h>
15 #include "types.h"
16 /* IIO TODO LIST */
18 * Provide means of adjusting timer accuracy.
19 * Currently assumes nano seconds.
22 enum iio_data_type {
23 IIO_RAW,
24 IIO_PROCESSED,
27 /* Could add the raw attributes as well - allowing buffer only devices */
28 enum iio_chan_info_enum {
29 /* 0 is reserverd for raw attributes */
30 IIO_CHAN_INFO_SCALE = 1,
31 IIO_CHAN_INFO_OFFSET,
32 IIO_CHAN_INFO_CALIBSCALE,
33 IIO_CHAN_INFO_CALIBBIAS,
34 IIO_CHAN_INFO_PEAK,
35 IIO_CHAN_INFO_PEAK_SCALE,
36 IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW,
37 IIO_CHAN_INFO_AVERAGE_RAW,
38 IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY,
41 #define IIO_CHAN_INFO_SHARED_BIT(type) BIT(type*2)
42 #define IIO_CHAN_INFO_SEPARATE_BIT(type) BIT(type*2 + 1)
44 #define IIO_CHAN_INFO_SCALE_SEPARATE_BIT \
45 IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_SCALE)
46 #define IIO_CHAN_INFO_SCALE_SHARED_BIT \
47 IIO_CHAN_INFO_SHARED_BIT(IIO_CHAN_INFO_SCALE)
48 #define IIO_CHAN_INFO_OFFSET_SEPARATE_BIT \
49 IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_OFFSET)
50 #define IIO_CHAN_INFO_OFFSET_SHARED_BIT \
51 IIO_CHAN_INFO_SHARED_BIT(IIO_CHAN_INFO_OFFSET)
52 #define IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT \
53 IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_CALIBSCALE)
54 #define IIO_CHAN_INFO_CALIBSCALE_SHARED_BIT \
55 IIO_CHAN_INFO_SHARED_BIT(IIO_CHAN_INFO_CALIBSCALE)
56 #define IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT \
57 IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_CALIBBIAS)
58 #define IIO_CHAN_INFO_CALIBBIAS_SHARED_BIT \
59 IIO_CHAN_INFO_SHARED_BIT(IIO_CHAN_INFO_CALIBBIAS)
60 #define IIO_CHAN_INFO_PEAK_SEPARATE_BIT \
61 IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_PEAK)
62 #define IIO_CHAN_INFO_PEAK_SHARED_BIT \
63 IIO_CHAN_INFO_SHARED_BIT(IIO_CHAN_INFO_PEAK)
64 #define IIO_CHAN_INFO_PEAKSCALE_SEPARATE_BIT \
65 IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_PEAKSCALE)
66 #define IIO_CHAN_INFO_PEAKSCALE_SHARED_BIT \
67 IIO_CHAN_INFO_SHARED_BIT(IIO_CHAN_INFO_PEAKSCALE)
68 #define IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW_SEPARATE_BIT \
69 IIO_CHAN_INFO_SEPARATE_BIT( \
70 IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW)
71 #define IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW_SHARED_BIT \
72 IIO_CHAN_INFO_SHARED_BIT( \
73 IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW)
74 #define IIO_CHAN_INFO_AVERAGE_RAW_SEPARATE_BIT \
75 IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_AVERAGE_RAW)
76 #define IIO_CHAN_INFO_AVERAGE_RAW_SHARED_BIT \
77 IIO_CHAN_INFO_SHARED_BIT(IIO_CHAN_INFO_AVERAGE_RAW)
78 #define IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY_SHARED_BIT \
79 IIO_CHAN_INFO_SHARED_BIT( \
80 IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY)
81 #define IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY_SEPARATE_BIT \
82 IIO_CHAN_INFO_SEPARATE_BIT( \
83 IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY)
85 enum iio_endian {
86 IIO_CPU,
87 IIO_BE,
88 IIO_LE,
91 /**
92 * struct iio_chan_spec - specification of a single channel
93 * @type: What type of measurement is the channel making.
94 * @channel: What number or name do we wish to asign the channel.
95 * @channel2: If there is a second number for a differential
96 * channel then this is it. If modified is set then the
97 * value here specifies the modifier.
98 * @address: Driver specific identifier.
99 * @scan_index: Monotonic index to give ordering in scans when read
100 * from a buffer.
101 * @scan_type: Sign: 's' or 'u' to specify signed or unsigned
102 * realbits: Number of valid bits of data
103 * storage_bits: Realbits + padding
104 * shift: Shift right by this before masking out
105 * realbits.
106 * endianness: little or big endian
107 * @info_mask: What information is to be exported about this channel.
108 * This includes calibbias, scale etc.
109 * @event_mask: What events can this channel produce.
110 * @extend_name: Allows labeling of channel attributes with an
111 * informative name. Note this has no effect codes etc,
112 * unlike modifiers.
113 * @datasheet_name: A name used in in kernel mapping of channels. It should
114 * corrspond to the first name that the channel is referred
115 * to by in the datasheet (e.g. IND), or the nearest
116 * possible compound name (e.g. IND-INC).
117 * @processed_val: Flag to specify the data access attribute should be
118 * *_input rather than *_raw.
119 * @modified: Does a modifier apply to this channel. What these are
120 * depends on the channel type. Modifier is set in
121 * channel2. Examples are IIO_MOD_X for axial sensors about
122 * the 'x' axis.
123 * @indexed: Specify the channel has a numerical index. If not,
124 * the value in channel will be suppressed for attribute
125 * but not for event codes. Typically set it to 0 when
126 * the index is false.
127 * @differential: Channel is differential.
129 struct iio_chan_spec {
130 enum iio_chan_type type;
131 int channel;
132 int channel2;
133 unsigned long address;
134 int scan_index;
135 struct {
136 char sign;
137 u8 realbits;
138 u8 storagebits;
139 u8 shift;
140 enum iio_endian endianness;
141 } scan_type;
142 long info_mask;
143 long event_mask;
144 char *extend_name;
145 const char *datasheet_name;
146 unsigned processed_val:1;
147 unsigned modified:1;
148 unsigned indexed:1;
149 unsigned output:1;
150 unsigned differential:1;
153 #define IIO_ST(si, rb, sb, sh) \
154 { .sign = si, .realbits = rb, .storagebits = sb, .shift = sh }
156 /* Macro assumes input channels */
157 #define IIO_CHAN(_type, _mod, _indexed, _proc, _name, _chan, _chan2, \
158 _inf_mask, _address, _si, _stype, _event_mask) \
159 { .type = _type, \
160 .output = 0, \
161 .modified = _mod, \
162 .indexed = _indexed, \
163 .processed_val = _proc, \
164 .extend_name = _name, \
165 .channel = _chan, \
166 .channel2 = _chan2, \
167 .info_mask = _inf_mask, \
168 .address = _address, \
169 .scan_index = _si, \
170 .scan_type = _stype, \
171 .event_mask = _event_mask }
173 #define IIO_CHAN_SOFT_TIMESTAMP(_si) \
174 { .type = IIO_TIMESTAMP, .channel = -1, \
175 .scan_index = _si, .scan_type = IIO_ST('s', 64, 64, 0) }
178 * iio_get_time_ns() - utility function to get a time stamp for events etc
180 static inline s64 iio_get_time_ns(void)
182 struct timespec ts;
184 * calls getnstimeofday.
185 * If hrtimers then up to ns accurate, if not microsecond.
187 ktime_get_real_ts(&ts);
189 return timespec_to_ns(&ts);
192 /* Device operating modes */
193 #define INDIO_DIRECT_MODE 0x01
194 #define INDIO_BUFFER_TRIGGERED 0x02
195 #define INDIO_BUFFER_HARDWARE 0x08
197 #define INDIO_ALL_BUFFER_MODES \
198 (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE)
200 /* Vast majority of this is set by the industrialio subsystem on a
201 * call to iio_device_register. */
202 #define IIO_VAL_INT 1
203 #define IIO_VAL_INT_PLUS_MICRO 2
204 #define IIO_VAL_INT_PLUS_NANO 3
206 struct iio_trigger; /* forward declaration */
207 struct iio_dev;
210 * struct iio_info - constant information about device
211 * @driver_module: module structure used to ensure correct
212 * ownership of chrdevs etc
213 * @event_attrs: event control attributes
214 * @attrs: general purpose device attributes
215 * @read_raw: function to request a value from the device.
216 * mask specifies which value. Note 0 means a reading of
217 * the channel in question. Return value will specify the
218 * type of value returned by the device. val and val2 will
219 * contain the elements making up the returned value.
220 * @write_raw: function to write a value to the device.
221 * Parameters are the same as for read_raw.
222 * @write_raw_get_fmt: callback function to query the expected
223 * format/precision. If not set by the driver, write_raw
224 * returns IIO_VAL_INT_PLUS_MICRO.
225 * @read_event_config: find out if the event is enabled.
226 * @write_event_config: set if the event is enabled.
227 * @read_event_value: read a value associated with the event. Meaning
228 * is event dependant. event_code specifies which event.
229 * @write_event_value: write the value associate with the event.
230 * Meaning is event dependent.
231 * @validate_trigger: function to validate the trigger when the
232 * current trigger gets changed.
234 struct iio_info {
235 struct module *driver_module;
236 struct attribute_group *event_attrs;
237 const struct attribute_group *attrs;
239 int (*read_raw)(struct iio_dev *indio_dev,
240 struct iio_chan_spec const *chan,
241 int *val,
242 int *val2,
243 long mask);
245 int (*write_raw)(struct iio_dev *indio_dev,
246 struct iio_chan_spec const *chan,
247 int val,
248 int val2,
249 long mask);
251 int (*write_raw_get_fmt)(struct iio_dev *indio_dev,
252 struct iio_chan_spec const *chan,
253 long mask);
255 int (*read_event_config)(struct iio_dev *indio_dev,
256 u64 event_code);
258 int (*write_event_config)(struct iio_dev *indio_dev,
259 u64 event_code,
260 int state);
262 int (*read_event_value)(struct iio_dev *indio_dev,
263 u64 event_code,
264 int *val);
265 int (*write_event_value)(struct iio_dev *indio_dev,
266 u64 event_code,
267 int val);
268 int (*validate_trigger)(struct iio_dev *indio_dev,
269 struct iio_trigger *trig);
270 int (*update_scan_mode)(struct iio_dev *indio_dev,
271 const unsigned long *scan_mask);
275 * struct iio_buffer_setup_ops - buffer setup related callbacks
276 * @preenable: [DRIVER] function to run prior to marking buffer enabled
277 * @postenable: [DRIVER] function to run after marking buffer enabled
278 * @predisable: [DRIVER] function to run prior to marking buffer
279 * disabled
280 * @postdisable: [DRIVER] function to run after marking buffer disabled
282 struct iio_buffer_setup_ops {
283 int (*preenable)(struct iio_dev *);
284 int (*postenable)(struct iio_dev *);
285 int (*predisable)(struct iio_dev *);
286 int (*postdisable)(struct iio_dev *);
290 * struct iio_dev - industrial I/O device
291 * @id: [INTERN] used to identify device internally
292 * @modes: [DRIVER] operating modes supported by device
293 * @currentmode: [DRIVER] current operating mode
294 * @dev: [DRIVER] device structure, should be assigned a parent
295 * and owner
296 * @event_interface: [INTERN] event chrdevs associated with interrupt lines
297 * @buffer: [DRIVER] any buffer present
298 * @mlock: [INTERN] lock used to prevent simultaneous device state
299 * changes
300 * @available_scan_masks: [DRIVER] optional array of allowed bitmasks
301 * @masklength: [INTERN] the length of the mask established from
302 * channels
303 * @active_scan_mask: [INTERN] union of all scan masks requested by buffers
304 * @trig: [INTERN] current device trigger (buffer modes)
305 * @pollfunc: [DRIVER] function run on trigger being received
306 * @channels: [DRIVER] channel specification structure table
307 * @num_channels: [DRIVER] number of chanels specified in @channels.
308 * @channel_attr_list: [INTERN] keep track of automatically created channel
309 * attributes
310 * @chan_attr_group: [INTERN] group for all attrs in base directory
311 * @name: [DRIVER] name of the device.
312 * @info: [DRIVER] callbacks and constant info from driver
313 * @chrdev: [INTERN] associated character device
314 * @groups: [INTERN] attribute groups
315 * @groupcounter: [INTERN] index of next attribute group
316 * @flags: [INTERN] file ops related flags including busy flag.
318 struct iio_dev {
319 int id;
321 int modes;
322 int currentmode;
323 struct device dev;
325 struct iio_event_interface *event_interface;
327 struct iio_buffer *buffer;
328 struct mutex mlock;
330 unsigned long *available_scan_masks;
331 unsigned masklength;
332 unsigned long *active_scan_mask;
333 struct iio_trigger *trig;
334 struct iio_poll_func *pollfunc;
336 struct iio_chan_spec const *channels;
337 int num_channels;
339 struct list_head channel_attr_list;
340 struct attribute_group chan_attr_group;
341 const char *name;
342 const struct iio_info *info;
343 const struct iio_buffer_setup_ops *setup_ops;
344 struct cdev chrdev;
345 #define IIO_MAX_GROUPS 6
346 const struct attribute_group *groups[IIO_MAX_GROUPS + 1];
347 int groupcounter;
349 unsigned long flags;
353 * iio_find_channel_from_si() - get channel from its scan index
354 * @indio_dev: device
355 * @si: scan index to match
357 const struct iio_chan_spec
358 *iio_find_channel_from_si(struct iio_dev *indio_dev, int si);
361 * iio_device_register() - register a device with the IIO subsystem
362 * @indio_dev: Device structure filled by the device driver
364 int iio_device_register(struct iio_dev *indio_dev);
367 * iio_device_unregister() - unregister a device from the IIO subsystem
368 * @indio_dev: Device structure representing the device.
370 void iio_device_unregister(struct iio_dev *indio_dev);
373 * iio_push_event() - try to add event to the list for userspace reading
374 * @indio_dev: IIO device structure
375 * @ev_code: What event
376 * @timestamp: When the event occurred
378 int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp);
380 extern struct bus_type iio_bus_type;
383 * iio_put_device() - reference counted deallocation of struct device
384 * @dev: the iio_device containing the device
386 static inline void iio_put_device(struct iio_dev *indio_dev)
388 if (indio_dev)
389 put_device(&indio_dev->dev);
392 /* Can we make this smaller? */
393 #define IIO_ALIGN L1_CACHE_BYTES
395 * iio_allocate_device() - allocate an iio_dev from a driver
396 * @sizeof_priv: Space to allocate for private structure.
398 struct iio_dev *iio_allocate_device(int sizeof_priv);
400 static inline void *iio_priv(const struct iio_dev *indio_dev)
402 return (char *)indio_dev + ALIGN(sizeof(struct iio_dev), IIO_ALIGN);
405 static inline struct iio_dev *iio_priv_to_dev(void *priv)
407 return (struct iio_dev *)((char *)priv -
408 ALIGN(sizeof(struct iio_dev), IIO_ALIGN));
412 * iio_free_device() - free an iio_dev from a driver
413 * @dev: the iio_dev associated with the device
415 void iio_free_device(struct iio_dev *indio_dev);
418 * iio_buffer_enabled() - helper function to test if the buffer is enabled
419 * @indio_dev: IIO device info structure for device
421 static inline bool iio_buffer_enabled(struct iio_dev *indio_dev)
423 return indio_dev->currentmode
424 & (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE);
427 #endif /* _INDUSTRIAL_IO_H_ */