dc.c 63 KB

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