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 #define VADC_CONV_TIME_MIN_US			2000
10 #define VADC_CONV_TIME_MAX_US			2100
11 
12 /* Min ADC code represents 0V */
13 #define VADC_MIN_ADC_CODE			0x6000
14 /* Max ADC code represents full-scale range of 1.8V */
15 #define VADC_MAX_ADC_CODE			0xa800
16 
17 #define VADC_ABSOLUTE_RANGE_UV			625000
18 #define VADC_RATIOMETRIC_RANGE			1800
19 
20 #define VADC_DEF_PRESCALING			0 /* 1:1 */
21 #define VADC_DEF_DECIMATION			0 /* 512 */
22 #define VADC_DEF_HW_SETTLE_TIME			0 /* 0 us */
23 #define VADC_DEF_AVG_SAMPLES			0 /* 1 sample */
24 #define VADC_DEF_CALIB_TYPE			VADC_CALIB_ABSOLUTE
25 
26 #define VADC_DECIMATION_MIN			512
27 #define VADC_DECIMATION_MAX			4096
28 
29 #define VADC_HW_SETTLE_DELAY_MAX		10000
30 #define VADC_AVG_SAMPLES_MAX			512
31 
32 #define KELVINMIL_CELSIUSMIL			273150
33 
34 #define PMI_CHG_SCALE_1				-138890
35 #define PMI_CHG_SCALE_2				391750000000LL
36 
37 /**
38  * struct vadc_map_pt - Map the graph representation for ADC channel
39  * @x: Represent the ADC digitized code.
40  * @y: Represent the physical data which can be temperature, voltage,
41  *     resistance.
42  */
43 struct vadc_map_pt {
44 	s32 x;
45 	s32 y;
46 };
47 
48 /*
49  * VADC_CALIB_ABSOLUTE: uses the 625mV and 1.25V as reference channels.
50  * VADC_CALIB_RATIOMETRIC: uses the reference voltage (1.8V) and GND for
51  * calibration.
52  */
53 enum vadc_calibration {
54 	VADC_CALIB_ABSOLUTE = 0,
55 	VADC_CALIB_RATIOMETRIC
56 };
57 
58 /**
59  * struct vadc_linear_graph - Represent ADC characteristics.
60  * @dy: numerator slope to calculate the gain.
61  * @dx: denominator slope to calculate the gain.
62  * @gnd: A/D word of the ground reference used for the channel.
63  *
64  * Each ADC device has different offset and gain parameters which are
65  * computed to calibrate the device.
66  */
67 struct vadc_linear_graph {
68 	s32 dy;
69 	s32 dx;
70 	s32 gnd;
71 };
72 
73 /**
74  * struct vadc_prescale_ratio - Represent scaling ratio for ADC input.
75  * @num: the inverse numerator of the gain applied to the input channel.
76  * @den: the inverse denominator of the gain applied to the input channel.
77  */
78 struct vadc_prescale_ratio {
79 	u32 num;
80 	u32 den;
81 };
82 
83 /**
84  * enum vadc_scale_fn_type - Scaling function to convert ADC code to
85  *				physical scaled units for the channel.
86  * SCALE_DEFAULT: Default scaling to convert raw adc code to voltage (uV).
87  * SCALE_THERM_100K_PULLUP: Returns temperature in millidegC.
88  *				 Uses a mapping table with 100K pullup.
89  * SCALE_PMIC_THERM: Returns result in milli degree's Centigrade.
90  * SCALE_XOTHERM: Returns XO thermistor voltage in millidegC.
91  * SCALE_PMI_CHG_TEMP: Conversion for PMI CHG temp
92  */
93 enum vadc_scale_fn_type {
94 	SCALE_DEFAULT = 0,
95 	SCALE_THERM_100K_PULLUP,
96 	SCALE_PMIC_THERM,
97 	SCALE_XOTHERM,
98 	SCALE_PMI_CHG_TEMP,
99 };
100 
101 int qcom_vadc_scale(enum vadc_scale_fn_type scaletype,
102 		    const struct vadc_linear_graph *calib_graph,
103 		    const struct vadc_prescale_ratio *prescale,
104 		    bool absolute,
105 		    u16 adc_code, int *result_mdec);
106 
107 int qcom_vadc_decimation_from_dt(u32 value);
108 
109 #endif /* QCOM_VADC_COMMON_H */
110