1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615 |
- /* Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 and
- * only version 2 as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- */
- #define pr_fmt(fmt) "%s: " fmt, __func__
- #include <linux/kernel.h>
- #include <linux/of.h>
- #include <linux/err.h>
- #include <linux/init.h>
- #include <linux/slab.h>
- #include <linux/delay.h>
- #include <linux/mutex.h>
- #include <linux/types.h>
- #include <linux/hwmon.h>
- #include <linux/module.h>
- #include <linux/debugfs.h>
- #include <linux/spmi.h>
- #include <linux/of_irq.h>
- #include <linux/interrupt.h>
- #include <linux/completion.h>
- #include <linux/qpnp/qpnp-adc.h>
- #include <linux/platform_device.h>
- /* Min ADC code represets 0V */
- #define QPNP_VADC_MIN_ADC_CODE 0x6000
- /* Max ADC code represents full-scale range of 1.8V */
- #define QPNP_VADC_MAX_ADC_CODE 0xA800
- #define KELVINMIL_DEGMIL 273160
- /* Units for temperature below (on x axis) is in 0.1DegC as
- required by the battery driver. Note the resolution used
- here to compute the table was done for DegC to milli-volts.
- In consideration to limit the size of the table for the given
- temperature range below, the result is linearly interpolated
- and provided to the battery driver in the units desired for
- their framework which is 0.1DegC. True resolution of 0.1DegC
- will result in the below table size to increase by 10 times */
- #if defined(CONFIG_SEC_KANAS_PROJECT)
- static const struct qpnp_vadc_map_pt adcmap_btm_threshold[] = {
- {-200, 1692},
- {-190, 1685},
- {-180, 1678},
- {-170, 1671},
- {-160, 1664},
- {-150, 1657},
- {-140, 1650},
- {-130, 1643},
- {-120, 1636},
- {-110, 1629},
- {-100, 1619},
- {-90, 1608},
- {-80, 1597},
- {-70, 1586},
- {-60, 1575},
- {-50, 1564},
- {-40, 1553},
- {-30, 1542},
- {-20, 1531},
- {-10, 1520},
- {0, 1508},
- {10, 1493},
- {20, 1478},
- {30, 1463},
- {40, 1448},
- {50, 1433},
- {60, 1418},
- {70, 1403},
- {80, 1388},
- {90, 1373},
- {100, 1358},
- {110, 1339},
- {120, 1320},
- {130, 1301},
- {140, 1282},
- {150, 1263},
- {160, 1244},
- {170, 1225},
- {180, 1206},
- {190, 1187},
- {200, 1170},
- {210, 1150},
- {220, 1130},
- {230, 1110},
- {240, 1090},
- {250, 1070},
- {260, 1050},
- {270, 1030},
- {280, 1010},
- {290, 990},
- {300, 970},
- {310, 950},
- {320, 930},
- {330, 910},
- {340, 890},
- {350, 870},
- {360, 850},
- {370, 830},
- {380, 810},
- {390, 790},
- {400, 773},
- {410, 754},
- {420, 735},
- {430, 716},
- {440, 697},
- {450, 678},
- {460, 659},
- {470, 640},
- {480, 621},
- {490, 602},
- {500, 586},
- {510, 572},
- {520, 558},
- {530, 544},
- {540, 530},
- {550, 516},
- {560, 502},
- {570, 488},
- {580, 474},
- {590, 460},
- {600, 447},
- };
- #elif defined(CONFIG_MACH_S3VE3G_EUR) || defined(CONFIG_MACH_VICTOR3GDSDTV_LTN)
- static const struct qpnp_vadc_map_pt adcmap_btm_threshold[] = {
- {-220, 1673},
- {-200, 1658},
- {-170, 1630},
- {-150, 1616},
- {-100, 1544},
- {-70, 1501},
- {-50, 1468},
- {-20, 1430},
- {0, 1394},
- {20, 1351},
- {50, 1305},
- {100, 1188},
- {150, 1077},
- {200, 974 },
- {250, 865 },
- {300, 759 },
- {320, 733 },
- {350, 675 },
- {370, 629 },
- {400, 578 },
- {420, 547 },
- {450, 508 },
- {470, 478 },
- {500, 442 },
- {520, 409 },
- {550, 376 },
- {570, 347 },
- {600, 319 },
- {620, 295 },
- {650, 269 },
- {700, 231 },
- {800, 177 },
- {820, 171 },
- {850, 151 },
- };
- #elif (defined(CONFIG_MACH_MS01_EUR_3G) || defined(CONFIG_MACH_MS01_EUR_LTE))
- /* SAMSUNG temperature table for MS01 EUR 3G */
- static const struct qpnp_vadc_map_pt adcmap_btm_threshold[] = {
- {-350, 1747},
- {-340, 1743},
- {-330, 1739},
- {-320, 1735},
- {-310, 1730},
- {-300, 1726},
- {-290, 1720},
- {-280, 1716},
- {-270, 1710},
- {-260, 1705},
- {-250, 1695},
- {-240, 1685},
- {-230, 1675},
- {-220, 1665},
- {-210, 1655},
- {-200, 1645},
- {-190, 1640},
- {-180, 1635},
- {-170, 1630},
- {-160, 1625},
- {-150, 1620},
- {-140, 1610},
- {-130, 1600},
- {-120, 1585},
- {-110, 1570},
- {-100, 1555},
- {-90, 1540},
- {-80, 1525},
- {-70, 1514},
- {-60, 1479},
- {-50, 1440},
- {-40, 1425},
- {-30, 1410},
- {-20, 1375},
- {-10, 1345},
- {0, 1330},
- {10, 1315},
- {20, 1310},
- {30, 1305},
- {40, 1302},
- {50, 1300},
- {60, 1280},
- {70, 1260},
- {80, 1220},
- {90, 1200},
- {100, 1180},
- {110, 1160},
- {120, 1135},
- {130, 1110},
- {140, 1085},
- {150, 1060},
- {160, 1035},
- {170, 1010},
- {180, 985},
- {190, 960},
- {200, 935},
- {210, 910},
- {220, 885},
- {230, 860},
- {240, 835},
- {250, 810},
- {260, 785},
- {270, 760},
- {280, 735},
- {290, 710},
- {300, 685},
- {310, 660},
- {320, 635},
- {330, 610},
- {340, 585},
- {350, 570},
- {360, 560},
- {370, 550},
- {380, 530},
- {390, 515},
- {400, 500},
- {410, 485},
- {420, 470},
- {430, 460},
- {440, 450},
- {450, 440},
- {460, 430},
- {470, 420},
- {480, 410},
- {490, 400},
- {500, 395},
- {510, 390},
- {520, 385},
- {530, 380},
- {540, 370},
- {550, 360},
- {560, 350},
- {570, 340},
- {580, 320},
- {590, 310},
- {600, 290},
- {610, 285},
- {620, 280},
- {630, 278},
- {640, 275},
- {650, 270},
- {660, 261},
- {670, 253},
- {680, 245},
- {690, 237},
- {700, 230},
- {710, 222},
- {720, 215},
- {730, 209},
- {740, 202},
- {750, 196},
- {760, 189},
- {770, 183},
- {780, 178},
- {790, 172},
- {800, 167},
- {810, 161},
- {820, 156},
- {830, 151},
- {840, 147},
- {850, 142},
- {860, 138},
- {870, 133},
- {880, 129},
- {890, 125},
- {900, 121},
- {910, 118},
- {920, 114},
- {930, 111},
- {940, 107},
- {950, 104},
- {960, 101},
- {970, 98},
- {980, 95},
- {990, 92},
- {1000, 89},
- {1010, 87},
- {1020, 84},
- {1030, 81},
- {1040, 79},
- {1050, 77},
- {1060, 74},
- {1070, 72},
- {1080, 70},
- {1090, 68},
- {1100, 66},
- {1110, 64},
- {1120, 62},
- {1130, 61},
- {1140, 59},
- {1150, 57},
- {1160, 56},
- {1170, 54},
- {1180, 52},
- {1190, 51},
- {1200, 50},
- };
- #elif defined(CONFIG_SEC_VICTOR_PROJECT)
- static const struct qpnp_vadc_map_pt adcmap_btm_threshold[] = {
- {-300, 1642},
- {-200, 1544},
- {-100, 1414},
- {0, 1260},
- {10, 1244},
- {20, 1228},
- {30, 1212},
- {40, 1195},
- {50, 1179},
- {60, 1162},
- {70, 1146},
- {80, 1129},
- {90, 1113},
- {100, 1097},
- {110, 1080},
- {120, 1064},
- {130, 1048},
- {140, 1032},
- {150, 1016},
- {160, 1000},
- {170, 985},
- {180, 969},
- {190, 954},
- {200, 939},
- {210, 924},
- {220, 909},
- {230, 894},
- {240, 880},
- {250, 866},
- {260, 852},
- {270, 838},
- {280, 824},
- {290, 811},
- {300, 798},
- {310, 785},
- {320, 773},
- {330, 760},
- {340, 748},
- {350, 735},
- {360, 725},
- {370, 713},
- {380, 702},
- {390, 691},
- {400, 681},
- {410, 670},
- {420, 660},
- {430, 650},
- {440, 640},
- {450, 631},
- {460, 622},
- {470, 613},
- {480, 604},
- {490, 595},
- {500, 587},
- {510, 579},
- {520, 571},
- {530, 563},
- {540, 556},
- {550, 548},
- {560, 541},
- {570, 534},
- {580, 527},
- {590, 521},
- {600, 514},
- {610, 508},
- {620, 502},
- {630, 496},
- {640, 490},
- {650, 485},
- {660, 281},
- {670, 274},
- {680, 267},
- {690, 260},
- {700, 254},
- {710, 247},
- {720, 241},
- {730, 235},
- {740, 229},
- {750, 224},
- {760, 218},
- {770, 213},
- {780, 208},
- {790, 203},
- };
- #else
- static const struct qpnp_vadc_map_pt adcmap_btm_threshold[] = {
- {-300, 1642},
- {-200, 1544},
- {-100, 1414},
- {0, 1260},
- {10, 1244},
- {20, 1228},
- {30, 1212},
- {40, 1195},
- {50, 1179},
- {60, 1162},
- {70, 1146},
- {80, 1129},
- {90, 1113},
- {100, 1097},
- {110, 1080},
- {120, 1064},
- {130, 1048},
- {140, 1032},
- {150, 1016},
- {160, 1000},
- {170, 985},
- {180, 969},
- {190, 954},
- {200, 939},
- {210, 924},
- {220, 909},
- {230, 894},
- {240, 880},
- {250, 866},
- {260, 852},
- {270, 838},
- {280, 824},
- {290, 811},
- {300, 798},
- {310, 785},
- {320, 773},
- {330, 760},
- {340, 748},
- {350, 736},
- {360, 725},
- {370, 713},
- {380, 702},
- {390, 691},
- {400, 681},
- {410, 670},
- {420, 660},
- {430, 650},
- {440, 640},
- {450, 631},
- {460, 622},
- {470, 613},
- {480, 604},
- {490, 595},
- {500, 587},
- {510, 579},
- {520, 571},
- {530, 563},
- {540, 556},
- {550, 548},
- {560, 541},
- {570, 534},
- {580, 527},
- {590, 521},
- {600, 514},
- {610, 508},
- {620, 502},
- {630, 496},
- {640, 490},
- {650, 485},
- {660, 281},
- {670, 274},
- {680, 267},
- {690, 260},
- {700, 254},
- {710, 247},
- {720, 241},
- {730, 235},
- {740, 229},
- {750, 224},
- {760, 218},
- {770, 213},
- {780, 208},
- {790, 203}
- };
- #endif
- static const struct qpnp_vadc_map_pt adcmap_qrd_btm_threshold[] = {
- {-200, 1540},
- {-180, 1517},
- {-160, 1492},
- {-140, 1467},
- {-120, 1440},
- {-100, 1412},
- {-80, 1383},
- {-60, 1353},
- {-40, 1323},
- {-20, 1292},
- {0, 1260},
- {20, 1228},
- {40, 1196},
- {60, 1163},
- {80, 1131},
- {100, 1098},
- {120, 1066},
- {140, 1034},
- {160, 1002},
- {180, 971},
- {200, 941},
- {220, 911},
- {240, 882},
- {260, 854},
- {280, 826},
- {300, 800},
- {320, 774},
- {340, 749},
- {360, 726},
- {380, 703},
- {400, 681},
- {420, 660},
- {440, 640},
- {460, 621},
- {480, 602},
- {500, 585},
- {520, 568},
- {540, 552},
- {560, 537},
- {580, 523},
- {600, 510},
- {620, 497},
- {640, 485},
- {660, 473},
- {680, 462},
- {700, 452},
- {720, 442},
- {740, 433},
- {760, 424},
- {780, 416},
- {800, 408},
- };
- static const struct qpnp_vadc_map_pt adcmap_qrd_skuaa_btm_threshold[] = {
- {-200, 1476},
- {-180, 1450},
- {-160, 1422},
- {-140, 1394},
- {-120, 1365},
- {-100, 1336},
- {-80, 1306},
- {-60, 1276},
- {-40, 1246},
- {-20, 1216},
- {0, 1185},
- {20, 1155},
- {40, 1126},
- {60, 1096},
- {80, 1068},
- {100, 1040},
- {120, 1012},
- {140, 986},
- {160, 960},
- {180, 935},
- {200, 911},
- {220, 888},
- {240, 866},
- {260, 844},
- {280, 824},
- {300, 805},
- {320, 786},
- {340, 769},
- {360, 752},
- {380, 737},
- {400, 722},
- {420, 707},
- {440, 694},
- {460, 681},
- {480, 669},
- {500, 658},
- {520, 648},
- {540, 637},
- {560, 628},
- {580, 619},
- {600, 611},
- {620, 603},
- {640, 595},
- {660, 588},
- {680, 582},
- {700, 575},
- {720, 569},
- {740, 564},
- {760, 559},
- {780, 554},
- {800, 549},
- };
- static const struct qpnp_vadc_map_pt adcmap_qrd_skug_btm_threshold[] = {
- {-200, 1338},
- {-180, 1307},
- {-160, 1276},
- {-140, 1244},
- {-120, 1213},
- {-100, 1182},
- {-80, 1151},
- {-60, 1121},
- {-40, 1092},
- {-20, 1063},
- {0, 1035},
- {20, 1008},
- {40, 982},
- {60, 957},
- {80, 933},
- {100, 910},
- {120, 889},
- {140, 868},
- {160, 848},
- {180, 830},
- {200, 812},
- {220, 795},
- {240, 780},
- {260, 765},
- {280, 751},
- {300, 738},
- {320, 726},
- {340, 714},
- {360, 704},
- {380, 694},
- {400, 684},
- {420, 675},
- {440, 667},
- {460, 659},
- {480, 652},
- {500, 645},
- {520, 639},
- {540, 633},
- {560, 627},
- {580, 622},
- {600, 617},
- {620, 613},
- {640, 608},
- {660, 604},
- {680, 600},
- {700, 597},
- {720, 593},
- {740, 590},
- {760, 587},
- {780, 585},
- {800, 582},
- };
- /* Voltage to temperature */
- static const struct qpnp_vadc_map_pt adcmap_100k_104ef_104fb[] = {
- {1758, -40},
- {1742, -35},
- {1719, -30},
- {1691, -25},
- {1654, -20},
- {1608, -15},
- {1551, -10},
- {1483, -5},
- {1404, 0},
- {1315, 5},
- {1218, 10},
- {1114, 15},
- {1007, 20},
- {900, 25},
- {795, 30},
- {696, 35},
- {605, 40},
- {522, 45},
- {448, 50},
- {383, 55},
- {327, 60},
- {278, 65},
- {237, 70},
- {202, 75},
- {172, 80},
- {146, 85},
- {125, 90},
- {107, 95},
- {92, 100},
- {79, 105},
- {68, 110},
- {59, 115},
- {51, 120},
- {44, 125}
- };
- /* Voltage to temperature */
- static const struct qpnp_vadc_map_pt adcmap_150k_104ef_104fb[] = {
- {1738, -40},
- {1714, -35},
- {1682, -30},
- {1641, -25},
- {1589, -20},
- {1526, -15},
- {1451, -10},
- {1363, -5},
- {1266, 0},
- {1159, 5},
- {1048, 10},
- {936, 15},
- {825, 20},
- {720, 25},
- {622, 30},
- {533, 35},
- {454, 40},
- {385, 45},
- {326, 50},
- {275, 55},
- {232, 60},
- {195, 65},
- {165, 70},
- {139, 75},
- {118, 80},
- {100, 85},
- {85, 90},
- {73, 95},
- {62, 100},
- {53, 105},
- {46, 110},
- {40, 115},
- {34, 120},
- {30, 125}
- };
- static int32_t qpnp_adc_map_voltage_temp(const struct qpnp_vadc_map_pt *pts,
- uint32_t tablesize, int32_t input, int64_t *output)
- {
- bool descending = 1;
- uint32_t i = 0;
- if (pts == NULL)
- return -EINVAL;
- /* Check if table is descending or ascending */
- if (tablesize > 1) {
- if (pts[0].x < pts[1].x)
- descending = 0;
- }
- while (i < tablesize) {
- if ((descending == 1) && (pts[i].x < input)) {
- /* table entry is less than measured
- value and table is descending, stop */
- break;
- } else if ((descending == 0) &&
- (pts[i].x > input)) {
- /* table entry is greater than measured
- value and table is ascending, stop */
- break;
- } else {
- i++;
- }
- }
- if (i == 0)
- *output = pts[0].y;
- else if (i == tablesize)
- *output = pts[tablesize-1].y;
- else {
- /* result is between search_index and search_index-1 */
- /* interpolate linearly */
- *output = (((int32_t) ((pts[i].y - pts[i-1].y)*
- (input - pts[i-1].x))/
- (pts[i].x - pts[i-1].x))+
- pts[i-1].y);
- }
- return 0;
- }
- static int32_t qpnp_adc_map_temp_voltage(const struct qpnp_vadc_map_pt *pts,
- uint32_t tablesize, int32_t input, int64_t *output)
- {
- bool descending = 1;
- uint32_t i = 0;
- if (pts == NULL)
- return -EINVAL;
- /* Check if table is descending or ascending */
- if (tablesize > 1) {
- if (pts[0].y < pts[1].y)
- descending = 0;
- }
- while (i < tablesize) {
- if ((descending == 1) && (pts[i].y < input)) {
- /* table entry is less than measured
- value and table is descending, stop */
- break;
- } else if ((descending == 0) && (pts[i].y > input)) {
- /* table entry is greater than measured
- value and table is ascending, stop */
- break;
- } else {
- i++;
- }
- }
- if (i == 0) {
- *output = pts[0].x;
- } else if (i == tablesize) {
- *output = pts[tablesize-1].x;
- } else {
- /* result is between search_index and search_index-1 */
- /* interpolate linearly */
- *output = (((int32_t) ((pts[i].x - pts[i-1].x)*
- (input - pts[i-1].y))/
- (pts[i].y - pts[i-1].y))+
- pts[i-1].x);
- }
- return 0;
- }
- static int64_t qpnp_adc_scale_ratiometric_calib(int32_t adc_code,
- const struct qpnp_adc_properties *adc_properties,
- const struct qpnp_vadc_chan_properties *chan_properties)
- {
- int64_t adc_voltage = 0;
- bool negative_offset = 0;
- if (!chan_properties || !chan_properties->offset_gain_numerator ||
- !chan_properties->offset_gain_denominator || !adc_properties)
- return -EINVAL;
- adc_voltage = (adc_code -
- chan_properties->adc_graph[CALIB_RATIOMETRIC].adc_gnd)
- * adc_properties->adc_vdd_reference;
- if (adc_voltage < 0) {
- negative_offset = 1;
- adc_voltage = -adc_voltage;
- }
- do_div(adc_voltage,
- chan_properties->adc_graph[CALIB_RATIOMETRIC].dy);
- if (negative_offset)
- adc_voltage = -adc_voltage;
- return adc_voltage;
- }
- int32_t qpnp_adc_scale_pmic_therm(struct qpnp_vadc_chip *vadc,
- int32_t adc_code,
- const struct qpnp_adc_properties *adc_properties,
- const struct qpnp_vadc_chan_properties *chan_properties,
- struct qpnp_vadc_result *adc_chan_result)
- {
- int64_t pmic_voltage = 0;
- bool negative_offset = 0;
- if (!chan_properties || !chan_properties->offset_gain_numerator ||
- !chan_properties->offset_gain_denominator || !adc_properties
- || !adc_chan_result
- || !chan_properties->adc_graph[CALIB_ABSOLUTE].dy)
- return -EINVAL;
- pmic_voltage = (adc_code -
- chan_properties->adc_graph[CALIB_ABSOLUTE].adc_gnd)
- * chan_properties->adc_graph[CALIB_ABSOLUTE].dx;
- if (pmic_voltage < 0) {
- negative_offset = 1;
- pmic_voltage = -pmic_voltage;
- }
- do_div(pmic_voltage,
- chan_properties->adc_graph[CALIB_ABSOLUTE].dy);
- if (negative_offset)
- pmic_voltage = -pmic_voltage;
- pmic_voltage += chan_properties->adc_graph[CALIB_ABSOLUTE].dx;
- if (pmic_voltage > 0) {
- /* 2mV/K */
- adc_chan_result->measurement = pmic_voltage*
- chan_properties->offset_gain_denominator;
- do_div(adc_chan_result->measurement,
- chan_properties->offset_gain_numerator * 2);
- } else {
- adc_chan_result->measurement = 0;
- }
- /* Change to .001 deg C */
- adc_chan_result->measurement -= KELVINMIL_DEGMIL;
- adc_chan_result->physical = (int32_t)adc_chan_result->measurement;
- return 0;
- }
- EXPORT_SYMBOL(qpnp_adc_scale_pmic_therm);
- int32_t qpnp_adc_scale_millidegc_pmic_voltage_thr(struct qpnp_vadc_chip *chip,
- struct qpnp_adc_tm_btm_param *param,
- uint32_t *low_threshold, uint32_t *high_threshold)
- {
- struct qpnp_vadc_linear_graph btm_param;
- int64_t low_output = 0, high_output = 0;
- int rc = 0, sign = 0;
- rc = qpnp_get_vadc_gain_and_offset(chip, &btm_param, CALIB_ABSOLUTE);
- if (rc < 0) {
- pr_err("Could not acquire gain and offset\n");
- return rc;
- }
- /* Convert to Kelvin and account for voltage to be written as 2mV/K */
- low_output = (param->low_temp + KELVINMIL_DEGMIL) * 2;
- /* Convert to voltage threshold */
- low_output = (low_output - QPNP_ADC_625_UV) * btm_param.dy;
- if (low_output < 0) {
- sign = 1;
- low_output = -low_output;
- }
- do_div(low_output, QPNP_ADC_625_UV);
- if (sign)
- low_output = -low_output;
- low_output += btm_param.adc_gnd;
- sign = 0;
- /* Convert to Kelvin and account for voltage to be written as 2mV/K */
- high_output = (param->high_temp + KELVINMIL_DEGMIL) * 2;
- /* Convert to voltage threshold */
- high_output = (high_output - QPNP_ADC_625_UV) * btm_param.dy;
- if (high_output < 0) {
- sign = 1;
- high_output = -high_output;
- }
- do_div(high_output, QPNP_ADC_625_UV);
- if (sign)
- high_output = -high_output;
- high_output += btm_param.adc_gnd;
- *low_threshold = (uint32_t) low_output;
- *high_threshold = (uint32_t) high_output;
- pr_debug("high_temp:%d, low_temp:%d\n", param->high_temp,
- param->low_temp);
- pr_debug("adc_code_high:%x, adc_code_low:%x\n", *high_threshold,
- *low_threshold);
- return 0;
- }
- EXPORT_SYMBOL(qpnp_adc_scale_millidegc_pmic_voltage_thr);
- /* Scales the ADC code to degC using the mapping
- * table for the XO thermistor.
- */
- int32_t qpnp_adc_tdkntcg_therm(struct qpnp_vadc_chip *chip,
- int32_t adc_code,
- const struct qpnp_adc_properties *adc_properties,
- const struct qpnp_vadc_chan_properties *chan_properties,
- struct qpnp_vadc_result *adc_chan_result)
- {
- int64_t xo_thm = 0;
- if (!chan_properties || !chan_properties->offset_gain_numerator ||
- !chan_properties->offset_gain_denominator || !adc_properties
- || !adc_chan_result)
- return -EINVAL;
- xo_thm = qpnp_adc_scale_ratiometric_calib(adc_code,
- adc_properties, chan_properties);
- qpnp_adc_map_voltage_temp(adcmap_100k_104ef_104fb,
- ARRAY_SIZE(adcmap_100k_104ef_104fb),
- xo_thm, &adc_chan_result->physical);
- return 0;
- }
- EXPORT_SYMBOL(qpnp_adc_tdkntcg_therm);
- int32_t qpnp_adc_scale_batt_therm(struct qpnp_vadc_chip *chip,
- int32_t adc_code,
- const struct qpnp_adc_properties *adc_properties,
- const struct qpnp_vadc_chan_properties *chan_properties,
- struct qpnp_vadc_result *adc_chan_result)
- {
- int64_t bat_voltage = 0;
- bat_voltage = qpnp_adc_scale_ratiometric_calib(adc_code,
- adc_properties, chan_properties);
- return qpnp_adc_map_temp_voltage(
- adcmap_btm_threshold,
- ARRAY_SIZE(adcmap_btm_threshold),
- bat_voltage,
- &adc_chan_result->physical);
- }
- EXPORT_SYMBOL(qpnp_adc_scale_batt_therm);
- int32_t qpnp_adc_scale_qrd_batt_therm(struct qpnp_vadc_chip *chip,
- int32_t adc_code,
- const struct qpnp_adc_properties *adc_properties,
- const struct qpnp_vadc_chan_properties *chan_properties,
- struct qpnp_vadc_result *adc_chan_result)
- {
- int64_t bat_voltage = 0;
- bat_voltage = qpnp_adc_scale_ratiometric_calib(adc_code,
- adc_properties, chan_properties);
- return qpnp_adc_map_temp_voltage(
- adcmap_qrd_btm_threshold,
- ARRAY_SIZE(adcmap_qrd_btm_threshold),
- bat_voltage,
- &adc_chan_result->physical);
- }
- EXPORT_SYMBOL(qpnp_adc_scale_qrd_batt_therm);
- int32_t qpnp_adc_scale_qrd_skuaa_batt_therm(struct qpnp_vadc_chip *chip,
- int32_t adc_code,
- const struct qpnp_adc_properties *adc_properties,
- const struct qpnp_vadc_chan_properties *chan_properties,
- struct qpnp_vadc_result *adc_chan_result)
- {
- int64_t bat_voltage = 0;
- bat_voltage = qpnp_adc_scale_ratiometric_calib(adc_code,
- adc_properties, chan_properties);
- return qpnp_adc_map_temp_voltage(
- adcmap_qrd_skuaa_btm_threshold,
- ARRAY_SIZE(adcmap_qrd_skuaa_btm_threshold),
- bat_voltage,
- &adc_chan_result->physical);
- }
- EXPORT_SYMBOL(qpnp_adc_scale_qrd_skuaa_batt_therm);
- int32_t qpnp_adc_scale_qrd_skug_batt_therm(struct qpnp_vadc_chip *chip,
- int32_t adc_code,
- const struct qpnp_adc_properties *adc_properties,
- const struct qpnp_vadc_chan_properties *chan_properties,
- struct qpnp_vadc_result *adc_chan_result)
- {
- int64_t bat_voltage = 0;
- bat_voltage = qpnp_adc_scale_ratiometric_calib(adc_code,
- adc_properties, chan_properties);
- return qpnp_adc_map_temp_voltage(
- adcmap_qrd_skug_btm_threshold,
- ARRAY_SIZE(adcmap_qrd_skug_btm_threshold),
- bat_voltage,
- &adc_chan_result->physical);
- }
- EXPORT_SYMBOL(qpnp_adc_scale_qrd_skug_batt_therm);
- int32_t qpnp_adc_scale_therm_pu1(struct qpnp_vadc_chip *chip,
- int32_t adc_code,
- const struct qpnp_adc_properties *adc_properties,
- const struct qpnp_vadc_chan_properties *chan_properties,
- struct qpnp_vadc_result *adc_chan_result)
- {
- int64_t therm_voltage = 0;
- therm_voltage = qpnp_adc_scale_ratiometric_calib(adc_code,
- adc_properties, chan_properties);
- qpnp_adc_map_voltage_temp(adcmap_150k_104ef_104fb,
- ARRAY_SIZE(adcmap_150k_104ef_104fb),
- therm_voltage, &adc_chan_result->physical);
- return 0;
- }
- EXPORT_SYMBOL(qpnp_adc_scale_therm_pu1);
- int32_t qpnp_adc_scale_therm_pu2(struct qpnp_vadc_chip *chip,
- int32_t adc_code,
- const struct qpnp_adc_properties *adc_properties,
- const struct qpnp_vadc_chan_properties *chan_properties,
- struct qpnp_vadc_result *adc_chan_result)
- {
- int64_t therm_voltage = 0;
- therm_voltage = qpnp_adc_scale_ratiometric_calib(adc_code,
- adc_properties, chan_properties);
- qpnp_adc_map_voltage_temp(adcmap_100k_104ef_104fb,
- ARRAY_SIZE(adcmap_100k_104ef_104fb),
- therm_voltage, &adc_chan_result->physical);
- return 0;
- }
- EXPORT_SYMBOL(qpnp_adc_scale_therm_pu2);
- int32_t qpnp_adc_tm_scale_voltage_therm_pu2(struct qpnp_vadc_chip *chip,
- uint32_t reg, int64_t *result)
- {
- int64_t adc_voltage = 0;
- struct qpnp_vadc_linear_graph param1;
- int negative_offset;
- qpnp_get_vadc_gain_and_offset(chip, ¶m1, CALIB_RATIOMETRIC);
- adc_voltage = (reg - param1.adc_gnd) * param1.adc_vref;
- if (adc_voltage < 0) {
- negative_offset = 1;
- adc_voltage = -adc_voltage;
- }
- do_div(adc_voltage, param1.dy);
- qpnp_adc_map_voltage_temp(adcmap_100k_104ef_104fb,
- ARRAY_SIZE(adcmap_100k_104ef_104fb),
- adc_voltage, result);
- if (negative_offset)
- adc_voltage = -adc_voltage;
- return 0;
- }
- EXPORT_SYMBOL(qpnp_adc_tm_scale_voltage_therm_pu2);
- int32_t qpnp_adc_tm_scale_therm_voltage_pu2(struct qpnp_vadc_chip *chip,
- struct qpnp_adc_tm_config *param)
- {
- struct qpnp_vadc_linear_graph param1;
- int rc;
- qpnp_get_vadc_gain_and_offset(chip, ¶m1, CALIB_RATIOMETRIC);
- rc = qpnp_adc_map_temp_voltage(adcmap_100k_104ef_104fb,
- ARRAY_SIZE(adcmap_100k_104ef_104fb),
- param->low_thr_temp, ¶m->low_thr_voltage);
- if (rc)
- return rc;
- param->low_thr_voltage *= param1.dy;
- do_div(param->low_thr_voltage, param1.adc_vref);
- param->low_thr_voltage += param1.adc_gnd;
- rc = qpnp_adc_map_temp_voltage(adcmap_100k_104ef_104fb,
- ARRAY_SIZE(adcmap_100k_104ef_104fb),
- param->high_thr_temp, ¶m->high_thr_voltage);
- if (rc)
- return rc;
- param->high_thr_voltage *= param1.dy;
- do_div(param->high_thr_voltage, param1.adc_vref);
- param->high_thr_voltage += param1.adc_gnd;
- return 0;
- }
- EXPORT_SYMBOL(qpnp_adc_tm_scale_therm_voltage_pu2);
- int32_t qpnp_adc_scale_batt_id(struct qpnp_vadc_chip *chip,
- int32_t adc_code,
- const struct qpnp_adc_properties *adc_properties,
- const struct qpnp_vadc_chan_properties *chan_properties,
- struct qpnp_vadc_result *adc_chan_result)
- {
- int64_t batt_id_voltage = 0;
- batt_id_voltage = qpnp_adc_scale_ratiometric_calib(adc_code,
- adc_properties, chan_properties);
- adc_chan_result->physical = batt_id_voltage;
- adc_chan_result->physical = adc_chan_result->measurement;
- return 0;
- }
- EXPORT_SYMBOL(qpnp_adc_scale_batt_id);
- int32_t qpnp_adc_scale_default(struct qpnp_vadc_chip *vadc,
- int32_t adc_code,
- const struct qpnp_adc_properties *adc_properties,
- const struct qpnp_vadc_chan_properties *chan_properties,
- struct qpnp_vadc_result *adc_chan_result)
- {
- bool negative_rawfromoffset = 0, negative_offset = 0;
- int64_t scale_voltage = 0;
- if (!chan_properties || !chan_properties->offset_gain_numerator ||
- !chan_properties->offset_gain_denominator || !adc_properties
- || !adc_chan_result)
- return -EINVAL;
- scale_voltage = (adc_code -
- chan_properties->adc_graph[chan_properties->calib_type].adc_gnd)
- * chan_properties->adc_graph[chan_properties->calib_type].dx;
- if (scale_voltage < 0) {
- negative_offset = 1;
- scale_voltage = -scale_voltage;
- }
- do_div(scale_voltage,
- chan_properties->adc_graph[chan_properties->calib_type].dy);
- if (negative_offset)
- scale_voltage = -scale_voltage;
- if (chan_properties->calib_type == CALIB_ABSOLUTE)
- scale_voltage +=
- chan_properties->adc_graph[chan_properties->calib_type].dx;
- else
- scale_voltage *= 1000;
- if (scale_voltage < 0) {
- if (adc_properties->bipolar) {
- scale_voltage = -scale_voltage;
- negative_rawfromoffset = 1;
- } else {
- scale_voltage = 0;
- }
- }
- adc_chan_result->measurement = scale_voltage *
- chan_properties->offset_gain_denominator;
- /* do_div only perform positive integer division! */
- do_div(adc_chan_result->measurement,
- chan_properties->offset_gain_numerator);
- if (negative_rawfromoffset)
- adc_chan_result->measurement = -adc_chan_result->measurement;
- /*
- * Note: adc_chan_result->measurement is in the unit of
- * adc_properties.adc_reference. For generic channel processing,
- * channel measurement is a scale/ratio relative to the adc
- * reference input
- */
- adc_chan_result->physical = adc_chan_result->measurement;
- return 0;
- }
- EXPORT_SYMBOL(qpnp_adc_scale_default);
- int32_t qpnp_adc_usb_scaler(struct qpnp_vadc_chip *chip,
- struct qpnp_adc_tm_btm_param *param,
- uint32_t *low_threshold, uint32_t *high_threshold)
- {
- struct qpnp_vadc_linear_graph usb_param;
- qpnp_get_vadc_gain_and_offset(chip, &usb_param, CALIB_RATIOMETRIC);
- *low_threshold = param->low_thr * usb_param.dy;
- do_div(*low_threshold, usb_param.adc_vref);
- *low_threshold += usb_param.adc_gnd;
- *high_threshold = param->high_thr * usb_param.dy;
- do_div(*high_threshold, usb_param.adc_vref);
- *high_threshold += usb_param.adc_gnd;
- pr_debug("high_volt:%d, low_volt:%d\n", param->high_thr,
- param->low_thr);
- return 0;
- }
- EXPORT_SYMBOL(qpnp_adc_usb_scaler);
- int32_t qpnp_adc_vbatt_rscaler(struct qpnp_vadc_chip *chip,
- struct qpnp_adc_tm_btm_param *param,
- uint32_t *low_threshold, uint32_t *high_threshold)
- {
- struct qpnp_vadc_linear_graph vbatt_param;
- int rc = 0, sign = 0;
- int64_t low_thr = 0, high_thr = 0;
- rc = qpnp_get_vadc_gain_and_offset(chip, &vbatt_param, CALIB_ABSOLUTE);
- if (rc < 0)
- return rc;
- low_thr = (((param->low_thr/3) - QPNP_ADC_625_UV) *
- vbatt_param.dy);
- if (low_thr < 0) {
- sign = 1;
- low_thr = -low_thr;
- }
- do_div(low_thr, QPNP_ADC_625_UV);
- if (sign)
- low_thr = -low_thr;
- *low_threshold = low_thr + vbatt_param.adc_gnd;
- sign = 0;
- high_thr = (((param->high_thr/3) - QPNP_ADC_625_UV) *
- vbatt_param.dy);
- if (high_thr < 0) {
- sign = 1;
- high_thr = -high_thr;
- }
- do_div(high_thr, QPNP_ADC_625_UV);
- if (sign)
- high_thr = -high_thr;
- *high_threshold = high_thr + vbatt_param.adc_gnd;
- pr_debug("high_volt:%d, low_volt:%d\n", param->high_thr,
- param->low_thr);
- pr_debug("adc_code_high:%x, adc_code_low:%x\n", *high_threshold,
- *low_threshold);
- return 0;
- }
- EXPORT_SYMBOL(qpnp_adc_vbatt_rscaler);
- int32_t qpnp_adc_btm_scaler(struct qpnp_vadc_chip *chip,
- struct qpnp_adc_tm_btm_param *param,
- uint32_t *low_threshold, uint32_t *high_threshold)
- {
- struct qpnp_vadc_linear_graph btm_param;
- int64_t low_output = 0, high_output = 0;
- int rc = 0;
- qpnp_get_vadc_gain_and_offset(chip, &btm_param, CALIB_RATIOMETRIC);
- pr_debug("warm_temp:%d and cool_temp:%d\n", param->high_temp,
- param->low_temp);
- rc = qpnp_adc_map_voltage_temp(
- adcmap_btm_threshold,
- ARRAY_SIZE(adcmap_btm_threshold),
- (param->low_temp),
- &low_output);
- if (rc) {
- pr_debug("low_temp mapping failed with %d\n", rc);
- return rc;
- }
- pr_debug("low_output:%lld\n", low_output);
- low_output *= btm_param.dy;
- do_div(low_output, btm_param.adc_vref);
- low_output += btm_param.adc_gnd;
- rc = qpnp_adc_map_voltage_temp(
- adcmap_btm_threshold,
- ARRAY_SIZE(adcmap_btm_threshold),
- (param->high_temp),
- &high_output);
- if (rc) {
- pr_debug("high temp mapping failed with %d\n", rc);
- return rc;
- }
- pr_debug("high_output:%lld\n", high_output);
- high_output *= btm_param.dy;
- do_div(high_output, btm_param.adc_vref);
- high_output += btm_param.adc_gnd;
- /* btm low temperature correspondes to high voltage threshold */
- *low_threshold = high_output;
- /* btm high temperature correspondes to low voltage threshold */
- *high_threshold = low_output;
- pr_debug("high_volt:%d, low_volt:%d\n", *high_threshold,
- *low_threshold);
- return 0;
- }
- EXPORT_SYMBOL(qpnp_adc_btm_scaler);
- int32_t qpnp_vadc_check_result(int32_t *data)
- {
- if (*data < QPNP_VADC_MIN_ADC_CODE)
- *data = QPNP_VADC_MIN_ADC_CODE;
- else if (*data > QPNP_VADC_MAX_ADC_CODE)
- *data = QPNP_VADC_MAX_ADC_CODE;
- return 0;
- }
- EXPORT_SYMBOL(qpnp_vadc_check_result);
- int qpnp_adc_get_revid_version(struct device *dev)
- {
- struct pmic_revid_data *revid_data;
- struct device_node *revid_dev_node;
- revid_dev_node = of_parse_phandle(dev->of_node,
- "qcom,pmic-revid", 0);
- if (!revid_dev_node) {
- pr_debug("Missing qcom,pmic-revid property\n");
- return -EINVAL;
- }
- revid_data = get_revid_data(revid_dev_node);
- if (IS_ERR(revid_data)) {
- pr_debug("revid error rc = %ld\n", PTR_ERR(revid_data));
- return -EINVAL;
- }
- if ((revid_data->rev1 == PM8941_V3P1_REV1) &&
- (revid_data->rev2 == PM8941_V3P1_REV2) &&
- (revid_data->rev3 == PM8941_V3P1_REV3) &&
- (revid_data->rev4 == PM8941_V3P1_REV4) &&
- (revid_data->pmic_type == PM8941_V3P1_TYPE) &&
- (revid_data->pmic_subtype == PM8941_V3P1_SUBTYPE))
- return QPNP_REV_ID_8941_3_1;
- else if ((revid_data->rev1 == PM8941_V3P0_REV1) &&
- (revid_data->rev2 == PM8941_V3P0_REV2) &&
- (revid_data->rev3 == PM8941_V3P0_REV3) &&
- (revid_data->rev4 == PM8941_V3P0_REV4) &&
- (revid_data->pmic_type == PM8941_V3P0_TYPE) &&
- (revid_data->pmic_subtype == PM8941_V3P0_SUBTYPE))
- return QPNP_REV_ID_8941_3_0;
- else if ((revid_data->rev1 == PM8941_V2P0_REV1) &&
- (revid_data->rev2 == PM8941_V2P0_REV2) &&
- (revid_data->rev3 == PM8941_V2P0_REV3) &&
- (revid_data->rev4 == PM8941_V2P0_REV4) &&
- (revid_data->pmic_type == PM8941_V2P0_TYPE) &&
- (revid_data->pmic_subtype == PM8941_V2P0_SUBTYPE))
- return QPNP_REV_ID_8941_2_0;
- else if ((revid_data->rev1 == PM8226_V2P2_REV1) &&
- (revid_data->rev2 == PM8226_V2P2_REV2) &&
- (revid_data->rev3 == PM8226_V2P2_REV3) &&
- (revid_data->rev4 == PM8226_V2P2_REV4) &&
- (revid_data->pmic_type == PM8226_V2P2_TYPE) &&
- (revid_data->pmic_subtype == PM8226_V2P2_SUBTYPE))
- return QPNP_REV_ID_8026_2_2;
- else if ((revid_data->rev1 == PM8226_V2P1_REV1) &&
- (revid_data->rev2 == PM8226_V2P1_REV2) &&
- (revid_data->rev3 == PM8226_V2P1_REV3) &&
- (revid_data->rev4 == PM8226_V2P1_REV4) &&
- (revid_data->pmic_type == PM8226_V2P1_TYPE) &&
- (revid_data->pmic_subtype == PM8226_V2P1_SUBTYPE))
- return QPNP_REV_ID_8026_2_1;
- else if ((revid_data->rev1 == PM8226_V2P0_REV1) &&
- (revid_data->rev2 == PM8226_V2P0_REV2) &&
- (revid_data->rev3 == PM8226_V2P0_REV3) &&
- (revid_data->rev4 == PM8226_V2P0_REV4) &&
- (revid_data->pmic_type == PM8226_V2P0_TYPE) &&
- (revid_data->pmic_subtype == PM8226_V2P0_SUBTYPE))
- return QPNP_REV_ID_8026_2_0;
- else if ((revid_data->rev1 == PM8226_V1P0_REV1) &&
- (revid_data->rev2 == PM8226_V1P0_REV2) &&
- (revid_data->rev3 == PM8226_V1P0_REV3) &&
- (revid_data->rev4 == PM8226_V1P0_REV4) &&
- (revid_data->pmic_type == PM8226_V1P0_TYPE) &&
- (revid_data->pmic_subtype == PM8226_V1P0_SUBTYPE))
- return QPNP_REV_ID_8026_1_0;
- else if ((revid_data->rev1 == PM8110_V1P0_REV1) &&
- (revid_data->rev2 == PM8110_V1P0_REV2) &&
- (revid_data->rev3 == PM8110_V1P0_REV3) &&
- (revid_data->rev4 == PM8110_V1P0_REV4) &&
- (revid_data->pmic_type == PM8110_V1P0_TYPE) &&
- (revid_data->pmic_subtype == PM8110_V1P0_SUBTYPE))
- return QPNP_REV_ID_8110_1_0;
- else if ((revid_data->rev1 == PM8110_V2P0_REV1) &&
- (revid_data->rev2 == PM8110_V2P0_REV2) &&
- (revid_data->rev3 == PM8110_V2P0_REV3) &&
- (revid_data->rev4 == PM8110_V2P0_REV4) &&
- (revid_data->pmic_type == PM8110_V2P0_TYPE) &&
- (revid_data->pmic_subtype == PM8110_V2P0_SUBTYPE))
- return QPNP_REV_ID_8110_2_0;
- else
- return -EINVAL;
- }
- EXPORT_SYMBOL(qpnp_adc_get_revid_version);
- int32_t qpnp_adc_get_devicetree_data(struct spmi_device *spmi,
- struct qpnp_adc_drv *adc_qpnp)
- {
- struct device_node *node = spmi->dev.of_node;
- struct resource *res;
- struct device_node *child;
- struct qpnp_adc_amux *adc_channel_list;
- struct qpnp_adc_properties *adc_prop;
- struct qpnp_adc_amux_properties *amux_prop;
- int count_adc_channel_list = 0, decimation, rc = 0, i = 0;
- if (!node)
- return -EINVAL;
- for_each_child_of_node(node, child)
- count_adc_channel_list++;
- if (!count_adc_channel_list) {
- pr_err("No channel listing\n");
- return -EINVAL;
- }
- adc_qpnp->spmi = spmi;
- adc_prop = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_adc_properties),
- GFP_KERNEL);
- if (!adc_prop) {
- dev_err(&spmi->dev, "Unable to allocate memory\n");
- return -ENOMEM;
- }
- adc_channel_list = devm_kzalloc(&spmi->dev,
- ((sizeof(struct qpnp_adc_amux)) * count_adc_channel_list),
- GFP_KERNEL);
- if (!adc_channel_list) {
- dev_err(&spmi->dev, "Unable to allocate memory\n");
- return -ENOMEM;
- }
- amux_prop = devm_kzalloc(&spmi->dev,
- sizeof(struct qpnp_adc_amux_properties) +
- sizeof(struct qpnp_vadc_chan_properties), GFP_KERNEL);
- if (!amux_prop) {
- dev_err(&spmi->dev, "Unable to allocate memory\n");
- return -ENOMEM;
- }
- adc_qpnp->adc_channels = adc_channel_list;
- adc_qpnp->amux_prop = amux_prop;
- for_each_child_of_node(node, child) {
- int channel_num, scaling, post_scaling, hw_settle_time;
- int fast_avg_setup, calib_type = 0, rc;
- const char *calibration_param, *channel_name;
- channel_name = of_get_property(child,
- "label", NULL) ? : child->name;
- if (!channel_name) {
- pr_err("Invalid channel name\n");
- return -EINVAL;
- }
- rc = of_property_read_u32(child, "reg", &channel_num);
- if (rc) {
- pr_err("Invalid channel num\n");
- return -EINVAL;
- }
- rc = of_property_read_u32(child, "qcom,decimation",
- &decimation);
- if (rc) {
- pr_err("Invalid channel decimation property\n");
- return -EINVAL;
- }
- if (!of_device_is_compatible(node, "qcom,qpnp-iadc")) {
- rc = of_property_read_u32(child,
- "qcom,hw-settle-time", &hw_settle_time);
- if (rc) {
- pr_err("Invalid channel hw settle time property\n");
- return -EINVAL;
- }
- rc = of_property_read_u32(child,
- "qcom,pre-div-channel-scaling", &scaling);
- if (rc) {
- pr_err("Invalid channel scaling property\n");
- return -EINVAL;
- }
- rc = of_property_read_u32(child,
- "qcom,scale-function", &post_scaling);
- if (rc) {
- pr_err("Invalid channel post scaling property\n");
- return -EINVAL;
- }
- rc = of_property_read_string(child,
- "qcom,calibration-type", &calibration_param);
- if (rc) {
- pr_err("Invalid calibration type\n");
- return -EINVAL;
- }
- if (!strcmp(calibration_param, "absolute"))
- calib_type = CALIB_ABSOLUTE;
- else if (!strcmp(calibration_param, "ratiometric"))
- calib_type = CALIB_RATIOMETRIC;
- else {
- pr_err("%s: Invalid calibration property\n",
- __func__);
- return -EINVAL;
- }
- }
- rc = of_property_read_u32(child,
- "qcom,fast-avg-setup", &fast_avg_setup);
- if (rc) {
- pr_err("Invalid channel fast average setup\n");
- return -EINVAL;
- }
- /* Individual channel properties */
- adc_channel_list[i].name = (char *)channel_name;
- adc_channel_list[i].channel_num = channel_num;
- adc_channel_list[i].adc_decimation = decimation;
- adc_channel_list[i].fast_avg_setup = fast_avg_setup;
- if (!of_device_is_compatible(node, "qcom,qpnp-iadc")) {
- adc_channel_list[i].chan_path_prescaling = scaling;
- adc_channel_list[i].adc_scale_fn = post_scaling;
- adc_channel_list[i].hw_settle_time = hw_settle_time;
- adc_channel_list[i].calib_type = calib_type;
- }
- i++;
- }
- /* Get the ADC VDD reference voltage and ADC bit resolution */
- rc = of_property_read_u32(node, "qcom,adc-vdd-reference",
- &adc_prop->adc_vdd_reference);
- if (rc) {
- pr_err("Invalid adc vdd reference property\n");
- return -EINVAL;
- }
- rc = of_property_read_u32(node, "qcom,adc-bit-resolution",
- &adc_prop->bitresolution);
- if (rc) {
- pr_err("Invalid adc bit resolution property\n");
- return -EINVAL;
- }
- adc_qpnp->adc_prop = adc_prop;
- /* Get the peripheral address */
- res = spmi_get_resource(spmi, 0, IORESOURCE_MEM, 0);
- if (!res) {
- pr_err("No base address definition\n");
- return -EINVAL;
- }
- adc_qpnp->slave = spmi->sid;
- adc_qpnp->offset = res->start;
- /* Register the ADC peripheral interrupt */
- adc_qpnp->adc_irq_eoc = spmi_get_irq_byname(spmi, NULL,
- "eoc-int-en-set");
- if (adc_qpnp->adc_irq_eoc < 0) {
- pr_err("Invalid irq\n");
- return -ENXIO;
- }
- init_completion(&adc_qpnp->adc_rslt_completion);
- return 0;
- }
- EXPORT_SYMBOL(qpnp_adc_get_devicetree_data);
|