iio.h 16 KB

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