sor.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348
  1. /*
  2. * Copyright (C) 2013 NVIDIA Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/clk-provider.h>
  10. #include <linux/debugfs.h>
  11. #include <linux/gpio.h>
  12. #include <linux/io.h>
  13. #include <linux/of_device.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <linux/reset.h>
  18. #include <soc/tegra/pmc.h>
  19. #include <drm/drm_atomic_helper.h>
  20. #include <drm/drm_dp_helper.h>
  21. #include <drm/drm_panel.h>
  22. #include <drm/drm_scdc_helper.h>
  23. #include "dc.h"
  24. #include "drm.h"
  25. #include "sor.h"
  26. #include "trace.h"
  27. /*
  28. * XXX Remove this after the commit adding it to soc/tegra/pmc.h has been
  29. * merged. Having this around after the commit is merged should be safe since
  30. * the preprocessor will effectively replace all occurrences and therefore no
  31. * duplicate will be defined.
  32. */
  33. #define TEGRA_IO_PAD_HDMI_DP0 26
  34. #define SOR_REKEY 0x38
  35. struct tegra_sor_hdmi_settings {
  36. unsigned long frequency;
  37. u8 vcocap;
  38. u8 filter;
  39. u8 ichpmp;
  40. u8 loadadj;
  41. u8 tmds_termadj;
  42. u8 tx_pu_value;
  43. u8 bg_temp_coef;
  44. u8 bg_vref_level;
  45. u8 avdd10_level;
  46. u8 avdd14_level;
  47. u8 sparepll;
  48. u8 drive_current[4];
  49. u8 preemphasis[4];
  50. };
  51. #if 1
  52. static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
  53. {
  54. .frequency = 54000000,
  55. .vcocap = 0x0,
  56. .filter = 0x0,
  57. .ichpmp = 0x1,
  58. .loadadj = 0x3,
  59. .tmds_termadj = 0x9,
  60. .tx_pu_value = 0x10,
  61. .bg_temp_coef = 0x3,
  62. .bg_vref_level = 0x8,
  63. .avdd10_level = 0x4,
  64. .avdd14_level = 0x4,
  65. .sparepll = 0x0,
  66. .drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
  67. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  68. }, {
  69. .frequency = 75000000,
  70. .vcocap = 0x3,
  71. .filter = 0x0,
  72. .ichpmp = 0x1,
  73. .loadadj = 0x3,
  74. .tmds_termadj = 0x9,
  75. .tx_pu_value = 0x40,
  76. .bg_temp_coef = 0x3,
  77. .bg_vref_level = 0x8,
  78. .avdd10_level = 0x4,
  79. .avdd14_level = 0x4,
  80. .sparepll = 0x0,
  81. .drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
  82. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  83. }, {
  84. .frequency = 150000000,
  85. .vcocap = 0x3,
  86. .filter = 0x0,
  87. .ichpmp = 0x1,
  88. .loadadj = 0x3,
  89. .tmds_termadj = 0x9,
  90. .tx_pu_value = 0x66,
  91. .bg_temp_coef = 0x3,
  92. .bg_vref_level = 0x8,
  93. .avdd10_level = 0x4,
  94. .avdd14_level = 0x4,
  95. .sparepll = 0x0,
  96. .drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
  97. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  98. }, {
  99. .frequency = 300000000,
  100. .vcocap = 0x3,
  101. .filter = 0x0,
  102. .ichpmp = 0x1,
  103. .loadadj = 0x3,
  104. .tmds_termadj = 0x9,
  105. .tx_pu_value = 0x66,
  106. .bg_temp_coef = 0x3,
  107. .bg_vref_level = 0xa,
  108. .avdd10_level = 0x4,
  109. .avdd14_level = 0x4,
  110. .sparepll = 0x0,
  111. .drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
  112. .preemphasis = { 0x00, 0x17, 0x17, 0x17 },
  113. }, {
  114. .frequency = 600000000,
  115. .vcocap = 0x3,
  116. .filter = 0x0,
  117. .ichpmp = 0x1,
  118. .loadadj = 0x3,
  119. .tmds_termadj = 0x9,
  120. .tx_pu_value = 0x66,
  121. .bg_temp_coef = 0x3,
  122. .bg_vref_level = 0x8,
  123. .avdd10_level = 0x4,
  124. .avdd14_level = 0x4,
  125. .sparepll = 0x0,
  126. .drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
  127. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  128. },
  129. };
  130. #else
  131. static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
  132. {
  133. .frequency = 75000000,
  134. .vcocap = 0x3,
  135. .filter = 0x0,
  136. .ichpmp = 0x1,
  137. .loadadj = 0x3,
  138. .tmds_termadj = 0x9,
  139. .tx_pu_value = 0x40,
  140. .bg_temp_coef = 0x3,
  141. .bg_vref_level = 0x8,
  142. .avdd10_level = 0x4,
  143. .avdd14_level = 0x4,
  144. .sparepll = 0x0,
  145. .drive_current = { 0x29, 0x29, 0x29, 0x29 },
  146. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  147. }, {
  148. .frequency = 150000000,
  149. .vcocap = 0x3,
  150. .filter = 0x0,
  151. .ichpmp = 0x1,
  152. .loadadj = 0x3,
  153. .tmds_termadj = 0x9,
  154. .tx_pu_value = 0x66,
  155. .bg_temp_coef = 0x3,
  156. .bg_vref_level = 0x8,
  157. .avdd10_level = 0x4,
  158. .avdd14_level = 0x4,
  159. .sparepll = 0x0,
  160. .drive_current = { 0x30, 0x37, 0x37, 0x37 },
  161. .preemphasis = { 0x01, 0x02, 0x02, 0x02 },
  162. }, {
  163. .frequency = 300000000,
  164. .vcocap = 0x3,
  165. .filter = 0x0,
  166. .ichpmp = 0x6,
  167. .loadadj = 0x3,
  168. .tmds_termadj = 0x9,
  169. .tx_pu_value = 0x66,
  170. .bg_temp_coef = 0x3,
  171. .bg_vref_level = 0xf,
  172. .avdd10_level = 0x4,
  173. .avdd14_level = 0x4,
  174. .sparepll = 0x0,
  175. .drive_current = { 0x30, 0x37, 0x37, 0x37 },
  176. .preemphasis = { 0x10, 0x3e, 0x3e, 0x3e },
  177. }, {
  178. .frequency = 600000000,
  179. .vcocap = 0x3,
  180. .filter = 0x0,
  181. .ichpmp = 0xa,
  182. .loadadj = 0x3,
  183. .tmds_termadj = 0xb,
  184. .tx_pu_value = 0x66,
  185. .bg_temp_coef = 0x3,
  186. .bg_vref_level = 0xe,
  187. .avdd10_level = 0x4,
  188. .avdd14_level = 0x4,
  189. .sparepll = 0x0,
  190. .drive_current = { 0x35, 0x3e, 0x3e, 0x3e },
  191. .preemphasis = { 0x02, 0x3f, 0x3f, 0x3f },
  192. },
  193. };
  194. #endif
  195. static const struct tegra_sor_hdmi_settings tegra186_sor_hdmi_defaults[] = {
  196. {
  197. .frequency = 54000000,
  198. .vcocap = 0,
  199. .filter = 5,
  200. .ichpmp = 5,
  201. .loadadj = 3,
  202. .tmds_termadj = 0xf,
  203. .tx_pu_value = 0,
  204. .bg_temp_coef = 3,
  205. .bg_vref_level = 8,
  206. .avdd10_level = 4,
  207. .avdd14_level = 4,
  208. .sparepll = 0x54,
  209. .drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
  210. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  211. }, {
  212. .frequency = 75000000,
  213. .vcocap = 1,
  214. .filter = 5,
  215. .ichpmp = 5,
  216. .loadadj = 3,
  217. .tmds_termadj = 0xf,
  218. .tx_pu_value = 0,
  219. .bg_temp_coef = 3,
  220. .bg_vref_level = 8,
  221. .avdd10_level = 4,
  222. .avdd14_level = 4,
  223. .sparepll = 0x44,
  224. .drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
  225. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  226. }, {
  227. .frequency = 150000000,
  228. .vcocap = 3,
  229. .filter = 5,
  230. .ichpmp = 5,
  231. .loadadj = 3,
  232. .tmds_termadj = 15,
  233. .tx_pu_value = 0x66 /* 0 */,
  234. .bg_temp_coef = 3,
  235. .bg_vref_level = 8,
  236. .avdd10_level = 4,
  237. .avdd14_level = 4,
  238. .sparepll = 0x00, /* 0x34 */
  239. .drive_current = { 0x3a, 0x3a, 0x3a, 0x37 },
  240. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  241. }, {
  242. .frequency = 300000000,
  243. .vcocap = 3,
  244. .filter = 5,
  245. .ichpmp = 5,
  246. .loadadj = 3,
  247. .tmds_termadj = 15,
  248. .tx_pu_value = 64,
  249. .bg_temp_coef = 3,
  250. .bg_vref_level = 8,
  251. .avdd10_level = 4,
  252. .avdd14_level = 4,
  253. .sparepll = 0x34,
  254. .drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
  255. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  256. }, {
  257. .frequency = 600000000,
  258. .vcocap = 3,
  259. .filter = 5,
  260. .ichpmp = 5,
  261. .loadadj = 3,
  262. .tmds_termadj = 12,
  263. .tx_pu_value = 96,
  264. .bg_temp_coef = 3,
  265. .bg_vref_level = 8,
  266. .avdd10_level = 4,
  267. .avdd14_level = 4,
  268. .sparepll = 0x34,
  269. .drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
  270. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  271. }
  272. };
  273. static const struct tegra_sor_hdmi_settings tegra194_sor_hdmi_defaults[] = {
  274. {
  275. .frequency = 54000000,
  276. .vcocap = 0,
  277. .filter = 5,
  278. .ichpmp = 5,
  279. .loadadj = 3,
  280. .tmds_termadj = 0xf,
  281. .tx_pu_value = 0,
  282. .bg_temp_coef = 3,
  283. .bg_vref_level = 8,
  284. .avdd10_level = 4,
  285. .avdd14_level = 4,
  286. .sparepll = 0x54,
  287. .drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
  288. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  289. }, {
  290. .frequency = 75000000,
  291. .vcocap = 1,
  292. .filter = 5,
  293. .ichpmp = 5,
  294. .loadadj = 3,
  295. .tmds_termadj = 0xf,
  296. .tx_pu_value = 0,
  297. .bg_temp_coef = 3,
  298. .bg_vref_level = 8,
  299. .avdd10_level = 4,
  300. .avdd14_level = 4,
  301. .sparepll = 0x44,
  302. .drive_current = { 0x3a, 0x3a, 0x3a, 0x33 },
  303. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  304. }, {
  305. .frequency = 150000000,
  306. .vcocap = 3,
  307. .filter = 5,
  308. .ichpmp = 5,
  309. .loadadj = 3,
  310. .tmds_termadj = 15,
  311. .tx_pu_value = 0x66 /* 0 */,
  312. .bg_temp_coef = 3,
  313. .bg_vref_level = 8,
  314. .avdd10_level = 4,
  315. .avdd14_level = 4,
  316. .sparepll = 0x00, /* 0x34 */
  317. .drive_current = { 0x3a, 0x3a, 0x3a, 0x37 },
  318. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  319. }, {
  320. .frequency = 300000000,
  321. .vcocap = 3,
  322. .filter = 5,
  323. .ichpmp = 5,
  324. .loadadj = 3,
  325. .tmds_termadj = 15,
  326. .tx_pu_value = 64,
  327. .bg_temp_coef = 3,
  328. .bg_vref_level = 8,
  329. .avdd10_level = 4,
  330. .avdd14_level = 4,
  331. .sparepll = 0x34,
  332. .drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
  333. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  334. }, {
  335. .frequency = 600000000,
  336. .vcocap = 3,
  337. .filter = 5,
  338. .ichpmp = 5,
  339. .loadadj = 3,
  340. .tmds_termadj = 12,
  341. .tx_pu_value = 96,
  342. .bg_temp_coef = 3,
  343. .bg_vref_level = 8,
  344. .avdd10_level = 4,
  345. .avdd14_level = 4,
  346. .sparepll = 0x34,
  347. .drive_current = { 0x3d, 0x3d, 0x3d, 0x33 },
  348. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  349. }
  350. };
  351. struct tegra_sor_regs {
  352. unsigned int head_state0;
  353. unsigned int head_state1;
  354. unsigned int head_state2;
  355. unsigned int head_state3;
  356. unsigned int head_state4;
  357. unsigned int head_state5;
  358. unsigned int pll0;
  359. unsigned int pll1;
  360. unsigned int pll2;
  361. unsigned int pll3;
  362. unsigned int dp_padctl0;
  363. unsigned int dp_padctl2;
  364. };
  365. struct tegra_sor_soc {
  366. bool supports_edp;
  367. bool supports_lvds;
  368. bool supports_hdmi;
  369. bool supports_dp;
  370. const struct tegra_sor_regs *regs;
  371. bool has_nvdisplay;
  372. const struct tegra_sor_hdmi_settings *settings;
  373. unsigned int num_settings;
  374. const u8 *xbar_cfg;
  375. };
  376. struct tegra_sor;
  377. struct tegra_sor_ops {
  378. const char *name;
  379. int (*probe)(struct tegra_sor *sor);
  380. int (*remove)(struct tegra_sor *sor);
  381. };
  382. struct tegra_sor {
  383. struct host1x_client client;
  384. struct tegra_output output;
  385. struct device *dev;
  386. const struct tegra_sor_soc *soc;
  387. void __iomem *regs;
  388. unsigned int index;
  389. struct reset_control *rst;
  390. struct clk *clk_parent;
  391. struct clk *clk_safe;
  392. struct clk *clk_out;
  393. struct clk *clk_pad;
  394. struct clk *clk_dp;
  395. struct clk *clk;
  396. struct drm_dp_aux *aux;
  397. struct drm_info_list *debugfs_files;
  398. const struct tegra_sor_ops *ops;
  399. enum tegra_io_pad pad;
  400. /* for HDMI 2.0 */
  401. struct tegra_sor_hdmi_settings *settings;
  402. unsigned int num_settings;
  403. struct regulator *avdd_io_supply;
  404. struct regulator *vdd_pll_supply;
  405. struct regulator *hdmi_supply;
  406. struct delayed_work scdc;
  407. bool scdc_enabled;
  408. };
  409. struct tegra_sor_state {
  410. struct drm_connector_state base;
  411. unsigned int link_speed;
  412. unsigned long pclk;
  413. unsigned int bpc;
  414. };
  415. static inline struct tegra_sor_state *
  416. to_sor_state(struct drm_connector_state *state)
  417. {
  418. return container_of(state, struct tegra_sor_state, base);
  419. }
  420. struct tegra_sor_config {
  421. u32 bits_per_pixel;
  422. u32 active_polarity;
  423. u32 active_count;
  424. u32 tu_size;
  425. u32 active_frac;
  426. u32 watermark;
  427. u32 hblank_symbols;
  428. u32 vblank_symbols;
  429. };
  430. static inline struct tegra_sor *
  431. host1x_client_to_sor(struct host1x_client *client)
  432. {
  433. return container_of(client, struct tegra_sor, client);
  434. }
  435. static inline struct tegra_sor *to_sor(struct tegra_output *output)
  436. {
  437. return container_of(output, struct tegra_sor, output);
  438. }
  439. static inline u32 tegra_sor_readl(struct tegra_sor *sor, unsigned int offset)
  440. {
  441. u32 value = readl(sor->regs + (offset << 2));
  442. trace_sor_readl(sor->dev, offset, value);
  443. return value;
  444. }
  445. static inline void tegra_sor_writel(struct tegra_sor *sor, u32 value,
  446. unsigned int offset)
  447. {
  448. trace_sor_writel(sor->dev, offset, value);
  449. writel(value, sor->regs + (offset << 2));
  450. }
  451. static int tegra_sor_set_parent_clock(struct tegra_sor *sor, struct clk *parent)
  452. {
  453. int err;
  454. clk_disable_unprepare(sor->clk);
  455. err = clk_set_parent(sor->clk_out, parent);
  456. if (err < 0)
  457. return err;
  458. err = clk_prepare_enable(sor->clk);
  459. if (err < 0)
  460. return err;
  461. return 0;
  462. }
  463. struct tegra_clk_sor_pad {
  464. struct clk_hw hw;
  465. struct tegra_sor *sor;
  466. };
  467. static inline struct tegra_clk_sor_pad *to_pad(struct clk_hw *hw)
  468. {
  469. return container_of(hw, struct tegra_clk_sor_pad, hw);
  470. }
  471. static const char * const tegra_clk_sor_pad_parents[] = {
  472. "pll_d2_out0", "pll_dp"
  473. };
  474. static int tegra_clk_sor_pad_set_parent(struct clk_hw *hw, u8 index)
  475. {
  476. struct tegra_clk_sor_pad *pad = to_pad(hw);
  477. struct tegra_sor *sor = pad->sor;
  478. u32 value;
  479. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  480. value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
  481. switch (index) {
  482. case 0:
  483. value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
  484. break;
  485. case 1:
  486. value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
  487. break;
  488. }
  489. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  490. return 0;
  491. }
  492. static u8 tegra_clk_sor_pad_get_parent(struct clk_hw *hw)
  493. {
  494. struct tegra_clk_sor_pad *pad = to_pad(hw);
  495. struct tegra_sor *sor = pad->sor;
  496. u8 parent = U8_MAX;
  497. u32 value;
  498. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  499. switch (value & SOR_CLK_CNTRL_DP_CLK_SEL_MASK) {
  500. case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK:
  501. case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_PCLK:
  502. parent = 0;
  503. break;
  504. case SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK:
  505. case SOR_CLK_CNTRL_DP_CLK_SEL_DIFF_DPCLK:
  506. parent = 1;
  507. break;
  508. }
  509. return parent;
  510. }
  511. static const struct clk_ops tegra_clk_sor_pad_ops = {
  512. .set_parent = tegra_clk_sor_pad_set_parent,
  513. .get_parent = tegra_clk_sor_pad_get_parent,
  514. };
  515. static struct clk *tegra_clk_sor_pad_register(struct tegra_sor *sor,
  516. const char *name)
  517. {
  518. struct tegra_clk_sor_pad *pad;
  519. struct clk_init_data init;
  520. struct clk *clk;
  521. pad = devm_kzalloc(sor->dev, sizeof(*pad), GFP_KERNEL);
  522. if (!pad)
  523. return ERR_PTR(-ENOMEM);
  524. pad->sor = sor;
  525. init.name = name;
  526. init.flags = 0;
  527. init.parent_names = tegra_clk_sor_pad_parents;
  528. init.num_parents = ARRAY_SIZE(tegra_clk_sor_pad_parents);
  529. init.ops = &tegra_clk_sor_pad_ops;
  530. pad->hw.init = &init;
  531. clk = devm_clk_register(sor->dev, &pad->hw);
  532. return clk;
  533. }
  534. static int tegra_sor_dp_train_fast(struct tegra_sor *sor,
  535. struct drm_dp_link *link)
  536. {
  537. unsigned int i;
  538. u8 pattern;
  539. u32 value;
  540. int err;
  541. /* setup lane parameters */
  542. value = SOR_LANE_DRIVE_CURRENT_LANE3(0x40) |
  543. SOR_LANE_DRIVE_CURRENT_LANE2(0x40) |
  544. SOR_LANE_DRIVE_CURRENT_LANE1(0x40) |
  545. SOR_LANE_DRIVE_CURRENT_LANE0(0x40);
  546. tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
  547. value = SOR_LANE_PREEMPHASIS_LANE3(0x0f) |
  548. SOR_LANE_PREEMPHASIS_LANE2(0x0f) |
  549. SOR_LANE_PREEMPHASIS_LANE1(0x0f) |
  550. SOR_LANE_PREEMPHASIS_LANE0(0x0f);
  551. tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
  552. value = SOR_LANE_POSTCURSOR_LANE3(0x00) |
  553. SOR_LANE_POSTCURSOR_LANE2(0x00) |
  554. SOR_LANE_POSTCURSOR_LANE1(0x00) |
  555. SOR_LANE_POSTCURSOR_LANE0(0x00);
  556. tegra_sor_writel(sor, value, SOR_LANE_POSTCURSOR0);
  557. /* disable LVDS mode */
  558. tegra_sor_writel(sor, 0, SOR_LVDS);
  559. value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
  560. value |= SOR_DP_PADCTL_TX_PU_ENABLE;
  561. value &= ~SOR_DP_PADCTL_TX_PU_MASK;
  562. value |= SOR_DP_PADCTL_TX_PU(2); /* XXX: don't hardcode? */
  563. tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
  564. value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
  565. value |= SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
  566. SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0;
  567. tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
  568. usleep_range(10, 100);
  569. value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
  570. value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
  571. SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
  572. tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
  573. err = drm_dp_aux_prepare(sor->aux, DP_SET_ANSI_8B10B);
  574. if (err < 0)
  575. return err;
  576. for (i = 0, value = 0; i < link->num_lanes; i++) {
  577. unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
  578. SOR_DP_TPG_SCRAMBLER_NONE |
  579. SOR_DP_TPG_PATTERN_TRAIN1;
  580. value = (value << 8) | lane;
  581. }
  582. tegra_sor_writel(sor, value, SOR_DP_TPG);
  583. pattern = DP_TRAINING_PATTERN_1;
  584. err = drm_dp_aux_train(sor->aux, link, pattern);
  585. if (err < 0)
  586. return err;
  587. value = tegra_sor_readl(sor, SOR_DP_SPARE0);
  588. value |= SOR_DP_SPARE_SEQ_ENABLE;
  589. value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
  590. value |= SOR_DP_SPARE_MACRO_SOR_CLK;
  591. tegra_sor_writel(sor, value, SOR_DP_SPARE0);
  592. for (i = 0, value = 0; i < link->num_lanes; i++) {
  593. unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
  594. SOR_DP_TPG_SCRAMBLER_NONE |
  595. SOR_DP_TPG_PATTERN_TRAIN2;
  596. value = (value << 8) | lane;
  597. }
  598. tegra_sor_writel(sor, value, SOR_DP_TPG);
  599. pattern = DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_2;
  600. err = drm_dp_aux_train(sor->aux, link, pattern);
  601. if (err < 0)
  602. return err;
  603. for (i = 0, value = 0; i < link->num_lanes; i++) {
  604. unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
  605. SOR_DP_TPG_SCRAMBLER_GALIOS |
  606. SOR_DP_TPG_PATTERN_NONE;
  607. value = (value << 8) | lane;
  608. }
  609. tegra_sor_writel(sor, value, SOR_DP_TPG);
  610. pattern = DP_TRAINING_PATTERN_DISABLE;
  611. err = drm_dp_aux_train(sor->aux, link, pattern);
  612. if (err < 0)
  613. return err;
  614. return 0;
  615. }
  616. static void tegra_sor_super_update(struct tegra_sor *sor)
  617. {
  618. tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
  619. tegra_sor_writel(sor, 1, SOR_SUPER_STATE0);
  620. tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
  621. }
  622. static void tegra_sor_update(struct tegra_sor *sor)
  623. {
  624. tegra_sor_writel(sor, 0, SOR_STATE0);
  625. tegra_sor_writel(sor, 1, SOR_STATE0);
  626. tegra_sor_writel(sor, 0, SOR_STATE0);
  627. }
  628. static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
  629. {
  630. u32 value;
  631. value = tegra_sor_readl(sor, SOR_PWM_DIV);
  632. value &= ~SOR_PWM_DIV_MASK;
  633. value |= 0x400; /* period */
  634. tegra_sor_writel(sor, value, SOR_PWM_DIV);
  635. value = tegra_sor_readl(sor, SOR_PWM_CTL);
  636. value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
  637. value |= 0x400; /* duty cycle */
  638. value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
  639. value |= SOR_PWM_CTL_TRIGGER;
  640. tegra_sor_writel(sor, value, SOR_PWM_CTL);
  641. timeout = jiffies + msecs_to_jiffies(timeout);
  642. while (time_before(jiffies, timeout)) {
  643. value = tegra_sor_readl(sor, SOR_PWM_CTL);
  644. if ((value & SOR_PWM_CTL_TRIGGER) == 0)
  645. return 0;
  646. usleep_range(25, 100);
  647. }
  648. return -ETIMEDOUT;
  649. }
  650. static int tegra_sor_attach(struct tegra_sor *sor)
  651. {
  652. unsigned long value, timeout;
  653. /* wake up in normal mode */
  654. value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
  655. value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
  656. value |= SOR_SUPER_STATE_MODE_NORMAL;
  657. tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
  658. tegra_sor_super_update(sor);
  659. /* attach */
  660. value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
  661. value |= SOR_SUPER_STATE_ATTACHED;
  662. tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
  663. tegra_sor_super_update(sor);
  664. timeout = jiffies + msecs_to_jiffies(250);
  665. while (time_before(jiffies, timeout)) {
  666. value = tegra_sor_readl(sor, SOR_TEST);
  667. if ((value & SOR_TEST_ATTACHED) != 0)
  668. return 0;
  669. usleep_range(25, 100);
  670. }
  671. return -ETIMEDOUT;
  672. }
  673. static int tegra_sor_wakeup(struct tegra_sor *sor)
  674. {
  675. unsigned long value, timeout;
  676. timeout = jiffies + msecs_to_jiffies(250);
  677. /* wait for head to wake up */
  678. while (time_before(jiffies, timeout)) {
  679. value = tegra_sor_readl(sor, SOR_TEST);
  680. value &= SOR_TEST_HEAD_MODE_MASK;
  681. if (value == SOR_TEST_HEAD_MODE_AWAKE)
  682. return 0;
  683. usleep_range(25, 100);
  684. }
  685. return -ETIMEDOUT;
  686. }
  687. static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
  688. {
  689. u32 value;
  690. value = tegra_sor_readl(sor, SOR_PWR);
  691. value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
  692. tegra_sor_writel(sor, value, SOR_PWR);
  693. timeout = jiffies + msecs_to_jiffies(timeout);
  694. while (time_before(jiffies, timeout)) {
  695. value = tegra_sor_readl(sor, SOR_PWR);
  696. if ((value & SOR_PWR_TRIGGER) == 0)
  697. return 0;
  698. usleep_range(25, 100);
  699. }
  700. return -ETIMEDOUT;
  701. }
  702. struct tegra_sor_params {
  703. /* number of link clocks per line */
  704. unsigned int num_clocks;
  705. /* ratio between input and output */
  706. u64 ratio;
  707. /* precision factor */
  708. u64 precision;
  709. unsigned int active_polarity;
  710. unsigned int active_count;
  711. unsigned int active_frac;
  712. unsigned int tu_size;
  713. unsigned int error;
  714. };
  715. static int tegra_sor_compute_params(struct tegra_sor *sor,
  716. struct tegra_sor_params *params,
  717. unsigned int tu_size)
  718. {
  719. u64 active_sym, active_count, frac, approx;
  720. u32 active_polarity, active_frac = 0;
  721. const u64 f = params->precision;
  722. s64 error;
  723. active_sym = params->ratio * tu_size;
  724. active_count = div_u64(active_sym, f) * f;
  725. frac = active_sym - active_count;
  726. /* fraction < 0.5 */
  727. if (frac >= (f / 2)) {
  728. active_polarity = 1;
  729. frac = f - frac;
  730. } else {
  731. active_polarity = 0;
  732. }
  733. if (frac != 0) {
  734. frac = div_u64(f * f, frac); /* 1/fraction */
  735. if (frac <= (15 * f)) {
  736. active_frac = div_u64(frac, f);
  737. /* round up */
  738. if (active_polarity)
  739. active_frac++;
  740. } else {
  741. active_frac = active_polarity ? 1 : 15;
  742. }
  743. }
  744. if (active_frac == 1)
  745. active_polarity = 0;
  746. if (active_polarity == 1) {
  747. if (active_frac) {
  748. approx = active_count + (active_frac * (f - 1)) * f;
  749. approx = div_u64(approx, active_frac * f);
  750. } else {
  751. approx = active_count + f;
  752. }
  753. } else {
  754. if (active_frac)
  755. approx = active_count + div_u64(f, active_frac);
  756. else
  757. approx = active_count;
  758. }
  759. error = div_s64(active_sym - approx, tu_size);
  760. error *= params->num_clocks;
  761. if (error <= 0 && abs(error) < params->error) {
  762. params->active_count = div_u64(active_count, f);
  763. params->active_polarity = active_polarity;
  764. params->active_frac = active_frac;
  765. params->error = abs(error);
  766. params->tu_size = tu_size;
  767. if (error == 0)
  768. return true;
  769. }
  770. return false;
  771. }
  772. static int tegra_sor_compute_config(struct tegra_sor *sor,
  773. const struct drm_display_mode *mode,
  774. struct tegra_sor_config *config,
  775. struct drm_dp_link *link)
  776. {
  777. const u64 f = 100000, link_rate = link->rate * 1000;
  778. const u64 pclk = mode->clock * 1000;
  779. u64 input, output, watermark, num;
  780. struct tegra_sor_params params;
  781. u32 num_syms_per_line;
  782. unsigned int i;
  783. if (!link_rate || !link->num_lanes || !pclk || !config->bits_per_pixel)
  784. return -EINVAL;
  785. output = link_rate * 8 * link->num_lanes;
  786. input = pclk * config->bits_per_pixel;
  787. if (input >= output)
  788. return -ERANGE;
  789. memset(&params, 0, sizeof(params));
  790. params.ratio = div64_u64(input * f, output);
  791. params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
  792. params.precision = f;
  793. params.error = 64 * f;
  794. params.tu_size = 64;
  795. for (i = params.tu_size; i >= 32; i--)
  796. if (tegra_sor_compute_params(sor, &params, i))
  797. break;
  798. if (params.active_frac == 0) {
  799. config->active_polarity = 0;
  800. config->active_count = params.active_count;
  801. if (!params.active_polarity)
  802. config->active_count--;
  803. config->tu_size = params.tu_size;
  804. config->active_frac = 1;
  805. } else {
  806. config->active_polarity = params.active_polarity;
  807. config->active_count = params.active_count;
  808. config->active_frac = params.active_frac;
  809. config->tu_size = params.tu_size;
  810. }
  811. dev_dbg(sor->dev,
  812. "polarity: %d active count: %d tu size: %d active frac: %d\n",
  813. config->active_polarity, config->active_count,
  814. config->tu_size, config->active_frac);
  815. watermark = params.ratio * config->tu_size * (f - params.ratio);
  816. watermark = div_u64(watermark, f);
  817. watermark = div_u64(watermark + params.error, f);
  818. config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
  819. num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
  820. (link->num_lanes * 8);
  821. if (config->watermark > 30) {
  822. config->watermark = 30;
  823. dev_err(sor->dev,
  824. "unable to compute TU size, forcing watermark to %u\n",
  825. config->watermark);
  826. } else if (config->watermark > num_syms_per_line) {
  827. config->watermark = num_syms_per_line;
  828. dev_err(sor->dev, "watermark too high, forcing to %u\n",
  829. config->watermark);
  830. }
  831. /* compute the number of symbols per horizontal blanking interval */
  832. num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
  833. config->hblank_symbols = div_u64(num, pclk);
  834. if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
  835. config->hblank_symbols -= 3;
  836. config->hblank_symbols -= 12 / link->num_lanes;
  837. /* compute the number of symbols per vertical blanking interval */
  838. num = (mode->hdisplay - 25) * link_rate;
  839. config->vblank_symbols = div_u64(num, pclk);
  840. config->vblank_symbols -= 36 / link->num_lanes + 4;
  841. dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
  842. config->vblank_symbols);
  843. return 0;
  844. }
  845. static void tegra_sor_apply_config(struct tegra_sor *sor,
  846. const struct tegra_sor_config *config)
  847. {
  848. u32 value;
  849. value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
  850. value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
  851. value |= SOR_DP_LINKCTL_TU_SIZE(config->tu_size);
  852. tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
  853. value = tegra_sor_readl(sor, SOR_DP_CONFIG0);
  854. value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
  855. value |= SOR_DP_CONFIG_WATERMARK(config->watermark);
  856. value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
  857. value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config->active_count);
  858. value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
  859. value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config->active_frac);
  860. if (config->active_polarity)
  861. value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
  862. else
  863. value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
  864. value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
  865. value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
  866. tegra_sor_writel(sor, value, SOR_DP_CONFIG0);
  867. value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
  868. value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
  869. value |= config->hblank_symbols & 0xffff;
  870. tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
  871. value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
  872. value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
  873. value |= config->vblank_symbols & 0xffff;
  874. tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
  875. }
  876. static void tegra_sor_mode_set(struct tegra_sor *sor,
  877. const struct drm_display_mode *mode,
  878. struct tegra_sor_state *state)
  879. {
  880. struct tegra_dc *dc = to_tegra_dc(sor->output.encoder.crtc);
  881. unsigned int vbe, vse, hbe, hse, vbs, hbs;
  882. u32 value;
  883. value = tegra_sor_readl(sor, SOR_STATE1);
  884. value &= ~SOR_STATE_ASY_PIXELDEPTH_MASK;
  885. value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
  886. value &= ~SOR_STATE_ASY_OWNER_MASK;
  887. value |= SOR_STATE_ASY_CRC_MODE_COMPLETE |
  888. SOR_STATE_ASY_OWNER(dc->pipe + 1);
  889. if (mode->flags & DRM_MODE_FLAG_PHSYNC)
  890. value &= ~SOR_STATE_ASY_HSYNCPOL;
  891. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  892. value |= SOR_STATE_ASY_HSYNCPOL;
  893. if (mode->flags & DRM_MODE_FLAG_PVSYNC)
  894. value &= ~SOR_STATE_ASY_VSYNCPOL;
  895. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  896. value |= SOR_STATE_ASY_VSYNCPOL;
  897. switch (state->bpc) {
  898. case 16:
  899. value |= SOR_STATE_ASY_PIXELDEPTH_BPP_48_444;
  900. break;
  901. case 12:
  902. value |= SOR_STATE_ASY_PIXELDEPTH_BPP_36_444;
  903. break;
  904. case 10:
  905. value |= SOR_STATE_ASY_PIXELDEPTH_BPP_30_444;
  906. break;
  907. case 8:
  908. value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
  909. break;
  910. case 6:
  911. value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
  912. break;
  913. default:
  914. value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
  915. break;
  916. }
  917. tegra_sor_writel(sor, value, SOR_STATE1);
  918. /*
  919. * TODO: The video timing programming below doesn't seem to match the
  920. * register definitions.
  921. */
  922. value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
  923. tegra_sor_writel(sor, value, sor->soc->regs->head_state1 + dc->pipe);
  924. /* sync end = sync width - 1 */
  925. vse = mode->vsync_end - mode->vsync_start - 1;
  926. hse = mode->hsync_end - mode->hsync_start - 1;
  927. value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
  928. tegra_sor_writel(sor, value, sor->soc->regs->head_state2 + dc->pipe);
  929. /* blank end = sync end + back porch */
  930. vbe = vse + (mode->vtotal - mode->vsync_end);
  931. hbe = hse + (mode->htotal - mode->hsync_end);
  932. value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
  933. tegra_sor_writel(sor, value, sor->soc->regs->head_state3 + dc->pipe);
  934. /* blank start = blank end + active */
  935. vbs = vbe + mode->vdisplay;
  936. hbs = hbe + mode->hdisplay;
  937. value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
  938. tegra_sor_writel(sor, value, sor->soc->regs->head_state4 + dc->pipe);
  939. /* XXX interlacing support */
  940. tegra_sor_writel(sor, 0x001, sor->soc->regs->head_state5 + dc->pipe);
  941. }
  942. static int tegra_sor_detach(struct tegra_sor *sor)
  943. {
  944. unsigned long value, timeout;
  945. /* switch to safe mode */
  946. value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
  947. value &= ~SOR_SUPER_STATE_MODE_NORMAL;
  948. tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
  949. tegra_sor_super_update(sor);
  950. timeout = jiffies + msecs_to_jiffies(250);
  951. while (time_before(jiffies, timeout)) {
  952. value = tegra_sor_readl(sor, SOR_PWR);
  953. if (value & SOR_PWR_MODE_SAFE)
  954. break;
  955. }
  956. if ((value & SOR_PWR_MODE_SAFE) == 0)
  957. return -ETIMEDOUT;
  958. /* go to sleep */
  959. value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
  960. value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
  961. tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
  962. tegra_sor_super_update(sor);
  963. /* detach */
  964. value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
  965. value &= ~SOR_SUPER_STATE_ATTACHED;
  966. tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
  967. tegra_sor_super_update(sor);
  968. timeout = jiffies + msecs_to_jiffies(250);
  969. while (time_before(jiffies, timeout)) {
  970. value = tegra_sor_readl(sor, SOR_TEST);
  971. if ((value & SOR_TEST_ATTACHED) == 0)
  972. break;
  973. usleep_range(25, 100);
  974. }
  975. if ((value & SOR_TEST_ATTACHED) != 0)
  976. return -ETIMEDOUT;
  977. return 0;
  978. }
  979. static int tegra_sor_power_down(struct tegra_sor *sor)
  980. {
  981. unsigned long value, timeout;
  982. int err;
  983. value = tegra_sor_readl(sor, SOR_PWR);
  984. value &= ~SOR_PWR_NORMAL_STATE_PU;
  985. value |= SOR_PWR_TRIGGER;
  986. tegra_sor_writel(sor, value, SOR_PWR);
  987. timeout = jiffies + msecs_to_jiffies(250);
  988. while (time_before(jiffies, timeout)) {
  989. value = tegra_sor_readl(sor, SOR_PWR);
  990. if ((value & SOR_PWR_TRIGGER) == 0)
  991. return 0;
  992. usleep_range(25, 100);
  993. }
  994. if ((value & SOR_PWR_TRIGGER) != 0)
  995. return -ETIMEDOUT;
  996. /* switch to safe parent clock */
  997. err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
  998. if (err < 0) {
  999. dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
  1000. return err;
  1001. }
  1002. value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
  1003. value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
  1004. SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
  1005. tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
  1006. /* stop lane sequencer */
  1007. value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
  1008. SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
  1009. tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
  1010. timeout = jiffies + msecs_to_jiffies(250);
  1011. while (time_before(jiffies, timeout)) {
  1012. value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
  1013. if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
  1014. break;
  1015. usleep_range(25, 100);
  1016. }
  1017. if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
  1018. return -ETIMEDOUT;
  1019. value = tegra_sor_readl(sor, sor->soc->regs->pll2);
  1020. value |= SOR_PLL2_PORT_POWERDOWN;
  1021. tegra_sor_writel(sor, value, sor->soc->regs->pll2);
  1022. usleep_range(20, 100);
  1023. value = tegra_sor_readl(sor, sor->soc->regs->pll0);
  1024. value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
  1025. tegra_sor_writel(sor, value, sor->soc->regs->pll0);
  1026. value = tegra_sor_readl(sor, sor->soc->regs->pll2);
  1027. value |= SOR_PLL2_SEQ_PLLCAPPD;
  1028. value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
  1029. tegra_sor_writel(sor, value, sor->soc->regs->pll2);
  1030. usleep_range(20, 100);
  1031. return 0;
  1032. }
  1033. static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
  1034. {
  1035. u32 value;
  1036. timeout = jiffies + msecs_to_jiffies(timeout);
  1037. while (time_before(jiffies, timeout)) {
  1038. value = tegra_sor_readl(sor, SOR_CRCA);
  1039. if (value & SOR_CRCA_VALID)
  1040. return 0;
  1041. usleep_range(100, 200);
  1042. }
  1043. return -ETIMEDOUT;
  1044. }
  1045. static int tegra_sor_show_crc(struct seq_file *s, void *data)
  1046. {
  1047. struct drm_info_node *node = s->private;
  1048. struct tegra_sor *sor = node->info_ent->data;
  1049. struct drm_crtc *crtc = sor->output.encoder.crtc;
  1050. struct drm_device *drm = node->minor->dev;
  1051. int err = 0;
  1052. u32 value;
  1053. drm_modeset_lock_all(drm);
  1054. if (!crtc || !crtc->state->active) {
  1055. err = -EBUSY;
  1056. goto unlock;
  1057. }
  1058. value = tegra_sor_readl(sor, SOR_STATE1);
  1059. value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
  1060. tegra_sor_writel(sor, value, SOR_STATE1);
  1061. value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
  1062. value |= SOR_CRC_CNTRL_ENABLE;
  1063. tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
  1064. value = tegra_sor_readl(sor, SOR_TEST);
  1065. value &= ~SOR_TEST_CRC_POST_SERIALIZE;
  1066. tegra_sor_writel(sor, value, SOR_TEST);
  1067. err = tegra_sor_crc_wait(sor, 100);
  1068. if (err < 0)
  1069. goto unlock;
  1070. tegra_sor_writel(sor, SOR_CRCA_RESET, SOR_CRCA);
  1071. value = tegra_sor_readl(sor, SOR_CRCB);
  1072. seq_printf(s, "%08x\n", value);
  1073. unlock:
  1074. drm_modeset_unlock_all(drm);
  1075. return err;
  1076. }
  1077. #define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
  1078. static const struct debugfs_reg32 tegra_sor_regs[] = {
  1079. DEBUGFS_REG32(SOR_CTXSW),
  1080. DEBUGFS_REG32(SOR_SUPER_STATE0),
  1081. DEBUGFS_REG32(SOR_SUPER_STATE1),
  1082. DEBUGFS_REG32(SOR_STATE0),
  1083. DEBUGFS_REG32(SOR_STATE1),
  1084. DEBUGFS_REG32(SOR_HEAD_STATE0(0)),
  1085. DEBUGFS_REG32(SOR_HEAD_STATE0(1)),
  1086. DEBUGFS_REG32(SOR_HEAD_STATE1(0)),
  1087. DEBUGFS_REG32(SOR_HEAD_STATE1(1)),
  1088. DEBUGFS_REG32(SOR_HEAD_STATE2(0)),
  1089. DEBUGFS_REG32(SOR_HEAD_STATE2(1)),
  1090. DEBUGFS_REG32(SOR_HEAD_STATE3(0)),
  1091. DEBUGFS_REG32(SOR_HEAD_STATE3(1)),
  1092. DEBUGFS_REG32(SOR_HEAD_STATE4(0)),
  1093. DEBUGFS_REG32(SOR_HEAD_STATE4(1)),
  1094. DEBUGFS_REG32(SOR_HEAD_STATE5(0)),
  1095. DEBUGFS_REG32(SOR_HEAD_STATE5(1)),
  1096. DEBUGFS_REG32(SOR_CRC_CNTRL),
  1097. DEBUGFS_REG32(SOR_DP_DEBUG_MVID),
  1098. DEBUGFS_REG32(SOR_CLK_CNTRL),
  1099. DEBUGFS_REG32(SOR_CAP),
  1100. DEBUGFS_REG32(SOR_PWR),
  1101. DEBUGFS_REG32(SOR_TEST),
  1102. DEBUGFS_REG32(SOR_PLL0),
  1103. DEBUGFS_REG32(SOR_PLL1),
  1104. DEBUGFS_REG32(SOR_PLL2),
  1105. DEBUGFS_REG32(SOR_PLL3),
  1106. DEBUGFS_REG32(SOR_CSTM),
  1107. DEBUGFS_REG32(SOR_LVDS),
  1108. DEBUGFS_REG32(SOR_CRCA),
  1109. DEBUGFS_REG32(SOR_CRCB),
  1110. DEBUGFS_REG32(SOR_BLANK),
  1111. DEBUGFS_REG32(SOR_SEQ_CTL),
  1112. DEBUGFS_REG32(SOR_LANE_SEQ_CTL),
  1113. DEBUGFS_REG32(SOR_SEQ_INST(0)),
  1114. DEBUGFS_REG32(SOR_SEQ_INST(1)),
  1115. DEBUGFS_REG32(SOR_SEQ_INST(2)),
  1116. DEBUGFS_REG32(SOR_SEQ_INST(3)),
  1117. DEBUGFS_REG32(SOR_SEQ_INST(4)),
  1118. DEBUGFS_REG32(SOR_SEQ_INST(5)),
  1119. DEBUGFS_REG32(SOR_SEQ_INST(6)),
  1120. DEBUGFS_REG32(SOR_SEQ_INST(7)),
  1121. DEBUGFS_REG32(SOR_SEQ_INST(8)),
  1122. DEBUGFS_REG32(SOR_SEQ_INST(9)),
  1123. DEBUGFS_REG32(SOR_SEQ_INST(10)),
  1124. DEBUGFS_REG32(SOR_SEQ_INST(11)),
  1125. DEBUGFS_REG32(SOR_SEQ_INST(12)),
  1126. DEBUGFS_REG32(SOR_SEQ_INST(13)),
  1127. DEBUGFS_REG32(SOR_SEQ_INST(14)),
  1128. DEBUGFS_REG32(SOR_SEQ_INST(15)),
  1129. DEBUGFS_REG32(SOR_PWM_DIV),
  1130. DEBUGFS_REG32(SOR_PWM_CTL),
  1131. DEBUGFS_REG32(SOR_VCRC_A0),
  1132. DEBUGFS_REG32(SOR_VCRC_A1),
  1133. DEBUGFS_REG32(SOR_VCRC_B0),
  1134. DEBUGFS_REG32(SOR_VCRC_B1),
  1135. DEBUGFS_REG32(SOR_CCRC_A0),
  1136. DEBUGFS_REG32(SOR_CCRC_A1),
  1137. DEBUGFS_REG32(SOR_CCRC_B0),
  1138. DEBUGFS_REG32(SOR_CCRC_B1),
  1139. DEBUGFS_REG32(SOR_EDATA_A0),
  1140. DEBUGFS_REG32(SOR_EDATA_A1),
  1141. DEBUGFS_REG32(SOR_EDATA_B0),
  1142. DEBUGFS_REG32(SOR_EDATA_B1),
  1143. DEBUGFS_REG32(SOR_COUNT_A0),
  1144. DEBUGFS_REG32(SOR_COUNT_A1),
  1145. DEBUGFS_REG32(SOR_COUNT_B0),
  1146. DEBUGFS_REG32(SOR_COUNT_B1),
  1147. DEBUGFS_REG32(SOR_DEBUG_A0),
  1148. DEBUGFS_REG32(SOR_DEBUG_A1),
  1149. DEBUGFS_REG32(SOR_DEBUG_B0),
  1150. DEBUGFS_REG32(SOR_DEBUG_B1),
  1151. DEBUGFS_REG32(SOR_TRIG),
  1152. DEBUGFS_REG32(SOR_MSCHECK),
  1153. DEBUGFS_REG32(SOR_XBAR_CTRL),
  1154. DEBUGFS_REG32(SOR_XBAR_POL),
  1155. DEBUGFS_REG32(SOR_DP_LINKCTL0),
  1156. DEBUGFS_REG32(SOR_DP_LINKCTL1),
  1157. DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT0),
  1158. DEBUGFS_REG32(SOR_LANE_DRIVE_CURRENT1),
  1159. DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT0),
  1160. DEBUGFS_REG32(SOR_LANE4_DRIVE_CURRENT1),
  1161. DEBUGFS_REG32(SOR_LANE_PREEMPHASIS0),
  1162. DEBUGFS_REG32(SOR_LANE_PREEMPHASIS1),
  1163. DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS0),
  1164. DEBUGFS_REG32(SOR_LANE4_PREEMPHASIS1),
  1165. DEBUGFS_REG32(SOR_LANE_POSTCURSOR0),
  1166. DEBUGFS_REG32(SOR_LANE_POSTCURSOR1),
  1167. DEBUGFS_REG32(SOR_DP_CONFIG0),
  1168. DEBUGFS_REG32(SOR_DP_CONFIG1),
  1169. DEBUGFS_REG32(SOR_DP_MN0),
  1170. DEBUGFS_REG32(SOR_DP_MN1),
  1171. DEBUGFS_REG32(SOR_DP_PADCTL0),
  1172. DEBUGFS_REG32(SOR_DP_PADCTL1),
  1173. DEBUGFS_REG32(SOR_DP_PADCTL2),
  1174. DEBUGFS_REG32(SOR_DP_DEBUG0),
  1175. DEBUGFS_REG32(SOR_DP_DEBUG1),
  1176. DEBUGFS_REG32(SOR_DP_SPARE0),
  1177. DEBUGFS_REG32(SOR_DP_SPARE1),
  1178. DEBUGFS_REG32(SOR_DP_AUDIO_CTRL),
  1179. DEBUGFS_REG32(SOR_DP_AUDIO_HBLANK_SYMBOLS),
  1180. DEBUGFS_REG32(SOR_DP_AUDIO_VBLANK_SYMBOLS),
  1181. DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_HEADER),
  1182. DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK0),
  1183. DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK1),
  1184. DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK2),
  1185. DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK3),
  1186. DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK4),
  1187. DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK5),
  1188. DEBUGFS_REG32(SOR_DP_GENERIC_INFOFRAME_SUBPACK6),
  1189. DEBUGFS_REG32(SOR_DP_TPG),
  1190. DEBUGFS_REG32(SOR_DP_TPG_CONFIG),
  1191. DEBUGFS_REG32(SOR_DP_LQ_CSTM0),
  1192. DEBUGFS_REG32(SOR_DP_LQ_CSTM1),
  1193. DEBUGFS_REG32(SOR_DP_LQ_CSTM2),
  1194. };
  1195. static int tegra_sor_show_regs(struct seq_file *s, void *data)
  1196. {
  1197. struct drm_info_node *node = s->private;
  1198. struct tegra_sor *sor = node->info_ent->data;
  1199. struct drm_crtc *crtc = sor->output.encoder.crtc;
  1200. struct drm_device *drm = node->minor->dev;
  1201. unsigned int i;
  1202. int err = 0;
  1203. drm_modeset_lock_all(drm);
  1204. if (!crtc || !crtc->state->active) {
  1205. err = -EBUSY;
  1206. goto unlock;
  1207. }
  1208. for (i = 0; i < ARRAY_SIZE(tegra_sor_regs); i++) {
  1209. unsigned int offset = tegra_sor_regs[i].offset;
  1210. seq_printf(s, "%-38s %#05x %08x\n", tegra_sor_regs[i].name,
  1211. offset, tegra_sor_readl(sor, offset));
  1212. }
  1213. unlock:
  1214. drm_modeset_unlock_all(drm);
  1215. return err;
  1216. }
  1217. static const struct drm_info_list debugfs_files[] = {
  1218. { "crc", tegra_sor_show_crc, 0, NULL },
  1219. { "regs", tegra_sor_show_regs, 0, NULL },
  1220. };
  1221. static int tegra_sor_late_register(struct drm_connector *connector)
  1222. {
  1223. struct tegra_output *output = connector_to_output(connector);
  1224. unsigned int i, count = ARRAY_SIZE(debugfs_files);
  1225. struct drm_minor *minor = connector->dev->primary;
  1226. struct dentry *root = connector->debugfs_entry;
  1227. struct tegra_sor *sor = to_sor(output);
  1228. int err;
  1229. sor->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
  1230. GFP_KERNEL);
  1231. if (!sor->debugfs_files)
  1232. return -ENOMEM;
  1233. for (i = 0; i < count; i++)
  1234. sor->debugfs_files[i].data = sor;
  1235. err = drm_debugfs_create_files(sor->debugfs_files, count, root, minor);
  1236. if (err < 0)
  1237. goto free;
  1238. return 0;
  1239. free:
  1240. kfree(sor->debugfs_files);
  1241. sor->debugfs_files = NULL;
  1242. return err;
  1243. }
  1244. static void tegra_sor_early_unregister(struct drm_connector *connector)
  1245. {
  1246. struct tegra_output *output = connector_to_output(connector);
  1247. unsigned int count = ARRAY_SIZE(debugfs_files);
  1248. struct tegra_sor *sor = to_sor(output);
  1249. drm_debugfs_remove_files(sor->debugfs_files, count,
  1250. connector->dev->primary);
  1251. kfree(sor->debugfs_files);
  1252. sor->debugfs_files = NULL;
  1253. }
  1254. static void tegra_sor_connector_reset(struct drm_connector *connector)
  1255. {
  1256. struct tegra_sor_state *state;
  1257. state = kzalloc(sizeof(*state), GFP_KERNEL);
  1258. if (!state)
  1259. return;
  1260. if (connector->state) {
  1261. __drm_atomic_helper_connector_destroy_state(connector->state);
  1262. kfree(connector->state);
  1263. }
  1264. __drm_atomic_helper_connector_reset(connector, &state->base);
  1265. }
  1266. static enum drm_connector_status
  1267. tegra_sor_connector_detect(struct drm_connector *connector, bool force)
  1268. {
  1269. struct tegra_output *output = connector_to_output(connector);
  1270. struct tegra_sor *sor = to_sor(output);
  1271. if (sor->aux)
  1272. return drm_dp_aux_detect(sor->aux);
  1273. return tegra_output_connector_detect(connector, force);
  1274. }
  1275. static struct drm_connector_state *
  1276. tegra_sor_connector_duplicate_state(struct drm_connector *connector)
  1277. {
  1278. struct tegra_sor_state *state = to_sor_state(connector->state);
  1279. struct tegra_sor_state *copy;
  1280. copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
  1281. if (!copy)
  1282. return NULL;
  1283. __drm_atomic_helper_connector_duplicate_state(connector, &copy->base);
  1284. return &copy->base;
  1285. }
  1286. static const struct drm_connector_funcs tegra_sor_connector_funcs = {
  1287. .reset = tegra_sor_connector_reset,
  1288. .detect = tegra_sor_connector_detect,
  1289. .fill_modes = drm_helper_probe_single_connector_modes,
  1290. .destroy = tegra_output_connector_destroy,
  1291. .atomic_duplicate_state = tegra_sor_connector_duplicate_state,
  1292. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  1293. .late_register = tegra_sor_late_register,
  1294. .early_unregister = tegra_sor_early_unregister,
  1295. };
  1296. static int tegra_sor_connector_get_modes(struct drm_connector *connector)
  1297. {
  1298. struct tegra_output *output = connector_to_output(connector);
  1299. struct tegra_sor *sor = to_sor(output);
  1300. int err;
  1301. if (sor->aux)
  1302. drm_dp_aux_enable(sor->aux);
  1303. err = tegra_output_connector_get_modes(connector);
  1304. if (sor->aux)
  1305. drm_dp_aux_disable(sor->aux);
  1306. return err;
  1307. }
  1308. static enum drm_mode_status
  1309. tegra_sor_connector_mode_valid(struct drm_connector *connector,
  1310. struct drm_display_mode *mode)
  1311. {
  1312. return MODE_OK;
  1313. }
  1314. static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
  1315. .get_modes = tegra_sor_connector_get_modes,
  1316. .mode_valid = tegra_sor_connector_mode_valid,
  1317. };
  1318. static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
  1319. .destroy = tegra_output_encoder_destroy,
  1320. };
  1321. static void tegra_sor_edp_disable(struct drm_encoder *encoder)
  1322. {
  1323. struct tegra_output *output = encoder_to_output(encoder);
  1324. struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
  1325. struct tegra_sor *sor = to_sor(output);
  1326. u32 value;
  1327. int err;
  1328. if (output->panel)
  1329. drm_panel_disable(output->panel);
  1330. err = tegra_sor_detach(sor);
  1331. if (err < 0)
  1332. dev_err(sor->dev, "failed to detach SOR: %d\n", err);
  1333. tegra_sor_writel(sor, 0, SOR_STATE1);
  1334. tegra_sor_update(sor);
  1335. /*
  1336. * The following accesses registers of the display controller, so make
  1337. * sure it's only executed when the output is attached to one.
  1338. */
  1339. if (dc) {
  1340. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  1341. value &= ~SOR_ENABLE(0);
  1342. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  1343. tegra_dc_commit(dc);
  1344. }
  1345. err = tegra_sor_power_down(sor);
  1346. if (err < 0)
  1347. dev_err(sor->dev, "failed to power down SOR: %d\n", err);
  1348. if (sor->aux) {
  1349. err = drm_dp_aux_disable(sor->aux);
  1350. if (err < 0)
  1351. dev_err(sor->dev, "failed to disable DP: %d\n", err);
  1352. }
  1353. err = tegra_io_pad_power_disable(sor->pad);
  1354. if (err < 0)
  1355. dev_err(sor->dev, "failed to power off I/O pad: %d\n", err);
  1356. if (output->panel)
  1357. drm_panel_unprepare(output->panel);
  1358. pm_runtime_put(sor->dev);
  1359. }
  1360. #if 0
  1361. static int calc_h_ref_to_sync(const struct drm_display_mode *mode,
  1362. unsigned int *value)
  1363. {
  1364. unsigned int hfp, hsw, hbp, a = 0, b;
  1365. hfp = mode->hsync_start - mode->hdisplay;
  1366. hsw = mode->hsync_end - mode->hsync_start;
  1367. hbp = mode->htotal - mode->hsync_end;
  1368. pr_info("hfp: %u, hsw: %u, hbp: %u\n", hfp, hsw, hbp);
  1369. b = hfp - 1;
  1370. pr_info("a: %u, b: %u\n", a, b);
  1371. pr_info("a + hsw + hbp = %u\n", a + hsw + hbp);
  1372. if (a + hsw + hbp <= 11) {
  1373. a = 1 + 11 - hsw - hbp;
  1374. pr_info("a: %u\n", a);
  1375. }
  1376. if (a > b)
  1377. return -EINVAL;
  1378. if (hsw < 1)
  1379. return -EINVAL;
  1380. if (mode->hdisplay < 16)
  1381. return -EINVAL;
  1382. if (value) {
  1383. if (b > a && a % 2)
  1384. *value = a + 1;
  1385. else
  1386. *value = a;
  1387. }
  1388. return 0;
  1389. }
  1390. #endif
  1391. static void tegra_sor_edp_enable(struct drm_encoder *encoder)
  1392. {
  1393. struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
  1394. struct tegra_output *output = encoder_to_output(encoder);
  1395. struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
  1396. struct tegra_sor *sor = to_sor(output);
  1397. struct tegra_sor_config config;
  1398. struct tegra_sor_state *state;
  1399. struct drm_dp_link link;
  1400. u8 rate, lanes;
  1401. unsigned int i;
  1402. int err = 0;
  1403. u32 value;
  1404. state = to_sor_state(output->connector.state);
  1405. pm_runtime_get_sync(sor->dev);
  1406. if (output->panel)
  1407. drm_panel_prepare(output->panel);
  1408. err = drm_dp_aux_enable(sor->aux);
  1409. if (err < 0)
  1410. dev_err(sor->dev, "failed to enable DP: %d\n", err);
  1411. err = drm_dp_link_probe(sor->aux, &link);
  1412. if (err < 0) {
  1413. dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
  1414. return;
  1415. }
  1416. /* switch to safe parent clock */
  1417. err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
  1418. if (err < 0)
  1419. dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
  1420. memset(&config, 0, sizeof(config));
  1421. config.bits_per_pixel = state->bpc * 3;
  1422. err = tegra_sor_compute_config(sor, mode, &config, &link);
  1423. if (err < 0)
  1424. dev_err(sor->dev, "failed to compute configuration: %d\n", err);
  1425. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  1426. value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
  1427. value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
  1428. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  1429. value = tegra_sor_readl(sor, sor->soc->regs->pll2);
  1430. value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
  1431. tegra_sor_writel(sor, value, sor->soc->regs->pll2);
  1432. usleep_range(20, 100);
  1433. value = tegra_sor_readl(sor, sor->soc->regs->pll3);
  1434. value |= SOR_PLL3_PLL_VDD_MODE_3V3;
  1435. tegra_sor_writel(sor, value, sor->soc->regs->pll3);
  1436. value = SOR_PLL0_ICHPMP(0xf) | SOR_PLL0_VCOCAP_RST |
  1437. SOR_PLL0_PLLREG_LEVEL_V45 | SOR_PLL0_RESISTOR_EXT;
  1438. tegra_sor_writel(sor, value, sor->soc->regs->pll0);
  1439. value = tegra_sor_readl(sor, sor->soc->regs->pll2);
  1440. value |= SOR_PLL2_SEQ_PLLCAPPD;
  1441. value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
  1442. value |= SOR_PLL2_LVDS_ENABLE;
  1443. tegra_sor_writel(sor, value, sor->soc->regs->pll2);
  1444. value = SOR_PLL1_TERM_COMPOUT | SOR_PLL1_TMDS_TERM;
  1445. tegra_sor_writel(sor, value, sor->soc->regs->pll1);
  1446. while (true) {
  1447. value = tegra_sor_readl(sor, sor->soc->regs->pll2);
  1448. if ((value & SOR_PLL2_SEQ_PLLCAPPD_ENFORCE) == 0)
  1449. break;
  1450. usleep_range(250, 1000);
  1451. }
  1452. value = tegra_sor_readl(sor, sor->soc->regs->pll2);
  1453. value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
  1454. value &= ~SOR_PLL2_PORT_POWERDOWN;
  1455. tegra_sor_writel(sor, value, sor->soc->regs->pll2);
  1456. /*
  1457. * power up
  1458. */
  1459. /* set safe link bandwidth (1.62 Gbps) */
  1460. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  1461. value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
  1462. value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
  1463. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  1464. /* step 1 */
  1465. value = tegra_sor_readl(sor, sor->soc->regs->pll2);
  1466. value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL2_PORT_POWERDOWN |
  1467. SOR_PLL2_BANDGAP_POWERDOWN;
  1468. tegra_sor_writel(sor, value, sor->soc->regs->pll2);
  1469. value = tegra_sor_readl(sor, sor->soc->regs->pll0);
  1470. value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
  1471. tegra_sor_writel(sor, value, sor->soc->regs->pll0);
  1472. value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
  1473. value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
  1474. tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
  1475. /* step 2 */
  1476. err = tegra_io_pad_power_enable(sor->pad);
  1477. if (err < 0)
  1478. dev_err(sor->dev, "failed to power on I/O pad: %d\n", err);
  1479. usleep_range(5, 100);
  1480. /* step 3 */
  1481. value = tegra_sor_readl(sor, sor->soc->regs->pll2);
  1482. value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
  1483. tegra_sor_writel(sor, value, sor->soc->regs->pll2);
  1484. usleep_range(20, 100);
  1485. /* step 4 */
  1486. value = tegra_sor_readl(sor, sor->soc->regs->pll0);
  1487. value &= ~SOR_PLL0_VCOPD;
  1488. value &= ~SOR_PLL0_PWR;
  1489. tegra_sor_writel(sor, value, sor->soc->regs->pll0);
  1490. value = tegra_sor_readl(sor, sor->soc->regs->pll2);
  1491. value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
  1492. tegra_sor_writel(sor, value, sor->soc->regs->pll2);
  1493. usleep_range(200, 1000);
  1494. /* step 5 */
  1495. value = tegra_sor_readl(sor, sor->soc->regs->pll2);
  1496. value &= ~SOR_PLL2_PORT_POWERDOWN;
  1497. tegra_sor_writel(sor, value, sor->soc->regs->pll2);
  1498. /* XXX not in TRM */
  1499. for (value = 0, i = 0; i < 5; i++)
  1500. value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->soc->xbar_cfg[i]) |
  1501. SOR_XBAR_CTRL_LINK1_XSEL(i, i);
  1502. tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
  1503. tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
  1504. /* switch to DP parent clock */
  1505. err = tegra_sor_set_parent_clock(sor, sor->clk_dp);
  1506. if (err < 0)
  1507. dev_err(sor->dev, "failed to set parent clock: %d\n", err);
  1508. /* power DP lanes */
  1509. value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
  1510. if (link.num_lanes <= 2)
  1511. value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2);
  1512. else
  1513. value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2;
  1514. if (link.num_lanes <= 1)
  1515. value &= ~SOR_DP_PADCTL_PD_TXD_1;
  1516. else
  1517. value |= SOR_DP_PADCTL_PD_TXD_1;
  1518. if (link.num_lanes == 0)
  1519. value &= ~SOR_DP_PADCTL_PD_TXD_0;
  1520. else
  1521. value |= SOR_DP_PADCTL_PD_TXD_0;
  1522. tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
  1523. value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
  1524. value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
  1525. value |= SOR_DP_LINKCTL_LANE_COUNT(link.num_lanes);
  1526. tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
  1527. /* start lane sequencer */
  1528. value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
  1529. SOR_LANE_SEQ_CTL_POWER_STATE_UP;
  1530. tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
  1531. while (true) {
  1532. value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
  1533. if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
  1534. break;
  1535. usleep_range(250, 1000);
  1536. }
  1537. /* set link bandwidth */
  1538. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  1539. value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
  1540. value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
  1541. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  1542. tegra_sor_apply_config(sor, &config);
  1543. /* enable link */
  1544. value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
  1545. value |= SOR_DP_LINKCTL_ENABLE;
  1546. value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
  1547. tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
  1548. for (i = 0, value = 0; i < 4; i++) {
  1549. unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
  1550. SOR_DP_TPG_SCRAMBLER_GALIOS |
  1551. SOR_DP_TPG_PATTERN_NONE;
  1552. value = (value << 8) | lane;
  1553. }
  1554. tegra_sor_writel(sor, value, SOR_DP_TPG);
  1555. /* enable pad calibration logic */
  1556. value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
  1557. value |= SOR_DP_PADCTL_PAD_CAL_PD;
  1558. tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
  1559. err = drm_dp_link_probe(sor->aux, &link);
  1560. if (err < 0)
  1561. dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
  1562. err = drm_dp_link_power_up(sor->aux, &link);
  1563. if (err < 0)
  1564. dev_err(sor->dev, "failed to power up eDP link: %d\n", err);
  1565. err = drm_dp_link_configure(sor->aux, &link);
  1566. if (err < 0)
  1567. dev_err(sor->dev, "failed to configure eDP link: %d\n", err);
  1568. rate = drm_dp_link_rate_to_bw_code(link.rate);
  1569. lanes = link.num_lanes;
  1570. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  1571. value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
  1572. value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
  1573. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  1574. value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
  1575. value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
  1576. value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
  1577. if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
  1578. value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
  1579. tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
  1580. /* disable training pattern generator */
  1581. for (i = 0; i < link.num_lanes; i++) {
  1582. unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
  1583. SOR_DP_TPG_SCRAMBLER_GALIOS |
  1584. SOR_DP_TPG_PATTERN_NONE;
  1585. value = (value << 8) | lane;
  1586. }
  1587. tegra_sor_writel(sor, value, SOR_DP_TPG);
  1588. err = tegra_sor_dp_train_fast(sor, &link);
  1589. if (err < 0)
  1590. dev_err(sor->dev, "DP fast link training failed: %d\n", err);
  1591. dev_dbg(sor->dev, "fast link training succeeded\n");
  1592. err = tegra_sor_power_up(sor, 250);
  1593. if (err < 0)
  1594. dev_err(sor->dev, "failed to power up SOR: %d\n", err);
  1595. /* CSTM (LVDS, link A/B, upper) */
  1596. value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
  1597. SOR_CSTM_UPPER;
  1598. tegra_sor_writel(sor, value, SOR_CSTM);
  1599. /* use DP-A protocol */
  1600. value = tegra_sor_readl(sor, SOR_STATE1);
  1601. value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
  1602. value |= SOR_STATE_ASY_PROTOCOL_DP_A;
  1603. tegra_sor_writel(sor, value, SOR_STATE1);
  1604. tegra_sor_mode_set(sor, mode, state);
  1605. /* PWM setup */
  1606. err = tegra_sor_setup_pwm(sor, 250);
  1607. if (err < 0)
  1608. dev_err(sor->dev, "failed to setup PWM: %d\n", err);
  1609. tegra_sor_update(sor);
  1610. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  1611. value |= SOR_ENABLE(0);
  1612. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  1613. tegra_dc_commit(dc);
  1614. err = tegra_sor_attach(sor);
  1615. if (err < 0)
  1616. dev_err(sor->dev, "failed to attach SOR: %d\n", err);
  1617. err = tegra_sor_wakeup(sor);
  1618. if (err < 0)
  1619. dev_err(sor->dev, "failed to enable DC: %d\n", err);
  1620. if (output->panel)
  1621. drm_panel_enable(output->panel);
  1622. }
  1623. static int
  1624. tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
  1625. struct drm_crtc_state *crtc_state,
  1626. struct drm_connector_state *conn_state)
  1627. {
  1628. struct tegra_output *output = encoder_to_output(encoder);
  1629. struct tegra_sor_state *state = to_sor_state(conn_state);
  1630. struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
  1631. unsigned long pclk = crtc_state->mode.clock * 1000;
  1632. struct tegra_sor *sor = to_sor(output);
  1633. struct drm_display_info *info;
  1634. int err;
  1635. info = &output->connector.display_info;
  1636. /*
  1637. * For HBR2 modes, the SOR brick needs to use the x20 multiplier, so
  1638. * the pixel clock must be corrected accordingly.
  1639. */
  1640. if (pclk >= 340000000) {
  1641. state->link_speed = 20;
  1642. state->pclk = pclk / 2;
  1643. } else {
  1644. state->link_speed = 10;
  1645. state->pclk = pclk;
  1646. }
  1647. err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
  1648. pclk, 0);
  1649. if (err < 0) {
  1650. dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
  1651. return err;
  1652. }
  1653. switch (info->bpc) {
  1654. case 8:
  1655. case 6:
  1656. state->bpc = info->bpc;
  1657. break;
  1658. default:
  1659. DRM_DEBUG_KMS("%u bits-per-color not supported\n", info->bpc);
  1660. state->bpc = 8;
  1661. break;
  1662. }
  1663. return 0;
  1664. }
  1665. static const struct drm_encoder_helper_funcs tegra_sor_edp_helpers = {
  1666. .disable = tegra_sor_edp_disable,
  1667. .enable = tegra_sor_edp_enable,
  1668. .atomic_check = tegra_sor_encoder_atomic_check,
  1669. };
  1670. static inline u32 tegra_sor_hdmi_subpack(const u8 *ptr, size_t size)
  1671. {
  1672. u32 value = 0;
  1673. size_t i;
  1674. for (i = size; i > 0; i--)
  1675. value = (value << 8) | ptr[i - 1];
  1676. return value;
  1677. }
  1678. static void tegra_sor_hdmi_write_infopack(struct tegra_sor *sor,
  1679. const void *data, size_t size)
  1680. {
  1681. const u8 *ptr = data;
  1682. unsigned long offset;
  1683. size_t i, j;
  1684. u32 value;
  1685. switch (ptr[0]) {
  1686. case HDMI_INFOFRAME_TYPE_AVI:
  1687. offset = SOR_HDMI_AVI_INFOFRAME_HEADER;
  1688. break;
  1689. case HDMI_INFOFRAME_TYPE_AUDIO:
  1690. offset = SOR_HDMI_AUDIO_INFOFRAME_HEADER;
  1691. break;
  1692. case HDMI_INFOFRAME_TYPE_VENDOR:
  1693. offset = SOR_HDMI_VSI_INFOFRAME_HEADER;
  1694. break;
  1695. default:
  1696. dev_err(sor->dev, "unsupported infoframe type: %02x\n",
  1697. ptr[0]);
  1698. return;
  1699. }
  1700. value = INFOFRAME_HEADER_TYPE(ptr[0]) |
  1701. INFOFRAME_HEADER_VERSION(ptr[1]) |
  1702. INFOFRAME_HEADER_LEN(ptr[2]);
  1703. tegra_sor_writel(sor, value, offset);
  1704. offset++;
  1705. /*
  1706. * Each subpack contains 7 bytes, divided into:
  1707. * - subpack_low: bytes 0 - 3
  1708. * - subpack_high: bytes 4 - 6 (with byte 7 padded to 0x00)
  1709. */
  1710. for (i = 3, j = 0; i < size; i += 7, j += 8) {
  1711. size_t rem = size - i, num = min_t(size_t, rem, 4);
  1712. value = tegra_sor_hdmi_subpack(&ptr[i], num);
  1713. tegra_sor_writel(sor, value, offset++);
  1714. num = min_t(size_t, rem - num, 3);
  1715. value = tegra_sor_hdmi_subpack(&ptr[i + 4], num);
  1716. tegra_sor_writel(sor, value, offset++);
  1717. }
  1718. }
  1719. static int
  1720. tegra_sor_hdmi_setup_avi_infoframe(struct tegra_sor *sor,
  1721. const struct drm_display_mode *mode)
  1722. {
  1723. u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
  1724. struct hdmi_avi_infoframe frame;
  1725. u32 value;
  1726. int err;
  1727. /* disable AVI infoframe */
  1728. value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
  1729. value &= ~INFOFRAME_CTRL_SINGLE;
  1730. value &= ~INFOFRAME_CTRL_OTHER;
  1731. value &= ~INFOFRAME_CTRL_ENABLE;
  1732. tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
  1733. err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode, false);
  1734. if (err < 0) {
  1735. dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
  1736. return err;
  1737. }
  1738. err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
  1739. if (err < 0) {
  1740. dev_err(sor->dev, "failed to pack AVI infoframe: %d\n", err);
  1741. return err;
  1742. }
  1743. tegra_sor_hdmi_write_infopack(sor, buffer, err);
  1744. /* enable AVI infoframe */
  1745. value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
  1746. value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
  1747. value |= INFOFRAME_CTRL_ENABLE;
  1748. tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
  1749. return 0;
  1750. }
  1751. static void tegra_sor_hdmi_disable_audio_infoframe(struct tegra_sor *sor)
  1752. {
  1753. u32 value;
  1754. value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
  1755. value &= ~INFOFRAME_CTRL_ENABLE;
  1756. tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
  1757. }
  1758. static struct tegra_sor_hdmi_settings *
  1759. tegra_sor_hdmi_find_settings(struct tegra_sor *sor, unsigned long frequency)
  1760. {
  1761. unsigned int i;
  1762. for (i = 0; i < sor->num_settings; i++)
  1763. if (frequency <= sor->settings[i].frequency)
  1764. return &sor->settings[i];
  1765. return NULL;
  1766. }
  1767. static void tegra_sor_hdmi_disable_scrambling(struct tegra_sor *sor)
  1768. {
  1769. u32 value;
  1770. value = tegra_sor_readl(sor, SOR_HDMI2_CTRL);
  1771. value &= ~SOR_HDMI2_CTRL_CLOCK_MODE_DIV_BY_4;
  1772. value &= ~SOR_HDMI2_CTRL_SCRAMBLE;
  1773. tegra_sor_writel(sor, value, SOR_HDMI2_CTRL);
  1774. }
  1775. static void tegra_sor_hdmi_scdc_disable(struct tegra_sor *sor)
  1776. {
  1777. struct i2c_adapter *ddc = sor->output.ddc;
  1778. drm_scdc_set_high_tmds_clock_ratio(ddc, false);
  1779. drm_scdc_set_scrambling(ddc, false);
  1780. tegra_sor_hdmi_disable_scrambling(sor);
  1781. }
  1782. static void tegra_sor_hdmi_scdc_stop(struct tegra_sor *sor)
  1783. {
  1784. if (sor->scdc_enabled) {
  1785. cancel_delayed_work_sync(&sor->scdc);
  1786. tegra_sor_hdmi_scdc_disable(sor);
  1787. }
  1788. }
  1789. static void tegra_sor_hdmi_enable_scrambling(struct tegra_sor *sor)
  1790. {
  1791. u32 value;
  1792. value = tegra_sor_readl(sor, SOR_HDMI2_CTRL);
  1793. value |= SOR_HDMI2_CTRL_CLOCK_MODE_DIV_BY_4;
  1794. value |= SOR_HDMI2_CTRL_SCRAMBLE;
  1795. tegra_sor_writel(sor, value, SOR_HDMI2_CTRL);
  1796. }
  1797. static void tegra_sor_hdmi_scdc_enable(struct tegra_sor *sor)
  1798. {
  1799. struct i2c_adapter *ddc = sor->output.ddc;
  1800. drm_scdc_set_high_tmds_clock_ratio(ddc, true);
  1801. drm_scdc_set_scrambling(ddc, true);
  1802. tegra_sor_hdmi_enable_scrambling(sor);
  1803. }
  1804. static void tegra_sor_hdmi_scdc_work(struct work_struct *work)
  1805. {
  1806. struct tegra_sor *sor = container_of(work, struct tegra_sor, scdc.work);
  1807. struct i2c_adapter *ddc = sor->output.ddc;
  1808. if (!drm_scdc_get_scrambling_status(ddc)) {
  1809. DRM_DEBUG_KMS("SCDC not scrambled\n");
  1810. tegra_sor_hdmi_scdc_enable(sor);
  1811. }
  1812. schedule_delayed_work(&sor->scdc, msecs_to_jiffies(5000));
  1813. }
  1814. static void tegra_sor_hdmi_scdc_start(struct tegra_sor *sor)
  1815. {
  1816. struct drm_scdc *scdc = &sor->output.connector.display_info.hdmi.scdc;
  1817. struct drm_display_mode *mode;
  1818. mode = &sor->output.encoder.crtc->state->adjusted_mode;
  1819. if (mode->clock >= 340000 && scdc->supported) {
  1820. schedule_delayed_work(&sor->scdc, msecs_to_jiffies(5000));
  1821. tegra_sor_hdmi_scdc_enable(sor);
  1822. sor->scdc_enabled = true;
  1823. }
  1824. }
  1825. static void tegra_sor_hdmi_disable(struct drm_encoder *encoder)
  1826. {
  1827. struct tegra_output *output = encoder_to_output(encoder);
  1828. struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
  1829. struct tegra_sor *sor = to_sor(output);
  1830. u32 value;
  1831. int err;
  1832. tegra_sor_hdmi_scdc_stop(sor);
  1833. err = tegra_sor_detach(sor);
  1834. if (err < 0)
  1835. dev_err(sor->dev, "failed to detach SOR: %d\n", err);
  1836. tegra_sor_writel(sor, 0, SOR_STATE1);
  1837. tegra_sor_update(sor);
  1838. /* disable display to SOR clock */
  1839. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  1840. if (!sor->soc->has_nvdisplay)
  1841. value &= ~(SOR1_TIMING_CYA | SOR_ENABLE(1));
  1842. else
  1843. value &= ~SOR_ENABLE(sor->index);
  1844. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  1845. tegra_dc_commit(dc);
  1846. err = tegra_sor_power_down(sor);
  1847. if (err < 0)
  1848. dev_err(sor->dev, "failed to power down SOR: %d\n", err);
  1849. err = tegra_io_pad_power_disable(sor->pad);
  1850. if (err < 0)
  1851. dev_err(sor->dev, "failed to power off I/O pad: %d\n", err);
  1852. pm_runtime_put(sor->dev);
  1853. }
  1854. static void tegra_sor_hdmi_enable(struct drm_encoder *encoder)
  1855. {
  1856. struct tegra_output *output = encoder_to_output(encoder);
  1857. unsigned int h_ref_to_sync = 1, pulse_start, max_ac;
  1858. struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
  1859. struct tegra_sor_hdmi_settings *settings;
  1860. struct tegra_sor *sor = to_sor(output);
  1861. struct tegra_sor_state *state;
  1862. struct drm_display_mode *mode;
  1863. unsigned long rate, pclk;
  1864. unsigned int div, i;
  1865. u32 value;
  1866. int err;
  1867. state = to_sor_state(output->connector.state);
  1868. mode = &encoder->crtc->state->adjusted_mode;
  1869. pclk = mode->clock * 1000;
  1870. pm_runtime_get_sync(sor->dev);
  1871. /* switch to safe parent clock */
  1872. err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
  1873. if (err < 0) {
  1874. dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
  1875. return;
  1876. }
  1877. div = clk_get_rate(sor->clk) / 1000000 * 4;
  1878. err = tegra_io_pad_power_enable(sor->pad);
  1879. if (err < 0)
  1880. dev_err(sor->dev, "failed to power on I/O pad: %d\n", err);
  1881. usleep_range(20, 100);
  1882. value = tegra_sor_readl(sor, sor->soc->regs->pll2);
  1883. value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
  1884. tegra_sor_writel(sor, value, sor->soc->regs->pll2);
  1885. usleep_range(20, 100);
  1886. value = tegra_sor_readl(sor, sor->soc->regs->pll3);
  1887. value &= ~SOR_PLL3_PLL_VDD_MODE_3V3;
  1888. tegra_sor_writel(sor, value, sor->soc->regs->pll3);
  1889. value = tegra_sor_readl(sor, sor->soc->regs->pll0);
  1890. value &= ~SOR_PLL0_VCOPD;
  1891. value &= ~SOR_PLL0_PWR;
  1892. tegra_sor_writel(sor, value, sor->soc->regs->pll0);
  1893. value = tegra_sor_readl(sor, sor->soc->regs->pll2);
  1894. value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
  1895. tegra_sor_writel(sor, value, sor->soc->regs->pll2);
  1896. usleep_range(200, 400);
  1897. value = tegra_sor_readl(sor, sor->soc->regs->pll2);
  1898. value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
  1899. value &= ~SOR_PLL2_PORT_POWERDOWN;
  1900. tegra_sor_writel(sor, value, sor->soc->regs->pll2);
  1901. usleep_range(20, 100);
  1902. value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
  1903. value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
  1904. SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2;
  1905. tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
  1906. while (true) {
  1907. value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
  1908. if ((value & SOR_LANE_SEQ_CTL_STATE_BUSY) == 0)
  1909. break;
  1910. usleep_range(250, 1000);
  1911. }
  1912. value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
  1913. SOR_LANE_SEQ_CTL_POWER_STATE_UP | SOR_LANE_SEQ_CTL_DELAY(5);
  1914. tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
  1915. while (true) {
  1916. value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
  1917. if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
  1918. break;
  1919. usleep_range(250, 1000);
  1920. }
  1921. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  1922. value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
  1923. value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
  1924. if (mode->clock < 340000) {
  1925. DRM_DEBUG_KMS("setting 2.7 GHz link speed\n");
  1926. value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G2_70;
  1927. } else {
  1928. DRM_DEBUG_KMS("setting 5.4 GHz link speed\n");
  1929. value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G5_40;
  1930. }
  1931. value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
  1932. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  1933. /* SOR pad PLL stabilization time */
  1934. usleep_range(250, 1000);
  1935. value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
  1936. value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
  1937. value |= SOR_DP_LINKCTL_LANE_COUNT(4);
  1938. tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
  1939. value = tegra_sor_readl(sor, SOR_DP_SPARE0);
  1940. value &= ~SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
  1941. value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
  1942. value &= ~SOR_DP_SPARE_SEQ_ENABLE;
  1943. value &= ~SOR_DP_SPARE_MACRO_SOR_CLK;
  1944. tegra_sor_writel(sor, value, SOR_DP_SPARE0);
  1945. value = SOR_SEQ_CTL_PU_PC(0) | SOR_SEQ_CTL_PU_PC_ALT(0) |
  1946. SOR_SEQ_CTL_PD_PC(8) | SOR_SEQ_CTL_PD_PC_ALT(8);
  1947. tegra_sor_writel(sor, value, SOR_SEQ_CTL);
  1948. value = SOR_SEQ_INST_DRIVE_PWM_OUT_LO | SOR_SEQ_INST_HALT |
  1949. SOR_SEQ_INST_WAIT_VSYNC | SOR_SEQ_INST_WAIT(1);
  1950. tegra_sor_writel(sor, value, SOR_SEQ_INST(0));
  1951. tegra_sor_writel(sor, value, SOR_SEQ_INST(8));
  1952. if (!sor->soc->has_nvdisplay) {
  1953. /* program the reference clock */
  1954. value = SOR_REFCLK_DIV_INT(div) | SOR_REFCLK_DIV_FRAC(div);
  1955. tegra_sor_writel(sor, value, SOR_REFCLK);
  1956. }
  1957. /* XXX not in TRM */
  1958. for (value = 0, i = 0; i < 5; i++)
  1959. value |= SOR_XBAR_CTRL_LINK0_XSEL(i, sor->soc->xbar_cfg[i]) |
  1960. SOR_XBAR_CTRL_LINK1_XSEL(i, i);
  1961. tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
  1962. tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
  1963. /* switch to parent clock */
  1964. err = clk_set_parent(sor->clk, sor->clk_parent);
  1965. if (err < 0) {
  1966. dev_err(sor->dev, "failed to set parent clock: %d\n", err);
  1967. return;
  1968. }
  1969. err = tegra_sor_set_parent_clock(sor, sor->clk_pad);
  1970. if (err < 0) {
  1971. dev_err(sor->dev, "failed to set pad clock: %d\n", err);
  1972. return;
  1973. }
  1974. /* adjust clock rate for HDMI 2.0 modes */
  1975. rate = clk_get_rate(sor->clk_parent);
  1976. if (mode->clock >= 340000)
  1977. rate /= 2;
  1978. DRM_DEBUG_KMS("setting clock to %lu Hz, mode: %lu Hz\n", rate, pclk);
  1979. clk_set_rate(sor->clk, rate);
  1980. if (!sor->soc->has_nvdisplay) {
  1981. value = SOR_INPUT_CONTROL_HDMI_SRC_SELECT(dc->pipe);
  1982. /* XXX is this the proper check? */
  1983. if (mode->clock < 75000)
  1984. value |= SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_LIMITED;
  1985. tegra_sor_writel(sor, value, SOR_INPUT_CONTROL);
  1986. }
  1987. max_ac = ((mode->htotal - mode->hdisplay) - SOR_REKEY - 18) / 32;
  1988. value = SOR_HDMI_CTRL_ENABLE | SOR_HDMI_CTRL_MAX_AC_PACKET(max_ac) |
  1989. SOR_HDMI_CTRL_AUDIO_LAYOUT | SOR_HDMI_CTRL_REKEY(SOR_REKEY);
  1990. tegra_sor_writel(sor, value, SOR_HDMI_CTRL);
  1991. if (!dc->soc->has_nvdisplay) {
  1992. /* H_PULSE2 setup */
  1993. pulse_start = h_ref_to_sync +
  1994. (mode->hsync_end - mode->hsync_start) +
  1995. (mode->htotal - mode->hsync_end) - 10;
  1996. value = PULSE_LAST_END_A | PULSE_QUAL_VACTIVE |
  1997. PULSE_POLARITY_HIGH | PULSE_MODE_NORMAL;
  1998. tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_CONTROL);
  1999. value = PULSE_END(pulse_start + 8) | PULSE_START(pulse_start);
  2000. tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_POSITION_A);
  2001. value = tegra_dc_readl(dc, DC_DISP_DISP_SIGNAL_OPTIONS0);
  2002. value |= H_PULSE2_ENABLE;
  2003. tegra_dc_writel(dc, value, DC_DISP_DISP_SIGNAL_OPTIONS0);
  2004. }
  2005. /* infoframe setup */
  2006. err = tegra_sor_hdmi_setup_avi_infoframe(sor, mode);
  2007. if (err < 0)
  2008. dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
  2009. /* XXX HDMI audio support not implemented yet */
  2010. tegra_sor_hdmi_disable_audio_infoframe(sor);
  2011. /* use single TMDS protocol */
  2012. value = tegra_sor_readl(sor, SOR_STATE1);
  2013. value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
  2014. value |= SOR_STATE_ASY_PROTOCOL_SINGLE_TMDS_A;
  2015. tegra_sor_writel(sor, value, SOR_STATE1);
  2016. /* power up pad calibration */
  2017. value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
  2018. value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
  2019. tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
  2020. /* production settings */
  2021. settings = tegra_sor_hdmi_find_settings(sor, mode->clock * 1000);
  2022. if (!settings) {
  2023. dev_err(sor->dev, "no settings for pixel clock %d Hz\n",
  2024. mode->clock * 1000);
  2025. return;
  2026. }
  2027. value = tegra_sor_readl(sor, sor->soc->regs->pll0);
  2028. value &= ~SOR_PLL0_ICHPMP_MASK;
  2029. value &= ~SOR_PLL0_FILTER_MASK;
  2030. value &= ~SOR_PLL0_VCOCAP_MASK;
  2031. value |= SOR_PLL0_ICHPMP(settings->ichpmp);
  2032. value |= SOR_PLL0_FILTER(settings->filter);
  2033. value |= SOR_PLL0_VCOCAP(settings->vcocap);
  2034. tegra_sor_writel(sor, value, sor->soc->regs->pll0);
  2035. /* XXX not in TRM */
  2036. value = tegra_sor_readl(sor, sor->soc->regs->pll1);
  2037. value &= ~SOR_PLL1_LOADADJ_MASK;
  2038. value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
  2039. value |= SOR_PLL1_LOADADJ(settings->loadadj);
  2040. value |= SOR_PLL1_TMDS_TERMADJ(settings->tmds_termadj);
  2041. value |= SOR_PLL1_TMDS_TERM;
  2042. tegra_sor_writel(sor, value, sor->soc->regs->pll1);
  2043. value = tegra_sor_readl(sor, sor->soc->regs->pll3);
  2044. value &= ~SOR_PLL3_BG_TEMP_COEF_MASK;
  2045. value &= ~SOR_PLL3_BG_VREF_LEVEL_MASK;
  2046. value &= ~SOR_PLL3_AVDD10_LEVEL_MASK;
  2047. value &= ~SOR_PLL3_AVDD14_LEVEL_MASK;
  2048. value |= SOR_PLL3_BG_TEMP_COEF(settings->bg_temp_coef);
  2049. value |= SOR_PLL3_BG_VREF_LEVEL(settings->bg_vref_level);
  2050. value |= SOR_PLL3_AVDD10_LEVEL(settings->avdd10_level);
  2051. value |= SOR_PLL3_AVDD14_LEVEL(settings->avdd14_level);
  2052. tegra_sor_writel(sor, value, sor->soc->regs->pll3);
  2053. value = settings->drive_current[3] << 24 |
  2054. settings->drive_current[2] << 16 |
  2055. settings->drive_current[1] << 8 |
  2056. settings->drive_current[0] << 0;
  2057. tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
  2058. value = settings->preemphasis[3] << 24 |
  2059. settings->preemphasis[2] << 16 |
  2060. settings->preemphasis[1] << 8 |
  2061. settings->preemphasis[0] << 0;
  2062. tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
  2063. value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
  2064. value &= ~SOR_DP_PADCTL_TX_PU_MASK;
  2065. value |= SOR_DP_PADCTL_TX_PU_ENABLE;
  2066. value |= SOR_DP_PADCTL_TX_PU(settings->tx_pu_value);
  2067. tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
  2068. value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl2);
  2069. value &= ~SOR_DP_PADCTL_SPAREPLL_MASK;
  2070. value |= SOR_DP_PADCTL_SPAREPLL(settings->sparepll);
  2071. tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl2);
  2072. /* power down pad calibration */
  2073. value = tegra_sor_readl(sor, sor->soc->regs->dp_padctl0);
  2074. value |= SOR_DP_PADCTL_PAD_CAL_PD;
  2075. tegra_sor_writel(sor, value, sor->soc->regs->dp_padctl0);
  2076. if (!dc->soc->has_nvdisplay) {
  2077. /* miscellaneous display controller settings */
  2078. value = VSYNC_H_POSITION(1);
  2079. tegra_dc_writel(dc, value, DC_DISP_DISP_TIMING_OPTIONS);
  2080. }
  2081. value = tegra_dc_readl(dc, DC_DISP_DISP_COLOR_CONTROL);
  2082. value &= ~DITHER_CONTROL_MASK;
  2083. value &= ~BASE_COLOR_SIZE_MASK;
  2084. switch (state->bpc) {
  2085. case 6:
  2086. value |= BASE_COLOR_SIZE_666;
  2087. break;
  2088. case 8:
  2089. value |= BASE_COLOR_SIZE_888;
  2090. break;
  2091. case 10:
  2092. value |= BASE_COLOR_SIZE_101010;
  2093. break;
  2094. case 12:
  2095. value |= BASE_COLOR_SIZE_121212;
  2096. break;
  2097. default:
  2098. WARN(1, "%u bits-per-color not supported\n", state->bpc);
  2099. value |= BASE_COLOR_SIZE_888;
  2100. break;
  2101. }
  2102. tegra_dc_writel(dc, value, DC_DISP_DISP_COLOR_CONTROL);
  2103. /* XXX set display head owner */
  2104. value = tegra_sor_readl(sor, SOR_STATE1);
  2105. value &= ~SOR_STATE_ASY_OWNER_MASK;
  2106. value |= SOR_STATE_ASY_OWNER(1 + dc->pipe);
  2107. tegra_sor_writel(sor, value, SOR_STATE1);
  2108. err = tegra_sor_power_up(sor, 250);
  2109. if (err < 0)
  2110. dev_err(sor->dev, "failed to power up SOR: %d\n", err);
  2111. /* configure dynamic range of output */
  2112. value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
  2113. value &= ~SOR_HEAD_STATE_RANGECOMPRESS_MASK;
  2114. value &= ~SOR_HEAD_STATE_DYNRANGE_MASK;
  2115. tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
  2116. /* configure colorspace */
  2117. value = tegra_sor_readl(sor, sor->soc->regs->head_state0 + dc->pipe);
  2118. value &= ~SOR_HEAD_STATE_COLORSPACE_MASK;
  2119. value |= SOR_HEAD_STATE_COLORSPACE_RGB;
  2120. tegra_sor_writel(sor, value, sor->soc->regs->head_state0 + dc->pipe);
  2121. tegra_sor_mode_set(sor, mode, state);
  2122. tegra_sor_update(sor);
  2123. /* program preamble timing in SOR (XXX) */
  2124. value = tegra_sor_readl(sor, SOR_DP_SPARE0);
  2125. value &= ~SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
  2126. tegra_sor_writel(sor, value, SOR_DP_SPARE0);
  2127. err = tegra_sor_attach(sor);
  2128. if (err < 0)
  2129. dev_err(sor->dev, "failed to attach SOR: %d\n", err);
  2130. /* enable display to SOR clock and generate HDMI preamble */
  2131. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  2132. if (!sor->soc->has_nvdisplay)
  2133. value |= SOR_ENABLE(1) | SOR1_TIMING_CYA;
  2134. else
  2135. value |= SOR_ENABLE(sor->index);
  2136. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  2137. if (dc->soc->has_nvdisplay) {
  2138. value = tegra_dc_readl(dc, DC_DISP_CORE_SOR_SET_CONTROL(sor->index));
  2139. value &= ~PROTOCOL_MASK;
  2140. value |= PROTOCOL_SINGLE_TMDS_A;
  2141. tegra_dc_writel(dc, value, DC_DISP_CORE_SOR_SET_CONTROL(sor->index));
  2142. }
  2143. tegra_dc_commit(dc);
  2144. err = tegra_sor_wakeup(sor);
  2145. if (err < 0)
  2146. dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
  2147. tegra_sor_hdmi_scdc_start(sor);
  2148. }
  2149. static const struct drm_encoder_helper_funcs tegra_sor_hdmi_helpers = {
  2150. .disable = tegra_sor_hdmi_disable,
  2151. .enable = tegra_sor_hdmi_enable,
  2152. .atomic_check = tegra_sor_encoder_atomic_check,
  2153. };
  2154. static int tegra_sor_init(struct host1x_client *client)
  2155. {
  2156. struct drm_device *drm = dev_get_drvdata(client->parent);
  2157. const struct drm_encoder_helper_funcs *helpers = NULL;
  2158. struct tegra_sor *sor = host1x_client_to_sor(client);
  2159. int connector = DRM_MODE_CONNECTOR_Unknown;
  2160. int encoder = DRM_MODE_ENCODER_NONE;
  2161. int err;
  2162. if (!sor->aux) {
  2163. if (sor->soc->supports_hdmi) {
  2164. connector = DRM_MODE_CONNECTOR_HDMIA;
  2165. encoder = DRM_MODE_ENCODER_TMDS;
  2166. helpers = &tegra_sor_hdmi_helpers;
  2167. } else if (sor->soc->supports_lvds) {
  2168. connector = DRM_MODE_CONNECTOR_LVDS;
  2169. encoder = DRM_MODE_ENCODER_LVDS;
  2170. }
  2171. } else {
  2172. if (sor->soc->supports_edp) {
  2173. connector = DRM_MODE_CONNECTOR_eDP;
  2174. encoder = DRM_MODE_ENCODER_TMDS;
  2175. helpers = &tegra_sor_edp_helpers;
  2176. } else if (sor->soc->supports_dp) {
  2177. connector = DRM_MODE_CONNECTOR_DisplayPort;
  2178. encoder = DRM_MODE_ENCODER_TMDS;
  2179. }
  2180. }
  2181. sor->output.dev = sor->dev;
  2182. drm_connector_init(drm, &sor->output.connector,
  2183. &tegra_sor_connector_funcs,
  2184. connector);
  2185. drm_connector_helper_add(&sor->output.connector,
  2186. &tegra_sor_connector_helper_funcs);
  2187. sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
  2188. drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
  2189. encoder, NULL);
  2190. drm_encoder_helper_add(&sor->output.encoder, helpers);
  2191. drm_connector_attach_encoder(&sor->output.connector,
  2192. &sor->output.encoder);
  2193. drm_connector_register(&sor->output.connector);
  2194. err = tegra_output_init(drm, &sor->output);
  2195. if (err < 0) {
  2196. dev_err(client->dev, "failed to initialize output: %d\n", err);
  2197. return err;
  2198. }
  2199. tegra_output_find_possible_crtcs(&sor->output, drm);
  2200. if (sor->aux) {
  2201. err = drm_dp_aux_attach(sor->aux, &sor->output);
  2202. if (err < 0) {
  2203. dev_err(sor->dev, "failed to attach DP: %d\n", err);
  2204. return err;
  2205. }
  2206. }
  2207. /*
  2208. * XXX: Remove this reset once proper hand-over from firmware to
  2209. * kernel is possible.
  2210. */
  2211. if (sor->rst) {
  2212. err = reset_control_assert(sor->rst);
  2213. if (err < 0) {
  2214. dev_err(sor->dev, "failed to assert SOR reset: %d\n",
  2215. err);
  2216. return err;
  2217. }
  2218. }
  2219. err = clk_prepare_enable(sor->clk);
  2220. if (err < 0) {
  2221. dev_err(sor->dev, "failed to enable clock: %d\n", err);
  2222. return err;
  2223. }
  2224. usleep_range(1000, 3000);
  2225. if (sor->rst) {
  2226. err = reset_control_deassert(sor->rst);
  2227. if (err < 0) {
  2228. dev_err(sor->dev, "failed to deassert SOR reset: %d\n",
  2229. err);
  2230. return err;
  2231. }
  2232. }
  2233. err = clk_prepare_enable(sor->clk_safe);
  2234. if (err < 0)
  2235. return err;
  2236. err = clk_prepare_enable(sor->clk_dp);
  2237. if (err < 0)
  2238. return err;
  2239. return 0;
  2240. }
  2241. static int tegra_sor_exit(struct host1x_client *client)
  2242. {
  2243. struct tegra_sor *sor = host1x_client_to_sor(client);
  2244. int err;
  2245. tegra_output_exit(&sor->output);
  2246. if (sor->aux) {
  2247. err = drm_dp_aux_detach(sor->aux);
  2248. if (err < 0) {
  2249. dev_err(sor->dev, "failed to detach DP: %d\n", err);
  2250. return err;
  2251. }
  2252. }
  2253. clk_disable_unprepare(sor->clk_safe);
  2254. clk_disable_unprepare(sor->clk_dp);
  2255. clk_disable_unprepare(sor->clk);
  2256. return 0;
  2257. }
  2258. static const struct host1x_client_ops sor_client_ops = {
  2259. .init = tegra_sor_init,
  2260. .exit = tegra_sor_exit,
  2261. };
  2262. static const struct tegra_sor_ops tegra_sor_edp_ops = {
  2263. .name = "eDP",
  2264. };
  2265. static int tegra_sor_hdmi_probe(struct tegra_sor *sor)
  2266. {
  2267. int err;
  2268. sor->avdd_io_supply = devm_regulator_get(sor->dev, "avdd-io");
  2269. if (IS_ERR(sor->avdd_io_supply)) {
  2270. dev_err(sor->dev, "cannot get AVDD I/O supply: %ld\n",
  2271. PTR_ERR(sor->avdd_io_supply));
  2272. return PTR_ERR(sor->avdd_io_supply);
  2273. }
  2274. err = regulator_enable(sor->avdd_io_supply);
  2275. if (err < 0) {
  2276. dev_err(sor->dev, "failed to enable AVDD I/O supply: %d\n",
  2277. err);
  2278. return err;
  2279. }
  2280. sor->vdd_pll_supply = devm_regulator_get(sor->dev, "vdd-pll");
  2281. if (IS_ERR(sor->vdd_pll_supply)) {
  2282. dev_err(sor->dev, "cannot get VDD PLL supply: %ld\n",
  2283. PTR_ERR(sor->vdd_pll_supply));
  2284. return PTR_ERR(sor->vdd_pll_supply);
  2285. }
  2286. err = regulator_enable(sor->vdd_pll_supply);
  2287. if (err < 0) {
  2288. dev_err(sor->dev, "failed to enable VDD PLL supply: %d\n",
  2289. err);
  2290. return err;
  2291. }
  2292. sor->hdmi_supply = devm_regulator_get(sor->dev, "hdmi");
  2293. if (IS_ERR(sor->hdmi_supply)) {
  2294. dev_err(sor->dev, "cannot get HDMI supply: %ld\n",
  2295. PTR_ERR(sor->hdmi_supply));
  2296. return PTR_ERR(sor->hdmi_supply);
  2297. }
  2298. err = regulator_enable(sor->hdmi_supply);
  2299. if (err < 0) {
  2300. dev_err(sor->dev, "failed to enable HDMI supply: %d\n", err);
  2301. return err;
  2302. }
  2303. INIT_DELAYED_WORK(&sor->scdc, tegra_sor_hdmi_scdc_work);
  2304. return 0;
  2305. }
  2306. static int tegra_sor_hdmi_remove(struct tegra_sor *sor)
  2307. {
  2308. regulator_disable(sor->hdmi_supply);
  2309. regulator_disable(sor->vdd_pll_supply);
  2310. regulator_disable(sor->avdd_io_supply);
  2311. return 0;
  2312. }
  2313. static const struct tegra_sor_ops tegra_sor_hdmi_ops = {
  2314. .name = "HDMI",
  2315. .probe = tegra_sor_hdmi_probe,
  2316. .remove = tegra_sor_hdmi_remove,
  2317. };
  2318. static const u8 tegra124_sor_xbar_cfg[5] = {
  2319. 0, 1, 2, 3, 4
  2320. };
  2321. static const struct tegra_sor_regs tegra124_sor_regs = {
  2322. .head_state0 = 0x05,
  2323. .head_state1 = 0x07,
  2324. .head_state2 = 0x09,
  2325. .head_state3 = 0x0b,
  2326. .head_state4 = 0x0d,
  2327. .head_state5 = 0x0f,
  2328. .pll0 = 0x17,
  2329. .pll1 = 0x18,
  2330. .pll2 = 0x19,
  2331. .pll3 = 0x1a,
  2332. .dp_padctl0 = 0x5c,
  2333. .dp_padctl2 = 0x73,
  2334. };
  2335. static const struct tegra_sor_soc tegra124_sor = {
  2336. .supports_edp = true,
  2337. .supports_lvds = true,
  2338. .supports_hdmi = false,
  2339. .supports_dp = false,
  2340. .regs = &tegra124_sor_regs,
  2341. .has_nvdisplay = false,
  2342. .xbar_cfg = tegra124_sor_xbar_cfg,
  2343. };
  2344. static const struct tegra_sor_regs tegra210_sor_regs = {
  2345. .head_state0 = 0x05,
  2346. .head_state1 = 0x07,
  2347. .head_state2 = 0x09,
  2348. .head_state3 = 0x0b,
  2349. .head_state4 = 0x0d,
  2350. .head_state5 = 0x0f,
  2351. .pll0 = 0x17,
  2352. .pll1 = 0x18,
  2353. .pll2 = 0x19,
  2354. .pll3 = 0x1a,
  2355. .dp_padctl0 = 0x5c,
  2356. .dp_padctl2 = 0x73,
  2357. };
  2358. static const struct tegra_sor_soc tegra210_sor = {
  2359. .supports_edp = true,
  2360. .supports_lvds = false,
  2361. .supports_hdmi = false,
  2362. .supports_dp = false,
  2363. .regs = &tegra210_sor_regs,
  2364. .has_nvdisplay = false,
  2365. .xbar_cfg = tegra124_sor_xbar_cfg,
  2366. };
  2367. static const u8 tegra210_sor_xbar_cfg[5] = {
  2368. 2, 1, 0, 3, 4
  2369. };
  2370. static const struct tegra_sor_soc tegra210_sor1 = {
  2371. .supports_edp = false,
  2372. .supports_lvds = false,
  2373. .supports_hdmi = true,
  2374. .supports_dp = true,
  2375. .regs = &tegra210_sor_regs,
  2376. .has_nvdisplay = false,
  2377. .num_settings = ARRAY_SIZE(tegra210_sor_hdmi_defaults),
  2378. .settings = tegra210_sor_hdmi_defaults,
  2379. .xbar_cfg = tegra210_sor_xbar_cfg,
  2380. };
  2381. static const struct tegra_sor_regs tegra186_sor_regs = {
  2382. .head_state0 = 0x151,
  2383. .head_state1 = 0x154,
  2384. .head_state2 = 0x157,
  2385. .head_state3 = 0x15a,
  2386. .head_state4 = 0x15d,
  2387. .head_state5 = 0x160,
  2388. .pll0 = 0x163,
  2389. .pll1 = 0x164,
  2390. .pll2 = 0x165,
  2391. .pll3 = 0x166,
  2392. .dp_padctl0 = 0x168,
  2393. .dp_padctl2 = 0x16a,
  2394. };
  2395. static const struct tegra_sor_soc tegra186_sor = {
  2396. .supports_edp = false,
  2397. .supports_lvds = false,
  2398. .supports_hdmi = false,
  2399. .supports_dp = true,
  2400. .regs = &tegra186_sor_regs,
  2401. .has_nvdisplay = true,
  2402. .xbar_cfg = tegra124_sor_xbar_cfg,
  2403. };
  2404. static const struct tegra_sor_soc tegra186_sor1 = {
  2405. .supports_edp = false,
  2406. .supports_lvds = false,
  2407. .supports_hdmi = true,
  2408. .supports_dp = true,
  2409. .regs = &tegra186_sor_regs,
  2410. .has_nvdisplay = true,
  2411. .num_settings = ARRAY_SIZE(tegra186_sor_hdmi_defaults),
  2412. .settings = tegra186_sor_hdmi_defaults,
  2413. .xbar_cfg = tegra124_sor_xbar_cfg,
  2414. };
  2415. static const struct tegra_sor_regs tegra194_sor_regs = {
  2416. .head_state0 = 0x151,
  2417. .head_state1 = 0x155,
  2418. .head_state2 = 0x159,
  2419. .head_state3 = 0x15d,
  2420. .head_state4 = 0x161,
  2421. .head_state5 = 0x165,
  2422. .pll0 = 0x169,
  2423. .pll1 = 0x16a,
  2424. .pll2 = 0x16b,
  2425. .pll3 = 0x16c,
  2426. .dp_padctl0 = 0x16e,
  2427. .dp_padctl2 = 0x16f,
  2428. };
  2429. static const struct tegra_sor_soc tegra194_sor = {
  2430. .supports_edp = true,
  2431. .supports_lvds = false,
  2432. .supports_hdmi = true,
  2433. .supports_dp = true,
  2434. .regs = &tegra194_sor_regs,
  2435. .has_nvdisplay = true,
  2436. .num_settings = ARRAY_SIZE(tegra194_sor_hdmi_defaults),
  2437. .settings = tegra194_sor_hdmi_defaults,
  2438. .xbar_cfg = tegra210_sor_xbar_cfg,
  2439. };
  2440. static const struct of_device_id tegra_sor_of_match[] = {
  2441. { .compatible = "nvidia,tegra194-sor", .data = &tegra194_sor },
  2442. { .compatible = "nvidia,tegra186-sor1", .data = &tegra186_sor1 },
  2443. { .compatible = "nvidia,tegra186-sor", .data = &tegra186_sor },
  2444. { .compatible = "nvidia,tegra210-sor1", .data = &tegra210_sor1 },
  2445. { .compatible = "nvidia,tegra210-sor", .data = &tegra210_sor },
  2446. { .compatible = "nvidia,tegra124-sor", .data = &tegra124_sor },
  2447. { },
  2448. };
  2449. MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
  2450. static int tegra_sor_parse_dt(struct tegra_sor *sor)
  2451. {
  2452. struct device_node *np = sor->dev->of_node;
  2453. u32 value;
  2454. int err;
  2455. if (sor->soc->has_nvdisplay) {
  2456. err = of_property_read_u32(np, "nvidia,interface", &value);
  2457. if (err < 0)
  2458. return err;
  2459. sor->index = value;
  2460. /*
  2461. * override the default that we already set for Tegra210 and
  2462. * earlier
  2463. */
  2464. sor->pad = TEGRA_IO_PAD_HDMI_DP0 + sor->index;
  2465. }
  2466. return 0;
  2467. }
  2468. static int tegra_sor_probe(struct platform_device *pdev)
  2469. {
  2470. struct device_node *np;
  2471. struct tegra_sor *sor;
  2472. struct resource *regs;
  2473. int err;
  2474. sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
  2475. if (!sor)
  2476. return -ENOMEM;
  2477. sor->soc = of_device_get_match_data(&pdev->dev);
  2478. sor->output.dev = sor->dev = &pdev->dev;
  2479. sor->settings = devm_kmemdup(&pdev->dev, sor->soc->settings,
  2480. sor->soc->num_settings *
  2481. sizeof(*sor->settings),
  2482. GFP_KERNEL);
  2483. if (!sor->settings)
  2484. return -ENOMEM;
  2485. sor->num_settings = sor->soc->num_settings;
  2486. np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
  2487. if (np) {
  2488. sor->aux = drm_dp_aux_find_by_of_node(np);
  2489. of_node_put(np);
  2490. if (!sor->aux)
  2491. return -EPROBE_DEFER;
  2492. }
  2493. if (!sor->aux) {
  2494. if (sor->soc->supports_hdmi) {
  2495. sor->ops = &tegra_sor_hdmi_ops;
  2496. sor->pad = TEGRA_IO_PAD_HDMI;
  2497. } else if (sor->soc->supports_lvds) {
  2498. dev_err(&pdev->dev, "LVDS not supported yet\n");
  2499. return -ENODEV;
  2500. } else {
  2501. dev_err(&pdev->dev, "unknown (non-DP) support\n");
  2502. return -ENODEV;
  2503. }
  2504. } else {
  2505. if (sor->soc->supports_edp) {
  2506. sor->ops = &tegra_sor_edp_ops;
  2507. sor->pad = TEGRA_IO_PAD_LVDS;
  2508. } else if (sor->soc->supports_dp) {
  2509. dev_err(&pdev->dev, "DisplayPort not supported yet\n");
  2510. return -ENODEV;
  2511. } else {
  2512. dev_err(&pdev->dev, "unknown (DP) support\n");
  2513. return -ENODEV;
  2514. }
  2515. }
  2516. err = tegra_sor_parse_dt(sor);
  2517. if (err < 0)
  2518. return err;
  2519. err = tegra_output_probe(&sor->output);
  2520. if (err < 0) {
  2521. dev_err(&pdev->dev, "failed to probe output: %d\n", err);
  2522. return err;
  2523. }
  2524. if (sor->ops && sor->ops->probe) {
  2525. err = sor->ops->probe(sor);
  2526. if (err < 0) {
  2527. dev_err(&pdev->dev, "failed to probe %s: %d\n",
  2528. sor->ops->name, err);
  2529. goto output;
  2530. }
  2531. }
  2532. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2533. sor->regs = devm_ioremap_resource(&pdev->dev, regs);
  2534. if (IS_ERR(sor->regs)) {
  2535. err = PTR_ERR(sor->regs);
  2536. goto remove;
  2537. }
  2538. if (!pdev->dev.pm_domain) {
  2539. sor->rst = devm_reset_control_get(&pdev->dev, "sor");
  2540. if (IS_ERR(sor->rst)) {
  2541. err = PTR_ERR(sor->rst);
  2542. dev_err(&pdev->dev, "failed to get reset control: %d\n",
  2543. err);
  2544. goto remove;
  2545. }
  2546. }
  2547. sor->clk = devm_clk_get(&pdev->dev, NULL);
  2548. if (IS_ERR(sor->clk)) {
  2549. err = PTR_ERR(sor->clk);
  2550. dev_err(&pdev->dev, "failed to get module clock: %d\n", err);
  2551. goto remove;
  2552. }
  2553. if (sor->soc->supports_hdmi || sor->soc->supports_dp) {
  2554. struct device_node *np = pdev->dev.of_node;
  2555. const char *name;
  2556. /*
  2557. * For backwards compatibility with Tegra210 device trees,
  2558. * fall back to the old clock name "source" if the new "out"
  2559. * clock is not available.
  2560. */
  2561. if (of_property_match_string(np, "clock-names", "out") < 0)
  2562. name = "source";
  2563. else
  2564. name = "out";
  2565. sor->clk_out = devm_clk_get(&pdev->dev, name);
  2566. if (IS_ERR(sor->clk_out)) {
  2567. err = PTR_ERR(sor->clk_out);
  2568. dev_err(sor->dev, "failed to get %s clock: %d\n",
  2569. name, err);
  2570. goto remove;
  2571. }
  2572. } else {
  2573. /* fall back to the module clock on SOR0 (eDP/LVDS only) */
  2574. sor->clk_out = sor->clk;
  2575. }
  2576. sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
  2577. if (IS_ERR(sor->clk_parent)) {
  2578. err = PTR_ERR(sor->clk_parent);
  2579. dev_err(&pdev->dev, "failed to get parent clock: %d\n", err);
  2580. goto remove;
  2581. }
  2582. sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
  2583. if (IS_ERR(sor->clk_safe)) {
  2584. err = PTR_ERR(sor->clk_safe);
  2585. dev_err(&pdev->dev, "failed to get safe clock: %d\n", err);
  2586. goto remove;
  2587. }
  2588. sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
  2589. if (IS_ERR(sor->clk_dp)) {
  2590. err = PTR_ERR(sor->clk_dp);
  2591. dev_err(&pdev->dev, "failed to get DP clock: %d\n", err);
  2592. goto remove;
  2593. }
  2594. /*
  2595. * Starting with Tegra186, the BPMP provides an implementation for
  2596. * the pad output clock, so we have to look it up from device tree.
  2597. */
  2598. sor->clk_pad = devm_clk_get(&pdev->dev, "pad");
  2599. if (IS_ERR(sor->clk_pad)) {
  2600. if (sor->clk_pad != ERR_PTR(-ENOENT)) {
  2601. err = PTR_ERR(sor->clk_pad);
  2602. goto remove;
  2603. }
  2604. /*
  2605. * If the pad output clock is not available, then we assume
  2606. * we're on Tegra210 or earlier and have to provide our own
  2607. * implementation.
  2608. */
  2609. sor->clk_pad = NULL;
  2610. }
  2611. /*
  2612. * The bootloader may have set up the SOR such that it's module clock
  2613. * is sourced by one of the display PLLs. However, that doesn't work
  2614. * without properly having set up other bits of the SOR.
  2615. */
  2616. err = clk_set_parent(sor->clk_out, sor->clk_safe);
  2617. if (err < 0) {
  2618. dev_err(&pdev->dev, "failed to use safe clock: %d\n", err);
  2619. goto remove;
  2620. }
  2621. platform_set_drvdata(pdev, sor);
  2622. pm_runtime_enable(&pdev->dev);
  2623. /*
  2624. * On Tegra210 and earlier, provide our own implementation for the
  2625. * pad output clock.
  2626. */
  2627. if (!sor->clk_pad) {
  2628. err = pm_runtime_get_sync(&pdev->dev);
  2629. if (err < 0) {
  2630. dev_err(&pdev->dev, "failed to get runtime PM: %d\n",
  2631. err);
  2632. goto remove;
  2633. }
  2634. sor->clk_pad = tegra_clk_sor_pad_register(sor,
  2635. "sor1_pad_clkout");
  2636. pm_runtime_put(&pdev->dev);
  2637. }
  2638. if (IS_ERR(sor->clk_pad)) {
  2639. err = PTR_ERR(sor->clk_pad);
  2640. dev_err(&pdev->dev, "failed to register SOR pad clock: %d\n",
  2641. err);
  2642. goto remove;
  2643. }
  2644. INIT_LIST_HEAD(&sor->client.list);
  2645. sor->client.ops = &sor_client_ops;
  2646. sor->client.dev = &pdev->dev;
  2647. err = host1x_client_register(&sor->client);
  2648. if (err < 0) {
  2649. dev_err(&pdev->dev, "failed to register host1x client: %d\n",
  2650. err);
  2651. goto remove;
  2652. }
  2653. return 0;
  2654. remove:
  2655. if (sor->ops && sor->ops->remove)
  2656. sor->ops->remove(sor);
  2657. output:
  2658. tegra_output_remove(&sor->output);
  2659. return err;
  2660. }
  2661. static int tegra_sor_remove(struct platform_device *pdev)
  2662. {
  2663. struct tegra_sor *sor = platform_get_drvdata(pdev);
  2664. int err;
  2665. pm_runtime_disable(&pdev->dev);
  2666. err = host1x_client_unregister(&sor->client);
  2667. if (err < 0) {
  2668. dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
  2669. err);
  2670. return err;
  2671. }
  2672. if (sor->ops && sor->ops->remove) {
  2673. err = sor->ops->remove(sor);
  2674. if (err < 0)
  2675. dev_err(&pdev->dev, "failed to remove SOR: %d\n", err);
  2676. }
  2677. tegra_output_remove(&sor->output);
  2678. return 0;
  2679. }
  2680. #ifdef CONFIG_PM
  2681. static int tegra_sor_suspend(struct device *dev)
  2682. {
  2683. struct tegra_sor *sor = dev_get_drvdata(dev);
  2684. int err;
  2685. if (sor->rst) {
  2686. err = reset_control_assert(sor->rst);
  2687. if (err < 0) {
  2688. dev_err(dev, "failed to assert reset: %d\n", err);
  2689. return err;
  2690. }
  2691. }
  2692. usleep_range(1000, 2000);
  2693. clk_disable_unprepare(sor->clk);
  2694. return 0;
  2695. }
  2696. static int tegra_sor_resume(struct device *dev)
  2697. {
  2698. struct tegra_sor *sor = dev_get_drvdata(dev);
  2699. int err;
  2700. err = clk_prepare_enable(sor->clk);
  2701. if (err < 0) {
  2702. dev_err(dev, "failed to enable clock: %d\n", err);
  2703. return err;
  2704. }
  2705. usleep_range(1000, 2000);
  2706. if (sor->rst) {
  2707. err = reset_control_deassert(sor->rst);
  2708. if (err < 0) {
  2709. dev_err(dev, "failed to deassert reset: %d\n", err);
  2710. clk_disable_unprepare(sor->clk);
  2711. return err;
  2712. }
  2713. }
  2714. return 0;
  2715. }
  2716. #endif
  2717. static const struct dev_pm_ops tegra_sor_pm_ops = {
  2718. SET_RUNTIME_PM_OPS(tegra_sor_suspend, tegra_sor_resume, NULL)
  2719. };
  2720. struct platform_driver tegra_sor_driver = {
  2721. .driver = {
  2722. .name = "tegra-sor",
  2723. .of_match_table = tegra_sor_of_match,
  2724. .pm = &tegra_sor_pm_ops,
  2725. },
  2726. .probe = tegra_sor_probe,
  2727. .remove = tegra_sor_remove,
  2728. };