dc.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  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/reset.h>
  12. #include "dc.h"
  13. #include "drm.h"
  14. #include "gem.h"
  15. struct tegra_dc_soc_info {
  16. bool supports_interlacing;
  17. bool supports_cursor;
  18. bool supports_block_linear;
  19. unsigned int pitch_align;
  20. };
  21. struct tegra_plane {
  22. struct drm_plane base;
  23. unsigned int index;
  24. };
  25. static inline struct tegra_plane *to_tegra_plane(struct drm_plane *plane)
  26. {
  27. return container_of(plane, struct tegra_plane, base);
  28. }
  29. static unsigned int tegra_dc_format(uint32_t format, uint32_t *swap)
  30. {
  31. /* assume no swapping of fetched data */
  32. if (swap)
  33. *swap = BYTE_SWAP_NOSWAP;
  34. switch (format) {
  35. case DRM_FORMAT_XBGR8888:
  36. return WIN_COLOR_DEPTH_R8G8B8A8;
  37. case DRM_FORMAT_XRGB8888:
  38. return WIN_COLOR_DEPTH_B8G8R8A8;
  39. case DRM_FORMAT_RGB565:
  40. return WIN_COLOR_DEPTH_B5G6R5;
  41. case DRM_FORMAT_UYVY:
  42. return WIN_COLOR_DEPTH_YCbCr422;
  43. case DRM_FORMAT_YUYV:
  44. if (swap)
  45. *swap = BYTE_SWAP_SWAP2;
  46. return WIN_COLOR_DEPTH_YCbCr422;
  47. case DRM_FORMAT_YUV420:
  48. return WIN_COLOR_DEPTH_YCbCr420P;
  49. case DRM_FORMAT_YUV422:
  50. return WIN_COLOR_DEPTH_YCbCr422P;
  51. default:
  52. break;
  53. }
  54. WARN(1, "unsupported pixel format %u, using default\n", format);
  55. return WIN_COLOR_DEPTH_B8G8R8A8;
  56. }
  57. static bool tegra_dc_format_is_yuv(unsigned int format, bool *planar)
  58. {
  59. switch (format) {
  60. case WIN_COLOR_DEPTH_YCbCr422:
  61. case WIN_COLOR_DEPTH_YUV422:
  62. if (planar)
  63. *planar = false;
  64. return true;
  65. case WIN_COLOR_DEPTH_YCbCr420P:
  66. case WIN_COLOR_DEPTH_YUV420P:
  67. case WIN_COLOR_DEPTH_YCbCr422P:
  68. case WIN_COLOR_DEPTH_YUV422P:
  69. case WIN_COLOR_DEPTH_YCbCr422R:
  70. case WIN_COLOR_DEPTH_YUV422R:
  71. case WIN_COLOR_DEPTH_YCbCr422RA:
  72. case WIN_COLOR_DEPTH_YUV422RA:
  73. if (planar)
  74. *planar = true;
  75. return true;
  76. }
  77. return false;
  78. }
  79. static inline u32 compute_dda_inc(unsigned int in, unsigned int out, bool v,
  80. unsigned int bpp)
  81. {
  82. fixed20_12 outf = dfixed_init(out);
  83. fixed20_12 inf = dfixed_init(in);
  84. u32 dda_inc;
  85. int max;
  86. if (v)
  87. max = 15;
  88. else {
  89. switch (bpp) {
  90. case 2:
  91. max = 8;
  92. break;
  93. default:
  94. WARN_ON_ONCE(1);
  95. /* fallthrough */
  96. case 4:
  97. max = 4;
  98. break;
  99. }
  100. }
  101. outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
  102. inf.full -= dfixed_const(1);
  103. dda_inc = dfixed_div(inf, outf);
  104. dda_inc = min_t(u32, dda_inc, dfixed_const(max));
  105. return dda_inc;
  106. }
  107. static inline u32 compute_initial_dda(unsigned int in)
  108. {
  109. fixed20_12 inf = dfixed_init(in);
  110. return dfixed_frac(inf);
  111. }
  112. static int tegra_dc_setup_window(struct tegra_dc *dc, unsigned int index,
  113. const struct tegra_dc_window *window)
  114. {
  115. unsigned h_offset, v_offset, h_size, v_size, h_dda, v_dda, bpp;
  116. unsigned long value;
  117. bool yuv, planar;
  118. /*
  119. * For YUV planar modes, the number of bytes per pixel takes into
  120. * account only the luma component and therefore is 1.
  121. */
  122. yuv = tegra_dc_format_is_yuv(window->format, &planar);
  123. if (!yuv)
  124. bpp = window->bits_per_pixel / 8;
  125. else
  126. bpp = planar ? 1 : 2;
  127. value = WINDOW_A_SELECT << index;
  128. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
  129. tegra_dc_writel(dc, window->format, DC_WIN_COLOR_DEPTH);
  130. tegra_dc_writel(dc, window->swap, DC_WIN_BYTE_SWAP);
  131. value = V_POSITION(window->dst.y) | H_POSITION(window->dst.x);
  132. tegra_dc_writel(dc, value, DC_WIN_POSITION);
  133. value = V_SIZE(window->dst.h) | H_SIZE(window->dst.w);
  134. tegra_dc_writel(dc, value, DC_WIN_SIZE);
  135. h_offset = window->src.x * bpp;
  136. v_offset = window->src.y;
  137. h_size = window->src.w * bpp;
  138. v_size = window->src.h;
  139. value = V_PRESCALED_SIZE(v_size) | H_PRESCALED_SIZE(h_size);
  140. tegra_dc_writel(dc, value, DC_WIN_PRESCALED_SIZE);
  141. /*
  142. * For DDA computations the number of bytes per pixel for YUV planar
  143. * modes needs to take into account all Y, U and V components.
  144. */
  145. if (yuv && planar)
  146. bpp = 2;
  147. h_dda = compute_dda_inc(window->src.w, window->dst.w, false, bpp);
  148. v_dda = compute_dda_inc(window->src.h, window->dst.h, true, bpp);
  149. value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
  150. tegra_dc_writel(dc, value, DC_WIN_DDA_INC);
  151. h_dda = compute_initial_dda(window->src.x);
  152. v_dda = compute_initial_dda(window->src.y);
  153. tegra_dc_writel(dc, h_dda, DC_WIN_H_INITIAL_DDA);
  154. tegra_dc_writel(dc, v_dda, DC_WIN_V_INITIAL_DDA);
  155. tegra_dc_writel(dc, 0, DC_WIN_UV_BUF_STRIDE);
  156. tegra_dc_writel(dc, 0, DC_WIN_BUF_STRIDE);
  157. tegra_dc_writel(dc, window->base[0], DC_WINBUF_START_ADDR);
  158. if (yuv && planar) {
  159. tegra_dc_writel(dc, window->base[1], DC_WINBUF_START_ADDR_U);
  160. tegra_dc_writel(dc, window->base[2], DC_WINBUF_START_ADDR_V);
  161. value = window->stride[1] << 16 | window->stride[0];
  162. tegra_dc_writel(dc, value, DC_WIN_LINE_STRIDE);
  163. } else {
  164. tegra_dc_writel(dc, window->stride[0], DC_WIN_LINE_STRIDE);
  165. }
  166. if (window->bottom_up)
  167. v_offset += window->src.h - 1;
  168. tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
  169. tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
  170. if (dc->soc->supports_block_linear) {
  171. unsigned long height = window->tiling.value;
  172. switch (window->tiling.mode) {
  173. case TEGRA_BO_TILING_MODE_PITCH:
  174. value = DC_WINBUF_SURFACE_KIND_PITCH;
  175. break;
  176. case TEGRA_BO_TILING_MODE_TILED:
  177. value = DC_WINBUF_SURFACE_KIND_TILED;
  178. break;
  179. case TEGRA_BO_TILING_MODE_BLOCK:
  180. value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
  181. DC_WINBUF_SURFACE_KIND_BLOCK;
  182. break;
  183. }
  184. tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
  185. } else {
  186. switch (window->tiling.mode) {
  187. case TEGRA_BO_TILING_MODE_PITCH:
  188. value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
  189. DC_WIN_BUFFER_ADDR_MODE_LINEAR;
  190. break;
  191. case TEGRA_BO_TILING_MODE_TILED:
  192. value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
  193. DC_WIN_BUFFER_ADDR_MODE_TILE;
  194. break;
  195. case TEGRA_BO_TILING_MODE_BLOCK:
  196. DRM_ERROR("hardware doesn't support block linear mode\n");
  197. return -EINVAL;
  198. }
  199. tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
  200. }
  201. value = WIN_ENABLE;
  202. if (yuv) {
  203. /* setup default colorspace conversion coefficients */
  204. tegra_dc_writel(dc, 0x00f0, DC_WIN_CSC_YOF);
  205. tegra_dc_writel(dc, 0x012a, DC_WIN_CSC_KYRGB);
  206. tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KUR);
  207. tegra_dc_writel(dc, 0x0198, DC_WIN_CSC_KVR);
  208. tegra_dc_writel(dc, 0x039b, DC_WIN_CSC_KUG);
  209. tegra_dc_writel(dc, 0x032f, DC_WIN_CSC_KVG);
  210. tegra_dc_writel(dc, 0x0204, DC_WIN_CSC_KUB);
  211. tegra_dc_writel(dc, 0x0000, DC_WIN_CSC_KVB);
  212. value |= CSC_ENABLE;
  213. } else if (window->bits_per_pixel < 24) {
  214. value |= COLOR_EXPAND;
  215. }
  216. if (window->bottom_up)
  217. value |= V_DIRECTION;
  218. tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
  219. /*
  220. * Disable blending and assume Window A is the bottom-most window,
  221. * Window C is the top-most window and Window B is in the middle.
  222. */
  223. tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_NOKEY);
  224. tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_1WIN);
  225. switch (index) {
  226. case 0:
  227. tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_X);
  228. tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
  229. tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
  230. break;
  231. case 1:
  232. tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
  233. tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_2WIN_Y);
  234. tegra_dc_writel(dc, 0x000000, DC_WIN_BLEND_3WIN_XY);
  235. break;
  236. case 2:
  237. tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_X);
  238. tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_2WIN_Y);
  239. tegra_dc_writel(dc, 0xffff00, DC_WIN_BLEND_3WIN_XY);
  240. break;
  241. }
  242. tegra_dc_writel(dc, WIN_A_UPDATE << index, DC_CMD_STATE_CONTROL);
  243. tegra_dc_writel(dc, WIN_A_ACT_REQ << index, DC_CMD_STATE_CONTROL);
  244. return 0;
  245. }
  246. static int tegra_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
  247. struct drm_framebuffer *fb, int crtc_x,
  248. int crtc_y, unsigned int crtc_w,
  249. unsigned int crtc_h, uint32_t src_x,
  250. uint32_t src_y, uint32_t src_w, uint32_t src_h)
  251. {
  252. struct tegra_plane *p = to_tegra_plane(plane);
  253. struct tegra_dc *dc = to_tegra_dc(crtc);
  254. struct tegra_dc_window window;
  255. unsigned int i;
  256. int err;
  257. memset(&window, 0, sizeof(window));
  258. window.src.x = src_x >> 16;
  259. window.src.y = src_y >> 16;
  260. window.src.w = src_w >> 16;
  261. window.src.h = src_h >> 16;
  262. window.dst.x = crtc_x;
  263. window.dst.y = crtc_y;
  264. window.dst.w = crtc_w;
  265. window.dst.h = crtc_h;
  266. window.format = tegra_dc_format(fb->pixel_format, &window.swap);
  267. window.bits_per_pixel = fb->bits_per_pixel;
  268. window.bottom_up = tegra_fb_is_bottom_up(fb);
  269. err = tegra_fb_get_tiling(fb, &window.tiling);
  270. if (err < 0)
  271. return err;
  272. for (i = 0; i < drm_format_num_planes(fb->pixel_format); i++) {
  273. struct tegra_bo *bo = tegra_fb_get_plane(fb, i);
  274. window.base[i] = bo->paddr + fb->offsets[i];
  275. /*
  276. * Tegra doesn't support different strides for U and V planes
  277. * so we display a warning if the user tries to display a
  278. * framebuffer with such a configuration.
  279. */
  280. if (i >= 2) {
  281. if (fb->pitches[i] != window.stride[1])
  282. DRM_ERROR("unsupported UV-plane configuration\n");
  283. } else {
  284. window.stride[i] = fb->pitches[i];
  285. }
  286. }
  287. return tegra_dc_setup_window(dc, p->index, &window);
  288. }
  289. static int tegra_plane_disable(struct drm_plane *plane)
  290. {
  291. struct tegra_dc *dc = to_tegra_dc(plane->crtc);
  292. struct tegra_plane *p = to_tegra_plane(plane);
  293. unsigned long value;
  294. if (!plane->crtc)
  295. return 0;
  296. value = WINDOW_A_SELECT << p->index;
  297. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_WINDOW_HEADER);
  298. value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
  299. value &= ~WIN_ENABLE;
  300. tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
  301. tegra_dc_writel(dc, WIN_A_UPDATE << p->index, DC_CMD_STATE_CONTROL);
  302. tegra_dc_writel(dc, WIN_A_ACT_REQ << p->index, DC_CMD_STATE_CONTROL);
  303. return 0;
  304. }
  305. static void tegra_plane_destroy(struct drm_plane *plane)
  306. {
  307. struct tegra_plane *p = to_tegra_plane(plane);
  308. tegra_plane_disable(plane);
  309. drm_plane_cleanup(plane);
  310. kfree(p);
  311. }
  312. static const struct drm_plane_funcs tegra_plane_funcs = {
  313. .update_plane = tegra_plane_update,
  314. .disable_plane = tegra_plane_disable,
  315. .destroy = tegra_plane_destroy,
  316. };
  317. static const uint32_t plane_formats[] = {
  318. DRM_FORMAT_XBGR8888,
  319. DRM_FORMAT_XRGB8888,
  320. DRM_FORMAT_RGB565,
  321. DRM_FORMAT_UYVY,
  322. DRM_FORMAT_YUYV,
  323. DRM_FORMAT_YUV420,
  324. DRM_FORMAT_YUV422,
  325. };
  326. static int tegra_dc_add_planes(struct drm_device *drm, struct tegra_dc *dc)
  327. {
  328. unsigned int i;
  329. int err = 0;
  330. for (i = 0; i < 2; i++) {
  331. struct tegra_plane *plane;
  332. plane = kzalloc(sizeof(*plane), GFP_KERNEL);
  333. if (!plane)
  334. return -ENOMEM;
  335. plane->index = 1 + i;
  336. err = drm_plane_init(drm, &plane->base, 1 << dc->pipe,
  337. &tegra_plane_funcs, plane_formats,
  338. ARRAY_SIZE(plane_formats), false);
  339. if (err < 0) {
  340. kfree(plane);
  341. return err;
  342. }
  343. }
  344. return 0;
  345. }
  346. static int tegra_dc_set_base(struct tegra_dc *dc, int x, int y,
  347. struct drm_framebuffer *fb)
  348. {
  349. struct tegra_bo *bo = tegra_fb_get_plane(fb, 0);
  350. unsigned int h_offset = 0, v_offset = 0;
  351. struct tegra_bo_tiling tiling;
  352. unsigned int format, swap;
  353. unsigned long value;
  354. int err;
  355. err = tegra_fb_get_tiling(fb, &tiling);
  356. if (err < 0)
  357. return err;
  358. tegra_dc_writel(dc, WINDOW_A_SELECT, DC_CMD_DISPLAY_WINDOW_HEADER);
  359. value = fb->offsets[0] + y * fb->pitches[0] +
  360. x * fb->bits_per_pixel / 8;
  361. tegra_dc_writel(dc, bo->paddr + value, DC_WINBUF_START_ADDR);
  362. tegra_dc_writel(dc, fb->pitches[0], DC_WIN_LINE_STRIDE);
  363. format = tegra_dc_format(fb->pixel_format, &swap);
  364. tegra_dc_writel(dc, format, DC_WIN_COLOR_DEPTH);
  365. tegra_dc_writel(dc, swap, DC_WIN_BYTE_SWAP);
  366. if (dc->soc->supports_block_linear) {
  367. unsigned long height = tiling.value;
  368. switch (tiling.mode) {
  369. case TEGRA_BO_TILING_MODE_PITCH:
  370. value = DC_WINBUF_SURFACE_KIND_PITCH;
  371. break;
  372. case TEGRA_BO_TILING_MODE_TILED:
  373. value = DC_WINBUF_SURFACE_KIND_TILED;
  374. break;
  375. case TEGRA_BO_TILING_MODE_BLOCK:
  376. value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
  377. DC_WINBUF_SURFACE_KIND_BLOCK;
  378. break;
  379. }
  380. tegra_dc_writel(dc, value, DC_WINBUF_SURFACE_KIND);
  381. } else {
  382. switch (tiling.mode) {
  383. case TEGRA_BO_TILING_MODE_PITCH:
  384. value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
  385. DC_WIN_BUFFER_ADDR_MODE_LINEAR;
  386. break;
  387. case TEGRA_BO_TILING_MODE_TILED:
  388. value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
  389. DC_WIN_BUFFER_ADDR_MODE_TILE;
  390. break;
  391. case TEGRA_BO_TILING_MODE_BLOCK:
  392. DRM_ERROR("hardware doesn't support block linear mode\n");
  393. return -EINVAL;
  394. }
  395. tegra_dc_writel(dc, value, DC_WIN_BUFFER_ADDR_MODE);
  396. }
  397. /* make sure bottom-up buffers are properly displayed */
  398. if (tegra_fb_is_bottom_up(fb)) {
  399. value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
  400. value |= V_DIRECTION;
  401. tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
  402. v_offset += fb->height - 1;
  403. } else {
  404. value = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
  405. value &= ~V_DIRECTION;
  406. tegra_dc_writel(dc, value, DC_WIN_WIN_OPTIONS);
  407. }
  408. tegra_dc_writel(dc, h_offset, DC_WINBUF_ADDR_H_OFFSET);
  409. tegra_dc_writel(dc, v_offset, DC_WINBUF_ADDR_V_OFFSET);
  410. value = GENERAL_UPDATE | WIN_A_UPDATE;
  411. tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
  412. value = GENERAL_ACT_REQ | WIN_A_ACT_REQ;
  413. tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
  414. return 0;
  415. }
  416. void tegra_dc_enable_vblank(struct tegra_dc *dc)
  417. {
  418. unsigned long value, flags;
  419. spin_lock_irqsave(&dc->lock, flags);
  420. value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
  421. value |= VBLANK_INT;
  422. tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
  423. spin_unlock_irqrestore(&dc->lock, flags);
  424. }
  425. void tegra_dc_disable_vblank(struct tegra_dc *dc)
  426. {
  427. unsigned long value, flags;
  428. spin_lock_irqsave(&dc->lock, flags);
  429. value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
  430. value &= ~VBLANK_INT;
  431. tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
  432. spin_unlock_irqrestore(&dc->lock, flags);
  433. }
  434. static int tegra_dc_cursor_set2(struct drm_crtc *crtc, struct drm_file *file,
  435. uint32_t handle, uint32_t width,
  436. uint32_t height, int32_t hot_x, int32_t hot_y)
  437. {
  438. unsigned long value = CURSOR_CLIP_DISPLAY;
  439. struct tegra_dc *dc = to_tegra_dc(crtc);
  440. struct drm_gem_object *gem;
  441. struct tegra_bo *bo = NULL;
  442. if (!dc->soc->supports_cursor)
  443. return -ENXIO;
  444. if (width != height)
  445. return -EINVAL;
  446. switch (width) {
  447. case 32:
  448. value |= CURSOR_SIZE_32x32;
  449. break;
  450. case 64:
  451. value |= CURSOR_SIZE_64x64;
  452. break;
  453. case 128:
  454. value |= CURSOR_SIZE_128x128;
  455. case 256:
  456. value |= CURSOR_SIZE_256x256;
  457. break;
  458. default:
  459. return -EINVAL;
  460. }
  461. if (handle) {
  462. gem = drm_gem_object_lookup(crtc->dev, file, handle);
  463. if (!gem)
  464. return -ENOENT;
  465. bo = to_tegra_bo(gem);
  466. }
  467. if (bo) {
  468. unsigned long addr = (bo->paddr & 0xfffffc00) >> 10;
  469. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  470. unsigned long high = (bo->paddr & 0xfffffffc) >> 32;
  471. #endif
  472. tegra_dc_writel(dc, value | addr, DC_DISP_CURSOR_START_ADDR);
  473. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  474. tegra_dc_writel(dc, high, DC_DISP_CURSOR_START_ADDR_HI);
  475. #endif
  476. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  477. value |= CURSOR_ENABLE;
  478. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  479. value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
  480. value &= ~CURSOR_DST_BLEND_MASK;
  481. value &= ~CURSOR_SRC_BLEND_MASK;
  482. value |= CURSOR_MODE_NORMAL;
  483. value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
  484. value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
  485. value |= CURSOR_ALPHA;
  486. tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
  487. } else {
  488. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  489. value &= ~CURSOR_ENABLE;
  490. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  491. }
  492. tegra_dc_writel(dc, CURSOR_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
  493. tegra_dc_writel(dc, CURSOR_ACT_REQ, DC_CMD_STATE_CONTROL);
  494. tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
  495. tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
  496. return 0;
  497. }
  498. static int tegra_dc_cursor_move(struct drm_crtc *crtc, int x, int y)
  499. {
  500. struct tegra_dc *dc = to_tegra_dc(crtc);
  501. unsigned long value;
  502. if (!dc->soc->supports_cursor)
  503. return -ENXIO;
  504. value = ((y & 0x3fff) << 16) | (x & 0x3fff);
  505. tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
  506. tegra_dc_writel(dc, CURSOR_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
  507. tegra_dc_writel(dc, CURSOR_ACT_REQ, DC_CMD_STATE_CONTROL);
  508. /* XXX: only required on generations earlier than Tegra124? */
  509. tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
  510. tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
  511. return 0;
  512. }
  513. static void tegra_dc_finish_page_flip(struct tegra_dc *dc)
  514. {
  515. struct drm_device *drm = dc->base.dev;
  516. struct drm_crtc *crtc = &dc->base;
  517. unsigned long flags, base;
  518. struct tegra_bo *bo;
  519. if (!dc->event)
  520. return;
  521. bo = tegra_fb_get_plane(crtc->primary->fb, 0);
  522. /* check if new start address has been latched */
  523. tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
  524. base = tegra_dc_readl(dc, DC_WINBUF_START_ADDR);
  525. tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
  526. if (base == bo->paddr + crtc->primary->fb->offsets[0]) {
  527. spin_lock_irqsave(&drm->event_lock, flags);
  528. drm_send_vblank_event(drm, dc->pipe, dc->event);
  529. drm_vblank_put(drm, dc->pipe);
  530. dc->event = NULL;
  531. spin_unlock_irqrestore(&drm->event_lock, flags);
  532. }
  533. }
  534. void tegra_dc_cancel_page_flip(struct drm_crtc *crtc, struct drm_file *file)
  535. {
  536. struct tegra_dc *dc = to_tegra_dc(crtc);
  537. struct drm_device *drm = crtc->dev;
  538. unsigned long flags;
  539. spin_lock_irqsave(&drm->event_lock, flags);
  540. if (dc->event && dc->event->base.file_priv == file) {
  541. dc->event->base.destroy(&dc->event->base);
  542. drm_vblank_put(drm, dc->pipe);
  543. dc->event = NULL;
  544. }
  545. spin_unlock_irqrestore(&drm->event_lock, flags);
  546. }
  547. static int tegra_dc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  548. struct drm_pending_vblank_event *event, uint32_t page_flip_flags)
  549. {
  550. struct tegra_dc *dc = to_tegra_dc(crtc);
  551. struct drm_device *drm = crtc->dev;
  552. if (dc->event)
  553. return -EBUSY;
  554. if (event) {
  555. event->pipe = dc->pipe;
  556. dc->event = event;
  557. drm_vblank_get(drm, dc->pipe);
  558. }
  559. tegra_dc_set_base(dc, 0, 0, fb);
  560. crtc->primary->fb = fb;
  561. return 0;
  562. }
  563. static void drm_crtc_clear(struct drm_crtc *crtc)
  564. {
  565. memset(crtc, 0, sizeof(*crtc));
  566. }
  567. static void tegra_dc_destroy(struct drm_crtc *crtc)
  568. {
  569. drm_crtc_cleanup(crtc);
  570. drm_crtc_clear(crtc);
  571. }
  572. static const struct drm_crtc_funcs tegra_crtc_funcs = {
  573. .cursor_set2 = tegra_dc_cursor_set2,
  574. .cursor_move = tegra_dc_cursor_move,
  575. .page_flip = tegra_dc_page_flip,
  576. .set_config = drm_crtc_helper_set_config,
  577. .destroy = tegra_dc_destroy,
  578. };
  579. static void tegra_crtc_disable(struct drm_crtc *crtc)
  580. {
  581. struct drm_device *drm = crtc->dev;
  582. struct drm_plane *plane;
  583. drm_for_each_legacy_plane(plane, &drm->mode_config.plane_list) {
  584. if (plane->crtc == crtc) {
  585. tegra_plane_disable(plane);
  586. plane->crtc = NULL;
  587. if (plane->fb) {
  588. drm_framebuffer_unreference(plane->fb);
  589. plane->fb = NULL;
  590. }
  591. }
  592. }
  593. drm_crtc_vblank_off(crtc);
  594. }
  595. static bool tegra_crtc_mode_fixup(struct drm_crtc *crtc,
  596. const struct drm_display_mode *mode,
  597. struct drm_display_mode *adjusted)
  598. {
  599. return true;
  600. }
  601. static int tegra_dc_set_timings(struct tegra_dc *dc,
  602. struct drm_display_mode *mode)
  603. {
  604. unsigned int h_ref_to_sync = 1;
  605. unsigned int v_ref_to_sync = 1;
  606. unsigned long value;
  607. tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
  608. value = (v_ref_to_sync << 16) | h_ref_to_sync;
  609. tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
  610. value = ((mode->vsync_end - mode->vsync_start) << 16) |
  611. ((mode->hsync_end - mode->hsync_start) << 0);
  612. tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
  613. value = ((mode->vtotal - mode->vsync_end) << 16) |
  614. ((mode->htotal - mode->hsync_end) << 0);
  615. tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
  616. value = ((mode->vsync_start - mode->vdisplay) << 16) |
  617. ((mode->hsync_start - mode->hdisplay) << 0);
  618. tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
  619. value = (mode->vdisplay << 16) | mode->hdisplay;
  620. tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
  621. return 0;
  622. }
  623. static int tegra_crtc_setup_clk(struct drm_crtc *crtc,
  624. struct drm_display_mode *mode)
  625. {
  626. unsigned long pclk = mode->clock * 1000;
  627. struct tegra_dc *dc = to_tegra_dc(crtc);
  628. struct tegra_output *output = NULL;
  629. struct drm_encoder *encoder;
  630. unsigned int div;
  631. u32 value;
  632. long err;
  633. list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list, head)
  634. if (encoder->crtc == crtc) {
  635. output = encoder_to_output(encoder);
  636. break;
  637. }
  638. if (!output)
  639. return -ENODEV;
  640. /*
  641. * This assumes that the parent clock is pll_d_out0 or pll_d2_out
  642. * respectively, each of which divides the base pll_d by 2.
  643. */
  644. err = tegra_output_setup_clock(output, dc->clk, pclk, &div);
  645. if (err < 0) {
  646. dev_err(dc->dev, "failed to setup clock: %ld\n", err);
  647. return err;
  648. }
  649. DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk), div);
  650. value = SHIFT_CLK_DIVIDER(div) | PIXEL_CLK_DIVIDER_PCD1;
  651. tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
  652. return 0;
  653. }
  654. static int tegra_crtc_mode_set(struct drm_crtc *crtc,
  655. struct drm_display_mode *mode,
  656. struct drm_display_mode *adjusted,
  657. int x, int y, struct drm_framebuffer *old_fb)
  658. {
  659. struct tegra_bo *bo = tegra_fb_get_plane(crtc->primary->fb, 0);
  660. struct tegra_dc *dc = to_tegra_dc(crtc);
  661. struct tegra_dc_window window;
  662. u32 value;
  663. int err;
  664. err = tegra_crtc_setup_clk(crtc, mode);
  665. if (err) {
  666. dev_err(dc->dev, "failed to setup clock for CRTC: %d\n", err);
  667. return err;
  668. }
  669. /* program display mode */
  670. tegra_dc_set_timings(dc, mode);
  671. /* interlacing isn't supported yet, so disable it */
  672. if (dc->soc->supports_interlacing) {
  673. value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
  674. value &= ~INTERLACE_ENABLE;
  675. tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
  676. }
  677. /* setup window parameters */
  678. memset(&window, 0, sizeof(window));
  679. window.src.x = 0;
  680. window.src.y = 0;
  681. window.src.w = mode->hdisplay;
  682. window.src.h = mode->vdisplay;
  683. window.dst.x = 0;
  684. window.dst.y = 0;
  685. window.dst.w = mode->hdisplay;
  686. window.dst.h = mode->vdisplay;
  687. window.format = tegra_dc_format(crtc->primary->fb->pixel_format,
  688. &window.swap);
  689. window.bits_per_pixel = crtc->primary->fb->bits_per_pixel;
  690. window.stride[0] = crtc->primary->fb->pitches[0];
  691. window.base[0] = bo->paddr;
  692. err = tegra_dc_setup_window(dc, 0, &window);
  693. if (err < 0)
  694. dev_err(dc->dev, "failed to enable root plane\n");
  695. return 0;
  696. }
  697. static int tegra_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  698. struct drm_framebuffer *old_fb)
  699. {
  700. struct tegra_dc *dc = to_tegra_dc(crtc);
  701. return tegra_dc_set_base(dc, x, y, crtc->primary->fb);
  702. }
  703. static void tegra_crtc_prepare(struct drm_crtc *crtc)
  704. {
  705. struct tegra_dc *dc = to_tegra_dc(crtc);
  706. unsigned int syncpt;
  707. unsigned long value;
  708. drm_crtc_vblank_off(crtc);
  709. /* hardware initialization */
  710. reset_control_deassert(dc->rst);
  711. usleep_range(10000, 20000);
  712. if (dc->pipe)
  713. syncpt = SYNCPT_VBLANK1;
  714. else
  715. syncpt = SYNCPT_VBLANK0;
  716. /* initialize display controller */
  717. tegra_dc_writel(dc, 0x00000100, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
  718. tegra_dc_writel(dc, 0x100 | syncpt, DC_CMD_CONT_SYNCPT_VSYNC);
  719. value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT | WIN_A_OF_INT;
  720. tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
  721. value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
  722. WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
  723. tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
  724. /* initialize timer */
  725. value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
  726. WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
  727. tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
  728. value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
  729. WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
  730. tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
  731. value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT;
  732. tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
  733. value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT;
  734. tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
  735. }
  736. static void tegra_crtc_commit(struct drm_crtc *crtc)
  737. {
  738. struct tegra_dc *dc = to_tegra_dc(crtc);
  739. unsigned long value;
  740. value = GENERAL_UPDATE | WIN_A_UPDATE;
  741. tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
  742. value = GENERAL_ACT_REQ | WIN_A_ACT_REQ;
  743. tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
  744. drm_crtc_vblank_on(crtc);
  745. }
  746. static void tegra_crtc_load_lut(struct drm_crtc *crtc)
  747. {
  748. }
  749. static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
  750. .disable = tegra_crtc_disable,
  751. .mode_fixup = tegra_crtc_mode_fixup,
  752. .mode_set = tegra_crtc_mode_set,
  753. .mode_set_base = tegra_crtc_mode_set_base,
  754. .prepare = tegra_crtc_prepare,
  755. .commit = tegra_crtc_commit,
  756. .load_lut = tegra_crtc_load_lut,
  757. };
  758. static irqreturn_t tegra_dc_irq(int irq, void *data)
  759. {
  760. struct tegra_dc *dc = data;
  761. unsigned long status;
  762. status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
  763. tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
  764. if (status & FRAME_END_INT) {
  765. /*
  766. dev_dbg(dc->dev, "%s(): frame end\n", __func__);
  767. */
  768. }
  769. if (status & VBLANK_INT) {
  770. /*
  771. dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
  772. */
  773. drm_handle_vblank(dc->base.dev, dc->pipe);
  774. tegra_dc_finish_page_flip(dc);
  775. }
  776. if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
  777. /*
  778. dev_dbg(dc->dev, "%s(): underflow\n", __func__);
  779. */
  780. }
  781. return IRQ_HANDLED;
  782. }
  783. static int tegra_dc_show_regs(struct seq_file *s, void *data)
  784. {
  785. struct drm_info_node *node = s->private;
  786. struct tegra_dc *dc = node->info_ent->data;
  787. #define DUMP_REG(name) \
  788. seq_printf(s, "%-40s %#05x %08lx\n", #name, name, \
  789. tegra_dc_readl(dc, name))
  790. DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT);
  791. DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
  792. DUMP_REG(DC_CMD_GENERAL_INCR_SYNCPT_ERROR);
  793. DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT);
  794. DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL);
  795. DUMP_REG(DC_CMD_WIN_A_INCR_SYNCPT_ERROR);
  796. DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT);
  797. DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL);
  798. DUMP_REG(DC_CMD_WIN_B_INCR_SYNCPT_ERROR);
  799. DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT);
  800. DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL);
  801. DUMP_REG(DC_CMD_WIN_C_INCR_SYNCPT_ERROR);
  802. DUMP_REG(DC_CMD_CONT_SYNCPT_VSYNC);
  803. DUMP_REG(DC_CMD_DISPLAY_COMMAND_OPTION0);
  804. DUMP_REG(DC_CMD_DISPLAY_COMMAND);
  805. DUMP_REG(DC_CMD_SIGNAL_RAISE);
  806. DUMP_REG(DC_CMD_DISPLAY_POWER_CONTROL);
  807. DUMP_REG(DC_CMD_INT_STATUS);
  808. DUMP_REG(DC_CMD_INT_MASK);
  809. DUMP_REG(DC_CMD_INT_ENABLE);
  810. DUMP_REG(DC_CMD_INT_TYPE);
  811. DUMP_REG(DC_CMD_INT_POLARITY);
  812. DUMP_REG(DC_CMD_SIGNAL_RAISE1);
  813. DUMP_REG(DC_CMD_SIGNAL_RAISE2);
  814. DUMP_REG(DC_CMD_SIGNAL_RAISE3);
  815. DUMP_REG(DC_CMD_STATE_ACCESS);
  816. DUMP_REG(DC_CMD_STATE_CONTROL);
  817. DUMP_REG(DC_CMD_DISPLAY_WINDOW_HEADER);
  818. DUMP_REG(DC_CMD_REG_ACT_CONTROL);
  819. DUMP_REG(DC_COM_CRC_CONTROL);
  820. DUMP_REG(DC_COM_CRC_CHECKSUM);
  821. DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(0));
  822. DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(1));
  823. DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(2));
  824. DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE(3));
  825. DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(0));
  826. DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(1));
  827. DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(2));
  828. DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY(3));
  829. DUMP_REG(DC_COM_PIN_OUTPUT_DATA(0));
  830. DUMP_REG(DC_COM_PIN_OUTPUT_DATA(1));
  831. DUMP_REG(DC_COM_PIN_OUTPUT_DATA(2));
  832. DUMP_REG(DC_COM_PIN_OUTPUT_DATA(3));
  833. DUMP_REG(DC_COM_PIN_INPUT_ENABLE(0));
  834. DUMP_REG(DC_COM_PIN_INPUT_ENABLE(1));
  835. DUMP_REG(DC_COM_PIN_INPUT_ENABLE(2));
  836. DUMP_REG(DC_COM_PIN_INPUT_ENABLE(3));
  837. DUMP_REG(DC_COM_PIN_INPUT_DATA(0));
  838. DUMP_REG(DC_COM_PIN_INPUT_DATA(1));
  839. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(0));
  840. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(1));
  841. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(2));
  842. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(3));
  843. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(4));
  844. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(5));
  845. DUMP_REG(DC_COM_PIN_OUTPUT_SELECT(6));
  846. DUMP_REG(DC_COM_PIN_MISC_CONTROL);
  847. DUMP_REG(DC_COM_PIN_PM0_CONTROL);
  848. DUMP_REG(DC_COM_PIN_PM0_DUTY_CYCLE);
  849. DUMP_REG(DC_COM_PIN_PM1_CONTROL);
  850. DUMP_REG(DC_COM_PIN_PM1_DUTY_CYCLE);
  851. DUMP_REG(DC_COM_SPI_CONTROL);
  852. DUMP_REG(DC_COM_SPI_START_BYTE);
  853. DUMP_REG(DC_COM_HSPI_WRITE_DATA_AB);
  854. DUMP_REG(DC_COM_HSPI_WRITE_DATA_CD);
  855. DUMP_REG(DC_COM_HSPI_CS_DC);
  856. DUMP_REG(DC_COM_SCRATCH_REGISTER_A);
  857. DUMP_REG(DC_COM_SCRATCH_REGISTER_B);
  858. DUMP_REG(DC_COM_GPIO_CTRL);
  859. DUMP_REG(DC_COM_GPIO_DEBOUNCE_COUNTER);
  860. DUMP_REG(DC_COM_CRC_CHECKSUM_LATCHED);
  861. DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS0);
  862. DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS1);
  863. DUMP_REG(DC_DISP_DISP_WIN_OPTIONS);
  864. DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY);
  865. DUMP_REG(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
  866. DUMP_REG(DC_DISP_DISP_TIMING_OPTIONS);
  867. DUMP_REG(DC_DISP_REF_TO_SYNC);
  868. DUMP_REG(DC_DISP_SYNC_WIDTH);
  869. DUMP_REG(DC_DISP_BACK_PORCH);
  870. DUMP_REG(DC_DISP_ACTIVE);
  871. DUMP_REG(DC_DISP_FRONT_PORCH);
  872. DUMP_REG(DC_DISP_H_PULSE0_CONTROL);
  873. DUMP_REG(DC_DISP_H_PULSE0_POSITION_A);
  874. DUMP_REG(DC_DISP_H_PULSE0_POSITION_B);
  875. DUMP_REG(DC_DISP_H_PULSE0_POSITION_C);
  876. DUMP_REG(DC_DISP_H_PULSE0_POSITION_D);
  877. DUMP_REG(DC_DISP_H_PULSE1_CONTROL);
  878. DUMP_REG(DC_DISP_H_PULSE1_POSITION_A);
  879. DUMP_REG(DC_DISP_H_PULSE1_POSITION_B);
  880. DUMP_REG(DC_DISP_H_PULSE1_POSITION_C);
  881. DUMP_REG(DC_DISP_H_PULSE1_POSITION_D);
  882. DUMP_REG(DC_DISP_H_PULSE2_CONTROL);
  883. DUMP_REG(DC_DISP_H_PULSE2_POSITION_A);
  884. DUMP_REG(DC_DISP_H_PULSE2_POSITION_B);
  885. DUMP_REG(DC_DISP_H_PULSE2_POSITION_C);
  886. DUMP_REG(DC_DISP_H_PULSE2_POSITION_D);
  887. DUMP_REG(DC_DISP_V_PULSE0_CONTROL);
  888. DUMP_REG(DC_DISP_V_PULSE0_POSITION_A);
  889. DUMP_REG(DC_DISP_V_PULSE0_POSITION_B);
  890. DUMP_REG(DC_DISP_V_PULSE0_POSITION_C);
  891. DUMP_REG(DC_DISP_V_PULSE1_CONTROL);
  892. DUMP_REG(DC_DISP_V_PULSE1_POSITION_A);
  893. DUMP_REG(DC_DISP_V_PULSE1_POSITION_B);
  894. DUMP_REG(DC_DISP_V_PULSE1_POSITION_C);
  895. DUMP_REG(DC_DISP_V_PULSE2_CONTROL);
  896. DUMP_REG(DC_DISP_V_PULSE2_POSITION_A);
  897. DUMP_REG(DC_DISP_V_PULSE3_CONTROL);
  898. DUMP_REG(DC_DISP_V_PULSE3_POSITION_A);
  899. DUMP_REG(DC_DISP_M0_CONTROL);
  900. DUMP_REG(DC_DISP_M1_CONTROL);
  901. DUMP_REG(DC_DISP_DI_CONTROL);
  902. DUMP_REG(DC_DISP_PP_CONTROL);
  903. DUMP_REG(DC_DISP_PP_SELECT_A);
  904. DUMP_REG(DC_DISP_PP_SELECT_B);
  905. DUMP_REG(DC_DISP_PP_SELECT_C);
  906. DUMP_REG(DC_DISP_PP_SELECT_D);
  907. DUMP_REG(DC_DISP_DISP_CLOCK_CONTROL);
  908. DUMP_REG(DC_DISP_DISP_INTERFACE_CONTROL);
  909. DUMP_REG(DC_DISP_DISP_COLOR_CONTROL);
  910. DUMP_REG(DC_DISP_SHIFT_CLOCK_OPTIONS);
  911. DUMP_REG(DC_DISP_DATA_ENABLE_OPTIONS);
  912. DUMP_REG(DC_DISP_SERIAL_INTERFACE_OPTIONS);
  913. DUMP_REG(DC_DISP_LCD_SPI_OPTIONS);
  914. DUMP_REG(DC_DISP_BORDER_COLOR);
  915. DUMP_REG(DC_DISP_COLOR_KEY0_LOWER);
  916. DUMP_REG(DC_DISP_COLOR_KEY0_UPPER);
  917. DUMP_REG(DC_DISP_COLOR_KEY1_LOWER);
  918. DUMP_REG(DC_DISP_COLOR_KEY1_UPPER);
  919. DUMP_REG(DC_DISP_CURSOR_FOREGROUND);
  920. DUMP_REG(DC_DISP_CURSOR_BACKGROUND);
  921. DUMP_REG(DC_DISP_CURSOR_START_ADDR);
  922. DUMP_REG(DC_DISP_CURSOR_START_ADDR_NS);
  923. DUMP_REG(DC_DISP_CURSOR_POSITION);
  924. DUMP_REG(DC_DISP_CURSOR_POSITION_NS);
  925. DUMP_REG(DC_DISP_INIT_SEQ_CONTROL);
  926. DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_A);
  927. DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_B);
  928. DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_C);
  929. DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_D);
  930. DUMP_REG(DC_DISP_DC_MCCIF_FIFOCTRL);
  931. DUMP_REG(DC_DISP_MCCIF_DISPLAY0A_HYST);
  932. DUMP_REG(DC_DISP_MCCIF_DISPLAY0B_HYST);
  933. DUMP_REG(DC_DISP_MCCIF_DISPLAY1A_HYST);
  934. DUMP_REG(DC_DISP_MCCIF_DISPLAY1B_HYST);
  935. DUMP_REG(DC_DISP_DAC_CRT_CTRL);
  936. DUMP_REG(DC_DISP_DISP_MISC_CONTROL);
  937. DUMP_REG(DC_DISP_SD_CONTROL);
  938. DUMP_REG(DC_DISP_SD_CSC_COEFF);
  939. DUMP_REG(DC_DISP_SD_LUT(0));
  940. DUMP_REG(DC_DISP_SD_LUT(1));
  941. DUMP_REG(DC_DISP_SD_LUT(2));
  942. DUMP_REG(DC_DISP_SD_LUT(3));
  943. DUMP_REG(DC_DISP_SD_LUT(4));
  944. DUMP_REG(DC_DISP_SD_LUT(5));
  945. DUMP_REG(DC_DISP_SD_LUT(6));
  946. DUMP_REG(DC_DISP_SD_LUT(7));
  947. DUMP_REG(DC_DISP_SD_LUT(8));
  948. DUMP_REG(DC_DISP_SD_FLICKER_CONTROL);
  949. DUMP_REG(DC_DISP_DC_PIXEL_COUNT);
  950. DUMP_REG(DC_DISP_SD_HISTOGRAM(0));
  951. DUMP_REG(DC_DISP_SD_HISTOGRAM(1));
  952. DUMP_REG(DC_DISP_SD_HISTOGRAM(2));
  953. DUMP_REG(DC_DISP_SD_HISTOGRAM(3));
  954. DUMP_REG(DC_DISP_SD_HISTOGRAM(4));
  955. DUMP_REG(DC_DISP_SD_HISTOGRAM(5));
  956. DUMP_REG(DC_DISP_SD_HISTOGRAM(6));
  957. DUMP_REG(DC_DISP_SD_HISTOGRAM(7));
  958. DUMP_REG(DC_DISP_SD_BL_TF(0));
  959. DUMP_REG(DC_DISP_SD_BL_TF(1));
  960. DUMP_REG(DC_DISP_SD_BL_TF(2));
  961. DUMP_REG(DC_DISP_SD_BL_TF(3));
  962. DUMP_REG(DC_DISP_SD_BL_CONTROL);
  963. DUMP_REG(DC_DISP_SD_HW_K_VALUES);
  964. DUMP_REG(DC_DISP_SD_MAN_K_VALUES);
  965. DUMP_REG(DC_DISP_CURSOR_START_ADDR_HI);
  966. DUMP_REG(DC_DISP_BLEND_CURSOR_CONTROL);
  967. DUMP_REG(DC_WIN_WIN_OPTIONS);
  968. DUMP_REG(DC_WIN_BYTE_SWAP);
  969. DUMP_REG(DC_WIN_BUFFER_CONTROL);
  970. DUMP_REG(DC_WIN_COLOR_DEPTH);
  971. DUMP_REG(DC_WIN_POSITION);
  972. DUMP_REG(DC_WIN_SIZE);
  973. DUMP_REG(DC_WIN_PRESCALED_SIZE);
  974. DUMP_REG(DC_WIN_H_INITIAL_DDA);
  975. DUMP_REG(DC_WIN_V_INITIAL_DDA);
  976. DUMP_REG(DC_WIN_DDA_INC);
  977. DUMP_REG(DC_WIN_LINE_STRIDE);
  978. DUMP_REG(DC_WIN_BUF_STRIDE);
  979. DUMP_REG(DC_WIN_UV_BUF_STRIDE);
  980. DUMP_REG(DC_WIN_BUFFER_ADDR_MODE);
  981. DUMP_REG(DC_WIN_DV_CONTROL);
  982. DUMP_REG(DC_WIN_BLEND_NOKEY);
  983. DUMP_REG(DC_WIN_BLEND_1WIN);
  984. DUMP_REG(DC_WIN_BLEND_2WIN_X);
  985. DUMP_REG(DC_WIN_BLEND_2WIN_Y);
  986. DUMP_REG(DC_WIN_BLEND_3WIN_XY);
  987. DUMP_REG(DC_WIN_HP_FETCH_CONTROL);
  988. DUMP_REG(DC_WINBUF_START_ADDR);
  989. DUMP_REG(DC_WINBUF_START_ADDR_NS);
  990. DUMP_REG(DC_WINBUF_START_ADDR_U);
  991. DUMP_REG(DC_WINBUF_START_ADDR_U_NS);
  992. DUMP_REG(DC_WINBUF_START_ADDR_V);
  993. DUMP_REG(DC_WINBUF_START_ADDR_V_NS);
  994. DUMP_REG(DC_WINBUF_ADDR_H_OFFSET);
  995. DUMP_REG(DC_WINBUF_ADDR_H_OFFSET_NS);
  996. DUMP_REG(DC_WINBUF_ADDR_V_OFFSET);
  997. DUMP_REG(DC_WINBUF_ADDR_V_OFFSET_NS);
  998. DUMP_REG(DC_WINBUF_UFLOW_STATUS);
  999. DUMP_REG(DC_WINBUF_AD_UFLOW_STATUS);
  1000. DUMP_REG(DC_WINBUF_BD_UFLOW_STATUS);
  1001. DUMP_REG(DC_WINBUF_CD_UFLOW_STATUS);
  1002. #undef DUMP_REG
  1003. return 0;
  1004. }
  1005. static struct drm_info_list debugfs_files[] = {
  1006. { "regs", tegra_dc_show_regs, 0, NULL },
  1007. };
  1008. static int tegra_dc_debugfs_init(struct tegra_dc *dc, struct drm_minor *minor)
  1009. {
  1010. unsigned int i;
  1011. char *name;
  1012. int err;
  1013. name = kasprintf(GFP_KERNEL, "dc.%d", dc->pipe);
  1014. dc->debugfs = debugfs_create_dir(name, minor->debugfs_root);
  1015. kfree(name);
  1016. if (!dc->debugfs)
  1017. return -ENOMEM;
  1018. dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
  1019. GFP_KERNEL);
  1020. if (!dc->debugfs_files) {
  1021. err = -ENOMEM;
  1022. goto remove;
  1023. }
  1024. for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
  1025. dc->debugfs_files[i].data = dc;
  1026. err = drm_debugfs_create_files(dc->debugfs_files,
  1027. ARRAY_SIZE(debugfs_files),
  1028. dc->debugfs, minor);
  1029. if (err < 0)
  1030. goto free;
  1031. dc->minor = minor;
  1032. return 0;
  1033. free:
  1034. kfree(dc->debugfs_files);
  1035. dc->debugfs_files = NULL;
  1036. remove:
  1037. debugfs_remove(dc->debugfs);
  1038. dc->debugfs = NULL;
  1039. return err;
  1040. }
  1041. static int tegra_dc_debugfs_exit(struct tegra_dc *dc)
  1042. {
  1043. drm_debugfs_remove_files(dc->debugfs_files, ARRAY_SIZE(debugfs_files),
  1044. dc->minor);
  1045. dc->minor = NULL;
  1046. kfree(dc->debugfs_files);
  1047. dc->debugfs_files = NULL;
  1048. debugfs_remove(dc->debugfs);
  1049. dc->debugfs = NULL;
  1050. return 0;
  1051. }
  1052. static int tegra_dc_init(struct host1x_client *client)
  1053. {
  1054. struct drm_device *drm = dev_get_drvdata(client->parent);
  1055. struct tegra_dc *dc = host1x_client_to_dc(client);
  1056. struct tegra_drm *tegra = drm->dev_private;
  1057. int err;
  1058. drm_crtc_init(drm, &dc->base, &tegra_crtc_funcs);
  1059. drm_mode_crtc_set_gamma_size(&dc->base, 256);
  1060. drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
  1061. /*
  1062. * Keep track of the minimum pitch alignment across all display
  1063. * controllers.
  1064. */
  1065. if (dc->soc->pitch_align > tegra->pitch_align)
  1066. tegra->pitch_align = dc->soc->pitch_align;
  1067. err = tegra_dc_rgb_init(drm, dc);
  1068. if (err < 0 && err != -ENODEV) {
  1069. dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
  1070. return err;
  1071. }
  1072. err = tegra_dc_add_planes(drm, dc);
  1073. if (err < 0)
  1074. return err;
  1075. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  1076. err = tegra_dc_debugfs_init(dc, drm->primary);
  1077. if (err < 0)
  1078. dev_err(dc->dev, "debugfs setup failed: %d\n", err);
  1079. }
  1080. err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
  1081. dev_name(dc->dev), dc);
  1082. if (err < 0) {
  1083. dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
  1084. err);
  1085. return err;
  1086. }
  1087. return 0;
  1088. }
  1089. static int tegra_dc_exit(struct host1x_client *client)
  1090. {
  1091. struct tegra_dc *dc = host1x_client_to_dc(client);
  1092. int err;
  1093. devm_free_irq(dc->dev, dc->irq, dc);
  1094. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  1095. err = tegra_dc_debugfs_exit(dc);
  1096. if (err < 0)
  1097. dev_err(dc->dev, "debugfs cleanup failed: %d\n", err);
  1098. }
  1099. err = tegra_dc_rgb_exit(dc);
  1100. if (err) {
  1101. dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
  1102. return err;
  1103. }
  1104. return 0;
  1105. }
  1106. static const struct host1x_client_ops dc_client_ops = {
  1107. .init = tegra_dc_init,
  1108. .exit = tegra_dc_exit,
  1109. };
  1110. static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
  1111. .supports_interlacing = false,
  1112. .supports_cursor = false,
  1113. .supports_block_linear = false,
  1114. .pitch_align = 8,
  1115. };
  1116. static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
  1117. .supports_interlacing = false,
  1118. .supports_cursor = false,
  1119. .supports_block_linear = false,
  1120. .pitch_align = 8,
  1121. };
  1122. static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
  1123. .supports_interlacing = false,
  1124. .supports_cursor = false,
  1125. .supports_block_linear = false,
  1126. .pitch_align = 64,
  1127. };
  1128. static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
  1129. .supports_interlacing = true,
  1130. .supports_cursor = true,
  1131. .supports_block_linear = true,
  1132. .pitch_align = 64,
  1133. };
  1134. static const struct of_device_id tegra_dc_of_match[] = {
  1135. {
  1136. .compatible = "nvidia,tegra124-dc",
  1137. .data = &tegra124_dc_soc_info,
  1138. }, {
  1139. .compatible = "nvidia,tegra30-dc",
  1140. .data = &tegra30_dc_soc_info,
  1141. }, {
  1142. .compatible = "nvidia,tegra20-dc",
  1143. .data = &tegra20_dc_soc_info,
  1144. }, {
  1145. /* sentinel */
  1146. }
  1147. };
  1148. MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
  1149. static int tegra_dc_parse_dt(struct tegra_dc *dc)
  1150. {
  1151. struct device_node *np;
  1152. u32 value = 0;
  1153. int err;
  1154. err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
  1155. if (err < 0) {
  1156. dev_err(dc->dev, "missing \"nvidia,head\" property\n");
  1157. /*
  1158. * If the nvidia,head property isn't present, try to find the
  1159. * correct head number by looking up the position of this
  1160. * display controller's node within the device tree. Assuming
  1161. * that the nodes are ordered properly in the DTS file and
  1162. * that the translation into a flattened device tree blob
  1163. * preserves that ordering this will actually yield the right
  1164. * head number.
  1165. *
  1166. * If those assumptions don't hold, this will still work for
  1167. * cases where only a single display controller is used.
  1168. */
  1169. for_each_matching_node(np, tegra_dc_of_match) {
  1170. if (np == dc->dev->of_node)
  1171. break;
  1172. value++;
  1173. }
  1174. }
  1175. dc->pipe = value;
  1176. return 0;
  1177. }
  1178. static int tegra_dc_probe(struct platform_device *pdev)
  1179. {
  1180. const struct of_device_id *id;
  1181. struct resource *regs;
  1182. struct tegra_dc *dc;
  1183. int err;
  1184. dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
  1185. if (!dc)
  1186. return -ENOMEM;
  1187. id = of_match_node(tegra_dc_of_match, pdev->dev.of_node);
  1188. if (!id)
  1189. return -ENODEV;
  1190. spin_lock_init(&dc->lock);
  1191. INIT_LIST_HEAD(&dc->list);
  1192. dc->dev = &pdev->dev;
  1193. dc->soc = id->data;
  1194. err = tegra_dc_parse_dt(dc);
  1195. if (err < 0)
  1196. return err;
  1197. dc->clk = devm_clk_get(&pdev->dev, NULL);
  1198. if (IS_ERR(dc->clk)) {
  1199. dev_err(&pdev->dev, "failed to get clock\n");
  1200. return PTR_ERR(dc->clk);
  1201. }
  1202. dc->rst = devm_reset_control_get(&pdev->dev, "dc");
  1203. if (IS_ERR(dc->rst)) {
  1204. dev_err(&pdev->dev, "failed to get reset\n");
  1205. return PTR_ERR(dc->rst);
  1206. }
  1207. err = clk_prepare_enable(dc->clk);
  1208. if (err < 0)
  1209. return err;
  1210. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1211. dc->regs = devm_ioremap_resource(&pdev->dev, regs);
  1212. if (IS_ERR(dc->regs))
  1213. return PTR_ERR(dc->regs);
  1214. dc->irq = platform_get_irq(pdev, 0);
  1215. if (dc->irq < 0) {
  1216. dev_err(&pdev->dev, "failed to get IRQ\n");
  1217. return -ENXIO;
  1218. }
  1219. INIT_LIST_HEAD(&dc->client.list);
  1220. dc->client.ops = &dc_client_ops;
  1221. dc->client.dev = &pdev->dev;
  1222. err = tegra_dc_rgb_probe(dc);
  1223. if (err < 0 && err != -ENODEV) {
  1224. dev_err(&pdev->dev, "failed to probe RGB output: %d\n", err);
  1225. return err;
  1226. }
  1227. err = host1x_client_register(&dc->client);
  1228. if (err < 0) {
  1229. dev_err(&pdev->dev, "failed to register host1x client: %d\n",
  1230. err);
  1231. return err;
  1232. }
  1233. platform_set_drvdata(pdev, dc);
  1234. return 0;
  1235. }
  1236. static int tegra_dc_remove(struct platform_device *pdev)
  1237. {
  1238. struct tegra_dc *dc = platform_get_drvdata(pdev);
  1239. int err;
  1240. err = host1x_client_unregister(&dc->client);
  1241. if (err < 0) {
  1242. dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
  1243. err);
  1244. return err;
  1245. }
  1246. err = tegra_dc_rgb_remove(dc);
  1247. if (err < 0) {
  1248. dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
  1249. return err;
  1250. }
  1251. reset_control_assert(dc->rst);
  1252. clk_disable_unprepare(dc->clk);
  1253. return 0;
  1254. }
  1255. struct platform_driver tegra_dc_driver = {
  1256. .driver = {
  1257. .name = "tegra-dc",
  1258. .owner = THIS_MODULE,
  1259. .of_match_table = tegra_dc_of_match,
  1260. },
  1261. .probe = tegra_dc_probe,
  1262. .remove = tegra_dc_remove,
  1263. };