dc.c 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012 Avionic Design GmbH
  4. * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/debugfs.h>
  8. #include <linux/delay.h>
  9. #include <linux/dma-mapping.h>
  10. #include <linux/iommu.h>
  11. #include <linux/interconnect.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pm_domain.h>
  16. #include <linux/pm_opp.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/reset.h>
  19. #include <soc/tegra/common.h>
  20. #include <soc/tegra/pmc.h>
  21. #include <drm/drm_atomic.h>
  22. #include <drm/drm_atomic_helper.h>
  23. #include <drm/drm_blend.h>
  24. #include <drm/drm_debugfs.h>
  25. #include <drm/drm_fourcc.h>
  26. #include <drm/drm_framebuffer.h>
  27. #include <drm/drm_print.h>
  28. #include <drm/drm_vblank.h>
  29. #include "dc.h"
  30. #include "drm.h"
  31. #include "gem.h"
  32. #include "hub.h"
  33. #include "plane.h"
  34. static void tegra_crtc_atomic_destroy_state(struct drm_crtc *crtc,
  35. struct drm_crtc_state *state);
  36. static void tegra_dc_stats_reset(struct tegra_dc_stats *stats)
  37. {
  38. stats->frames = 0;
  39. stats->vblank = 0;
  40. stats->underflow = 0;
  41. stats->overflow = 0;
  42. }
  43. /* Reads the active copy of a register. */
  44. static u32 tegra_dc_readl_active(struct tegra_dc *dc, unsigned long offset)
  45. {
  46. u32 value;
  47. tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
  48. value = tegra_dc_readl(dc, offset);
  49. tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
  50. return value;
  51. }
  52. static inline unsigned int tegra_plane_offset(struct tegra_plane *plane,
  53. unsigned int offset)
  54. {
  55. if (offset >= 0x500 && offset <= 0x638) {
  56. offset = 0x000 + (offset - 0x500);
  57. return plane->offset + offset;
  58. }
  59. if (offset >= 0x700 && offset <= 0x719) {
  60. offset = 0x180 + (offset - 0x700);
  61. return plane->offset + offset;
  62. }
  63. if (offset >= 0x800 && offset <= 0x839) {
  64. offset = 0x1c0 + (offset - 0x800);
  65. return plane->offset + offset;
  66. }
  67. dev_WARN(plane->dc->dev, "invalid offset: %x\n", offset);
  68. return plane->offset + offset;
  69. }
  70. static inline u32 tegra_plane_readl(struct tegra_plane *plane,
  71. unsigned int offset)
  72. {
  73. return tegra_dc_readl(plane->dc, tegra_plane_offset(plane, offset));
  74. }
  75. static inline void tegra_plane_writel(struct tegra_plane *plane, u32 value,
  76. unsigned int offset)
  77. {
  78. tegra_dc_writel(plane->dc, value, tegra_plane_offset(plane, offset));
  79. }
  80. bool tegra_dc_has_output(struct tegra_dc *dc, struct device *dev)
  81. {
  82. struct device_node *np = dc->dev->of_node;
  83. struct of_phandle_iterator it;
  84. int err;
  85. of_for_each_phandle(&it, err, np, "nvidia,outputs", NULL, 0)
  86. if (it.node == dev->of_node)
  87. return true;
  88. return false;
  89. }
  90. /*
  91. * Double-buffered registers have two copies: ASSEMBLY and ACTIVE. When the
  92. * *_ACT_REQ bits are set the ASSEMBLY copy is latched into the ACTIVE copy.
  93. * Latching happens mmediately if the display controller is in STOP mode or
  94. * on the next frame boundary otherwise.
  95. *
  96. * Triple-buffered registers have three copies: ASSEMBLY, ARM and ACTIVE. The
  97. * ASSEMBLY copy is latched into the ARM copy immediately after *_UPDATE bits
  98. * are written. When the *_ACT_REQ bits are written, the ARM copy is latched
  99. * into the ACTIVE copy, either immediately if the display controller is in
  100. * STOP mode, or at the next frame boundary otherwise.
  101. */
  102. void tegra_dc_commit(struct tegra_dc *dc)
  103. {
  104. tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
  105. tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
  106. }
  107. static inline u32 compute_dda_inc(unsigned int in, unsigned int out, bool v,
  108. unsigned int bpp)
  109. {
  110. fixed20_12 outf = dfixed_init(out);
  111. fixed20_12 inf = dfixed_init(in);
  112. u32 dda_inc;
  113. int max;
  114. if (v)
  115. max = 15;
  116. else {
  117. switch (bpp) {
  118. case 2:
  119. max = 8;
  120. break;
  121. default:
  122. WARN_ON_ONCE(1);
  123. fallthrough;
  124. case 4:
  125. max = 4;
  126. break;
  127. }
  128. }
  129. outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
  130. inf.full -= dfixed_const(1);
  131. dda_inc = dfixed_div(inf, outf);
  132. dda_inc = min_t(u32, dda_inc, dfixed_const(max));
  133. return dda_inc;
  134. }
  135. static inline u32 compute_initial_dda(unsigned int in)
  136. {
  137. fixed20_12 inf = dfixed_init(in);
  138. return dfixed_frac(inf);
  139. }
  140. static void tegra_plane_setup_blending_legacy(struct tegra_plane *plane)
  141. {
  142. u32 background[3] = {
  143. BLEND_WEIGHT1(0) | BLEND_WEIGHT0(0) | BLEND_COLOR_KEY_NONE,
  144. BLEND_WEIGHT1(0) | BLEND_WEIGHT0(0) | BLEND_COLOR_KEY_NONE,
  145. BLEND_WEIGHT1(0) | BLEND_WEIGHT0(0) | BLEND_COLOR_KEY_NONE,
  146. };
  147. u32 foreground = BLEND_WEIGHT1(255) | BLEND_WEIGHT0(255) |
  148. BLEND_COLOR_KEY_NONE;
  149. u32 blendnokey = BLEND_WEIGHT1(255) | BLEND_WEIGHT0(255);
  150. struct tegra_plane_state *state;
  151. u32 blending[2];
  152. unsigned int i;
  153. /* disable blending for non-overlapping case */
  154. tegra_plane_writel(plane, blendnokey, DC_WIN_BLEND_NOKEY);
  155. tegra_plane_writel(plane, foreground, DC_WIN_BLEND_1WIN);
  156. state = to_tegra_plane_state(plane->base.state);
  157. if (state->opaque) {
  158. /*
  159. * Since custom fix-weight blending isn't utilized and weight
  160. * of top window is set to max, we can enforce dependent
  161. * blending which in this case results in transparent bottom
  162. * window if top window is opaque and if top window enables
  163. * alpha blending, then bottom window is getting alpha value
  164. * of 1 minus the sum of alpha components of the overlapping
  165. * plane.
  166. */
  167. background[0] |= BLEND_CONTROL_DEPENDENT;
  168. background[1] |= BLEND_CONTROL_DEPENDENT;
  169. /*
  170. * The region where three windows overlap is the intersection
  171. * of the two regions where two windows overlap. It contributes
  172. * to the area if all of the windows on top of it have an alpha
  173. * component.
  174. */
  175. switch (state->base.normalized_zpos) {
  176. case 0:
  177. if (state->blending[0].alpha &&
  178. state->blending[1].alpha)
  179. background[2] |= BLEND_CONTROL_DEPENDENT;
  180. break;
  181. case 1:
  182. background[2] |= BLEND_CONTROL_DEPENDENT;
  183. break;
  184. }
  185. } else {
  186. /*
  187. * Enable alpha blending if pixel format has an alpha
  188. * component.
  189. */
  190. foreground |= BLEND_CONTROL_ALPHA;
  191. /*
  192. * If any of the windows on top of this window is opaque, it
  193. * will completely conceal this window within that area. If
  194. * top window has an alpha component, it is blended over the
  195. * bottom window.
  196. */
  197. for (i = 0; i < 2; i++) {
  198. if (state->blending[i].alpha &&
  199. state->blending[i].top)
  200. background[i] |= BLEND_CONTROL_DEPENDENT;
  201. }
  202. switch (state->base.normalized_zpos) {
  203. case 0:
  204. if (state->blending[0].alpha &&
  205. state->blending[1].alpha)
  206. background[2] |= BLEND_CONTROL_DEPENDENT;
  207. break;
  208. case 1:
  209. /*
  210. * When both middle and topmost windows have an alpha,
  211. * these windows a mixed together and then the result
  212. * is blended over the bottom window.
  213. */
  214. if (state->blending[0].alpha &&
  215. state->blending[0].top)
  216. background[2] |= BLEND_CONTROL_ALPHA;
  217. if (state->blending[1].alpha &&
  218. state->blending[1].top)
  219. background[2] |= BLEND_CONTROL_ALPHA;
  220. break;
  221. }
  222. }
  223. switch (state->base.normalized_zpos) {
  224. case 0:
  225. tegra_plane_writel(plane, background[0], DC_WIN_BLEND_2WIN_X);
  226. tegra_plane_writel(plane, background[1], DC_WIN_BLEND_2WIN_Y);
  227. tegra_plane_writel(plane, background[2], DC_WIN_BLEND_3WIN_XY);
  228. break;
  229. case 1:
  230. /*
  231. * If window B / C is topmost, then X / Y registers are
  232. * matching the order of blending[...] state indices,
  233. * otherwise a swap is required.
  234. */
  235. if (!state->blending[0].top && state->blending[1].top) {
  236. blending[0] = foreground;
  237. blending[1] = background[1];
  238. } else {
  239. blending[0] = background[0];
  240. blending[1] = foreground;
  241. }
  242. tegra_plane_writel(plane, blending[0], DC_WIN_BLEND_2WIN_X);
  243. tegra_plane_writel(plane, blending[1], DC_WIN_BLEND_2WIN_Y);
  244. tegra_plane_writel(plane, background[2], DC_WIN_BLEND_3WIN_XY);
  245. break;
  246. case 2:
  247. tegra_plane_writel(plane, foreground, DC_WIN_BLEND_2WIN_X);
  248. tegra_plane_writel(plane, foreground, DC_WIN_BLEND_2WIN_Y);
  249. tegra_plane_writel(plane, foreground, DC_WIN_BLEND_3WIN_XY);
  250. break;
  251. }
  252. }
  253. static void tegra_plane_setup_blending(struct tegra_plane *plane,
  254. const struct tegra_dc_window *window)
  255. {
  256. u32 value;
  257. value = BLEND_FACTOR_DST_ALPHA_ZERO | BLEND_FACTOR_SRC_ALPHA_K2 |
  258. BLEND_FACTOR_DST_COLOR_NEG_K1_TIMES_SRC |
  259. BLEND_FACTOR_SRC_COLOR_K1_TIMES_SRC;
  260. tegra_plane_writel(plane, value, DC_WIN_BLEND_MATCH_SELECT);
  261. value = BLEND_FACTOR_DST_ALPHA_ZERO | BLEND_FACTOR_SRC_ALPHA_K2 |
  262. BLEND_FACTOR_DST_COLOR_NEG_K1_TIMES_SRC |
  263. BLEND_FACTOR_SRC_COLOR_K1_TIMES_SRC;
  264. tegra_plane_writel(plane, value, DC_WIN_BLEND_NOMATCH_SELECT);
  265. value = K2(255) | K1(255) | WINDOW_LAYER_DEPTH(255 - window->zpos);
  266. tegra_plane_writel(plane, value, DC_WIN_BLEND_LAYER_CONTROL);
  267. }
  268. static bool
  269. tegra_plane_use_horizontal_filtering(struct tegra_plane *plane,
  270. const struct tegra_dc_window *window)
  271. {
  272. struct tegra_dc *dc = plane->dc;
  273. if (window->src.w == window->dst.w)
  274. return false;
  275. if (plane->index == 0 && dc->soc->has_win_a_without_filters)
  276. return false;
  277. return true;
  278. }
  279. static bool
  280. tegra_plane_use_vertical_filtering(struct tegra_plane *plane,
  281. const struct tegra_dc_window *window)
  282. {
  283. struct tegra_dc *dc = plane->dc;
  284. if (window->src.h == window->dst.h)
  285. return false;
  286. if (plane->index == 0 && dc->soc->has_win_a_without_filters)
  287. return false;
  288. if (plane->index == 2 && dc->soc->has_win_c_without_vert_filter)
  289. return false;
  290. return true;
  291. }
  292. static void tegra_dc_setup_window(struct tegra_plane *plane,
  293. const struct tegra_dc_window *window)
  294. {
  295. unsigned h_offset, v_offset, h_size, v_size, h_dda, v_dda, bpp;
  296. struct tegra_dc *dc = plane->dc;
  297. unsigned int planes;
  298. u32 value;
  299. bool yuv;
  300. /*
  301. * For YUV planar modes, the number of bytes per pixel takes into
  302. * account only the luma component and therefore is 1.
  303. */
  304. yuv = tegra_plane_format_is_yuv(window->format, &planes, NULL);
  305. if (!yuv)
  306. bpp = window->bits_per_pixel / 8;
  307. else
  308. bpp = (planes > 1) ? 1 : 2;
  309. tegra_plane_writel(plane, window->format, DC_WIN_COLOR_DEPTH);
  310. tegra_plane_writel(plane, window->swap, DC_WIN_BYTE_SWAP);
  311. value = V_POSITION(window->dst.y) | H_POSITION(window->dst.x);
  312. tegra_plane_writel(plane, value, DC_WIN_POSITION);
  313. value = V_SIZE(window->dst.h) | H_SIZE(window->dst.w);
  314. tegra_plane_writel(plane, value, DC_WIN_SIZE);
  315. h_offset = window->src.x * bpp;
  316. v_offset = window->src.y;
  317. h_size = window->src.w * bpp;
  318. v_size = window->src.h;
  319. if (window->reflect_x)
  320. h_offset += (window->src.w - 1) * bpp;
  321. if (window->reflect_y)
  322. v_offset += window->src.h - 1;
  323. value = V_PRESCALED_SIZE(v_size) | H_PRESCALED_SIZE(h_size);
  324. tegra_plane_writel(plane, value, DC_WIN_PRESCALED_SIZE);
  325. /*
  326. * For DDA computations the number of bytes per pixel for YUV planar
  327. * modes needs to take into account all Y, U and V components.
  328. */
  329. if (yuv && planes > 1)
  330. bpp = 2;
  331. h_dda = compute_dda_inc(window->src.w, window->dst.w, false, bpp);
  332. v_dda = compute_dda_inc(window->src.h, window->dst.h, true, bpp);
  333. value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
  334. tegra_plane_writel(plane, value, DC_WIN_DDA_INC);
  335. h_dda = compute_initial_dda(window->src.x);
  336. v_dda = compute_initial_dda(window->src.y);
  337. tegra_plane_writel(plane, h_dda, DC_WIN_H_INITIAL_DDA);
  338. tegra_plane_writel(plane, v_dda, DC_WIN_V_INITIAL_DDA);
  339. tegra_plane_writel(plane, 0, DC_WIN_UV_BUF_STRIDE);
  340. tegra_plane_writel(plane, 0, DC_WIN_BUF_STRIDE);
  341. tegra_plane_writel(plane, window->base[0], DC_WINBUF_START_ADDR);
  342. if (yuv && planes > 1) {
  343. tegra_plane_writel(plane, window->base[1], DC_WINBUF_START_ADDR_U);
  344. if (planes > 2)
  345. tegra_plane_writel(plane, window->base[2], DC_WINBUF_START_ADDR_V);
  346. value = window->stride[1] << 16 | window->stride[0];
  347. tegra_plane_writel(plane, value, DC_WIN_LINE_STRIDE);
  348. } else {
  349. tegra_plane_writel(plane, window->stride[0], DC_WIN_LINE_STRIDE);
  350. }
  351. tegra_plane_writel(plane, h_offset, DC_WINBUF_ADDR_H_OFFSET);
  352. tegra_plane_writel(plane, v_offset, DC_WINBUF_ADDR_V_OFFSET);
  353. if (dc->soc->supports_block_linear) {
  354. unsigned long height = window->tiling.value;
  355. switch (window->tiling.mode) {
  356. case TEGRA_BO_TILING_MODE_PITCH:
  357. value = DC_WINBUF_SURFACE_KIND_PITCH;
  358. break;
  359. case TEGRA_BO_TILING_MODE_TILED:
  360. value = DC_WINBUF_SURFACE_KIND_TILED;
  361. break;
  362. case TEGRA_BO_TILING_MODE_BLOCK:
  363. value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
  364. DC_WINBUF_SURFACE_KIND_BLOCK;
  365. break;
  366. }
  367. tegra_plane_writel(plane, value, DC_WINBUF_SURFACE_KIND);
  368. } else {
  369. switch (window->tiling.mode) {
  370. case TEGRA_BO_TILING_MODE_PITCH:
  371. value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
  372. DC_WIN_BUFFER_ADDR_MODE_LINEAR;
  373. break;
  374. case TEGRA_BO_TILING_MODE_TILED:
  375. value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
  376. DC_WIN_BUFFER_ADDR_MODE_TILE;
  377. break;
  378. case TEGRA_BO_TILING_MODE_BLOCK:
  379. /*
  380. * No need to handle this here because ->atomic_check
  381. * will already have filtered it out.
  382. */
  383. break;
  384. }
  385. tegra_plane_writel(plane, value, DC_WIN_BUFFER_ADDR_MODE);
  386. }
  387. value = WIN_ENABLE;
  388. if (yuv) {
  389. /* setup default colorspace conversion coefficients */
  390. tegra_plane_writel(plane, 0x00f0, DC_WIN_CSC_YOF);
  391. tegra_plane_writel(plane, 0x012a, DC_WIN_CSC_KYRGB);
  392. tegra_plane_writel(plane, 0x0000, DC_WIN_CSC_KUR);
  393. tegra_plane_writel(plane, 0x0198, DC_WIN_CSC_KVR);
  394. tegra_plane_writel(plane, 0x039b, DC_WIN_CSC_KUG);
  395. tegra_plane_writel(plane, 0x032f, DC_WIN_CSC_KVG);
  396. tegra_plane_writel(plane, 0x0204, DC_WIN_CSC_KUB);
  397. tegra_plane_writel(plane, 0x0000, DC_WIN_CSC_KVB);
  398. value |= CSC_ENABLE;
  399. } else if (window->bits_per_pixel < 24) {
  400. value |= COLOR_EXPAND;
  401. }
  402. if (window->reflect_x)
  403. value |= H_DIRECTION;
  404. if (window->reflect_y)
  405. value |= V_DIRECTION;
  406. if (tegra_plane_use_horizontal_filtering(plane, window)) {
  407. /*
  408. * Enable horizontal 6-tap filter and set filtering
  409. * coefficients to the default values defined in TRM.
  410. */
  411. tegra_plane_writel(plane, 0x00008000, DC_WIN_H_FILTER_P(0));
  412. tegra_plane_writel(plane, 0x3e087ce1, DC_WIN_H_FILTER_P(1));
  413. tegra_plane_writel(plane, 0x3b117ac1, DC_WIN_H_FILTER_P(2));
  414. tegra_plane_writel(plane, 0x591b73aa, DC_WIN_H_FILTER_P(3));
  415. tegra_plane_writel(plane, 0x57256d9a, DC_WIN_H_FILTER_P(4));
  416. tegra_plane_writel(plane, 0x552f668b, DC_WIN_H_FILTER_P(5));
  417. tegra_plane_writel(plane, 0x73385e8b, DC_WIN_H_FILTER_P(6));
  418. tegra_plane_writel(plane, 0x72435583, DC_WIN_H_FILTER_P(7));
  419. tegra_plane_writel(plane, 0x714c4c8b, DC_WIN_H_FILTER_P(8));
  420. tegra_plane_writel(plane, 0x70554393, DC_WIN_H_FILTER_P(9));
  421. tegra_plane_writel(plane, 0x715e389b, DC_WIN_H_FILTER_P(10));
  422. tegra_plane_writel(plane, 0x71662faa, DC_WIN_H_FILTER_P(11));
  423. tegra_plane_writel(plane, 0x536d25ba, DC_WIN_H_FILTER_P(12));
  424. tegra_plane_writel(plane, 0x55731bca, DC_WIN_H_FILTER_P(13));
  425. tegra_plane_writel(plane, 0x387a11d9, DC_WIN_H_FILTER_P(14));
  426. tegra_plane_writel(plane, 0x3c7c08f1, DC_WIN_H_FILTER_P(15));
  427. value |= H_FILTER;
  428. }
  429. if (tegra_plane_use_vertical_filtering(plane, window)) {
  430. unsigned int i, k;
  431. /*
  432. * Enable vertical 2-tap filter and set filtering
  433. * coefficients to the default values defined in TRM.
  434. */
  435. for (i = 0, k = 128; i < 16; i++, k -= 8)
  436. tegra_plane_writel(plane, k, DC_WIN_V_FILTER_P(i));
  437. value |= V_FILTER;
  438. }
  439. tegra_plane_writel(plane, value, DC_WIN_WIN_OPTIONS);
  440. if (dc->soc->has_legacy_blending)
  441. tegra_plane_setup_blending_legacy(plane);
  442. else
  443. tegra_plane_setup_blending(plane, window);
  444. }
  445. static const u32 tegra20_primary_formats[] = {
  446. DRM_FORMAT_ARGB4444,
  447. DRM_FORMAT_ARGB1555,
  448. DRM_FORMAT_RGB565,
  449. DRM_FORMAT_RGBA5551,
  450. DRM_FORMAT_ABGR8888,
  451. DRM_FORMAT_ARGB8888,
  452. /* non-native formats */
  453. DRM_FORMAT_XRGB1555,
  454. DRM_FORMAT_RGBX5551,
  455. DRM_FORMAT_XBGR8888,
  456. DRM_FORMAT_XRGB8888,
  457. };
  458. static const u64 tegra20_modifiers[] = {
  459. DRM_FORMAT_MOD_LINEAR,
  460. DRM_FORMAT_MOD_NVIDIA_TEGRA_TILED,
  461. DRM_FORMAT_MOD_INVALID
  462. };
  463. static const u32 tegra114_primary_formats[] = {
  464. DRM_FORMAT_ARGB4444,
  465. DRM_FORMAT_ARGB1555,
  466. DRM_FORMAT_RGB565,
  467. DRM_FORMAT_RGBA5551,
  468. DRM_FORMAT_ABGR8888,
  469. DRM_FORMAT_ARGB8888,
  470. /* new on Tegra114 */
  471. DRM_FORMAT_ABGR4444,
  472. DRM_FORMAT_ABGR1555,
  473. DRM_FORMAT_BGRA5551,
  474. DRM_FORMAT_XRGB1555,
  475. DRM_FORMAT_RGBX5551,
  476. DRM_FORMAT_XBGR1555,
  477. DRM_FORMAT_BGRX5551,
  478. DRM_FORMAT_BGR565,
  479. DRM_FORMAT_BGRA8888,
  480. DRM_FORMAT_RGBA8888,
  481. DRM_FORMAT_XRGB8888,
  482. DRM_FORMAT_XBGR8888,
  483. };
  484. static const u32 tegra124_primary_formats[] = {
  485. DRM_FORMAT_ARGB4444,
  486. DRM_FORMAT_ARGB1555,
  487. DRM_FORMAT_RGB565,
  488. DRM_FORMAT_RGBA5551,
  489. DRM_FORMAT_ABGR8888,
  490. DRM_FORMAT_ARGB8888,
  491. /* new on Tegra114 */
  492. DRM_FORMAT_ABGR4444,
  493. DRM_FORMAT_ABGR1555,
  494. DRM_FORMAT_BGRA5551,
  495. DRM_FORMAT_XRGB1555,
  496. DRM_FORMAT_RGBX5551,
  497. DRM_FORMAT_XBGR1555,
  498. DRM_FORMAT_BGRX5551,
  499. DRM_FORMAT_BGR565,
  500. DRM_FORMAT_BGRA8888,
  501. DRM_FORMAT_RGBA8888,
  502. DRM_FORMAT_XRGB8888,
  503. DRM_FORMAT_XBGR8888,
  504. /* new on Tegra124 */
  505. DRM_FORMAT_RGBX8888,
  506. DRM_FORMAT_BGRX8888,
  507. };
  508. static const u64 tegra124_modifiers[] = {
  509. DRM_FORMAT_MOD_LINEAR,
  510. DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(0),
  511. DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(1),
  512. DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(2),
  513. DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(3),
  514. DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(4),
  515. DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(5),
  516. DRM_FORMAT_MOD_INVALID
  517. };
  518. static int tegra_plane_atomic_check(struct drm_plane *plane,
  519. struct drm_atomic_state *state)
  520. {
  521. struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
  522. plane);
  523. struct tegra_plane_state *plane_state = to_tegra_plane_state(new_plane_state);
  524. unsigned int supported_rotation = DRM_MODE_ROTATE_0 |
  525. DRM_MODE_REFLECT_X |
  526. DRM_MODE_REFLECT_Y;
  527. unsigned int rotation = new_plane_state->rotation;
  528. struct tegra_bo_tiling *tiling = &plane_state->tiling;
  529. struct tegra_plane *tegra = to_tegra_plane(plane);
  530. struct tegra_dc *dc = to_tegra_dc(new_plane_state->crtc);
  531. int err;
  532. plane_state->peak_memory_bandwidth = 0;
  533. plane_state->avg_memory_bandwidth = 0;
  534. /* no need for further checks if the plane is being disabled */
  535. if (!new_plane_state->crtc) {
  536. plane_state->total_peak_memory_bandwidth = 0;
  537. return 0;
  538. }
  539. err = tegra_plane_format(new_plane_state->fb->format->format,
  540. &plane_state->format,
  541. &plane_state->swap);
  542. if (err < 0)
  543. return err;
  544. /*
  545. * Tegra20 and Tegra30 are special cases here because they support
  546. * only variants of specific formats with an alpha component, but not
  547. * the corresponding opaque formats. However, the opaque formats can
  548. * be emulated by disabling alpha blending for the plane.
  549. */
  550. if (dc->soc->has_legacy_blending) {
  551. err = tegra_plane_setup_legacy_state(tegra, plane_state);
  552. if (err < 0)
  553. return err;
  554. }
  555. err = tegra_fb_get_tiling(new_plane_state->fb, tiling);
  556. if (err < 0)
  557. return err;
  558. if (tiling->mode == TEGRA_BO_TILING_MODE_BLOCK &&
  559. !dc->soc->supports_block_linear) {
  560. DRM_ERROR("hardware doesn't support block linear mode\n");
  561. return -EINVAL;
  562. }
  563. /*
  564. * Older userspace used custom BO flag in order to specify the Y
  565. * reflection, while modern userspace uses the generic DRM rotation
  566. * property in order to achieve the same result. The legacy BO flag
  567. * duplicates the DRM rotation property when both are set.
  568. */
  569. if (tegra_fb_is_bottom_up(new_plane_state->fb))
  570. rotation |= DRM_MODE_REFLECT_Y;
  571. rotation = drm_rotation_simplify(rotation, supported_rotation);
  572. if (rotation & DRM_MODE_REFLECT_X)
  573. plane_state->reflect_x = true;
  574. else
  575. plane_state->reflect_x = false;
  576. if (rotation & DRM_MODE_REFLECT_Y)
  577. plane_state->reflect_y = true;
  578. else
  579. plane_state->reflect_y = false;
  580. /*
  581. * Tegra doesn't support different strides for U and V planes so we
  582. * error out if the user tries to display a framebuffer with such a
  583. * configuration.
  584. */
  585. if (new_plane_state->fb->format->num_planes > 2) {
  586. if (new_plane_state->fb->pitches[2] != new_plane_state->fb->pitches[1]) {
  587. DRM_ERROR("unsupported UV-plane configuration\n");
  588. return -EINVAL;
  589. }
  590. }
  591. err = tegra_plane_state_add(tegra, new_plane_state);
  592. if (err < 0)
  593. return err;
  594. return 0;
  595. }
  596. static void tegra_plane_atomic_disable(struct drm_plane *plane,
  597. struct drm_atomic_state *state)
  598. {
  599. struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
  600. plane);
  601. struct tegra_plane *p = to_tegra_plane(plane);
  602. u32 value;
  603. /* rien ne va plus */
  604. if (!old_state || !old_state->crtc)
  605. return;
  606. value = tegra_plane_readl(p, DC_WIN_WIN_OPTIONS);
  607. value &= ~WIN_ENABLE;
  608. tegra_plane_writel(p, value, DC_WIN_WIN_OPTIONS);
  609. }
  610. static void tegra_plane_atomic_update(struct drm_plane *plane,
  611. struct drm_atomic_state *state)
  612. {
  613. struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
  614. plane);
  615. struct tegra_plane_state *tegra_plane_state = to_tegra_plane_state(new_state);
  616. struct drm_framebuffer *fb = new_state->fb;
  617. struct tegra_plane *p = to_tegra_plane(plane);
  618. struct tegra_dc_window window;
  619. unsigned int i;
  620. /* rien ne va plus */
  621. if (!new_state->crtc || !new_state->fb)
  622. return;
  623. if (!new_state->visible)
  624. return tegra_plane_atomic_disable(plane, state);
  625. memset(&window, 0, sizeof(window));
  626. window.src.x = new_state->src.x1 >> 16;
  627. window.src.y = new_state->src.y1 >> 16;
  628. window.src.w = drm_rect_width(&new_state->src) >> 16;
  629. window.src.h = drm_rect_height(&new_state->src) >> 16;
  630. window.dst.x = new_state->dst.x1;
  631. window.dst.y = new_state->dst.y1;
  632. window.dst.w = drm_rect_width(&new_state->dst);
  633. window.dst.h = drm_rect_height(&new_state->dst);
  634. window.bits_per_pixel = fb->format->cpp[0] * 8;
  635. window.reflect_x = tegra_plane_state->reflect_x;
  636. window.reflect_y = tegra_plane_state->reflect_y;
  637. /* copy from state */
  638. window.zpos = new_state->normalized_zpos;
  639. window.tiling = tegra_plane_state->tiling;
  640. window.format = tegra_plane_state->format;
  641. window.swap = tegra_plane_state->swap;
  642. for (i = 0; i < fb->format->num_planes; i++) {
  643. window.base[i] = tegra_plane_state->iova[i] + fb->offsets[i];
  644. /*
  645. * Tegra uses a shared stride for UV planes. Framebuffers are
  646. * already checked for this in the tegra_plane_atomic_check()
  647. * function, so it's safe to ignore the V-plane pitch here.
  648. */
  649. if (i < 2)
  650. window.stride[i] = fb->pitches[i];
  651. }
  652. tegra_dc_setup_window(p, &window);
  653. }
  654. static const struct drm_plane_helper_funcs tegra_plane_helper_funcs = {
  655. .prepare_fb = tegra_plane_prepare_fb,
  656. .cleanup_fb = tegra_plane_cleanup_fb,
  657. .atomic_check = tegra_plane_atomic_check,
  658. .atomic_disable = tegra_plane_atomic_disable,
  659. .atomic_update = tegra_plane_atomic_update,
  660. };
  661. static unsigned long tegra_plane_get_possible_crtcs(struct drm_device *drm)
  662. {
  663. /*
  664. * Ideally this would use drm_crtc_mask(), but that would require the
  665. * CRTC to already be in the mode_config's list of CRTCs. However, it
  666. * will only be added to that list in the drm_crtc_init_with_planes()
  667. * (in tegra_dc_init()), which in turn requires registration of these
  668. * planes. So we have ourselves a nice little chicken and egg problem
  669. * here.
  670. *
  671. * We work around this by manually creating the mask from the number
  672. * of CRTCs that have been registered, and should therefore always be
  673. * the same as drm_crtc_index() after registration.
  674. */
  675. return 1 << drm->mode_config.num_crtc;
  676. }
  677. static struct drm_plane *tegra_primary_plane_create(struct drm_device *drm,
  678. struct tegra_dc *dc)
  679. {
  680. unsigned long possible_crtcs = tegra_plane_get_possible_crtcs(drm);
  681. enum drm_plane_type type = DRM_PLANE_TYPE_PRIMARY;
  682. struct tegra_plane *plane;
  683. unsigned int num_formats;
  684. const u64 *modifiers;
  685. const u32 *formats;
  686. int err;
  687. plane = kzalloc_obj(*plane);
  688. if (!plane)
  689. return ERR_PTR(-ENOMEM);
  690. /* Always use window A as primary window */
  691. plane->offset = 0xa00;
  692. plane->index = 0;
  693. plane->dc = dc;
  694. num_formats = dc->soc->num_primary_formats;
  695. formats = dc->soc->primary_formats;
  696. modifiers = dc->soc->modifiers;
  697. err = tegra_plane_interconnect_init(plane);
  698. if (err) {
  699. kfree(plane);
  700. return ERR_PTR(err);
  701. }
  702. err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
  703. &tegra_plane_funcs, formats,
  704. num_formats, modifiers, type, NULL);
  705. if (err < 0) {
  706. kfree(plane);
  707. return ERR_PTR(err);
  708. }
  709. drm_plane_helper_add(&plane->base, &tegra_plane_helper_funcs);
  710. drm_plane_create_zpos_property(&plane->base, plane->index, 0, 255);
  711. err = drm_plane_create_rotation_property(&plane->base,
  712. DRM_MODE_ROTATE_0,
  713. DRM_MODE_ROTATE_0 |
  714. DRM_MODE_ROTATE_180 |
  715. DRM_MODE_REFLECT_X |
  716. DRM_MODE_REFLECT_Y);
  717. if (err < 0)
  718. dev_err(dc->dev, "failed to create rotation property: %d\n",
  719. err);
  720. return &plane->base;
  721. }
  722. static const u32 tegra_legacy_cursor_plane_formats[] = {
  723. DRM_FORMAT_RGBA8888,
  724. };
  725. static const u32 tegra_cursor_plane_formats[] = {
  726. DRM_FORMAT_ARGB8888,
  727. };
  728. static int tegra_cursor_atomic_check(struct drm_plane *plane,
  729. struct drm_atomic_state *state)
  730. {
  731. struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
  732. plane);
  733. struct tegra_plane_state *plane_state = to_tegra_plane_state(new_plane_state);
  734. struct tegra_plane *tegra = to_tegra_plane(plane);
  735. int err;
  736. plane_state->peak_memory_bandwidth = 0;
  737. plane_state->avg_memory_bandwidth = 0;
  738. /* no need for further checks if the plane is being disabled */
  739. if (!new_plane_state->crtc) {
  740. plane_state->total_peak_memory_bandwidth = 0;
  741. return 0;
  742. }
  743. /* scaling not supported for cursor */
  744. if ((new_plane_state->src_w >> 16 != new_plane_state->crtc_w) ||
  745. (new_plane_state->src_h >> 16 != new_plane_state->crtc_h))
  746. return -EINVAL;
  747. /* only square cursors supported */
  748. if (new_plane_state->src_w != new_plane_state->src_h)
  749. return -EINVAL;
  750. if (new_plane_state->crtc_w != 32 && new_plane_state->crtc_w != 64 &&
  751. new_plane_state->crtc_w != 128 && new_plane_state->crtc_w != 256)
  752. return -EINVAL;
  753. err = tegra_plane_state_add(tegra, new_plane_state);
  754. if (err < 0)
  755. return err;
  756. return 0;
  757. }
  758. static void __tegra_cursor_atomic_update(struct drm_plane *plane,
  759. struct drm_plane_state *new_state)
  760. {
  761. struct tegra_plane_state *tegra_plane_state = to_tegra_plane_state(new_state);
  762. struct tegra_dc *dc = to_tegra_dc(new_state->crtc);
  763. struct tegra_drm *tegra = plane->dev->dev_private;
  764. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  765. u64 dma_mask = *dc->dev->dma_mask;
  766. #endif
  767. unsigned int x, y;
  768. u32 value = 0;
  769. /* rien ne va plus */
  770. if (!new_state->crtc || !new_state->fb)
  771. return;
  772. /*
  773. * Legacy display supports hardware clipping of the cursor, but
  774. * nvdisplay relies on software to clip the cursor to the screen.
  775. */
  776. if (!dc->soc->has_nvdisplay)
  777. value |= CURSOR_CLIP_DISPLAY;
  778. switch (new_state->crtc_w) {
  779. case 32:
  780. value |= CURSOR_SIZE_32x32;
  781. break;
  782. case 64:
  783. value |= CURSOR_SIZE_64x64;
  784. break;
  785. case 128:
  786. value |= CURSOR_SIZE_128x128;
  787. break;
  788. case 256:
  789. value |= CURSOR_SIZE_256x256;
  790. break;
  791. default:
  792. WARN(1, "cursor size %ux%u not supported\n",
  793. new_state->crtc_w, new_state->crtc_h);
  794. return;
  795. }
  796. value |= (tegra_plane_state->iova[0] >> 10) & 0x3fffff;
  797. tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR);
  798. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  799. value = (tegra_plane_state->iova[0] >> 32) & (dma_mask >> 32);
  800. tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR_HI);
  801. #endif
  802. /* enable cursor and set blend mode */
  803. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  804. value |= CURSOR_ENABLE;
  805. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  806. value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
  807. value &= ~CURSOR_DST_BLEND_MASK;
  808. value &= ~CURSOR_SRC_BLEND_MASK;
  809. if (dc->soc->has_nvdisplay)
  810. value &= ~CURSOR_COMPOSITION_MODE_XOR;
  811. else
  812. value |= CURSOR_MODE_NORMAL;
  813. value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
  814. value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
  815. value |= CURSOR_ALPHA;
  816. tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
  817. /* nvdisplay relies on software for clipping */
  818. if (dc->soc->has_nvdisplay) {
  819. struct drm_rect src;
  820. x = new_state->dst.x1;
  821. y = new_state->dst.y1;
  822. drm_rect_fp_to_int(&src, &new_state->src);
  823. value = (src.y1 & tegra->vmask) << 16 | (src.x1 & tegra->hmask);
  824. tegra_dc_writel(dc, value, DC_DISP_PCALC_HEAD_SET_CROPPED_POINT_IN_CURSOR);
  825. value = (drm_rect_height(&src) & tegra->vmask) << 16 |
  826. (drm_rect_width(&src) & tegra->hmask);
  827. tegra_dc_writel(dc, value, DC_DISP_PCALC_HEAD_SET_CROPPED_SIZE_IN_CURSOR);
  828. } else {
  829. x = new_state->crtc_x;
  830. y = new_state->crtc_y;
  831. }
  832. /* position the cursor */
  833. value = ((y & tegra->vmask) << 16) | (x & tegra->hmask);
  834. tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
  835. }
  836. static void tegra_cursor_atomic_update(struct drm_plane *plane,
  837. struct drm_atomic_state *state)
  838. {
  839. struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, plane);
  840. __tegra_cursor_atomic_update(plane, new_state);
  841. }
  842. static void tegra_cursor_atomic_disable(struct drm_plane *plane,
  843. struct drm_atomic_state *state)
  844. {
  845. struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
  846. plane);
  847. struct tegra_dc *dc;
  848. u32 value;
  849. /* rien ne va plus */
  850. if (!old_state || !old_state->crtc)
  851. return;
  852. dc = to_tegra_dc(old_state->crtc);
  853. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  854. value &= ~CURSOR_ENABLE;
  855. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  856. }
  857. static int tegra_cursor_atomic_async_check(struct drm_plane *plane, struct drm_atomic_state *state,
  858. bool flip)
  859. {
  860. struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, plane);
  861. struct drm_crtc_state *crtc_state;
  862. int min_scale, max_scale;
  863. int err;
  864. crtc_state = drm_atomic_get_new_crtc_state(state, new_state->crtc);
  865. if (WARN_ON(!crtc_state))
  866. return -EINVAL;
  867. if (!crtc_state->active)
  868. return -EINVAL;
  869. if (plane->state->crtc != new_state->crtc ||
  870. plane->state->src_w != new_state->src_w ||
  871. plane->state->src_h != new_state->src_h ||
  872. plane->state->crtc_w != new_state->crtc_w ||
  873. plane->state->crtc_h != new_state->crtc_h ||
  874. plane->state->fb != new_state->fb ||
  875. plane->state->fb == NULL)
  876. return -EINVAL;
  877. min_scale = (1 << 16) / 8;
  878. max_scale = (8 << 16) / 1;
  879. err = drm_atomic_helper_check_plane_state(new_state, crtc_state, min_scale, max_scale,
  880. true, true);
  881. if (err < 0)
  882. return err;
  883. if (new_state->visible != plane->state->visible)
  884. return -EINVAL;
  885. return 0;
  886. }
  887. static void tegra_cursor_atomic_async_update(struct drm_plane *plane,
  888. struct drm_atomic_state *state)
  889. {
  890. struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, plane);
  891. struct tegra_dc *dc = to_tegra_dc(new_state->crtc);
  892. plane->state->src_x = new_state->src_x;
  893. plane->state->src_y = new_state->src_y;
  894. plane->state->crtc_x = new_state->crtc_x;
  895. plane->state->crtc_y = new_state->crtc_y;
  896. if (new_state->visible) {
  897. struct tegra_plane *p = to_tegra_plane(plane);
  898. u32 value;
  899. __tegra_cursor_atomic_update(plane, new_state);
  900. value = (WIN_A_ACT_REQ << p->index) << 8 | GENERAL_UPDATE;
  901. tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
  902. (void)tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
  903. value = (WIN_A_ACT_REQ << p->index) | GENERAL_ACT_REQ;
  904. tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
  905. (void)tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
  906. }
  907. }
  908. static const struct drm_plane_helper_funcs tegra_cursor_plane_helper_funcs = {
  909. .prepare_fb = tegra_plane_prepare_fb,
  910. .cleanup_fb = tegra_plane_cleanup_fb,
  911. .atomic_check = tegra_cursor_atomic_check,
  912. .atomic_update = tegra_cursor_atomic_update,
  913. .atomic_disable = tegra_cursor_atomic_disable,
  914. .atomic_async_check = tegra_cursor_atomic_async_check,
  915. .atomic_async_update = tegra_cursor_atomic_async_update,
  916. };
  917. static const uint64_t linear_modifiers[] = {
  918. DRM_FORMAT_MOD_LINEAR,
  919. DRM_FORMAT_MOD_INVALID
  920. };
  921. static struct drm_plane *tegra_dc_cursor_plane_create(struct drm_device *drm,
  922. struct tegra_dc *dc)
  923. {
  924. unsigned long possible_crtcs = tegra_plane_get_possible_crtcs(drm);
  925. struct tegra_plane *plane;
  926. unsigned int num_formats;
  927. const u32 *formats;
  928. int err;
  929. plane = kzalloc_obj(*plane);
  930. if (!plane)
  931. return ERR_PTR(-ENOMEM);
  932. /*
  933. * This index is kind of fake. The cursor isn't a regular plane, but
  934. * its update and activation request bits in DC_CMD_STATE_CONTROL do
  935. * use the same programming. Setting this fake index here allows the
  936. * code in tegra_add_plane_state() to do the right thing without the
  937. * need to special-casing the cursor plane.
  938. */
  939. plane->index = 6;
  940. plane->dc = dc;
  941. if (!dc->soc->has_nvdisplay) {
  942. num_formats = ARRAY_SIZE(tegra_legacy_cursor_plane_formats);
  943. formats = tegra_legacy_cursor_plane_formats;
  944. err = tegra_plane_interconnect_init(plane);
  945. if (err) {
  946. kfree(plane);
  947. return ERR_PTR(err);
  948. }
  949. } else {
  950. num_formats = ARRAY_SIZE(tegra_cursor_plane_formats);
  951. formats = tegra_cursor_plane_formats;
  952. }
  953. err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
  954. &tegra_plane_funcs, formats,
  955. num_formats, linear_modifiers,
  956. DRM_PLANE_TYPE_CURSOR, NULL);
  957. if (err < 0) {
  958. kfree(plane);
  959. return ERR_PTR(err);
  960. }
  961. drm_plane_helper_add(&plane->base, &tegra_cursor_plane_helper_funcs);
  962. drm_plane_create_zpos_immutable_property(&plane->base, 255);
  963. return &plane->base;
  964. }
  965. static const u32 tegra20_overlay_formats[] = {
  966. DRM_FORMAT_ARGB4444,
  967. DRM_FORMAT_ARGB1555,
  968. DRM_FORMAT_RGB565,
  969. DRM_FORMAT_RGBA5551,
  970. DRM_FORMAT_ABGR8888,
  971. DRM_FORMAT_ARGB8888,
  972. /* non-native formats */
  973. DRM_FORMAT_XRGB1555,
  974. DRM_FORMAT_RGBX5551,
  975. DRM_FORMAT_XBGR8888,
  976. DRM_FORMAT_XRGB8888,
  977. /* planar formats */
  978. DRM_FORMAT_UYVY,
  979. DRM_FORMAT_YUYV,
  980. DRM_FORMAT_YUV420,
  981. DRM_FORMAT_YUV422,
  982. };
  983. static const u32 tegra114_overlay_formats[] = {
  984. DRM_FORMAT_ARGB4444,
  985. DRM_FORMAT_ARGB1555,
  986. DRM_FORMAT_RGB565,
  987. DRM_FORMAT_RGBA5551,
  988. DRM_FORMAT_ABGR8888,
  989. DRM_FORMAT_ARGB8888,
  990. /* new on Tegra114 */
  991. DRM_FORMAT_ABGR4444,
  992. DRM_FORMAT_ABGR1555,
  993. DRM_FORMAT_BGRA5551,
  994. DRM_FORMAT_XRGB1555,
  995. DRM_FORMAT_RGBX5551,
  996. DRM_FORMAT_XBGR1555,
  997. DRM_FORMAT_BGRX5551,
  998. DRM_FORMAT_BGR565,
  999. DRM_FORMAT_BGRA8888,
  1000. DRM_FORMAT_RGBA8888,
  1001. DRM_FORMAT_XRGB8888,
  1002. DRM_FORMAT_XBGR8888,
  1003. /* planar formats */
  1004. DRM_FORMAT_UYVY,
  1005. DRM_FORMAT_YUYV,
  1006. DRM_FORMAT_YUV420,
  1007. DRM_FORMAT_YUV422,
  1008. /* semi-planar formats */
  1009. DRM_FORMAT_NV12,
  1010. DRM_FORMAT_NV21,
  1011. DRM_FORMAT_NV16,
  1012. DRM_FORMAT_NV61,
  1013. DRM_FORMAT_NV24,
  1014. DRM_FORMAT_NV42,
  1015. };
  1016. static const u32 tegra124_overlay_formats[] = {
  1017. DRM_FORMAT_ARGB4444,
  1018. DRM_FORMAT_ARGB1555,
  1019. DRM_FORMAT_RGB565,
  1020. DRM_FORMAT_RGBA5551,
  1021. DRM_FORMAT_ABGR8888,
  1022. DRM_FORMAT_ARGB8888,
  1023. /* new on Tegra114 */
  1024. DRM_FORMAT_ABGR4444,
  1025. DRM_FORMAT_ABGR1555,
  1026. DRM_FORMAT_BGRA5551,
  1027. DRM_FORMAT_XRGB1555,
  1028. DRM_FORMAT_RGBX5551,
  1029. DRM_FORMAT_XBGR1555,
  1030. DRM_FORMAT_BGRX5551,
  1031. DRM_FORMAT_BGR565,
  1032. DRM_FORMAT_BGRA8888,
  1033. DRM_FORMAT_RGBA8888,
  1034. DRM_FORMAT_XRGB8888,
  1035. DRM_FORMAT_XBGR8888,
  1036. /* new on Tegra124 */
  1037. DRM_FORMAT_RGBX8888,
  1038. DRM_FORMAT_BGRX8888,
  1039. /* planar formats */
  1040. DRM_FORMAT_UYVY,
  1041. DRM_FORMAT_YUYV,
  1042. DRM_FORMAT_YVYU,
  1043. DRM_FORMAT_VYUY,
  1044. DRM_FORMAT_YUV420, /* YU12 */
  1045. DRM_FORMAT_YUV422, /* YU16 */
  1046. DRM_FORMAT_YUV444, /* YU24 */
  1047. /* semi-planar formats */
  1048. DRM_FORMAT_NV12,
  1049. DRM_FORMAT_NV21,
  1050. DRM_FORMAT_NV16,
  1051. DRM_FORMAT_NV61,
  1052. DRM_FORMAT_NV24,
  1053. DRM_FORMAT_NV42,
  1054. };
  1055. static struct drm_plane *tegra_dc_overlay_plane_create(struct drm_device *drm,
  1056. struct tegra_dc *dc,
  1057. unsigned int index,
  1058. bool cursor)
  1059. {
  1060. unsigned long possible_crtcs = tegra_plane_get_possible_crtcs(drm);
  1061. struct tegra_plane *plane;
  1062. unsigned int num_formats;
  1063. enum drm_plane_type type;
  1064. const u32 *formats;
  1065. int err;
  1066. plane = kzalloc_obj(*plane);
  1067. if (!plane)
  1068. return ERR_PTR(-ENOMEM);
  1069. plane->offset = 0xa00 + 0x200 * index;
  1070. plane->index = index;
  1071. plane->dc = dc;
  1072. num_formats = dc->soc->num_overlay_formats;
  1073. formats = dc->soc->overlay_formats;
  1074. err = tegra_plane_interconnect_init(plane);
  1075. if (err) {
  1076. kfree(plane);
  1077. return ERR_PTR(err);
  1078. }
  1079. if (!cursor)
  1080. type = DRM_PLANE_TYPE_OVERLAY;
  1081. else
  1082. type = DRM_PLANE_TYPE_CURSOR;
  1083. err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
  1084. &tegra_plane_funcs, formats,
  1085. num_formats, linear_modifiers,
  1086. type, NULL);
  1087. if (err < 0) {
  1088. kfree(plane);
  1089. return ERR_PTR(err);
  1090. }
  1091. drm_plane_helper_add(&plane->base, &tegra_plane_helper_funcs);
  1092. drm_plane_create_zpos_property(&plane->base, plane->index, 0, 255);
  1093. err = drm_plane_create_rotation_property(&plane->base,
  1094. DRM_MODE_ROTATE_0,
  1095. DRM_MODE_ROTATE_0 |
  1096. DRM_MODE_ROTATE_180 |
  1097. DRM_MODE_REFLECT_X |
  1098. DRM_MODE_REFLECT_Y);
  1099. if (err < 0)
  1100. dev_err(dc->dev, "failed to create rotation property: %d\n",
  1101. err);
  1102. return &plane->base;
  1103. }
  1104. static struct drm_plane *tegra_dc_add_shared_planes(struct drm_device *drm,
  1105. struct tegra_dc *dc)
  1106. {
  1107. struct drm_plane *plane, *primary = NULL;
  1108. unsigned int i, j;
  1109. for (i = 0; i < dc->soc->num_wgrps; i++) {
  1110. const struct tegra_windowgroup_soc *wgrp = &dc->soc->wgrps[i];
  1111. if (wgrp->dc == dc->pipe) {
  1112. for (j = 0; j < wgrp->num_windows; j++) {
  1113. unsigned int index = wgrp->windows[j];
  1114. enum drm_plane_type type;
  1115. if (primary)
  1116. type = DRM_PLANE_TYPE_OVERLAY;
  1117. else
  1118. type = DRM_PLANE_TYPE_PRIMARY;
  1119. plane = tegra_shared_plane_create(drm, dc,
  1120. wgrp->index,
  1121. index, type);
  1122. if (IS_ERR(plane))
  1123. return plane;
  1124. /*
  1125. * Choose the first shared plane owned by this
  1126. * head as the primary plane.
  1127. */
  1128. if (!primary)
  1129. primary = plane;
  1130. }
  1131. }
  1132. }
  1133. return primary;
  1134. }
  1135. static struct drm_plane *tegra_dc_add_planes(struct drm_device *drm,
  1136. struct tegra_dc *dc)
  1137. {
  1138. struct drm_plane *planes[2], *primary;
  1139. unsigned int planes_num;
  1140. unsigned int i;
  1141. int err;
  1142. primary = tegra_primary_plane_create(drm, dc);
  1143. if (IS_ERR(primary))
  1144. return primary;
  1145. if (dc->soc->supports_cursor)
  1146. planes_num = 2;
  1147. else
  1148. planes_num = 1;
  1149. for (i = 0; i < planes_num; i++) {
  1150. planes[i] = tegra_dc_overlay_plane_create(drm, dc, 1 + i,
  1151. false);
  1152. if (IS_ERR(planes[i])) {
  1153. err = PTR_ERR(planes[i]);
  1154. while (i--)
  1155. planes[i]->funcs->destroy(planes[i]);
  1156. primary->funcs->destroy(primary);
  1157. return ERR_PTR(err);
  1158. }
  1159. }
  1160. return primary;
  1161. }
  1162. static void tegra_dc_destroy(struct drm_crtc *crtc)
  1163. {
  1164. drm_crtc_cleanup(crtc);
  1165. }
  1166. static void tegra_crtc_reset(struct drm_crtc *crtc)
  1167. {
  1168. struct tegra_dc_state *state = kzalloc_obj(*state);
  1169. if (crtc->state)
  1170. tegra_crtc_atomic_destroy_state(crtc, crtc->state);
  1171. if (state)
  1172. __drm_atomic_helper_crtc_reset(crtc, &state->base);
  1173. else
  1174. __drm_atomic_helper_crtc_reset(crtc, NULL);
  1175. }
  1176. static struct drm_crtc_state *
  1177. tegra_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
  1178. {
  1179. struct tegra_dc_state *state = to_dc_state(crtc->state);
  1180. struct tegra_dc_state *copy;
  1181. copy = kmalloc_obj(*copy);
  1182. if (!copy)
  1183. return NULL;
  1184. __drm_atomic_helper_crtc_duplicate_state(crtc, &copy->base);
  1185. copy->clk = state->clk;
  1186. copy->pclk = state->pclk;
  1187. copy->div = state->div;
  1188. copy->planes = state->planes;
  1189. return &copy->base;
  1190. }
  1191. static void tegra_crtc_atomic_destroy_state(struct drm_crtc *crtc,
  1192. struct drm_crtc_state *state)
  1193. {
  1194. __drm_atomic_helper_crtc_destroy_state(state);
  1195. kfree(state);
  1196. }
  1197. #define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
  1198. static const struct debugfs_reg32 tegra_dc_regs[] = {
  1199. DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT),
  1200. DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL),
  1201. DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT_ERROR),
  1202. DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT),
  1203. DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL),
  1204. DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT_ERROR),
  1205. DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT),
  1206. DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL),
  1207. DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT_ERROR),
  1208. DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT),
  1209. DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL),
  1210. DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT_ERROR),
  1211. DEBUGFS_REG32(DC_CMD_CONT_SYNCPT_VSYNC),
  1212. DEBUGFS_REG32(DC_CMD_DISPLAY_COMMAND_OPTION0),
  1213. DEBUGFS_REG32(DC_CMD_DISPLAY_COMMAND),
  1214. DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE),
  1215. DEBUGFS_REG32(DC_CMD_DISPLAY_POWER_CONTROL),
  1216. DEBUGFS_REG32(DC_CMD_INT_STATUS),
  1217. DEBUGFS_REG32(DC_CMD_INT_MASK),
  1218. DEBUGFS_REG32(DC_CMD_INT_ENABLE),
  1219. DEBUGFS_REG32(DC_CMD_INT_TYPE),
  1220. DEBUGFS_REG32(DC_CMD_INT_POLARITY),
  1221. DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE1),
  1222. DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE2),
  1223. DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE3),
  1224. DEBUGFS_REG32(DC_CMD_STATE_ACCESS),
  1225. DEBUGFS_REG32(DC_CMD_STATE_CONTROL),
  1226. DEBUGFS_REG32(DC_CMD_DISPLAY_WINDOW_HEADER),
  1227. DEBUGFS_REG32(DC_CMD_REG_ACT_CONTROL),
  1228. DEBUGFS_REG32(DC_COM_CRC_CONTROL),
  1229. DEBUGFS_REG32(DC_COM_CRC_CHECKSUM),
  1230. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(0)),
  1231. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(1)),
  1232. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(2)),
  1233. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(3)),
  1234. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(0)),
  1235. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(1)),
  1236. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(2)),
  1237. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(3)),
  1238. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(0)),
  1239. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(1)),
  1240. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(2)),
  1241. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(3)),
  1242. DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(0)),
  1243. DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(1)),
  1244. DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(2)),
  1245. DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(3)),
  1246. DEBUGFS_REG32(DC_COM_PIN_INPUT_DATA(0)),
  1247. DEBUGFS_REG32(DC_COM_PIN_INPUT_DATA(1)),
  1248. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(0)),
  1249. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(1)),
  1250. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(2)),
  1251. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(3)),
  1252. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(4)),
  1253. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(5)),
  1254. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(6)),
  1255. DEBUGFS_REG32(DC_COM_PIN_MISC_CONTROL),
  1256. DEBUGFS_REG32(DC_COM_PIN_PM0_CONTROL),
  1257. DEBUGFS_REG32(DC_COM_PIN_PM0_DUTY_CYCLE),
  1258. DEBUGFS_REG32(DC_COM_PIN_PM1_CONTROL),
  1259. DEBUGFS_REG32(DC_COM_PIN_PM1_DUTY_CYCLE),
  1260. DEBUGFS_REG32(DC_COM_SPI_CONTROL),
  1261. DEBUGFS_REG32(DC_COM_SPI_START_BYTE),
  1262. DEBUGFS_REG32(DC_COM_HSPI_WRITE_DATA_AB),
  1263. DEBUGFS_REG32(DC_COM_HSPI_WRITE_DATA_CD),
  1264. DEBUGFS_REG32(DC_COM_HSPI_CS_DC),
  1265. DEBUGFS_REG32(DC_COM_SCRATCH_REGISTER_A),
  1266. DEBUGFS_REG32(DC_COM_SCRATCH_REGISTER_B),
  1267. DEBUGFS_REG32(DC_COM_GPIO_CTRL),
  1268. DEBUGFS_REG32(DC_COM_GPIO_DEBOUNCE_COUNTER),
  1269. DEBUGFS_REG32(DC_COM_CRC_CHECKSUM_LATCHED),
  1270. DEBUGFS_REG32(DC_DISP_DISP_SIGNAL_OPTIONS0),
  1271. DEBUGFS_REG32(DC_DISP_DISP_SIGNAL_OPTIONS1),
  1272. DEBUGFS_REG32(DC_DISP_DISP_WIN_OPTIONS),
  1273. DEBUGFS_REG32(DC_DISP_DISP_MEM_HIGH_PRIORITY),
  1274. DEBUGFS_REG32(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER),
  1275. DEBUGFS_REG32(DC_DISP_DISP_TIMING_OPTIONS),
  1276. DEBUGFS_REG32(DC_DISP_REF_TO_SYNC),
  1277. DEBUGFS_REG32(DC_DISP_SYNC_WIDTH),
  1278. DEBUGFS_REG32(DC_DISP_BACK_PORCH),
  1279. DEBUGFS_REG32(DC_DISP_ACTIVE),
  1280. DEBUGFS_REG32(DC_DISP_FRONT_PORCH),
  1281. DEBUGFS_REG32(DC_DISP_H_PULSE0_CONTROL),
  1282. DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_A),
  1283. DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_B),
  1284. DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_C),
  1285. DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_D),
  1286. DEBUGFS_REG32(DC_DISP_H_PULSE1_CONTROL),
  1287. DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_A),
  1288. DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_B),
  1289. DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_C),
  1290. DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_D),
  1291. DEBUGFS_REG32(DC_DISP_H_PULSE2_CONTROL),
  1292. DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_A),
  1293. DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_B),
  1294. DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_C),
  1295. DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_D),
  1296. DEBUGFS_REG32(DC_DISP_V_PULSE0_CONTROL),
  1297. DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_A),
  1298. DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_B),
  1299. DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_C),
  1300. DEBUGFS_REG32(DC_DISP_V_PULSE1_CONTROL),
  1301. DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_A),
  1302. DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_B),
  1303. DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_C),
  1304. DEBUGFS_REG32(DC_DISP_V_PULSE2_CONTROL),
  1305. DEBUGFS_REG32(DC_DISP_V_PULSE2_POSITION_A),
  1306. DEBUGFS_REG32(DC_DISP_V_PULSE3_CONTROL),
  1307. DEBUGFS_REG32(DC_DISP_V_PULSE3_POSITION_A),
  1308. DEBUGFS_REG32(DC_DISP_M0_CONTROL),
  1309. DEBUGFS_REG32(DC_DISP_M1_CONTROL),
  1310. DEBUGFS_REG32(DC_DISP_DI_CONTROL),
  1311. DEBUGFS_REG32(DC_DISP_PP_CONTROL),
  1312. DEBUGFS_REG32(DC_DISP_PP_SELECT_A),
  1313. DEBUGFS_REG32(DC_DISP_PP_SELECT_B),
  1314. DEBUGFS_REG32(DC_DISP_PP_SELECT_C),
  1315. DEBUGFS_REG32(DC_DISP_PP_SELECT_D),
  1316. DEBUGFS_REG32(DC_DISP_DISP_CLOCK_CONTROL),
  1317. DEBUGFS_REG32(DC_DISP_DISP_INTERFACE_CONTROL),
  1318. DEBUGFS_REG32(DC_DISP_DISP_COLOR_CONTROL),
  1319. DEBUGFS_REG32(DC_DISP_SHIFT_CLOCK_OPTIONS),
  1320. DEBUGFS_REG32(DC_DISP_DATA_ENABLE_OPTIONS),
  1321. DEBUGFS_REG32(DC_DISP_SERIAL_INTERFACE_OPTIONS),
  1322. DEBUGFS_REG32(DC_DISP_LCD_SPI_OPTIONS),
  1323. DEBUGFS_REG32(DC_DISP_BORDER_COLOR),
  1324. DEBUGFS_REG32(DC_DISP_COLOR_KEY0_LOWER),
  1325. DEBUGFS_REG32(DC_DISP_COLOR_KEY0_UPPER),
  1326. DEBUGFS_REG32(DC_DISP_COLOR_KEY1_LOWER),
  1327. DEBUGFS_REG32(DC_DISP_COLOR_KEY1_UPPER),
  1328. DEBUGFS_REG32(DC_DISP_CURSOR_FOREGROUND),
  1329. DEBUGFS_REG32(DC_DISP_CURSOR_BACKGROUND),
  1330. DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR),
  1331. DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR_NS),
  1332. DEBUGFS_REG32(DC_DISP_CURSOR_POSITION),
  1333. DEBUGFS_REG32(DC_DISP_CURSOR_POSITION_NS),
  1334. DEBUGFS_REG32(DC_DISP_INIT_SEQ_CONTROL),
  1335. DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_A),
  1336. DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_B),
  1337. DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_C),
  1338. DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_D),
  1339. DEBUGFS_REG32(DC_DISP_DC_MCCIF_FIFOCTRL),
  1340. DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY0A_HYST),
  1341. DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY0B_HYST),
  1342. DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY1A_HYST),
  1343. DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY1B_HYST),
  1344. DEBUGFS_REG32(DC_DISP_DAC_CRT_CTRL),
  1345. DEBUGFS_REG32(DC_DISP_DISP_MISC_CONTROL),
  1346. DEBUGFS_REG32(DC_DISP_SD_CONTROL),
  1347. DEBUGFS_REG32(DC_DISP_SD_CSC_COEFF),
  1348. DEBUGFS_REG32(DC_DISP_SD_LUT(0)),
  1349. DEBUGFS_REG32(DC_DISP_SD_LUT(1)),
  1350. DEBUGFS_REG32(DC_DISP_SD_LUT(2)),
  1351. DEBUGFS_REG32(DC_DISP_SD_LUT(3)),
  1352. DEBUGFS_REG32(DC_DISP_SD_LUT(4)),
  1353. DEBUGFS_REG32(DC_DISP_SD_LUT(5)),
  1354. DEBUGFS_REG32(DC_DISP_SD_LUT(6)),
  1355. DEBUGFS_REG32(DC_DISP_SD_LUT(7)),
  1356. DEBUGFS_REG32(DC_DISP_SD_LUT(8)),
  1357. DEBUGFS_REG32(DC_DISP_SD_FLICKER_CONTROL),
  1358. DEBUGFS_REG32(DC_DISP_DC_PIXEL_COUNT),
  1359. DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(0)),
  1360. DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(1)),
  1361. DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(2)),
  1362. DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(3)),
  1363. DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(4)),
  1364. DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(5)),
  1365. DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(6)),
  1366. DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(7)),
  1367. DEBUGFS_REG32(DC_DISP_SD_BL_TF(0)),
  1368. DEBUGFS_REG32(DC_DISP_SD_BL_TF(1)),
  1369. DEBUGFS_REG32(DC_DISP_SD_BL_TF(2)),
  1370. DEBUGFS_REG32(DC_DISP_SD_BL_TF(3)),
  1371. DEBUGFS_REG32(DC_DISP_SD_BL_CONTROL),
  1372. DEBUGFS_REG32(DC_DISP_SD_HW_K_VALUES),
  1373. DEBUGFS_REG32(DC_DISP_SD_MAN_K_VALUES),
  1374. DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR_HI),
  1375. DEBUGFS_REG32(DC_DISP_BLEND_CURSOR_CONTROL),
  1376. DEBUGFS_REG32(DC_WIN_WIN_OPTIONS),
  1377. DEBUGFS_REG32(DC_WIN_BYTE_SWAP),
  1378. DEBUGFS_REG32(DC_WIN_BUFFER_CONTROL),
  1379. DEBUGFS_REG32(DC_WIN_COLOR_DEPTH),
  1380. DEBUGFS_REG32(DC_WIN_POSITION),
  1381. DEBUGFS_REG32(DC_WIN_SIZE),
  1382. DEBUGFS_REG32(DC_WIN_PRESCALED_SIZE),
  1383. DEBUGFS_REG32(DC_WIN_H_INITIAL_DDA),
  1384. DEBUGFS_REG32(DC_WIN_V_INITIAL_DDA),
  1385. DEBUGFS_REG32(DC_WIN_DDA_INC),
  1386. DEBUGFS_REG32(DC_WIN_LINE_STRIDE),
  1387. DEBUGFS_REG32(DC_WIN_BUF_STRIDE),
  1388. DEBUGFS_REG32(DC_WIN_UV_BUF_STRIDE),
  1389. DEBUGFS_REG32(DC_WIN_BUFFER_ADDR_MODE),
  1390. DEBUGFS_REG32(DC_WIN_DV_CONTROL),
  1391. DEBUGFS_REG32(DC_WIN_BLEND_NOKEY),
  1392. DEBUGFS_REG32(DC_WIN_BLEND_1WIN),
  1393. DEBUGFS_REG32(DC_WIN_BLEND_2WIN_X),
  1394. DEBUGFS_REG32(DC_WIN_BLEND_2WIN_Y),
  1395. DEBUGFS_REG32(DC_WIN_BLEND_3WIN_XY),
  1396. DEBUGFS_REG32(DC_WIN_HP_FETCH_CONTROL),
  1397. DEBUGFS_REG32(DC_WINBUF_START_ADDR),
  1398. DEBUGFS_REG32(DC_WINBUF_START_ADDR_NS),
  1399. DEBUGFS_REG32(DC_WINBUF_START_ADDR_U),
  1400. DEBUGFS_REG32(DC_WINBUF_START_ADDR_U_NS),
  1401. DEBUGFS_REG32(DC_WINBUF_START_ADDR_V),
  1402. DEBUGFS_REG32(DC_WINBUF_START_ADDR_V_NS),
  1403. DEBUGFS_REG32(DC_WINBUF_ADDR_H_OFFSET),
  1404. DEBUGFS_REG32(DC_WINBUF_ADDR_H_OFFSET_NS),
  1405. DEBUGFS_REG32(DC_WINBUF_ADDR_V_OFFSET),
  1406. DEBUGFS_REG32(DC_WINBUF_ADDR_V_OFFSET_NS),
  1407. DEBUGFS_REG32(DC_WINBUF_UFLOW_STATUS),
  1408. DEBUGFS_REG32(DC_WINBUF_AD_UFLOW_STATUS),
  1409. DEBUGFS_REG32(DC_WINBUF_BD_UFLOW_STATUS),
  1410. DEBUGFS_REG32(DC_WINBUF_CD_UFLOW_STATUS),
  1411. };
  1412. static int tegra_dc_show_regs(struct seq_file *s, void *data)
  1413. {
  1414. struct drm_info_node *node = s->private;
  1415. struct tegra_dc *dc = node->info_ent->data;
  1416. unsigned int i;
  1417. int err = 0;
  1418. drm_modeset_lock(&dc->base.mutex, NULL);
  1419. if (!dc->base.state->active) {
  1420. err = -EBUSY;
  1421. goto unlock;
  1422. }
  1423. for (i = 0; i < ARRAY_SIZE(tegra_dc_regs); i++) {
  1424. unsigned int offset = tegra_dc_regs[i].offset;
  1425. seq_printf(s, "%-40s %#05x %08x\n", tegra_dc_regs[i].name,
  1426. offset, tegra_dc_readl(dc, offset));
  1427. }
  1428. unlock:
  1429. drm_modeset_unlock(&dc->base.mutex);
  1430. return err;
  1431. }
  1432. static int tegra_dc_show_crc(struct seq_file *s, void *data)
  1433. {
  1434. struct drm_info_node *node = s->private;
  1435. struct tegra_dc *dc = node->info_ent->data;
  1436. int err = 0;
  1437. u32 value;
  1438. drm_modeset_lock(&dc->base.mutex, NULL);
  1439. if (!dc->base.state->active) {
  1440. err = -EBUSY;
  1441. goto unlock;
  1442. }
  1443. value = DC_COM_CRC_CONTROL_ACTIVE_DATA | DC_COM_CRC_CONTROL_ENABLE;
  1444. tegra_dc_writel(dc, value, DC_COM_CRC_CONTROL);
  1445. tegra_dc_commit(dc);
  1446. drm_crtc_wait_one_vblank(&dc->base);
  1447. drm_crtc_wait_one_vblank(&dc->base);
  1448. value = tegra_dc_readl(dc, DC_COM_CRC_CHECKSUM);
  1449. seq_printf(s, "%08x\n", value);
  1450. tegra_dc_writel(dc, 0, DC_COM_CRC_CONTROL);
  1451. unlock:
  1452. drm_modeset_unlock(&dc->base.mutex);
  1453. return err;
  1454. }
  1455. static int tegra_dc_show_stats(struct seq_file *s, void *data)
  1456. {
  1457. struct drm_info_node *node = s->private;
  1458. struct tegra_dc *dc = node->info_ent->data;
  1459. seq_printf(s, "frames: %lu\n", dc->stats.frames);
  1460. seq_printf(s, "vblank: %lu\n", dc->stats.vblank);
  1461. seq_printf(s, "underflow: %lu\n", dc->stats.underflow);
  1462. seq_printf(s, "overflow: %lu\n", dc->stats.overflow);
  1463. seq_printf(s, "frames total: %lu\n", dc->stats.frames_total);
  1464. seq_printf(s, "vblank total: %lu\n", dc->stats.vblank_total);
  1465. seq_printf(s, "underflow total: %lu\n", dc->stats.underflow_total);
  1466. seq_printf(s, "overflow total: %lu\n", dc->stats.overflow_total);
  1467. return 0;
  1468. }
  1469. static struct drm_info_list debugfs_files[] = {
  1470. { "regs", tegra_dc_show_regs, 0, NULL },
  1471. { "crc", tegra_dc_show_crc, 0, NULL },
  1472. { "stats", tegra_dc_show_stats, 0, NULL },
  1473. };
  1474. static int tegra_dc_late_register(struct drm_crtc *crtc)
  1475. {
  1476. unsigned int i, count = ARRAY_SIZE(debugfs_files);
  1477. struct drm_minor *minor = crtc->dev->primary;
  1478. struct dentry *root;
  1479. struct tegra_dc *dc = to_tegra_dc(crtc);
  1480. #ifdef CONFIG_DEBUG_FS
  1481. root = crtc->debugfs_entry;
  1482. #else
  1483. root = NULL;
  1484. #endif
  1485. dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
  1486. GFP_KERNEL);
  1487. if (!dc->debugfs_files)
  1488. return -ENOMEM;
  1489. for (i = 0; i < count; i++)
  1490. dc->debugfs_files[i].data = dc;
  1491. drm_debugfs_create_files(dc->debugfs_files, count, root, minor);
  1492. return 0;
  1493. }
  1494. static void tegra_dc_early_unregister(struct drm_crtc *crtc)
  1495. {
  1496. unsigned int count = ARRAY_SIZE(debugfs_files);
  1497. struct drm_minor *minor = crtc->dev->primary;
  1498. struct tegra_dc *dc = to_tegra_dc(crtc);
  1499. struct dentry *root;
  1500. #ifdef CONFIG_DEBUG_FS
  1501. root = crtc->debugfs_entry;
  1502. #else
  1503. root = NULL;
  1504. #endif
  1505. drm_debugfs_remove_files(dc->debugfs_files, count, root, minor);
  1506. kfree(dc->debugfs_files);
  1507. dc->debugfs_files = NULL;
  1508. }
  1509. static u32 tegra_dc_get_vblank_counter(struct drm_crtc *crtc)
  1510. {
  1511. struct tegra_dc *dc = to_tegra_dc(crtc);
  1512. /* XXX vblank syncpoints don't work with nvdisplay yet */
  1513. if (dc->syncpt && !dc->soc->has_nvdisplay)
  1514. return host1x_syncpt_read(dc->syncpt);
  1515. /* fallback to software emulated VBLANK counter */
  1516. return (u32)drm_crtc_vblank_count(&dc->base);
  1517. }
  1518. static int tegra_dc_enable_vblank(struct drm_crtc *crtc)
  1519. {
  1520. struct tegra_dc *dc = to_tegra_dc(crtc);
  1521. u32 value;
  1522. value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
  1523. value |= VBLANK_INT;
  1524. tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
  1525. return 0;
  1526. }
  1527. static void tegra_dc_disable_vblank(struct drm_crtc *crtc)
  1528. {
  1529. struct tegra_dc *dc = to_tegra_dc(crtc);
  1530. u32 value;
  1531. value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
  1532. value &= ~VBLANK_INT;
  1533. tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
  1534. }
  1535. static const struct drm_crtc_funcs tegra_crtc_funcs = {
  1536. .page_flip = drm_atomic_helper_page_flip,
  1537. .set_config = drm_atomic_helper_set_config,
  1538. .destroy = tegra_dc_destroy,
  1539. .reset = tegra_crtc_reset,
  1540. .atomic_duplicate_state = tegra_crtc_atomic_duplicate_state,
  1541. .atomic_destroy_state = tegra_crtc_atomic_destroy_state,
  1542. .late_register = tegra_dc_late_register,
  1543. .early_unregister = tegra_dc_early_unregister,
  1544. .get_vblank_counter = tegra_dc_get_vblank_counter,
  1545. .enable_vblank = tegra_dc_enable_vblank,
  1546. .disable_vblank = tegra_dc_disable_vblank,
  1547. };
  1548. static int tegra_dc_set_timings(struct tegra_dc *dc,
  1549. struct drm_display_mode *mode)
  1550. {
  1551. unsigned int h_ref_to_sync = 1;
  1552. unsigned int v_ref_to_sync = 1;
  1553. unsigned long value;
  1554. if (!dc->soc->has_nvdisplay) {
  1555. tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
  1556. value = (v_ref_to_sync << 16) | h_ref_to_sync;
  1557. tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
  1558. }
  1559. value = ((mode->vsync_end - mode->vsync_start) << 16) |
  1560. ((mode->hsync_end - mode->hsync_start) << 0);
  1561. tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
  1562. value = ((mode->vtotal - mode->vsync_end) << 16) |
  1563. ((mode->htotal - mode->hsync_end) << 0);
  1564. tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
  1565. value = ((mode->vsync_start - mode->vdisplay) << 16) |
  1566. ((mode->hsync_start - mode->hdisplay) << 0);
  1567. tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
  1568. value = (mode->vdisplay << 16) | mode->hdisplay;
  1569. tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
  1570. return 0;
  1571. }
  1572. /**
  1573. * tegra_dc_state_setup_clock - check clock settings and store them in atomic
  1574. * state
  1575. * @dc: display controller
  1576. * @crtc_state: CRTC atomic state
  1577. * @clk: parent clock for display controller
  1578. * @pclk: pixel clock
  1579. * @div: shift clock divider
  1580. *
  1581. * Returns:
  1582. * 0 on success or a negative error-code on failure.
  1583. */
  1584. int tegra_dc_state_setup_clock(struct tegra_dc *dc,
  1585. struct drm_crtc_state *crtc_state,
  1586. struct clk *clk, unsigned long pclk,
  1587. unsigned int div)
  1588. {
  1589. struct tegra_dc_state *state = to_dc_state(crtc_state);
  1590. if (!clk_has_parent(dc->clk, clk))
  1591. return -EINVAL;
  1592. state->clk = clk;
  1593. state->pclk = pclk;
  1594. state->div = div;
  1595. return 0;
  1596. }
  1597. static void tegra_dc_update_voltage_state(struct tegra_dc *dc,
  1598. struct tegra_dc_state *state)
  1599. {
  1600. unsigned long rate, pstate;
  1601. struct dev_pm_opp *opp;
  1602. int err;
  1603. if (!dc->has_opp_table)
  1604. return;
  1605. /* calculate actual pixel clock rate which depends on internal divider */
  1606. rate = DIV_ROUND_UP(clk_get_rate(dc->clk) * 2, state->div + 2);
  1607. /* find suitable OPP for the rate */
  1608. opp = dev_pm_opp_find_freq_ceil(dc->dev, &rate);
  1609. /*
  1610. * Very high resolution modes may results in a clock rate that is
  1611. * above the characterized maximum. In this case it's okay to fall
  1612. * back to the characterized maximum.
  1613. */
  1614. if (opp == ERR_PTR(-ERANGE))
  1615. opp = dev_pm_opp_find_freq_floor(dc->dev, &rate);
  1616. if (IS_ERR(opp)) {
  1617. dev_err(dc->dev, "failed to find OPP for %luHz: %pe\n",
  1618. rate, opp);
  1619. return;
  1620. }
  1621. pstate = dev_pm_opp_get_required_pstate(opp, 0);
  1622. dev_pm_opp_put(opp);
  1623. /*
  1624. * The minimum core voltage depends on the pixel clock rate (which
  1625. * depends on internal clock divider of the CRTC) and not on the
  1626. * rate of the display controller clock. This is why we're not using
  1627. * dev_pm_opp_set_rate() API and instead controlling the power domain
  1628. * directly.
  1629. */
  1630. err = dev_pm_genpd_set_performance_state(dc->dev, pstate);
  1631. if (err)
  1632. dev_err(dc->dev, "failed to set power domain state to %lu: %d\n",
  1633. pstate, err);
  1634. }
  1635. static void tegra_dc_set_clock_rate(struct tegra_dc *dc,
  1636. struct tegra_dc_state *state)
  1637. {
  1638. int err;
  1639. err = clk_set_parent(dc->clk, state->clk);
  1640. if (err < 0)
  1641. dev_err(dc->dev, "failed to set parent clock: %d\n", err);
  1642. /*
  1643. * Outputs may not want to change the parent clock rate. This is only
  1644. * relevant to Tegra20 where only a single display PLL is available.
  1645. * Since that PLL would typically be used for HDMI, an internal LVDS
  1646. * panel would need to be driven by some other clock such as PLL_P
  1647. * which is shared with other peripherals. Changing the clock rate
  1648. * should therefore be avoided.
  1649. */
  1650. if (state->pclk > 0) {
  1651. err = clk_set_rate(state->clk, state->pclk);
  1652. if (err < 0)
  1653. dev_err(dc->dev,
  1654. "failed to set clock rate to %lu Hz\n",
  1655. state->pclk);
  1656. err = clk_set_rate(dc->clk, state->pclk);
  1657. if (err < 0)
  1658. dev_err(dc->dev, "failed to set clock %pC to %lu Hz: %d\n",
  1659. dc->clk, state->pclk, err);
  1660. }
  1661. DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk),
  1662. state->div);
  1663. DRM_DEBUG_KMS("pclk: %lu\n", state->pclk);
  1664. tegra_dc_update_voltage_state(dc, state);
  1665. }
  1666. static void tegra_dc_stop(struct tegra_dc *dc)
  1667. {
  1668. u32 value;
  1669. /* stop the display controller */
  1670. value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
  1671. value &= ~DISP_CTRL_MODE_MASK;
  1672. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
  1673. tegra_dc_commit(dc);
  1674. }
  1675. static bool tegra_dc_idle(struct tegra_dc *dc)
  1676. {
  1677. u32 value;
  1678. value = tegra_dc_readl_active(dc, DC_CMD_DISPLAY_COMMAND);
  1679. return (value & DISP_CTRL_MODE_MASK) == 0;
  1680. }
  1681. static int tegra_dc_wait_idle(struct tegra_dc *dc, unsigned long timeout)
  1682. {
  1683. timeout = jiffies + msecs_to_jiffies(timeout);
  1684. while (time_before(jiffies, timeout)) {
  1685. if (tegra_dc_idle(dc))
  1686. return 0;
  1687. usleep_range(1000, 2000);
  1688. }
  1689. dev_dbg(dc->dev, "timeout waiting for DC to become idle\n");
  1690. return -ETIMEDOUT;
  1691. }
  1692. static void
  1693. tegra_crtc_update_memory_bandwidth(struct drm_crtc *crtc,
  1694. struct drm_atomic_state *state,
  1695. bool prepare_bandwidth_transition)
  1696. {
  1697. const struct tegra_plane_state *old_tegra_state, *new_tegra_state;
  1698. u32 i, new_avg_bw, old_avg_bw, new_peak_bw, old_peak_bw;
  1699. const struct drm_plane_state *old_plane_state;
  1700. const struct drm_crtc_state *old_crtc_state;
  1701. struct tegra_dc_window window, old_window;
  1702. struct tegra_dc *dc = to_tegra_dc(crtc);
  1703. struct tegra_plane *tegra;
  1704. struct drm_plane *plane;
  1705. if (dc->soc->has_nvdisplay)
  1706. return;
  1707. old_crtc_state = drm_atomic_get_old_crtc_state(state, crtc);
  1708. if (!crtc->state->active) {
  1709. if (!old_crtc_state->active)
  1710. return;
  1711. /*
  1712. * When CRTC is disabled on DPMS, the state of attached planes
  1713. * is kept unchanged. Hence we need to enforce removal of the
  1714. * bandwidths from the ICC paths.
  1715. */
  1716. drm_atomic_crtc_for_each_plane(plane, crtc) {
  1717. tegra = to_tegra_plane(plane);
  1718. icc_set_bw(tegra->icc_mem, 0, 0);
  1719. icc_set_bw(tegra->icc_mem_vfilter, 0, 0);
  1720. }
  1721. return;
  1722. }
  1723. for_each_old_plane_in_state(old_crtc_state->state, plane,
  1724. old_plane_state, i) {
  1725. old_tegra_state = to_const_tegra_plane_state(old_plane_state);
  1726. new_tegra_state = to_const_tegra_plane_state(plane->state);
  1727. tegra = to_tegra_plane(plane);
  1728. /*
  1729. * We're iterating over the global atomic state and it contains
  1730. * planes from another CRTC, hence we need to filter out the
  1731. * planes unrelated to this CRTC.
  1732. */
  1733. if (tegra->dc != dc)
  1734. continue;
  1735. new_avg_bw = new_tegra_state->avg_memory_bandwidth;
  1736. old_avg_bw = old_tegra_state->avg_memory_bandwidth;
  1737. new_peak_bw = new_tegra_state->total_peak_memory_bandwidth;
  1738. old_peak_bw = old_tegra_state->total_peak_memory_bandwidth;
  1739. /*
  1740. * See the comment related to !crtc->state->active above,
  1741. * which explains why bandwidths need to be updated when
  1742. * CRTC is turning ON.
  1743. */
  1744. if (new_avg_bw == old_avg_bw && new_peak_bw == old_peak_bw &&
  1745. old_crtc_state->active)
  1746. continue;
  1747. window.src.h = drm_rect_height(&plane->state->src) >> 16;
  1748. window.dst.h = drm_rect_height(&plane->state->dst);
  1749. old_window.src.h = drm_rect_height(&old_plane_state->src) >> 16;
  1750. old_window.dst.h = drm_rect_height(&old_plane_state->dst);
  1751. /*
  1752. * During the preparation phase (atomic_begin), the memory
  1753. * freq should go high before the DC changes are committed
  1754. * if bandwidth requirement goes up, otherwise memory freq
  1755. * should to stay high if BW requirement goes down. The
  1756. * opposite applies to the completion phase (post_commit).
  1757. */
  1758. if (prepare_bandwidth_transition) {
  1759. new_avg_bw = max(old_avg_bw, new_avg_bw);
  1760. new_peak_bw = max(old_peak_bw, new_peak_bw);
  1761. if (tegra_plane_use_vertical_filtering(tegra, &old_window))
  1762. window = old_window;
  1763. }
  1764. icc_set_bw(tegra->icc_mem, new_avg_bw, new_peak_bw);
  1765. if (tegra_plane_use_vertical_filtering(tegra, &window))
  1766. icc_set_bw(tegra->icc_mem_vfilter, new_avg_bw, new_peak_bw);
  1767. else
  1768. icc_set_bw(tegra->icc_mem_vfilter, 0, 0);
  1769. }
  1770. }
  1771. static void tegra_crtc_atomic_disable(struct drm_crtc *crtc,
  1772. struct drm_atomic_state *state)
  1773. {
  1774. struct tegra_dc *dc = to_tegra_dc(crtc);
  1775. u32 value;
  1776. int err;
  1777. if (!tegra_dc_idle(dc)) {
  1778. tegra_dc_stop(dc);
  1779. /*
  1780. * Ignore the return value, there isn't anything useful to do
  1781. * in case this fails.
  1782. */
  1783. tegra_dc_wait_idle(dc, 100);
  1784. }
  1785. /*
  1786. * This should really be part of the RGB encoder driver, but clearing
  1787. * these bits has the side-effect of stopping the display controller.
  1788. * When that happens no VBLANK interrupts will be raised. At the same
  1789. * time the encoder is disabled before the display controller, so the
  1790. * above code is always going to timeout waiting for the controller
  1791. * to go idle.
  1792. *
  1793. * Given the close coupling between the RGB encoder and the display
  1794. * controller doing it here is still kind of okay. None of the other
  1795. * encoder drivers require these bits to be cleared.
  1796. *
  1797. * XXX: Perhaps given that the display controller is switched off at
  1798. * this point anyway maybe clearing these bits isn't even useful for
  1799. * the RGB encoder?
  1800. */
  1801. if (dc->rgb) {
  1802. value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
  1803. value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
  1804. PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
  1805. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
  1806. }
  1807. tegra_dc_stats_reset(&dc->stats);
  1808. drm_crtc_vblank_off(crtc);
  1809. spin_lock_irq(&crtc->dev->event_lock);
  1810. if (crtc->state->event) {
  1811. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  1812. crtc->state->event = NULL;
  1813. }
  1814. spin_unlock_irq(&crtc->dev->event_lock);
  1815. err = host1x_client_suspend(&dc->client);
  1816. if (err < 0)
  1817. dev_err(dc->dev, "failed to suspend: %d\n", err);
  1818. if (dc->has_opp_table) {
  1819. err = dev_pm_genpd_set_performance_state(dc->dev, 0);
  1820. if (err)
  1821. dev_err(dc->dev,
  1822. "failed to clear power domain state: %d\n", err);
  1823. }
  1824. }
  1825. static void tegra_crtc_atomic_enable(struct drm_crtc *crtc,
  1826. struct drm_atomic_state *state)
  1827. {
  1828. struct drm_display_mode *mode = &crtc->state->adjusted_mode;
  1829. struct tegra_dc_state *crtc_state = to_dc_state(crtc->state);
  1830. struct tegra_dc *dc = to_tegra_dc(crtc);
  1831. u32 value;
  1832. int err;
  1833. /* apply PLL changes */
  1834. tegra_dc_set_clock_rate(dc, crtc_state);
  1835. err = host1x_client_resume(&dc->client);
  1836. if (err < 0) {
  1837. dev_err(dc->dev, "failed to resume: %d\n", err);
  1838. return;
  1839. }
  1840. /* initialize display controller */
  1841. if (dc->syncpt) {
  1842. u32 syncpt = host1x_syncpt_id(dc->syncpt), enable;
  1843. if (dc->soc->has_nvdisplay)
  1844. enable = 1 << 31;
  1845. else
  1846. enable = 1 << 8;
  1847. value = SYNCPT_CNTRL_NO_STALL;
  1848. tegra_dc_writel(dc, value, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
  1849. value = enable | syncpt;
  1850. tegra_dc_writel(dc, value, DC_CMD_CONT_SYNCPT_VSYNC);
  1851. }
  1852. if (dc->soc->has_nvdisplay) {
  1853. value = DSC_TO_UF_INT | DSC_BBUF_UF_INT | DSC_RBUF_UF_INT |
  1854. DSC_OBUF_UF_INT;
  1855. tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
  1856. value = DSC_TO_UF_INT | DSC_BBUF_UF_INT | DSC_RBUF_UF_INT |
  1857. DSC_OBUF_UF_INT | SD3_BUCKET_WALK_DONE_INT |
  1858. HEAD_UF_INT | MSF_INT | REG_TMOUT_INT |
  1859. REGION_CRC_INT | V_PULSE2_INT | V_PULSE3_INT |
  1860. VBLANK_INT | FRAME_END_INT;
  1861. tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
  1862. value = SD3_BUCKET_WALK_DONE_INT | HEAD_UF_INT | VBLANK_INT |
  1863. FRAME_END_INT;
  1864. tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
  1865. value = HEAD_UF_INT | REG_TMOUT_INT | FRAME_END_INT;
  1866. tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
  1867. tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
  1868. } else {
  1869. value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
  1870. WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
  1871. tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
  1872. value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
  1873. WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
  1874. tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
  1875. /* initialize timer */
  1876. value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
  1877. WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
  1878. tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
  1879. value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
  1880. WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
  1881. tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
  1882. value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
  1883. WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
  1884. tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
  1885. value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
  1886. WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
  1887. tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
  1888. }
  1889. if (dc->soc->supports_background_color)
  1890. tegra_dc_writel(dc, 0, DC_DISP_BLEND_BACKGROUND_COLOR);
  1891. else
  1892. tegra_dc_writel(dc, 0, DC_DISP_BORDER_COLOR);
  1893. /* apply pixel clock changes */
  1894. if (!dc->soc->has_nvdisplay) {
  1895. value = SHIFT_CLK_DIVIDER(crtc_state->div) | PIXEL_CLK_DIVIDER_PCD1;
  1896. tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
  1897. }
  1898. /* program display mode */
  1899. tegra_dc_set_timings(dc, mode);
  1900. /* interlacing isn't supported yet, so disable it */
  1901. if (dc->soc->supports_interlacing) {
  1902. value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
  1903. value &= ~INTERLACE_ENABLE;
  1904. tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
  1905. }
  1906. value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
  1907. value &= ~DISP_CTRL_MODE_MASK;
  1908. value |= DISP_CTRL_MODE_C_DISPLAY;
  1909. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
  1910. if (!dc->soc->has_nvdisplay) {
  1911. value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
  1912. value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
  1913. PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
  1914. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
  1915. }
  1916. /* enable underflow reporting and display red for missing pixels */
  1917. if (dc->soc->has_nvdisplay) {
  1918. value = UNDERFLOW_MODE_RED | UNDERFLOW_REPORT_ENABLE;
  1919. tegra_dc_writel(dc, value, DC_COM_RG_UNDERFLOW);
  1920. }
  1921. if (dc->rgb) {
  1922. /* XXX: parameterize? */
  1923. value = SC0_H_QUALIFIER_NONE | SC1_H_QUALIFIER_NONE;
  1924. tegra_dc_writel(dc, value, DC_DISP_SHIFT_CLOCK_OPTIONS);
  1925. }
  1926. tegra_dc_commit(dc);
  1927. drm_crtc_vblank_on(crtc);
  1928. }
  1929. static void tegra_crtc_atomic_begin(struct drm_crtc *crtc,
  1930. struct drm_atomic_state *state)
  1931. {
  1932. unsigned long flags;
  1933. tegra_crtc_update_memory_bandwidth(crtc, state, true);
  1934. if (crtc->state->event) {
  1935. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  1936. if (drm_crtc_vblank_get(crtc) != 0)
  1937. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  1938. else
  1939. drm_crtc_arm_vblank_event(crtc, crtc->state->event);
  1940. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  1941. crtc->state->event = NULL;
  1942. }
  1943. }
  1944. static void tegra_crtc_atomic_flush(struct drm_crtc *crtc,
  1945. struct drm_atomic_state *state)
  1946. {
  1947. struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
  1948. crtc);
  1949. struct tegra_dc_state *dc_state = to_dc_state(crtc_state);
  1950. struct tegra_dc *dc = to_tegra_dc(crtc);
  1951. u32 value;
  1952. value = dc_state->planes << 8 | GENERAL_UPDATE;
  1953. tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
  1954. value = tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
  1955. value = dc_state->planes | GENERAL_ACT_REQ;
  1956. tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
  1957. value = tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
  1958. }
  1959. static bool tegra_plane_is_cursor(const struct drm_plane_state *state)
  1960. {
  1961. const struct tegra_dc_soc_info *soc = to_tegra_dc(state->crtc)->soc;
  1962. const struct drm_format_info *fmt = state->fb->format;
  1963. unsigned int src_w = drm_rect_width(&state->src) >> 16;
  1964. unsigned int dst_w = drm_rect_width(&state->dst);
  1965. if (state->plane->type != DRM_PLANE_TYPE_CURSOR)
  1966. return false;
  1967. if (soc->supports_cursor)
  1968. return true;
  1969. if (src_w != dst_w || fmt->num_planes != 1 || src_w * fmt->cpp[0] > 256)
  1970. return false;
  1971. return true;
  1972. }
  1973. static unsigned long
  1974. tegra_plane_overlap_mask(struct drm_crtc_state *state,
  1975. const struct drm_plane_state *plane_state)
  1976. {
  1977. const struct drm_plane_state *other_state;
  1978. const struct tegra_plane *tegra;
  1979. unsigned long overlap_mask = 0;
  1980. struct drm_plane *plane;
  1981. struct drm_rect rect;
  1982. if (!plane_state->visible || !plane_state->fb)
  1983. return 0;
  1984. /*
  1985. * Data-prefetch FIFO will easily help to overcome temporal memory
  1986. * pressure if other plane overlaps with the cursor plane.
  1987. */
  1988. if (tegra_plane_is_cursor(plane_state))
  1989. return 0;
  1990. drm_atomic_crtc_state_for_each_plane_state(plane, other_state, state) {
  1991. rect = plane_state->dst;
  1992. tegra = to_tegra_plane(other_state->plane);
  1993. if (!other_state->visible || !other_state->fb)
  1994. continue;
  1995. /*
  1996. * Ignore cursor plane overlaps because it's not practical to
  1997. * assume that it contributes to the bandwidth in overlapping
  1998. * area if window width is small.
  1999. */
  2000. if (tegra_plane_is_cursor(other_state))
  2001. continue;
  2002. if (drm_rect_intersect(&rect, &other_state->dst))
  2003. overlap_mask |= BIT(tegra->index);
  2004. }
  2005. return overlap_mask;
  2006. }
  2007. static int tegra_crtc_calculate_memory_bandwidth(struct drm_crtc *crtc,
  2008. struct drm_atomic_state *state)
  2009. {
  2010. ulong overlap_mask[TEGRA_DC_LEGACY_PLANES_NUM] = {}, mask;
  2011. u32 plane_peak_bw[TEGRA_DC_LEGACY_PLANES_NUM] = {};
  2012. bool all_planes_overlap_simultaneously = true;
  2013. const struct tegra_plane_state *tegra_state;
  2014. const struct drm_plane_state *plane_state;
  2015. struct tegra_dc *dc = to_tegra_dc(crtc);
  2016. struct drm_crtc_state *new_state;
  2017. struct tegra_plane *tegra;
  2018. struct drm_plane *plane;
  2019. /*
  2020. * The nv-display uses shared planes. The algorithm below assumes
  2021. * maximum 3 planes per-CRTC, this assumption isn't applicable to
  2022. * the nv-display. Note that T124 support has additional windows,
  2023. * but currently they aren't supported by the driver.
  2024. */
  2025. if (dc->soc->has_nvdisplay)
  2026. return 0;
  2027. new_state = drm_atomic_get_new_crtc_state(state, crtc);
  2028. /*
  2029. * For overlapping planes pixel's data is fetched for each plane at
  2030. * the same time, hence bandwidths are accumulated in this case.
  2031. * This needs to be taken into account for calculating total bandwidth
  2032. * consumed by all planes.
  2033. *
  2034. * Here we get the overlapping state of each plane, which is a
  2035. * bitmask of plane indices telling with what planes there is an
  2036. * overlap. Note that bitmask[plane] includes BIT(plane) in order
  2037. * to make further code nicer and simpler.
  2038. */
  2039. drm_atomic_crtc_state_for_each_plane_state(plane, plane_state, new_state) {
  2040. tegra_state = to_const_tegra_plane_state(plane_state);
  2041. tegra = to_tegra_plane(plane);
  2042. if (WARN_ON_ONCE(tegra->index >= TEGRA_DC_LEGACY_PLANES_NUM))
  2043. return -EINVAL;
  2044. plane_peak_bw[tegra->index] = tegra_state->peak_memory_bandwidth;
  2045. mask = tegra_plane_overlap_mask(new_state, plane_state);
  2046. overlap_mask[tegra->index] = mask;
  2047. if (hweight_long(mask) != 3)
  2048. all_planes_overlap_simultaneously = false;
  2049. }
  2050. /*
  2051. * Then we calculate maximum bandwidth of each plane state.
  2052. * The bandwidth includes the plane BW + BW of the "simultaneously"
  2053. * overlapping planes, where "simultaneously" means areas where DC
  2054. * fetches from the planes simultaneously during of scan-out process.
  2055. *
  2056. * For example, if plane A overlaps with planes B and C, but B and C
  2057. * don't overlap, then the peak bandwidth will be either in area where
  2058. * A-and-B or A-and-C planes overlap.
  2059. *
  2060. * The plane_peak_bw[] contains peak memory bandwidth values of
  2061. * each plane, this information is needed by interconnect provider
  2062. * in order to set up latency allowance based on the peak BW, see
  2063. * tegra_crtc_update_memory_bandwidth().
  2064. */
  2065. drm_atomic_crtc_state_for_each_plane_state(plane, plane_state, new_state) {
  2066. u32 i, old_peak_bw, new_peak_bw, overlap_bw = 0;
  2067. /*
  2068. * Note that plane's atomic check doesn't touch the
  2069. * total_peak_memory_bandwidth of enabled plane, hence the
  2070. * current state contains the old bandwidth state from the
  2071. * previous CRTC commit.
  2072. */
  2073. tegra_state = to_const_tegra_plane_state(plane_state);
  2074. tegra = to_tegra_plane(plane);
  2075. for_each_set_bit(i, &overlap_mask[tegra->index], 3) {
  2076. if (i == tegra->index)
  2077. continue;
  2078. if (all_planes_overlap_simultaneously)
  2079. overlap_bw += plane_peak_bw[i];
  2080. else
  2081. overlap_bw = max(overlap_bw, plane_peak_bw[i]);
  2082. }
  2083. new_peak_bw = plane_peak_bw[tegra->index] + overlap_bw;
  2084. old_peak_bw = tegra_state->total_peak_memory_bandwidth;
  2085. /*
  2086. * If plane's peak bandwidth changed (for example plane isn't
  2087. * overlapped anymore) and plane isn't in the atomic state,
  2088. * then add plane to the state in order to have the bandwidth
  2089. * updated.
  2090. */
  2091. if (old_peak_bw != new_peak_bw) {
  2092. struct tegra_plane_state *new_tegra_state;
  2093. struct drm_plane_state *new_plane_state;
  2094. new_plane_state = drm_atomic_get_plane_state(state, plane);
  2095. if (IS_ERR(new_plane_state))
  2096. return PTR_ERR(new_plane_state);
  2097. new_tegra_state = to_tegra_plane_state(new_plane_state);
  2098. new_tegra_state->total_peak_memory_bandwidth = new_peak_bw;
  2099. }
  2100. }
  2101. return 0;
  2102. }
  2103. static int tegra_crtc_atomic_check(struct drm_crtc *crtc,
  2104. struct drm_atomic_state *state)
  2105. {
  2106. int err;
  2107. err = tegra_crtc_calculate_memory_bandwidth(crtc, state);
  2108. if (err)
  2109. return err;
  2110. return 0;
  2111. }
  2112. void tegra_crtc_atomic_post_commit(struct drm_crtc *crtc,
  2113. struct drm_atomic_state *state)
  2114. {
  2115. /*
  2116. * Display bandwidth is allowed to go down only once hardware state
  2117. * is known to be armed, i.e. state was committed and VBLANK event
  2118. * received.
  2119. */
  2120. tegra_crtc_update_memory_bandwidth(crtc, state, false);
  2121. }
  2122. static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
  2123. .atomic_check = tegra_crtc_atomic_check,
  2124. .atomic_begin = tegra_crtc_atomic_begin,
  2125. .atomic_flush = tegra_crtc_atomic_flush,
  2126. .atomic_enable = tegra_crtc_atomic_enable,
  2127. .atomic_disable = tegra_crtc_atomic_disable,
  2128. };
  2129. static irqreturn_t tegra_dc_irq(int irq, void *data)
  2130. {
  2131. struct tegra_dc *dc = data;
  2132. unsigned long status;
  2133. status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
  2134. tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
  2135. if (status & FRAME_END_INT) {
  2136. /*
  2137. dev_dbg(dc->dev, "%s(): frame end\n", __func__);
  2138. */
  2139. dc->stats.frames_total++;
  2140. dc->stats.frames++;
  2141. }
  2142. if (status & VBLANK_INT) {
  2143. /*
  2144. dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
  2145. */
  2146. drm_crtc_handle_vblank(&dc->base);
  2147. dc->stats.vblank_total++;
  2148. dc->stats.vblank++;
  2149. }
  2150. if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
  2151. /*
  2152. dev_dbg(dc->dev, "%s(): underflow\n", __func__);
  2153. */
  2154. dc->stats.underflow_total++;
  2155. dc->stats.underflow++;
  2156. }
  2157. if (status & (WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT)) {
  2158. /*
  2159. dev_dbg(dc->dev, "%s(): overflow\n", __func__);
  2160. */
  2161. dc->stats.overflow_total++;
  2162. dc->stats.overflow++;
  2163. }
  2164. if (status & HEAD_UF_INT) {
  2165. dev_dbg_ratelimited(dc->dev, "%s(): head underflow\n", __func__);
  2166. dc->stats.underflow_total++;
  2167. dc->stats.underflow++;
  2168. }
  2169. return IRQ_HANDLED;
  2170. }
  2171. static bool tegra_dc_has_window_groups(struct tegra_dc *dc)
  2172. {
  2173. unsigned int i;
  2174. if (!dc->soc->wgrps)
  2175. return true;
  2176. for (i = 0; i < dc->soc->num_wgrps; i++) {
  2177. const struct tegra_windowgroup_soc *wgrp = &dc->soc->wgrps[i];
  2178. if (wgrp->dc == dc->pipe && wgrp->num_windows > 0)
  2179. return true;
  2180. }
  2181. return false;
  2182. }
  2183. static int tegra_dc_early_init(struct host1x_client *client)
  2184. {
  2185. struct drm_device *drm = dev_get_drvdata(client->host);
  2186. struct tegra_drm *tegra = drm->dev_private;
  2187. tegra->num_crtcs++;
  2188. return 0;
  2189. }
  2190. static int tegra_dc_init(struct host1x_client *client)
  2191. {
  2192. struct drm_device *drm = dev_get_drvdata(client->host);
  2193. unsigned long flags = HOST1X_SYNCPT_CLIENT_MANAGED;
  2194. struct tegra_dc *dc = host1x_client_to_dc(client);
  2195. struct tegra_drm *tegra = drm->dev_private;
  2196. struct drm_plane *primary = NULL;
  2197. struct drm_plane *cursor = NULL;
  2198. int err;
  2199. /*
  2200. * DC has been reset by now, so VBLANK syncpoint can be released
  2201. * for general use.
  2202. */
  2203. host1x_syncpt_release_vblank_reservation(client, 26 + dc->pipe);
  2204. /*
  2205. * XXX do not register DCs with no window groups because we cannot
  2206. * assign a primary plane to them, which in turn will cause KMS to
  2207. * crash.
  2208. */
  2209. if (!tegra_dc_has_window_groups(dc))
  2210. return 0;
  2211. /*
  2212. * Set the display hub as the host1x client parent for the display
  2213. * controller. This is needed for the runtime reference counting that
  2214. * ensures the display hub is always powered when any of the display
  2215. * controllers are.
  2216. */
  2217. if (dc->soc->has_nvdisplay)
  2218. client->parent = &tegra->hub->client;
  2219. dc->syncpt = host1x_syncpt_request(client, flags);
  2220. if (!dc->syncpt)
  2221. dev_warn(dc->dev, "failed to allocate syncpoint\n");
  2222. err = host1x_client_iommu_attach(client);
  2223. if (err < 0 && err != -ENODEV) {
  2224. dev_err(client->dev, "failed to attach to domain: %d\n", err);
  2225. return err;
  2226. }
  2227. if (dc->soc->wgrps)
  2228. primary = tegra_dc_add_shared_planes(drm, dc);
  2229. else
  2230. primary = tegra_dc_add_planes(drm, dc);
  2231. if (IS_ERR(primary)) {
  2232. err = PTR_ERR(primary);
  2233. goto cleanup;
  2234. }
  2235. if (dc->soc->supports_cursor) {
  2236. cursor = tegra_dc_cursor_plane_create(drm, dc);
  2237. if (IS_ERR(cursor)) {
  2238. err = PTR_ERR(cursor);
  2239. goto cleanup;
  2240. }
  2241. } else {
  2242. /* dedicate one overlay to mouse cursor */
  2243. cursor = tegra_dc_overlay_plane_create(drm, dc, 2, true);
  2244. if (IS_ERR(cursor)) {
  2245. err = PTR_ERR(cursor);
  2246. goto cleanup;
  2247. }
  2248. }
  2249. err = drm_crtc_init_with_planes(drm, &dc->base, primary, cursor,
  2250. &tegra_crtc_funcs, NULL);
  2251. if (err < 0)
  2252. goto cleanup;
  2253. drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
  2254. /*
  2255. * Keep track of the minimum pitch alignment across all display
  2256. * controllers.
  2257. */
  2258. if (dc->soc->pitch_align > tegra->pitch_align)
  2259. tegra->pitch_align = dc->soc->pitch_align;
  2260. /* track maximum resolution */
  2261. if (dc->soc->has_nvdisplay)
  2262. drm->mode_config.max_width = drm->mode_config.max_height = 16384;
  2263. else
  2264. drm->mode_config.max_width = drm->mode_config.max_height = 4096;
  2265. err = tegra_dc_rgb_init(drm, dc);
  2266. if (err < 0 && err != -ENODEV) {
  2267. dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
  2268. goto cleanup;
  2269. }
  2270. err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
  2271. dev_name(dc->dev), dc);
  2272. if (err < 0) {
  2273. dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
  2274. err);
  2275. goto cleanup;
  2276. }
  2277. /*
  2278. * Inherit the DMA parameters (such as maximum segment size) from the
  2279. * parent host1x device.
  2280. */
  2281. client->dev->dma_parms = client->host->dma_parms;
  2282. return 0;
  2283. cleanup:
  2284. if (!IS_ERR_OR_NULL(cursor))
  2285. drm_plane_cleanup(cursor);
  2286. if (!IS_ERR(primary))
  2287. drm_plane_cleanup(primary);
  2288. host1x_client_iommu_detach(client);
  2289. host1x_syncpt_put(dc->syncpt);
  2290. return err;
  2291. }
  2292. static int tegra_dc_exit(struct host1x_client *client)
  2293. {
  2294. struct tegra_dc *dc = host1x_client_to_dc(client);
  2295. int err;
  2296. if (!tegra_dc_has_window_groups(dc))
  2297. return 0;
  2298. /* avoid a dangling pointer just in case this disappears */
  2299. client->dev->dma_parms = NULL;
  2300. devm_free_irq(dc->dev, dc->irq, dc);
  2301. err = tegra_dc_rgb_exit(dc);
  2302. if (err) {
  2303. dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
  2304. return err;
  2305. }
  2306. host1x_client_iommu_detach(client);
  2307. host1x_syncpt_put(dc->syncpt);
  2308. return 0;
  2309. }
  2310. static int tegra_dc_late_exit(struct host1x_client *client)
  2311. {
  2312. struct drm_device *drm = dev_get_drvdata(client->host);
  2313. struct tegra_drm *tegra = drm->dev_private;
  2314. tegra->num_crtcs--;
  2315. return 0;
  2316. }
  2317. static int tegra_dc_runtime_suspend(struct host1x_client *client)
  2318. {
  2319. struct tegra_dc *dc = host1x_client_to_dc(client);
  2320. struct device *dev = client->dev;
  2321. int err;
  2322. err = reset_control_assert(dc->rst);
  2323. if (err < 0) {
  2324. dev_err(dev, "failed to assert reset: %d\n", err);
  2325. return err;
  2326. }
  2327. if (dc->soc->has_powergate)
  2328. tegra_powergate_power_off(dc->powergate);
  2329. clk_disable_unprepare(dc->clk);
  2330. pm_runtime_put_sync(dev);
  2331. return 0;
  2332. }
  2333. static int tegra_dc_runtime_resume(struct host1x_client *client)
  2334. {
  2335. struct tegra_dc *dc = host1x_client_to_dc(client);
  2336. struct device *dev = client->dev;
  2337. int err;
  2338. err = pm_runtime_resume_and_get(dev);
  2339. if (err < 0) {
  2340. dev_err(dev, "failed to get runtime PM: %d\n", err);
  2341. return err;
  2342. }
  2343. if (dc->soc->has_powergate) {
  2344. err = tegra_powergate_sequence_power_up(dc->powergate, dc->clk,
  2345. dc->rst);
  2346. if (err < 0) {
  2347. dev_err(dev, "failed to power partition: %d\n", err);
  2348. goto put_rpm;
  2349. }
  2350. } else {
  2351. err = clk_prepare_enable(dc->clk);
  2352. if (err < 0) {
  2353. dev_err(dev, "failed to enable clock: %d\n", err);
  2354. goto put_rpm;
  2355. }
  2356. err = reset_control_deassert(dc->rst);
  2357. if (err < 0) {
  2358. dev_err(dev, "failed to deassert reset: %d\n", err);
  2359. goto disable_clk;
  2360. }
  2361. }
  2362. return 0;
  2363. disable_clk:
  2364. clk_disable_unprepare(dc->clk);
  2365. put_rpm:
  2366. pm_runtime_put_sync(dev);
  2367. return err;
  2368. }
  2369. static const struct host1x_client_ops dc_client_ops = {
  2370. .early_init = tegra_dc_early_init,
  2371. .init = tegra_dc_init,
  2372. .exit = tegra_dc_exit,
  2373. .late_exit = tegra_dc_late_exit,
  2374. .suspend = tegra_dc_runtime_suspend,
  2375. .resume = tegra_dc_runtime_resume,
  2376. };
  2377. static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
  2378. .supports_background_color = false,
  2379. .supports_interlacing = false,
  2380. .supports_cursor = false,
  2381. .supports_block_linear = false,
  2382. .supports_sector_layout = false,
  2383. .has_legacy_blending = true,
  2384. .pitch_align = 8,
  2385. .has_powergate = false,
  2386. .coupled_pm = true,
  2387. .has_nvdisplay = false,
  2388. .num_primary_formats = ARRAY_SIZE(tegra20_primary_formats),
  2389. .primary_formats = tegra20_primary_formats,
  2390. .num_overlay_formats = ARRAY_SIZE(tegra20_overlay_formats),
  2391. .overlay_formats = tegra20_overlay_formats,
  2392. .modifiers = tegra20_modifiers,
  2393. .has_win_a_without_filters = true,
  2394. .has_win_b_vfilter_mem_client = true,
  2395. .has_win_c_without_vert_filter = true,
  2396. .plane_tiled_memory_bandwidth_x2 = false,
  2397. .has_pll_d2_out0 = false,
  2398. };
  2399. static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
  2400. .supports_background_color = false,
  2401. .supports_interlacing = false,
  2402. .supports_cursor = false,
  2403. .supports_block_linear = false,
  2404. .supports_sector_layout = false,
  2405. .has_legacy_blending = true,
  2406. .pitch_align = 8,
  2407. .has_powergate = false,
  2408. .coupled_pm = false,
  2409. .has_nvdisplay = false,
  2410. .num_primary_formats = ARRAY_SIZE(tegra20_primary_formats),
  2411. .primary_formats = tegra20_primary_formats,
  2412. .num_overlay_formats = ARRAY_SIZE(tegra20_overlay_formats),
  2413. .overlay_formats = tegra20_overlay_formats,
  2414. .modifiers = tegra20_modifiers,
  2415. .has_win_a_without_filters = false,
  2416. .has_win_b_vfilter_mem_client = true,
  2417. .has_win_c_without_vert_filter = false,
  2418. .plane_tiled_memory_bandwidth_x2 = true,
  2419. .has_pll_d2_out0 = true,
  2420. };
  2421. static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
  2422. .supports_background_color = false,
  2423. .supports_interlacing = false,
  2424. .supports_cursor = false,
  2425. .supports_block_linear = false,
  2426. .supports_sector_layout = false,
  2427. .has_legacy_blending = true,
  2428. .pitch_align = 64,
  2429. .has_powergate = true,
  2430. .coupled_pm = false,
  2431. .has_nvdisplay = false,
  2432. .num_primary_formats = ARRAY_SIZE(tegra114_primary_formats),
  2433. .primary_formats = tegra114_primary_formats,
  2434. .num_overlay_formats = ARRAY_SIZE(tegra114_overlay_formats),
  2435. .overlay_formats = tegra114_overlay_formats,
  2436. .modifiers = tegra20_modifiers,
  2437. .has_win_a_without_filters = false,
  2438. .has_win_b_vfilter_mem_client = false,
  2439. .has_win_c_without_vert_filter = false,
  2440. .plane_tiled_memory_bandwidth_x2 = true,
  2441. .has_pll_d2_out0 = true,
  2442. };
  2443. static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
  2444. .supports_background_color = true,
  2445. .supports_interlacing = true,
  2446. .supports_cursor = true,
  2447. .supports_block_linear = true,
  2448. .supports_sector_layout = false,
  2449. .has_legacy_blending = false,
  2450. .pitch_align = 64,
  2451. .has_powergate = true,
  2452. .coupled_pm = false,
  2453. .has_nvdisplay = false,
  2454. .num_primary_formats = ARRAY_SIZE(tegra124_primary_formats),
  2455. .primary_formats = tegra124_primary_formats,
  2456. .num_overlay_formats = ARRAY_SIZE(tegra124_overlay_formats),
  2457. .overlay_formats = tegra124_overlay_formats,
  2458. .modifiers = tegra124_modifiers,
  2459. .has_win_a_without_filters = false,
  2460. .has_win_b_vfilter_mem_client = false,
  2461. .has_win_c_without_vert_filter = false,
  2462. .plane_tiled_memory_bandwidth_x2 = false,
  2463. .has_pll_d2_out0 = true,
  2464. };
  2465. static const struct tegra_dc_soc_info tegra210_dc_soc_info = {
  2466. .supports_background_color = true,
  2467. .supports_interlacing = true,
  2468. .supports_cursor = true,
  2469. .supports_block_linear = true,
  2470. .supports_sector_layout = false,
  2471. .has_legacy_blending = false,
  2472. .pitch_align = 64,
  2473. .has_powergate = true,
  2474. .coupled_pm = false,
  2475. .has_nvdisplay = false,
  2476. .num_primary_formats = ARRAY_SIZE(tegra114_primary_formats),
  2477. .primary_formats = tegra114_primary_formats,
  2478. .num_overlay_formats = ARRAY_SIZE(tegra114_overlay_formats),
  2479. .overlay_formats = tegra114_overlay_formats,
  2480. .modifiers = tegra124_modifiers,
  2481. .has_win_a_without_filters = false,
  2482. .has_win_b_vfilter_mem_client = false,
  2483. .has_win_c_without_vert_filter = false,
  2484. .plane_tiled_memory_bandwidth_x2 = false,
  2485. .has_pll_d2_out0 = true,
  2486. };
  2487. static const struct tegra_windowgroup_soc tegra186_dc_wgrps[] = {
  2488. {
  2489. .index = 0,
  2490. .dc = 0,
  2491. .windows = (const unsigned int[]) { 0 },
  2492. .num_windows = 1,
  2493. }, {
  2494. .index = 1,
  2495. .dc = 1,
  2496. .windows = (const unsigned int[]) { 1 },
  2497. .num_windows = 1,
  2498. }, {
  2499. .index = 2,
  2500. .dc = 1,
  2501. .windows = (const unsigned int[]) { 2 },
  2502. .num_windows = 1,
  2503. }, {
  2504. .index = 3,
  2505. .dc = 2,
  2506. .windows = (const unsigned int[]) { 3 },
  2507. .num_windows = 1,
  2508. }, {
  2509. .index = 4,
  2510. .dc = 2,
  2511. .windows = (const unsigned int[]) { 4 },
  2512. .num_windows = 1,
  2513. }, {
  2514. .index = 5,
  2515. .dc = 2,
  2516. .windows = (const unsigned int[]) { 5 },
  2517. .num_windows = 1,
  2518. },
  2519. };
  2520. static const struct tegra_dc_soc_info tegra186_dc_soc_info = {
  2521. .supports_background_color = true,
  2522. .supports_interlacing = true,
  2523. .supports_cursor = true,
  2524. .supports_block_linear = true,
  2525. .supports_sector_layout = false,
  2526. .has_legacy_blending = false,
  2527. .pitch_align = 64,
  2528. .has_powergate = false,
  2529. .coupled_pm = false,
  2530. .has_nvdisplay = true,
  2531. .wgrps = tegra186_dc_wgrps,
  2532. .num_wgrps = ARRAY_SIZE(tegra186_dc_wgrps),
  2533. .plane_tiled_memory_bandwidth_x2 = false,
  2534. .has_pll_d2_out0 = false,
  2535. };
  2536. static const struct tegra_windowgroup_soc tegra194_dc_wgrps[] = {
  2537. {
  2538. .index = 0,
  2539. .dc = 0,
  2540. .windows = (const unsigned int[]) { 0 },
  2541. .num_windows = 1,
  2542. }, {
  2543. .index = 1,
  2544. .dc = 1,
  2545. .windows = (const unsigned int[]) { 1 },
  2546. .num_windows = 1,
  2547. }, {
  2548. .index = 2,
  2549. .dc = 1,
  2550. .windows = (const unsigned int[]) { 2 },
  2551. .num_windows = 1,
  2552. }, {
  2553. .index = 3,
  2554. .dc = 2,
  2555. .windows = (const unsigned int[]) { 3 },
  2556. .num_windows = 1,
  2557. }, {
  2558. .index = 4,
  2559. .dc = 2,
  2560. .windows = (const unsigned int[]) { 4 },
  2561. .num_windows = 1,
  2562. }, {
  2563. .index = 5,
  2564. .dc = 2,
  2565. .windows = (const unsigned int[]) { 5 },
  2566. .num_windows = 1,
  2567. },
  2568. };
  2569. static const struct tegra_dc_soc_info tegra194_dc_soc_info = {
  2570. .supports_background_color = true,
  2571. .supports_interlacing = true,
  2572. .supports_cursor = true,
  2573. .supports_block_linear = true,
  2574. .supports_sector_layout = true,
  2575. .has_legacy_blending = false,
  2576. .pitch_align = 64,
  2577. .has_powergate = false,
  2578. .coupled_pm = false,
  2579. .has_nvdisplay = true,
  2580. .wgrps = tegra194_dc_wgrps,
  2581. .num_wgrps = ARRAY_SIZE(tegra194_dc_wgrps),
  2582. .plane_tiled_memory_bandwidth_x2 = false,
  2583. .has_pll_d2_out0 = false,
  2584. };
  2585. static const struct of_device_id tegra_dc_of_match[] = {
  2586. {
  2587. .compatible = "nvidia,tegra194-dc",
  2588. .data = &tegra194_dc_soc_info,
  2589. }, {
  2590. .compatible = "nvidia,tegra186-dc",
  2591. .data = &tegra186_dc_soc_info,
  2592. }, {
  2593. .compatible = "nvidia,tegra210-dc",
  2594. .data = &tegra210_dc_soc_info,
  2595. }, {
  2596. .compatible = "nvidia,tegra124-dc",
  2597. .data = &tegra124_dc_soc_info,
  2598. }, {
  2599. .compatible = "nvidia,tegra114-dc",
  2600. .data = &tegra114_dc_soc_info,
  2601. }, {
  2602. .compatible = "nvidia,tegra30-dc",
  2603. .data = &tegra30_dc_soc_info,
  2604. }, {
  2605. .compatible = "nvidia,tegra20-dc",
  2606. .data = &tegra20_dc_soc_info,
  2607. }, {
  2608. /* sentinel */
  2609. }
  2610. };
  2611. MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
  2612. static int tegra_dc_parse_dt(struct tegra_dc *dc)
  2613. {
  2614. struct device_node *np;
  2615. u32 value = 0;
  2616. int err;
  2617. err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
  2618. if (err < 0) {
  2619. dev_err(dc->dev, "missing \"nvidia,head\" property\n");
  2620. /*
  2621. * If the nvidia,head property isn't present, try to find the
  2622. * correct head number by looking up the position of this
  2623. * display controller's node within the device tree. Assuming
  2624. * that the nodes are ordered properly in the DTS file and
  2625. * that the translation into a flattened device tree blob
  2626. * preserves that ordering this will actually yield the right
  2627. * head number.
  2628. *
  2629. * If those assumptions don't hold, this will still work for
  2630. * cases where only a single display controller is used.
  2631. */
  2632. for_each_matching_node(np, tegra_dc_of_match) {
  2633. if (np == dc->dev->of_node) {
  2634. of_node_put(np);
  2635. break;
  2636. }
  2637. value++;
  2638. }
  2639. }
  2640. dc->pipe = value;
  2641. return 0;
  2642. }
  2643. static int tegra_dc_match_by_pipe(struct device *dev, const void *data)
  2644. {
  2645. struct tegra_dc *dc = dev_get_drvdata(dev);
  2646. unsigned int pipe = (unsigned long)(void *)data;
  2647. return dc->pipe == pipe;
  2648. }
  2649. static int tegra_dc_couple(struct tegra_dc *dc)
  2650. {
  2651. /*
  2652. * On Tegra20, DC1 requires DC0 to be taken out of reset in order to
  2653. * be enabled, otherwise CPU hangs on writing to CMD_DISPLAY_COMMAND /
  2654. * POWER_CONTROL registers during CRTC enabling.
  2655. */
  2656. if (dc->soc->coupled_pm && dc->pipe == 1) {
  2657. struct device *companion;
  2658. struct tegra_dc *parent;
  2659. companion = driver_find_device(dc->dev->driver, NULL, (const void *)0,
  2660. tegra_dc_match_by_pipe);
  2661. if (!companion)
  2662. return -EPROBE_DEFER;
  2663. parent = dev_get_drvdata(companion);
  2664. dc->client.parent = &parent->client;
  2665. dev_dbg(dc->dev, "coupled to %s\n", dev_name(companion));
  2666. put_device(companion);
  2667. }
  2668. return 0;
  2669. }
  2670. static int tegra_dc_init_opp_table(struct tegra_dc *dc)
  2671. {
  2672. struct tegra_core_opp_params opp_params = {};
  2673. int err;
  2674. err = devm_tegra_core_dev_init_opp_table(dc->dev, &opp_params);
  2675. if (err && err != -ENODEV)
  2676. return err;
  2677. if (err)
  2678. dc->has_opp_table = false;
  2679. else
  2680. dc->has_opp_table = true;
  2681. return 0;
  2682. }
  2683. static int tegra_dc_probe(struct platform_device *pdev)
  2684. {
  2685. u64 dma_mask = dma_get_mask(pdev->dev.parent);
  2686. struct tegra_dc *dc;
  2687. int err;
  2688. err = dma_coerce_mask_and_coherent(&pdev->dev, dma_mask);
  2689. if (err < 0) {
  2690. dev_err(&pdev->dev, "failed to set DMA mask: %d\n", err);
  2691. return err;
  2692. }
  2693. dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
  2694. if (!dc)
  2695. return -ENOMEM;
  2696. dc->soc = of_device_get_match_data(&pdev->dev);
  2697. INIT_LIST_HEAD(&dc->list);
  2698. dc->dev = &pdev->dev;
  2699. err = tegra_dc_parse_dt(dc);
  2700. if (err < 0)
  2701. return err;
  2702. err = tegra_dc_couple(dc);
  2703. if (err < 0)
  2704. return err;
  2705. dc->clk = devm_clk_get(&pdev->dev, NULL);
  2706. if (IS_ERR(dc->clk)) {
  2707. dev_err(&pdev->dev, "failed to get clock\n");
  2708. return PTR_ERR(dc->clk);
  2709. }
  2710. dc->rst = devm_reset_control_get(&pdev->dev, "dc");
  2711. if (IS_ERR(dc->rst)) {
  2712. dev_err(&pdev->dev, "failed to get reset\n");
  2713. return PTR_ERR(dc->rst);
  2714. }
  2715. /* assert reset and disable clock */
  2716. err = clk_prepare_enable(dc->clk);
  2717. if (err < 0)
  2718. return err;
  2719. usleep_range(2000, 4000);
  2720. err = reset_control_assert(dc->rst);
  2721. if (err < 0) {
  2722. clk_disable_unprepare(dc->clk);
  2723. return err;
  2724. }
  2725. usleep_range(2000, 4000);
  2726. clk_disable_unprepare(dc->clk);
  2727. if (dc->soc->has_powergate) {
  2728. if (dc->pipe == 0)
  2729. dc->powergate = TEGRA_POWERGATE_DIS;
  2730. else
  2731. dc->powergate = TEGRA_POWERGATE_DISB;
  2732. tegra_powergate_power_off(dc->powergate);
  2733. }
  2734. err = tegra_dc_init_opp_table(dc);
  2735. if (err < 0)
  2736. return err;
  2737. dc->regs = devm_platform_ioremap_resource(pdev, 0);
  2738. if (IS_ERR(dc->regs))
  2739. return PTR_ERR(dc->regs);
  2740. dc->irq = platform_get_irq(pdev, 0);
  2741. if (dc->irq < 0)
  2742. return -ENXIO;
  2743. err = tegra_dc_rgb_probe(dc);
  2744. if (err < 0 && err != -ENODEV)
  2745. return dev_err_probe(&pdev->dev, err,
  2746. "failed to probe RGB output\n");
  2747. platform_set_drvdata(pdev, dc);
  2748. pm_runtime_enable(&pdev->dev);
  2749. INIT_LIST_HEAD(&dc->client.list);
  2750. dc->client.ops = &dc_client_ops;
  2751. dc->client.dev = &pdev->dev;
  2752. err = host1x_client_register(&dc->client);
  2753. if (err < 0) {
  2754. dev_err(&pdev->dev, "failed to register host1x client: %d\n",
  2755. err);
  2756. goto disable_pm;
  2757. }
  2758. return 0;
  2759. disable_pm:
  2760. pm_runtime_disable(&pdev->dev);
  2761. tegra_dc_rgb_remove(dc);
  2762. return err;
  2763. }
  2764. static void tegra_dc_remove(struct platform_device *pdev)
  2765. {
  2766. struct tegra_dc *dc = platform_get_drvdata(pdev);
  2767. host1x_client_unregister(&dc->client);
  2768. tegra_dc_rgb_remove(dc);
  2769. pm_runtime_disable(&pdev->dev);
  2770. }
  2771. struct platform_driver tegra_dc_driver = {
  2772. .driver = {
  2773. .name = "tegra-dc",
  2774. .of_match_table = tegra_dc_of_match,
  2775. },
  2776. .probe = tegra_dc_probe,
  2777. .remove = tegra_dc_remove,
  2778. };