iio.h 22 KB

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