iio.h 16 KB

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