dc.c 52 KB

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