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