sor.c 81 KB

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