staging: iio sync drivers with current ABI
[pandora-kernel.git] / drivers / staging / iio / sysfs.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  * General attributes
10  */
11
12 #ifndef _INDUSTRIAL_IO_SYSFS_H_
13 #define _INDUSTRIAL_IO_SYSFS_H_
14
15 #include "iio.h"
16
17 /**
18  * struct iio_event_attr - event control attribute
19  * @dev_attr:   underlying device attribute
20  * @mask:       mask for the event when detecting
21  * @listel:     list header to allow addition to list of event handlers
22 */
23 struct iio_event_attr {
24         struct device_attribute dev_attr;
25         int mask;
26         struct iio_event_handler_list *listel;
27 };
28
29 #define to_iio_event_attr(_dev_attr) \
30         container_of(_dev_attr, struct iio_event_attr, dev_attr)
31
32 /**
33  * struct iio_chrdev_minor_attr - simple attribute to allow reading of chrdev
34  *                              minor number
35  * @dev_attr:   underlying device attribute
36  * @minor:      the minor number
37  */
38 struct iio_chrdev_minor_attr {
39         struct device_attribute dev_attr;
40         int minor;
41 };
42
43 void
44 __init_iio_chrdev_minor_attr(struct iio_chrdev_minor_attr *minor_attr,
45                            const char *name,
46                            struct module *owner,
47                            int id);
48
49
50 #define to_iio_chrdev_minor_attr(_dev_attr) \
51         container_of(_dev_attr, struct iio_chrdev_minor_attr, dev_attr);
52
53 /**
54  * struct iio_dev_attr - iio specific device attribute
55  * @dev_attr:   underlying device attribute
56  * @address:    associated register address
57  * @val2:       secondary attribute value
58  */
59 struct iio_dev_attr {
60         struct device_attribute dev_attr;
61         int address;
62         int val2;
63 };
64
65 #define to_iio_dev_attr(_dev_attr)                              \
66         container_of(_dev_attr, struct iio_dev_attr, dev_attr)
67
68 ssize_t iio_read_const_attr(struct device *dev,
69                             struct device_attribute *attr,
70                             char *len);
71
72 /**
73  * struct iio_const_attr - constant device specific attribute
74  *                         often used for things like available modes
75  * @string:     attribute string
76  * @dev_attr:   underlying device attribute
77  */
78 struct iio_const_attr {
79         const char *string;
80         struct device_attribute dev_attr;
81 };
82
83 #define to_iio_const_attr(_dev_attr) \
84         container_of(_dev_attr, struct iio_const_attr, dev_attr)
85
86 /* Some attributes will be hard coded (device dependent) and not require an
87    address, in these cases pass a negative */
88 #define IIO_ATTR(_name, _mode, _show, _store, _addr)            \
89         { .dev_attr = __ATTR(_name, _mode, _show, _store),      \
90           .address = _addr }
91
92 #define IIO_ATTR_2(_name, _mode, _show, _store, _addr, _val2)   \
93         { .dev_attr = __ATTR(_name, _mode, _show, _store),      \
94                         .address = _addr,                       \
95                         .val2 = _val2 }
96
97 #define IIO_DEVICE_ATTR(_name, _mode, _show, _store, _addr)     \
98         struct iio_dev_attr iio_dev_attr_##_name                \
99         = IIO_ATTR(_name, _mode, _show, _store, _addr)
100
101 #define IIO_DEVICE_ATTR_NAMED(_vname, _name, _mode, _show, _store, _addr) \
102         struct iio_dev_attr iio_dev_attr_##_vname                       \
103         = IIO_ATTR(_name, _mode, _show, _store, _addr)
104
105 #define IIO_DEVICE_ATTR_2(_name, _mode, _show, _store, _addr, _val2)    \
106         struct iio_dev_attr iio_dev_attr_##_name                        \
107         = IIO_ATTR_2(_name, _mode, _show, _store, _addr, _val2)
108
109 #define IIO_CONST_ATTR(_name, _string)                                  \
110         struct iio_const_attr iio_const_attr_##_name                    \
111         = { .string = _string,                                          \
112             .dev_attr = __ATTR(_name, S_IRUGO, iio_read_const_attr, NULL)}
113
114 /* Generic attributes of onetype or another */
115
116 /**
117  * IIO_DEV_ATTR_REV - revision number for the device
118  * @_show: output method for the attribute
119  *
120  * Very much device dependent.
121  **/
122 #define IIO_DEV_ATTR_REV(_show)                 \
123         IIO_DEVICE_ATTR(revision, S_IRUGO, _show, NULL, 0)
124
125 /**
126  * IIO_DEV_ATTR_NAME - chip type dependent identifier
127  * @_show: output method for the attribute
128  **/
129 #define IIO_DEV_ATTR_NAME(_show)                                \
130         IIO_DEVICE_ATTR(name, S_IRUGO, _show, NULL, 0)
131
132 /**
133  * IIO_CONST_ATTR_NAME - constant identifier
134  * @_string: the name
135  **/
136 #define IIO_CONST_ATTR_NAME(_string)                            \
137         IIO_CONST_ATTR(name, _string)
138
139 /**
140  * IIO_DEV_ATTR_SAMP_FREQ - sets any internal clock frequency
141  * @_mode: sysfs file mode/permissions
142  * @_show: output method for the attribute
143  * @_store: input method for the attribute
144  **/
145 #define IIO_DEV_ATTR_SAMP_FREQ(_mode, _show, _store)                    \
146         IIO_DEVICE_ATTR(sampling_frequency, _mode, _show, _store, 0)
147
148 /**
149  * IIO_DEV_ATTR_AVAIL_SAMP_FREQ - list available sampling frequencies
150  * @_show: output method for the attribute
151  *
152  * May be mode dependent on some devices
153  **/
154 /* Deprecated */
155 #define IIO_DEV_ATTR_AVAIL_SAMP_FREQ(_show)                             \
156         IIO_DEVICE_ATTR(available_sampling_frequency, S_IRUGO, _show, NULL, 0)
157
158 #define IIO_DEV_ATTR_SAMP_FREQ_AVAIL(_show)                             \
159         IIO_DEVICE_ATTR(sampling_frequency_available, S_IRUGO, _show, NULL, 0)
160 /**
161  * IIO_CONST_ATTR_AVAIL_SAMP_FREQ - list available sampling frequencies
162  * @_string: frequency string for the attribute
163  *
164  * Constant version
165  **/
166 #define IIO_CONST_ATTR_SAMP_FREQ_AVAIL(_string)                 \
167         IIO_CONST_ATTR(sampling_frequency_available, _string)
168
169 /**
170  * IIO_DEV_ATTR_SCAN_MODE - select a scan mode
171  * @_mode: sysfs file mode/permissions
172  * @_show: output method for the attribute
173  * @_store: input method for the attribute
174  *
175  * This is used when only certain combinations of inputs may be read in one
176  * scan.
177  **/
178 #define IIO_DEV_ATTR_SCAN_MODE(_mode, _show, _store)            \
179         IIO_DEVICE_ATTR(scan_mode, _mode, _show, _store, 0)
180
181 /**
182  * IIO_DEV_ATTR_AVAIL_SCAN_MODES - list available scan modes
183  * @_show: output method for the attribute
184  **/
185 #define IIO_DEV_ATTR_AVAIL_SCAN_MODES(_show)                            \
186         IIO_DEVICE_ATTR(available_scan_modes, S_IRUGO, _show, NULL, 0)
187
188 /**
189  * IIO_DEV_ATTR_SCAN - result of scan of multiple channels
190  * @_show: output method for the attribute
191  **/
192 #define IIO_DEV_ATTR_SCAN(_show)                \
193         IIO_DEVICE_ATTR(scan, S_IRUGO, _show, NULL, 0);
194
195 /**
196  * IIO_DEV_ATTR_INPUT - direct read of a single input channel
197  * @_number: input channel number
198  * @_show: output method for the attribute
199  **/
200 #define IIO_DEV_ATTR_INPUT(_number, _show)                              \
201         IIO_DEVICE_ATTR(in##_number, S_IRUGO, _show, NULL, _number)
202
203 /**
204  * IIO_DEV_ATTR_SW_RING_ENABLE - enable software ring buffer
205  * @_show: output method for the attribute
206  * @_store: input method for the attribute
207  *
208  * Success may be dependent on attachment of trigger previously.
209  **/
210 #define IIO_DEV_ATTR_SW_RING_ENABLE(_show, _store)                      \
211         IIO_DEVICE_ATTR(sw_ring_enable, S_IRUGO | S_IWUSR, _show, _store, 0)
212
213 /**
214  * IIO_DEV_ATTR_HW_RING_ENABLE - enable hardware ring buffer
215  * @_show: output method for the attribute
216  * @_store: input method for the attribute
217  *
218  * This is a different attribute from the software one as one can envision
219  * schemes where a combination of the two may be used.
220  **/
221 #define IIO_DEV_ATTR_HW_RING_ENABLE(_show, _store)                      \
222         IIO_DEVICE_ATTR(hw_ring_enable, S_IRUGO | S_IWUSR, _show, _store, 0)
223
224 /**
225  * IIO_DEV_ATTR_BPSE - set number of bits per scan element
226  * @_mode: sysfs file mode/permissions
227  * @_show: output method for the attribute
228  * @_store: input method for the attribute
229  **/
230 #define IIO_DEV_ATTR_BPSE(_mode, _show, _store)         \
231         IIO_DEVICE_ATTR(bpse, _mode, _show, _store, 0)
232
233 /**
234  * IIO_DEV_ATTR_BPSE_AVAILABLE - number of bits per scan element supported
235  * @_show: output method for the attribute
236  **/
237 #define IIO_DEV_ATTR_BPSE_AVAILABLE(_show)                              \
238         IIO_DEVICE_ATTR(bpse_available, S_IRUGO, _show, NULL, 0)
239
240 /**
241  * IIO_DEV_ATTR_TEMP - many sensors have auxiliary temperature sensors
242  * @_show: output method for the attribute
243  **/
244 #define IIO_DEV_ATTR_TEMP(_show)                        \
245         IIO_DEVICE_ATTR(temp, S_IRUGO, _show, NULL, 0)
246
247 #define IIO_DEV_ATTR_TEMP_RAW(_show)                    \
248         IIO_DEVICE_ATTR(temp_raw, S_IRUGO, _show, NULL, 0)
249
250 #define IIO_CONST_ATTR_TEMP_OFFSET(_string)             \
251         IIO_CONST_ATTR(temp_offset, _string)
252
253 #define IIO_CONST_ATTR_TEMP_SCALE(_string)              \
254         IIO_CONST_ATTR(temp_scale, _string)
255
256 /**
257  * IIO_EVENT_SH - generic shared event handler
258  * @_name: event name
259  * @_handler: handler function to be called
260  *
261  * This is used in cases where more than one event may result from a single
262  * handler.  Often the case that some alarm register must be read and multiple
263  * alarms may have been triggered.
264  **/
265 #define IIO_EVENT_SH(_name, _handler)                                   \
266         static struct iio_event_handler_list                            \
267         iio_event_##_name = {                                           \
268                 .handler = _handler,                                    \
269                 .refcount = 0,                                          \
270                 .exist_lock = __MUTEX_INITIALIZER(iio_event_##_name     \
271                                                   .exist_lock),         \
272                 .list = {                                               \
273                         .next = &iio_event_##_name.list,                \
274                         .prev = &iio_event_##_name.list,                \
275                 },                                                      \
276         };
277
278 /**
279  * IIO_EVENT_ATTR_SH - generic shared event attribute
280  * @_name: event name
281  * @_ev_list: event handler list
282  * @_show: output method for the attribute
283  * @_store: input method for the attribute
284  * @_mask: mask used when detecting the event
285  *
286  * An attribute with an associated IIO_EVENT_SH
287  **/
288 #define IIO_EVENT_ATTR_SH(_name, _ev_list, _show, _store, _mask)        \
289         static struct iio_event_attr                                    \
290         iio_event_attr_##_name                                          \
291         = { .dev_attr = __ATTR(_name, S_IRUGO | S_IWUSR,                \
292                                _show, _store),                          \
293             .mask = _mask,                                              \
294             .listel = &_ev_list };
295
296 #define IIO_EVENT_ATTR_NAMED_SH(_vname, _name, _ev_list, _show, _store, _mask) \
297         static struct iio_event_attr                                    \
298         iio_event_attr_##_vname                                         \
299         = { .dev_attr = __ATTR(_name, S_IRUGO | S_IWUSR,                \
300                                _show, _store),                          \
301             .mask = _mask,                                              \
302             .listel = &_ev_list };
303
304 /**
305  * IIO_EVENT_ATTR - non-shared event attribute
306  * @_name: event name
307  * @_show: output method for the attribute
308  * @_store: input method for the attribute
309  * @_mask: mask used when detecting the event
310  * @_handler: handler function to be called
311  **/
312 #define IIO_EVENT_ATTR(_name, _show, _store, _mask, _handler)           \
313         IIO_EVENT_SH(_name, _handler);                                  \
314         static struct                                                   \
315         iio_event_attr                                                  \
316         iio_event_attr_##_name                                          \
317         = { .dev_attr = __ATTR(_name, S_IRUGO | S_IWUSR,                \
318                                _show, _store),                          \
319             .mask = _mask,                                              \
320             .listel = &iio_event_##_name };                             \
321
322 /**
323  * IIO_EVENT_ATTR_DATA_RDY - event driven by data ready signal
324  * @_show: output method for the attribute
325  * @_store: input method for the attribute
326  * @_mask: mask used when detecting the event
327  * @_handler: handler function to be called
328  *
329  * Not typically implemented in devices where full triggering support
330  * has been implemented.
331  **/
332 #define IIO_EVENT_ATTR_DATA_RDY(_show, _store, _mask, _handler) \
333         IIO_EVENT_ATTR(data_rdy, _show, _store, _mask, _handler)
334
335 #define IIO_EVENT_CODE_DATA_RDY         100
336 #define IIO_EVENT_CODE_RING_BASE        200
337 #define IIO_EVENT_CODE_ACCEL_BASE       300
338 #define IIO_EVENT_CODE_GYRO_BASE        400
339 #define IIO_EVENT_CODE_ADC_BASE         500
340 #define IIO_EVENT_CODE_MISC_BASE        600
341 #define IIO_EVENT_CODE_LIGHT_BASE       700
342
343 #define IIO_EVENT_CODE_DEVICE_SPECIFIC  1000
344
345 /**
346  * IIO_EVENT_ATTR_RING_50_FULL - ring buffer event to indicate 50% full
347  * @_show: output method for the attribute
348  * @_store: input method for the attribute
349  * @_mask: mask used when detecting the event
350  * @_handler: handler function to be called
351  **/
352 #define IIO_EVENT_ATTR_RING_50_FULL(_show, _store, _mask, _handler)     \
353         IIO_EVENT_ATTR(ring_50_full, _show, _store, _mask, _handler)
354
355 /**
356  * IIO_EVENT_ATTR_RING_50_FULL_SH - shared ring event to indicate 50% full
357  * @_evlist: event handler list
358  * @_show: output method for the attribute
359  * @_store: input method for the attribute
360  * @_mask: mask used when detecting the event
361  **/
362 #define IIO_EVENT_ATTR_RING_50_FULL_SH(_evlist, _show, _store, _mask)   \
363         IIO_EVENT_ATTR_SH(ring_50_full, _evlist, _show, _store, _mask)
364
365 /**
366  * IIO_EVENT_ATTR_RING_75_FULL_SH - shared ring event to indicate 75% full
367  * @_evlist: event handler list
368  * @_show: output method for the attribute
369  * @_store: input method for the attribute
370  * @_mask: mask used when detecting the event
371  **/
372 #define IIO_EVENT_ATTR_RING_75_FULL_SH(_evlist, _show, _store, _mask)   \
373         IIO_EVENT_ATTR_SH(ring_75_full, _evlist, _show, _store, _mask)
374
375 #define IIO_EVENT_CODE_RING_50_FULL     IIO_EVENT_CODE_RING_BASE
376 #define IIO_EVENT_CODE_RING_75_FULL     (IIO_EVENT_CODE_RING_BASE + 1)
377 #define IIO_EVENT_CODE_RING_100_FULL    (IIO_EVENT_CODE_RING_BASE + 2)
378
379 #endif /* _INDUSTRIAL_IO_SYSFS_H_ */