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