cpr3-regulator.c 198 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682
  1. /*
  2. * Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #define pr_fmt(fmt) "%s: " fmt, __func__
  14. #include <linux/bitops.h>
  15. #include <linux/debugfs.h>
  16. #include <linux/delay.h>
  17. #include <linux/err.h>
  18. #include <linux/init.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <linux/kernel.h>
  22. #include <linux/ktime.h>
  23. #include <linux/list.h>
  24. #include <linux/module.h>
  25. #include <linux/of.h>
  26. #include <linux/of_device.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/pm_opp.h>
  29. #include <linux/slab.h>
  30. #include <linux/sort.h>
  31. #include <linux/string.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/regulator/driver.h>
  34. #include <linux/regulator/machine.h>
  35. #include <linux/regulator/of_regulator.h>
  36. #include <linux/regulator/msm-ldo-regulator.h>
  37. #include <soc/qcom/spm.h>
  38. #include "cpr3-regulator.h"
  39. #define CPR3_REGULATOR_CORNER_INVALID (-1)
  40. #define CPR3_RO_MASK GENMASK(CPR3_RO_COUNT - 1, 0)
  41. /* CPR3 registers */
  42. #define CPR3_REG_CPR_VERSION 0x0
  43. #define CPRH_CPR_VERSION_4P5 0x40050000
  44. #define CPR3_REG_CPR_CTL 0x4
  45. #define CPR3_CPR_CTL_LOOP_EN_MASK BIT(0)
  46. #define CPR3_CPR_CTL_LOOP_ENABLE BIT(0)
  47. #define CPR3_CPR_CTL_LOOP_DISABLE 0
  48. #define CPR3_CPR_CTL_IDLE_CLOCKS_MASK GENMASK(5, 1)
  49. #define CPR3_CPR_CTL_IDLE_CLOCKS_SHIFT 1
  50. #define CPR3_CPR_CTL_COUNT_MODE_MASK GENMASK(7, 6)
  51. #define CPR3_CPR_CTL_COUNT_MODE_SHIFT 6
  52. #define CPR3_CPR_CTL_COUNT_MODE_ALL_AT_ONCE_MIN 0
  53. #define CPR3_CPR_CTL_COUNT_MODE_ALL_AT_ONCE_MAX 1
  54. #define CPR3_CPR_CTL_COUNT_MODE_STAGGERED 2
  55. #define CPR3_CPR_CTL_COUNT_MODE_ALL_AT_ONCE_AGE 3
  56. #define CPR3_CPR_CTL_COUNT_REPEAT_MASK GENMASK(31, 9)
  57. #define CPR3_CPR_CTL_COUNT_REPEAT_SHIFT 9
  58. #define CPR3_REG_CPR_STATUS 0x8
  59. #define CPR3_CPR_STATUS_BUSY_MASK BIT(0)
  60. #define CPR3_CPR_STATUS_AGING_MEASUREMENT_MASK BIT(1)
  61. /*
  62. * This register is not present on controllers that support HW closed-loop
  63. * except CPR4 APSS controller.
  64. */
  65. #define CPR3_REG_CPR_TIMER_AUTO_CONT 0xC
  66. #define CPR3_REG_CPR_STEP_QUOT 0x14
  67. #define CPR3_CPR_STEP_QUOT_MIN_MASK GENMASK(5, 0)
  68. #define CPR3_CPR_STEP_QUOT_MIN_SHIFT 0
  69. #define CPR3_CPR_STEP_QUOT_MAX_MASK GENMASK(11, 6)
  70. #define CPR3_CPR_STEP_QUOT_MAX_SHIFT 6
  71. #define CPR3_REG_GCNT(ro) (0xA0 + 0x4 * (ro))
  72. #define CPR3_REG_SENSOR_BYPASS_WRITE(sensor) (0xE0 + 0x4 * ((sensor) / 32))
  73. #define CPR3_REG_SENSOR_BYPASS_WRITE_BANK(bank) (0xE0 + 0x4 * (bank))
  74. #define CPR3_REG_SENSOR_MASK_WRITE(sensor) (0x120 + 0x4 * ((sensor) / 32))
  75. #define CPR3_REG_SENSOR_MASK_WRITE_BANK(bank) (0x120 + 0x4 * (bank))
  76. #define CPR3_REG_SENSOR_MASK_READ(sensor) (0x140 + 0x4 * ((sensor) / 32))
  77. #define CPR3_REG_SENSOR_OWNER(sensor) (0x200 + 0x4 * (sensor))
  78. #define CPR3_REG_CONT_CMD 0x800
  79. #define CPR3_CONT_CMD_ACK 0x1
  80. #define CPR3_CONT_CMD_NACK 0x0
  81. #define CPR3_REG_THRESH(thread) (0x808 + 0x440 * (thread))
  82. #define CPR3_THRESH_CONS_DOWN_MASK GENMASK(3, 0)
  83. #define CPR3_THRESH_CONS_DOWN_SHIFT 0
  84. #define CPR3_THRESH_CONS_UP_MASK GENMASK(7, 4)
  85. #define CPR3_THRESH_CONS_UP_SHIFT 4
  86. #define CPR3_THRESH_DOWN_THRESH_MASK GENMASK(12, 8)
  87. #define CPR3_THRESH_DOWN_THRESH_SHIFT 8
  88. #define CPR3_THRESH_UP_THRESH_MASK GENMASK(17, 13)
  89. #define CPR3_THRESH_UP_THRESH_SHIFT 13
  90. #define CPR3_REG_RO_MASK(thread) (0x80C + 0x440 * (thread))
  91. #define CPR3_REG_RESULT0(thread) (0x810 + 0x440 * (thread))
  92. #define CPR3_RESULT0_BUSY_MASK BIT(0)
  93. #define CPR3_RESULT0_STEP_DN_MASK BIT(1)
  94. #define CPR3_RESULT0_STEP_UP_MASK BIT(2)
  95. #define CPR3_RESULT0_ERROR_STEPS_MASK GENMASK(7, 3)
  96. #define CPR3_RESULT0_ERROR_STEPS_SHIFT 3
  97. #define CPR3_RESULT0_ERROR_MASK GENMASK(19, 8)
  98. #define CPR3_RESULT0_ERROR_SHIFT 8
  99. #define CPR3_RESULT0_NEGATIVE_MASK BIT(20)
  100. #define CPR3_REG_RESULT1(thread) (0x814 + 0x440 * (thread))
  101. #define CPR3_RESULT1_QUOT_MIN_MASK GENMASK(11, 0)
  102. #define CPR3_RESULT1_QUOT_MIN_SHIFT 0
  103. #define CPR3_RESULT1_QUOT_MAX_MASK GENMASK(23, 12)
  104. #define CPR3_RESULT1_QUOT_MAX_SHIFT 12
  105. #define CPR3_RESULT1_RO_MIN_MASK GENMASK(27, 24)
  106. #define CPR3_RESULT1_RO_MIN_SHIFT 24
  107. #define CPR3_RESULT1_RO_MAX_MASK GENMASK(31, 28)
  108. #define CPR3_RESULT1_RO_MAX_SHIFT 28
  109. #define CPR3_REG_RESULT2(thread) (0x818 + 0x440 * (thread))
  110. #define CPR3_RESULT2_STEP_QUOT_MIN_MASK GENMASK(5, 0)
  111. #define CPR3_RESULT2_STEP_QUOT_MIN_SHIFT 0
  112. #define CPR3_RESULT2_STEP_QUOT_MAX_MASK GENMASK(11, 6)
  113. #define CPR3_RESULT2_STEP_QUOT_MAX_SHIFT 6
  114. #define CPR3_RESULT2_SENSOR_MIN_MASK GENMASK(23, 16)
  115. #define CPR3_RESULT2_SENSOR_MIN_SHIFT 16
  116. #define CPR3_RESULT2_SENSOR_MAX_MASK GENMASK(31, 24)
  117. #define CPR3_RESULT2_SENSOR_MAX_SHIFT 24
  118. #define CPR3_REG_IRQ_EN 0x81C
  119. #define CPR3_REG_IRQ_CLEAR 0x820
  120. #define CPR3_REG_IRQ_STATUS 0x824
  121. #define CPR3_IRQ_UP BIT(3)
  122. #define CPR3_IRQ_MID BIT(2)
  123. #define CPR3_IRQ_DOWN BIT(1)
  124. #define CPR3_REG_TARGET_QUOT(thread, ro) \
  125. (0x840 + 0x440 * (thread) + 0x4 * (ro))
  126. /* Registers found only on controllers that support HW closed-loop. */
  127. #define CPR3_REG_PD_THROTTLE 0xE8
  128. #define CPR3_PD_THROTTLE_DISABLE 0x0
  129. #define CPR3_REG_HW_CLOSED_LOOP 0x3000
  130. #define CPR3_HW_CLOSED_LOOP_ENABLE 0x0
  131. #define CPR3_HW_CLOSED_LOOP_DISABLE 0x1
  132. #define CPR3_REG_CPR_TIMER_MID_CONT 0x3004
  133. #define CPR3_REG_CPR_TIMER_UP_DN_CONT 0x3008
  134. #define CPR3_REG_LAST_MEASUREMENT 0x7F8
  135. #define CPR3_LAST_MEASUREMENT_THREAD_DN_SHIFT 0
  136. #define CPR3_LAST_MEASUREMENT_THREAD_UP_SHIFT 4
  137. #define CPR3_LAST_MEASUREMENT_THREAD_DN(thread) \
  138. (BIT(thread) << CPR3_LAST_MEASUREMENT_THREAD_DN_SHIFT)
  139. #define CPR3_LAST_MEASUREMENT_THREAD_UP(thread) \
  140. (BIT(thread) << CPR3_LAST_MEASUREMENT_THREAD_UP_SHIFT)
  141. #define CPR3_LAST_MEASUREMENT_AGGR_DN BIT(8)
  142. #define CPR3_LAST_MEASUREMENT_AGGR_MID BIT(9)
  143. #define CPR3_LAST_MEASUREMENT_AGGR_UP BIT(10)
  144. #define CPR3_LAST_MEASUREMENT_VALID BIT(11)
  145. #define CPR3_LAST_MEASUREMENT_SAW_ERROR BIT(12)
  146. #define CPR3_LAST_MEASUREMENT_PD_BYPASS_MASK GENMASK(23, 16)
  147. #define CPR3_LAST_MEASUREMENT_PD_BYPASS_SHIFT 16
  148. /* CPR4 controller specific registers and bit definitions */
  149. #define CPR4_REG_CPR_TIMER_CLAMP 0x10
  150. #define CPR4_CPR_TIMER_CLAMP_THREAD_AGGREGATION_EN BIT(27)
  151. #define CPR4_REG_MISC 0x700
  152. #define CPR4_MISC_RESET_STEP_QUOT_LOOP_EN BIT(2)
  153. #define CPR4_MISC_THREAD_HAS_ALWAYS_VOTE_EN BIT(3)
  154. #define CPR4_MISC_MARGIN_TABLE_ROW_SELECT_MASK GENMASK(23, 20)
  155. #define CPR4_MISC_MARGIN_TABLE_ROW_SELECT_SHIFT 20
  156. #define CPR4_MISC_TEMP_SENSOR_ID_START_MASK GENMASK(27, 24)
  157. #define CPR4_MISC_TEMP_SENSOR_ID_START_SHIFT 24
  158. #define CPR4_MISC_TEMP_SENSOR_ID_END_MASK GENMASK(31, 28)
  159. #define CPR4_MISC_TEMP_SENSOR_ID_END_SHIFT 28
  160. #define CPR4_REG_SAW_ERROR_STEP_LIMIT 0x7A4
  161. #define CPR4_SAW_ERROR_STEP_LIMIT_UP_MASK GENMASK(4, 0)
  162. #define CPR4_SAW_ERROR_STEP_LIMIT_UP_SHIFT 0
  163. #define CPR4_SAW_ERROR_STEP_LIMIT_DN_MASK GENMASK(9, 5)
  164. #define CPR4_SAW_ERROR_STEP_LIMIT_DN_SHIFT 5
  165. #define CPR4_REG_MARGIN_TEMP_CORE_TIMERS 0x7A8
  166. #define CPR4_MARGIN_TEMP_CORE_TIMERS_SETTLE_VOLTAGE_COUNT_MASK GENMASK(28, 18)
  167. #define CPR4_MARGIN_TEMP_CORE_TIMERS_SETTLE_VOLTAGE_COUNT_SHIFT 18
  168. #define CPR4_REG_MARGIN_TEMP_CORE(core) (0x7AC + 0x4 * (core))
  169. #define CPR4_MARGIN_TEMP_CORE_ADJ_MASK GENMASK(7, 0)
  170. #define CPR4_MARGIN_TEMP_CORE_ADJ_SHIFT 8
  171. #define CPR4_REG_MARGIN_TEMP_POINT0N1 0x7F0
  172. #define CPR4_MARGIN_TEMP_POINT0_MASK GENMASK(11, 0)
  173. #define CPR4_MARGIN_TEMP_POINT0_SHIFT 0
  174. #define CPR4_MARGIN_TEMP_POINT1_MASK GENMASK(23, 12)
  175. #define CPR4_MARGIN_TEMP_POINT1_SHIFT 12
  176. #define CPR4_REG_MARGIN_TEMP_POINT2 0x7F4
  177. #define CPR4_MARGIN_TEMP_POINT2_MASK GENMASK(11, 0)
  178. #define CPR4_MARGIN_TEMP_POINT2_SHIFT 0
  179. #define CPR4_REG_MARGIN_ADJ_CTL 0x7F8
  180. #define CPR4_MARGIN_ADJ_CTL_BOOST_EN BIT(0)
  181. #define CPR4_MARGIN_ADJ_CTL_CORE_ADJ_EN BIT(1)
  182. #define CPR4_MARGIN_ADJ_CTL_TEMP_ADJ_EN BIT(2)
  183. #define CPR4_MARGIN_ADJ_CTL_TIMER_SETTLE_VOLTAGE_EN BIT(3)
  184. #define CPR4_MARGIN_ADJ_CTL_HW_CLOSED_LOOP_EN_MASK BIT(4)
  185. #define CPR4_MARGIN_ADJ_CTL_HW_CLOSED_LOOP_ENABLE BIT(4)
  186. #define CPR4_MARGIN_ADJ_CTL_HW_CLOSED_LOOP_DISABLE 0
  187. #define CPR4_MARGIN_ADJ_CTL_PER_RO_KV_MARGIN_EN BIT(7)
  188. #define CPR4_MARGIN_ADJ_CTL_KV_MARGIN_ADJ_EN BIT(8)
  189. #define CPR4_MARGIN_ADJ_CTL_PMIC_STEP_SIZE_MASK GENMASK(16, 12)
  190. #define CPR4_MARGIN_ADJ_CTL_PMIC_STEP_SIZE_SHIFT 12
  191. #define CPR4_MARGIN_ADJ_CTL_INITIAL_TEMP_BAND_MASK GENMASK(21, 19)
  192. #define CPR4_MARGIN_ADJ_CTL_INITIAL_TEMP_BAND_SHIFT 19
  193. #define CPR4_MARGIN_ADJ_CTL_MAX_NUM_CORES_MASK GENMASK(25, 22)
  194. #define CPR4_MARGIN_ADJ_CTL_MAX_NUM_CORES_SHIFT 22
  195. #define CPR4_MARGIN_ADJ_CTL_KV_MARGIN_ADJ_STEP_QUOT_MASK GENMASK(31, 26)
  196. #define CPR4_MARGIN_ADJ_CTL_KV_MARGIN_ADJ_STEP_QUOT_SHIFT 26
  197. #define CPR4_REG_CPR_MASK_THREAD(thread) (0x80C + 0x440 * (thread))
  198. #define CPR4_CPR_MASK_THREAD_DISABLE_THREAD BIT(31)
  199. #define CPR4_CPR_MASK_THREAD_RO_MASK4THREAD_MASK GENMASK(15, 0)
  200. /* CPRh controller specific registers and bit definitions */
  201. #define CPRH_REG_CORNER(thread, corner) \
  202. ((thread)->ctrl->cpr_hw_version >= CPRH_CPR_VERSION_4P5 \
  203. ? 0x3500 + 0xA0 * (thread)->thread_id + 0x4 * (corner) \
  204. : 0x3A00 + 0x4 * (corner))
  205. #define CPRH_CORNER_INIT_VOLTAGE_MASK GENMASK(7, 0)
  206. #define CPRH_CORNER_INIT_VOLTAGE_SHIFT 0
  207. #define CPRH_CORNER_FLOOR_VOLTAGE_MASK GENMASK(15, 8)
  208. #define CPRH_CORNER_FLOOR_VOLTAGE_SHIFT 8
  209. #define CPRH_CORNER_QUOT_DELTA_MASK GENMASK(24, 16)
  210. #define CPRH_CORNER_QUOT_DELTA_SHIFT 16
  211. #define CPRH_CORNER_RO_SEL_MASK GENMASK(28, 25)
  212. #define CPRH_CORNER_RO_SEL_SHIFT 25
  213. #define CPRH_CORNER_CPR_CL_DISABLE BIT(29)
  214. #define CPRH_CORNER_CORE_TEMP_MARGIN_DISABLE BIT(30)
  215. #define CPRH_CORNER_LAST_KNOWN_VOLTAGE_ENABLE BIT(31)
  216. #define CPRH_CORNER_INIT_VOLTAGE_MAX_VALUE 255
  217. #define CPRH_CORNER_FLOOR_VOLTAGE_MAX_VALUE 255
  218. #define CPRH_CORNER_QUOT_DELTA_MAX_VALUE 511
  219. #define CPRH_REG_CTL(ctrl) \
  220. ((ctrl)->cpr_hw_version >= CPRH_CPR_VERSION_4P5 ? 0x3A80 : 0x3AA0)
  221. #define CPRH_CTL_OSM_ENABLED BIT(0)
  222. #define CPRH_CTL_BASE_VOLTAGE_MASK GENMASK(10, 1)
  223. #define CPRH_CTL_BASE_VOLTAGE_SHIFT 1
  224. #define CPRH_CTL_INIT_MODE_MASK GENMASK(16, 11)
  225. #define CPRH_CTL_INIT_MODE_SHIFT 11
  226. #define CPRH_CTL_MODE_SWITCH_DELAY_MASK GENMASK(24, 17)
  227. #define CPRH_CTL_MODE_SWITCH_DELAY_SHIFT 17
  228. #define CPRH_CTL_VOLTAGE_MULTIPLIER_MASK GENMASK(28, 25)
  229. #define CPRH_CTL_VOLTAGE_MULTIPLIER_SHIFT 25
  230. #define CPRH_CTL_LAST_KNOWN_VOLTAGE_MARGIN_MASK GENMASK(31, 29)
  231. #define CPRH_CTL_LAST_KNOWN_VOLTAGE_MARGIN_SHIFT 29
  232. #define CPRH_REG_STATUS(thread) \
  233. ((thread)->ctrl->cpr_hw_version >= CPRH_CPR_VERSION_4P5 \
  234. ? 0x3A84 + 0x4 * (thread)->thread_id : 0x3AA4)
  235. #define CPRH_STATUS_CORNER GENMASK(5, 0)
  236. #define CPRH_STATUS_CORNER_LAST_VOLT_MASK GENMASK(17, 6)
  237. #define CPRH_STATUS_CORNER_LAST_VOLT_SHIFT 6
  238. #define CPRH_REG_CORNER_BAND 0x3AA8
  239. #define CPRH_CORNER_BAND_MASK GENMASK(5, 0)
  240. #define CPRH_CORNER_BAND_SHIFT 6
  241. #define CPRH_CORNER_BAND_MAX_COUNT 4
  242. #define CPRH_MARGIN_TEMP_CORE_VBAND(core, vband) \
  243. ((vband) == 0 ? CPR4_REG_MARGIN_TEMP_CORE(core) \
  244. : 0x3AB0 + 0x40 * ((vband) - 1) + 0x4 * (core))
  245. #define CPRH_REG_MISC_REG2 0x3AAC
  246. #define CPRH_MISC_REG2_ACD_ADJ_STEP_UP_LIMIT_MASK GENMASK(31, 29)
  247. #define CPRH_MISC_REG2_ACD_ADJ_STEP_UP_LIMIT_SHIFT 29
  248. #define CPRH_MISC_REG2_ACD_ADJ_STEP_DOWN_LIMIT_MASK GENMASK(28, 24)
  249. #define CPRH_MISC_REG2_ACD_ADJ_STEP_DOWN_LIMIT_SHIFT 24
  250. #define CPRH_MISC_REG2_ACD_ADJ_STEP_SIZE_UP_MASK GENMASK(23, 22)
  251. #define CPRH_MISC_REG2_ACD_ADJ_STEP_SIZE_UP_SHIFT 22
  252. #define CPRH_MISC_REG2_ACD_ADJ_STEP_SIZE_DOWN_MASK GENMASK(21, 20)
  253. #define CPRH_MISC_REG2_ACD_ADJ_STEP_SIZE_DOWN_SHIFT 20
  254. #define CPRH_MISC_REG2_ACD_NOTWAIT_4_CL_SETTLE_MASK BIT(16)
  255. #define CPRH_MISC_REG2_ACD_NOTWAIT_4_CL_SETTLE_EN BIT(16)
  256. #define CPRH_MISC_REG2_ACD_AVG_FAST_UPDATE_EN_MASK BIT(13)
  257. #define CPRH_MISC_REG2_ACD_AVG_FAST_UPDATE_EN BIT(13)
  258. #define CPRH_MISC_REG2_ACD_AVG_EN_MASK BIT(12)
  259. #define CPRH_MISC_REG2_ACD_AVG_ENABLE BIT(12)
  260. /* SAW module registers */
  261. #define SAW_REG_AVS_CTL 0x904
  262. #define SAW_REG_AVS_LIMIT 0x908
  263. /*
  264. * The amount of time to wait for the CPR controller to become idle when
  265. * performing an aging measurement.
  266. */
  267. #define CPR3_AGING_MEASUREMENT_TIMEOUT_NS 5000000
  268. /*
  269. * The number of individual aging measurements to perform which are then
  270. * averaged together in order to determine the final aging adjustment value.
  271. */
  272. #define CPR3_AGING_MEASUREMENT_ITERATIONS 16
  273. /*
  274. * Aging measurements for the aged and unaged ring oscillators take place a few
  275. * microseconds apart. If the vdd-supply voltage fluctuates between the two
  276. * measurements, then the difference between them will be incorrect. The
  277. * difference could end up too high or too low. This constant defines the
  278. * number of lowest and highest measurements to ignore when averaging.
  279. */
  280. #define CPR3_AGING_MEASUREMENT_FILTER 3
  281. /*
  282. * The number of times to attempt the full aging measurement sequence before
  283. * declaring a measurement failure.
  284. */
  285. #define CPR3_AGING_RETRY_COUNT 5
  286. /*
  287. * The maximum time to wait in microseconds for a CPR register write to
  288. * complete.
  289. */
  290. #define CPR3_REGISTER_WRITE_DELAY_US 200
  291. /*
  292. * The number of times the CPRh controller multiplies the mode switch
  293. * delay before utilizing it.
  294. */
  295. #define CPRH_MODE_SWITCH_DELAY_FACTOR 4
  296. /*
  297. * The number of times the CPRh controller multiplies the delta quotient
  298. * steps before utilizing it.
  299. */
  300. #define CPRH_DELTA_QUOT_STEP_FACTOR 4
  301. static DEFINE_MUTEX(cpr3_controller_list_mutex);
  302. static LIST_HEAD(cpr3_controller_list);
  303. static struct dentry *cpr3_debugfs_base;
  304. /**
  305. * cpr3_read() - read four bytes from the memory address specified
  306. * @ctrl: Pointer to the CPR3 controller
  307. * @offset: Offset in bytes from the CPR3 controller's base address
  308. *
  309. * Return: memory address value
  310. */
  311. static inline u32 cpr3_read(struct cpr3_controller *ctrl, u32 offset)
  312. {
  313. if (!ctrl->cpr_enabled) {
  314. cpr3_err(ctrl, "CPR register reads are not possible when CPR clocks are disabled\n");
  315. return 0;
  316. }
  317. return readl_relaxed(ctrl->cpr_ctrl_base + offset);
  318. }
  319. /**
  320. * cpr3_write() - write four bytes to the memory address specified
  321. * @ctrl: Pointer to the CPR3 controller
  322. * @offset: Offset in bytes from the CPR3 controller's base address
  323. * @value: Value to write to the memory address
  324. *
  325. * Return: none
  326. */
  327. static inline void cpr3_write(struct cpr3_controller *ctrl, u32 offset,
  328. u32 value)
  329. {
  330. if (!ctrl->cpr_enabled) {
  331. cpr3_err(ctrl, "CPR register writes are not possible when CPR clocks are disabled\n");
  332. return;
  333. }
  334. writel_relaxed(value, ctrl->cpr_ctrl_base + offset);
  335. }
  336. /**
  337. * cpr3_masked_write() - perform a read-modify-write sequence so that only
  338. * masked bits are modified
  339. * @ctrl: Pointer to the CPR3 controller
  340. * @offset: Offset in bytes from the CPR3 controller's base address
  341. * @mask: Mask identifying the bits that should be modified
  342. * @value: Value to write to the memory address
  343. *
  344. * Return: none
  345. */
  346. static inline void cpr3_masked_write(struct cpr3_controller *ctrl, u32 offset,
  347. u32 mask, u32 value)
  348. {
  349. u32 reg_val, orig_val;
  350. if (!ctrl->cpr_enabled) {
  351. cpr3_err(ctrl, "CPR register writes are not possible when CPR clocks are disabled\n");
  352. return;
  353. }
  354. reg_val = orig_val = readl_relaxed(ctrl->cpr_ctrl_base + offset);
  355. reg_val &= ~mask;
  356. reg_val |= value & mask;
  357. if (reg_val != orig_val)
  358. writel_relaxed(reg_val, ctrl->cpr_ctrl_base + offset);
  359. }
  360. /**
  361. * cpr3_ctrl_loop_enable() - enable the CPR sensing loop for a given controller
  362. * @ctrl: Pointer to the CPR3 controller
  363. *
  364. * Return: none
  365. */
  366. static inline void cpr3_ctrl_loop_enable(struct cpr3_controller *ctrl)
  367. {
  368. if (ctrl->cpr_enabled && !(ctrl->aggr_corner.sdelta
  369. && ctrl->aggr_corner.sdelta->allow_boost))
  370. cpr3_masked_write(ctrl, CPR3_REG_CPR_CTL,
  371. CPR3_CPR_CTL_LOOP_EN_MASK, CPR3_CPR_CTL_LOOP_ENABLE);
  372. }
  373. /**
  374. * cpr3_ctrl_loop_disable() - disable the CPR sensing loop for a given
  375. * controller
  376. * @ctrl: Pointer to the CPR3 controller
  377. *
  378. * Return: none
  379. */
  380. static inline void cpr3_ctrl_loop_disable(struct cpr3_controller *ctrl)
  381. {
  382. if (ctrl->cpr_enabled)
  383. cpr3_masked_write(ctrl, CPR3_REG_CPR_CTL,
  384. CPR3_CPR_CTL_LOOP_EN_MASK, CPR3_CPR_CTL_LOOP_DISABLE);
  385. }
  386. /**
  387. * cpr3_clock_enable() - prepare and enable all clocks used by this CPR3
  388. * controller
  389. * @ctrl: Pointer to the CPR3 controller
  390. *
  391. * Return: 0 on success, errno on failure
  392. */
  393. static int cpr3_clock_enable(struct cpr3_controller *ctrl)
  394. {
  395. int rc;
  396. rc = clk_prepare_enable(ctrl->bus_clk);
  397. if (rc) {
  398. cpr3_err(ctrl, "failed to enable bus clock, rc=%d\n", rc);
  399. return rc;
  400. }
  401. rc = clk_prepare_enable(ctrl->iface_clk);
  402. if (rc) {
  403. cpr3_err(ctrl, "failed to enable interface clock, rc=%d\n", rc);
  404. clk_disable_unprepare(ctrl->bus_clk);
  405. return rc;
  406. }
  407. rc = clk_prepare_enable(ctrl->core_clk);
  408. if (rc) {
  409. cpr3_err(ctrl, "failed to enable core clock, rc=%d\n", rc);
  410. clk_disable_unprepare(ctrl->iface_clk);
  411. clk_disable_unprepare(ctrl->bus_clk);
  412. return rc;
  413. }
  414. return 0;
  415. }
  416. /**
  417. * cpr3_clock_disable() - disable and unprepare all clocks used by this CPR3
  418. * controller
  419. * @ctrl: Pointer to the CPR3 controller
  420. *
  421. * Return: none
  422. */
  423. static void cpr3_clock_disable(struct cpr3_controller *ctrl)
  424. {
  425. clk_disable_unprepare(ctrl->core_clk);
  426. clk_disable_unprepare(ctrl->iface_clk);
  427. clk_disable_unprepare(ctrl->bus_clk);
  428. }
  429. /**
  430. * cpr3_ctrl_clear_cpr4_config() - clear the CPR4 register configuration
  431. * programmed for current aggregated corner of a given controller
  432. * @ctrl: Pointer to the CPR3 controller
  433. *
  434. * Return: 0 on success, errno on failure
  435. */
  436. static inline int cpr3_ctrl_clear_cpr4_config(struct cpr3_controller *ctrl)
  437. {
  438. struct cpr4_sdelta *aggr_sdelta = ctrl->aggr_corner.sdelta;
  439. bool cpr_enabled = ctrl->cpr_enabled;
  440. int i, rc = 0;
  441. if (!aggr_sdelta || !(aggr_sdelta->allow_core_count_adj
  442. || aggr_sdelta->allow_temp_adj || aggr_sdelta->allow_boost))
  443. /* cpr4 features are not enabled */
  444. return 0;
  445. /* Ensure that CPR clocks are enabled before writing to registers. */
  446. if (!cpr_enabled) {
  447. rc = cpr3_clock_enable(ctrl);
  448. if (rc) {
  449. cpr3_err(ctrl, "clock enable failed, rc=%d\n", rc);
  450. return rc;
  451. }
  452. ctrl->cpr_enabled = true;
  453. }
  454. /*
  455. * Clear feature enable configuration made for current
  456. * aggregated corner.
  457. */
  458. cpr3_masked_write(ctrl, CPR4_REG_MARGIN_ADJ_CTL,
  459. CPR4_MARGIN_ADJ_CTL_MAX_NUM_CORES_MASK
  460. | CPR4_MARGIN_ADJ_CTL_CORE_ADJ_EN
  461. | CPR4_MARGIN_ADJ_CTL_TEMP_ADJ_EN
  462. | CPR4_MARGIN_ADJ_CTL_KV_MARGIN_ADJ_EN
  463. | CPR4_MARGIN_ADJ_CTL_BOOST_EN
  464. | CPR4_MARGIN_ADJ_CTL_HW_CLOSED_LOOP_EN_MASK, 0);
  465. cpr3_masked_write(ctrl, CPR4_REG_MISC,
  466. CPR4_MISC_MARGIN_TABLE_ROW_SELECT_MASK,
  467. 0 << CPR4_MISC_MARGIN_TABLE_ROW_SELECT_SHIFT);
  468. for (i = 0; i <= aggr_sdelta->max_core_count; i++) {
  469. /* Clear voltage margin adjustments programmed in TEMP_COREi */
  470. cpr3_write(ctrl, CPR4_REG_MARGIN_TEMP_CORE(i), 0);
  471. }
  472. /* Turn off CPR clocks if they were off before this function call. */
  473. if (!cpr_enabled) {
  474. cpr3_clock_disable(ctrl);
  475. ctrl->cpr_enabled = false;
  476. }
  477. return 0;
  478. }
  479. /**
  480. * cpr3_closed_loop_enable() - enable logical CPR closed-loop operation
  481. * @ctrl: Pointer to the CPR3 controller
  482. *
  483. * Return: 0 on success, errno on failure
  484. */
  485. static int cpr3_closed_loop_enable(struct cpr3_controller *ctrl)
  486. {
  487. int rc;
  488. if (!ctrl->cpr_allowed_hw || !ctrl->cpr_allowed_sw) {
  489. cpr3_err(ctrl, "cannot enable closed-loop CPR operation because it is disallowed\n");
  490. return -EPERM;
  491. } else if (ctrl->cpr_enabled) {
  492. /* Already enabled */
  493. return 0;
  494. } else if (ctrl->cpr_suspended) {
  495. /*
  496. * CPR must remain disabled as the system is entering suspend.
  497. */
  498. return 0;
  499. }
  500. rc = cpr3_clock_enable(ctrl);
  501. if (rc) {
  502. cpr3_err(ctrl, "unable to enable CPR clocks, rc=%d\n", rc);
  503. return rc;
  504. }
  505. ctrl->cpr_enabled = true;
  506. cpr3_debug(ctrl, "CPR closed-loop operation enabled\n");
  507. return 0;
  508. }
  509. /**
  510. * cpr3_closed_loop_disable() - disable logical CPR closed-loop operation
  511. * @ctrl: Pointer to the CPR3 controller
  512. *
  513. * Return: 0 on success, errno on failure
  514. */
  515. static inline int cpr3_closed_loop_disable(struct cpr3_controller *ctrl)
  516. {
  517. if (!ctrl->cpr_enabled) {
  518. /* Already disabled */
  519. return 0;
  520. }
  521. cpr3_clock_disable(ctrl);
  522. ctrl->cpr_enabled = false;
  523. cpr3_debug(ctrl, "CPR closed-loop operation disabled\n");
  524. return 0;
  525. }
  526. /**
  527. * cpr3_regulator_get_gcnt() - returns the GCNT register value corresponding
  528. * to the clock rate and sensor time of the CPR3 controller
  529. * @ctrl: Pointer to the CPR3 controller
  530. *
  531. * Return: GCNT value
  532. */
  533. static u32 cpr3_regulator_get_gcnt(struct cpr3_controller *ctrl)
  534. {
  535. u64 temp;
  536. unsigned int remainder;
  537. u32 gcnt;
  538. temp = (u64)ctrl->cpr_clock_rate * (u64)ctrl->sensor_time;
  539. remainder = do_div(temp, 1000000000);
  540. if (remainder)
  541. temp++;
  542. /*
  543. * GCNT == 0 corresponds to a single ref clock measurement interval so
  544. * offset GCNT values by 1.
  545. */
  546. gcnt = temp - 1;
  547. return gcnt;
  548. }
  549. /**
  550. * cpr3_regulator_init_thread() - performs hardware initialization of CPR
  551. * thread registers
  552. * @thread: Pointer to the CPR3 thread
  553. *
  554. * CPR interface/bus clocks must be enabled before calling this function.
  555. *
  556. * Return: 0 on success, errno on failure
  557. */
  558. static int cpr3_regulator_init_thread(struct cpr3_thread *thread)
  559. {
  560. u32 reg;
  561. reg = (thread->consecutive_up << CPR3_THRESH_CONS_UP_SHIFT)
  562. & CPR3_THRESH_CONS_UP_MASK;
  563. reg |= (thread->consecutive_down << CPR3_THRESH_CONS_DOWN_SHIFT)
  564. & CPR3_THRESH_CONS_DOWN_MASK;
  565. reg |= (thread->up_threshold << CPR3_THRESH_UP_THRESH_SHIFT)
  566. & CPR3_THRESH_UP_THRESH_MASK;
  567. reg |= (thread->down_threshold << CPR3_THRESH_DOWN_THRESH_SHIFT)
  568. & CPR3_THRESH_DOWN_THRESH_MASK;
  569. cpr3_write(thread->ctrl, CPR3_REG_THRESH(thread->thread_id), reg);
  570. /*
  571. * Mask all RO's initially so that unused thread doesn't contribute
  572. * to closed-loop voltage.
  573. */
  574. cpr3_write(thread->ctrl, CPR3_REG_RO_MASK(thread->thread_id),
  575. CPR3_RO_MASK);
  576. return 0;
  577. }
  578. /**
  579. * cpr4_regulator_init_temp_points() - performs hardware initialization of CPR4
  580. * registers to track tsen temperature data and also specify the
  581. * temperature band range values to apply different voltage margins
  582. * @ctrl: Pointer to the CPR3 controller
  583. *
  584. * CPR interface/bus clocks must be enabled before calling this function.
  585. *
  586. * Return: 0 on success, errno on failure
  587. */
  588. static int cpr4_regulator_init_temp_points(struct cpr3_controller *ctrl)
  589. {
  590. if (!ctrl->allow_temp_adj)
  591. return 0;
  592. cpr3_masked_write(ctrl, CPR4_REG_MISC,
  593. CPR4_MISC_TEMP_SENSOR_ID_START_MASK,
  594. ctrl->temp_sensor_id_start
  595. << CPR4_MISC_TEMP_SENSOR_ID_START_SHIFT);
  596. cpr3_masked_write(ctrl, CPR4_REG_MISC,
  597. CPR4_MISC_TEMP_SENSOR_ID_END_MASK,
  598. ctrl->temp_sensor_id_end
  599. << CPR4_MISC_TEMP_SENSOR_ID_END_SHIFT);
  600. cpr3_masked_write(ctrl, CPR4_REG_MARGIN_TEMP_POINT2,
  601. CPR4_MARGIN_TEMP_POINT2_MASK,
  602. (ctrl->temp_band_count == 4 ? ctrl->temp_points[2] : 0x7FF)
  603. << CPR4_MARGIN_TEMP_POINT2_SHIFT);
  604. cpr3_masked_write(ctrl, CPR4_REG_MARGIN_TEMP_POINT0N1,
  605. CPR4_MARGIN_TEMP_POINT1_MASK,
  606. (ctrl->temp_band_count >= 3 ? ctrl->temp_points[1] : 0x7FF)
  607. << CPR4_MARGIN_TEMP_POINT1_SHIFT);
  608. cpr3_masked_write(ctrl, CPR4_REG_MARGIN_TEMP_POINT0N1,
  609. CPR4_MARGIN_TEMP_POINT0_MASK,
  610. (ctrl->temp_band_count >= 2 ? ctrl->temp_points[0] : 0x7FF)
  611. << CPR4_MARGIN_TEMP_POINT0_SHIFT);
  612. return 0;
  613. }
  614. /**
  615. * cpr3_regulator_init_cpr4() - performs hardware initialization at the
  616. * controller and thread level required for CPR4 operation.
  617. * @ctrl: Pointer to the CPR3 controller
  618. *
  619. * CPR interface/bus clocks must be enabled before calling this function.
  620. * This function allocates sdelta structures and sdelta tables for aggregated
  621. * corners of the controller and its threads.
  622. *
  623. * Return: 0 on success, errno on failure
  624. */
  625. static int cpr3_regulator_init_cpr4(struct cpr3_controller *ctrl)
  626. {
  627. struct cpr3_thread *thread;
  628. struct cpr3_regulator *vreg;
  629. struct cpr4_sdelta *sdelta;
  630. int i, j, ctrl_max_core_count, thread_max_core_count, rc = 0;
  631. bool ctrl_valid_sdelta, thread_valid_sdelta;
  632. u32 pmic_step_size = 1;
  633. int thread_id = 0;
  634. u64 temp;
  635. if (ctrl->reset_step_quot_loop_en)
  636. cpr3_masked_write(ctrl, CPR4_REG_MISC,
  637. CPR4_MISC_RESET_STEP_QUOT_LOOP_EN,
  638. CPR4_MISC_RESET_STEP_QUOT_LOOP_EN);
  639. if (ctrl->thread_has_always_vote_en)
  640. cpr3_masked_write(ctrl, CPR4_REG_MISC,
  641. CPR4_MISC_THREAD_HAS_ALWAYS_VOTE_EN,
  642. CPR4_MISC_THREAD_HAS_ALWAYS_VOTE_EN);
  643. if (ctrl->supports_hw_closed_loop) {
  644. if (ctrl->saw_use_unit_mV)
  645. pmic_step_size = ctrl->step_volt / 1000;
  646. cpr3_masked_write(ctrl, CPR4_REG_MARGIN_ADJ_CTL,
  647. CPR4_MARGIN_ADJ_CTL_PMIC_STEP_SIZE_MASK,
  648. (pmic_step_size
  649. << CPR4_MARGIN_ADJ_CTL_PMIC_STEP_SIZE_SHIFT));
  650. cpr3_masked_write(ctrl, CPR4_REG_SAW_ERROR_STEP_LIMIT,
  651. CPR4_SAW_ERROR_STEP_LIMIT_DN_MASK,
  652. (ctrl->down_error_step_limit
  653. << CPR4_SAW_ERROR_STEP_LIMIT_DN_SHIFT));
  654. cpr3_masked_write(ctrl, CPR4_REG_SAW_ERROR_STEP_LIMIT,
  655. CPR4_SAW_ERROR_STEP_LIMIT_UP_MASK,
  656. (ctrl->up_error_step_limit
  657. << CPR4_SAW_ERROR_STEP_LIMIT_UP_SHIFT));
  658. /*
  659. * Enable thread aggregation regardless of which threads are
  660. * enabled or disabled.
  661. */
  662. cpr3_masked_write(ctrl, CPR4_REG_CPR_TIMER_CLAMP,
  663. CPR4_CPR_TIMER_CLAMP_THREAD_AGGREGATION_EN,
  664. CPR4_CPR_TIMER_CLAMP_THREAD_AGGREGATION_EN);
  665. switch (ctrl->thread_count) {
  666. case 0:
  667. /* Disable both threads */
  668. cpr3_masked_write(ctrl, CPR4_REG_CPR_MASK_THREAD(0),
  669. CPR4_CPR_MASK_THREAD_DISABLE_THREAD
  670. | CPR4_CPR_MASK_THREAD_RO_MASK4THREAD_MASK,
  671. CPR4_CPR_MASK_THREAD_DISABLE_THREAD
  672. | CPR4_CPR_MASK_THREAD_RO_MASK4THREAD_MASK);
  673. cpr3_masked_write(ctrl, CPR4_REG_CPR_MASK_THREAD(1),
  674. CPR4_CPR_MASK_THREAD_DISABLE_THREAD
  675. | CPR4_CPR_MASK_THREAD_RO_MASK4THREAD_MASK,
  676. CPR4_CPR_MASK_THREAD_DISABLE_THREAD
  677. | CPR4_CPR_MASK_THREAD_RO_MASK4THREAD_MASK);
  678. break;
  679. case 1:
  680. /* Disable unused thread */
  681. thread_id = ctrl->thread[0].thread_id ? 0 : 1;
  682. cpr3_masked_write(ctrl,
  683. CPR4_REG_CPR_MASK_THREAD(thread_id),
  684. CPR4_CPR_MASK_THREAD_DISABLE_THREAD
  685. | CPR4_CPR_MASK_THREAD_RO_MASK4THREAD_MASK,
  686. CPR4_CPR_MASK_THREAD_DISABLE_THREAD
  687. | CPR4_CPR_MASK_THREAD_RO_MASK4THREAD_MASK);
  688. break;
  689. }
  690. }
  691. if (!ctrl->allow_core_count_adj && !ctrl->allow_temp_adj
  692. && !ctrl->allow_boost) {
  693. /*
  694. * Skip below configuration as none of the features
  695. * are enabled.
  696. */
  697. return rc;
  698. }
  699. if (ctrl->supports_hw_closed_loop)
  700. cpr3_masked_write(ctrl, CPR4_REG_MARGIN_ADJ_CTL,
  701. CPR4_MARGIN_ADJ_CTL_TIMER_SETTLE_VOLTAGE_EN,
  702. CPR4_MARGIN_ADJ_CTL_TIMER_SETTLE_VOLTAGE_EN);
  703. cpr3_masked_write(ctrl, CPR4_REG_MARGIN_ADJ_CTL,
  704. CPR4_MARGIN_ADJ_CTL_KV_MARGIN_ADJ_STEP_QUOT_MASK,
  705. ctrl->step_quot_fixed
  706. << CPR4_MARGIN_ADJ_CTL_KV_MARGIN_ADJ_STEP_QUOT_SHIFT);
  707. cpr3_masked_write(ctrl, CPR4_REG_MARGIN_ADJ_CTL,
  708. CPR4_MARGIN_ADJ_CTL_PER_RO_KV_MARGIN_EN,
  709. (ctrl->use_dynamic_step_quot
  710. ? CPR4_MARGIN_ADJ_CTL_PER_RO_KV_MARGIN_EN : 0));
  711. cpr3_masked_write(ctrl, CPR4_REG_MARGIN_ADJ_CTL,
  712. CPR4_MARGIN_ADJ_CTL_INITIAL_TEMP_BAND_MASK,
  713. ctrl->initial_temp_band
  714. << CPR4_MARGIN_ADJ_CTL_INITIAL_TEMP_BAND_SHIFT);
  715. rc = cpr4_regulator_init_temp_points(ctrl);
  716. if (rc) {
  717. cpr3_err(ctrl, "initialize temp points failed, rc=%d\n", rc);
  718. return rc;
  719. }
  720. if (ctrl->voltage_settling_time) {
  721. /*
  722. * Configure the settling timer used to account for
  723. * one VDD supply step.
  724. */
  725. temp = (u64)ctrl->cpr_clock_rate
  726. * (u64)ctrl->voltage_settling_time;
  727. do_div(temp, 1000000000);
  728. cpr3_masked_write(ctrl, CPR4_REG_MARGIN_TEMP_CORE_TIMERS,
  729. CPR4_MARGIN_TEMP_CORE_TIMERS_SETTLE_VOLTAGE_COUNT_MASK,
  730. temp
  731. << CPR4_MARGIN_TEMP_CORE_TIMERS_SETTLE_VOLTAGE_COUNT_SHIFT);
  732. }
  733. /*
  734. * Allocate memory for cpr4_sdelta structure and sdelta table for
  735. * controller aggregated corner by finding the maximum core count
  736. * used by any cpr3 regulators.
  737. */
  738. ctrl_max_core_count = 1;
  739. ctrl_valid_sdelta = false;
  740. for (i = 0; i < ctrl->thread_count; i++) {
  741. thread = &ctrl->thread[i];
  742. /*
  743. * Allocate memory for cpr4_sdelta structure and sdelta table
  744. * for thread aggregated corner by finding the maximum core
  745. * count used by any cpr3 regulators of the thread.
  746. */
  747. thread_max_core_count = 1;
  748. thread_valid_sdelta = false;
  749. for (j = 0; j < thread->vreg_count; j++) {
  750. vreg = &thread->vreg[j];
  751. thread_max_core_count = max(thread_max_core_count,
  752. vreg->max_core_count);
  753. thread_valid_sdelta |= (vreg->allow_core_count_adj
  754. | vreg->allow_temp_adj
  755. | vreg->allow_boost);
  756. }
  757. if (thread_valid_sdelta) {
  758. sdelta = devm_kzalloc(ctrl->dev, sizeof(*sdelta),
  759. GFP_KERNEL);
  760. if (!sdelta)
  761. return -ENOMEM;
  762. sdelta->table = devm_kcalloc(ctrl->dev,
  763. thread_max_core_count
  764. * ctrl->temp_band_count,
  765. sizeof(*sdelta->table),
  766. GFP_KERNEL);
  767. if (!sdelta->table)
  768. return -ENOMEM;
  769. sdelta->boost_table = devm_kcalloc(ctrl->dev,
  770. ctrl->temp_band_count,
  771. sizeof(*sdelta->boost_table),
  772. GFP_KERNEL);
  773. if (!sdelta->boost_table)
  774. return -ENOMEM;
  775. thread->aggr_corner.sdelta = sdelta;
  776. }
  777. ctrl_valid_sdelta |= thread_valid_sdelta;
  778. ctrl_max_core_count = max(ctrl_max_core_count,
  779. thread_max_core_count);
  780. }
  781. if (ctrl_valid_sdelta) {
  782. sdelta = devm_kzalloc(ctrl->dev, sizeof(*sdelta), GFP_KERNEL);
  783. if (!sdelta)
  784. return -ENOMEM;
  785. sdelta->table = devm_kcalloc(ctrl->dev, ctrl_max_core_count
  786. * ctrl->temp_band_count,
  787. sizeof(*sdelta->table), GFP_KERNEL);
  788. if (!sdelta->table)
  789. return -ENOMEM;
  790. sdelta->boost_table = devm_kcalloc(ctrl->dev,
  791. ctrl->temp_band_count,
  792. sizeof(*sdelta->boost_table),
  793. GFP_KERNEL);
  794. if (!sdelta->boost_table)
  795. return -ENOMEM;
  796. ctrl->aggr_corner.sdelta = sdelta;
  797. }
  798. return 0;
  799. }
  800. /**
  801. * cpr3_write_temp_core_margin() - programs hardware SDELTA registers with
  802. * the voltage margin adjustments that need to be applied for
  803. * different online core-count and temperature bands.
  804. * @ctrl: Pointer to the CPR3 controller
  805. * @addr: SDELTA register address
  806. * @temp_core_adj: Array of voltage margin values for different temperature
  807. * bands.
  808. *
  809. * CPR interface/bus clocks must be enabled before calling this function.
  810. *
  811. * Return: none
  812. */
  813. static void cpr3_write_temp_core_margin(struct cpr3_controller *ctrl,
  814. int addr, int *temp_core_adj)
  815. {
  816. int i, margin_steps;
  817. u32 reg = 0;
  818. for (i = 0; i < ctrl->temp_band_count; i++) {
  819. margin_steps = max(min(temp_core_adj[i], 127), -128);
  820. reg |= (margin_steps & CPR4_MARGIN_TEMP_CORE_ADJ_MASK) <<
  821. (i * CPR4_MARGIN_TEMP_CORE_ADJ_SHIFT);
  822. }
  823. cpr3_write(ctrl, addr, reg);
  824. cpr3_debug(ctrl, "sdelta offset=0x%08x, val=0x%08x\n", addr, reg);
  825. }
  826. /**
  827. * cpr3_controller_program_sdelta() - programs hardware SDELTA registers with
  828. * the voltage margin adjustments that need to be applied at
  829. * different online core-count and temperature bands. Also,
  830. * programs hardware register configuration for per-online-core
  831. * and per-temperature based adjustments.
  832. * @ctrl: Pointer to the CPR3 controller
  833. *
  834. * CPR interface/bus clocks must be enabled before calling this function.
  835. *
  836. * Return: 0 on success, errno on failure
  837. */
  838. static int cpr3_controller_program_sdelta(struct cpr3_controller *ctrl)
  839. {
  840. struct cpr3_corner *corner = &ctrl->aggr_corner;
  841. struct cpr4_sdelta *sdelta = corner->sdelta;
  842. int i, index, max_core_count, rc = 0;
  843. bool cpr_enabled = ctrl->cpr_enabled;
  844. if (!sdelta)
  845. /* cpr4_sdelta not defined for current aggregated corner */
  846. return 0;
  847. if (ctrl->supports_hw_closed_loop && ctrl->cpr_enabled) {
  848. cpr3_masked_write(ctrl, CPR4_REG_MARGIN_ADJ_CTL,
  849. CPR4_MARGIN_ADJ_CTL_HW_CLOSED_LOOP_EN_MASK,
  850. (ctrl->use_hw_closed_loop && !sdelta->allow_boost)
  851. ? CPR4_MARGIN_ADJ_CTL_HW_CLOSED_LOOP_ENABLE : 0);
  852. }
  853. if (!sdelta->allow_core_count_adj && !sdelta->allow_temp_adj
  854. && !sdelta->allow_boost) {
  855. /*
  856. * Per-online-core, per-temperature and voltage boost
  857. * adjustments are disabled for this aggregation corner.
  858. */
  859. return 0;
  860. }
  861. /* Ensure that CPR clocks are enabled before writing to registers. */
  862. if (!cpr_enabled) {
  863. rc = cpr3_clock_enable(ctrl);
  864. if (rc) {
  865. cpr3_err(ctrl, "clock enable failed, rc=%d\n", rc);
  866. return rc;
  867. }
  868. ctrl->cpr_enabled = true;
  869. }
  870. max_core_count = sdelta->max_core_count;
  871. if (sdelta->allow_core_count_adj || sdelta->allow_temp_adj) {
  872. if (sdelta->allow_core_count_adj) {
  873. /* Program TEMP_CORE0 to same margins as TEMP_CORE1 */
  874. cpr3_write_temp_core_margin(ctrl,
  875. CPR4_REG_MARGIN_TEMP_CORE(0),
  876. &sdelta->table[0]);
  877. }
  878. for (i = 0; i < max_core_count; i++) {
  879. index = i * sdelta->temp_band_count;
  880. /*
  881. * Program TEMP_COREi with voltage margin adjustments
  882. * that need to be applied when the number of cores
  883. * becomes i.
  884. */
  885. cpr3_write_temp_core_margin(ctrl,
  886. CPR4_REG_MARGIN_TEMP_CORE(
  887. sdelta->allow_core_count_adj
  888. ? i + 1 : max_core_count),
  889. &sdelta->table[index]);
  890. }
  891. }
  892. if (sdelta->allow_boost) {
  893. /* Program only boost_num_cores row of SDELTA */
  894. cpr3_write_temp_core_margin(ctrl,
  895. CPR4_REG_MARGIN_TEMP_CORE(sdelta->boost_num_cores),
  896. &sdelta->boost_table[0]);
  897. }
  898. if (!sdelta->allow_core_count_adj && !sdelta->allow_boost) {
  899. cpr3_masked_write(ctrl, CPR4_REG_MISC,
  900. CPR4_MISC_MARGIN_TABLE_ROW_SELECT_MASK,
  901. max_core_count
  902. << CPR4_MISC_MARGIN_TABLE_ROW_SELECT_SHIFT);
  903. }
  904. cpr3_masked_write(ctrl, CPR4_REG_MARGIN_ADJ_CTL,
  905. CPR4_MARGIN_ADJ_CTL_MAX_NUM_CORES_MASK
  906. | CPR4_MARGIN_ADJ_CTL_CORE_ADJ_EN
  907. | CPR4_MARGIN_ADJ_CTL_TEMP_ADJ_EN
  908. | CPR4_MARGIN_ADJ_CTL_KV_MARGIN_ADJ_EN
  909. | CPR4_MARGIN_ADJ_CTL_BOOST_EN,
  910. max_core_count << CPR4_MARGIN_ADJ_CTL_MAX_NUM_CORES_SHIFT
  911. | ((sdelta->allow_core_count_adj || sdelta->allow_boost)
  912. ? CPR4_MARGIN_ADJ_CTL_CORE_ADJ_EN : 0)
  913. | ((sdelta->allow_temp_adj && ctrl->supports_hw_closed_loop)
  914. ? CPR4_MARGIN_ADJ_CTL_TEMP_ADJ_EN : 0)
  915. | (((ctrl->use_hw_closed_loop && !sdelta->allow_boost)
  916. || !ctrl->supports_hw_closed_loop)
  917. ? CPR4_MARGIN_ADJ_CTL_KV_MARGIN_ADJ_EN : 0)
  918. | (sdelta->allow_boost
  919. ? CPR4_MARGIN_ADJ_CTL_BOOST_EN : 0));
  920. /*
  921. * Ensure that all previous CPR register writes have completed before
  922. * continuing.
  923. */
  924. mb();
  925. /* Turn off CPR clocks if they were off before this function call. */
  926. if (!cpr_enabled) {
  927. cpr3_clock_disable(ctrl);
  928. ctrl->cpr_enabled = false;
  929. }
  930. return 0;
  931. }
  932. /**
  933. * cpr3_regulator_set_base_target_quot() - configure the target quotient
  934. * for each RO of the CPR3 regulator for CPRh operation.
  935. * In particular, the quotient of the RO selected for operation
  936. * should correspond to the lowest target quotient across the
  937. * corners supported by the single regulator of the CPR3 thread.
  938. * @vreg: Pointer to the CPR3 regulator
  939. * @base_quots: Pointer to the base quotient array. The array must be
  940. * of size CPR3_RO_COUNT and it is populated with the
  941. * base quotient per-RO.
  942. *
  943. * Return: none
  944. */
  945. static void cpr3_regulator_set_base_target_quot(struct cpr3_regulator *vreg,
  946. u32 *base_quots)
  947. {
  948. struct cpr3_controller *ctrl = vreg->thread->ctrl;
  949. int i, j, ro_mask = CPR3_RO_MASK;
  950. u32 min_quot;
  951. for (i = 0; i < vreg->corner_count; i++)
  952. ro_mask &= vreg->corner[i].ro_mask;
  953. /* Unmask the ROs selected for active use. */
  954. cpr3_write(ctrl, CPR3_REG_RO_MASK(vreg->thread->thread_id),
  955. ro_mask);
  956. for (i = 0; i < CPR3_RO_COUNT; i++) {
  957. for (j = 0, min_quot = INT_MAX; j < vreg->corner_count; j++)
  958. if (vreg->corner[j].target_quot[i])
  959. min_quot = min(min_quot,
  960. vreg->corner[j].target_quot[i]);
  961. if (min_quot == INT_MAX)
  962. min_quot = 0;
  963. cpr3_write(ctrl,
  964. CPR3_REG_TARGET_QUOT(vreg->thread->thread_id, i),
  965. min_quot);
  966. base_quots[i] = min_quot;
  967. }
  968. }
  969. /**
  970. * cpr3_regulator_init_cprh_corners() - configure the per-corner CPRh registers
  971. * @vreg: Pointer to the CPR3 regulator
  972. *
  973. * This function programs the controller registers which contain all information
  974. * necessary to resolve the closed-loop voltage per-corner at runtime such as
  975. * open-loop and floor voltages, target quotient delta, and RO select value.
  976. * These registers also provide a means to disable closed-loop operation, core
  977. * and temperature adjustments.
  978. *
  979. * Return: 0 on success, errno on failure
  980. */
  981. static int cpr3_regulator_init_cprh_corners(struct cpr3_regulator *vreg)
  982. {
  983. struct cpr3_controller *ctrl = vreg->thread->ctrl;
  984. struct cpr3_corner *corner;
  985. u32 reg, delta_quot_steps, ro_sel;
  986. u32 *base_quots;
  987. int open_loop_volt_steps, floor_volt_steps, i, j, rc = 0;
  988. base_quots = kcalloc(CPR3_RO_COUNT, sizeof(*base_quots),
  989. GFP_KERNEL);
  990. if (!base_quots)
  991. return -ENOMEM;
  992. cpr3_regulator_set_base_target_quot(vreg, base_quots);
  993. for (i = 0; i < vreg->corner_count; i++) {
  994. corner = &vreg->corner[i];
  995. for (j = 0, ro_sel = INT_MAX; j < CPR3_RO_COUNT; j++) {
  996. if (corner->target_quot[j]) {
  997. ro_sel = j;
  998. break;
  999. }
  1000. }
  1001. if (ro_sel == INT_MAX) {
  1002. if (!corner->proc_freq) {
  1003. /*
  1004. * Corner is not used as active DCVS set point
  1005. * select RO 0 arbitrarily.
  1006. */
  1007. ro_sel = 0;
  1008. } else if (ctrl->ignore_invalid_fuses) {
  1009. /*
  1010. * Fuses are not initialized on some simulator
  1011. * targets. Select RO 0 arbitrarily.
  1012. */
  1013. cpr3_debug(vreg, "ignored that corner=%d has invalid RO select value\n",
  1014. i);
  1015. ro_sel = 0;
  1016. } else {
  1017. cpr3_err(vreg, "corner=%d has invalid RO select value\n",
  1018. i);
  1019. rc = -EINVAL;
  1020. goto free_base_quots;
  1021. }
  1022. }
  1023. open_loop_volt_steps = DIV_ROUND_UP(corner->open_loop_volt -
  1024. ctrl->base_volt,
  1025. ctrl->step_volt);
  1026. floor_volt_steps = DIV_ROUND_UP(corner->floor_volt -
  1027. ctrl->base_volt,
  1028. ctrl->step_volt);
  1029. delta_quot_steps = corner->proc_freq ?
  1030. DIV_ROUND_UP(corner->target_quot[ro_sel] -
  1031. base_quots[ro_sel],
  1032. CPRH_DELTA_QUOT_STEP_FACTOR) :
  1033. 0;
  1034. if (open_loop_volt_steps > CPRH_CORNER_INIT_VOLTAGE_MAX_VALUE ||
  1035. floor_volt_steps > CPRH_CORNER_FLOOR_VOLTAGE_MAX_VALUE ||
  1036. delta_quot_steps > CPRH_CORNER_QUOT_DELTA_MAX_VALUE) {
  1037. cpr3_err(ctrl, "invalid CPRh corner configuration: open_loop_volt_steps=%d (%d max.), floor_volt_steps=%d (%d max), delta_quot_steps=%d (%d max)\n",
  1038. open_loop_volt_steps,
  1039. CPRH_CORNER_INIT_VOLTAGE_MAX_VALUE,
  1040. floor_volt_steps,
  1041. CPRH_CORNER_FLOOR_VOLTAGE_MAX_VALUE,
  1042. delta_quot_steps,
  1043. CPRH_CORNER_QUOT_DELTA_MAX_VALUE);
  1044. rc = -EINVAL;
  1045. goto free_base_quots;
  1046. }
  1047. reg = (open_loop_volt_steps << CPRH_CORNER_INIT_VOLTAGE_SHIFT)
  1048. & CPRH_CORNER_INIT_VOLTAGE_MASK;
  1049. reg |= (floor_volt_steps << CPRH_CORNER_FLOOR_VOLTAGE_SHIFT)
  1050. & CPRH_CORNER_FLOOR_VOLTAGE_MASK;
  1051. reg |= (delta_quot_steps << CPRH_CORNER_QUOT_DELTA_SHIFT)
  1052. & CPRH_CORNER_QUOT_DELTA_MASK;
  1053. reg |= (ro_sel << CPRH_CORNER_RO_SEL_SHIFT)
  1054. & CPRH_CORNER_RO_SEL_MASK;
  1055. if (corner->use_open_loop)
  1056. reg |= CPRH_CORNER_CPR_CL_DISABLE;
  1057. cpr3_debug(ctrl, "corner=%d open_loop_volt_steps=%d, floor_volt_steps=%d, delta_quot_steps=%d, base_volt=%d, step_volt=%d, base_quot=%d\n",
  1058. i, open_loop_volt_steps, floor_volt_steps,
  1059. delta_quot_steps, ctrl->base_volt,
  1060. ctrl->step_volt, base_quots[ro_sel]);
  1061. cpr3_write(ctrl, CPRH_REG_CORNER(vreg->thread, i), reg);
  1062. }
  1063. free_base_quots:
  1064. kfree(base_quots);
  1065. return rc;
  1066. }
  1067. /**
  1068. * cprh_controller_program_sdelta() - programs hardware SDELTA registers with
  1069. * the margins that need to be applied at different online
  1070. * core-count and temperature bands for each corner band. Also,
  1071. * programs hardware register configuration for core-count and
  1072. * temp-based adjustments
  1073. *
  1074. * @ctrl: Pointer to the CPR3 controller
  1075. *
  1076. * CPR interface/bus clocks must be enabled before calling this function.
  1077. *
  1078. * Return: none
  1079. */
  1080. static void cprh_controller_program_sdelta(
  1081. struct cpr3_controller *ctrl)
  1082. {
  1083. /* Only thread 0 supports sdelta */
  1084. struct cpr3_regulator *vreg = &ctrl->thread[0].vreg[0];
  1085. struct cprh_corner_band *corner_band;
  1086. struct cpr4_sdelta *sdelta;
  1087. int i, j, index;
  1088. u32 reg = 0;
  1089. if (!vreg->allow_core_count_adj && !vreg->allow_temp_adj)
  1090. return;
  1091. if (vreg->thread->thread_id != 0) {
  1092. cpr3_err(vreg, "core count and temperature based adjustments are only allowed for CPR thread 0\n");
  1093. return;
  1094. }
  1095. cpr4_regulator_init_temp_points(ctrl);
  1096. for (i = 0; i < CPRH_CORNER_BAND_MAX_COUNT; i++) {
  1097. reg |= (i < vreg->corner_band_count ?
  1098. vreg->corner_band[i].corner
  1099. & CPRH_CORNER_BAND_MASK :
  1100. vreg->corner_count + 1)
  1101. << (i * CPRH_CORNER_BAND_SHIFT);
  1102. }
  1103. cpr3_write(ctrl, CPRH_REG_CORNER_BAND, reg);
  1104. for (i = 0; i < vreg->corner_band_count; i++) {
  1105. corner_band = &vreg->corner_band[i];
  1106. sdelta = corner_band->sdelta;
  1107. if (!sdelta->allow_core_count_adj && !sdelta->allow_temp_adj) {
  1108. /*
  1109. * Per-online-core and per-temperature margin
  1110. * adjustments are disabled for this corner band.
  1111. */
  1112. continue;
  1113. }
  1114. if (vreg->allow_core_count_adj)
  1115. cpr3_write_temp_core_margin(ctrl,
  1116. CPRH_MARGIN_TEMP_CORE_VBAND(0, i),
  1117. &sdelta->table[0]);
  1118. for (j = 0; j < sdelta->max_core_count; j++) {
  1119. index = j * sdelta->temp_band_count;
  1120. cpr3_write_temp_core_margin(ctrl,
  1121. CPRH_MARGIN_TEMP_CORE_VBAND(
  1122. sdelta->allow_core_count_adj
  1123. ? j + 1 : vreg->max_core_count, i),
  1124. &sdelta->table[index]);
  1125. }
  1126. }
  1127. if (!vreg->allow_core_count_adj) {
  1128. cpr3_masked_write(ctrl, CPR4_REG_MISC,
  1129. CPR4_MISC_MARGIN_TABLE_ROW_SELECT_MASK,
  1130. vreg->max_core_count
  1131. << CPR4_MISC_MARGIN_TABLE_ROW_SELECT_SHIFT);
  1132. }
  1133. cpr3_masked_write(ctrl, CPR4_REG_MARGIN_ADJ_CTL,
  1134. CPR4_MARGIN_ADJ_CTL_MAX_NUM_CORES_MASK
  1135. | CPR4_MARGIN_ADJ_CTL_CORE_ADJ_EN
  1136. | CPR4_MARGIN_ADJ_CTL_TEMP_ADJ_EN
  1137. | CPR4_MARGIN_ADJ_CTL_KV_MARGIN_ADJ_EN
  1138. | CPR4_MARGIN_ADJ_CTL_HW_CLOSED_LOOP_EN_MASK,
  1139. vreg->max_core_count << CPR4_MARGIN_ADJ_CTL_MAX_NUM_CORES_SHIFT
  1140. | ((vreg->allow_core_count_adj)
  1141. ? CPR4_MARGIN_ADJ_CTL_CORE_ADJ_EN : 0)
  1142. | (vreg->allow_temp_adj ? CPR4_MARGIN_ADJ_CTL_TEMP_ADJ_EN : 0)
  1143. | ((ctrl->use_hw_closed_loop)
  1144. ? CPR4_MARGIN_ADJ_CTL_KV_MARGIN_ADJ_EN : 0)
  1145. | (ctrl->use_hw_closed_loop
  1146. ? CPR4_MARGIN_ADJ_CTL_HW_CLOSED_LOOP_ENABLE : 0));
  1147. /* Ensure that previous CPR register writes complete */
  1148. mb();
  1149. }
  1150. static int cprh_regulator_aging_adjust(struct cpr3_controller *ctrl);
  1151. /**
  1152. * cpr3_regulator_cprh_initialized() - checks if CPRh has already been
  1153. * initialized by the boot loader
  1154. * @ctrl: Pointer to the CPR3 controller
  1155. *
  1156. * Return: true if CPRh controller is already initialized else false
  1157. */
  1158. static bool cpr3_regulator_cprh_initialized(struct cpr3_controller *ctrl)
  1159. {
  1160. u32 reg;
  1161. if (ctrl->ctrl_type != CPR_CTRL_TYPE_CPRH)
  1162. return false;
  1163. ctrl->cpr_hw_version = readl_relaxed(ctrl->cpr_ctrl_base
  1164. + CPR3_REG_CPR_VERSION);
  1165. reg = readl_relaxed(ctrl->cpr_ctrl_base + CPRH_REG_CTL(ctrl));
  1166. return reg & CPRH_CTL_OSM_ENABLED;
  1167. }
  1168. /**
  1169. * cpr3_regulator_init_cprh() - performs hardware initialization at the
  1170. * controller and thread level required for CPRh operation.
  1171. * @ctrl: Pointer to the CPR3 controller
  1172. *
  1173. * CPR interface/bus clocks must be enabled before calling this function.
  1174. *
  1175. * Return: 0 on success, errno on failure
  1176. */
  1177. static int cpr3_regulator_init_cprh(struct cpr3_controller *ctrl)
  1178. {
  1179. u32 reg, pmic_step_size = 1;
  1180. u64 temp;
  1181. int i, rc;
  1182. /* One or two threads each with a single regulator supported */
  1183. if (ctrl->thread_count < 1 || ctrl->thread_count > 2) {
  1184. cpr3_err(ctrl, "expected 1 or 2 threads but found %d\n",
  1185. ctrl->thread_count);
  1186. return -EINVAL;
  1187. } else if (ctrl->thread[0].vreg_count != 1) {
  1188. cpr3_err(ctrl, "expected 1 regulator for thread 0 but found %d\n",
  1189. ctrl->thread[0].vreg_count);
  1190. return -EINVAL;
  1191. } else if (ctrl->thread_count == 2 && ctrl->thread[1].vreg_count != 1) {
  1192. cpr3_err(ctrl, "expected 1 regulator for thread 1 but found %d\n",
  1193. ctrl->thread[1].vreg_count);
  1194. return -EINVAL;
  1195. }
  1196. rc = cprh_regulator_aging_adjust(ctrl);
  1197. if (rc && rc != -ETIMEDOUT) {
  1198. /*
  1199. * Don't fail initialization if the CPR aging measurement
  1200. * timed out due to sensors not being available.
  1201. */
  1202. cpr3_err(ctrl, "CPR aging adjustment failed, rc=%d\n", rc);
  1203. return rc;
  1204. }
  1205. cprh_controller_program_sdelta(ctrl);
  1206. for (i = 0; i < ctrl->thread_count; i++) {
  1207. rc = cpr3_regulator_init_cprh_corners(&ctrl->thread[i].vreg[0]);
  1208. if (rc) {
  1209. cpr3_err(ctrl, "failed to initialize CPRh corner registers\n");
  1210. return rc;
  1211. }
  1212. }
  1213. if (ctrl->reset_step_quot_loop_en)
  1214. cpr3_masked_write(ctrl, CPR4_REG_MISC,
  1215. CPR4_MISC_RESET_STEP_QUOT_LOOP_EN,
  1216. CPR4_MISC_RESET_STEP_QUOT_LOOP_EN);
  1217. if (ctrl->saw_use_unit_mV)
  1218. pmic_step_size = ctrl->step_volt / 1000;
  1219. cpr3_masked_write(ctrl, CPR4_REG_MARGIN_ADJ_CTL,
  1220. CPR4_MARGIN_ADJ_CTL_PMIC_STEP_SIZE_MASK,
  1221. (pmic_step_size
  1222. << CPR4_MARGIN_ADJ_CTL_PMIC_STEP_SIZE_SHIFT));
  1223. cpr3_masked_write(ctrl, CPR4_REG_SAW_ERROR_STEP_LIMIT,
  1224. CPR4_SAW_ERROR_STEP_LIMIT_DN_MASK,
  1225. (ctrl->down_error_step_limit
  1226. << CPR4_SAW_ERROR_STEP_LIMIT_DN_SHIFT));
  1227. cpr3_masked_write(ctrl, CPR4_REG_SAW_ERROR_STEP_LIMIT,
  1228. CPR4_SAW_ERROR_STEP_LIMIT_UP_MASK,
  1229. (ctrl->up_error_step_limit
  1230. << CPR4_SAW_ERROR_STEP_LIMIT_UP_SHIFT));
  1231. cpr3_masked_write(ctrl, CPR4_REG_MARGIN_ADJ_CTL,
  1232. CPR4_MARGIN_ADJ_CTL_KV_MARGIN_ADJ_STEP_QUOT_MASK,
  1233. ctrl->step_quot_fixed
  1234. << CPR4_MARGIN_ADJ_CTL_KV_MARGIN_ADJ_STEP_QUOT_SHIFT);
  1235. cpr3_masked_write(ctrl, CPR4_REG_MARGIN_ADJ_CTL,
  1236. CPR4_MARGIN_ADJ_CTL_PER_RO_KV_MARGIN_EN,
  1237. (ctrl->use_dynamic_step_quot
  1238. ? CPR4_MARGIN_ADJ_CTL_PER_RO_KV_MARGIN_EN : 0));
  1239. if (ctrl->thread_count > 1)
  1240. cpr3_masked_write(ctrl, CPR4_REG_CPR_TIMER_CLAMP,
  1241. CPR4_CPR_TIMER_CLAMP_THREAD_AGGREGATION_EN,
  1242. CPR4_CPR_TIMER_CLAMP_THREAD_AGGREGATION_EN);
  1243. if (ctrl->voltage_settling_time) {
  1244. /*
  1245. * Configure the settling timer used to account for
  1246. * one VDD supply step.
  1247. */
  1248. temp = (u64)ctrl->cpr_clock_rate
  1249. * (u64)ctrl->voltage_settling_time;
  1250. do_div(temp, 1000000000);
  1251. cpr3_masked_write(ctrl, CPR4_REG_MARGIN_TEMP_CORE_TIMERS,
  1252. CPR4_MARGIN_TEMP_CORE_TIMERS_SETTLE_VOLTAGE_COUNT_MASK,
  1253. temp
  1254. << CPR4_MARGIN_TEMP_CORE_TIMERS_SETTLE_VOLTAGE_COUNT_SHIFT);
  1255. }
  1256. if (ctrl->corner_switch_delay_time) {
  1257. /*
  1258. * Configure the settling timer used to delay
  1259. * following SAW requests
  1260. */
  1261. temp = (u64)ctrl->cpr_clock_rate
  1262. * (u64)ctrl->corner_switch_delay_time;
  1263. do_div(temp, 1000000000);
  1264. do_div(temp, CPRH_MODE_SWITCH_DELAY_FACTOR);
  1265. cpr3_masked_write(ctrl, CPRH_REG_CTL(ctrl),
  1266. CPRH_CTL_MODE_SWITCH_DELAY_MASK,
  1267. temp << CPRH_CTL_MODE_SWITCH_DELAY_SHIFT);
  1268. }
  1269. /*
  1270. * Configure CPRh ACD AVG registers on controllers
  1271. * that support this feature.
  1272. */
  1273. if (ctrl->cpr_hw_version >= CPRH_CPR_VERSION_4P5
  1274. && ctrl->acd_avg_enabled) {
  1275. cpr3_masked_write(ctrl, CPRH_REG_MISC_REG2,
  1276. CPRH_MISC_REG2_ACD_ADJ_STEP_UP_LIMIT_MASK,
  1277. ctrl->acd_adj_up_step_limit <<
  1278. CPRH_MISC_REG2_ACD_ADJ_STEP_UP_LIMIT_SHIFT);
  1279. cpr3_masked_write(ctrl, CPRH_REG_MISC_REG2,
  1280. CPRH_MISC_REG2_ACD_ADJ_STEP_DOWN_LIMIT_MASK,
  1281. ctrl->acd_adj_down_step_limit <<
  1282. CPRH_MISC_REG2_ACD_ADJ_STEP_DOWN_LIMIT_SHIFT);
  1283. cpr3_masked_write(ctrl, CPRH_REG_MISC_REG2,
  1284. CPRH_MISC_REG2_ACD_ADJ_STEP_SIZE_UP_MASK,
  1285. ctrl->acd_adj_up_step_size <<
  1286. CPRH_MISC_REG2_ACD_ADJ_STEP_SIZE_UP_SHIFT);
  1287. cpr3_masked_write(ctrl, CPRH_REG_MISC_REG2,
  1288. CPRH_MISC_REG2_ACD_ADJ_STEP_SIZE_DOWN_MASK,
  1289. ctrl->acd_adj_down_step_size <<
  1290. CPRH_MISC_REG2_ACD_ADJ_STEP_SIZE_DOWN_SHIFT);
  1291. cpr3_masked_write(ctrl, CPRH_REG_MISC_REG2,
  1292. CPRH_MISC_REG2_ACD_NOTWAIT_4_CL_SETTLE_MASK,
  1293. (ctrl->acd_notwait_for_cl_settled
  1294. ? CPRH_MISC_REG2_ACD_NOTWAIT_4_CL_SETTLE_EN
  1295. : 0));
  1296. cpr3_masked_write(ctrl, CPRH_REG_MISC_REG2,
  1297. CPRH_MISC_REG2_ACD_AVG_FAST_UPDATE_EN_MASK,
  1298. (ctrl->acd_adj_avg_fast_update
  1299. ? CPRH_MISC_REG2_ACD_AVG_FAST_UPDATE_EN
  1300. : 0));
  1301. cpr3_masked_write(ctrl, CPRH_REG_MISC_REG2,
  1302. CPRH_MISC_REG2_ACD_AVG_EN_MASK,
  1303. CPRH_MISC_REG2_ACD_AVG_ENABLE);
  1304. }
  1305. /*
  1306. * Program base voltage and voltage multiplier values which
  1307. * are used for floor and initial voltage calculations by the
  1308. * CPRh controller.
  1309. */
  1310. reg = (DIV_ROUND_UP(ctrl->base_volt, ctrl->step_volt)
  1311. << CPRH_CTL_BASE_VOLTAGE_SHIFT)
  1312. & CPRH_CTL_BASE_VOLTAGE_MASK;
  1313. reg |= (DIV_ROUND_UP(ctrl->step_volt, 1000)
  1314. << CPRH_CTL_VOLTAGE_MULTIPLIER_SHIFT)
  1315. & CPRH_CTL_VOLTAGE_MULTIPLIER_MASK;
  1316. /* Enable OSM block interface with CPR */
  1317. reg |= CPRH_CTL_OSM_ENABLED;
  1318. cpr3_masked_write(ctrl, CPRH_REG_CTL(ctrl), CPRH_CTL_BASE_VOLTAGE_MASK
  1319. | CPRH_CTL_VOLTAGE_MULTIPLIER_MASK
  1320. | CPRH_CTL_OSM_ENABLED, reg);
  1321. /* Enable loop_en */
  1322. cpr3_ctrl_loop_enable(ctrl);
  1323. return 0;
  1324. }
  1325. /**
  1326. * cpr3_regulator_init_ctrl() - performs hardware initialization of CPR
  1327. * controller registers
  1328. * @ctrl: Pointer to the CPR3 controller
  1329. *
  1330. * Return: 0 on success, errno on failure
  1331. */
  1332. static int cpr3_regulator_init_ctrl(struct cpr3_controller *ctrl)
  1333. {
  1334. int i, j, k, m, rc;
  1335. u32 ro_used = 0;
  1336. u32 gcnt, cont_dly, up_down_dly, val;
  1337. u64 temp;
  1338. char *mode;
  1339. if (ctrl->core_clk) {
  1340. rc = clk_set_rate(ctrl->core_clk, ctrl->cpr_clock_rate);
  1341. if (rc) {
  1342. cpr3_err(ctrl, "clk_set_rate(core_clk, %u) failed, rc=%d\n",
  1343. ctrl->cpr_clock_rate, rc);
  1344. return rc;
  1345. }
  1346. }
  1347. rc = cpr3_clock_enable(ctrl);
  1348. if (rc) {
  1349. cpr3_err(ctrl, "clock enable failed, rc=%d\n", rc);
  1350. return rc;
  1351. }
  1352. ctrl->cpr_enabled = true;
  1353. ctrl->cpr_hw_version = cpr3_read(ctrl, CPR3_REG_CPR_VERSION);
  1354. /* Find all RO's used by any corner of any regulator. */
  1355. for (i = 0; i < ctrl->thread_count; i++)
  1356. for (j = 0; j < ctrl->thread[i].vreg_count; j++)
  1357. for (k = 0; k < ctrl->thread[i].vreg[j].corner_count;
  1358. k++)
  1359. for (m = 0; m < CPR3_RO_COUNT; m++)
  1360. if (ctrl->thread[i].vreg[j].corner[k].
  1361. target_quot[m])
  1362. ro_used |= BIT(m);
  1363. /* Configure the GCNT of the RO's that will be used */
  1364. gcnt = cpr3_regulator_get_gcnt(ctrl);
  1365. for (i = 0; i < CPR3_RO_COUNT; i++)
  1366. if (ro_used & BIT(i))
  1367. cpr3_write(ctrl, CPR3_REG_GCNT(i), gcnt);
  1368. /* Configure the loop delay time */
  1369. temp = (u64)ctrl->cpr_clock_rate * (u64)ctrl->loop_time;
  1370. do_div(temp, 1000000000);
  1371. cont_dly = temp;
  1372. if (ctrl->supports_hw_closed_loop
  1373. && ctrl->ctrl_type == CPR_CTRL_TYPE_CPR3)
  1374. cpr3_write(ctrl, CPR3_REG_CPR_TIMER_MID_CONT, cont_dly);
  1375. else
  1376. cpr3_write(ctrl, CPR3_REG_CPR_TIMER_AUTO_CONT, cont_dly);
  1377. if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPR3) {
  1378. temp = (u64)ctrl->cpr_clock_rate *
  1379. (u64)ctrl->up_down_delay_time;
  1380. do_div(temp, 1000000000);
  1381. up_down_dly = temp;
  1382. if (ctrl->supports_hw_closed_loop)
  1383. cpr3_write(ctrl, CPR3_REG_CPR_TIMER_UP_DN_CONT,
  1384. up_down_dly);
  1385. cpr3_debug(ctrl, "up_down_dly=%u, up_down_delay_time=%u ns\n",
  1386. up_down_dly, ctrl->up_down_delay_time);
  1387. }
  1388. cpr3_debug(ctrl, "cpr_clock_rate=%u HZ, sensor_time=%u ns, loop_time=%u ns, gcnt=%u, cont_dly=%u\n",
  1389. ctrl->cpr_clock_rate, ctrl->sensor_time, ctrl->loop_time,
  1390. gcnt, cont_dly);
  1391. /* Configure CPR sensor operation */
  1392. val = (ctrl->idle_clocks << CPR3_CPR_CTL_IDLE_CLOCKS_SHIFT)
  1393. & CPR3_CPR_CTL_IDLE_CLOCKS_MASK;
  1394. val |= (ctrl->count_mode << CPR3_CPR_CTL_COUNT_MODE_SHIFT)
  1395. & CPR3_CPR_CTL_COUNT_MODE_MASK;
  1396. val |= (ctrl->count_repeat << CPR3_CPR_CTL_COUNT_REPEAT_SHIFT)
  1397. & CPR3_CPR_CTL_COUNT_REPEAT_MASK;
  1398. cpr3_write(ctrl, CPR3_REG_CPR_CTL, val);
  1399. cpr3_debug(ctrl, "idle_clocks=%u, count_mode=%u, count_repeat=%u; CPR_CTL=0x%08X\n",
  1400. ctrl->idle_clocks, ctrl->count_mode, ctrl->count_repeat, val);
  1401. /* Configure CPR default step quotients */
  1402. val = (ctrl->step_quot_init_min << CPR3_CPR_STEP_QUOT_MIN_SHIFT)
  1403. & CPR3_CPR_STEP_QUOT_MIN_MASK;
  1404. val |= (ctrl->step_quot_init_max << CPR3_CPR_STEP_QUOT_MAX_SHIFT)
  1405. & CPR3_CPR_STEP_QUOT_MAX_MASK;
  1406. cpr3_write(ctrl, CPR3_REG_CPR_STEP_QUOT, val);
  1407. cpr3_debug(ctrl, "step_quot_min=%u, step_quot_max=%u; STEP_QUOT=0x%08X\n",
  1408. ctrl->step_quot_init_min, ctrl->step_quot_init_max, val);
  1409. /* Configure the CPR sensor ownership */
  1410. for (i = 0; i < ctrl->sensor_count; i++)
  1411. cpr3_write(ctrl, CPR3_REG_SENSOR_OWNER(i),
  1412. ctrl->sensor_owner[i]);
  1413. /* Configure per-thread registers */
  1414. for (i = 0; i < ctrl->thread_count; i++) {
  1415. rc = cpr3_regulator_init_thread(&ctrl->thread[i]);
  1416. if (rc) {
  1417. cpr3_err(ctrl, "CPR thread register initialization failed, rc=%d\n",
  1418. rc);
  1419. return rc;
  1420. }
  1421. }
  1422. if (ctrl->supports_hw_closed_loop) {
  1423. if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPR4 ||
  1424. ctrl->ctrl_type == CPR_CTRL_TYPE_CPRH) {
  1425. cpr3_masked_write(ctrl, CPR4_REG_MARGIN_ADJ_CTL,
  1426. CPR4_MARGIN_ADJ_CTL_HW_CLOSED_LOOP_EN_MASK,
  1427. ctrl->use_hw_closed_loop
  1428. ? CPR4_MARGIN_ADJ_CTL_HW_CLOSED_LOOP_ENABLE
  1429. : CPR4_MARGIN_ADJ_CTL_HW_CLOSED_LOOP_DISABLE);
  1430. } else if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPR3) {
  1431. cpr3_write(ctrl, CPR3_REG_HW_CLOSED_LOOP,
  1432. ctrl->use_hw_closed_loop
  1433. ? CPR3_HW_CLOSED_LOOP_ENABLE
  1434. : CPR3_HW_CLOSED_LOOP_DISABLE);
  1435. cpr3_debug(ctrl, "PD_THROTTLE=0x%08X\n",
  1436. ctrl->proc_clock_throttle);
  1437. }
  1438. if ((ctrl->use_hw_closed_loop ||
  1439. ctrl->ctrl_type == CPR_CTRL_TYPE_CPR4) &&
  1440. ctrl->ctrl_type != CPR_CTRL_TYPE_CPRH) {
  1441. rc = regulator_enable(ctrl->vdd_limit_regulator);
  1442. if (rc) {
  1443. cpr3_err(ctrl, "CPR limit regulator enable failed, rc=%d\n",
  1444. rc);
  1445. return rc;
  1446. }
  1447. if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPR3) {
  1448. rc = msm_spm_avs_enable_irq(0,
  1449. MSM_SPM_AVS_IRQ_MAX);
  1450. if (rc) {
  1451. cpr3_err(ctrl, "could not enable max IRQ, rc=%d\n",
  1452. rc);
  1453. return rc;
  1454. }
  1455. }
  1456. }
  1457. }
  1458. if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPR4) {
  1459. rc = cpr3_regulator_init_cpr4(ctrl);
  1460. if (rc) {
  1461. cpr3_err(ctrl, "CPR4-specific controller initialization failed, rc=%d\n",
  1462. rc);
  1463. return rc;
  1464. }
  1465. } else if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPRH) {
  1466. rc = cpr3_regulator_init_cprh(ctrl);
  1467. if (rc) {
  1468. cpr3_err(ctrl, "CPRh-specific controller initialization failed, rc=%d\n",
  1469. rc);
  1470. return rc;
  1471. }
  1472. }
  1473. /* Ensure that all register writes complete before disabling clocks. */
  1474. wmb();
  1475. /* Keep CPR clocks on for CPRh full HW closed-loop operation */
  1476. if (ctrl->ctrl_type != CPR_CTRL_TYPE_CPRH) {
  1477. cpr3_clock_disable(ctrl);
  1478. ctrl->cpr_enabled = false;
  1479. }
  1480. if (!ctrl->cpr_allowed_sw || !ctrl->cpr_allowed_hw)
  1481. mode = "open-loop";
  1482. else if (ctrl->supports_hw_closed_loop &&
  1483. ctrl->ctrl_type != CPR_CTRL_TYPE_CPRH)
  1484. mode = ctrl->use_hw_closed_loop
  1485. ? "HW closed-loop" : "SW closed-loop";
  1486. else if (ctrl->supports_hw_closed_loop &&
  1487. ctrl->ctrl_type == CPR_CTRL_TYPE_CPRH)
  1488. mode = ctrl->use_hw_closed_loop
  1489. ? "full HW closed-loop" : "open-loop";
  1490. else
  1491. mode = "closed-loop";
  1492. cpr3_info(ctrl, "Default CPR mode = %s", mode);
  1493. return 0;
  1494. }
  1495. /**
  1496. * cpr3_regulator_init_hw_closed_loop_dependencies() - perform hardware
  1497. * initialization steps to ensure that CPR HW closed-loop voltage
  1498. * change requests are able to reach the PMIC regulator
  1499. * @pdev: Platform device pointer for the CPR3 controller
  1500. * @ctrl: Pointer to the CPR3 controller
  1501. *
  1502. * Return: 0 on success, errno on failure
  1503. */
  1504. static int
  1505. cpr3_regulator_init_hw_closed_loop_dependencies(struct platform_device *pdev,
  1506. struct cpr3_controller *ctrl)
  1507. {
  1508. struct resource *res;
  1509. int rc = 0;
  1510. u32 val;
  1511. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "saw");
  1512. if (res && res->start)
  1513. ctrl->saw_base = devm_ioremap(&pdev->dev, res->start,
  1514. resource_size(res));
  1515. if (ctrl->saw_base) {
  1516. /* Configure SAW registers directly. */
  1517. rc = of_property_read_u32(ctrl->dev->of_node,
  1518. "qcom,saw-avs-ctrl", &val);
  1519. if (rc) {
  1520. cpr3_err(ctrl, "unable to read DT property qcom,saw-avs-ctrl, rc=%d\n",
  1521. rc);
  1522. return rc;
  1523. }
  1524. writel_relaxed(val, ctrl->saw_base + SAW_REG_AVS_CTL);
  1525. rc = of_property_read_u32(ctrl->dev->of_node,
  1526. "qcom,saw-avs-limit", &val);
  1527. if (rc) {
  1528. cpr3_err(ctrl, "unable to read DT property qcom,saw-avs-limit, rc=%d\n",
  1529. rc);
  1530. return rc;
  1531. }
  1532. writel_relaxed(val, ctrl->saw_base + SAW_REG_AVS_LIMIT);
  1533. } else {
  1534. /* Wait for SPM driver to configure SAW registers. */
  1535. rc = msm_spm_probe_done();
  1536. if (rc) {
  1537. if (rc != -EPROBE_DEFER)
  1538. cpr3_err(ctrl, "spm unavailable, rc=%d\n", rc);
  1539. return rc;
  1540. }
  1541. }
  1542. return 0;
  1543. }
  1544. /**
  1545. * cpr3_regulator_set_target_quot() - configure the target quotient for each
  1546. * RO of the CPR3 thread and set the RO mask
  1547. * @thread: Pointer to the CPR3 thread
  1548. *
  1549. * Return: none
  1550. */
  1551. static void cpr3_regulator_set_target_quot(struct cpr3_thread *thread)
  1552. {
  1553. u32 new_quot, last_quot;
  1554. int i;
  1555. if (thread->aggr_corner.ro_mask == CPR3_RO_MASK
  1556. && thread->last_closed_loop_aggr_corner.ro_mask == CPR3_RO_MASK) {
  1557. /* Avoid writing target quotients since all RO's are masked. */
  1558. return;
  1559. } else if (thread->aggr_corner.ro_mask == CPR3_RO_MASK) {
  1560. cpr3_write(thread->ctrl, CPR3_REG_RO_MASK(thread->thread_id),
  1561. CPR3_RO_MASK);
  1562. thread->last_closed_loop_aggr_corner.ro_mask = CPR3_RO_MASK;
  1563. /*
  1564. * Only the RO_MASK register needs to be written since all
  1565. * RO's are masked.
  1566. */
  1567. return;
  1568. } else if (thread->aggr_corner.ro_mask
  1569. != thread->last_closed_loop_aggr_corner.ro_mask) {
  1570. cpr3_write(thread->ctrl, CPR3_REG_RO_MASK(thread->thread_id),
  1571. thread->aggr_corner.ro_mask);
  1572. }
  1573. for (i = 0; i < CPR3_RO_COUNT; i++) {
  1574. new_quot = thread->aggr_corner.target_quot[i];
  1575. last_quot = thread->last_closed_loop_aggr_corner.target_quot[i];
  1576. if (new_quot != last_quot)
  1577. cpr3_write(thread->ctrl,
  1578. CPR3_REG_TARGET_QUOT(thread->thread_id, i),
  1579. new_quot);
  1580. }
  1581. thread->last_closed_loop_aggr_corner = thread->aggr_corner;
  1582. }
  1583. /**
  1584. * cpr3_update_vreg_closed_loop_volt() - update the last known settled
  1585. * closed loop voltage for a CPR3 regulator
  1586. * @vreg: Pointer to the CPR3 regulator
  1587. * @vdd_volt: Last known settled voltage in microvolts for the
  1588. * VDD supply
  1589. * @reg_last_measurement: Value read from the LAST_MEASUREMENT register
  1590. *
  1591. * Return: none
  1592. */
  1593. static void cpr3_update_vreg_closed_loop_volt(struct cpr3_regulator *vreg,
  1594. int vdd_volt, u32 reg_last_measurement)
  1595. {
  1596. bool step_dn, step_up, aggr_step_up, aggr_step_dn, aggr_step_mid;
  1597. bool valid, pd_valid, saw_error;
  1598. struct cpr3_controller *ctrl = vreg->thread->ctrl;
  1599. struct cpr3_corner *corner;
  1600. u32 id;
  1601. if (vreg->last_closed_loop_corner == CPR3_REGULATOR_CORNER_INVALID)
  1602. return;
  1603. corner = &vreg->corner[vreg->last_closed_loop_corner];
  1604. if (vreg->thread->last_closed_loop_aggr_corner.ro_mask
  1605. == CPR3_RO_MASK || !vreg->aggregated) {
  1606. return;
  1607. } else if (!ctrl->cpr_enabled || !ctrl->last_corner_was_closed_loop) {
  1608. return;
  1609. } else if (ctrl->thread_count == 1
  1610. && vdd_volt >= corner->floor_volt
  1611. && vdd_volt <= corner->ceiling_volt) {
  1612. corner->last_volt = vdd_volt;
  1613. cpr3_debug(vreg, "last_volt updated: last_volt[%d]=%d, ceiling_volt[%d]=%d, floor_volt[%d]=%d\n",
  1614. vreg->last_closed_loop_corner, corner->last_volt,
  1615. vreg->last_closed_loop_corner,
  1616. corner->ceiling_volt,
  1617. vreg->last_closed_loop_corner,
  1618. corner->floor_volt);
  1619. return;
  1620. } else if (!ctrl->supports_hw_closed_loop) {
  1621. return;
  1622. } else if (ctrl->ctrl_type != CPR_CTRL_TYPE_CPR3) {
  1623. corner->last_volt = vdd_volt;
  1624. cpr3_debug(vreg, "last_volt updated: last_volt[%d]=%d, ceiling_volt[%d]=%d, floor_volt[%d]=%d\n",
  1625. vreg->last_closed_loop_corner, corner->last_volt,
  1626. vreg->last_closed_loop_corner,
  1627. corner->ceiling_volt,
  1628. vreg->last_closed_loop_corner,
  1629. corner->floor_volt);
  1630. return;
  1631. }
  1632. /* CPR clocks are on and HW closed loop is supported */
  1633. valid = !!(reg_last_measurement & CPR3_LAST_MEASUREMENT_VALID);
  1634. if (!valid) {
  1635. cpr3_debug(vreg, "CPR_LAST_VALID_MEASUREMENT=0x%X valid bit not set\n",
  1636. reg_last_measurement);
  1637. return;
  1638. }
  1639. id = vreg->thread->thread_id;
  1640. step_dn
  1641. = !!(reg_last_measurement & CPR3_LAST_MEASUREMENT_THREAD_DN(id));
  1642. step_up
  1643. = !!(reg_last_measurement & CPR3_LAST_MEASUREMENT_THREAD_UP(id));
  1644. aggr_step_dn = !!(reg_last_measurement & CPR3_LAST_MEASUREMENT_AGGR_DN);
  1645. aggr_step_mid
  1646. = !!(reg_last_measurement & CPR3_LAST_MEASUREMENT_AGGR_MID);
  1647. aggr_step_up = !!(reg_last_measurement & CPR3_LAST_MEASUREMENT_AGGR_UP);
  1648. saw_error = !!(reg_last_measurement & CPR3_LAST_MEASUREMENT_SAW_ERROR);
  1649. pd_valid
  1650. = !((((reg_last_measurement & CPR3_LAST_MEASUREMENT_PD_BYPASS_MASK)
  1651. >> CPR3_LAST_MEASUREMENT_PD_BYPASS_SHIFT)
  1652. & vreg->pd_bypass_mask) == vreg->pd_bypass_mask);
  1653. if (!pd_valid) {
  1654. cpr3_debug(vreg, "CPR_LAST_VALID_MEASUREMENT=0x%X, all power domains bypassed\n",
  1655. reg_last_measurement);
  1656. return;
  1657. } else if (step_dn && step_up) {
  1658. cpr3_err(vreg, "both up and down status bits set, CPR_LAST_VALID_MEASUREMENT=0x%X\n",
  1659. reg_last_measurement);
  1660. return;
  1661. } else if (aggr_step_dn && step_dn && vdd_volt < corner->last_volt
  1662. && vdd_volt >= corner->floor_volt) {
  1663. corner->last_volt = vdd_volt;
  1664. } else if (aggr_step_up && step_up && vdd_volt > corner->last_volt
  1665. && vdd_volt <= corner->ceiling_volt) {
  1666. corner->last_volt = vdd_volt;
  1667. } else if (aggr_step_mid
  1668. && vdd_volt >= corner->floor_volt
  1669. && vdd_volt <= corner->ceiling_volt) {
  1670. corner->last_volt = vdd_volt;
  1671. } else if (saw_error && (vdd_volt == corner->ceiling_volt
  1672. || vdd_volt == corner->floor_volt)) {
  1673. corner->last_volt = vdd_volt;
  1674. } else {
  1675. cpr3_debug(vreg, "last_volt not updated: last_volt[%d]=%d, ceiling_volt[%d]=%d, floor_volt[%d]=%d, vdd_volt=%d, CPR_LAST_VALID_MEASUREMENT=0x%X\n",
  1676. vreg->last_closed_loop_corner, corner->last_volt,
  1677. vreg->last_closed_loop_corner,
  1678. corner->ceiling_volt,
  1679. vreg->last_closed_loop_corner, corner->floor_volt,
  1680. vdd_volt, reg_last_measurement);
  1681. return;
  1682. }
  1683. cpr3_debug(vreg, "last_volt updated: last_volt[%d]=%d, ceiling_volt[%d]=%d, floor_volt[%d]=%d, CPR_LAST_VALID_MEASUREMENT=0x%X\n",
  1684. vreg->last_closed_loop_corner, corner->last_volt,
  1685. vreg->last_closed_loop_corner, corner->ceiling_volt,
  1686. vreg->last_closed_loop_corner, corner->floor_volt,
  1687. reg_last_measurement);
  1688. }
  1689. /**
  1690. * cpr3_regulator_config_ldo_retention() - configure per-regulator LDO retention
  1691. * mode
  1692. * @vreg: Pointer to the CPR3 regulator to configure
  1693. * @ref_volt: Reference voltage used to determine if LDO retention
  1694. * mode can be allowed. It corresponds either to the
  1695. * aggregated floor voltage or the next VDD supply setpoint
  1696. *
  1697. * This function determines if a CPR3 regulator's configuration satisfies safe
  1698. * operating voltages for LDO retention and uses the regulator_allow_bypass()
  1699. * interface on the LDO retention regulator to enable or disable such feature
  1700. * accordingly.
  1701. *
  1702. * Return: 0 on success, errno on failure
  1703. */
  1704. static int cpr3_regulator_config_ldo_retention(struct cpr3_regulator *vreg,
  1705. int ref_volt)
  1706. {
  1707. struct regulator *ldo_ret_reg = vreg->ldo_ret_regulator;
  1708. int retention_volt, rc;
  1709. enum msm_ldo_supply_mode mode;
  1710. if (!ldo_ret_reg) {
  1711. /* LDO retention regulator is not defined */
  1712. return 0;
  1713. }
  1714. retention_volt = regulator_get_voltage(ldo_ret_reg);
  1715. if (retention_volt < 0) {
  1716. cpr3_err(vreg, "regulator_get_voltage(ldo_ret) failed, rc=%d\n",
  1717. retention_volt);
  1718. return retention_volt;
  1719. }
  1720. mode = ref_volt >= retention_volt + vreg->ldo_min_headroom_volt
  1721. ? LDO_MODE : BHS_MODE;
  1722. rc = regulator_allow_bypass(ldo_ret_reg, mode);
  1723. if (rc)
  1724. cpr3_err(vreg, "regulator_allow_bypass(ldo_ret) == %s failed, rc=%d\n",
  1725. mode ? "true" : "false", rc);
  1726. return rc;
  1727. }
  1728. /**
  1729. * cpr3_regulator_config_kryo_ldo_mem_acc() - configure the mem-acc regulator
  1730. * corner based upon a future Kryo LDO regulator voltage setpoint
  1731. * @vreg: Pointer to the CPR3 regulator
  1732. * @new_volt: New voltage in microvolts that the LDO regulator needs
  1733. * to end up at
  1734. *
  1735. * This function determines if a new LDO regulator set point will result
  1736. * in crossing the voltage threshold that requires reconfiguration of
  1737. * the mem-acc regulator associated with a CPR3 regulator and if so, performs
  1738. * the correct sequence to select the correct mem-acc corner.
  1739. *
  1740. * Return: 0 on success, errno on failure
  1741. */
  1742. static int cpr3_regulator_config_kryo_ldo_mem_acc(struct cpr3_regulator *vreg,
  1743. int new_volt)
  1744. {
  1745. struct cpr3_controller *ctrl = vreg->thread->ctrl;
  1746. struct regulator *ldo_reg = vreg->ldo_regulator;
  1747. struct regulator *mem_acc_reg = vreg->mem_acc_regulator;
  1748. int mem_acc_volt = ctrl->mem_acc_threshold_volt;
  1749. int last_volt, safe_volt, mem_acc_corn, rc;
  1750. enum msm_apm_supply apm_mode;
  1751. if (!mem_acc_reg || !mem_acc_volt || !ldo_reg)
  1752. return 0;
  1753. apm_mode = msm_apm_get_supply(ctrl->apm);
  1754. if (apm_mode < 0) {
  1755. cpr3_err(ctrl, "APM get supply failed, rc=%d\n",
  1756. apm_mode);
  1757. return apm_mode;
  1758. }
  1759. last_volt = regulator_get_voltage(ldo_reg);
  1760. if (last_volt < 0) {
  1761. cpr3_err(vreg, "regulator_get_voltage(ldo) failed, rc=%d\n",
  1762. last_volt);
  1763. return last_volt;
  1764. }
  1765. if (((last_volt < mem_acc_volt && mem_acc_volt <= new_volt)
  1766. || (last_volt >= mem_acc_volt && mem_acc_volt > new_volt))) {
  1767. if (apm_mode == ctrl->apm_high_supply)
  1768. safe_volt = min(vreg->ldo_max_volt, mem_acc_volt);
  1769. else
  1770. safe_volt = min(max(ctrl->system_supply_max_volt -
  1771. vreg->ldo_max_headroom_volt,
  1772. mem_acc_volt), vreg->ldo_max_volt);
  1773. rc = regulator_set_voltage(ldo_reg, safe_volt,
  1774. max(new_volt, last_volt));
  1775. if (rc) {
  1776. cpr3_err(ctrl, "regulator_set_voltage(ldo) == %d failed, rc=%d\n",
  1777. mem_acc_volt, rc);
  1778. return rc;
  1779. }
  1780. mem_acc_corn = new_volt < mem_acc_volt ?
  1781. ctrl->mem_acc_corner_map[CPR3_MEM_ACC_LOW_CORNER] :
  1782. ctrl->mem_acc_corner_map[CPR3_MEM_ACC_HIGH_CORNER];
  1783. rc = regulator_set_voltage(mem_acc_reg, mem_acc_corn,
  1784. mem_acc_corn);
  1785. if (rc) {
  1786. cpr3_err(ctrl, "regulator_set_voltage(mem_acc) == %d failed, rc=%d\n",
  1787. 0, rc);
  1788. return rc;
  1789. }
  1790. }
  1791. return 0;
  1792. }
  1793. /**
  1794. * cpr3_regulator_kryo_bhs_prepare() - configure the Kryo LDO regulator
  1795. * associated with a CPR3 regulator in preparation for BHS
  1796. * mode switch.
  1797. * @vreg: Pointer to the CPR3 regulator
  1798. * @vdd_volt: Last known settled voltage in microvolts for the VDD
  1799. * supply
  1800. * @vdd_ceiling_volt: Last known aggregated ceiling voltage in microvolts for
  1801. * the VDD supply
  1802. *
  1803. * This function performs the necessary steps prior to switching a Kryo LDO
  1804. * regulator to BHS mode (LDO bypassed mode).
  1805. *
  1806. * Return: 0 on success, errno on failure
  1807. */
  1808. static int cpr3_regulator_kryo_bhs_prepare(struct cpr3_regulator *vreg,
  1809. int vdd_volt, int vdd_ceiling_volt)
  1810. {
  1811. struct regulator *ldo_reg = vreg->ldo_regulator;
  1812. int bhs_volt, rc;
  1813. bhs_volt = vdd_volt - vreg->ldo_min_headroom_volt;
  1814. if (bhs_volt > vreg->ldo_max_volt) {
  1815. cpr3_debug(vreg, "limited to LDO output of %d uV when switching to BHS mode\n",
  1816. vreg->ldo_max_volt);
  1817. bhs_volt = vreg->ldo_max_volt;
  1818. }
  1819. rc = cpr3_regulator_config_kryo_ldo_mem_acc(vreg, bhs_volt);
  1820. if (rc) {
  1821. cpr3_err(vreg, "failed to configure mem-acc settings\n");
  1822. return rc;
  1823. }
  1824. rc = regulator_set_voltage(ldo_reg, bhs_volt, min(vdd_ceiling_volt,
  1825. vreg->ldo_max_volt));
  1826. if (rc) {
  1827. cpr3_err(vreg, "regulator_set_voltage(ldo) == %d failed, rc=%d\n",
  1828. bhs_volt, rc);
  1829. return rc;
  1830. }
  1831. return rc;
  1832. }
  1833. /**
  1834. * cpr3_regulator_set_bhs_mode() - configure the LDO regulator associated with
  1835. * a CPR3 regulator to BHS mode
  1836. * @vreg: Pointer to the CPR3 regulator
  1837. * @vdd_volt: Last known settled voltage in microvolts for the VDD
  1838. * supply
  1839. * @vdd_ceiling_volt: Last known aggregated ceiling voltage in microvolts for
  1840. * the VDD supply
  1841. *
  1842. * This function performs the necessary steps to switch an LDO regulator
  1843. * to BHS mode (LDO bypassed mode).
  1844. */
  1845. static int cpr3_regulator_set_bhs_mode(struct cpr3_regulator *vreg,
  1846. int vdd_volt, int vdd_ceiling_volt)
  1847. {
  1848. struct regulator *ldo_reg = vreg->ldo_regulator;
  1849. int rc;
  1850. if (vreg->ldo_type == CPR3_LDO_KRYO) {
  1851. rc = cpr3_regulator_kryo_bhs_prepare(vreg, vdd_volt,
  1852. vdd_ceiling_volt);
  1853. if (rc) {
  1854. cpr3_err(vreg, "cpr3 regulator bhs mode prepare failed, rc=%d\n",
  1855. rc);
  1856. return rc;
  1857. }
  1858. }
  1859. rc = regulator_allow_bypass(ldo_reg, BHS_MODE);
  1860. if (rc) {
  1861. cpr3_err(vreg, "regulator_allow_bypass(bhs) == %s failed, rc=%d\n",
  1862. BHS_MODE ? "true" : "false", rc);
  1863. return rc;
  1864. }
  1865. vreg->ldo_regulator_bypass = BHS_MODE;
  1866. return rc;
  1867. }
  1868. /**
  1869. * cpr3_regulator_ldo_apm_prepare() - configure LDO regulators associated
  1870. * with each CPR3 regulator of a CPR3 controller in preparation
  1871. * for an APM switch.
  1872. * @ctrl: Pointer to the CPR3 controller
  1873. * @new_volt: New voltage in microvolts that the VDD supply
  1874. * needs to end up at
  1875. * @last_volt: Last known voltage in microvolts for the VDD supply
  1876. * @aggr_corner: Pointer to the CPR3 corner which corresponds to the max
  1877. * corner aggregated from all CPR3 threads managed by the
  1878. * CPR3 controller
  1879. *
  1880. * This function ensures LDO regulator hardware requirements are met before
  1881. * an APM switch is requested. The function must be called as the last step
  1882. * before switching the APM mode.
  1883. *
  1884. * Return: 0 on success, errno on failure
  1885. */
  1886. static int cpr3_regulator_ldo_apm_prepare(struct cpr3_controller *ctrl,
  1887. int new_volt, int last_volt,
  1888. struct cpr3_corner *aggr_corner)
  1889. {
  1890. struct cpr3_regulator *vreg;
  1891. struct cpr3_corner *current_corner;
  1892. enum msm_apm_supply apm_mode;
  1893. int i, j, safe_volt, max_volt, ldo_volt, ref_volt, rc;
  1894. apm_mode = msm_apm_get_supply(ctrl->apm);
  1895. if (apm_mode < 0) {
  1896. cpr3_err(ctrl, "APM get supply failed, rc=%d\n", apm_mode);
  1897. return apm_mode;
  1898. }
  1899. if (apm_mode == ctrl->apm_low_supply ||
  1900. new_volt >= ctrl->apm_threshold_volt)
  1901. return 0;
  1902. /*
  1903. * Guarantee LDO maximum headroom is not violated when the APM is
  1904. * switched to the system-supply source.
  1905. */
  1906. for (i = 0; i < ctrl->thread_count; i++) {
  1907. for (j = 0; j < ctrl->thread[i].vreg_count; j++) {
  1908. vreg = &ctrl->thread[i].vreg[j];
  1909. if (!vreg->vreg_enabled || vreg->current_corner
  1910. == CPR3_REGULATOR_CORNER_INVALID)
  1911. continue;
  1912. if (!vreg->ldo_regulator || !vreg->ldo_mode_allowed ||
  1913. vreg->ldo_regulator_bypass == BHS_MODE)
  1914. continue;
  1915. /*
  1916. * If the new VDD configuration does not satisfy
  1917. * requirements for LDO usage, switch the regulator
  1918. * to BHS mode. By doing so, the LDO maximum headroom
  1919. * does not need to be enforced.
  1920. */
  1921. current_corner = &vreg->corner[vreg->current_corner];
  1922. ldo_volt = current_corner->open_loop_volt
  1923. - vreg->ldo_adjust_volt;
  1924. ref_volt = ctrl->use_hw_closed_loop ?
  1925. aggr_corner->floor_volt :
  1926. new_volt;
  1927. if (ref_volt < ldo_volt + vreg->ldo_min_headroom_volt
  1928. || ldo_volt < ctrl->system_supply_max_volt -
  1929. vreg->ldo_max_headroom_volt ||
  1930. ldo_volt > vreg->ldo_max_volt) {
  1931. rc = cpr3_regulator_set_bhs_mode(vreg,
  1932. last_volt, aggr_corner->ceiling_volt);
  1933. if (rc)
  1934. return rc;
  1935. /*
  1936. * Do not enforce LDO maximum headroom since the
  1937. * regulator is now configured to BHS mode.
  1938. */
  1939. continue;
  1940. }
  1941. safe_volt = min(max(ldo_volt,
  1942. ctrl->system_supply_max_volt
  1943. - vreg->ldo_max_headroom_volt),
  1944. vreg->ldo_max_volt);
  1945. max_volt = min(ctrl->system_supply_max_volt,
  1946. vreg->ldo_max_volt);
  1947. rc = regulator_set_voltage(vreg->ldo_regulator,
  1948. safe_volt, max_volt);
  1949. if (rc) {
  1950. cpr3_err(vreg, "regulator_set_voltage(ldo) == %d failed, rc=%d\n",
  1951. safe_volt, rc);
  1952. return rc;
  1953. }
  1954. }
  1955. }
  1956. return 0;
  1957. }
  1958. /**
  1959. * cpr3_regulator_config_vreg_kryo_ldo() - configure the voltage and bypass
  1960. * state for the Kryo LDO regulator associated with a single CPR3
  1961. * regulator.
  1962. *
  1963. * @vreg: Pointer to the CPR3 regulator
  1964. * @vdd_floor_volt: Last known aggregated floor voltage in microvolts for
  1965. * the VDD supply
  1966. * @vdd_ceiling_volt: Last known aggregated ceiling voltage in microvolts for
  1967. * the VDD supply
  1968. * @ref_volt: Reference voltage in microvolts corresponds either to
  1969. * the aggregated floor voltage or the next VDD supply
  1970. * setpoint.
  1971. * @last_volt: Last known voltage in microvolts for the VDD supply
  1972. *
  1973. * This function performs all relevant LDO or BHS configurations if a Kryo LDO
  1974. * regulator is specified.
  1975. *
  1976. * Return: 0 on success, errno on failure
  1977. */
  1978. static int cpr3_regulator_config_vreg_kryo_ldo(struct cpr3_regulator *vreg,
  1979. int vdd_floor_volt, int vdd_ceiling_volt,
  1980. int ref_volt, int last_volt)
  1981. {
  1982. struct cpr3_controller *ctrl = vreg->thread->ctrl;
  1983. struct regulator *ldo_reg = vreg->ldo_regulator;
  1984. struct cpr3_corner *current_corner;
  1985. enum msm_apm_supply apm_mode;
  1986. int rc, ldo_volt, final_ldo_volt, bhs_volt, max_volt, safe_volt;
  1987. current_corner = &vreg->corner[vreg->current_corner];
  1988. ldo_volt = current_corner->open_loop_volt
  1989. - vreg->ldo_adjust_volt;
  1990. bhs_volt = last_volt - vreg->ldo_min_headroom_volt;
  1991. max_volt = min(vdd_ceiling_volt, vreg->ldo_max_volt);
  1992. if (ref_volt >= ldo_volt + vreg->ldo_min_headroom_volt &&
  1993. ldo_volt >= ctrl->system_supply_max_volt -
  1994. vreg->ldo_max_headroom_volt &&
  1995. bhs_volt >= ctrl->system_supply_max_volt -
  1996. vreg->ldo_max_headroom_volt &&
  1997. ldo_volt <= vreg->ldo_max_volt) {
  1998. /* LDO minimum and maximum headrooms satisfied */
  1999. apm_mode = msm_apm_get_supply(ctrl->apm);
  2000. if (apm_mode < 0) {
  2001. cpr3_err(ctrl, "APM get supply failed, rc=%d\n",
  2002. apm_mode);
  2003. return apm_mode;
  2004. }
  2005. if (vreg->ldo_regulator_bypass == BHS_MODE) {
  2006. /*
  2007. * BHS to LDO transition. Configure LDO output
  2008. * to min(max LDO output, VDD - LDO headroom)
  2009. * voltage if APM is on high supply source or
  2010. * min(max(system-supply ceiling - LDO max headroom,
  2011. * VDD - LDO headroom), max LDO output) if
  2012. * APM is on low supply source, then switch
  2013. * regulator mode.
  2014. */
  2015. if (apm_mode == ctrl->apm_high_supply)
  2016. safe_volt = min(vreg->ldo_max_volt, bhs_volt);
  2017. else
  2018. safe_volt =
  2019. min(max(ctrl->system_supply_max_volt -
  2020. vreg->ldo_max_headroom_volt,
  2021. bhs_volt),
  2022. vreg->ldo_max_volt);
  2023. rc = cpr3_regulator_config_kryo_ldo_mem_acc(vreg,
  2024. safe_volt);
  2025. if (rc) {
  2026. cpr3_err(vreg, "failed to configure mem-acc settings\n");
  2027. return rc;
  2028. }
  2029. rc = regulator_set_voltage(ldo_reg, safe_volt,
  2030. max_volt);
  2031. if (rc) {
  2032. cpr3_err(vreg, "regulator_set_voltage(ldo) == %d failed, rc=%d\n",
  2033. safe_volt, rc);
  2034. return rc;
  2035. }
  2036. rc = regulator_allow_bypass(ldo_reg, LDO_MODE);
  2037. if (rc) {
  2038. cpr3_err(vreg, "regulator_allow_bypass(ldo) == %s failed, rc=%d\n",
  2039. LDO_MODE ? "true" : "false", rc);
  2040. return rc;
  2041. }
  2042. vreg->ldo_regulator_bypass = LDO_MODE;
  2043. }
  2044. /* Configure final LDO output voltage */
  2045. if (apm_mode == ctrl->apm_high_supply)
  2046. final_ldo_volt = max(ldo_volt,
  2047. vdd_ceiling_volt -
  2048. vreg->ldo_max_headroom_volt);
  2049. else
  2050. final_ldo_volt = ldo_volt;
  2051. rc = cpr3_regulator_config_kryo_ldo_mem_acc(vreg,
  2052. final_ldo_volt);
  2053. if (rc) {
  2054. cpr3_err(vreg, "failed to configure mem-acc settings\n");
  2055. return rc;
  2056. }
  2057. rc = regulator_set_voltage(ldo_reg, final_ldo_volt, max_volt);
  2058. if (rc) {
  2059. cpr3_err(vreg, "regulator_set_voltage(ldo) == %d failed, rc=%d\n",
  2060. final_ldo_volt, rc);
  2061. return rc;
  2062. }
  2063. } else {
  2064. if (vreg->ldo_regulator_bypass == LDO_MODE) {
  2065. /* LDO to BHS transition */
  2066. rc = cpr3_regulator_set_bhs_mode(vreg, last_volt,
  2067. vdd_ceiling_volt);
  2068. if (rc)
  2069. return rc;
  2070. }
  2071. }
  2072. return 0;
  2073. }
  2074. /**
  2075. * cpr3_regulator_config_vreg_ldo300() - configure the voltage and bypass state
  2076. * for the LDO300 regulator associated with a single CPR3
  2077. * regulator.
  2078. *
  2079. * @vreg: Pointer to the CPR3 regulator
  2080. * @new_volt: New voltage in microvolts that VDD supply needs to
  2081. * end up at
  2082. * @vdd_ceiling_volt: Last known aggregated ceiling voltage in microvolts for
  2083. * the VDD supply
  2084. *
  2085. * This function performs all relevant LDO or BHS configurations for an LDO300
  2086. * type regulator.
  2087. *
  2088. * Return: 0 on success, errno on failure
  2089. */
  2090. static int cpr3_regulator_config_vreg_ldo300(struct cpr3_regulator *vreg,
  2091. int new_volt, int vdd_ceiling_volt)
  2092. {
  2093. struct regulator *ldo_reg = vreg->ldo_regulator;
  2094. struct cpr3_corner *corner;
  2095. bool mode;
  2096. int rc = 0;
  2097. corner = &vreg->corner[vreg->current_corner];
  2098. mode = corner->ldo_mode_allowed ? LDO_MODE : BHS_MODE;
  2099. if (mode == LDO_MODE) {
  2100. rc = regulator_set_voltage(ldo_reg, new_volt, vdd_ceiling_volt);
  2101. if (rc) {
  2102. cpr3_err(vreg, "regulator_set_voltage(ldo) == %d failed, rc=%d\n",
  2103. new_volt, rc);
  2104. return rc;
  2105. }
  2106. }
  2107. if (vreg->ldo_regulator_bypass != mode) {
  2108. rc = regulator_allow_bypass(ldo_reg, mode);
  2109. if (rc) {
  2110. cpr3_err(vreg, "regulator_allow_bypass(%s) is failed, rc=%d\n",
  2111. mode == LDO_MODE ? "ldo" : "bhs", rc);
  2112. return rc;
  2113. }
  2114. vreg->ldo_regulator_bypass = mode;
  2115. }
  2116. return rc;
  2117. }
  2118. /**
  2119. * cpr3_regulator_config_vreg_ldo() - configure the voltage and bypass state for
  2120. * the LDO regulator associated with a single CPR3 regulator.
  2121. *
  2122. * @vreg: Pointer to the CPR3 regulator
  2123. * @vdd_floor_volt: Last known aggregated floor voltage in microvolts for
  2124. * the VDD supply
  2125. * @vdd_ceiling_volt: Last known aggregated ceiling voltage in microvolts for
  2126. * the VDD supply
  2127. * @new_volt: New voltage in microvolts that VDD supply needs to
  2128. * end up at
  2129. * @last_volt: Last known voltage in microvolts for the VDD supply
  2130. *
  2131. * This function identifies the type of LDO regulator associated with a CPR3
  2132. * regulator and invokes the LDO specific configuration functions.
  2133. *
  2134. * Return: 0 on success, errno on failure
  2135. */
  2136. static int cpr3_regulator_config_vreg_ldo(struct cpr3_regulator *vreg,
  2137. int vdd_floor_volt, int vdd_ceiling_volt,
  2138. int new_volt, int last_volt)
  2139. {
  2140. struct cpr3_controller *ctrl = vreg->thread->ctrl;
  2141. int ref_volt, rc;
  2142. ref_volt = ctrl->use_hw_closed_loop ? vdd_floor_volt :
  2143. new_volt;
  2144. rc = cpr3_regulator_config_ldo_retention(vreg, ref_volt);
  2145. if (rc)
  2146. return rc;
  2147. if (!vreg->vreg_enabled ||
  2148. vreg->current_corner == CPR3_REGULATOR_CORNER_INVALID)
  2149. return 0;
  2150. switch (vreg->ldo_type) {
  2151. case CPR3_LDO_KRYO:
  2152. rc = cpr3_regulator_config_vreg_kryo_ldo(vreg, vdd_floor_volt,
  2153. vdd_ceiling_volt, ref_volt, last_volt);
  2154. if (rc)
  2155. cpr3_err(vreg, "kryo ldo regulator config failed, rc=%d\n",
  2156. rc);
  2157. break;
  2158. case CPR3_LDO300:
  2159. rc = cpr3_regulator_config_vreg_ldo300(vreg, new_volt,
  2160. vdd_ceiling_volt);
  2161. if (rc)
  2162. cpr3_err(vreg, "ldo300 regulator config failed, rc=%d\n",
  2163. rc);
  2164. break;
  2165. default:
  2166. cpr3_err(vreg, "invalid ldo regulator type = %d\n",
  2167. vreg->ldo_type);
  2168. rc = -EINVAL;
  2169. }
  2170. return rc;
  2171. }
  2172. /**
  2173. * cpr3_regulator_config_ldo() - configure the voltage and bypass state for the
  2174. * LDO regulator associated with each CPR3 regulator of a CPR3
  2175. * controller
  2176. * @ctrl: Pointer to the CPR3 controller
  2177. * @vdd_floor_volt: Last known aggregated floor voltage in microvolts for
  2178. * the VDD supply
  2179. * @vdd_ceiling_volt: Last known aggregated ceiling voltage in microvolts for
  2180. * the VDD supply
  2181. * @new_volt: New voltage in microvolts that VDD supply needs to
  2182. * end up at
  2183. * @last_volt: Last known voltage in microvolts for the VDD supply
  2184. *
  2185. * Return: 0 on success, errno on failure
  2186. */
  2187. static int cpr3_regulator_config_ldo(struct cpr3_controller *ctrl,
  2188. int vdd_floor_volt, int vdd_ceiling_volt,
  2189. int new_volt, int last_volt)
  2190. {
  2191. struct cpr3_regulator *vreg;
  2192. int i, j, rc;
  2193. for (i = 0; i < ctrl->thread_count; i++) {
  2194. for (j = 0; j < ctrl->thread[i].vreg_count; j++) {
  2195. vreg = &ctrl->thread[i].vreg[j];
  2196. if (!vreg->ldo_regulator || !vreg->ldo_mode_allowed)
  2197. continue;
  2198. rc = cpr3_regulator_config_vreg_ldo(vreg,
  2199. vdd_floor_volt, vdd_ceiling_volt,
  2200. new_volt, last_volt);
  2201. if (rc)
  2202. return rc;
  2203. }
  2204. }
  2205. return 0;
  2206. }
  2207. /**
  2208. * cpr3_regulator_mem_acc_bhs_used() - determines if mem-acc regulators powered
  2209. * through a BHS are associated with the CPR3 controller or any of
  2210. * the CPR3 regulators it controls.
  2211. * @ctrl: Pointer to the CPR3 controller
  2212. *
  2213. * This function determines if the CPR3 controller or any of its CPR3 regulators
  2214. * need to manage mem-acc regulators that are currently powered through a BHS
  2215. * and whose corner selection is based upon a particular voltage threshold.
  2216. *
  2217. * Return: true or false
  2218. */
  2219. static bool cpr3_regulator_mem_acc_bhs_used(struct cpr3_controller *ctrl)
  2220. {
  2221. struct cpr3_regulator *vreg;
  2222. int i, j;
  2223. if (!ctrl->mem_acc_threshold_volt)
  2224. return false;
  2225. if (ctrl->mem_acc_regulator)
  2226. return true;
  2227. for (i = 0; i < ctrl->thread_count; i++) {
  2228. for (j = 0; j < ctrl->thread[i].vreg_count; j++) {
  2229. vreg = &ctrl->thread[i].vreg[j];
  2230. if (vreg->mem_acc_regulator &&
  2231. (!vreg->ldo_regulator ||
  2232. vreg->ldo_regulator_bypass
  2233. == BHS_MODE))
  2234. return true;
  2235. }
  2236. }
  2237. return false;
  2238. }
  2239. /**
  2240. * cpr3_regulator_config_bhs_mem_acc() - configure the mem-acc regulator
  2241. * settings for hardware blocks currently powered through the BHS.
  2242. * @ctrl: Pointer to the CPR3 controller
  2243. * @new_volt: New voltage in microvolts that VDD supply needs to
  2244. * end up at
  2245. * @last_volt: Pointer to the last known voltage in microvolts for the
  2246. * VDD supply
  2247. * @aggr_corner: Pointer to the CPR3 corner which corresponds to the max
  2248. * corner aggregated from all CPR3 threads managed by the
  2249. * CPR3 controller
  2250. *
  2251. * This function programs the mem-acc regulator corners for CPR3 regulators
  2252. * whose LDO regulators are in bypassed state. The function also handles
  2253. * CPR3 controllers which utilize mem-acc regulators that operate independently
  2254. * from the LDO hardware and that must be programmed when the VDD supply
  2255. * crosses a particular voltage threshold.
  2256. *
  2257. * Return: 0 on success, errno on failure. If the VDD supply voltage is
  2258. * modified, last_volt is updated to reflect the new voltage setpoint.
  2259. */
  2260. static int cpr3_regulator_config_bhs_mem_acc(struct cpr3_controller *ctrl,
  2261. int new_volt, int *last_volt,
  2262. struct cpr3_corner *aggr_corner)
  2263. {
  2264. struct cpr3_regulator *vreg;
  2265. int i, j, rc, mem_acc_corn, safe_volt;
  2266. int mem_acc_volt = ctrl->mem_acc_threshold_volt;
  2267. int ref_volt;
  2268. if (!cpr3_regulator_mem_acc_bhs_used(ctrl))
  2269. return 0;
  2270. ref_volt = ctrl->use_hw_closed_loop ? aggr_corner->floor_volt :
  2271. new_volt;
  2272. if (((*last_volt < mem_acc_volt && mem_acc_volt <= ref_volt) ||
  2273. (*last_volt >= mem_acc_volt && mem_acc_volt > ref_volt))) {
  2274. if (ref_volt < *last_volt)
  2275. safe_volt = max(mem_acc_volt, aggr_corner->last_volt);
  2276. else
  2277. safe_volt = max(mem_acc_volt, *last_volt);
  2278. rc = regulator_set_voltage(ctrl->vdd_regulator, safe_volt,
  2279. new_volt < *last_volt ?
  2280. ctrl->aggr_corner.ceiling_volt :
  2281. new_volt);
  2282. if (rc) {
  2283. cpr3_err(ctrl, "regulator_set_voltage(vdd) == %d failed, rc=%d\n",
  2284. safe_volt, rc);
  2285. return rc;
  2286. }
  2287. *last_volt = safe_volt;
  2288. mem_acc_corn = ref_volt < mem_acc_volt ?
  2289. ctrl->mem_acc_corner_map[CPR3_MEM_ACC_LOW_CORNER] :
  2290. ctrl->mem_acc_corner_map[CPR3_MEM_ACC_HIGH_CORNER];
  2291. if (ctrl->mem_acc_regulator) {
  2292. rc = regulator_set_voltage(ctrl->mem_acc_regulator,
  2293. mem_acc_corn, mem_acc_corn);
  2294. if (rc) {
  2295. cpr3_err(ctrl, "regulator_set_voltage(mem_acc) == %d failed, rc=%d\n",
  2296. mem_acc_corn, rc);
  2297. return rc;
  2298. }
  2299. }
  2300. for (i = 0; i < ctrl->thread_count; i++) {
  2301. for (j = 0; j < ctrl->thread[i].vreg_count; j++) {
  2302. vreg = &ctrl->thread[i].vreg[j];
  2303. if (!vreg->mem_acc_regulator ||
  2304. (vreg->ldo_regulator &&
  2305. vreg->ldo_regulator_bypass
  2306. == LDO_MODE))
  2307. continue;
  2308. rc = regulator_set_voltage(
  2309. vreg->mem_acc_regulator, mem_acc_corn,
  2310. mem_acc_corn);
  2311. if (rc) {
  2312. cpr3_err(vreg, "regulator_set_voltage(mem_acc) == %d failed, rc=%d\n",
  2313. mem_acc_corn, rc);
  2314. return rc;
  2315. }
  2316. }
  2317. }
  2318. }
  2319. return 0;
  2320. }
  2321. /**
  2322. * cpr3_regulator_switch_apm_mode() - switch the mode of the APM controller
  2323. * associated with a given CPR3 controller
  2324. * @ctrl: Pointer to the CPR3 controller
  2325. * @new_volt: New voltage in microvolts that VDD supply needs to
  2326. * end up at
  2327. * @last_volt: Pointer to the last known voltage in microvolts for the
  2328. * VDD supply
  2329. * @aggr_corner: Pointer to the CPR3 corner which corresponds to the max
  2330. * corner aggregated from all CPR3 threads managed by the
  2331. * CPR3 controller
  2332. *
  2333. * This function requests a switch of the APM mode while guaranteeing
  2334. * any LDO regulator hardware requirements are satisfied. The function must
  2335. * be called once it is known a new VDD supply setpoint crosses the APM
  2336. * voltage threshold.
  2337. *
  2338. * Return: 0 on success, errno on failure. If the VDD supply voltage is
  2339. * modified, last_volt is updated to reflect the new voltage setpoint.
  2340. */
  2341. static int cpr3_regulator_switch_apm_mode(struct cpr3_controller *ctrl,
  2342. int new_volt, int *last_volt,
  2343. struct cpr3_corner *aggr_corner)
  2344. {
  2345. struct regulator *vdd = ctrl->vdd_regulator;
  2346. int apm_volt = ctrl->apm_threshold_volt;
  2347. int orig_last_volt = *last_volt;
  2348. int rc;
  2349. rc = regulator_set_voltage(vdd, apm_volt, apm_volt);
  2350. if (rc) {
  2351. cpr3_err(ctrl, "regulator_set_voltage(vdd) == %d failed, rc=%d\n",
  2352. apm_volt, rc);
  2353. return rc;
  2354. }
  2355. *last_volt = apm_volt;
  2356. rc = cpr3_regulator_ldo_apm_prepare(ctrl, new_volt, *last_volt,
  2357. aggr_corner);
  2358. if (rc) {
  2359. cpr3_err(ctrl, "unable to prepare LDO state for APM switch, rc=%d\n",
  2360. rc);
  2361. return rc;
  2362. }
  2363. rc = msm_apm_set_supply(ctrl->apm, new_volt >= apm_volt
  2364. ? ctrl->apm_high_supply : ctrl->apm_low_supply);
  2365. if (rc) {
  2366. cpr3_err(ctrl, "APM switch failed, rc=%d\n", rc);
  2367. /* Roll back the voltage. */
  2368. regulator_set_voltage(vdd, orig_last_volt, INT_MAX);
  2369. *last_volt = orig_last_volt;
  2370. return rc;
  2371. }
  2372. return 0;
  2373. }
  2374. /**
  2375. * cpr3_regulator_config_voltage_crossings() - configure APM and mem-acc
  2376. * settings depending upon a new VDD supply setpoint
  2377. *
  2378. * @ctrl: Pointer to the CPR3 controller
  2379. * @new_volt: New voltage in microvolts that VDD supply needs to
  2380. * end up at
  2381. * @last_volt: Pointer to the last known voltage in microvolts for the
  2382. * VDD supply
  2383. * @aggr_corner: Pointer to the CPR3 corner which corresponds to the max
  2384. * corner aggregated from all CPR3 threads managed by the
  2385. * CPR3 controller
  2386. *
  2387. * This function handles the APM and mem-acc regulator reconfiguration if
  2388. * the new VDD supply voltage will result in crossing their respective voltage
  2389. * thresholds.
  2390. *
  2391. * Return: 0 on success, errno on failure. If the VDD supply voltage is
  2392. * modified, last_volt is updated to reflect the new voltage setpoint.
  2393. */
  2394. static int cpr3_regulator_config_voltage_crossings(struct cpr3_controller *ctrl,
  2395. int new_volt, int *last_volt,
  2396. struct cpr3_corner *aggr_corner)
  2397. {
  2398. bool apm_crossing = false, mem_acc_crossing = false;
  2399. bool mem_acc_bhs_used;
  2400. int apm_volt = ctrl->apm_threshold_volt;
  2401. int mem_acc_volt = ctrl->mem_acc_threshold_volt;
  2402. int ref_volt, rc;
  2403. if (ctrl->apm && apm_volt > 0
  2404. && ((*last_volt < apm_volt && apm_volt <= new_volt)
  2405. || (*last_volt >= apm_volt && apm_volt > new_volt)))
  2406. apm_crossing = true;
  2407. mem_acc_bhs_used = cpr3_regulator_mem_acc_bhs_used(ctrl);
  2408. ref_volt = ctrl->use_hw_closed_loop ? aggr_corner->floor_volt :
  2409. new_volt;
  2410. if (mem_acc_bhs_used &&
  2411. (((*last_volt < mem_acc_volt && mem_acc_volt <= ref_volt) ||
  2412. (*last_volt >= mem_acc_volt && mem_acc_volt > ref_volt))))
  2413. mem_acc_crossing = true;
  2414. if (apm_crossing && mem_acc_crossing) {
  2415. if ((new_volt < *last_volt && apm_volt >= mem_acc_volt) ||
  2416. (new_volt >= *last_volt && apm_volt < mem_acc_volt)) {
  2417. rc = cpr3_regulator_switch_apm_mode(ctrl, new_volt,
  2418. last_volt,
  2419. aggr_corner);
  2420. if (rc) {
  2421. cpr3_err(ctrl, "unable to switch APM mode\n");
  2422. return rc;
  2423. }
  2424. rc = cpr3_regulator_config_bhs_mem_acc(ctrl, new_volt,
  2425. last_volt, aggr_corner);
  2426. if (rc) {
  2427. cpr3_err(ctrl, "unable to configure BHS mem-acc settings\n");
  2428. return rc;
  2429. }
  2430. } else {
  2431. rc = cpr3_regulator_config_bhs_mem_acc(ctrl, new_volt,
  2432. last_volt, aggr_corner);
  2433. if (rc) {
  2434. cpr3_err(ctrl, "unable to configure BHS mem-acc settings\n");
  2435. return rc;
  2436. }
  2437. rc = cpr3_regulator_switch_apm_mode(ctrl, new_volt,
  2438. last_volt,
  2439. aggr_corner);
  2440. if (rc) {
  2441. cpr3_err(ctrl, "unable to switch APM mode\n");
  2442. return rc;
  2443. }
  2444. }
  2445. } else if (apm_crossing) {
  2446. rc = cpr3_regulator_switch_apm_mode(ctrl, new_volt, last_volt,
  2447. aggr_corner);
  2448. if (rc) {
  2449. cpr3_err(ctrl, "unable to switch APM mode\n");
  2450. return rc;
  2451. }
  2452. } else if (mem_acc_crossing) {
  2453. rc = cpr3_regulator_config_bhs_mem_acc(ctrl, new_volt,
  2454. last_volt, aggr_corner);
  2455. if (rc) {
  2456. cpr3_err(ctrl, "unable to configure BHS mem-acc settings\n");
  2457. return rc;
  2458. }
  2459. }
  2460. return 0;
  2461. }
  2462. /**
  2463. * cpr3_regulator_config_mem_acc() - configure the corner of the mem-acc
  2464. * regulator associated with the CPR3 controller
  2465. * @ctrl: Pointer to the CPR3 controller
  2466. * @aggr_corner: Pointer to the CPR3 corner which corresponds to the max
  2467. * corner aggregated from all CPR3 threads managed by the
  2468. * CPR3 controller
  2469. *
  2470. * Return: 0 on success, errno on failure
  2471. */
  2472. static int cpr3_regulator_config_mem_acc(struct cpr3_controller *ctrl,
  2473. struct cpr3_corner *aggr_corner)
  2474. {
  2475. int rc;
  2476. if (ctrl->mem_acc_regulator && aggr_corner->mem_acc_volt) {
  2477. rc = regulator_set_voltage(ctrl->mem_acc_regulator,
  2478. aggr_corner->mem_acc_volt,
  2479. aggr_corner->mem_acc_volt);
  2480. if (rc) {
  2481. cpr3_err(ctrl, "regulator_set_voltage(mem_acc) == %d failed, rc=%d\n",
  2482. aggr_corner->mem_acc_volt, rc);
  2483. return rc;
  2484. }
  2485. }
  2486. return 0;
  2487. }
  2488. /**
  2489. * cpr3_regulator_scale_vdd_voltage() - scale the CPR controlled VDD supply
  2490. * voltage to the new level while satisfying any other hardware
  2491. * requirements
  2492. * @ctrl: Pointer to the CPR3 controller
  2493. * @new_volt: New voltage in microvolts that VDD supply needs to end
  2494. * up at
  2495. * @last_volt: Last known voltage in microvolts for the VDD supply
  2496. * @aggr_corner: Pointer to the CPR3 corner which corresponds to the max
  2497. * corner aggregated from all CPR3 threads managed by the
  2498. * CPR3 controller
  2499. *
  2500. * This function scales the CPR controlled VDD supply voltage from its
  2501. * current level to the new voltage that is specified. If the supply is
  2502. * configured to use the APM and the APM threshold is crossed as a result of
  2503. * the voltage scaling, then this function also stops at the APM threshold,
  2504. * switches the APM source, and finally sets the final new voltage.
  2505. *
  2506. * Return: 0 on success, errno on failure
  2507. */
  2508. static int cpr3_regulator_scale_vdd_voltage(struct cpr3_controller *ctrl,
  2509. int new_volt, int last_volt,
  2510. struct cpr3_corner *aggr_corner)
  2511. {
  2512. struct regulator *vdd = ctrl->vdd_regulator;
  2513. int rc;
  2514. if (new_volt < last_volt) {
  2515. if (ctrl->support_ldo300_vreg) {
  2516. rc = cpr3_regulator_config_mem_acc(ctrl, aggr_corner);
  2517. if (rc)
  2518. return rc;
  2519. }
  2520. /* Decreasing VDD voltage */
  2521. rc = cpr3_regulator_config_ldo(ctrl, aggr_corner->floor_volt,
  2522. ctrl->aggr_corner.ceiling_volt,
  2523. new_volt, last_volt);
  2524. if (rc) {
  2525. cpr3_err(ctrl, "unable to configure LDO state, rc=%d\n",
  2526. rc);
  2527. return rc;
  2528. }
  2529. if (!ctrl->support_ldo300_vreg) {
  2530. rc = cpr3_regulator_config_mem_acc(ctrl, aggr_corner);
  2531. if (rc)
  2532. return rc;
  2533. }
  2534. } else {
  2535. /* Increasing VDD voltage */
  2536. if (ctrl->system_regulator) {
  2537. rc = regulator_set_voltage(ctrl->system_regulator,
  2538. aggr_corner->system_volt, INT_MAX);
  2539. if (rc) {
  2540. cpr3_err(ctrl, "regulator_set_voltage(system) == %d failed, rc=%d\n",
  2541. aggr_corner->system_volt, rc);
  2542. return rc;
  2543. }
  2544. }
  2545. }
  2546. rc = cpr3_regulator_config_voltage_crossings(ctrl, new_volt, &last_volt,
  2547. aggr_corner);
  2548. if (rc) {
  2549. cpr3_err(ctrl, "unable to handle voltage threshold crossing configurations, rc=%d\n",
  2550. rc);
  2551. return rc;
  2552. }
  2553. /*
  2554. * Subtract a small amount from the min_uV parameter so that the
  2555. * set voltage request is not dropped by the framework due to being
  2556. * duplicate. This is needed in order to switch from hardware
  2557. * closed-loop to open-loop successfully.
  2558. */
  2559. rc = regulator_set_voltage(vdd, new_volt - (ctrl->cpr_enabled ? 0 : 1),
  2560. aggr_corner->ceiling_volt);
  2561. if (rc) {
  2562. cpr3_err(ctrl, "regulator_set_voltage(vdd) == %d failed, rc=%d\n",
  2563. new_volt, rc);
  2564. return rc;
  2565. }
  2566. if (new_volt == last_volt && ctrl->supports_hw_closed_loop
  2567. && ctrl->ctrl_type == CPR_CTRL_TYPE_CPR4) {
  2568. /*
  2569. * CPR4 features enforce voltage reprogramming when the last
  2570. * set voltage and new set voltage are same. This way, we can
  2571. * ensure that SAW PMIC STATUS register is updated with newly
  2572. * programmed voltage.
  2573. */
  2574. rc = regulator_sync_voltage(vdd);
  2575. if (rc) {
  2576. cpr3_err(ctrl, "regulator_sync_voltage(vdd) == %d failed, rc=%d\n",
  2577. new_volt, rc);
  2578. return rc;
  2579. }
  2580. }
  2581. if (new_volt >= last_volt) {
  2582. /* Increasing VDD voltage */
  2583. rc = cpr3_regulator_config_ldo(ctrl, aggr_corner->floor_volt,
  2584. aggr_corner->ceiling_volt,
  2585. new_volt, new_volt);
  2586. if (rc) {
  2587. cpr3_err(ctrl, "unable to configure LDO state, rc=%d\n",
  2588. rc);
  2589. return rc;
  2590. }
  2591. rc = cpr3_regulator_config_mem_acc(ctrl, aggr_corner);
  2592. if (rc)
  2593. return rc;
  2594. } else {
  2595. /* Decreasing VDD voltage */
  2596. if (ctrl->system_regulator) {
  2597. rc = regulator_set_voltage(ctrl->system_regulator,
  2598. aggr_corner->system_volt, INT_MAX);
  2599. if (rc) {
  2600. cpr3_err(ctrl, "regulator_set_voltage(system) == %d failed, rc=%d\n",
  2601. aggr_corner->system_volt, rc);
  2602. return rc;
  2603. }
  2604. }
  2605. }
  2606. return 0;
  2607. }
  2608. /**
  2609. * cpr3_regulator_get_dynamic_floor_volt() - returns the current dynamic floor
  2610. * voltage based upon static configurations and the state of all
  2611. * power domains during the last CPR measurement
  2612. * @ctrl: Pointer to the CPR3 controller
  2613. * @reg_last_measurement: Value read from the LAST_MEASUREMENT register
  2614. *
  2615. * When using HW closed-loop, the dynamic floor voltage is always returned
  2616. * regardless of the current state of the power domains.
  2617. *
  2618. * Return: dynamic floor voltage in microvolts or 0 if dynamic floor is not
  2619. * currently required
  2620. */
  2621. static int cpr3_regulator_get_dynamic_floor_volt(struct cpr3_controller *ctrl,
  2622. u32 reg_last_measurement)
  2623. {
  2624. int dynamic_floor_volt = 0;
  2625. struct cpr3_regulator *vreg;
  2626. bool valid, pd_valid;
  2627. u32 bypass_bits;
  2628. int i, j;
  2629. if (!ctrl->supports_hw_closed_loop)
  2630. return 0;
  2631. if (likely(!ctrl->use_hw_closed_loop)) {
  2632. valid = !!(reg_last_measurement & CPR3_LAST_MEASUREMENT_VALID);
  2633. bypass_bits
  2634. = (reg_last_measurement & CPR3_LAST_MEASUREMENT_PD_BYPASS_MASK)
  2635. >> CPR3_LAST_MEASUREMENT_PD_BYPASS_SHIFT;
  2636. } else {
  2637. /*
  2638. * Ensure that the dynamic floor voltage is always used for
  2639. * HW closed-loop since the conditions below cannot be evaluated
  2640. * after each CPR measurement.
  2641. */
  2642. valid = false;
  2643. bypass_bits = 0;
  2644. }
  2645. for (i = 0; i < ctrl->thread_count; i++) {
  2646. for (j = 0; j < ctrl->thread[i].vreg_count; j++) {
  2647. vreg = &ctrl->thread[i].vreg[j];
  2648. if (!vreg->uses_dynamic_floor)
  2649. continue;
  2650. pd_valid = !((bypass_bits & vreg->pd_bypass_mask)
  2651. == vreg->pd_bypass_mask);
  2652. if (!valid || !pd_valid)
  2653. dynamic_floor_volt = max(dynamic_floor_volt,
  2654. vreg->corner[
  2655. vreg->dynamic_floor_corner].last_volt);
  2656. }
  2657. }
  2658. return dynamic_floor_volt;
  2659. }
  2660. /**
  2661. * cpr3_regulator_max_sdelta_diff() - returns the maximum voltage difference in
  2662. * microvolts that can result from different operating conditions
  2663. * for the specified sdelta struct
  2664. * @sdelta: Pointer to the sdelta structure
  2665. * @step_volt: Step size in microvolts between available set
  2666. * points of the VDD supply.
  2667. *
  2668. * Return: voltage difference between the highest and lowest adjustments if
  2669. * sdelta and sdelta->table are valid, else 0.
  2670. */
  2671. static int cpr3_regulator_max_sdelta_diff(const struct cpr4_sdelta *sdelta,
  2672. int step_volt)
  2673. {
  2674. int i, j, index, sdelta_min = INT_MAX, sdelta_max = INT_MIN;
  2675. if (!sdelta || !sdelta->table)
  2676. return 0;
  2677. for (i = 0; i < sdelta->max_core_count; i++) {
  2678. for (j = 0; j < sdelta->temp_band_count; j++) {
  2679. index = i * sdelta->temp_band_count + j;
  2680. sdelta_min = min(sdelta_min, sdelta->table[index]);
  2681. sdelta_max = max(sdelta_max, sdelta->table[index]);
  2682. }
  2683. }
  2684. return (sdelta_max - sdelta_min) * step_volt;
  2685. }
  2686. /**
  2687. * cpr3_regulator_aggregate_sdelta() - check open-loop voltages of current
  2688. * aggregated corner and current corner of a given regulator
  2689. * and adjust the sdelta strucuture data of aggregate corner.
  2690. * @aggr_corner: Pointer to accumulated aggregated corner which
  2691. * is both an input and an output
  2692. * @corner: Pointer to the corner to be aggregated with
  2693. * aggr_corner
  2694. * @step_volt: Step size in microvolts between available set
  2695. * points of the VDD supply.
  2696. *
  2697. * Return: none
  2698. */
  2699. static void cpr3_regulator_aggregate_sdelta(
  2700. struct cpr3_corner *aggr_corner,
  2701. const struct cpr3_corner *corner, int step_volt)
  2702. {
  2703. struct cpr4_sdelta *aggr_sdelta, *sdelta;
  2704. int aggr_core_count, core_count, temp_band_count;
  2705. u32 aggr_index, index;
  2706. int i, j, sdelta_size, cap_steps, adjust_sdelta;
  2707. aggr_sdelta = aggr_corner->sdelta;
  2708. sdelta = corner->sdelta;
  2709. if (aggr_corner->open_loop_volt < corner->open_loop_volt) {
  2710. /*
  2711. * Found the new dominant regulator as its open-loop requirement
  2712. * is higher than previous dominant regulator. Calculate cap
  2713. * voltage to limit the SDELTA values to make sure the runtime
  2714. * (Core-count/temp) adjustments do not violate other
  2715. * regulators' voltage requirements. Use cpr4_sdelta values of
  2716. * new dominant regulator.
  2717. */
  2718. aggr_sdelta->cap_volt = min(aggr_sdelta->cap_volt,
  2719. (corner->open_loop_volt -
  2720. aggr_corner->open_loop_volt));
  2721. /* Clear old data in the sdelta table */
  2722. sdelta_size = aggr_sdelta->max_core_count
  2723. * aggr_sdelta->temp_band_count;
  2724. if (aggr_sdelta->allow_core_count_adj
  2725. || aggr_sdelta->allow_temp_adj)
  2726. memset(aggr_sdelta->table, 0, sdelta_size
  2727. * sizeof(*aggr_sdelta->table));
  2728. if (sdelta->allow_temp_adj || sdelta->allow_core_count_adj) {
  2729. /* Copy new data in sdelta table */
  2730. sdelta_size = sdelta->max_core_count
  2731. * sdelta->temp_band_count;
  2732. if (sdelta->table)
  2733. memcpy(aggr_sdelta->table, sdelta->table,
  2734. sdelta_size * sizeof(*sdelta->table));
  2735. }
  2736. if (sdelta->allow_boost) {
  2737. memcpy(aggr_sdelta->boost_table, sdelta->boost_table,
  2738. sdelta->temp_band_count
  2739. * sizeof(*sdelta->boost_table));
  2740. aggr_sdelta->boost_num_cores = sdelta->boost_num_cores;
  2741. } else if (aggr_sdelta->allow_boost) {
  2742. for (i = 0; i < aggr_sdelta->temp_band_count; i++) {
  2743. adjust_sdelta = (corner->open_loop_volt
  2744. - aggr_corner->open_loop_volt)
  2745. / step_volt;
  2746. aggr_sdelta->boost_table[i] += adjust_sdelta;
  2747. aggr_sdelta->boost_table[i]
  2748. = min(aggr_sdelta->boost_table[i], 0);
  2749. }
  2750. }
  2751. aggr_corner->open_loop_volt = corner->open_loop_volt;
  2752. aggr_sdelta->allow_temp_adj = sdelta->allow_temp_adj;
  2753. aggr_sdelta->allow_core_count_adj
  2754. = sdelta->allow_core_count_adj;
  2755. aggr_sdelta->max_core_count = sdelta->max_core_count;
  2756. aggr_sdelta->temp_band_count = sdelta->temp_band_count;
  2757. } else if (aggr_corner->open_loop_volt > corner->open_loop_volt) {
  2758. /*
  2759. * Adjust the cap voltage if the open-loop requirement of new
  2760. * regulator is the next highest.
  2761. */
  2762. aggr_sdelta->cap_volt = min(aggr_sdelta->cap_volt,
  2763. (aggr_corner->open_loop_volt
  2764. - corner->open_loop_volt));
  2765. if (sdelta->allow_boost) {
  2766. for (i = 0; i < aggr_sdelta->temp_band_count; i++) {
  2767. adjust_sdelta = (aggr_corner->open_loop_volt
  2768. - corner->open_loop_volt)
  2769. / step_volt;
  2770. aggr_sdelta->boost_table[i] =
  2771. sdelta->boost_table[i] + adjust_sdelta;
  2772. aggr_sdelta->boost_table[i]
  2773. = min(aggr_sdelta->boost_table[i], 0);
  2774. }
  2775. aggr_sdelta->boost_num_cores = sdelta->boost_num_cores;
  2776. }
  2777. } else {
  2778. /*
  2779. * Found another dominant regulator with same open-loop
  2780. * requirement. Make cap voltage to '0'. Disable core-count
  2781. * adjustments as we couldn't support for both regulators.
  2782. * Keep enable temp based adjustments if enabled for both
  2783. * regulators and choose mininum margin adjustment values
  2784. * between them.
  2785. */
  2786. aggr_sdelta->cap_volt = 0;
  2787. aggr_sdelta->allow_core_count_adj = false;
  2788. if (aggr_sdelta->allow_temp_adj
  2789. && sdelta->allow_temp_adj) {
  2790. aggr_core_count = aggr_sdelta->max_core_count - 1;
  2791. core_count = sdelta->max_core_count - 1;
  2792. temp_band_count = sdelta->temp_band_count;
  2793. for (j = 0; j < temp_band_count; j++) {
  2794. aggr_index = aggr_core_count * temp_band_count
  2795. + j;
  2796. index = core_count * temp_band_count + j;
  2797. aggr_sdelta->table[aggr_index] =
  2798. min(aggr_sdelta->table[aggr_index],
  2799. sdelta->table[index]);
  2800. }
  2801. } else {
  2802. aggr_sdelta->allow_temp_adj = false;
  2803. }
  2804. if (sdelta->allow_boost) {
  2805. memcpy(aggr_sdelta->boost_table, sdelta->boost_table,
  2806. sdelta->temp_band_count
  2807. * sizeof(*sdelta->boost_table));
  2808. aggr_sdelta->boost_num_cores = sdelta->boost_num_cores;
  2809. }
  2810. }
  2811. /* Keep non-dominant clients boost enable state */
  2812. aggr_sdelta->allow_boost |= sdelta->allow_boost;
  2813. if (aggr_sdelta->allow_boost)
  2814. aggr_sdelta->allow_core_count_adj = false;
  2815. if (aggr_sdelta->cap_volt && !(aggr_sdelta->cap_volt == INT_MAX)) {
  2816. core_count = aggr_sdelta->max_core_count;
  2817. temp_band_count = aggr_sdelta->temp_band_count;
  2818. /*
  2819. * Convert cap voltage from uV to PMIC steps and use to limit
  2820. * sdelta margin adjustments.
  2821. */
  2822. cap_steps = aggr_sdelta->cap_volt / step_volt;
  2823. for (i = 0; i < core_count; i++)
  2824. for (j = 0; j < temp_band_count; j++) {
  2825. index = i * temp_band_count + j;
  2826. aggr_sdelta->table[index] =
  2827. min(aggr_sdelta->table[index],
  2828. cap_steps);
  2829. }
  2830. }
  2831. }
  2832. /**
  2833. * cpr3_regulator_aggregate_corners() - aggregate two corners together
  2834. * @aggr_corner: Pointer to accumulated aggregated corner which
  2835. * is both an input and an output
  2836. * @corner: Pointer to the corner to be aggregated with
  2837. * aggr_corner
  2838. * @aggr_quot: Flag indicating that target quotients should be
  2839. * aggregated as well.
  2840. * @step_volt: Step size in microvolts between available set
  2841. * points of the VDD supply.
  2842. *
  2843. * Return: none
  2844. */
  2845. static void cpr3_regulator_aggregate_corners(struct cpr3_corner *aggr_corner,
  2846. const struct cpr3_corner *corner, bool aggr_quot,
  2847. int step_volt)
  2848. {
  2849. int i;
  2850. aggr_corner->ceiling_volt
  2851. = max(aggr_corner->ceiling_volt, corner->ceiling_volt);
  2852. aggr_corner->floor_volt
  2853. = max(aggr_corner->floor_volt, corner->floor_volt);
  2854. aggr_corner->last_volt
  2855. = max(aggr_corner->last_volt, corner->last_volt);
  2856. aggr_corner->system_volt
  2857. = max(aggr_corner->system_volt, corner->system_volt);
  2858. aggr_corner->mem_acc_volt
  2859. = max(aggr_corner->mem_acc_volt, corner->mem_acc_volt);
  2860. aggr_corner->irq_en |= corner->irq_en;
  2861. aggr_corner->use_open_loop |= corner->use_open_loop;
  2862. aggr_corner->ldo_mode_allowed |= corner->ldo_mode_allowed;
  2863. if (aggr_quot) {
  2864. aggr_corner->ro_mask &= corner->ro_mask;
  2865. for (i = 0; i < CPR3_RO_COUNT; i++)
  2866. aggr_corner->target_quot[i]
  2867. = max(aggr_corner->target_quot[i],
  2868. corner->target_quot[i]);
  2869. }
  2870. if (aggr_corner->sdelta && corner->sdelta
  2871. && (aggr_corner->sdelta->table
  2872. || aggr_corner->sdelta->boost_table)) {
  2873. cpr3_regulator_aggregate_sdelta(aggr_corner, corner, step_volt);
  2874. } else {
  2875. aggr_corner->open_loop_volt
  2876. = max(aggr_corner->open_loop_volt,
  2877. corner->open_loop_volt);
  2878. }
  2879. }
  2880. /**
  2881. * cpr3_regulator_update_ctrl_state() - update the state of the CPR controller
  2882. * to reflect the corners used by all CPR3 regulators as well as
  2883. * the CPR operating mode
  2884. * @ctrl: Pointer to the CPR3 controller
  2885. *
  2886. * This function aggregates the CPR parameters for all CPR3 regulators
  2887. * associated with the VDD supply. Upon success, it sets the aggregated last
  2888. * known good voltage.
  2889. *
  2890. * The VDD supply voltage will not be physically configured unless this
  2891. * condition is met by at least one of the regulators of the controller:
  2892. * regulator->vreg_enabled == true &&
  2893. * regulator->current_corner != CPR3_REGULATOR_CORNER_INVALID
  2894. *
  2895. * CPR registers for the controller and each thread are updated as long as
  2896. * ctrl->cpr_enabled == true.
  2897. *
  2898. * Note, CPR3 controller lock must be held by the caller.
  2899. *
  2900. * Return: 0 on success, errno on failure
  2901. */
  2902. static int _cpr3_regulator_update_ctrl_state(struct cpr3_controller *ctrl)
  2903. {
  2904. struct cpr3_corner aggr_corner = {};
  2905. struct cpr3_thread *thread;
  2906. struct cpr3_regulator *vreg;
  2907. struct cpr4_sdelta *sdelta;
  2908. bool valid = false;
  2909. bool thread_valid;
  2910. int i, j, rc, new_volt, vdd_volt, dynamic_floor_volt;
  2911. int last_corner_volt = 0;
  2912. u32 reg_last_measurement = 0, sdelta_size;
  2913. int *sdelta_table, *boost_table;
  2914. if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPR4) {
  2915. rc = cpr3_ctrl_clear_cpr4_config(ctrl);
  2916. if (rc) {
  2917. cpr3_err(ctrl, "failed to clear CPR4 configuration,rc=%d\n",
  2918. rc);
  2919. return rc;
  2920. }
  2921. }
  2922. cpr3_ctrl_loop_disable(ctrl);
  2923. vdd_volt = regulator_get_voltage(ctrl->vdd_regulator);
  2924. if (vdd_volt < 0) {
  2925. cpr3_err(ctrl, "regulator_get_voltage(vdd) failed, rc=%d\n",
  2926. vdd_volt);
  2927. return vdd_volt;
  2928. }
  2929. if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPR4) {
  2930. /*
  2931. * Save aggregated corner open-loop voltage which was programmed
  2932. * during last corner switch which is used when programming new
  2933. * aggregated corner open-loop voltage.
  2934. */
  2935. last_corner_volt = ctrl->aggr_corner.open_loop_volt;
  2936. }
  2937. if (ctrl->cpr_enabled && ctrl->use_hw_closed_loop &&
  2938. ctrl->ctrl_type == CPR_CTRL_TYPE_CPR3)
  2939. reg_last_measurement
  2940. = cpr3_read(ctrl, CPR3_REG_LAST_MEASUREMENT);
  2941. aggr_corner.sdelta = ctrl->aggr_corner.sdelta;
  2942. if (aggr_corner.sdelta) {
  2943. sdelta = aggr_corner.sdelta;
  2944. sdelta_table = sdelta->table;
  2945. if (sdelta_table) {
  2946. sdelta_size = sdelta->max_core_count *
  2947. sdelta->temp_band_count;
  2948. memset(sdelta_table, 0, sdelta_size
  2949. * sizeof(*sdelta_table));
  2950. }
  2951. boost_table = sdelta->boost_table;
  2952. if (boost_table)
  2953. memset(boost_table, 0, sdelta->temp_band_count
  2954. * sizeof(*boost_table));
  2955. memset(sdelta, 0, sizeof(*sdelta));
  2956. sdelta->table = sdelta_table;
  2957. sdelta->cap_volt = INT_MAX;
  2958. sdelta->boost_table = boost_table;
  2959. }
  2960. /* Aggregate the requests of all threads */
  2961. for (i = 0; i < ctrl->thread_count; i++) {
  2962. thread = &ctrl->thread[i];
  2963. thread_valid = false;
  2964. sdelta = thread->aggr_corner.sdelta;
  2965. if (sdelta) {
  2966. sdelta_table = sdelta->table;
  2967. if (sdelta_table) {
  2968. sdelta_size = sdelta->max_core_count *
  2969. sdelta->temp_band_count;
  2970. memset(sdelta_table, 0, sdelta_size
  2971. * sizeof(*sdelta_table));
  2972. }
  2973. boost_table = sdelta->boost_table;
  2974. if (boost_table)
  2975. memset(boost_table, 0, sdelta->temp_band_count
  2976. * sizeof(*boost_table));
  2977. memset(sdelta, 0, sizeof(*sdelta));
  2978. sdelta->table = sdelta_table;
  2979. sdelta->cap_volt = INT_MAX;
  2980. sdelta->boost_table = boost_table;
  2981. }
  2982. memset(&thread->aggr_corner, 0, sizeof(thread->aggr_corner));
  2983. thread->aggr_corner.sdelta = sdelta;
  2984. thread->aggr_corner.ro_mask = CPR3_RO_MASK;
  2985. for (j = 0; j < thread->vreg_count; j++) {
  2986. vreg = &thread->vreg[j];
  2987. if (ctrl->cpr_enabled && ctrl->use_hw_closed_loop)
  2988. cpr3_update_vreg_closed_loop_volt(vreg,
  2989. vdd_volt, reg_last_measurement);
  2990. if (!vreg->vreg_enabled
  2991. || vreg->current_corner
  2992. == CPR3_REGULATOR_CORNER_INVALID) {
  2993. /* Cannot participate in aggregation. */
  2994. vreg->aggregated = false;
  2995. continue;
  2996. } else {
  2997. vreg->aggregated = true;
  2998. thread_valid = true;
  2999. }
  3000. cpr3_regulator_aggregate_corners(&thread->aggr_corner,
  3001. &vreg->corner[vreg->current_corner],
  3002. true, ctrl->step_volt);
  3003. }
  3004. valid |= thread_valid;
  3005. if (thread_valid)
  3006. cpr3_regulator_aggregate_corners(&aggr_corner,
  3007. &thread->aggr_corner,
  3008. false, ctrl->step_volt);
  3009. }
  3010. if (valid && ctrl->cpr_allowed_hw && ctrl->cpr_allowed_sw) {
  3011. rc = cpr3_closed_loop_enable(ctrl);
  3012. if (rc) {
  3013. cpr3_err(ctrl, "could not enable CPR, rc=%d\n", rc);
  3014. return rc;
  3015. }
  3016. } else {
  3017. rc = cpr3_closed_loop_disable(ctrl);
  3018. if (rc) {
  3019. cpr3_err(ctrl, "could not disable CPR, rc=%d\n", rc);
  3020. return rc;
  3021. }
  3022. }
  3023. /* No threads are enabled with a valid corner so exit. */
  3024. if (!valid)
  3025. return 0;
  3026. /*
  3027. * When using CPR hardware closed-loop, the voltage may vary anywhere
  3028. * between the floor and ceiling voltage without software notification.
  3029. * Therefore, it is required that the floor to ceiling range for the
  3030. * aggregated corner not intersect the APM threshold voltage. Adjust
  3031. * the floor to ceiling range if this requirement is violated.
  3032. *
  3033. * The following algorithm is applied in the case that
  3034. * floor < threshold <= ceiling:
  3035. * if open_loop >= threshold - adj, then floor = threshold
  3036. * else ceiling = threshold - step
  3037. * where adj = an adjustment factor to ensure sufficient voltage margin
  3038. * and step = VDD output step size
  3039. *
  3040. * The open-loop and last known voltages are also bounded by the new
  3041. * floor or ceiling value as needed.
  3042. */
  3043. if (ctrl->use_hw_closed_loop
  3044. && aggr_corner.ceiling_volt >= ctrl->apm_threshold_volt
  3045. && aggr_corner.floor_volt < ctrl->apm_threshold_volt) {
  3046. if (aggr_corner.open_loop_volt
  3047. >= ctrl->apm_threshold_volt - ctrl->apm_adj_volt)
  3048. aggr_corner.floor_volt = ctrl->apm_threshold_volt;
  3049. else
  3050. aggr_corner.ceiling_volt
  3051. = ctrl->apm_threshold_volt - ctrl->step_volt;
  3052. aggr_corner.last_volt
  3053. = max(aggr_corner.last_volt, aggr_corner.floor_volt);
  3054. aggr_corner.last_volt
  3055. = min(aggr_corner.last_volt, aggr_corner.ceiling_volt);
  3056. aggr_corner.open_loop_volt
  3057. = max(aggr_corner.open_loop_volt, aggr_corner.floor_volt);
  3058. aggr_corner.open_loop_volt
  3059. = min(aggr_corner.open_loop_volt, aggr_corner.ceiling_volt);
  3060. }
  3061. if (ctrl->use_hw_closed_loop
  3062. && aggr_corner.ceiling_volt >= ctrl->mem_acc_threshold_volt
  3063. && aggr_corner.floor_volt < ctrl->mem_acc_threshold_volt) {
  3064. aggr_corner.floor_volt = ctrl->mem_acc_threshold_volt;
  3065. aggr_corner.last_volt = max(aggr_corner.last_volt,
  3066. aggr_corner.floor_volt);
  3067. aggr_corner.open_loop_volt = max(aggr_corner.open_loop_volt,
  3068. aggr_corner.floor_volt);
  3069. }
  3070. if (ctrl->use_hw_closed_loop) {
  3071. dynamic_floor_volt
  3072. = cpr3_regulator_get_dynamic_floor_volt(ctrl,
  3073. reg_last_measurement);
  3074. if (aggr_corner.floor_volt < dynamic_floor_volt) {
  3075. aggr_corner.floor_volt = dynamic_floor_volt;
  3076. aggr_corner.last_volt = max(aggr_corner.last_volt,
  3077. aggr_corner.floor_volt);
  3078. aggr_corner.open_loop_volt
  3079. = max(aggr_corner.open_loop_volt,
  3080. aggr_corner.floor_volt);
  3081. aggr_corner.ceiling_volt = max(aggr_corner.ceiling_volt,
  3082. aggr_corner.floor_volt);
  3083. }
  3084. }
  3085. if (ctrl->cpr_enabled && ctrl->last_corner_was_closed_loop) {
  3086. /*
  3087. * Always program open-loop voltage for CPR4 controllers which
  3088. * support hardware closed-loop. Storing the last closed loop
  3089. * voltage in corner structure can still help with debugging.
  3090. */
  3091. if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPR3)
  3092. new_volt = aggr_corner.last_volt;
  3093. else if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPR4
  3094. && ctrl->supports_hw_closed_loop)
  3095. new_volt = aggr_corner.open_loop_volt;
  3096. else
  3097. new_volt = min(aggr_corner.last_volt +
  3098. cpr3_regulator_max_sdelta_diff(aggr_corner.sdelta,
  3099. ctrl->step_volt),
  3100. aggr_corner.ceiling_volt);
  3101. } else {
  3102. new_volt = aggr_corner.open_loop_volt;
  3103. aggr_corner.last_volt = aggr_corner.open_loop_volt;
  3104. }
  3105. if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPR4
  3106. && ctrl->supports_hw_closed_loop) {
  3107. /*
  3108. * Store last aggregated corner open-loop voltage in vdd_volt
  3109. * which is used when programming current aggregated corner
  3110. * required voltage.
  3111. */
  3112. vdd_volt = last_corner_volt;
  3113. }
  3114. cpr3_debug(ctrl, "setting new voltage=%d uV\n", new_volt);
  3115. rc = cpr3_regulator_scale_vdd_voltage(ctrl, new_volt,
  3116. vdd_volt, &aggr_corner);
  3117. if (rc) {
  3118. cpr3_err(ctrl, "vdd voltage scaling failed, rc=%d\n", rc);
  3119. return rc;
  3120. }
  3121. /* Only update registers if CPR is enabled. */
  3122. if (ctrl->cpr_enabled) {
  3123. if (ctrl->use_hw_closed_loop) {
  3124. /* Hardware closed-loop */
  3125. /* Set ceiling and floor limits in hardware */
  3126. rc = regulator_set_voltage(ctrl->vdd_limit_regulator,
  3127. aggr_corner.floor_volt,
  3128. aggr_corner.ceiling_volt);
  3129. if (rc) {
  3130. cpr3_err(ctrl, "could not configure HW closed-loop voltage limits, rc=%d\n",
  3131. rc);
  3132. return rc;
  3133. }
  3134. } else {
  3135. /* Software closed-loop */
  3136. /*
  3137. * Disable UP or DOWN interrupts when at ceiling or
  3138. * floor respectively.
  3139. */
  3140. if (new_volt == aggr_corner.floor_volt)
  3141. aggr_corner.irq_en &= ~CPR3_IRQ_DOWN;
  3142. if (new_volt == aggr_corner.ceiling_volt)
  3143. aggr_corner.irq_en &= ~CPR3_IRQ_UP;
  3144. cpr3_write(ctrl, CPR3_REG_IRQ_CLEAR,
  3145. CPR3_IRQ_UP | CPR3_IRQ_DOWN);
  3146. cpr3_write(ctrl, CPR3_REG_IRQ_EN, aggr_corner.irq_en);
  3147. }
  3148. for (i = 0; i < ctrl->thread_count; i++) {
  3149. cpr3_regulator_set_target_quot(&ctrl->thread[i]);
  3150. for (j = 0; j < ctrl->thread[i].vreg_count; j++) {
  3151. vreg = &ctrl->thread[i].vreg[j];
  3152. if (vreg->vreg_enabled)
  3153. vreg->last_closed_loop_corner
  3154. = vreg->current_corner;
  3155. }
  3156. }
  3157. if (ctrl->proc_clock_throttle) {
  3158. if (aggr_corner.ceiling_volt > aggr_corner.floor_volt
  3159. && (ctrl->use_hw_closed_loop
  3160. || new_volt < aggr_corner.ceiling_volt))
  3161. cpr3_write(ctrl, CPR3_REG_PD_THROTTLE,
  3162. ctrl->proc_clock_throttle);
  3163. else
  3164. cpr3_write(ctrl, CPR3_REG_PD_THROTTLE,
  3165. CPR3_PD_THROTTLE_DISABLE);
  3166. }
  3167. /*
  3168. * Ensure that all CPR register writes complete before
  3169. * re-enabling CPR loop operation.
  3170. */
  3171. wmb();
  3172. } else if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPR4
  3173. && ctrl->vdd_limit_regulator) {
  3174. /* Set ceiling and floor limits in hardware */
  3175. rc = regulator_set_voltage(ctrl->vdd_limit_regulator,
  3176. aggr_corner.floor_volt,
  3177. aggr_corner.ceiling_volt);
  3178. if (rc) {
  3179. cpr3_err(ctrl, "could not configure HW closed-loop voltage limits, rc=%d\n",
  3180. rc);
  3181. return rc;
  3182. }
  3183. }
  3184. ctrl->aggr_corner = aggr_corner;
  3185. if (ctrl->allow_core_count_adj || ctrl->allow_temp_adj
  3186. || ctrl->allow_boost) {
  3187. rc = cpr3_controller_program_sdelta(ctrl);
  3188. if (rc) {
  3189. cpr3_err(ctrl, "failed to program sdelta, rc=%d\n", rc);
  3190. return rc;
  3191. }
  3192. }
  3193. /*
  3194. * Only enable the CPR controller if it is possible to set more than
  3195. * one vdd-supply voltage.
  3196. */
  3197. if (aggr_corner.ceiling_volt > aggr_corner.floor_volt &&
  3198. !aggr_corner.use_open_loop)
  3199. cpr3_ctrl_loop_enable(ctrl);
  3200. ctrl->last_corner_was_closed_loop = ctrl->cpr_enabled;
  3201. cpr3_debug(ctrl, "CPR configuration updated\n");
  3202. return 0;
  3203. }
  3204. /**
  3205. * cpr3_regulator_wait_for_idle() - wait for the CPR controller to no longer be
  3206. * busy
  3207. * @ctrl: Pointer to the CPR3 controller
  3208. * @max_wait_ns: Max wait time in nanoseconds
  3209. *
  3210. * Return: 0 on success or -ETIMEDOUT if the controller was still busy after
  3211. * the maximum delay time
  3212. */
  3213. static int cpr3_regulator_wait_for_idle(struct cpr3_controller *ctrl,
  3214. s64 max_wait_ns)
  3215. {
  3216. ktime_t start, end;
  3217. s64 time_ns;
  3218. u32 reg;
  3219. /*
  3220. * Ensure that all previous CPR register writes have completed before
  3221. * checking the status register.
  3222. */
  3223. mb();
  3224. start = ktime_get();
  3225. do {
  3226. end = ktime_get();
  3227. time_ns = ktime_to_ns(ktime_sub(end, start));
  3228. if (time_ns > max_wait_ns) {
  3229. cpr3_err(ctrl, "CPR controller still busy after %lld us\n",
  3230. div_s64(time_ns, 1000));
  3231. return -ETIMEDOUT;
  3232. }
  3233. usleep_range(50, 100);
  3234. reg = cpr3_read(ctrl, CPR3_REG_CPR_STATUS);
  3235. } while (reg & CPR3_CPR_STATUS_BUSY_MASK);
  3236. return 0;
  3237. }
  3238. /**
  3239. * cmp_int() - int comparison function to be passed into the sort() function
  3240. * which leads to ascending sorting
  3241. * @a: First int value
  3242. * @b: Second int value
  3243. *
  3244. * Return: >0 if a > b, 0 if a == b, <0 if a < b
  3245. */
  3246. static int cmp_int(const void *a, const void *b)
  3247. {
  3248. return *(int *)a - *(int *)b;
  3249. }
  3250. /**
  3251. * cpr3_regulator_measure_aging() - measure the quotient difference for the
  3252. * specified CPR aging sensor
  3253. * @ctrl: Pointer to the CPR3 controller
  3254. * @aging_sensor: Aging sensor to measure
  3255. *
  3256. * Note that vdd-supply must be configured to the aging reference voltage before
  3257. * calling this function.
  3258. *
  3259. * Return: 0 on success, errno on failure
  3260. */
  3261. static int cpr3_regulator_measure_aging(struct cpr3_controller *ctrl,
  3262. struct cpr3_aging_sensor_info *aging_sensor)
  3263. {
  3264. u32 mask, reg, result, quot_min, quot_max, sel_min, sel_max;
  3265. u32 quot_min_scaled, quot_max_scaled;
  3266. u32 gcnt, gcnt_ref, gcnt0_restore, gcnt1_restore, irq_restore;
  3267. u32 ro_mask_restore, cont_dly_restore, up_down_dly_restore = 0;
  3268. int quot_delta, quot_delta_scaled, quot_delta_scaled_sum;
  3269. int *quot_delta_results;
  3270. int rc, rc2, i, aging_measurement_count, filtered_count;
  3271. bool is_aging_measurement;
  3272. quot_delta_results = kcalloc(CPR3_AGING_MEASUREMENT_ITERATIONS,
  3273. sizeof(*quot_delta_results), GFP_KERNEL);
  3274. if (!quot_delta_results)
  3275. return -ENOMEM;
  3276. if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPR4) {
  3277. rc = cpr3_ctrl_clear_cpr4_config(ctrl);
  3278. if (rc) {
  3279. cpr3_err(ctrl, "failed to clear CPR4 configuration,rc=%d\n",
  3280. rc);
  3281. kfree(quot_delta_results);
  3282. return rc;
  3283. }
  3284. }
  3285. cpr3_ctrl_loop_disable(ctrl);
  3286. /* Enable up, down, and mid CPR interrupts */
  3287. irq_restore = cpr3_read(ctrl, CPR3_REG_IRQ_EN);
  3288. cpr3_write(ctrl, CPR3_REG_IRQ_EN,
  3289. CPR3_IRQ_UP | CPR3_IRQ_DOWN | CPR3_IRQ_MID);
  3290. /* Ensure that the aging sensor is assigned to CPR thread 0 */
  3291. cpr3_write(ctrl, CPR3_REG_SENSOR_OWNER(aging_sensor->sensor_id), 0);
  3292. /* Switch from HW to SW closed-loop if necessary */
  3293. if (ctrl->supports_hw_closed_loop) {
  3294. if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPR4 ||
  3295. ctrl->ctrl_type == CPR_CTRL_TYPE_CPRH) {
  3296. cpr3_masked_write(ctrl, CPR4_REG_MARGIN_ADJ_CTL,
  3297. CPR4_MARGIN_ADJ_CTL_HW_CLOSED_LOOP_EN_MASK,
  3298. CPR4_MARGIN_ADJ_CTL_HW_CLOSED_LOOP_DISABLE);
  3299. } else if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPR3) {
  3300. cpr3_write(ctrl, CPR3_REG_HW_CLOSED_LOOP,
  3301. CPR3_HW_CLOSED_LOOP_DISABLE);
  3302. }
  3303. }
  3304. /* Configure the GCNT for RO0 and RO1 that are used for aging */
  3305. gcnt0_restore = cpr3_read(ctrl, CPR3_REG_GCNT(0));
  3306. gcnt1_restore = cpr3_read(ctrl, CPR3_REG_GCNT(1));
  3307. gcnt_ref = cpr3_regulator_get_gcnt(ctrl);
  3308. gcnt = gcnt_ref * 3 / 2;
  3309. cpr3_write(ctrl, CPR3_REG_GCNT(0), gcnt);
  3310. cpr3_write(ctrl, CPR3_REG_GCNT(1), gcnt);
  3311. /* Unmask all RO's */
  3312. ro_mask_restore = cpr3_read(ctrl, CPR3_REG_RO_MASK(0));
  3313. cpr3_write(ctrl, CPR3_REG_RO_MASK(0), 0);
  3314. /*
  3315. * Mask all sensors except for the one to measure and bypass all
  3316. * sensors in collapsible domains.
  3317. */
  3318. for (i = 0; i <= ctrl->sensor_count / 32; i++) {
  3319. mask = GENMASK(min(31, ctrl->sensor_count - i * 32), 0);
  3320. if (aging_sensor->sensor_id / 32 >= i
  3321. && aging_sensor->sensor_id / 32 < (i + 1))
  3322. mask &= ~BIT(aging_sensor->sensor_id % 32);
  3323. cpr3_write(ctrl, CPR3_REG_SENSOR_MASK_WRITE_BANK(i), mask);
  3324. cpr3_write(ctrl, CPR3_REG_SENSOR_BYPASS_WRITE_BANK(i),
  3325. aging_sensor->bypass_mask[i]);
  3326. }
  3327. /* Set CPR loop delays to 0 us */
  3328. if (ctrl->supports_hw_closed_loop
  3329. && ctrl->ctrl_type == CPR_CTRL_TYPE_CPR3) {
  3330. cont_dly_restore = cpr3_read(ctrl, CPR3_REG_CPR_TIMER_MID_CONT);
  3331. up_down_dly_restore = cpr3_read(ctrl,
  3332. CPR3_REG_CPR_TIMER_UP_DN_CONT);
  3333. cpr3_write(ctrl, CPR3_REG_CPR_TIMER_MID_CONT, 0);
  3334. cpr3_write(ctrl, CPR3_REG_CPR_TIMER_UP_DN_CONT, 0);
  3335. } else {
  3336. cont_dly_restore = cpr3_read(ctrl,
  3337. CPR3_REG_CPR_TIMER_AUTO_CONT);
  3338. cpr3_write(ctrl, CPR3_REG_CPR_TIMER_AUTO_CONT, 0);
  3339. }
  3340. /* Set count mode to all-at-once min with no repeat */
  3341. cpr3_masked_write(ctrl, CPR3_REG_CPR_CTL,
  3342. CPR3_CPR_CTL_COUNT_MODE_MASK | CPR3_CPR_CTL_COUNT_REPEAT_MASK,
  3343. CPR3_CPR_CTL_COUNT_MODE_ALL_AT_ONCE_MIN
  3344. << CPR3_CPR_CTL_COUNT_MODE_SHIFT);
  3345. cpr3_ctrl_loop_enable(ctrl);
  3346. rc = cpr3_regulator_wait_for_idle(ctrl,
  3347. CPR3_AGING_MEASUREMENT_TIMEOUT_NS);
  3348. if (rc)
  3349. goto cleanup;
  3350. /* Set count mode to all-at-once aging */
  3351. cpr3_masked_write(ctrl, CPR3_REG_CPR_CTL, CPR3_CPR_CTL_COUNT_MODE_MASK,
  3352. CPR3_CPR_CTL_COUNT_MODE_ALL_AT_ONCE_AGE
  3353. << CPR3_CPR_CTL_COUNT_MODE_SHIFT);
  3354. aging_measurement_count = 0;
  3355. for (i = 0; i < CPR3_AGING_MEASUREMENT_ITERATIONS; i++) {
  3356. /* Send CONT_NACK */
  3357. cpr3_write(ctrl, CPR3_REG_CONT_CMD, CPR3_CONT_CMD_NACK);
  3358. rc = cpr3_regulator_wait_for_idle(ctrl,
  3359. CPR3_AGING_MEASUREMENT_TIMEOUT_NS);
  3360. if (rc)
  3361. goto cleanup;
  3362. /* Check for PAGE_IS_AGE flag in status register */
  3363. reg = cpr3_read(ctrl, CPR3_REG_CPR_STATUS);
  3364. is_aging_measurement
  3365. = reg & CPR3_CPR_STATUS_AGING_MEASUREMENT_MASK;
  3366. /* Read CPR measurement results */
  3367. result = cpr3_read(ctrl, CPR3_REG_RESULT1(0));
  3368. quot_min = (result & CPR3_RESULT1_QUOT_MIN_MASK)
  3369. >> CPR3_RESULT1_QUOT_MIN_SHIFT;
  3370. quot_max = (result & CPR3_RESULT1_QUOT_MAX_MASK)
  3371. >> CPR3_RESULT1_QUOT_MAX_SHIFT;
  3372. sel_min = (result & CPR3_RESULT1_RO_MIN_MASK)
  3373. >> CPR3_RESULT1_RO_MIN_SHIFT;
  3374. sel_max = (result & CPR3_RESULT1_RO_MAX_MASK)
  3375. >> CPR3_RESULT1_RO_MAX_SHIFT;
  3376. /*
  3377. * Scale the quotients so that they are equivalent to the fused
  3378. * values. This accounts for the difference in measurement
  3379. * interval times.
  3380. */
  3381. quot_min_scaled = quot_min * (gcnt_ref + 1) / (gcnt + 1);
  3382. quot_max_scaled = quot_max * (gcnt_ref + 1) / (gcnt + 1);
  3383. if (sel_max == 1) {
  3384. quot_delta = quot_max - quot_min;
  3385. quot_delta_scaled = quot_max_scaled - quot_min_scaled;
  3386. } else {
  3387. quot_delta = quot_min - quot_max;
  3388. quot_delta_scaled = quot_min_scaled - quot_max_scaled;
  3389. }
  3390. if (is_aging_measurement)
  3391. quot_delta_results[aging_measurement_count++]
  3392. = quot_delta_scaled;
  3393. cpr3_debug(ctrl, "aging results: page_is_age=%u, sel_min=%u, sel_max=%u, quot_min=%u, quot_max=%u, quot_delta=%d, quot_min_scaled=%u, quot_max_scaled=%u, quot_delta_scaled=%d\n",
  3394. is_aging_measurement, sel_min, sel_max, quot_min,
  3395. quot_max, quot_delta, quot_min_scaled, quot_max_scaled,
  3396. quot_delta_scaled);
  3397. }
  3398. filtered_count
  3399. = aging_measurement_count - CPR3_AGING_MEASUREMENT_FILTER * 2;
  3400. if (filtered_count > 0) {
  3401. sort(quot_delta_results, aging_measurement_count,
  3402. sizeof(*quot_delta_results), cmp_int, NULL);
  3403. quot_delta_scaled_sum = 0;
  3404. for (i = 0; i < filtered_count; i++)
  3405. quot_delta_scaled_sum
  3406. += quot_delta_results[i
  3407. + CPR3_AGING_MEASUREMENT_FILTER];
  3408. aging_sensor->measured_quot_diff
  3409. = quot_delta_scaled_sum / filtered_count;
  3410. cpr3_info(ctrl, "average quotient delta=%d (count=%d)\n",
  3411. aging_sensor->measured_quot_diff,
  3412. filtered_count);
  3413. } else {
  3414. cpr3_err(ctrl, "%d aging measurements completed after %d iterations\n",
  3415. aging_measurement_count,
  3416. CPR3_AGING_MEASUREMENT_ITERATIONS);
  3417. rc = -EBUSY;
  3418. }
  3419. cleanup:
  3420. kfree(quot_delta_results);
  3421. if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPR4) {
  3422. rc2 = cpr3_ctrl_clear_cpr4_config(ctrl);
  3423. if (rc2) {
  3424. cpr3_err(ctrl, "failed to clear CPR4 configuration,rc=%d\n",
  3425. rc2);
  3426. rc = rc2;
  3427. }
  3428. }
  3429. cpr3_ctrl_loop_disable(ctrl);
  3430. cpr3_write(ctrl, CPR3_REG_IRQ_EN, irq_restore);
  3431. cpr3_write(ctrl, CPR3_REG_RO_MASK(0), ro_mask_restore);
  3432. cpr3_write(ctrl, CPR3_REG_GCNT(0), gcnt0_restore);
  3433. cpr3_write(ctrl, CPR3_REG_GCNT(1), gcnt1_restore);
  3434. if (ctrl->supports_hw_closed_loop
  3435. && ctrl->ctrl_type == CPR_CTRL_TYPE_CPR3) {
  3436. cpr3_write(ctrl, CPR3_REG_CPR_TIMER_MID_CONT, cont_dly_restore);
  3437. cpr3_write(ctrl, CPR3_REG_CPR_TIMER_UP_DN_CONT,
  3438. up_down_dly_restore);
  3439. } else {
  3440. cpr3_write(ctrl, CPR3_REG_CPR_TIMER_AUTO_CONT,
  3441. cont_dly_restore);
  3442. }
  3443. for (i = 0; i <= ctrl->sensor_count / 32; i++) {
  3444. cpr3_write(ctrl, CPR3_REG_SENSOR_MASK_WRITE_BANK(i), 0);
  3445. cpr3_write(ctrl, CPR3_REG_SENSOR_BYPASS_WRITE_BANK(i), 0);
  3446. }
  3447. cpr3_masked_write(ctrl, CPR3_REG_CPR_CTL,
  3448. CPR3_CPR_CTL_COUNT_MODE_MASK | CPR3_CPR_CTL_COUNT_REPEAT_MASK,
  3449. (ctrl->count_mode << CPR3_CPR_CTL_COUNT_MODE_SHIFT)
  3450. | (ctrl->count_repeat << CPR3_CPR_CTL_COUNT_REPEAT_SHIFT));
  3451. cpr3_write(ctrl, CPR3_REG_SENSOR_OWNER(aging_sensor->sensor_id),
  3452. ctrl->sensor_owner[aging_sensor->sensor_id]);
  3453. cpr3_write(ctrl, CPR3_REG_IRQ_CLEAR,
  3454. CPR3_IRQ_UP | CPR3_IRQ_DOWN | CPR3_IRQ_MID);
  3455. if (ctrl->supports_hw_closed_loop) {
  3456. if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPR4 ||
  3457. ctrl->ctrl_type == CPR_CTRL_TYPE_CPRH) {
  3458. cpr3_masked_write(ctrl, CPR4_REG_MARGIN_ADJ_CTL,
  3459. CPR4_MARGIN_ADJ_CTL_HW_CLOSED_LOOP_EN_MASK,
  3460. ctrl->use_hw_closed_loop
  3461. ? CPR4_MARGIN_ADJ_CTL_HW_CLOSED_LOOP_ENABLE
  3462. : CPR4_MARGIN_ADJ_CTL_HW_CLOSED_LOOP_DISABLE);
  3463. } else if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPR3) {
  3464. cpr3_write(ctrl, CPR3_REG_HW_CLOSED_LOOP,
  3465. ctrl->use_hw_closed_loop
  3466. ? CPR3_HW_CLOSED_LOOP_ENABLE
  3467. : CPR3_HW_CLOSED_LOOP_DISABLE);
  3468. }
  3469. }
  3470. return rc;
  3471. }
  3472. /**
  3473. * cpr3_regulator_readjust_volt_and_quot() - readjust the target quotients as
  3474. * well as the floor, ceiling, and open-loop voltages for the
  3475. * regulator by removing the old adjustment and adding the new one
  3476. * @vreg: Pointer to the CPR3 regulator
  3477. * @old_adjust_volt: Old aging adjustment voltage in microvolts
  3478. * @new_adjust_volt: New aging adjustment voltage in microvolts
  3479. *
  3480. * Also reset the cached closed loop voltage (last_volt) to equal the open-loop
  3481. * voltage for each corner.
  3482. *
  3483. * Return: None
  3484. */
  3485. static void cpr3_regulator_readjust_volt_and_quot(struct cpr3_regulator *vreg,
  3486. int old_adjust_volt, int new_adjust_volt)
  3487. {
  3488. unsigned long long temp;
  3489. int i, j, old_volt, new_volt, rounded_volt;
  3490. if (!vreg->aging_allowed)
  3491. return;
  3492. for (i = 0; i < vreg->corner_count; i++) {
  3493. temp = (unsigned long long)old_adjust_volt
  3494. * (unsigned long long)vreg->corner[i].aging_derate;
  3495. do_div(temp, 1000);
  3496. old_volt = temp;
  3497. temp = (unsigned long long)new_adjust_volt
  3498. * (unsigned long long)vreg->corner[i].aging_derate;
  3499. do_div(temp, 1000);
  3500. new_volt = temp;
  3501. old_volt = min(vreg->aging_max_adjust_volt, old_volt);
  3502. new_volt = min(vreg->aging_max_adjust_volt, new_volt);
  3503. for (j = 0; j < CPR3_RO_COUNT; j++) {
  3504. if (vreg->corner[i].target_quot[j] != 0) {
  3505. vreg->corner[i].target_quot[j]
  3506. += cpr3_quot_adjustment(
  3507. vreg->corner[i].ro_scale[j],
  3508. new_volt)
  3509. - cpr3_quot_adjustment(
  3510. vreg->corner[i].ro_scale[j],
  3511. old_volt);
  3512. }
  3513. }
  3514. rounded_volt = CPR3_ROUND(new_volt,
  3515. vreg->thread->ctrl->step_volt);
  3516. if (!vreg->aging_allow_open_loop_adj)
  3517. rounded_volt = 0;
  3518. vreg->corner[i].ceiling_volt
  3519. = vreg->corner[i].unaged_ceiling_volt + rounded_volt;
  3520. vreg->corner[i].ceiling_volt = min(vreg->corner[i].ceiling_volt,
  3521. vreg->corner[i].abs_ceiling_volt);
  3522. vreg->corner[i].floor_volt
  3523. = vreg->corner[i].unaged_floor_volt + rounded_volt;
  3524. vreg->corner[i].floor_volt = min(vreg->corner[i].floor_volt,
  3525. vreg->corner[i].ceiling_volt);
  3526. vreg->corner[i].open_loop_volt
  3527. = vreg->corner[i].unaged_open_loop_volt + rounded_volt;
  3528. vreg->corner[i].open_loop_volt
  3529. = min(vreg->corner[i].open_loop_volt,
  3530. vreg->corner[i].ceiling_volt);
  3531. vreg->corner[i].last_volt = vreg->corner[i].open_loop_volt;
  3532. cpr3_debug(vreg, "corner %d: applying %d uV closed-loop and %d uV open-loop voltage margin adjustment\n",
  3533. i, new_volt, rounded_volt);
  3534. }
  3535. }
  3536. /**
  3537. * cpr3_regulator_set_aging_ref_adjustment() - adjust target quotients for the
  3538. * regulators managed by this CPR controller to account for aging
  3539. * @ctrl: Pointer to the CPR3 controller
  3540. * @ref_adjust_volt: New aging reference adjustment voltage in microvolts to
  3541. * apply to all regulators managed by this CPR controller
  3542. *
  3543. * The existing aging adjustment as defined by ctrl->aging_ref_adjust_volt is
  3544. * first removed and then the adjustment is applied. Lastly, the value of
  3545. * ctrl->aging_ref_adjust_volt is updated to ref_adjust_volt.
  3546. */
  3547. static void cpr3_regulator_set_aging_ref_adjustment(
  3548. struct cpr3_controller *ctrl, int ref_adjust_volt)
  3549. {
  3550. struct cpr3_regulator *vreg;
  3551. int i, j;
  3552. for (i = 0; i < ctrl->thread_count; i++) {
  3553. for (j = 0; j < ctrl->thread[i].vreg_count; j++) {
  3554. vreg = &ctrl->thread[i].vreg[j];
  3555. cpr3_regulator_readjust_volt_and_quot(vreg,
  3556. ctrl->aging_ref_adjust_volt, ref_adjust_volt);
  3557. if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPRH)
  3558. cprh_adjust_voltages_for_apm(vreg);
  3559. }
  3560. }
  3561. ctrl->aging_ref_adjust_volt = ref_adjust_volt;
  3562. }
  3563. /**
  3564. * cpr3_regulator_aging_adjust() - adjust the target quotients for regulators
  3565. * based on the output of CPR aging sensors
  3566. * @ctrl: Pointer to the CPR3 controller
  3567. *
  3568. * Return: 0 on success, errno on failure
  3569. */
  3570. static int cpr3_regulator_aging_adjust(struct cpr3_controller *ctrl)
  3571. {
  3572. struct cpr3_regulator *vreg;
  3573. struct cpr3_corner restore_aging_corner;
  3574. struct cpr3_corner *corner;
  3575. int *restore_current_corner;
  3576. bool *restore_vreg_enabled;
  3577. int i, j, id, rc, rc2, vreg_count, aging_volt, max_aging_volt = 0;
  3578. u32 reg;
  3579. if (!ctrl->aging_required || !ctrl->cpr_enabled
  3580. || ctrl->aggr_corner.ceiling_volt == 0
  3581. || ctrl->aggr_corner.ceiling_volt > ctrl->aging_ref_volt)
  3582. return 0;
  3583. for (i = 0, vreg_count = 0; i < ctrl->thread_count; i++) {
  3584. for (j = 0; j < ctrl->thread[i].vreg_count; j++) {
  3585. vreg = &ctrl->thread[i].vreg[j];
  3586. vreg_count++;
  3587. if (vreg->aging_allowed && vreg->vreg_enabled
  3588. && vreg->current_corner > vreg->aging_corner)
  3589. return 0;
  3590. }
  3591. }
  3592. /* Verify that none of the aging sensors are currently masked. */
  3593. for (i = 0; i < ctrl->aging_sensor_count; i++) {
  3594. id = ctrl->aging_sensor[i].sensor_id;
  3595. reg = cpr3_read(ctrl, CPR3_REG_SENSOR_MASK_READ(id));
  3596. if (reg & BIT(id % 32))
  3597. return 0;
  3598. }
  3599. /*
  3600. * Verify that the aging possible register (if specified) has an
  3601. * acceptable value.
  3602. */
  3603. if (ctrl->aging_possible_reg) {
  3604. reg = readl_relaxed(ctrl->aging_possible_reg);
  3605. reg &= ctrl->aging_possible_mask;
  3606. if (reg != ctrl->aging_possible_val)
  3607. return 0;
  3608. }
  3609. restore_current_corner = kcalloc(vreg_count,
  3610. sizeof(*restore_current_corner), GFP_KERNEL);
  3611. restore_vreg_enabled = kcalloc(vreg_count,
  3612. sizeof(*restore_vreg_enabled), GFP_KERNEL);
  3613. if (!restore_current_corner || !restore_vreg_enabled) {
  3614. kfree(restore_current_corner);
  3615. kfree(restore_vreg_enabled);
  3616. return -ENOMEM;
  3617. }
  3618. /* Force all regulators to the aging corner */
  3619. for (i = 0, vreg_count = 0; i < ctrl->thread_count; i++) {
  3620. for (j = 0; j < ctrl->thread[i].vreg_count; j++, vreg_count++) {
  3621. vreg = &ctrl->thread[i].vreg[j];
  3622. restore_current_corner[vreg_count]
  3623. = vreg->current_corner;
  3624. restore_vreg_enabled[vreg_count]
  3625. = vreg->vreg_enabled;
  3626. vreg->current_corner = vreg->aging_corner;
  3627. vreg->vreg_enabled = true;
  3628. }
  3629. }
  3630. /* Force one of the regulators to require the aging reference voltage */
  3631. vreg = &ctrl->thread[0].vreg[0];
  3632. corner = &vreg->corner[vreg->current_corner];
  3633. restore_aging_corner = *corner;
  3634. corner->ceiling_volt = ctrl->aging_ref_volt;
  3635. corner->floor_volt = ctrl->aging_ref_volt;
  3636. corner->open_loop_volt = ctrl->aging_ref_volt;
  3637. corner->last_volt = ctrl->aging_ref_volt;
  3638. /* Skip last_volt caching */
  3639. ctrl->last_corner_was_closed_loop = false;
  3640. /* Set the vdd supply voltage to the aging reference voltage */
  3641. rc = _cpr3_regulator_update_ctrl_state(ctrl);
  3642. if (rc) {
  3643. cpr3_err(ctrl, "unable to force vdd-supply to the aging reference voltage=%d uV, rc=%d\n",
  3644. ctrl->aging_ref_volt, rc);
  3645. goto cleanup;
  3646. }
  3647. if (ctrl->aging_vdd_mode) {
  3648. rc = regulator_set_mode(ctrl->vdd_regulator,
  3649. ctrl->aging_vdd_mode);
  3650. if (rc) {
  3651. cpr3_err(ctrl, "unable to configure vdd-supply for mode=%u, rc=%d\n",
  3652. ctrl->aging_vdd_mode, rc);
  3653. goto cleanup;
  3654. }
  3655. }
  3656. /* Perform aging measurement on all aging sensors */
  3657. for (i = 0; i < ctrl->aging_sensor_count; i++) {
  3658. for (j = 0; j < CPR3_AGING_RETRY_COUNT; j++) {
  3659. rc = cpr3_regulator_measure_aging(ctrl,
  3660. &ctrl->aging_sensor[i]);
  3661. if (!rc)
  3662. break;
  3663. }
  3664. if (!rc) {
  3665. aging_volt =
  3666. cpr3_voltage_adjustment(
  3667. ctrl->aging_sensor[i].ro_scale,
  3668. ctrl->aging_sensor[i].measured_quot_diff
  3669. - ctrl->aging_sensor[i].init_quot_diff);
  3670. max_aging_volt = max(max_aging_volt, aging_volt);
  3671. } else {
  3672. cpr3_err(ctrl, "CPR aging measurement failed after %d tries, rc=%d\n",
  3673. j, rc);
  3674. ctrl->aging_failed = true;
  3675. ctrl->aging_required = false;
  3676. goto cleanup;
  3677. }
  3678. }
  3679. cleanup:
  3680. vreg = &ctrl->thread[0].vreg[0];
  3681. vreg->corner[vreg->current_corner] = restore_aging_corner;
  3682. for (i = 0, vreg_count = 0; i < ctrl->thread_count; i++) {
  3683. for (j = 0; j < ctrl->thread[i].vreg_count; j++, vreg_count++) {
  3684. vreg = &ctrl->thread[i].vreg[j];
  3685. vreg->current_corner
  3686. = restore_current_corner[vreg_count];
  3687. vreg->vreg_enabled = restore_vreg_enabled[vreg_count];
  3688. }
  3689. }
  3690. kfree(restore_current_corner);
  3691. kfree(restore_vreg_enabled);
  3692. /* Adjust the CPR target quotients according to the aging measurement */
  3693. if (!rc) {
  3694. cpr3_regulator_set_aging_ref_adjustment(ctrl, max_aging_volt);
  3695. cpr3_info(ctrl, "aging measurement successful; aging reference adjustment voltage=%d uV\n",
  3696. ctrl->aging_ref_adjust_volt);
  3697. ctrl->aging_succeeded = true;
  3698. ctrl->aging_required = false;
  3699. }
  3700. if (ctrl->aging_complete_vdd_mode) {
  3701. rc = regulator_set_mode(ctrl->vdd_regulator,
  3702. ctrl->aging_complete_vdd_mode);
  3703. if (rc)
  3704. cpr3_err(ctrl, "unable to configure vdd-supply for mode=%u, rc=%d\n",
  3705. ctrl->aging_complete_vdd_mode, rc);
  3706. }
  3707. /* Skip last_volt caching */
  3708. ctrl->last_corner_was_closed_loop = false;
  3709. /*
  3710. * Restore vdd-supply to the voltage before the aging measurement and
  3711. * restore the CPR3 controller hardware state.
  3712. */
  3713. rc2 = _cpr3_regulator_update_ctrl_state(ctrl);
  3714. /* Stop last_volt caching on for the next request */
  3715. ctrl->last_corner_was_closed_loop = false;
  3716. return rc ? rc : rc2;
  3717. }
  3718. /**
  3719. * cprh_regulator_aging_adjust() - adjust the target quotients and open-loop
  3720. * voltages for CPRh regulators based on the output of CPR aging
  3721. * sensors
  3722. * @ctrl: Pointer to the CPR3 controller
  3723. *
  3724. * Return: 0 on success, errno on failure
  3725. */
  3726. static int cprh_regulator_aging_adjust(struct cpr3_controller *ctrl)
  3727. {
  3728. int i, j, id, rc, rc2, aging_volt, init_volt;
  3729. int max_aging_volt = 0;
  3730. u32 reg;
  3731. if (!ctrl->aging_required || !ctrl->cpr_enabled)
  3732. return 0;
  3733. if (!ctrl->vdd_regulator) {
  3734. cpr3_err(ctrl, "vdd-supply regulator missing\n");
  3735. return -ENODEV;
  3736. }
  3737. init_volt = regulator_get_voltage(ctrl->vdd_regulator);
  3738. if (init_volt < 0) {
  3739. cpr3_err(ctrl, "could not get vdd-supply voltage, rc=%d\n",
  3740. init_volt);
  3741. return init_volt;
  3742. }
  3743. if (init_volt > ctrl->aging_ref_volt) {
  3744. cpr3_info(ctrl, "unable to perform CPR aging measurement as vdd=%d uV > aging voltage=%d uV\n",
  3745. init_volt, ctrl->aging_ref_volt);
  3746. return 0;
  3747. }
  3748. /* Verify that none of the aging sensors are currently masked. */
  3749. for (i = 0; i < ctrl->aging_sensor_count; i++) {
  3750. id = ctrl->aging_sensor[i].sensor_id;
  3751. reg = cpr3_read(ctrl, CPR3_REG_SENSOR_MASK_READ(id));
  3752. if (reg & BIT(id % 32)) {
  3753. cpr3_info(ctrl, "unable to perform CPR aging measurement as CPR sensor %d is masked\n",
  3754. id);
  3755. return 0;
  3756. }
  3757. }
  3758. rc = regulator_set_voltage(ctrl->vdd_regulator, ctrl->aging_ref_volt,
  3759. INT_MAX);
  3760. if (rc) {
  3761. cpr3_err(ctrl, "unable to set vdd-supply to aging voltage=%d uV, rc=%d\n",
  3762. ctrl->aging_ref_volt, rc);
  3763. return rc;
  3764. }
  3765. if (ctrl->aging_vdd_mode) {
  3766. rc = regulator_set_mode(ctrl->vdd_regulator,
  3767. ctrl->aging_vdd_mode);
  3768. if (rc) {
  3769. cpr3_err(ctrl, "unable to configure vdd-supply for mode=%u, rc=%d\n",
  3770. ctrl->aging_vdd_mode, rc);
  3771. goto cleanup;
  3772. }
  3773. }
  3774. /* Perform aging measurement on all aging sensors */
  3775. for (i = 0; i < ctrl->aging_sensor_count; i++) {
  3776. for (j = 0; j < CPR3_AGING_RETRY_COUNT; j++) {
  3777. rc = cpr3_regulator_measure_aging(ctrl,
  3778. &ctrl->aging_sensor[i]);
  3779. if (!rc)
  3780. break;
  3781. }
  3782. if (!rc) {
  3783. aging_volt =
  3784. cpr3_voltage_adjustment(
  3785. ctrl->aging_sensor[i].ro_scale,
  3786. ctrl->aging_sensor[i].measured_quot_diff
  3787. - ctrl->aging_sensor[i].init_quot_diff);
  3788. max_aging_volt = max(max_aging_volt, aging_volt);
  3789. } else {
  3790. cpr3_err(ctrl, "CPR aging measurement failed after %d tries, rc=%d\n",
  3791. j, rc);
  3792. ctrl->aging_failed = true;
  3793. ctrl->aging_required = false;
  3794. goto cleanup;
  3795. }
  3796. }
  3797. cleanup:
  3798. /* Adjust the CPR target quotients according to the aging measurement */
  3799. if (!rc) {
  3800. cpr3_regulator_set_aging_ref_adjustment(ctrl, max_aging_volt);
  3801. cpr3_info(ctrl, "aging measurement successful; aging reference adjustment voltage=%d uV\n",
  3802. ctrl->aging_ref_adjust_volt);
  3803. ctrl->aging_succeeded = true;
  3804. ctrl->aging_required = false;
  3805. }
  3806. rc2 = regulator_set_voltage(ctrl->vdd_regulator, init_volt, INT_MAX);
  3807. if (rc2) {
  3808. cpr3_err(ctrl, "unable to reset vdd-supply to initial voltage=%d uV, rc=%d\n",
  3809. init_volt, rc2);
  3810. return rc2;
  3811. }
  3812. if (ctrl->aging_complete_vdd_mode) {
  3813. rc2 = regulator_set_mode(ctrl->vdd_regulator,
  3814. ctrl->aging_complete_vdd_mode);
  3815. if (rc2) {
  3816. cpr3_err(ctrl, "unable to configure vdd-supply for mode=%u, rc=%d\n",
  3817. ctrl->aging_complete_vdd_mode, rc2);
  3818. return rc2;
  3819. }
  3820. }
  3821. return rc;
  3822. }
  3823. /**
  3824. * cpr3_regulator_update_ctrl_state() - update the state of the CPR controller
  3825. * to reflect the corners used by all CPR3 regulators as well as
  3826. * the CPR operating mode and perform aging adjustments if needed
  3827. * @ctrl: Pointer to the CPR3 controller
  3828. *
  3829. * Note, CPR3 controller lock must be held by the caller.
  3830. *
  3831. * Return: 0 on success, errno on failure
  3832. */
  3833. static int cpr3_regulator_update_ctrl_state(struct cpr3_controller *ctrl)
  3834. {
  3835. int rc;
  3836. rc = _cpr3_regulator_update_ctrl_state(ctrl);
  3837. if (rc)
  3838. return rc;
  3839. return cpr3_regulator_aging_adjust(ctrl);
  3840. }
  3841. /**
  3842. * cpr3_regulator_set_voltage() - set the voltage corner for the CPR3 regulator
  3843. * associated with the regulator device
  3844. * @rdev: Regulator device pointer for the cpr3-regulator
  3845. * @corner: New voltage corner to set (offset by CPR3_CORNER_OFFSET)
  3846. * @corner_max: Maximum voltage corner allowed (offset by
  3847. * CPR3_CORNER_OFFSET)
  3848. * @selector: Pointer which is filled with the selector value for the
  3849. * corner
  3850. *
  3851. * This function is passed as a callback function into the regulator ops that
  3852. * are registered for each cpr3-regulator device. The VDD voltage will not be
  3853. * physically configured until both this function and cpr3_regulator_enable()
  3854. * are called.
  3855. *
  3856. * Return: 0 on success, errno on failure
  3857. */
  3858. static int cpr3_regulator_set_voltage(struct regulator_dev *rdev,
  3859. int corner, int corner_max, unsigned int *selector)
  3860. {
  3861. struct cpr3_regulator *vreg = rdev_get_drvdata(rdev);
  3862. struct cpr3_controller *ctrl = vreg->thread->ctrl;
  3863. int rc = 0;
  3864. int last_corner;
  3865. corner -= CPR3_CORNER_OFFSET;
  3866. corner_max -= CPR3_CORNER_OFFSET;
  3867. *selector = corner;
  3868. mutex_lock(&ctrl->lock);
  3869. if (!vreg->vreg_enabled) {
  3870. vreg->current_corner = corner;
  3871. cpr3_debug(vreg, "stored corner=%d\n", corner);
  3872. goto done;
  3873. } else if (vreg->current_corner == corner) {
  3874. goto done;
  3875. }
  3876. last_corner = vreg->current_corner;
  3877. vreg->current_corner = corner;
  3878. rc = cpr3_regulator_update_ctrl_state(ctrl);
  3879. if (rc) {
  3880. cpr3_err(vreg, "could not update CPR state, rc=%d\n", rc);
  3881. vreg->current_corner = last_corner;
  3882. }
  3883. cpr3_debug(vreg, "set corner=%d\n", corner);
  3884. done:
  3885. mutex_unlock(&ctrl->lock);
  3886. return rc;
  3887. }
  3888. /**
  3889. * cpr3_regulator_get_voltage() - get the voltage corner for the CPR3 regulator
  3890. * associated with the regulator device
  3891. * @rdev: Regulator device pointer for the cpr3-regulator
  3892. *
  3893. * This function is passed as a callback function into the regulator ops that
  3894. * are registered for each cpr3-regulator device.
  3895. *
  3896. * Return: voltage corner value offset by CPR3_CORNER_OFFSET
  3897. */
  3898. static int cpr3_regulator_get_voltage(struct regulator_dev *rdev)
  3899. {
  3900. struct cpr3_regulator *vreg = rdev_get_drvdata(rdev);
  3901. if (vreg->current_corner == CPR3_REGULATOR_CORNER_INVALID)
  3902. return CPR3_CORNER_OFFSET;
  3903. else
  3904. return vreg->current_corner + CPR3_CORNER_OFFSET;
  3905. }
  3906. /**
  3907. * cpr3_regulator_list_voltage() - return the voltage corner mapped to the
  3908. * specified selector
  3909. * @rdev: Regulator device pointer for the cpr3-regulator
  3910. * @selector: Regulator selector
  3911. *
  3912. * This function is passed as a callback function into the regulator ops that
  3913. * are registered for each cpr3-regulator device.
  3914. *
  3915. * Return: voltage corner value offset by CPR3_CORNER_OFFSET
  3916. */
  3917. static int cpr3_regulator_list_voltage(struct regulator_dev *rdev,
  3918. unsigned int selector)
  3919. {
  3920. struct cpr3_regulator *vreg = rdev_get_drvdata(rdev);
  3921. if (selector < vreg->corner_count)
  3922. return selector + CPR3_CORNER_OFFSET;
  3923. else
  3924. return 0;
  3925. }
  3926. /**
  3927. * cpr3_regulator_list_corner_voltage() - return the ceiling voltage mapped to
  3928. * the specified voltage corner
  3929. * @rdev: Regulator device pointer for the cpr3-regulator
  3930. * @corner: Voltage corner
  3931. *
  3932. * This function is passed as a callback function into the regulator ops that
  3933. * are registered for each cpr3-regulator device.
  3934. *
  3935. * Return: voltage value in microvolts or -EINVAL if the corner is out of range
  3936. */
  3937. static int cpr3_regulator_list_corner_voltage(struct regulator_dev *rdev,
  3938. int corner)
  3939. {
  3940. struct cpr3_regulator *vreg = rdev_get_drvdata(rdev);
  3941. corner -= CPR3_CORNER_OFFSET;
  3942. if (corner >= 0 && corner < vreg->corner_count)
  3943. return vreg->corner[corner].ceiling_volt;
  3944. else
  3945. return -EINVAL;
  3946. }
  3947. /**
  3948. * cpr3_regulator_is_enabled() - return the enable state of the CPR3 regulator
  3949. * @rdev: Regulator device pointer for the cpr3-regulator
  3950. *
  3951. * This function is passed as a callback function into the regulator ops that
  3952. * are registered for each cpr3-regulator device.
  3953. *
  3954. * Return: true if regulator is enabled, false if regulator is disabled
  3955. */
  3956. static int cpr3_regulator_is_enabled(struct regulator_dev *rdev)
  3957. {
  3958. struct cpr3_regulator *vreg = rdev_get_drvdata(rdev);
  3959. return vreg->vreg_enabled;
  3960. }
  3961. /**
  3962. * cpr3_regulator_enable() - enable the CPR3 regulator
  3963. * @rdev: Regulator device pointer for the cpr3-regulator
  3964. *
  3965. * This function is passed as a callback function into the regulator ops that
  3966. * are registered for each cpr3-regulator device.
  3967. *
  3968. * Return: 0 on success, errno on failure
  3969. */
  3970. static int cpr3_regulator_enable(struct regulator_dev *rdev)
  3971. {
  3972. struct cpr3_regulator *vreg = rdev_get_drvdata(rdev);
  3973. struct cpr3_controller *ctrl = vreg->thread->ctrl;
  3974. int rc = 0;
  3975. if (vreg->vreg_enabled == true)
  3976. return 0;
  3977. mutex_lock(&ctrl->lock);
  3978. if (ctrl->system_regulator) {
  3979. rc = regulator_enable(ctrl->system_regulator);
  3980. if (rc) {
  3981. cpr3_err(ctrl, "regulator_enable(system) failed, rc=%d\n",
  3982. rc);
  3983. goto done;
  3984. }
  3985. }
  3986. rc = regulator_enable(ctrl->vdd_regulator);
  3987. if (rc) {
  3988. cpr3_err(vreg, "regulator_enable(vdd) failed, rc=%d\n", rc);
  3989. goto done;
  3990. }
  3991. if (vreg->ldo_regulator) {
  3992. rc = regulator_enable(vreg->ldo_regulator);
  3993. if (rc) {
  3994. cpr3_err(vreg, "regulator_enable(ldo) failed, rc=%d\n",
  3995. rc);
  3996. goto done;
  3997. }
  3998. }
  3999. vreg->vreg_enabled = true;
  4000. rc = cpr3_regulator_update_ctrl_state(ctrl);
  4001. if (rc) {
  4002. cpr3_err(vreg, "could not update CPR state, rc=%d\n", rc);
  4003. regulator_disable(ctrl->vdd_regulator);
  4004. vreg->vreg_enabled = false;
  4005. goto done;
  4006. }
  4007. cpr3_debug(vreg, "Enabled\n");
  4008. done:
  4009. mutex_unlock(&ctrl->lock);
  4010. return rc;
  4011. }
  4012. /**
  4013. * cpr3_regulator_disable() - disable the CPR3 regulator
  4014. * @rdev: Regulator device pointer for the cpr3-regulator
  4015. *
  4016. * This function is passed as a callback function into the regulator ops that
  4017. * are registered for each cpr3-regulator device.
  4018. *
  4019. * Return: 0 on success, errno on failure
  4020. */
  4021. static int cpr3_regulator_disable(struct regulator_dev *rdev)
  4022. {
  4023. struct cpr3_regulator *vreg = rdev_get_drvdata(rdev);
  4024. struct cpr3_controller *ctrl = vreg->thread->ctrl;
  4025. int rc, rc2;
  4026. if (vreg->vreg_enabled == false)
  4027. return 0;
  4028. mutex_lock(&ctrl->lock);
  4029. if (vreg->ldo_regulator && vreg->ldo_regulator_bypass == LDO_MODE) {
  4030. rc = regulator_get_voltage(ctrl->vdd_regulator);
  4031. if (rc < 0) {
  4032. cpr3_err(vreg, "regulator_get_voltage(vdd) failed, rc=%d\n",
  4033. rc);
  4034. goto done;
  4035. }
  4036. /* Switch back to BHS for safe operation */
  4037. rc = cpr3_regulator_set_bhs_mode(vreg, rc,
  4038. ctrl->aggr_corner.ceiling_volt);
  4039. if (rc) {
  4040. cpr3_err(vreg, "unable to switch to BHS mode, rc=%d\n",
  4041. rc);
  4042. goto done;
  4043. }
  4044. }
  4045. if (vreg->ldo_regulator) {
  4046. rc = regulator_disable(vreg->ldo_regulator);
  4047. if (rc) {
  4048. cpr3_err(vreg, "regulator_disable(ldo) failed, rc=%d\n",
  4049. rc);
  4050. goto done;
  4051. }
  4052. }
  4053. rc = regulator_disable(ctrl->vdd_regulator);
  4054. if (rc) {
  4055. cpr3_err(vreg, "regulator_disable(vdd) failed, rc=%d\n", rc);
  4056. goto done;
  4057. }
  4058. vreg->vreg_enabled = false;
  4059. rc = cpr3_regulator_update_ctrl_state(ctrl);
  4060. if (rc) {
  4061. cpr3_err(vreg, "could not update CPR state, rc=%d\n", rc);
  4062. rc2 = regulator_enable(ctrl->vdd_regulator);
  4063. vreg->vreg_enabled = true;
  4064. goto done;
  4065. }
  4066. if (ctrl->system_regulator) {
  4067. rc = regulator_disable(ctrl->system_regulator);
  4068. if (rc) {
  4069. cpr3_err(ctrl, "regulator_disable(system) failed, rc=%d\n",
  4070. rc);
  4071. goto done;
  4072. }
  4073. if (ctrl->support_ldo300_vreg) {
  4074. rc = regulator_set_voltage(ctrl->system_regulator, 0,
  4075. INT_MAX);
  4076. if (rc)
  4077. cpr3_err(ctrl, "failed to set voltage on system rc=%d\n",
  4078. rc);
  4079. goto done;
  4080. }
  4081. }
  4082. cpr3_debug(vreg, "Disabled\n");
  4083. done:
  4084. mutex_unlock(&ctrl->lock);
  4085. return rc;
  4086. }
  4087. static struct regulator_ops cpr3_regulator_ops = {
  4088. .enable = cpr3_regulator_enable,
  4089. .disable = cpr3_regulator_disable,
  4090. .is_enabled = cpr3_regulator_is_enabled,
  4091. .set_voltage = cpr3_regulator_set_voltage,
  4092. .get_voltage = cpr3_regulator_get_voltage,
  4093. .list_voltage = cpr3_regulator_list_voltage,
  4094. .list_corner_voltage = cpr3_regulator_list_corner_voltage,
  4095. };
  4096. /**
  4097. * cprh_regulator_get_voltage() - get the voltage corner for the CPR3 regulator
  4098. * associated with the regulator device
  4099. * @rdev: Regulator device pointer for the cpr3-regulator
  4100. *
  4101. * This function is passed as a callback function into the regulator ops that
  4102. * are registered for each cpr3-regulator device of a CPRh controller. The
  4103. * corner is read directly from CPRh hardware register.
  4104. *
  4105. * Return: voltage corner value offset by CPR3_CORNER_OFFSET
  4106. */
  4107. static int cprh_regulator_get_voltage(struct regulator_dev *rdev)
  4108. {
  4109. struct cpr3_regulator *vreg = rdev_get_drvdata(rdev);
  4110. struct cpr3_controller *ctrl = vreg->thread->ctrl;
  4111. bool cpr_enabled;
  4112. u32 reg, rc;
  4113. mutex_lock(&ctrl->lock);
  4114. cpr_enabled = ctrl->cpr_enabled;
  4115. if (!cpr_enabled) {
  4116. rc = cpr3_clock_enable(ctrl);
  4117. if (rc) {
  4118. cpr3_err(ctrl, "clock enable failed, rc=%d\n", rc);
  4119. mutex_unlock(&ctrl->lock);
  4120. return CPR3_REGULATOR_CORNER_INVALID;
  4121. }
  4122. ctrl->cpr_enabled = true;
  4123. }
  4124. reg = cpr3_read(vreg->thread->ctrl, CPRH_REG_STATUS(vreg->thread));
  4125. if (!cpr_enabled) {
  4126. cpr3_clock_disable(ctrl);
  4127. ctrl->cpr_enabled = false;
  4128. }
  4129. mutex_unlock(&ctrl->lock);
  4130. return (reg & CPRH_STATUS_CORNER)
  4131. + CPR3_CORNER_OFFSET;
  4132. }
  4133. static struct regulator_ops cprh_regulator_ops = {
  4134. .get_voltage = cprh_regulator_get_voltage,
  4135. .list_corner_voltage = cpr3_regulator_list_corner_voltage,
  4136. };
  4137. /**
  4138. * cpr3_print_result() - print CPR measurement results to the kernel log for
  4139. * debugging purposes
  4140. * @thread: Pointer to the CPR3 thread
  4141. *
  4142. * Return: None
  4143. */
  4144. static void cpr3_print_result(struct cpr3_thread *thread)
  4145. {
  4146. struct cpr3_controller *ctrl = thread->ctrl;
  4147. u32 result[3], busy, step_dn, step_up, error_steps, error, negative;
  4148. u32 quot_min, quot_max, ro_min, ro_max, step_quot_min, step_quot_max;
  4149. u32 sensor_min, sensor_max;
  4150. char *sign;
  4151. result[0] = cpr3_read(ctrl, CPR3_REG_RESULT0(thread->thread_id));
  4152. result[1] = cpr3_read(ctrl, CPR3_REG_RESULT1(thread->thread_id));
  4153. result[2] = cpr3_read(ctrl, CPR3_REG_RESULT2(thread->thread_id));
  4154. busy = !!(result[0] & CPR3_RESULT0_BUSY_MASK);
  4155. step_dn = !!(result[0] & CPR3_RESULT0_STEP_DN_MASK);
  4156. step_up = !!(result[0] & CPR3_RESULT0_STEP_UP_MASK);
  4157. error_steps = (result[0] & CPR3_RESULT0_ERROR_STEPS_MASK)
  4158. >> CPR3_RESULT0_ERROR_STEPS_SHIFT;
  4159. error = (result[0] & CPR3_RESULT0_ERROR_MASK)
  4160. >> CPR3_RESULT0_ERROR_SHIFT;
  4161. negative = !!(result[0] & CPR3_RESULT0_NEGATIVE_MASK);
  4162. quot_min = (result[1] & CPR3_RESULT1_QUOT_MIN_MASK)
  4163. >> CPR3_RESULT1_QUOT_MIN_SHIFT;
  4164. quot_max = (result[1] & CPR3_RESULT1_QUOT_MAX_MASK)
  4165. >> CPR3_RESULT1_QUOT_MAX_SHIFT;
  4166. ro_min = (result[1] & CPR3_RESULT1_RO_MIN_MASK)
  4167. >> CPR3_RESULT1_RO_MIN_SHIFT;
  4168. ro_max = (result[1] & CPR3_RESULT1_RO_MAX_MASK)
  4169. >> CPR3_RESULT1_RO_MAX_SHIFT;
  4170. step_quot_min = (result[2] & CPR3_RESULT2_STEP_QUOT_MIN_MASK)
  4171. >> CPR3_RESULT2_STEP_QUOT_MIN_SHIFT;
  4172. step_quot_max = (result[2] & CPR3_RESULT2_STEP_QUOT_MAX_MASK)
  4173. >> CPR3_RESULT2_STEP_QUOT_MAX_SHIFT;
  4174. sensor_min = (result[2] & CPR3_RESULT2_SENSOR_MIN_MASK)
  4175. >> CPR3_RESULT2_SENSOR_MIN_SHIFT;
  4176. sensor_max = (result[2] & CPR3_RESULT2_SENSOR_MAX_MASK)
  4177. >> CPR3_RESULT2_SENSOR_MAX_SHIFT;
  4178. sign = negative ? "-" : "";
  4179. cpr3_debug(ctrl, "thread %u: busy=%u, step_dn=%u, step_up=%u, error_steps=%s%u, error=%s%u\n",
  4180. thread->thread_id, busy, step_dn, step_up, sign, error_steps,
  4181. sign, error);
  4182. cpr3_debug(ctrl, "thread %u: quot_min=%u, quot_max=%u, ro_min=%u, ro_max=%u\n",
  4183. thread->thread_id, quot_min, quot_max, ro_min, ro_max);
  4184. cpr3_debug(ctrl, "thread %u: step_quot_min=%u, step_quot_max=%u, sensor_min=%u, sensor_max=%u\n",
  4185. thread->thread_id, step_quot_min, step_quot_max, sensor_min,
  4186. sensor_max);
  4187. }
  4188. /**
  4189. * cpr3_thread_busy() - returns if the specified CPR3 thread is busy taking
  4190. * a measurement
  4191. * @thread: Pointer to the CPR3 thread
  4192. *
  4193. * Return: CPR3 busy status
  4194. */
  4195. static bool cpr3_thread_busy(struct cpr3_thread *thread)
  4196. {
  4197. u32 result;
  4198. result = cpr3_read(thread->ctrl, CPR3_REG_RESULT0(thread->thread_id));
  4199. return !!(result & CPR3_RESULT0_BUSY_MASK);
  4200. }
  4201. /**
  4202. * cpr3_irq_handler() - CPR interrupt handler callback function used for
  4203. * software closed-loop operation
  4204. * @irq: CPR interrupt number
  4205. * @data: Private data corresponding to the CPR3 controller
  4206. * pointer
  4207. *
  4208. * This function increases or decreases the vdd supply voltage based upon the
  4209. * CPR controller recommendation.
  4210. *
  4211. * Return: IRQ_HANDLED
  4212. */
  4213. static irqreturn_t cpr3_irq_handler(int irq, void *data)
  4214. {
  4215. struct cpr3_controller *ctrl = data;
  4216. struct cpr3_corner *aggr = &ctrl->aggr_corner;
  4217. u32 cont = CPR3_CONT_CMD_NACK;
  4218. u32 reg_last_measurement = 0;
  4219. struct cpr3_regulator *vreg;
  4220. struct cpr3_corner *corner;
  4221. unsigned long flags;
  4222. int i, j, new_volt, last_volt, dynamic_floor_volt, rc;
  4223. u32 irq_en, status, cpr_status, ctl;
  4224. bool up, down;
  4225. mutex_lock(&ctrl->lock);
  4226. if (!ctrl->cpr_enabled) {
  4227. cpr3_debug(ctrl, "CPR interrupt received but CPR is disabled\n");
  4228. mutex_unlock(&ctrl->lock);
  4229. return IRQ_HANDLED;
  4230. } else if (ctrl->use_hw_closed_loop) {
  4231. cpr3_debug(ctrl, "CPR interrupt received but CPR is using HW closed-loop\n");
  4232. goto done;
  4233. }
  4234. /*
  4235. * CPR IRQ status checking and CPR controller disabling must happen
  4236. * atomically and without invening delay in order to avoid an interrupt
  4237. * storm caused by the handler racing with the CPR controller.
  4238. */
  4239. local_irq_save(flags);
  4240. preempt_disable();
  4241. status = cpr3_read(ctrl, CPR3_REG_IRQ_STATUS);
  4242. up = status & CPR3_IRQ_UP;
  4243. down = status & CPR3_IRQ_DOWN;
  4244. if (!up && !down) {
  4245. /*
  4246. * Toggle the CPR controller off and then back on since the
  4247. * hardware and software states are out of sync. This condition
  4248. * occurs after an aging measurement completes as the CPR IRQ
  4249. * physically triggers during the aging measurement but the
  4250. * handler is stuck waiting on the mutex lock.
  4251. */
  4252. cpr3_ctrl_loop_disable(ctrl);
  4253. local_irq_restore(flags);
  4254. preempt_enable();
  4255. /* Wait for the loop disable write to complete */
  4256. mb();
  4257. /* Wait for BUSY=1 and LOOP_EN=0 in CPR controller registers. */
  4258. for (i = 0; i < CPR3_REGISTER_WRITE_DELAY_US / 10; i++) {
  4259. cpr_status = cpr3_read(ctrl, CPR3_REG_CPR_STATUS);
  4260. ctl = cpr3_read(ctrl, CPR3_REG_CPR_CTL);
  4261. if (cpr_status & CPR3_CPR_STATUS_BUSY_MASK
  4262. && (ctl & CPR3_CPR_CTL_LOOP_EN_MASK)
  4263. == CPR3_CPR_CTL_LOOP_DISABLE)
  4264. break;
  4265. udelay(10);
  4266. }
  4267. if (i == CPR3_REGISTER_WRITE_DELAY_US / 10)
  4268. cpr3_debug(ctrl, "CPR controller not disabled after %d us\n",
  4269. CPR3_REGISTER_WRITE_DELAY_US);
  4270. /* Clear interrupt status */
  4271. cpr3_write(ctrl, CPR3_REG_IRQ_CLEAR,
  4272. CPR3_IRQ_UP | CPR3_IRQ_DOWN);
  4273. /* Wait for the interrupt clearing write to complete */
  4274. mb();
  4275. /* Wait for IRQ_STATUS register to be cleared. */
  4276. for (i = 0; i < CPR3_REGISTER_WRITE_DELAY_US / 10; i++) {
  4277. status = cpr3_read(ctrl, CPR3_REG_IRQ_STATUS);
  4278. if (!(status & (CPR3_IRQ_UP | CPR3_IRQ_DOWN)))
  4279. break;
  4280. udelay(10);
  4281. }
  4282. if (i == CPR3_REGISTER_WRITE_DELAY_US / 10)
  4283. cpr3_debug(ctrl, "CPR interrupts not cleared after %d us\n",
  4284. CPR3_REGISTER_WRITE_DELAY_US);
  4285. cpr3_ctrl_loop_enable(ctrl);
  4286. cpr3_debug(ctrl, "CPR interrupt received but no up or down status bit is set\n");
  4287. mutex_unlock(&ctrl->lock);
  4288. return IRQ_HANDLED;
  4289. } else if (up && down) {
  4290. cpr3_debug(ctrl, "both up and down status bits set\n");
  4291. /* The up flag takes precedence over the down flag. */
  4292. down = false;
  4293. }
  4294. if (ctrl->supports_hw_closed_loop)
  4295. reg_last_measurement
  4296. = cpr3_read(ctrl, CPR3_REG_LAST_MEASUREMENT);
  4297. dynamic_floor_volt = cpr3_regulator_get_dynamic_floor_volt(ctrl,
  4298. reg_last_measurement);
  4299. local_irq_restore(flags);
  4300. preempt_enable();
  4301. irq_en = aggr->irq_en;
  4302. last_volt = aggr->last_volt;
  4303. for (i = 0; i < ctrl->thread_count; i++) {
  4304. if (cpr3_thread_busy(&ctrl->thread[i])) {
  4305. cpr3_debug(ctrl, "CPR thread %u busy when it should be waiting for SW cont\n",
  4306. ctrl->thread[i].thread_id);
  4307. goto done;
  4308. }
  4309. }
  4310. new_volt = up ? last_volt + ctrl->step_volt
  4311. : last_volt - ctrl->step_volt;
  4312. /* Re-enable UP/DOWN interrupt when its opposite is received. */
  4313. irq_en |= up ? CPR3_IRQ_DOWN : CPR3_IRQ_UP;
  4314. if (new_volt > aggr->ceiling_volt) {
  4315. new_volt = aggr->ceiling_volt;
  4316. irq_en &= ~CPR3_IRQ_UP;
  4317. cpr3_debug(ctrl, "limiting to ceiling=%d uV\n",
  4318. aggr->ceiling_volt);
  4319. } else if (new_volt < aggr->floor_volt) {
  4320. new_volt = aggr->floor_volt;
  4321. irq_en &= ~CPR3_IRQ_DOWN;
  4322. cpr3_debug(ctrl, "limiting to floor=%d uV\n", aggr->floor_volt);
  4323. }
  4324. if (down && new_volt < dynamic_floor_volt) {
  4325. /*
  4326. * The vdd-supply voltage should not be decreased below the
  4327. * dynamic floor voltage. However, it is not necessary (and
  4328. * counter productive) to force the voltage up to this level
  4329. * if it happened to be below it since the closed-loop voltage
  4330. * must have gotten there in a safe manner while the power
  4331. * domains for the CPR3 regulator imposing the dynamic floor
  4332. * were not bypassed.
  4333. */
  4334. new_volt = last_volt;
  4335. irq_en &= ~CPR3_IRQ_DOWN;
  4336. cpr3_debug(ctrl, "limiting to dynamic floor=%d uV\n",
  4337. dynamic_floor_volt);
  4338. }
  4339. for (i = 0; i < ctrl->thread_count; i++)
  4340. cpr3_print_result(&ctrl->thread[i]);
  4341. cpr3_debug(ctrl, "%s: new_volt=%d uV, last_volt=%d uV\n",
  4342. up ? "UP" : "DN", new_volt, last_volt);
  4343. if (ctrl->proc_clock_throttle && last_volt == aggr->ceiling_volt
  4344. && new_volt < last_volt)
  4345. cpr3_write(ctrl, CPR3_REG_PD_THROTTLE,
  4346. ctrl->proc_clock_throttle);
  4347. if (new_volt != last_volt) {
  4348. rc = cpr3_regulator_scale_vdd_voltage(ctrl, new_volt,
  4349. last_volt,
  4350. aggr);
  4351. if (rc) {
  4352. cpr3_err(ctrl, "scale_vdd() failed to set vdd=%d uV, rc=%d\n",
  4353. new_volt, rc);
  4354. goto done;
  4355. }
  4356. cont = CPR3_CONT_CMD_ACK;
  4357. /*
  4358. * Update the closed-loop voltage for all regulators managed
  4359. * by this CPR controller.
  4360. */
  4361. for (i = 0; i < ctrl->thread_count; i++) {
  4362. for (j = 0; j < ctrl->thread[i].vreg_count; j++) {
  4363. vreg = &ctrl->thread[i].vreg[j];
  4364. cpr3_update_vreg_closed_loop_volt(vreg,
  4365. new_volt, reg_last_measurement);
  4366. }
  4367. }
  4368. }
  4369. if (ctrl->proc_clock_throttle && new_volt == aggr->ceiling_volt)
  4370. cpr3_write(ctrl, CPR3_REG_PD_THROTTLE,
  4371. CPR3_PD_THROTTLE_DISABLE);
  4372. corner = &ctrl->thread[0].vreg[0].corner[
  4373. ctrl->thread[0].vreg[0].current_corner];
  4374. if (irq_en != aggr->irq_en) {
  4375. aggr->irq_en = irq_en;
  4376. cpr3_write(ctrl, CPR3_REG_IRQ_EN, irq_en);
  4377. }
  4378. aggr->last_volt = new_volt;
  4379. done:
  4380. /* Clear interrupt status */
  4381. cpr3_write(ctrl, CPR3_REG_IRQ_CLEAR, CPR3_IRQ_UP | CPR3_IRQ_DOWN);
  4382. /* ACK or NACK the CPR controller */
  4383. cpr3_write(ctrl, CPR3_REG_CONT_CMD, cont);
  4384. mutex_unlock(&ctrl->lock);
  4385. return IRQ_HANDLED;
  4386. }
  4387. /**
  4388. * cpr3_ceiling_irq_handler() - CPR ceiling reached interrupt handler callback
  4389. * function used for hardware closed-loop operation
  4390. * @irq: CPR ceiling interrupt number
  4391. * @data: Private data corresponding to the CPR3 controller
  4392. * pointer
  4393. *
  4394. * This function disables processor clock throttling and closed-loop operation
  4395. * when the ceiling voltage is reached.
  4396. *
  4397. * Return: IRQ_HANDLED
  4398. */
  4399. static irqreturn_t cpr3_ceiling_irq_handler(int irq, void *data)
  4400. {
  4401. struct cpr3_controller *ctrl = data;
  4402. int rc, volt;
  4403. mutex_lock(&ctrl->lock);
  4404. if (!ctrl->cpr_enabled) {
  4405. cpr3_debug(ctrl, "CPR ceiling interrupt received but CPR is disabled\n");
  4406. goto done;
  4407. } else if (!ctrl->use_hw_closed_loop) {
  4408. cpr3_debug(ctrl, "CPR ceiling interrupt received but CPR is using SW closed-loop\n");
  4409. goto done;
  4410. }
  4411. volt = regulator_get_voltage(ctrl->vdd_regulator);
  4412. if (volt < 0) {
  4413. cpr3_err(ctrl, "could not get vdd voltage, rc=%d\n", volt);
  4414. goto done;
  4415. } else if (volt != ctrl->aggr_corner.ceiling_volt) {
  4416. cpr3_debug(ctrl, "CPR ceiling interrupt received but vdd voltage: %d uV != ceiling voltage: %d uV\n",
  4417. volt, ctrl->aggr_corner.ceiling_volt);
  4418. goto done;
  4419. }
  4420. if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPR3) {
  4421. /*
  4422. * Since the ceiling voltage has been reached, disable processor
  4423. * clock throttling as well as CPR closed-loop operation.
  4424. */
  4425. cpr3_write(ctrl, CPR3_REG_PD_THROTTLE,
  4426. CPR3_PD_THROTTLE_DISABLE);
  4427. cpr3_ctrl_loop_disable(ctrl);
  4428. cpr3_debug(ctrl, "CPR closed-loop and throttling disabled\n");
  4429. }
  4430. done:
  4431. rc = msm_spm_avs_clear_irq(0, MSM_SPM_AVS_IRQ_MAX);
  4432. if (rc)
  4433. cpr3_err(ctrl, "could not clear max IRQ, rc=%d\n", rc);
  4434. mutex_unlock(&ctrl->lock);
  4435. return IRQ_HANDLED;
  4436. }
  4437. /**
  4438. * cpr3_regulator_vreg_register() - register a regulator device for a CPR3
  4439. * regulator
  4440. * @vreg: Pointer to the CPR3 regulator
  4441. *
  4442. * This function initializes all regulator framework related structures and then
  4443. * calls regulator_register() for the CPR3 regulator.
  4444. *
  4445. * Return: 0 on success, errno on failure
  4446. */
  4447. static int cpr3_regulator_vreg_register(struct cpr3_regulator *vreg)
  4448. {
  4449. struct regulator_config config = {};
  4450. struct regulator_desc *rdesc;
  4451. struct regulator_init_data *init_data;
  4452. int rc;
  4453. init_data = of_get_regulator_init_data(vreg->thread->ctrl->dev,
  4454. vreg->of_node, &vreg->rdesc);
  4455. if (!init_data) {
  4456. cpr3_err(vreg, "regulator init data is missing\n");
  4457. return -EINVAL;
  4458. }
  4459. init_data->constraints.input_uV = init_data->constraints.max_uV;
  4460. rdesc = &vreg->rdesc;
  4461. if (vreg->thread->ctrl->ctrl_type == CPR_CTRL_TYPE_CPRH) {
  4462. /* CPRh regulators are treated as always-on regulators */
  4463. rdesc->ops = &cprh_regulator_ops;
  4464. } else {
  4465. init_data->constraints.valid_ops_mask
  4466. |= REGULATOR_CHANGE_VOLTAGE | REGULATOR_CHANGE_STATUS;
  4467. rdesc->ops = &cpr3_regulator_ops;
  4468. }
  4469. rdesc->n_voltages = vreg->corner_count;
  4470. rdesc->name = init_data->constraints.name;
  4471. rdesc->owner = THIS_MODULE;
  4472. rdesc->type = REGULATOR_VOLTAGE;
  4473. config.dev = vreg->thread->ctrl->dev;
  4474. config.driver_data = vreg;
  4475. config.init_data = init_data;
  4476. config.of_node = vreg->of_node;
  4477. vreg->rdev = regulator_register(rdesc, &config);
  4478. if (IS_ERR(vreg->rdev)) {
  4479. rc = PTR_ERR(vreg->rdev);
  4480. cpr3_err(vreg, "regulator_register failed, rc=%d\n", rc);
  4481. return rc;
  4482. }
  4483. return 0;
  4484. }
  4485. static int debugfs_int_set(void *data, u64 val)
  4486. {
  4487. *(int *)data = val;
  4488. return 0;
  4489. }
  4490. static int debugfs_int_get(void *data, u64 *val)
  4491. {
  4492. *val = *(int *)data;
  4493. return 0;
  4494. }
  4495. DEFINE_SIMPLE_ATTRIBUTE(fops_int, debugfs_int_get, debugfs_int_set, "%lld\n");
  4496. DEFINE_SIMPLE_ATTRIBUTE(fops_int_ro, debugfs_int_get, NULL, "%lld\n");
  4497. DEFINE_SIMPLE_ATTRIBUTE(fops_int_wo, NULL, debugfs_int_set, "%lld\n");
  4498. /**
  4499. * debugfs_create_int - create a debugfs file that is used to read and write a
  4500. * signed int value
  4501. * @name: Pointer to a string containing the name of the file to
  4502. * create
  4503. * @mode: The permissions that the file should have
  4504. * @parent: Pointer to the parent dentry for this file. This should
  4505. * be a directory dentry if set. If this parameter is
  4506. * %NULL, then the file will be created in the root of the
  4507. * debugfs filesystem.
  4508. * @value: Pointer to the variable that the file should read to and
  4509. * write from
  4510. *
  4511. * This function creates a file in debugfs with the given name that
  4512. * contains the value of the variable @value. If the @mode variable is so
  4513. * set, it can be read from, and written to.
  4514. *
  4515. * This function will return a pointer to a dentry if it succeeds. This
  4516. * pointer must be passed to the debugfs_remove() function when the file is
  4517. * to be removed. If an error occurs, %NULL will be returned.
  4518. */
  4519. static struct dentry *debugfs_create_int(const char *name, umode_t mode,
  4520. struct dentry *parent, int *value)
  4521. {
  4522. /* if there are no write bits set, make read only */
  4523. if (!(mode & 0222))
  4524. return debugfs_create_file(name, mode, parent, value,
  4525. &fops_int_ro);
  4526. /* if there are no read bits set, make write only */
  4527. if (!(mode & 0444))
  4528. return debugfs_create_file(name, mode, parent, value,
  4529. &fops_int_wo);
  4530. return debugfs_create_file(name, mode, parent, value, &fops_int);
  4531. }
  4532. static int debugfs_bool_get(void *data, u64 *val)
  4533. {
  4534. *val = *(bool *)data;
  4535. return 0;
  4536. }
  4537. DEFINE_SIMPLE_ATTRIBUTE(fops_bool_ro, debugfs_bool_get, NULL, "%lld\n");
  4538. /**
  4539. * cpr3_debug_ldo_mode_allowed_set() - debugfs callback used to change the
  4540. * value of the CPR3 regulator ldo_mode_allowed flag
  4541. * @data: Pointer to private data which is equal to the CPR3
  4542. * regulator pointer
  4543. * @val: New value for ldo_mode_allowed
  4544. *
  4545. * Return: 0 on success, errno on failure
  4546. */
  4547. static int cpr3_debug_ldo_mode_allowed_set(void *data, u64 val)
  4548. {
  4549. struct cpr3_regulator *vreg = data;
  4550. struct cpr3_controller *ctrl = vreg->thread->ctrl;
  4551. bool allow = !!val;
  4552. int rc, vdd_volt;
  4553. mutex_lock(&ctrl->lock);
  4554. if (vreg->ldo_mode_allowed == allow)
  4555. goto done;
  4556. vreg->ldo_mode_allowed = allow;
  4557. if (!allow && vreg->ldo_regulator_bypass == LDO_MODE) {
  4558. vdd_volt = regulator_get_voltage(ctrl->vdd_regulator);
  4559. if (vdd_volt < 0) {
  4560. cpr3_err(vreg, "regulator_get_voltage(vdd) failed, rc=%d\n",
  4561. vdd_volt);
  4562. goto done;
  4563. }
  4564. /* Switch back to BHS */
  4565. rc = cpr3_regulator_set_bhs_mode(vreg, vdd_volt,
  4566. ctrl->aggr_corner.ceiling_volt);
  4567. if (rc) {
  4568. cpr3_err(vreg, "unable to switch to BHS mode, rc=%d\n",
  4569. rc);
  4570. goto done;
  4571. }
  4572. } else {
  4573. rc = cpr3_regulator_update_ctrl_state(ctrl);
  4574. if (rc) {
  4575. cpr3_err(vreg, "could not change LDO mode=%s, rc=%d\n",
  4576. allow ? "allowed" : "disallowed", rc);
  4577. goto done;
  4578. }
  4579. }
  4580. cpr3_debug(vreg, "LDO mode=%s\n", allow ? "allowed" : "disallowed");
  4581. done:
  4582. mutex_unlock(&ctrl->lock);
  4583. return 0;
  4584. }
  4585. /**
  4586. * cpr3_debug_ldo_mode_allowed_get() - debugfs callback used to retrieve the
  4587. * value of the CPR3 regulator ldo_mode_allowed flag
  4588. * @data: Pointer to private data which is equal to the CPR3
  4589. * regulator pointer
  4590. * @val: Output parameter written with a value of the
  4591. * ldo_mode_allowed flag
  4592. *
  4593. * Return: 0 on success, errno on failure
  4594. */
  4595. static int cpr3_debug_ldo_mode_allowed_get(void *data, u64 *val)
  4596. {
  4597. struct cpr3_regulator *vreg = data;
  4598. *val = vreg->ldo_mode_allowed;
  4599. return 0;
  4600. }
  4601. DEFINE_SIMPLE_ATTRIBUTE(cpr3_debug_ldo_mode_allowed_fops,
  4602. cpr3_debug_ldo_mode_allowed_get,
  4603. cpr3_debug_ldo_mode_allowed_set,
  4604. "%llu\n");
  4605. /**
  4606. * cpr3_debug_ldo_mode_get() - debugfs callback used to retrieve the state of
  4607. * the CPR3 regulator's LDO
  4608. * @data: Pointer to private data which is equal to the CPR3
  4609. * regulator pointer
  4610. * @val: Output parameter written with a value of 1 if using
  4611. * LDO mode or 0 if the LDO is bypassed
  4612. *
  4613. * Return: 0 on success, errno on failure
  4614. */
  4615. static int cpr3_debug_ldo_mode_get(void *data, u64 *val)
  4616. {
  4617. struct cpr3_regulator *vreg = data;
  4618. *val = (vreg->ldo_regulator_bypass == LDO_MODE);
  4619. return 0;
  4620. }
  4621. DEFINE_SIMPLE_ATTRIBUTE(cpr3_debug_ldo_mode_fops, cpr3_debug_ldo_mode_get,
  4622. NULL, "%llu\n");
  4623. /**
  4624. * struct cpr3_debug_corner_info - data structure used by the
  4625. * cpr3_debugfs_create_corner_int function
  4626. * @vreg: Pointer to the CPR3 regulator
  4627. * @index: Pointer to the corner array index
  4628. * @member_offset: Offset in bytes from the beginning of struct cpr3_corner
  4629. * to the beginning of the value to be read from
  4630. * @corner: Pointer to the CPR3 corner array
  4631. */
  4632. struct cpr3_debug_corner_info {
  4633. struct cpr3_regulator *vreg;
  4634. int *index;
  4635. size_t member_offset;
  4636. struct cpr3_corner *corner;
  4637. };
  4638. static int cpr3_debug_corner_int_get(void *data, u64 *val)
  4639. {
  4640. struct cpr3_debug_corner_info *info = data;
  4641. struct cpr3_controller *ctrl = info->vreg->thread->ctrl;
  4642. int i;
  4643. mutex_lock(&ctrl->lock);
  4644. i = *info->index;
  4645. if (i < 0)
  4646. i = 0;
  4647. *val = *(int *)((char *)&info->vreg->corner[i] + info->member_offset);
  4648. mutex_unlock(&ctrl->lock);
  4649. return 0;
  4650. }
  4651. DEFINE_SIMPLE_ATTRIBUTE(cpr3_debug_corner_int_fops, cpr3_debug_corner_int_get,
  4652. NULL, "%lld\n");
  4653. /**
  4654. * cpr3_debugfs_create_corner_int - create a debugfs file that is used to read
  4655. * a signed int value out of a CPR3 regulator's corner array
  4656. * @vreg: Pointer to the CPR3 regulator
  4657. * @name: Pointer to a string containing the name of the file to
  4658. * create
  4659. * @mode: The permissions that the file should have
  4660. * @parent: Pointer to the parent dentry for this file. This should
  4661. * be a directory dentry if set. If this parameter is
  4662. * %NULL, then the file will be created in the root of the
  4663. * debugfs filesystem.
  4664. * @index: Pointer to the corner array index
  4665. * @member_offset: Offset in bytes from the beginning of struct cpr3_corner
  4666. * to the beginning of the value to be read from
  4667. *
  4668. * This function creates a file in debugfs with the given name that
  4669. * contains the value of the int type variable vreg->corner[index].member
  4670. * where member_offset == offsetof(struct cpr3_corner, member).
  4671. */
  4672. static struct dentry *cpr3_debugfs_create_corner_int(
  4673. struct cpr3_regulator *vreg, const char *name, umode_t mode,
  4674. struct dentry *parent, int *index, size_t member_offset)
  4675. {
  4676. struct cpr3_debug_corner_info *info;
  4677. info = devm_kzalloc(vreg->thread->ctrl->dev, sizeof(*info), GFP_KERNEL);
  4678. if (!info)
  4679. return NULL;
  4680. info->vreg = vreg;
  4681. info->index = index;
  4682. info->member_offset = member_offset;
  4683. return debugfs_create_file(name, mode, parent, info,
  4684. &cpr3_debug_corner_int_fops);
  4685. }
  4686. static int cpr3_debug_quot_open(struct inode *inode, struct file *file)
  4687. {
  4688. struct cpr3_debug_corner_info *info = inode->i_private;
  4689. struct cpr3_thread *thread = info->vreg->thread;
  4690. int size, i, pos;
  4691. u32 *quot;
  4692. char *buf;
  4693. /*
  4694. * Max size:
  4695. * - 10 digits + ' ' or '\n' = 11 bytes per number
  4696. * - terminating '\0'
  4697. */
  4698. size = CPR3_RO_COUNT * 11;
  4699. buf = kzalloc(size + 1, GFP_KERNEL);
  4700. if (!buf)
  4701. return -ENOMEM;
  4702. file->private_data = buf;
  4703. mutex_lock(&thread->ctrl->lock);
  4704. quot = info->corner[*info->index].target_quot;
  4705. for (i = 0, pos = 0; i < CPR3_RO_COUNT; i++)
  4706. pos += scnprintf(buf + pos, size - pos, "%u%c",
  4707. quot[i], i < CPR3_RO_COUNT - 1 ? ' ' : '\n');
  4708. mutex_unlock(&thread->ctrl->lock);
  4709. return nonseekable_open(inode, file);
  4710. }
  4711. static ssize_t cpr3_debug_quot_read(struct file *file, char __user *buf,
  4712. size_t len, loff_t *ppos)
  4713. {
  4714. return simple_read_from_buffer(buf, len, ppos, file->private_data,
  4715. strlen(file->private_data));
  4716. }
  4717. static int cpr3_debug_quot_release(struct inode *inode, struct file *file)
  4718. {
  4719. kfree(file->private_data);
  4720. return 0;
  4721. }
  4722. static const struct file_operations cpr3_debug_quot_fops = {
  4723. .owner = THIS_MODULE,
  4724. .open = cpr3_debug_quot_open,
  4725. .release = cpr3_debug_quot_release,
  4726. .read = cpr3_debug_quot_read,
  4727. .llseek = no_llseek,
  4728. };
  4729. /**
  4730. * cpr3_regulator_debugfs_corner_add() - add debugfs files to expose
  4731. * configuration data for the CPR corner
  4732. * @vreg: Pointer to the CPR3 regulator
  4733. * @corner_dir: Pointer to the parent corner dentry for the new files
  4734. * @index: Pointer to the corner array index
  4735. *
  4736. * Return: none
  4737. */
  4738. static void cpr3_regulator_debugfs_corner_add(struct cpr3_regulator *vreg,
  4739. struct dentry *corner_dir, int *index)
  4740. {
  4741. struct cpr3_debug_corner_info *info;
  4742. struct dentry *temp;
  4743. temp = cpr3_debugfs_create_corner_int(vreg, "floor_volt", 0444,
  4744. corner_dir, index, offsetof(struct cpr3_corner, floor_volt));
  4745. if (IS_ERR_OR_NULL(temp)) {
  4746. cpr3_err(vreg, "floor_volt debugfs file creation failed\n");
  4747. return;
  4748. }
  4749. temp = cpr3_debugfs_create_corner_int(vreg, "ceiling_volt", 0444,
  4750. corner_dir, index, offsetof(struct cpr3_corner, ceiling_volt));
  4751. if (IS_ERR_OR_NULL(temp)) {
  4752. cpr3_err(vreg, "ceiling_volt debugfs file creation failed\n");
  4753. return;
  4754. }
  4755. temp = cpr3_debugfs_create_corner_int(vreg, "open_loop_volt", 0444,
  4756. corner_dir, index,
  4757. offsetof(struct cpr3_corner, open_loop_volt));
  4758. if (IS_ERR_OR_NULL(temp)) {
  4759. cpr3_err(vreg, "open_loop_volt debugfs file creation failed\n");
  4760. return;
  4761. }
  4762. temp = cpr3_debugfs_create_corner_int(vreg, "last_volt", 0444,
  4763. corner_dir, index, offsetof(struct cpr3_corner, last_volt));
  4764. if (IS_ERR_OR_NULL(temp)) {
  4765. cpr3_err(vreg, "last_volt debugfs file creation failed\n");
  4766. return;
  4767. }
  4768. info = devm_kzalloc(vreg->thread->ctrl->dev, sizeof(*info), GFP_KERNEL);
  4769. if (!info)
  4770. return;
  4771. info->vreg = vreg;
  4772. info->index = index;
  4773. info->corner = vreg->corner;
  4774. temp = debugfs_create_file("target_quots", 0444, corner_dir, info,
  4775. &cpr3_debug_quot_fops);
  4776. if (IS_ERR_OR_NULL(temp)) {
  4777. cpr3_err(vreg, "target_quots debugfs file creation failed\n");
  4778. return;
  4779. }
  4780. }
  4781. /**
  4782. * cpr3_debug_corner_index_set() - debugfs callback used to change the
  4783. * value of the CPR3 regulator debug_corner index
  4784. * @data: Pointer to private data which is equal to the CPR3
  4785. * regulator pointer
  4786. * @val: New value for debug_corner
  4787. *
  4788. * Return: 0 on success, errno on failure
  4789. */
  4790. static int cpr3_debug_corner_index_set(void *data, u64 val)
  4791. {
  4792. struct cpr3_regulator *vreg = data;
  4793. if (val < CPR3_CORNER_OFFSET || val > vreg->corner_count) {
  4794. cpr3_err(vreg, "invalid corner index %llu; allowed values: %d-%d\n",
  4795. val, CPR3_CORNER_OFFSET, vreg->corner_count);
  4796. return -EINVAL;
  4797. }
  4798. mutex_lock(&vreg->thread->ctrl->lock);
  4799. vreg->debug_corner = val - CPR3_CORNER_OFFSET;
  4800. mutex_unlock(&vreg->thread->ctrl->lock);
  4801. return 0;
  4802. }
  4803. /**
  4804. * cpr3_debug_corner_index_get() - debugfs callback used to retrieve
  4805. * the value of the CPR3 regulator debug_corner index
  4806. * @data: Pointer to private data which is equal to the CPR3
  4807. * regulator pointer
  4808. * @val: Output parameter written with the value of
  4809. * debug_corner
  4810. *
  4811. * Return: 0 on success, errno on failure
  4812. */
  4813. static int cpr3_debug_corner_index_get(void *data, u64 *val)
  4814. {
  4815. struct cpr3_regulator *vreg = data;
  4816. *val = vreg->debug_corner + CPR3_CORNER_OFFSET;
  4817. return 0;
  4818. }
  4819. DEFINE_SIMPLE_ATTRIBUTE(cpr3_debug_corner_index_fops,
  4820. cpr3_debug_corner_index_get,
  4821. cpr3_debug_corner_index_set,
  4822. "%llu\n");
  4823. /**
  4824. * cpr3_debug_current_corner_index_get() - debugfs callback used to retrieve
  4825. * the value of the CPR3 regulator current_corner index
  4826. * @data: Pointer to private data which is equal to the CPR3
  4827. * regulator pointer
  4828. * @val: Output parameter written with the value of
  4829. * current_corner
  4830. *
  4831. * Return: 0 on success, errno on failure
  4832. */
  4833. static int cpr3_debug_current_corner_index_get(void *data, u64 *val)
  4834. {
  4835. struct cpr3_regulator *vreg = data;
  4836. *val = vreg->current_corner + CPR3_CORNER_OFFSET;
  4837. return 0;
  4838. }
  4839. DEFINE_SIMPLE_ATTRIBUTE(cpr3_debug_current_corner_index_fops,
  4840. cpr3_debug_current_corner_index_get,
  4841. NULL, "%llu\n");
  4842. /**
  4843. * cpr3_regulator_debugfs_vreg_add() - add debugfs files to expose configuration
  4844. * data for the CPR3 regulator
  4845. * @vreg: Pointer to the CPR3 regulator
  4846. * @thread_dir CPR3 thread debugfs directory handle
  4847. *
  4848. * Return: none
  4849. */
  4850. static void cpr3_regulator_debugfs_vreg_add(struct cpr3_regulator *vreg,
  4851. struct dentry *thread_dir)
  4852. {
  4853. struct dentry *temp, *corner_dir, *vreg_dir;
  4854. vreg_dir = debugfs_create_dir(vreg->name, thread_dir);
  4855. if (IS_ERR_OR_NULL(vreg_dir)) {
  4856. cpr3_err(vreg, "%s debugfs directory creation failed\n",
  4857. vreg->name);
  4858. return;
  4859. }
  4860. temp = debugfs_create_int("speed_bin_fuse", 0444, vreg_dir,
  4861. &vreg->speed_bin_fuse);
  4862. if (IS_ERR_OR_NULL(temp)) {
  4863. cpr3_err(vreg, "speed_bin_fuse debugfs file creation failed\n");
  4864. return;
  4865. }
  4866. temp = debugfs_create_int("cpr_rev_fuse", 0444, vreg_dir,
  4867. &vreg->cpr_rev_fuse);
  4868. if (IS_ERR_OR_NULL(temp)) {
  4869. cpr3_err(vreg, "cpr_rev_fuse debugfs file creation failed\n");
  4870. return;
  4871. }
  4872. temp = debugfs_create_int("fuse_combo", 0444, vreg_dir,
  4873. &vreg->fuse_combo);
  4874. if (IS_ERR_OR_NULL(temp)) {
  4875. cpr3_err(vreg, "fuse_combo debugfs file creation failed\n");
  4876. return;
  4877. }
  4878. if (vreg->ldo_regulator) {
  4879. temp = debugfs_create_file("ldo_mode", 0444, vreg_dir, vreg,
  4880. &cpr3_debug_ldo_mode_fops);
  4881. if (IS_ERR_OR_NULL(temp)) {
  4882. cpr3_err(vreg, "ldo_mode debugfs file creation failed\n");
  4883. return;
  4884. }
  4885. temp = debugfs_create_file("ldo_mode_allowed",
  4886. 0644, vreg_dir, vreg,
  4887. &cpr3_debug_ldo_mode_allowed_fops);
  4888. if (IS_ERR_OR_NULL(temp)) {
  4889. cpr3_err(vreg, "ldo_mode_allowed debugfs file creation failed\n");
  4890. return;
  4891. }
  4892. }
  4893. temp = debugfs_create_int("corner_count", 0444, vreg_dir,
  4894. &vreg->corner_count);
  4895. if (IS_ERR_OR_NULL(temp)) {
  4896. cpr3_err(vreg, "corner_count debugfs file creation failed\n");
  4897. return;
  4898. }
  4899. corner_dir = debugfs_create_dir("corner", vreg_dir);
  4900. if (IS_ERR_OR_NULL(corner_dir)) {
  4901. cpr3_err(vreg, "corner debugfs directory creation failed\n");
  4902. return;
  4903. }
  4904. temp = debugfs_create_file("index", 0644, corner_dir, vreg,
  4905. &cpr3_debug_corner_index_fops);
  4906. if (IS_ERR_OR_NULL(temp)) {
  4907. cpr3_err(vreg, "index debugfs file creation failed\n");
  4908. return;
  4909. }
  4910. cpr3_regulator_debugfs_corner_add(vreg, corner_dir,
  4911. &vreg->debug_corner);
  4912. corner_dir = debugfs_create_dir("current_corner", vreg_dir);
  4913. if (IS_ERR_OR_NULL(corner_dir)) {
  4914. cpr3_err(vreg, "current_corner debugfs directory creation failed\n");
  4915. return;
  4916. }
  4917. temp = debugfs_create_file("index", 0444, corner_dir, vreg,
  4918. &cpr3_debug_current_corner_index_fops);
  4919. if (IS_ERR_OR_NULL(temp)) {
  4920. cpr3_err(vreg, "index debugfs file creation failed\n");
  4921. return;
  4922. }
  4923. cpr3_regulator_debugfs_corner_add(vreg, corner_dir,
  4924. &vreg->current_corner);
  4925. }
  4926. /**
  4927. * cpr3_regulator_debugfs_thread_add() - add debugfs files to expose
  4928. * configuration data for the CPR thread
  4929. * @thread: Pointer to the CPR3 thread
  4930. *
  4931. * Return: none
  4932. */
  4933. static void cpr3_regulator_debugfs_thread_add(struct cpr3_thread *thread)
  4934. {
  4935. struct cpr3_controller *ctrl = thread->ctrl;
  4936. struct dentry *aggr_dir, *temp, *thread_dir;
  4937. struct cpr3_debug_corner_info *info;
  4938. char buf[20];
  4939. int *index;
  4940. int i;
  4941. scnprintf(buf, sizeof(buf), "thread%u", thread->thread_id);
  4942. thread_dir = debugfs_create_dir(buf, thread->ctrl->debugfs);
  4943. if (IS_ERR_OR_NULL(thread_dir)) {
  4944. cpr3_err(ctrl, "thread %u %s debugfs directory creation failed\n",
  4945. thread->thread_id, buf);
  4946. return;
  4947. }
  4948. aggr_dir = debugfs_create_dir("max_aggregated_params", thread_dir);
  4949. if (IS_ERR_OR_NULL(aggr_dir)) {
  4950. cpr3_err(ctrl, "thread %u max_aggregated_params debugfs directory creation failed\n",
  4951. thread->thread_id);
  4952. return;
  4953. }
  4954. temp = debugfs_create_int("floor_volt", 0444, aggr_dir,
  4955. &thread->aggr_corner.floor_volt);
  4956. if (IS_ERR_OR_NULL(temp)) {
  4957. cpr3_err(ctrl, "thread %u aggr floor_volt debugfs file creation failed\n",
  4958. thread->thread_id);
  4959. return;
  4960. }
  4961. temp = debugfs_create_int("ceiling_volt", 0444, aggr_dir,
  4962. &thread->aggr_corner.ceiling_volt);
  4963. if (IS_ERR_OR_NULL(temp)) {
  4964. cpr3_err(ctrl, "thread %u aggr ceiling_volt debugfs file creation failed\n",
  4965. thread->thread_id);
  4966. return;
  4967. }
  4968. temp = debugfs_create_int("open_loop_volt", 0444, aggr_dir,
  4969. &thread->aggr_corner.open_loop_volt);
  4970. if (IS_ERR_OR_NULL(temp)) {
  4971. cpr3_err(ctrl, "thread %u aggr open_loop_volt debugfs file creation failed\n",
  4972. thread->thread_id);
  4973. return;
  4974. }
  4975. temp = debugfs_create_int("last_volt", 0444, aggr_dir,
  4976. &thread->aggr_corner.last_volt);
  4977. if (IS_ERR_OR_NULL(temp)) {
  4978. cpr3_err(ctrl, "thread %u aggr last_volt debugfs file creation failed\n",
  4979. thread->thread_id);
  4980. return;
  4981. }
  4982. info = devm_kzalloc(thread->ctrl->dev, sizeof(*info), GFP_KERNEL);
  4983. index = devm_kzalloc(thread->ctrl->dev, sizeof(*index), GFP_KERNEL);
  4984. if (!info || !index)
  4985. return;
  4986. *index = 0;
  4987. info->vreg = &thread->vreg[0];
  4988. info->index = index;
  4989. info->corner = &thread->aggr_corner;
  4990. temp = debugfs_create_file("target_quots", 0444, aggr_dir, info,
  4991. &cpr3_debug_quot_fops);
  4992. if (IS_ERR_OR_NULL(temp)) {
  4993. cpr3_err(ctrl, "thread %u target_quots debugfs file creation failed\n",
  4994. thread->thread_id);
  4995. return;
  4996. }
  4997. for (i = 0; i < thread->vreg_count; i++)
  4998. cpr3_regulator_debugfs_vreg_add(&thread->vreg[i], thread_dir);
  4999. }
  5000. /**
  5001. * cpr3_debug_closed_loop_enable_set() - debugfs callback used to change the
  5002. * value of the CPR controller cpr_allowed_sw flag which enables or
  5003. * disables closed-loop operation
  5004. * @data: Pointer to private data which is equal to the CPR
  5005. * controller pointer
  5006. * @val: New value for cpr_allowed_sw
  5007. *
  5008. * Return: 0 on success, errno on failure
  5009. */
  5010. static int cpr3_debug_closed_loop_enable_set(void *data, u64 val)
  5011. {
  5012. struct cpr3_controller *ctrl = data;
  5013. bool enable = !!val;
  5014. int rc;
  5015. mutex_lock(&ctrl->lock);
  5016. if (ctrl->cpr_allowed_sw == enable)
  5017. goto done;
  5018. if (enable && !ctrl->cpr_allowed_hw) {
  5019. cpr3_err(ctrl, "CPR closed-loop operation is not allowed\n");
  5020. goto done;
  5021. }
  5022. ctrl->cpr_allowed_sw = enable;
  5023. if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPRH) {
  5024. cpr3_masked_write(ctrl, CPR4_REG_MARGIN_ADJ_CTL,
  5025. CPR4_MARGIN_ADJ_CTL_HW_CLOSED_LOOP_EN_MASK,
  5026. ctrl->cpr_allowed_sw && ctrl->use_hw_closed_loop
  5027. ? CPR4_MARGIN_ADJ_CTL_HW_CLOSED_LOOP_ENABLE
  5028. : CPR4_MARGIN_ADJ_CTL_HW_CLOSED_LOOP_DISABLE);
  5029. } else {
  5030. rc = cpr3_regulator_update_ctrl_state(ctrl);
  5031. if (rc) {
  5032. cpr3_err(ctrl, "could not change CPR enable state=%u, rc=%d\n",
  5033. enable, rc);
  5034. goto done;
  5035. }
  5036. if (ctrl->proc_clock_throttle && !ctrl->cpr_enabled) {
  5037. rc = cpr3_clock_enable(ctrl);
  5038. if (rc) {
  5039. cpr3_err(ctrl, "clock enable failed, rc=%d\n",
  5040. rc);
  5041. goto done;
  5042. }
  5043. ctrl->cpr_enabled = true;
  5044. cpr3_write(ctrl, CPR3_REG_PD_THROTTLE,
  5045. CPR3_PD_THROTTLE_DISABLE);
  5046. cpr3_clock_disable(ctrl);
  5047. ctrl->cpr_enabled = false;
  5048. }
  5049. }
  5050. if (ctrl->ctrl_type != CPR_CTRL_TYPE_CPRH) {
  5051. cpr3_debug(ctrl, "closed-loop=%s\n", enable ?
  5052. "enabled" : "disabled");
  5053. } else {
  5054. cpr3_debug(ctrl, "closed-loop=%s\n", enable &&
  5055. ctrl->use_hw_closed_loop ? "enabled" : "disabled");
  5056. }
  5057. done:
  5058. mutex_unlock(&ctrl->lock);
  5059. return 0;
  5060. }
  5061. /**
  5062. * cpr3_debug_closed_loop_enable_get() - debugfs callback used to retrieve
  5063. * the value of the CPR controller cpr_allowed_sw flag which
  5064. * indicates if closed-loop operation is enabled
  5065. * @data: Pointer to private data which is equal to the CPR
  5066. * controller pointer
  5067. * @val: Output parameter written with the value of
  5068. * cpr_allowed_sw
  5069. *
  5070. * Return: 0 on success, errno on failure
  5071. */
  5072. static int cpr3_debug_closed_loop_enable_get(void *data, u64 *val)
  5073. {
  5074. struct cpr3_controller *ctrl = data;
  5075. *val = ctrl->cpr_allowed_sw;
  5076. return 0;
  5077. }
  5078. DEFINE_SIMPLE_ATTRIBUTE(cpr3_debug_closed_loop_enable_fops,
  5079. cpr3_debug_closed_loop_enable_get,
  5080. cpr3_debug_closed_loop_enable_set,
  5081. "%llu\n");
  5082. /**
  5083. * cpr3_debug_hw_closed_loop_enable_set() - debugfs callback used to change the
  5084. * value of the CPR controller use_hw_closed_loop flag which
  5085. * switches between software closed-loop and hardware closed-loop
  5086. * operation for CPR3 and CPR4 controllers and between open-loop
  5087. * and full hardware closed-loop operation for CPRh controllers.
  5088. * @data: Pointer to private data which is equal to the CPR
  5089. * controller pointer
  5090. * @val: New value for use_hw_closed_loop
  5091. *
  5092. * Return: 0 on success, errno on failure
  5093. */
  5094. static int cpr3_debug_hw_closed_loop_enable_set(void *data, u64 val)
  5095. {
  5096. struct cpr3_controller *ctrl = data;
  5097. bool use_hw_closed_loop = !!val;
  5098. struct cpr3_regulator *vreg;
  5099. bool cpr_enabled;
  5100. int i, j, k, rc;
  5101. mutex_lock(&ctrl->lock);
  5102. if (ctrl->use_hw_closed_loop == use_hw_closed_loop)
  5103. goto done;
  5104. if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPR4) {
  5105. rc = cpr3_ctrl_clear_cpr4_config(ctrl);
  5106. if (rc) {
  5107. cpr3_err(ctrl, "failed to clear CPR4 configuration,rc=%d\n",
  5108. rc);
  5109. goto done;
  5110. }
  5111. }
  5112. if (ctrl->ctrl_type != CPR_CTRL_TYPE_CPRH)
  5113. cpr3_ctrl_loop_disable(ctrl);
  5114. ctrl->use_hw_closed_loop = use_hw_closed_loop;
  5115. cpr_enabled = ctrl->cpr_enabled;
  5116. /* Ensure that CPR clocks are enabled before writing to registers. */
  5117. if (!cpr_enabled) {
  5118. rc = cpr3_clock_enable(ctrl);
  5119. if (rc) {
  5120. cpr3_err(ctrl, "clock enable failed, rc=%d\n", rc);
  5121. goto done;
  5122. }
  5123. ctrl->cpr_enabled = true;
  5124. }
  5125. if (ctrl->use_hw_closed_loop && ctrl->ctrl_type != CPR_CTRL_TYPE_CPRH)
  5126. cpr3_write(ctrl, CPR3_REG_IRQ_EN, 0);
  5127. if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPR4) {
  5128. cpr3_masked_write(ctrl, CPR4_REG_MARGIN_ADJ_CTL,
  5129. CPR4_MARGIN_ADJ_CTL_HW_CLOSED_LOOP_EN_MASK,
  5130. ctrl->use_hw_closed_loop
  5131. ? CPR4_MARGIN_ADJ_CTL_HW_CLOSED_LOOP_ENABLE
  5132. : CPR4_MARGIN_ADJ_CTL_HW_CLOSED_LOOP_DISABLE);
  5133. } else if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPRH) {
  5134. cpr3_masked_write(ctrl, CPR4_REG_MARGIN_ADJ_CTL,
  5135. CPR4_MARGIN_ADJ_CTL_HW_CLOSED_LOOP_EN_MASK,
  5136. ctrl->cpr_allowed_sw && ctrl->use_hw_closed_loop
  5137. ? CPR4_MARGIN_ADJ_CTL_HW_CLOSED_LOOP_ENABLE
  5138. : CPR4_MARGIN_ADJ_CTL_HW_CLOSED_LOOP_DISABLE);
  5139. } else if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPR3) {
  5140. cpr3_write(ctrl, CPR3_REG_HW_CLOSED_LOOP,
  5141. ctrl->use_hw_closed_loop
  5142. ? CPR3_HW_CLOSED_LOOP_ENABLE
  5143. : CPR3_HW_CLOSED_LOOP_DISABLE);
  5144. }
  5145. /* Turn off CPR clocks if they were off before this function call. */
  5146. if (!cpr_enabled) {
  5147. cpr3_clock_disable(ctrl);
  5148. ctrl->cpr_enabled = false;
  5149. }
  5150. if (ctrl->use_hw_closed_loop && ctrl->ctrl_type == CPR_CTRL_TYPE_CPR3) {
  5151. rc = regulator_enable(ctrl->vdd_limit_regulator);
  5152. if (rc) {
  5153. cpr3_err(ctrl, "CPR limit regulator enable failed, rc=%d\n",
  5154. rc);
  5155. goto done;
  5156. }
  5157. rc = msm_spm_avs_enable_irq(0, MSM_SPM_AVS_IRQ_MAX);
  5158. if (rc) {
  5159. cpr3_err(ctrl, "could not enable max IRQ, rc=%d\n", rc);
  5160. goto done;
  5161. }
  5162. } else if (!ctrl->use_hw_closed_loop
  5163. && ctrl->ctrl_type == CPR_CTRL_TYPE_CPR3) {
  5164. rc = regulator_disable(ctrl->vdd_limit_regulator);
  5165. if (rc) {
  5166. cpr3_err(ctrl, "CPR limit regulator disable failed, rc=%d\n",
  5167. rc);
  5168. goto done;
  5169. }
  5170. rc = msm_spm_avs_disable_irq(0, MSM_SPM_AVS_IRQ_MAX);
  5171. if (rc) {
  5172. cpr3_err(ctrl, "could not disable max IRQ, rc=%d\n",
  5173. rc);
  5174. goto done;
  5175. }
  5176. }
  5177. if (ctrl->ctrl_type != CPR_CTRL_TYPE_CPRH) {
  5178. /*
  5179. * Due to APM and mem-acc floor restriction constraints,
  5180. * the closed-loop voltage may be different when using
  5181. * software closed-loop vs hardware closed-loop. Therefore,
  5182. * reset the cached closed-loop voltage for all corners to the
  5183. * corresponding open-loop voltage when switching between
  5184. * SW and HW closed-loop mode.
  5185. */
  5186. for (i = 0; i < ctrl->thread_count; i++) {
  5187. for (j = 0; j < ctrl->thread[i].vreg_count; j++) {
  5188. vreg = &ctrl->thread[i].vreg[j];
  5189. for (k = 0; k < vreg->corner_count; k++)
  5190. vreg->corner[k].last_volt
  5191. = vreg->corner[k].open_loop_volt;
  5192. }
  5193. }
  5194. /* Skip last_volt caching */
  5195. ctrl->last_corner_was_closed_loop = false;
  5196. rc = cpr3_regulator_update_ctrl_state(ctrl);
  5197. if (rc) {
  5198. cpr3_err(ctrl, "could not change CPR HW closed-loop enable state=%u, rc=%d\n",
  5199. use_hw_closed_loop, rc);
  5200. goto done;
  5201. }
  5202. cpr3_debug(ctrl, "CPR mode=%s\n",
  5203. use_hw_closed_loop ?
  5204. "HW closed-loop" : "SW closed-loop");
  5205. } else {
  5206. cpr3_debug(ctrl, "CPR mode=%s\n",
  5207. ctrl->cpr_allowed_sw && use_hw_closed_loop ?
  5208. "full HW closed-loop" : "open-loop");
  5209. }
  5210. done:
  5211. mutex_unlock(&ctrl->lock);
  5212. return 0;
  5213. }
  5214. /**
  5215. * cpr3_debug_hw_closed_loop_enable_get() - debugfs callback used to retrieve
  5216. * the value of the CPR controller use_hw_closed_loop flag which
  5217. * indicates if hardware closed-loop operation is being used in
  5218. * place of software closed-loop operation
  5219. * @data: Pointer to private data which is equal to the CPR
  5220. * controller pointer
  5221. * @val: Output parameter written with the value of
  5222. * use_hw_closed_loop
  5223. *
  5224. * Return: 0 on success, errno on failure
  5225. */
  5226. static int cpr3_debug_hw_closed_loop_enable_get(void *data, u64 *val)
  5227. {
  5228. struct cpr3_controller *ctrl = data;
  5229. *val = ctrl->use_hw_closed_loop;
  5230. return 0;
  5231. }
  5232. DEFINE_SIMPLE_ATTRIBUTE(cpr3_debug_hw_closed_loop_enable_fops,
  5233. cpr3_debug_hw_closed_loop_enable_get,
  5234. cpr3_debug_hw_closed_loop_enable_set,
  5235. "%llu\n");
  5236. /**
  5237. * cpr3_debug_trigger_aging_measurement_set() - debugfs callback used to trigger
  5238. * another CPR measurement
  5239. * @data: Pointer to private data which is equal to the CPR
  5240. * controller pointer
  5241. * @val: Unused
  5242. *
  5243. * Return: 0 on success, errno on failure
  5244. */
  5245. static int cpr3_debug_trigger_aging_measurement_set(void *data, u64 val)
  5246. {
  5247. struct cpr3_controller *ctrl = data;
  5248. int rc;
  5249. mutex_lock(&ctrl->lock);
  5250. if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPR4) {
  5251. rc = cpr3_ctrl_clear_cpr4_config(ctrl);
  5252. if (rc) {
  5253. cpr3_err(ctrl, "failed to clear CPR4 configuration,rc=%d\n",
  5254. rc);
  5255. goto done;
  5256. }
  5257. }
  5258. cpr3_ctrl_loop_disable(ctrl);
  5259. cpr3_regulator_set_aging_ref_adjustment(ctrl, INT_MAX);
  5260. ctrl->aging_required = true;
  5261. ctrl->aging_succeeded = false;
  5262. ctrl->aging_failed = false;
  5263. rc = cpr3_regulator_update_ctrl_state(ctrl);
  5264. if (rc) {
  5265. cpr3_err(ctrl, "could not update the CPR controller state, rc=%d\n",
  5266. rc);
  5267. goto done;
  5268. }
  5269. done:
  5270. mutex_unlock(&ctrl->lock);
  5271. return 0;
  5272. }
  5273. DEFINE_SIMPLE_ATTRIBUTE(cpr3_debug_trigger_aging_measurement_fops,
  5274. NULL,
  5275. cpr3_debug_trigger_aging_measurement_set,
  5276. "%llu\n");
  5277. /**
  5278. * cpr3_regulator_debugfs_ctrl_add() - add debugfs files to expose configuration
  5279. * data for the CPR controller
  5280. * @ctrl: Pointer to the CPR3 controller
  5281. *
  5282. * Return: none
  5283. */
  5284. static void cpr3_regulator_debugfs_ctrl_add(struct cpr3_controller *ctrl)
  5285. {
  5286. struct dentry *temp, *aggr_dir;
  5287. int i;
  5288. /* Add cpr3-regulator base directory if it isn't present already. */
  5289. if (cpr3_debugfs_base == NULL) {
  5290. cpr3_debugfs_base = debugfs_create_dir("cpr3-regulator", NULL);
  5291. if (IS_ERR_OR_NULL(cpr3_debugfs_base)) {
  5292. cpr3_err(ctrl, "cpr3-regulator debugfs base directory creation failed\n");
  5293. cpr3_debugfs_base = NULL;
  5294. return;
  5295. }
  5296. }
  5297. ctrl->debugfs = debugfs_create_dir(ctrl->name, cpr3_debugfs_base);
  5298. if (IS_ERR_OR_NULL(ctrl->debugfs)) {
  5299. cpr3_err(ctrl, "cpr3-regulator controller debugfs directory creation failed\n");
  5300. return;
  5301. }
  5302. temp = debugfs_create_file("cpr_closed_loop_enable", 0644,
  5303. ctrl->debugfs, ctrl,
  5304. &cpr3_debug_closed_loop_enable_fops);
  5305. if (IS_ERR_OR_NULL(temp)) {
  5306. cpr3_err(ctrl, "cpr_closed_loop_enable debugfs file creation failed\n");
  5307. return;
  5308. }
  5309. if (ctrl->supports_hw_closed_loop) {
  5310. temp = debugfs_create_file("use_hw_closed_loop", 0644,
  5311. ctrl->debugfs, ctrl,
  5312. &cpr3_debug_hw_closed_loop_enable_fops);
  5313. if (IS_ERR_OR_NULL(temp)) {
  5314. cpr3_err(ctrl, "use_hw_closed_loop debugfs file creation failed\n");
  5315. return;
  5316. }
  5317. }
  5318. temp = debugfs_create_int("thread_count", 0444, ctrl->debugfs,
  5319. &ctrl->thread_count);
  5320. if (IS_ERR_OR_NULL(temp)) {
  5321. cpr3_err(ctrl, "thread_count debugfs file creation failed\n");
  5322. return;
  5323. }
  5324. if (ctrl->apm) {
  5325. temp = debugfs_create_int("apm_threshold_volt", 0444,
  5326. ctrl->debugfs, &ctrl->apm_threshold_volt);
  5327. if (IS_ERR_OR_NULL(temp)) {
  5328. cpr3_err(ctrl, "apm_threshold_volt debugfs file creation failed\n");
  5329. return;
  5330. }
  5331. }
  5332. if (ctrl->aging_required || ctrl->aging_succeeded
  5333. || ctrl->aging_failed) {
  5334. temp = debugfs_create_int("aging_adj_volt", 0444,
  5335. ctrl->debugfs, &ctrl->aging_ref_adjust_volt);
  5336. if (IS_ERR_OR_NULL(temp)) {
  5337. cpr3_err(ctrl, "aging_adj_volt debugfs file creation failed\n");
  5338. return;
  5339. }
  5340. temp = debugfs_create_file("aging_succeeded", 0444,
  5341. ctrl->debugfs, &ctrl->aging_succeeded, &fops_bool_ro);
  5342. if (IS_ERR_OR_NULL(temp)) {
  5343. cpr3_err(ctrl, "aging_succeeded debugfs file creation failed\n");
  5344. return;
  5345. }
  5346. temp = debugfs_create_file("aging_failed", 0444,
  5347. ctrl->debugfs, &ctrl->aging_failed, &fops_bool_ro);
  5348. if (IS_ERR_OR_NULL(temp)) {
  5349. cpr3_err(ctrl, "aging_failed debugfs file creation failed\n");
  5350. return;
  5351. }
  5352. temp = debugfs_create_file("aging_trigger", 0200,
  5353. ctrl->debugfs, ctrl,
  5354. &cpr3_debug_trigger_aging_measurement_fops);
  5355. if (IS_ERR_OR_NULL(temp)) {
  5356. cpr3_err(ctrl, "aging_trigger debugfs file creation failed\n");
  5357. return;
  5358. }
  5359. }
  5360. aggr_dir = debugfs_create_dir("max_aggregated_voltages", ctrl->debugfs);
  5361. if (IS_ERR_OR_NULL(aggr_dir)) {
  5362. cpr3_err(ctrl, "max_aggregated_voltages debugfs directory creation failed\n");
  5363. return;
  5364. }
  5365. temp = debugfs_create_int("floor_volt", 0444, aggr_dir,
  5366. &ctrl->aggr_corner.floor_volt);
  5367. if (IS_ERR_OR_NULL(temp)) {
  5368. cpr3_err(ctrl, "aggr floor_volt debugfs file creation failed\n");
  5369. return;
  5370. }
  5371. temp = debugfs_create_int("ceiling_volt", 0444, aggr_dir,
  5372. &ctrl->aggr_corner.ceiling_volt);
  5373. if (IS_ERR_OR_NULL(temp)) {
  5374. cpr3_err(ctrl, "aggr ceiling_volt debugfs file creation failed\n");
  5375. return;
  5376. }
  5377. temp = debugfs_create_int("open_loop_volt", 0444, aggr_dir,
  5378. &ctrl->aggr_corner.open_loop_volt);
  5379. if (IS_ERR_OR_NULL(temp)) {
  5380. cpr3_err(ctrl, "aggr open_loop_volt debugfs file creation failed\n");
  5381. return;
  5382. }
  5383. temp = debugfs_create_int("last_volt", 0444, aggr_dir,
  5384. &ctrl->aggr_corner.last_volt);
  5385. if (IS_ERR_OR_NULL(temp)) {
  5386. cpr3_err(ctrl, "aggr last_volt debugfs file creation failed\n");
  5387. return;
  5388. }
  5389. for (i = 0; i < ctrl->thread_count; i++)
  5390. cpr3_regulator_debugfs_thread_add(&ctrl->thread[i]);
  5391. }
  5392. /**
  5393. * cpr3_regulator_debugfs_ctrl_remove() - remove debugfs files for the CPR
  5394. * controller
  5395. * @ctrl: Pointer to the CPR3 controller
  5396. *
  5397. * Note, this function must be called after the controller has been removed from
  5398. * cpr3_controller_list and while the cpr3_controller_list_mutex lock is held.
  5399. *
  5400. * Return: none
  5401. */
  5402. static void cpr3_regulator_debugfs_ctrl_remove(struct cpr3_controller *ctrl)
  5403. {
  5404. if (list_empty(&cpr3_controller_list)) {
  5405. debugfs_remove_recursive(cpr3_debugfs_base);
  5406. cpr3_debugfs_base = NULL;
  5407. } else {
  5408. debugfs_remove_recursive(ctrl->debugfs);
  5409. }
  5410. }
  5411. /**
  5412. * cpr3_regulator_init_ctrl_data() - performs initialization of CPR controller
  5413. * elements
  5414. * @ctrl: Pointer to the CPR3 controller
  5415. *
  5416. * Return: 0 on success, errno on failure
  5417. */
  5418. static int cpr3_regulator_init_ctrl_data(struct cpr3_controller *ctrl)
  5419. {
  5420. /* Read the initial vdd voltage from hardware. */
  5421. ctrl->aggr_corner.last_volt
  5422. = regulator_get_voltage(ctrl->vdd_regulator);
  5423. if (ctrl->aggr_corner.last_volt < 0) {
  5424. cpr3_err(ctrl, "regulator_get_voltage(vdd) failed, rc=%d\n",
  5425. ctrl->aggr_corner.last_volt);
  5426. return ctrl->aggr_corner.last_volt;
  5427. }
  5428. ctrl->aggr_corner.open_loop_volt = ctrl->aggr_corner.last_volt;
  5429. return 0;
  5430. }
  5431. /**
  5432. * cpr3_regulator_init_vreg_data() - performs initialization of common CPR3
  5433. * regulator elements and validate aging configurations
  5434. * @vreg: Pointer to the CPR3 regulator
  5435. *
  5436. * Return: 0 on success, errno on failure
  5437. */
  5438. static int cpr3_regulator_init_vreg_data(struct cpr3_regulator *vreg)
  5439. {
  5440. int i, j;
  5441. bool init_aging;
  5442. vreg->current_corner = CPR3_REGULATOR_CORNER_INVALID;
  5443. vreg->last_closed_loop_corner = CPR3_REGULATOR_CORNER_INVALID;
  5444. init_aging = vreg->aging_allowed && vreg->thread->ctrl->aging_required;
  5445. for (i = 0; i < vreg->corner_count; i++) {
  5446. vreg->corner[i].last_volt = vreg->corner[i].open_loop_volt;
  5447. vreg->corner[i].irq_en = CPR3_IRQ_UP | CPR3_IRQ_DOWN;
  5448. vreg->corner[i].ro_mask = 0;
  5449. for (j = 0; j < CPR3_RO_COUNT; j++) {
  5450. if (vreg->corner[i].target_quot[j] == 0)
  5451. vreg->corner[i].ro_mask |= BIT(j);
  5452. }
  5453. if (init_aging) {
  5454. vreg->corner[i].unaged_floor_volt
  5455. = vreg->corner[i].floor_volt;
  5456. vreg->corner[i].unaged_ceiling_volt
  5457. = vreg->corner[i].ceiling_volt;
  5458. vreg->corner[i].unaged_open_loop_volt
  5459. = vreg->corner[i].open_loop_volt;
  5460. }
  5461. if (vreg->aging_allowed) {
  5462. if (vreg->corner[i].unaged_floor_volt <= 0) {
  5463. cpr3_err(vreg, "invalid unaged_floor_volt[%d] = %d\n",
  5464. i, vreg->corner[i].unaged_floor_volt);
  5465. return -EINVAL;
  5466. }
  5467. if (vreg->corner[i].unaged_ceiling_volt <= 0) {
  5468. cpr3_err(vreg, "invalid unaged_ceiling_volt[%d] = %d\n",
  5469. i, vreg->corner[i].unaged_ceiling_volt);
  5470. return -EINVAL;
  5471. }
  5472. if (vreg->corner[i].unaged_open_loop_volt <= 0) {
  5473. cpr3_err(vreg, "invalid unaged_open_loop_volt[%d] = %d\n",
  5474. i, vreg->corner[i].unaged_open_loop_volt);
  5475. return -EINVAL;
  5476. }
  5477. }
  5478. }
  5479. if (vreg->aging_allowed && vreg->corner[vreg->aging_corner].ceiling_volt
  5480. > vreg->thread->ctrl->aging_ref_volt) {
  5481. cpr3_err(vreg, "aging corner %d ceiling voltage = %d > aging ref voltage = %d uV\n",
  5482. vreg->aging_corner,
  5483. vreg->corner[vreg->aging_corner].ceiling_volt,
  5484. vreg->thread->ctrl->aging_ref_volt);
  5485. return -EINVAL;
  5486. }
  5487. return 0;
  5488. }
  5489. /**
  5490. * cpr3_regulator_suspend() - perform common required CPR3 power down steps
  5491. * before the system enters suspend
  5492. * @ctrl: Pointer to the CPR3 controller
  5493. *
  5494. * Return: 0 on success, errno on failure
  5495. */
  5496. int cpr3_regulator_suspend(struct cpr3_controller *ctrl)
  5497. {
  5498. int rc;
  5499. mutex_lock(&ctrl->lock);
  5500. if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPR4) {
  5501. rc = cpr3_ctrl_clear_cpr4_config(ctrl);
  5502. if (rc) {
  5503. cpr3_err(ctrl, "failed to clear CPR4 configuration,rc=%d\n",
  5504. rc);
  5505. mutex_unlock(&ctrl->lock);
  5506. return rc;
  5507. }
  5508. }
  5509. cpr3_ctrl_loop_disable(ctrl);
  5510. if (ctrl->ctrl_type != CPR_CTRL_TYPE_CPRH) {
  5511. rc = cpr3_closed_loop_disable(ctrl);
  5512. if (rc)
  5513. cpr3_err(ctrl, "could not disable CPR, rc=%d\n", rc);
  5514. ctrl->cpr_suspended = true;
  5515. }
  5516. mutex_unlock(&ctrl->lock);
  5517. return 0;
  5518. }
  5519. /**
  5520. * cpr3_regulator_resume() - perform common required CPR3 power up steps after
  5521. * the system resumes from suspend
  5522. * @ctrl: Pointer to the CPR3 controller
  5523. *
  5524. * Return: 0 on success, errno on failure
  5525. */
  5526. int cpr3_regulator_resume(struct cpr3_controller *ctrl)
  5527. {
  5528. int rc;
  5529. mutex_lock(&ctrl->lock);
  5530. if (ctrl->ctrl_type != CPR_CTRL_TYPE_CPRH) {
  5531. ctrl->cpr_suspended = false;
  5532. rc = cpr3_regulator_update_ctrl_state(ctrl);
  5533. if (rc)
  5534. cpr3_err(ctrl, "could not enable CPR, rc=%d\n", rc);
  5535. } else {
  5536. cpr3_ctrl_loop_enable(ctrl);
  5537. }
  5538. mutex_unlock(&ctrl->lock);
  5539. return 0;
  5540. }
  5541. /**
  5542. * cpr3_regulator_cpu_hotplug_callback() - reset CPR IRQ affinity when a CPU is
  5543. * brought online via hotplug
  5544. * @nb: Pointer to the notifier block
  5545. * @action: hotplug action
  5546. * @hcpu: long value corresponding to the CPU number
  5547. *
  5548. * Return: NOTIFY_OK
  5549. */
  5550. static int cpr3_regulator_cpu_hotplug_callback(struct notifier_block *nb,
  5551. unsigned long action, void *hcpu)
  5552. {
  5553. struct cpr3_controller *ctrl = container_of(nb, struct cpr3_controller,
  5554. cpu_hotplug_notifier);
  5555. int cpu = (long)hcpu;
  5556. action &= ~CPU_TASKS_FROZEN;
  5557. if (action == CPU_ONLINE
  5558. && cpumask_test_cpu(cpu, &ctrl->irq_affinity_mask))
  5559. irq_set_affinity(ctrl->irq, &ctrl->irq_affinity_mask);
  5560. return NOTIFY_OK;
  5561. }
  5562. /**
  5563. * cpr3_regulator_validate_controller() - verify the data passed in via the
  5564. * cpr3_controller data structure
  5565. * @ctrl: Pointer to the CPR3 controller
  5566. *
  5567. * Return: 0 on success, errno on failure
  5568. */
  5569. static int cpr3_regulator_validate_controller(struct cpr3_controller *ctrl)
  5570. {
  5571. struct cpr3_thread *thread;
  5572. struct cpr3_regulator *vreg;
  5573. int i, j, allow_boost_vreg_count = 0;
  5574. if (!ctrl->vdd_regulator && ctrl->ctrl_type != CPR_CTRL_TYPE_CPRH) {
  5575. cpr3_err(ctrl, "vdd regulator missing\n");
  5576. return -EINVAL;
  5577. } else if (ctrl->sensor_count <= 0
  5578. || ctrl->sensor_count > CPR3_MAX_SENSOR_COUNT) {
  5579. cpr3_err(ctrl, "invalid CPR sensor count=%d\n",
  5580. ctrl->sensor_count);
  5581. return -EINVAL;
  5582. } else if (!ctrl->sensor_owner) {
  5583. cpr3_err(ctrl, "CPR sensor ownership table missing\n");
  5584. return -EINVAL;
  5585. }
  5586. if (ctrl->aging_required) {
  5587. for (i = 0; i < ctrl->aging_sensor_count; i++) {
  5588. if (ctrl->aging_sensor[i].sensor_id
  5589. >= ctrl->sensor_count) {
  5590. cpr3_err(ctrl, "aging_sensor[%d] id=%u is not in the value range 0-%d",
  5591. i, ctrl->aging_sensor[i].sensor_id,
  5592. ctrl->sensor_count - 1);
  5593. return -EINVAL;
  5594. }
  5595. }
  5596. }
  5597. for (i = 0; i < ctrl->thread_count; i++) {
  5598. thread = &ctrl->thread[i];
  5599. for (j = 0; j < thread->vreg_count; j++) {
  5600. vreg = &thread->vreg[j];
  5601. if (vreg->allow_boost)
  5602. allow_boost_vreg_count++;
  5603. }
  5604. }
  5605. if (allow_boost_vreg_count > 1) {
  5606. /*
  5607. * Boost feature is not allowed to be used for more
  5608. * than one CPR3 regulator of a CPR3 controller.
  5609. */
  5610. cpr3_err(ctrl, "Boost feature is enabled for more than one regulator\n");
  5611. return -EINVAL;
  5612. }
  5613. return 0;
  5614. }
  5615. /**
  5616. * cpr3_panic_callback() - panic notification callback function. This function
  5617. * is invoked when a kernel panic occurs.
  5618. * @nfb: Notifier block pointer of CPR3 controller
  5619. * @event: Value passed unmodified to notifier function
  5620. * @data: Pointer passed unmodified to notifier function
  5621. *
  5622. * Return: NOTIFY_OK
  5623. */
  5624. static int cpr3_panic_callback(struct notifier_block *nfb,
  5625. unsigned long event, void *data)
  5626. {
  5627. struct cpr3_controller *ctrl = container_of(nfb,
  5628. struct cpr3_controller, panic_notifier);
  5629. struct cpr3_panic_regs_info *regs_info = ctrl->panic_regs_info;
  5630. struct cpr3_reg_info *reg;
  5631. int i = 0;
  5632. for (i = 0; i < regs_info->reg_count; i++) {
  5633. reg = &(regs_info->regs[i]);
  5634. reg->value = readl_relaxed(reg->virt_addr);
  5635. pr_err("%s[0x%08x] = 0x%08x\n", reg->name, reg->addr,
  5636. reg->value);
  5637. }
  5638. /*
  5639. * Barrier to ensure that the information has been updated in the
  5640. * structure.
  5641. */
  5642. mb();
  5643. return NOTIFY_OK;
  5644. }
  5645. /**
  5646. * cpr3_regulator_register() - register the regulators for a CPR3 controller and
  5647. * perform CPR hardware initialization
  5648. * @pdev: Platform device pointer for the CPR3 controller
  5649. * @ctrl: Pointer to the CPR3 controller
  5650. *
  5651. * Return: 0 on success, errno on failure
  5652. */
  5653. int cpr3_regulator_register(struct platform_device *pdev,
  5654. struct cpr3_controller *ctrl)
  5655. {
  5656. struct device *dev = &pdev->dev;
  5657. struct resource *res;
  5658. int i, j, rc;
  5659. if (!dev->of_node) {
  5660. dev_err(dev, "%s: Device tree node is missing\n", __func__);
  5661. return -EINVAL;
  5662. }
  5663. if (!ctrl || !ctrl->name) {
  5664. dev_err(dev, "%s: CPR controller data is missing\n", __func__);
  5665. return -EINVAL;
  5666. }
  5667. rc = cpr3_regulator_validate_controller(ctrl);
  5668. if (rc) {
  5669. cpr3_err(ctrl, "controller validation failed, rc=%d\n", rc);
  5670. return rc;
  5671. }
  5672. mutex_init(&ctrl->lock);
  5673. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cpr_ctrl");
  5674. if (!res || !res->start) {
  5675. cpr3_err(ctrl, "CPR controller address is missing\n");
  5676. return -ENXIO;
  5677. }
  5678. ctrl->cpr_ctrl_base = devm_ioremap(dev, res->start, resource_size(res));
  5679. if (cpr3_regulator_cprh_initialized(ctrl)) {
  5680. cpr3_err(ctrl, "CPRh controller already initialized by boot loader\n");
  5681. return -EPERM;
  5682. }
  5683. if (ctrl->aging_possible_mask) {
  5684. /*
  5685. * Aging possible register address is required if an aging
  5686. * possible mask has been specified.
  5687. */
  5688. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  5689. "aging_allowed");
  5690. if (!res || !res->start) {
  5691. cpr3_err(ctrl, "CPR aging allowed address is missing\n");
  5692. return -ENXIO;
  5693. }
  5694. ctrl->aging_possible_reg = devm_ioremap(dev, res->start,
  5695. resource_size(res));
  5696. }
  5697. if (ctrl->ctrl_type != CPR_CTRL_TYPE_CPRH) {
  5698. ctrl->irq = platform_get_irq_byname(pdev, "cpr");
  5699. if (ctrl->irq < 0) {
  5700. cpr3_err(ctrl, "missing CPR interrupt\n");
  5701. return ctrl->irq;
  5702. }
  5703. }
  5704. if (ctrl->supports_hw_closed_loop) {
  5705. rc = cpr3_regulator_init_hw_closed_loop_dependencies(pdev,
  5706. ctrl);
  5707. if (rc)
  5708. return rc;
  5709. if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPR3) {
  5710. ctrl->ceiling_irq = platform_get_irq_byname(pdev,
  5711. "ceiling");
  5712. if (ctrl->ceiling_irq < 0) {
  5713. cpr3_err(ctrl, "missing ceiling interrupt\n");
  5714. return ctrl->ceiling_irq;
  5715. }
  5716. }
  5717. }
  5718. if (ctrl->ctrl_type != CPR_CTRL_TYPE_CPRH) {
  5719. rc = cpr3_regulator_init_ctrl_data(ctrl);
  5720. if (rc) {
  5721. cpr3_err(ctrl, "CPR controller data initialization failed, rc=%d\n",
  5722. rc);
  5723. return rc;
  5724. }
  5725. }
  5726. for (i = 0; i < ctrl->thread_count; i++) {
  5727. for (j = 0; j < ctrl->thread[i].vreg_count; j++) {
  5728. rc = cpr3_regulator_init_vreg_data(
  5729. &ctrl->thread[i].vreg[j]);
  5730. if (rc)
  5731. return rc;
  5732. cpr3_print_quots(&ctrl->thread[i].vreg[j]);
  5733. }
  5734. }
  5735. /*
  5736. * Add the maximum possible aging voltage margin until it is possible
  5737. * to perform an aging measurement.
  5738. */
  5739. if (ctrl->aging_required)
  5740. cpr3_regulator_set_aging_ref_adjustment(ctrl, INT_MAX);
  5741. rc = cpr3_regulator_init_ctrl(ctrl);
  5742. if (rc) {
  5743. cpr3_err(ctrl, "CPR controller initialization failed, rc=%d\n",
  5744. rc);
  5745. return rc;
  5746. }
  5747. /* Register regulator devices for all threads. */
  5748. for (i = 0; i < ctrl->thread_count; i++) {
  5749. for (j = 0; j < ctrl->thread[i].vreg_count; j++) {
  5750. rc = cpr3_regulator_vreg_register(
  5751. &ctrl->thread[i].vreg[j]);
  5752. if (rc) {
  5753. cpr3_err(&ctrl->thread[i].vreg[j], "failed to register regulator, rc=%d\n",
  5754. rc);
  5755. goto free_regulators;
  5756. }
  5757. }
  5758. }
  5759. if (ctrl->ctrl_type != CPR_CTRL_TYPE_CPRH) {
  5760. rc = devm_request_threaded_irq(dev, ctrl->irq, NULL,
  5761. cpr3_irq_handler,
  5762. IRQF_ONESHOT |
  5763. IRQF_TRIGGER_RISING,
  5764. "cpr3", ctrl);
  5765. if (rc) {
  5766. cpr3_err(ctrl, "could not request IRQ %d, rc=%d\n",
  5767. ctrl->irq, rc);
  5768. goto free_regulators;
  5769. }
  5770. }
  5771. if (ctrl->supports_hw_closed_loop &&
  5772. ctrl->ctrl_type == CPR_CTRL_TYPE_CPR3) {
  5773. rc = devm_request_threaded_irq(dev, ctrl->ceiling_irq, NULL,
  5774. cpr3_ceiling_irq_handler,
  5775. IRQF_ONESHOT | IRQF_TRIGGER_RISING,
  5776. "cpr3_ceiling", ctrl);
  5777. if (rc) {
  5778. cpr3_err(ctrl, "could not request ceiling IRQ %d, rc=%d\n",
  5779. ctrl->ceiling_irq, rc);
  5780. goto free_regulators;
  5781. }
  5782. }
  5783. if (ctrl->irq && !cpumask_empty(&ctrl->irq_affinity_mask)) {
  5784. irq_set_affinity(ctrl->irq, &ctrl->irq_affinity_mask);
  5785. ctrl->cpu_hotplug_notifier.notifier_call
  5786. = cpr3_regulator_cpu_hotplug_callback;
  5787. register_hotcpu_notifier(&ctrl->cpu_hotplug_notifier);
  5788. }
  5789. mutex_lock(&cpr3_controller_list_mutex);
  5790. cpr3_regulator_debugfs_ctrl_add(ctrl);
  5791. list_add(&ctrl->list, &cpr3_controller_list);
  5792. mutex_unlock(&cpr3_controller_list_mutex);
  5793. if (ctrl->panic_regs_info) {
  5794. /* Register panic notification call back */
  5795. ctrl->panic_notifier.notifier_call = cpr3_panic_callback;
  5796. atomic_notifier_chain_register(&panic_notifier_list,
  5797. &ctrl->panic_notifier);
  5798. }
  5799. return 0;
  5800. free_regulators:
  5801. for (i = 0; i < ctrl->thread_count; i++)
  5802. for (j = 0; j < ctrl->thread[i].vreg_count; j++)
  5803. if (!IS_ERR_OR_NULL(ctrl->thread[i].vreg[j].rdev))
  5804. regulator_unregister(
  5805. ctrl->thread[i].vreg[j].rdev);
  5806. return rc;
  5807. }
  5808. /**
  5809. * cpr3_regulator_unregister() - unregister the regulators for a CPR3 controller
  5810. * and perform CPR hardware shutdown
  5811. * @ctrl: Pointer to the CPR3 controller
  5812. *
  5813. * Return: 0 on success, errno on failure
  5814. */
  5815. int cpr3_regulator_unregister(struct cpr3_controller *ctrl)
  5816. {
  5817. int i, j, rc = 0;
  5818. mutex_lock(&cpr3_controller_list_mutex);
  5819. list_del(&ctrl->list);
  5820. cpr3_regulator_debugfs_ctrl_remove(ctrl);
  5821. mutex_unlock(&cpr3_controller_list_mutex);
  5822. if (ctrl->irq && !cpumask_empty(&ctrl->irq_affinity_mask))
  5823. unregister_hotcpu_notifier(&ctrl->cpu_hotplug_notifier);
  5824. if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPR4) {
  5825. rc = cpr3_ctrl_clear_cpr4_config(ctrl);
  5826. if (rc)
  5827. cpr3_err(ctrl, "failed to clear CPR4 configuration,rc=%d\n",
  5828. rc);
  5829. }
  5830. cpr3_ctrl_loop_disable(ctrl);
  5831. cpr3_closed_loop_disable(ctrl);
  5832. if (ctrl->vdd_limit_regulator) {
  5833. regulator_disable(ctrl->vdd_limit_regulator);
  5834. if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPR3)
  5835. msm_spm_avs_disable_irq(0, MSM_SPM_AVS_IRQ_MAX);
  5836. }
  5837. for (i = 0; i < ctrl->thread_count; i++)
  5838. for (j = 0; j < ctrl->thread[i].vreg_count; j++)
  5839. regulator_unregister(ctrl->thread[i].vreg[j].rdev);
  5840. if (ctrl->panic_notifier.notifier_call)
  5841. atomic_notifier_chain_unregister(&panic_notifier_list,
  5842. &ctrl->panic_notifier);
  5843. return 0;
  5844. }