sor.c 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2013 NVIDIA Corporation
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/clk-provider.h>
  7. #include <linux/debugfs.h>
  8. #include <linux/io.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/pm_runtime.h>
  13. #include <linux/regulator/consumer.h>
  14. #include <linux/reset.h>
  15. #include <soc/tegra/pmc.h>
  16. #include <drm/display/drm_dp_helper.h>
  17. #include <drm/display/drm_scdc_helper.h>
  18. #include <drm/drm_atomic_helper.h>
  19. #include <drm/drm_debugfs.h>
  20. #include <drm/drm_edid.h>
  21. #include <drm/drm_eld.h>
  22. #include <drm/drm_file.h>
  23. #include <drm/drm_panel.h>
  24. #include <drm/drm_simple_kms_helper.h>
  25. #include "dc.h"
  26. #include "dp.h"
  27. #include "drm.h"
  28. #include "hda.h"
  29. #include "sor.h"
  30. #include "trace.h"
  31. #define SOR_REKEY 0x38
  32. struct tegra_sor_hdmi_settings {
  33. unsigned long frequency;
  34. u8 vcocap;
  35. u8 filter;
  36. u8 ichpmp;
  37. u8 loadadj;
  38. u8 tmds_termadj;
  39. u8 tx_pu_value;
  40. u8 bg_temp_coef;
  41. u8 bg_vref_level;
  42. u8 avdd10_level;
  43. u8 avdd14_level;
  44. u8 sparepll;
  45. u8 drive_current[4];
  46. u8 preemphasis[4];
  47. };
  48. #if 1
  49. static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
  50. {
  51. .frequency = 54000000,
  52. .vcocap = 0x0,
  53. .filter = 0x0,
  54. .ichpmp = 0x1,
  55. .loadadj = 0x3,
  56. .tmds_termadj = 0x9,
  57. .tx_pu_value = 0x10,
  58. .bg_temp_coef = 0x3,
  59. .bg_vref_level = 0x8,
  60. .avdd10_level = 0x4,
  61. .avdd14_level = 0x4,
  62. .sparepll = 0x0,
  63. .drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
  64. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  65. }, {
  66. .frequency = 75000000,
  67. .vcocap = 0x3,
  68. .filter = 0x0,
  69. .ichpmp = 0x1,
  70. .loadadj = 0x3,
  71. .tmds_termadj = 0x9,
  72. .tx_pu_value = 0x40,
  73. .bg_temp_coef = 0x3,
  74. .bg_vref_level = 0x8,
  75. .avdd10_level = 0x4,
  76. .avdd14_level = 0x4,
  77. .sparepll = 0x0,
  78. .drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
  79. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  80. }, {
  81. .frequency = 150000000,
  82. .vcocap = 0x3,
  83. .filter = 0x0,
  84. .ichpmp = 0x1,
  85. .loadadj = 0x3,
  86. .tmds_termadj = 0x9,
  87. .tx_pu_value = 0x66,
  88. .bg_temp_coef = 0x3,
  89. .bg_vref_level = 0x8,
  90. .avdd10_level = 0x4,
  91. .avdd14_level = 0x4,
  92. .sparepll = 0x0,
  93. .drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
  94. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  95. }, {
  96. .frequency = 300000000,
  97. .vcocap = 0x3,
  98. .filter = 0x0,
  99. .ichpmp = 0x1,
  100. .loadadj = 0x3,
  101. .tmds_termadj = 0x9,
  102. .tx_pu_value = 0x66,
  103. .bg_temp_coef = 0x3,
  104. .bg_vref_level = 0xa,
  105. .avdd10_level = 0x4,
  106. .avdd14_level = 0x4,
  107. .sparepll = 0x0,
  108. .drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
  109. .preemphasis = { 0x00, 0x17, 0x17, 0x17 },
  110. }, {
  111. .frequency = 600000000,
  112. .vcocap = 0x3,
  113. .filter = 0x0,
  114. .ichpmp = 0x1,
  115. .loadadj = 0x3,
  116. .tmds_termadj = 0x9,
  117. .tx_pu_value = 0x66,
  118. .bg_temp_coef = 0x3,
  119. .bg_vref_level = 0x8,
  120. .avdd10_level = 0x4,
  121. .avdd14_level = 0x4,
  122. .sparepll = 0x0,
  123. .drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
  124. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  125. },
  126. };
  127. #else
  128. static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
  129. {
  130. .frequency = 75000000,
  131. .vcocap = 0x3,
  132. .filter = 0x0,
  133. .ichpmp = 0x1,
  134. .loadadj = 0x3,
  135. .tmds_termadj = 0x9,
  136. .tx_pu_value = 0x40,
  137. .bg_temp_coef = 0x3,
  138. .bg_vref_level = 0x8,
  139. .avdd10_level = 0x4,
  140. .avdd14_level = 0x4,
  141. .sparepll = 0x0,
  142. .drive_current = { 0x29, 0x29, 0x29, 0x29 },
  143. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  144. }, {
  145. .frequency = 150000000,
  146. .vcocap = 0x3,
  147. .filter = 0x0,
  148. .ichpmp = 0x1,
  149. .loadadj = 0x3,
  150. .tmds_termadj = 0x9,
  151. .tx_pu_value = 0x66,
  152. .bg_temp_coef = 0x3,
  153. .bg_vref_level = 0x8,
  154. .avdd10_level = 0x4,
  155. .avdd14_level = 0x4,
  156. .sparepll = 0x0,
  157. .drive_current = { 0x30, 0x37, 0x37, 0x37 },
  158. .preemphasis = { 0x01, 0x02, 0x02, 0x02 },
  159. }, {
  160. .frequency = 300000000,
  161. .vcocap = 0x3,
  162. .filter = 0x0,
  163. .ichpmp = 0x6,
  164. .loadadj = 0x3,
  165. .tmds_termadj = 0x9,
  166. .tx_pu_value = 0x66,
  167. .bg_temp_coef = 0x3,
  168. .bg_vref_level = 0xf,
  169. .avdd10_level = 0x4,
  170. .avdd14_level = 0x4,
  171. .sparepll = 0x0,
  172. .drive_current = { 0x30, 0x37, 0x37, 0x37 },
  173. .preemphasis = { 0x10, 0x3e, 0x3e, 0x3e },
  174. }, {
  175. .frequency = 600000000,
  176. .vcocap = 0x3,
  177. .filter = 0x0,
  178. .ichpmp = 0xa,
  179. .loadadj = 0x3,
  180. .tmds_termadj = 0xb,
  181. .tx_pu_value = 0x66,
  182. .bg_temp_coef = 0x3,
  183. .bg_vref_level = 0xe,
  184. .avdd10_level = 0x4,
  185. .avdd14_level = 0x4,
  186. .sparepll = 0x0,
  187. .drive_current = { 0x35, 0x3e, 0x3e, 0x3e },
  188. .preemphasis = { 0x02, 0x3f, 0x3f, 0x3f },
  189. },
  190. };
  191. #endif
  192. static const struct tegra_sor_hdmi_settings tegra186_sor_hdmi_defaults[] = {
  193. {
  194. .frequency = 54000000,
  195. .vcocap = 0,
  196. .filter = 5,
  197. .ichpmp = 5,
  198. .loadadj = 3,
  199. .tmds_termadj = 0xf,
  200. .tx_pu_value = 0,
  201. .bg_temp_coef = 3,
  202. .bg_vref_level = 8,
  203. .avdd10_level = 4,
  204. .avdd14_level = 4,
  205. .sparepll = 0x54,
  206. .drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
  207. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  208. }, {
  209. .frequency = 75000000,
  210. .vcocap = 1,
  211. .filter = 5,
  212. .ichpmp = 5,
  213. .loadadj = 3,
  214. .tmds_termadj = 0xf,
  215. .tx_pu_value = 0,
  216. .bg_temp_coef = 3,
  217. .bg_vref_level = 8,
  218. .avdd10_level = 4,
  219. .avdd14_level = 4,
  220. .sparepll = 0x44,
  221. .drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
  222. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  223. }, {
  224. .frequency = 150000000,
  225. .vcocap = 3,
  226. .filter = 5,
  227. .ichpmp = 5,
  228. .loadadj = 3,
  229. .tmds_termadj = 15,
  230. .tx_pu_value = 0x66 /* 0 */,
  231. .bg_temp_coef = 3,
  232. .bg_vref_level = 8,
  233. .avdd10_level = 4,
  234. .avdd14_level = 4,
  235. .sparepll = 0x00, /* 0x34 */
  236. .drive_current = { 0x3a, 0x3a, 0x3a, 0x37 },
  237. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  238. }, {
  239. .frequency = 300000000,
  240. .vcocap = 3,
  241. .filter = 5,
  242. .ichpmp = 5,
  243. .loadadj = 3,
  244. .tmds_termadj = 15,
  245. .tx_pu_value = 64,
  246. .bg_temp_coef = 3,
  247. .bg_vref_level = 8,
  248. .avdd10_level = 4,
  249. .avdd14_level = 4,
  250. .sparepll = 0x34,
  251. .drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
  252. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  253. }, {
  254. .frequency = 600000000,
  255. .vcocap = 3,
  256. .filter = 5,
  257. .ichpmp = 5,
  258. .loadadj = 3,
  259. .tmds_termadj = 12,
  260. .tx_pu_value = 96,
  261. .bg_temp_coef = 3,
  262. .bg_vref_level = 8,
  263. .avdd10_level = 4,
  264. .avdd14_level = 4,
  265. .sparepll = 0x34,
  266. .drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
  267. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  268. }
  269. };
  270. static const struct tegra_sor_hdmi_settings tegra194_sor_hdmi_defaults[] = {
  271. {
  272. .frequency = 54000000,
  273. .vcocap = 0,
  274. .filter = 5,
  275. .ichpmp = 5,
  276. .loadadj = 3,
  277. .tmds_termadj = 0xf,
  278. .tx_pu_value = 0,
  279. .bg_temp_coef = 3,
  280. .bg_vref_level = 8,
  281. .avdd10_level = 4,
  282. .avdd14_level = 4,
  283. .sparepll = 0x54,
  284. .drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
  285. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  286. }, {
  287. .frequency = 75000000,
  288. .vcocap = 1,
  289. .filter = 5,
  290. .ichpmp = 5,
  291. .loadadj = 3,
  292. .tmds_termadj = 0xf,
  293. .tx_pu_value = 0,
  294. .bg_temp_coef = 3,
  295. .bg_vref_level = 8,
  296. .avdd10_level = 4,
  297. .avdd14_level = 4,
  298. .sparepll = 0x44,
  299. .drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
  300. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  301. }, {
  302. .frequency = 150000000,
  303. .vcocap = 3,
  304. .filter = 5,
  305. .ichpmp = 5,
  306. .loadadj = 3,
  307. .tmds_termadj = 15,
  308. .tx_pu_value = 0x66 /* 0 */,
  309. .bg_temp_coef = 3,
  310. .bg_vref_level = 8,
  311. .avdd10_level = 4,
  312. .avdd14_level = 4,
  313. .sparepll = 0x00, /* 0x34 */
  314. .drive_current = { 0x3a, 0x3a, 0x3a, 0x37 },
  315. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  316. }, {
  317. .frequency = 300000000,
  318. .vcocap = 3,
  319. .filter = 5,
  320. .ichpmp = 5,
  321. .loadadj = 3,
  322. .tmds_termadj = 15,
  323. .tx_pu_value = 64,
  324. .bg_temp_coef = 3,
  325. .bg_vref_level = 8,
  326. .avdd10_level = 4,
  327. .avdd14_level = 4,
  328. .sparepll = 0x34,
  329. .drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
  330. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  331. }, {
  332. .frequency = 600000000,
  333. .vcocap = 3,
  334. .filter = 5,
  335. .ichpmp = 5,
  336. .loadadj = 3,
  337. .tmds_termadj = 12,
  338. .tx_pu_value = 96,
  339. .bg_temp_coef = 3,
  340. .bg_vref_level = 8,
  341. .avdd10_level = 4,
  342. .avdd14_level = 4,
  343. .sparepll = 0x34,
  344. .drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
  345. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  346. }
  347. };
  348. struct tegra_sor_regs {
  349. unsigned int head_state0;
  350. unsigned int head_state1;
  351. unsigned int head_state2;
  352. unsigned int head_state3;
  353. unsigned int head_state4;
  354. unsigned int head_state5;
  355. unsigned int pll0;
  356. unsigned int pll1;
  357. unsigned int pll2;
  358. unsigned int pll3;
  359. unsigned int dp_padctl0;
  360. unsigned int dp_padctl2;
  361. };
  362. struct tegra_sor_soc {
  363. bool supports_lvds;
  364. bool supports_hdmi;
  365. bool supports_dp;
  366. bool supports_audio;
  367. bool supports_hdcp;
  368. const struct tegra_sor_regs *regs;
  369. bool has_nvdisplay;
  370. const struct tegra_sor_hdmi_settings *settings;
  371. unsigned int num_settings;
  372. const u8 *xbar_cfg;
  373. const u8 *lane_map;
  374. const u8 (*voltage_swing)[4][4];
  375. const u8 (*pre_emphasis)[4][4];
  376. const u8 (*post_cursor)[4][4];
  377. const u8 (*tx_pu)[4][4];
  378. };
  379. struct tegra_sor;
  380. struct tegra_sor_ops {
  381. const char *name;
  382. int (*probe)(struct tegra_sor *sor);
  383. void (*audio_enable)(struct tegra_sor *sor);
  384. void (*audio_disable)(struct tegra_sor *sor);
  385. };
  386. struct tegra_sor {
  387. struct host1x_client client;
  388. struct tegra_output output;
  389. struct device *dev;
  390. const struct tegra_sor_soc *soc;
  391. void __iomem *regs;
  392. unsigned int index;
  393. unsigned int irq;
  394. struct reset_control *rst;
  395. struct clk *clk_parent;
  396. struct clk *clk_safe;
  397. struct clk *clk_out;
  398. struct clk *clk_pad;
  399. struct clk *clk_dp;
  400. struct clk *clk;
  401. u8 xbar_cfg[5];
  402. struct drm_dp_link link;
  403. struct drm_dp_aux *aux;
  404. struct drm_info_list *debugfs_files;
  405. const struct tegra_sor_ops *ops;
  406. enum tegra_io_pad pad;
  407. /* for HDMI 2.0 */
  408. struct tegra_sor_hdmi_settings *settings;
  409. unsigned int num_settings;
  410. struct regulator *avdd_io_supply;
  411. struct regulator *vdd_pll_supply;
  412. struct regulator *hdmi_supply;
  413. struct delayed_work scdc;
  414. bool scdc_enabled;
  415. struct tegra_hda_format format;
  416. };
  417. struct tegra_sor_state {
  418. struct drm_connector_state base;
  419. unsigned int link_speed;
  420. unsigned long pclk;
  421. unsigned int bpc;
  422. };
  423. static inline struct tegra_sor_state *
  424. to_sor_state(struct drm_connector_state *state)
  425. {
  426. return container_of(state, struct tegra_sor_state, base);
  427. }
  428. struct tegra_sor_config {
  429. u32 bits_per_pixel;
  430. u32 active_polarity;
  431. u32 active_count;
  432. u32 tu_size;
  433. u32 active_frac;
  434. u32 watermark;
  435. u32 hblank_symbols;
  436. u32 vblank_symbols;
  437. };
  438. static inline struct tegra_sor *
  439. host1x_client_to_sor(struct host1x_client *client)
  440. {
  441. return container_of(client, struct tegra_sor, client);
  442. }
  443. static inline struct tegra_sor *to_sor(struct tegra_output *output)
  444. {
  445. return container_of(output, struct tegra_sor, output);
  446. }
  447. static inline u32 tegra_sor_readl(struct tegra_sor *sor, unsigned int offset)
  448. {
  449. u32 value = readl(sor->regs + (offset << 2));
  450. trace_sor_readl(sor->dev, offset, value);
  451. return value;
  452. }
  453. static inline void tegra_sor_writel(struct tegra_sor *sor, u32 value,
  454. unsigned int offset)
  455. {
  456. trace_sor_writel(sor->dev, offset, value);
  457. writel(value, sor->regs + (offset << 2));
  458. }
  459. static int tegra_sor_set_parent_clock(struct tegra_sor *sor, struct clk *parent)
  460. {
  461. int err;
  462. clk_disable_unprepare(sor->clk);
  463. err = clk_set_parent(sor->clk_out, parent);
  464. if (err < 0)
  465. return err;
  466. err = clk_prepare_enable(sor->clk);
  467. if (err < 0)
  468. return err;
  469. return 0;
  470. }
  471. struct tegra_clk_sor_pad {
  472. struct clk_hw hw;
  473. struct tegra_sor *sor;
  474. };
  475. static inline struct tegra_clk_sor_pad *to_pad(struct clk_hw *hw)
  476. {
  477. return container_of(hw, struct tegra_clk_sor_pad, hw);
  478. }
  479. static const char * const tegra_clk_sor_pad_parents[2][2] = {
  480. { "pll_d_out0", "pll_dp" },
  481. { "pll_d2_out0", "pll_dp" },
  482. };
  483. /*
  484. * Implementing ->set_parent() here isn't really required because the parent
  485. * will be explicitly selected in the driver code via the DP_CLK_SEL mux in
  486. * the SOR_CLK_CNTRL register. This is primarily for compatibility with the
  487. * Tegra186 and later SoC generations where the BPMP implements this clock
  488. * and doesn't expose the mux via the common clock framework.
  489. */
  490. static int tegra_clk_sor_pad_set_parent(struct clk_hw *hw, u8 index)
  491. {
  492. struct tegra_clk_sor_pad *pad = to_pad(hw);
  493. struct tegra_sor *sor = pad->sor;
  494. u32 value;
  495. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  496. value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
  497. switch (index) {
  498. case 0:
  499. value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
  500. break;
  501. case 1:
  502. value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
  503. break;
  504. }
  505. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  506. return 0;
  507. }
  508. static u8 tegra_clk_sor_pad_get_parent(struct clk_hw *hw)
  509. {
  510. struct tegra_clk_sor_pad *pad = to_pad(hw);
  511. struct tegra_sor *sor = pad->sor;
  512. u8 parent = U8_MAX;
  513. u32 value;
  514. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  515. switch (value & SOR_CLK_CNTRL_DP_CLK_SEL_MASK) {
  516. case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK:
  517. case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_PCLK:
  518. parent = 0;
  519. break;
  520. case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK:
  521. case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_DPCLK:
  522. parent = 1;
  523. break;
  524. }
  525. return parent;
  526. }
  527. static const struct clk_ops tegra_clk_sor_pad_ops = {
  528. .determine_rate = clk_hw_determine_rate_no_reparent,
  529. .set_parent = tegra_clk_sor_pad_set_parent,
  530. .get_parent = tegra_clk_sor_pad_get_parent,
  531. };
  532. static struct clk *tegra_clk_sor_pad_register(struct tegra_sor *sor,
  533. const char *name)
  534. {
  535. struct tegra_clk_sor_pad *pad;
  536. struct clk_init_data init;
  537. struct clk *clk;
  538. pad = devm_kzalloc(sor->dev, sizeof(*pad), GFP_KERNEL);
  539. if (!pad)
  540. return ERR_PTR(-ENOMEM);
  541. pad->sor = sor;
  542. init.name = name;
  543. init.flags = 0;
  544. init.parent_names = tegra_clk_sor_pad_parents[sor->index];
  545. init.num_parents = ARRAY_SIZE(tegra_clk_sor_pad_parents[sor->index]);
  546. init.ops = &tegra_clk_sor_pad_ops;
  547. pad->hw.init = &init;
  548. clk = devm_clk_register(sor->dev, &pad->hw);
  549. return clk;
  550. }
  551. static void tegra_sor_filter_rates(struct tegra_sor *sor)
  552. {
  553. struct drm_dp_link *link = &sor->link;
  554. unsigned int i;
  555. /* Tegra only supports RBR, HBR and HBR2 */
  556. for (i = 0; i < link->num_rates; i++) {
  557. switch (link->rates[i]) {
  558. case 1620000:
  559. case 2700000:
  560. case 5400000:
  561. break;
  562. default:
  563. DRM_DEBUG_KMS("link rate %lu kHz not supported\n",
  564. link->rates[i]);
  565. link->rates[i] = 0;
  566. break;
  567. }
  568. }
  569. drm_dp_link_update_rates(link);
  570. }
  571. static int tegra_sor_power_up_lanes(struct tegra_sor *sor, unsigned int lanes)
  572. {
  573. unsigned long timeout;
  574. u32 value;
  575. /*
  576. * Clear or set the PD_TXD bit corresponding to each lane, depending
  577. * on whether it is used or not.
  578. */
  579. value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
  580. if (lanes <= 2)
  581. value &= ~(SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[3]) |
  582. SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[2]));
  583. else
  584. value |= SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[3]) |
  585. SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[2]);
  586. if (lanes <= 1)
  587. value &= ~SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[1]);
  588. else
  589. value |= SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[1]);
  590. if (lanes == 0)
  591. value &= ~SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[0]);
  592. else
  593. value |= SOR_DP_PADCTL_PD_TXD(sor->soc->lane_map[0]);
  594. tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
  595. /* start lane sequencer */
  596. value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
  597. SOR_LANE_SEQ_CTL_POWER_STATE_UP;
  598. tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
  599. timeout = jiffies + msecs_to_jiffies(250);
  600. while (time_before(jiffies, timeout)) {
  601. value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
  602. if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
  603. break;
  604. usleep_range(250, 1000);
  605. }
  606. if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
  607. return -ETIMEDOUT;
  608. return 0;
  609. }
  610. static int tegra_sor_power_down_lanes(struct tegra_sor *sor)
  611. {
  612. unsigned long timeout;
  613. u32 value;
  614. /* power down all lanes */
  615. value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
  616. value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
  617. SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
  618. tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
  619. /* start lane sequencer */
  620. value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
  621. SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
  622. tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
  623. timeout = jiffies + msecs_to_jiffies(250);
  624. while (time_before(jiffies, timeout)) {
  625. value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
  626. if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
  627. break;
  628. usleep_range(25, 100);
  629. }
  630. if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
  631. return -ETIMEDOUT;
  632. return 0;
  633. }
  634. static void tegra_sor_dp_precharge(struct tegra_sor *sor, unsigned int lanes)
  635. {
  636. u32 value;
  637. /* pre-charge all used lanes */
  638. value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
  639. if (lanes <= 2)
  640. value &= ~(SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[3]) |
  641. SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[2]));
  642. else
  643. value |= SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[3]) |
  644. SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[2]);
  645. if (lanes <= 1)
  646. value &= ~SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[1]);
  647. else
  648. value |= SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[1]);
  649. if (lanes == 0)
  650. value &= ~SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[0]);
  651. else
  652. value |= SOR_DP_PADCTL_CM_TXD(sor->soc->lane_map[0]);
  653. tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
  654. usleep_range(15, 100);
  655. value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
  656. value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
  657. SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
  658. tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
  659. }
  660. static void tegra_sor_dp_term_calibrate(struct tegra_sor *sor)
  661. {
  662. u32 mask = 0x08, adj = 0, value;
  663. /* enable pad calibration logic */
  664. value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
  665. value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
  666. tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
  667. value = tegra_sor_readl(sor, sor->soc->regs->pll1);
  668. value |= SOR_PLL1_TMDS_TERM;
  669. tegra_sor_writel(sor, value, sor->soc->regs->pll1);
  670. while (mask) {
  671. adj |= mask;
  672. value = tegra_sor_readl(sor, sor->soc->regs->pll1);
  673. value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
  674. value |= SOR_PLL1_TMDS_TERMADJ(adj);
  675. tegra_sor_writel(sor, value, sor->soc->regs->pll1);
  676. usleep_range(100, 200);
  677. value = tegra_sor_readl(sor, sor->soc->regs->pll1);
  678. if (value & SOR_PLL1_TERM_COMPOUT)
  679. adj &= ~mask;
  680. mask >>= 1;
  681. }
  682. value = tegra_sor_readl(sor, sor->soc->regs->pll1);
  683. value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
  684. value |= SOR_PLL1_TMDS_TERMADJ(adj);
  685. tegra_sor_writel(sor, value, sor->soc->regs->pll1);
  686. /* disable pad calibration logic */
  687. value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
  688. value |= SOR_DP_PADCTL_PAD_CAL_PD;
  689. tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
  690. }
  691. static int tegra_sor_dp_link_apply_training(struct drm_dp_link *link)
  692. {
  693. struct tegra_sor *sor = container_of(link, struct tegra_sor, link);
  694. u32 voltage_swing = 0, pre_emphasis = 0, post_cursor = 0;
  695. const struct tegra_sor_soc *soc = sor->soc;
  696. u32 pattern = 0, tx_pu = 0, value;
  697. unsigned int i;
  698. for (value = 0, i = 0; i < link->lanes; i++) {
  699. u8 vs = link->train.request.voltage_swing[i];
  700. u8 pe = link->train.request.pre_emphasis[i];
  701. u8 pc = link->train.request.post_cursor[i];
  702. u8 shift = sor->soc->lane_map[i] << 3;
  703. voltage_swing |= soc->voltage_swing[pc][vs][pe] << shift;
  704. pre_emphasis |= soc->pre_emphasis[pc][vs][pe] << shift;
  705. post_cursor |= soc->post_cursor[pc][vs][pe] << shift;
  706. if (sor->soc->tx_pu[pc][vs][pe] > tx_pu)
  707. tx_pu = sor->soc->tx_pu[pc][vs][pe];
  708. switch (link->train.pattern) {
  709. case DP_TRAINING_PATTERN_DISABLE:
  710. value = SOR_DP_TPG_SCRAMBLER_GALIOS |
  711. SOR_DP_TPG_PATTERN_NONE;
  712. break;
  713. case DP_TRAINING_PATTERN_1:
  714. value = SOR_DP_TPG_SCRAMBLER_NONE |
  715. SOR_DP_TPG_PATTERN_TRAIN1;
  716. break;
  717. case DP_TRAINING_PATTERN_2:
  718. value = SOR_DP_TPG_SCRAMBLER_NONE |
  719. SOR_DP_TPG_PATTERN_TRAIN2;
  720. break;
  721. case DP_TRAINING_PATTERN_3:
  722. value = SOR_DP_TPG_SCRAMBLER_NONE |
  723. SOR_DP_TPG_PATTERN_TRAIN3;
  724. break;
  725. default:
  726. return -EINVAL;
  727. }
  728. if (link->caps.channel_coding)
  729. value |= SOR_DP_TPG_CHANNEL_CODING;
  730. pattern = pattern << 8 | value;
  731. }
  732. tegra_sor_writel(sor, voltage_swing, SOR_LANE_DRIVE_CURRENT0);
  733. tegra_sor_writel(sor, pre_emphasis, SOR_LANE_PREEMPHASIS0);
  734. if (link->caps.tps3_supported)
  735. tegra_sor_writel(sor, post_cursor, SOR_LANE_POSTCURSOR0);
  736. tegra_sor_writel(sor, pattern, SOR_DP_TPG);
  737. value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
  738. value &= ~SOR_DP_PADCTL_TX_PU_MASK;
  739. value |= SOR_DP_PADCTL_TX_PU_ENABLE;
  740. value |= SOR_DP_PADCTL_TX_PU(tx_pu);
  741. tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
  742. usleep_range(20, 100);
  743. return 0;
  744. }
  745. static int tegra_sor_dp_link_configure(struct drm_dp_link *link)
  746. {
  747. struct tegra_sor *sor = container_of(link, struct tegra_sor, link);
  748. unsigned int rate, lanes;
  749. u32 value;
  750. int err;
  751. rate = drm_dp_link_rate_to_bw_code(link->rate);
  752. lanes = link->lanes;
  753. /* configure link speed and lane count */
  754. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  755. value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
  756. value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
  757. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  758. value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
  759. value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
  760. value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
  761. if (link->caps.enhanced_framing)
  762. value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
  763. tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
  764. usleep_range(400, 1000);
  765. /* configure load pulse position adjustment */
  766. value = tegra_sor_readl(sor, sor->soc->regs->pll1);
  767. value &= ~SOR_PLL1_LOADADJ_MASK;
  768. switch (rate) {
  769. case DP_LINK_BW_1_62:
  770. value |= SOR_PLL1_LOADADJ(0x3);
  771. break;
  772. case DP_LINK_BW_2_7:
  773. value |= SOR_PLL1_LOADADJ(0x4);
  774. break;
  775. case DP_LINK_BW_5_4:
  776. value |= SOR_PLL1_LOADADJ(0x6);
  777. break;
  778. }
  779. tegra_sor_writel(sor, value, sor->soc->regs->pll1);
  780. /* use alternate scrambler reset for eDP */
  781. value = tegra_sor_readl(sor, SOR_DP_SPARE0);
  782. if (link->edp == 0)
  783. value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
  784. else
  785. value |= SOR_DP_SPARE_PANEL_INTERNAL;
  786. tegra_sor_writel(sor, value, SOR_DP_SPARE0);
  787. err = tegra_sor_power_down_lanes(sor);
  788. if (err < 0) {
  789. dev_err(sor->dev, "failed to power down lanes: %d\n", err);
  790. return err;
  791. }
  792. /* power up and pre-charge lanes */
  793. err = tegra_sor_power_up_lanes(sor, lanes);
  794. if (err < 0) {
  795. dev_err(sor->dev, "failed to power up %u lane%s: %d\n",
  796. lanes, (lanes != 1) ? "s" : "", err);
  797. return err;
  798. }
  799. tegra_sor_dp_precharge(sor, lanes);
  800. return 0;
  801. }
  802. static const struct drm_dp_link_ops tegra_sor_dp_link_ops = {
  803. .apply_training = tegra_sor_dp_link_apply_training,
  804. .configure = tegra_sor_dp_link_configure,
  805. };
  806. static void tegra_sor_super_update(struct tegra_sor *sor)
  807. {
  808. tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
  809. tegra_sor_writel(sor, 1, SOR_SUPER_STATE0);
  810. tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
  811. }
  812. static void tegra_sor_update(struct tegra_sor *sor)
  813. {
  814. tegra_sor_writel(sor, 0, SOR_STATE0);
  815. tegra_sor_writel(sor, 1, SOR_STATE0);
  816. tegra_sor_writel(sor, 0, SOR_STATE0);
  817. }
  818. static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
  819. {
  820. u32 value;
  821. value = tegra_sor_readl(sor, SOR_PWM_DIV);
  822. value &= ~SOR_PWM_DIV_MASK;
  823. value |= 0x400; /* period */
  824. tegra_sor_writel(sor, value, SOR_PWM_DIV);
  825. value = tegra_sor_readl(sor, SOR_PWM_CTL);
  826. value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
  827. value |= 0x400; /* duty cycle */
  828. value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
  829. value |= SOR_PWM_CTL_TRIGGER;
  830. tegra_sor_writel(sor, value, SOR_PWM_CTL);
  831. timeout = jiffies + msecs_to_jiffies(timeout);
  832. while (time_before(jiffies, timeout)) {
  833. value = tegra_sor_readl(sor, SOR_PWM_CTL);
  834. if ((value & SOR_PWM_CTL_TRIGGER) == 0)
  835. return 0;
  836. usleep_range(25, 100);
  837. }
  838. return -ETIMEDOUT;
  839. }
  840. static int tegra_sor_attach(struct tegra_sor *sor)
  841. {
  842. unsigned long value, timeout;
  843. /* wake up in normal mode */
  844. value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
  845. value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
  846. value |= SOR_SUPER_STATE_MODE_NORMAL;
  847. tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
  848. tegra_sor_super_update(sor);
  849. /* attach */
  850. value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
  851. value |= SOR_SUPER_STATE_ATTACHED;
  852. tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
  853. tegra_sor_super_update(sor);
  854. timeout = jiffies + msecs_to_jiffies(250);
  855. while (time_before(jiffies, timeout)) {
  856. value = tegra_sor_readl(sor, SOR_TEST);
  857. if ((value & SOR_TEST_ATTACHED) != 0)
  858. return 0;
  859. usleep_range(25, 100);
  860. }
  861. return -ETIMEDOUT;
  862. }
  863. static int tegra_sor_wakeup(struct tegra_sor *sor)
  864. {
  865. unsigned long value, timeout;
  866. timeout = jiffies + msecs_to_jiffies(250);
  867. /* wait for head to wake up */
  868. while (time_before(jiffies, timeout)) {
  869. value = tegra_sor_readl(sor, SOR_TEST);
  870. value &= SOR_TEST_HEAD_MODE_MASK;
  871. if (value == SOR_TEST_HEAD_MODE_AWAKE)
  872. return 0;
  873. usleep_range(25, 100);
  874. }
  875. return -ETIMEDOUT;
  876. }
  877. static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
  878. {
  879. u32 value;
  880. value = tegra_sor_readl(sor, SOR_PWR);
  881. value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
  882. tegra_sor_writel(sor, value, SOR_PWR);
  883. timeout = jiffies + msecs_to_jiffies(timeout);
  884. while (time_before(jiffies, timeout)) {
  885. value = tegra_sor_readl(sor, SOR_PWR);
  886. if ((value & SOR_PWR_TRIGGER) == 0)
  887. return 0;
  888. usleep_range(25, 100);
  889. }
  890. return -ETIMEDOUT;
  891. }
  892. struct tegra_sor_params {
  893. /* number of link clocks per line */
  894. unsigned int num_clocks;
  895. /* ratio between input and output */
  896. u64 ratio;
  897. /* precision factor */
  898. u64 precision;
  899. unsigned int active_polarity;
  900. unsigned int active_count;
  901. unsigned int active_frac;
  902. unsigned int tu_size;
  903. unsigned int error;
  904. };
  905. static int tegra_sor_compute_params(struct tegra_sor *sor,
  906. struct tegra_sor_params *params,
  907. unsigned int tu_size)
  908. {
  909. u64 active_sym, active_count, frac, approx;
  910. u32 active_polarity, active_frac = 0;
  911. const u64 f = params->precision;
  912. s64 error;
  913. active_sym = params->ratio * tu_size;
  914. active_count = div_u64(active_sym, f) * f;
  915. frac = active_sym - active_count;
  916. /* fraction < 0.5 */
  917. if (frac >= (f / 2)) {
  918. active_polarity = 1;
  919. frac = f - frac;
  920. } else {
  921. active_polarity = 0;
  922. }
  923. if (frac != 0) {
  924. frac = div_u64(f * f, frac); /* 1/fraction */
  925. if (frac <= (15 * f)) {
  926. active_frac = div_u64(frac, f);
  927. /* round up */
  928. if (active_polarity)
  929. active_frac++;
  930. } else {
  931. active_frac = active_polarity ? 1 : 15;
  932. }
  933. }
  934. if (active_frac == 1)
  935. active_polarity = 0;
  936. if (active_polarity == 1) {
  937. if (active_frac) {
  938. approx = active_count + (active_frac * (f - 1)) * f;
  939. approx = div_u64(approx, active_frac * f);
  940. } else {
  941. approx = active_count + f;
  942. }
  943. } else {
  944. if (active_frac)
  945. approx = active_count + div_u64(f, active_frac);
  946. else
  947. approx = active_count;
  948. }
  949. error = div_s64(active_sym - approx, tu_size);
  950. error *= params->num_clocks;
  951. if (error <= 0 && abs(error) < params->error) {
  952. params->active_count = div_u64(active_count, f);
  953. params->active_polarity = active_polarity;
  954. params->active_frac = active_frac;
  955. params->error = abs(error);
  956. params->tu_size = tu_size;
  957. if (error == 0)
  958. return true;
  959. }
  960. return false;
  961. }
  962. static int tegra_sor_compute_config(struct tegra_sor *sor,
  963. const struct drm_display_mode *mode,
  964. struct tegra_sor_config *config,
  965. struct drm_dp_link *link)
  966. {
  967. const u64 f = 100000, link_rate = link->rate * 1000;
  968. const u64 pclk = (u64)mode->clock * 1000;
  969. u64 input, output, watermark, num;
  970. struct tegra_sor_params params;
  971. u32 num_syms_per_line;
  972. unsigned int i;
  973. if (!link_rate || !link->lanes || !pclk || !config->bits_per_pixel)
  974. return -EINVAL;
  975. input = pclk * config->bits_per_pixel;
  976. output = link_rate * 8 * link->lanes;
  977. if (input >= output)
  978. return -ERANGE;
  979. memset(&params, 0, sizeof(params));
  980. params.ratio = div64_u64(input * f, output);
  981. params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
  982. params.precision = f;
  983. params.error = 64 * f;
  984. params.tu_size = 64;
  985. for (i = params.tu_size; i >= 32; i--)
  986. if (tegra_sor_compute_params(sor, &params, i))
  987. break;
  988. if (params.active_frac == 0) {
  989. config->active_polarity = 0;
  990. config->active_count = params.active_count;
  991. if (!params.active_polarity)
  992. config->active_count--;
  993. config->tu_size = params.tu_size;
  994. config->active_frac = 1;
  995. } else {
  996. config->active_polarity = params.active_polarity;
  997. config->active_count = params.active_count;
  998. config->active_frac = params.active_frac;
  999. config->tu_size = params.tu_size;
  1000. }
  1001. dev_dbg(sor->dev,
  1002. "polarity: %d active count: %d tu size: %d active frac: %d\n",
  1003. config->active_polarity, config->active_count,
  1004. config->tu_size, config->active_frac);
  1005. watermark = params.ratio * config->tu_size * (f - params.ratio);
  1006. watermark = div_u64(watermark, f);
  1007. watermark = div_u64(watermark + params.error, f);
  1008. config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
  1009. num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
  1010. (link->lanes * 8);
  1011. if (config->watermark > 30) {
  1012. config->watermark = 30;
  1013. dev_err(sor->dev,
  1014. "unable to compute TU size, forcing watermark to %u\n",
  1015. config->watermark);
  1016. } else if (config->watermark > num_syms_per_line) {
  1017. config->watermark = num_syms_per_line;
  1018. dev_err(sor->dev, "watermark too high, forcing to %u\n",
  1019. config->watermark);
  1020. }
  1021. /* compute the number of symbols per horizontal blanking interval */
  1022. num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
  1023. config->hblank_symbols = div_u64(num, pclk);
  1024. if (link->caps.enhanced_framing)
  1025. config->hblank_symbols -= 3;
  1026. config->hblank_symbols -= 12 / link->lanes;
  1027. /* compute the number of symbols per vertical blanking interval */
  1028. num = (mode->hdisplay - 25) * link_rate;
  1029. config->vblank_symbols = div_u64(num, pclk);
  1030. config->vblank_symbols -= 36 / link->lanes + 4;
  1031. dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
  1032. config->vblank_symbols);
  1033. return 0;
  1034. }
  1035. static void tegra_sor_apply_config(struct tegra_sor *sor,
  1036. const struct tegra_sor_config *config)
  1037. {
  1038. u32 value;
  1039. value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
  1040. value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
  1041. value |= SOR_DP_LINKCTL_TU_SIZE(config->tu_size);
  1042. tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
  1043. value = tegra_sor_readl(sor, SOR_DP_CONFIG0);
  1044. value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
  1045. value |= SOR_DP_CONFIG_WATERMARK(config->watermark);
  1046. value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
  1047. value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config->active_count);
  1048. value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
  1049. value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config->active_frac);
  1050. if (config->active_polarity)
  1051. value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
  1052. else
  1053. value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
  1054. value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
  1055. value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
  1056. tegra_sor_writel(sor, value, SOR_DP_CONFIG0);
  1057. value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
  1058. value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
  1059. value |= config->hblank_symbols & 0xffff;
  1060. tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
  1061. value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
  1062. value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
  1063. value |= config->vblank_symbols & 0xffff;
  1064. tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
  1065. }
  1066. static void tegra_sor_mode_set(struct tegra_sor *sor,
  1067. const struct drm_display_mode *mode,
  1068. struct tegra_sor_state *state)
  1069. {
  1070. struct tegra_dc *dc = to_tegra_dc(sor->output.encoder.crtc);
  1071. unsigned int vbe, vse, hbe, hse, vbs, hbs;
  1072. u32 value;
  1073. value = tegra_sor_readl(sor, SOR_STATE1);
  1074. value &= ~SOR_STATE_ASY_PIXELDEPTH_MASK;
  1075. value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
  1076. value &= ~SOR_STATE_ASY_OWNER_MASK;
  1077. value |= SOR_STATE_ASY_CRC_MODE_COMPLETE |
  1078. SOR_STATE_ASY_OWNER(dc->pipe + 1);
  1079. if (mode->flags & DRM_MODE_FLAG_PHSYNC)
  1080. value &= ~SOR_STATE_ASY_HSYNCPOL;
  1081. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  1082. value |= SOR_STATE_ASY_HSYNCPOL;
  1083. if (mode->flags & DRM_MODE_FLAG_PVSYNC)
  1084. value &= ~SOR_STATE_ASY_VSYNCPOL;
  1085. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  1086. value |= SOR_STATE_ASY_VSYNCPOL;
  1087. switch (state->bpc) {
  1088. case 16:
  1089. value |= SOR_STATE_ASY_PIXELDEPTH_BPP_48_444;
  1090. break;
  1091. case 12:
  1092. value |= SOR_STATE_ASY_PIXELDEPTH_BPP_36_444;
  1093. break;
  1094. case 10:
  1095. value |= SOR_STATE_ASY_PIXELDEPTH_BPP_30_444;
  1096. break;
  1097. case 8:
  1098. value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
  1099. break;
  1100. case 6:
  1101. value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
  1102. break;
  1103. default:
  1104. value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
  1105. break;
  1106. }
  1107. tegra_sor_writel(sor, value, SOR_STATE1);
  1108. /*
  1109. * TODO: The video timing programming below doesn't seem to match the
  1110. * register definitions.
  1111. */
  1112. value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
  1113. tegra_sor_writel(sor, value, sor->soc->regs->head_state1 + dc->pipe);
  1114. /* sync end = sync width - 1 */
  1115. vse = mode->vsync_end - mode->vsync_start - 1;
  1116. hse = mode->hsync_end - mode->hsync_start - 1;
  1117. value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
  1118. tegra_sor_writel(sor, value, sor->soc->regs->head_state2 + dc->pipe);
  1119. /* blank end = sync end + back porch */
  1120. vbe = vse + (mode->vtotal - mode->vsync_end);
  1121. hbe = hse + (mode->htotal - mode->hsync_end);
  1122. value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
  1123. tegra_sor_writel(sor, value, sor->soc->regs->head_state3 + dc->pipe);
  1124. /* blank start = blank end + active */
  1125. vbs = vbe + mode->vdisplay;
  1126. hbs = hbe + mode->hdisplay;
  1127. value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
  1128. tegra_sor_writel(sor, value, sor->soc->regs->head_state4 + dc->pipe);
  1129. /* XXX interlacing support */
  1130. tegra_sor_writel(sor, 0x001, sor->soc->regs->head_state5 + dc->pipe);
  1131. }
  1132. static int tegra_sor_detach(struct tegra_sor *sor)
  1133. {
  1134. unsigned long value, timeout;
  1135. /* switch to safe mode */
  1136. value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
  1137. value &= ~SOR_SUPER_STATE_MODE_NORMAL;
  1138. tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
  1139. tegra_sor_super_update(sor);
  1140. timeout = jiffies + msecs_to_jiffies(250);
  1141. while (time_before(jiffies, timeout)) {
  1142. value = tegra_sor_readl(sor, SOR_PWR);
  1143. if (value & SOR_PWR_MODE_SAFE)
  1144. break;
  1145. }
  1146. if ((value & SOR_PWR_MODE_SAFE) == 0)
  1147. return -ETIMEDOUT;
  1148. /* go to sleep */
  1149. value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
  1150. value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
  1151. tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
  1152. tegra_sor_super_update(sor);
  1153. /* detach */
  1154. value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
  1155. value &= ~SOR_SUPER_STATE_ATTACHED;
  1156. tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
  1157. tegra_sor_super_update(sor);
  1158. timeout = jiffies + msecs_to_jiffies(250);
  1159. while (time_before(jiffies, timeout)) {
  1160. value = tegra_sor_readl(sor, SOR_TEST);
  1161. if ((value & SOR_TEST_ATTACHED) == 0)
  1162. break;
  1163. usleep_range(25, 100);
  1164. }
  1165. if ((value & SOR_TEST_ATTACHED) != 0)
  1166. return -ETIMEDOUT;
  1167. return 0;
  1168. }
  1169. static int tegra_sor_power_down(struct tegra_sor *sor)
  1170. {
  1171. unsigned long value, timeout;
  1172. int err;
  1173. value = tegra_sor_readl(sor, SOR_PWR);
  1174. value &= ~SOR_PWR_NORMAL_STATE_PU;
  1175. value |= SOR_PWR_TRIGGER;
  1176. tegra_sor_writel(sor, value, SOR_PWR);
  1177. timeout = jiffies + msecs_to_jiffies(250);
  1178. while (time_before(jiffies, timeout)) {
  1179. value = tegra_sor_readl(sor, SOR_PWR);
  1180. if ((value & SOR_PWR_TRIGGER) == 0)
  1181. return 0;
  1182. usleep_range(25, 100);
  1183. }
  1184. if ((value & SOR_PWR_TRIGGER) != 0)
  1185. return -ETIMEDOUT;
  1186. /* switch to safe parent clock */
  1187. err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
  1188. if (err < 0) {
  1189. dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
  1190. return err;
  1191. }
  1192. value = tegra_sor_readl(sor, sor->soc->regs->pll2);
  1193. value |= SOR_PLL2_PORT_POWERDOWN;
  1194. tegra_sor_writel(sor, value, sor->soc->regs->pll2);
  1195. usleep_range(20, 100);
  1196. value = tegra_sor_readl(sor, sor->soc->regs->pll0);
  1197. value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
  1198. tegra_sor_writel(sor, value, sor->soc->regs->pll0);
  1199. value = tegra_sor_readl(sor, sor->soc->regs->pll2);
  1200. value |= SOR_PLL2_SEQ_PLLCAPPD;
  1201. value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
  1202. tegra_sor_writel(sor, value, sor->soc->regs->pll2);
  1203. usleep_range(20, 100);
  1204. return 0;
  1205. }
  1206. static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
  1207. {
  1208. u32 value;
  1209. timeout = jiffies + msecs_to_jiffies(timeout);
  1210. while (time_before(jiffies, timeout)) {
  1211. value = tegra_sor_readl(sor, SOR_CRCA);
  1212. if (value & SOR_CRCA_VALID)
  1213. return 0;
  1214. usleep_range(100, 200);
  1215. }
  1216. return -ETIMEDOUT;
  1217. }
  1218. static int tegra_sor_show_crc(struct seq_file *s, void *data)
  1219. {
  1220. struct drm_info_node *node = s->private;
  1221. struct tegra_sor *sor = node->info_ent->data;
  1222. struct drm_crtc *crtc = sor->output.encoder.crtc;
  1223. struct drm_device *drm = node->minor->dev;
  1224. int err = 0;
  1225. u32 value;
  1226. drm_modeset_lock_all(drm);
  1227. if (!crtc || !crtc->state->active) {
  1228. err = -EBUSY;
  1229. goto unlock;
  1230. }
  1231. value = tegra_sor_readl(sor, SOR_STATE1);
  1232. value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
  1233. tegra_sor_writel(sor, value, SOR_STATE1);
  1234. value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
  1235. value |= SOR_CRC_CNTRL_ENABLE;
  1236. tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
  1237. value = tegra_sor_readl(sor, SOR_TEST);
  1238. value &= ~SOR_TEST_CRC_POST_SERIALIZE;
  1239. tegra_sor_writel(sor, value, SOR_TEST);
  1240. err = tegra_sor_crc_wait(sor, 100);
  1241. if (err < 0)
  1242. goto unlock;
  1243. tegra_sor_writel(sor, SOR_CRCA_RESET, SOR_CRCA);
  1244. value = tegra_sor_readl(sor, SOR_CRCB);
  1245. seq_printf(s, "%08x\n", value);
  1246. unlock:
  1247. drm_modeset_unlock_all(drm);
  1248. return err;
  1249. }
  1250. #define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
  1251. static const struct debugfs_reg32 tegra_sor_regs[] = {
  1252. DEBUGFS_REG32(SOR_CTXSW),
  1253. DEBUGFS_REG32(SOR_SUPER_STATE0),
  1254. DEBUGFS_REG32(SOR_SUPER_STATE1),
  1255. DEBUGFS_REG32(SOR_STATE0),
  1256. DEBUGFS_REG32(SOR_STATE1),
  1257. DEBUGFS_REG32(SOR_HEAD_STATE0(0)),
  1258. DEBUGFS_REG32(SOR_HEAD_STATE0(1)),
  1259. DEBUGFS_REG32(SOR_HEAD_STATE1(0)),
  1260. DEBUGFS_REG32(SOR_HEAD_STATE1(1)),
  1261. DEBUGFS_REG32(SOR_HEAD_STATE2(0)),
  1262. DEBUGFS_REG32(SOR_HEAD_STATE2(1)),
  1263. DEBUGFS_REG32(SOR_HEAD_STATE3(0)),
  1264. DEBUGFS_REG32(SOR_HEAD_STATE3(1)),
  1265. DEBUGFS_REG32(SOR_HEAD_STATE4(0)),
  1266. DEBUGFS_REG32(SOR_HEAD_STATE4(1)),
  1267. DEBUGFS_REG32(SOR_HEAD_STATE5(0)),
  1268. DEBUGFS_REG32(SOR_HEAD_STATE5(1)),
  1269. DEBUGFS_REG32(SOR_CRC_CNTRL),
  1270. DEBUGFS_REG32(SOR_DP_DEBUG_MVID),
  1271. DEBUGFS_REG32(SOR_CLK_CNTRL),
  1272. DEBUGFS_REG32(SOR_CAP),
  1273. DEBUGFS_REG32(SOR_PWR),
  1274. DEBUGFS_REG32(SOR_TEST),
  1275. DEBUGFS_REG32(SOR_PLL0),
  1276. DEBUGFS_REG32(SOR_PLL1),
  1277. DEBUGFS_REG32(SOR_PLL2),
  1278. DEBUGFS_REG32(SOR_PLL3),
  1279. DEBUGFS_REG32(SOR_CSTM),
  1280. DEBUGFS_REG32(SOR_LVDS),
  1281. DEBUGFS_REG32(SOR_CRCA),
  1282. DEBUGFS_REG32(SOR_CRCB),
  1283. DEBUGFS_REG32(SOR_BLANK),
  1284. DEBUGFS_REG32(SOR_SEQ_CTL),
  1285. DEBUGFS_REG32(SOR_LANE_SEQ_CTL),
  1286. DEBUGFS_REG32(SOR_SEQ_INST(0)),
  1287. DEBUGFS_REG32(SOR_SEQ_INST(1)),
  1288. DEBUGFS_REG32(SOR_SEQ_INST(2)),
  1289. DEBUGFS_REG32(SOR_SEQ_INST(3)),
  1290. DEBUGFS_REG32(SOR_SEQ_INST(4)),
  1291. DEBUGFS_REG32(SOR_SEQ_INST(5)),
  1292. DEBUGFS_REG32(SOR_SEQ_INST(6)),
  1293. DEBUGFS_REG32(SOR_SEQ_INST(7)),
  1294. DEBUGFS_REG32(SOR_SEQ_INST(8)),
  1295. DEBUGFS_REG32(SOR_SEQ_INST(9)),
  1296. DEBUGFS_REG32(SOR_SEQ_INST(10)),
  1297. DEBUGFS_REG32(SOR_SEQ_INST(11)),
  1298. DEBUGFS_REG32(SOR_SEQ_INST(12)),
  1299. DEBUGFS_REG32(SOR_SEQ_INST(13)),
  1300. DEBUGFS_REG32(SOR_SEQ_INST(14)),
  1301. DEBUGFS_REG32(SOR_SEQ_INST(15)),
  1302. DEBUGFS_REG32(SOR_PWM_DIV),
  1303. DEBUGFS_REG32(SOR_PWM_CTL),
  1304. DEBUGFS_REG32(SOR_VCRC_A0),
  1305. DEBUGFS_REG32(SOR_VCRC_A1),
  1306. DEBUGFS_REG32(SOR_VCRC_B0),
  1307. DEBUGFS_REG32(SOR_VCRC_B1),
  1308. DEBUGFS_REG32(SOR_CCRC_A0),
  1309. DEBUGFS_REG32(SOR_CCRC_A1),
  1310. DEBUGFS_REG32(SOR_CCRC_B0),
  1311. DEBUGFS_REG32(SOR_CCRC_B1),
  1312. DEBUGFS_REG32(SOR_EDATA_A0),
  1313. DEBUGFS_REG32(SOR_EDATA_A1),
  1314. DEBUGFS_REG32(SOR_EDATA_B0),
  1315. DEBUGFS_REG32(SOR_EDATA_B1),
  1316. DEBUGFS_REG32(SOR_COUNT_A0),
  1317. DEBUGFS_REG32(SOR_COUNT_A1),
  1318. DEBUGFS_REG32(SOR_COUNT_B0),
  1319. DEBUGFS_REG32(SOR_COUNT_B1),
  1320. DEBUGFS_REG32(SOR_DEBUG_A0),
  1321. DEBUGFS_REG32(SOR_DEBUG_A1),
  1322. DEBUGFS_REG32(SOR_DEBUG_B0),
  1323. DEBUGFS_REG32(SOR_DEBUG_B1),
  1324. DEBUGFS_REG32(SOR_TRIG),
  1325. DEBUGFS_REG32(SOR_MSCHECK),
  1326. DEBUGFS_REG32(SOR_XBAR_CTRL),
  1327. DEBUGFS_REG32(SOR_XBAR_POL),
  1328. DEBUGFS_REG32(SOR_DP_LINKCTL0),
  1329. DEBUGFS_REG32(SOR_DP_LINKCTL1),
  1330. DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT0),
  1331. DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT1),
  1332. DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT0),
  1333. DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT1),
  1334. DEBUGFS_REG32(SOR_LANE_PREEMPHASIS0),
  1335. DEBUGFS_REG32(SOR_LANE_PREEMPHASIS1),
  1336. DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS0),
  1337. DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS1),
  1338. DEBUGFS_REG32(SOR_LANE_POSTCURSOR0),
  1339. DEBUGFS_REG32(SOR_LANE_POSTCURSOR1),
  1340. DEBUGFS_REG32(SOR_DP_CONFIG0),
  1341. DEBUGFS_REG32(SOR_DP_CONFIG1),
  1342. DEBUGFS_REG32(SOR_DP_MN0),
  1343. DEBUGFS_REG32(SOR_DP_MN1),
  1344. DEBUGFS_REG32(SOR_DP_PADCTL0),
  1345. DEBUGFS_REG32(SOR_DP_PADCTL1),
  1346. DEBUGFS_REG32(SOR_DP_PADCTL2),
  1347. DEBUGFS_REG32(SOR_DP_DEBUG0),
  1348. DEBUGFS_REG32(SOR_DP_DEBUG1),
  1349. DEBUGFS_REG32(SOR_DP_SPARE0),
  1350. DEBUGFS_REG32(SOR_DP_SPARE1),
  1351. DEBUGFS_REG32(SOR_DP_AUDIO_CTRL),
  1352. DEBUGFS_REG32(SOR_DP_AUDIO_HBLANK_SYMBOLS),
  1353. DEBUGFS_REG32(SOR_DP_AUDIO_VBLANK_SYMBOLS),
  1354. DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_HEADER),
  1355. DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK0),
  1356. DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK1),
  1357. DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK2),
  1358. DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK3),
  1359. DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK4),
  1360. DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK5),
  1361. DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK6),
  1362. DEBUGFS_REG32(SOR_DP_TPG),
  1363. DEBUGFS_REG32(SOR_DP_TPG_CONFIG),
  1364. DEBUGFS_REG32(SOR_DP_LQ_CSTM0),
  1365. DEBUGFS_REG32(SOR_DP_LQ_CSTM1),
  1366. DEBUGFS_REG32(SOR_DP_LQ_CSTM2),
  1367. };
  1368. static int tegra_sor_show_regs(struct seq_file *s, void *data)
  1369. {
  1370. struct drm_info_node *node = s->private;
  1371. struct tegra_sor *sor = node->info_ent->data;
  1372. struct drm_crtc *crtc = sor->output.encoder.crtc;
  1373. struct drm_device *drm = node->minor->dev;
  1374. unsigned int i;
  1375. int err = 0;
  1376. drm_modeset_lock_all(drm);
  1377. if (!crtc || !crtc->state->active) {
  1378. err = -EBUSY;
  1379. goto unlock;
  1380. }
  1381. for (i = 0; i < ARRAY_SIZE(tegra_sor_regs); i++) {
  1382. unsigned int offset = tegra_sor_regs[i].offset;
  1383. seq_printf(s, "%-38s %#05x %08x\n", tegra_sor_regs[i].name,
  1384. offset, tegra_sor_readl(sor, offset));
  1385. }
  1386. unlock:
  1387. drm_modeset_unlock_all(drm);
  1388. return err;
  1389. }
  1390. static const struct drm_info_list debugfs_files[] = {
  1391. { "crc", tegra_sor_show_crc, 0, NULL },
  1392. { "regs", tegra_sor_show_regs, 0, NULL },
  1393. };
  1394. static int tegra_sor_late_register(struct drm_connector *connector)
  1395. {
  1396. struct tegra_output *output = connector_to_output(connector);
  1397. unsigned int i, count = ARRAY_SIZE(debugfs_files);
  1398. struct drm_minor *minor = connector->dev->primary;
  1399. struct dentry *root = connector->debugfs_entry;
  1400. struct tegra_sor *sor = to_sor(output);
  1401. sor->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
  1402. GFP_KERNEL);
  1403. if (!sor->debugfs_files)
  1404. return -ENOMEM;
  1405. for (i = 0; i < count; i++)
  1406. sor->debugfs_files[i].data = sor;
  1407. drm_debugfs_create_files(sor->debugfs_files, count, root, minor);
  1408. return 0;
  1409. }
  1410. static void tegra_sor_early_unregister(struct drm_connector *connector)
  1411. {
  1412. struct tegra_output *output = connector_to_output(connector);
  1413. unsigned int count = ARRAY_SIZE(debugfs_files);
  1414. struct tegra_sor *sor = to_sor(output);
  1415. drm_debugfs_remove_files(sor->debugfs_files, count,
  1416. connector->debugfs_entry,
  1417. connector->dev->primary);
  1418. kfree(sor->debugfs_files);
  1419. sor->debugfs_files = NULL;
  1420. }
  1421. static void tegra_sor_connector_reset(struct drm_connector *connector)
  1422. {
  1423. struct tegra_sor_state *state;
  1424. state = kzalloc(sizeof(*state), GFP_KERNEL);
  1425. if (!state)
  1426. return;
  1427. if (connector->state) {
  1428. __drm_atomic_helper_connector_destroy_state(connector->state);
  1429. kfree(connector->state);
  1430. }
  1431. __drm_atomic_helper_connector_reset(connector, &state->base);
  1432. }
  1433. static enum drm_connector_status
  1434. tegra_sor_connector_detect(struct drm_connector *connector, bool force)
  1435. {
  1436. struct tegra_output *output = connector_to_output(connector);
  1437. struct tegra_sor *sor = to_sor(output);
  1438. if (sor->aux)
  1439. return drm_dp_aux_detect(sor->aux);
  1440. return tegra_output_connector_detect(connector, force);
  1441. }
  1442. static struct drm_connector_state *
  1443. tegra_sor_connector_duplicate_state(struct drm_connector *connector)
  1444. {
  1445. struct tegra_sor_state *state = to_sor_state(connector->state);
  1446. struct tegra_sor_state *copy;
  1447. copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
  1448. if (!copy)
  1449. return NULL;
  1450. __drm_atomic_helper_connector_duplicate_state(connector, &copy->base);
  1451. return &copy->base;
  1452. }
  1453. static const struct drm_connector_funcs tegra_sor_connector_funcs = {
  1454. .reset = tegra_sor_connector_reset,
  1455. .detect = tegra_sor_connector_detect,
  1456. .fill_modes = drm_helper_probe_single_connector_modes,
  1457. .destroy = tegra_output_connector_destroy,
  1458. .atomic_duplicate_state = tegra_sor_connector_duplicate_state,
  1459. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  1460. .late_register = tegra_sor_late_register,
  1461. .early_unregister = tegra_sor_early_unregister,
  1462. };
  1463. static int tegra_sor_connector_get_modes(struct drm_connector *connector)
  1464. {
  1465. struct tegra_output *output = connector_to_output(connector);
  1466. struct tegra_sor *sor = to_sor(output);
  1467. int err;
  1468. if (sor->aux)
  1469. drm_dp_aux_enable(sor->aux);
  1470. err = tegra_output_connector_get_modes(connector);
  1471. if (sor->aux)
  1472. drm_dp_aux_disable(sor->aux);
  1473. return err;
  1474. }
  1475. static enum drm_mode_status
  1476. tegra_sor_connector_mode_valid(struct drm_connector *connector,
  1477. struct drm_display_mode *mode)
  1478. {
  1479. return MODE_OK;
  1480. }
  1481. static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
  1482. .get_modes = tegra_sor_connector_get_modes,
  1483. .mode_valid = tegra_sor_connector_mode_valid,
  1484. };
  1485. static int
  1486. tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
  1487. struct drm_crtc_state *crtc_state,
  1488. struct drm_connector_state *conn_state)
  1489. {
  1490. struct tegra_output *output = encoder_to_output(encoder);
  1491. struct tegra_sor_state *state = to_sor_state(conn_state);
  1492. struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
  1493. unsigned long pclk = crtc_state->mode.clock * 1000;
  1494. struct tegra_sor *sor = to_sor(output);
  1495. struct drm_display_info *info;
  1496. int err;
  1497. info = &output->connector.display_info;
  1498. /*
  1499. * For HBR2 modes, the SOR brick needs to use the x20 multiplier, so
  1500. * the pixel clock must be corrected accordingly.
  1501. */
  1502. if (pclk >= 340000000) {
  1503. state->link_speed = 20;
  1504. state->pclk = pclk / 2;
  1505. } else {
  1506. state->link_speed = 10;
  1507. state->pclk = pclk;
  1508. }
  1509. err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
  1510. pclk, 0);
  1511. if (err < 0) {
  1512. dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
  1513. return err;
  1514. }
  1515. switch (info->bpc) {
  1516. case 8:
  1517. case 6:
  1518. state->bpc = info->bpc;
  1519. break;
  1520. default:
  1521. DRM_DEBUG_KMS("%u bits-per-color not supported\n", info->bpc);
  1522. state->bpc = 8;
  1523. break;
  1524. }
  1525. return 0;
  1526. }
  1527. static inline u32 tegra_sor_hdmi_subpack(const u8 *ptr, size_t size)
  1528. {
  1529. u32 value = 0;
  1530. size_t i;
  1531. for (i = size; i > 0; i--)
  1532. value = (value << 8) | ptr[i - 1];
  1533. return value;
  1534. }
  1535. static void tegra_sor_hdmi_write_infopack(struct tegra_sor *sor,
  1536. const void *data, size_t size)
  1537. {
  1538. const u8 *ptr = data;
  1539. unsigned long offset;
  1540. size_t i, j;
  1541. u32 value;
  1542. switch (ptr[0]) {
  1543. case HDMI_INFOFRAME_TYPE_AVI:
  1544. offset = SOR_HDMI_AVI_INFOFRAME_HEADER;
  1545. break;
  1546. case HDMI_INFOFRAME_TYPE_AUDIO:
  1547. offset = SOR_HDMI_AUDIO_INFOFRAME_HEADER;
  1548. break;
  1549. case HDMI_INFOFRAME_TYPE_VENDOR:
  1550. offset = SOR_HDMI_VSI_INFOFRAME_HEADER;
  1551. break;
  1552. default:
  1553. dev_err(sor->dev, "unsupported infoframe type: %02x\n",
  1554. ptr[0]);
  1555. return;
  1556. }
  1557. value = INFOFRAME_HEADER_TYPE(ptr[0]) |
  1558. INFOFRAME_HEADER_VERSION(ptr[1]) |
  1559. INFOFRAME_HEADER_LEN(ptr[2]);
  1560. tegra_sor_writel(sor, value, offset);
  1561. offset++;
  1562. /*
  1563. * Each subpack contains 7 bytes, divided into:
  1564. * - subpack_low: bytes 0 - 3
  1565. * - subpack_high: bytes 4 - 6 (with byte 7 padded to 0x00)
  1566. */
  1567. for (i = 3, j = 0; i < size; i += 7, j += 8) {
  1568. size_t rem = size - i, num = min_t(size_t, rem, 4);
  1569. value = tegra_sor_hdmi_subpack(&ptr[i], num);
  1570. tegra_sor_writel(sor, value, offset++);
  1571. num = min_t(size_t, rem - num, 3);
  1572. value = tegra_sor_hdmi_subpack(&ptr[i + 4], num);
  1573. tegra_sor_writel(sor, value, offset++);
  1574. }
  1575. }
  1576. static int
  1577. tegra_sor_hdmi_setup_avi_infoframe(struct tegra_sor *sor,
  1578. const struct drm_display_mode *mode)
  1579. {
  1580. u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
  1581. struct hdmi_avi_infoframe frame;
  1582. u32 value;
  1583. int err;
  1584. /* disable AVI infoframe */
  1585. value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
  1586. value &= ~INFOFRAME_CTRL_SINGLE;
  1587. value &= ~INFOFRAME_CTRL_OTHER;
  1588. value &= ~INFOFRAME_CTRL_ENABLE;
  1589. tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
  1590. err = drm_hdmi_avi_infoframe_from_display_mode(&frame,
  1591. &sor->output.connector, mode);
  1592. if (err < 0) {
  1593. dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
  1594. return err;
  1595. }
  1596. err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
  1597. if (err < 0) {
  1598. dev_err(sor->dev, "failed to pack AVI infoframe: %d\n", err);
  1599. return err;
  1600. }
  1601. tegra_sor_hdmi_write_infopack(sor, buffer, err);
  1602. /* enable AVI infoframe */
  1603. value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
  1604. value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
  1605. value |= INFOFRAME_CTRL_ENABLE;
  1606. tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
  1607. return 0;
  1608. }
  1609. static void tegra_sor_write_eld(struct tegra_sor *sor)
  1610. {
  1611. size_t length = drm_eld_size(sor->output.connector.eld), i;
  1612. for (i = 0; i < length; i++)
  1613. tegra_sor_writel(sor, i << 8 | sor->output.connector.eld[i],
  1614. SOR_AUDIO_HDA_ELD_BUFWR);
  1615. /*
  1616. * The HDA codec will always report an ELD buffer size of 96 bytes and
  1617. * the HDA codec driver will check that each byte read from the buffer
  1618. * is valid. Therefore every byte must be written, even if no 96 bytes
  1619. * were parsed from EDID.
  1620. */
  1621. for (i = length; i < 96; i++)
  1622. tegra_sor_writel(sor, i << 8 | 0, SOR_AUDIO_HDA_ELD_BUFWR);
  1623. }
  1624. static void tegra_sor_audio_prepare(struct tegra_sor *sor)
  1625. {
  1626. u32 value;
  1627. /*
  1628. * Enable and unmask the HDA codec SCRATCH0 register interrupt. This
  1629. * is used for interoperability between the HDA codec driver and the
  1630. * HDMI/DP driver.
  1631. */
  1632. value = SOR_INT_CODEC_SCRATCH1 | SOR_INT_CODEC_SCRATCH0;
  1633. tegra_sor_writel(sor, value, SOR_INT_ENABLE);
  1634. tegra_sor_writel(sor, value, SOR_INT_MASK);
  1635. tegra_sor_write_eld(sor);
  1636. value = SOR_AUDIO_HDA_PRESENSE_ELDV | SOR_AUDIO_HDA_PRESENSE_PD;
  1637. tegra_sor_writel(sor, value, SOR_AUDIO_HDA_PRESENSE);
  1638. }
  1639. static void tegra_sor_audio_unprepare(struct tegra_sor *sor)
  1640. {
  1641. tegra_sor_writel(sor, 0, SOR_AUDIO_HDA_PRESENSE);
  1642. tegra_sor_writel(sor, 0, SOR_INT_MASK);
  1643. tegra_sor_writel(sor, 0, SOR_INT_ENABLE);
  1644. }
  1645. static void tegra_sor_audio_enable(struct tegra_sor *sor)
  1646. {
  1647. u32 value;
  1648. value = tegra_sor_readl(sor, SOR_AUDIO_CNTRL);
  1649. /* select HDA audio input */
  1650. value &= ~SOR_AUDIO_CNTRL_SOURCE_SELECT(SOURCE_SELECT_MASK);
  1651. value |= SOR_AUDIO_CNTRL_SOURCE_SELECT(SOURCE_SELECT_HDA);
  1652. /* inject null samples */
  1653. if (sor->format.channels != 2)
  1654. value &= ~SOR_AUDIO_CNTRL_INJECT_NULLSMPL;
  1655. else
  1656. value |= SOR_AUDIO_CNTRL_INJECT_NULLSMPL;
  1657. value |= SOR_AUDIO_CNTRL_AFIFO_FLUSH;
  1658. tegra_sor_writel(sor, value, SOR_AUDIO_CNTRL);
  1659. /* enable advertising HBR capability */
  1660. tegra_sor_writel(sor, SOR_AUDIO_SPARE_HBR_ENABLE, SOR_AUDIO_SPARE);
  1661. }
  1662. static int tegra_sor_hdmi_enable_audio_infoframe(struct tegra_sor *sor)
  1663. {
  1664. u8 buffer[HDMI_INFOFRAME_SIZE(AUDIO)];
  1665. struct hdmi_audio_infoframe frame;
  1666. u32 value;
  1667. int err;
  1668. err = hdmi_audio_infoframe_init(&frame);
  1669. if (err < 0) {
  1670. dev_err(sor->dev, "failed to setup audio infoframe: %d\n", err);
  1671. return err;
  1672. }
  1673. frame.channels = sor->format.channels;
  1674. err = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
  1675. if (err < 0) {
  1676. dev_err(sor->dev, "failed to pack audio infoframe: %d\n", err);
  1677. return err;
  1678. }
  1679. tegra_sor_hdmi_write_infopack(sor, buffer, err);
  1680. value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
  1681. value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
  1682. value |= INFOFRAME_CTRL_ENABLE;
  1683. tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
  1684. return 0;
  1685. }
  1686. static void tegra_sor_hdmi_audio_enable(struct tegra_sor *sor)
  1687. {
  1688. u32 value;
  1689. tegra_sor_audio_enable(sor);
  1690. tegra_sor_writel(sor, 0, SOR_HDMI_ACR_CTRL);
  1691. value = SOR_HDMI_SPARE_ACR_PRIORITY_HIGH |
  1692. SOR_HDMI_SPARE_CTS_RESET(1) |
  1693. SOR_HDMI_SPARE_HW_CTS_ENABLE;
  1694. tegra_sor_writel(sor, value, SOR_HDMI_SPARE);
  1695. /* enable HW CTS */
  1696. value = SOR_HDMI_ACR_SUBPACK_LOW_SB1(0);
  1697. tegra_sor_writel(sor, value, SOR_HDMI_ACR_0441_SUBPACK_LOW);
  1698. /* allow packet to be sent */
  1699. value = SOR_HDMI_ACR_SUBPACK_HIGH_ENABLE;
  1700. tegra_sor_writel(sor, value, SOR_HDMI_ACR_0441_SUBPACK_HIGH);
  1701. /* reset N counter and enable lookup */
  1702. value = SOR_HDMI_AUDIO_N_RESET | SOR_HDMI_AUDIO_N_LOOKUP;
  1703. tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_N);
  1704. value = (24000 * 4096) / (128 * sor->format.sample_rate / 1000);
  1705. tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_0320);
  1706. tegra_sor_writel(sor, 4096, SOR_AUDIO_NVAL_0320);
  1707. tegra_sor_writel(sor, 20000, SOR_AUDIO_AVAL_0441);
  1708. tegra_sor_writel(sor, 4704, SOR_AUDIO_NVAL_0441);
  1709. tegra_sor_writel(sor, 20000, SOR_AUDIO_AVAL_0882);
  1710. tegra_sor_writel(sor, 9408, SOR_AUDIO_NVAL_0882);
  1711. tegra_sor_writel(sor, 20000, SOR_AUDIO_AVAL_1764);
  1712. tegra_sor_writel(sor, 18816, SOR_AUDIO_NVAL_1764);
  1713. value = (24000 * 6144) / (128 * sor->format.sample_rate / 1000);
  1714. tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_0480);
  1715. tegra_sor_writel(sor, 6144, SOR_AUDIO_NVAL_0480);
  1716. value = (24000 * 12288) / (128 * sor->format.sample_rate / 1000);
  1717. tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_0960);
  1718. tegra_sor_writel(sor, 12288, SOR_AUDIO_NVAL_0960);
  1719. value = (24000 * 24576) / (128 * sor->format.sample_rate / 1000);
  1720. tegra_sor_writel(sor, value, SOR_AUDIO_AVAL_1920);
  1721. tegra_sor_writel(sor, 24576, SOR_AUDIO_NVAL_1920);
  1722. value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_N);
  1723. value &= ~SOR_HDMI_AUDIO_N_RESET;
  1724. tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_N);
  1725. tegra_sor_hdmi_enable_audio_infoframe(sor);
  1726. }
  1727. static void tegra_sor_hdmi_disable_audio_infoframe(struct tegra_sor *sor)
  1728. {
  1729. u32 value;
  1730. value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
  1731. value &= ~INFOFRAME_CTRL_ENABLE;
  1732. tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
  1733. }
  1734. static void tegra_sor_hdmi_audio_disable(struct tegra_sor *sor)
  1735. {
  1736. tegra_sor_hdmi_disable_audio_infoframe(sor);
  1737. }
  1738. static struct tegra_sor_hdmi_settings *
  1739. tegra_sor_hdmi_find_settings(struct tegra_sor *sor, unsigned long frequency)
  1740. {
  1741. unsigned int i;
  1742. for (i = 0; i < sor->num_settings; i++)
  1743. if (frequency <= sor->settings[i].frequency)
  1744. return &sor->settings[i];
  1745. return NULL;
  1746. }
  1747. static void tegra_sor_hdmi_disable_scrambling(struct tegra_sor *sor)
  1748. {
  1749. u32 value;
  1750. value = tegra_sor_readl(sor, SOR_HDMI2_CTRL);
  1751. value &= ~SOR_HDMI2_CTRL_CLOCK_MODE_DIV_BY_4;
  1752. value &= ~SOR_HDMI2_CTRL_SCRAMBLE;
  1753. tegra_sor_writel(sor, value, SOR_HDMI2_CTRL);
  1754. }
  1755. static void tegra_sor_hdmi_scdc_disable(struct tegra_sor *sor)
  1756. {
  1757. drm_scdc_set_high_tmds_clock_ratio(&sor->output.connector, false);
  1758. drm_scdc_set_scrambling(&sor->output.connector, false);
  1759. tegra_sor_hdmi_disable_scrambling(sor);
  1760. }
  1761. static void tegra_sor_hdmi_scdc_stop(struct tegra_sor *sor)
  1762. {
  1763. if (sor->scdc_enabled) {
  1764. cancel_delayed_work_sync(&sor->scdc);
  1765. tegra_sor_hdmi_scdc_disable(sor);
  1766. }
  1767. }
  1768. static void tegra_sor_hdmi_enable_scrambling(struct tegra_sor *sor)
  1769. {
  1770. u32 value;
  1771. value = tegra_sor_readl(sor, SOR_HDMI2_CTRL);
  1772. value |= SOR_HDMI2_CTRL_CLOCK_MODE_DIV_BY_4;
  1773. value |= SOR_HDMI2_CTRL_SCRAMBLE;
  1774. tegra_sor_writel(sor, value, SOR_HDMI2_CTRL);
  1775. }
  1776. static void tegra_sor_hdmi_scdc_enable(struct tegra_sor *sor)
  1777. {
  1778. drm_scdc_set_high_tmds_clock_ratio(&sor->output.connector, true);
  1779. drm_scdc_set_scrambling(&sor->output.connector, true);
  1780. tegra_sor_hdmi_enable_scrambling(sor);
  1781. }
  1782. static void tegra_sor_hdmi_scdc_work(struct work_struct *work)
  1783. {
  1784. struct tegra_sor *sor = container_of(work, struct tegra_sor, scdc.work);
  1785. if (!drm_scdc_get_scrambling_status(&sor->output.connector)) {
  1786. DRM_DEBUG_KMS("SCDC not scrambled\n");
  1787. tegra_sor_hdmi_scdc_enable(sor);
  1788. }
  1789. schedule_delayed_work(&sor->scdc, msecs_to_jiffies(5000));
  1790. }
  1791. static void tegra_sor_hdmi_scdc_start(struct tegra_sor *sor)
  1792. {
  1793. struct drm_scdc *scdc = &sor->output.connector.display_info.hdmi.scdc;
  1794. struct drm_display_mode *mode;
  1795. mode = &sor->output.encoder.crtc->state->adjusted_mode;
  1796. if (mode->clock >= 340000 && scdc->supported) {
  1797. schedule_delayed_work(&sor->scdc, msecs_to_jiffies(5000));
  1798. tegra_sor_hdmi_scdc_enable(sor);
  1799. sor->scdc_enabled = true;
  1800. }
  1801. }
  1802. static void tegra_sor_hdmi_disable(struct drm_encoder *encoder)
  1803. {
  1804. struct tegra_output *output = encoder_to_output(encoder);
  1805. struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
  1806. struct tegra_sor *sor = to_sor(output);
  1807. u32 value;
  1808. int err;
  1809. tegra_sor_audio_unprepare(sor);
  1810. tegra_sor_hdmi_scdc_stop(sor);
  1811. err = tegra_sor_detach(sor);
  1812. if (err < 0)
  1813. dev_err(sor->dev, "failed to detach SOR: %d\n", err);
  1814. tegra_sor_writel(sor, 0, SOR_STATE1);
  1815. tegra_sor_update(sor);
  1816. /* disable display to SOR clock */
  1817. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  1818. if (!sor->soc->has_nvdisplay)
  1819. value &= ~SOR1_TIMING_CYA;
  1820. value &= ~SOR_ENABLE(sor->index);
  1821. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  1822. tegra_dc_commit(dc);
  1823. err = tegra_sor_power_down(sor);
  1824. if (err < 0)
  1825. dev_err(sor->dev, "failed to power down SOR: %d\n", err);
  1826. err = tegra_io_pad_power_disable(sor->pad);
  1827. if (err < 0)
  1828. dev_err(sor->dev, "failed to power off I/O pad: %d\n", err);
  1829. host1x_client_suspend(&sor->client);
  1830. }
  1831. static void tegra_sor_hdmi_enable(struct drm_encoder *encoder)
  1832. {
  1833. struct tegra_output *output = encoder_to_output(encoder);
  1834. unsigned int h_ref_to_sync = 1, pulse_start, max_ac;
  1835. struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
  1836. struct tegra_sor_hdmi_settings *settings;
  1837. struct tegra_sor *sor = to_sor(output);
  1838. struct tegra_sor_state *state;
  1839. struct drm_display_mode *mode;
  1840. unsigned long rate, pclk;
  1841. unsigned int div, i;
  1842. u32 value;
  1843. int err;
  1844. state = to_sor_state(output->connector.state);
  1845. mode = &encoder->crtc->state->adjusted_mode;
  1846. pclk = mode->clock * 1000;
  1847. err = host1x_client_resume(&sor->client);
  1848. if (err < 0) {
  1849. dev_err(sor->dev, "failed to resume: %d\n", err);
  1850. return;
  1851. }
  1852. /* switch to safe parent clock */
  1853. err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
  1854. if (err < 0) {
  1855. dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
  1856. return;
  1857. }
  1858. div = clk_get_rate(sor->clk) / 1000000 * 4;
  1859. err = tegra_io_pad_power_enable(sor->pad);
  1860. if (err < 0)
  1861. dev_err(sor->dev, "failed to power on I/O pad: %d\n", err);
  1862. usleep_range(20, 100);
  1863. value = tegra_sor_readl(sor, sor->soc->regs->pll2);
  1864. value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
  1865. tegra_sor_writel(sor, value, sor->soc->regs->pll2);
  1866. usleep_range(20, 100);
  1867. value = tegra_sor_readl(sor, sor->soc->regs->pll3);
  1868. value &= ~SOR_PLL3_PLL_VDD_MODE_3V3;
  1869. tegra_sor_writel(sor, value, sor->soc->regs->pll3);
  1870. value = tegra_sor_readl(sor, sor->soc->regs->pll0);
  1871. value &= ~SOR_PLL0_VCOPD;
  1872. value &= ~SOR_PLL0_PWR;
  1873. tegra_sor_writel(sor, value, sor->soc->regs->pll0);
  1874. value = tegra_sor_readl(sor, sor->soc->regs->pll2);
  1875. value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
  1876. tegra_sor_writel(sor, value, sor->soc->regs->pll2);
  1877. usleep_range(200, 400);
  1878. value = tegra_sor_readl(sor, sor->soc->regs->pll2);
  1879. value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
  1880. value &= ~SOR_PLL2_PORT_POWERDOWN;
  1881. tegra_sor_writel(sor, value, sor->soc->regs->pll2);
  1882. usleep_range(20, 100);
  1883. value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
  1884. value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
  1885. SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2;
  1886. tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
  1887. while (true) {
  1888. value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
  1889. if ((value & SOR_LANE_SEQ_CTL_STATE_BUSY) == 0)
  1890. break;
  1891. usleep_range(250, 1000);
  1892. }
  1893. value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
  1894. SOR_LANE_SEQ_CTL_POWER_STATE_UP | SOR_LANE_SEQ_CTL_DELAY(5);
  1895. tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
  1896. while (true) {
  1897. value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
  1898. if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
  1899. break;
  1900. usleep_range(250, 1000);
  1901. }
  1902. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  1903. value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
  1904. value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
  1905. if (mode->clock < 340000) {
  1906. DRM_DEBUG_KMS("setting 2.7 GHz link speed\n");
  1907. value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G2_70;
  1908. } else {
  1909. DRM_DEBUG_KMS("setting 5.4 GHz link speed\n");
  1910. value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G5_40;
  1911. }
  1912. value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
  1913. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  1914. /* SOR pad PLL stabilization time */
  1915. usleep_range(250, 1000);
  1916. value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
  1917. value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
  1918. value |= SOR_DP_LINKCTL_LANE_COUNT(4);
  1919. tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
  1920. value = tegra_sor_readl(sor, SOR_DP_SPARE0);
  1921. value &= ~SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
  1922. value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
  1923. value &= ~SOR_DP_SPARE_SEQ_ENABLE;
  1924. value &= ~SOR_DP_SPARE_MACRO_SOR_CLK;
  1925. tegra_sor_writel(sor, value, SOR_DP_SPARE0);
  1926. value = SOR_SEQ_CTL_PU_PC(0) | SOR_SEQ_CTL_PU_PC_ALT(0) |
  1927. SOR_SEQ_CTL_PD_PC(8) | SOR_SEQ_CTL_PD_PC_ALT(8);
  1928. tegra_sor_writel(sor, value, SOR_SEQ_CTL);
  1929. value = SOR_SEQ_INST_DRIVE_PWM_OUT_LO | SOR_SEQ_INST_HALT |
  1930. SOR_SEQ_INST_WAIT_VSYNC | SOR_SEQ_INST_WAIT(1);
  1931. tegra_sor_writel(sor, value, SOR_SEQ_INST(0));
  1932. tegra_sor_writel(sor, value, SOR_SEQ_INST(8));
  1933. if (!sor->soc->has_nvdisplay) {
  1934. /* program the reference clock */
  1935. value = SOR_REFCLK_DIV_INT(div) | SOR_REFCLK_DIV_FRAC(div);
  1936. tegra_sor_writel(sor, value, SOR_REFCLK);
  1937. }
  1938. /* XXX not in TRM */
  1939. for (value = 0, i = 0; i < 5; i++)
  1940. value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->xbar_cfg[i]) |
  1941. SOR_XBAR_CTRL_LINK1_XSEL(i, i);
  1942. tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
  1943. tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
  1944. /*
  1945. * Switch the pad clock to the DP clock. Note that we cannot actually
  1946. * do this because Tegra186 and later don't support clk_set_parent()
  1947. * on the sorX_pad_clkout clocks. We already do the equivalent above
  1948. * using the DP_CLK_SEL mux of the SOR_CLK_CNTRL register.
  1949. */
  1950. #if 0
  1951. err = clk_set_parent(sor->clk_pad, sor->clk_dp);
  1952. if (err < 0) {
  1953. dev_err(sor->dev, "failed to select pad parent clock: %d\n",
  1954. err);
  1955. return;
  1956. }
  1957. #endif
  1958. /* switch the SOR clock to the pad clock */
  1959. err = tegra_sor_set_parent_clock(sor, sor->clk_pad);
  1960. if (err < 0) {
  1961. dev_err(sor->dev, "failed to select SOR parent clock: %d\n",
  1962. err);
  1963. return;
  1964. }
  1965. /* switch the output clock to the parent pixel clock */
  1966. err = clk_set_parent(sor->clk, sor->clk_parent);
  1967. if (err < 0) {
  1968. dev_err(sor->dev, "failed to select output parent clock: %d\n",
  1969. err);
  1970. return;
  1971. }
  1972. /* adjust clock rate for HDMI 2.0 modes */
  1973. rate = clk_get_rate(sor->clk_parent);
  1974. if (mode->clock >= 340000)
  1975. rate /= 2;
  1976. DRM_DEBUG_KMS("setting clock to %lu Hz, mode: %lu Hz\n", rate, pclk);
  1977. clk_set_rate(sor->clk, rate);
  1978. if (!sor->soc->has_nvdisplay) {
  1979. value = SOR_INPUT_CONTROL_HDMI_SRC_SELECT(dc->pipe);
  1980. /* XXX is this the proper check? */
  1981. if (mode->clock < 75000)
  1982. value |= SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_LIMITED;
  1983. tegra_sor_writel(sor, value, SOR_INPUT_CONTROL);
  1984. }
  1985. max_ac = ((mode->htotal - mode->hdisplay) - SOR_REKEY - 18) / 32;
  1986. value = SOR_HDMI_CTRL_ENABLE | SOR_HDMI_CTRL_MAX_AC_PACKET(max_ac) |
  1987. SOR_HDMI_CTRL_AUDIO_LAYOUT | SOR_HDMI_CTRL_REKEY(SOR_REKEY);
  1988. tegra_sor_writel(sor, value, SOR_HDMI_CTRL);
  1989. if (!dc->soc->has_nvdisplay) {
  1990. /* H_PULSE2 setup */
  1991. pulse_start = h_ref_to_sync +
  1992. (mode->hsync_end - mode->hsync_start) +
  1993. (mode->htotal - mode->hsync_end) - 10;
  1994. value = PULSE_LAST_END_A | PULSE_QUAL_VACTIVE |
  1995. PULSE_POLARITY_HIGH | PULSE_MODE_NORMAL;
  1996. tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_CONTROL);
  1997. value = PULSE_END(pulse_start + 8) | PULSE_START(pulse_start);
  1998. tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_POSITION_A);
  1999. value = tegra_dc_readl(dc, DC_DISP_DISP_SIGNAL_OPTIONS0);
  2000. value |= H_PULSE2_ENABLE;
  2001. tegra_dc_writel(dc, value, DC_DISP_DISP_SIGNAL_OPTIONS0);
  2002. }
  2003. /* infoframe setup */
  2004. err = tegra_sor_hdmi_setup_avi_infoframe(sor, mode);
  2005. if (err < 0)
  2006. dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
  2007. /* XXX HDMI audio support not implemented yet */
  2008. tegra_sor_hdmi_disable_audio_infoframe(sor);
  2009. /* use single TMDS protocol */
  2010. value = tegra_sor_readl(sor, SOR_STATE1);
  2011. value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
  2012. value |= SOR_STATE_ASY_PROTOCOL_SINGLE_TMDS_A;
  2013. tegra_sor_writel(sor, value, SOR_STATE1);
  2014. /* power up pad calibration */
  2015. value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
  2016. value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
  2017. tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
  2018. /* production settings */
  2019. settings = tegra_sor_hdmi_find_settings(sor, mode->clock * 1000);
  2020. if (!settings) {
  2021. dev_err(sor->dev, "no settings for pixel clock %d Hz\n",
  2022. mode->clock * 1000);
  2023. return;
  2024. }
  2025. value = tegra_sor_readl(sor, sor->soc->regs->pll0);
  2026. value &= ~SOR_PLL0_ICHPMP_MASK;
  2027. value &= ~SOR_PLL0_FILTER_MASK;
  2028. value &= ~SOR_PLL0_VCOCAP_MASK;
  2029. value |= SOR_PLL0_ICHPMP(settings->ichpmp);
  2030. value |= SOR_PLL0_FILTER(settings->filter);
  2031. value |= SOR_PLL0_VCOCAP(settings->vcocap);
  2032. tegra_sor_writel(sor, value, sor->soc->regs->pll0);
  2033. /* XXX not in TRM */
  2034. value = tegra_sor_readl(sor, sor->soc->regs->pll1);
  2035. value &= ~SOR_PLL1_LOADADJ_MASK;
  2036. value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
  2037. value |= SOR_PLL1_LOADADJ(settings->loadadj);
  2038. value |= SOR_PLL1_TMDS_TERMADJ(settings->tmds_termadj);
  2039. value |= SOR_PLL1_TMDS_TERM;
  2040. tegra_sor_writel(sor, value, sor->soc->regs->pll1);
  2041. value = tegra_sor_readl(sor, sor->soc->regs->pll3);
  2042. value &= ~SOR_PLL3_BG_TEMP_COEF_MASK;
  2043. value &= ~SOR_PLL3_BG_VREF_LEVEL_MASK;
  2044. value &= ~SOR_PLL3_AVDD10_LEVEL_MASK;
  2045. value &= ~SOR_PLL3_AVDD14_LEVEL_MASK;
  2046. value |= SOR_PLL3_BG_TEMP_COEF(settings->bg_temp_coef);
  2047. value |= SOR_PLL3_BG_VREF_LEVEL(settings->bg_vref_level);
  2048. value |= SOR_PLL3_AVDD10_LEVEL(settings->avdd10_level);
  2049. value |= SOR_PLL3_AVDD14_LEVEL(settings->avdd14_level);
  2050. tegra_sor_writel(sor, value, sor->soc->regs->pll3);
  2051. value = settings->drive_current[3] << 24 |
  2052. settings->drive_current[2] << 16 |
  2053. settings->drive_current[1] << 8 |
  2054. settings->drive_current[0] << 0;
  2055. tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
  2056. value = settings->preemphasis[3] << 24 |
  2057. settings->preemphasis[2] << 16 |
  2058. settings->preemphasis[1] << 8 |
  2059. settings->preemphasis[0] << 0;
  2060. tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
  2061. value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
  2062. value &= ~SOR_DP_PADCTL_TX_PU_MASK;
  2063. value |= SOR_DP_PADCTL_TX_PU_ENABLE;
  2064. value |= SOR_DP_PADCTL_TX_PU(settings->tx_pu_value);
  2065. tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
  2066. value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl2);
  2067. value &= ~SOR_DP_PADCTL_SPAREPLL_MASK;
  2068. value |= SOR_DP_PADCTL_SPAREPLL(settings->sparepll);
  2069. tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl2);
  2070. /* power down pad calibration */
  2071. value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
  2072. value |= SOR_DP_PADCTL_PAD_CAL_PD;
  2073. tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
  2074. if (!dc->soc->has_nvdisplay) {
  2075. /* miscellaneous display controller settings */
  2076. value = VSYNC_H_POSITION(1);
  2077. tegra_dc_writel(dc, value, DC_DISP_DISP_TIMING_OPTIONS);
  2078. }
  2079. value = tegra_dc_readl(dc, DC_DISP_DISP_COLOR_CONTROL);
  2080. value &= ~DITHER_CONTROL_MASK;
  2081. value &= ~BASE_COLOR_SIZE_MASK;
  2082. switch (state->bpc) {
  2083. case 6:
  2084. value |= BASE_COLOR_SIZE_666;
  2085. break;
  2086. case 8:
  2087. value |= BASE_COLOR_SIZE_888;
  2088. break;
  2089. case 10:
  2090. value |= BASE_COLOR_SIZE_101010;
  2091. break;
  2092. case 12:
  2093. value |= BASE_COLOR_SIZE_121212;
  2094. break;
  2095. default:
  2096. WARN(1, "%u bits-per-color not supported\n", state->bpc);
  2097. value |= BASE_COLOR_SIZE_888;
  2098. break;
  2099. }
  2100. tegra_dc_writel(dc, value, DC_DISP_DISP_COLOR_CONTROL);
  2101. /* XXX set display head owner */
  2102. value = tegra_sor_readl(sor, SOR_STATE1);
  2103. value &= ~SOR_STATE_ASY_OWNER_MASK;
  2104. value |= SOR_STATE_ASY_OWNER(1 + dc->pipe);
  2105. tegra_sor_writel(sor, value, SOR_STATE1);
  2106. err = tegra_sor_power_up(sor, 250);
  2107. if (err < 0)
  2108. dev_err(sor->dev, "failed to power up SOR: %d\n", err);
  2109. /* configure dynamic range of output */
  2110. value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
  2111. value &= ~SOR_HEAD_STATE_RANGECOMPRESS_MASK;
  2112. value &= ~SOR_HEAD_STATE_DYNRANGE_MASK;
  2113. tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
  2114. /* configure colorspace */
  2115. value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
  2116. value &= ~SOR_HEAD_STATE_COLORSPACE_MASK;
  2117. value |= SOR_HEAD_STATE_COLORSPACE_RGB;
  2118. tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
  2119. tegra_sor_mode_set(sor, mode, state);
  2120. tegra_sor_update(sor);
  2121. /* program preamble timing in SOR (XXX) */
  2122. value = tegra_sor_readl(sor, SOR_DP_SPARE0);
  2123. value &= ~SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
  2124. tegra_sor_writel(sor, value, SOR_DP_SPARE0);
  2125. err = tegra_sor_attach(sor);
  2126. if (err < 0)
  2127. dev_err(sor->dev, "failed to attach SOR: %d\n", err);
  2128. /* enable display to SOR clock and generate HDMI preamble */
  2129. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  2130. if (!sor->soc->has_nvdisplay)
  2131. value |= SOR1_TIMING_CYA;
  2132. value |= SOR_ENABLE(sor->index);
  2133. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  2134. if (dc->soc->has_nvdisplay) {
  2135. value = tegra_dc_readl(dc, DC_DISP_CORE_SOR_SET_CONTROL(sor->index));
  2136. value &= ~PROTOCOL_MASK;
  2137. value |= PROTOCOL_SINGLE_TMDS_A;
  2138. tegra_dc_writel(dc, value, DC_DISP_CORE_SOR_SET_CONTROL(sor->index));
  2139. }
  2140. tegra_dc_commit(dc);
  2141. err = tegra_sor_wakeup(sor);
  2142. if (err < 0)
  2143. dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
  2144. tegra_sor_hdmi_scdc_start(sor);
  2145. tegra_sor_audio_prepare(sor);
  2146. }
  2147. static const struct drm_encoder_helper_funcs tegra_sor_hdmi_helpers = {
  2148. .disable = tegra_sor_hdmi_disable,
  2149. .enable = tegra_sor_hdmi_enable,
  2150. .atomic_check = tegra_sor_encoder_atomic_check,
  2151. };
  2152. static void tegra_sor_dp_disable(struct drm_encoder *encoder)
  2153. {
  2154. struct tegra_output *output = encoder_to_output(encoder);
  2155. struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
  2156. struct tegra_sor *sor = to_sor(output);
  2157. u32 value;
  2158. int err;
  2159. if (output->panel)
  2160. drm_panel_disable(output->panel);
  2161. /*
  2162. * Do not attempt to power down a DP link if we're not connected since
  2163. * the AUX transactions would just be timing out.
  2164. */
  2165. if (output->connector.status != connector_status_disconnected) {
  2166. err = drm_dp_link_power_down(sor->aux, &sor->link);
  2167. if (err < 0)
  2168. dev_err(sor->dev, "failed to power down link: %d\n",
  2169. err);
  2170. }
  2171. err = tegra_sor_detach(sor);
  2172. if (err < 0)
  2173. dev_err(sor->dev, "failed to detach SOR: %d\n", err);
  2174. tegra_sor_writel(sor, 0, SOR_STATE1);
  2175. tegra_sor_update(sor);
  2176. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  2177. value &= ~SOR_ENABLE(sor->index);
  2178. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  2179. tegra_dc_commit(dc);
  2180. value = tegra_sor_readl(sor, SOR_STATE1);
  2181. value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
  2182. value &= ~SOR_STATE_ASY_SUBOWNER_MASK;
  2183. value &= ~SOR_STATE_ASY_OWNER_MASK;
  2184. tegra_sor_writel(sor, value, SOR_STATE1);
  2185. tegra_sor_update(sor);
  2186. /* switch to safe parent clock */
  2187. err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
  2188. if (err < 0)
  2189. dev_err(sor->dev, "failed to set safe clock: %d\n", err);
  2190. err = tegra_sor_power_down(sor);
  2191. if (err < 0)
  2192. dev_err(sor->dev, "failed to power down SOR: %d\n", err);
  2193. err = tegra_io_pad_power_disable(sor->pad);
  2194. if (err < 0)
  2195. dev_err(sor->dev, "failed to power off I/O pad: %d\n", err);
  2196. err = drm_dp_aux_disable(sor->aux);
  2197. if (err < 0)
  2198. dev_err(sor->dev, "failed disable DPAUX: %d\n", err);
  2199. if (output->panel)
  2200. drm_panel_unprepare(output->panel);
  2201. host1x_client_suspend(&sor->client);
  2202. }
  2203. static void tegra_sor_dp_enable(struct drm_encoder *encoder)
  2204. {
  2205. struct tegra_output *output = encoder_to_output(encoder);
  2206. struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
  2207. struct tegra_sor *sor = to_sor(output);
  2208. struct tegra_sor_config config;
  2209. struct tegra_sor_state *state;
  2210. struct drm_display_mode *mode;
  2211. struct drm_display_info *info;
  2212. unsigned int i;
  2213. u32 value;
  2214. int err;
  2215. state = to_sor_state(output->connector.state);
  2216. mode = &encoder->crtc->state->adjusted_mode;
  2217. info = &output->connector.display_info;
  2218. err = host1x_client_resume(&sor->client);
  2219. if (err < 0) {
  2220. dev_err(sor->dev, "failed to resume: %d\n", err);
  2221. return;
  2222. }
  2223. /* switch to safe parent clock */
  2224. err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
  2225. if (err < 0)
  2226. dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
  2227. err = tegra_io_pad_power_enable(sor->pad);
  2228. if (err < 0)
  2229. dev_err(sor->dev, "failed to power on LVDS rail: %d\n", err);
  2230. usleep_range(20, 100);
  2231. err = drm_dp_aux_enable(sor->aux);
  2232. if (err < 0)
  2233. dev_err(sor->dev, "failed to enable DPAUX: %d\n", err);
  2234. err = drm_dp_link_probe(sor->aux, &sor->link);
  2235. if (err < 0)
  2236. dev_err(sor->dev, "failed to probe DP link: %d\n", err);
  2237. tegra_sor_filter_rates(sor);
  2238. err = drm_dp_link_choose(&sor->link, mode, info);
  2239. if (err < 0)
  2240. dev_err(sor->dev, "failed to choose link: %d\n", err);
  2241. if (output->panel)
  2242. drm_panel_prepare(output->panel);
  2243. value = tegra_sor_readl(sor, sor->soc->regs->pll2);
  2244. value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
  2245. tegra_sor_writel(sor, value, sor->soc->regs->pll2);
  2246. usleep_range(20, 40);
  2247. value = tegra_sor_readl(sor, sor->soc->regs->pll3);
  2248. value |= SOR_PLL3_PLL_VDD_MODE_3V3;
  2249. tegra_sor_writel(sor, value, sor->soc->regs->pll3);
  2250. value = tegra_sor_readl(sor, sor->soc->regs->pll0);
  2251. value &= ~(SOR_PLL0_VCOPD | SOR_PLL0_PWR);
  2252. tegra_sor_writel(sor, value, sor->soc->regs->pll0);
  2253. value = tegra_sor_readl(sor, sor->soc->regs->pll2);
  2254. value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
  2255. value |= SOR_PLL2_SEQ_PLLCAPPD;
  2256. tegra_sor_writel(sor, value, sor->soc->regs->pll2);
  2257. usleep_range(200, 400);
  2258. value = tegra_sor_readl(sor, sor->soc->regs->pll2);
  2259. value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
  2260. value &= ~SOR_PLL2_PORT_POWERDOWN;
  2261. tegra_sor_writel(sor, value, sor->soc->regs->pll2);
  2262. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  2263. value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
  2264. if (output->panel)
  2265. value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
  2266. else
  2267. value |= SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_DPCLK;
  2268. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  2269. usleep_range(200, 400);
  2270. value = tegra_sor_readl(sor, SOR_DP_SPARE0);
  2271. /* XXX not in TRM */
  2272. if (output->panel)
  2273. value |= SOR_DP_SPARE_PANEL_INTERNAL;
  2274. else
  2275. value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
  2276. value |= SOR_DP_SPARE_SEQ_ENABLE;
  2277. tegra_sor_writel(sor, value, SOR_DP_SPARE0);
  2278. /* XXX not in TRM */
  2279. tegra_sor_writel(sor, 0, SOR_LVDS);
  2280. value = tegra_sor_readl(sor, sor->soc->regs->pll0);
  2281. value &= ~SOR_PLL0_ICHPMP_MASK;
  2282. value &= ~SOR_PLL0_VCOCAP_MASK;
  2283. value |= SOR_PLL0_ICHPMP(0x1);
  2284. value |= SOR_PLL0_VCOCAP(0x3);
  2285. value |= SOR_PLL0_RESISTOR_EXT;
  2286. tegra_sor_writel(sor, value, sor->soc->regs->pll0);
  2287. /* XXX not in TRM */
  2288. for (value = 0, i = 0; i < 5; i++)
  2289. value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->soc->xbar_cfg[i]) |
  2290. SOR_XBAR_CTRL_LINK1_XSEL(i, i);
  2291. tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
  2292. tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
  2293. /*
  2294. * Switch the pad clock to the DP clock. Note that we cannot actually
  2295. * do this because Tegra186 and later don't support clk_set_parent()
  2296. * on the sorX_pad_clkout clocks. We already do the equivalent above
  2297. * using the DP_CLK_SEL mux of the SOR_CLK_CNTRL register.
  2298. */
  2299. #if 0
  2300. err = clk_set_parent(sor->clk_pad, sor->clk_parent);
  2301. if (err < 0) {
  2302. dev_err(sor->dev, "failed to select pad parent clock: %d\n",
  2303. err);
  2304. return;
  2305. }
  2306. #endif
  2307. /* switch the SOR clock to the pad clock */
  2308. err = tegra_sor_set_parent_clock(sor, sor->clk_pad);
  2309. if (err < 0) {
  2310. dev_err(sor->dev, "failed to select SOR parent clock: %d\n",
  2311. err);
  2312. return;
  2313. }
  2314. /* switch the output clock to the parent pixel clock */
  2315. err = clk_set_parent(sor->clk, sor->clk_parent);
  2316. if (err < 0) {
  2317. dev_err(sor->dev, "failed to select output parent clock: %d\n",
  2318. err);
  2319. return;
  2320. }
  2321. /* use DP-A protocol */
  2322. value = tegra_sor_readl(sor, SOR_STATE1);
  2323. value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
  2324. value |= SOR_STATE_ASY_PROTOCOL_DP_A;
  2325. tegra_sor_writel(sor, value, SOR_STATE1);
  2326. /* enable port */
  2327. value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
  2328. value |= SOR_DP_LINKCTL_ENABLE;
  2329. tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
  2330. tegra_sor_dp_term_calibrate(sor);
  2331. err = drm_dp_link_train(&sor->link);
  2332. if (err < 0)
  2333. dev_err(sor->dev, "link training failed: %d\n", err);
  2334. else
  2335. dev_dbg(sor->dev, "link training succeeded\n");
  2336. err = drm_dp_link_power_up(sor->aux, &sor->link);
  2337. if (err < 0)
  2338. dev_err(sor->dev, "failed to power up DP link: %d\n", err);
  2339. /* compute configuration */
  2340. memset(&config, 0, sizeof(config));
  2341. config.bits_per_pixel = state->bpc * 3;
  2342. err = tegra_sor_compute_config(sor, mode, &config, &sor->link);
  2343. if (err < 0)
  2344. dev_err(sor->dev, "failed to compute configuration: %d\n", err);
  2345. tegra_sor_apply_config(sor, &config);
  2346. tegra_sor_mode_set(sor, mode, state);
  2347. if (output->panel) {
  2348. /* CSTM (LVDS, link A/B, upper) */
  2349. value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
  2350. SOR_CSTM_UPPER;
  2351. tegra_sor_writel(sor, value, SOR_CSTM);
  2352. /* PWM setup */
  2353. err = tegra_sor_setup_pwm(sor, 250);
  2354. if (err < 0)
  2355. dev_err(sor->dev, "failed to setup PWM: %d\n", err);
  2356. }
  2357. tegra_sor_update(sor);
  2358. err = tegra_sor_power_up(sor, 250);
  2359. if (err < 0)
  2360. dev_err(sor->dev, "failed to power up SOR: %d\n", err);
  2361. /* attach and wake up */
  2362. err = tegra_sor_attach(sor);
  2363. if (err < 0)
  2364. dev_err(sor->dev, "failed to attach SOR: %d\n", err);
  2365. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  2366. value |= SOR_ENABLE(sor->index);
  2367. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  2368. tegra_dc_commit(dc);
  2369. err = tegra_sor_wakeup(sor);
  2370. if (err < 0)
  2371. dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
  2372. if (output->panel)
  2373. drm_panel_enable(output->panel);
  2374. }
  2375. static const struct drm_encoder_helper_funcs tegra_sor_dp_helpers = {
  2376. .disable = tegra_sor_dp_disable,
  2377. .enable = tegra_sor_dp_enable,
  2378. .atomic_check = tegra_sor_encoder_atomic_check,
  2379. };
  2380. static void tegra_sor_disable_regulator(void *data)
  2381. {
  2382. struct regulator *reg = data;
  2383. regulator_disable(reg);
  2384. }
  2385. static int tegra_sor_enable_regulator(struct tegra_sor *sor, struct regulator *reg)
  2386. {
  2387. int err;
  2388. err = regulator_enable(reg);
  2389. if (err)
  2390. return err;
  2391. return devm_add_action_or_reset(sor->dev, tegra_sor_disable_regulator, reg);
  2392. }
  2393. static int tegra_sor_hdmi_probe(struct tegra_sor *sor)
  2394. {
  2395. int err;
  2396. sor->avdd_io_supply = devm_regulator_get(sor->dev, "avdd-io-hdmi-dp");
  2397. if (IS_ERR(sor->avdd_io_supply))
  2398. return dev_err_probe(sor->dev, PTR_ERR(sor->avdd_io_supply),
  2399. "cannot get AVDD I/O supply\n");
  2400. err = tegra_sor_enable_regulator(sor, sor->avdd_io_supply);
  2401. if (err < 0) {
  2402. dev_err(sor->dev, "failed to enable AVDD I/O supply: %d\n",
  2403. err);
  2404. return err;
  2405. }
  2406. sor->vdd_pll_supply = devm_regulator_get(sor->dev, "vdd-hdmi-dp-pll");
  2407. if (IS_ERR(sor->vdd_pll_supply))
  2408. return dev_err_probe(sor->dev, PTR_ERR(sor->vdd_pll_supply),
  2409. "cannot get VDD PLL supply\n");
  2410. err = tegra_sor_enable_regulator(sor, sor->vdd_pll_supply);
  2411. if (err < 0) {
  2412. dev_err(sor->dev, "failed to enable VDD PLL supply: %d\n",
  2413. err);
  2414. return err;
  2415. }
  2416. sor->hdmi_supply = devm_regulator_get(sor->dev, "hdmi");
  2417. if (IS_ERR(sor->hdmi_supply))
  2418. return dev_err_probe(sor->dev, PTR_ERR(sor->hdmi_supply),
  2419. "cannot get HDMI supply\n");
  2420. err = tegra_sor_enable_regulator(sor, sor->hdmi_supply);
  2421. if (err < 0) {
  2422. dev_err(sor->dev, "failed to enable HDMI supply: %d\n", err);
  2423. return err;
  2424. }
  2425. INIT_DELAYED_WORK(&sor->scdc, tegra_sor_hdmi_scdc_work);
  2426. return 0;
  2427. }
  2428. static const struct tegra_sor_ops tegra_sor_hdmi_ops = {
  2429. .name = "HDMI",
  2430. .probe = tegra_sor_hdmi_probe,
  2431. .audio_enable = tegra_sor_hdmi_audio_enable,
  2432. .audio_disable = tegra_sor_hdmi_audio_disable,
  2433. };
  2434. static int tegra_sor_dp_probe(struct tegra_sor *sor)
  2435. {
  2436. int err;
  2437. sor->avdd_io_supply = devm_regulator_get(sor->dev, "avdd-io-hdmi-dp");
  2438. if (IS_ERR(sor->avdd_io_supply))
  2439. return PTR_ERR(sor->avdd_io_supply);
  2440. err = tegra_sor_enable_regulator(sor, sor->avdd_io_supply);
  2441. if (err < 0)
  2442. return err;
  2443. sor->vdd_pll_supply = devm_regulator_get(sor->dev, "vdd-hdmi-dp-pll");
  2444. if (IS_ERR(sor->vdd_pll_supply))
  2445. return PTR_ERR(sor->vdd_pll_supply);
  2446. err = tegra_sor_enable_regulator(sor, sor->vdd_pll_supply);
  2447. if (err < 0)
  2448. return err;
  2449. return 0;
  2450. }
  2451. static const struct tegra_sor_ops tegra_sor_dp_ops = {
  2452. .name = "DP",
  2453. .probe = tegra_sor_dp_probe,
  2454. };
  2455. static int tegra_sor_init(struct host1x_client *client)
  2456. {
  2457. struct drm_device *drm = dev_get_drvdata(client->host);
  2458. const struct drm_encoder_helper_funcs *helpers = NULL;
  2459. struct tegra_sor *sor = host1x_client_to_sor(client);
  2460. int connector = DRM_MODE_CONNECTOR_Unknown;
  2461. int encoder = DRM_MODE_ENCODER_NONE;
  2462. int err;
  2463. if (!sor->aux) {
  2464. if (sor->ops == &tegra_sor_hdmi_ops) {
  2465. connector = DRM_MODE_CONNECTOR_HDMIA;
  2466. encoder = DRM_MODE_ENCODER_TMDS;
  2467. helpers = &tegra_sor_hdmi_helpers;
  2468. } else if (sor->soc->supports_lvds) {
  2469. connector = DRM_MODE_CONNECTOR_LVDS;
  2470. encoder = DRM_MODE_ENCODER_LVDS;
  2471. }
  2472. } else {
  2473. if (sor->output.panel) {
  2474. connector = DRM_MODE_CONNECTOR_eDP;
  2475. encoder = DRM_MODE_ENCODER_TMDS;
  2476. helpers = &tegra_sor_dp_helpers;
  2477. } else {
  2478. connector = DRM_MODE_CONNECTOR_DisplayPort;
  2479. encoder = DRM_MODE_ENCODER_TMDS;
  2480. helpers = &tegra_sor_dp_helpers;
  2481. }
  2482. sor->link.ops = &tegra_sor_dp_link_ops;
  2483. sor->link.aux = sor->aux;
  2484. }
  2485. sor->output.dev = sor->dev;
  2486. drm_connector_init_with_ddc(drm, &sor->output.connector,
  2487. &tegra_sor_connector_funcs,
  2488. connector,
  2489. sor->output.ddc);
  2490. drm_connector_helper_add(&sor->output.connector,
  2491. &tegra_sor_connector_helper_funcs);
  2492. sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
  2493. drm_simple_encoder_init(drm, &sor->output.encoder, encoder);
  2494. drm_encoder_helper_add(&sor->output.encoder, helpers);
  2495. drm_connector_attach_encoder(&sor->output.connector,
  2496. &sor->output.encoder);
  2497. drm_connector_register(&sor->output.connector);
  2498. err = tegra_output_init(drm, &sor->output);
  2499. if (err < 0) {
  2500. dev_err(client->dev, "failed to initialize output: %d\n", err);
  2501. return err;
  2502. }
  2503. tegra_output_find_possible_crtcs(&sor->output, drm);
  2504. if (sor->aux) {
  2505. err = drm_dp_aux_attach(sor->aux, &sor->output);
  2506. if (err < 0) {
  2507. dev_err(sor->dev, "failed to attach DP: %d\n", err);
  2508. return err;
  2509. }
  2510. }
  2511. /*
  2512. * XXX: Remove this reset once proper hand-over from firmware to
  2513. * kernel is possible.
  2514. */
  2515. if (sor->rst) {
  2516. err = pm_runtime_resume_and_get(sor->dev);
  2517. if (err < 0) {
  2518. dev_err(sor->dev, "failed to get runtime PM: %d\n", err);
  2519. return err;
  2520. }
  2521. err = reset_control_acquire(sor->rst);
  2522. if (err < 0) {
  2523. dev_err(sor->dev, "failed to acquire SOR reset: %d\n",
  2524. err);
  2525. goto rpm_put;
  2526. }
  2527. err = reset_control_assert(sor->rst);
  2528. if (err < 0) {
  2529. dev_err(sor->dev, "failed to assert SOR reset: %d\n",
  2530. err);
  2531. goto rpm_put;
  2532. }
  2533. }
  2534. err = clk_prepare_enable(sor->clk);
  2535. if (err < 0) {
  2536. dev_err(sor->dev, "failed to enable clock: %d\n", err);
  2537. goto rpm_put;
  2538. }
  2539. usleep_range(1000, 3000);
  2540. if (sor->rst) {
  2541. err = reset_control_deassert(sor->rst);
  2542. if (err < 0) {
  2543. dev_err(sor->dev, "failed to deassert SOR reset: %d\n",
  2544. err);
  2545. clk_disable_unprepare(sor->clk);
  2546. goto rpm_put;
  2547. }
  2548. reset_control_release(sor->rst);
  2549. pm_runtime_put(sor->dev);
  2550. }
  2551. err = clk_prepare_enable(sor->clk_safe);
  2552. if (err < 0) {
  2553. clk_disable_unprepare(sor->clk);
  2554. return err;
  2555. }
  2556. err = clk_prepare_enable(sor->clk_dp);
  2557. if (err < 0) {
  2558. clk_disable_unprepare(sor->clk_safe);
  2559. clk_disable_unprepare(sor->clk);
  2560. return err;
  2561. }
  2562. return 0;
  2563. rpm_put:
  2564. if (sor->rst)
  2565. pm_runtime_put(sor->dev);
  2566. return err;
  2567. }
  2568. static int tegra_sor_exit(struct host1x_client *client)
  2569. {
  2570. struct tegra_sor *sor = host1x_client_to_sor(client);
  2571. int err;
  2572. tegra_output_exit(&sor->output);
  2573. if (sor->aux) {
  2574. err = drm_dp_aux_detach(sor->aux);
  2575. if (err < 0) {
  2576. dev_err(sor->dev, "failed to detach DP: %d\n", err);
  2577. return err;
  2578. }
  2579. }
  2580. clk_disable_unprepare(sor->clk_safe);
  2581. clk_disable_unprepare(sor->clk_dp);
  2582. clk_disable_unprepare(sor->clk);
  2583. return 0;
  2584. }
  2585. static int tegra_sor_runtime_suspend(struct host1x_client *client)
  2586. {
  2587. struct tegra_sor *sor = host1x_client_to_sor(client);
  2588. struct device *dev = client->dev;
  2589. int err;
  2590. if (sor->rst) {
  2591. err = reset_control_assert(sor->rst);
  2592. if (err < 0) {
  2593. dev_err(dev, "failed to assert reset: %d\n", err);
  2594. return err;
  2595. }
  2596. reset_control_release(sor->rst);
  2597. }
  2598. usleep_range(1000, 2000);
  2599. clk_disable_unprepare(sor->clk);
  2600. pm_runtime_put_sync(dev);
  2601. return 0;
  2602. }
  2603. static int tegra_sor_runtime_resume(struct host1x_client *client)
  2604. {
  2605. struct tegra_sor *sor = host1x_client_to_sor(client);
  2606. struct device *dev = client->dev;
  2607. int err;
  2608. err = pm_runtime_resume_and_get(dev);
  2609. if (err < 0) {
  2610. dev_err(dev, "failed to get runtime PM: %d\n", err);
  2611. return err;
  2612. }
  2613. err = clk_prepare_enable(sor->clk);
  2614. if (err < 0) {
  2615. dev_err(dev, "failed to enable clock: %d\n", err);
  2616. goto put_rpm;
  2617. }
  2618. usleep_range(1000, 2000);
  2619. if (sor->rst) {
  2620. err = reset_control_acquire(sor->rst);
  2621. if (err < 0) {
  2622. dev_err(dev, "failed to acquire reset: %d\n", err);
  2623. goto disable_clk;
  2624. }
  2625. err = reset_control_deassert(sor->rst);
  2626. if (err < 0) {
  2627. dev_err(dev, "failed to deassert reset: %d\n", err);
  2628. goto release_reset;
  2629. }
  2630. }
  2631. return 0;
  2632. release_reset:
  2633. reset_control_release(sor->rst);
  2634. disable_clk:
  2635. clk_disable_unprepare(sor->clk);
  2636. put_rpm:
  2637. pm_runtime_put_sync(dev);
  2638. return err;
  2639. }
  2640. static const struct host1x_client_ops sor_client_ops = {
  2641. .init = tegra_sor_init,
  2642. .exit = tegra_sor_exit,
  2643. .suspend = tegra_sor_runtime_suspend,
  2644. .resume = tegra_sor_runtime_resume,
  2645. };
  2646. static const u8 tegra124_sor_xbar_cfg[5] = {
  2647. 0, 1, 2, 3, 4
  2648. };
  2649. static const struct tegra_sor_regs tegra124_sor_regs = {
  2650. .head_state0 = 0x05,
  2651. .head_state1 = 0x07,
  2652. .head_state2 = 0x09,
  2653. .head_state3 = 0x0b,
  2654. .head_state4 = 0x0d,
  2655. .head_state5 = 0x0f,
  2656. .pll0 = 0x17,
  2657. .pll1 = 0x18,
  2658. .pll2 = 0x19,
  2659. .pll3 = 0x1a,
  2660. .dp_padctl0 = 0x5c,
  2661. .dp_padctl2 = 0x73,
  2662. };
  2663. /* Tegra124 and Tegra132 have lanes 0 and 2 swapped. */
  2664. static const u8 tegra124_sor_lane_map[4] = {
  2665. 2, 1, 0, 3,
  2666. };
  2667. static const u8 tegra124_sor_voltage_swing[4][4][4] = {
  2668. {
  2669. { 0x13, 0x19, 0x1e, 0x28 },
  2670. { 0x1e, 0x25, 0x2d, },
  2671. { 0x28, 0x32, },
  2672. { 0x3c, },
  2673. }, {
  2674. { 0x12, 0x17, 0x1b, 0x25 },
  2675. { 0x1c, 0x23, 0x2a, },
  2676. { 0x25, 0x2f, },
  2677. { 0x39, }
  2678. }, {
  2679. { 0x12, 0x16, 0x1a, 0x22 },
  2680. { 0x1b, 0x20, 0x27, },
  2681. { 0x24, 0x2d, },
  2682. { 0x36, },
  2683. }, {
  2684. { 0x11, 0x14, 0x17, 0x1f },
  2685. { 0x19, 0x1e, 0x24, },
  2686. { 0x22, 0x2a, },
  2687. { 0x32, },
  2688. },
  2689. };
  2690. static const u8 tegra124_sor_pre_emphasis[4][4][4] = {
  2691. {
  2692. { 0x00, 0x09, 0x13, 0x25 },
  2693. { 0x00, 0x0f, 0x1e, },
  2694. { 0x00, 0x14, },
  2695. { 0x00, },
  2696. }, {
  2697. { 0x00, 0x0a, 0x14, 0x28 },
  2698. { 0x00, 0x0f, 0x1e, },
  2699. { 0x00, 0x14, },
  2700. { 0x00 },
  2701. }, {
  2702. { 0x00, 0x0a, 0x14, 0x28 },
  2703. { 0x00, 0x0f, 0x1e, },
  2704. { 0x00, 0x14, },
  2705. { 0x00, },
  2706. }, {
  2707. { 0x00, 0x0a, 0x14, 0x28 },
  2708. { 0x00, 0x0f, 0x1e, },
  2709. { 0x00, 0x14, },
  2710. { 0x00, },
  2711. },
  2712. };
  2713. static const u8 tegra124_sor_post_cursor[4][4][4] = {
  2714. {
  2715. { 0x00, 0x00, 0x00, 0x00 },
  2716. { 0x00, 0x00, 0x00, },
  2717. { 0x00, 0x00, },
  2718. { 0x00, },
  2719. }, {
  2720. { 0x02, 0x02, 0x04, 0x05 },
  2721. { 0x02, 0x04, 0x05, },
  2722. { 0x04, 0x05, },
  2723. { 0x05, },
  2724. }, {
  2725. { 0x04, 0x05, 0x08, 0x0b },
  2726. { 0x05, 0x09, 0x0b, },
  2727. { 0x08, 0x0a, },
  2728. { 0x0b, },
  2729. }, {
  2730. { 0x05, 0x09, 0x0b, 0x12 },
  2731. { 0x09, 0x0d, 0x12, },
  2732. { 0x0b, 0x0f, },
  2733. { 0x12, },
  2734. },
  2735. };
  2736. static const u8 tegra124_sor_tx_pu[4][4][4] = {
  2737. {
  2738. { 0x20, 0x30, 0x40, 0x60 },
  2739. { 0x30, 0x40, 0x60, },
  2740. { 0x40, 0x60, },
  2741. { 0x60, },
  2742. }, {
  2743. { 0x20, 0x20, 0x30, 0x50 },
  2744. { 0x30, 0x40, 0x50, },
  2745. { 0x40, 0x50, },
  2746. { 0x60, },
  2747. }, {
  2748. { 0x20, 0x20, 0x30, 0x40, },
  2749. { 0x30, 0x30, 0x40, },
  2750. { 0x40, 0x50, },
  2751. { 0x60, },
  2752. }, {
  2753. { 0x20, 0x20, 0x20, 0x40, },
  2754. { 0x30, 0x30, 0x40, },
  2755. { 0x40, 0x40, },
  2756. { 0x60, },
  2757. },
  2758. };
  2759. static const struct tegra_sor_soc tegra124_sor = {
  2760. .supports_lvds = true,
  2761. .supports_hdmi = false,
  2762. .supports_dp = true,
  2763. .supports_audio = false,
  2764. .supports_hdcp = false,
  2765. .regs = &tegra124_sor_regs,
  2766. .has_nvdisplay = false,
  2767. .xbar_cfg = tegra124_sor_xbar_cfg,
  2768. .lane_map = tegra124_sor_lane_map,
  2769. .voltage_swing = tegra124_sor_voltage_swing,
  2770. .pre_emphasis = tegra124_sor_pre_emphasis,
  2771. .post_cursor = tegra124_sor_post_cursor,
  2772. .tx_pu = tegra124_sor_tx_pu,
  2773. };
  2774. static const u8 tegra132_sor_pre_emphasis[4][4][4] = {
  2775. {
  2776. { 0x00, 0x08, 0x12, 0x24 },
  2777. { 0x01, 0x0e, 0x1d, },
  2778. { 0x01, 0x13, },
  2779. { 0x00, },
  2780. }, {
  2781. { 0x00, 0x08, 0x12, 0x24 },
  2782. { 0x00, 0x0e, 0x1d, },
  2783. { 0x00, 0x13, },
  2784. { 0x00 },
  2785. }, {
  2786. { 0x00, 0x08, 0x12, 0x24 },
  2787. { 0x00, 0x0e, 0x1d, },
  2788. { 0x00, 0x13, },
  2789. { 0x00, },
  2790. }, {
  2791. { 0x00, 0x08, 0x12, 0x24 },
  2792. { 0x00, 0x0e, 0x1d, },
  2793. { 0x00, 0x13, },
  2794. { 0x00, },
  2795. },
  2796. };
  2797. static const struct tegra_sor_soc tegra132_sor = {
  2798. .supports_lvds = true,
  2799. .supports_hdmi = false,
  2800. .supports_dp = true,
  2801. .supports_audio = false,
  2802. .supports_hdcp = false,
  2803. .regs = &tegra124_sor_regs,
  2804. .has_nvdisplay = false,
  2805. .xbar_cfg = tegra124_sor_xbar_cfg,
  2806. .lane_map = tegra124_sor_lane_map,
  2807. .voltage_swing = tegra124_sor_voltage_swing,
  2808. .pre_emphasis = tegra132_sor_pre_emphasis,
  2809. .post_cursor = tegra124_sor_post_cursor,
  2810. .tx_pu = tegra124_sor_tx_pu,
  2811. };
  2812. static const struct tegra_sor_regs tegra210_sor_regs = {
  2813. .head_state0 = 0x05,
  2814. .head_state1 = 0x07,
  2815. .head_state2 = 0x09,
  2816. .head_state3 = 0x0b,
  2817. .head_state4 = 0x0d,
  2818. .head_state5 = 0x0f,
  2819. .pll0 = 0x17,
  2820. .pll1 = 0x18,
  2821. .pll2 = 0x19,
  2822. .pll3 = 0x1a,
  2823. .dp_padctl0 = 0x5c,
  2824. .dp_padctl2 = 0x73,
  2825. };
  2826. static const u8 tegra210_sor_xbar_cfg[5] = {
  2827. 2, 1, 0, 3, 4
  2828. };
  2829. static const u8 tegra210_sor_lane_map[4] = {
  2830. 0, 1, 2, 3,
  2831. };
  2832. static const struct tegra_sor_soc tegra210_sor = {
  2833. .supports_lvds = false,
  2834. .supports_hdmi = false,
  2835. .supports_dp = true,
  2836. .supports_audio = false,
  2837. .supports_hdcp = false,
  2838. .regs = &tegra210_sor_regs,
  2839. .has_nvdisplay = false,
  2840. .xbar_cfg = tegra210_sor_xbar_cfg,
  2841. .lane_map = tegra210_sor_lane_map,
  2842. .voltage_swing = tegra124_sor_voltage_swing,
  2843. .pre_emphasis = tegra124_sor_pre_emphasis,
  2844. .post_cursor = tegra124_sor_post_cursor,
  2845. .tx_pu = tegra124_sor_tx_pu,
  2846. };
  2847. static const struct tegra_sor_soc tegra210_sor1 = {
  2848. .supports_lvds = false,
  2849. .supports_hdmi = true,
  2850. .supports_dp = true,
  2851. .supports_audio = true,
  2852. .supports_hdcp = true,
  2853. .regs = &tegra210_sor_regs,
  2854. .has_nvdisplay = false,
  2855. .num_settings = ARRAY_SIZE(tegra210_sor_hdmi_defaults),
  2856. .settings = tegra210_sor_hdmi_defaults,
  2857. .xbar_cfg = tegra210_sor_xbar_cfg,
  2858. .lane_map = tegra210_sor_lane_map,
  2859. .voltage_swing = tegra124_sor_voltage_swing,
  2860. .pre_emphasis = tegra124_sor_pre_emphasis,
  2861. .post_cursor = tegra124_sor_post_cursor,
  2862. .tx_pu = tegra124_sor_tx_pu,
  2863. };
  2864. static const struct tegra_sor_regs tegra186_sor_regs = {
  2865. .head_state0 = 0x151,
  2866. .head_state1 = 0x154,
  2867. .head_state2 = 0x157,
  2868. .head_state3 = 0x15a,
  2869. .head_state4 = 0x15d,
  2870. .head_state5 = 0x160,
  2871. .pll0 = 0x163,
  2872. .pll1 = 0x164,
  2873. .pll2 = 0x165,
  2874. .pll3 = 0x166,
  2875. .dp_padctl0 = 0x168,
  2876. .dp_padctl2 = 0x16a,
  2877. };
  2878. static const u8 tegra186_sor_voltage_swing[4][4][4] = {
  2879. {
  2880. { 0x13, 0x19, 0x1e, 0x28 },
  2881. { 0x1e, 0x25, 0x2d, },
  2882. { 0x28, 0x32, },
  2883. { 0x39, },
  2884. }, {
  2885. { 0x12, 0x16, 0x1b, 0x25 },
  2886. { 0x1c, 0x23, 0x2a, },
  2887. { 0x25, 0x2f, },
  2888. { 0x37, }
  2889. }, {
  2890. { 0x12, 0x16, 0x1a, 0x22 },
  2891. { 0x1b, 0x20, 0x27, },
  2892. { 0x24, 0x2d, },
  2893. { 0x35, },
  2894. }, {
  2895. { 0x11, 0x14, 0x17, 0x1f },
  2896. { 0x19, 0x1e, 0x24, },
  2897. { 0x22, 0x2a, },
  2898. { 0x32, },
  2899. },
  2900. };
  2901. static const u8 tegra186_sor_pre_emphasis[4][4][4] = {
  2902. {
  2903. { 0x00, 0x08, 0x12, 0x24 },
  2904. { 0x01, 0x0e, 0x1d, },
  2905. { 0x01, 0x13, },
  2906. { 0x00, },
  2907. }, {
  2908. { 0x00, 0x08, 0x12, 0x24 },
  2909. { 0x00, 0x0e, 0x1d, },
  2910. { 0x00, 0x13, },
  2911. { 0x00 },
  2912. }, {
  2913. { 0x00, 0x08, 0x14, 0x24 },
  2914. { 0x00, 0x0e, 0x1d, },
  2915. { 0x00, 0x13, },
  2916. { 0x00, },
  2917. }, {
  2918. { 0x00, 0x08, 0x12, 0x24 },
  2919. { 0x00, 0x0e, 0x1d, },
  2920. { 0x00, 0x13, },
  2921. { 0x00, },
  2922. },
  2923. };
  2924. static const struct tegra_sor_soc tegra186_sor = {
  2925. .supports_lvds = false,
  2926. .supports_hdmi = true,
  2927. .supports_dp = true,
  2928. .supports_audio = true,
  2929. .supports_hdcp = true,
  2930. .regs = &tegra186_sor_regs,
  2931. .has_nvdisplay = true,
  2932. .num_settings = ARRAY_SIZE(tegra186_sor_hdmi_defaults),
  2933. .settings = tegra186_sor_hdmi_defaults,
  2934. .xbar_cfg = tegra124_sor_xbar_cfg,
  2935. .lane_map = tegra124_sor_lane_map,
  2936. .voltage_swing = tegra186_sor_voltage_swing,
  2937. .pre_emphasis = tegra186_sor_pre_emphasis,
  2938. .post_cursor = tegra124_sor_post_cursor,
  2939. .tx_pu = tegra124_sor_tx_pu,
  2940. };
  2941. static const struct tegra_sor_regs tegra194_sor_regs = {
  2942. .head_state0 = 0x151,
  2943. .head_state1 = 0x155,
  2944. .head_state2 = 0x159,
  2945. .head_state3 = 0x15d,
  2946. .head_state4 = 0x161,
  2947. .head_state5 = 0x165,
  2948. .pll0 = 0x169,
  2949. .pll1 = 0x16a,
  2950. .pll2 = 0x16b,
  2951. .pll3 = 0x16c,
  2952. .dp_padctl0 = 0x16e,
  2953. .dp_padctl2 = 0x16f,
  2954. };
  2955. static const struct tegra_sor_soc tegra194_sor = {
  2956. .supports_lvds = false,
  2957. .supports_hdmi = true,
  2958. .supports_dp = true,
  2959. .supports_audio = true,
  2960. .supports_hdcp = true,
  2961. .regs = &tegra194_sor_regs,
  2962. .has_nvdisplay = true,
  2963. .num_settings = ARRAY_SIZE(tegra194_sor_hdmi_defaults),
  2964. .settings = tegra194_sor_hdmi_defaults,
  2965. .xbar_cfg = tegra210_sor_xbar_cfg,
  2966. .lane_map = tegra124_sor_lane_map,
  2967. .voltage_swing = tegra186_sor_voltage_swing,
  2968. .pre_emphasis = tegra186_sor_pre_emphasis,
  2969. .post_cursor = tegra124_sor_post_cursor,
  2970. .tx_pu = tegra124_sor_tx_pu,
  2971. };
  2972. static const struct of_device_id tegra_sor_of_match[] = {
  2973. { .compatible = "nvidia,tegra194-sor", .data = &tegra194_sor },
  2974. { .compatible = "nvidia,tegra186-sor", .data = &tegra186_sor },
  2975. { .compatible = "nvidia,tegra210-sor1", .data = &tegra210_sor1 },
  2976. { .compatible = "nvidia,tegra210-sor", .data = &tegra210_sor },
  2977. { .compatible = "nvidia,tegra132-sor", .data = &tegra132_sor },
  2978. { .compatible = "nvidia,tegra124-sor", .data = &tegra124_sor },
  2979. { },
  2980. };
  2981. MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
  2982. static int tegra_sor_parse_dt(struct tegra_sor *sor)
  2983. {
  2984. struct device_node *np = sor->dev->of_node;
  2985. u32 xbar_cfg[5];
  2986. unsigned int i;
  2987. u32 value;
  2988. int err;
  2989. if (sor->soc->has_nvdisplay) {
  2990. err = of_property_read_u32(np, "nvidia,interface", &value);
  2991. if (err < 0)
  2992. return err;
  2993. sor->index = value;
  2994. /*
  2995. * override the default that we already set for Tegra210 and
  2996. * earlier
  2997. */
  2998. sor->pad = TEGRA_IO_PAD_HDMI_DP0 + sor->index;
  2999. } else {
  3000. if (!sor->soc->supports_audio)
  3001. sor->index = 0;
  3002. else
  3003. sor->index = 1;
  3004. }
  3005. err = of_property_read_u32_array(np, "nvidia,xbar-cfg", xbar_cfg, 5);
  3006. if (err < 0) {
  3007. /* fall back to default per-SoC XBAR configuration */
  3008. for (i = 0; i < 5; i++)
  3009. sor->xbar_cfg[i] = sor->soc->xbar_cfg[i];
  3010. } else {
  3011. /* copy cells to SOR XBAR configuration */
  3012. for (i = 0; i < 5; i++)
  3013. sor->xbar_cfg[i] = xbar_cfg[i];
  3014. }
  3015. return 0;
  3016. }
  3017. static irqreturn_t tegra_sor_irq(int irq, void *data)
  3018. {
  3019. struct tegra_sor *sor = data;
  3020. u32 value;
  3021. value = tegra_sor_readl(sor, SOR_INT_STATUS);
  3022. tegra_sor_writel(sor, value, SOR_INT_STATUS);
  3023. if (value & SOR_INT_CODEC_SCRATCH0) {
  3024. value = tegra_sor_readl(sor, SOR_AUDIO_HDA_CODEC_SCRATCH0);
  3025. if (value & SOR_AUDIO_HDA_CODEC_SCRATCH0_VALID) {
  3026. unsigned int format;
  3027. format = value & SOR_AUDIO_HDA_CODEC_SCRATCH0_FMT_MASK;
  3028. tegra_hda_parse_format(format, &sor->format);
  3029. if (sor->ops->audio_enable)
  3030. sor->ops->audio_enable(sor);
  3031. } else {
  3032. if (sor->ops->audio_disable)
  3033. sor->ops->audio_disable(sor);
  3034. }
  3035. }
  3036. return IRQ_HANDLED;
  3037. }
  3038. static int tegra_sor_probe(struct platform_device *pdev)
  3039. {
  3040. struct device_node *np;
  3041. struct tegra_sor *sor;
  3042. int err;
  3043. sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
  3044. if (!sor)
  3045. return -ENOMEM;
  3046. sor->soc = of_device_get_match_data(&pdev->dev);
  3047. sor->output.dev = sor->dev = &pdev->dev;
  3048. sor->settings = devm_kmemdup(&pdev->dev, sor->soc->settings,
  3049. sor->soc->num_settings *
  3050. sizeof(*sor->settings),
  3051. GFP_KERNEL);
  3052. if (!sor->settings)
  3053. return -ENOMEM;
  3054. sor->num_settings = sor->soc->num_settings;
  3055. np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
  3056. if (np) {
  3057. sor->aux = drm_dp_aux_find_by_of_node(np);
  3058. of_node_put(np);
  3059. if (!sor->aux)
  3060. return -EPROBE_DEFER;
  3061. if (get_device(sor->aux->dev))
  3062. sor->output.ddc = &sor->aux->ddc;
  3063. }
  3064. if (!sor->aux) {
  3065. if (sor->soc->supports_hdmi) {
  3066. sor->ops = &tegra_sor_hdmi_ops;
  3067. sor->pad = TEGRA_IO_PAD_HDMI;
  3068. } else if (sor->soc->supports_lvds) {
  3069. dev_err(&pdev->dev, "LVDS not supported yet\n");
  3070. return -ENODEV;
  3071. } else {
  3072. dev_err(&pdev->dev, "unknown (non-DP) support\n");
  3073. return -ENODEV;
  3074. }
  3075. } else {
  3076. np = of_parse_phandle(pdev->dev.of_node, "nvidia,panel", 0);
  3077. /*
  3078. * No need to keep this around since we only use it as a check
  3079. * to see if a panel is connected (eDP) or not (DP).
  3080. */
  3081. of_node_put(np);
  3082. sor->ops = &tegra_sor_dp_ops;
  3083. sor->pad = TEGRA_IO_PAD_LVDS;
  3084. }
  3085. err = tegra_sor_parse_dt(sor);
  3086. if (err < 0)
  3087. goto put_aux;
  3088. err = tegra_output_probe(&sor->output);
  3089. if (err < 0) {
  3090. dev_err_probe(&pdev->dev, err, "failed to probe output\n");
  3091. goto put_aux;
  3092. }
  3093. if (sor->ops && sor->ops->probe) {
  3094. err = sor->ops->probe(sor);
  3095. if (err < 0) {
  3096. dev_err(&pdev->dev, "failed to probe %s: %d\n",
  3097. sor->ops->name, err);
  3098. goto remove;
  3099. }
  3100. }
  3101. sor->regs = devm_platform_ioremap_resource(pdev, 0);
  3102. if (IS_ERR(sor->regs)) {
  3103. err = PTR_ERR(sor->regs);
  3104. goto remove;
  3105. }
  3106. err = platform_get_irq(pdev, 0);
  3107. if (err < 0)
  3108. goto remove;
  3109. sor->irq = err;
  3110. err = devm_request_irq(sor->dev, sor->irq, tegra_sor_irq, 0,
  3111. dev_name(sor->dev), sor);
  3112. if (err < 0) {
  3113. dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
  3114. goto remove;
  3115. }
  3116. sor->rst = devm_reset_control_get_exclusive_released(&pdev->dev, "sor");
  3117. if (IS_ERR(sor->rst)) {
  3118. err = PTR_ERR(sor->rst);
  3119. if (err != -EBUSY || WARN_ON(!pdev->dev.pm_domain)) {
  3120. dev_err(&pdev->dev, "failed to get reset control: %d\n",
  3121. err);
  3122. goto remove;
  3123. }
  3124. /*
  3125. * At this point, the reset control is most likely being used
  3126. * by the generic power domain implementation. With any luck
  3127. * the power domain will have taken care of resetting the SOR
  3128. * and we don't have to do anything.
  3129. */
  3130. sor->rst = NULL;
  3131. }
  3132. sor->clk = devm_clk_get(&pdev->dev, NULL);
  3133. if (IS_ERR(sor->clk)) {
  3134. err = PTR_ERR(sor->clk);
  3135. dev_err(&pdev->dev, "failed to get module clock: %d\n", err);
  3136. goto remove;
  3137. }
  3138. if (sor->soc->supports_hdmi || sor->soc->supports_dp) {
  3139. struct device_node *np = pdev->dev.of_node;
  3140. const char *name;
  3141. /*
  3142. * For backwards compatibility with Tegra210 device trees,
  3143. * fall back to the old clock name "source" if the new "out"
  3144. * clock is not available.
  3145. */
  3146. if (of_property_match_string(np, "clock-names", "out") < 0)
  3147. name = "source";
  3148. else
  3149. name = "out";
  3150. sor->clk_out = devm_clk_get(&pdev->dev, name);
  3151. if (IS_ERR(sor->clk_out)) {
  3152. err = PTR_ERR(sor->clk_out);
  3153. dev_err(sor->dev, "failed to get %s clock: %d\n",
  3154. name, err);
  3155. goto remove;
  3156. }
  3157. } else {
  3158. /* fall back to the module clock on SOR0 (eDP/LVDS only) */
  3159. sor->clk_out = sor->clk;
  3160. }
  3161. sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
  3162. if (IS_ERR(sor->clk_parent)) {
  3163. err = PTR_ERR(sor->clk_parent);
  3164. dev_err(&pdev->dev, "failed to get parent clock: %d\n", err);
  3165. goto remove;
  3166. }
  3167. sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
  3168. if (IS_ERR(sor->clk_safe)) {
  3169. err = PTR_ERR(sor->clk_safe);
  3170. dev_err(&pdev->dev, "failed to get safe clock: %d\n", err);
  3171. goto remove;
  3172. }
  3173. sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
  3174. if (IS_ERR(sor->clk_dp)) {
  3175. err = PTR_ERR(sor->clk_dp);
  3176. dev_err(&pdev->dev, "failed to get DP clock: %d\n", err);
  3177. goto remove;
  3178. }
  3179. /*
  3180. * Starting with Tegra186, the BPMP provides an implementation for
  3181. * the pad output clock, so we have to look it up from device tree.
  3182. */
  3183. sor->clk_pad = devm_clk_get(&pdev->dev, "pad");
  3184. if (IS_ERR(sor->clk_pad)) {
  3185. if (sor->clk_pad != ERR_PTR(-ENOENT)) {
  3186. err = PTR_ERR(sor->clk_pad);
  3187. goto remove;
  3188. }
  3189. /*
  3190. * If the pad output clock is not available, then we assume
  3191. * we're on Tegra210 or earlier and have to provide our own
  3192. * implementation.
  3193. */
  3194. sor->clk_pad = NULL;
  3195. }
  3196. /*
  3197. * The bootloader may have set up the SOR such that it's module clock
  3198. * is sourced by one of the display PLLs. However, that doesn't work
  3199. * without properly having set up other bits of the SOR.
  3200. */
  3201. err = clk_set_parent(sor->clk_out, sor->clk_safe);
  3202. if (err < 0) {
  3203. dev_err(&pdev->dev, "failed to use safe clock: %d\n", err);
  3204. goto remove;
  3205. }
  3206. platform_set_drvdata(pdev, sor);
  3207. pm_runtime_enable(&pdev->dev);
  3208. host1x_client_init(&sor->client);
  3209. sor->client.ops = &sor_client_ops;
  3210. sor->client.dev = &pdev->dev;
  3211. /*
  3212. * On Tegra210 and earlier, provide our own implementation for the
  3213. * pad output clock.
  3214. */
  3215. if (!sor->clk_pad) {
  3216. char *name;
  3217. name = devm_kasprintf(sor->dev, GFP_KERNEL, "sor%u_pad_clkout",
  3218. sor->index);
  3219. if (!name) {
  3220. err = -ENOMEM;
  3221. goto uninit;
  3222. }
  3223. err = host1x_client_resume(&sor->client);
  3224. if (err < 0) {
  3225. dev_err(sor->dev, "failed to resume: %d\n", err);
  3226. goto uninit;
  3227. }
  3228. sor->clk_pad = tegra_clk_sor_pad_register(sor, name);
  3229. host1x_client_suspend(&sor->client);
  3230. }
  3231. if (IS_ERR(sor->clk_pad)) {
  3232. err = PTR_ERR(sor->clk_pad);
  3233. dev_err(sor->dev, "failed to register SOR pad clock: %d\n",
  3234. err);
  3235. goto uninit;
  3236. }
  3237. err = __host1x_client_register(&sor->client);
  3238. if (err < 0) {
  3239. dev_err(&pdev->dev, "failed to register host1x client: %d\n",
  3240. err);
  3241. goto uninit;
  3242. }
  3243. return 0;
  3244. uninit:
  3245. host1x_client_exit(&sor->client);
  3246. pm_runtime_disable(&pdev->dev);
  3247. remove:
  3248. if (sor->aux)
  3249. sor->output.ddc = NULL;
  3250. tegra_output_remove(&sor->output);
  3251. put_aux:
  3252. if (sor->aux)
  3253. put_device(sor->aux->dev);
  3254. return err;
  3255. }
  3256. static void tegra_sor_remove(struct platform_device *pdev)
  3257. {
  3258. struct tegra_sor *sor = platform_get_drvdata(pdev);
  3259. host1x_client_unregister(&sor->client);
  3260. pm_runtime_disable(&pdev->dev);
  3261. if (sor->aux) {
  3262. put_device(sor->aux->dev);
  3263. sor->output.ddc = NULL;
  3264. }
  3265. tegra_output_remove(&sor->output);
  3266. }
  3267. static int __maybe_unused tegra_sor_suspend(struct device *dev)
  3268. {
  3269. struct tegra_sor *sor = dev_get_drvdata(dev);
  3270. int err;
  3271. err = tegra_output_suspend(&sor->output);
  3272. if (err < 0) {
  3273. dev_err(dev, "failed to suspend output: %d\n", err);
  3274. return err;
  3275. }
  3276. if (sor->hdmi_supply) {
  3277. err = regulator_disable(sor->hdmi_supply);
  3278. if (err < 0) {
  3279. tegra_output_resume(&sor->output);
  3280. return err;
  3281. }
  3282. }
  3283. return 0;
  3284. }
  3285. static int __maybe_unused tegra_sor_resume(struct device *dev)
  3286. {
  3287. struct tegra_sor *sor = dev_get_drvdata(dev);
  3288. int err;
  3289. if (sor->hdmi_supply) {
  3290. err = regulator_enable(sor->hdmi_supply);
  3291. if (err < 0)
  3292. return err;
  3293. }
  3294. err = tegra_output_resume(&sor->output);
  3295. if (err < 0) {
  3296. dev_err(dev, "failed to resume output: %d\n", err);
  3297. if (sor->hdmi_supply)
  3298. regulator_disable(sor->hdmi_supply);
  3299. return err;
  3300. }
  3301. return 0;
  3302. }
  3303. static const struct dev_pm_ops tegra_sor_pm_ops = {
  3304. SET_SYSTEM_SLEEP_PM_OPS(tegra_sor_suspend, tegra_sor_resume)
  3305. };
  3306. struct platform_driver tegra_sor_driver = {
  3307. .driver = {
  3308. .name = "tegra-sor",
  3309. .of_match_table = tegra_sor_of_match,
  3310. .pm = &tegra_sor_pm_ops,
  3311. },
  3312. .probe = tegra_sor_probe,
  3313. .remove_new = tegra_sor_remove,
  3314. };