qcom-vadc-common.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 KELVINMIL_CELSIUSMIL 273150
  34. #define PMIC5_CHG_TEMP_SCALE_FACTOR 377500
  35. #define PMIC5_SMB_TEMP_CONSTANT 419400
  36. #define PMIC5_SMB_TEMP_SCALE_FACTOR 356
  37. #define PMI_CHG_SCALE_1 -138890
  38. #define PMI_CHG_SCALE_2 391750000000LL
  39. #define VADC5_MAX_CODE 0x7fff
  40. #define ADC5_FULL_SCALE_CODE 0x70e4
  41. #define ADC5_USR_DATA_CHECK 0x8000
  42. /**
  43. * struct vadc_map_pt - Map the graph representation for ADC channel
  44. * @x: Represent the ADC digitized code.
  45. * @y: Represent the physical data which can be temperature, voltage,
  46. * resistance.
  47. */
  48. struct vadc_map_pt {
  49. s32 x;
  50. s32 y;
  51. };
  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. * struct vadc_prescale_ratio - Represent scaling ratio for ADC input.
  77. * @num: the inverse numerator of the gain applied to the input channel.
  78. * @den: the inverse denominator of the gain applied to the input channel.
  79. */
  80. struct vadc_prescale_ratio {
  81. u32 num;
  82. u32 den;
  83. };
  84. /**
  85. * enum vadc_scale_fn_type - Scaling function to convert ADC code to
  86. * physical scaled units for the channel.
  87. * SCALE_DEFAULT: Default scaling to convert raw adc code to voltage (uV).
  88. * SCALE_THERM_100K_PULLUP: Returns temperature in millidegC.
  89. * Uses a mapping table with 100K pullup.
  90. * SCALE_PMIC_THERM: Returns result in milli degree's Centigrade.
  91. * SCALE_XOTHERM: Returns XO thermistor voltage in millidegC.
  92. * SCALE_PMI_CHG_TEMP: Conversion for PMI CHG temp
  93. * SCALE_HW_CALIB_DEFAULT: Default scaling to convert raw adc code to
  94. * voltage (uV) with hardware applied offset/slope values to adc code.
  95. * SCALE_HW_CALIB_THERM_100K_PULLUP: Returns temperature in millidegC using
  96. * lookup table. The hardware applies offset/slope to adc code.
  97. * SCALE_HW_CALIB_XOTHERM: Returns XO thermistor voltage in millidegC using
  98. * 100k pullup. The hardware applies offset/slope to adc code.
  99. * SCALE_HW_CALIB_PMIC_THERM: Returns result in milli degree's Centigrade.
  100. * The hardware applies offset/slope to adc code.
  101. * SCALE_HW_CALIB_PM5_CHG_TEMP: Returns result in millidegrees for PMIC5
  102. * charger temperature.
  103. * SCALE_HW_CALIB_PM5_SMB_TEMP: Returns result in millidegrees for PMIC5
  104. * SMB1390 temperature.
  105. */
  106. enum vadc_scale_fn_type {
  107. SCALE_DEFAULT = 0,
  108. SCALE_THERM_100K_PULLUP,
  109. SCALE_PMIC_THERM,
  110. SCALE_XOTHERM,
  111. SCALE_PMI_CHG_TEMP,
  112. SCALE_HW_CALIB_DEFAULT,
  113. SCALE_HW_CALIB_THERM_100K_PULLUP,
  114. SCALE_HW_CALIB_XOTHERM,
  115. SCALE_HW_CALIB_PMIC_THERM,
  116. SCALE_HW_CALIB_PM5_CHG_TEMP,
  117. SCALE_HW_CALIB_PM5_SMB_TEMP,
  118. SCALE_HW_CALIB_INVALID,
  119. };
  120. struct adc5_data {
  121. const u32 full_scale_code_volt;
  122. const u32 full_scale_code_cur;
  123. const struct adc5_channels *adc_chans;
  124. unsigned int *decimation;
  125. unsigned int *hw_settle_1;
  126. unsigned int *hw_settle_2;
  127. };
  128. int qcom_vadc_scale(enum vadc_scale_fn_type scaletype,
  129. const struct vadc_linear_graph *calib_graph,
  130. const struct vadc_prescale_ratio *prescale,
  131. bool absolute,
  132. u16 adc_code, int *result_mdec);
  133. struct qcom_adc5_scale_type {
  134. int (*scale_fn)(const struct vadc_prescale_ratio *prescale,
  135. const struct adc5_data *data, u16 adc_code, int *result);
  136. };
  137. int qcom_adc5_hw_scale(enum vadc_scale_fn_type scaletype,
  138. const struct vadc_prescale_ratio *prescale,
  139. const struct adc5_data *data,
  140. u16 adc_code, int *result_mdec);
  141. int qcom_vadc_decimation_from_dt(u32 value);
  142. #endif /* QCOM_VADC_COMMON_H */