dc.c 45 KB

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