adc.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright (C) 2015 Samsung Electronics
  3. * Przemyslaw Marczak <p.marczak@samsung.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #ifndef _ADC_H_
  8. #define _ADC_H_
  9. /* ADC_CHANNEL() - ADC channel bit mask, to select only required channels */
  10. #define ADC_CHANNEL(x) (1 << x)
  11. /* The last possible selected channel with 32-bit mask */
  12. #define ADC_MAX_CHANNEL 31
  13. /**
  14. * adc_data_format: define the ADC output data format, can be useful when
  15. * the device's input Voltage range is bipolar.
  16. * - ADC_DATA_FORMAT_BIN - binary offset
  17. * - ADC_DATA_FORMAT_2S - two's complement
  18. *
  19. * Note: Device's driver should fill the 'data_format' field of its uclass's
  20. * platform data using one of the above data format types.
  21. */
  22. enum adc_data_format {
  23. ADC_DATA_FORMAT_BIN,
  24. ADC_DATA_FORMAT_2S,
  25. };
  26. /**
  27. * struct adc_channel - structure to hold channel conversion data.
  28. * Useful to keep the result of a multi-channel conversion output.
  29. *
  30. * @id - channel id
  31. * @data - channel conversion data
  32. */
  33. struct adc_channel {
  34. int id;
  35. unsigned int data;
  36. };
  37. /**
  38. * struct adc_uclass_platdata - basic ADC info
  39. *
  40. * Note: The positive/negative reference Voltage is only a name and it doesn't
  41. * provide an information about the value polarity. It is possible, for both
  42. * values to be a negative or positive. For this purpose the uclass's platform
  43. * data provides a bool fields: 'vdd/vss_supply_is_negative'. This is useful,
  44. * since the regulator API returns only a positive Voltage values.
  45. *
  46. * To get the reference Voltage values with polarity, use functions:
  47. * - adc_vdd_value()
  48. * - adc_vss_value()
  49. * Those are useful for some cases of ADC's references, e.g.:
  50. * * Vdd: +3.3V; Vss: -3.3V -> 6.6 Vdiff
  51. * * Vdd: +3.3V; Vss: +0.3V -> 3.0 Vdiff
  52. * * Vdd: +3.3V; Vss: 0.0V -> 3.3 Vdiff
  53. * The last one is usually standard and doesn't require the fdt polarity info.
  54. *
  55. * For more informations read binding info:
  56. * - doc/device-tree-bindings/adc/adc.txt
  57. *
  58. * @data_mask - conversion output data mask
  59. * @data_timeout_us - single channel conversion timeout
  60. * @multidata_timeout_us - multi channel conversion timeout
  61. * @channel_mask - bit mask of available channels [0:31]
  62. * @vdd_supply - positive reference Voltage supply (regulator)
  63. * @vss_supply - negative reference Voltage supply (regulator)
  64. * @vdd_polarity_negative - positive reference Voltage has negative polarity
  65. * @vss_polarity_negative - negative reference Voltage has negative polarity
  66. * @vdd_microvolts - positive reference Voltage value
  67. * @vss_microvolts - negative reference Voltage value
  68. */
  69. struct adc_uclass_platdata {
  70. int data_format;
  71. unsigned int data_mask;
  72. unsigned int data_timeout_us;
  73. unsigned int multidata_timeout_us;
  74. unsigned int channel_mask;
  75. struct udevice *vdd_supply;
  76. struct udevice *vss_supply;
  77. bool vdd_polarity_negative;
  78. bool vss_polarity_negative;
  79. int vdd_microvolts;
  80. int vss_microvolts;
  81. };
  82. /**
  83. * struct adc_ops - ADC device operations for single/multi-channel operation.
  84. */
  85. struct adc_ops {
  86. /**
  87. * start_channel() - start conversion with its default parameters
  88. * for the given channel number.
  89. *
  90. * @dev: ADC device to init
  91. * @channel: analog channel number
  92. * @return: 0 if OK, -ve on error
  93. */
  94. int (*start_channel)(struct udevice *dev, int channel);
  95. /**
  96. * start_channels() - start conversion with its default parameters
  97. * for the channel numbers selected by the bit mask.
  98. *
  99. * This is optional, useful when the hardware supports multichannel
  100. * conversion by the single software trigger.
  101. *
  102. * @dev: ADC device to init
  103. * @channel_mask: bit mask of selected analog channels
  104. * @return: 0 if OK, -ve on error
  105. */
  106. int (*start_channels)(struct udevice *dev, unsigned int channel_mask);
  107. /**
  108. * channel_data() - get conversion output data for the given channel.
  109. *
  110. * Note: The implementation of this function should only check, that
  111. * the conversion data is available at the call time. If the hardware
  112. * requires some delay to get the data, then this function should
  113. * return with -EBUSY value. The ADC API will call it in a loop,
  114. * until the data is available or the timeout expires. The maximum
  115. * timeout for this operation is defined by the field 'data_timeout_us'
  116. * in ADC uclasses platform data structure.
  117. *
  118. * @dev: ADC device to trigger
  119. * @channel: selected analog channel number
  120. * @data: returned pointer to selected channel's output data
  121. * @return: 0 if OK, -EBUSY if busy, and other negative on error
  122. */
  123. int (*channel_data)(struct udevice *dev, int channel,
  124. unsigned int *data);
  125. /**
  126. * channels_data() - get conversion data for the selected channels.
  127. *
  128. * This is optional, useful when multichannel conversion is supported
  129. * by the hardware, by the single software trigger.
  130. *
  131. * For the proper implementation, please look at the 'Note' for the
  132. * above method. The only difference is in used timeout value, which
  133. * is defined by field 'multidata_timeout_us'.
  134. *
  135. * @dev: ADC device to trigger
  136. * @channel_mask: bit mask of selected analog channels
  137. * @channels: returned pointer to array of output data for channels
  138. * selected by the given mask
  139. * @return: 0 if OK, -ve on error
  140. */
  141. int (*channels_data)(struct udevice *dev, unsigned int channel_mask,
  142. struct adc_channel *channels);
  143. /**
  144. * stop() - stop conversion of the given ADC device
  145. *
  146. * @dev: ADC device to stop
  147. * @return: 0 if OK, -ve on error
  148. */
  149. int (*stop)(struct udevice *dev);
  150. };
  151. /**
  152. * adc_start_channel() - start conversion for given device/channel and exit.
  153. *
  154. * @dev: ADC device
  155. * @channel: analog channel number
  156. * @return: 0 if OK, -ve on error
  157. */
  158. int adc_start_channel(struct udevice *dev, int channel);
  159. /**
  160. * adc_start_channels() - start conversion for given device/channels and exit.
  161. *
  162. * Note:
  163. * To use this function, device must implement method: start_channels().
  164. *
  165. * @dev: ADC device to start
  166. * @channel_mask: channel selection - a bit mask
  167. * @channel_mask: bit mask of analog channels
  168. * @return: 0 if OK, -ve on error
  169. */
  170. int adc_start_channels(struct udevice *dev, unsigned int channel_mask);
  171. /**
  172. * adc_channel_data() - get conversion data for the given device channel number.
  173. *
  174. * @dev: ADC device to read
  175. * @channel: analog channel number
  176. * @data: pointer to returned channel's data
  177. * @return: 0 if OK, -ve on error
  178. */
  179. int adc_channel_data(struct udevice *dev, int channel, unsigned int *data);
  180. /**
  181. * adc_channels_data() - get conversion data for the channels selected by mask
  182. *
  183. * Note:
  184. * To use this function, device must implement methods:
  185. * - start_channels()
  186. * - channels_data()
  187. *
  188. * @dev: ADC device to read
  189. * @channel_mask: channel selection - a bit mask
  190. * @channels: pointer to structure array of returned data for each channel
  191. * @return: 0 if OK, -ve on error
  192. */
  193. int adc_channels_data(struct udevice *dev, unsigned int channel_mask,
  194. struct adc_channel *channels);
  195. /**
  196. * adc_data_mask() - get data mask (ADC resolution bitmask) for given ADC device
  197. *
  198. * This can be used if adc uclass platform data is filled.
  199. *
  200. * @dev: ADC device to check
  201. * @data_mask: pointer to the returned data bitmask
  202. * @return: 0 if OK, -ve on error
  203. */
  204. int adc_data_mask(struct udevice *dev, unsigned int *data_mask);
  205. /**
  206. * adc_channel_single_shot() - get output data of conversion for the ADC
  207. * device's channel. This function searches for the device with the given name,
  208. * starts the given channel conversion and returns the output data.
  209. *
  210. * Note: To use this function, device must implement metods:
  211. * - start_channel()
  212. * - channel_data()
  213. *
  214. * @name: device's name to search
  215. * @channel: device's input channel to init
  216. * @data: pointer to conversion output data
  217. * @return: 0 if OK, -ve on error
  218. */
  219. int adc_channel_single_shot(const char *name, int channel, unsigned int *data);
  220. /**
  221. * adc_channels_single_shot() - get ADC conversion output data for the selected
  222. * device's channels. This function searches for the device by the given name,
  223. * starts the selected channels conversion and returns the output data as array
  224. * of type 'struct adc_channel'.
  225. *
  226. * Note: This function can be used if device implements one of ADC's single
  227. * or multi-channel operation API. If multi-channel operation is not supported,
  228. * then each selected channel is triggered by the sequence start/data in a loop.
  229. *
  230. * @name: device's name to search
  231. * @channel_mask: channel selection - a bit mask
  232. * @channels: pointer to conversion output data for the selected channels
  233. * @return: 0 if OK, -ve on error
  234. */
  235. int adc_channels_single_shot(const char *name, unsigned int channel_mask,
  236. struct adc_channel *channels);
  237. /**
  238. * adc_vdd_value() - get the ADC device's positive reference Voltage value
  239. *
  240. * Note: Depending on bool value 'vdd_supply_is_negative' of platform data,
  241. * the returned uV value can be negative, and it's not an error.
  242. *
  243. * @dev: ADC device to check
  244. * @uV: Voltage value with polarization sign (uV)
  245. * @return: 0 on success or -ve on error
  246. */
  247. int adc_vdd_value(struct udevice *dev, int *uV);
  248. /**
  249. * adc_vss_value() - get the ADC device's negative reference Voltage value
  250. *
  251. * Note: Depending on bool value 'vdd_supply_is_negative' of platform data,
  252. * the returned uV value can be negative, and it's not an error.
  253. *
  254. * @dev: ADC device to check
  255. * @uV: Voltage value with polarization sign (uV)
  256. * @return: 0 on success or -ve on error
  257. */
  258. int adc_vss_value(struct udevice *dev, int *uV);
  259. /**
  260. * adc_stop() - stop operation for given ADC device.
  261. *
  262. * @dev: ADC device to stop
  263. * @return: 0 if OK, -ve on error
  264. */
  265. int adc_stop(struct udevice *dev);
  266. #endif