Merge branch 'kconfig' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild
[deliverable/linux.git] / drivers / staging / iio / iio.h
CommitLineData
7d438178 1
847ec80b
JC
2/* The industrial I/O core
3 *
4 * Copyright (c) 2008 Jonathan Cameron
5 *
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 */
847ec80b
JC
10#ifndef _INDUSTRIAL_IO_H_
11#define _INDUSTRIAL_IO_H_
12
13#include <linux/device.h>
14#include <linux/cdev.h>
af5046af 15#include "types.h"
847ec80b 16/* IIO TODO LIST */
751a3700 17/*
847ec80b
JC
18 * Provide means of adjusting timer accuracy.
19 * Currently assumes nano seconds.
20 */
21
1e8dfcc6
JC
22enum iio_data_type {
23 IIO_RAW,
24 IIO_PROCESSED,
25};
26
1d892719
JC
27/* Could add the raw attributes as well - allowing buffer only devices */
28enum iio_chan_info_enum {
924f8a21 29 /* 0 is reserverd for raw attributes */
c8a9f805
JC
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,
df94aba8 38 IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY,
1d892719
JC
39};
40
c8a9f805
JC
41#define IIO_CHAN_INFO_SHARED_BIT(type) BIT(type*2)
42#define IIO_CHAN_INFO_SEPARATE_BIT(type) BIT(type*2 + 1)
43
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)
df94aba8
JC
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)
c8a9f805 84
8310b86c
JC
85enum iio_endian {
86 IIO_CPU,
87 IIO_BE,
88 IIO_LE,
89};
90
1d892719
JC
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.
8310b86c 106 * endianness: little or big endian
1d892719
JC
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.
6c63dded
JC
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).
1d892719
JC
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.
ade7ef7b 127 * @differential: Channel is differential.
1d892719
JC
128 */
129struct 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;
8310b86c 140 enum iio_endian endianness;
1d892719 141 } scan_type;
1c5e6a3f
MH
142 long info_mask;
143 long event_mask;
144 char *extend_name;
6c63dded 145 const char *datasheet_name;
1d892719
JC
146 unsigned processed_val:1;
147 unsigned modified:1;
148 unsigned indexed:1;
c6fc8062 149 unsigned output:1;
ade7ef7b 150 unsigned differential:1;
1d892719 151};
df9c1c42 152
1d892719
JC
153#define IIO_ST(si, rb, sb, sh) \
154 { .sign = si, .realbits = rb, .storagebits = sb, .shift = sh }
155
c6fc8062
JC
156/* Macro assumes input channels */
157#define IIO_CHAN(_type, _mod, _indexed, _proc, _name, _chan, _chan2, \
aaf370db 158 _inf_mask, _address, _si, _stype, _event_mask) \
1d892719 159 { .type = _type, \
c6fc8062 160 .output = 0, \
1d892719
JC
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, \
aaf370db 171 .event_mask = _event_mask }
1d892719
JC
172
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) }
176
847ec80b
JC
177/**
178 * iio_get_time_ns() - utility function to get a time stamp for events etc
179 **/
180static inline s64 iio_get_time_ns(void)
181{
182 struct timespec ts;
183 /*
184 * calls getnstimeofday.
185 * If hrtimers then up to ns accurate, if not microsecond.
186 */
187 ktime_get_real_ts(&ts);
188
189 return timespec_to_ns(&ts);
190}
191
847ec80b
JC
192/* Device operating modes */
193#define INDIO_DIRECT_MODE 0x01
ec3afa40
JC
194#define INDIO_BUFFER_TRIGGERED 0x02
195#define INDIO_BUFFER_HARDWARE 0x08
847ec80b 196
ec3afa40
JC
197#define INDIO_ALL_BUFFER_MODES \
198 (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE)
847ec80b
JC
199
200/* Vast majority of this is set by the industrialio subsystem on a
201 * call to iio_device_register. */
1d892719
JC
202#define IIO_VAL_INT 1
203#define IIO_VAL_INT_PLUS_MICRO 2
71646e2c 204#define IIO_VAL_INT_PLUS_NANO 3
6fe8135f 205
43a4360e 206struct iio_trigger; /* forward declaration */
7ae8cf62 207struct iio_dev;
43a4360e 208
6fe8135f
JC
209/**
210 * struct iio_info - constant information about device
211 * @driver_module: module structure used to ensure correct
212 * ownership of chrdevs etc
6fe8135f
JC
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.
5c04af04
MH
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.
6fe8135f
JC
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.
43a4360e
MH
231 * @validate_trigger: function to validate the trigger when the
232 * current trigger gets changed.
6fe8135f
JC
233 **/
234struct iio_info {
235 struct module *driver_module;
6fe8135f
JC
236 struct attribute_group *event_attrs;
237 const struct attribute_group *attrs;
238
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);
244
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);
250
5c04af04
MH
251 int (*write_raw_get_fmt)(struct iio_dev *indio_dev,
252 struct iio_chan_spec const *chan,
253 long mask);
254
6fe8135f 255 int (*read_event_config)(struct iio_dev *indio_dev,
330c6c57 256 u64 event_code);
6fe8135f
JC
257
258 int (*write_event_config)(struct iio_dev *indio_dev,
330c6c57 259 u64 event_code,
6fe8135f
JC
260 int state);
261
262 int (*read_event_value)(struct iio_dev *indio_dev,
330c6c57 263 u64 event_code,
6fe8135f
JC
264 int *val);
265 int (*write_event_value)(struct iio_dev *indio_dev,
330c6c57 266 u64 event_code,
6fe8135f 267 int val);
43a4360e
MH
268 int (*validate_trigger)(struct iio_dev *indio_dev,
269 struct iio_trigger *trig);
4d5f8d3d
JC
270 int (*update_scan_mode)(struct iio_dev *indio_dev,
271 const unsigned long *scan_mask);
6fe8135f
JC
272};
273
1612244f
JC
274/**
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
281 */
282struct 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 *);
287};
288
847ec80b
JC
289/**
290 * struct iio_dev - industrial I/O device
291 * @id: [INTERN] used to identify device internally
847ec80b
JC
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
5aa96188 296 * @event_interface: [INTERN] event chrdevs associated with interrupt lines
14555b14 297 * @buffer: [DRIVER] any buffer present
847ec80b
JC
298 * @mlock: [INTERN] lock used to prevent simultaneous device state
299 * changes
e5c003ae 300 * @available_scan_masks: [DRIVER] optional array of allowed bitmasks
32b5eeca
JC
301 * @masklength: [INTERN] the length of the mask established from
302 * channels
959d2952 303 * @active_scan_mask: [INTERN] union of all scan masks requested by buffers
14555b14 304 * @trig: [INTERN] current device trigger (buffer modes)
25985edc 305 * @pollfunc: [DRIVER] function run on trigger being received
1d892719
JC
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
1a25e592 309 * attributes
26d25ae3 310 * @chan_attr_group: [INTERN] group for all attrs in base directory
1d892719 311 * @name: [DRIVER] name of the device.
1a25e592
JC
312 * @info: [DRIVER] callbacks and constant info from driver
313 * @chrdev: [INTERN] associated character device
26d25ae3
JC
314 * @groups: [INTERN] attribute groups
315 * @groupcounter: [INTERN] index of next attribute group
bb01443e 316 * @flags: [INTERN] file ops related flags including busy flag.
847ec80b
JC
317 **/
318struct iio_dev {
319 int id;
4024bc73 320
847ec80b
JC
321 int modes;
322 int currentmode;
323 struct device dev;
847ec80b 324
5aa96188 325 struct iio_event_interface *event_interface;
847ec80b 326
14555b14 327 struct iio_buffer *buffer;
847ec80b
JC
328 struct mutex mlock;
329
32b5eeca
JC
330 unsigned long *available_scan_masks;
331 unsigned masklength;
959d2952 332 unsigned long *active_scan_mask;
847ec80b
JC
333 struct iio_trigger *trig;
334 struct iio_poll_func *pollfunc;
1d892719 335
1a25e592
JC
336 struct iio_chan_spec const *channels;
337 int num_channels;
1d892719 338
1a25e592 339 struct list_head channel_attr_list;
26d25ae3 340 struct attribute_group chan_attr_group;
1a25e592
JC
341 const char *name;
342 const struct iio_info *info;
1612244f 343 const struct iio_buffer_setup_ops *setup_ops;
1a25e592 344 struct cdev chrdev;
26d25ae3
JC
345#define IIO_MAX_GROUPS 6
346 const struct attribute_group *groups[IIO_MAX_GROUPS + 1];
347 int groupcounter;
bb01443e
LPC
348
349 unsigned long flags;
847ec80b
JC
350};
351
5fb21c82
JC
352/**
353 * iio_find_channel_from_si() - get channel from its scan index
354 * @indio_dev: device
355 * @si: scan index to match
356 */
357const struct iio_chan_spec
358*iio_find_channel_from_si(struct iio_dev *indio_dev, int si);
359
847ec80b
JC
360/**
361 * iio_device_register() - register a device with the IIO subsystem
f8c6f4e9 362 * @indio_dev: Device structure filled by the device driver
847ec80b 363 **/
f8c6f4e9 364int iio_device_register(struct iio_dev *indio_dev);
847ec80b
JC
365
366/**
367 * iio_device_unregister() - unregister a device from the IIO subsystem
f8c6f4e9 368 * @indio_dev: Device structure representing the device.
847ec80b 369 **/
f8c6f4e9 370void iio_device_unregister(struct iio_dev *indio_dev);
847ec80b 371
847ec80b
JC
372/**
373 * iio_push_event() - try to add event to the list for userspace reading
f8c6f4e9 374 * @indio_dev: IIO device structure
847ec80b 375 * @ev_code: What event
4c572605 376 * @timestamp: When the event occurred
847ec80b 377 **/
f8c6f4e9 378int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp);
847ec80b 379
5aaaeba8 380extern struct bus_type iio_bus_type;
847ec80b
JC
381
382/**
4c572605 383 * iio_put_device() - reference counted deallocation of struct device
847ec80b
JC
384 * @dev: the iio_device containing the device
385 **/
f8c6f4e9 386static inline void iio_put_device(struct iio_dev *indio_dev)
847ec80b 387{
f8c6f4e9
JC
388 if (indio_dev)
389 put_device(&indio_dev->dev);
847ec80b
JC
390};
391
6f7c8ee5
JC
392/* Can we make this smaller? */
393#define IIO_ALIGN L1_CACHE_BYTES
847ec80b
JC
394/**
395 * iio_allocate_device() - allocate an iio_dev from a driver
6f7c8ee5 396 * @sizeof_priv: Space to allocate for private structure.
847ec80b 397 **/
6f7c8ee5
JC
398struct iio_dev *iio_allocate_device(int sizeof_priv);
399
f8c6f4e9 400static inline void *iio_priv(const struct iio_dev *indio_dev)
6f7c8ee5 401{
f8c6f4e9 402 return (char *)indio_dev + ALIGN(sizeof(struct iio_dev), IIO_ALIGN);
6f7c8ee5
JC
403}
404
405static inline struct iio_dev *iio_priv_to_dev(void *priv)
406{
407 return (struct iio_dev *)((char *)priv -
408 ALIGN(sizeof(struct iio_dev), IIO_ALIGN));
409}
847ec80b
JC
410
411/**
412 * iio_free_device() - free an iio_dev from a driver
4c572605 413 * @dev: the iio_dev associated with the device
847ec80b 414 **/
f8c6f4e9 415void iio_free_device(struct iio_dev *indio_dev);
847ec80b 416
847ec80b 417/**
14555b14 418 * iio_buffer_enabled() - helper function to test if the buffer is enabled
f8c6f4e9 419 * @indio_dev: IIO device info structure for device
847ec80b 420 **/
f8c6f4e9 421static inline bool iio_buffer_enabled(struct iio_dev *indio_dev)
847ec80b 422{
f8c6f4e9 423 return indio_dev->currentmode
ec3afa40 424 & (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE);
847ec80b
JC
425};
426
847ec80b 427#endif /* _INDUSTRIAL_IO_H_ */
This page took 0.309981 seconds and 5 git commands to generate.