iio: trigger: Add filter callback
[pandora-kernel.git] / drivers / staging / iio / iio.h
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 <linux/irq.h>
16 #include "sysfs.h"
17 #include "chrdev.h"
18
19 /* IIO TODO LIST */
20 /*
21  * Provide means of adjusting timer accuracy.
22  * Currently assumes nano seconds.
23  */
24
25 /* Event interface flags */
26 #define IIO_BUSY_BIT_POS 1
27
28 /* naughty temporary hack to match these against the event version
29    - need to flattern these together */
30 enum iio_chan_type {
31         /* real channel types */
32         IIO_IN,
33         IIO_CURRENT,
34         IIO_POWER,
35         IIO_ACCEL,
36         IIO_IN_DIFF,
37         IIO_GYRO,
38         IIO_MAGN,
39         IIO_LIGHT,
40         IIO_INTENSITY,
41         IIO_PROXIMITY,
42         IIO_TEMP,
43         IIO_INCLI,
44         IIO_ROT,
45         IIO_ANGL,
46         IIO_TIMESTAMP,
47 };
48
49 #define IIO_MOD_X                       0
50 #define IIO_MOD_LIGHT_BOTH              0
51 #define IIO_MOD_Y                       1
52 #define IIO_MOD_LIGHT_IR                1
53 #define IIO_MOD_Z                       2
54 #define IIO_MOD_X_AND_Y                 3
55 #define IIO_MOD_X_ANX_Z                 4
56 #define IIO_MOD_Y_AND_Z                 5
57 #define IIO_MOD_X_AND_Y_AND_Z           6
58 #define IIO_MOD_X_OR_Y                  7
59 #define IIO_MOD_X_OR_Z                  8
60 #define IIO_MOD_Y_OR_Z                  9
61 #define IIO_MOD_X_OR_Y_OR_Z             10
62
63 /* Could add the raw attributes as well - allowing buffer only devices */
64 enum iio_chan_info_enum {
65         IIO_CHAN_INFO_SCALE_SHARED,
66         IIO_CHAN_INFO_SCALE_SEPARATE,
67         IIO_CHAN_INFO_OFFSET_SHARED,
68         IIO_CHAN_INFO_OFFSET_SEPARATE,
69         IIO_CHAN_INFO_CALIBSCALE_SHARED,
70         IIO_CHAN_INFO_CALIBSCALE_SEPARATE,
71         IIO_CHAN_INFO_CALIBBIAS_SHARED,
72         IIO_CHAN_INFO_CALIBBIAS_SEPARATE,
73         IIO_CHAN_INFO_PEAK_SHARED,
74         IIO_CHAN_INFO_PEAK_SEPARATE,
75         IIO_CHAN_INFO_PEAK_SCALE_SHARED,
76         IIO_CHAN_INFO_PEAK_SCALE_SEPARATE,
77 };
78
79 /**
80  * struct iio_chan_spec - specification of a single channel
81  * @type:               What type of measurement is the channel making.
82  * @channel:            What number or name do we wish to asign the channel.
83  * @channel2:           If there is a second number for a differential
84  *                      channel then this is it. If modified is set then the
85  *                      value here specifies the modifier.
86  * @address:            Driver specific identifier.
87  * @scan_index: Monotonic index to give ordering in scans when read
88  *                      from a buffer.
89  * @scan_type:          Sign:           's' or 'u' to specify signed or unsigned
90  *                      realbits:       Number of valid bits of data
91  *                      storage_bits:   Realbits + padding
92  *                      shift:          Shift right by this before masking out
93  *                                      realbits.
94  * @info_mask:          What information is to be exported about this channel.
95  *                      This includes calibbias, scale etc.
96  * @event_mask: What events can this channel produce.
97  * @extend_name:        Allows labeling of channel attributes with an
98  *                      informative name. Note this has no effect codes etc,
99  *                      unlike modifiers.
100  * @processed_val:      Flag to specify the data access attribute should be
101  *                      *_input rather than *_raw.
102  * @modified:           Does a modifier apply to this channel. What these are
103  *                      depends on the channel type.  Modifier is set in
104  *                      channel2. Examples are IIO_MOD_X for axial sensors about
105  *                      the 'x' axis.
106  * @indexed:            Specify the channel has a numerical index. If not,
107  *                      the value in channel will be suppressed for attribute
108  *                      but not for event codes. Typically set it to 0 when
109  *                      the index is false.
110  */
111 struct iio_chan_spec {
112         enum iio_chan_type      type;
113         int                     channel;
114         int                     channel2;
115         unsigned long           address;
116         int                     scan_index;
117         struct {
118                 char    sign;
119                 u8      realbits;
120                 u8      storagebits;
121                 u8      shift;
122         } scan_type;
123         const long              info_mask;
124         const long              event_mask;
125         const char              *extend_name;
126         unsigned                processed_val:1;
127         unsigned                modified:1;
128         unsigned                indexed:1;
129 };
130 /* Meant for internal use only */
131 void __iio_device_attr_deinit(struct device_attribute *dev_attr);
132 int __iio_device_attr_init(struct device_attribute *dev_attr,
133                            const char *postfix,
134                            struct iio_chan_spec const *chan,
135                            ssize_t (*readfunc)(struct device *dev,
136                                                struct device_attribute *attr,
137                                                char *buf),
138                            ssize_t (*writefunc)(struct device *dev,
139                                                 struct device_attribute *attr,
140                                                 const char *buf,
141                                                 size_t len),
142                            bool generic);
143 #define IIO_ST(si, rb, sb, sh)                                          \
144         { .sign = si, .realbits = rb, .storagebits = sb, .shift = sh }
145
146 #define IIO_CHAN(_type, _mod, _indexed, _proc, _name, _chan, _chan2,    \
147                  _inf_mask, _address, _si, _stype, _event_mask)         \
148         { .type = _type,                                                \
149           .modified = _mod,                                             \
150           .indexed = _indexed,                                          \
151           .processed_val = _proc,                                       \
152           .extend_name = _name,                                         \
153           .channel = _chan,                                             \
154           .channel2 = _chan2,                                           \
155           .info_mask = _inf_mask,                                       \
156           .address = _address,                                          \
157           .scan_index = _si,                                            \
158           .scan_type = _stype,                                          \
159           .event_mask = _event_mask }
160
161 #define IIO_CHAN_SOFT_TIMESTAMP(_si)                                    \
162         { .type = IIO_TIMESTAMP, .channel = -1,                         \
163                         .scan_index = _si, .scan_type = IIO_ST('s', 64, 64, 0) }
164
165 int __iio_add_chan_devattr(const char *postfix,
166                            const char *group,
167                            struct iio_chan_spec const *chan,
168                            ssize_t (*func)(struct device *dev,
169                                            struct device_attribute *attr,
170                                            char *buf),
171                            ssize_t (*writefunc)(struct device *dev,
172                                                 struct device_attribute *attr,
173                                                 const char *buf,
174                                                 size_t len),
175                            int mask,
176                            bool generic,
177                            struct device *dev,
178                            struct list_head *attr_list);
179 /**
180  * iio_get_time_ns() - utility function to get a time stamp for events etc
181  **/
182 static inline s64 iio_get_time_ns(void)
183 {
184         struct timespec ts;
185         /*
186          * calls getnstimeofday.
187          * If hrtimers then up to ns accurate, if not microsecond.
188          */
189         ktime_get_real_ts(&ts);
190
191         return timespec_to_ns(&ts);
192 }
193
194 /* Device operating modes */
195 #define INDIO_DIRECT_MODE               0x01
196 #define INDIO_RING_TRIGGERED            0x02
197 #define INDIO_RING_HARDWARE_BUFFER      0x08
198
199 #define INDIO_ALL_RING_MODES (INDIO_RING_TRIGGERED | INDIO_RING_HARDWARE_BUFFER)
200
201 /* Vast majority of this is set by the industrialio subsystem on a
202  * call to iio_device_register. */
203 #define IIO_VAL_INT 1
204 #define IIO_VAL_INT_PLUS_MICRO 2
205 #define IIO_VAL_INT_PLUS_NANO 3
206
207 struct iio_trigger; /* forward declaration */
208
209 /**
210  * struct iio_info - constant information about device
211  * @driver_module:      module structure used to ensure correct
212  *                      ownership of chrdevs etc
213  * @num_interrupt_lines:number of physical interrupt lines from device
214  * @event_attrs:        event control attributes
215  * @attrs:              general purpose device attributes
216  * @read_raw:           function to request a value from the device.
217  *                      mask specifies which value. Note 0 means a reading of
218  *                      the channel in question.  Return value will specify the
219  *                      type of value returned by the device. val and val2 will
220  *                      contain the elements making up the returned value.
221  * @write_raw:          function to write a value to the device.
222  *                      Parameters are the same as for read_raw.
223  * @read_event_config:  find out if the event is enabled.
224  * @write_event_config: set if the event is enabled.
225  * @read_event_value:   read a value associated with the event. Meaning
226  *                      is event dependant. event_code specifies which event.
227  * @write_event_value:  write the value associate with the event.
228  *                      Meaning is event dependent.
229  * @validate_trigger:   function to validate the trigger when the
230  *                      current trigger gets changed.
231  **/
232 struct iio_info {
233         struct module                   *driver_module;
234         int                             num_interrupt_lines;
235         struct attribute_group          *event_attrs;
236         const struct attribute_group    *attrs;
237
238         int (*read_raw)(struct iio_dev *indio_dev,
239                         struct iio_chan_spec const *chan,
240                         int *val,
241                         int *val2,
242                         long mask);
243
244         int (*write_raw)(struct iio_dev *indio_dev,
245                          struct iio_chan_spec const *chan,
246                          int val,
247                          int val2,
248                          long mask);
249
250         int (*read_event_config)(struct iio_dev *indio_dev,
251                                  int event_code);
252
253         int (*write_event_config)(struct iio_dev *indio_dev,
254                                   int event_code,
255                                   int state);
256
257         int (*read_event_value)(struct iio_dev *indio_dev,
258                                 int event_code,
259                                 int *val);
260         int (*write_event_value)(struct iio_dev *indio_dev,
261                                  int event_code,
262                                  int val);
263         int (*validate_trigger)(struct iio_dev *indio_dev,
264                                 struct iio_trigger *trig);
265
266 };
267
268 /**
269  * struct iio_dev - industrial I/O device
270  * @id:                 [INTERN] used to identify device internally
271  * @dev_data:           [DRIVER] device specific data
272  * @modes:              [DRIVER] operating modes supported by device
273  * @currentmode:        [DRIVER] current operating mode
274  * @dev:                [DRIVER] device structure, should be assigned a parent
275  *                      and owner
276  * @event_interfaces:   [INTERN] event chrdevs associated with interrupt lines
277  * @ring:               [DRIVER] any ring buffer present
278  * @mlock:              [INTERN] lock used to prevent simultaneous device state
279  *                      changes
280  * @available_scan_masks: [DRIVER] optional array of allowed bitmasks
281  * @trig:               [INTERN] current device trigger (ring 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  * @name:               [DRIVER] name of the device.
288  **/
289 struct iio_dev {
290         int                             id;
291         void                            *dev_data;
292         int                             modes;
293         int                             currentmode;
294         struct device                   dev;
295
296         struct iio_event_interface      *event_interfaces;
297
298         struct iio_ring_buffer          *ring;
299         struct mutex                    mlock;
300
301         u32                             *available_scan_masks;
302         struct iio_trigger              *trig;
303         struct iio_poll_func            *pollfunc;
304
305         struct iio_chan_spec const *channels;
306         int num_channels;
307
308         struct list_head channel_attr_list;
309         const char *name;
310         const struct iio_info *info;
311 };
312
313 /**
314  * iio_device_register() - register a device with the IIO subsystem
315  * @dev_info:           Device structure filled by the device driver
316  **/
317 int iio_device_register(struct iio_dev *dev_info);
318
319 /**
320  * iio_device_unregister() - unregister a device from the IIO subsystem
321  * @dev_info:           Device structure representing the device.
322  **/
323 void iio_device_unregister(struct iio_dev *dev_info);
324
325 /**
326  * iio_push_event() - try to add event to the list for userspace reading
327  * @dev_info:           IIO device structure
328  * @ev_line:            Which event line (hardware interrupt)
329  * @ev_code:            What event
330  * @timestamp:          When the event occurred
331  **/
332 int iio_push_event(struct iio_dev *dev_info,
333                   int ev_line,
334                   int ev_code,
335                   s64 timestamp);
336
337 /* Used to distinguish between bipolar and unipolar scan elemenents.
338  * Whilst this may seem obvious, we may well want to change the representation
339  * in the future!*/
340 #define IIO_SIGNED(a) -(a)
341 #define IIO_UNSIGNED(a) (a)
342
343 extern dev_t iio_devt;
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 /**
357  * to_iio_dev() - get iio_dev for which we have the struct device
358  * @d: the struct device
359  **/
360 static inline struct iio_dev *to_iio_dev(struct device *d)
361 {
362         return container_of(d, struct iio_dev, dev);
363 };
364
365 /**
366  * iio_dev_get_devdata() - helper function gets device specific data
367  * @d: the iio_dev associated with the device
368  **/
369 static inline void *iio_dev_get_devdata(struct iio_dev *d)
370 {
371         return d->dev_data;
372 }
373
374
375 /* Can we make this smaller? */
376 #define IIO_ALIGN L1_CACHE_BYTES
377 /**
378  * iio_allocate_device() - allocate an iio_dev from a driver
379  * @sizeof_priv: Space to allocate for private structure.
380  **/
381 struct iio_dev *iio_allocate_device(int sizeof_priv);
382
383 static inline void *iio_priv(const struct iio_dev *dev)
384 {
385         return (char *)dev + ALIGN(sizeof(struct iio_dev), IIO_ALIGN);
386 }
387
388 static inline struct iio_dev *iio_priv_to_dev(void *priv)
389 {
390         return (struct iio_dev *)((char *)priv -
391                                   ALIGN(sizeof(struct iio_dev), IIO_ALIGN));
392 }
393
394 /**
395  * iio_free_device() - free an iio_dev from a driver
396  * @dev: the iio_dev associated with the device
397  **/
398 void iio_free_device(struct iio_dev *dev);
399
400 /**
401  * iio_put() - internal module reference count reduce
402  **/
403 void iio_put(void);
404
405 /**
406  * iio_get() - internal module reference count increase
407  **/
408 void iio_get(void);
409
410 /**
411  * iio_device_get_chrdev_minor() - get an unused minor number
412  **/
413 int iio_device_get_chrdev_minor(void);
414 void iio_device_free_chrdev_minor(int val);
415
416 /**
417  * iio_ring_enabled() - helper function to test if any form of ring is enabled
418  * @dev_info:           IIO device info structure for device
419  **/
420 static inline bool iio_ring_enabled(struct iio_dev *dev_info)
421 {
422         return dev_info->currentmode
423                 & (INDIO_RING_TRIGGERED
424                    | INDIO_RING_HARDWARE_BUFFER);
425 };
426
427 struct ida;
428
429 int iio_get_new_ida_val(struct ida *this_ida);
430 void iio_free_ida_val(struct ida *this_ida, int id);
431 #endif /* _INDUSTRIAL_IO_H_ */