iio.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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. IIO_CHAN_INFO_HYSTERESIS,
  36. };
  37. #define IIO_CHAN_INFO_SHARED_BIT(type) BIT(type*2)
  38. #define IIO_CHAN_INFO_SEPARATE_BIT(type) BIT(type*2 + 1)
  39. #define IIO_CHAN_INFO_BITS(type) (IIO_CHAN_INFO_SHARED_BIT(type) | \
  40. IIO_CHAN_INFO_SEPARATE_BIT(type))
  41. #define IIO_CHAN_INFO_RAW_SEPARATE_BIT \
  42. IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_RAW)
  43. #define IIO_CHAN_INFO_PROCESSED_SEPARATE_BIT \
  44. IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_PROCESSED)
  45. #define IIO_CHAN_INFO_SCALE_SEPARATE_BIT \
  46. IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_SCALE)
  47. #define IIO_CHAN_INFO_SCALE_SHARED_BIT \
  48. IIO_CHAN_INFO_SHARED_BIT(IIO_CHAN_INFO_SCALE)
  49. #define IIO_CHAN_INFO_OFFSET_SEPARATE_BIT \
  50. IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_OFFSET)
  51. #define IIO_CHAN_INFO_OFFSET_SHARED_BIT \
  52. IIO_CHAN_INFO_SHARED_BIT(IIO_CHAN_INFO_OFFSET)
  53. #define IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT \
  54. IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_CALIBSCALE)
  55. #define IIO_CHAN_INFO_CALIBSCALE_SHARED_BIT \
  56. IIO_CHAN_INFO_SHARED_BIT(IIO_CHAN_INFO_CALIBSCALE)
  57. #define IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT \
  58. IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_CALIBBIAS)
  59. #define IIO_CHAN_INFO_CALIBBIAS_SHARED_BIT \
  60. IIO_CHAN_INFO_SHARED_BIT(IIO_CHAN_INFO_CALIBBIAS)
  61. #define IIO_CHAN_INFO_PEAK_SEPARATE_BIT \
  62. IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_PEAK)
  63. #define IIO_CHAN_INFO_PEAK_SHARED_BIT \
  64. IIO_CHAN_INFO_SHARED_BIT(IIO_CHAN_INFO_PEAK)
  65. #define IIO_CHAN_INFO_PEAKSCALE_SEPARATE_BIT \
  66. IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_PEAKSCALE)
  67. #define IIO_CHAN_INFO_PEAKSCALE_SHARED_BIT \
  68. IIO_CHAN_INFO_SHARED_BIT(IIO_CHAN_INFO_PEAKSCALE)
  69. #define IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW_SEPARATE_BIT \
  70. IIO_CHAN_INFO_SEPARATE_BIT( \
  71. IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW)
  72. #define IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW_SHARED_BIT \
  73. IIO_CHAN_INFO_SHARED_BIT( \
  74. IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW)
  75. #define IIO_CHAN_INFO_AVERAGE_RAW_SEPARATE_BIT \
  76. IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_AVERAGE_RAW)
  77. #define IIO_CHAN_INFO_AVERAGE_RAW_SHARED_BIT \
  78. IIO_CHAN_INFO_SHARED_BIT(IIO_CHAN_INFO_AVERAGE_RAW)
  79. #define IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY_SHARED_BIT \
  80. IIO_CHAN_INFO_SHARED_BIT( \
  81. IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY)
  82. #define IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY_SEPARATE_BIT \
  83. IIO_CHAN_INFO_SEPARATE_BIT( \
  84. IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY)
  85. #define IIO_CHAN_INFO_SAMP_FREQ_SEPARATE_BIT \
  86. IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_SAMP_FREQ)
  87. #define IIO_CHAN_INFO_SAMP_FREQ_SHARED_BIT \
  88. IIO_CHAN_INFO_SHARED_BIT(IIO_CHAN_INFO_SAMP_FREQ)
  89. #define IIO_CHAN_INFO_FREQUENCY_SEPARATE_BIT \
  90. IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_FREQUENCY)
  91. #define IIO_CHAN_INFO_FREQUENCY_SHARED_BIT \
  92. IIO_CHAN_INFO_SHARED_BIT(IIO_CHAN_INFO_FREQUENCY)
  93. #define IIO_CHAN_INFO_PHASE_SEPARATE_BIT \
  94. IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_PHASE)
  95. #define IIO_CHAN_INFO_PHASE_SHARED_BIT \
  96. IIO_CHAN_INFO_SHARED_BIT(IIO_CHAN_INFO_PHASE)
  97. #define IIO_CHAN_INFO_HARDWAREGAIN_SEPARATE_BIT \
  98. IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_HARDWAREGAIN)
  99. #define IIO_CHAN_INFO_HARDWAREGAIN_SHARED_BIT \
  100. IIO_CHAN_INFO_SHARED_BIT(IIO_CHAN_INFO_HARDWAREGAIN)
  101. #define IIO_CHAN_INFO_HYSTERESIS_SEPARATE_BIT \
  102. IIO_CHAN_INFO_SEPARATE_BIT(IIO_CHAN_INFO_HYSTERESIS)
  103. #define IIO_CHAN_INFO_HYSTERESIS_SHARED_BIT \
  104. IIO_CHAN_INFO_SHARED_BIT(IIO_CHAN_INFO_HYSTERESIS)
  105. enum iio_endian {
  106. IIO_CPU,
  107. IIO_BE,
  108. IIO_LE,
  109. };
  110. struct iio_chan_spec;
  111. struct iio_dev;
  112. /**
  113. * struct iio_chan_spec_ext_info - Extended channel info attribute
  114. * @name: Info attribute name
  115. * @shared: Whether this attribute is shared between all channels.
  116. * @read: Read callback for this info attribute, may be NULL.
  117. * @write: Write callback for this info attribute, may be NULL.
  118. * @private: Data private to the driver.
  119. */
  120. struct iio_chan_spec_ext_info {
  121. const char *name;
  122. bool shared;
  123. ssize_t (*read)(struct iio_dev *, uintptr_t private,
  124. struct iio_chan_spec const *, char *buf);
  125. ssize_t (*write)(struct iio_dev *, uintptr_t private,
  126. struct iio_chan_spec const *, const char *buf,
  127. size_t len);
  128. uintptr_t private;
  129. };
  130. /**
  131. * struct iio_enum - Enum channel info attribute
  132. * @items: An array of strings.
  133. * @num_items: Length of the item array.
  134. * @set: Set callback function, may be NULL.
  135. * @get: Get callback function, may be NULL.
  136. *
  137. * The iio_enum struct can be used to implement enum style channel attributes.
  138. * Enum style attributes are those which have a set of strings which map to
  139. * unsigned integer values. The IIO enum helper code takes care of mapping
  140. * between value and string as well as generating a "_available" file which
  141. * contains a list of all available items. The set callback will be called when
  142. * the attribute is updated. The last parameter is the index to the newly
  143. * activated item. The get callback will be used to query the currently active
  144. * item and is supposed to return the index for it.
  145. */
  146. struct iio_enum {
  147. const char * const *items;
  148. unsigned int num_items;
  149. int (*set)(struct iio_dev *, const struct iio_chan_spec *, unsigned int);
  150. int (*get)(struct iio_dev *, const struct iio_chan_spec *);
  151. };
  152. ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
  153. uintptr_t priv, const struct iio_chan_spec *chan, char *buf);
  154. ssize_t iio_enum_read(struct iio_dev *indio_dev,
  155. uintptr_t priv, const struct iio_chan_spec *chan, char *buf);
  156. ssize_t iio_enum_write(struct iio_dev *indio_dev,
  157. uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
  158. size_t len);
  159. /**
  160. * IIO_ENUM() - Initialize enum extended channel attribute
  161. * @_name: Attribute name
  162. * @_shared: Whether the attribute is shared between all channels
  163. * @_e: Pointer to an iio_enum struct
  164. *
  165. * This should usually be used together with IIO_ENUM_AVAILABLE()
  166. */
  167. #define IIO_ENUM(_name, _shared, _e) \
  168. { \
  169. .name = (_name), \
  170. .shared = (_shared), \
  171. .read = iio_enum_read, \
  172. .write = iio_enum_write, \
  173. .private = (uintptr_t)(_e), \
  174. }
  175. /**
  176. * IIO_ENUM_AVAILABLE() - Initialize enum available extended channel attribute
  177. * @_name: Attribute name ("_available" will be appended to the name)
  178. * @_e: Pointer to an iio_enum struct
  179. *
  180. * Creates a read only attribute which lists all the available enum items in a
  181. * space separated list. This should usually be used together with IIO_ENUM()
  182. */
  183. #define IIO_ENUM_AVAILABLE(_name, _e) \
  184. { \
  185. .name = (_name "_available"), \
  186. .shared = true, \
  187. .read = iio_enum_available_read, \
  188. .private = (uintptr_t)(_e), \
  189. }
  190. /**
  191. * struct iio_chan_spec - specification of a single channel
  192. * @type: What type of measurement is the channel making.
  193. * @channel: What number do we wish to assign the channel.
  194. * @channel2: If there is a second number for a differential
  195. * channel then this is it. If modified is set then the
  196. * value here specifies the modifier.
  197. * @address: Driver specific identifier.
  198. * @scan_index: Monotonic index to give ordering in scans when read
  199. * from a buffer.
  200. * @scan_type: Sign: 's' or 'u' to specify signed or unsigned
  201. * realbits: Number of valid bits of data
  202. * storage_bits: Realbits + padding
  203. * shift: Shift right by this before masking out
  204. * realbits.
  205. * endianness: little or big endian
  206. * @info_mask: What information is to be exported about this channel.
  207. * This includes calibbias, scale etc.
  208. * @event_mask: What events can this channel produce.
  209. * @ext_info: Array of extended info attributes for this channel.
  210. * The array is NULL terminated, the last element should
  211. * have its name field set to NULL.
  212. * @extend_name: Allows labeling of channel attributes with an
  213. * informative name. Note this has no effect codes etc,
  214. * unlike modifiers.
  215. * @datasheet_name: A name used in in-kernel mapping of channels. It should
  216. * correspond to the first name that the channel is referred
  217. * to by in the datasheet (e.g. IND), or the nearest
  218. * possible compound name (e.g. IND-INC).
  219. * @modified: Does a modifier apply to this channel. What these are
  220. * depends on the channel type. Modifier is set in
  221. * channel2. Examples are IIO_MOD_X for axial sensors about
  222. * the 'x' axis.
  223. * @indexed: Specify the channel has a numerical index. If not,
  224. * the channel index number will be suppressed for sysfs
  225. * attributes but not for event codes.
  226. * @output: Channel is output.
  227. * @differential: Channel is differential.
  228. */
  229. struct iio_chan_spec {
  230. enum iio_chan_type type;
  231. int channel;
  232. int channel2;
  233. unsigned long address;
  234. int scan_index;
  235. struct {
  236. char sign;
  237. u8 realbits;
  238. u8 storagebits;
  239. u8 shift;
  240. enum iio_endian endianness;
  241. } scan_type;
  242. long info_mask;
  243. long event_mask;
  244. const struct iio_chan_spec_ext_info *ext_info;
  245. const char *extend_name;
  246. const char *datasheet_name;
  247. unsigned modified:1;
  248. unsigned indexed:1;
  249. unsigned output:1;
  250. unsigned differential:1;
  251. };
  252. /**
  253. * iio_channel_has_info() - Checks whether a channel supports a info attribute
  254. * @chan: The channel to be queried
  255. * @type: Type of the info attribute to be checked
  256. *
  257. * Returns true if the channels supports reporting values for the given info
  258. * attribute type, false otherwise.
  259. */
  260. static inline bool iio_channel_has_info(const struct iio_chan_spec *chan,
  261. enum iio_chan_info_enum type)
  262. {
  263. return chan->info_mask & IIO_CHAN_INFO_BITS(type);
  264. }
  265. #define IIO_ST(si, rb, sb, sh) \
  266. { .sign = si, .realbits = rb, .storagebits = sb, .shift = sh }
  267. #define IIO_CHAN_SOFT_TIMESTAMP(_si) \
  268. { .type = IIO_TIMESTAMP, .channel = -1, \
  269. .scan_index = _si, .scan_type = IIO_ST('s', 64, 64, 0) }
  270. /**
  271. * iio_get_time_ns() - utility function to get a time stamp for events etc
  272. **/
  273. static inline s64 iio_get_time_ns(void)
  274. {
  275. struct timespec ts;
  276. /*
  277. * calls getnstimeofday.
  278. * If hrtimers then up to ns accurate, if not microsecond.
  279. */
  280. ktime_get_real_ts(&ts);
  281. return timespec_to_ns(&ts);
  282. }
  283. /* Device operating modes */
  284. #define INDIO_DIRECT_MODE 0x01
  285. #define INDIO_BUFFER_TRIGGERED 0x02
  286. #define INDIO_BUFFER_HARDWARE 0x08
  287. #define INDIO_ALL_BUFFER_MODES \
  288. (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE)
  289. struct iio_trigger; /* forward declaration */
  290. struct iio_dev;
  291. /**
  292. * struct iio_info - constant information about device
  293. * @driver_module: module structure used to ensure correct
  294. * ownership of chrdevs etc
  295. * @event_attrs: event control attributes
  296. * @attrs: general purpose device attributes
  297. * @read_raw: function to request a value from the device.
  298. * mask specifies which value. Note 0 means a reading of
  299. * the channel in question. Return value will specify the
  300. * type of value returned by the device. val and val2 will
  301. * contain the elements making up the returned value.
  302. * @write_raw: function to write a value to the device.
  303. * Parameters are the same as for read_raw.
  304. * @write_raw_get_fmt: callback function to query the expected
  305. * format/precision. If not set by the driver, write_raw
  306. * returns IIO_VAL_INT_PLUS_MICRO.
  307. * @read_event_config: find out if the event is enabled.
  308. * @write_event_config: set if the event is enabled.
  309. * @read_event_value: read a value associated with the event. Meaning
  310. * is event dependant. event_code specifies which event.
  311. * @write_event_value: write the value associated with the event.
  312. * Meaning is event dependent.
  313. * @validate_trigger: function to validate the trigger when the
  314. * current trigger gets changed.
  315. * @update_scan_mode: function to configure device and scan buffer when
  316. * channels have changed
  317. * @debugfs_reg_access: function to read or write register value of device
  318. **/
  319. struct iio_info {
  320. struct module *driver_module;
  321. struct attribute_group *event_attrs;
  322. const struct attribute_group *attrs;
  323. int (*read_raw)(struct iio_dev *indio_dev,
  324. struct iio_chan_spec const *chan,
  325. int *val,
  326. int *val2,
  327. long mask);
  328. int (*write_raw)(struct iio_dev *indio_dev,
  329. struct iio_chan_spec const *chan,
  330. int val,
  331. int val2,
  332. long mask);
  333. int (*write_raw_get_fmt)(struct iio_dev *indio_dev,
  334. struct iio_chan_spec const *chan,
  335. long mask);
  336. int (*read_event_config)(struct iio_dev *indio_dev,
  337. u64 event_code);
  338. int (*write_event_config)(struct iio_dev *indio_dev,
  339. u64 event_code,
  340. int state);
  341. int (*read_event_value)(struct iio_dev *indio_dev,
  342. u64 event_code,
  343. int *val);
  344. int (*write_event_value)(struct iio_dev *indio_dev,
  345. u64 event_code,
  346. int val);
  347. int (*validate_trigger)(struct iio_dev *indio_dev,
  348. struct iio_trigger *trig);
  349. int (*update_scan_mode)(struct iio_dev *indio_dev,
  350. const unsigned long *scan_mask);
  351. int (*debugfs_reg_access)(struct iio_dev *indio_dev,
  352. unsigned reg, unsigned writeval,
  353. unsigned *readval);
  354. };
  355. /**
  356. * struct iio_buffer_setup_ops - buffer setup related callbacks
  357. * @preenable: [DRIVER] function to run prior to marking buffer enabled
  358. * @postenable: [DRIVER] function to run after marking buffer enabled
  359. * @predisable: [DRIVER] function to run prior to marking buffer
  360. * disabled
  361. * @postdisable: [DRIVER] function to run after marking buffer disabled
  362. * @validate_scan_mask: [DRIVER] function callback to check whether a given
  363. * scan mask is valid for the device.
  364. */
  365. struct iio_buffer_setup_ops {
  366. int (*preenable)(struct iio_dev *);
  367. int (*postenable)(struct iio_dev *);
  368. int (*predisable)(struct iio_dev *);
  369. int (*postdisable)(struct iio_dev *);
  370. bool (*validate_scan_mask)(struct iio_dev *indio_dev,
  371. const unsigned long *scan_mask);
  372. };
  373. /**
  374. * struct iio_dev - industrial I/O device
  375. * @id: [INTERN] used to identify device internally
  376. * @modes: [DRIVER] operating modes supported by device
  377. * @currentmode: [DRIVER] current operating mode
  378. * @dev: [DRIVER] device structure, should be assigned a parent
  379. * and owner
  380. * @event_interface: [INTERN] event chrdevs associated with interrupt lines
  381. * @buffer: [DRIVER] any buffer present
  382. * @scan_bytes: [INTERN] num bytes captured to be fed to buffer demux
  383. * @mlock: [INTERN] lock used to prevent simultaneous device state
  384. * changes
  385. * @available_scan_masks: [DRIVER] optional array of allowed bitmasks
  386. * @masklength: [INTERN] the length of the mask established from
  387. * channels
  388. * @active_scan_mask: [INTERN] union of all scan masks requested by buffers
  389. * @scan_timestamp: [INTERN] set if any buffers have requested timestamp
  390. * @scan_index_timestamp:[INTERN] cache of the index to the timestamp
  391. * @trig: [INTERN] current device trigger (buffer modes)
  392. * @pollfunc: [DRIVER] function run on trigger being received
  393. * @channels: [DRIVER] channel specification structure table
  394. * @num_channels: [DRIVER] number of channels specified in @channels.
  395. * @channel_attr_list: [INTERN] keep track of automatically created channel
  396. * attributes
  397. * @chan_attr_group: [INTERN] group for all attrs in base directory
  398. * @name: [DRIVER] name of the device.
  399. * @info: [DRIVER] callbacks and constant info from driver
  400. * @info_exist_lock: [INTERN] lock to prevent use during removal
  401. * @setup_ops: [DRIVER] callbacks to call before and after buffer
  402. * enable/disable
  403. * @chrdev: [INTERN] associated character device
  404. * @groups: [INTERN] attribute groups
  405. * @groupcounter: [INTERN] index of next attribute group
  406. * @flags: [INTERN] file ops related flags including busy flag.
  407. * @debugfs_dentry: [INTERN] device specific debugfs dentry.
  408. * @cached_reg_addr: [INTERN] cached register address for debugfs reads.
  409. */
  410. struct iio_dev {
  411. int id;
  412. int modes;
  413. int currentmode;
  414. struct device dev;
  415. struct iio_event_interface *event_interface;
  416. struct iio_buffer *buffer;
  417. int scan_bytes;
  418. struct mutex mlock;
  419. const unsigned long *available_scan_masks;
  420. unsigned masklength;
  421. const unsigned long *active_scan_mask;
  422. bool scan_timestamp;
  423. unsigned scan_index_timestamp;
  424. struct iio_trigger *trig;
  425. struct iio_poll_func *pollfunc;
  426. struct iio_chan_spec const *channels;
  427. int num_channels;
  428. struct list_head channel_attr_list;
  429. struct attribute_group chan_attr_group;
  430. const char *name;
  431. const struct iio_info *info;
  432. struct mutex info_exist_lock;
  433. const struct iio_buffer_setup_ops *setup_ops;
  434. struct cdev chrdev;
  435. #define IIO_MAX_GROUPS 6
  436. const struct attribute_group *groups[IIO_MAX_GROUPS + 1];
  437. int groupcounter;
  438. unsigned long flags;
  439. #if defined(CONFIG_DEBUG_FS)
  440. struct dentry *debugfs_dentry;
  441. unsigned cached_reg_addr;
  442. #endif
  443. };
  444. /**
  445. * iio_find_channel_from_si() - get channel from its scan index
  446. * @indio_dev: device
  447. * @si: scan index to match
  448. */
  449. const struct iio_chan_spec
  450. *iio_find_channel_from_si(struct iio_dev *indio_dev, int si);
  451. /**
  452. * iio_device_register() - register a device with the IIO subsystem
  453. * @indio_dev: Device structure filled by the device driver
  454. **/
  455. int iio_device_register(struct iio_dev *indio_dev);
  456. /**
  457. * iio_device_unregister() - unregister a device from the IIO subsystem
  458. * @indio_dev: Device structure representing the device.
  459. **/
  460. void iio_device_unregister(struct iio_dev *indio_dev);
  461. /**
  462. * iio_push_event() - try to add event to the list for userspace reading
  463. * @indio_dev: IIO device structure
  464. * @ev_code: What event
  465. * @timestamp: When the event occurred
  466. **/
  467. int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp);
  468. extern struct bus_type iio_bus_type;
  469. /**
  470. * iio_device_put() - reference counted deallocation of struct device
  471. * @indio_dev: IIO device structure containing the device
  472. **/
  473. static inline void iio_device_put(struct iio_dev *indio_dev)
  474. {
  475. if (indio_dev)
  476. put_device(&indio_dev->dev);
  477. };
  478. /**
  479. * dev_to_iio_dev() - Get IIO device struct from a device struct
  480. * @dev: The device embedded in the IIO device
  481. *
  482. * Note: The device must be a IIO device, otherwise the result is undefined.
  483. */
  484. static inline struct iio_dev *dev_to_iio_dev(struct device *dev)
  485. {
  486. return container_of(dev, struct iio_dev, dev);
  487. }
  488. /**
  489. * iio_device_get() - increment reference count for the device
  490. * @indio_dev: IIO device structure
  491. *
  492. * Returns: The passed IIO device
  493. **/
  494. static inline struct iio_dev *iio_device_get(struct iio_dev *indio_dev)
  495. {
  496. return indio_dev ? dev_to_iio_dev(get_device(&indio_dev->dev)) : NULL;
  497. }
  498. /**
  499. * iio_device_set_drvdata() - Set device driver data
  500. * @indio_dev: IIO device structure
  501. * @data: Driver specific data
  502. *
  503. * Allows to attach an arbitrary pointer to an IIO device, which can later be
  504. * retrieved by iio_device_get_drvdata().
  505. */
  506. static inline void iio_device_set_drvdata(struct iio_dev *indio_dev, void *data)
  507. {
  508. dev_set_drvdata(&indio_dev->dev, data);
  509. }
  510. /**
  511. * iio_device_get_drvdata() - Get device driver data
  512. * @indio_dev: IIO device structure
  513. *
  514. * Returns the data previously set with iio_device_set_drvdata()
  515. */
  516. static inline void *iio_device_get_drvdata(struct iio_dev *indio_dev)
  517. {
  518. return dev_get_drvdata(&indio_dev->dev);
  519. }
  520. /* Can we make this smaller? */
  521. #define IIO_ALIGN L1_CACHE_BYTES
  522. /**
  523. * iio_device_alloc() - allocate an iio_dev from a driver
  524. * @sizeof_priv: Space to allocate for private structure.
  525. **/
  526. struct iio_dev *iio_device_alloc(int sizeof_priv);
  527. static inline void *iio_priv(const struct iio_dev *indio_dev)
  528. {
  529. return (char *)indio_dev + ALIGN(sizeof(struct iio_dev), IIO_ALIGN);
  530. }
  531. static inline struct iio_dev *iio_priv_to_dev(void *priv)
  532. {
  533. return (struct iio_dev *)((char *)priv -
  534. ALIGN(sizeof(struct iio_dev), IIO_ALIGN));
  535. }
  536. /**
  537. * iio_device_free() - free an iio_dev from a driver
  538. * @indio_dev: the iio_dev associated with the device
  539. **/
  540. void iio_device_free(struct iio_dev *indio_dev);
  541. /**
  542. * iio_buffer_enabled() - helper function to test if the buffer is enabled
  543. * @indio_dev: IIO device structure for device
  544. **/
  545. static inline bool iio_buffer_enabled(struct iio_dev *indio_dev)
  546. {
  547. return indio_dev->currentmode
  548. & (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE);
  549. };
  550. /**
  551. * iio_get_debugfs_dentry() - helper function to get the debugfs_dentry
  552. * @indio_dev: IIO device structure for device
  553. **/
  554. #if defined(CONFIG_DEBUG_FS)
  555. static inline struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
  556. {
  557. return indio_dev->debugfs_dentry;
  558. };
  559. #else
  560. static inline struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
  561. {
  562. return NULL;
  563. };
  564. #endif
  565. /**
  566. * IIO_DEGREE_TO_RAD() - Convert degree to rad
  567. * @deg: A value in degree
  568. *
  569. * Returns the given value converted from degree to rad
  570. */
  571. #define IIO_DEGREE_TO_RAD(deg) (((deg) * 314159ULL + 9000000ULL) / 18000000ULL)
  572. /**
  573. * IIO_G_TO_M_S_2() - Convert g to meter / second**2
  574. * @g: A value in g
  575. *
  576. * Returns the given value converted from g to meter / second**2
  577. */
  578. #define IIO_G_TO_M_S_2(g) ((g) * 980665ULL / 100000ULL)
  579. #endif /* _INDUSTRIAL_IO_H_ */