dc.c 52 KB

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