dc.c 32 KB

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