iio.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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_enum - Enum channel info attribute
  125. * items: An array of strings.
  126. * num_items: Length of the item array.
  127. * set: Set callback function, may be NULL.
  128. * get: Get callback function, may be NULL.
  129. *
  130. * The iio_enum struct can be used to implement enum style channel attributes.
  131. * Enum style attributes are those which have a set of strings which map to
  132. * unsigned integer values. The IIO enum helper code takes care of mapping
  133. * between value and string as well as generating a "_available" file which
  134. * contains a list of all available items. The set callback will be called when
  135. * the attribute is updated. The last parameter is the index to the newly
  136. * activated item. The get callback will be used to query the currently active
  137. * item and is supposed to return the index for it.
  138. */
  139. struct iio_enum {
  140. const char * const *items;
  141. unsigned int num_items;
  142. int (*set)(struct iio_dev *, const struct iio_chan_spec *, unsigned int);
  143. int (*get)(struct iio_dev *, const struct iio_chan_spec *);
  144. };
  145. ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
  146. uintptr_t priv, const struct iio_chan_spec *chan, char *buf);
  147. ssize_t iio_enum_read(struct iio_dev *indio_dev,
  148. uintptr_t priv, const struct iio_chan_spec *chan, char *buf);
  149. ssize_t iio_enum_write(struct iio_dev *indio_dev,
  150. uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
  151. size_t len);
  152. /**
  153. * IIO_ENUM() - Initialize enum extended channel attribute
  154. * @_name: Attribute name
  155. * @_shared: Whether the attribute is shared between all channels
  156. * @_e: Pointer to a iio_enum struct
  157. *
  158. * This should usually be used together with IIO_ENUM_AVAILABLE()
  159. */
  160. #define IIO_ENUM(_name, _shared, _e) \
  161. { \
  162. .name = (_name), \
  163. .shared = (_shared), \
  164. .read = iio_enum_read, \
  165. .write = iio_enum_write, \
  166. .private = (uintptr_t)(_e), \
  167. }
  168. /**
  169. * IIO_ENUM_AVAILABLE() - Initialize enum available extended channel attribute
  170. * @_name: Attribute name ("_available" will be appended to the name)
  171. * @_e: Pointer to a iio_enum struct
  172. *
  173. * Creates a read only attribute which list all the available enum items in a
  174. * space separated list. This should usually be used together with IIO_ENUM()
  175. */
  176. #define IIO_ENUM_AVAILABLE(_name, _e) \
  177. { \
  178. .name = (_name "_available"), \
  179. .shared = true, \
  180. .read = iio_enum_available_read, \
  181. .private = (uintptr_t)(_e), \
  182. }
  183. /**
  184. * struct iio_chan_spec - specification of a single channel
  185. * @type: What type of measurement is the channel making.
  186. * @channel: What number or name do we wish to assign the channel.
  187. * @channel2: If there is a second number for a differential
  188. * channel then this is it. If modified is set then the
  189. * value here specifies the modifier.
  190. * @address: Driver specific identifier.
  191. * @scan_index: Monotonic index to give ordering in scans when read
  192. * from a buffer.
  193. * @scan_type: Sign: 's' or 'u' to specify signed or unsigned
  194. * realbits: Number of valid bits of data
  195. * storage_bits: Realbits + padding
  196. * shift: Shift right by this before masking out
  197. * realbits.
  198. * endianness: little or big endian
  199. * @info_mask: What information is to be exported about this channel.
  200. * This includes calibbias, scale etc.
  201. * @event_mask: What events can this channel produce.
  202. * @ext_info: Array of extended info attributes for this channel.
  203. * The array is NULL terminated, the last element should
  204. * have it's name field set to NULL.
  205. * @extend_name: Allows labeling of channel attributes with an
  206. * informative name. Note this has no effect codes etc,
  207. * unlike modifiers.
  208. * @datasheet_name: A name used in in-kernel mapping of channels. It should
  209. * correspond to the first name that the channel is referred
  210. * to by in the datasheet (e.g. IND), or the nearest
  211. * possible compound name (e.g. IND-INC).
  212. * @modified: Does a modifier apply to this channel. What these are
  213. * depends on the channel type. Modifier is set in
  214. * channel2. Examples are IIO_MOD_X for axial sensors about
  215. * the 'x' axis.
  216. * @indexed: Specify the channel has a numerical index. If not,
  217. * the value in channel will be suppressed for attribute
  218. * but not for event codes. Typically set it to 0 when
  219. * the index is false.
  220. * @differential: Channel is differential.
  221. */
  222. struct iio_chan_spec {
  223. enum iio_chan_type type;
  224. int channel;
  225. int channel2;
  226. unsigned long address;
  227. int scan_index;
  228. struct {
  229. char sign;
  230. u8 realbits;
  231. u8 storagebits;
  232. u8 shift;
  233. enum iio_endian endianness;
  234. } scan_type;
  235. long info_mask;
  236. long event_mask;
  237. const struct iio_chan_spec_ext_info *ext_info;
  238. const char *extend_name;
  239. const char *datasheet_name;
  240. unsigned modified:1;
  241. unsigned indexed:1;
  242. unsigned output:1;
  243. unsigned differential:1;
  244. };
  245. #define IIO_ST(si, rb, sb, sh) \
  246. { .sign = si, .realbits = rb, .storagebits = sb, .shift = sh }
  247. #define IIO_CHAN_SOFT_TIMESTAMP(_si) \
  248. { .type = IIO_TIMESTAMP, .channel = -1, \
  249. .scan_index = _si, .scan_type = IIO_ST('s', 64, 64, 0) }
  250. /**
  251. * iio_get_time_ns() - utility function to get a time stamp for events etc
  252. **/
  253. static inline s64 iio_get_time_ns(void)
  254. {
  255. struct timespec ts;
  256. /*
  257. * calls getnstimeofday.
  258. * If hrtimers then up to ns accurate, if not microsecond.
  259. */
  260. ktime_get_real_ts(&ts);
  261. return timespec_to_ns(&ts);
  262. }
  263. /* Device operating modes */
  264. #define INDIO_DIRECT_MODE 0x01
  265. #define INDIO_BUFFER_TRIGGERED 0x02
  266. #define INDIO_BUFFER_HARDWARE 0x08
  267. #define INDIO_ALL_BUFFER_MODES \
  268. (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE)
  269. struct iio_trigger; /* forward declaration */
  270. struct iio_dev;
  271. /**
  272. * struct iio_info - constant information about device
  273. * @driver_module: module structure used to ensure correct
  274. * ownership of chrdevs etc
  275. * @event_attrs: event control attributes
  276. * @attrs: general purpose device attributes
  277. * @read_raw: function to request a value from the device.
  278. * mask specifies which value. Note 0 means a reading of
  279. * the channel in question. Return value will specify the
  280. * type of value returned by the device. val and val2 will
  281. * contain the elements making up the returned value.
  282. * @write_raw: function to write a value to the device.
  283. * Parameters are the same as for read_raw.
  284. * @write_raw_get_fmt: callback function to query the expected
  285. * format/precision. If not set by the driver, write_raw
  286. * returns IIO_VAL_INT_PLUS_MICRO.
  287. * @read_event_config: find out if the event is enabled.
  288. * @write_event_config: set if the event is enabled.
  289. * @read_event_value: read a value associated with the event. Meaning
  290. * is event dependant. event_code specifies which event.
  291. * @write_event_value: write the value associated with the event.
  292. * Meaning is event dependent.
  293. * @validate_trigger: function to validate the trigger when the
  294. * current trigger gets changed.
  295. **/
  296. struct iio_info {
  297. struct module *driver_module;
  298. struct attribute_group *event_attrs;
  299. const struct attribute_group *attrs;
  300. int (*read_raw)(struct iio_dev *indio_dev,
  301. struct iio_chan_spec const *chan,
  302. int *val,
  303. int *val2,
  304. long mask);
  305. int (*write_raw)(struct iio_dev *indio_dev,
  306. struct iio_chan_spec const *chan,
  307. int val,
  308. int val2,
  309. long mask);
  310. int (*write_raw_get_fmt)(struct iio_dev *indio_dev,
  311. struct iio_chan_spec const *chan,
  312. long mask);
  313. int (*read_event_config)(struct iio_dev *indio_dev,
  314. u64 event_code);
  315. int (*write_event_config)(struct iio_dev *indio_dev,
  316. u64 event_code,
  317. int state);
  318. int (*read_event_value)(struct iio_dev *indio_dev,
  319. u64 event_code,
  320. int *val);
  321. int (*write_event_value)(struct iio_dev *indio_dev,
  322. u64 event_code,
  323. int val);
  324. int (*validate_trigger)(struct iio_dev *indio_dev,
  325. struct iio_trigger *trig);
  326. int (*update_scan_mode)(struct iio_dev *indio_dev,
  327. const unsigned long *scan_mask);
  328. int (*debugfs_reg_access)(struct iio_dev *indio_dev,
  329. unsigned reg, unsigned writeval,
  330. unsigned *readval);
  331. };
  332. /**
  333. * struct iio_buffer_setup_ops - buffer setup related callbacks
  334. * @preenable: [DRIVER] function to run prior to marking buffer enabled
  335. * @postenable: [DRIVER] function to run after marking buffer enabled
  336. * @predisable: [DRIVER] function to run prior to marking buffer
  337. * disabled
  338. * @postdisable: [DRIVER] function to run after marking buffer disabled
  339. */
  340. struct iio_buffer_setup_ops {
  341. int (*preenable)(struct iio_dev *);
  342. int (*postenable)(struct iio_dev *);
  343. int (*predisable)(struct iio_dev *);
  344. int (*postdisable)(struct iio_dev *);
  345. };
  346. /**
  347. * struct iio_dev - industrial I/O device
  348. * @id: [INTERN] used to identify device internally
  349. * @modes: [DRIVER] operating modes supported by device
  350. * @currentmode: [DRIVER] current operating mode
  351. * @dev: [DRIVER] device structure, should be assigned a parent
  352. * and owner
  353. * @event_interface: [INTERN] event chrdevs associated with interrupt lines
  354. * @buffer: [DRIVER] any buffer present
  355. * @scan_bytes: [INTERN] num bytes captured to be fed to buffer demux
  356. * @mlock: [INTERN] lock used to prevent simultaneous device state
  357. * changes
  358. * @available_scan_masks: [DRIVER] optional array of allowed bitmasks
  359. * @masklength: [INTERN] the length of the mask established from
  360. * channels
  361. * @active_scan_mask: [INTERN] union of all scan masks requested by buffers
  362. * @scan_timestamp: [INTERN] set if any buffers have requested timestamp
  363. * @scan_index_timestamp:[INTERN] cache of the index to the timestamp
  364. * @trig: [INTERN] current device trigger (buffer modes)
  365. * @pollfunc: [DRIVER] function run on trigger being received
  366. * @channels: [DRIVER] channel specification structure table
  367. * @num_channels: [DRIVER] number of channels specified in @channels.
  368. * @channel_attr_list: [INTERN] keep track of automatically created channel
  369. * attributes
  370. * @chan_attr_group: [INTERN] group for all attrs in base directory
  371. * @name: [DRIVER] name of the device.
  372. * @info: [DRIVER] callbacks and constant info from driver
  373. * @info_exist_lock: [INTERN] lock to prevent use during removal
  374. * @setup_ops: [DRIVER] callbacks to call before and after buffer
  375. * enable/disable
  376. * @chrdev: [INTERN] associated character device
  377. * @groups: [INTERN] attribute groups
  378. * @groupcounter: [INTERN] index of next attribute group
  379. * @flags: [INTERN] file ops related flags including busy flag.
  380. * @debugfs_dentry: [INTERN] device specific debugfs dentry.
  381. * @cached_reg_addr: [INTERN] cached register address for debugfs reads.
  382. */
  383. struct iio_dev {
  384. int id;
  385. int modes;
  386. int currentmode;
  387. struct device dev;
  388. struct iio_event_interface *event_interface;
  389. struct iio_buffer *buffer;
  390. int scan_bytes;
  391. struct mutex mlock;
  392. const unsigned long *available_scan_masks;
  393. unsigned masklength;
  394. const unsigned long *active_scan_mask;
  395. bool scan_timestamp;
  396. unsigned scan_index_timestamp;
  397. struct iio_trigger *trig;
  398. struct iio_poll_func *pollfunc;
  399. struct iio_chan_spec const *channels;
  400. int num_channels;
  401. struct list_head channel_attr_list;
  402. struct attribute_group chan_attr_group;
  403. const char *name;
  404. const struct iio_info *info;
  405. struct mutex info_exist_lock;
  406. const struct iio_buffer_setup_ops *setup_ops;
  407. struct cdev chrdev;
  408. #define IIO_MAX_GROUPS 6
  409. const struct attribute_group *groups[IIO_MAX_GROUPS + 1];
  410. int groupcounter;
  411. unsigned long flags;
  412. #if defined(CONFIG_DEBUG_FS)
  413. struct dentry *debugfs_dentry;
  414. unsigned cached_reg_addr;
  415. #endif
  416. };
  417. /**
  418. * iio_find_channel_from_si() - get channel from its scan index
  419. * @indio_dev: device
  420. * @si: scan index to match
  421. */
  422. const struct iio_chan_spec
  423. *iio_find_channel_from_si(struct iio_dev *indio_dev, int si);
  424. /**
  425. * iio_device_register() - register a device with the IIO subsystem
  426. * @indio_dev: Device structure filled by the device driver
  427. **/
  428. int iio_device_register(struct iio_dev *indio_dev);
  429. /**
  430. * iio_device_unregister() - unregister a device from the IIO subsystem
  431. * @indio_dev: Device structure representing the device.
  432. **/
  433. void iio_device_unregister(struct iio_dev *indio_dev);
  434. /**
  435. * iio_push_event() - try to add event to the list for userspace reading
  436. * @indio_dev: IIO device structure
  437. * @ev_code: What event
  438. * @timestamp: When the event occurred
  439. **/
  440. int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp);
  441. extern struct bus_type iio_bus_type;
  442. /**
  443. * iio_device_put() - reference counted deallocation of struct device
  444. * @dev: the iio_device containing the device
  445. **/
  446. static inline void iio_device_put(struct iio_dev *indio_dev)
  447. {
  448. if (indio_dev)
  449. put_device(&indio_dev->dev);
  450. };
  451. /**
  452. * dev_to_iio_dev() - Get IIO device struct from a device struct
  453. * @dev: The device embedded in the IIO device
  454. *
  455. * Note: The device must be a IIO device, otherwise the result is undefined.
  456. */
  457. static inline struct iio_dev *dev_to_iio_dev(struct device *dev)
  458. {
  459. return container_of(dev, struct iio_dev, dev);
  460. }
  461. /**
  462. * iio_device_get() - increment reference count for the device
  463. * @indio_dev: IIO device structure
  464. *
  465. * Returns: The passed IIO device
  466. **/
  467. static inline struct iio_dev *iio_device_get(struct iio_dev *indio_dev)
  468. {
  469. return indio_dev ? dev_to_iio_dev(get_device(&indio_dev->dev)) : NULL;
  470. }
  471. /* Can we make this smaller? */
  472. #define IIO_ALIGN L1_CACHE_BYTES
  473. /**
  474. * iio_device_alloc() - allocate an iio_dev from a driver
  475. * @sizeof_priv: Space to allocate for private structure.
  476. **/
  477. struct iio_dev *iio_device_alloc(int sizeof_priv);
  478. static inline void *iio_priv(const struct iio_dev *indio_dev)
  479. {
  480. return (char *)indio_dev + ALIGN(sizeof(struct iio_dev), IIO_ALIGN);
  481. }
  482. static inline struct iio_dev *iio_priv_to_dev(void *priv)
  483. {
  484. return (struct iio_dev *)((char *)priv -
  485. ALIGN(sizeof(struct iio_dev), IIO_ALIGN));
  486. }
  487. /**
  488. * iio_device_free() - free an iio_dev from a driver
  489. * @dev: the iio_dev associated with the device
  490. **/
  491. void iio_device_free(struct iio_dev *indio_dev);
  492. /**
  493. * iio_buffer_enabled() - helper function to test if the buffer is enabled
  494. * @indio_dev: IIO device info structure for device
  495. **/
  496. static inline bool iio_buffer_enabled(struct iio_dev *indio_dev)
  497. {
  498. return indio_dev->currentmode
  499. & (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE);
  500. };
  501. /**
  502. * iio_get_debugfs_dentry() - helper function to get the debugfs_dentry
  503. * @indio_dev: IIO device info structure for device
  504. **/
  505. #if defined(CONFIG_DEBUG_FS)
  506. static inline struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
  507. {
  508. return indio_dev->debugfs_dentry;
  509. };
  510. #else
  511. static inline struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
  512. {
  513. return NULL;
  514. };
  515. #endif
  516. #endif /* _INDUSTRIAL_IO_H_ */