iio.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. * @info_mask_separate: What information is to be exported that is specific to
  209. * this channel.
  210. * @info_mask_shared_by_type: What information is to be exported that is shared
  211. * by all channels of the same type.
  212. * @event_mask: What events can this channel produce.
  213. * @ext_info: Array of extended info attributes for this channel.
  214. * The array is NULL terminated, the last element should
  215. * have its name field set to NULL.
  216. * @extend_name: Allows labeling of channel attributes with an
  217. * informative name. Note this has no effect codes etc,
  218. * unlike modifiers.
  219. * @datasheet_name: A name used in in-kernel mapping of channels. It should
  220. * correspond to the first name that the channel is referred
  221. * to by in the datasheet (e.g. IND), or the nearest
  222. * possible compound name (e.g. IND-INC).
  223. * @modified: Does a modifier apply to this channel. What these are
  224. * depends on the channel type. Modifier is set in
  225. * channel2. Examples are IIO_MOD_X for axial sensors about
  226. * the 'x' axis.
  227. * @indexed: Specify the channel has a numerical index. If not,
  228. * the channel index number will be suppressed for sysfs
  229. * attributes but not for event codes.
  230. * @output: Channel is output.
  231. * @differential: Channel is differential.
  232. */
  233. struct iio_chan_spec {
  234. enum iio_chan_type type;
  235. int channel;
  236. int channel2;
  237. unsigned long address;
  238. int scan_index;
  239. struct {
  240. char sign;
  241. u8 realbits;
  242. u8 storagebits;
  243. u8 shift;
  244. enum iio_endian endianness;
  245. } scan_type;
  246. long info_mask;
  247. long info_mask_separate;
  248. long info_mask_shared_by_type;
  249. long event_mask;
  250. const struct iio_chan_spec_ext_info *ext_info;
  251. const char *extend_name;
  252. const char *datasheet_name;
  253. unsigned modified:1;
  254. unsigned indexed:1;
  255. unsigned output:1;
  256. unsigned differential:1;
  257. };
  258. /**
  259. * iio_channel_has_info() - Checks whether a channel supports a info attribute
  260. * @chan: The channel to be queried
  261. * @type: Type of the info attribute to be checked
  262. *
  263. * Returns true if the channels supports reporting values for the given info
  264. * attribute type, false otherwise.
  265. */
  266. static inline bool iio_channel_has_info(const struct iio_chan_spec *chan,
  267. enum iio_chan_info_enum type)
  268. {
  269. return (chan->info_mask & IIO_CHAN_INFO_BITS(type)) |
  270. (chan->info_mask_separate & type) |
  271. (chan->info_mask_shared_by_type & type);
  272. }
  273. #define IIO_ST(si, rb, sb, sh) \
  274. { .sign = si, .realbits = rb, .storagebits = sb, .shift = sh }
  275. #define IIO_CHAN_SOFT_TIMESTAMP(_si) \
  276. { .type = IIO_TIMESTAMP, .channel = -1, \
  277. .scan_index = _si, .scan_type = IIO_ST('s', 64, 64, 0) }
  278. /**
  279. * iio_get_time_ns() - utility function to get a time stamp for events etc
  280. **/
  281. static inline s64 iio_get_time_ns(void)
  282. {
  283. struct timespec ts;
  284. /*
  285. * calls getnstimeofday.
  286. * If hrtimers then up to ns accurate, if not microsecond.
  287. */
  288. ktime_get_real_ts(&ts);
  289. return timespec_to_ns(&ts);
  290. }
  291. /* Device operating modes */
  292. #define INDIO_DIRECT_MODE 0x01
  293. #define INDIO_BUFFER_TRIGGERED 0x02
  294. #define INDIO_BUFFER_HARDWARE 0x08
  295. #define INDIO_ALL_BUFFER_MODES \
  296. (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE)
  297. struct iio_trigger; /* forward declaration */
  298. struct iio_dev;
  299. /**
  300. * struct iio_info - constant information about device
  301. * @driver_module: module structure used to ensure correct
  302. * ownership of chrdevs etc
  303. * @event_attrs: event control attributes
  304. * @attrs: general purpose device attributes
  305. * @read_raw: function to request a value from the device.
  306. * mask specifies which value. Note 0 means a reading of
  307. * the channel in question. Return value will specify the
  308. * type of value returned by the device. val and val2 will
  309. * contain the elements making up the returned value.
  310. * @write_raw: function to write a value to the device.
  311. * Parameters are the same as for read_raw.
  312. * @write_raw_get_fmt: callback function to query the expected
  313. * format/precision. If not set by the driver, write_raw
  314. * returns IIO_VAL_INT_PLUS_MICRO.
  315. * @read_event_config: find out if the event is enabled.
  316. * @write_event_config: set if the event is enabled.
  317. * @read_event_value: read a value associated with the event. Meaning
  318. * is event dependant. event_code specifies which event.
  319. * @write_event_value: write the value associated with the event.
  320. * Meaning is event dependent.
  321. * @validate_trigger: function to validate the trigger when the
  322. * current trigger gets changed.
  323. * @update_scan_mode: function to configure device and scan buffer when
  324. * channels have changed
  325. * @debugfs_reg_access: function to read or write register value of device
  326. **/
  327. struct iio_info {
  328. struct module *driver_module;
  329. struct attribute_group *event_attrs;
  330. const struct attribute_group *attrs;
  331. int (*read_raw)(struct iio_dev *indio_dev,
  332. struct iio_chan_spec const *chan,
  333. int *val,
  334. int *val2,
  335. long mask);
  336. int (*write_raw)(struct iio_dev *indio_dev,
  337. struct iio_chan_spec const *chan,
  338. int val,
  339. int val2,
  340. long mask);
  341. int (*write_raw_get_fmt)(struct iio_dev *indio_dev,
  342. struct iio_chan_spec const *chan,
  343. long mask);
  344. int (*read_event_config)(struct iio_dev *indio_dev,
  345. u64 event_code);
  346. int (*write_event_config)(struct iio_dev *indio_dev,
  347. u64 event_code,
  348. int state);
  349. int (*read_event_value)(struct iio_dev *indio_dev,
  350. u64 event_code,
  351. int *val);
  352. int (*write_event_value)(struct iio_dev *indio_dev,
  353. u64 event_code,
  354. int val);
  355. int (*validate_trigger)(struct iio_dev *indio_dev,
  356. struct iio_trigger *trig);
  357. int (*update_scan_mode)(struct iio_dev *indio_dev,
  358. const unsigned long *scan_mask);
  359. int (*debugfs_reg_access)(struct iio_dev *indio_dev,
  360. unsigned reg, unsigned writeval,
  361. unsigned *readval);
  362. };
  363. /**
  364. * struct iio_buffer_setup_ops - buffer setup related callbacks
  365. * @preenable: [DRIVER] function to run prior to marking buffer enabled
  366. * @postenable: [DRIVER] function to run after marking buffer enabled
  367. * @predisable: [DRIVER] function to run prior to marking buffer
  368. * disabled
  369. * @postdisable: [DRIVER] function to run after marking buffer disabled
  370. * @validate_scan_mask: [DRIVER] function callback to check whether a given
  371. * scan mask is valid for the device.
  372. */
  373. struct iio_buffer_setup_ops {
  374. int (*preenable)(struct iio_dev *);
  375. int (*postenable)(struct iio_dev *);
  376. int (*predisable)(struct iio_dev *);
  377. int (*postdisable)(struct iio_dev *);
  378. bool (*validate_scan_mask)(struct iio_dev *indio_dev,
  379. const unsigned long *scan_mask);
  380. };
  381. /**
  382. * struct iio_dev - industrial I/O device
  383. * @id: [INTERN] used to identify device internally
  384. * @modes: [DRIVER] operating modes supported by device
  385. * @currentmode: [DRIVER] current operating mode
  386. * @dev: [DRIVER] device structure, should be assigned a parent
  387. * and owner
  388. * @event_interface: [INTERN] event chrdevs associated with interrupt lines
  389. * @buffer: [DRIVER] any buffer present
  390. * @buffer_list: [INTERN] list of all buffers currently attached
  391. * @scan_bytes: [INTERN] num bytes captured to be fed to buffer demux
  392. * @mlock: [INTERN] lock used to prevent simultaneous device state
  393. * changes
  394. * @available_scan_masks: [DRIVER] optional array of allowed bitmasks
  395. * @masklength: [INTERN] the length of the mask established from
  396. * channels
  397. * @active_scan_mask: [INTERN] union of all scan masks requested by buffers
  398. * @scan_timestamp: [INTERN] set if any buffers have requested timestamp
  399. * @scan_index_timestamp:[INTERN] cache of the index to the timestamp
  400. * @trig: [INTERN] current device trigger (buffer modes)
  401. * @pollfunc: [DRIVER] function run on trigger being received
  402. * @channels: [DRIVER] channel specification structure table
  403. * @num_channels: [DRIVER] number of channels specified in @channels.
  404. * @channel_attr_list: [INTERN] keep track of automatically created channel
  405. * attributes
  406. * @chan_attr_group: [INTERN] group for all attrs in base directory
  407. * @name: [DRIVER] name of the device.
  408. * @info: [DRIVER] callbacks and constant info from driver
  409. * @info_exist_lock: [INTERN] lock to prevent use during removal
  410. * @setup_ops: [DRIVER] callbacks to call before and after buffer
  411. * enable/disable
  412. * @chrdev: [INTERN] associated character device
  413. * @groups: [INTERN] attribute groups
  414. * @groupcounter: [INTERN] index of next attribute group
  415. * @flags: [INTERN] file ops related flags including busy flag.
  416. * @debugfs_dentry: [INTERN] device specific debugfs dentry.
  417. * @cached_reg_addr: [INTERN] cached register address for debugfs reads.
  418. */
  419. struct iio_dev {
  420. int id;
  421. int modes;
  422. int currentmode;
  423. struct device dev;
  424. struct iio_event_interface *event_interface;
  425. struct iio_buffer *buffer;
  426. struct list_head buffer_list;
  427. int scan_bytes;
  428. struct mutex mlock;
  429. const unsigned long *available_scan_masks;
  430. unsigned masklength;
  431. const unsigned long *active_scan_mask;
  432. bool scan_timestamp;
  433. unsigned scan_index_timestamp;
  434. struct iio_trigger *trig;
  435. struct iio_poll_func *pollfunc;
  436. struct iio_chan_spec const *channels;
  437. int num_channels;
  438. struct list_head channel_attr_list;
  439. struct attribute_group chan_attr_group;
  440. const char *name;
  441. const struct iio_info *info;
  442. struct mutex info_exist_lock;
  443. const struct iio_buffer_setup_ops *setup_ops;
  444. struct cdev chrdev;
  445. #define IIO_MAX_GROUPS 6
  446. const struct attribute_group *groups[IIO_MAX_GROUPS + 1];
  447. int groupcounter;
  448. unsigned long flags;
  449. #if defined(CONFIG_DEBUG_FS)
  450. struct dentry *debugfs_dentry;
  451. unsigned cached_reg_addr;
  452. #endif
  453. };
  454. /**
  455. * iio_find_channel_from_si() - get channel from its scan index
  456. * @indio_dev: device
  457. * @si: scan index to match
  458. */
  459. const struct iio_chan_spec
  460. *iio_find_channel_from_si(struct iio_dev *indio_dev, int si);
  461. /**
  462. * iio_device_register() - register a device with the IIO subsystem
  463. * @indio_dev: Device structure filled by the device driver
  464. **/
  465. int iio_device_register(struct iio_dev *indio_dev);
  466. /**
  467. * iio_device_unregister() - unregister a device from the IIO subsystem
  468. * @indio_dev: Device structure representing the device.
  469. **/
  470. void iio_device_unregister(struct iio_dev *indio_dev);
  471. /**
  472. * iio_push_event() - try to add event to the list for userspace reading
  473. * @indio_dev: IIO device structure
  474. * @ev_code: What event
  475. * @timestamp: When the event occurred
  476. **/
  477. int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp);
  478. extern struct bus_type iio_bus_type;
  479. /**
  480. * iio_device_put() - reference counted deallocation of struct device
  481. * @indio_dev: IIO device structure containing the device
  482. **/
  483. static inline void iio_device_put(struct iio_dev *indio_dev)
  484. {
  485. if (indio_dev)
  486. put_device(&indio_dev->dev);
  487. };
  488. /**
  489. * dev_to_iio_dev() - Get IIO device struct from a device struct
  490. * @dev: The device embedded in the IIO device
  491. *
  492. * Note: The device must be a IIO device, otherwise the result is undefined.
  493. */
  494. static inline struct iio_dev *dev_to_iio_dev(struct device *dev)
  495. {
  496. return container_of(dev, struct iio_dev, dev);
  497. }
  498. /**
  499. * iio_device_get() - increment reference count for the device
  500. * @indio_dev: IIO device structure
  501. *
  502. * Returns: The passed IIO device
  503. **/
  504. static inline struct iio_dev *iio_device_get(struct iio_dev *indio_dev)
  505. {
  506. return indio_dev ? dev_to_iio_dev(get_device(&indio_dev->dev)) : NULL;
  507. }
  508. /**
  509. * iio_device_set_drvdata() - Set device driver data
  510. * @indio_dev: IIO device structure
  511. * @data: Driver specific data
  512. *
  513. * Allows to attach an arbitrary pointer to an IIO device, which can later be
  514. * retrieved by iio_device_get_drvdata().
  515. */
  516. static inline void iio_device_set_drvdata(struct iio_dev *indio_dev, void *data)
  517. {
  518. dev_set_drvdata(&indio_dev->dev, data);
  519. }
  520. /**
  521. * iio_device_get_drvdata() - Get device driver data
  522. * @indio_dev: IIO device structure
  523. *
  524. * Returns the data previously set with iio_device_set_drvdata()
  525. */
  526. static inline void *iio_device_get_drvdata(struct iio_dev *indio_dev)
  527. {
  528. return dev_get_drvdata(&indio_dev->dev);
  529. }
  530. /* Can we make this smaller? */
  531. #define IIO_ALIGN L1_CACHE_BYTES
  532. /**
  533. * iio_device_alloc() - allocate an iio_dev from a driver
  534. * @sizeof_priv: Space to allocate for private structure.
  535. **/
  536. struct iio_dev *iio_device_alloc(int sizeof_priv);
  537. static inline void *iio_priv(const struct iio_dev *indio_dev)
  538. {
  539. return (char *)indio_dev + ALIGN(sizeof(struct iio_dev), IIO_ALIGN);
  540. }
  541. static inline struct iio_dev *iio_priv_to_dev(void *priv)
  542. {
  543. return (struct iio_dev *)((char *)priv -
  544. ALIGN(sizeof(struct iio_dev), IIO_ALIGN));
  545. }
  546. /**
  547. * iio_device_free() - free an iio_dev from a driver
  548. * @indio_dev: the iio_dev associated with the device
  549. **/
  550. void iio_device_free(struct iio_dev *indio_dev);
  551. /**
  552. * iio_buffer_enabled() - helper function to test if the buffer is enabled
  553. * @indio_dev: IIO device structure for device
  554. **/
  555. static inline bool iio_buffer_enabled(struct iio_dev *indio_dev)
  556. {
  557. return indio_dev->currentmode
  558. & (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE);
  559. };
  560. /**
  561. * iio_get_debugfs_dentry() - helper function to get the debugfs_dentry
  562. * @indio_dev: IIO device structure for device
  563. **/
  564. #if defined(CONFIG_DEBUG_FS)
  565. static inline struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
  566. {
  567. return indio_dev->debugfs_dentry;
  568. };
  569. #else
  570. static inline struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
  571. {
  572. return NULL;
  573. };
  574. #endif
  575. int iio_str_to_fixpoint(const char *str, int fract_mult, int *integer,
  576. int *fract);
  577. /**
  578. * IIO_DEGREE_TO_RAD() - Convert degree to rad
  579. * @deg: A value in degree
  580. *
  581. * Returns the given value converted from degree to rad
  582. */
  583. #define IIO_DEGREE_TO_RAD(deg) (((deg) * 314159ULL + 9000000ULL) / 18000000ULL)
  584. /**
  585. * IIO_G_TO_M_S_2() - Convert g to meter / second**2
  586. * @g: A value in g
  587. *
  588. * Returns the given value converted from g to meter / second**2
  589. */
  590. #define IIO_G_TO_M_S_2(g) ((g) * 980665ULL / 100000ULL)
  591. #endif /* _INDUSTRIAL_IO_H_ */