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