sor.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546
  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/debugfs.h>
  10. #include <linux/gpio.h>
  11. #include <linux/io.h>
  12. #include <linux/of_device.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/regulator/consumer.h>
  15. #include <linux/reset.h>
  16. #include <soc/tegra/pmc.h>
  17. #include <drm/drm_atomic_helper.h>
  18. #include <drm/drm_dp_helper.h>
  19. #include <drm/drm_panel.h>
  20. #include "dc.h"
  21. #include "drm.h"
  22. #include "sor.h"
  23. #define SOR_REKEY 0x38
  24. struct tegra_sor_hdmi_settings {
  25. unsigned long frequency;
  26. u8 vcocap;
  27. u8 ichpmp;
  28. u8 loadadj;
  29. u8 termadj;
  30. u8 tx_pu;
  31. u8 bg_vref;
  32. u8 drive_current[4];
  33. u8 preemphasis[4];
  34. };
  35. #if 1
  36. static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
  37. {
  38. .frequency = 54000000,
  39. .vcocap = 0x0,
  40. .ichpmp = 0x1,
  41. .loadadj = 0x3,
  42. .termadj = 0x9,
  43. .tx_pu = 0x10,
  44. .bg_vref = 0x8,
  45. .drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
  46. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  47. }, {
  48. .frequency = 75000000,
  49. .vcocap = 0x3,
  50. .ichpmp = 0x1,
  51. .loadadj = 0x3,
  52. .termadj = 0x9,
  53. .tx_pu = 0x40,
  54. .bg_vref = 0x8,
  55. .drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
  56. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  57. }, {
  58. .frequency = 150000000,
  59. .vcocap = 0x3,
  60. .ichpmp = 0x1,
  61. .loadadj = 0x3,
  62. .termadj = 0x9,
  63. .tx_pu = 0x66,
  64. .bg_vref = 0x8,
  65. .drive_current = { 0x33, 0x3a, 0x3a, 0x3a },
  66. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  67. }, {
  68. .frequency = 300000000,
  69. .vcocap = 0x3,
  70. .ichpmp = 0x1,
  71. .loadadj = 0x3,
  72. .termadj = 0x9,
  73. .tx_pu = 0x66,
  74. .bg_vref = 0xa,
  75. .drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
  76. .preemphasis = { 0x00, 0x17, 0x17, 0x17 },
  77. }, {
  78. .frequency = 600000000,
  79. .vcocap = 0x3,
  80. .ichpmp = 0x1,
  81. .loadadj = 0x3,
  82. .termadj = 0x9,
  83. .tx_pu = 0x66,
  84. .bg_vref = 0x8,
  85. .drive_current = { 0x33, 0x3f, 0x3f, 0x3f },
  86. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  87. },
  88. };
  89. #else
  90. static const struct tegra_sor_hdmi_settings tegra210_sor_hdmi_defaults[] = {
  91. {
  92. .frequency = 75000000,
  93. .vcocap = 0x3,
  94. .ichpmp = 0x1,
  95. .loadadj = 0x3,
  96. .termadj = 0x9,
  97. .tx_pu = 0x40,
  98. .bg_vref = 0x8,
  99. .drive_current = { 0x29, 0x29, 0x29, 0x29 },
  100. .preemphasis = { 0x00, 0x00, 0x00, 0x00 },
  101. }, {
  102. .frequency = 150000000,
  103. .vcocap = 0x3,
  104. .ichpmp = 0x1,
  105. .loadadj = 0x3,
  106. .termadj = 0x9,
  107. .tx_pu = 0x66,
  108. .bg_vref = 0x8,
  109. .drive_current = { 0x30, 0x37, 0x37, 0x37 },
  110. .preemphasis = { 0x01, 0x02, 0x02, 0x02 },
  111. }, {
  112. .frequency = 300000000,
  113. .vcocap = 0x3,
  114. .ichpmp = 0x6,
  115. .loadadj = 0x3,
  116. .termadj = 0x9,
  117. .tx_pu = 0x66,
  118. .bg_vref = 0xf,
  119. .drive_current = { 0x30, 0x37, 0x37, 0x37 },
  120. .preemphasis = { 0x10, 0x3e, 0x3e, 0x3e },
  121. }, {
  122. .frequency = 600000000,
  123. .vcocap = 0x3,
  124. .ichpmp = 0xa,
  125. .loadadj = 0x3,
  126. .termadj = 0xb,
  127. .tx_pu = 0x66,
  128. .bg_vref = 0xe,
  129. .drive_current = { 0x35, 0x3e, 0x3e, 0x3e },
  130. .preemphasis = { 0x02, 0x3f, 0x3f, 0x3f },
  131. },
  132. };
  133. #endif
  134. struct tegra_sor_soc {
  135. bool supports_edp;
  136. bool supports_lvds;
  137. bool supports_hdmi;
  138. bool supports_dp;
  139. const struct tegra_sor_hdmi_settings *settings;
  140. unsigned int num_settings;
  141. };
  142. struct tegra_sor;
  143. struct tegra_sor_ops {
  144. const char *name;
  145. int (*probe)(struct tegra_sor *sor);
  146. int (*remove)(struct tegra_sor *sor);
  147. };
  148. struct tegra_sor {
  149. struct host1x_client client;
  150. struct tegra_output output;
  151. struct device *dev;
  152. const struct tegra_sor_soc *soc;
  153. void __iomem *regs;
  154. struct reset_control *rst;
  155. struct clk *clk_parent;
  156. struct clk *clk_safe;
  157. struct clk *clk_dp;
  158. struct clk *clk;
  159. struct drm_dp_aux *aux;
  160. struct drm_info_list *debugfs_files;
  161. struct drm_minor *minor;
  162. struct dentry *debugfs;
  163. const struct tegra_sor_ops *ops;
  164. /* for HDMI 2.0 */
  165. struct tegra_sor_hdmi_settings *settings;
  166. unsigned int num_settings;
  167. struct regulator *avdd_io_supply;
  168. struct regulator *vdd_pll_supply;
  169. struct regulator *hdmi_supply;
  170. };
  171. struct tegra_sor_config {
  172. u32 bits_per_pixel;
  173. u32 active_polarity;
  174. u32 active_count;
  175. u32 tu_size;
  176. u32 active_frac;
  177. u32 watermark;
  178. u32 hblank_symbols;
  179. u32 vblank_symbols;
  180. };
  181. static inline struct tegra_sor *
  182. host1x_client_to_sor(struct host1x_client *client)
  183. {
  184. return container_of(client, struct tegra_sor, client);
  185. }
  186. static inline struct tegra_sor *to_sor(struct tegra_output *output)
  187. {
  188. return container_of(output, struct tegra_sor, output);
  189. }
  190. static inline u32 tegra_sor_readl(struct tegra_sor *sor, unsigned long offset)
  191. {
  192. return readl(sor->regs + (offset << 2));
  193. }
  194. static inline void tegra_sor_writel(struct tegra_sor *sor, u32 value,
  195. unsigned long offset)
  196. {
  197. writel(value, sor->regs + (offset << 2));
  198. }
  199. static int tegra_sor_set_parent_clock(struct tegra_sor *sor, struct clk *parent)
  200. {
  201. int err;
  202. clk_disable_unprepare(sor->clk);
  203. err = clk_set_parent(sor->clk, parent);
  204. if (err < 0)
  205. return err;
  206. err = clk_prepare_enable(sor->clk);
  207. if (err < 0)
  208. return err;
  209. return 0;
  210. }
  211. static int tegra_sor_dp_train_fast(struct tegra_sor *sor,
  212. struct drm_dp_link *link)
  213. {
  214. unsigned int i;
  215. u8 pattern;
  216. u32 value;
  217. int err;
  218. /* setup lane parameters */
  219. value = SOR_LANE_DRIVE_CURRENT_LANE3(0x40) |
  220. SOR_LANE_DRIVE_CURRENT_LANE2(0x40) |
  221. SOR_LANE_DRIVE_CURRENT_LANE1(0x40) |
  222. SOR_LANE_DRIVE_CURRENT_LANE0(0x40);
  223. tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
  224. value = SOR_LANE_PREEMPHASIS_LANE3(0x0f) |
  225. SOR_LANE_PREEMPHASIS_LANE2(0x0f) |
  226. SOR_LANE_PREEMPHASIS_LANE1(0x0f) |
  227. SOR_LANE_PREEMPHASIS_LANE0(0x0f);
  228. tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
  229. value = SOR_LANE_POSTCURSOR_LANE3(0x00) |
  230. SOR_LANE_POSTCURSOR_LANE2(0x00) |
  231. SOR_LANE_POSTCURSOR_LANE1(0x00) |
  232. SOR_LANE_POSTCURSOR_LANE0(0x00);
  233. tegra_sor_writel(sor, value, SOR_LANE_POSTCURSOR0);
  234. /* disable LVDS mode */
  235. tegra_sor_writel(sor, 0, SOR_LVDS);
  236. value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
  237. value |= SOR_DP_PADCTL_TX_PU_ENABLE;
  238. value &= ~SOR_DP_PADCTL_TX_PU_MASK;
  239. value |= SOR_DP_PADCTL_TX_PU(2); /* XXX: don't hardcode? */
  240. tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
  241. value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
  242. value |= SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
  243. SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0;
  244. tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
  245. usleep_range(10, 100);
  246. value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
  247. value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
  248. SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
  249. tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
  250. err = drm_dp_aux_prepare(sor->aux, DP_SET_ANSI_8B10B);
  251. if (err < 0)
  252. return err;
  253. for (i = 0, value = 0; i < link->num_lanes; i++) {
  254. unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
  255. SOR_DP_TPG_SCRAMBLER_NONE |
  256. SOR_DP_TPG_PATTERN_TRAIN1;
  257. value = (value << 8) | lane;
  258. }
  259. tegra_sor_writel(sor, value, SOR_DP_TPG);
  260. pattern = DP_TRAINING_PATTERN_1;
  261. err = drm_dp_aux_train(sor->aux, link, pattern);
  262. if (err < 0)
  263. return err;
  264. value = tegra_sor_readl(sor, SOR_DP_SPARE0);
  265. value |= SOR_DP_SPARE_SEQ_ENABLE;
  266. value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
  267. value |= SOR_DP_SPARE_MACRO_SOR_CLK;
  268. tegra_sor_writel(sor, value, SOR_DP_SPARE0);
  269. for (i = 0, value = 0; i < link->num_lanes; i++) {
  270. unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
  271. SOR_DP_TPG_SCRAMBLER_NONE |
  272. SOR_DP_TPG_PATTERN_TRAIN2;
  273. value = (value << 8) | lane;
  274. }
  275. tegra_sor_writel(sor, value, SOR_DP_TPG);
  276. pattern = DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_2;
  277. err = drm_dp_aux_train(sor->aux, link, pattern);
  278. if (err < 0)
  279. return err;
  280. for (i = 0, value = 0; i < link->num_lanes; i++) {
  281. unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
  282. SOR_DP_TPG_SCRAMBLER_GALIOS |
  283. SOR_DP_TPG_PATTERN_NONE;
  284. value = (value << 8) | lane;
  285. }
  286. tegra_sor_writel(sor, value, SOR_DP_TPG);
  287. pattern = DP_TRAINING_PATTERN_DISABLE;
  288. err = drm_dp_aux_train(sor->aux, link, pattern);
  289. if (err < 0)
  290. return err;
  291. return 0;
  292. }
  293. static void tegra_sor_dp_term_calibrate(struct tegra_sor *sor)
  294. {
  295. u32 mask = 0x08, adj = 0, value;
  296. /* enable pad calibration logic */
  297. value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
  298. value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
  299. tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
  300. value = tegra_sor_readl(sor, SOR_PLL1);
  301. value |= SOR_PLL1_TMDS_TERM;
  302. tegra_sor_writel(sor, value, SOR_PLL1);
  303. while (mask) {
  304. adj |= mask;
  305. value = tegra_sor_readl(sor, SOR_PLL1);
  306. value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
  307. value |= SOR_PLL1_TMDS_TERMADJ(adj);
  308. tegra_sor_writel(sor, value, SOR_PLL1);
  309. usleep_range(100, 200);
  310. value = tegra_sor_readl(sor, SOR_PLL1);
  311. if (value & SOR_PLL1_TERM_COMPOUT)
  312. adj &= ~mask;
  313. mask >>= 1;
  314. }
  315. value = tegra_sor_readl(sor, SOR_PLL1);
  316. value &= ~SOR_PLL1_TMDS_TERMADJ_MASK;
  317. value |= SOR_PLL1_TMDS_TERMADJ(adj);
  318. tegra_sor_writel(sor, value, SOR_PLL1);
  319. /* disable pad calibration logic */
  320. value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
  321. value |= SOR_DP_PADCTL_PAD_CAL_PD;
  322. tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
  323. }
  324. static void tegra_sor_super_update(struct tegra_sor *sor)
  325. {
  326. tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
  327. tegra_sor_writel(sor, 1, SOR_SUPER_STATE0);
  328. tegra_sor_writel(sor, 0, SOR_SUPER_STATE0);
  329. }
  330. static void tegra_sor_update(struct tegra_sor *sor)
  331. {
  332. tegra_sor_writel(sor, 0, SOR_STATE0);
  333. tegra_sor_writel(sor, 1, SOR_STATE0);
  334. tegra_sor_writel(sor, 0, SOR_STATE0);
  335. }
  336. static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
  337. {
  338. u32 value;
  339. value = tegra_sor_readl(sor, SOR_PWM_DIV);
  340. value &= ~SOR_PWM_DIV_MASK;
  341. value |= 0x400; /* period */
  342. tegra_sor_writel(sor, value, SOR_PWM_DIV);
  343. value = tegra_sor_readl(sor, SOR_PWM_CTL);
  344. value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
  345. value |= 0x400; /* duty cycle */
  346. value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
  347. value |= SOR_PWM_CTL_TRIGGER;
  348. tegra_sor_writel(sor, value, SOR_PWM_CTL);
  349. timeout = jiffies + msecs_to_jiffies(timeout);
  350. while (time_before(jiffies, timeout)) {
  351. value = tegra_sor_readl(sor, SOR_PWM_CTL);
  352. if ((value & SOR_PWM_CTL_TRIGGER) == 0)
  353. return 0;
  354. usleep_range(25, 100);
  355. }
  356. return -ETIMEDOUT;
  357. }
  358. static int tegra_sor_attach(struct tegra_sor *sor)
  359. {
  360. unsigned long value, timeout;
  361. /* wake up in normal mode */
  362. value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
  363. value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
  364. value |= SOR_SUPER_STATE_MODE_NORMAL;
  365. tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
  366. tegra_sor_super_update(sor);
  367. /* attach */
  368. value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
  369. value |= SOR_SUPER_STATE_ATTACHED;
  370. tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
  371. tegra_sor_super_update(sor);
  372. timeout = jiffies + msecs_to_jiffies(250);
  373. while (time_before(jiffies, timeout)) {
  374. value = tegra_sor_readl(sor, SOR_TEST);
  375. if ((value & SOR_TEST_ATTACHED) != 0)
  376. return 0;
  377. usleep_range(25, 100);
  378. }
  379. return -ETIMEDOUT;
  380. }
  381. static int tegra_sor_wakeup(struct tegra_sor *sor)
  382. {
  383. unsigned long value, timeout;
  384. timeout = jiffies + msecs_to_jiffies(250);
  385. /* wait for head to wake up */
  386. while (time_before(jiffies, timeout)) {
  387. value = tegra_sor_readl(sor, SOR_TEST);
  388. value &= SOR_TEST_HEAD_MODE_MASK;
  389. if (value == SOR_TEST_HEAD_MODE_AWAKE)
  390. return 0;
  391. usleep_range(25, 100);
  392. }
  393. return -ETIMEDOUT;
  394. }
  395. static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
  396. {
  397. u32 value;
  398. value = tegra_sor_readl(sor, SOR_PWR);
  399. value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
  400. tegra_sor_writel(sor, value, SOR_PWR);
  401. timeout = jiffies + msecs_to_jiffies(timeout);
  402. while (time_before(jiffies, timeout)) {
  403. value = tegra_sor_readl(sor, SOR_PWR);
  404. if ((value & SOR_PWR_TRIGGER) == 0)
  405. return 0;
  406. usleep_range(25, 100);
  407. }
  408. return -ETIMEDOUT;
  409. }
  410. struct tegra_sor_params {
  411. /* number of link clocks per line */
  412. unsigned int num_clocks;
  413. /* ratio between input and output */
  414. u64 ratio;
  415. /* precision factor */
  416. u64 precision;
  417. unsigned int active_polarity;
  418. unsigned int active_count;
  419. unsigned int active_frac;
  420. unsigned int tu_size;
  421. unsigned int error;
  422. };
  423. static int tegra_sor_compute_params(struct tegra_sor *sor,
  424. struct tegra_sor_params *params,
  425. unsigned int tu_size)
  426. {
  427. u64 active_sym, active_count, frac, approx;
  428. u32 active_polarity, active_frac = 0;
  429. const u64 f = params->precision;
  430. s64 error;
  431. active_sym = params->ratio * tu_size;
  432. active_count = div_u64(active_sym, f) * f;
  433. frac = active_sym - active_count;
  434. /* fraction < 0.5 */
  435. if (frac >= (f / 2)) {
  436. active_polarity = 1;
  437. frac = f - frac;
  438. } else {
  439. active_polarity = 0;
  440. }
  441. if (frac != 0) {
  442. frac = div_u64(f * f, frac); /* 1/fraction */
  443. if (frac <= (15 * f)) {
  444. active_frac = div_u64(frac, f);
  445. /* round up */
  446. if (active_polarity)
  447. active_frac++;
  448. } else {
  449. active_frac = active_polarity ? 1 : 15;
  450. }
  451. }
  452. if (active_frac == 1)
  453. active_polarity = 0;
  454. if (active_polarity == 1) {
  455. if (active_frac) {
  456. approx = active_count + (active_frac * (f - 1)) * f;
  457. approx = div_u64(approx, active_frac * f);
  458. } else {
  459. approx = active_count + f;
  460. }
  461. } else {
  462. if (active_frac)
  463. approx = active_count + div_u64(f, active_frac);
  464. else
  465. approx = active_count;
  466. }
  467. error = div_s64(active_sym - approx, tu_size);
  468. error *= params->num_clocks;
  469. if (error <= 0 && abs(error) < params->error) {
  470. params->active_count = div_u64(active_count, f);
  471. params->active_polarity = active_polarity;
  472. params->active_frac = active_frac;
  473. params->error = abs(error);
  474. params->tu_size = tu_size;
  475. if (error == 0)
  476. return true;
  477. }
  478. return false;
  479. }
  480. static int tegra_sor_compute_config(struct tegra_sor *sor,
  481. const struct drm_display_mode *mode,
  482. struct tegra_sor_config *config,
  483. struct drm_dp_link *link)
  484. {
  485. const u64 f = 100000, link_rate = link->rate * 1000;
  486. const u64 pclk = mode->clock * 1000;
  487. u64 input, output, watermark, num;
  488. struct tegra_sor_params params;
  489. u32 num_syms_per_line;
  490. unsigned int i;
  491. if (!link_rate || !link->num_lanes || !pclk || !config->bits_per_pixel)
  492. return -EINVAL;
  493. output = link_rate * 8 * link->num_lanes;
  494. input = pclk * config->bits_per_pixel;
  495. if (input >= output)
  496. return -ERANGE;
  497. memset(&params, 0, sizeof(params));
  498. params.ratio = div64_u64(input * f, output);
  499. params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
  500. params.precision = f;
  501. params.error = 64 * f;
  502. params.tu_size = 64;
  503. for (i = params.tu_size; i >= 32; i--)
  504. if (tegra_sor_compute_params(sor, &params, i))
  505. break;
  506. if (params.active_frac == 0) {
  507. config->active_polarity = 0;
  508. config->active_count = params.active_count;
  509. if (!params.active_polarity)
  510. config->active_count--;
  511. config->tu_size = params.tu_size;
  512. config->active_frac = 1;
  513. } else {
  514. config->active_polarity = params.active_polarity;
  515. config->active_count = params.active_count;
  516. config->active_frac = params.active_frac;
  517. config->tu_size = params.tu_size;
  518. }
  519. dev_dbg(sor->dev,
  520. "polarity: %d active count: %d tu size: %d active frac: %d\n",
  521. config->active_polarity, config->active_count,
  522. config->tu_size, config->active_frac);
  523. watermark = params.ratio * config->tu_size * (f - params.ratio);
  524. watermark = div_u64(watermark, f);
  525. watermark = div_u64(watermark + params.error, f);
  526. config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
  527. num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
  528. (link->num_lanes * 8);
  529. if (config->watermark > 30) {
  530. config->watermark = 30;
  531. dev_err(sor->dev,
  532. "unable to compute TU size, forcing watermark to %u\n",
  533. config->watermark);
  534. } else if (config->watermark > num_syms_per_line) {
  535. config->watermark = num_syms_per_line;
  536. dev_err(sor->dev, "watermark too high, forcing to %u\n",
  537. config->watermark);
  538. }
  539. /* compute the number of symbols per horizontal blanking interval */
  540. num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
  541. config->hblank_symbols = div_u64(num, pclk);
  542. if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
  543. config->hblank_symbols -= 3;
  544. config->hblank_symbols -= 12 / link->num_lanes;
  545. /* compute the number of symbols per vertical blanking interval */
  546. num = (mode->hdisplay - 25) * link_rate;
  547. config->vblank_symbols = div_u64(num, pclk);
  548. config->vblank_symbols -= 36 / link->num_lanes + 4;
  549. dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
  550. config->vblank_symbols);
  551. return 0;
  552. }
  553. static int tegra_sor_detach(struct tegra_sor *sor)
  554. {
  555. unsigned long value, timeout;
  556. /* switch to safe mode */
  557. value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
  558. value &= ~SOR_SUPER_STATE_MODE_NORMAL;
  559. tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
  560. tegra_sor_super_update(sor);
  561. timeout = jiffies + msecs_to_jiffies(250);
  562. while (time_before(jiffies, timeout)) {
  563. value = tegra_sor_readl(sor, SOR_PWR);
  564. if (value & SOR_PWR_MODE_SAFE)
  565. break;
  566. }
  567. if ((value & SOR_PWR_MODE_SAFE) == 0)
  568. return -ETIMEDOUT;
  569. /* go to sleep */
  570. value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
  571. value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
  572. tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
  573. tegra_sor_super_update(sor);
  574. /* detach */
  575. value = tegra_sor_readl(sor, SOR_SUPER_STATE1);
  576. value &= ~SOR_SUPER_STATE_ATTACHED;
  577. tegra_sor_writel(sor, value, SOR_SUPER_STATE1);
  578. tegra_sor_super_update(sor);
  579. timeout = jiffies + msecs_to_jiffies(250);
  580. while (time_before(jiffies, timeout)) {
  581. value = tegra_sor_readl(sor, SOR_TEST);
  582. if ((value & SOR_TEST_ATTACHED) == 0)
  583. break;
  584. usleep_range(25, 100);
  585. }
  586. if ((value & SOR_TEST_ATTACHED) != 0)
  587. return -ETIMEDOUT;
  588. return 0;
  589. }
  590. static int tegra_sor_power_down(struct tegra_sor *sor)
  591. {
  592. unsigned long value, timeout;
  593. int err;
  594. value = tegra_sor_readl(sor, SOR_PWR);
  595. value &= ~SOR_PWR_NORMAL_STATE_PU;
  596. value |= SOR_PWR_TRIGGER;
  597. tegra_sor_writel(sor, value, SOR_PWR);
  598. timeout = jiffies + msecs_to_jiffies(250);
  599. while (time_before(jiffies, timeout)) {
  600. value = tegra_sor_readl(sor, SOR_PWR);
  601. if ((value & SOR_PWR_TRIGGER) == 0)
  602. return 0;
  603. usleep_range(25, 100);
  604. }
  605. if ((value & SOR_PWR_TRIGGER) != 0)
  606. return -ETIMEDOUT;
  607. /* switch to safe parent clock */
  608. err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
  609. if (err < 0)
  610. dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
  611. value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
  612. value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
  613. SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
  614. tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
  615. /* stop lane sequencer */
  616. value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
  617. SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
  618. tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
  619. timeout = jiffies + msecs_to_jiffies(250);
  620. while (time_before(jiffies, timeout)) {
  621. value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
  622. if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
  623. break;
  624. usleep_range(25, 100);
  625. }
  626. if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
  627. return -ETIMEDOUT;
  628. value = tegra_sor_readl(sor, SOR_PLL2);
  629. value |= SOR_PLL2_PORT_POWERDOWN;
  630. tegra_sor_writel(sor, value, SOR_PLL2);
  631. usleep_range(20, 100);
  632. value = tegra_sor_readl(sor, SOR_PLL0);
  633. value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
  634. tegra_sor_writel(sor, value, SOR_PLL0);
  635. value = tegra_sor_readl(sor, SOR_PLL2);
  636. value |= SOR_PLL2_SEQ_PLLCAPPD;
  637. value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
  638. tegra_sor_writel(sor, value, SOR_PLL2);
  639. usleep_range(20, 100);
  640. return 0;
  641. }
  642. static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
  643. {
  644. u32 value;
  645. timeout = jiffies + msecs_to_jiffies(timeout);
  646. while (time_before(jiffies, timeout)) {
  647. value = tegra_sor_readl(sor, SOR_CRCA);
  648. if (value & SOR_CRCA_VALID)
  649. return 0;
  650. usleep_range(100, 200);
  651. }
  652. return -ETIMEDOUT;
  653. }
  654. static int tegra_sor_show_crc(struct seq_file *s, void *data)
  655. {
  656. struct drm_info_node *node = s->private;
  657. struct tegra_sor *sor = node->info_ent->data;
  658. struct drm_crtc *crtc = sor->output.encoder.crtc;
  659. struct drm_device *drm = node->minor->dev;
  660. int err = 0;
  661. u32 value;
  662. drm_modeset_lock_all(drm);
  663. if (!crtc || !crtc->state->active) {
  664. err = -EBUSY;
  665. goto unlock;
  666. }
  667. value = tegra_sor_readl(sor, SOR_STATE1);
  668. value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
  669. tegra_sor_writel(sor, value, SOR_STATE1);
  670. value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
  671. value |= SOR_CRC_CNTRL_ENABLE;
  672. tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
  673. value = tegra_sor_readl(sor, SOR_TEST);
  674. value &= ~SOR_TEST_CRC_POST_SERIALIZE;
  675. tegra_sor_writel(sor, value, SOR_TEST);
  676. err = tegra_sor_crc_wait(sor, 100);
  677. if (err < 0)
  678. goto unlock;
  679. tegra_sor_writel(sor, SOR_CRCA_RESET, SOR_CRCA);
  680. value = tegra_sor_readl(sor, SOR_CRCB);
  681. seq_printf(s, "%08x\n", value);
  682. unlock:
  683. drm_modeset_unlock_all(drm);
  684. return err;
  685. }
  686. static int tegra_sor_show_regs(struct seq_file *s, void *data)
  687. {
  688. struct drm_info_node *node = s->private;
  689. struct tegra_sor *sor = node->info_ent->data;
  690. struct drm_crtc *crtc = sor->output.encoder.crtc;
  691. struct drm_device *drm = node->minor->dev;
  692. int err = 0;
  693. drm_modeset_lock_all(drm);
  694. if (!crtc || !crtc->state->active) {
  695. err = -EBUSY;
  696. goto unlock;
  697. }
  698. #define DUMP_REG(name) \
  699. seq_printf(s, "%-38s %#05x %08x\n", #name, name, \
  700. tegra_sor_readl(sor, name))
  701. DUMP_REG(SOR_CTXSW);
  702. DUMP_REG(SOR_SUPER_STATE0);
  703. DUMP_REG(SOR_SUPER_STATE1);
  704. DUMP_REG(SOR_STATE0);
  705. DUMP_REG(SOR_STATE1);
  706. DUMP_REG(SOR_HEAD_STATE0(0));
  707. DUMP_REG(SOR_HEAD_STATE0(1));
  708. DUMP_REG(SOR_HEAD_STATE1(0));
  709. DUMP_REG(SOR_HEAD_STATE1(1));
  710. DUMP_REG(SOR_HEAD_STATE2(0));
  711. DUMP_REG(SOR_HEAD_STATE2(1));
  712. DUMP_REG(SOR_HEAD_STATE3(0));
  713. DUMP_REG(SOR_HEAD_STATE3(1));
  714. DUMP_REG(SOR_HEAD_STATE4(0));
  715. DUMP_REG(SOR_HEAD_STATE4(1));
  716. DUMP_REG(SOR_HEAD_STATE5(0));
  717. DUMP_REG(SOR_HEAD_STATE5(1));
  718. DUMP_REG(SOR_CRC_CNTRL);
  719. DUMP_REG(SOR_DP_DEBUG_MVID);
  720. DUMP_REG(SOR_CLK_CNTRL);
  721. DUMP_REG(SOR_CAP);
  722. DUMP_REG(SOR_PWR);
  723. DUMP_REG(SOR_TEST);
  724. DUMP_REG(SOR_PLL0);
  725. DUMP_REG(SOR_PLL1);
  726. DUMP_REG(SOR_PLL2);
  727. DUMP_REG(SOR_PLL3);
  728. DUMP_REG(SOR_CSTM);
  729. DUMP_REG(SOR_LVDS);
  730. DUMP_REG(SOR_CRCA);
  731. DUMP_REG(SOR_CRCB);
  732. DUMP_REG(SOR_BLANK);
  733. DUMP_REG(SOR_SEQ_CTL);
  734. DUMP_REG(SOR_LANE_SEQ_CTL);
  735. DUMP_REG(SOR_SEQ_INST(0));
  736. DUMP_REG(SOR_SEQ_INST(1));
  737. DUMP_REG(SOR_SEQ_INST(2));
  738. DUMP_REG(SOR_SEQ_INST(3));
  739. DUMP_REG(SOR_SEQ_INST(4));
  740. DUMP_REG(SOR_SEQ_INST(5));
  741. DUMP_REG(SOR_SEQ_INST(6));
  742. DUMP_REG(SOR_SEQ_INST(7));
  743. DUMP_REG(SOR_SEQ_INST(8));
  744. DUMP_REG(SOR_SEQ_INST(9));
  745. DUMP_REG(SOR_SEQ_INST(10));
  746. DUMP_REG(SOR_SEQ_INST(11));
  747. DUMP_REG(SOR_SEQ_INST(12));
  748. DUMP_REG(SOR_SEQ_INST(13));
  749. DUMP_REG(SOR_SEQ_INST(14));
  750. DUMP_REG(SOR_SEQ_INST(15));
  751. DUMP_REG(SOR_PWM_DIV);
  752. DUMP_REG(SOR_PWM_CTL);
  753. DUMP_REG(SOR_VCRC_A0);
  754. DUMP_REG(SOR_VCRC_A1);
  755. DUMP_REG(SOR_VCRC_B0);
  756. DUMP_REG(SOR_VCRC_B1);
  757. DUMP_REG(SOR_CCRC_A0);
  758. DUMP_REG(SOR_CCRC_A1);
  759. DUMP_REG(SOR_CCRC_B0);
  760. DUMP_REG(SOR_CCRC_B1);
  761. DUMP_REG(SOR_EDATA_A0);
  762. DUMP_REG(SOR_EDATA_A1);
  763. DUMP_REG(SOR_EDATA_B0);
  764. DUMP_REG(SOR_EDATA_B1);
  765. DUMP_REG(SOR_COUNT_A0);
  766. DUMP_REG(SOR_COUNT_A1);
  767. DUMP_REG(SOR_COUNT_B0);
  768. DUMP_REG(SOR_COUNT_B1);
  769. DUMP_REG(SOR_DEBUG_A0);
  770. DUMP_REG(SOR_DEBUG_A1);
  771. DUMP_REG(SOR_DEBUG_B0);
  772. DUMP_REG(SOR_DEBUG_B1);
  773. DUMP_REG(SOR_TRIG);
  774. DUMP_REG(SOR_MSCHECK);
  775. DUMP_REG(SOR_XBAR_CTRL);
  776. DUMP_REG(SOR_XBAR_POL);
  777. DUMP_REG(SOR_DP_LINKCTL0);
  778. DUMP_REG(SOR_DP_LINKCTL1);
  779. DUMP_REG(SOR_LANE_DRIVE_CURRENT0);
  780. DUMP_REG(SOR_LANE_DRIVE_CURRENT1);
  781. DUMP_REG(SOR_LANE4_DRIVE_CURRENT0);
  782. DUMP_REG(SOR_LANE4_DRIVE_CURRENT1);
  783. DUMP_REG(SOR_LANE_PREEMPHASIS0);
  784. DUMP_REG(SOR_LANE_PREEMPHASIS1);
  785. DUMP_REG(SOR_LANE4_PREEMPHASIS0);
  786. DUMP_REG(SOR_LANE4_PREEMPHASIS1);
  787. DUMP_REG(SOR_LANE_POSTCURSOR0);
  788. DUMP_REG(SOR_LANE_POSTCURSOR1);
  789. DUMP_REG(SOR_DP_CONFIG0);
  790. DUMP_REG(SOR_DP_CONFIG1);
  791. DUMP_REG(SOR_DP_MN0);
  792. DUMP_REG(SOR_DP_MN1);
  793. DUMP_REG(SOR_DP_PADCTL0);
  794. DUMP_REG(SOR_DP_PADCTL1);
  795. DUMP_REG(SOR_DP_DEBUG0);
  796. DUMP_REG(SOR_DP_DEBUG1);
  797. DUMP_REG(SOR_DP_SPARE0);
  798. DUMP_REG(SOR_DP_SPARE1);
  799. DUMP_REG(SOR_DP_AUDIO_CTRL);
  800. DUMP_REG(SOR_DP_AUDIO_HBLANK_SYMBOLS);
  801. DUMP_REG(SOR_DP_AUDIO_VBLANK_SYMBOLS);
  802. DUMP_REG(SOR_DP_GENERIC_INFOFRAME_HEADER);
  803. DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK0);
  804. DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK1);
  805. DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK2);
  806. DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK3);
  807. DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK4);
  808. DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK5);
  809. DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK6);
  810. DUMP_REG(SOR_DP_TPG);
  811. DUMP_REG(SOR_DP_TPG_CONFIG);
  812. DUMP_REG(SOR_DP_LQ_CSTM0);
  813. DUMP_REG(SOR_DP_LQ_CSTM1);
  814. DUMP_REG(SOR_DP_LQ_CSTM2);
  815. #undef DUMP_REG
  816. unlock:
  817. drm_modeset_unlock_all(drm);
  818. return err;
  819. }
  820. static const struct drm_info_list debugfs_files[] = {
  821. { "crc", tegra_sor_show_crc, 0, NULL },
  822. { "regs", tegra_sor_show_regs, 0, NULL },
  823. };
  824. static int tegra_sor_debugfs_init(struct tegra_sor *sor,
  825. struct drm_minor *minor)
  826. {
  827. const char *name = sor->soc->supports_dp ? "sor1" : "sor";
  828. unsigned int i;
  829. int err;
  830. sor->debugfs = debugfs_create_dir(name, minor->debugfs_root);
  831. if (!sor->debugfs)
  832. return -ENOMEM;
  833. sor->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
  834. GFP_KERNEL);
  835. if (!sor->debugfs_files) {
  836. err = -ENOMEM;
  837. goto remove;
  838. }
  839. for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
  840. sor->debugfs_files[i].data = sor;
  841. err = drm_debugfs_create_files(sor->debugfs_files,
  842. ARRAY_SIZE(debugfs_files),
  843. sor->debugfs, minor);
  844. if (err < 0)
  845. goto free;
  846. sor->minor = minor;
  847. return 0;
  848. free:
  849. kfree(sor->debugfs_files);
  850. sor->debugfs_files = NULL;
  851. remove:
  852. debugfs_remove_recursive(sor->debugfs);
  853. sor->debugfs = NULL;
  854. return err;
  855. }
  856. static void tegra_sor_debugfs_exit(struct tegra_sor *sor)
  857. {
  858. drm_debugfs_remove_files(sor->debugfs_files, ARRAY_SIZE(debugfs_files),
  859. sor->minor);
  860. sor->minor = NULL;
  861. kfree(sor->debugfs_files);
  862. sor->debugfs_files = NULL;
  863. debugfs_remove_recursive(sor->debugfs);
  864. sor->debugfs = NULL;
  865. }
  866. static enum drm_connector_status
  867. tegra_sor_connector_detect(struct drm_connector *connector, bool force)
  868. {
  869. struct tegra_output *output = connector_to_output(connector);
  870. struct tegra_sor *sor = to_sor(output);
  871. if (sor->aux)
  872. return drm_dp_aux_detect(sor->aux);
  873. return tegra_output_connector_detect(connector, force);
  874. }
  875. static const struct drm_connector_funcs tegra_sor_connector_funcs = {
  876. .dpms = drm_atomic_helper_connector_dpms,
  877. .reset = drm_atomic_helper_connector_reset,
  878. .detect = tegra_sor_connector_detect,
  879. .fill_modes = drm_helper_probe_single_connector_modes,
  880. .destroy = tegra_output_connector_destroy,
  881. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  882. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  883. };
  884. static int tegra_sor_connector_get_modes(struct drm_connector *connector)
  885. {
  886. struct tegra_output *output = connector_to_output(connector);
  887. struct tegra_sor *sor = to_sor(output);
  888. int err;
  889. if (sor->aux)
  890. drm_dp_aux_enable(sor->aux);
  891. err = tegra_output_connector_get_modes(connector);
  892. if (sor->aux)
  893. drm_dp_aux_disable(sor->aux);
  894. return err;
  895. }
  896. static enum drm_mode_status
  897. tegra_sor_connector_mode_valid(struct drm_connector *connector,
  898. struct drm_display_mode *mode)
  899. {
  900. return MODE_OK;
  901. }
  902. static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
  903. .get_modes = tegra_sor_connector_get_modes,
  904. .mode_valid = tegra_sor_connector_mode_valid,
  905. .best_encoder = tegra_output_connector_best_encoder,
  906. };
  907. static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
  908. .destroy = tegra_output_encoder_destroy,
  909. };
  910. static void tegra_sor_edp_disable(struct drm_encoder *encoder)
  911. {
  912. struct tegra_output *output = encoder_to_output(encoder);
  913. struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
  914. struct tegra_sor *sor = to_sor(output);
  915. u32 value;
  916. int err;
  917. if (output->panel)
  918. drm_panel_disable(output->panel);
  919. err = tegra_sor_detach(sor);
  920. if (err < 0)
  921. dev_err(sor->dev, "failed to detach SOR: %d\n", err);
  922. tegra_sor_writel(sor, 0, SOR_STATE1);
  923. tegra_sor_update(sor);
  924. /*
  925. * The following accesses registers of the display controller, so make
  926. * sure it's only executed when the output is attached to one.
  927. */
  928. if (dc) {
  929. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  930. value &= ~SOR_ENABLE;
  931. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  932. tegra_dc_commit(dc);
  933. }
  934. err = tegra_sor_power_down(sor);
  935. if (err < 0)
  936. dev_err(sor->dev, "failed to power down SOR: %d\n", err);
  937. if (sor->aux) {
  938. err = drm_dp_aux_disable(sor->aux);
  939. if (err < 0)
  940. dev_err(sor->dev, "failed to disable DP: %d\n", err);
  941. }
  942. err = tegra_io_rail_power_off(TEGRA_IO_RAIL_LVDS);
  943. if (err < 0)
  944. dev_err(sor->dev, "failed to power off I/O rail: %d\n", err);
  945. if (output->panel)
  946. drm_panel_unprepare(output->panel);
  947. reset_control_assert(sor->rst);
  948. clk_disable_unprepare(sor->clk);
  949. }
  950. #if 0
  951. static int calc_h_ref_to_sync(const struct drm_display_mode *mode,
  952. unsigned int *value)
  953. {
  954. unsigned int hfp, hsw, hbp, a = 0, b;
  955. hfp = mode->hsync_start - mode->hdisplay;
  956. hsw = mode->hsync_end - mode->hsync_start;
  957. hbp = mode->htotal - mode->hsync_end;
  958. pr_info("hfp: %u, hsw: %u, hbp: %u\n", hfp, hsw, hbp);
  959. b = hfp - 1;
  960. pr_info("a: %u, b: %u\n", a, b);
  961. pr_info("a + hsw + hbp = %u\n", a + hsw + hbp);
  962. if (a + hsw + hbp <= 11) {
  963. a = 1 + 11 - hsw - hbp;
  964. pr_info("a: %u\n", a);
  965. }
  966. if (a > b)
  967. return -EINVAL;
  968. if (hsw < 1)
  969. return -EINVAL;
  970. if (mode->hdisplay < 16)
  971. return -EINVAL;
  972. if (value) {
  973. if (b > a && a % 2)
  974. *value = a + 1;
  975. else
  976. *value = a;
  977. }
  978. return 0;
  979. }
  980. #endif
  981. static void tegra_sor_edp_enable(struct drm_encoder *encoder)
  982. {
  983. struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
  984. struct tegra_output *output = encoder_to_output(encoder);
  985. struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
  986. unsigned int vbe, vse, hbe, hse, vbs, hbs, i;
  987. struct tegra_sor *sor = to_sor(output);
  988. struct tegra_sor_config config;
  989. struct drm_dp_link link;
  990. u8 rate, lanes;
  991. int err = 0;
  992. u32 value;
  993. err = clk_prepare_enable(sor->clk);
  994. if (err < 0)
  995. dev_err(sor->dev, "failed to enable clock: %d\n", err);
  996. reset_control_deassert(sor->rst);
  997. if (output->panel)
  998. drm_panel_prepare(output->panel);
  999. err = drm_dp_aux_enable(sor->aux);
  1000. if (err < 0)
  1001. dev_err(sor->dev, "failed to enable DP: %d\n", err);
  1002. err = drm_dp_link_probe(sor->aux, &link);
  1003. if (err < 0) {
  1004. dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
  1005. return;
  1006. }
  1007. /* switch to safe parent clock */
  1008. err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
  1009. if (err < 0)
  1010. dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
  1011. memset(&config, 0, sizeof(config));
  1012. config.bits_per_pixel = output->connector.display_info.bpc * 3;
  1013. err = tegra_sor_compute_config(sor, mode, &config, &link);
  1014. if (err < 0)
  1015. dev_err(sor->dev, "failed to compute configuration: %d\n", err);
  1016. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  1017. value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
  1018. value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
  1019. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  1020. value = tegra_sor_readl(sor, SOR_PLL2);
  1021. value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
  1022. tegra_sor_writel(sor, value, SOR_PLL2);
  1023. usleep_range(20, 100);
  1024. value = tegra_sor_readl(sor, SOR_PLL3);
  1025. value |= SOR_PLL3_PLL_VDD_MODE_3V3;
  1026. tegra_sor_writel(sor, value, SOR_PLL3);
  1027. value = SOR_PLL0_ICHPMP(0xf) | SOR_PLL0_VCOCAP_RST |
  1028. SOR_PLL0_PLLREG_LEVEL_V45 | SOR_PLL0_RESISTOR_EXT;
  1029. tegra_sor_writel(sor, value, SOR_PLL0);
  1030. value = tegra_sor_readl(sor, SOR_PLL2);
  1031. value |= SOR_PLL2_SEQ_PLLCAPPD;
  1032. value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
  1033. value |= SOR_PLL2_LVDS_ENABLE;
  1034. tegra_sor_writel(sor, value, SOR_PLL2);
  1035. value = SOR_PLL1_TERM_COMPOUT | SOR_PLL1_TMDS_TERM;
  1036. tegra_sor_writel(sor, value, SOR_PLL1);
  1037. while (true) {
  1038. value = tegra_sor_readl(sor, SOR_PLL2);
  1039. if ((value & SOR_PLL2_SEQ_PLLCAPPD_ENFORCE) == 0)
  1040. break;
  1041. usleep_range(250, 1000);
  1042. }
  1043. value = tegra_sor_readl(sor, SOR_PLL2);
  1044. value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
  1045. value &= ~SOR_PLL2_PORT_POWERDOWN;
  1046. tegra_sor_writel(sor, value, SOR_PLL2);
  1047. /*
  1048. * power up
  1049. */
  1050. /* set safe link bandwidth (1.62 Gbps) */
  1051. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  1052. value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
  1053. value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
  1054. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  1055. /* step 1 */
  1056. value = tegra_sor_readl(sor, SOR_PLL2);
  1057. value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL2_PORT_POWERDOWN |
  1058. SOR_PLL2_BANDGAP_POWERDOWN;
  1059. tegra_sor_writel(sor, value, SOR_PLL2);
  1060. value = tegra_sor_readl(sor, SOR_PLL0);
  1061. value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
  1062. tegra_sor_writel(sor, value, SOR_PLL0);
  1063. value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
  1064. value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
  1065. tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
  1066. /* step 2 */
  1067. err = tegra_io_rail_power_on(TEGRA_IO_RAIL_LVDS);
  1068. if (err < 0)
  1069. dev_err(sor->dev, "failed to power on I/O rail: %d\n", err);
  1070. usleep_range(5, 100);
  1071. /* step 3 */
  1072. value = tegra_sor_readl(sor, SOR_PLL2);
  1073. value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
  1074. tegra_sor_writel(sor, value, SOR_PLL2);
  1075. usleep_range(20, 100);
  1076. /* step 4 */
  1077. value = tegra_sor_readl(sor, SOR_PLL0);
  1078. value &= ~SOR_PLL0_VCOPD;
  1079. value &= ~SOR_PLL0_PWR;
  1080. tegra_sor_writel(sor, value, SOR_PLL0);
  1081. value = tegra_sor_readl(sor, SOR_PLL2);
  1082. value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
  1083. tegra_sor_writel(sor, value, SOR_PLL2);
  1084. usleep_range(200, 1000);
  1085. /* step 5 */
  1086. value = tegra_sor_readl(sor, SOR_PLL2);
  1087. value &= ~SOR_PLL2_PORT_POWERDOWN;
  1088. tegra_sor_writel(sor, value, SOR_PLL2);
  1089. /* switch to DP parent clock */
  1090. err = tegra_sor_set_parent_clock(sor, sor->clk_dp);
  1091. if (err < 0)
  1092. dev_err(sor->dev, "failed to set parent clock: %d\n", err);
  1093. /* power DP lanes */
  1094. value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
  1095. if (link.num_lanes <= 2)
  1096. value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2);
  1097. else
  1098. value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2;
  1099. if (link.num_lanes <= 1)
  1100. value &= ~SOR_DP_PADCTL_PD_TXD_1;
  1101. else
  1102. value |= SOR_DP_PADCTL_PD_TXD_1;
  1103. if (link.num_lanes == 0)
  1104. value &= ~SOR_DP_PADCTL_PD_TXD_0;
  1105. else
  1106. value |= SOR_DP_PADCTL_PD_TXD_0;
  1107. tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
  1108. value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
  1109. value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
  1110. value |= SOR_DP_LINKCTL_LANE_COUNT(link.num_lanes);
  1111. tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
  1112. /* start lane sequencer */
  1113. value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
  1114. SOR_LANE_SEQ_CTL_POWER_STATE_UP;
  1115. tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
  1116. while (true) {
  1117. value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
  1118. if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
  1119. break;
  1120. usleep_range(250, 1000);
  1121. }
  1122. /* set link bandwidth */
  1123. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  1124. value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
  1125. value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
  1126. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  1127. /* set linkctl */
  1128. value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
  1129. value |= SOR_DP_LINKCTL_ENABLE;
  1130. value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
  1131. value |= SOR_DP_LINKCTL_TU_SIZE(config.tu_size);
  1132. value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
  1133. tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
  1134. for (i = 0, value = 0; i < 4; i++) {
  1135. unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
  1136. SOR_DP_TPG_SCRAMBLER_GALIOS |
  1137. SOR_DP_TPG_PATTERN_NONE;
  1138. value = (value << 8) | lane;
  1139. }
  1140. tegra_sor_writel(sor, value, SOR_DP_TPG);
  1141. value = tegra_sor_readl(sor, SOR_DP_CONFIG0);
  1142. value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
  1143. value |= SOR_DP_CONFIG_WATERMARK(config.watermark);
  1144. value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
  1145. value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config.active_count);
  1146. value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
  1147. value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config.active_frac);
  1148. if (config.active_polarity)
  1149. value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
  1150. else
  1151. value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
  1152. value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
  1153. value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
  1154. tegra_sor_writel(sor, value, SOR_DP_CONFIG0);
  1155. value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
  1156. value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
  1157. value |= config.hblank_symbols & 0xffff;
  1158. tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
  1159. value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
  1160. value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
  1161. value |= config.vblank_symbols & 0xffff;
  1162. tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
  1163. /* enable pad calibration logic */
  1164. value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
  1165. value |= SOR_DP_PADCTL_PAD_CAL_PD;
  1166. tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
  1167. err = drm_dp_link_probe(sor->aux, &link);
  1168. if (err < 0)
  1169. dev_err(sor->dev, "failed to probe eDP link: %d\n", err);
  1170. err = drm_dp_link_power_up(sor->aux, &link);
  1171. if (err < 0)
  1172. dev_err(sor->dev, "failed to power up eDP link: %d\n", err);
  1173. err = drm_dp_link_configure(sor->aux, &link);
  1174. if (err < 0)
  1175. dev_err(sor->dev, "failed to configure eDP link: %d\n", err);
  1176. rate = drm_dp_link_rate_to_bw_code(link.rate);
  1177. lanes = link.num_lanes;
  1178. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  1179. value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
  1180. value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
  1181. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  1182. value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
  1183. value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
  1184. value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
  1185. if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
  1186. value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
  1187. tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
  1188. /* disable training pattern generator */
  1189. for (i = 0; i < link.num_lanes; i++) {
  1190. unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
  1191. SOR_DP_TPG_SCRAMBLER_GALIOS |
  1192. SOR_DP_TPG_PATTERN_NONE;
  1193. value = (value << 8) | lane;
  1194. }
  1195. tegra_sor_writel(sor, value, SOR_DP_TPG);
  1196. err = tegra_sor_dp_train_fast(sor, &link);
  1197. if (err < 0)
  1198. dev_err(sor->dev, "DP fast link training failed: %d\n", err);
  1199. dev_dbg(sor->dev, "fast link training succeeded\n");
  1200. err = tegra_sor_power_up(sor, 250);
  1201. if (err < 0)
  1202. dev_err(sor->dev, "failed to power up SOR: %d\n", err);
  1203. /*
  1204. * configure panel (24bpp, vsync-, hsync-, DP-A protocol, complete
  1205. * raster, associate with display controller)
  1206. */
  1207. value = SOR_STATE_ASY_PROTOCOL_DP_A |
  1208. SOR_STATE_ASY_CRC_MODE_COMPLETE |
  1209. SOR_STATE_ASY_OWNER(dc->pipe + 1);
  1210. if (mode->flags & DRM_MODE_FLAG_PHSYNC)
  1211. value &= ~SOR_STATE_ASY_HSYNCPOL;
  1212. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  1213. value |= SOR_STATE_ASY_HSYNCPOL;
  1214. if (mode->flags & DRM_MODE_FLAG_PVSYNC)
  1215. value &= ~SOR_STATE_ASY_VSYNCPOL;
  1216. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  1217. value |= SOR_STATE_ASY_VSYNCPOL;
  1218. switch (config.bits_per_pixel) {
  1219. case 24:
  1220. value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
  1221. break;
  1222. case 18:
  1223. value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
  1224. break;
  1225. default:
  1226. BUG();
  1227. break;
  1228. }
  1229. tegra_sor_writel(sor, value, SOR_STATE1);
  1230. /*
  1231. * TODO: The video timing programming below doesn't seem to match the
  1232. * register definitions.
  1233. */
  1234. value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
  1235. tegra_sor_writel(sor, value, SOR_HEAD_STATE1(dc->pipe));
  1236. vse = mode->vsync_end - mode->vsync_start - 1;
  1237. hse = mode->hsync_end - mode->hsync_start - 1;
  1238. value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
  1239. tegra_sor_writel(sor, value, SOR_HEAD_STATE2(dc->pipe));
  1240. vbe = vse + (mode->vsync_start - mode->vdisplay);
  1241. hbe = hse + (mode->hsync_start - mode->hdisplay);
  1242. value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
  1243. tegra_sor_writel(sor, value, SOR_HEAD_STATE3(dc->pipe));
  1244. vbs = vbe + mode->vdisplay;
  1245. hbs = hbe + mode->hdisplay;
  1246. value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
  1247. tegra_sor_writel(sor, value, SOR_HEAD_STATE4(dc->pipe));
  1248. tegra_sor_writel(sor, 0x1, SOR_HEAD_STATE5(dc->pipe));
  1249. /* CSTM (LVDS, link A/B, upper) */
  1250. value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
  1251. SOR_CSTM_UPPER;
  1252. tegra_sor_writel(sor, value, SOR_CSTM);
  1253. /* PWM setup */
  1254. err = tegra_sor_setup_pwm(sor, 250);
  1255. if (err < 0)
  1256. dev_err(sor->dev, "failed to setup PWM: %d\n", err);
  1257. tegra_sor_update(sor);
  1258. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  1259. value |= SOR_ENABLE;
  1260. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  1261. tegra_dc_commit(dc);
  1262. err = tegra_sor_attach(sor);
  1263. if (err < 0)
  1264. dev_err(sor->dev, "failed to attach SOR: %d\n", err);
  1265. err = tegra_sor_wakeup(sor);
  1266. if (err < 0)
  1267. dev_err(sor->dev, "failed to enable DC: %d\n", err);
  1268. if (output->panel)
  1269. drm_panel_enable(output->panel);
  1270. }
  1271. static int
  1272. tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
  1273. struct drm_crtc_state *crtc_state,
  1274. struct drm_connector_state *conn_state)
  1275. {
  1276. struct tegra_output *output = encoder_to_output(encoder);
  1277. struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
  1278. unsigned long pclk = crtc_state->mode.clock * 1000;
  1279. struct tegra_sor *sor = to_sor(output);
  1280. int err;
  1281. err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
  1282. pclk, 0);
  1283. if (err < 0) {
  1284. dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
  1285. return err;
  1286. }
  1287. return 0;
  1288. }
  1289. static const struct drm_encoder_helper_funcs tegra_sor_edp_helpers = {
  1290. .disable = tegra_sor_edp_disable,
  1291. .enable = tegra_sor_edp_enable,
  1292. .atomic_check = tegra_sor_encoder_atomic_check,
  1293. };
  1294. static inline u32 tegra_sor_hdmi_subpack(const u8 *ptr, size_t size)
  1295. {
  1296. u32 value = 0;
  1297. size_t i;
  1298. for (i = size; i > 0; i--)
  1299. value = (value << 8) | ptr[i - 1];
  1300. return value;
  1301. }
  1302. static void tegra_sor_hdmi_write_infopack(struct tegra_sor *sor,
  1303. const void *data, size_t size)
  1304. {
  1305. const u8 *ptr = data;
  1306. unsigned long offset;
  1307. size_t i, j;
  1308. u32 value;
  1309. switch (ptr[0]) {
  1310. case HDMI_INFOFRAME_TYPE_AVI:
  1311. offset = SOR_HDMI_AVI_INFOFRAME_HEADER;
  1312. break;
  1313. case HDMI_INFOFRAME_TYPE_AUDIO:
  1314. offset = SOR_HDMI_AUDIO_INFOFRAME_HEADER;
  1315. break;
  1316. case HDMI_INFOFRAME_TYPE_VENDOR:
  1317. offset = SOR_HDMI_VSI_INFOFRAME_HEADER;
  1318. break;
  1319. default:
  1320. dev_err(sor->dev, "unsupported infoframe type: %02x\n",
  1321. ptr[0]);
  1322. return;
  1323. }
  1324. value = INFOFRAME_HEADER_TYPE(ptr[0]) |
  1325. INFOFRAME_HEADER_VERSION(ptr[1]) |
  1326. INFOFRAME_HEADER_LEN(ptr[2]);
  1327. tegra_sor_writel(sor, value, offset);
  1328. offset++;
  1329. /*
  1330. * Each subpack contains 7 bytes, divided into:
  1331. * - subpack_low: bytes 0 - 3
  1332. * - subpack_high: bytes 4 - 6 (with byte 7 padded to 0x00)
  1333. */
  1334. for (i = 3, j = 0; i < size; i += 7, j += 8) {
  1335. size_t rem = size - i, num = min_t(size_t, rem, 4);
  1336. value = tegra_sor_hdmi_subpack(&ptr[i], num);
  1337. tegra_sor_writel(sor, value, offset++);
  1338. num = min_t(size_t, rem - num, 3);
  1339. value = tegra_sor_hdmi_subpack(&ptr[i + 4], num);
  1340. tegra_sor_writel(sor, value, offset++);
  1341. }
  1342. }
  1343. static int
  1344. tegra_sor_hdmi_setup_avi_infoframe(struct tegra_sor *sor,
  1345. const struct drm_display_mode *mode)
  1346. {
  1347. u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
  1348. struct hdmi_avi_infoframe frame;
  1349. u32 value;
  1350. int err;
  1351. /* disable AVI infoframe */
  1352. value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
  1353. value &= ~INFOFRAME_CTRL_SINGLE;
  1354. value &= ~INFOFRAME_CTRL_OTHER;
  1355. value &= ~INFOFRAME_CTRL_ENABLE;
  1356. tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
  1357. err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
  1358. if (err < 0) {
  1359. dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
  1360. return err;
  1361. }
  1362. err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
  1363. if (err < 0) {
  1364. dev_err(sor->dev, "failed to pack AVI infoframe: %d\n", err);
  1365. return err;
  1366. }
  1367. tegra_sor_hdmi_write_infopack(sor, buffer, err);
  1368. /* enable AVI infoframe */
  1369. value = tegra_sor_readl(sor, SOR_HDMI_AVI_INFOFRAME_CTRL);
  1370. value |= INFOFRAME_CTRL_CHECKSUM_ENABLE;
  1371. value |= INFOFRAME_CTRL_ENABLE;
  1372. tegra_sor_writel(sor, value, SOR_HDMI_AVI_INFOFRAME_CTRL);
  1373. return 0;
  1374. }
  1375. static void tegra_sor_hdmi_disable_audio_infoframe(struct tegra_sor *sor)
  1376. {
  1377. u32 value;
  1378. value = tegra_sor_readl(sor, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
  1379. value &= ~INFOFRAME_CTRL_ENABLE;
  1380. tegra_sor_writel(sor, value, SOR_HDMI_AUDIO_INFOFRAME_CTRL);
  1381. }
  1382. static struct tegra_sor_hdmi_settings *
  1383. tegra_sor_hdmi_find_settings(struct tegra_sor *sor, unsigned long frequency)
  1384. {
  1385. unsigned int i;
  1386. for (i = 0; i < sor->num_settings; i++)
  1387. if (frequency <= sor->settings[i].frequency)
  1388. return &sor->settings[i];
  1389. return NULL;
  1390. }
  1391. static void tegra_sor_hdmi_disable(struct drm_encoder *encoder)
  1392. {
  1393. struct tegra_output *output = encoder_to_output(encoder);
  1394. struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
  1395. struct tegra_sor *sor = to_sor(output);
  1396. u32 value;
  1397. int err;
  1398. err = tegra_sor_detach(sor);
  1399. if (err < 0)
  1400. dev_err(sor->dev, "failed to detach SOR: %d\n", err);
  1401. tegra_sor_writel(sor, 0, SOR_STATE1);
  1402. tegra_sor_update(sor);
  1403. /* disable display to SOR clock */
  1404. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  1405. value &= ~SOR1_TIMING_CYA;
  1406. value &= ~SOR1_ENABLE;
  1407. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  1408. tegra_dc_commit(dc);
  1409. err = tegra_sor_power_down(sor);
  1410. if (err < 0)
  1411. dev_err(sor->dev, "failed to power down SOR: %d\n", err);
  1412. err = tegra_io_rail_power_off(TEGRA_IO_RAIL_HDMI);
  1413. if (err < 0)
  1414. dev_err(sor->dev, "failed to power off HDMI rail: %d\n", err);
  1415. reset_control_assert(sor->rst);
  1416. usleep_range(1000, 2000);
  1417. clk_disable_unprepare(sor->clk);
  1418. }
  1419. static void tegra_sor_hdmi_enable(struct drm_encoder *encoder)
  1420. {
  1421. struct tegra_output *output = encoder_to_output(encoder);
  1422. unsigned int h_ref_to_sync = 1, pulse_start, max_ac;
  1423. struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
  1424. unsigned int vbe, vse, hbe, hse, vbs, hbs, div;
  1425. struct tegra_sor_hdmi_settings *settings;
  1426. struct tegra_sor *sor = to_sor(output);
  1427. struct drm_display_mode *mode;
  1428. struct drm_display_info *info;
  1429. u32 value;
  1430. int err;
  1431. mode = &encoder->crtc->state->adjusted_mode;
  1432. info = &output->connector.display_info;
  1433. err = clk_prepare_enable(sor->clk);
  1434. if (err < 0)
  1435. dev_err(sor->dev, "failed to enable clock: %d\n", err);
  1436. usleep_range(1000, 2000);
  1437. reset_control_deassert(sor->rst);
  1438. /* switch to safe parent clock */
  1439. err = tegra_sor_set_parent_clock(sor, sor->clk_safe);
  1440. if (err < 0)
  1441. dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
  1442. div = clk_get_rate(sor->clk) / 1000000 * 4;
  1443. err = tegra_io_rail_power_on(TEGRA_IO_RAIL_HDMI);
  1444. if (err < 0)
  1445. dev_err(sor->dev, "failed to power on HDMI rail: %d\n", err);
  1446. usleep_range(20, 100);
  1447. value = tegra_sor_readl(sor, SOR_PLL2);
  1448. value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
  1449. tegra_sor_writel(sor, value, SOR_PLL2);
  1450. usleep_range(20, 100);
  1451. value = tegra_sor_readl(sor, SOR_PLL3);
  1452. value &= ~SOR_PLL3_PLL_VDD_MODE_3V3;
  1453. tegra_sor_writel(sor, value, SOR_PLL3);
  1454. value = tegra_sor_readl(sor, SOR_PLL0);
  1455. value &= ~SOR_PLL0_VCOPD;
  1456. value &= ~SOR_PLL0_PWR;
  1457. tegra_sor_writel(sor, value, SOR_PLL0);
  1458. value = tegra_sor_readl(sor, SOR_PLL2);
  1459. value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
  1460. tegra_sor_writel(sor, value, SOR_PLL2);
  1461. usleep_range(200, 400);
  1462. value = tegra_sor_readl(sor, SOR_PLL2);
  1463. value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
  1464. value &= ~SOR_PLL2_PORT_POWERDOWN;
  1465. tegra_sor_writel(sor, value, SOR_PLL2);
  1466. usleep_range(20, 100);
  1467. value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
  1468. value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
  1469. SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2;
  1470. tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
  1471. while (true) {
  1472. value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
  1473. if ((value & SOR_LANE_SEQ_CTL_STATE_BUSY) == 0)
  1474. break;
  1475. usleep_range(250, 1000);
  1476. }
  1477. value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
  1478. SOR_LANE_SEQ_CTL_POWER_STATE_UP | SOR_LANE_SEQ_CTL_DELAY(5);
  1479. tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
  1480. while (true) {
  1481. value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
  1482. if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
  1483. break;
  1484. usleep_range(250, 1000);
  1485. }
  1486. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  1487. value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
  1488. value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
  1489. if (mode->clock < 340000)
  1490. value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G2_70;
  1491. else
  1492. value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G5_40;
  1493. value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
  1494. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  1495. value = tegra_sor_readl(sor, SOR_DP_SPARE0);
  1496. value |= SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
  1497. value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
  1498. value |= SOR_DP_SPARE_SEQ_ENABLE;
  1499. tegra_sor_writel(sor, value, SOR_DP_SPARE0);
  1500. value = SOR_SEQ_CTL_PU_PC(0) | SOR_SEQ_CTL_PU_PC_ALT(0) |
  1501. SOR_SEQ_CTL_PD_PC(8) | SOR_SEQ_CTL_PD_PC_ALT(8);
  1502. tegra_sor_writel(sor, value, SOR_SEQ_CTL);
  1503. value = SOR_SEQ_INST_DRIVE_PWM_OUT_LO | SOR_SEQ_INST_HALT |
  1504. SOR_SEQ_INST_WAIT_VSYNC | SOR_SEQ_INST_WAIT(1);
  1505. tegra_sor_writel(sor, value, SOR_SEQ_INST(0));
  1506. tegra_sor_writel(sor, value, SOR_SEQ_INST(8));
  1507. /* program the reference clock */
  1508. value = SOR_REFCLK_DIV_INT(div) | SOR_REFCLK_DIV_FRAC(div);
  1509. tegra_sor_writel(sor, value, SOR_REFCLK);
  1510. /* XXX don't hardcode */
  1511. value = SOR_XBAR_CTRL_LINK1_XSEL(4, 4) |
  1512. SOR_XBAR_CTRL_LINK1_XSEL(3, 3) |
  1513. SOR_XBAR_CTRL_LINK1_XSEL(2, 2) |
  1514. SOR_XBAR_CTRL_LINK1_XSEL(1, 1) |
  1515. SOR_XBAR_CTRL_LINK1_XSEL(0, 0) |
  1516. SOR_XBAR_CTRL_LINK0_XSEL(4, 4) |
  1517. SOR_XBAR_CTRL_LINK0_XSEL(3, 3) |
  1518. SOR_XBAR_CTRL_LINK0_XSEL(2, 0) |
  1519. SOR_XBAR_CTRL_LINK0_XSEL(1, 1) |
  1520. SOR_XBAR_CTRL_LINK0_XSEL(0, 2);
  1521. tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
  1522. tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
  1523. /* switch to parent clock */
  1524. err = tegra_sor_set_parent_clock(sor, sor->clk_parent);
  1525. if (err < 0)
  1526. dev_err(sor->dev, "failed to set parent clock: %d\n", err);
  1527. value = SOR_INPUT_CONTROL_HDMI_SRC_SELECT(dc->pipe);
  1528. /* XXX is this the proper check? */
  1529. if (mode->clock < 75000)
  1530. value |= SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_LIMITED;
  1531. tegra_sor_writel(sor, value, SOR_INPUT_CONTROL);
  1532. max_ac = ((mode->htotal - mode->hdisplay) - SOR_REKEY - 18) / 32;
  1533. value = SOR_HDMI_CTRL_ENABLE | SOR_HDMI_CTRL_MAX_AC_PACKET(max_ac) |
  1534. SOR_HDMI_CTRL_AUDIO_LAYOUT | SOR_HDMI_CTRL_REKEY(SOR_REKEY);
  1535. tegra_sor_writel(sor, value, SOR_HDMI_CTRL);
  1536. /* H_PULSE2 setup */
  1537. pulse_start = h_ref_to_sync + (mode->hsync_end - mode->hsync_start) +
  1538. (mode->htotal - mode->hsync_end) - 10;
  1539. value = PULSE_LAST_END_A | PULSE_QUAL_VACTIVE |
  1540. PULSE_POLARITY_HIGH | PULSE_MODE_NORMAL;
  1541. tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_CONTROL);
  1542. value = PULSE_END(pulse_start + 8) | PULSE_START(pulse_start);
  1543. tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_POSITION_A);
  1544. value = tegra_dc_readl(dc, DC_DISP_DISP_SIGNAL_OPTIONS0);
  1545. value |= H_PULSE2_ENABLE;
  1546. tegra_dc_writel(dc, value, DC_DISP_DISP_SIGNAL_OPTIONS0);
  1547. /* infoframe setup */
  1548. err = tegra_sor_hdmi_setup_avi_infoframe(sor, mode);
  1549. if (err < 0)
  1550. dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
  1551. /* XXX HDMI audio support not implemented yet */
  1552. tegra_sor_hdmi_disable_audio_infoframe(sor);
  1553. /* use single TMDS protocol */
  1554. value = tegra_sor_readl(sor, SOR_STATE1);
  1555. value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
  1556. value |= SOR_STATE_ASY_PROTOCOL_SINGLE_TMDS_A;
  1557. tegra_sor_writel(sor, value, SOR_STATE1);
  1558. /* power up pad calibration */
  1559. value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
  1560. value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
  1561. tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
  1562. /* production settings */
  1563. settings = tegra_sor_hdmi_find_settings(sor, mode->clock * 1000);
  1564. if (!settings) {
  1565. dev_err(sor->dev, "no settings for pixel clock %d Hz\n",
  1566. mode->clock * 1000);
  1567. return;
  1568. }
  1569. value = tegra_sor_readl(sor, SOR_PLL0);
  1570. value &= ~SOR_PLL0_ICHPMP_MASK;
  1571. value &= ~SOR_PLL0_VCOCAP_MASK;
  1572. value |= SOR_PLL0_ICHPMP(settings->ichpmp);
  1573. value |= SOR_PLL0_VCOCAP(settings->vcocap);
  1574. tegra_sor_writel(sor, value, SOR_PLL0);
  1575. tegra_sor_dp_term_calibrate(sor);
  1576. value = tegra_sor_readl(sor, SOR_PLL1);
  1577. value &= ~SOR_PLL1_LOADADJ_MASK;
  1578. value |= SOR_PLL1_LOADADJ(settings->loadadj);
  1579. tegra_sor_writel(sor, value, SOR_PLL1);
  1580. value = tegra_sor_readl(sor, SOR_PLL3);
  1581. value &= ~SOR_PLL3_BG_VREF_LEVEL_MASK;
  1582. value |= SOR_PLL3_BG_VREF_LEVEL(settings->bg_vref);
  1583. tegra_sor_writel(sor, value, SOR_PLL3);
  1584. value = settings->drive_current[0] << 24 |
  1585. settings->drive_current[1] << 16 |
  1586. settings->drive_current[2] << 8 |
  1587. settings->drive_current[3] << 0;
  1588. tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
  1589. value = settings->preemphasis[0] << 24 |
  1590. settings->preemphasis[1] << 16 |
  1591. settings->preemphasis[2] << 8 |
  1592. settings->preemphasis[3] << 0;
  1593. tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
  1594. value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
  1595. value &= ~SOR_DP_PADCTL_TX_PU_MASK;
  1596. value |= SOR_DP_PADCTL_TX_PU_ENABLE;
  1597. value |= SOR_DP_PADCTL_TX_PU(settings->tx_pu);
  1598. tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
  1599. /* power down pad calibration */
  1600. value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
  1601. value |= SOR_DP_PADCTL_PAD_CAL_PD;
  1602. tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
  1603. /* miscellaneous display controller settings */
  1604. value = VSYNC_H_POSITION(1);
  1605. tegra_dc_writel(dc, value, DC_DISP_DISP_TIMING_OPTIONS);
  1606. value = tegra_dc_readl(dc, DC_DISP_DISP_COLOR_CONTROL);
  1607. value &= ~DITHER_CONTROL_MASK;
  1608. value &= ~BASE_COLOR_SIZE_MASK;
  1609. switch (info->bpc) {
  1610. case 6:
  1611. value |= BASE_COLOR_SIZE_666;
  1612. break;
  1613. case 8:
  1614. value |= BASE_COLOR_SIZE_888;
  1615. break;
  1616. default:
  1617. WARN(1, "%u bits-per-color not supported\n", info->bpc);
  1618. break;
  1619. }
  1620. tegra_dc_writel(dc, value, DC_DISP_DISP_COLOR_CONTROL);
  1621. err = tegra_sor_power_up(sor, 250);
  1622. if (err < 0)
  1623. dev_err(sor->dev, "failed to power up SOR: %d\n", err);
  1624. /* configure mode */
  1625. value = tegra_sor_readl(sor, SOR_STATE1);
  1626. value &= ~SOR_STATE_ASY_PIXELDEPTH_MASK;
  1627. value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
  1628. value &= ~SOR_STATE_ASY_OWNER_MASK;
  1629. value |= SOR_STATE_ASY_CRC_MODE_COMPLETE |
  1630. SOR_STATE_ASY_OWNER(dc->pipe + 1);
  1631. if (mode->flags & DRM_MODE_FLAG_PHSYNC)
  1632. value &= ~SOR_STATE_ASY_HSYNCPOL;
  1633. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  1634. value |= SOR_STATE_ASY_HSYNCPOL;
  1635. if (mode->flags & DRM_MODE_FLAG_PVSYNC)
  1636. value &= ~SOR_STATE_ASY_VSYNCPOL;
  1637. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  1638. value |= SOR_STATE_ASY_VSYNCPOL;
  1639. switch (info->bpc) {
  1640. case 8:
  1641. value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
  1642. break;
  1643. case 6:
  1644. value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
  1645. break;
  1646. default:
  1647. BUG();
  1648. break;
  1649. }
  1650. tegra_sor_writel(sor, value, SOR_STATE1);
  1651. value = tegra_sor_readl(sor, SOR_HEAD_STATE0(dc->pipe));
  1652. value &= ~SOR_HEAD_STATE_RANGECOMPRESS_MASK;
  1653. value &= ~SOR_HEAD_STATE_DYNRANGE_MASK;
  1654. tegra_sor_writel(sor, value, SOR_HEAD_STATE0(dc->pipe));
  1655. value = tegra_sor_readl(sor, SOR_HEAD_STATE0(dc->pipe));
  1656. value &= ~SOR_HEAD_STATE_COLORSPACE_MASK;
  1657. value |= SOR_HEAD_STATE_COLORSPACE_RGB;
  1658. tegra_sor_writel(sor, value, SOR_HEAD_STATE0(dc->pipe));
  1659. /*
  1660. * TODO: The video timing programming below doesn't seem to match the
  1661. * register definitions.
  1662. */
  1663. value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
  1664. tegra_sor_writel(sor, value, SOR_HEAD_STATE1(dc->pipe));
  1665. /* sync end = sync width - 1 */
  1666. vse = mode->vsync_end - mode->vsync_start - 1;
  1667. hse = mode->hsync_end - mode->hsync_start - 1;
  1668. value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
  1669. tegra_sor_writel(sor, value, SOR_HEAD_STATE2(dc->pipe));
  1670. /* blank end = sync end + back porch */
  1671. vbe = vse + (mode->vtotal - mode->vsync_end);
  1672. hbe = hse + (mode->htotal - mode->hsync_end);
  1673. value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
  1674. tegra_sor_writel(sor, value, SOR_HEAD_STATE3(dc->pipe));
  1675. /* blank start = blank end + active */
  1676. vbs = vbe + mode->vdisplay;
  1677. hbs = hbe + mode->hdisplay;
  1678. value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
  1679. tegra_sor_writel(sor, value, SOR_HEAD_STATE4(dc->pipe));
  1680. tegra_sor_writel(sor, 0x1, SOR_HEAD_STATE5(dc->pipe));
  1681. tegra_sor_update(sor);
  1682. err = tegra_sor_attach(sor);
  1683. if (err < 0)
  1684. dev_err(sor->dev, "failed to attach SOR: %d\n", err);
  1685. /* enable display to SOR clock and generate HDMI preamble */
  1686. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  1687. value |= SOR1_ENABLE | SOR1_TIMING_CYA;
  1688. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  1689. tegra_dc_commit(dc);
  1690. err = tegra_sor_wakeup(sor);
  1691. if (err < 0)
  1692. dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
  1693. }
  1694. static const struct drm_encoder_helper_funcs tegra_sor_hdmi_helpers = {
  1695. .disable = tegra_sor_hdmi_disable,
  1696. .enable = tegra_sor_hdmi_enable,
  1697. .atomic_check = tegra_sor_encoder_atomic_check,
  1698. };
  1699. static int tegra_sor_init(struct host1x_client *client)
  1700. {
  1701. struct drm_device *drm = dev_get_drvdata(client->parent);
  1702. const struct drm_encoder_helper_funcs *helpers = NULL;
  1703. struct tegra_sor *sor = host1x_client_to_sor(client);
  1704. int connector = DRM_MODE_CONNECTOR_Unknown;
  1705. int encoder = DRM_MODE_ENCODER_NONE;
  1706. int err;
  1707. if (!sor->aux) {
  1708. if (sor->soc->supports_hdmi) {
  1709. connector = DRM_MODE_CONNECTOR_HDMIA;
  1710. encoder = DRM_MODE_ENCODER_TMDS;
  1711. helpers = &tegra_sor_hdmi_helpers;
  1712. } else if (sor->soc->supports_lvds) {
  1713. connector = DRM_MODE_CONNECTOR_LVDS;
  1714. encoder = DRM_MODE_ENCODER_LVDS;
  1715. }
  1716. } else {
  1717. if (sor->soc->supports_edp) {
  1718. connector = DRM_MODE_CONNECTOR_eDP;
  1719. encoder = DRM_MODE_ENCODER_TMDS;
  1720. helpers = &tegra_sor_edp_helpers;
  1721. } else if (sor->soc->supports_dp) {
  1722. connector = DRM_MODE_CONNECTOR_DisplayPort;
  1723. encoder = DRM_MODE_ENCODER_TMDS;
  1724. }
  1725. }
  1726. sor->output.dev = sor->dev;
  1727. drm_connector_init(drm, &sor->output.connector,
  1728. &tegra_sor_connector_funcs,
  1729. connector);
  1730. drm_connector_helper_add(&sor->output.connector,
  1731. &tegra_sor_connector_helper_funcs);
  1732. sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
  1733. drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
  1734. encoder, NULL);
  1735. drm_encoder_helper_add(&sor->output.encoder, helpers);
  1736. drm_mode_connector_attach_encoder(&sor->output.connector,
  1737. &sor->output.encoder);
  1738. drm_connector_register(&sor->output.connector);
  1739. err = tegra_output_init(drm, &sor->output);
  1740. if (err < 0) {
  1741. dev_err(client->dev, "failed to initialize output: %d\n", err);
  1742. return err;
  1743. }
  1744. sor->output.encoder.possible_crtcs = 0x3;
  1745. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  1746. err = tegra_sor_debugfs_init(sor, drm->primary);
  1747. if (err < 0)
  1748. dev_err(sor->dev, "debugfs setup failed: %d\n", err);
  1749. }
  1750. if (sor->aux) {
  1751. err = drm_dp_aux_attach(sor->aux, &sor->output);
  1752. if (err < 0) {
  1753. dev_err(sor->dev, "failed to attach DP: %d\n", err);
  1754. return err;
  1755. }
  1756. }
  1757. /*
  1758. * XXX: Remove this reset once proper hand-over from firmware to
  1759. * kernel is possible.
  1760. */
  1761. err = reset_control_assert(sor->rst);
  1762. if (err < 0) {
  1763. dev_err(sor->dev, "failed to assert SOR reset: %d\n", err);
  1764. return err;
  1765. }
  1766. err = clk_prepare_enable(sor->clk);
  1767. if (err < 0) {
  1768. dev_err(sor->dev, "failed to enable clock: %d\n", err);
  1769. return err;
  1770. }
  1771. usleep_range(1000, 3000);
  1772. err = reset_control_deassert(sor->rst);
  1773. if (err < 0) {
  1774. dev_err(sor->dev, "failed to deassert SOR reset: %d\n", err);
  1775. return err;
  1776. }
  1777. err = clk_prepare_enable(sor->clk_safe);
  1778. if (err < 0)
  1779. return err;
  1780. err = clk_prepare_enable(sor->clk_dp);
  1781. if (err < 0)
  1782. return err;
  1783. return 0;
  1784. }
  1785. static int tegra_sor_exit(struct host1x_client *client)
  1786. {
  1787. struct tegra_sor *sor = host1x_client_to_sor(client);
  1788. int err;
  1789. tegra_output_exit(&sor->output);
  1790. if (sor->aux) {
  1791. err = drm_dp_aux_detach(sor->aux);
  1792. if (err < 0) {
  1793. dev_err(sor->dev, "failed to detach DP: %d\n", err);
  1794. return err;
  1795. }
  1796. }
  1797. clk_disable_unprepare(sor->clk_safe);
  1798. clk_disable_unprepare(sor->clk_dp);
  1799. clk_disable_unprepare(sor->clk);
  1800. if (IS_ENABLED(CONFIG_DEBUG_FS))
  1801. tegra_sor_debugfs_exit(sor);
  1802. return 0;
  1803. }
  1804. static const struct host1x_client_ops sor_client_ops = {
  1805. .init = tegra_sor_init,
  1806. .exit = tegra_sor_exit,
  1807. };
  1808. static const struct tegra_sor_ops tegra_sor_edp_ops = {
  1809. .name = "eDP",
  1810. };
  1811. static int tegra_sor_hdmi_probe(struct tegra_sor *sor)
  1812. {
  1813. int err;
  1814. sor->avdd_io_supply = devm_regulator_get(sor->dev, "avdd-io");
  1815. if (IS_ERR(sor->avdd_io_supply)) {
  1816. dev_err(sor->dev, "cannot get AVDD I/O supply: %ld\n",
  1817. PTR_ERR(sor->avdd_io_supply));
  1818. return PTR_ERR(sor->avdd_io_supply);
  1819. }
  1820. err = regulator_enable(sor->avdd_io_supply);
  1821. if (err < 0) {
  1822. dev_err(sor->dev, "failed to enable AVDD I/O supply: %d\n",
  1823. err);
  1824. return err;
  1825. }
  1826. sor->vdd_pll_supply = devm_regulator_get(sor->dev, "vdd-pll");
  1827. if (IS_ERR(sor->vdd_pll_supply)) {
  1828. dev_err(sor->dev, "cannot get VDD PLL supply: %ld\n",
  1829. PTR_ERR(sor->vdd_pll_supply));
  1830. return PTR_ERR(sor->vdd_pll_supply);
  1831. }
  1832. err = regulator_enable(sor->vdd_pll_supply);
  1833. if (err < 0) {
  1834. dev_err(sor->dev, "failed to enable VDD PLL supply: %d\n",
  1835. err);
  1836. return err;
  1837. }
  1838. sor->hdmi_supply = devm_regulator_get(sor->dev, "hdmi");
  1839. if (IS_ERR(sor->hdmi_supply)) {
  1840. dev_err(sor->dev, "cannot get HDMI supply: %ld\n",
  1841. PTR_ERR(sor->hdmi_supply));
  1842. return PTR_ERR(sor->hdmi_supply);
  1843. }
  1844. err = regulator_enable(sor->hdmi_supply);
  1845. if (err < 0) {
  1846. dev_err(sor->dev, "failed to enable HDMI supply: %d\n", err);
  1847. return err;
  1848. }
  1849. return 0;
  1850. }
  1851. static int tegra_sor_hdmi_remove(struct tegra_sor *sor)
  1852. {
  1853. regulator_disable(sor->hdmi_supply);
  1854. regulator_disable(sor->vdd_pll_supply);
  1855. regulator_disable(sor->avdd_io_supply);
  1856. return 0;
  1857. }
  1858. static const struct tegra_sor_ops tegra_sor_hdmi_ops = {
  1859. .name = "HDMI",
  1860. .probe = tegra_sor_hdmi_probe,
  1861. .remove = tegra_sor_hdmi_remove,
  1862. };
  1863. static const struct tegra_sor_soc tegra124_sor = {
  1864. .supports_edp = true,
  1865. .supports_lvds = true,
  1866. .supports_hdmi = false,
  1867. .supports_dp = false,
  1868. };
  1869. static const struct tegra_sor_soc tegra210_sor = {
  1870. .supports_edp = true,
  1871. .supports_lvds = false,
  1872. .supports_hdmi = false,
  1873. .supports_dp = false,
  1874. };
  1875. static const struct tegra_sor_soc tegra210_sor1 = {
  1876. .supports_edp = false,
  1877. .supports_lvds = false,
  1878. .supports_hdmi = true,
  1879. .supports_dp = true,
  1880. .num_settings = ARRAY_SIZE(tegra210_sor_hdmi_defaults),
  1881. .settings = tegra210_sor_hdmi_defaults,
  1882. };
  1883. static const struct of_device_id tegra_sor_of_match[] = {
  1884. { .compatible = "nvidia,tegra210-sor1", .data = &tegra210_sor1 },
  1885. { .compatible = "nvidia,tegra210-sor", .data = &tegra210_sor },
  1886. { .compatible = "nvidia,tegra124-sor", .data = &tegra124_sor },
  1887. { },
  1888. };
  1889. MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
  1890. static int tegra_sor_probe(struct platform_device *pdev)
  1891. {
  1892. const struct of_device_id *match;
  1893. struct device_node *np;
  1894. struct tegra_sor *sor;
  1895. struct resource *regs;
  1896. int err;
  1897. match = of_match_device(tegra_sor_of_match, &pdev->dev);
  1898. sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
  1899. if (!sor)
  1900. return -ENOMEM;
  1901. sor->output.dev = sor->dev = &pdev->dev;
  1902. sor->soc = match->data;
  1903. sor->settings = devm_kmemdup(&pdev->dev, sor->soc->settings,
  1904. sor->soc->num_settings *
  1905. sizeof(*sor->settings),
  1906. GFP_KERNEL);
  1907. if (!sor->settings)
  1908. return -ENOMEM;
  1909. sor->num_settings = sor->soc->num_settings;
  1910. np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
  1911. if (np) {
  1912. sor->aux = drm_dp_aux_find_by_of_node(np);
  1913. of_node_put(np);
  1914. if (!sor->aux)
  1915. return -EPROBE_DEFER;
  1916. }
  1917. if (!sor->aux) {
  1918. if (sor->soc->supports_hdmi) {
  1919. sor->ops = &tegra_sor_hdmi_ops;
  1920. } else if (sor->soc->supports_lvds) {
  1921. dev_err(&pdev->dev, "LVDS not supported yet\n");
  1922. return -ENODEV;
  1923. } else {
  1924. dev_err(&pdev->dev, "unknown (non-DP) support\n");
  1925. return -ENODEV;
  1926. }
  1927. } else {
  1928. if (sor->soc->supports_edp) {
  1929. sor->ops = &tegra_sor_edp_ops;
  1930. } else if (sor->soc->supports_dp) {
  1931. dev_err(&pdev->dev, "DisplayPort not supported yet\n");
  1932. return -ENODEV;
  1933. } else {
  1934. dev_err(&pdev->dev, "unknown (DP) support\n");
  1935. return -ENODEV;
  1936. }
  1937. }
  1938. err = tegra_output_probe(&sor->output);
  1939. if (err < 0) {
  1940. dev_err(&pdev->dev, "failed to probe output: %d\n", err);
  1941. return err;
  1942. }
  1943. if (sor->ops && sor->ops->probe) {
  1944. err = sor->ops->probe(sor);
  1945. if (err < 0) {
  1946. dev_err(&pdev->dev, "failed to probe %s: %d\n",
  1947. sor->ops->name, err);
  1948. goto output;
  1949. }
  1950. }
  1951. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1952. sor->regs = devm_ioremap_resource(&pdev->dev, regs);
  1953. if (IS_ERR(sor->regs)) {
  1954. err = PTR_ERR(sor->regs);
  1955. goto remove;
  1956. }
  1957. sor->rst = devm_reset_control_get(&pdev->dev, "sor");
  1958. if (IS_ERR(sor->rst)) {
  1959. err = PTR_ERR(sor->rst);
  1960. dev_err(&pdev->dev, "failed to get reset control: %d\n", err);
  1961. goto remove;
  1962. }
  1963. sor->clk = devm_clk_get(&pdev->dev, NULL);
  1964. if (IS_ERR(sor->clk)) {
  1965. err = PTR_ERR(sor->clk);
  1966. dev_err(&pdev->dev, "failed to get module clock: %d\n", err);
  1967. goto remove;
  1968. }
  1969. sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
  1970. if (IS_ERR(sor->clk_parent)) {
  1971. err = PTR_ERR(sor->clk_parent);
  1972. dev_err(&pdev->dev, "failed to get parent clock: %d\n", err);
  1973. goto remove;
  1974. }
  1975. sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
  1976. if (IS_ERR(sor->clk_safe)) {
  1977. err = PTR_ERR(sor->clk_safe);
  1978. dev_err(&pdev->dev, "failed to get safe clock: %d\n", err);
  1979. goto remove;
  1980. }
  1981. sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
  1982. if (IS_ERR(sor->clk_dp)) {
  1983. err = PTR_ERR(sor->clk_dp);
  1984. dev_err(&pdev->dev, "failed to get DP clock: %d\n", err);
  1985. goto remove;
  1986. }
  1987. INIT_LIST_HEAD(&sor->client.list);
  1988. sor->client.ops = &sor_client_ops;
  1989. sor->client.dev = &pdev->dev;
  1990. err = host1x_client_register(&sor->client);
  1991. if (err < 0) {
  1992. dev_err(&pdev->dev, "failed to register host1x client: %d\n",
  1993. err);
  1994. goto remove;
  1995. }
  1996. platform_set_drvdata(pdev, sor);
  1997. return 0;
  1998. remove:
  1999. if (sor->ops && sor->ops->remove)
  2000. sor->ops->remove(sor);
  2001. output:
  2002. tegra_output_remove(&sor->output);
  2003. return err;
  2004. }
  2005. static int tegra_sor_remove(struct platform_device *pdev)
  2006. {
  2007. struct tegra_sor *sor = platform_get_drvdata(pdev);
  2008. int err;
  2009. err = host1x_client_unregister(&sor->client);
  2010. if (err < 0) {
  2011. dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
  2012. err);
  2013. return err;
  2014. }
  2015. if (sor->ops && sor->ops->remove) {
  2016. err = sor->ops->remove(sor);
  2017. if (err < 0)
  2018. dev_err(&pdev->dev, "failed to remove SOR: %d\n", err);
  2019. }
  2020. tegra_output_remove(&sor->output);
  2021. return 0;
  2022. }
  2023. struct platform_driver tegra_sor_driver = {
  2024. .driver = {
  2025. .name = "tegra-sor",
  2026. .of_match_table = tegra_sor_of_match,
  2027. },
  2028. .probe = tegra_sor_probe,
  2029. .remove = tegra_sor_remove,
  2030. };