consumer.h 6.2 KB

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