iio.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /* The industrial I/O core
  2. *
  3. * Copyright (c) 2008 Jonathan Cameron
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. */
  9. #ifndef _INDUSTRIAL_IO_H_
  10. #define _INDUSTRIAL_IO_H_
  11. #include <linux/device.h>
  12. #include <linux/cdev.h>
  13. #include <linux/iio/types.h>
  14. #include <linux/of.h>
  15. /* IIO TODO LIST */
  16. /*
  17. * Provide means of adjusting timer accuracy.
  18. * Currently assumes nano seconds.
  19. */
  20. enum iio_chan_info_enum {
  21. IIO_CHAN_INFO_RAW = 0,
  22. IIO_CHAN_INFO_PROCESSED,
  23. IIO_CHAN_INFO_SCALE,
  24. IIO_CHAN_INFO_OFFSET,
  25. IIO_CHAN_INFO_CALIBSCALE,
  26. IIO_CHAN_INFO_CALIBBIAS,
  27. IIO_CHAN_INFO_PEAK,
  28. IIO_CHAN_INFO_PEAK_SCALE,
  29. IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW,
  30. IIO_CHAN_INFO_AVERAGE_RAW,
  31. IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY,
  32. IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY,
  33. IIO_CHAN_INFO_SAMP_FREQ,
  34. IIO_CHAN_INFO_FREQUENCY,
  35. IIO_CHAN_INFO_PHASE,
  36. IIO_CHAN_INFO_HARDWAREGAIN,
  37. IIO_CHAN_INFO_HYSTERESIS,
  38. IIO_CHAN_INFO_INT_TIME,
  39. IIO_CHAN_INFO_ENABLE,
  40. IIO_CHAN_INFO_CALIBHEIGHT,
  41. IIO_CHAN_INFO_CALIBWEIGHT,
  42. IIO_CHAN_INFO_DEBOUNCE_COUNT,
  43. IIO_CHAN_INFO_DEBOUNCE_TIME,
  44. IIO_CHAN_INFO_CALIBEMISSIVITY,
  45. IIO_CHAN_INFO_OVERSAMPLING_RATIO,
  46. };
  47. enum iio_shared_by {
  48. IIO_SEPARATE,
  49. IIO_SHARED_BY_TYPE,
  50. IIO_SHARED_BY_DIR,
  51. IIO_SHARED_BY_ALL
  52. };
  53. enum iio_endian {
  54. IIO_CPU,
  55. IIO_BE,
  56. IIO_LE,
  57. };
  58. struct iio_chan_spec;
  59. struct iio_dev;
  60. /**
  61. * struct iio_chan_spec_ext_info - Extended channel info attribute
  62. * @name: Info attribute name
  63. * @shared: Whether this attribute is shared between all channels.
  64. * @read: Read callback for this info attribute, may be NULL.
  65. * @write: Write callback for this info attribute, may be NULL.
  66. * @private: Data private to the driver.
  67. */
  68. struct iio_chan_spec_ext_info {
  69. const char *name;
  70. enum iio_shared_by shared;
  71. ssize_t (*read)(struct iio_dev *, uintptr_t private,
  72. struct iio_chan_spec const *, char *buf);
  73. ssize_t (*write)(struct iio_dev *, uintptr_t private,
  74. struct iio_chan_spec const *, const char *buf,
  75. size_t len);
  76. uintptr_t private;
  77. };
  78. /**
  79. * struct iio_enum - Enum channel info attribute
  80. * @items: An array of strings.
  81. * @num_items: Length of the item array.
  82. * @set: Set callback function, may be NULL.
  83. * @get: Get callback function, may be NULL.
  84. *
  85. * The iio_enum struct can be used to implement enum style channel attributes.
  86. * Enum style attributes are those which have a set of strings which map to
  87. * unsigned integer values. The IIO enum helper code takes care of mapping
  88. * between value and string as well as generating a "_available" file which
  89. * contains a list of all available items. The set callback will be called when
  90. * the attribute is updated. The last parameter is the index to the newly
  91. * activated item. The get callback will be used to query the currently active
  92. * item and is supposed to return the index for it.
  93. */
  94. struct iio_enum {
  95. const char * const *items;
  96. unsigned int num_items;
  97. int (*set)(struct iio_dev *, const struct iio_chan_spec *, unsigned int);
  98. int (*get)(struct iio_dev *, const struct iio_chan_spec *);
  99. };
  100. ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
  101. uintptr_t priv, const struct iio_chan_spec *chan, char *buf);
  102. ssize_t iio_enum_read(struct iio_dev *indio_dev,
  103. uintptr_t priv, const struct iio_chan_spec *chan, char *buf);
  104. ssize_t iio_enum_write(struct iio_dev *indio_dev,
  105. uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
  106. size_t len);
  107. /**
  108. * IIO_ENUM() - Initialize enum extended channel attribute
  109. * @_name: Attribute name
  110. * @_shared: Whether the attribute is shared between all channels
  111. * @_e: Pointer to an iio_enum struct
  112. *
  113. * This should usually be used together with IIO_ENUM_AVAILABLE()
  114. */
  115. #define IIO_ENUM(_name, _shared, _e) \
  116. { \
  117. .name = (_name), \
  118. .shared = (_shared), \
  119. .read = iio_enum_read, \
  120. .write = iio_enum_write, \
  121. .private = (uintptr_t)(_e), \
  122. }
  123. /**
  124. * IIO_ENUM_AVAILABLE() - Initialize enum available extended channel attribute
  125. * @_name: Attribute name ("_available" will be appended to the name)
  126. * @_e: Pointer to an iio_enum struct
  127. *
  128. * Creates a read only attribute which lists all the available enum items in a
  129. * space separated list. This should usually be used together with IIO_ENUM()
  130. */
  131. #define IIO_ENUM_AVAILABLE(_name, _e) \
  132. { \
  133. .name = (_name "_available"), \
  134. .shared = IIO_SHARED_BY_TYPE, \
  135. .read = iio_enum_available_read, \
  136. .private = (uintptr_t)(_e), \
  137. }
  138. /**
  139. * struct iio_mount_matrix - iio mounting matrix
  140. * @rotation: 3 dimensional space rotation matrix defining sensor alignment with
  141. * main hardware
  142. */
  143. struct iio_mount_matrix {
  144. const char *rotation[9];
  145. };
  146. ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv,
  147. const struct iio_chan_spec *chan, char *buf);
  148. int of_iio_read_mount_matrix(const struct device *dev, const char *propname,
  149. struct iio_mount_matrix *matrix);
  150. typedef const struct iio_mount_matrix *
  151. (iio_get_mount_matrix_t)(const struct iio_dev *indio_dev,
  152. const struct iio_chan_spec *chan);
  153. /**
  154. * IIO_MOUNT_MATRIX() - Initialize mount matrix extended channel attribute
  155. * @_shared: Whether the attribute is shared between all channels
  156. * @_get: Pointer to an iio_get_mount_matrix_t accessor
  157. */
  158. #define IIO_MOUNT_MATRIX(_shared, _get) \
  159. { \
  160. .name = "mount_matrix", \
  161. .shared = (_shared), \
  162. .read = iio_show_mount_matrix, \
  163. .private = (uintptr_t)(_get), \
  164. }
  165. /**
  166. * struct iio_event_spec - specification for a channel event
  167. * @type: Type of the event
  168. * @dir: Direction of the event
  169. * @mask_separate: Bit mask of enum iio_event_info values. Attributes
  170. * set in this mask will be registered per channel.
  171. * @mask_shared_by_type: Bit mask of enum iio_event_info values. Attributes
  172. * set in this mask will be shared by channel type.
  173. * @mask_shared_by_dir: Bit mask of enum iio_event_info values. Attributes
  174. * set in this mask will be shared by channel type and
  175. * direction.
  176. * @mask_shared_by_all: Bit mask of enum iio_event_info values. Attributes
  177. * set in this mask will be shared by all channels.
  178. */
  179. struct iio_event_spec {
  180. enum iio_event_type type;
  181. enum iio_event_direction dir;
  182. unsigned long mask_separate;
  183. unsigned long mask_shared_by_type;
  184. unsigned long mask_shared_by_dir;
  185. unsigned long mask_shared_by_all;
  186. };
  187. /**
  188. * struct iio_chan_spec - specification of a single channel
  189. * @type: What type of measurement is the channel making.
  190. * @channel: What number do we wish to assign the channel.
  191. * @channel2: If there is a second number for a differential
  192. * channel then this is it. If modified is set then the
  193. * value here specifies the modifier.
  194. * @address: Driver specific identifier.
  195. * @scan_index: Monotonic index to give ordering in scans when read
  196. * from a buffer.
  197. * @scan_type: sign: 's' or 'u' to specify signed or unsigned
  198. * realbits: Number of valid bits of data
  199. * storagebits: Realbits + padding
  200. * shift: Shift right by this before masking out
  201. * realbits.
  202. * repeat: Number of times real/storage bits
  203. * repeats. When the repeat element is
  204. * more than 1, then the type element in
  205. * sysfs will show a repeat value.
  206. * Otherwise, the number of repetitions is
  207. * omitted.
  208. * endianness: little or big endian
  209. * @info_mask_separate: What information is to be exported that is specific to
  210. * this channel.
  211. * @info_mask_shared_by_type: What information is to be exported that is shared
  212. * by all channels of the same type.
  213. * @info_mask_shared_by_dir: What information is to be exported that is shared
  214. * by all channels of the same direction.
  215. * @info_mask_shared_by_all: What information is to be exported that is shared
  216. * by all channels.
  217. * @event_spec: Array of events which should be registered for this
  218. * channel.
  219. * @num_event_specs: Size of the event_spec array.
  220. * @ext_info: Array of extended info attributes for this channel.
  221. * The array is NULL terminated, the last element should
  222. * have its name field set to NULL.
  223. * @extend_name: Allows labeling of channel attributes with an
  224. * informative name. Note this has no effect codes etc,
  225. * unlike modifiers.
  226. * @datasheet_name: A name used in in-kernel mapping of channels. It should
  227. * correspond to the first name that the channel is referred
  228. * to by in the datasheet (e.g. IND), or the nearest
  229. * possible compound name (e.g. IND-INC).
  230. * @modified: Does a modifier apply to this channel. What these are
  231. * depends on the channel type. Modifier is set in
  232. * channel2. Examples are IIO_MOD_X for axial sensors about
  233. * the 'x' axis.
  234. * @indexed: Specify the channel has a numerical index. If not,
  235. * the channel index number will be suppressed for sysfs
  236. * attributes but not for event codes.
  237. * @output: Channel is output.
  238. * @differential: Channel is differential.
  239. */
  240. struct iio_chan_spec {
  241. enum iio_chan_type type;
  242. int channel;
  243. int channel2;
  244. unsigned long address;
  245. int scan_index;
  246. struct {
  247. char sign;
  248. u8 realbits;
  249. u8 storagebits;
  250. u8 shift;
  251. u8 repeat;
  252. enum iio_endian endianness;
  253. } scan_type;
  254. long info_mask_separate;
  255. long info_mask_shared_by_type;
  256. long info_mask_shared_by_dir;
  257. long info_mask_shared_by_all;
  258. const struct iio_event_spec *event_spec;
  259. unsigned int num_event_specs;
  260. const struct iio_chan_spec_ext_info *ext_info;
  261. const char *extend_name;
  262. const char *datasheet_name;
  263. unsigned modified:1;
  264. unsigned indexed:1;
  265. unsigned output:1;
  266. unsigned differential:1;
  267. };
  268. /**
  269. * iio_channel_has_info() - Checks whether a channel supports a info attribute
  270. * @chan: The channel to be queried
  271. * @type: Type of the info attribute to be checked
  272. *
  273. * Returns true if the channels supports reporting values for the given info
  274. * attribute type, false otherwise.
  275. */
  276. static inline bool iio_channel_has_info(const struct iio_chan_spec *chan,
  277. enum iio_chan_info_enum type)
  278. {
  279. return (chan->info_mask_separate & BIT(type)) |
  280. (chan->info_mask_shared_by_type & BIT(type)) |
  281. (chan->info_mask_shared_by_dir & BIT(type)) |
  282. (chan->info_mask_shared_by_all & BIT(type));
  283. }
  284. #define IIO_CHAN_SOFT_TIMESTAMP(_si) { \
  285. .type = IIO_TIMESTAMP, \
  286. .channel = -1, \
  287. .scan_index = _si, \
  288. .scan_type = { \
  289. .sign = 's', \
  290. .realbits = 64, \
  291. .storagebits = 64, \
  292. }, \
  293. }
  294. s64 iio_get_time_ns(const struct iio_dev *indio_dev);
  295. unsigned int iio_get_time_res(const struct iio_dev *indio_dev);
  296. /* Device operating modes */
  297. #define INDIO_DIRECT_MODE 0x01
  298. #define INDIO_BUFFER_TRIGGERED 0x02
  299. #define INDIO_BUFFER_SOFTWARE 0x04
  300. #define INDIO_BUFFER_HARDWARE 0x08
  301. #define INDIO_EVENT_TRIGGERED 0x10
  302. #define INDIO_ALL_BUFFER_MODES \
  303. (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE | INDIO_BUFFER_SOFTWARE)
  304. #define INDIO_MAX_RAW_ELEMENTS 4
  305. struct iio_trigger; /* forward declaration */
  306. struct iio_dev;
  307. /**
  308. * struct iio_info - constant information about device
  309. * @driver_module: module structure used to ensure correct
  310. * ownership of chrdevs etc
  311. * @event_attrs: event control attributes
  312. * @attrs: general purpose device attributes
  313. * @read_raw: function to request a value from the device.
  314. * mask specifies which value. Note 0 means a reading of
  315. * the channel in question. Return value will specify the
  316. * type of value returned by the device. val and val2 will
  317. * contain the elements making up the returned value.
  318. * @read_raw_multi: function to return values from the device.
  319. * mask specifies which value. Note 0 means a reading of
  320. * the channel in question. Return value will specify the
  321. * type of value returned by the device. vals pointer
  322. * contain the elements making up the returned value.
  323. * max_len specifies maximum number of elements
  324. * vals pointer can contain. val_len is used to return
  325. * length of valid elements in vals.
  326. * @write_raw: function to write a value to the device.
  327. * Parameters are the same as for read_raw.
  328. * @write_raw_get_fmt: callback function to query the expected
  329. * format/precision. If not set by the driver, write_raw
  330. * returns IIO_VAL_INT_PLUS_MICRO.
  331. * @read_event_config: find out if the event is enabled.
  332. * @write_event_config: set if the event is enabled.
  333. * @read_event_value: read a configuration value associated with the event.
  334. * @write_event_value: write a configuration value for the event.
  335. * @validate_trigger: function to validate the trigger when the
  336. * current trigger gets changed.
  337. * @update_scan_mode: function to configure device and scan buffer when
  338. * channels have changed
  339. * @debugfs_reg_access: function to read or write register value of device
  340. * @of_xlate: function pointer to obtain channel specifier index.
  341. * When #iio-cells is greater than '0', the driver could
  342. * provide a custom of_xlate function that reads the
  343. * *args* and returns the appropriate index in registered
  344. * IIO channels array.
  345. * @hwfifo_set_watermark: function pointer to set the current hardware
  346. * fifo watermark level; see hwfifo_* entries in
  347. * Documentation/ABI/testing/sysfs-bus-iio for details on
  348. * how the hardware fifo operates
  349. * @hwfifo_flush_to_buffer: function pointer to flush the samples stored
  350. * in the hardware fifo to the device buffer. The driver
  351. * should not flush more than count samples. The function
  352. * must return the number of samples flushed, 0 if no
  353. * samples were flushed or a negative integer if no samples
  354. * were flushed and there was an error.
  355. **/
  356. struct iio_info {
  357. struct module *driver_module;
  358. struct attribute_group *event_attrs;
  359. const struct attribute_group *attrs;
  360. int (*read_raw)(struct iio_dev *indio_dev,
  361. struct iio_chan_spec const *chan,
  362. int *val,
  363. int *val2,
  364. long mask);
  365. int (*read_raw_multi)(struct iio_dev *indio_dev,
  366. struct iio_chan_spec const *chan,
  367. int max_len,
  368. int *vals,
  369. int *val_len,
  370. long mask);
  371. int (*write_raw)(struct iio_dev *indio_dev,
  372. struct iio_chan_spec const *chan,
  373. int val,
  374. int val2,
  375. long mask);
  376. int (*write_raw_get_fmt)(struct iio_dev *indio_dev,
  377. struct iio_chan_spec const *chan,
  378. long mask);
  379. int (*read_event_config)(struct iio_dev *indio_dev,
  380. const struct iio_chan_spec *chan,
  381. enum iio_event_type type,
  382. enum iio_event_direction dir);
  383. int (*write_event_config)(struct iio_dev *indio_dev,
  384. const struct iio_chan_spec *chan,
  385. enum iio_event_type type,
  386. enum iio_event_direction dir,
  387. int state);
  388. int (*read_event_value)(struct iio_dev *indio_dev,
  389. const struct iio_chan_spec *chan,
  390. enum iio_event_type type,
  391. enum iio_event_direction dir,
  392. enum iio_event_info info, int *val, int *val2);
  393. int (*write_event_value)(struct iio_dev *indio_dev,
  394. const struct iio_chan_spec *chan,
  395. enum iio_event_type type,
  396. enum iio_event_direction dir,
  397. enum iio_event_info info, int val, int val2);
  398. int (*validate_trigger)(struct iio_dev *indio_dev,
  399. struct iio_trigger *trig);
  400. int (*update_scan_mode)(struct iio_dev *indio_dev,
  401. const unsigned long *scan_mask);
  402. int (*debugfs_reg_access)(struct iio_dev *indio_dev,
  403. unsigned reg, unsigned writeval,
  404. unsigned *readval);
  405. int (*of_xlate)(struct iio_dev *indio_dev,
  406. const struct of_phandle_args *iiospec);
  407. int (*hwfifo_set_watermark)(struct iio_dev *indio_dev, unsigned val);
  408. int (*hwfifo_flush_to_buffer)(struct iio_dev *indio_dev,
  409. unsigned count);
  410. };
  411. /**
  412. * struct iio_buffer_setup_ops - buffer setup related callbacks
  413. * @preenable: [DRIVER] function to run prior to marking buffer enabled
  414. * @postenable: [DRIVER] function to run after marking buffer enabled
  415. * @predisable: [DRIVER] function to run prior to marking buffer
  416. * disabled
  417. * @postdisable: [DRIVER] function to run after marking buffer disabled
  418. * @validate_scan_mask: [DRIVER] function callback to check whether a given
  419. * scan mask is valid for the device.
  420. */
  421. struct iio_buffer_setup_ops {
  422. int (*preenable)(struct iio_dev *);
  423. int (*postenable)(struct iio_dev *);
  424. int (*predisable)(struct iio_dev *);
  425. int (*postdisable)(struct iio_dev *);
  426. bool (*validate_scan_mask)(struct iio_dev *indio_dev,
  427. const unsigned long *scan_mask);
  428. };
  429. /**
  430. * struct iio_dev - industrial I/O device
  431. * @id: [INTERN] used to identify device internally
  432. * @modes: [DRIVER] operating modes supported by device
  433. * @currentmode: [DRIVER] current operating mode
  434. * @dev: [DRIVER] device structure, should be assigned a parent
  435. * and owner
  436. * @event_interface: [INTERN] event chrdevs associated with interrupt lines
  437. * @buffer: [DRIVER] any buffer present
  438. * @buffer_list: [INTERN] list of all buffers currently attached
  439. * @scan_bytes: [INTERN] num bytes captured to be fed to buffer demux
  440. * @mlock: [DRIVER] lock used to prevent simultaneous device state
  441. * changes
  442. * @available_scan_masks: [DRIVER] optional array of allowed bitmasks
  443. * @masklength: [INTERN] the length of the mask established from
  444. * channels
  445. * @active_scan_mask: [INTERN] union of all scan masks requested by buffers
  446. * @scan_timestamp: [INTERN] set if any buffers have requested timestamp
  447. * @scan_index_timestamp:[INTERN] cache of the index to the timestamp
  448. * @trig: [INTERN] current device trigger (buffer modes)
  449. * @trig_readonly [INTERN] mark the current trigger immutable
  450. * @pollfunc: [DRIVER] function run on trigger being received
  451. * @pollfunc_event: [DRIVER] function run on events trigger being received
  452. * @channels: [DRIVER] channel specification structure table
  453. * @num_channels: [DRIVER] number of channels specified in @channels.
  454. * @channel_attr_list: [INTERN] keep track of automatically created channel
  455. * attributes
  456. * @chan_attr_group: [INTERN] group for all attrs in base directory
  457. * @name: [DRIVER] name of the device.
  458. * @info: [DRIVER] callbacks and constant info from driver
  459. * @clock_id: [INTERN] timestamping clock posix identifier
  460. * @info_exist_lock: [INTERN] lock to prevent use during removal
  461. * @setup_ops: [DRIVER] callbacks to call before and after buffer
  462. * enable/disable
  463. * @chrdev: [INTERN] associated character device
  464. * @groups: [INTERN] attribute groups
  465. * @groupcounter: [INTERN] index of next attribute group
  466. * @flags: [INTERN] file ops related flags including busy flag.
  467. * @debugfs_dentry: [INTERN] device specific debugfs dentry.
  468. * @cached_reg_addr: [INTERN] cached register address for debugfs reads.
  469. */
  470. struct iio_dev {
  471. int id;
  472. int modes;
  473. int currentmode;
  474. struct device dev;
  475. struct iio_event_interface *event_interface;
  476. struct iio_buffer *buffer;
  477. struct list_head buffer_list;
  478. int scan_bytes;
  479. struct mutex mlock;
  480. const unsigned long *available_scan_masks;
  481. unsigned masklength;
  482. const unsigned long *active_scan_mask;
  483. bool scan_timestamp;
  484. unsigned scan_index_timestamp;
  485. struct iio_trigger *trig;
  486. bool trig_readonly;
  487. struct iio_poll_func *pollfunc;
  488. struct iio_poll_func *pollfunc_event;
  489. struct iio_chan_spec const *channels;
  490. int num_channels;
  491. struct list_head channel_attr_list;
  492. struct attribute_group chan_attr_group;
  493. const char *name;
  494. const struct iio_info *info;
  495. clockid_t clock_id;
  496. struct mutex info_exist_lock;
  497. const struct iio_buffer_setup_ops *setup_ops;
  498. struct cdev chrdev;
  499. #define IIO_MAX_GROUPS 6
  500. const struct attribute_group *groups[IIO_MAX_GROUPS + 1];
  501. int groupcounter;
  502. unsigned long flags;
  503. #if defined(CONFIG_DEBUG_FS)
  504. struct dentry *debugfs_dentry;
  505. unsigned cached_reg_addr;
  506. #endif
  507. };
  508. const struct iio_chan_spec
  509. *iio_find_channel_from_si(struct iio_dev *indio_dev, int si);
  510. int iio_device_register(struct iio_dev *indio_dev);
  511. void iio_device_unregister(struct iio_dev *indio_dev);
  512. int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev);
  513. void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev);
  514. int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp);
  515. int iio_device_claim_direct_mode(struct iio_dev *indio_dev);
  516. void iio_device_release_direct_mode(struct iio_dev *indio_dev);
  517. extern struct bus_type iio_bus_type;
  518. /**
  519. * iio_device_put() - reference counted deallocation of struct device
  520. * @indio_dev: IIO device structure containing the device
  521. **/
  522. static inline void iio_device_put(struct iio_dev *indio_dev)
  523. {
  524. if (indio_dev)
  525. put_device(&indio_dev->dev);
  526. }
  527. /**
  528. * iio_device_get_clock() - Retrieve current timestamping clock for the device
  529. * @indio_dev: IIO device structure containing the device
  530. */
  531. static inline clockid_t iio_device_get_clock(const struct iio_dev *indio_dev)
  532. {
  533. return indio_dev->clock_id;
  534. }
  535. /**
  536. * dev_to_iio_dev() - Get IIO device struct from a device struct
  537. * @dev: The device embedded in the IIO device
  538. *
  539. * Note: The device must be a IIO device, otherwise the result is undefined.
  540. */
  541. static inline struct iio_dev *dev_to_iio_dev(struct device *dev)
  542. {
  543. return container_of(dev, struct iio_dev, dev);
  544. }
  545. /**
  546. * iio_device_get() - increment reference count for the device
  547. * @indio_dev: IIO device structure
  548. *
  549. * Returns: The passed IIO device
  550. **/
  551. static inline struct iio_dev *iio_device_get(struct iio_dev *indio_dev)
  552. {
  553. return indio_dev ? dev_to_iio_dev(get_device(&indio_dev->dev)) : NULL;
  554. }
  555. /**
  556. * iio_device_set_drvdata() - Set device driver data
  557. * @indio_dev: IIO device structure
  558. * @data: Driver specific data
  559. *
  560. * Allows to attach an arbitrary pointer to an IIO device, which can later be
  561. * retrieved by iio_device_get_drvdata().
  562. */
  563. static inline void iio_device_set_drvdata(struct iio_dev *indio_dev, void *data)
  564. {
  565. dev_set_drvdata(&indio_dev->dev, data);
  566. }
  567. /**
  568. * iio_device_get_drvdata() - Get device driver data
  569. * @indio_dev: IIO device structure
  570. *
  571. * Returns the data previously set with iio_device_set_drvdata()
  572. */
  573. static inline void *iio_device_get_drvdata(struct iio_dev *indio_dev)
  574. {
  575. return dev_get_drvdata(&indio_dev->dev);
  576. }
  577. /* Can we make this smaller? */
  578. #define IIO_ALIGN L1_CACHE_BYTES
  579. struct iio_dev *iio_device_alloc(int sizeof_priv);
  580. static inline void *iio_priv(const struct iio_dev *indio_dev)
  581. {
  582. return (char *)indio_dev + ALIGN(sizeof(struct iio_dev), IIO_ALIGN);
  583. }
  584. static inline struct iio_dev *iio_priv_to_dev(void *priv)
  585. {
  586. return (struct iio_dev *)((char *)priv -
  587. ALIGN(sizeof(struct iio_dev), IIO_ALIGN));
  588. }
  589. void iio_device_free(struct iio_dev *indio_dev);
  590. int devm_iio_device_match(struct device *dev, void *res, void *data);
  591. struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv);
  592. void devm_iio_device_free(struct device *dev, struct iio_dev *indio_dev);
  593. struct iio_trigger *devm_iio_trigger_alloc(struct device *dev,
  594. const char *fmt, ...);
  595. void devm_iio_trigger_free(struct device *dev, struct iio_trigger *iio_trig);
  596. /**
  597. * iio_buffer_enabled() - helper function to test if the buffer is enabled
  598. * @indio_dev: IIO device structure for device
  599. **/
  600. static inline bool iio_buffer_enabled(struct iio_dev *indio_dev)
  601. {
  602. return indio_dev->currentmode
  603. & (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE |
  604. INDIO_BUFFER_SOFTWARE);
  605. }
  606. /**
  607. * iio_get_debugfs_dentry() - helper function to get the debugfs_dentry
  608. * @indio_dev: IIO device structure for device
  609. **/
  610. #if defined(CONFIG_DEBUG_FS)
  611. static inline struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
  612. {
  613. return indio_dev->debugfs_dentry;
  614. }
  615. #else
  616. static inline struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
  617. {
  618. return NULL;
  619. }
  620. #endif
  621. ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals);
  622. int iio_str_to_fixpoint(const char *str, int fract_mult, int *integer,
  623. int *fract);
  624. /**
  625. * IIO_DEGREE_TO_RAD() - Convert degree to rad
  626. * @deg: A value in degree
  627. *
  628. * Returns the given value converted from degree to rad
  629. */
  630. #define IIO_DEGREE_TO_RAD(deg) (((deg) * 314159ULL + 9000000ULL) / 18000000ULL)
  631. /**
  632. * IIO_RAD_TO_DEGREE() - Convert rad to degree
  633. * @rad: A value in rad
  634. *
  635. * Returns the given value converted from rad to degree
  636. */
  637. #define IIO_RAD_TO_DEGREE(rad) \
  638. (((rad) * 18000000ULL + 314159ULL / 2) / 314159ULL)
  639. /**
  640. * IIO_G_TO_M_S_2() - Convert g to meter / second**2
  641. * @g: A value in g
  642. *
  643. * Returns the given value converted from g to meter / second**2
  644. */
  645. #define IIO_G_TO_M_S_2(g) ((g) * 980665ULL / 100000ULL)
  646. /**
  647. * IIO_M_S_2_TO_G() - Convert meter / second**2 to g
  648. * @ms2: A value in meter / second**2
  649. *
  650. * Returns the given value converted from meter / second**2 to g
  651. */
  652. #define IIO_M_S_2_TO_G(ms2) (((ms2) * 100000ULL + 980665ULL / 2) / 980665ULL)
  653. #endif /* _INDUSTRIAL_IO_H_ */