12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395 |
- /* Copyright (c) 2012-2018, 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/regmap.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/platform_device.h>
- #include <linux/of_irq.h>
- #include <linux/interrupt.h>
- #include <linux/completion.h>
- #include <linux/hwmon-sysfs.h>
- #include <linux/qpnp/qpnp-adc.h>
- #include <linux/thermal.h>
- #include <linux/platform_device.h>
- #include "thermal_core.h"
- /* QPNP VADC TM register definition */
- #define QPNP_REVISION3 0x2
- #define QPNP_PERPH_SUBTYPE 0x5
- #define QPNP_PERPH_TYPE2 0x2
- #define QPNP_REVISION_EIGHT_CHANNEL_SUPPORT 2
- #define QPNP_PERPH_SUBTYPE_TWO_CHANNEL_SUPPORT 0x22
- #define QPNP_STATUS1 0x8
- #define QPNP_STATUS1_OP_MODE 4
- #define QPNP_STATUS1_MEAS_INTERVAL_EN_STS BIT(2)
- #define QPNP_STATUS1_REQ_STS BIT(1)
- #define QPNP_STATUS1_EOC BIT(0)
- #define QPNP_STATUS2 0x9
- #define QPNP_STATUS2_CONV_SEQ_STATE 6
- #define QPNP_STATUS2_FIFO_NOT_EMPTY_FLAG BIT(1)
- #define QPNP_STATUS2_CONV_SEQ_TIMEOUT_STS BIT(0)
- #define QPNP_CONV_TIMEOUT_ERR 2
- #define QPNP_MODE_CTL 0x40
- #define QPNP_OP_MODE_SHIFT 3
- #define QPNP_VREF_XO_THM_FORCE BIT(2)
- #define QPNP_AMUX_TRIM_EN BIT(1)
- #define QPNP_ADC_TRIM_EN BIT(0)
- #define QPNP_EN_CTL1 0x46
- #define QPNP_ADC_TM_EN BIT(7)
- #define QPNP_BTM_CONV_REQ 0x47
- #define QPNP_ADC_CONV_REQ_EN BIT(7)
- #define QPNP_ADC_DIG_PARAM 0x50
- #define QPNP_ADC_DIG_DEC_RATIO_SEL_SHIFT 3
- #define QPNP_HW_SETTLE_DELAY 0x51
- #define QPNP_CONV_SEQ_CTL 0x54
- #define QPNP_CONV_SEQ_HOLDOFF_SHIFT 4
- #define QPNP_CONV_SEQ_TRIG_CTL 0x55
- #define QPNP_ADC_TM_MEAS_INTERVAL_CTL 0x57
- #define QPNP_ADC_TM_MEAS_INTERVAL_TIME_SHIFT 0x3
- #define QPNP_ADC_TM_MEAS_INTERVAL_CTL2 0x58
- #define QPNP_ADC_TM_MEAS_INTERVAL_CTL2_SHIFT 0x4
- #define QPNP_ADC_TM_MEAS_INTERVAL_CTL2_MASK 0xf0
- #define QPNP_ADC_TM_MEAS_INTERVAL_CTL3_MASK 0xf
- #define QPNP_ADC_MEAS_INTERVAL_OP_CTL 0x59
- #define QPNP_ADC_MEAS_INTERVAL_OP BIT(7)
- #define QPNP_OP_MODE_SHIFT 3
- #define QPNP_CONV_REQ 0x52
- #define QPNP_CONV_REQ_SET BIT(7)
- #define QPNP_FAST_AVG_CTL 0x5a
- #define QPNP_FAST_AVG_EN 0x5b
- #define QPNP_FAST_AVG_ENABLED BIT(7)
- #define QPNP_M0_LOW_THR_LSB 0x5c
- #define QPNP_M0_LOW_THR_MSB 0x5d
- #define QPNP_M0_HIGH_THR_LSB 0x5e
- #define QPNP_M0_HIGH_THR_MSB 0x5f
- #define QPNP_M1_ADC_CH_SEL_CTL 0x68
- #define QPNP_M1_LOW_THR_LSB 0x69
- #define QPNP_M1_LOW_THR_MSB 0x6a
- #define QPNP_M1_HIGH_THR_LSB 0x6b
- #define QPNP_M1_HIGH_THR_MSB 0x6c
- #define QPNP_M2_ADC_CH_SEL_CTL 0x70
- #define QPNP_M2_LOW_THR_LSB 0x71
- #define QPNP_M2_LOW_THR_MSB 0x72
- #define QPNP_M2_HIGH_THR_LSB 0x73
- #define QPNP_M2_HIGH_THR_MSB 0x74
- #define QPNP_M3_ADC_CH_SEL_CTL 0x78
- #define QPNP_M3_LOW_THR_LSB 0x79
- #define QPNP_M3_LOW_THR_MSB 0x7a
- #define QPNP_M3_HIGH_THR_LSB 0x7b
- #define QPNP_M3_HIGH_THR_MSB 0x7c
- #define QPNP_M4_ADC_CH_SEL_CTL 0x80
- #define QPNP_M4_LOW_THR_LSB 0x81
- #define QPNP_M4_LOW_THR_MSB 0x82
- #define QPNP_M4_HIGH_THR_LSB 0x83
- #define QPNP_M4_HIGH_THR_MSB 0x84
- #define QPNP_M5_ADC_CH_SEL_CTL 0x88
- #define QPNP_M5_LOW_THR_LSB 0x89
- #define QPNP_M5_LOW_THR_MSB 0x8a
- #define QPNP_M5_HIGH_THR_LSB 0x8b
- #define QPNP_M5_HIGH_THR_MSB 0x8c
- #define QPNP_M6_ADC_CH_SEL_CTL 0x90
- #define QPNP_M6_LOW_THR_LSB 0x91
- #define QPNP_M6_LOW_THR_MSB 0x92
- #define QPNP_M6_HIGH_THR_LSB 0x93
- #define QPNP_M6_HIGH_THR_MSB 0x94
- #define QPNP_M7_ADC_CH_SEL_CTL 0x98
- #define QPNP_M7_LOW_THR_LSB 0x99
- #define QPNP_M7_LOW_THR_MSB 0x9a
- #define QPNP_M7_HIGH_THR_LSB 0x9b
- #define QPNP_M7_HIGH_THR_MSB 0x9c
- #define QPNP_ADC_TM_MULTI_MEAS_EN 0x41
- #define QPNP_ADC_TM_MULTI_MEAS_EN_M0 BIT(0)
- #define QPNP_ADC_TM_MULTI_MEAS_EN_M1 BIT(1)
- #define QPNP_ADC_TM_MULTI_MEAS_EN_M2 BIT(2)
- #define QPNP_ADC_TM_MULTI_MEAS_EN_M3 BIT(3)
- #define QPNP_ADC_TM_MULTI_MEAS_EN_M4 BIT(4)
- #define QPNP_ADC_TM_MULTI_MEAS_EN_M5 BIT(5)
- #define QPNP_ADC_TM_MULTI_MEAS_EN_M6 BIT(6)
- #define QPNP_ADC_TM_MULTI_MEAS_EN_M7 BIT(7)
- #define QPNP_ADC_TM_LOW_THR_INT_EN 0x42
- #define QPNP_ADC_TM_LOW_THR_INT_EN_M0 BIT(0)
- #define QPNP_ADC_TM_LOW_THR_INT_EN_M1 BIT(1)
- #define QPNP_ADC_TM_LOW_THR_INT_EN_M2 BIT(2)
- #define QPNP_ADC_TM_LOW_THR_INT_EN_M3 BIT(3)
- #define QPNP_ADC_TM_LOW_THR_INT_EN_M4 BIT(4)
- #define QPNP_ADC_TM_LOW_THR_INT_EN_M5 BIT(5)
- #define QPNP_ADC_TM_LOW_THR_INT_EN_M6 BIT(6)
- #define QPNP_ADC_TM_LOW_THR_INT_EN_M7 BIT(7)
- #define QPNP_ADC_TM_HIGH_THR_INT_EN 0x43
- #define QPNP_ADC_TM_HIGH_THR_INT_EN_M0 BIT(0)
- #define QPNP_ADC_TM_HIGH_THR_INT_EN_M1 BIT(1)
- #define QPNP_ADC_TM_HIGH_THR_INT_EN_M2 BIT(2)
- #define QPNP_ADC_TM_HIGH_THR_INT_EN_M3 BIT(3)
- #define QPNP_ADC_TM_HIGH_THR_INT_EN_M4 BIT(4)
- #define QPNP_ADC_TM_HIGH_THR_INT_EN_M5 BIT(5)
- #define QPNP_ADC_TM_HIGH_THR_INT_EN_M6 BIT(6)
- #define QPNP_ADC_TM_HIGH_THR_INT_EN_M7 BIT(7)
- #define QPNP_ADC_TM_M0_MEAS_INTERVAL_CTL 0x59
- #define QPNP_ADC_TM_M1_MEAS_INTERVAL_CTL 0x6d
- #define QPNP_ADC_TM_M2_MEAS_INTERVAL_CTL 0x75
- #define QPNP_ADC_TM_M3_MEAS_INTERVAL_CTL 0x7d
- #define QPNP_ADC_TM_M4_MEAS_INTERVAL_CTL 0x85
- #define QPNP_ADC_TM_M5_MEAS_INTERVAL_CTL 0x8d
- #define QPNP_ADC_TM_M6_MEAS_INTERVAL_CTL 0x95
- #define QPNP_ADC_TM_M7_MEAS_INTERVAL_CTL 0x9d
- #define QPNP_ADC_TM_STATUS1 0x8
- #define QPNP_ADC_TM_STATUS_LOW 0xa
- #define QPNP_ADC_TM_STATUS_HIGH 0xb
- #define QPNP_ADC_TM_M0_LOW_THR 0x5d5c
- #define QPNP_ADC_TM_M0_HIGH_THR 0x5f5e
- #define QPNP_ADC_TM_MEAS_INTERVAL 0x0
- #define QPNP_ADC_TM_THR_LSB_MASK(val) (val & 0xff)
- #define QPNP_ADC_TM_THR_MSB_MASK(val) ((val & 0xff00) >> 8)
- #define QPNP_MIN_TIME 2000
- #define QPNP_MAX_TIME 2100
- #define QPNP_RETRY 1000
- /* QPNP ADC TM HC start */
- #define QPNP_BTM_HC_STATUS1 0x08
- #define QPNP_BTM_HC_STATUS_LOW 0x0a
- #define QPNP_BTM_HC_STATUS_HIGH 0x0b
- #define QPNP_BTM_HC_ADC_DIG_PARAM 0x42
- #define QPNP_BTM_HC_FAST_AVG_CTL 0x43
- #define QPNP_BTM_EN_CTL1 0x46
- #define QPNP_BTM_CONV_REQ 0x47
- #define QPNP_BTM_MEAS_INTERVAL_CTL 0x50
- #define QPNP_BTM_MEAS_INTERVAL_CTL2 0x51
- #define QPNP_BTM_MEAS_INTERVAL_CTL_PM5 0x44
- #define QPNP_BTM_MEAS_INTERVAL_CTL2_PM5 0x45
- #define QPNP_ADC_TM_MEAS_INTERVAL_TIME_SHIFT 0x3
- #define QPNP_ADC_TM_MEAS_INTERVAL_CTL2_SHIFT 0x4
- #define QPNP_ADC_TM_MEAS_INTERVAL_CTL2_MASK 0xf0
- #define QPNP_ADC_TM_MEAS_INTERVAL_CTL3_MASK 0xf
- #define QPNP_BTM_Mn_ADC_CH_SEL_CTL(n) ((n * 8) + 0x60)
- #define QPNP_BTM_Mn_LOW_THR0(n) ((n * 8) + 0x61)
- #define QPNP_BTM_Mn_LOW_THR1(n) ((n * 8) + 0x62)
- #define QPNP_BTM_Mn_HIGH_THR0(n) ((n * 8) + 0x63)
- #define QPNP_BTM_Mn_HIGH_THR1(n) ((n * 8) + 0x64)
- #define QPNP_BTM_Mn_MEAS_INTERVAL_CTL(n) ((n * 8) + 0x65)
- #define QPNP_BTM_Mn_CTL(n) ((n * 8) + 0x66)
- #define QPNP_BTM_CTL_HW_SETTLE_DELAY_MASK 0xf
- #define QPNP_BTM_CTL_CAL_SEL 0x30
- #define QPNP_BTM_CTL_CAL_SEL_MASK_SHIFT 4
- #define QPNP_BTM_CTL_CAL_VAL 0x40
- #define QPNP_BTM_Mn_EN(n) ((n * 8) + 0x67)
- #define QPNP_BTM_Mn_MEAS_EN BIT(7)
- #define QPNP_BTM_Mn_HIGH_THR_INT_EN BIT(1)
- #define QPNP_BTM_Mn_LOW_THR_INT_EN BIT(0)
- #define QPNP_BTM_Mn_DATA0(n) ((n * 2) + 0xa0)
- #define QPNP_BTM_Mn_DATA1(n) ((n * 2) + 0xa1)
- #define QPNP_BTM_CHANNELS 8
- #define QPNP_ADC_WAKEUP_SRC_TIMEOUT_MS 2000
- /* QPNP ADC TM HC end */
- struct qpnp_adc_thr_info {
- u8 status_low;
- u8 status_high;
- u8 qpnp_adc_tm_meas_en;
- u8 adc_tm_low_enable;
- u8 adc_tm_high_enable;
- u8 adc_tm_low_thr_set;
- u8 adc_tm_high_thr_set;
- spinlock_t adc_tm_low_lock;
- spinlock_t adc_tm_high_lock;
- };
- struct qpnp_adc_thr_client_info {
- struct list_head list;
- struct qpnp_adc_tm_btm_param *btm_param;
- int32_t low_thr_requested;
- int32_t high_thr_requested;
- enum qpnp_state_request state_requested;
- enum qpnp_state_request state_req_copy;
- bool low_thr_set;
- bool high_thr_set;
- bool notify_low_thr;
- bool notify_high_thr;
- };
- struct qpnp_adc_tm_sensor {
- struct thermal_zone_device *tz_dev;
- struct qpnp_adc_tm_chip *chip;
- enum thermal_device_mode mode;
- uint32_t sensor_num;
- enum qpnp_adc_meas_timer_select timer_select;
- uint32_t meas_interval;
- uint32_t low_thr;
- uint32_t high_thr;
- uint32_t btm_channel_num;
- uint32_t vadc_channel_num;
- struct workqueue_struct *req_wq;
- struct work_struct work;
- bool thermal_node;
- uint32_t scale_type;
- struct list_head thr_list;
- bool high_thr_triggered;
- bool low_thr_triggered;
- int emul_temperature;
- };
- struct qpnp_adc_tm_chip {
- struct device *dev;
- struct qpnp_adc_drv *adc;
- struct list_head list;
- bool adc_tm_initialized;
- bool adc_tm_recalib_check;
- int max_channels_available;
- struct qpnp_vadc_chip *vadc_dev;
- struct workqueue_struct *high_thr_wq;
- struct workqueue_struct *low_thr_wq;
- struct workqueue_struct *thr_wq;
- struct work_struct trigger_high_thr_work;
- struct work_struct trigger_low_thr_work;
- struct work_struct trigger_thr_work;
- bool adc_vote_enable;
- struct qpnp_adc_thr_info th_info;
- bool adc_tm_hc;
- struct qpnp_adc_tm_sensor sensor[0];
- };
- LIST_HEAD(qpnp_adc_tm_device_list);
- struct qpnp_adc_tm_trip_reg_type {
- enum qpnp_adc_tm_channel_select btm_amux_chan;
- uint16_t low_thr_lsb_addr;
- uint16_t low_thr_msb_addr;
- uint16_t high_thr_lsb_addr;
- uint16_t high_thr_msb_addr;
- u8 multi_meas_en;
- u8 low_thr_int_chan_en;
- u8 high_thr_int_chan_en;
- u8 meas_interval_ctl;
- };
- static struct qpnp_adc_tm_trip_reg_type adc_tm_data[] = {
- [QPNP_ADC_TM_CHAN0] = {QPNP_ADC_TM_M0_ADC_CH_SEL_CTL,
- QPNP_M0_LOW_THR_LSB,
- QPNP_M0_LOW_THR_MSB, QPNP_M0_HIGH_THR_LSB,
- QPNP_M0_HIGH_THR_MSB, QPNP_ADC_TM_MULTI_MEAS_EN_M0,
- QPNP_ADC_TM_LOW_THR_INT_EN_M0, QPNP_ADC_TM_HIGH_THR_INT_EN_M0,
- QPNP_ADC_TM_M0_MEAS_INTERVAL_CTL},
- [QPNP_ADC_TM_CHAN1] = {QPNP_ADC_TM_M1_ADC_CH_SEL_CTL,
- QPNP_M1_LOW_THR_LSB,
- QPNP_M1_LOW_THR_MSB, QPNP_M1_HIGH_THR_LSB,
- QPNP_M1_HIGH_THR_MSB, QPNP_ADC_TM_MULTI_MEAS_EN_M1,
- QPNP_ADC_TM_LOW_THR_INT_EN_M1, QPNP_ADC_TM_HIGH_THR_INT_EN_M1,
- QPNP_ADC_TM_M1_MEAS_INTERVAL_CTL},
- [QPNP_ADC_TM_CHAN2] = {QPNP_ADC_TM_M2_ADC_CH_SEL_CTL,
- QPNP_M2_LOW_THR_LSB,
- QPNP_M2_LOW_THR_MSB, QPNP_M2_HIGH_THR_LSB,
- QPNP_M2_HIGH_THR_MSB, QPNP_ADC_TM_MULTI_MEAS_EN_M2,
- QPNP_ADC_TM_LOW_THR_INT_EN_M2, QPNP_ADC_TM_HIGH_THR_INT_EN_M2,
- QPNP_ADC_TM_M2_MEAS_INTERVAL_CTL},
- [QPNP_ADC_TM_CHAN3] = {QPNP_ADC_TM_M3_ADC_CH_SEL_CTL,
- QPNP_M3_LOW_THR_LSB,
- QPNP_M3_LOW_THR_MSB, QPNP_M3_HIGH_THR_LSB,
- QPNP_M3_HIGH_THR_MSB, QPNP_ADC_TM_MULTI_MEAS_EN_M3,
- QPNP_ADC_TM_LOW_THR_INT_EN_M3, QPNP_ADC_TM_HIGH_THR_INT_EN_M3,
- QPNP_ADC_TM_M3_MEAS_INTERVAL_CTL},
- [QPNP_ADC_TM_CHAN4] = {QPNP_ADC_TM_M4_ADC_CH_SEL_CTL,
- QPNP_M4_LOW_THR_LSB,
- QPNP_M4_LOW_THR_MSB, QPNP_M4_HIGH_THR_LSB,
- QPNP_M4_HIGH_THR_MSB, QPNP_ADC_TM_MULTI_MEAS_EN_M4,
- QPNP_ADC_TM_LOW_THR_INT_EN_M4, QPNP_ADC_TM_HIGH_THR_INT_EN_M4,
- QPNP_ADC_TM_M4_MEAS_INTERVAL_CTL},
- [QPNP_ADC_TM_CHAN5] = {QPNP_ADC_TM_M5_ADC_CH_SEL_CTL,
- QPNP_M5_LOW_THR_LSB,
- QPNP_M5_LOW_THR_MSB, QPNP_M5_HIGH_THR_LSB,
- QPNP_M5_HIGH_THR_MSB, QPNP_ADC_TM_MULTI_MEAS_EN_M5,
- QPNP_ADC_TM_LOW_THR_INT_EN_M5, QPNP_ADC_TM_HIGH_THR_INT_EN_M5,
- QPNP_ADC_TM_M5_MEAS_INTERVAL_CTL},
- [QPNP_ADC_TM_CHAN6] = {QPNP_ADC_TM_M6_ADC_CH_SEL_CTL,
- QPNP_M6_LOW_THR_LSB,
- QPNP_M6_LOW_THR_MSB, QPNP_M6_HIGH_THR_LSB,
- QPNP_M6_HIGH_THR_MSB, QPNP_ADC_TM_MULTI_MEAS_EN_M6,
- QPNP_ADC_TM_LOW_THR_INT_EN_M6, QPNP_ADC_TM_HIGH_THR_INT_EN_M6,
- QPNP_ADC_TM_M6_MEAS_INTERVAL_CTL},
- [QPNP_ADC_TM_CHAN7] = {QPNP_ADC_TM_M7_ADC_CH_SEL_CTL,
- QPNP_M7_LOW_THR_LSB,
- QPNP_M7_LOW_THR_MSB, QPNP_M7_HIGH_THR_LSB,
- QPNP_M7_HIGH_THR_MSB, QPNP_ADC_TM_MULTI_MEAS_EN_M7,
- QPNP_ADC_TM_LOW_THR_INT_EN_M7, QPNP_ADC_TM_HIGH_THR_INT_EN_M7,
- QPNP_ADC_TM_M7_MEAS_INTERVAL_CTL},
- };
- static struct qpnp_adc_tm_reverse_scale_fn adc_tm_rscale_fn[] = {
- [SCALE_R_VBATT] = {qpnp_adc_vbatt_rscaler},
- [SCALE_RBATT_THERM] = {qpnp_adc_btm_scaler},
- [SCALE_R_USB_ID] = {qpnp_adc_usb_scaler},
- [SCALE_RPMIC_THERM] = {qpnp_adc_scale_millidegc_pmic_voltage_thr},
- [SCALE_R_SMB_BATT_THERM] = {qpnp_adc_smb_btm_rscaler},
- [SCALE_R_ABSOLUTE] = {qpnp_adc_absolute_rthr},
- [SCALE_QRD_SKUH_RBATT_THERM] = {qpnp_adc_qrd_skuh_btm_scaler},
- [SCALE_QRD_SKUT1_RBATT_THERM] = {qpnp_adc_qrd_skut1_btm_scaler},
- };
- static int32_t qpnp_adc_tm_read_reg(struct qpnp_adc_tm_chip *chip,
- int16_t reg, u8 *data, int len)
- {
- int rc = 0;
- rc = regmap_bulk_read(chip->adc->regmap, (chip->adc->offset + reg),
- data, len);
- if (rc < 0)
- pr_err("adc-tm read reg %d failed with %d\n", reg, rc);
- return rc;
- }
- static int32_t qpnp_adc_tm_write_reg(struct qpnp_adc_tm_chip *chip,
- int16_t reg, u8 data, int len)
- {
- int rc = 0;
- u8 *buf;
- buf = &data;
- rc = regmap_bulk_write(chip->adc->regmap, (chip->adc->offset + reg),
- buf, len);
- if (rc < 0)
- pr_err("adc-tm write reg %d failed with %d\n", reg, rc);
- return rc;
- }
- static int32_t qpnp_adc_tm_fast_avg_en(struct qpnp_adc_tm_chip *chip,
- uint32_t *fast_avg_sample)
- {
- int rc = 0, version = 0;
- u8 fast_avg_en = 0;
- version = qpnp_adc_get_revid_version(chip->dev);
- if (!((version == QPNP_REV_ID_8916_1_0) ||
- (version == QPNP_REV_ID_8916_1_1) ||
- (version == QPNP_REV_ID_8916_2_0))) {
- pr_debug("fast-avg-en not required for this version\n");
- return rc;
- }
- fast_avg_en = QPNP_FAST_AVG_ENABLED;
- rc = qpnp_adc_tm_write_reg(chip, QPNP_FAST_AVG_EN, fast_avg_en, 1);
- if (rc < 0) {
- pr_err("adc-tm fast-avg enable err\n");
- return rc;
- }
- if (*fast_avg_sample >= 3)
- *fast_avg_sample = 2;
- return rc;
- }
- static int qpnp_adc_tm_check_vreg_vote(struct qpnp_adc_tm_chip *chip)
- {
- int rc = 0;
- if (!chip->adc_vote_enable) {
- if (chip->adc->hkadc_ldo && chip->adc->hkadc_ldo_ok) {
- rc = qpnp_adc_enable_voltage(chip->adc);
- if (rc) {
- pr_err("failed enabling VADC LDO\n");
- return rc;
- }
- chip->adc_vote_enable = true;
- }
- }
- return rc;
- }
- static int32_t qpnp_adc_tm_enable(struct qpnp_adc_tm_chip *chip)
- {
- int rc = 0;
- u8 data = 0;
- rc = qpnp_adc_tm_check_vreg_vote(chip);
- if (rc) {
- pr_err("ADC TM VREG enable failed:%d\n", rc);
- return rc;
- }
- data = QPNP_ADC_TM_EN;
- rc = qpnp_adc_tm_write_reg(chip, QPNP_EN_CTL1, data, 1);
- if (rc < 0) {
- pr_err("adc-tm enable failed\n");
- return rc;
- }
- if (chip->adc_tm_hc) {
- data = QPNP_ADC_CONV_REQ_EN;
- rc = qpnp_adc_tm_write_reg(chip, QPNP_BTM_CONV_REQ, data, 1);
- if (rc < 0) {
- pr_err("adc-tm enable failed\n");
- return rc;
- }
- }
- return rc;
- }
- static int32_t qpnp_adc_tm_disable(struct qpnp_adc_tm_chip *chip)
- {
- u8 data = 0;
- int rc = 0;
- if (!chip->adc_tm_hc) {
- rc = qpnp_adc_tm_write_reg(chip, QPNP_EN_CTL1, data, 1);
- if (rc < 0) {
- pr_err("adc-tm disable failed\n");
- return rc;
- }
- }
- return rc;
- }
- static int qpnp_adc_tm_is_valid(struct qpnp_adc_tm_chip *chip)
- {
- struct qpnp_adc_tm_chip *adc_tm_chip = NULL;
- list_for_each_entry(adc_tm_chip, &qpnp_adc_tm_device_list, list)
- if (chip == adc_tm_chip)
- return 0;
- return -EINVAL;
- }
- static int32_t qpnp_adc_tm_rc_check_channel_en(struct qpnp_adc_tm_chip *chip)
- {
- u8 adc_tm_ctl = 0, status_low = 0, status_high = 0;
- int rc = 0, i = 0;
- bool ldo_en = false;
- for (i = 0; i < chip->max_channels_available; i++) {
- rc = qpnp_adc_tm_read_reg(chip, QPNP_BTM_Mn_CTL(i),
- &adc_tm_ctl, 1);
- if (rc) {
- pr_err("adc-tm-tm read ctl failed with %d\n", rc);
- return rc;
- }
- adc_tm_ctl &= QPNP_BTM_Mn_MEAS_EN;
- status_low = adc_tm_ctl & QPNP_BTM_Mn_LOW_THR_INT_EN;
- status_high = adc_tm_ctl & QPNP_BTM_Mn_HIGH_THR_INT_EN;
- /* Enable only if there are pending measurement requests */
- if ((adc_tm_ctl && status_high) ||
- (adc_tm_ctl && status_low)) {
- qpnp_adc_tm_enable(chip);
- ldo_en = true;
- /* Request conversion */
- rc = qpnp_adc_tm_write_reg(chip, QPNP_CONV_REQ,
- QPNP_CONV_REQ_SET, 1);
- if (rc < 0) {
- pr_err("adc-tm request conversion failed\n");
- return rc;
- }
- }
- break;
- }
- if (!ldo_en) {
- /* disable the vote if applicable */
- if (chip->adc_vote_enable && chip->adc->hkadc_ldo &&
- chip->adc->hkadc_ldo_ok) {
- qpnp_adc_disable_voltage(chip->adc);
- chip->adc_vote_enable = false;
- }
- }
- return rc;
- }
- static int32_t qpnp_adc_tm_enable_if_channel_meas(
- struct qpnp_adc_tm_chip *chip)
- {
- u8 adc_tm_meas_en = 0, status_low = 0, status_high = 0;
- int rc = 0;
- if (chip->adc_tm_hc) {
- rc = qpnp_adc_tm_rc_check_channel_en(chip);
- if (rc) {
- pr_err("adc_tm channel check failed\n");
- return rc;
- }
- } else {
- /* Check if a measurement request is still required */
- rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_TM_MULTI_MEAS_EN,
- &adc_tm_meas_en, 1);
- if (rc) {
- pr_err("read status high failed with %d\n", rc);
- return rc;
- }
- rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_TM_LOW_THR_INT_EN,
- &status_low, 1);
- if (rc) {
- pr_err("read status low failed with %d\n", rc);
- return rc;
- }
- rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_TM_HIGH_THR_INT_EN,
- &status_high, 1);
- if (rc) {
- pr_err("read status high failed with %d\n", rc);
- return rc;
- }
- /* Enable only if there are pending measurement requests */
- if ((adc_tm_meas_en && status_high) ||
- (adc_tm_meas_en && status_low)) {
- qpnp_adc_tm_enable(chip);
- /* Request conversion */
- rc = qpnp_adc_tm_write_reg(chip, QPNP_CONV_REQ,
- QPNP_CONV_REQ_SET, 1);
- if (rc < 0) {
- pr_err("adc-tm request conversion failed\n");
- return rc;
- }
- } else {
- /* disable the vote if applicable */
- if (chip->adc_vote_enable && chip->adc->hkadc_ldo &&
- chip->adc->hkadc_ldo_ok) {
- qpnp_adc_disable_voltage(chip->adc);
- chip->adc_vote_enable = false;
- }
- }
- }
- return rc;
- }
- static int32_t qpnp_adc_tm_mode_select(struct qpnp_adc_tm_chip *chip,
- u8 mode_ctl)
- {
- int rc;
- mode_ctl |= (QPNP_ADC_TRIM_EN | QPNP_AMUX_TRIM_EN);
- /* VADC_BTM current sets mode to recurring measurements */
- rc = qpnp_adc_tm_write_reg(chip, QPNP_MODE_CTL, mode_ctl, 1);
- if (rc < 0)
- pr_err("adc-tm write mode selection err\n");
- return rc;
- }
- static int32_t qpnp_adc_tm_req_sts_check(struct qpnp_adc_tm_chip *chip)
- {
- u8 status1 = 0, mode_ctl = 0;
- int rc, count = 0;
- /* Re-enable the peripheral */
- rc = qpnp_adc_tm_enable(chip);
- if (rc) {
- pr_err("adc-tm re-enable peripheral failed\n");
- return rc;
- }
- /* The VADC_TM bank needs to be disabled for new conversion request */
- rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_TM_STATUS1, &status1, 1);
- if (rc) {
- pr_err("adc-tm read status1 failed\n");
- return rc;
- }
- /* Disable the bank if a conversion is occurring */
- while (status1 & QPNP_STATUS1_REQ_STS) {
- if (count > QPNP_RETRY) {
- pr_err("retry error=%d with 0x%x\n", count, status1);
- break;
- }
- /*
- * Wait time is based on the optimum sampling rate
- * and adding enough time buffer to account for ADC conversions
- * occurring on different peripheral banks
- */
- usleep_range(QPNP_MIN_TIME, QPNP_MAX_TIME);
- rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_TM_STATUS1,
- &status1, 1);
- if (rc < 0) {
- pr_err("adc-tm disable failed\n");
- return rc;
- }
- count++;
- }
- if (!chip->adc_tm_hc) {
- /* Change the mode back to recurring measurement mode */
- mode_ctl = ADC_OP_MEASUREMENT_INTERVAL << QPNP_OP_MODE_SHIFT;
- rc = qpnp_adc_tm_mode_select(chip, mode_ctl);
- if (rc < 0) {
- pr_err("adc-tm mode change to recurring failed\n");
- return rc;
- }
- }
- /* Disable the peripheral */
- rc = qpnp_adc_tm_disable(chip);
- if (rc < 0) {
- pr_err("adc-tm peripheral disable failed\n");
- return rc;
- }
- return rc;
- }
- static int32_t qpnp_adc_tm_get_btm_idx(struct qpnp_adc_tm_chip *chip,
- uint32_t btm_chan, uint32_t *btm_chan_idx)
- {
- int rc = 0, i;
- bool chan_found = false;
- if (!chip->adc_tm_hc) {
- for (i = 0; i < QPNP_ADC_TM_CHAN_NONE; i++) {
- if (adc_tm_data[i].btm_amux_chan == btm_chan) {
- *btm_chan_idx = i;
- chan_found = true;
- }
- }
- } else {
- for (i = 0; i < chip->max_channels_available; i++) {
- if (chip->sensor[i].btm_channel_num == btm_chan) {
- *btm_chan_idx = i;
- chan_found = true;
- break;
- }
- }
- }
- if (!chan_found)
- return -EINVAL;
- return rc;
- }
- static int32_t qpnp_adc_tm_check_revision(struct qpnp_adc_tm_chip *chip,
- uint32_t btm_chan_num)
- {
- u8 rev, perph_subtype;
- int rc = 0;
- rc = qpnp_adc_tm_read_reg(chip, QPNP_REVISION3, &rev, 1);
- if (rc) {
- pr_err("adc-tm revision read failed\n");
- return rc;
- }
- rc = qpnp_adc_tm_read_reg(chip, QPNP_PERPH_SUBTYPE, &perph_subtype, 1);
- if (rc) {
- pr_err("adc-tm perph_subtype read failed\n");
- return rc;
- }
- if (perph_subtype == QPNP_PERPH_TYPE2) {
- if ((rev < QPNP_REVISION_EIGHT_CHANNEL_SUPPORT) &&
- (btm_chan_num > QPNP_ADC_TM_M4_ADC_CH_SEL_CTL)) {
- pr_debug("Version does not support more than 5 channels\n");
- return -EINVAL;
- }
- }
- if (perph_subtype == QPNP_PERPH_SUBTYPE_TWO_CHANNEL_SUPPORT) {
- if (btm_chan_num > QPNP_ADC_TM_M1_ADC_CH_SEL_CTL) {
- pr_debug("Version does not support more than 2 channels\n");
- return -EINVAL;
- }
- }
- return rc;
- }
- static int32_t qpnp_adc_tm_timer_interval_select(
- struct qpnp_adc_tm_chip *chip, uint32_t btm_chan,
- struct qpnp_vadc_chan_properties *chan_prop)
- {
- int rc, chan_idx = 0, i = 0;
- bool chan_found = false;
- u8 meas_interval_timer2 = 0, timer_interval_store = 0;
- uint32_t btm_chan_idx = 0;
- bool is_pmic_5 = chip->adc->adc_prop->is_pmic_5;
- while (i < chip->max_channels_available) {
- if (chip->sensor[i].btm_channel_num == btm_chan) {
- chan_idx = i;
- chan_found = true;
- i++;
- } else
- i++;
- }
- if (!chan_found) {
- pr_err("Channel not found\n");
- return -EINVAL;
- }
- switch (chip->sensor[chan_idx].timer_select) {
- case ADC_MEAS_TIMER_SELECT1:
- if (!chip->adc_tm_hc)
- rc = qpnp_adc_tm_write_reg(chip,
- QPNP_ADC_TM_MEAS_INTERVAL_CTL,
- chip->sensor[chan_idx].meas_interval, 1);
- else {
- if (!is_pmic_5)
- rc = qpnp_adc_tm_write_reg(chip,
- QPNP_BTM_MEAS_INTERVAL_CTL,
- chip->sensor[chan_idx].meas_interval,
- 1);
- else
- rc = qpnp_adc_tm_write_reg(chip,
- QPNP_BTM_MEAS_INTERVAL_CTL_PM5,
- chip->sensor[chan_idx].meas_interval,
- 1);
- }
- if (rc < 0) {
- pr_err("timer1 configure failed\n");
- return rc;
- }
- break;
- case ADC_MEAS_TIMER_SELECT2:
- /* Thermal channels uses timer2, default to 1 second */
- if (!chip->adc_tm_hc)
- rc = qpnp_adc_tm_read_reg(chip,
- QPNP_ADC_TM_MEAS_INTERVAL_CTL2,
- &meas_interval_timer2, 1);
- else {
- if (!is_pmic_5)
- rc = qpnp_adc_tm_read_reg(chip,
- QPNP_BTM_MEAS_INTERVAL_CTL2,
- &meas_interval_timer2, 1);
- else
- rc = qpnp_adc_tm_read_reg(chip,
- QPNP_BTM_MEAS_INTERVAL_CTL2_PM5,
- &meas_interval_timer2, 1);
- }
- if (rc < 0) {
- pr_err("timer2 configure read failed\n");
- return rc;
- }
- timer_interval_store = chip->sensor[chan_idx].meas_interval;
- timer_interval_store <<= QPNP_ADC_TM_MEAS_INTERVAL_CTL2_SHIFT;
- timer_interval_store &= QPNP_ADC_TM_MEAS_INTERVAL_CTL2_MASK;
- meas_interval_timer2 |= timer_interval_store;
- if (!chip->adc_tm_hc)
- rc = qpnp_adc_tm_write_reg(chip,
- QPNP_ADC_TM_MEAS_INTERVAL_CTL2,
- meas_interval_timer2, 1);
- else {
- if (!is_pmic_5)
- rc = qpnp_adc_tm_write_reg(chip,
- QPNP_BTM_MEAS_INTERVAL_CTL2,
- meas_interval_timer2, 1);
- else
- rc = qpnp_adc_tm_write_reg(chip,
- QPNP_BTM_MEAS_INTERVAL_CTL2_PM5,
- meas_interval_timer2, 1);
- }
- if (rc < 0) {
- pr_err("timer2 configure failed\n");
- return rc;
- }
- break;
- case ADC_MEAS_TIMER_SELECT3:
- if (!chip->adc_tm_hc)
- rc = qpnp_adc_tm_read_reg(chip,
- QPNP_ADC_TM_MEAS_INTERVAL_CTL2,
- &meas_interval_timer2, 1);
- else {
- if (!is_pmic_5)
- rc = qpnp_adc_tm_read_reg(chip,
- QPNP_BTM_MEAS_INTERVAL_CTL2,
- &meas_interval_timer2, 1);
- else
- rc = qpnp_adc_tm_read_reg(chip,
- QPNP_BTM_MEAS_INTERVAL_CTL2_PM5,
- &meas_interval_timer2, 1);
- }
- if (rc < 0) {
- pr_err("timer3 read failed\n");
- return rc;
- }
- timer_interval_store = chip->sensor[chan_idx].meas_interval;
- timer_interval_store &= QPNP_ADC_TM_MEAS_INTERVAL_CTL3_MASK;
- meas_interval_timer2 |= timer_interval_store;
- if (!chip->adc_tm_hc)
- rc = qpnp_adc_tm_write_reg(chip,
- QPNP_ADC_TM_MEAS_INTERVAL_CTL2,
- meas_interval_timer2, 1);
- else {
- if (!is_pmic_5)
- rc = qpnp_adc_tm_write_reg(chip,
- QPNP_BTM_MEAS_INTERVAL_CTL2,
- meas_interval_timer2, 1);
- else
- rc = qpnp_adc_tm_write_reg(chip,
- QPNP_BTM_MEAS_INTERVAL_CTL2_PM5,
- meas_interval_timer2, 1);
- }
- if (rc < 0) {
- pr_err("timer3 configure failed\n");
- return rc;
- }
- break;
- default:
- pr_err("Invalid timer selection\n");
- return -EINVAL;
- }
- /* Select the timer to use for the corresponding channel */
- rc = qpnp_adc_tm_get_btm_idx(chip, btm_chan, &btm_chan_idx);
- if (rc < 0) {
- pr_err("Invalid btm channel idx\n");
- return rc;
- }
- if (!chip->adc_tm_hc)
- rc = qpnp_adc_tm_write_reg(chip,
- adc_tm_data[btm_chan_idx].meas_interval_ctl,
- chip->sensor[chan_idx].timer_select, 1);
- else
- rc = qpnp_adc_tm_write_reg(chip,
- QPNP_BTM_Mn_MEAS_INTERVAL_CTL(btm_chan_idx),
- chip->sensor[chan_idx].timer_select, 1);
- if (rc < 0) {
- pr_err("TM channel timer configure failed\n");
- return rc;
- }
- pr_debug("timer select:%d, timer_value_within_select:%d, channel:%x\n",
- chip->sensor[chan_idx].timer_select,
- chip->sensor[chan_idx].meas_interval,
- btm_chan);
- return rc;
- }
- static int32_t qpnp_adc_tm_add_to_list(struct qpnp_adc_tm_chip *chip,
- uint32_t dt_index,
- struct qpnp_adc_tm_btm_param *param,
- struct qpnp_vadc_chan_properties *chan_prop)
- {
- struct qpnp_adc_thr_client_info *client_info = NULL;
- bool client_info_exists = false;
- list_for_each_entry(client_info,
- &chip->sensor[dt_index].thr_list, list) {
- if (client_info->btm_param == param) {
- client_info->low_thr_requested = chan_prop->low_thr;
- client_info->high_thr_requested = chan_prop->high_thr;
- client_info->state_requested = param->state_request;
- client_info->state_req_copy = param->state_request;
- client_info->notify_low_thr = false;
- client_info->notify_high_thr = false;
- client_info_exists = true;
- pr_debug("client found\n");
- }
- }
- if (!client_info_exists) {
- client_info = devm_kzalloc(chip->dev,
- sizeof(struct qpnp_adc_thr_client_info), GFP_KERNEL);
- if (!client_info)
- return -ENOMEM;
- pr_debug("new client\n");
- client_info->btm_param = param;
- client_info->low_thr_requested = chan_prop->low_thr;
- client_info->high_thr_requested = chan_prop->high_thr;
- client_info->state_requested = param->state_request;
- client_info->state_req_copy = param->state_request;
- list_add_tail(&client_info->list,
- &chip->sensor[dt_index].thr_list);
- }
- return 0;
- }
- static int32_t qpnp_adc_tm_reg_update(struct qpnp_adc_tm_chip *chip,
- uint16_t addr, u8 mask, bool state)
- {
- u8 reg_value = 0;
- int rc = 0;
- rc = qpnp_adc_tm_read_reg(chip, addr, ®_value, 1);
- if (rc < 0) {
- pr_err("read failed for addr:0x%x\n", addr);
- return rc;
- }
- reg_value = reg_value & ~mask;
- if (state)
- reg_value |= mask;
- pr_debug("state:%d, reg:0x%x with bits:0x%x and mask:0x%x\n",
- state, addr, reg_value, ~mask);
- rc = qpnp_adc_tm_write_reg(chip, addr, reg_value, 1);
- if (rc < 0) {
- pr_err("write failed for addr:%x\n", addr);
- return rc;
- }
- return rc;
- }
- static int32_t qpnp_adc_tm_read_thr_value(struct qpnp_adc_tm_chip *chip,
- uint32_t btm_chan)
- {
- int rc = 0;
- u8 data_lsb = 0, data_msb = 0;
- uint32_t btm_chan_idx = 0;
- int32_t low_thr = 0, high_thr = 0;
- if (!chip->adc_tm_hc) {
- pr_err("Not applicable for VADC HC peripheral\n");
- return -EINVAL;
- }
- rc = qpnp_adc_tm_get_btm_idx(chip, btm_chan, &btm_chan_idx);
- if (rc < 0) {
- pr_err("Invalid btm channel idx\n");
- return rc;
- }
- rc = qpnp_adc_tm_read_reg(chip,
- adc_tm_data[btm_chan_idx].low_thr_lsb_addr,
- &data_lsb, 1);
- if (rc < 0) {
- pr_err("low threshold lsb setting failed\n");
- return rc;
- }
- rc = qpnp_adc_tm_read_reg(chip,
- adc_tm_data[btm_chan_idx].low_thr_msb_addr,
- &data_msb, 1);
- if (rc < 0) {
- pr_err("low threshold msb setting failed\n");
- return rc;
- }
- low_thr = (data_msb << 8) | data_lsb;
- rc = qpnp_adc_tm_read_reg(chip,
- adc_tm_data[btm_chan_idx].high_thr_lsb_addr,
- &data_lsb, 1);
- if (rc < 0) {
- pr_err("high threshold lsb setting failed\n");
- return rc;
- }
- rc = qpnp_adc_tm_read_reg(chip,
- adc_tm_data[btm_chan_idx].high_thr_msb_addr,
- &data_msb, 1);
- if (rc < 0) {
- pr_err("high threshold msb setting failed\n");
- return rc;
- }
- high_thr = (data_msb << 8) | data_lsb;
- pr_debug("configured thresholds high:0x%x and low:0x%x\n",
- high_thr, low_thr);
- return rc;
- }
- static int32_t qpnp_adc_tm_thr_update(struct qpnp_adc_tm_chip *chip,
- uint32_t btm_chan, int32_t high_thr, int32_t low_thr)
- {
- int rc = 0;
- uint32_t btm_chan_idx = 0;
- rc = qpnp_adc_tm_get_btm_idx(chip, btm_chan, &btm_chan_idx);
- if (rc < 0) {
- pr_err("Invalid btm channel idx\n");
- return rc;
- }
- if (!chip->adc_tm_hc) {
- rc = qpnp_adc_tm_write_reg(chip,
- adc_tm_data[btm_chan_idx].low_thr_lsb_addr,
- QPNP_ADC_TM_THR_LSB_MASK(low_thr), 1);
- if (rc < 0) {
- pr_err("low threshold lsb setting failed\n");
- return rc;
- }
- rc = qpnp_adc_tm_write_reg(chip,
- adc_tm_data[btm_chan_idx].low_thr_msb_addr,
- QPNP_ADC_TM_THR_MSB_MASK(low_thr), 1);
- if (rc < 0) {
- pr_err("low threshold msb setting failed\n");
- return rc;
- }
- rc = qpnp_adc_tm_write_reg(chip,
- adc_tm_data[btm_chan_idx].high_thr_lsb_addr,
- QPNP_ADC_TM_THR_LSB_MASK(high_thr), 1);
- if (rc < 0) {
- pr_err("high threshold lsb setting failed\n");
- return rc;
- }
- rc = qpnp_adc_tm_write_reg(chip,
- adc_tm_data[btm_chan_idx].high_thr_msb_addr,
- QPNP_ADC_TM_THR_MSB_MASK(high_thr), 1);
- if (rc < 0)
- pr_err("high threshold msb setting failed\n");
- } else {
- rc = qpnp_adc_tm_write_reg(chip,
- QPNP_BTM_Mn_LOW_THR0(btm_chan_idx),
- QPNP_ADC_TM_THR_LSB_MASK(low_thr), 1);
- if (rc < 0) {
- pr_err("low threshold lsb setting failed\n");
- return rc;
- }
- rc = qpnp_adc_tm_write_reg(chip,
- QPNP_BTM_Mn_LOW_THR1(btm_chan_idx),
- QPNP_ADC_TM_THR_MSB_MASK(low_thr), 1);
- if (rc < 0) {
- pr_err("low threshold msb setting failed\n");
- return rc;
- }
- rc = qpnp_adc_tm_write_reg(chip,
- QPNP_BTM_Mn_HIGH_THR0(btm_chan_idx),
- QPNP_ADC_TM_THR_LSB_MASK(high_thr), 1);
- if (rc < 0) {
- pr_err("high threshold lsb setting failed\n");
- return rc;
- }
- rc = qpnp_adc_tm_write_reg(chip,
- QPNP_BTM_Mn_HIGH_THR1(btm_chan_idx),
- QPNP_ADC_TM_THR_MSB_MASK(high_thr), 1);
- if (rc < 0)
- pr_err("high threshold msb setting failed\n");
- }
- pr_debug("client requested high:%d and low:%d\n",
- high_thr, low_thr);
- return rc;
- }
- static int32_t qpnp_adc_tm_manage_thresholds(struct qpnp_adc_tm_chip *chip,
- uint32_t dt_index, uint32_t btm_chan)
- {
- struct qpnp_adc_thr_client_info *client_info = NULL;
- struct list_head *thr_list;
- int high_thr = 0, low_thr = 0, rc = 0;
- /*
- * high_thr/low_thr starting point and reset the high_thr_set and
- * low_thr_set back to reset since the thresholds will be
- * recomputed.
- */
- list_for_each(thr_list,
- &chip->sensor[dt_index].thr_list) {
- client_info = list_entry(thr_list,
- struct qpnp_adc_thr_client_info, list);
- high_thr = client_info->high_thr_requested;
- low_thr = client_info->low_thr_requested;
- client_info->high_thr_set = false;
- client_info->low_thr_set = false;
- }
- pr_debug("init threshold is high:%d and low:%d\n", high_thr, low_thr);
- /* Find the min of high_thr and max of low_thr */
- list_for_each(thr_list,
- &chip->sensor[dt_index].thr_list) {
- client_info = list_entry(thr_list,
- struct qpnp_adc_thr_client_info, list);
- if ((client_info->state_req_copy == ADC_TM_HIGH_THR_ENABLE) ||
- (client_info->state_req_copy ==
- ADC_TM_HIGH_LOW_THR_ENABLE))
- if (client_info->high_thr_requested < high_thr)
- high_thr = client_info->high_thr_requested;
- if ((client_info->state_req_copy == ADC_TM_LOW_THR_ENABLE) ||
- (client_info->state_req_copy ==
- ADC_TM_HIGH_LOW_THR_ENABLE))
- if (client_info->low_thr_requested > low_thr)
- low_thr = client_info->low_thr_requested;
- pr_debug("threshold compared is high:%d and low:%d\n",
- client_info->high_thr_requested,
- client_info->low_thr_requested);
- pr_debug("current threshold is high:%d and low:%d\n",
- high_thr, low_thr);
- }
- /* Check which of the high_thr and low_thr got set */
- list_for_each(thr_list,
- &chip->sensor[dt_index].thr_list) {
- client_info = list_entry(thr_list,
- struct qpnp_adc_thr_client_info, list);
- if ((client_info->state_req_copy == ADC_TM_HIGH_THR_ENABLE) ||
- (client_info->state_req_copy ==
- ADC_TM_HIGH_LOW_THR_ENABLE))
- if (high_thr == client_info->high_thr_requested)
- client_info->high_thr_set = true;
- if ((client_info->state_req_copy == ADC_TM_LOW_THR_ENABLE) ||
- (client_info->state_req_copy ==
- ADC_TM_HIGH_LOW_THR_ENABLE))
- if (low_thr == client_info->low_thr_requested)
- client_info->low_thr_set = true;
- }
- rc = qpnp_adc_tm_thr_update(chip, btm_chan, high_thr, low_thr);
- if (rc < 0)
- pr_err("setting chan:%d threshold failed\n", btm_chan);
- pr_debug("threshold written is high:%d and low:%d\n",
- high_thr, low_thr);
- return 0;
- }
- static int32_t qpnp_adc_tm_channel_configure(struct qpnp_adc_tm_chip *chip,
- uint32_t btm_chan,
- struct qpnp_vadc_chan_properties *chan_prop,
- uint32_t amux_channel)
- {
- int rc = 0, i = 0, chan_idx = 0;
- bool chan_found = false, high_thr_set = false, low_thr_set = false;
- u8 sensor_mask = 0;
- struct qpnp_adc_thr_client_info *client_info = NULL;
- uint32_t btm_chan_idx = 0;
- while (i < chip->max_channels_available) {
- if (chip->sensor[i].btm_channel_num == btm_chan) {
- chan_idx = i;
- chan_found = true;
- i++;
- } else
- i++;
- }
- if (!chan_found) {
- pr_err("Channel not found\n");
- return -EINVAL;
- }
- rc = qpnp_adc_tm_get_btm_idx(chip, btm_chan, &btm_chan_idx);
- if (rc < 0) {
- pr_err("Invalid btm channel idx\n");
- return rc;
- }
- sensor_mask = 1 << chan_idx;
- if (!chip->sensor[chan_idx].thermal_node) {
- /* Update low and high notification thresholds */
- rc = qpnp_adc_tm_manage_thresholds(chip, chan_idx,
- btm_chan);
- if (rc < 0) {
- pr_err("setting chan:%d threshold failed\n", btm_chan);
- return rc;
- }
- list_for_each_entry(client_info,
- &chip->sensor[chan_idx].thr_list, list) {
- if (client_info->high_thr_set == true)
- high_thr_set = true;
- if (client_info->low_thr_set == true)
- low_thr_set = true;
- }
- if (low_thr_set) {
- pr_debug("low sensor mask:%x with state:%d\n",
- sensor_mask, chan_prop->state_request);
- /* Enable low threshold's interrupt */
- if (!chip->adc_tm_hc)
- rc = qpnp_adc_tm_reg_update(chip,
- QPNP_ADC_TM_LOW_THR_INT_EN,
- sensor_mask, true);
- else
- rc = qpnp_adc_tm_reg_update(chip,
- QPNP_BTM_Mn_EN(btm_chan_idx),
- QPNP_BTM_Mn_LOW_THR_INT_EN, true);
- if (rc < 0) {
- pr_err("low thr enable err:%d\n", btm_chan);
- return rc;
- }
- }
- if (high_thr_set) {
- /* Enable high threshold's interrupt */
- pr_debug("high sensor mask:%x\n", sensor_mask);
- if (!chip->adc_tm_hc)
- rc = qpnp_adc_tm_reg_update(chip,
- QPNP_ADC_TM_HIGH_THR_INT_EN,
- sensor_mask, true);
- else
- rc = qpnp_adc_tm_reg_update(chip,
- QPNP_BTM_Mn_EN(btm_chan_idx),
- QPNP_BTM_Mn_HIGH_THR_INT_EN, true);
- if (rc < 0) {
- pr_err("high thr enable err:%d\n", btm_chan);
- return rc;
- }
- }
- }
- /* Enable corresponding BTM channel measurement */
- if (!chip->adc_tm_hc)
- rc = qpnp_adc_tm_reg_update(chip,
- QPNP_ADC_TM_MULTI_MEAS_EN, sensor_mask, true);
- else
- rc = qpnp_adc_tm_reg_update(chip, QPNP_BTM_Mn_EN(btm_chan_idx),
- QPNP_BTM_Mn_MEAS_EN, true);
- if (rc < 0) {
- pr_err("multi measurement en failed\n");
- return rc;
- }
- return rc;
- }
- static int32_t qpnp_adc_tm_hc_configure(struct qpnp_adc_tm_chip *chip,
- struct qpnp_adc_amux_properties *chan_prop)
- {
- u8 decimation = 0, fast_avg_ctl = 0;
- u8 buf[8];
- int rc = 0;
- uint32_t btm_chan = 0, cal_type = 0, btm_chan_idx = 0;
- /* Disable bank */
- rc = qpnp_adc_tm_disable(chip);
- if (rc)
- return rc;
- /* Decimation setup */
- decimation = chan_prop->decimation;
- rc = qpnp_adc_tm_write_reg(chip, QPNP_BTM_HC_ADC_DIG_PARAM,
- decimation, 1);
- if (rc < 0) {
- pr_err("adc-tm digital parameter setup err\n");
- return rc;
- }
- /* Fast averaging setup/enable */
- rc = qpnp_adc_tm_read_reg(chip, QPNP_BTM_HC_FAST_AVG_CTL,
- &fast_avg_ctl, 1);
- if (rc < 0) {
- pr_err("adc-tm fast-avg enable read err\n");
- return rc;
- }
- fast_avg_ctl |= chan_prop->fast_avg_setup;
- rc = qpnp_adc_tm_write_reg(chip, QPNP_BTM_HC_FAST_AVG_CTL,
- fast_avg_ctl, 1);
- if (rc < 0) {
- pr_err("adc-tm fast-avg enable write err\n");
- return rc;
- }
- /* Read block registers for respective BTM channel */
- btm_chan = chan_prop->chan_prop->tm_channel_select;
- rc = qpnp_adc_tm_get_btm_idx(chip, btm_chan, &btm_chan_idx);
- if (rc < 0) {
- pr_err("Invalid btm channel idx\n");
- return rc;
- }
- rc = qpnp_adc_tm_read_reg(chip,
- QPNP_BTM_Mn_ADC_CH_SEL_CTL(btm_chan_idx), buf, 8);
- if (rc < 0) {
- pr_err("qpnp adc configure block read failed\n");
- return rc;
- }
- /* Update ADC channel sel */
- rc = qpnp_adc_tm_write_reg(chip,
- QPNP_BTM_Mn_ADC_CH_SEL_CTL(btm_chan_idx),
- chan_prop->amux_channel, 1);
- if (rc < 0) {
- pr_err("adc-tm channel amux select failed\n");
- return rc;
- }
- /* Manage thresholds */
- rc = qpnp_adc_tm_channel_configure(chip, btm_chan,
- chan_prop->chan_prop, chan_prop->amux_channel);
- if (rc < 0) {
- pr_err("adc-tm channel threshold configure failed\n");
- return rc;
- }
- /* Measurement interval setup */
- rc = qpnp_adc_tm_timer_interval_select(chip, btm_chan,
- chan_prop->chan_prop);
- if (rc < 0) {
- pr_err("adc-tm timer select failed\n");
- return rc;
- }
- /* Set calibration select, hw_settle delay */
- cal_type |= (chan_prop->calib_type << QPNP_BTM_CTL_CAL_SEL_MASK_SHIFT);
- buf[6] &= ~QPNP_BTM_CTL_HW_SETTLE_DELAY_MASK;
- buf[6] |= chan_prop->hw_settle_time;
- buf[6] &= ~QPNP_BTM_CTL_CAL_SEL;
- buf[6] |= cal_type;
- rc = qpnp_adc_tm_write_reg(chip, QPNP_BTM_Mn_CTL(btm_chan_idx),
- buf[6], 1);
- if (rc < 0) {
- pr_err("adc-tm hw-settle, calib sel failed\n");
- return rc;
- }
- /* Enable bank */
- rc = qpnp_adc_tm_enable(chip);
- if (rc)
- return rc;
- /* Request conversion */
- rc = qpnp_adc_tm_write_reg(chip, QPNP_CONV_REQ, QPNP_CONV_REQ_SET, 1);
- if (rc < 0) {
- pr_err("adc-tm request conversion failed\n");
- return rc;
- }
- return 0;
- }
- static int32_t qpnp_adc_tm_configure(struct qpnp_adc_tm_chip *chip,
- struct qpnp_adc_amux_properties *chan_prop)
- {
- u8 decimation = 0, op_cntrl = 0, mode_ctl = 0;
- int rc = 0;
- uint32_t btm_chan = 0;
- /* Set measurement in single measurement mode */
- mode_ctl = ADC_OP_NORMAL_MODE << QPNP_OP_MODE_SHIFT;
- rc = qpnp_adc_tm_mode_select(chip, mode_ctl);
- if (rc < 0) {
- pr_err("adc-tm single mode select failed\n");
- return rc;
- }
- /* Disable bank */
- rc = qpnp_adc_tm_disable(chip);
- if (rc)
- return rc;
- /* Check if a conversion is in progress */
- rc = qpnp_adc_tm_req_sts_check(chip);
- if (rc < 0) {
- pr_err("adc-tm req_sts check failed\n");
- return rc;
- }
- /* Configure AMUX channel select for the corresponding BTM channel*/
- btm_chan = chan_prop->chan_prop->tm_channel_select;
- rc = qpnp_adc_tm_write_reg(chip, btm_chan, chan_prop->amux_channel, 1);
- if (rc < 0) {
- pr_err("adc-tm channel selection err\n");
- return rc;
- }
- /* Digital parameter setup */
- decimation |= chan_prop->decimation <<
- QPNP_ADC_DIG_DEC_RATIO_SEL_SHIFT;
- rc = qpnp_adc_tm_write_reg(chip, QPNP_ADC_DIG_PARAM, decimation, 1);
- if (rc < 0) {
- pr_err("adc-tm digital parameter setup err\n");
- return rc;
- }
- /* Hardware setting time */
- rc = qpnp_adc_tm_write_reg(chip, QPNP_HW_SETTLE_DELAY,
- chan_prop->hw_settle_time, 1);
- if (rc < 0) {
- pr_err("adc-tm hw settling time setup err\n");
- return rc;
- }
- /* Fast averaging setup/enable */
- rc = qpnp_adc_tm_fast_avg_en(chip, &chan_prop->fast_avg_setup);
- if (rc < 0) {
- pr_err("adc-tm fast-avg enable err\n");
- return rc;
- }
- rc = qpnp_adc_tm_write_reg(chip, QPNP_FAST_AVG_CTL,
- chan_prop->fast_avg_setup, 1);
- if (rc < 0) {
- pr_err("adc-tm fast-avg setup err\n");
- return rc;
- }
- /* Measurement interval setup */
- rc = qpnp_adc_tm_timer_interval_select(chip, btm_chan,
- chan_prop->chan_prop);
- if (rc < 0) {
- pr_err("adc-tm timer select failed\n");
- return rc;
- }
- /* Channel configuration setup */
- rc = qpnp_adc_tm_channel_configure(chip, btm_chan,
- chan_prop->chan_prop, chan_prop->amux_channel);
- if (rc < 0) {
- pr_err("adc-tm channel configure failed\n");
- return rc;
- }
- /* Recurring interval measurement enable */
- rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_MEAS_INTERVAL_OP_CTL,
- &op_cntrl, 1);
- op_cntrl |= QPNP_ADC_MEAS_INTERVAL_OP;
- rc = qpnp_adc_tm_reg_update(chip, QPNP_ADC_MEAS_INTERVAL_OP_CTL,
- op_cntrl, true);
- if (rc < 0) {
- pr_err("adc-tm meas interval op configure failed\n");
- return rc;
- }
- /* Enable bank */
- rc = qpnp_adc_tm_enable(chip);
- if (rc)
- return rc;
- /* Request conversion */
- rc = qpnp_adc_tm_write_reg(chip, QPNP_CONV_REQ, QPNP_CONV_REQ_SET, 1);
- if (rc < 0) {
- pr_err("adc-tm request conversion failed\n");
- return rc;
- }
- return 0;
- }
- static int qpnp_adc_tm_set_mode(struct qpnp_adc_tm_sensor *adc_tm,
- enum thermal_device_mode mode)
- {
- struct qpnp_adc_tm_chip *chip = adc_tm->chip;
- int rc = 0, channel;
- u8 sensor_mask = 0, mode_ctl = 0;
- uint32_t btm_chan_idx = 0, btm_chan = 0;
- if (qpnp_adc_tm_is_valid(chip)) {
- pr_err("invalid device\n");
- return -ENODEV;
- }
- if (qpnp_adc_tm_check_revision(chip, adc_tm->btm_channel_num))
- return -EINVAL;
- mutex_lock(&chip->adc->adc_lock);
- btm_chan = adc_tm->btm_channel_num;
- rc = qpnp_adc_tm_get_btm_idx(chip, btm_chan, &btm_chan_idx);
- if (rc < 0) {
- pr_err("Invalid btm channel idx\n");
- goto fail;
- }
- if (mode == THERMAL_DEVICE_ENABLED) {
- chip->adc->amux_prop->amux_channel =
- adc_tm->vadc_channel_num;
- channel = adc_tm->sensor_num;
- chip->adc->amux_prop->decimation =
- chip->adc->adc_channels[channel].adc_decimation;
- chip->adc->amux_prop->hw_settle_time =
- chip->adc->adc_channels[channel].hw_settle_time;
- chip->adc->amux_prop->fast_avg_setup =
- chip->adc->adc_channels[channel].fast_avg_setup;
- chip->adc->amux_prop->mode_sel =
- ADC_OP_MEASUREMENT_INTERVAL << QPNP_OP_MODE_SHIFT;
- chip->adc->amux_prop->chan_prop->low_thr = adc_tm->low_thr;
- chip->adc->amux_prop->chan_prop->high_thr = adc_tm->high_thr;
- chip->adc->amux_prop->chan_prop->tm_channel_select =
- adc_tm->btm_channel_num;
- chip->adc->amux_prop->calib_type =
- chip->adc->adc_channels[channel].calib_type;
- if (!chip->adc_tm_hc) {
- rc = qpnp_adc_tm_configure(chip, chip->adc->amux_prop);
- if (rc) {
- pr_err("adc-tm configure failed with %d\n", rc);
- goto fail;
- }
- } else {
- rc = qpnp_adc_tm_hc_configure(chip,
- chip->adc->amux_prop);
- if (rc) {
- pr_err("hc configure failed with %d\n", rc);
- goto fail;
- }
- }
- } else if (mode == THERMAL_DEVICE_DISABLED) {
- sensor_mask = 1 << adc_tm->sensor_num;
- if (!chip->adc_tm_hc) {
- mode_ctl = ADC_OP_NORMAL_MODE << QPNP_OP_MODE_SHIFT;
- rc = qpnp_adc_tm_mode_select(chip, mode_ctl);
- if (rc < 0) {
- pr_err("adc-tm single mode select failed\n");
- goto fail;
- }
- }
- /* Disable bank */
- rc = qpnp_adc_tm_disable(chip);
- if (rc < 0) {
- pr_err("adc-tm disable failed\n");
- goto fail;
- }
- if (!chip->adc_tm_hc) {
- /* Check if a conversion is in progress */
- rc = qpnp_adc_tm_req_sts_check(chip);
- if (rc < 0) {
- pr_err("adc-tm req_sts check failed\n");
- goto fail;
- }
- rc = qpnp_adc_tm_reg_update(chip,
- QPNP_ADC_TM_MULTI_MEAS_EN,
- sensor_mask, false);
- if (rc < 0) {
- pr_err("multi measurement update failed\n");
- goto fail;
- }
- } else {
- rc = qpnp_adc_tm_reg_update(chip,
- QPNP_BTM_Mn_EN(btm_chan_idx),
- QPNP_BTM_Mn_MEAS_EN, false);
- if (rc < 0) {
- pr_err("multi measurement disable failed\n");
- goto fail;
- }
- }
- rc = qpnp_adc_tm_enable_if_channel_meas(chip);
- if (rc < 0) {
- pr_err("re-enabling measurement failed\n");
- goto fail;
- }
- }
- adc_tm->mode = mode;
- fail:
- mutex_unlock(&chip->adc->adc_lock);
- return 0;
- }
- static int qpnp_adc_tm_activate_trip_type(struct qpnp_adc_tm_sensor *adc_tm,
- int trip, enum thermal_trip_activation_mode mode)
- {
- struct qpnp_adc_tm_chip *chip = adc_tm->chip;
- int rc = 0, sensor_mask = 0;
- u8 thr_int_en = 0;
- bool state = false;
- uint32_t btm_chan_idx = 0, btm_chan = 0;
- if (qpnp_adc_tm_is_valid(chip))
- return -ENODEV;
- if (qpnp_adc_tm_check_revision(chip, adc_tm->btm_channel_num))
- return -EINVAL;
- if (mode == THERMAL_TRIP_ACTIVATION_ENABLED)
- state = true;
- sensor_mask = 1 << adc_tm->sensor_num;
- pr_debug("Sensor number:%x with state:%d\n",
- adc_tm->sensor_num, state);
- btm_chan = adc_tm->btm_channel_num;
- rc = qpnp_adc_tm_get_btm_idx(chip, btm_chan, &btm_chan_idx);
- if (rc < 0) {
- pr_err("Invalid btm channel idx\n");
- return rc;
- }
- switch (trip) {
- case ADC_TM_TRIP_HIGH_WARM:
- /* low_thr (lower voltage) for higher temp */
- thr_int_en = adc_tm_data[btm_chan_idx].low_thr_int_chan_en;
- if (!chip->adc_tm_hc)
- rc = qpnp_adc_tm_reg_update(chip,
- QPNP_ADC_TM_LOW_THR_INT_EN,
- sensor_mask, state);
- else
- rc = qpnp_adc_tm_reg_update(chip,
- QPNP_BTM_Mn_EN(btm_chan_idx),
- QPNP_BTM_Mn_LOW_THR_INT_EN, state);
- if (rc)
- pr_err("channel:%x failed\n", btm_chan);
- break;
- case ADC_TM_TRIP_LOW_COOL:
- /* high_thr (higher voltage) for cooler temp */
- thr_int_en = adc_tm_data[btm_chan_idx].high_thr_int_chan_en;
- if (!chip->adc_tm_hc)
- rc = qpnp_adc_tm_reg_update(chip,
- QPNP_ADC_TM_HIGH_THR_INT_EN,
- sensor_mask, state);
- else
- rc = qpnp_adc_tm_reg_update(chip,
- QPNP_BTM_Mn_EN(btm_chan_idx),
- QPNP_BTM_Mn_HIGH_THR_INT_EN, state);
- if (rc)
- pr_err("channel:%x failed\n", btm_chan);
- break;
- default:
- return -EINVAL;
- }
- return rc;
- }
- static int qpnp_adc_tm_set_trip_temp(void *data, int low_temp, int high_temp)
- {
- struct qpnp_adc_tm_sensor *adc_tm = data;
- struct qpnp_adc_tm_chip *chip = adc_tm->chip;
- struct qpnp_adc_tm_config tm_config;
- u8 trip_cool_thr0, trip_cool_thr1, trip_warm_thr0, trip_warm_thr1;
- uint16_t reg_low_thr_lsb, reg_low_thr_msb;
- uint16_t reg_high_thr_lsb, reg_high_thr_msb;
- int rc = 0;
- uint32_t btm_chan = 0, btm_chan_idx = 0;
- int emul_temp;
- if (qpnp_adc_tm_is_valid(chip))
- return -ENODEV;
- if (qpnp_adc_tm_check_revision(chip, adc_tm->btm_channel_num))
- return -EINVAL;
- tm_config.channel = adc_tm->vadc_channel_num;
- tm_config.high_thr_temp = tm_config.low_thr_temp = 0;
- if (high_temp != INT_MAX)
- tm_config.high_thr_temp = high_temp;
- if (low_temp != INT_MIN)
- tm_config.low_thr_temp = low_temp;
- if ((high_temp == INT_MAX) && (low_temp == INT_MIN)) {
- pr_err("No trips to set\n");
- return -EINVAL;
- }
- /* Set threshold to extremely value while emul temp set */
- emul_temp = adc_tm->emul_temperature;
- if (emul_temp) {
- high_temp = INT_MAX;
- low_temp = INT_MIN;
- }
- pr_debug("requested a high - %d and low - %d\n",
- tm_config.high_thr_temp, tm_config.low_thr_temp);
- rc = qpnp_adc_tm_scale_therm_voltage_pu2(chip->vadc_dev,
- chip->adc->adc_prop, &tm_config);
- if (rc < 0) {
- pr_err("Failed to lookup the adc-tm thresholds\n");
- return rc;
- }
- trip_warm_thr0 = ((tm_config.low_thr_voltage << 24) >> 24);
- trip_warm_thr1 = ((tm_config.low_thr_voltage << 16) >> 24);
- trip_cool_thr0 = ((tm_config.high_thr_voltage << 24) >> 24);
- trip_cool_thr1 = ((tm_config.high_thr_voltage << 16) >> 24);
- pr_debug("low_thr:0x%llx, high_thr:0x%llx\n", tm_config.low_thr_voltage,
- tm_config.high_thr_voltage);
- btm_chan = adc_tm->btm_channel_num;
- rc = qpnp_adc_tm_get_btm_idx(chip, btm_chan, &btm_chan_idx);
- if (rc < 0) {
- pr_err("Invalid btm channel idx\n");
- return rc;
- }
- if (!chip->adc_tm_hc) {
- reg_low_thr_lsb = adc_tm_data[btm_chan_idx].low_thr_lsb_addr;
- reg_low_thr_msb = adc_tm_data[btm_chan_idx].low_thr_msb_addr;
- reg_high_thr_lsb = adc_tm_data[btm_chan_idx].high_thr_lsb_addr;
- reg_high_thr_msb = adc_tm_data[btm_chan_idx].high_thr_msb_addr;
- } else {
- reg_low_thr_lsb = QPNP_BTM_Mn_LOW_THR0(btm_chan_idx);
- reg_low_thr_msb = QPNP_BTM_Mn_LOW_THR1(btm_chan_idx);
- reg_high_thr_lsb = QPNP_BTM_Mn_HIGH_THR0(btm_chan_idx);
- reg_high_thr_msb = QPNP_BTM_Mn_HIGH_THR1(btm_chan_idx);
- }
- if (high_temp != INT_MAX) {
- rc = qpnp_adc_tm_write_reg(chip, reg_low_thr_lsb,
- trip_cool_thr0, 1);
- if (rc) {
- pr_err("adc-tm_tm read threshold err\n");
- return rc;
- }
- rc = qpnp_adc_tm_write_reg(chip, reg_low_thr_msb,
- trip_cool_thr1, 1);
- if (rc) {
- pr_err("adc-tm_tm read threshold err\n");
- return rc;
- }
- adc_tm->low_thr = tm_config.high_thr_voltage;
- rc = qpnp_adc_tm_activate_trip_type(adc_tm,
- ADC_TM_TRIP_HIGH_WARM,
- THERMAL_TRIP_ACTIVATION_ENABLED);
- if (rc) {
- pr_err("adc-tm warm activation failed\n");
- return rc;
- }
- } else {
- rc = qpnp_adc_tm_activate_trip_type(adc_tm,
- ADC_TM_TRIP_HIGH_WARM,
- THERMAL_TRIP_ACTIVATION_DISABLED);
- if (rc) {
- pr_err("adc-tm warm deactivation failed\n");
- return rc;
- }
- }
- if (low_temp != INT_MIN) {
- rc = qpnp_adc_tm_write_reg(chip, reg_high_thr_lsb,
- trip_warm_thr0, 1);
- if (rc) {
- pr_err("adc-tm_tm read threshold err\n");
- return rc;
- }
- rc = qpnp_adc_tm_write_reg(chip, reg_high_thr_msb,
- trip_warm_thr1, 1);
- if (rc) {
- pr_err("adc-tm_tm read threshold err\n");
- return rc;
- }
- adc_tm->high_thr = tm_config.low_thr_voltage;
- rc = qpnp_adc_tm_activate_trip_type(adc_tm,
- ADC_TM_TRIP_LOW_COOL,
- THERMAL_TRIP_ACTIVATION_ENABLED);
- if (rc) {
- pr_err("adc-tm cool activation failed\n");
- return rc;
- }
- } else {
- rc = qpnp_adc_tm_activate_trip_type(adc_tm,
- ADC_TM_TRIP_LOW_COOL,
- THERMAL_TRIP_ACTIVATION_DISABLED);
- if (rc) {
- pr_err("adc-tm cool deactivation failed\n");
- return rc;
- }
- }
- if ((high_temp != INT_MAX) || (low_temp != INT_MIN)) {
- rc = qpnp_adc_tm_set_mode(adc_tm, THERMAL_DEVICE_ENABLED);
- if (rc) {
- pr_err("sensor enabled failed\n");
- return rc;
- }
- } else {
- rc = qpnp_adc_tm_set_mode(adc_tm, THERMAL_DEVICE_DISABLED);
- if (rc) {
- pr_err("sensor disable failed\n");
- return rc;
- }
- }
- return 0;
- }
- static int qpnp_adc_tm_set_emul_temp(void *data, int emul_temp)
- {
- struct qpnp_adc_tm_sensor *adc_tm_sensor = data;
- adc_tm_sensor->emul_temperature = emul_temp;
- return 0;
- }
- static void notify_battery_therm(struct qpnp_adc_tm_sensor *adc_tm)
- {
- struct qpnp_adc_thr_client_info *client_info = NULL;
- list_for_each_entry(client_info,
- &adc_tm->thr_list, list) {
- /* Batt therm's warm temperature translates to low voltage */
- if (client_info->notify_low_thr) {
- /* HIGH_STATE = WARM_TEMP for battery client */
- client_info->btm_param->threshold_notification(
- ADC_TM_WARM_STATE, client_info->btm_param->btm_ctx);
- client_info->notify_low_thr = false;
- }
- /* Batt therm's cool temperature translates to high voltage */
- if (client_info->notify_high_thr) {
- /* LOW_STATE = COOL_TEMP for battery client */
- client_info->btm_param->threshold_notification(
- ADC_TM_COOL_STATE, client_info->btm_param->btm_ctx);
- client_info->notify_high_thr = false;
- }
- }
- }
- static void notify_clients(struct qpnp_adc_tm_sensor *adc_tm)
- {
- struct qpnp_adc_thr_client_info *client_info = NULL;
- list_for_each_entry(client_info,
- &adc_tm->thr_list, list) {
- /* For non batt therm clients */
- if (client_info->notify_low_thr) {
- if (client_info->btm_param->threshold_notification
- != NULL) {
- pr_debug("notify kernel with low state\n");
- client_info->btm_param->threshold_notification(
- ADC_TM_LOW_STATE,
- client_info->btm_param->btm_ctx);
- client_info->notify_low_thr = false;
- }
- }
- if (client_info->notify_high_thr) {
- if (client_info->btm_param->threshold_notification
- != NULL) {
- pr_debug("notify kernel with high state\n");
- client_info->btm_param->threshold_notification(
- ADC_TM_HIGH_STATE,
- client_info->btm_param->btm_ctx);
- client_info->notify_high_thr = false;
- }
- }
- }
- }
- static int qpnp_adc_read_temp(void *data, int *temp)
- {
- struct qpnp_adc_tm_sensor *adc_tm_sensor = data;
- struct qpnp_adc_tm_chip *chip = adc_tm_sensor->chip;
- struct qpnp_vadc_result result;
- int rc = 0;
- int emul_temp;
- emul_temp = adc_tm_sensor->emul_temperature;
- if (emul_temp) {
- *temp = emul_temp;
- return rc;
- }
- rc = qpnp_vadc_read(chip->vadc_dev,
- adc_tm_sensor->vadc_channel_num, &result);
- if (rc)
- return rc;
- *temp = result.physical;
- return rc;
- }
- static void notify_adc_tm_fn(struct work_struct *work)
- {
- struct qpnp_adc_tm_sensor *adc_tm = container_of(work,
- struct qpnp_adc_tm_sensor, work);
- int temp;
- int ret;
- if (adc_tm->thermal_node) {
- pr_debug("notifying uspace client\n");
- ret = qpnp_adc_read_temp(adc_tm, &temp);
- if (ret)
- of_thermal_handle_trip(adc_tm->tz_dev);
- else
- of_thermal_handle_trip_temp(adc_tm->tz_dev, temp);
- } else {
- if (adc_tm->scale_type == SCALE_RBATT_THERM)
- notify_battery_therm(adc_tm);
- else
- notify_clients(adc_tm);
- }
- }
- static int qpnp_adc_tm_recalib_request_check(struct qpnp_adc_tm_chip *chip,
- int sensor_num, u8 status_high, u8 *notify_check)
- {
- int rc = 0;
- u8 sensor_mask = 0, mode_ctl = 0;
- int32_t old_thr = 0, new_thr = 0;
- uint32_t channel, btm_chan_num, scale_type;
- struct qpnp_vadc_result result;
- struct qpnp_adc_thr_client_info *client_info = NULL;
- struct list_head *thr_list;
- bool status = false;
- if (!chip->adc_tm_recalib_check) {
- *notify_check = 1;
- return rc;
- }
- list_for_each(thr_list, &chip->sensor[sensor_num].thr_list) {
- client_info = list_entry(thr_list,
- struct qpnp_adc_thr_client_info, list);
- channel = client_info->btm_param->channel;
- btm_chan_num = chip->sensor[sensor_num].btm_channel_num;
- sensor_mask = 1 << sensor_num;
- rc = qpnp_vadc_read(chip->vadc_dev, channel, &result);
- if (rc < 0) {
- pr_err("failure to read vadc channel=%d\n",
- client_info->btm_param->channel);
- goto fail;
- }
- new_thr = result.physical;
- if (status_high)
- old_thr = client_info->btm_param->high_thr;
- else
- old_thr = client_info->btm_param->low_thr;
- if (new_thr > old_thr)
- status = (status_high) ? true : false;
- else
- status = (status_high) ? false : true;
- pr_debug(
- "recalib:sen=%d, new_thr=%d, new_thr_adc_code=0x%x, old_thr=%d status=%d valid_status=%d\n",
- sensor_num, new_thr, result.adc_code,
- old_thr, status_high, status);
- rc = qpnp_adc_tm_read_thr_value(chip, btm_chan_num);
- if (rc < 0) {
- pr_err("adc-tm thresholds read failed\n");
- goto fail;
- }
- if (status) {
- *notify_check = 1;
- pr_debug("Client can be notify\n");
- return rc;
- }
- pr_debug("Client can not be notify, restart measurement\n");
- /* Set measurement in single measurement mode */
- mode_ctl = ADC_OP_NORMAL_MODE << QPNP_OP_MODE_SHIFT;
- rc = qpnp_adc_tm_mode_select(chip, mode_ctl);
- if (rc < 0) {
- pr_err("adc-tm single mode select failed\n");
- goto fail;
- }
- /* Disable bank */
- rc = qpnp_adc_tm_disable(chip);
- if (rc < 0) {
- pr_err("adc-tm disable failed\n");
- goto fail;
- }
- /* Check if a conversion is in progress */
- rc = qpnp_adc_tm_req_sts_check(chip);
- if (rc < 0) {
- pr_err("adc-tm req_sts check failed\n");
- goto fail;
- }
- rc = qpnp_adc_tm_reg_update(chip, QPNP_ADC_TM_LOW_THR_INT_EN,
- sensor_mask, false);
- if (rc < 0) {
- pr_err("low threshold int write failed\n");
- goto fail;
- }
- rc = qpnp_adc_tm_reg_update(chip, QPNP_ADC_TM_HIGH_THR_INT_EN,
- sensor_mask, false);
- if (rc < 0) {
- pr_err("high threshold int enable failed\n");
- goto fail;
- }
- rc = qpnp_adc_tm_reg_update(chip, QPNP_ADC_TM_MULTI_MEAS_EN,
- sensor_mask, false);
- if (rc < 0) {
- pr_err("multi measurement en failed\n");
- goto fail;
- }
- /* restart measurement */
- scale_type = chip->sensor[sensor_num].scale_type;
- chip->adc->amux_prop->amux_channel = channel;
- chip->adc->amux_prop->decimation =
- chip->adc->adc_channels[sensor_num].adc_decimation;
- chip->adc->amux_prop->hw_settle_time =
- chip->adc->adc_channels[sensor_num].hw_settle_time;
- chip->adc->amux_prop->fast_avg_setup =
- chip->adc->adc_channels[sensor_num].fast_avg_setup;
- chip->adc->amux_prop->mode_sel =
- ADC_OP_MEASUREMENT_INTERVAL << QPNP_OP_MODE_SHIFT;
- adc_tm_rscale_fn[scale_type].chan(chip->vadc_dev,
- client_info->btm_param,
- &chip->adc->amux_prop->chan_prop->low_thr,
- &chip->adc->amux_prop->chan_prop->high_thr);
- qpnp_adc_tm_add_to_list(chip, sensor_num,
- client_info->btm_param,
- chip->adc->amux_prop->chan_prop);
- chip->adc->amux_prop->chan_prop->tm_channel_select =
- chip->sensor[sensor_num].btm_channel_num;
- chip->adc->amux_prop->chan_prop->state_request =
- client_info->btm_param->state_request;
- rc = qpnp_adc_tm_configure(chip, chip->adc->amux_prop);
- if (rc) {
- pr_err("adc-tm configure failed with %d\n", rc);
- goto fail;
- }
- *notify_check = 0;
- pr_debug("BTM channel reconfigured for measuremnt\n");
- }
- fail:
- return rc;
- }
- static int qpnp_adc_tm_disable_rearm_high_thresholds(
- struct qpnp_adc_tm_chip *chip, int sensor_num)
- {
- struct qpnp_adc_thr_client_info *client_info = NULL;
- struct list_head *thr_list;
- uint32_t btm_chan_num = 0, btm_chan_idx = 0;
- u8 sensor_mask = 0, notify_check = 0;
- int rc = 0;
- btm_chan_num = chip->sensor[sensor_num].btm_channel_num;
- rc = qpnp_adc_tm_get_btm_idx(chip, btm_chan_num, &btm_chan_idx);
- if (rc < 0) {
- pr_err("Invalid btm channel idx\n");
- return rc;
- }
- pr_debug("high:sen:%d, hs:0x%x, ls:0x%x, meas_en:0x%x\n",
- sensor_num, chip->th_info.adc_tm_high_enable,
- chip->th_info.adc_tm_low_enable,
- chip->th_info.qpnp_adc_tm_meas_en);
- if (!chip->sensor[sensor_num].thermal_node) {
- /*
- * For non thermal registered clients such as usb_id,
- * vbatt, pmic_therm
- */
- sensor_mask = 1 << sensor_num;
- pr_debug("non thermal node - mask:%x\n", sensor_mask);
- if (!chip->adc_tm_hc) {
- rc = qpnp_adc_tm_recalib_request_check(chip,
- sensor_num, true, ¬ify_check);
- if (rc < 0 || !notify_check) {
- pr_debug("Calib recheck re-armed rc=%d\n", rc);
- chip->th_info.adc_tm_high_enable = 0;
- return rc;
- }
- } else {
- rc = qpnp_adc_tm_reg_update(chip,
- QPNP_BTM_Mn_EN(btm_chan_idx),
- QPNP_BTM_Mn_HIGH_THR_INT_EN, false);
- if (rc < 0) {
- pr_err("high threshold int update failed\n");
- return rc;
- }
- }
- } else {
- /*
- * Uses the thermal sysfs registered device to disable
- * the corresponding high voltage threshold which
- * is triggered by low temp
- */
- sensor_mask = 1 << sensor_num;
- pr_debug("thermal node with mask:%x\n", sensor_mask);
- rc = qpnp_adc_tm_activate_trip_type(
- &chip->sensor[sensor_num],
- ADC_TM_TRIP_LOW_COOL,
- THERMAL_TRIP_ACTIVATION_DISABLED);
- if (rc < 0) {
- pr_err("notify error:%d\n", sensor_num);
- return rc;
- }
- }
- list_for_each(thr_list, &chip->sensor[sensor_num].thr_list) {
- client_info = list_entry(thr_list,
- struct qpnp_adc_thr_client_info, list);
- if (client_info->high_thr_set) {
- client_info->high_thr_set = false;
- client_info->notify_high_thr = true;
- if (client_info->state_req_copy ==
- ADC_TM_HIGH_LOW_THR_ENABLE)
- client_info->state_req_copy =
- ADC_TM_LOW_THR_ENABLE;
- else
- client_info->state_req_copy =
- ADC_TM_HIGH_THR_DISABLE;
- }
- }
- qpnp_adc_tm_manage_thresholds(chip, sensor_num, btm_chan_num);
- if (!chip->adc_tm_hc) {
- rc = qpnp_adc_tm_reg_update(chip,
- QPNP_ADC_TM_MULTI_MEAS_EN,
- sensor_mask, false);
- if (rc < 0) {
- pr_err("multi meas disable failed\n");
- return rc;
- }
- } else {
- rc = qpnp_adc_tm_reg_update(chip,
- QPNP_BTM_Mn_EN(sensor_num),
- QPNP_BTM_Mn_MEAS_EN, false);
- if (rc < 0) {
- pr_err("multi meas disable failed\n");
- return rc;
- }
- }
- rc = qpnp_adc_tm_enable_if_channel_meas(chip);
- if (rc < 0) {
- pr_err("re-enabling measurement failed\n");
- return rc;
- }
- queue_work(chip->sensor[sensor_num].req_wq,
- &chip->sensor[sensor_num].work);
- return rc;
- }
- static int qpnp_adc_tm_disable_rearm_low_thresholds(
- struct qpnp_adc_tm_chip *chip, int sensor_num)
- {
- struct qpnp_adc_thr_client_info *client_info = NULL;
- struct list_head *thr_list;
- uint32_t btm_chan_num = 0, btm_chan_idx = 0;
- u8 sensor_mask = 0, notify_check = 0;
- int rc = 0;
- btm_chan_num = chip->sensor[sensor_num].btm_channel_num;
- rc = qpnp_adc_tm_get_btm_idx(chip, btm_chan_num, &btm_chan_idx);
- if (rc < 0) {
- pr_err("Invalid btm channel idx\n");
- return rc;
- }
- pr_debug("low:sen:%d, hs:0x%x, ls:0x%x, meas_en:0x%x\n",
- sensor_num, chip->th_info.adc_tm_high_enable,
- chip->th_info.adc_tm_low_enable,
- chip->th_info.qpnp_adc_tm_meas_en);
- if (!chip->sensor[sensor_num].thermal_node) {
- /*
- * For non thermal registered clients such as usb_id,
- * vbatt, pmic_therm
- */
- sensor_mask = 1 << sensor_num;
- pr_debug("non thermal node - mask:%x\n", sensor_mask);
- if (!chip->adc_tm_hc) {
- rc = qpnp_adc_tm_recalib_request_check(chip,
- sensor_num, false, ¬ify_check);
- if (rc < 0 || !notify_check) {
- pr_debug("Calib recheck re-armed rc=%d\n", rc);
- chip->th_info.adc_tm_low_enable = 0;
- return rc;
- }
- } else {
- rc = qpnp_adc_tm_reg_update(chip,
- QPNP_BTM_Mn_EN(btm_chan_idx),
- QPNP_BTM_Mn_LOW_THR_INT_EN, false);
- if (rc < 0) {
- pr_err("low threshold int update failed\n");
- return rc;
- }
- }
- } else {
- /*
- * Uses the thermal sysfs registered device to disable
- * the corresponding high voltage threshold which
- * is triggered by low temp
- */
- sensor_mask = 1 << sensor_num;
- pr_debug("thermal node with mask:%x\n", sensor_mask);
- rc = qpnp_adc_tm_activate_trip_type(
- &chip->sensor[sensor_num],
- ADC_TM_TRIP_HIGH_WARM,
- THERMAL_TRIP_ACTIVATION_DISABLED);
- if (rc < 0) {
- pr_err("notify error:%d\n", sensor_num);
- return rc;
- }
- }
- list_for_each(thr_list, &chip->sensor[sensor_num].thr_list) {
- client_info = list_entry(thr_list,
- struct qpnp_adc_thr_client_info, list);
- if (client_info->low_thr_set) {
- client_info->low_thr_set = false;
- client_info->notify_low_thr = true;
- if (client_info->state_req_copy ==
- ADC_TM_HIGH_LOW_THR_ENABLE)
- client_info->state_req_copy =
- ADC_TM_HIGH_THR_ENABLE;
- else
- client_info->state_req_copy =
- ADC_TM_LOW_THR_DISABLE;
- }
- }
- qpnp_adc_tm_manage_thresholds(chip, sensor_num, btm_chan_num);
- if (!chip->adc_tm_hc) {
- rc = qpnp_adc_tm_reg_update(chip,
- QPNP_ADC_TM_MULTI_MEAS_EN,
- sensor_mask, false);
- if (rc < 0) {
- pr_err("multi meas disable failed\n");
- return rc;
- }
- } else {
- rc = qpnp_adc_tm_reg_update(chip,
- QPNP_BTM_Mn_EN(sensor_num),
- QPNP_BTM_Mn_MEAS_EN, false);
- if (rc < 0) {
- pr_err("multi meas disable failed\n");
- return rc;
- }
- }
- rc = qpnp_adc_tm_enable_if_channel_meas(chip);
- if (rc < 0) {
- pr_err("re-enabling measurement failed\n");
- return rc;
- }
- queue_work(chip->sensor[sensor_num].req_wq,
- &chip->sensor[sensor_num].work);
- return rc;
- }
- static int qpnp_adc_tm_read_status(struct qpnp_adc_tm_chip *chip)
- {
- int rc = 0, sensor_notify_num = 0, i = 0, sensor_num = 0;
- unsigned long flags;
- if (qpnp_adc_tm_is_valid(chip))
- return -ENODEV;
- mutex_lock(&chip->adc->adc_lock);
- rc = qpnp_adc_tm_req_sts_check(chip);
- if (rc) {
- pr_err("adc-tm-tm req sts check failed with %d\n", rc);
- goto fail;
- }
- if (chip->th_info.adc_tm_high_enable) {
- spin_lock_irqsave(&chip->th_info.adc_tm_high_lock, flags);
- sensor_notify_num = chip->th_info.adc_tm_high_enable;
- chip->th_info.adc_tm_high_enable = 0;
- spin_unlock_irqrestore(&chip->th_info.adc_tm_high_lock, flags);
- while (i < chip->max_channels_available) {
- if ((sensor_notify_num & 0x1) == 1) {
- sensor_num = i;
- rc = qpnp_adc_tm_disable_rearm_high_thresholds(
- chip, sensor_num);
- if (rc < 0) {
- pr_err("rearm threshold failed\n");
- goto fail;
- }
- }
- sensor_notify_num >>= 1;
- i++;
- }
- }
- if (chip->th_info.adc_tm_low_enable) {
- spin_lock_irqsave(&chip->th_info.adc_tm_low_lock, flags);
- sensor_notify_num = chip->th_info.adc_tm_low_enable;
- chip->th_info.adc_tm_low_enable = 0;
- spin_unlock_irqrestore(&chip->th_info.adc_tm_low_lock, flags);
- i = 0;
- while (i < chip->max_channels_available) {
- if ((sensor_notify_num & 0x1) == 1) {
- sensor_num = i;
- rc = qpnp_adc_tm_disable_rearm_low_thresholds(
- chip, sensor_num);
- if (rc < 0) {
- pr_err("rearm threshold failed\n");
- goto fail;
- }
- }
- sensor_notify_num >>= 1;
- i++;
- }
- }
- fail:
- mutex_unlock(&chip->adc->adc_lock);
- return rc;
- }
- static int qpnp_adc_tm_hc_read_status(struct qpnp_adc_tm_chip *chip)
- {
- int rc = 0, sensor_num = 0;
- if (qpnp_adc_tm_is_valid(chip))
- return -ENODEV;
- pr_debug("%s\n", __func__);
- mutex_lock(&chip->adc->adc_lock);
- if (!chip->adc_tm_hc) {
- rc = qpnp_adc_tm_req_sts_check(chip);
- if (rc) {
- pr_err("adc-tm-tm req sts check failed with %d\n", rc);
- goto fail;
- }
- }
- while (sensor_num < chip->max_channels_available) {
- if (chip->sensor[sensor_num].high_thr_triggered) {
- rc = qpnp_adc_tm_disable_rearm_high_thresholds(
- chip, sensor_num);
- if (rc) {
- pr_err("rearm threshold failed\n");
- goto fail;
- }
- chip->sensor[sensor_num].high_thr_triggered = false;
- }
- sensor_num++;
- }
- sensor_num = 0;
- while (sensor_num < chip->max_channels_available) {
- if (chip->sensor[sensor_num].low_thr_triggered) {
- rc = qpnp_adc_tm_disable_rearm_low_thresholds(
- chip, sensor_num);
- if (rc) {
- pr_err("rearm threshold failed\n");
- goto fail;
- }
- chip->sensor[sensor_num].low_thr_triggered = false;
- }
- sensor_num++;
- }
- fail:
- mutex_unlock(&chip->adc->adc_lock);
- return rc;
- }
- static void qpnp_adc_tm_high_thr_work(struct work_struct *work)
- {
- struct qpnp_adc_tm_chip *chip = container_of(work,
- struct qpnp_adc_tm_chip, trigger_high_thr_work);
- int rc;
- /* disable the vote if applicable */
- if (chip->adc_vote_enable && chip->adc->hkadc_ldo &&
- chip->adc->hkadc_ldo_ok) {
- qpnp_adc_disable_voltage(chip->adc);
- chip->adc_vote_enable = false;
- }
- pr_debug("thr:0x%x\n", chip->th_info.adc_tm_high_enable);
- if (!chip->adc_tm_hc) {
- rc = qpnp_adc_tm_read_status(chip);
- if (rc < 0)
- pr_err("adc-tm high thr work failed\n");
- } else {
- rc = qpnp_adc_tm_hc_read_status(chip);
- if (rc < 0)
- pr_err("adc-tm-hc high thr work failed\n");
- }
- }
- static irqreturn_t qpnp_adc_tm_high_thr_isr(int irq, void *data)
- {
- struct qpnp_adc_tm_chip *chip = data;
- u8 mode_ctl = 0, status1 = 0, sensor_mask = 0;
- int rc = 0, sensor_notify_num = 0, i = 0, sensor_num = 0;
- mode_ctl = ADC_OP_NORMAL_MODE << QPNP_OP_MODE_SHIFT;
- /* Set measurement in single measurement mode */
- qpnp_adc_tm_mode_select(chip, mode_ctl);
- qpnp_adc_tm_disable(chip);
- rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_TM_STATUS1, &status1, 1);
- if (rc) {
- pr_err("adc-tm read status1 failed\n");
- return IRQ_HANDLED;
- }
- rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_TM_STATUS_HIGH,
- &chip->th_info.status_high, 1);
- if (rc) {
- pr_err("adc-tm-tm read status high failed with %d\n", rc);
- return IRQ_HANDLED;
- }
- rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_TM_HIGH_THR_INT_EN,
- &chip->th_info.adc_tm_high_thr_set, 1);
- if (rc) {
- pr_err("adc-tm-tm read high thr failed with %d\n", rc);
- return IRQ_HANDLED;
- }
- /* Check which interrupt threshold is lower and measure against the
- * enabled channel
- */
- rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_TM_MULTI_MEAS_EN,
- &chip->th_info.qpnp_adc_tm_meas_en, 1);
- if (rc) {
- pr_err("adc-tm-tm read status high failed with %d\n", rc);
- return IRQ_HANDLED;
- }
- chip->th_info.adc_tm_high_enable = chip->th_info.qpnp_adc_tm_meas_en &
- chip->th_info.status_high;
- chip->th_info.adc_tm_high_enable &= chip->th_info.adc_tm_high_thr_set;
- sensor_notify_num = chip->th_info.adc_tm_high_enable;
- while (i < chip->max_channels_available) {
- if ((sensor_notify_num & 0x1) == 1)
- sensor_num = i;
- sensor_notify_num >>= 1;
- i++;
- }
- if (!chip->sensor[sensor_num].thermal_node) {
- sensor_mask = 1 << sensor_num;
- rc = qpnp_adc_tm_reg_update(chip,
- QPNP_ADC_TM_HIGH_THR_INT_EN,
- sensor_mask, false);
- if (rc < 0) {
- pr_err("high threshold int read failed\n");
- return IRQ_HANDLED;
- }
- } else {
- /*
- * Uses the thermal sysfs registered device to disable
- * the corresponding high voltage threshold which
- * is triggered by low temp
- */
- pr_debug("thermal node with mask:%x\n", sensor_mask);
- rc = qpnp_adc_tm_activate_trip_type(
- &chip->sensor[sensor_num],
- ADC_TM_TRIP_LOW_COOL,
- THERMAL_TRIP_ACTIVATION_DISABLED);
- if (rc < 0) {
- pr_err("notify error:%d\n", sensor_num);
- return IRQ_HANDLED;
- }
- }
- queue_work(chip->high_thr_wq, &chip->trigger_high_thr_work);
- return IRQ_HANDLED;
- }
- static void qpnp_adc_tm_low_thr_work(struct work_struct *work)
- {
- struct qpnp_adc_tm_chip *chip = container_of(work,
- struct qpnp_adc_tm_chip, trigger_low_thr_work);
- int rc;
- /* disable the vote if applicable */
- if (chip->adc_vote_enable && chip->adc->hkadc_ldo &&
- chip->adc->hkadc_ldo_ok) {
- qpnp_adc_disable_voltage(chip->adc);
- chip->adc_vote_enable = false;
- }
- pr_debug("thr:0x%x\n", chip->th_info.adc_tm_low_enable);
- if (!chip->adc_tm_hc) {
- rc = qpnp_adc_tm_read_status(chip);
- if (rc < 0)
- pr_err("adc-tm low thr work failed\n");
- } else {
- rc = qpnp_adc_tm_hc_read_status(chip);
- if (rc < 0)
- pr_err("adc-tm-hc low thr work failed\n");
- }
- }
- static irqreturn_t qpnp_adc_tm_low_thr_isr(int irq, void *data)
- {
- struct qpnp_adc_tm_chip *chip = data;
- u8 mode_ctl = 0, status1 = 0, sensor_mask = 0;
- int rc = 0, sensor_notify_num = 0, i = 0, sensor_num = 0;
- mode_ctl = ADC_OP_NORMAL_MODE << QPNP_OP_MODE_SHIFT;
- /* Set measurement in single measurement mode */
- qpnp_adc_tm_mode_select(chip, mode_ctl);
- qpnp_adc_tm_disable(chip);
- rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_TM_STATUS1, &status1, 1);
- if (rc) {
- pr_err("adc-tm read status1 failed\n");
- return IRQ_HANDLED;
- }
- rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_TM_STATUS_LOW,
- &chip->th_info.status_low, 1);
- if (rc) {
- pr_err("adc-tm-tm read status low failed with %d\n", rc);
- return IRQ_HANDLED;
- }
- rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_TM_LOW_THR_INT_EN,
- &chip->th_info.adc_tm_low_thr_set, 1);
- if (rc) {
- pr_err("adc-tm-tm read low thr failed with %d\n", rc);
- return IRQ_HANDLED;
- }
- rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_TM_MULTI_MEAS_EN,
- &chip->th_info.qpnp_adc_tm_meas_en, 1);
- if (rc) {
- pr_err("adc-tm-tm read status high failed with %d\n", rc);
- return IRQ_HANDLED;
- }
- chip->th_info.adc_tm_low_enable = chip->th_info.qpnp_adc_tm_meas_en &
- chip->th_info.status_low;
- chip->th_info.adc_tm_low_enable &= chip->th_info.adc_tm_low_thr_set;
- sensor_notify_num = chip->th_info.adc_tm_low_enable;
- while (i < chip->max_channels_available) {
- if ((sensor_notify_num & 0x1) == 1)
- sensor_num = i;
- sensor_notify_num >>= 1;
- i++;
- }
- if (!chip->sensor[sensor_num].thermal_node) {
- sensor_mask = 1 << sensor_num;
- rc = qpnp_adc_tm_reg_update(chip,
- QPNP_ADC_TM_LOW_THR_INT_EN,
- sensor_mask, false);
- if (rc < 0) {
- pr_err("low threshold int read failed\n");
- return IRQ_HANDLED;
- }
- } else {
- /*
- * Uses the thermal sysfs registered device to disable
- * the corresponding low voltage threshold which
- * is triggered by high temp
- */
- pr_debug("thermal node with mask:%x\n", sensor_mask);
- rc = qpnp_adc_tm_activate_trip_type(
- &chip->sensor[sensor_num],
- ADC_TM_TRIP_HIGH_WARM,
- THERMAL_TRIP_ACTIVATION_DISABLED);
- if (rc < 0) {
- pr_err("notify error:%d\n", sensor_num);
- return IRQ_HANDLED;
- }
- }
- queue_work(chip->low_thr_wq, &chip->trigger_low_thr_work);
- return IRQ_HANDLED;
- }
- static int qpnp_adc_tm_rc_check_sensor_trip(struct qpnp_adc_tm_chip *chip,
- u8 status_low, u8 status_high, int i,
- int *sensor_low_notify_num, int *sensor_high_notify_num)
- {
- int rc = 0;
- u8 ctl = 0, sensor_mask = 0;
- if (((status_low & 0x1) == 1) || ((status_high & 0x1) == 1)) {
- rc = qpnp_adc_tm_read_reg(chip,
- QPNP_BTM_Mn_EN(i), &ctl, 1);
- if (rc) {
- pr_err("ctl read failed with %d\n", rc);
- return IRQ_HANDLED;
- }
- if ((status_low & 0x1) && (ctl & QPNP_BTM_Mn_MEAS_EN)
- && (ctl & QPNP_BTM_Mn_LOW_THR_INT_EN)) {
- /* Mask the corresponding low threshold interrupt en */
- if (!chip->sensor[i].thermal_node) {
- rc = qpnp_adc_tm_reg_update(chip,
- QPNP_BTM_Mn_EN(i),
- QPNP_BTM_Mn_LOW_THR_INT_EN, false);
- if (rc < 0) {
- pr_err("low thr_int en failed\n");
- return IRQ_HANDLED;
- }
- } else {
- /*
- * Uses the thermal sysfs registered device to disable
- * the corresponding low voltage threshold which
- * is triggered by high temp
- */
- pr_debug("thermal node with mask:%x\n", sensor_mask);
- rc = qpnp_adc_tm_activate_trip_type(
- &chip->sensor[i],
- ADC_TM_TRIP_HIGH_WARM,
- THERMAL_TRIP_ACTIVATION_DISABLED);
- if (rc < 0) {
- pr_err("notify error:%d\n", i);
- return IRQ_HANDLED;
- }
- }
- *sensor_low_notify_num |= (status_low & 0x1);
- chip->sensor[i].low_thr_triggered = true;
- }
- if ((status_high & 0x1) && (ctl & QPNP_BTM_Mn_MEAS_EN) &&
- (ctl & QPNP_BTM_Mn_HIGH_THR_INT_EN)) {
- /* Mask the corresponding high threshold interrupt en */
- if (!chip->sensor[i].thermal_node) {
- rc = qpnp_adc_tm_reg_update(chip,
- QPNP_BTM_Mn_EN(i),
- QPNP_BTM_Mn_HIGH_THR_INT_EN, false);
- if (rc < 0) {
- pr_err("high thr_int en failed\n");
- return IRQ_HANDLED;
- }
- } else {
- /*
- * Uses the thermal sysfs registered device to disable
- * the corresponding high voltage threshold which
- * is triggered by low temp
- */
- pr_debug("thermal node with mask:%x\n", i);
- rc = qpnp_adc_tm_activate_trip_type(
- &chip->sensor[i],
- ADC_TM_TRIP_LOW_COOL,
- THERMAL_TRIP_ACTIVATION_DISABLED);
- if (rc < 0) {
- pr_err("notify error:%d\n", i);
- return IRQ_HANDLED;
- }
- }
- *sensor_high_notify_num |= (status_high & 0x1);
- chip->sensor[i].high_thr_triggered = true;
- }
- }
- return rc;
- }
- static irqreturn_t qpnp_adc_tm_rc_thr_isr(int irq, void *data)
- {
- struct qpnp_adc_tm_chip *chip = data;
- u8 status_low = 0, status_high = 0;
- int rc = 0, sensor_low_notify_num = 0, i = 0;
- int sensor_high_notify_num = 0;
- rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_TM_STATUS_LOW,
- &status_low, 1);
- if (rc) {
- pr_err("adc-tm-tm read status low failed with %d\n", rc);
- return IRQ_HANDLED;
- }
- if (status_low)
- chip->th_info.adc_tm_low_enable = status_low;
- rc = qpnp_adc_tm_read_reg(chip, QPNP_ADC_TM_STATUS_HIGH,
- &status_high, 1);
- if (rc) {
- pr_err("adc-tm-tm read status high failed with %d\n", rc);
- return IRQ_HANDLED;
- }
- if (status_high)
- chip->th_info.adc_tm_high_enable = status_high;
- while (i < chip->max_channels_available) {
- rc = qpnp_adc_tm_rc_check_sensor_trip(chip,
- status_low, status_high, i,
- &sensor_low_notify_num,
- &sensor_high_notify_num);
- if (rc) {
- pr_err("Sensor trip read failed\n");
- return IRQ_HANDLED;
- }
- status_low >>= 1;
- status_high >>= 1;
- i++;
- }
- if (sensor_low_notify_num) {
- pm_wakeup_event(chip->dev,
- QPNP_ADC_WAKEUP_SRC_TIMEOUT_MS);
- queue_work(chip->low_thr_wq, &chip->trigger_low_thr_work);
- }
- if (sensor_high_notify_num) {
- pm_wakeup_event(chip->dev,
- QPNP_ADC_WAKEUP_SRC_TIMEOUT_MS);
- queue_work(chip->high_thr_wq,
- &chip->trigger_high_thr_work);
- }
- return IRQ_HANDLED;
- }
- static struct thermal_zone_of_device_ops qpnp_adc_tm_thermal_ops = {
- .get_temp = qpnp_adc_read_temp,
- .set_trips = qpnp_adc_tm_set_trip_temp,
- .set_emul_temp = qpnp_adc_tm_set_emul_temp,
- };
- int32_t qpnp_adc_tm_channel_measure(struct qpnp_adc_tm_chip *chip,
- struct qpnp_adc_tm_btm_param *param)
- {
- uint32_t channel, amux_prescaling, dt_index = 0, scale_type = 0;
- int rc = 0, i = 0, version = 0;
- bool chan_found = false;
- if (qpnp_adc_tm_is_valid(chip)) {
- pr_err("chip not valid\n");
- return -ENODEV;
- }
- if (param->threshold_notification == NULL) {
- pr_debug("No notification for high/low temp??\n");
- return -EINVAL;
- }
- mutex_lock(&chip->adc->adc_lock);
- channel = param->channel;
- if (channel == VSYS) {
- version = qpnp_adc_get_revid_version(chip->dev);
- if (version == QPNP_REV_ID_PM8950_1_0) {
- pr_debug("Channel not supported\n");
- rc = -EINVAL;
- goto fail_unlock;
- }
- }
- while (i < chip->max_channels_available) {
- if (chip->adc->adc_channels[i].channel_num ==
- channel) {
- dt_index = i;
- chan_found = true;
- i++;
- } else
- i++;
- }
- if (!chan_found) {
- pr_err("not a valid ADC_TM channel\n");
- rc = -EINVAL;
- goto fail_unlock;
- }
- rc = qpnp_adc_tm_check_revision(chip,
- chip->sensor[dt_index].btm_channel_num);
- if (rc < 0)
- goto fail_unlock;
- scale_type = chip->adc->adc_channels[dt_index].adc_scale_fn;
- if (scale_type >= SCALE_RSCALE_NONE) {
- rc = -EBADF;
- goto fail_unlock;
- }
- amux_prescaling =
- chip->adc->adc_channels[dt_index].chan_path_prescaling;
- if (amux_prescaling >= PATH_SCALING_NONE) {
- rc = -EINVAL;
- goto fail_unlock;
- }
- pr_debug("channel:%d, scale_type:%d, dt_idx:%d",
- channel, scale_type, dt_index);
- param->gain_num = qpnp_vadc_amux_scaling_ratio[amux_prescaling].num;
- param->gain_den = qpnp_vadc_amux_scaling_ratio[amux_prescaling].den;
- param->adc_tm_hc = chip->adc_tm_hc;
- param->full_scale_code = chip->adc->adc_prop->full_scale_code;
- chip->adc->amux_prop->amux_channel = channel;
- chip->adc->amux_prop->decimation =
- chip->adc->adc_channels[dt_index].adc_decimation;
- chip->adc->amux_prop->hw_settle_time =
- chip->adc->adc_channels[dt_index].hw_settle_time;
- chip->adc->amux_prop->fast_avg_setup =
- chip->adc->adc_channels[dt_index].fast_avg_setup;
- chip->adc->amux_prop->mode_sel =
- ADC_OP_MEASUREMENT_INTERVAL << QPNP_OP_MODE_SHIFT;
- adc_tm_rscale_fn[scale_type].chan(chip->vadc_dev, param,
- &chip->adc->amux_prop->chan_prop->low_thr,
- &chip->adc->amux_prop->chan_prop->high_thr);
- qpnp_adc_tm_add_to_list(chip, dt_index, param,
- chip->adc->amux_prop->chan_prop);
- chip->adc->amux_prop->chan_prop->tm_channel_select =
- chip->sensor[dt_index].btm_channel_num;
- chip->adc->amux_prop->chan_prop->state_request =
- param->state_request;
- chip->adc->amux_prop->calib_type =
- chip->adc->adc_channels[dt_index].calib_type;
- if (!chip->adc_tm_hc) {
- rc = qpnp_adc_tm_configure(chip, chip->adc->amux_prop);
- if (rc) {
- pr_err("adc-tm configure failed with %d\n", rc);
- goto fail_unlock;
- }
- } else {
- rc = qpnp_adc_tm_hc_configure(chip, chip->adc->amux_prop);
- if (rc) {
- pr_err("adc-tm hc configure failed with %d\n", rc);
- goto fail_unlock;
- }
- }
- chip->sensor[dt_index].scale_type = scale_type;
- fail_unlock:
- mutex_unlock(&chip->adc->adc_lock);
- return rc;
- }
- EXPORT_SYMBOL(qpnp_adc_tm_channel_measure);
- int32_t qpnp_adc_tm_disable_chan_meas(struct qpnp_adc_tm_chip *chip,
- struct qpnp_adc_tm_btm_param *param)
- {
- uint32_t channel, dt_index = 0, btm_chan_num;
- u8 sensor_mask = 0, mode_ctl = 0;
- int rc = 0;
- if (qpnp_adc_tm_is_valid(chip))
- return -ENODEV;
- mutex_lock(&chip->adc->adc_lock);
- if (!chip->adc_tm_hc) {
- /* Set measurement in single measurement mode */
- mode_ctl = ADC_OP_NORMAL_MODE << QPNP_OP_MODE_SHIFT;
- rc = qpnp_adc_tm_mode_select(chip, mode_ctl);
- if (rc < 0) {
- pr_err("adc-tm single mode select failed\n");
- goto fail;
- }
- }
- /* Disable bank */
- rc = qpnp_adc_tm_disable(chip);
- if (rc < 0) {
- pr_err("adc-tm disable failed\n");
- goto fail;
- }
- if (!chip->adc_tm_hc) {
- /* Check if a conversion is in progress */
- rc = qpnp_adc_tm_req_sts_check(chip);
- if (rc < 0) {
- pr_err("adc-tm req_sts check failed\n");
- goto fail;
- }
- }
- channel = param->channel;
- while ((chip->adc->adc_channels[dt_index].channel_num
- != channel) && (dt_index < chip->max_channels_available))
- dt_index++;
- if (dt_index >= chip->max_channels_available) {
- pr_err("not a valid ADC_TMN channel\n");
- rc = -EINVAL;
- goto fail;
- }
- btm_chan_num = chip->sensor[dt_index].btm_channel_num;
- if (!chip->adc_tm_hc) {
- sensor_mask = 1 << chip->sensor[dt_index].sensor_num;
- rc = qpnp_adc_tm_reg_update(chip, QPNP_ADC_TM_LOW_THR_INT_EN,
- sensor_mask, false);
- if (rc < 0) {
- pr_err("high threshold int enable failed\n");
- goto fail;
- }
- rc = qpnp_adc_tm_reg_update(chip, QPNP_ADC_TM_MULTI_MEAS_EN,
- sensor_mask, false);
- if (rc < 0) {
- pr_err("multi measurement en failed\n");
- goto fail;
- }
- } else {
- rc = qpnp_adc_tm_reg_update(chip, QPNP_BTM_Mn_EN(btm_chan_num),
- QPNP_BTM_Mn_HIGH_THR_INT_EN, false);
- if (rc < 0) {
- pr_err("high thr disable err:%d\n", btm_chan_num);
- goto fail;
- }
- rc = qpnp_adc_tm_reg_update(chip, QPNP_BTM_Mn_EN(btm_chan_num),
- QPNP_BTM_Mn_LOW_THR_INT_EN, false);
- if (rc < 0) {
- pr_err("low thr disable err:%d\n", btm_chan_num);
- goto fail;
- }
- rc = qpnp_adc_tm_reg_update(chip, QPNP_BTM_Mn_EN(btm_chan_num),
- QPNP_BTM_Mn_MEAS_EN, false);
- if (rc < 0) {
- pr_err("multi measurement disable failed\n");
- goto fail;
- }
- }
- rc = qpnp_adc_tm_enable_if_channel_meas(chip);
- if (rc < 0)
- pr_err("re-enabling measurement failed\n");
- fail:
- mutex_unlock(&chip->adc->adc_lock);
- return rc;
- }
- EXPORT_SYMBOL(qpnp_adc_tm_disable_chan_meas);
- struct qpnp_adc_tm_chip *qpnp_get_adc_tm(struct device *dev, const char *name)
- {
- struct qpnp_adc_tm_chip *chip;
- struct device_node *node = NULL;
- char prop_name[QPNP_MAX_PROP_NAME_LEN];
- snprintf(prop_name, QPNP_MAX_PROP_NAME_LEN, "qcom,%s-adc_tm", name);
- node = of_parse_phandle(dev->of_node, prop_name, 0);
- if (node == NULL)
- return ERR_PTR(-ENODEV);
- list_for_each_entry(chip, &qpnp_adc_tm_device_list, list)
- if (chip->adc->pdev->dev.of_node == node)
- return chip;
- return ERR_PTR(-EPROBE_DEFER);
- }
- EXPORT_SYMBOL(qpnp_get_adc_tm);
- static int qpnp_adc_tm_initial_setup(struct qpnp_adc_tm_chip *chip)
- {
- u8 thr_init = 0;
- int rc = 0;
- rc = qpnp_adc_tm_write_reg(chip, QPNP_ADC_TM_HIGH_THR_INT_EN,
- thr_init, 1);
- if (rc < 0) {
- pr_err("high thr init failed\n");
- return rc;
- }
- rc = qpnp_adc_tm_write_reg(chip, QPNP_ADC_TM_LOW_THR_INT_EN,
- thr_init, 1);
- if (rc < 0) {
- pr_err("low thr init failed\n");
- return rc;
- }
- rc = qpnp_adc_tm_write_reg(chip, QPNP_ADC_TM_MULTI_MEAS_EN,
- thr_init, 1);
- if (rc < 0) {
- pr_err("multi meas en failed\n");
- return rc;
- }
- return rc;
- }
- static const struct of_device_id qpnp_adc_tm_match_table[] = {
- { .compatible = "qcom,qpnp-adc-tm" },
- { .compatible = "qcom,qpnp-adc-tm-hc" },
- { .compatible = "qcom,qpnp-adc-tm-hc-pm5" },
- {}
- };
- static int qpnp_adc_tm_probe(struct platform_device *pdev)
- {
- struct device_node *node = pdev->dev.of_node, *child;
- struct qpnp_adc_tm_chip *chip;
- struct qpnp_adc_drv *adc_qpnp;
- int32_t count_adc_channel_list = 0, rc, sen_idx = 0, i = 0;
- bool thermal_node = false;
- const struct of_device_id *id;
- for_each_child_of_node(node, child)
- count_adc_channel_list++;
- if (!count_adc_channel_list) {
- pr_err("No channel listing\n");
- return -EINVAL;
- }
- id = of_match_node(qpnp_adc_tm_match_table, node);
- if (id == NULL) {
- pr_err("qpnp_adc_tm_match of_node prop not present\n");
- return -ENODEV;
- }
- chip = devm_kzalloc(&pdev->dev, sizeof(struct qpnp_adc_tm_chip) +
- (count_adc_channel_list *
- sizeof(struct qpnp_adc_tm_sensor)),
- GFP_KERNEL);
- if (!chip)
- return -ENOMEM;
- list_add(&chip->list, &qpnp_adc_tm_device_list);
- chip->max_channels_available = count_adc_channel_list;
- adc_qpnp = devm_kzalloc(&pdev->dev, sizeof(struct qpnp_adc_drv),
- GFP_KERNEL);
- if (!adc_qpnp) {
- rc = -ENOMEM;
- goto fail;
- }
- chip->dev = &(pdev->dev);
- chip->adc = adc_qpnp;
- chip->adc->regmap = dev_get_regmap(pdev->dev.parent, NULL);
- if (!chip->adc->regmap) {
- dev_err(&pdev->dev, "Couldn't get parent's regmap\n");
- rc = -EINVAL;
- goto fail;
- }
- if (of_device_is_compatible(node, "qcom,qpnp-adc-tm-hc")) {
- chip->adc_tm_hc = true;
- chip->adc->adc_hc = true;
- }
- rc = qpnp_adc_get_devicetree_data(pdev, chip->adc);
- if (rc) {
- dev_err(&pdev->dev, "failed to read device tree\n");
- goto fail;
- }
- mutex_init(&chip->adc->adc_lock);
- /* Register the ADC peripheral interrupt */
- if (!chip->adc_tm_hc) {
- chip->adc->adc_high_thr_irq = platform_get_irq_byname(pdev,
- "high-thr-en-set");
- if (chip->adc->adc_high_thr_irq < 0) {
- pr_err("Invalid irq\n");
- rc = -ENXIO;
- goto fail;
- }
- chip->adc->adc_low_thr_irq = platform_get_irq_byname(pdev,
- "low-thr-en-set");
- if (chip->adc->adc_low_thr_irq < 0) {
- pr_err("Invalid irq\n");
- rc = -ENXIO;
- goto fail;
- }
- }
- chip->vadc_dev = qpnp_get_vadc(&pdev->dev, "adc_tm");
- if (IS_ERR(chip->vadc_dev)) {
- rc = PTR_ERR(chip->vadc_dev);
- if (rc != -EPROBE_DEFER)
- pr_err("vadc property missing, rc=%d\n", rc);
- goto fail;
- }
- chip->adc_tm_recalib_check = of_property_read_bool(node,
- "qcom,adc-tm-recalib-check");
- for_each_child_of_node(node, child) {
- char name[25];
- int btm_channel_num, timer_select = 0;
- rc = of_property_read_u32(child,
- "qcom,btm-channel-number", &btm_channel_num);
- if (rc) {
- pr_err("Invalid btm channel number\n");
- goto fail;
- }
- rc = of_property_read_u32(child,
- "qcom,meas-interval-timer-idx", &timer_select);
- if (rc) {
- pr_debug("Default to timer2 with interval of 1 sec\n");
- chip->sensor[sen_idx].timer_select =
- ADC_MEAS_TIMER_SELECT2;
- chip->sensor[sen_idx].meas_interval =
- ADC_MEAS2_INTERVAL_1S;
- } else {
- if (timer_select >= ADC_MEAS_TIMER_NUM) {
- pr_err("Invalid timer selection number\n");
- goto fail;
- }
- chip->sensor[sen_idx].timer_select = timer_select;
- if (timer_select == ADC_MEAS_TIMER_SELECT1)
- chip->sensor[sen_idx].meas_interval =
- ADC_MEAS1_INTERVAL_3P9MS;
- else if (timer_select == ADC_MEAS_TIMER_SELECT3)
- chip->sensor[sen_idx].meas_interval =
- ADC_MEAS3_INTERVAL_4S;
- else if (timer_select == ADC_MEAS_TIMER_SELECT2)
- chip->sensor[sen_idx].meas_interval =
- ADC_MEAS2_INTERVAL_1S;
- }
- chip->sensor[sen_idx].btm_channel_num = btm_channel_num;
- chip->sensor[sen_idx].vadc_channel_num =
- chip->adc->adc_channels[sen_idx].channel_num;
- chip->sensor[sen_idx].sensor_num = sen_idx;
- chip->sensor[sen_idx].chip = chip;
- pr_debug("btm_chan:%x, vadc_chan:%x\n", btm_channel_num,
- chip->adc->adc_channels[sen_idx].channel_num);
- thermal_node = of_property_read_bool(child,
- "qcom,thermal-node");
- if (thermal_node) {
- /* Register with the thermal zone */
- pr_debug("thermal node%x\n", btm_channel_num);
- chip->sensor[sen_idx].mode = THERMAL_DEVICE_DISABLED;
- chip->sensor[sen_idx].thermal_node = true;
- snprintf(name, sizeof(name), "%s",
- chip->adc->adc_channels[sen_idx].name);
- chip->sensor[sen_idx].low_thr =
- QPNP_ADC_TM_M0_LOW_THR;
- chip->sensor[sen_idx].high_thr =
- QPNP_ADC_TM_M0_HIGH_THR;
- chip->sensor[sen_idx].emul_temperature = 0;
- chip->sensor[sen_idx].tz_dev =
- devm_thermal_zone_of_sensor_register(
- chip->dev,
- chip->sensor[sen_idx].vadc_channel_num,
- &chip->sensor[sen_idx],
- &qpnp_adc_tm_thermal_ops);
- if (IS_ERR(chip->sensor[sen_idx].tz_dev))
- pr_err("thermal device register failed.\n");
- }
- chip->sensor[sen_idx].req_wq = alloc_workqueue(
- "qpnp_adc_notify_wq", WQ_HIGHPRI, 0);
- if (!chip->sensor[sen_idx].req_wq) {
- pr_err("Requesting priority wq failed\n");
- goto fail;
- }
- INIT_WORK(&chip->sensor[sen_idx].work, notify_adc_tm_fn);
- INIT_LIST_HEAD(&chip->sensor[sen_idx].thr_list);
- sen_idx++;
- }
- chip->high_thr_wq = alloc_workqueue("qpnp_adc_tm_high_thr_wq",
- WQ_HIGHPRI, 0);
- if (!chip->high_thr_wq) {
- pr_err("Requesting high thr priority wq failed\n");
- goto fail;
- }
- chip->low_thr_wq = alloc_workqueue("qpnp_adc_tm_low_thr_wq",
- WQ_HIGHPRI, 0);
- if (!chip->low_thr_wq) {
- pr_err("Requesting low thr priority wq failed\n");
- goto fail;
- }
- chip->thr_wq = alloc_workqueue("qpnp_adc_tm_thr_wq",
- WQ_HIGHPRI, 0);
- if (!chip->thr_wq) {
- pr_err("Requesting thr priority wq failed\n");
- goto fail;
- }
- INIT_WORK(&chip->trigger_high_thr_work, qpnp_adc_tm_high_thr_work);
- INIT_WORK(&chip->trigger_low_thr_work, qpnp_adc_tm_low_thr_work);
- if (!chip->adc_tm_hc) {
- rc = qpnp_adc_tm_initial_setup(chip);
- if (rc)
- goto fail;
- rc = devm_request_irq(&pdev->dev, chip->adc->adc_high_thr_irq,
- qpnp_adc_tm_high_thr_isr,
- IRQF_TRIGGER_RISING, "qpnp_adc_tm_high_interrupt", chip);
- if (rc) {
- dev_err(&pdev->dev, "failed to request adc irq\n");
- goto fail;
- } else {
- enable_irq_wake(chip->adc->adc_high_thr_irq);
- }
- rc = devm_request_irq(&pdev->dev, chip->adc->adc_low_thr_irq,
- qpnp_adc_tm_low_thr_isr,
- IRQF_TRIGGER_RISING,
- "qpnp_adc_tm_low_interrupt", chip);
- if (rc) {
- dev_err(&pdev->dev, "failed to request adc irq\n");
- goto fail;
- } else {
- enable_irq_wake(chip->adc->adc_low_thr_irq);
- }
- } else {
- rc = devm_request_irq(&pdev->dev, chip->adc->adc_irq_eoc,
- qpnp_adc_tm_rc_thr_isr,
- IRQF_TRIGGER_HIGH, "qpnp_adc_tm_interrupt", chip);
- if (rc)
- dev_err(&pdev->dev, "failed to request adc irq\n");
- else
- enable_irq_wake(chip->adc->adc_irq_eoc);
- }
- chip->adc_vote_enable = false;
- dev_set_drvdata(&pdev->dev, chip);
- spin_lock_init(&chip->th_info.adc_tm_low_lock);
- spin_lock_init(&chip->th_info.adc_tm_high_lock);
- pr_debug("OK\n");
- return 0;
- fail:
- for_each_child_of_node(node, child) {
- thermal_node = of_property_read_bool(child,
- "qcom,thermal-node");
- if (thermal_node) {
- thermal_zone_device_unregister(chip->sensor[i].tz_dev);
- if (chip->sensor[i].req_wq)
- destroy_workqueue(chip->sensor[sen_idx].req_wq);
- }
- }
- if (chip->high_thr_wq)
- destroy_workqueue(chip->high_thr_wq);
- if (chip->low_thr_wq)
- destroy_workqueue(chip->low_thr_wq);
- list_del(&chip->list);
- dev_set_drvdata(&pdev->dev, NULL);
- return rc;
- }
- static int qpnp_adc_tm_remove(struct platform_device *pdev)
- {
- struct qpnp_adc_tm_chip *chip = dev_get_drvdata(&pdev->dev);
- struct device_node *node = pdev->dev.of_node, *child;
- int i = 0;
- for_each_child_of_node(node, child) {
- if (chip->sensor[i].req_wq)
- destroy_workqueue(chip->sensor[i].req_wq);
- i++;
- }
- if (chip->high_thr_wq)
- destroy_workqueue(chip->high_thr_wq);
- if (chip->low_thr_wq)
- destroy_workqueue(chip->low_thr_wq);
- if (chip->adc->hkadc_ldo && chip->adc->hkadc_ldo_ok)
- qpnp_adc_free_voltage_resource(chip->adc);
- dev_set_drvdata(&pdev->dev, NULL);
- return 0;
- }
- static void qpnp_adc_tm_shutdown(struct platform_device *pdev)
- {
- struct qpnp_adc_tm_chip *chip = dev_get_drvdata(&pdev->dev);
- int rc = 0, i = 0;
- /* Disable bank */
- rc = qpnp_adc_tm_disable(chip);
- if (rc < 0)
- pr_err("adc-tm disable failed\n");
- for (i = 0; i < QPNP_BTM_CHANNELS; i++) {
- rc = qpnp_adc_tm_reg_update(chip,
- QPNP_BTM_Mn_EN(i),
- QPNP_BTM_Mn_MEAS_EN, false);
- if (rc < 0)
- pr_err("multi measurement disable failed\n");
- }
- }
- static int qpnp_adc_tm_suspend_noirq(struct device *dev)
- {
- struct qpnp_adc_tm_chip *chip = dev_get_drvdata(dev);
- struct device_node *node = dev->of_node, *child;
- int i = 0;
- flush_workqueue(chip->high_thr_wq);
- flush_workqueue(chip->low_thr_wq);
- for_each_child_of_node(node, child) {
- if (chip->sensor[i].req_wq) {
- pr_debug("flushing queue for sensor %d\n", i);
- flush_workqueue(chip->sensor[i].req_wq);
- }
- i++;
- }
- return 0;
- }
- static const struct dev_pm_ops qpnp_adc_tm_pm_ops = {
- .suspend_noirq = qpnp_adc_tm_suspend_noirq,
- };
- static struct platform_driver qpnp_adc_tm_driver = {
- .driver = {
- .name = "qcom,qpnp-adc-tm",
- .of_match_table = qpnp_adc_tm_match_table,
- .pm = &qpnp_adc_tm_pm_ops,
- },
- .probe = qpnp_adc_tm_probe,
- .remove = qpnp_adc_tm_remove,
- .shutdown = qpnp_adc_tm_shutdown,
- };
- static int __init qpnp_adc_tm_init(void)
- {
- return platform_driver_register(&qpnp_adc_tm_driver);
- }
- module_init(qpnp_adc_tm_init);
- static void __exit qpnp_adc_tm_exit(void)
- {
- platform_driver_unregister(&qpnp_adc_tm_driver);
- }
- module_exit(qpnp_adc_tm_exit);
- MODULE_DESCRIPTION("QPNP PMIC ADC Threshold Monitoring driver");
- MODULE_LICENSE("GPL v2");
|