dc.c 37 KB

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