mtk_dsi.c 157 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059
  1. /*
  2. * Copyright (c) 2015 MediaTek Inc.
  3. * Copyright (C) 2021 XiaoMi, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <drm/drmP.h>
  15. #include <drm/drm_atomic_helper.h>
  16. #include <drm/drm_crtc_helper.h>
  17. #include <drm/drm_mipi_dsi.h>
  18. #include <drm/drm_panel.h>
  19. #include <drm/drm_crtc_helper.h>
  20. #include <linux/clk.h>
  21. #include <linux/sched.h>
  22. #include <linux/sched/clock.h>
  23. #include <linux/component.h>
  24. #include <linux/irq.h>
  25. #include <linux/of.h>
  26. #include <linux/of_platform.h>
  27. #include <linux/of_graph.h>
  28. #include <linux/phy/phy.h>
  29. #include <linux/platform_device.h>
  30. #include <video/mipi_display.h>
  31. #include <video/videomode.h>
  32. #include <linux/soc/mediatek/mtk-cmdq.h>
  33. #if defined(CONFIG_MACH_MT6873) || defined(CONFIG_MACH_MT6853) \
  34. || defined(CONFIG_MACH_MT6833)
  35. #include <linux/ratelimit.h>
  36. #endif
  37. #include "mtk_drm_ddp_comp.h"
  38. #include "mtk_drm_crtc.h"
  39. #include "mtk_drm_drv.h"
  40. #include "mtk_drm_helper.h"
  41. #include "mtk_mipi_tx.h"
  42. #include "mtk_dump.h"
  43. #include "mtk_log.h"
  44. #include "mtk_drm_lowpower.h"
  45. #include "mtk_drm_mmp.h"
  46. #include "mtk_drm_arr.h"
  47. #include "mtk_panel_ext.h"
  48. /* ************ Panel Master ********** */
  49. #include "mtk_drm_fbdev.h"
  50. #include "mtk_fbconfig_kdebug.h"
  51. /* ********* end Panel Master *********** */
  52. #define DSI_START 0x00
  53. #define SLEEPOUT_START BIT(2)
  54. #define VM_CMD_START BIT(16)
  55. #define START_FLD_REG_START REG_FLD_MSB_LSB(0, 0)
  56. #define DSI_INTEN 0x08
  57. #define DSI_INTSTA 0x0c
  58. #define LPRX_RD_RDY_INT_FLAG BIT(0)
  59. #define CMD_DONE_INT_FLAG BIT(1)
  60. #define TE_RDY_INT_FLAG BIT(2)
  61. #define VM_DONE_INT_FLAG BIT(3)
  62. #define FRAME_DONE_INT_FLAG BIT(4)
  63. #define VM_CMD_DONE_INT_EN BIT(5)
  64. #define SLEEPOUT_DONE_INT_FLAG BIT(6)
  65. #define BUFFER_UNDERRUN_INT_FLAG BIT(12)
  66. #define INP_UNFINISH_INT_EN BIT(14)
  67. #define SLEEPIN_ULPS_DONE_INT_FLAG BIT(15)
  68. #define DSI_BUSY BIT(31)
  69. #define INTSTA_FLD_REG_RD_RDY REG_FLD_MSB_LSB(0, 0)
  70. #define INTSTA_FLD_REG_CMD_DONE REG_FLD_MSB_LSB(1, 1)
  71. #define INTSTA_FLD_REG_TE_RDY REG_FLD_MSB_LSB(2, 2)
  72. #define INTSTA_FLD_REG_VM_DONE REG_FLD_MSB_LSB(3, 3)
  73. #define INTSTA_FLD_REG_FRM_DONE REG_FLD_MSB_LSB(4, 4)
  74. #define INTSTA_FLD_REG_VM_CMD_DONE REG_FLD_MSB_LSB(5, 5)
  75. #define INTSTA_FLD_REG_SLEEPOUT_DONE REG_FLD_MSB_LSB(6, 6)
  76. #define INTSTA_FLD_REG_BUSY REG_FLD_MSB_LSB(31, 31)
  77. #define DSI_CON_CTRL 0x10
  78. #define DSI_RESET BIT(0)
  79. #define DSI_EN BIT(1)
  80. #define DSI_PHY_RESET BIT(2)
  81. #define CON_CTRL_FLD_REG_DUAL_EN REG_FLD_MSB_LSB(4, 4)
  82. #define DSI_CM_WAIT_FIFO_FULL_EN BIT(27)
  83. #define DSI_MODE_CTRL 0x14
  84. #define MODE (3)
  85. #define CMD_MODE 0
  86. #define SYNC_PULSE_MODE 1
  87. #define SYNC_EVENT_MODE 2
  88. #define BURST_MODE 3
  89. #define FRM_MODE BIT(16)
  90. #define MIX_MODE BIT(17)
  91. #define SLEEP_MODE BIT(20)
  92. #define MODE_FLD_REG_MODE_CON REG_FLD_MSB_LSB(1, 0)
  93. #define DSI_TXRX_CTRL 0x18
  94. #define VC_NUM BIT(1)
  95. #define LANE_NUM (0xf << 2)
  96. #define DIS_EOT BIT(6)
  97. #define NULL_EN BIT(7)
  98. #define TE_FREERUN BIT(8)
  99. #define EXT_TE_EN BIT(9)
  100. #define EXT_TE_EDGE BIT(10)
  101. #define MAX_RTN_SIZE (0xf << 12)
  102. #define HSTX_CKLP_EN BIT(16)
  103. #define TXRX_CTRL_FLD_REG_LANE_NUM REG_FLD_MSB_LSB(5, 2)
  104. #define TXRX_CTRL_FLD_REG_EXT_TE_EN REG_FLD_MSB_LSB(9, 9)
  105. #define TXRX_CTRL_FLD_REG_EXT_TE_EDGE REG_FLD_MSB_LSB(10, 10)
  106. #define TXRX_CTRL_FLD_REG_HSTX_CKLP_EN REG_FLD_MSB_LSB(16, 16)
  107. #define DSI_PSCTRL 0x1c
  108. #define DSI_PS_WC REG_FLD_MSB_LSB(14, 0)
  109. #define DSI_PS_SEL REG_FLD_MSB_LSB(18, 16)
  110. #define DSI_VSA_NL 0x20
  111. #define DSI_VBP_NL 0x24
  112. #define DSI_VFP_NL 0x28
  113. #define DSI_SIZE_CON 0x38
  114. #define DSI_VACT_NL 0x2C
  115. #define DSI_LFR_CON 0x30
  116. #define DSI_LFR_STA 0x34
  117. #define LFR_STA_FLD_REG_LFR_SKIP_STA REG_FLD_MSB_LSB(8, 8)
  118. #define LFR_STA_FLD_REG_LFR_SKIP_CNT REG_FLD_MSB_LSB(5, 0)
  119. #define LFR_CON_FLD_REG_LFR_MODE REG_FLD_MSB_LSB(1, 0)
  120. #define LFR_CON_FLD_REG_LFR_TYPE REG_FLD_MSB_LSB(3, 2)
  121. #define LFR_CON_FLD_REG_LFR_EN REG_FLD_MSB_LSB(4, 4)
  122. #define LFR_CON_FLD_REG_LFR_UPDATE REG_FLD_MSB_LSB(5, 5)
  123. #define LFR_CON_FLD_REG_LFR_VSE_DIS REG_FLD_MSB_LSB(6, 6)
  124. #define LFR_CON_FLD_REG_LFR_SKIP_NUM REG_FLD_MSB_LSB(13, 8)
  125. #define DSI_HSA_WC 0x50
  126. #define DSI_HBP_WC 0x54
  127. #define DSI_HFP_WC 0x58
  128. #define DSI_BLLP_WC 0x5C
  129. #define DSI_CMDQ_SIZE 0x60
  130. #define CMDQ_SIZE 0x3f
  131. #define DSI_HSTX_CKL_WC 0x64
  132. #define DSI_RX_DATA0 0x74
  133. #define DSI_RX_DATA1 0x78
  134. #define DSI_RX_DATA2 0x7c
  135. #define DSI_RX_DATA3 0x80
  136. #define DSI_RACK 0x84
  137. #define RACK BIT(0)
  138. #define DSI_MEM_CONTI 0x90
  139. #define DSI_WMEM_CONTI 0x3C
  140. #define DSI_TIME_CON0 0xA0
  141. #define DSI_PHY_LCCON 0x104
  142. #define LC_HS_TX_EN BIT(0)
  143. #define LC_ULPM_EN BIT(1)
  144. #define LC_WAKEUP_EN BIT(2)
  145. #define PHY_FLD_REG_LC_HSTX_EN REG_FLD_MSB_LSB(0, 0)
  146. #define DSI_PHY_LD0CON 0x108
  147. #define LD0_HS_TX_EN BIT(0)
  148. #define LD0_ULPM_EN BIT(1)
  149. #define LD0_WAKEUP_EN BIT(2)
  150. #define LDX_ULPM_AS_L0 BIT(3)
  151. #define DSI_PHY_TIMECON0 0x110
  152. #define LPX (0xff << 0)
  153. #define HS_PREP (0xff << 8)
  154. #define HS_ZERO (0xff << 16)
  155. #define HS_TRAIL (0xff << 24)
  156. #define FLD_LPX REG_FLD_MSB_LSB(7, 0)
  157. #define FLD_HS_PREP REG_FLD_MSB_LSB(15, 8)
  158. #define FLD_HS_ZERO REG_FLD_MSB_LSB(23, 16)
  159. #define FLD_HS_TRAIL REG_FLD_MSB_LSB(31, 24)
  160. #define DSI_PHY_TIMECON1 0x114
  161. #define TA_GO (0xff << 0)
  162. #define TA_SURE (0xff << 8)
  163. #define TA_GET (0xff << 16)
  164. #define DA_HS_EXIT (0xff << 24)
  165. #define FLD_TA_GO REG_FLD_MSB_LSB(7, 0)
  166. #define FLD_TA_SURE REG_FLD_MSB_LSB(15, 8)
  167. #define FLD_TA_GET REG_FLD_MSB_LSB(23, 16)
  168. #define FLD_DA_HS_EXIT REG_FLD_MSB_LSB(31, 24)
  169. #define DSI_PHY_TIMECON2 0x118
  170. #define CONT_DET (0xff << 0)
  171. #define CLK_ZERO (0xff << 16)
  172. #define CLK_TRAIL (0xff << 24)
  173. #define FLD_CONT_DET REG_FLD_MSB_LSB(7, 0)
  174. #define FLD_DA_HS_SYNC REG_FLD_MSB_LSB(15, 8)
  175. #define FLD_CLK_HS_ZERO REG_FLD_MSB_LSB(23, 16)
  176. #define FLD_CLK_HS_TRAIL REG_FLD_MSB_LSB(31, 24)
  177. #define DSI_PHY_TIMECON3 0x11c
  178. #define CLK_HS_PREP (0xff << 0)
  179. #define CLK_HS_POST (0xff << 8)
  180. #define CLK_HS_EXIT (0xff << 16)
  181. #define FLD_CLK_HS_PREP REG_FLD_MSB_LSB(7, 0)
  182. #define FLD_CLK_HS_POST REG_FLD_MSB_LSB(15, 8)
  183. #define FLD_CLK_HS_EXIT REG_FLD_MSB_LSB(23, 16)
  184. #define DSI_CPHY_CON0 0x120
  185. #define DSI_VM_CMD_CON 0x130
  186. #define VM_CMD_EN BIT(0)
  187. #define TS_VFP_EN BIT(5)
  188. #define DSI_VM_CMD_DATA0 0x134
  189. #define DSI_VM_CMD_DATA10 0x180
  190. #define DSI_VM_CMD_DATA20 0x1A0
  191. #define DSI_VM_CMD_DATA30 0x1B0
  192. #define DSI_VM_CMD_DATA0 0x134
  193. #define DSI_STATE_DBG6 0x160
  194. #define STATE_DBG6_FLD_REG_CMCTL_STATE REG_FLD_MSB_LSB(14, 0)
  195. #define DSI_SHADOW_DEBUG 0x190
  196. #define DSI_BYPASS_SHADOW BIT(1)
  197. #define DSI_READ_WORKING BIT(2)
  198. #define DSI_CMDQ0 0x200
  199. #define DSI_CMDQ1 0x204
  200. #define CONFIG (0xff << 0)
  201. #define SHORT_PACKET 0
  202. #define LONG_PACKET 2
  203. #define VM_LONG_PACKET BIT(1)
  204. #define BTA BIT(2)
  205. #define HSTX BIT(3)
  206. #define DATA_ID (0xff << 8)
  207. #define DATA_0 (0xff << 16)
  208. #define DATA_1 (0xff << 24)
  209. #define MMSYS_SW_RST_DSI_B BIT(2)
  210. #define MMSYS_SW_RST_DSI1_B BIT(3)
  211. #define DSI_START_FLD_DSI_START REG_FLD_MSB_LSB(0, 0)
  212. #define DSI_INSTA_FLD_DSI_BUSY REG_FLD_MSB_LSB(31, 31)
  213. #define DSI_COM_CON_FLD_DUAL_EN REG_FLD_MSB_LSB(4, 4)
  214. #define DSI_MODE_CON_FLD_MODE_CON REG_FLD_MSB_LSB(1, 0)
  215. #define T_LPX (8)
  216. #define T_HS_PREP (7)
  217. #define T_HS_TRAIL (8)
  218. #define T_HS_EXIT (16)
  219. #define T_HS_ZERO (15)
  220. #define DA_HS_SYNC (1)
  221. #define NS_TO_CYCLE(n, c) ((n) / (c))
  222. #define MTK_DSI_HOST_IS_READ(type) \
  223. ((type == MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM) || \
  224. (type == MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM) || \
  225. (type == MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM) || \
  226. (type == MIPI_DSI_DCS_READ))
  227. struct phy;
  228. struct mtk_dsi;
  229. #define DSI_DCS_SHORT_PACKET_ID_0 0x05
  230. #define DSI_DCS_SHORT_PACKET_ID_1 0x15
  231. #define DSI_DCS_LONG_PACKET_ID 0x39
  232. #define DSI_DCS_READ_PACKET_ID 0x06
  233. #define DSI_GERNERIC_SHORT_PACKET_ID_1 0x13
  234. #define DSI_GERNERIC_SHORT_PACKET_ID_2 0x23
  235. #define DSI_GERNERIC_LONG_PACKET_ID 0x29
  236. #define DSI_GERNERIC_READ_LONG_PACKET_ID 0x14
  237. struct DSI_T0_INS {
  238. unsigned CONFG : 8;
  239. unsigned Data_ID : 8;
  240. unsigned Data0 : 8;
  241. unsigned Data1 : 8;
  242. };
  243. #define DECLARE_DSI_PORCH(EXPR) \
  244. EXPR(DSI_VFP) \
  245. EXPR(DSI_VSA) \
  246. EXPR(DSI_VBP) \
  247. EXPR(DSI_VACT) \
  248. EXPR(DSI_HFP) \
  249. EXPR(DSI_HSA) \
  250. EXPR(DSI_HBP) \
  251. EXPR(DSI_BLLP) \
  252. EXPR(DSI_PORCH_NUM)
  253. enum dsi_porch_type { DECLARE_DSI_PORCH(DECLARE_NUM) };
  254. static const char * const mtk_dsi_porch_str[] = {
  255. DECLARE_DSI_PORCH(DECLARE_STR)};
  256. #define AS_UINT32(x) (*(u32 *)((void *)x))
  257. struct mtk_dsi_driver_data {
  258. const u32 reg_cmdq_ofs;
  259. s32 (*poll_for_idle)(struct mtk_dsi *dsi, struct cmdq_pkt *handle);
  260. irqreturn_t (*irq_handler)(int irq, void *dev_id);
  261. char *esd_eint_compat;
  262. bool support_shadow;
  263. };
  264. struct t_condition_wq {
  265. wait_queue_head_t wq;
  266. atomic_t condition;
  267. };
  268. struct mtk_dsi {
  269. struct mtk_ddp_comp ddp_comp;
  270. struct device *dev;
  271. struct mipi_dsi_host host;
  272. struct drm_encoder encoder;
  273. struct drm_connector conn;
  274. struct drm_panel *panel;
  275. struct mtk_panel_ext *ext;
  276. struct cmdq_pkt_buffer cmdq_buf;
  277. struct drm_bridge *bridge;
  278. struct phy *phy;
  279. void __iomem *regs;
  280. struct clk *engine_clk;
  281. struct clk *digital_clk;
  282. struct clk *hs_clk;
  283. u32 data_rate;
  284. unsigned long mode_flags;
  285. enum mipi_dsi_pixel_format format;
  286. unsigned int lanes;
  287. struct videomode vm;
  288. int clk_refcnt;
  289. bool output_en;
  290. bool doze_enabled;
  291. u32 irq_data;
  292. wait_queue_head_t irq_wait_queue;
  293. struct mtk_dsi_driver_data *driver_data;
  294. struct t_condition_wq enter_ulps_done;
  295. struct t_condition_wq exit_ulps_done;
  296. struct t_condition_wq te_rdy;
  297. struct t_condition_wq frame_done;
  298. unsigned int hs_trail;
  299. unsigned int hs_prpr;
  300. unsigned int hs_zero;
  301. unsigned int lpx;
  302. unsigned int ta_get;
  303. unsigned int ta_sure;
  304. unsigned int ta_go;
  305. unsigned int da_hs_exit;
  306. unsigned int cont_det;
  307. unsigned int clk_zero;
  308. unsigned int clk_hs_prpr;
  309. unsigned int clk_hs_exit;
  310. unsigned int clk_hs_post;
  311. unsigned int vsa;
  312. unsigned int vbp;
  313. unsigned int vfp;
  314. unsigned int hsa_byte;
  315. unsigned int hbp_byte;
  316. unsigned int hfp_byte;
  317. bool mipi_hopping_sta;
  318. bool panel_osc_hopping_sta;
  319. unsigned int data_phy_cycle;
  320. /* for Panel Master dcs read/write */
  321. struct mipi_dsi_device *dev_for_PM;
  322. };
  323. enum DSI_MODE_CON {
  324. MODE_CON_CMD = 0,
  325. MODE_CON_SYNC_PULSE_VDO,
  326. MODE_CON_SYNC_EVENT_VDO,
  327. MODE_CON_BURST_VDO,
  328. };
  329. struct mtk_panel_ext *mtk_dsi_get_panel_ext(struct mtk_ddp_comp *comp);
  330. static inline struct mtk_dsi *encoder_to_dsi(struct drm_encoder *e)
  331. {
  332. return container_of(e, struct mtk_dsi, encoder);
  333. }
  334. static inline struct mtk_dsi *connector_to_dsi(struct drm_connector *c)
  335. {
  336. return container_of(c, struct mtk_dsi, conn);
  337. }
  338. static inline struct mtk_dsi *host_to_dsi(struct mipi_dsi_host *h)
  339. {
  340. return container_of(h, struct mtk_dsi, host);
  341. }
  342. static void mtk_dsi_mask(struct mtk_dsi *dsi, u32 offset, u32 mask, u32 data)
  343. {
  344. u32 temp = readl(dsi->regs + offset);
  345. writel((temp & ~mask) | (data & mask), dsi->regs + offset);
  346. }
  347. #define CHK_SWITCH(a, b) ((a == 0) ? b : a)
  348. static bool mtk_dsi_doze_state(struct mtk_dsi *dsi)
  349. {
  350. struct drm_crtc *crtc = dsi->encoder.crtc;
  351. struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
  352. return state->prop_val[CRTC_PROP_DOZE_ACTIVE];
  353. }
  354. static bool mtk_dsi_doze_status_change(struct mtk_dsi *dsi)
  355. {
  356. bool doze_enabled = mtk_dsi_doze_state(dsi);
  357. if (dsi->doze_enabled == doze_enabled)
  358. return false;
  359. return true;
  360. }
  361. static void mtk_dsi_dphy_timconfig(struct mtk_dsi *dsi, void *handle)
  362. {
  363. struct mtk_dsi_phy_timcon *phy_timcon = NULL;
  364. u32 lpx = 0, hs_prpr = 0, hs_zero = 0, hs_trail = 0;
  365. u32 ta_get = 0, ta_sure = 0, ta_go = 0, da_hs_exit = 0;
  366. u32 clk_zero = 0, clk_trail = 0, da_hs_sync = 0;
  367. u32 clk_hs_prpr = 0, clk_hs_exit = 0, clk_hs_post = 0;
  368. u32 cont_det = 0;
  369. u32 ui = 0, cycle_time = 0;
  370. u32 value = 0;
  371. struct mtk_ddp_comp *comp = &dsi->ddp_comp;
  372. ui = 1000 / dsi->data_rate + 0x01;
  373. cycle_time = 8000 / dsi->data_rate + 0x01;
  374. lpx = NS_TO_CYCLE(dsi->data_rate * 0x4B, 0x1F40) + 0x1;
  375. hs_prpr = NS_TO_CYCLE((0x40 + 0x5 * ui), cycle_time) + 0x1;
  376. hs_zero = NS_TO_CYCLE((0xC8 + 0x0A * ui), cycle_time);
  377. hs_zero = hs_zero > hs_prpr ? hs_zero - hs_prpr : hs_zero;
  378. hs_trail = NS_TO_CYCLE((0x4 * ui + 0x50) *
  379. dsi->data_rate, 0x1F40) + 0x1;
  380. ta_get = 5 * lpx;
  381. ta_sure = 3 * lpx / 2;
  382. ta_go = 4 * lpx;
  383. da_hs_exit = 2 * lpx;
  384. clk_zero = NS_TO_CYCLE(0x190, cycle_time);
  385. clk_trail = NS_TO_CYCLE(0x64 * dsi->data_rate, 0x1F40) + 0x1;
  386. da_hs_sync = 0x1;
  387. cont_det = 0x3;
  388. clk_hs_prpr = NS_TO_CYCLE(0x50 * dsi->data_rate, 0x1F40);
  389. clk_hs_exit = 2 * lpx;
  390. clk_hs_post = NS_TO_CYCLE(0x60 + 0x34 * ui, cycle_time);
  391. if (!(dsi->ext && dsi->ext->params))
  392. goto CONFIG_REG;
  393. phy_timcon = &dsi->ext->params->phy_timcon;
  394. lpx = CHK_SWITCH(phy_timcon->lpx, lpx);
  395. hs_prpr = CHK_SWITCH(phy_timcon->hs_prpr, hs_prpr);
  396. hs_zero = CHK_SWITCH(phy_timcon->hs_zero, hs_zero);
  397. hs_trail = CHK_SWITCH(phy_timcon->hs_trail, hs_trail);
  398. ta_get = CHK_SWITCH(phy_timcon->ta_get, ta_get);
  399. ta_sure = CHK_SWITCH(phy_timcon->ta_sure, ta_sure);
  400. ta_go = CHK_SWITCH(phy_timcon->ta_go, ta_go);
  401. da_hs_exit = CHK_SWITCH(phy_timcon->da_hs_exit, da_hs_exit);
  402. clk_zero = CHK_SWITCH(phy_timcon->clk_zero, clk_zero);
  403. clk_trail = CHK_SWITCH(phy_timcon->clk_trail, clk_trail);
  404. da_hs_sync = CHK_SWITCH(phy_timcon->da_hs_sync, da_hs_sync);
  405. clk_hs_prpr = CHK_SWITCH(phy_timcon->clk_hs_prpr, clk_hs_prpr);
  406. clk_hs_exit = CHK_SWITCH(phy_timcon->clk_hs_exit, clk_hs_exit);
  407. clk_hs_post = CHK_SWITCH(phy_timcon->clk_hs_post, clk_hs_post);
  408. CONFIG_REG:
  409. value = REG_FLD_VAL(FLD_LPX, lpx)
  410. | REG_FLD_VAL(FLD_HS_PREP, hs_prpr)
  411. | REG_FLD_VAL(FLD_HS_ZERO, hs_zero)
  412. | REG_FLD_VAL(FLD_HS_TRAIL, hs_trail);
  413. if (handle)
  414. cmdq_pkt_write((struct cmdq_pkt *)handle, comp->cmdq_base,
  415. comp->regs_pa+DSI_PHY_TIMECON0, value, ~0);
  416. else
  417. writel(value, dsi->regs + DSI_PHY_TIMECON0);
  418. value = REG_FLD_VAL(FLD_TA_GO, ta_go)
  419. | REG_FLD_VAL(FLD_TA_SURE, ta_sure)
  420. | REG_FLD_VAL(FLD_TA_GET, ta_get)
  421. | REG_FLD_VAL(FLD_DA_HS_EXIT, da_hs_exit);
  422. if (handle)
  423. cmdq_pkt_write((struct cmdq_pkt *)handle, comp->cmdq_base,
  424. comp->regs_pa+DSI_PHY_TIMECON1, value, ~0);
  425. else
  426. writel(value, dsi->regs + DSI_PHY_TIMECON1);
  427. value = REG_FLD_VAL(FLD_CONT_DET, cont_det)
  428. | REG_FLD_VAL(FLD_DA_HS_SYNC, da_hs_sync)
  429. | REG_FLD_VAL(FLD_CLK_HS_ZERO, clk_zero)
  430. | REG_FLD_VAL(FLD_CLK_HS_TRAIL, clk_trail);
  431. if (handle)
  432. cmdq_pkt_write((struct cmdq_pkt *)handle, comp->cmdq_base,
  433. comp->regs_pa+DSI_PHY_TIMECON2, value, ~0);
  434. else
  435. writel(value, dsi->regs + DSI_PHY_TIMECON2);
  436. value = REG_FLD_VAL(FLD_CLK_HS_PREP, clk_hs_prpr)
  437. | REG_FLD_VAL(FLD_CLK_HS_POST, clk_hs_post)
  438. | REG_FLD_VAL(FLD_CLK_HS_EXIT, clk_hs_exit);
  439. if (handle)
  440. cmdq_pkt_write((struct cmdq_pkt *)handle, comp->cmdq_base,
  441. comp->regs_pa+DSI_PHY_TIMECON3, value, ~0);
  442. else
  443. writel(value, dsi->regs + DSI_PHY_TIMECON3);
  444. }
  445. static void mtk_dsi_cphy_timconfig(struct mtk_dsi *dsi, void *handle)
  446. {
  447. struct mtk_dsi_phy_timcon *phy_timcon = NULL;
  448. u32 lpx = 0, hs_prpr = 0, hs_zero = 0, hs_trail = 0;
  449. u32 ta_get = 0, ta_sure = 0, ta_go = 0, da_hs_exit = 0;
  450. u32 clk_zero = 0, clk_trail = 0, da_hs_sync = 0;
  451. u32 clk_hs_prpr = 0, clk_hs_exit = 0, clk_hs_post = 0;
  452. u32 ui = 0, cycle_time = 0;
  453. u32 value = 0;
  454. struct mtk_ddp_comp *comp = &dsi->ddp_comp;
  455. DDPINFO("%s+\n", __func__);
  456. ui = 1000 / dsi->data_rate + 0x01;
  457. cycle_time = 8000 / dsi->data_rate + 0x01;
  458. lpx = NS_TO_CYCLE(dsi->data_rate * 0x4B, 0x1B58) + 0x1;
  459. hs_prpr = NS_TO_CYCLE(NS_TO_CYCLE(dsi->data_rate, 2) * 101,
  460. 0x1B58) + 0x1;
  461. hs_zero = 0x30;
  462. hs_trail = 0x20;
  463. ta_get = 5 * NS_TO_CYCLE(0x55, cycle_time);
  464. ta_sure = 3 * NS_TO_CYCLE(0x55, cycle_time) / 2;
  465. ta_go = 4 * NS_TO_CYCLE(0x55, cycle_time);
  466. da_hs_exit = NS_TO_CYCLE(NS_TO_CYCLE(dsi->data_rate, 2) * 225,
  467. 0x1B58) + 0x1;
  468. clk_zero = NS_TO_CYCLE(0x190, cycle_time);
  469. clk_trail = NS_TO_CYCLE(0x60, cycle_time) + 0x1;
  470. da_hs_sync = 0x1;
  471. clk_hs_prpr = NS_TO_CYCLE(0x40, cycle_time);
  472. clk_hs_exit = 2 * lpx;
  473. clk_hs_post = NS_TO_CYCLE(0x60 + 0x34 * ui, cycle_time);
  474. if (!(dsi->ext && dsi->ext->params))
  475. goto CONFIG_REG;
  476. phy_timcon = &dsi->ext->params->phy_timcon;
  477. lpx = CHK_SWITCH(phy_timcon->lpx, lpx);
  478. hs_prpr = CHK_SWITCH(phy_timcon->hs_prpr, hs_prpr);
  479. hs_zero = CHK_SWITCH(phy_timcon->hs_zero, hs_zero);
  480. hs_trail = CHK_SWITCH(phy_timcon->hs_trail, hs_trail);
  481. ta_get = CHK_SWITCH(phy_timcon->ta_get, ta_get);
  482. ta_sure = CHK_SWITCH(phy_timcon->ta_sure, ta_sure);
  483. ta_go = CHK_SWITCH(phy_timcon->ta_go, ta_go);
  484. da_hs_exit = CHK_SWITCH(phy_timcon->da_hs_exit, da_hs_exit);
  485. clk_zero = CHK_SWITCH(phy_timcon->clk_zero, clk_zero);
  486. clk_trail = CHK_SWITCH(phy_timcon->clk_trail, clk_trail);
  487. da_hs_sync = CHK_SWITCH(phy_timcon->da_hs_sync, da_hs_sync);
  488. clk_hs_prpr = CHK_SWITCH(phy_timcon->clk_hs_prpr, clk_hs_prpr);
  489. clk_hs_exit = CHK_SWITCH(phy_timcon->clk_hs_exit, clk_hs_exit);
  490. clk_hs_post = CHK_SWITCH(phy_timcon->clk_hs_post, clk_hs_post);
  491. CONFIG_REG:
  492. dsi->data_phy_cycle = hs_prpr + hs_zero + da_hs_exit + lpx + 5;
  493. value = REG_FLD_VAL(FLD_LPX, lpx)
  494. | REG_FLD_VAL(FLD_HS_PREP, hs_prpr)
  495. | REG_FLD_VAL(FLD_HS_ZERO, hs_zero)
  496. | REG_FLD_VAL(FLD_HS_TRAIL, hs_trail);
  497. if (handle)
  498. cmdq_pkt_write((struct cmdq_pkt *)handle, comp->cmdq_base,
  499. comp->regs_pa+DSI_PHY_TIMECON0, value, ~0);
  500. else
  501. writel(value, dsi->regs + DSI_PHY_TIMECON0);
  502. value = REG_FLD_VAL(FLD_TA_GO, ta_go)
  503. | REG_FLD_VAL(FLD_TA_SURE, ta_sure)
  504. | REG_FLD_VAL(FLD_TA_GET, ta_get)
  505. | REG_FLD_VAL(FLD_DA_HS_EXIT, da_hs_exit);
  506. if (handle)
  507. cmdq_pkt_write((struct cmdq_pkt *)handle, comp->cmdq_base,
  508. comp->regs_pa+DSI_PHY_TIMECON1, value, ~0);
  509. else
  510. writel(value, dsi->regs + DSI_PHY_TIMECON1);
  511. value = REG_FLD_VAL(FLD_DA_HS_SYNC, da_hs_sync)
  512. | REG_FLD_VAL(FLD_CLK_HS_ZERO, clk_zero)
  513. | REG_FLD_VAL(FLD_CLK_HS_TRAIL, clk_trail);
  514. if (handle)
  515. cmdq_pkt_write((struct cmdq_pkt *)handle, comp->cmdq_base,
  516. comp->regs_pa+DSI_PHY_TIMECON2, value, ~0);
  517. else
  518. writel(value, dsi->regs + DSI_PHY_TIMECON2);
  519. value = REG_FLD_VAL(FLD_CLK_HS_PREP, clk_hs_prpr)
  520. | REG_FLD_VAL(FLD_CLK_HS_POST, clk_hs_post)
  521. | REG_FLD_VAL(FLD_CLK_HS_EXIT, clk_hs_exit);
  522. if (handle)
  523. cmdq_pkt_write((struct cmdq_pkt *)handle, comp->cmdq_base,
  524. comp->regs_pa+DSI_PHY_TIMECON3, value, ~0);
  525. else
  526. writel(value, dsi->regs + DSI_PHY_TIMECON3);
  527. if (handle)
  528. cmdq_pkt_write((struct cmdq_pkt *)handle, comp->cmdq_base,
  529. comp->regs_pa+DSI_PHY_TIMECON0, 0x012c003, ~0);
  530. else
  531. writel(0x012c0003, dsi->regs + DSI_CPHY_CON0);
  532. }
  533. static void mtk_dsi_phy_timconfig(struct mtk_dsi *dsi,
  534. struct cmdq_pkt *handle)
  535. {
  536. dsi->ext = find_panel_ext(dsi->panel);
  537. if (!dsi->ext)
  538. return;
  539. if (dsi->ext->params->is_cphy)
  540. mtk_dsi_cphy_timconfig(dsi, handle);
  541. else
  542. mtk_dsi_dphy_timconfig(dsi, handle);
  543. }
  544. static void mtk_dsi_enable(struct mtk_dsi *dsi)
  545. {
  546. mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_EN, DSI_EN);
  547. #if !defined(CONFIG_MACH_MT6885)
  548. mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_CM_WAIT_FIFO_FULL_EN,
  549. DSI_CM_WAIT_FIFO_FULL_EN);
  550. #endif
  551. }
  552. static void mtk_dsi_disable(struct mtk_dsi *dsi)
  553. {
  554. mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_EN, 0);
  555. }
  556. static void mtk_dsi_reset_engine(struct mtk_dsi *dsi)
  557. {
  558. mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_RESET, DSI_RESET);
  559. mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_RESET, 0);
  560. }
  561. static void mtk_dsi_phy_reset(struct mtk_dsi *dsi)
  562. {
  563. mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_PHY_RESET, DSI_PHY_RESET);
  564. mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_PHY_RESET, 0);
  565. }
  566. static void mtk_dsi_clear_rxrd_irq(struct mtk_dsi *dsi)
  567. {
  568. mtk_dsi_mask(dsi, DSI_INTSTA, LPRX_RD_RDY_INT_FLAG, 0);
  569. }
  570. static unsigned int mtk_dsi_default_rate(struct mtk_dsi *dsi)
  571. {
  572. u32 data_rate;
  573. /**
  574. * vm.pixelclock is in kHz, pixel_clock unit is Hz, so multiply by 1000
  575. * htotal_time = htotal * byte_per_pixel / num_lanes
  576. * overhead_time = lpx + hs_prepare + hs_zero + hs_trail + hs_exit
  577. * mipi_ratio = (htotal_time + overhead_time) / htotal_time
  578. * data_rate = pixel_clock * bit_per_pixel * mipi_ratio / num_lanes;
  579. */
  580. if (dsi->ext && dsi->ext->params->data_rate) {
  581. data_rate = dsi->ext->params->data_rate;
  582. } else if (dsi->ext && dsi->ext->params->pll_clk) {
  583. data_rate = dsi->ext->params->pll_clk * 2;
  584. } else {
  585. u64 pixel_clock, total_bits;
  586. u32 htotal, htotal_bits, bit_per_pixel;
  587. u32 overhead_cycles, overhead_bits;
  588. switch (dsi->format) {
  589. case MIPI_DSI_FMT_RGB565:
  590. bit_per_pixel = 16;
  591. break;
  592. case MIPI_DSI_FMT_RGB666_PACKED:
  593. bit_per_pixel = 18;
  594. break;
  595. case MIPI_DSI_FMT_RGB666:
  596. case MIPI_DSI_FMT_RGB888:
  597. default:
  598. bit_per_pixel = 24;
  599. break;
  600. }
  601. pixel_clock = dsi->vm.pixelclock * 1000;
  602. htotal = dsi->vm.hactive + dsi->vm.hback_porch +
  603. dsi->vm.hfront_porch + dsi->vm.hsync_len;
  604. htotal_bits = htotal * bit_per_pixel;
  605. overhead_cycles = T_LPX + T_HS_PREP + T_HS_ZERO + T_HS_TRAIL +
  606. T_HS_EXIT;
  607. overhead_bits = overhead_cycles * dsi->lanes * 8;
  608. total_bits = htotal_bits + overhead_bits;
  609. data_rate = DIV_ROUND_UP_ULL(pixel_clock * total_bits,
  610. htotal * dsi->lanes);
  611. data_rate /= 1000000;
  612. }
  613. return data_rate;
  614. }
  615. static int mtk_dsi_set_LFR(struct mtk_dsi *dsi, struct mtk_ddp_comp *comp,
  616. void *handle)
  617. {
  618. u32 val = 0, mask = 0;
  619. //lfr_dbg: setting value form debug mode
  620. unsigned int lfr_dbg = mtk_dbg_get_lfr_dbg_value();
  621. unsigned int lfr_mode = LFR_MODE_BOTH_MODE;
  622. unsigned int lfr_type = 0;
  623. unsigned int lfr_enable = 1;
  624. unsigned int lfr_skip_num = 0;
  625. if (mtk_dsi_is_cmd_mode(&dsi->ddp_comp))
  626. return -1;
  627. //Settings lfr settings to LFR_CON_REG
  628. if (dsi->ext && dsi->ext->params &&
  629. dsi->ext->params->dyn_fps.lfr_minimum_fps != 0 &&
  630. dsi->ext->params->dyn_fps.lfr_enable == 1) {
  631. lfr_skip_num =
  632. (dsi->ext->params->dyn_fps.vact_timing_fps /
  633. dsi->ext->params->dyn_fps.lfr_minimum_fps) - 1;
  634. }
  635. if (lfr_dbg) {
  636. lfr_mode = mtk_dbg_get_lfr_mode_value();
  637. lfr_type = mtk_dbg_get_lfr_type_value();
  638. lfr_enable = mtk_dbg_get_lfr_enable_value();
  639. lfr_skip_num = mtk_dbg_get_lfr_skip_num_value();
  640. }
  641. SET_VAL_MASK(val, mask, lfr_mode, LFR_CON_FLD_REG_LFR_MODE);
  642. SET_VAL_MASK(val, mask, lfr_type, LFR_CON_FLD_REG_LFR_TYPE);
  643. SET_VAL_MASK(val, mask, lfr_enable, LFR_CON_FLD_REG_LFR_EN);
  644. SET_VAL_MASK(val, mask, 0, LFR_CON_FLD_REG_LFR_UPDATE);
  645. SET_VAL_MASK(val, mask, 1, LFR_CON_FLD_REG_LFR_VSE_DIS);
  646. SET_VAL_MASK(val, mask, lfr_skip_num, LFR_CON_FLD_REG_LFR_SKIP_NUM);
  647. if (handle == NULL)
  648. mtk_dsi_mask(dsi, DSI_LFR_CON, mask, val);
  649. else
  650. cmdq_pkt_write(handle, comp->cmdq_base,
  651. comp->regs_pa + DSI_LFR_CON, val, mask);
  652. return 0;
  653. }
  654. static int mtk_dsi_LFR_update(struct mtk_dsi *dsi, struct mtk_ddp_comp *comp,
  655. void *handle)
  656. {
  657. u32 val = 0, mask = 0;
  658. if (mtk_dsi_is_cmd_mode(&dsi->ddp_comp))
  659. return -1;
  660. if (comp == NULL) {
  661. DDPPR_ERR("%s mtk_ddp_comp is null\n", __func__);
  662. return -1;
  663. }
  664. if (handle == NULL) {
  665. DDPPR_ERR("%s cmdq handle is null\n", __func__);
  666. return -1;
  667. }
  668. SET_VAL_MASK(val, mask, 0, LFR_CON_FLD_REG_LFR_UPDATE);
  669. cmdq_pkt_write(handle, comp->cmdq_base,
  670. comp->regs_pa + DSI_LFR_CON, val, mask);
  671. SET_VAL_MASK(val, mask, 1, LFR_CON_FLD_REG_LFR_UPDATE);
  672. cmdq_pkt_write(handle, comp->cmdq_base,
  673. comp->regs_pa + DSI_LFR_CON, val, mask);
  674. return 0;
  675. }
  676. static int mtk_dsi_LFR_status_check(struct mtk_dsi *dsi)
  677. {
  678. u32 dsi_LFR_sta;
  679. u32 dsi_LFR_skip_cnt;
  680. u32 data;
  681. data = readl(dsi->regs + DSI_LFR_STA);
  682. dsi_LFR_sta = REG_FLD_VAL_GET(LFR_STA_FLD_REG_LFR_SKIP_STA, data);
  683. dsi_LFR_skip_cnt = REG_FLD_VAL_GET(LFR_STA_FLD_REG_LFR_SKIP_CNT, data);
  684. DDPINFO("%s dsi_LFR_sta=%d, dsi_LFR_skip_cnt=%d\n",
  685. __func__, dsi_LFR_sta, dsi_LFR_skip_cnt);
  686. return 0;
  687. }
  688. static int mtk_dsi_poweron(struct mtk_dsi *dsi)
  689. {
  690. struct device *dev = dsi->dev;
  691. int ret;
  692. unsigned int data_rate;
  693. unsigned long mipi_tx_rate;
  694. DDPDBG("%s+\n", __func__);
  695. if (++dsi->clk_refcnt != 1)
  696. return 0;
  697. data_rate = mtk_dsi_default_rate(dsi);
  698. mipi_tx_rate = data_rate * 1000000;
  699. /* Store DSI data rate in MHz */
  700. dsi->data_rate = data_rate;
  701. DDPDBG("set mipitx's data rate: %lu Hz\n", mipi_tx_rate);
  702. ret = clk_set_rate(dsi->hs_clk, mipi_tx_rate);
  703. if (ret < 0) {
  704. dev_err(dev, "Failed to set data rate: %d\n", ret);
  705. goto err_refcount;
  706. }
  707. if (dsi->ext) {
  708. if (dsi->ext->params->is_cphy)
  709. mtk_mipi_tx_cphy_lane_config(dsi->phy, dsi->ext);
  710. else
  711. mtk_mipi_tx_dphy_lane_config(dsi->phy, dsi->ext);
  712. }
  713. phy_power_on(dsi->phy);
  714. ret = clk_prepare_enable(dsi->engine_clk);
  715. if (ret < 0) {
  716. dev_err(dev, "Failed to enable engine clock: %d\n", ret);
  717. goto err_phy_power_off;
  718. }
  719. ret = clk_prepare_enable(dsi->digital_clk);
  720. if (ret < 0) {
  721. dev_err(dev, "Failed to enable digital clock: %d\n", ret);
  722. goto err_disable_engine_clk;
  723. }
  724. mtk_dsi_set_LFR(dsi, NULL, NULL);
  725. #if defined(CONFIG_DRM_MTK_SHADOW_REGISTER_SUPPORT)
  726. if (dsi->driver_data->support_shadow) {
  727. /* Enable shadow register and read shadow register */
  728. mtk_dsi_mask(dsi, DSI_SHADOW_DEBUG,
  729. DSI_BYPASS_SHADOW, 0x0);
  730. } else {
  731. /* Bypass shadow register and read shadow register */
  732. mtk_dsi_mask(dsi, DSI_SHADOW_DEBUG,
  733. DSI_BYPASS_SHADOW, DSI_BYPASS_SHADOW);
  734. }
  735. #else
  736. #if defined(CONFIG_MACH_MT6873) || defined(CONFIG_MACH_MT6853) \
  737. || defined(CONFIG_MACH_MT6833)
  738. /* Bypass shadow register and read shadow register */
  739. mtk_dsi_mask(dsi, DSI_SHADOW_DEBUG,
  740. DSI_BYPASS_SHADOW, DSI_BYPASS_SHADOW);
  741. #endif
  742. #endif
  743. DDPDBG("%s-\n", __func__);
  744. return 0;
  745. err_disable_engine_clk:
  746. clk_disable_unprepare(dsi->engine_clk);
  747. err_phy_power_off:
  748. phy_power_off(dsi->phy);
  749. err_refcount:
  750. dsi->clk_refcnt--;
  751. return ret;
  752. }
  753. static bool mtk_dsi_clk_hs_state(struct mtk_dsi *dsi)
  754. {
  755. u32 tmp_reg1;
  756. tmp_reg1 = readl(dsi->regs + DSI_PHY_LCCON);
  757. return ((tmp_reg1 & LC_HS_TX_EN) == 1) ? true : false;
  758. }
  759. static void mtk_dsi_clk_hs_mode(struct mtk_dsi *dsi, bool enter)
  760. {
  761. if (enter && !mtk_dsi_clk_hs_state(dsi))
  762. mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_HS_TX_EN, LC_HS_TX_EN);
  763. else if (!enter && mtk_dsi_clk_hs_state(dsi))
  764. mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_HS_TX_EN, 0);
  765. }
  766. static void mtk_dsi_set_mode(struct mtk_dsi *dsi)
  767. {
  768. u32 vid_mode = CMD_MODE;
  769. if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO) {
  770. if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
  771. vid_mode = BURST_MODE;
  772. else if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
  773. vid_mode = SYNC_PULSE_MODE;
  774. else
  775. vid_mode = SYNC_EVENT_MODE;
  776. }
  777. writel(vid_mode, dsi->regs + DSI_MODE_CTRL);
  778. }
  779. static void mtk_dsi_set_vm_cmd(struct mtk_dsi *dsi)
  780. {
  781. mtk_dsi_mask(dsi, DSI_VM_CMD_CON, VM_CMD_EN, VM_CMD_EN);
  782. mtk_dsi_mask(dsi, DSI_VM_CMD_CON, TS_VFP_EN, TS_VFP_EN);
  783. }
  784. static int mtk_dsi_get_virtual_heigh(struct mtk_dsi *dsi,
  785. struct drm_crtc *crtc)
  786. {
  787. struct mtk_panel_ext *panel_ext = NULL;
  788. struct mtk_crtc_state *state =
  789. to_mtk_crtc_state(crtc->state);
  790. struct drm_display_mode adjusted_mode = state->base.adjusted_mode;
  791. unsigned int virtual_heigh = adjusted_mode.vdisplay;
  792. panel_ext = dsi->ext;
  793. if (panel_ext && panel_ext->funcs
  794. && panel_ext->funcs->get_virtual_heigh)
  795. virtual_heigh = panel_ext->funcs->get_virtual_heigh();
  796. if (!virtual_heigh)
  797. virtual_heigh = crtc->mode.vdisplay;
  798. DDPINFO("%s,virtual_heigh %d\n", __func__, virtual_heigh);
  799. return virtual_heigh;
  800. }
  801. static int mtk_dsi_get_virtual_width(struct mtk_dsi *dsi,
  802. struct drm_crtc *crtc)
  803. {
  804. struct mtk_panel_ext *panel_ext = NULL;
  805. struct mtk_crtc_state *state =
  806. to_mtk_crtc_state(crtc->state);
  807. struct drm_display_mode adjusted_mode = state->base.adjusted_mode;
  808. unsigned int virtual_width = adjusted_mode.hdisplay;
  809. panel_ext = dsi->ext;
  810. if (panel_ext && panel_ext->funcs
  811. && panel_ext->funcs->get_virtual_width)
  812. virtual_width = panel_ext->funcs->get_virtual_width();
  813. if (!virtual_width)
  814. virtual_width = crtc->mode.hdisplay;
  815. DDPINFO("%s,virtual_width %d\n", __func__, virtual_width);
  816. return virtual_width;
  817. }
  818. static void mtk_dsi_ps_control_vact(struct mtk_dsi *dsi)
  819. {
  820. u32 ps_wc, size;
  821. u32 dsi_buf_bpp, val;
  822. u32 value = 0, mask = 0;
  823. u32 width = mtk_dsi_get_virtual_width(dsi, dsi->encoder.crtc);
  824. u32 height = mtk_dsi_get_virtual_heigh(dsi, dsi->encoder.crtc);
  825. struct mtk_panel_ext *ext = mtk_dsi_get_panel_ext(&dsi->ddp_comp);
  826. struct mtk_panel_dsc_params *dsc_params = &ext->params->dsc_params;
  827. if (dsi->format == MIPI_DSI_FMT_RGB565)
  828. dsi_buf_bpp = 2;
  829. else
  830. dsi_buf_bpp = 3;
  831. if (dsc_params->enable == 0) {
  832. ps_wc = width * dsi_buf_bpp;
  833. SET_VAL_MASK(value, mask, ps_wc, DSI_PS_WC);
  834. switch (dsi->format) {
  835. case MIPI_DSI_FMT_RGB888:
  836. SET_VAL_MASK(value, mask, 3, DSI_PS_SEL);
  837. break;
  838. case MIPI_DSI_FMT_RGB666:
  839. SET_VAL_MASK(value, mask, 2, DSI_PS_SEL);
  840. break;
  841. case MIPI_DSI_FMT_RGB666_PACKED:
  842. SET_VAL_MASK(value, mask, 1, DSI_PS_SEL);
  843. break;
  844. case MIPI_DSI_FMT_RGB565:
  845. SET_VAL_MASK(value, mask, 0, DSI_PS_SEL);
  846. break;
  847. }
  848. size = (height << 16) + width;
  849. } else {
  850. ps_wc = (((dsc_params->chunk_size + 2) / 3) * 3);
  851. if (dsc_params->slice_mode == 1)
  852. ps_wc *= 2;
  853. SET_VAL_MASK(value, mask, ps_wc, DSI_PS_WC);
  854. SET_VAL_MASK(value, mask, 5, DSI_PS_SEL);
  855. size = (height << 16) + (ps_wc / 3);
  856. }
  857. writel(height, dsi->regs + DSI_VACT_NL);
  858. val = readl(dsi->regs + DSI_PSCTRL);
  859. val = (val & ~mask) | (value & mask);
  860. writel(val, dsi->regs + DSI_PSCTRL);
  861. #if !defined(CONFIG_MACH_MT6885) && !defined(CONFIG_MACH_MT6873) \
  862. && !defined(CONFIG_MACH_MT6893) && !defined(CONFIG_MACH_MT6853) \
  863. && !defined(CONFIG_MACH_MT6833)
  864. val = vm->hactive * dsi_buf_bpp;
  865. writel(val, dsi->regs + DSI_HSTX_CKL_WC);
  866. #endif
  867. writel(size, dsi->regs + DSI_SIZE_CON);
  868. }
  869. static void mtk_dsi_rxtx_control(struct mtk_dsi *dsi)
  870. {
  871. u32 tmp_reg;
  872. switch (dsi->lanes) {
  873. case 1:
  874. tmp_reg = 1 << 2;
  875. break;
  876. case 2:
  877. tmp_reg = 3 << 2;
  878. break;
  879. case 3:
  880. tmp_reg = 7 << 2;
  881. break;
  882. case 4:
  883. tmp_reg = 0xf << 2;
  884. break;
  885. default:
  886. tmp_reg = 0xf << 2;
  887. break;
  888. }
  889. tmp_reg |= (dsi->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS) << 6;
  890. #if !defined(CONFIG_MACH_MT6885) && !defined(CONFIG_MACH_MT6873) \
  891. && !defined(CONFIG_MACH_MT6893) && !defined(CONFIG_MACH_MT6853) \
  892. && !defined(CONFIG_MACH_MT6833)
  893. tmp_reg |= (dsi->mode_flags & MIPI_DSI_MODE_EOT_PACKET) >> 3;
  894. #endif
  895. writel(tmp_reg, dsi->regs + DSI_TXRX_CTRL);
  896. /* need to config for cmd mode to transmit frame data to DDIC */
  897. writel(DSI_WMEM_CONTI, dsi->regs + DSI_MEM_CONTI);
  898. }
  899. static void mtk_dsi_calc_vdo_timing(struct mtk_dsi *dsi)
  900. {
  901. u32 horizontal_sync_active_byte;
  902. u32 horizontal_backporch_byte;
  903. u32 horizontal_frontporch_byte;
  904. u32 dsi_tmp_buf_bpp;
  905. u32 t_vfp, t_vbp, t_vsa;
  906. u32 t_hfp, t_hbp, t_hsa;
  907. struct mtk_panel_ext *ext = dsi->ext;
  908. struct videomode *vm = &dsi->vm;
  909. struct dynamic_mipi_params *dyn = NULL;
  910. if (ext && ext->params)
  911. dyn = &ext->params->dyn;
  912. t_vfp = (dsi->mipi_hopping_sta) ?
  913. ((dyn && !!dyn->vfp) ?
  914. dyn->vfp : vm->vfront_porch) :
  915. vm->vfront_porch;
  916. t_vbp = (dsi->mipi_hopping_sta) ?
  917. ((dyn && !!dyn->vbp) ?
  918. dyn->vbp : vm->vback_porch) :
  919. vm->vback_porch;
  920. t_vsa = (dsi->mipi_hopping_sta) ?
  921. ((dyn && !!dyn->vsa) ?
  922. dyn->vsa : vm->vsync_len) :
  923. vm->vsync_len;
  924. t_hfp = (dsi->mipi_hopping_sta) ?
  925. ((dyn && !!dyn->hfp) ?
  926. dyn->hfp : vm->hfront_porch) :
  927. vm->hfront_porch;
  928. t_hbp = (dsi->mipi_hopping_sta) ?
  929. ((dyn && !!dyn->hbp) ?
  930. dyn->hbp : vm->hback_porch) :
  931. vm->hback_porch;
  932. t_hsa = (dsi->mipi_hopping_sta) ?
  933. ((dyn && !!dyn->hsa) ?
  934. dyn->hsa : vm->hsync_len) :
  935. vm->hsync_len;
  936. if (dsi->format == MIPI_DSI_FMT_RGB565)
  937. dsi_tmp_buf_bpp = 2;
  938. else
  939. dsi_tmp_buf_bpp = 3;
  940. dsi->ext = find_panel_ext(dsi->panel);
  941. if (!dsi->ext)
  942. return;
  943. if (dsi->ext->params->is_cphy) {
  944. if (t_hsa * dsi_tmp_buf_bpp < 10 * dsi->lanes + 26 + 5)
  945. horizontal_sync_active_byte = 4;
  946. else
  947. horizontal_sync_active_byte = ALIGN_TO(
  948. t_hsa * dsi_tmp_buf_bpp -
  949. 10 * dsi->lanes - 26, 2);
  950. if (t_hbp * dsi_tmp_buf_bpp < 12 * dsi->lanes + 26 + 5)
  951. horizontal_backporch_byte = 4;
  952. else
  953. horizontal_backporch_byte = ALIGN_TO(
  954. t_hbp * dsi_tmp_buf_bpp -
  955. 12 * dsi->lanes - 26, 2);
  956. if (t_hfp * dsi_tmp_buf_bpp < 8 * dsi->lanes + 28 +
  957. 2 * dsi->data_phy_cycle * dsi->lanes + 9)
  958. horizontal_frontporch_byte = 8;
  959. else if ((t_hfp * dsi_tmp_buf_bpp > 8 * dsi->lanes + 28 +
  960. 2 * dsi->data_phy_cycle * dsi->lanes + 8) &&
  961. (t_hfp * dsi_tmp_buf_bpp < 8 * dsi->lanes + 28 +
  962. 2 * dsi->data_phy_cycle * dsi->lanes +
  963. 2 * (32 + 1) * dsi->lanes - 6 * dsi->lanes - 12))
  964. horizontal_frontporch_byte = 2*(32 + 1)*dsi->lanes -
  965. 6*dsi->lanes - 12;
  966. else
  967. horizontal_frontporch_byte = t_hfp * dsi_tmp_buf_bpp -
  968. 8 * dsi->lanes - 28 -
  969. 2 * dsi->data_phy_cycle * dsi->lanes;
  970. } else {
  971. if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) {
  972. horizontal_sync_active_byte =
  973. ALIGN_TO((t_hsa * dsi_tmp_buf_bpp - 10), 4);
  974. horizontal_backporch_byte =
  975. ALIGN_TO((t_hbp * dsi_tmp_buf_bpp - 10), 4);
  976. } else {
  977. horizontal_sync_active_byte =
  978. ALIGN_TO((t_hsa * dsi_tmp_buf_bpp - 4), 4);
  979. horizontal_backporch_byte =
  980. ALIGN_TO(((t_hbp + t_hsa) * dsi_tmp_buf_bpp -
  981. 10), 4);
  982. }
  983. horizontal_frontporch_byte =
  984. ALIGN_TO((t_hfp * dsi_tmp_buf_bpp - 12), 4);
  985. }
  986. dsi->vfp = t_vfp;
  987. dsi->vbp = t_vbp;
  988. dsi->vsa = t_vsa;
  989. dsi->hfp_byte = horizontal_frontporch_byte;
  990. dsi->hbp_byte = horizontal_backporch_byte;
  991. dsi->hsa_byte = horizontal_sync_active_byte;
  992. }
  993. static void mtk_dsi_config_vdo_timing(struct mtk_dsi *dsi)
  994. {
  995. struct videomode *vm = &dsi->vm;
  996. unsigned int vact = vm->vactive;
  997. writel(dsi->vsa, dsi->regs + DSI_VSA_NL);
  998. writel(dsi->vbp, dsi->regs + DSI_VBP_NL);
  999. writel(dsi->vfp, dsi->regs + DSI_VFP_NL);
  1000. vact = mtk_dsi_get_virtual_heigh(dsi, dsi->encoder.crtc);
  1001. writel(vact, dsi->regs + DSI_VACT_NL);
  1002. writel(dsi->hsa_byte, dsi->regs + DSI_HSA_WC);
  1003. writel(dsi->hbp_byte, dsi->regs + DSI_HBP_WC);
  1004. writel(dsi->hfp_byte, dsi->regs + DSI_HFP_WC);
  1005. }
  1006. static void mtk_dsi_start(struct mtk_dsi *dsi)
  1007. {
  1008. writel(0, dsi->regs + DSI_START);
  1009. writel(1, dsi->regs + DSI_START);
  1010. }
  1011. static void mtk_dsi_vm_start(struct mtk_dsi *dsi)
  1012. {
  1013. mtk_dsi_mask(dsi, DSI_START, VM_CMD_START, 0);
  1014. mtk_dsi_mask(dsi, DSI_START, VM_CMD_START, VM_CMD_START);
  1015. }
  1016. static void mtk_dsi_stop(struct mtk_dsi *dsi)
  1017. {
  1018. writel(0, dsi->regs + DSI_START);
  1019. writel(0, dsi->regs + DSI_INTEN);
  1020. writel(0, dsi->regs + DSI_INTSTA);
  1021. }
  1022. static void mtk_dsi_set_interrupt_enable(struct mtk_dsi *dsi)
  1023. {
  1024. u32 inten;
  1025. inten = BUFFER_UNDERRUN_INT_FLAG | INP_UNFINISH_INT_EN;
  1026. if (!mtk_dsi_is_cmd_mode(&dsi->ddp_comp))
  1027. inten |= FRAME_DONE_INT_FLAG;
  1028. else
  1029. inten |= TE_RDY_INT_FLAG;
  1030. writel(inten, dsi->regs + DSI_INTEN);
  1031. }
  1032. static void mtk_dsi_irq_data_set(struct mtk_dsi *dsi, u32 irq_bit)
  1033. {
  1034. dsi->irq_data |= irq_bit;
  1035. }
  1036. static void mtk_dsi_irq_data_clear(struct mtk_dsi *dsi, u32 irq_bit)
  1037. {
  1038. dsi->irq_data &= ~irq_bit;
  1039. }
  1040. static s32 mtk_dsi_wait_for_irq_done(struct mtk_dsi *dsi, u32 irq_flag,
  1041. unsigned int timeout)
  1042. {
  1043. s32 ret = 0;
  1044. unsigned long jiffies = msecs_to_jiffies(timeout);
  1045. ret = wait_event_interruptible_timeout(
  1046. dsi->irq_wait_queue, dsi->irq_data & irq_flag, jiffies);
  1047. if (ret == 0) {
  1048. DRM_WARN("Wait DSI IRQ(0x%08x) Timeout\n", irq_flag);
  1049. mtk_dsi_enable(dsi);
  1050. mtk_dsi_reset_engine(dsi);
  1051. }
  1052. return ret;
  1053. }
  1054. static void mtk_dsi_cmdq_poll(struct mtk_ddp_comp *comp,
  1055. struct cmdq_pkt *handle, unsigned int reg,
  1056. unsigned int val, unsigned int mask)
  1057. {
  1058. struct mtk_drm_crtc *mtk_crtc = comp->mtk_crtc;
  1059. struct cmdq_client *client = mtk_crtc->gce_obj.client[CLIENT_DSI_CFG];
  1060. if (handle == NULL)
  1061. DDPPR_ERR("%s no cmdq handle\n", __func__);
  1062. #if 0
  1063. cmdq_pkt_poll_reg(handle, val, comp->cmdq_subsys, reg & 0xFFFF, mask);
  1064. #else
  1065. if (handle->cl == (void *)client) {
  1066. cmdq_pkt_poll_timeout(handle, val, SUBSYS_NO_SUPPORT,
  1067. reg, mask, 0xFFFF,
  1068. CMDQ_GPR_R14);
  1069. } else {
  1070. cmdq_pkt_poll_timeout(handle, val, SUBSYS_NO_SUPPORT,
  1071. reg, mask, 0xFFFF,
  1072. CMDQ_GPR_R07);
  1073. }
  1074. #endif
  1075. }
  1076. static s32 mtk_dsi_poll_for_idle(struct mtk_dsi *dsi, struct cmdq_pkt *handle)
  1077. {
  1078. unsigned int loop_cnt = 0;
  1079. s32 tmp;
  1080. if (handle) {
  1081. mtk_dsi_cmdq_poll(&dsi->ddp_comp, handle,
  1082. dsi->ddp_comp.regs_pa + DSI_INTSTA, 0,
  1083. 0x80000000);
  1084. return 1;
  1085. }
  1086. while (loop_cnt < 100 * 1000) {
  1087. tmp = readl(dsi->regs + DSI_INTSTA);
  1088. if (!(tmp & DSI_BUSY))
  1089. return 1;
  1090. loop_cnt++;
  1091. udelay(1);
  1092. }
  1093. DDPPR_ERR("%s timeout\n", __func__);
  1094. return 0;
  1095. }
  1096. static s32 mtk_dsi_wait_idle(struct mtk_dsi *dsi, u32 irq_flag,
  1097. unsigned int timeout, struct cmdq_pkt *handle)
  1098. {
  1099. if (dsi->driver_data->poll_for_idle)
  1100. return dsi->driver_data->poll_for_idle(dsi, handle);
  1101. return mtk_dsi_wait_for_irq_done(dsi, irq_flag, timeout);
  1102. }
  1103. static void init_dsi_wq(struct mtk_dsi *dsi)
  1104. {
  1105. init_waitqueue_head(&dsi->enter_ulps_done.wq);
  1106. init_waitqueue_head(&dsi->exit_ulps_done.wq);
  1107. init_waitqueue_head(&dsi->te_rdy.wq);
  1108. init_waitqueue_head(&dsi->frame_done.wq);
  1109. atomic_set(&dsi->enter_ulps_done.condition, 0);
  1110. atomic_set(&dsi->exit_ulps_done.condition, 0);
  1111. atomic_set(&dsi->te_rdy.condition, 0);
  1112. atomic_set(&dsi->frame_done.condition, 0);
  1113. }
  1114. static void reset_dsi_wq(struct t_condition_wq *wq)
  1115. {
  1116. atomic_set(&wq->condition, 0);
  1117. }
  1118. static void wakeup_dsi_wq(struct t_condition_wq *wq)
  1119. {
  1120. atomic_set(&wq->condition, 1);
  1121. wake_up(&wq->wq);
  1122. }
  1123. static int wait_dsi_wq(struct t_condition_wq *wq, int timeout)
  1124. {
  1125. int ret;
  1126. ret = wait_event_timeout(wq->wq, atomic_read(&wq->condition), timeout);
  1127. atomic_set(&wq->condition, 0);
  1128. return ret;
  1129. }
  1130. static irqreturn_t mtk_dsi_irq_status(int irq, void *dev_id)
  1131. {
  1132. struct mtk_dsi *dsi = dev_id;
  1133. struct mtk_drm_crtc *mtk_crtc;
  1134. struct mtk_panel_ext *panel_ext;
  1135. u32 status;
  1136. static unsigned int dsi_underrun_trigger = 1;
  1137. unsigned int ret = 0;
  1138. #if defined(CONFIG_MACH_MT6873) || defined(CONFIG_MACH_MT6853) \
  1139. || defined(CONFIG_MACH_MT6833)
  1140. static DEFINE_RATELIMIT_STATE(ioctl_ratelimit, 1 * HZ, 20);
  1141. #endif
  1142. bool doze_enabled = 0;
  1143. unsigned int doze_wait = 0;
  1144. static unsigned int cnt;
  1145. if (mtk_drm_top_clk_isr_get("dsi_irq") == false) {
  1146. DDPIRQ("%s, top clk off\n", __func__);
  1147. return IRQ_NONE;
  1148. }
  1149. status = readl(dsi->regs + DSI_INTSTA);
  1150. if (!status) {
  1151. ret = IRQ_NONE;
  1152. goto out;
  1153. }
  1154. DRM_MMP_MARK(IRQ, irq, status);
  1155. if (dsi->ddp_comp.id == DDP_COMPONENT_DSI0)
  1156. DRM_MMP_MARK(dsi0, status, 0);
  1157. else if (dsi->ddp_comp.id == DDP_COMPONENT_DSI1)
  1158. DRM_MMP_MARK(dsi1, status, 0);
  1159. DDPIRQ("%s irq, val:0x%x\n", mtk_dump_comp_str(&dsi->ddp_comp), status);
  1160. /*
  1161. * rd_rdy don't clear and wait for ESD &
  1162. * Read LCM will clear the bit.
  1163. */
  1164. /* do not clear vm command done */
  1165. status &= 0xffde;
  1166. if (status) {
  1167. writel(~status, dsi->regs + DSI_INTSTA);
  1168. if (status & BUFFER_UNDERRUN_INT_FLAG) {
  1169. struct mtk_drm_private *priv = NULL;
  1170. mtk_crtc = dsi->ddp_comp.mtk_crtc;
  1171. if (mtk_crtc && mtk_crtc->base.dev)
  1172. priv = mtk_crtc->base.dev->dev_private;
  1173. if (priv && mtk_drm_helper_get_opt(priv->helper_opt,
  1174. MTK_DRM_OPT_DSI_UNDERRUN_AEE)) {
  1175. if (dsi_underrun_trigger == 1) {
  1176. DDPAEE(
  1177. "[IRQ] %s:buffer underrun,sys_time=%u\n",
  1178. mtk_dump_comp_str(
  1179. &dsi->ddp_comp),
  1180. (u32)arch_counter_get_cntvct());
  1181. if (dsi->encoder.crtc) {
  1182. mtk_drm_crtc_analysis(
  1183. dsi->encoder.crtc);
  1184. mtk_drm_crtc_dump(
  1185. dsi->encoder.crtc);
  1186. }
  1187. dsi_underrun_trigger = 0;
  1188. }
  1189. }
  1190. #if defined(CONFIG_MACH_MT6873) || defined(CONFIG_MACH_MT6853) \
  1191. || defined(CONFIG_MACH_MT6833)
  1192. mtk_dprec_logger_pr(DPREC_LOGGER_ERROR,
  1193. "[IRQ] %s: buffer underrun\n",
  1194. mtk_dump_comp_str(&dsi->ddp_comp));
  1195. if (__ratelimit(&ioctl_ratelimit))
  1196. pr_err(pr_fmt("[IRQ] %s: buffer underrun\n"),
  1197. mtk_dump_comp_str(&dsi->ddp_comp));
  1198. #else
  1199. DDPPR_ERR("[IRQ] %s: buffer underrun\n",
  1200. mtk_dump_comp_str(&dsi->ddp_comp));
  1201. #endif
  1202. if (dsi_underrun_trigger == 1 && dsi->encoder.crtc) {
  1203. mtk_drm_crtc_analysis(dsi->encoder.crtc);
  1204. mtk_drm_crtc_dump(dsi->encoder.crtc);
  1205. dsi_underrun_trigger = 0;
  1206. }
  1207. }
  1208. if (status & INP_UNFINISH_INT_EN)
  1209. DDPPR_ERR("[IRQ] %s: input relay unfinish\n",
  1210. mtk_dump_comp_str(&dsi->ddp_comp));
  1211. if (status & SLEEPOUT_DONE_INT_FLAG)
  1212. wakeup_dsi_wq(&dsi->exit_ulps_done);
  1213. if (status & SLEEPIN_ULPS_DONE_INT_FLAG)
  1214. wakeup_dsi_wq(&dsi->enter_ulps_done);
  1215. if (status & TE_RDY_INT_FLAG) {
  1216. struct mtk_drm_private *priv = NULL;
  1217. if (dsi->ddp_comp.id == DDP_COMPONENT_DSI0) {
  1218. unsigned long long ext_te_time = sched_clock();
  1219. lcm_fps_ctx_update(ext_te_time, 0, 0);
  1220. }
  1221. mtk_crtc = dsi->ddp_comp.mtk_crtc;
  1222. if (mtk_crtc && mtk_crtc->base.dev)
  1223. priv = mtk_crtc->base.dev->dev_private;
  1224. if (priv && mtk_drm_helper_get_opt(priv->helper_opt,
  1225. MTK_DRM_OPT_HBM))
  1226. wakeup_dsi_wq(&dsi->te_rdy);
  1227. if (mtk_dsi_is_cmd_mode(&dsi->ddp_comp) &&
  1228. mtk_crtc && mtk_crtc->vblank_en) {
  1229. panel_ext = dsi->ext;
  1230. if (dsi->encoder.crtc)
  1231. doze_enabled = mtk_dsi_doze_state(dsi);
  1232. if (panel_ext->params->doze_delay &&
  1233. doze_enabled) {
  1234. doze_wait =
  1235. panel_ext->params->doze_delay;
  1236. if (cnt % doze_wait == 0) {
  1237. mtk_crtc_vblank_irq(
  1238. &mtk_crtc->base);
  1239. cnt = 0;
  1240. }
  1241. cnt++;
  1242. } else
  1243. mtk_crtc_vblank_irq(&mtk_crtc->base);
  1244. }
  1245. }
  1246. if (status & FRAME_DONE_INT_FLAG) {
  1247. struct mtk_drm_private *priv = NULL;
  1248. mtk_crtc = dsi->ddp_comp.mtk_crtc;
  1249. if (mtk_crtc && mtk_crtc->base.dev)
  1250. priv = mtk_crtc->base.dev->dev_private;
  1251. if (priv && mtk_drm_helper_get_opt(priv->helper_opt,
  1252. MTK_DRM_OPT_HBM))
  1253. wakeup_dsi_wq(&dsi->frame_done);
  1254. if (!mtk_dsi_is_cmd_mode(&dsi->ddp_comp) &&
  1255. mtk_crtc && mtk_crtc->vblank_en)
  1256. mtk_crtc_vblank_irq(&mtk_crtc->base);
  1257. }
  1258. }
  1259. ret = IRQ_HANDLED;
  1260. out:
  1261. mtk_drm_top_clk_isr_put("dsi_irq");
  1262. return ret;
  1263. }
  1264. static irqreturn_t mtk_dsi_irq(int irq, void *dev_id)
  1265. {
  1266. struct mtk_dsi *dsi = dev_id;
  1267. u32 status, tmp;
  1268. u32 flag = LPRX_RD_RDY_INT_FLAG | CMD_DONE_INT_FLAG | VM_DONE_INT_FLAG;
  1269. status = readl(dsi->regs + DSI_INTSTA) & flag;
  1270. if (status) {
  1271. do {
  1272. mtk_dsi_mask(dsi, DSI_RACK, RACK, RACK);
  1273. tmp = readl(dsi->regs + DSI_INTSTA);
  1274. } while (tmp & DSI_BUSY);
  1275. mtk_dsi_mask(dsi, DSI_INTSTA, status, 0);
  1276. mtk_dsi_irq_data_set(dsi, status);
  1277. wake_up_interruptible(&dsi->irq_wait_queue);
  1278. }
  1279. return IRQ_HANDLED;
  1280. }
  1281. static void mtk_dsi_poweroff(struct mtk_dsi *dsi)
  1282. {
  1283. DDPDBG("%s +\n", __func__);
  1284. if (dsi->clk_refcnt == 0) {
  1285. DDPAEE("%s:%d, invalid cnt:%d\n",
  1286. __func__, __LINE__,
  1287. dsi->clk_refcnt);
  1288. return;
  1289. }
  1290. if (--dsi->clk_refcnt != 0)
  1291. return;
  1292. clk_disable_unprepare(dsi->engine_clk);
  1293. clk_disable_unprepare(dsi->digital_clk);
  1294. phy_power_off(dsi->phy);
  1295. DDPDBG("%s -\n", __func__);
  1296. }
  1297. static void mtk_dsi_enter_ulps(struct mtk_dsi *dsi)
  1298. {
  1299. unsigned int ret = 0;
  1300. /* reset enter_ulps_done before waiting */
  1301. reset_dsi_wq(&dsi->enter_ulps_done);
  1302. /* config and trigger enter ulps mode */
  1303. mtk_dsi_mask(dsi, DSI_INTEN, SLEEPIN_ULPS_DONE_INT_FLAG,
  1304. SLEEPIN_ULPS_DONE_INT_FLAG);
  1305. mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_HS_TX_EN, 0);
  1306. mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LDX_ULPM_AS_L0, LDX_ULPM_AS_L0);
  1307. mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_ULPM_EN, LD0_ULPM_EN);
  1308. mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_ULPM_EN, LC_ULPM_EN);
  1309. /* wait enter_ulps_done */
  1310. ret = wait_dsi_wq(&dsi->enter_ulps_done, 2 * HZ);
  1311. if (ret)
  1312. DDPDBG("%s success\n", __func__);
  1313. else {
  1314. /* IRQ maybe be un-expectedly disabled for long time,
  1315. * which makes false alarm timeout...
  1316. */
  1317. u32 status = readl(dsi->regs + DSI_INTSTA);
  1318. if (status & SLEEPIN_ULPS_DONE_INT_FLAG)
  1319. DDPPR_ERR("%s success but IRQ is blocked\n",
  1320. __func__);
  1321. else {
  1322. mtk_dsi_dump(&dsi->ddp_comp);
  1323. DDPAEE("%s fail\n", __func__);
  1324. }
  1325. }
  1326. /* reset related setting */
  1327. mtk_dsi_mask(dsi, DSI_INTEN, SLEEPIN_ULPS_DONE_INT_FLAG, 0);
  1328. mtk_mipi_tx_pre_oe_config(dsi->phy, 0);
  1329. mtk_mipi_tx_sw_control_en(dsi->phy, 1);
  1330. /* set lane num = 0 */
  1331. mtk_dsi_mask(dsi, DSI_TXRX_CTRL, LANE_NUM, 0);
  1332. }
  1333. static void mtk_dsi_exit_ulps(struct mtk_dsi *dsi)
  1334. {
  1335. int wake_up_prd = (dsi->data_rate * 1000) / (1024 * 8) + 1;
  1336. unsigned int ret = 0;
  1337. mtk_dsi_phy_reset(dsi);
  1338. /* set pre oe */
  1339. mtk_mipi_tx_pre_oe_config(dsi->phy, 1);
  1340. /* reset exit_ulps_done before waiting */
  1341. reset_dsi_wq(&dsi->exit_ulps_done);
  1342. mtk_dsi_mask(dsi, DSI_INTEN, SLEEPOUT_DONE_INT_FLAG,
  1343. SLEEPOUT_DONE_INT_FLAG);
  1344. mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LDX_ULPM_AS_L0, LDX_ULPM_AS_L0);
  1345. mtk_dsi_mask(dsi, DSI_MODE_CTRL, SLEEP_MODE, SLEEP_MODE);
  1346. mtk_dsi_mask(dsi, DSI_TIME_CON0, 0xffff, wake_up_prd);
  1347. /* free sw control */
  1348. mtk_mipi_tx_sw_control_en(dsi->phy, 0);
  1349. mtk_dsi_mask(dsi, DSI_START, SLEEPOUT_START, 0);
  1350. mtk_dsi_mask(dsi, DSI_START, SLEEPOUT_START, SLEEPOUT_START);
  1351. /* wait exit_ulps_done */
  1352. ret = wait_dsi_wq(&dsi->exit_ulps_done, 2 * HZ);
  1353. if (ret)
  1354. DDPDBG("%s success\n", __func__);
  1355. else {
  1356. /* IRQ maybe be un-expectedly disabled for long time,
  1357. * which makes false alarm timeout...
  1358. */
  1359. u32 status = readl(dsi->regs + DSI_INTSTA);
  1360. if (status & SLEEPOUT_DONE_INT_FLAG)
  1361. DDPPR_ERR("%s success but IRQ is blocked\n",
  1362. __func__);
  1363. else {
  1364. mtk_dsi_dump(&dsi->ddp_comp);
  1365. DDPAEE("%s fail\n", __func__);
  1366. }
  1367. }
  1368. /* reset related setting */
  1369. mtk_dsi_mask(dsi, DSI_INTEN, SLEEPOUT_DONE_INT_FLAG, 0);
  1370. mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LDX_ULPM_AS_L0, 0);
  1371. mtk_dsi_mask(dsi, DSI_MODE_CTRL, SLEEP_MODE, 0);
  1372. mtk_dsi_mask(dsi, DSI_START, SLEEPOUT_START, 0);
  1373. /* do DSI reset after exit ULPS */
  1374. mtk_dsi_reset_engine(dsi);
  1375. }
  1376. static int mtk_dsi_stop_vdo_mode(struct mtk_dsi *dsi, void *handle);
  1377. static void mipi_dsi_dcs_write_gce2(struct mtk_dsi *dsi, struct cmdq_pkt *dummy,
  1378. const void *data, size_t len);
  1379. static void mtk_output_en_doze_switch(struct mtk_dsi *dsi)
  1380. {
  1381. bool doze_enabled = mtk_dsi_doze_state(dsi);
  1382. struct mtk_panel_funcs *panel_funcs;
  1383. if (!dsi->output_en)
  1384. return;
  1385. DDPINFO("%s doze_enabled state change %d->%d\n", __func__,
  1386. dsi->doze_enabled, doze_enabled);
  1387. if (dsi->ext && dsi->ext->funcs) {
  1388. panel_funcs = dsi->ext->funcs;
  1389. } else {
  1390. DDPINFO("%s, AOD should have use panel extension function\n",
  1391. __func__);
  1392. return;
  1393. }
  1394. /* Change LCM Doze mode */
  1395. if (doze_enabled && panel_funcs->doze_enable_start)
  1396. panel_funcs->doze_enable_start(dsi->panel, dsi,
  1397. mipi_dsi_dcs_write_gce2, NULL);
  1398. else if (!doze_enabled && panel_funcs->doze_disable)
  1399. panel_funcs->doze_disable(dsi->panel, dsi,
  1400. mipi_dsi_dcs_write_gce2, NULL);
  1401. /* Display mode switch */
  1402. if (panel_funcs->doze_get_mode_flags) {
  1403. if (!mtk_dsi_is_cmd_mode(&dsi->ddp_comp))
  1404. mtk_dsi_stop_vdo_mode(dsi, NULL);
  1405. /* set DSI into ULPS mode */
  1406. mtk_dsi_reset_engine(dsi);
  1407. dsi->mode_flags =
  1408. panel_funcs->doze_get_mode_flags(
  1409. dsi->panel, doze_enabled);
  1410. if (mtk_dsi_is_cmd_mode(&dsi->ddp_comp))
  1411. writel(0x0001023c, dsi->regs + DSI_TXRX_CTRL);
  1412. mtk_dsi_set_mode(dsi);
  1413. mtk_dsi_clk_hs_mode(dsi, 1);
  1414. /* Update RDMA golden setting after switch */
  1415. {
  1416. struct drm_crtc *crtc = dsi->encoder.crtc;
  1417. struct mtk_drm_crtc *mtk_crtc =
  1418. to_mtk_crtc(dsi->encoder.crtc);
  1419. unsigned int i, j;
  1420. struct cmdq_pkt *handle;
  1421. struct mtk_ddp_comp *comp;
  1422. struct mtk_ddp_config cfg;
  1423. mtk_crtc_pkt_create(&handle, &mtk_crtc->base,
  1424. mtk_crtc->gce_obj.client[CLIENT_CFG]);
  1425. cfg.w = crtc->state->adjusted_mode.hdisplay;
  1426. cfg.h = crtc->state->adjusted_mode.vdisplay;
  1427. cfg.vrefresh = crtc->state->adjusted_mode.vrefresh;
  1428. cfg.bpc = mtk_crtc->bpc;
  1429. cfg.p_golden_setting_context =
  1430. __get_golden_setting_context(mtk_crtc);
  1431. for_each_comp_in_cur_crtc_path(comp, mtk_crtc, i, j)
  1432. mtk_ddp_comp_io_cmd(comp, handle,
  1433. MTK_IO_CMD_RDMA_GOLDEN_SETTING, &cfg);
  1434. cmdq_pkt_flush(handle);
  1435. cmdq_pkt_destroy(handle);
  1436. }
  1437. if (!mtk_dsi_is_cmd_mode(&dsi->ddp_comp)) {
  1438. mtk_dsi_set_vm_cmd(dsi);
  1439. mtk_dsi_calc_vdo_timing(dsi);
  1440. mtk_dsi_config_vdo_timing(dsi);
  1441. mtk_dsi_start(dsi);
  1442. }
  1443. }
  1444. if (doze_enabled && panel_funcs->doze_enable)
  1445. panel_funcs->doze_enable(dsi->panel, dsi,
  1446. mipi_dsi_dcs_write_gce2, NULL);
  1447. if (doze_enabled && panel_funcs->doze_area)
  1448. panel_funcs->doze_area(dsi->panel, dsi,
  1449. mipi_dsi_dcs_write_gce2, NULL);
  1450. if (panel_funcs->doze_post_disp_on)
  1451. panel_funcs->doze_post_disp_on(dsi->panel,
  1452. dsi, mipi_dsi_dcs_write_gce2, NULL);
  1453. dsi->doze_enabled = doze_enabled;
  1454. }
  1455. static void mtk_output_dsi_enable(struct mtk_dsi *dsi,
  1456. int force_lcm_update)
  1457. {
  1458. int ret;
  1459. struct mtk_panel_ext *ext = dsi->ext;
  1460. bool new_doze_state = mtk_dsi_doze_state(dsi);
  1461. struct drm_crtc *crtc = dsi->encoder.crtc;
  1462. struct mtk_crtc_state *mtk_state = to_mtk_crtc_state(crtc->state);
  1463. unsigned int mode_id = mtk_state->prop_val[CRTC_PROP_DISP_MODE_IDX];
  1464. DDPINFO("%s +\n", __func__);
  1465. if (dsi->output_en) {
  1466. if (mtk_dsi_doze_status_change(dsi))
  1467. mtk_output_en_doze_switch(dsi);
  1468. else
  1469. DDPINFO("dsi is initialized\n");
  1470. return;
  1471. }
  1472. ret = mtk_dsi_poweron(dsi);
  1473. if (ret < 0) {
  1474. DDPPR_ERR("failed to power on dsi\n");
  1475. return;
  1476. }
  1477. mtk_dsi_enable(dsi);
  1478. mtk_dsi_phy_timconfig(dsi, NULL);
  1479. mtk_dsi_rxtx_control(dsi);
  1480. mtk_dsi_ps_control_vact(dsi);
  1481. if (!mtk_dsi_is_cmd_mode(&dsi->ddp_comp)) {
  1482. mtk_dsi_set_vm_cmd(dsi);
  1483. mtk_dsi_calc_vdo_timing(dsi);
  1484. mtk_dsi_config_vdo_timing(dsi);
  1485. }
  1486. mtk_dsi_set_interrupt_enable(dsi);
  1487. mtk_dsi_exit_ulps(dsi);
  1488. mtk_dsi_clk_hs_mode(dsi, 0);
  1489. if (dsi->panel) {
  1490. if ((!dsi->doze_enabled || force_lcm_update)
  1491. && drm_panel_prepare(dsi->panel)) {
  1492. DDPPR_ERR("failed to prepare the panel\n");
  1493. return;
  1494. }
  1495. /* add for ESD recovery */
  1496. if (mtk_dsi_is_cmd_mode(&dsi->ddp_comp) && mode_id != 0) {
  1497. if (dsi->ext && dsi->ext->funcs &&
  1498. dsi->ext->funcs->mode_switch) {
  1499. DDPMSG("%s do lcm mode_switch to %u\n",
  1500. __func__, mode_id);
  1501. dsi->ext->funcs->mode_switch(dsi->panel, 0,
  1502. mode_id, AFTER_DSI_POWERON);
  1503. }
  1504. }
  1505. if (new_doze_state && !dsi->doze_enabled) {
  1506. if (ext && ext->funcs &&
  1507. ext->funcs->doze_enable_start)
  1508. ext->funcs->doze_enable_start(dsi->panel, dsi,
  1509. mipi_dsi_dcs_write_gce2, NULL);
  1510. if (ext && ext->funcs
  1511. && ext->funcs->doze_enable)
  1512. ext->funcs->doze_enable(dsi->panel, dsi,
  1513. mipi_dsi_dcs_write_gce2, NULL);
  1514. if (ext && ext->funcs
  1515. && ext->funcs->doze_area)
  1516. ext->funcs->doze_area(dsi->panel, dsi,
  1517. mipi_dsi_dcs_write_gce2, NULL);
  1518. }
  1519. if (!new_doze_state && dsi->doze_enabled) {
  1520. if (ext && ext->funcs
  1521. && ext->funcs->doze_disable)
  1522. ext->funcs->doze_disable(dsi->panel, dsi,
  1523. mipi_dsi_dcs_write_gce2, NULL);
  1524. }
  1525. }
  1526. /*
  1527. * TODO: It's a temp workaround for cmd mode. When set the EXT_TE_EN bit
  1528. * before sending DSI cmd. System would hang. So move the bit control
  1529. * after
  1530. * lcm initialize.
  1531. */
  1532. if (mtk_dsi_is_cmd_mode(&dsi->ddp_comp))
  1533. writel(0x0001023c, dsi->regs + DSI_TXRX_CTRL);
  1534. mtk_dsi_set_mode(dsi);
  1535. mtk_dsi_clk_hs_mode(dsi, 1);
  1536. if (!mtk_dsi_is_cmd_mode(&dsi->ddp_comp))
  1537. mtk_dsi_start(dsi);
  1538. if (dsi->panel) {
  1539. if (drm_panel_enable(dsi->panel)) {
  1540. DDPPR_ERR("failed to enable the panel\n");
  1541. goto err_dsi_power_off;
  1542. }
  1543. /* Suspend to Doze */
  1544. if (mtk_dsi_doze_status_change(dsi)) {
  1545. /* We use doze_get_mode_flags to determine if
  1546. * there has CV switch in Doze mode.
  1547. */
  1548. if (ext && ext->funcs
  1549. && ext->funcs->doze_post_disp_on
  1550. && ext->funcs->doze_get_mode_flags)
  1551. ext->funcs->doze_post_disp_on(dsi->panel,
  1552. dsi, mipi_dsi_dcs_write_gce2, NULL);
  1553. }
  1554. }
  1555. DDPINFO("%s -\n", __func__);
  1556. dsi->output_en = true;
  1557. dsi->doze_enabled = new_doze_state;
  1558. return;
  1559. err_dsi_power_off:
  1560. mtk_dsi_stop(dsi);
  1561. mtk_dsi_poweroff(dsi);
  1562. }
  1563. static int mtk_dsi_stop_vdo_mode(struct mtk_dsi *dsi, void *handle);
  1564. static int mtk_dsi_wait_cmd_frame_done(struct mtk_dsi *dsi,
  1565. int force_lcm_update)
  1566. {
  1567. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(dsi->encoder.crtc);
  1568. struct cmdq_pkt *handle;
  1569. bool new_doze_state = mtk_dsi_doze_state(dsi);
  1570. mtk_crtc_pkt_create(&handle,
  1571. &mtk_crtc->base,
  1572. mtk_crtc->gce_obj.client[CLIENT_CFG]);
  1573. /* wait frame done */
  1574. cmdq_pkt_wait_no_clear(handle,
  1575. mtk_crtc->gce_obj.event[EVENT_STREAM_EOF]);
  1576. /* When system ready to go to Doze suspend stage, it has to
  1577. * update the latest image before entering it to make sure display
  1578. * correctly. Since it's hard to know how many frame config GCE
  1579. * commands are there in the waiting queue, so here we force
  1580. * frame updating and wait for the latest frame done.
  1581. */
  1582. if (new_doze_state && !force_lcm_update) {
  1583. cmdq_pkt_set_event(handle,
  1584. mtk_crtc->gce_obj.event[EVENT_STREAM_DIRTY]);
  1585. cmdq_pkt_wait_no_clear(handle,
  1586. mtk_crtc->gce_obj.event[EVENT_CMD_EOF]);
  1587. }
  1588. cmdq_pkt_clear_event(
  1589. handle,
  1590. mtk_crtc->gce_obj.event[EVENT_STREAM_BLOCK]);
  1591. cmdq_pkt_flush(handle);
  1592. cmdq_pkt_destroy(handle);
  1593. return 0;
  1594. }
  1595. static void mtk_output_dsi_disable(struct mtk_dsi *dsi,
  1596. int force_lcm_update)
  1597. {
  1598. bool new_doze_state = mtk_dsi_doze_state(dsi);
  1599. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(dsi->encoder.crtc);
  1600. DDPINFO("%s+ doze_enabled:%d\n", __func__, new_doze_state);
  1601. if (!dsi->output_en)
  1602. return;
  1603. mtk_drm_crtc_wait_blank(mtk_crtc);
  1604. /* 1. If not doze mode, turn off backlight */
  1605. if (dsi->panel && (!new_doze_state || force_lcm_update)) {
  1606. if (drm_panel_disable(dsi->panel)) {
  1607. DRM_ERROR("failed to disable the panel\n");
  1608. return;
  1609. }
  1610. }
  1611. /* 2. If VDO mode, stop it and set to CMD mode */
  1612. if (!mtk_dsi_is_cmd_mode(&dsi->ddp_comp))
  1613. mtk_dsi_stop_vdo_mode(dsi, NULL);
  1614. else
  1615. mtk_dsi_wait_cmd_frame_done(dsi, force_lcm_update);
  1616. /* 3. turn off panel or set to doze mode */
  1617. if (dsi->panel) {
  1618. if (!new_doze_state || force_lcm_update) {
  1619. if (drm_panel_unprepare(dsi->panel))
  1620. DRM_ERROR("failed to unprepare the panel\n");
  1621. } else if (new_doze_state && !dsi->doze_enabled) {
  1622. mtk_output_en_doze_switch(dsi);
  1623. }
  1624. }
  1625. /* set DSI into ULPS mode */
  1626. mtk_dsi_reset_engine(dsi);
  1627. mtk_dsi_enter_ulps(dsi);
  1628. mtk_dsi_disable(dsi);
  1629. mtk_dsi_stop(dsi);
  1630. mtk_dsi_poweroff(dsi);
  1631. dsi->output_en = false;
  1632. dsi->doze_enabled = new_doze_state;
  1633. DDPINFO("%s-\n", __func__);
  1634. }
  1635. static void mtk_dsi_encoder_destroy(struct drm_encoder *encoder)
  1636. {
  1637. drm_encoder_cleanup(encoder);
  1638. }
  1639. static const struct drm_encoder_funcs mtk_dsi_encoder_funcs = {
  1640. .destroy = mtk_dsi_encoder_destroy,
  1641. };
  1642. static bool mtk_dsi_encoder_mode_fixup(struct drm_encoder *encoder,
  1643. const struct drm_display_mode *mode,
  1644. struct drm_display_mode *adjusted_mode)
  1645. {
  1646. return true;
  1647. }
  1648. static void mtk_dsi_encoder_mode_set(struct drm_encoder *encoder,
  1649. struct drm_display_mode *mode,
  1650. struct drm_display_mode *adjusted)
  1651. {
  1652. struct mtk_dsi *dsi = encoder_to_dsi(encoder);
  1653. dsi->vm.pixelclock = adjusted->clock;
  1654. dsi->vm.hactive = adjusted->hdisplay;
  1655. dsi->vm.hback_porch = adjusted->htotal - adjusted->hsync_end;
  1656. dsi->vm.hfront_porch = adjusted->hsync_start - adjusted->hdisplay;
  1657. dsi->vm.hsync_len = adjusted->hsync_end - adjusted->hsync_start;
  1658. dsi->vm.vactive = adjusted->vdisplay;
  1659. dsi->vm.vback_porch = adjusted->vtotal - adjusted->vsync_end;
  1660. dsi->vm.vfront_porch = adjusted->vsync_start - adjusted->vdisplay;
  1661. dsi->vm.vsync_len = adjusted->vsync_end - adjusted->vsync_start;
  1662. }
  1663. static void mtk_dsi_encoder_disable(struct drm_encoder *encoder)
  1664. {
  1665. struct mtk_dsi *dsi = encoder_to_dsi(encoder);
  1666. struct drm_crtc *crtc = encoder->crtc;
  1667. int index = drm_crtc_index(crtc);
  1668. CRTC_MMP_EVENT_START(index, dsi_suspend,
  1669. (unsigned long)crtc, index);
  1670. DDPINFO("%s\n", __func__);
  1671. mtk_drm_idlemgr_kick(__func__, crtc, 0);
  1672. mtk_output_dsi_disable(dsi, false);
  1673. CRTC_MMP_EVENT_END(index, dsi_suspend,
  1674. (unsigned long)dsi->output_en, 0);
  1675. }
  1676. static void mtk_dsi_encoder_enable(struct drm_encoder *encoder)
  1677. {
  1678. struct mtk_dsi *dsi = encoder_to_dsi(encoder);
  1679. struct drm_crtc *crtc = encoder->crtc;
  1680. int index = drm_crtc_index(crtc);
  1681. CRTC_MMP_EVENT_START(index, dsi_resume,
  1682. (unsigned long)crtc, index);
  1683. DDPINFO("%s\n", __func__);
  1684. mtk_output_dsi_enable(dsi, false);
  1685. CRTC_MMP_EVENT_END(index, dsi_resume,
  1686. (unsigned long)dsi->output_en, 0);
  1687. }
  1688. static enum drm_connector_status
  1689. mtk_dsi_connector_detect(struct drm_connector *connector, bool force)
  1690. {
  1691. return connector_status_connected;
  1692. }
  1693. static int mtk_dsi_connector_get_modes(struct drm_connector *connector)
  1694. {
  1695. struct mtk_dsi *dsi = connector_to_dsi(connector);
  1696. return drm_panel_get_modes(dsi->panel);
  1697. }
  1698. static int mtk_dsi_atomic_check(struct drm_encoder *encoder,
  1699. struct drm_crtc_state *crtc_state,
  1700. struct drm_connector_state *conn_state)
  1701. {
  1702. struct mtk_drm_crtc *mtk_crtc =
  1703. container_of(conn_state->crtc, struct mtk_drm_crtc, base);
  1704. struct mtk_dsi *dsi = encoder_to_dsi(encoder);
  1705. switch (dsi->format) {
  1706. case MIPI_DSI_FMT_RGB565:
  1707. mtk_crtc->bpc = 5;
  1708. break;
  1709. case MIPI_DSI_FMT_RGB666_PACKED:
  1710. mtk_crtc->bpc = 6;
  1711. break;
  1712. case MIPI_DSI_FMT_RGB666:
  1713. case MIPI_DSI_FMT_RGB888:
  1714. default:
  1715. mtk_crtc->bpc = 8;
  1716. break;
  1717. }
  1718. return 0;
  1719. }
  1720. static const struct drm_encoder_helper_funcs mtk_dsi_encoder_helper_funcs = {
  1721. .mode_fixup = mtk_dsi_encoder_mode_fixup,
  1722. .mode_set = mtk_dsi_encoder_mode_set,
  1723. .disable = mtk_dsi_encoder_disable,
  1724. .enable = mtk_dsi_encoder_enable,
  1725. .atomic_check = mtk_dsi_atomic_check,
  1726. };
  1727. static const struct drm_connector_funcs mtk_dsi_connector_funcs = {
  1728. /* .dpms = drm_atomic_helper_connector_dpms, */
  1729. .detect = mtk_dsi_connector_detect,
  1730. .fill_modes = drm_helper_probe_single_connector_modes,
  1731. .destroy = drm_connector_cleanup,
  1732. .reset = drm_atomic_helper_connector_reset,
  1733. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  1734. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  1735. };
  1736. static const struct drm_connector_helper_funcs mtk_dsi_conn_helper_funcs = {
  1737. .get_modes = mtk_dsi_connector_get_modes,
  1738. };
  1739. static int mtk_drm_attach_bridge(struct drm_bridge *bridge,
  1740. struct drm_encoder *encoder)
  1741. {
  1742. int ret;
  1743. if (!bridge)
  1744. return -ENOENT;
  1745. encoder->bridge = bridge;
  1746. bridge->encoder = encoder;
  1747. ret = drm_bridge_attach(encoder, bridge, NULL);
  1748. if (ret) {
  1749. DRM_ERROR("Failed to attach bridge to drm\n");
  1750. encoder->bridge = NULL;
  1751. bridge->encoder = NULL;
  1752. }
  1753. return ret;
  1754. }
  1755. static int mtk_dsi_create_connector(struct drm_device *drm, struct mtk_dsi *dsi)
  1756. {
  1757. int ret;
  1758. ret = drm_connector_init(drm, &dsi->conn, &mtk_dsi_connector_funcs,
  1759. DRM_MODE_CONNECTOR_DSI);
  1760. if (ret) {
  1761. DRM_ERROR("Failed to connector init to drm\n");
  1762. return ret;
  1763. }
  1764. drm_connector_helper_add(&dsi->conn, &mtk_dsi_conn_helper_funcs);
  1765. dsi->conn.dpms = DRM_MODE_DPMS_OFF;
  1766. drm_mode_connector_attach_encoder(&dsi->conn, &dsi->encoder);
  1767. if (dsi->panel) {
  1768. ret = drm_panel_attach(dsi->panel, &dsi->conn);
  1769. if (ret) {
  1770. DRM_ERROR("Failed to attach panel to drm\n");
  1771. goto err_connector_cleanup;
  1772. }
  1773. }
  1774. return 0;
  1775. err_connector_cleanup:
  1776. drm_connector_cleanup(&dsi->conn);
  1777. return ret;
  1778. }
  1779. static int mtk_dsi_create_conn_enc(struct drm_device *drm, struct mtk_dsi *dsi)
  1780. {
  1781. int ret;
  1782. ret = drm_encoder_init(drm, &dsi->encoder, &mtk_dsi_encoder_funcs,
  1783. DRM_MODE_ENCODER_DSI, NULL);
  1784. if (ret) {
  1785. DRM_ERROR("Failed to encoder init to drm\n");
  1786. return ret;
  1787. }
  1788. drm_encoder_helper_add(&dsi->encoder, &mtk_dsi_encoder_helper_funcs);
  1789. /*
  1790. * Currently display data paths are statically assigned to a crtc each.
  1791. * crtc 0 is OVL0 -> COLOR0 -> AAL -> OD -> RDMA0 -> UFOE -> DSI0
  1792. */
  1793. dsi->encoder.possible_crtcs = 1;
  1794. /* If there's a bridge, attach to it and let it create the connector */
  1795. ret = mtk_drm_attach_bridge(dsi->bridge, &dsi->encoder);
  1796. if (ret) {
  1797. /* Otherwise create our own connector and attach to a panel */
  1798. ret = mtk_dsi_create_connector(drm, dsi);
  1799. if (ret)
  1800. goto err_encoder_cleanup;
  1801. }
  1802. return 0;
  1803. err_encoder_cleanup:
  1804. drm_encoder_cleanup(&dsi->encoder);
  1805. return ret;
  1806. }
  1807. static void mtk_dsi_destroy_conn_enc(struct mtk_dsi *dsi)
  1808. {
  1809. drm_encoder_cleanup(&dsi->encoder);
  1810. /* Skip connector cleanup if creation was delegated to the bridge */
  1811. if (dsi->conn.dev)
  1812. drm_connector_cleanup(&dsi->conn);
  1813. }
  1814. struct mtk_panel_ext *mtk_dsi_get_panel_ext(struct mtk_ddp_comp *comp)
  1815. {
  1816. struct mtk_dsi *dsi = container_of(comp, struct mtk_dsi, ddp_comp);
  1817. return dsi->ext;
  1818. }
  1819. /* SET MODE */
  1820. static void _mtk_dsi_set_mode(struct mtk_ddp_comp *comp, void *handle,
  1821. unsigned int mode)
  1822. {
  1823. cmdq_pkt_write(handle, comp->cmdq_base, comp->regs_pa + DSI_MODE_CTRL,
  1824. mode, ~0);
  1825. }
  1826. /* STOP VDO MODE */
  1827. static int mtk_dsi_stop_vdo_mode(struct mtk_dsi *dsi, void *handle)
  1828. {
  1829. struct mtk_ddp_comp *comp = &dsi->ddp_comp;
  1830. struct mtk_drm_crtc *mtk_crtc = comp->mtk_crtc;
  1831. int need_create_hnd = 0;
  1832. struct cmdq_pkt *cmdq_handle;
  1833. if (!mtk_crtc) {
  1834. DDPPR_ERR("%s, mtk_crtc is NULL\n", __func__);
  1835. return 1;
  1836. }
  1837. /* Add blocking flush for waiting dsi idle in other gce client */
  1838. if (handle) {
  1839. struct cmdq_pkt *cmdq_handle1 = (struct cmdq_pkt *)handle;
  1840. if (cmdq_handle1->cl !=
  1841. mtk_crtc->gce_obj.client[CLIENT_DSI_CFG]) {
  1842. mtk_crtc_pkt_create(&cmdq_handle,
  1843. &mtk_crtc->base,
  1844. mtk_crtc->gce_obj.client[CLIENT_DSI_CFG]);
  1845. cmdq_pkt_flush(cmdq_handle);
  1846. cmdq_pkt_destroy(cmdq_handle);
  1847. }
  1848. } else {
  1849. mtk_crtc_pkt_create(&cmdq_handle,
  1850. &mtk_crtc->base,
  1851. mtk_crtc->gce_obj.client[CLIENT_DSI_CFG]);
  1852. cmdq_pkt_flush(cmdq_handle);
  1853. cmdq_pkt_destroy(cmdq_handle);
  1854. }
  1855. if (!handle)
  1856. need_create_hnd = 1;
  1857. if (need_create_hnd) {
  1858. mtk_crtc_pkt_create((struct cmdq_pkt **)&handle,
  1859. &mtk_crtc->base,
  1860. mtk_crtc->gce_obj.client[CLIENT_CFG]);
  1861. /* wait frame done */
  1862. cmdq_pkt_wait_no_clear(handle,
  1863. mtk_crtc->gce_obj.event[EVENT_VDO_EOF]);
  1864. }
  1865. /* stop vdo mode */
  1866. _mtk_dsi_set_mode(&dsi->ddp_comp, handle, CMD_MODE);
  1867. cmdq_pkt_write(handle, dsi->ddp_comp.cmdq_base,
  1868. dsi->ddp_comp.regs_pa + DSI_START, 0, ~0);
  1869. mtk_dsi_poll_for_idle(dsi, handle);
  1870. if (need_create_hnd) {
  1871. cmdq_pkt_flush(handle);
  1872. cmdq_pkt_destroy(handle);
  1873. }
  1874. return 0;
  1875. }
  1876. static int mtk_dsi_start_vdo_mode(struct mtk_ddp_comp *comp, void *handle)
  1877. {
  1878. struct mtk_dsi *dsi = container_of(comp, struct mtk_dsi, ddp_comp);
  1879. u32 vid_mode = CMD_MODE;
  1880. if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO) {
  1881. if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
  1882. vid_mode = BURST_MODE;
  1883. else if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
  1884. vid_mode = SYNC_PULSE_MODE;
  1885. else
  1886. vid_mode = SYNC_EVENT_MODE;
  1887. }
  1888. cmdq_pkt_write(handle, comp->cmdq_base, comp->regs_pa + DSI_START, 0,
  1889. ~0);
  1890. _mtk_dsi_set_mode(comp, handle, vid_mode);
  1891. return 0;
  1892. }
  1893. static int mtk_dsi_trigger(struct mtk_ddp_comp *comp, void *handle)
  1894. {
  1895. cmdq_pkt_write(handle, comp->cmdq_base, comp->regs_pa + DSI_START, 1,
  1896. ~0);
  1897. return 0;
  1898. }
  1899. int mtk_dsi_read_gce(struct mtk_ddp_comp *comp, void *handle,
  1900. struct DSI_T0_INS *t0, int i, uintptr_t slot)
  1901. {
  1902. struct mtk_dsi *dsi = container_of(comp, struct mtk_dsi, ddp_comp);
  1903. dma_addr_t read_slot = (dma_addr_t)slot;
  1904. cmdq_pkt_write(handle, comp->cmdq_base, comp->regs_pa + DSI_CMDQ0,
  1905. 0x00013700, ~0);
  1906. cmdq_pkt_write(handle, comp->cmdq_base, comp->regs_pa + DSI_CMDQ1,
  1907. AS_UINT32(t0), ~0);
  1908. cmdq_pkt_write(handle, comp->cmdq_base, comp->regs_pa + DSI_CMDQ_SIZE,
  1909. 0x2, ~0);
  1910. cmdq_pkt_write(handle, comp->cmdq_base, comp->regs_pa + DSI_START,
  1911. 0x0, ~0);
  1912. cmdq_pkt_write(handle, comp->cmdq_base, comp->regs_pa + DSI_START,
  1913. 0x1, ~0);
  1914. mtk_dsi_cmdq_poll(comp, handle, comp->regs_pa + DSI_INTSTA, 0x1, 0x1);
  1915. cmdq_pkt_write(handle, comp->cmdq_base, comp->regs_pa + DSI_INTSTA,
  1916. 0x0, 0x1);
  1917. cmdq_pkt_mem_move(handle, comp->cmdq_base,
  1918. comp->regs_pa + DSI_RX_DATA0, read_slot + (i * 2) * 0x4,
  1919. CMDQ_THR_SPR_IDX3);
  1920. cmdq_pkt_mem_move(handle, comp->cmdq_base,
  1921. comp->regs_pa + DSI_RX_DATA1, read_slot + (i * 2 + 1) * 0x4,
  1922. CMDQ_THR_SPR_IDX3);
  1923. cmdq_pkt_write(handle, comp->cmdq_base, comp->regs_pa + DSI_RACK,
  1924. 0x1, 0x1);
  1925. cmdq_pkt_write(handle, comp->cmdq_base, comp->regs_pa + DSI_INTSTA,
  1926. 0x0, 0x1);
  1927. mtk_dsi_poll_for_idle(dsi, handle);
  1928. return 0;
  1929. }
  1930. int mtk_dsi_esd_read(struct mtk_ddp_comp *comp, void *handle, uintptr_t slot)
  1931. {
  1932. int i;
  1933. struct DSI_T0_INS t0;
  1934. struct mtk_dsi *dsi = container_of(comp, struct mtk_dsi, ddp_comp);
  1935. struct mtk_panel_params *params;
  1936. if (dsi->ext && dsi->ext->params)
  1937. params = dsi->ext->params;
  1938. else /* can't find panel ext information, stop esd read */
  1939. return 0;
  1940. for (i = 0 ; i < ESD_CHECK_NUM ; i++) {
  1941. if (params->lcm_esd_check_table[i].cmd == 0)
  1942. break;
  1943. t0.CONFG = 0x04;
  1944. t0.Data0 = params->lcm_esd_check_table[i].cmd;
  1945. t0.Data_ID = (t0.Data0 < 0xB0)
  1946. ? DSI_DCS_READ_PACKET_ID
  1947. : DSI_GERNERIC_READ_LONG_PACKET_ID;
  1948. t0.Data1 = 0;
  1949. mtk_dsi_read_gce(comp, handle, &t0, i, slot);
  1950. }
  1951. return 0;
  1952. }
  1953. int mtk_dsi_esd_cmp(struct mtk_ddp_comp *comp, void *handle, void *slot)
  1954. {
  1955. int i, ret = 0;
  1956. u32 tmp0, tmp1, chk_val;
  1957. struct mtk_dsi *dsi = container_of(comp, struct mtk_dsi, ddp_comp);
  1958. struct esd_check_item *lcm_esd_tb;
  1959. struct mtk_panel_params *params;
  1960. if (dsi->ext && dsi->ext->params)
  1961. params = dsi->ext->params;
  1962. else /* can't find panel ext information, stop esd read */
  1963. return 0;
  1964. for (i = 0; i < ESD_CHECK_NUM; i++) {
  1965. if (dsi->ext->params->lcm_esd_check_table[i].cmd == 0)
  1966. break;
  1967. if (slot) {
  1968. tmp0 = AS_UINT32(slot + (i * 2) * 0x4);
  1969. tmp1 = AS_UINT32(slot + (i * 2 + 1) * 0x4);
  1970. } else if (i == 0) {
  1971. tmp0 = readl(dsi->regs + DSI_RX_DATA0);
  1972. tmp1 = readl(dsi->regs + DSI_RX_DATA1);
  1973. }
  1974. lcm_esd_tb = &params->lcm_esd_check_table[i];
  1975. if ((tmp0 & 0xff) == 0x1C)
  1976. chk_val = tmp1 & 0xff;
  1977. else
  1978. chk_val = (tmp0 >> 8) & 0xff;
  1979. if (lcm_esd_tb->mask_list[0])
  1980. chk_val = chk_val & lcm_esd_tb->mask_list[0];
  1981. if (chk_val == lcm_esd_tb->para_list[0]) {
  1982. ret = 0;
  1983. } else {
  1984. DDPPR_ERR("[DSI]cmp fail:read(0x%x)!=expect(0x%x)\n",
  1985. chk_val, lcm_esd_tb->para_list[0]);
  1986. ret = -1;
  1987. break;
  1988. }
  1989. }
  1990. return ret;
  1991. }
  1992. static const char *mtk_dsi_cmd_mode_parse_state(unsigned int state)
  1993. {
  1994. switch (state) {
  1995. case 0x0001:
  1996. return "idle";
  1997. case 0x0002:
  1998. return "Reading command queue for header";
  1999. case 0x0004:
  2000. return "Sending type-0 command";
  2001. case 0x0008:
  2002. return "Waiting frame data from RDMA for type-1 command";
  2003. case 0x0010:
  2004. return "Sending type-1 command";
  2005. case 0x0020:
  2006. return "Sending type-2 command";
  2007. case 0x0040:
  2008. return "Reading command queue for type-2 data";
  2009. case 0x0080:
  2010. return "Sending type-3 command";
  2011. case 0x0100:
  2012. return "Sending BTA";
  2013. case 0x0200:
  2014. return "Waiting RX-read data";
  2015. case 0x0400:
  2016. return "Waiting SW RACK for RX-read data";
  2017. case 0x0800:
  2018. return "Waiting TE";
  2019. case 0x1000:
  2020. return "Get TE";
  2021. case 0x2000:
  2022. return "Waiting SW RACK for TE";
  2023. case 0x4000:
  2024. return "Waiting external TE";
  2025. case 0x8000:
  2026. return "Get external TE";
  2027. default:
  2028. return "unknown";
  2029. }
  2030. }
  2031. static const char *mtk_dsi_vdo_mode_parse_state(unsigned int state)
  2032. {
  2033. switch (state) {
  2034. case 0x0001:
  2035. return "Video mode idle";
  2036. case 0x0002:
  2037. return "Sync start packet";
  2038. case 0x0004:
  2039. return "Hsync active";
  2040. case 0x0008:
  2041. return "Sync end packet";
  2042. case 0x0010:
  2043. return "Hsync back porch";
  2044. case 0x0020:
  2045. return "Video data period";
  2046. case 0x0040:
  2047. return "Hsync front porch";
  2048. case 0x0080:
  2049. return "BLLP";
  2050. case 0x0100:
  2051. return "--";
  2052. case 0x0200:
  2053. return "Mix mode using command mode transmission";
  2054. case 0x0400:
  2055. return "Command transmission in BLLP";
  2056. default:
  2057. return "unknown";
  2058. }
  2059. }
  2060. int mtk_dsi_dump(struct mtk_ddp_comp *comp)
  2061. {
  2062. int k;
  2063. struct mtk_dsi *dsi = container_of(comp, struct mtk_dsi, ddp_comp);
  2064. void __iomem *baddr = comp->regs;
  2065. unsigned int reg_val;
  2066. if (DISP_REG_GET_FIELD(MODE_FLD_REG_MODE_CON,
  2067. baddr + DSI_MODE_CTRL)) {
  2068. /* VDO mode */
  2069. reg_val = (readl(dsi->regs + 0x164)) & 0xff;
  2070. DDPDUMP("state7(vdo mode):%s\n",
  2071. mtk_dsi_vdo_mode_parse_state(reg_val));
  2072. } else {
  2073. reg_val = (readl(dsi->regs + 0x160)) & 0xffff;
  2074. DDPDUMP("state6(cmd mode):%s\n",
  2075. mtk_dsi_cmd_mode_parse_state(reg_val));
  2076. }
  2077. reg_val = (readl(dsi->regs + 0x168)) & 0x3fff;
  2078. DDPDUMP("state8 WORD_COUNTER(cmd mode):%u\n", reg_val);
  2079. reg_val = (readl(dsi->regs + 0x16C)) & 0x3fffff;
  2080. DDPDUMP("state9 LINE_COUNTER(cmd mode):%u\n", reg_val);
  2081. DDPDUMP("== %s REGS ==\n", mtk_dump_comp_str(comp));
  2082. for (k = 0; k < 0x200; k += 16) {
  2083. DDPDUMP("0x%04x: 0x%08x 0x%08x 0x%08x 0x%08x\n", k,
  2084. readl(dsi->regs + k),
  2085. readl(dsi->regs + k + 0x4),
  2086. readl(dsi->regs + k + 0x8),
  2087. readl(dsi->regs + k + 0xc));
  2088. }
  2089. DDPDUMP("- DSI CMD REGS -\n");
  2090. for (k = 0; k < 32; k += 16) {
  2091. DDPDUMP("0x%04x: 0x%08x 0x%08x 0x%08x 0x%08x\n", k,
  2092. readl(dsi->regs + 0x200 + k),
  2093. readl(dsi->regs + 0x200 + k + 0x4),
  2094. readl(dsi->regs + 0x200 + k + 0x8),
  2095. readl(dsi->regs + 0x200 + k + 0xc));
  2096. }
  2097. mtk_mipi_tx_dump(dsi->phy);
  2098. return 0;
  2099. }
  2100. unsigned int mtk_dsi_fps_change_index(struct mtk_dsi *dsi,
  2101. struct mtk_drm_crtc *mtk_crtc, struct drm_crtc_state *old_state)
  2102. {
  2103. struct mtk_panel_ext *panel_ext = mtk_crtc->panel_ext;
  2104. struct mtk_panel_ext *get_panel_ext = find_panel_ext(dsi->panel);
  2105. struct drm_display_mode *old_mode;
  2106. struct drm_display_mode *adjust_mode;
  2107. struct mtk_panel_params *cur_panel_params = panel_ext->params;
  2108. struct mtk_panel_params *adjust_panel_params = NULL;
  2109. unsigned int fps_chg_index = 0;
  2110. unsigned int old_get_sta = 0, new_get_sta = 0;
  2111. struct mtk_crtc_state *state =
  2112. to_mtk_crtc_state(mtk_crtc->base.state);
  2113. struct mtk_crtc_state *old_mtk_state =
  2114. to_mtk_crtc_state(old_state);
  2115. unsigned int src_mode_idx =
  2116. old_mtk_state->prop_val[CRTC_PROP_DISP_MODE_IDX];
  2117. unsigned int dst_mode_idx =
  2118. state->prop_val[CRTC_PROP_DISP_MODE_IDX];
  2119. old_mode = &(mtk_crtc->avail_modes[src_mode_idx]);
  2120. adjust_mode = &(mtk_crtc->avail_modes[dst_mode_idx]);
  2121. if (panel_ext && panel_ext->funcs &&
  2122. panel_ext->funcs->ext_param_set) {
  2123. DDPINFO("old ext_param_set\n");
  2124. old_get_sta = panel_ext->funcs->ext_param_set(
  2125. dsi->panel, src_mode_idx);
  2126. }
  2127. if (old_get_sta)
  2128. DDPINFO("%s,error:not support src MODE:(%d)\n", __func__,
  2129. src_mode_idx);
  2130. if (get_panel_ext) {
  2131. cur_panel_params = get_panel_ext->params;
  2132. adjust_panel_params = get_panel_ext->params;
  2133. }
  2134. if (panel_ext && panel_ext->funcs &&
  2135. panel_ext->funcs->ext_param_set) {
  2136. DDPINFO("new ext_param_set\n");
  2137. new_get_sta = panel_ext->funcs->ext_param_set(
  2138. dsi->panel, dst_mode_idx);
  2139. }
  2140. if (new_get_sta)
  2141. DDPINFO("%s,error:not support dst MODE:(%d)\n", __func__,
  2142. dst_mode_idx);
  2143. if (!(dsi->mipi_hopping_sta && adjust_panel_params &&
  2144. cur_panel_params && cur_panel_params->dyn.switch_en &&
  2145. adjust_panel_params->dyn.switch_en == 1)) {
  2146. if (adjust_mode->vtotal !=
  2147. old_mode->vtotal) {
  2148. fps_chg_index |= DYNFPS_DSI_VFP;
  2149. }
  2150. if (adjust_mode->htotal !=
  2151. old_mode->htotal) {
  2152. fps_chg_index |= DYNFPS_DSI_HFP;
  2153. }
  2154. if (panel_ext && adjust_panel_params &&
  2155. panel_ext->params->data_rate !=
  2156. adjust_panel_params->data_rate) {
  2157. fps_chg_index |= DYNFPS_DSI_MIPI_CLK;
  2158. }
  2159. if (!fps_chg_index && cur_panel_params &&
  2160. adjust_panel_params && cur_panel_params->pll_clk !=
  2161. adjust_panel_params->pll_clk) {
  2162. fps_chg_index |= DYNFPS_DSI_MIPI_CLK;
  2163. }
  2164. if (!fps_chg_index &&
  2165. adjust_mode->clock != old_mode->clock) {
  2166. fps_chg_index |= DYNFPS_DSI_MIPI_CLK;
  2167. }
  2168. } else if (cur_panel_params && adjust_panel_params) {
  2169. if (cur_panel_params->dyn.vfp !=
  2170. adjust_panel_params->dyn.vfp) {
  2171. fps_chg_index |= DYNFPS_DSI_VFP;
  2172. }
  2173. if (cur_panel_params->dyn.hfp !=
  2174. adjust_panel_params->dyn.hfp) {
  2175. fps_chg_index |= DYNFPS_DSI_HFP;
  2176. }
  2177. if (cur_panel_params->dyn.pll_clk !=
  2178. adjust_panel_params->dyn.pll_clk) {
  2179. fps_chg_index |= DYNFPS_DSI_MIPI_CLK;
  2180. }
  2181. if (cur_panel_params->dyn.data_rate !=
  2182. adjust_panel_params->dyn.data_rate) {
  2183. fps_chg_index |= DYNFPS_DSI_MIPI_CLK;
  2184. }
  2185. }
  2186. mtk_crtc->fps_change_index = fps_chg_index;
  2187. DDPINFO("%s,chg %d->%d\n", __func__, old_mode->vrefresh,
  2188. adjust_mode->vrefresh);
  2189. DDPINFO("%s,mipi_hopping_sta %d,chg solution:0x%x\n", __func__,
  2190. dsi->mipi_hopping_sta, fps_chg_index);
  2191. return 0;
  2192. }
  2193. static const char *mtk_dsi_mode_spy(enum DSI_MODE_CON mode)
  2194. {
  2195. switch (mode) {
  2196. case MODE_CON_CMD:
  2197. return "CMD_MODE";
  2198. case MODE_CON_SYNC_PULSE_VDO:
  2199. return "SYNC_PULSE_VDO_MODE";
  2200. case MODE_CON_SYNC_EVENT_VDO:
  2201. return "SYNC_EVENT_VDO_MODE";
  2202. case MODE_CON_BURST_VDO:
  2203. return "BURST_VDO_MODE";
  2204. default:
  2205. break;
  2206. }
  2207. return "unknown-mode";
  2208. }
  2209. int mtk_dsi_analysis(struct mtk_ddp_comp *comp)
  2210. {
  2211. #ifndef CONFIG_FPGA_EARLY_PORTING
  2212. struct mtk_dsi *dsi = container_of(comp, struct mtk_dsi, ddp_comp);
  2213. #endif
  2214. void __iomem *baddr = comp->regs;
  2215. unsigned int reg_val;
  2216. DDPDUMP("== %s ANALYSIS ==\n", mtk_dump_comp_str(comp));
  2217. #ifndef CONFIG_FPGA_EARLY_PORTING
  2218. DDPDUMP("MIPITX Clock:%d\n", mtk_mipi_tx_pll_get_rate(dsi->phy));
  2219. #endif
  2220. DDPDUMP("start:%x,busy:%d,DSI_DUAL_EN:%d\n",
  2221. DISP_REG_GET_FIELD(START_FLD_REG_START, baddr + DSI_START),
  2222. DISP_REG_GET_FIELD(INTSTA_FLD_REG_BUSY, baddr + DSI_INTSTA),
  2223. DISP_REG_GET_FIELD(CON_CTRL_FLD_REG_DUAL_EN,
  2224. baddr + DSI_CON_CTRL));
  2225. DDPDUMP("mode:%s,high_speed:%d,FSM_State:%s\n",
  2226. mtk_dsi_mode_spy(DISP_REG_GET_FIELD(MODE_FLD_REG_MODE_CON,
  2227. baddr + DSI_MODE_CTRL)),
  2228. DISP_REG_GET_FIELD(PHY_FLD_REG_LC_HSTX_EN,
  2229. baddr + DSI_PHY_LCCON),
  2230. mtk_dsi_cmd_mode_parse_state(
  2231. DISP_REG_GET_FIELD(STATE_DBG6_FLD_REG_CMCTL_STATE,
  2232. baddr + DSI_STATE_DBG6)));
  2233. reg_val = readl(DSI_INTEN + baddr);
  2234. DDPDUMP("IRQ_EN,RD_RDY:%d,CMD_DONE:%d,SLEEPOUT_DONE:%d\n",
  2235. REG_FLD_VAL_GET(INTSTA_FLD_REG_RD_RDY, reg_val),
  2236. REG_FLD_VAL_GET(INTSTA_FLD_REG_CMD_DONE, reg_val),
  2237. REG_FLD_VAL_GET(INTSTA_FLD_REG_SLEEPOUT_DONE, reg_val));
  2238. DDPDUMP("TE_RDY:%d,VM_CMD_DONE:%d,VM_DONE:%d\n",
  2239. REG_FLD_VAL_GET(INTSTA_FLD_REG_TE_RDY, reg_val),
  2240. REG_FLD_VAL_GET(INTSTA_FLD_REG_VM_CMD_DONE, reg_val),
  2241. REG_FLD_VAL_GET(INTSTA_FLD_REG_VM_DONE, reg_val));
  2242. reg_val = readl(DSI_INTSTA + baddr);
  2243. DDPDUMP("IRQ,RD_RDY:%d,CMD_DONE:%d,SLEEPOUT_DONE:%d\n",
  2244. REG_FLD_VAL_GET(INTSTA_FLD_REG_RD_RDY, reg_val),
  2245. REG_FLD_VAL_GET(INTSTA_FLD_REG_CMD_DONE, reg_val),
  2246. REG_FLD_VAL_GET(INTSTA_FLD_REG_SLEEPOUT_DONE, reg_val));
  2247. DDPDUMP("TE_RDY:%d,VM_CMD_DONE:%d,VM_DONE:%d\n",
  2248. REG_FLD_VAL_GET(INTSTA_FLD_REG_TE_RDY, reg_val),
  2249. REG_FLD_VAL_GET(INTSTA_FLD_REG_VM_CMD_DONE, reg_val),
  2250. REG_FLD_VAL_GET(INTSTA_FLD_REG_VM_DONE, reg_val));
  2251. reg_val = readl(DSI_TXRX_CTRL + baddr);
  2252. DDPDUMP("lane_num:%d,Ext_TE_EN:%d,Ext_TE_Edge:%d,HSTX_CKLP_EN:%d\n",
  2253. REG_FLD_VAL_GET(TXRX_CTRL_FLD_REG_LANE_NUM, reg_val),
  2254. REG_FLD_VAL_GET(TXRX_CTRL_FLD_REG_EXT_TE_EN, reg_val),
  2255. REG_FLD_VAL_GET(TXRX_CTRL_FLD_REG_EXT_TE_EDGE, reg_val),
  2256. REG_FLD_VAL_GET(TXRX_CTRL_FLD_REG_HSTX_CKLP_EN, reg_val));
  2257. reg_val = readl(DSI_LFR_CON + baddr);
  2258. DDPDUMP("LFR_en:%d, LFR_VSE_DIS:%d, LFR_UPDATE:%d, LFR_MODE:%d, LFR_TYPE:%d, LFR_SKIP_NUMBER:%d\n",
  2259. REG_FLD_VAL_GET(LFR_CON_FLD_REG_LFR_EN, reg_val),
  2260. REG_FLD_VAL_GET(LFR_CON_FLD_REG_LFR_VSE_DIS, reg_val),
  2261. REG_FLD_VAL_GET(LFR_CON_FLD_REG_LFR_UPDATE, reg_val),
  2262. REG_FLD_VAL_GET(LFR_CON_FLD_REG_LFR_MODE, reg_val),
  2263. REG_FLD_VAL_GET(LFR_CON_FLD_REG_LFR_TYPE, reg_val),
  2264. REG_FLD_VAL_GET(LFR_CON_FLD_REG_LFR_SKIP_NUM, reg_val));
  2265. return 0;
  2266. }
  2267. static void mtk_dsi_ddp_prepare(struct mtk_ddp_comp *comp)
  2268. {
  2269. struct mtk_dsi *dsi = container_of(comp, struct mtk_dsi, ddp_comp);
  2270. mtk_dsi_poweron(dsi);
  2271. }
  2272. static void mtk_dsi_ddp_unprepare(struct mtk_ddp_comp *comp)
  2273. {
  2274. struct mtk_dsi *dsi = container_of(comp, struct mtk_dsi, ddp_comp);
  2275. mtk_dsi_poweroff(dsi);
  2276. }
  2277. static void mtk_dsi_config_trigger(struct mtk_ddp_comp *comp,
  2278. struct cmdq_pkt *handle,
  2279. enum mtk_ddp_comp_trigger_flag flag)
  2280. {
  2281. struct mtk_dsi *dsi = container_of(comp, struct mtk_dsi, ddp_comp);
  2282. switch (flag) {
  2283. case MTK_TRIG_FLAG_TRIGGER:
  2284. /* TODO: avoid hardcode: 0xF0 register offset */
  2285. cmdq_pkt_write(handle, comp->cmdq_base,
  2286. comp->mtk_crtc->config_regs_pa + 0xF0, 0x1, 0x1);
  2287. cmdq_pkt_write(handle, comp->cmdq_base, comp->regs_pa + 0x200,
  2288. 0x002c3909, ~0);
  2289. cmdq_pkt_write(handle, comp->cmdq_base, comp->regs_pa + 0x60, 1,
  2290. ~0);
  2291. cmdq_pkt_write(handle, comp->cmdq_base,
  2292. comp->regs_pa + DSI_START, 0, ~0);
  2293. cmdq_pkt_write(handle, comp->cmdq_base,
  2294. comp->regs_pa + DSI_START, 1, ~0);
  2295. break;
  2296. case MTK_TRIG_FLAG_EOF:
  2297. mtk_dsi_poll_for_idle(dsi, handle);
  2298. break;
  2299. default:
  2300. break;
  2301. }
  2302. }
  2303. static int mtk_dsi_is_busy(struct mtk_ddp_comp *comp)
  2304. {
  2305. int ret, tmp;
  2306. struct mtk_dsi *dsi = container_of(comp, struct mtk_dsi, ddp_comp);
  2307. tmp = readl(dsi->regs + DSI_INTSTA);
  2308. ret = (tmp & DSI_BUSY) ? 1 : 0;
  2309. DDPINFO("%s:%d is:%d regs:0x%x\n", __func__, __LINE__, ret, tmp);
  2310. return ret;
  2311. }
  2312. bool mtk_dsi_is_cmd_mode(struct mtk_ddp_comp *comp)
  2313. {
  2314. struct mtk_dsi *dsi;
  2315. if (mtk_ddp_comp_get_type(comp->id) == MTK_DISP_WDMA)
  2316. return true;
  2317. dsi = container_of(comp, struct mtk_dsi, ddp_comp);
  2318. if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO)
  2319. return false;
  2320. else
  2321. return true;
  2322. }
  2323. static const char *mtk_dsi_get_porch_str(enum dsi_porch_type type)
  2324. {
  2325. if (type < 0) {
  2326. DDPPR_ERR("%s: Invalid dsi porch type:%d\n", __func__, type);
  2327. type = 0;
  2328. }
  2329. return mtk_dsi_porch_str[type];
  2330. }
  2331. int mtk_dsi_porch_setting(struct mtk_ddp_comp *comp, struct cmdq_pkt *handle,
  2332. enum dsi_porch_type type, unsigned int value)
  2333. {
  2334. int ret = 0;
  2335. DDPINFO("%s set %s: %s to %d\n", __func__, mtk_dump_comp_str(comp),
  2336. mtk_dsi_get_porch_str(type), value);
  2337. switch (type) {
  2338. case DSI_VFP:
  2339. mtk_ddp_write_relaxed(comp, value, DSI_VFP_NL, handle);
  2340. break;
  2341. case DSI_VSA:
  2342. mtk_ddp_write_relaxed(comp, value, DSI_VSA_NL, handle);
  2343. break;
  2344. case DSI_VBP:
  2345. mtk_ddp_write_relaxed(comp, value, DSI_VBP_NL, handle);
  2346. break;
  2347. case DSI_VACT:
  2348. mtk_ddp_write_relaxed(comp, value, DSI_VACT_NL, handle);
  2349. break;
  2350. case DSI_HFP:
  2351. mtk_ddp_write_relaxed(comp, value, DSI_HFP_WC, handle);
  2352. break;
  2353. case DSI_HSA:
  2354. mtk_ddp_write_relaxed(comp, value, DSI_HSA_WC, handle);
  2355. break;
  2356. case DSI_HBP:
  2357. mtk_ddp_write_relaxed(comp, value, DSI_HBP_WC, handle);
  2358. break;
  2359. case DSI_BLLP:
  2360. mtk_ddp_write_relaxed(comp, value, DSI_BLLP_WC, handle);
  2361. break;
  2362. default:
  2363. break;
  2364. }
  2365. return ret;
  2366. }
  2367. /* TODO: refactor to remove duplicate code */
  2368. static void mtk_dsi_enter_idle(struct mtk_dsi *dsi)
  2369. {
  2370. mtk_dsi_mask(dsi, DSI_INTEN, ~0, 0);
  2371. mtk_dsi_reset_engine(dsi);
  2372. mtk_dsi_enter_ulps(dsi);
  2373. mtk_dsi_poweroff(dsi);
  2374. }
  2375. static void mtk_dsi_leave_idle(struct mtk_dsi *dsi)
  2376. {
  2377. int ret;
  2378. ret = mtk_dsi_poweron(dsi);
  2379. if (ret < 0) {
  2380. DDPPR_ERR("failed to power on dsi\n");
  2381. return;
  2382. }
  2383. mtk_dsi_enable(dsi);
  2384. mtk_dsi_phy_timconfig(dsi, NULL);
  2385. mtk_dsi_rxtx_control(dsi);
  2386. mtk_dsi_ps_control_vact(dsi);
  2387. mtk_dsi_set_interrupt_enable(dsi);
  2388. mtk_dsi_exit_ulps(dsi);
  2389. /*
  2390. * TODO: It's a temp workaround for cmd mode. When set the EXT_TE_EN bit
  2391. * before sending DSI cmd. System would hang. So move the bit control
  2392. * after
  2393. * lcm initialize.
  2394. */
  2395. if (mtk_dsi_is_cmd_mode(&dsi->ddp_comp))
  2396. writel(0x0001023c, dsi->regs + DSI_TXRX_CTRL);
  2397. mtk_dsi_set_mode(dsi);
  2398. mtk_dsi_clk_hs_mode(dsi, 1);
  2399. }
  2400. static void mtk_dsi_clk_change(struct mtk_dsi *dsi, int en)
  2401. {
  2402. struct mtk_panel_ext *ext = dsi->ext;
  2403. struct mtk_ddp_comp *comp = &dsi->ddp_comp;
  2404. struct mtk_drm_crtc *mtk_crtc = comp->mtk_crtc;
  2405. struct drm_crtc *crtc = &mtk_crtc->base;
  2406. bool mod_vfp, mod_vbp, mod_vsa;
  2407. bool mod_hfp, mod_hbp, mod_hsa;
  2408. unsigned int data_rate;
  2409. struct cmdq_pkt *cmdq_handle;
  2410. int index = 0;
  2411. if (!crtc) {
  2412. DDPPR_ERR("%s, crtc is NULL\n", __func__);
  2413. return;
  2414. }
  2415. index = drm_crtc_index(crtc);
  2416. dsi->mipi_hopping_sta = en;
  2417. if (!(ext && ext->params &&
  2418. ext->params->dyn.switch_en == 1))
  2419. return;
  2420. CRTC_MMP_EVENT_START(index, clk_change,
  2421. en, (ext->params->data_rate << 16)
  2422. | ext->params->pll_clk);
  2423. mod_vfp = !!ext->params->dyn.vfp;
  2424. mod_vbp = !!ext->params->dyn.vbp;
  2425. mod_vsa = !!ext->params->dyn.vsa;
  2426. mod_hfp = !!ext->params->dyn.hfp;
  2427. mod_hbp = !!ext->params->dyn.hbp;
  2428. mod_hsa = !!ext->params->dyn.hsa;
  2429. if (en) {
  2430. data_rate = !!ext->params->dyn.data_rate ?
  2431. ext->params->dyn.data_rate :
  2432. ext->params->dyn.pll_clk * 2;
  2433. } else {
  2434. data_rate = mtk_dsi_default_rate(dsi);
  2435. }
  2436. dsi->data_rate = data_rate;
  2437. mtk_mipi_tx_pll_rate_set_adpt(dsi->phy, data_rate);
  2438. /* implicit way for display power state */
  2439. if (dsi->clk_refcnt == 0) {
  2440. CRTC_MMP_MARK(index, clk_change, 0, 1);
  2441. goto done;
  2442. }
  2443. if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO)
  2444. mtk_crtc_pkt_create(&cmdq_handle, &mtk_crtc->base,
  2445. mtk_crtc->gce_obj.client[CLIENT_DSI_CFG]);
  2446. else
  2447. mtk_crtc_pkt_create(&cmdq_handle, &mtk_crtc->base,
  2448. mtk_crtc->gce_obj.client[CLIENT_CFG]);
  2449. if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO) {
  2450. mtk_dsi_calc_vdo_timing(dsi);
  2451. cmdq_pkt_wait_no_clear(cmdq_handle,
  2452. mtk_crtc->gce_obj.event[EVENT_VDO_EOF]);
  2453. mtk_dsi_phy_timconfig(dsi, cmdq_handle);
  2454. if (mod_hfp)
  2455. mtk_dsi_porch_setting(comp, cmdq_handle, DSI_HFP,
  2456. dsi->hfp_byte);
  2457. if (mod_hbp)
  2458. mtk_dsi_porch_setting(comp, cmdq_handle, DSI_HBP,
  2459. dsi->hbp_byte);
  2460. if (mod_hsa)
  2461. mtk_dsi_porch_setting(comp, cmdq_handle, DSI_HSA,
  2462. dsi->hsa_byte);
  2463. if (mod_vbp)
  2464. mtk_dsi_porch_setting(comp, cmdq_handle,
  2465. DSI_VBP, dsi->vbp);
  2466. if (mod_vsa)
  2467. mtk_dsi_porch_setting(comp, cmdq_handle,
  2468. DSI_VSA, dsi->vsa);
  2469. }
  2470. mtk_mipi_tx_pll_rate_switch_gce(dsi->phy, cmdq_handle, data_rate);
  2471. if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO) {
  2472. cmdq_pkt_clear_event(cmdq_handle,
  2473. mtk_crtc->gce_obj.event[EVENT_DSI0_SOF]);
  2474. cmdq_pkt_wait_no_clear(cmdq_handle,
  2475. mtk_crtc->gce_obj.event[EVENT_DSI0_SOF]);
  2476. if (mod_vfp)
  2477. mtk_dsi_porch_setting(comp, cmdq_handle,
  2478. DSI_VFP, dsi->vfp);
  2479. }
  2480. cmdq_pkt_flush(cmdq_handle);
  2481. cmdq_pkt_destroy(cmdq_handle);
  2482. done:
  2483. CRTC_MMP_EVENT_END(index, clk_change,
  2484. dsi->mode_flags,
  2485. (ext->params->dyn.data_rate << 16) |
  2486. ext->params->dyn.pll_clk);
  2487. }
  2488. static int mtk_dsi_host_attach(struct mipi_dsi_host *host,
  2489. struct mipi_dsi_device *device)
  2490. {
  2491. struct mtk_dsi *dsi = host_to_dsi(host);
  2492. dsi->lanes = device->lanes;
  2493. dsi->format = device->format;
  2494. dsi->mode_flags = device->mode_flags;
  2495. /* ********Panel Master********** */
  2496. dsi->dev_for_PM = device;
  2497. /* ******end Panel Master**** */
  2498. if (dsi->conn.dev)
  2499. drm_helper_hpd_irq_event(dsi->conn.dev);
  2500. return 0;
  2501. }
  2502. static int mtk_dsi_host_detach(struct mipi_dsi_host *host,
  2503. struct mipi_dsi_device *device)
  2504. {
  2505. struct mtk_dsi *dsi = host_to_dsi(host);
  2506. if (dsi->conn.dev)
  2507. drm_helper_hpd_irq_event(dsi->conn.dev);
  2508. return 0;
  2509. }
  2510. static u32 mtk_dsi_recv_cnt(u8 type, u8 *read_data)
  2511. {
  2512. switch (type) {
  2513. case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE:
  2514. case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
  2515. return 1;
  2516. case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE:
  2517. case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
  2518. return 2;
  2519. case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
  2520. case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
  2521. return read_data[1] + read_data[2] * 16;
  2522. case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
  2523. DDPINFO("type is 0x02, try again\n");
  2524. break;
  2525. default:
  2526. DDPINFO("type(0x%x) cannot be non-recognite\n", type);
  2527. break;
  2528. }
  2529. return 0;
  2530. }
  2531. static void mtk_dsi_cmdq(struct mtk_dsi *dsi, const struct mipi_dsi_msg *msg)
  2532. {
  2533. const char *tx_buf = msg->tx_buf;
  2534. u8 config, cmdq_size, cmdq_off, type = msg->type;
  2535. u32 reg_val, cmdq_mask, i;
  2536. unsigned long goto_addr;
  2537. if (MTK_DSI_HOST_IS_READ(type))
  2538. config = BTA;
  2539. else
  2540. config = (msg->tx_len > 2) ? LONG_PACKET : SHORT_PACKET;
  2541. if (msg->tx_len > 2) {
  2542. cmdq_size = 1 + (msg->tx_len + 3) / 4;
  2543. cmdq_off = 4;
  2544. cmdq_mask = CONFIG | DATA_ID | DATA_0 | DATA_1;
  2545. reg_val = (msg->tx_len << 16) | (type << 8) | config;
  2546. } else {
  2547. cmdq_size = 1;
  2548. cmdq_off = 2;
  2549. cmdq_mask = CONFIG | DATA_ID;
  2550. reg_val = (type << 8) | config;
  2551. }
  2552. for (i = 0; i < msg->tx_len; i++) {
  2553. goto_addr = dsi->driver_data->reg_cmdq_ofs + cmdq_off + i;
  2554. cmdq_mask = (0xFFu << ((goto_addr & 0x3u) * 8));
  2555. mtk_dsi_mask(dsi, goto_addr & (~(0x3UL)),
  2556. (0xFFu << ((goto_addr & 0x3u) * 8)),
  2557. tx_buf[i] << ((goto_addr & 0x3u) * 8));
  2558. }
  2559. if (msg->tx_len > 2)
  2560. cmdq_mask = CONFIG | DATA_ID | DATA_0 | DATA_1;
  2561. else
  2562. cmdq_mask = CONFIG | DATA_ID;
  2563. mtk_dsi_mask(dsi, dsi->driver_data->reg_cmdq_ofs, cmdq_mask, reg_val);
  2564. mtk_dsi_mask(dsi, DSI_CMDQ_SIZE, CMDQ_SIZE, cmdq_size);
  2565. }
  2566. static void build_vm_cmdq(struct mtk_dsi *dsi,
  2567. const struct mipi_dsi_msg *msg, struct cmdq_pkt *handle)
  2568. {
  2569. unsigned int i = 0, j = 0, k;
  2570. const char *tx_buf = msg->tx_buf;
  2571. while (i < msg->tx_len) {
  2572. unsigned int vm_cmd_val = 0;
  2573. unsigned int vm_cmd_addr = 0;
  2574. k = (((j + 4) > msg->tx_len) ? (msg->tx_len) : (j + 4));
  2575. for (j = i; j < k; j++)
  2576. vm_cmd_val += (tx_buf[j] << ((j - i) * 8));
  2577. if (i / 16 == 0)
  2578. vm_cmd_addr = DSI_VM_CMD_DATA0 + (i%16);
  2579. if (i / 16 == 1)
  2580. vm_cmd_addr = DSI_VM_CMD_DATA10 + (i%16);
  2581. if (i / 16 == 2)
  2582. vm_cmd_addr = DSI_VM_CMD_DATA20 + (i%16);
  2583. if (i / 16 == 3)
  2584. vm_cmd_addr = DSI_VM_CMD_DATA30 + (i%16);
  2585. if (handle)
  2586. cmdq_pkt_write(handle, dsi->ddp_comp.cmdq_base,
  2587. dsi->ddp_comp.regs_pa + vm_cmd_addr,
  2588. vm_cmd_val, ~0);
  2589. else
  2590. writel(vm_cmd_val, dsi->regs + vm_cmd_addr);
  2591. i += 4;
  2592. }
  2593. }
  2594. static void mtk_dsi_vm_cmdq(struct mtk_dsi *dsi,
  2595. const struct mipi_dsi_msg *msg, struct cmdq_pkt *handle)
  2596. {
  2597. const char *tx_buf = msg->tx_buf;
  2598. u8 config, type = msg->type;
  2599. u32 reg_val;
  2600. config = (msg->tx_len > 2) ? VM_LONG_PACKET : 0;
  2601. if (msg->tx_len > 2) {
  2602. build_vm_cmdq(dsi, msg, handle);
  2603. reg_val = (msg->tx_len << 16) | (type << 8) | config;
  2604. } else if (msg->tx_len == 2) {
  2605. reg_val = (tx_buf[1] << 24) | (tx_buf[0] << 16) |
  2606. (type << 8) | config;
  2607. } else {
  2608. reg_val = (tx_buf[0] << 16) | (type << 8) | config;
  2609. }
  2610. reg_val |= (VM_CMD_EN + TS_VFP_EN);
  2611. if (handle == NULL)
  2612. writel(reg_val, dsi->regs + DSI_VM_CMD_CON);
  2613. else
  2614. cmdq_pkt_write(handle, dsi->ddp_comp.cmdq_base,
  2615. dsi->ddp_comp.regs_pa + DSI_VM_CMD_CON, reg_val, ~0);
  2616. }
  2617. static void mtk_dsi_cmdq_gce(struct mtk_dsi *dsi, struct cmdq_pkt *handle,
  2618. const struct mipi_dsi_msg *msg)
  2619. {
  2620. const char *tx_buf = msg->tx_buf;
  2621. u8 config, cmdq_size, cmdq_off, type = msg->type;
  2622. u32 reg_val, cmdq_mask, i;
  2623. unsigned long goto_addr;
  2624. if (MTK_DSI_HOST_IS_READ(type))
  2625. config = BTA;
  2626. else
  2627. config = (msg->tx_len > 2) ? LONG_PACKET : SHORT_PACKET;
  2628. if (msg->tx_len > 2) {
  2629. cmdq_size = 1 + (msg->tx_len + 3) / 4;
  2630. cmdq_off = 4;
  2631. cmdq_mask = CONFIG | DATA_ID | DATA_0 | DATA_1;
  2632. reg_val = (msg->tx_len << 16) | (type << 8) | config;
  2633. } else {
  2634. cmdq_size = 1;
  2635. cmdq_off = 2;
  2636. cmdq_mask = CONFIG | DATA_ID;
  2637. reg_val = (type << 8) | config;
  2638. }
  2639. for (i = 0; i < msg->tx_len; i++) {
  2640. goto_addr = dsi->driver_data->reg_cmdq_ofs + cmdq_off + i;
  2641. cmdq_mask = (0xFFu << ((goto_addr & 0x3u) * 8));
  2642. mtk_ddp_write_mask(&dsi->ddp_comp,
  2643. tx_buf[i] << ((goto_addr & 0x3u) * 8),
  2644. goto_addr, (0xFFu << ((goto_addr & 0x3u) * 8)),
  2645. handle);
  2646. DDPINFO("set cmdqaddr %lx, val:%x, mask %x\n", goto_addr,
  2647. tx_buf[i] << ((goto_addr & 0x3u) * 8),
  2648. (0xFFu << ((goto_addr & 0x3u) * 8)));
  2649. }
  2650. if (msg->tx_len > 2)
  2651. cmdq_mask = CONFIG | DATA_ID | DATA_0 | DATA_1;
  2652. else
  2653. cmdq_mask = CONFIG | DATA_ID;
  2654. mtk_ddp_write_mask(&dsi->ddp_comp, reg_val,
  2655. dsi->driver_data->reg_cmdq_ofs,
  2656. cmdq_mask, handle);
  2657. DDPINFO("set cmdqaddr %u, val:%x, mask %x\n",
  2658. dsi->driver_data->reg_cmdq_ofs,
  2659. reg_val,
  2660. cmdq_mask);
  2661. mtk_ddp_write_mask(&dsi->ddp_comp, cmdq_size,
  2662. DSI_CMDQ_SIZE, CMDQ_SIZE, handle);
  2663. DDPINFO("set cmdqaddr %u, val:%x, mask %x\n", DSI_CMDQ_SIZE, cmdq_size,
  2664. CMDQ_SIZE);
  2665. }
  2666. static void mtk_dsi_cmdq_grp_gce(struct mtk_dsi *dsi, struct cmdq_pkt *handle,
  2667. struct mtk_panel_para_table *para_table,
  2668. unsigned int para_size)
  2669. {
  2670. struct mipi_dsi_msg msg;
  2671. const char *tx_buf;
  2672. u8 config, cmdq_off, type;
  2673. u8 cmdq_size, total_cmdq_size = 0;
  2674. u8 start_off = 0;
  2675. u32 reg_val, cmdq_val;
  2676. u32 cmdq_mask, i, j;
  2677. unsigned int base_addr;
  2678. struct mtk_ddp_comp *comp = &dsi->ddp_comp;
  2679. const u32 reg_cmdq_ofs = dsi->driver_data->reg_cmdq_ofs;
  2680. for (j = 0; j < para_size; j++) {
  2681. msg.tx_buf = para_table[j].para_list,
  2682. msg.tx_len = para_table[j].count;
  2683. switch (msg.tx_len) {
  2684. case 0:
  2685. continue;
  2686. case 1:
  2687. msg.type = MIPI_DSI_DCS_SHORT_WRITE;
  2688. break;
  2689. case 2:
  2690. msg.type = MIPI_DSI_DCS_SHORT_WRITE_PARAM;
  2691. break;
  2692. default:
  2693. msg.type = MIPI_DSI_DCS_LONG_WRITE;
  2694. break;
  2695. }
  2696. tx_buf = msg.tx_buf;
  2697. type = msg.type;
  2698. if (MTK_DSI_HOST_IS_READ(type))
  2699. config = BTA;
  2700. else
  2701. config = (msg.tx_len > 2) ? LONG_PACKET : SHORT_PACKET;
  2702. if (msg.tx_len > 2) {
  2703. cmdq_off = 4;
  2704. cmdq_mask = CONFIG | DATA_ID | DATA_0 | DATA_1;
  2705. reg_val = (msg.tx_len << 16) | (type << 8) | config;
  2706. mtk_ddp_write_relaxed(comp, reg_val,
  2707. reg_cmdq_ofs + start_off,
  2708. handle);
  2709. DDPINFO("set cmdq addr %x, val:%x\n",
  2710. reg_cmdq_ofs + start_off,
  2711. reg_val);
  2712. reg_val = 0;
  2713. for (i = 0; i < msg.tx_len; i++) {
  2714. cmdq_val = tx_buf[i] << ((i & 0x3u) * 8);
  2715. cmdq_mask = (0xFFu << ((i & 0x3u) * 8));
  2716. reg_val = reg_val | (cmdq_val & cmdq_mask);
  2717. if (((i & 0x3) == 0x3) ||
  2718. (i == (msg.tx_len - 1))) {
  2719. base_addr = reg_cmdq_ofs + start_off +
  2720. cmdq_off + ((i / 4) * 4);
  2721. mtk_ddp_write_relaxed(comp,
  2722. reg_val,
  2723. base_addr,
  2724. handle);
  2725. DDPINFO("set cmdq addr %x, val:%x\n",
  2726. base_addr,
  2727. reg_val);
  2728. reg_val = 0;
  2729. }
  2730. }
  2731. } else {
  2732. cmdq_off = 2;
  2733. cmdq_mask = CONFIG | DATA_ID;
  2734. reg_val = (type << 8) | config;
  2735. for (i = 0; i < msg.tx_len; i++) {
  2736. cmdq_val = tx_buf[i] << ((i & 0x3u) * 8);
  2737. cmdq_mask = (0xFFu << ((i & 0x3u) * 8));
  2738. reg_val = reg_val | (cmdq_val & cmdq_mask);
  2739. if (i == (msg.tx_len - 1)) {
  2740. base_addr = reg_cmdq_ofs + start_off +
  2741. cmdq_off + (i / 4) * 4;
  2742. mtk_ddp_write_relaxed(comp,
  2743. reg_val,
  2744. base_addr,
  2745. handle);
  2746. DDPINFO("set cmdq addr %x, val:%x\n",
  2747. base_addr,
  2748. reg_val);
  2749. reg_val = 0;
  2750. }
  2751. }
  2752. }
  2753. if (msg.tx_len > 2)
  2754. cmdq_size = 1 + ((msg.tx_len + 3) / 4);
  2755. else
  2756. cmdq_size = 1;
  2757. start_off += (cmdq_size * 4);
  2758. total_cmdq_size += cmdq_size;
  2759. DDPINFO("offset:%d, size:%d\n", start_off, cmdq_size);
  2760. }
  2761. mtk_ddp_write_mask(comp, total_cmdq_size,
  2762. DSI_CMDQ_SIZE, CMDQ_SIZE, handle);
  2763. mtk_ddp_write_relaxed(comp, 0x0, DSI_START, handle);
  2764. mtk_ddp_write_relaxed(comp, 0x1, DSI_START, handle);
  2765. mtk_dsi_cmdq_poll(comp, handle, comp->regs_pa + DSI_INTSTA,
  2766. CMD_DONE_INT_FLAG, CMD_DONE_INT_FLAG);
  2767. mtk_ddp_write_mask(comp, 0x0, DSI_INTSTA, CMD_DONE_INT_FLAG,
  2768. handle);
  2769. DDPINFO("set cmdqaddr %x, val:%d, mask %x\n", DSI_CMDQ_SIZE,
  2770. total_cmdq_size,
  2771. CMDQ_SIZE);
  2772. }
  2773. void mipi_dsi_dcs_write_gce(struct mtk_dsi *dsi, struct cmdq_pkt *handle,
  2774. const void *data, size_t len)
  2775. {
  2776. struct mipi_dsi_msg msg = {
  2777. .tx_buf = data,
  2778. .tx_len = len
  2779. };
  2780. switch (len) {
  2781. case 0:
  2782. return;
  2783. case 1:
  2784. msg.type = MIPI_DSI_DCS_SHORT_WRITE;
  2785. break;
  2786. case 2:
  2787. msg.type = MIPI_DSI_DCS_SHORT_WRITE_PARAM;
  2788. break;
  2789. default:
  2790. msg.type = MIPI_DSI_DCS_LONG_WRITE;
  2791. break;
  2792. }
  2793. if (mtk_dsi_is_cmd_mode(&dsi->ddp_comp)) {
  2794. mtk_dsi_poll_for_idle(dsi, handle);
  2795. mtk_dsi_cmdq_gce(dsi, handle, &msg);
  2796. cmdq_pkt_write(handle, dsi->ddp_comp.cmdq_base,
  2797. dsi->ddp_comp.regs_pa + DSI_START, 0x0, ~0);
  2798. cmdq_pkt_write(handle, dsi->ddp_comp.cmdq_base,
  2799. dsi->ddp_comp.regs_pa + DSI_START, 0x1, ~0);
  2800. mtk_dsi_poll_for_idle(dsi, handle);
  2801. } else {
  2802. /* set BL cmd */
  2803. mtk_dsi_vm_cmdq(dsi, &msg, handle);
  2804. /* clear VM_CMD_DONE */
  2805. cmdq_pkt_write(handle, dsi->ddp_comp.cmdq_base,
  2806. dsi->ddp_comp.regs_pa + DSI_INTSTA, 0,
  2807. VM_CMD_DONE_INT_EN);
  2808. /* start to send VM cmd */
  2809. cmdq_pkt_write(handle, dsi->ddp_comp.cmdq_base,
  2810. dsi->ddp_comp.regs_pa + DSI_START, 0,
  2811. VM_CMD_START);
  2812. cmdq_pkt_write(handle, dsi->ddp_comp.cmdq_base,
  2813. dsi->ddp_comp.regs_pa + DSI_START, VM_CMD_START,
  2814. VM_CMD_START);
  2815. /* poll VM cmd done */
  2816. mtk_dsi_cmdq_poll(&dsi->ddp_comp, handle,
  2817. dsi->ddp_comp.regs_pa + DSI_INTSTA,
  2818. VM_CMD_DONE_INT_EN, VM_CMD_DONE_INT_EN);
  2819. }
  2820. }
  2821. void mipi_dsi_dcs_write_gce_dyn(struct mtk_dsi *dsi, struct cmdq_pkt *handle,
  2822. const void *data, size_t len)
  2823. {
  2824. struct mipi_dsi_msg msg = {
  2825. .tx_buf = data,
  2826. .tx_len = len
  2827. };
  2828. switch (len) {
  2829. case 0:
  2830. return;
  2831. case 1:
  2832. msg.type = MIPI_DSI_DCS_SHORT_WRITE;
  2833. break;
  2834. case 2:
  2835. msg.type = MIPI_DSI_DCS_SHORT_WRITE_PARAM;
  2836. break;
  2837. default:
  2838. msg.type = MIPI_DSI_DCS_LONG_WRITE;
  2839. break;
  2840. }
  2841. mtk_dsi_poll_for_idle(dsi, handle);
  2842. mtk_dsi_cmdq_gce(dsi, handle, &msg);
  2843. cmdq_pkt_write(handle, dsi->ddp_comp.cmdq_base,
  2844. dsi->ddp_comp.regs_pa + DSI_START, 0x0, ~0);
  2845. cmdq_pkt_write(handle, dsi->ddp_comp.cmdq_base,
  2846. dsi->ddp_comp.regs_pa + DSI_START, 0x1, ~0);
  2847. mtk_dsi_poll_for_idle(dsi, handle);
  2848. }
  2849. void mipi_dsi_dcs_write_gce2(struct mtk_dsi *dsi, struct cmdq_pkt *dummy,
  2850. const void *data, size_t len)
  2851. {
  2852. struct cmdq_pkt *handle;
  2853. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(dsi->encoder.crtc);
  2854. int dsi_mode = readl(dsi->regs + DSI_MODE_CTRL);
  2855. struct mipi_dsi_msg msg = {
  2856. .tx_buf = data,
  2857. .tx_len = len
  2858. };
  2859. switch (len) {
  2860. case 0:
  2861. return;
  2862. case 1:
  2863. msg.type = MIPI_DSI_DCS_SHORT_WRITE;
  2864. break;
  2865. case 2:
  2866. msg.type = MIPI_DSI_DCS_SHORT_WRITE_PARAM;
  2867. break;
  2868. default:
  2869. msg.type = MIPI_DSI_DCS_LONG_WRITE;
  2870. break;
  2871. }
  2872. if (dsi_mode == 0) {
  2873. mtk_crtc_pkt_create(&handle, &mtk_crtc->base,
  2874. mtk_crtc->gce_obj.client[CLIENT_CFG]);
  2875. mtk_dsi_poll_for_idle(dsi, handle);
  2876. mtk_dsi_cmdq_gce(dsi, handle, &msg);
  2877. cmdq_pkt_write(handle, dsi->ddp_comp.cmdq_base,
  2878. dsi->ddp_comp.regs_pa + DSI_START, 0x0, ~0);
  2879. cmdq_pkt_write(handle, dsi->ddp_comp.cmdq_base,
  2880. dsi->ddp_comp.regs_pa + DSI_START, 0x1, ~0);
  2881. mtk_dsi_poll_for_idle(dsi, handle);
  2882. } else {
  2883. mtk_crtc_pkt_create(&handle, &mtk_crtc->base,
  2884. mtk_crtc->gce_obj.client[CLIENT_DSI_CFG]);
  2885. /* build VM cmd */
  2886. mtk_dsi_vm_cmdq(dsi, &msg, handle);
  2887. /* clear VM_CMD_DONE */
  2888. cmdq_pkt_write(handle, dsi->ddp_comp.cmdq_base,
  2889. dsi->ddp_comp.regs_pa + DSI_INTSTA, 0,
  2890. VM_CMD_DONE_INT_EN);
  2891. /* start to send VM cmd */
  2892. cmdq_pkt_write(handle, dsi->ddp_comp.cmdq_base,
  2893. dsi->ddp_comp.regs_pa + DSI_START, 0,
  2894. VM_CMD_START);
  2895. cmdq_pkt_write(handle, dsi->ddp_comp.cmdq_base,
  2896. dsi->ddp_comp.regs_pa + DSI_START, VM_CMD_START,
  2897. VM_CMD_START);
  2898. /* poll VM cmd done */
  2899. mtk_dsi_cmdq_poll(&dsi->ddp_comp, handle,
  2900. dsi->ddp_comp.regs_pa + DSI_INTSTA,
  2901. VM_CMD_DONE_INT_EN, VM_CMD_DONE_INT_EN);
  2902. cmdq_pkt_write(handle, dsi->ddp_comp.cmdq_base,
  2903. dsi->ddp_comp.regs_pa + DSI_START, 0,
  2904. VM_CMD_START);
  2905. /* clear VM_CMD_DONE */
  2906. cmdq_pkt_write(handle, dsi->ddp_comp.cmdq_base,
  2907. dsi->ddp_comp.regs_pa + DSI_INTSTA, 0,
  2908. VM_CMD_DONE_INT_EN);
  2909. }
  2910. cmdq_pkt_flush(handle);
  2911. cmdq_pkt_destroy(handle);
  2912. }
  2913. void mipi_dsi_dcs_grp_write_gce(struct mtk_dsi *dsi, struct cmdq_pkt *handle,
  2914. struct mtk_panel_para_table *para_table,
  2915. unsigned int para_size)
  2916. {
  2917. struct mtk_ddp_comp *comp = &dsi->ddp_comp;
  2918. /* wait DSI idle */
  2919. if (!mtk_dsi_is_cmd_mode(comp)) {
  2920. _mtk_dsi_set_mode(comp, handle, CMD_MODE);
  2921. cmdq_pkt_write(handle, comp->cmdq_base,
  2922. comp->regs_pa + DSI_START, 0, ~0);
  2923. mtk_dsi_cmdq_poll(comp, handle,
  2924. comp->regs_pa + DSI_INTSTA, 0,
  2925. DSI_BUSY);
  2926. }
  2927. mtk_dsi_cmdq_grp_gce(dsi, handle, para_table, para_size);
  2928. /* trigger */
  2929. if (!mtk_dsi_is_cmd_mode(comp)) {
  2930. mtk_dsi_start_vdo_mode(comp, handle);
  2931. mtk_disp_mutex_trigger(comp->mtk_crtc->mutex[0], handle);
  2932. mtk_dsi_trigger(comp, handle);
  2933. }
  2934. }
  2935. static void _mtk_mipi_dsi_write_gce(struct mtk_dsi *dsi,
  2936. struct cmdq_pkt *handle,
  2937. const struct mipi_dsi_msg *msg)
  2938. {
  2939. const char *tx_buf = msg->tx_buf;
  2940. u8 config, cmdq_size, cmdq_off, type = msg->type;
  2941. u32 reg_val, cmdq_mask, i;
  2942. unsigned long goto_addr;
  2943. DDPMSG("%s +\n", __func__);
  2944. if (MTK_DSI_HOST_IS_READ(type))
  2945. config = BTA;
  2946. else
  2947. config = (msg->tx_len > 2) ? LONG_PACKET : SHORT_PACKET;
  2948. if (!(msg->flags & MIPI_DSI_MSG_USE_LPM))
  2949. config |= HSTX;
  2950. if (msg->tx_len > 2) {
  2951. cmdq_size = 1 + (msg->tx_len + 3) / 4;
  2952. cmdq_off = 4;
  2953. cmdq_mask = CONFIG | DATA_ID | DATA_0 | DATA_1;
  2954. reg_val = (msg->tx_len << 16) | (type << 8) | config;
  2955. } else {
  2956. cmdq_size = 1;
  2957. cmdq_off = 2;
  2958. cmdq_mask = CONFIG | DATA_ID;
  2959. reg_val = (type << 8) | config;
  2960. }
  2961. for (i = 0; i < msg->tx_len; i++) {
  2962. goto_addr = dsi->driver_data->reg_cmdq_ofs + cmdq_off + i;
  2963. cmdq_mask = (0xFFu << ((goto_addr & 0x3u) * 8));
  2964. mtk_ddp_write_mask(&dsi->ddp_comp,
  2965. tx_buf[i] << ((goto_addr & 0x3u) * 8),
  2966. goto_addr, (0xFFu << ((goto_addr & 0x3u) * 8)),
  2967. handle);
  2968. DDPINFO("set cmdqaddr %lx, val:%x, mask %x\n", goto_addr,
  2969. tx_buf[i] << ((goto_addr & 0x3u) * 8),
  2970. (0xFFu << ((goto_addr & 0x3u) * 8)));
  2971. }
  2972. if (msg->tx_len > 2)
  2973. cmdq_mask = CONFIG | DATA_ID | DATA_0 | DATA_1;
  2974. else
  2975. cmdq_mask = CONFIG | DATA_ID;
  2976. mtk_ddp_write_mask(&dsi->ddp_comp, reg_val,
  2977. dsi->driver_data->reg_cmdq_ofs,
  2978. cmdq_mask, handle);
  2979. DDPINFO("set cmdqaddr %u, val:%x, mask %x\n",
  2980. dsi->driver_data->reg_cmdq_ofs,
  2981. reg_val,
  2982. cmdq_mask);
  2983. mtk_ddp_write_mask(&dsi->ddp_comp, cmdq_size,
  2984. DSI_CMDQ_SIZE, CMDQ_SIZE, handle);
  2985. DDPINFO("set cmdqaddr %u, val:%x, mask %x\n", DSI_CMDQ_SIZE, cmdq_size,
  2986. CMDQ_SIZE);
  2987. DDPMSG("%s -\n", __func__);
  2988. }
  2989. int mtk_mipi_dsi_write_gce(struct mtk_dsi *dsi,
  2990. struct cmdq_pkt *handle,
  2991. struct mtk_drm_crtc *mtk_crtc,
  2992. struct mtk_ddic_dsi_msg *cmd_msg)
  2993. {
  2994. unsigned int i = 0, j = 0;
  2995. int dsi_mode = readl(dsi->regs + DSI_MODE_CTRL) & MODE;
  2996. struct mipi_dsi_msg msg;
  2997. unsigned int use_lpm = cmd_msg->flags & MIPI_DSI_MSG_USE_LPM;
  2998. struct mtk_ddp_comp *comp = &dsi->ddp_comp;
  2999. DDPMSG("%s +\n", __func__);
  3000. /* Check cmd_msg param */
  3001. if (cmd_msg->type == 0 ||
  3002. cmd_msg->tx_cmd_num == 0 ||
  3003. cmd_msg->tx_cmd_num > MAX_TX_CMD_NUM) {
  3004. DDPPR_ERR("%s: type is %s, tx_cmd_num is %d\n",
  3005. __func__, cmd_msg->type, (int)cmd_msg->tx_cmd_num);
  3006. return -EINVAL;
  3007. }
  3008. for (i = 0; i < cmd_msg->tx_cmd_num; i++) {
  3009. if (cmd_msg->tx_buf[i] == 0 || cmd_msg->tx_len[i] == 0) {
  3010. DDPPR_ERR("%s: tx_buf[%d] is %s, tx_len[%d] is %d\n",
  3011. __func__, i, (char *)cmd_msg->tx_buf[i], i,
  3012. (int)cmd_msg->tx_len[i]);
  3013. return -EINVAL;
  3014. }
  3015. }
  3016. /* Debug info */
  3017. DDPINFO("%s: channel=%d, flags=0x%x, tx_cmd_num=%d\n",
  3018. __func__, cmd_msg->channel,
  3019. cmd_msg->flags, (int)cmd_msg->tx_cmd_num);
  3020. for (i = 0; i < cmd_msg->tx_cmd_num; i++) {
  3021. DDPINFO("type[%d]=0x%x, tx_len[%d]=%d\n",
  3022. i, cmd_msg->type[i], i, (int)cmd_msg->tx_len[i]);
  3023. for (j = 0; j < cmd_msg->tx_len[i]; j++) {
  3024. DDPINFO("tx_buf[%d]--byte:%d,val:0x%x\n",
  3025. i, j, *(char *)(cmd_msg->tx_buf[i] + j));
  3026. }
  3027. }
  3028. msg.channel = cmd_msg->channel;
  3029. msg.flags = cmd_msg->flags;
  3030. if (dsi_mode == 0) { /* CMD mode HS/LP */
  3031. for (i = 0; i < cmd_msg->tx_cmd_num; i++) {
  3032. msg.type = cmd_msg->type[i];
  3033. msg.tx_len = cmd_msg->tx_len[i];
  3034. msg.tx_buf = cmd_msg->tx_buf[i];
  3035. mtk_dsi_poll_for_idle(dsi, handle);
  3036. _mtk_mipi_dsi_write_gce(dsi, handle, &msg);
  3037. cmdq_pkt_write(handle, dsi->ddp_comp.cmdq_base,
  3038. dsi->ddp_comp.regs_pa + DSI_START, 0x0, ~0);
  3039. cmdq_pkt_write(handle, dsi->ddp_comp.cmdq_base,
  3040. dsi->ddp_comp.regs_pa + DSI_START, 0x1, ~0);
  3041. mtk_dsi_poll_for_idle(dsi, handle);
  3042. }
  3043. } else if (dsi_mode != 0 && !use_lpm) { /* VDO with VM_CMD */
  3044. for (i = 0; i < cmd_msg->tx_cmd_num; i++) {
  3045. msg.type = cmd_msg->type[i];
  3046. msg.tx_len = cmd_msg->tx_len[i];
  3047. msg.tx_buf = cmd_msg->tx_buf[i];
  3048. /* build VM cmd */
  3049. mtk_dsi_vm_cmdq(dsi, &msg, handle);
  3050. /* clear VM_CMD_DONE */
  3051. cmdq_pkt_write(handle, dsi->ddp_comp.cmdq_base,
  3052. dsi->ddp_comp.regs_pa + DSI_INTSTA, 0,
  3053. VM_CMD_DONE_INT_EN);
  3054. /* start to send VM cmd */
  3055. cmdq_pkt_write(handle, dsi->ddp_comp.cmdq_base,
  3056. dsi->ddp_comp.regs_pa + DSI_START, 0,
  3057. VM_CMD_START);
  3058. cmdq_pkt_write(handle, dsi->ddp_comp.cmdq_base,
  3059. dsi->ddp_comp.regs_pa + DSI_START, VM_CMD_START,
  3060. VM_CMD_START);
  3061. /* poll VM cmd done */
  3062. mtk_dsi_cmdq_poll(&dsi->ddp_comp, handle,
  3063. dsi->ddp_comp.regs_pa + DSI_INTSTA,
  3064. VM_CMD_DONE_INT_EN, VM_CMD_DONE_INT_EN);
  3065. cmdq_pkt_write(handle, dsi->ddp_comp.cmdq_base,
  3066. dsi->ddp_comp.regs_pa + DSI_START, 0,
  3067. VM_CMD_START);
  3068. /* clear VM_CMD_DONE */
  3069. cmdq_pkt_write(handle, dsi->ddp_comp.cmdq_base,
  3070. dsi->ddp_comp.regs_pa + DSI_INTSTA, 0,
  3071. VM_CMD_DONE_INT_EN);
  3072. }
  3073. } else if (dsi_mode != 0 && use_lpm) { /* VDO to CMD with LP */
  3074. mtk_dsi_stop_vdo_mode(dsi, handle);
  3075. for (i = 0; i < cmd_msg->tx_cmd_num; i++) {
  3076. msg.type = cmd_msg->type[i];
  3077. msg.tx_len = cmd_msg->tx_len[i];
  3078. msg.tx_buf = cmd_msg->tx_buf[i];
  3079. mtk_dsi_poll_for_idle(dsi, handle);
  3080. _mtk_mipi_dsi_write_gce(dsi, handle, &msg);
  3081. cmdq_pkt_write(handle, dsi->ddp_comp.cmdq_base,
  3082. dsi->ddp_comp.regs_pa + DSI_START, 0x0, ~0);
  3083. cmdq_pkt_write(handle, dsi->ddp_comp.cmdq_base,
  3084. dsi->ddp_comp.regs_pa + DSI_START, 0x1, ~0);
  3085. mtk_dsi_poll_for_idle(dsi, handle);
  3086. }
  3087. mtk_dsi_start_vdo_mode(comp, handle);
  3088. mtk_disp_mutex_trigger(comp->mtk_crtc->mutex[0], handle);
  3089. mtk_dsi_trigger(comp, handle);
  3090. }
  3091. DDPMSG("%s -\n", __func__);
  3092. return 0;
  3093. }
  3094. static void _mtk_mipi_dsi_read_gce(struct mtk_dsi *dsi,
  3095. struct cmdq_pkt *handle,
  3096. struct mipi_dsi_msg *msg)
  3097. {
  3098. struct mtk_ddp_comp *comp = &dsi->ddp_comp;
  3099. struct mtk_drm_crtc *mtk_crtc = dsi->ddp_comp.mtk_crtc;
  3100. struct DSI_T0_INS t0, t1;
  3101. dma_addr_t read_slot = mtk_crtc->gce_obj.buf.pa_base +
  3102. DISP_SLOT_READ_DDIC_BASE;
  3103. const char *tx_buf = msg->tx_buf;
  3104. DDPMSG("%s +\n", __func__);
  3105. DDPINFO("%s type=0x%x, tx_len=%d, tx_buf[0]=0x%x, rx_len=%d\n",
  3106. __func__, msg->type, (int)msg->tx_len,
  3107. tx_buf[0], (int)msg->rx_len);
  3108. if (msg->tx_len > 2) {
  3109. DDPPR_ERR("%s: msg->tx_len is more than 2\n", __func__);
  3110. goto done;
  3111. }
  3112. t0.CONFG = 0x00;
  3113. t0.Data_ID = 0x37;
  3114. t0.Data0 = msg->rx_len;
  3115. t0.Data1 = 0;
  3116. t1.CONFG = BTA;
  3117. t1.Data_ID = msg->type;
  3118. t1.Data0 = tx_buf[0];
  3119. if (msg->tx_len == 2)
  3120. t1.Data1 = tx_buf[1];
  3121. else
  3122. t1.Data1 = 0;
  3123. cmdq_pkt_write(handle, comp->cmdq_base, comp->regs_pa + DSI_CMDQ0,
  3124. AS_UINT32(&t0), ~0);
  3125. cmdq_pkt_write(handle, comp->cmdq_base, comp->regs_pa + DSI_CMDQ1,
  3126. AS_UINT32(&t1), ~0);
  3127. cmdq_pkt_write(handle, comp->cmdq_base, comp->regs_pa + DSI_CMDQ_SIZE,
  3128. 0x2, ~0);
  3129. cmdq_pkt_write(handle, comp->cmdq_base, comp->regs_pa + DSI_START,
  3130. 0x0, ~0);
  3131. cmdq_pkt_write(handle, comp->cmdq_base, comp->regs_pa + DSI_START,
  3132. 0x1, ~0);
  3133. mtk_dsi_cmdq_poll(comp, handle, comp->regs_pa + DSI_INTSTA, 0x1, 0x1);
  3134. cmdq_pkt_write(handle, comp->cmdq_base, comp->regs_pa + DSI_INTSTA,
  3135. 0x0, 0x1);
  3136. cmdq_pkt_mem_move(handle, comp->cmdq_base,
  3137. comp->regs_pa + DSI_RX_DATA0, read_slot,
  3138. CMDQ_THR_SPR_IDX3);
  3139. cmdq_pkt_mem_move(handle, comp->cmdq_base,
  3140. comp->regs_pa + DSI_RX_DATA1, read_slot + 1 * 0x4,
  3141. CMDQ_THR_SPR_IDX3);
  3142. cmdq_pkt_mem_move(handle, comp->cmdq_base,
  3143. comp->regs_pa + DSI_RX_DATA2, read_slot + 2 * 0x4,
  3144. CMDQ_THR_SPR_IDX3);
  3145. cmdq_pkt_mem_move(handle, comp->cmdq_base,
  3146. comp->regs_pa + DSI_RX_DATA3, read_slot + 3 * 0x4,
  3147. CMDQ_THR_SPR_IDX3);
  3148. cmdq_pkt_write(handle, comp->cmdq_base, comp->regs_pa + DSI_RACK,
  3149. 0x1, 0x1);
  3150. cmdq_pkt_write(handle, comp->cmdq_base, comp->regs_pa + DSI_INTSTA,
  3151. 0x0, 0x1);
  3152. mtk_dsi_poll_for_idle(dsi, handle);
  3153. done:
  3154. DDPMSG("%s -\n", __func__);
  3155. }
  3156. static unsigned int read_ddic_chk_sta;
  3157. static void ddic_read_timeout_cb(struct cmdq_cb_data data)
  3158. {
  3159. struct drm_crtc *crtc = data.data;
  3160. if (!crtc) {
  3161. DDPPR_ERR("%s find crtc fail\n", __func__);
  3162. return;
  3163. }
  3164. DDPPR_ERR("%s flush fail\n", __func__);
  3165. read_ddic_chk_sta = 0xff;
  3166. mtk_drm_crtc_analysis(crtc);
  3167. mtk_drm_crtc_dump(crtc);
  3168. }
  3169. int mtk_mipi_dsi_read_gce(struct mtk_dsi *dsi,
  3170. struct cmdq_pkt *handle,
  3171. struct mtk_drm_crtc *mtk_crtc,
  3172. struct mtk_ddic_dsi_msg *cmd_msg)
  3173. {
  3174. unsigned int i = 0, j = 0;
  3175. int dsi_mode = readl(dsi->regs + DSI_MODE_CTRL) & MODE;
  3176. struct drm_crtc *crtc = &mtk_crtc->base;
  3177. struct mipi_dsi_msg msg;
  3178. struct mtk_ddp_comp *comp = &dsi->ddp_comp;
  3179. struct cmdq_pkt *cmdq_handle, *cmdq_handle2;
  3180. int ret = 0;
  3181. struct DSI_RX_DATA_REG read_data0 = {0, 0, 0, 0};
  3182. struct DSI_RX_DATA_REG read_data1 = {0, 0, 0, 0};
  3183. struct DSI_RX_DATA_REG read_data2 = {0, 0, 0, 0};
  3184. struct DSI_RX_DATA_REG read_data3 = {0, 0, 0, 0};
  3185. unsigned char packet_type;
  3186. unsigned int recv_data_cnt = 0;
  3187. DDPMSG("%s +\n", __func__);
  3188. /* Check cmd_msg param */
  3189. if (cmd_msg->type == 0 ||
  3190. cmd_msg->tx_cmd_num == 0 ||
  3191. cmd_msg->rx_cmd_num == 0 ||
  3192. cmd_msg->tx_cmd_num > MAX_TX_CMD_NUM ||
  3193. cmd_msg->rx_cmd_num > MAX_RX_CMD_NUM) {
  3194. DDPPR_ERR(
  3195. "%s: type is %s, tx_cmd_num is %d, rx_cmd_num is %d\n",
  3196. __func__, cmd_msg->type,
  3197. (int)cmd_msg->tx_cmd_num, (int)cmd_msg->rx_cmd_num);
  3198. return -EINVAL;
  3199. }
  3200. if (cmd_msg->tx_cmd_num != cmd_msg->rx_cmd_num) {
  3201. DDPPR_ERR("%s: tx_cmd_num is %d, rx_cmd_num is %d\n",
  3202. __func__, (int)cmd_msg->tx_cmd_num,
  3203. (int)cmd_msg->rx_cmd_num);
  3204. return -EINVAL;
  3205. }
  3206. for (i = 0; i < cmd_msg->tx_cmd_num; i++) {
  3207. if (cmd_msg->tx_buf[i] == 0 || cmd_msg->tx_len[i] == 0) {
  3208. DDPPR_ERR("%s: tx_buf[%d] is %s, tx_len[%d] is %d\n",
  3209. __func__, i, (char *)cmd_msg->tx_buf[i], i,
  3210. (int)cmd_msg->tx_len[i]);
  3211. return -EINVAL;
  3212. }
  3213. }
  3214. for (i = 0; i < cmd_msg->rx_cmd_num; i++) {
  3215. if (cmd_msg->rx_buf[i] == 0 || cmd_msg->rx_len[i] == 0) {
  3216. DDPPR_ERR("%s: rx_buf[%d] is %s, rx_len[%d] is %d\n",
  3217. __func__, i, (char *)cmd_msg->rx_buf[i], i,
  3218. (int)cmd_msg->rx_len[i]);
  3219. return -EINVAL;
  3220. }
  3221. if (cmd_msg->rx_len[i] > RT_MAX_NUM) {
  3222. DDPPR_ERR("%s: only supprt read 10 bytes params\n",
  3223. __func__);
  3224. cmd_msg->rx_len[i] = RT_MAX_NUM;
  3225. }
  3226. }
  3227. /* Debug info */
  3228. DDPINFO("%s: channel=%d, flags=0x%x, tx_cmd_num=%d, rx_cmd_num=%d\n",
  3229. __func__, cmd_msg->channel,
  3230. cmd_msg->flags, (int)cmd_msg->tx_cmd_num,
  3231. (int)cmd_msg->rx_cmd_num);
  3232. for (i = 0; i < cmd_msg->tx_cmd_num; i++) {
  3233. DDPINFO("type[%d]=0x%x, tx_len[%d]=%d\n",
  3234. i, cmd_msg->type[i], i, (int)cmd_msg->tx_len[i]);
  3235. for (j = 0; j < (int)cmd_msg->tx_len[i]; j++) {
  3236. DDPINFO("tx_buf[%d]--byte:%d,val:0x%x\n",
  3237. i, j, *(char *)(cmd_msg->tx_buf[i] + j));
  3238. }
  3239. }
  3240. msg.channel = cmd_msg->channel;
  3241. msg.flags = cmd_msg->flags;
  3242. cmdq_handle = cmdq_pkt_create(mtk_crtc->gce_obj.client[CLIENT_DSI_CFG]);
  3243. cmdq_handle->err_cb.cb = ddic_read_timeout_cb;
  3244. cmdq_handle->err_cb.data = crtc;
  3245. /* Reset DISP_SLOT_READ_DDIC_BASE to 0xff00ff00 */
  3246. for (i = 0; i < READ_DDIC_SLOT_NUM; i++) {
  3247. cmdq_pkt_write(cmdq_handle,
  3248. mtk_crtc->gce_obj.base,
  3249. (mtk_crtc->gce_obj.buf.pa_base +
  3250. DISP_SLOT_READ_DDIC_BASE + i * 0x4),
  3251. 0xff00ff00, ~0);
  3252. }
  3253. /* Todo: Support read multiple registers */
  3254. msg.type = cmd_msg->type[0];
  3255. msg.tx_len = cmd_msg->tx_len[0];
  3256. msg.tx_buf = cmd_msg->tx_buf[0];
  3257. msg.rx_len = cmd_msg->rx_len[0];
  3258. msg.rx_buf = cmd_msg->rx_buf[0];
  3259. if (dsi_mode == 0) { /* CMD mode LP */
  3260. cmdq_pkt_wait_no_clear(cmdq_handle,
  3261. mtk_crtc->gce_obj.event[EVENT_STREAM_EOF]);
  3262. cmdq_pkt_clear_event(cmdq_handle,
  3263. mtk_crtc->gce_obj.event[EVENT_ESD_EOF]);
  3264. _mtk_mipi_dsi_read_gce(dsi, cmdq_handle, &msg);
  3265. cmdq_pkt_set_event(cmdq_handle,
  3266. mtk_crtc->gce_obj.event[EVENT_ESD_EOF]);
  3267. } else { /* VDO to CMD mode LP */
  3268. cmdq_pkt_wfe(cmdq_handle,
  3269. mtk_crtc->gce_obj.event[EVENT_VDO_EOF]);
  3270. mtk_dsi_stop_vdo_mode(dsi, cmdq_handle);
  3271. _mtk_mipi_dsi_read_gce(dsi, cmdq_handle, &msg);
  3272. mtk_dsi_start_vdo_mode(comp, cmdq_handle);
  3273. mtk_disp_mutex_trigger(comp->mtk_crtc->mutex[0], cmdq_handle);
  3274. mtk_dsi_trigger(comp, cmdq_handle);
  3275. }
  3276. read_ddic_chk_sta = 0;
  3277. cmdq_pkt_flush(cmdq_handle);
  3278. mtk_dsi_clear_rxrd_irq(dsi);
  3279. if (read_ddic_chk_sta == 0xff) {
  3280. ret = -EINVAL;
  3281. /* CMD mode error handle */
  3282. if (dsi_mode == 0) {
  3283. /* TODO: set ESD_EOF event through CPU is better */
  3284. mtk_crtc_pkt_create(&cmdq_handle2, crtc,
  3285. mtk_crtc->gce_obj.client[CLIENT_DSI_CFG]);
  3286. cmdq_pkt_set_event(
  3287. cmdq_handle2,
  3288. mtk_crtc->gce_obj.event[EVENT_ESD_EOF]);
  3289. cmdq_pkt_flush(cmdq_handle2);
  3290. cmdq_pkt_destroy(cmdq_handle2);
  3291. }
  3292. goto done;
  3293. }
  3294. /* Copy slot data to data array */
  3295. memcpy((void *)&read_data0,
  3296. (mtk_crtc->gce_obj.buf.va_base +
  3297. DISP_SLOT_READ_DDIC_BASE + 0 * 0x4),
  3298. sizeof(unsigned int));
  3299. memcpy((void *)&read_data1,
  3300. (mtk_crtc->gce_obj.buf.va_base +
  3301. DISP_SLOT_READ_DDIC_BASE + 1 * 0x4),
  3302. sizeof(unsigned int));
  3303. memcpy((void *)&read_data2,
  3304. (mtk_crtc->gce_obj.buf.va_base +
  3305. DISP_SLOT_READ_DDIC_BASE + 2 * 0x4),
  3306. sizeof(unsigned int));
  3307. memcpy((void *)&read_data3,
  3308. (mtk_crtc->gce_obj.buf.va_base +
  3309. DISP_SLOT_READ_DDIC_BASE + 3 * 0x4),
  3310. sizeof(unsigned int));
  3311. DDPINFO("%s: read_data0 byte0~3=0x%x~0x%x~0x%x~0x%x\n",
  3312. __func__, read_data0.byte0, read_data0.byte1
  3313. , read_data0.byte2, read_data0.byte3);
  3314. DDPINFO("%s: read_data1 byte0~3=0x%x~0x%x~0x%x~0x%x\n",
  3315. __func__, read_data1.byte0, read_data1.byte1
  3316. , read_data1.byte2, read_data1.byte3);
  3317. DDPINFO("%s: read_data2 byte0~3=0x%x~0x%x~0x%x~0x%x\n",
  3318. __func__, read_data2.byte0, read_data2.byte1
  3319. , read_data2.byte2, read_data2.byte3);
  3320. DDPINFO("%s: read_data3 byte0~3=0x%x~0x%x~0x%x~0x%x\n",
  3321. __func__, read_data3.byte0, read_data3.byte1
  3322. , read_data3.byte2, read_data3.byte3);
  3323. /*parse packet*/
  3324. packet_type = read_data0.byte0;
  3325. /* 0x02: acknowledge & error report */
  3326. /* 0x11: generic short read response(1 byte return) */
  3327. /* 0x12: generic short read response(2 byte return) */
  3328. /* 0x1a: generic long read response */
  3329. /* 0x1c: dcs long read response */
  3330. /* 0x21: dcs short read response(1 byte return) */
  3331. /* 0x22: dcs short read response(2 byte return) */
  3332. if (packet_type == 0x1A || packet_type == 0x1C) {
  3333. recv_data_cnt = read_data0.byte1
  3334. + read_data0.byte2 * 16;
  3335. if (recv_data_cnt > RT_MAX_NUM) {
  3336. DDPMSG("DSI read long packet data exceeds 10 bytes\n");
  3337. recv_data_cnt = RT_MAX_NUM;
  3338. }
  3339. if (recv_data_cnt > msg.rx_len)
  3340. recv_data_cnt = msg.rx_len;
  3341. DDPINFO("DSI read long packet size: %d\n",
  3342. recv_data_cnt);
  3343. if (recv_data_cnt <= 4) {
  3344. memcpy((void *)msg.rx_buf,
  3345. (void *)&read_data1, recv_data_cnt);
  3346. } else if (recv_data_cnt <= 8) {
  3347. memcpy((void *)msg.rx_buf,
  3348. (void *)&read_data1, 4);
  3349. memcpy((void *)(msg.rx_buf + 4),
  3350. (void *)&read_data2, recv_data_cnt - 4);
  3351. } else {
  3352. memcpy((void *)msg.rx_buf,
  3353. (void *)&read_data1, 4);
  3354. memcpy((void *)(msg.rx_buf + 4),
  3355. (void *)&read_data2, 4);
  3356. memcpy((void *)(msg.rx_buf + 8),
  3357. (void *)&read_data3, recv_data_cnt - 8);
  3358. }
  3359. } else if (packet_type == 0x11 || packet_type == 0x21) {
  3360. recv_data_cnt = 1;
  3361. memcpy((void *)msg.rx_buf,
  3362. (void *)&read_data0.byte1, recv_data_cnt);
  3363. } else if (packet_type == 0x12 || packet_type == 0x22) {
  3364. recv_data_cnt = 2;
  3365. if (recv_data_cnt > msg.rx_len)
  3366. recv_data_cnt = msg.rx_len;
  3367. memcpy((void *)msg.rx_buf,
  3368. (void *)&read_data0.byte1, recv_data_cnt);
  3369. } else if (packet_type == 0x02) {
  3370. DDPPR_ERR("read return type is 0x02, re-read\n");
  3371. } else {
  3372. DDPPR_ERR("read return type is non-recognite, type = 0x%x\n",
  3373. packet_type);
  3374. }
  3375. msg.rx_len = recv_data_cnt;
  3376. DDPINFO("[DSI]packet_type~recv_data_cnt = 0x%x~0x%x\n",
  3377. packet_type, recv_data_cnt);
  3378. /* Todo: Support read multiple registers */
  3379. cmd_msg->rx_len[0] = msg.rx_len;
  3380. cmd_msg->rx_buf[0] = msg.rx_buf;
  3381. /* Debug info */
  3382. for (i = 0; i < cmd_msg->rx_cmd_num; i++) {
  3383. DDPINFO("rx_len[%d]=%d\n", i, (int)cmd_msg->rx_len[i]);
  3384. for (j = 0; j < cmd_msg->rx_len[i]; j++) {
  3385. DDPINFO("rx_buf[%d]--byte:%d,val:0x%x\n",
  3386. i, j, *(char *)(cmd_msg->rx_buf[i] + j));
  3387. }
  3388. }
  3389. done:
  3390. cmdq_pkt_destroy(cmdq_handle);
  3391. DDPMSG("%s -\n", __func__);
  3392. return 0;
  3393. }
  3394. static ssize_t mtk_dsi_host_send_cmd(struct mtk_dsi *dsi,
  3395. const struct mipi_dsi_msg *msg, u8 flag)
  3396. {
  3397. mtk_dsi_wait_idle(dsi, flag, 2000, NULL);
  3398. mtk_dsi_irq_data_clear(dsi, flag);
  3399. mtk_dsi_cmdq(dsi, msg);
  3400. mtk_dsi_start(dsi);
  3401. if (MTK_DSI_HOST_IS_READ(msg->type)) {
  3402. unsigned int loop_cnt = 0;
  3403. s32 tmp;
  3404. udelay(1);
  3405. while (loop_cnt < 100 * 1000) {
  3406. tmp = readl(dsi->regs + DSI_INTSTA);
  3407. if ((tmp & LPRX_RD_RDY_INT_FLAG))
  3408. break;
  3409. loop_cnt++;
  3410. usleep_range(100, 200);
  3411. }
  3412. DDPINFO("%s wait RXDY done\n", __func__);
  3413. mtk_dsi_mask(dsi, DSI_INTSTA, LPRX_RD_RDY_INT_FLAG, 0);
  3414. mtk_dsi_mask(dsi, DSI_RACK, RACK, RACK);
  3415. }
  3416. if (!mtk_dsi_wait_idle(dsi, flag, 2000, NULL))
  3417. return -ETIME;
  3418. else
  3419. return 0;
  3420. }
  3421. static void mtk_dsi_dy_fps_cmdq_cb(struct cmdq_cb_data data)
  3422. {
  3423. struct mtk_cmdq_cb_data *cb_data = data.data;
  3424. DDPINFO("%s vdo mode fps change done\n", __func__);
  3425. cmdq_pkt_destroy(cb_data->cmdq_handle);
  3426. kfree(cb_data);
  3427. }
  3428. static ssize_t mtk_dsi_host_send_vm_cmd(struct mtk_dsi *dsi,
  3429. const struct mipi_dsi_msg *msg, u8 flag)
  3430. {
  3431. unsigned int loop_cnt = 0;
  3432. s32 tmp;
  3433. mtk_dsi_vm_cmdq(dsi, msg, NULL);
  3434. /* clear status */
  3435. mtk_dsi_mask(dsi, DSI_INTSTA, VM_CMD_DONE_INT_EN, 0);
  3436. mtk_dsi_vm_start(dsi);
  3437. while (loop_cnt < 100 * 1000) {
  3438. tmp = readl(dsi->regs + DSI_INTSTA);
  3439. if (!(tmp & VM_CMD_DONE_INT_EN))
  3440. return 0;
  3441. loop_cnt++;
  3442. udelay(1);
  3443. }
  3444. DDPMSG("%s timeout\n", __func__);
  3445. return -ETIME;
  3446. }
  3447. static ssize_t mtk_dsi_host_transfer(struct mipi_dsi_host *host,
  3448. const struct mipi_dsi_msg *msg)
  3449. {
  3450. struct mtk_dsi *dsi = host_to_dsi(host);
  3451. u32 recv_cnt, i;
  3452. u8 read_data[16];
  3453. void *src_addr;
  3454. u8 irq_flag;
  3455. if (readl(dsi->regs + DSI_MODE_CTRL) & MODE)
  3456. irq_flag = VM_CMD_DONE_INT_EN;
  3457. else
  3458. irq_flag = CMD_DONE_INT_FLAG;
  3459. if (MTK_DSI_HOST_IS_READ(msg->type)) {
  3460. struct mipi_dsi_msg set_rd_msg = {
  3461. .tx_buf = (u8 [1]) { msg->rx_len},
  3462. .tx_len = 0x1,
  3463. .type = MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE,
  3464. };
  3465. if (mtk_dsi_host_send_cmd(dsi, &set_rd_msg, irq_flag) < 0)
  3466. DDPPR_ERR("RX mtk_dsi_host_send_cmd fail\n");
  3467. irq_flag |= LPRX_RD_RDY_INT_FLAG;
  3468. }
  3469. if (readl(dsi->regs + DSI_MODE_CTRL) & MODE) {
  3470. if (mtk_dsi_host_send_vm_cmd(dsi, msg, irq_flag) < 0)
  3471. return -ETIME;
  3472. } else {
  3473. if (mtk_dsi_host_send_cmd(dsi, msg, irq_flag) < 0)
  3474. return -ETIME;
  3475. }
  3476. if (!MTK_DSI_HOST_IS_READ(msg->type))
  3477. return 0;
  3478. if (!msg->rx_buf) {
  3479. DRM_ERROR("dsi receive buffer size may be NULL\n");
  3480. return -EINVAL;
  3481. }
  3482. for (i = 0; i < 16; i++)
  3483. *(read_data + i) = readb(dsi->regs + DSI_RX_DATA0 + i);
  3484. recv_cnt = mtk_dsi_recv_cnt(read_data[0], read_data);
  3485. if (recv_cnt > 2)
  3486. src_addr = &read_data[4];
  3487. else
  3488. src_addr = &read_data[1];
  3489. if (recv_cnt > 10)
  3490. recv_cnt = 10;
  3491. if (recv_cnt > msg->rx_len)
  3492. recv_cnt = msg->rx_len;
  3493. if (recv_cnt)
  3494. memcpy(msg->rx_buf, src_addr, recv_cnt);
  3495. DDPINFO("dsi get %d byte data from the panel address(0x%x)\n", recv_cnt,
  3496. *((u8 *)(msg->tx_buf)));
  3497. return recv_cnt;
  3498. }
  3499. static const struct mipi_dsi_host_ops mtk_dsi_ops = {
  3500. .attach = mtk_dsi_host_attach,
  3501. .detach = mtk_dsi_host_detach,
  3502. .transfer = mtk_dsi_host_transfer,
  3503. };
  3504. void mtk_dsi_send_switch_cmd(struct mtk_dsi *dsi,
  3505. struct cmdq_pkt *handle,
  3506. struct mtk_drm_crtc *mtk_crtc, unsigned int cur_mode, unsigned int dst_mode)
  3507. {
  3508. unsigned int i;
  3509. struct dfps_switch_cmd *dfps_cmd = NULL;
  3510. struct mtk_panel_params *params = NULL;
  3511. struct drm_display_mode *old_mode = NULL;
  3512. old_mode = &(mtk_crtc->avail_modes[cur_mode]);
  3513. if (dsi->ext && dsi->ext->params)
  3514. params = mtk_crtc->panel_ext->params;
  3515. else /* can't find panel ext information,stop */
  3516. return;
  3517. for (i = 0; i < MAX_DYN_CMD_NUM; i++) {
  3518. dfps_cmd = &params->dyn_fps.dfps_cmd_table[i];
  3519. if (dfps_cmd->cmd_num == 0)
  3520. break;
  3521. if (dfps_cmd->src_fps == 0 || old_mode->vrefresh == dfps_cmd->src_fps)
  3522. mipi_dsi_dcs_write_gce_dyn(dsi, handle, dfps_cmd->para_list,
  3523. dfps_cmd->cmd_num);
  3524. }
  3525. }
  3526. static void mtk_dsi_cmd_timing_change(struct mtk_dsi *dsi,
  3527. struct mtk_drm_crtc *mtk_crtc, struct drm_crtc_state *old_state)
  3528. {
  3529. struct cmdq_pkt *cmdq_handle;
  3530. struct cmdq_pkt *cmdq_handle2;
  3531. int clk_refcnt = 0;
  3532. struct mtk_crtc_state *state =
  3533. to_mtk_crtc_state(mtk_crtc->base.state);
  3534. struct mtk_crtc_state *old_mtk_state =
  3535. to_mtk_crtc_state(old_state);
  3536. unsigned int src_mode =
  3537. old_mtk_state->prop_val[CRTC_PROP_DISP_MODE_IDX];
  3538. unsigned int dst_mode =
  3539. state->prop_val[CRTC_PROP_DISP_MODE_IDX];
  3540. bool need_mipi_change = 1;
  3541. /* use no mipi clk change solution */
  3542. if (dsi->ext && dsi->ext->params &&
  3543. dsi->ext->params->dyn_fps.switch_en > 0)
  3544. need_mipi_change = 0;
  3545. mtk_crtc_pkt_create(&cmdq_handle, &mtk_crtc->base,
  3546. mtk_crtc->gce_obj.client[CLIENT_CFG]);
  3547. /* 1. wait frame done & wait DSI not busy */
  3548. cmdq_pkt_wfe(cmdq_handle,
  3549. mtk_crtc->gce_obj.event[EVENT_STREAM_EOF]);
  3550. /* Clear stream block to prevent trigger loop start */
  3551. cmdq_pkt_clear_event(cmdq_handle,
  3552. mtk_crtc->gce_obj.event[EVENT_STREAM_BLOCK]);
  3553. mtk_dsi_poll_for_idle(dsi, cmdq_handle);
  3554. cmdq_pkt_flush(cmdq_handle);
  3555. cmdq_pkt_destroy(cmdq_handle);
  3556. if (need_mipi_change == 0)
  3557. goto skip_change_mipi;
  3558. /* send lcm cmd before DSI power down if needed */
  3559. if (dsi->ext && dsi->ext->funcs &&
  3560. dsi->ext->funcs->mode_switch)
  3561. dsi->ext->funcs->mode_switch(dsi->panel, src_mode,
  3562. dst_mode, BEFORE_DSI_POWERDOWN);
  3563. /* Power off DSI */
  3564. clk_refcnt = dsi->clk_refcnt;
  3565. while (dsi->clk_refcnt != 1)
  3566. mtk_dsi_ddp_unprepare(&dsi->ddp_comp);
  3567. mtk_dsi_enter_idle(dsi);
  3568. if (dsi->ext && dsi->ext->funcs &&
  3569. dsi->ext->funcs->ext_param_set)
  3570. dsi->ext->funcs->ext_param_set(dsi->panel,
  3571. state->prop_val[CRTC_PROP_DISP_MODE_IDX]);
  3572. /* Power on & re-config DSI*/
  3573. mtk_dsi_leave_idle(dsi);
  3574. while (dsi->clk_refcnt != clk_refcnt)
  3575. mtk_dsi_ddp_prepare(&dsi->ddp_comp);
  3576. skip_change_mipi:
  3577. /* send lcm cmd after DSI power on if needed */
  3578. if (dsi->ext && dsi->ext->funcs &&
  3579. dsi->ext->funcs->mode_switch)
  3580. dsi->ext->funcs->mode_switch(dsi->panel, src_mode,
  3581. dst_mode, AFTER_DSI_POWERON);
  3582. /* set frame done */
  3583. mtk_crtc_pkt_create(&cmdq_handle2, &mtk_crtc->base,
  3584. mtk_crtc->gce_obj.client[CLIENT_CFG]);
  3585. cmdq_pkt_set_event(cmdq_handle2,
  3586. mtk_crtc->gce_obj.event[EVENT_STREAM_EOF]);
  3587. cmdq_pkt_set_event(cmdq_handle2,
  3588. mtk_crtc->gce_obj.event[EVENT_STREAM_BLOCK]);
  3589. cmdq_pkt_flush(cmdq_handle2);
  3590. cmdq_pkt_destroy(cmdq_handle2);
  3591. }
  3592. static void mtk_dsi_vdo_timing_change(struct mtk_dsi *dsi,
  3593. struct mtk_drm_crtc *mtk_crtc, struct drm_crtc_state *old_state)
  3594. {
  3595. unsigned int vfp = 0;
  3596. unsigned int hfp = 0;
  3597. unsigned int fps_chg_index = 0;
  3598. struct cmdq_pkt *handle;
  3599. struct cmdq_client *client = mtk_crtc->gce_obj.client[CLIENT_DSI_CFG];
  3600. struct mtk_ddp_comp *comp = &dsi->ddp_comp;
  3601. struct mtk_crtc_state *state =
  3602. to_mtk_crtc_state(mtk_crtc->base.state);
  3603. struct mtk_cmdq_cb_data *cb_data;
  3604. struct drm_display_mode adjusted_mode = state->base.adjusted_mode;
  3605. struct mtk_crtc_state *old_mtk_state =
  3606. to_mtk_crtc_state(old_state);
  3607. unsigned int src_mode =
  3608. old_mtk_state->prop_val[CRTC_PROP_DISP_MODE_IDX];
  3609. DDPINFO("%s+\n", __func__);
  3610. if (dsi->ext && dsi->ext->funcs &&
  3611. dsi->ext->funcs->ext_param_set)
  3612. dsi->ext->funcs->ext_param_set(dsi->panel,
  3613. state->prop_val[CRTC_PROP_DISP_MODE_IDX]);
  3614. //1.fps change index
  3615. fps_chg_index = mtk_crtc->fps_change_index;
  3616. mtk_drm_idlemgr_kick(__func__, &(mtk_crtc->base), 0);
  3617. cb_data = kmalloc(sizeof(*cb_data), GFP_KERNEL);
  3618. if (!cb_data) {
  3619. DDPINFO("%s:%d, cb data creation failed\n",
  3620. __func__, __LINE__);
  3621. return;
  3622. }
  3623. mtk_crtc_pkt_create(&handle, &(mtk_crtc->base), client);
  3624. if (fps_chg_index & DYNFPS_DSI_MIPI_CLK) {
  3625. DDPINFO("%s, change MIPI Clock\n", __func__);
  3626. } else if (fps_chg_index & DYNFPS_DSI_HFP) {
  3627. DDPINFO("%s, change HFP\n", __func__);
  3628. /*wait and clear EOF
  3629. * avoid other display related task break fps change task
  3630. * because fps change need stop & re-start vdo mode
  3631. */
  3632. cmdq_pkt_wfe(handle,
  3633. mtk_crtc->gce_obj.event[EVENT_VDO_EOF]);
  3634. /*1.1 send cmd: stop vdo mode*/
  3635. mtk_dsi_stop_vdo_mode(dsi, handle);
  3636. /* for crtc first enable,dyn fps fail*/
  3637. if (dsi->data_rate == 0) {
  3638. dsi->data_rate = mtk_dsi_default_rate(dsi);
  3639. mtk_mipi_tx_pll_rate_set_adpt(dsi->phy, dsi->data_rate);
  3640. mtk_dsi_phy_timconfig(dsi, NULL);
  3641. }
  3642. if (dsi->mipi_hopping_sta) {
  3643. DDPINFO("%s,mipi_clk_change_sta\n", __func__);
  3644. hfp = dsi->ext->params->dyn.hfp;
  3645. } else
  3646. hfp = adjusted_mode.hsync_start -
  3647. adjusted_mode.hdisplay;
  3648. dsi->vm.hfront_porch = hfp;
  3649. mtk_dsi_calc_vdo_timing(dsi);
  3650. mtk_dsi_porch_setting(comp, handle, DSI_HFP, dsi->hfp_byte);
  3651. /*1.2 send cmd: send cmd*/
  3652. mtk_dsi_send_switch_cmd(dsi, handle, mtk_crtc, src_mode, adjusted_mode.vrefresh);
  3653. /*1.3 send cmd: start vdo mode*/
  3654. mtk_dsi_start_vdo_mode(comp, handle);
  3655. /*clear EOF
  3656. * avoid config continue after we trigger vdo mode
  3657. */
  3658. cmdq_pkt_clear_event(handle,
  3659. mtk_crtc->gce_obj.event[EVENT_VDO_EOF]);
  3660. /*1.3 send cmd: trigger*/
  3661. mtk_disp_mutex_trigger(comp->mtk_crtc->mutex[0], handle);
  3662. mtk_dsi_trigger(comp, handle);
  3663. } else if (fps_chg_index & DYNFPS_DSI_VFP) {
  3664. DDPINFO("%s, change VFP\n", __func__);
  3665. cmdq_pkt_clear_event(handle,
  3666. mtk_crtc->gce_obj.event[EVENT_DSI0_SOF]);
  3667. cmdq_pkt_wait_no_clear(handle,
  3668. mtk_crtc->gce_obj.event[EVENT_DSI0_SOF]);
  3669. comp = mtk_ddp_comp_request_output(mtk_crtc);
  3670. if (!comp) {
  3671. DDPPR_ERR("ddp comp is NULL\n");
  3672. return;
  3673. }
  3674. if (dsi->mipi_hopping_sta) {
  3675. DDPINFO("%s,mipi_clk_change_sta\n", __func__);
  3676. vfp = dsi->ext->params->dyn.vfp;
  3677. } else
  3678. vfp = adjusted_mode.vsync_start -
  3679. adjusted_mode.vdisplay;
  3680. dsi->vm.vfront_porch = vfp;
  3681. if (dsi->mipi_hopping_sta) {
  3682. DDPINFO("%s,mipi_clk_change_sta\n", __func__);
  3683. vfp = dsi->ext->params->dyn.vfp;
  3684. } else
  3685. vfp = adjusted_mode.vsync_start -
  3686. adjusted_mode.vdisplay;
  3687. dsi->vm.vfront_porch = vfp;
  3688. mtk_dsi_porch_setting(comp, handle, DSI_VFP, vfp);
  3689. }
  3690. cb_data->cmdq_handle = handle;
  3691. if (cmdq_pkt_flush_threaded(handle,
  3692. mtk_dsi_dy_fps_cmdq_cb, cb_data) < 0)
  3693. DDPPR_ERR("failed to flush dsi_dy_fps\n");
  3694. }
  3695. static void mtk_dsi_timing_change(struct mtk_dsi *dsi,
  3696. struct mtk_drm_crtc *mtk_crtc, struct drm_crtc_state *old_state)
  3697. {
  3698. if (mtk_dsi_is_cmd_mode(&dsi->ddp_comp))
  3699. mtk_dsi_cmd_timing_change(dsi, mtk_crtc, old_state);
  3700. else
  3701. mtk_dsi_vdo_timing_change(dsi, mtk_crtc, old_state);
  3702. }
  3703. unsigned int mtk_dsi_get_dsc_compress_rate(struct mtk_dsi *dsi)
  3704. {
  3705. unsigned int compress_rate, bpp, bpc;
  3706. struct mtk_panel_ext *ext = dsi->ext;
  3707. if (ext->params->dsc_params.enable) {
  3708. bpp = ext->params->dsc_params.bit_per_pixel / 16;
  3709. bpc = ext->params->dsc_params.bit_per_channel;
  3710. //compress_rate*100 for 3.75 or 2.5 case
  3711. compress_rate = bpc * 3 * 100 / bpp;
  3712. } else
  3713. compress_rate = 100;
  3714. return compress_rate;
  3715. }
  3716. /******************************************************************************
  3717. * HRT BW = Overlap x vact x hact x vrefresh x 4 x (vtotal/vact)
  3718. * In Video Mode , Using the Formula below:
  3719. * MM Clock
  3720. * DSC on: vact x hact x vrefresh x (vtotal / vact)
  3721. * DSC off: vact x hact x vrefresh x (vtotal x htotal) / (vact x hact)
  3722. * In Command Mode Using the Formula below:
  3723. * Type | MM Clock (unit: Pixel)
  3724. * CPHY | data_rate x (16/7) x lane_num x compress_ratio / bpp
  3725. * DPHY | data_rate x lane_num x compress_ratio / bpp
  3726. ******************************************************************************/
  3727. void mtk_dsi_set_mmclk_by_datarate(struct mtk_dsi *dsi,
  3728. struct mtk_drm_crtc *mtk_crtc, unsigned int en)
  3729. {
  3730. struct mtk_panel_ext *ext = dsi->ext;
  3731. unsigned int compress_rate;
  3732. unsigned int data_rate;
  3733. unsigned int pixclk = 0;
  3734. u32 bpp = mipi_dsi_pixel_format_to_bpp(dsi->format);
  3735. unsigned int pixclk_min = 0;
  3736. unsigned int hact = mtk_crtc->base.state->adjusted_mode.hdisplay;
  3737. unsigned int htotal = mtk_crtc->base.state->adjusted_mode.htotal;
  3738. unsigned int vtotal = mtk_crtc->base.state->adjusted_mode.vtotal;
  3739. unsigned int vact = mtk_crtc->base.state->adjusted_mode.vdisplay;
  3740. unsigned int vrefresh = mtk_crtc->base.state->adjusted_mode.vrefresh;
  3741. if (!en) {
  3742. mtk_drm_set_mmclk_by_pixclk(&mtk_crtc->base, pixclk,
  3743. __func__);
  3744. return;
  3745. }
  3746. //for FPS change,update dsi->ext
  3747. dsi->ext = find_panel_ext(dsi->panel);
  3748. data_rate = mtk_dsi_default_rate(dsi);
  3749. if (!dsi->ext) {
  3750. DDPPR_ERR("DSI panel ext is NULL\n");
  3751. return;
  3752. }
  3753. compress_rate = mtk_dsi_get_dsc_compress_rate(dsi);
  3754. if (!data_rate) {
  3755. DDPPR_ERR("DSI data_rate is NULL\n");
  3756. return;
  3757. }
  3758. //If DSI mode is vdo mode
  3759. if (!mtk_dsi_is_cmd_mode(&dsi->ddp_comp)) {
  3760. if (ext->params->is_cphy)
  3761. pixclk_min = data_rate * dsi->lanes * 2 / 7 / 3;
  3762. else
  3763. pixclk_min = data_rate * dsi->lanes / 8 / 3;
  3764. pixclk = vact * hact * vrefresh / 1000;
  3765. if (ext->params->dsc_params.enable)
  3766. pixclk = pixclk * vtotal / vact;
  3767. else
  3768. pixclk = pixclk * (vtotal * htotal * 100 /
  3769. (vact * hact)) / 100;
  3770. pixclk = (unsigned int)(pixclk / 1000);
  3771. pixclk = (pixclk_min > pixclk) ? pixclk_min : pixclk;
  3772. }
  3773. else {
  3774. pixclk = data_rate * dsi->lanes * compress_rate;
  3775. if (data_rate && ext->params->is_cphy)
  3776. pixclk = pixclk * 16 / 7;
  3777. pixclk = pixclk / bpp / 100;
  3778. }
  3779. DDPINFO("%s,data_rate =%d,clk=%u pixclk_min=%d\n", __func__,
  3780. data_rate, pixclk, pixclk_min);
  3781. mtk_drm_set_mmclk_by_pixclk(&mtk_crtc->base, pixclk, __func__);
  3782. }
  3783. /******************************************************************************
  3784. * DSI Type | PHY TYPE | HRT_BW (unit: Bytes) one frame ( Overlap * )
  3785. * VDO MODE | CPHY/DPHY| Overlap x vact x hact x vrefresh x 4 x (vtotal/vact)
  3786. * CMD MODE | CPHY | (16/7) x data_rate x lane_num x compress_ratio/ bpp x4
  3787. * CMD MODE | DPHY | data_rate x lane_num x compress_ratio / bpp x 4
  3788. ******************************************************************************/
  3789. unsigned long long mtk_dsi_get_frame_hrt_bw_base_by_datarate(
  3790. struct mtk_drm_crtc *mtk_crtc,
  3791. struct mtk_dsi *dsi)
  3792. {
  3793. static unsigned long long bw_base;
  3794. int hact = mtk_crtc->base.state->adjusted_mode.hdisplay;
  3795. int vtotal = mtk_crtc->base.state->adjusted_mode.vtotal;
  3796. int vact = mtk_crtc->base.state->adjusted_mode.vdisplay;
  3797. int vrefresh = mtk_crtc->base.state->adjusted_mode.vrefresh;
  3798. //For CMD mode to calculate HRT BW
  3799. unsigned int compress_rate = mtk_dsi_get_dsc_compress_rate(dsi);
  3800. unsigned int data_rate = mtk_dsi_default_rate(dsi);
  3801. u32 bpp = mipi_dsi_pixel_format_to_bpp(dsi->format);
  3802. bw_base = vact * hact * vrefresh * 4 / 1000;
  3803. if (!mtk_dsi_is_cmd_mode(&dsi->ddp_comp)) {
  3804. bw_base = bw_base * vtotal / vact;
  3805. bw_base = bw_base / 1000;
  3806. } else {
  3807. bw_base = data_rate * dsi->lanes * compress_rate * 4;
  3808. bw_base = bw_base / bpp / 100;
  3809. }
  3810. DDPDBG("Frame Bw:%llu", bw_base);
  3811. return bw_base;
  3812. }
  3813. static int mtk_dsi_io_cmd(struct mtk_ddp_comp *comp, struct cmdq_pkt *handle,
  3814. enum mtk_ddp_io_cmd cmd, void *params)
  3815. {
  3816. struct mtk_panel_ext **ext;
  3817. struct mtk_dsi *dsi = container_of(comp, struct mtk_dsi, ddp_comp);
  3818. void **out_params;
  3819. struct mtk_panel_ext *panel_ext = NULL;
  3820. struct drm_display_mode **mode;
  3821. bool *enable;
  3822. unsigned int vfp_low_power = 0;
  3823. switch (cmd) {
  3824. case REQ_PANEL_EXT:
  3825. ext = (struct mtk_panel_ext **)params;
  3826. *ext = mtk_dsi_get_panel_ext(comp);
  3827. break;
  3828. case DSI_START_VDO_MODE:
  3829. mtk_dsi_start_vdo_mode(comp, handle);
  3830. break;
  3831. case DSI_STOP_VDO_MODE:
  3832. mtk_dsi_stop_vdo_mode(dsi, handle);
  3833. break;
  3834. case ESD_CHECK_READ:
  3835. mtk_dsi_esd_read(comp, handle, (uintptr_t)params);
  3836. break;
  3837. case ESD_CHECK_CMP:
  3838. return mtk_dsi_esd_cmp(comp, handle, params);
  3839. case CONNECTOR_READ_EPILOG:
  3840. mtk_dsi_clear_rxrd_irq(dsi);
  3841. break;
  3842. case REQ_ESD_EINT_COMPAT:
  3843. out_params = (void **)params;
  3844. *out_params = (void *)dsi->driver_data->esd_eint_compat;
  3845. break;
  3846. case COMP_REG_START:
  3847. mtk_dsi_trigger(comp, handle);
  3848. break;
  3849. case CONNECTOR_PANEL_ENABLE:
  3850. mtk_output_dsi_enable(dsi, true);
  3851. break;
  3852. case CONNECTOR_PANEL_DISABLE:
  3853. {
  3854. mtk_output_dsi_disable(dsi, true);
  3855. dsi->doze_enabled = false;
  3856. }
  3857. break;
  3858. case CONNECTOR_ENABLE:
  3859. mtk_dsi_leave_idle(dsi);
  3860. break;
  3861. case CONNECTOR_DISABLE:
  3862. mtk_dsi_enter_idle(dsi);
  3863. break;
  3864. case CONNECTOR_RESET:
  3865. mtk_dsi_reset_engine(dsi);
  3866. break;
  3867. case CONNECTOR_IS_ENABLE:
  3868. enable = (bool *)params;
  3869. *enable = dsi->output_en;
  3870. break;
  3871. case DSI_VFP_IDLE_MODE:
  3872. {
  3873. panel_ext = mtk_dsi_get_panel_ext(comp);
  3874. if (dsi->mipi_hopping_sta && panel_ext && panel_ext->params
  3875. && panel_ext->params->dyn.vfp_lp_dyn)
  3876. vfp_low_power = panel_ext->params->dyn.vfp_lp_dyn;
  3877. else if (panel_ext && panel_ext->params
  3878. && panel_ext->params->vfp_low_power)
  3879. vfp_low_power = panel_ext->params->vfp_low_power;
  3880. if (vfp_low_power) {
  3881. DDPINFO("vfp_low_power=%d\n", vfp_low_power);
  3882. mtk_dsi_porch_setting(comp, handle, DSI_VFP,
  3883. vfp_low_power);
  3884. }
  3885. }
  3886. break;
  3887. case DSI_VFP_DEFAULT_MODE:
  3888. {
  3889. unsigned int vfront_porch = 0;
  3890. struct mtk_drm_crtc *crtc = comp->mtk_crtc;
  3891. panel_ext = mtk_dsi_get_panel_ext(comp);
  3892. if (dsi->mipi_hopping_sta && panel_ext && panel_ext->params
  3893. && panel_ext->params->dyn.vfp)
  3894. vfront_porch = panel_ext->params->dyn.vfp;
  3895. else
  3896. vfront_porch = dsi->vm.vfront_porch;
  3897. DDPINFO("vfront_porch=%d\n", vfront_porch);
  3898. if (panel_ext->params->wait_sof_before_dec_vfp) {
  3899. cmdq_pkt_clear_event(handle,
  3900. crtc->gce_obj.event[EVENT_DSI0_SOF]);
  3901. cmdq_pkt_wait_no_clear(handle,
  3902. crtc->gce_obj.event[EVENT_DSI0_SOF]);
  3903. }
  3904. mtk_dsi_porch_setting(comp, handle, DSI_VFP,
  3905. vfront_porch);
  3906. }
  3907. break;
  3908. case DSI_GET_TIMING:
  3909. mode = (struct drm_display_mode **)params;
  3910. *mode = list_first_entry(&dsi->conn.modes,
  3911. struct drm_display_mode, head);
  3912. break;
  3913. case DSI_GET_MODE_BY_MAX_VREFRESH:
  3914. {
  3915. struct drm_display_mode *tmp = NULL;
  3916. unsigned int vrefresh = 0;
  3917. mode = (struct drm_display_mode **)params;
  3918. list_for_each_entry(tmp, &dsi->conn.modes, head) {
  3919. if (tmp && tmp->vrefresh > vrefresh) {
  3920. vrefresh = tmp->vrefresh;
  3921. *mode = tmp;
  3922. }
  3923. }
  3924. }
  3925. break;
  3926. case IRQ_LEVEL_IDLE:
  3927. {
  3928. unsigned int inten;
  3929. if (!mtk_dsi_is_cmd_mode(&dsi->ddp_comp) && handle) {
  3930. inten = FRAME_DONE_INT_FLAG;
  3931. cmdq_pkt_write(handle, comp->cmdq_base,
  3932. comp->regs_pa + DSI_INTEN, 0, inten);
  3933. }
  3934. }
  3935. break;
  3936. case IRQ_LEVEL_ALL:
  3937. {
  3938. unsigned int inten;
  3939. if (!handle) {
  3940. DDPPR_ERR("GCE handle is NULL\n");
  3941. return 0;
  3942. }
  3943. inten = BUFFER_UNDERRUN_INT_FLAG | INP_UNFINISH_INT_EN;
  3944. if (!mtk_dsi_is_cmd_mode(&dsi->ddp_comp)) {
  3945. inten |= FRAME_DONE_INT_FLAG;
  3946. cmdq_pkt_write(handle, comp->cmdq_base,
  3947. comp->regs_pa + DSI_INTEN, inten, inten);
  3948. } else {
  3949. inten |= TE_RDY_INT_FLAG;
  3950. cmdq_pkt_write(handle, comp->cmdq_base,
  3951. comp->regs_pa + DSI_INTEN, inten, inten);
  3952. }
  3953. }
  3954. break;
  3955. case LCM_RESET:
  3956. {
  3957. struct mtk_dsi *dsi =
  3958. container_of(comp, struct mtk_dsi, ddp_comp);
  3959. panel_ext = mtk_dsi_get_panel_ext(comp);
  3960. if (panel_ext && panel_ext->funcs
  3961. && panel_ext->funcs->reset)
  3962. panel_ext->funcs->reset(dsi->panel, *(int *)params);
  3963. }
  3964. break;
  3965. case DSI_SET_BL:
  3966. {
  3967. struct mtk_dsi *dsi =
  3968. container_of(comp, struct mtk_dsi, ddp_comp);
  3969. panel_ext = mtk_dsi_get_panel_ext(comp);
  3970. if (panel_ext && panel_ext->funcs
  3971. && panel_ext->funcs->set_backlight_cmdq)
  3972. panel_ext->funcs->set_backlight_cmdq(dsi,
  3973. mipi_dsi_dcs_write_gce,
  3974. handle, *(int *)params);
  3975. }
  3976. break;
  3977. case DSI_SET_BL_AOD:
  3978. {
  3979. struct mtk_dsi *dsi =
  3980. container_of(comp, struct mtk_dsi, ddp_comp);
  3981. panel_ext = mtk_dsi_get_panel_ext(comp);
  3982. if (panel_ext && panel_ext->funcs
  3983. && panel_ext->funcs->set_aod_light_mode)
  3984. panel_ext->funcs->set_aod_light_mode(dsi,
  3985. mipi_dsi_dcs_write_gce,
  3986. handle, *(unsigned int *)params);
  3987. }
  3988. break;
  3989. case DSI_SET_BL_GRP:
  3990. {
  3991. struct mtk_dsi *dsi =
  3992. container_of(comp, struct mtk_dsi, ddp_comp);
  3993. panel_ext = mtk_dsi_get_panel_ext(comp);
  3994. if (panel_ext && panel_ext->funcs
  3995. && panel_ext->funcs->set_backlight_grp_cmdq)
  3996. panel_ext->funcs->set_backlight_grp_cmdq(dsi,
  3997. mipi_dsi_dcs_grp_write_gce,
  3998. handle, *(int *)params);
  3999. }
  4000. break;
  4001. case DSI_HBM_SET:
  4002. {
  4003. panel_ext = mtk_dsi_get_panel_ext(comp);
  4004. if (!(panel_ext && panel_ext->funcs &&
  4005. panel_ext->funcs->hbm_set_cmdq))
  4006. break;
  4007. panel_ext->funcs->hbm_set_cmdq(dsi->panel, dsi,
  4008. mipi_dsi_dcs_write_gce, handle,
  4009. *(bool *)params);
  4010. break;
  4011. }
  4012. case DSI_HBM_GET_STATE:
  4013. {
  4014. panel_ext = mtk_dsi_get_panel_ext(comp);
  4015. if (!(panel_ext && panel_ext->funcs &&
  4016. panel_ext->funcs->hbm_get_state))
  4017. break;
  4018. panel_ext->funcs->hbm_get_state(dsi->panel, (bool *)params);
  4019. break;
  4020. }
  4021. case DSI_HBM_GET_WAIT_STATE:
  4022. {
  4023. panel_ext = mtk_dsi_get_panel_ext(comp);
  4024. if (!(panel_ext && panel_ext->funcs &&
  4025. panel_ext->funcs->hbm_get_wait_state))
  4026. break;
  4027. panel_ext->funcs->hbm_get_wait_state(dsi->panel,
  4028. (bool *)params);
  4029. break;
  4030. }
  4031. case DSI_HBM_SET_WAIT_STATE:
  4032. {
  4033. panel_ext = mtk_dsi_get_panel_ext(comp);
  4034. if (!(panel_ext && panel_ext->funcs &&
  4035. panel_ext->funcs->hbm_set_wait_state))
  4036. break;
  4037. panel_ext->funcs->hbm_set_wait_state(dsi->panel,
  4038. *(bool *)params);
  4039. break;
  4040. }
  4041. case DSI_HBM_WAIT:
  4042. {
  4043. int ret = 0;
  4044. if (mtk_dsi_is_cmd_mode(&dsi->ddp_comp)) {
  4045. reset_dsi_wq(&dsi->te_rdy);
  4046. ret = wait_dsi_wq(&dsi->te_rdy, HZ);
  4047. } else {
  4048. reset_dsi_wq(&dsi->frame_done);
  4049. ret = wait_dsi_wq(&dsi->frame_done, HZ);
  4050. }
  4051. if (!ret)
  4052. DDPINFO("%s: DSI_HBM_WAIT failed\n", __func__);
  4053. break;
  4054. }
  4055. case LCM_ATA_CHECK:
  4056. {
  4057. struct mtk_dsi *dsi =
  4058. container_of(comp, struct mtk_dsi, ddp_comp);
  4059. int *val = (int *)params;
  4060. panel_ext = mtk_dsi_get_panel_ext(comp);
  4061. if (panel_ext && panel_ext->funcs
  4062. && panel_ext->funcs->ata_check)
  4063. *val = panel_ext->funcs->ata_check(dsi->panel);
  4064. }
  4065. break;
  4066. case DSI_SET_CRTC_AVAIL_MODES:
  4067. {
  4068. struct mtk_drm_crtc *crtc = (struct mtk_drm_crtc *)params;
  4069. struct drm_display_mode *m;
  4070. unsigned int i = 0;
  4071. crtc->avail_modes_num = 0;
  4072. list_for_each_entry(m, &dsi->conn.modes, head)
  4073. crtc->avail_modes_num++;
  4074. crtc->avail_modes =
  4075. vzalloc(sizeof(struct drm_display_mode) *
  4076. crtc->avail_modes_num);
  4077. list_for_each_entry(m, &dsi->conn.modes, head) {
  4078. drm_mode_copy(&crtc->avail_modes[i], m);
  4079. i++;
  4080. }
  4081. }
  4082. break;
  4083. case DSI_TIMING_CHANGE:
  4084. {
  4085. struct mtk_drm_crtc *crtc = comp->mtk_crtc;
  4086. struct drm_crtc_state *old_state =
  4087. (struct drm_crtc_state *)params;
  4088. mtk_dsi_timing_change(dsi, crtc, old_state);
  4089. }
  4090. break;
  4091. case GET_PANEL_NAME:
  4092. {
  4093. struct mtk_dsi *dsi =
  4094. container_of(comp, struct mtk_dsi, ddp_comp);
  4095. out_params = (void **)params;
  4096. *out_params = (void *)dsi->panel->dev->driver->name;
  4097. }
  4098. break;
  4099. case DSI_CHANGE_MODE:
  4100. {
  4101. struct mtk_dsi *dsi =
  4102. container_of(comp, struct mtk_dsi, ddp_comp);
  4103. int *aod_en = params;
  4104. panel_ext = mtk_dsi_get_panel_ext(comp);
  4105. if (dsi->ext && dsi->ext->funcs
  4106. && dsi->ext->funcs->doze_get_mode_flags) {
  4107. dsi->mode_flags =
  4108. dsi->ext->funcs->doze_get_mode_flags(
  4109. dsi->panel, *aod_en);
  4110. }
  4111. }
  4112. break;
  4113. case MIPI_HOPPING:
  4114. {
  4115. struct mtk_dsi *dsi =
  4116. container_of(comp, struct mtk_dsi, ddp_comp);
  4117. int *en = (int *)params;
  4118. mtk_dsi_clk_change(dsi, *en);
  4119. }
  4120. break;
  4121. case DYN_FPS_INDEX:
  4122. {
  4123. struct mtk_drm_crtc *crtc = comp->mtk_crtc;
  4124. struct drm_crtc_state *old_state =
  4125. (struct drm_crtc_state *)params;
  4126. mtk_dsi_fps_change_index(dsi, crtc, old_state);
  4127. }
  4128. break;
  4129. case SET_MMCLK_BY_DATARATE:
  4130. {
  4131. #ifndef CONFIG_FPGA_EARLY_PORTING
  4132. struct mtk_drm_crtc *crtc = comp->mtk_crtc;
  4133. unsigned int *pixclk = (unsigned int *)params;
  4134. mtk_dsi_set_mmclk_by_datarate(dsi, crtc, *pixclk);
  4135. #endif
  4136. }
  4137. break;
  4138. case GET_FRAME_HRT_BW_BY_DATARATE:
  4139. {
  4140. struct mtk_drm_crtc *crtc = comp->mtk_crtc;
  4141. unsigned long long *base_bw =
  4142. (unsigned long long *)params;
  4143. *base_bw = mtk_dsi_get_frame_hrt_bw_base_by_datarate(crtc, dsi);
  4144. }
  4145. break;
  4146. case DSI_SEND_DDIC_CMD:
  4147. {
  4148. struct mtk_drm_crtc *crtc = comp->mtk_crtc;
  4149. struct mtk_ddic_dsi_msg *cmd_msg =
  4150. (struct mtk_ddic_dsi_msg *)params;
  4151. return mtk_mipi_dsi_write_gce(dsi, handle, crtc, cmd_msg);
  4152. }
  4153. break;
  4154. case DSI_READ_DDIC_CMD:
  4155. {
  4156. struct mtk_drm_crtc *crtc = comp->mtk_crtc;
  4157. struct mtk_ddic_dsi_msg *cmd_msg =
  4158. (struct mtk_ddic_dsi_msg *)params;
  4159. return mtk_mipi_dsi_read_gce(dsi, handle, crtc, cmd_msg);
  4160. }
  4161. break;
  4162. case DSI_GET_VIRTUAL_HEIGH:
  4163. {
  4164. struct mtk_drm_crtc *crtc = comp->mtk_crtc;
  4165. return mtk_dsi_get_virtual_heigh(dsi, &crtc->base);
  4166. }
  4167. break;
  4168. case DSI_GET_VIRTUAL_WIDTH:
  4169. {
  4170. struct mtk_drm_crtc *crtc = comp->mtk_crtc;
  4171. return mtk_dsi_get_virtual_width(dsi, &crtc->base);
  4172. }
  4173. break;
  4174. case DSI_LFR_SET:
  4175. {
  4176. mtk_dsi_set_LFR(dsi, comp, handle);
  4177. }
  4178. break;
  4179. case DSI_LFR_UPDATE:
  4180. {
  4181. mtk_dsi_LFR_update(dsi, comp, handle);
  4182. }
  4183. break;
  4184. case DSI_LFR_STATUS_CHECK:
  4185. {
  4186. mtk_dsi_LFR_status_check(dsi);
  4187. }
  4188. break;
  4189. default:
  4190. break;
  4191. }
  4192. return 0;
  4193. }
  4194. static const struct mtk_ddp_comp_funcs mtk_dsi_funcs = {
  4195. .prepare = mtk_dsi_ddp_prepare,
  4196. .unprepare = mtk_dsi_ddp_unprepare,
  4197. .config_trigger = mtk_dsi_config_trigger,
  4198. .io_cmd = mtk_dsi_io_cmd,
  4199. .is_busy = mtk_dsi_is_busy,
  4200. };
  4201. static int mtk_dsi_bind(struct device *dev, struct device *master, void *data)
  4202. {
  4203. int ret;
  4204. struct drm_device *drm = data;
  4205. struct mtk_dsi *dsi = dev_get_drvdata(dev);
  4206. DDPINFO("%s+\n", __func__);
  4207. ret = mtk_ddp_comp_register(drm, &dsi->ddp_comp);
  4208. if (ret < 0) {
  4209. dev_err(dev, "Failed to register component %s: %d\n",
  4210. dev->of_node->full_name, ret);
  4211. return ret;
  4212. }
  4213. ret = mtk_dsi_create_conn_enc(drm, dsi);
  4214. if (ret) {
  4215. DRM_ERROR("Encoder create failed with %d\n", ret);
  4216. goto err_unregister;
  4217. }
  4218. DDPINFO("%s-\n", __func__);
  4219. return 0;
  4220. err_unregister:
  4221. mipi_dsi_host_unregister(&dsi->host);
  4222. mtk_ddp_comp_unregister(drm, &dsi->ddp_comp);
  4223. return ret;
  4224. }
  4225. static void mtk_dsi_unbind(struct device *dev, struct device *master,
  4226. void *data)
  4227. {
  4228. struct drm_device *drm = data;
  4229. struct mtk_dsi *dsi = dev_get_drvdata(dev);
  4230. mtk_dsi_destroy_conn_enc(dsi);
  4231. mipi_dsi_host_unregister(&dsi->host);
  4232. mtk_ddp_comp_unregister(drm, &dsi->ddp_comp);
  4233. }
  4234. static const struct component_ops mtk_dsi_component_ops = {
  4235. .bind = mtk_dsi_bind, .unbind = mtk_dsi_unbind,
  4236. };
  4237. static const struct mtk_dsi_driver_data mt8173_dsi_driver_data = {
  4238. .reg_cmdq_ofs = 0x200, .irq_handler = mtk_dsi_irq,
  4239. .support_shadow = false,
  4240. };
  4241. static const struct mtk_dsi_driver_data mt6779_dsi_driver_data = {
  4242. .reg_cmdq_ofs = 0x200,
  4243. .poll_for_idle = mtk_dsi_poll_for_idle,
  4244. .irq_handler = mtk_dsi_irq_status,
  4245. .esd_eint_compat = "mediatek, DSI_TE-eint",
  4246. .support_shadow = false,
  4247. };
  4248. static const struct mtk_dsi_driver_data mt6885_dsi_driver_data = {
  4249. .reg_cmdq_ofs = 0x200,
  4250. .poll_for_idle = mtk_dsi_poll_for_idle,
  4251. .irq_handler = mtk_dsi_irq_status,
  4252. .esd_eint_compat = "mediatek, DSI_TE-eint",
  4253. .support_shadow = false,
  4254. };
  4255. static const struct mtk_dsi_driver_data mt6873_dsi_driver_data = {
  4256. .reg_cmdq_ofs = 0x200,
  4257. .poll_for_idle = mtk_dsi_poll_for_idle,
  4258. .irq_handler = mtk_dsi_irq_status,
  4259. .esd_eint_compat = "mediatek, DSI_TE-eint",
  4260. .support_shadow = false,
  4261. };
  4262. static const struct mtk_dsi_driver_data mt6853_dsi_driver_data = {
  4263. .reg_cmdq_ofs = 0x200,
  4264. .poll_for_idle = mtk_dsi_poll_for_idle,
  4265. .irq_handler = mtk_dsi_irq_status,
  4266. .esd_eint_compat = "mediatek, DSI_TE-eint",
  4267. .support_shadow = false,
  4268. };
  4269. static const struct mtk_dsi_driver_data mt6833_dsi_driver_data = {
  4270. .reg_cmdq_ofs = 0x200,
  4271. .poll_for_idle = mtk_dsi_poll_for_idle,
  4272. .irq_handler = mtk_dsi_irq_status,
  4273. .esd_eint_compat = "mediatek, DSI_TE-eint",
  4274. .support_shadow = false,
  4275. };
  4276. static const struct mtk_dsi_driver_data mt2701_dsi_driver_data = {
  4277. .reg_cmdq_ofs = 0x180, .irq_handler = mtk_dsi_irq,
  4278. .support_shadow = false,
  4279. };
  4280. static const struct of_device_id mtk_dsi_of_match[] = {
  4281. {.compatible = "mediatek,mt2701-dsi", .data = &mt2701_dsi_driver_data},
  4282. {.compatible = "mediatek,mt6779-dsi", .data = &mt6779_dsi_driver_data},
  4283. {.compatible = "mediatek,mt8173-dsi", .data = &mt8173_dsi_driver_data},
  4284. {.compatible = "mediatek,mt6885-dsi", .data = &mt6885_dsi_driver_data},
  4285. {.compatible = "mediatek,mt6873-dsi", .data = &mt6873_dsi_driver_data},
  4286. {.compatible = "mediatek,mt6853-dsi", .data = &mt6853_dsi_driver_data},
  4287. {.compatible = "mediatek,mt6833-dsi", .data = &mt6833_dsi_driver_data},
  4288. {},
  4289. };
  4290. static int mtk_dsi_probe(struct platform_device *pdev)
  4291. {
  4292. struct mtk_dsi *dsi;
  4293. struct device *dev = &pdev->dev;
  4294. const struct of_device_id *of_id;
  4295. struct device_node *remote_node, *endpoint;
  4296. struct resource *regs;
  4297. int irq_num;
  4298. int comp_id;
  4299. int ret;
  4300. DDPINFO("%s+\n", __func__);
  4301. dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
  4302. if (!dsi)
  4303. return -ENOMEM;
  4304. dsi->host.ops = &mtk_dsi_ops;
  4305. dsi->host.dev = dev;
  4306. ret = mipi_dsi_host_register(&dsi->host);
  4307. if (ret < 0) {
  4308. dev_err(dev, "failed to register DSI host: %d\n", ret);
  4309. return -EPROBE_DEFER;
  4310. }
  4311. of_id = of_match_device(mtk_dsi_of_match, &pdev->dev);
  4312. if (!of_id) {
  4313. dev_err(dev, "DSI device match failed\n");
  4314. return -EPROBE_DEFER;
  4315. }
  4316. dsi->driver_data = (struct mtk_dsi_driver_data *)of_id->data;
  4317. endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
  4318. if (endpoint) {
  4319. remote_node = of_graph_get_remote_port_parent(endpoint);
  4320. if (!remote_node) {
  4321. dev_err(dev, "No panel connected\n");
  4322. ret = -ENODEV;
  4323. goto error;
  4324. }
  4325. dsi->bridge = of_drm_find_bridge(remote_node);
  4326. dsi->panel = of_drm_find_panel(remote_node);
  4327. of_node_put(remote_node);
  4328. if (!dsi->bridge && !dsi->panel) {
  4329. dev_info(dev, "Waiting for bridge or panel driver\n");
  4330. ret = -EPROBE_DEFER;
  4331. goto error;
  4332. }
  4333. }
  4334. dsi->ext = find_panel_ext(dsi->panel);
  4335. dsi->engine_clk = devm_clk_get(dev, "engine");
  4336. if (IS_ERR(dsi->engine_clk)) {
  4337. ret = PTR_ERR(dsi->engine_clk);
  4338. dev_err(dev, "Failed to get engine clock: %d\n", ret);
  4339. #ifndef CONFIG_FPGA_EARLY_PORTING
  4340. goto error;
  4341. #endif
  4342. }
  4343. dsi->digital_clk = devm_clk_get(dev, "digital");
  4344. if (IS_ERR(dsi->digital_clk)) {
  4345. ret = PTR_ERR(dsi->digital_clk);
  4346. dev_err(dev, "Failed to get digital clock: %d\n", ret);
  4347. #ifndef CONFIG_FPGA_EARLY_PORTING
  4348. goto error;
  4349. #endif
  4350. }
  4351. dsi->hs_clk = devm_clk_get(dev, "hs");
  4352. if (IS_ERR(dsi->hs_clk)) {
  4353. ret = PTR_ERR(dsi->hs_clk);
  4354. dev_err(dev, "Failed to get hs clock: %d\n", ret);
  4355. #ifndef CONFIG_FPGA_EARLY_PORTING
  4356. goto error;
  4357. #endif
  4358. }
  4359. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  4360. dsi->regs = devm_ioremap_resource(dev, regs);
  4361. if (IS_ERR(dsi->regs)) {
  4362. ret = PTR_ERR(dsi->regs);
  4363. dev_err(dev, "Failed to ioremap memory: %d\n", ret);
  4364. #ifndef CONFIG_FPGA_EARLY_PORTING
  4365. goto error;
  4366. #endif
  4367. }
  4368. dsi->phy = devm_phy_get(dev, "dphy");
  4369. if (IS_ERR(dsi->phy)) {
  4370. ret = PTR_ERR(dsi->phy);
  4371. dev_err(dev, "Failed to get MIPI-DPHY: %d\n", ret);
  4372. goto error;
  4373. }
  4374. comp_id = mtk_ddp_comp_get_id(dev->of_node, MTK_DSI);
  4375. if (comp_id < 0) {
  4376. dev_err(dev, "Failed to identify by alias: %d\n", comp_id);
  4377. ret = comp_id;
  4378. goto error;
  4379. }
  4380. ret = mtk_ddp_comp_init(dev, dev->of_node, &dsi->ddp_comp, comp_id,
  4381. &mtk_dsi_funcs);
  4382. if (ret) {
  4383. dev_err(dev, "Failed to initialize component: %d\n", ret);
  4384. goto error;
  4385. }
  4386. /* init wq */
  4387. init_dsi_wq(dsi);
  4388. irq_num = platform_get_irq(pdev, 0);
  4389. if (irq_num < 0) {
  4390. dev_err(&pdev->dev, "failed to request dsi irq resource\n");
  4391. ret = -EPROBE_DEFER;
  4392. goto error;
  4393. }
  4394. irq_set_status_flags(irq_num, IRQ_TYPE_LEVEL_HIGH);
  4395. ret = devm_request_irq(
  4396. &pdev->dev, irq_num, dsi->driver_data->irq_handler,
  4397. IRQF_TRIGGER_NONE | IRQF_SHARED, dev_name(&pdev->dev), dsi);
  4398. if (ret) {
  4399. DDPAEE("%s:%d, failed to request irq:%d ret:%d\n",
  4400. __func__, __LINE__,
  4401. irq_num, ret);
  4402. ret = -EPROBE_DEFER;
  4403. goto error;
  4404. }
  4405. init_waitqueue_head(&dsi->irq_wait_queue);
  4406. #ifndef CONFIG_FPGA_EARLY_PORTING
  4407. /* set ccf reference cnt = 1 */
  4408. phy_power_on(dsi->phy);
  4409. ret = clk_prepare_enable(dsi->engine_clk);
  4410. if (ret < 0)
  4411. pr_info("%s Failed to enable engine clock: %d\n",
  4412. __func__, ret);
  4413. ret = clk_prepare_enable(dsi->digital_clk);
  4414. if (ret < 0)
  4415. pr_info("%s Failed to enable digital clock: %d\n",
  4416. __func__, ret);
  4417. #endif
  4418. dsi->output_en = true;
  4419. dsi->clk_refcnt = 1;
  4420. platform_set_drvdata(pdev, dsi);
  4421. DDPINFO("%s-\n", __func__);
  4422. return component_add(&pdev->dev, &mtk_dsi_component_ops);
  4423. error:
  4424. mipi_dsi_host_unregister(&dsi->host);
  4425. return -EPROBE_DEFER;
  4426. }
  4427. static int mtk_dsi_remove(struct platform_device *pdev)
  4428. {
  4429. struct mtk_dsi *dsi = platform_get_drvdata(pdev);
  4430. mtk_output_dsi_disable(dsi, false);
  4431. component_del(&pdev->dev, &mtk_dsi_component_ops);
  4432. return 0;
  4433. }
  4434. struct platform_driver mtk_dsi_driver = {
  4435. .probe = mtk_dsi_probe,
  4436. .remove = mtk_dsi_remove,
  4437. .driver = {
  4438. .name = "mtk-dsi", .of_match_table = mtk_dsi_of_match,
  4439. },
  4440. };
  4441. /* ***************** PanelMaster ******************* */
  4442. u32 fbconfig_mtk_dsi_get_lanes_num(struct mtk_ddp_comp *comp)
  4443. {
  4444. struct mtk_dsi *dsi = container_of(comp, struct mtk_dsi, ddp_comp);
  4445. return dsi->lanes;
  4446. }
  4447. int pm_mtk_dsi_get_mode_type(struct mtk_dsi *dsi)
  4448. {
  4449. u32 vid_mode = CMD_MODE;
  4450. if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO) {
  4451. if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
  4452. vid_mode = BURST_MODE;
  4453. else if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
  4454. vid_mode = SYNC_PULSE_MODE;
  4455. else
  4456. vid_mode = SYNC_EVENT_MODE;
  4457. }
  4458. return vid_mode;
  4459. }
  4460. int fbconfig_mtk_dsi_get_mode_type(struct mtk_ddp_comp *comp)
  4461. {
  4462. struct mtk_dsi *dsi = container_of(comp, struct mtk_dsi, ddp_comp);
  4463. u32 vid_mode = pm_mtk_dsi_get_mode_type(dsi);
  4464. return vid_mode;
  4465. }
  4466. u32 PanelMaster_get_dsi_timing(struct mtk_dsi *dsi, enum MIPI_SETTING_TYPE type)
  4467. {
  4468. u32 dsi_val = 0;
  4469. u32 vid_mode;
  4470. u32 t_hsa;
  4471. int fbconfig_dsiTmpBufBpp = 0;
  4472. struct mtk_panel_ext *ext = dsi->ext;
  4473. struct videomode *vm = &dsi->vm;
  4474. struct dynamic_mipi_params *dyn = NULL;
  4475. if (ext && ext->params)
  4476. dyn = &ext->params->dyn;
  4477. if (dsi->format == MIPI_DSI_FMT_RGB565)
  4478. fbconfig_dsiTmpBufBpp = 2;
  4479. else
  4480. fbconfig_dsiTmpBufBpp = 3;
  4481. vid_mode = pm_mtk_dsi_get_mode_type(dsi);
  4482. t_hsa = (dsi->mipi_hopping_sta) ?
  4483. ((dyn && !!dyn->hsa) ?
  4484. dyn->hsa : vm->hsync_len) :
  4485. vm->hsync_len;
  4486. switch (type) {
  4487. case MIPI_LPX:
  4488. {
  4489. dsi_val = readl(dsi->regs + DSI_PHY_TIMECON0);
  4490. dsi_val &= LPX;
  4491. return dsi_val >> 0;
  4492. }
  4493. case MIPI_HS_PRPR:
  4494. {
  4495. dsi_val = readl(dsi->regs + DSI_PHY_TIMECON0);
  4496. dsi_val &= HS_PREP;
  4497. return dsi_val >> 8;
  4498. }
  4499. case MIPI_HS_ZERO:
  4500. {
  4501. dsi_val = readl(dsi->regs + DSI_PHY_TIMECON0);
  4502. dsi_val &= HS_ZERO;
  4503. return dsi_val >> 16;
  4504. }
  4505. case MIPI_HS_TRAIL:
  4506. {
  4507. dsi_val = readl(dsi->regs + DSI_PHY_TIMECON0);
  4508. dsi_val &= HS_TRAIL;
  4509. return dsi_val >> 24;
  4510. }
  4511. case MIPI_TA_GO:
  4512. {
  4513. dsi_val = readl(dsi->regs + DSI_PHY_TIMECON1);
  4514. dsi_val &= TA_GO;
  4515. return dsi_val >> 0;
  4516. }
  4517. case MIPI_TA_SURE:
  4518. {
  4519. dsi_val = readl(dsi->regs + DSI_PHY_TIMECON1);
  4520. dsi_val &= TA_SURE;
  4521. return dsi_val >> 8;
  4522. }
  4523. case MIPI_TA_GET:
  4524. {
  4525. dsi_val = readl(dsi->regs + DSI_PHY_TIMECON1);
  4526. dsi_val &= TA_GET;
  4527. return dsi_val >> 16;
  4528. }
  4529. case MIPI_DA_HS_EXIT:
  4530. {
  4531. dsi_val = readl(dsi->regs + DSI_PHY_TIMECON1);
  4532. dsi_val &= DA_HS_EXIT;
  4533. return dsi_val >> 24;
  4534. }
  4535. case MIPI_CONT_DET:
  4536. {
  4537. dsi_val = readl(dsi->regs + DSI_PHY_TIMECON2);
  4538. dsi_val &= CONT_DET;
  4539. return dsi_val >> 0;
  4540. }
  4541. case MIPI_CLK_ZERO:
  4542. {
  4543. dsi_val = readl(dsi->regs + DSI_PHY_TIMECON2);
  4544. dsi_val &= CLK_ZERO;
  4545. return dsi_val >> 16;
  4546. }
  4547. case MIPI_CLK_TRAIL:
  4548. {
  4549. dsi_val = readl(dsi->regs + DSI_PHY_TIMECON2);
  4550. dsi_val &= CLK_TRAIL;
  4551. return dsi_val >> 24;
  4552. }
  4553. case MIPI_CLK_HS_PRPR:
  4554. {
  4555. dsi_val = readl(dsi->regs + DSI_PHY_TIMECON3);
  4556. dsi_val &= CLK_HS_PREP;
  4557. return dsi_val >> 0;
  4558. }
  4559. case MIPI_CLK_HS_POST:
  4560. {
  4561. dsi_val = readl(dsi->regs + DSI_PHY_TIMECON3);
  4562. dsi_val &= CLK_HS_POST;
  4563. return dsi_val >> 8;
  4564. }
  4565. case MIPI_CLK_HS_EXIT:
  4566. {
  4567. dsi_val = readl(dsi->regs + DSI_PHY_TIMECON3);
  4568. dsi_val &= CLK_HS_EXIT;
  4569. return dsi_val >> 16;
  4570. }
  4571. case MIPI_HPW:
  4572. {
  4573. u32 tmp_hpw;
  4574. tmp_hpw = readl(dsi->regs + DSI_HSA_WC);
  4575. dsi_val = (tmp_hpw + 10) / fbconfig_dsiTmpBufBpp;
  4576. return dsi_val;
  4577. }
  4578. case MIPI_HFP:
  4579. {
  4580. u32 tmp_hfp;
  4581. tmp_hfp = readl(dsi->regs + DSI_HFP_WC);
  4582. dsi_val = (tmp_hfp + 12) / fbconfig_dsiTmpBufBpp;
  4583. return dsi_val;
  4584. }
  4585. case MIPI_HBP:
  4586. {
  4587. u32 tmp_hbp;
  4588. tmp_hbp = readl(dsi->regs + DSI_HBP_WC);
  4589. if (vid_mode == SYNC_EVENT_MODE || vid_mode == BURST_MODE)
  4590. return (tmp_hbp + 10) / fbconfig_dsiTmpBufBpp - t_hsa;
  4591. else
  4592. return (tmp_hbp + 10) / fbconfig_dsiTmpBufBpp;
  4593. }
  4594. case MIPI_VPW:
  4595. {
  4596. u32 tmp_vpw;
  4597. tmp_vpw = readl(dsi->regs + DSI_VACT_NL);
  4598. return tmp_vpw;
  4599. }
  4600. case MIPI_VFP:
  4601. {
  4602. u32 tmp_vfp;
  4603. tmp_vfp = readl(dsi->regs + DSI_VFP_NL);
  4604. return tmp_vfp;
  4605. }
  4606. case MIPI_VBP:
  4607. {
  4608. u32 tmp_vbp;
  4609. tmp_vbp = readl(dsi->regs + DSI_VBP_NL);
  4610. return tmp_vbp;
  4611. }
  4612. case MIPI_SSC_EN:
  4613. {
  4614. if (dsi->ext->params->ssc_disable)
  4615. dsi_val = 0;
  4616. else
  4617. dsi_val = 1;
  4618. return dsi_val;
  4619. }
  4620. default:
  4621. DDPMSG("fbconfig dsi set timing :no such type!!\n");
  4622. break;
  4623. }
  4624. dsi_val = 0;
  4625. return dsi_val;
  4626. }
  4627. u32 DSI_ssc_enable(struct mtk_dsi *dsi, u32 en)
  4628. {
  4629. u32 disable = en ? 0 : 1;
  4630. dsi->ext->params->ssc_disable = disable;
  4631. return 0;
  4632. }
  4633. int PanelMaster_DSI_set_timing(struct mtk_dsi *dsi, struct MIPI_TIMING timing)
  4634. {
  4635. u32 value;
  4636. int ret = 0;
  4637. u32 vid_mode;
  4638. u32 t_hsa;
  4639. int fbconfig_dsiTmpBufBpp = 0;
  4640. struct mtk_panel_ext *ext = dsi->ext;
  4641. struct videomode *vm = &dsi->vm;
  4642. struct dynamic_mipi_params *dyn = NULL;
  4643. if (ext && ext->params)
  4644. dyn = &ext->params->dyn;
  4645. if (dsi->format == MIPI_DSI_FMT_RGB565)
  4646. fbconfig_dsiTmpBufBpp = 2;
  4647. else
  4648. fbconfig_dsiTmpBufBpp = 3;
  4649. vid_mode = pm_mtk_dsi_get_mode_type(dsi);
  4650. t_hsa = (dsi->mipi_hopping_sta) ?
  4651. ((dyn && !!dyn->hsa) ?
  4652. dyn->hsa : vm->hsync_len) :
  4653. vm->hsync_len;
  4654. switch (timing.type) {
  4655. case MIPI_LPX:
  4656. {
  4657. value = readl(dsi->regs + DSI_PHY_TIMECON0);
  4658. value &= 0xffffff00;
  4659. value |= (timing.value << 0);
  4660. writel(value, dsi->regs + DSI_PHY_TIMECON0);
  4661. break;
  4662. }
  4663. case MIPI_HS_PRPR:
  4664. {
  4665. value = readl(dsi->regs + DSI_PHY_TIMECON0);
  4666. value &= 0xffff00ff;
  4667. value |= (timing.value << 8);
  4668. writel(value, dsi->regs + DSI_PHY_TIMECON0);
  4669. break;
  4670. }
  4671. case MIPI_HS_ZERO:
  4672. {
  4673. value = readl(dsi->regs + DSI_PHY_TIMECON0);
  4674. value &= 0xff00ffff;
  4675. value |= (timing.value << 16);
  4676. writel(value, dsi->regs + DSI_PHY_TIMECON0);
  4677. break;
  4678. }
  4679. case MIPI_HS_TRAIL:
  4680. {
  4681. value = readl(dsi->regs + DSI_PHY_TIMECON0);
  4682. value &= 0x00ffffff;
  4683. value |= (timing.value << 24);
  4684. writel(value, dsi->regs + DSI_PHY_TIMECON0);
  4685. break;
  4686. }
  4687. case MIPI_TA_GO:
  4688. {
  4689. value = readl(dsi->regs + DSI_PHY_TIMECON1);
  4690. value &= 0xffffff00;
  4691. value |= (timing.value << 0);
  4692. writel(value, dsi->regs + DSI_PHY_TIMECON1);
  4693. break;
  4694. }
  4695. case MIPI_TA_SURE:
  4696. {
  4697. value = readl(dsi->regs + DSI_PHY_TIMECON1);
  4698. value &= 0xffff00ff;
  4699. value |= (timing.value << 8);
  4700. writel(value, dsi->regs + DSI_PHY_TIMECON1);
  4701. break;
  4702. }
  4703. case MIPI_TA_GET:
  4704. {
  4705. value = readl(dsi->regs + DSI_PHY_TIMECON1);
  4706. value &= 0xff00ffff;
  4707. value |= (timing.value << 16);
  4708. writel(value, dsi->regs + DSI_PHY_TIMECON1);
  4709. break;
  4710. }
  4711. case MIPI_DA_HS_EXIT:
  4712. {
  4713. value = readl(dsi->regs + DSI_PHY_TIMECON1);
  4714. value &= 0x00ffffff;
  4715. value |= (timing.value << 24);
  4716. writel(value, dsi->regs + DSI_PHY_TIMECON1);
  4717. break;
  4718. }
  4719. case MIPI_CONT_DET:
  4720. {
  4721. value = readl(dsi->regs + DSI_PHY_TIMECON2);
  4722. value &= 0xffffff00;
  4723. value |= (timing.value << 0);
  4724. writel(value, dsi->regs + DSI_PHY_TIMECON2);
  4725. break;
  4726. }
  4727. case MIPI_CLK_ZERO:
  4728. {
  4729. value = readl(dsi->regs + DSI_PHY_TIMECON2);
  4730. value &= 0xff00ffff;
  4731. value |= (timing.value << 16);
  4732. writel(value, dsi->regs + DSI_PHY_TIMECON2);
  4733. break;
  4734. }
  4735. case MIPI_CLK_TRAIL:
  4736. {
  4737. value = readl(dsi->regs + DSI_PHY_TIMECON2);
  4738. value &= 0x00ffffff;
  4739. value |= (timing.value << 24);
  4740. writel(value, dsi->regs + DSI_PHY_TIMECON2);
  4741. break;
  4742. }
  4743. case MIPI_CLK_HS_PRPR:
  4744. {
  4745. value = readl(dsi->regs + DSI_PHY_TIMECON3);
  4746. value &= 0xffffff00;
  4747. value |= (timing.value << 0);
  4748. writel(value, dsi->regs + DSI_PHY_TIMECON3);
  4749. break;
  4750. }
  4751. case MIPI_CLK_HS_POST:
  4752. {
  4753. value = readl(dsi->regs + DSI_PHY_TIMECON3);
  4754. value &= 0xffff00ff;
  4755. value |= (timing.value << 8);
  4756. writel(value, dsi->regs + DSI_PHY_TIMECON3);
  4757. break;
  4758. }
  4759. case MIPI_CLK_HS_EXIT:
  4760. {
  4761. value = readl(dsi->regs + DSI_PHY_TIMECON3);
  4762. value &= 0xff00ffff;
  4763. value |= (timing.value << 16);
  4764. writel(value, dsi->regs + DSI_PHY_TIMECON3);
  4765. break;
  4766. }
  4767. case MIPI_HPW:
  4768. {
  4769. timing.value = timing.value * fbconfig_dsiTmpBufBpp - 10;
  4770. timing.value = ALIGN_TO((timing.value), 4);
  4771. writel(timing.value, dsi->regs + DSI_HSA_WC);
  4772. break;
  4773. }
  4774. case MIPI_HFP:
  4775. {
  4776. timing.value = timing.value * fbconfig_dsiTmpBufBpp - 12;
  4777. timing.value = ALIGN_TO(timing.value, 4);
  4778. writel(timing.value, dsi->regs + DSI_HFP_WC);
  4779. break;
  4780. }
  4781. case MIPI_HBP:
  4782. {
  4783. u32 hbp_byte;
  4784. if (vid_mode == SYNC_EVENT_MODE ||
  4785. vid_mode == BURST_MODE) {
  4786. hbp_byte = timing.value + t_hsa;
  4787. hbp_byte = hbp_byte * fbconfig_dsiTmpBufBpp - 10;
  4788. } else {
  4789. hbp_byte = timing.value * fbconfig_dsiTmpBufBpp - 10;
  4790. }
  4791. hbp_byte = ALIGN_TO(hbp_byte, 4);
  4792. writel(hbp_byte, dsi->regs + DSI_HBP_WC);
  4793. break;
  4794. }
  4795. case MIPI_VPW:
  4796. {
  4797. writel(timing.value, dsi->regs + DSI_VACT_NL);
  4798. break;
  4799. }
  4800. case MIPI_VFP:
  4801. {
  4802. writel(timing.value, dsi->regs + DSI_VFP_NL);
  4803. break;
  4804. }
  4805. case MIPI_VBP:
  4806. {
  4807. writel(timing.value, dsi->regs + DSI_VBP_NL);
  4808. break;
  4809. }
  4810. case MIPI_SSC_EN:
  4811. {
  4812. DSI_ssc_enable(dsi, timing.value);
  4813. break;
  4814. }
  4815. default:
  4816. DDPMSG("fbconfig dsi set timing :no such type!!\n");
  4817. break;
  4818. }
  4819. return ret;
  4820. }
  4821. static int dsi_dcs_write(struct mtk_dsi *dsi, void *data, size_t len)
  4822. {
  4823. struct mipi_dsi_device *dsi_device = dsi->dev_for_PM;
  4824. ssize_t ret;
  4825. char *addr;
  4826. addr = (char *)data;
  4827. if ((int)*addr < 0xB0)
  4828. ret = mipi_dsi_dcs_write_buffer(dsi_device, data, len);
  4829. else
  4830. ret = mipi_dsi_generic_write(dsi_device, data, len);
  4831. return ret;
  4832. }
  4833. static int dsi_dcs_read(struct mtk_dsi *dsi,
  4834. uint8_t cmd, void *data, size_t len)
  4835. {
  4836. struct mipi_dsi_device *dsi_device = dsi->dev_for_PM;
  4837. ssize_t ret;
  4838. ret = mipi_dsi_dcs_read(dsi_device, cmd, data, len);
  4839. return ret;
  4840. }
  4841. int fbconfig_get_esd_check(struct mtk_dsi *dsi, uint32_t cmd,
  4842. uint8_t *buffer, uint32_t num)
  4843. {
  4844. int array[4];
  4845. int ret = 0;
  4846. /* set max returen packet size */
  4847. /* array[0] = 0x00013700 */
  4848. array[0] = 0x3700 + (num << 16);
  4849. ret = dsi_dcs_write(dsi, array, 1);
  4850. if (ret < 0) {
  4851. DDPPR_ERR("fail to writing seq\n");
  4852. return -1;
  4853. }
  4854. ret = dsi_dcs_read(dsi, cmd, buffer, num);
  4855. if (ret < 0) {
  4856. DDPPR_ERR("fail to read seq\n");
  4857. return -1;
  4858. }
  4859. return 0;
  4860. }
  4861. int fbconfig_get_esd_check_test(struct drm_crtc *crtc,
  4862. uint32_t cmd, uint8_t *buffer, uint32_t num)
  4863. {
  4864. int ret = 0;
  4865. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  4866. struct mtk_ddp_comp *output_comp;
  4867. struct mtk_dsi *dsi;
  4868. struct mtk_panel_params *dsi_params;
  4869. int cmd_matched = 0;
  4870. uint32_t i = 0;
  4871. DDP_MUTEX_LOCK(&mtk_crtc->lock, __func__, __LINE__);
  4872. if (crtc->state && !(crtc->state->active)) {
  4873. DDPMSG("%s:crtc is inactive -- skip\n", __func__);
  4874. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  4875. goto done;
  4876. }
  4877. output_comp = mtk_ddp_comp_request_output(mtk_crtc);
  4878. if (unlikely(!output_comp)) {
  4879. DDPPR_ERR("%s: invalid output comp\n", __func__);
  4880. ret = -EINVAL;
  4881. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  4882. goto done;
  4883. }
  4884. dsi = container_of(output_comp, struct mtk_dsi, ddp_comp);
  4885. if (dsi && dsi->ext && dsi->ext->params)
  4886. dsi_params = dsi->ext->params;//get_dsi_params_handle((uint32_t)(PM_DSI0));
  4887. if (dsi && dsi_params) {
  4888. for (i = 0; i < ESD_CHECK_NUM; i++) {
  4889. if (dsi_params->lcm_esd_check_table[i].cmd == 0)
  4890. break;
  4891. if ((uint32_t)(dsi_params->lcm_esd_check_table[i].cmd) == cmd) {
  4892. cmd_matched = 1;
  4893. break;
  4894. }
  4895. }
  4896. } else {
  4897. DDPPR_ERR("%s: dsi or panel is invalid -- skip\n", __func__);
  4898. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  4899. goto done;
  4900. }
  4901. if (!cmd_matched) {
  4902. DDPPR_ERR("%s: cmd not matched support cmd=%d, test cmd =%d -- skip\n", __func__,
  4903. dsi_params->lcm_esd_check_table[0].cmd, cmd);
  4904. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  4905. goto done;
  4906. }
  4907. mtk_drm_idlemgr_kick(__func__, &mtk_crtc->base, 0);
  4908. /* 0 disable esd check */
  4909. if (mtk_drm_lcm_is_connect())
  4910. mtk_disp_esd_check_switch(crtc, false);
  4911. /* 1 stop crtc */
  4912. mtk_crtc_stop_for_pm(mtk_crtc, true);
  4913. /* 2 stop dsi */
  4914. mtk_dsi_stop(dsi);
  4915. mtk_dsi_clk_hs_mode(dsi, 0);
  4916. mtk_dsi_set_interrupt_enable(dsi);
  4917. /* 3 read lcm esd check */
  4918. ret = fbconfig_get_esd_check(dsi, cmd, buffer, num);
  4919. /* 4 start crtc */
  4920. mtk_crtc_start_for_pm(crtc);
  4921. /* 5 start dsi */
  4922. mtk_dsi_clk_hs_mode(dsi, 1);
  4923. mtk_dsi_start(dsi);
  4924. /* 6 enable esd check */
  4925. if (mtk_drm_lcm_is_connect())
  4926. mtk_disp_esd_check_switch(crtc, true);
  4927. mtk_crtc_hw_block_ready(crtc);
  4928. if (mtk_crtc_is_frame_trigger_mode(crtc)) {
  4929. struct cmdq_pkt *cmdq_handle;
  4930. mtk_crtc_pkt_create(&cmdq_handle, &mtk_crtc->base,
  4931. mtk_crtc->gce_obj.client[CLIENT_CFG]);
  4932. cmdq_pkt_set_event(cmdq_handle,
  4933. mtk_crtc->gce_obj.event[EVENT_STREAM_DIRTY]);
  4934. cmdq_pkt_set_event(cmdq_handle,
  4935. mtk_crtc->gce_obj.event[EVENT_CABC_EOF]);
  4936. cmdq_pkt_set_event(cmdq_handle,
  4937. mtk_crtc->gce_obj.event[EVENT_ESD_EOF]);
  4938. cmdq_pkt_flush(cmdq_handle);
  4939. cmdq_pkt_destroy(cmdq_handle);
  4940. }
  4941. mtk_drm_idlemgr_kick(__func__, &mtk_crtc->base, 0);
  4942. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  4943. done:
  4944. return ret;
  4945. }
  4946. void Panel_Master_primary_display_config_dsi(struct mtk_dsi *dsi,
  4947. const char *name, uint32_t config_value)
  4948. {
  4949. unsigned long mipi_tx_rate;
  4950. if (!strcmp(name, "PM_CLK")) {
  4951. pr_debug("Pmaster_config_dsi: PM_CLK:%d\n", config_value);
  4952. dsi->ext->params->pll_clk = config_value;
  4953. } else if (!strcmp(name, "PM_SSC")) {
  4954. pr_debug("Pmaster_config_dsi: PM_SSC:%d\n", config_value);
  4955. dsi->ext->params->ssc_range = config_value;
  4956. return;
  4957. }
  4958. dsi->data_rate = dsi->ext->params->pll_clk * 2;
  4959. mipi_tx_rate = dsi->data_rate * 1000000;
  4960. mtk_dsi_set_interrupt_enable(dsi);
  4961. /* config dsi clk */
  4962. clk_set_rate(dsi->hs_clk, mipi_tx_rate);
  4963. mtk_mipi_tx_pll_rate_set_adpt(dsi->phy, dsi->data_rate);
  4964. mtk_dsi_phy_timconfig(dsi, NULL);
  4965. if (!mtk_dsi_is_cmd_mode(&dsi->ddp_comp)) {
  4966. mtk_dsi_set_vm_cmd(dsi);
  4967. mtk_dsi_calc_vdo_timing(dsi);
  4968. mtk_dsi_config_vdo_timing(dsi);
  4969. }
  4970. }
  4971. u32 PanelMaster_get_CC(struct mtk_dsi *dsi)
  4972. {
  4973. u32 tmp_reg;
  4974. tmp_reg = readl(dsi->regs + DSI_TXRX_CTRL);
  4975. tmp_reg &= HSTX_CKLP_EN;
  4976. return (tmp_reg >> 16);
  4977. }
  4978. void PanelMaster_set_CC(struct mtk_dsi *dsi, u32 enable)
  4979. {
  4980. u32 tmp_reg;
  4981. DDPMSG("set_cc :%d\n", enable);
  4982. tmp_reg = readl(dsi->regs + DSI_TXRX_CTRL);
  4983. tmp_reg &= (~HSTX_CKLP_EN);
  4984. tmp_reg |= (enable << 16);
  4985. writel(tmp_reg, dsi->regs + DSI_TXRX_CTRL);
  4986. }
  4987. struct mtk_dsi *pm_get_mtk_dsi(struct drm_crtc *crtc)
  4988. {
  4989. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  4990. struct mtk_ddp_comp *output_comp = NULL;
  4991. struct mtk_dsi *dsi = NULL;
  4992. if (crtc->state && !(crtc->state->active)) {
  4993. DDPMSG("%s: crtc is inactive -- skip\n", __func__);
  4994. return dsi;
  4995. }
  4996. output_comp = mtk_ddp_comp_request_output(mtk_crtc);
  4997. if (unlikely(!output_comp)) {
  4998. DDPPR_ERR("%s: invalid output comp\n", __func__);
  4999. return dsi;
  5000. }
  5001. dsi = container_of(output_comp, struct mtk_dsi, ddp_comp);
  5002. return dsi;
  5003. }
  5004. int Panel_Master_dsi_config_entry(struct drm_crtc *crtc,
  5005. const char *name, int config_value)
  5006. {
  5007. int ret = 0;
  5008. struct mtk_dsi *dsi = NULL;
  5009. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  5010. dsi = pm_get_mtk_dsi(crtc);
  5011. if (!dsi) {
  5012. ret = -EINVAL;
  5013. goto done;
  5014. }
  5015. mtk_drm_idlemgr_kick(__func__, &mtk_crtc->base, 0);
  5016. /* disable esd check */
  5017. if (mtk_drm_lcm_is_connect())
  5018. mtk_disp_esd_check_switch(crtc, false);
  5019. if ((!strcmp(name, "PM_CLK")) || (!strcmp(name, "PM_SSC"))) {
  5020. Panel_Master_primary_display_config_dsi(dsi,
  5021. name, config_value);
  5022. } else if (!strcmp(name, "PM_DRIVER_IC_RESET") && (!config_value)) {
  5023. if (dsi->panel) {
  5024. if (drm_panel_prepare(dsi->panel))
  5025. DDPPR_ERR("failed to enable the panel\n");
  5026. }
  5027. }
  5028. /* enable esd check */
  5029. if (mtk_drm_lcm_is_connect())
  5030. mtk_disp_esd_check_switch(crtc, true);
  5031. done:
  5032. return ret;
  5033. }
  5034. int Panel_Master_lcm_get_dsi_timing_entry(struct drm_crtc *crtc,
  5035. int type)
  5036. {
  5037. int ret = 0;
  5038. struct mtk_dsi *dsi = NULL;
  5039. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  5040. DDP_MUTEX_LOCK(&mtk_crtc->lock, __func__, __LINE__);
  5041. dsi = pm_get_mtk_dsi(crtc);
  5042. if (!dsi) {
  5043. ret = -EINVAL;
  5044. goto done;
  5045. }
  5046. mtk_drm_idlemgr_kick(__func__, &mtk_crtc->base, 0);
  5047. ret = PanelMaster_get_dsi_timing(dsi, type);
  5048. done:
  5049. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  5050. return ret;
  5051. }
  5052. int Panel_Master_mipi_set_timing_entry(struct drm_crtc *crtc,
  5053. struct MIPI_TIMING timing)
  5054. {
  5055. int ret = 0;
  5056. struct mtk_dsi *dsi = NULL;
  5057. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  5058. DDP_MUTEX_LOCK(&mtk_crtc->lock, __func__, __LINE__);
  5059. dsi = pm_get_mtk_dsi(crtc);
  5060. if (!dsi) {
  5061. ret = -EINVAL;
  5062. goto done;
  5063. }
  5064. mtk_drm_idlemgr_kick(__func__, &mtk_crtc->base, 0);
  5065. ret = PanelMaster_DSI_set_timing(dsi, timing);
  5066. done:
  5067. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  5068. return ret;
  5069. }
  5070. int Panel_Master_mipi_set_cc_entry(struct drm_crtc *crtc,
  5071. int enable)
  5072. {
  5073. int ret = 0;
  5074. struct mtk_dsi *dsi = NULL;
  5075. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  5076. DDP_MUTEX_LOCK(&mtk_crtc->lock, __func__, __LINE__);
  5077. dsi = pm_get_mtk_dsi(crtc);
  5078. if (!dsi) {
  5079. ret = -EINVAL;
  5080. goto done;
  5081. }
  5082. mtk_drm_idlemgr_kick(__func__, &mtk_crtc->base, 0);
  5083. PanelMaster_set_CC(dsi, enable);
  5084. done:
  5085. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  5086. return ret;
  5087. }
  5088. int Panel_Master_mipi_get_cc_entry(struct drm_crtc *crtc)
  5089. {
  5090. int ret = 0;
  5091. struct mtk_dsi *dsi = NULL;
  5092. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  5093. DDP_MUTEX_LOCK(&mtk_crtc->lock, __func__, __LINE__);
  5094. dsi = pm_get_mtk_dsi(crtc);
  5095. if (!dsi) {
  5096. ret = -EINVAL;
  5097. goto done;
  5098. }
  5099. mtk_drm_idlemgr_kick(__func__, &mtk_crtc->base, 0);
  5100. ret = PanelMaster_get_CC(dsi);
  5101. done:
  5102. DDP_MUTEX_UNLOCK(&mtk_crtc->lock, __func__, __LINE__);
  5103. return ret;
  5104. }
  5105. /* ******************* end PanelMaster ***************** */