Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Code shared between the different Qualcomm PMIC voltage ADCs
0004  */
0005 
0006 #ifndef QCOM_VADC_COMMON_H
0007 #define QCOM_VADC_COMMON_H
0008 
0009 #include <linux/math.h>
0010 #include <linux/types.h>
0011 
0012 #define VADC_CONV_TIME_MIN_US           2000
0013 #define VADC_CONV_TIME_MAX_US           2100
0014 
0015 /* Min ADC code represents 0V */
0016 #define VADC_MIN_ADC_CODE           0x6000
0017 /* Max ADC code represents full-scale range of 1.8V */
0018 #define VADC_MAX_ADC_CODE           0xa800
0019 
0020 #define VADC_ABSOLUTE_RANGE_UV          625000
0021 #define VADC_RATIOMETRIC_RANGE          1800
0022 
0023 #define VADC_DEF_PRESCALING         0 /* 1:1 */
0024 #define VADC_DEF_DECIMATION         0 /* 512 */
0025 #define VADC_DEF_HW_SETTLE_TIME         0 /* 0 us */
0026 #define VADC_DEF_AVG_SAMPLES            0 /* 1 sample */
0027 #define VADC_DEF_CALIB_TYPE         VADC_CALIB_ABSOLUTE
0028 
0029 #define VADC_DECIMATION_MIN         512
0030 #define VADC_DECIMATION_MAX         4096
0031 #define ADC5_DEF_VBAT_PRESCALING        1 /* 1:3 */
0032 #define ADC5_DECIMATION_SHORT           250
0033 #define ADC5_DECIMATION_MEDIUM          420
0034 #define ADC5_DECIMATION_LONG            840
0035 /* Default decimation - 1024 for rev2, 840 for pmic5 */
0036 #define ADC5_DECIMATION_DEFAULT         2
0037 #define ADC5_DECIMATION_SAMPLES_MAX     3
0038 
0039 #define VADC_HW_SETTLE_DELAY_MAX        10000
0040 #define VADC_HW_SETTLE_SAMPLES_MAX      16
0041 #define VADC_AVG_SAMPLES_MAX            512
0042 #define ADC5_AVG_SAMPLES_MAX            16
0043 
0044 #define PMIC5_CHG_TEMP_SCALE_FACTOR     377500
0045 #define PMIC5_SMB_TEMP_CONSTANT         419400
0046 #define PMIC5_SMB_TEMP_SCALE_FACTOR     356
0047 
0048 #define PMI_CHG_SCALE_1             -138890
0049 #define PMI_CHG_SCALE_2             391750000000LL
0050 
0051 #define VADC5_MAX_CODE              0x7fff
0052 #define ADC5_FULL_SCALE_CODE            0x70e4
0053 #define ADC5_USR_DATA_CHECK         0x8000
0054 
0055 #define R_PU_100K               100000
0056 #define RATIO_MAX_ADC7              BIT(14)
0057 
0058 /*
0059  * VADC_CALIB_ABSOLUTE: uses the 625mV and 1.25V as reference channels.
0060  * VADC_CALIB_RATIOMETRIC: uses the reference voltage (1.8V) and GND for
0061  * calibration.
0062  */
0063 enum vadc_calibration {
0064     VADC_CALIB_ABSOLUTE = 0,
0065     VADC_CALIB_RATIOMETRIC
0066 };
0067 
0068 /**
0069  * struct vadc_linear_graph - Represent ADC characteristics.
0070  * @dy: numerator slope to calculate the gain.
0071  * @dx: denominator slope to calculate the gain.
0072  * @gnd: A/D word of the ground reference used for the channel.
0073  *
0074  * Each ADC device has different offset and gain parameters which are
0075  * computed to calibrate the device.
0076  */
0077 struct vadc_linear_graph {
0078     s32 dy;
0079     s32 dx;
0080     s32 gnd;
0081 };
0082 
0083 /**
0084  * enum vadc_scale_fn_type - Scaling function to convert ADC code to
0085  *              physical scaled units for the channel.
0086  * SCALE_DEFAULT: Default scaling to convert raw adc code to voltage (uV).
0087  * SCALE_THERM_100K_PULLUP: Returns temperature in millidegC.
0088  *               Uses a mapping table with 100K pullup.
0089  * SCALE_PMIC_THERM: Returns result in milli degree's Centigrade.
0090  * SCALE_XOTHERM: Returns XO thermistor voltage in millidegC.
0091  * SCALE_PMI_CHG_TEMP: Conversion for PMI CHG temp
0092  * SCALE_HW_CALIB_DEFAULT: Default scaling to convert raw adc code to
0093  *  voltage (uV) with hardware applied offset/slope values to adc code.
0094  * SCALE_HW_CALIB_THERM_100K_PULLUP: Returns temperature in millidegC using
0095  *  lookup table. The hardware applies offset/slope to adc code.
0096  * SCALE_HW_CALIB_XOTHERM: Returns XO thermistor voltage in millidegC using
0097  *  100k pullup. The hardware applies offset/slope to adc code.
0098  * SCALE_HW_CALIB_THERM_100K_PU_PM7: Returns temperature in millidegC using
0099  *  lookup table for PMIC7. The hardware applies offset/slope to adc code.
0100  * SCALE_HW_CALIB_PMIC_THERM: Returns result in milli degree's Centigrade.
0101  *  The hardware applies offset/slope to adc code.
0102  * SCALE_HW_CALIB_PMIC_THERM: Returns result in milli degree's Centigrade.
0103  *  The hardware applies offset/slope to adc code. This is for PMIC7.
0104  * SCALE_HW_CALIB_PM5_CHG_TEMP: Returns result in millidegrees for PMIC5
0105  *  charger temperature.
0106  * SCALE_HW_CALIB_PM5_SMB_TEMP: Returns result in millidegrees for PMIC5
0107  *  SMB1390 temperature.
0108  */
0109 enum vadc_scale_fn_type {
0110     SCALE_DEFAULT = 0,
0111     SCALE_THERM_100K_PULLUP,
0112     SCALE_PMIC_THERM,
0113     SCALE_XOTHERM,
0114     SCALE_PMI_CHG_TEMP,
0115     SCALE_HW_CALIB_DEFAULT,
0116     SCALE_HW_CALIB_THERM_100K_PULLUP,
0117     SCALE_HW_CALIB_XOTHERM,
0118     SCALE_HW_CALIB_THERM_100K_PU_PM7,
0119     SCALE_HW_CALIB_PMIC_THERM,
0120     SCALE_HW_CALIB_PMIC_THERM_PM7,
0121     SCALE_HW_CALIB_PM5_CHG_TEMP,
0122     SCALE_HW_CALIB_PM5_SMB_TEMP,
0123     SCALE_HW_CALIB_INVALID,
0124 };
0125 
0126 struct adc5_data {
0127     const u32   full_scale_code_volt;
0128     const u32   full_scale_code_cur;
0129     const struct adc5_channels *adc_chans;
0130     const struct iio_info *info;
0131     unsigned int    *decimation;
0132     unsigned int    *hw_settle_1;
0133     unsigned int    *hw_settle_2;
0134 };
0135 
0136 int qcom_vadc_scale(enum vadc_scale_fn_type scaletype,
0137             const struct vadc_linear_graph *calib_graph,
0138             const struct u32_fract *prescale,
0139             bool absolute,
0140             u16 adc_code, int *result_mdec);
0141 
0142 struct qcom_adc5_scale_type {
0143     int (*scale_fn)(const struct u32_fract *prescale,
0144         const struct adc5_data *data, u16 adc_code, int *result);
0145 };
0146 
0147 int qcom_adc5_hw_scale(enum vadc_scale_fn_type scaletype,
0148             unsigned int prescale_ratio,
0149             const struct adc5_data *data,
0150             u16 adc_code, int *result_mdec);
0151 
0152 u16 qcom_adc_tm5_temp_volt_scale(unsigned int prescale_ratio,
0153                  u32 full_scale_code_volt, int temp);
0154 
0155 u16 qcom_adc_tm5_gen2_temp_res_scale(int temp);
0156 
0157 int qcom_adc5_prescaling_from_dt(u32 num, u32 den);
0158 
0159 int qcom_adc5_hw_settle_time_from_dt(u32 value, const unsigned int *hw_settle);
0160 
0161 int qcom_adc5_avg_samples_from_dt(u32 value);
0162 
0163 int qcom_adc5_decimation_from_dt(u32 value, const unsigned int *decimation);
0164 
0165 int qcom_vadc_decimation_from_dt(u32 value);
0166 
0167 #endif /* QCOM_VADC_COMMON_H */