dc.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157
  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/pm_runtime.h>
  13. #include <linux/reset.h>
  14. #include <soc/tegra/pmc.h>
  15. #include "dc.h"
  16. #include "drm.h"
  17. #include "gem.h"
  18. #include <drm/drm_atomic.h>
  19. #include <drm/drm_atomic_helper.h>
  20. #include <drm/drm_plane_helper.h>
  21. struct tegra_dc_soc_info {
  22. bool supports_border_color;
  23. bool supports_interlacing;
  24. bool supports_cursor;
  25. bool supports_block_linear;
  26. unsigned int pitch_align;
  27. bool has_powergate;
  28. bool broken_reset;
  29. };
  30. struct tegra_plane {
  31. struct drm_plane base;
  32. unsigned int index;
  33. };
  34. static inline struct tegra_plane *to_tegra_plane(struct drm_plane *plane)
  35. {
  36. return container_of(plane, struct tegra_plane, base);
  37. }
  38. struct tegra_dc_state {
  39. struct drm_crtc_state base;
  40. struct clk *clk;
  41. unsigned long pclk;
  42. unsigned int div;
  43. u32 planes;
  44. };
  45. static inline struct tegra_dc_state *to_dc_state(struct drm_crtc_state *state)
  46. {
  47. if (state)
  48. return container_of(state, struct tegra_dc_state, base);
  49. return NULL;
  50. }
  51. struct tegra_plane_state {
  52. struct drm_plane_state base;
  53. struct tegra_bo_tiling tiling;
  54. u32 format;
  55. u32 swap;
  56. };
  57. static inline struct tegra_plane_state *
  58. to_tegra_plane_state(struct drm_plane_state *state)
  59. {
  60. if (state)
  61. return container_of(state, struct tegra_plane_state, base);
  62. return NULL;
  63. }
  64. static void tegra_dc_stats_reset(struct tegra_dc_stats *stats)
  65. {
  66. stats->frames = 0;
  67. stats->vblank = 0;
  68. stats->underflow = 0;
  69. stats->overflow = 0;
  70. }
  71. /*
  72. * Reads the active copy of a register. This takes the dc->lock spinlock to
  73. * prevent races with the VBLANK processing which also needs access to the
  74. * active copy of some registers.
  75. */
  76. static u32 tegra_dc_readl_active(struct tegra_dc *dc, unsigned long offset)
  77. {
  78. unsigned long flags;
  79. u32 value;
  80. spin_lock_irqsave(&dc->lock, flags);
  81. tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
  82. value = tegra_dc_readl(dc, offset);
  83. tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
  84. spin_unlock_irqrestore(&dc->lock, flags);
  85. return value;
  86. }
  87. /*
  88. * Double-buffered registers have two copies: ASSEMBLY and ACTIVE. When the
  89. * *_ACT_REQ bits are set the ASSEMBLY copy is latched into the ACTIVE copy.
  90. * Latching happens mmediately if the display controller is in STOP mode or
  91. * on the next frame boundary otherwise.
  92. *
  93. * Triple-buffered registers have three copies: ASSEMBLY, ARM and ACTIVE. The
  94. * ASSEMBLY copy is latched into the ARM copy immediately after *_UPDATE bits
  95. * are written. When the *_ACT_REQ bits are written, the ARM copy is latched
  96. * into the ACTIVE copy, either immediately if the display controller is in
  97. * STOP mode, or at the next frame boundary otherwise.
  98. */
  99. void tegra_dc_commit(struct tegra_dc *dc)
  100. {
  101. tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
  102. tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
  103. }
  104. static int tegra_dc_format(u32 fourcc, u32 *format, u32 *swap)
  105. {
  106. /* assume no swapping of fetched data */
  107. if (swap)
  108. *swap = BYTE_SWAP_NOSWAP;
  109. switch (fourcc) {
  110. case DRM_FORMAT_XBGR8888:
  111. *format = WIN_COLOR_DEPTH_R8G8B8A8;
  112. break;
  113. case DRM_FORMAT_XRGB8888:
  114. *format = WIN_COLOR_DEPTH_B8G8R8A8;
  115. break;
  116. case DRM_FORMAT_RGB565:
  117. *format = WIN_COLOR_DEPTH_B5G6R5;
  118. break;
  119. case DRM_FORMAT_UYVY:
  120. *format = WIN_COLOR_DEPTH_YCbCr422;
  121. break;
  122. case DRM_FORMAT_YUYV:
  123. if (swap)
  124. *swap = BYTE_SWAP_SWAP2;
  125. *format = WIN_COLOR_DEPTH_YCbCr422;
  126. break;
  127. case DRM_FORMAT_YUV420:
  128. *format = WIN_COLOR_DEPTH_YCbCr420P;
  129. break;
  130. case DRM_FORMAT_YUV422:
  131. *format = WIN_COLOR_DEPTH_YCbCr422P;
  132. break;
  133. default:
  134. return -EINVAL;
  135. }
  136. return 0;
  137. }
  138. static bool tegra_dc_format_is_yuv(unsigned int format, bool *planar)
  139. {
  140. switch (format) {
  141. case WIN_COLOR_DEPTH_YCbCr422:
  142. case WIN_COLOR_DEPTH_YUV422:
  143. if (planar)
  144. *planar = false;
  145. return true;
  146. case WIN_COLOR_DEPTH_YCbCr420P:
  147. case WIN_COLOR_DEPTH_YUV420P:
  148. case WIN_COLOR_DEPTH_YCbCr422P:
  149. case WIN_COLOR_DEPTH_YUV422P:
  150. case WIN_COLOR_DEPTH_YCbCr422R:
  151. case WIN_COLOR_DEPTH_YUV422R:
  152. case WIN_COLOR_DEPTH_YCbCr422RA:
  153. case WIN_COLOR_DEPTH_YUV422RA:
  154. if (planar)
  155. *planar = true;
  156. return true;
  157. }
  158. if (planar)
  159. *planar = false;
  160. return false;
  161. }
  162. static inline u32 compute_dda_inc(unsigned int in, unsigned int out, bool v,
  163. unsigned int bpp)
  164. {
  165. fixed20_12 outf = dfixed_init(out);
  166. fixed20_12 inf = dfixed_init(in);
  167. u32 dda_inc;
  168. int max;
  169. if (v)
  170. max = 15;
  171. else {
  172. switch (bpp) {
  173. case 2:
  174. max = 8;
  175. break;
  176. default:
  177. WARN_ON_ONCE(1);
  178. /* fallthrough */
  179. case 4:
  180. max = 4;
  181. break;
  182. }
  183. }
  184. outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
  185. inf.full -= dfixed_const(1);
  186. dda_inc = dfixed_div(inf, outf);
  187. dda_inc = min_t(u32, dda_inc, dfixed_const(max));
  188. return dda_inc;
  189. }
  190. static inline u32 compute_initial_dda(unsigned int in)
  191. {
  192. fixed20_12 inf = dfixed_init(in);
  193. return dfixed_frac(inf);
  194. }
  195. static void tegra_dc_setup_window(struct tegra_dc *dc, unsigned int index,
  196. const struct tegra_dc_window *window)
  197. {
  198. unsigned h_offset, v_offset, h_size, v_size, h_dda, v_dda, bpp;
  199. unsigned long value, flags;
  200. bool yuv, planar;
  201. /*
  202. * For YUV planar modes, the number of bytes per pixel takes into
  203. * account only the luma component and therefore is 1.
  204. */
  205. yuv = tegra_dc_format_is_yuv(window->format, &planar);
  206. if (!yuv)
  207. bpp = window->bits_per_pixel / 8;
  208. else
  209. bpp = planar ? 1 : 2;
  210. spin_lock_irqsave(&dc->lock, flags);
  211. value = WINDOW_A_SELECT << index;
  212. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
  213. tegra_dc_writel(dc, window->format, DC_WIN_COLOR_DEPTH);
  214. tegra_dc_writel(dc, window->swap, DC_WIN_BYTE_SWAP);
  215. value = V_POSITION(window->dst.y) | H_POSITION(window->dst.x);
  216. tegra_dc_writel(dc, value, DC_WIN_POSITION);
  217. value = V_SIZE(window->dst.h) | H_SIZE(window->dst.w);
  218. tegra_dc_writel(dc, value, DC_WIN_SIZE);
  219. h_offset = window->src.x * bpp;
  220. v_offset = window->src.y;
  221. h_size = window->src.w * bpp;
  222. v_size = window->src.h;
  223. value = V_PRESCALED_SIZE(v_size) | H_PRESCALED_SIZE(h_size);
  224. tegra_dc_writel(dc, value, DC_WIN_PRESCALED_SIZE);
  225. /*
  226. * For DDA computations the number of bytes per pixel for YUV planar
  227. * modes needs to take into account all Y, U and V components.
  228. */
  229. if (yuv && planar)
  230. bpp = 2;
  231. h_dda = compute_dda_inc(window->src.w, window->dst.w, false, bpp);
  232. v_dda = compute_dda_inc(window->src.h, window->dst.h, true, bpp);
  233. value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
  234. tegra_dc_writel(dc, value, DC_WIN_DDA_INC);
  235. h_dda = compute_initial_dda(window->src.x);
  236. v_dda = compute_initial_dda(window->src.y);
  237. tegra_dc_writel(dc, h_dda, DC_WIN_H_INITIAL_DDA);
  238. tegra_dc_writel(dc, v_dda, DC_WIN_V_INITIAL_DDA);
  239. tegra_dc_writel(dc, 0, DC_WIN_UV_BUF_STRIDE);
  240. tegra_dc_writel(dc, 0, DC_WIN_BUF_STRIDE);
  241. tegra_dc_writel(dc, window->base[0], DC_WINBUF_START_ADDR);
  242. if (yuv && planar) {
  243. tegra_dc_writel(dc, window->base[1], DC_WINBUF_START_ADDR_U);
  244. tegra_dc_writel(dc, window->base[2], DC_WINBUF_START_ADDR_V);
  245. value = window->stride[1] << 16 | window->stride[0];
  246. tegra_dc_writel(dc, value, DC_WIN_LINE_STRIDE);
  247. } else {
  248. tegra_dc_writel(dc, window->stride[0], DC_WIN_LINE_STRIDE);
  249. }
  250. if (window->bottom_up)
  251. v_offset += window->src.h - 1;
  252. tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
  253. tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
  254. if (dc->soc->supports_block_linear) {
  255. unsigned long height = window->tiling.value;
  256. switch (window->tiling.mode) {
  257. case TEGRA_BO_TILING_MODE_PITCH:
  258. value = DC_WINBUF_SURFACE_KIND_PITCH;
  259. break;
  260. case TEGRA_BO_TILING_MODE_TILED:
  261. value = DC_WINBUF_SURFACE_KIND_TILED;
  262. break;
  263. case TEGRA_BO_TILING_MODE_BLOCK:
  264. value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
  265. DC_WINBUF_SURFACE_KIND_BLOCK;
  266. break;
  267. }
  268. tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
  269. } else {
  270. switch (window->tiling.mode) {
  271. case TEGRA_BO_TILING_MODE_PITCH:
  272. value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
  273. DC_WIN_BUFFER_ADDR_MODE_LINEAR;
  274. break;
  275. case TEGRA_BO_TILING_MODE_TILED:
  276. value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
  277. DC_WIN_BUFFER_ADDR_MODE_TILE;
  278. break;
  279. case TEGRA_BO_TILING_MODE_BLOCK:
  280. /*
  281. * No need to handle this here because ->atomic_check
  282. * will already have filtered it out.
  283. */
  284. break;
  285. }
  286. tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
  287. }
  288. value = WIN_ENABLE;
  289. if (yuv) {
  290. /* setup default colorspace conversion coefficients */
  291. tegra_dc_writel(dc, 0x00f0, DC_WIN_CSC_YOF);
  292. tegra_dc_writel(dc, 0x012a, DC_WIN_CSC_KYRGB);
  293. tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KUR);
  294. tegra_dc_writel(dc, 0x0198, DC_WIN_CSC_KVR);
  295. tegra_dc_writel(dc, 0x039b, DC_WIN_CSC_KUG);
  296. tegra_dc_writel(dc, 0x032f, DC_WIN_CSC_KVG);
  297. tegra_dc_writel(dc, 0x0204, DC_WIN_CSC_KUB);
  298. tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KVB);
  299. value |= CSC_ENABLE;
  300. } else if (window->bits_per_pixel < 24) {
  301. value |= COLOR_EXPAND;
  302. }
  303. if (window->bottom_up)
  304. value |= V_DIRECTION;
  305. tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
  306. /*
  307. * Disable blending and assume Window A is the bottom-most window,
  308. * Window C is the top-most window and Window B is in the middle.
  309. */
  310. tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_NOKEY);
  311. tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_1WIN);
  312. switch (index) {
  313. case 0:
  314. tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_X);
  315. tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
  316. tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
  317. break;
  318. case 1:
  319. tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
  320. tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
  321. tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
  322. break;
  323. case 2:
  324. tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
  325. tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_Y);
  326. tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_3WIN_XY);
  327. break;
  328. }
  329. spin_unlock_irqrestore(&dc->lock, flags);
  330. }
  331. static void tegra_plane_destroy(struct drm_plane *plane)
  332. {
  333. struct tegra_plane *p = to_tegra_plane(plane);
  334. drm_plane_cleanup(plane);
  335. kfree(p);
  336. }
  337. static const u32 tegra_primary_plane_formats[] = {
  338. DRM_FORMAT_XBGR8888,
  339. DRM_FORMAT_XRGB8888,
  340. DRM_FORMAT_RGB565,
  341. };
  342. static void tegra_primary_plane_destroy(struct drm_plane *plane)
  343. {
  344. tegra_plane_destroy(plane);
  345. }
  346. static void tegra_plane_reset(struct drm_plane *plane)
  347. {
  348. struct tegra_plane_state *state;
  349. if (plane->state)
  350. __drm_atomic_helper_plane_destroy_state(plane->state);
  351. kfree(plane->state);
  352. plane->state = NULL;
  353. state = kzalloc(sizeof(*state), GFP_KERNEL);
  354. if (state) {
  355. plane->state = &state->base;
  356. plane->state->plane = plane;
  357. }
  358. }
  359. static struct drm_plane_state *tegra_plane_atomic_duplicate_state(struct drm_plane *plane)
  360. {
  361. struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
  362. struct tegra_plane_state *copy;
  363. copy = kmalloc(sizeof(*copy), GFP_KERNEL);
  364. if (!copy)
  365. return NULL;
  366. __drm_atomic_helper_plane_duplicate_state(plane, &copy->base);
  367. copy->tiling = state->tiling;
  368. copy->format = state->format;
  369. copy->swap = state->swap;
  370. return &copy->base;
  371. }
  372. static void tegra_plane_atomic_destroy_state(struct drm_plane *plane,
  373. struct drm_plane_state *state)
  374. {
  375. __drm_atomic_helper_plane_destroy_state(state);
  376. kfree(state);
  377. }
  378. static const struct drm_plane_funcs tegra_primary_plane_funcs = {
  379. .update_plane = drm_atomic_helper_update_plane,
  380. .disable_plane = drm_atomic_helper_disable_plane,
  381. .destroy = tegra_primary_plane_destroy,
  382. .reset = tegra_plane_reset,
  383. .atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
  384. .atomic_destroy_state = tegra_plane_atomic_destroy_state,
  385. };
  386. static int tegra_plane_state_add(struct tegra_plane *plane,
  387. struct drm_plane_state *state)
  388. {
  389. struct drm_crtc_state *crtc_state;
  390. struct tegra_dc_state *tegra;
  391. struct drm_rect clip;
  392. int err;
  393. /* Propagate errors from allocation or locking failures. */
  394. crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
  395. if (IS_ERR(crtc_state))
  396. return PTR_ERR(crtc_state);
  397. clip.x1 = 0;
  398. clip.y1 = 0;
  399. clip.x2 = crtc_state->mode.hdisplay;
  400. clip.y2 = crtc_state->mode.vdisplay;
  401. /* Check plane state for visibility and calculate clipping bounds */
  402. err = drm_plane_helper_check_state(state, &clip, 0, INT_MAX,
  403. true, true);
  404. if (err < 0)
  405. return err;
  406. tegra = to_dc_state(crtc_state);
  407. tegra->planes |= WIN_A_ACT_REQ << plane->index;
  408. return 0;
  409. }
  410. static int tegra_plane_atomic_check(struct drm_plane *plane,
  411. struct drm_plane_state *state)
  412. {
  413. struct tegra_plane_state *plane_state = to_tegra_plane_state(state);
  414. struct tegra_bo_tiling *tiling = &plane_state->tiling;
  415. struct tegra_plane *tegra = to_tegra_plane(plane);
  416. struct tegra_dc *dc = to_tegra_dc(state->crtc);
  417. int err;
  418. /* no need for further checks if the plane is being disabled */
  419. if (!state->crtc)
  420. return 0;
  421. err = tegra_dc_format(state->fb->format->format, &plane_state->format,
  422. &plane_state->swap);
  423. if (err < 0)
  424. return err;
  425. err = tegra_fb_get_tiling(state->fb, tiling);
  426. if (err < 0)
  427. return err;
  428. if (tiling->mode == TEGRA_BO_TILING_MODE_BLOCK &&
  429. !dc->soc->supports_block_linear) {
  430. DRM_ERROR("hardware doesn't support block linear mode\n");
  431. return -EINVAL;
  432. }
  433. /*
  434. * Tegra doesn't support different strides for U and V planes so we
  435. * error out if the user tries to display a framebuffer with such a
  436. * configuration.
  437. */
  438. if (state->fb->format->num_planes > 2) {
  439. if (state->fb->pitches[2] != state->fb->pitches[1]) {
  440. DRM_ERROR("unsupported UV-plane configuration\n");
  441. return -EINVAL;
  442. }
  443. }
  444. err = tegra_plane_state_add(tegra, state);
  445. if (err < 0)
  446. return err;
  447. return 0;
  448. }
  449. static void tegra_dc_disable_window(struct tegra_dc *dc, int index)
  450. {
  451. unsigned long flags;
  452. u32 value;
  453. spin_lock_irqsave(&dc->lock, flags);
  454. value = WINDOW_A_SELECT << index;
  455. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
  456. value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
  457. value &= ~WIN_ENABLE;
  458. tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
  459. spin_unlock_irqrestore(&dc->lock, flags);
  460. }
  461. static void tegra_plane_atomic_update(struct drm_plane *plane,
  462. struct drm_plane_state *old_state)
  463. {
  464. struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
  465. struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
  466. struct drm_framebuffer *fb = plane->state->fb;
  467. struct tegra_plane *p = to_tegra_plane(plane);
  468. struct tegra_dc_window window;
  469. unsigned int i;
  470. /* rien ne va plus */
  471. if (!plane->state->crtc || !plane->state->fb)
  472. return;
  473. if (!plane->state->visible)
  474. return tegra_dc_disable_window(dc, p->index);
  475. memset(&window, 0, sizeof(window));
  476. window.src.x = plane->state->src.x1 >> 16;
  477. window.src.y = plane->state->src.y1 >> 16;
  478. window.src.w = drm_rect_width(&plane->state->src) >> 16;
  479. window.src.h = drm_rect_height(&plane->state->src) >> 16;
  480. window.dst.x = plane->state->dst.x1;
  481. window.dst.y = plane->state->dst.y1;
  482. window.dst.w = drm_rect_width(&plane->state->dst);
  483. window.dst.h = drm_rect_height(&plane->state->dst);
  484. window.bits_per_pixel = fb->format->cpp[0] * 8;
  485. window.bottom_up = tegra_fb_is_bottom_up(fb);
  486. /* copy from state */
  487. window.tiling = state->tiling;
  488. window.format = state->format;
  489. window.swap = state->swap;
  490. for (i = 0; i < fb->format->num_planes; i++) {
  491. struct tegra_bo *bo = tegra_fb_get_plane(fb, i);
  492. window.base[i] = bo->paddr + fb->offsets[i];
  493. /*
  494. * Tegra uses a shared stride for UV planes. Framebuffers are
  495. * already checked for this in the tegra_plane_atomic_check()
  496. * function, so it's safe to ignore the V-plane pitch here.
  497. */
  498. if (i < 2)
  499. window.stride[i] = fb->pitches[i];
  500. }
  501. tegra_dc_setup_window(dc, p->index, &window);
  502. }
  503. static void tegra_plane_atomic_disable(struct drm_plane *plane,
  504. struct drm_plane_state *old_state)
  505. {
  506. struct tegra_plane *p = to_tegra_plane(plane);
  507. struct tegra_dc *dc;
  508. /* rien ne va plus */
  509. if (!old_state || !old_state->crtc)
  510. return;
  511. dc = to_tegra_dc(old_state->crtc);
  512. tegra_dc_disable_window(dc, p->index);
  513. }
  514. static const struct drm_plane_helper_funcs tegra_primary_plane_helper_funcs = {
  515. .atomic_check = tegra_plane_atomic_check,
  516. .atomic_update = tegra_plane_atomic_update,
  517. .atomic_disable = tegra_plane_atomic_disable,
  518. };
  519. static struct drm_plane *tegra_dc_primary_plane_create(struct drm_device *drm,
  520. struct tegra_dc *dc)
  521. {
  522. /*
  523. * Ideally this would use drm_crtc_mask(), but that would require the
  524. * CRTC to already be in the mode_config's list of CRTCs. However, it
  525. * will only be added to that list in the drm_crtc_init_with_planes()
  526. * (in tegra_dc_init()), which in turn requires registration of these
  527. * planes. So we have ourselves a nice little chicken and egg problem
  528. * here.
  529. *
  530. * We work around this by manually creating the mask from the number
  531. * of CRTCs that have been registered, and should therefore always be
  532. * the same as drm_crtc_index() after registration.
  533. */
  534. unsigned long possible_crtcs = 1 << drm->mode_config.num_crtc;
  535. struct tegra_plane *plane;
  536. unsigned int num_formats;
  537. const u32 *formats;
  538. int err;
  539. plane = kzalloc(sizeof(*plane), GFP_KERNEL);
  540. if (!plane)
  541. return ERR_PTR(-ENOMEM);
  542. num_formats = ARRAY_SIZE(tegra_primary_plane_formats);
  543. formats = tegra_primary_plane_formats;
  544. err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
  545. &tegra_primary_plane_funcs, formats,
  546. num_formats, DRM_PLANE_TYPE_PRIMARY,
  547. NULL);
  548. if (err < 0) {
  549. kfree(plane);
  550. return ERR_PTR(err);
  551. }
  552. drm_plane_helper_add(&plane->base, &tegra_primary_plane_helper_funcs);
  553. return &plane->base;
  554. }
  555. static const u32 tegra_cursor_plane_formats[] = {
  556. DRM_FORMAT_RGBA8888,
  557. };
  558. static int tegra_cursor_atomic_check(struct drm_plane *plane,
  559. struct drm_plane_state *state)
  560. {
  561. struct tegra_plane *tegra = to_tegra_plane(plane);
  562. int err;
  563. /* no need for further checks if the plane is being disabled */
  564. if (!state->crtc)
  565. return 0;
  566. /* scaling not supported for cursor */
  567. if ((state->src_w >> 16 != state->crtc_w) ||
  568. (state->src_h >> 16 != state->crtc_h))
  569. return -EINVAL;
  570. /* only square cursors supported */
  571. if (state->src_w != state->src_h)
  572. return -EINVAL;
  573. if (state->crtc_w != 32 && state->crtc_w != 64 &&
  574. state->crtc_w != 128 && state->crtc_w != 256)
  575. return -EINVAL;
  576. err = tegra_plane_state_add(tegra, state);
  577. if (err < 0)
  578. return err;
  579. return 0;
  580. }
  581. static void tegra_cursor_atomic_update(struct drm_plane *plane,
  582. struct drm_plane_state *old_state)
  583. {
  584. struct tegra_bo *bo = tegra_fb_get_plane(plane->state->fb, 0);
  585. struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
  586. struct drm_plane_state *state = plane->state;
  587. u32 value = CURSOR_CLIP_DISPLAY;
  588. /* rien ne va plus */
  589. if (!plane->state->crtc || !plane->state->fb)
  590. return;
  591. switch (state->crtc_w) {
  592. case 32:
  593. value |= CURSOR_SIZE_32x32;
  594. break;
  595. case 64:
  596. value |= CURSOR_SIZE_64x64;
  597. break;
  598. case 128:
  599. value |= CURSOR_SIZE_128x128;
  600. break;
  601. case 256:
  602. value |= CURSOR_SIZE_256x256;
  603. break;
  604. default:
  605. WARN(1, "cursor size %ux%u not supported\n", state->crtc_w,
  606. state->crtc_h);
  607. return;
  608. }
  609. value |= (bo->paddr >> 10) & 0x3fffff;
  610. tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR);
  611. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  612. value = (bo->paddr >> 32) & 0x3;
  613. tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR_HI);
  614. #endif
  615. /* enable cursor and set blend mode */
  616. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  617. value |= CURSOR_ENABLE;
  618. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  619. value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
  620. value &= ~CURSOR_DST_BLEND_MASK;
  621. value &= ~CURSOR_SRC_BLEND_MASK;
  622. value |= CURSOR_MODE_NORMAL;
  623. value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
  624. value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
  625. value |= CURSOR_ALPHA;
  626. tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
  627. /* position the cursor */
  628. value = (state->crtc_y & 0x3fff) << 16 | (state->crtc_x & 0x3fff);
  629. tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
  630. }
  631. static void tegra_cursor_atomic_disable(struct drm_plane *plane,
  632. struct drm_plane_state *old_state)
  633. {
  634. struct tegra_dc *dc;
  635. u32 value;
  636. /* rien ne va plus */
  637. if (!old_state || !old_state->crtc)
  638. return;
  639. dc = to_tegra_dc(old_state->crtc);
  640. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  641. value &= ~CURSOR_ENABLE;
  642. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  643. }
  644. static const struct drm_plane_funcs tegra_cursor_plane_funcs = {
  645. .update_plane = drm_atomic_helper_update_plane,
  646. .disable_plane = drm_atomic_helper_disable_plane,
  647. .destroy = tegra_plane_destroy,
  648. .reset = tegra_plane_reset,
  649. .atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
  650. .atomic_destroy_state = tegra_plane_atomic_destroy_state,
  651. };
  652. static const struct drm_plane_helper_funcs tegra_cursor_plane_helper_funcs = {
  653. .atomic_check = tegra_cursor_atomic_check,
  654. .atomic_update = tegra_cursor_atomic_update,
  655. .atomic_disable = tegra_cursor_atomic_disable,
  656. };
  657. static struct drm_plane *tegra_dc_cursor_plane_create(struct drm_device *drm,
  658. struct tegra_dc *dc)
  659. {
  660. struct tegra_plane *plane;
  661. unsigned int num_formats;
  662. const u32 *formats;
  663. int err;
  664. plane = kzalloc(sizeof(*plane), GFP_KERNEL);
  665. if (!plane)
  666. return ERR_PTR(-ENOMEM);
  667. /*
  668. * This index is kind of fake. The cursor isn't a regular plane, but
  669. * its update and activation request bits in DC_CMD_STATE_CONTROL do
  670. * use the same programming. Setting this fake index here allows the
  671. * code in tegra_add_plane_state() to do the right thing without the
  672. * need to special-casing the cursor plane.
  673. */
  674. plane->index = 6;
  675. num_formats = ARRAY_SIZE(tegra_cursor_plane_formats);
  676. formats = tegra_cursor_plane_formats;
  677. err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
  678. &tegra_cursor_plane_funcs, formats,
  679. num_formats, DRM_PLANE_TYPE_CURSOR,
  680. NULL);
  681. if (err < 0) {
  682. kfree(plane);
  683. return ERR_PTR(err);
  684. }
  685. drm_plane_helper_add(&plane->base, &tegra_cursor_plane_helper_funcs);
  686. return &plane->base;
  687. }
  688. static void tegra_overlay_plane_destroy(struct drm_plane *plane)
  689. {
  690. tegra_plane_destroy(plane);
  691. }
  692. static const struct drm_plane_funcs tegra_overlay_plane_funcs = {
  693. .update_plane = drm_atomic_helper_update_plane,
  694. .disable_plane = drm_atomic_helper_disable_plane,
  695. .destroy = tegra_overlay_plane_destroy,
  696. .reset = tegra_plane_reset,
  697. .atomic_duplicate_state = tegra_plane_atomic_duplicate_state,
  698. .atomic_destroy_state = tegra_plane_atomic_destroy_state,
  699. };
  700. static const uint32_t tegra_overlay_plane_formats[] = {
  701. DRM_FORMAT_XBGR8888,
  702. DRM_FORMAT_XRGB8888,
  703. DRM_FORMAT_RGB565,
  704. DRM_FORMAT_UYVY,
  705. DRM_FORMAT_YUYV,
  706. DRM_FORMAT_YUV420,
  707. DRM_FORMAT_YUV422,
  708. };
  709. static const struct drm_plane_helper_funcs tegra_overlay_plane_helper_funcs = {
  710. .atomic_check = tegra_plane_atomic_check,
  711. .atomic_update = tegra_plane_atomic_update,
  712. .atomic_disable = tegra_plane_atomic_disable,
  713. };
  714. static struct drm_plane *tegra_dc_overlay_plane_create(struct drm_device *drm,
  715. struct tegra_dc *dc,
  716. unsigned int index)
  717. {
  718. struct tegra_plane *plane;
  719. unsigned int num_formats;
  720. const u32 *formats;
  721. int err;
  722. plane = kzalloc(sizeof(*plane), GFP_KERNEL);
  723. if (!plane)
  724. return ERR_PTR(-ENOMEM);
  725. plane->index = index;
  726. num_formats = ARRAY_SIZE(tegra_overlay_plane_formats);
  727. formats = tegra_overlay_plane_formats;
  728. err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
  729. &tegra_overlay_plane_funcs, formats,
  730. num_formats, DRM_PLANE_TYPE_OVERLAY,
  731. NULL);
  732. if (err < 0) {
  733. kfree(plane);
  734. return ERR_PTR(err);
  735. }
  736. drm_plane_helper_add(&plane->base, &tegra_overlay_plane_helper_funcs);
  737. return &plane->base;
  738. }
  739. static int tegra_dc_add_planes(struct drm_device *drm, struct tegra_dc *dc)
  740. {
  741. struct drm_plane *plane;
  742. unsigned int i;
  743. for (i = 0; i < 2; i++) {
  744. plane = tegra_dc_overlay_plane_create(drm, dc, 1 + i);
  745. if (IS_ERR(plane))
  746. return PTR_ERR(plane);
  747. }
  748. return 0;
  749. }
  750. static u32 tegra_dc_get_vblank_counter(struct drm_crtc *crtc)
  751. {
  752. struct tegra_dc *dc = to_tegra_dc(crtc);
  753. if (dc->syncpt)
  754. return host1x_syncpt_read(dc->syncpt);
  755. /* fallback to software emulated VBLANK counter */
  756. return drm_crtc_vblank_count(&dc->base);
  757. }
  758. static int tegra_dc_enable_vblank(struct drm_crtc *crtc)
  759. {
  760. struct tegra_dc *dc = to_tegra_dc(crtc);
  761. unsigned long value, flags;
  762. spin_lock_irqsave(&dc->lock, flags);
  763. value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
  764. value |= VBLANK_INT;
  765. tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
  766. spin_unlock_irqrestore(&dc->lock, flags);
  767. return 0;
  768. }
  769. static void tegra_dc_disable_vblank(struct drm_crtc *crtc)
  770. {
  771. struct tegra_dc *dc = to_tegra_dc(crtc);
  772. unsigned long value, flags;
  773. spin_lock_irqsave(&dc->lock, flags);
  774. value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
  775. value &= ~VBLANK_INT;
  776. tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
  777. spin_unlock_irqrestore(&dc->lock, flags);
  778. }
  779. static void tegra_dc_finish_page_flip(struct tegra_dc *dc)
  780. {
  781. struct drm_device *drm = dc->base.dev;
  782. struct drm_crtc *crtc = &dc->base;
  783. unsigned long flags, base;
  784. struct tegra_bo *bo;
  785. spin_lock_irqsave(&drm->event_lock, flags);
  786. if (!dc->event) {
  787. spin_unlock_irqrestore(&drm->event_lock, flags);
  788. return;
  789. }
  790. bo = tegra_fb_get_plane(crtc->primary->fb, 0);
  791. spin_lock(&dc->lock);
  792. /* check if new start address has been latched */
  793. tegra_dc_writel(dc, WINDOW_A_SELECT, DC_CMD_DISPLAY_WINDOW_HEADER);
  794. tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
  795. base = tegra_dc_readl(dc, DC_WINBUF_START_ADDR);
  796. tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
  797. spin_unlock(&dc->lock);
  798. if (base == bo->paddr + crtc->primary->fb->offsets[0]) {
  799. drm_crtc_send_vblank_event(crtc, dc->event);
  800. drm_crtc_vblank_put(crtc);
  801. dc->event = NULL;
  802. }
  803. spin_unlock_irqrestore(&drm->event_lock, flags);
  804. }
  805. static void tegra_dc_destroy(struct drm_crtc *crtc)
  806. {
  807. drm_crtc_cleanup(crtc);
  808. }
  809. static void tegra_crtc_reset(struct drm_crtc *crtc)
  810. {
  811. struct tegra_dc_state *state;
  812. if (crtc->state)
  813. __drm_atomic_helper_crtc_destroy_state(crtc->state);
  814. kfree(crtc->state);
  815. crtc->state = NULL;
  816. state = kzalloc(sizeof(*state), GFP_KERNEL);
  817. if (state) {
  818. crtc->state = &state->base;
  819. crtc->state->crtc = crtc;
  820. }
  821. drm_crtc_vblank_reset(crtc);
  822. }
  823. static struct drm_crtc_state *
  824. tegra_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
  825. {
  826. struct tegra_dc_state *state = to_dc_state(crtc->state);
  827. struct tegra_dc_state *copy;
  828. copy = kmalloc(sizeof(*copy), GFP_KERNEL);
  829. if (!copy)
  830. return NULL;
  831. __drm_atomic_helper_crtc_duplicate_state(crtc, &copy->base);
  832. copy->clk = state->clk;
  833. copy->pclk = state->pclk;
  834. copy->div = state->div;
  835. copy->planes = state->planes;
  836. return &copy->base;
  837. }
  838. static void tegra_crtc_atomic_destroy_state(struct drm_crtc *crtc,
  839. struct drm_crtc_state *state)
  840. {
  841. __drm_atomic_helper_crtc_destroy_state(state);
  842. kfree(state);
  843. }
  844. static const struct drm_crtc_funcs tegra_crtc_funcs = {
  845. .page_flip = drm_atomic_helper_page_flip,
  846. .set_config = drm_atomic_helper_set_config,
  847. .destroy = tegra_dc_destroy,
  848. .reset = tegra_crtc_reset,
  849. .atomic_duplicate_state = tegra_crtc_atomic_duplicate_state,
  850. .atomic_destroy_state = tegra_crtc_atomic_destroy_state,
  851. .get_vblank_counter = tegra_dc_get_vblank_counter,
  852. .enable_vblank = tegra_dc_enable_vblank,
  853. .disable_vblank = tegra_dc_disable_vblank,
  854. };
  855. static int tegra_dc_set_timings(struct tegra_dc *dc,
  856. struct drm_display_mode *mode)
  857. {
  858. unsigned int h_ref_to_sync = 1;
  859. unsigned int v_ref_to_sync = 1;
  860. unsigned long value;
  861. tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
  862. value = (v_ref_to_sync << 16) | h_ref_to_sync;
  863. tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
  864. value = ((mode->vsync_end - mode->vsync_start) << 16) |
  865. ((mode->hsync_end - mode->hsync_start) << 0);
  866. tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
  867. value = ((mode->vtotal - mode->vsync_end) << 16) |
  868. ((mode->htotal - mode->hsync_end) << 0);
  869. tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
  870. value = ((mode->vsync_start - mode->vdisplay) << 16) |
  871. ((mode->hsync_start - mode->hdisplay) << 0);
  872. tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
  873. value = (mode->vdisplay << 16) | mode->hdisplay;
  874. tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
  875. return 0;
  876. }
  877. /**
  878. * tegra_dc_state_setup_clock - check clock settings and store them in atomic
  879. * state
  880. * @dc: display controller
  881. * @crtc_state: CRTC atomic state
  882. * @clk: parent clock for display controller
  883. * @pclk: pixel clock
  884. * @div: shift clock divider
  885. *
  886. * Returns:
  887. * 0 on success or a negative error-code on failure.
  888. */
  889. int tegra_dc_state_setup_clock(struct tegra_dc *dc,
  890. struct drm_crtc_state *crtc_state,
  891. struct clk *clk, unsigned long pclk,
  892. unsigned int div)
  893. {
  894. struct tegra_dc_state *state = to_dc_state(crtc_state);
  895. if (!clk_has_parent(dc->clk, clk))
  896. return -EINVAL;
  897. state->clk = clk;
  898. state->pclk = pclk;
  899. state->div = div;
  900. return 0;
  901. }
  902. static void tegra_dc_commit_state(struct tegra_dc *dc,
  903. struct tegra_dc_state *state)
  904. {
  905. u32 value;
  906. int err;
  907. err = clk_set_parent(dc->clk, state->clk);
  908. if (err < 0)
  909. dev_err(dc->dev, "failed to set parent clock: %d\n", err);
  910. /*
  911. * Outputs may not want to change the parent clock rate. This is only
  912. * relevant to Tegra20 where only a single display PLL is available.
  913. * Since that PLL would typically be used for HDMI, an internal LVDS
  914. * panel would need to be driven by some other clock such as PLL_P
  915. * which is shared with other peripherals. Changing the clock rate
  916. * should therefore be avoided.
  917. */
  918. if (state->pclk > 0) {
  919. err = clk_set_rate(state->clk, state->pclk);
  920. if (err < 0)
  921. dev_err(dc->dev,
  922. "failed to set clock rate to %lu Hz\n",
  923. state->pclk);
  924. }
  925. DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk),
  926. state->div);
  927. DRM_DEBUG_KMS("pclk: %lu\n", state->pclk);
  928. value = SHIFT_CLK_DIVIDER(state->div) | PIXEL_CLK_DIVIDER_PCD1;
  929. tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
  930. }
  931. static void tegra_dc_stop(struct tegra_dc *dc)
  932. {
  933. u32 value;
  934. /* stop the display controller */
  935. value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
  936. value &= ~DISP_CTRL_MODE_MASK;
  937. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
  938. tegra_dc_commit(dc);
  939. }
  940. static bool tegra_dc_idle(struct tegra_dc *dc)
  941. {
  942. u32 value;
  943. value = tegra_dc_readl_active(dc, DC_CMD_DISPLAY_COMMAND);
  944. return (value & DISP_CTRL_MODE_MASK) == 0;
  945. }
  946. static int tegra_dc_wait_idle(struct tegra_dc *dc, unsigned long timeout)
  947. {
  948. timeout = jiffies + msecs_to_jiffies(timeout);
  949. while (time_before(jiffies, timeout)) {
  950. if (tegra_dc_idle(dc))
  951. return 0;
  952. usleep_range(1000, 2000);
  953. }
  954. dev_dbg(dc->dev, "timeout waiting for DC to become idle\n");
  955. return -ETIMEDOUT;
  956. }
  957. static void tegra_crtc_disable(struct drm_crtc *crtc)
  958. {
  959. struct tegra_dc *dc = to_tegra_dc(crtc);
  960. u32 value;
  961. if (!tegra_dc_idle(dc)) {
  962. tegra_dc_stop(dc);
  963. /*
  964. * Ignore the return value, there isn't anything useful to do
  965. * in case this fails.
  966. */
  967. tegra_dc_wait_idle(dc, 100);
  968. }
  969. /*
  970. * This should really be part of the RGB encoder driver, but clearing
  971. * these bits has the side-effect of stopping the display controller.
  972. * When that happens no VBLANK interrupts will be raised. At the same
  973. * time the encoder is disabled before the display controller, so the
  974. * above code is always going to timeout waiting for the controller
  975. * to go idle.
  976. *
  977. * Given the close coupling between the RGB encoder and the display
  978. * controller doing it here is still kind of okay. None of the other
  979. * encoder drivers require these bits to be cleared.
  980. *
  981. * XXX: Perhaps given that the display controller is switched off at
  982. * this point anyway maybe clearing these bits isn't even useful for
  983. * the RGB encoder?
  984. */
  985. if (dc->rgb) {
  986. value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
  987. value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
  988. PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
  989. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
  990. }
  991. tegra_dc_stats_reset(&dc->stats);
  992. drm_crtc_vblank_off(crtc);
  993. pm_runtime_put_sync(dc->dev);
  994. }
  995. static void tegra_crtc_atomic_enable(struct drm_crtc *crtc,
  996. struct drm_crtc_state *old_state)
  997. {
  998. struct drm_display_mode *mode = &crtc->state->adjusted_mode;
  999. struct tegra_dc_state *state = to_dc_state(crtc->state);
  1000. struct tegra_dc *dc = to_tegra_dc(crtc);
  1001. u32 value;
  1002. pm_runtime_get_sync(dc->dev);
  1003. /* initialize display controller */
  1004. if (dc->syncpt) {
  1005. u32 syncpt = host1x_syncpt_id(dc->syncpt);
  1006. value = SYNCPT_CNTRL_NO_STALL;
  1007. tegra_dc_writel(dc, value, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
  1008. value = SYNCPT_VSYNC_ENABLE | syncpt;
  1009. tegra_dc_writel(dc, value, DC_CMD_CONT_SYNCPT_VSYNC);
  1010. }
  1011. value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
  1012. WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
  1013. tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
  1014. value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
  1015. WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
  1016. tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
  1017. /* initialize timer */
  1018. value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
  1019. WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
  1020. tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
  1021. value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
  1022. WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
  1023. tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
  1024. value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
  1025. WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
  1026. tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
  1027. value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
  1028. WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
  1029. tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
  1030. if (dc->soc->supports_border_color)
  1031. tegra_dc_writel(dc, 0, DC_DISP_BORDER_COLOR);
  1032. /* apply PLL and pixel clock changes */
  1033. tegra_dc_commit_state(dc, state);
  1034. /* program display mode */
  1035. tegra_dc_set_timings(dc, mode);
  1036. /* interlacing isn't supported yet, so disable it */
  1037. if (dc->soc->supports_interlacing) {
  1038. value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
  1039. value &= ~INTERLACE_ENABLE;
  1040. tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
  1041. }
  1042. value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
  1043. value &= ~DISP_CTRL_MODE_MASK;
  1044. value |= DISP_CTRL_MODE_C_DISPLAY;
  1045. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
  1046. value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
  1047. value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
  1048. PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
  1049. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
  1050. tegra_dc_commit(dc);
  1051. drm_crtc_vblank_on(crtc);
  1052. }
  1053. static int tegra_crtc_atomic_check(struct drm_crtc *crtc,
  1054. struct drm_crtc_state *state)
  1055. {
  1056. return 0;
  1057. }
  1058. static void tegra_crtc_atomic_begin(struct drm_crtc *crtc,
  1059. struct drm_crtc_state *old_crtc_state)
  1060. {
  1061. struct tegra_dc *dc = to_tegra_dc(crtc);
  1062. if (crtc->state->event) {
  1063. crtc->state->event->pipe = drm_crtc_index(crtc);
  1064. WARN_ON(drm_crtc_vblank_get(crtc) != 0);
  1065. dc->event = crtc->state->event;
  1066. crtc->state->event = NULL;
  1067. }
  1068. }
  1069. static void tegra_crtc_atomic_flush(struct drm_crtc *crtc,
  1070. struct drm_crtc_state *old_crtc_state)
  1071. {
  1072. struct tegra_dc_state *state = to_dc_state(crtc->state);
  1073. struct tegra_dc *dc = to_tegra_dc(crtc);
  1074. tegra_dc_writel(dc, state->planes << 8, DC_CMD_STATE_CONTROL);
  1075. tegra_dc_writel(dc, state->planes, DC_CMD_STATE_CONTROL);
  1076. }
  1077. static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
  1078. .disable = tegra_crtc_disable,
  1079. .atomic_check = tegra_crtc_atomic_check,
  1080. .atomic_begin = tegra_crtc_atomic_begin,
  1081. .atomic_flush = tegra_crtc_atomic_flush,
  1082. .atomic_enable = tegra_crtc_atomic_enable,
  1083. };
  1084. static irqreturn_t tegra_dc_irq(int irq, void *data)
  1085. {
  1086. struct tegra_dc *dc = data;
  1087. unsigned long status;
  1088. status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
  1089. tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
  1090. if (status & FRAME_END_INT) {
  1091. /*
  1092. dev_dbg(dc->dev, "%s(): frame end\n", __func__);
  1093. */
  1094. dc->stats.frames++;
  1095. }
  1096. if (status & VBLANK_INT) {
  1097. /*
  1098. dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
  1099. */
  1100. drm_crtc_handle_vblank(&dc->base);
  1101. tegra_dc_finish_page_flip(dc);
  1102. dc->stats.vblank++;
  1103. }
  1104. if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
  1105. /*
  1106. dev_dbg(dc->dev, "%s(): underflow\n", __func__);
  1107. */
  1108. dc->stats.underflow++;
  1109. }
  1110. if (status & (WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT)) {
  1111. /*
  1112. dev_dbg(dc->dev, "%s(): overflow\n", __func__);
  1113. */
  1114. dc->stats.overflow++;
  1115. }
  1116. return IRQ_HANDLED;
  1117. }
  1118. static int tegra_dc_show_regs(struct seq_file *s, void *data)
  1119. {
  1120. struct drm_info_node *node = s->private;
  1121. struct tegra_dc *dc = node->info_ent->data;
  1122. int err = 0;
  1123. drm_modeset_lock(&dc->base.mutex, NULL);
  1124. if (!dc->base.state->active) {
  1125. err = -EBUSY;
  1126. goto unlock;
  1127. }
  1128. #define DUMP_REG(name) \
  1129. seq_printf(s, "%-40s %#05x %08x\n", #name, name, \
  1130. tegra_dc_readl(dc, name))
  1131. DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT);
  1132. DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
  1133. DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_ERROR);
  1134. DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT);
  1135. DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL);
  1136. DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_ERROR);
  1137. DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT);
  1138. DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL);
  1139. DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_ERROR);
  1140. DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT);
  1141. DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL);
  1142. DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_ERROR);
  1143. DUMP_REG(DC_CMD_CONT_SYNCPT_VSYNC);
  1144. DUMP_REG(DC_CMD_DISPLAY_COMMAND_OPTION0);
  1145. DUMP_REG(DC_CMD_DISPLAY_COMMAND);
  1146. DUMP_REG(DC_CMD_SIGNAL_RAISE);
  1147. DUMP_REG(DC_CMD_DISPLAY_POWER_CONTROL);
  1148. DUMP_REG(DC_CMD_INT_STATUS);
  1149. DUMP_REG(DC_CMD_INT_MASK);
  1150. DUMP_REG(DC_CMD_INT_ENABLE);
  1151. DUMP_REG(DC_CMD_INT_TYPE);
  1152. DUMP_REG(DC_CMD_INT_POLARITY);
  1153. DUMP_REG(DC_CMD_SIGNAL_RAISE1);
  1154. DUMP_REG(DC_CMD_SIGNAL_RAISE2);
  1155. DUMP_REG(DC_CMD_SIGNAL_RAISE3);
  1156. DUMP_REG(DC_CMD_STATE_ACCESS);
  1157. DUMP_REG(DC_CMD_STATE_CONTROL);
  1158. DUMP_REG(DC_CMD_DISPLAY_WINDOW_HEADER);
  1159. DUMP_REG(DC_CMD_REG_ACT_CONTROL);
  1160. DUMP_REG(DC_COM_CRC_CONTROL);
  1161. DUMP_REG(DC_COM_CRC_CHECKSUM);
  1162. DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(0));
  1163. DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(1));
  1164. DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(2));
  1165. DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(3));
  1166. DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(0));
  1167. DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(1));
  1168. DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(2));
  1169. DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(3));
  1170. DUMP_REG(DC_COM_PIN_OUTPUT_DATA(0));
  1171. DUMP_REG(DC_COM_PIN_OUTPUT_DATA(1));
  1172. DUMP_REG(DC_COM_PIN_OUTPUT_DATA(2));
  1173. DUMP_REG(DC_COM_PIN_OUTPUT_DATA(3));
  1174. DUMP_REG(DC_COM_PIN_INPUT_ENABLE(0));
  1175. DUMP_REG(DC_COM_PIN_INPUT_ENABLE(1));
  1176. DUMP_REG(DC_COM_PIN_INPUT_ENABLE(2));
  1177. DUMP_REG(DC_COM_PIN_INPUT_ENABLE(3));
  1178. DUMP_REG(DC_COM_PIN_INPUT_DATA(0));
  1179. DUMP_REG(DC_COM_PIN_INPUT_DATA(1));
  1180. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(0));
  1181. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(1));
  1182. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(2));
  1183. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(3));
  1184. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(4));
  1185. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(5));
  1186. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(6));
  1187. DUMP_REG(DC_COM_PIN_MISC_CONTROL);
  1188. DUMP_REG(DC_COM_PIN_PM0_CONTROL);
  1189. DUMP_REG(DC_COM_PIN_PM0_DUTY_CYCLE);
  1190. DUMP_REG(DC_COM_PIN_PM1_CONTROL);
  1191. DUMP_REG(DC_COM_PIN_PM1_DUTY_CYCLE);
  1192. DUMP_REG(DC_COM_SPI_CONTROL);
  1193. DUMP_REG(DC_COM_SPI_START_BYTE);
  1194. DUMP_REG(DC_COM_HSPI_WRITE_DATA_AB);
  1195. DUMP_REG(DC_COM_HSPI_WRITE_DATA_CD);
  1196. DUMP_REG(DC_COM_HSPI_CS_DC);
  1197. DUMP_REG(DC_COM_SCRATCH_REGISTER_A);
  1198. DUMP_REG(DC_COM_SCRATCH_REGISTER_B);
  1199. DUMP_REG(DC_COM_GPIO_CTRL);
  1200. DUMP_REG(DC_COM_GPIO_DEBOUNCE_COUNTER);
  1201. DUMP_REG(DC_COM_CRC_CHECKSUM_LATCHED);
  1202. DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS0);
  1203. DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS1);
  1204. DUMP_REG(DC_DISP_DISP_WIN_OPTIONS);
  1205. DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY);
  1206. DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
  1207. DUMP_REG(DC_DISP_DISP_TIMING_OPTIONS);
  1208. DUMP_REG(DC_DISP_REF_TO_SYNC);
  1209. DUMP_REG(DC_DISP_SYNC_WIDTH);
  1210. DUMP_REG(DC_DISP_BACK_PORCH);
  1211. DUMP_REG(DC_DISP_ACTIVE);
  1212. DUMP_REG(DC_DISP_FRONT_PORCH);
  1213. DUMP_REG(DC_DISP_H_PULSE0_CONTROL);
  1214. DUMP_REG(DC_DISP_H_PULSE0_POSITION_A);
  1215. DUMP_REG(DC_DISP_H_PULSE0_POSITION_B);
  1216. DUMP_REG(DC_DISP_H_PULSE0_POSITION_C);
  1217. DUMP_REG(DC_DISP_H_PULSE0_POSITION_D);
  1218. DUMP_REG(DC_DISP_H_PULSE1_CONTROL);
  1219. DUMP_REG(DC_DISP_H_PULSE1_POSITION_A);
  1220. DUMP_REG(DC_DISP_H_PULSE1_POSITION_B);
  1221. DUMP_REG(DC_DISP_H_PULSE1_POSITION_C);
  1222. DUMP_REG(DC_DISP_H_PULSE1_POSITION_D);
  1223. DUMP_REG(DC_DISP_H_PULSE2_CONTROL);
  1224. DUMP_REG(DC_DISP_H_PULSE2_POSITION_A);
  1225. DUMP_REG(DC_DISP_H_PULSE2_POSITION_B);
  1226. DUMP_REG(DC_DISP_H_PULSE2_POSITION_C);
  1227. DUMP_REG(DC_DISP_H_PULSE2_POSITION_D);
  1228. DUMP_REG(DC_DISP_V_PULSE0_CONTROL);
  1229. DUMP_REG(DC_DISP_V_PULSE0_POSITION_A);
  1230. DUMP_REG(DC_DISP_V_PULSE0_POSITION_B);
  1231. DUMP_REG(DC_DISP_V_PULSE0_POSITION_C);
  1232. DUMP_REG(DC_DISP_V_PULSE1_CONTROL);
  1233. DUMP_REG(DC_DISP_V_PULSE1_POSITION_A);
  1234. DUMP_REG(DC_DISP_V_PULSE1_POSITION_B);
  1235. DUMP_REG(DC_DISP_V_PULSE1_POSITION_C);
  1236. DUMP_REG(DC_DISP_V_PULSE2_CONTROL);
  1237. DUMP_REG(DC_DISP_V_PULSE2_POSITION_A);
  1238. DUMP_REG(DC_DISP_V_PULSE3_CONTROL);
  1239. DUMP_REG(DC_DISP_V_PULSE3_POSITION_A);
  1240. DUMP_REG(DC_DISP_M0_CONTROL);
  1241. DUMP_REG(DC_DISP_M1_CONTROL);
  1242. DUMP_REG(DC_DISP_DI_CONTROL);
  1243. DUMP_REG(DC_DISP_PP_CONTROL);
  1244. DUMP_REG(DC_DISP_PP_SELECT_A);
  1245. DUMP_REG(DC_DISP_PP_SELECT_B);
  1246. DUMP_REG(DC_DISP_PP_SELECT_C);
  1247. DUMP_REG(DC_DISP_PP_SELECT_D);
  1248. DUMP_REG(DC_DISP_DISP_CLOCK_CONTROL);
  1249. DUMP_REG(DC_DISP_DISP_INTERFACE_CONTROL);
  1250. DUMP_REG(DC_DISP_DISP_COLOR_CONTROL);
  1251. DUMP_REG(DC_DISP_SHIFT_CLOCK_OPTIONS);
  1252. DUMP_REG(DC_DISP_DATA_ENABLE_OPTIONS);
  1253. DUMP_REG(DC_DISP_SERIAL_INTERFACE_OPTIONS);
  1254. DUMP_REG(DC_DISP_LCD_SPI_OPTIONS);
  1255. DUMP_REG(DC_DISP_BORDER_COLOR);
  1256. DUMP_REG(DC_DISP_COLOR_KEY0_LOWER);
  1257. DUMP_REG(DC_DISP_COLOR_KEY0_UPPER);
  1258. DUMP_REG(DC_DISP_COLOR_KEY1_LOWER);
  1259. DUMP_REG(DC_DISP_COLOR_KEY1_UPPER);
  1260. DUMP_REG(DC_DISP_CURSOR_FOREGROUND);
  1261. DUMP_REG(DC_DISP_CURSOR_BACKGROUND);
  1262. DUMP_REG(DC_DISP_CURSOR_START_ADDR);
  1263. DUMP_REG(DC_DISP_CURSOR_START_ADDR_NS);
  1264. DUMP_REG(DC_DISP_CURSOR_POSITION);
  1265. DUMP_REG(DC_DISP_CURSOR_POSITION_NS);
  1266. DUMP_REG(DC_DISP_INIT_SEQ_CONTROL);
  1267. DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_A);
  1268. DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_B);
  1269. DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_C);
  1270. DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_D);
  1271. DUMP_REG(DC_DISP_DC_MCCIF_FIFOCTRL);
  1272. DUMP_REG(DC_DISP_MCCIF_DISPLAY0A_HYST);
  1273. DUMP_REG(DC_DISP_MCCIF_DISPLAY0B_HYST);
  1274. DUMP_REG(DC_DISP_MCCIF_DISPLAY1A_HYST);
  1275. DUMP_REG(DC_DISP_MCCIF_DISPLAY1B_HYST);
  1276. DUMP_REG(DC_DISP_DAC_CRT_CTRL);
  1277. DUMP_REG(DC_DISP_DISP_MISC_CONTROL);
  1278. DUMP_REG(DC_DISP_SD_CONTROL);
  1279. DUMP_REG(DC_DISP_SD_CSC_COEFF);
  1280. DUMP_REG(DC_DISP_SD_LUT(0));
  1281. DUMP_REG(DC_DISP_SD_LUT(1));
  1282. DUMP_REG(DC_DISP_SD_LUT(2));
  1283. DUMP_REG(DC_DISP_SD_LUT(3));
  1284. DUMP_REG(DC_DISP_SD_LUT(4));
  1285. DUMP_REG(DC_DISP_SD_LUT(5));
  1286. DUMP_REG(DC_DISP_SD_LUT(6));
  1287. DUMP_REG(DC_DISP_SD_LUT(7));
  1288. DUMP_REG(DC_DISP_SD_LUT(8));
  1289. DUMP_REG(DC_DISP_SD_FLICKER_CONTROL);
  1290. DUMP_REG(DC_DISP_DC_PIXEL_COUNT);
  1291. DUMP_REG(DC_DISP_SD_HISTOGRAM(0));
  1292. DUMP_REG(DC_DISP_SD_HISTOGRAM(1));
  1293. DUMP_REG(DC_DISP_SD_HISTOGRAM(2));
  1294. DUMP_REG(DC_DISP_SD_HISTOGRAM(3));
  1295. DUMP_REG(DC_DISP_SD_HISTOGRAM(4));
  1296. DUMP_REG(DC_DISP_SD_HISTOGRAM(5));
  1297. DUMP_REG(DC_DISP_SD_HISTOGRAM(6));
  1298. DUMP_REG(DC_DISP_SD_HISTOGRAM(7));
  1299. DUMP_REG(DC_DISP_SD_BL_TF(0));
  1300. DUMP_REG(DC_DISP_SD_BL_TF(1));
  1301. DUMP_REG(DC_DISP_SD_BL_TF(2));
  1302. DUMP_REG(DC_DISP_SD_BL_TF(3));
  1303. DUMP_REG(DC_DISP_SD_BL_CONTROL);
  1304. DUMP_REG(DC_DISP_SD_HW_K_VALUES);
  1305. DUMP_REG(DC_DISP_SD_MAN_K_VALUES);
  1306. DUMP_REG(DC_DISP_CURSOR_START_ADDR_HI);
  1307. DUMP_REG(DC_DISP_BLEND_CURSOR_CONTROL);
  1308. DUMP_REG(DC_WIN_WIN_OPTIONS);
  1309. DUMP_REG(DC_WIN_BYTE_SWAP);
  1310. DUMP_REG(DC_WIN_BUFFER_CONTROL);
  1311. DUMP_REG(DC_WIN_COLOR_DEPTH);
  1312. DUMP_REG(DC_WIN_POSITION);
  1313. DUMP_REG(DC_WIN_SIZE);
  1314. DUMP_REG(DC_WIN_PRESCALED_SIZE);
  1315. DUMP_REG(DC_WIN_H_INITIAL_DDA);
  1316. DUMP_REG(DC_WIN_V_INITIAL_DDA);
  1317. DUMP_REG(DC_WIN_DDA_INC);
  1318. DUMP_REG(DC_WIN_LINE_STRIDE);
  1319. DUMP_REG(DC_WIN_BUF_STRIDE);
  1320. DUMP_REG(DC_WIN_UV_BUF_STRIDE);
  1321. DUMP_REG(DC_WIN_BUFFER_ADDR_MODE);
  1322. DUMP_REG(DC_WIN_DV_CONTROL);
  1323. DUMP_REG(DC_WIN_BLEND_NOKEY);
  1324. DUMP_REG(DC_WIN_BLEND_1WIN);
  1325. DUMP_REG(DC_WIN_BLEND_2WIN_X);
  1326. DUMP_REG(DC_WIN_BLEND_2WIN_Y);
  1327. DUMP_REG(DC_WIN_BLEND_3WIN_XY);
  1328. DUMP_REG(DC_WIN_HP_FETCH_CONTROL);
  1329. DUMP_REG(DC_WINBUF_START_ADDR);
  1330. DUMP_REG(DC_WINBUF_START_ADDR_NS);
  1331. DUMP_REG(DC_WINBUF_START_ADDR_U);
  1332. DUMP_REG(DC_WINBUF_START_ADDR_U_NS);
  1333. DUMP_REG(DC_WINBUF_START_ADDR_V);
  1334. DUMP_REG(DC_WINBUF_START_ADDR_V_NS);
  1335. DUMP_REG(DC_WINBUF_ADDR_H_OFFSET);
  1336. DUMP_REG(DC_WINBUF_ADDR_H_OFFSET_NS);
  1337. DUMP_REG(DC_WINBUF_ADDR_V_OFFSET);
  1338. DUMP_REG(DC_WINBUF_ADDR_V_OFFSET_NS);
  1339. DUMP_REG(DC_WINBUF_UFLOW_STATUS);
  1340. DUMP_REG(DC_WINBUF_AD_UFLOW_STATUS);
  1341. DUMP_REG(DC_WINBUF_BD_UFLOW_STATUS);
  1342. DUMP_REG(DC_WINBUF_CD_UFLOW_STATUS);
  1343. #undef DUMP_REG
  1344. unlock:
  1345. drm_modeset_unlock(&dc->base.mutex);
  1346. return err;
  1347. }
  1348. static int tegra_dc_show_crc(struct seq_file *s, void *data)
  1349. {
  1350. struct drm_info_node *node = s->private;
  1351. struct tegra_dc *dc = node->info_ent->data;
  1352. int err = 0;
  1353. u32 value;
  1354. drm_modeset_lock(&dc->base.mutex, NULL);
  1355. if (!dc->base.state->active) {
  1356. err = -EBUSY;
  1357. goto unlock;
  1358. }
  1359. value = DC_COM_CRC_CONTROL_ACTIVE_DATA | DC_COM_CRC_CONTROL_ENABLE;
  1360. tegra_dc_writel(dc, value, DC_COM_CRC_CONTROL);
  1361. tegra_dc_commit(dc);
  1362. drm_crtc_wait_one_vblank(&dc->base);
  1363. drm_crtc_wait_one_vblank(&dc->base);
  1364. value = tegra_dc_readl(dc, DC_COM_CRC_CHECKSUM);
  1365. seq_printf(s, "%08x\n", value);
  1366. tegra_dc_writel(dc, 0, DC_COM_CRC_CONTROL);
  1367. unlock:
  1368. drm_modeset_unlock(&dc->base.mutex);
  1369. return err;
  1370. }
  1371. static int tegra_dc_show_stats(struct seq_file *s, void *data)
  1372. {
  1373. struct drm_info_node *node = s->private;
  1374. struct tegra_dc *dc = node->info_ent->data;
  1375. seq_printf(s, "frames: %lu\n", dc->stats.frames);
  1376. seq_printf(s, "vblank: %lu\n", dc->stats.vblank);
  1377. seq_printf(s, "underflow: %lu\n", dc->stats.underflow);
  1378. seq_printf(s, "overflow: %lu\n", dc->stats.overflow);
  1379. return 0;
  1380. }
  1381. static struct drm_info_list debugfs_files[] = {
  1382. { "regs", tegra_dc_show_regs, 0, NULL },
  1383. { "crc", tegra_dc_show_crc, 0, NULL },
  1384. { "stats", tegra_dc_show_stats, 0, NULL },
  1385. };
  1386. static int tegra_dc_debugfs_init(struct tegra_dc *dc, struct drm_minor *minor)
  1387. {
  1388. unsigned int i;
  1389. char *name;
  1390. int err;
  1391. name = kasprintf(GFP_KERNEL, "dc.%d", dc->pipe);
  1392. dc->debugfs = debugfs_create_dir(name, minor->debugfs_root);
  1393. kfree(name);
  1394. if (!dc->debugfs)
  1395. return -ENOMEM;
  1396. dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
  1397. GFP_KERNEL);
  1398. if (!dc->debugfs_files) {
  1399. err = -ENOMEM;
  1400. goto remove;
  1401. }
  1402. for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
  1403. dc->debugfs_files[i].data = dc;
  1404. err = drm_debugfs_create_files(dc->debugfs_files,
  1405. ARRAY_SIZE(debugfs_files),
  1406. dc->debugfs, minor);
  1407. if (err < 0)
  1408. goto free;
  1409. dc->minor = minor;
  1410. return 0;
  1411. free:
  1412. kfree(dc->debugfs_files);
  1413. dc->debugfs_files = NULL;
  1414. remove:
  1415. debugfs_remove(dc->debugfs);
  1416. dc->debugfs = NULL;
  1417. return err;
  1418. }
  1419. static int tegra_dc_debugfs_exit(struct tegra_dc *dc)
  1420. {
  1421. drm_debugfs_remove_files(dc->debugfs_files, ARRAY_SIZE(debugfs_files),
  1422. dc->minor);
  1423. dc->minor = NULL;
  1424. kfree(dc->debugfs_files);
  1425. dc->debugfs_files = NULL;
  1426. debugfs_remove(dc->debugfs);
  1427. dc->debugfs = NULL;
  1428. return 0;
  1429. }
  1430. static int tegra_dc_init(struct host1x_client *client)
  1431. {
  1432. struct drm_device *drm = dev_get_drvdata(client->parent);
  1433. unsigned long flags = HOST1X_SYNCPT_CLIENT_MANAGED;
  1434. struct tegra_dc *dc = host1x_client_to_dc(client);
  1435. struct tegra_drm *tegra = drm->dev_private;
  1436. struct drm_plane *primary = NULL;
  1437. struct drm_plane *cursor = NULL;
  1438. int err;
  1439. dc->syncpt = host1x_syncpt_request(dc->dev, flags);
  1440. if (!dc->syncpt)
  1441. dev_warn(dc->dev, "failed to allocate syncpoint\n");
  1442. if (tegra->domain) {
  1443. err = iommu_attach_device(tegra->domain, dc->dev);
  1444. if (err < 0) {
  1445. dev_err(dc->dev, "failed to attach to domain: %d\n",
  1446. err);
  1447. return err;
  1448. }
  1449. dc->domain = tegra->domain;
  1450. }
  1451. primary = tegra_dc_primary_plane_create(drm, dc);
  1452. if (IS_ERR(primary)) {
  1453. err = PTR_ERR(primary);
  1454. goto cleanup;
  1455. }
  1456. if (dc->soc->supports_cursor) {
  1457. cursor = tegra_dc_cursor_plane_create(drm, dc);
  1458. if (IS_ERR(cursor)) {
  1459. err = PTR_ERR(cursor);
  1460. goto cleanup;
  1461. }
  1462. }
  1463. err = drm_crtc_init_with_planes(drm, &dc->base, primary, cursor,
  1464. &tegra_crtc_funcs, NULL);
  1465. if (err < 0)
  1466. goto cleanup;
  1467. drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
  1468. /*
  1469. * Keep track of the minimum pitch alignment across all display
  1470. * controllers.
  1471. */
  1472. if (dc->soc->pitch_align > tegra->pitch_align)
  1473. tegra->pitch_align = dc->soc->pitch_align;
  1474. err = tegra_dc_rgb_init(drm, dc);
  1475. if (err < 0 && err != -ENODEV) {
  1476. dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
  1477. goto cleanup;
  1478. }
  1479. err = tegra_dc_add_planes(drm, dc);
  1480. if (err < 0)
  1481. goto cleanup;
  1482. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  1483. err = tegra_dc_debugfs_init(dc, drm->primary);
  1484. if (err < 0)
  1485. dev_err(dc->dev, "debugfs setup failed: %d\n", err);
  1486. }
  1487. err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
  1488. dev_name(dc->dev), dc);
  1489. if (err < 0) {
  1490. dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
  1491. err);
  1492. goto cleanup;
  1493. }
  1494. return 0;
  1495. cleanup:
  1496. if (cursor)
  1497. drm_plane_cleanup(cursor);
  1498. if (primary)
  1499. drm_plane_cleanup(primary);
  1500. if (tegra->domain) {
  1501. iommu_detach_device(tegra->domain, dc->dev);
  1502. dc->domain = NULL;
  1503. }
  1504. return err;
  1505. }
  1506. static int tegra_dc_exit(struct host1x_client *client)
  1507. {
  1508. struct tegra_dc *dc = host1x_client_to_dc(client);
  1509. int err;
  1510. devm_free_irq(dc->dev, dc->irq, dc);
  1511. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  1512. err = tegra_dc_debugfs_exit(dc);
  1513. if (err < 0)
  1514. dev_err(dc->dev, "debugfs cleanup failed: %d\n", err);
  1515. }
  1516. err = tegra_dc_rgb_exit(dc);
  1517. if (err) {
  1518. dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
  1519. return err;
  1520. }
  1521. if (dc->domain) {
  1522. iommu_detach_device(dc->domain, dc->dev);
  1523. dc->domain = NULL;
  1524. }
  1525. host1x_syncpt_free(dc->syncpt);
  1526. return 0;
  1527. }
  1528. static const struct host1x_client_ops dc_client_ops = {
  1529. .init = tegra_dc_init,
  1530. .exit = tegra_dc_exit,
  1531. };
  1532. static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
  1533. .supports_border_color = true,
  1534. .supports_interlacing = false,
  1535. .supports_cursor = false,
  1536. .supports_block_linear = false,
  1537. .pitch_align = 8,
  1538. .has_powergate = false,
  1539. .broken_reset = true,
  1540. };
  1541. static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
  1542. .supports_border_color = true,
  1543. .supports_interlacing = false,
  1544. .supports_cursor = false,
  1545. .supports_block_linear = false,
  1546. .pitch_align = 8,
  1547. .has_powergate = false,
  1548. .broken_reset = false,
  1549. };
  1550. static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
  1551. .supports_border_color = true,
  1552. .supports_interlacing = false,
  1553. .supports_cursor = false,
  1554. .supports_block_linear = false,
  1555. .pitch_align = 64,
  1556. .has_powergate = true,
  1557. .broken_reset = false,
  1558. };
  1559. static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
  1560. .supports_border_color = false,
  1561. .supports_interlacing = true,
  1562. .supports_cursor = true,
  1563. .supports_block_linear = true,
  1564. .pitch_align = 64,
  1565. .has_powergate = true,
  1566. .broken_reset = false,
  1567. };
  1568. static const struct tegra_dc_soc_info tegra210_dc_soc_info = {
  1569. .supports_border_color = false,
  1570. .supports_interlacing = true,
  1571. .supports_cursor = true,
  1572. .supports_block_linear = true,
  1573. .pitch_align = 64,
  1574. .has_powergate = true,
  1575. .broken_reset = false,
  1576. };
  1577. static const struct of_device_id tegra_dc_of_match[] = {
  1578. {
  1579. .compatible = "nvidia,tegra210-dc",
  1580. .data = &tegra210_dc_soc_info,
  1581. }, {
  1582. .compatible = "nvidia,tegra124-dc",
  1583. .data = &tegra124_dc_soc_info,
  1584. }, {
  1585. .compatible = "nvidia,tegra114-dc",
  1586. .data = &tegra114_dc_soc_info,
  1587. }, {
  1588. .compatible = "nvidia,tegra30-dc",
  1589. .data = &tegra30_dc_soc_info,
  1590. }, {
  1591. .compatible = "nvidia,tegra20-dc",
  1592. .data = &tegra20_dc_soc_info,
  1593. }, {
  1594. /* sentinel */
  1595. }
  1596. };
  1597. MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
  1598. static int tegra_dc_parse_dt(struct tegra_dc *dc)
  1599. {
  1600. struct device_node *np;
  1601. u32 value = 0;
  1602. int err;
  1603. err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
  1604. if (err < 0) {
  1605. dev_err(dc->dev, "missing \"nvidia,head\" property\n");
  1606. /*
  1607. * If the nvidia,head property isn't present, try to find the
  1608. * correct head number by looking up the position of this
  1609. * display controller's node within the device tree. Assuming
  1610. * that the nodes are ordered properly in the DTS file and
  1611. * that the translation into a flattened device tree blob
  1612. * preserves that ordering this will actually yield the right
  1613. * head number.
  1614. *
  1615. * If those assumptions don't hold, this will still work for
  1616. * cases where only a single display controller is used.
  1617. */
  1618. for_each_matching_node(np, tegra_dc_of_match) {
  1619. if (np == dc->dev->of_node) {
  1620. of_node_put(np);
  1621. break;
  1622. }
  1623. value++;
  1624. }
  1625. }
  1626. dc->pipe = value;
  1627. return 0;
  1628. }
  1629. static int tegra_dc_probe(struct platform_device *pdev)
  1630. {
  1631. const struct of_device_id *id;
  1632. struct resource *regs;
  1633. struct tegra_dc *dc;
  1634. int err;
  1635. dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
  1636. if (!dc)
  1637. return -ENOMEM;
  1638. id = of_match_node(tegra_dc_of_match, pdev->dev.of_node);
  1639. if (!id)
  1640. return -ENODEV;
  1641. spin_lock_init(&dc->lock);
  1642. INIT_LIST_HEAD(&dc->list);
  1643. dc->dev = &pdev->dev;
  1644. dc->soc = id->data;
  1645. err = tegra_dc_parse_dt(dc);
  1646. if (err < 0)
  1647. return err;
  1648. dc->clk = devm_clk_get(&pdev->dev, NULL);
  1649. if (IS_ERR(dc->clk)) {
  1650. dev_err(&pdev->dev, "failed to get clock\n");
  1651. return PTR_ERR(dc->clk);
  1652. }
  1653. dc->rst = devm_reset_control_get(&pdev->dev, "dc");
  1654. if (IS_ERR(dc->rst)) {
  1655. dev_err(&pdev->dev, "failed to get reset\n");
  1656. return PTR_ERR(dc->rst);
  1657. }
  1658. if (!dc->soc->broken_reset)
  1659. reset_control_assert(dc->rst);
  1660. if (dc->soc->has_powergate) {
  1661. if (dc->pipe == 0)
  1662. dc->powergate = TEGRA_POWERGATE_DIS;
  1663. else
  1664. dc->powergate = TEGRA_POWERGATE_DISB;
  1665. tegra_powergate_power_off(dc->powergate);
  1666. }
  1667. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1668. dc->regs = devm_ioremap_resource(&pdev->dev, regs);
  1669. if (IS_ERR(dc->regs))
  1670. return PTR_ERR(dc->regs);
  1671. dc->irq = platform_get_irq(pdev, 0);
  1672. if (dc->irq < 0) {
  1673. dev_err(&pdev->dev, "failed to get IRQ\n");
  1674. return -ENXIO;
  1675. }
  1676. err = tegra_dc_rgb_probe(dc);
  1677. if (err < 0 && err != -ENODEV) {
  1678. dev_err(&pdev->dev, "failed to probe RGB output: %d\n", err);
  1679. return err;
  1680. }
  1681. platform_set_drvdata(pdev, dc);
  1682. pm_runtime_enable(&pdev->dev);
  1683. INIT_LIST_HEAD(&dc->client.list);
  1684. dc->client.ops = &dc_client_ops;
  1685. dc->client.dev = &pdev->dev;
  1686. err = host1x_client_register(&dc->client);
  1687. if (err < 0) {
  1688. dev_err(&pdev->dev, "failed to register host1x client: %d\n",
  1689. err);
  1690. return err;
  1691. }
  1692. return 0;
  1693. }
  1694. static int tegra_dc_remove(struct platform_device *pdev)
  1695. {
  1696. struct tegra_dc *dc = platform_get_drvdata(pdev);
  1697. int err;
  1698. err = host1x_client_unregister(&dc->client);
  1699. if (err < 0) {
  1700. dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
  1701. err);
  1702. return err;
  1703. }
  1704. err = tegra_dc_rgb_remove(dc);
  1705. if (err < 0) {
  1706. dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
  1707. return err;
  1708. }
  1709. pm_runtime_disable(&pdev->dev);
  1710. return 0;
  1711. }
  1712. #ifdef CONFIG_PM
  1713. static int tegra_dc_suspend(struct device *dev)
  1714. {
  1715. struct tegra_dc *dc = dev_get_drvdata(dev);
  1716. int err;
  1717. if (!dc->soc->broken_reset) {
  1718. err = reset_control_assert(dc->rst);
  1719. if (err < 0) {
  1720. dev_err(dev, "failed to assert reset: %d\n", err);
  1721. return err;
  1722. }
  1723. }
  1724. if (dc->soc->has_powergate)
  1725. tegra_powergate_power_off(dc->powergate);
  1726. clk_disable_unprepare(dc->clk);
  1727. return 0;
  1728. }
  1729. static int tegra_dc_resume(struct device *dev)
  1730. {
  1731. struct tegra_dc *dc = dev_get_drvdata(dev);
  1732. int err;
  1733. if (dc->soc->has_powergate) {
  1734. err = tegra_powergate_sequence_power_up(dc->powergate, dc->clk,
  1735. dc->rst);
  1736. if (err < 0) {
  1737. dev_err(dev, "failed to power partition: %d\n", err);
  1738. return err;
  1739. }
  1740. } else {
  1741. err = clk_prepare_enable(dc->clk);
  1742. if (err < 0) {
  1743. dev_err(dev, "failed to enable clock: %d\n", err);
  1744. return err;
  1745. }
  1746. if (!dc->soc->broken_reset) {
  1747. err = reset_control_deassert(dc->rst);
  1748. if (err < 0) {
  1749. dev_err(dev,
  1750. "failed to deassert reset: %d\n", err);
  1751. return err;
  1752. }
  1753. }
  1754. }
  1755. return 0;
  1756. }
  1757. #endif
  1758. static const struct dev_pm_ops tegra_dc_pm_ops = {
  1759. SET_RUNTIME_PM_OPS(tegra_dc_suspend, tegra_dc_resume, NULL)
  1760. };
  1761. struct platform_driver tegra_dc_driver = {
  1762. .driver = {
  1763. .name = "tegra-dc",
  1764. .of_match_table = tegra_dc_of_match,
  1765. .pm = &tegra_dc_pm_ops,
  1766. },
  1767. .probe = tegra_dc_probe,
  1768. .remove = tegra_dc_remove,
  1769. };