sor.c 64 KB

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