dc.c 32 KB

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