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