Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Walleij | e932d4f | 2017-04-04 14:08:18 +0200 | [diff] [blame] | 2 | #include <linux/bug.h> |
| 3 | #include <linux/kernel.h> |
| 4 | #include <linux/bitops.h> |
Dmitry Baryshkov | e2621ac | 2020-12-04 05:54:58 +0300 | [diff] [blame^] | 5 | #include <linux/fixp-arith.h> |
Linus Walleij | e932d4f | 2017-04-04 14:08:18 +0200 | [diff] [blame] | 6 | #include <linux/math64.h> |
| 7 | #include <linux/log2.h> |
| 8 | #include <linux/err.h> |
Linus Walleij | 9a0ebbc | 2017-11-13 11:29:55 +0100 | [diff] [blame] | 9 | #include <linux/module.h> |
Akinobu Mita | f88b426 | 2020-01-30 22:16:13 -0800 | [diff] [blame] | 10 | #include <linux/units.h> |
Linus Walleij | e932d4f | 2017-04-04 14:08:18 +0200 | [diff] [blame] | 11 | |
| 12 | #include "qcom-vadc-common.h" |
| 13 | |
| 14 | /* Voltage to temperature */ |
| 15 | static const struct vadc_map_pt adcmap_100k_104ef_104fb[] = { |
| 16 | {1758, -40}, |
| 17 | {1742, -35}, |
| 18 | {1719, -30}, |
| 19 | {1691, -25}, |
| 20 | {1654, -20}, |
| 21 | {1608, -15}, |
| 22 | {1551, -10}, |
| 23 | {1483, -5}, |
| 24 | {1404, 0}, |
| 25 | {1315, 5}, |
| 26 | {1218, 10}, |
| 27 | {1114, 15}, |
| 28 | {1007, 20}, |
| 29 | {900, 25}, |
| 30 | {795, 30}, |
| 31 | {696, 35}, |
| 32 | {605, 40}, |
| 33 | {522, 45}, |
| 34 | {448, 50}, |
| 35 | {383, 55}, |
| 36 | {327, 60}, |
| 37 | {278, 65}, |
| 38 | {237, 70}, |
| 39 | {202, 75}, |
| 40 | {172, 80}, |
| 41 | {146, 85}, |
| 42 | {125, 90}, |
| 43 | {107, 95}, |
| 44 | {92, 100}, |
| 45 | {79, 105}, |
| 46 | {68, 110}, |
| 47 | {59, 115}, |
| 48 | {51, 120}, |
| 49 | {44, 125} |
| 50 | }; |
| 51 | |
Siddartha Mohanadoss | e13d757 | 2018-08-02 18:43:53 -0700 | [diff] [blame] | 52 | /* |
| 53 | * Voltage to temperature table for 100k pull up for NTCG104EF104 with |
| 54 | * 1.875V reference. |
| 55 | */ |
| 56 | static const struct vadc_map_pt adcmap_100k_104ef_104fb_1875_vref[] = { |
| 57 | { 1831, -40000 }, |
| 58 | { 1814, -35000 }, |
| 59 | { 1791, -30000 }, |
| 60 | { 1761, -25000 }, |
| 61 | { 1723, -20000 }, |
| 62 | { 1675, -15000 }, |
| 63 | { 1616, -10000 }, |
| 64 | { 1545, -5000 }, |
| 65 | { 1463, 0 }, |
| 66 | { 1370, 5000 }, |
| 67 | { 1268, 10000 }, |
| 68 | { 1160, 15000 }, |
| 69 | { 1049, 20000 }, |
| 70 | { 937, 25000 }, |
| 71 | { 828, 30000 }, |
| 72 | { 726, 35000 }, |
| 73 | { 630, 40000 }, |
| 74 | { 544, 45000 }, |
| 75 | { 467, 50000 }, |
| 76 | { 399, 55000 }, |
| 77 | { 340, 60000 }, |
| 78 | { 290, 65000 }, |
| 79 | { 247, 70000 }, |
| 80 | { 209, 75000 }, |
| 81 | { 179, 80000 }, |
| 82 | { 153, 85000 }, |
| 83 | { 130, 90000 }, |
| 84 | { 112, 95000 }, |
| 85 | { 96, 100000 }, |
| 86 | { 82, 105000 }, |
| 87 | { 71, 110000 }, |
| 88 | { 62, 115000 }, |
| 89 | { 53, 120000 }, |
| 90 | { 46, 125000 }, |
| 91 | }; |
| 92 | |
Jishnu Prakash | 082111e | 2020-05-28 22:24:26 +0530 | [diff] [blame] | 93 | static const struct vadc_map_pt adcmap7_die_temp[] = { |
| 94 | { 433700, 1967}, |
| 95 | { 473100, 1964}, |
| 96 | { 512400, 1957}, |
| 97 | { 551500, 1949}, |
| 98 | { 590500, 1940}, |
| 99 | { 629300, 1930}, |
| 100 | { 667900, 1921}, |
| 101 | { 706400, 1910}, |
| 102 | { 744600, 1896}, |
| 103 | { 782500, 1878}, |
| 104 | { 820100, 1859}, |
| 105 | { 857300, 0}, |
| 106 | }; |
| 107 | |
| 108 | /* |
| 109 | * Resistance to temperature table for 100k pull up for NTCG104EF104. |
| 110 | */ |
| 111 | static const struct vadc_map_pt adcmap7_100k[] = { |
| 112 | { 4250657, -40960 }, |
| 113 | { 3962085, -39936 }, |
| 114 | { 3694875, -38912 }, |
| 115 | { 3447322, -37888 }, |
| 116 | { 3217867, -36864 }, |
| 117 | { 3005082, -35840 }, |
| 118 | { 2807660, -34816 }, |
| 119 | { 2624405, -33792 }, |
| 120 | { 2454218, -32768 }, |
| 121 | { 2296094, -31744 }, |
| 122 | { 2149108, -30720 }, |
| 123 | { 2012414, -29696 }, |
| 124 | { 1885232, -28672 }, |
| 125 | { 1766846, -27648 }, |
| 126 | { 1656598, -26624 }, |
| 127 | { 1553884, -25600 }, |
| 128 | { 1458147, -24576 }, |
| 129 | { 1368873, -23552 }, |
| 130 | { 1285590, -22528 }, |
| 131 | { 1207863, -21504 }, |
| 132 | { 1135290, -20480 }, |
| 133 | { 1067501, -19456 }, |
| 134 | { 1004155, -18432 }, |
| 135 | { 944935, -17408 }, |
| 136 | { 889550, -16384 }, |
| 137 | { 837731, -15360 }, |
| 138 | { 789229, -14336 }, |
| 139 | { 743813, -13312 }, |
| 140 | { 701271, -12288 }, |
| 141 | { 661405, -11264 }, |
| 142 | { 624032, -10240 }, |
| 143 | { 588982, -9216 }, |
| 144 | { 556100, -8192 }, |
| 145 | { 525239, -7168 }, |
| 146 | { 496264, -6144 }, |
| 147 | { 469050, -5120 }, |
| 148 | { 443480, -4096 }, |
| 149 | { 419448, -3072 }, |
| 150 | { 396851, -2048 }, |
| 151 | { 375597, -1024 }, |
| 152 | { 355598, 0 }, |
| 153 | { 336775, 1024 }, |
| 154 | { 319052, 2048 }, |
| 155 | { 302359, 3072 }, |
| 156 | { 286630, 4096 }, |
| 157 | { 271806, 5120 }, |
| 158 | { 257829, 6144 }, |
| 159 | { 244646, 7168 }, |
| 160 | { 232209, 8192 }, |
| 161 | { 220471, 9216 }, |
| 162 | { 209390, 10240 }, |
| 163 | { 198926, 11264 }, |
| 164 | { 189040, 12288 }, |
| 165 | { 179698, 13312 }, |
| 166 | { 170868, 14336 }, |
| 167 | { 162519, 15360 }, |
| 168 | { 154622, 16384 }, |
| 169 | { 147150, 17408 }, |
| 170 | { 140079, 18432 }, |
| 171 | { 133385, 19456 }, |
| 172 | { 127046, 20480 }, |
| 173 | { 121042, 21504 }, |
| 174 | { 115352, 22528 }, |
| 175 | { 109960, 23552 }, |
| 176 | { 104848, 24576 }, |
| 177 | { 100000, 25600 }, |
| 178 | { 95402, 26624 }, |
| 179 | { 91038, 27648 }, |
| 180 | { 86897, 28672 }, |
| 181 | { 82965, 29696 }, |
| 182 | { 79232, 30720 }, |
| 183 | { 75686, 31744 }, |
| 184 | { 72316, 32768 }, |
| 185 | { 69114, 33792 }, |
| 186 | { 66070, 34816 }, |
| 187 | { 63176, 35840 }, |
| 188 | { 60423, 36864 }, |
| 189 | { 57804, 37888 }, |
| 190 | { 55312, 38912 }, |
| 191 | { 52940, 39936 }, |
| 192 | { 50681, 40960 }, |
| 193 | { 48531, 41984 }, |
| 194 | { 46482, 43008 }, |
| 195 | { 44530, 44032 }, |
| 196 | { 42670, 45056 }, |
| 197 | { 40897, 46080 }, |
| 198 | { 39207, 47104 }, |
| 199 | { 37595, 48128 }, |
| 200 | { 36057, 49152 }, |
| 201 | { 34590, 50176 }, |
| 202 | { 33190, 51200 }, |
| 203 | { 31853, 52224 }, |
| 204 | { 30577, 53248 }, |
| 205 | { 29358, 54272 }, |
| 206 | { 28194, 55296 }, |
| 207 | { 27082, 56320 }, |
| 208 | { 26020, 57344 }, |
| 209 | { 25004, 58368 }, |
| 210 | { 24033, 59392 }, |
| 211 | { 23104, 60416 }, |
| 212 | { 22216, 61440 }, |
| 213 | { 21367, 62464 }, |
| 214 | { 20554, 63488 }, |
| 215 | { 19776, 64512 }, |
| 216 | { 19031, 65536 }, |
| 217 | { 18318, 66560 }, |
| 218 | { 17636, 67584 }, |
| 219 | { 16982, 68608 }, |
| 220 | { 16355, 69632 }, |
| 221 | { 15755, 70656 }, |
| 222 | { 15180, 71680 }, |
| 223 | { 14628, 72704 }, |
| 224 | { 14099, 73728 }, |
| 225 | { 13592, 74752 }, |
| 226 | { 13106, 75776 }, |
| 227 | { 12640, 76800 }, |
| 228 | { 12192, 77824 }, |
| 229 | { 11762, 78848 }, |
| 230 | { 11350, 79872 }, |
| 231 | { 10954, 80896 }, |
| 232 | { 10574, 81920 }, |
| 233 | { 10209, 82944 }, |
| 234 | { 9858, 83968 }, |
| 235 | { 9521, 84992 }, |
| 236 | { 9197, 86016 }, |
| 237 | { 8886, 87040 }, |
| 238 | { 8587, 88064 }, |
| 239 | { 8299, 89088 }, |
| 240 | { 8023, 90112 }, |
| 241 | { 7757, 91136 }, |
| 242 | { 7501, 92160 }, |
| 243 | { 7254, 93184 }, |
| 244 | { 7017, 94208 }, |
| 245 | { 6789, 95232 }, |
| 246 | { 6570, 96256 }, |
| 247 | { 6358, 97280 }, |
| 248 | { 6155, 98304 }, |
| 249 | { 5959, 99328 }, |
| 250 | { 5770, 100352 }, |
| 251 | { 5588, 101376 }, |
| 252 | { 5412, 102400 }, |
| 253 | { 5243, 103424 }, |
| 254 | { 5080, 104448 }, |
| 255 | { 4923, 105472 }, |
| 256 | { 4771, 106496 }, |
| 257 | { 4625, 107520 }, |
| 258 | { 4484, 108544 }, |
| 259 | { 4348, 109568 }, |
| 260 | { 4217, 110592 }, |
| 261 | { 4090, 111616 }, |
| 262 | { 3968, 112640 }, |
| 263 | { 3850, 113664 }, |
| 264 | { 3736, 114688 }, |
| 265 | { 3626, 115712 }, |
| 266 | { 3519, 116736 }, |
| 267 | { 3417, 117760 }, |
| 268 | { 3317, 118784 }, |
| 269 | { 3221, 119808 }, |
| 270 | { 3129, 120832 }, |
| 271 | { 3039, 121856 }, |
| 272 | { 2952, 122880 }, |
| 273 | { 2868, 123904 }, |
| 274 | { 2787, 124928 }, |
| 275 | { 2709, 125952 }, |
| 276 | { 2633, 126976 }, |
| 277 | { 2560, 128000 }, |
| 278 | { 2489, 129024 }, |
| 279 | { 2420, 130048 } |
| 280 | }; |
| 281 | |
Dmitry Baryshkov | c7ba98f | 2020-12-04 05:54:57 +0300 | [diff] [blame] | 282 | static const struct vadc_prescale_ratio adc5_prescale_ratios[] = { |
| 283 | {.num = 1, .den = 1}, |
| 284 | {.num = 1, .den = 3}, |
| 285 | {.num = 1, .den = 4}, |
| 286 | {.num = 1, .den = 6}, |
| 287 | {.num = 1, .den = 20}, |
| 288 | {.num = 1, .den = 8}, |
| 289 | {.num = 10, .den = 81}, |
| 290 | {.num = 1, .den = 10}, |
| 291 | {.num = 1, .den = 16} |
| 292 | }; |
| 293 | |
Siddartha Mohanadoss | e13d757 | 2018-08-02 18:43:53 -0700 | [diff] [blame] | 294 | static int qcom_vadc_scale_hw_calib_volt( |
| 295 | const struct vadc_prescale_ratio *prescale, |
| 296 | const struct adc5_data *data, |
| 297 | u16 adc_code, int *result_uv); |
| 298 | static int qcom_vadc_scale_hw_calib_therm( |
| 299 | const struct vadc_prescale_ratio *prescale, |
| 300 | const struct adc5_data *data, |
| 301 | u16 adc_code, int *result_mdec); |
Jishnu Prakash | 082111e | 2020-05-28 22:24:26 +0530 | [diff] [blame] | 302 | static int qcom_vadc7_scale_hw_calib_therm( |
| 303 | const struct vadc_prescale_ratio *prescale, |
| 304 | const struct adc5_data *data, |
| 305 | u16 adc_code, int *result_mdec); |
Siddartha Mohanadoss | e13d757 | 2018-08-02 18:43:53 -0700 | [diff] [blame] | 306 | static int qcom_vadc_scale_hw_smb_temp( |
| 307 | const struct vadc_prescale_ratio *prescale, |
| 308 | const struct adc5_data *data, |
| 309 | u16 adc_code, int *result_mdec); |
| 310 | static int qcom_vadc_scale_hw_chg5_temp( |
| 311 | const struct vadc_prescale_ratio *prescale, |
| 312 | const struct adc5_data *data, |
| 313 | u16 adc_code, int *result_mdec); |
| 314 | static int qcom_vadc_scale_hw_calib_die_temp( |
| 315 | const struct vadc_prescale_ratio *prescale, |
| 316 | const struct adc5_data *data, |
| 317 | u16 adc_code, int *result_mdec); |
Jishnu Prakash | 082111e | 2020-05-28 22:24:26 +0530 | [diff] [blame] | 318 | static int qcom_vadc7_scale_hw_calib_die_temp( |
| 319 | const struct vadc_prescale_ratio *prescale, |
| 320 | const struct adc5_data *data, |
| 321 | u16 adc_code, int *result_mdec); |
Siddartha Mohanadoss | e13d757 | 2018-08-02 18:43:53 -0700 | [diff] [blame] | 322 | |
| 323 | static struct qcom_adc5_scale_type scale_adc5_fn[] = { |
| 324 | [SCALE_HW_CALIB_DEFAULT] = {qcom_vadc_scale_hw_calib_volt}, |
| 325 | [SCALE_HW_CALIB_THERM_100K_PULLUP] = {qcom_vadc_scale_hw_calib_therm}, |
| 326 | [SCALE_HW_CALIB_XOTHERM] = {qcom_vadc_scale_hw_calib_therm}, |
Jishnu Prakash | 082111e | 2020-05-28 22:24:26 +0530 | [diff] [blame] | 327 | [SCALE_HW_CALIB_THERM_100K_PU_PM7] = { |
| 328 | qcom_vadc7_scale_hw_calib_therm}, |
Siddartha Mohanadoss | e13d757 | 2018-08-02 18:43:53 -0700 | [diff] [blame] | 329 | [SCALE_HW_CALIB_PMIC_THERM] = {qcom_vadc_scale_hw_calib_die_temp}, |
Jishnu Prakash | 082111e | 2020-05-28 22:24:26 +0530 | [diff] [blame] | 330 | [SCALE_HW_CALIB_PMIC_THERM_PM7] = { |
| 331 | qcom_vadc7_scale_hw_calib_die_temp}, |
Siddartha Mohanadoss | e13d757 | 2018-08-02 18:43:53 -0700 | [diff] [blame] | 332 | [SCALE_HW_CALIB_PM5_CHG_TEMP] = {qcom_vadc_scale_hw_chg5_temp}, |
| 333 | [SCALE_HW_CALIB_PM5_SMB_TEMP] = {qcom_vadc_scale_hw_smb_temp}, |
| 334 | }; |
| 335 | |
Linus Walleij | e932d4f | 2017-04-04 14:08:18 +0200 | [diff] [blame] | 336 | static int qcom_vadc_map_voltage_temp(const struct vadc_map_pt *pts, |
Siddartha Mohanadoss | e13d757 | 2018-08-02 18:43:53 -0700 | [diff] [blame] | 337 | u32 tablesize, s32 input, int *output) |
Linus Walleij | e932d4f | 2017-04-04 14:08:18 +0200 | [diff] [blame] | 338 | { |
| 339 | bool descending = 1; |
| 340 | u32 i = 0; |
| 341 | |
| 342 | if (!pts) |
| 343 | return -EINVAL; |
| 344 | |
| 345 | /* Check if table is descending or ascending */ |
| 346 | if (tablesize > 1) { |
| 347 | if (pts[0].x < pts[1].x) |
| 348 | descending = 0; |
| 349 | } |
| 350 | |
| 351 | while (i < tablesize) { |
| 352 | if ((descending) && (pts[i].x < input)) { |
| 353 | /* table entry is less than measured*/ |
| 354 | /* value and table is descending, stop */ |
| 355 | break; |
| 356 | } else if ((!descending) && |
| 357 | (pts[i].x > input)) { |
| 358 | /* table entry is greater than measured*/ |
| 359 | /*value and table is ascending, stop */ |
| 360 | break; |
| 361 | } |
| 362 | i++; |
| 363 | } |
| 364 | |
| 365 | if (i == 0) { |
| 366 | *output = pts[0].y; |
| 367 | } else if (i == tablesize) { |
| 368 | *output = pts[tablesize - 1].y; |
| 369 | } else { |
| 370 | /* result is between search_index and search_index-1 */ |
| 371 | /* interpolate linearly */ |
Dmitry Baryshkov | e2621ac | 2020-12-04 05:54:58 +0300 | [diff] [blame^] | 372 | *output = fixp_linear_interpolate(pts[i - 1].x, pts[i - 1].y, |
| 373 | pts[i].x, pts[i].y, |
| 374 | input); |
Linus Walleij | e932d4f | 2017-04-04 14:08:18 +0200 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | static void qcom_vadc_scale_calib(const struct vadc_linear_graph *calib_graph, |
| 381 | u16 adc_code, |
| 382 | bool absolute, |
| 383 | s64 *scale_voltage) |
| 384 | { |
| 385 | *scale_voltage = (adc_code - calib_graph->gnd); |
| 386 | *scale_voltage *= calib_graph->dx; |
| 387 | *scale_voltage = div64_s64(*scale_voltage, calib_graph->dy); |
| 388 | if (absolute) |
| 389 | *scale_voltage += calib_graph->dx; |
| 390 | |
| 391 | if (*scale_voltage < 0) |
| 392 | *scale_voltage = 0; |
| 393 | } |
| 394 | |
| 395 | static int qcom_vadc_scale_volt(const struct vadc_linear_graph *calib_graph, |
| 396 | const struct vadc_prescale_ratio *prescale, |
| 397 | bool absolute, u16 adc_code, |
| 398 | int *result_uv) |
| 399 | { |
| 400 | s64 voltage = 0, result = 0; |
| 401 | |
| 402 | qcom_vadc_scale_calib(calib_graph, adc_code, absolute, &voltage); |
| 403 | |
| 404 | voltage = voltage * prescale->den; |
| 405 | result = div64_s64(voltage, prescale->num); |
| 406 | *result_uv = result; |
| 407 | |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | static int qcom_vadc_scale_therm(const struct vadc_linear_graph *calib_graph, |
| 412 | const struct vadc_prescale_ratio *prescale, |
| 413 | bool absolute, u16 adc_code, |
| 414 | int *result_mdec) |
| 415 | { |
Siddartha Mohanadoss | e13d757 | 2018-08-02 18:43:53 -0700 | [diff] [blame] | 416 | s64 voltage = 0; |
Linus Walleij | e932d4f | 2017-04-04 14:08:18 +0200 | [diff] [blame] | 417 | int ret; |
| 418 | |
| 419 | qcom_vadc_scale_calib(calib_graph, adc_code, absolute, &voltage); |
| 420 | |
| 421 | if (absolute) |
| 422 | voltage = div64_s64(voltage, 1000); |
| 423 | |
| 424 | ret = qcom_vadc_map_voltage_temp(adcmap_100k_104ef_104fb, |
| 425 | ARRAY_SIZE(adcmap_100k_104ef_104fb), |
Siddartha Mohanadoss | e13d757 | 2018-08-02 18:43:53 -0700 | [diff] [blame] | 426 | voltage, result_mdec); |
Linus Walleij | e932d4f | 2017-04-04 14:08:18 +0200 | [diff] [blame] | 427 | if (ret) |
| 428 | return ret; |
| 429 | |
Siddartha Mohanadoss | e13d757 | 2018-08-02 18:43:53 -0700 | [diff] [blame] | 430 | *result_mdec *= 1000; |
Linus Walleij | e932d4f | 2017-04-04 14:08:18 +0200 | [diff] [blame] | 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | static int qcom_vadc_scale_die_temp(const struct vadc_linear_graph *calib_graph, |
| 436 | const struct vadc_prescale_ratio *prescale, |
| 437 | bool absolute, |
| 438 | u16 adc_code, int *result_mdec) |
| 439 | { |
| 440 | s64 voltage = 0; |
| 441 | u64 temp; /* Temporary variable for do_div */ |
| 442 | |
| 443 | qcom_vadc_scale_calib(calib_graph, adc_code, absolute, &voltage); |
| 444 | |
| 445 | if (voltage > 0) { |
| 446 | temp = voltage * prescale->den; |
| 447 | do_div(temp, prescale->num * 2); |
| 448 | voltage = temp; |
| 449 | } else { |
| 450 | voltage = 0; |
| 451 | } |
| 452 | |
Akinobu Mita | f88b426 | 2020-01-30 22:16:13 -0800 | [diff] [blame] | 453 | *result_mdec = milli_kelvin_to_millicelsius(voltage); |
Linus Walleij | e932d4f | 2017-04-04 14:08:18 +0200 | [diff] [blame] | 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | static int qcom_vadc_scale_chg_temp(const struct vadc_linear_graph *calib_graph, |
| 459 | const struct vadc_prescale_ratio *prescale, |
| 460 | bool absolute, |
| 461 | u16 adc_code, int *result_mdec) |
| 462 | { |
| 463 | s64 voltage = 0, result = 0; |
| 464 | |
| 465 | qcom_vadc_scale_calib(calib_graph, adc_code, absolute, &voltage); |
| 466 | |
| 467 | voltage = voltage * prescale->den; |
| 468 | voltage = div64_s64(voltage, prescale->num); |
| 469 | voltage = ((PMI_CHG_SCALE_1) * (voltage * 2)); |
| 470 | voltage = (voltage + PMI_CHG_SCALE_2); |
| 471 | result = div64_s64(voltage, 1000000); |
| 472 | *result_mdec = result; |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
Siddartha Mohanadoss | e13d757 | 2018-08-02 18:43:53 -0700 | [diff] [blame] | 477 | static int qcom_vadc_scale_code_voltage_factor(u16 adc_code, |
| 478 | const struct vadc_prescale_ratio *prescale, |
| 479 | const struct adc5_data *data, |
| 480 | unsigned int factor) |
| 481 | { |
| 482 | s64 voltage, temp, adc_vdd_ref_mv = 1875; |
| 483 | |
| 484 | /* |
| 485 | * The normal data range is between 0V to 1.875V. On cases where |
| 486 | * we read low voltage values, the ADC code can go beyond the |
| 487 | * range and the scale result is incorrect so we clamp the values |
| 488 | * for the cases where the code represents a value below 0V |
| 489 | */ |
| 490 | if (adc_code > VADC5_MAX_CODE) |
| 491 | adc_code = 0; |
| 492 | |
| 493 | /* (ADC code * vref_vadc (1.875V)) / full_scale_code */ |
| 494 | voltage = (s64) adc_code * adc_vdd_ref_mv * 1000; |
| 495 | voltage = div64_s64(voltage, data->full_scale_code_volt); |
| 496 | if (voltage > 0) { |
| 497 | voltage *= prescale->den; |
| 498 | temp = prescale->num * factor; |
| 499 | voltage = div64_s64(voltage, temp); |
| 500 | } else { |
| 501 | voltage = 0; |
| 502 | } |
| 503 | |
| 504 | return (int) voltage; |
| 505 | } |
| 506 | |
Jishnu Prakash | 082111e | 2020-05-28 22:24:26 +0530 | [diff] [blame] | 507 | static int qcom_vadc7_scale_hw_calib_therm( |
| 508 | const struct vadc_prescale_ratio *prescale, |
| 509 | const struct adc5_data *data, |
| 510 | u16 adc_code, int *result_mdec) |
| 511 | { |
| 512 | s64 resistance = adc_code; |
| 513 | int ret, result; |
| 514 | |
| 515 | if (adc_code >= RATIO_MAX_ADC7) |
| 516 | return -EINVAL; |
| 517 | |
| 518 | /* (ADC code * R_PULLUP (100Kohm)) / (full_scale_code - ADC code)*/ |
| 519 | resistance *= R_PU_100K; |
| 520 | resistance = div64_s64(resistance, RATIO_MAX_ADC7 - adc_code); |
| 521 | |
| 522 | ret = qcom_vadc_map_voltage_temp(adcmap7_100k, |
| 523 | ARRAY_SIZE(adcmap7_100k), |
| 524 | resistance, &result); |
| 525 | if (ret) |
| 526 | return ret; |
| 527 | |
| 528 | *result_mdec = result; |
| 529 | |
| 530 | return 0; |
| 531 | } |
| 532 | |
Siddartha Mohanadoss | e13d757 | 2018-08-02 18:43:53 -0700 | [diff] [blame] | 533 | static int qcom_vadc_scale_hw_calib_volt( |
| 534 | const struct vadc_prescale_ratio *prescale, |
| 535 | const struct adc5_data *data, |
| 536 | u16 adc_code, int *result_uv) |
| 537 | { |
| 538 | *result_uv = qcom_vadc_scale_code_voltage_factor(adc_code, |
| 539 | prescale, data, 1); |
| 540 | |
| 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | static int qcom_vadc_scale_hw_calib_therm( |
| 545 | const struct vadc_prescale_ratio *prescale, |
| 546 | const struct adc5_data *data, |
| 547 | u16 adc_code, int *result_mdec) |
| 548 | { |
| 549 | int voltage; |
| 550 | |
| 551 | voltage = qcom_vadc_scale_code_voltage_factor(adc_code, |
| 552 | prescale, data, 1000); |
| 553 | |
| 554 | /* Map voltage to temperature from look-up table */ |
| 555 | return qcom_vadc_map_voltage_temp(adcmap_100k_104ef_104fb_1875_vref, |
| 556 | ARRAY_SIZE(adcmap_100k_104ef_104fb_1875_vref), |
| 557 | voltage, result_mdec); |
| 558 | } |
| 559 | |
| 560 | static int qcom_vadc_scale_hw_calib_die_temp( |
| 561 | const struct vadc_prescale_ratio *prescale, |
| 562 | const struct adc5_data *data, |
| 563 | u16 adc_code, int *result_mdec) |
| 564 | { |
| 565 | *result_mdec = qcom_vadc_scale_code_voltage_factor(adc_code, |
| 566 | prescale, data, 2); |
Akinobu Mita | f88b426 | 2020-01-30 22:16:13 -0800 | [diff] [blame] | 567 | *result_mdec = milli_kelvin_to_millicelsius(*result_mdec); |
Siddartha Mohanadoss | e13d757 | 2018-08-02 18:43:53 -0700 | [diff] [blame] | 568 | |
| 569 | return 0; |
| 570 | } |
| 571 | |
Jishnu Prakash | 082111e | 2020-05-28 22:24:26 +0530 | [diff] [blame] | 572 | static int qcom_vadc7_scale_hw_calib_die_temp( |
| 573 | const struct vadc_prescale_ratio *prescale, |
| 574 | const struct adc5_data *data, |
| 575 | u16 adc_code, int *result_mdec) |
| 576 | { |
| 577 | |
| 578 | int voltage, vtemp0, temp, i; |
| 579 | |
| 580 | voltage = qcom_vadc_scale_code_voltage_factor(adc_code, |
| 581 | prescale, data, 1); |
| 582 | |
| 583 | if (adcmap7_die_temp[0].x > voltage) { |
| 584 | *result_mdec = DIE_TEMP_ADC7_SCALE_1; |
| 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | if (adcmap7_die_temp[ARRAY_SIZE(adcmap7_die_temp) - 1].x <= voltage) { |
| 589 | *result_mdec = DIE_TEMP_ADC7_MAX; |
| 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | for (i = 0; i < ARRAY_SIZE(adcmap7_die_temp); i++) |
| 594 | if (adcmap7_die_temp[i].x > voltage) |
| 595 | break; |
| 596 | |
| 597 | vtemp0 = adcmap7_die_temp[i - 1].x; |
| 598 | voltage = voltage - vtemp0; |
| 599 | temp = div64_s64(voltage * DIE_TEMP_ADC7_SCALE_FACTOR, |
| 600 | adcmap7_die_temp[i - 1].y); |
| 601 | temp += DIE_TEMP_ADC7_SCALE_1 + (DIE_TEMP_ADC7_SCALE_2 * (i - 1)); |
| 602 | *result_mdec = temp; |
| 603 | |
| 604 | return 0; |
| 605 | } |
| 606 | |
Siddartha Mohanadoss | e13d757 | 2018-08-02 18:43:53 -0700 | [diff] [blame] | 607 | static int qcom_vadc_scale_hw_smb_temp( |
| 608 | const struct vadc_prescale_ratio *prescale, |
| 609 | const struct adc5_data *data, |
| 610 | u16 adc_code, int *result_mdec) |
| 611 | { |
| 612 | *result_mdec = qcom_vadc_scale_code_voltage_factor(adc_code * 100, |
| 613 | prescale, data, PMIC5_SMB_TEMP_SCALE_FACTOR); |
| 614 | *result_mdec = PMIC5_SMB_TEMP_CONSTANT - *result_mdec; |
| 615 | |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | static int qcom_vadc_scale_hw_chg5_temp( |
| 620 | const struct vadc_prescale_ratio *prescale, |
| 621 | const struct adc5_data *data, |
| 622 | u16 adc_code, int *result_mdec) |
| 623 | { |
| 624 | *result_mdec = qcom_vadc_scale_code_voltage_factor(adc_code, |
| 625 | prescale, data, 4); |
| 626 | *result_mdec = PMIC5_CHG_TEMP_SCALE_FACTOR - *result_mdec; |
| 627 | |
| 628 | return 0; |
| 629 | } |
| 630 | |
Linus Walleij | e932d4f | 2017-04-04 14:08:18 +0200 | [diff] [blame] | 631 | int qcom_vadc_scale(enum vadc_scale_fn_type scaletype, |
| 632 | const struct vadc_linear_graph *calib_graph, |
| 633 | const struct vadc_prescale_ratio *prescale, |
| 634 | bool absolute, |
| 635 | u16 adc_code, int *result) |
| 636 | { |
| 637 | switch (scaletype) { |
| 638 | case SCALE_DEFAULT: |
| 639 | return qcom_vadc_scale_volt(calib_graph, prescale, |
| 640 | absolute, adc_code, |
| 641 | result); |
| 642 | case SCALE_THERM_100K_PULLUP: |
| 643 | case SCALE_XOTHERM: |
| 644 | return qcom_vadc_scale_therm(calib_graph, prescale, |
| 645 | absolute, adc_code, |
| 646 | result); |
| 647 | case SCALE_PMIC_THERM: |
| 648 | return qcom_vadc_scale_die_temp(calib_graph, prescale, |
| 649 | absolute, adc_code, |
| 650 | result); |
| 651 | case SCALE_PMI_CHG_TEMP: |
| 652 | return qcom_vadc_scale_chg_temp(calib_graph, prescale, |
| 653 | absolute, adc_code, |
| 654 | result); |
| 655 | default: |
| 656 | return -EINVAL; |
| 657 | } |
| 658 | } |
| 659 | EXPORT_SYMBOL(qcom_vadc_scale); |
| 660 | |
Siddartha Mohanadoss | e13d757 | 2018-08-02 18:43:53 -0700 | [diff] [blame] | 661 | int qcom_adc5_hw_scale(enum vadc_scale_fn_type scaletype, |
Dmitry Baryshkov | c7ba98f | 2020-12-04 05:54:57 +0300 | [diff] [blame] | 662 | unsigned int prescale_ratio, |
Siddartha Mohanadoss | e13d757 | 2018-08-02 18:43:53 -0700 | [diff] [blame] | 663 | const struct adc5_data *data, |
| 664 | u16 adc_code, int *result) |
| 665 | { |
Dmitry Baryshkov | c7ba98f | 2020-12-04 05:54:57 +0300 | [diff] [blame] | 666 | const struct vadc_prescale_ratio *prescale = &adc5_prescale_ratios[prescale_ratio]; |
| 667 | |
Siddartha Mohanadoss | e13d757 | 2018-08-02 18:43:53 -0700 | [diff] [blame] | 668 | if (!(scaletype >= SCALE_HW_CALIB_DEFAULT && |
| 669 | scaletype < SCALE_HW_CALIB_INVALID)) { |
| 670 | pr_err("Invalid scale type %d\n", scaletype); |
| 671 | return -EINVAL; |
| 672 | } |
| 673 | |
| 674 | return scale_adc5_fn[scaletype].scale_fn(prescale, data, |
| 675 | adc_code, result); |
| 676 | } |
| 677 | EXPORT_SYMBOL(qcom_adc5_hw_scale); |
| 678 | |
Dmitry Baryshkov | c7ba98f | 2020-12-04 05:54:57 +0300 | [diff] [blame] | 679 | int qcom_adc5_prescaling_from_dt(u32 num, u32 den) |
| 680 | { |
| 681 | unsigned int pre; |
| 682 | |
| 683 | for (pre = 0; pre < ARRAY_SIZE(adc5_prescale_ratios); pre++) |
| 684 | if (adc5_prescale_ratios[pre].num == num && |
| 685 | adc5_prescale_ratios[pre].den == den) |
| 686 | break; |
| 687 | |
| 688 | if (pre == ARRAY_SIZE(adc5_prescale_ratios)) |
| 689 | return -EINVAL; |
| 690 | |
| 691 | return pre; |
| 692 | } |
| 693 | EXPORT_SYMBOL(qcom_adc5_prescaling_from_dt); |
| 694 | |
| 695 | int qcom_adc5_hw_settle_time_from_dt(u32 value, |
| 696 | const unsigned int *hw_settle) |
| 697 | { |
| 698 | unsigned int i; |
| 699 | |
| 700 | for (i = 0; i < VADC_HW_SETTLE_SAMPLES_MAX; i++) { |
| 701 | if (value == hw_settle[i]) |
| 702 | return i; |
| 703 | } |
| 704 | |
| 705 | return -EINVAL; |
| 706 | } |
| 707 | EXPORT_SYMBOL(qcom_adc5_hw_settle_time_from_dt); |
| 708 | |
| 709 | int qcom_adc5_avg_samples_from_dt(u32 value) |
| 710 | { |
| 711 | if (!is_power_of_2(value) || value > ADC5_AVG_SAMPLES_MAX) |
| 712 | return -EINVAL; |
| 713 | |
| 714 | return __ffs(value); |
| 715 | } |
| 716 | EXPORT_SYMBOL(qcom_adc5_avg_samples_from_dt); |
| 717 | |
| 718 | int qcom_adc5_decimation_from_dt(u32 value, const unsigned int *decimation) |
| 719 | { |
| 720 | unsigned int i; |
| 721 | |
| 722 | for (i = 0; i < ADC5_DECIMATION_SAMPLES_MAX; i++) { |
| 723 | if (value == decimation[i]) |
| 724 | return i; |
| 725 | } |
| 726 | |
| 727 | return -EINVAL; |
| 728 | } |
| 729 | EXPORT_SYMBOL(qcom_adc5_decimation_from_dt); |
| 730 | |
Linus Walleij | e932d4f | 2017-04-04 14:08:18 +0200 | [diff] [blame] | 731 | int qcom_vadc_decimation_from_dt(u32 value) |
| 732 | { |
| 733 | if (!is_power_of_2(value) || value < VADC_DECIMATION_MIN || |
| 734 | value > VADC_DECIMATION_MAX) |
| 735 | return -EINVAL; |
| 736 | |
| 737 | return __ffs64(value / VADC_DECIMATION_MIN); |
| 738 | } |
| 739 | EXPORT_SYMBOL(qcom_vadc_decimation_from_dt); |
Linus Walleij | 9a0ebbc | 2017-11-13 11:29:55 +0100 | [diff] [blame] | 740 | |
| 741 | MODULE_LICENSE("GPL v2"); |
| 742 | MODULE_DESCRIPTION("Qualcomm ADC common functionality"); |