dc.c 63 KB

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