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/align.h> 11 #include <linux/device.h> 12 #include <linux/cdev.h> 13 #include <linux/cleanup.h> 14 #include <linux/compiler_types.h> 15 #include <linux/minmax.h> 16 #include <linux/slab.h> 17 #include <linux/iio/types.h> 18 /* IIO TODO LIST */ 19 /* 20 * Provide means of adjusting timer accuracy. 21 * Currently assumes nano seconds. 22 */ 23 24 struct fwnode_reference_args; 25 26 enum iio_shared_by { 27 IIO_SEPARATE, 28 IIO_SHARED_BY_TYPE, 29 IIO_SHARED_BY_DIR, 30 IIO_SHARED_BY_ALL 31 }; 32 33 enum iio_endian { 34 IIO_CPU, 35 IIO_BE, 36 IIO_LE, 37 }; 38 39 struct iio_chan_spec; 40 struct iio_dev; 41 42 /** 43 * struct iio_chan_spec_ext_info - Extended channel info attribute 44 * @name: Info attribute name 45 * @shared: Whether this attribute is shared between all channels. 46 * @read: Read callback for this info attribute, may be NULL. 47 * @write: Write callback for this info attribute, may be NULL. 48 * @private: Data private to the driver. 49 */ 50 struct iio_chan_spec_ext_info { 51 const char *name; 52 enum iio_shared_by shared; 53 ssize_t (*read)(struct iio_dev *, uintptr_t private, 54 struct iio_chan_spec const *, char *buf); 55 ssize_t (*write)(struct iio_dev *, uintptr_t private, 56 struct iio_chan_spec const *, const char *buf, 57 size_t len); 58 uintptr_t private; 59 }; 60 61 /** 62 * struct iio_enum - Enum channel info attribute 63 * @items: An array of strings. 64 * @num_items: Length of the item array. 65 * @set: Set callback function, may be NULL. 66 * @get: Get callback function, may be NULL. 67 * 68 * The iio_enum struct can be used to implement enum style channel attributes. 69 * Enum style attributes are those which have a set of strings which map to 70 * unsigned integer values. The IIO enum helper code takes care of mapping 71 * between value and string as well as generating a "_available" file which 72 * contains a list of all available items. The set callback will be called when 73 * the attribute is updated. The last parameter is the index to the newly 74 * activated item. The get callback will be used to query the currently active 75 * item and is supposed to return the index for it. 76 */ 77 struct iio_enum { 78 const char * const *items; 79 unsigned int num_items; 80 int (*set)(struct iio_dev *, const struct iio_chan_spec *, unsigned int); 81 int (*get)(struct iio_dev *, const struct iio_chan_spec *); 82 }; 83 84 ssize_t iio_enum_available_read(struct iio_dev *indio_dev, 85 uintptr_t priv, const struct iio_chan_spec *chan, char *buf); 86 ssize_t iio_enum_read(struct iio_dev *indio_dev, 87 uintptr_t priv, const struct iio_chan_spec *chan, char *buf); 88 ssize_t iio_enum_write(struct iio_dev *indio_dev, 89 uintptr_t priv, const struct iio_chan_spec *chan, const char *buf, 90 size_t len); 91 92 /** 93 * IIO_ENUM() - Initialize enum extended channel attribute 94 * @_name: Attribute name 95 * @_shared: Whether the attribute is shared between all channels 96 * @_e: Pointer to an iio_enum struct 97 * 98 * This should usually be used together with IIO_ENUM_AVAILABLE() 99 */ 100 #define IIO_ENUM(_name, _shared, _e) \ 101 { \ 102 .name = (_name), \ 103 .shared = (_shared), \ 104 .read = iio_enum_read, \ 105 .write = iio_enum_write, \ 106 .private = (uintptr_t)(_e), \ 107 } 108 109 /** 110 * IIO_ENUM_AVAILABLE() - Initialize enum available extended channel attribute 111 * @_name: Attribute name ("_available" will be appended to the name) 112 * @_shared: Whether the attribute is shared between all channels 113 * @_e: Pointer to an iio_enum struct 114 * 115 * Creates a read only attribute which lists all the available enum items in a 116 * space separated list. This should usually be used together with IIO_ENUM() 117 */ 118 #define IIO_ENUM_AVAILABLE(_name, _shared, _e) \ 119 { \ 120 .name = (_name "_available"), \ 121 .shared = _shared, \ 122 .read = iio_enum_available_read, \ 123 .private = (uintptr_t)(_e), \ 124 } 125 126 /** 127 * struct iio_mount_matrix - iio mounting matrix 128 * @rotation: 3 dimensional space rotation matrix defining sensor alignment with 129 * main hardware 130 */ 131 struct iio_mount_matrix { 132 const char *rotation[9]; 133 }; 134 135 ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv, 136 const struct iio_chan_spec *chan, char *buf); 137 int iio_read_mount_matrix(struct device *dev, struct iio_mount_matrix *matrix); 138 139 typedef const struct iio_mount_matrix * 140 (iio_get_mount_matrix_t)(const struct iio_dev *indio_dev, 141 const struct iio_chan_spec *chan); 142 143 /** 144 * IIO_MOUNT_MATRIX() - Initialize mount matrix extended channel attribute 145 * @_shared: Whether the attribute is shared between all channels 146 * @_get: Pointer to an iio_get_mount_matrix_t accessor 147 */ 148 #define IIO_MOUNT_MATRIX(_shared, _get) \ 149 { \ 150 .name = "mount_matrix", \ 151 .shared = (_shared), \ 152 .read = iio_show_mount_matrix, \ 153 .private = (uintptr_t)(_get), \ 154 } 155 156 /** 157 * struct iio_event_spec - specification for a channel event 158 * @type: Type of the event 159 * @dir: Direction of the event 160 * @mask_separate: Bit mask of enum iio_event_info values. Attributes 161 * set in this mask will be registered per channel. 162 * @mask_shared_by_type: Bit mask of enum iio_event_info values. Attributes 163 * set in this mask will be shared by channel type. 164 * @mask_shared_by_dir: Bit mask of enum iio_event_info values. Attributes 165 * set in this mask will be shared by channel type and 166 * direction. 167 * @mask_shared_by_all: Bit mask of enum iio_event_info values. Attributes 168 * set in this mask will be shared by all channels. 169 */ 170 struct iio_event_spec { 171 enum iio_event_type type; 172 enum iio_event_direction dir; 173 unsigned long mask_separate; 174 unsigned long mask_shared_by_type; 175 unsigned long mask_shared_by_dir; 176 unsigned long mask_shared_by_all; 177 }; 178 179 /** 180 * define IIO_SCAN_FORMAT_SIGNED_INT - signed integer data format 181 * 182 * &iio_scan_type.format value for signed integers (two's complement). 183 */ 184 #define IIO_SCAN_FORMAT_SIGNED_INT 's' 185 186 /** 187 * define IIO_SCAN_FORMAT_UNSIGNED_INT - unsigned integer data format 188 * 189 * &iio_scan_type.format value for unsigned integers. 190 */ 191 #define IIO_SCAN_FORMAT_UNSIGNED_INT 'u' 192 193 /** 194 * define IIO_SCAN_FORMAT_FLOAT - floating-point data format 195 * 196 * &iio_scan_type.format value for IEEE 754 floating-point numbers. 197 */ 198 #define IIO_SCAN_FORMAT_FLOAT 'f' 199 200 /** 201 * struct iio_scan_type - specification for channel data format in buffer 202 * @sign: Deprecated, use @format instead. 203 * @format: Data format, can have any of the IIO_SCAN_FORMAT_* 204 * values. 205 * @realbits: Number of valid bits of data 206 * @storagebits: Realbits + padding 207 * @shift: Shift right by this before masking out realbits. 208 * @repeat: Number of times real/storage bits repeats. When the 209 * repeat element is more than 1, then the type element in 210 * sysfs will show a repeat value. Otherwise, the number 211 * of repetitions is omitted. 212 * @endianness: little or big endian 213 */ 214 struct iio_scan_type { 215 union { 216 char sign; 217 char format; 218 }; 219 u8 realbits; 220 u8 storagebits; 221 u8 shift; 222 u8 repeat; 223 enum iio_endian endianness; 224 }; 225 226 /** 227 * struct iio_chan_spec - specification of a single channel 228 * @type: What type of measurement is the channel making. 229 * @channel: What number do we wish to assign the channel. 230 * @channel2: If there is a second number for a differential 231 * channel then this is it. If modified is set then the 232 * value here specifies the modifier. 233 * @address: Driver specific identifier. 234 * @scan_index: Monotonic index to give ordering in scans when read 235 * from a buffer. 236 * @scan_type: struct describing the scan type - mutually exclusive 237 * with ext_scan_type. 238 * @ext_scan_type: Used in rare cases where there is more than one scan 239 * format for a channel. When this is used, the flag 240 * has_ext_scan_type must be set and the driver must 241 * implement get_current_scan_type in struct iio_info. 242 * @num_ext_scan_type: Number of elements in ext_scan_type. 243 * @info_mask_separate: What information is to be exported that is specific to 244 * this channel. 245 * @info_mask_separate_available: What availability information is to be 246 * exported that is specific to this channel. 247 * @info_mask_shared_by_type: What information is to be exported that is shared 248 * by all channels of the same type. 249 * @info_mask_shared_by_type_available: What availability information is to be 250 * exported that is shared by all channels of the same 251 * type. 252 * @info_mask_shared_by_dir: What information is to be exported that is shared 253 * by all channels of the same direction. 254 * @info_mask_shared_by_dir_available: What availability information is to be 255 * exported that is shared by all channels of the same 256 * direction. 257 * @info_mask_shared_by_all: What information is to be exported that is shared 258 * by all channels. 259 * @info_mask_shared_by_all_available: What availability information is to be 260 * exported that is shared by all channels. 261 * @event_spec: Array of events which should be registered for this 262 * channel. 263 * @num_event_specs: Size of the event_spec array. 264 * @ext_info: Array of extended info attributes for this channel. 265 * The array is NULL terminated, the last element should 266 * have its name field set to NULL. 267 * @extend_name: Allows labeling of channel attributes with an 268 * informative name. Note this has no effect codes etc, 269 * unlike modifiers. 270 * This field is deprecated in favour of providing 271 * iio_info->read_label() to override the label, which 272 * unlike @extend_name does not affect sysfs filenames. 273 * @datasheet_name: A name used in in-kernel mapping of channels. It should 274 * correspond to the first name that the channel is referred 275 * to by in the datasheet (e.g. IND), or the nearest 276 * possible compound name (e.g. IND-INC). 277 * @modified: Does a modifier apply to this channel. What these are 278 * depends on the channel type. Modifier is set in 279 * channel2. Examples are IIO_MOD_X for axial sensors about 280 * the 'x' axis. 281 * @indexed: Specify the channel has a numerical index. If not, 282 * the channel index number will be suppressed for sysfs 283 * attributes but not for event codes. 284 * @output: Channel is output. 285 * @differential: Channel is differential. 286 * @has_ext_scan_type: True if ext_scan_type is used instead of scan_type. 287 */ 288 struct iio_chan_spec { 289 enum iio_chan_type type; 290 int channel; 291 int channel2; 292 unsigned long address; 293 int scan_index; 294 union { 295 struct iio_scan_type scan_type; 296 struct { 297 const struct iio_scan_type *ext_scan_type; 298 unsigned int num_ext_scan_type; 299 }; 300 }; 301 unsigned long info_mask_separate; 302 unsigned long info_mask_separate_available; 303 unsigned long info_mask_shared_by_type; 304 unsigned long info_mask_shared_by_type_available; 305 unsigned long info_mask_shared_by_dir; 306 unsigned long info_mask_shared_by_dir_available; 307 unsigned long info_mask_shared_by_all; 308 unsigned long info_mask_shared_by_all_available; 309 const struct iio_event_spec *event_spec; 310 unsigned int num_event_specs; 311 const struct iio_chan_spec_ext_info *ext_info; 312 const char *extend_name; 313 const char *datasheet_name; 314 unsigned int modified:1; 315 unsigned int indexed:1; 316 unsigned int output:1; 317 unsigned int differential:1; 318 unsigned int has_ext_scan_type:1; 319 }; 320 321 322 /** 323 * iio_channel_has_info() - Checks whether a channel supports a info attribute 324 * @chan: The channel to be queried 325 * @type: Type of the info attribute to be checked 326 * 327 * Returns true if the channels supports reporting values for the given info 328 * attribute type, false otherwise. 329 */ 330 static inline bool iio_channel_has_info(const struct iio_chan_spec *chan, 331 enum iio_chan_info_enum type) 332 { 333 return (chan->info_mask_separate & BIT(type)) | 334 (chan->info_mask_shared_by_type & BIT(type)) | 335 (chan->info_mask_shared_by_dir & BIT(type)) | 336 (chan->info_mask_shared_by_all & BIT(type)); 337 } 338 339 /** 340 * iio_channel_has_available() - Checks if a channel has an available attribute 341 * @chan: The channel to be queried 342 * @type: Type of the available attribute to be checked 343 * 344 * Returns true if the channel supports reporting available values for the 345 * given attribute type, false otherwise. 346 */ 347 static inline bool iio_channel_has_available(const struct iio_chan_spec *chan, 348 enum iio_chan_info_enum type) 349 { 350 return (chan->info_mask_separate_available & BIT(type)) | 351 (chan->info_mask_shared_by_type_available & BIT(type)) | 352 (chan->info_mask_shared_by_dir_available & BIT(type)) | 353 (chan->info_mask_shared_by_all_available & BIT(type)); 354 } 355 356 #define IIO_CHAN_SOFT_TIMESTAMP(_si) (struct iio_chan_spec) { \ 357 .type = IIO_TIMESTAMP, \ 358 .channel = -1, \ 359 .scan_index = _si, \ 360 .scan_type = { \ 361 .sign = 's', \ 362 .realbits = 64, \ 363 .storagebits = 64, \ 364 }, \ 365 } 366 367 s64 iio_get_time_ns(const struct iio_dev *indio_dev); 368 369 /* 370 * Device operating modes 371 * @INDIO_DIRECT_MODE: There is an access to either: 372 * a) The last single value available for devices that do not provide 373 * on-demand reads. 374 * b) A new value after performing an on-demand read otherwise. 375 * On most devices, this is a single-shot read. On some devices with data 376 * streams without an 'on-demand' function, this might also be the 'last value' 377 * feature. Above all, this mode internally means that we are not in any of the 378 * other modes, and sysfs reads should work. 379 * Device drivers should inform the core if they support this mode. 380 * @INDIO_BUFFER_TRIGGERED: Common mode when dealing with kfifo buffers. 381 * It indicates that an explicit trigger is required. This requests the core to 382 * attach a poll function when enabling the buffer, which is indicated by the 383 * _TRIGGERED suffix. 384 * The core will ensure this mode is set when registering a triggered buffer 385 * with iio_triggered_buffer_setup(). 386 * @INDIO_BUFFER_SOFTWARE: Another kfifo buffer mode, but not event triggered. 387 * No poll function can be attached because there is no triggered infrastructure 388 * we can use to cause capture. There is a kfifo that the driver will fill, but 389 * not "only one scan at a time". Typically, hardware will have a buffer that 390 * can hold multiple scans. Software may read one or more scans at a single time 391 * and push the available data to a Kfifo. This means the core will not attach 392 * any poll function when enabling the buffer. 393 * The core will ensure this mode is set when registering a simple kfifo buffer 394 * with devm_iio_kfifo_buffer_setup(). 395 * @INDIO_BUFFER_HARDWARE: For specific hardware, if unsure do not use this mode. 396 * Same as above but this time the buffer is not a kfifo where we have direct 397 * access to the data. Instead, the consumer driver must access the data through 398 * non software visible channels (or DMA when there is no demux possible in 399 * software) 400 * The core will ensure this mode is set when registering a dmaengine buffer 401 * with devm_iio_dmaengine_buffer_setup(). 402 * @INDIO_EVENT_TRIGGERED: Very unusual mode. 403 * Triggers usually refer to an external event which will start data capture. 404 * Here it is kind of the opposite as, a particular state of the data might 405 * produce an event which can be considered as an event. We don't necessarily 406 * have access to the data itself, but to the event produced. For example, this 407 * can be a threshold detector. The internal path of this mode is very close to 408 * the INDIO_BUFFER_TRIGGERED mode. 409 * The core will ensure this mode is set when registering a triggered event. 410 * @INDIO_HARDWARE_TRIGGERED: Very unusual mode. 411 * Here, triggers can result in data capture and can be routed to multiple 412 * hardware components, which make them close to regular triggers in the way 413 * they must be managed by the core, but without the entire interrupts/poll 414 * functions burden. Interrupts are irrelevant as the data flow is hardware 415 * mediated and distributed. 416 */ 417 #define INDIO_DIRECT_MODE 0x01 418 #define INDIO_BUFFER_TRIGGERED 0x02 419 #define INDIO_BUFFER_SOFTWARE 0x04 420 #define INDIO_BUFFER_HARDWARE 0x08 421 #define INDIO_EVENT_TRIGGERED 0x10 422 #define INDIO_HARDWARE_TRIGGERED 0x20 423 424 #define INDIO_ALL_BUFFER_MODES \ 425 (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE | INDIO_BUFFER_SOFTWARE) 426 427 #define INDIO_ALL_TRIGGERED_MODES \ 428 (INDIO_BUFFER_TRIGGERED \ 429 | INDIO_EVENT_TRIGGERED \ 430 | INDIO_HARDWARE_TRIGGERED) 431 432 #define INDIO_MAX_RAW_ELEMENTS 4 433 434 struct iio_val_int_plus_micro { 435 int integer; 436 int micro; 437 }; 438 439 struct iio_trigger; /* forward declaration */ 440 441 /** 442 * struct iio_info - constant information about device 443 * @event_attrs: event control attributes 444 * @attrs: general purpose device attributes 445 * @read_raw: function to request a value from the device. 446 * mask specifies which value. Note 0 means a reading of 447 * the channel in question. Return value will specify the 448 * type of value returned by the device. val and val2 will 449 * contain the elements making up the returned value. 450 * @read_raw_multi: function to return values from the device. 451 * mask specifies which value. Note 0 means a reading of 452 * the channel in question. Return value will specify the 453 * type of value returned by the device. vals pointer 454 * contain the elements making up the returned value. 455 * max_len specifies maximum number of elements 456 * vals pointer can contain. val_len is used to return 457 * length of valid elements in vals. 458 * @read_avail: function to return the available values from the device. 459 * mask specifies which value. Note 0 means the available 460 * values for the channel in question. Return value 461 * specifies if a IIO_AVAIL_LIST or a IIO_AVAIL_RANGE is 462 * returned in vals. The type of the vals are returned in 463 * type and the number of vals is returned in length. For 464 * ranges, there are always three vals returned; min, step 465 * and max. For lists, all possible values are enumerated. 466 * @write_raw: function to write a value to the device. 467 * Parameters are the same as for read_raw. 468 * @read_label: function to request label name for a specified label, 469 * for better channel identification. 470 * @write_raw_get_fmt: callback function to query the expected 471 * format/precision. If not set by the driver, write_raw 472 * returns IIO_VAL_INT_PLUS_MICRO. 473 * @read_event_config: find out if the event is enabled. 474 * @write_event_config: set if the event is enabled. 475 * @read_event_value: read a configuration value associated with the event. 476 * @write_event_value: write a configuration value for the event. 477 * @read_event_label: function to request label name for a specified label, 478 * for better event identification. 479 * @validate_trigger: function to validate the trigger when the 480 * current trigger gets changed. 481 * @get_current_scan_type: must be implemented by drivers that use ext_scan_type 482 * in the channel spec to return the index of the currently 483 * active ext_scan type for a channel. 484 * @update_scan_mode: function to configure device and scan buffer when 485 * channels have changed 486 * @debugfs_reg_access: function to read or write register value of device 487 * @fwnode_xlate: fwnode based function pointer to obtain channel specifier index. 488 * @hwfifo_set_watermark: function pointer to set the current hardware 489 * fifo watermark level; see hwfifo_* entries in 490 * Documentation/ABI/testing/sysfs-bus-iio for details on 491 * how the hardware fifo operates 492 * @hwfifo_flush_to_buffer: function pointer to flush the samples stored 493 * in the hardware fifo to the device buffer. The driver 494 * should not flush more than count samples. The function 495 * must return the number of samples flushed, 0 if no 496 * samples were flushed or a negative integer if no samples 497 * were flushed and there was an error. 498 **/ 499 struct iio_info { 500 const struct attribute_group *event_attrs; 501 const struct attribute_group *attrs; 502 503 int (*read_raw)(struct iio_dev *indio_dev, 504 struct iio_chan_spec const *chan, 505 int *val, 506 int *val2, 507 long mask); 508 509 int (*read_raw_multi)(struct iio_dev *indio_dev, 510 struct iio_chan_spec const *chan, 511 int max_len, 512 int *vals, 513 int *val_len, 514 long mask); 515 516 int (*read_avail)(struct iio_dev *indio_dev, 517 struct iio_chan_spec const *chan, 518 const int **vals, 519 int *type, 520 int *length, 521 long mask); 522 523 int (*write_raw)(struct iio_dev *indio_dev, 524 struct iio_chan_spec const *chan, 525 int val, 526 int val2, 527 long mask); 528 529 int (*read_label)(struct iio_dev *indio_dev, 530 struct iio_chan_spec const *chan, 531 char *label); 532 533 int (*write_raw_get_fmt)(struct iio_dev *indio_dev, 534 struct iio_chan_spec const *chan, 535 long mask); 536 537 int (*read_event_config)(struct iio_dev *indio_dev, 538 const struct iio_chan_spec *chan, 539 enum iio_event_type type, 540 enum iio_event_direction dir); 541 542 int (*write_event_config)(struct iio_dev *indio_dev, 543 const struct iio_chan_spec *chan, 544 enum iio_event_type type, 545 enum iio_event_direction dir, 546 bool state); 547 548 int (*read_event_value)(struct iio_dev *indio_dev, 549 const struct iio_chan_spec *chan, 550 enum iio_event_type type, 551 enum iio_event_direction dir, 552 enum iio_event_info info, int *val, int *val2); 553 554 int (*write_event_value)(struct iio_dev *indio_dev, 555 const struct iio_chan_spec *chan, 556 enum iio_event_type type, 557 enum iio_event_direction dir, 558 enum iio_event_info info, int val, int val2); 559 560 int (*read_event_label)(struct iio_dev *indio_dev, 561 struct iio_chan_spec const *chan, 562 enum iio_event_type type, 563 enum iio_event_direction dir, 564 char *label); 565 566 int (*validate_trigger)(struct iio_dev *indio_dev, 567 struct iio_trigger *trig); 568 int (*get_current_scan_type)(const struct iio_dev *indio_dev, 569 const struct iio_chan_spec *chan); 570 int (*update_scan_mode)(struct iio_dev *indio_dev, 571 const unsigned long *scan_mask); 572 int (*debugfs_reg_access)(struct iio_dev *indio_dev, 573 unsigned int reg, unsigned int writeval, 574 unsigned int *readval); 575 int (*fwnode_xlate)(struct iio_dev *indio_dev, 576 const struct fwnode_reference_args *iiospec); 577 int (*hwfifo_set_watermark)(struct iio_dev *indio_dev, unsigned int val); 578 int (*hwfifo_flush_to_buffer)(struct iio_dev *indio_dev, 579 unsigned int count); 580 }; 581 582 /** 583 * struct iio_buffer_setup_ops - buffer setup related callbacks 584 * @preenable: [DRIVER] function to run prior to marking buffer enabled 585 * @postenable: [DRIVER] function to run after marking buffer enabled 586 * @predisable: [DRIVER] function to run prior to marking buffer 587 * disabled 588 * @postdisable: [DRIVER] function to run after marking buffer disabled 589 * @validate_scan_mask: [DRIVER] function callback to check whether a given 590 * scan mask is valid for the device. 591 */ 592 struct iio_buffer_setup_ops { 593 int (*preenable)(struct iio_dev *); 594 int (*postenable)(struct iio_dev *); 595 int (*predisable)(struct iio_dev *); 596 int (*postdisable)(struct iio_dev *); 597 bool (*validate_scan_mask)(struct iio_dev *indio_dev, 598 const unsigned long *scan_mask); 599 }; 600 601 /** 602 * struct iio_dev - industrial I/O device 603 * @modes: [DRIVER] bitmask listing all the operating modes 604 * supported by the IIO device. This list should be 605 * initialized before registering the IIO device. It can 606 * also be filed up by the IIO core, as a result of 607 * enabling particular features in the driver 608 * (see iio_triggered_event_setup()). 609 * @dev: [DRIVER] device structure, should be assigned a parent 610 * and owner 611 * @buffer: [DRIVER] any buffer present 612 * @scan_bytes: [INTERN] num bytes captured to be fed to buffer demux 613 * @scan_timestamp_offset: [INTERN] cache of the offset (in bytes) for the 614 * timestamp in the scan buffer 615 * @available_scan_masks: [DRIVER] optional array of allowed bitmasks. Sort the 616 * array in order of preference, the most preferred 617 * masks first. 618 * @masklength: [INTERN] the length of the mask established from 619 * channels 620 * @active_scan_mask: [INTERN] union of all scan masks requested by buffers 621 * @scan_timestamp: [INTERN] set if any buffers have requested timestamp 622 * @trig: [INTERN] current device trigger (buffer modes) 623 * @pollfunc: [DRIVER] function run on trigger being received 624 * @pollfunc_event: [DRIVER] function run on events trigger being received 625 * @channels: [DRIVER] channel specification structure table 626 * @num_channels: [DRIVER] number of channels specified in @channels. 627 * @name: [DRIVER] name of the device. 628 * @label: [DRIVER] unique name to identify which device this is 629 * @info: [DRIVER] callbacks and constant info from driver 630 * @setup_ops: [DRIVER] callbacks to call before and after buffer 631 * enable/disable 632 * @priv: [DRIVER] reference to driver's private information 633 * **MUST** be accessed **ONLY** via iio_priv() helper 634 */ 635 struct iio_dev { 636 int modes; 637 struct device dev; 638 639 struct iio_buffer *buffer; 640 int scan_bytes; 641 unsigned int __private scan_timestamp_offset; 642 643 const unsigned long *available_scan_masks; 644 unsigned int __private masklength; 645 const unsigned long *active_scan_mask; 646 bool __private scan_timestamp; 647 struct iio_trigger *trig; 648 struct iio_poll_func *pollfunc; 649 struct iio_poll_func *pollfunc_event; 650 651 struct iio_chan_spec const *channels; 652 int num_channels; 653 654 const char *name; 655 const char *label; 656 const struct iio_info *info; 657 const struct iio_buffer_setup_ops *setup_ops; 658 659 void *__private priv; 660 }; 661 662 int iio_device_id(struct iio_dev *indio_dev); 663 int iio_device_get_current_mode(struct iio_dev *indio_dev); 664 bool iio_buffer_enabled(struct iio_dev *indio_dev); 665 666 const struct iio_chan_spec 667 *iio_find_channel_from_si(struct iio_dev *indio_dev, int si); 668 /** 669 * iio_device_register() - register a device with the IIO subsystem 670 * @indio_dev: Device structure filled by the device driver 671 **/ 672 #define iio_device_register(indio_dev) \ 673 __iio_device_register((indio_dev), THIS_MODULE) 674 int __iio_device_register(struct iio_dev *indio_dev, struct module *this_mod); 675 void iio_device_unregister(struct iio_dev *indio_dev); 676 /** 677 * devm_iio_device_register - Resource-managed iio_device_register() 678 * @dev: Device to allocate iio_dev for 679 * @indio_dev: Device structure filled by the device driver 680 * 681 * Managed iio_device_register. The IIO device registered with this 682 * function is automatically unregistered on driver detach. This function 683 * calls iio_device_register() internally. Refer to that function for more 684 * information. 685 * 686 * RETURNS: 687 * 0 on success, negative error number on failure. 688 */ 689 #define devm_iio_device_register(dev, indio_dev) \ 690 __devm_iio_device_register((dev), (indio_dev), THIS_MODULE) 691 int __devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev, 692 struct module *this_mod); 693 int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp); 694 695 void __iio_dev_mode_lock(struct iio_dev *indio_dev) __acquires(indio_dev); 696 void __iio_dev_mode_unlock(struct iio_dev *indio_dev) __releases(indio_dev); 697 698 /* 699 * Helper functions that allow claim and release of direct mode 700 * in a fashion that doesn't generate many false positives from sparse. 701 * Note this must remain static inline in the header so that sparse 702 * can see the __acquires() and __releases() annotations. 703 */ 704 705 /** 706 * iio_device_claim_direct() - Keep device in direct mode 707 * @indio_dev: the iio_dev associated with the device 708 * 709 * If the device is in direct mode it is guaranteed to stay 710 * that way until iio_device_release_direct() is called. 711 * 712 * Use with iio_device_release_direct(). 713 * 714 * Returns: true on success, false on failure. 715 */ 716 static inline bool iio_device_claim_direct(struct iio_dev *indio_dev) 717 { 718 __iio_dev_mode_lock(indio_dev); 719 720 if (iio_buffer_enabled(indio_dev)) { 721 __iio_dev_mode_unlock(indio_dev); 722 return false; 723 } 724 725 return true; 726 } 727 728 /** 729 * iio_device_release_direct() - Releases claim on direct mode 730 * @indio_dev: the iio_dev associated with the device 731 * 732 * Release the claim. Device is no longer guaranteed to stay 733 * in direct mode. 734 * 735 * Use with iio_device_claim_direct(). 736 */ 737 #define iio_device_release_direct(indio_dev) __iio_dev_mode_unlock(indio_dev) 738 739 /** 740 * iio_device_try_claim_buffer_mode() - Keep device in buffer mode 741 * @indio_dev: the iio_dev associated with the device 742 * 743 * If the device is in buffer mode it is guaranteed to stay 744 * that way until iio_device_release_buffer_mode() is called. 745 * 746 * Use with iio_device_release_buffer_mode(). 747 * 748 * Returns: true on success, false on failure. 749 */ 750 static inline bool iio_device_try_claim_buffer_mode(struct iio_dev *indio_dev) 751 { 752 __iio_dev_mode_lock(indio_dev); 753 754 if (!iio_buffer_enabled(indio_dev)) { 755 __iio_dev_mode_unlock(indio_dev); 756 return false; 757 } 758 759 return true; 760 } 761 762 /** 763 * iio_device_release_buffer_mode() - releases claim on buffer mode 764 * @indio_dev: the iio_dev associated with the device 765 * 766 * Release the claim. Device is no longer guaranteed to stay 767 * in buffer mode. 768 * 769 * Use with iio_device_try_claim_buffer_mode(). 770 */ 771 #define iio_device_release_buffer_mode(indio_dev) __iio_dev_mode_unlock(indio_dev) 772 773 /* 774 * These classes are not meant to be used directly by drivers (hence the 775 * __priv__ prefix). Instead, documented wrapper macros are provided below to 776 * enforce the use of ACQUIRE() or guard() semantics and avoid the problematic 777 * scoped guard variants. 778 */ 779 DEFINE_GUARD(__priv__iio_dev_mode_lock, struct iio_dev *, 780 __iio_dev_mode_lock(_T), __iio_dev_mode_unlock(_T)); 781 DEFINE_GUARD_COND(__priv__iio_dev_mode_lock, _try_direct, 782 iio_device_claim_direct(_T)); 783 784 /** 785 * IIO_DEV_ACQUIRE_DIRECT_MODE() - Tries to acquire the direct mode lock with 786 * automatic release 787 * @dev: IIO device instance 788 * @claim: Variable identifier to store acquire result 789 * 790 * Tries to acquire the direct mode lock with cleanup ACQUIRE() semantics and 791 * automatically releases it at the end of the scope. It most be always paired 792 * with IIO_DEV_ACQUIRE_ERR(), for example (notice the scope braces):: 793 * 794 * switch() { 795 * case IIO_CHAN_INFO_RAW: { 796 * IIO_DEV_ACQUIRE_DIRECT_MODE(indio_dev, claim); 797 * if (IIO_DEV_ACQUIRE_FAILED(claim)) 798 * return -EBUSY; 799 * 800 * ... 801 * } 802 * case IIO_CHAN_INFO_SCALE: 803 * ... 804 * ... 805 * } 806 * 807 * Context: Can sleep 808 */ 809 #define IIO_DEV_ACQUIRE_DIRECT_MODE(dev, claim) \ 810 ACQUIRE(__priv__iio_dev_mode_lock_try_direct, claim)(dev) 811 812 /** 813 * IIO_DEV_ACQUIRE_FAILED() - ACQUIRE_ERR() wrapper 814 * @claim: The claim variable passed to IIO_DEV_ACQUIRE_*_MODE() 815 * 816 * Return: true if failed to acquire the mode, otherwise false. 817 */ 818 #define IIO_DEV_ACQUIRE_FAILED(claim) \ 819 ACQUIRE_ERR(__priv__iio_dev_mode_lock_try_direct, &(claim)) 820 821 /** 822 * IIO_DEV_GUARD_CURRENT_MODE() - Acquires the mode lock with automatic release 823 * @dev: IIO device instance 824 * 825 * Acquires the mode lock with cleanup guard() semantics. It is usually paired 826 * with iio_buffer_enabled(). 827 * 828 * This should *not* be used to protect internal driver state and it's use in 829 * general is *strongly* discouraged. Use any of the IIO_DEV_ACQUIRE_*_MODE() 830 * variants. 831 * 832 * Context: Can sleep 833 */ 834 #define IIO_DEV_GUARD_CURRENT_MODE(dev) \ 835 guard(__priv__iio_dev_mode_lock)(dev) 836 837 extern const struct bus_type iio_bus_type; 838 839 /** 840 * iio_device_put() - reference counted deallocation of struct device 841 * @indio_dev: IIO device structure containing the device 842 **/ 843 static inline void iio_device_put(struct iio_dev *indio_dev) 844 { 845 if (indio_dev) 846 put_device(&indio_dev->dev); 847 } 848 849 clockid_t iio_device_get_clock(const struct iio_dev *indio_dev); 850 int iio_device_set_clock(struct iio_dev *indio_dev, clockid_t clock_id); 851 852 /** 853 * dev_to_iio_dev() - Get IIO device struct from a device struct 854 * @dev: The device embedded in the IIO device 855 * 856 * Note: The device must be a IIO device, otherwise the result is undefined. 857 */ 858 static inline struct iio_dev *dev_to_iio_dev(struct device *dev) 859 { 860 return container_of(dev, struct iio_dev, dev); 861 } 862 863 /** 864 * iio_device_get() - increment reference count for the device 865 * @indio_dev: IIO device structure 866 * 867 * Returns: The passed IIO device 868 **/ 869 static inline struct iio_dev *iio_device_get(struct iio_dev *indio_dev) 870 { 871 return indio_dev ? dev_to_iio_dev(get_device(&indio_dev->dev)) : NULL; 872 } 873 874 /** 875 * iio_device_set_parent() - assign parent device to the IIO device object 876 * @indio_dev: IIO device structure 877 * @parent: reference to parent device object 878 * 879 * This utility must be called between IIO device allocation 880 * (via devm_iio_device_alloc()) & IIO device registration 881 * (via iio_device_register() and devm_iio_device_register())). 882 * By default, the device allocation will also assign a parent device to 883 * the IIO device object. In cases where devm_iio_device_alloc() is used, 884 * sometimes the parent device must be different than the device used to 885 * manage the allocation. 886 * In that case, this helper should be used to change the parent, hence the 887 * requirement to call this between allocation & registration. 888 **/ 889 static inline void iio_device_set_parent(struct iio_dev *indio_dev, 890 struct device *parent) 891 { 892 indio_dev->dev.parent = parent; 893 } 894 895 /** 896 * iio_device_set_drvdata() - Set device driver data 897 * @indio_dev: IIO device structure 898 * @data: Driver specific data 899 * 900 * Allows to attach an arbitrary pointer to an IIO device, which can later be 901 * retrieved by iio_device_get_drvdata(). 902 */ 903 static inline void iio_device_set_drvdata(struct iio_dev *indio_dev, void *data) 904 { 905 dev_set_drvdata(&indio_dev->dev, data); 906 } 907 908 /** 909 * iio_device_get_drvdata() - Get device driver data 910 * @indio_dev: IIO device structure 911 * 912 * Returns the data previously set with iio_device_set_drvdata() 913 */ 914 static inline void *iio_device_get_drvdata(const struct iio_dev *indio_dev) 915 { 916 return dev_get_drvdata(&indio_dev->dev); 917 } 918 919 /* 920 * Used to ensure the iio_priv() structure is aligned to allow that structure 921 * to in turn include IIO_DMA_MINALIGN'd elements such as buffers which 922 * must not share cachelines with the rest of the structure, thus making 923 * them safe for use with non-coherent DMA. 924 * 925 * A number of drivers also use this on buffers that include a 64-bit timestamp 926 * that is used with iio_push_to_buffers_with_ts(). Therefore, in the case where 927 * DMA alignment is not sufficient for proper timestamp alignment, we align to 928 * 8 bytes instead. 929 */ 930 #define IIO_DMA_MINALIGN MAX(ARCH_DMA_MINALIGN, sizeof(s64)) 931 932 #define __IIO_DECLARE_BUFFER_WITH_TS(type, name, count) \ 933 type name[ALIGN((count), sizeof(s64) / sizeof(type)) + sizeof(s64) / sizeof(type)] 934 935 /** 936 * IIO_DECLARE_BUFFER_WITH_TS() - Declare a buffer with timestamp 937 * @type: element type of the buffer 938 * @name: identifier name of the buffer 939 * @count: number of elements in the buffer 940 * 941 * Declares a buffer that is safe to use with iio_push_to_buffers_with_ts(). In 942 * addition to allocating enough space for @count elements of @type, it also 943 * allocates space for a s64 timestamp at the end of the buffer and ensures 944 * proper alignment of the timestamp. 945 */ 946 #define IIO_DECLARE_BUFFER_WITH_TS(type, name, count) \ 947 __IIO_DECLARE_BUFFER_WITH_TS(type, name, count) __aligned(sizeof(s64)) 948 949 /** 950 * IIO_DECLARE_DMA_BUFFER_WITH_TS() - Declare a DMA-aligned buffer with timestamp 951 * @type: element type of the buffer 952 * @name: identifier name of the buffer 953 * @count: number of elements in the buffer 954 * 955 * Same as IIO_DECLARE_BUFFER_WITH_TS(), but is uses __aligned(IIO_DMA_MINALIGN) 956 * to ensure that the buffer doesn't share cachelines with anything that comes 957 * before it in a struct. This should not be used for stack-allocated buffers 958 * as stack memory cannot generally be used for DMA. 959 */ 960 #define IIO_DECLARE_DMA_BUFFER_WITH_TS(type, name, count) \ 961 __IIO_DECLARE_BUFFER_WITH_TS(type, name, count) __aligned(IIO_DMA_MINALIGN) 962 963 /** 964 * IIO_DECLARE_QUATERNION() - Declare a quaternion element 965 * @type: element type of the individual vectors 966 * @name: identifier name 967 * 968 * Quaternions are a vector composed of 4 elements (W, X, Y, Z). Use this macro 969 * to declare a quaternion element in a struct to ensure proper alignment in 970 * an IIO buffer. 971 */ 972 #define IIO_DECLARE_QUATERNION(type, name) \ 973 type name[4] __aligned(sizeof(type) * 4) 974 975 struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv); 976 977 /* The information at the returned address is guaranteed to be cacheline aligned */ 978 static inline void *iio_priv(const struct iio_dev *indio_dev) 979 { 980 return ACCESS_PRIVATE(indio_dev, priv); 981 } 982 983 void iio_device_free(struct iio_dev *indio_dev); 984 struct iio_dev *devm_iio_device_alloc(struct device *parent, int sizeof_priv); 985 986 #define devm_iio_trigger_alloc(parent, fmt, ...) \ 987 __devm_iio_trigger_alloc((parent), THIS_MODULE, (fmt), ##__VA_ARGS__) 988 __printf(3, 4) 989 struct iio_trigger *__devm_iio_trigger_alloc(struct device *parent, 990 struct module *this_mod, 991 const char *fmt, ...); 992 /** 993 * iio_get_debugfs_dentry() - helper function to get the debugfs_dentry 994 * @indio_dev: IIO device structure for device 995 **/ 996 #if defined(CONFIG_DEBUG_FS) 997 struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev); 998 #else 999 static inline struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev) 1000 { 1001 return NULL; 1002 } 1003 #endif 1004 1005 /** 1006 * iio_device_suspend_triggering() - suspend trigger attached to an iio_dev 1007 * @indio_dev: iio_dev associated with the device that will have triggers suspended 1008 * 1009 * Return 0 if successful, negative otherwise 1010 **/ 1011 int iio_device_suspend_triggering(struct iio_dev *indio_dev); 1012 1013 /** 1014 * iio_device_resume_triggering() - resume trigger attached to an iio_dev 1015 * that was previously suspended with iio_device_suspend_triggering() 1016 * @indio_dev: iio_dev associated with the device that will have triggers resumed 1017 * 1018 * Return 0 if successful, negative otherwise 1019 **/ 1020 int iio_device_resume_triggering(struct iio_dev *indio_dev); 1021 1022 #ifdef CONFIG_ACPI 1023 bool iio_read_acpi_mount_matrix(struct device *dev, 1024 struct iio_mount_matrix *orientation, 1025 char *acpi_method); 1026 const char *iio_get_acpi_device_name_and_data(struct device *dev, const void **data); 1027 #else 1028 static inline bool iio_read_acpi_mount_matrix(struct device *dev, 1029 struct iio_mount_matrix *orientation, 1030 char *acpi_method) 1031 { 1032 return false; 1033 } 1034 static inline const char * 1035 iio_get_acpi_device_name_and_data(struct device *dev, const void **data) 1036 { 1037 return NULL; 1038 } 1039 #endif 1040 static inline const char *iio_get_acpi_device_name(struct device *dev) 1041 { 1042 return iio_get_acpi_device_name_and_data(dev, NULL); 1043 } 1044 1045 /** 1046 * iio_get_current_scan_type - Get the current scan type for a channel 1047 * @indio_dev: the IIO device to get the scan type for 1048 * @chan: the channel to get the scan type for 1049 * 1050 * Most devices only have one scan type per channel and can just access it 1051 * directly without calling this function. Core IIO code and drivers that 1052 * implement ext_scan_type in the channel spec should use this function to 1053 * get the current scan type for a channel. 1054 * 1055 * Returns: the current scan type for the channel or error. 1056 */ 1057 static inline const struct iio_scan_type 1058 *iio_get_current_scan_type(const struct iio_dev *indio_dev, 1059 const struct iio_chan_spec *chan) 1060 { 1061 int ret; 1062 1063 if (chan->has_ext_scan_type) { 1064 ret = indio_dev->info->get_current_scan_type(indio_dev, chan); 1065 if (ret < 0) 1066 return ERR_PTR(ret); 1067 1068 if (ret >= chan->num_ext_scan_type) 1069 return ERR_PTR(-EINVAL); 1070 1071 return &chan->ext_scan_type[ret]; 1072 } 1073 1074 return &chan->scan_type; 1075 } 1076 1077 /** 1078 * iio_get_masklength - Get length of the channels mask 1079 * @indio_dev: the IIO device to get the masklength for 1080 */ 1081 static inline unsigned int iio_get_masklength(const struct iio_dev *indio_dev) 1082 { 1083 return ACCESS_PRIVATE(indio_dev, masklength); 1084 } 1085 1086 int iio_active_scan_mask_index(struct iio_dev *indio_dev); 1087 1088 /** 1089 * iio_for_each_active_channel - Iterated over active channels 1090 * @indio_dev: the IIO device 1091 * @chan: Holds the index of the enabled channel 1092 */ 1093 #define iio_for_each_active_channel(indio_dev, chan) \ 1094 for_each_set_bit((chan), (indio_dev)->active_scan_mask, \ 1095 iio_get_masklength(indio_dev)) 1096 1097 ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals); 1098 1099 int iio_str_to_fixpoint(const char *str, int fract_mult, int *integer, 1100 int *fract); 1101 1102 /** 1103 * IIO_DEGREE_TO_RAD() - Convert degree to rad 1104 * @deg: A value in degree 1105 * 1106 * Returns the given value converted from degree to rad 1107 */ 1108 #define IIO_DEGREE_TO_RAD(deg) (((deg) * 314159ULL + 9000000ULL) / 18000000ULL) 1109 1110 /** 1111 * IIO_RAD_TO_DEGREE() - Convert rad to degree 1112 * @rad: A value in rad 1113 * 1114 * Returns the given value converted from rad to degree 1115 */ 1116 #define IIO_RAD_TO_DEGREE(rad) \ 1117 (((rad) * 18000000ULL + 314159ULL / 2) / 314159ULL) 1118 1119 /** 1120 * IIO_G_TO_M_S_2() - Convert g to meter / second**2 1121 * @g: A value in g 1122 * 1123 * Returns the given value converted from g to meter / second**2 1124 */ 1125 #define IIO_G_TO_M_S_2(g) ((g) * 980665ULL / 100000ULL) 1126 1127 /** 1128 * IIO_M_S_2_TO_G() - Convert meter / second**2 to g 1129 * @ms2: A value in meter / second**2 1130 * 1131 * Returns the given value converted from meter / second**2 to g 1132 */ 1133 #define IIO_M_S_2_TO_G(ms2) (((ms2) * 100000ULL + 980665ULL / 2) / 980665ULL) 1134 1135 #endif /* _INDUSTRIAL_IO_H_ */ 1136