dc.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514
  1. /*
  2. * Copyright (C) 2012 Avionic Design GmbH
  3. * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/debugfs.h>
  11. #include <linux/iommu.h>
  12. #include <linux/of_device.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/reset.h>
  15. #include <soc/tegra/pmc.h>
  16. #include "dc.h"
  17. #include "drm.h"
  18. #include "gem.h"
  19. #include "hub.h"
  20. #include "plane.h"
  21. #include <drm/drm_atomic.h>
  22. #include <drm/drm_atomic_helper.h>
  23. #include <drm/drm_plane_helper.h>
  24. static void tegra_dc_stats_reset(struct tegra_dc_stats *stats)
  25. {
  26. stats->frames = 0;
  27. stats->vblank = 0;
  28. stats->underflow = 0;
  29. stats->overflow = 0;
  30. }
  31. /* Reads the active copy of a register. */
  32. static u32 tegra_dc_readl_active(struct tegra_dc *dc, unsigned long offset)
  33. {
  34. u32 value;
  35. tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
  36. value = tegra_dc_readl(dc, offset);
  37. tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
  38. return value;
  39. }
  40. static inline unsigned int tegra_plane_offset(struct tegra_plane *plane,
  41. unsigned int offset)
  42. {
  43. if (offset >= 0x500 && offset <= 0x638) {
  44. offset = 0x000 + (offset - 0x500);
  45. return plane->offset + offset;
  46. }
  47. if (offset >= 0x700 && offset <= 0x719) {
  48. offset = 0x180 + (offset - 0x700);
  49. return plane->offset + offset;
  50. }
  51. if (offset >= 0x800 && offset <= 0x839) {
  52. offset = 0x1c0 + (offset - 0x800);
  53. return plane->offset + offset;
  54. }
  55. dev_WARN(plane->dc->dev, "invalid offset: %x\n", offset);
  56. return plane->offset + offset;
  57. }
  58. static inline u32 tegra_plane_readl(struct tegra_plane *plane,
  59. unsigned int offset)
  60. {
  61. return tegra_dc_readl(plane->dc, tegra_plane_offset(plane, offset));
  62. }
  63. static inline void tegra_plane_writel(struct tegra_plane *plane, u32 value,
  64. unsigned int offset)
  65. {
  66. tegra_dc_writel(plane->dc, value, tegra_plane_offset(plane, offset));
  67. }
  68. bool tegra_dc_has_output(struct tegra_dc *dc, struct device *dev)
  69. {
  70. struct device_node *np = dc->dev->of_node;
  71. struct of_phandle_iterator it;
  72. int err;
  73. of_for_each_phandle(&it, err, np, "nvidia,outputs", NULL, 0)
  74. if (it.node == dev->of_node)
  75. return true;
  76. return false;
  77. }
  78. /*
  79. * Double-buffered registers have two copies: ASSEMBLY and ACTIVE. When the
  80. * *_ACT_REQ bits are set the ASSEMBLY copy is latched into the ACTIVE copy.
  81. * Latching happens mmediately if the display controller is in STOP mode or
  82. * on the next frame boundary otherwise.
  83. *
  84. * Triple-buffered registers have three copies: ASSEMBLY, ARM and ACTIVE. The
  85. * ASSEMBLY copy is latched into the ARM copy immediately after *_UPDATE bits
  86. * are written. When the *_ACT_REQ bits are written, the ARM copy is latched
  87. * into the ACTIVE copy, either immediately if the display controller is in
  88. * STOP mode, or at the next frame boundary otherwise.
  89. */
  90. void tegra_dc_commit(struct tegra_dc *dc)
  91. {
  92. tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
  93. tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
  94. }
  95. static inline u32 compute_dda_inc(unsigned int in, unsigned int out, bool v,
  96. unsigned int bpp)
  97. {
  98. fixed20_12 outf = dfixed_init(out);
  99. fixed20_12 inf = dfixed_init(in);
  100. u32 dda_inc;
  101. int max;
  102. if (v)
  103. max = 15;
  104. else {
  105. switch (bpp) {
  106. case 2:
  107. max = 8;
  108. break;
  109. default:
  110. WARN_ON_ONCE(1);
  111. /* fallthrough */
  112. case 4:
  113. max = 4;
  114. break;
  115. }
  116. }
  117. outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
  118. inf.full -= dfixed_const(1);
  119. dda_inc = dfixed_div(inf, outf);
  120. dda_inc = min_t(u32, dda_inc, dfixed_const(max));
  121. return dda_inc;
  122. }
  123. static inline u32 compute_initial_dda(unsigned int in)
  124. {
  125. fixed20_12 inf = dfixed_init(in);
  126. return dfixed_frac(inf);
  127. }
  128. static void tegra_plane_setup_blending_legacy(struct tegra_plane *plane)
  129. {
  130. u32 background[3] = {
  131. BLEND_WEIGHT1(0) | BLEND_WEIGHT0(0) | BLEND_COLOR_KEY_NONE,
  132. BLEND_WEIGHT1(0) | BLEND_WEIGHT0(0) | BLEND_COLOR_KEY_NONE,
  133. BLEND_WEIGHT1(0) | BLEND_WEIGHT0(0) | BLEND_COLOR_KEY_NONE,
  134. };
  135. u32 foreground = BLEND_WEIGHT1(255) | BLEND_WEIGHT0(255) |
  136. BLEND_COLOR_KEY_NONE;
  137. u32 blendnokey = BLEND_WEIGHT1(255) | BLEND_WEIGHT0(255);
  138. struct tegra_plane_state *state;
  139. u32 blending[2];
  140. unsigned int i;
  141. /* disable blending for non-overlapping case */
  142. tegra_plane_writel(plane, blendnokey, DC_WIN_BLEND_NOKEY);
  143. tegra_plane_writel(plane, foreground, DC_WIN_BLEND_1WIN);
  144. state = to_tegra_plane_state(plane->base.state);
  145. if (state->opaque) {
  146. /*
  147. * Since custom fix-weight blending isn't utilized and weight
  148. * of top window is set to max, we can enforce dependent
  149. * blending which in this case results in transparent bottom
  150. * window if top window is opaque and if top window enables
  151. * alpha blending, then bottom window is getting alpha value
  152. * of 1 minus the sum of alpha components of the overlapping
  153. * plane.
  154. */
  155. background[0] |= BLEND_CONTROL_DEPENDENT;
  156. background[1] |= BLEND_CONTROL_DEPENDENT;
  157. /*
  158. * The region where three windows overlap is the intersection
  159. * of the two regions where two windows overlap. It contributes
  160. * to the area if all of the windows on top of it have an alpha
  161. * component.
  162. */
  163. switch (state->base.normalized_zpos) {
  164. case 0:
  165. if (state->blending[0].alpha &&
  166. state->blending[1].alpha)
  167. background[2] |= BLEND_CONTROL_DEPENDENT;
  168. break;
  169. case 1:
  170. background[2] |= BLEND_CONTROL_DEPENDENT;
  171. break;
  172. }
  173. } else {
  174. /*
  175. * Enable alpha blending if pixel format has an alpha
  176. * component.
  177. */
  178. foreground |= BLEND_CONTROL_ALPHA;
  179. /*
  180. * If any of the windows on top of this window is opaque, it
  181. * will completely conceal this window within that area. If
  182. * top window has an alpha component, it is blended over the
  183. * bottom window.
  184. */
  185. for (i = 0; i < 2; i++) {
  186. if (state->blending[i].alpha &&
  187. state->blending[i].top)
  188. background[i] |= BLEND_CONTROL_DEPENDENT;
  189. }
  190. switch (state->base.normalized_zpos) {
  191. case 0:
  192. if (state->blending[0].alpha &&
  193. state->blending[1].alpha)
  194. background[2] |= BLEND_CONTROL_DEPENDENT;
  195. break;
  196. case 1:
  197. /*
  198. * When both middle and topmost windows have an alpha,
  199. * these windows a mixed together and then the result
  200. * is blended over the bottom window.
  201. */
  202. if (state->blending[0].alpha &&
  203. state->blending[0].top)
  204. background[2] |= BLEND_CONTROL_ALPHA;
  205. if (state->blending[1].alpha &&
  206. state->blending[1].top)
  207. background[2] |= BLEND_CONTROL_ALPHA;
  208. break;
  209. }
  210. }
  211. switch (state->base.normalized_zpos) {
  212. case 0:
  213. tegra_plane_writel(plane, background[0], DC_WIN_BLEND_2WIN_X);
  214. tegra_plane_writel(plane, background[1], DC_WIN_BLEND_2WIN_Y);
  215. tegra_plane_writel(plane, background[2], DC_WIN_BLEND_3WIN_XY);
  216. break;
  217. case 1:
  218. /*
  219. * If window B / C is topmost, then X / Y registers are
  220. * matching the order of blending[...] state indices,
  221. * otherwise a swap is required.
  222. */
  223. if (!state->blending[0].top && state->blending[1].top) {
  224. blending[0] = foreground;
  225. blending[1] = background[1];
  226. } else {
  227. blending[0] = background[0];
  228. blending[1] = foreground;
  229. }
  230. tegra_plane_writel(plane, blending[0], DC_WIN_BLEND_2WIN_X);
  231. tegra_plane_writel(plane, blending[1], DC_WIN_BLEND_2WIN_Y);
  232. tegra_plane_writel(plane, background[2], DC_WIN_BLEND_3WIN_XY);
  233. break;
  234. case 2:
  235. tegra_plane_writel(plane, foreground, DC_WIN_BLEND_2WIN_X);
  236. tegra_plane_writel(plane, foreground, DC_WIN_BLEND_2WIN_Y);
  237. tegra_plane_writel(plane, foreground, DC_WIN_BLEND_3WIN_XY);
  238. break;
  239. }
  240. }
  241. static void tegra_plane_setup_blending(struct tegra_plane *plane,
  242. const struct tegra_dc_window *window)
  243. {
  244. u32 value;
  245. value = BLEND_FACTOR_DST_ALPHA_ZERO | BLEND_FACTOR_SRC_ALPHA_K2 |
  246. BLEND_FACTOR_DST_COLOR_NEG_K1_TIMES_SRC |
  247. BLEND_FACTOR_SRC_COLOR_K1_TIMES_SRC;
  248. tegra_plane_writel(plane, value, DC_WIN_BLEND_MATCH_SELECT);
  249. value = BLEND_FACTOR_DST_ALPHA_ZERO | BLEND_FACTOR_SRC_ALPHA_K2 |
  250. BLEND_FACTOR_DST_COLOR_NEG_K1_TIMES_SRC |
  251. BLEND_FACTOR_SRC_COLOR_K1_TIMES_SRC;
  252. tegra_plane_writel(plane, value, DC_WIN_BLEND_NOMATCH_SELECT);
  253. value = K2(255) | K1(255) | WINDOW_LAYER_DEPTH(255 - window->zpos);
  254. tegra_plane_writel(plane, value, DC_WIN_BLEND_LAYER_CONTROL);
  255. }
  256. static bool
  257. tegra_plane_use_horizontal_filtering(struct tegra_plane *plane,
  258. const struct tegra_dc_window *window)
  259. {
  260. struct tegra_dc *dc = plane->dc;
  261. if (window->src.w == window->dst.w)
  262. return false;
  263. if (plane->index == 0 && dc->soc->has_win_a_without_filters)
  264. return false;
  265. return true;
  266. }
  267. static bool
  268. tegra_plane_use_vertical_filtering(struct tegra_plane *plane,
  269. const struct tegra_dc_window *window)
  270. {
  271. struct tegra_dc *dc = plane->dc;
  272. if (window->src.h == window->dst.h)
  273. return false;
  274. if (plane->index == 0 && dc->soc->has_win_a_without_filters)
  275. return false;
  276. if (plane->index == 2 && dc->soc->has_win_c_without_vert_filter)
  277. return false;
  278. return true;
  279. }
  280. static void tegra_dc_setup_window(struct tegra_plane *plane,
  281. const struct tegra_dc_window *window)
  282. {
  283. unsigned h_offset, v_offset, h_size, v_size, h_dda, v_dda, bpp;
  284. struct tegra_dc *dc = plane->dc;
  285. bool yuv, planar;
  286. u32 value;
  287. /*
  288. * For YUV planar modes, the number of bytes per pixel takes into
  289. * account only the luma component and therefore is 1.
  290. */
  291. yuv = tegra_plane_format_is_yuv(window->format, &planar);
  292. if (!yuv)
  293. bpp = window->bits_per_pixel / 8;
  294. else
  295. bpp = planar ? 1 : 2;
  296. tegra_plane_writel(plane, window->format, DC_WIN_COLOR_DEPTH);
  297. tegra_plane_writel(plane, window->swap, DC_WIN_BYTE_SWAP);
  298. value = V_POSITION(window->dst.y) | H_POSITION(window->dst.x);
  299. tegra_plane_writel(plane, value, DC_WIN_POSITION);
  300. value = V_SIZE(window->dst.h) | H_SIZE(window->dst.w);
  301. tegra_plane_writel(plane, value, DC_WIN_SIZE);
  302. h_offset = window->src.x * bpp;
  303. v_offset = window->src.y;
  304. h_size = window->src.w * bpp;
  305. v_size = window->src.h;
  306. value = V_PRESCALED_SIZE(v_size) | H_PRESCALED_SIZE(h_size);
  307. tegra_plane_writel(plane, value, DC_WIN_PRESCALED_SIZE);
  308. /*
  309. * For DDA computations the number of bytes per pixel for YUV planar
  310. * modes needs to take into account all Y, U and V components.
  311. */
  312. if (yuv && planar)
  313. bpp = 2;
  314. h_dda = compute_dda_inc(window->src.w, window->dst.w, false, bpp);
  315. v_dda = compute_dda_inc(window->src.h, window->dst.h, true, bpp);
  316. value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
  317. tegra_plane_writel(plane, value, DC_WIN_DDA_INC);
  318. h_dda = compute_initial_dda(window->src.x);
  319. v_dda = compute_initial_dda(window->src.y);
  320. tegra_plane_writel(plane, h_dda, DC_WIN_H_INITIAL_DDA);
  321. tegra_plane_writel(plane, v_dda, DC_WIN_V_INITIAL_DDA);
  322. tegra_plane_writel(plane, 0, DC_WIN_UV_BUF_STRIDE);
  323. tegra_plane_writel(plane, 0, DC_WIN_BUF_STRIDE);
  324. tegra_plane_writel(plane, window->base[0], DC_WINBUF_START_ADDR);
  325. if (yuv && planar) {
  326. tegra_plane_writel(plane, window->base[1], DC_WINBUF_START_ADDR_U);
  327. tegra_plane_writel(plane, window->base[2], DC_WINBUF_START_ADDR_V);
  328. value = window->stride[1] << 16 | window->stride[0];
  329. tegra_plane_writel(plane, value, DC_WIN_LINE_STRIDE);
  330. } else {
  331. tegra_plane_writel(plane, window->stride[0], DC_WIN_LINE_STRIDE);
  332. }
  333. if (window->bottom_up)
  334. v_offset += window->src.h - 1;
  335. tegra_plane_writel(plane, h_offset, DC_WINBUF_ADDR_H_OFFSET);
  336. tegra_plane_writel(plane, v_offset, DC_WINBUF_ADDR_V_OFFSET);
  337. if (dc->soc->supports_block_linear) {
  338. unsigned long height = window->tiling.value;
  339. switch (window->tiling.mode) {
  340. case TEGRA_BO_TILING_MODE_PITCH:
  341. value = DC_WINBUF_SURFACE_KIND_PITCH;
  342. break;
  343. case TEGRA_BO_TILING_MODE_TILED:
  344. value = DC_WINBUF_SURFACE_KIND_TILED;
  345. break;
  346. case TEGRA_BO_TILING_MODE_BLOCK:
  347. value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
  348. DC_WINBUF_SURFACE_KIND_BLOCK;
  349. break;
  350. }
  351. tegra_plane_writel(plane, value, DC_WINBUF_SURFACE_KIND);
  352. } else {
  353. switch (window->tiling.mode) {
  354. case TEGRA_BO_TILING_MODE_PITCH:
  355. value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
  356. DC_WIN_BUFFER_ADDR_MODE_LINEAR;
  357. break;
  358. case TEGRA_BO_TILING_MODE_TILED:
  359. value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
  360. DC_WIN_BUFFER_ADDR_MODE_TILE;
  361. break;
  362. case TEGRA_BO_TILING_MODE_BLOCK:
  363. /*
  364. * No need to handle this here because ->atomic_check
  365. * will already have filtered it out.
  366. */
  367. break;
  368. }
  369. tegra_plane_writel(plane, value, DC_WIN_BUFFER_ADDR_MODE);
  370. }
  371. value = WIN_ENABLE;
  372. if (yuv) {
  373. /* setup default colorspace conversion coefficients */
  374. tegra_plane_writel(plane, 0x00f0, DC_WIN_CSC_YOF);
  375. tegra_plane_writel(plane, 0x012a, DC_WIN_CSC_KYRGB);
  376. tegra_plane_writel(plane, 0x0000, DC_WIN_CSC_KUR);
  377. tegra_plane_writel(plane, 0x0198, DC_WIN_CSC_KVR);
  378. tegra_plane_writel(plane, 0x039b, DC_WIN_CSC_KUG);
  379. tegra_plane_writel(plane, 0x032f, DC_WIN_CSC_KVG);
  380. tegra_plane_writel(plane, 0x0204, DC_WIN_CSC_KUB);
  381. tegra_plane_writel(plane, 0x0000, DC_WIN_CSC_KVB);
  382. value |= CSC_ENABLE;
  383. } else if (window->bits_per_pixel < 24) {
  384. value |= COLOR_EXPAND;
  385. }
  386. if (window->bottom_up)
  387. value |= V_DIRECTION;
  388. if (tegra_plane_use_horizontal_filtering(plane, window)) {
  389. /*
  390. * Enable horizontal 6-tap filter and set filtering
  391. * coefficients to the default values defined in TRM.
  392. */
  393. tegra_plane_writel(plane, 0x00008000, DC_WIN_H_FILTER_P(0));
  394. tegra_plane_writel(plane, 0x3e087ce1, DC_WIN_H_FILTER_P(1));
  395. tegra_plane_writel(plane, 0x3b117ac1, DC_WIN_H_FILTER_P(2));
  396. tegra_plane_writel(plane, 0x591b73aa, DC_WIN_H_FILTER_P(3));
  397. tegra_plane_writel(plane, 0x57256d9a, DC_WIN_H_FILTER_P(4));
  398. tegra_plane_writel(plane, 0x552f668b, DC_WIN_H_FILTER_P(5));
  399. tegra_plane_writel(plane, 0x73385e8b, DC_WIN_H_FILTER_P(6));
  400. tegra_plane_writel(plane, 0x72435583, DC_WIN_H_FILTER_P(7));
  401. tegra_plane_writel(plane, 0x714c4c8b, DC_WIN_H_FILTER_P(8));
  402. tegra_plane_writel(plane, 0x70554393, DC_WIN_H_FILTER_P(9));
  403. tegra_plane_writel(plane, 0x715e389b, DC_WIN_H_FILTER_P(10));
  404. tegra_plane_writel(plane, 0x71662faa, DC_WIN_H_FILTER_P(11));
  405. tegra_plane_writel(plane, 0x536d25ba, DC_WIN_H_FILTER_P(12));
  406. tegra_plane_writel(plane, 0x55731bca, DC_WIN_H_FILTER_P(13));
  407. tegra_plane_writel(plane, 0x387a11d9, DC_WIN_H_FILTER_P(14));
  408. tegra_plane_writel(plane, 0x3c7c08f1, DC_WIN_H_FILTER_P(15));
  409. value |= H_FILTER;
  410. }
  411. if (tegra_plane_use_vertical_filtering(plane, window)) {
  412. unsigned int i, k;
  413. /*
  414. * Enable vertical 2-tap filter and set filtering
  415. * coefficients to the default values defined in TRM.
  416. */
  417. for (i = 0, k = 128; i < 16; i++, k -= 8)
  418. tegra_plane_writel(plane, k, DC_WIN_V_FILTER_P(i));
  419. value |= V_FILTER;
  420. }
  421. tegra_plane_writel(plane, value, DC_WIN_WIN_OPTIONS);
  422. if (dc->soc->has_legacy_blending)
  423. tegra_plane_setup_blending_legacy(plane);
  424. else
  425. tegra_plane_setup_blending(plane, window);
  426. }
  427. static const u32 tegra20_primary_formats[] = {
  428. DRM_FORMAT_ARGB4444,
  429. DRM_FORMAT_ARGB1555,
  430. DRM_FORMAT_RGB565,
  431. DRM_FORMAT_RGBA5551,
  432. DRM_FORMAT_ABGR8888,
  433. DRM_FORMAT_ARGB8888,
  434. /* non-native formats */
  435. DRM_FORMAT_XRGB1555,
  436. DRM_FORMAT_RGBX5551,
  437. DRM_FORMAT_XBGR8888,
  438. DRM_FORMAT_XRGB8888,
  439. };
  440. static const u64 tegra20_modifiers[] = {
  441. DRM_FORMAT_MOD_LINEAR,
  442. DRM_FORMAT_MOD_NVIDIA_TEGRA_TILED,
  443. DRM_FORMAT_MOD_INVALID
  444. };
  445. static const u32 tegra114_primary_formats[] = {
  446. DRM_FORMAT_ARGB4444,
  447. DRM_FORMAT_ARGB1555,
  448. DRM_FORMAT_RGB565,
  449. DRM_FORMAT_RGBA5551,
  450. DRM_FORMAT_ABGR8888,
  451. DRM_FORMAT_ARGB8888,
  452. /* new on Tegra114 */
  453. DRM_FORMAT_ABGR4444,
  454. DRM_FORMAT_ABGR1555,
  455. DRM_FORMAT_BGRA5551,
  456. DRM_FORMAT_XRGB1555,
  457. DRM_FORMAT_RGBX5551,
  458. DRM_FORMAT_XBGR1555,
  459. DRM_FORMAT_BGRX5551,
  460. DRM_FORMAT_BGR565,
  461. DRM_FORMAT_BGRA8888,
  462. DRM_FORMAT_RGBA8888,
  463. DRM_FORMAT_XRGB8888,
  464. DRM_FORMAT_XBGR8888,
  465. };
  466. static const u32 tegra124_primary_formats[] = {
  467. DRM_FORMAT_ARGB4444,
  468. DRM_FORMAT_ARGB1555,
  469. DRM_FORMAT_RGB565,
  470. DRM_FORMAT_RGBA5551,
  471. DRM_FORMAT_ABGR8888,
  472. DRM_FORMAT_ARGB8888,
  473. /* new on Tegra114 */
  474. DRM_FORMAT_ABGR4444,
  475. DRM_FORMAT_ABGR1555,
  476. DRM_FORMAT_BGRA5551,
  477. DRM_FORMAT_XRGB1555,
  478. DRM_FORMAT_RGBX5551,
  479. DRM_FORMAT_XBGR1555,
  480. DRM_FORMAT_BGRX5551,
  481. DRM_FORMAT_BGR565,
  482. DRM_FORMAT_BGRA8888,
  483. DRM_FORMAT_RGBA8888,
  484. DRM_FORMAT_XRGB8888,
  485. DRM_FORMAT_XBGR8888,
  486. /* new on Tegra124 */
  487. DRM_FORMAT_RGBX8888,
  488. DRM_FORMAT_BGRX8888,
  489. };
  490. static const u64 tegra124_modifiers[] = {
  491. DRM_FORMAT_MOD_LINEAR,
  492. DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(0),
  493. DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(1),
  494. DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(2),
  495. DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(3),
  496. DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(4),
  497. DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(5),
  498. DRM_FORMAT_MOD_INVALID
  499. };
  500. static int tegra_plane_atomic_check(struct drm_plane *plane,
  501. struct drm_plane_state *state)
  502. {
  503. struct tegra_plane_state *plane_state = to_tegra_plane_state(state);
  504. unsigned int rotation = DRM_MODE_ROTATE_0 | DRM_MODE_REFLECT_Y;
  505. struct tegra_bo_tiling *tiling = &plane_state->tiling;
  506. struct tegra_plane *tegra = to_tegra_plane(plane);
  507. struct tegra_dc *dc = to_tegra_dc(state->crtc);
  508. int err;
  509. /* no need for further checks if the plane is being disabled */
  510. if (!state->crtc)
  511. return 0;
  512. err = tegra_plane_format(state->fb->format->format,
  513. &plane_state->format,
  514. &plane_state->swap);
  515. if (err < 0)
  516. return err;
  517. /*
  518. * Tegra20 and Tegra30 are special cases here because they support
  519. * only variants of specific formats with an alpha component, but not
  520. * the corresponding opaque formats. However, the opaque formats can
  521. * be emulated by disabling alpha blending for the plane.
  522. */
  523. if (dc->soc->has_legacy_blending) {
  524. err = tegra_plane_setup_legacy_state(tegra, plane_state);
  525. if (err < 0)
  526. return err;
  527. }
  528. err = tegra_fb_get_tiling(state->fb, tiling);
  529. if (err < 0)
  530. return err;
  531. if (tiling->mode == TEGRA_BO_TILING_MODE_BLOCK &&
  532. !dc->soc->supports_block_linear) {
  533. DRM_ERROR("hardware doesn't support block linear mode\n");
  534. return -EINVAL;
  535. }
  536. rotation = drm_rotation_simplify(state->rotation, rotation);
  537. if (rotation & DRM_MODE_REFLECT_Y)
  538. plane_state->bottom_up = true;
  539. else
  540. plane_state->bottom_up = false;
  541. /*
  542. * Tegra doesn't support different strides for U and V planes so we
  543. * error out if the user tries to display a framebuffer with such a
  544. * configuration.
  545. */
  546. if (state->fb->format->num_planes > 2) {
  547. if (state->fb->pitches[2] != state->fb->pitches[1]) {
  548. DRM_ERROR("unsupported UV-plane configuration\n");
  549. return -EINVAL;
  550. }
  551. }
  552. err = tegra_plane_state_add(tegra, state);
  553. if (err < 0)
  554. return err;
  555. return 0;
  556. }
  557. static void tegra_plane_atomic_disable(struct drm_plane *plane,
  558. struct drm_plane_state *old_state)
  559. {
  560. struct tegra_plane *p = to_tegra_plane(plane);
  561. u32 value;
  562. /* rien ne va plus */
  563. if (!old_state || !old_state->crtc)
  564. return;
  565. value = tegra_plane_readl(p, DC_WIN_WIN_OPTIONS);
  566. value &= ~WIN_ENABLE;
  567. tegra_plane_writel(p, value, DC_WIN_WIN_OPTIONS);
  568. }
  569. static void tegra_plane_atomic_update(struct drm_plane *plane,
  570. struct drm_plane_state *old_state)
  571. {
  572. struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
  573. struct drm_framebuffer *fb = plane->state->fb;
  574. struct tegra_plane *p = to_tegra_plane(plane);
  575. struct tegra_dc_window window;
  576. unsigned int i;
  577. /* rien ne va plus */
  578. if (!plane->state->crtc || !plane->state->fb)
  579. return;
  580. if (!plane->state->visible)
  581. return tegra_plane_atomic_disable(plane, old_state);
  582. memset(&window, 0, sizeof(window));
  583. window.src.x = plane->state->src.x1 >> 16;
  584. window.src.y = plane->state->src.y1 >> 16;
  585. window.src.w = drm_rect_width(&plane->state->src) >> 16;
  586. window.src.h = drm_rect_height(&plane->state->src) >> 16;
  587. window.dst.x = plane->state->dst.x1;
  588. window.dst.y = plane->state->dst.y1;
  589. window.dst.w = drm_rect_width(&plane->state->dst);
  590. window.dst.h = drm_rect_height(&plane->state->dst);
  591. window.bits_per_pixel = fb->format->cpp[0] * 8;
  592. window.bottom_up = tegra_fb_is_bottom_up(fb) || state->bottom_up;
  593. /* copy from state */
  594. window.zpos = plane->state->normalized_zpos;
  595. window.tiling = state->tiling;
  596. window.format = state->format;
  597. window.swap = state->swap;
  598. for (i = 0; i < fb->format->num_planes; i++) {
  599. struct tegra_bo *bo = tegra_fb_get_plane(fb, i);
  600. window.base[i] = bo->paddr + fb->offsets[i];
  601. /*
  602. * Tegra uses a shared stride for UV planes. Framebuffers are
  603. * already checked for this in the tegra_plane_atomic_check()
  604. * function, so it's safe to ignore the V-plane pitch here.
  605. */
  606. if (i < 2)
  607. window.stride[i] = fb->pitches[i];
  608. }
  609. tegra_dc_setup_window(p, &window);
  610. }
  611. static const struct drm_plane_helper_funcs tegra_plane_helper_funcs = {
  612. .atomic_check = tegra_plane_atomic_check,
  613. .atomic_disable = tegra_plane_atomic_disable,
  614. .atomic_update = tegra_plane_atomic_update,
  615. };
  616. static unsigned long tegra_plane_get_possible_crtcs(struct drm_device *drm)
  617. {
  618. /*
  619. * Ideally this would use drm_crtc_mask(), but that would require the
  620. * CRTC to already be in the mode_config's list of CRTCs. However, it
  621. * will only be added to that list in the drm_crtc_init_with_planes()
  622. * (in tegra_dc_init()), which in turn requires registration of these
  623. * planes. So we have ourselves a nice little chicken and egg problem
  624. * here.
  625. *
  626. * We work around this by manually creating the mask from the number
  627. * of CRTCs that have been registered, and should therefore always be
  628. * the same as drm_crtc_index() after registration.
  629. */
  630. return 1 << drm->mode_config.num_crtc;
  631. }
  632. static struct drm_plane *tegra_primary_plane_create(struct drm_device *drm,
  633. struct tegra_dc *dc)
  634. {
  635. unsigned long possible_crtcs = tegra_plane_get_possible_crtcs(drm);
  636. enum drm_plane_type type = DRM_PLANE_TYPE_PRIMARY;
  637. struct tegra_plane *plane;
  638. unsigned int num_formats;
  639. const u64 *modifiers;
  640. const u32 *formats;
  641. int err;
  642. plane = kzalloc(sizeof(*plane), GFP_KERNEL);
  643. if (!plane)
  644. return ERR_PTR(-ENOMEM);
  645. /* Always use window A as primary window */
  646. plane->offset = 0xa00;
  647. plane->index = 0;
  648. plane->dc = dc;
  649. num_formats = dc->soc->num_primary_formats;
  650. formats = dc->soc->primary_formats;
  651. modifiers = dc->soc->modifiers;
  652. err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
  653. &tegra_plane_funcs, formats,
  654. num_formats, modifiers, type, NULL);
  655. if (err < 0) {
  656. kfree(plane);
  657. return ERR_PTR(err);
  658. }
  659. drm_plane_helper_add(&plane->base, &tegra_plane_helper_funcs);
  660. drm_plane_create_zpos_property(&plane->base, plane->index, 0, 255);
  661. err = drm_plane_create_rotation_property(&plane->base,
  662. DRM_MODE_ROTATE_0,
  663. DRM_MODE_ROTATE_0 |
  664. DRM_MODE_REFLECT_Y);
  665. if (err < 0)
  666. dev_err(dc->dev, "failed to create rotation property: %d\n",
  667. err);
  668. return &plane->base;
  669. }
  670. static const u32 tegra_cursor_plane_formats[] = {
  671. DRM_FORMAT_RGBA8888,
  672. };
  673. static int tegra_cursor_atomic_check(struct drm_plane *plane,
  674. struct drm_plane_state *state)
  675. {
  676. struct tegra_plane *tegra = to_tegra_plane(plane);
  677. int err;
  678. /* no need for further checks if the plane is being disabled */
  679. if (!state->crtc)
  680. return 0;
  681. /* scaling not supported for cursor */
  682. if ((state->src_w >> 16 != state->crtc_w) ||
  683. (state->src_h >> 16 != state->crtc_h))
  684. return -EINVAL;
  685. /* only square cursors supported */
  686. if (state->src_w != state->src_h)
  687. return -EINVAL;
  688. if (state->crtc_w != 32 && state->crtc_w != 64 &&
  689. state->crtc_w != 128 && state->crtc_w != 256)
  690. return -EINVAL;
  691. err = tegra_plane_state_add(tegra, state);
  692. if (err < 0)
  693. return err;
  694. return 0;
  695. }
  696. static void tegra_cursor_atomic_update(struct drm_plane *plane,
  697. struct drm_plane_state *old_state)
  698. {
  699. struct tegra_bo *bo = tegra_fb_get_plane(plane->state->fb, 0);
  700. struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
  701. struct drm_plane_state *state = plane->state;
  702. u32 value = CURSOR_CLIP_DISPLAY;
  703. /* rien ne va plus */
  704. if (!plane->state->crtc || !plane->state->fb)
  705. return;
  706. switch (state->crtc_w) {
  707. case 32:
  708. value |= CURSOR_SIZE_32x32;
  709. break;
  710. case 64:
  711. value |= CURSOR_SIZE_64x64;
  712. break;
  713. case 128:
  714. value |= CURSOR_SIZE_128x128;
  715. break;
  716. case 256:
  717. value |= CURSOR_SIZE_256x256;
  718. break;
  719. default:
  720. WARN(1, "cursor size %ux%u not supported\n", state->crtc_w,
  721. state->crtc_h);
  722. return;
  723. }
  724. value |= (bo->paddr >> 10) & 0x3fffff;
  725. tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR);
  726. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  727. value = (bo->paddr >> 32) & 0x3;
  728. tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR_HI);
  729. #endif
  730. /* enable cursor and set blend mode */
  731. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  732. value |= CURSOR_ENABLE;
  733. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  734. value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
  735. value &= ~CURSOR_DST_BLEND_MASK;
  736. value &= ~CURSOR_SRC_BLEND_MASK;
  737. value |= CURSOR_MODE_NORMAL;
  738. value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
  739. value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
  740. value |= CURSOR_ALPHA;
  741. tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
  742. /* position the cursor */
  743. value = (state->crtc_y & 0x3fff) << 16 | (state->crtc_x & 0x3fff);
  744. tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
  745. }
  746. static void tegra_cursor_atomic_disable(struct drm_plane *plane,
  747. struct drm_plane_state *old_state)
  748. {
  749. struct tegra_dc *dc;
  750. u32 value;
  751. /* rien ne va plus */
  752. if (!old_state || !old_state->crtc)
  753. return;
  754. dc = to_tegra_dc(old_state->crtc);
  755. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  756. value &= ~CURSOR_ENABLE;
  757. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  758. }
  759. static const struct drm_plane_helper_funcs tegra_cursor_plane_helper_funcs = {
  760. .atomic_check = tegra_cursor_atomic_check,
  761. .atomic_update = tegra_cursor_atomic_update,
  762. .atomic_disable = tegra_cursor_atomic_disable,
  763. };
  764. static struct drm_plane *tegra_dc_cursor_plane_create(struct drm_device *drm,
  765. struct tegra_dc *dc)
  766. {
  767. unsigned long possible_crtcs = tegra_plane_get_possible_crtcs(drm);
  768. struct tegra_plane *plane;
  769. unsigned int num_formats;
  770. const u32 *formats;
  771. int err;
  772. plane = kzalloc(sizeof(*plane), GFP_KERNEL);
  773. if (!plane)
  774. return ERR_PTR(-ENOMEM);
  775. /*
  776. * This index is kind of fake. The cursor isn't a regular plane, but
  777. * its update and activation request bits in DC_CMD_STATE_CONTROL do
  778. * use the same programming. Setting this fake index here allows the
  779. * code in tegra_add_plane_state() to do the right thing without the
  780. * need to special-casing the cursor plane.
  781. */
  782. plane->index = 6;
  783. plane->dc = dc;
  784. num_formats = ARRAY_SIZE(tegra_cursor_plane_formats);
  785. formats = tegra_cursor_plane_formats;
  786. err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
  787. &tegra_plane_funcs, formats,
  788. num_formats, NULL,
  789. DRM_PLANE_TYPE_CURSOR, NULL);
  790. if (err < 0) {
  791. kfree(plane);
  792. return ERR_PTR(err);
  793. }
  794. drm_plane_helper_add(&plane->base, &tegra_cursor_plane_helper_funcs);
  795. return &plane->base;
  796. }
  797. static const u32 tegra20_overlay_formats[] = {
  798. DRM_FORMAT_ARGB4444,
  799. DRM_FORMAT_ARGB1555,
  800. DRM_FORMAT_RGB565,
  801. DRM_FORMAT_RGBA5551,
  802. DRM_FORMAT_ABGR8888,
  803. DRM_FORMAT_ARGB8888,
  804. /* non-native formats */
  805. DRM_FORMAT_XRGB1555,
  806. DRM_FORMAT_RGBX5551,
  807. DRM_FORMAT_XBGR8888,
  808. DRM_FORMAT_XRGB8888,
  809. /* planar formats */
  810. DRM_FORMAT_UYVY,
  811. DRM_FORMAT_YUYV,
  812. DRM_FORMAT_YUV420,
  813. DRM_FORMAT_YUV422,
  814. };
  815. static const u32 tegra114_overlay_formats[] = {
  816. DRM_FORMAT_ARGB4444,
  817. DRM_FORMAT_ARGB1555,
  818. DRM_FORMAT_RGB565,
  819. DRM_FORMAT_RGBA5551,
  820. DRM_FORMAT_ABGR8888,
  821. DRM_FORMAT_ARGB8888,
  822. /* new on Tegra114 */
  823. DRM_FORMAT_ABGR4444,
  824. DRM_FORMAT_ABGR1555,
  825. DRM_FORMAT_BGRA5551,
  826. DRM_FORMAT_XRGB1555,
  827. DRM_FORMAT_RGBX5551,
  828. DRM_FORMAT_XBGR1555,
  829. DRM_FORMAT_BGRX5551,
  830. DRM_FORMAT_BGR565,
  831. DRM_FORMAT_BGRA8888,
  832. DRM_FORMAT_RGBA8888,
  833. DRM_FORMAT_XRGB8888,
  834. DRM_FORMAT_XBGR8888,
  835. /* planar formats */
  836. DRM_FORMAT_UYVY,
  837. DRM_FORMAT_YUYV,
  838. DRM_FORMAT_YUV420,
  839. DRM_FORMAT_YUV422,
  840. };
  841. static const u32 tegra124_overlay_formats[] = {
  842. DRM_FORMAT_ARGB4444,
  843. DRM_FORMAT_ARGB1555,
  844. DRM_FORMAT_RGB565,
  845. DRM_FORMAT_RGBA5551,
  846. DRM_FORMAT_ABGR8888,
  847. DRM_FORMAT_ARGB8888,
  848. /* new on Tegra114 */
  849. DRM_FORMAT_ABGR4444,
  850. DRM_FORMAT_ABGR1555,
  851. DRM_FORMAT_BGRA5551,
  852. DRM_FORMAT_XRGB1555,
  853. DRM_FORMAT_RGBX5551,
  854. DRM_FORMAT_XBGR1555,
  855. DRM_FORMAT_BGRX5551,
  856. DRM_FORMAT_BGR565,
  857. DRM_FORMAT_BGRA8888,
  858. DRM_FORMAT_RGBA8888,
  859. DRM_FORMAT_XRGB8888,
  860. DRM_FORMAT_XBGR8888,
  861. /* new on Tegra124 */
  862. DRM_FORMAT_RGBX8888,
  863. DRM_FORMAT_BGRX8888,
  864. /* planar formats */
  865. DRM_FORMAT_UYVY,
  866. DRM_FORMAT_YUYV,
  867. DRM_FORMAT_YUV420,
  868. DRM_FORMAT_YUV422,
  869. };
  870. static struct drm_plane *tegra_dc_overlay_plane_create(struct drm_device *drm,
  871. struct tegra_dc *dc,
  872. unsigned int index,
  873. bool cursor)
  874. {
  875. unsigned long possible_crtcs = tegra_plane_get_possible_crtcs(drm);
  876. struct tegra_plane *plane;
  877. unsigned int num_formats;
  878. enum drm_plane_type type;
  879. const u32 *formats;
  880. int err;
  881. plane = kzalloc(sizeof(*plane), GFP_KERNEL);
  882. if (!plane)
  883. return ERR_PTR(-ENOMEM);
  884. plane->offset = 0xa00 + 0x200 * index;
  885. plane->index = index;
  886. plane->dc = dc;
  887. num_formats = dc->soc->num_overlay_formats;
  888. formats = dc->soc->overlay_formats;
  889. if (!cursor)
  890. type = DRM_PLANE_TYPE_OVERLAY;
  891. else
  892. type = DRM_PLANE_TYPE_CURSOR;
  893. err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
  894. &tegra_plane_funcs, formats,
  895. num_formats, NULL, type, NULL);
  896. if (err < 0) {
  897. kfree(plane);
  898. return ERR_PTR(err);
  899. }
  900. drm_plane_helper_add(&plane->base, &tegra_plane_helper_funcs);
  901. drm_plane_create_zpos_property(&plane->base, plane->index, 0, 255);
  902. err = drm_plane_create_rotation_property(&plane->base,
  903. DRM_MODE_ROTATE_0,
  904. DRM_MODE_ROTATE_0 |
  905. DRM_MODE_REFLECT_Y);
  906. if (err < 0)
  907. dev_err(dc->dev, "failed to create rotation property: %d\n",
  908. err);
  909. return &plane->base;
  910. }
  911. static struct drm_plane *tegra_dc_add_shared_planes(struct drm_device *drm,
  912. struct tegra_dc *dc)
  913. {
  914. struct drm_plane *plane, *primary = NULL;
  915. unsigned int i, j;
  916. for (i = 0; i < dc->soc->num_wgrps; i++) {
  917. const struct tegra_windowgroup_soc *wgrp = &dc->soc->wgrps[i];
  918. if (wgrp->dc == dc->pipe) {
  919. for (j = 0; j < wgrp->num_windows; j++) {
  920. unsigned int index = wgrp->windows[j];
  921. plane = tegra_shared_plane_create(drm, dc,
  922. wgrp->index,
  923. index);
  924. if (IS_ERR(plane))
  925. return plane;
  926. /*
  927. * Choose the first shared plane owned by this
  928. * head as the primary plane.
  929. */
  930. if (!primary) {
  931. plane->type = DRM_PLANE_TYPE_PRIMARY;
  932. primary = plane;
  933. }
  934. }
  935. }
  936. }
  937. return primary;
  938. }
  939. static struct drm_plane *tegra_dc_add_planes(struct drm_device *drm,
  940. struct tegra_dc *dc)
  941. {
  942. struct drm_plane *planes[2], *primary;
  943. unsigned int planes_num;
  944. unsigned int i;
  945. int err;
  946. primary = tegra_primary_plane_create(drm, dc);
  947. if (IS_ERR(primary))
  948. return primary;
  949. if (dc->soc->supports_cursor)
  950. planes_num = 2;
  951. else
  952. planes_num = 1;
  953. for (i = 0; i < planes_num; i++) {
  954. planes[i] = tegra_dc_overlay_plane_create(drm, dc, 1 + i,
  955. false);
  956. if (IS_ERR(planes[i])) {
  957. err = PTR_ERR(planes[i]);
  958. while (i--)
  959. tegra_plane_funcs.destroy(planes[i]);
  960. tegra_plane_funcs.destroy(primary);
  961. return ERR_PTR(err);
  962. }
  963. }
  964. return primary;
  965. }
  966. static void tegra_dc_destroy(struct drm_crtc *crtc)
  967. {
  968. drm_crtc_cleanup(crtc);
  969. }
  970. static void tegra_crtc_reset(struct drm_crtc *crtc)
  971. {
  972. struct tegra_dc_state *state;
  973. if (crtc->state)
  974. __drm_atomic_helper_crtc_destroy_state(crtc->state);
  975. kfree(crtc->state);
  976. crtc->state = NULL;
  977. state = kzalloc(sizeof(*state), GFP_KERNEL);
  978. if (state) {
  979. crtc->state = &state->base;
  980. crtc->state->crtc = crtc;
  981. }
  982. drm_crtc_vblank_reset(crtc);
  983. }
  984. static struct drm_crtc_state *
  985. tegra_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
  986. {
  987. struct tegra_dc_state *state = to_dc_state(crtc->state);
  988. struct tegra_dc_state *copy;
  989. copy = kmalloc(sizeof(*copy), GFP_KERNEL);
  990. if (!copy)
  991. return NULL;
  992. __drm_atomic_helper_crtc_duplicate_state(crtc, &copy->base);
  993. copy->clk = state->clk;
  994. copy->pclk = state->pclk;
  995. copy->div = state->div;
  996. copy->planes = state->planes;
  997. return &copy->base;
  998. }
  999. static void tegra_crtc_atomic_destroy_state(struct drm_crtc *crtc,
  1000. struct drm_crtc_state *state)
  1001. {
  1002. __drm_atomic_helper_crtc_destroy_state(state);
  1003. kfree(state);
  1004. }
  1005. #define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
  1006. static const struct debugfs_reg32 tegra_dc_regs[] = {
  1007. DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT),
  1008. DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL),
  1009. DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT_ERROR),
  1010. DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT),
  1011. DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL),
  1012. DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT_ERROR),
  1013. DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT),
  1014. DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL),
  1015. DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT_ERROR),
  1016. DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT),
  1017. DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL),
  1018. DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT_ERROR),
  1019. DEBUGFS_REG32(DC_CMD_CONT_SYNCPT_VSYNC),
  1020. DEBUGFS_REG32(DC_CMD_DISPLAY_COMMAND_OPTION0),
  1021. DEBUGFS_REG32(DC_CMD_DISPLAY_COMMAND),
  1022. DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE),
  1023. DEBUGFS_REG32(DC_CMD_DISPLAY_POWER_CONTROL),
  1024. DEBUGFS_REG32(DC_CMD_INT_STATUS),
  1025. DEBUGFS_REG32(DC_CMD_INT_MASK),
  1026. DEBUGFS_REG32(DC_CMD_INT_ENABLE),
  1027. DEBUGFS_REG32(DC_CMD_INT_TYPE),
  1028. DEBUGFS_REG32(DC_CMD_INT_POLARITY),
  1029. DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE1),
  1030. DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE2),
  1031. DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE3),
  1032. DEBUGFS_REG32(DC_CMD_STATE_ACCESS),
  1033. DEBUGFS_REG32(DC_CMD_STATE_CONTROL),
  1034. DEBUGFS_REG32(DC_CMD_DISPLAY_WINDOW_HEADER),
  1035. DEBUGFS_REG32(DC_CMD_REG_ACT_CONTROL),
  1036. DEBUGFS_REG32(DC_COM_CRC_CONTROL),
  1037. DEBUGFS_REG32(DC_COM_CRC_CHECKSUM),
  1038. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(0)),
  1039. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(1)),
  1040. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(2)),
  1041. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(3)),
  1042. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(0)),
  1043. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(1)),
  1044. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(2)),
  1045. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(3)),
  1046. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(0)),
  1047. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(1)),
  1048. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(2)),
  1049. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(3)),
  1050. DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(0)),
  1051. DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(1)),
  1052. DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(2)),
  1053. DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(3)),
  1054. DEBUGFS_REG32(DC_COM_PIN_INPUT_DATA(0)),
  1055. DEBUGFS_REG32(DC_COM_PIN_INPUT_DATA(1)),
  1056. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(0)),
  1057. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(1)),
  1058. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(2)),
  1059. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(3)),
  1060. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(4)),
  1061. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(5)),
  1062. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(6)),
  1063. DEBUGFS_REG32(DC_COM_PIN_MISC_CONTROL),
  1064. DEBUGFS_REG32(DC_COM_PIN_PM0_CONTROL),
  1065. DEBUGFS_REG32(DC_COM_PIN_PM0_DUTY_CYCLE),
  1066. DEBUGFS_REG32(DC_COM_PIN_PM1_CONTROL),
  1067. DEBUGFS_REG32(DC_COM_PIN_PM1_DUTY_CYCLE),
  1068. DEBUGFS_REG32(DC_COM_SPI_CONTROL),
  1069. DEBUGFS_REG32(DC_COM_SPI_START_BYTE),
  1070. DEBUGFS_REG32(DC_COM_HSPI_WRITE_DATA_AB),
  1071. DEBUGFS_REG32(DC_COM_HSPI_WRITE_DATA_CD),
  1072. DEBUGFS_REG32(DC_COM_HSPI_CS_DC),
  1073. DEBUGFS_REG32(DC_COM_SCRATCH_REGISTER_A),
  1074. DEBUGFS_REG32(DC_COM_SCRATCH_REGISTER_B),
  1075. DEBUGFS_REG32(DC_COM_GPIO_CTRL),
  1076. DEBUGFS_REG32(DC_COM_GPIO_DEBOUNCE_COUNTER),
  1077. DEBUGFS_REG32(DC_COM_CRC_CHECKSUM_LATCHED),
  1078. DEBUGFS_REG32(DC_DISP_DISP_SIGNAL_OPTIONS0),
  1079. DEBUGFS_REG32(DC_DISP_DISP_SIGNAL_OPTIONS1),
  1080. DEBUGFS_REG32(DC_DISP_DISP_WIN_OPTIONS),
  1081. DEBUGFS_REG32(DC_DISP_DISP_MEM_HIGH_PRIORITY),
  1082. DEBUGFS_REG32(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER),
  1083. DEBUGFS_REG32(DC_DISP_DISP_TIMING_OPTIONS),
  1084. DEBUGFS_REG32(DC_DISP_REF_TO_SYNC),
  1085. DEBUGFS_REG32(DC_DISP_SYNC_WIDTH),
  1086. DEBUGFS_REG32(DC_DISP_BACK_PORCH),
  1087. DEBUGFS_REG32(DC_DISP_ACTIVE),
  1088. DEBUGFS_REG32(DC_DISP_FRONT_PORCH),
  1089. DEBUGFS_REG32(DC_DISP_H_PULSE0_CONTROL),
  1090. DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_A),
  1091. DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_B),
  1092. DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_C),
  1093. DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_D),
  1094. DEBUGFS_REG32(DC_DISP_H_PULSE1_CONTROL),
  1095. DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_A),
  1096. DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_B),
  1097. DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_C),
  1098. DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_D),
  1099. DEBUGFS_REG32(DC_DISP_H_PULSE2_CONTROL),
  1100. DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_A),
  1101. DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_B),
  1102. DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_C),
  1103. DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_D),
  1104. DEBUGFS_REG32(DC_DISP_V_PULSE0_CONTROL),
  1105. DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_A),
  1106. DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_B),
  1107. DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_C),
  1108. DEBUGFS_REG32(DC_DISP_V_PULSE1_CONTROL),
  1109. DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_A),
  1110. DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_B),
  1111. DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_C),
  1112. DEBUGFS_REG32(DC_DISP_V_PULSE2_CONTROL),
  1113. DEBUGFS_REG32(DC_DISP_V_PULSE2_POSITION_A),
  1114. DEBUGFS_REG32(DC_DISP_V_PULSE3_CONTROL),
  1115. DEBUGFS_REG32(DC_DISP_V_PULSE3_POSITION_A),
  1116. DEBUGFS_REG32(DC_DISP_M0_CONTROL),
  1117. DEBUGFS_REG32(DC_DISP_M1_CONTROL),
  1118. DEBUGFS_REG32(DC_DISP_DI_CONTROL),
  1119. DEBUGFS_REG32(DC_DISP_PP_CONTROL),
  1120. DEBUGFS_REG32(DC_DISP_PP_SELECT_A),
  1121. DEBUGFS_REG32(DC_DISP_PP_SELECT_B),
  1122. DEBUGFS_REG32(DC_DISP_PP_SELECT_C),
  1123. DEBUGFS_REG32(DC_DISP_PP_SELECT_D),
  1124. DEBUGFS_REG32(DC_DISP_DISP_CLOCK_CONTROL),
  1125. DEBUGFS_REG32(DC_DISP_DISP_INTERFACE_CONTROL),
  1126. DEBUGFS_REG32(DC_DISP_DISP_COLOR_CONTROL),
  1127. DEBUGFS_REG32(DC_DISP_SHIFT_CLOCK_OPTIONS),
  1128. DEBUGFS_REG32(DC_DISP_DATA_ENABLE_OPTIONS),
  1129. DEBUGFS_REG32(DC_DISP_SERIAL_INTERFACE_OPTIONS),
  1130. DEBUGFS_REG32(DC_DISP_LCD_SPI_OPTIONS),
  1131. DEBUGFS_REG32(DC_DISP_BORDER_COLOR),
  1132. DEBUGFS_REG32(DC_DISP_COLOR_KEY0_LOWER),
  1133. DEBUGFS_REG32(DC_DISP_COLOR_KEY0_UPPER),
  1134. DEBUGFS_REG32(DC_DISP_COLOR_KEY1_LOWER),
  1135. DEBUGFS_REG32(DC_DISP_COLOR_KEY1_UPPER),
  1136. DEBUGFS_REG32(DC_DISP_CURSOR_FOREGROUND),
  1137. DEBUGFS_REG32(DC_DISP_CURSOR_BACKGROUND),
  1138. DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR),
  1139. DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR_NS),
  1140. DEBUGFS_REG32(DC_DISP_CURSOR_POSITION),
  1141. DEBUGFS_REG32(DC_DISP_CURSOR_POSITION_NS),
  1142. DEBUGFS_REG32(DC_DISP_INIT_SEQ_CONTROL),
  1143. DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_A),
  1144. DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_B),
  1145. DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_C),
  1146. DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_D),
  1147. DEBUGFS_REG32(DC_DISP_DC_MCCIF_FIFOCTRL),
  1148. DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY0A_HYST),
  1149. DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY0B_HYST),
  1150. DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY1A_HYST),
  1151. DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY1B_HYST),
  1152. DEBUGFS_REG32(DC_DISP_DAC_CRT_CTRL),
  1153. DEBUGFS_REG32(DC_DISP_DISP_MISC_CONTROL),
  1154. DEBUGFS_REG32(DC_DISP_SD_CONTROL),
  1155. DEBUGFS_REG32(DC_DISP_SD_CSC_COEFF),
  1156. DEBUGFS_REG32(DC_DISP_SD_LUT(0)),
  1157. DEBUGFS_REG32(DC_DISP_SD_LUT(1)),
  1158. DEBUGFS_REG32(DC_DISP_SD_LUT(2)),
  1159. DEBUGFS_REG32(DC_DISP_SD_LUT(3)),
  1160. DEBUGFS_REG32(DC_DISP_SD_LUT(4)),
  1161. DEBUGFS_REG32(DC_DISP_SD_LUT(5)),
  1162. DEBUGFS_REG32(DC_DISP_SD_LUT(6)),
  1163. DEBUGFS_REG32(DC_DISP_SD_LUT(7)),
  1164. DEBUGFS_REG32(DC_DISP_SD_LUT(8)),
  1165. DEBUGFS_REG32(DC_DISP_SD_FLICKER_CONTROL),
  1166. DEBUGFS_REG32(DC_DISP_DC_PIXEL_COUNT),
  1167. DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(0)),
  1168. DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(1)),
  1169. DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(2)),
  1170. DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(3)),
  1171. DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(4)),
  1172. DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(5)),
  1173. DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(6)),
  1174. DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(7)),
  1175. DEBUGFS_REG32(DC_DISP_SD_BL_TF(0)),
  1176. DEBUGFS_REG32(DC_DISP_SD_BL_TF(1)),
  1177. DEBUGFS_REG32(DC_DISP_SD_BL_TF(2)),
  1178. DEBUGFS_REG32(DC_DISP_SD_BL_TF(3)),
  1179. DEBUGFS_REG32(DC_DISP_SD_BL_CONTROL),
  1180. DEBUGFS_REG32(DC_DISP_SD_HW_K_VALUES),
  1181. DEBUGFS_REG32(DC_DISP_SD_MAN_K_VALUES),
  1182. DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR_HI),
  1183. DEBUGFS_REG32(DC_DISP_BLEND_CURSOR_CONTROL),
  1184. DEBUGFS_REG32(DC_WIN_WIN_OPTIONS),
  1185. DEBUGFS_REG32(DC_WIN_BYTE_SWAP),
  1186. DEBUGFS_REG32(DC_WIN_BUFFER_CONTROL),
  1187. DEBUGFS_REG32(DC_WIN_COLOR_DEPTH),
  1188. DEBUGFS_REG32(DC_WIN_POSITION),
  1189. DEBUGFS_REG32(DC_WIN_SIZE),
  1190. DEBUGFS_REG32(DC_WIN_PRESCALED_SIZE),
  1191. DEBUGFS_REG32(DC_WIN_H_INITIAL_DDA),
  1192. DEBUGFS_REG32(DC_WIN_V_INITIAL_DDA),
  1193. DEBUGFS_REG32(DC_WIN_DDA_INC),
  1194. DEBUGFS_REG32(DC_WIN_LINE_STRIDE),
  1195. DEBUGFS_REG32(DC_WIN_BUF_STRIDE),
  1196. DEBUGFS_REG32(DC_WIN_UV_BUF_STRIDE),
  1197. DEBUGFS_REG32(DC_WIN_BUFFER_ADDR_MODE),
  1198. DEBUGFS_REG32(DC_WIN_DV_CONTROL),
  1199. DEBUGFS_REG32(DC_WIN_BLEND_NOKEY),
  1200. DEBUGFS_REG32(DC_WIN_BLEND_1WIN),
  1201. DEBUGFS_REG32(DC_WIN_BLEND_2WIN_X),
  1202. DEBUGFS_REG32(DC_WIN_BLEND_2WIN_Y),
  1203. DEBUGFS_REG32(DC_WIN_BLEND_3WIN_XY),
  1204. DEBUGFS_REG32(DC_WIN_HP_FETCH_CONTROL),
  1205. DEBUGFS_REG32(DC_WINBUF_START_ADDR),
  1206. DEBUGFS_REG32(DC_WINBUF_START_ADDR_NS),
  1207. DEBUGFS_REG32(DC_WINBUF_START_ADDR_U),
  1208. DEBUGFS_REG32(DC_WINBUF_START_ADDR_U_NS),
  1209. DEBUGFS_REG32(DC_WINBUF_START_ADDR_V),
  1210. DEBUGFS_REG32(DC_WINBUF_START_ADDR_V_NS),
  1211. DEBUGFS_REG32(DC_WINBUF_ADDR_H_OFFSET),
  1212. DEBUGFS_REG32(DC_WINBUF_ADDR_H_OFFSET_NS),
  1213. DEBUGFS_REG32(DC_WINBUF_ADDR_V_OFFSET),
  1214. DEBUGFS_REG32(DC_WINBUF_ADDR_V_OFFSET_NS),
  1215. DEBUGFS_REG32(DC_WINBUF_UFLOW_STATUS),
  1216. DEBUGFS_REG32(DC_WINBUF_AD_UFLOW_STATUS),
  1217. DEBUGFS_REG32(DC_WINBUF_BD_UFLOW_STATUS),
  1218. DEBUGFS_REG32(DC_WINBUF_CD_UFLOW_STATUS),
  1219. };
  1220. static int tegra_dc_show_regs(struct seq_file *s, void *data)
  1221. {
  1222. struct drm_info_node *node = s->private;
  1223. struct tegra_dc *dc = node->info_ent->data;
  1224. unsigned int i;
  1225. int err = 0;
  1226. drm_modeset_lock(&dc->base.mutex, NULL);
  1227. if (!dc->base.state->active) {
  1228. err = -EBUSY;
  1229. goto unlock;
  1230. }
  1231. for (i = 0; i < ARRAY_SIZE(tegra_dc_regs); i++) {
  1232. unsigned int offset = tegra_dc_regs[i].offset;
  1233. seq_printf(s, "%-40s %#05x %08x\n", tegra_dc_regs[i].name,
  1234. offset, tegra_dc_readl(dc, offset));
  1235. }
  1236. unlock:
  1237. drm_modeset_unlock(&dc->base.mutex);
  1238. return err;
  1239. }
  1240. static int tegra_dc_show_crc(struct seq_file *s, void *data)
  1241. {
  1242. struct drm_info_node *node = s->private;
  1243. struct tegra_dc *dc = node->info_ent->data;
  1244. int err = 0;
  1245. u32 value;
  1246. drm_modeset_lock(&dc->base.mutex, NULL);
  1247. if (!dc->base.state->active) {
  1248. err = -EBUSY;
  1249. goto unlock;
  1250. }
  1251. value = DC_COM_CRC_CONTROL_ACTIVE_DATA | DC_COM_CRC_CONTROL_ENABLE;
  1252. tegra_dc_writel(dc, value, DC_COM_CRC_CONTROL);
  1253. tegra_dc_commit(dc);
  1254. drm_crtc_wait_one_vblank(&dc->base);
  1255. drm_crtc_wait_one_vblank(&dc->base);
  1256. value = tegra_dc_readl(dc, DC_COM_CRC_CHECKSUM);
  1257. seq_printf(s, "%08x\n", value);
  1258. tegra_dc_writel(dc, 0, DC_COM_CRC_CONTROL);
  1259. unlock:
  1260. drm_modeset_unlock(&dc->base.mutex);
  1261. return err;
  1262. }
  1263. static int tegra_dc_show_stats(struct seq_file *s, void *data)
  1264. {
  1265. struct drm_info_node *node = s->private;
  1266. struct tegra_dc *dc = node->info_ent->data;
  1267. seq_printf(s, "frames: %lu\n", dc->stats.frames);
  1268. seq_printf(s, "vblank: %lu\n", dc->stats.vblank);
  1269. seq_printf(s, "underflow: %lu\n", dc->stats.underflow);
  1270. seq_printf(s, "overflow: %lu\n", dc->stats.overflow);
  1271. return 0;
  1272. }
  1273. static struct drm_info_list debugfs_files[] = {
  1274. { "regs", tegra_dc_show_regs, 0, NULL },
  1275. { "crc", tegra_dc_show_crc, 0, NULL },
  1276. { "stats", tegra_dc_show_stats, 0, NULL },
  1277. };
  1278. static int tegra_dc_late_register(struct drm_crtc *crtc)
  1279. {
  1280. unsigned int i, count = ARRAY_SIZE(debugfs_files);
  1281. struct drm_minor *minor = crtc->dev->primary;
  1282. struct dentry *root;
  1283. struct tegra_dc *dc = to_tegra_dc(crtc);
  1284. int err;
  1285. #ifdef CONFIG_DEBUG_FS
  1286. root = crtc->debugfs_entry;
  1287. #else
  1288. root = NULL;
  1289. #endif
  1290. dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
  1291. GFP_KERNEL);
  1292. if (!dc->debugfs_files)
  1293. return -ENOMEM;
  1294. for (i = 0; i < count; i++)
  1295. dc->debugfs_files[i].data = dc;
  1296. err = drm_debugfs_create_files(dc->debugfs_files, count, root, minor);
  1297. if (err < 0)
  1298. goto free;
  1299. return 0;
  1300. free:
  1301. kfree(dc->debugfs_files);
  1302. dc->debugfs_files = NULL;
  1303. return err;
  1304. }
  1305. static void tegra_dc_early_unregister(struct drm_crtc *crtc)
  1306. {
  1307. unsigned int count = ARRAY_SIZE(debugfs_files);
  1308. struct drm_minor *minor = crtc->dev->primary;
  1309. struct tegra_dc *dc = to_tegra_dc(crtc);
  1310. drm_debugfs_remove_files(dc->debugfs_files, count, minor);
  1311. kfree(dc->debugfs_files);
  1312. dc->debugfs_files = NULL;
  1313. }
  1314. static u32 tegra_dc_get_vblank_counter(struct drm_crtc *crtc)
  1315. {
  1316. struct tegra_dc *dc = to_tegra_dc(crtc);
  1317. /* XXX vblank syncpoints don't work with nvdisplay yet */
  1318. if (dc->syncpt && !dc->soc->has_nvdisplay)
  1319. return host1x_syncpt_read(dc->syncpt);
  1320. /* fallback to software emulated VBLANK counter */
  1321. return (u32)drm_crtc_vblank_count(&dc->base);
  1322. }
  1323. static int tegra_dc_enable_vblank(struct drm_crtc *crtc)
  1324. {
  1325. struct tegra_dc *dc = to_tegra_dc(crtc);
  1326. u32 value;
  1327. value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
  1328. value |= VBLANK_INT;
  1329. tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
  1330. return 0;
  1331. }
  1332. static void tegra_dc_disable_vblank(struct drm_crtc *crtc)
  1333. {
  1334. struct tegra_dc *dc = to_tegra_dc(crtc);
  1335. u32 value;
  1336. value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
  1337. value &= ~VBLANK_INT;
  1338. tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
  1339. }
  1340. static const struct drm_crtc_funcs tegra_crtc_funcs = {
  1341. .page_flip = drm_atomic_helper_page_flip,
  1342. .set_config = drm_atomic_helper_set_config,
  1343. .destroy = tegra_dc_destroy,
  1344. .reset = tegra_crtc_reset,
  1345. .atomic_duplicate_state = tegra_crtc_atomic_duplicate_state,
  1346. .atomic_destroy_state = tegra_crtc_atomic_destroy_state,
  1347. .late_register = tegra_dc_late_register,
  1348. .early_unregister = tegra_dc_early_unregister,
  1349. .get_vblank_counter = tegra_dc_get_vblank_counter,
  1350. .enable_vblank = tegra_dc_enable_vblank,
  1351. .disable_vblank = tegra_dc_disable_vblank,
  1352. };
  1353. static int tegra_dc_set_timings(struct tegra_dc *dc,
  1354. struct drm_display_mode *mode)
  1355. {
  1356. unsigned int h_ref_to_sync = 1;
  1357. unsigned int v_ref_to_sync = 1;
  1358. unsigned long value;
  1359. if (!dc->soc->has_nvdisplay) {
  1360. tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
  1361. value = (v_ref_to_sync << 16) | h_ref_to_sync;
  1362. tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
  1363. }
  1364. value = ((mode->vsync_end - mode->vsync_start) << 16) |
  1365. ((mode->hsync_end - mode->hsync_start) << 0);
  1366. tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
  1367. value = ((mode->vtotal - mode->vsync_end) << 16) |
  1368. ((mode->htotal - mode->hsync_end) << 0);
  1369. tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
  1370. value = ((mode->vsync_start - mode->vdisplay) << 16) |
  1371. ((mode->hsync_start - mode->hdisplay) << 0);
  1372. tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
  1373. value = (mode->vdisplay << 16) | mode->hdisplay;
  1374. tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
  1375. return 0;
  1376. }
  1377. /**
  1378. * tegra_dc_state_setup_clock - check clock settings and store them in atomic
  1379. * state
  1380. * @dc: display controller
  1381. * @crtc_state: CRTC atomic state
  1382. * @clk: parent clock for display controller
  1383. * @pclk: pixel clock
  1384. * @div: shift clock divider
  1385. *
  1386. * Returns:
  1387. * 0 on success or a negative error-code on failure.
  1388. */
  1389. int tegra_dc_state_setup_clock(struct tegra_dc *dc,
  1390. struct drm_crtc_state *crtc_state,
  1391. struct clk *clk, unsigned long pclk,
  1392. unsigned int div)
  1393. {
  1394. struct tegra_dc_state *state = to_dc_state(crtc_state);
  1395. if (!clk_has_parent(dc->clk, clk))
  1396. return -EINVAL;
  1397. state->clk = clk;
  1398. state->pclk = pclk;
  1399. state->div = div;
  1400. return 0;
  1401. }
  1402. static void tegra_dc_commit_state(struct tegra_dc *dc,
  1403. struct tegra_dc_state *state)
  1404. {
  1405. u32 value;
  1406. int err;
  1407. err = clk_set_parent(dc->clk, state->clk);
  1408. if (err < 0)
  1409. dev_err(dc->dev, "failed to set parent clock: %d\n", err);
  1410. /*
  1411. * Outputs may not want to change the parent clock rate. This is only
  1412. * relevant to Tegra20 where only a single display PLL is available.
  1413. * Since that PLL would typically be used for HDMI, an internal LVDS
  1414. * panel would need to be driven by some other clock such as PLL_P
  1415. * which is shared with other peripherals. Changing the clock rate
  1416. * should therefore be avoided.
  1417. */
  1418. if (state->pclk > 0) {
  1419. err = clk_set_rate(state->clk, state->pclk);
  1420. if (err < 0)
  1421. dev_err(dc->dev,
  1422. "failed to set clock rate to %lu Hz\n",
  1423. state->pclk);
  1424. }
  1425. DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk),
  1426. state->div);
  1427. DRM_DEBUG_KMS("pclk: %lu\n", state->pclk);
  1428. if (!dc->soc->has_nvdisplay) {
  1429. value = SHIFT_CLK_DIVIDER(state->div) | PIXEL_CLK_DIVIDER_PCD1;
  1430. tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
  1431. }
  1432. err = clk_set_rate(dc->clk, state->pclk);
  1433. if (err < 0)
  1434. dev_err(dc->dev, "failed to set clock %pC to %lu Hz: %d\n",
  1435. dc->clk, state->pclk, err);
  1436. }
  1437. static void tegra_dc_stop(struct tegra_dc *dc)
  1438. {
  1439. u32 value;
  1440. /* stop the display controller */
  1441. value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
  1442. value &= ~DISP_CTRL_MODE_MASK;
  1443. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
  1444. tegra_dc_commit(dc);
  1445. }
  1446. static bool tegra_dc_idle(struct tegra_dc *dc)
  1447. {
  1448. u32 value;
  1449. value = tegra_dc_readl_active(dc, DC_CMD_DISPLAY_COMMAND);
  1450. return (value & DISP_CTRL_MODE_MASK) == 0;
  1451. }
  1452. static int tegra_dc_wait_idle(struct tegra_dc *dc, unsigned long timeout)
  1453. {
  1454. timeout = jiffies + msecs_to_jiffies(timeout);
  1455. while (time_before(jiffies, timeout)) {
  1456. if (tegra_dc_idle(dc))
  1457. return 0;
  1458. usleep_range(1000, 2000);
  1459. }
  1460. dev_dbg(dc->dev, "timeout waiting for DC to become idle\n");
  1461. return -ETIMEDOUT;
  1462. }
  1463. static void tegra_crtc_atomic_disable(struct drm_crtc *crtc,
  1464. struct drm_crtc_state *old_state)
  1465. {
  1466. struct tegra_dc *dc = to_tegra_dc(crtc);
  1467. u32 value;
  1468. if (!tegra_dc_idle(dc)) {
  1469. tegra_dc_stop(dc);
  1470. /*
  1471. * Ignore the return value, there isn't anything useful to do
  1472. * in case this fails.
  1473. */
  1474. tegra_dc_wait_idle(dc, 100);
  1475. }
  1476. /*
  1477. * This should really be part of the RGB encoder driver, but clearing
  1478. * these bits has the side-effect of stopping the display controller.
  1479. * When that happens no VBLANK interrupts will be raised. At the same
  1480. * time the encoder is disabled before the display controller, so the
  1481. * above code is always going to timeout waiting for the controller
  1482. * to go idle.
  1483. *
  1484. * Given the close coupling between the RGB encoder and the display
  1485. * controller doing it here is still kind of okay. None of the other
  1486. * encoder drivers require these bits to be cleared.
  1487. *
  1488. * XXX: Perhaps given that the display controller is switched off at
  1489. * this point anyway maybe clearing these bits isn't even useful for
  1490. * the RGB encoder?
  1491. */
  1492. if (dc->rgb) {
  1493. value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
  1494. value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
  1495. PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
  1496. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
  1497. }
  1498. tegra_dc_stats_reset(&dc->stats);
  1499. drm_crtc_vblank_off(crtc);
  1500. spin_lock_irq(&crtc->dev->event_lock);
  1501. if (crtc->state->event) {
  1502. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  1503. crtc->state->event = NULL;
  1504. }
  1505. spin_unlock_irq(&crtc->dev->event_lock);
  1506. pm_runtime_put_sync(dc->dev);
  1507. }
  1508. static void tegra_crtc_atomic_enable(struct drm_crtc *crtc,
  1509. struct drm_crtc_state *old_state)
  1510. {
  1511. struct drm_display_mode *mode = &crtc->state->adjusted_mode;
  1512. struct tegra_dc_state *state = to_dc_state(crtc->state);
  1513. struct tegra_dc *dc = to_tegra_dc(crtc);
  1514. u32 value;
  1515. pm_runtime_get_sync(dc->dev);
  1516. /* initialize display controller */
  1517. if (dc->syncpt) {
  1518. u32 syncpt = host1x_syncpt_id(dc->syncpt), enable;
  1519. if (dc->soc->has_nvdisplay)
  1520. enable = 1 << 31;
  1521. else
  1522. enable = 1 << 8;
  1523. value = SYNCPT_CNTRL_NO_STALL;
  1524. tegra_dc_writel(dc, value, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
  1525. value = enable | syncpt;
  1526. tegra_dc_writel(dc, value, DC_CMD_CONT_SYNCPT_VSYNC);
  1527. }
  1528. if (dc->soc->has_nvdisplay) {
  1529. value = DSC_TO_UF_INT | DSC_BBUF_UF_INT | DSC_RBUF_UF_INT |
  1530. DSC_OBUF_UF_INT;
  1531. tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
  1532. value = DSC_TO_UF_INT | DSC_BBUF_UF_INT | DSC_RBUF_UF_INT |
  1533. DSC_OBUF_UF_INT | SD3_BUCKET_WALK_DONE_INT |
  1534. HEAD_UF_INT | MSF_INT | REG_TMOUT_INT |
  1535. REGION_CRC_INT | V_PULSE2_INT | V_PULSE3_INT |
  1536. VBLANK_INT | FRAME_END_INT;
  1537. tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
  1538. value = SD3_BUCKET_WALK_DONE_INT | HEAD_UF_INT | VBLANK_INT |
  1539. FRAME_END_INT;
  1540. tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
  1541. value = HEAD_UF_INT | REG_TMOUT_INT | FRAME_END_INT;
  1542. tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
  1543. tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
  1544. } else {
  1545. value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
  1546. WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
  1547. tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
  1548. value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
  1549. WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
  1550. tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
  1551. /* initialize timer */
  1552. value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
  1553. WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
  1554. tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
  1555. value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
  1556. WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
  1557. tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
  1558. value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
  1559. WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
  1560. tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
  1561. value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
  1562. WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
  1563. tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
  1564. }
  1565. if (dc->soc->supports_background_color)
  1566. tegra_dc_writel(dc, 0, DC_DISP_BLEND_BACKGROUND_COLOR);
  1567. else
  1568. tegra_dc_writel(dc, 0, DC_DISP_BORDER_COLOR);
  1569. /* apply PLL and pixel clock changes */
  1570. tegra_dc_commit_state(dc, state);
  1571. /* program display mode */
  1572. tegra_dc_set_timings(dc, mode);
  1573. /* interlacing isn't supported yet, so disable it */
  1574. if (dc->soc->supports_interlacing) {
  1575. value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
  1576. value &= ~INTERLACE_ENABLE;
  1577. tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
  1578. }
  1579. value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
  1580. value &= ~DISP_CTRL_MODE_MASK;
  1581. value |= DISP_CTRL_MODE_C_DISPLAY;
  1582. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
  1583. if (!dc->soc->has_nvdisplay) {
  1584. value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
  1585. value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
  1586. PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
  1587. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
  1588. }
  1589. /* enable underflow reporting and display red for missing pixels */
  1590. if (dc->soc->has_nvdisplay) {
  1591. value = UNDERFLOW_MODE_RED | UNDERFLOW_REPORT_ENABLE;
  1592. tegra_dc_writel(dc, value, DC_COM_RG_UNDERFLOW);
  1593. }
  1594. tegra_dc_commit(dc);
  1595. drm_crtc_vblank_on(crtc);
  1596. }
  1597. static void tegra_crtc_atomic_begin(struct drm_crtc *crtc,
  1598. struct drm_crtc_state *old_crtc_state)
  1599. {
  1600. unsigned long flags;
  1601. if (crtc->state->event) {
  1602. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  1603. if (drm_crtc_vblank_get(crtc) != 0)
  1604. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  1605. else
  1606. drm_crtc_arm_vblank_event(crtc, crtc->state->event);
  1607. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  1608. crtc->state->event = NULL;
  1609. }
  1610. }
  1611. static void tegra_crtc_atomic_flush(struct drm_crtc *crtc,
  1612. struct drm_crtc_state *old_crtc_state)
  1613. {
  1614. struct tegra_dc_state *state = to_dc_state(crtc->state);
  1615. struct tegra_dc *dc = to_tegra_dc(crtc);
  1616. u32 value;
  1617. value = state->planes << 8 | GENERAL_UPDATE;
  1618. tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
  1619. value = tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
  1620. value = state->planes | GENERAL_ACT_REQ;
  1621. tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
  1622. value = tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
  1623. }
  1624. static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
  1625. .atomic_begin = tegra_crtc_atomic_begin,
  1626. .atomic_flush = tegra_crtc_atomic_flush,
  1627. .atomic_enable = tegra_crtc_atomic_enable,
  1628. .atomic_disable = tegra_crtc_atomic_disable,
  1629. };
  1630. static irqreturn_t tegra_dc_irq(int irq, void *data)
  1631. {
  1632. struct tegra_dc *dc = data;
  1633. unsigned long status;
  1634. status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
  1635. tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
  1636. if (status & FRAME_END_INT) {
  1637. /*
  1638. dev_dbg(dc->dev, "%s(): frame end\n", __func__);
  1639. */
  1640. dc->stats.frames++;
  1641. }
  1642. if (status & VBLANK_INT) {
  1643. /*
  1644. dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
  1645. */
  1646. drm_crtc_handle_vblank(&dc->base);
  1647. dc->stats.vblank++;
  1648. }
  1649. if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
  1650. /*
  1651. dev_dbg(dc->dev, "%s(): underflow\n", __func__);
  1652. */
  1653. dc->stats.underflow++;
  1654. }
  1655. if (status & (WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT)) {
  1656. /*
  1657. dev_dbg(dc->dev, "%s(): overflow\n", __func__);
  1658. */
  1659. dc->stats.overflow++;
  1660. }
  1661. if (status & HEAD_UF_INT) {
  1662. dev_dbg_ratelimited(dc->dev, "%s(): head underflow\n", __func__);
  1663. dc->stats.underflow++;
  1664. }
  1665. return IRQ_HANDLED;
  1666. }
  1667. static int tegra_dc_init(struct host1x_client *client)
  1668. {
  1669. struct drm_device *drm = dev_get_drvdata(client->parent);
  1670. unsigned long flags = HOST1X_SYNCPT_CLIENT_MANAGED;
  1671. struct tegra_dc *dc = host1x_client_to_dc(client);
  1672. struct tegra_drm *tegra = drm->dev_private;
  1673. struct drm_plane *primary = NULL;
  1674. struct drm_plane *cursor = NULL;
  1675. int err;
  1676. dc->syncpt = host1x_syncpt_request(client, flags);
  1677. if (!dc->syncpt)
  1678. dev_warn(dc->dev, "failed to allocate syncpoint\n");
  1679. dc->group = host1x_client_iommu_attach(client, true);
  1680. if (IS_ERR(dc->group)) {
  1681. err = PTR_ERR(dc->group);
  1682. dev_err(client->dev, "failed to attach to domain: %d\n", err);
  1683. return err;
  1684. }
  1685. if (dc->soc->wgrps)
  1686. primary = tegra_dc_add_shared_planes(drm, dc);
  1687. else
  1688. primary = tegra_dc_add_planes(drm, dc);
  1689. if (IS_ERR(primary)) {
  1690. err = PTR_ERR(primary);
  1691. goto cleanup;
  1692. }
  1693. if (dc->soc->supports_cursor) {
  1694. cursor = tegra_dc_cursor_plane_create(drm, dc);
  1695. if (IS_ERR(cursor)) {
  1696. err = PTR_ERR(cursor);
  1697. goto cleanup;
  1698. }
  1699. } else {
  1700. /* dedicate one overlay to mouse cursor */
  1701. cursor = tegra_dc_overlay_plane_create(drm, dc, 2, true);
  1702. if (IS_ERR(cursor)) {
  1703. err = PTR_ERR(cursor);
  1704. goto cleanup;
  1705. }
  1706. }
  1707. err = drm_crtc_init_with_planes(drm, &dc->base, primary, cursor,
  1708. &tegra_crtc_funcs, NULL);
  1709. if (err < 0)
  1710. goto cleanup;
  1711. drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
  1712. /*
  1713. * Keep track of the minimum pitch alignment across all display
  1714. * controllers.
  1715. */
  1716. if (dc->soc->pitch_align > tegra->pitch_align)
  1717. tegra->pitch_align = dc->soc->pitch_align;
  1718. err = tegra_dc_rgb_init(drm, dc);
  1719. if (err < 0 && err != -ENODEV) {
  1720. dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
  1721. goto cleanup;
  1722. }
  1723. err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
  1724. dev_name(dc->dev), dc);
  1725. if (err < 0) {
  1726. dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
  1727. err);
  1728. goto cleanup;
  1729. }
  1730. return 0;
  1731. cleanup:
  1732. if (!IS_ERR_OR_NULL(cursor))
  1733. drm_plane_cleanup(cursor);
  1734. if (!IS_ERR(primary))
  1735. drm_plane_cleanup(primary);
  1736. host1x_client_iommu_detach(client, dc->group);
  1737. host1x_syncpt_free(dc->syncpt);
  1738. return err;
  1739. }
  1740. static int tegra_dc_exit(struct host1x_client *client)
  1741. {
  1742. struct tegra_dc *dc = host1x_client_to_dc(client);
  1743. int err;
  1744. devm_free_irq(dc->dev, dc->irq, dc);
  1745. err = tegra_dc_rgb_exit(dc);
  1746. if (err) {
  1747. dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
  1748. return err;
  1749. }
  1750. host1x_client_iommu_detach(client, dc->group);
  1751. host1x_syncpt_free(dc->syncpt);
  1752. return 0;
  1753. }
  1754. static const struct host1x_client_ops dc_client_ops = {
  1755. .init = tegra_dc_init,
  1756. .exit = tegra_dc_exit,
  1757. };
  1758. static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
  1759. .supports_background_color = false,
  1760. .supports_interlacing = false,
  1761. .supports_cursor = false,
  1762. .supports_block_linear = false,
  1763. .has_legacy_blending = true,
  1764. .pitch_align = 8,
  1765. .has_powergate = false,
  1766. .coupled_pm = true,
  1767. .has_nvdisplay = false,
  1768. .num_primary_formats = ARRAY_SIZE(tegra20_primary_formats),
  1769. .primary_formats = tegra20_primary_formats,
  1770. .num_overlay_formats = ARRAY_SIZE(tegra20_overlay_formats),
  1771. .overlay_formats = tegra20_overlay_formats,
  1772. .modifiers = tegra20_modifiers,
  1773. .has_win_a_without_filters = true,
  1774. .has_win_c_without_vert_filter = true,
  1775. };
  1776. static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
  1777. .supports_background_color = false,
  1778. .supports_interlacing = false,
  1779. .supports_cursor = false,
  1780. .supports_block_linear = false,
  1781. .has_legacy_blending = true,
  1782. .pitch_align = 8,
  1783. .has_powergate = false,
  1784. .coupled_pm = false,
  1785. .has_nvdisplay = false,
  1786. .num_primary_formats = ARRAY_SIZE(tegra20_primary_formats),
  1787. .primary_formats = tegra20_primary_formats,
  1788. .num_overlay_formats = ARRAY_SIZE(tegra20_overlay_formats),
  1789. .overlay_formats = tegra20_overlay_formats,
  1790. .modifiers = tegra20_modifiers,
  1791. .has_win_a_without_filters = false,
  1792. .has_win_c_without_vert_filter = false,
  1793. };
  1794. static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
  1795. .supports_background_color = false,
  1796. .supports_interlacing = false,
  1797. .supports_cursor = false,
  1798. .supports_block_linear = false,
  1799. .has_legacy_blending = true,
  1800. .pitch_align = 64,
  1801. .has_powergate = true,
  1802. .coupled_pm = false,
  1803. .has_nvdisplay = false,
  1804. .num_primary_formats = ARRAY_SIZE(tegra114_primary_formats),
  1805. .primary_formats = tegra114_primary_formats,
  1806. .num_overlay_formats = ARRAY_SIZE(tegra114_overlay_formats),
  1807. .overlay_formats = tegra114_overlay_formats,
  1808. .modifiers = tegra20_modifiers,
  1809. .has_win_a_without_filters = false,
  1810. .has_win_c_without_vert_filter = false,
  1811. };
  1812. static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
  1813. .supports_background_color = true,
  1814. .supports_interlacing = true,
  1815. .supports_cursor = true,
  1816. .supports_block_linear = true,
  1817. .has_legacy_blending = false,
  1818. .pitch_align = 64,
  1819. .has_powergate = true,
  1820. .coupled_pm = false,
  1821. .has_nvdisplay = false,
  1822. .num_primary_formats = ARRAY_SIZE(tegra124_primary_formats),
  1823. .primary_formats = tegra124_primary_formats,
  1824. .num_overlay_formats = ARRAY_SIZE(tegra124_overlay_formats),
  1825. .overlay_formats = tegra124_overlay_formats,
  1826. .modifiers = tegra124_modifiers,
  1827. .has_win_a_without_filters = false,
  1828. .has_win_c_without_vert_filter = false,
  1829. };
  1830. static const struct tegra_dc_soc_info tegra210_dc_soc_info = {
  1831. .supports_background_color = true,
  1832. .supports_interlacing = true,
  1833. .supports_cursor = true,
  1834. .supports_block_linear = true,
  1835. .has_legacy_blending = false,
  1836. .pitch_align = 64,
  1837. .has_powergate = true,
  1838. .coupled_pm = false,
  1839. .has_nvdisplay = false,
  1840. .num_primary_formats = ARRAY_SIZE(tegra114_primary_formats),
  1841. .primary_formats = tegra114_primary_formats,
  1842. .num_overlay_formats = ARRAY_SIZE(tegra114_overlay_formats),
  1843. .overlay_formats = tegra114_overlay_formats,
  1844. .modifiers = tegra124_modifiers,
  1845. .has_win_a_without_filters = false,
  1846. .has_win_c_without_vert_filter = false,
  1847. };
  1848. static const struct tegra_windowgroup_soc tegra186_dc_wgrps[] = {
  1849. {
  1850. .index = 0,
  1851. .dc = 0,
  1852. .windows = (const unsigned int[]) { 0 },
  1853. .num_windows = 1,
  1854. }, {
  1855. .index = 1,
  1856. .dc = 1,
  1857. .windows = (const unsigned int[]) { 1 },
  1858. .num_windows = 1,
  1859. }, {
  1860. .index = 2,
  1861. .dc = 1,
  1862. .windows = (const unsigned int[]) { 2 },
  1863. .num_windows = 1,
  1864. }, {
  1865. .index = 3,
  1866. .dc = 2,
  1867. .windows = (const unsigned int[]) { 3 },
  1868. .num_windows = 1,
  1869. }, {
  1870. .index = 4,
  1871. .dc = 2,
  1872. .windows = (const unsigned int[]) { 4 },
  1873. .num_windows = 1,
  1874. }, {
  1875. .index = 5,
  1876. .dc = 2,
  1877. .windows = (const unsigned int[]) { 5 },
  1878. .num_windows = 1,
  1879. },
  1880. };
  1881. static const struct tegra_dc_soc_info tegra186_dc_soc_info = {
  1882. .supports_background_color = true,
  1883. .supports_interlacing = true,
  1884. .supports_cursor = true,
  1885. .supports_block_linear = true,
  1886. .has_legacy_blending = false,
  1887. .pitch_align = 64,
  1888. .has_powergate = false,
  1889. .coupled_pm = false,
  1890. .has_nvdisplay = true,
  1891. .wgrps = tegra186_dc_wgrps,
  1892. .num_wgrps = ARRAY_SIZE(tegra186_dc_wgrps),
  1893. };
  1894. static const struct of_device_id tegra_dc_of_match[] = {
  1895. {
  1896. .compatible = "nvidia,tegra186-dc",
  1897. .data = &tegra186_dc_soc_info,
  1898. }, {
  1899. .compatible = "nvidia,tegra210-dc",
  1900. .data = &tegra210_dc_soc_info,
  1901. }, {
  1902. .compatible = "nvidia,tegra124-dc",
  1903. .data = &tegra124_dc_soc_info,
  1904. }, {
  1905. .compatible = "nvidia,tegra114-dc",
  1906. .data = &tegra114_dc_soc_info,
  1907. }, {
  1908. .compatible = "nvidia,tegra30-dc",
  1909. .data = &tegra30_dc_soc_info,
  1910. }, {
  1911. .compatible = "nvidia,tegra20-dc",
  1912. .data = &tegra20_dc_soc_info,
  1913. }, {
  1914. /* sentinel */
  1915. }
  1916. };
  1917. MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
  1918. static int tegra_dc_parse_dt(struct tegra_dc *dc)
  1919. {
  1920. struct device_node *np;
  1921. u32 value = 0;
  1922. int err;
  1923. err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
  1924. if (err < 0) {
  1925. dev_err(dc->dev, "missing \"nvidia,head\" property\n");
  1926. /*
  1927. * If the nvidia,head property isn't present, try to find the
  1928. * correct head number by looking up the position of this
  1929. * display controller's node within the device tree. Assuming
  1930. * that the nodes are ordered properly in the DTS file and
  1931. * that the translation into a flattened device tree blob
  1932. * preserves that ordering this will actually yield the right
  1933. * head number.
  1934. *
  1935. * If those assumptions don't hold, this will still work for
  1936. * cases where only a single display controller is used.
  1937. */
  1938. for_each_matching_node(np, tegra_dc_of_match) {
  1939. if (np == dc->dev->of_node) {
  1940. of_node_put(np);
  1941. break;
  1942. }
  1943. value++;
  1944. }
  1945. }
  1946. dc->pipe = value;
  1947. return 0;
  1948. }
  1949. static int tegra_dc_match_by_pipe(struct device *dev, void *data)
  1950. {
  1951. struct tegra_dc *dc = dev_get_drvdata(dev);
  1952. unsigned int pipe = (unsigned long)data;
  1953. return dc->pipe == pipe;
  1954. }
  1955. static int tegra_dc_couple(struct tegra_dc *dc)
  1956. {
  1957. /*
  1958. * On Tegra20, DC1 requires DC0 to be taken out of reset in order to
  1959. * be enabled, otherwise CPU hangs on writing to CMD_DISPLAY_COMMAND /
  1960. * POWER_CONTROL registers during CRTC enabling.
  1961. */
  1962. if (dc->soc->coupled_pm && dc->pipe == 1) {
  1963. u32 flags = DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE;
  1964. struct device_link *link;
  1965. struct device *partner;
  1966. partner = driver_find_device(dc->dev->driver, NULL, NULL,
  1967. tegra_dc_match_by_pipe);
  1968. if (!partner)
  1969. return -EPROBE_DEFER;
  1970. link = device_link_add(dc->dev, partner, flags);
  1971. if (!link) {
  1972. dev_err(dc->dev, "failed to link controllers\n");
  1973. return -EINVAL;
  1974. }
  1975. dev_dbg(dc->dev, "coupled to %s\n", dev_name(partner));
  1976. }
  1977. return 0;
  1978. }
  1979. static int tegra_dc_probe(struct platform_device *pdev)
  1980. {
  1981. struct resource *regs;
  1982. struct tegra_dc *dc;
  1983. int err;
  1984. dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
  1985. if (!dc)
  1986. return -ENOMEM;
  1987. dc->soc = of_device_get_match_data(&pdev->dev);
  1988. INIT_LIST_HEAD(&dc->list);
  1989. dc->dev = &pdev->dev;
  1990. err = tegra_dc_parse_dt(dc);
  1991. if (err < 0)
  1992. return err;
  1993. err = tegra_dc_couple(dc);
  1994. if (err < 0)
  1995. return err;
  1996. dc->clk = devm_clk_get(&pdev->dev, NULL);
  1997. if (IS_ERR(dc->clk)) {
  1998. dev_err(&pdev->dev, "failed to get clock\n");
  1999. return PTR_ERR(dc->clk);
  2000. }
  2001. dc->rst = devm_reset_control_get(&pdev->dev, "dc");
  2002. if (IS_ERR(dc->rst)) {
  2003. dev_err(&pdev->dev, "failed to get reset\n");
  2004. return PTR_ERR(dc->rst);
  2005. }
  2006. /* assert reset and disable clock */
  2007. err = clk_prepare_enable(dc->clk);
  2008. if (err < 0)
  2009. return err;
  2010. usleep_range(2000, 4000);
  2011. err = reset_control_assert(dc->rst);
  2012. if (err < 0)
  2013. return err;
  2014. usleep_range(2000, 4000);
  2015. clk_disable_unprepare(dc->clk);
  2016. if (dc->soc->has_powergate) {
  2017. if (dc->pipe == 0)
  2018. dc->powergate = TEGRA_POWERGATE_DIS;
  2019. else
  2020. dc->powergate = TEGRA_POWERGATE_DISB;
  2021. tegra_powergate_power_off(dc->powergate);
  2022. }
  2023. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2024. dc->regs = devm_ioremap_resource(&pdev->dev, regs);
  2025. if (IS_ERR(dc->regs))
  2026. return PTR_ERR(dc->regs);
  2027. dc->irq = platform_get_irq(pdev, 0);
  2028. if (dc->irq < 0) {
  2029. dev_err(&pdev->dev, "failed to get IRQ\n");
  2030. return -ENXIO;
  2031. }
  2032. err = tegra_dc_rgb_probe(dc);
  2033. if (err < 0 && err != -ENODEV) {
  2034. dev_err(&pdev->dev, "failed to probe RGB output: %d\n", err);
  2035. return err;
  2036. }
  2037. platform_set_drvdata(pdev, dc);
  2038. pm_runtime_enable(&pdev->dev);
  2039. INIT_LIST_HEAD(&dc->client.list);
  2040. dc->client.ops = &dc_client_ops;
  2041. dc->client.dev = &pdev->dev;
  2042. err = host1x_client_register(&dc->client);
  2043. if (err < 0) {
  2044. dev_err(&pdev->dev, "failed to register host1x client: %d\n",
  2045. err);
  2046. return err;
  2047. }
  2048. return 0;
  2049. }
  2050. static int tegra_dc_remove(struct platform_device *pdev)
  2051. {
  2052. struct tegra_dc *dc = platform_get_drvdata(pdev);
  2053. int err;
  2054. err = host1x_client_unregister(&dc->client);
  2055. if (err < 0) {
  2056. dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
  2057. err);
  2058. return err;
  2059. }
  2060. err = tegra_dc_rgb_remove(dc);
  2061. if (err < 0) {
  2062. dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
  2063. return err;
  2064. }
  2065. pm_runtime_disable(&pdev->dev);
  2066. return 0;
  2067. }
  2068. #ifdef CONFIG_PM
  2069. static int tegra_dc_suspend(struct device *dev)
  2070. {
  2071. struct tegra_dc *dc = dev_get_drvdata(dev);
  2072. int err;
  2073. err = reset_control_assert(dc->rst);
  2074. if (err < 0) {
  2075. dev_err(dev, "failed to assert reset: %d\n", err);
  2076. return err;
  2077. }
  2078. if (dc->soc->has_powergate)
  2079. tegra_powergate_power_off(dc->powergate);
  2080. clk_disable_unprepare(dc->clk);
  2081. return 0;
  2082. }
  2083. static int tegra_dc_resume(struct device *dev)
  2084. {
  2085. struct tegra_dc *dc = dev_get_drvdata(dev);
  2086. int err;
  2087. if (dc->soc->has_powergate) {
  2088. err = tegra_powergate_sequence_power_up(dc->powergate, dc->clk,
  2089. dc->rst);
  2090. if (err < 0) {
  2091. dev_err(dev, "failed to power partition: %d\n", err);
  2092. return err;
  2093. }
  2094. } else {
  2095. err = clk_prepare_enable(dc->clk);
  2096. if (err < 0) {
  2097. dev_err(dev, "failed to enable clock: %d\n", err);
  2098. return err;
  2099. }
  2100. err = reset_control_deassert(dc->rst);
  2101. if (err < 0) {
  2102. dev_err(dev, "failed to deassert reset: %d\n", err);
  2103. return err;
  2104. }
  2105. }
  2106. return 0;
  2107. }
  2108. #endif
  2109. static const struct dev_pm_ops tegra_dc_pm_ops = {
  2110. SET_RUNTIME_PM_OPS(tegra_dc_suspend, tegra_dc_resume, NULL)
  2111. };
  2112. struct platform_driver tegra_dc_driver = {
  2113. .driver = {
  2114. .name = "tegra-dc",
  2115. .of_match_table = tegra_dc_of_match,
  2116. .pm = &tegra_dc_pm_ops,
  2117. },
  2118. .probe = tegra_dc_probe,
  2119. .remove = tegra_dc_remove,
  2120. };