dw-hdmi.c 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * DesignWare High-Definition Multimedia Interface (HDMI) driver
  4. *
  5. * Copyright (C) 2013-2015 Mentor Graphics Inc.
  6. * Copyright (C) 2011-2013 Freescale Semiconductor, Inc.
  7. * Copyright (C) 2010, Guennadi Liakhovetski <[email protected]>
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/err.h>
  12. #include <linux/hdmi.h>
  13. #include <linux/i2c.h>
  14. #include <linux/irq.h>
  15. #include <linux/module.h>
  16. #include <linux/mutex.h>
  17. #include <linux/of_device.h>
  18. #include <linux/pinctrl/consumer.h>
  19. #include <linux/regmap.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/spinlock.h>
  22. #include <media/cec-notifier.h>
  23. #include <uapi/linux/media-bus-format.h>
  24. #include <uapi/linux/videodev2.h>
  25. #include <drm/bridge/dw_hdmi.h>
  26. #include <drm/display/drm_hdmi_helper.h>
  27. #include <drm/display/drm_scdc_helper.h>
  28. #include <drm/drm_atomic.h>
  29. #include <drm/drm_atomic_helper.h>
  30. #include <drm/drm_bridge.h>
  31. #include <drm/drm_of.h>
  32. #include <drm/drm_print.h>
  33. #include <drm/drm_probe_helper.h>
  34. #include "dw-hdmi-audio.h"
  35. #include "dw-hdmi-cec.h"
  36. #include "dw-hdmi.h"
  37. #define DDC_CI_ADDR 0x37
  38. #define DDC_SEGMENT_ADDR 0x30
  39. #define HDMI_EDID_LEN 512
  40. /* DW-HDMI Controller >= 0x200a are at least compliant with SCDC version 1 */
  41. #define SCDC_MIN_SOURCE_VERSION 0x1
  42. #define HDMI14_MAX_TMDSCLK 340000000
  43. enum hdmi_datamap {
  44. RGB444_8B = 0x01,
  45. RGB444_10B = 0x03,
  46. RGB444_12B = 0x05,
  47. RGB444_16B = 0x07,
  48. YCbCr444_8B = 0x09,
  49. YCbCr444_10B = 0x0B,
  50. YCbCr444_12B = 0x0D,
  51. YCbCr444_16B = 0x0F,
  52. YCbCr422_8B = 0x16,
  53. YCbCr422_10B = 0x14,
  54. YCbCr422_12B = 0x12,
  55. };
  56. static const u16 csc_coeff_default[3][4] = {
  57. { 0x2000, 0x0000, 0x0000, 0x0000 },
  58. { 0x0000, 0x2000, 0x0000, 0x0000 },
  59. { 0x0000, 0x0000, 0x2000, 0x0000 }
  60. };
  61. static const u16 csc_coeff_rgb_out_eitu601[3][4] = {
  62. { 0x2000, 0x6926, 0x74fd, 0x010e },
  63. { 0x2000, 0x2cdd, 0x0000, 0x7e9a },
  64. { 0x2000, 0x0000, 0x38b4, 0x7e3b }
  65. };
  66. static const u16 csc_coeff_rgb_out_eitu709[3][4] = {
  67. { 0x2000, 0x7106, 0x7a02, 0x00a7 },
  68. { 0x2000, 0x3264, 0x0000, 0x7e6d },
  69. { 0x2000, 0x0000, 0x3b61, 0x7e25 }
  70. };
  71. static const u16 csc_coeff_rgb_in_eitu601[3][4] = {
  72. { 0x2591, 0x1322, 0x074b, 0x0000 },
  73. { 0x6535, 0x2000, 0x7acc, 0x0200 },
  74. { 0x6acd, 0x7534, 0x2000, 0x0200 }
  75. };
  76. static const u16 csc_coeff_rgb_in_eitu709[3][4] = {
  77. { 0x2dc5, 0x0d9b, 0x049e, 0x0000 },
  78. { 0x62f0, 0x2000, 0x7d11, 0x0200 },
  79. { 0x6756, 0x78ab, 0x2000, 0x0200 }
  80. };
  81. static const u16 csc_coeff_rgb_full_to_rgb_limited[3][4] = {
  82. { 0x1b7c, 0x0000, 0x0000, 0x0020 },
  83. { 0x0000, 0x1b7c, 0x0000, 0x0020 },
  84. { 0x0000, 0x0000, 0x1b7c, 0x0020 }
  85. };
  86. struct hdmi_vmode {
  87. bool mdataenablepolarity;
  88. unsigned int mpixelclock;
  89. unsigned int mpixelrepetitioninput;
  90. unsigned int mpixelrepetitionoutput;
  91. unsigned int mtmdsclock;
  92. };
  93. struct hdmi_data_info {
  94. unsigned int enc_in_bus_format;
  95. unsigned int enc_out_bus_format;
  96. unsigned int enc_in_encoding;
  97. unsigned int enc_out_encoding;
  98. unsigned int pix_repet_factor;
  99. unsigned int hdcp_enable;
  100. struct hdmi_vmode video_mode;
  101. bool rgb_limited_range;
  102. };
  103. struct dw_hdmi_i2c {
  104. struct i2c_adapter adap;
  105. struct mutex lock; /* used to serialize data transfers */
  106. struct completion cmp;
  107. u8 stat;
  108. u8 slave_reg;
  109. bool is_regaddr;
  110. bool is_segment;
  111. };
  112. struct dw_hdmi_phy_data {
  113. enum dw_hdmi_phy_type type;
  114. const char *name;
  115. unsigned int gen;
  116. bool has_svsret;
  117. int (*configure)(struct dw_hdmi *hdmi,
  118. const struct dw_hdmi_plat_data *pdata,
  119. unsigned long mpixelclock);
  120. };
  121. struct dw_hdmi {
  122. struct drm_connector connector;
  123. struct drm_bridge bridge;
  124. struct drm_bridge *next_bridge;
  125. unsigned int version;
  126. struct platform_device *audio;
  127. struct platform_device *cec;
  128. struct device *dev;
  129. struct clk *isfr_clk;
  130. struct clk *iahb_clk;
  131. struct clk *cec_clk;
  132. struct dw_hdmi_i2c *i2c;
  133. struct hdmi_data_info hdmi_data;
  134. const struct dw_hdmi_plat_data *plat_data;
  135. int vic;
  136. u8 edid[HDMI_EDID_LEN];
  137. struct {
  138. const struct dw_hdmi_phy_ops *ops;
  139. const char *name;
  140. void *data;
  141. bool enabled;
  142. } phy;
  143. struct drm_display_mode previous_mode;
  144. struct i2c_adapter *ddc;
  145. void __iomem *regs;
  146. bool sink_is_hdmi;
  147. bool sink_has_audio;
  148. struct pinctrl *pinctrl;
  149. struct pinctrl_state *default_state;
  150. struct pinctrl_state *unwedge_state;
  151. struct mutex mutex; /* for state below and previous_mode */
  152. enum drm_connector_force force; /* mutex-protected force state */
  153. struct drm_connector *curr_conn;/* current connector (only valid when !disabled) */
  154. bool disabled; /* DRM has disabled our bridge */
  155. bool bridge_is_on; /* indicates the bridge is on */
  156. bool rxsense; /* rxsense state */
  157. u8 phy_mask; /* desired phy int mask settings */
  158. u8 mc_clkdis; /* clock disable register */
  159. spinlock_t audio_lock;
  160. struct mutex audio_mutex;
  161. unsigned int sample_non_pcm;
  162. unsigned int sample_width;
  163. unsigned int sample_rate;
  164. unsigned int channels;
  165. unsigned int audio_cts;
  166. unsigned int audio_n;
  167. bool audio_enable;
  168. unsigned int reg_shift;
  169. struct regmap *regm;
  170. void (*enable_audio)(struct dw_hdmi *hdmi);
  171. void (*disable_audio)(struct dw_hdmi *hdmi);
  172. struct mutex cec_notifier_mutex;
  173. struct cec_notifier *cec_notifier;
  174. hdmi_codec_plugged_cb plugged_cb;
  175. struct device *codec_dev;
  176. enum drm_connector_status last_connector_result;
  177. };
  178. #define HDMI_IH_PHY_STAT0_RX_SENSE \
  179. (HDMI_IH_PHY_STAT0_RX_SENSE0 | HDMI_IH_PHY_STAT0_RX_SENSE1 | \
  180. HDMI_IH_PHY_STAT0_RX_SENSE2 | HDMI_IH_PHY_STAT0_RX_SENSE3)
  181. #define HDMI_PHY_RX_SENSE \
  182. (HDMI_PHY_RX_SENSE0 | HDMI_PHY_RX_SENSE1 | \
  183. HDMI_PHY_RX_SENSE2 | HDMI_PHY_RX_SENSE3)
  184. static inline void hdmi_writeb(struct dw_hdmi *hdmi, u8 val, int offset)
  185. {
  186. regmap_write(hdmi->regm, offset << hdmi->reg_shift, val);
  187. }
  188. static inline u8 hdmi_readb(struct dw_hdmi *hdmi, int offset)
  189. {
  190. unsigned int val = 0;
  191. regmap_read(hdmi->regm, offset << hdmi->reg_shift, &val);
  192. return val;
  193. }
  194. static void handle_plugged_change(struct dw_hdmi *hdmi, bool plugged)
  195. {
  196. if (hdmi->plugged_cb && hdmi->codec_dev)
  197. hdmi->plugged_cb(hdmi->codec_dev, plugged);
  198. }
  199. int dw_hdmi_set_plugged_cb(struct dw_hdmi *hdmi, hdmi_codec_plugged_cb fn,
  200. struct device *codec_dev)
  201. {
  202. bool plugged;
  203. mutex_lock(&hdmi->mutex);
  204. hdmi->plugged_cb = fn;
  205. hdmi->codec_dev = codec_dev;
  206. plugged = hdmi->last_connector_result == connector_status_connected;
  207. handle_plugged_change(hdmi, plugged);
  208. mutex_unlock(&hdmi->mutex);
  209. return 0;
  210. }
  211. EXPORT_SYMBOL_GPL(dw_hdmi_set_plugged_cb);
  212. static void hdmi_modb(struct dw_hdmi *hdmi, u8 data, u8 mask, unsigned reg)
  213. {
  214. regmap_update_bits(hdmi->regm, reg << hdmi->reg_shift, mask, data);
  215. }
  216. static void hdmi_mask_writeb(struct dw_hdmi *hdmi, u8 data, unsigned int reg,
  217. u8 shift, u8 mask)
  218. {
  219. hdmi_modb(hdmi, data << shift, mask, reg);
  220. }
  221. static void dw_hdmi_i2c_init(struct dw_hdmi *hdmi)
  222. {
  223. hdmi_writeb(hdmi, HDMI_PHY_I2CM_INT_ADDR_DONE_POL,
  224. HDMI_PHY_I2CM_INT_ADDR);
  225. hdmi_writeb(hdmi, HDMI_PHY_I2CM_CTLINT_ADDR_NAC_POL |
  226. HDMI_PHY_I2CM_CTLINT_ADDR_ARBITRATION_POL,
  227. HDMI_PHY_I2CM_CTLINT_ADDR);
  228. /* Software reset */
  229. hdmi_writeb(hdmi, 0x00, HDMI_I2CM_SOFTRSTZ);
  230. /* Set Standard Mode speed (determined to be 100KHz on iMX6) */
  231. hdmi_writeb(hdmi, 0x00, HDMI_I2CM_DIV);
  232. /* Set done, not acknowledged and arbitration interrupt polarities */
  233. hdmi_writeb(hdmi, HDMI_I2CM_INT_DONE_POL, HDMI_I2CM_INT);
  234. hdmi_writeb(hdmi, HDMI_I2CM_CTLINT_NAC_POL | HDMI_I2CM_CTLINT_ARB_POL,
  235. HDMI_I2CM_CTLINT);
  236. /* Clear DONE and ERROR interrupts */
  237. hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,
  238. HDMI_IH_I2CM_STAT0);
  239. /* Mute DONE and ERROR interrupts */
  240. hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,
  241. HDMI_IH_MUTE_I2CM_STAT0);
  242. }
  243. static bool dw_hdmi_i2c_unwedge(struct dw_hdmi *hdmi)
  244. {
  245. /* If no unwedge state then give up */
  246. if (!hdmi->unwedge_state)
  247. return false;
  248. dev_info(hdmi->dev, "Attempting to unwedge stuck i2c bus\n");
  249. /*
  250. * This is a huge hack to workaround a problem where the dw_hdmi i2c
  251. * bus could sometimes get wedged. Once wedged there doesn't appear
  252. * to be any way to unwedge it (including the HDMI_I2CM_SOFTRSTZ)
  253. * other than pulsing the SDA line.
  254. *
  255. * We appear to be able to pulse the SDA line (in the eyes of dw_hdmi)
  256. * by:
  257. * 1. Remux the pin as a GPIO output, driven low.
  258. * 2. Wait a little while. 1 ms seems to work, but we'll do 10.
  259. * 3. Immediately jump to remux the pin as dw_hdmi i2c again.
  260. *
  261. * At the moment of remuxing, the line will still be low due to its
  262. * recent stint as an output, but then it will be pulled high by the
  263. * (presumed) external pullup. dw_hdmi seems to see this as a rising
  264. * edge and that seems to get it out of its jam.
  265. *
  266. * This wedging was only ever seen on one TV, and only on one of
  267. * its HDMI ports. It happened when the TV was powered on while the
  268. * device was plugged in. A scope trace shows the TV bringing both SDA
  269. * and SCL low, then bringing them both back up at roughly the same
  270. * time. Presumably this confuses dw_hdmi because it saw activity but
  271. * no real STOP (maybe it thinks there's another master on the bus?).
  272. * Giving it a clean rising edge of SDA while SCL is already high
  273. * presumably makes dw_hdmi see a STOP which seems to bring dw_hdmi out
  274. * of its stupor.
  275. *
  276. * Note that after coming back alive, transfers seem to immediately
  277. * resume, so if we unwedge due to a timeout we should wait a little
  278. * longer for our transfer to finish, since it might have just started
  279. * now.
  280. */
  281. pinctrl_select_state(hdmi->pinctrl, hdmi->unwedge_state);
  282. msleep(10);
  283. pinctrl_select_state(hdmi->pinctrl, hdmi->default_state);
  284. return true;
  285. }
  286. static int dw_hdmi_i2c_wait(struct dw_hdmi *hdmi)
  287. {
  288. struct dw_hdmi_i2c *i2c = hdmi->i2c;
  289. int stat;
  290. stat = wait_for_completion_timeout(&i2c->cmp, HZ / 10);
  291. if (!stat) {
  292. /* If we can't unwedge, return timeout */
  293. if (!dw_hdmi_i2c_unwedge(hdmi))
  294. return -EAGAIN;
  295. /* We tried to unwedge; give it another chance */
  296. stat = wait_for_completion_timeout(&i2c->cmp, HZ / 10);
  297. if (!stat)
  298. return -EAGAIN;
  299. }
  300. /* Check for error condition on the bus */
  301. if (i2c->stat & HDMI_IH_I2CM_STAT0_ERROR)
  302. return -EIO;
  303. return 0;
  304. }
  305. static int dw_hdmi_i2c_read(struct dw_hdmi *hdmi,
  306. unsigned char *buf, unsigned int length)
  307. {
  308. struct dw_hdmi_i2c *i2c = hdmi->i2c;
  309. int ret;
  310. if (!i2c->is_regaddr) {
  311. dev_dbg(hdmi->dev, "set read register address to 0\n");
  312. i2c->slave_reg = 0x00;
  313. i2c->is_regaddr = true;
  314. }
  315. while (length--) {
  316. reinit_completion(&i2c->cmp);
  317. hdmi_writeb(hdmi, i2c->slave_reg++, HDMI_I2CM_ADDRESS);
  318. if (i2c->is_segment)
  319. hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_READ_EXT,
  320. HDMI_I2CM_OPERATION);
  321. else
  322. hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_READ,
  323. HDMI_I2CM_OPERATION);
  324. ret = dw_hdmi_i2c_wait(hdmi);
  325. if (ret)
  326. return ret;
  327. *buf++ = hdmi_readb(hdmi, HDMI_I2CM_DATAI);
  328. }
  329. i2c->is_segment = false;
  330. return 0;
  331. }
  332. static int dw_hdmi_i2c_write(struct dw_hdmi *hdmi,
  333. unsigned char *buf, unsigned int length)
  334. {
  335. struct dw_hdmi_i2c *i2c = hdmi->i2c;
  336. int ret;
  337. if (!i2c->is_regaddr) {
  338. /* Use the first write byte as register address */
  339. i2c->slave_reg = buf[0];
  340. length--;
  341. buf++;
  342. i2c->is_regaddr = true;
  343. }
  344. while (length--) {
  345. reinit_completion(&i2c->cmp);
  346. hdmi_writeb(hdmi, *buf++, HDMI_I2CM_DATAO);
  347. hdmi_writeb(hdmi, i2c->slave_reg++, HDMI_I2CM_ADDRESS);
  348. hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_WRITE,
  349. HDMI_I2CM_OPERATION);
  350. ret = dw_hdmi_i2c_wait(hdmi);
  351. if (ret)
  352. return ret;
  353. }
  354. return 0;
  355. }
  356. static int dw_hdmi_i2c_xfer(struct i2c_adapter *adap,
  357. struct i2c_msg *msgs, int num)
  358. {
  359. struct dw_hdmi *hdmi = i2c_get_adapdata(adap);
  360. struct dw_hdmi_i2c *i2c = hdmi->i2c;
  361. u8 addr = msgs[0].addr;
  362. int i, ret = 0;
  363. if (addr == DDC_CI_ADDR)
  364. /*
  365. * The internal I2C controller does not support the multi-byte
  366. * read and write operations needed for DDC/CI.
  367. * TOFIX: Blacklist the DDC/CI address until we filter out
  368. * unsupported I2C operations.
  369. */
  370. return -EOPNOTSUPP;
  371. dev_dbg(hdmi->dev, "xfer: num: %d, addr: %#x\n", num, addr);
  372. for (i = 0; i < num; i++) {
  373. if (msgs[i].len == 0) {
  374. dev_dbg(hdmi->dev,
  375. "unsupported transfer %d/%d, no data\n",
  376. i + 1, num);
  377. return -EOPNOTSUPP;
  378. }
  379. }
  380. mutex_lock(&i2c->lock);
  381. /* Unmute DONE and ERROR interrupts */
  382. hdmi_writeb(hdmi, 0x00, HDMI_IH_MUTE_I2CM_STAT0);
  383. /* Set slave device address taken from the first I2C message */
  384. hdmi_writeb(hdmi, addr, HDMI_I2CM_SLAVE);
  385. /* Set slave device register address on transfer */
  386. i2c->is_regaddr = false;
  387. /* Set segment pointer for I2C extended read mode operation */
  388. i2c->is_segment = false;
  389. for (i = 0; i < num; i++) {
  390. dev_dbg(hdmi->dev, "xfer: num: %d/%d, len: %d, flags: %#x\n",
  391. i + 1, num, msgs[i].len, msgs[i].flags);
  392. if (msgs[i].addr == DDC_SEGMENT_ADDR && msgs[i].len == 1) {
  393. i2c->is_segment = true;
  394. hdmi_writeb(hdmi, DDC_SEGMENT_ADDR, HDMI_I2CM_SEGADDR);
  395. hdmi_writeb(hdmi, *msgs[i].buf, HDMI_I2CM_SEGPTR);
  396. } else {
  397. if (msgs[i].flags & I2C_M_RD)
  398. ret = dw_hdmi_i2c_read(hdmi, msgs[i].buf,
  399. msgs[i].len);
  400. else
  401. ret = dw_hdmi_i2c_write(hdmi, msgs[i].buf,
  402. msgs[i].len);
  403. }
  404. if (ret < 0)
  405. break;
  406. }
  407. if (!ret)
  408. ret = num;
  409. /* Mute DONE and ERROR interrupts */
  410. hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,
  411. HDMI_IH_MUTE_I2CM_STAT0);
  412. mutex_unlock(&i2c->lock);
  413. return ret;
  414. }
  415. static u32 dw_hdmi_i2c_func(struct i2c_adapter *adapter)
  416. {
  417. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  418. }
  419. static const struct i2c_algorithm dw_hdmi_algorithm = {
  420. .master_xfer = dw_hdmi_i2c_xfer,
  421. .functionality = dw_hdmi_i2c_func,
  422. };
  423. static struct i2c_adapter *dw_hdmi_i2c_adapter(struct dw_hdmi *hdmi)
  424. {
  425. struct i2c_adapter *adap;
  426. struct dw_hdmi_i2c *i2c;
  427. int ret;
  428. i2c = devm_kzalloc(hdmi->dev, sizeof(*i2c), GFP_KERNEL);
  429. if (!i2c)
  430. return ERR_PTR(-ENOMEM);
  431. mutex_init(&i2c->lock);
  432. init_completion(&i2c->cmp);
  433. adap = &i2c->adap;
  434. adap->class = I2C_CLASS_DDC;
  435. adap->owner = THIS_MODULE;
  436. adap->dev.parent = hdmi->dev;
  437. adap->algo = &dw_hdmi_algorithm;
  438. strlcpy(adap->name, "DesignWare HDMI", sizeof(adap->name));
  439. i2c_set_adapdata(adap, hdmi);
  440. ret = i2c_add_adapter(adap);
  441. if (ret) {
  442. dev_warn(hdmi->dev, "cannot add %s I2C adapter\n", adap->name);
  443. devm_kfree(hdmi->dev, i2c);
  444. return ERR_PTR(ret);
  445. }
  446. hdmi->i2c = i2c;
  447. dev_info(hdmi->dev, "registered %s I2C bus driver\n", adap->name);
  448. return adap;
  449. }
  450. static void hdmi_set_cts_n(struct dw_hdmi *hdmi, unsigned int cts,
  451. unsigned int n)
  452. {
  453. /* Must be set/cleared first */
  454. hdmi_modb(hdmi, 0, HDMI_AUD_CTS3_CTS_MANUAL, HDMI_AUD_CTS3);
  455. /* nshift factor = 0 */
  456. hdmi_modb(hdmi, 0, HDMI_AUD_CTS3_N_SHIFT_MASK, HDMI_AUD_CTS3);
  457. /* Use automatic CTS generation mode when CTS is not set */
  458. if (cts)
  459. hdmi_writeb(hdmi, ((cts >> 16) &
  460. HDMI_AUD_CTS3_AUDCTS19_16_MASK) |
  461. HDMI_AUD_CTS3_CTS_MANUAL,
  462. HDMI_AUD_CTS3);
  463. else
  464. hdmi_writeb(hdmi, 0, HDMI_AUD_CTS3);
  465. hdmi_writeb(hdmi, (cts >> 8) & 0xff, HDMI_AUD_CTS2);
  466. hdmi_writeb(hdmi, cts & 0xff, HDMI_AUD_CTS1);
  467. hdmi_writeb(hdmi, (n >> 16) & 0x0f, HDMI_AUD_N3);
  468. hdmi_writeb(hdmi, (n >> 8) & 0xff, HDMI_AUD_N2);
  469. hdmi_writeb(hdmi, n & 0xff, HDMI_AUD_N1);
  470. }
  471. static unsigned int hdmi_compute_n(unsigned int freq, unsigned long pixel_clk)
  472. {
  473. unsigned int n = (128 * freq) / 1000;
  474. unsigned int mult = 1;
  475. while (freq > 48000) {
  476. mult *= 2;
  477. freq /= 2;
  478. }
  479. switch (freq) {
  480. case 32000:
  481. if (pixel_clk == 25175000)
  482. n = 4576;
  483. else if (pixel_clk == 27027000)
  484. n = 4096;
  485. else if (pixel_clk == 74176000 || pixel_clk == 148352000)
  486. n = 11648;
  487. else if (pixel_clk == 297000000)
  488. n = 3072;
  489. else
  490. n = 4096;
  491. n *= mult;
  492. break;
  493. case 44100:
  494. if (pixel_clk == 25175000)
  495. n = 7007;
  496. else if (pixel_clk == 74176000)
  497. n = 17836;
  498. else if (pixel_clk == 148352000)
  499. n = 8918;
  500. else if (pixel_clk == 297000000)
  501. n = 4704;
  502. else
  503. n = 6272;
  504. n *= mult;
  505. break;
  506. case 48000:
  507. if (pixel_clk == 25175000)
  508. n = 6864;
  509. else if (pixel_clk == 27027000)
  510. n = 6144;
  511. else if (pixel_clk == 74176000)
  512. n = 11648;
  513. else if (pixel_clk == 148352000)
  514. n = 5824;
  515. else if (pixel_clk == 297000000)
  516. n = 5120;
  517. else
  518. n = 6144;
  519. n *= mult;
  520. break;
  521. default:
  522. break;
  523. }
  524. return n;
  525. }
  526. /*
  527. * When transmitting IEC60958 linear PCM audio, these registers allow to
  528. * configure the channel status information of all the channel status
  529. * bits in the IEC60958 frame. For the moment this configuration is only
  530. * used when the I2S audio interface, General Purpose Audio (GPA),
  531. * or AHB audio DMA (AHBAUDDMA) interface is active
  532. * (for S/PDIF interface this information comes from the stream).
  533. */
  534. void dw_hdmi_set_channel_status(struct dw_hdmi *hdmi,
  535. u8 *channel_status)
  536. {
  537. /*
  538. * Set channel status register for frequency and word length.
  539. * Use default values for other registers.
  540. */
  541. hdmi_writeb(hdmi, channel_status[3], HDMI_FC_AUDSCHNLS7);
  542. hdmi_writeb(hdmi, channel_status[4], HDMI_FC_AUDSCHNLS8);
  543. }
  544. EXPORT_SYMBOL_GPL(dw_hdmi_set_channel_status);
  545. static void hdmi_set_clk_regenerator(struct dw_hdmi *hdmi,
  546. unsigned long pixel_clk, unsigned int sample_rate)
  547. {
  548. unsigned long ftdms = pixel_clk;
  549. unsigned int n, cts;
  550. u8 config3;
  551. u64 tmp;
  552. n = hdmi_compute_n(sample_rate, pixel_clk);
  553. config3 = hdmi_readb(hdmi, HDMI_CONFIG3_ID);
  554. /* Compute CTS when using internal AHB audio or General Parallel audio*/
  555. if ((config3 & HDMI_CONFIG3_AHBAUDDMA) || (config3 & HDMI_CONFIG3_GPAUD)) {
  556. /*
  557. * Compute the CTS value from the N value. Note that CTS and N
  558. * can be up to 20 bits in total, so we need 64-bit math. Also
  559. * note that our TDMS clock is not fully accurate; it is
  560. * accurate to kHz. This can introduce an unnecessary remainder
  561. * in the calculation below, so we don't try to warn about that.
  562. */
  563. tmp = (u64)ftdms * n;
  564. do_div(tmp, 128 * sample_rate);
  565. cts = tmp;
  566. dev_dbg(hdmi->dev, "%s: fs=%uHz ftdms=%lu.%03luMHz N=%d cts=%d\n",
  567. __func__, sample_rate,
  568. ftdms / 1000000, (ftdms / 1000) % 1000,
  569. n, cts);
  570. } else {
  571. cts = 0;
  572. }
  573. spin_lock_irq(&hdmi->audio_lock);
  574. hdmi->audio_n = n;
  575. hdmi->audio_cts = cts;
  576. hdmi_set_cts_n(hdmi, cts, hdmi->audio_enable ? n : 0);
  577. spin_unlock_irq(&hdmi->audio_lock);
  578. }
  579. static void hdmi_init_clk_regenerator(struct dw_hdmi *hdmi)
  580. {
  581. mutex_lock(&hdmi->audio_mutex);
  582. hdmi_set_clk_regenerator(hdmi, 74250000, hdmi->sample_rate);
  583. mutex_unlock(&hdmi->audio_mutex);
  584. }
  585. static void hdmi_clk_regenerator_update_pixel_clock(struct dw_hdmi *hdmi)
  586. {
  587. mutex_lock(&hdmi->audio_mutex);
  588. hdmi_set_clk_regenerator(hdmi, hdmi->hdmi_data.video_mode.mtmdsclock,
  589. hdmi->sample_rate);
  590. mutex_unlock(&hdmi->audio_mutex);
  591. }
  592. void dw_hdmi_set_sample_width(struct dw_hdmi *hdmi, unsigned int width)
  593. {
  594. mutex_lock(&hdmi->audio_mutex);
  595. hdmi->sample_width = width;
  596. mutex_unlock(&hdmi->audio_mutex);
  597. }
  598. EXPORT_SYMBOL_GPL(dw_hdmi_set_sample_width);
  599. void dw_hdmi_set_sample_non_pcm(struct dw_hdmi *hdmi, unsigned int non_pcm)
  600. {
  601. mutex_lock(&hdmi->audio_mutex);
  602. hdmi->sample_non_pcm = non_pcm;
  603. mutex_unlock(&hdmi->audio_mutex);
  604. }
  605. EXPORT_SYMBOL_GPL(dw_hdmi_set_sample_non_pcm);
  606. void dw_hdmi_set_sample_rate(struct dw_hdmi *hdmi, unsigned int rate)
  607. {
  608. mutex_lock(&hdmi->audio_mutex);
  609. hdmi->sample_rate = rate;
  610. hdmi_set_clk_regenerator(hdmi, hdmi->hdmi_data.video_mode.mtmdsclock,
  611. hdmi->sample_rate);
  612. mutex_unlock(&hdmi->audio_mutex);
  613. }
  614. EXPORT_SYMBOL_GPL(dw_hdmi_set_sample_rate);
  615. void dw_hdmi_set_channel_count(struct dw_hdmi *hdmi, unsigned int cnt)
  616. {
  617. u8 layout;
  618. mutex_lock(&hdmi->audio_mutex);
  619. hdmi->channels = cnt;
  620. /*
  621. * For >2 channel PCM audio, we need to select layout 1
  622. * and set an appropriate channel map.
  623. */
  624. if (cnt > 2)
  625. layout = HDMI_FC_AUDSCONF_AUD_PACKET_LAYOUT_LAYOUT1;
  626. else
  627. layout = HDMI_FC_AUDSCONF_AUD_PACKET_LAYOUT_LAYOUT0;
  628. hdmi_modb(hdmi, layout, HDMI_FC_AUDSCONF_AUD_PACKET_LAYOUT_MASK,
  629. HDMI_FC_AUDSCONF);
  630. /* Set the audio infoframes channel count */
  631. hdmi_modb(hdmi, (cnt - 1) << HDMI_FC_AUDICONF0_CC_OFFSET,
  632. HDMI_FC_AUDICONF0_CC_MASK, HDMI_FC_AUDICONF0);
  633. mutex_unlock(&hdmi->audio_mutex);
  634. }
  635. EXPORT_SYMBOL_GPL(dw_hdmi_set_channel_count);
  636. void dw_hdmi_set_channel_allocation(struct dw_hdmi *hdmi, unsigned int ca)
  637. {
  638. mutex_lock(&hdmi->audio_mutex);
  639. hdmi_writeb(hdmi, ca, HDMI_FC_AUDICONF2);
  640. mutex_unlock(&hdmi->audio_mutex);
  641. }
  642. EXPORT_SYMBOL_GPL(dw_hdmi_set_channel_allocation);
  643. static void hdmi_enable_audio_clk(struct dw_hdmi *hdmi, bool enable)
  644. {
  645. if (enable)
  646. hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_AUDCLK_DISABLE;
  647. else
  648. hdmi->mc_clkdis |= HDMI_MC_CLKDIS_AUDCLK_DISABLE;
  649. hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);
  650. }
  651. static u8 *hdmi_audio_get_eld(struct dw_hdmi *hdmi)
  652. {
  653. if (!hdmi->curr_conn)
  654. return NULL;
  655. return hdmi->curr_conn->eld;
  656. }
  657. static void dw_hdmi_gp_audio_enable(struct dw_hdmi *hdmi)
  658. {
  659. const struct dw_hdmi_plat_data *pdata = hdmi->plat_data;
  660. int sample_freq = 0x2, org_sample_freq = 0xD;
  661. int ch_mask = BIT(hdmi->channels) - 1;
  662. switch (hdmi->sample_rate) {
  663. case 32000:
  664. sample_freq = 0x03;
  665. org_sample_freq = 0x0C;
  666. break;
  667. case 44100:
  668. sample_freq = 0x00;
  669. org_sample_freq = 0x0F;
  670. break;
  671. case 48000:
  672. sample_freq = 0x02;
  673. org_sample_freq = 0x0D;
  674. break;
  675. case 88200:
  676. sample_freq = 0x08;
  677. org_sample_freq = 0x07;
  678. break;
  679. case 96000:
  680. sample_freq = 0x0A;
  681. org_sample_freq = 0x05;
  682. break;
  683. case 176400:
  684. sample_freq = 0x0C;
  685. org_sample_freq = 0x03;
  686. break;
  687. case 192000:
  688. sample_freq = 0x0E;
  689. org_sample_freq = 0x01;
  690. break;
  691. default:
  692. break;
  693. }
  694. hdmi_set_cts_n(hdmi, hdmi->audio_cts, hdmi->audio_n);
  695. hdmi_enable_audio_clk(hdmi, true);
  696. hdmi_writeb(hdmi, 0x1, HDMI_FC_AUDSCHNLS0);
  697. hdmi_writeb(hdmi, hdmi->channels, HDMI_FC_AUDSCHNLS2);
  698. hdmi_writeb(hdmi, 0x22, HDMI_FC_AUDSCHNLS3);
  699. hdmi_writeb(hdmi, 0x22, HDMI_FC_AUDSCHNLS4);
  700. hdmi_writeb(hdmi, 0x11, HDMI_FC_AUDSCHNLS5);
  701. hdmi_writeb(hdmi, 0x11, HDMI_FC_AUDSCHNLS6);
  702. hdmi_writeb(hdmi, (0x3 << 4) | sample_freq, HDMI_FC_AUDSCHNLS7);
  703. hdmi_writeb(hdmi, (org_sample_freq << 4) | 0xb, HDMI_FC_AUDSCHNLS8);
  704. hdmi_writeb(hdmi, ch_mask, HDMI_GP_CONF1);
  705. hdmi_writeb(hdmi, 0x02, HDMI_GP_CONF2);
  706. hdmi_writeb(hdmi, 0x01, HDMI_GP_CONF0);
  707. hdmi_modb(hdmi, 0x3, 0x3, HDMI_FC_DATAUTO3);
  708. /* hbr */
  709. if (hdmi->sample_rate == 192000 && hdmi->channels == 8 &&
  710. hdmi->sample_width == 32 && hdmi->sample_non_pcm)
  711. hdmi_modb(hdmi, 0x01, 0x01, HDMI_GP_CONF2);
  712. if (pdata->enable_audio)
  713. pdata->enable_audio(hdmi,
  714. hdmi->channels,
  715. hdmi->sample_width,
  716. hdmi->sample_rate,
  717. hdmi->sample_non_pcm);
  718. }
  719. static void dw_hdmi_gp_audio_disable(struct dw_hdmi *hdmi)
  720. {
  721. const struct dw_hdmi_plat_data *pdata = hdmi->plat_data;
  722. hdmi_set_cts_n(hdmi, hdmi->audio_cts, 0);
  723. hdmi_modb(hdmi, 0, 0x3, HDMI_FC_DATAUTO3);
  724. if (pdata->disable_audio)
  725. pdata->disable_audio(hdmi);
  726. hdmi_enable_audio_clk(hdmi, false);
  727. }
  728. static void dw_hdmi_ahb_audio_enable(struct dw_hdmi *hdmi)
  729. {
  730. hdmi_set_cts_n(hdmi, hdmi->audio_cts, hdmi->audio_n);
  731. }
  732. static void dw_hdmi_ahb_audio_disable(struct dw_hdmi *hdmi)
  733. {
  734. hdmi_set_cts_n(hdmi, hdmi->audio_cts, 0);
  735. }
  736. static void dw_hdmi_i2s_audio_enable(struct dw_hdmi *hdmi)
  737. {
  738. hdmi_set_cts_n(hdmi, hdmi->audio_cts, hdmi->audio_n);
  739. hdmi_enable_audio_clk(hdmi, true);
  740. }
  741. static void dw_hdmi_i2s_audio_disable(struct dw_hdmi *hdmi)
  742. {
  743. hdmi_enable_audio_clk(hdmi, false);
  744. }
  745. void dw_hdmi_audio_enable(struct dw_hdmi *hdmi)
  746. {
  747. unsigned long flags;
  748. spin_lock_irqsave(&hdmi->audio_lock, flags);
  749. hdmi->audio_enable = true;
  750. if (hdmi->enable_audio)
  751. hdmi->enable_audio(hdmi);
  752. spin_unlock_irqrestore(&hdmi->audio_lock, flags);
  753. }
  754. EXPORT_SYMBOL_GPL(dw_hdmi_audio_enable);
  755. void dw_hdmi_audio_disable(struct dw_hdmi *hdmi)
  756. {
  757. unsigned long flags;
  758. spin_lock_irqsave(&hdmi->audio_lock, flags);
  759. hdmi->audio_enable = false;
  760. if (hdmi->disable_audio)
  761. hdmi->disable_audio(hdmi);
  762. spin_unlock_irqrestore(&hdmi->audio_lock, flags);
  763. }
  764. EXPORT_SYMBOL_GPL(dw_hdmi_audio_disable);
  765. static bool hdmi_bus_fmt_is_rgb(unsigned int bus_format)
  766. {
  767. switch (bus_format) {
  768. case MEDIA_BUS_FMT_RGB888_1X24:
  769. case MEDIA_BUS_FMT_RGB101010_1X30:
  770. case MEDIA_BUS_FMT_RGB121212_1X36:
  771. case MEDIA_BUS_FMT_RGB161616_1X48:
  772. return true;
  773. default:
  774. return false;
  775. }
  776. }
  777. static bool hdmi_bus_fmt_is_yuv444(unsigned int bus_format)
  778. {
  779. switch (bus_format) {
  780. case MEDIA_BUS_FMT_YUV8_1X24:
  781. case MEDIA_BUS_FMT_YUV10_1X30:
  782. case MEDIA_BUS_FMT_YUV12_1X36:
  783. case MEDIA_BUS_FMT_YUV16_1X48:
  784. return true;
  785. default:
  786. return false;
  787. }
  788. }
  789. static bool hdmi_bus_fmt_is_yuv422(unsigned int bus_format)
  790. {
  791. switch (bus_format) {
  792. case MEDIA_BUS_FMT_UYVY8_1X16:
  793. case MEDIA_BUS_FMT_UYVY10_1X20:
  794. case MEDIA_BUS_FMT_UYVY12_1X24:
  795. return true;
  796. default:
  797. return false;
  798. }
  799. }
  800. static bool hdmi_bus_fmt_is_yuv420(unsigned int bus_format)
  801. {
  802. switch (bus_format) {
  803. case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
  804. case MEDIA_BUS_FMT_UYYVYY10_0_5X30:
  805. case MEDIA_BUS_FMT_UYYVYY12_0_5X36:
  806. case MEDIA_BUS_FMT_UYYVYY16_0_5X48:
  807. return true;
  808. default:
  809. return false;
  810. }
  811. }
  812. static int hdmi_bus_fmt_color_depth(unsigned int bus_format)
  813. {
  814. switch (bus_format) {
  815. case MEDIA_BUS_FMT_RGB888_1X24:
  816. case MEDIA_BUS_FMT_YUV8_1X24:
  817. case MEDIA_BUS_FMT_UYVY8_1X16:
  818. case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
  819. return 8;
  820. case MEDIA_BUS_FMT_RGB101010_1X30:
  821. case MEDIA_BUS_FMT_YUV10_1X30:
  822. case MEDIA_BUS_FMT_UYVY10_1X20:
  823. case MEDIA_BUS_FMT_UYYVYY10_0_5X30:
  824. return 10;
  825. case MEDIA_BUS_FMT_RGB121212_1X36:
  826. case MEDIA_BUS_FMT_YUV12_1X36:
  827. case MEDIA_BUS_FMT_UYVY12_1X24:
  828. case MEDIA_BUS_FMT_UYYVYY12_0_5X36:
  829. return 12;
  830. case MEDIA_BUS_FMT_RGB161616_1X48:
  831. case MEDIA_BUS_FMT_YUV16_1X48:
  832. case MEDIA_BUS_FMT_UYYVYY16_0_5X48:
  833. return 16;
  834. default:
  835. return 0;
  836. }
  837. }
  838. /*
  839. * this submodule is responsible for the video data synchronization.
  840. * for example, for RGB 4:4:4 input, the data map is defined as
  841. * pin{47~40} <==> R[7:0]
  842. * pin{31~24} <==> G[7:0]
  843. * pin{15~8} <==> B[7:0]
  844. */
  845. static void hdmi_video_sample(struct dw_hdmi *hdmi)
  846. {
  847. int color_format = 0;
  848. u8 val;
  849. switch (hdmi->hdmi_data.enc_in_bus_format) {
  850. case MEDIA_BUS_FMT_RGB888_1X24:
  851. color_format = 0x01;
  852. break;
  853. case MEDIA_BUS_FMT_RGB101010_1X30:
  854. color_format = 0x03;
  855. break;
  856. case MEDIA_BUS_FMT_RGB121212_1X36:
  857. color_format = 0x05;
  858. break;
  859. case MEDIA_BUS_FMT_RGB161616_1X48:
  860. color_format = 0x07;
  861. break;
  862. case MEDIA_BUS_FMT_YUV8_1X24:
  863. case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
  864. color_format = 0x09;
  865. break;
  866. case MEDIA_BUS_FMT_YUV10_1X30:
  867. case MEDIA_BUS_FMT_UYYVYY10_0_5X30:
  868. color_format = 0x0B;
  869. break;
  870. case MEDIA_BUS_FMT_YUV12_1X36:
  871. case MEDIA_BUS_FMT_UYYVYY12_0_5X36:
  872. color_format = 0x0D;
  873. break;
  874. case MEDIA_BUS_FMT_YUV16_1X48:
  875. case MEDIA_BUS_FMT_UYYVYY16_0_5X48:
  876. color_format = 0x0F;
  877. break;
  878. case MEDIA_BUS_FMT_UYVY8_1X16:
  879. color_format = 0x16;
  880. break;
  881. case MEDIA_BUS_FMT_UYVY10_1X20:
  882. color_format = 0x14;
  883. break;
  884. case MEDIA_BUS_FMT_UYVY12_1X24:
  885. color_format = 0x12;
  886. break;
  887. default:
  888. return;
  889. }
  890. val = HDMI_TX_INVID0_INTERNAL_DE_GENERATOR_DISABLE |
  891. ((color_format << HDMI_TX_INVID0_VIDEO_MAPPING_OFFSET) &
  892. HDMI_TX_INVID0_VIDEO_MAPPING_MASK);
  893. hdmi_writeb(hdmi, val, HDMI_TX_INVID0);
  894. /* Enable TX stuffing: When DE is inactive, fix the output data to 0 */
  895. val = HDMI_TX_INSTUFFING_BDBDATA_STUFFING_ENABLE |
  896. HDMI_TX_INSTUFFING_RCRDATA_STUFFING_ENABLE |
  897. HDMI_TX_INSTUFFING_GYDATA_STUFFING_ENABLE;
  898. hdmi_writeb(hdmi, val, HDMI_TX_INSTUFFING);
  899. hdmi_writeb(hdmi, 0x0, HDMI_TX_GYDATA0);
  900. hdmi_writeb(hdmi, 0x0, HDMI_TX_GYDATA1);
  901. hdmi_writeb(hdmi, 0x0, HDMI_TX_RCRDATA0);
  902. hdmi_writeb(hdmi, 0x0, HDMI_TX_RCRDATA1);
  903. hdmi_writeb(hdmi, 0x0, HDMI_TX_BCBDATA0);
  904. hdmi_writeb(hdmi, 0x0, HDMI_TX_BCBDATA1);
  905. }
  906. static int is_color_space_conversion(struct dw_hdmi *hdmi)
  907. {
  908. struct hdmi_data_info *hdmi_data = &hdmi->hdmi_data;
  909. bool is_input_rgb, is_output_rgb;
  910. is_input_rgb = hdmi_bus_fmt_is_rgb(hdmi_data->enc_in_bus_format);
  911. is_output_rgb = hdmi_bus_fmt_is_rgb(hdmi_data->enc_out_bus_format);
  912. return (is_input_rgb != is_output_rgb) ||
  913. (is_input_rgb && is_output_rgb && hdmi_data->rgb_limited_range);
  914. }
  915. static int is_color_space_decimation(struct dw_hdmi *hdmi)
  916. {
  917. if (!hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format))
  918. return 0;
  919. if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_in_bus_format) ||
  920. hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_in_bus_format))
  921. return 1;
  922. return 0;
  923. }
  924. static int is_color_space_interpolation(struct dw_hdmi *hdmi)
  925. {
  926. if (!hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_in_bus_format))
  927. return 0;
  928. if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format) ||
  929. hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format))
  930. return 1;
  931. return 0;
  932. }
  933. static bool is_csc_needed(struct dw_hdmi *hdmi)
  934. {
  935. return is_color_space_conversion(hdmi) ||
  936. is_color_space_decimation(hdmi) ||
  937. is_color_space_interpolation(hdmi);
  938. }
  939. static void dw_hdmi_update_csc_coeffs(struct dw_hdmi *hdmi)
  940. {
  941. const u16 (*csc_coeff)[3][4] = &csc_coeff_default;
  942. bool is_input_rgb, is_output_rgb;
  943. unsigned i;
  944. u32 csc_scale = 1;
  945. is_input_rgb = hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_in_bus_format);
  946. is_output_rgb = hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format);
  947. if (!is_input_rgb && is_output_rgb) {
  948. if (hdmi->hdmi_data.enc_out_encoding == V4L2_YCBCR_ENC_601)
  949. csc_coeff = &csc_coeff_rgb_out_eitu601;
  950. else
  951. csc_coeff = &csc_coeff_rgb_out_eitu709;
  952. } else if (is_input_rgb && !is_output_rgb) {
  953. if (hdmi->hdmi_data.enc_out_encoding == V4L2_YCBCR_ENC_601)
  954. csc_coeff = &csc_coeff_rgb_in_eitu601;
  955. else
  956. csc_coeff = &csc_coeff_rgb_in_eitu709;
  957. csc_scale = 0;
  958. } else if (is_input_rgb && is_output_rgb &&
  959. hdmi->hdmi_data.rgb_limited_range) {
  960. csc_coeff = &csc_coeff_rgb_full_to_rgb_limited;
  961. }
  962. /* The CSC registers are sequential, alternating MSB then LSB */
  963. for (i = 0; i < ARRAY_SIZE(csc_coeff_default[0]); i++) {
  964. u16 coeff_a = (*csc_coeff)[0][i];
  965. u16 coeff_b = (*csc_coeff)[1][i];
  966. u16 coeff_c = (*csc_coeff)[2][i];
  967. hdmi_writeb(hdmi, coeff_a & 0xff, HDMI_CSC_COEF_A1_LSB + i * 2);
  968. hdmi_writeb(hdmi, coeff_a >> 8, HDMI_CSC_COEF_A1_MSB + i * 2);
  969. hdmi_writeb(hdmi, coeff_b & 0xff, HDMI_CSC_COEF_B1_LSB + i * 2);
  970. hdmi_writeb(hdmi, coeff_b >> 8, HDMI_CSC_COEF_B1_MSB + i * 2);
  971. hdmi_writeb(hdmi, coeff_c & 0xff, HDMI_CSC_COEF_C1_LSB + i * 2);
  972. hdmi_writeb(hdmi, coeff_c >> 8, HDMI_CSC_COEF_C1_MSB + i * 2);
  973. }
  974. hdmi_modb(hdmi, csc_scale, HDMI_CSC_SCALE_CSCSCALE_MASK,
  975. HDMI_CSC_SCALE);
  976. }
  977. static void hdmi_video_csc(struct dw_hdmi *hdmi)
  978. {
  979. int color_depth = 0;
  980. int interpolation = HDMI_CSC_CFG_INTMODE_DISABLE;
  981. int decimation = 0;
  982. /* YCC422 interpolation to 444 mode */
  983. if (is_color_space_interpolation(hdmi))
  984. interpolation = HDMI_CSC_CFG_INTMODE_CHROMA_INT_FORMULA1;
  985. else if (is_color_space_decimation(hdmi))
  986. decimation = HDMI_CSC_CFG_DECMODE_CHROMA_INT_FORMULA3;
  987. switch (hdmi_bus_fmt_color_depth(hdmi->hdmi_data.enc_out_bus_format)) {
  988. case 8:
  989. color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_24BPP;
  990. break;
  991. case 10:
  992. color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_30BPP;
  993. break;
  994. case 12:
  995. color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_36BPP;
  996. break;
  997. case 16:
  998. color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_48BPP;
  999. break;
  1000. default:
  1001. return;
  1002. }
  1003. /* Configure the CSC registers */
  1004. hdmi_writeb(hdmi, interpolation | decimation, HDMI_CSC_CFG);
  1005. hdmi_modb(hdmi, color_depth, HDMI_CSC_SCALE_CSC_COLORDE_PTH_MASK,
  1006. HDMI_CSC_SCALE);
  1007. dw_hdmi_update_csc_coeffs(hdmi);
  1008. }
  1009. /*
  1010. * HDMI video packetizer is used to packetize the data.
  1011. * for example, if input is YCC422 mode or repeater is used,
  1012. * data should be repacked this module can be bypassed.
  1013. */
  1014. static void hdmi_video_packetize(struct dw_hdmi *hdmi)
  1015. {
  1016. unsigned int color_depth = 0;
  1017. unsigned int remap_size = HDMI_VP_REMAP_YCC422_16bit;
  1018. unsigned int output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_PP;
  1019. struct hdmi_data_info *hdmi_data = &hdmi->hdmi_data;
  1020. u8 val, vp_conf;
  1021. u8 clear_gcp_auto = 0;
  1022. if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format) ||
  1023. hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format) ||
  1024. hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format)) {
  1025. switch (hdmi_bus_fmt_color_depth(
  1026. hdmi->hdmi_data.enc_out_bus_format)) {
  1027. case 8:
  1028. color_depth = 4;
  1029. output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;
  1030. clear_gcp_auto = 1;
  1031. break;
  1032. case 10:
  1033. color_depth = 5;
  1034. break;
  1035. case 12:
  1036. color_depth = 6;
  1037. break;
  1038. case 16:
  1039. color_depth = 7;
  1040. break;
  1041. default:
  1042. output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;
  1043. }
  1044. } else if (hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format)) {
  1045. switch (hdmi_bus_fmt_color_depth(
  1046. hdmi->hdmi_data.enc_out_bus_format)) {
  1047. case 0:
  1048. case 8:
  1049. remap_size = HDMI_VP_REMAP_YCC422_16bit;
  1050. clear_gcp_auto = 1;
  1051. break;
  1052. case 10:
  1053. remap_size = HDMI_VP_REMAP_YCC422_20bit;
  1054. break;
  1055. case 12:
  1056. remap_size = HDMI_VP_REMAP_YCC422_24bit;
  1057. break;
  1058. default:
  1059. return;
  1060. }
  1061. output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_YCC422;
  1062. } else {
  1063. return;
  1064. }
  1065. /* set the packetizer registers */
  1066. val = ((color_depth << HDMI_VP_PR_CD_COLOR_DEPTH_OFFSET) &
  1067. HDMI_VP_PR_CD_COLOR_DEPTH_MASK) |
  1068. ((hdmi_data->pix_repet_factor <<
  1069. HDMI_VP_PR_CD_DESIRED_PR_FACTOR_OFFSET) &
  1070. HDMI_VP_PR_CD_DESIRED_PR_FACTOR_MASK);
  1071. hdmi_writeb(hdmi, val, HDMI_VP_PR_CD);
  1072. /* HDMI1.4b specification section 6.5.3:
  1073. * Source shall only send GCPs with non-zero CD to sinks
  1074. * that indicate support for Deep Color.
  1075. * GCP only transmit CD and do not handle AVMUTE, PP norDefault_Phase (yet).
  1076. * Disable Auto GCP when 24-bit color for sinks that not support Deep Color.
  1077. */
  1078. val = hdmi_readb(hdmi, HDMI_FC_DATAUTO3);
  1079. if (clear_gcp_auto == 1)
  1080. val &= ~HDMI_FC_DATAUTO3_GCP_AUTO;
  1081. else
  1082. val |= HDMI_FC_DATAUTO3_GCP_AUTO;
  1083. hdmi_writeb(hdmi, val, HDMI_FC_DATAUTO3);
  1084. hdmi_modb(hdmi, HDMI_VP_STUFF_PR_STUFFING_STUFFING_MODE,
  1085. HDMI_VP_STUFF_PR_STUFFING_MASK, HDMI_VP_STUFF);
  1086. /* Data from pixel repeater block */
  1087. if (hdmi_data->pix_repet_factor > 1) {
  1088. vp_conf = HDMI_VP_CONF_PR_EN_ENABLE |
  1089. HDMI_VP_CONF_BYPASS_SELECT_PIX_REPEATER;
  1090. } else { /* data from packetizer block */
  1091. vp_conf = HDMI_VP_CONF_PR_EN_DISABLE |
  1092. HDMI_VP_CONF_BYPASS_SELECT_VID_PACKETIZER;
  1093. }
  1094. hdmi_modb(hdmi, vp_conf,
  1095. HDMI_VP_CONF_PR_EN_MASK |
  1096. HDMI_VP_CONF_BYPASS_SELECT_MASK, HDMI_VP_CONF);
  1097. hdmi_modb(hdmi, 1 << HDMI_VP_STUFF_IDEFAULT_PHASE_OFFSET,
  1098. HDMI_VP_STUFF_IDEFAULT_PHASE_MASK, HDMI_VP_STUFF);
  1099. hdmi_writeb(hdmi, remap_size, HDMI_VP_REMAP);
  1100. if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_PP) {
  1101. vp_conf = HDMI_VP_CONF_BYPASS_EN_DISABLE |
  1102. HDMI_VP_CONF_PP_EN_ENABLE |
  1103. HDMI_VP_CONF_YCC422_EN_DISABLE;
  1104. } else if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_YCC422) {
  1105. vp_conf = HDMI_VP_CONF_BYPASS_EN_DISABLE |
  1106. HDMI_VP_CONF_PP_EN_DISABLE |
  1107. HDMI_VP_CONF_YCC422_EN_ENABLE;
  1108. } else if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS) {
  1109. vp_conf = HDMI_VP_CONF_BYPASS_EN_ENABLE |
  1110. HDMI_VP_CONF_PP_EN_DISABLE |
  1111. HDMI_VP_CONF_YCC422_EN_DISABLE;
  1112. } else {
  1113. return;
  1114. }
  1115. hdmi_modb(hdmi, vp_conf,
  1116. HDMI_VP_CONF_BYPASS_EN_MASK | HDMI_VP_CONF_PP_EN_ENMASK |
  1117. HDMI_VP_CONF_YCC422_EN_MASK, HDMI_VP_CONF);
  1118. hdmi_modb(hdmi, HDMI_VP_STUFF_PP_STUFFING_STUFFING_MODE |
  1119. HDMI_VP_STUFF_YCC422_STUFFING_STUFFING_MODE,
  1120. HDMI_VP_STUFF_PP_STUFFING_MASK |
  1121. HDMI_VP_STUFF_YCC422_STUFFING_MASK, HDMI_VP_STUFF);
  1122. hdmi_modb(hdmi, output_select, HDMI_VP_CONF_OUTPUT_SELECTOR_MASK,
  1123. HDMI_VP_CONF);
  1124. }
  1125. /* -----------------------------------------------------------------------------
  1126. * Synopsys PHY Handling
  1127. */
  1128. static inline void hdmi_phy_test_clear(struct dw_hdmi *hdmi,
  1129. unsigned char bit)
  1130. {
  1131. hdmi_modb(hdmi, bit << HDMI_PHY_TST0_TSTCLR_OFFSET,
  1132. HDMI_PHY_TST0_TSTCLR_MASK, HDMI_PHY_TST0);
  1133. }
  1134. static bool hdmi_phy_wait_i2c_done(struct dw_hdmi *hdmi, int msec)
  1135. {
  1136. u32 val;
  1137. while ((val = hdmi_readb(hdmi, HDMI_IH_I2CMPHY_STAT0) & 0x3) == 0) {
  1138. if (msec-- == 0)
  1139. return false;
  1140. udelay(1000);
  1141. }
  1142. hdmi_writeb(hdmi, val, HDMI_IH_I2CMPHY_STAT0);
  1143. return true;
  1144. }
  1145. void dw_hdmi_phy_i2c_write(struct dw_hdmi *hdmi, unsigned short data,
  1146. unsigned char addr)
  1147. {
  1148. hdmi_writeb(hdmi, 0xFF, HDMI_IH_I2CMPHY_STAT0);
  1149. hdmi_writeb(hdmi, addr, HDMI_PHY_I2CM_ADDRESS_ADDR);
  1150. hdmi_writeb(hdmi, (unsigned char)(data >> 8),
  1151. HDMI_PHY_I2CM_DATAO_1_ADDR);
  1152. hdmi_writeb(hdmi, (unsigned char)(data >> 0),
  1153. HDMI_PHY_I2CM_DATAO_0_ADDR);
  1154. hdmi_writeb(hdmi, HDMI_PHY_I2CM_OPERATION_ADDR_WRITE,
  1155. HDMI_PHY_I2CM_OPERATION_ADDR);
  1156. hdmi_phy_wait_i2c_done(hdmi, 1000);
  1157. }
  1158. EXPORT_SYMBOL_GPL(dw_hdmi_phy_i2c_write);
  1159. /* Filter out invalid setups to avoid configuring SCDC and scrambling */
  1160. static bool dw_hdmi_support_scdc(struct dw_hdmi *hdmi,
  1161. const struct drm_display_info *display)
  1162. {
  1163. /* Completely disable SCDC support for older controllers */
  1164. if (hdmi->version < 0x200a)
  1165. return false;
  1166. /* Disable if no DDC bus */
  1167. if (!hdmi->ddc)
  1168. return false;
  1169. /* Disable if SCDC is not supported, or if an HF-VSDB block is absent */
  1170. if (!display->hdmi.scdc.supported ||
  1171. !display->hdmi.scdc.scrambling.supported)
  1172. return false;
  1173. /*
  1174. * Disable if display only support low TMDS rates and scrambling
  1175. * for low rates is not supported either
  1176. */
  1177. if (!display->hdmi.scdc.scrambling.low_rates &&
  1178. display->max_tmds_clock <= 340000)
  1179. return false;
  1180. return true;
  1181. }
  1182. /*
  1183. * HDMI2.0 Specifies the following procedure for High TMDS Bit Rates:
  1184. * - The Source shall suspend transmission of the TMDS clock and data
  1185. * - The Source shall write to the TMDS_Bit_Clock_Ratio bit to change it
  1186. * from a 0 to a 1 or from a 1 to a 0
  1187. * - The Source shall allow a minimum of 1 ms and a maximum of 100 ms from
  1188. * the time the TMDS_Bit_Clock_Ratio bit is written until resuming
  1189. * transmission of TMDS clock and data
  1190. *
  1191. * To respect the 100ms maximum delay, the dw_hdmi_set_high_tmds_clock_ratio()
  1192. * helper should called right before enabling the TMDS Clock and Data in
  1193. * the PHY configuration callback.
  1194. */
  1195. void dw_hdmi_set_high_tmds_clock_ratio(struct dw_hdmi *hdmi,
  1196. const struct drm_display_info *display)
  1197. {
  1198. unsigned long mtmdsclock = hdmi->hdmi_data.video_mode.mtmdsclock;
  1199. /* Control for TMDS Bit Period/TMDS Clock-Period Ratio */
  1200. if (dw_hdmi_support_scdc(hdmi, display)) {
  1201. if (mtmdsclock > HDMI14_MAX_TMDSCLK)
  1202. drm_scdc_set_high_tmds_clock_ratio(hdmi->ddc, 1);
  1203. else
  1204. drm_scdc_set_high_tmds_clock_ratio(hdmi->ddc, 0);
  1205. }
  1206. }
  1207. EXPORT_SYMBOL_GPL(dw_hdmi_set_high_tmds_clock_ratio);
  1208. static void dw_hdmi_phy_enable_powerdown(struct dw_hdmi *hdmi, bool enable)
  1209. {
  1210. hdmi_mask_writeb(hdmi, !enable, HDMI_PHY_CONF0,
  1211. HDMI_PHY_CONF0_PDZ_OFFSET,
  1212. HDMI_PHY_CONF0_PDZ_MASK);
  1213. }
  1214. static void dw_hdmi_phy_enable_tmds(struct dw_hdmi *hdmi, u8 enable)
  1215. {
  1216. hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
  1217. HDMI_PHY_CONF0_ENTMDS_OFFSET,
  1218. HDMI_PHY_CONF0_ENTMDS_MASK);
  1219. }
  1220. static void dw_hdmi_phy_enable_svsret(struct dw_hdmi *hdmi, u8 enable)
  1221. {
  1222. hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
  1223. HDMI_PHY_CONF0_SVSRET_OFFSET,
  1224. HDMI_PHY_CONF0_SVSRET_MASK);
  1225. }
  1226. void dw_hdmi_phy_gen2_pddq(struct dw_hdmi *hdmi, u8 enable)
  1227. {
  1228. hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
  1229. HDMI_PHY_CONF0_GEN2_PDDQ_OFFSET,
  1230. HDMI_PHY_CONF0_GEN2_PDDQ_MASK);
  1231. }
  1232. EXPORT_SYMBOL_GPL(dw_hdmi_phy_gen2_pddq);
  1233. void dw_hdmi_phy_gen2_txpwron(struct dw_hdmi *hdmi, u8 enable)
  1234. {
  1235. hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
  1236. HDMI_PHY_CONF0_GEN2_TXPWRON_OFFSET,
  1237. HDMI_PHY_CONF0_GEN2_TXPWRON_MASK);
  1238. }
  1239. EXPORT_SYMBOL_GPL(dw_hdmi_phy_gen2_txpwron);
  1240. static void dw_hdmi_phy_sel_data_en_pol(struct dw_hdmi *hdmi, u8 enable)
  1241. {
  1242. hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
  1243. HDMI_PHY_CONF0_SELDATAENPOL_OFFSET,
  1244. HDMI_PHY_CONF0_SELDATAENPOL_MASK);
  1245. }
  1246. static void dw_hdmi_phy_sel_interface_control(struct dw_hdmi *hdmi, u8 enable)
  1247. {
  1248. hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
  1249. HDMI_PHY_CONF0_SELDIPIF_OFFSET,
  1250. HDMI_PHY_CONF0_SELDIPIF_MASK);
  1251. }
  1252. void dw_hdmi_phy_gen1_reset(struct dw_hdmi *hdmi)
  1253. {
  1254. /* PHY reset. The reset signal is active low on Gen1 PHYs. */
  1255. hdmi_writeb(hdmi, 0, HDMI_MC_PHYRSTZ);
  1256. hdmi_writeb(hdmi, HDMI_MC_PHYRSTZ_PHYRSTZ, HDMI_MC_PHYRSTZ);
  1257. }
  1258. EXPORT_SYMBOL_GPL(dw_hdmi_phy_gen1_reset);
  1259. void dw_hdmi_phy_gen2_reset(struct dw_hdmi *hdmi)
  1260. {
  1261. /* PHY reset. The reset signal is active high on Gen2 PHYs. */
  1262. hdmi_writeb(hdmi, HDMI_MC_PHYRSTZ_PHYRSTZ, HDMI_MC_PHYRSTZ);
  1263. hdmi_writeb(hdmi, 0, HDMI_MC_PHYRSTZ);
  1264. }
  1265. EXPORT_SYMBOL_GPL(dw_hdmi_phy_gen2_reset);
  1266. void dw_hdmi_phy_i2c_set_addr(struct dw_hdmi *hdmi, u8 address)
  1267. {
  1268. hdmi_phy_test_clear(hdmi, 1);
  1269. hdmi_writeb(hdmi, address, HDMI_PHY_I2CM_SLAVE_ADDR);
  1270. hdmi_phy_test_clear(hdmi, 0);
  1271. }
  1272. EXPORT_SYMBOL_GPL(dw_hdmi_phy_i2c_set_addr);
  1273. static void dw_hdmi_phy_power_off(struct dw_hdmi *hdmi)
  1274. {
  1275. const struct dw_hdmi_phy_data *phy = hdmi->phy.data;
  1276. unsigned int i;
  1277. u16 val;
  1278. if (phy->gen == 1) {
  1279. dw_hdmi_phy_enable_tmds(hdmi, 0);
  1280. dw_hdmi_phy_enable_powerdown(hdmi, true);
  1281. return;
  1282. }
  1283. dw_hdmi_phy_gen2_txpwron(hdmi, 0);
  1284. /*
  1285. * Wait for TX_PHY_LOCK to be deasserted to indicate that the PHY went
  1286. * to low power mode.
  1287. */
  1288. for (i = 0; i < 5; ++i) {
  1289. val = hdmi_readb(hdmi, HDMI_PHY_STAT0);
  1290. if (!(val & HDMI_PHY_TX_PHY_LOCK))
  1291. break;
  1292. usleep_range(1000, 2000);
  1293. }
  1294. if (val & HDMI_PHY_TX_PHY_LOCK)
  1295. dev_warn(hdmi->dev, "PHY failed to power down\n");
  1296. else
  1297. dev_dbg(hdmi->dev, "PHY powered down in %u iterations\n", i);
  1298. dw_hdmi_phy_gen2_pddq(hdmi, 1);
  1299. }
  1300. static int dw_hdmi_phy_power_on(struct dw_hdmi *hdmi)
  1301. {
  1302. const struct dw_hdmi_phy_data *phy = hdmi->phy.data;
  1303. unsigned int i;
  1304. u8 val;
  1305. if (phy->gen == 1) {
  1306. dw_hdmi_phy_enable_powerdown(hdmi, false);
  1307. /* Toggle TMDS enable. */
  1308. dw_hdmi_phy_enable_tmds(hdmi, 0);
  1309. dw_hdmi_phy_enable_tmds(hdmi, 1);
  1310. return 0;
  1311. }
  1312. dw_hdmi_phy_gen2_txpwron(hdmi, 1);
  1313. dw_hdmi_phy_gen2_pddq(hdmi, 0);
  1314. /* Wait for PHY PLL lock */
  1315. for (i = 0; i < 5; ++i) {
  1316. val = hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_TX_PHY_LOCK;
  1317. if (val)
  1318. break;
  1319. usleep_range(1000, 2000);
  1320. }
  1321. if (!val) {
  1322. dev_err(hdmi->dev, "PHY PLL failed to lock\n");
  1323. return -ETIMEDOUT;
  1324. }
  1325. dev_dbg(hdmi->dev, "PHY PLL locked %u iterations\n", i);
  1326. return 0;
  1327. }
  1328. /*
  1329. * PHY configuration function for the DWC HDMI 3D TX PHY. Based on the available
  1330. * information the DWC MHL PHY has the same register layout and is thus also
  1331. * supported by this function.
  1332. */
  1333. static int hdmi_phy_configure_dwc_hdmi_3d_tx(struct dw_hdmi *hdmi,
  1334. const struct dw_hdmi_plat_data *pdata,
  1335. unsigned long mpixelclock)
  1336. {
  1337. const struct dw_hdmi_mpll_config *mpll_config = pdata->mpll_cfg;
  1338. const struct dw_hdmi_curr_ctrl *curr_ctrl = pdata->cur_ctr;
  1339. const struct dw_hdmi_phy_config *phy_config = pdata->phy_config;
  1340. /* TOFIX Will need 420 specific PHY configuration tables */
  1341. /* PLL/MPLL Cfg - always match on final entry */
  1342. for (; mpll_config->mpixelclock != ~0UL; mpll_config++)
  1343. if (mpixelclock <= mpll_config->mpixelclock)
  1344. break;
  1345. for (; curr_ctrl->mpixelclock != ~0UL; curr_ctrl++)
  1346. if (mpixelclock <= curr_ctrl->mpixelclock)
  1347. break;
  1348. for (; phy_config->mpixelclock != ~0UL; phy_config++)
  1349. if (mpixelclock <= phy_config->mpixelclock)
  1350. break;
  1351. if (mpll_config->mpixelclock == ~0UL ||
  1352. curr_ctrl->mpixelclock == ~0UL ||
  1353. phy_config->mpixelclock == ~0UL)
  1354. return -EINVAL;
  1355. dw_hdmi_phy_i2c_write(hdmi, mpll_config->res[0].cpce,
  1356. HDMI_3D_TX_PHY_CPCE_CTRL);
  1357. dw_hdmi_phy_i2c_write(hdmi, mpll_config->res[0].gmp,
  1358. HDMI_3D_TX_PHY_GMPCTRL);
  1359. dw_hdmi_phy_i2c_write(hdmi, curr_ctrl->curr[0],
  1360. HDMI_3D_TX_PHY_CURRCTRL);
  1361. dw_hdmi_phy_i2c_write(hdmi, 0, HDMI_3D_TX_PHY_PLLPHBYCTRL);
  1362. dw_hdmi_phy_i2c_write(hdmi, HDMI_3D_TX_PHY_MSM_CTRL_CKO_SEL_FB_CLK,
  1363. HDMI_3D_TX_PHY_MSM_CTRL);
  1364. dw_hdmi_phy_i2c_write(hdmi, phy_config->term, HDMI_3D_TX_PHY_TXTERM);
  1365. dw_hdmi_phy_i2c_write(hdmi, phy_config->sym_ctr,
  1366. HDMI_3D_TX_PHY_CKSYMTXCTRL);
  1367. dw_hdmi_phy_i2c_write(hdmi, phy_config->vlev_ctr,
  1368. HDMI_3D_TX_PHY_VLEVCTRL);
  1369. /* Override and disable clock termination. */
  1370. dw_hdmi_phy_i2c_write(hdmi, HDMI_3D_TX_PHY_CKCALCTRL_OVERRIDE,
  1371. HDMI_3D_TX_PHY_CKCALCTRL);
  1372. return 0;
  1373. }
  1374. static int hdmi_phy_configure(struct dw_hdmi *hdmi,
  1375. const struct drm_display_info *display)
  1376. {
  1377. const struct dw_hdmi_phy_data *phy = hdmi->phy.data;
  1378. const struct dw_hdmi_plat_data *pdata = hdmi->plat_data;
  1379. unsigned long mpixelclock = hdmi->hdmi_data.video_mode.mpixelclock;
  1380. unsigned long mtmdsclock = hdmi->hdmi_data.video_mode.mtmdsclock;
  1381. int ret;
  1382. dw_hdmi_phy_power_off(hdmi);
  1383. dw_hdmi_set_high_tmds_clock_ratio(hdmi, display);
  1384. /* Leave low power consumption mode by asserting SVSRET. */
  1385. if (phy->has_svsret)
  1386. dw_hdmi_phy_enable_svsret(hdmi, 1);
  1387. dw_hdmi_phy_gen2_reset(hdmi);
  1388. hdmi_writeb(hdmi, HDMI_MC_HEACPHY_RST_ASSERT, HDMI_MC_HEACPHY_RST);
  1389. dw_hdmi_phy_i2c_set_addr(hdmi, HDMI_PHY_I2CM_SLAVE_ADDR_PHY_GEN2);
  1390. /* Write to the PHY as configured by the platform */
  1391. if (pdata->configure_phy)
  1392. ret = pdata->configure_phy(hdmi, pdata->priv_data, mpixelclock);
  1393. else
  1394. ret = phy->configure(hdmi, pdata, mpixelclock);
  1395. if (ret) {
  1396. dev_err(hdmi->dev, "PHY configuration failed (clock %lu)\n",
  1397. mpixelclock);
  1398. return ret;
  1399. }
  1400. /* Wait for resuming transmission of TMDS clock and data */
  1401. if (mtmdsclock > HDMI14_MAX_TMDSCLK)
  1402. msleep(100);
  1403. return dw_hdmi_phy_power_on(hdmi);
  1404. }
  1405. static int dw_hdmi_phy_init(struct dw_hdmi *hdmi, void *data,
  1406. const struct drm_display_info *display,
  1407. const struct drm_display_mode *mode)
  1408. {
  1409. int i, ret;
  1410. /* HDMI Phy spec says to do the phy initialization sequence twice */
  1411. for (i = 0; i < 2; i++) {
  1412. dw_hdmi_phy_sel_data_en_pol(hdmi, 1);
  1413. dw_hdmi_phy_sel_interface_control(hdmi, 0);
  1414. ret = hdmi_phy_configure(hdmi, display);
  1415. if (ret)
  1416. return ret;
  1417. }
  1418. return 0;
  1419. }
  1420. static void dw_hdmi_phy_disable(struct dw_hdmi *hdmi, void *data)
  1421. {
  1422. dw_hdmi_phy_power_off(hdmi);
  1423. }
  1424. enum drm_connector_status dw_hdmi_phy_read_hpd(struct dw_hdmi *hdmi,
  1425. void *data)
  1426. {
  1427. return hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_HPD ?
  1428. connector_status_connected : connector_status_disconnected;
  1429. }
  1430. EXPORT_SYMBOL_GPL(dw_hdmi_phy_read_hpd);
  1431. void dw_hdmi_phy_update_hpd(struct dw_hdmi *hdmi, void *data,
  1432. bool force, bool disabled, bool rxsense)
  1433. {
  1434. u8 old_mask = hdmi->phy_mask;
  1435. if (force || disabled || !rxsense)
  1436. hdmi->phy_mask |= HDMI_PHY_RX_SENSE;
  1437. else
  1438. hdmi->phy_mask &= ~HDMI_PHY_RX_SENSE;
  1439. if (old_mask != hdmi->phy_mask)
  1440. hdmi_writeb(hdmi, hdmi->phy_mask, HDMI_PHY_MASK0);
  1441. }
  1442. EXPORT_SYMBOL_GPL(dw_hdmi_phy_update_hpd);
  1443. void dw_hdmi_phy_setup_hpd(struct dw_hdmi *hdmi, void *data)
  1444. {
  1445. /*
  1446. * Configure the PHY RX SENSE and HPD interrupts polarities and clear
  1447. * any pending interrupt.
  1448. */
  1449. hdmi_writeb(hdmi, HDMI_PHY_HPD | HDMI_PHY_RX_SENSE, HDMI_PHY_POL0);
  1450. hdmi_writeb(hdmi, HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE,
  1451. HDMI_IH_PHY_STAT0);
  1452. /* Enable cable hot plug irq. */
  1453. hdmi_writeb(hdmi, hdmi->phy_mask, HDMI_PHY_MASK0);
  1454. /* Clear and unmute interrupts. */
  1455. hdmi_writeb(hdmi, HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE,
  1456. HDMI_IH_PHY_STAT0);
  1457. hdmi_writeb(hdmi, ~(HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE),
  1458. HDMI_IH_MUTE_PHY_STAT0);
  1459. }
  1460. EXPORT_SYMBOL_GPL(dw_hdmi_phy_setup_hpd);
  1461. static const struct dw_hdmi_phy_ops dw_hdmi_synopsys_phy_ops = {
  1462. .init = dw_hdmi_phy_init,
  1463. .disable = dw_hdmi_phy_disable,
  1464. .read_hpd = dw_hdmi_phy_read_hpd,
  1465. .update_hpd = dw_hdmi_phy_update_hpd,
  1466. .setup_hpd = dw_hdmi_phy_setup_hpd,
  1467. };
  1468. /* -----------------------------------------------------------------------------
  1469. * HDMI TX Setup
  1470. */
  1471. static void hdmi_tx_hdcp_config(struct dw_hdmi *hdmi)
  1472. {
  1473. u8 de;
  1474. if (hdmi->hdmi_data.video_mode.mdataenablepolarity)
  1475. de = HDMI_A_VIDPOLCFG_DATAENPOL_ACTIVE_HIGH;
  1476. else
  1477. de = HDMI_A_VIDPOLCFG_DATAENPOL_ACTIVE_LOW;
  1478. /* disable rx detect */
  1479. hdmi_modb(hdmi, HDMI_A_HDCPCFG0_RXDETECT_DISABLE,
  1480. HDMI_A_HDCPCFG0_RXDETECT_MASK, HDMI_A_HDCPCFG0);
  1481. hdmi_modb(hdmi, de, HDMI_A_VIDPOLCFG_DATAENPOL_MASK, HDMI_A_VIDPOLCFG);
  1482. hdmi_modb(hdmi, HDMI_A_HDCPCFG1_ENCRYPTIONDISABLE_DISABLE,
  1483. HDMI_A_HDCPCFG1_ENCRYPTIONDISABLE_MASK, HDMI_A_HDCPCFG1);
  1484. }
  1485. static void hdmi_config_AVI(struct dw_hdmi *hdmi,
  1486. const struct drm_connector *connector,
  1487. const struct drm_display_mode *mode)
  1488. {
  1489. struct hdmi_avi_infoframe frame;
  1490. u8 val;
  1491. /* Initialise info frame from DRM mode */
  1492. drm_hdmi_avi_infoframe_from_display_mode(&frame, connector, mode);
  1493. if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format)) {
  1494. drm_hdmi_avi_infoframe_quant_range(&frame, connector, mode,
  1495. hdmi->hdmi_data.rgb_limited_range ?
  1496. HDMI_QUANTIZATION_RANGE_LIMITED :
  1497. HDMI_QUANTIZATION_RANGE_FULL);
  1498. } else {
  1499. frame.quantization_range = HDMI_QUANTIZATION_RANGE_DEFAULT;
  1500. frame.ycc_quantization_range =
  1501. HDMI_YCC_QUANTIZATION_RANGE_LIMITED;
  1502. }
  1503. if (hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format))
  1504. frame.colorspace = HDMI_COLORSPACE_YUV444;
  1505. else if (hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format))
  1506. frame.colorspace = HDMI_COLORSPACE_YUV422;
  1507. else if (hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format))
  1508. frame.colorspace = HDMI_COLORSPACE_YUV420;
  1509. else
  1510. frame.colorspace = HDMI_COLORSPACE_RGB;
  1511. /* Set up colorimetry */
  1512. if (!hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format)) {
  1513. switch (hdmi->hdmi_data.enc_out_encoding) {
  1514. case V4L2_YCBCR_ENC_601:
  1515. if (hdmi->hdmi_data.enc_in_encoding == V4L2_YCBCR_ENC_XV601)
  1516. frame.colorimetry = HDMI_COLORIMETRY_EXTENDED;
  1517. else
  1518. frame.colorimetry = HDMI_COLORIMETRY_ITU_601;
  1519. frame.extended_colorimetry =
  1520. HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
  1521. break;
  1522. case V4L2_YCBCR_ENC_709:
  1523. if (hdmi->hdmi_data.enc_in_encoding == V4L2_YCBCR_ENC_XV709)
  1524. frame.colorimetry = HDMI_COLORIMETRY_EXTENDED;
  1525. else
  1526. frame.colorimetry = HDMI_COLORIMETRY_ITU_709;
  1527. frame.extended_colorimetry =
  1528. HDMI_EXTENDED_COLORIMETRY_XV_YCC_709;
  1529. break;
  1530. default: /* Carries no data */
  1531. frame.colorimetry = HDMI_COLORIMETRY_ITU_601;
  1532. frame.extended_colorimetry =
  1533. HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
  1534. break;
  1535. }
  1536. } else {
  1537. frame.colorimetry = HDMI_COLORIMETRY_NONE;
  1538. frame.extended_colorimetry =
  1539. HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
  1540. }
  1541. /*
  1542. * The Designware IP uses a different byte format from standard
  1543. * AVI info frames, though generally the bits are in the correct
  1544. * bytes.
  1545. */
  1546. /*
  1547. * AVI data byte 1 differences: Colorspace in bits 0,1 rather than 5,6,
  1548. * scan info in bits 4,5 rather than 0,1 and active aspect present in
  1549. * bit 6 rather than 4.
  1550. */
  1551. val = (frame.scan_mode & 3) << 4 | (frame.colorspace & 3);
  1552. if (frame.active_aspect & 15)
  1553. val |= HDMI_FC_AVICONF0_ACTIVE_FMT_INFO_PRESENT;
  1554. if (frame.top_bar || frame.bottom_bar)
  1555. val |= HDMI_FC_AVICONF0_BAR_DATA_HORIZ_BAR;
  1556. if (frame.left_bar || frame.right_bar)
  1557. val |= HDMI_FC_AVICONF0_BAR_DATA_VERT_BAR;
  1558. hdmi_writeb(hdmi, val, HDMI_FC_AVICONF0);
  1559. /* AVI data byte 2 differences: none */
  1560. val = ((frame.colorimetry & 0x3) << 6) |
  1561. ((frame.picture_aspect & 0x3) << 4) |
  1562. (frame.active_aspect & 0xf);
  1563. hdmi_writeb(hdmi, val, HDMI_FC_AVICONF1);
  1564. /* AVI data byte 3 differences: none */
  1565. val = ((frame.extended_colorimetry & 0x7) << 4) |
  1566. ((frame.quantization_range & 0x3) << 2) |
  1567. (frame.nups & 0x3);
  1568. if (frame.itc)
  1569. val |= HDMI_FC_AVICONF2_IT_CONTENT_VALID;
  1570. hdmi_writeb(hdmi, val, HDMI_FC_AVICONF2);
  1571. /* AVI data byte 4 differences: none */
  1572. val = frame.video_code & 0x7f;
  1573. hdmi_writeb(hdmi, val, HDMI_FC_AVIVID);
  1574. /* AVI Data Byte 5- set up input and output pixel repetition */
  1575. val = (((hdmi->hdmi_data.video_mode.mpixelrepetitioninput + 1) <<
  1576. HDMI_FC_PRCONF_INCOMING_PR_FACTOR_OFFSET) &
  1577. HDMI_FC_PRCONF_INCOMING_PR_FACTOR_MASK) |
  1578. ((hdmi->hdmi_data.video_mode.mpixelrepetitionoutput <<
  1579. HDMI_FC_PRCONF_OUTPUT_PR_FACTOR_OFFSET) &
  1580. HDMI_FC_PRCONF_OUTPUT_PR_FACTOR_MASK);
  1581. hdmi_writeb(hdmi, val, HDMI_FC_PRCONF);
  1582. /*
  1583. * AVI data byte 5 differences: content type in 0,1 rather than 4,5,
  1584. * ycc range in bits 2,3 rather than 6,7
  1585. */
  1586. val = ((frame.ycc_quantization_range & 0x3) << 2) |
  1587. (frame.content_type & 0x3);
  1588. hdmi_writeb(hdmi, val, HDMI_FC_AVICONF3);
  1589. /* AVI Data Bytes 6-13 */
  1590. hdmi_writeb(hdmi, frame.top_bar & 0xff, HDMI_FC_AVIETB0);
  1591. hdmi_writeb(hdmi, (frame.top_bar >> 8) & 0xff, HDMI_FC_AVIETB1);
  1592. hdmi_writeb(hdmi, frame.bottom_bar & 0xff, HDMI_FC_AVISBB0);
  1593. hdmi_writeb(hdmi, (frame.bottom_bar >> 8) & 0xff, HDMI_FC_AVISBB1);
  1594. hdmi_writeb(hdmi, frame.left_bar & 0xff, HDMI_FC_AVIELB0);
  1595. hdmi_writeb(hdmi, (frame.left_bar >> 8) & 0xff, HDMI_FC_AVIELB1);
  1596. hdmi_writeb(hdmi, frame.right_bar & 0xff, HDMI_FC_AVISRB0);
  1597. hdmi_writeb(hdmi, (frame.right_bar >> 8) & 0xff, HDMI_FC_AVISRB1);
  1598. }
  1599. static void hdmi_config_vendor_specific_infoframe(struct dw_hdmi *hdmi,
  1600. const struct drm_connector *connector,
  1601. const struct drm_display_mode *mode)
  1602. {
  1603. struct hdmi_vendor_infoframe frame;
  1604. u8 buffer[10];
  1605. ssize_t err;
  1606. err = drm_hdmi_vendor_infoframe_from_display_mode(&frame, connector,
  1607. mode);
  1608. if (err < 0)
  1609. /*
  1610. * Going into that statement does not means vendor infoframe
  1611. * fails. It just informed us that vendor infoframe is not
  1612. * needed for the selected mode. Only 4k or stereoscopic 3D
  1613. * mode requires vendor infoframe. So just simply return.
  1614. */
  1615. return;
  1616. err = hdmi_vendor_infoframe_pack(&frame, buffer, sizeof(buffer));
  1617. if (err < 0) {
  1618. dev_err(hdmi->dev, "Failed to pack vendor infoframe: %zd\n",
  1619. err);
  1620. return;
  1621. }
  1622. hdmi_mask_writeb(hdmi, 0, HDMI_FC_DATAUTO0, HDMI_FC_DATAUTO0_VSD_OFFSET,
  1623. HDMI_FC_DATAUTO0_VSD_MASK);
  1624. /* Set the length of HDMI vendor specific InfoFrame payload */
  1625. hdmi_writeb(hdmi, buffer[2], HDMI_FC_VSDSIZE);
  1626. /* Set 24bit IEEE Registration Identifier */
  1627. hdmi_writeb(hdmi, buffer[4], HDMI_FC_VSDIEEEID0);
  1628. hdmi_writeb(hdmi, buffer[5], HDMI_FC_VSDIEEEID1);
  1629. hdmi_writeb(hdmi, buffer[6], HDMI_FC_VSDIEEEID2);
  1630. /* Set HDMI_Video_Format and HDMI_VIC/3D_Structure */
  1631. hdmi_writeb(hdmi, buffer[7], HDMI_FC_VSDPAYLOAD0);
  1632. hdmi_writeb(hdmi, buffer[8], HDMI_FC_VSDPAYLOAD1);
  1633. if (frame.s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF)
  1634. hdmi_writeb(hdmi, buffer[9], HDMI_FC_VSDPAYLOAD2);
  1635. /* Packet frame interpolation */
  1636. hdmi_writeb(hdmi, 1, HDMI_FC_DATAUTO1);
  1637. /* Auto packets per frame and line spacing */
  1638. hdmi_writeb(hdmi, 0x11, HDMI_FC_DATAUTO2);
  1639. /* Configures the Frame Composer On RDRB mode */
  1640. hdmi_mask_writeb(hdmi, 1, HDMI_FC_DATAUTO0, HDMI_FC_DATAUTO0_VSD_OFFSET,
  1641. HDMI_FC_DATAUTO0_VSD_MASK);
  1642. }
  1643. static void hdmi_config_drm_infoframe(struct dw_hdmi *hdmi,
  1644. const struct drm_connector *connector)
  1645. {
  1646. const struct drm_connector_state *conn_state = connector->state;
  1647. struct hdmi_drm_infoframe frame;
  1648. u8 buffer[30];
  1649. ssize_t err;
  1650. int i;
  1651. if (!hdmi->plat_data->use_drm_infoframe)
  1652. return;
  1653. hdmi_modb(hdmi, HDMI_FC_PACKET_TX_EN_DRM_DISABLE,
  1654. HDMI_FC_PACKET_TX_EN_DRM_MASK, HDMI_FC_PACKET_TX_EN);
  1655. err = drm_hdmi_infoframe_set_hdr_metadata(&frame, conn_state);
  1656. if (err < 0)
  1657. return;
  1658. err = hdmi_drm_infoframe_pack(&frame, buffer, sizeof(buffer));
  1659. if (err < 0) {
  1660. dev_err(hdmi->dev, "Failed to pack drm infoframe: %zd\n", err);
  1661. return;
  1662. }
  1663. hdmi_writeb(hdmi, frame.version, HDMI_FC_DRM_HB0);
  1664. hdmi_writeb(hdmi, frame.length, HDMI_FC_DRM_HB1);
  1665. for (i = 0; i < frame.length; i++)
  1666. hdmi_writeb(hdmi, buffer[4 + i], HDMI_FC_DRM_PB0 + i);
  1667. hdmi_writeb(hdmi, 1, HDMI_FC_DRM_UP);
  1668. hdmi_modb(hdmi, HDMI_FC_PACKET_TX_EN_DRM_ENABLE,
  1669. HDMI_FC_PACKET_TX_EN_DRM_MASK, HDMI_FC_PACKET_TX_EN);
  1670. }
  1671. static void hdmi_av_composer(struct dw_hdmi *hdmi,
  1672. const struct drm_display_info *display,
  1673. const struct drm_display_mode *mode)
  1674. {
  1675. u8 inv_val, bytes;
  1676. const struct drm_hdmi_info *hdmi_info = &display->hdmi;
  1677. struct hdmi_vmode *vmode = &hdmi->hdmi_data.video_mode;
  1678. int hblank, vblank, h_de_hs, v_de_vs, hsync_len, vsync_len;
  1679. unsigned int vdisplay, hdisplay;
  1680. vmode->mpixelclock = mode->clock * 1000;
  1681. dev_dbg(hdmi->dev, "final pixclk = %d\n", vmode->mpixelclock);
  1682. vmode->mtmdsclock = vmode->mpixelclock;
  1683. if (!hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format)) {
  1684. switch (hdmi_bus_fmt_color_depth(
  1685. hdmi->hdmi_data.enc_out_bus_format)) {
  1686. case 16:
  1687. vmode->mtmdsclock = vmode->mpixelclock * 2;
  1688. break;
  1689. case 12:
  1690. vmode->mtmdsclock = vmode->mpixelclock * 3 / 2;
  1691. break;
  1692. case 10:
  1693. vmode->mtmdsclock = vmode->mpixelclock * 5 / 4;
  1694. break;
  1695. }
  1696. }
  1697. if (hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format))
  1698. vmode->mtmdsclock /= 2;
  1699. dev_dbg(hdmi->dev, "final tmdsclock = %d\n", vmode->mtmdsclock);
  1700. /* Set up HDMI_FC_INVIDCONF */
  1701. inv_val = (hdmi->hdmi_data.hdcp_enable ||
  1702. (dw_hdmi_support_scdc(hdmi, display) &&
  1703. (vmode->mtmdsclock > HDMI14_MAX_TMDSCLK ||
  1704. hdmi_info->scdc.scrambling.low_rates)) ?
  1705. HDMI_FC_INVIDCONF_HDCP_KEEPOUT_ACTIVE :
  1706. HDMI_FC_INVIDCONF_HDCP_KEEPOUT_INACTIVE);
  1707. inv_val |= mode->flags & DRM_MODE_FLAG_PVSYNC ?
  1708. HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_HIGH :
  1709. HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_LOW;
  1710. inv_val |= mode->flags & DRM_MODE_FLAG_PHSYNC ?
  1711. HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_HIGH :
  1712. HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_LOW;
  1713. inv_val |= (vmode->mdataenablepolarity ?
  1714. HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_HIGH :
  1715. HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_LOW);
  1716. if (hdmi->vic == 39)
  1717. inv_val |= HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_HIGH;
  1718. else
  1719. inv_val |= mode->flags & DRM_MODE_FLAG_INTERLACE ?
  1720. HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_HIGH :
  1721. HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_LOW;
  1722. inv_val |= mode->flags & DRM_MODE_FLAG_INTERLACE ?
  1723. HDMI_FC_INVIDCONF_IN_I_P_INTERLACED :
  1724. HDMI_FC_INVIDCONF_IN_I_P_PROGRESSIVE;
  1725. inv_val |= hdmi->sink_is_hdmi ?
  1726. HDMI_FC_INVIDCONF_DVI_MODEZ_HDMI_MODE :
  1727. HDMI_FC_INVIDCONF_DVI_MODEZ_DVI_MODE;
  1728. hdmi_writeb(hdmi, inv_val, HDMI_FC_INVIDCONF);
  1729. hdisplay = mode->hdisplay;
  1730. hblank = mode->htotal - mode->hdisplay;
  1731. h_de_hs = mode->hsync_start - mode->hdisplay;
  1732. hsync_len = mode->hsync_end - mode->hsync_start;
  1733. /*
  1734. * When we're setting a YCbCr420 mode, we need
  1735. * to adjust the horizontal timing to suit.
  1736. */
  1737. if (hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format)) {
  1738. hdisplay /= 2;
  1739. hblank /= 2;
  1740. h_de_hs /= 2;
  1741. hsync_len /= 2;
  1742. }
  1743. vdisplay = mode->vdisplay;
  1744. vblank = mode->vtotal - mode->vdisplay;
  1745. v_de_vs = mode->vsync_start - mode->vdisplay;
  1746. vsync_len = mode->vsync_end - mode->vsync_start;
  1747. /*
  1748. * When we're setting an interlaced mode, we need
  1749. * to adjust the vertical timing to suit.
  1750. */
  1751. if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
  1752. vdisplay /= 2;
  1753. vblank /= 2;
  1754. v_de_vs /= 2;
  1755. vsync_len /= 2;
  1756. }
  1757. /* Scrambling Control */
  1758. if (dw_hdmi_support_scdc(hdmi, display)) {
  1759. if (vmode->mtmdsclock > HDMI14_MAX_TMDSCLK ||
  1760. hdmi_info->scdc.scrambling.low_rates) {
  1761. /*
  1762. * HDMI2.0 Specifies the following procedure:
  1763. * After the Source Device has determined that
  1764. * SCDC_Present is set (=1), the Source Device should
  1765. * write the accurate Version of the Source Device
  1766. * to the Source Version field in the SCDCS.
  1767. * Source Devices compliant shall set the
  1768. * Source Version = 1.
  1769. */
  1770. drm_scdc_readb(hdmi->ddc, SCDC_SINK_VERSION,
  1771. &bytes);
  1772. drm_scdc_writeb(hdmi->ddc, SCDC_SOURCE_VERSION,
  1773. min_t(u8, bytes, SCDC_MIN_SOURCE_VERSION));
  1774. /* Enabled Scrambling in the Sink */
  1775. drm_scdc_set_scrambling(hdmi->ddc, 1);
  1776. /*
  1777. * To activate the scrambler feature, you must ensure
  1778. * that the quasi-static configuration bit
  1779. * fc_invidconf.HDCP_keepout is set at configuration
  1780. * time, before the required mc_swrstzreq.tmdsswrst_req
  1781. * reset request is issued.
  1782. */
  1783. hdmi_writeb(hdmi, (u8)~HDMI_MC_SWRSTZ_TMDSSWRST_REQ,
  1784. HDMI_MC_SWRSTZ);
  1785. hdmi_writeb(hdmi, 1, HDMI_FC_SCRAMBLER_CTRL);
  1786. } else {
  1787. hdmi_writeb(hdmi, 0, HDMI_FC_SCRAMBLER_CTRL);
  1788. hdmi_writeb(hdmi, (u8)~HDMI_MC_SWRSTZ_TMDSSWRST_REQ,
  1789. HDMI_MC_SWRSTZ);
  1790. drm_scdc_set_scrambling(hdmi->ddc, 0);
  1791. }
  1792. }
  1793. /* Set up horizontal active pixel width */
  1794. hdmi_writeb(hdmi, hdisplay >> 8, HDMI_FC_INHACTV1);
  1795. hdmi_writeb(hdmi, hdisplay, HDMI_FC_INHACTV0);
  1796. /* Set up vertical active lines */
  1797. hdmi_writeb(hdmi, vdisplay >> 8, HDMI_FC_INVACTV1);
  1798. hdmi_writeb(hdmi, vdisplay, HDMI_FC_INVACTV0);
  1799. /* Set up horizontal blanking pixel region width */
  1800. hdmi_writeb(hdmi, hblank >> 8, HDMI_FC_INHBLANK1);
  1801. hdmi_writeb(hdmi, hblank, HDMI_FC_INHBLANK0);
  1802. /* Set up vertical blanking pixel region width */
  1803. hdmi_writeb(hdmi, vblank, HDMI_FC_INVBLANK);
  1804. /* Set up HSYNC active edge delay width (in pixel clks) */
  1805. hdmi_writeb(hdmi, h_de_hs >> 8, HDMI_FC_HSYNCINDELAY1);
  1806. hdmi_writeb(hdmi, h_de_hs, HDMI_FC_HSYNCINDELAY0);
  1807. /* Set up VSYNC active edge delay (in lines) */
  1808. hdmi_writeb(hdmi, v_de_vs, HDMI_FC_VSYNCINDELAY);
  1809. /* Set up HSYNC active pulse width (in pixel clks) */
  1810. hdmi_writeb(hdmi, hsync_len >> 8, HDMI_FC_HSYNCINWIDTH1);
  1811. hdmi_writeb(hdmi, hsync_len, HDMI_FC_HSYNCINWIDTH0);
  1812. /* Set up VSYNC active edge delay (in lines) */
  1813. hdmi_writeb(hdmi, vsync_len, HDMI_FC_VSYNCINWIDTH);
  1814. }
  1815. /* HDMI Initialization Step B.4 */
  1816. static void dw_hdmi_enable_video_path(struct dw_hdmi *hdmi)
  1817. {
  1818. /* control period minimum duration */
  1819. hdmi_writeb(hdmi, 12, HDMI_FC_CTRLDUR);
  1820. hdmi_writeb(hdmi, 32, HDMI_FC_EXCTRLDUR);
  1821. hdmi_writeb(hdmi, 1, HDMI_FC_EXCTRLSPAC);
  1822. /* Set to fill TMDS data channels */
  1823. hdmi_writeb(hdmi, 0x0B, HDMI_FC_CH0PREAM);
  1824. hdmi_writeb(hdmi, 0x16, HDMI_FC_CH1PREAM);
  1825. hdmi_writeb(hdmi, 0x21, HDMI_FC_CH2PREAM);
  1826. /* Enable pixel clock and tmds data path */
  1827. hdmi->mc_clkdis |= HDMI_MC_CLKDIS_HDCPCLK_DISABLE |
  1828. HDMI_MC_CLKDIS_CSCCLK_DISABLE |
  1829. HDMI_MC_CLKDIS_AUDCLK_DISABLE |
  1830. HDMI_MC_CLKDIS_PREPCLK_DISABLE |
  1831. HDMI_MC_CLKDIS_TMDSCLK_DISABLE;
  1832. hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_PIXELCLK_DISABLE;
  1833. hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);
  1834. hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_TMDSCLK_DISABLE;
  1835. hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);
  1836. /* Enable csc path */
  1837. if (is_csc_needed(hdmi)) {
  1838. hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_CSCCLK_DISABLE;
  1839. hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);
  1840. hdmi_writeb(hdmi, HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_IN_PATH,
  1841. HDMI_MC_FLOWCTRL);
  1842. } else {
  1843. hdmi->mc_clkdis |= HDMI_MC_CLKDIS_CSCCLK_DISABLE;
  1844. hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);
  1845. hdmi_writeb(hdmi, HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_BYPASS,
  1846. HDMI_MC_FLOWCTRL);
  1847. }
  1848. }
  1849. /* Workaround to clear the overflow condition */
  1850. static void dw_hdmi_clear_overflow(struct dw_hdmi *hdmi)
  1851. {
  1852. unsigned int count;
  1853. unsigned int i;
  1854. u8 val;
  1855. /*
  1856. * Under some circumstances the Frame Composer arithmetic unit can miss
  1857. * an FC register write due to being busy processing the previous one.
  1858. * The issue can be worked around by issuing a TMDS software reset and
  1859. * then write one of the FC registers several times.
  1860. *
  1861. * The number of iterations matters and depends on the HDMI TX revision
  1862. * (and possibly on the platform).
  1863. * 4 iterations for i.MX6Q(v1.30a) and 1 iteration for others.
  1864. * i.MX6DL (v1.31a), Allwinner SoCs (v1.32a), Rockchip RK3288 SoC (v2.00a),
  1865. * Amlogic Meson GX SoCs (v2.01a), RK3328/RK3399 SoCs (v2.11a)
  1866. * and i.MX8MPlus (v2.13a) have been identified as needing the workaround
  1867. * with a single iteration.
  1868. */
  1869. switch (hdmi->version) {
  1870. case 0x130a:
  1871. count = 4;
  1872. break;
  1873. default:
  1874. count = 1;
  1875. break;
  1876. }
  1877. /* TMDS software reset */
  1878. hdmi_writeb(hdmi, (u8)~HDMI_MC_SWRSTZ_TMDSSWRST_REQ, HDMI_MC_SWRSTZ);
  1879. val = hdmi_readb(hdmi, HDMI_FC_INVIDCONF);
  1880. for (i = 0; i < count; i++)
  1881. hdmi_writeb(hdmi, val, HDMI_FC_INVIDCONF);
  1882. }
  1883. static void hdmi_disable_overflow_interrupts(struct dw_hdmi *hdmi)
  1884. {
  1885. hdmi_writeb(hdmi, HDMI_IH_MUTE_FC_STAT2_OVERFLOW_MASK,
  1886. HDMI_IH_MUTE_FC_STAT2);
  1887. }
  1888. static int dw_hdmi_setup(struct dw_hdmi *hdmi,
  1889. const struct drm_connector *connector,
  1890. const struct drm_display_mode *mode)
  1891. {
  1892. int ret;
  1893. hdmi_disable_overflow_interrupts(hdmi);
  1894. hdmi->vic = drm_match_cea_mode(mode);
  1895. if (!hdmi->vic) {
  1896. dev_dbg(hdmi->dev, "Non-CEA mode used in HDMI\n");
  1897. } else {
  1898. dev_dbg(hdmi->dev, "CEA mode used vic=%d\n", hdmi->vic);
  1899. }
  1900. if ((hdmi->vic == 6) || (hdmi->vic == 7) ||
  1901. (hdmi->vic == 21) || (hdmi->vic == 22) ||
  1902. (hdmi->vic == 2) || (hdmi->vic == 3) ||
  1903. (hdmi->vic == 17) || (hdmi->vic == 18))
  1904. hdmi->hdmi_data.enc_out_encoding = V4L2_YCBCR_ENC_601;
  1905. else
  1906. hdmi->hdmi_data.enc_out_encoding = V4L2_YCBCR_ENC_709;
  1907. hdmi->hdmi_data.video_mode.mpixelrepetitionoutput = 0;
  1908. hdmi->hdmi_data.video_mode.mpixelrepetitioninput = 0;
  1909. if (hdmi->hdmi_data.enc_in_bus_format == MEDIA_BUS_FMT_FIXED)
  1910. hdmi->hdmi_data.enc_in_bus_format = MEDIA_BUS_FMT_RGB888_1X24;
  1911. /* TOFIX: Get input encoding from plat data or fallback to none */
  1912. if (hdmi->plat_data->input_bus_encoding)
  1913. hdmi->hdmi_data.enc_in_encoding =
  1914. hdmi->plat_data->input_bus_encoding;
  1915. else
  1916. hdmi->hdmi_data.enc_in_encoding = V4L2_YCBCR_ENC_DEFAULT;
  1917. if (hdmi->hdmi_data.enc_out_bus_format == MEDIA_BUS_FMT_FIXED)
  1918. hdmi->hdmi_data.enc_out_bus_format = MEDIA_BUS_FMT_RGB888_1X24;
  1919. hdmi->hdmi_data.rgb_limited_range = hdmi->sink_is_hdmi &&
  1920. drm_default_rgb_quant_range(mode) ==
  1921. HDMI_QUANTIZATION_RANGE_LIMITED;
  1922. hdmi->hdmi_data.pix_repet_factor = 0;
  1923. hdmi->hdmi_data.hdcp_enable = 0;
  1924. hdmi->hdmi_data.video_mode.mdataenablepolarity = true;
  1925. /* HDMI Initialization Step B.1 */
  1926. hdmi_av_composer(hdmi, &connector->display_info, mode);
  1927. /* HDMI Initializateion Step B.2 */
  1928. ret = hdmi->phy.ops->init(hdmi, hdmi->phy.data,
  1929. &connector->display_info,
  1930. &hdmi->previous_mode);
  1931. if (ret)
  1932. return ret;
  1933. hdmi->phy.enabled = true;
  1934. /* HDMI Initialization Step B.3 */
  1935. dw_hdmi_enable_video_path(hdmi);
  1936. if (hdmi->sink_has_audio) {
  1937. dev_dbg(hdmi->dev, "sink has audio support\n");
  1938. /* HDMI Initialization Step E - Configure audio */
  1939. hdmi_clk_regenerator_update_pixel_clock(hdmi);
  1940. hdmi_enable_audio_clk(hdmi, hdmi->audio_enable);
  1941. }
  1942. /* not for DVI mode */
  1943. if (hdmi->sink_is_hdmi) {
  1944. dev_dbg(hdmi->dev, "%s HDMI mode\n", __func__);
  1945. /* HDMI Initialization Step F - Configure AVI InfoFrame */
  1946. hdmi_config_AVI(hdmi, connector, mode);
  1947. hdmi_config_vendor_specific_infoframe(hdmi, connector, mode);
  1948. hdmi_config_drm_infoframe(hdmi, connector);
  1949. } else {
  1950. dev_dbg(hdmi->dev, "%s DVI mode\n", __func__);
  1951. }
  1952. hdmi_video_packetize(hdmi);
  1953. hdmi_video_csc(hdmi);
  1954. hdmi_video_sample(hdmi);
  1955. hdmi_tx_hdcp_config(hdmi);
  1956. dw_hdmi_clear_overflow(hdmi);
  1957. return 0;
  1958. }
  1959. static void initialize_hdmi_ih_mutes(struct dw_hdmi *hdmi)
  1960. {
  1961. u8 ih_mute;
  1962. /*
  1963. * Boot up defaults are:
  1964. * HDMI_IH_MUTE = 0x03 (disabled)
  1965. * HDMI_IH_MUTE_* = 0x00 (enabled)
  1966. *
  1967. * Disable top level interrupt bits in HDMI block
  1968. */
  1969. ih_mute = hdmi_readb(hdmi, HDMI_IH_MUTE) |
  1970. HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |
  1971. HDMI_IH_MUTE_MUTE_ALL_INTERRUPT;
  1972. hdmi_writeb(hdmi, ih_mute, HDMI_IH_MUTE);
  1973. /* by default mask all interrupts */
  1974. hdmi_writeb(hdmi, 0xff, HDMI_VP_MASK);
  1975. hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK0);
  1976. hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK1);
  1977. hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK2);
  1978. hdmi_writeb(hdmi, 0xff, HDMI_PHY_MASK0);
  1979. hdmi_writeb(hdmi, 0xff, HDMI_PHY_I2CM_INT_ADDR);
  1980. hdmi_writeb(hdmi, 0xff, HDMI_PHY_I2CM_CTLINT_ADDR);
  1981. hdmi_writeb(hdmi, 0xff, HDMI_AUD_INT);
  1982. hdmi_writeb(hdmi, 0xff, HDMI_AUD_SPDIFINT);
  1983. hdmi_writeb(hdmi, 0xff, HDMI_AUD_HBR_MASK);
  1984. hdmi_writeb(hdmi, 0xff, HDMI_GP_MASK);
  1985. hdmi_writeb(hdmi, 0xff, HDMI_A_APIINTMSK);
  1986. hdmi_writeb(hdmi, 0xff, HDMI_I2CM_INT);
  1987. hdmi_writeb(hdmi, 0xff, HDMI_I2CM_CTLINT);
  1988. /* Disable interrupts in the IH_MUTE_* registers */
  1989. hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT0);
  1990. hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT1);
  1991. hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT2);
  1992. hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_AS_STAT0);
  1993. hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_PHY_STAT0);
  1994. hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_I2CM_STAT0);
  1995. hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_CEC_STAT0);
  1996. hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_VP_STAT0);
  1997. hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_I2CMPHY_STAT0);
  1998. hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_AHBDMAAUD_STAT0);
  1999. /* Enable top level interrupt bits in HDMI block */
  2000. ih_mute &= ~(HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |
  2001. HDMI_IH_MUTE_MUTE_ALL_INTERRUPT);
  2002. hdmi_writeb(hdmi, ih_mute, HDMI_IH_MUTE);
  2003. }
  2004. static void dw_hdmi_poweron(struct dw_hdmi *hdmi)
  2005. {
  2006. hdmi->bridge_is_on = true;
  2007. /*
  2008. * The curr_conn field is guaranteed to be valid here, as this function
  2009. * is only be called when !hdmi->disabled.
  2010. */
  2011. dw_hdmi_setup(hdmi, hdmi->curr_conn, &hdmi->previous_mode);
  2012. }
  2013. static void dw_hdmi_poweroff(struct dw_hdmi *hdmi)
  2014. {
  2015. if (hdmi->phy.enabled) {
  2016. hdmi->phy.ops->disable(hdmi, hdmi->phy.data);
  2017. hdmi->phy.enabled = false;
  2018. }
  2019. hdmi->bridge_is_on = false;
  2020. }
  2021. static void dw_hdmi_update_power(struct dw_hdmi *hdmi)
  2022. {
  2023. int force = hdmi->force;
  2024. if (hdmi->disabled) {
  2025. force = DRM_FORCE_OFF;
  2026. } else if (force == DRM_FORCE_UNSPECIFIED) {
  2027. if (hdmi->rxsense)
  2028. force = DRM_FORCE_ON;
  2029. else
  2030. force = DRM_FORCE_OFF;
  2031. }
  2032. if (force == DRM_FORCE_OFF) {
  2033. if (hdmi->bridge_is_on)
  2034. dw_hdmi_poweroff(hdmi);
  2035. } else {
  2036. if (!hdmi->bridge_is_on)
  2037. dw_hdmi_poweron(hdmi);
  2038. }
  2039. }
  2040. /*
  2041. * Adjust the detection of RXSENSE according to whether we have a forced
  2042. * connection mode enabled, or whether we have been disabled. There is
  2043. * no point processing RXSENSE interrupts if we have a forced connection
  2044. * state, or DRM has us disabled.
  2045. *
  2046. * We also disable rxsense interrupts when we think we're disconnected
  2047. * to avoid floating TDMS signals giving false rxsense interrupts.
  2048. *
  2049. * Note: we still need to listen for HPD interrupts even when DRM has us
  2050. * disabled so that we can detect a connect event.
  2051. */
  2052. static void dw_hdmi_update_phy_mask(struct dw_hdmi *hdmi)
  2053. {
  2054. if (hdmi->phy.ops->update_hpd)
  2055. hdmi->phy.ops->update_hpd(hdmi, hdmi->phy.data,
  2056. hdmi->force, hdmi->disabled,
  2057. hdmi->rxsense);
  2058. }
  2059. static enum drm_connector_status dw_hdmi_detect(struct dw_hdmi *hdmi)
  2060. {
  2061. enum drm_connector_status result;
  2062. result = hdmi->phy.ops->read_hpd(hdmi, hdmi->phy.data);
  2063. mutex_lock(&hdmi->mutex);
  2064. if (result != hdmi->last_connector_result) {
  2065. dev_dbg(hdmi->dev, "read_hpd result: %d", result);
  2066. handle_plugged_change(hdmi,
  2067. result == connector_status_connected);
  2068. hdmi->last_connector_result = result;
  2069. }
  2070. mutex_unlock(&hdmi->mutex);
  2071. return result;
  2072. }
  2073. static struct edid *dw_hdmi_get_edid(struct dw_hdmi *hdmi,
  2074. struct drm_connector *connector)
  2075. {
  2076. struct edid *edid;
  2077. if (!hdmi->ddc)
  2078. return NULL;
  2079. edid = drm_get_edid(connector, hdmi->ddc);
  2080. if (!edid) {
  2081. dev_dbg(hdmi->dev, "failed to get edid\n");
  2082. return NULL;
  2083. }
  2084. dev_dbg(hdmi->dev, "got edid: width[%d] x height[%d]\n",
  2085. edid->width_cm, edid->height_cm);
  2086. hdmi->sink_is_hdmi = drm_detect_hdmi_monitor(edid);
  2087. hdmi->sink_has_audio = drm_detect_monitor_audio(edid);
  2088. return edid;
  2089. }
  2090. /* -----------------------------------------------------------------------------
  2091. * DRM Connector Operations
  2092. */
  2093. static enum drm_connector_status
  2094. dw_hdmi_connector_detect(struct drm_connector *connector, bool force)
  2095. {
  2096. struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
  2097. connector);
  2098. return dw_hdmi_detect(hdmi);
  2099. }
  2100. static int dw_hdmi_connector_get_modes(struct drm_connector *connector)
  2101. {
  2102. struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
  2103. connector);
  2104. struct edid *edid;
  2105. int ret;
  2106. edid = dw_hdmi_get_edid(hdmi, connector);
  2107. if (!edid)
  2108. return 0;
  2109. drm_connector_update_edid_property(connector, edid);
  2110. cec_notifier_set_phys_addr_from_edid(hdmi->cec_notifier, edid);
  2111. ret = drm_add_edid_modes(connector, edid);
  2112. kfree(edid);
  2113. return ret;
  2114. }
  2115. static int dw_hdmi_connector_atomic_check(struct drm_connector *connector,
  2116. struct drm_atomic_state *state)
  2117. {
  2118. struct drm_connector_state *old_state =
  2119. drm_atomic_get_old_connector_state(state, connector);
  2120. struct drm_connector_state *new_state =
  2121. drm_atomic_get_new_connector_state(state, connector);
  2122. struct drm_crtc *crtc = new_state->crtc;
  2123. struct drm_crtc_state *crtc_state;
  2124. if (!crtc)
  2125. return 0;
  2126. if (!drm_connector_atomic_hdr_metadata_equal(old_state, new_state)) {
  2127. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  2128. if (IS_ERR(crtc_state))
  2129. return PTR_ERR(crtc_state);
  2130. crtc_state->mode_changed = true;
  2131. }
  2132. return 0;
  2133. }
  2134. static void dw_hdmi_connector_force(struct drm_connector *connector)
  2135. {
  2136. struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
  2137. connector);
  2138. mutex_lock(&hdmi->mutex);
  2139. hdmi->force = connector->force;
  2140. dw_hdmi_update_power(hdmi);
  2141. dw_hdmi_update_phy_mask(hdmi);
  2142. mutex_unlock(&hdmi->mutex);
  2143. }
  2144. static const struct drm_connector_funcs dw_hdmi_connector_funcs = {
  2145. .fill_modes = drm_helper_probe_single_connector_modes,
  2146. .detect = dw_hdmi_connector_detect,
  2147. .destroy = drm_connector_cleanup,
  2148. .force = dw_hdmi_connector_force,
  2149. .reset = drm_atomic_helper_connector_reset,
  2150. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  2151. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  2152. };
  2153. static const struct drm_connector_helper_funcs dw_hdmi_connector_helper_funcs = {
  2154. .get_modes = dw_hdmi_connector_get_modes,
  2155. .atomic_check = dw_hdmi_connector_atomic_check,
  2156. };
  2157. static int dw_hdmi_connector_create(struct dw_hdmi *hdmi)
  2158. {
  2159. struct drm_connector *connector = &hdmi->connector;
  2160. struct cec_connector_info conn_info;
  2161. struct cec_notifier *notifier;
  2162. if (hdmi->version >= 0x200a)
  2163. connector->ycbcr_420_allowed =
  2164. hdmi->plat_data->ycbcr_420_allowed;
  2165. else
  2166. connector->ycbcr_420_allowed = false;
  2167. connector->interlace_allowed = 1;
  2168. connector->polled = DRM_CONNECTOR_POLL_HPD;
  2169. drm_connector_helper_add(connector, &dw_hdmi_connector_helper_funcs);
  2170. drm_connector_init_with_ddc(hdmi->bridge.dev, connector,
  2171. &dw_hdmi_connector_funcs,
  2172. DRM_MODE_CONNECTOR_HDMIA,
  2173. hdmi->ddc);
  2174. /*
  2175. * drm_connector_attach_max_bpc_property() requires the
  2176. * connector to have a state.
  2177. */
  2178. drm_atomic_helper_connector_reset(connector);
  2179. drm_connector_attach_max_bpc_property(connector, 8, 16);
  2180. if (hdmi->version >= 0x200a && hdmi->plat_data->use_drm_infoframe)
  2181. drm_connector_attach_hdr_output_metadata_property(connector);
  2182. drm_connector_attach_encoder(connector, hdmi->bridge.encoder);
  2183. cec_fill_conn_info_from_drm(&conn_info, connector);
  2184. notifier = cec_notifier_conn_register(hdmi->dev, NULL, &conn_info);
  2185. if (!notifier)
  2186. return -ENOMEM;
  2187. mutex_lock(&hdmi->cec_notifier_mutex);
  2188. hdmi->cec_notifier = notifier;
  2189. mutex_unlock(&hdmi->cec_notifier_mutex);
  2190. return 0;
  2191. }
  2192. /* -----------------------------------------------------------------------------
  2193. * DRM Bridge Operations
  2194. */
  2195. /*
  2196. * Possible output formats :
  2197. * - MEDIA_BUS_FMT_UYYVYY16_0_5X48,
  2198. * - MEDIA_BUS_FMT_UYYVYY12_0_5X36,
  2199. * - MEDIA_BUS_FMT_UYYVYY10_0_5X30,
  2200. * - MEDIA_BUS_FMT_UYYVYY8_0_5X24,
  2201. * - MEDIA_BUS_FMT_YUV16_1X48,
  2202. * - MEDIA_BUS_FMT_RGB161616_1X48,
  2203. * - MEDIA_BUS_FMT_UYVY12_1X24,
  2204. * - MEDIA_BUS_FMT_YUV12_1X36,
  2205. * - MEDIA_BUS_FMT_RGB121212_1X36,
  2206. * - MEDIA_BUS_FMT_UYVY10_1X20,
  2207. * - MEDIA_BUS_FMT_YUV10_1X30,
  2208. * - MEDIA_BUS_FMT_RGB101010_1X30,
  2209. * - MEDIA_BUS_FMT_UYVY8_1X16,
  2210. * - MEDIA_BUS_FMT_YUV8_1X24,
  2211. * - MEDIA_BUS_FMT_RGB888_1X24,
  2212. */
  2213. /* Can return a maximum of 11 possible output formats for a mode/connector */
  2214. #define MAX_OUTPUT_SEL_FORMATS 11
  2215. static u32 *dw_hdmi_bridge_atomic_get_output_bus_fmts(struct drm_bridge *bridge,
  2216. struct drm_bridge_state *bridge_state,
  2217. struct drm_crtc_state *crtc_state,
  2218. struct drm_connector_state *conn_state,
  2219. unsigned int *num_output_fmts)
  2220. {
  2221. struct drm_connector *conn = conn_state->connector;
  2222. struct drm_display_info *info = &conn->display_info;
  2223. struct drm_display_mode *mode = &crtc_state->mode;
  2224. u8 max_bpc = conn_state->max_requested_bpc;
  2225. bool is_hdmi2_sink = info->hdmi.scdc.supported ||
  2226. (info->color_formats & DRM_COLOR_FORMAT_YCBCR420);
  2227. u32 *output_fmts;
  2228. unsigned int i = 0;
  2229. *num_output_fmts = 0;
  2230. output_fmts = kcalloc(MAX_OUTPUT_SEL_FORMATS, sizeof(*output_fmts),
  2231. GFP_KERNEL);
  2232. if (!output_fmts)
  2233. return NULL;
  2234. /* If dw-hdmi is the first or only bridge, avoid negociating with ourselves */
  2235. if (list_is_singular(&bridge->encoder->bridge_chain) ||
  2236. list_is_first(&bridge->chain_node, &bridge->encoder->bridge_chain)) {
  2237. *num_output_fmts = 1;
  2238. output_fmts[0] = MEDIA_BUS_FMT_FIXED;
  2239. return output_fmts;
  2240. }
  2241. /*
  2242. * If the current mode enforces 4:2:0, force the output but format
  2243. * to 4:2:0 and do not add the YUV422/444/RGB formats
  2244. */
  2245. if (conn->ycbcr_420_allowed &&
  2246. (drm_mode_is_420_only(info, mode) ||
  2247. (is_hdmi2_sink && drm_mode_is_420_also(info, mode)))) {
  2248. /* Order bus formats from 16bit to 8bit if supported */
  2249. if (max_bpc >= 16 && info->bpc == 16 &&
  2250. (info->hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_48))
  2251. output_fmts[i++] = MEDIA_BUS_FMT_UYYVYY16_0_5X48;
  2252. if (max_bpc >= 12 && info->bpc >= 12 &&
  2253. (info->hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_36))
  2254. output_fmts[i++] = MEDIA_BUS_FMT_UYYVYY12_0_5X36;
  2255. if (max_bpc >= 10 && info->bpc >= 10 &&
  2256. (info->hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_30))
  2257. output_fmts[i++] = MEDIA_BUS_FMT_UYYVYY10_0_5X30;
  2258. /* Default 8bit fallback */
  2259. output_fmts[i++] = MEDIA_BUS_FMT_UYYVYY8_0_5X24;
  2260. *num_output_fmts = i;
  2261. return output_fmts;
  2262. }
  2263. /*
  2264. * Order bus formats from 16bit to 8bit and from YUV422 to RGB
  2265. * if supported. In any case the default RGB888 format is added
  2266. */
  2267. /* Default 8bit RGB fallback */
  2268. output_fmts[i++] = MEDIA_BUS_FMT_RGB888_1X24;
  2269. if (max_bpc >= 16 && info->bpc == 16) {
  2270. if (info->color_formats & DRM_COLOR_FORMAT_YCBCR444)
  2271. output_fmts[i++] = MEDIA_BUS_FMT_YUV16_1X48;
  2272. output_fmts[i++] = MEDIA_BUS_FMT_RGB161616_1X48;
  2273. }
  2274. if (max_bpc >= 12 && info->bpc >= 12) {
  2275. if (info->color_formats & DRM_COLOR_FORMAT_YCBCR422)
  2276. output_fmts[i++] = MEDIA_BUS_FMT_UYVY12_1X24;
  2277. if (info->color_formats & DRM_COLOR_FORMAT_YCBCR444)
  2278. output_fmts[i++] = MEDIA_BUS_FMT_YUV12_1X36;
  2279. output_fmts[i++] = MEDIA_BUS_FMT_RGB121212_1X36;
  2280. }
  2281. if (max_bpc >= 10 && info->bpc >= 10) {
  2282. if (info->color_formats & DRM_COLOR_FORMAT_YCBCR422)
  2283. output_fmts[i++] = MEDIA_BUS_FMT_UYVY10_1X20;
  2284. if (info->color_formats & DRM_COLOR_FORMAT_YCBCR444)
  2285. output_fmts[i++] = MEDIA_BUS_FMT_YUV10_1X30;
  2286. output_fmts[i++] = MEDIA_BUS_FMT_RGB101010_1X30;
  2287. }
  2288. if (info->color_formats & DRM_COLOR_FORMAT_YCBCR422)
  2289. output_fmts[i++] = MEDIA_BUS_FMT_UYVY8_1X16;
  2290. if (info->color_formats & DRM_COLOR_FORMAT_YCBCR444)
  2291. output_fmts[i++] = MEDIA_BUS_FMT_YUV8_1X24;
  2292. *num_output_fmts = i;
  2293. return output_fmts;
  2294. }
  2295. /*
  2296. * Possible input formats :
  2297. * - MEDIA_BUS_FMT_RGB888_1X24
  2298. * - MEDIA_BUS_FMT_YUV8_1X24
  2299. * - MEDIA_BUS_FMT_UYVY8_1X16
  2300. * - MEDIA_BUS_FMT_UYYVYY8_0_5X24
  2301. * - MEDIA_BUS_FMT_RGB101010_1X30
  2302. * - MEDIA_BUS_FMT_YUV10_1X30
  2303. * - MEDIA_BUS_FMT_UYVY10_1X20
  2304. * - MEDIA_BUS_FMT_UYYVYY10_0_5X30
  2305. * - MEDIA_BUS_FMT_RGB121212_1X36
  2306. * - MEDIA_BUS_FMT_YUV12_1X36
  2307. * - MEDIA_BUS_FMT_UYVY12_1X24
  2308. * - MEDIA_BUS_FMT_UYYVYY12_0_5X36
  2309. * - MEDIA_BUS_FMT_RGB161616_1X48
  2310. * - MEDIA_BUS_FMT_YUV16_1X48
  2311. * - MEDIA_BUS_FMT_UYYVYY16_0_5X48
  2312. */
  2313. /* Can return a maximum of 3 possible input formats for an output format */
  2314. #define MAX_INPUT_SEL_FORMATS 3
  2315. static u32 *dw_hdmi_bridge_atomic_get_input_bus_fmts(struct drm_bridge *bridge,
  2316. struct drm_bridge_state *bridge_state,
  2317. struct drm_crtc_state *crtc_state,
  2318. struct drm_connector_state *conn_state,
  2319. u32 output_fmt,
  2320. unsigned int *num_input_fmts)
  2321. {
  2322. u32 *input_fmts;
  2323. unsigned int i = 0;
  2324. *num_input_fmts = 0;
  2325. input_fmts = kcalloc(MAX_INPUT_SEL_FORMATS, sizeof(*input_fmts),
  2326. GFP_KERNEL);
  2327. if (!input_fmts)
  2328. return NULL;
  2329. switch (output_fmt) {
  2330. /* If MEDIA_BUS_FMT_FIXED is tested, return default bus format */
  2331. case MEDIA_BUS_FMT_FIXED:
  2332. input_fmts[i++] = MEDIA_BUS_FMT_RGB888_1X24;
  2333. break;
  2334. /* 8bit */
  2335. case MEDIA_BUS_FMT_RGB888_1X24:
  2336. input_fmts[i++] = MEDIA_BUS_FMT_RGB888_1X24;
  2337. input_fmts[i++] = MEDIA_BUS_FMT_YUV8_1X24;
  2338. input_fmts[i++] = MEDIA_BUS_FMT_UYVY8_1X16;
  2339. break;
  2340. case MEDIA_BUS_FMT_YUV8_1X24:
  2341. input_fmts[i++] = MEDIA_BUS_FMT_YUV8_1X24;
  2342. input_fmts[i++] = MEDIA_BUS_FMT_UYVY8_1X16;
  2343. input_fmts[i++] = MEDIA_BUS_FMT_RGB888_1X24;
  2344. break;
  2345. case MEDIA_BUS_FMT_UYVY8_1X16:
  2346. input_fmts[i++] = MEDIA_BUS_FMT_UYVY8_1X16;
  2347. input_fmts[i++] = MEDIA_BUS_FMT_YUV8_1X24;
  2348. input_fmts[i++] = MEDIA_BUS_FMT_RGB888_1X24;
  2349. break;
  2350. /* 10bit */
  2351. case MEDIA_BUS_FMT_RGB101010_1X30:
  2352. input_fmts[i++] = MEDIA_BUS_FMT_RGB101010_1X30;
  2353. input_fmts[i++] = MEDIA_BUS_FMT_YUV10_1X30;
  2354. input_fmts[i++] = MEDIA_BUS_FMT_UYVY10_1X20;
  2355. break;
  2356. case MEDIA_BUS_FMT_YUV10_1X30:
  2357. input_fmts[i++] = MEDIA_BUS_FMT_YUV10_1X30;
  2358. input_fmts[i++] = MEDIA_BUS_FMT_UYVY10_1X20;
  2359. input_fmts[i++] = MEDIA_BUS_FMT_RGB101010_1X30;
  2360. break;
  2361. case MEDIA_BUS_FMT_UYVY10_1X20:
  2362. input_fmts[i++] = MEDIA_BUS_FMT_UYVY10_1X20;
  2363. input_fmts[i++] = MEDIA_BUS_FMT_YUV10_1X30;
  2364. input_fmts[i++] = MEDIA_BUS_FMT_RGB101010_1X30;
  2365. break;
  2366. /* 12bit */
  2367. case MEDIA_BUS_FMT_RGB121212_1X36:
  2368. input_fmts[i++] = MEDIA_BUS_FMT_RGB121212_1X36;
  2369. input_fmts[i++] = MEDIA_BUS_FMT_YUV12_1X36;
  2370. input_fmts[i++] = MEDIA_BUS_FMT_UYVY12_1X24;
  2371. break;
  2372. case MEDIA_BUS_FMT_YUV12_1X36:
  2373. input_fmts[i++] = MEDIA_BUS_FMT_YUV12_1X36;
  2374. input_fmts[i++] = MEDIA_BUS_FMT_UYVY12_1X24;
  2375. input_fmts[i++] = MEDIA_BUS_FMT_RGB121212_1X36;
  2376. break;
  2377. case MEDIA_BUS_FMT_UYVY12_1X24:
  2378. input_fmts[i++] = MEDIA_BUS_FMT_UYVY12_1X24;
  2379. input_fmts[i++] = MEDIA_BUS_FMT_YUV12_1X36;
  2380. input_fmts[i++] = MEDIA_BUS_FMT_RGB121212_1X36;
  2381. break;
  2382. /* 16bit */
  2383. case MEDIA_BUS_FMT_RGB161616_1X48:
  2384. input_fmts[i++] = MEDIA_BUS_FMT_RGB161616_1X48;
  2385. input_fmts[i++] = MEDIA_BUS_FMT_YUV16_1X48;
  2386. break;
  2387. case MEDIA_BUS_FMT_YUV16_1X48:
  2388. input_fmts[i++] = MEDIA_BUS_FMT_YUV16_1X48;
  2389. input_fmts[i++] = MEDIA_BUS_FMT_RGB161616_1X48;
  2390. break;
  2391. /*YUV 4:2:0 */
  2392. case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
  2393. case MEDIA_BUS_FMT_UYYVYY10_0_5X30:
  2394. case MEDIA_BUS_FMT_UYYVYY12_0_5X36:
  2395. case MEDIA_BUS_FMT_UYYVYY16_0_5X48:
  2396. input_fmts[i++] = output_fmt;
  2397. break;
  2398. }
  2399. *num_input_fmts = i;
  2400. if (*num_input_fmts == 0) {
  2401. kfree(input_fmts);
  2402. input_fmts = NULL;
  2403. }
  2404. return input_fmts;
  2405. }
  2406. static int dw_hdmi_bridge_atomic_check(struct drm_bridge *bridge,
  2407. struct drm_bridge_state *bridge_state,
  2408. struct drm_crtc_state *crtc_state,
  2409. struct drm_connector_state *conn_state)
  2410. {
  2411. struct dw_hdmi *hdmi = bridge->driver_private;
  2412. hdmi->hdmi_data.enc_out_bus_format =
  2413. bridge_state->output_bus_cfg.format;
  2414. hdmi->hdmi_data.enc_in_bus_format =
  2415. bridge_state->input_bus_cfg.format;
  2416. dev_dbg(hdmi->dev, "input format 0x%04x, output format 0x%04x\n",
  2417. bridge_state->input_bus_cfg.format,
  2418. bridge_state->output_bus_cfg.format);
  2419. return 0;
  2420. }
  2421. static int dw_hdmi_bridge_attach(struct drm_bridge *bridge,
  2422. enum drm_bridge_attach_flags flags)
  2423. {
  2424. struct dw_hdmi *hdmi = bridge->driver_private;
  2425. if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
  2426. return drm_bridge_attach(bridge->encoder, hdmi->next_bridge,
  2427. bridge, flags);
  2428. return dw_hdmi_connector_create(hdmi);
  2429. }
  2430. static void dw_hdmi_bridge_detach(struct drm_bridge *bridge)
  2431. {
  2432. struct dw_hdmi *hdmi = bridge->driver_private;
  2433. mutex_lock(&hdmi->cec_notifier_mutex);
  2434. cec_notifier_conn_unregister(hdmi->cec_notifier);
  2435. hdmi->cec_notifier = NULL;
  2436. mutex_unlock(&hdmi->cec_notifier_mutex);
  2437. }
  2438. static enum drm_mode_status
  2439. dw_hdmi_bridge_mode_valid(struct drm_bridge *bridge,
  2440. const struct drm_display_info *info,
  2441. const struct drm_display_mode *mode)
  2442. {
  2443. struct dw_hdmi *hdmi = bridge->driver_private;
  2444. const struct dw_hdmi_plat_data *pdata = hdmi->plat_data;
  2445. enum drm_mode_status mode_status = MODE_OK;
  2446. /* We don't support double-clocked modes */
  2447. if (mode->flags & DRM_MODE_FLAG_DBLCLK)
  2448. return MODE_BAD;
  2449. if (pdata->mode_valid)
  2450. mode_status = pdata->mode_valid(hdmi, pdata->priv_data, info,
  2451. mode);
  2452. return mode_status;
  2453. }
  2454. static void dw_hdmi_bridge_mode_set(struct drm_bridge *bridge,
  2455. const struct drm_display_mode *orig_mode,
  2456. const struct drm_display_mode *mode)
  2457. {
  2458. struct dw_hdmi *hdmi = bridge->driver_private;
  2459. mutex_lock(&hdmi->mutex);
  2460. /* Store the display mode for plugin/DKMS poweron events */
  2461. drm_mode_copy(&hdmi->previous_mode, mode);
  2462. mutex_unlock(&hdmi->mutex);
  2463. }
  2464. static void dw_hdmi_bridge_atomic_disable(struct drm_bridge *bridge,
  2465. struct drm_bridge_state *old_state)
  2466. {
  2467. struct dw_hdmi *hdmi = bridge->driver_private;
  2468. mutex_lock(&hdmi->mutex);
  2469. hdmi->disabled = true;
  2470. hdmi->curr_conn = NULL;
  2471. dw_hdmi_update_power(hdmi);
  2472. dw_hdmi_update_phy_mask(hdmi);
  2473. mutex_unlock(&hdmi->mutex);
  2474. }
  2475. static void dw_hdmi_bridge_atomic_enable(struct drm_bridge *bridge,
  2476. struct drm_bridge_state *old_state)
  2477. {
  2478. struct dw_hdmi *hdmi = bridge->driver_private;
  2479. struct drm_atomic_state *state = old_state->base.state;
  2480. struct drm_connector *connector;
  2481. connector = drm_atomic_get_new_connector_for_encoder(state,
  2482. bridge->encoder);
  2483. mutex_lock(&hdmi->mutex);
  2484. hdmi->disabled = false;
  2485. hdmi->curr_conn = connector;
  2486. dw_hdmi_update_power(hdmi);
  2487. dw_hdmi_update_phy_mask(hdmi);
  2488. mutex_unlock(&hdmi->mutex);
  2489. }
  2490. static enum drm_connector_status dw_hdmi_bridge_detect(struct drm_bridge *bridge)
  2491. {
  2492. struct dw_hdmi *hdmi = bridge->driver_private;
  2493. return dw_hdmi_detect(hdmi);
  2494. }
  2495. static struct edid *dw_hdmi_bridge_get_edid(struct drm_bridge *bridge,
  2496. struct drm_connector *connector)
  2497. {
  2498. struct dw_hdmi *hdmi = bridge->driver_private;
  2499. return dw_hdmi_get_edid(hdmi, connector);
  2500. }
  2501. static const struct drm_bridge_funcs dw_hdmi_bridge_funcs = {
  2502. .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
  2503. .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
  2504. .atomic_reset = drm_atomic_helper_bridge_reset,
  2505. .attach = dw_hdmi_bridge_attach,
  2506. .detach = dw_hdmi_bridge_detach,
  2507. .atomic_check = dw_hdmi_bridge_atomic_check,
  2508. .atomic_get_output_bus_fmts = dw_hdmi_bridge_atomic_get_output_bus_fmts,
  2509. .atomic_get_input_bus_fmts = dw_hdmi_bridge_atomic_get_input_bus_fmts,
  2510. .atomic_enable = dw_hdmi_bridge_atomic_enable,
  2511. .atomic_disable = dw_hdmi_bridge_atomic_disable,
  2512. .mode_set = dw_hdmi_bridge_mode_set,
  2513. .mode_valid = dw_hdmi_bridge_mode_valid,
  2514. .detect = dw_hdmi_bridge_detect,
  2515. .get_edid = dw_hdmi_bridge_get_edid,
  2516. };
  2517. /* -----------------------------------------------------------------------------
  2518. * IRQ Handling
  2519. */
  2520. static irqreturn_t dw_hdmi_i2c_irq(struct dw_hdmi *hdmi)
  2521. {
  2522. struct dw_hdmi_i2c *i2c = hdmi->i2c;
  2523. unsigned int stat;
  2524. stat = hdmi_readb(hdmi, HDMI_IH_I2CM_STAT0);
  2525. if (!stat)
  2526. return IRQ_NONE;
  2527. hdmi_writeb(hdmi, stat, HDMI_IH_I2CM_STAT0);
  2528. i2c->stat = stat;
  2529. complete(&i2c->cmp);
  2530. return IRQ_HANDLED;
  2531. }
  2532. static irqreturn_t dw_hdmi_hardirq(int irq, void *dev_id)
  2533. {
  2534. struct dw_hdmi *hdmi = dev_id;
  2535. u8 intr_stat;
  2536. irqreturn_t ret = IRQ_NONE;
  2537. if (hdmi->i2c)
  2538. ret = dw_hdmi_i2c_irq(hdmi);
  2539. intr_stat = hdmi_readb(hdmi, HDMI_IH_PHY_STAT0);
  2540. if (intr_stat) {
  2541. hdmi_writeb(hdmi, ~0, HDMI_IH_MUTE_PHY_STAT0);
  2542. return IRQ_WAKE_THREAD;
  2543. }
  2544. return ret;
  2545. }
  2546. void dw_hdmi_setup_rx_sense(struct dw_hdmi *hdmi, bool hpd, bool rx_sense)
  2547. {
  2548. mutex_lock(&hdmi->mutex);
  2549. if (!hdmi->force) {
  2550. /*
  2551. * If the RX sense status indicates we're disconnected,
  2552. * clear the software rxsense status.
  2553. */
  2554. if (!rx_sense)
  2555. hdmi->rxsense = false;
  2556. /*
  2557. * Only set the software rxsense status when both
  2558. * rxsense and hpd indicates we're connected.
  2559. * This avoids what seems to be bad behaviour in
  2560. * at least iMX6S versions of the phy.
  2561. */
  2562. if (hpd)
  2563. hdmi->rxsense = true;
  2564. dw_hdmi_update_power(hdmi);
  2565. dw_hdmi_update_phy_mask(hdmi);
  2566. }
  2567. mutex_unlock(&hdmi->mutex);
  2568. }
  2569. EXPORT_SYMBOL_GPL(dw_hdmi_setup_rx_sense);
  2570. static irqreturn_t dw_hdmi_irq(int irq, void *dev_id)
  2571. {
  2572. struct dw_hdmi *hdmi = dev_id;
  2573. u8 intr_stat, phy_int_pol, phy_pol_mask, phy_stat;
  2574. enum drm_connector_status status = connector_status_unknown;
  2575. intr_stat = hdmi_readb(hdmi, HDMI_IH_PHY_STAT0);
  2576. phy_int_pol = hdmi_readb(hdmi, HDMI_PHY_POL0);
  2577. phy_stat = hdmi_readb(hdmi, HDMI_PHY_STAT0);
  2578. phy_pol_mask = 0;
  2579. if (intr_stat & HDMI_IH_PHY_STAT0_HPD)
  2580. phy_pol_mask |= HDMI_PHY_HPD;
  2581. if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE0)
  2582. phy_pol_mask |= HDMI_PHY_RX_SENSE0;
  2583. if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE1)
  2584. phy_pol_mask |= HDMI_PHY_RX_SENSE1;
  2585. if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE2)
  2586. phy_pol_mask |= HDMI_PHY_RX_SENSE2;
  2587. if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE3)
  2588. phy_pol_mask |= HDMI_PHY_RX_SENSE3;
  2589. if (phy_pol_mask)
  2590. hdmi_modb(hdmi, ~phy_int_pol, phy_pol_mask, HDMI_PHY_POL0);
  2591. /*
  2592. * RX sense tells us whether the TDMS transmitters are detecting
  2593. * load - in other words, there's something listening on the
  2594. * other end of the link. Use this to decide whether we should
  2595. * power on the phy as HPD may be toggled by the sink to merely
  2596. * ask the source to re-read the EDID.
  2597. */
  2598. if (intr_stat &
  2599. (HDMI_IH_PHY_STAT0_RX_SENSE | HDMI_IH_PHY_STAT0_HPD)) {
  2600. dw_hdmi_setup_rx_sense(hdmi,
  2601. phy_stat & HDMI_PHY_HPD,
  2602. phy_stat & HDMI_PHY_RX_SENSE);
  2603. if ((phy_stat & (HDMI_PHY_RX_SENSE | HDMI_PHY_HPD)) == 0) {
  2604. mutex_lock(&hdmi->cec_notifier_mutex);
  2605. cec_notifier_phys_addr_invalidate(hdmi->cec_notifier);
  2606. mutex_unlock(&hdmi->cec_notifier_mutex);
  2607. }
  2608. if (phy_stat & HDMI_PHY_HPD)
  2609. status = connector_status_connected;
  2610. if (!(phy_stat & (HDMI_PHY_HPD | HDMI_PHY_RX_SENSE)))
  2611. status = connector_status_disconnected;
  2612. }
  2613. if (status != connector_status_unknown) {
  2614. dev_dbg(hdmi->dev, "EVENT=%s\n",
  2615. status == connector_status_connected ?
  2616. "plugin" : "plugout");
  2617. if (hdmi->bridge.dev) {
  2618. drm_helper_hpd_irq_event(hdmi->bridge.dev);
  2619. drm_bridge_hpd_notify(&hdmi->bridge, status);
  2620. }
  2621. }
  2622. hdmi_writeb(hdmi, intr_stat, HDMI_IH_PHY_STAT0);
  2623. hdmi_writeb(hdmi, ~(HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE),
  2624. HDMI_IH_MUTE_PHY_STAT0);
  2625. return IRQ_HANDLED;
  2626. }
  2627. static const struct dw_hdmi_phy_data dw_hdmi_phys[] = {
  2628. {
  2629. .type = DW_HDMI_PHY_DWC_HDMI_TX_PHY,
  2630. .name = "DWC HDMI TX PHY",
  2631. .gen = 1,
  2632. }, {
  2633. .type = DW_HDMI_PHY_DWC_MHL_PHY_HEAC,
  2634. .name = "DWC MHL PHY + HEAC PHY",
  2635. .gen = 2,
  2636. .has_svsret = true,
  2637. .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
  2638. }, {
  2639. .type = DW_HDMI_PHY_DWC_MHL_PHY,
  2640. .name = "DWC MHL PHY",
  2641. .gen = 2,
  2642. .has_svsret = true,
  2643. .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
  2644. }, {
  2645. .type = DW_HDMI_PHY_DWC_HDMI_3D_TX_PHY_HEAC,
  2646. .name = "DWC HDMI 3D TX PHY + HEAC PHY",
  2647. .gen = 2,
  2648. .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
  2649. }, {
  2650. .type = DW_HDMI_PHY_DWC_HDMI_3D_TX_PHY,
  2651. .name = "DWC HDMI 3D TX PHY",
  2652. .gen = 2,
  2653. .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
  2654. }, {
  2655. .type = DW_HDMI_PHY_DWC_HDMI20_TX_PHY,
  2656. .name = "DWC HDMI 2.0 TX PHY",
  2657. .gen = 2,
  2658. .has_svsret = true,
  2659. .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
  2660. }, {
  2661. .type = DW_HDMI_PHY_VENDOR_PHY,
  2662. .name = "Vendor PHY",
  2663. }
  2664. };
  2665. static int dw_hdmi_detect_phy(struct dw_hdmi *hdmi)
  2666. {
  2667. unsigned int i;
  2668. u8 phy_type;
  2669. phy_type = hdmi->plat_data->phy_force_vendor ?
  2670. DW_HDMI_PHY_VENDOR_PHY :
  2671. hdmi_readb(hdmi, HDMI_CONFIG2_ID);
  2672. if (phy_type == DW_HDMI_PHY_VENDOR_PHY) {
  2673. /* Vendor PHYs require support from the glue layer. */
  2674. if (!hdmi->plat_data->phy_ops || !hdmi->plat_data->phy_name) {
  2675. dev_err(hdmi->dev,
  2676. "Vendor HDMI PHY not supported by glue layer\n");
  2677. return -ENODEV;
  2678. }
  2679. hdmi->phy.ops = hdmi->plat_data->phy_ops;
  2680. hdmi->phy.data = hdmi->plat_data->phy_data;
  2681. hdmi->phy.name = hdmi->plat_data->phy_name;
  2682. return 0;
  2683. }
  2684. /* Synopsys PHYs are handled internally. */
  2685. for (i = 0; i < ARRAY_SIZE(dw_hdmi_phys); ++i) {
  2686. if (dw_hdmi_phys[i].type == phy_type) {
  2687. hdmi->phy.ops = &dw_hdmi_synopsys_phy_ops;
  2688. hdmi->phy.name = dw_hdmi_phys[i].name;
  2689. hdmi->phy.data = (void *)&dw_hdmi_phys[i];
  2690. if (!dw_hdmi_phys[i].configure &&
  2691. !hdmi->plat_data->configure_phy) {
  2692. dev_err(hdmi->dev, "%s requires platform support\n",
  2693. hdmi->phy.name);
  2694. return -ENODEV;
  2695. }
  2696. return 0;
  2697. }
  2698. }
  2699. dev_err(hdmi->dev, "Unsupported HDMI PHY type (%02x)\n", phy_type);
  2700. return -ENODEV;
  2701. }
  2702. static void dw_hdmi_cec_enable(struct dw_hdmi *hdmi)
  2703. {
  2704. mutex_lock(&hdmi->mutex);
  2705. hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_CECCLK_DISABLE;
  2706. hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);
  2707. mutex_unlock(&hdmi->mutex);
  2708. }
  2709. static void dw_hdmi_cec_disable(struct dw_hdmi *hdmi)
  2710. {
  2711. mutex_lock(&hdmi->mutex);
  2712. hdmi->mc_clkdis |= HDMI_MC_CLKDIS_CECCLK_DISABLE;
  2713. hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);
  2714. mutex_unlock(&hdmi->mutex);
  2715. }
  2716. static const struct dw_hdmi_cec_ops dw_hdmi_cec_ops = {
  2717. .write = hdmi_writeb,
  2718. .read = hdmi_readb,
  2719. .enable = dw_hdmi_cec_enable,
  2720. .disable = dw_hdmi_cec_disable,
  2721. };
  2722. static const struct regmap_config hdmi_regmap_8bit_config = {
  2723. .reg_bits = 32,
  2724. .val_bits = 8,
  2725. .reg_stride = 1,
  2726. .max_register = HDMI_I2CM_FS_SCL_LCNT_0_ADDR,
  2727. };
  2728. static const struct regmap_config hdmi_regmap_32bit_config = {
  2729. .reg_bits = 32,
  2730. .val_bits = 32,
  2731. .reg_stride = 4,
  2732. .max_register = HDMI_I2CM_FS_SCL_LCNT_0_ADDR << 2,
  2733. };
  2734. static void dw_hdmi_init_hw(struct dw_hdmi *hdmi)
  2735. {
  2736. initialize_hdmi_ih_mutes(hdmi);
  2737. /*
  2738. * Reset HDMI DDC I2C master controller and mute I2CM interrupts.
  2739. * Even if we are using a separate i2c adapter doing this doesn't
  2740. * hurt.
  2741. */
  2742. dw_hdmi_i2c_init(hdmi);
  2743. if (hdmi->phy.ops->setup_hpd)
  2744. hdmi->phy.ops->setup_hpd(hdmi, hdmi->phy.data);
  2745. }
  2746. /* -----------------------------------------------------------------------------
  2747. * Probe/remove API, used from platforms based on the DRM bridge API.
  2748. */
  2749. static int dw_hdmi_parse_dt(struct dw_hdmi *hdmi)
  2750. {
  2751. struct device_node *endpoint;
  2752. struct device_node *remote;
  2753. if (!hdmi->plat_data->output_port)
  2754. return 0;
  2755. endpoint = of_graph_get_endpoint_by_regs(hdmi->dev->of_node,
  2756. hdmi->plat_data->output_port,
  2757. -1);
  2758. if (!endpoint) {
  2759. /*
  2760. * On platforms whose bindings don't make the output port
  2761. * mandatory (such as Rockchip) the plat_data->output_port
  2762. * field isn't set, so it's safe to make this a fatal error.
  2763. */
  2764. dev_err(hdmi->dev, "Missing endpoint in port@%u\n",
  2765. hdmi->plat_data->output_port);
  2766. return -ENODEV;
  2767. }
  2768. remote = of_graph_get_remote_port_parent(endpoint);
  2769. of_node_put(endpoint);
  2770. if (!remote) {
  2771. dev_err(hdmi->dev, "Endpoint in port@%u unconnected\n",
  2772. hdmi->plat_data->output_port);
  2773. return -ENODEV;
  2774. }
  2775. if (!of_device_is_available(remote)) {
  2776. dev_err(hdmi->dev, "port@%u remote device is disabled\n",
  2777. hdmi->plat_data->output_port);
  2778. of_node_put(remote);
  2779. return -ENODEV;
  2780. }
  2781. hdmi->next_bridge = of_drm_find_bridge(remote);
  2782. of_node_put(remote);
  2783. if (!hdmi->next_bridge)
  2784. return -EPROBE_DEFER;
  2785. return 0;
  2786. }
  2787. struct dw_hdmi *dw_hdmi_probe(struct platform_device *pdev,
  2788. const struct dw_hdmi_plat_data *plat_data)
  2789. {
  2790. struct device *dev = &pdev->dev;
  2791. struct device_node *np = dev->of_node;
  2792. struct platform_device_info pdevinfo;
  2793. struct device_node *ddc_node;
  2794. struct dw_hdmi_cec_data cec;
  2795. struct dw_hdmi *hdmi;
  2796. struct resource *iores = NULL;
  2797. int irq;
  2798. int ret;
  2799. u32 val = 1;
  2800. u8 prod_id0;
  2801. u8 prod_id1;
  2802. u8 config0;
  2803. u8 config3;
  2804. hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
  2805. if (!hdmi)
  2806. return ERR_PTR(-ENOMEM);
  2807. hdmi->plat_data = plat_data;
  2808. hdmi->dev = dev;
  2809. hdmi->sample_rate = 48000;
  2810. hdmi->channels = 2;
  2811. hdmi->disabled = true;
  2812. hdmi->rxsense = true;
  2813. hdmi->phy_mask = (u8)~(HDMI_PHY_HPD | HDMI_PHY_RX_SENSE);
  2814. hdmi->mc_clkdis = 0x7f;
  2815. hdmi->last_connector_result = connector_status_disconnected;
  2816. mutex_init(&hdmi->mutex);
  2817. mutex_init(&hdmi->audio_mutex);
  2818. mutex_init(&hdmi->cec_notifier_mutex);
  2819. spin_lock_init(&hdmi->audio_lock);
  2820. ret = dw_hdmi_parse_dt(hdmi);
  2821. if (ret < 0)
  2822. return ERR_PTR(ret);
  2823. ddc_node = of_parse_phandle(np, "ddc-i2c-bus", 0);
  2824. if (ddc_node) {
  2825. hdmi->ddc = of_get_i2c_adapter_by_node(ddc_node);
  2826. of_node_put(ddc_node);
  2827. if (!hdmi->ddc) {
  2828. dev_dbg(hdmi->dev, "failed to read ddc node\n");
  2829. return ERR_PTR(-EPROBE_DEFER);
  2830. }
  2831. } else {
  2832. dev_dbg(hdmi->dev, "no ddc property found\n");
  2833. }
  2834. if (!plat_data->regm) {
  2835. const struct regmap_config *reg_config;
  2836. of_property_read_u32(np, "reg-io-width", &val);
  2837. switch (val) {
  2838. case 4:
  2839. reg_config = &hdmi_regmap_32bit_config;
  2840. hdmi->reg_shift = 2;
  2841. break;
  2842. case 1:
  2843. reg_config = &hdmi_regmap_8bit_config;
  2844. break;
  2845. default:
  2846. dev_err(dev, "reg-io-width must be 1 or 4\n");
  2847. return ERR_PTR(-EINVAL);
  2848. }
  2849. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2850. hdmi->regs = devm_ioremap_resource(dev, iores);
  2851. if (IS_ERR(hdmi->regs)) {
  2852. ret = PTR_ERR(hdmi->regs);
  2853. goto err_res;
  2854. }
  2855. hdmi->regm = devm_regmap_init_mmio(dev, hdmi->regs, reg_config);
  2856. if (IS_ERR(hdmi->regm)) {
  2857. dev_err(dev, "Failed to configure regmap\n");
  2858. ret = PTR_ERR(hdmi->regm);
  2859. goto err_res;
  2860. }
  2861. } else {
  2862. hdmi->regm = plat_data->regm;
  2863. }
  2864. hdmi->isfr_clk = devm_clk_get(hdmi->dev, "isfr");
  2865. if (IS_ERR(hdmi->isfr_clk)) {
  2866. ret = PTR_ERR(hdmi->isfr_clk);
  2867. dev_err(hdmi->dev, "Unable to get HDMI isfr clk: %d\n", ret);
  2868. goto err_res;
  2869. }
  2870. ret = clk_prepare_enable(hdmi->isfr_clk);
  2871. if (ret) {
  2872. dev_err(hdmi->dev, "Cannot enable HDMI isfr clock: %d\n", ret);
  2873. goto err_res;
  2874. }
  2875. hdmi->iahb_clk = devm_clk_get(hdmi->dev, "iahb");
  2876. if (IS_ERR(hdmi->iahb_clk)) {
  2877. ret = PTR_ERR(hdmi->iahb_clk);
  2878. dev_err(hdmi->dev, "Unable to get HDMI iahb clk: %d\n", ret);
  2879. goto err_isfr;
  2880. }
  2881. ret = clk_prepare_enable(hdmi->iahb_clk);
  2882. if (ret) {
  2883. dev_err(hdmi->dev, "Cannot enable HDMI iahb clock: %d\n", ret);
  2884. goto err_isfr;
  2885. }
  2886. hdmi->cec_clk = devm_clk_get(hdmi->dev, "cec");
  2887. if (PTR_ERR(hdmi->cec_clk) == -ENOENT) {
  2888. hdmi->cec_clk = NULL;
  2889. } else if (IS_ERR(hdmi->cec_clk)) {
  2890. ret = PTR_ERR(hdmi->cec_clk);
  2891. if (ret != -EPROBE_DEFER)
  2892. dev_err(hdmi->dev, "Cannot get HDMI cec clock: %d\n",
  2893. ret);
  2894. hdmi->cec_clk = NULL;
  2895. goto err_iahb;
  2896. } else {
  2897. ret = clk_prepare_enable(hdmi->cec_clk);
  2898. if (ret) {
  2899. dev_err(hdmi->dev, "Cannot enable HDMI cec clock: %d\n",
  2900. ret);
  2901. goto err_iahb;
  2902. }
  2903. }
  2904. /* Product and revision IDs */
  2905. hdmi->version = (hdmi_readb(hdmi, HDMI_DESIGN_ID) << 8)
  2906. | (hdmi_readb(hdmi, HDMI_REVISION_ID) << 0);
  2907. prod_id0 = hdmi_readb(hdmi, HDMI_PRODUCT_ID0);
  2908. prod_id1 = hdmi_readb(hdmi, HDMI_PRODUCT_ID1);
  2909. if (prod_id0 != HDMI_PRODUCT_ID0_HDMI_TX ||
  2910. (prod_id1 & ~HDMI_PRODUCT_ID1_HDCP) != HDMI_PRODUCT_ID1_HDMI_TX) {
  2911. dev_err(dev, "Unsupported HDMI controller (%04x:%02x:%02x)\n",
  2912. hdmi->version, prod_id0, prod_id1);
  2913. ret = -ENODEV;
  2914. goto err_iahb;
  2915. }
  2916. ret = dw_hdmi_detect_phy(hdmi);
  2917. if (ret < 0)
  2918. goto err_iahb;
  2919. dev_info(dev, "Detected HDMI TX controller v%x.%03x %s HDCP (%s)\n",
  2920. hdmi->version >> 12, hdmi->version & 0xfff,
  2921. prod_id1 & HDMI_PRODUCT_ID1_HDCP ? "with" : "without",
  2922. hdmi->phy.name);
  2923. dw_hdmi_init_hw(hdmi);
  2924. irq = platform_get_irq(pdev, 0);
  2925. if (irq < 0) {
  2926. ret = irq;
  2927. goto err_iahb;
  2928. }
  2929. ret = devm_request_threaded_irq(dev, irq, dw_hdmi_hardirq,
  2930. dw_hdmi_irq, IRQF_SHARED,
  2931. dev_name(dev), hdmi);
  2932. if (ret)
  2933. goto err_iahb;
  2934. /*
  2935. * To prevent overflows in HDMI_IH_FC_STAT2, set the clk regenerator
  2936. * N and cts values before enabling phy
  2937. */
  2938. hdmi_init_clk_regenerator(hdmi);
  2939. /* If DDC bus is not specified, try to register HDMI I2C bus */
  2940. if (!hdmi->ddc) {
  2941. /* Look for (optional) stuff related to unwedging */
  2942. hdmi->pinctrl = devm_pinctrl_get(dev);
  2943. if (!IS_ERR(hdmi->pinctrl)) {
  2944. hdmi->unwedge_state =
  2945. pinctrl_lookup_state(hdmi->pinctrl, "unwedge");
  2946. hdmi->default_state =
  2947. pinctrl_lookup_state(hdmi->pinctrl, "default");
  2948. if (IS_ERR(hdmi->default_state) ||
  2949. IS_ERR(hdmi->unwedge_state)) {
  2950. if (!IS_ERR(hdmi->unwedge_state))
  2951. dev_warn(dev,
  2952. "Unwedge requires default pinctrl\n");
  2953. hdmi->default_state = NULL;
  2954. hdmi->unwedge_state = NULL;
  2955. }
  2956. }
  2957. hdmi->ddc = dw_hdmi_i2c_adapter(hdmi);
  2958. if (IS_ERR(hdmi->ddc))
  2959. hdmi->ddc = NULL;
  2960. }
  2961. hdmi->bridge.driver_private = hdmi;
  2962. hdmi->bridge.funcs = &dw_hdmi_bridge_funcs;
  2963. hdmi->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID
  2964. | DRM_BRIDGE_OP_HPD;
  2965. hdmi->bridge.interlace_allowed = true;
  2966. #ifdef CONFIG_OF
  2967. hdmi->bridge.of_node = pdev->dev.of_node;
  2968. #endif
  2969. memset(&pdevinfo, 0, sizeof(pdevinfo));
  2970. pdevinfo.parent = dev;
  2971. pdevinfo.id = PLATFORM_DEVID_AUTO;
  2972. config0 = hdmi_readb(hdmi, HDMI_CONFIG0_ID);
  2973. config3 = hdmi_readb(hdmi, HDMI_CONFIG3_ID);
  2974. if (iores && config3 & HDMI_CONFIG3_AHBAUDDMA) {
  2975. struct dw_hdmi_audio_data audio;
  2976. audio.phys = iores->start;
  2977. audio.base = hdmi->regs;
  2978. audio.irq = irq;
  2979. audio.hdmi = hdmi;
  2980. audio.get_eld = hdmi_audio_get_eld;
  2981. hdmi->enable_audio = dw_hdmi_ahb_audio_enable;
  2982. hdmi->disable_audio = dw_hdmi_ahb_audio_disable;
  2983. pdevinfo.name = "dw-hdmi-ahb-audio";
  2984. pdevinfo.data = &audio;
  2985. pdevinfo.size_data = sizeof(audio);
  2986. pdevinfo.dma_mask = DMA_BIT_MASK(32);
  2987. hdmi->audio = platform_device_register_full(&pdevinfo);
  2988. } else if (config0 & HDMI_CONFIG0_I2S) {
  2989. struct dw_hdmi_i2s_audio_data audio;
  2990. audio.hdmi = hdmi;
  2991. audio.get_eld = hdmi_audio_get_eld;
  2992. audio.write = hdmi_writeb;
  2993. audio.read = hdmi_readb;
  2994. hdmi->enable_audio = dw_hdmi_i2s_audio_enable;
  2995. hdmi->disable_audio = dw_hdmi_i2s_audio_disable;
  2996. pdevinfo.name = "dw-hdmi-i2s-audio";
  2997. pdevinfo.data = &audio;
  2998. pdevinfo.size_data = sizeof(audio);
  2999. pdevinfo.dma_mask = DMA_BIT_MASK(32);
  3000. hdmi->audio = platform_device_register_full(&pdevinfo);
  3001. } else if (iores && config3 & HDMI_CONFIG3_GPAUD) {
  3002. struct dw_hdmi_audio_data audio;
  3003. audio.phys = iores->start;
  3004. audio.base = hdmi->regs;
  3005. audio.irq = irq;
  3006. audio.hdmi = hdmi;
  3007. audio.get_eld = hdmi_audio_get_eld;
  3008. hdmi->enable_audio = dw_hdmi_gp_audio_enable;
  3009. hdmi->disable_audio = dw_hdmi_gp_audio_disable;
  3010. pdevinfo.name = "dw-hdmi-gp-audio";
  3011. pdevinfo.id = PLATFORM_DEVID_NONE;
  3012. pdevinfo.data = &audio;
  3013. pdevinfo.size_data = sizeof(audio);
  3014. pdevinfo.dma_mask = DMA_BIT_MASK(32);
  3015. hdmi->audio = platform_device_register_full(&pdevinfo);
  3016. }
  3017. if (!plat_data->disable_cec && (config0 & HDMI_CONFIG0_CEC)) {
  3018. cec.hdmi = hdmi;
  3019. cec.ops = &dw_hdmi_cec_ops;
  3020. cec.irq = irq;
  3021. pdevinfo.name = "dw-hdmi-cec";
  3022. pdevinfo.data = &cec;
  3023. pdevinfo.size_data = sizeof(cec);
  3024. pdevinfo.dma_mask = 0;
  3025. hdmi->cec = platform_device_register_full(&pdevinfo);
  3026. }
  3027. drm_bridge_add(&hdmi->bridge);
  3028. return hdmi;
  3029. err_iahb:
  3030. clk_disable_unprepare(hdmi->iahb_clk);
  3031. clk_disable_unprepare(hdmi->cec_clk);
  3032. err_isfr:
  3033. clk_disable_unprepare(hdmi->isfr_clk);
  3034. err_res:
  3035. i2c_put_adapter(hdmi->ddc);
  3036. return ERR_PTR(ret);
  3037. }
  3038. EXPORT_SYMBOL_GPL(dw_hdmi_probe);
  3039. void dw_hdmi_remove(struct dw_hdmi *hdmi)
  3040. {
  3041. drm_bridge_remove(&hdmi->bridge);
  3042. if (hdmi->audio && !IS_ERR(hdmi->audio))
  3043. platform_device_unregister(hdmi->audio);
  3044. if (!IS_ERR(hdmi->cec))
  3045. platform_device_unregister(hdmi->cec);
  3046. /* Disable all interrupts */
  3047. hdmi_writeb(hdmi, ~0, HDMI_IH_MUTE_PHY_STAT0);
  3048. clk_disable_unprepare(hdmi->iahb_clk);
  3049. clk_disable_unprepare(hdmi->isfr_clk);
  3050. clk_disable_unprepare(hdmi->cec_clk);
  3051. if (hdmi->i2c)
  3052. i2c_del_adapter(&hdmi->i2c->adap);
  3053. else
  3054. i2c_put_adapter(hdmi->ddc);
  3055. }
  3056. EXPORT_SYMBOL_GPL(dw_hdmi_remove);
  3057. /* -----------------------------------------------------------------------------
  3058. * Bind/unbind API, used from platforms based on the component framework.
  3059. */
  3060. struct dw_hdmi *dw_hdmi_bind(struct platform_device *pdev,
  3061. struct drm_encoder *encoder,
  3062. const struct dw_hdmi_plat_data *plat_data)
  3063. {
  3064. struct dw_hdmi *hdmi;
  3065. int ret;
  3066. hdmi = dw_hdmi_probe(pdev, plat_data);
  3067. if (IS_ERR(hdmi))
  3068. return hdmi;
  3069. ret = drm_bridge_attach(encoder, &hdmi->bridge, NULL, 0);
  3070. if (ret) {
  3071. dw_hdmi_remove(hdmi);
  3072. return ERR_PTR(ret);
  3073. }
  3074. return hdmi;
  3075. }
  3076. EXPORT_SYMBOL_GPL(dw_hdmi_bind);
  3077. void dw_hdmi_unbind(struct dw_hdmi *hdmi)
  3078. {
  3079. dw_hdmi_remove(hdmi);
  3080. }
  3081. EXPORT_SYMBOL_GPL(dw_hdmi_unbind);
  3082. void dw_hdmi_resume(struct dw_hdmi *hdmi)
  3083. {
  3084. dw_hdmi_init_hw(hdmi);
  3085. }
  3086. EXPORT_SYMBOL_GPL(dw_hdmi_resume);
  3087. MODULE_AUTHOR("Sascha Hauer <[email protected]>");
  3088. MODULE_AUTHOR("Andy Yan <[email protected]>");
  3089. MODULE_AUTHOR("Yakir Yang <[email protected]>");
  3090. MODULE_AUTHOR("Vladimir Zapolskiy <[email protected]>");
  3091. MODULE_DESCRIPTION("DW HDMI transmitter driver");
  3092. MODULE_LICENSE("GPL");
  3093. MODULE_ALIAS("platform:dw-hdmi");