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