vc4_hdmi.c 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2015 Broadcom
  4. * Copyright (c) 2014 The Linux Foundation. All rights reserved.
  5. * Copyright (C) 2013 Red Hat
  6. * Author: Rob Clark <[email protected]>
  7. */
  8. /**
  9. * DOC: VC4 Falcon HDMI module
  10. *
  11. * The HDMI core has a state machine and a PHY. On BCM2835, most of
  12. * the unit operates off of the HSM clock from CPRMAN. It also
  13. * internally uses the PLLH_PIX clock for the PHY.
  14. *
  15. * HDMI infoframes are kept within a small packet ram, where each
  16. * packet can be individually enabled for including in a frame.
  17. *
  18. * HDMI audio is implemented entirely within the HDMI IP block. A
  19. * register in the HDMI encoder takes SPDIF frames from the DMA engine
  20. * and transfers them over an internal MAI (multi-channel audio
  21. * interconnect) bus to the encoder side for insertion into the video
  22. * blank regions.
  23. *
  24. * The driver's HDMI encoder does not yet support power management.
  25. * The HDMI encoder's power domain and the HSM/pixel clocks are kept
  26. * continuously running, and only the HDMI logic and packet ram are
  27. * powered off/on at disable/enable time.
  28. *
  29. * The driver does not yet support CEC control, though the HDMI
  30. * encoder block has CEC support.
  31. */
  32. #include <drm/display/drm_hdmi_helper.h>
  33. #include <drm/display/drm_scdc_helper.h>
  34. #include <drm/drm_atomic_helper.h>
  35. #include <drm/drm_drv.h>
  36. #include <drm/drm_probe_helper.h>
  37. #include <drm/drm_simple_kms_helper.h>
  38. #include <linux/clk.h>
  39. #include <linux/component.h>
  40. #include <linux/gpio/consumer.h>
  41. #include <linux/i2c.h>
  42. #include <linux/of_address.h>
  43. #include <linux/of_platform.h>
  44. #include <linux/pm_runtime.h>
  45. #include <linux/rational.h>
  46. #include <linux/reset.h>
  47. #include <sound/dmaengine_pcm.h>
  48. #include <sound/hdmi-codec.h>
  49. #include <sound/pcm_drm_eld.h>
  50. #include <sound/pcm_params.h>
  51. #include <sound/soc.h>
  52. #include "media/cec.h"
  53. #include "vc4_drv.h"
  54. #include "vc4_hdmi.h"
  55. #include "vc4_hdmi_regs.h"
  56. #include "vc4_regs.h"
  57. #define VC5_HDMI_HORZA_HFP_SHIFT 16
  58. #define VC5_HDMI_HORZA_HFP_MASK VC4_MASK(28, 16)
  59. #define VC5_HDMI_HORZA_VPOS BIT(15)
  60. #define VC5_HDMI_HORZA_HPOS BIT(14)
  61. #define VC5_HDMI_HORZA_HAP_SHIFT 0
  62. #define VC5_HDMI_HORZA_HAP_MASK VC4_MASK(13, 0)
  63. #define VC5_HDMI_HORZB_HBP_SHIFT 16
  64. #define VC5_HDMI_HORZB_HBP_MASK VC4_MASK(26, 16)
  65. #define VC5_HDMI_HORZB_HSP_SHIFT 0
  66. #define VC5_HDMI_HORZB_HSP_MASK VC4_MASK(10, 0)
  67. #define VC5_HDMI_VERTA_VSP_SHIFT 24
  68. #define VC5_HDMI_VERTA_VSP_MASK VC4_MASK(28, 24)
  69. #define VC5_HDMI_VERTA_VFP_SHIFT 16
  70. #define VC5_HDMI_VERTA_VFP_MASK VC4_MASK(22, 16)
  71. #define VC5_HDMI_VERTA_VAL_SHIFT 0
  72. #define VC5_HDMI_VERTA_VAL_MASK VC4_MASK(12, 0)
  73. #define VC5_HDMI_VERTB_VSPO_SHIFT 16
  74. #define VC5_HDMI_VERTB_VSPO_MASK VC4_MASK(29, 16)
  75. #define VC4_HDMI_MISC_CONTROL_PIXEL_REP_SHIFT 0
  76. #define VC4_HDMI_MISC_CONTROL_PIXEL_REP_MASK VC4_MASK(3, 0)
  77. #define VC5_HDMI_MISC_CONTROL_PIXEL_REP_SHIFT 0
  78. #define VC5_HDMI_MISC_CONTROL_PIXEL_REP_MASK VC4_MASK(3, 0)
  79. #define VC5_HDMI_SCRAMBLER_CTL_ENABLE BIT(0)
  80. #define VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_SHIFT 8
  81. #define VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_MASK VC4_MASK(10, 8)
  82. #define VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_SHIFT 0
  83. #define VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_MASK VC4_MASK(3, 0)
  84. #define VC5_HDMI_GCP_CONFIG_GCP_ENABLE BIT(31)
  85. #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_SHIFT 8
  86. #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_MASK VC4_MASK(15, 8)
  87. # define VC4_HD_M_SW_RST BIT(2)
  88. # define VC4_HD_M_ENABLE BIT(0)
  89. #define HSM_MIN_CLOCK_FREQ 120000000
  90. #define CEC_CLOCK_FREQ 40000
  91. #define HDMI_14_MAX_TMDS_CLK (340 * 1000 * 1000)
  92. static const char * const output_format_str[] = {
  93. [VC4_HDMI_OUTPUT_RGB] = "RGB",
  94. [VC4_HDMI_OUTPUT_YUV420] = "YUV 4:2:0",
  95. [VC4_HDMI_OUTPUT_YUV422] = "YUV 4:2:2",
  96. [VC4_HDMI_OUTPUT_YUV444] = "YUV 4:4:4",
  97. };
  98. static const char *vc4_hdmi_output_fmt_str(enum vc4_hdmi_output_format fmt)
  99. {
  100. if (fmt >= ARRAY_SIZE(output_format_str))
  101. return "invalid";
  102. return output_format_str[fmt];
  103. }
  104. static unsigned long long
  105. vc4_hdmi_encoder_compute_mode_clock(const struct drm_display_mode *mode,
  106. unsigned int bpc, enum vc4_hdmi_output_format fmt);
  107. static bool vc4_hdmi_supports_scrambling(struct drm_encoder *encoder)
  108. {
  109. struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
  110. struct drm_display_info *display = &vc4_hdmi->connector.display_info;
  111. lockdep_assert_held(&vc4_hdmi->mutex);
  112. if (!display->is_hdmi)
  113. return false;
  114. if (!display->hdmi.scdc.supported ||
  115. !display->hdmi.scdc.scrambling.supported)
  116. return false;
  117. return true;
  118. }
  119. static bool vc4_hdmi_mode_needs_scrambling(const struct drm_display_mode *mode,
  120. unsigned int bpc,
  121. enum vc4_hdmi_output_format fmt)
  122. {
  123. unsigned long long clock = vc4_hdmi_encoder_compute_mode_clock(mode, bpc, fmt);
  124. return clock > HDMI_14_MAX_TMDS_CLK;
  125. }
  126. static bool vc4_hdmi_is_full_range_rgb(struct vc4_hdmi *vc4_hdmi,
  127. const struct drm_display_mode *mode)
  128. {
  129. struct drm_display_info *display = &vc4_hdmi->connector.display_info;
  130. return !display->is_hdmi ||
  131. drm_default_rgb_quant_range(mode) == HDMI_QUANTIZATION_RANGE_FULL;
  132. }
  133. static int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused)
  134. {
  135. struct drm_info_node *node = (struct drm_info_node *)m->private;
  136. struct vc4_hdmi *vc4_hdmi = node->info_ent->data;
  137. struct drm_device *drm = vc4_hdmi->connector.dev;
  138. struct drm_printer p = drm_seq_file_printer(m);
  139. int idx;
  140. if (!drm_dev_enter(drm, &idx))
  141. return -ENODEV;
  142. drm_print_regset32(&p, &vc4_hdmi->hdmi_regset);
  143. drm_print_regset32(&p, &vc4_hdmi->hd_regset);
  144. drm_print_regset32(&p, &vc4_hdmi->cec_regset);
  145. drm_print_regset32(&p, &vc4_hdmi->csc_regset);
  146. drm_print_regset32(&p, &vc4_hdmi->dvp_regset);
  147. drm_print_regset32(&p, &vc4_hdmi->phy_regset);
  148. drm_print_regset32(&p, &vc4_hdmi->ram_regset);
  149. drm_print_regset32(&p, &vc4_hdmi->rm_regset);
  150. drm_dev_exit(idx);
  151. return 0;
  152. }
  153. static void vc4_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
  154. {
  155. struct drm_device *drm = vc4_hdmi->connector.dev;
  156. unsigned long flags;
  157. int idx;
  158. /*
  159. * We can be called by our bind callback, when the
  160. * connector->dev pointer might not be initialised yet.
  161. */
  162. if (drm && !drm_dev_enter(drm, &idx))
  163. return;
  164. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  165. HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_SW_RST);
  166. udelay(1);
  167. HDMI_WRITE(HDMI_M_CTL, 0);
  168. HDMI_WRITE(HDMI_M_CTL, VC4_HD_M_ENABLE);
  169. HDMI_WRITE(HDMI_SW_RESET_CONTROL,
  170. VC4_HDMI_SW_RESET_HDMI |
  171. VC4_HDMI_SW_RESET_FORMAT_DETECT);
  172. HDMI_WRITE(HDMI_SW_RESET_CONTROL, 0);
  173. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  174. if (drm)
  175. drm_dev_exit(idx);
  176. }
  177. static void vc5_hdmi_reset(struct vc4_hdmi *vc4_hdmi)
  178. {
  179. struct drm_device *drm = vc4_hdmi->connector.dev;
  180. unsigned long flags;
  181. int idx;
  182. /*
  183. * We can be called by our bind callback, when the
  184. * connector->dev pointer might not be initialised yet.
  185. */
  186. if (drm && !drm_dev_enter(drm, &idx))
  187. return;
  188. reset_control_reset(vc4_hdmi->reset);
  189. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  190. HDMI_WRITE(HDMI_DVP_CTL, 0);
  191. HDMI_WRITE(HDMI_CLOCK_STOP,
  192. HDMI_READ(HDMI_CLOCK_STOP) | VC4_DVP_HT_CLOCK_STOP_PIXEL);
  193. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  194. if (drm)
  195. drm_dev_exit(idx);
  196. }
  197. #ifdef CONFIG_DRM_VC4_HDMI_CEC
  198. static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi)
  199. {
  200. struct drm_device *drm = vc4_hdmi->connector.dev;
  201. unsigned long cec_rate;
  202. unsigned long flags;
  203. u16 clk_cnt;
  204. u32 value;
  205. int idx;
  206. /*
  207. * This function is called by our runtime_resume implementation
  208. * and thus at bind time, when we haven't registered our
  209. * connector yet and thus don't have a pointer to the DRM
  210. * device.
  211. */
  212. if (drm && !drm_dev_enter(drm, &idx))
  213. return;
  214. cec_rate = clk_get_rate(vc4_hdmi->cec_clock);
  215. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  216. value = HDMI_READ(HDMI_CEC_CNTRL_1);
  217. value &= ~VC4_HDMI_CEC_DIV_CLK_CNT_MASK;
  218. /*
  219. * Set the clock divider: the hsm_clock rate and this divider
  220. * setting will give a 40 kHz CEC clock.
  221. */
  222. clk_cnt = cec_rate / CEC_CLOCK_FREQ;
  223. value |= clk_cnt << VC4_HDMI_CEC_DIV_CLK_CNT_SHIFT;
  224. HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
  225. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  226. if (drm)
  227. drm_dev_exit(idx);
  228. }
  229. #else
  230. static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi *vc4_hdmi) {}
  231. #endif
  232. static int reset_pipe(struct drm_crtc *crtc,
  233. struct drm_modeset_acquire_ctx *ctx)
  234. {
  235. struct drm_atomic_state *state;
  236. struct drm_crtc_state *crtc_state;
  237. int ret;
  238. state = drm_atomic_state_alloc(crtc->dev);
  239. if (!state)
  240. return -ENOMEM;
  241. state->acquire_ctx = ctx;
  242. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  243. if (IS_ERR(crtc_state)) {
  244. ret = PTR_ERR(crtc_state);
  245. goto out;
  246. }
  247. crtc_state->connectors_changed = true;
  248. ret = drm_atomic_commit(state);
  249. out:
  250. drm_atomic_state_put(state);
  251. return ret;
  252. }
  253. static int vc4_hdmi_reset_link(struct drm_connector *connector,
  254. struct drm_modeset_acquire_ctx *ctx)
  255. {
  256. struct drm_device *drm = connector->dev;
  257. struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
  258. struct drm_encoder *encoder = &vc4_hdmi->encoder.base;
  259. struct drm_connector_state *conn_state;
  260. struct drm_crtc_state *crtc_state;
  261. struct drm_crtc *crtc;
  262. bool scrambling_needed;
  263. u8 config;
  264. int ret;
  265. if (!connector)
  266. return 0;
  267. ret = drm_modeset_lock(&drm->mode_config.connection_mutex, ctx);
  268. if (ret)
  269. return ret;
  270. conn_state = connector->state;
  271. crtc = conn_state->crtc;
  272. if (!crtc)
  273. return 0;
  274. ret = drm_modeset_lock(&crtc->mutex, ctx);
  275. if (ret)
  276. return ret;
  277. crtc_state = crtc->state;
  278. if (!crtc_state->active)
  279. return 0;
  280. mutex_lock(&vc4_hdmi->mutex);
  281. if (!vc4_hdmi_supports_scrambling(encoder)) {
  282. mutex_unlock(&vc4_hdmi->mutex);
  283. return 0;
  284. }
  285. scrambling_needed = vc4_hdmi_mode_needs_scrambling(&vc4_hdmi->saved_adjusted_mode,
  286. vc4_hdmi->output_bpc,
  287. vc4_hdmi->output_format);
  288. if (!scrambling_needed) {
  289. mutex_unlock(&vc4_hdmi->mutex);
  290. return 0;
  291. }
  292. if (conn_state->commit &&
  293. !try_wait_for_completion(&conn_state->commit->hw_done)) {
  294. mutex_unlock(&vc4_hdmi->mutex);
  295. return 0;
  296. }
  297. ret = drm_scdc_readb(connector->ddc, SCDC_TMDS_CONFIG, &config);
  298. if (ret < 0) {
  299. drm_err(drm, "Failed to read TMDS config: %d\n", ret);
  300. mutex_unlock(&vc4_hdmi->mutex);
  301. return 0;
  302. }
  303. if (!!(config & SCDC_SCRAMBLING_ENABLE) == scrambling_needed) {
  304. mutex_unlock(&vc4_hdmi->mutex);
  305. return 0;
  306. }
  307. mutex_unlock(&vc4_hdmi->mutex);
  308. /*
  309. * HDMI 2.0 says that one should not send scrambled data
  310. * prior to configuring the sink scrambling, and that
  311. * TMDS clock/data transmission should be suspended when
  312. * changing the TMDS clock rate in the sink. So let's
  313. * just do a full modeset here, even though some sinks
  314. * would be perfectly happy if were to just reconfigure
  315. * the SCDC settings on the fly.
  316. */
  317. return reset_pipe(crtc, ctx);
  318. }
  319. static void vc4_hdmi_handle_hotplug(struct vc4_hdmi *vc4_hdmi,
  320. struct drm_modeset_acquire_ctx *ctx,
  321. enum drm_connector_status status)
  322. {
  323. struct drm_connector *connector = &vc4_hdmi->connector;
  324. struct edid *edid;
  325. int ret;
  326. /*
  327. * NOTE: This function should really be called with
  328. * vc4_hdmi->mutex held, but doing so results in reentrancy
  329. * issues since cec_s_phys_addr_from_edid might call
  330. * .adap_enable, which leads to that funtion being called with
  331. * our mutex held.
  332. *
  333. * A similar situation occurs with vc4_hdmi_reset_link() that
  334. * will call into our KMS hooks if the scrambling was enabled.
  335. *
  336. * Concurrency isn't an issue at the moment since we don't share
  337. * any state with any of the other frameworks so we can ignore
  338. * the lock for now.
  339. */
  340. if (status == connector_status_disconnected) {
  341. cec_phys_addr_invalidate(vc4_hdmi->cec_adap);
  342. return;
  343. }
  344. edid = drm_get_edid(connector, vc4_hdmi->ddc);
  345. if (!edid)
  346. return;
  347. cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
  348. kfree(edid);
  349. for (;;) {
  350. ret = vc4_hdmi_reset_link(connector, ctx);
  351. if (ret == -EDEADLK) {
  352. drm_modeset_backoff(ctx);
  353. continue;
  354. }
  355. break;
  356. }
  357. }
  358. static int vc4_hdmi_connector_detect_ctx(struct drm_connector *connector,
  359. struct drm_modeset_acquire_ctx *ctx,
  360. bool force)
  361. {
  362. struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
  363. enum drm_connector_status status = connector_status_disconnected;
  364. /*
  365. * NOTE: This function should really take vc4_hdmi->mutex, but
  366. * doing so results in reentrancy issues since
  367. * vc4_hdmi_handle_hotplug() can call into other functions that
  368. * would take the mutex while it's held here.
  369. *
  370. * Concurrency isn't an issue at the moment since we don't share
  371. * any state with any of the other frameworks so we can ignore
  372. * the lock for now.
  373. */
  374. WARN_ON(pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev));
  375. if (vc4_hdmi->hpd_gpio) {
  376. if (gpiod_get_value_cansleep(vc4_hdmi->hpd_gpio))
  377. status = connector_status_connected;
  378. } else {
  379. if (vc4_hdmi->variant->hp_detect &&
  380. vc4_hdmi->variant->hp_detect(vc4_hdmi))
  381. status = connector_status_connected;
  382. }
  383. vc4_hdmi_handle_hotplug(vc4_hdmi, ctx, status);
  384. pm_runtime_put(&vc4_hdmi->pdev->dev);
  385. return status;
  386. }
  387. static int vc4_hdmi_connector_get_modes(struct drm_connector *connector)
  388. {
  389. struct vc4_hdmi *vc4_hdmi = connector_to_vc4_hdmi(connector);
  390. int ret = 0;
  391. struct edid *edid;
  392. /*
  393. * NOTE: This function should really take vc4_hdmi->mutex, but
  394. * doing so results in reentrancy issues since
  395. * cec_s_phys_addr_from_edid might call .adap_enable, which
  396. * leads to that funtion being called with our mutex held.
  397. *
  398. * Concurrency isn't an issue at the moment since we don't share
  399. * any state with any of the other frameworks so we can ignore
  400. * the lock for now.
  401. */
  402. edid = drm_get_edid(connector, vc4_hdmi->ddc);
  403. cec_s_phys_addr_from_edid(vc4_hdmi->cec_adap, edid);
  404. if (!edid)
  405. return -ENODEV;
  406. drm_connector_update_edid_property(connector, edid);
  407. ret = drm_add_edid_modes(connector, edid);
  408. kfree(edid);
  409. if (vc4_hdmi->disable_4kp60) {
  410. struct drm_device *drm = connector->dev;
  411. const struct drm_display_mode *mode;
  412. list_for_each_entry(mode, &connector->probed_modes, head) {
  413. if (vc4_hdmi_mode_needs_scrambling(mode, 8, VC4_HDMI_OUTPUT_RGB)) {
  414. drm_warn_once(drm, "The core clock cannot reach frequencies high enough to support 4k @ 60Hz.");
  415. drm_warn_once(drm, "Please change your config.txt file to add hdmi_enable_4kp60.");
  416. }
  417. }
  418. }
  419. return ret;
  420. }
  421. static int vc4_hdmi_connector_atomic_check(struct drm_connector *connector,
  422. struct drm_atomic_state *state)
  423. {
  424. struct drm_connector_state *old_state =
  425. drm_atomic_get_old_connector_state(state, connector);
  426. struct drm_connector_state *new_state =
  427. drm_atomic_get_new_connector_state(state, connector);
  428. struct drm_crtc *crtc = new_state->crtc;
  429. if (!crtc)
  430. return 0;
  431. if (old_state->colorspace != new_state->colorspace ||
  432. !drm_connector_atomic_hdr_metadata_equal(old_state, new_state)) {
  433. struct drm_crtc_state *crtc_state;
  434. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  435. if (IS_ERR(crtc_state))
  436. return PTR_ERR(crtc_state);
  437. crtc_state->mode_changed = true;
  438. }
  439. return 0;
  440. }
  441. static void vc4_hdmi_connector_reset(struct drm_connector *connector)
  442. {
  443. struct vc4_hdmi_connector_state *old_state =
  444. conn_state_to_vc4_hdmi_conn_state(connector->state);
  445. struct vc4_hdmi_connector_state *new_state =
  446. kzalloc(sizeof(*new_state), GFP_KERNEL);
  447. if (connector->state)
  448. __drm_atomic_helper_connector_destroy_state(connector->state);
  449. kfree(old_state);
  450. __drm_atomic_helper_connector_reset(connector, &new_state->base);
  451. if (!new_state)
  452. return;
  453. new_state->base.max_bpc = 8;
  454. new_state->base.max_requested_bpc = 8;
  455. new_state->output_format = VC4_HDMI_OUTPUT_RGB;
  456. drm_atomic_helper_connector_tv_reset(connector);
  457. }
  458. static struct drm_connector_state *
  459. vc4_hdmi_connector_duplicate_state(struct drm_connector *connector)
  460. {
  461. struct drm_connector_state *conn_state = connector->state;
  462. struct vc4_hdmi_connector_state *vc4_state = conn_state_to_vc4_hdmi_conn_state(conn_state);
  463. struct vc4_hdmi_connector_state *new_state;
  464. new_state = kzalloc(sizeof(*new_state), GFP_KERNEL);
  465. if (!new_state)
  466. return NULL;
  467. new_state->tmds_char_rate = vc4_state->tmds_char_rate;
  468. new_state->output_bpc = vc4_state->output_bpc;
  469. new_state->output_format = vc4_state->output_format;
  470. __drm_atomic_helper_connector_duplicate_state(connector, &new_state->base);
  471. return &new_state->base;
  472. }
  473. static const struct drm_connector_funcs vc4_hdmi_connector_funcs = {
  474. .fill_modes = drm_helper_probe_single_connector_modes,
  475. .reset = vc4_hdmi_connector_reset,
  476. .atomic_duplicate_state = vc4_hdmi_connector_duplicate_state,
  477. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  478. };
  479. static const struct drm_connector_helper_funcs vc4_hdmi_connector_helper_funcs = {
  480. .detect_ctx = vc4_hdmi_connector_detect_ctx,
  481. .get_modes = vc4_hdmi_connector_get_modes,
  482. .atomic_check = vc4_hdmi_connector_atomic_check,
  483. };
  484. static int vc4_hdmi_connector_init(struct drm_device *dev,
  485. struct vc4_hdmi *vc4_hdmi)
  486. {
  487. struct drm_connector *connector = &vc4_hdmi->connector;
  488. struct drm_encoder *encoder = &vc4_hdmi->encoder.base;
  489. int ret;
  490. ret = drmm_connector_init(dev, connector,
  491. &vc4_hdmi_connector_funcs,
  492. DRM_MODE_CONNECTOR_HDMIA,
  493. vc4_hdmi->ddc);
  494. if (ret)
  495. return ret;
  496. drm_connector_helper_add(connector, &vc4_hdmi_connector_helper_funcs);
  497. /*
  498. * Some of the properties below require access to state, like bpc.
  499. * Allocate some default initial connector state with our reset helper.
  500. */
  501. if (connector->funcs->reset)
  502. connector->funcs->reset(connector);
  503. /* Create and attach TV margin props to this connector. */
  504. ret = drm_mode_create_tv_margin_properties(dev);
  505. if (ret)
  506. return ret;
  507. ret = drm_mode_create_hdmi_colorspace_property(connector);
  508. if (ret)
  509. return ret;
  510. drm_connector_attach_colorspace_property(connector);
  511. drm_connector_attach_tv_margin_properties(connector);
  512. drm_connector_attach_max_bpc_property(connector, 8, 12);
  513. connector->polled = (DRM_CONNECTOR_POLL_CONNECT |
  514. DRM_CONNECTOR_POLL_DISCONNECT);
  515. connector->interlace_allowed = 1;
  516. connector->doublescan_allowed = 0;
  517. connector->stereo_allowed = 1;
  518. if (vc4_hdmi->variant->supports_hdr)
  519. drm_connector_attach_hdr_output_metadata_property(connector);
  520. drm_connector_attach_encoder(connector, encoder);
  521. return 0;
  522. }
  523. static int vc4_hdmi_stop_packet(struct drm_encoder *encoder,
  524. enum hdmi_infoframe_type type,
  525. bool poll)
  526. {
  527. struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
  528. struct drm_device *drm = vc4_hdmi->connector.dev;
  529. u32 packet_id = type - 0x80;
  530. unsigned long flags;
  531. int ret = 0;
  532. int idx;
  533. if (!drm_dev_enter(drm, &idx))
  534. return -ENODEV;
  535. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  536. HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
  537. HDMI_READ(HDMI_RAM_PACKET_CONFIG) & ~BIT(packet_id));
  538. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  539. if (poll) {
  540. ret = wait_for(!(HDMI_READ(HDMI_RAM_PACKET_STATUS) &
  541. BIT(packet_id)), 100);
  542. }
  543. drm_dev_exit(idx);
  544. return ret;
  545. }
  546. static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder,
  547. union hdmi_infoframe *frame)
  548. {
  549. struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
  550. struct drm_device *drm = vc4_hdmi->connector.dev;
  551. u32 packet_id = frame->any.type - 0x80;
  552. const struct vc4_hdmi_register *ram_packet_start =
  553. &vc4_hdmi->variant->registers[HDMI_RAM_PACKET_START];
  554. u32 packet_reg = ram_packet_start->offset + VC4_HDMI_PACKET_STRIDE * packet_id;
  555. u32 packet_reg_next = ram_packet_start->offset +
  556. VC4_HDMI_PACKET_STRIDE * (packet_id + 1);
  557. void __iomem *base = __vc4_hdmi_get_field_base(vc4_hdmi,
  558. ram_packet_start->reg);
  559. uint8_t buffer[VC4_HDMI_PACKET_STRIDE] = {};
  560. unsigned long flags;
  561. ssize_t len, i;
  562. int ret;
  563. int idx;
  564. if (!drm_dev_enter(drm, &idx))
  565. return;
  566. WARN_ONCE(!(HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
  567. VC4_HDMI_RAM_PACKET_ENABLE),
  568. "Packet RAM has to be on to store the packet.");
  569. len = hdmi_infoframe_pack(frame, buffer, sizeof(buffer));
  570. if (len < 0)
  571. goto out;
  572. ret = vc4_hdmi_stop_packet(encoder, frame->any.type, true);
  573. if (ret) {
  574. DRM_ERROR("Failed to wait for infoframe to go idle: %d\n", ret);
  575. goto out;
  576. }
  577. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  578. for (i = 0; i < len; i += 7) {
  579. writel(buffer[i + 0] << 0 |
  580. buffer[i + 1] << 8 |
  581. buffer[i + 2] << 16,
  582. base + packet_reg);
  583. packet_reg += 4;
  584. writel(buffer[i + 3] << 0 |
  585. buffer[i + 4] << 8 |
  586. buffer[i + 5] << 16 |
  587. buffer[i + 6] << 24,
  588. base + packet_reg);
  589. packet_reg += 4;
  590. }
  591. /*
  592. * clear remainder of packet ram as it's included in the
  593. * infoframe and triggers a checksum error on hdmi analyser
  594. */
  595. for (; packet_reg < packet_reg_next; packet_reg += 4)
  596. writel(0, base + packet_reg);
  597. HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
  598. HDMI_READ(HDMI_RAM_PACKET_CONFIG) | BIT(packet_id));
  599. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  600. ret = wait_for((HDMI_READ(HDMI_RAM_PACKET_STATUS) &
  601. BIT(packet_id)), 100);
  602. if (ret)
  603. DRM_ERROR("Failed to wait for infoframe to start: %d\n", ret);
  604. out:
  605. drm_dev_exit(idx);
  606. }
  607. static void vc4_hdmi_avi_infoframe_colorspace(struct hdmi_avi_infoframe *frame,
  608. enum vc4_hdmi_output_format fmt)
  609. {
  610. switch (fmt) {
  611. case VC4_HDMI_OUTPUT_RGB:
  612. frame->colorspace = HDMI_COLORSPACE_RGB;
  613. break;
  614. case VC4_HDMI_OUTPUT_YUV420:
  615. frame->colorspace = HDMI_COLORSPACE_YUV420;
  616. break;
  617. case VC4_HDMI_OUTPUT_YUV422:
  618. frame->colorspace = HDMI_COLORSPACE_YUV422;
  619. break;
  620. case VC4_HDMI_OUTPUT_YUV444:
  621. frame->colorspace = HDMI_COLORSPACE_YUV444;
  622. break;
  623. default:
  624. break;
  625. }
  626. }
  627. static void vc4_hdmi_set_avi_infoframe(struct drm_encoder *encoder)
  628. {
  629. struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
  630. struct drm_connector *connector = &vc4_hdmi->connector;
  631. struct drm_connector_state *cstate = connector->state;
  632. struct vc4_hdmi_connector_state *vc4_state =
  633. conn_state_to_vc4_hdmi_conn_state(cstate);
  634. const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
  635. union hdmi_infoframe frame;
  636. int ret;
  637. lockdep_assert_held(&vc4_hdmi->mutex);
  638. ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi,
  639. connector, mode);
  640. if (ret < 0) {
  641. DRM_ERROR("couldn't fill AVI infoframe\n");
  642. return;
  643. }
  644. drm_hdmi_avi_infoframe_quant_range(&frame.avi,
  645. connector, mode,
  646. vc4_hdmi_is_full_range_rgb(vc4_hdmi, mode) ?
  647. HDMI_QUANTIZATION_RANGE_FULL :
  648. HDMI_QUANTIZATION_RANGE_LIMITED);
  649. drm_hdmi_avi_infoframe_colorimetry(&frame.avi, cstate);
  650. vc4_hdmi_avi_infoframe_colorspace(&frame.avi, vc4_state->output_format);
  651. drm_hdmi_avi_infoframe_bars(&frame.avi, cstate);
  652. vc4_hdmi_write_infoframe(encoder, &frame);
  653. }
  654. static void vc4_hdmi_set_spd_infoframe(struct drm_encoder *encoder)
  655. {
  656. union hdmi_infoframe frame;
  657. int ret;
  658. ret = hdmi_spd_infoframe_init(&frame.spd, "Broadcom", "Videocore");
  659. if (ret < 0) {
  660. DRM_ERROR("couldn't fill SPD infoframe\n");
  661. return;
  662. }
  663. frame.spd.sdi = HDMI_SPD_SDI_PC;
  664. vc4_hdmi_write_infoframe(encoder, &frame);
  665. }
  666. static void vc4_hdmi_set_audio_infoframe(struct drm_encoder *encoder)
  667. {
  668. struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
  669. struct hdmi_audio_infoframe *audio = &vc4_hdmi->audio.infoframe;
  670. union hdmi_infoframe frame;
  671. memcpy(&frame.audio, audio, sizeof(*audio));
  672. if (vc4_hdmi->packet_ram_enabled)
  673. vc4_hdmi_write_infoframe(encoder, &frame);
  674. }
  675. static void vc4_hdmi_set_hdr_infoframe(struct drm_encoder *encoder)
  676. {
  677. struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
  678. struct drm_connector *connector = &vc4_hdmi->connector;
  679. struct drm_connector_state *conn_state = connector->state;
  680. union hdmi_infoframe frame;
  681. lockdep_assert_held(&vc4_hdmi->mutex);
  682. if (!vc4_hdmi->variant->supports_hdr)
  683. return;
  684. if (!conn_state->hdr_output_metadata)
  685. return;
  686. if (drm_hdmi_infoframe_set_hdr_metadata(&frame.drm, conn_state))
  687. return;
  688. vc4_hdmi_write_infoframe(encoder, &frame);
  689. }
  690. static void vc4_hdmi_set_infoframes(struct drm_encoder *encoder)
  691. {
  692. struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
  693. lockdep_assert_held(&vc4_hdmi->mutex);
  694. vc4_hdmi_set_avi_infoframe(encoder);
  695. vc4_hdmi_set_spd_infoframe(encoder);
  696. /*
  697. * If audio was streaming, then we need to reenabled the audio
  698. * infoframe here during encoder_enable.
  699. */
  700. if (vc4_hdmi->audio.streaming)
  701. vc4_hdmi_set_audio_infoframe(encoder);
  702. vc4_hdmi_set_hdr_infoframe(encoder);
  703. }
  704. #define SCRAMBLING_POLLING_DELAY_MS 1000
  705. static void vc4_hdmi_enable_scrambling(struct drm_encoder *encoder)
  706. {
  707. struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
  708. struct drm_device *drm = vc4_hdmi->connector.dev;
  709. const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
  710. unsigned long flags;
  711. int idx;
  712. lockdep_assert_held(&vc4_hdmi->mutex);
  713. if (!vc4_hdmi_supports_scrambling(encoder))
  714. return;
  715. if (!vc4_hdmi_mode_needs_scrambling(mode,
  716. vc4_hdmi->output_bpc,
  717. vc4_hdmi->output_format))
  718. return;
  719. if (!drm_dev_enter(drm, &idx))
  720. return;
  721. drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, true);
  722. drm_scdc_set_scrambling(vc4_hdmi->ddc, true);
  723. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  724. HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) |
  725. VC5_HDMI_SCRAMBLER_CTL_ENABLE);
  726. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  727. drm_dev_exit(idx);
  728. vc4_hdmi->scdc_enabled = true;
  729. queue_delayed_work(system_wq, &vc4_hdmi->scrambling_work,
  730. msecs_to_jiffies(SCRAMBLING_POLLING_DELAY_MS));
  731. }
  732. static void vc4_hdmi_disable_scrambling(struct drm_encoder *encoder)
  733. {
  734. struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
  735. struct drm_device *drm = vc4_hdmi->connector.dev;
  736. unsigned long flags;
  737. int idx;
  738. lockdep_assert_held(&vc4_hdmi->mutex);
  739. if (!vc4_hdmi->scdc_enabled)
  740. return;
  741. vc4_hdmi->scdc_enabled = false;
  742. if (delayed_work_pending(&vc4_hdmi->scrambling_work))
  743. cancel_delayed_work_sync(&vc4_hdmi->scrambling_work);
  744. if (!drm_dev_enter(drm, &idx))
  745. return;
  746. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  747. HDMI_WRITE(HDMI_SCRAMBLER_CTL, HDMI_READ(HDMI_SCRAMBLER_CTL) &
  748. ~VC5_HDMI_SCRAMBLER_CTL_ENABLE);
  749. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  750. drm_scdc_set_scrambling(vc4_hdmi->ddc, false);
  751. drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, false);
  752. drm_dev_exit(idx);
  753. }
  754. static void vc4_hdmi_scrambling_wq(struct work_struct *work)
  755. {
  756. struct vc4_hdmi *vc4_hdmi = container_of(to_delayed_work(work),
  757. struct vc4_hdmi,
  758. scrambling_work);
  759. if (drm_scdc_get_scrambling_status(vc4_hdmi->ddc))
  760. return;
  761. drm_scdc_set_high_tmds_clock_ratio(vc4_hdmi->ddc, true);
  762. drm_scdc_set_scrambling(vc4_hdmi->ddc, true);
  763. queue_delayed_work(system_wq, &vc4_hdmi->scrambling_work,
  764. msecs_to_jiffies(SCRAMBLING_POLLING_DELAY_MS));
  765. }
  766. static void vc4_hdmi_encoder_post_crtc_disable(struct drm_encoder *encoder,
  767. struct drm_atomic_state *state)
  768. {
  769. struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
  770. struct drm_device *drm = vc4_hdmi->connector.dev;
  771. unsigned long flags;
  772. int idx;
  773. mutex_lock(&vc4_hdmi->mutex);
  774. vc4_hdmi->packet_ram_enabled = false;
  775. if (!drm_dev_enter(drm, &idx))
  776. goto out;
  777. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  778. HDMI_WRITE(HDMI_RAM_PACKET_CONFIG, 0);
  779. HDMI_WRITE(HDMI_VID_CTL, HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_CLRRGB);
  780. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  781. mdelay(1);
  782. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  783. HDMI_WRITE(HDMI_VID_CTL,
  784. HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_ENABLE);
  785. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  786. vc4_hdmi_disable_scrambling(encoder);
  787. drm_dev_exit(idx);
  788. out:
  789. mutex_unlock(&vc4_hdmi->mutex);
  790. }
  791. static void vc4_hdmi_encoder_post_crtc_powerdown(struct drm_encoder *encoder,
  792. struct drm_atomic_state *state)
  793. {
  794. struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
  795. struct drm_device *drm = vc4_hdmi->connector.dev;
  796. unsigned long flags;
  797. int ret;
  798. int idx;
  799. mutex_lock(&vc4_hdmi->mutex);
  800. if (!drm_dev_enter(drm, &idx))
  801. goto out;
  802. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  803. HDMI_WRITE(HDMI_VID_CTL,
  804. HDMI_READ(HDMI_VID_CTL) | VC4_HD_VID_CTL_BLANKPIX);
  805. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  806. if (vc4_hdmi->variant->phy_disable)
  807. vc4_hdmi->variant->phy_disable(vc4_hdmi);
  808. clk_disable_unprepare(vc4_hdmi->pixel_bvb_clock);
  809. clk_disable_unprepare(vc4_hdmi->pixel_clock);
  810. ret = pm_runtime_put(&vc4_hdmi->pdev->dev);
  811. if (ret < 0)
  812. DRM_ERROR("Failed to release power domain: %d\n", ret);
  813. drm_dev_exit(idx);
  814. out:
  815. mutex_unlock(&vc4_hdmi->mutex);
  816. }
  817. static void vc4_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi,
  818. struct drm_connector_state *state,
  819. const struct drm_display_mode *mode)
  820. {
  821. struct drm_device *drm = vc4_hdmi->connector.dev;
  822. unsigned long flags;
  823. u32 csc_ctl;
  824. int idx;
  825. if (!drm_dev_enter(drm, &idx))
  826. return;
  827. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  828. csc_ctl = VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR,
  829. VC4_HD_CSC_CTL_ORDER);
  830. if (!vc4_hdmi_is_full_range_rgb(vc4_hdmi, mode)) {
  831. /* CEA VICs other than #1 requre limited range RGB
  832. * output unless overridden by an AVI infoframe.
  833. * Apply a colorspace conversion to squash 0-255 down
  834. * to 16-235. The matrix here is:
  835. *
  836. * [ 0 0 0.8594 16]
  837. * [ 0 0.8594 0 16]
  838. * [ 0.8594 0 0 16]
  839. * [ 0 0 0 1]
  840. */
  841. csc_ctl |= VC4_HD_CSC_CTL_ENABLE;
  842. csc_ctl |= VC4_HD_CSC_CTL_RGB2YCC;
  843. csc_ctl |= VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
  844. VC4_HD_CSC_CTL_MODE);
  845. HDMI_WRITE(HDMI_CSC_12_11, (0x000 << 16) | 0x000);
  846. HDMI_WRITE(HDMI_CSC_14_13, (0x100 << 16) | 0x6e0);
  847. HDMI_WRITE(HDMI_CSC_22_21, (0x6e0 << 16) | 0x000);
  848. HDMI_WRITE(HDMI_CSC_24_23, (0x100 << 16) | 0x000);
  849. HDMI_WRITE(HDMI_CSC_32_31, (0x000 << 16) | 0x6e0);
  850. HDMI_WRITE(HDMI_CSC_34_33, (0x100 << 16) | 0x000);
  851. }
  852. /* The RGB order applies even when CSC is disabled. */
  853. HDMI_WRITE(HDMI_CSC_CTL, csc_ctl);
  854. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  855. drm_dev_exit(idx);
  856. }
  857. /*
  858. * If we need to output Full Range RGB, then use the unity matrix
  859. *
  860. * [ 1 0 0 0]
  861. * [ 0 1 0 0]
  862. * [ 0 0 1 0]
  863. *
  864. * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
  865. */
  866. static const u16 vc5_hdmi_csc_full_rgb_unity[3][4] = {
  867. { 0x2000, 0x0000, 0x0000, 0x0000 },
  868. { 0x0000, 0x2000, 0x0000, 0x0000 },
  869. { 0x0000, 0x0000, 0x2000, 0x0000 },
  870. };
  871. /*
  872. * CEA VICs other than #1 require limited range RGB output unless
  873. * overridden by an AVI infoframe. Apply a colorspace conversion to
  874. * squash 0-255 down to 16-235. The matrix here is:
  875. *
  876. * [ 0.8594 0 0 16]
  877. * [ 0 0.8594 0 16]
  878. * [ 0 0 0.8594 16]
  879. *
  880. * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
  881. */
  882. static const u16 vc5_hdmi_csc_full_rgb_to_limited_rgb[3][4] = {
  883. { 0x1b80, 0x0000, 0x0000, 0x0400 },
  884. { 0x0000, 0x1b80, 0x0000, 0x0400 },
  885. { 0x0000, 0x0000, 0x1b80, 0x0400 },
  886. };
  887. /*
  888. * Conversion between Full Range RGB and Full Range YUV422 using the
  889. * BT.709 Colorspace
  890. *
  891. *
  892. * [ 0.181906 0.611804 0.061758 16 ]
  893. * [ -0.100268 -0.337232 0.437500 128 ]
  894. * [ 0.437500 -0.397386 -0.040114 128 ]
  895. *
  896. * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
  897. */
  898. static const u16 vc5_hdmi_csc_full_rgb_to_limited_yuv422_bt709[3][4] = {
  899. { 0x05d2, 0x1394, 0x01fa, 0x0400 },
  900. { 0xfccc, 0xf536, 0x0e00, 0x2000 },
  901. { 0x0e00, 0xf34a, 0xfeb8, 0x2000 },
  902. };
  903. /*
  904. * Conversion between Full Range RGB and Full Range YUV444 using the
  905. * BT.709 Colorspace
  906. *
  907. * [ -0.100268 -0.337232 0.437500 128 ]
  908. * [ 0.437500 -0.397386 -0.040114 128 ]
  909. * [ 0.181906 0.611804 0.061758 16 ]
  910. *
  911. * Matrix is signed 2p13 fixed point, with signed 9p6 offsets
  912. */
  913. static const u16 vc5_hdmi_csc_full_rgb_to_limited_yuv444_bt709[3][4] = {
  914. { 0xfccc, 0xf536, 0x0e00, 0x2000 },
  915. { 0x0e00, 0xf34a, 0xfeb8, 0x2000 },
  916. { 0x05d2, 0x1394, 0x01fa, 0x0400 },
  917. };
  918. static void vc5_hdmi_set_csc_coeffs(struct vc4_hdmi *vc4_hdmi,
  919. const u16 coeffs[3][4])
  920. {
  921. lockdep_assert_held(&vc4_hdmi->hw_lock);
  922. HDMI_WRITE(HDMI_CSC_12_11, (coeffs[0][1] << 16) | coeffs[0][0]);
  923. HDMI_WRITE(HDMI_CSC_14_13, (coeffs[0][3] << 16) | coeffs[0][2]);
  924. HDMI_WRITE(HDMI_CSC_22_21, (coeffs[1][1] << 16) | coeffs[1][0]);
  925. HDMI_WRITE(HDMI_CSC_24_23, (coeffs[1][3] << 16) | coeffs[1][2]);
  926. HDMI_WRITE(HDMI_CSC_32_31, (coeffs[2][1] << 16) | coeffs[2][0]);
  927. HDMI_WRITE(HDMI_CSC_34_33, (coeffs[2][3] << 16) | coeffs[2][2]);
  928. }
  929. static void vc5_hdmi_csc_setup(struct vc4_hdmi *vc4_hdmi,
  930. struct drm_connector_state *state,
  931. const struct drm_display_mode *mode)
  932. {
  933. struct drm_device *drm = vc4_hdmi->connector.dev;
  934. struct vc4_hdmi_connector_state *vc4_state =
  935. conn_state_to_vc4_hdmi_conn_state(state);
  936. unsigned long flags;
  937. u32 if_cfg = 0;
  938. u32 if_xbar = 0x543210;
  939. u32 csc_chan_ctl = 0;
  940. u32 csc_ctl = VC5_MT_CP_CSC_CTL_ENABLE | VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
  941. VC5_MT_CP_CSC_CTL_MODE);
  942. int idx;
  943. if (!drm_dev_enter(drm, &idx))
  944. return;
  945. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  946. switch (vc4_state->output_format) {
  947. case VC4_HDMI_OUTPUT_YUV444:
  948. vc5_hdmi_set_csc_coeffs(vc4_hdmi, vc5_hdmi_csc_full_rgb_to_limited_yuv444_bt709);
  949. break;
  950. case VC4_HDMI_OUTPUT_YUV422:
  951. csc_ctl |= VC4_SET_FIELD(VC5_MT_CP_CSC_CTL_FILTER_MODE_444_TO_422_STANDARD,
  952. VC5_MT_CP_CSC_CTL_FILTER_MODE_444_TO_422) |
  953. VC5_MT_CP_CSC_CTL_USE_444_TO_422 |
  954. VC5_MT_CP_CSC_CTL_USE_RNG_SUPPRESSION;
  955. csc_chan_ctl |= VC4_SET_FIELD(VC5_MT_CP_CHANNEL_CTL_OUTPUT_REMAP_LEGACY_STYLE,
  956. VC5_MT_CP_CHANNEL_CTL_OUTPUT_REMAP);
  957. if_cfg |= VC4_SET_FIELD(VC5_DVP_HT_VEC_INTERFACE_CFG_SEL_422_FORMAT_422_LEGACY,
  958. VC5_DVP_HT_VEC_INTERFACE_CFG_SEL_422);
  959. vc5_hdmi_set_csc_coeffs(vc4_hdmi, vc5_hdmi_csc_full_rgb_to_limited_yuv422_bt709);
  960. break;
  961. case VC4_HDMI_OUTPUT_RGB:
  962. if_xbar = 0x354021;
  963. if (!vc4_hdmi_is_full_range_rgb(vc4_hdmi, mode))
  964. vc5_hdmi_set_csc_coeffs(vc4_hdmi, vc5_hdmi_csc_full_rgb_to_limited_rgb);
  965. else
  966. vc5_hdmi_set_csc_coeffs(vc4_hdmi, vc5_hdmi_csc_full_rgb_unity);
  967. break;
  968. default:
  969. break;
  970. }
  971. HDMI_WRITE(HDMI_VEC_INTERFACE_CFG, if_cfg);
  972. HDMI_WRITE(HDMI_VEC_INTERFACE_XBAR, if_xbar);
  973. HDMI_WRITE(HDMI_CSC_CHANNEL_CTL, csc_chan_ctl);
  974. HDMI_WRITE(HDMI_CSC_CTL, csc_ctl);
  975. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  976. drm_dev_exit(idx);
  977. }
  978. static void vc4_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
  979. struct drm_connector_state *state,
  980. const struct drm_display_mode *mode)
  981. {
  982. struct drm_device *drm = vc4_hdmi->connector.dev;
  983. bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
  984. bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
  985. bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
  986. u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
  987. u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
  988. VC4_HDMI_VERTA_VSP) |
  989. VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
  990. VC4_HDMI_VERTA_VFP) |
  991. VC4_SET_FIELD(mode->crtc_vdisplay, VC4_HDMI_VERTA_VAL));
  992. u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
  993. VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end +
  994. interlaced,
  995. VC4_HDMI_VERTB_VBP));
  996. u32 vertb_even = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
  997. VC4_SET_FIELD(mode->crtc_vtotal -
  998. mode->crtc_vsync_end,
  999. VC4_HDMI_VERTB_VBP));
  1000. unsigned long flags;
  1001. u32 reg;
  1002. int idx;
  1003. if (!drm_dev_enter(drm, &idx))
  1004. return;
  1005. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  1006. HDMI_WRITE(HDMI_HORZA,
  1007. (vsync_pos ? VC4_HDMI_HORZA_VPOS : 0) |
  1008. (hsync_pos ? VC4_HDMI_HORZA_HPOS : 0) |
  1009. VC4_SET_FIELD(mode->hdisplay * pixel_rep,
  1010. VC4_HDMI_HORZA_HAP));
  1011. HDMI_WRITE(HDMI_HORZB,
  1012. VC4_SET_FIELD((mode->htotal -
  1013. mode->hsync_end) * pixel_rep,
  1014. VC4_HDMI_HORZB_HBP) |
  1015. VC4_SET_FIELD((mode->hsync_end -
  1016. mode->hsync_start) * pixel_rep,
  1017. VC4_HDMI_HORZB_HSP) |
  1018. VC4_SET_FIELD((mode->hsync_start -
  1019. mode->hdisplay) * pixel_rep,
  1020. VC4_HDMI_HORZB_HFP));
  1021. HDMI_WRITE(HDMI_VERTA0, verta);
  1022. HDMI_WRITE(HDMI_VERTA1, verta);
  1023. HDMI_WRITE(HDMI_VERTB0, vertb_even);
  1024. HDMI_WRITE(HDMI_VERTB1, vertb);
  1025. reg = HDMI_READ(HDMI_MISC_CONTROL);
  1026. reg &= ~VC4_HDMI_MISC_CONTROL_PIXEL_REP_MASK;
  1027. reg |= VC4_SET_FIELD(pixel_rep - 1, VC4_HDMI_MISC_CONTROL_PIXEL_REP);
  1028. HDMI_WRITE(HDMI_MISC_CONTROL, reg);
  1029. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  1030. drm_dev_exit(idx);
  1031. }
  1032. static void vc5_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
  1033. struct drm_connector_state *state,
  1034. const struct drm_display_mode *mode)
  1035. {
  1036. struct drm_device *drm = vc4_hdmi->connector.dev;
  1037. const struct vc4_hdmi_connector_state *vc4_state =
  1038. conn_state_to_vc4_hdmi_conn_state(state);
  1039. bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
  1040. bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
  1041. bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
  1042. u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
  1043. u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
  1044. VC5_HDMI_VERTA_VSP) |
  1045. VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
  1046. VC5_HDMI_VERTA_VFP) |
  1047. VC4_SET_FIELD(mode->crtc_vdisplay, VC5_HDMI_VERTA_VAL));
  1048. u32 vertb = (VC4_SET_FIELD(mode->htotal >> (2 - pixel_rep),
  1049. VC5_HDMI_VERTB_VSPO) |
  1050. VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end +
  1051. interlaced,
  1052. VC4_HDMI_VERTB_VBP));
  1053. u32 vertb_even = (VC4_SET_FIELD(0, VC5_HDMI_VERTB_VSPO) |
  1054. VC4_SET_FIELD(mode->crtc_vtotal -
  1055. mode->crtc_vsync_end,
  1056. VC4_HDMI_VERTB_VBP));
  1057. unsigned long flags;
  1058. unsigned char gcp;
  1059. bool gcp_en;
  1060. u32 reg;
  1061. int idx;
  1062. if (!drm_dev_enter(drm, &idx))
  1063. return;
  1064. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  1065. HDMI_WRITE(HDMI_HORZA,
  1066. (vsync_pos ? VC5_HDMI_HORZA_VPOS : 0) |
  1067. (hsync_pos ? VC5_HDMI_HORZA_HPOS : 0) |
  1068. VC4_SET_FIELD(mode->hdisplay * pixel_rep,
  1069. VC5_HDMI_HORZA_HAP) |
  1070. VC4_SET_FIELD((mode->hsync_start -
  1071. mode->hdisplay) * pixel_rep,
  1072. VC5_HDMI_HORZA_HFP));
  1073. HDMI_WRITE(HDMI_HORZB,
  1074. VC4_SET_FIELD((mode->htotal -
  1075. mode->hsync_end) * pixel_rep,
  1076. VC5_HDMI_HORZB_HBP) |
  1077. VC4_SET_FIELD((mode->hsync_end -
  1078. mode->hsync_start) * pixel_rep,
  1079. VC5_HDMI_HORZB_HSP));
  1080. HDMI_WRITE(HDMI_VERTA0, verta);
  1081. HDMI_WRITE(HDMI_VERTA1, verta);
  1082. HDMI_WRITE(HDMI_VERTB0, vertb_even);
  1083. HDMI_WRITE(HDMI_VERTB1, vertb);
  1084. switch (vc4_state->output_bpc) {
  1085. case 12:
  1086. gcp = 6;
  1087. gcp_en = true;
  1088. break;
  1089. case 10:
  1090. gcp = 5;
  1091. gcp_en = true;
  1092. break;
  1093. case 8:
  1094. default:
  1095. gcp = 4;
  1096. gcp_en = false;
  1097. break;
  1098. }
  1099. /*
  1100. * YCC422 is always 36-bit and not considered deep colour so
  1101. * doesn't signal in GCP.
  1102. */
  1103. if (vc4_state->output_format == VC4_HDMI_OUTPUT_YUV422) {
  1104. gcp = 4;
  1105. gcp_en = false;
  1106. }
  1107. reg = HDMI_READ(HDMI_DEEP_COLOR_CONFIG_1);
  1108. reg &= ~(VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_MASK |
  1109. VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_MASK);
  1110. reg |= VC4_SET_FIELD(2, VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE) |
  1111. VC4_SET_FIELD(gcp, VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH);
  1112. HDMI_WRITE(HDMI_DEEP_COLOR_CONFIG_1, reg);
  1113. reg = HDMI_READ(HDMI_GCP_WORD_1);
  1114. reg &= ~VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_MASK;
  1115. reg |= VC4_SET_FIELD(gcp, VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1);
  1116. HDMI_WRITE(HDMI_GCP_WORD_1, reg);
  1117. reg = HDMI_READ(HDMI_GCP_CONFIG);
  1118. reg &= ~VC5_HDMI_GCP_CONFIG_GCP_ENABLE;
  1119. reg |= gcp_en ? VC5_HDMI_GCP_CONFIG_GCP_ENABLE : 0;
  1120. HDMI_WRITE(HDMI_GCP_CONFIG, reg);
  1121. reg = HDMI_READ(HDMI_MISC_CONTROL);
  1122. reg &= ~VC5_HDMI_MISC_CONTROL_PIXEL_REP_MASK;
  1123. reg |= VC4_SET_FIELD(pixel_rep - 1, VC5_HDMI_MISC_CONTROL_PIXEL_REP);
  1124. HDMI_WRITE(HDMI_MISC_CONTROL, reg);
  1125. HDMI_WRITE(HDMI_CLOCK_STOP, 0);
  1126. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  1127. drm_dev_exit(idx);
  1128. }
  1129. static void vc4_hdmi_recenter_fifo(struct vc4_hdmi *vc4_hdmi)
  1130. {
  1131. struct drm_device *drm = vc4_hdmi->connector.dev;
  1132. unsigned long flags;
  1133. u32 drift;
  1134. int ret;
  1135. int idx;
  1136. if (!drm_dev_enter(drm, &idx))
  1137. return;
  1138. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  1139. drift = HDMI_READ(HDMI_FIFO_CTL);
  1140. drift &= VC4_HDMI_FIFO_VALID_WRITE_MASK;
  1141. HDMI_WRITE(HDMI_FIFO_CTL,
  1142. drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
  1143. HDMI_WRITE(HDMI_FIFO_CTL,
  1144. drift | VC4_HDMI_FIFO_CTL_RECENTER);
  1145. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  1146. usleep_range(1000, 1100);
  1147. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  1148. HDMI_WRITE(HDMI_FIFO_CTL,
  1149. drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
  1150. HDMI_WRITE(HDMI_FIFO_CTL,
  1151. drift | VC4_HDMI_FIFO_CTL_RECENTER);
  1152. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  1153. ret = wait_for(HDMI_READ(HDMI_FIFO_CTL) &
  1154. VC4_HDMI_FIFO_CTL_RECENTER_DONE, 1);
  1155. WARN_ONCE(ret, "Timeout waiting for "
  1156. "VC4_HDMI_FIFO_CTL_RECENTER_DONE");
  1157. drm_dev_exit(idx);
  1158. }
  1159. static void vc4_hdmi_encoder_pre_crtc_configure(struct drm_encoder *encoder,
  1160. struct drm_atomic_state *state)
  1161. {
  1162. struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
  1163. struct drm_device *drm = vc4_hdmi->connector.dev;
  1164. struct drm_connector *connector = &vc4_hdmi->connector;
  1165. struct drm_connector_state *conn_state =
  1166. drm_atomic_get_new_connector_state(state, connector);
  1167. struct vc4_hdmi_connector_state *vc4_conn_state =
  1168. conn_state_to_vc4_hdmi_conn_state(conn_state);
  1169. const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
  1170. unsigned long tmds_char_rate = vc4_conn_state->tmds_char_rate;
  1171. unsigned long bvb_rate, hsm_rate;
  1172. unsigned long flags;
  1173. int ret;
  1174. int idx;
  1175. mutex_lock(&vc4_hdmi->mutex);
  1176. if (!drm_dev_enter(drm, &idx))
  1177. goto out;
  1178. /*
  1179. * As stated in RPi's vc4 firmware "HDMI state machine (HSM) clock must
  1180. * be faster than pixel clock, infinitesimally faster, tested in
  1181. * simulation. Otherwise, exact value is unimportant for HDMI
  1182. * operation." This conflicts with bcm2835's vc4 documentation, which
  1183. * states HSM's clock has to be at least 108% of the pixel clock.
  1184. *
  1185. * Real life tests reveal that vc4's firmware statement holds up, and
  1186. * users are able to use pixel clocks closer to HSM's, namely for
  1187. * 1920x1200@60Hz. So it was decided to have leave a 1% margin between
  1188. * both clocks. Which, for RPi0-3 implies a maximum pixel clock of
  1189. * 162MHz.
  1190. *
  1191. * Additionally, the AXI clock needs to be at least 25% of
  1192. * pixel clock, but HSM ends up being the limiting factor.
  1193. */
  1194. hsm_rate = max_t(unsigned long, 120000000, (tmds_char_rate / 100) * 101);
  1195. ret = clk_set_min_rate(vc4_hdmi->hsm_clock, hsm_rate);
  1196. if (ret) {
  1197. DRM_ERROR("Failed to set HSM clock rate: %d\n", ret);
  1198. goto err_dev_exit;
  1199. }
  1200. ret = pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev);
  1201. if (ret < 0) {
  1202. DRM_ERROR("Failed to retain power domain: %d\n", ret);
  1203. goto err_dev_exit;
  1204. }
  1205. ret = clk_set_rate(vc4_hdmi->pixel_clock, tmds_char_rate);
  1206. if (ret) {
  1207. DRM_ERROR("Failed to set pixel clock rate: %d\n", ret);
  1208. goto err_put_runtime_pm;
  1209. }
  1210. ret = clk_prepare_enable(vc4_hdmi->pixel_clock);
  1211. if (ret) {
  1212. DRM_ERROR("Failed to turn on pixel clock: %d\n", ret);
  1213. goto err_put_runtime_pm;
  1214. }
  1215. vc4_hdmi_cec_update_clk_div(vc4_hdmi);
  1216. if (tmds_char_rate > 297000000)
  1217. bvb_rate = 300000000;
  1218. else if (tmds_char_rate > 148500000)
  1219. bvb_rate = 150000000;
  1220. else
  1221. bvb_rate = 75000000;
  1222. ret = clk_set_min_rate(vc4_hdmi->pixel_bvb_clock, bvb_rate);
  1223. if (ret) {
  1224. DRM_ERROR("Failed to set pixel bvb clock rate: %d\n", ret);
  1225. goto err_disable_pixel_clock;
  1226. }
  1227. ret = clk_prepare_enable(vc4_hdmi->pixel_bvb_clock);
  1228. if (ret) {
  1229. DRM_ERROR("Failed to turn on pixel bvb clock: %d\n", ret);
  1230. goto err_disable_pixel_clock;
  1231. }
  1232. if (vc4_hdmi->variant->phy_init)
  1233. vc4_hdmi->variant->phy_init(vc4_hdmi, vc4_conn_state);
  1234. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  1235. HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
  1236. HDMI_READ(HDMI_SCHEDULER_CONTROL) |
  1237. VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT |
  1238. VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS);
  1239. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  1240. if (vc4_hdmi->variant->set_timings)
  1241. vc4_hdmi->variant->set_timings(vc4_hdmi, conn_state, mode);
  1242. drm_dev_exit(idx);
  1243. mutex_unlock(&vc4_hdmi->mutex);
  1244. return;
  1245. err_disable_pixel_clock:
  1246. clk_disable_unprepare(vc4_hdmi->pixel_clock);
  1247. err_put_runtime_pm:
  1248. pm_runtime_put(&vc4_hdmi->pdev->dev);
  1249. err_dev_exit:
  1250. drm_dev_exit(idx);
  1251. out:
  1252. mutex_unlock(&vc4_hdmi->mutex);
  1253. return;
  1254. }
  1255. static void vc4_hdmi_encoder_pre_crtc_enable(struct drm_encoder *encoder,
  1256. struct drm_atomic_state *state)
  1257. {
  1258. struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
  1259. struct drm_device *drm = vc4_hdmi->connector.dev;
  1260. struct drm_connector *connector = &vc4_hdmi->connector;
  1261. const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
  1262. struct drm_connector_state *conn_state =
  1263. drm_atomic_get_new_connector_state(state, connector);
  1264. unsigned long flags;
  1265. int idx;
  1266. mutex_lock(&vc4_hdmi->mutex);
  1267. if (!drm_dev_enter(drm, &idx))
  1268. goto out;
  1269. if (vc4_hdmi->variant->csc_setup)
  1270. vc4_hdmi->variant->csc_setup(vc4_hdmi, conn_state, mode);
  1271. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  1272. HDMI_WRITE(HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N);
  1273. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  1274. drm_dev_exit(idx);
  1275. out:
  1276. mutex_unlock(&vc4_hdmi->mutex);
  1277. }
  1278. static void vc4_hdmi_encoder_post_crtc_enable(struct drm_encoder *encoder,
  1279. struct drm_atomic_state *state)
  1280. {
  1281. struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
  1282. struct drm_device *drm = vc4_hdmi->connector.dev;
  1283. const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
  1284. struct drm_display_info *display = &vc4_hdmi->connector.display_info;
  1285. bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
  1286. bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
  1287. unsigned long flags;
  1288. int ret;
  1289. int idx;
  1290. mutex_lock(&vc4_hdmi->mutex);
  1291. if (!drm_dev_enter(drm, &idx))
  1292. goto out;
  1293. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  1294. HDMI_WRITE(HDMI_VID_CTL,
  1295. VC4_HD_VID_CTL_ENABLE |
  1296. VC4_HD_VID_CTL_CLRRGB |
  1297. VC4_HD_VID_CTL_UNDERFLOW_ENABLE |
  1298. VC4_HD_VID_CTL_FRAME_COUNTER_RESET |
  1299. (vsync_pos ? 0 : VC4_HD_VID_CTL_VSYNC_LOW) |
  1300. (hsync_pos ? 0 : VC4_HD_VID_CTL_HSYNC_LOW));
  1301. HDMI_WRITE(HDMI_VID_CTL,
  1302. HDMI_READ(HDMI_VID_CTL) & ~VC4_HD_VID_CTL_BLANKPIX);
  1303. if (display->is_hdmi) {
  1304. HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
  1305. HDMI_READ(HDMI_SCHEDULER_CONTROL) |
  1306. VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
  1307. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  1308. ret = wait_for(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
  1309. VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1000);
  1310. WARN_ONCE(ret, "Timeout waiting for "
  1311. "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
  1312. } else {
  1313. HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
  1314. HDMI_READ(HDMI_RAM_PACKET_CONFIG) &
  1315. ~(VC4_HDMI_RAM_PACKET_ENABLE));
  1316. HDMI_WRITE(HDMI_SCHEDULER_CONTROL,
  1317. HDMI_READ(HDMI_SCHEDULER_CONTROL) &
  1318. ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
  1319. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  1320. ret = wait_for(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
  1321. VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1000);
  1322. WARN_ONCE(ret, "Timeout waiting for "
  1323. "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
  1324. }
  1325. if (display->is_hdmi) {
  1326. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  1327. WARN_ON(!(HDMI_READ(HDMI_SCHEDULER_CONTROL) &
  1328. VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE));
  1329. HDMI_WRITE(HDMI_RAM_PACKET_CONFIG,
  1330. VC4_HDMI_RAM_PACKET_ENABLE);
  1331. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  1332. vc4_hdmi->packet_ram_enabled = true;
  1333. vc4_hdmi_set_infoframes(encoder);
  1334. }
  1335. vc4_hdmi_recenter_fifo(vc4_hdmi);
  1336. vc4_hdmi_enable_scrambling(encoder);
  1337. drm_dev_exit(idx);
  1338. out:
  1339. mutex_unlock(&vc4_hdmi->mutex);
  1340. }
  1341. static void vc4_hdmi_encoder_atomic_mode_set(struct drm_encoder *encoder,
  1342. struct drm_crtc_state *crtc_state,
  1343. struct drm_connector_state *conn_state)
  1344. {
  1345. struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
  1346. struct vc4_hdmi_connector_state *vc4_state =
  1347. conn_state_to_vc4_hdmi_conn_state(conn_state);
  1348. mutex_lock(&vc4_hdmi->mutex);
  1349. drm_mode_copy(&vc4_hdmi->saved_adjusted_mode,
  1350. &crtc_state->adjusted_mode);
  1351. vc4_hdmi->output_bpc = vc4_state->output_bpc;
  1352. vc4_hdmi->output_format = vc4_state->output_format;
  1353. mutex_unlock(&vc4_hdmi->mutex);
  1354. }
  1355. static bool
  1356. vc4_hdmi_sink_supports_format_bpc(const struct vc4_hdmi *vc4_hdmi,
  1357. const struct drm_display_info *info,
  1358. const struct drm_display_mode *mode,
  1359. unsigned int format, unsigned int bpc)
  1360. {
  1361. struct drm_device *dev = vc4_hdmi->connector.dev;
  1362. u8 vic = drm_match_cea_mode(mode);
  1363. if (vic == 1 && bpc != 8) {
  1364. drm_dbg(dev, "VIC1 requires a bpc of 8, got %u\n", bpc);
  1365. return false;
  1366. }
  1367. if (!info->is_hdmi &&
  1368. (format != VC4_HDMI_OUTPUT_RGB || bpc != 8)) {
  1369. drm_dbg(dev, "DVI Monitors require an RGB output at 8 bpc\n");
  1370. return false;
  1371. }
  1372. switch (format) {
  1373. case VC4_HDMI_OUTPUT_RGB:
  1374. drm_dbg(dev, "RGB Format, checking the constraints.\n");
  1375. if (!(info->color_formats & DRM_COLOR_FORMAT_RGB444))
  1376. return false;
  1377. if (bpc == 10 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_30)) {
  1378. drm_dbg(dev, "10 BPC but sink doesn't support Deep Color 30.\n");
  1379. return false;
  1380. }
  1381. if (bpc == 12 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_36)) {
  1382. drm_dbg(dev, "12 BPC but sink doesn't support Deep Color 36.\n");
  1383. return false;
  1384. }
  1385. drm_dbg(dev, "RGB format supported in that configuration.\n");
  1386. return true;
  1387. case VC4_HDMI_OUTPUT_YUV422:
  1388. drm_dbg(dev, "YUV422 format, checking the constraints.\n");
  1389. if (!(info->color_formats & DRM_COLOR_FORMAT_YCBCR422)) {
  1390. drm_dbg(dev, "Sink doesn't support YUV422.\n");
  1391. return false;
  1392. }
  1393. if (bpc != 12) {
  1394. drm_dbg(dev, "YUV422 only supports 12 bpc.\n");
  1395. return false;
  1396. }
  1397. drm_dbg(dev, "YUV422 format supported in that configuration.\n");
  1398. return true;
  1399. case VC4_HDMI_OUTPUT_YUV444:
  1400. drm_dbg(dev, "YUV444 format, checking the constraints.\n");
  1401. if (!(info->color_formats & DRM_COLOR_FORMAT_YCBCR444)) {
  1402. drm_dbg(dev, "Sink doesn't support YUV444.\n");
  1403. return false;
  1404. }
  1405. if (bpc == 10 && !(info->edid_hdmi_ycbcr444_dc_modes & DRM_EDID_HDMI_DC_30)) {
  1406. drm_dbg(dev, "10 BPC but sink doesn't support Deep Color 30.\n");
  1407. return false;
  1408. }
  1409. if (bpc == 12 && !(info->edid_hdmi_ycbcr444_dc_modes & DRM_EDID_HDMI_DC_36)) {
  1410. drm_dbg(dev, "12 BPC but sink doesn't support Deep Color 36.\n");
  1411. return false;
  1412. }
  1413. drm_dbg(dev, "YUV444 format supported in that configuration.\n");
  1414. return true;
  1415. }
  1416. return false;
  1417. }
  1418. static enum drm_mode_status
  1419. vc4_hdmi_encoder_clock_valid(const struct vc4_hdmi *vc4_hdmi,
  1420. unsigned long long clock)
  1421. {
  1422. const struct drm_connector *connector = &vc4_hdmi->connector;
  1423. const struct drm_display_info *info = &connector->display_info;
  1424. if (clock > vc4_hdmi->variant->max_pixel_clock)
  1425. return MODE_CLOCK_HIGH;
  1426. if (vc4_hdmi->disable_4kp60 && clock > HDMI_14_MAX_TMDS_CLK)
  1427. return MODE_CLOCK_HIGH;
  1428. if (info->max_tmds_clock && clock > (info->max_tmds_clock * 1000))
  1429. return MODE_CLOCK_HIGH;
  1430. return MODE_OK;
  1431. }
  1432. static unsigned long long
  1433. vc4_hdmi_encoder_compute_mode_clock(const struct drm_display_mode *mode,
  1434. unsigned int bpc,
  1435. enum vc4_hdmi_output_format fmt)
  1436. {
  1437. unsigned long long clock = mode->clock * 1000ULL;
  1438. if (mode->flags & DRM_MODE_FLAG_DBLCLK)
  1439. clock = clock * 2;
  1440. if (fmt == VC4_HDMI_OUTPUT_YUV422)
  1441. bpc = 8;
  1442. clock = clock * bpc;
  1443. do_div(clock, 8);
  1444. return clock;
  1445. }
  1446. static int
  1447. vc4_hdmi_encoder_compute_clock(const struct vc4_hdmi *vc4_hdmi,
  1448. struct vc4_hdmi_connector_state *vc4_state,
  1449. const struct drm_display_mode *mode,
  1450. unsigned int bpc, unsigned int fmt)
  1451. {
  1452. unsigned long long clock;
  1453. clock = vc4_hdmi_encoder_compute_mode_clock(mode, bpc, fmt);
  1454. if (vc4_hdmi_encoder_clock_valid(vc4_hdmi, clock) != MODE_OK)
  1455. return -EINVAL;
  1456. vc4_state->tmds_char_rate = clock;
  1457. return 0;
  1458. }
  1459. static int
  1460. vc4_hdmi_encoder_compute_format(const struct vc4_hdmi *vc4_hdmi,
  1461. struct vc4_hdmi_connector_state *vc4_state,
  1462. const struct drm_display_mode *mode,
  1463. unsigned int bpc)
  1464. {
  1465. struct drm_device *dev = vc4_hdmi->connector.dev;
  1466. const struct drm_connector *connector = &vc4_hdmi->connector;
  1467. const struct drm_display_info *info = &connector->display_info;
  1468. unsigned int format;
  1469. drm_dbg(dev, "Trying with an RGB output\n");
  1470. format = VC4_HDMI_OUTPUT_RGB;
  1471. if (vc4_hdmi_sink_supports_format_bpc(vc4_hdmi, info, mode, format, bpc)) {
  1472. int ret;
  1473. ret = vc4_hdmi_encoder_compute_clock(vc4_hdmi, vc4_state,
  1474. mode, bpc, format);
  1475. if (!ret) {
  1476. vc4_state->output_format = format;
  1477. return 0;
  1478. }
  1479. }
  1480. drm_dbg(dev, "Failed, Trying with an YUV422 output\n");
  1481. format = VC4_HDMI_OUTPUT_YUV422;
  1482. if (vc4_hdmi_sink_supports_format_bpc(vc4_hdmi, info, mode, format, bpc)) {
  1483. int ret;
  1484. ret = vc4_hdmi_encoder_compute_clock(vc4_hdmi, vc4_state,
  1485. mode, bpc, format);
  1486. if (!ret) {
  1487. vc4_state->output_format = format;
  1488. return 0;
  1489. }
  1490. }
  1491. drm_dbg(dev, "Failed. No Format Supported for that bpc count.\n");
  1492. return -EINVAL;
  1493. }
  1494. static int
  1495. vc4_hdmi_encoder_compute_config(const struct vc4_hdmi *vc4_hdmi,
  1496. struct vc4_hdmi_connector_state *vc4_state,
  1497. const struct drm_display_mode *mode)
  1498. {
  1499. struct drm_device *dev = vc4_hdmi->connector.dev;
  1500. struct drm_connector_state *conn_state = &vc4_state->base;
  1501. unsigned int max_bpc = clamp_t(unsigned int, conn_state->max_bpc, 8, 12);
  1502. unsigned int bpc;
  1503. int ret;
  1504. for (bpc = max_bpc; bpc >= 8; bpc -= 2) {
  1505. drm_dbg(dev, "Trying with a %d bpc output\n", bpc);
  1506. ret = vc4_hdmi_encoder_compute_format(vc4_hdmi, vc4_state,
  1507. mode, bpc);
  1508. if (ret)
  1509. continue;
  1510. vc4_state->output_bpc = bpc;
  1511. drm_dbg(dev,
  1512. "Mode %ux%u @ %uHz: Found configuration: bpc: %u, fmt: %s, clock: %llu\n",
  1513. mode->hdisplay, mode->vdisplay, drm_mode_vrefresh(mode),
  1514. vc4_state->output_bpc,
  1515. vc4_hdmi_output_fmt_str(vc4_state->output_format),
  1516. vc4_state->tmds_char_rate);
  1517. break;
  1518. }
  1519. return ret;
  1520. }
  1521. #define WIFI_2_4GHz_CH1_MIN_FREQ 2400000000ULL
  1522. #define WIFI_2_4GHz_CH1_MAX_FREQ 2422000000ULL
  1523. static int vc4_hdmi_encoder_atomic_check(struct drm_encoder *encoder,
  1524. struct drm_crtc_state *crtc_state,
  1525. struct drm_connector_state *conn_state)
  1526. {
  1527. struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
  1528. struct drm_connector *connector = &vc4_hdmi->connector;
  1529. struct drm_connector_state *old_conn_state =
  1530. drm_atomic_get_old_connector_state(conn_state->state, connector);
  1531. struct vc4_hdmi_connector_state *old_vc4_state =
  1532. conn_state_to_vc4_hdmi_conn_state(old_conn_state);
  1533. struct vc4_hdmi_connector_state *vc4_state = conn_state_to_vc4_hdmi_conn_state(conn_state);
  1534. struct drm_display_mode *mode = &crtc_state->adjusted_mode;
  1535. unsigned long long tmds_char_rate = mode->clock * 1000;
  1536. unsigned long long tmds_bit_rate;
  1537. int ret;
  1538. if (vc4_hdmi->variant->unsupported_odd_h_timings) {
  1539. if (mode->flags & DRM_MODE_FLAG_DBLCLK) {
  1540. /* Only try to fixup DBLCLK modes to get 480i and 576i
  1541. * working.
  1542. * A generic solution for all modes with odd horizontal
  1543. * timing values seems impossible based on trying to
  1544. * solve it for 1366x768 monitors.
  1545. */
  1546. if ((mode->hsync_start - mode->hdisplay) & 1)
  1547. mode->hsync_start--;
  1548. if ((mode->hsync_end - mode->hsync_start) & 1)
  1549. mode->hsync_end--;
  1550. }
  1551. /* Now check whether we still have odd values remaining */
  1552. if ((mode->hdisplay % 2) || (mode->hsync_start % 2) ||
  1553. (mode->hsync_end % 2) || (mode->htotal % 2))
  1554. return -EINVAL;
  1555. }
  1556. /*
  1557. * The 1440p@60 pixel rate is in the same range than the first
  1558. * WiFi channel (between 2.4GHz and 2.422GHz with 22MHz
  1559. * bandwidth). Slightly lower the frequency to bring it out of
  1560. * the WiFi range.
  1561. */
  1562. tmds_bit_rate = tmds_char_rate * 10;
  1563. if (vc4_hdmi->disable_wifi_frequencies &&
  1564. (tmds_bit_rate >= WIFI_2_4GHz_CH1_MIN_FREQ &&
  1565. tmds_bit_rate <= WIFI_2_4GHz_CH1_MAX_FREQ)) {
  1566. mode->clock = 238560;
  1567. tmds_char_rate = mode->clock * 1000;
  1568. }
  1569. ret = vc4_hdmi_encoder_compute_config(vc4_hdmi, vc4_state, mode);
  1570. if (ret)
  1571. return ret;
  1572. /* vc4_hdmi_encoder_compute_config may have changed output_bpc and/or output_format */
  1573. if (vc4_state->output_bpc != old_vc4_state->output_bpc ||
  1574. vc4_state->output_format != old_vc4_state->output_format)
  1575. crtc_state->mode_changed = true;
  1576. return 0;
  1577. }
  1578. static enum drm_mode_status
  1579. vc4_hdmi_encoder_mode_valid(struct drm_encoder *encoder,
  1580. const struct drm_display_mode *mode)
  1581. {
  1582. struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
  1583. if (vc4_hdmi->variant->unsupported_odd_h_timings &&
  1584. !(mode->flags & DRM_MODE_FLAG_DBLCLK) &&
  1585. ((mode->hdisplay % 2) || (mode->hsync_start % 2) ||
  1586. (mode->hsync_end % 2) || (mode->htotal % 2)))
  1587. return MODE_H_ILLEGAL;
  1588. return vc4_hdmi_encoder_clock_valid(vc4_hdmi, mode->clock * 1000);
  1589. }
  1590. static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = {
  1591. .atomic_check = vc4_hdmi_encoder_atomic_check,
  1592. .atomic_mode_set = vc4_hdmi_encoder_atomic_mode_set,
  1593. .mode_valid = vc4_hdmi_encoder_mode_valid,
  1594. };
  1595. static int vc4_hdmi_late_register(struct drm_encoder *encoder)
  1596. {
  1597. struct drm_device *drm = encoder->dev;
  1598. struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
  1599. const struct vc4_hdmi_variant *variant = vc4_hdmi->variant;
  1600. int ret;
  1601. ret = vc4_debugfs_add_file(drm->primary, variant->debugfs_name,
  1602. vc4_hdmi_debugfs_regs,
  1603. vc4_hdmi);
  1604. if (ret)
  1605. return ret;
  1606. return 0;
  1607. }
  1608. static const struct drm_encoder_funcs vc4_hdmi_encoder_funcs = {
  1609. .late_register = vc4_hdmi_late_register,
  1610. };
  1611. static u32 vc4_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask)
  1612. {
  1613. int i;
  1614. u32 channel_map = 0;
  1615. for (i = 0; i < 8; i++) {
  1616. if (channel_mask & BIT(i))
  1617. channel_map |= i << (3 * i);
  1618. }
  1619. return channel_map;
  1620. }
  1621. static u32 vc5_hdmi_channel_map(struct vc4_hdmi *vc4_hdmi, u32 channel_mask)
  1622. {
  1623. int i;
  1624. u32 channel_map = 0;
  1625. for (i = 0; i < 8; i++) {
  1626. if (channel_mask & BIT(i))
  1627. channel_map |= i << (4 * i);
  1628. }
  1629. return channel_map;
  1630. }
  1631. static bool vc5_hdmi_hp_detect(struct vc4_hdmi *vc4_hdmi)
  1632. {
  1633. struct drm_device *drm = vc4_hdmi->connector.dev;
  1634. unsigned long flags;
  1635. u32 hotplug;
  1636. int idx;
  1637. if (!drm_dev_enter(drm, &idx))
  1638. return false;
  1639. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  1640. hotplug = HDMI_READ(HDMI_HOTPLUG);
  1641. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  1642. drm_dev_exit(idx);
  1643. return !!(hotplug & VC4_HDMI_HOTPLUG_CONNECTED);
  1644. }
  1645. /* HDMI audio codec callbacks */
  1646. static void vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi *vc4_hdmi,
  1647. unsigned int samplerate)
  1648. {
  1649. struct drm_device *drm = vc4_hdmi->connector.dev;
  1650. u32 hsm_clock;
  1651. unsigned long flags;
  1652. unsigned long n, m;
  1653. int idx;
  1654. if (!drm_dev_enter(drm, &idx))
  1655. return;
  1656. hsm_clock = clk_get_rate(vc4_hdmi->audio_clock);
  1657. rational_best_approximation(hsm_clock, samplerate,
  1658. VC4_HD_MAI_SMP_N_MASK >>
  1659. VC4_HD_MAI_SMP_N_SHIFT,
  1660. (VC4_HD_MAI_SMP_M_MASK >>
  1661. VC4_HD_MAI_SMP_M_SHIFT) + 1,
  1662. &n, &m);
  1663. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  1664. HDMI_WRITE(HDMI_MAI_SMP,
  1665. VC4_SET_FIELD(n, VC4_HD_MAI_SMP_N) |
  1666. VC4_SET_FIELD(m - 1, VC4_HD_MAI_SMP_M));
  1667. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  1668. drm_dev_exit(idx);
  1669. }
  1670. static void vc4_hdmi_set_n_cts(struct vc4_hdmi *vc4_hdmi, unsigned int samplerate)
  1671. {
  1672. const struct drm_display_mode *mode = &vc4_hdmi->saved_adjusted_mode;
  1673. u32 n, cts;
  1674. u64 tmp;
  1675. lockdep_assert_held(&vc4_hdmi->mutex);
  1676. lockdep_assert_held(&vc4_hdmi->hw_lock);
  1677. n = 128 * samplerate / 1000;
  1678. tmp = (u64)(mode->clock * 1000) * n;
  1679. do_div(tmp, 128 * samplerate);
  1680. cts = tmp;
  1681. HDMI_WRITE(HDMI_CRP_CFG,
  1682. VC4_HDMI_CRP_CFG_EXTERNAL_CTS_EN |
  1683. VC4_SET_FIELD(n, VC4_HDMI_CRP_CFG_N));
  1684. /*
  1685. * We could get slightly more accurate clocks in some cases by
  1686. * providing a CTS_1 value. The two CTS values are alternated
  1687. * between based on the period fields
  1688. */
  1689. HDMI_WRITE(HDMI_CTS_0, cts);
  1690. HDMI_WRITE(HDMI_CTS_1, cts);
  1691. }
  1692. static inline struct vc4_hdmi *dai_to_hdmi(struct snd_soc_dai *dai)
  1693. {
  1694. struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai);
  1695. return snd_soc_card_get_drvdata(card);
  1696. }
  1697. static bool vc4_hdmi_audio_can_stream(struct vc4_hdmi *vc4_hdmi)
  1698. {
  1699. struct drm_display_info *display = &vc4_hdmi->connector.display_info;
  1700. lockdep_assert_held(&vc4_hdmi->mutex);
  1701. /*
  1702. * If the encoder is currently in DVI mode, treat the codec DAI
  1703. * as missing.
  1704. */
  1705. if (!display->is_hdmi)
  1706. return false;
  1707. return true;
  1708. }
  1709. static int vc4_hdmi_audio_startup(struct device *dev, void *data)
  1710. {
  1711. struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
  1712. struct drm_device *drm = vc4_hdmi->connector.dev;
  1713. unsigned long flags;
  1714. int ret = 0;
  1715. int idx;
  1716. mutex_lock(&vc4_hdmi->mutex);
  1717. if (!drm_dev_enter(drm, &idx)) {
  1718. ret = -ENODEV;
  1719. goto out;
  1720. }
  1721. if (!vc4_hdmi_audio_can_stream(vc4_hdmi)) {
  1722. ret = -ENODEV;
  1723. goto out_dev_exit;
  1724. }
  1725. vc4_hdmi->audio.streaming = true;
  1726. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  1727. HDMI_WRITE(HDMI_MAI_CTL,
  1728. VC4_HD_MAI_CTL_RESET |
  1729. VC4_HD_MAI_CTL_FLUSH |
  1730. VC4_HD_MAI_CTL_DLATE |
  1731. VC4_HD_MAI_CTL_ERRORE |
  1732. VC4_HD_MAI_CTL_ERRORF);
  1733. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  1734. if (vc4_hdmi->variant->phy_rng_enable)
  1735. vc4_hdmi->variant->phy_rng_enable(vc4_hdmi);
  1736. out_dev_exit:
  1737. drm_dev_exit(idx);
  1738. out:
  1739. mutex_unlock(&vc4_hdmi->mutex);
  1740. return ret;
  1741. }
  1742. static void vc4_hdmi_audio_reset(struct vc4_hdmi *vc4_hdmi)
  1743. {
  1744. struct drm_encoder *encoder = &vc4_hdmi->encoder.base;
  1745. struct device *dev = &vc4_hdmi->pdev->dev;
  1746. unsigned long flags;
  1747. int ret;
  1748. lockdep_assert_held(&vc4_hdmi->mutex);
  1749. vc4_hdmi->audio.streaming = false;
  1750. ret = vc4_hdmi_stop_packet(encoder, HDMI_INFOFRAME_TYPE_AUDIO, false);
  1751. if (ret)
  1752. dev_err(dev, "Failed to stop audio infoframe: %d\n", ret);
  1753. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  1754. HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_RESET);
  1755. HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_ERRORF);
  1756. HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_FLUSH);
  1757. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  1758. }
  1759. static void vc4_hdmi_audio_shutdown(struct device *dev, void *data)
  1760. {
  1761. struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
  1762. struct drm_device *drm = vc4_hdmi->connector.dev;
  1763. unsigned long flags;
  1764. int idx;
  1765. mutex_lock(&vc4_hdmi->mutex);
  1766. if (!drm_dev_enter(drm, &idx))
  1767. goto out;
  1768. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  1769. HDMI_WRITE(HDMI_MAI_CTL,
  1770. VC4_HD_MAI_CTL_DLATE |
  1771. VC4_HD_MAI_CTL_ERRORE |
  1772. VC4_HD_MAI_CTL_ERRORF);
  1773. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  1774. if (vc4_hdmi->variant->phy_rng_disable)
  1775. vc4_hdmi->variant->phy_rng_disable(vc4_hdmi);
  1776. vc4_hdmi->audio.streaming = false;
  1777. vc4_hdmi_audio_reset(vc4_hdmi);
  1778. drm_dev_exit(idx);
  1779. out:
  1780. mutex_unlock(&vc4_hdmi->mutex);
  1781. }
  1782. static int sample_rate_to_mai_fmt(int samplerate)
  1783. {
  1784. switch (samplerate) {
  1785. case 8000:
  1786. return VC4_HDMI_MAI_SAMPLE_RATE_8000;
  1787. case 11025:
  1788. return VC4_HDMI_MAI_SAMPLE_RATE_11025;
  1789. case 12000:
  1790. return VC4_HDMI_MAI_SAMPLE_RATE_12000;
  1791. case 16000:
  1792. return VC4_HDMI_MAI_SAMPLE_RATE_16000;
  1793. case 22050:
  1794. return VC4_HDMI_MAI_SAMPLE_RATE_22050;
  1795. case 24000:
  1796. return VC4_HDMI_MAI_SAMPLE_RATE_24000;
  1797. case 32000:
  1798. return VC4_HDMI_MAI_SAMPLE_RATE_32000;
  1799. case 44100:
  1800. return VC4_HDMI_MAI_SAMPLE_RATE_44100;
  1801. case 48000:
  1802. return VC4_HDMI_MAI_SAMPLE_RATE_48000;
  1803. case 64000:
  1804. return VC4_HDMI_MAI_SAMPLE_RATE_64000;
  1805. case 88200:
  1806. return VC4_HDMI_MAI_SAMPLE_RATE_88200;
  1807. case 96000:
  1808. return VC4_HDMI_MAI_SAMPLE_RATE_96000;
  1809. case 128000:
  1810. return VC4_HDMI_MAI_SAMPLE_RATE_128000;
  1811. case 176400:
  1812. return VC4_HDMI_MAI_SAMPLE_RATE_176400;
  1813. case 192000:
  1814. return VC4_HDMI_MAI_SAMPLE_RATE_192000;
  1815. default:
  1816. return VC4_HDMI_MAI_SAMPLE_RATE_NOT_INDICATED;
  1817. }
  1818. }
  1819. /* HDMI audio codec callbacks */
  1820. static int vc4_hdmi_audio_prepare(struct device *dev, void *data,
  1821. struct hdmi_codec_daifmt *daifmt,
  1822. struct hdmi_codec_params *params)
  1823. {
  1824. struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
  1825. struct drm_device *drm = vc4_hdmi->connector.dev;
  1826. struct drm_encoder *encoder = &vc4_hdmi->encoder.base;
  1827. unsigned int sample_rate = params->sample_rate;
  1828. unsigned int channels = params->channels;
  1829. unsigned long flags;
  1830. u32 audio_packet_config, channel_mask;
  1831. u32 channel_map;
  1832. u32 mai_audio_format;
  1833. u32 mai_sample_rate;
  1834. int ret = 0;
  1835. int idx;
  1836. dev_dbg(dev, "%s: %u Hz, %d bit, %d channels\n", __func__,
  1837. sample_rate, params->sample_width, channels);
  1838. mutex_lock(&vc4_hdmi->mutex);
  1839. if (!drm_dev_enter(drm, &idx)) {
  1840. ret = -ENODEV;
  1841. goto out;
  1842. }
  1843. if (!vc4_hdmi_audio_can_stream(vc4_hdmi)) {
  1844. ret = -EINVAL;
  1845. goto out_dev_exit;
  1846. }
  1847. vc4_hdmi_audio_set_mai_clock(vc4_hdmi, sample_rate);
  1848. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  1849. HDMI_WRITE(HDMI_MAI_CTL,
  1850. VC4_SET_FIELD(channels, VC4_HD_MAI_CTL_CHNUM) |
  1851. VC4_HD_MAI_CTL_WHOLSMP |
  1852. VC4_HD_MAI_CTL_CHALIGN |
  1853. VC4_HD_MAI_CTL_ENABLE);
  1854. mai_sample_rate = sample_rate_to_mai_fmt(sample_rate);
  1855. if (params->iec.status[0] & IEC958_AES0_NONAUDIO &&
  1856. params->channels == 8)
  1857. mai_audio_format = VC4_HDMI_MAI_FORMAT_HBR;
  1858. else
  1859. mai_audio_format = VC4_HDMI_MAI_FORMAT_PCM;
  1860. HDMI_WRITE(HDMI_MAI_FMT,
  1861. VC4_SET_FIELD(mai_sample_rate,
  1862. VC4_HDMI_MAI_FORMAT_SAMPLE_RATE) |
  1863. VC4_SET_FIELD(mai_audio_format,
  1864. VC4_HDMI_MAI_FORMAT_AUDIO_FORMAT));
  1865. /* The B frame identifier should match the value used by alsa-lib (8) */
  1866. audio_packet_config =
  1867. VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_SAMPLE_FLAT |
  1868. VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_INACTIVE_CHANNELS |
  1869. VC4_SET_FIELD(0x8, VC4_HDMI_AUDIO_PACKET_B_FRAME_IDENTIFIER);
  1870. channel_mask = GENMASK(channels - 1, 0);
  1871. audio_packet_config |= VC4_SET_FIELD(channel_mask,
  1872. VC4_HDMI_AUDIO_PACKET_CEA_MASK);
  1873. /* Set the MAI threshold */
  1874. HDMI_WRITE(HDMI_MAI_THR,
  1875. VC4_SET_FIELD(0x08, VC4_HD_MAI_THR_PANICHIGH) |
  1876. VC4_SET_FIELD(0x08, VC4_HD_MAI_THR_PANICLOW) |
  1877. VC4_SET_FIELD(0x06, VC4_HD_MAI_THR_DREQHIGH) |
  1878. VC4_SET_FIELD(0x08, VC4_HD_MAI_THR_DREQLOW));
  1879. HDMI_WRITE(HDMI_MAI_CONFIG,
  1880. VC4_HDMI_MAI_CONFIG_BIT_REVERSE |
  1881. VC4_HDMI_MAI_CONFIG_FORMAT_REVERSE |
  1882. VC4_SET_FIELD(channel_mask, VC4_HDMI_MAI_CHANNEL_MASK));
  1883. channel_map = vc4_hdmi->variant->channel_map(vc4_hdmi, channel_mask);
  1884. HDMI_WRITE(HDMI_MAI_CHANNEL_MAP, channel_map);
  1885. HDMI_WRITE(HDMI_AUDIO_PACKET_CONFIG, audio_packet_config);
  1886. vc4_hdmi_set_n_cts(vc4_hdmi, sample_rate);
  1887. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  1888. memcpy(&vc4_hdmi->audio.infoframe, &params->cea, sizeof(params->cea));
  1889. vc4_hdmi_set_audio_infoframe(encoder);
  1890. out_dev_exit:
  1891. drm_dev_exit(idx);
  1892. out:
  1893. mutex_unlock(&vc4_hdmi->mutex);
  1894. return ret;
  1895. }
  1896. static const struct snd_soc_component_driver vc4_hdmi_audio_cpu_dai_comp = {
  1897. .name = "vc4-hdmi-cpu-dai-component",
  1898. .legacy_dai_naming = 1,
  1899. };
  1900. static int vc4_hdmi_audio_cpu_dai_probe(struct snd_soc_dai *dai)
  1901. {
  1902. struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
  1903. snd_soc_dai_init_dma_data(dai, &vc4_hdmi->audio.dma_data, NULL);
  1904. return 0;
  1905. }
  1906. static struct snd_soc_dai_driver vc4_hdmi_audio_cpu_dai_drv = {
  1907. .name = "vc4-hdmi-cpu-dai",
  1908. .probe = vc4_hdmi_audio_cpu_dai_probe,
  1909. .playback = {
  1910. .stream_name = "Playback",
  1911. .channels_min = 1,
  1912. .channels_max = 8,
  1913. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
  1914. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
  1915. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
  1916. SNDRV_PCM_RATE_192000,
  1917. .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
  1918. },
  1919. };
  1920. static const struct snd_dmaengine_pcm_config pcm_conf = {
  1921. .chan_names[SNDRV_PCM_STREAM_PLAYBACK] = "audio-rx",
  1922. .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
  1923. };
  1924. static int vc4_hdmi_audio_get_eld(struct device *dev, void *data,
  1925. uint8_t *buf, size_t len)
  1926. {
  1927. struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
  1928. struct drm_connector *connector = &vc4_hdmi->connector;
  1929. mutex_lock(&vc4_hdmi->mutex);
  1930. memcpy(buf, connector->eld, min(sizeof(connector->eld), len));
  1931. mutex_unlock(&vc4_hdmi->mutex);
  1932. return 0;
  1933. }
  1934. static const struct hdmi_codec_ops vc4_hdmi_codec_ops = {
  1935. .get_eld = vc4_hdmi_audio_get_eld,
  1936. .prepare = vc4_hdmi_audio_prepare,
  1937. .audio_shutdown = vc4_hdmi_audio_shutdown,
  1938. .audio_startup = vc4_hdmi_audio_startup,
  1939. };
  1940. static struct hdmi_codec_pdata vc4_hdmi_codec_pdata = {
  1941. .ops = &vc4_hdmi_codec_ops,
  1942. .max_i2s_channels = 8,
  1943. .i2s = 1,
  1944. };
  1945. static void vc4_hdmi_audio_codec_release(void *ptr)
  1946. {
  1947. struct vc4_hdmi *vc4_hdmi = ptr;
  1948. platform_device_unregister(vc4_hdmi->audio.codec_pdev);
  1949. vc4_hdmi->audio.codec_pdev = NULL;
  1950. }
  1951. static int vc4_hdmi_audio_init(struct vc4_hdmi *vc4_hdmi)
  1952. {
  1953. const struct vc4_hdmi_register *mai_data =
  1954. &vc4_hdmi->variant->registers[HDMI_MAI_DATA];
  1955. struct snd_soc_dai_link *dai_link = &vc4_hdmi->audio.link;
  1956. struct snd_soc_card *card = &vc4_hdmi->audio.card;
  1957. struct device *dev = &vc4_hdmi->pdev->dev;
  1958. struct platform_device *codec_pdev;
  1959. const __be32 *addr;
  1960. int index, len;
  1961. int ret;
  1962. /*
  1963. * ASoC makes it a bit hard to retrieve a pointer to the
  1964. * vc4_hdmi structure. Registering the card will overwrite our
  1965. * device drvdata with a pointer to the snd_soc_card structure,
  1966. * which can then be used to retrieve whatever drvdata we want
  1967. * to associate.
  1968. *
  1969. * However, that doesn't fly in the case where we wouldn't
  1970. * register an ASoC card (because of an old DT that is missing
  1971. * the dmas properties for example), then the card isn't
  1972. * registered and the device drvdata wouldn't be set.
  1973. *
  1974. * We can deal with both cases by making sure a snd_soc_card
  1975. * pointer and a vc4_hdmi structure are pointing to the same
  1976. * memory address, so we can treat them indistinctly without any
  1977. * issue.
  1978. */
  1979. BUILD_BUG_ON(offsetof(struct vc4_hdmi_audio, card) != 0);
  1980. BUILD_BUG_ON(offsetof(struct vc4_hdmi, audio) != 0);
  1981. if (!of_find_property(dev->of_node, "dmas", &len) || !len) {
  1982. dev_warn(dev,
  1983. "'dmas' DT property is missing or empty, no HDMI audio\n");
  1984. return 0;
  1985. }
  1986. if (mai_data->reg != VC4_HD) {
  1987. WARN_ONCE(true, "MAI isn't in the HD block\n");
  1988. return -EINVAL;
  1989. }
  1990. /*
  1991. * Get the physical address of VC4_HD_MAI_DATA. We need to retrieve
  1992. * the bus address specified in the DT, because the physical address
  1993. * (the one returned by platform_get_resource()) is not appropriate
  1994. * for DMA transfers.
  1995. * This VC/MMU should probably be exposed to avoid this kind of hacks.
  1996. */
  1997. index = of_property_match_string(dev->of_node, "reg-names", "hd");
  1998. /* Before BCM2711, we don't have a named register range */
  1999. if (index < 0)
  2000. index = 1;
  2001. addr = of_get_address(dev->of_node, index, NULL, NULL);
  2002. vc4_hdmi->audio.dma_data.addr = be32_to_cpup(addr) + mai_data->offset;
  2003. vc4_hdmi->audio.dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  2004. vc4_hdmi->audio.dma_data.maxburst = 2;
  2005. /*
  2006. * NOTE: Strictly speaking, we should probably use a DRM-managed
  2007. * registration there to avoid removing all the audio components
  2008. * by the time the driver doesn't have any user anymore.
  2009. *
  2010. * However, the ASoC core uses a number of devm_kzalloc calls
  2011. * when registering, even when using non-device-managed
  2012. * functions (such as in snd_soc_register_component()).
  2013. *
  2014. * If we call snd_soc_unregister_component() in a DRM-managed
  2015. * action, the device-managed actions have already been executed
  2016. * and thus we would access memory that has been freed.
  2017. *
  2018. * Using device-managed hooks here probably leaves us open to a
  2019. * bunch of issues if userspace still has a handle on the ALSA
  2020. * device when the device is removed. However, this is mitigated
  2021. * by the use of drm_dev_enter()/drm_dev_exit() in the audio
  2022. * path to prevent the access to the device resources if it
  2023. * isn't there anymore.
  2024. *
  2025. * Then, the vc4_hdmi structure is DRM-managed and thus only
  2026. * freed whenever the last user has closed the DRM device file.
  2027. * It should thus outlive ALSA in most situations.
  2028. */
  2029. ret = devm_snd_dmaengine_pcm_register(dev, &pcm_conf, 0);
  2030. if (ret) {
  2031. dev_err(dev, "Could not register PCM component: %d\n", ret);
  2032. return ret;
  2033. }
  2034. ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_cpu_dai_comp,
  2035. &vc4_hdmi_audio_cpu_dai_drv, 1);
  2036. if (ret) {
  2037. dev_err(dev, "Could not register CPU DAI: %d\n", ret);
  2038. return ret;
  2039. }
  2040. codec_pdev = platform_device_register_data(dev, HDMI_CODEC_DRV_NAME,
  2041. PLATFORM_DEVID_AUTO,
  2042. &vc4_hdmi_codec_pdata,
  2043. sizeof(vc4_hdmi_codec_pdata));
  2044. if (IS_ERR(codec_pdev)) {
  2045. dev_err(dev, "Couldn't register the HDMI codec: %ld\n", PTR_ERR(codec_pdev));
  2046. return PTR_ERR(codec_pdev);
  2047. }
  2048. vc4_hdmi->audio.codec_pdev = codec_pdev;
  2049. ret = devm_add_action_or_reset(dev, vc4_hdmi_audio_codec_release, vc4_hdmi);
  2050. if (ret)
  2051. return ret;
  2052. dai_link->cpus = &vc4_hdmi->audio.cpu;
  2053. dai_link->codecs = &vc4_hdmi->audio.codec;
  2054. dai_link->platforms = &vc4_hdmi->audio.platform;
  2055. dai_link->num_cpus = 1;
  2056. dai_link->num_codecs = 1;
  2057. dai_link->num_platforms = 1;
  2058. dai_link->name = "MAI";
  2059. dai_link->stream_name = "MAI PCM";
  2060. dai_link->codecs->dai_name = "i2s-hifi";
  2061. dai_link->cpus->dai_name = dev_name(dev);
  2062. dai_link->codecs->name = dev_name(&codec_pdev->dev);
  2063. dai_link->platforms->name = dev_name(dev);
  2064. card->dai_link = dai_link;
  2065. card->num_links = 1;
  2066. card->name = vc4_hdmi->variant->card_name;
  2067. card->driver_name = "vc4-hdmi";
  2068. card->dev = dev;
  2069. card->owner = THIS_MODULE;
  2070. /*
  2071. * Be careful, snd_soc_register_card() calls dev_set_drvdata() and
  2072. * stores a pointer to the snd card object in dev->driver_data. This
  2073. * means we cannot use it for something else. The hdmi back-pointer is
  2074. * now stored in card->drvdata and should be retrieved with
  2075. * snd_soc_card_get_drvdata() if needed.
  2076. */
  2077. snd_soc_card_set_drvdata(card, vc4_hdmi);
  2078. ret = devm_snd_soc_register_card(dev, card);
  2079. if (ret)
  2080. dev_err_probe(dev, ret, "Could not register sound card\n");
  2081. return ret;
  2082. }
  2083. static irqreturn_t vc4_hdmi_hpd_irq_thread(int irq, void *priv)
  2084. {
  2085. struct vc4_hdmi *vc4_hdmi = priv;
  2086. struct drm_connector *connector = &vc4_hdmi->connector;
  2087. struct drm_device *dev = connector->dev;
  2088. if (dev && dev->registered)
  2089. drm_connector_helper_hpd_irq_event(connector);
  2090. return IRQ_HANDLED;
  2091. }
  2092. static int vc4_hdmi_hotplug_init(struct vc4_hdmi *vc4_hdmi)
  2093. {
  2094. struct drm_connector *connector = &vc4_hdmi->connector;
  2095. struct platform_device *pdev = vc4_hdmi->pdev;
  2096. int ret;
  2097. if (vc4_hdmi->variant->external_irq_controller) {
  2098. unsigned int hpd_con = platform_get_irq_byname(pdev, "hpd-connected");
  2099. unsigned int hpd_rm = platform_get_irq_byname(pdev, "hpd-removed");
  2100. ret = devm_request_threaded_irq(&pdev->dev, hpd_con,
  2101. NULL,
  2102. vc4_hdmi_hpd_irq_thread, IRQF_ONESHOT,
  2103. "vc4 hdmi hpd connected", vc4_hdmi);
  2104. if (ret)
  2105. return ret;
  2106. ret = devm_request_threaded_irq(&pdev->dev, hpd_rm,
  2107. NULL,
  2108. vc4_hdmi_hpd_irq_thread, IRQF_ONESHOT,
  2109. "vc4 hdmi hpd disconnected", vc4_hdmi);
  2110. if (ret)
  2111. return ret;
  2112. connector->polled = DRM_CONNECTOR_POLL_HPD;
  2113. }
  2114. return 0;
  2115. }
  2116. #ifdef CONFIG_DRM_VC4_HDMI_CEC
  2117. static irqreturn_t vc4_cec_irq_handler_rx_thread(int irq, void *priv)
  2118. {
  2119. struct vc4_hdmi *vc4_hdmi = priv;
  2120. if (vc4_hdmi->cec_rx_msg.len)
  2121. cec_received_msg(vc4_hdmi->cec_adap,
  2122. &vc4_hdmi->cec_rx_msg);
  2123. return IRQ_HANDLED;
  2124. }
  2125. static irqreturn_t vc4_cec_irq_handler_tx_thread(int irq, void *priv)
  2126. {
  2127. struct vc4_hdmi *vc4_hdmi = priv;
  2128. if (vc4_hdmi->cec_tx_ok) {
  2129. cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_OK,
  2130. 0, 0, 0, 0);
  2131. } else {
  2132. /*
  2133. * This CEC implementation makes 1 retry, so if we
  2134. * get a NACK, then that means it made 2 attempts.
  2135. */
  2136. cec_transmit_done(vc4_hdmi->cec_adap, CEC_TX_STATUS_NACK,
  2137. 0, 2, 0, 0);
  2138. }
  2139. return IRQ_HANDLED;
  2140. }
  2141. static irqreturn_t vc4_cec_irq_handler_thread(int irq, void *priv)
  2142. {
  2143. struct vc4_hdmi *vc4_hdmi = priv;
  2144. irqreturn_t ret;
  2145. if (vc4_hdmi->cec_irq_was_rx)
  2146. ret = vc4_cec_irq_handler_rx_thread(irq, priv);
  2147. else
  2148. ret = vc4_cec_irq_handler_tx_thread(irq, priv);
  2149. return ret;
  2150. }
  2151. static void vc4_cec_read_msg(struct vc4_hdmi *vc4_hdmi, u32 cntrl1)
  2152. {
  2153. struct drm_device *dev = vc4_hdmi->connector.dev;
  2154. struct cec_msg *msg = &vc4_hdmi->cec_rx_msg;
  2155. unsigned int i;
  2156. lockdep_assert_held(&vc4_hdmi->hw_lock);
  2157. msg->len = 1 + ((cntrl1 & VC4_HDMI_CEC_REC_WRD_CNT_MASK) >>
  2158. VC4_HDMI_CEC_REC_WRD_CNT_SHIFT);
  2159. if (msg->len > 16) {
  2160. drm_err(dev, "Attempting to read too much data (%d)\n", msg->len);
  2161. return;
  2162. }
  2163. for (i = 0; i < msg->len; i += 4) {
  2164. u32 val = HDMI_READ(HDMI_CEC_RX_DATA_1 + (i >> 2));
  2165. msg->msg[i] = val & 0xff;
  2166. msg->msg[i + 1] = (val >> 8) & 0xff;
  2167. msg->msg[i + 2] = (val >> 16) & 0xff;
  2168. msg->msg[i + 3] = (val >> 24) & 0xff;
  2169. }
  2170. }
  2171. static irqreturn_t vc4_cec_irq_handler_tx_bare_locked(struct vc4_hdmi *vc4_hdmi)
  2172. {
  2173. u32 cntrl1;
  2174. /*
  2175. * We don't need to protect the register access using
  2176. * drm_dev_enter() there because the interrupt handler lifetime
  2177. * is tied to the device itself, and not to the DRM device.
  2178. *
  2179. * So when the device will be gone, one of the first thing we
  2180. * will be doing will be to unregister the interrupt handler,
  2181. * and then unregister the DRM device. drm_dev_enter() would
  2182. * thus always succeed if we are here.
  2183. */
  2184. lockdep_assert_held(&vc4_hdmi->hw_lock);
  2185. cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1);
  2186. vc4_hdmi->cec_tx_ok = cntrl1 & VC4_HDMI_CEC_TX_STATUS_GOOD;
  2187. cntrl1 &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
  2188. HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
  2189. return IRQ_WAKE_THREAD;
  2190. }
  2191. static irqreturn_t vc4_cec_irq_handler_tx_bare(int irq, void *priv)
  2192. {
  2193. struct vc4_hdmi *vc4_hdmi = priv;
  2194. irqreturn_t ret;
  2195. spin_lock(&vc4_hdmi->hw_lock);
  2196. ret = vc4_cec_irq_handler_tx_bare_locked(vc4_hdmi);
  2197. spin_unlock(&vc4_hdmi->hw_lock);
  2198. return ret;
  2199. }
  2200. static irqreturn_t vc4_cec_irq_handler_rx_bare_locked(struct vc4_hdmi *vc4_hdmi)
  2201. {
  2202. u32 cntrl1;
  2203. lockdep_assert_held(&vc4_hdmi->hw_lock);
  2204. /*
  2205. * We don't need to protect the register access using
  2206. * drm_dev_enter() there because the interrupt handler lifetime
  2207. * is tied to the device itself, and not to the DRM device.
  2208. *
  2209. * So when the device will be gone, one of the first thing we
  2210. * will be doing will be to unregister the interrupt handler,
  2211. * and then unregister the DRM device. drm_dev_enter() would
  2212. * thus always succeed if we are here.
  2213. */
  2214. vc4_hdmi->cec_rx_msg.len = 0;
  2215. cntrl1 = HDMI_READ(HDMI_CEC_CNTRL_1);
  2216. vc4_cec_read_msg(vc4_hdmi, cntrl1);
  2217. cntrl1 |= VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
  2218. HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
  2219. cntrl1 &= ~VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
  2220. HDMI_WRITE(HDMI_CEC_CNTRL_1, cntrl1);
  2221. return IRQ_WAKE_THREAD;
  2222. }
  2223. static irqreturn_t vc4_cec_irq_handler_rx_bare(int irq, void *priv)
  2224. {
  2225. struct vc4_hdmi *vc4_hdmi = priv;
  2226. irqreturn_t ret;
  2227. spin_lock(&vc4_hdmi->hw_lock);
  2228. ret = vc4_cec_irq_handler_rx_bare_locked(vc4_hdmi);
  2229. spin_unlock(&vc4_hdmi->hw_lock);
  2230. return ret;
  2231. }
  2232. static irqreturn_t vc4_cec_irq_handler(int irq, void *priv)
  2233. {
  2234. struct vc4_hdmi *vc4_hdmi = priv;
  2235. u32 stat = HDMI_READ(HDMI_CEC_CPU_STATUS);
  2236. irqreturn_t ret;
  2237. u32 cntrl5;
  2238. /*
  2239. * We don't need to protect the register access using
  2240. * drm_dev_enter() there because the interrupt handler lifetime
  2241. * is tied to the device itself, and not to the DRM device.
  2242. *
  2243. * So when the device will be gone, one of the first thing we
  2244. * will be doing will be to unregister the interrupt handler,
  2245. * and then unregister the DRM device. drm_dev_enter() would
  2246. * thus always succeed if we are here.
  2247. */
  2248. if (!(stat & VC4_HDMI_CPU_CEC))
  2249. return IRQ_NONE;
  2250. spin_lock(&vc4_hdmi->hw_lock);
  2251. cntrl5 = HDMI_READ(HDMI_CEC_CNTRL_5);
  2252. vc4_hdmi->cec_irq_was_rx = cntrl5 & VC4_HDMI_CEC_RX_CEC_INT;
  2253. if (vc4_hdmi->cec_irq_was_rx)
  2254. ret = vc4_cec_irq_handler_rx_bare_locked(vc4_hdmi);
  2255. else
  2256. ret = vc4_cec_irq_handler_tx_bare_locked(vc4_hdmi);
  2257. HDMI_WRITE(HDMI_CEC_CPU_CLEAR, VC4_HDMI_CPU_CEC);
  2258. spin_unlock(&vc4_hdmi->hw_lock);
  2259. return ret;
  2260. }
  2261. static int vc4_hdmi_cec_enable(struct cec_adapter *adap)
  2262. {
  2263. struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
  2264. struct drm_device *drm = vc4_hdmi->connector.dev;
  2265. /* clock period in microseconds */
  2266. const u32 usecs = 1000000 / CEC_CLOCK_FREQ;
  2267. unsigned long flags;
  2268. u32 val;
  2269. int ret;
  2270. int idx;
  2271. if (!drm_dev_enter(drm, &idx))
  2272. /*
  2273. * We can't return an error code, because the CEC
  2274. * framework will emit WARN_ON messages at unbind
  2275. * otherwise.
  2276. */
  2277. return 0;
  2278. ret = pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev);
  2279. if (ret) {
  2280. drm_dev_exit(idx);
  2281. return ret;
  2282. }
  2283. mutex_lock(&vc4_hdmi->mutex);
  2284. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  2285. val = HDMI_READ(HDMI_CEC_CNTRL_5);
  2286. val &= ~(VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET |
  2287. VC4_HDMI_CEC_CNT_TO_4700_US_MASK |
  2288. VC4_HDMI_CEC_CNT_TO_4500_US_MASK);
  2289. val |= ((4700 / usecs) << VC4_HDMI_CEC_CNT_TO_4700_US_SHIFT) |
  2290. ((4500 / usecs) << VC4_HDMI_CEC_CNT_TO_4500_US_SHIFT);
  2291. HDMI_WRITE(HDMI_CEC_CNTRL_5, val |
  2292. VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
  2293. HDMI_WRITE(HDMI_CEC_CNTRL_5, val);
  2294. HDMI_WRITE(HDMI_CEC_CNTRL_2,
  2295. ((1500 / usecs) << VC4_HDMI_CEC_CNT_TO_1500_US_SHIFT) |
  2296. ((1300 / usecs) << VC4_HDMI_CEC_CNT_TO_1300_US_SHIFT) |
  2297. ((800 / usecs) << VC4_HDMI_CEC_CNT_TO_800_US_SHIFT) |
  2298. ((600 / usecs) << VC4_HDMI_CEC_CNT_TO_600_US_SHIFT) |
  2299. ((400 / usecs) << VC4_HDMI_CEC_CNT_TO_400_US_SHIFT));
  2300. HDMI_WRITE(HDMI_CEC_CNTRL_3,
  2301. ((2750 / usecs) << VC4_HDMI_CEC_CNT_TO_2750_US_SHIFT) |
  2302. ((2400 / usecs) << VC4_HDMI_CEC_CNT_TO_2400_US_SHIFT) |
  2303. ((2050 / usecs) << VC4_HDMI_CEC_CNT_TO_2050_US_SHIFT) |
  2304. ((1700 / usecs) << VC4_HDMI_CEC_CNT_TO_1700_US_SHIFT));
  2305. HDMI_WRITE(HDMI_CEC_CNTRL_4,
  2306. ((4300 / usecs) << VC4_HDMI_CEC_CNT_TO_4300_US_SHIFT) |
  2307. ((3900 / usecs) << VC4_HDMI_CEC_CNT_TO_3900_US_SHIFT) |
  2308. ((3600 / usecs) << VC4_HDMI_CEC_CNT_TO_3600_US_SHIFT) |
  2309. ((3500 / usecs) << VC4_HDMI_CEC_CNT_TO_3500_US_SHIFT));
  2310. if (!vc4_hdmi->variant->external_irq_controller)
  2311. HDMI_WRITE(HDMI_CEC_CPU_MASK_CLEAR, VC4_HDMI_CPU_CEC);
  2312. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  2313. mutex_unlock(&vc4_hdmi->mutex);
  2314. drm_dev_exit(idx);
  2315. return 0;
  2316. }
  2317. static int vc4_hdmi_cec_disable(struct cec_adapter *adap)
  2318. {
  2319. struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
  2320. struct drm_device *drm = vc4_hdmi->connector.dev;
  2321. unsigned long flags;
  2322. int idx;
  2323. if (!drm_dev_enter(drm, &idx))
  2324. /*
  2325. * We can't return an error code, because the CEC
  2326. * framework will emit WARN_ON messages at unbind
  2327. * otherwise.
  2328. */
  2329. return 0;
  2330. mutex_lock(&vc4_hdmi->mutex);
  2331. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  2332. if (!vc4_hdmi->variant->external_irq_controller)
  2333. HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, VC4_HDMI_CPU_CEC);
  2334. HDMI_WRITE(HDMI_CEC_CNTRL_5, HDMI_READ(HDMI_CEC_CNTRL_5) |
  2335. VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
  2336. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  2337. mutex_unlock(&vc4_hdmi->mutex);
  2338. pm_runtime_put(&vc4_hdmi->pdev->dev);
  2339. drm_dev_exit(idx);
  2340. return 0;
  2341. }
  2342. static int vc4_hdmi_cec_adap_enable(struct cec_adapter *adap, bool enable)
  2343. {
  2344. if (enable)
  2345. return vc4_hdmi_cec_enable(adap);
  2346. else
  2347. return vc4_hdmi_cec_disable(adap);
  2348. }
  2349. static int vc4_hdmi_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
  2350. {
  2351. struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
  2352. struct drm_device *drm = vc4_hdmi->connector.dev;
  2353. unsigned long flags;
  2354. int idx;
  2355. if (!drm_dev_enter(drm, &idx))
  2356. /*
  2357. * We can't return an error code, because the CEC
  2358. * framework will emit WARN_ON messages at unbind
  2359. * otherwise.
  2360. */
  2361. return 0;
  2362. mutex_lock(&vc4_hdmi->mutex);
  2363. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  2364. HDMI_WRITE(HDMI_CEC_CNTRL_1,
  2365. (HDMI_READ(HDMI_CEC_CNTRL_1) & ~VC4_HDMI_CEC_ADDR_MASK) |
  2366. (log_addr & 0xf) << VC4_HDMI_CEC_ADDR_SHIFT);
  2367. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  2368. mutex_unlock(&vc4_hdmi->mutex);
  2369. drm_dev_exit(idx);
  2370. return 0;
  2371. }
  2372. static int vc4_hdmi_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
  2373. u32 signal_free_time, struct cec_msg *msg)
  2374. {
  2375. struct vc4_hdmi *vc4_hdmi = cec_get_drvdata(adap);
  2376. struct drm_device *dev = vc4_hdmi->connector.dev;
  2377. unsigned long flags;
  2378. u32 val;
  2379. unsigned int i;
  2380. int idx;
  2381. if (!drm_dev_enter(dev, &idx))
  2382. return -ENODEV;
  2383. if (msg->len > 16) {
  2384. drm_err(dev, "Attempting to transmit too much data (%d)\n", msg->len);
  2385. drm_dev_exit(idx);
  2386. return -ENOMEM;
  2387. }
  2388. mutex_lock(&vc4_hdmi->mutex);
  2389. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  2390. for (i = 0; i < msg->len; i += 4)
  2391. HDMI_WRITE(HDMI_CEC_TX_DATA_1 + (i >> 2),
  2392. (msg->msg[i]) |
  2393. (msg->msg[i + 1] << 8) |
  2394. (msg->msg[i + 2] << 16) |
  2395. (msg->msg[i + 3] << 24));
  2396. val = HDMI_READ(HDMI_CEC_CNTRL_1);
  2397. val &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
  2398. HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
  2399. val &= ~VC4_HDMI_CEC_MESSAGE_LENGTH_MASK;
  2400. val |= (msg->len - 1) << VC4_HDMI_CEC_MESSAGE_LENGTH_SHIFT;
  2401. val |= VC4_HDMI_CEC_START_XMIT_BEGIN;
  2402. HDMI_WRITE(HDMI_CEC_CNTRL_1, val);
  2403. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  2404. mutex_unlock(&vc4_hdmi->mutex);
  2405. drm_dev_exit(idx);
  2406. return 0;
  2407. }
  2408. static const struct cec_adap_ops vc4_hdmi_cec_adap_ops = {
  2409. .adap_enable = vc4_hdmi_cec_adap_enable,
  2410. .adap_log_addr = vc4_hdmi_cec_adap_log_addr,
  2411. .adap_transmit = vc4_hdmi_cec_adap_transmit,
  2412. };
  2413. static void vc4_hdmi_cec_release(void *ptr)
  2414. {
  2415. struct vc4_hdmi *vc4_hdmi = ptr;
  2416. cec_unregister_adapter(vc4_hdmi->cec_adap);
  2417. vc4_hdmi->cec_adap = NULL;
  2418. }
  2419. static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
  2420. {
  2421. struct cec_connector_info conn_info;
  2422. struct platform_device *pdev = vc4_hdmi->pdev;
  2423. struct device *dev = &pdev->dev;
  2424. int ret;
  2425. if (!of_find_property(dev->of_node, "interrupts", NULL)) {
  2426. dev_warn(dev, "'interrupts' DT property is missing, no CEC\n");
  2427. return 0;
  2428. }
  2429. vc4_hdmi->cec_adap = cec_allocate_adapter(&vc4_hdmi_cec_adap_ops,
  2430. vc4_hdmi,
  2431. vc4_hdmi->variant->card_name,
  2432. CEC_CAP_DEFAULTS |
  2433. CEC_CAP_CONNECTOR_INFO, 1);
  2434. ret = PTR_ERR_OR_ZERO(vc4_hdmi->cec_adap);
  2435. if (ret < 0)
  2436. return ret;
  2437. cec_fill_conn_info_from_drm(&conn_info, &vc4_hdmi->connector);
  2438. cec_s_conn_info(vc4_hdmi->cec_adap, &conn_info);
  2439. if (vc4_hdmi->variant->external_irq_controller) {
  2440. ret = devm_request_threaded_irq(dev, platform_get_irq_byname(pdev, "cec-rx"),
  2441. vc4_cec_irq_handler_rx_bare,
  2442. vc4_cec_irq_handler_rx_thread, 0,
  2443. "vc4 hdmi cec rx", vc4_hdmi);
  2444. if (ret)
  2445. goto err_delete_cec_adap;
  2446. ret = devm_request_threaded_irq(dev, platform_get_irq_byname(pdev, "cec-tx"),
  2447. vc4_cec_irq_handler_tx_bare,
  2448. vc4_cec_irq_handler_tx_thread, 0,
  2449. "vc4 hdmi cec tx", vc4_hdmi);
  2450. if (ret)
  2451. goto err_delete_cec_adap;
  2452. } else {
  2453. ret = devm_request_threaded_irq(dev, platform_get_irq(pdev, 0),
  2454. vc4_cec_irq_handler,
  2455. vc4_cec_irq_handler_thread, 0,
  2456. "vc4 hdmi cec", vc4_hdmi);
  2457. if (ret)
  2458. goto err_delete_cec_adap;
  2459. }
  2460. ret = cec_register_adapter(vc4_hdmi->cec_adap, &pdev->dev);
  2461. if (ret < 0)
  2462. goto err_delete_cec_adap;
  2463. /*
  2464. * NOTE: Strictly speaking, we should probably use a DRM-managed
  2465. * registration there to avoid removing the CEC adapter by the
  2466. * time the DRM driver doesn't have any user anymore.
  2467. *
  2468. * However, the CEC framework already cleans up the CEC adapter
  2469. * only when the last user has closed its file descriptor, so we
  2470. * don't need to handle it in DRM.
  2471. *
  2472. * By the time the device-managed hook is executed, we will give
  2473. * up our reference to the CEC adapter and therefore don't
  2474. * really care when it's actually freed.
  2475. *
  2476. * There's still a problematic sequence: if we unregister our
  2477. * CEC adapter, but the userspace keeps a handle on the CEC
  2478. * adapter but not the DRM device for some reason. In such a
  2479. * case, our vc4_hdmi structure will be freed, but the
  2480. * cec_adapter structure will have a dangling pointer to what
  2481. * used to be our HDMI controller. If we get a CEC call at that
  2482. * moment, we could end up with a use-after-free. Fortunately,
  2483. * the CEC framework already handles this too, by calling
  2484. * cec_is_registered() in cec_ioctl() and cec_poll().
  2485. */
  2486. ret = devm_add_action_or_reset(dev, vc4_hdmi_cec_release, vc4_hdmi);
  2487. if (ret)
  2488. return ret;
  2489. return 0;
  2490. err_delete_cec_adap:
  2491. cec_delete_adapter(vc4_hdmi->cec_adap);
  2492. return ret;
  2493. }
  2494. #else
  2495. static int vc4_hdmi_cec_init(struct vc4_hdmi *vc4_hdmi)
  2496. {
  2497. return 0;
  2498. }
  2499. #endif
  2500. static void vc4_hdmi_free_regset(struct drm_device *drm, void *ptr)
  2501. {
  2502. struct debugfs_reg32 *regs = ptr;
  2503. kfree(regs);
  2504. }
  2505. static int vc4_hdmi_build_regset(struct drm_device *drm,
  2506. struct vc4_hdmi *vc4_hdmi,
  2507. struct debugfs_regset32 *regset,
  2508. enum vc4_hdmi_regs reg)
  2509. {
  2510. const struct vc4_hdmi_variant *variant = vc4_hdmi->variant;
  2511. struct debugfs_reg32 *regs, *new_regs;
  2512. unsigned int count = 0;
  2513. unsigned int i;
  2514. int ret;
  2515. regs = kcalloc(variant->num_registers, sizeof(*regs),
  2516. GFP_KERNEL);
  2517. if (!regs)
  2518. return -ENOMEM;
  2519. for (i = 0; i < variant->num_registers; i++) {
  2520. const struct vc4_hdmi_register *field = &variant->registers[i];
  2521. if (field->reg != reg)
  2522. continue;
  2523. regs[count].name = field->name;
  2524. regs[count].offset = field->offset;
  2525. count++;
  2526. }
  2527. new_regs = krealloc(regs, count * sizeof(*regs), GFP_KERNEL);
  2528. if (!new_regs)
  2529. return -ENOMEM;
  2530. regset->base = __vc4_hdmi_get_field_base(vc4_hdmi, reg);
  2531. regset->regs = new_regs;
  2532. regset->nregs = count;
  2533. ret = drmm_add_action_or_reset(drm, vc4_hdmi_free_regset, new_regs);
  2534. if (ret)
  2535. return ret;
  2536. return 0;
  2537. }
  2538. static int vc4_hdmi_init_resources(struct drm_device *drm,
  2539. struct vc4_hdmi *vc4_hdmi)
  2540. {
  2541. struct platform_device *pdev = vc4_hdmi->pdev;
  2542. struct device *dev = &pdev->dev;
  2543. int ret;
  2544. vc4_hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0);
  2545. if (IS_ERR(vc4_hdmi->hdmicore_regs))
  2546. return PTR_ERR(vc4_hdmi->hdmicore_regs);
  2547. vc4_hdmi->hd_regs = vc4_ioremap_regs(pdev, 1);
  2548. if (IS_ERR(vc4_hdmi->hd_regs))
  2549. return PTR_ERR(vc4_hdmi->hd_regs);
  2550. ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD);
  2551. if (ret)
  2552. return ret;
  2553. ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI);
  2554. if (ret)
  2555. return ret;
  2556. vc4_hdmi->pixel_clock = devm_clk_get(dev, "pixel");
  2557. if (IS_ERR(vc4_hdmi->pixel_clock)) {
  2558. ret = PTR_ERR(vc4_hdmi->pixel_clock);
  2559. if (ret != -EPROBE_DEFER)
  2560. DRM_ERROR("Failed to get pixel clock\n");
  2561. return ret;
  2562. }
  2563. vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
  2564. if (IS_ERR(vc4_hdmi->hsm_clock)) {
  2565. DRM_ERROR("Failed to get HDMI state machine clock\n");
  2566. return PTR_ERR(vc4_hdmi->hsm_clock);
  2567. }
  2568. vc4_hdmi->audio_clock = vc4_hdmi->hsm_clock;
  2569. vc4_hdmi->cec_clock = vc4_hdmi->hsm_clock;
  2570. vc4_hdmi->hsm_rpm_clock = devm_clk_get(dev, "hdmi");
  2571. if (IS_ERR(vc4_hdmi->hsm_rpm_clock)) {
  2572. DRM_ERROR("Failed to get HDMI state machine clock\n");
  2573. return PTR_ERR(vc4_hdmi->hsm_rpm_clock);
  2574. }
  2575. return 0;
  2576. }
  2577. static int vc5_hdmi_init_resources(struct drm_device *drm,
  2578. struct vc4_hdmi *vc4_hdmi)
  2579. {
  2580. struct platform_device *pdev = vc4_hdmi->pdev;
  2581. struct device *dev = &pdev->dev;
  2582. struct resource *res;
  2583. int ret;
  2584. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hdmi");
  2585. if (!res)
  2586. return -ENODEV;
  2587. vc4_hdmi->hdmicore_regs = devm_ioremap(dev, res->start,
  2588. resource_size(res));
  2589. if (!vc4_hdmi->hdmicore_regs)
  2590. return -ENOMEM;
  2591. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hd");
  2592. if (!res)
  2593. return -ENODEV;
  2594. vc4_hdmi->hd_regs = devm_ioremap(dev, res->start, resource_size(res));
  2595. if (!vc4_hdmi->hd_regs)
  2596. return -ENOMEM;
  2597. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cec");
  2598. if (!res)
  2599. return -ENODEV;
  2600. vc4_hdmi->cec_regs = devm_ioremap(dev, res->start, resource_size(res));
  2601. if (!vc4_hdmi->cec_regs)
  2602. return -ENOMEM;
  2603. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "csc");
  2604. if (!res)
  2605. return -ENODEV;
  2606. vc4_hdmi->csc_regs = devm_ioremap(dev, res->start, resource_size(res));
  2607. if (!vc4_hdmi->csc_regs)
  2608. return -ENOMEM;
  2609. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dvp");
  2610. if (!res)
  2611. return -ENODEV;
  2612. vc4_hdmi->dvp_regs = devm_ioremap(dev, res->start, resource_size(res));
  2613. if (!vc4_hdmi->dvp_regs)
  2614. return -ENOMEM;
  2615. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy");
  2616. if (!res)
  2617. return -ENODEV;
  2618. vc4_hdmi->phy_regs = devm_ioremap(dev, res->start, resource_size(res));
  2619. if (!vc4_hdmi->phy_regs)
  2620. return -ENOMEM;
  2621. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "packet");
  2622. if (!res)
  2623. return -ENODEV;
  2624. vc4_hdmi->ram_regs = devm_ioremap(dev, res->start, resource_size(res));
  2625. if (!vc4_hdmi->ram_regs)
  2626. return -ENOMEM;
  2627. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rm");
  2628. if (!res)
  2629. return -ENODEV;
  2630. vc4_hdmi->rm_regs = devm_ioremap(dev, res->start, resource_size(res));
  2631. if (!vc4_hdmi->rm_regs)
  2632. return -ENOMEM;
  2633. vc4_hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
  2634. if (IS_ERR(vc4_hdmi->hsm_clock)) {
  2635. DRM_ERROR("Failed to get HDMI state machine clock\n");
  2636. return PTR_ERR(vc4_hdmi->hsm_clock);
  2637. }
  2638. vc4_hdmi->hsm_rpm_clock = devm_clk_get(dev, "hdmi");
  2639. if (IS_ERR(vc4_hdmi->hsm_rpm_clock)) {
  2640. DRM_ERROR("Failed to get HDMI state machine clock\n");
  2641. return PTR_ERR(vc4_hdmi->hsm_rpm_clock);
  2642. }
  2643. vc4_hdmi->pixel_bvb_clock = devm_clk_get(dev, "bvb");
  2644. if (IS_ERR(vc4_hdmi->pixel_bvb_clock)) {
  2645. DRM_ERROR("Failed to get pixel bvb clock\n");
  2646. return PTR_ERR(vc4_hdmi->pixel_bvb_clock);
  2647. }
  2648. vc4_hdmi->audio_clock = devm_clk_get(dev, "audio");
  2649. if (IS_ERR(vc4_hdmi->audio_clock)) {
  2650. DRM_ERROR("Failed to get audio clock\n");
  2651. return PTR_ERR(vc4_hdmi->audio_clock);
  2652. }
  2653. vc4_hdmi->cec_clock = devm_clk_get(dev, "cec");
  2654. if (IS_ERR(vc4_hdmi->cec_clock)) {
  2655. DRM_ERROR("Failed to get CEC clock\n");
  2656. return PTR_ERR(vc4_hdmi->cec_clock);
  2657. }
  2658. vc4_hdmi->reset = devm_reset_control_get(dev, NULL);
  2659. if (IS_ERR(vc4_hdmi->reset)) {
  2660. DRM_ERROR("Failed to get HDMI reset line\n");
  2661. return PTR_ERR(vc4_hdmi->reset);
  2662. }
  2663. ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hdmi_regset, VC4_HDMI);
  2664. if (ret)
  2665. return ret;
  2666. ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->hd_regset, VC4_HD);
  2667. if (ret)
  2668. return ret;
  2669. ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->cec_regset, VC5_CEC);
  2670. if (ret)
  2671. return ret;
  2672. ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->csc_regset, VC5_CSC);
  2673. if (ret)
  2674. return ret;
  2675. ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->dvp_regset, VC5_DVP);
  2676. if (ret)
  2677. return ret;
  2678. ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->phy_regset, VC5_PHY);
  2679. if (ret)
  2680. return ret;
  2681. ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->ram_regset, VC5_RAM);
  2682. if (ret)
  2683. return ret;
  2684. ret = vc4_hdmi_build_regset(drm, vc4_hdmi, &vc4_hdmi->rm_regset, VC5_RM);
  2685. if (ret)
  2686. return ret;
  2687. return 0;
  2688. }
  2689. static int vc4_hdmi_runtime_suspend(struct device *dev)
  2690. {
  2691. struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
  2692. clk_disable_unprepare(vc4_hdmi->hsm_rpm_clock);
  2693. return 0;
  2694. }
  2695. static int vc4_hdmi_runtime_resume(struct device *dev)
  2696. {
  2697. struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
  2698. unsigned long __maybe_unused flags;
  2699. u32 __maybe_unused value;
  2700. unsigned long rate;
  2701. int ret;
  2702. /*
  2703. * The HSM clock is in the HDMI power domain, so we need to set
  2704. * its frequency while the power domain is active so that it
  2705. * keeps its rate.
  2706. */
  2707. ret = clk_set_min_rate(vc4_hdmi->hsm_rpm_clock, HSM_MIN_CLOCK_FREQ);
  2708. if (ret)
  2709. return ret;
  2710. ret = clk_prepare_enable(vc4_hdmi->hsm_rpm_clock);
  2711. if (ret)
  2712. return ret;
  2713. /*
  2714. * Whenever the RaspberryPi boots without an HDMI monitor
  2715. * plugged in, the firmware won't have initialized the HSM clock
  2716. * rate and it will be reported as 0.
  2717. *
  2718. * If we try to access a register of the controller in such a
  2719. * case, it will lead to a silent CPU stall. Let's make sure we
  2720. * prevent such a case.
  2721. */
  2722. rate = clk_get_rate(vc4_hdmi->hsm_rpm_clock);
  2723. if (!rate) {
  2724. ret = -EINVAL;
  2725. goto err_disable_clk;
  2726. }
  2727. if (vc4_hdmi->variant->reset)
  2728. vc4_hdmi->variant->reset(vc4_hdmi);
  2729. #ifdef CONFIG_DRM_VC4_HDMI_CEC
  2730. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  2731. value = HDMI_READ(HDMI_CEC_CNTRL_1);
  2732. /* Set the logical address to Unregistered */
  2733. value |= VC4_HDMI_CEC_ADDR_MASK;
  2734. HDMI_WRITE(HDMI_CEC_CNTRL_1, value);
  2735. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  2736. vc4_hdmi_cec_update_clk_div(vc4_hdmi);
  2737. if (!vc4_hdmi->variant->external_irq_controller) {
  2738. spin_lock_irqsave(&vc4_hdmi->hw_lock, flags);
  2739. HDMI_WRITE(HDMI_CEC_CPU_MASK_SET, 0xffffffff);
  2740. spin_unlock_irqrestore(&vc4_hdmi->hw_lock, flags);
  2741. }
  2742. #endif
  2743. return 0;
  2744. err_disable_clk:
  2745. clk_disable_unprepare(vc4_hdmi->hsm_clock);
  2746. return ret;
  2747. }
  2748. static void vc4_hdmi_put_ddc_device(void *ptr)
  2749. {
  2750. struct vc4_hdmi *vc4_hdmi = ptr;
  2751. put_device(&vc4_hdmi->ddc->dev);
  2752. }
  2753. static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
  2754. {
  2755. const struct vc4_hdmi_variant *variant = of_device_get_match_data(dev);
  2756. struct platform_device *pdev = to_platform_device(dev);
  2757. struct drm_device *drm = dev_get_drvdata(master);
  2758. struct vc4_hdmi *vc4_hdmi;
  2759. struct drm_encoder *encoder;
  2760. struct device_node *ddc_node;
  2761. int ret;
  2762. vc4_hdmi = drmm_kzalloc(drm, sizeof(*vc4_hdmi), GFP_KERNEL);
  2763. if (!vc4_hdmi)
  2764. return -ENOMEM;
  2765. ret = drmm_mutex_init(drm, &vc4_hdmi->mutex);
  2766. if (ret)
  2767. return ret;
  2768. spin_lock_init(&vc4_hdmi->hw_lock);
  2769. INIT_DELAYED_WORK(&vc4_hdmi->scrambling_work, vc4_hdmi_scrambling_wq);
  2770. dev_set_drvdata(dev, vc4_hdmi);
  2771. encoder = &vc4_hdmi->encoder.base;
  2772. vc4_hdmi->encoder.type = variant->encoder_type;
  2773. vc4_hdmi->encoder.pre_crtc_configure = vc4_hdmi_encoder_pre_crtc_configure;
  2774. vc4_hdmi->encoder.pre_crtc_enable = vc4_hdmi_encoder_pre_crtc_enable;
  2775. vc4_hdmi->encoder.post_crtc_enable = vc4_hdmi_encoder_post_crtc_enable;
  2776. vc4_hdmi->encoder.post_crtc_disable = vc4_hdmi_encoder_post_crtc_disable;
  2777. vc4_hdmi->encoder.post_crtc_powerdown = vc4_hdmi_encoder_post_crtc_powerdown;
  2778. vc4_hdmi->pdev = pdev;
  2779. vc4_hdmi->variant = variant;
  2780. /*
  2781. * Since we don't know the state of the controller and its
  2782. * display (if any), let's assume it's always enabled.
  2783. * vc4_hdmi_disable_scrambling() will thus run at boot, make
  2784. * sure it's disabled, and avoid any inconsistency.
  2785. */
  2786. if (variant->max_pixel_clock > HDMI_14_MAX_TMDS_CLK)
  2787. vc4_hdmi->scdc_enabled = true;
  2788. ret = variant->init_resources(drm, vc4_hdmi);
  2789. if (ret)
  2790. return ret;
  2791. ddc_node = of_parse_phandle(dev->of_node, "ddc", 0);
  2792. if (!ddc_node) {
  2793. DRM_ERROR("Failed to find ddc node in device tree\n");
  2794. return -ENODEV;
  2795. }
  2796. vc4_hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
  2797. of_node_put(ddc_node);
  2798. if (!vc4_hdmi->ddc) {
  2799. DRM_DEBUG("Failed to get ddc i2c adapter by node\n");
  2800. return -EPROBE_DEFER;
  2801. }
  2802. ret = devm_add_action_or_reset(dev, vc4_hdmi_put_ddc_device, vc4_hdmi);
  2803. if (ret)
  2804. return ret;
  2805. /* Only use the GPIO HPD pin if present in the DT, otherwise
  2806. * we'll use the HDMI core's register.
  2807. */
  2808. vc4_hdmi->hpd_gpio = devm_gpiod_get_optional(dev, "hpd", GPIOD_IN);
  2809. if (IS_ERR(vc4_hdmi->hpd_gpio)) {
  2810. return PTR_ERR(vc4_hdmi->hpd_gpio);
  2811. }
  2812. vc4_hdmi->disable_wifi_frequencies =
  2813. of_property_read_bool(dev->of_node, "wifi-2.4ghz-coexistence");
  2814. if (variant->max_pixel_clock == 600000000) {
  2815. struct vc4_dev *vc4 = to_vc4_dev(drm);
  2816. long max_rate = clk_round_rate(vc4->hvs->core_clk, 550000000);
  2817. if (max_rate < 550000000)
  2818. vc4_hdmi->disable_4kp60 = true;
  2819. }
  2820. ret = devm_pm_runtime_enable(dev);
  2821. if (ret)
  2822. return ret;
  2823. /*
  2824. * We need to have the device powered up at this point to call
  2825. * our reset hook and for the CEC init.
  2826. */
  2827. ret = pm_runtime_resume_and_get(dev);
  2828. if (ret)
  2829. return ret;
  2830. if ((of_device_is_compatible(dev->of_node, "brcm,bcm2711-hdmi0") ||
  2831. of_device_is_compatible(dev->of_node, "brcm,bcm2711-hdmi1")) &&
  2832. HDMI_READ(HDMI_VID_CTL) & VC4_HD_VID_CTL_ENABLE) {
  2833. clk_prepare_enable(vc4_hdmi->pixel_clock);
  2834. clk_prepare_enable(vc4_hdmi->hsm_clock);
  2835. clk_prepare_enable(vc4_hdmi->pixel_bvb_clock);
  2836. }
  2837. ret = drmm_encoder_init(drm, encoder,
  2838. &vc4_hdmi_encoder_funcs,
  2839. DRM_MODE_ENCODER_TMDS,
  2840. NULL);
  2841. if (ret)
  2842. goto err_put_runtime_pm;
  2843. drm_encoder_helper_add(encoder, &vc4_hdmi_encoder_helper_funcs);
  2844. ret = vc4_hdmi_connector_init(drm, vc4_hdmi);
  2845. if (ret)
  2846. goto err_put_runtime_pm;
  2847. ret = vc4_hdmi_hotplug_init(vc4_hdmi);
  2848. if (ret)
  2849. goto err_put_runtime_pm;
  2850. ret = vc4_hdmi_cec_init(vc4_hdmi);
  2851. if (ret)
  2852. goto err_put_runtime_pm;
  2853. ret = vc4_hdmi_audio_init(vc4_hdmi);
  2854. if (ret)
  2855. goto err_put_runtime_pm;
  2856. pm_runtime_put_sync(dev);
  2857. return 0;
  2858. err_put_runtime_pm:
  2859. pm_runtime_put_sync(dev);
  2860. return ret;
  2861. }
  2862. static const struct component_ops vc4_hdmi_ops = {
  2863. .bind = vc4_hdmi_bind,
  2864. };
  2865. static int vc4_hdmi_dev_probe(struct platform_device *pdev)
  2866. {
  2867. return component_add(&pdev->dev, &vc4_hdmi_ops);
  2868. }
  2869. static int vc4_hdmi_dev_remove(struct platform_device *pdev)
  2870. {
  2871. component_del(&pdev->dev, &vc4_hdmi_ops);
  2872. return 0;
  2873. }
  2874. static const struct vc4_hdmi_variant bcm2835_variant = {
  2875. .encoder_type = VC4_ENCODER_TYPE_HDMI0,
  2876. .debugfs_name = "hdmi_regs",
  2877. .card_name = "vc4-hdmi",
  2878. .max_pixel_clock = 162000000,
  2879. .registers = vc4_hdmi_fields,
  2880. .num_registers = ARRAY_SIZE(vc4_hdmi_fields),
  2881. .init_resources = vc4_hdmi_init_resources,
  2882. .csc_setup = vc4_hdmi_csc_setup,
  2883. .reset = vc4_hdmi_reset,
  2884. .set_timings = vc4_hdmi_set_timings,
  2885. .phy_init = vc4_hdmi_phy_init,
  2886. .phy_disable = vc4_hdmi_phy_disable,
  2887. .phy_rng_enable = vc4_hdmi_phy_rng_enable,
  2888. .phy_rng_disable = vc4_hdmi_phy_rng_disable,
  2889. .channel_map = vc4_hdmi_channel_map,
  2890. .supports_hdr = false,
  2891. };
  2892. static const struct vc4_hdmi_variant bcm2711_hdmi0_variant = {
  2893. .encoder_type = VC4_ENCODER_TYPE_HDMI0,
  2894. .debugfs_name = "hdmi0_regs",
  2895. .card_name = "vc4-hdmi-0",
  2896. .max_pixel_clock = 600000000,
  2897. .registers = vc5_hdmi_hdmi0_fields,
  2898. .num_registers = ARRAY_SIZE(vc5_hdmi_hdmi0_fields),
  2899. .phy_lane_mapping = {
  2900. PHY_LANE_0,
  2901. PHY_LANE_1,
  2902. PHY_LANE_2,
  2903. PHY_LANE_CK,
  2904. },
  2905. .unsupported_odd_h_timings = true,
  2906. .external_irq_controller = true,
  2907. .init_resources = vc5_hdmi_init_resources,
  2908. .csc_setup = vc5_hdmi_csc_setup,
  2909. .reset = vc5_hdmi_reset,
  2910. .set_timings = vc5_hdmi_set_timings,
  2911. .phy_init = vc5_hdmi_phy_init,
  2912. .phy_disable = vc5_hdmi_phy_disable,
  2913. .phy_rng_enable = vc5_hdmi_phy_rng_enable,
  2914. .phy_rng_disable = vc5_hdmi_phy_rng_disable,
  2915. .channel_map = vc5_hdmi_channel_map,
  2916. .supports_hdr = true,
  2917. .hp_detect = vc5_hdmi_hp_detect,
  2918. };
  2919. static const struct vc4_hdmi_variant bcm2711_hdmi1_variant = {
  2920. .encoder_type = VC4_ENCODER_TYPE_HDMI1,
  2921. .debugfs_name = "hdmi1_regs",
  2922. .card_name = "vc4-hdmi-1",
  2923. .max_pixel_clock = HDMI_14_MAX_TMDS_CLK,
  2924. .registers = vc5_hdmi_hdmi1_fields,
  2925. .num_registers = ARRAY_SIZE(vc5_hdmi_hdmi1_fields),
  2926. .phy_lane_mapping = {
  2927. PHY_LANE_1,
  2928. PHY_LANE_0,
  2929. PHY_LANE_CK,
  2930. PHY_LANE_2,
  2931. },
  2932. .unsupported_odd_h_timings = true,
  2933. .external_irq_controller = true,
  2934. .init_resources = vc5_hdmi_init_resources,
  2935. .csc_setup = vc5_hdmi_csc_setup,
  2936. .reset = vc5_hdmi_reset,
  2937. .set_timings = vc5_hdmi_set_timings,
  2938. .phy_init = vc5_hdmi_phy_init,
  2939. .phy_disable = vc5_hdmi_phy_disable,
  2940. .phy_rng_enable = vc5_hdmi_phy_rng_enable,
  2941. .phy_rng_disable = vc5_hdmi_phy_rng_disable,
  2942. .channel_map = vc5_hdmi_channel_map,
  2943. .supports_hdr = true,
  2944. .hp_detect = vc5_hdmi_hp_detect,
  2945. };
  2946. static const struct of_device_id vc4_hdmi_dt_match[] = {
  2947. { .compatible = "brcm,bcm2835-hdmi", .data = &bcm2835_variant },
  2948. { .compatible = "brcm,bcm2711-hdmi0", .data = &bcm2711_hdmi0_variant },
  2949. { .compatible = "brcm,bcm2711-hdmi1", .data = &bcm2711_hdmi1_variant },
  2950. {}
  2951. };
  2952. static const struct dev_pm_ops vc4_hdmi_pm_ops = {
  2953. SET_RUNTIME_PM_OPS(vc4_hdmi_runtime_suspend,
  2954. vc4_hdmi_runtime_resume,
  2955. NULL)
  2956. };
  2957. struct platform_driver vc4_hdmi_driver = {
  2958. .probe = vc4_hdmi_dev_probe,
  2959. .remove = vc4_hdmi_dev_remove,
  2960. .driver = {
  2961. .name = "vc4_hdmi",
  2962. .of_match_table = vc4_hdmi_dt_match,
  2963. .pm = &vc4_hdmi_pm_ops,
  2964. },
  2965. };