dc.c 54 KB

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