qcom-vadc-common.c 16 KB

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