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