dc.c 55 KB

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