Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
7d438178 | 2 | |
847ec80b JC |
3 | /* The industrial I/O core |
4 | * | |
5 | * Copyright (c) 2008 Jonathan Cameron | |
847ec80b | 6 | */ |
847ec80b JC |
7 | #ifndef _INDUSTRIAL_IO_H_ |
8 | #define _INDUSTRIAL_IO_H_ | |
9 | ||
10 | #include <linux/device.h> | |
11 | #include <linux/cdev.h> | |
06458e27 | 12 | #include <linux/iio/types.h> |
acd82567 | 13 | #include <linux/of.h> |
847ec80b | 14 | /* IIO TODO LIST */ |
751a3700 | 15 | /* |
847ec80b JC |
16 | * Provide means of adjusting timer accuracy. |
17 | * Currently assumes nano seconds. | |
18 | */ | |
19 | ||
3704432f JC |
20 | enum iio_shared_by { |
21 | IIO_SEPARATE, | |
c006ec83 JC |
22 | IIO_SHARED_BY_TYPE, |
23 | IIO_SHARED_BY_DIR, | |
24 | IIO_SHARED_BY_ALL | |
3704432f JC |
25 | }; |
26 | ||
8310b86c JC |
27 | enum iio_endian { |
28 | IIO_CPU, | |
29 | IIO_BE, | |
30 | IIO_LE, | |
31 | }; | |
32 | ||
5f420b42 LPC |
33 | struct iio_chan_spec; |
34 | struct iio_dev; | |
35 | ||
36 | /** | |
37 | * struct iio_chan_spec_ext_info - Extended channel info attribute | |
38 | * @name: Info attribute name | |
39 | * @shared: Whether this attribute is shared between all channels. | |
40 | * @read: Read callback for this info attribute, may be NULL. | |
41 | * @write: Write callback for this info attribute, may be NULL. | |
fc6d1139 | 42 | * @private: Data private to the driver. |
5f420b42 LPC |
43 | */ |
44 | struct iio_chan_spec_ext_info { | |
45 | const char *name; | |
3704432f | 46 | enum iio_shared_by shared; |
fc6d1139 MH |
47 | ssize_t (*read)(struct iio_dev *, uintptr_t private, |
48 | struct iio_chan_spec const *, char *buf); | |
49 | ssize_t (*write)(struct iio_dev *, uintptr_t private, | |
50 | struct iio_chan_spec const *, const char *buf, | |
51 | size_t len); | |
52 | uintptr_t private; | |
5f420b42 LPC |
53 | }; |
54 | ||
5212cc8a LPC |
55 | /** |
56 | * struct iio_enum - Enum channel info attribute | |
7dcd7b60 PM |
57 | * @items: An array of strings. |
58 | * @num_items: Length of the item array. | |
59 | * @set: Set callback function, may be NULL. | |
60 | * @get: Get callback function, may be NULL. | |
5212cc8a LPC |
61 | * |
62 | * The iio_enum struct can be used to implement enum style channel attributes. | |
63 | * Enum style attributes are those which have a set of strings which map to | |
64 | * unsigned integer values. The IIO enum helper code takes care of mapping | |
65 | * between value and string as well as generating a "_available" file which | |
66 | * contains a list of all available items. The set callback will be called when | |
67 | * the attribute is updated. The last parameter is the index to the newly | |
68 | * activated item. The get callback will be used to query the currently active | |
69 | * item and is supposed to return the index for it. | |
70 | */ | |
71 | struct iio_enum { | |
72 | const char * const *items; | |
73 | unsigned int num_items; | |
74 | int (*set)(struct iio_dev *, const struct iio_chan_spec *, unsigned int); | |
75 | int (*get)(struct iio_dev *, const struct iio_chan_spec *); | |
76 | }; | |
77 | ||
78 | ssize_t iio_enum_available_read(struct iio_dev *indio_dev, | |
79 | uintptr_t priv, const struct iio_chan_spec *chan, char *buf); | |
80 | ssize_t iio_enum_read(struct iio_dev *indio_dev, | |
81 | uintptr_t priv, const struct iio_chan_spec *chan, char *buf); | |
82 | ssize_t iio_enum_write(struct iio_dev *indio_dev, | |
83 | uintptr_t priv, const struct iio_chan_spec *chan, const char *buf, | |
84 | size_t len); | |
85 | ||
86 | /** | |
87 | * IIO_ENUM() - Initialize enum extended channel attribute | |
7dcd7b60 PM |
88 | * @_name: Attribute name |
89 | * @_shared: Whether the attribute is shared between all channels | |
d25b3808 | 90 | * @_e: Pointer to an iio_enum struct |
5212cc8a LPC |
91 | * |
92 | * This should usually be used together with IIO_ENUM_AVAILABLE() | |
93 | */ | |
94 | #define IIO_ENUM(_name, _shared, _e) \ | |
95 | { \ | |
96 | .name = (_name), \ | |
97 | .shared = (_shared), \ | |
98 | .read = iio_enum_read, \ | |
99 | .write = iio_enum_write, \ | |
100 | .private = (uintptr_t)(_e), \ | |
101 | } | |
102 | ||
103 | /** | |
104 | * IIO_ENUM_AVAILABLE() - Initialize enum available extended channel attribute | |
7dcd7b60 | 105 | * @_name: Attribute name ("_available" will be appended to the name) |
d25b3808 | 106 | * @_e: Pointer to an iio_enum struct |
5212cc8a | 107 | * |
d25b3808 | 108 | * Creates a read only attribute which lists all the available enum items in a |
5212cc8a LPC |
109 | * space separated list. This should usually be used together with IIO_ENUM() |
110 | */ | |
111 | #define IIO_ENUM_AVAILABLE(_name, _e) \ | |
112 | { \ | |
113 | .name = (_name "_available"), \ | |
3704432f | 114 | .shared = IIO_SHARED_BY_TYPE, \ |
5212cc8a LPC |
115 | .read = iio_enum_available_read, \ |
116 | .private = (uintptr_t)(_e), \ | |
117 | } | |
118 | ||
dfc57732 GB |
119 | /** |
120 | * struct iio_mount_matrix - iio mounting matrix | |
121 | * @rotation: 3 dimensional space rotation matrix defining sensor alignment with | |
122 | * main hardware | |
123 | */ | |
124 | struct iio_mount_matrix { | |
125 | const char *rotation[9]; | |
126 | }; | |
127 | ||
128 | ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv, | |
129 | const struct iio_chan_spec *chan, char *buf); | |
fb158971 AS |
130 | int iio_read_mount_matrix(struct device *dev, const char *propname, |
131 | struct iio_mount_matrix *matrix); | |
dfc57732 GB |
132 | |
133 | typedef const struct iio_mount_matrix * | |
134 | (iio_get_mount_matrix_t)(const struct iio_dev *indio_dev, | |
135 | const struct iio_chan_spec *chan); | |
136 | ||
137 | /** | |
138 | * IIO_MOUNT_MATRIX() - Initialize mount matrix extended channel attribute | |
139 | * @_shared: Whether the attribute is shared between all channels | |
140 | * @_get: Pointer to an iio_get_mount_matrix_t accessor | |
141 | */ | |
142 | #define IIO_MOUNT_MATRIX(_shared, _get) \ | |
143 | { \ | |
144 | .name = "mount_matrix", \ | |
145 | .shared = (_shared), \ | |
146 | .read = iio_show_mount_matrix, \ | |
147 | .private = (uintptr_t)(_get), \ | |
148 | } | |
149 | ||
b4e3ac0a LPC |
150 | /** |
151 | * struct iio_event_spec - specification for a channel event | |
152 | * @type: Type of the event | |
153 | * @dir: Direction of the event | |
154 | * @mask_separate: Bit mask of enum iio_event_info values. Attributes | |
155 | * set in this mask will be registered per channel. | |
156 | * @mask_shared_by_type: Bit mask of enum iio_event_info values. Attributes | |
157 | * set in this mask will be shared by channel type. | |
158 | * @mask_shared_by_dir: Bit mask of enum iio_event_info values. Attributes | |
159 | * set in this mask will be shared by channel type and | |
160 | * direction. | |
161 | * @mask_shared_by_all: Bit mask of enum iio_event_info values. Attributes | |
162 | * set in this mask will be shared by all channels. | |
163 | */ | |
164 | struct iio_event_spec { | |
165 | enum iio_event_type type; | |
166 | enum iio_event_direction dir; | |
167 | unsigned long mask_separate; | |
168 | unsigned long mask_shared_by_type; | |
169 | unsigned long mask_shared_by_dir; | |
170 | unsigned long mask_shared_by_all; | |
171 | }; | |
172 | ||
1d892719 JC |
173 | /** |
174 | * struct iio_chan_spec - specification of a single channel | |
175 | * @type: What type of measurement is the channel making. | |
a16561c6 | 176 | * @channel: What number do we wish to assign the channel. |
1d892719 JC |
177 | * @channel2: If there is a second number for a differential |
178 | * channel then this is it. If modified is set then the | |
179 | * value here specifies the modifier. | |
180 | * @address: Driver specific identifier. | |
7dcd7b60 | 181 | * @scan_index: Monotonic index to give ordering in scans when read |
1d892719 | 182 | * from a buffer. |
04e7b3d7 MCC |
183 | * @scan_type: struct describing the scan type |
184 | * @scan_type.sign: 's' or 'u' to specify signed or unsigned | |
185 | * @scan_type.realbits: Number of valid bits of data | |
186 | * @scan_type.storagebits: Realbits + padding | |
187 | * @scan_type.shift: Shift right by this before masking out | |
188 | * realbits. | |
189 | * @scan_type.repeat: Number of times real/storage bits repeats. | |
190 | * When the repeat element is more than 1, then | |
191 | * the type element in sysfs will show a repeat | |
192 | * value. Otherwise, the number of repetitions | |
193 | * is omitted. | |
194 | * @scan_type.endianness: little or big endian | |
8655cc49 JC |
195 | * @info_mask_separate: What information is to be exported that is specific to |
196 | * this channel. | |
51239600 JC |
197 | * @info_mask_separate_available: What availability information is to be |
198 | * exported that is specific to this channel. | |
8655cc49 | 199 | * @info_mask_shared_by_type: What information is to be exported that is shared |
9761696f | 200 | * by all channels of the same type. |
51239600 JC |
201 | * @info_mask_shared_by_type_available: What availability information is to be |
202 | * exported that is shared by all channels of the same | |
203 | * type. | |
c006ec83 JC |
204 | * @info_mask_shared_by_dir: What information is to be exported that is shared |
205 | * by all channels of the same direction. | |
51239600 JC |
206 | * @info_mask_shared_by_dir_available: What availability information is to be |
207 | * exported that is shared by all channels of the same | |
208 | * direction. | |
c006ec83 JC |
209 | * @info_mask_shared_by_all: What information is to be exported that is shared |
210 | * by all channels. | |
51239600 JC |
211 | * @info_mask_shared_by_all_available: What availability information is to be |
212 | * exported that is shared by all channels. | |
b4e3ac0a LPC |
213 | * @event_spec: Array of events which should be registered for this |
214 | * channel. | |
215 | * @num_event_specs: Size of the event_spec array. | |
5f420b42 LPC |
216 | * @ext_info: Array of extended info attributes for this channel. |
217 | * The array is NULL terminated, the last element should | |
9cc113bc | 218 | * have its name field set to NULL. |
1d892719 JC |
219 | * @extend_name: Allows labeling of channel attributes with an |
220 | * informative name. Note this has no effect codes etc, | |
221 | * unlike modifiers. | |
17879488 | 222 | * @datasheet_name: A name used in in-kernel mapping of channels. It should |
4abf6f8b | 223 | * correspond to the first name that the channel is referred |
6c63dded JC |
224 | * to by in the datasheet (e.g. IND), or the nearest |
225 | * possible compound name (e.g. IND-INC). | |
1d892719 JC |
226 | * @modified: Does a modifier apply to this channel. What these are |
227 | * depends on the channel type. Modifier is set in | |
228 | * channel2. Examples are IIO_MOD_X for axial sensors about | |
229 | * the 'x' axis. | |
230 | * @indexed: Specify the channel has a numerical index. If not, | |
a16561c6 PM |
231 | * the channel index number will be suppressed for sysfs |
232 | * attributes but not for event codes. | |
c3668a0f | 233 | * @output: Channel is output. |
ade7ef7b | 234 | * @differential: Channel is differential. |
1d892719 JC |
235 | */ |
236 | struct iio_chan_spec { | |
237 | enum iio_chan_type type; | |
238 | int channel; | |
239 | int channel2; | |
240 | unsigned long address; | |
241 | int scan_index; | |
242 | struct { | |
243 | char sign; | |
244 | u8 realbits; | |
245 | u8 storagebits; | |
246 | u8 shift; | |
0ee8546a | 247 | u8 repeat; |
8310b86c | 248 | enum iio_endian endianness; |
1d892719 | 249 | } scan_type; |
8655cc49 | 250 | long info_mask_separate; |
51239600 | 251 | long info_mask_separate_available; |
8655cc49 | 252 | long info_mask_shared_by_type; |
51239600 | 253 | long info_mask_shared_by_type_available; |
c006ec83 | 254 | long info_mask_shared_by_dir; |
51239600 | 255 | long info_mask_shared_by_dir_available; |
c006ec83 | 256 | long info_mask_shared_by_all; |
51239600 | 257 | long info_mask_shared_by_all_available; |
b4e3ac0a LPC |
258 | const struct iio_event_spec *event_spec; |
259 | unsigned int num_event_specs; | |
5f420b42 | 260 | const struct iio_chan_spec_ext_info *ext_info; |
344692b1 | 261 | const char *extend_name; |
6c63dded | 262 | const char *datasheet_name; |
1d892719 JC |
263 | unsigned modified:1; |
264 | unsigned indexed:1; | |
c6fc8062 | 265 | unsigned output:1; |
ade7ef7b | 266 | unsigned differential:1; |
1d892719 | 267 | }; |
df9c1c42 | 268 | |
48e44ce0 LPC |
269 | |
270 | /** | |
271 | * iio_channel_has_info() - Checks whether a channel supports a info attribute | |
272 | * @chan: The channel to be queried | |
273 | * @type: Type of the info attribute to be checked | |
274 | * | |
275 | * Returns true if the channels supports reporting values for the given info | |
276 | * attribute type, false otherwise. | |
277 | */ | |
278 | static inline bool iio_channel_has_info(const struct iio_chan_spec *chan, | |
279 | enum iio_chan_info_enum type) | |
280 | { | |
1c297a66 | 281 | return (chan->info_mask_separate & BIT(type)) | |
c006ec83 JC |
282 | (chan->info_mask_shared_by_type & BIT(type)) | |
283 | (chan->info_mask_shared_by_dir & BIT(type)) | | |
284 | (chan->info_mask_shared_by_all & BIT(type)); | |
48e44ce0 LPC |
285 | } |
286 | ||
00c5f80c PR |
287 | /** |
288 | * iio_channel_has_available() - Checks if a channel has an available attribute | |
289 | * @chan: The channel to be queried | |
290 | * @type: Type of the available attribute to be checked | |
291 | * | |
292 | * Returns true if the channel supports reporting available values for the | |
293 | * given attribute type, false otherwise. | |
294 | */ | |
295 | static inline bool iio_channel_has_available(const struct iio_chan_spec *chan, | |
296 | enum iio_chan_info_enum type) | |
297 | { | |
298 | return (chan->info_mask_separate_available & BIT(type)) | | |
299 | (chan->info_mask_shared_by_type_available & BIT(type)) | | |
300 | (chan->info_mask_shared_by_dir_available & BIT(type)) | | |
301 | (chan->info_mask_shared_by_all_available & BIT(type)); | |
302 | } | |
303 | ||
07d4655b JC |
304 | #define IIO_CHAN_SOFT_TIMESTAMP(_si) { \ |
305 | .type = IIO_TIMESTAMP, \ | |
306 | .channel = -1, \ | |
307 | .scan_index = _si, \ | |
308 | .scan_type = { \ | |
309 | .sign = 's', \ | |
310 | .realbits = 64, \ | |
311 | .storagebits = 64, \ | |
312 | }, \ | |
313 | } | |
1d892719 | 314 | |
bc2b7dab GB |
315 | s64 iio_get_time_ns(const struct iio_dev *indio_dev); |
316 | unsigned int iio_get_time_res(const struct iio_dev *indio_dev); | |
847ec80b | 317 | |
847ec80b JC |
318 | /* Device operating modes */ |
319 | #define INDIO_DIRECT_MODE 0x01 | |
ec3afa40 | 320 | #define INDIO_BUFFER_TRIGGERED 0x02 |
03af03ad | 321 | #define INDIO_BUFFER_SOFTWARE 0x04 |
ec3afa40 | 322 | #define INDIO_BUFFER_HARDWARE 0x08 |
735ad074 | 323 | #define INDIO_EVENT_TRIGGERED 0x10 |
d89e119a | 324 | #define INDIO_HARDWARE_TRIGGERED 0x20 |
847ec80b | 325 | |
ec3afa40 | 326 | #define INDIO_ALL_BUFFER_MODES \ |
03af03ad | 327 | (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE | INDIO_BUFFER_SOFTWARE) |
847ec80b | 328 | |
d89e119a BG |
329 | #define INDIO_ALL_TRIGGERED_MODES \ |
330 | (INDIO_BUFFER_TRIGGERED \ | |
331 | | INDIO_EVENT_TRIGGERED \ | |
332 | | INDIO_HARDWARE_TRIGGERED) | |
333 | ||
9fbfb4b3 SP |
334 | #define INDIO_MAX_RAW_ELEMENTS 4 |
335 | ||
43a4360e MH |
336 | struct iio_trigger; /* forward declaration */ |
337 | ||
6fe8135f JC |
338 | /** |
339 | * struct iio_info - constant information about device | |
6fe8135f JC |
340 | * @event_attrs: event control attributes |
341 | * @attrs: general purpose device attributes | |
342 | * @read_raw: function to request a value from the device. | |
343 | * mask specifies which value. Note 0 means a reading of | |
344 | * the channel in question. Return value will specify the | |
345 | * type of value returned by the device. val and val2 will | |
346 | * contain the elements making up the returned value. | |
9fbfb4b3 SP |
347 | * @read_raw_multi: function to return values from the device. |
348 | * mask specifies which value. Note 0 means a reading of | |
349 | * the channel in question. Return value will specify the | |
350 | * type of value returned by the device. vals pointer | |
351 | * contain the elements making up the returned value. | |
352 | * max_len specifies maximum number of elements | |
353 | * vals pointer can contain. val_len is used to return | |
354 | * length of valid elements in vals. | |
51239600 JC |
355 | * @read_avail: function to return the available values from the device. |
356 | * mask specifies which value. Note 0 means the available | |
357 | * values for the channel in question. Return value | |
358 | * specifies if a IIO_AVAIL_LIST or a IIO_AVAIL_RANGE is | |
359 | * returned in vals. The type of the vals are returned in | |
360 | * type and the number of vals is returned in length. For | |
361 | * ranges, there are always three vals returned; min, step | |
362 | * and max. For lists, all possible values are enumerated. | |
6fe8135f JC |
363 | * @write_raw: function to write a value to the device. |
364 | * Parameters are the same as for read_raw. | |
1d4ef9b3 CP |
365 | * @read_label: function to request label name for a specified label, |
366 | * for better channel identification. | |
5c04af04 MH |
367 | * @write_raw_get_fmt: callback function to query the expected |
368 | * format/precision. If not set by the driver, write_raw | |
369 | * returns IIO_VAL_INT_PLUS_MICRO. | |
6fe8135f JC |
370 | * @read_event_config: find out if the event is enabled. |
371 | * @write_event_config: set if the event is enabled. | |
cb955852 LPC |
372 | * @read_event_value: read a configuration value associated with the event. |
373 | * @write_event_value: write a configuration value for the event. | |
43a4360e MH |
374 | * @validate_trigger: function to validate the trigger when the |
375 | * current trigger gets changed. | |
c3668a0f PM |
376 | * @update_scan_mode: function to configure device and scan buffer when |
377 | * channels have changed | |
378 | * @debugfs_reg_access: function to read or write register value of device | |
acd82567 II |
379 | * @of_xlate: function pointer to obtain channel specifier index. |
380 | * When #iio-cells is greater than '0', the driver could | |
381 | * provide a custom of_xlate function that reads the | |
382 | * *args* and returns the appropriate index in registered | |
383 | * IIO channels array. | |
f4f4673b OP |
384 | * @hwfifo_set_watermark: function pointer to set the current hardware |
385 | * fifo watermark level; see hwfifo_* entries in | |
386 | * Documentation/ABI/testing/sysfs-bus-iio for details on | |
387 | * how the hardware fifo operates | |
388 | * @hwfifo_flush_to_buffer: function pointer to flush the samples stored | |
389 | * in the hardware fifo to the device buffer. The driver | |
390 | * should not flush more than count samples. The function | |
391 | * must return the number of samples flushed, 0 if no | |
392 | * samples were flushed or a negative integer if no samples | |
393 | * were flushed and there was an error. | |
6fe8135f JC |
394 | **/ |
395 | struct iio_info { | |
a9a0d64a | 396 | const struct attribute_group *event_attrs; |
6fe8135f JC |
397 | const struct attribute_group *attrs; |
398 | ||
399 | int (*read_raw)(struct iio_dev *indio_dev, | |
400 | struct iio_chan_spec const *chan, | |
401 | int *val, | |
402 | int *val2, | |
403 | long mask); | |
404 | ||
9fbfb4b3 SP |
405 | int (*read_raw_multi)(struct iio_dev *indio_dev, |
406 | struct iio_chan_spec const *chan, | |
407 | int max_len, | |
408 | int *vals, | |
409 | int *val_len, | |
410 | long mask); | |
411 | ||
51239600 JC |
412 | int (*read_avail)(struct iio_dev *indio_dev, |
413 | struct iio_chan_spec const *chan, | |
414 | const int **vals, | |
415 | int *type, | |
416 | int *length, | |
417 | long mask); | |
418 | ||
6fe8135f JC |
419 | int (*write_raw)(struct iio_dev *indio_dev, |
420 | struct iio_chan_spec const *chan, | |
421 | int val, | |
422 | int val2, | |
423 | long mask); | |
424 | ||
1d4ef9b3 CP |
425 | int (*read_label)(struct iio_dev *indio_dev, |
426 | struct iio_chan_spec const *chan, | |
427 | char *label); | |
428 | ||
5c04af04 MH |
429 | int (*write_raw_get_fmt)(struct iio_dev *indio_dev, |
430 | struct iio_chan_spec const *chan, | |
431 | long mask); | |
432 | ||
6fe8135f | 433 | int (*read_event_config)(struct iio_dev *indio_dev, |
b4e3ac0a LPC |
434 | const struct iio_chan_spec *chan, |
435 | enum iio_event_type type, | |
436 | enum iio_event_direction dir); | |
437 | ||
cb955852 | 438 | int (*write_event_config)(struct iio_dev *indio_dev, |
b4e3ac0a LPC |
439 | const struct iio_chan_spec *chan, |
440 | enum iio_event_type type, | |
441 | enum iio_event_direction dir, | |
442 | int state); | |
443 | ||
cb955852 | 444 | int (*read_event_value)(struct iio_dev *indio_dev, |
b4e3ac0a LPC |
445 | const struct iio_chan_spec *chan, |
446 | enum iio_event_type type, | |
447 | enum iio_event_direction dir, | |
448 | enum iio_event_info info, int *val, int *val2); | |
449 | ||
cb955852 | 450 | int (*write_event_value)(struct iio_dev *indio_dev, |
b4e3ac0a LPC |
451 | const struct iio_chan_spec *chan, |
452 | enum iio_event_type type, | |
453 | enum iio_event_direction dir, | |
454 | enum iio_event_info info, int val, int val2); | |
455 | ||
43a4360e MH |
456 | int (*validate_trigger)(struct iio_dev *indio_dev, |
457 | struct iio_trigger *trig); | |
4d5f8d3d JC |
458 | int (*update_scan_mode)(struct iio_dev *indio_dev, |
459 | const unsigned long *scan_mask); | |
e553f182 MH |
460 | int (*debugfs_reg_access)(struct iio_dev *indio_dev, |
461 | unsigned reg, unsigned writeval, | |
462 | unsigned *readval); | |
acd82567 II |
463 | int (*of_xlate)(struct iio_dev *indio_dev, |
464 | const struct of_phandle_args *iiospec); | |
f4f4673b OP |
465 | int (*hwfifo_set_watermark)(struct iio_dev *indio_dev, unsigned val); |
466 | int (*hwfifo_flush_to_buffer)(struct iio_dev *indio_dev, | |
467 | unsigned count); | |
6fe8135f JC |
468 | }; |
469 | ||
1612244f JC |
470 | /** |
471 | * struct iio_buffer_setup_ops - buffer setup related callbacks | |
472 | * @preenable: [DRIVER] function to run prior to marking buffer enabled | |
473 | * @postenable: [DRIVER] function to run after marking buffer enabled | |
474 | * @predisable: [DRIVER] function to run prior to marking buffer | |
475 | * disabled | |
476 | * @postdisable: [DRIVER] function to run after marking buffer disabled | |
939546d1 LPC |
477 | * @validate_scan_mask: [DRIVER] function callback to check whether a given |
478 | * scan mask is valid for the device. | |
1612244f JC |
479 | */ |
480 | struct iio_buffer_setup_ops { | |
99698b45 PM |
481 | int (*preenable)(struct iio_dev *); |
482 | int (*postenable)(struct iio_dev *); | |
483 | int (*predisable)(struct iio_dev *); | |
484 | int (*postdisable)(struct iio_dev *); | |
939546d1 LPC |
485 | bool (*validate_scan_mask)(struct iio_dev *indio_dev, |
486 | const unsigned long *scan_mask); | |
1612244f JC |
487 | }; |
488 | ||
847ec80b JC |
489 | /** |
490 | * struct iio_dev - industrial I/O device | |
491 | * @id: [INTERN] used to identify device internally | |
63b19547 | 492 | * @driver_module: [INTERN] used to make it harder to undercut users |
847ec80b JC |
493 | * @modes: [DRIVER] operating modes supported by device |
494 | * @currentmode: [DRIVER] current operating mode | |
495 | * @dev: [DRIVER] device structure, should be assigned a parent | |
496 | * and owner | |
14555b14 | 497 | * @buffer: [DRIVER] any buffer present |
420fe2e9 | 498 | * @scan_bytes: [INTERN] num bytes captured to be fed to buffer demux |
143324fd | 499 | * @mlock: [INTERN] lock used to prevent simultaneous device state |
847ec80b | 500 | * changes |
e5c003ae | 501 | * @available_scan_masks: [DRIVER] optional array of allowed bitmasks |
32b5eeca JC |
502 | * @masklength: [INTERN] the length of the mask established from |
503 | * channels | |
959d2952 | 504 | * @active_scan_mask: [INTERN] union of all scan masks requested by buffers |
fd6487f8 | 505 | * @scan_timestamp: [INTERN] set if any buffers have requested timestamp |
f1264809 | 506 | * @scan_index_timestamp:[INTERN] cache of the index to the timestamp |
14555b14 | 507 | * @trig: [INTERN] current device trigger (buffer modes) |
f00fd7ae | 508 | * @trig_readonly: [INTERN] mark the current trigger immutable |
25985edc | 509 | * @pollfunc: [DRIVER] function run on trigger being received |
735ad074 | 510 | * @pollfunc_event: [DRIVER] function run on events trigger being received |
1d892719 | 511 | * @channels: [DRIVER] channel specification structure table |
17879488 | 512 | * @num_channels: [DRIVER] number of channels specified in @channels. |
1d892719 | 513 | * @name: [DRIVER] name of the device. |
2c3d0c9f | 514 | * @label: [DRIVER] unique name to identify which device this is |
1a25e592 | 515 | * @info: [DRIVER] callbacks and constant info from driver |
bc2b7dab | 516 | * @clock_id: [INTERN] timestamping clock posix identifier |
ac917a81 | 517 | * @info_exist_lock: [INTERN] lock to prevent use during removal |
ecbf20ca JC |
518 | * @setup_ops: [DRIVER] callbacks to call before and after buffer |
519 | * enable/disable | |
1a25e592 | 520 | * @chrdev: [INTERN] associated character device |
26d25ae3 JC |
521 | * @groups: [INTERN] attribute groups |
522 | * @groupcounter: [INTERN] index of next attribute group | |
bb01443e | 523 | * @flags: [INTERN] file ops related flags including busy flag. |
6d4ebd56 AA |
524 | * @priv: [DRIVER] reference to driver's private information |
525 | * **MUST** be accessed **ONLY** via iio_priv() helper | |
e553f182 | 526 | */ |
847ec80b JC |
527 | struct iio_dev { |
528 | int id; | |
63b19547 | 529 | struct module *driver_module; |
4024bc73 | 530 | |
847ec80b JC |
531 | int modes; |
532 | int currentmode; | |
533 | struct device dev; | |
847ec80b | 534 | |
14555b14 | 535 | struct iio_buffer *buffer; |
420fe2e9 | 536 | int scan_bytes; |
847ec80b JC |
537 | struct mutex mlock; |
538 | ||
cd4361c7 | 539 | const unsigned long *available_scan_masks; |
32b5eeca | 540 | unsigned masklength; |
cd4361c7 | 541 | const unsigned long *active_scan_mask; |
fd6487f8 | 542 | bool scan_timestamp; |
f1264809 | 543 | unsigned scan_index_timestamp; |
847ec80b | 544 | struct iio_trigger *trig; |
c8cdf708 | 545 | bool trig_readonly; |
847ec80b | 546 | struct iio_poll_func *pollfunc; |
735ad074 | 547 | struct iio_poll_func *pollfunc_event; |
1d892719 | 548 | |
1a25e592 JC |
549 | struct iio_chan_spec const *channels; |
550 | int num_channels; | |
1d892719 | 551 | |
1a25e592 | 552 | const char *name; |
2c3d0c9f | 553 | const char *label; |
1a25e592 | 554 | const struct iio_info *info; |
bc2b7dab | 555 | clockid_t clock_id; |
ac917a81 | 556 | struct mutex info_exist_lock; |
1612244f | 557 | const struct iio_buffer_setup_ops *setup_ops; |
1a25e592 | 558 | struct cdev chrdev; |
26d25ae3 JC |
559 | #define IIO_MAX_GROUPS 6 |
560 | const struct attribute_group *groups[IIO_MAX_GROUPS + 1]; | |
561 | int groupcounter; | |
bb01443e LPC |
562 | |
563 | unsigned long flags; | |
6d4ebd56 | 564 | void *priv; |
847ec80b JC |
565 | }; |
566 | ||
5fb21c82 JC |
567 | const struct iio_chan_spec |
568 | *iio_find_channel_from_si(struct iio_dev *indio_dev, int si); | |
63b19547 JC |
569 | /** |
570 | * iio_device_register() - register a device with the IIO subsystem | |
571 | * @indio_dev: Device structure filled by the device driver | |
572 | **/ | |
08e2e51f TH |
573 | #define iio_device_register(indio_dev) \ |
574 | __iio_device_register((indio_dev), THIS_MODULE) | |
63b19547 | 575 | int __iio_device_register(struct iio_dev *indio_dev, struct module *this_mod); |
f8c6f4e9 | 576 | void iio_device_unregister(struct iio_dev *indio_dev); |
63b19547 JC |
577 | /** |
578 | * devm_iio_device_register - Resource-managed iio_device_register() | |
579 | * @dev: Device to allocate iio_dev for | |
580 | * @indio_dev: Device structure filled by the device driver | |
581 | * | |
582 | * Managed iio_device_register. The IIO device registered with this | |
583 | * function is automatically unregistered on driver detach. This function | |
584 | * calls iio_device_register() internally. Refer to that function for more | |
585 | * information. | |
586 | * | |
63b19547 JC |
587 | * RETURNS: |
588 | * 0 on success, negative error number on failure. | |
589 | */ | |
590 | #define devm_iio_device_register(dev, indio_dev) \ | |
a0747914 | 591 | __devm_iio_device_register((dev), (indio_dev), THIS_MODULE) |
63b19547 JC |
592 | int __devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev, |
593 | struct module *this_mod); | |
f8c6f4e9 | 594 | int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp); |
08a33805 AS |
595 | int iio_device_claim_direct_mode(struct iio_dev *indio_dev); |
596 | void iio_device_release_direct_mode(struct iio_dev *indio_dev); | |
847ec80b | 597 | |
5aaaeba8 | 598 | extern struct bus_type iio_bus_type; |
847ec80b JC |
599 | |
600 | /** | |
7cbb7537 | 601 | * iio_device_put() - reference counted deallocation of struct device |
bc2b7dab | 602 | * @indio_dev: IIO device structure containing the device |
847ec80b | 603 | **/ |
7cbb7537 | 604 | static inline void iio_device_put(struct iio_dev *indio_dev) |
847ec80b | 605 | { |
f8c6f4e9 JC |
606 | if (indio_dev) |
607 | put_device(&indio_dev->dev); | |
6898eb89 | 608 | } |
847ec80b | 609 | |
bc2b7dab GB |
610 | /** |
611 | * iio_device_get_clock() - Retrieve current timestamping clock for the device | |
612 | * @indio_dev: IIO device structure containing the device | |
613 | */ | |
614 | static inline clockid_t iio_device_get_clock(const struct iio_dev *indio_dev) | |
615 | { | |
616 | return indio_dev->clock_id; | |
617 | } | |
618 | ||
69f0793e GG |
619 | int iio_device_set_clock(struct iio_dev *indio_dev, clockid_t clock_id); |
620 | ||
7a7913f1 LPC |
621 | /** |
622 | * dev_to_iio_dev() - Get IIO device struct from a device struct | |
7dcd7b60 | 623 | * @dev: The device embedded in the IIO device |
7a7913f1 LPC |
624 | * |
625 | * Note: The device must be a IIO device, otherwise the result is undefined. | |
626 | */ | |
627 | static inline struct iio_dev *dev_to_iio_dev(struct device *dev) | |
628 | { | |
629 | return container_of(dev, struct iio_dev, dev); | |
630 | } | |
631 | ||
e4e8b776 LPC |
632 | /** |
633 | * iio_device_get() - increment reference count for the device | |
7dcd7b60 | 634 | * @indio_dev: IIO device structure |
e4e8b776 LPC |
635 | * |
636 | * Returns: The passed IIO device | |
637 | **/ | |
638 | static inline struct iio_dev *iio_device_get(struct iio_dev *indio_dev) | |
639 | { | |
640 | return indio_dev ? dev_to_iio_dev(get_device(&indio_dev->dev)) : NULL; | |
641 | } | |
642 | ||
f5d01793 AA |
643 | /** |
644 | * iio_device_set_parent() - assign parent device to the IIO device object | |
645 | * @indio_dev: IIO device structure | |
646 | * @parent: reference to parent device object | |
647 | * | |
648 | * This utility must be called between IIO device allocation | |
649 | * (via devm_iio_device_alloc()) & IIO device registration | |
78ff97eb | 650 | * (via iio_device_register() and devm_iio_device_register())). |
f5d01793 AA |
651 | * By default, the device allocation will also assign a parent device to |
652 | * the IIO device object. In cases where devm_iio_device_alloc() is used, | |
653 | * sometimes the parent device must be different than the device used to | |
654 | * manage the allocation. | |
655 | * In that case, this helper should be used to change the parent, hence the | |
656 | * requirement to call this between allocation & registration. | |
657 | **/ | |
658 | static inline void iio_device_set_parent(struct iio_dev *indio_dev, | |
659 | struct device *parent) | |
660 | { | |
661 | indio_dev->dev.parent = parent; | |
662 | } | |
2d66f389 LPC |
663 | |
664 | /** | |
665 | * iio_device_set_drvdata() - Set device driver data | |
666 | * @indio_dev: IIO device structure | |
667 | * @data: Driver specific data | |
668 | * | |
669 | * Allows to attach an arbitrary pointer to an IIO device, which can later be | |
670 | * retrieved by iio_device_get_drvdata(). | |
671 | */ | |
672 | static inline void iio_device_set_drvdata(struct iio_dev *indio_dev, void *data) | |
673 | { | |
674 | dev_set_drvdata(&indio_dev->dev, data); | |
675 | } | |
676 | ||
677 | /** | |
678 | * iio_device_get_drvdata() - Get device driver data | |
679 | * @indio_dev: IIO device structure | |
680 | * | |
681 | * Returns the data previously set with iio_device_set_drvdata() | |
682 | */ | |
18d56385 | 683 | static inline void *iio_device_get_drvdata(const struct iio_dev *indio_dev) |
2d66f389 LPC |
684 | { |
685 | return dev_get_drvdata(&indio_dev->dev); | |
686 | } | |
687 | ||
6f7c8ee5 JC |
688 | /* Can we make this smaller? */ |
689 | #define IIO_ALIGN L1_CACHE_BYTES | |
78289b4a | 690 | struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv); |
6f7c8ee5 | 691 | |
6d4ebd56 | 692 | /* The information at the returned address is guaranteed to be cacheline aligned */ |
f8c6f4e9 | 693 | static inline void *iio_priv(const struct iio_dev *indio_dev) |
6f7c8ee5 | 694 | { |
6d4ebd56 | 695 | return indio_dev->priv; |
6f7c8ee5 JC |
696 | } |
697 | ||
7cbb7537 | 698 | void iio_device_free(struct iio_dev *indio_dev); |
78289b4a | 699 | struct iio_dev *devm_iio_device_alloc(struct device *parent, int sizeof_priv); |
cd7798cb | 700 | __printf(2, 3) |
d536321d | 701 | struct iio_trigger *devm_iio_trigger_alloc(struct device *dev, |
cd7798cb | 702 | const char *fmt, ...); |
847ec80b | 703 | /** |
14555b14 | 704 | * iio_buffer_enabled() - helper function to test if the buffer is enabled |
7dcd7b60 | 705 | * @indio_dev: IIO device structure for device |
847ec80b | 706 | **/ |
f8c6f4e9 | 707 | static inline bool iio_buffer_enabled(struct iio_dev *indio_dev) |
847ec80b | 708 | { |
f8c6f4e9 | 709 | return indio_dev->currentmode |
03af03ad KW |
710 | & (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE | |
711 | INDIO_BUFFER_SOFTWARE); | |
6898eb89 | 712 | } |
847ec80b | 713 | |
e553f182 MH |
714 | /** |
715 | * iio_get_debugfs_dentry() - helper function to get the debugfs_dentry | |
7dcd7b60 | 716 | * @indio_dev: IIO device structure for device |
e553f182 MH |
717 | **/ |
718 | #if defined(CONFIG_DEBUG_FS) | |
96fb1b67 | 719 | struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev); |
e553f182 MH |
720 | #else |
721 | static inline struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev) | |
722 | { | |
723 | return NULL; | |
6898eb89 | 724 | } |
e553f182 MH |
725 | #endif |
726 | ||
7d2c2aca AD |
727 | ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals); |
728 | ||
6807d721 LPC |
729 | int iio_str_to_fixpoint(const char *str, int fract_mult, int *integer, |
730 | int *fract); | |
731 | ||
9dbf8ccd LPC |
732 | /** |
733 | * IIO_DEGREE_TO_RAD() - Convert degree to rad | |
734 | * @deg: A value in degree | |
735 | * | |
736 | * Returns the given value converted from degree to rad | |
737 | */ | |
738 | #define IIO_DEGREE_TO_RAD(deg) (((deg) * 314159ULL + 9000000ULL) / 18000000ULL) | |
739 | ||
c689a923 LPC |
740 | /** |
741 | * IIO_RAD_TO_DEGREE() - Convert rad to degree | |
742 | * @rad: A value in rad | |
743 | * | |
744 | * Returns the given value converted from rad to degree | |
745 | */ | |
746 | #define IIO_RAD_TO_DEGREE(rad) \ | |
747 | (((rad) * 18000000ULL + 314159ULL / 2) / 314159ULL) | |
748 | ||
9dbf8ccd LPC |
749 | /** |
750 | * IIO_G_TO_M_S_2() - Convert g to meter / second**2 | |
751 | * @g: A value in g | |
752 | * | |
753 | * Returns the given value converted from g to meter / second**2 | |
754 | */ | |
755 | #define IIO_G_TO_M_S_2(g) ((g) * 980665ULL / 100000ULL) | |
756 | ||
c689a923 LPC |
757 | /** |
758 | * IIO_M_S_2_TO_G() - Convert meter / second**2 to g | |
759 | * @ms2: A value in meter / second**2 | |
760 | * | |
761 | * Returns the given value converted from meter / second**2 to g | |
762 | */ | |
763 | #define IIO_M_S_2_TO_G(ms2) (((ms2) * 100000ULL + 980665ULL / 2) / 980665ULL) | |
764 | ||
847ec80b | 765 | #endif /* _INDUSTRIAL_IO_H_ */ |