dc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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 <mach/clk.h>
  15. #include "drm.h"
  16. #include "dc.h"
  17. struct tegra_dc_window {
  18. fixed20_12 x;
  19. fixed20_12 y;
  20. fixed20_12 w;
  21. fixed20_12 h;
  22. unsigned int outx;
  23. unsigned int outy;
  24. unsigned int outw;
  25. unsigned int outh;
  26. unsigned int stride;
  27. unsigned int fmt;
  28. };
  29. static const struct drm_crtc_funcs tegra_crtc_funcs = {
  30. .set_config = drm_crtc_helper_set_config,
  31. .destroy = drm_crtc_cleanup,
  32. };
  33. static void tegra_crtc_dpms(struct drm_crtc *crtc, int mode)
  34. {
  35. }
  36. static bool tegra_crtc_mode_fixup(struct drm_crtc *crtc,
  37. const struct drm_display_mode *mode,
  38. struct drm_display_mode *adjusted)
  39. {
  40. return true;
  41. }
  42. static inline u32 compute_dda_inc(fixed20_12 inf, unsigned int out, bool v,
  43. unsigned int bpp)
  44. {
  45. fixed20_12 outf = dfixed_init(out);
  46. u32 dda_inc;
  47. int max;
  48. if (v)
  49. max = 15;
  50. else {
  51. switch (bpp) {
  52. case 2:
  53. max = 8;
  54. break;
  55. default:
  56. WARN_ON_ONCE(1);
  57. /* fallthrough */
  58. case 4:
  59. max = 4;
  60. break;
  61. }
  62. }
  63. outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
  64. inf.full -= dfixed_const(1);
  65. dda_inc = dfixed_div(inf, outf);
  66. dda_inc = min_t(u32, dda_inc, dfixed_const(max));
  67. return dda_inc;
  68. }
  69. static inline u32 compute_initial_dda(fixed20_12 in)
  70. {
  71. return dfixed_frac(in);
  72. }
  73. static int tegra_dc_set_timings(struct tegra_dc *dc,
  74. struct drm_display_mode *mode)
  75. {
  76. /* TODO: For HDMI compliance, h & v ref_to_sync should be set to 1 */
  77. unsigned int h_ref_to_sync = 0;
  78. unsigned int v_ref_to_sync = 0;
  79. unsigned long value;
  80. tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
  81. value = (v_ref_to_sync << 16) | h_ref_to_sync;
  82. tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
  83. value = ((mode->vsync_end - mode->vsync_start) << 16) |
  84. ((mode->hsync_end - mode->hsync_start) << 0);
  85. tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
  86. value = ((mode->vtotal - mode->vsync_end) << 16) |
  87. ((mode->htotal - mode->hsync_end) << 0);
  88. tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
  89. value = ((mode->vsync_start - mode->vdisplay) << 16) |
  90. ((mode->hsync_start - mode->hdisplay) << 0);
  91. tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
  92. value = (mode->vdisplay << 16) | mode->hdisplay;
  93. tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
  94. return 0;
  95. }
  96. static int tegra_crtc_setup_clk(struct drm_crtc *crtc,
  97. struct drm_display_mode *mode,
  98. unsigned long *div)
  99. {
  100. unsigned long pclk = mode->clock * 1000, rate;
  101. struct tegra_dc *dc = to_tegra_dc(crtc);
  102. struct tegra_output *output = NULL;
  103. struct drm_encoder *encoder;
  104. long err;
  105. list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list, head)
  106. if (encoder->crtc == crtc) {
  107. output = encoder_to_output(encoder);
  108. break;
  109. }
  110. if (!output)
  111. return -ENODEV;
  112. /*
  113. * This assumes that the display controller will divide its parent
  114. * clock by 2 to generate the pixel clock.
  115. */
  116. err = tegra_output_setup_clock(output, dc->clk, pclk * 2);
  117. if (err < 0) {
  118. dev_err(dc->dev, "failed to setup clock: %ld\n", err);
  119. return err;
  120. }
  121. rate = clk_get_rate(dc->clk);
  122. *div = (rate * 2 / pclk) - 2;
  123. DRM_DEBUG_KMS("rate: %lu, div: %lu\n", rate, *div);
  124. return 0;
  125. }
  126. static int tegra_crtc_mode_set(struct drm_crtc *crtc,
  127. struct drm_display_mode *mode,
  128. struct drm_display_mode *adjusted,
  129. int x, int y, struct drm_framebuffer *old_fb)
  130. {
  131. struct tegra_framebuffer *fb = to_tegra_fb(crtc->fb);
  132. struct tegra_dc *dc = to_tegra_dc(crtc);
  133. unsigned int h_dda, v_dda, bpp;
  134. struct tegra_dc_window win;
  135. unsigned long div, value;
  136. int err;
  137. err = tegra_crtc_setup_clk(crtc, mode, &div);
  138. if (err) {
  139. dev_err(dc->dev, "failed to setup clock for CRTC: %d\n", err);
  140. return err;
  141. }
  142. /* program display mode */
  143. tegra_dc_set_timings(dc, mode);
  144. value = DE_SELECT_ACTIVE | DE_CONTROL_NORMAL;
  145. tegra_dc_writel(dc, value, DC_DISP_DATA_ENABLE_OPTIONS);
  146. value = tegra_dc_readl(dc, DC_COM_PIN_OUTPUT_POLARITY(1));
  147. value &= ~LVS_OUTPUT_POLARITY_LOW;
  148. value &= ~LHS_OUTPUT_POLARITY_LOW;
  149. tegra_dc_writel(dc, value, DC_COM_PIN_OUTPUT_POLARITY(1));
  150. value = DISP_DATA_FORMAT_DF1P1C | DISP_ALIGNMENT_MSB |
  151. DISP_ORDER_RED_BLUE;
  152. tegra_dc_writel(dc, value, DC_DISP_DISP_INTERFACE_CONTROL);
  153. tegra_dc_writel(dc, 0x00010001, DC_DISP_SHIFT_CLOCK_OPTIONS);
  154. value = SHIFT_CLK_DIVIDER(div) | PIXEL_CLK_DIVIDER_PCD1;
  155. tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
  156. /* setup window parameters */
  157. memset(&win, 0, sizeof(win));
  158. win.x.full = dfixed_const(0);
  159. win.y.full = dfixed_const(0);
  160. win.w.full = dfixed_const(mode->hdisplay);
  161. win.h.full = dfixed_const(mode->vdisplay);
  162. win.outx = 0;
  163. win.outy = 0;
  164. win.outw = mode->hdisplay;
  165. win.outh = mode->vdisplay;
  166. switch (crtc->fb->pixel_format) {
  167. case DRM_FORMAT_XRGB8888:
  168. win.fmt = WIN_COLOR_DEPTH_B8G8R8A8;
  169. break;
  170. case DRM_FORMAT_RGB565:
  171. win.fmt = WIN_COLOR_DEPTH_B5G6R5;
  172. break;
  173. default:
  174. win.fmt = WIN_COLOR_DEPTH_B8G8R8A8;
  175. WARN_ON(1);
  176. break;
  177. }
  178. bpp = crtc->fb->bits_per_pixel / 8;
  179. win.stride = crtc->fb->pitches[0];
  180. /* program window registers */
  181. value = WINDOW_A_SELECT;
  182. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
  183. tegra_dc_writel(dc, win.fmt, DC_WIN_COLOR_DEPTH);
  184. tegra_dc_writel(dc, 0, DC_WIN_BYTE_SWAP);
  185. value = V_POSITION(win.outy) | H_POSITION(win.outx);
  186. tegra_dc_writel(dc, value, DC_WIN_POSITION);
  187. value = V_SIZE(win.outh) | H_SIZE(win.outw);
  188. tegra_dc_writel(dc, value, DC_WIN_SIZE);
  189. value = V_PRESCALED_SIZE(dfixed_trunc(win.h)) |
  190. H_PRESCALED_SIZE(dfixed_trunc(win.w) * bpp);
  191. tegra_dc_writel(dc, value, DC_WIN_PRESCALED_SIZE);
  192. h_dda = compute_dda_inc(win.w, win.outw, false, bpp);
  193. v_dda = compute_dda_inc(win.h, win.outh, true, bpp);
  194. value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
  195. tegra_dc_writel(dc, value, DC_WIN_DDA_INC);
  196. h_dda = compute_initial_dda(win.x);
  197. v_dda = compute_initial_dda(win.y);
  198. tegra_dc_writel(dc, h_dda, DC_WIN_H_INITIAL_DDA);
  199. tegra_dc_writel(dc, v_dda, DC_WIN_V_INITIAL_DDA);
  200. tegra_dc_writel(dc, 0, DC_WIN_UV_BUF_STRIDE);
  201. tegra_dc_writel(dc, 0, DC_WIN_BUF_STRIDE);
  202. tegra_dc_writel(dc, fb->obj->paddr, DC_WINBUF_START_ADDR);
  203. tegra_dc_writel(dc, win.stride, DC_WIN_LINE_STRIDE);
  204. tegra_dc_writel(dc, dfixed_trunc(win.x) * bpp,
  205. DC_WINBUF_ADDR_H_OFFSET);
  206. tegra_dc_writel(dc, dfixed_trunc(win.y), DC_WINBUF_ADDR_V_OFFSET);
  207. value = WIN_ENABLE;
  208. if (bpp < 24)
  209. value |= COLOR_EXPAND;
  210. tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
  211. tegra_dc_writel(dc, 0xff00, DC_WIN_BLEND_NOKEY);
  212. tegra_dc_writel(dc, 0xff00, DC_WIN_BLEND_1WIN);
  213. return 0;
  214. }
  215. static void tegra_crtc_prepare(struct drm_crtc *crtc)
  216. {
  217. struct tegra_dc *dc = to_tegra_dc(crtc);
  218. unsigned int syncpt;
  219. unsigned long value;
  220. /* hardware initialization */
  221. tegra_periph_reset_deassert(dc->clk);
  222. usleep_range(10000, 20000);
  223. if (dc->pipe)
  224. syncpt = SYNCPT_VBLANK1;
  225. else
  226. syncpt = SYNCPT_VBLANK0;
  227. /* initialize display controller */
  228. tegra_dc_writel(dc, 0x00000100, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
  229. tegra_dc_writel(dc, 0x100 | syncpt, DC_CMD_CONT_SYNCPT_VSYNC);
  230. value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT | WIN_A_OF_INT;
  231. tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
  232. value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
  233. WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
  234. tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
  235. value = PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
  236. PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
  237. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
  238. value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
  239. value |= DISP_CTRL_MODE_C_DISPLAY;
  240. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
  241. /* initialize timer */
  242. value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
  243. WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
  244. tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
  245. value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
  246. WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
  247. tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
  248. value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT;
  249. tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
  250. value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT;
  251. tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
  252. }
  253. static void tegra_crtc_commit(struct drm_crtc *crtc)
  254. {
  255. struct tegra_dc *dc = to_tegra_dc(crtc);
  256. unsigned long update_mask;
  257. unsigned long value;
  258. update_mask = GENERAL_ACT_REQ | WIN_A_ACT_REQ;
  259. tegra_dc_writel(dc, update_mask << 8, DC_CMD_STATE_CONTROL);
  260. value = tegra_dc_readl(dc, DC_CMD_INT_ENABLE);
  261. value |= FRAME_END_INT;
  262. tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
  263. value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
  264. value |= FRAME_END_INT;
  265. tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
  266. tegra_dc_writel(dc, update_mask, DC_CMD_STATE_CONTROL);
  267. }
  268. static void tegra_crtc_load_lut(struct drm_crtc *crtc)
  269. {
  270. }
  271. static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
  272. .dpms = tegra_crtc_dpms,
  273. .mode_fixup = tegra_crtc_mode_fixup,
  274. .mode_set = tegra_crtc_mode_set,
  275. .prepare = tegra_crtc_prepare,
  276. .commit = tegra_crtc_commit,
  277. .load_lut = tegra_crtc_load_lut,
  278. };
  279. static irqreturn_t tegra_drm_irq(int irq, void *data)
  280. {
  281. struct tegra_dc *dc = data;
  282. unsigned long status;
  283. status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
  284. tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
  285. if (status & FRAME_END_INT) {
  286. /*
  287. dev_dbg(dc->dev, "%s(): frame end\n", __func__);
  288. */
  289. }
  290. if (status & VBLANK_INT) {
  291. /*
  292. dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
  293. */
  294. drm_handle_vblank(dc->base.dev, dc->pipe);
  295. }
  296. if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
  297. /*
  298. dev_dbg(dc->dev, "%s(): underflow\n", __func__);
  299. */
  300. }
  301. return IRQ_HANDLED;
  302. }
  303. static int tegra_dc_show_regs(struct seq_file *s, void *data)
  304. {
  305. struct drm_info_node *node = s->private;
  306. struct tegra_dc *dc = node->info_ent->data;
  307. #define DUMP_REG(name) \
  308. seq_printf(s, "%-40s %#05x %08lx\n", #name, name, \
  309. tegra_dc_readl(dc, name))
  310. DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT);
  311. DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
  312. DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_ERROR);
  313. DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT);
  314. DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL);
  315. DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_ERROR);
  316. DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT);
  317. DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL);
  318. DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_ERROR);
  319. DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT);
  320. DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL);
  321. DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_ERROR);
  322. DUMP_REG(DC_CMD_CONT_SYNCPT_VSYNC);
  323. DUMP_REG(DC_CMD_DISPLAY_COMMAND_OPTION0);
  324. DUMP_REG(DC_CMD_DISPLAY_COMMAND);
  325. DUMP_REG(DC_CMD_SIGNAL_RAISE);
  326. DUMP_REG(DC_CMD_DISPLAY_POWER_CONTROL);
  327. DUMP_REG(DC_CMD_INT_STATUS);
  328. DUMP_REG(DC_CMD_INT_MASK);
  329. DUMP_REG(DC_CMD_INT_ENABLE);
  330. DUMP_REG(DC_CMD_INT_TYPE);
  331. DUMP_REG(DC_CMD_INT_POLARITY);
  332. DUMP_REG(DC_CMD_SIGNAL_RAISE1);
  333. DUMP_REG(DC_CMD_SIGNAL_RAISE2);
  334. DUMP_REG(DC_CMD_SIGNAL_RAISE3);
  335. DUMP_REG(DC_CMD_STATE_ACCESS);
  336. DUMP_REG(DC_CMD_STATE_CONTROL);
  337. DUMP_REG(DC_CMD_DISPLAY_WINDOW_HEADER);
  338. DUMP_REG(DC_CMD_REG_ACT_CONTROL);
  339. DUMP_REG(DC_COM_CRC_CONTROL);
  340. DUMP_REG(DC_COM_CRC_CHECKSUM);
  341. DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(0));
  342. DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(1));
  343. DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(2));
  344. DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(3));
  345. DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(0));
  346. DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(1));
  347. DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(2));
  348. DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(3));
  349. DUMP_REG(DC_COM_PIN_OUTPUT_DATA(0));
  350. DUMP_REG(DC_COM_PIN_OUTPUT_DATA(1));
  351. DUMP_REG(DC_COM_PIN_OUTPUT_DATA(2));
  352. DUMP_REG(DC_COM_PIN_OUTPUT_DATA(3));
  353. DUMP_REG(DC_COM_PIN_INPUT_ENABLE(0));
  354. DUMP_REG(DC_COM_PIN_INPUT_ENABLE(1));
  355. DUMP_REG(DC_COM_PIN_INPUT_ENABLE(2));
  356. DUMP_REG(DC_COM_PIN_INPUT_ENABLE(3));
  357. DUMP_REG(DC_COM_PIN_INPUT_DATA(0));
  358. DUMP_REG(DC_COM_PIN_INPUT_DATA(1));
  359. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(0));
  360. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(1));
  361. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(2));
  362. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(3));
  363. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(4));
  364. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(5));
  365. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(6));
  366. DUMP_REG(DC_COM_PIN_MISC_CONTROL);
  367. DUMP_REG(DC_COM_PIN_PM0_CONTROL);
  368. DUMP_REG(DC_COM_PIN_PM0_DUTY_CYCLE);
  369. DUMP_REG(DC_COM_PIN_PM1_CONTROL);
  370. DUMP_REG(DC_COM_PIN_PM1_DUTY_CYCLE);
  371. DUMP_REG(DC_COM_SPI_CONTROL);
  372. DUMP_REG(DC_COM_SPI_START_BYTE);
  373. DUMP_REG(DC_COM_HSPI_WRITE_DATA_AB);
  374. DUMP_REG(DC_COM_HSPI_WRITE_DATA_CD);
  375. DUMP_REG(DC_COM_HSPI_CS_DC);
  376. DUMP_REG(DC_COM_SCRATCH_REGISTER_A);
  377. DUMP_REG(DC_COM_SCRATCH_REGISTER_B);
  378. DUMP_REG(DC_COM_GPIO_CTRL);
  379. DUMP_REG(DC_COM_GPIO_DEBOUNCE_COUNTER);
  380. DUMP_REG(DC_COM_CRC_CHECKSUM_LATCHED);
  381. DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS0);
  382. DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS1);
  383. DUMP_REG(DC_DISP_DISP_WIN_OPTIONS);
  384. DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY);
  385. DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
  386. DUMP_REG(DC_DISP_DISP_TIMING_OPTIONS);
  387. DUMP_REG(DC_DISP_REF_TO_SYNC);
  388. DUMP_REG(DC_DISP_SYNC_WIDTH);
  389. DUMP_REG(DC_DISP_BACK_PORCH);
  390. DUMP_REG(DC_DISP_ACTIVE);
  391. DUMP_REG(DC_DISP_FRONT_PORCH);
  392. DUMP_REG(DC_DISP_H_PULSE0_CONTROL);
  393. DUMP_REG(DC_DISP_H_PULSE0_POSITION_A);
  394. DUMP_REG(DC_DISP_H_PULSE0_POSITION_B);
  395. DUMP_REG(DC_DISP_H_PULSE0_POSITION_C);
  396. DUMP_REG(DC_DISP_H_PULSE0_POSITION_D);
  397. DUMP_REG(DC_DISP_H_PULSE1_CONTROL);
  398. DUMP_REG(DC_DISP_H_PULSE1_POSITION_A);
  399. DUMP_REG(DC_DISP_H_PULSE1_POSITION_B);
  400. DUMP_REG(DC_DISP_H_PULSE1_POSITION_C);
  401. DUMP_REG(DC_DISP_H_PULSE1_POSITION_D);
  402. DUMP_REG(DC_DISP_H_PULSE2_CONTROL);
  403. DUMP_REG(DC_DISP_H_PULSE2_POSITION_A);
  404. DUMP_REG(DC_DISP_H_PULSE2_POSITION_B);
  405. DUMP_REG(DC_DISP_H_PULSE2_POSITION_C);
  406. DUMP_REG(DC_DISP_H_PULSE2_POSITION_D);
  407. DUMP_REG(DC_DISP_V_PULSE0_CONTROL);
  408. DUMP_REG(DC_DISP_V_PULSE0_POSITION_A);
  409. DUMP_REG(DC_DISP_V_PULSE0_POSITION_B);
  410. DUMP_REG(DC_DISP_V_PULSE0_POSITION_C);
  411. DUMP_REG(DC_DISP_V_PULSE1_CONTROL);
  412. DUMP_REG(DC_DISP_V_PULSE1_POSITION_A);
  413. DUMP_REG(DC_DISP_V_PULSE1_POSITION_B);
  414. DUMP_REG(DC_DISP_V_PULSE1_POSITION_C);
  415. DUMP_REG(DC_DISP_V_PULSE2_CONTROL);
  416. DUMP_REG(DC_DISP_V_PULSE2_POSITION_A);
  417. DUMP_REG(DC_DISP_V_PULSE3_CONTROL);
  418. DUMP_REG(DC_DISP_V_PULSE3_POSITION_A);
  419. DUMP_REG(DC_DISP_M0_CONTROL);
  420. DUMP_REG(DC_DISP_M1_CONTROL);
  421. DUMP_REG(DC_DISP_DI_CONTROL);
  422. DUMP_REG(DC_DISP_PP_CONTROL);
  423. DUMP_REG(DC_DISP_PP_SELECT_A);
  424. DUMP_REG(DC_DISP_PP_SELECT_B);
  425. DUMP_REG(DC_DISP_PP_SELECT_C);
  426. DUMP_REG(DC_DISP_PP_SELECT_D);
  427. DUMP_REG(DC_DISP_DISP_CLOCK_CONTROL);
  428. DUMP_REG(DC_DISP_DISP_INTERFACE_CONTROL);
  429. DUMP_REG(DC_DISP_DISP_COLOR_CONTROL);
  430. DUMP_REG(DC_DISP_SHIFT_CLOCK_OPTIONS);
  431. DUMP_REG(DC_DISP_DATA_ENABLE_OPTIONS);
  432. DUMP_REG(DC_DISP_SERIAL_INTERFACE_OPTIONS);
  433. DUMP_REG(DC_DISP_LCD_SPI_OPTIONS);
  434. DUMP_REG(DC_DISP_BORDER_COLOR);
  435. DUMP_REG(DC_DISP_COLOR_KEY0_LOWER);
  436. DUMP_REG(DC_DISP_COLOR_KEY0_UPPER);
  437. DUMP_REG(DC_DISP_COLOR_KEY1_LOWER);
  438. DUMP_REG(DC_DISP_COLOR_KEY1_UPPER);
  439. DUMP_REG(DC_DISP_CURSOR_FOREGROUND);
  440. DUMP_REG(DC_DISP_CURSOR_BACKGROUND);
  441. DUMP_REG(DC_DISP_CURSOR_START_ADDR);
  442. DUMP_REG(DC_DISP_CURSOR_START_ADDR_NS);
  443. DUMP_REG(DC_DISP_CURSOR_POSITION);
  444. DUMP_REG(DC_DISP_CURSOR_POSITION_NS);
  445. DUMP_REG(DC_DISP_INIT_SEQ_CONTROL);
  446. DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_A);
  447. DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_B);
  448. DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_C);
  449. DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_D);
  450. DUMP_REG(DC_DISP_DC_MCCIF_FIFOCTRL);
  451. DUMP_REG(DC_DISP_MCCIF_DISPLAY0A_HYST);
  452. DUMP_REG(DC_DISP_MCCIF_DISPLAY0B_HYST);
  453. DUMP_REG(DC_DISP_MCCIF_DISPLAY1A_HYST);
  454. DUMP_REG(DC_DISP_MCCIF_DISPLAY1B_HYST);
  455. DUMP_REG(DC_DISP_DAC_CRT_CTRL);
  456. DUMP_REG(DC_DISP_DISP_MISC_CONTROL);
  457. DUMP_REG(DC_DISP_SD_CONTROL);
  458. DUMP_REG(DC_DISP_SD_CSC_COEFF);
  459. DUMP_REG(DC_DISP_SD_LUT(0));
  460. DUMP_REG(DC_DISP_SD_LUT(1));
  461. DUMP_REG(DC_DISP_SD_LUT(2));
  462. DUMP_REG(DC_DISP_SD_LUT(3));
  463. DUMP_REG(DC_DISP_SD_LUT(4));
  464. DUMP_REG(DC_DISP_SD_LUT(5));
  465. DUMP_REG(DC_DISP_SD_LUT(6));
  466. DUMP_REG(DC_DISP_SD_LUT(7));
  467. DUMP_REG(DC_DISP_SD_LUT(8));
  468. DUMP_REG(DC_DISP_SD_FLICKER_CONTROL);
  469. DUMP_REG(DC_DISP_DC_PIXEL_COUNT);
  470. DUMP_REG(DC_DISP_SD_HISTOGRAM(0));
  471. DUMP_REG(DC_DISP_SD_HISTOGRAM(1));
  472. DUMP_REG(DC_DISP_SD_HISTOGRAM(2));
  473. DUMP_REG(DC_DISP_SD_HISTOGRAM(3));
  474. DUMP_REG(DC_DISP_SD_HISTOGRAM(4));
  475. DUMP_REG(DC_DISP_SD_HISTOGRAM(5));
  476. DUMP_REG(DC_DISP_SD_HISTOGRAM(6));
  477. DUMP_REG(DC_DISP_SD_HISTOGRAM(7));
  478. DUMP_REG(DC_DISP_SD_BL_TF(0));
  479. DUMP_REG(DC_DISP_SD_BL_TF(1));
  480. DUMP_REG(DC_DISP_SD_BL_TF(2));
  481. DUMP_REG(DC_DISP_SD_BL_TF(3));
  482. DUMP_REG(DC_DISP_SD_BL_CONTROL);
  483. DUMP_REG(DC_DISP_SD_HW_K_VALUES);
  484. DUMP_REG(DC_DISP_SD_MAN_K_VALUES);
  485. DUMP_REG(DC_WIN_WIN_OPTIONS);
  486. DUMP_REG(DC_WIN_BYTE_SWAP);
  487. DUMP_REG(DC_WIN_BUFFER_CONTROL);
  488. DUMP_REG(DC_WIN_COLOR_DEPTH);
  489. DUMP_REG(DC_WIN_POSITION);
  490. DUMP_REG(DC_WIN_SIZE);
  491. DUMP_REG(DC_WIN_PRESCALED_SIZE);
  492. DUMP_REG(DC_WIN_H_INITIAL_DDA);
  493. DUMP_REG(DC_WIN_V_INITIAL_DDA);
  494. DUMP_REG(DC_WIN_DDA_INC);
  495. DUMP_REG(DC_WIN_LINE_STRIDE);
  496. DUMP_REG(DC_WIN_BUF_STRIDE);
  497. DUMP_REG(DC_WIN_UV_BUF_STRIDE);
  498. DUMP_REG(DC_WIN_BUFFER_ADDR_MODE);
  499. DUMP_REG(DC_WIN_DV_CONTROL);
  500. DUMP_REG(DC_WIN_BLEND_NOKEY);
  501. DUMP_REG(DC_WIN_BLEND_1WIN);
  502. DUMP_REG(DC_WIN_BLEND_2WIN_X);
  503. DUMP_REG(DC_WIN_BLEND_2WIN_Y);
  504. DUMP_REG(DC_WIN_BLEND32WIN_XY);
  505. DUMP_REG(DC_WIN_HP_FETCH_CONTROL);
  506. DUMP_REG(DC_WINBUF_START_ADDR);
  507. DUMP_REG(DC_WINBUF_START_ADDR_NS);
  508. DUMP_REG(DC_WINBUF_START_ADDR_U);
  509. DUMP_REG(DC_WINBUF_START_ADDR_U_NS);
  510. DUMP_REG(DC_WINBUF_START_ADDR_V);
  511. DUMP_REG(DC_WINBUF_START_ADDR_V_NS);
  512. DUMP_REG(DC_WINBUF_ADDR_H_OFFSET);
  513. DUMP_REG(DC_WINBUF_ADDR_H_OFFSET_NS);
  514. DUMP_REG(DC_WINBUF_ADDR_V_OFFSET);
  515. DUMP_REG(DC_WINBUF_ADDR_V_OFFSET_NS);
  516. DUMP_REG(DC_WINBUF_UFLOW_STATUS);
  517. DUMP_REG(DC_WINBUF_AD_UFLOW_STATUS);
  518. DUMP_REG(DC_WINBUF_BD_UFLOW_STATUS);
  519. DUMP_REG(DC_WINBUF_CD_UFLOW_STATUS);
  520. #undef DUMP_REG
  521. return 0;
  522. }
  523. static struct drm_info_list debugfs_files[] = {
  524. { "regs", tegra_dc_show_regs, 0, NULL },
  525. };
  526. static int tegra_dc_debugfs_init(struct tegra_dc *dc, struct drm_minor *minor)
  527. {
  528. unsigned int i;
  529. char *name;
  530. int err;
  531. name = kasprintf(GFP_KERNEL, "dc.%d", dc->pipe);
  532. dc->debugfs = debugfs_create_dir(name, minor->debugfs_root);
  533. kfree(name);
  534. if (!dc->debugfs)
  535. return -ENOMEM;
  536. dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
  537. GFP_KERNEL);
  538. if (!dc->debugfs_files) {
  539. err = -ENOMEM;
  540. goto remove;
  541. }
  542. for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
  543. dc->debugfs_files[i].data = dc;
  544. err = drm_debugfs_create_files(dc->debugfs_files,
  545. ARRAY_SIZE(debugfs_files),
  546. dc->debugfs, minor);
  547. if (err < 0)
  548. goto free;
  549. dc->minor = minor;
  550. return 0;
  551. free:
  552. kfree(dc->debugfs_files);
  553. dc->debugfs_files = NULL;
  554. remove:
  555. debugfs_remove(dc->debugfs);
  556. dc->debugfs = NULL;
  557. return err;
  558. }
  559. static int tegra_dc_debugfs_exit(struct tegra_dc *dc)
  560. {
  561. drm_debugfs_remove_files(dc->debugfs_files, ARRAY_SIZE(debugfs_files),
  562. dc->minor);
  563. dc->minor = NULL;
  564. kfree(dc->debugfs_files);
  565. dc->debugfs_files = NULL;
  566. debugfs_remove(dc->debugfs);
  567. dc->debugfs = NULL;
  568. return 0;
  569. }
  570. static int tegra_dc_drm_init(struct host1x_client *client,
  571. struct drm_device *drm)
  572. {
  573. struct tegra_dc *dc = host1x_client_to_dc(client);
  574. int err;
  575. dc->pipe = drm->mode_config.num_crtc;
  576. drm_crtc_init(drm, &dc->base, &tegra_crtc_funcs);
  577. drm_mode_crtc_set_gamma_size(&dc->base, 256);
  578. drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
  579. err = tegra_dc_rgb_init(drm, dc);
  580. if (err < 0 && err != -ENODEV) {
  581. dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
  582. return err;
  583. }
  584. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  585. err = tegra_dc_debugfs_init(dc, drm->primary);
  586. if (err < 0)
  587. dev_err(dc->dev, "debugfs setup failed: %d\n", err);
  588. }
  589. err = devm_request_irq(dc->dev, dc->irq, tegra_drm_irq, 0,
  590. dev_name(dc->dev), dc);
  591. if (err < 0) {
  592. dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
  593. err);
  594. return err;
  595. }
  596. return 0;
  597. }
  598. static int tegra_dc_drm_exit(struct host1x_client *client)
  599. {
  600. struct tegra_dc *dc = host1x_client_to_dc(client);
  601. int err;
  602. devm_free_irq(dc->dev, dc->irq, dc);
  603. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  604. err = tegra_dc_debugfs_exit(dc);
  605. if (err < 0)
  606. dev_err(dc->dev, "debugfs cleanup failed: %d\n", err);
  607. }
  608. err = tegra_dc_rgb_exit(dc);
  609. if (err) {
  610. dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
  611. return err;
  612. }
  613. return 0;
  614. }
  615. static const struct host1x_client_ops dc_client_ops = {
  616. .drm_init = tegra_dc_drm_init,
  617. .drm_exit = tegra_dc_drm_exit,
  618. };
  619. static int tegra_dc_probe(struct platform_device *pdev)
  620. {
  621. struct host1x *host1x = dev_get_drvdata(pdev->dev.parent);
  622. struct resource *regs;
  623. struct tegra_dc *dc;
  624. int err;
  625. dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
  626. if (!dc)
  627. return -ENOMEM;
  628. INIT_LIST_HEAD(&dc->list);
  629. dc->dev = &pdev->dev;
  630. dc->clk = devm_clk_get(&pdev->dev, NULL);
  631. if (IS_ERR(dc->clk)) {
  632. dev_err(&pdev->dev, "failed to get clock\n");
  633. return PTR_ERR(dc->clk);
  634. }
  635. err = clk_prepare_enable(dc->clk);
  636. if (err < 0)
  637. return err;
  638. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  639. if (!regs) {
  640. dev_err(&pdev->dev, "failed to get registers\n");
  641. return -ENXIO;
  642. }
  643. dc->regs = devm_request_and_ioremap(&pdev->dev, regs);
  644. if (!dc->regs) {
  645. dev_err(&pdev->dev, "failed to remap registers\n");
  646. return -ENXIO;
  647. }
  648. dc->irq = platform_get_irq(pdev, 0);
  649. if (dc->irq < 0) {
  650. dev_err(&pdev->dev, "failed to get IRQ\n");
  651. return -ENXIO;
  652. }
  653. INIT_LIST_HEAD(&dc->client.list);
  654. dc->client.ops = &dc_client_ops;
  655. dc->client.dev = &pdev->dev;
  656. err = tegra_dc_rgb_probe(dc);
  657. if (err < 0 && err != -ENODEV) {
  658. dev_err(&pdev->dev, "failed to probe RGB output: %d\n", err);
  659. return err;
  660. }
  661. err = host1x_register_client(host1x, &dc->client);
  662. if (err < 0) {
  663. dev_err(&pdev->dev, "failed to register host1x client: %d\n",
  664. err);
  665. return err;
  666. }
  667. platform_set_drvdata(pdev, dc);
  668. return 0;
  669. }
  670. static int tegra_dc_remove(struct platform_device *pdev)
  671. {
  672. struct host1x *host1x = dev_get_drvdata(pdev->dev.parent);
  673. struct tegra_dc *dc = platform_get_drvdata(pdev);
  674. int err;
  675. err = host1x_unregister_client(host1x, &dc->client);
  676. if (err < 0) {
  677. dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
  678. err);
  679. return err;
  680. }
  681. clk_disable_unprepare(dc->clk);
  682. return 0;
  683. }
  684. static struct of_device_id tegra_dc_of_match[] = {
  685. { .compatible = "nvidia,tegra30-dc", },
  686. { .compatible = "nvidia,tegra20-dc", },
  687. { },
  688. };
  689. struct platform_driver tegra_dc_driver = {
  690. .driver = {
  691. .name = "tegra-dc",
  692. .owner = THIS_MODULE,
  693. .of_match_table = tegra_dc_of_match,
  694. },
  695. .probe = tegra_dc_probe,
  696. .remove = tegra_dc_remove,
  697. };