dc.c 52 KB

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