]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/iio/adc/qcom-vadc-common.h
Merge tag 'dma-mapping-5.6' of git://git.infradead.org/users/hch/dma-mapping
[linux.git] / drivers / 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 #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 #define ADC5_DEF_VBAT_PRESCALING                1 /* 1:3 */
29 #define ADC5_DECIMATION_SHORT                   250
30 #define ADC5_DECIMATION_MEDIUM                  420
31 #define ADC5_DECIMATION_LONG                    840
32 /* Default decimation - 1024 for rev2, 840 for pmic5 */
33 #define ADC5_DECIMATION_DEFAULT                 2
34 #define ADC5_DECIMATION_SAMPLES_MAX             3
35
36 #define VADC_HW_SETTLE_DELAY_MAX                10000
37 #define VADC_HW_SETTLE_SAMPLES_MAX              16
38 #define VADC_AVG_SAMPLES_MAX                    512
39 #define ADC5_AVG_SAMPLES_MAX                    16
40
41 #define PMIC5_CHG_TEMP_SCALE_FACTOR             377500
42 #define PMIC5_SMB_TEMP_CONSTANT                 419400
43 #define PMIC5_SMB_TEMP_SCALE_FACTOR             356
44
45 #define PMI_CHG_SCALE_1                         -138890
46 #define PMI_CHG_SCALE_2                         391750000000LL
47
48 #define VADC5_MAX_CODE                          0x7fff
49 #define ADC5_FULL_SCALE_CODE                    0x70e4
50 #define ADC5_USR_DATA_CHECK                     0x8000
51
52 /**
53  * struct vadc_map_pt - Map the graph representation for ADC channel
54  * @x: Represent the ADC digitized code.
55  * @y: Represent the physical data which can be temperature, voltage,
56  *     resistance.
57  */
58 struct vadc_map_pt {
59         s32 x;
60         s32 y;
61 };
62
63 /*
64  * VADC_CALIB_ABSOLUTE: uses the 625mV and 1.25V as reference channels.
65  * VADC_CALIB_RATIOMETRIC: uses the reference voltage (1.8V) and GND for
66  * calibration.
67  */
68 enum vadc_calibration {
69         VADC_CALIB_ABSOLUTE = 0,
70         VADC_CALIB_RATIOMETRIC
71 };
72
73 /**
74  * struct vadc_linear_graph - Represent ADC characteristics.
75  * @dy: numerator slope to calculate the gain.
76  * @dx: denominator slope to calculate the gain.
77  * @gnd: A/D word of the ground reference used for the channel.
78  *
79  * Each ADC device has different offset and gain parameters which are
80  * computed to calibrate the device.
81  */
82 struct vadc_linear_graph {
83         s32 dy;
84         s32 dx;
85         s32 gnd;
86 };
87
88 /**
89  * struct vadc_prescale_ratio - Represent scaling ratio for ADC input.
90  * @num: the inverse numerator of the gain applied to the input channel.
91  * @den: the inverse denominator of the gain applied to the input channel.
92  */
93 struct vadc_prescale_ratio {
94         u32 num;
95         u32 den;
96 };
97
98 /**
99  * enum vadc_scale_fn_type - Scaling function to convert ADC code to
100  *                              physical scaled units for the channel.
101  * SCALE_DEFAULT: Default scaling to convert raw adc code to voltage (uV).
102  * SCALE_THERM_100K_PULLUP: Returns temperature in millidegC.
103  *                               Uses a mapping table with 100K pullup.
104  * SCALE_PMIC_THERM: Returns result in milli degree's Centigrade.
105  * SCALE_XOTHERM: Returns XO thermistor voltage in millidegC.
106  * SCALE_PMI_CHG_TEMP: Conversion for PMI CHG temp
107  * SCALE_HW_CALIB_DEFAULT: Default scaling to convert raw adc code to
108  *      voltage (uV) with hardware applied offset/slope values to adc code.
109  * SCALE_HW_CALIB_THERM_100K_PULLUP: Returns temperature in millidegC using
110  *      lookup table. The hardware applies offset/slope to adc code.
111  * SCALE_HW_CALIB_XOTHERM: Returns XO thermistor voltage in millidegC using
112  *      100k pullup. The hardware applies offset/slope to adc code.
113  * SCALE_HW_CALIB_PMIC_THERM: Returns result in milli degree's Centigrade.
114  *      The hardware applies offset/slope to adc code.
115  * SCALE_HW_CALIB_PM5_CHG_TEMP: Returns result in millidegrees for PMIC5
116  *      charger temperature.
117  * SCALE_HW_CALIB_PM5_SMB_TEMP: Returns result in millidegrees for PMIC5
118  *      SMB1390 temperature.
119  */
120 enum vadc_scale_fn_type {
121         SCALE_DEFAULT = 0,
122         SCALE_THERM_100K_PULLUP,
123         SCALE_PMIC_THERM,
124         SCALE_XOTHERM,
125         SCALE_PMI_CHG_TEMP,
126         SCALE_HW_CALIB_DEFAULT,
127         SCALE_HW_CALIB_THERM_100K_PULLUP,
128         SCALE_HW_CALIB_XOTHERM,
129         SCALE_HW_CALIB_PMIC_THERM,
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         unsigned int    *decimation;
140         unsigned int    *hw_settle_1;
141         unsigned int    *hw_settle_2;
142 };
143
144 int qcom_vadc_scale(enum vadc_scale_fn_type scaletype,
145                     const struct vadc_linear_graph *calib_graph,
146                     const struct vadc_prescale_ratio *prescale,
147                     bool absolute,
148                     u16 adc_code, int *result_mdec);
149
150 struct qcom_adc5_scale_type {
151         int (*scale_fn)(const struct vadc_prescale_ratio *prescale,
152                 const struct adc5_data *data, u16 adc_code, int *result);
153 };
154
155 int qcom_adc5_hw_scale(enum vadc_scale_fn_type scaletype,
156                     const struct vadc_prescale_ratio *prescale,
157                     const struct adc5_data *data,
158                     u16 adc_code, int *result_mdec);
159
160 int qcom_vadc_decimation_from_dt(u32 value);
161
162 #endif /* QCOM_VADC_COMMON_H */