staging:iio:adc:ad7606 remove unused chip info elements.
[pandora-kernel.git] / drivers / staging / iio / iio.h
1
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  */
10
11 #ifndef _INDUSTRIAL_IO_H_
12 #define _INDUSTRIAL_IO_H_
13
14 #include <linux/device.h>
15 #include <linux/cdev.h>
16
17 /* IIO TODO LIST */
18 /*
19  * Provide means of adjusting timer accuracy.
20  * Currently assumes nano seconds.
21  */
22
23 enum iio_data_type {
24         IIO_RAW,
25         IIO_PROCESSED,
26 };
27
28 enum iio_chan_type {
29         /* real channel types */
30         IIO_VOLTAGE,
31         IIO_CURRENT,
32         IIO_POWER,
33         IIO_ACCEL,
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,
44         IIO_CAPACITANCE,
45 };
46
47 enum iio_modifier {
48         IIO_NO_MOD,
49         IIO_MOD_X,
50         IIO_MOD_Y,
51         IIO_MOD_Z,
52         IIO_MOD_X_AND_Y,
53         IIO_MOD_X_ANX_Z,
54         IIO_MOD_Y_AND_Z,
55         IIO_MOD_X_AND_Y_AND_Z,
56         IIO_MOD_X_OR_Y,
57         IIO_MOD_X_OR_Z,
58         IIO_MOD_Y_OR_Z,
59         IIO_MOD_X_OR_Y_OR_Z,
60         IIO_MOD_LIGHT_BOTH,
61         IIO_MOD_LIGHT_IR,
62 };
63
64 /* Could add the raw attributes as well - allowing buffer only devices */
65 enum iio_chan_info_enum {
66         IIO_CHAN_INFO_SCALE_SHARED,
67         IIO_CHAN_INFO_SCALE_SEPARATE,
68         IIO_CHAN_INFO_OFFSET_SHARED,
69         IIO_CHAN_INFO_OFFSET_SEPARATE,
70         IIO_CHAN_INFO_CALIBSCALE_SHARED,
71         IIO_CHAN_INFO_CALIBSCALE_SEPARATE,
72         IIO_CHAN_INFO_CALIBBIAS_SHARED,
73         IIO_CHAN_INFO_CALIBBIAS_SEPARATE,
74         IIO_CHAN_INFO_PEAK_SHARED,
75         IIO_CHAN_INFO_PEAK_SEPARATE,
76         IIO_CHAN_INFO_PEAK_SCALE_SHARED,
77         IIO_CHAN_INFO_PEAK_SCALE_SEPARATE,
78         IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW_SHARED,
79         IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW_SEPARATE,
80         IIO_CHAN_INFO_AVERAGE_RAW_SHARED,
81         IIO_CHAN_INFO_AVERAGE_RAW_SEPARATE,
82 };
83
84 enum iio_endian {
85         IIO_CPU,
86         IIO_BE,
87         IIO_LE,
88 };
89
90 /**
91  * struct iio_chan_spec - specification of a single channel
92  * @type:               What type of measurement is the channel making.
93  * @channel:            What number or name do we wish to asign the channel.
94  * @channel2:           If there is a second number for a differential
95  *                      channel then this is it. If modified is set then the
96  *                      value here specifies the modifier.
97  * @address:            Driver specific identifier.
98  * @scan_index: Monotonic index to give ordering in scans when read
99  *                      from a buffer.
100  * @scan_type:          Sign:           's' or 'u' to specify signed or unsigned
101  *                      realbits:       Number of valid bits of data
102  *                      storage_bits:   Realbits + padding
103  *                      shift:          Shift right by this before masking out
104  *                                      realbits.
105  *                      endianness:     little or big endian
106  * @info_mask:          What information is to be exported about this channel.
107  *                      This includes calibbias, scale etc.
108  * @event_mask: What events can this channel produce.
109  * @extend_name:        Allows labeling of channel attributes with an
110  *                      informative name. Note this has no effect codes etc,
111  *                      unlike modifiers.
112  * @processed_val:      Flag to specify the data access attribute should be
113  *                      *_input rather than *_raw.
114  * @modified:           Does a modifier apply to this channel. What these are
115  *                      depends on the channel type.  Modifier is set in
116  *                      channel2. Examples are IIO_MOD_X for axial sensors about
117  *                      the 'x' axis.
118  * @indexed:            Specify the channel has a numerical index. If not,
119  *                      the value in channel will be suppressed for attribute
120  *                      but not for event codes. Typically set it to 0 when
121  *                      the index is false.
122  * @differential:       Channel is differential.
123  */
124 struct iio_chan_spec {
125         enum iio_chan_type      type;
126         int                     channel;
127         int                     channel2;
128         unsigned long           address;
129         int                     scan_index;
130         struct {
131                 char    sign;
132                 u8      realbits;
133                 u8      storagebits;
134                 u8      shift;
135                 enum iio_endian endianness;
136         } scan_type;
137         long                    info_mask;
138         long                    event_mask;
139         char                    *extend_name;
140         unsigned                processed_val:1;
141         unsigned                modified:1;
142         unsigned                indexed:1;
143         unsigned                output:1;
144         unsigned                differential:1;
145 };
146
147 #define IIO_ST(si, rb, sb, sh)                                          \
148         { .sign = si, .realbits = rb, .storagebits = sb, .shift = sh }
149
150 /* Macro assumes input channels */
151 #define IIO_CHAN(_type, _mod, _indexed, _proc, _name, _chan, _chan2, \
152                  _inf_mask, _address, _si, _stype, _event_mask)         \
153         { .type = _type,                                                \
154           .output = 0,                                                  \
155           .modified = _mod,                                             \
156           .indexed = _indexed,                                          \
157           .processed_val = _proc,                                       \
158           .extend_name = _name,                                         \
159           .channel = _chan,                                             \
160           .channel2 = _chan2,                                           \
161           .info_mask = _inf_mask,                                       \
162           .address = _address,                                          \
163           .scan_index = _si,                                            \
164           .scan_type = _stype,                                          \
165           .event_mask = _event_mask }
166
167 #define IIO_CHAN_SOFT_TIMESTAMP(_si)                                    \
168         { .type = IIO_TIMESTAMP, .channel = -1,                         \
169                         .scan_index = _si, .scan_type = IIO_ST('s', 64, 64, 0) }
170
171 /**
172  * iio_get_time_ns() - utility function to get a time stamp for events etc
173  **/
174 static inline s64 iio_get_time_ns(void)
175 {
176         struct timespec ts;
177         /*
178          * calls getnstimeofday.
179          * If hrtimers then up to ns accurate, if not microsecond.
180          */
181         ktime_get_real_ts(&ts);
182
183         return timespec_to_ns(&ts);
184 }
185
186 /* Device operating modes */
187 #define INDIO_DIRECT_MODE               0x01
188 #define INDIO_BUFFER_TRIGGERED          0x02
189 #define INDIO_BUFFER_HARDWARE           0x08
190
191 #define INDIO_ALL_BUFFER_MODES                                  \
192         (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE)
193
194 /* Vast majority of this is set by the industrialio subsystem on a
195  * call to iio_device_register. */
196 #define IIO_VAL_INT 1
197 #define IIO_VAL_INT_PLUS_MICRO 2
198 #define IIO_VAL_INT_PLUS_NANO 3
199
200 struct iio_trigger; /* forward declaration */
201 struct iio_dev;
202
203 /**
204  * struct iio_info - constant information about device
205  * @driver_module:      module structure used to ensure correct
206  *                      ownership of chrdevs etc
207  * @event_attrs:        event control attributes
208  * @attrs:              general purpose device attributes
209  * @read_raw:           function to request a value from the device.
210  *                      mask specifies which value. Note 0 means a reading of
211  *                      the channel in question.  Return value will specify the
212  *                      type of value returned by the device. val and val2 will
213  *                      contain the elements making up the returned value.
214  * @write_raw:          function to write a value to the device.
215  *                      Parameters are the same as for read_raw.
216  * @write_raw_get_fmt:  callback function to query the expected
217  *                      format/precision. If not set by the driver, write_raw
218  *                      returns IIO_VAL_INT_PLUS_MICRO.
219  * @read_event_config:  find out if the event is enabled.
220  * @write_event_config: set if the event is enabled.
221  * @read_event_value:   read a value associated with the event. Meaning
222  *                      is event dependant. event_code specifies which event.
223  * @write_event_value:  write the value associate with the event.
224  *                      Meaning is event dependent.
225  * @validate_trigger:   function to validate the trigger when the
226  *                      current trigger gets changed.
227  **/
228 struct iio_info {
229         struct module                   *driver_module;
230         struct attribute_group          *event_attrs;
231         const struct attribute_group    *attrs;
232
233         int (*read_raw)(struct iio_dev *indio_dev,
234                         struct iio_chan_spec const *chan,
235                         int *val,
236                         int *val2,
237                         long mask);
238
239         int (*write_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_get_fmt)(struct iio_dev *indio_dev,
246                          struct iio_chan_spec const *chan,
247                          long mask);
248
249         int (*read_event_config)(struct iio_dev *indio_dev,
250                                  u64 event_code);
251
252         int (*write_event_config)(struct iio_dev *indio_dev,
253                                   u64 event_code,
254                                   int state);
255
256         int (*read_event_value)(struct iio_dev *indio_dev,
257                                 u64 event_code,
258                                 int *val);
259         int (*write_event_value)(struct iio_dev *indio_dev,
260                                  u64 event_code,
261                                  int val);
262         int (*validate_trigger)(struct iio_dev *indio_dev,
263                                 struct iio_trigger *trig);
264
265 };
266
267 /**
268  * struct iio_dev - industrial I/O device
269  * @id:                 [INTERN] used to identify device internally
270  * @modes:              [DRIVER] operating modes supported by device
271  * @currentmode:        [DRIVER] current operating mode
272  * @dev:                [DRIVER] device structure, should be assigned a parent
273  *                      and owner
274  * @event_interface:    [INTERN] event chrdevs associated with interrupt lines
275  * @buffer:             [DRIVER] any buffer present
276  * @mlock:              [INTERN] lock used to prevent simultaneous device state
277  *                      changes
278  * @available_scan_masks: [DRIVER] optional array of allowed bitmasks
279  * @masklength:         [INTERN] the length of the mask established from
280  *                      channels
281  * @trig:               [INTERN] current device trigger (buffer modes)
282  * @pollfunc:           [DRIVER] function run on trigger being received
283  * @channels:           [DRIVER] channel specification structure table
284  * @num_channels:       [DRIVER] number of chanels specified in @channels.
285  * @channel_attr_list:  [INTERN] keep track of automatically created channel
286  *                      attributes
287  * @chan_attr_group:    [INTERN] group for all attrs in base directory
288  * @name:               [DRIVER] name of the device.
289  * @info:               [DRIVER] callbacks and constant info from driver
290  * @chrdev:             [INTERN] associated character device
291  * @groups:             [INTERN] attribute groups
292  * @groupcounter:       [INTERN] index of next attribute group
293  **/
294 struct iio_dev {
295         int                             id;
296
297         int                             modes;
298         int                             currentmode;
299         struct device                   dev;
300
301         struct iio_event_interface      *event_interface;
302
303         struct iio_buffer               *buffer;
304         struct mutex                    mlock;
305
306         unsigned long                   *available_scan_masks;
307         unsigned                        masklength;
308         struct iio_trigger              *trig;
309         struct iio_poll_func            *pollfunc;
310
311         struct iio_chan_spec const      *channels;
312         int                             num_channels;
313
314         struct list_head                channel_attr_list;
315         struct attribute_group          chan_attr_group;
316         const char                      *name;
317         const struct iio_info           *info;
318         struct cdev                     chrdev;
319 #define IIO_MAX_GROUPS 6
320         const struct attribute_group    *groups[IIO_MAX_GROUPS + 1];
321         int                             groupcounter;
322 };
323
324 /**
325  * iio_device_register() - register a device with the IIO subsystem
326  * @dev_info:           Device structure filled by the device driver
327  **/
328 int iio_device_register(struct iio_dev *dev_info);
329
330 /**
331  * iio_device_unregister() - unregister a device from the IIO subsystem
332  * @dev_info:           Device structure representing the device.
333  **/
334 void iio_device_unregister(struct iio_dev *dev_info);
335
336 /**
337  * iio_push_event() - try to add event to the list for userspace reading
338  * @dev_info:           IIO device structure
339  * @ev_code:            What event
340  * @timestamp:          When the event occurred
341  **/
342 int iio_push_event(struct iio_dev *dev_info, u64 ev_code, s64 timestamp);
343
344 extern struct bus_type iio_bus_type;
345
346 /**
347  * iio_put_device() - reference counted deallocation of struct device
348  * @dev: the iio_device containing the device
349  **/
350 static inline void iio_put_device(struct iio_dev *dev)
351 {
352         if (dev)
353                 put_device(&dev->dev);
354 };
355
356 /* Can we make this smaller? */
357 #define IIO_ALIGN L1_CACHE_BYTES
358 /**
359  * iio_allocate_device() - allocate an iio_dev from a driver
360  * @sizeof_priv: Space to allocate for private structure.
361  **/
362 struct iio_dev *iio_allocate_device(int sizeof_priv);
363
364 static inline void *iio_priv(const struct iio_dev *dev)
365 {
366         return (char *)dev + ALIGN(sizeof(struct iio_dev), IIO_ALIGN);
367 }
368
369 static inline struct iio_dev *iio_priv_to_dev(void *priv)
370 {
371         return (struct iio_dev *)((char *)priv -
372                                   ALIGN(sizeof(struct iio_dev), IIO_ALIGN));
373 }
374
375 /**
376  * iio_free_device() - free an iio_dev from a driver
377  * @dev: the iio_dev associated with the device
378  **/
379 void iio_free_device(struct iio_dev *dev);
380
381 /**
382  * iio_buffer_enabled() - helper function to test if the buffer is enabled
383  * @dev_info:           IIO device info structure for device
384  **/
385 static inline bool iio_buffer_enabled(struct iio_dev *dev_info)
386 {
387         return dev_info->currentmode
388                 & (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE);
389 };
390
391 #endif /* _INDUSTRIAL_IO_H_ */