1 /* SPDX-License-Identifier: GPL-2.0-only */
3 /* The industrial I/O core
5 * Copyright (c) 2008 Jonathan Cameron
7 #ifndef _INDUSTRIAL_IO_H_
8 #define _INDUSTRIAL_IO_H_
10 #include <linux/device.h>
11 #include <linux/cdev.h>
12 #include <linux/cleanup.h>
13 #include <linux/slab.h>
14 #include <linux/iio/types.h>
17 * Provide means of adjusting timer accuracy.
18 * Currently assumes nano seconds.
21 struct fwnode_reference_args;
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.
45 * @private: Data private to the driver.
47 struct iio_chan_spec_ext_info {
49 enum iio_shared_by shared;
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,
59 * struct iio_enum - Enum channel info attribute
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.
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.
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 *);
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,
90 * IIO_ENUM() - Initialize enum extended channel attribute
91 * @_name: Attribute name
92 * @_shared: Whether the attribute is shared between all channels
93 * @_e: Pointer to an iio_enum struct
95 * This should usually be used together with IIO_ENUM_AVAILABLE()
97 #define IIO_ENUM(_name, _shared, _e) \
100 .shared = (_shared), \
101 .read = iio_enum_read, \
102 .write = iio_enum_write, \
103 .private = (uintptr_t)(_e), \
107 * IIO_ENUM_AVAILABLE() - Initialize enum available extended channel attribute
108 * @_name: Attribute name ("_available" will be appended to the name)
109 * @_shared: Whether the attribute is shared between all channels
110 * @_e: Pointer to an iio_enum struct
112 * Creates a read only attribute which lists all the available enum items in a
113 * space separated list. This should usually be used together with IIO_ENUM()
115 #define IIO_ENUM_AVAILABLE(_name, _shared, _e) \
117 .name = (_name "_available"), \
119 .read = iio_enum_available_read, \
120 .private = (uintptr_t)(_e), \
124 * struct iio_mount_matrix - iio mounting matrix
125 * @rotation: 3 dimensional space rotation matrix defining sensor alignment with
128 struct iio_mount_matrix {
129 const char *rotation[9];
132 ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv,
133 const struct iio_chan_spec *chan, char *buf);
134 int iio_read_mount_matrix(struct device *dev, struct iio_mount_matrix *matrix);
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);
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
145 #define IIO_MOUNT_MATRIX(_shared, _get) \
147 .name = "mount_matrix", \
148 .shared = (_shared), \
149 .read = iio_show_mount_matrix, \
150 .private = (uintptr_t)(_get), \
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
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.
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;
177 * struct iio_chan_spec - specification of a single channel
178 * @type: What type of measurement is the channel making.
179 * @channel: What number do we wish to assign the channel.
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.
184 * @scan_index: Monotonic index to give ordering in scans when read
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
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
197 * @scan_type.endianness: little or big endian
198 * @info_mask_separate: What information is to be exported that is specific to
200 * @info_mask_separate_available: What availability information is to be
201 * exported that is specific to this channel.
202 * @info_mask_shared_by_type: What information is to be exported that is shared
203 * by all channels of the same type.
204 * @info_mask_shared_by_type_available: What availability information is to be
205 * exported that is shared by all channels of the same
207 * @info_mask_shared_by_dir: What information is to be exported that is shared
208 * by all channels of the same direction.
209 * @info_mask_shared_by_dir_available: What availability information is to be
210 * exported that is shared by all channels of the same
212 * @info_mask_shared_by_all: What information is to be exported that is shared
214 * @info_mask_shared_by_all_available: What availability information is to be
215 * exported that is shared by all channels.
216 * @event_spec: Array of events which should be registered for this
218 * @num_event_specs: Size of the event_spec array.
219 * @ext_info: Array of extended info attributes for this channel.
220 * The array is NULL terminated, the last element should
221 * have its name field set to NULL.
222 * @extend_name: Allows labeling of channel attributes with an
223 * informative name. Note this has no effect codes etc,
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.
228 * @datasheet_name: A name used in in-kernel mapping of channels. It should
229 * correspond to the first name that the channel is referred
230 * to by in the datasheet (e.g. IND), or the nearest
231 * possible compound name (e.g. IND-INC).
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
236 * @indexed: Specify the channel has a numerical index. If not,
237 * the channel index number will be suppressed for sysfs
238 * attributes but not for event codes.
239 * @output: Channel is output.
240 * @differential: Channel is differential.
242 struct iio_chan_spec {
243 enum iio_chan_type type;
246 unsigned long address;
254 enum iio_endian endianness;
256 long info_mask_separate;
257 long info_mask_separate_available;
258 long info_mask_shared_by_type;
259 long info_mask_shared_by_type_available;
260 long info_mask_shared_by_dir;
261 long info_mask_shared_by_dir_available;
262 long info_mask_shared_by_all;
263 long info_mask_shared_by_all_available;
264 const struct iio_event_spec *event_spec;
265 unsigned int num_event_specs;
266 const struct iio_chan_spec_ext_info *ext_info;
267 const char *extend_name;
268 const char *datasheet_name;
272 unsigned differential:1;
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
281 * Returns true if the channels supports reporting values for the given info
282 * attribute type, false otherwise.
284 static inline bool iio_channel_has_info(const struct iio_chan_spec *chan,
285 enum iio_chan_info_enum type)
287 return (chan->info_mask_separate & BIT(type)) |
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));
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
298 * Returns true if the channel supports reporting available values for the
299 * given attribute type, false otherwise.
301 static inline bool iio_channel_has_available(const struct iio_chan_spec *chan,
302 enum iio_chan_info_enum type)
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));
310 #define IIO_CHAN_SOFT_TIMESTAMP(_si) { \
311 .type = IIO_TIMESTAMP, \
321 s64 iio_get_time_ns(const struct iio_dev *indio_dev);
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
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
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
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.
371 #define INDIO_DIRECT_MODE 0x01
372 #define INDIO_BUFFER_TRIGGERED 0x02
373 #define INDIO_BUFFER_SOFTWARE 0x04
374 #define INDIO_BUFFER_HARDWARE 0x08
375 #define INDIO_EVENT_TRIGGERED 0x10
376 #define INDIO_HARDWARE_TRIGGERED 0x20
378 #define INDIO_ALL_BUFFER_MODES \
379 (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE | INDIO_BUFFER_SOFTWARE)
381 #define INDIO_ALL_TRIGGERED_MODES \
382 (INDIO_BUFFER_TRIGGERED \
383 | INDIO_EVENT_TRIGGERED \
384 | INDIO_HARDWARE_TRIGGERED)
386 #define INDIO_MAX_RAW_ELEMENTS 4
388 struct iio_val_int_plus_micro {
393 struct iio_trigger; /* forward declaration */
396 * struct iio_info - constant information about device
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.
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.
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.
420 * @write_raw: function to write a value to the device.
421 * Parameters are the same as for read_raw.
422 * @read_label: function to request label name for a specified label,
423 * for better channel identification.
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.
427 * @read_event_config: find out if the event is enabled.
428 * @write_event_config: set if the event is enabled.
429 * @read_event_value: read a configuration value associated with the event.
430 * @write_event_value: write a configuration value for the event.
431 * @read_event_label: function to request label name for a specified label,
432 * for better event identification.
433 * @validate_trigger: function to validate the trigger when the
434 * current trigger gets changed.
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
438 * @fwnode_xlate: fwnode based function pointer to obtain channel specifier index.
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.
451 const struct attribute_group *event_attrs;
452 const struct attribute_group *attrs;
454 int (*read_raw)(struct iio_dev *indio_dev,
455 struct iio_chan_spec const *chan,
460 int (*read_raw_multi)(struct iio_dev *indio_dev,
461 struct iio_chan_spec const *chan,
467 int (*read_avail)(struct iio_dev *indio_dev,
468 struct iio_chan_spec const *chan,
474 int (*write_raw)(struct iio_dev *indio_dev,
475 struct iio_chan_spec const *chan,
480 int (*read_label)(struct iio_dev *indio_dev,
481 struct iio_chan_spec const *chan,
484 int (*write_raw_get_fmt)(struct iio_dev *indio_dev,
485 struct iio_chan_spec const *chan,
488 int (*read_event_config)(struct iio_dev *indio_dev,
489 const struct iio_chan_spec *chan,
490 enum iio_event_type type,
491 enum iio_event_direction dir);
493 int (*write_event_config)(struct iio_dev *indio_dev,
494 const struct iio_chan_spec *chan,
495 enum iio_event_type type,
496 enum iio_event_direction dir,
499 int (*read_event_value)(struct iio_dev *indio_dev,
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);
505 int (*write_event_value)(struct iio_dev *indio_dev,
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);
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,
517 int (*validate_trigger)(struct iio_dev *indio_dev,
518 struct iio_trigger *trig);
519 int (*update_scan_mode)(struct iio_dev *indio_dev,
520 const unsigned long *scan_mask);
521 int (*debugfs_reg_access)(struct iio_dev *indio_dev,
522 unsigned reg, unsigned writeval,
524 int (*fwnode_xlate)(struct iio_dev *indio_dev,
525 const struct fwnode_reference_args *iiospec);
526 int (*hwfifo_set_watermark)(struct iio_dev *indio_dev, unsigned val);
527 int (*hwfifo_flush_to_buffer)(struct iio_dev *indio_dev,
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
537 * @postdisable: [DRIVER] function to run after marking buffer disabled
538 * @validate_scan_mask: [DRIVER] function callback to check whether a given
539 * scan mask is valid for the device.
541 struct iio_buffer_setup_ops {
542 int (*preenable)(struct iio_dev *);
543 int (*postenable)(struct iio_dev *);
544 int (*predisable)(struct iio_dev *);
545 int (*postdisable)(struct iio_dev *);
546 bool (*validate_scan_mask)(struct iio_dev *indio_dev,
547 const unsigned long *scan_mask);
551 * struct iio_dev - industrial I/O device
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()).
558 * @dev: [DRIVER] device structure, should be assigned a parent
560 * @buffer: [DRIVER] any buffer present
561 * @scan_bytes: [INTERN] num bytes captured to be fed to buffer demux
562 * @available_scan_masks: [DRIVER] optional array of allowed bitmasks. Sort the
563 * array in order of preference, the most preferred
565 * @masklength: [INTERN] the length of the mask established from
567 * @active_scan_mask: [INTERN] union of all scan masks requested by buffers
568 * @scan_timestamp: [INTERN] set if any buffers have requested timestamp
569 * @trig: [INTERN] current device trigger (buffer modes)
570 * @pollfunc: [DRIVER] function run on trigger being received
571 * @pollfunc_event: [DRIVER] function run on events trigger being received
572 * @channels: [DRIVER] channel specification structure table
573 * @num_channels: [DRIVER] number of channels specified in @channels.
574 * @name: [DRIVER] name of the device.
575 * @label: [DRIVER] unique name to identify which device this is
576 * @info: [DRIVER] callbacks and constant info from driver
577 * @setup_ops: [DRIVER] callbacks to call before and after buffer
579 * @priv: [DRIVER] reference to driver's private information
580 * **MUST** be accessed **ONLY** via iio_priv() helper
586 struct iio_buffer *buffer;
589 const unsigned long *available_scan_masks;
591 const unsigned long *active_scan_mask;
593 struct iio_trigger *trig;
594 struct iio_poll_func *pollfunc;
595 struct iio_poll_func *pollfunc_event;
597 struct iio_chan_spec const *channels;
602 const struct iio_info *info;
603 const struct iio_buffer_setup_ops *setup_ops;
608 int iio_device_id(struct iio_dev *indio_dev);
609 int iio_device_get_current_mode(struct iio_dev *indio_dev);
610 bool iio_buffer_enabled(struct iio_dev *indio_dev);
612 const struct iio_chan_spec
613 *iio_find_channel_from_si(struct iio_dev *indio_dev, int si);
615 * iio_device_register() - register a device with the IIO subsystem
616 * @indio_dev: Device structure filled by the device driver
618 #define iio_device_register(indio_dev) \
619 __iio_device_register((indio_dev), THIS_MODULE)
620 int __iio_device_register(struct iio_dev *indio_dev, struct module *this_mod);
621 void iio_device_unregister(struct iio_dev *indio_dev);
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
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
633 * 0 on success, negative error number on failure.
635 #define devm_iio_device_register(dev, indio_dev) \
636 __devm_iio_device_register((dev), (indio_dev), THIS_MODULE)
637 int __devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev,
638 struct module *this_mod);
639 int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp);
640 int iio_device_claim_direct_mode(struct iio_dev *indio_dev);
641 void iio_device_release_direct_mode(struct iio_dev *indio_dev);
644 * This autocleanup logic is normally used via
645 * iio_device_claim_direct_scoped().
647 DEFINE_GUARD(iio_claim_direct, struct iio_dev *, iio_device_claim_direct_mode(_T),
648 iio_device_release_direct_mode(_T))
650 DEFINE_GUARD_COND(iio_claim_direct, _try, ({
652 int d = iio_device_claim_direct_mode(_T);
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
666 #define iio_device_claim_direct_scoped(fail, iio_dev) \
667 scoped_cond_guard(iio_claim_direct_try, fail, iio_dev)
669 int iio_device_claim_buffer_mode(struct iio_dev *indio_dev);
670 void iio_device_release_buffer_mode(struct iio_dev *indio_dev);
672 extern const struct bus_type iio_bus_type;
675 * iio_device_put() - reference counted deallocation of struct device
676 * @indio_dev: IIO device structure containing the device
678 static inline void iio_device_put(struct iio_dev *indio_dev)
681 put_device(&indio_dev->dev);
684 clockid_t iio_device_get_clock(const struct iio_dev *indio_dev);
685 int iio_device_set_clock(struct iio_dev *indio_dev, clockid_t clock_id);
688 * dev_to_iio_dev() - Get IIO device struct from a device struct
689 * @dev: The device embedded in the IIO device
691 * Note: The device must be a IIO device, otherwise the result is undefined.
693 static inline struct iio_dev *dev_to_iio_dev(struct device *dev)
695 return container_of(dev, struct iio_dev, dev);
699 * iio_device_get() - increment reference count for the device
700 * @indio_dev: IIO device structure
702 * Returns: The passed IIO device
704 static inline struct iio_dev *iio_device_get(struct iio_dev *indio_dev)
706 return indio_dev ? dev_to_iio_dev(get_device(&indio_dev->dev)) : NULL;
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
714 * This utility must be called between IIO device allocation
715 * (via devm_iio_device_alloc()) & IIO device registration
716 * (via iio_device_register() and devm_iio_device_register())).
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.
724 static inline void iio_device_set_parent(struct iio_dev *indio_dev,
725 struct device *parent)
727 indio_dev->dev.parent = parent;
731 * iio_device_set_drvdata() - Set device driver data
732 * @indio_dev: IIO device structure
733 * @data: Driver specific data
735 * Allows to attach an arbitrary pointer to an IIO device, which can later be
736 * retrieved by iio_device_get_drvdata().
738 static inline void iio_device_set_drvdata(struct iio_dev *indio_dev, void *data)
740 dev_set_drvdata(&indio_dev->dev, data);
744 * iio_device_get_drvdata() - Get device driver data
745 * @indio_dev: IIO device structure
747 * Returns the data previously set with iio_device_set_drvdata()
749 static inline void *iio_device_get_drvdata(const struct iio_dev *indio_dev)
751 return dev_get_drvdata(&indio_dev->dev);
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.
760 #define IIO_DMA_MINALIGN ARCH_DMA_MINALIGN
761 struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv);
763 /* The information at the returned address is guaranteed to be cacheline aligned */
764 static inline void *iio_priv(const struct iio_dev *indio_dev)
766 return indio_dev->priv;
769 void iio_device_free(struct iio_dev *indio_dev);
770 struct iio_dev *devm_iio_device_alloc(struct device *parent, int sizeof_priv);
772 #define devm_iio_trigger_alloc(parent, fmt, ...) \
773 __devm_iio_trigger_alloc((parent), THIS_MODULE, (fmt), ##__VA_ARGS__)
775 struct iio_trigger *__devm_iio_trigger_alloc(struct device *parent,
776 struct module *this_mod,
777 const char *fmt, ...);
779 * iio_get_debugfs_dentry() - helper function to get the debugfs_dentry
780 * @indio_dev: IIO device structure for device
782 #if defined(CONFIG_DEBUG_FS)
783 struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev);
785 static inline struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
791 ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals);
793 int iio_str_to_fixpoint(const char *str, int fract_mult, int *integer,
797 * IIO_DEGREE_TO_RAD() - Convert degree to rad
798 * @deg: A value in degree
800 * Returns the given value converted from degree to rad
802 #define IIO_DEGREE_TO_RAD(deg) (((deg) * 314159ULL + 9000000ULL) / 18000000ULL)
805 * IIO_RAD_TO_DEGREE() - Convert rad to degree
806 * @rad: A value in rad
808 * Returns the given value converted from rad to degree
810 #define IIO_RAD_TO_DEGREE(rad) \
811 (((rad) * 18000000ULL + 314159ULL / 2) / 314159ULL)
814 * IIO_G_TO_M_S_2() - Convert g to meter / second**2
817 * Returns the given value converted from g to meter / second**2
819 #define IIO_G_TO_M_S_2(g) ((g) * 980665ULL / 100000ULL)
822 * IIO_M_S_2_TO_G() - Convert meter / second**2 to g
823 * @ms2: A value in meter / second**2
825 * Returns the given value converted from meter / second**2 to g
827 #define IIO_M_S_2_TO_G(ms2) (((ms2) * 100000ULL + 980665ULL / 2) / 980665ULL)
829 #endif /* _INDUSTRIAL_IO_H_ */