sor.c 70 KB

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