dc.c 54 KB

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