consumer.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Industrial I/O in kernel consumer interface
  4. *
  5. * Copyright (c) 2011 Jonathan Cameron
  6. */
  7. #ifndef _IIO_INKERN_CONSUMER_H_
  8. #define _IIO_INKERN_CONSUMER_H_
  9. #include <linux/types.h>
  10. #include <linux/iio/types.h>
  11. struct iio_dev;
  12. struct iio_chan_spec;
  13. struct device;
  14. struct fwnode_handle;
  15. /**
  16. * struct iio_channel - everything needed for a consumer to use a channel
  17. * @indio_dev: Device on which the channel exists.
  18. * @channel: Full description of the channel.
  19. * @data: Data about the channel used by consumer.
  20. */
  21. struct iio_channel {
  22. struct iio_dev *indio_dev;
  23. const struct iio_chan_spec *channel;
  24. void *data;
  25. };
  26. /**
  27. * iio_channel_get() - get description of all that is needed to access channel.
  28. * @dev: Pointer to consumer device. Device name must match
  29. * the name of the device as provided in the iio_map
  30. * with which the desired provider to consumer mapping
  31. * was registered.
  32. * @consumer_channel: Unique name to identify the channel on the consumer
  33. * side. This typically describes the channels use within
  34. * the consumer. E.g. 'battery_voltage'
  35. */
  36. struct iio_channel *iio_channel_get(struct device *dev,
  37. const char *consumer_channel);
  38. /**
  39. * iio_channel_release() - release channels obtained via iio_channel_get
  40. * @chan: The channel to be released.
  41. */
  42. void iio_channel_release(struct iio_channel *chan);
  43. /**
  44. * devm_iio_channel_get() - Resource managed version of iio_channel_get().
  45. * @dev: Pointer to consumer device. Device name must match
  46. * the name of the device as provided in the iio_map
  47. * with which the desired provider to consumer mapping
  48. * was registered.
  49. * @consumer_channel: Unique name to identify the channel on the consumer
  50. * side. This typically describes the channels use within
  51. * the consumer. E.g. 'battery_voltage'
  52. *
  53. * Returns a pointer to negative errno if it is not able to get the iio channel
  54. * otherwise returns valid pointer for iio channel.
  55. *
  56. * The allocated iio channel is automatically released when the device is
  57. * unbound.
  58. */
  59. struct iio_channel *devm_iio_channel_get(struct device *dev,
  60. const char *consumer_channel);
  61. /**
  62. * iio_channel_get_all() - get all channels associated with a client
  63. * @dev: Pointer to consumer device.
  64. *
  65. * Returns an array of iio_channel structures terminated with one with
  66. * null iio_dev pointer.
  67. * This function is used by fairly generic consumers to get all the
  68. * channels registered as having this consumer.
  69. */
  70. struct iio_channel *iio_channel_get_all(struct device *dev);
  71. /**
  72. * iio_channel_release_all() - reverse iio_channel_get_all
  73. * @chan: Array of channels to be released.
  74. */
  75. void iio_channel_release_all(struct iio_channel *chan);
  76. /**
  77. * devm_iio_channel_get_all() - Resource managed version of
  78. * iio_channel_get_all().
  79. * @dev: Pointer to consumer device.
  80. *
  81. * Returns a pointer to negative errno if it is not able to get the iio channel
  82. * otherwise returns an array of iio_channel structures terminated with one with
  83. * null iio_dev pointer.
  84. *
  85. * This function is used by fairly generic consumers to get all the
  86. * channels registered as having this consumer.
  87. *
  88. * The allocated iio channels are automatically released when the device is
  89. * unbounded.
  90. */
  91. struct iio_channel *devm_iio_channel_get_all(struct device *dev);
  92. /**
  93. * fwnode_iio_channel_get_by_name() - get description of all that is needed to access channel.
  94. * @fwnode: Pointer to consumer Firmware node
  95. * @consumer_channel: Unique name to identify the channel on the consumer
  96. * side. This typically describes the channels use within
  97. * the consumer. E.g. 'battery_voltage'
  98. */
  99. struct iio_channel *fwnode_iio_channel_get_by_name(struct fwnode_handle *fwnode,
  100. const char *name);
  101. /**
  102. * devm_fwnode_iio_channel_get_by_name() - Resource managed version of
  103. * fwnode_iio_channel_get_by_name().
  104. * @dev: Pointer to consumer device.
  105. * @fwnode: Pointer to consumer Firmware node
  106. * @consumer_channel: Unique name to identify the channel on the consumer
  107. * side. This typically describes the channels use within
  108. * the consumer. E.g. 'battery_voltage'
  109. *
  110. * Returns a pointer to negative errno if it is not able to get the iio channel
  111. * otherwise returns valid pointer for iio channel.
  112. *
  113. * The allocated iio channel is automatically released when the device is
  114. * unbound.
  115. */
  116. struct iio_channel *devm_fwnode_iio_channel_get_by_name(struct device *dev,
  117. struct fwnode_handle *fwnode,
  118. const char *consumer_channel);
  119. struct iio_cb_buffer;
  120. /**
  121. * iio_channel_get_all_cb() - register callback for triggered capture
  122. * @dev: Pointer to client device.
  123. * @cb: Callback function.
  124. * @private: Private data passed to callback.
  125. *
  126. * NB right now we have no ability to mux data from multiple devices.
  127. * So if the channels requested come from different devices this will
  128. * fail.
  129. */
  130. struct iio_cb_buffer *iio_channel_get_all_cb(struct device *dev,
  131. int (*cb)(const void *data,
  132. void *private),
  133. void *private);
  134. /**
  135. * iio_channel_cb_set_buffer_watermark() - set the buffer watermark.
  136. * @cb_buffer: The callback buffer from whom we want the channel
  137. * information.
  138. * @watermark: buffer watermark in bytes.
  139. *
  140. * This function allows to configure the buffer watermark.
  141. */
  142. int iio_channel_cb_set_buffer_watermark(struct iio_cb_buffer *cb_buffer,
  143. size_t watermark);
  144. /**
  145. * iio_channel_release_all_cb() - release and unregister the callback.
  146. * @cb_buffer: The callback buffer that was allocated.
  147. */
  148. void iio_channel_release_all_cb(struct iio_cb_buffer *cb_buffer);
  149. /**
  150. * iio_channel_start_all_cb() - start the flow of data through callback.
  151. * @cb_buff: The callback buffer we are starting.
  152. */
  153. int iio_channel_start_all_cb(struct iio_cb_buffer *cb_buff);
  154. /**
  155. * iio_channel_stop_all_cb() - stop the flow of data through the callback.
  156. * @cb_buff: The callback buffer we are stopping.
  157. */
  158. void iio_channel_stop_all_cb(struct iio_cb_buffer *cb_buff);
  159. /**
  160. * iio_channel_cb_get_channels() - get access to the underlying channels.
  161. * @cb_buffer: The callback buffer from whom we want the channel
  162. * information.
  163. *
  164. * This function allows one to obtain information about the channels.
  165. * Whilst this may allow direct reading if all buffers are disabled, the
  166. * primary aim is to allow drivers that are consuming a channel to query
  167. * things like scaling of the channel.
  168. */
  169. struct iio_channel
  170. *iio_channel_cb_get_channels(const struct iio_cb_buffer *cb_buffer);
  171. /**
  172. * iio_channel_cb_get_iio_dev() - get access to the underlying device.
  173. * @cb_buffer: The callback buffer from whom we want the device
  174. * information.
  175. *
  176. * This function allows one to obtain information about the device.
  177. * The primary aim is to allow drivers that are consuming a device to query
  178. * things like current trigger.
  179. */
  180. struct iio_dev
  181. *iio_channel_cb_get_iio_dev(const struct iio_cb_buffer *cb_buffer);
  182. /**
  183. * iio_read_channel_raw() - read from a given channel
  184. * @chan: The channel being queried.
  185. * @val: Value read back.
  186. *
  187. * Note raw reads from iio channels are in adc counts and hence
  188. * scale will need to be applied if standard units required.
  189. */
  190. int iio_read_channel_raw(struct iio_channel *chan,
  191. int *val);
  192. /**
  193. * iio_read_channel_average_raw() - read from a given channel
  194. * @chan: The channel being queried.
  195. * @val: Value read back.
  196. *
  197. * Note raw reads from iio channels are in adc counts and hence
  198. * scale will need to be applied if standard units required.
  199. *
  200. * In opposit to the normal iio_read_channel_raw this function
  201. * returns the average of multiple reads.
  202. */
  203. int iio_read_channel_average_raw(struct iio_channel *chan, int *val);
  204. /**
  205. * iio_read_channel_processed() - read processed value from a given channel
  206. * @chan: The channel being queried.
  207. * @val: Value read back.
  208. *
  209. * Returns an error code or 0.
  210. *
  211. * This function will read a processed value from a channel. A processed value
  212. * means that this value will have the correct unit and not some device internal
  213. * representation. If the device does not support reporting a processed value
  214. * the function will query the raw value and the channels scale and offset and
  215. * do the appropriate transformation.
  216. */
  217. int iio_read_channel_processed(struct iio_channel *chan, int *val);
  218. /**
  219. * iio_read_channel_processed_scale() - read and scale a processed value
  220. * @chan: The channel being queried.
  221. * @val: Value read back.
  222. * @scale: Scale factor to apply during the conversion
  223. *
  224. * Returns an error code or 0.
  225. *
  226. * This function will read a processed value from a channel. This will work
  227. * like @iio_read_channel_processed() but also scale with an additional
  228. * scale factor while attempting to minimize any precision loss.
  229. */
  230. int iio_read_channel_processed_scale(struct iio_channel *chan, int *val,
  231. unsigned int scale);
  232. /**
  233. * iio_write_channel_attribute() - Write values to the device attribute.
  234. * @chan: The channel being queried.
  235. * @val: Value being written.
  236. * @val2: Value being written.val2 use depends on attribute type.
  237. * @attribute: info attribute to be read.
  238. *
  239. * Returns an error code or 0.
  240. */
  241. int iio_write_channel_attribute(struct iio_channel *chan, int val,
  242. int val2, enum iio_chan_info_enum attribute);
  243. /**
  244. * iio_read_channel_attribute() - Read values from the device attribute.
  245. * @chan: The channel being queried.
  246. * @val: Value being written.
  247. * @val2: Value being written.Val2 use depends on attribute type.
  248. * @attribute: info attribute to be written.
  249. *
  250. * Returns an error code if failed. Else returns a description of what is in val
  251. * and val2, such as IIO_VAL_INT_PLUS_MICRO telling us we have a value of val
  252. * + val2/1e6
  253. */
  254. int iio_read_channel_attribute(struct iio_channel *chan, int *val,
  255. int *val2, enum iio_chan_info_enum attribute);
  256. /**
  257. * iio_write_channel_raw() - write to a given channel
  258. * @chan: The channel being queried.
  259. * @val: Value being written.
  260. *
  261. * Note raw writes to iio channels are in dac counts and hence
  262. * scale will need to be applied if standard units required.
  263. */
  264. int iio_write_channel_raw(struct iio_channel *chan, int val);
  265. /**
  266. * iio_read_max_channel_raw() - read maximum available raw value from a given
  267. * channel, i.e. the maximum possible value.
  268. * @chan: The channel being queried.
  269. * @val: Value read back.
  270. *
  271. * Note raw reads from iio channels are in adc counts and hence
  272. * scale will need to be applied if standard units are required.
  273. */
  274. int iio_read_max_channel_raw(struct iio_channel *chan, int *val);
  275. /**
  276. * iio_read_avail_channel_raw() - read available raw values from a given channel
  277. * @chan: The channel being queried.
  278. * @vals: Available values read back.
  279. * @length: Number of entries in vals.
  280. *
  281. * Returns an error code, IIO_AVAIL_RANGE or IIO_AVAIL_LIST.
  282. *
  283. * For ranges, three vals are always returned; min, step and max.
  284. * For lists, all the possible values are enumerated.
  285. *
  286. * Note raw available values from iio channels are in adc counts and
  287. * hence scale will need to be applied if standard units are required.
  288. */
  289. int iio_read_avail_channel_raw(struct iio_channel *chan,
  290. const int **vals, int *length);
  291. /**
  292. * iio_read_avail_channel_attribute() - read available channel attribute values
  293. * @chan: The channel being queried.
  294. * @vals: Available values read back.
  295. * @type: Type of values read back.
  296. * @length: Number of entries in vals.
  297. * @attribute: info attribute to be read back.
  298. *
  299. * Returns an error code, IIO_AVAIL_RANGE or IIO_AVAIL_LIST.
  300. */
  301. int iio_read_avail_channel_attribute(struct iio_channel *chan,
  302. const int **vals, int *type, int *length,
  303. enum iio_chan_info_enum attribute);
  304. /**
  305. * iio_get_channel_type() - get the type of a channel
  306. * @channel: The channel being queried.
  307. * @type: The type of the channel.
  308. *
  309. * returns the enum iio_chan_type of the channel
  310. */
  311. int iio_get_channel_type(struct iio_channel *channel,
  312. enum iio_chan_type *type);
  313. /**
  314. * iio_read_channel_offset() - read the offset value for a channel
  315. * @chan: The channel being queried.
  316. * @val: First part of value read back.
  317. * @val2: Second part of value read back.
  318. *
  319. * Note returns a description of what is in val and val2, such
  320. * as IIO_VAL_INT_PLUS_MICRO telling us we have a value of val
  321. * + val2/1e6
  322. */
  323. int iio_read_channel_offset(struct iio_channel *chan, int *val,
  324. int *val2);
  325. /**
  326. * iio_read_channel_scale() - read the scale value for a channel
  327. * @chan: The channel being queried.
  328. * @val: First part of value read back.
  329. * @val2: Second part of value read back.
  330. *
  331. * Note returns a description of what is in val and val2, such
  332. * as IIO_VAL_INT_PLUS_MICRO telling us we have a value of val
  333. * + val2/1e6
  334. */
  335. int iio_read_channel_scale(struct iio_channel *chan, int *val,
  336. int *val2);
  337. /**
  338. * iio_convert_raw_to_processed() - Converts a raw value to a processed value
  339. * @chan: The channel being queried
  340. * @raw: The raw IIO to convert
  341. * @processed: The result of the conversion
  342. * @scale: Scale factor to apply during the conversion
  343. *
  344. * Returns an error code or 0.
  345. *
  346. * This function converts a raw value to processed value for a specific channel.
  347. * A raw value is the device internal representation of a sample and the value
  348. * returned by iio_read_channel_raw, so the unit of that value is device
  349. * depended. A processed value on the other hand is value has a normed unit
  350. * according with the IIO specification.
  351. *
  352. * The scale factor allows to increase the precession of the returned value. For
  353. * a scale factor of 1 the function will return the result in the normal IIO
  354. * unit for the channel type. E.g. millivolt for voltage channels, if you want
  355. * nanovolts instead pass 1000000 as the scale factor.
  356. */
  357. int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
  358. int *processed, unsigned int scale);
  359. /**
  360. * iio_get_channel_ext_info_count() - get number of ext_info attributes
  361. * connected to the channel.
  362. * @chan: The channel being queried
  363. *
  364. * Returns the number of ext_info attributes
  365. */
  366. unsigned int iio_get_channel_ext_info_count(struct iio_channel *chan);
  367. /**
  368. * iio_read_channel_ext_info() - read ext_info attribute from a given channel
  369. * @chan: The channel being queried.
  370. * @attr: The ext_info attribute to read.
  371. * @buf: Where to store the attribute value. Assumed to hold
  372. * at least PAGE_SIZE bytes.
  373. *
  374. * Returns the number of bytes written to buf (perhaps w/o zero termination;
  375. * it need not even be a string), or an error code.
  376. */
  377. ssize_t iio_read_channel_ext_info(struct iio_channel *chan,
  378. const char *attr, char *buf);
  379. /**
  380. * iio_write_channel_ext_info() - write ext_info attribute from a given channel
  381. * @chan: The channel being queried.
  382. * @attr: The ext_info attribute to read.
  383. * @buf: The new attribute value. Strings needs to be zero-
  384. * terminated, but the terminator should not be included
  385. * in the below len.
  386. * @len: The size of the new attribute value.
  387. *
  388. * Returns the number of accepted bytes, which should be the same as len.
  389. * An error code can also be returned.
  390. */
  391. ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr,
  392. const char *buf, size_t len);
  393. #endif