qcom-vadc-common.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. #define VADC_CONV_TIME_MIN_US 2000
  8. #define VADC_CONV_TIME_MAX_US 2100
  9. /* Min ADC code represents 0V */
  10. #define VADC_MIN_ADC_CODE 0x6000
  11. /* Max ADC code represents full-scale range of 1.8V */
  12. #define VADC_MAX_ADC_CODE 0xa800
  13. #define VADC_ABSOLUTE_RANGE_UV 625000
  14. #define VADC_RATIOMETRIC_RANGE 1800
  15. #define VADC_DEF_PRESCALING 0 /* 1:1 */
  16. #define VADC_DEF_DECIMATION 0 /* 512 */
  17. #define VADC_DEF_HW_SETTLE_TIME 0 /* 0 us */
  18. #define VADC_DEF_AVG_SAMPLES 0 /* 1 sample */
  19. #define VADC_DEF_CALIB_TYPE VADC_CALIB_ABSOLUTE
  20. #define VADC_DECIMATION_MIN 512
  21. #define VADC_DECIMATION_MAX 4096
  22. #define ADC5_DEF_VBAT_PRESCALING 1 /* 1:3 */
  23. #define ADC5_DECIMATION_SHORT 250
  24. #define ADC5_DECIMATION_MEDIUM 420
  25. #define ADC5_DECIMATION_LONG 840
  26. /* Default decimation - 1024 for rev2, 840 for pmic5 */
  27. #define ADC5_DECIMATION_DEFAULT 2
  28. #define ADC5_DECIMATION_SAMPLES_MAX 3
  29. #define VADC_HW_SETTLE_DELAY_MAX 10000
  30. #define VADC_HW_SETTLE_SAMPLES_MAX 16
  31. #define VADC_AVG_SAMPLES_MAX 512
  32. #define ADC5_AVG_SAMPLES_MAX 16
  33. #define PMIC5_CHG_TEMP_SCALE_FACTOR 377500
  34. #define PMIC5_SMB_TEMP_CONSTANT 419400
  35. #define PMIC5_SMB_TEMP_SCALE_FACTOR 356
  36. #define PMI_CHG_SCALE_1 -138890
  37. #define PMI_CHG_SCALE_2 391750000000LL
  38. #define VADC5_MAX_CODE 0x7fff
  39. #define ADC5_FULL_SCALE_CODE 0x70e4
  40. #define ADC5_USR_DATA_CHECK 0x8000
  41. #define R_PU_100K 100000
  42. #define RATIO_MAX_ADC7 BIT(14)
  43. #define DIE_TEMP_ADC7_SCALE_1 -60000
  44. #define DIE_TEMP_ADC7_SCALE_2 20000
  45. #define DIE_TEMP_ADC7_SCALE_FACTOR 1000
  46. #define DIE_TEMP_ADC7_MAX 160000
  47. /**
  48. * struct vadc_map_pt - Map the graph representation for ADC channel
  49. * @x: Represent the ADC digitized code.
  50. * @y: Represent the physical data which can be temperature, voltage,
  51. * resistance.
  52. */
  53. struct vadc_map_pt {
  54. s32 x;
  55. s32 y;
  56. };
  57. /*
  58. * VADC_CALIB_ABSOLUTE: uses the 625mV and 1.25V as reference channels.
  59. * VADC_CALIB_RATIOMETRIC: uses the reference voltage (1.8V) and GND for
  60. * calibration.
  61. */
  62. enum vadc_calibration {
  63. VADC_CALIB_ABSOLUTE = 0,
  64. VADC_CALIB_RATIOMETRIC
  65. };
  66. /**
  67. * struct vadc_linear_graph - Represent ADC characteristics.
  68. * @dy: numerator slope to calculate the gain.
  69. * @dx: denominator slope to calculate the gain.
  70. * @gnd: A/D word of the ground reference used for the channel.
  71. *
  72. * Each ADC device has different offset and gain parameters which are
  73. * computed to calibrate the device.
  74. */
  75. struct vadc_linear_graph {
  76. s32 dy;
  77. s32 dx;
  78. s32 gnd;
  79. };
  80. /**
  81. * struct vadc_prescale_ratio - Represent scaling ratio for ADC input.
  82. * @num: the inverse numerator of the gain applied to the input channel.
  83. * @den: the inverse denominator of the gain applied to the input channel.
  84. */
  85. struct vadc_prescale_ratio {
  86. u32 num;
  87. u32 den;
  88. };
  89. /**
  90. * enum vadc_scale_fn_type - Scaling function to convert ADC code to
  91. * physical scaled units for the channel.
  92. * SCALE_DEFAULT: Default scaling to convert raw adc code to voltage (uV).
  93. * SCALE_THERM_100K_PULLUP: Returns temperature in millidegC.
  94. * Uses a mapping table with 100K pullup.
  95. * SCALE_PMIC_THERM: Returns result in milli degree's Centigrade.
  96. * SCALE_XOTHERM: Returns XO thermistor voltage in millidegC.
  97. * SCALE_PMI_CHG_TEMP: Conversion for PMI CHG temp
  98. * SCALE_HW_CALIB_DEFAULT: Default scaling to convert raw adc code to
  99. * voltage (uV) with hardware applied offset/slope values to adc code.
  100. * SCALE_HW_CALIB_THERM_100K_PULLUP: Returns temperature in millidegC using
  101. * lookup table. The hardware applies offset/slope to adc code.
  102. * SCALE_HW_CALIB_XOTHERM: Returns XO thermistor voltage in millidegC using
  103. * 100k pullup. The hardware applies offset/slope to adc code.
  104. * SCALE_HW_CALIB_THERM_100K_PU_PM7: Returns temperature in millidegC using
  105. * lookup table for PMIC7. The hardware applies offset/slope to adc code.
  106. * SCALE_HW_CALIB_PMIC_THERM: Returns result in milli degree's Centigrade.
  107. * The hardware applies offset/slope to adc code.
  108. * SCALE_HW_CALIB_PMIC_THERM: Returns result in milli degree's Centigrade.
  109. * The hardware applies offset/slope to adc code. This is for PMIC7.
  110. * SCALE_HW_CALIB_PM5_CHG_TEMP: Returns result in millidegrees for PMIC5
  111. * charger temperature.
  112. * SCALE_HW_CALIB_PM5_SMB_TEMP: Returns result in millidegrees for PMIC5
  113. * SMB1390 temperature.
  114. */
  115. enum vadc_scale_fn_type {
  116. SCALE_DEFAULT = 0,
  117. SCALE_THERM_100K_PULLUP,
  118. SCALE_PMIC_THERM,
  119. SCALE_XOTHERM,
  120. SCALE_PMI_CHG_TEMP,
  121. SCALE_HW_CALIB_DEFAULT,
  122. SCALE_HW_CALIB_THERM_100K_PULLUP,
  123. SCALE_HW_CALIB_XOTHERM,
  124. SCALE_HW_CALIB_THERM_100K_PU_PM7,
  125. SCALE_HW_CALIB_PMIC_THERM,
  126. SCALE_HW_CALIB_PMIC_THERM_PM7,
  127. SCALE_HW_CALIB_PM5_CHG_TEMP,
  128. SCALE_HW_CALIB_PM5_SMB_TEMP,
  129. SCALE_HW_CALIB_INVALID,
  130. };
  131. struct adc5_data {
  132. const u32 full_scale_code_volt;
  133. const u32 full_scale_code_cur;
  134. const struct adc5_channels *adc_chans;
  135. const struct iio_info *info;
  136. unsigned int *decimation;
  137. unsigned int *hw_settle_1;
  138. unsigned int *hw_settle_2;
  139. };
  140. int qcom_vadc_scale(enum vadc_scale_fn_type scaletype,
  141. const struct vadc_linear_graph *calib_graph,
  142. const struct vadc_prescale_ratio *prescale,
  143. bool absolute,
  144. u16 adc_code, int *result_mdec);
  145. struct qcom_adc5_scale_type {
  146. int (*scale_fn)(const struct vadc_prescale_ratio *prescale,
  147. const struct adc5_data *data, u16 adc_code, int *result);
  148. };
  149. int qcom_adc5_hw_scale(enum vadc_scale_fn_type scaletype,
  150. const struct vadc_prescale_ratio *prescale,
  151. const struct adc5_data *data,
  152. u16 adc_code, int *result_mdec);
  153. int qcom_vadc_decimation_from_dt(u32 value);
  154. #endif /* QCOM_VADC_COMMON_H */