dc.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539
  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/reset.h>
  12. #include "dc.h"
  13. #include "drm.h"
  14. #include "gem.h"
  15. struct tegra_dc_soc_info {
  16. bool supports_interlacing;
  17. bool supports_cursor;
  18. bool supports_block_linear;
  19. unsigned int pitch_align;
  20. };
  21. struct tegra_plane {
  22. struct drm_plane base;
  23. unsigned int index;
  24. };
  25. static inline struct tegra_plane *to_tegra_plane(struct drm_plane *plane)
  26. {
  27. return container_of(plane, struct tegra_plane, base);
  28. }
  29. static unsigned int tegra_dc_format(uint32_t format, uint32_t *swap)
  30. {
  31. /* assume no swapping of fetched data */
  32. if (swap)
  33. *swap = BYTE_SWAP_NOSWAP;
  34. switch (format) {
  35. case DRM_FORMAT_XBGR8888:
  36. return WIN_COLOR_DEPTH_R8G8B8A8;
  37. case DRM_FORMAT_XRGB8888:
  38. return WIN_COLOR_DEPTH_B8G8R8A8;
  39. case DRM_FORMAT_RGB565:
  40. return WIN_COLOR_DEPTH_B5G6R5;
  41. case DRM_FORMAT_UYVY:
  42. return WIN_COLOR_DEPTH_YCbCr422;
  43. case DRM_FORMAT_YUYV:
  44. if (swap)
  45. *swap = BYTE_SWAP_SWAP2;
  46. return WIN_COLOR_DEPTH_YCbCr422;
  47. case DRM_FORMAT_YUV420:
  48. return WIN_COLOR_DEPTH_YCbCr420P;
  49. case DRM_FORMAT_YUV422:
  50. return WIN_COLOR_DEPTH_YCbCr422P;
  51. default:
  52. break;
  53. }
  54. WARN(1, "unsupported pixel format %u, using default\n", format);
  55. return WIN_COLOR_DEPTH_B8G8R8A8;
  56. }
  57. static bool tegra_dc_format_is_yuv(unsigned int format, bool *planar)
  58. {
  59. switch (format) {
  60. case WIN_COLOR_DEPTH_YCbCr422:
  61. case WIN_COLOR_DEPTH_YUV422:
  62. if (planar)
  63. *planar = false;
  64. return true;
  65. case WIN_COLOR_DEPTH_YCbCr420P:
  66. case WIN_COLOR_DEPTH_YUV420P:
  67. case WIN_COLOR_DEPTH_YCbCr422P:
  68. case WIN_COLOR_DEPTH_YUV422P:
  69. case WIN_COLOR_DEPTH_YCbCr422R:
  70. case WIN_COLOR_DEPTH_YUV422R:
  71. case WIN_COLOR_DEPTH_YCbCr422RA:
  72. case WIN_COLOR_DEPTH_YUV422RA:
  73. if (planar)
  74. *planar = true;
  75. return true;
  76. }
  77. return false;
  78. }
  79. static inline u32 compute_dda_inc(unsigned int in, unsigned int out, bool v,
  80. unsigned int bpp)
  81. {
  82. fixed20_12 outf = dfixed_init(out);
  83. fixed20_12 inf = dfixed_init(in);
  84. u32 dda_inc;
  85. int max;
  86. if (v)
  87. max = 15;
  88. else {
  89. switch (bpp) {
  90. case 2:
  91. max = 8;
  92. break;
  93. default:
  94. WARN_ON_ONCE(1);
  95. /* fallthrough */
  96. case 4:
  97. max = 4;
  98. break;
  99. }
  100. }
  101. outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
  102. inf.full -= dfixed_const(1);
  103. dda_inc = dfixed_div(inf, outf);
  104. dda_inc = min_t(u32, dda_inc, dfixed_const(max));
  105. return dda_inc;
  106. }
  107. static inline u32 compute_initial_dda(unsigned int in)
  108. {
  109. fixed20_12 inf = dfixed_init(in);
  110. return dfixed_frac(inf);
  111. }
  112. static int tegra_dc_setup_window(struct tegra_dc *dc, unsigned int index,
  113. const struct tegra_dc_window *window)
  114. {
  115. unsigned h_offset, v_offset, h_size, v_size, h_dda, v_dda, bpp;
  116. unsigned long value;
  117. bool yuv, planar;
  118. /*
  119. * For YUV planar modes, the number of bytes per pixel takes into
  120. * account only the luma component and therefore is 1.
  121. */
  122. yuv = tegra_dc_format_is_yuv(window->format, &planar);
  123. if (!yuv)
  124. bpp = window->bits_per_pixel / 8;
  125. else
  126. bpp = planar ? 1 : 2;
  127. value = WINDOW_A_SELECT << index;
  128. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
  129. tegra_dc_writel(dc, window->format, DC_WIN_COLOR_DEPTH);
  130. tegra_dc_writel(dc, window->swap, DC_WIN_BYTE_SWAP);
  131. value = V_POSITION(window->dst.y) | H_POSITION(window->dst.x);
  132. tegra_dc_writel(dc, value, DC_WIN_POSITION);
  133. value = V_SIZE(window->dst.h) | H_SIZE(window->dst.w);
  134. tegra_dc_writel(dc, value, DC_WIN_SIZE);
  135. h_offset = window->src.x * bpp;
  136. v_offset = window->src.y;
  137. h_size = window->src.w * bpp;
  138. v_size = window->src.h;
  139. value = V_PRESCALED_SIZE(v_size) | H_PRESCALED_SIZE(h_size);
  140. tegra_dc_writel(dc, value, DC_WIN_PRESCALED_SIZE);
  141. /*
  142. * For DDA computations the number of bytes per pixel for YUV planar
  143. * modes needs to take into account all Y, U and V components.
  144. */
  145. if (yuv && planar)
  146. bpp = 2;
  147. h_dda = compute_dda_inc(window->src.w, window->dst.w, false, bpp);
  148. v_dda = compute_dda_inc(window->src.h, window->dst.h, true, bpp);
  149. value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
  150. tegra_dc_writel(dc, value, DC_WIN_DDA_INC);
  151. h_dda = compute_initial_dda(window->src.x);
  152. v_dda = compute_initial_dda(window->src.y);
  153. tegra_dc_writel(dc, h_dda, DC_WIN_H_INITIAL_DDA);
  154. tegra_dc_writel(dc, v_dda, DC_WIN_V_INITIAL_DDA);
  155. tegra_dc_writel(dc, 0, DC_WIN_UV_BUF_STRIDE);
  156. tegra_dc_writel(dc, 0, DC_WIN_BUF_STRIDE);
  157. tegra_dc_writel(dc, window->base[0], DC_WINBUF_START_ADDR);
  158. if (yuv && planar) {
  159. tegra_dc_writel(dc, window->base[1], DC_WINBUF_START_ADDR_U);
  160. tegra_dc_writel(dc, window->base[2], DC_WINBUF_START_ADDR_V);
  161. value = window->stride[1] << 16 | window->stride[0];
  162. tegra_dc_writel(dc, value, DC_WIN_LINE_STRIDE);
  163. } else {
  164. tegra_dc_writel(dc, window->stride[0], DC_WIN_LINE_STRIDE);
  165. }
  166. if (window->bottom_up)
  167. v_offset += window->src.h - 1;
  168. tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
  169. tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
  170. if (dc->soc->supports_block_linear) {
  171. unsigned long height = window->tiling.value;
  172. switch (window->tiling.mode) {
  173. case TEGRA_BO_TILING_MODE_PITCH:
  174. value = DC_WINBUF_SURFACE_KIND_PITCH;
  175. break;
  176. case TEGRA_BO_TILING_MODE_TILED:
  177. value = DC_WINBUF_SURFACE_KIND_TILED;
  178. break;
  179. case TEGRA_BO_TILING_MODE_BLOCK:
  180. value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
  181. DC_WINBUF_SURFACE_KIND_BLOCK;
  182. break;
  183. }
  184. tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
  185. } else {
  186. switch (window->tiling.mode) {
  187. case TEGRA_BO_TILING_MODE_PITCH:
  188. value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
  189. DC_WIN_BUFFER_ADDR_MODE_LINEAR;
  190. break;
  191. case TEGRA_BO_TILING_MODE_TILED:
  192. value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
  193. DC_WIN_BUFFER_ADDR_MODE_TILE;
  194. break;
  195. case TEGRA_BO_TILING_MODE_BLOCK:
  196. DRM_ERROR("hardware doesn't support block linear mode\n");
  197. return -EINVAL;
  198. }
  199. tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
  200. }
  201. value = WIN_ENABLE;
  202. if (yuv) {
  203. /* setup default colorspace conversion coefficients */
  204. tegra_dc_writel(dc, 0x00f0, DC_WIN_CSC_YOF);
  205. tegra_dc_writel(dc, 0x012a, DC_WIN_CSC_KYRGB);
  206. tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KUR);
  207. tegra_dc_writel(dc, 0x0198, DC_WIN_CSC_KVR);
  208. tegra_dc_writel(dc, 0x039b, DC_WIN_CSC_KUG);
  209. tegra_dc_writel(dc, 0x032f, DC_WIN_CSC_KVG);
  210. tegra_dc_writel(dc, 0x0204, DC_WIN_CSC_KUB);
  211. tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KVB);
  212. value |= CSC_ENABLE;
  213. } else if (window->bits_per_pixel < 24) {
  214. value |= COLOR_EXPAND;
  215. }
  216. if (window->bottom_up)
  217. value |= V_DIRECTION;
  218. tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
  219. /*
  220. * Disable blending and assume Window A is the bottom-most window,
  221. * Window C is the top-most window and Window B is in the middle.
  222. */
  223. tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_NOKEY);
  224. tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_1WIN);
  225. switch (index) {
  226. case 0:
  227. tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_X);
  228. tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
  229. tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
  230. break;
  231. case 1:
  232. tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
  233. tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
  234. tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
  235. break;
  236. case 2:
  237. tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
  238. tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_Y);
  239. tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_3WIN_XY);
  240. break;
  241. }
  242. tegra_dc_writel(dc, WIN_A_UPDATE << index, DC_CMD_STATE_CONTROL);
  243. tegra_dc_writel(dc, WIN_A_ACT_REQ << index, DC_CMD_STATE_CONTROL);
  244. return 0;
  245. }
  246. static int tegra_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
  247. struct drm_framebuffer *fb, int crtc_x,
  248. int crtc_y, unsigned int crtc_w,
  249. unsigned int crtc_h, uint32_t src_x,
  250. uint32_t src_y, uint32_t src_w, uint32_t src_h)
  251. {
  252. struct tegra_plane *p = to_tegra_plane(plane);
  253. struct tegra_dc *dc = to_tegra_dc(crtc);
  254. struct tegra_dc_window window;
  255. unsigned int i;
  256. int err;
  257. memset(&window, 0, sizeof(window));
  258. window.src.x = src_x >> 16;
  259. window.src.y = src_y >> 16;
  260. window.src.w = src_w >> 16;
  261. window.src.h = src_h >> 16;
  262. window.dst.x = crtc_x;
  263. window.dst.y = crtc_y;
  264. window.dst.w = crtc_w;
  265. window.dst.h = crtc_h;
  266. window.format = tegra_dc_format(fb->pixel_format, &window.swap);
  267. window.bits_per_pixel = fb->bits_per_pixel;
  268. window.bottom_up = tegra_fb_is_bottom_up(fb);
  269. err = tegra_fb_get_tiling(fb, &window.tiling);
  270. if (err < 0)
  271. return err;
  272. for (i = 0; i < drm_format_num_planes(fb->pixel_format); i++) {
  273. struct tegra_bo *bo = tegra_fb_get_plane(fb, i);
  274. window.base[i] = bo->paddr + fb->offsets[i];
  275. /*
  276. * Tegra doesn't support different strides for U and V planes
  277. * so we display a warning if the user tries to display a
  278. * framebuffer with such a configuration.
  279. */
  280. if (i >= 2) {
  281. if (fb->pitches[i] != window.stride[1])
  282. DRM_ERROR("unsupported UV-plane configuration\n");
  283. } else {
  284. window.stride[i] = fb->pitches[i];
  285. }
  286. }
  287. return tegra_dc_setup_window(dc, p->index, &window);
  288. }
  289. static int tegra_plane_disable(struct drm_plane *plane)
  290. {
  291. struct tegra_dc *dc = to_tegra_dc(plane->crtc);
  292. struct tegra_plane *p = to_tegra_plane(plane);
  293. unsigned long value;
  294. if (!plane->crtc)
  295. return 0;
  296. value = WINDOW_A_SELECT << p->index;
  297. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
  298. value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
  299. value &= ~WIN_ENABLE;
  300. tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
  301. tegra_dc_writel(dc, WIN_A_UPDATE << p->index, DC_CMD_STATE_CONTROL);
  302. tegra_dc_writel(dc, WIN_A_ACT_REQ << p->index, DC_CMD_STATE_CONTROL);
  303. return 0;
  304. }
  305. static void tegra_plane_destroy(struct drm_plane *plane)
  306. {
  307. struct tegra_plane *p = to_tegra_plane(plane);
  308. tegra_plane_disable(plane);
  309. drm_plane_cleanup(plane);
  310. kfree(p);
  311. }
  312. static const struct drm_plane_funcs tegra_plane_funcs = {
  313. .update_plane = tegra_plane_update,
  314. .disable_plane = tegra_plane_disable,
  315. .destroy = tegra_plane_destroy,
  316. };
  317. static const uint32_t plane_formats[] = {
  318. DRM_FORMAT_XBGR8888,
  319. DRM_FORMAT_XRGB8888,
  320. DRM_FORMAT_RGB565,
  321. DRM_FORMAT_UYVY,
  322. DRM_FORMAT_YUYV,
  323. DRM_FORMAT_YUV420,
  324. DRM_FORMAT_YUV422,
  325. };
  326. static int tegra_dc_add_planes(struct drm_device *drm, struct tegra_dc *dc)
  327. {
  328. unsigned int i;
  329. int err = 0;
  330. for (i = 0; i < 2; i++) {
  331. struct tegra_plane *plane;
  332. plane = kzalloc(sizeof(*plane), GFP_KERNEL);
  333. if (!plane)
  334. return -ENOMEM;
  335. plane->index = 1 + i;
  336. err = drm_plane_init(drm, &plane->base, 1 << dc->pipe,
  337. &tegra_plane_funcs, plane_formats,
  338. ARRAY_SIZE(plane_formats), false);
  339. if (err < 0) {
  340. kfree(plane);
  341. return err;
  342. }
  343. }
  344. return 0;
  345. }
  346. static int tegra_dc_set_base(struct tegra_dc *dc, int x, int y,
  347. struct drm_framebuffer *fb)
  348. {
  349. struct tegra_bo *bo = tegra_fb_get_plane(fb, 0);
  350. unsigned int h_offset = 0, v_offset = 0;
  351. struct tegra_bo_tiling tiling;
  352. unsigned int format, swap;
  353. unsigned long value;
  354. int err;
  355. err = tegra_fb_get_tiling(fb, &tiling);
  356. if (err < 0)
  357. return err;
  358. tegra_dc_writel(dc, WINDOW_A_SELECT, DC_CMD_DISPLAY_WINDOW_HEADER);
  359. value = fb->offsets[0] + y * fb->pitches[0] +
  360. x * fb->bits_per_pixel / 8;
  361. tegra_dc_writel(dc, bo->paddr + value, DC_WINBUF_START_ADDR);
  362. tegra_dc_writel(dc, fb->pitches[0], DC_WIN_LINE_STRIDE);
  363. format = tegra_dc_format(fb->pixel_format, &swap);
  364. tegra_dc_writel(dc, format, DC_WIN_COLOR_DEPTH);
  365. tegra_dc_writel(dc, swap, DC_WIN_BYTE_SWAP);
  366. if (dc->soc->supports_block_linear) {
  367. unsigned long height = tiling.value;
  368. switch (tiling.mode) {
  369. case TEGRA_BO_TILING_MODE_PITCH:
  370. value = DC_WINBUF_SURFACE_KIND_PITCH;
  371. break;
  372. case TEGRA_BO_TILING_MODE_TILED:
  373. value = DC_WINBUF_SURFACE_KIND_TILED;
  374. break;
  375. case TEGRA_BO_TILING_MODE_BLOCK:
  376. value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
  377. DC_WINBUF_SURFACE_KIND_BLOCK;
  378. break;
  379. }
  380. tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
  381. } else {
  382. switch (tiling.mode) {
  383. case TEGRA_BO_TILING_MODE_PITCH:
  384. value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
  385. DC_WIN_BUFFER_ADDR_MODE_LINEAR;
  386. break;
  387. case TEGRA_BO_TILING_MODE_TILED:
  388. value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
  389. DC_WIN_BUFFER_ADDR_MODE_TILE;
  390. break;
  391. case TEGRA_BO_TILING_MODE_BLOCK:
  392. DRM_ERROR("hardware doesn't support block linear mode\n");
  393. return -EINVAL;
  394. }
  395. tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
  396. }
  397. /* make sure bottom-up buffers are properly displayed */
  398. if (tegra_fb_is_bottom_up(fb)) {
  399. value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
  400. value |= V_DIRECTION;
  401. tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
  402. v_offset += fb->height - 1;
  403. } else {
  404. value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
  405. value &= ~V_DIRECTION;
  406. tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
  407. }
  408. tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
  409. tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
  410. value = GENERAL_UPDATE | WIN_A_UPDATE;
  411. tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
  412. value = GENERAL_ACT_REQ | WIN_A_ACT_REQ;
  413. tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
  414. return 0;
  415. }
  416. void tegra_dc_enable_vblank(struct tegra_dc *dc)
  417. {
  418. unsigned long value, flags;
  419. spin_lock_irqsave(&dc->lock, flags);
  420. value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
  421. value |= VBLANK_INT;
  422. tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
  423. spin_unlock_irqrestore(&dc->lock, flags);
  424. }
  425. void tegra_dc_disable_vblank(struct tegra_dc *dc)
  426. {
  427. unsigned long value, flags;
  428. spin_lock_irqsave(&dc->lock, flags);
  429. value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
  430. value &= ~VBLANK_INT;
  431. tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
  432. spin_unlock_irqrestore(&dc->lock, flags);
  433. }
  434. static int tegra_dc_cursor_set2(struct drm_crtc *crtc, struct drm_file *file,
  435. uint32_t handle, uint32_t width,
  436. uint32_t height, int32_t hot_x, int32_t hot_y)
  437. {
  438. unsigned long value = CURSOR_CLIP_DISPLAY;
  439. struct tegra_dc *dc = to_tegra_dc(crtc);
  440. struct drm_gem_object *gem;
  441. struct tegra_bo *bo = NULL;
  442. if (!dc->soc->supports_cursor)
  443. return -ENXIO;
  444. if (width != height)
  445. return -EINVAL;
  446. switch (width) {
  447. case 32:
  448. value |= CURSOR_SIZE_32x32;
  449. break;
  450. case 64:
  451. value |= CURSOR_SIZE_64x64;
  452. break;
  453. case 128:
  454. value |= CURSOR_SIZE_128x128;
  455. case 256:
  456. value |= CURSOR_SIZE_256x256;
  457. break;
  458. default:
  459. return -EINVAL;
  460. }
  461. if (handle) {
  462. gem = drm_gem_object_lookup(crtc->dev, file, handle);
  463. if (!gem)
  464. return -ENOENT;
  465. bo = to_tegra_bo(gem);
  466. }
  467. if (bo) {
  468. unsigned long addr = (bo->paddr & 0xfffffc00) >> 10;
  469. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  470. unsigned long high = (bo->paddr & 0xfffffffc) >> 32;
  471. #endif
  472. tegra_dc_writel(dc, value | addr, DC_DISP_CURSOR_START_ADDR);
  473. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  474. tegra_dc_writel(dc, high, DC_DISP_CURSOR_START_ADDR_HI);
  475. #endif
  476. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  477. value |= CURSOR_ENABLE;
  478. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  479. value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
  480. value &= ~CURSOR_DST_BLEND_MASK;
  481. value &= ~CURSOR_SRC_BLEND_MASK;
  482. value |= CURSOR_MODE_NORMAL;
  483. value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
  484. value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
  485. value |= CURSOR_ALPHA;
  486. tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
  487. } else {
  488. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  489. value &= ~CURSOR_ENABLE;
  490. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  491. }
  492. tegra_dc_writel(dc, CURSOR_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
  493. tegra_dc_writel(dc, CURSOR_ACT_REQ, DC_CMD_STATE_CONTROL);
  494. tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
  495. tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
  496. return 0;
  497. }
  498. static int tegra_dc_cursor_move(struct drm_crtc *crtc, int x, int y)
  499. {
  500. struct tegra_dc *dc = to_tegra_dc(crtc);
  501. unsigned long value;
  502. if (!dc->soc->supports_cursor)
  503. return -ENXIO;
  504. value = ((y & 0x3fff) << 16) | (x & 0x3fff);
  505. tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
  506. tegra_dc_writel(dc, CURSOR_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
  507. tegra_dc_writel(dc, CURSOR_ACT_REQ, DC_CMD_STATE_CONTROL);
  508. /* XXX: only required on generations earlier than Tegra124? */
  509. tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
  510. tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
  511. return 0;
  512. }
  513. static void tegra_dc_finish_page_flip(struct tegra_dc *dc)
  514. {
  515. struct drm_device *drm = dc->base.dev;
  516. struct drm_crtc *crtc = &dc->base;
  517. unsigned long flags, base;
  518. struct tegra_bo *bo;
  519. if (!dc->event)
  520. return;
  521. bo = tegra_fb_get_plane(crtc->primary->fb, 0);
  522. /* check if new start address has been latched */
  523. tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
  524. base = tegra_dc_readl(dc, DC_WINBUF_START_ADDR);
  525. tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
  526. if (base == bo->paddr + crtc->primary->fb->offsets[0]) {
  527. spin_lock_irqsave(&drm->event_lock, flags);
  528. drm_send_vblank_event(drm, dc->pipe, dc->event);
  529. drm_vblank_put(drm, dc->pipe);
  530. dc->event = NULL;
  531. spin_unlock_irqrestore(&drm->event_lock, flags);
  532. }
  533. }
  534. void tegra_dc_cancel_page_flip(struct drm_crtc *crtc, struct drm_file *file)
  535. {
  536. struct tegra_dc *dc = to_tegra_dc(crtc);
  537. struct drm_device *drm = crtc->dev;
  538. unsigned long flags;
  539. spin_lock_irqsave(&drm->event_lock, flags);
  540. if (dc->event && dc->event->base.file_priv == file) {
  541. dc->event->base.destroy(&dc->event->base);
  542. drm_vblank_put(drm, dc->pipe);
  543. dc->event = NULL;
  544. }
  545. spin_unlock_irqrestore(&drm->event_lock, flags);
  546. }
  547. static int tegra_dc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  548. struct drm_pending_vblank_event *event, uint32_t page_flip_flags)
  549. {
  550. struct tegra_dc *dc = to_tegra_dc(crtc);
  551. struct drm_device *drm = crtc->dev;
  552. if (dc->event)
  553. return -EBUSY;
  554. if (event) {
  555. event->pipe = dc->pipe;
  556. dc->event = event;
  557. drm_vblank_get(drm, dc->pipe);
  558. }
  559. tegra_dc_set_base(dc, 0, 0, fb);
  560. crtc->primary->fb = fb;
  561. return 0;
  562. }
  563. static void drm_crtc_clear(struct drm_crtc *crtc)
  564. {
  565. memset(crtc, 0, sizeof(*crtc));
  566. }
  567. static void tegra_dc_destroy(struct drm_crtc *crtc)
  568. {
  569. drm_crtc_cleanup(crtc);
  570. drm_crtc_clear(crtc);
  571. }
  572. static const struct drm_crtc_funcs tegra_crtc_funcs = {
  573. .cursor_set2 = tegra_dc_cursor_set2,
  574. .cursor_move = tegra_dc_cursor_move,
  575. .page_flip = tegra_dc_page_flip,
  576. .set_config = drm_crtc_helper_set_config,
  577. .destroy = tegra_dc_destroy,
  578. };
  579. static void tegra_crtc_disable(struct drm_crtc *crtc)
  580. {
  581. struct tegra_dc *dc = to_tegra_dc(crtc);
  582. struct drm_device *drm = crtc->dev;
  583. struct drm_plane *plane;
  584. drm_for_each_legacy_plane(plane, &drm->mode_config.plane_list) {
  585. if (plane->crtc == crtc) {
  586. tegra_plane_disable(plane);
  587. plane->crtc = NULL;
  588. if (plane->fb) {
  589. drm_framebuffer_unreference(plane->fb);
  590. plane->fb = NULL;
  591. }
  592. }
  593. }
  594. drm_vblank_off(drm, dc->pipe);
  595. }
  596. static bool tegra_crtc_mode_fixup(struct drm_crtc *crtc,
  597. const struct drm_display_mode *mode,
  598. struct drm_display_mode *adjusted)
  599. {
  600. return true;
  601. }
  602. static int tegra_dc_set_timings(struct tegra_dc *dc,
  603. struct drm_display_mode *mode)
  604. {
  605. unsigned int h_ref_to_sync = 1;
  606. unsigned int v_ref_to_sync = 1;
  607. unsigned long value;
  608. tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
  609. value = (v_ref_to_sync << 16) | h_ref_to_sync;
  610. tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
  611. value = ((mode->vsync_end - mode->vsync_start) << 16) |
  612. ((mode->hsync_end - mode->hsync_start) << 0);
  613. tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
  614. value = ((mode->vtotal - mode->vsync_end) << 16) |
  615. ((mode->htotal - mode->hsync_end) << 0);
  616. tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
  617. value = ((mode->vsync_start - mode->vdisplay) << 16) |
  618. ((mode->hsync_start - mode->hdisplay) << 0);
  619. tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
  620. value = (mode->vdisplay << 16) | mode->hdisplay;
  621. tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
  622. return 0;
  623. }
  624. static int tegra_crtc_setup_clk(struct drm_crtc *crtc,
  625. struct drm_display_mode *mode)
  626. {
  627. unsigned long pclk = mode->clock * 1000;
  628. struct tegra_dc *dc = to_tegra_dc(crtc);
  629. struct tegra_output *output = NULL;
  630. struct drm_encoder *encoder;
  631. unsigned int div;
  632. u32 value;
  633. long err;
  634. list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list, head)
  635. if (encoder->crtc == crtc) {
  636. output = encoder_to_output(encoder);
  637. break;
  638. }
  639. if (!output)
  640. return -ENODEV;
  641. /*
  642. * This assumes that the parent clock is pll_d_out0 or pll_d2_out
  643. * respectively, each of which divides the base pll_d by 2.
  644. */
  645. err = tegra_output_setup_clock(output, dc->clk, pclk, &div);
  646. if (err < 0) {
  647. dev_err(dc->dev, "failed to setup clock: %ld\n", err);
  648. return err;
  649. }
  650. DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk), div);
  651. value = SHIFT_CLK_DIVIDER(div) | PIXEL_CLK_DIVIDER_PCD1;
  652. tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
  653. return 0;
  654. }
  655. static int tegra_crtc_mode_set(struct drm_crtc *crtc,
  656. struct drm_display_mode *mode,
  657. struct drm_display_mode *adjusted,
  658. int x, int y, struct drm_framebuffer *old_fb)
  659. {
  660. struct tegra_bo *bo = tegra_fb_get_plane(crtc->primary->fb, 0);
  661. struct tegra_dc *dc = to_tegra_dc(crtc);
  662. struct tegra_dc_window window;
  663. u32 value;
  664. int err;
  665. drm_vblank_pre_modeset(crtc->dev, dc->pipe);
  666. err = tegra_crtc_setup_clk(crtc, mode);
  667. if (err) {
  668. dev_err(dc->dev, "failed to setup clock for CRTC: %d\n", err);
  669. return err;
  670. }
  671. /* program display mode */
  672. tegra_dc_set_timings(dc, mode);
  673. /* interlacing isn't supported yet, so disable it */
  674. if (dc->soc->supports_interlacing) {
  675. value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
  676. value &= ~INTERLACE_ENABLE;
  677. tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
  678. }
  679. /* setup window parameters */
  680. memset(&window, 0, sizeof(window));
  681. window.src.x = 0;
  682. window.src.y = 0;
  683. window.src.w = mode->hdisplay;
  684. window.src.h = mode->vdisplay;
  685. window.dst.x = 0;
  686. window.dst.y = 0;
  687. window.dst.w = mode->hdisplay;
  688. window.dst.h = mode->vdisplay;
  689. window.format = tegra_dc_format(crtc->primary->fb->pixel_format,
  690. &window.swap);
  691. window.bits_per_pixel = crtc->primary->fb->bits_per_pixel;
  692. window.stride[0] = crtc->primary->fb->pitches[0];
  693. window.base[0] = bo->paddr;
  694. err = tegra_dc_setup_window(dc, 0, &window);
  695. if (err < 0)
  696. dev_err(dc->dev, "failed to enable root plane\n");
  697. return 0;
  698. }
  699. static int tegra_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  700. struct drm_framebuffer *old_fb)
  701. {
  702. struct tegra_dc *dc = to_tegra_dc(crtc);
  703. return tegra_dc_set_base(dc, x, y, crtc->primary->fb);
  704. }
  705. static void tegra_crtc_prepare(struct drm_crtc *crtc)
  706. {
  707. struct tegra_dc *dc = to_tegra_dc(crtc);
  708. unsigned int syncpt;
  709. unsigned long value;
  710. /* hardware initialization */
  711. reset_control_deassert(dc->rst);
  712. usleep_range(10000, 20000);
  713. if (dc->pipe)
  714. syncpt = SYNCPT_VBLANK1;
  715. else
  716. syncpt = SYNCPT_VBLANK0;
  717. /* initialize display controller */
  718. tegra_dc_writel(dc, 0x00000100, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
  719. tegra_dc_writel(dc, 0x100 | syncpt, DC_CMD_CONT_SYNCPT_VSYNC);
  720. value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT | WIN_A_OF_INT;
  721. tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
  722. value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
  723. WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
  724. tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
  725. /* initialize timer */
  726. value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
  727. WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
  728. tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
  729. value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
  730. WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
  731. tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
  732. value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT;
  733. tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
  734. value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT;
  735. tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
  736. }
  737. static void tegra_crtc_commit(struct drm_crtc *crtc)
  738. {
  739. struct tegra_dc *dc = to_tegra_dc(crtc);
  740. unsigned long value;
  741. value = GENERAL_UPDATE | WIN_A_UPDATE;
  742. tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
  743. value = GENERAL_ACT_REQ | WIN_A_ACT_REQ;
  744. tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
  745. drm_vblank_post_modeset(crtc->dev, dc->pipe);
  746. }
  747. static void tegra_crtc_load_lut(struct drm_crtc *crtc)
  748. {
  749. }
  750. static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
  751. .disable = tegra_crtc_disable,
  752. .mode_fixup = tegra_crtc_mode_fixup,
  753. .mode_set = tegra_crtc_mode_set,
  754. .mode_set_base = tegra_crtc_mode_set_base,
  755. .prepare = tegra_crtc_prepare,
  756. .commit = tegra_crtc_commit,
  757. .load_lut = tegra_crtc_load_lut,
  758. };
  759. static irqreturn_t tegra_dc_irq(int irq, void *data)
  760. {
  761. struct tegra_dc *dc = data;
  762. unsigned long status;
  763. status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
  764. tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
  765. if (status & FRAME_END_INT) {
  766. /*
  767. dev_dbg(dc->dev, "%s(): frame end\n", __func__);
  768. */
  769. }
  770. if (status & VBLANK_INT) {
  771. /*
  772. dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
  773. */
  774. drm_handle_vblank(dc->base.dev, dc->pipe);
  775. tegra_dc_finish_page_flip(dc);
  776. }
  777. if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
  778. /*
  779. dev_dbg(dc->dev, "%s(): underflow\n", __func__);
  780. */
  781. }
  782. return IRQ_HANDLED;
  783. }
  784. static int tegra_dc_show_regs(struct seq_file *s, void *data)
  785. {
  786. struct drm_info_node *node = s->private;
  787. struct tegra_dc *dc = node->info_ent->data;
  788. #define DUMP_REG(name) \
  789. seq_printf(s, "%-40s %#05x %08lx\n", #name, name, \
  790. tegra_dc_readl(dc, name))
  791. DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT);
  792. DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
  793. DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_ERROR);
  794. DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT);
  795. DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL);
  796. DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_ERROR);
  797. DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT);
  798. DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL);
  799. DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_ERROR);
  800. DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT);
  801. DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL);
  802. DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_ERROR);
  803. DUMP_REG(DC_CMD_CONT_SYNCPT_VSYNC);
  804. DUMP_REG(DC_CMD_DISPLAY_COMMAND_OPTION0);
  805. DUMP_REG(DC_CMD_DISPLAY_COMMAND);
  806. DUMP_REG(DC_CMD_SIGNAL_RAISE);
  807. DUMP_REG(DC_CMD_DISPLAY_POWER_CONTROL);
  808. DUMP_REG(DC_CMD_INT_STATUS);
  809. DUMP_REG(DC_CMD_INT_MASK);
  810. DUMP_REG(DC_CMD_INT_ENABLE);
  811. DUMP_REG(DC_CMD_INT_TYPE);
  812. DUMP_REG(DC_CMD_INT_POLARITY);
  813. DUMP_REG(DC_CMD_SIGNAL_RAISE1);
  814. DUMP_REG(DC_CMD_SIGNAL_RAISE2);
  815. DUMP_REG(DC_CMD_SIGNAL_RAISE3);
  816. DUMP_REG(DC_CMD_STATE_ACCESS);
  817. DUMP_REG(DC_CMD_STATE_CONTROL);
  818. DUMP_REG(DC_CMD_DISPLAY_WINDOW_HEADER);
  819. DUMP_REG(DC_CMD_REG_ACT_CONTROL);
  820. DUMP_REG(DC_COM_CRC_CONTROL);
  821. DUMP_REG(DC_COM_CRC_CHECKSUM);
  822. DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(0));
  823. DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(1));
  824. DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(2));
  825. DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(3));
  826. DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(0));
  827. DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(1));
  828. DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(2));
  829. DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(3));
  830. DUMP_REG(DC_COM_PIN_OUTPUT_DATA(0));
  831. DUMP_REG(DC_COM_PIN_OUTPUT_DATA(1));
  832. DUMP_REG(DC_COM_PIN_OUTPUT_DATA(2));
  833. DUMP_REG(DC_COM_PIN_OUTPUT_DATA(3));
  834. DUMP_REG(DC_COM_PIN_INPUT_ENABLE(0));
  835. DUMP_REG(DC_COM_PIN_INPUT_ENABLE(1));
  836. DUMP_REG(DC_COM_PIN_INPUT_ENABLE(2));
  837. DUMP_REG(DC_COM_PIN_INPUT_ENABLE(3));
  838. DUMP_REG(DC_COM_PIN_INPUT_DATA(0));
  839. DUMP_REG(DC_COM_PIN_INPUT_DATA(1));
  840. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(0));
  841. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(1));
  842. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(2));
  843. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(3));
  844. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(4));
  845. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(5));
  846. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(6));
  847. DUMP_REG(DC_COM_PIN_MISC_CONTROL);
  848. DUMP_REG(DC_COM_PIN_PM0_CONTROL);
  849. DUMP_REG(DC_COM_PIN_PM0_DUTY_CYCLE);
  850. DUMP_REG(DC_COM_PIN_PM1_CONTROL);
  851. DUMP_REG(DC_COM_PIN_PM1_DUTY_CYCLE);
  852. DUMP_REG(DC_COM_SPI_CONTROL);
  853. DUMP_REG(DC_COM_SPI_START_BYTE);
  854. DUMP_REG(DC_COM_HSPI_WRITE_DATA_AB);
  855. DUMP_REG(DC_COM_HSPI_WRITE_DATA_CD);
  856. DUMP_REG(DC_COM_HSPI_CS_DC);
  857. DUMP_REG(DC_COM_SCRATCH_REGISTER_A);
  858. DUMP_REG(DC_COM_SCRATCH_REGISTER_B);
  859. DUMP_REG(DC_COM_GPIO_CTRL);
  860. DUMP_REG(DC_COM_GPIO_DEBOUNCE_COUNTER);
  861. DUMP_REG(DC_COM_CRC_CHECKSUM_LATCHED);
  862. DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS0);
  863. DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS1);
  864. DUMP_REG(DC_DISP_DISP_WIN_OPTIONS);
  865. DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY);
  866. DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
  867. DUMP_REG(DC_DISP_DISP_TIMING_OPTIONS);
  868. DUMP_REG(DC_DISP_REF_TO_SYNC);
  869. DUMP_REG(DC_DISP_SYNC_WIDTH);
  870. DUMP_REG(DC_DISP_BACK_PORCH);
  871. DUMP_REG(DC_DISP_ACTIVE);
  872. DUMP_REG(DC_DISP_FRONT_PORCH);
  873. DUMP_REG(DC_DISP_H_PULSE0_CONTROL);
  874. DUMP_REG(DC_DISP_H_PULSE0_POSITION_A);
  875. DUMP_REG(DC_DISP_H_PULSE0_POSITION_B);
  876. DUMP_REG(DC_DISP_H_PULSE0_POSITION_C);
  877. DUMP_REG(DC_DISP_H_PULSE0_POSITION_D);
  878. DUMP_REG(DC_DISP_H_PULSE1_CONTROL);
  879. DUMP_REG(DC_DISP_H_PULSE1_POSITION_A);
  880. DUMP_REG(DC_DISP_H_PULSE1_POSITION_B);
  881. DUMP_REG(DC_DISP_H_PULSE1_POSITION_C);
  882. DUMP_REG(DC_DISP_H_PULSE1_POSITION_D);
  883. DUMP_REG(DC_DISP_H_PULSE2_CONTROL);
  884. DUMP_REG(DC_DISP_H_PULSE2_POSITION_A);
  885. DUMP_REG(DC_DISP_H_PULSE2_POSITION_B);
  886. DUMP_REG(DC_DISP_H_PULSE2_POSITION_C);
  887. DUMP_REG(DC_DISP_H_PULSE2_POSITION_D);
  888. DUMP_REG(DC_DISP_V_PULSE0_CONTROL);
  889. DUMP_REG(DC_DISP_V_PULSE0_POSITION_A);
  890. DUMP_REG(DC_DISP_V_PULSE0_POSITION_B);
  891. DUMP_REG(DC_DISP_V_PULSE0_POSITION_C);
  892. DUMP_REG(DC_DISP_V_PULSE1_CONTROL);
  893. DUMP_REG(DC_DISP_V_PULSE1_POSITION_A);
  894. DUMP_REG(DC_DISP_V_PULSE1_POSITION_B);
  895. DUMP_REG(DC_DISP_V_PULSE1_POSITION_C);
  896. DUMP_REG(DC_DISP_V_PULSE2_CONTROL);
  897. DUMP_REG(DC_DISP_V_PULSE2_POSITION_A);
  898. DUMP_REG(DC_DISP_V_PULSE3_CONTROL);
  899. DUMP_REG(DC_DISP_V_PULSE3_POSITION_A);
  900. DUMP_REG(DC_DISP_M0_CONTROL);
  901. DUMP_REG(DC_DISP_M1_CONTROL);
  902. DUMP_REG(DC_DISP_DI_CONTROL);
  903. DUMP_REG(DC_DISP_PP_CONTROL);
  904. DUMP_REG(DC_DISP_PP_SELECT_A);
  905. DUMP_REG(DC_DISP_PP_SELECT_B);
  906. DUMP_REG(DC_DISP_PP_SELECT_C);
  907. DUMP_REG(DC_DISP_PP_SELECT_D);
  908. DUMP_REG(DC_DISP_DISP_CLOCK_CONTROL);
  909. DUMP_REG(DC_DISP_DISP_INTERFACE_CONTROL);
  910. DUMP_REG(DC_DISP_DISP_COLOR_CONTROL);
  911. DUMP_REG(DC_DISP_SHIFT_CLOCK_OPTIONS);
  912. DUMP_REG(DC_DISP_DATA_ENABLE_OPTIONS);
  913. DUMP_REG(DC_DISP_SERIAL_INTERFACE_OPTIONS);
  914. DUMP_REG(DC_DISP_LCD_SPI_OPTIONS);
  915. DUMP_REG(DC_DISP_BORDER_COLOR);
  916. DUMP_REG(DC_DISP_COLOR_KEY0_LOWER);
  917. DUMP_REG(DC_DISP_COLOR_KEY0_UPPER);
  918. DUMP_REG(DC_DISP_COLOR_KEY1_LOWER);
  919. DUMP_REG(DC_DISP_COLOR_KEY1_UPPER);
  920. DUMP_REG(DC_DISP_CURSOR_FOREGROUND);
  921. DUMP_REG(DC_DISP_CURSOR_BACKGROUND);
  922. DUMP_REG(DC_DISP_CURSOR_START_ADDR);
  923. DUMP_REG(DC_DISP_CURSOR_START_ADDR_NS);
  924. DUMP_REG(DC_DISP_CURSOR_POSITION);
  925. DUMP_REG(DC_DISP_CURSOR_POSITION_NS);
  926. DUMP_REG(DC_DISP_INIT_SEQ_CONTROL);
  927. DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_A);
  928. DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_B);
  929. DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_C);
  930. DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_D);
  931. DUMP_REG(DC_DISP_DC_MCCIF_FIFOCTRL);
  932. DUMP_REG(DC_DISP_MCCIF_DISPLAY0A_HYST);
  933. DUMP_REG(DC_DISP_MCCIF_DISPLAY0B_HYST);
  934. DUMP_REG(DC_DISP_MCCIF_DISPLAY1A_HYST);
  935. DUMP_REG(DC_DISP_MCCIF_DISPLAY1B_HYST);
  936. DUMP_REG(DC_DISP_DAC_CRT_CTRL);
  937. DUMP_REG(DC_DISP_DISP_MISC_CONTROL);
  938. DUMP_REG(DC_DISP_SD_CONTROL);
  939. DUMP_REG(DC_DISP_SD_CSC_COEFF);
  940. DUMP_REG(DC_DISP_SD_LUT(0));
  941. DUMP_REG(DC_DISP_SD_LUT(1));
  942. DUMP_REG(DC_DISP_SD_LUT(2));
  943. DUMP_REG(DC_DISP_SD_LUT(3));
  944. DUMP_REG(DC_DISP_SD_LUT(4));
  945. DUMP_REG(DC_DISP_SD_LUT(5));
  946. DUMP_REG(DC_DISP_SD_LUT(6));
  947. DUMP_REG(DC_DISP_SD_LUT(7));
  948. DUMP_REG(DC_DISP_SD_LUT(8));
  949. DUMP_REG(DC_DISP_SD_FLICKER_CONTROL);
  950. DUMP_REG(DC_DISP_DC_PIXEL_COUNT);
  951. DUMP_REG(DC_DISP_SD_HISTOGRAM(0));
  952. DUMP_REG(DC_DISP_SD_HISTOGRAM(1));
  953. DUMP_REG(DC_DISP_SD_HISTOGRAM(2));
  954. DUMP_REG(DC_DISP_SD_HISTOGRAM(3));
  955. DUMP_REG(DC_DISP_SD_HISTOGRAM(4));
  956. DUMP_REG(DC_DISP_SD_HISTOGRAM(5));
  957. DUMP_REG(DC_DISP_SD_HISTOGRAM(6));
  958. DUMP_REG(DC_DISP_SD_HISTOGRAM(7));
  959. DUMP_REG(DC_DISP_SD_BL_TF(0));
  960. DUMP_REG(DC_DISP_SD_BL_TF(1));
  961. DUMP_REG(DC_DISP_SD_BL_TF(2));
  962. DUMP_REG(DC_DISP_SD_BL_TF(3));
  963. DUMP_REG(DC_DISP_SD_BL_CONTROL);
  964. DUMP_REG(DC_DISP_SD_HW_K_VALUES);
  965. DUMP_REG(DC_DISP_SD_MAN_K_VALUES);
  966. DUMP_REG(DC_DISP_CURSOR_START_ADDR_HI);
  967. DUMP_REG(DC_DISP_BLEND_CURSOR_CONTROL);
  968. DUMP_REG(DC_WIN_WIN_OPTIONS);
  969. DUMP_REG(DC_WIN_BYTE_SWAP);
  970. DUMP_REG(DC_WIN_BUFFER_CONTROL);
  971. DUMP_REG(DC_WIN_COLOR_DEPTH);
  972. DUMP_REG(DC_WIN_POSITION);
  973. DUMP_REG(DC_WIN_SIZE);
  974. DUMP_REG(DC_WIN_PRESCALED_SIZE);
  975. DUMP_REG(DC_WIN_H_INITIAL_DDA);
  976. DUMP_REG(DC_WIN_V_INITIAL_DDA);
  977. DUMP_REG(DC_WIN_DDA_INC);
  978. DUMP_REG(DC_WIN_LINE_STRIDE);
  979. DUMP_REG(DC_WIN_BUF_STRIDE);
  980. DUMP_REG(DC_WIN_UV_BUF_STRIDE);
  981. DUMP_REG(DC_WIN_BUFFER_ADDR_MODE);
  982. DUMP_REG(DC_WIN_DV_CONTROL);
  983. DUMP_REG(DC_WIN_BLEND_NOKEY);
  984. DUMP_REG(DC_WIN_BLEND_1WIN);
  985. DUMP_REG(DC_WIN_BLEND_2WIN_X);
  986. DUMP_REG(DC_WIN_BLEND_2WIN_Y);
  987. DUMP_REG(DC_WIN_BLEND_3WIN_XY);
  988. DUMP_REG(DC_WIN_HP_FETCH_CONTROL);
  989. DUMP_REG(DC_WINBUF_START_ADDR);
  990. DUMP_REG(DC_WINBUF_START_ADDR_NS);
  991. DUMP_REG(DC_WINBUF_START_ADDR_U);
  992. DUMP_REG(DC_WINBUF_START_ADDR_U_NS);
  993. DUMP_REG(DC_WINBUF_START_ADDR_V);
  994. DUMP_REG(DC_WINBUF_START_ADDR_V_NS);
  995. DUMP_REG(DC_WINBUF_ADDR_H_OFFSET);
  996. DUMP_REG(DC_WINBUF_ADDR_H_OFFSET_NS);
  997. DUMP_REG(DC_WINBUF_ADDR_V_OFFSET);
  998. DUMP_REG(DC_WINBUF_ADDR_V_OFFSET_NS);
  999. DUMP_REG(DC_WINBUF_UFLOW_STATUS);
  1000. DUMP_REG(DC_WINBUF_AD_UFLOW_STATUS);
  1001. DUMP_REG(DC_WINBUF_BD_UFLOW_STATUS);
  1002. DUMP_REG(DC_WINBUF_CD_UFLOW_STATUS);
  1003. #undef DUMP_REG
  1004. return 0;
  1005. }
  1006. static struct drm_info_list debugfs_files[] = {
  1007. { "regs", tegra_dc_show_regs, 0, NULL },
  1008. };
  1009. static int tegra_dc_debugfs_init(struct tegra_dc *dc, struct drm_minor *minor)
  1010. {
  1011. unsigned int i;
  1012. char *name;
  1013. int err;
  1014. name = kasprintf(GFP_KERNEL, "dc.%d", dc->pipe);
  1015. dc->debugfs = debugfs_create_dir(name, minor->debugfs_root);
  1016. kfree(name);
  1017. if (!dc->debugfs)
  1018. return -ENOMEM;
  1019. dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
  1020. GFP_KERNEL);
  1021. if (!dc->debugfs_files) {
  1022. err = -ENOMEM;
  1023. goto remove;
  1024. }
  1025. for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
  1026. dc->debugfs_files[i].data = dc;
  1027. err = drm_debugfs_create_files(dc->debugfs_files,
  1028. ARRAY_SIZE(debugfs_files),
  1029. dc->debugfs, minor);
  1030. if (err < 0)
  1031. goto free;
  1032. dc->minor = minor;
  1033. return 0;
  1034. free:
  1035. kfree(dc->debugfs_files);
  1036. dc->debugfs_files = NULL;
  1037. remove:
  1038. debugfs_remove(dc->debugfs);
  1039. dc->debugfs = NULL;
  1040. return err;
  1041. }
  1042. static int tegra_dc_debugfs_exit(struct tegra_dc *dc)
  1043. {
  1044. drm_debugfs_remove_files(dc->debugfs_files, ARRAY_SIZE(debugfs_files),
  1045. dc->minor);
  1046. dc->minor = NULL;
  1047. kfree(dc->debugfs_files);
  1048. dc->debugfs_files = NULL;
  1049. debugfs_remove(dc->debugfs);
  1050. dc->debugfs = NULL;
  1051. return 0;
  1052. }
  1053. static int tegra_dc_init(struct host1x_client *client)
  1054. {
  1055. struct drm_device *drm = dev_get_drvdata(client->parent);
  1056. struct tegra_dc *dc = host1x_client_to_dc(client);
  1057. struct tegra_drm *tegra = drm->dev_private;
  1058. int err;
  1059. drm_crtc_init(drm, &dc->base, &tegra_crtc_funcs);
  1060. drm_mode_crtc_set_gamma_size(&dc->base, 256);
  1061. drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
  1062. /*
  1063. * Keep track of the minimum pitch alignment across all display
  1064. * controllers.
  1065. */
  1066. if (dc->soc->pitch_align > tegra->pitch_align)
  1067. tegra->pitch_align = dc->soc->pitch_align;
  1068. err = tegra_dc_rgb_init(drm, dc);
  1069. if (err < 0 && err != -ENODEV) {
  1070. dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
  1071. return err;
  1072. }
  1073. err = tegra_dc_add_planes(drm, dc);
  1074. if (err < 0)
  1075. return err;
  1076. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  1077. err = tegra_dc_debugfs_init(dc, drm->primary);
  1078. if (err < 0)
  1079. dev_err(dc->dev, "debugfs setup failed: %d\n", err);
  1080. }
  1081. err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
  1082. dev_name(dc->dev), dc);
  1083. if (err < 0) {
  1084. dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
  1085. err);
  1086. return err;
  1087. }
  1088. return 0;
  1089. }
  1090. static int tegra_dc_exit(struct host1x_client *client)
  1091. {
  1092. struct tegra_dc *dc = host1x_client_to_dc(client);
  1093. int err;
  1094. devm_free_irq(dc->dev, dc->irq, dc);
  1095. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  1096. err = tegra_dc_debugfs_exit(dc);
  1097. if (err < 0)
  1098. dev_err(dc->dev, "debugfs cleanup failed: %d\n", err);
  1099. }
  1100. err = tegra_dc_rgb_exit(dc);
  1101. if (err) {
  1102. dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
  1103. return err;
  1104. }
  1105. return 0;
  1106. }
  1107. static const struct host1x_client_ops dc_client_ops = {
  1108. .init = tegra_dc_init,
  1109. .exit = tegra_dc_exit,
  1110. };
  1111. static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
  1112. .supports_interlacing = false,
  1113. .supports_cursor = false,
  1114. .supports_block_linear = false,
  1115. .pitch_align = 8,
  1116. };
  1117. static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
  1118. .supports_interlacing = false,
  1119. .supports_cursor = false,
  1120. .supports_block_linear = false,
  1121. .pitch_align = 8,
  1122. };
  1123. static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
  1124. .supports_interlacing = false,
  1125. .supports_cursor = false,
  1126. .supports_block_linear = false,
  1127. .pitch_align = 64,
  1128. };
  1129. static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
  1130. .supports_interlacing = true,
  1131. .supports_cursor = true,
  1132. .supports_block_linear = true,
  1133. .pitch_align = 64,
  1134. };
  1135. static const struct of_device_id tegra_dc_of_match[] = {
  1136. {
  1137. .compatible = "nvidia,tegra124-dc",
  1138. .data = &tegra124_dc_soc_info,
  1139. }, {
  1140. .compatible = "nvidia,tegra30-dc",
  1141. .data = &tegra30_dc_soc_info,
  1142. }, {
  1143. .compatible = "nvidia,tegra20-dc",
  1144. .data = &tegra20_dc_soc_info,
  1145. }, {
  1146. /* sentinel */
  1147. }
  1148. };
  1149. MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
  1150. static int tegra_dc_parse_dt(struct tegra_dc *dc)
  1151. {
  1152. struct device_node *np;
  1153. u32 value = 0;
  1154. int err;
  1155. err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
  1156. if (err < 0) {
  1157. dev_err(dc->dev, "missing \"nvidia,head\" property\n");
  1158. /*
  1159. * If the nvidia,head property isn't present, try to find the
  1160. * correct head number by looking up the position of this
  1161. * display controller's node within the device tree. Assuming
  1162. * that the nodes are ordered properly in the DTS file and
  1163. * that the translation into a flattened device tree blob
  1164. * preserves that ordering this will actually yield the right
  1165. * head number.
  1166. *
  1167. * If those assumptions don't hold, this will still work for
  1168. * cases where only a single display controller is used.
  1169. */
  1170. for_each_matching_node(np, tegra_dc_of_match) {
  1171. if (np == dc->dev->of_node)
  1172. break;
  1173. value++;
  1174. }
  1175. }
  1176. dc->pipe = value;
  1177. return 0;
  1178. }
  1179. static int tegra_dc_probe(struct platform_device *pdev)
  1180. {
  1181. const struct of_device_id *id;
  1182. struct resource *regs;
  1183. struct tegra_dc *dc;
  1184. int err;
  1185. dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
  1186. if (!dc)
  1187. return -ENOMEM;
  1188. id = of_match_node(tegra_dc_of_match, pdev->dev.of_node);
  1189. if (!id)
  1190. return -ENODEV;
  1191. spin_lock_init(&dc->lock);
  1192. INIT_LIST_HEAD(&dc->list);
  1193. dc->dev = &pdev->dev;
  1194. dc->soc = id->data;
  1195. err = tegra_dc_parse_dt(dc);
  1196. if (err < 0)
  1197. return err;
  1198. dc->clk = devm_clk_get(&pdev->dev, NULL);
  1199. if (IS_ERR(dc->clk)) {
  1200. dev_err(&pdev->dev, "failed to get clock\n");
  1201. return PTR_ERR(dc->clk);
  1202. }
  1203. dc->rst = devm_reset_control_get(&pdev->dev, "dc");
  1204. if (IS_ERR(dc->rst)) {
  1205. dev_err(&pdev->dev, "failed to get reset\n");
  1206. return PTR_ERR(dc->rst);
  1207. }
  1208. err = clk_prepare_enable(dc->clk);
  1209. if (err < 0)
  1210. return err;
  1211. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1212. dc->regs = devm_ioremap_resource(&pdev->dev, regs);
  1213. if (IS_ERR(dc->regs))
  1214. return PTR_ERR(dc->regs);
  1215. dc->irq = platform_get_irq(pdev, 0);
  1216. if (dc->irq < 0) {
  1217. dev_err(&pdev->dev, "failed to get IRQ\n");
  1218. return -ENXIO;
  1219. }
  1220. INIT_LIST_HEAD(&dc->client.list);
  1221. dc->client.ops = &dc_client_ops;
  1222. dc->client.dev = &pdev->dev;
  1223. err = tegra_dc_rgb_probe(dc);
  1224. if (err < 0 && err != -ENODEV) {
  1225. dev_err(&pdev->dev, "failed to probe RGB output: %d\n", err);
  1226. return err;
  1227. }
  1228. err = host1x_client_register(&dc->client);
  1229. if (err < 0) {
  1230. dev_err(&pdev->dev, "failed to register host1x client: %d\n",
  1231. err);
  1232. return err;
  1233. }
  1234. platform_set_drvdata(pdev, dc);
  1235. return 0;
  1236. }
  1237. static int tegra_dc_remove(struct platform_device *pdev)
  1238. {
  1239. struct tegra_dc *dc = platform_get_drvdata(pdev);
  1240. int err;
  1241. err = host1x_client_unregister(&dc->client);
  1242. if (err < 0) {
  1243. dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
  1244. err);
  1245. return err;
  1246. }
  1247. err = tegra_dc_rgb_remove(dc);
  1248. if (err < 0) {
  1249. dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
  1250. return err;
  1251. }
  1252. reset_control_assert(dc->rst);
  1253. clk_disable_unprepare(dc->clk);
  1254. return 0;
  1255. }
  1256. struct platform_driver tegra_dc_driver = {
  1257. .driver = {
  1258. .name = "tegra-dc",
  1259. .owner = THIS_MODULE,
  1260. .of_match_table = tegra_dc_of_match,
  1261. },
  1262. .probe = tegra_dc_probe,
  1263. .remove = tegra_dc_remove,
  1264. };