dc.c 33 KB

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