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