dc.c 58 KB

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