blob: c5653feac14a73629bb1df0d16683f9dc80452ab [file] [log] [blame]
Jishnu Prakash3b3fe942018-01-08 12:23:39 +05301/* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#define pr_fmt(fmt) "%s: " fmt, __func__
14
15#include <linux/kernel.h>
16#include <linux/of.h>
17#include <linux/err.h>
18#include <linux/init.h>
19#include <linux/slab.h>
20#include <linux/delay.h>
21#include <linux/mutex.h>
22#include <linux/types.h>
23#include <linux/hwmon.h>
24#include <linux/module.h>
25#include <linux/debugfs.h>
26#include <linux/spmi.h>
27#include <linux/platform_device.h>
28#include <linux/of_irq.h>
29#include <linux/interrupt.h>
30#include <linux/completion.h>
31#include <linux/qpnp/qpnp-adc.h>
32
33#define KELVINMIL_DEGMIL 273160
34#define QPNP_VADC_LDO_VOLTAGE_MIN 1800000
35#define QPNP_VADC_LDO_VOLTAGE_MAX 1800000
36#define QPNP_VADC_OK_VOLTAGE_MIN 1000000
37#define QPNP_VADC_OK_VOLTAGE_MAX 1000000
38#define PMI_CHG_SCALE_1 -138890
39#define PMI_CHG_SCALE_2 391750000000
40#define QPNP_VADC_HC_VREF_CODE 0x4000
41#define QPNP_VADC_HC_VDD_REFERENCE_MV 1875
Jishnu Prakash3b3fe942018-01-08 12:23:39 +053042#define CHRG_SCALE_1 -250
43#define CHRG_SCALE_2 377500000
44#define DIE_SCALE_1 500
45#define DIE_SCALE_2 -273150000
46
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -080047/* Clamp negative ADC code to 0 */
48#define QPNP_VADC_HC_MAX_CODE 0x7FFF
49
Jishnu Prakash3b3fe942018-01-08 12:23:39 +053050/*Invalid current reading*/
51#define QPNP_IADC_INV 0x8000
52
53#define IADC_SCALE_1 0xffff
54#define IADC_SCALE_2 152593
55
56#define USBIN_I_SCALE 25
57
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -080058/*
59 * Units for temperature below (on x axis) is in 0.1DegC as
60 * required by the battery driver. Note the resolution used
61 * here to compute the table was done for DegC to milli-volts.
62 * In consideration to limit the size of the table for the given
63 * temperature range below, the result is linearly interpolated
64 * and provided to the battery driver in the units desired for
65 * their framework which is 0.1DegC. True resolution of 0.1DegC
66 * will result in the below table size to increase by 10 times.
67 */
68static const struct qpnp_vadc_map_pt adcmap_btm_threshold[] = {
69 {-300, 1642},
70 {-200, 1544},
71 {-100, 1414},
72 {0, 1260},
73 {10, 1244},
74 {20, 1228},
75 {30, 1212},
76 {40, 1195},
77 {50, 1179},
78 {60, 1162},
79 {70, 1146},
80 {80, 1129},
81 {90, 1113},
82 {100, 1097},
83 {110, 1080},
84 {120, 1064},
85 {130, 1048},
86 {140, 1032},
87 {150, 1016},
88 {160, 1000},
89 {170, 985},
90 {180, 969},
91 {190, 954},
92 {200, 939},
93 {210, 924},
94 {220, 909},
95 {230, 894},
96 {240, 880},
97 {250, 866},
98 {260, 852},
99 {270, 838},
100 {280, 824},
101 {290, 811},
102 {300, 798},
103 {310, 785},
104 {320, 773},
105 {330, 760},
106 {340, 748},
107 {350, 736},
108 {360, 725},
109 {370, 713},
110 {380, 702},
111 {390, 691},
112 {400, 681},
113 {410, 670},
114 {420, 660},
115 {430, 650},
116 {440, 640},
117 {450, 631},
118 {460, 622},
119 {470, 613},
120 {480, 604},
121 {490, 595},
122 {500, 587},
123 {510, 579},
124 {520, 571},
125 {530, 563},
126 {540, 556},
127 {550, 548},
128 {560, 541},
129 {570, 534},
130 {580, 527},
131 {590, 521},
132 {600, 514},
133 {610, 508},
134 {620, 502},
135 {630, 496},
136 {640, 490},
137 {650, 485},
138 {660, 281},
139 {670, 274},
140 {680, 267},
141 {690, 260},
142 {700, 254},
143 {710, 247},
144 {720, 241},
145 {730, 235},
146 {740, 229},
147 {750, 224},
148 {760, 218},
149 {770, 213},
150 {780, 208},
151 {790, 203}
152};
153
154static const struct qpnp_vadc_map_pt adcmap_qrd_btm_threshold[] = {
155 {-200, 1540},
156 {-180, 1517},
157 {-160, 1492},
158 {-140, 1467},
159 {-120, 1440},
160 {-100, 1412},
161 {-80, 1383},
162 {-60, 1353},
163 {-40, 1323},
164 {-20, 1292},
165 {0, 1260},
166 {20, 1228},
167 {40, 1196},
168 {60, 1163},
169 {80, 1131},
170 {100, 1098},
171 {120, 1066},
172 {140, 1034},
173 {160, 1002},
174 {180, 971},
175 {200, 941},
176 {220, 911},
177 {240, 882},
178 {260, 854},
179 {280, 826},
180 {300, 800},
181 {320, 774},
182 {340, 749},
183 {360, 726},
184 {380, 703},
185 {400, 681},
186 {420, 660},
187 {440, 640},
188 {460, 621},
189 {480, 602},
190 {500, 585},
191 {520, 568},
192 {540, 552},
193 {560, 537},
194 {580, 523},
195 {600, 510},
196 {620, 497},
197 {640, 485},
198 {660, 473},
199 {680, 462},
200 {700, 452},
201 {720, 442},
202 {740, 433},
203 {760, 424},
204 {780, 416},
205 {800, 408},
206};
207
208static const struct qpnp_vadc_map_pt adcmap_qrd_skuaa_btm_threshold[] = {
209 {-200, 1476},
210 {-180, 1450},
211 {-160, 1422},
212 {-140, 1394},
213 {-120, 1365},
214 {-100, 1336},
215 {-80, 1306},
216 {-60, 1276},
217 {-40, 1246},
218 {-20, 1216},
219 {0, 1185},
220 {20, 1155},
221 {40, 1126},
222 {60, 1096},
223 {80, 1068},
224 {100, 1040},
225 {120, 1012},
226 {140, 986},
227 {160, 960},
228 {180, 935},
229 {200, 911},
230 {220, 888},
231 {240, 866},
232 {260, 844},
233 {280, 824},
234 {300, 805},
235 {320, 786},
236 {340, 769},
237 {360, 752},
238 {380, 737},
239 {400, 722},
240 {420, 707},
241 {440, 694},
242 {460, 681},
243 {480, 669},
244 {500, 658},
245 {520, 648},
246 {540, 637},
247 {560, 628},
248 {580, 619},
249 {600, 611},
250 {620, 603},
251 {640, 595},
252 {660, 588},
253 {680, 582},
254 {700, 575},
255 {720, 569},
256 {740, 564},
257 {760, 559},
258 {780, 554},
259 {800, 549},
260};
261
262static const struct qpnp_vadc_map_pt adcmap_qrd_skug_btm_threshold[] = {
263 {-200, 1338},
264 {-180, 1307},
265 {-160, 1276},
266 {-140, 1244},
267 {-120, 1213},
268 {-100, 1182},
269 {-80, 1151},
270 {-60, 1121},
271 {-40, 1092},
272 {-20, 1063},
273 {0, 1035},
274 {20, 1008},
275 {40, 982},
276 {60, 957},
277 {80, 933},
278 {100, 910},
279 {120, 889},
280 {140, 868},
281 {160, 848},
282 {180, 830},
283 {200, 812},
284 {220, 795},
285 {240, 780},
286 {260, 765},
287 {280, 751},
288 {300, 738},
289 {320, 726},
290 {340, 714},
291 {360, 704},
292 {380, 694},
293 {400, 684},
294 {420, 675},
295 {440, 667},
296 {460, 659},
297 {480, 652},
298 {500, 645},
299 {520, 639},
300 {540, 633},
301 {560, 627},
302 {580, 622},
303 {600, 617},
304 {620, 613},
305 {640, 608},
306 {660, 604},
307 {680, 600},
308 {700, 597},
309 {720, 593},
310 {740, 590},
311 {760, 587},
312 {780, 585},
313 {800, 582},
314};
315
316static const struct qpnp_vadc_map_pt adcmap_qrd_skuh_btm_threshold[] = {
317 {-200, 1531},
318 {-180, 1508},
319 {-160, 1483},
320 {-140, 1458},
321 {-120, 1432},
322 {-100, 1404},
323 {-80, 1377},
324 {-60, 1348},
325 {-40, 1319},
326 {-20, 1290},
327 {0, 1260},
328 {20, 1230},
329 {40, 1200},
330 {60, 1171},
331 {80, 1141},
332 {100, 1112},
333 {120, 1083},
334 {140, 1055},
335 {160, 1027},
336 {180, 1000},
337 {200, 973},
338 {220, 948},
339 {240, 923},
340 {260, 899},
341 {280, 876},
342 {300, 854},
343 {320, 832},
344 {340, 812},
345 {360, 792},
346 {380, 774},
347 {400, 756},
348 {420, 739},
349 {440, 723},
350 {460, 707},
351 {480, 692},
352 {500, 679},
353 {520, 665},
354 {540, 653},
355 {560, 641},
356 {580, 630},
357 {600, 619},
358 {620, 609},
359 {640, 600},
360 {660, 591},
361 {680, 583},
362 {700, 575},
363 {720, 567},
364 {740, 560},
365 {760, 553},
366 {780, 547},
367 {800, 541},
368 {820, 535},
369 {840, 530},
370 {860, 524},
371 {880, 520},
372};
373
374static const struct qpnp_vadc_map_pt adcmap_qrd_skut1_btm_threshold[] = {
375 {-400, 1759},
376 {-350, 1742},
377 {-300, 1720},
378 {-250, 1691},
379 {-200, 1654},
380 {-150, 1619},
381 {-100, 1556},
382 {-50, 1493},
383 {0, 1422},
384 {50, 1345},
385 {100, 1264},
386 {150, 1180},
387 {200, 1097},
388 {250, 1017},
389 {300, 942},
390 {350, 873},
391 {400, 810},
392 {450, 754},
393 {500, 706},
394 {550, 664},
395 {600, 627},
396 {650, 596},
397 {700, 570},
398 {750, 547},
399 {800, 528},
400 {850, 512},
401 {900, 499},
402 {950, 487},
403 {1000, 477},
404};
405
406/* Voltage to temperature */
407static const struct qpnp_vadc_map_pt adcmap_100k_104ef_104fb[] = {
Siddartha Mohanadoss73f4f4e2017-04-14 17:44:14 -0700408 {1758, -40000},
409 {1742, -35000},
410 {1719, -30000},
411 {1691, -25000},
412 {1654, -20000},
413 {1608, -15000},
414 {1551, -10000},
415 {1483, -5000},
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -0800416 {1404, 0},
Siddartha Mohanadoss73f4f4e2017-04-14 17:44:14 -0700417 {1315, 5000},
418 {1218, 10000},
419 {1114, 15000},
420 {1007, 20000},
421 {900, 25000},
422 {795, 30000},
423 {696, 35000},
424 {605, 40000},
425 {522, 45000},
426 {448, 50000},
427 {383, 55000},
428 {327, 60000},
429 {278, 65000},
430 {237, 70000},
431 {202, 75000},
432 {172, 80000},
433 {146, 85000},
434 {125, 90000},
435 {107, 95000},
436 {92, 100000},
437 {79, 105000},
438 {68, 110000},
439 {59, 115000},
440 {51, 120000},
441 {44, 125000}
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -0800442};
443
444/* Voltage to temperature */
445static const struct qpnp_vadc_map_pt adcmap_150k_104ef_104fb[] = {
Siddartha Mohanadoss73f4f4e2017-04-14 17:44:14 -0700446 {1738, -40000},
447 {1714, -35000},
448 {1682, -30000},
449 {1641, -25000},
450 {1589, -20000},
451 {1526, -15000},
452 {1451, -10000},
453 {1363, -5000},
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -0800454 {1266, 0},
Siddartha Mohanadoss73f4f4e2017-04-14 17:44:14 -0700455 {1159, 5000},
456 {1048, 10000},
457 {936, 15000},
458 {825, 20000},
459 {720, 25000},
460 {622, 30000},
461 {533, 35000},
462 {454, 40000},
463 {385, 45000},
464 {326, 50000},
465 {275, 55000},
466 {232, 60000},
467 {195, 65000},
468 {165, 70000},
469 {139, 75000},
470 {118, 80000},
471 {100, 85000},
472 {85, 90000},
473 {73, 95000},
474 {62, 100000},
475 {53, 105000},
476 {46, 110000},
477 {40, 115000},
478 {34, 120000},
479 {30, 125000}
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -0800480};
481
482static const struct qpnp_vadc_map_pt adcmap_smb_batt_therm[] = {
483 {-300, 1625},
484 {-200, 1515},
485 {-100, 1368},
486 {0, 1192},
487 {10, 1173},
488 {20, 1154},
489 {30, 1135},
490 {40, 1116},
491 {50, 1097},
492 {60, 1078},
493 {70, 1059},
494 {80, 1040},
495 {90, 1020},
496 {100, 1001},
497 {110, 982},
498 {120, 963},
499 {130, 944},
500 {140, 925},
501 {150, 907},
502 {160, 888},
503 {170, 870},
504 {180, 851},
505 {190, 833},
506 {200, 815},
507 {210, 797},
508 {220, 780},
509 {230, 762},
510 {240, 745},
511 {250, 728},
512 {260, 711},
513 {270, 695},
514 {280, 679},
515 {290, 663},
516 {300, 647},
517 {310, 632},
518 {320, 616},
519 {330, 602},
520 {340, 587},
521 {350, 573},
522 {360, 559},
523 {370, 545},
524 {380, 531},
525 {390, 518},
526 {400, 505},
527 {410, 492},
528 {420, 480},
529 {430, 465},
530 {440, 456},
531 {450, 445},
532 {460, 433},
533 {470, 422},
534 {480, 412},
535 {490, 401},
536 {500, 391},
537 {510, 381},
538 {520, 371},
539 {530, 362},
540 {540, 352},
541 {550, 343},
542 {560, 335},
543 {570, 326},
544 {580, 318},
545 {590, 309},
546 {600, 302},
547 {610, 294},
548 {620, 286},
549 {630, 279},
550 {640, 272},
551 {650, 265},
552 {660, 258},
553 {670, 252},
554 {680, 245},
555 {690, 239},
556 {700, 233},
557 {710, 227},
558 {720, 221},
559 {730, 216},
560 {740, 211},
561 {750, 205},
562 {760, 200},
563 {770, 195},
564 {780, 190},
565 {790, 186}
566};
567
568/* Voltage to temperature */
569static const struct qpnp_vadc_map_pt adcmap_ncp03wf683[] = {
570 {1742, -40},
571 {1718, -35},
572 {1687, -30},
573 {1647, -25},
574 {1596, -20},
575 {1534, -15},
576 {1459, -10},
577 {1372, -5},
578 {1275, 0},
579 {1169, 5},
580 {1058, 10},
581 {945, 15},
582 {834, 20},
583 {729, 25},
584 {630, 30},
585 {541, 35},
586 {461, 40},
587 {392, 45},
588 {332, 50},
589 {280, 55},
590 {236, 60},
591 {199, 65},
592 {169, 70},
593 {142, 75},
594 {121, 80},
595 {102, 85},
596 {87, 90},
597 {74, 95},
598 {64, 100},
599 {55, 105},
600 {47, 110},
601 {40, 115},
602 {35, 120},
603 {30, 125}
604};
605
Jishnu Prakash3b3fe942018-01-08 12:23:39 +0530606/* Voltage to temperature */
607static const struct qpnp_vadc_map_pt adcmap_batt_therm[] = {
608 {1770, -400},
609 {1757, -380},
610 {1743, -360},
611 {1727, -340},
612 {1710, -320},
613 {1691, -300},
614 {1671, -280},
615 {1650, -260},
616 {1627, -240},
617 {1602, -220},
618 {1576, -200},
619 {1548, -180},
620 {1519, -160},
621 {1488, -140},
622 {1456, -120},
623 {1423, -100},
624 {1388, -80},
625 {1353, -60},
626 {1316, -40},
627 {1278, -20},
628 {1240, 0},
629 {1201, 20},
630 {1162, 40},
631 {1122, 60},
632 {1082, 80},
633 {1042, 100},
634 {1003, 120},
635 {964, 140},
636 {925, 160},
637 {887, 180},
638 {849, 200},
639 {812, 220},
640 {777, 240},
641 {742, 260},
642 {708, 280},
643 {675, 300},
644 {643, 320},
645 {613, 340},
646 {583, 360},
647 {555, 380},
648 {528, 400},
649 {502, 420},
650 {477, 440},
651 {453, 460},
652 {430, 480},
653 {409, 500},
654 {388, 520},
655 {369, 540},
656 {350, 560},
657 {333, 580},
658 {316, 600},
659 {300, 620},
660 {285, 640},
661 {271, 660},
662 {257, 680},
663 {245, 700},
664 {233, 720},
665 {221, 740},
666 {210, 760},
667 {200, 780},
668 {190, 800},
669 {181, 820},
670 {173, 840},
671 {164, 860},
672 {157, 880},
673 {149, 900},
674 {142, 920},
675 {136, 940},
676 {129, 960},
677 {124, 980}
678};
679
Jishnu Prakash20343d42018-02-12 14:53:33 +0530680/* Voltage to temperature */
681static const struct qpnp_vadc_map_pt adcmap_batt_therm_qrd[] = {
682 {1840, -400},
683 {1835, -380},
684 {1828, -360},
685 {1821, -340},
686 {1813, -320},
687 {1803, -300},
688 {1793, -280},
689 {1781, -260},
690 {1768, -240},
691 {1753, -220},
692 {1737, -200},
693 {1719, -180},
694 {1700, -160},
695 {1679, -140},
696 {1655, -120},
697 {1630, -100},
698 {1603, -80},
699 {1574, -60},
700 {1543, -40},
701 {1510, -20},
702 {1475, 00},
703 {1438, 20},
704 {1400, 40},
705 {1360, 60},
706 {1318, 80},
707 {1276, 100},
708 {1232, 120},
709 {1187, 140},
710 {1142, 160},
711 {1097, 180},
712 {1051, 200},
713 {1005, 220},
714 {960, 240},
715 {915, 260},
716 {871, 280},
717 {828, 300},
718 {786, 320},
719 {745, 340},
720 {705, 360},
721 {666, 380},
722 {629, 400},
723 {594, 420},
724 {560, 440},
725 {527, 460},
726 {497, 480},
727 {467, 500},
728 {439, 520},
729 {413, 540},
730 {388, 560},
731 {365, 580},
732 {343, 600},
733 {322, 620},
734 {302, 640},
735 {284, 660},
736 {267, 680},
737 {251, 700},
738 {235, 720},
739 {221, 740},
740 {208, 760},
741 {195, 780},
742 {184, 800},
743 {173, 820},
744 {163, 840},
745 {153, 860},
746 {144, 880},
747 {136, 900},
748 {128, 920},
749 {120, 940},
750 {114, 960},
751 {107, 980}
752};
753
Jishnu Prakash64c9d3f2018-04-12 17:01:32 +0530754/* Voltage to temperature */
755static const struct qpnp_vadc_map_pt adcmap_batt_therm_pu30[] = {
756 {1842, -400},
757 {1838, -380},
758 {1833, -360},
759 {1828, -340},
760 {1822, -320},
761 {1816, -300},
762 {1809, -280},
763 {1801, -260},
764 {1793, -240},
765 {1784, -220},
766 {1774, -200},
767 {1763, -180},
768 {1752, -160},
769 {1739, -140},
770 {1726, -120},
771 {1712, -100},
772 {1697, -80},
773 {1680, -60},
774 {1663, -40},
775 {1645, -20},
776 {1625, 0},
777 {1605, 20},
778 {1583, 40},
779 {1561, 60},
780 {1537, 80},
781 {1513, 100},
782 {1487, 120},
783 {1461, 140},
784 {1433, 160},
785 {1405, 180},
786 {1376, 200},
787 {1347, 220},
788 {1316, 240},
789 {1286, 260},
790 {1254, 280},
791 {1223, 300},
792 {1191, 320},
793 {1159, 340},
794 {1126, 360},
795 {1094, 380},
796 {1062, 400},
797 {1029, 420},
798 {997, 440},
799 {966, 460},
800 {934, 480},
801 {903, 500},
802 {873, 520},
803 {843, 540},
804 {813, 560},
805 {784, 580},
806 {756, 600},
807 {728, 620},
808 {702, 640},
809 {675, 660},
810 {650, 680},
811 {625, 700},
812 {601, 720},
813 {578, 740},
814 {556, 760},
815 {534, 780},
816 {513, 800},
817 {493, 820},
818 {474, 840},
819 {455, 860},
820 {437, 880},
821 {420, 900},
822 {403, 920},
823 {387, 940},
824 {372, 960},
825 {357, 980}
826};
827
828/* Voltage to temp0erature */
829static const struct qpnp_vadc_map_pt adcmap_batt_therm_pu400[] = {
830 {1516, -400},
831 {1478, -380},
832 {1438, -360},
833 {1396, -340},
834 {1353, -320},
835 {1307, -300},
836 {1261, -280},
837 {1213, -260},
838 {1164, -240},
839 {1115, -220},
840 {1066, -200},
841 {1017, -180},
842 {968, -160},
843 {920, -140},
844 {872, -120},
845 {826, -100},
846 {781, -80},
847 {737, -60},
848 {694, -40},
849 {654, -20},
850 {615, 0},
851 {578, 20},
852 {542, 40},
853 {509, 60},
854 {477, 80},
855 {447, 100},
856 {419, 120},
857 {392, 140},
858 {367, 160},
859 {343, 180},
860 {321, 200},
861 {301, 220},
862 {282, 240},
863 {264, 260},
864 {247, 280},
865 {231, 300},
866 {216, 320},
867 {203, 340},
868 {190, 360},
869 {178, 380},
870 {167, 400},
871 {157, 420},
872 {147, 440},
873 {138, 460},
874 {130, 480},
875 {122, 500},
876 {115, 520},
877 {108, 540},
878 {102, 560},
879 {96, 580},
880 {90, 600},
881 {85, 620},
882 {80, 640},
883 {76, 660},
884 {72, 680},
885 {68, 700},
886 {64, 720},
887 {61, 740},
888 {57, 760},
889 {54, 780},
890 {52, 800},
891 {49, 820},
892 {46, 840},
893 {44, 860},
894 {42, 880},
895 {40, 900},
896 {38, 920},
897 {36, 940},
898 {34, 960},
899 {32, 980}
900};
901
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -0800902/*
903 * Voltage to temperature table for 100k pull up for NTCG104EF104 with
904 * 1.875V reference.
905 */
906static const struct qpnp_vadc_map_pt adcmap_100k_104ef_104fb_1875_vref[] = {
Siddartha Mohanadoss73f4f4e2017-04-14 17:44:14 -0700907 { 1831, -40000 },
908 { 1814, -35000 },
909 { 1791, -30000 },
910 { 1761, -25000 },
911 { 1723, -20000 },
912 { 1675, -15000 },
913 { 1616, -10000 },
914 { 1545, -5000 },
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -0800915 { 1463, 0 },
Siddartha Mohanadoss73f4f4e2017-04-14 17:44:14 -0700916 { 1370, 5000 },
917 { 1268, 10000 },
918 { 1160, 15000 },
919 { 1049, 20000 },
920 { 937, 25000 },
921 { 828, 30000 },
922 { 726, 35000 },
923 { 630, 40000 },
924 { 544, 45000 },
925 { 467, 50000 },
926 { 399, 55000 },
927 { 340, 60000 },
928 { 290, 65000 },
929 { 247, 70000 },
930 { 209, 75000 },
931 { 179, 80000 },
932 { 153, 85000 },
933 { 130, 90000 },
934 { 112, 95000 },
935 { 96, 100000 },
936 { 82, 105000 },
937 { 71, 110000 },
938 { 62, 115000 },
939 { 53, 120000 },
940 { 46, 125000 },
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -0800941};
942
943static int32_t qpnp_adc_map_voltage_temp(const struct qpnp_vadc_map_pt *pts,
944 uint32_t tablesize, int32_t input, int64_t *output)
945{
946 bool descending = 1;
947 uint32_t i = 0;
948
949 if (pts == NULL)
950 return -EINVAL;
951
952 /* Check if table is descending or ascending */
953 if (tablesize > 1) {
954 if (pts[0].x < pts[1].x)
955 descending = 0;
956 }
957
958 while (i < tablesize) {
959 if ((descending == 1) && (pts[i].x < input)) {
960 /*
961 * table entry is less than measured
962 * value and table is descending, stop.
963 */
964 break;
965 } else if ((descending == 0) &&
966 (pts[i].x > input)) {
967 /*
968 * table entry is greater than measured
969 * value and table is ascending, stop.
970 */
971 break;
972 }
973 i++;
974 }
975
976 if (i == 0)
977 *output = pts[0].y;
978 else if (i == tablesize)
979 *output = pts[tablesize-1].y;
980 else {
981 /* result is between search_index and search_index-1 */
982 /* interpolate linearly */
983 *output = (((int32_t) ((pts[i].y - pts[i-1].y)*
984 (input - pts[i-1].x))/
985 (pts[i].x - pts[i-1].x))+
986 pts[i-1].y);
987 }
988
989 return 0;
990}
991
992static int32_t qpnp_adc_map_temp_voltage(const struct qpnp_vadc_map_pt *pts,
993 uint32_t tablesize, int32_t input, int64_t *output)
994{
995 bool descending = 1;
996 uint32_t i = 0;
997
998 if (pts == NULL)
999 return -EINVAL;
1000
1001 /* Check if table is descending or ascending */
1002 if (tablesize > 1) {
1003 if (pts[0].y < pts[1].y)
1004 descending = 0;
1005 }
1006
1007 while (i < tablesize) {
1008 if ((descending == 1) && (pts[i].y < input)) {
1009 /* Table entry is less than measured value. */
1010 /* Table is descending, stop. */
1011 break;
1012 } else if ((descending == 0) && (pts[i].y > input)) {
1013 /* Table entry is greater than measured value. */
1014 /* Table is ascending, stop. */
1015 break;
1016 }
1017 i++;
1018 }
1019
1020 if (i == 0) {
1021 *output = pts[0].x;
1022 } else if (i == tablesize) {
1023 *output = pts[tablesize-1].x;
1024 } else {
1025 /* result is between search_index and search_index-1 */
1026 /* interpolate linearly */
1027 *output = (((int32_t) ((pts[i].x - pts[i-1].x)*
1028 (input - pts[i-1].y))/
1029 (pts[i].y - pts[i-1].y))+
1030 pts[i-1].x);
1031 }
1032
1033 return 0;
1034}
1035
1036static void qpnp_adc_scale_with_calib_param(int32_t adc_code,
1037 const struct qpnp_adc_properties *adc_properties,
1038 const struct qpnp_vadc_chan_properties *chan_properties,
1039 int64_t *scale_voltage)
1040{
1041 *scale_voltage = (adc_code -
1042 chan_properties->adc_graph[chan_properties->calib_type].adc_gnd)
1043 * chan_properties->adc_graph[chan_properties->calib_type].dx;
1044 *scale_voltage = div64_s64(*scale_voltage,
1045 chan_properties->adc_graph[chan_properties->calib_type].dy);
1046
1047 if (chan_properties->calib_type == CALIB_ABSOLUTE)
1048 *scale_voltage +=
1049 chan_properties->adc_graph[chan_properties->calib_type].dx;
1050
1051 if (*scale_voltage < 0)
1052 *scale_voltage = 0;
1053}
1054
1055int32_t qpnp_adc_scale_pmic_therm(struct qpnp_vadc_chip *vadc,
1056 int32_t adc_code,
1057 const struct qpnp_adc_properties *adc_properties,
1058 const struct qpnp_vadc_chan_properties *chan_properties,
1059 struct qpnp_vadc_result *adc_chan_result)
1060{
1061 int64_t pmic_voltage = 0;
1062
1063 if (!chan_properties || !chan_properties->offset_gain_numerator ||
1064 !chan_properties->offset_gain_denominator || !adc_properties
1065 || !adc_chan_result)
1066 return -EINVAL;
1067
1068 if (adc_properties->adc_hc) {
Siddartha Mohanadossd9e74fe2017-10-13 15:27:55 -07001069 /* (ADC code * vref_vadc (1.875V)) / scale_code */
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001070 if (adc_code > QPNP_VADC_HC_MAX_CODE)
1071 adc_code = 0;
1072 pmic_voltage = (int64_t) adc_code;
1073 pmic_voltage *= (int64_t) (adc_properties->adc_vdd_reference
1074 * 1000);
1075 pmic_voltage = div64_s64(pmic_voltage,
Siddartha Mohanadossd9e74fe2017-10-13 15:27:55 -07001076 adc_properties->full_scale_code);
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001077 } else {
1078 if (!chan_properties->adc_graph[CALIB_ABSOLUTE].dy)
1079 return -EINVAL;
1080 qpnp_adc_scale_with_calib_param(adc_code, adc_properties,
1081 chan_properties, &pmic_voltage);
1082 }
1083
1084 if (pmic_voltage > 0) {
1085 /* 2mV/K */
1086 adc_chan_result->measurement = pmic_voltage*
1087 chan_properties->offset_gain_denominator;
1088
Siddartha Mohanadoss04e90272017-10-23 13:36:21 -07001089 adc_chan_result->measurement =
1090 div64_s64(adc_chan_result->measurement,
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001091 chan_properties->offset_gain_numerator * 2);
1092 } else
1093 adc_chan_result->measurement = 0;
1094
1095 /* Change to .001 deg C */
1096 adc_chan_result->measurement -= KELVINMIL_DEGMIL;
1097 adc_chan_result->physical = (int32_t) adc_chan_result->measurement;
1098
1099 return 0;
1100}
1101EXPORT_SYMBOL(qpnp_adc_scale_pmic_therm);
1102
1103int32_t qpnp_adc_scale_millidegc_pmic_voltage_thr(struct qpnp_vadc_chip *chip,
1104 struct qpnp_adc_tm_btm_param *param,
1105 uint32_t *low_threshold, uint32_t *high_threshold)
1106{
1107 struct qpnp_vadc_linear_graph btm_param;
1108 int64_t low_output = 0, high_output = 0;
1109 int rc = 0, sign = 0;
1110
1111 /* Convert to Kelvin and account for voltage to be written as 2mV/K */
1112 low_output = (param->low_temp + KELVINMIL_DEGMIL) * 2;
1113 /* Convert to Kelvin and account for voltage to be written as 2mV/K */
1114 high_output = (param->high_temp + KELVINMIL_DEGMIL) * 2;
1115
1116 if (param->adc_tm_hc) {
Siddartha Mohanadossd9e74fe2017-10-13 15:27:55 -07001117 low_output *= param->full_scale_code;
Siddartha Mohanadoss04e90272017-10-23 13:36:21 -07001118 low_output = div64_s64(low_output,
1119 (QPNP_VADC_HC_VDD_REFERENCE_MV * 1000));
Siddartha Mohanadossd9e74fe2017-10-13 15:27:55 -07001120 high_output *= param->full_scale_code;
Siddartha Mohanadoss04e90272017-10-23 13:36:21 -07001121 high_output = div64_s64(high_output,
1122 (QPNP_VADC_HC_VDD_REFERENCE_MV * 1000));
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001123 } else {
1124 rc = qpnp_get_vadc_gain_and_offset(chip, &btm_param,
1125 CALIB_ABSOLUTE);
1126 if (rc < 0) {
1127 pr_err("Could not acquire gain and offset\n");
1128 return rc;
1129 }
1130
1131 /* Convert to voltage threshold */
1132 low_output = (low_output - QPNP_ADC_625_UV) * btm_param.dy;
1133 if (low_output < 0) {
1134 sign = 1;
1135 low_output = -low_output;
1136 }
Siddartha Mohanadoss04e90272017-10-23 13:36:21 -07001137 low_output = div64_s64(low_output, QPNP_ADC_625_UV);
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001138 if (sign)
1139 low_output = -low_output;
1140 low_output += btm_param.adc_gnd;
1141
1142 sign = 0;
1143 /* Convert to voltage threshold */
1144 high_output = (high_output - QPNP_ADC_625_UV) * btm_param.dy;
1145 if (high_output < 0) {
1146 sign = 1;
1147 high_output = -high_output;
1148 }
Siddartha Mohanadoss04e90272017-10-23 13:36:21 -07001149 high_output = div64_s64(high_output, QPNP_ADC_625_UV);
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001150 if (sign)
1151 high_output = -high_output;
1152 high_output += btm_param.adc_gnd;
1153 }
1154
1155 *low_threshold = (uint32_t) low_output;
1156 *high_threshold = (uint32_t) high_output;
1157
1158 pr_debug("high_temp:%d, low_temp:%d\n", param->high_temp,
1159 param->low_temp);
1160 pr_debug("adc_code_high:%x, adc_code_low:%x\n", *high_threshold,
1161 *low_threshold);
1162
1163 return 0;
1164}
1165EXPORT_SYMBOL(qpnp_adc_scale_millidegc_pmic_voltage_thr);
1166
1167/* Scales the ADC code to degC using the mapping
1168 * table for the XO thermistor.
1169 */
1170int32_t qpnp_adc_tdkntcg_therm(struct qpnp_vadc_chip *chip,
1171 int32_t adc_code,
1172 const struct qpnp_adc_properties *adc_properties,
1173 const struct qpnp_vadc_chan_properties *chan_properties,
1174 struct qpnp_vadc_result *adc_chan_result)
1175{
1176 int64_t xo_thm_voltage = 0;
1177
1178 if (!chan_properties || !chan_properties->offset_gain_numerator ||
1179 !chan_properties->offset_gain_denominator || !adc_properties
1180 || !adc_chan_result)
1181 return -EINVAL;
1182
1183 if (adc_properties->adc_hc) {
Siddartha Mohanadossd9e74fe2017-10-13 15:27:55 -07001184 /* (code * vref_vadc (1.875V) * 1000) / (scale_code * 1000) */
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001185 if (adc_code > QPNP_VADC_HC_MAX_CODE)
1186 adc_code = 0;
1187 xo_thm_voltage = (int64_t) adc_code;
1188 xo_thm_voltage *= (int64_t) (adc_properties->adc_vdd_reference
1189 * 1000);
1190 xo_thm_voltage = div64_s64(xo_thm_voltage,
Siddartha Mohanadossd9e74fe2017-10-13 15:27:55 -07001191 adc_properties->full_scale_code * 1000);
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001192 qpnp_adc_map_voltage_temp(adcmap_100k_104ef_104fb_1875_vref,
1193 ARRAY_SIZE(adcmap_100k_104ef_104fb_1875_vref),
1194 xo_thm_voltage, &adc_chan_result->physical);
1195 } else {
1196 qpnp_adc_scale_with_calib_param(adc_code,
1197 adc_properties, chan_properties, &xo_thm_voltage);
1198
1199 if (chan_properties->calib_type == CALIB_ABSOLUTE)
Siddartha Mohanadoss04e90272017-10-23 13:36:21 -07001200 xo_thm_voltage = div64_s64(xo_thm_voltage, 1000);
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001201
1202 qpnp_adc_map_voltage_temp(adcmap_100k_104ef_104fb,
1203 ARRAY_SIZE(adcmap_100k_104ef_104fb),
1204 xo_thm_voltage, &adc_chan_result->physical);
1205 }
1206
1207 return 0;
1208}
1209EXPORT_SYMBOL(qpnp_adc_tdkntcg_therm);
1210
Jishnu Prakash3b3fe942018-01-08 12:23:39 +05301211int32_t qpnp_adc_batt_therm(struct qpnp_vadc_chip *chip,
1212 int32_t adc_code,
1213 const struct qpnp_adc_properties *adc_properties,
1214 const struct qpnp_vadc_chan_properties *chan_properties,
1215 struct qpnp_vadc_result *adc_chan_result)
1216{
1217 int64_t batt_thm_voltage = 0;
1218
1219 if (!chan_properties || !chan_properties->offset_gain_numerator ||
1220 !chan_properties->offset_gain_denominator || !adc_properties
1221 || !adc_chan_result)
1222 return -EINVAL;
1223
1224 if (adc_properties->adc_hc) {
1225 /* (code * vref_vadc (1.875V) * 1000) / (scale_code * 1000) */
1226 if (adc_code > QPNP_VADC_HC_MAX_CODE)
1227 adc_code = 0;
1228 batt_thm_voltage = (int64_t) adc_code;
1229 batt_thm_voltage *= (adc_properties->adc_vdd_reference
1230 * 1000);
1231 batt_thm_voltage = div64_s64(batt_thm_voltage,
1232 adc_properties->full_scale_code * 1000);
1233 qpnp_adc_map_voltage_temp(adcmap_batt_therm,
1234 ARRAY_SIZE(adcmap_batt_therm),
1235 batt_thm_voltage, &adc_chan_result->physical);
1236 }
1237 return 0;
1238}
1239EXPORT_SYMBOL(qpnp_adc_batt_therm);
1240
Jishnu Prakash20343d42018-02-12 14:53:33 +05301241int32_t qpnp_adc_batt_therm_qrd(struct qpnp_vadc_chip *chip,
1242 int32_t adc_code,
1243 const struct qpnp_adc_properties *adc_properties,
1244 const struct qpnp_vadc_chan_properties *chan_properties,
1245 struct qpnp_vadc_result *adc_chan_result)
1246{
1247 int64_t batt_thm_voltage = 0;
1248
1249 if (!chan_properties || !chan_properties->offset_gain_numerator ||
1250 !chan_properties->offset_gain_denominator || !adc_properties
1251 || !adc_chan_result)
1252 return -EINVAL;
1253
1254 if (adc_properties->adc_hc) {
1255 /* (code * vref_vadc (1.875V) * 1000) / (scale_code * 1000) */
1256 if (adc_code > QPNP_VADC_HC_MAX_CODE)
1257 adc_code = 0;
1258 batt_thm_voltage = (int64_t) adc_code;
1259 batt_thm_voltage *= (adc_properties->adc_vdd_reference
1260 * 1000);
1261 batt_thm_voltage = div64_s64(batt_thm_voltage,
1262 adc_properties->full_scale_code * 1000);
1263 qpnp_adc_map_voltage_temp(adcmap_batt_therm_qrd,
1264 ARRAY_SIZE(adcmap_batt_therm_qrd),
1265 batt_thm_voltage, &adc_chan_result->physical);
Jishnu Prakash142a3ff2018-10-08 15:28:08 +05301266 } else {
1267
1268 qpnp_adc_scale_with_calib_param(adc_code,
1269 adc_properties, chan_properties, &batt_thm_voltage);
1270
1271 adc_chan_result->measurement = batt_thm_voltage;
1272
1273 return qpnp_adc_map_voltage_temp(
1274 adcmap_batt_therm_qrd,
1275 ARRAY_SIZE(adcmap_batt_therm_qrd),
1276 batt_thm_voltage,
1277 &adc_chan_result->physical);
1278
Jishnu Prakash20343d42018-02-12 14:53:33 +05301279 }
1280 return 0;
1281}
1282EXPORT_SYMBOL(qpnp_adc_batt_therm_qrd);
1283
Jishnu Prakash64c9d3f2018-04-12 17:01:32 +05301284int32_t qpnp_adc_batt_therm_pu30(struct qpnp_vadc_chip *chip,
1285 int32_t adc_code,
1286 const struct qpnp_adc_properties *adc_properties,
1287 const struct qpnp_vadc_chan_properties *chan_properties,
1288 struct qpnp_vadc_result *adc_chan_result)
1289{
1290 int64_t batt_thm_voltage = 0;
1291
1292 if (!chan_properties || !chan_properties->offset_gain_numerator ||
1293 !chan_properties->offset_gain_denominator || !adc_properties
1294 || !adc_chan_result)
1295 return -EINVAL;
1296
1297 /* (code * vref_vadc (1.875V) * 1000) / (scale_code * 1000) */
1298 if (adc_code > QPNP_VADC_HC_MAX_CODE)
1299 adc_code = 0;
1300 batt_thm_voltage = (int64_t) adc_code;
1301 batt_thm_voltage *= (adc_properties->adc_vdd_reference
1302 * 1000);
1303 batt_thm_voltage = div64_s64(batt_thm_voltage,
1304 adc_properties->full_scale_code * 1000);
1305 qpnp_adc_map_voltage_temp(adcmap_batt_therm_pu30,
1306 ARRAY_SIZE(adcmap_batt_therm_pu30),
1307 batt_thm_voltage, &adc_chan_result->physical);
1308 return 0;
1309}
1310EXPORT_SYMBOL(qpnp_adc_batt_therm_pu30);
1311
1312int32_t qpnp_adc_batt_therm_pu400(struct qpnp_vadc_chip *chip,
1313 int32_t adc_code,
1314 const struct qpnp_adc_properties *adc_properties,
1315 const struct qpnp_vadc_chan_properties *chan_properties,
1316 struct qpnp_vadc_result *adc_chan_result)
1317{
1318 int64_t batt_thm_voltage = 0;
1319
1320 if (!chan_properties || !chan_properties->offset_gain_numerator ||
1321 !chan_properties->offset_gain_denominator || !adc_properties
1322 || !adc_chan_result)
1323 return -EINVAL;
1324
1325 /* (code * vref_vadc (1.875V) * 1000) / (scale_code * 1000) */
1326 if (adc_code > QPNP_VADC_HC_MAX_CODE)
1327 adc_code = 0;
1328 batt_thm_voltage = (int64_t) adc_code;
1329 batt_thm_voltage *= (adc_properties->adc_vdd_reference
1330 * 1000);
1331 batt_thm_voltage = div64_s64(batt_thm_voltage,
1332 adc_properties->full_scale_code * 1000);
1333 qpnp_adc_map_voltage_temp(adcmap_batt_therm_pu400,
1334 ARRAY_SIZE(adcmap_batt_therm_pu400),
1335 batt_thm_voltage, &adc_chan_result->physical);
1336 return 0;
1337}
1338EXPORT_SYMBOL(qpnp_adc_batt_therm_pu400);
1339
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001340int32_t qpnp_adc_scale_batt_therm(struct qpnp_vadc_chip *chip,
1341 int32_t adc_code,
1342 const struct qpnp_adc_properties *adc_properties,
1343 const struct qpnp_vadc_chan_properties *chan_properties,
1344 struct qpnp_vadc_result *adc_chan_result)
1345{
1346 int64_t bat_voltage = 0;
1347
1348 qpnp_adc_scale_with_calib_param(adc_code,
1349 adc_properties, chan_properties, &bat_voltage);
1350
1351 adc_chan_result->measurement = bat_voltage;
1352
1353 return qpnp_adc_map_temp_voltage(
1354 adcmap_btm_threshold,
1355 ARRAY_SIZE(adcmap_btm_threshold),
1356 bat_voltage,
1357 &adc_chan_result->physical);
1358}
1359EXPORT_SYMBOL(qpnp_adc_scale_batt_therm);
1360
Jishnu Prakash3b3fe942018-01-08 12:23:39 +05301361int32_t qpnp_adc_scale_chrg_temp(struct qpnp_vadc_chip *vadc,
1362 int32_t adc_code,
1363 const struct qpnp_adc_properties *adc_properties,
1364 const struct qpnp_vadc_chan_properties *chan_properties,
1365 struct qpnp_vadc_result *adc_chan_result)
1366{
1367 int rc = 0;
1368
1369 if (!chan_properties || !chan_properties->offset_gain_numerator ||
1370 !chan_properties->offset_gain_denominator || !adc_properties
1371 || !adc_chan_result)
1372 return -EINVAL;
1373
1374 rc = qpnp_adc_scale_default(vadc, adc_code, adc_properties,
1375 chan_properties, adc_chan_result);
1376 if (rc < 0)
1377 return rc;
1378
1379 pr_debug("raw_code:%x, v_adc:%lld\n", adc_code,
1380 adc_chan_result->physical);
1381 adc_chan_result->physical = (int64_t) ((CHRG_SCALE_1) *
1382 (adc_chan_result->physical));
1383 adc_chan_result->physical = (int64_t) (adc_chan_result->physical +
1384 CHRG_SCALE_2);
1385 adc_chan_result->physical = (int64_t) adc_chan_result->physical;
1386 adc_chan_result->physical = div64_s64(adc_chan_result->physical,
1387 1000000);
1388
1389 return 0;
1390}
1391EXPORT_SYMBOL(qpnp_adc_scale_chrg_temp);
1392
1393int32_t qpnp_adc_scale_die_temp(struct qpnp_vadc_chip *vadc,
1394 int32_t adc_code,
1395 const struct qpnp_adc_properties *adc_properties,
1396 const struct qpnp_vadc_chan_properties *chan_properties,
1397 struct qpnp_vadc_result *adc_chan_result)
1398{
1399 int rc = 0;
1400
1401 if (!chan_properties || !chan_properties->offset_gain_numerator ||
1402 !chan_properties->offset_gain_denominator || !adc_properties
1403 || !adc_chan_result)
1404 return -EINVAL;
1405
1406 rc = qpnp_adc_scale_default(vadc, adc_code, adc_properties,
1407 chan_properties, adc_chan_result);
1408 if (rc < 0)
1409 return rc;
1410
1411 pr_debug("raw_code:%x, v_adc:%lld\n", adc_code,
1412 adc_chan_result->physical);
1413 adc_chan_result->physical = (int64_t) ((DIE_SCALE_1) *
1414 (adc_chan_result->physical));
1415 adc_chan_result->physical = (int64_t) (adc_chan_result->physical +
1416 DIE_SCALE_2);
1417 adc_chan_result->physical = (int64_t) adc_chan_result->physical;
1418 adc_chan_result->physical = div64_s64(adc_chan_result->physical,
1419 1000000);
1420
1421 return 0;
1422}
1423EXPORT_SYMBOL(qpnp_adc_scale_die_temp);
1424
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001425int32_t qpnp_adc_scale_qrd_batt_therm(struct qpnp_vadc_chip *chip,
1426 int32_t adc_code,
1427 const struct qpnp_adc_properties *adc_properties,
1428 const struct qpnp_vadc_chan_properties *chan_properties,
1429 struct qpnp_vadc_result *adc_chan_result)
1430{
1431 int64_t bat_voltage = 0;
1432
1433 qpnp_adc_scale_with_calib_param(adc_code,
1434 adc_properties, chan_properties, &bat_voltage);
1435
1436 adc_chan_result->measurement = bat_voltage;
1437
1438 return qpnp_adc_map_temp_voltage(
1439 adcmap_qrd_btm_threshold,
1440 ARRAY_SIZE(adcmap_qrd_btm_threshold),
1441 bat_voltage,
1442 &adc_chan_result->physical);
1443}
1444EXPORT_SYMBOL(qpnp_adc_scale_qrd_batt_therm);
1445
1446int32_t qpnp_adc_scale_qrd_skuaa_batt_therm(struct qpnp_vadc_chip *chip,
1447 int32_t adc_code,
1448 const struct qpnp_adc_properties *adc_properties,
1449 const struct qpnp_vadc_chan_properties *chan_properties,
1450 struct qpnp_vadc_result *adc_chan_result)
1451{
1452 int64_t bat_voltage = 0;
1453
1454 qpnp_adc_scale_with_calib_param(adc_code,
1455 adc_properties, chan_properties, &bat_voltage);
1456
1457 adc_chan_result->measurement = bat_voltage;
1458
1459 return qpnp_adc_map_temp_voltage(
1460 adcmap_qrd_skuaa_btm_threshold,
1461 ARRAY_SIZE(adcmap_qrd_skuaa_btm_threshold),
1462 bat_voltage,
1463 &adc_chan_result->physical);
1464}
1465EXPORT_SYMBOL(qpnp_adc_scale_qrd_skuaa_batt_therm);
1466
1467int32_t qpnp_adc_scale_qrd_skug_batt_therm(struct qpnp_vadc_chip *chip,
1468 int32_t adc_code,
1469 const struct qpnp_adc_properties *adc_properties,
1470 const struct qpnp_vadc_chan_properties *chan_properties,
1471 struct qpnp_vadc_result *adc_chan_result)
1472{
1473 int64_t bat_voltage = 0;
1474
1475 qpnp_adc_scale_with_calib_param(adc_code,
1476 adc_properties, chan_properties, &bat_voltage);
1477 adc_chan_result->measurement = bat_voltage;
1478
1479 return qpnp_adc_map_temp_voltage(
1480 adcmap_qrd_skug_btm_threshold,
1481 ARRAY_SIZE(adcmap_qrd_skug_btm_threshold),
1482 bat_voltage,
1483 &adc_chan_result->physical);
1484}
1485EXPORT_SYMBOL(qpnp_adc_scale_qrd_skug_batt_therm);
1486
1487int32_t qpnp_adc_scale_qrd_skuh_batt_therm(struct qpnp_vadc_chip *chip,
1488 int32_t adc_code,
1489 const struct qpnp_adc_properties *adc_properties,
1490 const struct qpnp_vadc_chan_properties *chan_properties,
1491 struct qpnp_vadc_result *adc_chan_result)
1492{
1493 int64_t bat_voltage = 0;
1494
1495 qpnp_adc_scale_with_calib_param(adc_code,
1496 adc_properties, chan_properties, &bat_voltage);
1497
1498 return qpnp_adc_map_temp_voltage(
1499 adcmap_qrd_skuh_btm_threshold,
1500 ARRAY_SIZE(adcmap_qrd_skuh_btm_threshold),
1501 bat_voltage,
1502 &adc_chan_result->physical);
1503}
1504EXPORT_SYMBOL(qpnp_adc_scale_qrd_skuh_batt_therm);
1505
1506int32_t qpnp_adc_scale_qrd_skut1_batt_therm(struct qpnp_vadc_chip *chip,
1507 int32_t adc_code,
1508 const struct qpnp_adc_properties *adc_properties,
1509 const struct qpnp_vadc_chan_properties *chan_properties,
1510 struct qpnp_vadc_result *adc_chan_result)
1511{
1512 int64_t bat_voltage = 0;
1513
1514 qpnp_adc_scale_with_calib_param(adc_code,
1515 adc_properties, chan_properties, &bat_voltage);
1516
1517 return qpnp_adc_map_temp_voltage(
1518 adcmap_qrd_skut1_btm_threshold,
1519 ARRAY_SIZE(adcmap_qrd_skut1_btm_threshold),
1520 bat_voltage,
1521 &adc_chan_result->physical);
1522}
1523EXPORT_SYMBOL(qpnp_adc_scale_qrd_skut1_batt_therm);
1524
1525int32_t qpnp_adc_scale_smb_batt_therm(struct qpnp_vadc_chip *chip,
1526 int32_t adc_code,
1527 const struct qpnp_adc_properties *adc_properties,
1528 const struct qpnp_vadc_chan_properties *chan_properties,
1529 struct qpnp_vadc_result *adc_chan_result)
1530{
1531 int64_t bat_voltage = 0;
1532
1533 qpnp_adc_scale_with_calib_param(adc_code,
1534 adc_properties, chan_properties, &bat_voltage);
1535
1536 return qpnp_adc_map_temp_voltage(
1537 adcmap_smb_batt_therm,
1538 ARRAY_SIZE(adcmap_smb_batt_therm),
1539 bat_voltage,
1540 &adc_chan_result->physical);
1541}
1542EXPORT_SYMBOL(qpnp_adc_scale_smb_batt_therm);
1543
1544int32_t qpnp_adc_scale_therm_pu1(struct qpnp_vadc_chip *chip,
1545 int32_t adc_code,
1546 const struct qpnp_adc_properties *adc_properties,
1547 const struct qpnp_vadc_chan_properties *chan_properties,
1548 struct qpnp_vadc_result *adc_chan_result)
1549{
1550 int64_t therm_voltage = 0;
1551
1552 qpnp_adc_scale_with_calib_param(adc_code,
1553 adc_properties, chan_properties, &therm_voltage);
1554
1555 qpnp_adc_map_voltage_temp(adcmap_150k_104ef_104fb,
1556 ARRAY_SIZE(adcmap_150k_104ef_104fb),
1557 therm_voltage, &adc_chan_result->physical);
1558
1559 return 0;
1560}
1561EXPORT_SYMBOL(qpnp_adc_scale_therm_pu1);
1562
1563int32_t qpnp_adc_scale_therm_pu2(struct qpnp_vadc_chip *chip,
1564 int32_t adc_code,
1565 const struct qpnp_adc_properties *adc_properties,
1566 const struct qpnp_vadc_chan_properties *chan_properties,
1567 struct qpnp_vadc_result *adc_chan_result)
1568{
1569 int64_t therm_voltage = 0;
1570
1571 if (!chan_properties || !chan_properties->offset_gain_numerator ||
1572 !chan_properties->offset_gain_denominator || !adc_properties)
1573 return -EINVAL;
1574
1575 if (adc_properties->adc_hc) {
Siddartha Mohanadossd9e74fe2017-10-13 15:27:55 -07001576 /* (code * vref_vadc (1.875V) * 1000) / (scale code * 1000) */
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001577 if (adc_code > QPNP_VADC_HC_MAX_CODE)
1578 adc_code = 0;
1579 therm_voltage = (int64_t) adc_code;
1580 therm_voltage *= (int64_t) (adc_properties->adc_vdd_reference
1581 * 1000);
1582 therm_voltage = div64_s64(therm_voltage,
Siddartha Mohanadossd9e74fe2017-10-13 15:27:55 -07001583 (adc_properties->full_scale_code * 1000));
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001584
1585 qpnp_adc_map_voltage_temp(adcmap_100k_104ef_104fb_1875_vref,
1586 ARRAY_SIZE(adcmap_100k_104ef_104fb_1875_vref),
1587 therm_voltage, &adc_chan_result->physical);
1588 } else {
1589 qpnp_adc_scale_with_calib_param(adc_code,
1590 adc_properties, chan_properties, &therm_voltage);
1591
1592 if (chan_properties->calib_type == CALIB_ABSOLUTE)
Siddartha Mohanadoss04e90272017-10-23 13:36:21 -07001593 therm_voltage = div64_s64(therm_voltage, 1000);
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001594
1595 qpnp_adc_map_voltage_temp(adcmap_100k_104ef_104fb,
1596 ARRAY_SIZE(adcmap_100k_104ef_104fb),
1597 therm_voltage, &adc_chan_result->physical);
1598 }
1599
1600 return 0;
1601}
1602EXPORT_SYMBOL(qpnp_adc_scale_therm_pu2);
1603
1604int32_t qpnp_adc_tm_scale_voltage_therm_pu2(struct qpnp_vadc_chip *chip,
1605 const struct qpnp_adc_properties *adc_properties,
1606 uint32_t reg, int64_t *result)
1607{
1608 int64_t adc_voltage = 0;
1609 struct qpnp_vadc_linear_graph param1;
1610 int negative_offset = 0;
1611
1612 if (adc_properties->adc_hc) {
Siddartha Mohanadossd9e74fe2017-10-13 15:27:55 -07001613 /* (ADC code * vref_vadc (1.875V)) / full_scale_code */
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001614 if (reg > QPNP_VADC_HC_MAX_CODE)
1615 reg = 0;
1616 adc_voltage = (int64_t) reg;
1617 adc_voltage *= QPNP_VADC_HC_VDD_REFERENCE_MV;
1618 adc_voltage = div64_s64(adc_voltage,
Siddartha Mohanadossd9e74fe2017-10-13 15:27:55 -07001619 adc_properties->full_scale_code);
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001620 qpnp_adc_map_voltage_temp(adcmap_100k_104ef_104fb_1875_vref,
1621 ARRAY_SIZE(adcmap_100k_104ef_104fb_1875_vref),
1622 adc_voltage, result);
1623 } else {
1624 qpnp_get_vadc_gain_and_offset(chip, &param1, CALIB_RATIOMETRIC);
1625
1626 adc_voltage = (reg - param1.adc_gnd) * param1.adc_vref;
1627 if (adc_voltage < 0) {
1628 negative_offset = 1;
1629 adc_voltage = -adc_voltage;
1630 }
1631
Siddartha Mohanadoss04e90272017-10-23 13:36:21 -07001632 adc_voltage = div64_s64(adc_voltage, param1.dy);
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001633
1634 qpnp_adc_map_voltage_temp(adcmap_100k_104ef_104fb,
1635 ARRAY_SIZE(adcmap_100k_104ef_104fb),
1636 adc_voltage, result);
1637 if (negative_offset)
1638 adc_voltage = -adc_voltage;
1639 }
1640
1641 return 0;
1642}
1643EXPORT_SYMBOL(qpnp_adc_tm_scale_voltage_therm_pu2);
1644
1645int32_t qpnp_adc_tm_scale_therm_voltage_pu2(struct qpnp_vadc_chip *chip,
1646 const struct qpnp_adc_properties *adc_properties,
1647 struct qpnp_adc_tm_config *param)
1648{
1649 struct qpnp_vadc_linear_graph param1;
1650 int rc;
1651
1652 if (adc_properties->adc_hc) {
1653 rc = qpnp_adc_map_temp_voltage(
1654 adcmap_100k_104ef_104fb_1875_vref,
1655 ARRAY_SIZE(adcmap_100k_104ef_104fb_1875_vref),
1656 param->low_thr_temp, &param->low_thr_voltage);
1657 if (rc)
1658 return rc;
Siddartha Mohanadossd9e74fe2017-10-13 15:27:55 -07001659 param->low_thr_voltage *= adc_properties->full_scale_code;
Siddartha Mohanadoss04e90272017-10-23 13:36:21 -07001660 param->low_thr_voltage = div64_s64(param->low_thr_voltage,
1661 QPNP_VADC_HC_VDD_REFERENCE_MV);
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001662
1663 rc = qpnp_adc_map_temp_voltage(
1664 adcmap_100k_104ef_104fb_1875_vref,
1665 ARRAY_SIZE(adcmap_100k_104ef_104fb_1875_vref),
1666 param->high_thr_temp, &param->high_thr_voltage);
1667 if (rc)
1668 return rc;
Siddartha Mohanadossd9e74fe2017-10-13 15:27:55 -07001669 param->high_thr_voltage *= adc_properties->full_scale_code;
Siddartha Mohanadoss04e90272017-10-23 13:36:21 -07001670 param->high_thr_voltage = div64_s64(param->high_thr_voltage,
1671 QPNP_VADC_HC_VDD_REFERENCE_MV);
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001672 } else {
1673 qpnp_get_vadc_gain_and_offset(chip, &param1, CALIB_RATIOMETRIC);
1674
1675 rc = qpnp_adc_map_temp_voltage(adcmap_100k_104ef_104fb,
1676 ARRAY_SIZE(adcmap_100k_104ef_104fb),
1677 param->low_thr_temp, &param->low_thr_voltage);
1678 if (rc)
1679 return rc;
1680
1681 param->low_thr_voltage *= param1.dy;
Siddartha Mohanadoss04e90272017-10-23 13:36:21 -07001682 param->low_thr_voltage = div64_s64(param->low_thr_voltage,
1683 param1.adc_vref);
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001684 param->low_thr_voltage += param1.adc_gnd;
1685
1686 rc = qpnp_adc_map_temp_voltage(adcmap_100k_104ef_104fb,
1687 ARRAY_SIZE(adcmap_100k_104ef_104fb),
1688 param->high_thr_temp, &param->high_thr_voltage);
1689 if (rc)
1690 return rc;
1691
1692 param->high_thr_voltage *= param1.dy;
Siddartha Mohanadoss04e90272017-10-23 13:36:21 -07001693 param->high_thr_voltage = div64_s64(param->high_thr_voltage,
1694 param1.adc_vref);
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001695 param->high_thr_voltage += param1.adc_gnd;
1696 }
1697
1698 return 0;
1699}
1700EXPORT_SYMBOL(qpnp_adc_tm_scale_therm_voltage_pu2);
1701
1702int32_t qpnp_adc_scale_therm_ncp03(struct qpnp_vadc_chip *chip,
1703 int32_t adc_code,
1704 const struct qpnp_adc_properties *adc_properties,
1705 const struct qpnp_vadc_chan_properties *chan_properties,
1706 struct qpnp_vadc_result *adc_chan_result)
1707{
1708 int64_t therm_voltage = 0;
1709
1710 qpnp_adc_scale_with_calib_param(adc_code,
1711 adc_properties, chan_properties, &therm_voltage);
1712
1713 qpnp_adc_map_voltage_temp(adcmap_ncp03wf683,
1714 ARRAY_SIZE(adcmap_ncp03wf683),
1715 therm_voltage, &adc_chan_result->physical);
1716
1717 return 0;
1718}
1719EXPORT_SYMBOL(qpnp_adc_scale_therm_ncp03);
1720
1721int32_t qpnp_adc_scale_batt_id(struct qpnp_vadc_chip *chip,
1722 int32_t adc_code,
1723 const struct qpnp_adc_properties *adc_properties,
1724 const struct qpnp_vadc_chan_properties *chan_properties,
1725 struct qpnp_vadc_result *adc_chan_result)
1726{
1727 int64_t batt_id_voltage = 0;
1728
1729 qpnp_adc_scale_with_calib_param(adc_code,
1730 adc_properties, chan_properties, &batt_id_voltage);
1731
1732 adc_chan_result->physical = batt_id_voltage;
1733 adc_chan_result->physical = adc_chan_result->measurement;
1734
1735 return 0;
1736}
1737EXPORT_SYMBOL(qpnp_adc_scale_batt_id);
1738
1739int32_t qpnp_adc_scale_default(struct qpnp_vadc_chip *vadc,
1740 int32_t adc_code,
1741 const struct qpnp_adc_properties *adc_properties,
1742 const struct qpnp_vadc_chan_properties *chan_properties,
1743 struct qpnp_vadc_result *adc_chan_result)
1744{
1745 int64_t scale_voltage = 0;
1746
1747 if (!chan_properties || !chan_properties->offset_gain_numerator ||
1748 !chan_properties->offset_gain_denominator || !adc_properties
1749 || !adc_chan_result)
1750 return -EINVAL;
1751
1752 if (adc_properties->adc_hc) {
Siddartha Mohanadossd9e74fe2017-10-13 15:27:55 -07001753 /* (ADC code * vref_vadc (1.875V)) / full_scale_code */
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001754 if (adc_code > QPNP_VADC_HC_MAX_CODE)
1755 adc_code = 0;
1756 scale_voltage = (int64_t) adc_code;
1757 scale_voltage *= (adc_properties->adc_vdd_reference * 1000);
1758 scale_voltage = div64_s64(scale_voltage,
Siddartha Mohanadossd9e74fe2017-10-13 15:27:55 -07001759 adc_properties->full_scale_code);
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001760 } else {
1761 qpnp_adc_scale_with_calib_param(adc_code, adc_properties,
1762 chan_properties, &scale_voltage);
1763 if (!chan_properties->calib_type == CALIB_ABSOLUTE)
1764 scale_voltage *= 1000;
1765 }
1766
1767
1768 scale_voltage *= chan_properties->offset_gain_denominator;
1769 scale_voltage = div64_s64(scale_voltage,
1770 chan_properties->offset_gain_numerator);
1771 adc_chan_result->measurement = scale_voltage;
1772 /*
1773 * Note: adc_chan_result->measurement is in the unit of
1774 * adc_properties.adc_reference. For generic channel processing,
1775 * channel measurement is a scale/ratio relative to the adc
1776 * reference input
1777 */
1778 adc_chan_result->physical = adc_chan_result->measurement;
1779
1780 return 0;
1781}
1782EXPORT_SYMBOL(qpnp_adc_scale_default);
1783
Jishnu Prakash3b3fe942018-01-08 12:23:39 +05301784int32_t qpnp_iadc_scale_default(struct qpnp_vadc_chip *vadc,
1785 int32_t adc_code,
1786 const struct qpnp_adc_properties *adc_properties,
1787 const struct qpnp_vadc_chan_properties *chan_properties,
1788 struct qpnp_vadc_result *adc_chan_result)
1789{
1790 int64_t scale_current = 0;
1791
1792 if (!chan_properties || !chan_properties->offset_gain_numerator ||
1793 !chan_properties->offset_gain_denominator || !adc_properties
1794 || !adc_chan_result)
1795 return -EINVAL;
1796
1797 if (adc_properties->adc_hc) {
1798
1799 if (adc_code == QPNP_IADC_INV)
1800 return -EINVAL;
1801
1802 scale_current = (int64_t) adc_code;
1803
1804 if (adc_code > QPNP_IADC_INV) {
1805 scale_current = ((~scale_current) & IADC_SCALE_1);
1806 scale_current++;
1807 scale_current = -scale_current;
1808 }
1809 }
1810
1811 scale_current *= IADC_SCALE_2;
1812 scale_current = div64_s64(scale_current,
1813 1000);
1814 scale_current *= chan_properties->offset_gain_denominator;
1815 scale_current = div64_s64(scale_current,
1816 chan_properties->offset_gain_numerator);
1817 adc_chan_result->measurement = scale_current;
1818 /*
1819 * Note: adc_chan_result->measurement is in uA.
1820 */
1821 adc_chan_result->physical = adc_chan_result->measurement;
1822
1823 return 0;
1824}
1825EXPORT_SYMBOL(qpnp_iadc_scale_default);
1826
1827int qpnp_adc_scale_usbin_curr(struct qpnp_vadc_chip *vadc,
1828 int32_t adc_code,
1829 const struct qpnp_adc_properties *adc_properties,
1830 const struct qpnp_vadc_chan_properties *chan_properties,
1831 struct qpnp_vadc_result *adc_chan_result)
1832{
1833 int rc = 0;
1834
1835 rc = qpnp_adc_scale_default(vadc, adc_code, adc_properties,
1836 chan_properties, adc_chan_result);
1837 if (rc < 0)
1838 return rc;
1839
1840 pr_debug("raw_code:%x, v_adc:%lld\n", adc_code,
1841 adc_chan_result->physical);
1842 adc_chan_result->physical = (int64_t) ((USBIN_I_SCALE) *
1843 adc_chan_result->physical);
1844 adc_chan_result->physical = div64_s64(adc_chan_result->physical,
1845 10);
1846
1847 return 0;
1848}
1849EXPORT_SYMBOL(qpnp_adc_scale_usbin_curr);
1850
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001851int32_t qpnp_adc_usb_scaler(struct qpnp_vadc_chip *chip,
1852 struct qpnp_adc_tm_btm_param *param,
1853 uint32_t *low_threshold, uint32_t *high_threshold)
1854{
1855 struct qpnp_vadc_linear_graph usb_param;
1856
1857 qpnp_get_vadc_gain_and_offset(chip, &usb_param, CALIB_RATIOMETRIC);
1858
1859 *low_threshold = param->low_thr * usb_param.dy;
Siddartha Mohanadoss04e90272017-10-23 13:36:21 -07001860 *low_threshold = div64_s64(*low_threshold, usb_param.adc_vref);
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001861 *low_threshold += usb_param.adc_gnd;
1862
1863 *high_threshold = param->high_thr * usb_param.dy;
Siddartha Mohanadoss04e90272017-10-23 13:36:21 -07001864 *high_threshold = div64_s64(*high_threshold, usb_param.adc_vref);
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001865 *high_threshold += usb_param.adc_gnd;
1866
1867 pr_debug("high_volt:%d, low_volt:%d\n", param->high_thr,
1868 param->low_thr);
1869 return 0;
1870}
1871EXPORT_SYMBOL(qpnp_adc_usb_scaler);
1872
1873int32_t qpnp_adc_absolute_rthr(struct qpnp_vadc_chip *chip,
1874 struct qpnp_adc_tm_btm_param *param,
1875 uint32_t *low_threshold, uint32_t *high_threshold)
1876{
1877 struct qpnp_vadc_linear_graph vbatt_param;
1878 int rc = 0, sign = 0;
1879 int64_t low_thr = 0, high_thr = 0;
1880
1881 if (param->adc_tm_hc) {
1882 low_thr = (param->low_thr/param->gain_den);
1883 low_thr *= param->gain_num;
Siddartha Mohanadossd9e74fe2017-10-13 15:27:55 -07001884 low_thr *= param->full_scale_code;
Siddartha Mohanadoss04e90272017-10-23 13:36:21 -07001885 low_thr = div64_s64(low_thr,
1886 (QPNP_VADC_HC_VDD_REFERENCE_MV * 1000));
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001887 *low_threshold = low_thr;
1888
1889 high_thr = (param->high_thr/param->gain_den);
1890 high_thr *= param->gain_num;
Siddartha Mohanadossd9e74fe2017-10-13 15:27:55 -07001891 high_thr *= param->full_scale_code;
Siddartha Mohanadoss04e90272017-10-23 13:36:21 -07001892 high_thr = div64_s64(high_thr,
1893 (QPNP_VADC_HC_VDD_REFERENCE_MV * 1000));
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001894 *high_threshold = high_thr;
1895 } else {
1896 rc = qpnp_get_vadc_gain_and_offset(chip, &vbatt_param,
1897 CALIB_ABSOLUTE);
1898 if (rc < 0)
1899 return rc;
1900
1901 low_thr = (((param->low_thr/param->gain_den) -
1902 QPNP_ADC_625_UV) * vbatt_param.dy);
1903 if (low_thr < 0) {
1904 sign = 1;
1905 low_thr = -low_thr;
1906 }
1907 low_thr = low_thr * param->gain_num;
Siddartha Mohanadoss04e90272017-10-23 13:36:21 -07001908 low_thr = div64_s64(low_thr, QPNP_ADC_625_UV);
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001909 if (sign)
1910 low_thr = -low_thr;
1911 *low_threshold = low_thr + vbatt_param.adc_gnd;
1912
1913 sign = 0;
1914 high_thr = (((param->high_thr/param->gain_den) -
1915 QPNP_ADC_625_UV) * vbatt_param.dy);
1916 if (high_thr < 0) {
1917 sign = 1;
1918 high_thr = -high_thr;
1919 }
1920 high_thr = high_thr * param->gain_num;
Siddartha Mohanadoss04e90272017-10-23 13:36:21 -07001921 high_thr = div64_s64(high_thr, QPNP_ADC_625_UV);
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001922 if (sign)
1923 high_thr = -high_thr;
1924 *high_threshold = high_thr + vbatt_param.adc_gnd;
1925 }
1926
1927 pr_debug("high_volt:%d, low_volt:%d\n", param->high_thr,
1928 param->low_thr);
1929 pr_debug("adc_code_high:%x, adc_code_low:%x\n", *high_threshold,
1930 *low_threshold);
1931 return 0;
1932}
1933EXPORT_SYMBOL(qpnp_adc_absolute_rthr);
1934
1935int32_t qpnp_adc_vbatt_rscaler(struct qpnp_vadc_chip *chip,
1936 struct qpnp_adc_tm_btm_param *param,
1937 uint32_t *low_threshold, uint32_t *high_threshold)
1938{
1939 return qpnp_adc_absolute_rthr(chip, param, low_threshold,
1940 high_threshold);
1941}
1942EXPORT_SYMBOL(qpnp_adc_vbatt_rscaler);
1943
1944int32_t qpnp_vadc_absolute_rthr(struct qpnp_vadc_chip *chip,
1945 const struct qpnp_vadc_chan_properties *chan_prop,
1946 struct qpnp_adc_tm_btm_param *param,
1947 uint32_t *low_threshold, uint32_t *high_threshold)
1948{
1949 struct qpnp_vadc_linear_graph vbatt_param;
1950 int rc = 0, sign = 0;
1951 int64_t low_thr = 0, high_thr = 0;
1952
1953 if (!chan_prop || !chan_prop->offset_gain_numerator ||
1954 !chan_prop->offset_gain_denominator)
1955 return -EINVAL;
1956
1957 rc = qpnp_get_vadc_gain_and_offset(chip, &vbatt_param, CALIB_ABSOLUTE);
1958 if (rc < 0)
1959 return rc;
1960
1961 low_thr = (((param->low_thr)/(int)chan_prop->offset_gain_denominator
1962 - QPNP_ADC_625_UV) * vbatt_param.dy);
1963 if (low_thr < 0) {
1964 sign = 1;
1965 low_thr = -low_thr;
1966 }
1967 low_thr = low_thr * chan_prop->offset_gain_numerator;
Siddartha Mohanadoss04e90272017-10-23 13:36:21 -07001968 low_thr = div64_s64(low_thr, QPNP_ADC_625_UV);
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001969 if (sign)
1970 low_thr = -low_thr;
1971 *low_threshold = low_thr + vbatt_param.adc_gnd;
1972
1973 sign = 0;
1974 high_thr = (((param->high_thr)/(int)chan_prop->offset_gain_denominator
1975 - QPNP_ADC_625_UV) * vbatt_param.dy);
1976 if (high_thr < 0) {
1977 sign = 1;
1978 high_thr = -high_thr;
1979 }
1980 high_thr = high_thr * chan_prop->offset_gain_numerator;
Siddartha Mohanadoss04e90272017-10-23 13:36:21 -07001981 high_thr = div64_s64(high_thr, QPNP_ADC_625_UV);
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001982 if (sign)
1983 high_thr = -high_thr;
1984 *high_threshold = high_thr + vbatt_param.adc_gnd;
1985
1986 pr_debug("high_volt:%d, low_volt:%d\n", param->high_thr,
1987 param->low_thr);
1988 pr_debug("adc_code_high:%x, adc_code_low:%x\n", *high_threshold,
1989 *low_threshold);
1990 return 0;
1991}
1992EXPORT_SYMBOL(qpnp_vadc_absolute_rthr);
1993
1994int32_t qpnp_adc_btm_scaler(struct qpnp_vadc_chip *chip,
1995 struct qpnp_adc_tm_btm_param *param,
1996 uint32_t *low_threshold, uint32_t *high_threshold)
1997{
1998 struct qpnp_vadc_linear_graph btm_param;
1999 int64_t low_output = 0, high_output = 0;
2000 int rc = 0;
2001
2002 if (param->adc_tm_hc) {
2003 pr_err("Update scaling for VADC_TM_HC\n");
2004 return -EINVAL;
2005 }
2006
2007 qpnp_get_vadc_gain_and_offset(chip, &btm_param, CALIB_RATIOMETRIC);
2008
2009 pr_debug("warm_temp:%d and cool_temp:%d\n", param->high_temp,
2010 param->low_temp);
2011 rc = qpnp_adc_map_voltage_temp(
2012 adcmap_btm_threshold,
2013 ARRAY_SIZE(adcmap_btm_threshold),
2014 (param->low_temp),
2015 &low_output);
2016 if (rc) {
2017 pr_debug("low_temp mapping failed with %d\n", rc);
2018 return rc;
2019 }
2020
2021 pr_debug("low_output:%lld\n", low_output);
2022 low_output *= btm_param.dy;
Siddartha Mohanadoss04e90272017-10-23 13:36:21 -07002023 low_output = div64_s64(low_output, btm_param.adc_vref);
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08002024 low_output += btm_param.adc_gnd;
2025
2026 rc = qpnp_adc_map_voltage_temp(
2027 adcmap_btm_threshold,
2028 ARRAY_SIZE(adcmap_btm_threshold),
2029 (param->high_temp),
2030 &high_output);
2031 if (rc) {
2032 pr_debug("high temp mapping failed with %d\n", rc);
2033 return rc;
2034 }
2035
2036 pr_debug("high_output:%lld\n", high_output);
2037 high_output *= btm_param.dy;
Siddartha Mohanadoss04e90272017-10-23 13:36:21 -07002038 high_output = div64_s64(high_output, btm_param.adc_vref);
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08002039 high_output += btm_param.adc_gnd;
2040
2041 /* btm low temperature correspondes to high voltage threshold */
2042 *low_threshold = high_output;
2043 /* btm high temperature correspondes to low voltage threshold */
2044 *high_threshold = low_output;
2045
2046 pr_debug("high_volt:%d, low_volt:%d\n", *high_threshold,
2047 *low_threshold);
2048 return 0;
2049}
2050EXPORT_SYMBOL(qpnp_adc_btm_scaler);
2051
2052int32_t qpnp_adc_qrd_skuh_btm_scaler(struct qpnp_vadc_chip *chip,
2053 struct qpnp_adc_tm_btm_param *param,
2054 uint32_t *low_threshold, uint32_t *high_threshold)
2055{
2056 struct qpnp_vadc_linear_graph btm_param;
2057 int64_t low_output = 0, high_output = 0;
2058 int rc = 0;
2059
2060 if (param->adc_tm_hc) {
2061 pr_err("Update scaling for VADC_TM_HC\n");
2062 return -EINVAL;
2063 }
2064
2065 qpnp_get_vadc_gain_and_offset(chip, &btm_param, CALIB_RATIOMETRIC);
2066
2067 pr_debug("warm_temp:%d and cool_temp:%d\n", param->high_temp,
2068 param->low_temp);
2069 rc = qpnp_adc_map_voltage_temp(
2070 adcmap_qrd_skuh_btm_threshold,
2071 ARRAY_SIZE(adcmap_qrd_skuh_btm_threshold),
2072 (param->low_temp),
2073 &low_output);
2074 if (rc) {
2075 pr_debug("low_temp mapping failed with %d\n", rc);
2076 return rc;
2077 }
2078
2079 pr_debug("low_output:%lld\n", low_output);
2080 low_output *= btm_param.dy;
Siddartha Mohanadoss04e90272017-10-23 13:36:21 -07002081 low_output = div64_s64(low_output, btm_param.adc_vref);
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08002082 low_output += btm_param.adc_gnd;
2083
2084 rc = qpnp_adc_map_voltage_temp(
2085 adcmap_qrd_skuh_btm_threshold,
2086 ARRAY_SIZE(adcmap_qrd_skuh_btm_threshold),
2087 (param->high_temp),
2088 &high_output);
2089 if (rc) {
2090 pr_debug("high temp mapping failed with %d\n", rc);
2091 return rc;
2092 }
2093
2094 pr_debug("high_output:%lld\n", high_output);
2095 high_output *= btm_param.dy;
Siddartha Mohanadoss04e90272017-10-23 13:36:21 -07002096 high_output = div64_s64(high_output, btm_param.adc_vref);
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08002097 high_output += btm_param.adc_gnd;
2098
2099 /* btm low temperature correspondes to high voltage threshold */
2100 *low_threshold = high_output;
2101 /* btm high temperature correspondes to low voltage threshold */
2102 *high_threshold = low_output;
2103
2104 pr_debug("high_volt:%d, low_volt:%d\n", *high_threshold,
2105 *low_threshold);
2106 return 0;
2107}
2108EXPORT_SYMBOL(qpnp_adc_qrd_skuh_btm_scaler);
2109
2110int32_t qpnp_adc_qrd_skut1_btm_scaler(struct qpnp_vadc_chip *chip,
2111 struct qpnp_adc_tm_btm_param *param,
2112 uint32_t *low_threshold, uint32_t *high_threshold)
2113{
2114 struct qpnp_vadc_linear_graph btm_param;
2115 int64_t low_output = 0, high_output = 0;
2116 int rc = 0;
2117
2118 if (param->adc_tm_hc) {
2119 pr_err("Update scaling for VADC_TM_HC\n");
2120 return -EINVAL;
2121 }
2122
2123 qpnp_get_vadc_gain_and_offset(chip, &btm_param, CALIB_RATIOMETRIC);
2124
2125 pr_debug("warm_temp:%d and cool_temp:%d\n", param->high_temp,
2126 param->low_temp);
2127 rc = qpnp_adc_map_voltage_temp(
2128 adcmap_qrd_skut1_btm_threshold,
2129 ARRAY_SIZE(adcmap_qrd_skut1_btm_threshold),
2130 (param->low_temp),
2131 &low_output);
2132 if (rc) {
2133 pr_debug("low_temp mapping failed with %d\n", rc);
2134 return rc;
2135 }
2136
2137 pr_debug("low_output:%lld\n", low_output);
2138 low_output *= btm_param.dy;
Siddartha Mohanadoss04e90272017-10-23 13:36:21 -07002139 low_output = div64_s64(low_output, btm_param.adc_vref);
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08002140 low_output += btm_param.adc_gnd;
2141
2142 rc = qpnp_adc_map_voltage_temp(
2143 adcmap_qrd_skut1_btm_threshold,
2144 ARRAY_SIZE(adcmap_qrd_skut1_btm_threshold),
2145 (param->high_temp),
2146 &high_output);
2147 if (rc) {
2148 pr_debug("high temp mapping failed with %d\n", rc);
2149 return rc;
2150 }
2151
2152 pr_debug("high_output:%lld\n", high_output);
2153 high_output *= btm_param.dy;
Siddartha Mohanadoss04e90272017-10-23 13:36:21 -07002154 high_output = div64_s64(high_output, btm_param.adc_vref);
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08002155 high_output += btm_param.adc_gnd;
2156
2157 /* btm low temperature correspondes to high voltage threshold */
2158 *low_threshold = high_output;
2159 /* btm high temperature correspondes to low voltage threshold */
2160 *high_threshold = low_output;
2161
2162 pr_debug("high_volt:%d, low_volt:%d\n", *high_threshold,
2163 *low_threshold);
2164 return 0;
2165}
2166EXPORT_SYMBOL(qpnp_adc_qrd_skut1_btm_scaler);
2167
Jishnu Prakash142a3ff2018-10-08 15:28:08 +05302168int32_t qpnp_adc_qrd_215_btm_scaler(struct qpnp_vadc_chip *chip,
2169 struct qpnp_adc_tm_btm_param *param,
2170 uint32_t *low_threshold, uint32_t *high_threshold)
2171{
2172 struct qpnp_vadc_linear_graph btm_param;
2173 int64_t low_output = 0, high_output = 0;
2174 int rc = 0;
2175
2176 if (param->adc_tm_hc) {
2177 pr_debug("Update scaling for VADC_TM_HC\n");
2178 return -EINVAL;
2179 }
2180
2181 qpnp_get_vadc_gain_and_offset(chip, &btm_param, CALIB_RATIOMETRIC);
2182
2183 pr_debug("warm_temp:%d and cool_temp:%d\n", param->high_temp,
2184 param->low_temp);
2185 rc = qpnp_adc_map_voltage_temp(
2186 adcmap_batt_therm_qrd,
2187 ARRAY_SIZE(adcmap_batt_therm_qrd),
2188 (param->low_temp),
2189 &low_output);
2190 if (rc) {
2191 pr_debug("low_temp mapping failed with %d\n", rc);
2192 return rc;
2193 }
2194
2195 pr_debug("low_output:%lld\n", low_output);
2196 low_output *= btm_param.dy;
2197 low_output = div64_s64(low_output, btm_param.adc_vref);
2198 low_output += btm_param.adc_gnd;
2199
2200 rc = qpnp_adc_map_voltage_temp(
2201 adcmap_batt_therm_qrd,
2202 ARRAY_SIZE(adcmap_batt_therm_qrd),
2203 (param->high_temp),
2204 &high_output);
2205 if (rc) {
2206 pr_debug("high temp mapping failed with %d\n", rc);
2207 return rc;
2208 }
2209
2210 pr_debug("high_output:%lld\n", high_output);
2211 high_output *= btm_param.dy;
2212 high_output = div64_s64(high_output, btm_param.adc_vref);
2213 high_output += btm_param.adc_gnd;
2214
2215 /* btm low temperature correspondes to high voltage threshold */
2216 *low_threshold = high_output;
2217 /* btm high temperature correspondes to low voltage threshold */
2218 *high_threshold = low_output;
2219
2220 pr_debug("high_volt:%d, low_volt:%d\n", *high_threshold,
2221 *low_threshold);
2222 return 0;
2223}
2224EXPORT_SYMBOL(qpnp_adc_qrd_215_btm_scaler);
2225
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08002226int32_t qpnp_adc_smb_btm_rscaler(struct qpnp_vadc_chip *chip,
2227 struct qpnp_adc_tm_btm_param *param,
2228 uint32_t *low_threshold, uint32_t *high_threshold)
2229{
2230 struct qpnp_vadc_linear_graph btm_param;
2231 int64_t low_output = 0, high_output = 0;
2232 int rc = 0;
2233
2234 if (param->adc_tm_hc) {
2235 pr_err("Update scaling for VADC_TM_HC\n");
2236 return -EINVAL;
2237 }
2238
2239 qpnp_get_vadc_gain_and_offset(chip, &btm_param, CALIB_RATIOMETRIC);
2240
2241 pr_debug("warm_temp:%d and cool_temp:%d\n", param->high_temp,
2242 param->low_temp);
2243 rc = qpnp_adc_map_voltage_temp(
2244 adcmap_smb_batt_therm,
2245 ARRAY_SIZE(adcmap_smb_batt_therm),
2246 (param->low_temp),
2247 &low_output);
2248 if (rc) {
2249 pr_debug("low_temp mapping failed with %d\n", rc);
2250 return rc;
2251 }
2252
2253 pr_debug("low_output:%lld\n", low_output);
2254 low_output *= btm_param.dy;
Siddartha Mohanadoss04e90272017-10-23 13:36:21 -07002255 low_output = div64_s64(low_output, btm_param.adc_vref);
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08002256 low_output += btm_param.adc_gnd;
2257
2258 rc = qpnp_adc_map_voltage_temp(
2259 adcmap_smb_batt_therm,
2260 ARRAY_SIZE(adcmap_smb_batt_therm),
2261 (param->high_temp),
2262 &high_output);
2263 if (rc) {
2264 pr_debug("high temp mapping failed with %d\n", rc);
2265 return rc;
2266 }
2267
2268 pr_debug("high_output:%lld\n", high_output);
2269 high_output *= btm_param.dy;
Siddartha Mohanadoss04e90272017-10-23 13:36:21 -07002270 high_output = div64_s64(high_output, btm_param.adc_vref);
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08002271 high_output += btm_param.adc_gnd;
2272
2273 /* btm low temperature correspondes to high voltage threshold */
2274 *low_threshold = high_output;
2275 /* btm high temperature correspondes to low voltage threshold */
2276 *high_threshold = low_output;
2277
2278 pr_debug("high_volt:%d, low_volt:%d\n", *high_threshold,
2279 *low_threshold);
2280 return 0;
2281}
2282EXPORT_SYMBOL(qpnp_adc_smb_btm_rscaler);
2283
2284int32_t qpnp_adc_scale_pmi_chg_temp(struct qpnp_vadc_chip *vadc,
2285 int32_t adc_code,
2286 const struct qpnp_adc_properties *adc_properties,
2287 const struct qpnp_vadc_chan_properties *chan_properties,
2288 struct qpnp_vadc_result *adc_chan_result)
2289{
2290 int rc = 0;
2291
2292 rc = qpnp_adc_scale_default(vadc, adc_code, adc_properties,
2293 chan_properties, adc_chan_result);
2294 if (rc < 0)
2295 return rc;
2296
2297 pr_debug("raw_code:%x, v_adc:%lld\n", adc_code,
2298 adc_chan_result->physical);
2299 adc_chan_result->physical = (int64_t) ((PMI_CHG_SCALE_1) *
2300 (adc_chan_result->physical * 2));
2301 adc_chan_result->physical = (int64_t) (adc_chan_result->physical +
2302 PMI_CHG_SCALE_2);
2303 adc_chan_result->physical = (int64_t) adc_chan_result->physical;
2304 adc_chan_result->physical = div64_s64(adc_chan_result->physical,
2305 1000000);
2306
2307 return 0;
2308}
2309EXPORT_SYMBOL(qpnp_adc_scale_pmi_chg_temp);
2310
Jishnu Prakash62aff112017-09-15 15:06:59 +05302311int32_t qpnp_adc_scale_die_temp_1390(struct qpnp_vadc_chip *chip,
2312 int32_t adc_code,
2313 const struct qpnp_adc_properties *adc_properties,
2314 const struct qpnp_vadc_chan_properties *chan_properties,
2315 struct qpnp_vadc_result *adc_chan_result)
2316{
2317 int rc = 0;
2318
2319 if (!chan_properties || !chan_properties->offset_gain_numerator ||
2320 !chan_properties->offset_gain_denominator || !adc_properties
2321 || !adc_chan_result)
2322 return -EINVAL;
2323
2324 rc = qpnp_adc_scale_default(chip, adc_code, adc_properties,
2325 chan_properties, adc_chan_result);
2326 if (rc < 0)
2327 return rc;
2328
2329 pr_debug("raw_code:%x, v_adc:%lld\n", adc_code,
2330 adc_chan_result->physical);
2331 /* T = (1.49322 – V) / 0.00356 */
2332 adc_chan_result->physical = 1493220 - adc_chan_result->physical;
2333 adc_chan_result->physical = div64_s64(adc_chan_result->physical, 356);
2334
2335 return 0;
2336}
2337EXPORT_SYMBOL(qpnp_adc_scale_die_temp_1390);
2338
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08002339int32_t qpnp_adc_enable_voltage(struct qpnp_adc_drv *adc)
2340{
2341 int rc = 0;
2342
2343 if (adc->hkadc_ldo) {
2344 rc = regulator_enable(adc->hkadc_ldo);
2345 if (rc < 0) {
2346 pr_err("Failed to enable hkadc ldo\n");
2347 return rc;
2348 }
2349 }
2350
2351 if (adc->hkadc_ldo_ok) {
2352 rc = regulator_enable(adc->hkadc_ldo_ok);
2353 if (rc < 0) {
2354 pr_err("Failed to enable hkadc ok signal\n");
2355 return rc;
2356 }
2357 }
2358
2359 return rc;
2360}
2361EXPORT_SYMBOL(qpnp_adc_enable_voltage);
2362
2363void qpnp_adc_disable_voltage(struct qpnp_adc_drv *adc)
2364{
2365 if (adc->hkadc_ldo)
2366 regulator_disable(adc->hkadc_ldo);
2367
2368 if (adc->hkadc_ldo_ok)
2369 regulator_disable(adc->hkadc_ldo_ok);
2370
2371}
2372EXPORT_SYMBOL(qpnp_adc_disable_voltage);
2373
2374void qpnp_adc_free_voltage_resource(struct qpnp_adc_drv *adc)
2375{
2376 if (adc->hkadc_ldo)
2377 regulator_put(adc->hkadc_ldo);
2378
2379 if (adc->hkadc_ldo_ok)
2380 regulator_put(adc->hkadc_ldo_ok);
2381}
2382EXPORT_SYMBOL(qpnp_adc_free_voltage_resource);
2383
2384int qpnp_adc_get_revid_version(struct device *dev)
2385{
2386 struct pmic_revid_data *revid_data;
2387 struct device_node *revid_dev_node;
2388
2389 revid_dev_node = of_parse_phandle(dev->of_node,
2390 "qcom,pmic-revid", 0);
2391 if (!revid_dev_node) {
2392 pr_debug("Missing qcom,pmic-revid property\n");
2393 return -EINVAL;
2394 }
2395
2396 revid_data = get_revid_data(revid_dev_node);
2397 if (IS_ERR(revid_data)) {
2398 pr_debug("revid error rc = %ld\n", PTR_ERR(revid_data));
2399 return -EINVAL;
2400 }
2401
2402 if (!revid_data)
2403 return -EINVAL;
2404
2405 if ((revid_data->rev1 == PM8941_V3P1_REV1) &&
2406 (revid_data->rev2 == PM8941_V3P1_REV2) &&
2407 (revid_data->rev3 == PM8941_V3P1_REV3) &&
2408 (revid_data->rev4 == PM8941_V3P1_REV4) &&
2409 (revid_data->pmic_subtype == PM8941_SUBTYPE))
2410 return QPNP_REV_ID_8941_3_1;
2411 else if ((revid_data->rev1 == PM8941_V3P0_REV1) &&
2412 (revid_data->rev2 == PM8941_V3P0_REV2) &&
2413 (revid_data->rev3 == PM8941_V3P0_REV3) &&
2414 (revid_data->rev4 == PM8941_V3P0_REV4) &&
2415 (revid_data->pmic_subtype == PM8941_SUBTYPE))
2416 return QPNP_REV_ID_8941_3_0;
2417 else if ((revid_data->rev1 == PM8941_V2P0_REV1) &&
2418 (revid_data->rev2 == PM8941_V2P0_REV2) &&
2419 (revid_data->rev3 == PM8941_V2P0_REV3) &&
2420 (revid_data->rev4 == PM8941_V2P0_REV4) &&
2421 (revid_data->pmic_subtype == PM8941_SUBTYPE))
2422 return QPNP_REV_ID_8941_2_0;
2423 else if ((revid_data->rev1 == PM8226_V2P2_REV1) &&
2424 (revid_data->rev2 == PM8226_V2P2_REV2) &&
2425 (revid_data->rev3 == PM8226_V2P2_REV3) &&
2426 (revid_data->rev4 == PM8226_V2P2_REV4) &&
2427 (revid_data->pmic_subtype == PM8226_SUBTYPE))
2428 return QPNP_REV_ID_8026_2_2;
2429 else if ((revid_data->rev1 == PM8226_V2P1_REV1) &&
2430 (revid_data->rev2 == PM8226_V2P1_REV2) &&
2431 (revid_data->rev3 == PM8226_V2P1_REV3) &&
2432 (revid_data->rev4 == PM8226_V2P1_REV4) &&
2433 (revid_data->pmic_subtype == PM8226_SUBTYPE))
2434 return QPNP_REV_ID_8026_2_1;
2435 else if ((revid_data->rev1 == PM8226_V2P0_REV1) &&
2436 (revid_data->rev2 == PM8226_V2P0_REV2) &&
2437 (revid_data->rev3 == PM8226_V2P0_REV3) &&
2438 (revid_data->rev4 == PM8226_V2P0_REV4) &&
2439 (revid_data->pmic_subtype == PM8226_SUBTYPE))
2440 return QPNP_REV_ID_8026_2_0;
2441 else if ((revid_data->rev1 == PM8226_V1P0_REV1) &&
2442 (revid_data->rev2 == PM8226_V1P0_REV2) &&
2443 (revid_data->rev3 == PM8226_V1P0_REV3) &&
2444 (revid_data->rev4 == PM8226_V1P0_REV4) &&
2445 (revid_data->pmic_subtype == PM8226_SUBTYPE))
2446 return QPNP_REV_ID_8026_1_0;
2447 else if ((revid_data->rev1 == PM8110_V1P0_REV1) &&
2448 (revid_data->rev2 == PM8110_V1P0_REV2) &&
2449 (revid_data->rev3 == PM8110_V1P0_REV3) &&
2450 (revid_data->rev4 == PM8110_V1P0_REV4) &&
2451 (revid_data->pmic_subtype == PM8110_SUBTYPE))
2452 return QPNP_REV_ID_8110_1_0;
2453 else if ((revid_data->rev1 == PM8110_V2P0_REV1) &&
2454 (revid_data->rev2 == PM8110_V2P0_REV2) &&
2455 (revid_data->rev3 == PM8110_V2P0_REV3) &&
2456 (revid_data->rev4 == PM8110_V2P0_REV4) &&
2457 (revid_data->pmic_subtype == PM8110_SUBTYPE))
2458 return QPNP_REV_ID_8110_2_0;
2459 else if ((revid_data->rev1 == PM8916_V1P0_REV1) &&
2460 (revid_data->rev2 == PM8916_V1P0_REV2) &&
2461 (revid_data->rev3 == PM8916_V1P0_REV3) &&
2462 (revid_data->rev4 == PM8916_V1P0_REV4) &&
2463 (revid_data->pmic_subtype == PM8916_SUBTYPE))
2464 return QPNP_REV_ID_8916_1_0;
2465 else if ((revid_data->rev1 == PM8916_V1P1_REV1) &&
2466 (revid_data->rev2 == PM8916_V1P1_REV2) &&
2467 (revid_data->rev3 == PM8916_V1P1_REV3) &&
2468 (revid_data->rev4 == PM8916_V1P1_REV4) &&
2469 (revid_data->pmic_subtype == PM8916_SUBTYPE))
2470 return QPNP_REV_ID_8916_1_1;
2471 else if ((revid_data->rev1 == PM8916_V2P0_REV1) &&
2472 (revid_data->rev2 == PM8916_V2P0_REV2) &&
2473 (revid_data->rev3 == PM8916_V2P0_REV3) &&
2474 (revid_data->rev4 == PM8916_V2P0_REV4) &&
2475 (revid_data->pmic_subtype == PM8916_SUBTYPE))
2476 return QPNP_REV_ID_8916_2_0;
2477 else if ((revid_data->rev1 == PM8909_V1P0_REV1) &&
2478 (revid_data->rev2 == PM8909_V1P0_REV2) &&
2479 (revid_data->rev3 == PM8909_V1P0_REV3) &&
2480 (revid_data->rev4 == PM8909_V1P0_REV4) &&
2481 (revid_data->pmic_subtype == PM8909_SUBTYPE))
2482 return QPNP_REV_ID_8909_1_0;
2483 else if ((revid_data->rev1 == PM8909_V1P1_REV1) &&
2484 (revid_data->rev2 == PM8909_V1P1_REV2) &&
2485 (revid_data->rev3 == PM8909_V1P1_REV3) &&
2486 (revid_data->rev4 == PM8909_V1P1_REV4) &&
2487 (revid_data->pmic_subtype == PM8909_SUBTYPE))
2488 return QPNP_REV_ID_8909_1_1;
2489 else if ((revid_data->rev4 == PM8950_V1P0_REV4) &&
2490 (revid_data->pmic_subtype == PM8950_SUBTYPE))
2491 return QPNP_REV_ID_PM8950_1_0;
2492 else
2493 return -EINVAL;
2494}
2495EXPORT_SYMBOL(qpnp_adc_get_revid_version);
2496
2497int32_t qpnp_adc_get_devicetree_data(struct platform_device *pdev,
2498 struct qpnp_adc_drv *adc_qpnp)
2499{
2500 struct device_node *node = pdev->dev.of_node;
2501 unsigned int base;
2502 struct device_node *child;
2503 struct qpnp_adc_amux *adc_channel_list;
2504 struct qpnp_adc_properties *adc_prop;
2505 struct qpnp_adc_amux_properties *amux_prop;
2506 int count_adc_channel_list = 0, decimation = 0, rc = 0, i = 0;
2507 int decimation_tm_hc = 0, fast_avg_setup_tm_hc = 0, cal_val_hc = 0;
2508 bool adc_hc;
2509
2510 if (!node)
2511 return -EINVAL;
2512
2513 for_each_child_of_node(node, child)
2514 count_adc_channel_list++;
2515
2516 if (!count_adc_channel_list) {
2517 pr_err("No channel listing\n");
2518 return -EINVAL;
2519 }
2520
2521 adc_qpnp->pdev = pdev;
2522
2523 adc_prop = devm_kzalloc(&pdev->dev,
2524 sizeof(struct qpnp_adc_properties),
2525 GFP_KERNEL);
2526 if (!adc_prop)
2527 return -ENOMEM;
2528
2529 adc_channel_list = devm_kzalloc(&pdev->dev,
2530 ((sizeof(struct qpnp_adc_amux)) * count_adc_channel_list),
2531 GFP_KERNEL);
2532 if (!adc_channel_list)
2533 return -ENOMEM;
2534
2535 amux_prop = devm_kzalloc(&pdev->dev,
2536 sizeof(struct qpnp_adc_amux_properties) +
2537 sizeof(struct qpnp_vadc_chan_properties), GFP_KERNEL);
2538 if (!amux_prop) {
2539 dev_err(&pdev->dev, "Unable to allocate memory\n");
2540 return -ENOMEM;
2541 }
2542
2543 adc_qpnp->adc_channels = adc_channel_list;
2544 adc_qpnp->amux_prop = amux_prop;
2545 adc_hc = adc_qpnp->adc_hc;
2546 adc_prop->adc_hc = adc_hc;
2547
2548 if (of_device_is_compatible(node, "qcom,qpnp-adc-tm-hc")) {
2549 rc = of_property_read_u32(node, "qcom,decimation",
2550 &decimation_tm_hc);
2551 if (rc) {
2552 pr_err("Invalid decimation property\n");
2553 return -EINVAL;
2554 }
2555
2556 rc = of_property_read_u32(node,
2557 "qcom,fast-avg-setup", &fast_avg_setup_tm_hc);
2558 if (rc) {
2559 pr_err("Invalid fast average setup with %d\n", rc);
2560 return -EINVAL;
2561 }
2562
2563 if ((fast_avg_setup_tm_hc) > ADC_FAST_AVG_SAMPLE_16) {
2564 pr_err("Max average support is 2^16\n");
2565 return -EINVAL;
2566 }
2567 }
2568
Jishnu Prakashd09bc692018-05-02 10:54:28 +05302569 if (of_device_is_compatible(node, "qcom,qpnp-adc-hc-pm5") ||
2570 of_device_is_compatible(node, "qcom,qpnp-adc-tm-hc-pm5"))
2571 adc_prop->is_pmic_5 = true;
2572 else
2573 adc_prop->is_pmic_5 = false;
2574
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08002575 for_each_child_of_node(node, child) {
2576 int channel_num, scaling = 0, post_scaling = 0;
2577 int fast_avg_setup, calib_type = 0, rc, hw_settle_time = 0;
2578 const char *calibration_param, *channel_name;
2579
2580 channel_name = of_get_property(child,
2581 "label", NULL) ? : child->name;
2582 if (!channel_name) {
2583 pr_err("Invalid channel name\n");
2584 return -EINVAL;
2585 }
2586
2587 rc = of_property_read_u32(child, "reg", &channel_num);
2588 if (rc) {
2589 pr_err("Invalid channel num\n");
2590 return -EINVAL;
2591 }
2592
2593 if (!of_device_is_compatible(node, "qcom,qpnp-iadc")) {
2594 rc = of_property_read_u32(child,
2595 "qcom,hw-settle-time", &hw_settle_time);
2596 if (rc) {
2597 pr_err("Invalid channel hw settle time property\n");
2598 return -EINVAL;
2599 }
2600 rc = of_property_read_u32(child,
2601 "qcom,pre-div-channel-scaling", &scaling);
2602 if (rc) {
2603 pr_err("Invalid channel scaling property\n");
2604 return -EINVAL;
2605 }
2606 rc = of_property_read_u32(child,
2607 "qcom,scale-function", &post_scaling);
2608 if (rc) {
2609 pr_err("Invalid channel post scaling property\n");
2610 return -EINVAL;
2611 }
2612 rc = of_property_read_string(child,
2613 "qcom,calibration-type", &calibration_param);
2614 if (rc) {
2615 pr_err("Invalid calibration type\n");
2616 return -EINVAL;
2617 }
2618
2619 if (!strcmp(calibration_param, "absolute")) {
2620 if (adc_hc)
2621 calib_type = ADC_HC_ABS_CAL;
2622 else
2623 calib_type = CALIB_ABSOLUTE;
2624 } else if (!strcmp(calibration_param, "ratiometric")) {
2625 if (adc_hc)
2626 calib_type = ADC_HC_RATIO_CAL;
2627 else
2628 calib_type = CALIB_RATIOMETRIC;
2629 } else if (!strcmp(calibration_param, "no_cal")) {
2630 if (adc_hc)
2631 calib_type = ADC_HC_NO_CAL;
2632 else {
2633 pr_err("%s: Invalid calibration property\n",
2634 __func__);
2635 return -EINVAL;
2636 }
2637 } else {
2638 pr_err("%s: Invalid calibration property\n",
2639 __func__);
2640 return -EINVAL;
2641 }
2642 }
2643
2644 /* ADC_TM_HC fast avg setting is common across channels */
2645 if (!of_device_is_compatible(node, "qcom,qpnp-adc-tm-hc")) {
2646 rc = of_property_read_u32(child,
2647 "qcom,fast-avg-setup", &fast_avg_setup);
2648 if (rc) {
2649 pr_err("Invalid channel fast average setup\n");
2650 return -EINVAL;
2651 }
2652 } else {
2653 fast_avg_setup = fast_avg_setup_tm_hc;
2654 }
2655
2656 /* ADC_TM_HC decimation setting is common across channels */
2657 if (!of_device_is_compatible(node, "qcom,qpnp-adc-tm-hc")) {
2658 rc = of_property_read_u32(child,
2659 "qcom,decimation", &decimation);
2660 if (rc) {
2661 pr_err("Invalid decimation\n");
2662 return -EINVAL;
2663 }
2664 } else {
2665 decimation = decimation_tm_hc;
2666 }
2667
2668 if (of_device_is_compatible(node, "qcom,qpnp-vadc-hc")) {
2669 rc = of_property_read_u32(child, "qcom,cal-val",
2670 &cal_val_hc);
2671 if (rc) {
2672 pr_debug("Use calibration value from timer\n");
2673 adc_channel_list[i].cal_val = ADC_TIMER_CAL;
2674 } else {
2675 adc_channel_list[i].cal_val = cal_val_hc;
2676 }
2677 }
2678
2679 /* Individual channel properties */
2680 adc_channel_list[i].name = (char *)channel_name;
2681 adc_channel_list[i].channel_num = channel_num;
2682 adc_channel_list[i].adc_decimation = decimation;
2683 adc_channel_list[i].fast_avg_setup = fast_avg_setup;
2684 if (!of_device_is_compatible(node, "qcom,qpnp-iadc")) {
2685 adc_channel_list[i].chan_path_prescaling = scaling;
2686 adc_channel_list[i].adc_scale_fn = post_scaling;
2687 adc_channel_list[i].hw_settle_time = hw_settle_time;
2688 adc_channel_list[i].calib_type = calib_type;
2689 }
2690 i++;
2691 }
2692
2693 /* Get the ADC VDD reference voltage and ADC bit resolution */
2694 rc = of_property_read_u32(node, "qcom,adc-vdd-reference",
2695 &adc_prop->adc_vdd_reference);
2696 if (rc) {
2697 pr_err("Invalid adc vdd reference property\n");
2698 return -EINVAL;
2699 }
Siddartha Mohanadossd9e74fe2017-10-13 15:27:55 -07002700 rc = of_property_read_u32(node, "qcom,adc-full-scale-code",
2701 &adc_prop->full_scale_code);
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08002702 if (rc) {
Siddartha Mohanadossd9e74fe2017-10-13 15:27:55 -07002703 pr_debug("Use default value of 0x4000 for full scale\n");
2704 adc_prop->full_scale_code = QPNP_VADC_HC_VREF_CODE;
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08002705 }
2706 adc_qpnp->adc_prop = adc_prop;
2707
2708 /* Get the peripheral address */
2709 rc = of_property_read_u32(pdev->dev.of_node, "reg", &base);
2710 if (rc < 0) {
2711 dev_err(&pdev->dev,
2712 "Couldn't find reg in node = %s rc = %d\n",
2713 pdev->dev.of_node->full_name, rc);
2714 return rc;
2715 }
2716
2717 adc_qpnp->slave = to_spmi_device(pdev->dev.parent)->usid;
2718 adc_qpnp->offset = base;
2719
2720 /* Register the ADC peripheral interrupt */
2721 adc_qpnp->adc_irq_eoc = platform_get_irq_byname(pdev,
2722 "eoc-int-en-set");
2723 if (adc_qpnp->adc_irq_eoc < 0) {
2724 pr_err("Invalid irq\n");
2725 return -ENXIO;
2726 }
2727
2728 init_completion(&adc_qpnp->adc_rslt_completion);
2729
2730 if (of_get_property(node, "hkadc_ldo-supply", NULL)) {
2731 adc_qpnp->hkadc_ldo = regulator_get(&pdev->dev, "hkadc_ldo");
2732 if (IS_ERR(adc_qpnp->hkadc_ldo)) {
2733 pr_err("hkadc_ldo-supply node not found\n");
2734 return -EINVAL;
2735 }
2736
2737 rc = regulator_set_voltage(adc_qpnp->hkadc_ldo,
2738 QPNP_VADC_LDO_VOLTAGE_MIN,
2739 QPNP_VADC_LDO_VOLTAGE_MAX);
2740 if (rc < 0) {
2741 pr_err("setting voltage for hkadc_ldo failed\n");
2742 return rc;
2743 }
2744
2745 rc = regulator_set_load(adc_qpnp->hkadc_ldo, 100000);
2746 if (rc < 0) {
2747 pr_err("hkadc_ldo optimum mode failed%d\n", rc);
2748 return rc;
2749 }
2750 }
2751
2752 if (of_get_property(node, "hkadc_ok-supply", NULL)) {
2753 adc_qpnp->hkadc_ldo_ok = regulator_get(&pdev->dev,
2754 "hkadc_ok");
2755 if (IS_ERR(adc_qpnp->hkadc_ldo_ok)) {
2756 pr_err("hkadc_ok node not found\n");
2757 return -EINVAL;
2758 }
2759
2760 rc = regulator_set_voltage(adc_qpnp->hkadc_ldo_ok,
2761 QPNP_VADC_OK_VOLTAGE_MIN,
2762 QPNP_VADC_OK_VOLTAGE_MAX);
2763 if (rc < 0) {
2764 pr_err("setting voltage for hkadc-ldo-ok failed\n");
2765 return rc;
2766 }
2767 }
2768
2769 return 0;
2770}
2771EXPORT_SYMBOL(qpnp_adc_get_devicetree_data);