vc4_hdmi.c 95 KB

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