]> Pileus Git - ~andy/linux/blob - drivers/staging/iio/sysfs.h
staging:iio: allow channels to be set up using a table of iio_channel_spec structures.
[~andy/linux] / 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         struct list_head l;
28 };
29
30 #define to_iio_event_attr(_dev_attr) \
31         container_of(_dev_attr, struct iio_event_attr, dev_attr)
32
33 /**
34  * struct iio_dev_attr - iio specific device attribute
35  * @dev_attr:   underlying device attribute
36  * @address:    associated register address
37  * @val2:       secondary attribute value
38  * @l:          list head for maintaining list of dynamically created attrs.
39  */
40 struct iio_dev_attr {
41         struct device_attribute dev_attr;
42         int address;
43         int val2;
44         struct list_head l;
45         struct iio_chan_spec const *c;
46 };
47
48 #define to_iio_dev_attr(_dev_attr)                              \
49         container_of(_dev_attr, struct iio_dev_attr, dev_attr)
50
51 ssize_t iio_read_const_attr(struct device *dev,
52                             struct device_attribute *attr,
53                             char *len);
54
55 /**
56  * struct iio_const_attr - constant device specific attribute
57  *                         often used for things like available modes
58  * @string:     attribute string
59  * @dev_attr:   underlying device attribute
60  */
61 struct iio_const_attr {
62         const char *string;
63         struct device_attribute dev_attr;
64 };
65
66 #define to_iio_const_attr(_dev_attr) \
67         container_of(_dev_attr, struct iio_const_attr, dev_attr)
68
69 /* Some attributes will be hard coded (device dependent) and not require an
70    address, in these cases pass a negative */
71 #define IIO_ATTR(_name, _mode, _show, _store, _addr)            \
72         { .dev_attr = __ATTR(_name, _mode, _show, _store),      \
73           .address = _addr }
74
75 #define IIO_DEVICE_ATTR(_name, _mode, _show, _store, _addr)     \
76         struct iio_dev_attr iio_dev_attr_##_name                \
77         = IIO_ATTR(_name, _mode, _show, _store, _addr)
78
79 #define IIO_DEVICE_ATTR_NAMED(_vname, _name, _mode, _show, _store, _addr) \
80         struct iio_dev_attr iio_dev_attr_##_vname                       \
81         = IIO_ATTR(_name, _mode, _show, _store, _addr)
82
83 #define IIO_DEVICE_ATTR_2(_name, _mode, _show, _store, _addr, _val2)    \
84         struct iio_dev_attr iio_dev_attr_##_name                        \
85         = IIO_ATTR_2(_name, _mode, _show, _store, _addr, _val2)
86
87 #define IIO_CONST_ATTR(_name, _string)                                  \
88         struct iio_const_attr iio_const_attr_##_name                    \
89         = { .string = _string,                                          \
90             .dev_attr = __ATTR(_name, S_IRUGO, iio_read_const_attr, NULL)}
91
92 #define IIO_CONST_ATTR_NAMED(_vname, _name, _string)                    \
93         struct iio_const_attr iio_const_attr_##_vname                   \
94         = { .string = _string,                                          \
95             .dev_attr = __ATTR(_name, S_IRUGO, iio_read_const_attr, NULL)}
96 /* Generic attributes of onetype or another */
97
98 /**
99  * IIO_DEV_ATTR_REV - revision number for the device
100  * @_show: output method for the attribute
101  *
102  * Very much device dependent.
103  **/
104 #define IIO_DEV_ATTR_REV(_show)                 \
105         IIO_DEVICE_ATTR(revision, S_IRUGO, _show, NULL, 0)
106
107 /**
108  * IIO_DEV_ATTR_NAME - chip type dependent identifier
109  * @_show: output method for the attribute
110  **/
111 #define IIO_DEV_ATTR_NAME(_show)                                \
112         IIO_DEVICE_ATTR(name, S_IRUGO, _show, NULL, 0)
113
114 /**
115  * IIO_DEV_ATTR_RESET: resets the device
116  **/
117 #define IIO_DEV_ATTR_RESET(_store)                      \
118         IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, _store, 0)
119
120 /**
121  * IIO_CONST_ATTR_NAME - constant identifier
122  * @_string: the name
123  **/
124 #define IIO_CONST_ATTR_NAME(_string)                            \
125         IIO_CONST_ATTR(name, _string)
126
127 /**
128  * IIO_DEV_ATTR_SAMP_FREQ - sets any internal clock frequency
129  * @_mode: sysfs file mode/permissions
130  * @_show: output method for the attribute
131  * @_store: input method for the attribute
132  **/
133 #define IIO_DEV_ATTR_SAMP_FREQ(_mode, _show, _store)                    \
134         IIO_DEVICE_ATTR(sampling_frequency, _mode, _show, _store, 0)
135
136 /**
137  * IIO_DEV_ATTR_AVAIL_SAMP_FREQ - list available sampling frequencies
138  * @_show: output method for the attribute
139  *
140  * May be mode dependent on some devices
141  **/
142 /* Deprecated */
143 #define IIO_DEV_ATTR_AVAIL_SAMP_FREQ(_show)                             \
144         IIO_DEVICE_ATTR(available_sampling_frequency, S_IRUGO, _show, NULL, 0)
145
146 #define IIO_DEV_ATTR_SAMP_FREQ_AVAIL(_show)                             \
147         IIO_DEVICE_ATTR(sampling_frequency_available, S_IRUGO, _show, NULL, 0)
148 /**
149  * IIO_CONST_ATTR_AVAIL_SAMP_FREQ - list available sampling frequencies
150  * @_string: frequency string for the attribute
151  *
152  * Constant version
153  **/
154 #define IIO_CONST_ATTR_SAMP_FREQ_AVAIL(_string)                 \
155         IIO_CONST_ATTR(sampling_frequency_available, _string)
156
157 /**
158  * IIO_DEV_ATTR_SW_RING_ENABLE - enable software ring buffer
159  * @_show: output method for the attribute
160  * @_store: input method for the attribute
161  *
162  * Success may be dependent on attachment of trigger previously.
163  **/
164 #define IIO_DEV_ATTR_SW_RING_ENABLE(_show, _store)                      \
165         IIO_DEVICE_ATTR(sw_ring_enable, S_IRUGO | S_IWUSR, _show, _store, 0)
166
167 /**
168  * IIO_DEV_ATTR_HW_RING_ENABLE - enable hardware ring buffer
169  * @_show: output method for the attribute
170  * @_store: input method for the attribute
171  *
172  * This is a different attribute from the software one as one can envision
173  * schemes where a combination of the two may be used.
174  **/
175 #define IIO_DEV_ATTR_HW_RING_ENABLE(_show, _store)                      \
176         IIO_DEVICE_ATTR(hw_ring_enable, S_IRUGO | S_IWUSR, _show, _store, 0)
177
178 #define IIO_DEV_ATTR_TEMP_RAW(_show)                    \
179         IIO_DEVICE_ATTR(temp_raw, S_IRUGO, _show, NULL, 0)
180
181 #define IIO_CONST_ATTR_TEMP_OFFSET(_string)             \
182         IIO_CONST_ATTR(temp_offset, _string)
183
184 #define IIO_CONST_ATTR_TEMP_SCALE(_string)              \
185         IIO_CONST_ATTR(temp_scale, _string)
186
187 /**
188  * IIO_EVENT_SH - generic shared event handler
189  * @_name: event name
190  * @_handler: handler function to be called
191  *
192  * This is used in cases where more than one event may result from a single
193  * handler.  Often the case that some alarm register must be read and multiple
194  * alarms may have been triggered.
195  **/
196 #define IIO_EVENT_SH(_name, _handler)                                   \
197         static struct iio_event_handler_list                            \
198         iio_event_##_name = {                                           \
199                 .handler = _handler,                                    \
200                 .refcount = 0,                                          \
201                 .exist_lock = __MUTEX_INITIALIZER(iio_event_##_name     \
202                                                   .exist_lock),         \
203                 .list = {                                               \
204                         .next = &iio_event_##_name.list,                \
205                         .prev = &iio_event_##_name.list,                \
206                 },                                                      \
207         };
208
209 /**
210  * IIO_EVENT_ATTR_SH - generic shared event attribute
211  * @_name: event name
212  * @_ev_list: event handler list
213  * @_show: output method for the attribute
214  * @_store: input method for the attribute
215  * @_mask: mask used when detecting the event
216  *
217  * An attribute with an associated IIO_EVENT_SH
218  **/
219 #define IIO_EVENT_ATTR_SH(_name, _ev_list, _show, _store, _mask)        \
220         static struct iio_event_attr                                    \
221         iio_event_attr_##_name                                          \
222         = { .dev_attr = __ATTR(_name, S_IRUGO | S_IWUSR,                \
223                                _show, _store),                          \
224             .mask = _mask,                                              \
225             .listel = &_ev_list };
226
227 #define IIO_EVENT_ATTR_NAMED_SH(_vname, _name, _ev_list, _show, _store, _mask) \
228         static struct iio_event_attr                                    \
229         iio_event_attr_##_vname                                         \
230         = { .dev_attr = __ATTR(_name, S_IRUGO | S_IWUSR,                \
231                                _show, _store),                          \
232             .mask = _mask,                                              \
233             .listel = &_ev_list };
234
235 /**
236  * IIO_EVENT_ATTR - non-shared event attribute
237  * @_name: event name
238  * @_show: output method for the attribute
239  * @_store: input method for the attribute
240  * @_mask: mask used when detecting the event
241  * @_handler: handler function to be called
242  **/
243 #define IIO_EVENT_ATTR(_name, _show, _store, _mask, _handler)           \
244         IIO_EVENT_SH(_name, _handler);                                  \
245         static struct                                                   \
246         iio_event_attr                                                  \
247         iio_event_attr_##_name                                          \
248         = { .dev_attr = __ATTR(_name, S_IRUGO | S_IWUSR,                \
249                                _show, _store),                          \
250             .mask = _mask,                                              \
251             .listel = &iio_event_##_name };                             \
252
253 /**
254  * IIO_EVENT_ATTR_DATA_RDY - event driven by data ready signal
255  * @_show: output method for the attribute
256  * @_store: input method for the attribute
257  * @_mask: mask used when detecting the event
258  * @_handler: handler function to be called
259  *
260  * Not typically implemented in devices where full triggering support
261  * has been implemented.
262  **/
263 #define IIO_EVENT_ATTR_DATA_RDY(_show, _store, _mask, _handler) \
264         IIO_EVENT_ATTR(data_rdy, _show, _store, _mask, _handler)
265
266 /* must match our channel defs */
267 #define IIO_EV_CLASS_IN                 IIO_IN
268 #define IIO_EV_CLASS_IN_DIFF            IIO_IN_DIFF
269 #define IIO_EV_CLASS_ACCEL              IIO_ACCEL
270 #define IIO_EV_CLASS_GYRO               IIO_GYRO
271 #define IIO_EV_CLASS_MAGN               IIO_MAGN
272 #define IIO_EV_CLASS_LIGHT              IIO_LIGHT
273 #define IIO_EV_CLASS_PROXIMITY          IIO_PROXIMITY
274 #define IIO_EV_CLASS_TEMP               IIO_TEMP
275 #define IIO_EV_CLASS_BUFFER             IIO_BUFFER
276
277 #define IIO_EV_MOD_X                    IIO_MOD_X
278 #define IIO_EV_MOD_Y                    IIO_MOD_Y
279 #define IIO_EV_MOD_Z                    IIO_MOD_Z
280 #define IIO_EV_MOD_X_AND_Y              IIO_MOD_X_AND_Y
281 #define IIO_EV_MOD_X_ANX_Z              IIO_MOD_X_AND_Z
282 #define IIO_EV_MOD_Y_AND_Z              IIO_MOD_Y_AND_Z
283 #define IIO_EV_MOD_X_AND_Y_AND_Z        IIO_MOD_X_AND_Y_AND_Z
284 #define IIO_EV_MOD_X_OR_Y               IIO_MOD_X_OR_Y
285 #define IIO_EV_MOD_X_OR_Z               IIO_MOD_X_OR_Z
286 #define IIO_EV_MOD_Y_OR_Z               IIO_MOD_Y_OR_Z
287 #define IIO_EV_MOD_X_OR_Y_OR_Z          IIO_MOD_X_OR_Y_OR_Z
288
289 #define IIO_EV_TYPE_THRESH              0
290 #define IIO_EV_TYPE_MAG                 1
291 #define IIO_EV_TYPE_ROC                 2
292
293 #define IIO_EV_DIR_EITHER               0
294 #define IIO_EV_DIR_RISING               1
295 #define IIO_EV_DIR_FALLING              2
296
297 #define IIO_EV_TYPE_MAX 8
298 #define IIO_EV_BIT(type, direction)                     \
299         (1 << (type*IIO_EV_TYPE_MAX + direction))
300
301 #define IIO_EVENT_CODE(channelclass, orient_bit, number,                \
302                        modifier, type, direction)                       \
303         (channelclass | (orient_bit << 8) | ((number) << 9) |           \
304          ((modifier) << 13) | ((type) << 16) | ((direction) << 24))
305
306 #define IIO_MOD_EVENT_CODE(channelclass, number, modifier,              \
307                            type, direction)                             \
308         IIO_EVENT_CODE(channelclass, 1, number, modifier, type, direction)
309
310 #define IIO_UNMOD_EVENT_CODE(channelclass, number, type, direction)     \
311         IIO_EVENT_CODE(channelclass, 0, number, 0, type, direction)
312
313
314 #define IIO_BUFFER_EVENT_CODE(code)             \
315         (IIO_EV_CLASS_BUFFER | (code << 8))
316
317 #define IIO_EVENT_CODE_EXTRACT_DIR(mask) ((mask >> 24) & 0xf)
318
319 /* Event code number extraction depends on which type of event we have.
320  * Perhaps review this function in the future*/
321 #define IIO_EVENT_CODE_EXTRACT_NUM(mask) ((mask >> 9) & 0x0f)
322
323 #define IIO_EVENT_CODE_EXTRACT_MODIFIER(mask) ((mask >> 13) & 0x7)
324
325 /**
326  * IIO_EVENT_ATTR_RING_50_FULL - ring buffer event to indicate 50% full
327  * @_show: output method for the attribute
328  * @_store: input method for the attribute
329  * @_mask: mask used when detecting the event
330  * @_handler: handler function to be called
331  **/
332 #define IIO_EVENT_ATTR_RING_50_FULL(_show, _store, _mask, _handler)     \
333         IIO_EVENT_ATTR(ring_50_full, _show, _store, _mask, _handler)
334
335 /**
336  * IIO_EVENT_ATTR_RING_50_FULL_SH - shared ring event to indicate 50% full
337  * @_evlist: event handler list
338  * @_show: output method for the attribute
339  * @_store: input method for the attribute
340  * @_mask: mask used when detecting the event
341  **/
342 #define IIO_EVENT_ATTR_RING_50_FULL_SH(_evlist, _show, _store, _mask)   \
343         IIO_EVENT_ATTR_SH(ring_50_full, _evlist, _show, _store, _mask)
344
345 /**
346  * IIO_EVENT_ATTR_RING_75_FULL_SH - shared ring event to indicate 75% full
347  * @_evlist: event handler list
348  * @_show: output method for the attribute
349  * @_store: input method for the attribute
350  * @_mask: mask used when detecting the event
351  **/
352 #define IIO_EVENT_ATTR_RING_75_FULL_SH(_evlist, _show, _store, _mask)   \
353         IIO_EVENT_ATTR_SH(ring_75_full, _evlist, _show, _store, _mask)
354
355 #define IIO_EVENT_CODE_RING_50_FULL     IIO_BUFFER_EVENT_CODE(0)
356 #define IIO_EVENT_CODE_RING_75_FULL     IIO_BUFFER_EVENT_CODE(1)
357 #define IIO_EVENT_CODE_RING_100_FULL    IIO_BUFFER_EVENT_CODE(2)
358
359 #endif /* _INDUSTRIAL_IO_SYSFS_H_ */