consumer.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Industrial I/O in kernel consumer interface
  3. *
  4. * Copyright (c) 2011 Jonathan Cameron
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. */
  10. #ifndef _IIO_INKERN_CONSUMER_H_
  11. #define _IIO_INKERN_CONSUMER_H_
  12. #include <linux/types.h>
  13. #include <linux/iio/types.h>
  14. struct iio_dev;
  15. struct iio_chan_spec;
  16. struct device;
  17. /**
  18. * struct iio_channel - everything needed for a consumer to use a channel
  19. * @indio_dev: Device on which the channel exists.
  20. * @channel: Full description of the channel.
  21. * @data: Data about the channel used by consumer.
  22. */
  23. struct iio_channel {
  24. struct iio_dev *indio_dev;
  25. const struct iio_chan_spec *channel;
  26. void *data;
  27. };
  28. /**
  29. * iio_channel_get() - get description of all that is needed to access channel.
  30. * @dev: Pointer to consumer device. Device name must match
  31. * the name of the device as provided in the iio_map
  32. * with which the desired provider to consumer mapping
  33. * was registered.
  34. * @consumer_channel: Unique name to identify the channel on the consumer
  35. * side. This typically describes the channels use within
  36. * the consumer. E.g. 'battery_voltage'
  37. */
  38. struct iio_channel *iio_channel_get(struct device *dev,
  39. const char *consumer_channel);
  40. /**
  41. * iio_channel_release() - release channels obtained via iio_channel_get
  42. * @chan: The channel to be released.
  43. */
  44. void iio_channel_release(struct iio_channel *chan);
  45. /**
  46. * devm_iio_channel_get() - Resource managed version of iio_channel_get().
  47. * @dev: Pointer to consumer device. Device name must match
  48. * the name of the device as provided in the iio_map
  49. * with which the desired provider to consumer mapping
  50. * was registered.
  51. * @consumer_channel: Unique name to identify the channel on the consumer
  52. * side. This typically describes the channels use within
  53. * the consumer. E.g. 'battery_voltage'
  54. *
  55. * Returns a pointer to negative errno if it is not able to get the iio channel
  56. * otherwise returns valid pointer for iio channel.
  57. *
  58. * The allocated iio channel is automatically released when the device is
  59. * unbound.
  60. */
  61. struct iio_channel *devm_iio_channel_get(struct device *dev,
  62. const char *consumer_channel);
  63. /**
  64. * devm_iio_channel_release() - Resource managed version of
  65. * iio_channel_release().
  66. * @dev: Pointer to consumer device for which resource
  67. * is allocared.
  68. * @chan: The channel to be released.
  69. */
  70. void devm_iio_channel_release(struct device *dev, struct iio_channel *chan);
  71. /**
  72. * iio_channel_get_all() - get all channels associated with a client
  73. * @dev: Pointer to consumer device.
  74. *
  75. * Returns an array of iio_channel structures terminated with one with
  76. * null iio_dev pointer.
  77. * This function is used by fairly generic consumers to get all the
  78. * channels registered as having this consumer.
  79. */
  80. struct iio_channel *iio_channel_get_all(struct device *dev);
  81. /**
  82. * iio_channel_release_all() - reverse iio_channel_get_all
  83. * @chan: Array of channels to be released.
  84. */
  85. void iio_channel_release_all(struct iio_channel *chan);
  86. /**
  87. * devm_iio_channel_get_all() - Resource managed version of
  88. * iio_channel_get_all().
  89. * @dev: Pointer to consumer device.
  90. *
  91. * Returns a pointer to negative errno if it is not able to get the iio channel
  92. * otherwise returns an array of iio_channel structures terminated with one with
  93. * null iio_dev pointer.
  94. *
  95. * This function is used by fairly generic consumers to get all the
  96. * channels registered as having this consumer.
  97. *
  98. * The allocated iio channels are automatically released when the device is
  99. * unbounded.
  100. */
  101. struct iio_channel *devm_iio_channel_get_all(struct device *dev);
  102. /**
  103. * devm_iio_channel_release_all() - Resource managed version of
  104. * iio_channel_release_all().
  105. * @dev: Pointer to consumer device for which resource
  106. * is allocared.
  107. * @chan: Array channel to be released.
  108. */
  109. void devm_iio_channel_release_all(struct device *dev, struct iio_channel *chan);
  110. struct iio_cb_buffer;
  111. /**
  112. * iio_channel_get_all_cb() - register callback for triggered capture
  113. * @dev: Pointer to client device.
  114. * @cb: Callback function.
  115. * @private: Private data passed to callback.
  116. *
  117. * NB right now we have no ability to mux data from multiple devices.
  118. * So if the channels requested come from different devices this will
  119. * fail.
  120. */
  121. struct iio_cb_buffer *iio_channel_get_all_cb(struct device *dev,
  122. int (*cb)(const void *data,
  123. void *private),
  124. void *private);
  125. /**
  126. * iio_channel_release_all_cb() - release and unregister the callback.
  127. * @cb_buffer: The callback buffer that was allocated.
  128. */
  129. void iio_channel_release_all_cb(struct iio_cb_buffer *cb_buffer);
  130. /**
  131. * iio_channel_start_all_cb() - start the flow of data through callback.
  132. * @cb_buff: The callback buffer we are starting.
  133. */
  134. int iio_channel_start_all_cb(struct iio_cb_buffer *cb_buff);
  135. /**
  136. * iio_channel_stop_all_cb() - stop the flow of data through the callback.
  137. * @cb_buff: The callback buffer we are stopping.
  138. */
  139. void iio_channel_stop_all_cb(struct iio_cb_buffer *cb_buff);
  140. /**
  141. * iio_channel_cb_get_channels() - get access to the underlying channels.
  142. * @cb_buffer: The callback buffer from whom we want the channel
  143. * information.
  144. *
  145. * This function allows one to obtain information about the channels.
  146. * Whilst this may allow direct reading if all buffers are disabled, the
  147. * primary aim is to allow drivers that are consuming a channel to query
  148. * things like scaling of the channel.
  149. */
  150. struct iio_channel
  151. *iio_channel_cb_get_channels(const struct iio_cb_buffer *cb_buffer);
  152. /**
  153. * iio_channel_cb_get_iio_dev() - get access to the underlying device.
  154. * @cb_buffer: The callback buffer from whom we want the device
  155. * information.
  156. *
  157. * This function allows one to obtain information about the device.
  158. * The primary aim is to allow drivers that are consuming a device to query
  159. * things like current trigger.
  160. */
  161. struct iio_dev
  162. *iio_channel_cb_get_iio_dev(const struct iio_cb_buffer *cb_buffer);
  163. /**
  164. * iio_read_channel_raw() - read from a given channel
  165. * @chan: The channel being queried.
  166. * @val: Value read back.
  167. *
  168. * Note raw reads from iio channels are in adc counts and hence
  169. * scale will need to be applied if standard units required.
  170. */
  171. int iio_read_channel_raw(struct iio_channel *chan,
  172. int *val);
  173. /**
  174. * iio_read_channel_average_raw() - read from a given channel
  175. * @chan: The channel being queried.
  176. * @val: Value read back.
  177. *
  178. * Note raw reads from iio channels are in adc counts and hence
  179. * scale will need to be applied if standard units required.
  180. *
  181. * In opposit to the normal iio_read_channel_raw this function
  182. * returns the average of multiple reads.
  183. */
  184. int iio_read_channel_average_raw(struct iio_channel *chan, int *val);
  185. /**
  186. * iio_read_channel_processed() - read processed value from a given channel
  187. * @chan: The channel being queried.
  188. * @val: Value read back.
  189. *
  190. * Returns an error code or 0.
  191. *
  192. * This function will read a processed value from a channel. A processed value
  193. * means that this value will have the correct unit and not some device internal
  194. * representation. If the device does not support reporting a processed value
  195. * the function will query the raw value and the channels scale and offset and
  196. * do the appropriate transformation.
  197. */
  198. int iio_read_channel_processed(struct iio_channel *chan, int *val);
  199. /**
  200. * iio_write_channel_raw() - write to a given channel
  201. * @chan: The channel being queried.
  202. * @val: Value being written.
  203. *
  204. * Note raw writes to iio channels are in dac counts and hence
  205. * scale will need to be applied if standard units required.
  206. */
  207. int iio_write_channel_raw(struct iio_channel *chan, int val);
  208. /**
  209. * iio_get_channel_type() - get the type of a channel
  210. * @channel: The channel being queried.
  211. * @type: The type of the channel.
  212. *
  213. * returns the enum iio_chan_type of the channel
  214. */
  215. int iio_get_channel_type(struct iio_channel *channel,
  216. enum iio_chan_type *type);
  217. /**
  218. * iio_read_channel_scale() - read the scale value for a channel
  219. * @chan: The channel being queried.
  220. * @val: First part of value read back.
  221. * @val2: Second part of value read back.
  222. *
  223. * Note returns a description of what is in val and val2, such
  224. * as IIO_VAL_INT_PLUS_MICRO telling us we have a value of val
  225. * + val2/1e6
  226. */
  227. int iio_read_channel_scale(struct iio_channel *chan, int *val,
  228. int *val2);
  229. /**
  230. * iio_convert_raw_to_processed() - Converts a raw value to a processed value
  231. * @chan: The channel being queried
  232. * @raw: The raw IIO to convert
  233. * @processed: The result of the conversion
  234. * @scale: Scale factor to apply during the conversion
  235. *
  236. * Returns an error code or 0.
  237. *
  238. * This function converts a raw value to processed value for a specific channel.
  239. * A raw value is the device internal representation of a sample and the value
  240. * returned by iio_read_channel_raw, so the unit of that value is device
  241. * depended. A processed value on the other hand is value has a normed unit
  242. * according with the IIO specification.
  243. *
  244. * The scale factor allows to increase the precession of the returned value. For
  245. * a scale factor of 1 the function will return the result in the normal IIO
  246. * unit for the channel type. E.g. millivolt for voltage channels, if you want
  247. * nanovolts instead pass 1000000 as the scale factor.
  248. */
  249. int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
  250. int *processed, unsigned int scale);
  251. #endif