dc.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912
  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/iommu.h>
  12. #include <linux/reset.h>
  13. #include <soc/tegra/pmc.h>
  14. #include "dc.h"
  15. #include "drm.h"
  16. #include "gem.h"
  17. #include <drm/drm_atomic.h>
  18. #include <drm/drm_atomic_helper.h>
  19. #include <drm/drm_plane_helper.h>
  20. struct tegra_dc_soc_info {
  21. bool supports_border_color;
  22. bool supports_interlacing;
  23. bool supports_cursor;
  24. bool supports_block_linear;
  25. unsigned int pitch_align;
  26. bool has_powergate;
  27. };
  28. struct tegra_plane {
  29. struct drm_plane base;
  30. unsigned int index;
  31. };
  32. static inline struct tegra_plane *to_tegra_plane(struct drm_plane *plane)
  33. {
  34. return container_of(plane, struct tegra_plane, base);
  35. }
  36. static void tegra_dc_window_commit(struct tegra_dc *dc, unsigned int index)
  37. {
  38. u32 value = WIN_A_ACT_REQ << index;
  39. tegra_dc_writel(dc, value << 8, DC_CMD_STATE_CONTROL);
  40. tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
  41. }
  42. static void tegra_dc_cursor_commit(struct tegra_dc *dc)
  43. {
  44. tegra_dc_writel(dc, CURSOR_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
  45. tegra_dc_writel(dc, CURSOR_ACT_REQ, DC_CMD_STATE_CONTROL);
  46. }
  47. /*
  48. * Reads the active copy of a register. This takes the dc->lock spinlock to
  49. * prevent races with the VBLANK processing which also needs access to the
  50. * active copy of some registers.
  51. */
  52. static u32 tegra_dc_readl_active(struct tegra_dc *dc, unsigned long offset)
  53. {
  54. unsigned long flags;
  55. u32 value;
  56. spin_lock_irqsave(&dc->lock, flags);
  57. tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
  58. value = tegra_dc_readl(dc, offset);
  59. tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
  60. spin_unlock_irqrestore(&dc->lock, flags);
  61. return value;
  62. }
  63. /*
  64. * Double-buffered registers have two copies: ASSEMBLY and ACTIVE. When the
  65. * *_ACT_REQ bits are set the ASSEMBLY copy is latched into the ACTIVE copy.
  66. * Latching happens mmediately if the display controller is in STOP mode or
  67. * on the next frame boundary otherwise.
  68. *
  69. * Triple-buffered registers have three copies: ASSEMBLY, ARM and ACTIVE. The
  70. * ASSEMBLY copy is latched into the ARM copy immediately after *_UPDATE bits
  71. * are written. When the *_ACT_REQ bits are written, the ARM copy is latched
  72. * into the ACTIVE copy, either immediately if the display controller is in
  73. * STOP mode, or at the next frame boundary otherwise.
  74. */
  75. void tegra_dc_commit(struct tegra_dc *dc)
  76. {
  77. tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
  78. tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
  79. }
  80. static unsigned int tegra_dc_format(uint32_t format, uint32_t *swap)
  81. {
  82. /* assume no swapping of fetched data */
  83. if (swap)
  84. *swap = BYTE_SWAP_NOSWAP;
  85. switch (format) {
  86. case DRM_FORMAT_XBGR8888:
  87. return WIN_COLOR_DEPTH_R8G8B8A8;
  88. case DRM_FORMAT_XRGB8888:
  89. return WIN_COLOR_DEPTH_B8G8R8A8;
  90. case DRM_FORMAT_RGB565:
  91. return WIN_COLOR_DEPTH_B5G6R5;
  92. case DRM_FORMAT_UYVY:
  93. return WIN_COLOR_DEPTH_YCbCr422;
  94. case DRM_FORMAT_YUYV:
  95. if (swap)
  96. *swap = BYTE_SWAP_SWAP2;
  97. return WIN_COLOR_DEPTH_YCbCr422;
  98. case DRM_FORMAT_YUV420:
  99. return WIN_COLOR_DEPTH_YCbCr420P;
  100. case DRM_FORMAT_YUV422:
  101. return WIN_COLOR_DEPTH_YCbCr422P;
  102. default:
  103. break;
  104. }
  105. WARN(1, "unsupported pixel format %u, using default\n", format);
  106. return WIN_COLOR_DEPTH_B8G8R8A8;
  107. }
  108. static bool tegra_dc_format_is_yuv(unsigned int format, bool *planar)
  109. {
  110. switch (format) {
  111. case WIN_COLOR_DEPTH_YCbCr422:
  112. case WIN_COLOR_DEPTH_YUV422:
  113. if (planar)
  114. *planar = false;
  115. return true;
  116. case WIN_COLOR_DEPTH_YCbCr420P:
  117. case WIN_COLOR_DEPTH_YUV420P:
  118. case WIN_COLOR_DEPTH_YCbCr422P:
  119. case WIN_COLOR_DEPTH_YUV422P:
  120. case WIN_COLOR_DEPTH_YCbCr422R:
  121. case WIN_COLOR_DEPTH_YUV422R:
  122. case WIN_COLOR_DEPTH_YCbCr422RA:
  123. case WIN_COLOR_DEPTH_YUV422RA:
  124. if (planar)
  125. *planar = true;
  126. return true;
  127. }
  128. if (planar)
  129. *planar = false;
  130. return false;
  131. }
  132. static inline u32 compute_dda_inc(unsigned int in, unsigned int out, bool v,
  133. unsigned int bpp)
  134. {
  135. fixed20_12 outf = dfixed_init(out);
  136. fixed20_12 inf = dfixed_init(in);
  137. u32 dda_inc;
  138. int max;
  139. if (v)
  140. max = 15;
  141. else {
  142. switch (bpp) {
  143. case 2:
  144. max = 8;
  145. break;
  146. default:
  147. WARN_ON_ONCE(1);
  148. /* fallthrough */
  149. case 4:
  150. max = 4;
  151. break;
  152. }
  153. }
  154. outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
  155. inf.full -= dfixed_const(1);
  156. dda_inc = dfixed_div(inf, outf);
  157. dda_inc = min_t(u32, dda_inc, dfixed_const(max));
  158. return dda_inc;
  159. }
  160. static inline u32 compute_initial_dda(unsigned int in)
  161. {
  162. fixed20_12 inf = dfixed_init(in);
  163. return dfixed_frac(inf);
  164. }
  165. static void tegra_dc_setup_window(struct tegra_dc *dc, unsigned int index,
  166. const struct tegra_dc_window *window)
  167. {
  168. unsigned h_offset, v_offset, h_size, v_size, h_dda, v_dda, bpp;
  169. unsigned long value, flags;
  170. bool yuv, planar;
  171. /*
  172. * For YUV planar modes, the number of bytes per pixel takes into
  173. * account only the luma component and therefore is 1.
  174. */
  175. yuv = tegra_dc_format_is_yuv(window->format, &planar);
  176. if (!yuv)
  177. bpp = window->bits_per_pixel / 8;
  178. else
  179. bpp = planar ? 1 : 2;
  180. spin_lock_irqsave(&dc->lock, flags);
  181. value = WINDOW_A_SELECT << index;
  182. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
  183. tegra_dc_writel(dc, window->format, DC_WIN_COLOR_DEPTH);
  184. tegra_dc_writel(dc, window->swap, DC_WIN_BYTE_SWAP);
  185. value = V_POSITION(window->dst.y) | H_POSITION(window->dst.x);
  186. tegra_dc_writel(dc, value, DC_WIN_POSITION);
  187. value = V_SIZE(window->dst.h) | H_SIZE(window->dst.w);
  188. tegra_dc_writel(dc, value, DC_WIN_SIZE);
  189. h_offset = window->src.x * bpp;
  190. v_offset = window->src.y;
  191. h_size = window->src.w * bpp;
  192. v_size = window->src.h;
  193. value = V_PRESCALED_SIZE(v_size) | H_PRESCALED_SIZE(h_size);
  194. tegra_dc_writel(dc, value, DC_WIN_PRESCALED_SIZE);
  195. /*
  196. * For DDA computations the number of bytes per pixel for YUV planar
  197. * modes needs to take into account all Y, U and V components.
  198. */
  199. if (yuv && planar)
  200. bpp = 2;
  201. h_dda = compute_dda_inc(window->src.w, window->dst.w, false, bpp);
  202. v_dda = compute_dda_inc(window->src.h, window->dst.h, true, bpp);
  203. value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
  204. tegra_dc_writel(dc, value, DC_WIN_DDA_INC);
  205. h_dda = compute_initial_dda(window->src.x);
  206. v_dda = compute_initial_dda(window->src.y);
  207. tegra_dc_writel(dc, h_dda, DC_WIN_H_INITIAL_DDA);
  208. tegra_dc_writel(dc, v_dda, DC_WIN_V_INITIAL_DDA);
  209. tegra_dc_writel(dc, 0, DC_WIN_UV_BUF_STRIDE);
  210. tegra_dc_writel(dc, 0, DC_WIN_BUF_STRIDE);
  211. tegra_dc_writel(dc, window->base[0], DC_WINBUF_START_ADDR);
  212. if (yuv && planar) {
  213. tegra_dc_writel(dc, window->base[1], DC_WINBUF_START_ADDR_U);
  214. tegra_dc_writel(dc, window->base[2], DC_WINBUF_START_ADDR_V);
  215. value = window->stride[1] << 16 | window->stride[0];
  216. tegra_dc_writel(dc, value, DC_WIN_LINE_STRIDE);
  217. } else {
  218. tegra_dc_writel(dc, window->stride[0], DC_WIN_LINE_STRIDE);
  219. }
  220. if (window->bottom_up)
  221. v_offset += window->src.h - 1;
  222. tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
  223. tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
  224. if (dc->soc->supports_block_linear) {
  225. unsigned long height = window->tiling.value;
  226. switch (window->tiling.mode) {
  227. case TEGRA_BO_TILING_MODE_PITCH:
  228. value = DC_WINBUF_SURFACE_KIND_PITCH;
  229. break;
  230. case TEGRA_BO_TILING_MODE_TILED:
  231. value = DC_WINBUF_SURFACE_KIND_TILED;
  232. break;
  233. case TEGRA_BO_TILING_MODE_BLOCK:
  234. value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
  235. DC_WINBUF_SURFACE_KIND_BLOCK;
  236. break;
  237. }
  238. tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
  239. } else {
  240. switch (window->tiling.mode) {
  241. case TEGRA_BO_TILING_MODE_PITCH:
  242. value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
  243. DC_WIN_BUFFER_ADDR_MODE_LINEAR;
  244. break;
  245. case TEGRA_BO_TILING_MODE_TILED:
  246. value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
  247. DC_WIN_BUFFER_ADDR_MODE_TILE;
  248. break;
  249. case TEGRA_BO_TILING_MODE_BLOCK:
  250. /*
  251. * No need to handle this here because ->atomic_check
  252. * will already have filtered it out.
  253. */
  254. break;
  255. }
  256. tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
  257. }
  258. value = WIN_ENABLE;
  259. if (yuv) {
  260. /* setup default colorspace conversion coefficients */
  261. tegra_dc_writel(dc, 0x00f0, DC_WIN_CSC_YOF);
  262. tegra_dc_writel(dc, 0x012a, DC_WIN_CSC_KYRGB);
  263. tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KUR);
  264. tegra_dc_writel(dc, 0x0198, DC_WIN_CSC_KVR);
  265. tegra_dc_writel(dc, 0x039b, DC_WIN_CSC_KUG);
  266. tegra_dc_writel(dc, 0x032f, DC_WIN_CSC_KVG);
  267. tegra_dc_writel(dc, 0x0204, DC_WIN_CSC_KUB);
  268. tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KVB);
  269. value |= CSC_ENABLE;
  270. } else if (window->bits_per_pixel < 24) {
  271. value |= COLOR_EXPAND;
  272. }
  273. if (window->bottom_up)
  274. value |= V_DIRECTION;
  275. tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
  276. /*
  277. * Disable blending and assume Window A is the bottom-most window,
  278. * Window C is the top-most window and Window B is in the middle.
  279. */
  280. tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_NOKEY);
  281. tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_1WIN);
  282. switch (index) {
  283. case 0:
  284. tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_X);
  285. tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
  286. tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
  287. break;
  288. case 1:
  289. tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
  290. tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
  291. tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
  292. break;
  293. case 2:
  294. tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
  295. tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_Y);
  296. tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_3WIN_XY);
  297. break;
  298. }
  299. tegra_dc_window_commit(dc, index);
  300. spin_unlock_irqrestore(&dc->lock, flags);
  301. }
  302. static void tegra_plane_destroy(struct drm_plane *plane)
  303. {
  304. struct tegra_plane *p = to_tegra_plane(plane);
  305. drm_plane_cleanup(plane);
  306. kfree(p);
  307. }
  308. static const u32 tegra_primary_plane_formats[] = {
  309. DRM_FORMAT_XBGR8888,
  310. DRM_FORMAT_XRGB8888,
  311. DRM_FORMAT_RGB565,
  312. };
  313. static void tegra_primary_plane_destroy(struct drm_plane *plane)
  314. {
  315. tegra_plane_destroy(plane);
  316. }
  317. static const struct drm_plane_funcs tegra_primary_plane_funcs = {
  318. .update_plane = drm_atomic_helper_update_plane,
  319. .disable_plane = drm_atomic_helper_disable_plane,
  320. .destroy = tegra_primary_plane_destroy,
  321. .reset = drm_atomic_helper_plane_reset,
  322. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  323. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  324. };
  325. static int tegra_plane_prepare_fb(struct drm_plane *plane,
  326. struct drm_framebuffer *fb)
  327. {
  328. return 0;
  329. }
  330. static void tegra_plane_cleanup_fb(struct drm_plane *plane,
  331. struct drm_framebuffer *fb)
  332. {
  333. }
  334. static int tegra_plane_atomic_check(struct drm_plane *plane,
  335. struct drm_plane_state *state)
  336. {
  337. struct tegra_dc *dc = to_tegra_dc(state->crtc);
  338. struct tegra_bo_tiling tiling;
  339. int err;
  340. /* no need for further checks if the plane is being disabled */
  341. if (!state->crtc)
  342. return 0;
  343. err = tegra_fb_get_tiling(state->fb, &tiling);
  344. if (err < 0)
  345. return err;
  346. if (tiling.mode == TEGRA_BO_TILING_MODE_BLOCK &&
  347. !dc->soc->supports_block_linear) {
  348. DRM_ERROR("hardware doesn't support block linear mode\n");
  349. return -EINVAL;
  350. }
  351. /*
  352. * Tegra doesn't support different strides for U and V planes so we
  353. * error out if the user tries to display a framebuffer with such a
  354. * configuration.
  355. */
  356. if (drm_format_num_planes(state->fb->pixel_format) > 2) {
  357. if (state->fb->pitches[2] != state->fb->pitches[1]) {
  358. DRM_ERROR("unsupported UV-plane configuration\n");
  359. return -EINVAL;
  360. }
  361. }
  362. return 0;
  363. }
  364. static void tegra_plane_atomic_update(struct drm_plane *plane,
  365. struct drm_plane_state *old_state)
  366. {
  367. struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
  368. struct drm_framebuffer *fb = plane->state->fb;
  369. struct tegra_plane *p = to_tegra_plane(plane);
  370. struct tegra_dc_window window;
  371. unsigned int i;
  372. int err;
  373. /* rien ne va plus */
  374. if (!plane->state->crtc || !plane->state->fb)
  375. return;
  376. memset(&window, 0, sizeof(window));
  377. window.src.x = plane->state->src_x >> 16;
  378. window.src.y = plane->state->src_y >> 16;
  379. window.src.w = plane->state->src_w >> 16;
  380. window.src.h = plane->state->src_h >> 16;
  381. window.dst.x = plane->state->crtc_x;
  382. window.dst.y = plane->state->crtc_y;
  383. window.dst.w = plane->state->crtc_w;
  384. window.dst.h = plane->state->crtc_h;
  385. window.format = tegra_dc_format(fb->pixel_format, &window.swap);
  386. window.bits_per_pixel = fb->bits_per_pixel;
  387. window.bottom_up = tegra_fb_is_bottom_up(fb);
  388. err = tegra_fb_get_tiling(fb, &window.tiling);
  389. WARN_ON(err < 0);
  390. for (i = 0; i < drm_format_num_planes(fb->pixel_format); i++) {
  391. struct tegra_bo *bo = tegra_fb_get_plane(fb, i);
  392. window.base[i] = bo->paddr + fb->offsets[i];
  393. window.stride[i] = fb->pitches[i];
  394. }
  395. tegra_dc_setup_window(dc, p->index, &window);
  396. }
  397. static void tegra_plane_atomic_disable(struct drm_plane *plane,
  398. struct drm_plane_state *old_state)
  399. {
  400. struct tegra_plane *p = to_tegra_plane(plane);
  401. struct tegra_dc *dc;
  402. unsigned long flags;
  403. u32 value;
  404. /* rien ne va plus */
  405. if (!old_state || !old_state->crtc)
  406. return;
  407. dc = to_tegra_dc(old_state->crtc);
  408. spin_lock_irqsave(&dc->lock, flags);
  409. value = WINDOW_A_SELECT << p->index;
  410. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
  411. value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
  412. value &= ~WIN_ENABLE;
  413. tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
  414. tegra_dc_window_commit(dc, p->index);
  415. spin_unlock_irqrestore(&dc->lock, flags);
  416. }
  417. static const struct drm_plane_helper_funcs tegra_primary_plane_helper_funcs = {
  418. .prepare_fb = tegra_plane_prepare_fb,
  419. .cleanup_fb = tegra_plane_cleanup_fb,
  420. .atomic_check = tegra_plane_atomic_check,
  421. .atomic_update = tegra_plane_atomic_update,
  422. .atomic_disable = tegra_plane_atomic_disable,
  423. };
  424. static struct drm_plane *tegra_dc_primary_plane_create(struct drm_device *drm,
  425. struct tegra_dc *dc)
  426. {
  427. /*
  428. * Ideally this would use drm_crtc_mask(), but that would require the
  429. * CRTC to already be in the mode_config's list of CRTCs. However, it
  430. * will only be added to that list in the drm_crtc_init_with_planes()
  431. * (in tegra_dc_init()), which in turn requires registration of these
  432. * planes. So we have ourselves a nice little chicken and egg problem
  433. * here.
  434. *
  435. * We work around this by manually creating the mask from the number
  436. * of CRTCs that have been registered, and should therefore always be
  437. * the same as drm_crtc_index() after registration.
  438. */
  439. unsigned long possible_crtcs = 1 << drm->mode_config.num_crtc;
  440. struct tegra_plane *plane;
  441. unsigned int num_formats;
  442. const u32 *formats;
  443. int err;
  444. plane = kzalloc(sizeof(*plane), GFP_KERNEL);
  445. if (!plane)
  446. return ERR_PTR(-ENOMEM);
  447. num_formats = ARRAY_SIZE(tegra_primary_plane_formats);
  448. formats = tegra_primary_plane_formats;
  449. err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
  450. &tegra_primary_plane_funcs, formats,
  451. num_formats, DRM_PLANE_TYPE_PRIMARY);
  452. if (err < 0) {
  453. kfree(plane);
  454. return ERR_PTR(err);
  455. }
  456. drm_plane_helper_add(&plane->base, &tegra_primary_plane_helper_funcs);
  457. return &plane->base;
  458. }
  459. static const u32 tegra_cursor_plane_formats[] = {
  460. DRM_FORMAT_RGBA8888,
  461. };
  462. static int tegra_cursor_atomic_check(struct drm_plane *plane,
  463. struct drm_plane_state *state)
  464. {
  465. /* no need for further checks if the plane is being disabled */
  466. if (!state->crtc)
  467. return 0;
  468. /* scaling not supported for cursor */
  469. if ((state->src_w >> 16 != state->crtc_w) ||
  470. (state->src_h >> 16 != state->crtc_h))
  471. return -EINVAL;
  472. /* only square cursors supported */
  473. if (state->src_w != state->src_h)
  474. return -EINVAL;
  475. if (state->crtc_w != 32 && state->crtc_w != 64 &&
  476. state->crtc_w != 128 && state->crtc_w != 256)
  477. return -EINVAL;
  478. return 0;
  479. }
  480. static void tegra_cursor_atomic_update(struct drm_plane *plane,
  481. struct drm_plane_state *old_state)
  482. {
  483. struct tegra_bo *bo = tegra_fb_get_plane(plane->state->fb, 0);
  484. struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
  485. struct drm_plane_state *state = plane->state;
  486. u32 value = CURSOR_CLIP_DISPLAY;
  487. /* rien ne va plus */
  488. if (!plane->state->crtc || !plane->state->fb)
  489. return;
  490. switch (state->crtc_w) {
  491. case 32:
  492. value |= CURSOR_SIZE_32x32;
  493. break;
  494. case 64:
  495. value |= CURSOR_SIZE_64x64;
  496. break;
  497. case 128:
  498. value |= CURSOR_SIZE_128x128;
  499. break;
  500. case 256:
  501. value |= CURSOR_SIZE_256x256;
  502. break;
  503. default:
  504. WARN(1, "cursor size %ux%u not supported\n", state->crtc_w,
  505. state->crtc_h);
  506. return;
  507. }
  508. value |= (bo->paddr >> 10) & 0x3fffff;
  509. tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR);
  510. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  511. value = (bo->paddr >> 32) & 0x3;
  512. tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR_HI);
  513. #endif
  514. /* enable cursor and set blend mode */
  515. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  516. value |= CURSOR_ENABLE;
  517. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  518. value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
  519. value &= ~CURSOR_DST_BLEND_MASK;
  520. value &= ~CURSOR_SRC_BLEND_MASK;
  521. value |= CURSOR_MODE_NORMAL;
  522. value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
  523. value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
  524. value |= CURSOR_ALPHA;
  525. tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
  526. /* position the cursor */
  527. value = (state->crtc_y & 0x3fff) << 16 | (state->crtc_x & 0x3fff);
  528. tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
  529. /* apply changes */
  530. tegra_dc_cursor_commit(dc);
  531. tegra_dc_commit(dc);
  532. }
  533. static void tegra_cursor_atomic_disable(struct drm_plane *plane,
  534. struct drm_plane_state *old_state)
  535. {
  536. struct tegra_dc *dc;
  537. u32 value;
  538. /* rien ne va plus */
  539. if (!old_state || !old_state->crtc)
  540. return;
  541. dc = to_tegra_dc(old_state->crtc);
  542. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  543. value &= ~CURSOR_ENABLE;
  544. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  545. tegra_dc_cursor_commit(dc);
  546. tegra_dc_commit(dc);
  547. }
  548. static const struct drm_plane_funcs tegra_cursor_plane_funcs = {
  549. .update_plane = drm_atomic_helper_update_plane,
  550. .disable_plane = drm_atomic_helper_disable_plane,
  551. .destroy = tegra_plane_destroy,
  552. .reset = drm_atomic_helper_plane_reset,
  553. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  554. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  555. };
  556. static const struct drm_plane_helper_funcs tegra_cursor_plane_helper_funcs = {
  557. .prepare_fb = tegra_plane_prepare_fb,
  558. .cleanup_fb = tegra_plane_cleanup_fb,
  559. .atomic_check = tegra_cursor_atomic_check,
  560. .atomic_update = tegra_cursor_atomic_update,
  561. .atomic_disable = tegra_cursor_atomic_disable,
  562. };
  563. static struct drm_plane *tegra_dc_cursor_plane_create(struct drm_device *drm,
  564. struct tegra_dc *dc)
  565. {
  566. struct tegra_plane *plane;
  567. unsigned int num_formats;
  568. const u32 *formats;
  569. int err;
  570. plane = kzalloc(sizeof(*plane), GFP_KERNEL);
  571. if (!plane)
  572. return ERR_PTR(-ENOMEM);
  573. num_formats = ARRAY_SIZE(tegra_cursor_plane_formats);
  574. formats = tegra_cursor_plane_formats;
  575. err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
  576. &tegra_cursor_plane_funcs, formats,
  577. num_formats, DRM_PLANE_TYPE_CURSOR);
  578. if (err < 0) {
  579. kfree(plane);
  580. return ERR_PTR(err);
  581. }
  582. drm_plane_helper_add(&plane->base, &tegra_cursor_plane_helper_funcs);
  583. return &plane->base;
  584. }
  585. static void tegra_overlay_plane_destroy(struct drm_plane *plane)
  586. {
  587. tegra_plane_destroy(plane);
  588. }
  589. static const struct drm_plane_funcs tegra_overlay_plane_funcs = {
  590. .update_plane = drm_atomic_helper_update_plane,
  591. .disable_plane = drm_atomic_helper_disable_plane,
  592. .destroy = tegra_overlay_plane_destroy,
  593. .reset = drm_atomic_helper_plane_reset,
  594. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  595. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  596. };
  597. static const uint32_t tegra_overlay_plane_formats[] = {
  598. DRM_FORMAT_XBGR8888,
  599. DRM_FORMAT_XRGB8888,
  600. DRM_FORMAT_RGB565,
  601. DRM_FORMAT_UYVY,
  602. DRM_FORMAT_YUYV,
  603. DRM_FORMAT_YUV420,
  604. DRM_FORMAT_YUV422,
  605. };
  606. static const struct drm_plane_helper_funcs tegra_overlay_plane_helper_funcs = {
  607. .prepare_fb = tegra_plane_prepare_fb,
  608. .cleanup_fb = tegra_plane_cleanup_fb,
  609. .atomic_check = tegra_plane_atomic_check,
  610. .atomic_update = tegra_plane_atomic_update,
  611. .atomic_disable = tegra_plane_atomic_disable,
  612. };
  613. static struct drm_plane *tegra_dc_overlay_plane_create(struct drm_device *drm,
  614. struct tegra_dc *dc,
  615. unsigned int index)
  616. {
  617. struct tegra_plane *plane;
  618. unsigned int num_formats;
  619. const u32 *formats;
  620. int err;
  621. plane = kzalloc(sizeof(*plane), GFP_KERNEL);
  622. if (!plane)
  623. return ERR_PTR(-ENOMEM);
  624. plane->index = index;
  625. num_formats = ARRAY_SIZE(tegra_overlay_plane_formats);
  626. formats = tegra_overlay_plane_formats;
  627. err = drm_universal_plane_init(drm, &plane->base, 1 << dc->pipe,
  628. &tegra_overlay_plane_funcs, formats,
  629. num_formats, DRM_PLANE_TYPE_OVERLAY);
  630. if (err < 0) {
  631. kfree(plane);
  632. return ERR_PTR(err);
  633. }
  634. drm_plane_helper_add(&plane->base, &tegra_overlay_plane_helper_funcs);
  635. return &plane->base;
  636. }
  637. static int tegra_dc_add_planes(struct drm_device *drm, struct tegra_dc *dc)
  638. {
  639. struct drm_plane *plane;
  640. unsigned int i;
  641. for (i = 0; i < 2; i++) {
  642. plane = tegra_dc_overlay_plane_create(drm, dc, 1 + i);
  643. if (IS_ERR(plane))
  644. return PTR_ERR(plane);
  645. }
  646. return 0;
  647. }
  648. static int tegra_dc_set_base(struct tegra_dc *dc, int x, int y,
  649. struct drm_framebuffer *fb)
  650. {
  651. struct tegra_bo *bo = tegra_fb_get_plane(fb, 0);
  652. unsigned int h_offset = 0, v_offset = 0;
  653. struct tegra_bo_tiling tiling;
  654. unsigned long value, flags;
  655. unsigned int format, swap;
  656. int err;
  657. err = tegra_fb_get_tiling(fb, &tiling);
  658. if (err < 0)
  659. return err;
  660. spin_lock_irqsave(&dc->lock, flags);
  661. tegra_dc_writel(dc, WINDOW_A_SELECT, DC_CMD_DISPLAY_WINDOW_HEADER);
  662. value = fb->offsets[0] + y * fb->pitches[0] +
  663. x * fb->bits_per_pixel / 8;
  664. tegra_dc_writel(dc, bo->paddr + value, DC_WINBUF_START_ADDR);
  665. tegra_dc_writel(dc, fb->pitches[0], DC_WIN_LINE_STRIDE);
  666. format = tegra_dc_format(fb->pixel_format, &swap);
  667. tegra_dc_writel(dc, format, DC_WIN_COLOR_DEPTH);
  668. tegra_dc_writel(dc, swap, DC_WIN_BYTE_SWAP);
  669. if (dc->soc->supports_block_linear) {
  670. unsigned long height = tiling.value;
  671. switch (tiling.mode) {
  672. case TEGRA_BO_TILING_MODE_PITCH:
  673. value = DC_WINBUF_SURFACE_KIND_PITCH;
  674. break;
  675. case TEGRA_BO_TILING_MODE_TILED:
  676. value = DC_WINBUF_SURFACE_KIND_TILED;
  677. break;
  678. case TEGRA_BO_TILING_MODE_BLOCK:
  679. value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
  680. DC_WINBUF_SURFACE_KIND_BLOCK;
  681. break;
  682. }
  683. tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
  684. } else {
  685. switch (tiling.mode) {
  686. case TEGRA_BO_TILING_MODE_PITCH:
  687. value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
  688. DC_WIN_BUFFER_ADDR_MODE_LINEAR;
  689. break;
  690. case TEGRA_BO_TILING_MODE_TILED:
  691. value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
  692. DC_WIN_BUFFER_ADDR_MODE_TILE;
  693. break;
  694. case TEGRA_BO_TILING_MODE_BLOCK:
  695. DRM_ERROR("hardware doesn't support block linear mode\n");
  696. spin_unlock_irqrestore(&dc->lock, flags);
  697. return -EINVAL;
  698. }
  699. tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
  700. }
  701. /* make sure bottom-up buffers are properly displayed */
  702. if (tegra_fb_is_bottom_up(fb)) {
  703. value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
  704. value |= V_DIRECTION;
  705. tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
  706. v_offset += fb->height - 1;
  707. } else {
  708. value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
  709. value &= ~V_DIRECTION;
  710. tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
  711. }
  712. tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
  713. tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
  714. value = GENERAL_ACT_REQ | WIN_A_ACT_REQ;
  715. tegra_dc_writel(dc, value << 8, DC_CMD_STATE_CONTROL);
  716. tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
  717. spin_unlock_irqrestore(&dc->lock, flags);
  718. return 0;
  719. }
  720. void tegra_dc_enable_vblank(struct tegra_dc *dc)
  721. {
  722. unsigned long value, flags;
  723. spin_lock_irqsave(&dc->lock, flags);
  724. value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
  725. value |= VBLANK_INT;
  726. tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
  727. spin_unlock_irqrestore(&dc->lock, flags);
  728. }
  729. void tegra_dc_disable_vblank(struct tegra_dc *dc)
  730. {
  731. unsigned long value, flags;
  732. spin_lock_irqsave(&dc->lock, flags);
  733. value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
  734. value &= ~VBLANK_INT;
  735. tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
  736. spin_unlock_irqrestore(&dc->lock, flags);
  737. }
  738. static void tegra_dc_finish_page_flip(struct tegra_dc *dc)
  739. {
  740. struct drm_device *drm = dc->base.dev;
  741. struct drm_crtc *crtc = &dc->base;
  742. unsigned long flags, base;
  743. struct tegra_bo *bo;
  744. spin_lock_irqsave(&drm->event_lock, flags);
  745. if (!dc->event) {
  746. spin_unlock_irqrestore(&drm->event_lock, flags);
  747. return;
  748. }
  749. bo = tegra_fb_get_plane(crtc->primary->fb, 0);
  750. spin_lock(&dc->lock);
  751. /* check if new start address has been latched */
  752. tegra_dc_writel(dc, WINDOW_A_SELECT, DC_CMD_DISPLAY_WINDOW_HEADER);
  753. tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
  754. base = tegra_dc_readl(dc, DC_WINBUF_START_ADDR);
  755. tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
  756. spin_unlock(&dc->lock);
  757. if (base == bo->paddr + crtc->primary->fb->offsets[0]) {
  758. drm_crtc_send_vblank_event(crtc, dc->event);
  759. drm_crtc_vblank_put(crtc);
  760. dc->event = NULL;
  761. }
  762. spin_unlock_irqrestore(&drm->event_lock, flags);
  763. }
  764. void tegra_dc_cancel_page_flip(struct drm_crtc *crtc, struct drm_file *file)
  765. {
  766. struct tegra_dc *dc = to_tegra_dc(crtc);
  767. struct drm_device *drm = crtc->dev;
  768. unsigned long flags;
  769. spin_lock_irqsave(&drm->event_lock, flags);
  770. if (dc->event && dc->event->base.file_priv == file) {
  771. dc->event->base.destroy(&dc->event->base);
  772. drm_crtc_vblank_put(crtc);
  773. dc->event = NULL;
  774. }
  775. spin_unlock_irqrestore(&drm->event_lock, flags);
  776. }
  777. static int tegra_dc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  778. struct drm_pending_vblank_event *event, uint32_t page_flip_flags)
  779. {
  780. unsigned int pipe = drm_crtc_index(crtc);
  781. struct tegra_dc *dc = to_tegra_dc(crtc);
  782. if (dc->event)
  783. return -EBUSY;
  784. if (event) {
  785. event->pipe = pipe;
  786. dc->event = event;
  787. drm_crtc_vblank_get(crtc);
  788. }
  789. if (crtc->primary->state)
  790. drm_atomic_set_fb_for_plane(crtc->primary->state, fb);
  791. tegra_dc_set_base(dc, 0, 0, fb);
  792. crtc->primary->fb = fb;
  793. return 0;
  794. }
  795. static void tegra_dc_destroy(struct drm_crtc *crtc)
  796. {
  797. drm_crtc_cleanup(crtc);
  798. }
  799. static const struct drm_crtc_funcs tegra_crtc_funcs = {
  800. .page_flip = tegra_dc_page_flip,
  801. .set_config = drm_crtc_helper_set_config,
  802. .destroy = tegra_dc_destroy,
  803. .reset = drm_atomic_helper_crtc_reset,
  804. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  805. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  806. };
  807. static void tegra_dc_stop(struct tegra_dc *dc)
  808. {
  809. u32 value;
  810. /* stop the display controller */
  811. value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
  812. value &= ~DISP_CTRL_MODE_MASK;
  813. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
  814. tegra_dc_commit(dc);
  815. }
  816. static bool tegra_dc_idle(struct tegra_dc *dc)
  817. {
  818. u32 value;
  819. value = tegra_dc_readl_active(dc, DC_CMD_DISPLAY_COMMAND);
  820. return (value & DISP_CTRL_MODE_MASK) == 0;
  821. }
  822. static int tegra_dc_wait_idle(struct tegra_dc *dc, unsigned long timeout)
  823. {
  824. timeout = jiffies + msecs_to_jiffies(timeout);
  825. while (time_before(jiffies, timeout)) {
  826. if (tegra_dc_idle(dc))
  827. return 0;
  828. usleep_range(1000, 2000);
  829. }
  830. dev_dbg(dc->dev, "timeout waiting for DC to become idle\n");
  831. return -ETIMEDOUT;
  832. }
  833. static void tegra_crtc_disable(struct drm_crtc *crtc)
  834. {
  835. struct tegra_dc *dc = to_tegra_dc(crtc);
  836. u32 value;
  837. if (!tegra_dc_idle(dc)) {
  838. tegra_dc_stop(dc);
  839. /*
  840. * Ignore the return value, there isn't anything useful to do
  841. * in case this fails.
  842. */
  843. tegra_dc_wait_idle(dc, 100);
  844. }
  845. /*
  846. * This should really be part of the RGB encoder driver, but clearing
  847. * these bits has the side-effect of stopping the display controller.
  848. * When that happens no VBLANK interrupts will be raised. At the same
  849. * time the encoder is disabled before the display controller, so the
  850. * above code is always going to timeout waiting for the controller
  851. * to go idle.
  852. *
  853. * Given the close coupling between the RGB encoder and the display
  854. * controller doing it here is still kind of okay. None of the other
  855. * encoder drivers require these bits to be cleared.
  856. *
  857. * XXX: Perhaps given that the display controller is switched off at
  858. * this point anyway maybe clearing these bits isn't even useful for
  859. * the RGB encoder?
  860. */
  861. if (dc->rgb) {
  862. value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
  863. value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
  864. PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
  865. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
  866. }
  867. drm_crtc_vblank_off(crtc);
  868. tegra_dc_commit(dc);
  869. }
  870. static bool tegra_crtc_mode_fixup(struct drm_crtc *crtc,
  871. const struct drm_display_mode *mode,
  872. struct drm_display_mode *adjusted)
  873. {
  874. return true;
  875. }
  876. static int tegra_dc_set_timings(struct tegra_dc *dc,
  877. struct drm_display_mode *mode)
  878. {
  879. unsigned int h_ref_to_sync = 1;
  880. unsigned int v_ref_to_sync = 1;
  881. unsigned long value;
  882. tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
  883. value = (v_ref_to_sync << 16) | h_ref_to_sync;
  884. tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
  885. value = ((mode->vsync_end - mode->vsync_start) << 16) |
  886. ((mode->hsync_end - mode->hsync_start) << 0);
  887. tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
  888. value = ((mode->vtotal - mode->vsync_end) << 16) |
  889. ((mode->htotal - mode->hsync_end) << 0);
  890. tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
  891. value = ((mode->vsync_start - mode->vdisplay) << 16) |
  892. ((mode->hsync_start - mode->hdisplay) << 0);
  893. tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
  894. value = (mode->vdisplay << 16) | mode->hdisplay;
  895. tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
  896. return 0;
  897. }
  898. int tegra_dc_setup_clock(struct tegra_dc *dc, struct clk *parent,
  899. unsigned long pclk, unsigned int div)
  900. {
  901. u32 value;
  902. int err;
  903. err = clk_set_parent(dc->clk, parent);
  904. if (err < 0) {
  905. dev_err(dc->dev, "failed to set parent clock: %d\n", err);
  906. return err;
  907. }
  908. DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk), div);
  909. value = SHIFT_CLK_DIVIDER(div) | PIXEL_CLK_DIVIDER_PCD1;
  910. tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
  911. return 0;
  912. }
  913. static void tegra_crtc_mode_set_nofb(struct drm_crtc *crtc)
  914. {
  915. struct drm_display_mode *mode = &crtc->state->adjusted_mode;
  916. struct tegra_dc *dc = to_tegra_dc(crtc);
  917. u32 value;
  918. /* program display mode */
  919. tegra_dc_set_timings(dc, mode);
  920. if (dc->soc->supports_border_color)
  921. tegra_dc_writel(dc, 0, DC_DISP_BORDER_COLOR);
  922. /* interlacing isn't supported yet, so disable it */
  923. if (dc->soc->supports_interlacing) {
  924. value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
  925. value &= ~INTERLACE_ENABLE;
  926. tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
  927. }
  928. }
  929. static void tegra_crtc_prepare(struct drm_crtc *crtc)
  930. {
  931. struct tegra_dc *dc = to_tegra_dc(crtc);
  932. unsigned int syncpt;
  933. unsigned long value;
  934. drm_crtc_vblank_off(crtc);
  935. if (dc->pipe)
  936. syncpt = SYNCPT_VBLANK1;
  937. else
  938. syncpt = SYNCPT_VBLANK0;
  939. /* initialize display controller */
  940. tegra_dc_writel(dc, 0x00000100, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
  941. tegra_dc_writel(dc, 0x100 | syncpt, DC_CMD_CONT_SYNCPT_VSYNC);
  942. value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT | WIN_A_OF_INT;
  943. tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
  944. value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
  945. WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
  946. tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
  947. /* initialize timer */
  948. value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
  949. WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
  950. tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
  951. value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
  952. WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
  953. tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
  954. value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT;
  955. tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
  956. value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT;
  957. tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
  958. }
  959. static void tegra_crtc_commit(struct drm_crtc *crtc)
  960. {
  961. struct tegra_dc *dc = to_tegra_dc(crtc);
  962. drm_crtc_vblank_on(crtc);
  963. tegra_dc_commit(dc);
  964. }
  965. static int tegra_crtc_atomic_check(struct drm_crtc *crtc,
  966. struct drm_crtc_state *state)
  967. {
  968. return 0;
  969. }
  970. static void tegra_crtc_atomic_begin(struct drm_crtc *crtc)
  971. {
  972. }
  973. static void tegra_crtc_atomic_flush(struct drm_crtc *crtc)
  974. {
  975. }
  976. static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
  977. .disable = tegra_crtc_disable,
  978. .mode_fixup = tegra_crtc_mode_fixup,
  979. .mode_set = drm_helper_crtc_mode_set,
  980. .mode_set_nofb = tegra_crtc_mode_set_nofb,
  981. .mode_set_base = drm_helper_crtc_mode_set_base,
  982. .prepare = tegra_crtc_prepare,
  983. .commit = tegra_crtc_commit,
  984. .atomic_check = tegra_crtc_atomic_check,
  985. .atomic_begin = tegra_crtc_atomic_begin,
  986. .atomic_flush = tegra_crtc_atomic_flush,
  987. };
  988. static irqreturn_t tegra_dc_irq(int irq, void *data)
  989. {
  990. struct tegra_dc *dc = data;
  991. unsigned long status;
  992. status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
  993. tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
  994. if (status & FRAME_END_INT) {
  995. /*
  996. dev_dbg(dc->dev, "%s(): frame end\n", __func__);
  997. */
  998. }
  999. if (status & VBLANK_INT) {
  1000. /*
  1001. dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
  1002. */
  1003. drm_crtc_handle_vblank(&dc->base);
  1004. tegra_dc_finish_page_flip(dc);
  1005. }
  1006. if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
  1007. /*
  1008. dev_dbg(dc->dev, "%s(): underflow\n", __func__);
  1009. */
  1010. }
  1011. return IRQ_HANDLED;
  1012. }
  1013. static int tegra_dc_show_regs(struct seq_file *s, void *data)
  1014. {
  1015. struct drm_info_node *node = s->private;
  1016. struct tegra_dc *dc = node->info_ent->data;
  1017. #define DUMP_REG(name) \
  1018. seq_printf(s, "%-40s %#05x %08x\n", #name, name, \
  1019. tegra_dc_readl(dc, name))
  1020. DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT);
  1021. DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
  1022. DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_ERROR);
  1023. DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT);
  1024. DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL);
  1025. DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_ERROR);
  1026. DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT);
  1027. DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL);
  1028. DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_ERROR);
  1029. DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT);
  1030. DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL);
  1031. DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_ERROR);
  1032. DUMP_REG(DC_CMD_CONT_SYNCPT_VSYNC);
  1033. DUMP_REG(DC_CMD_DISPLAY_COMMAND_OPTION0);
  1034. DUMP_REG(DC_CMD_DISPLAY_COMMAND);
  1035. DUMP_REG(DC_CMD_SIGNAL_RAISE);
  1036. DUMP_REG(DC_CMD_DISPLAY_POWER_CONTROL);
  1037. DUMP_REG(DC_CMD_INT_STATUS);
  1038. DUMP_REG(DC_CMD_INT_MASK);
  1039. DUMP_REG(DC_CMD_INT_ENABLE);
  1040. DUMP_REG(DC_CMD_INT_TYPE);
  1041. DUMP_REG(DC_CMD_INT_POLARITY);
  1042. DUMP_REG(DC_CMD_SIGNAL_RAISE1);
  1043. DUMP_REG(DC_CMD_SIGNAL_RAISE2);
  1044. DUMP_REG(DC_CMD_SIGNAL_RAISE3);
  1045. DUMP_REG(DC_CMD_STATE_ACCESS);
  1046. DUMP_REG(DC_CMD_STATE_CONTROL);
  1047. DUMP_REG(DC_CMD_DISPLAY_WINDOW_HEADER);
  1048. DUMP_REG(DC_CMD_REG_ACT_CONTROL);
  1049. DUMP_REG(DC_COM_CRC_CONTROL);
  1050. DUMP_REG(DC_COM_CRC_CHECKSUM);
  1051. DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(0));
  1052. DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(1));
  1053. DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(2));
  1054. DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(3));
  1055. DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(0));
  1056. DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(1));
  1057. DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(2));
  1058. DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(3));
  1059. DUMP_REG(DC_COM_PIN_OUTPUT_DATA(0));
  1060. DUMP_REG(DC_COM_PIN_OUTPUT_DATA(1));
  1061. DUMP_REG(DC_COM_PIN_OUTPUT_DATA(2));
  1062. DUMP_REG(DC_COM_PIN_OUTPUT_DATA(3));
  1063. DUMP_REG(DC_COM_PIN_INPUT_ENABLE(0));
  1064. DUMP_REG(DC_COM_PIN_INPUT_ENABLE(1));
  1065. DUMP_REG(DC_COM_PIN_INPUT_ENABLE(2));
  1066. DUMP_REG(DC_COM_PIN_INPUT_ENABLE(3));
  1067. DUMP_REG(DC_COM_PIN_INPUT_DATA(0));
  1068. DUMP_REG(DC_COM_PIN_INPUT_DATA(1));
  1069. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(0));
  1070. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(1));
  1071. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(2));
  1072. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(3));
  1073. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(4));
  1074. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(5));
  1075. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(6));
  1076. DUMP_REG(DC_COM_PIN_MISC_CONTROL);
  1077. DUMP_REG(DC_COM_PIN_PM0_CONTROL);
  1078. DUMP_REG(DC_COM_PIN_PM0_DUTY_CYCLE);
  1079. DUMP_REG(DC_COM_PIN_PM1_CONTROL);
  1080. DUMP_REG(DC_COM_PIN_PM1_DUTY_CYCLE);
  1081. DUMP_REG(DC_COM_SPI_CONTROL);
  1082. DUMP_REG(DC_COM_SPI_START_BYTE);
  1083. DUMP_REG(DC_COM_HSPI_WRITE_DATA_AB);
  1084. DUMP_REG(DC_COM_HSPI_WRITE_DATA_CD);
  1085. DUMP_REG(DC_COM_HSPI_CS_DC);
  1086. DUMP_REG(DC_COM_SCRATCH_REGISTER_A);
  1087. DUMP_REG(DC_COM_SCRATCH_REGISTER_B);
  1088. DUMP_REG(DC_COM_GPIO_CTRL);
  1089. DUMP_REG(DC_COM_GPIO_DEBOUNCE_COUNTER);
  1090. DUMP_REG(DC_COM_CRC_CHECKSUM_LATCHED);
  1091. DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS0);
  1092. DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS1);
  1093. DUMP_REG(DC_DISP_DISP_WIN_OPTIONS);
  1094. DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY);
  1095. DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
  1096. DUMP_REG(DC_DISP_DISP_TIMING_OPTIONS);
  1097. DUMP_REG(DC_DISP_REF_TO_SYNC);
  1098. DUMP_REG(DC_DISP_SYNC_WIDTH);
  1099. DUMP_REG(DC_DISP_BACK_PORCH);
  1100. DUMP_REG(DC_DISP_ACTIVE);
  1101. DUMP_REG(DC_DISP_FRONT_PORCH);
  1102. DUMP_REG(DC_DISP_H_PULSE0_CONTROL);
  1103. DUMP_REG(DC_DISP_H_PULSE0_POSITION_A);
  1104. DUMP_REG(DC_DISP_H_PULSE0_POSITION_B);
  1105. DUMP_REG(DC_DISP_H_PULSE0_POSITION_C);
  1106. DUMP_REG(DC_DISP_H_PULSE0_POSITION_D);
  1107. DUMP_REG(DC_DISP_H_PULSE1_CONTROL);
  1108. DUMP_REG(DC_DISP_H_PULSE1_POSITION_A);
  1109. DUMP_REG(DC_DISP_H_PULSE1_POSITION_B);
  1110. DUMP_REG(DC_DISP_H_PULSE1_POSITION_C);
  1111. DUMP_REG(DC_DISP_H_PULSE1_POSITION_D);
  1112. DUMP_REG(DC_DISP_H_PULSE2_CONTROL);
  1113. DUMP_REG(DC_DISP_H_PULSE2_POSITION_A);
  1114. DUMP_REG(DC_DISP_H_PULSE2_POSITION_B);
  1115. DUMP_REG(DC_DISP_H_PULSE2_POSITION_C);
  1116. DUMP_REG(DC_DISP_H_PULSE2_POSITION_D);
  1117. DUMP_REG(DC_DISP_V_PULSE0_CONTROL);
  1118. DUMP_REG(DC_DISP_V_PULSE0_POSITION_A);
  1119. DUMP_REG(DC_DISP_V_PULSE0_POSITION_B);
  1120. DUMP_REG(DC_DISP_V_PULSE0_POSITION_C);
  1121. DUMP_REG(DC_DISP_V_PULSE1_CONTROL);
  1122. DUMP_REG(DC_DISP_V_PULSE1_POSITION_A);
  1123. DUMP_REG(DC_DISP_V_PULSE1_POSITION_B);
  1124. DUMP_REG(DC_DISP_V_PULSE1_POSITION_C);
  1125. DUMP_REG(DC_DISP_V_PULSE2_CONTROL);
  1126. DUMP_REG(DC_DISP_V_PULSE2_POSITION_A);
  1127. DUMP_REG(DC_DISP_V_PULSE3_CONTROL);
  1128. DUMP_REG(DC_DISP_V_PULSE3_POSITION_A);
  1129. DUMP_REG(DC_DISP_M0_CONTROL);
  1130. DUMP_REG(DC_DISP_M1_CONTROL);
  1131. DUMP_REG(DC_DISP_DI_CONTROL);
  1132. DUMP_REG(DC_DISP_PP_CONTROL);
  1133. DUMP_REG(DC_DISP_PP_SELECT_A);
  1134. DUMP_REG(DC_DISP_PP_SELECT_B);
  1135. DUMP_REG(DC_DISP_PP_SELECT_C);
  1136. DUMP_REG(DC_DISP_PP_SELECT_D);
  1137. DUMP_REG(DC_DISP_DISP_CLOCK_CONTROL);
  1138. DUMP_REG(DC_DISP_DISP_INTERFACE_CONTROL);
  1139. DUMP_REG(DC_DISP_DISP_COLOR_CONTROL);
  1140. DUMP_REG(DC_DISP_SHIFT_CLOCK_OPTIONS);
  1141. DUMP_REG(DC_DISP_DATA_ENABLE_OPTIONS);
  1142. DUMP_REG(DC_DISP_SERIAL_INTERFACE_OPTIONS);
  1143. DUMP_REG(DC_DISP_LCD_SPI_OPTIONS);
  1144. DUMP_REG(DC_DISP_BORDER_COLOR);
  1145. DUMP_REG(DC_DISP_COLOR_KEY0_LOWER);
  1146. DUMP_REG(DC_DISP_COLOR_KEY0_UPPER);
  1147. DUMP_REG(DC_DISP_COLOR_KEY1_LOWER);
  1148. DUMP_REG(DC_DISP_COLOR_KEY1_UPPER);
  1149. DUMP_REG(DC_DISP_CURSOR_FOREGROUND);
  1150. DUMP_REG(DC_DISP_CURSOR_BACKGROUND);
  1151. DUMP_REG(DC_DISP_CURSOR_START_ADDR);
  1152. DUMP_REG(DC_DISP_CURSOR_START_ADDR_NS);
  1153. DUMP_REG(DC_DISP_CURSOR_POSITION);
  1154. DUMP_REG(DC_DISP_CURSOR_POSITION_NS);
  1155. DUMP_REG(DC_DISP_INIT_SEQ_CONTROL);
  1156. DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_A);
  1157. DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_B);
  1158. DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_C);
  1159. DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_D);
  1160. DUMP_REG(DC_DISP_DC_MCCIF_FIFOCTRL);
  1161. DUMP_REG(DC_DISP_MCCIF_DISPLAY0A_HYST);
  1162. DUMP_REG(DC_DISP_MCCIF_DISPLAY0B_HYST);
  1163. DUMP_REG(DC_DISP_MCCIF_DISPLAY1A_HYST);
  1164. DUMP_REG(DC_DISP_MCCIF_DISPLAY1B_HYST);
  1165. DUMP_REG(DC_DISP_DAC_CRT_CTRL);
  1166. DUMP_REG(DC_DISP_DISP_MISC_CONTROL);
  1167. DUMP_REG(DC_DISP_SD_CONTROL);
  1168. DUMP_REG(DC_DISP_SD_CSC_COEFF);
  1169. DUMP_REG(DC_DISP_SD_LUT(0));
  1170. DUMP_REG(DC_DISP_SD_LUT(1));
  1171. DUMP_REG(DC_DISP_SD_LUT(2));
  1172. DUMP_REG(DC_DISP_SD_LUT(3));
  1173. DUMP_REG(DC_DISP_SD_LUT(4));
  1174. DUMP_REG(DC_DISP_SD_LUT(5));
  1175. DUMP_REG(DC_DISP_SD_LUT(6));
  1176. DUMP_REG(DC_DISP_SD_LUT(7));
  1177. DUMP_REG(DC_DISP_SD_LUT(8));
  1178. DUMP_REG(DC_DISP_SD_FLICKER_CONTROL);
  1179. DUMP_REG(DC_DISP_DC_PIXEL_COUNT);
  1180. DUMP_REG(DC_DISP_SD_HISTOGRAM(0));
  1181. DUMP_REG(DC_DISP_SD_HISTOGRAM(1));
  1182. DUMP_REG(DC_DISP_SD_HISTOGRAM(2));
  1183. DUMP_REG(DC_DISP_SD_HISTOGRAM(3));
  1184. DUMP_REG(DC_DISP_SD_HISTOGRAM(4));
  1185. DUMP_REG(DC_DISP_SD_HISTOGRAM(5));
  1186. DUMP_REG(DC_DISP_SD_HISTOGRAM(6));
  1187. DUMP_REG(DC_DISP_SD_HISTOGRAM(7));
  1188. DUMP_REG(DC_DISP_SD_BL_TF(0));
  1189. DUMP_REG(DC_DISP_SD_BL_TF(1));
  1190. DUMP_REG(DC_DISP_SD_BL_TF(2));
  1191. DUMP_REG(DC_DISP_SD_BL_TF(3));
  1192. DUMP_REG(DC_DISP_SD_BL_CONTROL);
  1193. DUMP_REG(DC_DISP_SD_HW_K_VALUES);
  1194. DUMP_REG(DC_DISP_SD_MAN_K_VALUES);
  1195. DUMP_REG(DC_DISP_CURSOR_START_ADDR_HI);
  1196. DUMP_REG(DC_DISP_BLEND_CURSOR_CONTROL);
  1197. DUMP_REG(DC_WIN_WIN_OPTIONS);
  1198. DUMP_REG(DC_WIN_BYTE_SWAP);
  1199. DUMP_REG(DC_WIN_BUFFER_CONTROL);
  1200. DUMP_REG(DC_WIN_COLOR_DEPTH);
  1201. DUMP_REG(DC_WIN_POSITION);
  1202. DUMP_REG(DC_WIN_SIZE);
  1203. DUMP_REG(DC_WIN_PRESCALED_SIZE);
  1204. DUMP_REG(DC_WIN_H_INITIAL_DDA);
  1205. DUMP_REG(DC_WIN_V_INITIAL_DDA);
  1206. DUMP_REG(DC_WIN_DDA_INC);
  1207. DUMP_REG(DC_WIN_LINE_STRIDE);
  1208. DUMP_REG(DC_WIN_BUF_STRIDE);
  1209. DUMP_REG(DC_WIN_UV_BUF_STRIDE);
  1210. DUMP_REG(DC_WIN_BUFFER_ADDR_MODE);
  1211. DUMP_REG(DC_WIN_DV_CONTROL);
  1212. DUMP_REG(DC_WIN_BLEND_NOKEY);
  1213. DUMP_REG(DC_WIN_BLEND_1WIN);
  1214. DUMP_REG(DC_WIN_BLEND_2WIN_X);
  1215. DUMP_REG(DC_WIN_BLEND_2WIN_Y);
  1216. DUMP_REG(DC_WIN_BLEND_3WIN_XY);
  1217. DUMP_REG(DC_WIN_HP_FETCH_CONTROL);
  1218. DUMP_REG(DC_WINBUF_START_ADDR);
  1219. DUMP_REG(DC_WINBUF_START_ADDR_NS);
  1220. DUMP_REG(DC_WINBUF_START_ADDR_U);
  1221. DUMP_REG(DC_WINBUF_START_ADDR_U_NS);
  1222. DUMP_REG(DC_WINBUF_START_ADDR_V);
  1223. DUMP_REG(DC_WINBUF_START_ADDR_V_NS);
  1224. DUMP_REG(DC_WINBUF_ADDR_H_OFFSET);
  1225. DUMP_REG(DC_WINBUF_ADDR_H_OFFSET_NS);
  1226. DUMP_REG(DC_WINBUF_ADDR_V_OFFSET);
  1227. DUMP_REG(DC_WINBUF_ADDR_V_OFFSET_NS);
  1228. DUMP_REG(DC_WINBUF_UFLOW_STATUS);
  1229. DUMP_REG(DC_WINBUF_AD_UFLOW_STATUS);
  1230. DUMP_REG(DC_WINBUF_BD_UFLOW_STATUS);
  1231. DUMP_REG(DC_WINBUF_CD_UFLOW_STATUS);
  1232. #undef DUMP_REG
  1233. return 0;
  1234. }
  1235. static struct drm_info_list debugfs_files[] = {
  1236. { "regs", tegra_dc_show_regs, 0, NULL },
  1237. };
  1238. static int tegra_dc_debugfs_init(struct tegra_dc *dc, struct drm_minor *minor)
  1239. {
  1240. unsigned int i;
  1241. char *name;
  1242. int err;
  1243. name = kasprintf(GFP_KERNEL, "dc.%d", dc->pipe);
  1244. dc->debugfs = debugfs_create_dir(name, minor->debugfs_root);
  1245. kfree(name);
  1246. if (!dc->debugfs)
  1247. return -ENOMEM;
  1248. dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
  1249. GFP_KERNEL);
  1250. if (!dc->debugfs_files) {
  1251. err = -ENOMEM;
  1252. goto remove;
  1253. }
  1254. for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
  1255. dc->debugfs_files[i].data = dc;
  1256. err = drm_debugfs_create_files(dc->debugfs_files,
  1257. ARRAY_SIZE(debugfs_files),
  1258. dc->debugfs, minor);
  1259. if (err < 0)
  1260. goto free;
  1261. dc->minor = minor;
  1262. return 0;
  1263. free:
  1264. kfree(dc->debugfs_files);
  1265. dc->debugfs_files = NULL;
  1266. remove:
  1267. debugfs_remove(dc->debugfs);
  1268. dc->debugfs = NULL;
  1269. return err;
  1270. }
  1271. static int tegra_dc_debugfs_exit(struct tegra_dc *dc)
  1272. {
  1273. drm_debugfs_remove_files(dc->debugfs_files, ARRAY_SIZE(debugfs_files),
  1274. dc->minor);
  1275. dc->minor = NULL;
  1276. kfree(dc->debugfs_files);
  1277. dc->debugfs_files = NULL;
  1278. debugfs_remove(dc->debugfs);
  1279. dc->debugfs = NULL;
  1280. return 0;
  1281. }
  1282. static int tegra_dc_init(struct host1x_client *client)
  1283. {
  1284. struct drm_device *drm = dev_get_drvdata(client->parent);
  1285. struct tegra_dc *dc = host1x_client_to_dc(client);
  1286. struct tegra_drm *tegra = drm->dev_private;
  1287. struct drm_plane *primary = NULL;
  1288. struct drm_plane *cursor = NULL;
  1289. int err;
  1290. if (tegra->domain) {
  1291. err = iommu_attach_device(tegra->domain, dc->dev);
  1292. if (err < 0) {
  1293. dev_err(dc->dev, "failed to attach to domain: %d\n",
  1294. err);
  1295. return err;
  1296. }
  1297. dc->domain = tegra->domain;
  1298. }
  1299. primary = tegra_dc_primary_plane_create(drm, dc);
  1300. if (IS_ERR(primary)) {
  1301. err = PTR_ERR(primary);
  1302. goto cleanup;
  1303. }
  1304. if (dc->soc->supports_cursor) {
  1305. cursor = tegra_dc_cursor_plane_create(drm, dc);
  1306. if (IS_ERR(cursor)) {
  1307. err = PTR_ERR(cursor);
  1308. goto cleanup;
  1309. }
  1310. }
  1311. err = drm_crtc_init_with_planes(drm, &dc->base, primary, cursor,
  1312. &tegra_crtc_funcs);
  1313. if (err < 0)
  1314. goto cleanup;
  1315. drm_mode_crtc_set_gamma_size(&dc->base, 256);
  1316. drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
  1317. /*
  1318. * Keep track of the minimum pitch alignment across all display
  1319. * controllers.
  1320. */
  1321. if (dc->soc->pitch_align > tegra->pitch_align)
  1322. tegra->pitch_align = dc->soc->pitch_align;
  1323. err = tegra_dc_rgb_init(drm, dc);
  1324. if (err < 0 && err != -ENODEV) {
  1325. dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
  1326. goto cleanup;
  1327. }
  1328. err = tegra_dc_add_planes(drm, dc);
  1329. if (err < 0)
  1330. goto cleanup;
  1331. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  1332. err = tegra_dc_debugfs_init(dc, drm->primary);
  1333. if (err < 0)
  1334. dev_err(dc->dev, "debugfs setup failed: %d\n", err);
  1335. }
  1336. err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
  1337. dev_name(dc->dev), dc);
  1338. if (err < 0) {
  1339. dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
  1340. err);
  1341. goto cleanup;
  1342. }
  1343. return 0;
  1344. cleanup:
  1345. if (cursor)
  1346. drm_plane_cleanup(cursor);
  1347. if (primary)
  1348. drm_plane_cleanup(primary);
  1349. if (tegra->domain) {
  1350. iommu_detach_device(tegra->domain, dc->dev);
  1351. dc->domain = NULL;
  1352. }
  1353. return err;
  1354. }
  1355. static int tegra_dc_exit(struct host1x_client *client)
  1356. {
  1357. struct tegra_dc *dc = host1x_client_to_dc(client);
  1358. int err;
  1359. devm_free_irq(dc->dev, dc->irq, dc);
  1360. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  1361. err = tegra_dc_debugfs_exit(dc);
  1362. if (err < 0)
  1363. dev_err(dc->dev, "debugfs cleanup failed: %d\n", err);
  1364. }
  1365. err = tegra_dc_rgb_exit(dc);
  1366. if (err) {
  1367. dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
  1368. return err;
  1369. }
  1370. if (dc->domain) {
  1371. iommu_detach_device(dc->domain, dc->dev);
  1372. dc->domain = NULL;
  1373. }
  1374. return 0;
  1375. }
  1376. static const struct host1x_client_ops dc_client_ops = {
  1377. .init = tegra_dc_init,
  1378. .exit = tegra_dc_exit,
  1379. };
  1380. static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
  1381. .supports_border_color = true,
  1382. .supports_interlacing = false,
  1383. .supports_cursor = false,
  1384. .supports_block_linear = false,
  1385. .pitch_align = 8,
  1386. .has_powergate = false,
  1387. };
  1388. static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
  1389. .supports_border_color = true,
  1390. .supports_interlacing = false,
  1391. .supports_cursor = false,
  1392. .supports_block_linear = false,
  1393. .pitch_align = 8,
  1394. .has_powergate = false,
  1395. };
  1396. static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
  1397. .supports_border_color = true,
  1398. .supports_interlacing = false,
  1399. .supports_cursor = false,
  1400. .supports_block_linear = false,
  1401. .pitch_align = 64,
  1402. .has_powergate = true,
  1403. };
  1404. static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
  1405. .supports_border_color = false,
  1406. .supports_interlacing = true,
  1407. .supports_cursor = true,
  1408. .supports_block_linear = true,
  1409. .pitch_align = 64,
  1410. .has_powergate = true,
  1411. };
  1412. static const struct of_device_id tegra_dc_of_match[] = {
  1413. {
  1414. .compatible = "nvidia,tegra124-dc",
  1415. .data = &tegra124_dc_soc_info,
  1416. }, {
  1417. .compatible = "nvidia,tegra114-dc",
  1418. .data = &tegra114_dc_soc_info,
  1419. }, {
  1420. .compatible = "nvidia,tegra30-dc",
  1421. .data = &tegra30_dc_soc_info,
  1422. }, {
  1423. .compatible = "nvidia,tegra20-dc",
  1424. .data = &tegra20_dc_soc_info,
  1425. }, {
  1426. /* sentinel */
  1427. }
  1428. };
  1429. MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
  1430. static int tegra_dc_parse_dt(struct tegra_dc *dc)
  1431. {
  1432. struct device_node *np;
  1433. u32 value = 0;
  1434. int err;
  1435. err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
  1436. if (err < 0) {
  1437. dev_err(dc->dev, "missing \"nvidia,head\" property\n");
  1438. /*
  1439. * If the nvidia,head property isn't present, try to find the
  1440. * correct head number by looking up the position of this
  1441. * display controller's node within the device tree. Assuming
  1442. * that the nodes are ordered properly in the DTS file and
  1443. * that the translation into a flattened device tree blob
  1444. * preserves that ordering this will actually yield the right
  1445. * head number.
  1446. *
  1447. * If those assumptions don't hold, this will still work for
  1448. * cases where only a single display controller is used.
  1449. */
  1450. for_each_matching_node(np, tegra_dc_of_match) {
  1451. if (np == dc->dev->of_node)
  1452. break;
  1453. value++;
  1454. }
  1455. }
  1456. dc->pipe = value;
  1457. return 0;
  1458. }
  1459. static int tegra_dc_probe(struct platform_device *pdev)
  1460. {
  1461. const struct of_device_id *id;
  1462. struct resource *regs;
  1463. struct tegra_dc *dc;
  1464. int err;
  1465. dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
  1466. if (!dc)
  1467. return -ENOMEM;
  1468. id = of_match_node(tegra_dc_of_match, pdev->dev.of_node);
  1469. if (!id)
  1470. return -ENODEV;
  1471. spin_lock_init(&dc->lock);
  1472. INIT_LIST_HEAD(&dc->list);
  1473. dc->dev = &pdev->dev;
  1474. dc->soc = id->data;
  1475. err = tegra_dc_parse_dt(dc);
  1476. if (err < 0)
  1477. return err;
  1478. dc->clk = devm_clk_get(&pdev->dev, NULL);
  1479. if (IS_ERR(dc->clk)) {
  1480. dev_err(&pdev->dev, "failed to get clock\n");
  1481. return PTR_ERR(dc->clk);
  1482. }
  1483. dc->rst = devm_reset_control_get(&pdev->dev, "dc");
  1484. if (IS_ERR(dc->rst)) {
  1485. dev_err(&pdev->dev, "failed to get reset\n");
  1486. return PTR_ERR(dc->rst);
  1487. }
  1488. if (dc->soc->has_powergate) {
  1489. if (dc->pipe == 0)
  1490. dc->powergate = TEGRA_POWERGATE_DIS;
  1491. else
  1492. dc->powergate = TEGRA_POWERGATE_DISB;
  1493. err = tegra_powergate_sequence_power_up(dc->powergate, dc->clk,
  1494. dc->rst);
  1495. if (err < 0) {
  1496. dev_err(&pdev->dev, "failed to power partition: %d\n",
  1497. err);
  1498. return err;
  1499. }
  1500. } else {
  1501. err = clk_prepare_enable(dc->clk);
  1502. if (err < 0) {
  1503. dev_err(&pdev->dev, "failed to enable clock: %d\n",
  1504. err);
  1505. return err;
  1506. }
  1507. err = reset_control_deassert(dc->rst);
  1508. if (err < 0) {
  1509. dev_err(&pdev->dev, "failed to deassert reset: %d\n",
  1510. err);
  1511. return err;
  1512. }
  1513. }
  1514. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1515. dc->regs = devm_ioremap_resource(&pdev->dev, regs);
  1516. if (IS_ERR(dc->regs))
  1517. return PTR_ERR(dc->regs);
  1518. dc->irq = platform_get_irq(pdev, 0);
  1519. if (dc->irq < 0) {
  1520. dev_err(&pdev->dev, "failed to get IRQ\n");
  1521. return -ENXIO;
  1522. }
  1523. INIT_LIST_HEAD(&dc->client.list);
  1524. dc->client.ops = &dc_client_ops;
  1525. dc->client.dev = &pdev->dev;
  1526. err = tegra_dc_rgb_probe(dc);
  1527. if (err < 0 && err != -ENODEV) {
  1528. dev_err(&pdev->dev, "failed to probe RGB output: %d\n", err);
  1529. return err;
  1530. }
  1531. err = host1x_client_register(&dc->client);
  1532. if (err < 0) {
  1533. dev_err(&pdev->dev, "failed to register host1x client: %d\n",
  1534. err);
  1535. return err;
  1536. }
  1537. platform_set_drvdata(pdev, dc);
  1538. return 0;
  1539. }
  1540. static int tegra_dc_remove(struct platform_device *pdev)
  1541. {
  1542. struct tegra_dc *dc = platform_get_drvdata(pdev);
  1543. int err;
  1544. err = host1x_client_unregister(&dc->client);
  1545. if (err < 0) {
  1546. dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
  1547. err);
  1548. return err;
  1549. }
  1550. err = tegra_dc_rgb_remove(dc);
  1551. if (err < 0) {
  1552. dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
  1553. return err;
  1554. }
  1555. reset_control_assert(dc->rst);
  1556. if (dc->soc->has_powergate)
  1557. tegra_powergate_power_off(dc->powergate);
  1558. clk_disable_unprepare(dc->clk);
  1559. return 0;
  1560. }
  1561. struct platform_driver tegra_dc_driver = {
  1562. .driver = {
  1563. .name = "tegra-dc",
  1564. .owner = THIS_MODULE,
  1565. .of_match_table = tegra_dc_of_match,
  1566. },
  1567. .probe = tegra_dc_probe,
  1568. .remove = tegra_dc_remove,
  1569. };