dc.c 50 KB

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