dc.c 45 KB

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