d8fe0e276036a0b79cfbe4ae14e4840fc8d90e79
[GitHub/mt8127/android_kernel_alcatel_ttab.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_DEV_ATTR_SAMP_FREQ - sets any internal clock frequency
134 * @_mode: sysfs file mode/permissions
135 * @_show: output method for the attribute
136 * @_store: input method for the attribute
137 **/
138 #define IIO_DEV_ATTR_SAMP_FREQ(_mode, _show, _store) \
139 IIO_DEVICE_ATTR(sampling_frequency, _mode, _show, _store, 0)
140
141 /**
142 * IIO_DEV_ATTR_AVAIL_SAMP_FREQ - list available sampling frequencies
143 * @_show: output method for the attribute
144 *
145 * May be mode dependent on some devices
146 **/
147 #define IIO_DEV_ATTR_AVAIL_SAMP_FREQ(_show) \
148 IIO_DEVICE_ATTR(available_sampling_frequency, S_IRUGO, _show, NULL, 0)
149
150 /**
151 * IIO_CONST_ATTR_AVAIL_SAMP_FREQ - list available sampling frequencies
152 * @_string: frequency string for the attribute
153 *
154 * Constant version
155 **/
156 #define IIO_CONST_ATTR_AVAIL_SAMP_FREQ(_string) \
157 IIO_CONST_ATTR(available_sampling_frequency, _string)
158
159 /**
160 * IIO_DEV_ATTR_SCAN_MODE - select a scan mode
161 * @_mode: sysfs file mode/permissions
162 * @_show: output method for the attribute
163 * @_store: input method for the attribute
164 *
165 * This is used when only certain combinations of inputs may be read in one
166 * scan.
167 **/
168 #define IIO_DEV_ATTR_SCAN_MODE(_mode, _show, _store) \
169 IIO_DEVICE_ATTR(scan_mode, _mode, _show, _store, 0)
170
171 /**
172 * IIO_DEV_ATTR_AVAIL_SCAN_MODES - list available scan modes
173 * @_show: output method for the attribute
174 **/
175 #define IIO_DEV_ATTR_AVAIL_SCAN_MODES(_show) \
176 IIO_DEVICE_ATTR(available_scan_modes, S_IRUGO, _show, NULL, 0)
177
178 /**
179 * IIO_DEV_ATTR_SCAN - result of scan of multiple channels
180 * @_show: output method for the attribute
181 **/
182 #define IIO_DEV_ATTR_SCAN(_show) \
183 IIO_DEVICE_ATTR(scan, S_IRUGO, _show, NULL, 0);
184
185 /**
186 * IIO_DEV_ATTR_INPUT - direct read of a single input channel
187 * @_number: input channel number
188 * @_show: output method for the attribute
189 **/
190 #define IIO_DEV_ATTR_INPUT(_number, _show) \
191 IIO_DEVICE_ATTR(in##_number, S_IRUGO, _show, NULL, _number)
192
193 /**
194 * IIO_DEV_ATTR_SW_RING_ENABLE - enable software ring buffer
195 * @_show: output method for the attribute
196 * @_store: input method for the attribute
197 *
198 * Success may be dependent on attachment of trigger previously.
199 **/
200 #define IIO_DEV_ATTR_SW_RING_ENABLE(_show, _store) \
201 IIO_DEVICE_ATTR(sw_ring_enable, S_IRUGO | S_IWUSR, _show, _store, 0)
202
203 /**
204 * IIO_DEV_ATTR_HW_RING_ENABLE - enable hardware ring buffer
205 * @_show: output method for the attribute
206 * @_store: input method for the attribute
207 *
208 * This is a different attribute from the software one as one can envision
209 * schemes where a combination of the two may be used.
210 **/
211 #define IIO_DEV_ATTR_HW_RING_ENABLE(_show, _store) \
212 IIO_DEVICE_ATTR(hw_ring_enable, S_IRUGO | S_IWUSR, _show, _store, 0)
213
214 /**
215 * IIO_DEV_ATTR_BPSE - set number of bits per scan element
216 * @_mode: sysfs file mode/permissions
217 * @_show: output method for the attribute
218 * @_store: input method for the attribute
219 **/
220 #define IIO_DEV_ATTR_BPSE(_mode, _show, _store) \
221 IIO_DEVICE_ATTR(bpse, _mode, _show, _store, 0)
222
223 /**
224 * IIO_DEV_ATTR_BPSE_AVAILABLE - number of bits per scan element supported
225 * @_show: output method for the attribute
226 **/
227 #define IIO_DEV_ATTR_BPSE_AVAILABLE(_show) \
228 IIO_DEVICE_ATTR(bpse_available, S_IRUGO, _show, NULL, 0)
229
230 /**
231 * IIO_DEV_ATTR_TEMP - many sensors have auxiliary temperature sensors
232 * @_show: output method for the attribute
233 **/
234 #define IIO_DEV_ATTR_TEMP(_show) \
235 IIO_DEVICE_ATTR(temp, S_IRUGO, _show, NULL, 0)
236
237 /**
238 * IIO_EVENT_SH - generic shared event handler
239 * @_name: event name
240 * @_handler: handler function to be called
241 *
242 * This is used in cases where more than one event may result from a single
243 * handler. Often the case that some alarm register must be read and multiple
244 * alarms may have been triggered.
245 **/
246 #define IIO_EVENT_SH(_name, _handler) \
247 static struct iio_event_handler_list \
248 iio_event_##_name = { \
249 .handler = _handler, \
250 .refcount = 0, \
251 .exist_lock = __MUTEX_INITIALIZER(iio_event_##_name \
252 .exist_lock), \
253 .list = { \
254 .next = &iio_event_##_name.list, \
255 .prev = &iio_event_##_name.list, \
256 }, \
257 };
258
259 /**
260 * IIO_EVENT_ATTR_SH - generic shared event attribute
261 * @_name: event name
262 * @_ev_list: event handler list
263 * @_show: output method for the attribute
264 * @_store: input method for the attribute
265 * @_mask: mask used when detecting the event
266 *
267 * An attribute with an associated IIO_EVENT_SH
268 **/
269 #define IIO_EVENT_ATTR_SH(_name, _ev_list, _show, _store, _mask) \
270 static struct iio_event_attr \
271 iio_event_attr_##_name \
272 = { .dev_attr = __ATTR(_name, S_IRUGO | S_IWUSR, \
273 _show, _store), \
274 .mask = _mask, \
275 .listel = &_ev_list };
276
277 /**
278 * IIO_EVENT_ATTR - non-shared event attribute
279 * @_name: event name
280 * @_show: output method for the attribute
281 * @_store: input method for the attribute
282 * @_mask: mask used when detecting the event
283 * @_handler: handler function to be called
284 **/
285 #define IIO_EVENT_ATTR(_name, _show, _store, _mask, _handler) \
286 static struct iio_event_handler_list \
287 iio_event_##_name = { \
288 .handler = _handler, \
289 }; \
290 static struct \
291 iio_event_attr \
292 iio_event_attr_##_name \
293 = { .dev_attr = __ATTR(_name, S_IRUGO | S_IWUSR, \
294 _show, _store), \
295 .mask = _mask, \
296 .listel = &iio_event_##_name }; \
297
298 /**
299 * IIO_EVENT_ATTR_DATA_RDY - event driven by data ready signal
300 * @_show: output method for the attribute
301 * @_store: input method for the attribute
302 * @_mask: mask used when detecting the event
303 * @_handler: handler function to be called
304 *
305 * Not typically implemented in devices where full triggering support
306 * has been implemented.
307 **/
308 #define IIO_EVENT_ATTR_DATA_RDY(_show, _store, _mask, _handler) \
309 IIO_EVENT_ATTR(data_rdy, _show, _store, _mask, _handler)
310
311 #define IIO_EVENT_CODE_DATA_RDY 100
312 #define IIO_EVENT_CODE_RING_BASE 200
313 #define IIO_EVENT_CODE_ACCEL_BASE 300
314 #define IIO_EVENT_CODE_GYRO_BASE 400
315 #define IIO_EVENT_CODE_ADC_BASE 500
316 #define IIO_EVENT_CODE_MISC_BASE 600
317
318 #define IIO_EVENT_CODE_DEVICE_SPECIFIC 1000
319
320 /**
321 * IIO_EVENT_ATTR_RING_50_FULL - ring buffer event to indicate 50% full
322 * @_show: output method for the attribute
323 * @_store: input method for the attribute
324 * @_mask: mask used when detecting the event
325 * @_handler: handler function to be called
326 **/
327 #define IIO_EVENT_ATTR_RING_50_FULL(_show, _store, _mask, _handler) \
328 IIO_EVENT_ATTR(ring_50_full, _show, _store, _mask, _handler)
329
330 /**
331 * IIO_EVENT_ATTR_RING_50_FULL_SH - shared ring event to indicate 50% full
332 * @_evlist: event handler list
333 * @_show: output method for the attribute
334 * @_store: input method for the attribute
335 * @_mask: mask used when detecting the event
336 **/
337 #define IIO_EVENT_ATTR_RING_50_FULL_SH(_evlist, _show, _store, _mask) \
338 IIO_EVENT_ATTR_SH(ring_50_full, _evlist, _show, _store, _mask)
339
340 /**
341 * IIO_EVENT_ATTR_RING_75_FULL_SH - shared ring event to indicate 75% full
342 * @_evlist: event handler list
343 * @_show: output method for the attribute
344 * @_store: input method for the attribute
345 * @_mask: mask used when detecting the event
346 **/
347 #define IIO_EVENT_ATTR_RING_75_FULL_SH(_evlist, _show, _store, _mask) \
348 IIO_EVENT_ATTR_SH(ring_75_full, _evlist, _show, _store, _mask)
349
350 #define IIO_EVENT_CODE_RING_50_FULL IIO_EVENT_CODE_RING_BASE
351 #define IIO_EVENT_CODE_RING_75_FULL (IIO_EVENT_CODE_RING_BASE + 1)
352 #define IIO_EVENT_CODE_RING_100_FULL (IIO_EVENT_CODE_RING_BASE + 2)
353
354 #endif /* _INDUSTRIAL_IO_SYSFS_H_ */