dc.c 63 KB

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