dc.c 90 KB

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