dw-hdmi.c 100 KB

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