iio.h 29 KB

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