iio.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. /* IIO TODO LIST */
  15. /*
  16. * Provide means of adjusting timer accuracy.
  17. * Currently assumes nano seconds.
  18. */
  19. enum iio_chan_info_enum {
  20. IIO_CHAN_INFO_RAW = 0,
  21. IIO_CHAN_INFO_PROCESSED,
  22. IIO_CHAN_INFO_SCALE,
  23. IIO_CHAN_INFO_OFFSET,
  24. IIO_CHAN_INFO_CALIBSCALE,
  25. IIO_CHAN_INFO_CALIBBIAS,
  26. IIO_CHAN_INFO_PEAK,
  27. IIO_CHAN_INFO_PEAK_SCALE,
  28. IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW,
  29. IIO_CHAN_INFO_AVERAGE_RAW,
  30. IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY,
  31. IIO_CHAN_INFO_SAMP_FREQ,
  32. };
  33. #define IIO_CHAN_INFO_SHARED_BIT(type) BIT(type*2)
  34. #define IIO_CHAN_INFO_SEPARATE_BIT(type) BIT(type*2 + 1)
  35. #define IIO_CHAN_INFO_RAW_SEPARATE_BIT \
  36. IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_RAW)
  37. #define IIO_CHAN_INFO_PROCESSED_SEPARATE_BIT \
  38. IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_PROCESSED)
  39. #define IIO_CHAN_INFO_SCALE_SEPARATE_BIT \
  40. IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_SCALE)
  41. #define IIO_CHAN_INFO_SCALE_SHARED_BIT \
  42. IIO_CHAN_INFO_SHARED_BIT(IIO_CHAN_INFO_SCALE)
  43. #define IIO_CHAN_INFO_OFFSET_SEPARATE_BIT \
  44. IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_OFFSET)
  45. #define IIO_CHAN_INFO_OFFSET_SHARED_BIT \
  46. IIO_CHAN_INFO_SHARED_BIT(IIO_CHAN_INFO_OFFSET)
  47. #define IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT \
  48. IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_CALIBSCALE)
  49. #define IIO_CHAN_INFO_CALIBSCALE_SHARED_BIT \
  50. IIO_CHAN_INFO_SHARED_BIT(IIO_CHAN_INFO_CALIBSCALE)
  51. #define IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT \
  52. IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_CALIBBIAS)
  53. #define IIO_CHAN_INFO_CALIBBIAS_SHARED_BIT \
  54. IIO_CHAN_INFO_SHARED_BIT(IIO_CHAN_INFO_CALIBBIAS)
  55. #define IIO_CHAN_INFO_PEAK_SEPARATE_BIT \
  56. IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_PEAK)
  57. #define IIO_CHAN_INFO_PEAK_SHARED_BIT \
  58. IIO_CHAN_INFO_SHARED_BIT(IIO_CHAN_INFO_PEAK)
  59. #define IIO_CHAN_INFO_PEAKSCALE_SEPARATE_BIT \
  60. IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_PEAKSCALE)
  61. #define IIO_CHAN_INFO_PEAKSCALE_SHARED_BIT \
  62. IIO_CHAN_INFO_SHARED_BIT(IIO_CHAN_INFO_PEAKSCALE)
  63. #define IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW_SEPARATE_BIT \
  64. IIO_CHAN_INFO_SEPARATE_BIT( \
  65. IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW)
  66. #define IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW_SHARED_BIT \
  67. IIO_CHAN_INFO_SHARED_BIT( \
  68. IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW)
  69. #define IIO_CHAN_INFO_AVERAGE_RAW_SEPARATE_BIT \
  70. IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_AVERAGE_RAW)
  71. #define IIO_CHAN_INFO_AVERAGE_RAW_SHARED_BIT \
  72. IIO_CHAN_INFO_SHARED_BIT(IIO_CHAN_INFO_AVERAGE_RAW)
  73. #define IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY_SHARED_BIT \
  74. IIO_CHAN_INFO_SHARED_BIT( \
  75. IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY)
  76. #define IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY_SEPARATE_BIT \
  77. IIO_CHAN_INFO_SEPARATE_BIT( \
  78. IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY)
  79. #define IIO_CHAN_INFO_SAMP_FREQ_SEPARATE_BIT \
  80. IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_SAMP_FREQ)
  81. #define IIO_CHAN_INFO_SAMP_FREQ_SHARED_BIT \
  82. IIO_CHAN_INFO_SHARED_BIT(IIO_CHAN_INFO_SAMP_FREQ)
  83. enum iio_endian {
  84. IIO_CPU,
  85. IIO_BE,
  86. IIO_LE,
  87. };
  88. struct iio_chan_spec;
  89. struct iio_dev;
  90. /**
  91. * struct iio_chan_spec_ext_info - Extended channel info attribute
  92. * @name: Info attribute name
  93. * @shared: Whether this attribute is shared between all channels.
  94. * @read: Read callback for this info attribute, may be NULL.
  95. * @write: Write callback for this info attribute, may be NULL.
  96. */
  97. struct iio_chan_spec_ext_info {
  98. const char *name;
  99. bool shared;
  100. ssize_t (*read)(struct iio_dev *, struct iio_chan_spec const *,
  101. char *buf);
  102. ssize_t (*write)(struct iio_dev *, struct iio_chan_spec const *,
  103. const char *buf, size_t len);
  104. };
  105. /**
  106. * struct iio_chan_spec - specification of a single channel
  107. * @type: What type of measurement is the channel making.
  108. * @channel: What number or name do we wish to assign the channel.
  109. * @channel2: If there is a second number for a differential
  110. * channel then this is it. If modified is set then the
  111. * value here specifies the modifier.
  112. * @address: Driver specific identifier.
  113. * @scan_index: Monotonic index to give ordering in scans when read
  114. * from a buffer.
  115. * @scan_type: Sign: 's' or 'u' to specify signed or unsigned
  116. * realbits: Number of valid bits of data
  117. * storage_bits: Realbits + padding
  118. * shift: Shift right by this before masking out
  119. * realbits.
  120. * endianness: little or big endian
  121. * @info_mask: What information is to be exported about this channel.
  122. * This includes calibbias, scale etc.
  123. * @event_mask: What events can this channel produce.
  124. * @ext_info: Array of extended info attributes for this channel.
  125. * The array is NULL terminated, the last element should
  126. * have it's name field set to NULL.
  127. * @extend_name: Allows labeling of channel attributes with an
  128. * informative name. Note this has no effect codes etc,
  129. * unlike modifiers.
  130. * @datasheet_name: A name used in in kernel mapping of channels. It should
  131. * correspond to the first name that the channel is referred
  132. * to by in the datasheet (e.g. IND), or the nearest
  133. * possible compound name (e.g. IND-INC).
  134. * @modified: Does a modifier apply to this channel. What these are
  135. * depends on the channel type. Modifier is set in
  136. * channel2. Examples are IIO_MOD_X for axial sensors about
  137. * the 'x' axis.
  138. * @indexed: Specify the channel has a numerical index. If not,
  139. * the value in channel will be suppressed for attribute
  140. * but not for event codes. Typically set it to 0 when
  141. * the index is false.
  142. * @differential: Channel is differential.
  143. */
  144. struct iio_chan_spec {
  145. enum iio_chan_type type;
  146. int channel;
  147. int channel2;
  148. unsigned long address;
  149. int scan_index;
  150. struct {
  151. char sign;
  152. u8 realbits;
  153. u8 storagebits;
  154. u8 shift;
  155. enum iio_endian endianness;
  156. } scan_type;
  157. long info_mask;
  158. long event_mask;
  159. const struct iio_chan_spec_ext_info *ext_info;
  160. const char *extend_name;
  161. const char *datasheet_name;
  162. unsigned modified:1;
  163. unsigned indexed:1;
  164. unsigned output:1;
  165. unsigned differential:1;
  166. };
  167. #define IIO_ST(si, rb, sb, sh) \
  168. { .sign = si, .realbits = rb, .storagebits = sb, .shift = sh }
  169. #define IIO_CHAN_SOFT_TIMESTAMP(_si) \
  170. { .type = IIO_TIMESTAMP, .channel = -1, \
  171. .scan_index = _si, .scan_type = IIO_ST('s', 64, 64, 0) }
  172. /**
  173. * iio_get_time_ns() - utility function to get a time stamp for events etc
  174. **/
  175. static inline s64 iio_get_time_ns(void)
  176. {
  177. struct timespec ts;
  178. /*
  179. * calls getnstimeofday.
  180. * If hrtimers then up to ns accurate, if not microsecond.
  181. */
  182. ktime_get_real_ts(&ts);
  183. return timespec_to_ns(&ts);
  184. }
  185. /* Device operating modes */
  186. #define INDIO_DIRECT_MODE 0x01
  187. #define INDIO_BUFFER_TRIGGERED 0x02
  188. #define INDIO_BUFFER_HARDWARE 0x08
  189. #define INDIO_ALL_BUFFER_MODES \
  190. (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE)
  191. struct iio_trigger; /* forward declaration */
  192. struct iio_dev;
  193. /**
  194. * struct iio_info - constant information about device
  195. * @driver_module: module structure used to ensure correct
  196. * ownership of chrdevs etc
  197. * @event_attrs: event control attributes
  198. * @attrs: general purpose device attributes
  199. * @read_raw: function to request a value from the device.
  200. * mask specifies which value. Note 0 means a reading of
  201. * the channel in question. Return value will specify the
  202. * type of value returned by the device. val and val2 will
  203. * contain the elements making up the returned value.
  204. * @write_raw: function to write a value to the device.
  205. * Parameters are the same as for read_raw.
  206. * @write_raw_get_fmt: callback function to query the expected
  207. * format/precision. If not set by the driver, write_raw
  208. * returns IIO_VAL_INT_PLUS_MICRO.
  209. * @read_event_config: find out if the event is enabled.
  210. * @write_event_config: set if the event is enabled.
  211. * @read_event_value: read a value associated with the event. Meaning
  212. * is event dependant. event_code specifies which event.
  213. * @write_event_value: write the value associated with the event.
  214. * Meaning is event dependent.
  215. * @validate_trigger: function to validate the trigger when the
  216. * current trigger gets changed.
  217. **/
  218. struct iio_info {
  219. struct module *driver_module;
  220. struct attribute_group *event_attrs;
  221. const struct attribute_group *attrs;
  222. int (*read_raw)(struct iio_dev *indio_dev,
  223. struct iio_chan_spec const *chan,
  224. int *val,
  225. int *val2,
  226. long mask);
  227. int (*write_raw)(struct iio_dev *indio_dev,
  228. struct iio_chan_spec const *chan,
  229. int val,
  230. int val2,
  231. long mask);
  232. int (*write_raw_get_fmt)(struct iio_dev *indio_dev,
  233. struct iio_chan_spec const *chan,
  234. long mask);
  235. int (*read_event_config)(struct iio_dev *indio_dev,
  236. u64 event_code);
  237. int (*write_event_config)(struct iio_dev *indio_dev,
  238. u64 event_code,
  239. int state);
  240. int (*read_event_value)(struct iio_dev *indio_dev,
  241. u64 event_code,
  242. int *val);
  243. int (*write_event_value)(struct iio_dev *indio_dev,
  244. u64 event_code,
  245. int val);
  246. int (*validate_trigger)(struct iio_dev *indio_dev,
  247. struct iio_trigger *trig);
  248. int (*update_scan_mode)(struct iio_dev *indio_dev,
  249. const unsigned long *scan_mask);
  250. int (*debugfs_reg_access)(struct iio_dev *indio_dev,
  251. unsigned reg, unsigned writeval,
  252. unsigned *readval);
  253. };
  254. /**
  255. * struct iio_buffer_setup_ops - buffer setup related callbacks
  256. * @preenable: [DRIVER] function to run prior to marking buffer enabled
  257. * @postenable: [DRIVER] function to run after marking buffer enabled
  258. * @predisable: [DRIVER] function to run prior to marking buffer
  259. * disabled
  260. * @postdisable: [DRIVER] function to run after marking buffer disabled
  261. */
  262. struct iio_buffer_setup_ops {
  263. int (*preenable)(struct iio_dev *);
  264. int (*postenable)(struct iio_dev *);
  265. int (*predisable)(struct iio_dev *);
  266. int (*postdisable)(struct iio_dev *);
  267. };
  268. /**
  269. * struct iio_dev - industrial I/O device
  270. * @id: [INTERN] used to identify device internally
  271. * @modes: [DRIVER] operating modes supported by device
  272. * @currentmode: [DRIVER] current operating mode
  273. * @dev: [DRIVER] device structure, should be assigned a parent
  274. * and owner
  275. * @event_interface: [INTERN] event chrdevs associated with interrupt lines
  276. * @buffer: [DRIVER] any buffer present
  277. * @scan_bytes: [INTERN] num bytes captured to be fed to buffer demux
  278. * @mlock: [INTERN] lock used to prevent simultaneous device state
  279. * changes
  280. * @available_scan_masks: [DRIVER] optional array of allowed bitmasks
  281. * @masklength: [INTERN] the length of the mask established from
  282. * channels
  283. * @active_scan_mask: [INTERN] union of all scan masks requested by buffers
  284. * @scan_timestamp: [INTERN] set if any buffers have requested timestamp
  285. * @scan_index_timestamp:[INTERN] cache of the index to the timestamp
  286. * @trig: [INTERN] current device trigger (buffer modes)
  287. * @pollfunc: [DRIVER] function run on trigger being received
  288. * @channels: [DRIVER] channel specification structure table
  289. * @num_channels: [DRIVER] number of chanels specified in @channels.
  290. * @channel_attr_list: [INTERN] keep track of automatically created channel
  291. * attributes
  292. * @chan_attr_group: [INTERN] group for all attrs in base directory
  293. * @name: [DRIVER] name of the device.
  294. * @info: [DRIVER] callbacks and constant info from driver
  295. * @info_exist_lock: [INTERN] lock to prevent use during removal
  296. * @setup_ops: [DRIVER] callbacks to call before and after buffer
  297. * enable/disable
  298. * @chrdev: [INTERN] associated character device
  299. * @groups: [INTERN] attribute groups
  300. * @groupcounter: [INTERN] index of next attribute group
  301. * @flags: [INTERN] file ops related flags including busy flag.
  302. * @debugfs_dentry: [INTERN] device specific debugfs dentry.
  303. * @cached_reg_addr: [INTERN] cached register address for debugfs reads.
  304. */
  305. struct iio_dev {
  306. int id;
  307. int modes;
  308. int currentmode;
  309. struct device dev;
  310. struct iio_event_interface *event_interface;
  311. struct iio_buffer *buffer;
  312. int scan_bytes;
  313. struct mutex mlock;
  314. const unsigned long *available_scan_masks;
  315. unsigned masklength;
  316. const unsigned long *active_scan_mask;
  317. bool scan_timestamp;
  318. unsigned scan_index_timestamp;
  319. struct iio_trigger *trig;
  320. struct iio_poll_func *pollfunc;
  321. struct iio_chan_spec const *channels;
  322. int num_channels;
  323. struct list_head channel_attr_list;
  324. struct attribute_group chan_attr_group;
  325. const char *name;
  326. const struct iio_info *info;
  327. struct mutex info_exist_lock;
  328. const struct iio_buffer_setup_ops *setup_ops;
  329. struct cdev chrdev;
  330. #define IIO_MAX_GROUPS 6
  331. const struct attribute_group *groups[IIO_MAX_GROUPS + 1];
  332. int groupcounter;
  333. unsigned long flags;
  334. #if defined(CONFIG_DEBUG_FS)
  335. struct dentry *debugfs_dentry;
  336. unsigned cached_reg_addr;
  337. #endif
  338. };
  339. /**
  340. * iio_find_channel_from_si() - get channel from its scan index
  341. * @indio_dev: device
  342. * @si: scan index to match
  343. */
  344. const struct iio_chan_spec
  345. *iio_find_channel_from_si(struct iio_dev *indio_dev, int si);
  346. /**
  347. * iio_device_register() - register a device with the IIO subsystem
  348. * @indio_dev: Device structure filled by the device driver
  349. **/
  350. int iio_device_register(struct iio_dev *indio_dev);
  351. /**
  352. * iio_device_unregister() - unregister a device from the IIO subsystem
  353. * @indio_dev: Device structure representing the device.
  354. **/
  355. void iio_device_unregister(struct iio_dev *indio_dev);
  356. /**
  357. * iio_push_event() - try to add event to the list for userspace reading
  358. * @indio_dev: IIO device structure
  359. * @ev_code: What event
  360. * @timestamp: When the event occurred
  361. **/
  362. int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp);
  363. extern struct bus_type iio_bus_type;
  364. /**
  365. * iio_device_put() - reference counted deallocation of struct device
  366. * @dev: the iio_device containing the device
  367. **/
  368. static inline void iio_device_put(struct iio_dev *indio_dev)
  369. {
  370. if (indio_dev)
  371. put_device(&indio_dev->dev);
  372. };
  373. /* Can we make this smaller? */
  374. #define IIO_ALIGN L1_CACHE_BYTES
  375. /**
  376. * iio_device_alloc() - allocate an iio_dev from a driver
  377. * @sizeof_priv: Space to allocate for private structure.
  378. **/
  379. struct iio_dev *iio_device_alloc(int sizeof_priv);
  380. static inline void *iio_priv(const struct iio_dev *indio_dev)
  381. {
  382. return (char *)indio_dev + ALIGN(sizeof(struct iio_dev), IIO_ALIGN);
  383. }
  384. static inline struct iio_dev *iio_priv_to_dev(void *priv)
  385. {
  386. return (struct iio_dev *)((char *)priv -
  387. ALIGN(sizeof(struct iio_dev), IIO_ALIGN));
  388. }
  389. /**
  390. * iio_device_free() - free an iio_dev from a driver
  391. * @dev: the iio_dev associated with the device
  392. **/
  393. void iio_device_free(struct iio_dev *indio_dev);
  394. /**
  395. * iio_buffer_enabled() - helper function to test if the buffer is enabled
  396. * @indio_dev: IIO device info structure for device
  397. **/
  398. static inline bool iio_buffer_enabled(struct iio_dev *indio_dev)
  399. {
  400. return indio_dev->currentmode
  401. & (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE);
  402. };
  403. /**
  404. * iio_get_debugfs_dentry() - helper function to get the debugfs_dentry
  405. * @indio_dev: IIO device info structure for device
  406. **/
  407. #if defined(CONFIG_DEBUG_FS)
  408. static inline struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
  409. {
  410. return indio_dev->debugfs_dentry;
  411. };
  412. #else
  413. static inline struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
  414. {
  415. return NULL;
  416. };
  417. #endif
  418. #endif /* _INDUSTRIAL_IO_H_ */