sor.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541
  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 tegra_dpaux *dpaux;
  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 = tegra_dpaux_prepare(sor->dpaux, 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 = tegra_dpaux_train(sor->dpaux, 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 = tegra_dpaux_train(sor->dpaux, 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 = tegra_dpaux_train(sor->dpaux, 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->dpaux)
  859. return tegra_dpaux_detect(sor->dpaux);
  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->dpaux)
  877. tegra_dpaux_enable(sor->dpaux);
  878. err = tegra_output_connector_get_modes(connector);
  879. if (sor->dpaux)
  880. tegra_dpaux_disable(sor->dpaux);
  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. .best_encoder = tegra_output_connector_best_encoder,
  893. };
  894. static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
  895. .destroy = tegra_output_encoder_destroy,
  896. };
  897. static void tegra_sor_edp_disable(struct drm_encoder *encoder)
  898. {
  899. struct tegra_output *output = encoder_to_output(encoder);
  900. struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
  901. struct tegra_sor *sor = to_sor(output);
  902. u32 value;
  903. int err;
  904. if (output->panel)
  905. drm_panel_disable(output->panel);
  906. err = tegra_sor_detach(sor);
  907. if (err < 0)
  908. dev_err(sor->dev, "failed to detach SOR: %d\n", err);
  909. tegra_sor_writel(sor, 0, SOR_STATE1);
  910. tegra_sor_update(sor);
  911. /*
  912. * The following accesses registers of the display controller, so make
  913. * sure it's only executed when the output is attached to one.
  914. */
  915. if (dc) {
  916. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  917. value &= ~SOR_ENABLE;
  918. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  919. tegra_dc_commit(dc);
  920. }
  921. err = tegra_sor_power_down(sor);
  922. if (err < 0)
  923. dev_err(sor->dev, "failed to power down SOR: %d\n", err);
  924. if (sor->dpaux) {
  925. err = tegra_dpaux_disable(sor->dpaux);
  926. if (err < 0)
  927. dev_err(sor->dev, "failed to disable DP: %d\n", err);
  928. }
  929. err = tegra_io_rail_power_off(TEGRA_IO_RAIL_LVDS);
  930. if (err < 0)
  931. dev_err(sor->dev, "failed to power off I/O rail: %d\n", err);
  932. if (output->panel)
  933. drm_panel_unprepare(output->panel);
  934. reset_control_assert(sor->rst);
  935. clk_disable_unprepare(sor->clk);
  936. }
  937. #if 0
  938. static int calc_h_ref_to_sync(const struct drm_display_mode *mode,
  939. unsigned int *value)
  940. {
  941. unsigned int hfp, hsw, hbp, a = 0, b;
  942. hfp = mode->hsync_start - mode->hdisplay;
  943. hsw = mode->hsync_end - mode->hsync_start;
  944. hbp = mode->htotal - mode->hsync_end;
  945. pr_info("hfp: %u, hsw: %u, hbp: %u\n", hfp, hsw, hbp);
  946. b = hfp - 1;
  947. pr_info("a: %u, b: %u\n", a, b);
  948. pr_info("a + hsw + hbp = %u\n", a + hsw + hbp);
  949. if (a + hsw + hbp <= 11) {
  950. a = 1 + 11 - hsw - hbp;
  951. pr_info("a: %u\n", a);
  952. }
  953. if (a > b)
  954. return -EINVAL;
  955. if (hsw < 1)
  956. return -EINVAL;
  957. if (mode->hdisplay < 16)
  958. return -EINVAL;
  959. if (value) {
  960. if (b > a && a % 2)
  961. *value = a + 1;
  962. else
  963. *value = a;
  964. }
  965. return 0;
  966. }
  967. #endif
  968. static void tegra_sor_edp_enable(struct drm_encoder *encoder)
  969. {
  970. struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
  971. struct tegra_output *output = encoder_to_output(encoder);
  972. struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
  973. unsigned int vbe, vse, hbe, hse, vbs, hbs, i;
  974. struct tegra_sor *sor = to_sor(output);
  975. struct tegra_sor_config config;
  976. struct drm_dp_link link;
  977. struct drm_dp_aux *aux;
  978. int err = 0;
  979. u32 value;
  980. err = clk_prepare_enable(sor->clk);
  981. if (err < 0)
  982. dev_err(sor->dev, "failed to enable clock: %d\n", err);
  983. reset_control_deassert(sor->rst);
  984. if (output->panel)
  985. drm_panel_prepare(output->panel);
  986. /* FIXME: properly convert to struct drm_dp_aux */
  987. aux = (struct drm_dp_aux *)sor->dpaux;
  988. if (sor->dpaux) {
  989. err = tegra_dpaux_enable(sor->dpaux);
  990. if (err < 0)
  991. dev_err(sor->dev, "failed to enable DP: %d\n", err);
  992. err = drm_dp_link_probe(aux, &link);
  993. if (err < 0) {
  994. dev_err(sor->dev, "failed to probe eDP link: %d\n",
  995. err);
  996. return;
  997. }
  998. }
  999. err = clk_set_parent(sor->clk, sor->clk_safe);
  1000. if (err < 0)
  1001. dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
  1002. memset(&config, 0, sizeof(config));
  1003. config.bits_per_pixel = output->connector.display_info.bpc * 3;
  1004. err = tegra_sor_calc_config(sor, mode, &config, &link);
  1005. if (err < 0)
  1006. dev_err(sor->dev, "failed to compute link configuration: %d\n",
  1007. err);
  1008. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  1009. value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
  1010. value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
  1011. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  1012. value = tegra_sor_readl(sor, SOR_PLL2);
  1013. value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
  1014. tegra_sor_writel(sor, value, SOR_PLL2);
  1015. usleep_range(20, 100);
  1016. value = tegra_sor_readl(sor, SOR_PLL3);
  1017. value |= SOR_PLL3_PLL_VDD_MODE_3V3;
  1018. tegra_sor_writel(sor, value, SOR_PLL3);
  1019. value = SOR_PLL0_ICHPMP(0xf) | SOR_PLL0_VCOCAP_RST |
  1020. SOR_PLL0_PLLREG_LEVEL_V45 | SOR_PLL0_RESISTOR_EXT;
  1021. tegra_sor_writel(sor, value, SOR_PLL0);
  1022. value = tegra_sor_readl(sor, SOR_PLL2);
  1023. value |= SOR_PLL2_SEQ_PLLCAPPD;
  1024. value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
  1025. value |= SOR_PLL2_LVDS_ENABLE;
  1026. tegra_sor_writel(sor, value, SOR_PLL2);
  1027. value = SOR_PLL1_TERM_COMPOUT | SOR_PLL1_TMDS_TERM;
  1028. tegra_sor_writel(sor, value, SOR_PLL1);
  1029. while (true) {
  1030. value = tegra_sor_readl(sor, SOR_PLL2);
  1031. if ((value & SOR_PLL2_SEQ_PLLCAPPD_ENFORCE) == 0)
  1032. break;
  1033. usleep_range(250, 1000);
  1034. }
  1035. value = tegra_sor_readl(sor, SOR_PLL2);
  1036. value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
  1037. value &= ~SOR_PLL2_PORT_POWERDOWN;
  1038. tegra_sor_writel(sor, value, SOR_PLL2);
  1039. /*
  1040. * power up
  1041. */
  1042. /* set safe link bandwidth (1.62 Gbps) */
  1043. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  1044. value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
  1045. value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
  1046. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  1047. /* step 1 */
  1048. value = tegra_sor_readl(sor, SOR_PLL2);
  1049. value |= SOR_PLL2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL2_PORT_POWERDOWN |
  1050. SOR_PLL2_BANDGAP_POWERDOWN;
  1051. tegra_sor_writel(sor, value, SOR_PLL2);
  1052. value = tegra_sor_readl(sor, SOR_PLL0);
  1053. value |= SOR_PLL0_VCOPD | SOR_PLL0_PWR;
  1054. tegra_sor_writel(sor, value, SOR_PLL0);
  1055. value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
  1056. value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
  1057. tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
  1058. /* step 2 */
  1059. err = tegra_io_rail_power_on(TEGRA_IO_RAIL_LVDS);
  1060. if (err < 0)
  1061. dev_err(sor->dev, "failed to power on I/O rail: %d\n", err);
  1062. usleep_range(5, 100);
  1063. /* step 3 */
  1064. value = tegra_sor_readl(sor, SOR_PLL2);
  1065. value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
  1066. tegra_sor_writel(sor, value, SOR_PLL2);
  1067. usleep_range(20, 100);
  1068. /* step 4 */
  1069. value = tegra_sor_readl(sor, SOR_PLL0);
  1070. value &= ~SOR_PLL0_VCOPD;
  1071. value &= ~SOR_PLL0_PWR;
  1072. tegra_sor_writel(sor, value, SOR_PLL0);
  1073. value = tegra_sor_readl(sor, SOR_PLL2);
  1074. value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
  1075. tegra_sor_writel(sor, value, SOR_PLL2);
  1076. usleep_range(200, 1000);
  1077. /* step 5 */
  1078. value = tegra_sor_readl(sor, SOR_PLL2);
  1079. value &= ~SOR_PLL2_PORT_POWERDOWN;
  1080. tegra_sor_writel(sor, value, SOR_PLL2);
  1081. /* switch to DP clock */
  1082. err = clk_set_parent(sor->clk, sor->clk_dp);
  1083. if (err < 0)
  1084. dev_err(sor->dev, "failed to set DP parent clock: %d\n", err);
  1085. /* power DP lanes */
  1086. value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
  1087. if (link.num_lanes <= 2)
  1088. value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2);
  1089. else
  1090. value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2;
  1091. if (link.num_lanes <= 1)
  1092. value &= ~SOR_DP_PADCTL_PD_TXD_1;
  1093. else
  1094. value |= SOR_DP_PADCTL_PD_TXD_1;
  1095. if (link.num_lanes == 0)
  1096. value &= ~SOR_DP_PADCTL_PD_TXD_0;
  1097. else
  1098. value |= SOR_DP_PADCTL_PD_TXD_0;
  1099. tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
  1100. value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
  1101. value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
  1102. value |= SOR_DP_LINKCTL_LANE_COUNT(link.num_lanes);
  1103. tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
  1104. /* start lane sequencer */
  1105. value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
  1106. SOR_LANE_SEQ_CTL_POWER_STATE_UP;
  1107. tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
  1108. while (true) {
  1109. value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
  1110. if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
  1111. break;
  1112. usleep_range(250, 1000);
  1113. }
  1114. /* set link bandwidth */
  1115. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  1116. value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
  1117. value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
  1118. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  1119. /* set linkctl */
  1120. value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
  1121. value |= SOR_DP_LINKCTL_ENABLE;
  1122. value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
  1123. value |= SOR_DP_LINKCTL_TU_SIZE(config.tu_size);
  1124. value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
  1125. tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
  1126. for (i = 0, value = 0; i < 4; i++) {
  1127. unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
  1128. SOR_DP_TPG_SCRAMBLER_GALIOS |
  1129. SOR_DP_TPG_PATTERN_NONE;
  1130. value = (value << 8) | lane;
  1131. }
  1132. tegra_sor_writel(sor, value, SOR_DP_TPG);
  1133. value = tegra_sor_readl(sor, SOR_DP_CONFIG0);
  1134. value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
  1135. value |= SOR_DP_CONFIG_WATERMARK(config.watermark);
  1136. value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
  1137. value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config.active_count);
  1138. value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
  1139. value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config.active_frac);
  1140. if (config.active_polarity)
  1141. value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
  1142. else
  1143. value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
  1144. value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
  1145. value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
  1146. tegra_sor_writel(sor, value, SOR_DP_CONFIG0);
  1147. value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
  1148. value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
  1149. value |= config.hblank_symbols & 0xffff;
  1150. tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
  1151. value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
  1152. value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
  1153. value |= config.vblank_symbols & 0xffff;
  1154. tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
  1155. /* enable pad calibration logic */
  1156. value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
  1157. value |= SOR_DP_PADCTL_PAD_CAL_PD;
  1158. tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
  1159. if (sor->dpaux) {
  1160. u8 rate, lanes;
  1161. err = drm_dp_link_probe(aux, &link);
  1162. if (err < 0)
  1163. dev_err(sor->dev, "failed to probe eDP link: %d\n",
  1164. err);
  1165. err = drm_dp_link_power_up(aux, &link);
  1166. if (err < 0)
  1167. dev_err(sor->dev, "failed to power up eDP link: %d\n",
  1168. err);
  1169. err = drm_dp_link_configure(aux, &link);
  1170. if (err < 0)
  1171. dev_err(sor->dev, "failed to configure eDP link: %d\n",
  1172. err);
  1173. rate = drm_dp_link_rate_to_bw_code(link.rate);
  1174. lanes = link.num_lanes;
  1175. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  1176. value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
  1177. value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
  1178. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  1179. value = tegra_sor_readl(sor, SOR_DP_LINKCTL0);
  1180. value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
  1181. value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
  1182. if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
  1183. value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
  1184. tegra_sor_writel(sor, value, SOR_DP_LINKCTL0);
  1185. /* disable training pattern generator */
  1186. for (i = 0; i < link.num_lanes; i++) {
  1187. unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
  1188. SOR_DP_TPG_SCRAMBLER_GALIOS |
  1189. SOR_DP_TPG_PATTERN_NONE;
  1190. value = (value << 8) | lane;
  1191. }
  1192. tegra_sor_writel(sor, value, SOR_DP_TPG);
  1193. err = tegra_sor_dp_train_fast(sor, &link);
  1194. if (err < 0) {
  1195. dev_err(sor->dev, "DP fast link training failed: %d\n",
  1196. err);
  1197. }
  1198. dev_dbg(sor->dev, "fast link training succeeded\n");
  1199. }
  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. err = clk_set_parent(sor->clk, sor->clk_safe);
  1439. if (err < 0)
  1440. dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
  1441. div = clk_get_rate(sor->clk) / 1000000 * 4;
  1442. err = tegra_io_rail_power_on(TEGRA_IO_RAIL_HDMI);
  1443. if (err < 0)
  1444. dev_err(sor->dev, "failed to power on HDMI rail: %d\n", err);
  1445. usleep_range(20, 100);
  1446. value = tegra_sor_readl(sor, SOR_PLL2);
  1447. value &= ~SOR_PLL2_BANDGAP_POWERDOWN;
  1448. tegra_sor_writel(sor, value, SOR_PLL2);
  1449. usleep_range(20, 100);
  1450. value = tegra_sor_readl(sor, SOR_PLL3);
  1451. value &= ~SOR_PLL3_PLL_VDD_MODE_3V3;
  1452. tegra_sor_writel(sor, value, SOR_PLL3);
  1453. value = tegra_sor_readl(sor, SOR_PLL0);
  1454. value &= ~SOR_PLL0_VCOPD;
  1455. value &= ~SOR_PLL0_PWR;
  1456. tegra_sor_writel(sor, value, SOR_PLL0);
  1457. value = tegra_sor_readl(sor, SOR_PLL2);
  1458. value &= ~SOR_PLL2_SEQ_PLLCAPPD_ENFORCE;
  1459. tegra_sor_writel(sor, value, SOR_PLL2);
  1460. usleep_range(200, 400);
  1461. value = tegra_sor_readl(sor, SOR_PLL2);
  1462. value &= ~SOR_PLL2_POWERDOWN_OVERRIDE;
  1463. value &= ~SOR_PLL2_PORT_POWERDOWN;
  1464. tegra_sor_writel(sor, value, SOR_PLL2);
  1465. usleep_range(20, 100);
  1466. value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
  1467. value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
  1468. SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2;
  1469. tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
  1470. while (true) {
  1471. value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
  1472. if ((value & SOR_LANE_SEQ_CTL_STATE_BUSY) == 0)
  1473. break;
  1474. usleep_range(250, 1000);
  1475. }
  1476. value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
  1477. SOR_LANE_SEQ_CTL_POWER_STATE_UP | SOR_LANE_SEQ_CTL_DELAY(5);
  1478. tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
  1479. while (true) {
  1480. value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
  1481. if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
  1482. break;
  1483. usleep_range(250, 1000);
  1484. }
  1485. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  1486. value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
  1487. value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
  1488. if (mode->clock < 340000)
  1489. value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G2_70;
  1490. else
  1491. value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G5_40;
  1492. value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_PCLK;
  1493. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  1494. value = tegra_sor_readl(sor, SOR_DP_SPARE0);
  1495. value |= SOR_DP_SPARE_DISP_VIDEO_PREAMBLE;
  1496. value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
  1497. value |= SOR_DP_SPARE_SEQ_ENABLE;
  1498. tegra_sor_writel(sor, value, SOR_DP_SPARE0);
  1499. value = SOR_SEQ_CTL_PU_PC(0) | SOR_SEQ_CTL_PU_PC_ALT(0) |
  1500. SOR_SEQ_CTL_PD_PC(8) | SOR_SEQ_CTL_PD_PC_ALT(8);
  1501. tegra_sor_writel(sor, value, SOR_SEQ_CTL);
  1502. value = SOR_SEQ_INST_DRIVE_PWM_OUT_LO | SOR_SEQ_INST_HALT |
  1503. SOR_SEQ_INST_WAIT_VSYNC | SOR_SEQ_INST_WAIT(1);
  1504. tegra_sor_writel(sor, value, SOR_SEQ_INST(0));
  1505. tegra_sor_writel(sor, value, SOR_SEQ_INST(8));
  1506. /* program the reference clock */
  1507. value = SOR_REFCLK_DIV_INT(div) | SOR_REFCLK_DIV_FRAC(div);
  1508. tegra_sor_writel(sor, value, SOR_REFCLK);
  1509. /* XXX don't hardcode */
  1510. value = SOR_XBAR_CTRL_LINK1_XSEL(4, 4) |
  1511. SOR_XBAR_CTRL_LINK1_XSEL(3, 3) |
  1512. SOR_XBAR_CTRL_LINK1_XSEL(2, 2) |
  1513. SOR_XBAR_CTRL_LINK1_XSEL(1, 1) |
  1514. SOR_XBAR_CTRL_LINK1_XSEL(0, 0) |
  1515. SOR_XBAR_CTRL_LINK0_XSEL(4, 4) |
  1516. SOR_XBAR_CTRL_LINK0_XSEL(3, 3) |
  1517. SOR_XBAR_CTRL_LINK0_XSEL(2, 0) |
  1518. SOR_XBAR_CTRL_LINK0_XSEL(1, 1) |
  1519. SOR_XBAR_CTRL_LINK0_XSEL(0, 2);
  1520. tegra_sor_writel(sor, value, SOR_XBAR_CTRL);
  1521. tegra_sor_writel(sor, 0x00000000, SOR_XBAR_POL);
  1522. err = clk_set_parent(sor->clk, sor->clk_parent);
  1523. if (err < 0)
  1524. dev_err(sor->dev, "failed to set parent clock: %d\n", err);
  1525. value = SOR_INPUT_CONTROL_HDMI_SRC_SELECT(dc->pipe);
  1526. /* XXX is this the proper check? */
  1527. if (mode->clock < 75000)
  1528. value |= SOR_INPUT_CONTROL_ARM_VIDEO_RANGE_LIMITED;
  1529. tegra_sor_writel(sor, value, SOR_INPUT_CONTROL);
  1530. max_ac = ((mode->htotal - mode->hdisplay) - SOR_REKEY - 18) / 32;
  1531. value = SOR_HDMI_CTRL_ENABLE | SOR_HDMI_CTRL_MAX_AC_PACKET(max_ac) |
  1532. SOR_HDMI_CTRL_AUDIO_LAYOUT | SOR_HDMI_CTRL_REKEY(SOR_REKEY);
  1533. tegra_sor_writel(sor, value, SOR_HDMI_CTRL);
  1534. /* H_PULSE2 setup */
  1535. pulse_start = h_ref_to_sync + (mode->hsync_end - mode->hsync_start) +
  1536. (mode->htotal - mode->hsync_end) - 10;
  1537. value = PULSE_LAST_END_A | PULSE_QUAL_VACTIVE |
  1538. PULSE_POLARITY_HIGH | PULSE_MODE_NORMAL;
  1539. tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_CONTROL);
  1540. value = PULSE_END(pulse_start + 8) | PULSE_START(pulse_start);
  1541. tegra_dc_writel(dc, value, DC_DISP_H_PULSE2_POSITION_A);
  1542. value = tegra_dc_readl(dc, DC_DISP_DISP_SIGNAL_OPTIONS0);
  1543. value |= H_PULSE2_ENABLE;
  1544. tegra_dc_writel(dc, value, DC_DISP_DISP_SIGNAL_OPTIONS0);
  1545. /* infoframe setup */
  1546. err = tegra_sor_hdmi_setup_avi_infoframe(sor, mode);
  1547. if (err < 0)
  1548. dev_err(sor->dev, "failed to setup AVI infoframe: %d\n", err);
  1549. /* XXX HDMI audio support not implemented yet */
  1550. tegra_sor_hdmi_disable_audio_infoframe(sor);
  1551. /* use single TMDS protocol */
  1552. value = tegra_sor_readl(sor, SOR_STATE1);
  1553. value &= ~SOR_STATE_ASY_PROTOCOL_MASK;
  1554. value |= SOR_STATE_ASY_PROTOCOL_SINGLE_TMDS_A;
  1555. tegra_sor_writel(sor, value, SOR_STATE1);
  1556. /* power up pad calibration */
  1557. value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
  1558. value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
  1559. tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
  1560. /* production settings */
  1561. settings = tegra_sor_hdmi_find_settings(sor, mode->clock * 1000);
  1562. if (IS_ERR(settings)) {
  1563. dev_err(sor->dev, "no settings for pixel clock %d Hz: %ld\n",
  1564. mode->clock * 1000, PTR_ERR(settings));
  1565. return;
  1566. }
  1567. value = tegra_sor_readl(sor, SOR_PLL0);
  1568. value &= ~SOR_PLL0_ICHPMP_MASK;
  1569. value &= ~SOR_PLL0_VCOCAP_MASK;
  1570. value |= SOR_PLL0_ICHPMP(settings->ichpmp);
  1571. value |= SOR_PLL0_VCOCAP(settings->vcocap);
  1572. tegra_sor_writel(sor, value, SOR_PLL0);
  1573. tegra_sor_dp_term_calibrate(sor);
  1574. value = tegra_sor_readl(sor, SOR_PLL1);
  1575. value &= ~SOR_PLL1_LOADADJ_MASK;
  1576. value |= SOR_PLL1_LOADADJ(settings->loadadj);
  1577. tegra_sor_writel(sor, value, SOR_PLL1);
  1578. value = tegra_sor_readl(sor, SOR_PLL3);
  1579. value &= ~SOR_PLL3_BG_VREF_LEVEL_MASK;
  1580. value |= SOR_PLL3_BG_VREF_LEVEL(settings->bg_vref);
  1581. tegra_sor_writel(sor, value, SOR_PLL3);
  1582. value = settings->drive_current[0] << 24 |
  1583. settings->drive_current[1] << 16 |
  1584. settings->drive_current[2] << 8 |
  1585. settings->drive_current[3] << 0;
  1586. tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT0);
  1587. value = settings->preemphasis[0] << 24 |
  1588. settings->preemphasis[1] << 16 |
  1589. settings->preemphasis[2] << 8 |
  1590. settings->preemphasis[3] << 0;
  1591. tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS0);
  1592. value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
  1593. value &= ~SOR_DP_PADCTL_TX_PU_MASK;
  1594. value |= SOR_DP_PADCTL_TX_PU_ENABLE;
  1595. value |= SOR_DP_PADCTL_TX_PU(settings->tx_pu);
  1596. tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
  1597. /* power down pad calibration */
  1598. value = tegra_sor_readl(sor, SOR_DP_PADCTL0);
  1599. value |= SOR_DP_PADCTL_PAD_CAL_PD;
  1600. tegra_sor_writel(sor, value, SOR_DP_PADCTL0);
  1601. /* miscellaneous display controller settings */
  1602. value = VSYNC_H_POSITION(1);
  1603. tegra_dc_writel(dc, value, DC_DISP_DISP_TIMING_OPTIONS);
  1604. value = tegra_dc_readl(dc, DC_DISP_DISP_COLOR_CONTROL);
  1605. value &= ~DITHER_CONTROL_MASK;
  1606. value &= ~BASE_COLOR_SIZE_MASK;
  1607. switch (info->bpc) {
  1608. case 6:
  1609. value |= BASE_COLOR_SIZE_666;
  1610. break;
  1611. case 8:
  1612. value |= BASE_COLOR_SIZE_888;
  1613. break;
  1614. default:
  1615. WARN(1, "%u bits-per-color not supported\n", info->bpc);
  1616. break;
  1617. }
  1618. tegra_dc_writel(dc, value, DC_DISP_DISP_COLOR_CONTROL);
  1619. err = tegra_sor_power_up(sor, 250);
  1620. if (err < 0)
  1621. dev_err(sor->dev, "failed to power up SOR: %d\n", err);
  1622. /* configure mode */
  1623. value = tegra_sor_readl(sor, SOR_STATE1);
  1624. value &= ~SOR_STATE_ASY_PIXELDEPTH_MASK;
  1625. value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
  1626. value &= ~SOR_STATE_ASY_OWNER_MASK;
  1627. value |= SOR_STATE_ASY_CRC_MODE_COMPLETE |
  1628. SOR_STATE_ASY_OWNER(dc->pipe + 1);
  1629. if (mode->flags & DRM_MODE_FLAG_PHSYNC)
  1630. value &= ~SOR_STATE_ASY_HSYNCPOL;
  1631. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  1632. value |= SOR_STATE_ASY_HSYNCPOL;
  1633. if (mode->flags & DRM_MODE_FLAG_PVSYNC)
  1634. value &= ~SOR_STATE_ASY_VSYNCPOL;
  1635. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  1636. value |= SOR_STATE_ASY_VSYNCPOL;
  1637. switch (info->bpc) {
  1638. case 8:
  1639. value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
  1640. break;
  1641. case 6:
  1642. value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
  1643. break;
  1644. default:
  1645. BUG();
  1646. break;
  1647. }
  1648. tegra_sor_writel(sor, value, SOR_STATE1);
  1649. value = tegra_sor_readl(sor, SOR_HEAD_STATE0(dc->pipe));
  1650. value &= ~SOR_HEAD_STATE_RANGECOMPRESS_MASK;
  1651. value &= ~SOR_HEAD_STATE_DYNRANGE_MASK;
  1652. tegra_sor_writel(sor, value, SOR_HEAD_STATE0(dc->pipe));
  1653. value = tegra_sor_readl(sor, SOR_HEAD_STATE0(dc->pipe));
  1654. value &= ~SOR_HEAD_STATE_COLORSPACE_MASK;
  1655. value |= SOR_HEAD_STATE_COLORSPACE_RGB;
  1656. tegra_sor_writel(sor, value, SOR_HEAD_STATE0(dc->pipe));
  1657. /*
  1658. * TODO: The video timing programming below doesn't seem to match the
  1659. * register definitions.
  1660. */
  1661. value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
  1662. tegra_sor_writel(sor, value, SOR_HEAD_STATE1(dc->pipe));
  1663. /* sync end = sync width - 1 */
  1664. vse = mode->vsync_end - mode->vsync_start - 1;
  1665. hse = mode->hsync_end - mode->hsync_start - 1;
  1666. value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
  1667. tegra_sor_writel(sor, value, SOR_HEAD_STATE2(dc->pipe));
  1668. /* blank end = sync end + back porch */
  1669. vbe = vse + (mode->vtotal - mode->vsync_end);
  1670. hbe = hse + (mode->htotal - mode->hsync_end);
  1671. value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
  1672. tegra_sor_writel(sor, value, SOR_HEAD_STATE3(dc->pipe));
  1673. /* blank start = blank end + active */
  1674. vbs = vbe + mode->vdisplay;
  1675. hbs = hbe + mode->hdisplay;
  1676. value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
  1677. tegra_sor_writel(sor, value, SOR_HEAD_STATE4(dc->pipe));
  1678. tegra_sor_writel(sor, 0x1, SOR_HEAD_STATE5(dc->pipe));
  1679. tegra_sor_update(sor);
  1680. err = tegra_sor_attach(sor);
  1681. if (err < 0)
  1682. dev_err(sor->dev, "failed to attach SOR: %d\n", err);
  1683. /* enable display to SOR clock and generate HDMI preamble */
  1684. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  1685. value |= SOR1_ENABLE | SOR1_TIMING_CYA;
  1686. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  1687. tegra_dc_commit(dc);
  1688. err = tegra_sor_wakeup(sor);
  1689. if (err < 0)
  1690. dev_err(sor->dev, "failed to wakeup SOR: %d\n", err);
  1691. }
  1692. static const struct drm_encoder_helper_funcs tegra_sor_hdmi_helpers = {
  1693. .disable = tegra_sor_hdmi_disable,
  1694. .enable = tegra_sor_hdmi_enable,
  1695. .atomic_check = tegra_sor_encoder_atomic_check,
  1696. };
  1697. static int tegra_sor_init(struct host1x_client *client)
  1698. {
  1699. struct drm_device *drm = dev_get_drvdata(client->parent);
  1700. const struct drm_encoder_helper_funcs *helpers = NULL;
  1701. struct tegra_sor *sor = host1x_client_to_sor(client);
  1702. int connector = DRM_MODE_CONNECTOR_Unknown;
  1703. int encoder = DRM_MODE_ENCODER_NONE;
  1704. int err;
  1705. if (!sor->dpaux) {
  1706. if (sor->soc->supports_hdmi) {
  1707. connector = DRM_MODE_CONNECTOR_HDMIA;
  1708. encoder = DRM_MODE_ENCODER_TMDS;
  1709. helpers = &tegra_sor_hdmi_helpers;
  1710. } else if (sor->soc->supports_lvds) {
  1711. connector = DRM_MODE_CONNECTOR_LVDS;
  1712. encoder = DRM_MODE_ENCODER_LVDS;
  1713. }
  1714. } else {
  1715. if (sor->soc->supports_edp) {
  1716. connector = DRM_MODE_CONNECTOR_eDP;
  1717. encoder = DRM_MODE_ENCODER_TMDS;
  1718. helpers = &tegra_sor_edp_helpers;
  1719. } else if (sor->soc->supports_dp) {
  1720. connector = DRM_MODE_CONNECTOR_DisplayPort;
  1721. encoder = DRM_MODE_ENCODER_TMDS;
  1722. }
  1723. }
  1724. sor->output.dev = sor->dev;
  1725. drm_connector_init(drm, &sor->output.connector,
  1726. &tegra_sor_connector_funcs,
  1727. connector);
  1728. drm_connector_helper_add(&sor->output.connector,
  1729. &tegra_sor_connector_helper_funcs);
  1730. sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
  1731. drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
  1732. encoder);
  1733. drm_encoder_helper_add(&sor->output.encoder, helpers);
  1734. drm_mode_connector_attach_encoder(&sor->output.connector,
  1735. &sor->output.encoder);
  1736. drm_connector_register(&sor->output.connector);
  1737. err = tegra_output_init(drm, &sor->output);
  1738. if (err < 0) {
  1739. dev_err(client->dev, "failed to initialize output: %d\n", err);
  1740. return err;
  1741. }
  1742. sor->output.encoder.possible_crtcs = 0x3;
  1743. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  1744. err = tegra_sor_debugfs_init(sor, drm->primary);
  1745. if (err < 0)
  1746. dev_err(sor->dev, "debugfs setup failed: %d\n", err);
  1747. }
  1748. if (sor->dpaux) {
  1749. err = tegra_dpaux_attach(sor->dpaux, &sor->output);
  1750. if (err < 0) {
  1751. dev_err(sor->dev, "failed to attach DP: %d\n", err);
  1752. return err;
  1753. }
  1754. }
  1755. /*
  1756. * XXX: Remove this reset once proper hand-over from firmware to
  1757. * kernel is possible.
  1758. */
  1759. err = reset_control_assert(sor->rst);
  1760. if (err < 0) {
  1761. dev_err(sor->dev, "failed to assert SOR reset: %d\n", err);
  1762. return err;
  1763. }
  1764. err = clk_prepare_enable(sor->clk);
  1765. if (err < 0) {
  1766. dev_err(sor->dev, "failed to enable clock: %d\n", err);
  1767. return err;
  1768. }
  1769. usleep_range(1000, 3000);
  1770. err = reset_control_deassert(sor->rst);
  1771. if (err < 0) {
  1772. dev_err(sor->dev, "failed to deassert SOR reset: %d\n", err);
  1773. return err;
  1774. }
  1775. err = clk_prepare_enable(sor->clk_safe);
  1776. if (err < 0)
  1777. return err;
  1778. err = clk_prepare_enable(sor->clk_dp);
  1779. if (err < 0)
  1780. return err;
  1781. return 0;
  1782. }
  1783. static int tegra_sor_exit(struct host1x_client *client)
  1784. {
  1785. struct tegra_sor *sor = host1x_client_to_sor(client);
  1786. int err;
  1787. tegra_output_exit(&sor->output);
  1788. if (sor->dpaux) {
  1789. err = tegra_dpaux_detach(sor->dpaux);
  1790. if (err < 0) {
  1791. dev_err(sor->dev, "failed to detach DP: %d\n", err);
  1792. return err;
  1793. }
  1794. }
  1795. clk_disable_unprepare(sor->clk_safe);
  1796. clk_disable_unprepare(sor->clk_dp);
  1797. clk_disable_unprepare(sor->clk);
  1798. if (IS_ENABLED(CONFIG_DEBUG_FS))
  1799. tegra_sor_debugfs_exit(sor);
  1800. return 0;
  1801. }
  1802. static const struct host1x_client_ops sor_client_ops = {
  1803. .init = tegra_sor_init,
  1804. .exit = tegra_sor_exit,
  1805. };
  1806. static const struct tegra_sor_ops tegra_sor_edp_ops = {
  1807. .name = "eDP",
  1808. };
  1809. static int tegra_sor_hdmi_probe(struct tegra_sor *sor)
  1810. {
  1811. int err;
  1812. sor->avdd_io_supply = devm_regulator_get(sor->dev, "avdd-io");
  1813. if (IS_ERR(sor->avdd_io_supply)) {
  1814. dev_err(sor->dev, "cannot get AVDD I/O supply: %ld\n",
  1815. PTR_ERR(sor->avdd_io_supply));
  1816. return PTR_ERR(sor->avdd_io_supply);
  1817. }
  1818. err = regulator_enable(sor->avdd_io_supply);
  1819. if (err < 0) {
  1820. dev_err(sor->dev, "failed to enable AVDD I/O supply: %d\n",
  1821. err);
  1822. return err;
  1823. }
  1824. sor->vdd_pll_supply = devm_regulator_get(sor->dev, "vdd-pll");
  1825. if (IS_ERR(sor->vdd_pll_supply)) {
  1826. dev_err(sor->dev, "cannot get VDD PLL supply: %ld\n",
  1827. PTR_ERR(sor->vdd_pll_supply));
  1828. return PTR_ERR(sor->vdd_pll_supply);
  1829. }
  1830. err = regulator_enable(sor->vdd_pll_supply);
  1831. if (err < 0) {
  1832. dev_err(sor->dev, "failed to enable VDD PLL supply: %d\n",
  1833. err);
  1834. return err;
  1835. }
  1836. sor->hdmi_supply = devm_regulator_get(sor->dev, "hdmi");
  1837. if (IS_ERR(sor->hdmi_supply)) {
  1838. dev_err(sor->dev, "cannot get HDMI supply: %ld\n",
  1839. PTR_ERR(sor->hdmi_supply));
  1840. return PTR_ERR(sor->hdmi_supply);
  1841. }
  1842. err = regulator_enable(sor->hdmi_supply);
  1843. if (err < 0) {
  1844. dev_err(sor->dev, "failed to enable HDMI supply: %d\n", err);
  1845. return err;
  1846. }
  1847. return 0;
  1848. }
  1849. static int tegra_sor_hdmi_remove(struct tegra_sor *sor)
  1850. {
  1851. regulator_disable(sor->hdmi_supply);
  1852. regulator_disable(sor->vdd_pll_supply);
  1853. regulator_disable(sor->avdd_io_supply);
  1854. return 0;
  1855. }
  1856. static const struct tegra_sor_ops tegra_sor_hdmi_ops = {
  1857. .name = "HDMI",
  1858. .probe = tegra_sor_hdmi_probe,
  1859. .remove = tegra_sor_hdmi_remove,
  1860. };
  1861. static const struct tegra_sor_soc tegra124_sor = {
  1862. .supports_edp = true,
  1863. .supports_lvds = true,
  1864. .supports_hdmi = false,
  1865. .supports_dp = false,
  1866. };
  1867. static const struct tegra_sor_soc tegra210_sor = {
  1868. .supports_edp = true,
  1869. .supports_lvds = false,
  1870. .supports_hdmi = false,
  1871. .supports_dp = false,
  1872. };
  1873. static const struct tegra_sor_soc tegra210_sor1 = {
  1874. .supports_edp = false,
  1875. .supports_lvds = false,
  1876. .supports_hdmi = true,
  1877. .supports_dp = true,
  1878. .num_settings = ARRAY_SIZE(tegra210_sor_hdmi_defaults),
  1879. .settings = tegra210_sor_hdmi_defaults,
  1880. };
  1881. static const struct of_device_id tegra_sor_of_match[] = {
  1882. { .compatible = "nvidia,tegra210-sor1", .data = &tegra210_sor1 },
  1883. { .compatible = "nvidia,tegra210-sor", .data = &tegra210_sor },
  1884. { .compatible = "nvidia,tegra124-sor", .data = &tegra124_sor },
  1885. { },
  1886. };
  1887. MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
  1888. static int tegra_sor_probe(struct platform_device *pdev)
  1889. {
  1890. const struct of_device_id *match;
  1891. struct device_node *np;
  1892. struct tegra_sor *sor;
  1893. struct resource *regs;
  1894. int err;
  1895. match = of_match_device(tegra_sor_of_match, &pdev->dev);
  1896. sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
  1897. if (!sor)
  1898. return -ENOMEM;
  1899. sor->output.dev = sor->dev = &pdev->dev;
  1900. sor->soc = match->data;
  1901. sor->settings = devm_kmemdup(&pdev->dev, sor->soc->settings,
  1902. sor->soc->num_settings *
  1903. sizeof(*sor->settings),
  1904. GFP_KERNEL);
  1905. if (!sor->settings)
  1906. return -ENOMEM;
  1907. sor->num_settings = sor->soc->num_settings;
  1908. np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
  1909. if (np) {
  1910. sor->dpaux = tegra_dpaux_find_by_of_node(np);
  1911. of_node_put(np);
  1912. if (!sor->dpaux)
  1913. return -EPROBE_DEFER;
  1914. }
  1915. if (!sor->dpaux) {
  1916. if (sor->soc->supports_hdmi) {
  1917. sor->ops = &tegra_sor_hdmi_ops;
  1918. } else if (sor->soc->supports_lvds) {
  1919. dev_err(&pdev->dev, "LVDS not supported yet\n");
  1920. return -ENODEV;
  1921. } else {
  1922. dev_err(&pdev->dev, "unknown (non-DP) support\n");
  1923. return -ENODEV;
  1924. }
  1925. } else {
  1926. if (sor->soc->supports_edp) {
  1927. sor->ops = &tegra_sor_edp_ops;
  1928. } else if (sor->soc->supports_dp) {
  1929. dev_err(&pdev->dev, "DisplayPort not supported yet\n");
  1930. return -ENODEV;
  1931. } else {
  1932. dev_err(&pdev->dev, "unknown (DP) support\n");
  1933. return -ENODEV;
  1934. }
  1935. }
  1936. err = tegra_output_probe(&sor->output);
  1937. if (err < 0) {
  1938. dev_err(&pdev->dev, "failed to probe output: %d\n", err);
  1939. return err;
  1940. }
  1941. if (sor->ops && sor->ops->probe) {
  1942. err = sor->ops->probe(sor);
  1943. if (err < 0) {
  1944. dev_err(&pdev->dev, "failed to probe %s: %d\n",
  1945. sor->ops->name, err);
  1946. goto output;
  1947. }
  1948. }
  1949. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1950. sor->regs = devm_ioremap_resource(&pdev->dev, regs);
  1951. if (IS_ERR(sor->regs)) {
  1952. err = PTR_ERR(sor->regs);
  1953. goto remove;
  1954. }
  1955. sor->rst = devm_reset_control_get(&pdev->dev, "sor");
  1956. if (IS_ERR(sor->rst)) {
  1957. err = PTR_ERR(sor->rst);
  1958. dev_err(&pdev->dev, "failed to get reset control: %d\n", err);
  1959. goto remove;
  1960. }
  1961. sor->clk = devm_clk_get(&pdev->dev, NULL);
  1962. if (IS_ERR(sor->clk)) {
  1963. err = PTR_ERR(sor->clk);
  1964. dev_err(&pdev->dev, "failed to get module clock: %d\n", err);
  1965. goto remove;
  1966. }
  1967. sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
  1968. if (IS_ERR(sor->clk_parent)) {
  1969. err = PTR_ERR(sor->clk_parent);
  1970. dev_err(&pdev->dev, "failed to get parent clock: %d\n", err);
  1971. goto remove;
  1972. }
  1973. sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
  1974. if (IS_ERR(sor->clk_safe)) {
  1975. err = PTR_ERR(sor->clk_safe);
  1976. dev_err(&pdev->dev, "failed to get safe clock: %d\n", err);
  1977. goto remove;
  1978. }
  1979. sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
  1980. if (IS_ERR(sor->clk_dp)) {
  1981. err = PTR_ERR(sor->clk_dp);
  1982. dev_err(&pdev->dev, "failed to get DP clock: %d\n", err);
  1983. goto remove;
  1984. }
  1985. INIT_LIST_HEAD(&sor->client.list);
  1986. sor->client.ops = &sor_client_ops;
  1987. sor->client.dev = &pdev->dev;
  1988. err = host1x_client_register(&sor->client);
  1989. if (err < 0) {
  1990. dev_err(&pdev->dev, "failed to register host1x client: %d\n",
  1991. err);
  1992. goto remove;
  1993. }
  1994. platform_set_drvdata(pdev, sor);
  1995. return 0;
  1996. remove:
  1997. if (sor->ops && sor->ops->remove)
  1998. sor->ops->remove(sor);
  1999. output:
  2000. tegra_output_remove(&sor->output);
  2001. return err;
  2002. }
  2003. static int tegra_sor_remove(struct platform_device *pdev)
  2004. {
  2005. struct tegra_sor *sor = platform_get_drvdata(pdev);
  2006. int err;
  2007. err = host1x_client_unregister(&sor->client);
  2008. if (err < 0) {
  2009. dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
  2010. err);
  2011. return err;
  2012. }
  2013. if (sor->ops && sor->ops->remove) {
  2014. err = sor->ops->remove(sor);
  2015. if (err < 0)
  2016. dev_err(&pdev->dev, "failed to remove SOR: %d\n", err);
  2017. }
  2018. tegra_output_remove(&sor->output);
  2019. return 0;
  2020. }
  2021. struct platform_driver tegra_sor_driver = {
  2022. .driver = {
  2023. .name = "tegra-sor",
  2024. .of_match_table = tegra_sor_of_match,
  2025. },
  2026. .probe = tegra_sor_probe,
  2027. .remove = tegra_sor_remove,
  2028. };