qcom-vadc-common.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Code shared between the different Qualcomm PMIC voltage ADCs
  4. */
  5. #ifndef QCOM_VADC_COMMON_H
  6. #define QCOM_VADC_COMMON_H
  7. #include <linux/adc-tm-clients.h>
  8. #include <linux/math.h>
  9. #include <linux/types.h>
  10. #define VADC_CONV_TIME_MIN_US 2000
  11. #define VADC_CONV_TIME_MAX_US 2100
  12. /* Min ADC code represents 0V */
  13. #define VADC_MIN_ADC_CODE 0x6000
  14. /* Max ADC code represents full-scale range of 1.8V */
  15. #define VADC_MAX_ADC_CODE 0xa800
  16. #define VADC_ABSOLUTE_RANGE_UV 625000
  17. #define VADC_RATIOMETRIC_RANGE 1800
  18. #define VADC_DEF_PRESCALING 0 /* 1:1 */
  19. #define VADC_DEF_DECIMATION 0 /* 512 */
  20. #define VADC_DEF_HW_SETTLE_TIME 0 /* 0 us */
  21. #define VADC_DEF_AVG_SAMPLES 0 /* 1 sample */
  22. #define VADC_DEF_CALIB_TYPE VADC_CALIB_ABSOLUTE
  23. #define VADC_DECIMATION_MIN 512
  24. #define VADC_DECIMATION_MAX 4096
  25. #define ADC5_DEF_VBAT_PRESCALING 1 /* 1:3 */
  26. #define ADC5_DECIMATION_SHORT 250
  27. #define ADC5_DECIMATION_MEDIUM 420
  28. #define ADC5_DECIMATION_LONG 840
  29. /* Default decimation - 1024 for rev2, 840 for pmic5 */
  30. #define ADC5_DECIMATION_DEFAULT 2
  31. #define ADC5_DECIMATION_SAMPLES_MAX 3
  32. #define VADC_HW_SETTLE_DELAY_MAX 10000
  33. #define VADC_HW_SETTLE_SAMPLES_MAX 16
  34. #define VADC_AVG_SAMPLES_MAX 512
  35. #define ADC5_AVG_SAMPLES_MAX 16
  36. #define PMIC5_CHG_TEMP_SCALE_FACTOR 377500
  37. #define PMIC5_SMB_TEMP_CONSTANT 419400
  38. #define PMIC5_SMB_TEMP_SCALE_FACTOR 356
  39. #define PMIC5_SMB1398_TEMP_CONSTANT 268235
  40. #define PMIC5_SMB1398_TEMP_SCALE_FACTOR 340
  41. #define PMIC5_PM2250_S3_DIE_TEMP_SCALE_FACTOR 187263
  42. #define PMIC5_PM2250_S3_DIE_TEMP_CONSTANT 720100
  43. #define PMI_CHG_SCALE_1 -138890
  44. #define PMI_CHG_SCALE_2 391750000000LL
  45. #define VADC5_MAX_CODE 0x7fff
  46. #define ADC5_FULL_SCALE_CODE 0x70e4
  47. #define ADC5_USR_DATA_CHECK 0x8000
  48. #define R_PU_100K 100000
  49. #define RATIO_MAX_ADC7 BIT(14)
  50. #define PMIC5_GEN3_USB_IN_I_SCALE_FACTOR 9248
  51. #define ADC_VDD_REF 1875000
  52. /*
  53. * VADC_CALIB_ABSOLUTE: uses the 625mV and 1.25V as reference channels.
  54. * VADC_CALIB_RATIOMETRIC: uses the reference voltage (1.8V) and GND for
  55. * calibration.
  56. */
  57. enum vadc_calibration {
  58. VADC_CALIB_ABSOLUTE = 0,
  59. VADC_CALIB_RATIOMETRIC
  60. };
  61. /**
  62. * struct vadc_linear_graph - Represent ADC characteristics.
  63. * @dy: numerator slope to calculate the gain.
  64. * @dx: denominator slope to calculate the gain.
  65. * @gnd: A/D word of the ground reference used for the channel.
  66. *
  67. * Each ADC device has different offset and gain parameters which are
  68. * computed to calibrate the device.
  69. */
  70. struct vadc_linear_graph {
  71. s32 dy;
  72. s32 dx;
  73. s32 gnd;
  74. };
  75. /**
  76. * enum adc_tm_rscale_fn_type - Scaling function used to convert the
  77. * channels input voltage/temperature to corresponding ADC code that is
  78. * applied for thresholds. Check the corresponding channels scaling to
  79. * determine the appropriate temperature/voltage units that are passed
  80. * to the scaling function. Example battery follows the power supply
  81. * framework that needs its units to be in decidegreesC so it passes
  82. * deci-degreesC. PA_THERM clients pass the temperature in degrees.
  83. * The order below should match the one in the driver for
  84. * adc_tm_rscale_fn[].
  85. */
  86. enum adc_tm_rscale_fn_type {
  87. SCALE_R_ABSOLUTE = 0,
  88. SCALE_RSCALE_NONE,
  89. };
  90. /**
  91. * struct adc_tm_config - Represent ADC Thermal Monitor configuration.
  92. * @high_thr_temp: Temperature at which high threshold notification is required.
  93. * @low_thr_temp: Temperature at which low threshold notification is required.
  94. * @low_thr_voltage : Low threshold voltage ADC code used for reverse
  95. * calibration.
  96. * @high_thr_voltage: High threshold voltage ADC code used for reverse
  97. * calibration.
  98. */
  99. struct adc_tm_config {
  100. int high_thr_temp;
  101. int low_thr_temp;
  102. int64_t high_thr_voltage;
  103. int64_t low_thr_voltage;
  104. };
  105. struct adc_tm_reverse_scale_fn {
  106. int32_t (*chan)(struct adc_tm_config *tm_config);
  107. };
  108. struct adc_tm_client_info {
  109. struct list_head list;
  110. struct adc_tm_param *param;
  111. int32_t low_thr_requested;
  112. int32_t high_thr_requested;
  113. bool notify_low_thr;
  114. bool notify_high_thr;
  115. bool high_thr_set;
  116. bool low_thr_set;
  117. enum adc_tm_state_request state_request;
  118. };
  119. /**
  120. * enum vadc_scale_fn_type - Scaling function to convert ADC code to
  121. * physical scaled units for the channel.
  122. * SCALE_DEFAULT: Default scaling to convert raw adc code to voltage (uV).
  123. * SCALE_THERM_100K_PULLUP: Returns temperature in millidegC.
  124. * Uses a mapping table with 100K pullup.
  125. * SCALE_PMIC_THERM: Returns result in milli degree's Centigrade.
  126. * SCALE_XOTHERM: Returns XO thermistor voltage in millidegC.
  127. * SCALE_PMI_CHG_TEMP: Conversion for PMI CHG temp
  128. * SCALE_HW_CALIB_DEFAULT: Default scaling to convert raw adc code to
  129. * voltage (uV) with hardware applied offset/slope values to adc code.
  130. * SCALE_HW_CALIB_THERM_100K_PULLUP: Returns temperature in millidegC using
  131. * lookup table. The hardware applies offset/slope to adc code.
  132. * SCALE_HW_CALIB_XOTHERM: Returns XO thermistor voltage in millidegC using
  133. * 100k pullup. The hardware applies offset/slope to adc code.
  134. * SCALE_HW_CALIB_THERM_100K_PU_PM7: Returns temperature in millidegC using
  135. * lookup table for PMIC7. The hardware applies offset/slope to adc code.
  136. * SCALE_HW_CALIB_PMIC_THERM: Returns result in milli degree's Centigrade.
  137. * The hardware applies offset/slope to adc code.
  138. * SCALE_HW_CALIB_PMIC_THERM_PM7: Returns result in milli degree's Centigrade.
  139. * The hardware applies offset/slope to adc code. This is for PMIC7.
  140. * SCALE_HW_CALIB_PM5_CHG_TEMP: Returns result in millidegrees for PMIC5
  141. * charger temperature.
  142. * SCALE_HW_CALIB_PM5_SMB_TEMP: Returns result in millidegrees for PMIC5
  143. * SMB1390 temperature.
  144. * SCALE_HW_CALIB_BATT_THERM_100K: Returns battery thermistor temperature in
  145. * decidegC using 100k pullup. The hardware applies offset/slope to adc
  146. * code.
  147. * SCALE_HW_CALIB_BATT_THERM_30K: Returns battery thermistor temperature in
  148. * decidegC using 30k pullup. The hardware applies offset/slope to adc
  149. * code.
  150. * SCALE_HW_CALIB_BATT_THERM_400K: Returns battery thermistor temperature in
  151. * decidegC using 400k pullup. The hardware applies offset/slope to adc
  152. * code.
  153. * SCALE_HW_CALIB_PM5_SMB1398_TEMP: Returns result in millidegrees for PMIC5
  154. * SMB1398 temperature.
  155. * SCALE_HW_CALIB_PM7_SMB_TEMP: Returns result in millidegrees for PMIC7
  156. * SMB139x temperature.
  157. * SCALE_HW_CALIB_PM7_CHG_TEMP: Returns result in millidegrees for PMIC7
  158. * charger temperature.
  159. * SCALE_HW_CALIB_CUR: Returns result in microamperes for PMIC7 channels that
  160. * use voltage scaling.
  161. * SCALE_HW_CALIB_CUR_RAW: Returns result in microamperes for PMIC7 channels
  162. * that use raw ADC code.
  163. * SCALE_HW_CALIB_PM2250_S3_DIE_TEMP: Returns result in millidegrees for
  164. * S3 die temperature channel on PM2250.
  165. * SCALE_HW_CALIB_PM5_CUR: Returns result in microamperes for PMIC5 channels
  166. * that use voltage scaling.
  167. * SCALE_HW_CALIB_PM5_GEN3_BATT_THERM_100K: Returns battery thermistor
  168. * temperature in decidegC using 100k pullup. The hardware applies
  169. * offset/slope to adc code.
  170. * SCALE_HW_CALIB_PM5_GEN3_BATT_ID_100K: Returns battery ID resistance
  171. * in ohms using 100k pullup. The hardware applies offset/slope to
  172. * adc code.
  173. * SCALE_HW_CALIB_PM5_GEN3_USB_IN_I: Returns USB input current in microamperes.
  174. */
  175. enum vadc_scale_fn_type {
  176. SCALE_DEFAULT = 0,
  177. SCALE_THERM_100K_PULLUP,
  178. SCALE_PMIC_THERM,
  179. SCALE_XOTHERM,
  180. SCALE_PMI_CHG_TEMP,
  181. SCALE_HW_CALIB_DEFAULT,
  182. SCALE_HW_CALIB_THERM_100K_PULLUP,
  183. SCALE_HW_CALIB_XOTHERM,
  184. SCALE_HW_CALIB_THERM_100K_PU_PM7,
  185. SCALE_HW_CALIB_PMIC_THERM,
  186. SCALE_HW_CALIB_PMIC_THERM_PM7,
  187. SCALE_HW_CALIB_PM5_CHG_TEMP,
  188. SCALE_HW_CALIB_PM5_SMB_TEMP,
  189. SCALE_HW_CALIB_BATT_THERM_100K,
  190. SCALE_HW_CALIB_BATT_THERM_30K,
  191. SCALE_HW_CALIB_BATT_THERM_400K,
  192. SCALE_HW_CALIB_PM5_SMB1398_TEMP,
  193. SCALE_HW_CALIB_PM7_SMB_TEMP,
  194. SCALE_HW_CALIB_PM7_CHG_TEMP,
  195. SCALE_HW_CALIB_CUR,
  196. SCALE_HW_CALIB_CUR_RAW,
  197. SCALE_HW_CALIB_PM2250_S3_DIE_TEMP,
  198. SCALE_HW_CALIB_PM5_CUR,
  199. SCALE_HW_CALIB_PM5_GEN3_BATT_THERM_100K,
  200. SCALE_HW_CALIB_PM5_GEN3_BATT_ID_100K,
  201. SCALE_HW_CALIB_PM5_GEN3_USB_IN_I,
  202. SCALE_HW_CALIB_INVALID,
  203. };
  204. struct adc5_data {
  205. const char *name;
  206. const u32 full_scale_code_volt;
  207. const u32 full_scale_code_cur;
  208. const struct adc5_channels *adc_chans;
  209. const struct iio_info *info;
  210. unsigned int *decimation;
  211. unsigned int *hw_settle_1;
  212. unsigned int *hw_settle_2;
  213. };
  214. int qcom_vadc_scale(enum vadc_scale_fn_type scaletype,
  215. const struct vadc_linear_graph *calib_graph,
  216. const struct u32_fract *prescale,
  217. bool absolute,
  218. u16 adc_code, int *result_mdec);
  219. struct qcom_adc5_scale_type {
  220. int (*scale_fn)(const struct u32_fract *prescale,
  221. const struct adc5_data *data, u16 adc_code, int *result);
  222. };
  223. int qcom_adc5_hw_scale(enum vadc_scale_fn_type scaletype,
  224. unsigned int prescale_ratio,
  225. const struct adc5_data *data,
  226. u16 adc_code, int *result_mdec);
  227. u16 qcom_adc_tm5_temp_volt_scale(unsigned int prescale_ratio,
  228. u32 full_scale_code_volt, int temp);
  229. u16 qcom_adc_tm5_gen2_temp_res_scale(int temp);
  230. int qcom_adc5_prescaling_from_dt(u32 num, u32 den);
  231. int qcom_adc5_hw_settle_time_from_dt(u32 value, const unsigned int *hw_settle);
  232. int qcom_adc5_avg_samples_from_dt(u32 value);
  233. int qcom_adc5_decimation_from_dt(u32 value, const unsigned int *decimation);
  234. int qcom_vadc_decimation_from_dt(u32 value);
  235. void adc_tm_scale_therm_voltage_100k_gen3(struct adc_tm_config *param);
  236. int32_t adc_tm_absolute_rthr_gen3(struct adc_tm_config *tm_config);
  237. #endif /* QCOM_VADC_COMMON_H */