GNU Linux-libre 6.9.2-gnu
[releases.git] / include / linux / iio / iio.h
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/cleanup.h>
13 #include <linux/slab.h>
14 #include <linux/iio/types.h>
15 /* IIO TODO LIST */
16 /*
17  * Provide means of adjusting timer accuracy.
18  * Currently assumes nano seconds.
19  */
20
21 struct fwnode_reference_args;
22
23 enum iio_shared_by {
24         IIO_SEPARATE,
25         IIO_SHARED_BY_TYPE,
26         IIO_SHARED_BY_DIR,
27         IIO_SHARED_BY_ALL
28 };
29
30 enum iio_endian {
31         IIO_CPU,
32         IIO_BE,
33         IIO_LE,
34 };
35
36 struct iio_chan_spec;
37 struct iio_dev;
38
39 /**
40  * struct iio_chan_spec_ext_info - Extended channel info attribute
41  * @name:       Info attribute name
42  * @shared:     Whether this attribute is shared between all channels.
43  * @read:       Read callback for this info attribute, may be NULL.
44  * @write:      Write callback for this info attribute, may be NULL.
45  * @private:    Data private to the driver.
46  */
47 struct iio_chan_spec_ext_info {
48         const char *name;
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,
54                          size_t len);
55         uintptr_t private;
56 };
57
58 /**
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.
64  *
65  * The iio_enum struct can be used to implement enum style channel attributes.
66  * Enum style attributes are those which have a set of strings which map to
67  * unsigned integer values. The IIO enum helper code takes care of mapping
68  * between value and string as well as generating a "_available" file which
69  * contains a list of all available items. The set callback will be called when
70  * the attribute is updated. The last parameter is the index to the newly
71  * activated item. The get callback will be used to query the currently active
72  * item and is supposed to return the index for it.
73  */
74 struct iio_enum {
75         const char * const *items;
76         unsigned int num_items;
77         int (*set)(struct iio_dev *, const struct iio_chan_spec *, unsigned int);
78         int (*get)(struct iio_dev *, const struct iio_chan_spec *);
79 };
80
81 ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
82         uintptr_t priv, const struct iio_chan_spec *chan, char *buf);
83 ssize_t iio_enum_read(struct iio_dev *indio_dev,
84         uintptr_t priv, const struct iio_chan_spec *chan, char *buf);
85 ssize_t iio_enum_write(struct iio_dev *indio_dev,
86         uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
87         size_t len);
88
89 /**
90  * IIO_ENUM() - Initialize enum extended channel attribute
91  * @_name:      Attribute name
92  * @_shared:    Whether the attribute is shared between all channels
93  * @_e:         Pointer to an iio_enum struct
94  *
95  * This should usually be used together with IIO_ENUM_AVAILABLE()
96  */
97 #define IIO_ENUM(_name, _shared, _e) \
98 { \
99         .name = (_name), \
100         .shared = (_shared), \
101         .read = iio_enum_read, \
102         .write = iio_enum_write, \
103         .private = (uintptr_t)(_e), \
104 }
105
106 /**
107  * IIO_ENUM_AVAILABLE() - Initialize enum available extended channel attribute
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
111  *
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()
114  */
115 #define IIO_ENUM_AVAILABLE(_name, _shared, _e) \
116 { \
117         .name = (_name "_available"), \
118         .shared = _shared, \
119         .read = iio_enum_available_read, \
120         .private = (uintptr_t)(_e), \
121 }
122
123 /**
124  * struct iio_mount_matrix - iio mounting matrix
125  * @rotation: 3 dimensional space rotation matrix defining sensor alignment with
126  *            main hardware
127  */
128 struct iio_mount_matrix {
129         const char *rotation[9];
130 };
131
132 ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv,
133                               const struct iio_chan_spec *chan, char *buf);
134 int iio_read_mount_matrix(struct device *dev, struct iio_mount_matrix *matrix);
135
136 typedef const struct iio_mount_matrix *
137         (iio_get_mount_matrix_t)(const struct iio_dev *indio_dev,
138                                  const struct iio_chan_spec *chan);
139
140 /**
141  * IIO_MOUNT_MATRIX() - Initialize mount matrix extended channel attribute
142  * @_shared:    Whether the attribute is shared between all channels
143  * @_get:       Pointer to an iio_get_mount_matrix_t accessor
144  */
145 #define IIO_MOUNT_MATRIX(_shared, _get) \
146 { \
147         .name = "mount_matrix", \
148         .shared = (_shared), \
149         .read = iio_show_mount_matrix, \
150         .private = (uintptr_t)(_get), \
151 }
152
153 /**
154  * struct iio_event_spec - specification for a channel event
155  * @type:                   Type of the event
156  * @dir:                    Direction of the event
157  * @mask_separate:          Bit mask of enum iio_event_info values. Attributes
158  *                          set in this mask will be registered per channel.
159  * @mask_shared_by_type:    Bit mask of enum iio_event_info values. Attributes
160  *                          set in this mask will be shared by channel type.
161  * @mask_shared_by_dir:     Bit mask of enum iio_event_info values. Attributes
162  *                          set in this mask will be shared by channel type and
163  *                          direction.
164  * @mask_shared_by_all:     Bit mask of enum iio_event_info values. Attributes
165  *                          set in this mask will be shared by all channels.
166  */
167 struct iio_event_spec {
168         enum iio_event_type type;
169         enum iio_event_direction dir;
170         unsigned long mask_separate;
171         unsigned long mask_shared_by_type;
172         unsigned long mask_shared_by_dir;
173         unsigned long mask_shared_by_all;
174 };
175
176 /**
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
185  *                      from a buffer.
186  * @scan_type:          struct describing the scan type
187  * @scan_type.sign:             's' or 'u' to specify signed or unsigned
188  * @scan_type.realbits:         Number of valid bits of data
189  * @scan_type.storagebits:      Realbits + padding
190  * @scan_type.shift:            Shift right by this before masking out
191  *                              realbits.
192  * @scan_type.repeat:           Number of times real/storage bits repeats.
193  *                              When the repeat element is more than 1, then
194  *                              the type element in sysfs will show a repeat
195  *                              value. Otherwise, the number of repetitions
196  *                              is omitted.
197  * @scan_type.endianness:       little or big endian
198  * @info_mask_separate: What information is to be exported that is specific to
199  *                      this channel.
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
206  *                      type.
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
211  *                      direction.
212  * @info_mask_shared_by_all: What information is to be exported that is shared
213  *                      by all channels.
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
217  *                      channel.
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,
224  *                      unlike modifiers.
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
235  *                      the 'x' axis.
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.
241  */
242 struct iio_chan_spec {
243         enum iio_chan_type      type;
244         int                     channel;
245         int                     channel2;
246         unsigned long           address;
247         int                     scan_index;
248         struct {
249                 char    sign;
250                 u8      realbits;
251                 u8      storagebits;
252                 u8      shift;
253                 u8      repeat;
254                 enum iio_endian endianness;
255         } scan_type;
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;
269         unsigned                modified:1;
270         unsigned                indexed:1;
271         unsigned                output:1;
272         unsigned                differential:1;
273 };
274
275
276 /**
277  * iio_channel_has_info() - Checks whether a channel supports a info attribute
278  * @chan: The channel to be queried
279  * @type: Type of the info attribute to be checked
280  *
281  * Returns true if the channels supports reporting values for the given info
282  * attribute type, false otherwise.
283  */
284 static inline bool iio_channel_has_info(const struct iio_chan_spec *chan,
285         enum iio_chan_info_enum type)
286 {
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));
291 }
292
293 /**
294  * iio_channel_has_available() - Checks if a channel has an available attribute
295  * @chan: The channel to be queried
296  * @type: Type of the available attribute to be checked
297  *
298  * Returns true if the channel supports reporting available values for the
299  * given attribute type, false otherwise.
300  */
301 static inline bool iio_channel_has_available(const struct iio_chan_spec *chan,
302                                              enum iio_chan_info_enum type)
303 {
304         return (chan->info_mask_separate_available & BIT(type)) |
305                 (chan->info_mask_shared_by_type_available & BIT(type)) |
306                 (chan->info_mask_shared_by_dir_available & BIT(type)) |
307                 (chan->info_mask_shared_by_all_available & BIT(type));
308 }
309
310 #define IIO_CHAN_SOFT_TIMESTAMP(_si) {                                  \
311         .type = IIO_TIMESTAMP,                                          \
312         .channel = -1,                                                  \
313         .scan_index = _si,                                              \
314         .scan_type = {                                                  \
315                 .sign = 's',                                            \
316                 .realbits = 64,                                 \
317                 .storagebits = 64,                                      \
318                 },                                                      \
319 }
320
321 s64 iio_get_time_ns(const struct iio_dev *indio_dev);
322
323 /*
324  * Device operating modes
325  * @INDIO_DIRECT_MODE: There is an access to either:
326  * a) The last single value available for devices that do not provide
327  *    on-demand reads.
328  * b) A new value after performing an on-demand read otherwise.
329  * On most devices, this is a single-shot read. On some devices with data
330  * streams without an 'on-demand' function, this might also be the 'last value'
331  * feature. Above all, this mode internally means that we are not in any of the
332  * other modes, and sysfs reads should work.
333  * Device drivers should inform the core if they support this mode.
334  * @INDIO_BUFFER_TRIGGERED: Common mode when dealing with kfifo buffers.
335  * It indicates that an explicit trigger is required. This requests the core to
336  * attach a poll function when enabling the buffer, which is indicated by the
337  * _TRIGGERED suffix.
338  * The core will ensure this mode is set when registering a triggered buffer
339  * with iio_triggered_buffer_setup().
340  * @INDIO_BUFFER_SOFTWARE: Another kfifo buffer mode, but not event triggered.
341  * No poll function can be attached because there is no triggered infrastructure
342  * we can use to cause capture. There is a kfifo that the driver will fill, but
343  * not "only one scan at a time". Typically, hardware will have a buffer that
344  * can hold multiple scans. Software may read one or more scans at a single time
345  * and push the available data to a Kfifo. This means the core will not attach
346  * any poll function when enabling the buffer.
347  * The core will ensure this mode is set when registering a simple kfifo buffer
348  * with devm_iio_kfifo_buffer_setup().
349  * @INDIO_BUFFER_HARDWARE: For specific hardware, if unsure do not use this mode.
350  * Same as above but this time the buffer is not a kfifo where we have direct
351  * access to the data. Instead, the consumer driver must access the data through
352  * non software visible channels (or DMA when there is no demux possible in
353  * software)
354  * The core will ensure this mode is set when registering a dmaengine buffer
355  * with devm_iio_dmaengine_buffer_setup().
356  * @INDIO_EVENT_TRIGGERED: Very unusual mode.
357  * Triggers usually refer to an external event which will start data capture.
358  * Here it is kind of the opposite as, a particular state of the data might
359  * produce an event which can be considered as an event. We don't necessarily
360  * have access to the data itself, but to the event produced. For example, this
361  * can be a threshold detector. The internal path of this mode is very close to
362  * the INDIO_BUFFER_TRIGGERED mode.
363  * The core will ensure this mode is set when registering a triggered event.
364  * @INDIO_HARDWARE_TRIGGERED: Very unusual mode.
365  * Here, triggers can result in data capture and can be routed to multiple
366  * hardware components, which make them close to regular triggers in the way
367  * they must be managed by the core, but without the entire interrupts/poll
368  * functions burden. Interrupts are irrelevant as the data flow is hardware
369  * mediated and distributed.
370  */
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
377
378 #define INDIO_ALL_BUFFER_MODES                                  \
379         (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE | INDIO_BUFFER_SOFTWARE)
380
381 #define INDIO_ALL_TRIGGERED_MODES       \
382         (INDIO_BUFFER_TRIGGERED         \
383          | INDIO_EVENT_TRIGGERED        \
384          | INDIO_HARDWARE_TRIGGERED)
385
386 #define INDIO_MAX_RAW_ELEMENTS          4
387
388 struct iio_val_int_plus_micro {
389         int integer;
390         int micro;
391 };
392
393 struct iio_trigger; /* forward declaration */
394
395 /**
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.
449  **/
450 struct iio_info {
451         const struct attribute_group    *event_attrs;
452         const struct attribute_group    *attrs;
453
454         int (*read_raw)(struct iio_dev *indio_dev,
455                         struct iio_chan_spec const *chan,
456                         int *val,
457                         int *val2,
458                         long mask);
459
460         int (*read_raw_multi)(struct iio_dev *indio_dev,
461                         struct iio_chan_spec const *chan,
462                         int max_len,
463                         int *vals,
464                         int *val_len,
465                         long mask);
466
467         int (*read_avail)(struct iio_dev *indio_dev,
468                           struct iio_chan_spec const *chan,
469                           const int **vals,
470                           int *type,
471                           int *length,
472                           long mask);
473
474         int (*write_raw)(struct iio_dev *indio_dev,
475                          struct iio_chan_spec const *chan,
476                          int val,
477                          int val2,
478                          long mask);
479
480         int (*read_label)(struct iio_dev *indio_dev,
481                          struct iio_chan_spec const *chan,
482                          char *label);
483
484         int (*write_raw_get_fmt)(struct iio_dev *indio_dev,
485                          struct iio_chan_spec const *chan,
486                          long mask);
487
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);
492
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,
497                                   int state);
498
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);
504
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);
510
511         int (*read_event_label)(struct iio_dev *indio_dev,
512                                 struct iio_chan_spec const *chan,
513                                 enum iio_event_type type,
514                                 enum iio_event_direction dir,
515                                 char *label);
516
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,
523                                   unsigned *readval);
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,
528                                       unsigned count);
529 };
530
531 /**
532  * struct iio_buffer_setup_ops - buffer setup related callbacks
533  * @preenable:          [DRIVER] function to run prior to marking buffer enabled
534  * @postenable:         [DRIVER] function to run after marking buffer enabled
535  * @predisable:         [DRIVER] function to run prior to marking buffer
536  *                      disabled
537  * @postdisable:        [DRIVER] function to run after marking buffer disabled
538  * @validate_scan_mask: [DRIVER] function callback to check whether a given
539  *                      scan mask is valid for the device.
540  */
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);
548 };
549
550 /**
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
559  *                      and owner
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
564  *                         masks first.
565  * @masklength:         [INTERN] the length of the mask established from
566  *                      channels
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
578  *                      enable/disable
579  * @priv:               [DRIVER] reference to driver's private information
580  *                      **MUST** be accessed **ONLY** via iio_priv() helper
581  */
582 struct iio_dev {
583         int                             modes;
584         struct device                   dev;
585
586         struct iio_buffer               *buffer;
587         int                             scan_bytes;
588
589         const unsigned long             *available_scan_masks;
590         unsigned                        masklength;
591         const unsigned long             *active_scan_mask;
592         bool                            scan_timestamp;
593         struct iio_trigger              *trig;
594         struct iio_poll_func            *pollfunc;
595         struct iio_poll_func            *pollfunc_event;
596
597         struct iio_chan_spec const      *channels;
598         int                             num_channels;
599
600         const char                      *name;
601         const char                      *label;
602         const struct iio_info           *info;
603         const struct iio_buffer_setup_ops       *setup_ops;
604
605         void                            *priv;
606 };
607
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);
611
612 const struct iio_chan_spec
613 *iio_find_channel_from_si(struct iio_dev *indio_dev, int si);
614 /**
615  * iio_device_register() - register a device with the IIO subsystem
616  * @indio_dev:          Device structure filled by the device driver
617  **/
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);
622 /**
623  * devm_iio_device_register - Resource-managed iio_device_register()
624  * @dev:        Device to allocate iio_dev for
625  * @indio_dev:  Device structure filled by the device driver
626  *
627  * Managed iio_device_register.  The IIO device registered with this
628  * function is automatically unregistered on driver detach. This function
629  * calls iio_device_register() internally. Refer to that function for more
630  * information.
631  *
632  * RETURNS:
633  * 0 on success, negative error number on failure.
634  */
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);
642
643 /*
644  * This autocleanup logic is normally used via
645  * iio_device_claim_direct_scoped().
646  */
647 DEFINE_GUARD(iio_claim_direct, struct iio_dev *, iio_device_claim_direct_mode(_T),
648              iio_device_release_direct_mode(_T))
649
650 DEFINE_GUARD_COND(iio_claim_direct, _try, ({
651                         struct iio_dev *dev;
652                         int d = iio_device_claim_direct_mode(_T);
653
654                         if (d < 0)
655                                 dev = NULL;
656                         else
657                                 dev = _T;
658                         dev;
659                 }))
660
661 /**
662  * iio_device_claim_direct_scoped() - Scoped call to iio_device_claim_direct.
663  * @fail: What to do on failure to claim device.
664  * @iio_dev: Pointer to the IIO devices structure
665  */
666 #define iio_device_claim_direct_scoped(fail, iio_dev) \
667         scoped_cond_guard(iio_claim_direct_try, fail, iio_dev)
668
669 int iio_device_claim_buffer_mode(struct iio_dev *indio_dev);
670 void iio_device_release_buffer_mode(struct iio_dev *indio_dev);
671
672 extern const struct bus_type iio_bus_type;
673
674 /**
675  * iio_device_put() - reference counted deallocation of struct device
676  * @indio_dev: IIO device structure containing the device
677  **/
678 static inline void iio_device_put(struct iio_dev *indio_dev)
679 {
680         if (indio_dev)
681                 put_device(&indio_dev->dev);
682 }
683
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);
686
687 /**
688  * dev_to_iio_dev() - Get IIO device struct from a device struct
689  * @dev:                The device embedded in the IIO device
690  *
691  * Note: The device must be a IIO device, otherwise the result is undefined.
692  */
693 static inline struct iio_dev *dev_to_iio_dev(struct device *dev)
694 {
695         return container_of(dev, struct iio_dev, dev);
696 }
697
698 /**
699  * iio_device_get() - increment reference count for the device
700  * @indio_dev:          IIO device structure
701  *
702  * Returns: The passed IIO device
703  **/
704 static inline struct iio_dev *iio_device_get(struct iio_dev *indio_dev)
705 {
706         return indio_dev ? dev_to_iio_dev(get_device(&indio_dev->dev)) : NULL;
707 }
708
709 /**
710  * iio_device_set_parent() - assign parent device to the IIO device object
711  * @indio_dev:          IIO device structure
712  * @parent:             reference to parent device object
713  *
714  * This utility must be called between IIO device allocation
715  * (via devm_iio_device_alloc()) & IIO device registration
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.
723  **/
724 static inline void iio_device_set_parent(struct iio_dev *indio_dev,
725                                          struct device *parent)
726 {
727         indio_dev->dev.parent = parent;
728 }
729
730 /**
731  * iio_device_set_drvdata() - Set device driver data
732  * @indio_dev: IIO device structure
733  * @data: Driver specific data
734  *
735  * Allows to attach an arbitrary pointer to an IIO device, which can later be
736  * retrieved by iio_device_get_drvdata().
737  */
738 static inline void iio_device_set_drvdata(struct iio_dev *indio_dev, void *data)
739 {
740         dev_set_drvdata(&indio_dev->dev, data);
741 }
742
743 /**
744  * iio_device_get_drvdata() - Get device driver data
745  * @indio_dev: IIO device structure
746  *
747  * Returns the data previously set with iio_device_set_drvdata()
748  */
749 static inline void *iio_device_get_drvdata(const struct iio_dev *indio_dev)
750 {
751         return dev_get_drvdata(&indio_dev->dev);
752 }
753
754 /*
755  * Used to ensure the iio_priv() structure is aligned to allow that structure
756  * to in turn include IIO_DMA_MINALIGN'd elements such as buffers which
757  * must not share  cachelines with the rest of the structure, thus making
758  * them safe for use with non-coherent DMA.
759  */
760 #define IIO_DMA_MINALIGN ARCH_DMA_MINALIGN
761 struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv);
762
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)
765 {
766         return indio_dev->priv;
767 }
768
769 void iio_device_free(struct iio_dev *indio_dev);
770 struct iio_dev *devm_iio_device_alloc(struct device *parent, int sizeof_priv);
771
772 #define devm_iio_trigger_alloc(parent, fmt, ...) \
773         __devm_iio_trigger_alloc((parent), THIS_MODULE, (fmt), ##__VA_ARGS__)
774 __printf(3, 4)
775 struct iio_trigger *__devm_iio_trigger_alloc(struct device *parent,
776                                              struct module *this_mod,
777                                              const char *fmt, ...);
778 /**
779  * iio_get_debugfs_dentry() - helper function to get the debugfs_dentry
780  * @indio_dev:          IIO device structure for device
781  **/
782 #if defined(CONFIG_DEBUG_FS)
783 struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev);
784 #else
785 static inline struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
786 {
787         return NULL;
788 }
789 #endif
790
791 ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals);
792
793 int iio_str_to_fixpoint(const char *str, int fract_mult, int *integer,
794         int *fract);
795
796 /**
797  * IIO_DEGREE_TO_RAD() - Convert degree to rad
798  * @deg: A value in degree
799  *
800  * Returns the given value converted from degree to rad
801  */
802 #define IIO_DEGREE_TO_RAD(deg) (((deg) * 314159ULL + 9000000ULL) / 18000000ULL)
803
804 /**
805  * IIO_RAD_TO_DEGREE() - Convert rad to degree
806  * @rad: A value in rad
807  *
808  * Returns the given value converted from rad to degree
809  */
810 #define IIO_RAD_TO_DEGREE(rad) \
811         (((rad) * 18000000ULL + 314159ULL / 2) / 314159ULL)
812
813 /**
814  * IIO_G_TO_M_S_2() - Convert g to meter / second**2
815  * @g: A value in g
816  *
817  * Returns the given value converted from g to meter / second**2
818  */
819 #define IIO_G_TO_M_S_2(g) ((g) * 980665ULL / 100000ULL)
820
821 /**
822  * IIO_M_S_2_TO_G() - Convert meter / second**2 to g
823  * @ms2: A value in meter / second**2
824  *
825  * Returns the given value converted from meter / second**2 to g
826  */
827 #define IIO_M_S_2_TO_G(ms2) (((ms2) * 100000ULL + 980665ULL / 2) / 980665ULL)
828
829 #endif /* _INDUSTRIAL_IO_H_ */