mt_cpufreq.c 143 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441
  1. /**
  2. * @file mt_cpufreq.c
  3. * @brief Driver for CPU DVFS
  4. *
  5. */
  6. #define __MT_CPUFREQ_C__
  7. #define DEBUG 1
  8. /*=============================================================*/
  9. /* Include files */
  10. /*=============================================================*/
  11. /* system includes */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/sched.h>
  15. #include <linux/init.h>
  16. #include <linux/cpu.h>
  17. #include <linux/cpufreq.h>
  18. #include <linux/delay.h>
  19. #include <linux/slab.h>
  20. #include <linux/proc_fs.h>
  21. #include <linux/miscdevice.h>
  22. #include <linux/platform_device.h>
  23. #ifdef CONFIG_HAS_EARLYSUSPEND
  24. #include <linux/earlysuspend.h>
  25. #else
  26. #include <linux/notifier.h>
  27. #include <linux/fb.h>
  28. #endif
  29. #include <linux/spinlock.h>
  30. #include <linux/kthread.h>
  31. #include <linux/hrtimer.h>
  32. #include <linux/ktime.h>
  33. #include <linux/jiffies.h>
  34. #include <linux/bitops.h>
  35. #include <linux/uaccess.h>
  36. #include <linux/seq_file.h>
  37. #include <asm/io.h>
  38. #ifdef CONFIG_OF
  39. #include <linux/of.h>
  40. #include <linux/of_address.h>
  41. #endif
  42. #include "mt-plat/aee.h"
  43. /* project includes */
  44. #include "mach/mt_thermal.h"
  45. #include <mt-plat/mt_hotplug_strategy.h>
  46. /*#include "mach/mt_spm_idle.h"*/
  47. #include "mach/mt_clkmgr.h"
  48. #include "mach/mt_freqhopping.h"
  49. #include "mt_ptp.h"
  50. #include "mt_static_power.h"
  51. #include "mach/mt_pbm.h"
  52. #include "mt-plat/mt_devinfo.h"
  53. #ifndef __KERNEL__
  54. #include "freqhop_sw.h"
  55. #include "mt_spm.h"
  56. #include "pmic.h"
  57. #include "mt_pmic_wrap.h"
  58. #include "efuse.h" /* for SLT efuse check */
  59. #else
  60. #include "mt_spm.h"
  61. #include "mt-plat/upmu_common.h"
  62. #include "mach/upmu_sw.h"
  63. #include "mach/upmu_hw.h"
  64. /* #include "pwrap_hal.h" */
  65. #endif
  66. /* local includes */
  67. #include "mt_cpufreq.h"
  68. /*=============================================================*/
  69. /* Macro definition */
  70. /*=============================================================*/
  71. #if defined(CONFIG_ARCH_MT6735M)
  72. #define PMIC_WRAP_DVFS_ADR0 ((unsigned long)(PMIC_WRAP_BASE+0xE8))
  73. #define PMIC_WRAP_DVFS_WDATA0 ((unsigned long)(PMIC_WRAP_BASE+0xEC))
  74. #define PMIC_WRAP_DVFS_ADR1 ((unsigned long)(PMIC_WRAP_BASE+0xF0))
  75. #define PMIC_WRAP_DVFS_WDATA1 ((unsigned long)(PMIC_WRAP_BASE+0xF4))
  76. #define PMIC_WRAP_DVFS_ADR2 ((unsigned long)(PMIC_WRAP_BASE+0xF8))
  77. #define PMIC_WRAP_DVFS_WDATA2 ((unsigned long)(PMIC_WRAP_BASE+0xFC))
  78. #define PMIC_WRAP_DVFS_ADR3 ((unsigned long)(PMIC_WRAP_BASE+0x100))
  79. #define PMIC_WRAP_DVFS_WDATA3 ((unsigned long)(PMIC_WRAP_BASE+0x104))
  80. #define PMIC_WRAP_DVFS_ADR4 ((unsigned long)(PMIC_WRAP_BASE+0x108))
  81. #define PMIC_WRAP_DVFS_WDATA4 ((unsigned long)(PMIC_WRAP_BASE+0x10C))
  82. #define PMIC_WRAP_DVFS_ADR5 ((unsigned long)(PMIC_WRAP_BASE+0x110))
  83. #define PMIC_WRAP_DVFS_WDATA5 ((unsigned long)(PMIC_WRAP_BASE+0x114))
  84. #define PMIC_WRAP_DVFS_ADR6 ((unsigned long)(PMIC_WRAP_BASE+0x118))
  85. #define PMIC_WRAP_DVFS_WDATA6 ((unsigned long)(PMIC_WRAP_BASE+0x11C))
  86. #define PMIC_WRAP_DVFS_ADR7 ((unsigned long)(PMIC_WRAP_BASE+0x120))
  87. #define PMIC_WRAP_DVFS_WDATA7 ((unsigned long)(PMIC_WRAP_BASE+0x124))
  88. #define PMIC_WRAP_DVFS_ADR8 ((unsigned long)(PMIC_WRAP_BASE+0x128))
  89. #define PMIC_WRAP_DVFS_WDATA8 ((unsigned long)(PMIC_WRAP_BASE+0x12C))
  90. #define PMIC_WRAP_DVFS_ADR9 ((unsigned long)(PMIC_WRAP_BASE+0x130))
  91. #define PMIC_WRAP_DVFS_WDATA9 ((unsigned long)(PMIC_WRAP_BASE+0x134))
  92. #define PMIC_WRAP_DVFS_ADR10 ((unsigned long)(PMIC_WRAP_BASE+0x138))
  93. #define PMIC_WRAP_DVFS_WDATA10 ((unsigned long)(PMIC_WRAP_BASE+0x13C))
  94. #define PMIC_WRAP_DVFS_ADR11 ((unsigned long)(PMIC_WRAP_BASE+0x140))
  95. #define PMIC_WRAP_DVFS_WDATA11 ((unsigned long)(PMIC_WRAP_BASE+0x144))
  96. #define PMIC_WRAP_DVFS_ADR12 ((unsigned long)(PMIC_WRAP_BASE+0x148))
  97. #define PMIC_WRAP_DVFS_WDATA12 ((unsigned long)(PMIC_WRAP_BASE+0x14C))
  98. #define PMIC_WRAP_DVFS_ADR13 ((unsigned long)(PMIC_WRAP_BASE+0x150))
  99. #define PMIC_WRAP_DVFS_WDATA13 ((unsigned long)(PMIC_WRAP_BASE+0x154))
  100. #define PMIC_WRAP_DVFS_ADR14 ((unsigned long)(PMIC_WRAP_BASE+0x158))
  101. #define PMIC_WRAP_DVFS_WDATA14 ((unsigned long)(PMIC_WRAP_BASE+0x15C))
  102. #define PMIC_WRAP_DVFS_ADR15 ((unsigned long)(PMIC_WRAP_BASE+0x160))
  103. #define PMIC_WRAP_DVFS_WDATA15 ((unsigned long)(PMIC_WRAP_BASE+0x164))
  104. #else
  105. #define PMIC_WRAP_DVFS_ADR0 ((unsigned long)(PMIC_WRAP_BASE+0xFC))
  106. #define PMIC_WRAP_DVFS_WDATA0 ((unsigned long)(PMIC_WRAP_BASE+0x100))
  107. #define PMIC_WRAP_DVFS_ADR1 ((unsigned long)(PMIC_WRAP_BASE+0x104))
  108. #define PMIC_WRAP_DVFS_WDATA1 ((unsigned long)(PMIC_WRAP_BASE+0x108))
  109. #define PMIC_WRAP_DVFS_ADR2 ((unsigned long)(PMIC_WRAP_BASE+0x10C))
  110. #define PMIC_WRAP_DVFS_WDATA2 ((unsigned long)(PMIC_WRAP_BASE+0x110))
  111. #define PMIC_WRAP_DVFS_ADR3 ((unsigned long)(PMIC_WRAP_BASE+0x114))
  112. #define PMIC_WRAP_DVFS_WDATA3 ((unsigned long)(PMIC_WRAP_BASE+0x118))
  113. #define PMIC_WRAP_DVFS_ADR4 ((unsigned long)(PMIC_WRAP_BASE+0x11C))
  114. #define PMIC_WRAP_DVFS_WDATA4 ((unsigned long)(PMIC_WRAP_BASE+0x120))
  115. #define PMIC_WRAP_DVFS_ADR5 ((unsigned long)(PMIC_WRAP_BASE+0x124))
  116. #define PMIC_WRAP_DVFS_WDATA5 ((unsigned long)(PMIC_WRAP_BASE+0x128))
  117. #define PMIC_WRAP_DVFS_ADR6 ((unsigned long)(PMIC_WRAP_BASE+0x12C))
  118. #define PMIC_WRAP_DVFS_WDATA6 ((unsigned long)(PMIC_WRAP_BASE+0x130))
  119. #define PMIC_WRAP_DVFS_ADR7 ((unsigned long)(PMIC_WRAP_BASE+0x134))
  120. #define PMIC_WRAP_DVFS_WDATA7 ((unsigned long)(PMIC_WRAP_BASE+0x138))
  121. #define PMIC_WRAP_DVFS_ADR8 ((unsigned long)(PMIC_WRAP_BASE+0x13C))
  122. #define PMIC_WRAP_DVFS_WDATA8 ((unsigned long)(PMIC_WRAP_BASE+0x140))
  123. #define PMIC_WRAP_DVFS_ADR9 ((unsigned long)(PMIC_WRAP_BASE+0x144))
  124. #define PMIC_WRAP_DVFS_WDATA9 ((unsigned long)(PMIC_WRAP_BASE+0x148))
  125. #define PMIC_WRAP_DVFS_ADR10 ((unsigned long)(PMIC_WRAP_BASE+0x14C))
  126. #define PMIC_WRAP_DVFS_WDATA10 ((unsigned long)(PMIC_WRAP_BASE+0x150))
  127. #define PMIC_WRAP_DVFS_ADR11 ((unsigned long)(PMIC_WRAP_BASE+0x154))
  128. #define PMIC_WRAP_DVFS_WDATA11 ((unsigned long)(PMIC_WRAP_BASE+0x158))
  129. #define PMIC_WRAP_DVFS_ADR12 ((unsigned long)(PMIC_WRAP_BASE+0x15C))
  130. #define PMIC_WRAP_DVFS_WDATA12 ((unsigned long)(PMIC_WRAP_BASE+0x160))
  131. #define PMIC_WRAP_DVFS_ADR13 ((unsigned long)(PMIC_WRAP_BASE+0x164))
  132. #define PMIC_WRAP_DVFS_WDATA13 ((unsigned long)(PMIC_WRAP_BASE+0x168))
  133. #define PMIC_WRAP_DVFS_ADR14 ((unsigned long)(PMIC_WRAP_BASE+0x16C))
  134. #define PMIC_WRAP_DVFS_WDATA14 ((unsigned long)(PMIC_WRAP_BASE+0x170))
  135. #define PMIC_WRAP_DVFS_ADR15 ((unsigned long)(PMIC_WRAP_BASE+0x174))
  136. #define PMIC_WRAP_DVFS_WDATA15 ((unsigned long)(PMIC_WRAP_BASE+0x178))
  137. #endif
  138. /* Operations and REG Access */
  139. #define MAX(a, b) ((a) >= (b) ? (a) : (b))
  140. #define MIN(a, b) ((a) >= (b) ? (b) : (a))
  141. #define _BIT_(_bit_) (unsigned)(1 << (_bit_))
  142. #define _BITS_(_bits_, _val_) \
  143. ((((unsigned) -1 >> (31 - ((1) ? _bits_))) & ~((1U << ((0) ? _bits_)) - 1)) & ((_val_)<<((0) ? _bits_)))
  144. #define _BITMASK_(_bits_) \
  145. (((unsigned) -1 >> (31 - ((1) ? _bits_))) & ~((1U << ((0) ? _bits_)) - 1))
  146. #define _GET_BITS_VAL_(_bits_, _val_) (((_val_) & (_BITMASK_(_bits_))) >> ((0) ? _bits_))
  147. #define cpufreq_read(addr) __raw_readl(addr)
  148. #define cpufreq_write(addr, val) mt_reg_sync_writel(val, addr)
  149. #define cpufreq_write_mask(addr, mask, val) \
  150. cpufreq_write(addr, (cpufreq_read(addr) & ~(_BITMASK_(mask))) | _BITS_(mask, val))
  151. #define for_each_cpu_dvfs(i, p) for (i = 0, p = cpu_dvfs; i < NR_MT_CPU_DVFS; i++, p = &cpu_dvfs[i])
  152. #define cpu_dvfs_is(p, id) (p == &cpu_dvfs[id])
  153. #define cpu_dvfs_is_available(p) (p->opp_tbl)
  154. #define cpu_dvfs_get_name(p) (p->name)
  155. #define cpu_dvfs_get_cur_freq(p) (p->opp_tbl[p->idx_opp_tbl].cpufreq_khz)
  156. #define cpu_dvfs_get_freq_by_idx(p, idx) (p->opp_tbl[idx].cpufreq_khz)
  157. #define cpu_dvfs_get_max_freq(p) (p->opp_tbl[0].cpufreq_khz)
  158. #define cpu_dvfs_get_normal_max_freq(p) (p->opp_tbl[p->idx_normal_max_opp].cpufreq_khz)
  159. #define cpu_dvfs_get_min_freq(p) (p->opp_tbl[p->nr_opp_tbl - 1].cpufreq_khz)
  160. #define cpu_dvfs_get_cur_volt(p) (p->opp_tbl[p->idx_opp_tbl].cpufreq_volt)
  161. #define cpu_dvfs_get_volt_by_idx(p, idx) (p->opp_tbl[idx].cpufreq_volt)
  162. #ifdef CONFIG_CPU_DVFS_HAS_EXTBUCK
  163. #define cpu_dvfs_is_extbuck_valid() (is_ext_buck_exist() && is_ext_buck_sw_ready())
  164. /* used @ _mt_cpufreq_set_cur_volt_extBuck() for SW tracking */
  165. #define NORMAL_DIFF_VSRAM_VPROC (10000) /* 100mv * 100 */
  166. #define MAX_DIFF_VSRAM_VPROC (20000) /* 200mv * 100 */
  167. #endif
  168. #define MIN_VSRAM_VOLT (93125) /* 931.25mv * 100 */
  169. #define MAX_VSRAM_VOLT (131250) /* 1312.5mv * 100 */
  170. #define MAX_VPROC_VOLT (125000) /* 1250mv * 100 */
  171. /* PMIC/PLL settle time (us), should not be changed */
  172. #define PMIC_CMD_DELAY_TIME (5)
  173. #define MIN_PMIC_SETTLE_TIME (25)
  174. #define PMIC_VOLT_UP_SETTLE_TIME(old_volt, new_volt) \
  175. (((((new_volt) - (old_volt)) + 1250 - 1) / 1250) + PMIC_CMD_DELAY_TIME)
  176. #define PMIC_VOLT_DOWN_SETTLE_TIME(old_volt, new_volt) \
  177. (((((old_volt) - (new_volt)) * 2) / 625) + PMIC_CMD_DELAY_TIME)
  178. #define PLL_SETTLE_TIME (20)
  179. /* PMIC wrapper */
  180. #define VOLT_TO_PMIC_VAL(volt) (((volt) - 60000 + 625 - 1) / 625)
  181. #define PMIC_VAL_TO_VOLT(pmic) (((pmic) * 625) + 60000)
  182. #ifdef CONFIG_CPU_DVFS_HAS_EXTBUCK
  183. #define VOLT_TO_EXTBUCK_VAL(volt) VOLT_TO_PMIC_VAL(volt) /* (((((volt) - 300) + 9) / 10) & 0x7F) */
  184. #define EXTBUCK_VAL_TO_VOLT(val) PMIC_VAL_TO_VOLT(val) /* (300 + ((val) & 0x7F) * 10) */
  185. #endif
  186. #define NR_PMIC_WRAP_CMD 16 /* num of pmic wrap cmd (fixed value) */
  187. /* DVFS OPP table */
  188. #ifdef CONFIG_ARCH_MT6735M
  189. #define CPU_DVFS_FREQ0_1 (1248000) /* KHz */
  190. #define CPU_DVFS_FREQ0 (1144000) /* KHz */
  191. #define CPU_DVFS_FREQ1_1 (1092000) /* KHz */
  192. #define CPU_DVFS_FREQ1 (1027000) /* KHz */
  193. #define CPU_DVFS_FREQ2 (988000) /* KHz */
  194. #define CPU_DVFS_FREQ3 (923000) /* KHz */
  195. #define CPU_DVFS_FREQ4 (910000) /* KHz */
  196. #define CPU_DVFS_FREQ5 (858000) /* KHz */
  197. #define CPU_DVFS_FREQ6 (793000) /* KHz */
  198. #define CPU_DVFS_FREQ7 (637000) /* KHz */
  199. #define CPU_DVFS_FREQ8 (494000) /* KHz */
  200. #define CPU_DVFS_FREQ9 (364000) /* KHz */
  201. #define CPU_DVFS_FREQ10 (221000) /* KHz */
  202. #define CPUFREQ_BOUNDARY_FOR_FHCTL (CPU_DVFS_FREQ1)
  203. #define CPUFREQ_FIX_FREQ_FOR_ES (CPU_DVFS_FREQ2)
  204. #else
  205. #define CPU_DVFS_FREQ0 (1495000) /* KHz */
  206. #define CPU_DVFS_FREQ0_1 (1443000) /* KHz */
  207. #define CPU_DVFS_FREQ1 (1300000) /* KHz */
  208. #define CPU_DVFS_FREQ2 (1235000) /* KHz */
  209. #define CPU_DVFS_FREQ3 (1170000) /* KHz */
  210. #define CPU_DVFS_FREQ3_1 (1144000) /* KHz (for 6753 FY)*/
  211. #define CPU_DVFS_FREQ3_2 (1092000) /* KHz (for 37T to 35M+)*/
  212. #define CPU_DVFS_FREQ4 (1040000) /* KHz */
  213. #define CPU_DVFS_FREQ4_1 (988000) /* KHz (for 37T to 35M+)*/
  214. #define CPU_DVFS_FREQ4_2 (858000) /* KHz (for 37T to 35M+)*/
  215. #define CPU_DVFS_FREQ5 (819000) /* KHz */
  216. #define CPU_DVFS_FREQ5_1 (793000) /* KHz (for 37T to 35M+)*/
  217. #define CPU_DVFS_FREQ5_2 (637000) /* KHz (for 37T to 35M+)*/
  218. #define CPU_DVFS_FREQ6 (598000) /* KHz */
  219. #define CPU_DVFS_FREQ6_1 (494000) /* KHz (for 37T to 35M+)*/
  220. #define CPU_DVFS_FREQ7 (442000) /* KHz */
  221. #define CPU_DVFS_FREQ7_1 (364000) /* KHz (for 37T to 35M+)*/
  222. #define CPU_DVFS_FREQ8 (299000) /* KHz */
  223. #define CPU_DVFS_FREQ8_1 (221000) /* KHz (for 37T to 35M+)*/
  224. #define CPUFREQ_BOUNDARY_FOR_FHCTL (CPU_DVFS_FREQ4) /* if cross 1040MHz when DFS, don't used FHCTL */
  225. #define CPUFREQ_FIX_FREQ_FOR_ES (CPU_DVFS_FREQ4)
  226. #endif
  227. #define OP(khz, volt) { \
  228. .cpufreq_khz = khz, \
  229. .cpufreq_volt = volt, \
  230. .cpufreq_volt_org = volt, \
  231. }
  232. /* DDS calculation */
  233. #define PLL_FREQ_STEP (13000) /* KHz */
  234. #define PLL_DIV1_FREQ (1001000) /* KHz */
  235. #define PLL_DIV2_FREQ (520000) /* KHz */
  236. #define PLL_DIV4_FREQ (260000) /* KHz */
  237. #define PLL_DIV8_FREQ (130000) /* KHz */
  238. #define DDS_DIV1_FREQ (0x0009A000) /* 1001MHz */
  239. #define DDS_DIV2_FREQ (0x010A0000) /* 520MHz */
  240. #define DDS_DIV4_FREQ (0x020A0000) /* 260MHz */
  241. #define DDS_DIV8_FREQ (0x030A0000) /* 130MHz */
  242. /* Turbo mode */
  243. #ifdef CONFIG_CPU_DVFS_TURBO_MODE
  244. #define TURBO_MODE_BOUNDARY_CPU_NUM 2
  245. #define TURBO_MODE_FREQ(mode, freq) \
  246. (((freq * (100 + turbo_mode_cfg[mode].freq_delta)) / PLL_FREQ_STEP) / 100 * PLL_FREQ_STEP)
  247. #define TURBO_MODE_VOLT(mode, volt) (volt + turbo_mode_cfg[mode].volt_delta)
  248. /* idx sort by temp from low to high */
  249. enum turbo_mode {
  250. TURBO_MODE_2,
  251. TURBO_MODE_1,
  252. TURBO_MODE_NONE,
  253. NR_TURBO_MODE,
  254. };
  255. #endif
  256. /* Power table */
  257. /* Notice: Each table MUST has 8 element to avoid ptpod error */
  258. #define NR_MAX_OPP_TBL 8
  259. #ifdef CONFIG_ARCH_MT6753
  260. #define NR_MAX_CPU 8
  261. #else
  262. #define NR_MAX_CPU 4
  263. #endif
  264. /* Power throttling */
  265. #ifdef CONFIG_CPU_DVFS_POWER_THROTTLING
  266. #define PWR_THRO_MODE_LBAT_819MHZ BIT(0)
  267. #define PWR_THRO_MODE_BAT_PER_819MHZ BIT(1)
  268. #define PWR_THRO_MODE_BAT_OC_1040MHZ BIT(2)
  269. #endif
  270. /* PMIC 5A throttle (only for MT6753) */
  271. #ifdef CONFIG_ARCH_MT6753
  272. #define PMIC_5A_THRO_MAX_CPU_CORE_NUM 6
  273. #define PMIC_5A_THRO_MAX_CPU_FREQ 1144000
  274. #endif
  275. /* Debugging */
  276. #undef TAG
  277. #define TAG "[Power/cpufreq] "
  278. #define cpufreq_err(fmt, args...) \
  279. pr_err(TAG"[ERROR]"fmt, ##args)
  280. #define cpufreq_warn(fmt, args...) \
  281. pr_warn(TAG"[WARNING]"fmt, ##args)
  282. #define cpufreq_info(fmt, args...) \
  283. pr_warn(TAG""fmt, ##args)
  284. #define cpufreq_dbg(fmt, args...) \
  285. pr_debug(TAG""fmt, ##args)
  286. #define cpufreq_ver(fmt, args...) \
  287. do { \
  288. if (func_lv_mask) \
  289. cpufreq_info(fmt, ##args); \
  290. } while (0)
  291. #define FUNC_LV_MODULE BIT(0) /* module, platform driver interface */
  292. #define FUNC_LV_CPUFREQ BIT(1) /* cpufreq driver interface */
  293. #define FUNC_LV_API BIT(2) /* mt_cpufreq driver global function */
  294. #define FUNC_LV_LOCAL BIT(3) /* mt_cpufreq driver local function */
  295. #define FUNC_LV_HELP BIT(4) /* mt_cpufreq driver help function */
  296. #define FUNC_ENTER(lv) \
  297. do { if ((lv) & func_lv_mask) cpufreq_dbg(">> %s()\n", __func__); } while (0)
  298. #define FUNC_EXIT(lv) \
  299. do { if ((lv) & func_lv_mask) cpufreq_dbg("<< %s():%d\n", __func__, __LINE__); } while (0)
  300. /* Lock */
  301. #define cpufreq_lock(flags) \
  302. do { \
  303. /* to fix compile warning */ \
  304. flags = (unsigned long)&flags; \
  305. mutex_lock(&cpufreq_mutex); \
  306. is_in_cpufreq = 1;\
  307. /* spm_mcdi_wakeup_all_cores(); */ \
  308. } while (0)
  309. #define cpufreq_unlock(flags) \
  310. do { \
  311. /* to fix compile warning */ \
  312. flags = (unsigned long)&flags; \
  313. is_in_cpufreq = 0;\
  314. mutex_unlock(&cpufreq_mutex); \
  315. } while (0)
  316. #if 1
  317. static DEFINE_SPINLOCK(pmic_wrap_lock);
  318. #define pmic_wrap_lock(flags) spin_lock_irqsave(&pmic_wrap_lock, flags)
  319. #define pmic_wrap_unlock(flags) spin_unlock_irqrestore(&pmic_wrap_lock, flags)
  320. #else
  321. #define pmic_wrap_lock(flags) \
  322. do { \
  323. /* to fix compile warning */ \
  324. flags = (unsigned long)&flags; \
  325. mutex_lock(&pmic_wrap_mutex); \
  326. } while (0)
  327. #define pmic_wrap_unlock(flags) \
  328. do { \
  329. /* to fix compile warning */ \
  330. flags = (unsigned long)&flags; \
  331. mutex_unlock(&pmic_wrap_mutex); \
  332. } while (0)
  333. #endif
  334. /* EFUSE */
  335. #define CPUFREQ_EFUSE_INDEX (3)
  336. /* #define FUNC_CODE_EFUSE_INDEX (28) */
  337. #define CPU_LEVEL_0 (0x0)
  338. #define CPU_LEVEL_1 (0x1)
  339. #define CPU_LEVEL_2 (0x2)
  340. #define CPU_LEVEL_3 (0x3)
  341. #define CPU_LEVEL_4 (0x4)
  342. /* SRAM debugging */
  343. #ifdef CONFIG_CPU_DVFS_AEE_RR_REC
  344. enum cpu_dvfs_state {
  345. CPU_DVFS_LITTLE_IS_DOING_DVFS = 0,
  346. CPU_DVFS_LITTLE_IS_TURBO,
  347. };
  348. #endif
  349. /*=============================================================*/
  350. /* Local type definition */
  351. /*=============================================================*/
  352. struct pmic_wrap_cmd {
  353. unsigned long cmd_addr;
  354. unsigned long cmd_wdata;
  355. };
  356. struct pmic_wrap_setting {
  357. enum pmic_wrap_phase_id phase;
  358. struct pmic_wrap_cmd addr[NR_PMIC_WRAP_CMD];
  359. struct {
  360. struct {
  361. unsigned long cmd_addr;
  362. unsigned long cmd_wdata;
  363. } _[NR_PMIC_WRAP_CMD];
  364. const int nr_idx;
  365. } set[NR_PMIC_WRAP_PHASE];
  366. };
  367. struct mt_cpu_freq_info {
  368. const unsigned int cpufreq_khz;
  369. unsigned int cpufreq_volt; /* mv * 100 */
  370. const unsigned int cpufreq_volt_org; /* mv * 100 */
  371. };
  372. struct mt_cpu_power_info {
  373. unsigned int cpufreq_khz;
  374. unsigned int cpufreq_ncpu;
  375. unsigned int cpufreq_power;
  376. };
  377. struct mt_cpu_dvfs {
  378. const char *name;
  379. unsigned int cpu_id; /* for cpufreq */
  380. unsigned int cpu_level;
  381. struct mt_cpu_dvfs_ops *ops;
  382. /* opp (freq) table */
  383. struct mt_cpu_freq_info *opp_tbl; /* OPP table */
  384. int nr_opp_tbl; /* size for OPP table */
  385. int idx_opp_tbl; /* current OPP idx */
  386. int idx_normal_max_opp; /* idx for normal max OPP */
  387. int idx_opp_tbl_for_late_resume; /* keep the setting for late resume */
  388. struct cpufreq_frequency_table *freq_tbl_for_cpufreq; /* freq table for cpufreq */
  389. /* power table */
  390. struct mt_cpu_power_info *power_tbl;
  391. unsigned int nr_power_tbl;
  392. /* enable/disable DVFS function */
  393. bool dvfs_disable_by_ptpod;
  394. bool dvfs_disable_by_suspend;
  395. bool dvfs_disable_by_early_suspend;
  396. bool dvfs_disable_by_procfs;
  397. /* limit for thermal/PBM */
  398. unsigned int limited_max_ncpu;
  399. unsigned int limited_max_freq;
  400. unsigned int limited_power_idx;
  401. unsigned int idx_opp_tbl_for_thermal_thro;
  402. unsigned int limited_power_by_thermal;
  403. #ifndef DISABLE_PBM_FEATURE
  404. unsigned int limited_power_by_pbm;
  405. #endif
  406. /* limit for HEVC (via. sysfs) */
  407. unsigned int limited_freq_by_hevc;
  408. /* limit max freq from user */
  409. unsigned int limited_max_freq_by_user;
  410. /* limit min/max freq from other kernel driver (for BSP package only) */
  411. unsigned int limited_min_freq_by_kdriver;
  412. unsigned int limited_max_freq_by_kdriver;
  413. #ifdef CONFIG_CPU_DVFS_TURBO_MODE
  414. /* turbo mode */
  415. unsigned int turbo_mode;
  416. #endif
  417. /* power throttling */
  418. #ifdef CONFIG_CPU_DVFS_POWER_THROTTLING
  419. int idx_opp_tbl_for_pwr_thro; /* keep the setting for power throttling */
  420. int idx_pwr_thro_max_opp; /* idx for power throttle max OPP */
  421. unsigned int pwr_thro_mode;
  422. #endif
  423. };
  424. struct mt_cpu_dvfs_ops {
  425. /* for thermal/PBM */
  426. int (*setup_power_table)(struct mt_cpu_dvfs *p);
  427. /* for freq change (PLL/MUX), return (physical) freq (KHz) */
  428. unsigned int (*get_cur_phy_freq)(struct mt_cpu_dvfs *p);
  429. /* set freq */
  430. void (*set_cur_freq)(struct mt_cpu_dvfs *p, unsigned int cur_khz, unsigned int target_khz);
  431. /* for volt change (PMICWRAP/extBuck), return volt (mV * 100)*/
  432. unsigned int (*get_cur_volt)(struct mt_cpu_dvfs *p);
  433. /* set volt (mv * 100), return 0 (success), -1 (fail) */
  434. int (*set_cur_volt)(struct mt_cpu_dvfs *p, unsigned int volt);
  435. };
  436. struct opp_tbl_info {
  437. struct mt_cpu_freq_info *const opp_tbl;
  438. const int size;
  439. };
  440. #ifdef CONFIG_CPU_DVFS_TURBO_MODE
  441. struct turbo_mode_cfg {
  442. int temp; /* degree x 1000 */
  443. int freq_delta; /* percentage */
  444. int volt_delta; /* mv * 100 */
  445. };
  446. #endif
  447. /*=============================================================*/
  448. /* Local function definition */
  449. /*=============================================================*/
  450. /* for thermal */
  451. static int _mt_cpufreq_setup_power_table(struct mt_cpu_dvfs *p);
  452. /* for freq change (PLL/MUX) */
  453. static unsigned int _mt_cpufreq_get_cur_phy_freq(struct mt_cpu_dvfs *p);
  454. static void _mt_cpufreq_set_cur_freq(struct mt_cpu_dvfs *p, unsigned int cur_khz,
  455. unsigned int target_khz);
  456. /* for volt change (PMICWRAP/extBuck) */
  457. static unsigned int _mt_cpufreq_get_cur_volt(struct mt_cpu_dvfs *p);
  458. static int _mt_cpufreq_set_cur_volt(struct mt_cpu_dvfs *p, unsigned int volt); /* volt: mv * 100 */
  459. #ifdef CONFIG_CPU_DVFS_HAS_EXTBUCK
  460. static unsigned int _mt_cpufreq_get_cur_volt_extbuck(struct mt_cpu_dvfs *p);
  461. static int _mt_cpufreq_set_cur_volt_extbuck(struct mt_cpu_dvfs *p, unsigned int volt); /* volt: mv * 100 */
  462. #endif
  463. /* CPU callback */
  464. static int __cpuinit _mt_cpufreq_cpu_CB(struct notifier_block *nfb, unsigned long action,
  465. void *hcpu);
  466. /* cpufreq driver */
  467. #ifdef CONFIG_CPU_FREQ
  468. static int _mt_cpufreq_verify(struct cpufreq_policy *policy);
  469. static int _mt_cpufreq_target(struct cpufreq_policy *policy, unsigned int target_freq,
  470. unsigned int relation);
  471. static int _mt_cpufreq_init(struct cpufreq_policy *policy);
  472. static unsigned int _mt_cpufreq_get(unsigned int cpu);
  473. #endif
  474. /* (early-)suspend */
  475. #ifdef CONFIG_HAS_EARLYSUSPEND
  476. static void _mt_cpufreq_early_suspend(struct early_suspend *h);
  477. static void _mt_cpufreq_late_resume(struct early_suspend *h);
  478. #else
  479. static int _mt_cpufreq_fb_notifier_callback(struct notifier_block *self, unsigned long event, void *data);
  480. #endif
  481. static int _mt_cpufreq_suspend(struct device *dev);
  482. static int _mt_cpufreq_resume(struct device *dev);
  483. static int _mt_cpufreq_pm_restore_early(struct device *dev); /* for IPO-H HW(freq) / SW(opp_tbl_idx) */
  484. /*Platform driver */
  485. static int _mt_cpufreq_pdrv_probe(struct platform_device *pdev);
  486. static int _mt_cpufreq_pdrv_remove(struct platform_device *pdev);
  487. /*=============================================================*/
  488. /* Global Variables */
  489. /*=============================================================*/
  490. /* PMIC WRAP */
  491. static struct pmic_wrap_setting pw = {
  492. .phase = NR_PMIC_WRAP_PHASE, /* invalid setting for init */
  493. .addr = {{0, 0} },
  494. #ifdef CONFIG_ARCH_MT6735M
  495. .set[PMIC_WRAP_PHASE_NORMAL] = {
  496. ._[IDX_NM_VCORE_TRANS4] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(121250),},
  497. ._[IDX_NM_VCORE_TRANS3] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(118125),},
  498. ._[IDX_NM_VCORE_HPM] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(115000),},
  499. ._[IDX_NM_VCORE_TRANS2] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(111250),},
  500. ._[IDX_NM_VCORE_TRANS1] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(108125),},
  501. ._[IDX_NM_VCORE_LPM] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(105000),},
  502. ._[IDX_NM_VCORE_UHPM] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(125000),},
  503. ._[IDX_NM_VRF18_0_PWR_ON] = {MT6328_PMIC_RG_VRF18_0_EN_ADDR, _BITS_(1:1, 1),},
  504. ._[IDX_NM_NOT_USED] = {0, 0,},
  505. ._[IDX_NM_VRF18_0_SHUTDOWN] = {MT6328_PMIC_RG_VRF18_0_EN_ADDR, _BITS_(1:1, 0),},
  506. .nr_idx = NR_IDX_NM,
  507. },
  508. #else
  509. .set[PMIC_WRAP_PHASE_NORMAL] = {
  510. ._[IDX_NM_NOT_USED1] = {0, 0,},
  511. ._[IDX_NM_NOT_USED2] = {0, 0,},
  512. #ifdef CONFIG_ARCH_MT6753
  513. ._[IDX_NM_VCORE_HPM] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(125000),},
  514. ._[IDX_NM_VCORE_TRANS2] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(121250),},
  515. ._[IDX_NM_VCORE_TRANS1] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(118125),},
  516. ._[IDX_NM_VCORE_LPM] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(115000),},
  517. #else /* 6735 */
  518. ._[IDX_NM_VCORE_HPM] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(115000),},
  519. ._[IDX_NM_VCORE_TRANS2] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(111250),},
  520. ._[IDX_NM_VCORE_TRANS1] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(108125),},
  521. ._[IDX_NM_VCORE_LPM] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(105000),},
  522. #endif
  523. ._[IDX_NM_VRF18_0_SHUTDOWN] = {MT6328_PMIC_RG_VRF18_0_EN_ADDR, _BITS_(1:1, 0),},
  524. ._[IDX_NM_VRF18_0_PWR_ON] = {MT6328_PMIC_RG_VRF18_0_EN_ADDR, _BITS_(1:1, 1),},
  525. .nr_idx = NR_IDX_NM,
  526. },
  527. #endif
  528. .set[PMIC_WRAP_PHASE_SUSPEND] = {
  529. ._[IDX_SP_VPROC_PWR_ON] = {MT6328_PMIC_VPROC_EN_ADDR, _BITS_(0:0, 1),},
  530. ._[IDX_SP_VPROC_SHUTDOWN] = {MT6328_PMIC_VPROC_EN_ADDR, _BITS_(0:0, 0),},
  531. #ifdef CONFIG_ARCH_MT6753
  532. ._[IDX_SP_VCORE_HPM] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(125000),},
  533. ._[IDX_SP_VCORE_TRANS2] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(121250),},
  534. ._[IDX_SP_VCORE_TRANS1] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(118125),},
  535. ._[IDX_SP_VCORE_LPM] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(115000),},
  536. #else /* 6735(M) */
  537. ._[IDX_SP_VCORE_HPM] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(115000),},
  538. ._[IDX_SP_VCORE_TRANS2] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(111250),},
  539. ._[IDX_SP_VCORE_TRANS1] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(108125),},
  540. ._[IDX_SP_VCORE_LPM] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(105000),},
  541. #endif
  542. ._[IDX_SP_VSRAM_SHUTDOWN] = {MT6328_PMIC_RG_VSRAM_EN_ADDR, _BITS_(1:1, 0),},
  543. ._[IDX_SP_VRF18_0_PWR_ON] = {MT6328_PMIC_RG_VRF18_0_EN_ADDR, _BITS_(1:1, 1),},
  544. ._[IDX_SP_VSRAM_PWR_ON] = {MT6328_PMIC_RG_VSRAM_EN_ADDR, _BITS_(1:1, 1),},
  545. ._[IDX_SP_VRF18_0_SHUTDOWN] = {MT6328_PMIC_RG_VRF18_0_EN_ADDR, _BITS_(1:1, 0),},
  546. .nr_idx = NR_IDX_SP,
  547. },
  548. .set[PMIC_WRAP_PHASE_DEEPIDLE] = {
  549. ._[IDX_DI_VPROC_NORMAL] = {MT6328_PMIC_VPROC_VOSEL_CTRL_ADDR, _BITS_(1:1, 1),},
  550. ._[IDX_DI_VPROC_SLEEP] = {MT6328_PMIC_VPROC_VOSEL_CTRL_ADDR, _BITS_(1:1, 0),},
  551. #ifdef CONFIG_ARCH_MT6753
  552. ._[IDX_DI_VCORE_HPM] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(125000),},
  553. ._[IDX_DI_VCORE_TRANS2] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(121250),},
  554. ._[IDX_DI_VCORE_TRANS1] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(118125),},
  555. ._[IDX_DI_VCORE_LPM] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(115000),},
  556. ._[IDX_DI_VSRAM_SLEEP] = {MT6328_PMIC_VSRAM_VOSEL_OFFSET_ADDR, _BITS_(15:0, 0x10),},
  557. ._[IDX_DI_VRF18_0_PWR_ON] = {MT6328_PMIC_RG_VRF18_0_EN_ADDR, _BITS_(1:1, 1),},
  558. /* We will get actual value at DVFS driver init procedure */
  559. ._[IDX_DI_VSRAM_NORMAL] = {MT6328_PMIC_VSRAM_VOSEL_OFFSET_ADDR, _BITS_(15:0, 0x10),},
  560. ._[IDX_DI_VRF18_0_SHUTDOWN] = {MT6328_PMIC_RG_VRF18_0_EN_ADDR, _BITS_(1:1, 0),},
  561. ._[IDX_DI_VCORE_IDLE_LPM] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(105000),},
  562. ._[IDX_DI_VSRAM_SLEEP_FOR_TURBO] = {MT6328_PMIC_VSRAM_VOSEL_CTRL_ADDR, _BITS_(1:1, 1),},
  563. ._[IDX_DI_VSRAM_NORMAL_FOR_TURBO] = {MT6328_PMIC_VSRAM_VOSEL_CTRL_ADDR, _BITS_(1:1, 0),},
  564. #else /* 6735(M) */
  565. ._[IDX_DI_VCORE_HPM] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(115000),},
  566. ._[IDX_DI_VCORE_TRANS2] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(111250),},
  567. ._[IDX_DI_VCORE_TRANS1] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(108125),},
  568. ._[IDX_DI_VCORE_LPM] = {MT6328_PMIC_VCORE1_VOSEL_ON_ADDR, VOLT_TO_PMIC_VAL(105000),},
  569. ._[IDX_DI_VSRAM_SLEEP] = {MT6328_PMIC_VSRAM_VOSEL_OFFSET_ADDR, _BITS_(15:0, 0x10),},
  570. ._[IDX_DI_VRF18_0_PWR_ON] = {MT6328_PMIC_RG_VRF18_0_EN_ADDR, _BITS_(1:1, 1),},
  571. ._[IDX_DI_VSRAM_NORMAL] = {MT6328_PMIC_VSRAM_VOSEL_CTRL_ADDR, _BITS_(1:1, 1),},
  572. ._[IDX_DI_VRF18_0_SHUTDOWN] = {MT6328_PMIC_RG_VRF18_0_EN_ADDR, _BITS_(1:1, 0),},
  573. #endif
  574. .nr_idx = NR_IDX_DI,
  575. },
  576. };
  577. /* DVFS operation and structure */
  578. static struct mt_cpu_dvfs_ops dvfs_ops = {
  579. .setup_power_table = _mt_cpufreq_setup_power_table,
  580. .get_cur_phy_freq = _mt_cpufreq_get_cur_phy_freq,
  581. .set_cur_freq = _mt_cpufreq_set_cur_freq,
  582. .get_cur_volt = _mt_cpufreq_get_cur_volt,
  583. .set_cur_volt = _mt_cpufreq_set_cur_volt,
  584. };
  585. #ifdef CONFIG_CPU_DVFS_HAS_EXTBUCK
  586. static struct mt_cpu_dvfs_ops dvfs_ops_extbuck = {
  587. .setup_power_table = _mt_cpufreq_setup_power_table,
  588. .get_cur_phy_freq = _mt_cpufreq_get_cur_phy_freq,
  589. .set_cur_freq = _mt_cpufreq_set_cur_freq,
  590. .get_cur_volt = _mt_cpufreq_get_cur_volt_extbuck,
  591. .set_cur_volt = _mt_cpufreq_set_cur_volt_extbuck,
  592. };
  593. #endif
  594. static struct mt_cpu_dvfs cpu_dvfs[] = {
  595. [MT_CPU_DVFS_LITTLE] = {
  596. .name = __stringify(MT_CPU_DVFS_LITTLE),
  597. .cpu_id = MT_CPU_DVFS_LITTLE, /* TODO: FIXME */
  598. .cpu_level = CPU_LEVEL_1, /* FY segment */
  599. .ops = &dvfs_ops,
  600. #ifdef CONFIG_CPU_DVFS_TURBO_MODE
  601. .turbo_mode = 0,
  602. #endif
  603. #ifdef CONFIG_CPU_DVFS_POWER_THROTTLING
  604. .idx_opp_tbl_for_pwr_thro = -1,
  605. .idx_pwr_thro_max_opp = 0,
  606. #endif
  607. },
  608. };
  609. /* DVFS table */
  610. #ifdef CONFIG_ARCH_MT6753
  611. /* CPU LEVEL 0, 1.5GHz segment */
  612. static struct mt_cpu_freq_info opp_tbl_e1_0[] = {
  613. OP(CPU_DVFS_FREQ0, 125000),
  614. OP(CPU_DVFS_FREQ1, 121250),
  615. OP(CPU_DVFS_FREQ3, 118125),
  616. OP(CPU_DVFS_FREQ4, 115000),
  617. OP(CPU_DVFS_FREQ5, 108750),
  618. OP(CPU_DVFS_FREQ6, 103125),
  619. OP(CPU_DVFS_FREQ7, 99375),
  620. OP(CPU_DVFS_FREQ8, 95000),
  621. };
  622. /* CPU LEVEL 1, 1.3GHz segment */
  623. static struct mt_cpu_freq_info opp_tbl_e1_1[] = {
  624. OP(CPU_DVFS_FREQ1, 125000),
  625. OP(CPU_DVFS_FREQ2, 123125),
  626. OP(CPU_DVFS_FREQ3_1, 120000),
  627. OP(CPU_DVFS_FREQ4, 115000),
  628. OP(CPU_DVFS_FREQ5, 110000),
  629. OP(CPU_DVFS_FREQ6, 105000),
  630. OP(CPU_DVFS_FREQ7, 100000),
  631. OP(CPU_DVFS_FREQ8, 95000),
  632. };
  633. static struct opp_tbl_info opp_tbls[] = {
  634. [CPU_LEVEL_0] = {opp_tbl_e1_0, ARRAY_SIZE(opp_tbl_e1_0)},
  635. [CPU_LEVEL_1] = {opp_tbl_e1_1, ARRAY_SIZE(opp_tbl_e1_1)},
  636. };
  637. #elif defined(CONFIG_ARCH_MT6735M)
  638. /* CPU LEVEL 0, 1.2GHz segment */
  639. static struct mt_cpu_freq_info opp_tbl_e1_0[] = {
  640. OP(CPU_DVFS_FREQ0, 125000),
  641. OP(CPU_DVFS_FREQ1, 121875),
  642. OP(CPU_DVFS_FREQ4, 118750),
  643. OP(CPU_DVFS_FREQ6, 115000),
  644. OP(CPU_DVFS_FREQ7, 110000),
  645. OP(CPU_DVFS_FREQ8, 105000),
  646. OP(CPU_DVFS_FREQ9, 100000),
  647. OP(CPU_DVFS_FREQ10, 95000),
  648. };
  649. /* CPU LEVEL 1, 1GHz segment */
  650. static struct mt_cpu_freq_info opp_tbl_e1_1[] = {
  651. OP(CPU_DVFS_FREQ2, 125000),
  652. OP(CPU_DVFS_FREQ3, 121875),
  653. OP(CPU_DVFS_FREQ5, 118750),
  654. OP(CPU_DVFS_FREQ6, 115000),
  655. OP(CPU_DVFS_FREQ7, 110000),
  656. OP(CPU_DVFS_FREQ8, 105000),
  657. OP(CPU_DVFS_FREQ9, 100000),
  658. OP(CPU_DVFS_FREQ10, 95000),
  659. };
  660. /* CPU LEVEL 2, 1.25GHz segment */
  661. static struct mt_cpu_freq_info opp_tbl_e1_2[] = {
  662. OP(CPU_DVFS_FREQ0_1,125000),
  663. OP(CPU_DVFS_FREQ1, 121875),
  664. OP(CPU_DVFS_FREQ5, 118750),
  665. OP(CPU_DVFS_FREQ6, 115000),
  666. OP(CPU_DVFS_FREQ7, 110000),
  667. OP(CPU_DVFS_FREQ8, 105000),
  668. OP(CPU_DVFS_FREQ9, 100000),
  669. OP(CPU_DVFS_FREQ10, 95000),
  670. };
  671. /* CPU LEVEL 3, 1.1GHz segment */
  672. static struct mt_cpu_freq_info opp_tbl_e1_3[] = {
  673. OP(CPU_DVFS_FREQ1_1,125000),
  674. OP(CPU_DVFS_FREQ2, 121875),
  675. OP(CPU_DVFS_FREQ5, 118750),
  676. OP(CPU_DVFS_FREQ6, 115000),
  677. OP(CPU_DVFS_FREQ7, 110000),
  678. OP(CPU_DVFS_FREQ8, 105000),
  679. OP(CPU_DVFS_FREQ9, 100000),
  680. OP(CPU_DVFS_FREQ10, 95000),
  681. };
  682. static struct opp_tbl_info opp_tbls[] = {
  683. [CPU_LEVEL_0] = {opp_tbl_e1_0, ARRAY_SIZE(opp_tbl_e1_0)},
  684. [CPU_LEVEL_1] = {opp_tbl_e1_1, ARRAY_SIZE(opp_tbl_e1_1)},
  685. [CPU_LEVEL_2] = {opp_tbl_e1_2, ARRAY_SIZE(opp_tbl_e1_2)},
  686. [CPU_LEVEL_3] = {opp_tbl_e1_3, ARRAY_SIZE(opp_tbl_e1_3)},
  687. };
  688. #else
  689. /* CPU LEVEL 0, 1.5GHz segment */
  690. static struct mt_cpu_freq_info opp_tbl_e1_0[] = {
  691. OP(CPU_DVFS_FREQ0, 125000),
  692. OP(CPU_DVFS_FREQ1, 121250),
  693. OP(CPU_DVFS_FREQ3, 118125),
  694. OP(CPU_DVFS_FREQ4, 115000),
  695. OP(CPU_DVFS_FREQ5, 108750),
  696. OP(CPU_DVFS_FREQ6, 103125),
  697. OP(CPU_DVFS_FREQ7, 99375),
  698. OP(CPU_DVFS_FREQ8, 95000),
  699. };
  700. /* CPU LEVEL 1, 1.3GHz segment */
  701. static struct mt_cpu_freq_info opp_tbl_e1_1[] = {
  702. OP(CPU_DVFS_FREQ1, 125000),
  703. OP(CPU_DVFS_FREQ2, 123125),
  704. OP(CPU_DVFS_FREQ3, 120625),
  705. OP(CPU_DVFS_FREQ4, 115000),
  706. OP(CPU_DVFS_FREQ5, 110000),
  707. OP(CPU_DVFS_FREQ6, 105000),
  708. OP(CPU_DVFS_FREQ7, 100000),
  709. OP(CPU_DVFS_FREQ8, 95000),
  710. };
  711. /* CPU LEVEL 2, 1.1GHz segment */
  712. static struct mt_cpu_freq_info opp_tbl_e1_2[] = {
  713. OP(CPU_DVFS_FREQ4, 115000),
  714. OP(CPU_DVFS_FREQ5, 110000),
  715. OP(CPU_DVFS_FREQ6, 105000),
  716. OP(CPU_DVFS_FREQ7, 100000),
  717. OP(CPU_DVFS_FREQ8, 95000),
  718. OP(CPU_DVFS_FREQ8, 95000),
  719. OP(CPU_DVFS_FREQ8, 95000),
  720. OP(CPU_DVFS_FREQ8, 95000),
  721. };
  722. /* CPU LEVEL 3, 1.45GHz segment */
  723. static struct mt_cpu_freq_info opp_tbl_e1_3[] = {
  724. OP(CPU_DVFS_FREQ0_1,125000),
  725. OP(CPU_DVFS_FREQ1, 123125),
  726. OP(CPU_DVFS_FREQ3, 120625),
  727. OP(CPU_DVFS_FREQ4, 115000),
  728. OP(CPU_DVFS_FREQ5, 110000),
  729. OP(CPU_DVFS_FREQ6, 105000),
  730. OP(CPU_DVFS_FREQ7, 100000),
  731. OP(CPU_DVFS_FREQ8, 95000),
  732. };
  733. /* CPU LEVEL 4, 1.1GHz segment (for 37T to 35M+)*/
  734. static struct mt_cpu_freq_info opp_tbl_e1_4[] = {
  735. OP(CPU_DVFS_FREQ3_2, 125000),
  736. OP(CPU_DVFS_FREQ4_1, 123125),
  737. OP(CPU_DVFS_FREQ4_2, 120625),
  738. OP(CPU_DVFS_FREQ5_1, 115000),
  739. OP(CPU_DVFS_FREQ5_2, 110000),
  740. OP(CPU_DVFS_FREQ6_1, 105000),
  741. OP(CPU_DVFS_FREQ7_1, 100000),
  742. OP(CPU_DVFS_FREQ8_1, 95000),
  743. };
  744. static struct opp_tbl_info opp_tbls[] = {
  745. [CPU_LEVEL_0] = {opp_tbl_e1_0, ARRAY_SIZE(opp_tbl_e1_0)},
  746. [CPU_LEVEL_1] = {opp_tbl_e1_1, ARRAY_SIZE(opp_tbl_e1_1)},
  747. [CPU_LEVEL_2] = {opp_tbl_e1_2, ARRAY_SIZE(opp_tbl_e1_2)},
  748. [CPU_LEVEL_3] = {opp_tbl_e1_3, ARRAY_SIZE(opp_tbl_e1_3)},
  749. [CPU_LEVEL_4] = {opp_tbl_e1_4, ARRAY_SIZE(opp_tbl_e1_4)},
  750. };
  751. #endif
  752. /* CPU on/off callback */
  753. #if defined(CONFIG_ARCH_MT6753) || defined(CONFIG_CPU_DVFS_TURBO_MODE)
  754. static unsigned int num_online_cpus_delta;
  755. #endif
  756. static struct notifier_block __refdata _mt_cpufreq_cpu_notifier = {
  757. .notifier_call = _mt_cpufreq_cpu_CB,
  758. };
  759. /* TODO: waiting for HPT to provide turbo settings */
  760. #ifdef CONFIG_CPU_DVFS_TURBO_MODE
  761. struct turbo_mode_cfg turbo_mode_cfg[] = {
  762. [TURBO_MODE_2] = {
  763. .temp = 65000,
  764. .freq_delta = 10,
  765. .volt_delta = 4000,
  766. },
  767. [TURBO_MODE_1] = {
  768. .temp = 85000,
  769. .freq_delta = 5,
  770. .volt_delta = 2000,
  771. },
  772. [TURBO_MODE_NONE] = {
  773. .temp = 125000,
  774. .freq_delta = 0,
  775. .volt_delta = 0,
  776. },
  777. };
  778. static bool is_in_turbo_mode;
  779. #endif
  780. #ifdef CONFIG_CPU_FREQ
  781. static struct freq_attr *_mt_cpufreq_attr[] = {
  782. &cpufreq_freq_attr_scaling_available_freqs,
  783. NULL,
  784. };
  785. static struct cpufreq_driver _mt_cpufreq_driver = {
  786. .flags = CPUFREQ_ASYNC_NOTIFICATION,
  787. .verify = _mt_cpufreq_verify,
  788. .target = _mt_cpufreq_target,
  789. .init = _mt_cpufreq_init,
  790. .get = _mt_cpufreq_get,
  791. .name = "mt-cpufreq",
  792. .attr = _mt_cpufreq_attr,
  793. };
  794. #endif
  795. /* (early-)suspend / (late-)resume */
  796. #ifdef CONFIG_HAS_EARLYSUSPEND
  797. static struct early_suspend _mt_cpufreq_early_suspend_handler = {
  798. .level = EARLY_SUSPEND_LEVEL_DISABLE_FB + 200,
  799. .suspend = _mt_cpufreq_early_suspend,
  800. .resume = _mt_cpufreq_late_resume,
  801. };
  802. #else
  803. static struct notifier_block _mt_cpufreq_fb_notifier = {
  804. .notifier_call = _mt_cpufreq_fb_notifier_callback,
  805. };
  806. #endif /* CONFIG_HAS_EARLYSUSPEND */
  807. static const struct dev_pm_ops _mt_cpufreq_pm_ops = {
  808. .suspend = _mt_cpufreq_suspend,
  809. .resume = _mt_cpufreq_resume,
  810. .restore_early = _mt_cpufreq_pm_restore_early,
  811. .freeze = _mt_cpufreq_suspend,
  812. .thaw = _mt_cpufreq_resume,
  813. .restore = _mt_cpufreq_resume,
  814. };
  815. static bool _allow_dpidle_ctrl_vproc;
  816. static unsigned int is_fix_freq_in_ES = 1;
  817. /* Platform driver */
  818. static struct platform_device _mt_cpufreq_pdev = {
  819. .name = "mt-cpufreq",
  820. .id = -1,
  821. };
  822. static struct platform_driver _mt_cpufreq_pdrv = {
  823. .probe = _mt_cpufreq_pdrv_probe,
  824. .remove = _mt_cpufreq_pdrv_remove,
  825. .driver = {
  826. .name = "mt-cpufreq",
  827. .pm = &_mt_cpufreq_pm_ops,
  828. .owner = THIS_MODULE,
  829. },
  830. };
  831. /* Debugging */
  832. /* static unsigned int func_lv_mask =
  833. (FUNC_LV_MODULE | FUNC_LV_CPUFREQ | FUNC_LV_API | FUNC_LV_LOCAL | FUNC_LV_HELP); */
  834. static unsigned int func_lv_mask;
  835. static unsigned int do_dvfs_stress_test;
  836. /* Lock */
  837. static DEFINE_MUTEX(cpufreq_mutex);
  838. /* static DEFINE_MUTEX(pmic_wrap_mutex); */
  839. bool is_in_cpufreq = 0; /* used in MCDI */
  840. /* cpu voltage sampler */
  841. static cpuVoltsampler_func g_pCpuVoltSampler;
  842. /* for PMIC 5A throttle */
  843. #ifdef CONFIG_ARCH_MT6753
  844. static bool pmic_5A_throttle_enable;
  845. static bool pmic_5A_throttle_on;
  846. #endif
  847. /*=============================================================*/
  848. /* Function Implementation */
  849. /*=============================================================*/
  850. /* weak functions */
  851. void __attribute__ ((weak))
  852. register_battery_oc_notify(void (*battery_oc_callback) (BATTERY_OC_LEVEL), BATTERY_OC_PRIO prio_val)
  853. {
  854. cpufreq_err("%s doesn't exist\n", __func__);
  855. }
  856. void __attribute__ ((weak))
  857. register_battery_percent_notify(void (*battery_percent_callback) (BATTERY_PERCENT_LEVEL),
  858. BATTERY_PERCENT_PRIO prio_val)
  859. {
  860. cpufreq_err("%s doesn't exist\n", __func__);
  861. }
  862. #ifdef CONFIG_ARCH_MT6753
  863. static bool is_need_5A_throttle(struct mt_cpu_dvfs *p, unsigned int cur_freq, unsigned int cur_core_num)
  864. {
  865. if (pmic_5A_throttle_enable && pmic_5A_throttle_on
  866. && (cur_core_num > PMIC_5A_THRO_MAX_CPU_CORE_NUM)
  867. && (cur_freq > PMIC_5A_THRO_MAX_CPU_FREQ))
  868. return true;
  869. return false;
  870. }
  871. #endif
  872. static struct mt_cpu_dvfs *id_to_cpu_dvfs(enum mt_cpu_dvfs_id id)
  873. {
  874. return (id < NR_MT_CPU_DVFS) ? &cpu_dvfs[id] : NULL;
  875. }
  876. static enum mt_cpu_dvfs_id _get_cpu_dvfs_id(unsigned int cpu_id)
  877. {
  878. return MT_CPU_DVFS_LITTLE;
  879. }
  880. #ifdef __KERNEL__
  881. static unsigned int _mt_cpufreq_get_cpu_level(void)
  882. {
  883. unsigned int lv = 0;
  884. unsigned int cpu_spd_bond = _GET_BITS_VAL_(2:0, get_devinfo_with_index(CPUFREQ_EFUSE_INDEX));
  885. unsigned int cpu_speed = 0;
  886. #ifdef CONFIG_OF
  887. struct device_node *node = of_find_node_by_type(NULL, "cpu");
  888. if (!of_property_read_u32(node, "clock-frequency", &cpu_speed))
  889. cpu_speed = cpu_speed / 1000 / 1000; // MHz
  890. else {
  891. cpufreq_err("@%s: missing clock-frequency property, use default CPU level\n", __func__);
  892. return CPU_LEVEL_1;
  893. }
  894. cpufreq_info("CPU clock-frequency from DT = %d MHz\n", cpu_speed);
  895. #endif
  896. cpufreq_info("@%s: efuse cpu_spd_bond = 0x%x\n", __func__, cpu_spd_bond);
  897. #ifdef CONFIG_ARCH_MT6753
  898. {
  899. unsigned int efuse_spare2 = _GET_BITS_VAL_(21:20, get_devinfo_with_index(5));
  900. cpufreq_info("@%s: efuse_spare2 = 0x%x\n", __func__, efuse_spare2);
  901. /* 6753T check, spare2[21:20] should be 0x3 */
  902. if (cpu_spd_bond == 0 && efuse_spare2 == 3)
  903. return CPU_LEVEL_0;
  904. }
  905. #else
  906. {
  907. unsigned int segment_code = _GET_BITS_VAL_(31 : 25, get_devinfo_with_index(47));
  908. cpufreq_info("@%s: segment_code = 0x%x\n", __func__, segment_code);
  909. #if defined(CONFIG_ARCH_MT6735) && defined(CONFIG_MTK_EFUSE_DOWNGRADE)
  910. return CPU_LEVEL_4; /* SW config 37T to 35M+ */
  911. #endif
  912. switch (segment_code) {
  913. case 0x41:
  914. case 0x42:
  915. case 0x43:
  916. return CPU_LEVEL_3; /* 37T: 1.45G */
  917. case 0x49:
  918. return CPU_LEVEL_2; /* 37M: 1.1G */
  919. case 0x4A:
  920. case 0x4B:
  921. return CPU_LEVEL_3; /* 35M+: 1.1G */
  922. case 0x51:
  923. return CPU_LEVEL_1; /* 37: 1.3G */
  924. case 0x52:
  925. case 0x53:
  926. #ifdef CONFIG_MTK_EFUSE_DOWNGRADE
  927. return CPU_LEVEL_3; /* SW config to 35M+ */
  928. #else
  929. return CPU_LEVEL_2; /* 35P+ 1.25G */
  930. #endif
  931. default:
  932. break;
  933. }
  934. }
  935. #endif
  936. /* No efuse, use clock-frequency from device tree to determine CPU table type! */
  937. if (cpu_spd_bond == 0) {
  938. #ifdef CONFIG_ARCH_MT6753
  939. if (cpu_speed >= 1500 && cpu_dvfs_is_extbuck_valid())
  940. lv = CPU_LEVEL_0; /* 1.5G */
  941. else if (cpu_speed >= 1300)
  942. lv = CPU_LEVEL_1; /* 1.3G */
  943. else {
  944. cpufreq_err("No suitable DVFS table, set to default CPU level! clock-frequency=%d\n",
  945. cpu_speed);
  946. lv = CPU_LEVEL_1;
  947. }
  948. #elif defined(CONFIG_ARCH_MT6735M)
  949. if (cpu_speed >= 1150)
  950. lv = CPU_LEVEL_0; /* 1.15G */
  951. else if (cpu_speed >= 1000)
  952. lv = CPU_LEVEL_1; /* 1G */
  953. else {
  954. cpufreq_err("No suitable DVFS table, set to default CPU level! clock-frequency=%d\n",
  955. cpu_speed);
  956. lv = CPU_LEVEL_1;
  957. }
  958. #else /* CONFIG_ARCH_MT6735 */
  959. if (cpu_speed >= 1500)
  960. lv = CPU_LEVEL_0; /* 1.5G */
  961. else if (cpu_speed >= 1300)
  962. lv = CPU_LEVEL_1; /* 1.3G */
  963. else if (cpu_speed >= 1100)
  964. lv = CPU_LEVEL_2; /* 1.1G */
  965. else {
  966. cpufreq_err("No suitable DVFS table, set to default CPU level! clock-frequency=%d\n",
  967. cpu_speed);
  968. lv = CPU_LEVEL_1;
  969. }
  970. #endif
  971. return lv;
  972. }
  973. /* no DT, we should check efuse for CPU speed HW bounding */
  974. switch (cpu_spd_bond) {
  975. #ifdef CONFIG_ARCH_MT6735M
  976. case 1:
  977. case 2:
  978. case 3:
  979. case 4:
  980. case 5:
  981. lv = CPU_LEVEL_0; /* 1.15G */
  982. break;
  983. case 6:
  984. case 7:
  985. default:
  986. lv = CPU_LEVEL_1; /* 1G */
  987. break;
  988. #else /* !CONFIG_ARCH_MT6735M */
  989. case 1:
  990. case 2:
  991. lv = CPU_LEVEL_0; /* 1.5G */
  992. break;
  993. case 3:
  994. case 4:
  995. lv = CPU_LEVEL_1; /* 1.3G */
  996. break;
  997. case 5:
  998. case 6:
  999. case 7:
  1000. #ifdef CONFIG_ARCH_MT6735
  1001. lv = CPU_LEVEL_2; /* 1.1G */
  1002. break;
  1003. #endif
  1004. default:
  1005. lv = CPU_LEVEL_1; /* 1.3G */
  1006. break;
  1007. #endif
  1008. }
  1009. return lv;
  1010. }
  1011. #else
  1012. static unsigned int _mt_cpufreq_get_cpu_level(void)
  1013. {
  1014. #ifdef __MTK_SLT_
  1015. CHIP_TYPE chip_type = mt_get_chip_type_by_efuse();
  1016. unsigned int lv = 0;
  1017. switch (chip_type) {
  1018. case CHIP_MT6735:
  1019. lv = CPU_LEVEL_1; /* D1 1.3G */
  1020. break;
  1021. case CHIP_MT6735M:
  1022. case CHIP_MT6735P:
  1023. #ifdef CONFIG_ARCH_MT6735M
  1024. lv = CPU_LEVEL_1; /* D2 1.1G */
  1025. #else
  1026. lv = CPU_LEVEL_2; /* D1 1.1G */
  1027. #endif
  1028. break;
  1029. case CHIP_NONE:
  1030. default:
  1031. lv = CPU_LEVEL_1; /* FY table */
  1032. break;
  1033. }
  1034. cpufreq_info("@%s: chip_type = 0x%x, cpu_lv = %d\n", __func__, chip_type, lv);
  1035. return lv;
  1036. #else
  1037. return CPU_LEVEL_1; /* always use FY table for DVT */
  1038. #endif
  1039. }
  1040. #endif
  1041. #ifdef CONFIG_CPU_DVFS_AEE_RR_REC
  1042. static void _mt_cpufreq_aee_init(void)
  1043. {
  1044. aee_rr_rec_cpu_dvfs_vproc_big(0xFF);
  1045. aee_rr_rec_cpu_dvfs_vproc_little(0xFF);
  1046. aee_rr_rec_cpu_dvfs_oppidx(0xFF);
  1047. aee_rr_rec_cpu_dvfs_status(0xFC);
  1048. }
  1049. #endif
  1050. static int _mt_cpufreq_set_spm_dvfs_ctrl_volt(u32 value)
  1051. {
  1052. #define MAX_RETRY_COUNT (100)
  1053. u32 ap_dvfs_con;
  1054. int retry = 0;
  1055. FUNC_ENTER(FUNC_LV_HELP);
  1056. spm_write(SPM_POWERON_CONFIG_SET, (SPM_PROJECT_CODE << 16) | (1U << 0));
  1057. ap_dvfs_con = spm_read(SPM_AP_DVFS_CON_SET);
  1058. #ifdef CONFIG_ARCH_MT6753
  1059. /* 6753 has 16 PWRAP commands */
  1060. spm_write(SPM_AP_DVFS_CON_SET, (ap_dvfs_con & ~(0xF)) | value);
  1061. #else
  1062. spm_write(SPM_AP_DVFS_CON_SET, (ap_dvfs_con & ~(0x7)) | value);
  1063. #endif
  1064. udelay(5);
  1065. while ((spm_read(SPM_AP_DVFS_CON_SET) & (0x1 << 31)) == 0) {
  1066. if (retry >= MAX_RETRY_COUNT) {
  1067. cpufreq_err("@%s: SPM write fail!\n", __func__);
  1068. return -1;
  1069. }
  1070. retry++;
  1071. /* cpufreq_dbg("wait for ACK signal from PMIC wrapper, retry = %d\n", retry); */
  1072. udelay(5);
  1073. }
  1074. FUNC_EXIT(FUNC_LV_HELP);
  1075. return 0;
  1076. }
  1077. static void _mt_cpufreq_pmic_table_init(void)
  1078. {
  1079. struct pmic_wrap_cmd pwrap_cmd_default[NR_PMIC_WRAP_CMD] = {
  1080. {(unsigned long)PMIC_WRAP_DVFS_ADR0, (unsigned long)PMIC_WRAP_DVFS_WDATA0,},
  1081. {(unsigned long)PMIC_WRAP_DVFS_ADR1, (unsigned long)PMIC_WRAP_DVFS_WDATA1,},
  1082. {(unsigned long)PMIC_WRAP_DVFS_ADR2, (unsigned long)PMIC_WRAP_DVFS_WDATA2,},
  1083. {(unsigned long)PMIC_WRAP_DVFS_ADR3, (unsigned long)PMIC_WRAP_DVFS_WDATA3,},
  1084. {(unsigned long)PMIC_WRAP_DVFS_ADR4, (unsigned long)PMIC_WRAP_DVFS_WDATA4,},
  1085. {(unsigned long)PMIC_WRAP_DVFS_ADR5, (unsigned long)PMIC_WRAP_DVFS_WDATA5,},
  1086. {(unsigned long)PMIC_WRAP_DVFS_ADR6, (unsigned long)PMIC_WRAP_DVFS_WDATA6,},
  1087. {(unsigned long)PMIC_WRAP_DVFS_ADR7, (unsigned long)PMIC_WRAP_DVFS_WDATA7,},
  1088. {(unsigned long)PMIC_WRAP_DVFS_ADR8, (unsigned long)PMIC_WRAP_DVFS_WDATA8,},
  1089. {(unsigned long)PMIC_WRAP_DVFS_ADR9, (unsigned long)PMIC_WRAP_DVFS_WDATA9,},
  1090. {(unsigned long)PMIC_WRAP_DVFS_ADR10, (unsigned long)PMIC_WRAP_DVFS_WDATA10,},
  1091. {(unsigned long)PMIC_WRAP_DVFS_ADR11, (unsigned long)PMIC_WRAP_DVFS_WDATA11,},
  1092. {(unsigned long)PMIC_WRAP_DVFS_ADR12, (unsigned long)PMIC_WRAP_DVFS_WDATA12,},
  1093. {(unsigned long)PMIC_WRAP_DVFS_ADR13, (unsigned long)PMIC_WRAP_DVFS_WDATA13,},
  1094. {(unsigned long)PMIC_WRAP_DVFS_ADR14, (unsigned long)PMIC_WRAP_DVFS_WDATA14,},
  1095. {(unsigned long)PMIC_WRAP_DVFS_ADR15, (unsigned long)PMIC_WRAP_DVFS_WDATA15,},
  1096. };
  1097. FUNC_ENTER(FUNC_LV_HELP);
  1098. memcpy(pw.addr, pwrap_cmd_default, sizeof(pwrap_cmd_default));
  1099. FUNC_EXIT(FUNC_LV_HELP);
  1100. }
  1101. void mt_cpufreq_set_pmic_phase(enum pmic_wrap_phase_id phase)
  1102. {
  1103. int i;
  1104. unsigned long flags;
  1105. FUNC_ENTER(FUNC_LV_API);
  1106. BUG_ON(phase >= NR_PMIC_WRAP_PHASE);
  1107. if (pw.addr[0].cmd_addr == 0) {
  1108. cpufreq_warn("pmic table not initialized\n");
  1109. _mt_cpufreq_pmic_table_init();
  1110. }
  1111. pmic_wrap_lock(flags);
  1112. pw.phase = phase;
  1113. for (i = 0; i < pw.set[phase].nr_idx; i++) {
  1114. cpufreq_write(pw.addr[i].cmd_addr, pw.set[phase]._[i].cmd_addr);
  1115. cpufreq_write(pw.addr[i].cmd_wdata, pw.set[phase]._[i].cmd_wdata);
  1116. }
  1117. pmic_wrap_unlock(flags);
  1118. FUNC_EXIT(FUNC_LV_API);
  1119. }
  1120. EXPORT_SYMBOL(mt_cpufreq_set_pmic_phase);
  1121. /* just set wdata value */
  1122. void mt_cpufreq_set_pmic_cmd(enum pmic_wrap_phase_id phase, int idx, unsigned int cmd_wdata)
  1123. {
  1124. unsigned long flags;
  1125. FUNC_ENTER(FUNC_LV_API);
  1126. BUG_ON(phase >= NR_PMIC_WRAP_PHASE);
  1127. BUG_ON(idx >= pw.set[phase].nr_idx);
  1128. /* cpufreq_dbg("@%s: phase = 0x%x, idx = %d, cmd_wdata = 0x%x\n", __func__, phase, idx, cmd_wdata); */
  1129. pmic_wrap_lock(flags);
  1130. pw.set[phase]._[idx].cmd_wdata = cmd_wdata;
  1131. if (pw.phase == phase)
  1132. cpufreq_write(pw.addr[idx].cmd_wdata, cmd_wdata);
  1133. pmic_wrap_unlock(flags);
  1134. FUNC_EXIT(FUNC_LV_API);
  1135. }
  1136. EXPORT_SYMBOL(mt_cpufreq_set_pmic_cmd);
  1137. /* kick spm */
  1138. void mt_cpufreq_apply_pmic_cmd(int idx)
  1139. {
  1140. unsigned long flags;
  1141. FUNC_ENTER(FUNC_LV_API);
  1142. BUG_ON(idx >= pw.set[pw.phase].nr_idx);
  1143. /* cpufreq_dbg("@%s: idx = %d\n", __func__, idx); */
  1144. pmic_wrap_lock(flags);
  1145. _mt_cpufreq_set_spm_dvfs_ctrl_volt(idx);
  1146. pmic_wrap_unlock(flags);
  1147. FUNC_EXIT(FUNC_LV_API);
  1148. }
  1149. EXPORT_SYMBOL(mt_cpufreq_apply_pmic_cmd);
  1150. void mt_cpufreq_setvolt_registerCB(cpuVoltsampler_func pCB)
  1151. {
  1152. g_pCpuVoltSampler = pCB;
  1153. }
  1154. EXPORT_SYMBOL(mt_cpufreq_setvolt_registerCB);
  1155. #ifdef CONFIG_CPU_DVFS_TURBO_MODE
  1156. static enum turbo_mode _mt_cpufreq_get_turbo_mode(struct mt_cpu_dvfs *p, unsigned int target_khz)
  1157. {
  1158. enum turbo_mode mode = TURBO_MODE_NONE;
  1159. #ifdef CONFIG_THERMAL
  1160. int temp = tscpu_get_temp_by_bank(THERMAL_BANK0); /* bank0 for CPU */
  1161. #else
  1162. int temp = 40;
  1163. #endif
  1164. unsigned int online_cpus = num_online_cpus() + num_online_cpus_delta;
  1165. int i;
  1166. if (p->turbo_mode && target_khz == cpu_dvfs_get_freq_by_idx(p, 0)
  1167. && online_cpus <= TURBO_MODE_BOUNDARY_CPU_NUM) {
  1168. for (i = 0; i < NR_TURBO_MODE; i++) {
  1169. if (temp < turbo_mode_cfg[i].temp) {
  1170. mode = i;
  1171. break;
  1172. }
  1173. }
  1174. }
  1175. /* enter turbo mode, SW workaround here */
  1176. if (mode < TURBO_MODE_NONE && is_in_turbo_mode == false) {
  1177. is_in_turbo_mode = true;
  1178. #ifdef CONFIG_CPU_DVFS_AEE_RR_REC
  1179. aee_rr_rec_cpu_dvfs_status(aee_rr_curr_cpu_dvfs_status() | (1 << CPU_DVFS_LITTLE_IS_TURBO));
  1180. #endif
  1181. }
  1182. cpufreq_ver("%s(), mode = %d, temp = %d, target_khz = %d (%d), num_online_cpus = %d\n",
  1183. __func__,
  1184. mode,
  1185. temp,
  1186. target_khz,
  1187. TURBO_MODE_FREQ(mode, target_khz),
  1188. online_cpus
  1189. );
  1190. return mode;
  1191. }
  1192. #endif
  1193. /* for PTP-OD */
  1194. static int _mt_cpufreq_set_cur_volt_locked(struct mt_cpu_dvfs *p, unsigned int volt) /* volt: mv * 100 */
  1195. {
  1196. int ret = -1;
  1197. FUNC_ENTER(FUNC_LV_HELP);
  1198. BUG_ON(NULL == p);
  1199. if (!cpu_dvfs_is_available(p)) {
  1200. FUNC_EXIT(FUNC_LV_HELP);
  1201. return 0;
  1202. }
  1203. /* set volt */
  1204. ret = p->ops->set_cur_volt(p, volt);
  1205. FUNC_EXIT(FUNC_LV_HELP);
  1206. return ret;
  1207. }
  1208. static int _mt_cpufreq_restore_default_volt(struct mt_cpu_dvfs *p)
  1209. {
  1210. unsigned long flags;
  1211. int i;
  1212. int ret = -1;
  1213. FUNC_ENTER(FUNC_LV_HELP);
  1214. BUG_ON(NULL == p);
  1215. if (!cpu_dvfs_is_available(p)) {
  1216. FUNC_EXIT(FUNC_LV_HELP);
  1217. return 0;
  1218. }
  1219. cpufreq_lock(flags);
  1220. /* restore to default volt */
  1221. for (i = 0; i < p->nr_opp_tbl; i++)
  1222. p->opp_tbl[i].cpufreq_volt = p->opp_tbl[i].cpufreq_volt_org;
  1223. /* set volt */
  1224. #ifdef CONFIG_CPU_DVFS_TURBO_MODE
  1225. ret = _mt_cpufreq_set_cur_volt_locked(
  1226. p,
  1227. TURBO_MODE_VOLT(
  1228. _mt_cpufreq_get_turbo_mode
  1229. (p, cpu_dvfs_get_cur_freq(p)),
  1230. cpu_dvfs_get_cur_volt(p)
  1231. )
  1232. );
  1233. #else
  1234. ret = _mt_cpufreq_set_cur_volt_locked(p, cpu_dvfs_get_cur_volt(p));
  1235. #endif
  1236. cpufreq_unlock(flags);
  1237. FUNC_EXIT(FUNC_LV_HELP);
  1238. return ret;
  1239. }
  1240. static int _mt_cpufreq_set_limit_by_pwr_budget(unsigned int budget)
  1241. {
  1242. struct mt_cpu_dvfs *p = id_to_cpu_dvfs(0); /* TODO: FIXME */
  1243. int possible_cpu = num_possible_cpus();
  1244. int ncpu;
  1245. int i;
  1246. for (ncpu = possible_cpu; ncpu > 0; ncpu--) {
  1247. for (i = 0; i < p->nr_opp_tbl * possible_cpu; i++) {
  1248. #ifdef CONFIG_ARCH_MT6753
  1249. if (is_need_5A_throttle(p, p->power_tbl[i].cpufreq_khz,
  1250. p->power_tbl[i].cpufreq_ncpu))
  1251. continue;
  1252. #endif
  1253. if (p->power_tbl[i].cpufreq_power <= budget) {
  1254. p->limited_power_idx = i;
  1255. p->limited_max_ncpu = p->power_tbl[i].cpufreq_ncpu;
  1256. p->limited_max_freq = p->power_tbl[i].cpufreq_khz;
  1257. return 1;
  1258. }
  1259. }
  1260. }
  1261. /* not found and use lowest power limit */
  1262. p->limited_power_idx = p->nr_power_tbl - 1;
  1263. p->limited_max_ncpu = p->power_tbl[p->limited_power_idx].cpufreq_ncpu;
  1264. p->limited_max_freq = p->power_tbl[p->limited_power_idx].cpufreq_khz;
  1265. return 0;
  1266. }
  1267. #ifndef DISABLE_PBM_FEATURE
  1268. static unsigned int _mt_cpufreq_get_limited_core_num(unsigned int budget)
  1269. {
  1270. struct mt_cpu_dvfs *p = id_to_cpu_dvfs(0); /* TODO: FIXME */
  1271. int possible_cpu = num_possible_cpus();
  1272. int ncpu;
  1273. int i;
  1274. for (ncpu = possible_cpu; ncpu > 0; ncpu--) {
  1275. for (i = 0; i < p->nr_opp_tbl * possible_cpu; i++) {
  1276. #ifdef CONFIG_ARCH_MT6753
  1277. if (is_need_5A_throttle(p, p->power_tbl[i].cpufreq_khz,
  1278. p->power_tbl[i].cpufreq_ncpu))
  1279. continue;
  1280. #endif
  1281. if (p->power_tbl[i].cpufreq_power <= budget)
  1282. return p->power_tbl[i].cpufreq_ncpu;
  1283. }
  1284. }
  1285. /* not found */
  1286. return p->power_tbl[p->nr_power_tbl - 1].cpufreq_ncpu;
  1287. }
  1288. /* for power budget calculate */
  1289. static int _mt_cpufreq_get_pwr_idx(struct mt_cpu_dvfs *p, unsigned int freq, unsigned int ncpu)
  1290. {
  1291. int possible_cpu = num_possible_cpus();
  1292. int i;
  1293. if (freq > cpu_dvfs_get_max_freq(p))
  1294. freq = cpu_dvfs_get_max_freq(p); /* skip turbo freq HERE */
  1295. for (i = 0; i < p->nr_opp_tbl * possible_cpu; i++) {
  1296. if (p->power_tbl[i].cpufreq_ncpu == ncpu && p->power_tbl[i].cpufreq_khz == freq)
  1297. return i;
  1298. }
  1299. return -1; /* not found */
  1300. }
  1301. static void _kick_PBM_by_cpu(struct mt_cpu_dvfs *p)
  1302. {
  1303. unsigned int cur_freq = p->ops->get_cur_phy_freq(p);
  1304. unsigned int cur_volt = p->ops->get_cur_volt(p) / 100; /* mV */
  1305. unsigned int cur_ncpu = num_online_cpus();
  1306. int idx = _mt_cpufreq_get_pwr_idx(p, cur_freq, cur_ncpu);
  1307. unsigned int limited_power = 0;
  1308. if (idx == -1) { /* Not found in power table */
  1309. limited_power = p->power_tbl[p->nr_power_tbl - 1].cpufreq_power;
  1310. cpufreq_warn("@%s: Not found in power table! cur_freq = %d, cur_ncpu = %d\n",
  1311. __func__, cur_freq, cur_ncpu);
  1312. } else {
  1313. limited_power = p->power_tbl[idx].cpufreq_power;
  1314. cpufreq_ver("@%s: cur_freq = %d, cur_volt = %d, cur_ncpu = %d, limited_power = %d\n",
  1315. __func__, cur_freq, cur_volt, cur_ncpu, limited_power);
  1316. kicker_pbm_by_cpu(limited_power, cur_ncpu, cur_volt);
  1317. }
  1318. }
  1319. #endif
  1320. void mt_cpufreq_set_power_limit_by_pbm(unsigned int limited_power)
  1321. {
  1322. #ifndef DISABLE_PBM_FEATURE
  1323. struct mt_cpu_dvfs *p = id_to_cpu_dvfs(0); /* TODO: FIXME */
  1324. unsigned long flags;
  1325. FUNC_ENTER(FUNC_LV_API);
  1326. BUG_ON(NULL == p);
  1327. if (!cpu_dvfs_is_available(p)) {
  1328. FUNC_EXIT(FUNC_LV_API);
  1329. return;
  1330. }
  1331. if (p->limited_power_by_thermal != 0 && limited_power > p->limited_power_by_thermal) {
  1332. cpufreq_ver("@%s: power is limited by thermal(%d), ignore budget from PBM(%d)...\n",
  1333. __func__, p->limited_power_by_thermal, limited_power);
  1334. /* we still need to update limited core */
  1335. hps_set_cpu_num_limit(LIMIT_LOW_BATTERY, _mt_cpufreq_get_limited_core_num(limited_power), 0);
  1336. return;
  1337. }
  1338. if (limited_power == p->limited_power_by_pbm) {
  1339. cpufreq_ver("@%s: limited_power(%d mW) not changed, skip it!\n", __func__,
  1340. limited_power);
  1341. return;
  1342. }
  1343. cpufreq_lock(flags);
  1344. p->limited_power_by_pbm = limited_power;
  1345. cpufreq_ver("@%s: limited power from PBM = %d\n", __func__, p->limited_power_by_pbm);
  1346. if (!p->limited_power_by_pbm && !p->limited_power_by_thermal) {
  1347. #ifdef CONFIG_ARCH_MT6753
  1348. if (p->cpu_level == CPU_LEVEL_1) {
  1349. /* give a large budget to find the first safe limit combination */
  1350. _mt_cpufreq_set_limit_by_pwr_budget(99999);
  1351. } else {
  1352. p->limited_max_ncpu = num_possible_cpus();
  1353. p->limited_max_freq = cpu_dvfs_get_max_freq(p);
  1354. p->limited_power_idx = 0;
  1355. }
  1356. #else
  1357. p->limited_max_ncpu = num_possible_cpus();
  1358. p->limited_max_freq = cpu_dvfs_get_max_freq(p);
  1359. p->limited_power_idx = 0;
  1360. #endif
  1361. } else
  1362. _mt_cpufreq_set_limit_by_pwr_budget(p->limited_power_by_pbm);
  1363. if (p->limited_max_ncpu < num_online_cpus())
  1364. cpufreq_dbg("@%s: limited power = %d, limited_max_ncpu = %d, online CPU = %d\n",
  1365. __func__, p->limited_power_by_pbm, p->limited_max_ncpu, num_online_cpus());
  1366. cpufreq_ver("@%s: limited_power_idx = %d, limited_max_freq = %d, limited_max_ncpu = %d\n",
  1367. __func__, p->limited_power_idx, p->limited_max_freq, p->limited_max_ncpu);
  1368. cpufreq_unlock(flags);
  1369. hps_set_cpu_num_limit(LIMIT_LOW_BATTERY, p->limited_max_ncpu, 0);
  1370. /* TODO: Trigger DVFS here? */
  1371. FUNC_EXIT(FUNC_LV_API);
  1372. #endif
  1373. }
  1374. unsigned int mt_cpufreq_get_leakage_mw(enum mt_cpu_dvfs_id id)
  1375. {
  1376. #ifndef DISABLE_PBM_FEATURE
  1377. struct mt_cpu_dvfs *p = id_to_cpu_dvfs(id);
  1378. #ifdef CONFIG_THERMAL
  1379. int temp = tscpu_get_temp_by_bank(THERMAL_BANK0) / 1000; /* bank0 for CPU */
  1380. #else
  1381. int temp = 40;
  1382. #endif
  1383. return mt_spower_get_leakage(MT_SPOWER_CPU, p->ops->get_cur_volt(p) / 100, temp);
  1384. #else
  1385. return 0;
  1386. #endif
  1387. }
  1388. void mt_cpufreq_set_min_freq(enum mt_cpu_dvfs_id id, unsigned int freq)
  1389. {
  1390. struct mt_cpu_dvfs *p = id_to_cpu_dvfs(id);
  1391. FUNC_ENTER(FUNC_LV_API);
  1392. BUG_ON(NULL == p);
  1393. if (!cpu_dvfs_is_available(p)) {
  1394. FUNC_EXIT(FUNC_LV_API);
  1395. return;
  1396. }
  1397. p->limited_min_freq_by_kdriver = freq;
  1398. if ((p->limited_min_freq_by_kdriver != 0)
  1399. && (p->limited_min_freq_by_kdriver > cpu_dvfs_get_cur_freq(p))) {
  1400. struct cpufreq_policy *policy = cpufreq_cpu_get(p->cpu_id);
  1401. if (policy) {
  1402. cpufreq_driver_target(
  1403. policy, p->limited_min_freq_by_kdriver, CPUFREQ_RELATION_L);
  1404. cpufreq_cpu_put(policy);
  1405. }
  1406. }
  1407. FUNC_EXIT(FUNC_LV_API);
  1408. }
  1409. void mt_cpufreq_set_max_freq(enum mt_cpu_dvfs_id id, unsigned int freq)
  1410. {
  1411. struct mt_cpu_dvfs *p = id_to_cpu_dvfs(id);
  1412. FUNC_ENTER(FUNC_LV_API);
  1413. BUG_ON(NULL == p);
  1414. if (!cpu_dvfs_is_available(p)) {
  1415. FUNC_EXIT(FUNC_LV_API);
  1416. return;
  1417. }
  1418. p->limited_max_freq_by_kdriver = freq;
  1419. if ((p->limited_max_freq_by_kdriver != 0)
  1420. && (p->limited_max_freq_by_kdriver < cpu_dvfs_get_cur_freq(p))) {
  1421. struct cpufreq_policy *policy = cpufreq_cpu_get(p->cpu_id);
  1422. if (policy) {
  1423. cpufreq_driver_target(
  1424. policy, p->limited_max_freq_by_kdriver, CPUFREQ_RELATION_H);
  1425. cpufreq_cpu_put(policy);
  1426. }
  1427. }
  1428. FUNC_EXIT(FUNC_LV_API);
  1429. }
  1430. unsigned int mt_cpufreq_get_cur_freq(enum mt_cpu_dvfs_id id)
  1431. {
  1432. struct mt_cpu_dvfs *p = id_to_cpu_dvfs(id);
  1433. FUNC_ENTER(FUNC_LV_API);
  1434. BUG_ON(NULL == p);
  1435. if (!cpu_dvfs_is_available(p)) {
  1436. FUNC_EXIT(FUNC_LV_API);
  1437. return 0;
  1438. }
  1439. FUNC_EXIT(FUNC_LV_API);
  1440. return _mt_cpufreq_get_cur_phy_freq(p);
  1441. }
  1442. unsigned int mt_cpufreq_get_freq_by_idx(enum mt_cpu_dvfs_id id, int idx)
  1443. {
  1444. struct mt_cpu_dvfs *p = id_to_cpu_dvfs(id);
  1445. FUNC_ENTER(FUNC_LV_API);
  1446. BUG_ON(NULL == p);
  1447. if (!cpu_dvfs_is_available(p)) {
  1448. FUNC_EXIT(FUNC_LV_API);
  1449. return 0;
  1450. }
  1451. BUG_ON(idx >= p->nr_opp_tbl);
  1452. FUNC_EXIT(FUNC_LV_API);
  1453. return cpu_dvfs_get_freq_by_idx(p, idx);
  1454. }
  1455. EXPORT_SYMBOL(mt_cpufreq_get_freq_by_idx);
  1456. int mt_cpufreq_update_volt(enum mt_cpu_dvfs_id id, unsigned int *volt_tbl, int nr_volt_tbl)
  1457. {
  1458. struct mt_cpu_dvfs *p = id_to_cpu_dvfs(id);
  1459. unsigned long flags;
  1460. int i;
  1461. int ret = -1;
  1462. FUNC_ENTER(FUNC_LV_API);
  1463. BUG_ON(NULL == p);
  1464. if (!cpu_dvfs_is_available(p)) {
  1465. FUNC_EXIT(FUNC_LV_API);
  1466. return 0;
  1467. }
  1468. BUG_ON(nr_volt_tbl > p->nr_opp_tbl);
  1469. cpufreq_lock(flags);
  1470. /* update volt table */
  1471. for (i = 0; i < nr_volt_tbl; i++)
  1472. p->opp_tbl[i].cpufreq_volt = PMIC_VAL_TO_VOLT(volt_tbl[i]);
  1473. /* set volt */
  1474. #ifdef CONFIG_CPU_DVFS_TURBO_MODE
  1475. ret = _mt_cpufreq_set_cur_volt_locked(
  1476. p,
  1477. TURBO_MODE_VOLT(
  1478. _mt_cpufreq_get_turbo_mode
  1479. (p, cpu_dvfs_get_cur_freq(p)),
  1480. cpu_dvfs_get_cur_volt(p)
  1481. )
  1482. );
  1483. #else
  1484. ret = _mt_cpufreq_set_cur_volt_locked(p, cpu_dvfs_get_cur_volt(p));
  1485. #endif
  1486. cpufreq_unlock(flags);
  1487. FUNC_EXIT(FUNC_LV_API);
  1488. return ret;
  1489. }
  1490. EXPORT_SYMBOL(mt_cpufreq_update_volt);
  1491. void mt_cpufreq_restore_default_volt(enum mt_cpu_dvfs_id id)
  1492. {
  1493. struct mt_cpu_dvfs *p = id_to_cpu_dvfs(id);
  1494. FUNC_ENTER(FUNC_LV_API);
  1495. BUG_ON(NULL == p);
  1496. if (!cpu_dvfs_is_available(p)) {
  1497. FUNC_EXIT(FUNC_LV_API);
  1498. return;
  1499. }
  1500. _mt_cpufreq_restore_default_volt(p);
  1501. FUNC_EXIT(FUNC_LV_API);
  1502. }
  1503. EXPORT_SYMBOL(mt_cpufreq_restore_default_volt);
  1504. static unsigned int _mt_cpufreq_cpu_freq_calc(unsigned int con1, unsigned int ckdiv1)
  1505. {
  1506. unsigned int freq = 0;
  1507. con1 &= _BITMASK_(26:0);
  1508. if (con1 >= DDS_DIV8_FREQ) {
  1509. freq = DDS_DIV8_FREQ;
  1510. freq = PLL_DIV8_FREQ + (((con1 - freq) / 0x2000) * PLL_FREQ_STEP / 8);
  1511. } else if (con1 >= DDS_DIV4_FREQ) {
  1512. freq = DDS_DIV4_FREQ;
  1513. freq = PLL_DIV4_FREQ + (((con1 - freq) / 0x2000) * PLL_FREQ_STEP / 4);
  1514. } else if (con1 >= DDS_DIV2_FREQ) {
  1515. freq = DDS_DIV2_FREQ;
  1516. freq = PLL_DIV2_FREQ + (((con1 - freq) / 0x2000) * PLL_FREQ_STEP / 2);
  1517. } else if (con1 >= DDS_DIV1_FREQ) {
  1518. freq = DDS_DIV1_FREQ;
  1519. freq = PLL_DIV1_FREQ + (((con1 - freq) / 0x2000) * PLL_FREQ_STEP);
  1520. } else {
  1521. cpufreq_err("@%s: Invalid DDS value = 0x%x\n", __func__, con1);
  1522. BUG();
  1523. }
  1524. FUNC_ENTER(FUNC_LV_HELP);
  1525. switch (ckdiv1) {
  1526. case 9:
  1527. freq = freq * 3 / 4;
  1528. break;
  1529. case 10:
  1530. freq = freq * 2 / 4;
  1531. break;
  1532. case 11:
  1533. freq = freq * 1 / 4;
  1534. break;
  1535. case 17:
  1536. freq = freq * 4 / 5;
  1537. break;
  1538. case 18:
  1539. freq = freq * 3 / 5;
  1540. break;
  1541. case 19:
  1542. freq = freq * 2 / 5;
  1543. break;
  1544. case 20:
  1545. freq = freq * 1 / 5;
  1546. break;
  1547. case 25:
  1548. freq = freq * 5 / 6;
  1549. break;
  1550. case 26:
  1551. freq = freq * 4 / 6;
  1552. break;
  1553. case 27:
  1554. freq = freq * 3 / 6;
  1555. break;
  1556. case 28:
  1557. freq = freq * 2 / 6;
  1558. break;
  1559. case 29:
  1560. freq = freq * 1 / 6;
  1561. break;
  1562. case 8:
  1563. case 16:
  1564. case 24:
  1565. default:
  1566. break;
  1567. }
  1568. FUNC_EXIT(FUNC_LV_HELP);
  1569. return freq; /* TODO: adjust by ptp level??? */
  1570. }
  1571. static unsigned int _mt_cpufreq_get_cur_phy_freq(struct mt_cpu_dvfs *p)
  1572. {
  1573. unsigned int con1;
  1574. unsigned int ckdiv1;
  1575. unsigned int cur_khz;
  1576. FUNC_ENTER(FUNC_LV_LOCAL);
  1577. BUG_ON(NULL == p);
  1578. con1 = cpufreq_read(ARMPLL_CON1);
  1579. ckdiv1 = cpufreq_read(TOP_CKDIV1);
  1580. ckdiv1 = _GET_BITS_VAL_(4:0, ckdiv1);
  1581. cur_khz = _mt_cpufreq_cpu_freq_calc(con1, ckdiv1);
  1582. cpufreq_ver("@%s: cur_khz = %d, con1 = 0x%x, ckdiv1_val = 0x%x\n",
  1583. __func__, cur_khz, con1, ckdiv1);
  1584. FUNC_EXIT(FUNC_LV_LOCAL);
  1585. return cur_khz;
  1586. }
  1587. static unsigned int _mt_cpufreq_cpu_dds_calc(unsigned int khz)
  1588. {
  1589. unsigned int dds = 0;
  1590. FUNC_ENTER(FUNC_LV_HELP);
  1591. if (khz >= PLL_DIV1_FREQ)
  1592. dds = DDS_DIV1_FREQ + ((khz - PLL_DIV1_FREQ) / PLL_FREQ_STEP) * 0x2000;
  1593. else if (khz >= PLL_DIV2_FREQ)
  1594. dds = DDS_DIV2_FREQ + ((khz - PLL_DIV2_FREQ) * 2 / PLL_FREQ_STEP) * 0x2000;
  1595. else if (khz >= PLL_DIV4_FREQ)
  1596. dds = DDS_DIV4_FREQ + ((khz - PLL_DIV4_FREQ) * 4 / PLL_FREQ_STEP) * 0x2000;
  1597. else if (khz >= PLL_DIV8_FREQ)
  1598. dds = DDS_DIV8_FREQ + ((khz - PLL_DIV8_FREQ) * 8 / PLL_FREQ_STEP) * 0x2000;
  1599. else
  1600. BUG();
  1601. FUNC_EXIT(FUNC_LV_HELP);
  1602. return dds;
  1603. }
  1604. static void _mt_cpufreq_set_cpu_clk_src(struct mt_cpu_dvfs *p, enum top_ckmuxsel sel)
  1605. {
  1606. FUNC_ENTER(FUNC_LV_HELP);
  1607. switch (sel) {
  1608. case TOP_CKMUXSEL_CLKSQ:
  1609. case TOP_CKMUXSEL_ARMPLL:
  1610. cpufreq_write_mask(TOP_CKMUXSEL, 3:2, sel);
  1611. /* disable MAINPLL_CORE_CK_CG_EN */
  1612. cpufreq_write_mask(AP_PLL_CON2, 5:5, 0x0);
  1613. break;
  1614. case TOP_CKMUXSEL_MAINPLL:
  1615. /* enable MAINPLL_CORE_CK_CG_EN */
  1616. cpufreq_write_mask(AP_PLL_CON2, 5:5, 0x1);
  1617. udelay(3);
  1618. cpufreq_write_mask(TOP_CKMUXSEL, 3:2, sel);
  1619. break;
  1620. default:
  1621. BUG();
  1622. break;
  1623. }
  1624. FUNC_EXIT(FUNC_LV_HELP);
  1625. }
  1626. static enum top_ckmuxsel _mt_cpufreq_get_cpu_clk_src(struct mt_cpu_dvfs *p)
  1627. {
  1628. unsigned int val = cpufreq_read(TOP_CKMUXSEL);
  1629. unsigned int mask = _BITMASK_(3:2);
  1630. FUNC_ENTER(FUNC_LV_HELP);
  1631. val = (val & mask) >> 2;
  1632. FUNC_EXIT(FUNC_LV_HELP);
  1633. return val;
  1634. }
  1635. int mt_cpufreq_set_cpu_clk_src(enum mt_cpu_dvfs_id id, enum top_ckmuxsel sel)
  1636. {
  1637. struct mt_cpu_dvfs *p = id_to_cpu_dvfs(id);
  1638. if (!p)
  1639. return -1;
  1640. _mt_cpufreq_set_cpu_clk_src(p, sel);
  1641. return 0;
  1642. }
  1643. enum top_ckmuxsel mt_cpufreq_get_cpu_clk_src(enum mt_cpu_dvfs_id id)
  1644. {
  1645. struct mt_cpu_dvfs *p = id_to_cpu_dvfs(id);
  1646. if (!p)
  1647. return -1;
  1648. return _mt_cpufreq_get_cpu_clk_src(p);
  1649. }
  1650. static void _mt_cpufreq_dfs_by_set_armpll(struct mt_cpu_dvfs *p, unsigned int dds,
  1651. unsigned int ckdiv_sel)
  1652. {
  1653. unsigned int ckdiv1_val = _GET_BITS_VAL_(4:0, cpufreq_read(TOP_CKDIV1));
  1654. unsigned int ckdiv1_mask = _BITMASK_(4:0);
  1655. /* set ARMPLL and CLKDIV */
  1656. #ifdef __KERNEL__
  1657. freqhopping_config(FH_ARM_PLLID, 1599000, 0); /* disable SSC */
  1658. #endif
  1659. _mt_cpufreq_set_cpu_clk_src(p, TOP_CKMUXSEL_MAINPLL);
  1660. cpufreq_write(ARMPLL_CON1, dds | _BIT_(31)); /* CHG */
  1661. udelay(PLL_SETTLE_TIME);
  1662. cpufreq_write(TOP_CKDIV1, (ckdiv1_val & ~ckdiv1_mask) | ckdiv_sel);
  1663. _mt_cpufreq_set_cpu_clk_src(p, TOP_CKMUXSEL_ARMPLL);
  1664. #ifdef __KERNEL__
  1665. freqhopping_config(FH_ARM_PLLID, 1599000, 1); /* enable SSC */
  1666. #endif
  1667. }
  1668. #ifdef CONFIG_ARCH_MT6735M
  1669. /*
  1670. * CPU freq scaling (only freqhopping)
  1671. */
  1672. static void _mt_cpufreq_set_cur_freq(struct mt_cpu_dvfs *p, unsigned int cur_khz,
  1673. unsigned int target_khz)
  1674. {
  1675. FUNC_ENTER(FUNC_LV_LOCAL);
  1676. if (cur_khz == target_khz)
  1677. return;
  1678. if (p->cpu_level == CPU_LEVEL_1) {
  1679. unsigned int dds = 0;
  1680. unsigned int sel = 0;
  1681. unsigned int ckdiv1_val = _GET_BITS_VAL_(4:0, cpufreq_read(TOP_CKDIV1));
  1682. unsigned int ckdiv1_mask = _BITMASK_(4:0);
  1683. switch (target_khz) {
  1684. case CPU_DVFS_FREQ0:
  1685. case CPU_DVFS_FREQ0_1:
  1686. case CPU_DVFS_FREQ1:
  1687. case CPU_DVFS_FREQ1_1:
  1688. case CPU_DVFS_FREQ2:
  1689. case CPU_DVFS_FREQ3:
  1690. case CPU_DVFS_FREQ4:
  1691. case CPU_DVFS_FREQ5:
  1692. case CPU_DVFS_FREQ6:
  1693. case CPU_DVFS_FREQ7:
  1694. dds = _mt_cpufreq_cpu_dds_calc(target_khz);
  1695. sel = 8; /* 4/4 */
  1696. break;
  1697. case CPU_DVFS_FREQ8:
  1698. case CPU_DVFS_FREQ9:
  1699. dds = _mt_cpufreq_cpu_dds_calc(target_khz * 2);
  1700. sel = 10; /* 2/4 */
  1701. break;
  1702. case CPU_DVFS_FREQ10:
  1703. dds = _mt_cpufreq_cpu_dds_calc(target_khz * 4);
  1704. sel = 11; /* 1/4 */
  1705. break;
  1706. default:
  1707. cpufreq_err("@%s: invalid target freq = %dKHz\n", __func__, target_khz);
  1708. BUG();
  1709. }
  1710. /* ignore postdiv */
  1711. dds &= ~(_BITMASK_(26:24));
  1712. cpufreq_ver("@%s():%d, cur_khz = %d, target_khz = %d, dds = 0x%x, sel = %d\n",
  1713. __func__, __LINE__, cur_khz, target_khz, dds, sel);
  1714. /* posdiv should be 2 */
  1715. /* BUG_ON((dds & _BITMASK_(26 : 24)) != (1 << 24)); */
  1716. #if !defined(__KERNEL__) /* for CTP */
  1717. if (target_khz > cur_khz) {
  1718. #if defined(MTKDRV_FREQHOP)
  1719. fhdrv_dvt_dvfs_enable(ARMPLL_ID, dds);
  1720. #endif
  1721. cpufreq_write(TOP_CKDIV1, (ckdiv1_val & ~ckdiv1_mask) | sel);
  1722. } else {
  1723. cpufreq_write(TOP_CKDIV1, (ckdiv1_val & ~ckdiv1_mask) | sel);
  1724. #if defined(MTKDRV_FREQHOP)
  1725. fhdrv_dvt_dvfs_enable(ARMPLL_ID, dds);
  1726. #endif
  1727. }
  1728. #else /* __KERNEL__ */
  1729. if (target_khz > cur_khz) {
  1730. mt_dfs_armpll(FH_ARM_PLLID, dds);
  1731. cpufreq_write(TOP_CKDIV1, (ckdiv1_val & ~ckdiv1_mask) | sel);
  1732. } else {
  1733. cpufreq_write(TOP_CKDIV1, (ckdiv1_val & ~ckdiv1_mask) | sel);
  1734. mt_dfs_armpll(FH_ARM_PLLID, dds);
  1735. }
  1736. #endif
  1737. } else {
  1738. unsigned int dds = 0;
  1739. unsigned int is_fhctl_used;
  1740. unsigned int sel = 0;
  1741. unsigned int cur_volt = 0;
  1742. if (((cur_khz < CPUFREQ_BOUNDARY_FOR_FHCTL) && (target_khz > CPUFREQ_BOUNDARY_FOR_FHCTL))
  1743. || ((target_khz < CPUFREQ_BOUNDARY_FOR_FHCTL) && (cur_khz > CPUFREQ_BOUNDARY_FOR_FHCTL))) {
  1744. _mt_cpufreq_set_cur_freq(p, cur_khz, CPUFREQ_BOUNDARY_FOR_FHCTL);
  1745. cur_khz = CPUFREQ_BOUNDARY_FOR_FHCTL;
  1746. }
  1747. is_fhctl_used = ((target_khz >= CPUFREQ_BOUNDARY_FOR_FHCTL) && (cur_khz >= CPUFREQ_BOUNDARY_FOR_FHCTL)) ? 1 : 0;
  1748. cpufreq_ver("@%s():%d, cur_khz = %d, target_khz = %d, is_fhctl_used = %d\n",
  1749. __func__,
  1750. __LINE__,
  1751. cur_khz,
  1752. target_khz,
  1753. is_fhctl_used
  1754. );
  1755. if (!is_fhctl_used) {
  1756. switch (target_khz) {
  1757. case CPU_DVFS_FREQ1_1:
  1758. case CPU_DVFS_FREQ1:
  1759. case CPU_DVFS_FREQ2:
  1760. case CPU_DVFS_FREQ3:
  1761. case CPU_DVFS_FREQ4:
  1762. case CPU_DVFS_FREQ5:
  1763. case CPU_DVFS_FREQ6:
  1764. case CPU_DVFS_FREQ7:
  1765. dds = _mt_cpufreq_cpu_dds_calc(target_khz);
  1766. sel = 8; /* 4/4 */
  1767. break;
  1768. case CPU_DVFS_FREQ8:
  1769. case CPU_DVFS_FREQ9:
  1770. dds = _mt_cpufreq_cpu_dds_calc(target_khz * 2);
  1771. sel = 10; /* 2/4 */
  1772. break;
  1773. case CPU_DVFS_FREQ10:
  1774. dds = _mt_cpufreq_cpu_dds_calc(target_khz * 4);
  1775. sel = 11; /* 1/4 */
  1776. break;
  1777. default:
  1778. cpufreq_err("@%s: invalid target freq = %dKHz\n", __func__, target_khz);
  1779. BUG();
  1780. }
  1781. cur_volt = p->ops->get_cur_volt(p);
  1782. if (cur_volt < 125000)
  1783. p->ops->set_cur_volt(p, 125000);
  1784. else
  1785. cur_volt = 0;
  1786. /* set ARMPLL and CLKDIV */
  1787. _mt_cpufreq_dfs_by_set_armpll(p, dds, sel);
  1788. /* restore Vproc */
  1789. if (cur_volt)
  1790. p->ops->set_cur_volt(p, cur_volt);
  1791. } else {
  1792. unsigned int ckdiv1_val = _GET_BITS_VAL_(4 : 0, cpufreq_read(TOP_CKDIV1));
  1793. #define IS_CLKDIV_USED(clkdiv) (((clkdiv < 8) || ((clkdiv % 8) == 0)) ? 0 : 1)
  1794. dds = _mt_cpufreq_cpu_dds_calc(target_khz);
  1795. BUG_ON(dds & _BITMASK_(26 : 24)); /* should not use posdiv */
  1796. BUG_ON(IS_CLKDIV_USED(ckdiv1_val)); /* should not use clkdiv */
  1797. #if !defined(__KERNEL__)
  1798. #if defined(MTKDRV_FREQHOP)
  1799. fhdrv_dvt_dvfs_enable(ARMPLL_ID, dds);
  1800. #else
  1801. _mt_cpufreq_dfs_by_set_armpll(p, dds, 8);
  1802. #endif
  1803. }
  1804. #else /* __KERNEL__ */
  1805. #if 1
  1806. mt_dfs_armpll(FH_ARM_PLLID, dds);
  1807. #else
  1808. _mt_cpufreq_dfs_by_set_armpll(p, dds, 8);
  1809. #endif
  1810. #endif /* ! __KERNEL__ */
  1811. }
  1812. }
  1813. FUNC_EXIT(FUNC_LV_LOCAL);
  1814. }
  1815. #else
  1816. /*
  1817. * CPU freq scaling (set ARMPLL + freqhopping)
  1818. *
  1819. * above 1GHz: use freq hopping
  1820. * below 1GHz: set ARMPLL and CLKDIV
  1821. * if cross 1GHz, migrate to 1GHz first.
  1822. *
  1823. */
  1824. static void _mt_cpufreq_set_cur_freq(struct mt_cpu_dvfs *p, unsigned int cur_khz,
  1825. unsigned int target_khz)
  1826. {
  1827. unsigned int dds = 0;
  1828. unsigned int is_fhctl_used;
  1829. unsigned int sel = 0;
  1830. unsigned int cur_volt = 0;
  1831. FUNC_ENTER(FUNC_LV_LOCAL);
  1832. if (cur_khz == target_khz)
  1833. return;
  1834. if (((cur_khz < CPUFREQ_BOUNDARY_FOR_FHCTL) && (target_khz > CPUFREQ_BOUNDARY_FOR_FHCTL))
  1835. || ((target_khz < CPUFREQ_BOUNDARY_FOR_FHCTL)
  1836. && (cur_khz > CPUFREQ_BOUNDARY_FOR_FHCTL))) {
  1837. _mt_cpufreq_set_cur_freq(p, cur_khz, CPUFREQ_BOUNDARY_FOR_FHCTL);
  1838. cur_khz = CPUFREQ_BOUNDARY_FOR_FHCTL;
  1839. }
  1840. is_fhctl_used = ((target_khz >= CPUFREQ_BOUNDARY_FOR_FHCTL)
  1841. && (cur_khz >= CPUFREQ_BOUNDARY_FOR_FHCTL)) ? 1 : 0;
  1842. cpufreq_ver("@%s():%d, cur_khz = %d, target_khz = %d, is_fhctl_used = %d\n",
  1843. __func__, __LINE__, cur_khz, target_khz, is_fhctl_used);
  1844. if (!is_fhctl_used) {
  1845. switch (target_khz) {
  1846. case CPU_DVFS_FREQ4:
  1847. case CPU_DVFS_FREQ4_1:
  1848. case CPU_DVFS_FREQ4_2:
  1849. case CPU_DVFS_FREQ5:
  1850. case CPU_DVFS_FREQ5_1:
  1851. case CPU_DVFS_FREQ5_2:
  1852. case CPU_DVFS_FREQ6:
  1853. dds = _mt_cpufreq_cpu_dds_calc(target_khz);
  1854. sel = 8; /* 4/4 */
  1855. break;
  1856. case CPU_DVFS_FREQ6_1:
  1857. case CPU_DVFS_FREQ7:
  1858. case CPU_DVFS_FREQ7_1:
  1859. case CPU_DVFS_FREQ8:
  1860. dds = _mt_cpufreq_cpu_dds_calc(target_khz * 2);
  1861. sel = 10; /* 2/4 */
  1862. break;
  1863. case CPU_DVFS_FREQ8_1:
  1864. dds = _mt_cpufreq_cpu_dds_calc(target_khz * 4);
  1865. sel = 11; /* 1/4 */
  1866. break;
  1867. default:
  1868. cpufreq_err("@%s: invalid target freq = %dKHz\n", __func__, target_khz);
  1869. BUG();
  1870. }
  1871. cur_volt = p->ops->get_cur_volt(p);
  1872. #ifdef CONFIG_ARCH_MT6735
  1873. /* Raise to 1.25V directly to avoid armpll_divider timing violation issue (only for 6735) */
  1874. if (cur_volt < 125000)
  1875. p->ops->set_cur_volt(p, 125000);
  1876. #else
  1877. /* Adjust Vproc since MAINPLL is 1092 MHz (~= CPU_DVFS_FREQ3 or FREQ3_1) */
  1878. if (cur_volt < cpu_dvfs_get_volt_by_idx(p, 2))
  1879. p->ops->set_cur_volt(p, cpu_dvfs_get_volt_by_idx(p, 2));
  1880. #endif
  1881. else
  1882. cur_volt = 0;
  1883. /* set ARMPLL and CLKDIV */
  1884. _mt_cpufreq_dfs_by_set_armpll(p, dds, sel);
  1885. /* restore Vproc */
  1886. if (cur_volt)
  1887. p->ops->set_cur_volt(p, cur_volt);
  1888. } else {
  1889. unsigned int ckdiv1_val = _GET_BITS_VAL_(4:0, cpufreq_read(TOP_CKDIV1));
  1890. #define IS_CLKDIV_USED(clkdiv) (((clkdiv < 8) || ((clkdiv % 8) == 0)) ? 0 : 1)
  1891. dds = _mt_cpufreq_cpu_dds_calc(target_khz);
  1892. BUG_ON(dds & _BITMASK_(26:24)); /* should not use posdiv */
  1893. BUG_ON(IS_CLKDIV_USED(ckdiv1_val)); /* should not use clkdiv */
  1894. #if !defined(__KERNEL__)
  1895. #if defined(MTKDRV_FREQHOP)
  1896. fhdrv_dvt_dvfs_enable(ARMPLL_ID, dds);
  1897. #else
  1898. _mt_cpufreq_dfs_by_set_armpll(p, dds, 8);
  1899. #endif
  1900. #else /* __KERNEL__ */
  1901. #if 1
  1902. mt_dfs_armpll(FH_ARM_PLLID, dds);
  1903. #else
  1904. _mt_cpufreq_dfs_by_set_armpll(p, dds, 8);
  1905. #endif
  1906. #endif /* ! __KERNEL__ */
  1907. }
  1908. FUNC_EXIT(FUNC_LV_LOCAL);
  1909. }
  1910. #endif
  1911. /* for volt change (PMICWRAP/extBuck) */
  1912. static unsigned int _mt_cpufreq_get_cur_volt(struct mt_cpu_dvfs *p)
  1913. {
  1914. unsigned int rdata = 0;
  1915. unsigned int retry_cnt = 5;
  1916. FUNC_ENTER(FUNC_LV_LOCAL);
  1917. rdata = pmic_get_register_value(PMIC_VPROC_EN);
  1918. if (rdata) { /* enabled i.e. not 0mv */
  1919. do {
  1920. rdata = pmic_get_register_value(PMIC_VPROC_VOSEL_ON);
  1921. } while ((rdata == 0 || rdata > 0x7F) && retry_cnt--);
  1922. rdata = PMIC_VAL_TO_VOLT(rdata);
  1923. /* cpufreq_ver("@%s: volt = %d\n", __func__, rdata); */
  1924. } else
  1925. cpufreq_err("@%s: read VPROC_EN failed, rdata = 0x%x\n", __func__, rdata);
  1926. FUNC_EXIT(FUNC_LV_LOCAL);
  1927. return rdata; /* vproc: mv*100 */
  1928. }
  1929. unsigned int mt_cpufreq_get_cur_volt(enum mt_cpu_dvfs_id id)
  1930. {
  1931. struct mt_cpu_dvfs *p = id_to_cpu_dvfs(id);
  1932. FUNC_ENTER(FUNC_LV_API);
  1933. BUG_ON(NULL == p);
  1934. BUG_ON(NULL == p->ops);
  1935. FUNC_EXIT(FUNC_LV_API);
  1936. return p->ops->get_cur_volt(p); /* mv * 100 */
  1937. }
  1938. EXPORT_SYMBOL(mt_cpufreq_get_cur_volt);
  1939. static unsigned int _mt_cpufreq_calc_pmic_settle_time(unsigned int old_vproc,
  1940. unsigned int old_vsram,
  1941. unsigned int new_vproc,
  1942. unsigned int new_vsram)
  1943. {
  1944. unsigned delay = 100;
  1945. if (new_vproc == old_vproc && new_vsram == old_vsram)
  1946. return 0;
  1947. /* VPROC is UP */
  1948. if (new_vproc >= old_vproc) {
  1949. /* VSRAM is UP too, choose larger one to calculate settle time */
  1950. if (new_vsram >= old_vsram)
  1951. delay = MAX(PMIC_VOLT_UP_SETTLE_TIME(old_vsram, new_vsram),
  1952. PMIC_VOLT_UP_SETTLE_TIME(old_vproc, new_vproc)
  1953. );
  1954. /* VSRAM is DOWN, it may happen at bootup stage */
  1955. else
  1956. delay = MAX(PMIC_VOLT_DOWN_SETTLE_TIME(old_vsram, new_vsram),
  1957. PMIC_VOLT_UP_SETTLE_TIME(old_vproc, new_vproc)
  1958. );
  1959. }
  1960. /* VPROC is DOWN */
  1961. else {
  1962. /* VSRAM is DOWN too, choose larger one to calculate settle time */
  1963. if (old_vsram >= new_vsram)
  1964. delay = MAX(PMIC_VOLT_DOWN_SETTLE_TIME(old_vsram, new_vsram),
  1965. PMIC_VOLT_DOWN_SETTLE_TIME(old_vproc, new_vproc)
  1966. );
  1967. /* VSRAM is UP, it may happen at bootup stage */
  1968. else
  1969. delay = MAX(PMIC_VOLT_UP_SETTLE_TIME(old_vsram, new_vsram),
  1970. PMIC_VOLT_DOWN_SETTLE_TIME(old_vproc, new_vproc)
  1971. );
  1972. }
  1973. if (delay < MIN_PMIC_SETTLE_TIME)
  1974. delay = MIN_PMIC_SETTLE_TIME;
  1975. return delay;
  1976. }
  1977. static int _mt_cpufreq_set_cur_volt(struct mt_cpu_dvfs *p, unsigned int volt)
  1978. { /* volt: vproc (mv*100) */
  1979. unsigned int cur_volt = _mt_cpufreq_get_cur_volt(p);
  1980. #ifdef CONFIG_CPU_DVFS_TURBO_MODE
  1981. bool is_leaving_turbo_mode = false;
  1982. #endif
  1983. FUNC_ENTER(FUNC_LV_LOCAL);
  1984. #ifdef CONFIG_CPU_DVFS_AEE_RR_REC
  1985. aee_rr_rec_cpu_dvfs_vproc_little(VOLT_TO_PMIC_VAL(volt));
  1986. #endif
  1987. #ifdef CONFIG_CPU_DVFS_TURBO_MODE
  1988. if (is_in_turbo_mode && cur_volt > cpu_dvfs_get_volt_by_idx(p, 0)
  1989. && volt <= cpu_dvfs_get_volt_by_idx(p, 0))
  1990. is_leaving_turbo_mode = true;
  1991. #endif
  1992. pmic_set_register_value(PMIC_VPROC_VOSEL_ON, VOLT_TO_PMIC_VAL(volt));
  1993. /* delay for scaling up */
  1994. if (volt > cur_volt)
  1995. /* vsram is autotracking, bypass it */
  1996. udelay(_mt_cpufreq_calc_pmic_settle_time(cur_volt, 0, volt, 0));
  1997. if (NULL != g_pCpuVoltSampler)
  1998. g_pCpuVoltSampler(MT_CPU_DVFS_LITTLE, volt / 100); /* mv */
  1999. #ifdef CONFIG_CPU_DVFS_TURBO_MODE
  2000. if (is_leaving_turbo_mode) {
  2001. /* cpufreq_dbg("@%s: turbo mode end\n", __func__); */
  2002. is_in_turbo_mode = false;
  2003. #ifdef CONFIG_CPU_DVFS_AEE_RR_REC
  2004. aee_rr_rec_cpu_dvfs_status(
  2005. aee_rr_curr_cpu_dvfs_status() & ~(1 << CPU_DVFS_LITTLE_IS_TURBO));
  2006. #endif
  2007. }
  2008. #endif
  2009. FUNC_EXIT(FUNC_LV_LOCAL);
  2010. return 0;
  2011. }
  2012. #ifdef CONFIG_CPU_DVFS_HAS_EXTBUCK
  2013. /* for SW tracking error handling */
  2014. static void _mt_cpufreq_dump_opp_table(struct mt_cpu_dvfs *p)
  2015. {
  2016. int i;
  2017. cpufreq_err("[%s/%d]\n" "cpufreq_oppidx = %d\n", p->name, p->cpu_id, p->idx_opp_tbl);
  2018. for (i = 0; i < p->nr_opp_tbl; i++) {
  2019. cpufreq_err("\tOP(%d, %d),\n",
  2020. cpu_dvfs_get_freq_by_idx(p, i), cpu_dvfs_get_volt_by_idx(p, i)
  2021. );
  2022. }
  2023. }
  2024. static unsigned int _mt_cpufreq_get_cur_vsram(struct mt_cpu_dvfs *p)
  2025. {
  2026. unsigned int rdata = 0;
  2027. unsigned int retry_cnt = 5;
  2028. FUNC_ENTER(FUNC_LV_LOCAL);
  2029. rdata = pmic_get_register_value(PMIC_RG_VSRAM_EN);
  2030. if (rdata) { /* enabled i.e. not 0mv */
  2031. do {
  2032. rdata = pmic_get_register_value(PMIC_RG_VSRAM_VOSEL);
  2033. } while ((rdata == 0 || rdata > 0x7F) && retry_cnt--);
  2034. rdata = PMIC_VAL_TO_VOLT(rdata);
  2035. /* cpufreq_ver("@%s: volt = %d\n", __func__, rdata); */
  2036. } else
  2037. cpufreq_err("@%s: read VSRAM_EN failed, rdata = 0x%x\n", __func__, rdata);
  2038. FUNC_EXIT(FUNC_LV_LOCAL);
  2039. return rdata; /* vproc: mv*100 */
  2040. }
  2041. static unsigned int _mt_cpufreq_get_cur_volt_extbuck(struct mt_cpu_dvfs *p)
  2042. {
  2043. unsigned char ret_val = 0;
  2044. unsigned int ret_volt = 0; /* volt: mv * 100 */
  2045. unsigned int retry_cnt = 5;
  2046. FUNC_ENTER(FUNC_LV_LOCAL);
  2047. if (cpu_dvfs_is_extbuck_valid()) {
  2048. do {
  2049. if (!mt6311_read_byte(MT6311_VDVFS11_CON13, &ret_val)) {
  2050. cpufreq_err("%s(), fail to read ext buck volt\n", __func__);
  2051. ret_volt = 0;
  2052. } else {
  2053. ret_volt = EXTBUCK_VAL_TO_VOLT(ret_val);
  2054. cpufreq_ver("@%s: volt = %d\n", __func__, ret_volt);
  2055. }
  2056. /* XXX: EXTBUCK_VAL_TO_VOLT(0) is impossible setting and need to retry */
  2057. } while (ret_volt == EXTBUCK_VAL_TO_VOLT(0) && retry_cnt--);
  2058. } else
  2059. cpufreq_err("%s(), can not use ext buck!\n", __func__);
  2060. FUNC_EXIT(FUNC_LV_LOCAL);
  2061. return ret_volt;
  2062. }
  2063. /* volt: vproc (mv*100) */
  2064. static int _mt_cpufreq_set_cur_volt_extbuck(struct mt_cpu_dvfs *p, unsigned int volt)
  2065. {
  2066. unsigned int cur_vsram = _mt_cpufreq_get_cur_vsram(p);
  2067. unsigned int cur_vproc = _mt_cpufreq_get_cur_volt_extbuck(p);
  2068. unsigned int delay_us = 0;
  2069. #ifdef CONFIG_CPU_DVFS_TURBO_MODE
  2070. bool is_leaving_turbo_mode = false;
  2071. #endif
  2072. int ret = 0;
  2073. FUNC_ENTER(FUNC_LV_LOCAL);
  2074. #ifdef CONFIG_CPU_DVFS_AEE_RR_REC
  2075. aee_rr_rec_cpu_dvfs_vproc_little(VOLT_TO_EXTBUCK_VAL(volt));
  2076. #endif
  2077. if (cur_vproc == 0 || !cpu_dvfs_is_extbuck_valid()) {
  2078. cpufreq_err("@%s():%d, can not use ext buck!\n", __func__, __LINE__);
  2079. return -1;
  2080. }
  2081. #ifdef CONFIG_CPU_DVFS_TURBO_MODE
  2082. if (is_in_turbo_mode && cur_vproc > cpu_dvfs_get_volt_by_idx(p, 0)
  2083. && volt <= cpu_dvfs_get_volt_by_idx(p, 0))
  2084. is_leaving_turbo_mode = true;
  2085. #endif
  2086. if (unlikely
  2087. (!((cur_vsram > cur_vproc) && (MAX_DIFF_VSRAM_VPROC >= (cur_vsram - cur_vproc))))) {
  2088. unsigned int i, val, extbuck_chip_id = mt6311_get_chip_id();
  2089. _mt_cpufreq_dump_opp_table(p);
  2090. cpufreq_err("@%s():%d, cur_vsram = %d, cur_vproc = %d, extbuck_chip_id = 0x%x\n",
  2091. __func__, __LINE__, cur_vsram, cur_vproc, extbuck_chip_id);
  2092. /* read extbuck chip id to verify I2C is still worked or not */
  2093. for (i = 0; i < 10; i++) {
  2094. val = ((mt6311_get_cid() << 8) | (mt6311_get_swcid()));
  2095. cpufreq_err("read 6311 chip id from I2C, id = 0x%x\n", val);
  2096. }
  2097. /* read pmic wrap chip id */
  2098. for (i = 0; i < 10; i++) {
  2099. /* pwrap_read(0x200, &val); */
  2100. val = pmic_get_register_value(PMIC_HWCID);
  2101. cpufreq_err("pmic 6328 HW CID = %x\n", val);
  2102. }
  2103. aee_kernel_warning(TAG, "@%s():%d, cur_vsram = %d, cur_vproc = %d\n",
  2104. __func__, __LINE__, cur_vsram, cur_vproc);
  2105. cur_vproc = cpu_dvfs_get_cur_volt(p);
  2106. cur_vsram = cur_vproc + NORMAL_DIFF_VSRAM_VPROC;
  2107. }
  2108. /* UP */
  2109. if (volt > cur_vproc) {
  2110. unsigned int target_vsram = volt + NORMAL_DIFF_VSRAM_VPROC;
  2111. unsigned int next_vsram;
  2112. do {
  2113. unsigned int old_vproc = cur_vproc;
  2114. unsigned int old_vsram = cur_vsram;
  2115. next_vsram = MIN(((MAX_DIFF_VSRAM_VPROC - 2500) + cur_vproc), target_vsram);
  2116. /* update vsram */
  2117. cur_vsram = MAX(next_vsram, MIN_VSRAM_VOLT);
  2118. if (cur_vsram > MAX_VSRAM_VOLT) {
  2119. cur_vsram = MAX_VSRAM_VOLT;
  2120. target_vsram = MAX_VSRAM_VOLT; /* to end the loop */
  2121. }
  2122. if (unlikely
  2123. (!((cur_vsram > cur_vproc)
  2124. && (MAX_DIFF_VSRAM_VPROC >= (cur_vsram - cur_vproc))))) {
  2125. _mt_cpufreq_dump_opp_table(p);
  2126. cpufreq_err("@%s():%d, cur_vsram = %d, cur_vproc = %d\n",
  2127. __func__, __LINE__, cur_vsram, cur_vproc);
  2128. BUG();
  2129. }
  2130. pmic_set_register_value(PMIC_RG_VSRAM_VOSEL, VOLT_TO_PMIC_VAL(cur_vsram));
  2131. /* update vproc */
  2132. if (next_vsram > MAX_VSRAM_VOLT)
  2133. cur_vproc = volt; /* Vsram was limited, set to target vproc directly */
  2134. else
  2135. cur_vproc = next_vsram - NORMAL_DIFF_VSRAM_VPROC;
  2136. if (unlikely
  2137. (!((cur_vsram > cur_vproc)
  2138. && (MAX_DIFF_VSRAM_VPROC >= (cur_vsram - cur_vproc))))) {
  2139. _mt_cpufreq_dump_opp_table(p);
  2140. cpufreq_err("@%s():%d, cur_vsram = %d, cur_vproc = %d\n",
  2141. __func__, __LINE__, cur_vsram, cur_vproc);
  2142. BUG();
  2143. }
  2144. if (cpu_dvfs_is_extbuck_valid()) {
  2145. mt6311_set_vdvfs11_vosel_on(VOLT_TO_EXTBUCK_VAL(cur_vproc));
  2146. } else {
  2147. cpufreq_err("%s(), fail to set ext buck volt\n", __func__);
  2148. ret = -1;
  2149. break;
  2150. }
  2151. delay_us =
  2152. _mt_cpufreq_calc_pmic_settle_time(
  2153. old_vproc, old_vsram, cur_vproc, cur_vsram);
  2154. udelay(delay_us);
  2155. cpufreq_ver("@%s(): UP --> old_vsram=%d, cur_vsram=%d, old_vproc=%d, cur_vproc=%d, delay=%d\n",
  2156. __func__, old_vsram, cur_vsram, old_vproc, cur_vproc, delay_us);
  2157. } while (target_vsram > cur_vsram);
  2158. }
  2159. /* DOWN */
  2160. else if (volt < cur_vproc) {
  2161. unsigned int next_vproc;
  2162. unsigned int next_vsram = cur_vproc + NORMAL_DIFF_VSRAM_VPROC;
  2163. do {
  2164. unsigned int old_vproc = cur_vproc;
  2165. unsigned int old_vsram = cur_vsram;
  2166. next_vproc = MAX((next_vsram - (MAX_DIFF_VSRAM_VPROC - 2500)), volt);
  2167. /* update vproc */
  2168. cur_vproc = next_vproc;
  2169. if (unlikely
  2170. (!((cur_vsram > cur_vproc)
  2171. && (MAX_DIFF_VSRAM_VPROC >= (cur_vsram - cur_vproc))))) {
  2172. _mt_cpufreq_dump_opp_table(p);
  2173. cpufreq_err("@%s():%d, cur_vsram = %d, cur_vproc = %d\n",
  2174. __func__, __LINE__, cur_vsram, cur_vproc);
  2175. BUG();
  2176. }
  2177. if (cpu_dvfs_is_extbuck_valid()) {
  2178. mt6311_set_vdvfs11_vosel_on(VOLT_TO_EXTBUCK_VAL(cur_vproc));
  2179. } else {
  2180. cpufreq_err("%s(), fail to set ext buck volt\n", __func__);
  2181. ret = -1;
  2182. break;
  2183. }
  2184. /* update vsram */
  2185. next_vsram = cur_vproc + NORMAL_DIFF_VSRAM_VPROC;
  2186. cur_vsram = MAX(next_vsram, MIN_VSRAM_VOLT);
  2187. cur_vsram = MIN(cur_vsram, MAX_VSRAM_VOLT);
  2188. if (unlikely
  2189. (!((cur_vsram > cur_vproc)
  2190. && (MAX_DIFF_VSRAM_VPROC >= (cur_vsram - cur_vproc))))) {
  2191. _mt_cpufreq_dump_opp_table(p);
  2192. cpufreq_err("@%s():%d, cur_vsram = %d, cur_vproc = %d\n",
  2193. __func__, __LINE__, cur_vsram, cur_vproc);
  2194. BUG();
  2195. }
  2196. pmic_set_register_value(PMIC_RG_VSRAM_VOSEL, VOLT_TO_PMIC_VAL(cur_vsram));
  2197. delay_us =
  2198. _mt_cpufreq_calc_pmic_settle_time(
  2199. old_vproc, old_vsram, cur_vproc, cur_vsram);
  2200. udelay(delay_us);
  2201. cpufreq_ver(
  2202. "@%s(): DOWN --> old_vsram=%d, cur_vsram=%d, old_vproc=%d, cur_vproc=%d, delay=%d\n",
  2203. __func__, old_vsram, cur_vsram, old_vproc, cur_vproc, delay_us);
  2204. } while (cur_vproc > volt);
  2205. }
  2206. if (NULL != g_pCpuVoltSampler)
  2207. g_pCpuVoltSampler(MT_CPU_DVFS_LITTLE, volt / 100); /* mv */
  2208. cpufreq_ver("@%s():%d, cur_vsram = %d, cur_vproc = %d\n",
  2209. __func__, __LINE__, cur_vsram, cur_vproc);
  2210. #ifdef CONFIG_CPU_DVFS_TURBO_MODE
  2211. if (is_leaving_turbo_mode) {
  2212. /* cpufreq_dbg("@%s: turbo mode end\n", __func__); */
  2213. is_in_turbo_mode = false;
  2214. #ifdef CONFIG_CPU_DVFS_AEE_RR_REC
  2215. aee_rr_rec_cpu_dvfs_status(
  2216. aee_rr_curr_cpu_dvfs_status() & ~(1 << CPU_DVFS_LITTLE_IS_TURBO));
  2217. #endif
  2218. }
  2219. #endif
  2220. FUNC_EXIT(FUNC_LV_LOCAL);
  2221. return ret;
  2222. }
  2223. #endif
  2224. /* cpufreq set (freq & volt) */
  2225. static unsigned int _mt_cpufreq_search_available_volt(struct mt_cpu_dvfs *p,
  2226. unsigned int target_khz)
  2227. {
  2228. int i;
  2229. FUNC_ENTER(FUNC_LV_HELP);
  2230. BUG_ON(NULL == p);
  2231. /* search available voltage */
  2232. for (i = p->nr_opp_tbl - 1; i >= 0; i--) {
  2233. if (target_khz <= cpu_dvfs_get_freq_by_idx(p, i))
  2234. break;
  2235. }
  2236. BUG_ON(i < 0); /* i.e. target_khz > p->opp_tbl[0].cpufreq_khz */
  2237. FUNC_EXIT(FUNC_LV_HELP);
  2238. return cpu_dvfs_get_volt_by_idx(p, i); /* mv * 100 */
  2239. }
  2240. static int _mt_cpufreq_set_locked(struct mt_cpu_dvfs *p, unsigned int cur_khz,
  2241. unsigned int target_khz, struct cpufreq_policy *policy)
  2242. {
  2243. unsigned int volt; /* mv * 100 */
  2244. int ret = 0;
  2245. #ifdef CONFIG_CPU_FREQ
  2246. struct cpufreq_freqs freqs;
  2247. /* unsigned int cpu; */
  2248. unsigned int target_khz_orig = target_khz;
  2249. #endif
  2250. #ifdef CONFIG_CPU_DVFS_TURBO_MODE
  2251. enum turbo_mode mode = _mt_cpufreq_get_turbo_mode(p, target_khz);
  2252. #endif
  2253. FUNC_ENTER(FUNC_LV_HELP);
  2254. volt = _mt_cpufreq_search_available_volt(p, target_khz);
  2255. #ifdef CONFIG_CPU_DVFS_TURBO_MODE
  2256. if (cur_khz != TURBO_MODE_FREQ(mode, target_khz))
  2257. cpufreq_ver("@%s(), target_khz = %d (%d), volt = %d (%d), num_online_cpus = %d, cur_khz = %d\n",
  2258. __func__, target_khz, TURBO_MODE_FREQ(mode, target_khz), volt,
  2259. TURBO_MODE_VOLT(mode, volt), num_online_cpus(), cur_khz
  2260. );
  2261. volt = TURBO_MODE_VOLT(mode, volt);
  2262. target_khz = TURBO_MODE_FREQ(mode, target_khz);
  2263. #else
  2264. if (cur_khz != target_khz)
  2265. cpufreq_ver("@%s(), target_khz = %d, volt = %d, cur_khz = %d\n",
  2266. __func__, target_khz, volt, cur_khz);
  2267. #endif
  2268. if (cur_khz == target_khz)
  2269. goto out;
  2270. /* set volt (UP) */
  2271. if (cur_khz < target_khz) {
  2272. ret = p->ops->set_cur_volt(p, volt);
  2273. if (ret) /* set volt fail */
  2274. goto out;
  2275. }
  2276. #ifdef CONFIG_CPU_FREQ
  2277. freqs.old = cur_khz;
  2278. /* new freq without turbo */
  2279. freqs.new = target_khz_orig;
  2280. #if 0
  2281. if (policy) {
  2282. for_each_online_cpu(cpu) {
  2283. freqs.cpu = cpu;
  2284. cpufreq_freq_transition_begin(policy, &freqs);
  2285. }
  2286. }
  2287. #else
  2288. /* fix notify transition hang issue for Linux-3.18 */
  2289. if (policy) {
  2290. freqs.cpu = policy->cpu;
  2291. cpufreq_freq_transition_begin(policy, &freqs);
  2292. }
  2293. #endif
  2294. #endif
  2295. /* set freq (UP/DOWN) */
  2296. if (cur_khz != target_khz)
  2297. p->ops->set_cur_freq(p, cur_khz, target_khz);
  2298. #ifdef CONFIG_CPU_FREQ
  2299. #if 0
  2300. if (policy) {
  2301. for_each_online_cpu(cpu) {
  2302. freqs.cpu = cpu;
  2303. cpufreq_freq_transition_end(policy, &freqs, 0);
  2304. }
  2305. }
  2306. #else
  2307. /* fix notify transition hang issue for Linux-3.18 */
  2308. if (policy)
  2309. cpufreq_freq_transition_end(policy, &freqs, 0);
  2310. #endif
  2311. #endif
  2312. /* set volt (DOWN) */
  2313. if (cur_khz > target_khz) {
  2314. ret = p->ops->set_cur_volt(p, volt);
  2315. if (ret) /* set volt fail */
  2316. goto out;
  2317. }
  2318. cpufreq_dbg("@%s(): Vproc = %dmv, freq = %d KHz\n",
  2319. __func__, (p->ops->get_cur_volt(p)) / 100, p->ops->get_cur_phy_freq(p)
  2320. );
  2321. #ifdef CONFIG_CPU_DVFS_HAS_EXTBUCK
  2322. /* SW tracking, print Vsram result */
  2323. if (cpu_dvfs_is_extbuck_valid())
  2324. cpufreq_dbg("@%s(): Vsram = %dmv\n", __func__, _mt_cpufreq_get_cur_vsram(p) / 100);
  2325. #endif
  2326. #ifndef DISABLE_PBM_FEATURE
  2327. if (!p->dvfs_disable_by_suspend)
  2328. _kick_PBM_by_cpu(p);
  2329. #endif
  2330. /* trigger exception if freq/volt not correct during stress */
  2331. if (do_dvfs_stress_test) {
  2332. BUG_ON(p->ops->get_cur_volt(p) != volt);
  2333. BUG_ON(p->ops->get_cur_phy_freq(p) != target_khz);
  2334. }
  2335. FUNC_EXIT(FUNC_LV_HELP);
  2336. out:
  2337. return ret;
  2338. }
  2339. /* return -1 (not found) */
  2340. static int _mt_cpufreq_get_idx_by_freq(struct mt_cpu_dvfs *p, unsigned int target_khz,
  2341. unsigned int relation)
  2342. {
  2343. int new_opp_idx = -1;
  2344. int i;
  2345. FUNC_ENTER(FUNC_LV_HELP);
  2346. if (CPUFREQ_RELATION_L == relation) {
  2347. for (i = (signed)(p->nr_opp_tbl - 1); i >= 0; i--) {
  2348. if (cpu_dvfs_get_freq_by_idx(p, i) >= target_khz) {
  2349. new_opp_idx = i;
  2350. break;
  2351. }
  2352. }
  2353. } else { /* CPUFREQ_RELATION_H */
  2354. for (i = 0; i < (signed)p->nr_opp_tbl; i++) {
  2355. if (cpu_dvfs_get_freq_by_idx(p, i) <= target_khz) {
  2356. new_opp_idx = i;
  2357. break;
  2358. }
  2359. }
  2360. }
  2361. FUNC_EXIT(FUNC_LV_HELP);
  2362. return new_opp_idx;
  2363. }
  2364. #ifdef CONFIG_ARCH_MT6753
  2365. static bool is_limit_modified_by_5A_throttle;
  2366. #endif
  2367. static int _mt_cpufreq_power_limited_verify(struct mt_cpu_dvfs *p, int new_opp_idx)
  2368. {
  2369. unsigned int target_khz = cpu_dvfs_get_freq_by_idx(p, new_opp_idx);
  2370. int possible_cpu = 0;
  2371. unsigned int online_cpu = 0;
  2372. int found = 0;
  2373. int i;
  2374. FUNC_ENTER(FUNC_LV_HELP);
  2375. possible_cpu = num_possible_cpus();
  2376. online_cpu = num_online_cpus();
  2377. /* cpufreq_dbg("%s(): begin, idx = %d, online_cpu = %d\n", __func__, new_opp_idx, online_cpu); */
  2378. /* no limited */
  2379. #ifndef DISABLE_PBM_FEATURE
  2380. if (0 == p->limited_power_by_thermal && 0 == p->limited_power_by_pbm)
  2381. #else
  2382. if (0 == p->limited_power_by_thermal)
  2383. #endif
  2384. return new_opp_idx;
  2385. #ifdef CONFIG_ARCH_MT6753
  2386. if (is_need_5A_throttle(p, p->limited_max_freq, p->limited_max_ncpu)) {
  2387. cpufreq_ver("@%s: modify limited max freq and ncpu due to 5A limit enabled!\n", __func__);
  2388. p->limited_max_freq = PMIC_5A_THRO_MAX_CPU_FREQ;
  2389. p->limited_max_ncpu = possible_cpu;
  2390. p->limited_power_idx = 3;
  2391. is_limit_modified_by_5A_throttle = true;
  2392. } else if (is_limit_modified_by_5A_throttle) {
  2393. /* re-calculate limit */
  2394. #ifndef DISABLE_PBM_FEATURE
  2395. if (p->limited_power_by_pbm && p->limited_power_by_thermal)
  2396. _mt_cpufreq_set_limit_by_pwr_budget(MIN(p->limited_power_by_pbm, p->limited_power_by_thermal));
  2397. else if (p->limited_power_by_pbm)
  2398. _mt_cpufreq_set_limit_by_pwr_budget(p->limited_power_by_pbm);
  2399. else if (p->limited_power_by_thermal)
  2400. _mt_cpufreq_set_limit_by_pwr_budget(p->limited_power_by_thermal);
  2401. else {
  2402. /* unlimit */
  2403. p->limited_max_freq = cpu_dvfs_get_max_freq(p);
  2404. p->limited_max_ncpu = possible_cpu;
  2405. p->limited_power_idx = 0;
  2406. }
  2407. #else
  2408. if (p->limited_power_by_thermal)
  2409. _mt_cpufreq_set_limit_by_pwr_budget(p->limited_power_by_thermal);
  2410. else {
  2411. /* unlimit */
  2412. p->limited_max_freq = cpu_dvfs_get_max_freq(p);
  2413. p->limited_max_ncpu = possible_cpu;
  2414. p->limited_power_idx = 0;
  2415. }
  2416. #endif
  2417. is_limit_modified_by_5A_throttle = false;
  2418. }
  2419. #endif
  2420. for (i = 0; i < p->nr_opp_tbl * possible_cpu; i++) {
  2421. if (p->power_tbl[i].cpufreq_ncpu == p->limited_max_ncpu
  2422. && p->power_tbl[i].cpufreq_khz == p->limited_max_freq)
  2423. break;
  2424. }
  2425. cpufreq_ver("%s(): idx = %d, limited_max_ncpu = %d, limited_max_freq = %d\n",
  2426. __func__, i, p->limited_max_ncpu, p->limited_max_freq);
  2427. for (; i < p->nr_opp_tbl * possible_cpu; i++) {
  2428. if (p->power_tbl[i].cpufreq_ncpu == online_cpu) {
  2429. if (target_khz >= p->power_tbl[i].cpufreq_khz) {
  2430. found = 1;
  2431. break;
  2432. }
  2433. }
  2434. }
  2435. if (found) {
  2436. target_khz = p->power_tbl[i].cpufreq_khz;
  2437. cpufreq_ver("%s(): freq found, idx = %d, target_khz = %d, online_cpu = %d\n",
  2438. __func__, i, target_khz, online_cpu);
  2439. } else {
  2440. target_khz = p->limited_max_freq;
  2441. cpufreq_dbg("%s(): freq not found, set to limited_max_freq = %d\n",
  2442. __func__, target_khz);
  2443. }
  2444. /* TODO: refine this function for idx searching */
  2445. i = _mt_cpufreq_get_idx_by_freq(p, target_khz, CPUFREQ_RELATION_H);
  2446. FUNC_EXIT(FUNC_LV_HELP);
  2447. return i;
  2448. }
  2449. static unsigned int _mt_cpufreq_calc_new_opp_idx(struct mt_cpu_dvfs *p, int new_opp_idx)
  2450. {
  2451. int idx;
  2452. FUNC_ENTER(FUNC_LV_HELP);
  2453. BUG_ON(NULL == p);
  2454. /* kdriver limit min freq */
  2455. if (p->limited_min_freq_by_kdriver) {
  2456. idx = _mt_cpufreq_get_idx_by_freq(p, p->limited_min_freq_by_kdriver, CPUFREQ_RELATION_L);
  2457. if (idx != -1 && new_opp_idx > idx) {
  2458. new_opp_idx = idx;
  2459. cpufreq_ver("%s(): kdriver limited freq, idx = %d\n", __func__, new_opp_idx);
  2460. }
  2461. }
  2462. /* HEVC */
  2463. if (p->limited_freq_by_hevc) {
  2464. idx = _mt_cpufreq_get_idx_by_freq(p, p->limited_freq_by_hevc, CPUFREQ_RELATION_L);
  2465. if (idx != -1 && new_opp_idx > idx) {
  2466. new_opp_idx = idx;
  2467. cpufreq_ver("%s(): hevc limited freq, idx = %d\n", __func__, new_opp_idx);
  2468. }
  2469. }
  2470. /* search power limited freq */
  2471. idx = _mt_cpufreq_power_limited_verify(p, new_opp_idx);
  2472. if (idx != -1 && idx != new_opp_idx) {
  2473. new_opp_idx = idx;
  2474. cpufreq_ver("%s(): thermal/DLPT limited freq, idx = %d\n", __func__, new_opp_idx);
  2475. }
  2476. /* for early suspend */
  2477. if (p->dvfs_disable_by_early_suspend) {
  2478. if (is_fix_freq_in_ES) {
  2479. /* Fix at about 1GHz to fix OGG Vorbis Playback noise issue */
  2480. idx =
  2481. _mt_cpufreq_get_idx_by_freq(
  2482. p, CPUFREQ_FIX_FREQ_FOR_ES, CPUFREQ_RELATION_L);
  2483. if (idx != -1)
  2484. new_opp_idx = idx;
  2485. else
  2486. new_opp_idx = p->idx_normal_max_opp;
  2487. } else {
  2488. if (new_opp_idx > p->idx_normal_max_opp)
  2489. new_opp_idx = p->idx_normal_max_opp;
  2490. }
  2491. cpufreq_ver("%s(): for early suspend, idx = %d\n", __func__, new_opp_idx);
  2492. }
  2493. /* for suspend */
  2494. if (p->dvfs_disable_by_suspend)
  2495. new_opp_idx = p->idx_normal_max_opp;
  2496. /* for power throttling */
  2497. #ifdef CONFIG_CPU_DVFS_POWER_THROTTLING
  2498. if (p->pwr_thro_mode && new_opp_idx < p->idx_pwr_thro_max_opp) {
  2499. new_opp_idx = p->idx_pwr_thro_max_opp;
  2500. cpufreq_ver("%s(): for power throttling = %d\n", __func__, new_opp_idx);
  2501. }
  2502. #endif
  2503. /* limit max freq by user */
  2504. if (p->limited_max_freq_by_user) {
  2505. idx = _mt_cpufreq_get_idx_by_freq(
  2506. p, p->limited_max_freq_by_user, CPUFREQ_RELATION_H);
  2507. if (idx != -1 && new_opp_idx < idx) {
  2508. new_opp_idx = idx;
  2509. cpufreq_ver("%s(): limited max freq by user, idx = %d\n",
  2510. __func__, new_opp_idx);
  2511. }
  2512. }
  2513. /* kdriver limit max freq */
  2514. if (p->limited_max_freq_by_kdriver) {
  2515. idx = _mt_cpufreq_get_idx_by_freq(
  2516. p, p->limited_max_freq_by_kdriver, CPUFREQ_RELATION_H);
  2517. if (idx != -1 && new_opp_idx < idx) {
  2518. new_opp_idx = idx;
  2519. cpufreq_ver("%s(): limited max freq by kdriver, idx = %d\n",
  2520. __func__, new_opp_idx);
  2521. }
  2522. }
  2523. /* for ptpod init */
  2524. if (p->dvfs_disable_by_ptpod) {
  2525. #ifdef CONFIG_ARCH_MT6753
  2526. /* idx 2 for Vboot = 1.2V */
  2527. new_opp_idx = 2;
  2528. #else
  2529. /* idx 0 for Vboot = 1.25V */
  2530. new_opp_idx = 0;
  2531. #endif
  2532. cpufreq_info("%s(): for ptpod init, idx = %d\n", __func__, new_opp_idx);
  2533. }
  2534. #ifdef CONFIG_ARCH_MT6753
  2535. if (is_need_5A_throttle(p, cpu_dvfs_get_freq_by_idx(p, new_opp_idx),
  2536. num_online_cpus() + num_online_cpus_delta)) {
  2537. idx = _mt_cpufreq_get_idx_by_freq(p, PMIC_5A_THRO_MAX_CPU_FREQ, CPUFREQ_RELATION_H);
  2538. if (idx != -1 && new_opp_idx < idx) {
  2539. new_opp_idx = idx;
  2540. cpufreq_ver("%s(): limited max freq by PMIC 5A throttle, idx = %d\n",
  2541. __func__, new_opp_idx);
  2542. }
  2543. }
  2544. #endif
  2545. #ifdef CONFIG_CPU_DVFS_AEE_RR_REC
  2546. aee_rr_rec_cpu_dvfs_oppidx((aee_rr_curr_cpu_dvfs_oppidx() & 0xF0) | new_opp_idx);
  2547. #endif
  2548. FUNC_EXIT(FUNC_LV_HELP);
  2549. return new_opp_idx;
  2550. }
  2551. static void _mt_cpufreq_set(enum mt_cpu_dvfs_id id, int new_opp_idx)
  2552. {
  2553. unsigned long flags;
  2554. struct mt_cpu_dvfs *p = id_to_cpu_dvfs(id);
  2555. unsigned int cur_freq;
  2556. unsigned int target_freq;
  2557. #ifdef CONFIG_CPU_FREQ
  2558. struct cpufreq_policy *policy;
  2559. #endif
  2560. FUNC_ENTER(FUNC_LV_LOCAL);
  2561. BUG_ON(NULL == p);
  2562. BUG_ON(new_opp_idx >= p->nr_opp_tbl);
  2563. #ifdef CONFIG_CPU_FREQ
  2564. policy = cpufreq_cpu_get(p->cpu_id);
  2565. #endif
  2566. cpufreq_lock(flags); /* <-XXX */
  2567. /* get current idx here to avoid idx synchronization issue */
  2568. if (new_opp_idx == -1)
  2569. new_opp_idx = p->idx_opp_tbl;
  2570. if (do_dvfs_stress_test && !(p->dvfs_disable_by_suspend))
  2571. new_opp_idx = jiffies & 0x7; /* 0~7 */
  2572. else
  2573. new_opp_idx = _mt_cpufreq_calc_new_opp_idx(id_to_cpu_dvfs(id), new_opp_idx);
  2574. cur_freq = p->ops->get_cur_phy_freq(p);
  2575. target_freq = cpu_dvfs_get_freq_by_idx(p, new_opp_idx);
  2576. #ifdef CONFIG_CPU_FREQ
  2577. _mt_cpufreq_set_locked(p, cur_freq, target_freq, policy);
  2578. #else
  2579. _mt_cpufreq_set_locked(p, cur_freq, target_freq, NULL);
  2580. #endif
  2581. p->idx_opp_tbl = new_opp_idx;
  2582. cpufreq_unlock(flags); /* <-XXX */
  2583. #ifdef CONFIG_CPU_FREQ
  2584. if (policy)
  2585. cpufreq_cpu_put(policy);
  2586. #endif
  2587. FUNC_EXIT(FUNC_LV_LOCAL);
  2588. }
  2589. static int __cpuinit _mt_cpufreq_cpu_CB(struct notifier_block *nfb, unsigned long action,
  2590. void *hcpu)
  2591. {
  2592. unsigned int cpu = (unsigned long)hcpu;
  2593. unsigned int online_cpus = num_online_cpus();
  2594. struct device *dev;
  2595. struct mt_cpu_dvfs *p = id_to_cpu_dvfs(0);
  2596. cpufreq_ver("@%s():%d, cpu = %d, action = %lu, oppidx = %d, num_online_cpus = %d\n",
  2597. __func__, __LINE__, cpu, action, p->idx_opp_tbl, online_cpus); /* <-XXX */
  2598. dev = get_cpu_device(cpu);
  2599. if (dev) {
  2600. #ifdef CONFIG_ARCH_MT6753
  2601. cpufreq_ver("@%s():%d, num_online_cpus_delta = %d\n",
  2602. __func__, __LINE__, num_online_cpus_delta);
  2603. if ((p->cpu_level == CPU_LEVEL_1 && !cpu_dvfs_is_extbuck_valid()
  2604. && PMIC_5A_THRO_MAX_CPU_CORE_NUM == online_cpus)
  2605. #ifdef CONFIG_CPU_DVFS_TURBO_MODE
  2606. || TURBO_MODE_BOUNDARY_CPU_NUM == online_cpus
  2607. #endif
  2608. ) {
  2609. switch (action) {
  2610. case CPU_UP_PREPARE:
  2611. case CPU_UP_PREPARE_FROZEN:
  2612. num_online_cpus_delta = 1;
  2613. case CPU_DEAD:
  2614. case CPU_DEAD_FROZEN:
  2615. _mt_cpufreq_set(MT_CPU_DVFS_LITTLE, -1);
  2616. break;
  2617. }
  2618. } else {
  2619. switch (action) {
  2620. case CPU_ONLINE: /* CPU UP done */
  2621. case CPU_ONLINE_FROZEN:
  2622. case CPU_UP_CANCELED: /* CPU UP failed */
  2623. case CPU_UP_CANCELED_FROZEN:
  2624. num_online_cpus_delta = 0;
  2625. break;
  2626. }
  2627. }
  2628. cpufreq_ver("@%s():%d, num_online_cpus_delta = %d\n",
  2629. __func__, __LINE__, num_online_cpus_delta);
  2630. #endif
  2631. #ifndef DISABLE_PBM_FEATURE
  2632. /* Notify PBM after CPU on/off */
  2633. if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN
  2634. || action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
  2635. unsigned long flags;
  2636. cpufreq_lock(flags);
  2637. if (!p->dvfs_disable_by_suspend)
  2638. _kick_PBM_by_cpu(p);
  2639. cpufreq_unlock(flags);
  2640. }
  2641. #endif
  2642. cpufreq_ver("@%s():%d, cpu = %d, action = %lu, oppidx = %d, num_online_cpus = %d\n",
  2643. __func__, __LINE__, cpu, action, p->idx_opp_tbl, online_cpus); /* <-XXX */
  2644. }
  2645. return NOTIFY_OK;
  2646. }
  2647. static int _mt_cpufreq_sync_opp_tbl_idx(struct mt_cpu_dvfs *p)
  2648. {
  2649. int ret = -1;
  2650. unsigned int freq;
  2651. int i;
  2652. FUNC_ENTER(FUNC_LV_HELP);
  2653. BUG_ON(NULL == p);
  2654. BUG_ON(NULL == p->opp_tbl);
  2655. BUG_ON(NULL == p->ops);
  2656. freq = p->ops->get_cur_phy_freq(p);
  2657. for (i = p->nr_opp_tbl - 1; i >= 0; i--) {
  2658. if (freq <= cpu_dvfs_get_freq_by_idx(p, i)) {
  2659. p->idx_opp_tbl = i;
  2660. break;
  2661. }
  2662. }
  2663. if (i >= 0) {
  2664. cpufreq_info("%s freq = %d\n", cpu_dvfs_get_name(p), cpu_dvfs_get_cur_freq(p));
  2665. /* TODO: apply correct voltage??? */
  2666. ret = 0;
  2667. } else
  2668. cpufreq_warn("%s can't find freq = %d\n", cpu_dvfs_get_name(p), freq);
  2669. FUNC_EXIT(FUNC_LV_HELP);
  2670. return ret;
  2671. }
  2672. #ifdef CONFIG_ARCH_MT6735
  2673. unsigned int leakage_data[NR_MAX_OPP_TBL] = {638, 594, 535, 424, 344, 279, 227, 183};
  2674. #endif
  2675. static void _mt_cpufreq_power_calculation(struct mt_cpu_dvfs *p, int oppidx, int ncpu)
  2676. {
  2677. #ifdef CONFIG_ARCH_MT6753
  2678. #define CA53_REF_POWER 2456 /* mW */
  2679. #define CA53_REF_FREQ 1300000 /* KHz */
  2680. #elif defined(CONFIG_ARCH_MT6735M)
  2681. #define CA53_REF_POWER 988 /* mW */
  2682. #define CA53_REF_FREQ 1000000 /* KHz */
  2683. #else
  2684. #define CA53_REF_POWER 1228 /* mW */
  2685. #define CA53_REF_FREQ 1300000 /* KHz */
  2686. #endif
  2687. #define CA53_REF_VOLT 125000 /* mV * 100 */
  2688. int p_dynamic = 0, p_leakage = 0, ref_freq, ref_volt;
  2689. int possible_cpu = num_possible_cpus();
  2690. FUNC_ENTER(FUNC_LV_HELP);
  2691. ref_freq = CA53_REF_FREQ;
  2692. ref_volt = CA53_REF_VOLT;
  2693. p_dynamic = CA53_REF_POWER;
  2694. /* TODO: Use temp=65 to calculate leakage? check this! */
  2695. #ifdef CONFIG_ARCH_MT6735
  2696. if (p->cpu_level == CPU_LEVEL_3)
  2697. p_leakage = leakage_data[oppidx];
  2698. else
  2699. p_leakage = mt_spower_get_leakage(MT_SPOWER_CPU, p->opp_tbl[oppidx].cpufreq_volt / 100, 65);
  2700. #else
  2701. p_leakage = mt_spower_get_leakage(MT_SPOWER_CPU, p->opp_tbl[oppidx].cpufreq_volt / 100, 65);
  2702. #endif
  2703. p_dynamic = p_dynamic *
  2704. (p->opp_tbl[oppidx].cpufreq_khz / 1000) / (ref_freq / 1000) *
  2705. p->opp_tbl[oppidx].cpufreq_volt / ref_volt *
  2706. p->opp_tbl[oppidx].cpufreq_volt / ref_volt + p_leakage;
  2707. p->power_tbl[NR_MAX_OPP_TBL * (possible_cpu - 1 - ncpu) + oppidx].cpufreq_ncpu = ncpu + 1;
  2708. p->power_tbl[NR_MAX_OPP_TBL * (possible_cpu - 1 - ncpu) + oppidx].cpufreq_khz =
  2709. p->opp_tbl[oppidx].cpufreq_khz;
  2710. p->power_tbl[NR_MAX_OPP_TBL * (possible_cpu - 1 - ncpu) + oppidx].cpufreq_power =
  2711. p_dynamic * (ncpu + 1) / possible_cpu;
  2712. FUNC_EXIT(FUNC_LV_HELP);
  2713. }
  2714. static int _mt_cpufreq_setup_power_table(struct mt_cpu_dvfs *p)
  2715. {
  2716. #ifdef CONFIG_ARCH_MT6753
  2717. static const unsigned int pwr_tbl_cgf[NR_MAX_CPU] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  2718. #else
  2719. static const unsigned int pwr_tbl_cgf[NR_MAX_CPU] = { 0, 0, 0, 0 };
  2720. #endif
  2721. unsigned int pwr_eff_tbl[NR_MAX_OPP_TBL][NR_MAX_CPU];
  2722. unsigned int pwr_eff_num;
  2723. int possible_cpu = num_possible_cpus();
  2724. int i, j;
  2725. int ret = 0;
  2726. FUNC_ENTER(FUNC_LV_LOCAL);
  2727. BUG_ON(NULL == p);
  2728. if (p->power_tbl)
  2729. goto out;
  2730. /* allocate power table */
  2731. memset((void *)pwr_eff_tbl, 0, sizeof(pwr_eff_tbl));
  2732. p->power_tbl =
  2733. kzalloc(p->nr_opp_tbl * possible_cpu * sizeof(struct mt_cpu_power_info), GFP_KERNEL);
  2734. if (NULL == p->power_tbl) {
  2735. ret = -ENOMEM;
  2736. goto out;
  2737. }
  2738. /* setup power efficiency array */
  2739. for (i = 0, pwr_eff_num = 0; i < possible_cpu; i++) {
  2740. if (1 == pwr_tbl_cgf[i])
  2741. pwr_eff_num++;
  2742. }
  2743. for (i = 0; i < p->nr_opp_tbl; i++) {
  2744. for (j = 0; j < possible_cpu; j++) {
  2745. if (1 == pwr_tbl_cgf[j])
  2746. pwr_eff_tbl[i][j] = 1;
  2747. }
  2748. }
  2749. p->nr_power_tbl = p->nr_opp_tbl * (possible_cpu - pwr_eff_num);
  2750. /* calc power and fill in power table */
  2751. for (i = 0; i < p->nr_opp_tbl; i++) {
  2752. for (j = 0; j < possible_cpu; j++) {
  2753. if (0 == pwr_eff_tbl[i][j])
  2754. _mt_cpufreq_power_calculation(p, i, j);
  2755. }
  2756. }
  2757. /* sort power table */
  2758. for (i = p->nr_opp_tbl * possible_cpu; i > 0; i--) {
  2759. for (j = 1; j < i; j++) {
  2760. if (p->power_tbl[j - 1].cpufreq_power < p->power_tbl[j].cpufreq_power) {
  2761. struct mt_cpu_power_info tmp;
  2762. tmp.cpufreq_khz = p->power_tbl[j - 1].cpufreq_khz;
  2763. tmp.cpufreq_ncpu = p->power_tbl[j - 1].cpufreq_ncpu;
  2764. tmp.cpufreq_power = p->power_tbl[j - 1].cpufreq_power;
  2765. p->power_tbl[j - 1].cpufreq_khz = p->power_tbl[j].cpufreq_khz;
  2766. p->power_tbl[j - 1].cpufreq_ncpu = p->power_tbl[j].cpufreq_ncpu;
  2767. p->power_tbl[j - 1].cpufreq_power = p->power_tbl[j].cpufreq_power;
  2768. p->power_tbl[j].cpufreq_khz = tmp.cpufreq_khz;
  2769. p->power_tbl[j].cpufreq_ncpu = tmp.cpufreq_ncpu;
  2770. p->power_tbl[j].cpufreq_power = tmp.cpufreq_power;
  2771. }
  2772. }
  2773. }
  2774. /* dump power table */
  2775. for (i = 0; i < p->nr_opp_tbl * possible_cpu; i++) {
  2776. cpufreq_info
  2777. ("[%d] = { .cpufreq_khz = %d,\t.cpufreq_ncpu = %d,\t.cpufreq_power = %d }\n", i,
  2778. p->power_tbl[i].cpufreq_khz, p->power_tbl[i].cpufreq_ncpu,
  2779. p->power_tbl[i].cpufreq_power);
  2780. }
  2781. #if 0 /* unused */
  2782. #ifdef CONFIG_THERMAL
  2783. mtk_cpufreq_register(p->power_tbl, p->nr_power_tbl);
  2784. #endif
  2785. #endif
  2786. out:
  2787. FUNC_EXIT(FUNC_LV_LOCAL);
  2788. return ret;
  2789. }
  2790. static int _mt_cpufreq_setup_freqs_table(struct cpufreq_policy *policy,
  2791. struct mt_cpu_freq_info *freqs, int num)
  2792. {
  2793. struct mt_cpu_dvfs *p;
  2794. struct cpufreq_frequency_table *table;
  2795. int i, ret = 0;
  2796. FUNC_ENTER(FUNC_LV_LOCAL);
  2797. BUG_ON(NULL == policy);
  2798. BUG_ON(NULL == freqs);
  2799. p = id_to_cpu_dvfs(_get_cpu_dvfs_id(policy->cpu));
  2800. if (NULL == p->freq_tbl_for_cpufreq) {
  2801. table = kzalloc((num + 1) * sizeof(*table), GFP_KERNEL);
  2802. if (NULL == table) {
  2803. ret = -ENOMEM;
  2804. goto out;
  2805. }
  2806. for (i = 0; i < num; i++) {
  2807. table[i].driver_data = i;
  2808. table[i].frequency = freqs[i].cpufreq_khz;
  2809. }
  2810. table[num].driver_data = i; /* TODO: FIXME, why need this??? */
  2811. table[num].frequency = CPUFREQ_TABLE_END;
  2812. p->opp_tbl = freqs;
  2813. p->nr_opp_tbl = num;
  2814. p->freq_tbl_for_cpufreq = table;
  2815. }
  2816. #ifdef CONFIG_CPU_FREQ
  2817. ret = cpufreq_frequency_table_cpuinfo(policy, p->freq_tbl_for_cpufreq);
  2818. #if 0 /* not available for kernel 3.18 */
  2819. if (!ret)
  2820. cpufreq_frequency_table_get_attr(p->freq_tbl_for_cpufreq, policy->cpu);
  2821. #else
  2822. if (!ret)
  2823. policy->freq_table = p->freq_tbl_for_cpufreq;
  2824. #endif
  2825. #endif
  2826. if (NULL == p->power_tbl)
  2827. p->ops->setup_power_table(p);
  2828. out:
  2829. FUNC_EXIT(FUNC_LV_LOCAL);
  2830. return 0;
  2831. }
  2832. void mt_cpufreq_enable_by_ptpod(enum mt_cpu_dvfs_id id)
  2833. {
  2834. struct mt_cpu_dvfs *p = id_to_cpu_dvfs(id);
  2835. FUNC_ENTER(FUNC_LV_API);
  2836. BUG_ON(NULL == p);
  2837. p->dvfs_disable_by_ptpod = false;
  2838. /* Turbo mode is enabled if efuse turbo bit is set */
  2839. #ifdef CONFIG_CPU_DVFS_TURBO_MODE
  2840. /* TODO: add turbo bit check HERE! */
  2841. if (0) {
  2842. cpufreq_info("@%s: Turbo mode enabled!\n", __func__);
  2843. p->turbo_mode = 1;
  2844. }
  2845. #endif
  2846. if (!cpu_dvfs_is_available(p)) {
  2847. FUNC_EXIT(FUNC_LV_API);
  2848. return;
  2849. }
  2850. _mt_cpufreq_set(id, p->idx_opp_tbl_for_late_resume);
  2851. FUNC_EXIT(FUNC_LV_API);
  2852. }
  2853. EXPORT_SYMBOL(mt_cpufreq_enable_by_ptpod);
  2854. unsigned int mt_cpufreq_disable_by_ptpod(enum mt_cpu_dvfs_id id)
  2855. {
  2856. struct mt_cpu_dvfs *p = id_to_cpu_dvfs(id);
  2857. FUNC_ENTER(FUNC_LV_API);
  2858. BUG_ON(NULL == p);
  2859. p->dvfs_disable_by_ptpod = true;
  2860. if (!cpu_dvfs_is_available(p)) {
  2861. FUNC_EXIT(FUNC_LV_API);
  2862. return 0;
  2863. }
  2864. p->idx_opp_tbl_for_late_resume = p->idx_opp_tbl;
  2865. _mt_cpufreq_set(id, p->idx_normal_max_opp); /* XXX: useless, decided @ _mt_cpufreq_calc_new_opp_idx() */
  2866. FUNC_EXIT(FUNC_LV_API);
  2867. return cpu_dvfs_get_cur_freq(p);
  2868. }
  2869. EXPORT_SYMBOL(mt_cpufreq_disable_by_ptpod);
  2870. int mt_cpufreq_set_lte_volt(int pmic_val)
  2871. {
  2872. pmic_set_register_value(PMIC_VLTE_VOSEL_ON, pmic_val);
  2873. return 0;
  2874. }
  2875. EXPORT_SYMBOL(mt_cpufreq_set_lte_volt);
  2876. void mt_cpufreq_thermal_protect(unsigned int limited_power)
  2877. {
  2878. FUNC_ENTER(FUNC_LV_API);
  2879. cpufreq_info("%s(): limited_power = %d\n", __func__, limited_power);
  2880. #ifdef CONFIG_CPU_FREQ
  2881. {
  2882. struct cpufreq_policy *policy;
  2883. struct mt_cpu_dvfs *p;
  2884. int possible_cpu;
  2885. int found = 0;
  2886. unsigned long flags;
  2887. policy = cpufreq_cpu_get(0); /* TODO: FIXME if it has more than one DVFS policy */
  2888. if (NULL == policy) {
  2889. cpufreq_warn("@%s: No DVFS policy, DVFS driver not init yet!\n", __func__);
  2890. goto no_policy;
  2891. }
  2892. p = id_to_cpu_dvfs(_get_cpu_dvfs_id(policy->cpu));
  2893. BUG_ON(NULL == p);
  2894. cpufreq_lock(flags); /* <- lock */
  2895. /* save current oppidx */
  2896. if (!p->limited_power_by_thermal)
  2897. p->idx_opp_tbl_for_thermal_thro = p->idx_opp_tbl;
  2898. p->limited_power_by_thermal = limited_power;
  2899. possible_cpu = num_possible_cpus();
  2900. /* no limited */
  2901. if (0 == limited_power) {
  2902. #ifdef CONFIG_ARCH_MT6753
  2903. if (p->cpu_level == CPU_LEVEL_1) {
  2904. /* give a large budget to find the first safe limit combination */
  2905. _mt_cpufreq_set_limit_by_pwr_budget(99999);
  2906. } else {
  2907. p->limited_max_ncpu = possible_cpu;
  2908. p->limited_max_freq = cpu_dvfs_get_max_freq(p);
  2909. p->limited_power_idx = 0;
  2910. }
  2911. #else
  2912. p->limited_max_ncpu = possible_cpu;
  2913. p->limited_max_freq = cpu_dvfs_get_max_freq(p);
  2914. p->limited_power_idx = 0;
  2915. #endif
  2916. /* restore oppidx */
  2917. p->idx_opp_tbl = p->idx_opp_tbl_for_thermal_thro;
  2918. } else
  2919. found = _mt_cpufreq_set_limit_by_pwr_budget(limited_power);
  2920. cpufreq_dbg(
  2921. "@%s: found = %d, limited_power_idx = %d, limited_max_freq = %d, limited_max_ncpu = %d\n",
  2922. __func__, found, p->limited_power_idx, p->limited_max_freq, p->limited_max_ncpu);
  2923. cpufreq_unlock(flags); /* <- unlock */
  2924. hps_set_cpu_num_limit(LIMIT_THERMAL, p->limited_max_ncpu, 0);
  2925. /* correct opp idx will be calcualted in _mt_cpufreq_power_limited_verify() */
  2926. _mt_cpufreq_set(MT_CPU_DVFS_LITTLE, -1);
  2927. cpufreq_cpu_put(policy); /* <- policy put */
  2928. }
  2929. no_policy:
  2930. #endif
  2931. FUNC_EXIT(FUNC_LV_API);
  2932. }
  2933. EXPORT_SYMBOL(mt_cpufreq_thermal_protect);
  2934. void mt_cpufreq_thermal_5A_limit(bool enable)
  2935. {
  2936. FUNC_ENTER(FUNC_LV_API);
  2937. cpufreq_info("%s(): PMIC 5A limit = %d\n", __func__, enable);
  2938. #ifdef CONFIG_ARCH_MT6753
  2939. {
  2940. struct mt_cpu_dvfs *p = id_to_cpu_dvfs(MT_CPU_DVFS_LITTLE);
  2941. pmic_5A_throttle_on = enable;
  2942. if (cpu_dvfs_is_available(p))
  2943. _mt_cpufreq_set(MT_CPU_DVFS_LITTLE, -1);
  2944. }
  2945. #endif
  2946. FUNC_EXIT(FUNC_LV_API);
  2947. }
  2948. EXPORT_SYMBOL(mt_cpufreq_thermal_5A_limit);
  2949. #ifdef CONFIG_CPU_DVFS_POWER_THROTTLING
  2950. static void _mt_cpufreq_calc_power_throttle_idx(struct mt_cpu_dvfs *p)
  2951. {
  2952. FUNC_ENTER(FUNC_LV_HELP);
  2953. cpufreq_ver("%s(): original idx = %d\n", __func__, p->idx_pwr_thro_max_opp);
  2954. if (!p->pwr_thro_mode)
  2955. p->idx_pwr_thro_max_opp = 0;
  2956. else if ((p->pwr_thro_mode & PWR_THRO_MODE_LBAT_819MHZ)
  2957. || (p->pwr_thro_mode & PWR_THRO_MODE_BAT_PER_819MHZ)) {
  2958. switch (p->cpu_level) {
  2959. #ifdef CONFIG_ARCH_MT6735M
  2960. case CPU_LEVEL_0:
  2961. case CPU_LEVEL_1:
  2962. case CPU_LEVEL_2:
  2963. case CPU_LEVEL_3:
  2964. p->idx_pwr_thro_max_opp = 3;
  2965. break;
  2966. #else /* 6735/6753 */
  2967. case CPU_LEVEL_0:
  2968. case CPU_LEVEL_1:
  2969. p->idx_pwr_thro_max_opp = 4;
  2970. break;
  2971. #ifdef CONFIG_ARCH_MT6735
  2972. case CPU_LEVEL_2:
  2973. p->idx_pwr_thro_max_opp = 1;
  2974. break;
  2975. case CPU_LEVEL_3:
  2976. p->idx_pwr_thro_max_opp = 4;
  2977. break;
  2978. case CPU_LEVEL_4:
  2979. p->idx_pwr_thro_max_opp = 3;
  2980. break;
  2981. #endif
  2982. #endif
  2983. default:
  2984. break;
  2985. }
  2986. } else if (p->pwr_thro_mode & PWR_THRO_MODE_BAT_OC_1040MHZ) {
  2987. switch (p->cpu_level) {
  2988. #ifdef CONFIG_ARCH_MT6735M
  2989. case CPU_LEVEL_0:
  2990. case CPU_LEVEL_2:
  2991. case CPU_LEVEL_3:
  2992. p->idx_pwr_thro_max_opp = 1;
  2993. break;
  2994. case CPU_LEVEL_1:
  2995. p->idx_pwr_thro_max_opp = 0;
  2996. break;
  2997. #else /* 6735/6753 */
  2998. case CPU_LEVEL_0:
  2999. case CPU_LEVEL_1:
  3000. p->idx_pwr_thro_max_opp = 3;
  3001. break;
  3002. #ifdef CONFIG_ARCH_MT6735
  3003. case CPU_LEVEL_2:
  3004. p->idx_pwr_thro_max_opp = 0;
  3005. break;
  3006. case CPU_LEVEL_3:
  3007. p->idx_pwr_thro_max_opp = 3;
  3008. break;
  3009. case CPU_LEVEL_4:
  3010. p->idx_pwr_thro_max_opp = 1;
  3011. break;
  3012. #endif
  3013. #endif
  3014. default:
  3015. break;
  3016. }
  3017. }
  3018. cpufreq_ver("%s(): new idx = %d\n", __func__, p->idx_pwr_thro_max_opp);
  3019. FUNC_EXIT(FUNC_LV_HELP);
  3020. }
  3021. static void _mt_cpufreq_power_throttle_bat_per_CB(BATTERY_PERCENT_LEVEL level)
  3022. {
  3023. struct mt_cpu_dvfs *p;
  3024. int i;
  3025. unsigned long flags;
  3026. cpufreq_dbg("@%s: level: %d\n", __func__, level);
  3027. for_each_cpu_dvfs(i, p) {
  3028. if (!cpu_dvfs_is_available(p))
  3029. continue;
  3030. cpufreq_lock(flags);
  3031. if (!p->pwr_thro_mode)
  3032. p->idx_opp_tbl_for_pwr_thro = p->idx_opp_tbl;
  3033. switch (level) {
  3034. case BATTERY_PERCENT_LEVEL_1:
  3035. /* Trigger CPU Limit to under 819M */
  3036. p->pwr_thro_mode |= PWR_THRO_MODE_BAT_PER_819MHZ;
  3037. break;
  3038. default:
  3039. /* Unlimit CPU */
  3040. p->pwr_thro_mode &= ~PWR_THRO_MODE_BAT_PER_819MHZ;
  3041. break;
  3042. }
  3043. _mt_cpufreq_calc_power_throttle_idx(p);
  3044. if (!p->pwr_thro_mode)
  3045. p->idx_opp_tbl = p->idx_opp_tbl_for_pwr_thro;
  3046. cpufreq_unlock(flags);
  3047. #ifdef CONFIG_ARCH_MT6753
  3048. switch (level) {
  3049. case BATTERY_PERCENT_LEVEL_1:
  3050. /* Limit CPU core num to 4 */
  3051. hps_set_cpu_num_limit(LIMIT_LOW_BATTERY, 4, 0);
  3052. break;
  3053. default:
  3054. /* Unlimit CPU core num if no other limit */
  3055. if (!p->pwr_thro_mode)
  3056. hps_set_cpu_num_limit(LIMIT_LOW_BATTERY, num_possible_cpus(), 0);
  3057. break;
  3058. }
  3059. #endif
  3060. _mt_cpufreq_set(MT_CPU_DVFS_LITTLE, -1);
  3061. }
  3062. }
  3063. static void _mt_cpufreq_power_throttle_bat_oc_CB(BATTERY_OC_LEVEL level)
  3064. {
  3065. struct mt_cpu_dvfs *p;
  3066. int i;
  3067. unsigned long flags;
  3068. cpufreq_dbg("@%s: level: %d\n", __func__, level);
  3069. for_each_cpu_dvfs(i, p) {
  3070. if (!cpu_dvfs_is_available(p))
  3071. continue;
  3072. cpufreq_lock(flags);
  3073. if (!p->pwr_thro_mode)
  3074. p->idx_opp_tbl_for_pwr_thro = p->idx_opp_tbl;
  3075. switch (level) {
  3076. case BATTERY_OC_LEVEL_1:
  3077. /* Trigger CPU Limit to under 1G */
  3078. p->pwr_thro_mode |= PWR_THRO_MODE_BAT_OC_1040MHZ;
  3079. break;
  3080. default:
  3081. /* Unlimit CPU */
  3082. p->pwr_thro_mode &= ~PWR_THRO_MODE_BAT_OC_1040MHZ;
  3083. break;
  3084. }
  3085. _mt_cpufreq_calc_power_throttle_idx(p);
  3086. if (!p->pwr_thro_mode)
  3087. p->idx_opp_tbl = p->idx_opp_tbl_for_pwr_thro;
  3088. cpufreq_unlock(flags);
  3089. #ifdef CONFIG_ARCH_MT6753
  3090. switch (level) {
  3091. case BATTERY_OC_LEVEL_1:
  3092. /* Limit CPU core num to 4 */
  3093. hps_set_cpu_num_limit(LIMIT_LOW_BATTERY, 4, 0);
  3094. break;
  3095. default:
  3096. /* Unlimit CPU core num if no other limit */
  3097. if (!p->pwr_thro_mode)
  3098. hps_set_cpu_num_limit(LIMIT_LOW_BATTERY, num_possible_cpus(), 0);
  3099. break;
  3100. }
  3101. #endif
  3102. _mt_cpufreq_set(MT_CPU_DVFS_LITTLE, -1);
  3103. }
  3104. }
  3105. void _mt_cpufreq_power_throttle_low_bat_CB(LOW_BATTERY_LEVEL level)
  3106. {
  3107. struct mt_cpu_dvfs *p;
  3108. int i;
  3109. unsigned long flags;
  3110. cpufreq_dbg("@%s: level: %d\n", __func__, level);
  3111. for_each_cpu_dvfs(i, p) {
  3112. if (!cpu_dvfs_is_available(p))
  3113. continue;
  3114. cpufreq_lock(flags);
  3115. if (!p->pwr_thro_mode)
  3116. p->idx_opp_tbl_for_pwr_thro = p->idx_opp_tbl;
  3117. switch (level) {
  3118. case LOW_BATTERY_LEVEL_1:
  3119. case LOW_BATTERY_LEVEL_2:
  3120. /* 1st and 2nd LV trigger CPU Limit to under 819M */
  3121. p->pwr_thro_mode |= PWR_THRO_MODE_LBAT_819MHZ;
  3122. break;
  3123. default:
  3124. /* Unlimit CPU */
  3125. p->pwr_thro_mode &= ~PWR_THRO_MODE_LBAT_819MHZ;
  3126. break;
  3127. }
  3128. _mt_cpufreq_calc_power_throttle_idx(p);
  3129. if (!p->pwr_thro_mode)
  3130. p->idx_opp_tbl = p->idx_opp_tbl_for_pwr_thro;
  3131. cpufreq_unlock(flags);
  3132. #ifdef CONFIG_ARCH_MT6753
  3133. switch (level) {
  3134. case LOW_BATTERY_LEVEL_1:
  3135. case LOW_BATTERY_LEVEL_2:
  3136. /* Limit CPU core num to 4 */
  3137. hps_set_cpu_num_limit(LIMIT_LOW_BATTERY, 4, 0);
  3138. break;
  3139. default:
  3140. /* Unlimit CPU core num if no other limit */
  3141. if (!p->pwr_thro_mode)
  3142. hps_set_cpu_num_limit(LIMIT_LOW_BATTERY, num_possible_cpus(), 0);
  3143. break;
  3144. }
  3145. #endif
  3146. _mt_cpufreq_set(MT_CPU_DVFS_LITTLE, -1);
  3147. }
  3148. }
  3149. #endif
  3150. /*
  3151. * cpufreq driver
  3152. */
  3153. #ifdef CONFIG_CPU_FREQ
  3154. static int _mt_cpufreq_verify(struct cpufreq_policy *policy)
  3155. {
  3156. struct mt_cpu_dvfs *p;
  3157. int ret = 0; /* cpufreq_frequency_table_verify() always return 0 */
  3158. FUNC_ENTER(FUNC_LV_MODULE);
  3159. p = id_to_cpu_dvfs(_get_cpu_dvfs_id(policy->cpu));
  3160. BUG_ON(NULL == p);
  3161. #ifdef CONFIG_CPU_FREQ
  3162. ret = cpufreq_frequency_table_verify(policy, p->freq_tbl_for_cpufreq);
  3163. #endif
  3164. FUNC_EXIT(FUNC_LV_MODULE);
  3165. return ret;
  3166. }
  3167. static int _mt_cpufreq_target(struct cpufreq_policy *policy, unsigned int target_freq,
  3168. unsigned int relation)
  3169. {
  3170. unsigned int new_opp_idx;
  3171. enum mt_cpu_dvfs_id id = _get_cpu_dvfs_id(policy->cpu);
  3172. int ret = 0; /* -EINVAL; */
  3173. FUNC_ENTER(FUNC_LV_MODULE);
  3174. if (policy->cpu >= num_possible_cpus()
  3175. || cpufreq_frequency_table_target(policy, id_to_cpu_dvfs(id)->freq_tbl_for_cpufreq,
  3176. target_freq, relation, &new_opp_idx)
  3177. || (id_to_cpu_dvfs(id) && id_to_cpu_dvfs(id)->dvfs_disable_by_procfs)
  3178. )
  3179. return -EINVAL;
  3180. #ifdef CONFIG_CPU_DVFS_AEE_RR_REC
  3181. aee_rr_rec_cpu_dvfs_status(aee_rr_curr_cpu_dvfs_status() |
  3182. (1 << CPU_DVFS_LITTLE_IS_DOING_DVFS));
  3183. #endif
  3184. _mt_cpufreq_set(id, new_opp_idx);
  3185. #ifdef CONFIG_CPU_DVFS_AEE_RR_REC
  3186. aee_rr_rec_cpu_dvfs_status(
  3187. aee_rr_curr_cpu_dvfs_status() & ~(1 << CPU_DVFS_LITTLE_IS_DOING_DVFS));
  3188. #endif
  3189. FUNC_EXIT(FUNC_LV_MODULE);
  3190. return ret;
  3191. }
  3192. static int _mt_cpufreq_init(struct cpufreq_policy *policy)
  3193. {
  3194. int ret = -EINVAL;
  3195. FUNC_ENTER(FUNC_LV_MODULE);
  3196. if (policy->cpu >= num_possible_cpus())
  3197. return -EINVAL;
  3198. cpufreq_info("@%s: num_possible_cpus: %d\n", __func__, num_possible_cpus());
  3199. policy->shared_type = CPUFREQ_SHARED_TYPE_ANY;
  3200. cpumask_setall(policy->cpus);
  3201. /*******************************************************
  3202. * 1 us, assumed, will be overwrited by min_sampling_rate
  3203. ********************************************************/
  3204. policy->cpuinfo.transition_latency = 1000;
  3205. /*********************************************
  3206. * set default policy and cpuinfo, unit : Khz
  3207. **********************************************/
  3208. {
  3209. #define DORMANT_MODE_VOLT 85000
  3210. enum mt_cpu_dvfs_id id = _get_cpu_dvfs_id(policy->cpu);
  3211. struct mt_cpu_dvfs *p = id_to_cpu_dvfs(id);
  3212. unsigned int lv = _mt_cpufreq_get_cpu_level();
  3213. struct opp_tbl_info *opp_tbl_info = &opp_tbls[lv];
  3214. BUG_ON(NULL == p);
  3215. BUG_ON(!(lv == CPU_LEVEL_0 || lv == CPU_LEVEL_1 || lv == CPU_LEVEL_2
  3216. || lv == CPU_LEVEL_3 || lv == CPU_LEVEL_4));
  3217. p->cpu_level = lv;
  3218. #ifdef CONFIG_ARCH_MT6753
  3219. /* check 5A throttle */
  3220. if (p->cpu_level == CPU_LEVEL_1 && !cpu_dvfs_is_extbuck_valid()) {
  3221. pmic_5A_throttle_enable = true;
  3222. cpufreq_info("@%s: PMIC 5A throttle enabled!\n", __func__);
  3223. }
  3224. #endif
  3225. #ifndef CONFIG_CPU_DVFS_HAS_EXTBUCK
  3226. /* make sure Vproc & Vsram in normal mode path */
  3227. pmic_set_register_value(PMIC_VPROC_VOSEL_CTRL, 0x1);
  3228. pmic_set_register_value(PMIC_VSRAM_VOSEL_CTRL, 0x1);
  3229. /* set dpidle volt for Vproc & Vsram */
  3230. pmic_set_register_value(PMIC_VPROC_VOSEL, VOLT_TO_PMIC_VAL(DORMANT_MODE_VOLT));
  3231. pmic_set_register_value(PMIC_VSRAM_VOSEL_RSV, VOLT_TO_PMIC_VAL(DORMANT_MODE_VOLT));
  3232. #else
  3233. if (cpu_dvfs_is_extbuck_valid()) {
  3234. p->ops = &dvfs_ops_extbuck;
  3235. cpufreq_info("@%s: Has ExtBuck!\n", __func__);
  3236. /* set dpidle volt for Vproc */
  3237. mt6311_set_vdvfs11_vosel_ctrl(0x1);
  3238. mt6311_set_vdvfs11_vosel(VOLT_TO_EXTBUCK_VAL(DORMANT_MODE_VOLT));
  3239. cpufreq_info("@%s: cur_vsram = %dmV, cur_vproc = %dmV\n",
  3240. __func__, _mt_cpufreq_get_cur_vsram(p) / 100,
  3241. (p->ops->get_cur_volt(p)) / 100);
  3242. } else {
  3243. cpufreq_info("@%s: No ExtBuck!\n", __func__);
  3244. /* make sure Vproc & Vsram in normal mode path */
  3245. pmic_set_register_value(PMIC_VPROC_VOSEL_CTRL, 0x1);
  3246. pmic_set_register_value(PMIC_VSRAM_VOSEL_CTRL, 0x1);
  3247. /* set dpidle volt for Vproc & Vsram */
  3248. pmic_set_register_value(
  3249. PMIC_VPROC_VOSEL, VOLT_TO_PMIC_VAL(DORMANT_MODE_VOLT));
  3250. pmic_set_register_value(
  3251. PMIC_VSRAM_VOSEL_RSV, VOLT_TO_PMIC_VAL(DORMANT_MODE_VOLT));
  3252. }
  3253. #endif
  3254. ret = _mt_cpufreq_setup_freqs_table(
  3255. policy, opp_tbl_info->opp_tbl, opp_tbl_info->size);
  3256. policy->cpuinfo.max_freq = cpu_dvfs_get_max_freq(id_to_cpu_dvfs(id));
  3257. policy->cpuinfo.min_freq = cpu_dvfs_get_min_freq(id_to_cpu_dvfs(id));
  3258. policy->cur = _mt_cpufreq_get_cur_phy_freq(p); /* use cur phy freq is better */
  3259. policy->max = cpu_dvfs_get_max_freq(id_to_cpu_dvfs(id));
  3260. policy->min = cpu_dvfs_get_min_freq(id_to_cpu_dvfs(id));
  3261. if (_mt_cpufreq_sync_opp_tbl_idx(p) >= 0)
  3262. p->idx_normal_max_opp = p->idx_opp_tbl;
  3263. /* restore default volt, sync opp idx, set default limit */
  3264. _mt_cpufreq_restore_default_volt(p);
  3265. /* reset thermal/PBM limit */
  3266. #ifndef DISABLE_PBM_FEATURE
  3267. p->limited_power_by_pbm = 0;
  3268. #endif
  3269. p->limited_power_by_thermal = 0;
  3270. #ifdef CONFIG_ARCH_MT6753
  3271. if (p->cpu_level == CPU_LEVEL_1) {
  3272. /* give a large budget to find the first safe limit combination */
  3273. _mt_cpufreq_set_limit_by_pwr_budget(99999);
  3274. } else {
  3275. p->limited_max_freq = cpu_dvfs_get_max_freq(p);
  3276. p->limited_max_ncpu = num_possible_cpus();
  3277. p->limited_power_idx = 0;
  3278. }
  3279. #else
  3280. p->limited_max_freq = cpu_dvfs_get_max_freq(p);
  3281. p->limited_max_ncpu = num_possible_cpus();
  3282. p->limited_power_idx = 0;
  3283. #endif
  3284. cpufreq_info("@%s: limited_power_idx = %d\n", __func__, p->limited_power_idx);
  3285. #ifdef CONFIG_CPU_DVFS_SYSTEM_BOOTUP_BOOST
  3286. p->limited_min_freq_by_kdriver = cpu_dvfs_get_max_freq(p);
  3287. #endif
  3288. #ifdef CONFIG_CPU_DVFS_POWER_THROTTLING
  3289. register_battery_percent_notify(
  3290. &_mt_cpufreq_power_throttle_bat_per_CB, BATTERY_PERCENT_PRIO_CPU_L);
  3291. register_battery_oc_notify(
  3292. &_mt_cpufreq_power_throttle_bat_oc_CB, BATTERY_OC_PRIO_CPU_L);
  3293. register_low_battery_notify(
  3294. &_mt_cpufreq_power_throttle_low_bat_CB, LOW_BATTERY_PRIO_CPU_L);
  3295. #endif
  3296. }
  3297. #ifdef CONFIG_ARCH_MT6753
  3298. /* save VSRAM_VOSEL_OFFSET and DELTA init value to deepidle CMD8 */
  3299. mt_cpufreq_set_pmic_cmd(PMIC_WRAP_PHASE_DEEPIDLE,
  3300. IDX_DI_VSRAM_NORMAL,
  3301. (pmic_get_register_value(PMIC_VSRAM_VOSEL_OFFSET) << 8) |
  3302. pmic_get_register_value(PMIC_VSRAM_VOSEL_DELTA)
  3303. );
  3304. #endif
  3305. if (ret)
  3306. cpufreq_err("failed to setup frequency table\n");
  3307. else
  3308. cpufreq_info("CPU DVFS init done!\n");
  3309. FUNC_EXIT(FUNC_LV_MODULE);
  3310. return ret;
  3311. }
  3312. static unsigned int _mt_cpufreq_get(unsigned int cpu)
  3313. {
  3314. struct mt_cpu_dvfs *p;
  3315. FUNC_ENTER(FUNC_LV_MODULE);
  3316. p = id_to_cpu_dvfs(_get_cpu_dvfs_id(cpu));
  3317. BUG_ON(NULL == p);
  3318. FUNC_EXIT(FUNC_LV_MODULE);
  3319. return cpu_dvfs_get_cur_freq(p);
  3320. }
  3321. #endif
  3322. /*
  3323. * Early suspend
  3324. */
  3325. bool mt_cpufreq_earlysuspend_status_get(void)
  3326. {
  3327. return _allow_dpidle_ctrl_vproc;
  3328. }
  3329. EXPORT_SYMBOL(mt_cpufreq_earlysuspend_status_get);
  3330. static void _mt_cpufreq_lcm_status_switch(int onoff)
  3331. {
  3332. struct mt_cpu_dvfs *p;
  3333. int i;
  3334. cpufreq_info("@%s: LCM is %s\n", __func__, (onoff) ? "on" : "off");
  3335. /* onoff = 0: LCM OFF */
  3336. /* others: LCM ON */
  3337. if (onoff) {
  3338. _allow_dpidle_ctrl_vproc = false;
  3339. for_each_cpu_dvfs(i, p) {
  3340. if (!cpu_dvfs_is_available(p))
  3341. continue;
  3342. p->dvfs_disable_by_early_suspend = false;
  3343. if (is_fix_freq_in_ES) {
  3344. #ifdef CONFIG_CPU_FREQ
  3345. struct cpufreq_policy *policy = cpufreq_cpu_get(p->cpu_id);
  3346. if (policy) {
  3347. cpufreq_driver_target(
  3348. policy,
  3349. cpu_dvfs_get_freq_by_idx(
  3350. p,
  3351. p->idx_opp_tbl_for_late_resume
  3352. ),
  3353. CPUFREQ_RELATION_L
  3354. );
  3355. cpufreq_cpu_put(policy);
  3356. }
  3357. #endif
  3358. }
  3359. }
  3360. } else {
  3361. for_each_cpu_dvfs(i, p) {
  3362. if (!cpu_dvfs_is_available(p))
  3363. continue;
  3364. p->dvfs_disable_by_early_suspend = true;
  3365. p->idx_opp_tbl_for_late_resume = p->idx_opp_tbl;
  3366. if (is_fix_freq_in_ES) {
  3367. #ifdef CONFIG_CPU_FREQ
  3368. struct cpufreq_policy *policy = cpufreq_cpu_get(p->cpu_id);
  3369. if (policy) {
  3370. cpufreq_driver_target(
  3371. policy,
  3372. cpu_dvfs_get_normal_max_freq(p), CPUFREQ_RELATION_L);
  3373. cpufreq_cpu_put(policy);
  3374. }
  3375. #endif
  3376. }
  3377. }
  3378. _allow_dpidle_ctrl_vproc = true;
  3379. }
  3380. }
  3381. #ifdef CONFIG_HAS_EARLYSUSPEND
  3382. static void _mt_cpufreq_early_suspend(struct early_suspend *h)
  3383. {
  3384. FUNC_ENTER(FUNC_LV_MODULE);
  3385. _mt_cpufreq_lcm_status_switch(0);
  3386. FUNC_EXIT(FUNC_LV_MODULE);
  3387. }
  3388. static void _mt_cpufreq_late_resume(struct early_suspend *h)
  3389. {
  3390. FUNC_ENTER(FUNC_LV_MODULE);
  3391. _mt_cpufreq_lcm_status_switch(1);
  3392. FUNC_EXIT(FUNC_LV_MODULE);
  3393. }
  3394. #else
  3395. static int _mt_cpufreq_fb_notifier_callback(struct notifier_block *self, unsigned long event, void *data)
  3396. {
  3397. struct fb_event *evdata = data;
  3398. int blank;
  3399. FUNC_ENTER(FUNC_LV_MODULE);
  3400. blank = *(int *)evdata->data;
  3401. cpufreq_ver("@%s: blank = %d, event = %lu\n", __func__, blank, event);
  3402. /* skip if it's not a blank event */
  3403. if (event != FB_EVENT_BLANK)
  3404. return 0;
  3405. switch (blank) {
  3406. /* LCM ON */
  3407. case FB_BLANK_UNBLANK:
  3408. _mt_cpufreq_lcm_status_switch(1);
  3409. break;
  3410. /* LCM OFF */
  3411. case FB_BLANK_POWERDOWN:
  3412. _mt_cpufreq_lcm_status_switch(0);
  3413. break;
  3414. default:
  3415. break;
  3416. }
  3417. FUNC_EXIT(FUNC_LV_MODULE);
  3418. return 0;
  3419. }
  3420. #endif
  3421. static int _mt_cpufreq_suspend(struct device *dev)
  3422. {
  3423. /* struct cpufreq_policy *policy; */
  3424. struct mt_cpu_dvfs *p;
  3425. int i;
  3426. FUNC_ENTER(FUNC_LV_MODULE);
  3427. for_each_cpu_dvfs(i, p) {
  3428. if (!cpu_dvfs_is_available(p))
  3429. continue;
  3430. p->dvfs_disable_by_suspend = true;
  3431. /* XXX: useless, decided @ _mt_cpufreq_calc_new_opp_idx() */
  3432. _mt_cpufreq_set(MT_CPU_DVFS_LITTLE, p->idx_normal_max_opp);
  3433. }
  3434. FUNC_EXIT(FUNC_LV_MODULE);
  3435. return 0;
  3436. }
  3437. static int _mt_cpufreq_resume(struct device *dev)
  3438. {
  3439. struct mt_cpu_dvfs *p;
  3440. int i;
  3441. FUNC_ENTER(FUNC_LV_MODULE);
  3442. for_each_cpu_dvfs(i, p) {
  3443. if (!cpu_dvfs_is_available(p))
  3444. continue;
  3445. p->dvfs_disable_by_suspend = false;
  3446. }
  3447. FUNC_EXIT(FUNC_LV_MODULE);
  3448. return 0;
  3449. }
  3450. static int _mt_cpufreq_pm_restore_early(struct device *dev) /* for IPO-H HW(freq) / SW(opp_tbl_idx) */
  3451. {
  3452. struct mt_cpu_dvfs *p;
  3453. int i;
  3454. FUNC_ENTER(FUNC_LV_MODULE);
  3455. for_each_cpu_dvfs(i, p) {
  3456. if (cpu_dvfs_is_available(p))
  3457. _mt_cpufreq_sync_opp_tbl_idx(p);
  3458. }
  3459. FUNC_EXIT(FUNC_LV_MODULE);
  3460. return 0;
  3461. }
  3462. /*
  3463. * Platform driver
  3464. */
  3465. static int _mt_cpufreq_pdrv_probe(struct platform_device *pdev)
  3466. {
  3467. FUNC_ENTER(FUNC_LV_MODULE);
  3468. #ifdef CONFIG_CPU_DVFS_PERFORMANCE_TEST
  3469. {
  3470. struct mt_cpu_dvfs *p = id_to_cpu_dvfs(0);
  3471. /* fix at max freq(FY) and do not enable DVFS */
  3472. pmic_set_register_value(PMIC_VPROC_VOSEL_ON, VOLT_TO_PMIC_VAL(125000));
  3473. udelay(300);
  3474. #ifdef CONFIG_ARCH_MT6735M
  3475. _mt_cpufreq_dfs_by_set_armpll(p, _mt_cpufreq_cpu_dds_calc(CPU_DVFS_FREQ2), 8);
  3476. #else
  3477. _mt_cpufreq_dfs_by_set_armpll(p, _mt_cpufreq_cpu_dds_calc(CPU_DVFS_FREQ1), 8);
  3478. #endif
  3479. cpufreq_info("@%s: Disable DVFS and fix freq at %dKHz\n",
  3480. __func__, _mt_cpufreq_get_cur_phy_freq(p));
  3481. return 0;
  3482. }
  3483. #endif
  3484. if (pw.addr[0].cmd_addr == 0)
  3485. _mt_cpufreq_pmic_table_init();
  3486. /* init static power table */
  3487. mt_spower_init();
  3488. #ifdef CONFIG_CPU_DVFS_AEE_RR_REC
  3489. _mt_cpufreq_aee_init();
  3490. #endif
  3491. /* register early suspend */
  3492. #ifdef CONFIG_HAS_EARLYSUSPEND
  3493. register_early_suspend(&_mt_cpufreq_early_suspend_handler);
  3494. #else
  3495. if (fb_register_client(&_mt_cpufreq_fb_notifier)) {
  3496. cpufreq_err("@%s: register FB client failed!\n", __func__);
  3497. return 0;
  3498. }
  3499. #endif
  3500. /* init PMIC_WRAP & volt */
  3501. mt_cpufreq_set_pmic_phase(PMIC_WRAP_PHASE_NORMAL);
  3502. #ifdef CONFIG_CPU_FREQ
  3503. cpufreq_register_driver(&_mt_cpufreq_driver);
  3504. #endif
  3505. register_hotcpu_notifier(&_mt_cpufreq_cpu_notifier); /* <-XXX */
  3506. cpufreq_info("CPU DVFS driver probe done\n");
  3507. FUNC_EXIT(FUNC_LV_MODULE);
  3508. return 0;
  3509. }
  3510. static int _mt_cpufreq_pdrv_remove(struct platform_device *pdev)
  3511. {
  3512. FUNC_ENTER(FUNC_LV_MODULE);
  3513. unregister_hotcpu_notifier(&_mt_cpufreq_cpu_notifier); /* <-XXX */
  3514. #ifdef CONFIG_CPU_FREQ
  3515. cpufreq_unregister_driver(&_mt_cpufreq_driver);
  3516. #endif
  3517. FUNC_EXIT(FUNC_LV_MODULE);
  3518. return 0;
  3519. }
  3520. #ifndef __KERNEL__
  3521. /*
  3522. * For CTP
  3523. */
  3524. static unsigned int _mt_get_cpu_freq(void);
  3525. int mt_cpufreq_pdrv_probe(void)
  3526. {
  3527. static struct cpufreq_policy policy;
  3528. _mt_cpufreq_pdrv_probe(NULL);
  3529. policy.cpu = cpu_dvfs[MT_CPU_DVFS_LITTLE].cpu_id;
  3530. _mt_cpufreq_init(&policy);
  3531. #ifdef __MTK_SLT_
  3532. /* modify SLT DVFS table for HV/NV/LV */
  3533. #ifdef CONFIG_ARCH_MT6735M
  3534. #if defined(SLT_VMAX)
  3535. cpu_dvfs[MT_CPU_DVFS_LITTLE].opp_tbl[0].cpufreq_volt = 131250;
  3536. #elif defined(SLT_VMIN)
  3537. cpu_dvfs[MT_CPU_DVFS_LITTLE].opp_tbl[0].cpufreq_volt = 113750;
  3538. #else
  3539. cpu_dvfs[MT_CPU_DVFS_LITTLE].opp_tbl[0].cpufreq_volt = 125000;
  3540. #endif
  3541. #else /* !CONFIG_ARCH_MT6735M */
  3542. #if defined(SLT_VMAX)
  3543. cpu_dvfs[MT_CPU_DVFS_LITTLE].opp_tbl[0].cpufreq_volt = 126875;
  3544. #elif defined(SLT_VMIN)
  3545. cpu_dvfs[MT_CPU_DVFS_LITTLE].opp_tbl[0].cpufreq_volt = 109375;
  3546. #else
  3547. cpu_dvfs[MT_CPU_DVFS_LITTLE].opp_tbl[0].cpufreq_volt = 115000;
  3548. #endif
  3549. #endif
  3550. /* fix CPU at Max Freq */
  3551. mt_cpufreq_set_freq(MT_CPU_DVFS_LITTLE, 0);
  3552. cpufreq_info("set CPU freq to %d KHz\n", _mt_get_cpu_freq());
  3553. #endif
  3554. return 0;
  3555. }
  3556. int mt_cpufreq_set_opp_volt(enum mt_cpu_dvfs_id id, int idx)
  3557. {
  3558. int ret = 0;
  3559. static struct opp_tbl_info *info;
  3560. struct mt_cpu_dvfs *p = id_to_cpu_dvfs(id);
  3561. info = &opp_tbls[(p->cpu_level)];
  3562. if (idx >= info->size)
  3563. return -1;
  3564. return _mt_cpufreq_set_cur_volt_locked(p, info->opp_tbl[idx].cpufreq_volt);
  3565. }
  3566. int mt_cpufreq_set_freq(enum mt_cpu_dvfs_id id, int idx)
  3567. {
  3568. unsigned int cur_freq;
  3569. unsigned int target_freq;
  3570. int ret;
  3571. struct mt_cpu_dvfs *p = id_to_cpu_dvfs(id);
  3572. cur_freq = p->ops->get_cur_phy_freq(p);
  3573. target_freq = cpu_dvfs_get_freq_by_idx(p, idx);
  3574. ret = _mt_cpufreq_set_locked(p, cur_freq, target_freq, NULL);
  3575. if (ret < 0)
  3576. return ret;
  3577. p->idx_opp_tbl = idx;
  3578. return target_freq;
  3579. }
  3580. #include "dvfs.h"
  3581. static unsigned int _mt_get_cpu_freq(void)
  3582. {
  3583. int output = 0;
  3584. int i = 0;
  3585. unsigned int temp, clk26cali_0, clk_cfg_8, clk_misc_cfg_1;
  3586. clk26cali_0 = DRV_Reg32(CLK26CALI_0);
  3587. DRV_WriteReg32(CLK26CALI_0, clk26cali_0 | 0x80); /* enable fmeter_en */
  3588. clk_misc_cfg_1 = DRV_Reg32(CLK_MISC_CFG_1);
  3589. DRV_WriteReg32(CLK_MISC_CFG_1, 0xFFFF0300); /* select divider */
  3590. clk_cfg_8 = DRV_Reg32(CLK_CFG_8);
  3591. DRV_WriteReg32(CLK_CFG_8, (39 << 8)); /* select abist_cksw */
  3592. temp = DRV_Reg32(CLK26CALI_0);
  3593. DRV_WriteReg32(CLK26CALI_0, temp | 0x1); /* start fmeter */
  3594. /* wait frequency meter finish */
  3595. while (DRV_Reg32(CLK26CALI_0) & 0x1) {
  3596. mdelay(10);
  3597. i++;
  3598. if (i > 10)
  3599. break;
  3600. }
  3601. temp = DRV_Reg32(CLK26CALI_1) & 0xFFFF;
  3602. output = ((temp * 26000) / 1024) * 4; /* Khz */
  3603. DRV_WriteReg32(CLK_CFG_8, clk_cfg_8);
  3604. DRV_WriteReg32(CLK_MISC_CFG_1, clk_misc_cfg_1);
  3605. DRV_WriteReg32(CLK26CALI_0, clk26cali_0);
  3606. if (i > 10)
  3607. return 0;
  3608. else
  3609. return output;
  3610. }
  3611. unsigned int dvfs_get_cpu_freq(enum mt_cpu_dvfs_id id)
  3612. {
  3613. #ifdef __MTK_SLT_
  3614. struct mt_cpu_dvfs *p = id_to_cpu_dvfs(id);
  3615. return _mt_cpufreq_get_cur_phy_freq(p);
  3616. #else
  3617. return _mt_get_cpu_freq();
  3618. #endif
  3619. }
  3620. void dvfs_set_cpu_freq_FH(enum mt_cpu_dvfs_id id, int freq)
  3621. {
  3622. struct mt_cpu_dvfs *p = id_to_cpu_dvfs(id);
  3623. int idx;
  3624. if (!p) {
  3625. cpufreq_err("%s(%d, %d), id is wrong\n", __func__, id, freq);
  3626. return;
  3627. }
  3628. idx = _mt_cpufreq_get_idx_by_freq(p, freq, CPUFREQ_RELATION_H);
  3629. if (-1 == idx) {
  3630. cpufreq_err("%s(%d, %d), freq is wrong\n", __func__, id, freq);
  3631. return;
  3632. }
  3633. mt_cpufreq_set_freq(id, idx);
  3634. }
  3635. unsigned int dvfs_get_cur_oppidx(enum mt_cpu_dvfs_id id)
  3636. {
  3637. struct mt_cpu_dvfs *p = id_to_cpu_dvfs(id);
  3638. return p->idx_opp_tbl;
  3639. }
  3640. unsigned int cpu_frequency_output_slt(enum mt_cpu_dvfs_id id)
  3641. {
  3642. return (MT_CPU_DVFS_LITTLE == id) ? _mt_get_cpu_freq() : 0;
  3643. }
  3644. unsigned int mt_get_cur_volt_lte(void) /* volt: mv * 100 */
  3645. {
  3646. unsigned int volt = 0;
  3647. volt = pmic_get_register_value(PMIC_VLTE_VOSEL_ON);
  3648. volt = PMIC_VAL_TO_VOLT(volt);
  3649. return volt;
  3650. }
  3651. unsigned int dvfs_get_cpu_volt(enum mt_cpu_dvfs_id id) /* volt: mv * 100 */
  3652. {
  3653. struct mt_cpu_dvfs *p = id_to_cpu_dvfs(id);
  3654. unsigned int volt = 0;
  3655. volt = p->ops->get_cur_volt(p);
  3656. cpufreq_dbg("%s(%d) volt = %d\n", __func__, id, volt);
  3657. return volt;
  3658. }
  3659. void dvfs_set_cpu_volt(enum mt_cpu_dvfs_id id, int volt) /* volt: mv * 100 */
  3660. {
  3661. struct mt_cpu_dvfs *p = id_to_cpu_dvfs(id);
  3662. cpufreq_dbg("%s(%d, %d)\n", __func__, id, volt);
  3663. if (!p) {
  3664. cpufreq_err("%s(%d, %d), id is wrong\n", __func__, id, volt);
  3665. return;
  3666. }
  3667. if (_mt_cpufreq_set_cur_volt_locked(p, volt))
  3668. cpufreq_err("%s(%d, %d), set volt fail\n", __func__, id, volt);
  3669. cpufreq_dbg("%s(%d, %d) Vproc = %d\n", __func__, id, volt, p->ops->get_cur_volt(p));
  3670. #ifdef CONFIG_CPU_DVFS_HAS_EXTBUCK
  3671. cpufreq_dbg("%s, Vsram = %d\n", __func__, _mt_cpufreq_get_cur_vsram(p));
  3672. #endif
  3673. }
  3674. #if 0
  3675. void dvfs_set_gpu_volt(int pmic_val)
  3676. {
  3677. cpufreq_dbg("%s(%d)\n", __func__, pmic_val);
  3678. mt_cpufreq_set_pmic_cmd(PMIC_WRAP_PHASE_NORMAL, IDX_NM_VGPU, pmic_val);
  3679. mt_cpufreq_apply_pmic_cmd(IDX_NM_VGPU);
  3680. }
  3681. #endif
  3682. /* NOTE: This is ONLY for PTPOD SLT. Should not adjust VCORE in other cases. */
  3683. void dvfs_set_vcore(int pmic_val)
  3684. {
  3685. cpufreq_dbg("%s(%d)\n", __func__, pmic_val);
  3686. mt_cpufreq_set_pmic_cmd(PMIC_WRAP_PHASE_NORMAL, IDX_NM_VCORE_HPM, pmic_val);
  3687. mt_cpufreq_apply_pmic_cmd(IDX_NM_VCORE_HPM);
  3688. }
  3689. void dvfs_set_vlte(int pmic_val)
  3690. {
  3691. pmic_set_register_value(PMIC_VLTE_VOSEL_ON, pmic_val);
  3692. }
  3693. #if 0
  3694. void dvfs_set_vcore_pdn_volt(int pmic_val)
  3695. {
  3696. cpufreq_dbg("%s(%d)\n", __func__, pmic_val);
  3697. mt_cpufreq_set_pmic_phase(PMIC_WRAP_PHASE_DEEPIDLE);
  3698. mt_cpufreq_set_pmic_cmd(PMIC_WRAP_PHASE_DEEPIDLE, IDX_DI_VCORE_PDN_NORMAL, pmic_val);
  3699. mt_cpufreq_apply_pmic_cmd(IDX_DI_VCORE_PDN_NORMAL);
  3700. mt_cpufreq_set_pmic_phase(PMIC_WRAP_PHASE_NORMAL);
  3701. }
  3702. #endif
  3703. static unsigned int vcpu_backup;
  3704. static unsigned int vcore_backup;
  3705. static unsigned int vlte_backup;
  3706. enum {
  3707. PTP_CTRL_CPU = 0,
  3708. PTP_CTRL_SOC = 1,
  3709. PTP_CTRL_LTE = 2,
  3710. PTP_CTRL_ALL = 3,
  3711. NR_PTP_CTRL,
  3712. };
  3713. void dvfs_disable_by_ptpod(int id)
  3714. {
  3715. struct mt_cpu_dvfs *p = id_to_cpu_dvfs(MT_CPU_DVFS_LITTLE);
  3716. cpufreq_dbg("%s()\n", __func__); /* <-XXX */
  3717. switch (id) {
  3718. case PTP_CTRL_CPU:
  3719. vcpu_backup = dvfs_get_cpu_volt(MT_CPU_DVFS_LITTLE);
  3720. #ifdef __MTK_SLT_
  3721. /* force Vboot = 1.25V for SLT */
  3722. dvfs_set_cpu_volt(MT_CPU_DVFS_LITTLE, 125000);
  3723. #else
  3724. mt_cpufreq_disable_by_ptpod(MT_CPU_DVFS_LITTLE); /* VBOOT = 1.25 */
  3725. #endif
  3726. break;
  3727. case PTP_CTRL_SOC:
  3728. vcore_backup = pmic_get_register_value(PMIC_VCORE1_VOSEL_ON);
  3729. dvfs_set_vcore(VOLT_TO_PMIC_VAL(115000)); /* VBOOT = 1.15 */
  3730. break;
  3731. case PTP_CTRL_LTE:
  3732. vlte_backup = pmic_get_register_value(PMIC_VLTE_VOSEL_ON);
  3733. pmic_set_register_value(PMIC_VLTE_VOSEL_ON, VOLT_TO_PMIC_VAL(105000)); /* VBOOT = 1.05 */
  3734. break;
  3735. case PTP_CTRL_ALL:
  3736. vcpu_backup = dvfs_get_cpu_volt(MT_CPU_DVFS_LITTLE);
  3737. vcore_backup = pmic_get_register_value(PMIC_VCORE1_VOSEL_ON);
  3738. vlte_backup = pmic_get_register_value(PMIC_VLTE_VOSEL_ON);
  3739. #ifdef __MTK_SLT_
  3740. /* force Vboot = 1.25V for SLT */
  3741. dvfs_set_cpu_volt(MT_CPU_DVFS_LITTLE, 125000);
  3742. #else
  3743. mt_cpufreq_disable_by_ptpod(MT_CPU_DVFS_LITTLE);
  3744. #endif
  3745. /* dvfs_set_vcore(VOLT_TO_PMIC_VAL(115000)); //VBOOT = 1.15 */
  3746. /* pmic_set_register_value(PMIC_VLTE_VOSEL_ON, VOLT_TO_PMIC_VAL(105000));//VBOOT = 1.05 */
  3747. break;
  3748. default:
  3749. break;
  3750. }
  3751. }
  3752. void dvfs_enable_by_ptpod(int id)
  3753. {
  3754. #ifndef __MTK_SLT_
  3755. struct mt_cpu_dvfs *p = id_to_cpu_dvfs(MT_CPU_DVFS_LITTLE);
  3756. cpufreq_dbg("%s()\n", __func__); /* <-XXX */
  3757. switch (id) {
  3758. case PTP_CTRL_CPU:
  3759. dvfs_set_cpu_volt(MT_CPU_DVFS_LITTLE, vcpu_backup);
  3760. break;
  3761. case PTP_CTRL_SOC:
  3762. dvfs_set_vcore(vcore_backup);
  3763. break;
  3764. case PTP_CTRL_LTE:
  3765. pmic_set_register_value(PMIC_VLTE_VOSEL_ON, vlte_backup);
  3766. break;
  3767. case PTP_CTRL_ALL:
  3768. dvfs_set_cpu_volt(MT_CPU_DVFS_LITTLE, vcpu_backup);
  3769. dvfs_set_vcore(vcore_backup);
  3770. pmic_set_register_value(PMIC_VLTE_VOSEL_ON, vlte_backup);
  3771. break;
  3772. default:
  3773. break;
  3774. }
  3775. #endif
  3776. }
  3777. #endif /* ! __KERNEL__ */
  3778. #ifdef CONFIG_PROC_FS
  3779. /*
  3780. * PROC
  3781. */
  3782. static char *_copy_from_user_for_proc(const char __user *buffer, size_t count)
  3783. {
  3784. char *buf = (char *)__get_free_page(GFP_USER);
  3785. if (!buf)
  3786. return NULL;
  3787. if (count >= PAGE_SIZE)
  3788. goto out;
  3789. if (copy_from_user(buf, buffer, count))
  3790. goto out;
  3791. buf[count] = '\0';
  3792. return buf;
  3793. out:
  3794. free_page((unsigned long)buf);
  3795. return NULL;
  3796. }
  3797. /* cpufreq_debug */
  3798. static int cpufreq_debug_proc_show(struct seq_file *m, void *v)
  3799. {
  3800. seq_printf(m, "cpufreq debug (log level) = %d\n", func_lv_mask);
  3801. return 0;
  3802. }
  3803. static ssize_t cpufreq_debug_proc_write(struct file *file, const char __user *buffer, size_t count,
  3804. loff_t *pos)
  3805. {
  3806. unsigned int dbg_lv;
  3807. char *buf = _copy_from_user_for_proc(buffer, count);
  3808. if (!buf)
  3809. return -EINVAL;
  3810. if (!kstrtouint(buf, 10, &dbg_lv))
  3811. func_lv_mask = dbg_lv;
  3812. else
  3813. cpufreq_err("echo dbg_lv (dec) > /proc/cpufreq/cpufreq_debug\n");
  3814. free_page((unsigned long)buf);
  3815. return count;
  3816. }
  3817. /* cpufreq_fftt_test */
  3818. #include <linux/sched_clock.h>
  3819. static unsigned long long _delay_us;
  3820. static unsigned long long _delay_us_buf;
  3821. static int cpufreq_fftt_test_proc_show(struct seq_file *m, void *v)
  3822. {
  3823. seq_printf(m, "%llu\n", _delay_us);
  3824. if (_delay_us < _delay_us_buf)
  3825. cpufreq_err("@%s(), %llu < %llu, loops_per_jiffy = %lu\n",
  3826. __func__, _delay_us, _delay_us_buf, loops_per_jiffy);
  3827. return 0;
  3828. }
  3829. static ssize_t cpufreq_fftt_test_proc_write(struct file *file, const char __user *buffer,
  3830. size_t count, loff_t *pos)
  3831. {
  3832. char *buf = _copy_from_user_for_proc(buffer, count);
  3833. if (!buf)
  3834. return -EINVAL;
  3835. if (!kstrtoull(buf, 10, &_delay_us_buf)) {
  3836. unsigned long start;
  3837. start = (unsigned long)sched_clock();
  3838. udelay(_delay_us_buf);
  3839. _delay_us = ((unsigned long)sched_clock() - start) / 1000;
  3840. cpufreq_ver("@%s(%llu), _delay_us = %llu, loops_per_jiffy = %lu\n",
  3841. __func__, _delay_us_buf, _delay_us, loops_per_jiffy);
  3842. }
  3843. free_page((unsigned long)buf);
  3844. return count;
  3845. }
  3846. static int cpufreq_stress_test_proc_show(struct seq_file *m, void *v)
  3847. {
  3848. seq_printf(m, "%d\n", do_dvfs_stress_test);
  3849. return 0;
  3850. }
  3851. static ssize_t cpufreq_stress_test_proc_write(struct file *file, const char __user *buffer,
  3852. size_t count, loff_t *pos)
  3853. {
  3854. unsigned int do_stress;
  3855. char *buf = _copy_from_user_for_proc(buffer, count);
  3856. if (!buf)
  3857. return -EINVAL;
  3858. if (!kstrtouint(buf, 10, &do_stress))
  3859. do_dvfs_stress_test = do_stress;
  3860. else
  3861. cpufreq_err("echo 0/1 > /proc/cpufreq/cpufreq_stress_test\n");
  3862. free_page((unsigned long)buf);
  3863. return count;
  3864. }
  3865. static int cpufreq_fix_freq_in_es_proc_show(struct seq_file *m, void *v)
  3866. {
  3867. seq_printf(m, "%d\n", is_fix_freq_in_ES);
  3868. return 0;
  3869. }
  3870. static ssize_t cpufreq_fix_freq_in_es_proc_write(struct file *file, const char __user *buffer,
  3871. size_t count, loff_t *pos)
  3872. {
  3873. unsigned int fix_freq_in_ES;
  3874. char *buf = _copy_from_user_for_proc(buffer, count);
  3875. if (!buf)
  3876. return -EINVAL;
  3877. if (!kstrtouint(buf, 10, &fix_freq_in_ES))
  3878. is_fix_freq_in_ES = fix_freq_in_ES;
  3879. else
  3880. cpufreq_err("echo 0/1 > /proc/cpufreq/cpufreq_fix_freq_in_es\n");
  3881. free_page((unsigned long)buf);
  3882. return count;
  3883. }
  3884. /* cpufreq_limited_by_hevc */
  3885. static int cpufreq_limited_by_hevc_proc_show(struct seq_file *m, void *v)
  3886. {
  3887. struct mt_cpu_dvfs *p = (struct mt_cpu_dvfs *)m->private;
  3888. seq_printf(m, "%d\n", p->limited_freq_by_hevc);
  3889. return 0;
  3890. }
  3891. static ssize_t cpufreq_limited_by_hevc_proc_write(struct file *file, const char __user *buffer,
  3892. size_t count, loff_t *pos)
  3893. {
  3894. struct mt_cpu_dvfs *p = (struct mt_cpu_dvfs *)PDE_DATA(file_inode(file));
  3895. int limited_freq_by_hevc;
  3896. char *buf = _copy_from_user_for_proc(buffer, count);
  3897. if (!buf)
  3898. return -EINVAL;
  3899. if (!kstrtoint(buf, 10, &limited_freq_by_hevc)) {
  3900. p->limited_freq_by_hevc = limited_freq_by_hevc;
  3901. if (cpu_dvfs_is_available(p)
  3902. && (p->limited_freq_by_hevc > cpu_dvfs_get_cur_freq(p))) {
  3903. #ifdef CONFIG_CPU_FREQ
  3904. struct cpufreq_policy *policy = cpufreq_cpu_get(p->cpu_id);
  3905. if (policy) {
  3906. cpufreq_driver_target(
  3907. policy, p->limited_freq_by_hevc, CPUFREQ_RELATION_L);
  3908. cpufreq_cpu_put(policy);
  3909. }
  3910. #endif
  3911. }
  3912. } else
  3913. cpufreq_err("echo limited_freq_by_hevc (dec) > /proc/cpufreq/cpufreq_limited_by_hevc\n");
  3914. free_page((unsigned long)buf);
  3915. return count;
  3916. }
  3917. /* cpufreq_limited_by_pbm */
  3918. #ifndef DISABLE_PBM_FEATURE
  3919. static int cpufreq_limited_by_pbm_proc_show(struct seq_file *m, void *v)
  3920. {
  3921. struct mt_cpu_dvfs *p;
  3922. int i;
  3923. for_each_cpu_dvfs(i, p) {
  3924. seq_printf(m, "[%s/%d]\n"
  3925. "limited_power_idx = %d\n"
  3926. "limited_max_freq = %d\n"
  3927. "limited_max_ncpu = %d\n"
  3928. "limited_power_by_thermal = %d\n"
  3929. "limited_power_by_pbm = %d\n",
  3930. p->name, i,
  3931. p->limited_power_idx,
  3932. p->limited_max_freq,
  3933. p->limited_max_ncpu,
  3934. p->limited_power_by_thermal, p->limited_power_by_pbm);
  3935. }
  3936. return 0;
  3937. }
  3938. static ssize_t cpufreq_limited_by_pbm_proc_write(struct file *file, const char __user *buffer,
  3939. size_t count, loff_t *pos)
  3940. {
  3941. int limited_power;
  3942. char *buf = _copy_from_user_for_proc(buffer, count);
  3943. if (!buf)
  3944. return -EINVAL;
  3945. if (!kstrtoint(buf, 10, &limited_power))
  3946. mt_cpufreq_set_power_limit_by_pbm(limited_power); /* TODO: specify limited_power by id??? */
  3947. else
  3948. cpufreq_err("echo limited_power (dec) > /proc/cpufreq/cpufreq_limited_by_pbm\n");
  3949. free_page((unsigned long)buf);
  3950. return count;
  3951. }
  3952. #endif
  3953. /* cpufreq_limited_by_thermal */
  3954. static int cpufreq_limited_by_thermal_proc_show(struct seq_file *m, void *v)
  3955. {
  3956. struct mt_cpu_dvfs *p;
  3957. int i;
  3958. for_each_cpu_dvfs(i, p) {
  3959. seq_printf(m, "[%s/%d]\n"
  3960. "limited_power_idx = %d\n"
  3961. "limited_max_freq = %d\n"
  3962. "limited_max_ncpu = %d\n"
  3963. "limited_power_by_thermal = %d\n",
  3964. p->name, i,
  3965. p->limited_power_idx,
  3966. p->limited_max_freq, p->limited_max_ncpu, p->limited_power_by_thermal);
  3967. }
  3968. return 0;
  3969. }
  3970. static ssize_t cpufreq_limited_by_thermal_proc_write(struct file *file, const char __user *buffer,
  3971. size_t count, loff_t *pos)
  3972. {
  3973. int limited_power;
  3974. char *buf = _copy_from_user_for_proc(buffer, count);
  3975. if (!buf)
  3976. return -EINVAL;
  3977. if (!kstrtoint(buf, 10, &limited_power))
  3978. mt_cpufreq_thermal_protect(limited_power); /* TODO: specify limited_power by id??? */
  3979. else
  3980. cpufreq_err("echo limited_power (dec) > /proc/cpufreq/cpufreq_limited_by_thermal\n");
  3981. free_page((unsigned long)buf);
  3982. return count;
  3983. }
  3984. /* PMIC 5A limit */
  3985. #ifdef CONFIG_ARCH_MT6753
  3986. static int cpufreq_5A_throttle_enable_proc_show(struct seq_file *m, void *v)
  3987. {
  3988. seq_printf(m, "cpufreq PMIC 5A throttle enable = %d\n", pmic_5A_throttle_enable);
  3989. seq_printf(m, "cpufreq PMIC 5A throttle on/off = %d\n", pmic_5A_throttle_on);
  3990. return 0;
  3991. }
  3992. static ssize_t cpufreq_5A_throttle_enable_proc_write(struct file *file, const char __user *buffer,
  3993. size_t count, loff_t *pos)
  3994. {
  3995. unsigned int enable;
  3996. char *buf = _copy_from_user_for_proc(buffer, count);
  3997. if (!buf)
  3998. return -EINVAL;
  3999. if (!kstrtouint(buf, 10, &enable)) {
  4000. pmic_5A_throttle_enable = enable;
  4001. _mt_cpufreq_set(MT_CPU_DVFS_LITTLE, -1);
  4002. } else
  4003. cpufreq_err("echo 1/0 > /proc/cpufreq/cpufreq_5A_throttle_enable\n");
  4004. free_page((unsigned long)buf);
  4005. return count;
  4006. }
  4007. #endif
  4008. /* cpufreq_limited_max_freq_by_user */
  4009. static int cpufreq_limited_max_freq_by_user_proc_show(struct seq_file *m, void *v)
  4010. {
  4011. struct mt_cpu_dvfs *p = (struct mt_cpu_dvfs *)m->private;
  4012. seq_printf(m, "%d\n", p->limited_max_freq_by_user);
  4013. return 0;
  4014. }
  4015. static ssize_t cpufreq_limited_max_freq_by_user_proc_write(struct file *file,
  4016. const char __user *buffer, size_t count,
  4017. loff_t *pos)
  4018. {
  4019. struct mt_cpu_dvfs *p = (struct mt_cpu_dvfs *)PDE_DATA(file_inode(file));
  4020. int limited_max_freq;
  4021. char *buf = _copy_from_user_for_proc(buffer, count);
  4022. if (!buf)
  4023. return -EINVAL;
  4024. if (!kstrtoint(buf, 10, &limited_max_freq)) {
  4025. p->limited_max_freq_by_user = limited_max_freq;
  4026. if (cpu_dvfs_is_available(p) && (p->limited_max_freq_by_user != 0)
  4027. && (p->limited_max_freq_by_user < cpu_dvfs_get_cur_freq(p))) {
  4028. struct cpufreq_policy *policy = cpufreq_cpu_get(p->cpu_id);
  4029. if (policy) {
  4030. cpufreq_driver_target(
  4031. policy, p->limited_max_freq_by_user, CPUFREQ_RELATION_H);
  4032. cpufreq_cpu_put(policy);
  4033. }
  4034. }
  4035. } else
  4036. cpufreq_err("echo limited_max_freq (dec) > /proc/cpufreq/%s/cpufreq_limited_max_freq_by_user\n",
  4037. p->name);
  4038. free_page((unsigned long)buf);
  4039. return count;
  4040. }
  4041. /* cpufreq_power_dump */
  4042. static int cpufreq_power_dump_proc_show(struct seq_file *m, void *v)
  4043. {
  4044. struct mt_cpu_dvfs *p;
  4045. int i, j;
  4046. for_each_cpu_dvfs(i, p) {
  4047. seq_printf(m, "[%s/%d]\n", p->name, i);
  4048. for (j = 0; j < p->nr_power_tbl; j++) {
  4049. seq_printf(m,
  4050. "[%d] = { .cpufreq_khz = %d,\t.cpufreq_ncpu = %d,\t.cpufreq_power = %d, },\n",
  4051. j, p->power_tbl[j].cpufreq_khz, p->power_tbl[j].cpufreq_ncpu,
  4052. p->power_tbl[j].cpufreq_power);
  4053. }
  4054. }
  4055. return 0;
  4056. }
  4057. /* cpufreq_ptpod_freq_volt */
  4058. static int cpufreq_ptpod_freq_volt_proc_show(struct seq_file *m, void *v)
  4059. {
  4060. struct mt_cpu_dvfs *p = (struct mt_cpu_dvfs *)m->private;
  4061. int j;
  4062. for (j = 0; j < p->nr_opp_tbl; j++) {
  4063. seq_printf(m,
  4064. "[%d] = { .cpufreq_khz = %d,\t.cpufreq_volt = %d,\t.cpufreq_volt_org = %d, },\n",
  4065. j, p->opp_tbl[j].cpufreq_khz, p->opp_tbl[j].cpufreq_volt,
  4066. p->opp_tbl[j].cpufreq_volt_org);
  4067. }
  4068. return 0;
  4069. }
  4070. /* cpufreq_state */
  4071. static int cpufreq_state_proc_show(struct seq_file *m, void *v)
  4072. {
  4073. struct mt_cpu_dvfs *p;
  4074. int i;
  4075. for_each_cpu_dvfs(i, p) {
  4076. seq_printf(m, "[%s/%d]\n"
  4077. "dvfs_disable_by_procfs = %d\n"
  4078. "limited_freq_by_hevc = %d KHz\n"
  4079. "limited_power_by_thermal = %d mW\n"
  4080. "dvfs_disable_by_early_suspend = %d\n" "dvfs_disable_by_suspend = %d\n"
  4081. #ifdef CONFIG_CPU_DVFS_POWER_THROTTLING
  4082. "pwr_thro_mode = %d\n"
  4083. #endif
  4084. "limited_max_freq_by_user = %d KHz\n"
  4085. "dvfs_disable_by_ptpod = %d\n",
  4086. p->name, i,
  4087. p->dvfs_disable_by_procfs,
  4088. p->limited_freq_by_hevc,
  4089. p->limited_power_by_thermal,
  4090. p->dvfs_disable_by_early_suspend, p->dvfs_disable_by_suspend,
  4091. #ifdef CONFIG_CPU_DVFS_POWER_THROTTLING
  4092. p->pwr_thro_mode,
  4093. #endif
  4094. p->limited_max_freq_by_user, p->dvfs_disable_by_ptpod);
  4095. }
  4096. return 0;
  4097. }
  4098. static ssize_t cpufreq_state_proc_write(struct file *file, const char __user *buffer, size_t count,
  4099. loff_t *pos)
  4100. {
  4101. struct mt_cpu_dvfs *p = (struct mt_cpu_dvfs *)PDE_DATA(file_inode(file));
  4102. char *buf = _copy_from_user_for_proc(buffer, count);
  4103. int enable;
  4104. if (!buf)
  4105. return -EINVAL;
  4106. if (!kstrtoint(buf, 10, &enable)) {
  4107. if (enable == 0)
  4108. p->dvfs_disable_by_procfs = true;
  4109. else
  4110. p->dvfs_disable_by_procfs = false;
  4111. } else
  4112. cpufreq_err("echo 1/0 > /proc/cpufreq/cpufreq_state\n");
  4113. free_page((unsigned long)buf);
  4114. return count;
  4115. }
  4116. /* cpufreq_oppidx */
  4117. static int cpufreq_oppidx_proc_show(struct seq_file *m, void *v) /* <-XXX */
  4118. {
  4119. struct mt_cpu_dvfs *p = (struct mt_cpu_dvfs *)m->private;
  4120. int j;
  4121. seq_printf(m, "[%s/%d]\n" "cpufreq_oppidx = %d\n", p->name, p->cpu_id, p->idx_opp_tbl);
  4122. for (j = 0; j < p->nr_opp_tbl; j++) {
  4123. seq_printf(m, "\tOP(%d, %d),\n",
  4124. cpu_dvfs_get_freq_by_idx(p, j), cpu_dvfs_get_volt_by_idx(p, j)
  4125. );
  4126. }
  4127. return 0;
  4128. }
  4129. static ssize_t cpufreq_oppidx_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos)
  4130. {
  4131. struct mt_cpu_dvfs *p = (struct mt_cpu_dvfs *)PDE_DATA(file_inode(file));
  4132. int oppidx;
  4133. char *buf = _copy_from_user_for_proc(buffer, count);
  4134. if (!buf)
  4135. return -EINVAL;
  4136. BUG_ON(NULL == p);
  4137. if (!kstrtoint(buf, 10, &oppidx) && 0 <= oppidx && oppidx < p->nr_opp_tbl) {
  4138. p->dvfs_disable_by_procfs = true;
  4139. _mt_cpufreq_set(MT_CPU_DVFS_LITTLE, oppidx);
  4140. } else {
  4141. p->dvfs_disable_by_procfs = false;
  4142. cpufreq_err("echo oppidx > /proc/cpufreq/cpufreq_oppidx (0 <= %d < %d)\n",
  4143. oppidx, p->nr_opp_tbl);
  4144. }
  4145. free_page((unsigned long)buf);
  4146. return count;
  4147. }
  4148. /* cpufreq_freq */
  4149. static int cpufreq_freq_proc_show(struct seq_file *m, void *v) /* <-XXX */
  4150. {
  4151. struct mt_cpu_dvfs *p = (struct mt_cpu_dvfs *)m->private;
  4152. seq_printf(m, "%d KHz\n", p->ops->get_cur_phy_freq(p));
  4153. return 0;
  4154. }
  4155. static ssize_t cpufreq_freq_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos)
  4156. {
  4157. unsigned long flags;
  4158. struct mt_cpu_dvfs *p = (struct mt_cpu_dvfs *)PDE_DATA(file_inode(file));
  4159. unsigned int cur_freq;
  4160. int freq, i, found;
  4161. char *buf = _copy_from_user_for_proc(buffer, count);
  4162. if (!buf)
  4163. return -EINVAL;
  4164. BUG_ON(NULL == p);
  4165. if (!kstrtoint(buf, 10, &freq)) {
  4166. if (freq < cpu_dvfs_get_min_freq(p)) {
  4167. if (freq != 0)
  4168. cpufreq_err("frequency should higher than %dKHz!\n",
  4169. cpu_dvfs_get_min_freq(p));
  4170. p->dvfs_disable_by_procfs = false;
  4171. goto end;
  4172. } else {
  4173. for (i = 0; i < p->nr_opp_tbl; i++) {
  4174. if (freq == p->opp_tbl[i].cpufreq_khz) {
  4175. found = 1;
  4176. break;
  4177. }
  4178. }
  4179. if (found == 1) {
  4180. p->dvfs_disable_by_procfs = true;
  4181. cpufreq_lock(flags);
  4182. cur_freq = p->ops->get_cur_phy_freq(p);
  4183. if (freq != cur_freq) {
  4184. #ifdef CONFIG_ARCH_MT6753
  4185. if (is_need_5A_throttle(p, freq, num_online_cpus() + num_online_cpus_delta)) {
  4186. cpufreq_warn("@%s: frequency %dKHz over 5A limit!\n", __func__, freq);
  4187. p->ops->set_cur_freq(p, cur_freq, PMIC_5A_THRO_MAX_CPU_FREQ);
  4188. } else
  4189. p->ops->set_cur_freq(p, cur_freq, freq);
  4190. #else
  4191. p->ops->set_cur_freq(p, cur_freq, freq);
  4192. #endif
  4193. #ifndef DISABLE_PBM_FEATURE
  4194. if (!p->dvfs_disable_by_suspend)
  4195. _kick_PBM_by_cpu(p);
  4196. #endif
  4197. }
  4198. cpufreq_unlock(flags);
  4199. } else {
  4200. p->dvfs_disable_by_procfs = false;
  4201. cpufreq_err("frequency %dKHz! is not found in CPU opp table\n",
  4202. freq);
  4203. }
  4204. }
  4205. } else {
  4206. p->dvfs_disable_by_procfs = false;
  4207. cpufreq_err("echo khz > /proc/cpufreq/cpufreq_freq\n");
  4208. }
  4209. end:
  4210. free_page((unsigned long)buf);
  4211. return count;
  4212. }
  4213. /* cpufreq_volt */
  4214. static int cpufreq_volt_proc_show(struct seq_file *m, void *v) /* <-XXX */
  4215. {
  4216. struct mt_cpu_dvfs *p = (struct mt_cpu_dvfs *)m->private;
  4217. #ifdef CONFIG_CPU_DVFS_HAS_EXTBUCK
  4218. if (cpu_dvfs_is_extbuck_valid()) {
  4219. seq_printf(m, "Vproc: %d mv\n", p->ops->get_cur_volt(p) / 100); /* mv */
  4220. seq_printf(m, "Vsram: %d mv\n", _mt_cpufreq_get_cur_vsram(p) / 100); /* mv */
  4221. } else
  4222. seq_printf(m, "%d mv\n", p->ops->get_cur_volt(p) / 100); /* mv */
  4223. #else
  4224. seq_printf(m, "%d mv\n", p->ops->get_cur_volt(p) / 100); /* mv */
  4225. #endif
  4226. return 0;
  4227. }
  4228. static ssize_t cpufreq_volt_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos)
  4229. {
  4230. unsigned long flags;
  4231. struct mt_cpu_dvfs *p = (struct mt_cpu_dvfs *)PDE_DATA(file_inode(file));
  4232. int mv;
  4233. char *buf = _copy_from_user_for_proc(buffer, count);
  4234. if (!buf)
  4235. return -EINVAL;
  4236. if (!kstrtoint(buf, 10, &mv)) {
  4237. p->dvfs_disable_by_procfs = true;
  4238. cpufreq_lock(flags);
  4239. _mt_cpufreq_set_cur_volt_locked(p, mv * 100);
  4240. cpufreq_unlock(flags);
  4241. } else {
  4242. p->dvfs_disable_by_procfs = false;
  4243. cpufreq_err("echo mv > /proc/cpufreq/cpufreq_volt\n");
  4244. }
  4245. free_page((unsigned long)buf);
  4246. return count;
  4247. }
  4248. #ifdef CONFIG_CPU_DVFS_TURBO_MODE
  4249. /* cpufreq_turbo_mode */
  4250. static int cpufreq_turbo_mode_proc_show(struct seq_file *m, void *v) /* <-XXX */
  4251. {
  4252. struct mt_cpu_dvfs *p = (struct mt_cpu_dvfs *)m->private;
  4253. int i;
  4254. seq_printf(m, "turbo_mode = %d\n", p->turbo_mode);
  4255. for (i = 0; i < NR_TURBO_MODE; i++) {
  4256. seq_printf(m, "[%d] = { .temp = %d, .freq_delta = %d, .volt_delta = %d }\n",
  4257. i,
  4258. turbo_mode_cfg[i].temp,
  4259. turbo_mode_cfg[i].freq_delta, turbo_mode_cfg[i].volt_delta);
  4260. }
  4261. return 0;
  4262. }
  4263. static ssize_t cpufreq_turbo_mode_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos)
  4264. struct mt_cpu_dvfs *p = (struct mt_cpu_dvfs *)PDE_DATA(file_inode(file));
  4265. unsigned int turbo_mode;
  4266. int temp;
  4267. int freq_delta;
  4268. int volt_delta;
  4269. char *buf = _copy_from_user_for_proc(buffer, count);
  4270. if (!buf)
  4271. return -EINVAL;
  4272. if ((sscanf(buf, "%d %d %d %d", &turbo_mode, &temp, &freq_delta, &volt_delta) == 4)
  4273. && turbo_mode < NR_TURBO_MODE) {
  4274. turbo_mode_cfg[turbo_mode].temp = temp;
  4275. turbo_mode_cfg[turbo_mode].freq_delta = freq_delta;
  4276. turbo_mode_cfg[turbo_mode].volt_delta = volt_delta;
  4277. } else if (!kstrtouint(buf, 10, &turbo_mode))
  4278. p->turbo_mode = turbo_mode;
  4279. else {
  4280. cpufreq_err("echo 0/1 > /proc/cpufreq/cpufreq_turbo_mode\n");
  4281. cpufreq_err("echo idx temp freq_delta volt_delta > /proc/cpufreq/cpufreq_turbo_mode\n");
  4282. }
  4283. free_page((unsigned long)buf);
  4284. return count;
  4285. }
  4286. #endif
  4287. #define PROC_FOPS_RW(name) \
  4288. static int name ## _proc_open(struct inode *inode, struct file *file) \
  4289. { \
  4290. return single_open(file, name ## _proc_show, PDE_DATA(inode)); \
  4291. } \
  4292. static const struct file_operations name ## _proc_fops = { \
  4293. .owner = THIS_MODULE, \
  4294. .open = name ## _proc_open, \
  4295. .read = seq_read, \
  4296. .llseek = seq_lseek, \
  4297. .release = single_release, \
  4298. .write = name ## _proc_write, \
  4299. }
  4300. #define PROC_FOPS_RO(name) \
  4301. static int name ## _proc_open(struct inode *inode, struct file *file) \
  4302. { \
  4303. return single_open(file, name ## _proc_show, PDE_DATA(inode)); \
  4304. } \
  4305. static const struct file_operations name ## _proc_fops = { \
  4306. .owner = THIS_MODULE, \
  4307. .open = name ## _proc_open, \
  4308. .read = seq_read, \
  4309. .llseek = seq_lseek, \
  4310. .release = single_release, \
  4311. }
  4312. #define PROC_ENTRY(name) {__stringify(name), &name ## _proc_fops}
  4313. PROC_FOPS_RW(cpufreq_debug);
  4314. PROC_FOPS_RW(cpufreq_fftt_test);
  4315. PROC_FOPS_RW(cpufreq_stress_test);
  4316. PROC_FOPS_RW(cpufreq_fix_freq_in_es);
  4317. PROC_FOPS_RW(cpufreq_limited_by_hevc);
  4318. #ifndef DISABLE_PBM_FEATURE
  4319. PROC_FOPS_RW(cpufreq_limited_by_pbm);
  4320. #endif
  4321. PROC_FOPS_RW(cpufreq_limited_by_thermal);
  4322. #ifdef CONFIG_ARCH_MT6753
  4323. PROC_FOPS_RW(cpufreq_5A_throttle_enable);
  4324. #endif
  4325. PROC_FOPS_RW(cpufreq_limited_max_freq_by_user);
  4326. PROC_FOPS_RO(cpufreq_power_dump);
  4327. PROC_FOPS_RO(cpufreq_ptpod_freq_volt);
  4328. PROC_FOPS_RW(cpufreq_state);
  4329. PROC_FOPS_RW(cpufreq_oppidx); /* <-XXX */
  4330. PROC_FOPS_RW(cpufreq_freq); /* <-XXX */
  4331. PROC_FOPS_RW(cpufreq_volt); /* <-XXX */
  4332. #ifdef CONFIG_CPU_DVFS_TURBO_MODE
  4333. PROC_FOPS_RW(cpufreq_turbo_mode); /* <-XXX */
  4334. #endif
  4335. static int _mt_cpufreq_create_procfs(void)
  4336. {
  4337. struct proc_dir_entry *dir = NULL;
  4338. /* struct proc_dir_entry *cpu_dir = NULL; */
  4339. struct mt_cpu_dvfs *p = id_to_cpu_dvfs(0);
  4340. int i; /* , j; */
  4341. struct pentry {
  4342. const char *name;
  4343. const struct file_operations *fops;
  4344. };
  4345. const struct pentry entries[] = {
  4346. PROC_ENTRY(cpufreq_debug),
  4347. PROC_ENTRY(cpufreq_fftt_test),
  4348. PROC_ENTRY(cpufreq_stress_test),
  4349. PROC_ENTRY(cpufreq_fix_freq_in_es),
  4350. PROC_ENTRY(cpufreq_limited_by_thermal),
  4351. #ifdef CONFIG_ARCH_MT6753
  4352. PROC_ENTRY(cpufreq_5A_throttle_enable),
  4353. #endif
  4354. #ifndef DISABLE_PBM_FEATURE
  4355. PROC_ENTRY(cpufreq_limited_by_pbm),
  4356. #endif
  4357. PROC_ENTRY(cpufreq_power_dump),
  4358. };
  4359. const struct pentry cpu_entries[] = {
  4360. PROC_ENTRY(cpufreq_limited_by_hevc),
  4361. PROC_ENTRY(cpufreq_limited_max_freq_by_user),
  4362. PROC_ENTRY(cpufreq_ptpod_freq_volt),
  4363. PROC_ENTRY(cpufreq_state),
  4364. PROC_ENTRY(cpufreq_oppidx), /* <-XXX */
  4365. PROC_ENTRY(cpufreq_freq), /* <-XXX */
  4366. PROC_ENTRY(cpufreq_volt), /* <-XXX */
  4367. #ifdef CONFIG_CPU_DVFS_TURBO_MODE
  4368. PROC_ENTRY(cpufreq_turbo_mode), /* <-XXX */
  4369. #endif
  4370. };
  4371. dir = proc_mkdir("cpufreq", NULL);
  4372. if (!dir) {
  4373. cpufreq_err("fail to create /proc/cpufreq @ %s()\n", __func__);
  4374. return -ENOMEM;
  4375. }
  4376. for (i = 0; i < ARRAY_SIZE(entries); i++) {
  4377. if (!proc_create
  4378. (entries[i].name, S_IRUGO | S_IWUSR | S_IWGRP, dir, entries[i].fops))
  4379. cpufreq_err("%s(), create /proc/cpufreq/%s failed\n",
  4380. __func__, entries[i].name);
  4381. }
  4382. for (i = 0; i < ARRAY_SIZE(cpu_entries); i++) {
  4383. if (!proc_create_data
  4384. (cpu_entries[i].name, S_IRUGO | S_IWUSR | S_IWGRP, dir, cpu_entries[i].fops, p))
  4385. cpufreq_err("%s(), create /proc/cpufreq/%s failed\n",
  4386. __func__, cpu_entries[i].name);
  4387. }
  4388. return 0;
  4389. }
  4390. #endif /* CONFIG_PROC_FS */
  4391. /*
  4392. * Module driver
  4393. */
  4394. static int __init _mt_cpufreq_pdrv_init(void)
  4395. {
  4396. int ret = 0;
  4397. FUNC_ENTER(FUNC_LV_MODULE);
  4398. #ifdef CONFIG_PROC_FS
  4399. /* init proc */
  4400. if (_mt_cpufreq_create_procfs())
  4401. goto out;
  4402. #endif /* CONFIG_PROC_FS */
  4403. /* register platform device/driver */
  4404. ret = platform_device_register(&_mt_cpufreq_pdev);
  4405. if (ret) {
  4406. cpufreq_err("fail to register cpufreq device @ %s()\n", __func__);
  4407. goto out;
  4408. }
  4409. ret = platform_driver_register(&_mt_cpufreq_pdrv);
  4410. if (ret) {
  4411. cpufreq_err("fail to register cpufreq driver @ %s()\n", __func__);
  4412. platform_device_unregister(&_mt_cpufreq_pdev);
  4413. }
  4414. cpufreq_info("CPU DVFS pdrv init done.\n");
  4415. out:
  4416. FUNC_EXIT(FUNC_LV_MODULE);
  4417. return ret;
  4418. }
  4419. static void __exit _mt_cpufreq_pdrv_exit(void)
  4420. {
  4421. FUNC_ENTER(FUNC_LV_MODULE);
  4422. platform_driver_unregister(&_mt_cpufreq_pdrv);
  4423. platform_device_unregister(&_mt_cpufreq_pdev);
  4424. FUNC_EXIT(FUNC_LV_MODULE);
  4425. }
  4426. late_initcall(_mt_cpufreq_pdrv_init);
  4427. module_exit(_mt_cpufreq_pdrv_exit);
  4428. MODULE_DESCRIPTION("MediaTek CPU DVFS Driver v0.3");
  4429. MODULE_LICENSE("GPL");