consumer.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. * iio_channel_get_all() - get all channels associated with a client
  47. * @dev: Pointer to consumer device.
  48. *
  49. * Returns an array of iio_channel structures terminated with one with
  50. * null iio_dev pointer.
  51. * This function is used by fairly generic consumers to get all the
  52. * channels registered as having this consumer.
  53. */
  54. struct iio_channel *iio_channel_get_all(struct device *dev);
  55. /**
  56. * iio_channel_release_all() - reverse iio_channel_get_all
  57. * @chan: Array of channels to be released.
  58. */
  59. void iio_channel_release_all(struct iio_channel *chan);
  60. struct iio_cb_buffer;
  61. /**
  62. * iio_channel_get_all_cb() - register callback for triggered capture
  63. * @dev: Pointer to client device.
  64. * @cb: Callback function.
  65. * @private: Private data passed to callback.
  66. *
  67. * NB right now we have no ability to mux data from multiple devices.
  68. * So if the channels requested come from different devices this will
  69. * fail.
  70. */
  71. struct iio_cb_buffer *iio_channel_get_all_cb(struct device *dev,
  72. int (*cb)(const void *data,
  73. void *private),
  74. void *private);
  75. /**
  76. * iio_channel_release_all_cb() - release and unregister the callback.
  77. * @cb_buffer: The callback buffer that was allocated.
  78. */
  79. void iio_channel_release_all_cb(struct iio_cb_buffer *cb_buffer);
  80. /**
  81. * iio_channel_start_all_cb() - start the flow of data through callback.
  82. * @cb_buff: The callback buffer we are starting.
  83. */
  84. int iio_channel_start_all_cb(struct iio_cb_buffer *cb_buff);
  85. /**
  86. * iio_channel_stop_all_cb() - stop the flow of data through the callback.
  87. * @cb_buff: The callback buffer we are stopping.
  88. */
  89. void iio_channel_stop_all_cb(struct iio_cb_buffer *cb_buff);
  90. /**
  91. * iio_channel_cb_get_channels() - get access to the underlying channels.
  92. * @cb_buffer: The callback buffer from whom we want the channel
  93. * information.
  94. *
  95. * This function allows one to obtain information about the channels.
  96. * Whilst this may allow direct reading if all buffers are disabled, the
  97. * primary aim is to allow drivers that are consuming a channel to query
  98. * things like scaling of the channel.
  99. */
  100. struct iio_channel
  101. *iio_channel_cb_get_channels(const struct iio_cb_buffer *cb_buffer);
  102. /**
  103. * iio_read_channel_raw() - read from a given channel
  104. * @chan: The channel being queried.
  105. * @val: Value read back.
  106. *
  107. * Note raw reads from iio channels are in adc counts and hence
  108. * scale will need to be applied if standard units required.
  109. */
  110. int iio_read_channel_raw(struct iio_channel *chan,
  111. int *val);
  112. /**
  113. * iio_read_channel_average_raw() - read from a given channel
  114. * @chan: The channel being queried.
  115. * @val: Value read back.
  116. *
  117. * Note raw reads from iio channels are in adc counts and hence
  118. * scale will need to be applied if standard units required.
  119. *
  120. * In opposit to the normal iio_read_channel_raw this function
  121. * returns the average of multiple reads.
  122. */
  123. int iio_read_channel_average_raw(struct iio_channel *chan, int *val);
  124. /**
  125. * iio_read_channel_processed() - read processed value from a given channel
  126. * @chan: The channel being queried.
  127. * @val: Value read back.
  128. *
  129. * Returns an error code or 0.
  130. *
  131. * This function will read a processed value from a channel. A processed value
  132. * means that this value will have the correct unit and not some device internal
  133. * representation. If the device does not support reporting a processed value
  134. * the function will query the raw value and the channels scale and offset and
  135. * do the appropriate transformation.
  136. */
  137. int iio_read_channel_processed(struct iio_channel *chan, int *val);
  138. /**
  139. * iio_write_channel_raw() - write to a given channel
  140. * @chan: The channel being queried.
  141. * @val: Value being written.
  142. *
  143. * Note raw writes to iio channels are in dac counts and hence
  144. * scale will need to be applied if standard units required.
  145. */
  146. int iio_write_channel_raw(struct iio_channel *chan, int val);
  147. /**
  148. * iio_get_channel_type() - get the type of a channel
  149. * @channel: The channel being queried.
  150. * @type: The type of the channel.
  151. *
  152. * returns the enum iio_chan_type of the channel
  153. */
  154. int iio_get_channel_type(struct iio_channel *channel,
  155. enum iio_chan_type *type);
  156. /**
  157. * iio_read_channel_scale() - read the scale value for a channel
  158. * @chan: The channel being queried.
  159. * @val: First part of value read back.
  160. * @val2: Second part of value read back.
  161. *
  162. * Note returns a description of what is in val and val2, such
  163. * as IIO_VAL_INT_PLUS_MICRO telling us we have a value of val
  164. * + val2/1e6
  165. */
  166. int iio_read_channel_scale(struct iio_channel *chan, int *val,
  167. int *val2);
  168. /**
  169. * iio_convert_raw_to_processed() - Converts a raw value to a processed value
  170. * @chan: The channel being queried
  171. * @raw: The raw IIO to convert
  172. * @processed: The result of the conversion
  173. * @scale: Scale factor to apply during the conversion
  174. *
  175. * Returns an error code or 0.
  176. *
  177. * This function converts a raw value to processed value for a specific channel.
  178. * A raw value is the device internal representation of a sample and the value
  179. * returned by iio_read_channel_raw, so the unit of that value is device
  180. * depended. A processed value on the other hand is value has a normed unit
  181. * according with the IIO specification.
  182. *
  183. * The scale factor allows to increase the precession of the returned value. For
  184. * a scale factor of 1 the function will return the result in the normal IIO
  185. * unit for the channel type. E.g. millivolt for voltage channels, if you want
  186. * nanovolts instead pass 1000000 as the scale factor.
  187. */
  188. int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
  189. int *processed, unsigned int scale);
  190. #endif