18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Code shared between the different Qualcomm PMIC voltage ADCs 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#ifndef QCOM_VADC_COMMON_H 78c2ecf20Sopenharmony_ci#define QCOM_VADC_COMMON_H 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#define VADC_CONV_TIME_MIN_US 2000 108c2ecf20Sopenharmony_ci#define VADC_CONV_TIME_MAX_US 2100 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci/* Min ADC code represents 0V */ 138c2ecf20Sopenharmony_ci#define VADC_MIN_ADC_CODE 0x6000 148c2ecf20Sopenharmony_ci/* Max ADC code represents full-scale range of 1.8V */ 158c2ecf20Sopenharmony_ci#define VADC_MAX_ADC_CODE 0xa800 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#define VADC_ABSOLUTE_RANGE_UV 625000 188c2ecf20Sopenharmony_ci#define VADC_RATIOMETRIC_RANGE 1800 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci#define VADC_DEF_PRESCALING 0 /* 1:1 */ 218c2ecf20Sopenharmony_ci#define VADC_DEF_DECIMATION 0 /* 512 */ 228c2ecf20Sopenharmony_ci#define VADC_DEF_HW_SETTLE_TIME 0 /* 0 us */ 238c2ecf20Sopenharmony_ci#define VADC_DEF_AVG_SAMPLES 0 /* 1 sample */ 248c2ecf20Sopenharmony_ci#define VADC_DEF_CALIB_TYPE VADC_CALIB_ABSOLUTE 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci#define VADC_DECIMATION_MIN 512 278c2ecf20Sopenharmony_ci#define VADC_DECIMATION_MAX 4096 288c2ecf20Sopenharmony_ci#define ADC5_DEF_VBAT_PRESCALING 1 /* 1:3 */ 298c2ecf20Sopenharmony_ci#define ADC5_DECIMATION_SHORT 250 308c2ecf20Sopenharmony_ci#define ADC5_DECIMATION_MEDIUM 420 318c2ecf20Sopenharmony_ci#define ADC5_DECIMATION_LONG 840 328c2ecf20Sopenharmony_ci/* Default decimation - 1024 for rev2, 840 for pmic5 */ 338c2ecf20Sopenharmony_ci#define ADC5_DECIMATION_DEFAULT 2 348c2ecf20Sopenharmony_ci#define ADC5_DECIMATION_SAMPLES_MAX 3 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci#define VADC_HW_SETTLE_DELAY_MAX 10000 378c2ecf20Sopenharmony_ci#define VADC_HW_SETTLE_SAMPLES_MAX 16 388c2ecf20Sopenharmony_ci#define VADC_AVG_SAMPLES_MAX 512 398c2ecf20Sopenharmony_ci#define ADC5_AVG_SAMPLES_MAX 16 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci#define PMIC5_CHG_TEMP_SCALE_FACTOR 377500 428c2ecf20Sopenharmony_ci#define PMIC5_SMB_TEMP_CONSTANT 419400 438c2ecf20Sopenharmony_ci#define PMIC5_SMB_TEMP_SCALE_FACTOR 356 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci#define PMI_CHG_SCALE_1 -138890 468c2ecf20Sopenharmony_ci#define PMI_CHG_SCALE_2 391750000000LL 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci#define VADC5_MAX_CODE 0x7fff 498c2ecf20Sopenharmony_ci#define ADC5_FULL_SCALE_CODE 0x70e4 508c2ecf20Sopenharmony_ci#define ADC5_USR_DATA_CHECK 0x8000 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci#define R_PU_100K 100000 538c2ecf20Sopenharmony_ci#define RATIO_MAX_ADC7 BIT(14) 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci#define DIE_TEMP_ADC7_SCALE_1 -60000 568c2ecf20Sopenharmony_ci#define DIE_TEMP_ADC7_SCALE_2 20000 578c2ecf20Sopenharmony_ci#define DIE_TEMP_ADC7_SCALE_FACTOR 1000 588c2ecf20Sopenharmony_ci#define DIE_TEMP_ADC7_MAX 160000 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci/** 618c2ecf20Sopenharmony_ci * struct vadc_map_pt - Map the graph representation for ADC channel 628c2ecf20Sopenharmony_ci * @x: Represent the ADC digitized code. 638c2ecf20Sopenharmony_ci * @y: Represent the physical data which can be temperature, voltage, 648c2ecf20Sopenharmony_ci * resistance. 658c2ecf20Sopenharmony_ci */ 668c2ecf20Sopenharmony_cistruct vadc_map_pt { 678c2ecf20Sopenharmony_ci s32 x; 688c2ecf20Sopenharmony_ci s32 y; 698c2ecf20Sopenharmony_ci}; 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci/* 728c2ecf20Sopenharmony_ci * VADC_CALIB_ABSOLUTE: uses the 625mV and 1.25V as reference channels. 738c2ecf20Sopenharmony_ci * VADC_CALIB_RATIOMETRIC: uses the reference voltage (1.8V) and GND for 748c2ecf20Sopenharmony_ci * calibration. 758c2ecf20Sopenharmony_ci */ 768c2ecf20Sopenharmony_cienum vadc_calibration { 778c2ecf20Sopenharmony_ci VADC_CALIB_ABSOLUTE = 0, 788c2ecf20Sopenharmony_ci VADC_CALIB_RATIOMETRIC 798c2ecf20Sopenharmony_ci}; 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci/** 828c2ecf20Sopenharmony_ci * struct vadc_linear_graph - Represent ADC characteristics. 838c2ecf20Sopenharmony_ci * @dy: numerator slope to calculate the gain. 848c2ecf20Sopenharmony_ci * @dx: denominator slope to calculate the gain. 858c2ecf20Sopenharmony_ci * @gnd: A/D word of the ground reference used for the channel. 868c2ecf20Sopenharmony_ci * 878c2ecf20Sopenharmony_ci * Each ADC device has different offset and gain parameters which are 888c2ecf20Sopenharmony_ci * computed to calibrate the device. 898c2ecf20Sopenharmony_ci */ 908c2ecf20Sopenharmony_cistruct vadc_linear_graph { 918c2ecf20Sopenharmony_ci s32 dy; 928c2ecf20Sopenharmony_ci s32 dx; 938c2ecf20Sopenharmony_ci s32 gnd; 948c2ecf20Sopenharmony_ci}; 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci/** 978c2ecf20Sopenharmony_ci * struct vadc_prescale_ratio - Represent scaling ratio for ADC input. 988c2ecf20Sopenharmony_ci * @num: the inverse numerator of the gain applied to the input channel. 998c2ecf20Sopenharmony_ci * @den: the inverse denominator of the gain applied to the input channel. 1008c2ecf20Sopenharmony_ci */ 1018c2ecf20Sopenharmony_cistruct vadc_prescale_ratio { 1028c2ecf20Sopenharmony_ci u32 num; 1038c2ecf20Sopenharmony_ci u32 den; 1048c2ecf20Sopenharmony_ci}; 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci/** 1078c2ecf20Sopenharmony_ci * enum vadc_scale_fn_type - Scaling function to convert ADC code to 1088c2ecf20Sopenharmony_ci * physical scaled units for the channel. 1098c2ecf20Sopenharmony_ci * SCALE_DEFAULT: Default scaling to convert raw adc code to voltage (uV). 1108c2ecf20Sopenharmony_ci * SCALE_THERM_100K_PULLUP: Returns temperature in millidegC. 1118c2ecf20Sopenharmony_ci * Uses a mapping table with 100K pullup. 1128c2ecf20Sopenharmony_ci * SCALE_PMIC_THERM: Returns result in milli degree's Centigrade. 1138c2ecf20Sopenharmony_ci * SCALE_XOTHERM: Returns XO thermistor voltage in millidegC. 1148c2ecf20Sopenharmony_ci * SCALE_PMI_CHG_TEMP: Conversion for PMI CHG temp 1158c2ecf20Sopenharmony_ci * SCALE_HW_CALIB_DEFAULT: Default scaling to convert raw adc code to 1168c2ecf20Sopenharmony_ci * voltage (uV) with hardware applied offset/slope values to adc code. 1178c2ecf20Sopenharmony_ci * SCALE_HW_CALIB_THERM_100K_PULLUP: Returns temperature in millidegC using 1188c2ecf20Sopenharmony_ci * lookup table. The hardware applies offset/slope to adc code. 1198c2ecf20Sopenharmony_ci * SCALE_HW_CALIB_XOTHERM: Returns XO thermistor voltage in millidegC using 1208c2ecf20Sopenharmony_ci * 100k pullup. The hardware applies offset/slope to adc code. 1218c2ecf20Sopenharmony_ci * SCALE_HW_CALIB_THERM_100K_PU_PM7: Returns temperature in millidegC using 1228c2ecf20Sopenharmony_ci * lookup table for PMIC7. The hardware applies offset/slope to adc code. 1238c2ecf20Sopenharmony_ci * SCALE_HW_CALIB_PMIC_THERM: Returns result in milli degree's Centigrade. 1248c2ecf20Sopenharmony_ci * The hardware applies offset/slope to adc code. 1258c2ecf20Sopenharmony_ci * SCALE_HW_CALIB_PMIC_THERM: Returns result in milli degree's Centigrade. 1268c2ecf20Sopenharmony_ci * The hardware applies offset/slope to adc code. This is for PMIC7. 1278c2ecf20Sopenharmony_ci * SCALE_HW_CALIB_PM5_CHG_TEMP: Returns result in millidegrees for PMIC5 1288c2ecf20Sopenharmony_ci * charger temperature. 1298c2ecf20Sopenharmony_ci * SCALE_HW_CALIB_PM5_SMB_TEMP: Returns result in millidegrees for PMIC5 1308c2ecf20Sopenharmony_ci * SMB1390 temperature. 1318c2ecf20Sopenharmony_ci */ 1328c2ecf20Sopenharmony_cienum vadc_scale_fn_type { 1338c2ecf20Sopenharmony_ci SCALE_DEFAULT = 0, 1348c2ecf20Sopenharmony_ci SCALE_THERM_100K_PULLUP, 1358c2ecf20Sopenharmony_ci SCALE_PMIC_THERM, 1368c2ecf20Sopenharmony_ci SCALE_XOTHERM, 1378c2ecf20Sopenharmony_ci SCALE_PMI_CHG_TEMP, 1388c2ecf20Sopenharmony_ci SCALE_HW_CALIB_DEFAULT, 1398c2ecf20Sopenharmony_ci SCALE_HW_CALIB_THERM_100K_PULLUP, 1408c2ecf20Sopenharmony_ci SCALE_HW_CALIB_XOTHERM, 1418c2ecf20Sopenharmony_ci SCALE_HW_CALIB_THERM_100K_PU_PM7, 1428c2ecf20Sopenharmony_ci SCALE_HW_CALIB_PMIC_THERM, 1438c2ecf20Sopenharmony_ci SCALE_HW_CALIB_PMIC_THERM_PM7, 1448c2ecf20Sopenharmony_ci SCALE_HW_CALIB_PM5_CHG_TEMP, 1458c2ecf20Sopenharmony_ci SCALE_HW_CALIB_PM5_SMB_TEMP, 1468c2ecf20Sopenharmony_ci SCALE_HW_CALIB_INVALID, 1478c2ecf20Sopenharmony_ci}; 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_cistruct adc5_data { 1508c2ecf20Sopenharmony_ci const u32 full_scale_code_volt; 1518c2ecf20Sopenharmony_ci const u32 full_scale_code_cur; 1528c2ecf20Sopenharmony_ci const struct adc5_channels *adc_chans; 1538c2ecf20Sopenharmony_ci const struct iio_info *info; 1548c2ecf20Sopenharmony_ci unsigned int *decimation; 1558c2ecf20Sopenharmony_ci unsigned int *hw_settle_1; 1568c2ecf20Sopenharmony_ci unsigned int *hw_settle_2; 1578c2ecf20Sopenharmony_ci}; 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ciint qcom_vadc_scale(enum vadc_scale_fn_type scaletype, 1608c2ecf20Sopenharmony_ci const struct vadc_linear_graph *calib_graph, 1618c2ecf20Sopenharmony_ci const struct vadc_prescale_ratio *prescale, 1628c2ecf20Sopenharmony_ci bool absolute, 1638c2ecf20Sopenharmony_ci u16 adc_code, int *result_mdec); 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_cistruct qcom_adc5_scale_type { 1668c2ecf20Sopenharmony_ci int (*scale_fn)(const struct vadc_prescale_ratio *prescale, 1678c2ecf20Sopenharmony_ci const struct adc5_data *data, u16 adc_code, int *result); 1688c2ecf20Sopenharmony_ci}; 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ciint qcom_adc5_hw_scale(enum vadc_scale_fn_type scaletype, 1718c2ecf20Sopenharmony_ci const struct vadc_prescale_ratio *prescale, 1728c2ecf20Sopenharmony_ci const struct adc5_data *data, 1738c2ecf20Sopenharmony_ci u16 adc_code, int *result_mdec); 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ciint qcom_vadc_decimation_from_dt(u32 value); 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci#endif /* QCOM_VADC_COMMON_H */ 178