dc.c 55 KB

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