dc.c 35 KB

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