iio.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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 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 its 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 channel index number will be suppressed for sysfs
  218. * attributes but not for event codes.
  219. * @output: Channel is output.
  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. * @update_scan_mode: function to configure device and scan buffer when
  296. * channels have changed
  297. * @debugfs_reg_access: function to read or write register value of device
  298. **/
  299. struct iio_info {
  300. struct module *driver_module;
  301. struct attribute_group *event_attrs;
  302. const struct attribute_group *attrs;
  303. int (*read_raw)(struct iio_dev *indio_dev,
  304. struct iio_chan_spec const *chan,
  305. int *val,
  306. int *val2,
  307. long mask);
  308. int (*write_raw)(struct iio_dev *indio_dev,
  309. struct iio_chan_spec const *chan,
  310. int val,
  311. int val2,
  312. long mask);
  313. int (*write_raw_get_fmt)(struct iio_dev *indio_dev,
  314. struct iio_chan_spec const *chan,
  315. long mask);
  316. int (*read_event_config)(struct iio_dev *indio_dev,
  317. u64 event_code);
  318. int (*write_event_config)(struct iio_dev *indio_dev,
  319. u64 event_code,
  320. int state);
  321. int (*read_event_value)(struct iio_dev *indio_dev,
  322. u64 event_code,
  323. int *val);
  324. int (*write_event_value)(struct iio_dev *indio_dev,
  325. u64 event_code,
  326. int val);
  327. int (*validate_trigger)(struct iio_dev *indio_dev,
  328. struct iio_trigger *trig);
  329. int (*update_scan_mode)(struct iio_dev *indio_dev,
  330. const unsigned long *scan_mask);
  331. int (*debugfs_reg_access)(struct iio_dev *indio_dev,
  332. unsigned reg, unsigned writeval,
  333. unsigned *readval);
  334. };
  335. /**
  336. * struct iio_buffer_setup_ops - buffer setup related callbacks
  337. * @preenable: [DRIVER] function to run prior to marking buffer enabled
  338. * @postenable: [DRIVER] function to run after marking buffer enabled
  339. * @predisable: [DRIVER] function to run prior to marking buffer
  340. * disabled
  341. * @postdisable: [DRIVER] function to run after marking buffer disabled
  342. * @validate_scan_mask: [DRIVER] function callback to check whether a given
  343. * scan mask is valid for the device.
  344. */
  345. struct iio_buffer_setup_ops {
  346. int (*preenable)(struct iio_dev *);
  347. int (*postenable)(struct iio_dev *);
  348. int (*predisable)(struct iio_dev *);
  349. int (*postdisable)(struct iio_dev *);
  350. bool (*validate_scan_mask)(struct iio_dev *indio_dev,
  351. const unsigned long *scan_mask);
  352. };
  353. /**
  354. * struct iio_dev - industrial I/O device
  355. * @id: [INTERN] used to identify device internally
  356. * @modes: [DRIVER] operating modes supported by device
  357. * @currentmode: [DRIVER] current operating mode
  358. * @dev: [DRIVER] device structure, should be assigned a parent
  359. * and owner
  360. * @event_interface: [INTERN] event chrdevs associated with interrupt lines
  361. * @buffer: [DRIVER] any buffer present
  362. * @scan_bytes: [INTERN] num bytes captured to be fed to buffer demux
  363. * @mlock: [INTERN] lock used to prevent simultaneous device state
  364. * changes
  365. * @available_scan_masks: [DRIVER] optional array of allowed bitmasks
  366. * @masklength: [INTERN] the length of the mask established from
  367. * channels
  368. * @active_scan_mask: [INTERN] union of all scan masks requested by buffers
  369. * @scan_timestamp: [INTERN] set if any buffers have requested timestamp
  370. * @scan_index_timestamp:[INTERN] cache of the index to the timestamp
  371. * @trig: [INTERN] current device trigger (buffer modes)
  372. * @pollfunc: [DRIVER] function run on trigger being received
  373. * @channels: [DRIVER] channel specification structure table
  374. * @num_channels: [DRIVER] number of channels specified in @channels.
  375. * @channel_attr_list: [INTERN] keep track of automatically created channel
  376. * attributes
  377. * @chan_attr_group: [INTERN] group for all attrs in base directory
  378. * @name: [DRIVER] name of the device.
  379. * @info: [DRIVER] callbacks and constant info from driver
  380. * @info_exist_lock: [INTERN] lock to prevent use during removal
  381. * @setup_ops: [DRIVER] callbacks to call before and after buffer
  382. * enable/disable
  383. * @chrdev: [INTERN] associated character device
  384. * @groups: [INTERN] attribute groups
  385. * @groupcounter: [INTERN] index of next attribute group
  386. * @flags: [INTERN] file ops related flags including busy flag.
  387. * @debugfs_dentry: [INTERN] device specific debugfs dentry.
  388. * @cached_reg_addr: [INTERN] cached register address for debugfs reads.
  389. */
  390. struct iio_dev {
  391. int id;
  392. int modes;
  393. int currentmode;
  394. struct device dev;
  395. struct iio_event_interface *event_interface;
  396. struct iio_buffer *buffer;
  397. int scan_bytes;
  398. struct mutex mlock;
  399. const unsigned long *available_scan_masks;
  400. unsigned masklength;
  401. const unsigned long *active_scan_mask;
  402. bool scan_timestamp;
  403. unsigned scan_index_timestamp;
  404. struct iio_trigger *trig;
  405. struct iio_poll_func *pollfunc;
  406. struct iio_chan_spec const *channels;
  407. int num_channels;
  408. struct list_head channel_attr_list;
  409. struct attribute_group chan_attr_group;
  410. const char *name;
  411. const struct iio_info *info;
  412. struct mutex info_exist_lock;
  413. const struct iio_buffer_setup_ops *setup_ops;
  414. struct cdev chrdev;
  415. #define IIO_MAX_GROUPS 6
  416. const struct attribute_group *groups[IIO_MAX_GROUPS + 1];
  417. int groupcounter;
  418. unsigned long flags;
  419. #if defined(CONFIG_DEBUG_FS)
  420. struct dentry *debugfs_dentry;
  421. unsigned cached_reg_addr;
  422. #endif
  423. };
  424. /**
  425. * iio_find_channel_from_si() - get channel from its scan index
  426. * @indio_dev: device
  427. * @si: scan index to match
  428. */
  429. const struct iio_chan_spec
  430. *iio_find_channel_from_si(struct iio_dev *indio_dev, int si);
  431. /**
  432. * iio_device_register() - register a device with the IIO subsystem
  433. * @indio_dev: Device structure filled by the device driver
  434. **/
  435. int iio_device_register(struct iio_dev *indio_dev);
  436. /**
  437. * iio_device_unregister() - unregister a device from the IIO subsystem
  438. * @indio_dev: Device structure representing the device.
  439. **/
  440. void iio_device_unregister(struct iio_dev *indio_dev);
  441. /**
  442. * iio_push_event() - try to add event to the list for userspace reading
  443. * @indio_dev: IIO device structure
  444. * @ev_code: What event
  445. * @timestamp: When the event occurred
  446. **/
  447. int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp);
  448. extern struct bus_type iio_bus_type;
  449. /**
  450. * iio_device_put() - reference counted deallocation of struct device
  451. * @indio_dev: IIO device structure containing the device
  452. **/
  453. static inline void iio_device_put(struct iio_dev *indio_dev)
  454. {
  455. if (indio_dev)
  456. put_device(&indio_dev->dev);
  457. };
  458. /**
  459. * dev_to_iio_dev() - Get IIO device struct from a device struct
  460. * @dev: The device embedded in the IIO device
  461. *
  462. * Note: The device must be a IIO device, otherwise the result is undefined.
  463. */
  464. static inline struct iio_dev *dev_to_iio_dev(struct device *dev)
  465. {
  466. return container_of(dev, struct iio_dev, dev);
  467. }
  468. /**
  469. * iio_device_get() - increment reference count for the device
  470. * @indio_dev: IIO device structure
  471. *
  472. * Returns: The passed IIO device
  473. **/
  474. static inline struct iio_dev *iio_device_get(struct iio_dev *indio_dev)
  475. {
  476. return indio_dev ? dev_to_iio_dev(get_device(&indio_dev->dev)) : NULL;
  477. }
  478. /**
  479. * iio_device_set_drvdata() - Set device driver data
  480. * @indio_dev: IIO device structure
  481. * @data: Driver specific data
  482. *
  483. * Allows to attach an arbitrary pointer to an IIO device, which can later be
  484. * retrieved by iio_device_get_drvdata().
  485. */
  486. static inline void iio_device_set_drvdata(struct iio_dev *indio_dev, void *data)
  487. {
  488. dev_set_drvdata(&indio_dev->dev, data);
  489. }
  490. /**
  491. * iio_device_get_drvdata() - Get device driver data
  492. * @indio_dev: IIO device structure
  493. *
  494. * Returns the data previously set with iio_device_set_drvdata()
  495. */
  496. static inline void *iio_device_get_drvdata(struct iio_dev *indio_dev)
  497. {
  498. return dev_get_drvdata(&indio_dev->dev);
  499. }
  500. /* Can we make this smaller? */
  501. #define IIO_ALIGN L1_CACHE_BYTES
  502. /**
  503. * iio_device_alloc() - allocate an iio_dev from a driver
  504. * @sizeof_priv: Space to allocate for private structure.
  505. **/
  506. struct iio_dev *iio_device_alloc(int sizeof_priv);
  507. static inline void *iio_priv(const struct iio_dev *indio_dev)
  508. {
  509. return (char *)indio_dev + ALIGN(sizeof(struct iio_dev), IIO_ALIGN);
  510. }
  511. static inline struct iio_dev *iio_priv_to_dev(void *priv)
  512. {
  513. return (struct iio_dev *)((char *)priv -
  514. ALIGN(sizeof(struct iio_dev), IIO_ALIGN));
  515. }
  516. /**
  517. * iio_device_free() - free an iio_dev from a driver
  518. * @indio_dev: the iio_dev associated with the device
  519. **/
  520. void iio_device_free(struct iio_dev *indio_dev);
  521. /**
  522. * iio_buffer_enabled() - helper function to test if the buffer is enabled
  523. * @indio_dev: IIO device structure for device
  524. **/
  525. static inline bool iio_buffer_enabled(struct iio_dev *indio_dev)
  526. {
  527. return indio_dev->currentmode
  528. & (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE);
  529. };
  530. /**
  531. * iio_get_debugfs_dentry() - helper function to get the debugfs_dentry
  532. * @indio_dev: IIO device structure for device
  533. **/
  534. #if defined(CONFIG_DEBUG_FS)
  535. static inline struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
  536. {
  537. return indio_dev->debugfs_dentry;
  538. };
  539. #else
  540. static inline struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
  541. {
  542. return NULL;
  543. };
  544. #endif
  545. #endif /* _INDUSTRIAL_IO_H_ */