blob: 33b760faf7bad64174a7658a61855e39df403f5b [file] [log] [blame]
Siddartha Mohanadoss37d9c8a2017-01-23 19:21:58 -08001/* Copyright (c) 2012-2017, The Linux Foundation. All rights reserved.
2 *
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
42/* Clamp negative ADC code to 0 */
43#define QPNP_VADC_HC_MAX_CODE 0x7FFF
44
45/*
46 * Units for temperature below (on x axis) is in 0.1DegC as
47 * required by the battery driver. Note the resolution used
48 * here to compute the table was done for DegC to milli-volts.
49 * In consideration to limit the size of the table for the given
50 * temperature range below, the result is linearly interpolated
51 * and provided to the battery driver in the units desired for
52 * their framework which is 0.1DegC. True resolution of 0.1DegC
53 * will result in the below table size to increase by 10 times.
54 */
55static const struct qpnp_vadc_map_pt adcmap_btm_threshold[] = {
56 {-300, 1642},
57 {-200, 1544},
58 {-100, 1414},
59 {0, 1260},
60 {10, 1244},
61 {20, 1228},
62 {30, 1212},
63 {40, 1195},
64 {50, 1179},
65 {60, 1162},
66 {70, 1146},
67 {80, 1129},
68 {90, 1113},
69 {100, 1097},
70 {110, 1080},
71 {120, 1064},
72 {130, 1048},
73 {140, 1032},
74 {150, 1016},
75 {160, 1000},
76 {170, 985},
77 {180, 969},
78 {190, 954},
79 {200, 939},
80 {210, 924},
81 {220, 909},
82 {230, 894},
83 {240, 880},
84 {250, 866},
85 {260, 852},
86 {270, 838},
87 {280, 824},
88 {290, 811},
89 {300, 798},
90 {310, 785},
91 {320, 773},
92 {330, 760},
93 {340, 748},
94 {350, 736},
95 {360, 725},
96 {370, 713},
97 {380, 702},
98 {390, 691},
99 {400, 681},
100 {410, 670},
101 {420, 660},
102 {430, 650},
103 {440, 640},
104 {450, 631},
105 {460, 622},
106 {470, 613},
107 {480, 604},
108 {490, 595},
109 {500, 587},
110 {510, 579},
111 {520, 571},
112 {530, 563},
113 {540, 556},
114 {550, 548},
115 {560, 541},
116 {570, 534},
117 {580, 527},
118 {590, 521},
119 {600, 514},
120 {610, 508},
121 {620, 502},
122 {630, 496},
123 {640, 490},
124 {650, 485},
125 {660, 281},
126 {670, 274},
127 {680, 267},
128 {690, 260},
129 {700, 254},
130 {710, 247},
131 {720, 241},
132 {730, 235},
133 {740, 229},
134 {750, 224},
135 {760, 218},
136 {770, 213},
137 {780, 208},
138 {790, 203}
139};
140
141static const struct qpnp_vadc_map_pt adcmap_qrd_btm_threshold[] = {
142 {-200, 1540},
143 {-180, 1517},
144 {-160, 1492},
145 {-140, 1467},
146 {-120, 1440},
147 {-100, 1412},
148 {-80, 1383},
149 {-60, 1353},
150 {-40, 1323},
151 {-20, 1292},
152 {0, 1260},
153 {20, 1228},
154 {40, 1196},
155 {60, 1163},
156 {80, 1131},
157 {100, 1098},
158 {120, 1066},
159 {140, 1034},
160 {160, 1002},
161 {180, 971},
162 {200, 941},
163 {220, 911},
164 {240, 882},
165 {260, 854},
166 {280, 826},
167 {300, 800},
168 {320, 774},
169 {340, 749},
170 {360, 726},
171 {380, 703},
172 {400, 681},
173 {420, 660},
174 {440, 640},
175 {460, 621},
176 {480, 602},
177 {500, 585},
178 {520, 568},
179 {540, 552},
180 {560, 537},
181 {580, 523},
182 {600, 510},
183 {620, 497},
184 {640, 485},
185 {660, 473},
186 {680, 462},
187 {700, 452},
188 {720, 442},
189 {740, 433},
190 {760, 424},
191 {780, 416},
192 {800, 408},
193};
194
195static const struct qpnp_vadc_map_pt adcmap_qrd_skuaa_btm_threshold[] = {
196 {-200, 1476},
197 {-180, 1450},
198 {-160, 1422},
199 {-140, 1394},
200 {-120, 1365},
201 {-100, 1336},
202 {-80, 1306},
203 {-60, 1276},
204 {-40, 1246},
205 {-20, 1216},
206 {0, 1185},
207 {20, 1155},
208 {40, 1126},
209 {60, 1096},
210 {80, 1068},
211 {100, 1040},
212 {120, 1012},
213 {140, 986},
214 {160, 960},
215 {180, 935},
216 {200, 911},
217 {220, 888},
218 {240, 866},
219 {260, 844},
220 {280, 824},
221 {300, 805},
222 {320, 786},
223 {340, 769},
224 {360, 752},
225 {380, 737},
226 {400, 722},
227 {420, 707},
228 {440, 694},
229 {460, 681},
230 {480, 669},
231 {500, 658},
232 {520, 648},
233 {540, 637},
234 {560, 628},
235 {580, 619},
236 {600, 611},
237 {620, 603},
238 {640, 595},
239 {660, 588},
240 {680, 582},
241 {700, 575},
242 {720, 569},
243 {740, 564},
244 {760, 559},
245 {780, 554},
246 {800, 549},
247};
248
249static const struct qpnp_vadc_map_pt adcmap_qrd_skug_btm_threshold[] = {
250 {-200, 1338},
251 {-180, 1307},
252 {-160, 1276},
253 {-140, 1244},
254 {-120, 1213},
255 {-100, 1182},
256 {-80, 1151},
257 {-60, 1121},
258 {-40, 1092},
259 {-20, 1063},
260 {0, 1035},
261 {20, 1008},
262 {40, 982},
263 {60, 957},
264 {80, 933},
265 {100, 910},
266 {120, 889},
267 {140, 868},
268 {160, 848},
269 {180, 830},
270 {200, 812},
271 {220, 795},
272 {240, 780},
273 {260, 765},
274 {280, 751},
275 {300, 738},
276 {320, 726},
277 {340, 714},
278 {360, 704},
279 {380, 694},
280 {400, 684},
281 {420, 675},
282 {440, 667},
283 {460, 659},
284 {480, 652},
285 {500, 645},
286 {520, 639},
287 {540, 633},
288 {560, 627},
289 {580, 622},
290 {600, 617},
291 {620, 613},
292 {640, 608},
293 {660, 604},
294 {680, 600},
295 {700, 597},
296 {720, 593},
297 {740, 590},
298 {760, 587},
299 {780, 585},
300 {800, 582},
301};
302
303static const struct qpnp_vadc_map_pt adcmap_qrd_skuh_btm_threshold[] = {
304 {-200, 1531},
305 {-180, 1508},
306 {-160, 1483},
307 {-140, 1458},
308 {-120, 1432},
309 {-100, 1404},
310 {-80, 1377},
311 {-60, 1348},
312 {-40, 1319},
313 {-20, 1290},
314 {0, 1260},
315 {20, 1230},
316 {40, 1200},
317 {60, 1171},
318 {80, 1141},
319 {100, 1112},
320 {120, 1083},
321 {140, 1055},
322 {160, 1027},
323 {180, 1000},
324 {200, 973},
325 {220, 948},
326 {240, 923},
327 {260, 899},
328 {280, 876},
329 {300, 854},
330 {320, 832},
331 {340, 812},
332 {360, 792},
333 {380, 774},
334 {400, 756},
335 {420, 739},
336 {440, 723},
337 {460, 707},
338 {480, 692},
339 {500, 679},
340 {520, 665},
341 {540, 653},
342 {560, 641},
343 {580, 630},
344 {600, 619},
345 {620, 609},
346 {640, 600},
347 {660, 591},
348 {680, 583},
349 {700, 575},
350 {720, 567},
351 {740, 560},
352 {760, 553},
353 {780, 547},
354 {800, 541},
355 {820, 535},
356 {840, 530},
357 {860, 524},
358 {880, 520},
359};
360
361static const struct qpnp_vadc_map_pt adcmap_qrd_skut1_btm_threshold[] = {
362 {-400, 1759},
363 {-350, 1742},
364 {-300, 1720},
365 {-250, 1691},
366 {-200, 1654},
367 {-150, 1619},
368 {-100, 1556},
369 {-50, 1493},
370 {0, 1422},
371 {50, 1345},
372 {100, 1264},
373 {150, 1180},
374 {200, 1097},
375 {250, 1017},
376 {300, 942},
377 {350, 873},
378 {400, 810},
379 {450, 754},
380 {500, 706},
381 {550, 664},
382 {600, 627},
383 {650, 596},
384 {700, 570},
385 {750, 547},
386 {800, 528},
387 {850, 512},
388 {900, 499},
389 {950, 487},
390 {1000, 477},
391};
392
393/* Voltage to temperature */
394static const struct qpnp_vadc_map_pt adcmap_100k_104ef_104fb[] = {
395 {1758, -40},
396 {1742, -35},
397 {1719, -30},
398 {1691, -25},
399 {1654, -20},
400 {1608, -15},
401 {1551, -10},
402 {1483, -5},
403 {1404, 0},
404 {1315, 5},
405 {1218, 10},
406 {1114, 15},
407 {1007, 20},
408 {900, 25},
409 {795, 30},
410 {696, 35},
411 {605, 40},
412 {522, 45},
413 {448, 50},
414 {383, 55},
415 {327, 60},
416 {278, 65},
417 {237, 70},
418 {202, 75},
419 {172, 80},
420 {146, 85},
421 {125, 90},
422 {107, 95},
423 {92, 100},
424 {79, 105},
425 {68, 110},
426 {59, 115},
427 {51, 120},
428 {44, 125}
429};
430
431/* Voltage to temperature */
432static const struct qpnp_vadc_map_pt adcmap_150k_104ef_104fb[] = {
433 {1738, -40},
434 {1714, -35},
435 {1682, -30},
436 {1641, -25},
437 {1589, -20},
438 {1526, -15},
439 {1451, -10},
440 {1363, -5},
441 {1266, 0},
442 {1159, 5},
443 {1048, 10},
444 {936, 15},
445 {825, 20},
446 {720, 25},
447 {622, 30},
448 {533, 35},
449 {454, 40},
450 {385, 45},
451 {326, 50},
452 {275, 55},
453 {232, 60},
454 {195, 65},
455 {165, 70},
456 {139, 75},
457 {118, 80},
458 {100, 85},
459 {85, 90},
460 {73, 95},
461 {62, 100},
462 {53, 105},
463 {46, 110},
464 {40, 115},
465 {34, 120},
466 {30, 125}
467};
468
469static const struct qpnp_vadc_map_pt adcmap_smb_batt_therm[] = {
470 {-300, 1625},
471 {-200, 1515},
472 {-100, 1368},
473 {0, 1192},
474 {10, 1173},
475 {20, 1154},
476 {30, 1135},
477 {40, 1116},
478 {50, 1097},
479 {60, 1078},
480 {70, 1059},
481 {80, 1040},
482 {90, 1020},
483 {100, 1001},
484 {110, 982},
485 {120, 963},
486 {130, 944},
487 {140, 925},
488 {150, 907},
489 {160, 888},
490 {170, 870},
491 {180, 851},
492 {190, 833},
493 {200, 815},
494 {210, 797},
495 {220, 780},
496 {230, 762},
497 {240, 745},
498 {250, 728},
499 {260, 711},
500 {270, 695},
501 {280, 679},
502 {290, 663},
503 {300, 647},
504 {310, 632},
505 {320, 616},
506 {330, 602},
507 {340, 587},
508 {350, 573},
509 {360, 559},
510 {370, 545},
511 {380, 531},
512 {390, 518},
513 {400, 505},
514 {410, 492},
515 {420, 480},
516 {430, 465},
517 {440, 456},
518 {450, 445},
519 {460, 433},
520 {470, 422},
521 {480, 412},
522 {490, 401},
523 {500, 391},
524 {510, 381},
525 {520, 371},
526 {530, 362},
527 {540, 352},
528 {550, 343},
529 {560, 335},
530 {570, 326},
531 {580, 318},
532 {590, 309},
533 {600, 302},
534 {610, 294},
535 {620, 286},
536 {630, 279},
537 {640, 272},
538 {650, 265},
539 {660, 258},
540 {670, 252},
541 {680, 245},
542 {690, 239},
543 {700, 233},
544 {710, 227},
545 {720, 221},
546 {730, 216},
547 {740, 211},
548 {750, 205},
549 {760, 200},
550 {770, 195},
551 {780, 190},
552 {790, 186}
553};
554
555/* Voltage to temperature */
556static const struct qpnp_vadc_map_pt adcmap_ncp03wf683[] = {
557 {1742, -40},
558 {1718, -35},
559 {1687, -30},
560 {1647, -25},
561 {1596, -20},
562 {1534, -15},
563 {1459, -10},
564 {1372, -5},
565 {1275, 0},
566 {1169, 5},
567 {1058, 10},
568 {945, 15},
569 {834, 20},
570 {729, 25},
571 {630, 30},
572 {541, 35},
573 {461, 40},
574 {392, 45},
575 {332, 50},
576 {280, 55},
577 {236, 60},
578 {199, 65},
579 {169, 70},
580 {142, 75},
581 {121, 80},
582 {102, 85},
583 {87, 90},
584 {74, 95},
585 {64, 100},
586 {55, 105},
587 {47, 110},
588 {40, 115},
589 {35, 120},
590 {30, 125}
591};
592
593/*
594 * Voltage to temperature table for 100k pull up for NTCG104EF104 with
595 * 1.875V reference.
596 */
597static const struct qpnp_vadc_map_pt adcmap_100k_104ef_104fb_1875_vref[] = {
598 { 1831, -40 },
599 { 1814, -35 },
600 { 1791, -30 },
601 { 1761, -25 },
602 { 1723, -20 },
603 { 1675, -15 },
604 { 1616, -10 },
605 { 1545, -5 },
606 { 1463, 0 },
607 { 1370, 5 },
608 { 1268, 10 },
609 { 1160, 15 },
610 { 1049, 20 },
611 { 937, 25 },
612 { 828, 30 },
613 { 726, 35 },
614 { 630, 40 },
615 { 544, 45 },
616 { 467, 50 },
617 { 399, 55 },
618 { 340, 60 },
619 { 290, 65 },
620 { 247, 70 },
621 { 209, 75 },
622 { 179, 80 },
623 { 153, 85 },
624 { 130, 90 },
625 { 112, 95 },
626 { 96, 100 },
627 { 82, 105 },
628 { 71, 110 },
629 { 62, 115 },
630 { 53, 120 },
631 { 46, 125 },
632};
633
634static int32_t qpnp_adc_map_voltage_temp(const struct qpnp_vadc_map_pt *pts,
635 uint32_t tablesize, int32_t input, int64_t *output)
636{
637 bool descending = 1;
638 uint32_t i = 0;
639
640 if (pts == NULL)
641 return -EINVAL;
642
643 /* Check if table is descending or ascending */
644 if (tablesize > 1) {
645 if (pts[0].x < pts[1].x)
646 descending = 0;
647 }
648
649 while (i < tablesize) {
650 if ((descending == 1) && (pts[i].x < input)) {
651 /*
652 * table entry is less than measured
653 * value and table is descending, stop.
654 */
655 break;
656 } else if ((descending == 0) &&
657 (pts[i].x > input)) {
658 /*
659 * table entry is greater than measured
660 * value and table is ascending, stop.
661 */
662 break;
663 }
664 i++;
665 }
666
667 if (i == 0)
668 *output = pts[0].y;
669 else if (i == tablesize)
670 *output = pts[tablesize-1].y;
671 else {
672 /* result is between search_index and search_index-1 */
673 /* interpolate linearly */
674 *output = (((int32_t) ((pts[i].y - pts[i-1].y)*
675 (input - pts[i-1].x))/
676 (pts[i].x - pts[i-1].x))+
677 pts[i-1].y);
678 }
679
680 return 0;
681}
682
683static int32_t qpnp_adc_map_temp_voltage(const struct qpnp_vadc_map_pt *pts,
684 uint32_t tablesize, int32_t input, int64_t *output)
685{
686 bool descending = 1;
687 uint32_t i = 0;
688
689 if (pts == NULL)
690 return -EINVAL;
691
692 /* Check if table is descending or ascending */
693 if (tablesize > 1) {
694 if (pts[0].y < pts[1].y)
695 descending = 0;
696 }
697
698 while (i < tablesize) {
699 if ((descending == 1) && (pts[i].y < input)) {
700 /* Table entry is less than measured value. */
701 /* Table is descending, stop. */
702 break;
703 } else if ((descending == 0) && (pts[i].y > input)) {
704 /* Table entry is greater than measured value. */
705 /* Table is ascending, stop. */
706 break;
707 }
708 i++;
709 }
710
711 if (i == 0) {
712 *output = pts[0].x;
713 } else if (i == tablesize) {
714 *output = pts[tablesize-1].x;
715 } else {
716 /* result is between search_index and search_index-1 */
717 /* interpolate linearly */
718 *output = (((int32_t) ((pts[i].x - pts[i-1].x)*
719 (input - pts[i-1].y))/
720 (pts[i].y - pts[i-1].y))+
721 pts[i-1].x);
722 }
723
724 return 0;
725}
726
727static void qpnp_adc_scale_with_calib_param(int32_t adc_code,
728 const struct qpnp_adc_properties *adc_properties,
729 const struct qpnp_vadc_chan_properties *chan_properties,
730 int64_t *scale_voltage)
731{
732 *scale_voltage = (adc_code -
733 chan_properties->adc_graph[chan_properties->calib_type].adc_gnd)
734 * chan_properties->adc_graph[chan_properties->calib_type].dx;
735 *scale_voltage = div64_s64(*scale_voltage,
736 chan_properties->adc_graph[chan_properties->calib_type].dy);
737
738 if (chan_properties->calib_type == CALIB_ABSOLUTE)
739 *scale_voltage +=
740 chan_properties->adc_graph[chan_properties->calib_type].dx;
741
742 if (*scale_voltage < 0)
743 *scale_voltage = 0;
744}
745
746int32_t qpnp_adc_scale_pmic_therm(struct qpnp_vadc_chip *vadc,
747 int32_t adc_code,
748 const struct qpnp_adc_properties *adc_properties,
749 const struct qpnp_vadc_chan_properties *chan_properties,
750 struct qpnp_vadc_result *adc_chan_result)
751{
752 int64_t pmic_voltage = 0;
753
754 if (!chan_properties || !chan_properties->offset_gain_numerator ||
755 !chan_properties->offset_gain_denominator || !adc_properties
756 || !adc_chan_result)
757 return -EINVAL;
758
759 if (adc_properties->adc_hc) {
760 /* (ADC code * vref_vadc (1.875V)) / 0x4000 */
761 if (adc_code > QPNP_VADC_HC_MAX_CODE)
762 adc_code = 0;
763 pmic_voltage = (int64_t) adc_code;
764 pmic_voltage *= (int64_t) (adc_properties->adc_vdd_reference
765 * 1000);
766 pmic_voltage = div64_s64(pmic_voltage,
767 QPNP_VADC_HC_VREF_CODE);
768 } else {
769 if (!chan_properties->adc_graph[CALIB_ABSOLUTE].dy)
770 return -EINVAL;
771 qpnp_adc_scale_with_calib_param(adc_code, adc_properties,
772 chan_properties, &pmic_voltage);
773 }
774
775 if (pmic_voltage > 0) {
776 /* 2mV/K */
777 adc_chan_result->measurement = pmic_voltage*
778 chan_properties->offset_gain_denominator;
779
780 do_div(adc_chan_result->measurement,
781 chan_properties->offset_gain_numerator * 2);
782 } else
783 adc_chan_result->measurement = 0;
784
785 /* Change to .001 deg C */
786 adc_chan_result->measurement -= KELVINMIL_DEGMIL;
787 adc_chan_result->physical = (int32_t) adc_chan_result->measurement;
788
789 return 0;
790}
791EXPORT_SYMBOL(qpnp_adc_scale_pmic_therm);
792
793int32_t qpnp_adc_scale_millidegc_pmic_voltage_thr(struct qpnp_vadc_chip *chip,
794 struct qpnp_adc_tm_btm_param *param,
795 uint32_t *low_threshold, uint32_t *high_threshold)
796{
797 struct qpnp_vadc_linear_graph btm_param;
798 int64_t low_output = 0, high_output = 0;
799 int rc = 0, sign = 0;
800
801 /* Convert to Kelvin and account for voltage to be written as 2mV/K */
802 low_output = (param->low_temp + KELVINMIL_DEGMIL) * 2;
803 /* Convert to Kelvin and account for voltage to be written as 2mV/K */
804 high_output = (param->high_temp + KELVINMIL_DEGMIL) * 2;
805
806 if (param->adc_tm_hc) {
807 low_output *= QPNP_VADC_HC_VREF_CODE;
808 do_div(low_output, (QPNP_VADC_HC_VDD_REFERENCE_MV * 1000));
809 high_output *= QPNP_VADC_HC_VREF_CODE;
810 do_div(high_output, (QPNP_VADC_HC_VDD_REFERENCE_MV * 1000));
811 } else {
812 rc = qpnp_get_vadc_gain_and_offset(chip, &btm_param,
813 CALIB_ABSOLUTE);
814 if (rc < 0) {
815 pr_err("Could not acquire gain and offset\n");
816 return rc;
817 }
818
819 /* Convert to voltage threshold */
820 low_output = (low_output - QPNP_ADC_625_UV) * btm_param.dy;
821 if (low_output < 0) {
822 sign = 1;
823 low_output = -low_output;
824 }
825 do_div(low_output, QPNP_ADC_625_UV);
826 if (sign)
827 low_output = -low_output;
828 low_output += btm_param.adc_gnd;
829
830 sign = 0;
831 /* Convert to voltage threshold */
832 high_output = (high_output - QPNP_ADC_625_UV) * btm_param.dy;
833 if (high_output < 0) {
834 sign = 1;
835 high_output = -high_output;
836 }
837 do_div(high_output, QPNP_ADC_625_UV);
838 if (sign)
839 high_output = -high_output;
840 high_output += btm_param.adc_gnd;
841 }
842
843 *low_threshold = (uint32_t) low_output;
844 *high_threshold = (uint32_t) high_output;
845
846 pr_debug("high_temp:%d, low_temp:%d\n", param->high_temp,
847 param->low_temp);
848 pr_debug("adc_code_high:%x, adc_code_low:%x\n", *high_threshold,
849 *low_threshold);
850
851 return 0;
852}
853EXPORT_SYMBOL(qpnp_adc_scale_millidegc_pmic_voltage_thr);
854
855/* Scales the ADC code to degC using the mapping
856 * table for the XO thermistor.
857 */
858int32_t qpnp_adc_tdkntcg_therm(struct qpnp_vadc_chip *chip,
859 int32_t adc_code,
860 const struct qpnp_adc_properties *adc_properties,
861 const struct qpnp_vadc_chan_properties *chan_properties,
862 struct qpnp_vadc_result *adc_chan_result)
863{
864 int64_t xo_thm_voltage = 0;
865
866 if (!chan_properties || !chan_properties->offset_gain_numerator ||
867 !chan_properties->offset_gain_denominator || !adc_properties
868 || !adc_chan_result)
869 return -EINVAL;
870
871 if (adc_properties->adc_hc) {
872 /* (ADC code * vref_vadc (1.875V) * 1000) / (0x4000 * 1000) */
873 if (adc_code > QPNP_VADC_HC_MAX_CODE)
874 adc_code = 0;
875 xo_thm_voltage = (int64_t) adc_code;
876 xo_thm_voltage *= (int64_t) (adc_properties->adc_vdd_reference
877 * 1000);
878 xo_thm_voltage = div64_s64(xo_thm_voltage,
879 QPNP_VADC_HC_VREF_CODE * 1000);
880 qpnp_adc_map_voltage_temp(adcmap_100k_104ef_104fb_1875_vref,
881 ARRAY_SIZE(adcmap_100k_104ef_104fb_1875_vref),
882 xo_thm_voltage, &adc_chan_result->physical);
883 } else {
884 qpnp_adc_scale_with_calib_param(adc_code,
885 adc_properties, chan_properties, &xo_thm_voltage);
886
887 if (chan_properties->calib_type == CALIB_ABSOLUTE)
888 do_div(xo_thm_voltage, 1000);
889
890 qpnp_adc_map_voltage_temp(adcmap_100k_104ef_104fb,
891 ARRAY_SIZE(adcmap_100k_104ef_104fb),
892 xo_thm_voltage, &adc_chan_result->physical);
893 }
894
895 return 0;
896}
897EXPORT_SYMBOL(qpnp_adc_tdkntcg_therm);
898
899int32_t qpnp_adc_scale_batt_therm(struct qpnp_vadc_chip *chip,
900 int32_t adc_code,
901 const struct qpnp_adc_properties *adc_properties,
902 const struct qpnp_vadc_chan_properties *chan_properties,
903 struct qpnp_vadc_result *adc_chan_result)
904{
905 int64_t bat_voltage = 0;
906
907 qpnp_adc_scale_with_calib_param(adc_code,
908 adc_properties, chan_properties, &bat_voltage);
909
910 adc_chan_result->measurement = bat_voltage;
911
912 return qpnp_adc_map_temp_voltage(
913 adcmap_btm_threshold,
914 ARRAY_SIZE(adcmap_btm_threshold),
915 bat_voltage,
916 &adc_chan_result->physical);
917}
918EXPORT_SYMBOL(qpnp_adc_scale_batt_therm);
919
920int32_t qpnp_adc_scale_qrd_batt_therm(struct qpnp_vadc_chip *chip,
921 int32_t adc_code,
922 const struct qpnp_adc_properties *adc_properties,
923 const struct qpnp_vadc_chan_properties *chan_properties,
924 struct qpnp_vadc_result *adc_chan_result)
925{
926 int64_t bat_voltage = 0;
927
928 qpnp_adc_scale_with_calib_param(adc_code,
929 adc_properties, chan_properties, &bat_voltage);
930
931 adc_chan_result->measurement = bat_voltage;
932
933 return qpnp_adc_map_temp_voltage(
934 adcmap_qrd_btm_threshold,
935 ARRAY_SIZE(adcmap_qrd_btm_threshold),
936 bat_voltage,
937 &adc_chan_result->physical);
938}
939EXPORT_SYMBOL(qpnp_adc_scale_qrd_batt_therm);
940
941int32_t qpnp_adc_scale_qrd_skuaa_batt_therm(struct qpnp_vadc_chip *chip,
942 int32_t adc_code,
943 const struct qpnp_adc_properties *adc_properties,
944 const struct qpnp_vadc_chan_properties *chan_properties,
945 struct qpnp_vadc_result *adc_chan_result)
946{
947 int64_t bat_voltage = 0;
948
949 qpnp_adc_scale_with_calib_param(adc_code,
950 adc_properties, chan_properties, &bat_voltage);
951
952 adc_chan_result->measurement = bat_voltage;
953
954 return qpnp_adc_map_temp_voltage(
955 adcmap_qrd_skuaa_btm_threshold,
956 ARRAY_SIZE(adcmap_qrd_skuaa_btm_threshold),
957 bat_voltage,
958 &adc_chan_result->physical);
959}
960EXPORT_SYMBOL(qpnp_adc_scale_qrd_skuaa_batt_therm);
961
962int32_t qpnp_adc_scale_qrd_skug_batt_therm(struct qpnp_vadc_chip *chip,
963 int32_t adc_code,
964 const struct qpnp_adc_properties *adc_properties,
965 const struct qpnp_vadc_chan_properties *chan_properties,
966 struct qpnp_vadc_result *adc_chan_result)
967{
968 int64_t bat_voltage = 0;
969
970 qpnp_adc_scale_with_calib_param(adc_code,
971 adc_properties, chan_properties, &bat_voltage);
972 adc_chan_result->measurement = bat_voltage;
973
974 return qpnp_adc_map_temp_voltage(
975 adcmap_qrd_skug_btm_threshold,
976 ARRAY_SIZE(adcmap_qrd_skug_btm_threshold),
977 bat_voltage,
978 &adc_chan_result->physical);
979}
980EXPORT_SYMBOL(qpnp_adc_scale_qrd_skug_batt_therm);
981
982int32_t qpnp_adc_scale_qrd_skuh_batt_therm(struct qpnp_vadc_chip *chip,
983 int32_t adc_code,
984 const struct qpnp_adc_properties *adc_properties,
985 const struct qpnp_vadc_chan_properties *chan_properties,
986 struct qpnp_vadc_result *adc_chan_result)
987{
988 int64_t bat_voltage = 0;
989
990 qpnp_adc_scale_with_calib_param(adc_code,
991 adc_properties, chan_properties, &bat_voltage);
992
993 return qpnp_adc_map_temp_voltage(
994 adcmap_qrd_skuh_btm_threshold,
995 ARRAY_SIZE(adcmap_qrd_skuh_btm_threshold),
996 bat_voltage,
997 &adc_chan_result->physical);
998}
999EXPORT_SYMBOL(qpnp_adc_scale_qrd_skuh_batt_therm);
1000
1001int32_t qpnp_adc_scale_qrd_skut1_batt_therm(struct qpnp_vadc_chip *chip,
1002 int32_t adc_code,
1003 const struct qpnp_adc_properties *adc_properties,
1004 const struct qpnp_vadc_chan_properties *chan_properties,
1005 struct qpnp_vadc_result *adc_chan_result)
1006{
1007 int64_t bat_voltage = 0;
1008
1009 qpnp_adc_scale_with_calib_param(adc_code,
1010 adc_properties, chan_properties, &bat_voltage);
1011
1012 return qpnp_adc_map_temp_voltage(
1013 adcmap_qrd_skut1_btm_threshold,
1014 ARRAY_SIZE(adcmap_qrd_skut1_btm_threshold),
1015 bat_voltage,
1016 &adc_chan_result->physical);
1017}
1018EXPORT_SYMBOL(qpnp_adc_scale_qrd_skut1_batt_therm);
1019
1020int32_t qpnp_adc_scale_smb_batt_therm(struct qpnp_vadc_chip *chip,
1021 int32_t adc_code,
1022 const struct qpnp_adc_properties *adc_properties,
1023 const struct qpnp_vadc_chan_properties *chan_properties,
1024 struct qpnp_vadc_result *adc_chan_result)
1025{
1026 int64_t bat_voltage = 0;
1027
1028 qpnp_adc_scale_with_calib_param(adc_code,
1029 adc_properties, chan_properties, &bat_voltage);
1030
1031 return qpnp_adc_map_temp_voltage(
1032 adcmap_smb_batt_therm,
1033 ARRAY_SIZE(adcmap_smb_batt_therm),
1034 bat_voltage,
1035 &adc_chan_result->physical);
1036}
1037EXPORT_SYMBOL(qpnp_adc_scale_smb_batt_therm);
1038
1039int32_t qpnp_adc_scale_therm_pu1(struct qpnp_vadc_chip *chip,
1040 int32_t adc_code,
1041 const struct qpnp_adc_properties *adc_properties,
1042 const struct qpnp_vadc_chan_properties *chan_properties,
1043 struct qpnp_vadc_result *adc_chan_result)
1044{
1045 int64_t therm_voltage = 0;
1046
1047 qpnp_adc_scale_with_calib_param(adc_code,
1048 adc_properties, chan_properties, &therm_voltage);
1049
1050 qpnp_adc_map_voltage_temp(adcmap_150k_104ef_104fb,
1051 ARRAY_SIZE(adcmap_150k_104ef_104fb),
1052 therm_voltage, &adc_chan_result->physical);
1053
1054 return 0;
1055}
1056EXPORT_SYMBOL(qpnp_adc_scale_therm_pu1);
1057
1058int32_t qpnp_adc_scale_therm_pu2(struct qpnp_vadc_chip *chip,
1059 int32_t adc_code,
1060 const struct qpnp_adc_properties *adc_properties,
1061 const struct qpnp_vadc_chan_properties *chan_properties,
1062 struct qpnp_vadc_result *adc_chan_result)
1063{
1064 int64_t therm_voltage = 0;
1065
1066 if (!chan_properties || !chan_properties->offset_gain_numerator ||
1067 !chan_properties->offset_gain_denominator || !adc_properties)
1068 return -EINVAL;
1069
1070 if (adc_properties->adc_hc) {
1071 /* (ADC code * vref_vadc (1.875V) * 1000) / (0x4000 * 1000) */
1072 if (adc_code > QPNP_VADC_HC_MAX_CODE)
1073 adc_code = 0;
1074 therm_voltage = (int64_t) adc_code;
1075 therm_voltage *= (int64_t) (adc_properties->adc_vdd_reference
1076 * 1000);
1077 therm_voltage = div64_s64(therm_voltage,
1078 (QPNP_VADC_HC_VREF_CODE * 1000));
1079
1080 qpnp_adc_map_voltage_temp(adcmap_100k_104ef_104fb_1875_vref,
1081 ARRAY_SIZE(adcmap_100k_104ef_104fb_1875_vref),
1082 therm_voltage, &adc_chan_result->physical);
1083 } else {
1084 qpnp_adc_scale_with_calib_param(adc_code,
1085 adc_properties, chan_properties, &therm_voltage);
1086
1087 if (chan_properties->calib_type == CALIB_ABSOLUTE)
1088 do_div(therm_voltage, 1000);
1089
1090 qpnp_adc_map_voltage_temp(adcmap_100k_104ef_104fb,
1091 ARRAY_SIZE(adcmap_100k_104ef_104fb),
1092 therm_voltage, &adc_chan_result->physical);
1093 }
1094
1095 return 0;
1096}
1097EXPORT_SYMBOL(qpnp_adc_scale_therm_pu2);
1098
1099int32_t qpnp_adc_tm_scale_voltage_therm_pu2(struct qpnp_vadc_chip *chip,
1100 const struct qpnp_adc_properties *adc_properties,
1101 uint32_t reg, int64_t *result)
1102{
1103 int64_t adc_voltage = 0;
1104 struct qpnp_vadc_linear_graph param1;
1105 int negative_offset = 0;
1106
1107 if (adc_properties->adc_hc) {
1108 /* (ADC code * vref_vadc (1.875V)) / 0x4000 */
1109 if (reg > QPNP_VADC_HC_MAX_CODE)
1110 reg = 0;
1111 adc_voltage = (int64_t) reg;
1112 adc_voltage *= QPNP_VADC_HC_VDD_REFERENCE_MV;
1113 adc_voltage = div64_s64(adc_voltage,
1114 QPNP_VADC_HC_VREF_CODE);
1115 qpnp_adc_map_voltage_temp(adcmap_100k_104ef_104fb_1875_vref,
1116 ARRAY_SIZE(adcmap_100k_104ef_104fb_1875_vref),
1117 adc_voltage, result);
1118 } else {
1119 qpnp_get_vadc_gain_and_offset(chip, &param1, CALIB_RATIOMETRIC);
1120
1121 adc_voltage = (reg - param1.adc_gnd) * param1.adc_vref;
1122 if (adc_voltage < 0) {
1123 negative_offset = 1;
1124 adc_voltage = -adc_voltage;
1125 }
1126
1127 do_div(adc_voltage, param1.dy);
1128
1129 qpnp_adc_map_voltage_temp(adcmap_100k_104ef_104fb,
1130 ARRAY_SIZE(adcmap_100k_104ef_104fb),
1131 adc_voltage, result);
1132 if (negative_offset)
1133 adc_voltage = -adc_voltage;
1134 }
1135
1136 return 0;
1137}
1138EXPORT_SYMBOL(qpnp_adc_tm_scale_voltage_therm_pu2);
1139
1140int32_t qpnp_adc_tm_scale_therm_voltage_pu2(struct qpnp_vadc_chip *chip,
1141 const struct qpnp_adc_properties *adc_properties,
1142 struct qpnp_adc_tm_config *param)
1143{
1144 struct qpnp_vadc_linear_graph param1;
1145 int rc;
1146
1147 if (adc_properties->adc_hc) {
1148 rc = qpnp_adc_map_temp_voltage(
1149 adcmap_100k_104ef_104fb_1875_vref,
1150 ARRAY_SIZE(adcmap_100k_104ef_104fb_1875_vref),
1151 param->low_thr_temp, &param->low_thr_voltage);
1152 if (rc)
1153 return rc;
1154 param->low_thr_voltage *= QPNP_VADC_HC_VREF_CODE;
1155 do_div(param->low_thr_voltage, QPNP_VADC_HC_VDD_REFERENCE_MV);
1156
1157 rc = qpnp_adc_map_temp_voltage(
1158 adcmap_100k_104ef_104fb_1875_vref,
1159 ARRAY_SIZE(adcmap_100k_104ef_104fb_1875_vref),
1160 param->high_thr_temp, &param->high_thr_voltage);
1161 if (rc)
1162 return rc;
1163 param->high_thr_voltage *= QPNP_VADC_HC_VREF_CODE;
1164 do_div(param->high_thr_voltage, QPNP_VADC_HC_VDD_REFERENCE_MV);
1165 } else {
1166 qpnp_get_vadc_gain_and_offset(chip, &param1, CALIB_RATIOMETRIC);
1167
1168 rc = qpnp_adc_map_temp_voltage(adcmap_100k_104ef_104fb,
1169 ARRAY_SIZE(adcmap_100k_104ef_104fb),
1170 param->low_thr_temp, &param->low_thr_voltage);
1171 if (rc)
1172 return rc;
1173
1174 param->low_thr_voltage *= param1.dy;
1175 do_div(param->low_thr_voltage, param1.adc_vref);
1176 param->low_thr_voltage += param1.adc_gnd;
1177
1178 rc = qpnp_adc_map_temp_voltage(adcmap_100k_104ef_104fb,
1179 ARRAY_SIZE(adcmap_100k_104ef_104fb),
1180 param->high_thr_temp, &param->high_thr_voltage);
1181 if (rc)
1182 return rc;
1183
1184 param->high_thr_voltage *= param1.dy;
1185 do_div(param->high_thr_voltage, param1.adc_vref);
1186 param->high_thr_voltage += param1.adc_gnd;
1187 }
1188
1189 return 0;
1190}
1191EXPORT_SYMBOL(qpnp_adc_tm_scale_therm_voltage_pu2);
1192
1193int32_t qpnp_adc_scale_therm_ncp03(struct qpnp_vadc_chip *chip,
1194 int32_t adc_code,
1195 const struct qpnp_adc_properties *adc_properties,
1196 const struct qpnp_vadc_chan_properties *chan_properties,
1197 struct qpnp_vadc_result *adc_chan_result)
1198{
1199 int64_t therm_voltage = 0;
1200
1201 qpnp_adc_scale_with_calib_param(adc_code,
1202 adc_properties, chan_properties, &therm_voltage);
1203
1204 qpnp_adc_map_voltage_temp(adcmap_ncp03wf683,
1205 ARRAY_SIZE(adcmap_ncp03wf683),
1206 therm_voltage, &adc_chan_result->physical);
1207
1208 return 0;
1209}
1210EXPORT_SYMBOL(qpnp_adc_scale_therm_ncp03);
1211
1212int32_t qpnp_adc_scale_batt_id(struct qpnp_vadc_chip *chip,
1213 int32_t adc_code,
1214 const struct qpnp_adc_properties *adc_properties,
1215 const struct qpnp_vadc_chan_properties *chan_properties,
1216 struct qpnp_vadc_result *adc_chan_result)
1217{
1218 int64_t batt_id_voltage = 0;
1219
1220 qpnp_adc_scale_with_calib_param(adc_code,
1221 adc_properties, chan_properties, &batt_id_voltage);
1222
1223 adc_chan_result->physical = batt_id_voltage;
1224 adc_chan_result->physical = adc_chan_result->measurement;
1225
1226 return 0;
1227}
1228EXPORT_SYMBOL(qpnp_adc_scale_batt_id);
1229
1230int32_t qpnp_adc_scale_default(struct qpnp_vadc_chip *vadc,
1231 int32_t adc_code,
1232 const struct qpnp_adc_properties *adc_properties,
1233 const struct qpnp_vadc_chan_properties *chan_properties,
1234 struct qpnp_vadc_result *adc_chan_result)
1235{
1236 int64_t scale_voltage = 0;
1237
1238 if (!chan_properties || !chan_properties->offset_gain_numerator ||
1239 !chan_properties->offset_gain_denominator || !adc_properties
1240 || !adc_chan_result)
1241 return -EINVAL;
1242
1243 if (adc_properties->adc_hc) {
1244 /* (ADC code * vref_vadc (1.875V)) / 0x4000 */
1245 if (adc_code > QPNP_VADC_HC_MAX_CODE)
1246 adc_code = 0;
1247 scale_voltage = (int64_t) adc_code;
1248 scale_voltage *= (adc_properties->adc_vdd_reference * 1000);
1249 scale_voltage = div64_s64(scale_voltage,
1250 QPNP_VADC_HC_VREF_CODE);
1251 } else {
1252 qpnp_adc_scale_with_calib_param(adc_code, adc_properties,
1253 chan_properties, &scale_voltage);
1254 if (!chan_properties->calib_type == CALIB_ABSOLUTE)
1255 scale_voltage *= 1000;
1256 }
1257
1258
1259 scale_voltage *= chan_properties->offset_gain_denominator;
1260 scale_voltage = div64_s64(scale_voltage,
1261 chan_properties->offset_gain_numerator);
1262 adc_chan_result->measurement = scale_voltage;
1263 /*
1264 * Note: adc_chan_result->measurement is in the unit of
1265 * adc_properties.adc_reference. For generic channel processing,
1266 * channel measurement is a scale/ratio relative to the adc
1267 * reference input
1268 */
1269 adc_chan_result->physical = adc_chan_result->measurement;
1270
1271 return 0;
1272}
1273EXPORT_SYMBOL(qpnp_adc_scale_default);
1274
1275int32_t qpnp_adc_usb_scaler(struct qpnp_vadc_chip *chip,
1276 struct qpnp_adc_tm_btm_param *param,
1277 uint32_t *low_threshold, uint32_t *high_threshold)
1278{
1279 struct qpnp_vadc_linear_graph usb_param;
1280
1281 qpnp_get_vadc_gain_and_offset(chip, &usb_param, CALIB_RATIOMETRIC);
1282
1283 *low_threshold = param->low_thr * usb_param.dy;
1284 do_div(*low_threshold, usb_param.adc_vref);
1285 *low_threshold += usb_param.adc_gnd;
1286
1287 *high_threshold = param->high_thr * usb_param.dy;
1288 do_div(*high_threshold, usb_param.adc_vref);
1289 *high_threshold += usb_param.adc_gnd;
1290
1291 pr_debug("high_volt:%d, low_volt:%d\n", param->high_thr,
1292 param->low_thr);
1293 return 0;
1294}
1295EXPORT_SYMBOL(qpnp_adc_usb_scaler);
1296
1297int32_t qpnp_adc_absolute_rthr(struct qpnp_vadc_chip *chip,
1298 struct qpnp_adc_tm_btm_param *param,
1299 uint32_t *low_threshold, uint32_t *high_threshold)
1300{
1301 struct qpnp_vadc_linear_graph vbatt_param;
1302 int rc = 0, sign = 0;
1303 int64_t low_thr = 0, high_thr = 0;
1304
1305 if (param->adc_tm_hc) {
1306 low_thr = (param->low_thr/param->gain_den);
1307 low_thr *= param->gain_num;
1308 low_thr *= QPNP_VADC_HC_VREF_CODE;
1309 do_div(low_thr, (QPNP_VADC_HC_VDD_REFERENCE_MV * 1000));
1310 *low_threshold = low_thr;
1311
1312 high_thr = (param->high_thr/param->gain_den);
1313 high_thr *= param->gain_num;
1314 high_thr *= QPNP_VADC_HC_VREF_CODE;
1315 do_div(high_thr, (QPNP_VADC_HC_VDD_REFERENCE_MV * 1000));
1316 *high_threshold = high_thr;
1317 } else {
1318 rc = qpnp_get_vadc_gain_and_offset(chip, &vbatt_param,
1319 CALIB_ABSOLUTE);
1320 if (rc < 0)
1321 return rc;
1322
1323 low_thr = (((param->low_thr/param->gain_den) -
1324 QPNP_ADC_625_UV) * vbatt_param.dy);
1325 if (low_thr < 0) {
1326 sign = 1;
1327 low_thr = -low_thr;
1328 }
1329 low_thr = low_thr * param->gain_num;
1330 do_div(low_thr, QPNP_ADC_625_UV);
1331 if (sign)
1332 low_thr = -low_thr;
1333 *low_threshold = low_thr + vbatt_param.adc_gnd;
1334
1335 sign = 0;
1336 high_thr = (((param->high_thr/param->gain_den) -
1337 QPNP_ADC_625_UV) * vbatt_param.dy);
1338 if (high_thr < 0) {
1339 sign = 1;
1340 high_thr = -high_thr;
1341 }
1342 high_thr = high_thr * param->gain_num;
1343 do_div(high_thr, QPNP_ADC_625_UV);
1344 if (sign)
1345 high_thr = -high_thr;
1346 *high_threshold = high_thr + vbatt_param.adc_gnd;
1347 }
1348
1349 pr_debug("high_volt:%d, low_volt:%d\n", param->high_thr,
1350 param->low_thr);
1351 pr_debug("adc_code_high:%x, adc_code_low:%x\n", *high_threshold,
1352 *low_threshold);
1353 return 0;
1354}
1355EXPORT_SYMBOL(qpnp_adc_absolute_rthr);
1356
1357int32_t qpnp_adc_vbatt_rscaler(struct qpnp_vadc_chip *chip,
1358 struct qpnp_adc_tm_btm_param *param,
1359 uint32_t *low_threshold, uint32_t *high_threshold)
1360{
1361 return qpnp_adc_absolute_rthr(chip, param, low_threshold,
1362 high_threshold);
1363}
1364EXPORT_SYMBOL(qpnp_adc_vbatt_rscaler);
1365
1366int32_t qpnp_vadc_absolute_rthr(struct qpnp_vadc_chip *chip,
1367 const struct qpnp_vadc_chan_properties *chan_prop,
1368 struct qpnp_adc_tm_btm_param *param,
1369 uint32_t *low_threshold, uint32_t *high_threshold)
1370{
1371 struct qpnp_vadc_linear_graph vbatt_param;
1372 int rc = 0, sign = 0;
1373 int64_t low_thr = 0, high_thr = 0;
1374
1375 if (!chan_prop || !chan_prop->offset_gain_numerator ||
1376 !chan_prop->offset_gain_denominator)
1377 return -EINVAL;
1378
1379 rc = qpnp_get_vadc_gain_and_offset(chip, &vbatt_param, CALIB_ABSOLUTE);
1380 if (rc < 0)
1381 return rc;
1382
1383 low_thr = (((param->low_thr)/(int)chan_prop->offset_gain_denominator
1384 - QPNP_ADC_625_UV) * vbatt_param.dy);
1385 if (low_thr < 0) {
1386 sign = 1;
1387 low_thr = -low_thr;
1388 }
1389 low_thr = low_thr * chan_prop->offset_gain_numerator;
1390 do_div(low_thr, QPNP_ADC_625_UV);
1391 if (sign)
1392 low_thr = -low_thr;
1393 *low_threshold = low_thr + vbatt_param.adc_gnd;
1394
1395 sign = 0;
1396 high_thr = (((param->high_thr)/(int)chan_prop->offset_gain_denominator
1397 - QPNP_ADC_625_UV) * vbatt_param.dy);
1398 if (high_thr < 0) {
1399 sign = 1;
1400 high_thr = -high_thr;
1401 }
1402 high_thr = high_thr * chan_prop->offset_gain_numerator;
1403 do_div(high_thr, QPNP_ADC_625_UV);
1404 if (sign)
1405 high_thr = -high_thr;
1406 *high_threshold = high_thr + vbatt_param.adc_gnd;
1407
1408 pr_debug("high_volt:%d, low_volt:%d\n", param->high_thr,
1409 param->low_thr);
1410 pr_debug("adc_code_high:%x, adc_code_low:%x\n", *high_threshold,
1411 *low_threshold);
1412 return 0;
1413}
1414EXPORT_SYMBOL(qpnp_vadc_absolute_rthr);
1415
1416int32_t qpnp_adc_btm_scaler(struct qpnp_vadc_chip *chip,
1417 struct qpnp_adc_tm_btm_param *param,
1418 uint32_t *low_threshold, uint32_t *high_threshold)
1419{
1420 struct qpnp_vadc_linear_graph btm_param;
1421 int64_t low_output = 0, high_output = 0;
1422 int rc = 0;
1423
1424 if (param->adc_tm_hc) {
1425 pr_err("Update scaling for VADC_TM_HC\n");
1426 return -EINVAL;
1427 }
1428
1429 qpnp_get_vadc_gain_and_offset(chip, &btm_param, CALIB_RATIOMETRIC);
1430
1431 pr_debug("warm_temp:%d and cool_temp:%d\n", param->high_temp,
1432 param->low_temp);
1433 rc = qpnp_adc_map_voltage_temp(
1434 adcmap_btm_threshold,
1435 ARRAY_SIZE(adcmap_btm_threshold),
1436 (param->low_temp),
1437 &low_output);
1438 if (rc) {
1439 pr_debug("low_temp mapping failed with %d\n", rc);
1440 return rc;
1441 }
1442
1443 pr_debug("low_output:%lld\n", low_output);
1444 low_output *= btm_param.dy;
1445 do_div(low_output, btm_param.adc_vref);
1446 low_output += btm_param.adc_gnd;
1447
1448 rc = qpnp_adc_map_voltage_temp(
1449 adcmap_btm_threshold,
1450 ARRAY_SIZE(adcmap_btm_threshold),
1451 (param->high_temp),
1452 &high_output);
1453 if (rc) {
1454 pr_debug("high temp mapping failed with %d\n", rc);
1455 return rc;
1456 }
1457
1458 pr_debug("high_output:%lld\n", high_output);
1459 high_output *= btm_param.dy;
1460 do_div(high_output, btm_param.adc_vref);
1461 high_output += btm_param.adc_gnd;
1462
1463 /* btm low temperature correspondes to high voltage threshold */
1464 *low_threshold = high_output;
1465 /* btm high temperature correspondes to low voltage threshold */
1466 *high_threshold = low_output;
1467
1468 pr_debug("high_volt:%d, low_volt:%d\n", *high_threshold,
1469 *low_threshold);
1470 return 0;
1471}
1472EXPORT_SYMBOL(qpnp_adc_btm_scaler);
1473
1474int32_t qpnp_adc_qrd_skuh_btm_scaler(struct qpnp_vadc_chip *chip,
1475 struct qpnp_adc_tm_btm_param *param,
1476 uint32_t *low_threshold, uint32_t *high_threshold)
1477{
1478 struct qpnp_vadc_linear_graph btm_param;
1479 int64_t low_output = 0, high_output = 0;
1480 int rc = 0;
1481
1482 if (param->adc_tm_hc) {
1483 pr_err("Update scaling for VADC_TM_HC\n");
1484 return -EINVAL;
1485 }
1486
1487 qpnp_get_vadc_gain_and_offset(chip, &btm_param, CALIB_RATIOMETRIC);
1488
1489 pr_debug("warm_temp:%d and cool_temp:%d\n", param->high_temp,
1490 param->low_temp);
1491 rc = qpnp_adc_map_voltage_temp(
1492 adcmap_qrd_skuh_btm_threshold,
1493 ARRAY_SIZE(adcmap_qrd_skuh_btm_threshold),
1494 (param->low_temp),
1495 &low_output);
1496 if (rc) {
1497 pr_debug("low_temp mapping failed with %d\n", rc);
1498 return rc;
1499 }
1500
1501 pr_debug("low_output:%lld\n", low_output);
1502 low_output *= btm_param.dy;
1503 do_div(low_output, btm_param.adc_vref);
1504 low_output += btm_param.adc_gnd;
1505
1506 rc = qpnp_adc_map_voltage_temp(
1507 adcmap_qrd_skuh_btm_threshold,
1508 ARRAY_SIZE(adcmap_qrd_skuh_btm_threshold),
1509 (param->high_temp),
1510 &high_output);
1511 if (rc) {
1512 pr_debug("high temp mapping failed with %d\n", rc);
1513 return rc;
1514 }
1515
1516 pr_debug("high_output:%lld\n", high_output);
1517 high_output *= btm_param.dy;
1518 do_div(high_output, btm_param.adc_vref);
1519 high_output += btm_param.adc_gnd;
1520
1521 /* btm low temperature correspondes to high voltage threshold */
1522 *low_threshold = high_output;
1523 /* btm high temperature correspondes to low voltage threshold */
1524 *high_threshold = low_output;
1525
1526 pr_debug("high_volt:%d, low_volt:%d\n", *high_threshold,
1527 *low_threshold);
1528 return 0;
1529}
1530EXPORT_SYMBOL(qpnp_adc_qrd_skuh_btm_scaler);
1531
1532int32_t qpnp_adc_qrd_skut1_btm_scaler(struct qpnp_vadc_chip *chip,
1533 struct qpnp_adc_tm_btm_param *param,
1534 uint32_t *low_threshold, uint32_t *high_threshold)
1535{
1536 struct qpnp_vadc_linear_graph btm_param;
1537 int64_t low_output = 0, high_output = 0;
1538 int rc = 0;
1539
1540 if (param->adc_tm_hc) {
1541 pr_err("Update scaling for VADC_TM_HC\n");
1542 return -EINVAL;
1543 }
1544
1545 qpnp_get_vadc_gain_and_offset(chip, &btm_param, CALIB_RATIOMETRIC);
1546
1547 pr_debug("warm_temp:%d and cool_temp:%d\n", param->high_temp,
1548 param->low_temp);
1549 rc = qpnp_adc_map_voltage_temp(
1550 adcmap_qrd_skut1_btm_threshold,
1551 ARRAY_SIZE(adcmap_qrd_skut1_btm_threshold),
1552 (param->low_temp),
1553 &low_output);
1554 if (rc) {
1555 pr_debug("low_temp mapping failed with %d\n", rc);
1556 return rc;
1557 }
1558
1559 pr_debug("low_output:%lld\n", low_output);
1560 low_output *= btm_param.dy;
1561 do_div(low_output, btm_param.adc_vref);
1562 low_output += btm_param.adc_gnd;
1563
1564 rc = qpnp_adc_map_voltage_temp(
1565 adcmap_qrd_skut1_btm_threshold,
1566 ARRAY_SIZE(adcmap_qrd_skut1_btm_threshold),
1567 (param->high_temp),
1568 &high_output);
1569 if (rc) {
1570 pr_debug("high temp mapping failed with %d\n", rc);
1571 return rc;
1572 }
1573
1574 pr_debug("high_output:%lld\n", high_output);
1575 high_output *= btm_param.dy;
1576 do_div(high_output, btm_param.adc_vref);
1577 high_output += btm_param.adc_gnd;
1578
1579 /* btm low temperature correspondes to high voltage threshold */
1580 *low_threshold = high_output;
1581 /* btm high temperature correspondes to low voltage threshold */
1582 *high_threshold = low_output;
1583
1584 pr_debug("high_volt:%d, low_volt:%d\n", *high_threshold,
1585 *low_threshold);
1586 return 0;
1587}
1588EXPORT_SYMBOL(qpnp_adc_qrd_skut1_btm_scaler);
1589
1590int32_t qpnp_adc_smb_btm_rscaler(struct qpnp_vadc_chip *chip,
1591 struct qpnp_adc_tm_btm_param *param,
1592 uint32_t *low_threshold, uint32_t *high_threshold)
1593{
1594 struct qpnp_vadc_linear_graph btm_param;
1595 int64_t low_output = 0, high_output = 0;
1596 int rc = 0;
1597
1598 if (param->adc_tm_hc) {
1599 pr_err("Update scaling for VADC_TM_HC\n");
1600 return -EINVAL;
1601 }
1602
1603 qpnp_get_vadc_gain_and_offset(chip, &btm_param, CALIB_RATIOMETRIC);
1604
1605 pr_debug("warm_temp:%d and cool_temp:%d\n", param->high_temp,
1606 param->low_temp);
1607 rc = qpnp_adc_map_voltage_temp(
1608 adcmap_smb_batt_therm,
1609 ARRAY_SIZE(adcmap_smb_batt_therm),
1610 (param->low_temp),
1611 &low_output);
1612 if (rc) {
1613 pr_debug("low_temp mapping failed with %d\n", rc);
1614 return rc;
1615 }
1616
1617 pr_debug("low_output:%lld\n", low_output);
1618 low_output *= btm_param.dy;
1619 do_div(low_output, btm_param.adc_vref);
1620 low_output += btm_param.adc_gnd;
1621
1622 rc = qpnp_adc_map_voltage_temp(
1623 adcmap_smb_batt_therm,
1624 ARRAY_SIZE(adcmap_smb_batt_therm),
1625 (param->high_temp),
1626 &high_output);
1627 if (rc) {
1628 pr_debug("high temp mapping failed with %d\n", rc);
1629 return rc;
1630 }
1631
1632 pr_debug("high_output:%lld\n", high_output);
1633 high_output *= btm_param.dy;
1634 do_div(high_output, btm_param.adc_vref);
1635 high_output += btm_param.adc_gnd;
1636
1637 /* btm low temperature correspondes to high voltage threshold */
1638 *low_threshold = high_output;
1639 /* btm high temperature correspondes to low voltage threshold */
1640 *high_threshold = low_output;
1641
1642 pr_debug("high_volt:%d, low_volt:%d\n", *high_threshold,
1643 *low_threshold);
1644 return 0;
1645}
1646EXPORT_SYMBOL(qpnp_adc_smb_btm_rscaler);
1647
1648int32_t qpnp_adc_scale_pmi_chg_temp(struct qpnp_vadc_chip *vadc,
1649 int32_t adc_code,
1650 const struct qpnp_adc_properties *adc_properties,
1651 const struct qpnp_vadc_chan_properties *chan_properties,
1652 struct qpnp_vadc_result *adc_chan_result)
1653{
1654 int rc = 0;
1655
1656 rc = qpnp_adc_scale_default(vadc, adc_code, adc_properties,
1657 chan_properties, adc_chan_result);
1658 if (rc < 0)
1659 return rc;
1660
1661 pr_debug("raw_code:%x, v_adc:%lld\n", adc_code,
1662 adc_chan_result->physical);
1663 adc_chan_result->physical = (int64_t) ((PMI_CHG_SCALE_1) *
1664 (adc_chan_result->physical * 2));
1665 adc_chan_result->physical = (int64_t) (adc_chan_result->physical +
1666 PMI_CHG_SCALE_2);
1667 adc_chan_result->physical = (int64_t) adc_chan_result->physical;
1668 adc_chan_result->physical = div64_s64(adc_chan_result->physical,
1669 1000000);
1670
1671 return 0;
1672}
1673EXPORT_SYMBOL(qpnp_adc_scale_pmi_chg_temp);
1674
1675int32_t qpnp_adc_enable_voltage(struct qpnp_adc_drv *adc)
1676{
1677 int rc = 0;
1678
1679 if (adc->hkadc_ldo) {
1680 rc = regulator_enable(adc->hkadc_ldo);
1681 if (rc < 0) {
1682 pr_err("Failed to enable hkadc ldo\n");
1683 return rc;
1684 }
1685 }
1686
1687 if (adc->hkadc_ldo_ok) {
1688 rc = regulator_enable(adc->hkadc_ldo_ok);
1689 if (rc < 0) {
1690 pr_err("Failed to enable hkadc ok signal\n");
1691 return rc;
1692 }
1693 }
1694
1695 return rc;
1696}
1697EXPORT_SYMBOL(qpnp_adc_enable_voltage);
1698
1699void qpnp_adc_disable_voltage(struct qpnp_adc_drv *adc)
1700{
1701 if (adc->hkadc_ldo)
1702 regulator_disable(adc->hkadc_ldo);
1703
1704 if (adc->hkadc_ldo_ok)
1705 regulator_disable(adc->hkadc_ldo_ok);
1706
1707}
1708EXPORT_SYMBOL(qpnp_adc_disable_voltage);
1709
1710void qpnp_adc_free_voltage_resource(struct qpnp_adc_drv *adc)
1711{
1712 if (adc->hkadc_ldo)
1713 regulator_put(adc->hkadc_ldo);
1714
1715 if (adc->hkadc_ldo_ok)
1716 regulator_put(adc->hkadc_ldo_ok);
1717}
1718EXPORT_SYMBOL(qpnp_adc_free_voltage_resource);
1719
1720int qpnp_adc_get_revid_version(struct device *dev)
1721{
1722 struct pmic_revid_data *revid_data;
1723 struct device_node *revid_dev_node;
1724
1725 revid_dev_node = of_parse_phandle(dev->of_node,
1726 "qcom,pmic-revid", 0);
1727 if (!revid_dev_node) {
1728 pr_debug("Missing qcom,pmic-revid property\n");
1729 return -EINVAL;
1730 }
1731
1732 revid_data = get_revid_data(revid_dev_node);
1733 if (IS_ERR(revid_data)) {
1734 pr_debug("revid error rc = %ld\n", PTR_ERR(revid_data));
1735 return -EINVAL;
1736 }
1737
1738 if (!revid_data)
1739 return -EINVAL;
1740
1741 if ((revid_data->rev1 == PM8941_V3P1_REV1) &&
1742 (revid_data->rev2 == PM8941_V3P1_REV2) &&
1743 (revid_data->rev3 == PM8941_V3P1_REV3) &&
1744 (revid_data->rev4 == PM8941_V3P1_REV4) &&
1745 (revid_data->pmic_subtype == PM8941_SUBTYPE))
1746 return QPNP_REV_ID_8941_3_1;
1747 else if ((revid_data->rev1 == PM8941_V3P0_REV1) &&
1748 (revid_data->rev2 == PM8941_V3P0_REV2) &&
1749 (revid_data->rev3 == PM8941_V3P0_REV3) &&
1750 (revid_data->rev4 == PM8941_V3P0_REV4) &&
1751 (revid_data->pmic_subtype == PM8941_SUBTYPE))
1752 return QPNP_REV_ID_8941_3_0;
1753 else if ((revid_data->rev1 == PM8941_V2P0_REV1) &&
1754 (revid_data->rev2 == PM8941_V2P0_REV2) &&
1755 (revid_data->rev3 == PM8941_V2P0_REV3) &&
1756 (revid_data->rev4 == PM8941_V2P0_REV4) &&
1757 (revid_data->pmic_subtype == PM8941_SUBTYPE))
1758 return QPNP_REV_ID_8941_2_0;
1759 else if ((revid_data->rev1 == PM8226_V2P2_REV1) &&
1760 (revid_data->rev2 == PM8226_V2P2_REV2) &&
1761 (revid_data->rev3 == PM8226_V2P2_REV3) &&
1762 (revid_data->rev4 == PM8226_V2P2_REV4) &&
1763 (revid_data->pmic_subtype == PM8226_SUBTYPE))
1764 return QPNP_REV_ID_8026_2_2;
1765 else if ((revid_data->rev1 == PM8226_V2P1_REV1) &&
1766 (revid_data->rev2 == PM8226_V2P1_REV2) &&
1767 (revid_data->rev3 == PM8226_V2P1_REV3) &&
1768 (revid_data->rev4 == PM8226_V2P1_REV4) &&
1769 (revid_data->pmic_subtype == PM8226_SUBTYPE))
1770 return QPNP_REV_ID_8026_2_1;
1771 else if ((revid_data->rev1 == PM8226_V2P0_REV1) &&
1772 (revid_data->rev2 == PM8226_V2P0_REV2) &&
1773 (revid_data->rev3 == PM8226_V2P0_REV3) &&
1774 (revid_data->rev4 == PM8226_V2P0_REV4) &&
1775 (revid_data->pmic_subtype == PM8226_SUBTYPE))
1776 return QPNP_REV_ID_8026_2_0;
1777 else if ((revid_data->rev1 == PM8226_V1P0_REV1) &&
1778 (revid_data->rev2 == PM8226_V1P0_REV2) &&
1779 (revid_data->rev3 == PM8226_V1P0_REV3) &&
1780 (revid_data->rev4 == PM8226_V1P0_REV4) &&
1781 (revid_data->pmic_subtype == PM8226_SUBTYPE))
1782 return QPNP_REV_ID_8026_1_0;
1783 else if ((revid_data->rev1 == PM8110_V1P0_REV1) &&
1784 (revid_data->rev2 == PM8110_V1P0_REV2) &&
1785 (revid_data->rev3 == PM8110_V1P0_REV3) &&
1786 (revid_data->rev4 == PM8110_V1P0_REV4) &&
1787 (revid_data->pmic_subtype == PM8110_SUBTYPE))
1788 return QPNP_REV_ID_8110_1_0;
1789 else if ((revid_data->rev1 == PM8110_V2P0_REV1) &&
1790 (revid_data->rev2 == PM8110_V2P0_REV2) &&
1791 (revid_data->rev3 == PM8110_V2P0_REV3) &&
1792 (revid_data->rev4 == PM8110_V2P0_REV4) &&
1793 (revid_data->pmic_subtype == PM8110_SUBTYPE))
1794 return QPNP_REV_ID_8110_2_0;
1795 else if ((revid_data->rev1 == PM8916_V1P0_REV1) &&
1796 (revid_data->rev2 == PM8916_V1P0_REV2) &&
1797 (revid_data->rev3 == PM8916_V1P0_REV3) &&
1798 (revid_data->rev4 == PM8916_V1P0_REV4) &&
1799 (revid_data->pmic_subtype == PM8916_SUBTYPE))
1800 return QPNP_REV_ID_8916_1_0;
1801 else if ((revid_data->rev1 == PM8916_V1P1_REV1) &&
1802 (revid_data->rev2 == PM8916_V1P1_REV2) &&
1803 (revid_data->rev3 == PM8916_V1P1_REV3) &&
1804 (revid_data->rev4 == PM8916_V1P1_REV4) &&
1805 (revid_data->pmic_subtype == PM8916_SUBTYPE))
1806 return QPNP_REV_ID_8916_1_1;
1807 else if ((revid_data->rev1 == PM8916_V2P0_REV1) &&
1808 (revid_data->rev2 == PM8916_V2P0_REV2) &&
1809 (revid_data->rev3 == PM8916_V2P0_REV3) &&
1810 (revid_data->rev4 == PM8916_V2P0_REV4) &&
1811 (revid_data->pmic_subtype == PM8916_SUBTYPE))
1812 return QPNP_REV_ID_8916_2_0;
1813 else if ((revid_data->rev1 == PM8909_V1P0_REV1) &&
1814 (revid_data->rev2 == PM8909_V1P0_REV2) &&
1815 (revid_data->rev3 == PM8909_V1P0_REV3) &&
1816 (revid_data->rev4 == PM8909_V1P0_REV4) &&
1817 (revid_data->pmic_subtype == PM8909_SUBTYPE))
1818 return QPNP_REV_ID_8909_1_0;
1819 else if ((revid_data->rev1 == PM8909_V1P1_REV1) &&
1820 (revid_data->rev2 == PM8909_V1P1_REV2) &&
1821 (revid_data->rev3 == PM8909_V1P1_REV3) &&
1822 (revid_data->rev4 == PM8909_V1P1_REV4) &&
1823 (revid_data->pmic_subtype == PM8909_SUBTYPE))
1824 return QPNP_REV_ID_8909_1_1;
1825 else if ((revid_data->rev4 == PM8950_V1P0_REV4) &&
1826 (revid_data->pmic_subtype == PM8950_SUBTYPE))
1827 return QPNP_REV_ID_PM8950_1_0;
1828 else
1829 return -EINVAL;
1830}
1831EXPORT_SYMBOL(qpnp_adc_get_revid_version);
1832
1833int32_t qpnp_adc_get_devicetree_data(struct platform_device *pdev,
1834 struct qpnp_adc_drv *adc_qpnp)
1835{
1836 struct device_node *node = pdev->dev.of_node;
1837 unsigned int base;
1838 struct device_node *child;
1839 struct qpnp_adc_amux *adc_channel_list;
1840 struct qpnp_adc_properties *adc_prop;
1841 struct qpnp_adc_amux_properties *amux_prop;
1842 int count_adc_channel_list = 0, decimation = 0, rc = 0, i = 0;
1843 int decimation_tm_hc = 0, fast_avg_setup_tm_hc = 0, cal_val_hc = 0;
1844 bool adc_hc;
1845
1846 if (!node)
1847 return -EINVAL;
1848
1849 for_each_child_of_node(node, child)
1850 count_adc_channel_list++;
1851
1852 if (!count_adc_channel_list) {
1853 pr_err("No channel listing\n");
1854 return -EINVAL;
1855 }
1856
1857 adc_qpnp->pdev = pdev;
1858
1859 adc_prop = devm_kzalloc(&pdev->dev,
1860 sizeof(struct qpnp_adc_properties),
1861 GFP_KERNEL);
1862 if (!adc_prop)
1863 return -ENOMEM;
1864
1865 adc_channel_list = devm_kzalloc(&pdev->dev,
1866 ((sizeof(struct qpnp_adc_amux)) * count_adc_channel_list),
1867 GFP_KERNEL);
1868 if (!adc_channel_list)
1869 return -ENOMEM;
1870
1871 amux_prop = devm_kzalloc(&pdev->dev,
1872 sizeof(struct qpnp_adc_amux_properties) +
1873 sizeof(struct qpnp_vadc_chan_properties), GFP_KERNEL);
1874 if (!amux_prop) {
1875 dev_err(&pdev->dev, "Unable to allocate memory\n");
1876 return -ENOMEM;
1877 }
1878
1879 adc_qpnp->adc_channels = adc_channel_list;
1880 adc_qpnp->amux_prop = amux_prop;
1881 adc_hc = adc_qpnp->adc_hc;
1882 adc_prop->adc_hc = adc_hc;
1883
1884 if (of_device_is_compatible(node, "qcom,qpnp-adc-tm-hc")) {
1885 rc = of_property_read_u32(node, "qcom,decimation",
1886 &decimation_tm_hc);
1887 if (rc) {
1888 pr_err("Invalid decimation property\n");
1889 return -EINVAL;
1890 }
1891
1892 rc = of_property_read_u32(node,
1893 "qcom,fast-avg-setup", &fast_avg_setup_tm_hc);
1894 if (rc) {
1895 pr_err("Invalid fast average setup with %d\n", rc);
1896 return -EINVAL;
1897 }
1898
1899 if ((fast_avg_setup_tm_hc) > ADC_FAST_AVG_SAMPLE_16) {
1900 pr_err("Max average support is 2^16\n");
1901 return -EINVAL;
1902 }
1903 }
1904
1905 for_each_child_of_node(node, child) {
1906 int channel_num, scaling = 0, post_scaling = 0;
1907 int fast_avg_setup, calib_type = 0, rc, hw_settle_time = 0;
1908 const char *calibration_param, *channel_name;
1909
1910 channel_name = of_get_property(child,
1911 "label", NULL) ? : child->name;
1912 if (!channel_name) {
1913 pr_err("Invalid channel name\n");
1914 return -EINVAL;
1915 }
1916
1917 rc = of_property_read_u32(child, "reg", &channel_num);
1918 if (rc) {
1919 pr_err("Invalid channel num\n");
1920 return -EINVAL;
1921 }
1922
1923 if (!of_device_is_compatible(node, "qcom,qpnp-iadc")) {
1924 rc = of_property_read_u32(child,
1925 "qcom,hw-settle-time", &hw_settle_time);
1926 if (rc) {
1927 pr_err("Invalid channel hw settle time property\n");
1928 return -EINVAL;
1929 }
1930 rc = of_property_read_u32(child,
1931 "qcom,pre-div-channel-scaling", &scaling);
1932 if (rc) {
1933 pr_err("Invalid channel scaling property\n");
1934 return -EINVAL;
1935 }
1936 rc = of_property_read_u32(child,
1937 "qcom,scale-function", &post_scaling);
1938 if (rc) {
1939 pr_err("Invalid channel post scaling property\n");
1940 return -EINVAL;
1941 }
1942 rc = of_property_read_string(child,
1943 "qcom,calibration-type", &calibration_param);
1944 if (rc) {
1945 pr_err("Invalid calibration type\n");
1946 return -EINVAL;
1947 }
1948
1949 if (!strcmp(calibration_param, "absolute")) {
1950 if (adc_hc)
1951 calib_type = ADC_HC_ABS_CAL;
1952 else
1953 calib_type = CALIB_ABSOLUTE;
1954 } else if (!strcmp(calibration_param, "ratiometric")) {
1955 if (adc_hc)
1956 calib_type = ADC_HC_RATIO_CAL;
1957 else
1958 calib_type = CALIB_RATIOMETRIC;
1959 } else if (!strcmp(calibration_param, "no_cal")) {
1960 if (adc_hc)
1961 calib_type = ADC_HC_NO_CAL;
1962 else {
1963 pr_err("%s: Invalid calibration property\n",
1964 __func__);
1965 return -EINVAL;
1966 }
1967 } else {
1968 pr_err("%s: Invalid calibration property\n",
1969 __func__);
1970 return -EINVAL;
1971 }
1972 }
1973
1974 /* ADC_TM_HC fast avg setting is common across channels */
1975 if (!of_device_is_compatible(node, "qcom,qpnp-adc-tm-hc")) {
1976 rc = of_property_read_u32(child,
1977 "qcom,fast-avg-setup", &fast_avg_setup);
1978 if (rc) {
1979 pr_err("Invalid channel fast average setup\n");
1980 return -EINVAL;
1981 }
1982 } else {
1983 fast_avg_setup = fast_avg_setup_tm_hc;
1984 }
1985
1986 /* ADC_TM_HC decimation setting is common across channels */
1987 if (!of_device_is_compatible(node, "qcom,qpnp-adc-tm-hc")) {
1988 rc = of_property_read_u32(child,
1989 "qcom,decimation", &decimation);
1990 if (rc) {
1991 pr_err("Invalid decimation\n");
1992 return -EINVAL;
1993 }
1994 } else {
1995 decimation = decimation_tm_hc;
1996 }
1997
1998 if (of_device_is_compatible(node, "qcom,qpnp-vadc-hc")) {
1999 rc = of_property_read_u32(child, "qcom,cal-val",
2000 &cal_val_hc);
2001 if (rc) {
2002 pr_debug("Use calibration value from timer\n");
2003 adc_channel_list[i].cal_val = ADC_TIMER_CAL;
2004 } else {
2005 adc_channel_list[i].cal_val = cal_val_hc;
2006 }
2007 }
2008
2009 /* Individual channel properties */
2010 adc_channel_list[i].name = (char *)channel_name;
2011 adc_channel_list[i].channel_num = channel_num;
2012 adc_channel_list[i].adc_decimation = decimation;
2013 adc_channel_list[i].fast_avg_setup = fast_avg_setup;
2014 if (!of_device_is_compatible(node, "qcom,qpnp-iadc")) {
2015 adc_channel_list[i].chan_path_prescaling = scaling;
2016 adc_channel_list[i].adc_scale_fn = post_scaling;
2017 adc_channel_list[i].hw_settle_time = hw_settle_time;
2018 adc_channel_list[i].calib_type = calib_type;
2019 }
2020 i++;
2021 }
2022
2023 /* Get the ADC VDD reference voltage and ADC bit resolution */
2024 rc = of_property_read_u32(node, "qcom,adc-vdd-reference",
2025 &adc_prop->adc_vdd_reference);
2026 if (rc) {
2027 pr_err("Invalid adc vdd reference property\n");
2028 return -EINVAL;
2029 }
2030 rc = of_property_read_u32(node, "qcom,adc-bit-resolution",
2031 &adc_prop->bitresolution);
2032 if (rc) {
2033 pr_err("Invalid adc bit resolution property\n");
2034 return -EINVAL;
2035 }
2036 adc_qpnp->adc_prop = adc_prop;
2037
2038 /* Get the peripheral address */
2039 rc = of_property_read_u32(pdev->dev.of_node, "reg", &base);
2040 if (rc < 0) {
2041 dev_err(&pdev->dev,
2042 "Couldn't find reg in node = %s rc = %d\n",
2043 pdev->dev.of_node->full_name, rc);
2044 return rc;
2045 }
2046
2047 adc_qpnp->slave = to_spmi_device(pdev->dev.parent)->usid;
2048 adc_qpnp->offset = base;
2049
2050 /* Register the ADC peripheral interrupt */
2051 adc_qpnp->adc_irq_eoc = platform_get_irq_byname(pdev,
2052 "eoc-int-en-set");
2053 if (adc_qpnp->adc_irq_eoc < 0) {
2054 pr_err("Invalid irq\n");
2055 return -ENXIO;
2056 }
2057
2058 init_completion(&adc_qpnp->adc_rslt_completion);
2059
2060 if (of_get_property(node, "hkadc_ldo-supply", NULL)) {
2061 adc_qpnp->hkadc_ldo = regulator_get(&pdev->dev, "hkadc_ldo");
2062 if (IS_ERR(adc_qpnp->hkadc_ldo)) {
2063 pr_err("hkadc_ldo-supply node not found\n");
2064 return -EINVAL;
2065 }
2066
2067 rc = regulator_set_voltage(adc_qpnp->hkadc_ldo,
2068 QPNP_VADC_LDO_VOLTAGE_MIN,
2069 QPNP_VADC_LDO_VOLTAGE_MAX);
2070 if (rc < 0) {
2071 pr_err("setting voltage for hkadc_ldo failed\n");
2072 return rc;
2073 }
2074
2075 rc = regulator_set_load(adc_qpnp->hkadc_ldo, 100000);
2076 if (rc < 0) {
2077 pr_err("hkadc_ldo optimum mode failed%d\n", rc);
2078 return rc;
2079 }
2080 }
2081
2082 if (of_get_property(node, "hkadc_ok-supply", NULL)) {
2083 adc_qpnp->hkadc_ldo_ok = regulator_get(&pdev->dev,
2084 "hkadc_ok");
2085 if (IS_ERR(adc_qpnp->hkadc_ldo_ok)) {
2086 pr_err("hkadc_ok node not found\n");
2087 return -EINVAL;
2088 }
2089
2090 rc = regulator_set_voltage(adc_qpnp->hkadc_ldo_ok,
2091 QPNP_VADC_OK_VOLTAGE_MIN,
2092 QPNP_VADC_OK_VOLTAGE_MAX);
2093 if (rc < 0) {
2094 pr_err("setting voltage for hkadc-ldo-ok failed\n");
2095 return rc;
2096 }
2097 }
2098
2099 return 0;
2100}
2101EXPORT_SYMBOL(qpnp_adc_get_devicetree_data);