dw-hdmi.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642
  1. /*
  2. * DesignWare High-Definition Multimedia Interface (HDMI) driver
  3. *
  4. * Copyright (C) 2013-2015 Mentor Graphics Inc.
  5. * Copyright (C) 2011-2013 Freescale Semiconductor, Inc.
  6. * Copyright (C) 2010, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. */
  14. #include <linux/module.h>
  15. #include <linux/irq.h>
  16. #include <linux/delay.h>
  17. #include <linux/err.h>
  18. #include <linux/clk.h>
  19. #include <linux/hdmi.h>
  20. #include <linux/mutex.h>
  21. #include <linux/of_device.h>
  22. #include <linux/regmap.h>
  23. #include <linux/spinlock.h>
  24. #include <drm/drm_of.h>
  25. #include <drm/drmP.h>
  26. #include <drm/drm_atomic_helper.h>
  27. #include <drm/drm_crtc_helper.h>
  28. #include <drm/drm_edid.h>
  29. #include <drm/drm_encoder_slave.h>
  30. #include <drm/bridge/dw_hdmi.h>
  31. #include <uapi/linux/media-bus-format.h>
  32. #include <uapi/linux/videodev2.h>
  33. #include "dw-hdmi.h"
  34. #include "dw-hdmi-audio.h"
  35. #include "dw-hdmi-cec.h"
  36. #include <media/cec-notifier.h>
  37. #define DDC_SEGMENT_ADDR 0x30
  38. #define HDMI_EDID_LEN 512
  39. enum hdmi_datamap {
  40. RGB444_8B = 0x01,
  41. RGB444_10B = 0x03,
  42. RGB444_12B = 0x05,
  43. RGB444_16B = 0x07,
  44. YCbCr444_8B = 0x09,
  45. YCbCr444_10B = 0x0B,
  46. YCbCr444_12B = 0x0D,
  47. YCbCr444_16B = 0x0F,
  48. YCbCr422_8B = 0x16,
  49. YCbCr422_10B = 0x14,
  50. YCbCr422_12B = 0x12,
  51. };
  52. static const u16 csc_coeff_default[3][4] = {
  53. { 0x2000, 0x0000, 0x0000, 0x0000 },
  54. { 0x0000, 0x2000, 0x0000, 0x0000 },
  55. { 0x0000, 0x0000, 0x2000, 0x0000 }
  56. };
  57. static const u16 csc_coeff_rgb_out_eitu601[3][4] = {
  58. { 0x2000, 0x6926, 0x74fd, 0x010e },
  59. { 0x2000, 0x2cdd, 0x0000, 0x7e9a },
  60. { 0x2000, 0x0000, 0x38b4, 0x7e3b }
  61. };
  62. static const u16 csc_coeff_rgb_out_eitu709[3][4] = {
  63. { 0x2000, 0x7106, 0x7a02, 0x00a7 },
  64. { 0x2000, 0x3264, 0x0000, 0x7e6d },
  65. { 0x2000, 0x0000, 0x3b61, 0x7e25 }
  66. };
  67. static const u16 csc_coeff_rgb_in_eitu601[3][4] = {
  68. { 0x2591, 0x1322, 0x074b, 0x0000 },
  69. { 0x6535, 0x2000, 0x7acc, 0x0200 },
  70. { 0x6acd, 0x7534, 0x2000, 0x0200 }
  71. };
  72. static const u16 csc_coeff_rgb_in_eitu709[3][4] = {
  73. { 0x2dc5, 0x0d9b, 0x049e, 0x0000 },
  74. { 0x62f0, 0x2000, 0x7d11, 0x0200 },
  75. { 0x6756, 0x78ab, 0x2000, 0x0200 }
  76. };
  77. struct hdmi_vmode {
  78. bool mdataenablepolarity;
  79. unsigned int mpixelclock;
  80. unsigned int mpixelrepetitioninput;
  81. unsigned int mpixelrepetitionoutput;
  82. };
  83. struct hdmi_data_info {
  84. unsigned int enc_in_bus_format;
  85. unsigned int enc_out_bus_format;
  86. unsigned int enc_in_encoding;
  87. unsigned int enc_out_encoding;
  88. unsigned int pix_repet_factor;
  89. unsigned int hdcp_enable;
  90. struct hdmi_vmode video_mode;
  91. };
  92. struct dw_hdmi_i2c {
  93. struct i2c_adapter adap;
  94. struct mutex lock; /* used to serialize data transfers */
  95. struct completion cmp;
  96. u8 stat;
  97. u8 slave_reg;
  98. bool is_regaddr;
  99. bool is_segment;
  100. };
  101. struct dw_hdmi_phy_data {
  102. enum dw_hdmi_phy_type type;
  103. const char *name;
  104. unsigned int gen;
  105. bool has_svsret;
  106. int (*configure)(struct dw_hdmi *hdmi,
  107. const struct dw_hdmi_plat_data *pdata,
  108. unsigned long mpixelclock);
  109. };
  110. struct dw_hdmi {
  111. struct drm_connector connector;
  112. struct drm_bridge bridge;
  113. unsigned int version;
  114. struct platform_device *audio;
  115. struct platform_device *cec;
  116. struct device *dev;
  117. struct clk *isfr_clk;
  118. struct clk *iahb_clk;
  119. struct clk *cec_clk;
  120. struct dw_hdmi_i2c *i2c;
  121. struct hdmi_data_info hdmi_data;
  122. const struct dw_hdmi_plat_data *plat_data;
  123. int vic;
  124. u8 edid[HDMI_EDID_LEN];
  125. struct {
  126. const struct dw_hdmi_phy_ops *ops;
  127. const char *name;
  128. void *data;
  129. bool enabled;
  130. } phy;
  131. struct drm_display_mode previous_mode;
  132. struct i2c_adapter *ddc;
  133. void __iomem *regs;
  134. bool sink_is_hdmi;
  135. bool sink_has_audio;
  136. struct mutex mutex; /* for state below and previous_mode */
  137. enum drm_connector_force force; /* mutex-protected force state */
  138. bool disabled; /* DRM has disabled our bridge */
  139. bool bridge_is_on; /* indicates the bridge is on */
  140. bool rxsense; /* rxsense state */
  141. u8 phy_mask; /* desired phy int mask settings */
  142. u8 mc_clkdis; /* clock disable register */
  143. spinlock_t audio_lock;
  144. struct mutex audio_mutex;
  145. unsigned int sample_rate;
  146. unsigned int audio_cts;
  147. unsigned int audio_n;
  148. bool audio_enable;
  149. unsigned int reg_shift;
  150. struct regmap *regm;
  151. void (*enable_audio)(struct dw_hdmi *hdmi);
  152. void (*disable_audio)(struct dw_hdmi *hdmi);
  153. struct cec_notifier *cec_notifier;
  154. };
  155. #define HDMI_IH_PHY_STAT0_RX_SENSE \
  156. (HDMI_IH_PHY_STAT0_RX_SENSE0 | HDMI_IH_PHY_STAT0_RX_SENSE1 | \
  157. HDMI_IH_PHY_STAT0_RX_SENSE2 | HDMI_IH_PHY_STAT0_RX_SENSE3)
  158. #define HDMI_PHY_RX_SENSE \
  159. (HDMI_PHY_RX_SENSE0 | HDMI_PHY_RX_SENSE1 | \
  160. HDMI_PHY_RX_SENSE2 | HDMI_PHY_RX_SENSE3)
  161. static inline void hdmi_writeb(struct dw_hdmi *hdmi, u8 val, int offset)
  162. {
  163. regmap_write(hdmi->regm, offset << hdmi->reg_shift, val);
  164. }
  165. static inline u8 hdmi_readb(struct dw_hdmi *hdmi, int offset)
  166. {
  167. unsigned int val = 0;
  168. regmap_read(hdmi->regm, offset << hdmi->reg_shift, &val);
  169. return val;
  170. }
  171. static void hdmi_modb(struct dw_hdmi *hdmi, u8 data, u8 mask, unsigned reg)
  172. {
  173. regmap_update_bits(hdmi->regm, reg << hdmi->reg_shift, mask, data);
  174. }
  175. static void hdmi_mask_writeb(struct dw_hdmi *hdmi, u8 data, unsigned int reg,
  176. u8 shift, u8 mask)
  177. {
  178. hdmi_modb(hdmi, data << shift, mask, reg);
  179. }
  180. static void dw_hdmi_i2c_init(struct dw_hdmi *hdmi)
  181. {
  182. /* Software reset */
  183. hdmi_writeb(hdmi, 0x00, HDMI_I2CM_SOFTRSTZ);
  184. /* Set Standard Mode speed (determined to be 100KHz on iMX6) */
  185. hdmi_writeb(hdmi, 0x00, HDMI_I2CM_DIV);
  186. /* Set done, not acknowledged and arbitration interrupt polarities */
  187. hdmi_writeb(hdmi, HDMI_I2CM_INT_DONE_POL, HDMI_I2CM_INT);
  188. hdmi_writeb(hdmi, HDMI_I2CM_CTLINT_NAC_POL | HDMI_I2CM_CTLINT_ARB_POL,
  189. HDMI_I2CM_CTLINT);
  190. /* Clear DONE and ERROR interrupts */
  191. hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,
  192. HDMI_IH_I2CM_STAT0);
  193. /* Mute DONE and ERROR interrupts */
  194. hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,
  195. HDMI_IH_MUTE_I2CM_STAT0);
  196. }
  197. static int dw_hdmi_i2c_read(struct dw_hdmi *hdmi,
  198. unsigned char *buf, unsigned int length)
  199. {
  200. struct dw_hdmi_i2c *i2c = hdmi->i2c;
  201. int stat;
  202. if (!i2c->is_regaddr) {
  203. dev_dbg(hdmi->dev, "set read register address to 0\n");
  204. i2c->slave_reg = 0x00;
  205. i2c->is_regaddr = true;
  206. }
  207. while (length--) {
  208. reinit_completion(&i2c->cmp);
  209. hdmi_writeb(hdmi, i2c->slave_reg++, HDMI_I2CM_ADDRESS);
  210. if (i2c->is_segment)
  211. hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_READ_EXT,
  212. HDMI_I2CM_OPERATION);
  213. else
  214. hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_READ,
  215. HDMI_I2CM_OPERATION);
  216. stat = wait_for_completion_timeout(&i2c->cmp, HZ / 10);
  217. if (!stat)
  218. return -EAGAIN;
  219. /* Check for error condition on the bus */
  220. if (i2c->stat & HDMI_IH_I2CM_STAT0_ERROR)
  221. return -EIO;
  222. *buf++ = hdmi_readb(hdmi, HDMI_I2CM_DATAI);
  223. }
  224. i2c->is_segment = false;
  225. return 0;
  226. }
  227. static int dw_hdmi_i2c_write(struct dw_hdmi *hdmi,
  228. unsigned char *buf, unsigned int length)
  229. {
  230. struct dw_hdmi_i2c *i2c = hdmi->i2c;
  231. int stat;
  232. if (!i2c->is_regaddr) {
  233. /* Use the first write byte as register address */
  234. i2c->slave_reg = buf[0];
  235. length--;
  236. buf++;
  237. i2c->is_regaddr = true;
  238. }
  239. while (length--) {
  240. reinit_completion(&i2c->cmp);
  241. hdmi_writeb(hdmi, *buf++, HDMI_I2CM_DATAO);
  242. hdmi_writeb(hdmi, i2c->slave_reg++, HDMI_I2CM_ADDRESS);
  243. hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_WRITE,
  244. HDMI_I2CM_OPERATION);
  245. stat = wait_for_completion_timeout(&i2c->cmp, HZ / 10);
  246. if (!stat)
  247. return -EAGAIN;
  248. /* Check for error condition on the bus */
  249. if (i2c->stat & HDMI_IH_I2CM_STAT0_ERROR)
  250. return -EIO;
  251. }
  252. return 0;
  253. }
  254. static int dw_hdmi_i2c_xfer(struct i2c_adapter *adap,
  255. struct i2c_msg *msgs, int num)
  256. {
  257. struct dw_hdmi *hdmi = i2c_get_adapdata(adap);
  258. struct dw_hdmi_i2c *i2c = hdmi->i2c;
  259. u8 addr = msgs[0].addr;
  260. int i, ret = 0;
  261. dev_dbg(hdmi->dev, "xfer: num: %d, addr: %#x\n", num, addr);
  262. for (i = 0; i < num; i++) {
  263. if (msgs[i].len == 0) {
  264. dev_dbg(hdmi->dev,
  265. "unsupported transfer %d/%d, no data\n",
  266. i + 1, num);
  267. return -EOPNOTSUPP;
  268. }
  269. }
  270. mutex_lock(&i2c->lock);
  271. /* Unmute DONE and ERROR interrupts */
  272. hdmi_writeb(hdmi, 0x00, HDMI_IH_MUTE_I2CM_STAT0);
  273. /* Set slave device address taken from the first I2C message */
  274. hdmi_writeb(hdmi, addr, HDMI_I2CM_SLAVE);
  275. /* Set slave device register address on transfer */
  276. i2c->is_regaddr = false;
  277. /* Set segment pointer for I2C extended read mode operation */
  278. i2c->is_segment = false;
  279. for (i = 0; i < num; i++) {
  280. dev_dbg(hdmi->dev, "xfer: num: %d/%d, len: %d, flags: %#x\n",
  281. i + 1, num, msgs[i].len, msgs[i].flags);
  282. if (msgs[i].addr == DDC_SEGMENT_ADDR && msgs[i].len == 1) {
  283. i2c->is_segment = true;
  284. hdmi_writeb(hdmi, DDC_SEGMENT_ADDR, HDMI_I2CM_SEGADDR);
  285. hdmi_writeb(hdmi, *msgs[i].buf, HDMI_I2CM_SEGPTR);
  286. } else {
  287. if (msgs[i].flags & I2C_M_RD)
  288. ret = dw_hdmi_i2c_read(hdmi, msgs[i].buf,
  289. msgs[i].len);
  290. else
  291. ret = dw_hdmi_i2c_write(hdmi, msgs[i].buf,
  292. msgs[i].len);
  293. }
  294. if (ret < 0)
  295. break;
  296. }
  297. if (!ret)
  298. ret = num;
  299. /* Mute DONE and ERROR interrupts */
  300. hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,
  301. HDMI_IH_MUTE_I2CM_STAT0);
  302. mutex_unlock(&i2c->lock);
  303. return ret;
  304. }
  305. static u32 dw_hdmi_i2c_func(struct i2c_adapter *adapter)
  306. {
  307. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  308. }
  309. static const struct i2c_algorithm dw_hdmi_algorithm = {
  310. .master_xfer = dw_hdmi_i2c_xfer,
  311. .functionality = dw_hdmi_i2c_func,
  312. };
  313. static struct i2c_adapter *dw_hdmi_i2c_adapter(struct dw_hdmi *hdmi)
  314. {
  315. struct i2c_adapter *adap;
  316. struct dw_hdmi_i2c *i2c;
  317. int ret;
  318. i2c = devm_kzalloc(hdmi->dev, sizeof(*i2c), GFP_KERNEL);
  319. if (!i2c)
  320. return ERR_PTR(-ENOMEM);
  321. mutex_init(&i2c->lock);
  322. init_completion(&i2c->cmp);
  323. adap = &i2c->adap;
  324. adap->class = I2C_CLASS_DDC;
  325. adap->owner = THIS_MODULE;
  326. adap->dev.parent = hdmi->dev;
  327. adap->algo = &dw_hdmi_algorithm;
  328. strlcpy(adap->name, "DesignWare HDMI", sizeof(adap->name));
  329. i2c_set_adapdata(adap, hdmi);
  330. ret = i2c_add_adapter(adap);
  331. if (ret) {
  332. dev_warn(hdmi->dev, "cannot add %s I2C adapter\n", adap->name);
  333. devm_kfree(hdmi->dev, i2c);
  334. return ERR_PTR(ret);
  335. }
  336. hdmi->i2c = i2c;
  337. dev_info(hdmi->dev, "registered %s I2C bus driver\n", adap->name);
  338. return adap;
  339. }
  340. static void hdmi_set_cts_n(struct dw_hdmi *hdmi, unsigned int cts,
  341. unsigned int n)
  342. {
  343. /* Must be set/cleared first */
  344. hdmi_modb(hdmi, 0, HDMI_AUD_CTS3_CTS_MANUAL, HDMI_AUD_CTS3);
  345. /* nshift factor = 0 */
  346. hdmi_modb(hdmi, 0, HDMI_AUD_CTS3_N_SHIFT_MASK, HDMI_AUD_CTS3);
  347. hdmi_writeb(hdmi, ((cts >> 16) & HDMI_AUD_CTS3_AUDCTS19_16_MASK) |
  348. HDMI_AUD_CTS3_CTS_MANUAL, HDMI_AUD_CTS3);
  349. hdmi_writeb(hdmi, (cts >> 8) & 0xff, HDMI_AUD_CTS2);
  350. hdmi_writeb(hdmi, cts & 0xff, HDMI_AUD_CTS1);
  351. hdmi_writeb(hdmi, (n >> 16) & 0x0f, HDMI_AUD_N3);
  352. hdmi_writeb(hdmi, (n >> 8) & 0xff, HDMI_AUD_N2);
  353. hdmi_writeb(hdmi, n & 0xff, HDMI_AUD_N1);
  354. }
  355. static unsigned int hdmi_compute_n(unsigned int freq, unsigned long pixel_clk)
  356. {
  357. unsigned int n = (128 * freq) / 1000;
  358. unsigned int mult = 1;
  359. while (freq > 48000) {
  360. mult *= 2;
  361. freq /= 2;
  362. }
  363. switch (freq) {
  364. case 32000:
  365. if (pixel_clk == 25175000)
  366. n = 4576;
  367. else if (pixel_clk == 27027000)
  368. n = 4096;
  369. else if (pixel_clk == 74176000 || pixel_clk == 148352000)
  370. n = 11648;
  371. else
  372. n = 4096;
  373. n *= mult;
  374. break;
  375. case 44100:
  376. if (pixel_clk == 25175000)
  377. n = 7007;
  378. else if (pixel_clk == 74176000)
  379. n = 17836;
  380. else if (pixel_clk == 148352000)
  381. n = 8918;
  382. else
  383. n = 6272;
  384. n *= mult;
  385. break;
  386. case 48000:
  387. if (pixel_clk == 25175000)
  388. n = 6864;
  389. else if (pixel_clk == 27027000)
  390. n = 6144;
  391. else if (pixel_clk == 74176000)
  392. n = 11648;
  393. else if (pixel_clk == 148352000)
  394. n = 5824;
  395. else
  396. n = 6144;
  397. n *= mult;
  398. break;
  399. default:
  400. break;
  401. }
  402. return n;
  403. }
  404. static void hdmi_set_clk_regenerator(struct dw_hdmi *hdmi,
  405. unsigned long pixel_clk, unsigned int sample_rate)
  406. {
  407. unsigned long ftdms = pixel_clk;
  408. unsigned int n, cts;
  409. u64 tmp;
  410. n = hdmi_compute_n(sample_rate, pixel_clk);
  411. /*
  412. * Compute the CTS value from the N value. Note that CTS and N
  413. * can be up to 20 bits in total, so we need 64-bit math. Also
  414. * note that our TDMS clock is not fully accurate; it is accurate
  415. * to kHz. This can introduce an unnecessary remainder in the
  416. * calculation below, so we don't try to warn about that.
  417. */
  418. tmp = (u64)ftdms * n;
  419. do_div(tmp, 128 * sample_rate);
  420. cts = tmp;
  421. dev_dbg(hdmi->dev, "%s: fs=%uHz ftdms=%lu.%03luMHz N=%d cts=%d\n",
  422. __func__, sample_rate, ftdms / 1000000, (ftdms / 1000) % 1000,
  423. n, cts);
  424. spin_lock_irq(&hdmi->audio_lock);
  425. hdmi->audio_n = n;
  426. hdmi->audio_cts = cts;
  427. hdmi_set_cts_n(hdmi, cts, hdmi->audio_enable ? n : 0);
  428. spin_unlock_irq(&hdmi->audio_lock);
  429. }
  430. static void hdmi_init_clk_regenerator(struct dw_hdmi *hdmi)
  431. {
  432. mutex_lock(&hdmi->audio_mutex);
  433. hdmi_set_clk_regenerator(hdmi, 74250000, hdmi->sample_rate);
  434. mutex_unlock(&hdmi->audio_mutex);
  435. }
  436. static void hdmi_clk_regenerator_update_pixel_clock(struct dw_hdmi *hdmi)
  437. {
  438. mutex_lock(&hdmi->audio_mutex);
  439. hdmi_set_clk_regenerator(hdmi, hdmi->hdmi_data.video_mode.mpixelclock,
  440. hdmi->sample_rate);
  441. mutex_unlock(&hdmi->audio_mutex);
  442. }
  443. void dw_hdmi_set_sample_rate(struct dw_hdmi *hdmi, unsigned int rate)
  444. {
  445. mutex_lock(&hdmi->audio_mutex);
  446. hdmi->sample_rate = rate;
  447. hdmi_set_clk_regenerator(hdmi, hdmi->hdmi_data.video_mode.mpixelclock,
  448. hdmi->sample_rate);
  449. mutex_unlock(&hdmi->audio_mutex);
  450. }
  451. EXPORT_SYMBOL_GPL(dw_hdmi_set_sample_rate);
  452. static void hdmi_enable_audio_clk(struct dw_hdmi *hdmi, bool enable)
  453. {
  454. if (enable)
  455. hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_AUDCLK_DISABLE;
  456. else
  457. hdmi->mc_clkdis |= HDMI_MC_CLKDIS_AUDCLK_DISABLE;
  458. hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);
  459. }
  460. static void dw_hdmi_ahb_audio_enable(struct dw_hdmi *hdmi)
  461. {
  462. hdmi_set_cts_n(hdmi, hdmi->audio_cts, hdmi->audio_n);
  463. }
  464. static void dw_hdmi_ahb_audio_disable(struct dw_hdmi *hdmi)
  465. {
  466. hdmi_set_cts_n(hdmi, hdmi->audio_cts, 0);
  467. }
  468. static void dw_hdmi_i2s_audio_enable(struct dw_hdmi *hdmi)
  469. {
  470. hdmi_set_cts_n(hdmi, hdmi->audio_cts, hdmi->audio_n);
  471. hdmi_enable_audio_clk(hdmi, true);
  472. }
  473. static void dw_hdmi_i2s_audio_disable(struct dw_hdmi *hdmi)
  474. {
  475. hdmi_enable_audio_clk(hdmi, false);
  476. }
  477. void dw_hdmi_audio_enable(struct dw_hdmi *hdmi)
  478. {
  479. unsigned long flags;
  480. spin_lock_irqsave(&hdmi->audio_lock, flags);
  481. hdmi->audio_enable = true;
  482. if (hdmi->enable_audio)
  483. hdmi->enable_audio(hdmi);
  484. spin_unlock_irqrestore(&hdmi->audio_lock, flags);
  485. }
  486. EXPORT_SYMBOL_GPL(dw_hdmi_audio_enable);
  487. void dw_hdmi_audio_disable(struct dw_hdmi *hdmi)
  488. {
  489. unsigned long flags;
  490. spin_lock_irqsave(&hdmi->audio_lock, flags);
  491. hdmi->audio_enable = false;
  492. if (hdmi->disable_audio)
  493. hdmi->disable_audio(hdmi);
  494. spin_unlock_irqrestore(&hdmi->audio_lock, flags);
  495. }
  496. EXPORT_SYMBOL_GPL(dw_hdmi_audio_disable);
  497. static bool hdmi_bus_fmt_is_rgb(unsigned int bus_format)
  498. {
  499. switch (bus_format) {
  500. case MEDIA_BUS_FMT_RGB888_1X24:
  501. case MEDIA_BUS_FMT_RGB101010_1X30:
  502. case MEDIA_BUS_FMT_RGB121212_1X36:
  503. case MEDIA_BUS_FMT_RGB161616_1X48:
  504. return true;
  505. default:
  506. return false;
  507. }
  508. }
  509. static bool hdmi_bus_fmt_is_yuv444(unsigned int bus_format)
  510. {
  511. switch (bus_format) {
  512. case MEDIA_BUS_FMT_YUV8_1X24:
  513. case MEDIA_BUS_FMT_YUV10_1X30:
  514. case MEDIA_BUS_FMT_YUV12_1X36:
  515. case MEDIA_BUS_FMT_YUV16_1X48:
  516. return true;
  517. default:
  518. return false;
  519. }
  520. }
  521. static bool hdmi_bus_fmt_is_yuv422(unsigned int bus_format)
  522. {
  523. switch (bus_format) {
  524. case MEDIA_BUS_FMT_UYVY8_1X16:
  525. case MEDIA_BUS_FMT_UYVY10_1X20:
  526. case MEDIA_BUS_FMT_UYVY12_1X24:
  527. return true;
  528. default:
  529. return false;
  530. }
  531. }
  532. static int hdmi_bus_fmt_color_depth(unsigned int bus_format)
  533. {
  534. switch (bus_format) {
  535. case MEDIA_BUS_FMT_RGB888_1X24:
  536. case MEDIA_BUS_FMT_YUV8_1X24:
  537. case MEDIA_BUS_FMT_UYVY8_1X16:
  538. case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
  539. return 8;
  540. case MEDIA_BUS_FMT_RGB101010_1X30:
  541. case MEDIA_BUS_FMT_YUV10_1X30:
  542. case MEDIA_BUS_FMT_UYVY10_1X20:
  543. case MEDIA_BUS_FMT_UYYVYY10_0_5X30:
  544. return 10;
  545. case MEDIA_BUS_FMT_RGB121212_1X36:
  546. case MEDIA_BUS_FMT_YUV12_1X36:
  547. case MEDIA_BUS_FMT_UYVY12_1X24:
  548. case MEDIA_BUS_FMT_UYYVYY12_0_5X36:
  549. return 12;
  550. case MEDIA_BUS_FMT_RGB161616_1X48:
  551. case MEDIA_BUS_FMT_YUV16_1X48:
  552. case MEDIA_BUS_FMT_UYYVYY16_0_5X48:
  553. return 16;
  554. default:
  555. return 0;
  556. }
  557. }
  558. /*
  559. * this submodule is responsible for the video data synchronization.
  560. * for example, for RGB 4:4:4 input, the data map is defined as
  561. * pin{47~40} <==> R[7:0]
  562. * pin{31~24} <==> G[7:0]
  563. * pin{15~8} <==> B[7:0]
  564. */
  565. static void hdmi_video_sample(struct dw_hdmi *hdmi)
  566. {
  567. int color_format = 0;
  568. u8 val;
  569. switch (hdmi->hdmi_data.enc_in_bus_format) {
  570. case MEDIA_BUS_FMT_RGB888_1X24:
  571. color_format = 0x01;
  572. break;
  573. case MEDIA_BUS_FMT_RGB101010_1X30:
  574. color_format = 0x03;
  575. break;
  576. case MEDIA_BUS_FMT_RGB121212_1X36:
  577. color_format = 0x05;
  578. break;
  579. case MEDIA_BUS_FMT_RGB161616_1X48:
  580. color_format = 0x07;
  581. break;
  582. case MEDIA_BUS_FMT_YUV8_1X24:
  583. case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
  584. color_format = 0x09;
  585. break;
  586. case MEDIA_BUS_FMT_YUV10_1X30:
  587. case MEDIA_BUS_FMT_UYYVYY10_0_5X30:
  588. color_format = 0x0B;
  589. break;
  590. case MEDIA_BUS_FMT_YUV12_1X36:
  591. case MEDIA_BUS_FMT_UYYVYY12_0_5X36:
  592. color_format = 0x0D;
  593. break;
  594. case MEDIA_BUS_FMT_YUV16_1X48:
  595. case MEDIA_BUS_FMT_UYYVYY16_0_5X48:
  596. color_format = 0x0F;
  597. break;
  598. case MEDIA_BUS_FMT_UYVY8_1X16:
  599. color_format = 0x16;
  600. break;
  601. case MEDIA_BUS_FMT_UYVY10_1X20:
  602. color_format = 0x14;
  603. break;
  604. case MEDIA_BUS_FMT_UYVY12_1X24:
  605. color_format = 0x12;
  606. break;
  607. default:
  608. return;
  609. }
  610. val = HDMI_TX_INVID0_INTERNAL_DE_GENERATOR_DISABLE |
  611. ((color_format << HDMI_TX_INVID0_VIDEO_MAPPING_OFFSET) &
  612. HDMI_TX_INVID0_VIDEO_MAPPING_MASK);
  613. hdmi_writeb(hdmi, val, HDMI_TX_INVID0);
  614. /* Enable TX stuffing: When DE is inactive, fix the output data to 0 */
  615. val = HDMI_TX_INSTUFFING_BDBDATA_STUFFING_ENABLE |
  616. HDMI_TX_INSTUFFING_RCRDATA_STUFFING_ENABLE |
  617. HDMI_TX_INSTUFFING_GYDATA_STUFFING_ENABLE;
  618. hdmi_writeb(hdmi, val, HDMI_TX_INSTUFFING);
  619. hdmi_writeb(hdmi, 0x0, HDMI_TX_GYDATA0);
  620. hdmi_writeb(hdmi, 0x0, HDMI_TX_GYDATA1);
  621. hdmi_writeb(hdmi, 0x0, HDMI_TX_RCRDATA0);
  622. hdmi_writeb(hdmi, 0x0, HDMI_TX_RCRDATA1);
  623. hdmi_writeb(hdmi, 0x0, HDMI_TX_BCBDATA0);
  624. hdmi_writeb(hdmi, 0x0, HDMI_TX_BCBDATA1);
  625. }
  626. static int is_color_space_conversion(struct dw_hdmi *hdmi)
  627. {
  628. return hdmi->hdmi_data.enc_in_bus_format != hdmi->hdmi_data.enc_out_bus_format;
  629. }
  630. static int is_color_space_decimation(struct dw_hdmi *hdmi)
  631. {
  632. if (!hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format))
  633. return 0;
  634. if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_in_bus_format) ||
  635. hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_in_bus_format))
  636. return 1;
  637. return 0;
  638. }
  639. static int is_color_space_interpolation(struct dw_hdmi *hdmi)
  640. {
  641. if (!hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_in_bus_format))
  642. return 0;
  643. if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format) ||
  644. hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format))
  645. return 1;
  646. return 0;
  647. }
  648. static void dw_hdmi_update_csc_coeffs(struct dw_hdmi *hdmi)
  649. {
  650. const u16 (*csc_coeff)[3][4] = &csc_coeff_default;
  651. unsigned i;
  652. u32 csc_scale = 1;
  653. if (is_color_space_conversion(hdmi)) {
  654. if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format)) {
  655. if (hdmi->hdmi_data.enc_out_encoding ==
  656. V4L2_YCBCR_ENC_601)
  657. csc_coeff = &csc_coeff_rgb_out_eitu601;
  658. else
  659. csc_coeff = &csc_coeff_rgb_out_eitu709;
  660. } else if (hdmi_bus_fmt_is_rgb(
  661. hdmi->hdmi_data.enc_in_bus_format)) {
  662. if (hdmi->hdmi_data.enc_out_encoding ==
  663. V4L2_YCBCR_ENC_601)
  664. csc_coeff = &csc_coeff_rgb_in_eitu601;
  665. else
  666. csc_coeff = &csc_coeff_rgb_in_eitu709;
  667. csc_scale = 0;
  668. }
  669. }
  670. /* The CSC registers are sequential, alternating MSB then LSB */
  671. for (i = 0; i < ARRAY_SIZE(csc_coeff_default[0]); i++) {
  672. u16 coeff_a = (*csc_coeff)[0][i];
  673. u16 coeff_b = (*csc_coeff)[1][i];
  674. u16 coeff_c = (*csc_coeff)[2][i];
  675. hdmi_writeb(hdmi, coeff_a & 0xff, HDMI_CSC_COEF_A1_LSB + i * 2);
  676. hdmi_writeb(hdmi, coeff_a >> 8, HDMI_CSC_COEF_A1_MSB + i * 2);
  677. hdmi_writeb(hdmi, coeff_b & 0xff, HDMI_CSC_COEF_B1_LSB + i * 2);
  678. hdmi_writeb(hdmi, coeff_b >> 8, HDMI_CSC_COEF_B1_MSB + i * 2);
  679. hdmi_writeb(hdmi, coeff_c & 0xff, HDMI_CSC_COEF_C1_LSB + i * 2);
  680. hdmi_writeb(hdmi, coeff_c >> 8, HDMI_CSC_COEF_C1_MSB + i * 2);
  681. }
  682. hdmi_modb(hdmi, csc_scale, HDMI_CSC_SCALE_CSCSCALE_MASK,
  683. HDMI_CSC_SCALE);
  684. }
  685. static void hdmi_video_csc(struct dw_hdmi *hdmi)
  686. {
  687. int color_depth = 0;
  688. int interpolation = HDMI_CSC_CFG_INTMODE_DISABLE;
  689. int decimation = 0;
  690. /* YCC422 interpolation to 444 mode */
  691. if (is_color_space_interpolation(hdmi))
  692. interpolation = HDMI_CSC_CFG_INTMODE_CHROMA_INT_FORMULA1;
  693. else if (is_color_space_decimation(hdmi))
  694. decimation = HDMI_CSC_CFG_DECMODE_CHROMA_INT_FORMULA3;
  695. switch (hdmi_bus_fmt_color_depth(hdmi->hdmi_data.enc_out_bus_format)) {
  696. case 8:
  697. color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_24BPP;
  698. break;
  699. case 10:
  700. color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_30BPP;
  701. break;
  702. case 12:
  703. color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_36BPP;
  704. break;
  705. case 16:
  706. color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_48BPP;
  707. break;
  708. default:
  709. return;
  710. }
  711. /* Configure the CSC registers */
  712. hdmi_writeb(hdmi, interpolation | decimation, HDMI_CSC_CFG);
  713. hdmi_modb(hdmi, color_depth, HDMI_CSC_SCALE_CSC_COLORDE_PTH_MASK,
  714. HDMI_CSC_SCALE);
  715. dw_hdmi_update_csc_coeffs(hdmi);
  716. }
  717. /*
  718. * HDMI video packetizer is used to packetize the data.
  719. * for example, if input is YCC422 mode or repeater is used,
  720. * data should be repacked this module can be bypassed.
  721. */
  722. static void hdmi_video_packetize(struct dw_hdmi *hdmi)
  723. {
  724. unsigned int color_depth = 0;
  725. unsigned int remap_size = HDMI_VP_REMAP_YCC422_16bit;
  726. unsigned int output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_PP;
  727. struct hdmi_data_info *hdmi_data = &hdmi->hdmi_data;
  728. u8 val, vp_conf;
  729. if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format) ||
  730. hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format)) {
  731. switch (hdmi_bus_fmt_color_depth(
  732. hdmi->hdmi_data.enc_out_bus_format)) {
  733. case 8:
  734. color_depth = 4;
  735. output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;
  736. break;
  737. case 10:
  738. color_depth = 5;
  739. break;
  740. case 12:
  741. color_depth = 6;
  742. break;
  743. case 16:
  744. color_depth = 7;
  745. break;
  746. default:
  747. output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;
  748. }
  749. } else if (hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format)) {
  750. switch (hdmi_bus_fmt_color_depth(
  751. hdmi->hdmi_data.enc_out_bus_format)) {
  752. case 0:
  753. case 8:
  754. remap_size = HDMI_VP_REMAP_YCC422_16bit;
  755. break;
  756. case 10:
  757. remap_size = HDMI_VP_REMAP_YCC422_20bit;
  758. break;
  759. case 12:
  760. remap_size = HDMI_VP_REMAP_YCC422_24bit;
  761. break;
  762. default:
  763. return;
  764. }
  765. output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_YCC422;
  766. } else {
  767. return;
  768. }
  769. /* set the packetizer registers */
  770. val = ((color_depth << HDMI_VP_PR_CD_COLOR_DEPTH_OFFSET) &
  771. HDMI_VP_PR_CD_COLOR_DEPTH_MASK) |
  772. ((hdmi_data->pix_repet_factor <<
  773. HDMI_VP_PR_CD_DESIRED_PR_FACTOR_OFFSET) &
  774. HDMI_VP_PR_CD_DESIRED_PR_FACTOR_MASK);
  775. hdmi_writeb(hdmi, val, HDMI_VP_PR_CD);
  776. hdmi_modb(hdmi, HDMI_VP_STUFF_PR_STUFFING_STUFFING_MODE,
  777. HDMI_VP_STUFF_PR_STUFFING_MASK, HDMI_VP_STUFF);
  778. /* Data from pixel repeater block */
  779. if (hdmi_data->pix_repet_factor > 1) {
  780. vp_conf = HDMI_VP_CONF_PR_EN_ENABLE |
  781. HDMI_VP_CONF_BYPASS_SELECT_PIX_REPEATER;
  782. } else { /* data from packetizer block */
  783. vp_conf = HDMI_VP_CONF_PR_EN_DISABLE |
  784. HDMI_VP_CONF_BYPASS_SELECT_VID_PACKETIZER;
  785. }
  786. hdmi_modb(hdmi, vp_conf,
  787. HDMI_VP_CONF_PR_EN_MASK |
  788. HDMI_VP_CONF_BYPASS_SELECT_MASK, HDMI_VP_CONF);
  789. hdmi_modb(hdmi, 1 << HDMI_VP_STUFF_IDEFAULT_PHASE_OFFSET,
  790. HDMI_VP_STUFF_IDEFAULT_PHASE_MASK, HDMI_VP_STUFF);
  791. hdmi_writeb(hdmi, remap_size, HDMI_VP_REMAP);
  792. if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_PP) {
  793. vp_conf = HDMI_VP_CONF_BYPASS_EN_DISABLE |
  794. HDMI_VP_CONF_PP_EN_ENABLE |
  795. HDMI_VP_CONF_YCC422_EN_DISABLE;
  796. } else if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_YCC422) {
  797. vp_conf = HDMI_VP_CONF_BYPASS_EN_DISABLE |
  798. HDMI_VP_CONF_PP_EN_DISABLE |
  799. HDMI_VP_CONF_YCC422_EN_ENABLE;
  800. } else if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS) {
  801. vp_conf = HDMI_VP_CONF_BYPASS_EN_ENABLE |
  802. HDMI_VP_CONF_PP_EN_DISABLE |
  803. HDMI_VP_CONF_YCC422_EN_DISABLE;
  804. } else {
  805. return;
  806. }
  807. hdmi_modb(hdmi, vp_conf,
  808. HDMI_VP_CONF_BYPASS_EN_MASK | HDMI_VP_CONF_PP_EN_ENMASK |
  809. HDMI_VP_CONF_YCC422_EN_MASK, HDMI_VP_CONF);
  810. hdmi_modb(hdmi, HDMI_VP_STUFF_PP_STUFFING_STUFFING_MODE |
  811. HDMI_VP_STUFF_YCC422_STUFFING_STUFFING_MODE,
  812. HDMI_VP_STUFF_PP_STUFFING_MASK |
  813. HDMI_VP_STUFF_YCC422_STUFFING_MASK, HDMI_VP_STUFF);
  814. hdmi_modb(hdmi, output_select, HDMI_VP_CONF_OUTPUT_SELECTOR_MASK,
  815. HDMI_VP_CONF);
  816. }
  817. /* -----------------------------------------------------------------------------
  818. * Synopsys PHY Handling
  819. */
  820. static inline void hdmi_phy_test_clear(struct dw_hdmi *hdmi,
  821. unsigned char bit)
  822. {
  823. hdmi_modb(hdmi, bit << HDMI_PHY_TST0_TSTCLR_OFFSET,
  824. HDMI_PHY_TST0_TSTCLR_MASK, HDMI_PHY_TST0);
  825. }
  826. static bool hdmi_phy_wait_i2c_done(struct dw_hdmi *hdmi, int msec)
  827. {
  828. u32 val;
  829. while ((val = hdmi_readb(hdmi, HDMI_IH_I2CMPHY_STAT0) & 0x3) == 0) {
  830. if (msec-- == 0)
  831. return false;
  832. udelay(1000);
  833. }
  834. hdmi_writeb(hdmi, val, HDMI_IH_I2CMPHY_STAT0);
  835. return true;
  836. }
  837. void dw_hdmi_phy_i2c_write(struct dw_hdmi *hdmi, unsigned short data,
  838. unsigned char addr)
  839. {
  840. hdmi_writeb(hdmi, 0xFF, HDMI_IH_I2CMPHY_STAT0);
  841. hdmi_writeb(hdmi, addr, HDMI_PHY_I2CM_ADDRESS_ADDR);
  842. hdmi_writeb(hdmi, (unsigned char)(data >> 8),
  843. HDMI_PHY_I2CM_DATAO_1_ADDR);
  844. hdmi_writeb(hdmi, (unsigned char)(data >> 0),
  845. HDMI_PHY_I2CM_DATAO_0_ADDR);
  846. hdmi_writeb(hdmi, HDMI_PHY_I2CM_OPERATION_ADDR_WRITE,
  847. HDMI_PHY_I2CM_OPERATION_ADDR);
  848. hdmi_phy_wait_i2c_done(hdmi, 1000);
  849. }
  850. EXPORT_SYMBOL_GPL(dw_hdmi_phy_i2c_write);
  851. static void dw_hdmi_phy_enable_powerdown(struct dw_hdmi *hdmi, bool enable)
  852. {
  853. hdmi_mask_writeb(hdmi, !enable, HDMI_PHY_CONF0,
  854. HDMI_PHY_CONF0_PDZ_OFFSET,
  855. HDMI_PHY_CONF0_PDZ_MASK);
  856. }
  857. static void dw_hdmi_phy_enable_tmds(struct dw_hdmi *hdmi, u8 enable)
  858. {
  859. hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
  860. HDMI_PHY_CONF0_ENTMDS_OFFSET,
  861. HDMI_PHY_CONF0_ENTMDS_MASK);
  862. }
  863. static void dw_hdmi_phy_enable_svsret(struct dw_hdmi *hdmi, u8 enable)
  864. {
  865. hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
  866. HDMI_PHY_CONF0_SVSRET_OFFSET,
  867. HDMI_PHY_CONF0_SVSRET_MASK);
  868. }
  869. void dw_hdmi_phy_gen2_pddq(struct dw_hdmi *hdmi, u8 enable)
  870. {
  871. hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
  872. HDMI_PHY_CONF0_GEN2_PDDQ_OFFSET,
  873. HDMI_PHY_CONF0_GEN2_PDDQ_MASK);
  874. }
  875. EXPORT_SYMBOL_GPL(dw_hdmi_phy_gen2_pddq);
  876. void dw_hdmi_phy_gen2_txpwron(struct dw_hdmi *hdmi, u8 enable)
  877. {
  878. hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
  879. HDMI_PHY_CONF0_GEN2_TXPWRON_OFFSET,
  880. HDMI_PHY_CONF0_GEN2_TXPWRON_MASK);
  881. }
  882. EXPORT_SYMBOL_GPL(dw_hdmi_phy_gen2_txpwron);
  883. static void dw_hdmi_phy_sel_data_en_pol(struct dw_hdmi *hdmi, u8 enable)
  884. {
  885. hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
  886. HDMI_PHY_CONF0_SELDATAENPOL_OFFSET,
  887. HDMI_PHY_CONF0_SELDATAENPOL_MASK);
  888. }
  889. static void dw_hdmi_phy_sel_interface_control(struct dw_hdmi *hdmi, u8 enable)
  890. {
  891. hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
  892. HDMI_PHY_CONF0_SELDIPIF_OFFSET,
  893. HDMI_PHY_CONF0_SELDIPIF_MASK);
  894. }
  895. void dw_hdmi_phy_reset(struct dw_hdmi *hdmi)
  896. {
  897. /* PHY reset. The reset signal is active high on Gen2 PHYs. */
  898. hdmi_writeb(hdmi, HDMI_MC_PHYRSTZ_PHYRSTZ, HDMI_MC_PHYRSTZ);
  899. hdmi_writeb(hdmi, 0, HDMI_MC_PHYRSTZ);
  900. }
  901. EXPORT_SYMBOL_GPL(dw_hdmi_phy_reset);
  902. void dw_hdmi_phy_i2c_set_addr(struct dw_hdmi *hdmi, u8 address)
  903. {
  904. hdmi_phy_test_clear(hdmi, 1);
  905. hdmi_writeb(hdmi, address, HDMI_PHY_I2CM_SLAVE_ADDR);
  906. hdmi_phy_test_clear(hdmi, 0);
  907. }
  908. EXPORT_SYMBOL_GPL(dw_hdmi_phy_i2c_set_addr);
  909. static void dw_hdmi_phy_power_off(struct dw_hdmi *hdmi)
  910. {
  911. const struct dw_hdmi_phy_data *phy = hdmi->phy.data;
  912. unsigned int i;
  913. u16 val;
  914. if (phy->gen == 1) {
  915. dw_hdmi_phy_enable_tmds(hdmi, 0);
  916. dw_hdmi_phy_enable_powerdown(hdmi, true);
  917. return;
  918. }
  919. dw_hdmi_phy_gen2_txpwron(hdmi, 0);
  920. /*
  921. * Wait for TX_PHY_LOCK to be deasserted to indicate that the PHY went
  922. * to low power mode.
  923. */
  924. for (i = 0; i < 5; ++i) {
  925. val = hdmi_readb(hdmi, HDMI_PHY_STAT0);
  926. if (!(val & HDMI_PHY_TX_PHY_LOCK))
  927. break;
  928. usleep_range(1000, 2000);
  929. }
  930. if (val & HDMI_PHY_TX_PHY_LOCK)
  931. dev_warn(hdmi->dev, "PHY failed to power down\n");
  932. else
  933. dev_dbg(hdmi->dev, "PHY powered down in %u iterations\n", i);
  934. dw_hdmi_phy_gen2_pddq(hdmi, 1);
  935. }
  936. static int dw_hdmi_phy_power_on(struct dw_hdmi *hdmi)
  937. {
  938. const struct dw_hdmi_phy_data *phy = hdmi->phy.data;
  939. unsigned int i;
  940. u8 val;
  941. if (phy->gen == 1) {
  942. dw_hdmi_phy_enable_powerdown(hdmi, false);
  943. /* Toggle TMDS enable. */
  944. dw_hdmi_phy_enable_tmds(hdmi, 0);
  945. dw_hdmi_phy_enable_tmds(hdmi, 1);
  946. return 0;
  947. }
  948. dw_hdmi_phy_gen2_txpwron(hdmi, 1);
  949. dw_hdmi_phy_gen2_pddq(hdmi, 0);
  950. /* Wait for PHY PLL lock */
  951. for (i = 0; i < 5; ++i) {
  952. val = hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_TX_PHY_LOCK;
  953. if (val)
  954. break;
  955. usleep_range(1000, 2000);
  956. }
  957. if (!val) {
  958. dev_err(hdmi->dev, "PHY PLL failed to lock\n");
  959. return -ETIMEDOUT;
  960. }
  961. dev_dbg(hdmi->dev, "PHY PLL locked %u iterations\n", i);
  962. return 0;
  963. }
  964. /*
  965. * PHY configuration function for the DWC HDMI 3D TX PHY. Based on the available
  966. * information the DWC MHL PHY has the same register layout and is thus also
  967. * supported by this function.
  968. */
  969. static int hdmi_phy_configure_dwc_hdmi_3d_tx(struct dw_hdmi *hdmi,
  970. const struct dw_hdmi_plat_data *pdata,
  971. unsigned long mpixelclock)
  972. {
  973. const struct dw_hdmi_mpll_config *mpll_config = pdata->mpll_cfg;
  974. const struct dw_hdmi_curr_ctrl *curr_ctrl = pdata->cur_ctr;
  975. const struct dw_hdmi_phy_config *phy_config = pdata->phy_config;
  976. /* PLL/MPLL Cfg - always match on final entry */
  977. for (; mpll_config->mpixelclock != ~0UL; mpll_config++)
  978. if (mpixelclock <= mpll_config->mpixelclock)
  979. break;
  980. for (; curr_ctrl->mpixelclock != ~0UL; curr_ctrl++)
  981. if (mpixelclock <= curr_ctrl->mpixelclock)
  982. break;
  983. for (; phy_config->mpixelclock != ~0UL; phy_config++)
  984. if (mpixelclock <= phy_config->mpixelclock)
  985. break;
  986. if (mpll_config->mpixelclock == ~0UL ||
  987. curr_ctrl->mpixelclock == ~0UL ||
  988. phy_config->mpixelclock == ~0UL)
  989. return -EINVAL;
  990. dw_hdmi_phy_i2c_write(hdmi, mpll_config->res[0].cpce,
  991. HDMI_3D_TX_PHY_CPCE_CTRL);
  992. dw_hdmi_phy_i2c_write(hdmi, mpll_config->res[0].gmp,
  993. HDMI_3D_TX_PHY_GMPCTRL);
  994. dw_hdmi_phy_i2c_write(hdmi, curr_ctrl->curr[0],
  995. HDMI_3D_TX_PHY_CURRCTRL);
  996. dw_hdmi_phy_i2c_write(hdmi, 0, HDMI_3D_TX_PHY_PLLPHBYCTRL);
  997. dw_hdmi_phy_i2c_write(hdmi, HDMI_3D_TX_PHY_MSM_CTRL_CKO_SEL_FB_CLK,
  998. HDMI_3D_TX_PHY_MSM_CTRL);
  999. dw_hdmi_phy_i2c_write(hdmi, phy_config->term, HDMI_3D_TX_PHY_TXTERM);
  1000. dw_hdmi_phy_i2c_write(hdmi, phy_config->sym_ctr,
  1001. HDMI_3D_TX_PHY_CKSYMTXCTRL);
  1002. dw_hdmi_phy_i2c_write(hdmi, phy_config->vlev_ctr,
  1003. HDMI_3D_TX_PHY_VLEVCTRL);
  1004. /* Override and disable clock termination. */
  1005. dw_hdmi_phy_i2c_write(hdmi, HDMI_3D_TX_PHY_CKCALCTRL_OVERRIDE,
  1006. HDMI_3D_TX_PHY_CKCALCTRL);
  1007. return 0;
  1008. }
  1009. static int hdmi_phy_configure(struct dw_hdmi *hdmi)
  1010. {
  1011. const struct dw_hdmi_phy_data *phy = hdmi->phy.data;
  1012. const struct dw_hdmi_plat_data *pdata = hdmi->plat_data;
  1013. unsigned long mpixelclock = hdmi->hdmi_data.video_mode.mpixelclock;
  1014. int ret;
  1015. dw_hdmi_phy_power_off(hdmi);
  1016. /* Leave low power consumption mode by asserting SVSRET. */
  1017. if (phy->has_svsret)
  1018. dw_hdmi_phy_enable_svsret(hdmi, 1);
  1019. dw_hdmi_phy_reset(hdmi);
  1020. hdmi_writeb(hdmi, HDMI_MC_HEACPHY_RST_ASSERT, HDMI_MC_HEACPHY_RST);
  1021. dw_hdmi_phy_i2c_set_addr(hdmi, HDMI_PHY_I2CM_SLAVE_ADDR_PHY_GEN2);
  1022. /* Write to the PHY as configured by the platform */
  1023. if (pdata->configure_phy)
  1024. ret = pdata->configure_phy(hdmi, pdata, mpixelclock);
  1025. else
  1026. ret = phy->configure(hdmi, pdata, mpixelclock);
  1027. if (ret) {
  1028. dev_err(hdmi->dev, "PHY configuration failed (clock %lu)\n",
  1029. mpixelclock);
  1030. return ret;
  1031. }
  1032. return dw_hdmi_phy_power_on(hdmi);
  1033. }
  1034. static int dw_hdmi_phy_init(struct dw_hdmi *hdmi, void *data,
  1035. struct drm_display_mode *mode)
  1036. {
  1037. int i, ret;
  1038. /* HDMI Phy spec says to do the phy initialization sequence twice */
  1039. for (i = 0; i < 2; i++) {
  1040. dw_hdmi_phy_sel_data_en_pol(hdmi, 1);
  1041. dw_hdmi_phy_sel_interface_control(hdmi, 0);
  1042. ret = hdmi_phy_configure(hdmi);
  1043. if (ret)
  1044. return ret;
  1045. }
  1046. return 0;
  1047. }
  1048. static void dw_hdmi_phy_disable(struct dw_hdmi *hdmi, void *data)
  1049. {
  1050. dw_hdmi_phy_power_off(hdmi);
  1051. }
  1052. enum drm_connector_status dw_hdmi_phy_read_hpd(struct dw_hdmi *hdmi,
  1053. void *data)
  1054. {
  1055. return hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_HPD ?
  1056. connector_status_connected : connector_status_disconnected;
  1057. }
  1058. EXPORT_SYMBOL_GPL(dw_hdmi_phy_read_hpd);
  1059. void dw_hdmi_phy_update_hpd(struct dw_hdmi *hdmi, void *data,
  1060. bool force, bool disabled, bool rxsense)
  1061. {
  1062. u8 old_mask = hdmi->phy_mask;
  1063. if (force || disabled || !rxsense)
  1064. hdmi->phy_mask |= HDMI_PHY_RX_SENSE;
  1065. else
  1066. hdmi->phy_mask &= ~HDMI_PHY_RX_SENSE;
  1067. if (old_mask != hdmi->phy_mask)
  1068. hdmi_writeb(hdmi, hdmi->phy_mask, HDMI_PHY_MASK0);
  1069. }
  1070. EXPORT_SYMBOL_GPL(dw_hdmi_phy_update_hpd);
  1071. void dw_hdmi_phy_setup_hpd(struct dw_hdmi *hdmi, void *data)
  1072. {
  1073. /*
  1074. * Configure the PHY RX SENSE and HPD interrupts polarities and clear
  1075. * any pending interrupt.
  1076. */
  1077. hdmi_writeb(hdmi, HDMI_PHY_HPD | HDMI_PHY_RX_SENSE, HDMI_PHY_POL0);
  1078. hdmi_writeb(hdmi, HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE,
  1079. HDMI_IH_PHY_STAT0);
  1080. /* Enable cable hot plug irq. */
  1081. hdmi_writeb(hdmi, hdmi->phy_mask, HDMI_PHY_MASK0);
  1082. /* Clear and unmute interrupts. */
  1083. hdmi_writeb(hdmi, HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE,
  1084. HDMI_IH_PHY_STAT0);
  1085. hdmi_writeb(hdmi, ~(HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE),
  1086. HDMI_IH_MUTE_PHY_STAT0);
  1087. }
  1088. EXPORT_SYMBOL_GPL(dw_hdmi_phy_setup_hpd);
  1089. static const struct dw_hdmi_phy_ops dw_hdmi_synopsys_phy_ops = {
  1090. .init = dw_hdmi_phy_init,
  1091. .disable = dw_hdmi_phy_disable,
  1092. .read_hpd = dw_hdmi_phy_read_hpd,
  1093. .update_hpd = dw_hdmi_phy_update_hpd,
  1094. .setup_hpd = dw_hdmi_phy_setup_hpd,
  1095. };
  1096. /* -----------------------------------------------------------------------------
  1097. * HDMI TX Setup
  1098. */
  1099. static void hdmi_tx_hdcp_config(struct dw_hdmi *hdmi)
  1100. {
  1101. u8 de;
  1102. if (hdmi->hdmi_data.video_mode.mdataenablepolarity)
  1103. de = HDMI_A_VIDPOLCFG_DATAENPOL_ACTIVE_HIGH;
  1104. else
  1105. de = HDMI_A_VIDPOLCFG_DATAENPOL_ACTIVE_LOW;
  1106. /* disable rx detect */
  1107. hdmi_modb(hdmi, HDMI_A_HDCPCFG0_RXDETECT_DISABLE,
  1108. HDMI_A_HDCPCFG0_RXDETECT_MASK, HDMI_A_HDCPCFG0);
  1109. hdmi_modb(hdmi, de, HDMI_A_VIDPOLCFG_DATAENPOL_MASK, HDMI_A_VIDPOLCFG);
  1110. hdmi_modb(hdmi, HDMI_A_HDCPCFG1_ENCRYPTIONDISABLE_DISABLE,
  1111. HDMI_A_HDCPCFG1_ENCRYPTIONDISABLE_MASK, HDMI_A_HDCPCFG1);
  1112. }
  1113. static void hdmi_config_AVI(struct dw_hdmi *hdmi, struct drm_display_mode *mode)
  1114. {
  1115. struct hdmi_avi_infoframe frame;
  1116. u8 val;
  1117. /* Initialise info frame from DRM mode */
  1118. drm_hdmi_avi_infoframe_from_display_mode(&frame, mode, false);
  1119. if (hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format))
  1120. frame.colorspace = HDMI_COLORSPACE_YUV444;
  1121. else if (hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format))
  1122. frame.colorspace = HDMI_COLORSPACE_YUV422;
  1123. else
  1124. frame.colorspace = HDMI_COLORSPACE_RGB;
  1125. /* Set up colorimetry */
  1126. switch (hdmi->hdmi_data.enc_out_encoding) {
  1127. case V4L2_YCBCR_ENC_601:
  1128. if (hdmi->hdmi_data.enc_in_encoding == V4L2_YCBCR_ENC_XV601)
  1129. frame.colorimetry = HDMI_COLORIMETRY_EXTENDED;
  1130. else
  1131. frame.colorimetry = HDMI_COLORIMETRY_ITU_601;
  1132. frame.extended_colorimetry =
  1133. HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
  1134. break;
  1135. case V4L2_YCBCR_ENC_709:
  1136. if (hdmi->hdmi_data.enc_in_encoding == V4L2_YCBCR_ENC_XV709)
  1137. frame.colorimetry = HDMI_COLORIMETRY_EXTENDED;
  1138. else
  1139. frame.colorimetry = HDMI_COLORIMETRY_ITU_709;
  1140. frame.extended_colorimetry =
  1141. HDMI_EXTENDED_COLORIMETRY_XV_YCC_709;
  1142. break;
  1143. default: /* Carries no data */
  1144. frame.colorimetry = HDMI_COLORIMETRY_ITU_601;
  1145. frame.extended_colorimetry =
  1146. HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
  1147. break;
  1148. }
  1149. frame.scan_mode = HDMI_SCAN_MODE_NONE;
  1150. /*
  1151. * The Designware IP uses a different byte format from standard
  1152. * AVI info frames, though generally the bits are in the correct
  1153. * bytes.
  1154. */
  1155. /*
  1156. * AVI data byte 1 differences: Colorspace in bits 0,1 rather than 5,6,
  1157. * scan info in bits 4,5 rather than 0,1 and active aspect present in
  1158. * bit 6 rather than 4.
  1159. */
  1160. val = (frame.scan_mode & 3) << 4 | (frame.colorspace & 3);
  1161. if (frame.active_aspect & 15)
  1162. val |= HDMI_FC_AVICONF0_ACTIVE_FMT_INFO_PRESENT;
  1163. if (frame.top_bar || frame.bottom_bar)
  1164. val |= HDMI_FC_AVICONF0_BAR_DATA_HORIZ_BAR;
  1165. if (frame.left_bar || frame.right_bar)
  1166. val |= HDMI_FC_AVICONF0_BAR_DATA_VERT_BAR;
  1167. hdmi_writeb(hdmi, val, HDMI_FC_AVICONF0);
  1168. /* AVI data byte 2 differences: none */
  1169. val = ((frame.colorimetry & 0x3) << 6) |
  1170. ((frame.picture_aspect & 0x3) << 4) |
  1171. (frame.active_aspect & 0xf);
  1172. hdmi_writeb(hdmi, val, HDMI_FC_AVICONF1);
  1173. /* AVI data byte 3 differences: none */
  1174. val = ((frame.extended_colorimetry & 0x7) << 4) |
  1175. ((frame.quantization_range & 0x3) << 2) |
  1176. (frame.nups & 0x3);
  1177. if (frame.itc)
  1178. val |= HDMI_FC_AVICONF2_IT_CONTENT_VALID;
  1179. hdmi_writeb(hdmi, val, HDMI_FC_AVICONF2);
  1180. /* AVI data byte 4 differences: none */
  1181. val = frame.video_code & 0x7f;
  1182. hdmi_writeb(hdmi, val, HDMI_FC_AVIVID);
  1183. /* AVI Data Byte 5- set up input and output pixel repetition */
  1184. val = (((hdmi->hdmi_data.video_mode.mpixelrepetitioninput + 1) <<
  1185. HDMI_FC_PRCONF_INCOMING_PR_FACTOR_OFFSET) &
  1186. HDMI_FC_PRCONF_INCOMING_PR_FACTOR_MASK) |
  1187. ((hdmi->hdmi_data.video_mode.mpixelrepetitionoutput <<
  1188. HDMI_FC_PRCONF_OUTPUT_PR_FACTOR_OFFSET) &
  1189. HDMI_FC_PRCONF_OUTPUT_PR_FACTOR_MASK);
  1190. hdmi_writeb(hdmi, val, HDMI_FC_PRCONF);
  1191. /*
  1192. * AVI data byte 5 differences: content type in 0,1 rather than 4,5,
  1193. * ycc range in bits 2,3 rather than 6,7
  1194. */
  1195. val = ((frame.ycc_quantization_range & 0x3) << 2) |
  1196. (frame.content_type & 0x3);
  1197. hdmi_writeb(hdmi, val, HDMI_FC_AVICONF3);
  1198. /* AVI Data Bytes 6-13 */
  1199. hdmi_writeb(hdmi, frame.top_bar & 0xff, HDMI_FC_AVIETB0);
  1200. hdmi_writeb(hdmi, (frame.top_bar >> 8) & 0xff, HDMI_FC_AVIETB1);
  1201. hdmi_writeb(hdmi, frame.bottom_bar & 0xff, HDMI_FC_AVISBB0);
  1202. hdmi_writeb(hdmi, (frame.bottom_bar >> 8) & 0xff, HDMI_FC_AVISBB1);
  1203. hdmi_writeb(hdmi, frame.left_bar & 0xff, HDMI_FC_AVIELB0);
  1204. hdmi_writeb(hdmi, (frame.left_bar >> 8) & 0xff, HDMI_FC_AVIELB1);
  1205. hdmi_writeb(hdmi, frame.right_bar & 0xff, HDMI_FC_AVISRB0);
  1206. hdmi_writeb(hdmi, (frame.right_bar >> 8) & 0xff, HDMI_FC_AVISRB1);
  1207. }
  1208. static void hdmi_config_vendor_specific_infoframe(struct dw_hdmi *hdmi,
  1209. struct drm_display_mode *mode)
  1210. {
  1211. struct hdmi_vendor_infoframe frame;
  1212. u8 buffer[10];
  1213. ssize_t err;
  1214. err = drm_hdmi_vendor_infoframe_from_display_mode(&frame,
  1215. &hdmi->connector,
  1216. mode);
  1217. if (err < 0)
  1218. /*
  1219. * Going into that statement does not means vendor infoframe
  1220. * fails. It just informed us that vendor infoframe is not
  1221. * needed for the selected mode. Only 4k or stereoscopic 3D
  1222. * mode requires vendor infoframe. So just simply return.
  1223. */
  1224. return;
  1225. err = hdmi_vendor_infoframe_pack(&frame, buffer, sizeof(buffer));
  1226. if (err < 0) {
  1227. dev_err(hdmi->dev, "Failed to pack vendor infoframe: %zd\n",
  1228. err);
  1229. return;
  1230. }
  1231. hdmi_mask_writeb(hdmi, 0, HDMI_FC_DATAUTO0, HDMI_FC_DATAUTO0_VSD_OFFSET,
  1232. HDMI_FC_DATAUTO0_VSD_MASK);
  1233. /* Set the length of HDMI vendor specific InfoFrame payload */
  1234. hdmi_writeb(hdmi, buffer[2], HDMI_FC_VSDSIZE);
  1235. /* Set 24bit IEEE Registration Identifier */
  1236. hdmi_writeb(hdmi, buffer[4], HDMI_FC_VSDIEEEID0);
  1237. hdmi_writeb(hdmi, buffer[5], HDMI_FC_VSDIEEEID1);
  1238. hdmi_writeb(hdmi, buffer[6], HDMI_FC_VSDIEEEID2);
  1239. /* Set HDMI_Video_Format and HDMI_VIC/3D_Structure */
  1240. hdmi_writeb(hdmi, buffer[7], HDMI_FC_VSDPAYLOAD0);
  1241. hdmi_writeb(hdmi, buffer[8], HDMI_FC_VSDPAYLOAD1);
  1242. if (frame.s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF)
  1243. hdmi_writeb(hdmi, buffer[9], HDMI_FC_VSDPAYLOAD2);
  1244. /* Packet frame interpolation */
  1245. hdmi_writeb(hdmi, 1, HDMI_FC_DATAUTO1);
  1246. /* Auto packets per frame and line spacing */
  1247. hdmi_writeb(hdmi, 0x11, HDMI_FC_DATAUTO2);
  1248. /* Configures the Frame Composer On RDRB mode */
  1249. hdmi_mask_writeb(hdmi, 1, HDMI_FC_DATAUTO0, HDMI_FC_DATAUTO0_VSD_OFFSET,
  1250. HDMI_FC_DATAUTO0_VSD_MASK);
  1251. }
  1252. static void hdmi_av_composer(struct dw_hdmi *hdmi,
  1253. const struct drm_display_mode *mode)
  1254. {
  1255. u8 inv_val;
  1256. struct hdmi_vmode *vmode = &hdmi->hdmi_data.video_mode;
  1257. int hblank, vblank, h_de_hs, v_de_vs, hsync_len, vsync_len;
  1258. unsigned int vdisplay;
  1259. vmode->mpixelclock = mode->clock * 1000;
  1260. dev_dbg(hdmi->dev, "final pixclk = %d\n", vmode->mpixelclock);
  1261. /* Set up HDMI_FC_INVIDCONF */
  1262. inv_val = (hdmi->hdmi_data.hdcp_enable ?
  1263. HDMI_FC_INVIDCONF_HDCP_KEEPOUT_ACTIVE :
  1264. HDMI_FC_INVIDCONF_HDCP_KEEPOUT_INACTIVE);
  1265. inv_val |= mode->flags & DRM_MODE_FLAG_PVSYNC ?
  1266. HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_HIGH :
  1267. HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_LOW;
  1268. inv_val |= mode->flags & DRM_MODE_FLAG_PHSYNC ?
  1269. HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_HIGH :
  1270. HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_LOW;
  1271. inv_val |= (vmode->mdataenablepolarity ?
  1272. HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_HIGH :
  1273. HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_LOW);
  1274. if (hdmi->vic == 39)
  1275. inv_val |= HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_HIGH;
  1276. else
  1277. inv_val |= mode->flags & DRM_MODE_FLAG_INTERLACE ?
  1278. HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_HIGH :
  1279. HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_LOW;
  1280. inv_val |= mode->flags & DRM_MODE_FLAG_INTERLACE ?
  1281. HDMI_FC_INVIDCONF_IN_I_P_INTERLACED :
  1282. HDMI_FC_INVIDCONF_IN_I_P_PROGRESSIVE;
  1283. inv_val |= hdmi->sink_is_hdmi ?
  1284. HDMI_FC_INVIDCONF_DVI_MODEZ_HDMI_MODE :
  1285. HDMI_FC_INVIDCONF_DVI_MODEZ_DVI_MODE;
  1286. hdmi_writeb(hdmi, inv_val, HDMI_FC_INVIDCONF);
  1287. vdisplay = mode->vdisplay;
  1288. vblank = mode->vtotal - mode->vdisplay;
  1289. v_de_vs = mode->vsync_start - mode->vdisplay;
  1290. vsync_len = mode->vsync_end - mode->vsync_start;
  1291. /*
  1292. * When we're setting an interlaced mode, we need
  1293. * to adjust the vertical timing to suit.
  1294. */
  1295. if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
  1296. vdisplay /= 2;
  1297. vblank /= 2;
  1298. v_de_vs /= 2;
  1299. vsync_len /= 2;
  1300. }
  1301. /* Set up horizontal active pixel width */
  1302. hdmi_writeb(hdmi, mode->hdisplay >> 8, HDMI_FC_INHACTV1);
  1303. hdmi_writeb(hdmi, mode->hdisplay, HDMI_FC_INHACTV0);
  1304. /* Set up vertical active lines */
  1305. hdmi_writeb(hdmi, vdisplay >> 8, HDMI_FC_INVACTV1);
  1306. hdmi_writeb(hdmi, vdisplay, HDMI_FC_INVACTV0);
  1307. /* Set up horizontal blanking pixel region width */
  1308. hblank = mode->htotal - mode->hdisplay;
  1309. hdmi_writeb(hdmi, hblank >> 8, HDMI_FC_INHBLANK1);
  1310. hdmi_writeb(hdmi, hblank, HDMI_FC_INHBLANK0);
  1311. /* Set up vertical blanking pixel region width */
  1312. hdmi_writeb(hdmi, vblank, HDMI_FC_INVBLANK);
  1313. /* Set up HSYNC active edge delay width (in pixel clks) */
  1314. h_de_hs = mode->hsync_start - mode->hdisplay;
  1315. hdmi_writeb(hdmi, h_de_hs >> 8, HDMI_FC_HSYNCINDELAY1);
  1316. hdmi_writeb(hdmi, h_de_hs, HDMI_FC_HSYNCINDELAY0);
  1317. /* Set up VSYNC active edge delay (in lines) */
  1318. hdmi_writeb(hdmi, v_de_vs, HDMI_FC_VSYNCINDELAY);
  1319. /* Set up HSYNC active pulse width (in pixel clks) */
  1320. hsync_len = mode->hsync_end - mode->hsync_start;
  1321. hdmi_writeb(hdmi, hsync_len >> 8, HDMI_FC_HSYNCINWIDTH1);
  1322. hdmi_writeb(hdmi, hsync_len, HDMI_FC_HSYNCINWIDTH0);
  1323. /* Set up VSYNC active edge delay (in lines) */
  1324. hdmi_writeb(hdmi, vsync_len, HDMI_FC_VSYNCINWIDTH);
  1325. }
  1326. /* HDMI Initialization Step B.4 */
  1327. static void dw_hdmi_enable_video_path(struct dw_hdmi *hdmi)
  1328. {
  1329. /* control period minimum duration */
  1330. hdmi_writeb(hdmi, 12, HDMI_FC_CTRLDUR);
  1331. hdmi_writeb(hdmi, 32, HDMI_FC_EXCTRLDUR);
  1332. hdmi_writeb(hdmi, 1, HDMI_FC_EXCTRLSPAC);
  1333. /* Set to fill TMDS data channels */
  1334. hdmi_writeb(hdmi, 0x0B, HDMI_FC_CH0PREAM);
  1335. hdmi_writeb(hdmi, 0x16, HDMI_FC_CH1PREAM);
  1336. hdmi_writeb(hdmi, 0x21, HDMI_FC_CH2PREAM);
  1337. /* Enable pixel clock and tmds data path */
  1338. hdmi->mc_clkdis |= HDMI_MC_CLKDIS_HDCPCLK_DISABLE |
  1339. HDMI_MC_CLKDIS_CSCCLK_DISABLE |
  1340. HDMI_MC_CLKDIS_AUDCLK_DISABLE |
  1341. HDMI_MC_CLKDIS_PREPCLK_DISABLE |
  1342. HDMI_MC_CLKDIS_TMDSCLK_DISABLE;
  1343. hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_PIXELCLK_DISABLE;
  1344. hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);
  1345. hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_TMDSCLK_DISABLE;
  1346. hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);
  1347. /* Enable csc path */
  1348. if (is_color_space_conversion(hdmi)) {
  1349. hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_CSCCLK_DISABLE;
  1350. hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);
  1351. }
  1352. /* Enable color space conversion if needed */
  1353. if (is_color_space_conversion(hdmi))
  1354. hdmi_writeb(hdmi, HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_IN_PATH,
  1355. HDMI_MC_FLOWCTRL);
  1356. else
  1357. hdmi_writeb(hdmi, HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_BYPASS,
  1358. HDMI_MC_FLOWCTRL);
  1359. }
  1360. /* Workaround to clear the overflow condition */
  1361. static void dw_hdmi_clear_overflow(struct dw_hdmi *hdmi)
  1362. {
  1363. unsigned int count;
  1364. unsigned int i;
  1365. u8 val;
  1366. /*
  1367. * Under some circumstances the Frame Composer arithmetic unit can miss
  1368. * an FC register write due to being busy processing the previous one.
  1369. * The issue can be worked around by issuing a TMDS software reset and
  1370. * then write one of the FC registers several times.
  1371. *
  1372. * The number of iterations matters and depends on the HDMI TX revision
  1373. * (and possibly on the platform). So far i.MX6Q (v1.30a), i.MX6DL
  1374. * (v1.31a) and multiple Allwinner SoCs (v1.32a) have been identified
  1375. * as needing the workaround, with 4 iterations for v1.30a and 1
  1376. * iteration for others.
  1377. * The Amlogic Meson GX SoCs (v2.01a) have been identified as needing
  1378. * the workaround with a single iteration.
  1379. */
  1380. switch (hdmi->version) {
  1381. case 0x130a:
  1382. count = 4;
  1383. break;
  1384. case 0x131a:
  1385. case 0x132a:
  1386. case 0x201a:
  1387. count = 1;
  1388. break;
  1389. default:
  1390. return;
  1391. }
  1392. /* TMDS software reset */
  1393. hdmi_writeb(hdmi, (u8)~HDMI_MC_SWRSTZ_TMDSSWRST_REQ, HDMI_MC_SWRSTZ);
  1394. val = hdmi_readb(hdmi, HDMI_FC_INVIDCONF);
  1395. for (i = 0; i < count; i++)
  1396. hdmi_writeb(hdmi, val, HDMI_FC_INVIDCONF);
  1397. }
  1398. static void hdmi_disable_overflow_interrupts(struct dw_hdmi *hdmi)
  1399. {
  1400. hdmi_writeb(hdmi, HDMI_IH_MUTE_FC_STAT2_OVERFLOW_MASK,
  1401. HDMI_IH_MUTE_FC_STAT2);
  1402. }
  1403. static int dw_hdmi_setup(struct dw_hdmi *hdmi, struct drm_display_mode *mode)
  1404. {
  1405. int ret;
  1406. hdmi_disable_overflow_interrupts(hdmi);
  1407. hdmi->vic = drm_match_cea_mode(mode);
  1408. if (!hdmi->vic) {
  1409. dev_dbg(hdmi->dev, "Non-CEA mode used in HDMI\n");
  1410. } else {
  1411. dev_dbg(hdmi->dev, "CEA mode used vic=%d\n", hdmi->vic);
  1412. }
  1413. if ((hdmi->vic == 6) || (hdmi->vic == 7) ||
  1414. (hdmi->vic == 21) || (hdmi->vic == 22) ||
  1415. (hdmi->vic == 2) || (hdmi->vic == 3) ||
  1416. (hdmi->vic == 17) || (hdmi->vic == 18))
  1417. hdmi->hdmi_data.enc_out_encoding = V4L2_YCBCR_ENC_601;
  1418. else
  1419. hdmi->hdmi_data.enc_out_encoding = V4L2_YCBCR_ENC_709;
  1420. hdmi->hdmi_data.video_mode.mpixelrepetitionoutput = 0;
  1421. hdmi->hdmi_data.video_mode.mpixelrepetitioninput = 0;
  1422. /* TOFIX: Get input format from plat data or fallback to RGB888 */
  1423. if (hdmi->plat_data->input_bus_format)
  1424. hdmi->hdmi_data.enc_in_bus_format =
  1425. hdmi->plat_data->input_bus_format;
  1426. else
  1427. hdmi->hdmi_data.enc_in_bus_format = MEDIA_BUS_FMT_RGB888_1X24;
  1428. /* TOFIX: Get input encoding from plat data or fallback to none */
  1429. if (hdmi->plat_data->input_bus_encoding)
  1430. hdmi->hdmi_data.enc_in_encoding =
  1431. hdmi->plat_data->input_bus_encoding;
  1432. else
  1433. hdmi->hdmi_data.enc_in_encoding = V4L2_YCBCR_ENC_DEFAULT;
  1434. /* TOFIX: Default to RGB888 output format */
  1435. hdmi->hdmi_data.enc_out_bus_format = MEDIA_BUS_FMT_RGB888_1X24;
  1436. hdmi->hdmi_data.pix_repet_factor = 0;
  1437. hdmi->hdmi_data.hdcp_enable = 0;
  1438. hdmi->hdmi_data.video_mode.mdataenablepolarity = true;
  1439. /* HDMI Initialization Step B.1 */
  1440. hdmi_av_composer(hdmi, mode);
  1441. /* HDMI Initializateion Step B.2 */
  1442. ret = hdmi->phy.ops->init(hdmi, hdmi->phy.data, &hdmi->previous_mode);
  1443. if (ret)
  1444. return ret;
  1445. hdmi->phy.enabled = true;
  1446. /* HDMI Initialization Step B.3 */
  1447. dw_hdmi_enable_video_path(hdmi);
  1448. if (hdmi->sink_has_audio) {
  1449. dev_dbg(hdmi->dev, "sink has audio support\n");
  1450. /* HDMI Initialization Step E - Configure audio */
  1451. hdmi_clk_regenerator_update_pixel_clock(hdmi);
  1452. hdmi_enable_audio_clk(hdmi, true);
  1453. }
  1454. /* not for DVI mode */
  1455. if (hdmi->sink_is_hdmi) {
  1456. dev_dbg(hdmi->dev, "%s HDMI mode\n", __func__);
  1457. /* HDMI Initialization Step F - Configure AVI InfoFrame */
  1458. hdmi_config_AVI(hdmi, mode);
  1459. hdmi_config_vendor_specific_infoframe(hdmi, mode);
  1460. } else {
  1461. dev_dbg(hdmi->dev, "%s DVI mode\n", __func__);
  1462. }
  1463. hdmi_video_packetize(hdmi);
  1464. hdmi_video_csc(hdmi);
  1465. hdmi_video_sample(hdmi);
  1466. hdmi_tx_hdcp_config(hdmi);
  1467. dw_hdmi_clear_overflow(hdmi);
  1468. return 0;
  1469. }
  1470. static void dw_hdmi_setup_i2c(struct dw_hdmi *hdmi)
  1471. {
  1472. hdmi_writeb(hdmi, HDMI_PHY_I2CM_INT_ADDR_DONE_POL,
  1473. HDMI_PHY_I2CM_INT_ADDR);
  1474. hdmi_writeb(hdmi, HDMI_PHY_I2CM_CTLINT_ADDR_NAC_POL |
  1475. HDMI_PHY_I2CM_CTLINT_ADDR_ARBITRATION_POL,
  1476. HDMI_PHY_I2CM_CTLINT_ADDR);
  1477. }
  1478. static void initialize_hdmi_ih_mutes(struct dw_hdmi *hdmi)
  1479. {
  1480. u8 ih_mute;
  1481. /*
  1482. * Boot up defaults are:
  1483. * HDMI_IH_MUTE = 0x03 (disabled)
  1484. * HDMI_IH_MUTE_* = 0x00 (enabled)
  1485. *
  1486. * Disable top level interrupt bits in HDMI block
  1487. */
  1488. ih_mute = hdmi_readb(hdmi, HDMI_IH_MUTE) |
  1489. HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |
  1490. HDMI_IH_MUTE_MUTE_ALL_INTERRUPT;
  1491. hdmi_writeb(hdmi, ih_mute, HDMI_IH_MUTE);
  1492. /* by default mask all interrupts */
  1493. hdmi_writeb(hdmi, 0xff, HDMI_VP_MASK);
  1494. hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK0);
  1495. hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK1);
  1496. hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK2);
  1497. hdmi_writeb(hdmi, 0xff, HDMI_PHY_MASK0);
  1498. hdmi_writeb(hdmi, 0xff, HDMI_PHY_I2CM_INT_ADDR);
  1499. hdmi_writeb(hdmi, 0xff, HDMI_PHY_I2CM_CTLINT_ADDR);
  1500. hdmi_writeb(hdmi, 0xff, HDMI_AUD_INT);
  1501. hdmi_writeb(hdmi, 0xff, HDMI_AUD_SPDIFINT);
  1502. hdmi_writeb(hdmi, 0xff, HDMI_AUD_HBR_MASK);
  1503. hdmi_writeb(hdmi, 0xff, HDMI_GP_MASK);
  1504. hdmi_writeb(hdmi, 0xff, HDMI_A_APIINTMSK);
  1505. hdmi_writeb(hdmi, 0xff, HDMI_I2CM_INT);
  1506. hdmi_writeb(hdmi, 0xff, HDMI_I2CM_CTLINT);
  1507. /* Disable interrupts in the IH_MUTE_* registers */
  1508. hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT0);
  1509. hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT1);
  1510. hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT2);
  1511. hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_AS_STAT0);
  1512. hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_PHY_STAT0);
  1513. hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_I2CM_STAT0);
  1514. hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_CEC_STAT0);
  1515. hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_VP_STAT0);
  1516. hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_I2CMPHY_STAT0);
  1517. hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_AHBDMAAUD_STAT0);
  1518. /* Enable top level interrupt bits in HDMI block */
  1519. ih_mute &= ~(HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |
  1520. HDMI_IH_MUTE_MUTE_ALL_INTERRUPT);
  1521. hdmi_writeb(hdmi, ih_mute, HDMI_IH_MUTE);
  1522. }
  1523. static void dw_hdmi_poweron(struct dw_hdmi *hdmi)
  1524. {
  1525. hdmi->bridge_is_on = true;
  1526. dw_hdmi_setup(hdmi, &hdmi->previous_mode);
  1527. }
  1528. static void dw_hdmi_poweroff(struct dw_hdmi *hdmi)
  1529. {
  1530. if (hdmi->phy.enabled) {
  1531. hdmi->phy.ops->disable(hdmi, hdmi->phy.data);
  1532. hdmi->phy.enabled = false;
  1533. }
  1534. hdmi->bridge_is_on = false;
  1535. }
  1536. static void dw_hdmi_update_power(struct dw_hdmi *hdmi)
  1537. {
  1538. int force = hdmi->force;
  1539. if (hdmi->disabled) {
  1540. force = DRM_FORCE_OFF;
  1541. } else if (force == DRM_FORCE_UNSPECIFIED) {
  1542. if (hdmi->rxsense)
  1543. force = DRM_FORCE_ON;
  1544. else
  1545. force = DRM_FORCE_OFF;
  1546. }
  1547. if (force == DRM_FORCE_OFF) {
  1548. if (hdmi->bridge_is_on)
  1549. dw_hdmi_poweroff(hdmi);
  1550. } else {
  1551. if (!hdmi->bridge_is_on)
  1552. dw_hdmi_poweron(hdmi);
  1553. }
  1554. }
  1555. /*
  1556. * Adjust the detection of RXSENSE according to whether we have a forced
  1557. * connection mode enabled, or whether we have been disabled. There is
  1558. * no point processing RXSENSE interrupts if we have a forced connection
  1559. * state, or DRM has us disabled.
  1560. *
  1561. * We also disable rxsense interrupts when we think we're disconnected
  1562. * to avoid floating TDMS signals giving false rxsense interrupts.
  1563. *
  1564. * Note: we still need to listen for HPD interrupts even when DRM has us
  1565. * disabled so that we can detect a connect event.
  1566. */
  1567. static void dw_hdmi_update_phy_mask(struct dw_hdmi *hdmi)
  1568. {
  1569. if (hdmi->phy.ops->update_hpd)
  1570. hdmi->phy.ops->update_hpd(hdmi, hdmi->phy.data,
  1571. hdmi->force, hdmi->disabled,
  1572. hdmi->rxsense);
  1573. }
  1574. static enum drm_connector_status
  1575. dw_hdmi_connector_detect(struct drm_connector *connector, bool force)
  1576. {
  1577. struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
  1578. connector);
  1579. mutex_lock(&hdmi->mutex);
  1580. hdmi->force = DRM_FORCE_UNSPECIFIED;
  1581. dw_hdmi_update_power(hdmi);
  1582. dw_hdmi_update_phy_mask(hdmi);
  1583. mutex_unlock(&hdmi->mutex);
  1584. return hdmi->phy.ops->read_hpd(hdmi, hdmi->phy.data);
  1585. }
  1586. static int dw_hdmi_connector_get_modes(struct drm_connector *connector)
  1587. {
  1588. struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
  1589. connector);
  1590. struct edid *edid;
  1591. int ret = 0;
  1592. if (!hdmi->ddc)
  1593. return 0;
  1594. edid = drm_get_edid(connector, hdmi->ddc);
  1595. if (edid) {
  1596. dev_dbg(hdmi->dev, "got edid: width[%d] x height[%d]\n",
  1597. edid->width_cm, edid->height_cm);
  1598. hdmi->sink_is_hdmi = drm_detect_hdmi_monitor(edid);
  1599. hdmi->sink_has_audio = drm_detect_monitor_audio(edid);
  1600. drm_connector_update_edid_property(connector, edid);
  1601. cec_notifier_set_phys_addr_from_edid(hdmi->cec_notifier, edid);
  1602. ret = drm_add_edid_modes(connector, edid);
  1603. kfree(edid);
  1604. } else {
  1605. dev_dbg(hdmi->dev, "failed to get edid\n");
  1606. }
  1607. return ret;
  1608. }
  1609. static void dw_hdmi_connector_force(struct drm_connector *connector)
  1610. {
  1611. struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
  1612. connector);
  1613. mutex_lock(&hdmi->mutex);
  1614. hdmi->force = connector->force;
  1615. dw_hdmi_update_power(hdmi);
  1616. dw_hdmi_update_phy_mask(hdmi);
  1617. mutex_unlock(&hdmi->mutex);
  1618. }
  1619. static const struct drm_connector_funcs dw_hdmi_connector_funcs = {
  1620. .fill_modes = drm_helper_probe_single_connector_modes,
  1621. .detect = dw_hdmi_connector_detect,
  1622. .destroy = drm_connector_cleanup,
  1623. .force = dw_hdmi_connector_force,
  1624. .reset = drm_atomic_helper_connector_reset,
  1625. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  1626. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  1627. };
  1628. static const struct drm_connector_helper_funcs dw_hdmi_connector_helper_funcs = {
  1629. .get_modes = dw_hdmi_connector_get_modes,
  1630. .best_encoder = drm_atomic_helper_best_encoder,
  1631. };
  1632. static int dw_hdmi_bridge_attach(struct drm_bridge *bridge)
  1633. {
  1634. struct dw_hdmi *hdmi = bridge->driver_private;
  1635. struct drm_encoder *encoder = bridge->encoder;
  1636. struct drm_connector *connector = &hdmi->connector;
  1637. connector->interlace_allowed = 1;
  1638. connector->polled = DRM_CONNECTOR_POLL_HPD;
  1639. drm_connector_helper_add(connector, &dw_hdmi_connector_helper_funcs);
  1640. drm_connector_init(bridge->dev, connector, &dw_hdmi_connector_funcs,
  1641. DRM_MODE_CONNECTOR_HDMIA);
  1642. drm_connector_attach_encoder(connector, encoder);
  1643. return 0;
  1644. }
  1645. static enum drm_mode_status
  1646. dw_hdmi_bridge_mode_valid(struct drm_bridge *bridge,
  1647. const struct drm_display_mode *mode)
  1648. {
  1649. struct dw_hdmi *hdmi = bridge->driver_private;
  1650. struct drm_connector *connector = &hdmi->connector;
  1651. enum drm_mode_status mode_status = MODE_OK;
  1652. /* We don't support double-clocked modes */
  1653. if (mode->flags & DRM_MODE_FLAG_DBLCLK)
  1654. return MODE_BAD;
  1655. if (hdmi->plat_data->mode_valid)
  1656. mode_status = hdmi->plat_data->mode_valid(connector, mode);
  1657. return mode_status;
  1658. }
  1659. static void dw_hdmi_bridge_mode_set(struct drm_bridge *bridge,
  1660. struct drm_display_mode *orig_mode,
  1661. struct drm_display_mode *mode)
  1662. {
  1663. struct dw_hdmi *hdmi = bridge->driver_private;
  1664. mutex_lock(&hdmi->mutex);
  1665. /* Store the display mode for plugin/DKMS poweron events */
  1666. memcpy(&hdmi->previous_mode, mode, sizeof(hdmi->previous_mode));
  1667. mutex_unlock(&hdmi->mutex);
  1668. }
  1669. static void dw_hdmi_bridge_disable(struct drm_bridge *bridge)
  1670. {
  1671. struct dw_hdmi *hdmi = bridge->driver_private;
  1672. mutex_lock(&hdmi->mutex);
  1673. hdmi->disabled = true;
  1674. dw_hdmi_update_power(hdmi);
  1675. dw_hdmi_update_phy_mask(hdmi);
  1676. mutex_unlock(&hdmi->mutex);
  1677. }
  1678. static void dw_hdmi_bridge_enable(struct drm_bridge *bridge)
  1679. {
  1680. struct dw_hdmi *hdmi = bridge->driver_private;
  1681. mutex_lock(&hdmi->mutex);
  1682. hdmi->disabled = false;
  1683. dw_hdmi_update_power(hdmi);
  1684. dw_hdmi_update_phy_mask(hdmi);
  1685. mutex_unlock(&hdmi->mutex);
  1686. }
  1687. static const struct drm_bridge_funcs dw_hdmi_bridge_funcs = {
  1688. .attach = dw_hdmi_bridge_attach,
  1689. .enable = dw_hdmi_bridge_enable,
  1690. .disable = dw_hdmi_bridge_disable,
  1691. .mode_set = dw_hdmi_bridge_mode_set,
  1692. .mode_valid = dw_hdmi_bridge_mode_valid,
  1693. };
  1694. static irqreturn_t dw_hdmi_i2c_irq(struct dw_hdmi *hdmi)
  1695. {
  1696. struct dw_hdmi_i2c *i2c = hdmi->i2c;
  1697. unsigned int stat;
  1698. stat = hdmi_readb(hdmi, HDMI_IH_I2CM_STAT0);
  1699. if (!stat)
  1700. return IRQ_NONE;
  1701. hdmi_writeb(hdmi, stat, HDMI_IH_I2CM_STAT0);
  1702. i2c->stat = stat;
  1703. complete(&i2c->cmp);
  1704. return IRQ_HANDLED;
  1705. }
  1706. static irqreturn_t dw_hdmi_hardirq(int irq, void *dev_id)
  1707. {
  1708. struct dw_hdmi *hdmi = dev_id;
  1709. u8 intr_stat;
  1710. irqreturn_t ret = IRQ_NONE;
  1711. if (hdmi->i2c)
  1712. ret = dw_hdmi_i2c_irq(hdmi);
  1713. intr_stat = hdmi_readb(hdmi, HDMI_IH_PHY_STAT0);
  1714. if (intr_stat) {
  1715. hdmi_writeb(hdmi, ~0, HDMI_IH_MUTE_PHY_STAT0);
  1716. return IRQ_WAKE_THREAD;
  1717. }
  1718. return ret;
  1719. }
  1720. void dw_hdmi_setup_rx_sense(struct dw_hdmi *hdmi, bool hpd, bool rx_sense)
  1721. {
  1722. mutex_lock(&hdmi->mutex);
  1723. if (!hdmi->force) {
  1724. /*
  1725. * If the RX sense status indicates we're disconnected,
  1726. * clear the software rxsense status.
  1727. */
  1728. if (!rx_sense)
  1729. hdmi->rxsense = false;
  1730. /*
  1731. * Only set the software rxsense status when both
  1732. * rxsense and hpd indicates we're connected.
  1733. * This avoids what seems to be bad behaviour in
  1734. * at least iMX6S versions of the phy.
  1735. */
  1736. if (hpd)
  1737. hdmi->rxsense = true;
  1738. dw_hdmi_update_power(hdmi);
  1739. dw_hdmi_update_phy_mask(hdmi);
  1740. }
  1741. mutex_unlock(&hdmi->mutex);
  1742. }
  1743. EXPORT_SYMBOL_GPL(dw_hdmi_setup_rx_sense);
  1744. static irqreturn_t dw_hdmi_irq(int irq, void *dev_id)
  1745. {
  1746. struct dw_hdmi *hdmi = dev_id;
  1747. u8 intr_stat, phy_int_pol, phy_pol_mask, phy_stat;
  1748. intr_stat = hdmi_readb(hdmi, HDMI_IH_PHY_STAT0);
  1749. phy_int_pol = hdmi_readb(hdmi, HDMI_PHY_POL0);
  1750. phy_stat = hdmi_readb(hdmi, HDMI_PHY_STAT0);
  1751. phy_pol_mask = 0;
  1752. if (intr_stat & HDMI_IH_PHY_STAT0_HPD)
  1753. phy_pol_mask |= HDMI_PHY_HPD;
  1754. if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE0)
  1755. phy_pol_mask |= HDMI_PHY_RX_SENSE0;
  1756. if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE1)
  1757. phy_pol_mask |= HDMI_PHY_RX_SENSE1;
  1758. if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE2)
  1759. phy_pol_mask |= HDMI_PHY_RX_SENSE2;
  1760. if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE3)
  1761. phy_pol_mask |= HDMI_PHY_RX_SENSE3;
  1762. if (phy_pol_mask)
  1763. hdmi_modb(hdmi, ~phy_int_pol, phy_pol_mask, HDMI_PHY_POL0);
  1764. /*
  1765. * RX sense tells us whether the TDMS transmitters are detecting
  1766. * load - in other words, there's something listening on the
  1767. * other end of the link. Use this to decide whether we should
  1768. * power on the phy as HPD may be toggled by the sink to merely
  1769. * ask the source to re-read the EDID.
  1770. */
  1771. if (intr_stat &
  1772. (HDMI_IH_PHY_STAT0_RX_SENSE | HDMI_IH_PHY_STAT0_HPD)) {
  1773. dw_hdmi_setup_rx_sense(hdmi,
  1774. phy_stat & HDMI_PHY_HPD,
  1775. phy_stat & HDMI_PHY_RX_SENSE);
  1776. if ((phy_stat & (HDMI_PHY_RX_SENSE | HDMI_PHY_HPD)) == 0)
  1777. cec_notifier_set_phys_addr(hdmi->cec_notifier,
  1778. CEC_PHYS_ADDR_INVALID);
  1779. }
  1780. if (intr_stat & HDMI_IH_PHY_STAT0_HPD) {
  1781. dev_dbg(hdmi->dev, "EVENT=%s\n",
  1782. phy_int_pol & HDMI_PHY_HPD ? "plugin" : "plugout");
  1783. if (hdmi->bridge.dev)
  1784. drm_helper_hpd_irq_event(hdmi->bridge.dev);
  1785. }
  1786. hdmi_writeb(hdmi, intr_stat, HDMI_IH_PHY_STAT0);
  1787. hdmi_writeb(hdmi, ~(HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE),
  1788. HDMI_IH_MUTE_PHY_STAT0);
  1789. return IRQ_HANDLED;
  1790. }
  1791. static const struct dw_hdmi_phy_data dw_hdmi_phys[] = {
  1792. {
  1793. .type = DW_HDMI_PHY_DWC_HDMI_TX_PHY,
  1794. .name = "DWC HDMI TX PHY",
  1795. .gen = 1,
  1796. }, {
  1797. .type = DW_HDMI_PHY_DWC_MHL_PHY_HEAC,
  1798. .name = "DWC MHL PHY + HEAC PHY",
  1799. .gen = 2,
  1800. .has_svsret = true,
  1801. .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
  1802. }, {
  1803. .type = DW_HDMI_PHY_DWC_MHL_PHY,
  1804. .name = "DWC MHL PHY",
  1805. .gen = 2,
  1806. .has_svsret = true,
  1807. .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
  1808. }, {
  1809. .type = DW_HDMI_PHY_DWC_HDMI_3D_TX_PHY_HEAC,
  1810. .name = "DWC HDMI 3D TX PHY + HEAC PHY",
  1811. .gen = 2,
  1812. .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
  1813. }, {
  1814. .type = DW_HDMI_PHY_DWC_HDMI_3D_TX_PHY,
  1815. .name = "DWC HDMI 3D TX PHY",
  1816. .gen = 2,
  1817. .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
  1818. }, {
  1819. .type = DW_HDMI_PHY_DWC_HDMI20_TX_PHY,
  1820. .name = "DWC HDMI 2.0 TX PHY",
  1821. .gen = 2,
  1822. .has_svsret = true,
  1823. .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
  1824. }, {
  1825. .type = DW_HDMI_PHY_VENDOR_PHY,
  1826. .name = "Vendor PHY",
  1827. }
  1828. };
  1829. static int dw_hdmi_detect_phy(struct dw_hdmi *hdmi)
  1830. {
  1831. unsigned int i;
  1832. u8 phy_type;
  1833. phy_type = hdmi_readb(hdmi, HDMI_CONFIG2_ID);
  1834. if (phy_type == DW_HDMI_PHY_VENDOR_PHY) {
  1835. /* Vendor PHYs require support from the glue layer. */
  1836. if (!hdmi->plat_data->phy_ops || !hdmi->plat_data->phy_name) {
  1837. dev_err(hdmi->dev,
  1838. "Vendor HDMI PHY not supported by glue layer\n");
  1839. return -ENODEV;
  1840. }
  1841. hdmi->phy.ops = hdmi->plat_data->phy_ops;
  1842. hdmi->phy.data = hdmi->plat_data->phy_data;
  1843. hdmi->phy.name = hdmi->plat_data->phy_name;
  1844. return 0;
  1845. }
  1846. /* Synopsys PHYs are handled internally. */
  1847. for (i = 0; i < ARRAY_SIZE(dw_hdmi_phys); ++i) {
  1848. if (dw_hdmi_phys[i].type == phy_type) {
  1849. hdmi->phy.ops = &dw_hdmi_synopsys_phy_ops;
  1850. hdmi->phy.name = dw_hdmi_phys[i].name;
  1851. hdmi->phy.data = (void *)&dw_hdmi_phys[i];
  1852. if (!dw_hdmi_phys[i].configure &&
  1853. !hdmi->plat_data->configure_phy) {
  1854. dev_err(hdmi->dev, "%s requires platform support\n",
  1855. hdmi->phy.name);
  1856. return -ENODEV;
  1857. }
  1858. return 0;
  1859. }
  1860. }
  1861. dev_err(hdmi->dev, "Unsupported HDMI PHY type (%02x)\n", phy_type);
  1862. return -ENODEV;
  1863. }
  1864. static void dw_hdmi_cec_enable(struct dw_hdmi *hdmi)
  1865. {
  1866. mutex_lock(&hdmi->mutex);
  1867. hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_CECCLK_DISABLE;
  1868. hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);
  1869. mutex_unlock(&hdmi->mutex);
  1870. }
  1871. static void dw_hdmi_cec_disable(struct dw_hdmi *hdmi)
  1872. {
  1873. mutex_lock(&hdmi->mutex);
  1874. hdmi->mc_clkdis |= HDMI_MC_CLKDIS_CECCLK_DISABLE;
  1875. hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);
  1876. mutex_unlock(&hdmi->mutex);
  1877. }
  1878. static const struct dw_hdmi_cec_ops dw_hdmi_cec_ops = {
  1879. .write = hdmi_writeb,
  1880. .read = hdmi_readb,
  1881. .enable = dw_hdmi_cec_enable,
  1882. .disable = dw_hdmi_cec_disable,
  1883. };
  1884. static const struct regmap_config hdmi_regmap_8bit_config = {
  1885. .reg_bits = 32,
  1886. .val_bits = 8,
  1887. .reg_stride = 1,
  1888. .max_register = HDMI_I2CM_FS_SCL_LCNT_0_ADDR,
  1889. };
  1890. static const struct regmap_config hdmi_regmap_32bit_config = {
  1891. .reg_bits = 32,
  1892. .val_bits = 32,
  1893. .reg_stride = 4,
  1894. .max_register = HDMI_I2CM_FS_SCL_LCNT_0_ADDR << 2,
  1895. };
  1896. static struct dw_hdmi *
  1897. __dw_hdmi_probe(struct platform_device *pdev,
  1898. const struct dw_hdmi_plat_data *plat_data)
  1899. {
  1900. struct device *dev = &pdev->dev;
  1901. struct device_node *np = dev->of_node;
  1902. struct platform_device_info pdevinfo;
  1903. struct device_node *ddc_node;
  1904. struct dw_hdmi_cec_data cec;
  1905. struct dw_hdmi *hdmi;
  1906. struct resource *iores = NULL;
  1907. int irq;
  1908. int ret;
  1909. u32 val = 1;
  1910. u8 prod_id0;
  1911. u8 prod_id1;
  1912. u8 config0;
  1913. u8 config3;
  1914. hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
  1915. if (!hdmi)
  1916. return ERR_PTR(-ENOMEM);
  1917. hdmi->plat_data = plat_data;
  1918. hdmi->dev = dev;
  1919. hdmi->sample_rate = 48000;
  1920. hdmi->disabled = true;
  1921. hdmi->rxsense = true;
  1922. hdmi->phy_mask = (u8)~(HDMI_PHY_HPD | HDMI_PHY_RX_SENSE);
  1923. hdmi->mc_clkdis = 0x7f;
  1924. mutex_init(&hdmi->mutex);
  1925. mutex_init(&hdmi->audio_mutex);
  1926. spin_lock_init(&hdmi->audio_lock);
  1927. ddc_node = of_parse_phandle(np, "ddc-i2c-bus", 0);
  1928. if (ddc_node) {
  1929. hdmi->ddc = of_get_i2c_adapter_by_node(ddc_node);
  1930. of_node_put(ddc_node);
  1931. if (!hdmi->ddc) {
  1932. dev_dbg(hdmi->dev, "failed to read ddc node\n");
  1933. return ERR_PTR(-EPROBE_DEFER);
  1934. }
  1935. } else {
  1936. dev_dbg(hdmi->dev, "no ddc property found\n");
  1937. }
  1938. if (!plat_data->regm) {
  1939. const struct regmap_config *reg_config;
  1940. of_property_read_u32(np, "reg-io-width", &val);
  1941. switch (val) {
  1942. case 4:
  1943. reg_config = &hdmi_regmap_32bit_config;
  1944. hdmi->reg_shift = 2;
  1945. break;
  1946. case 1:
  1947. reg_config = &hdmi_regmap_8bit_config;
  1948. break;
  1949. default:
  1950. dev_err(dev, "reg-io-width must be 1 or 4\n");
  1951. return ERR_PTR(-EINVAL);
  1952. }
  1953. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1954. hdmi->regs = devm_ioremap_resource(dev, iores);
  1955. if (IS_ERR(hdmi->regs)) {
  1956. ret = PTR_ERR(hdmi->regs);
  1957. goto err_res;
  1958. }
  1959. hdmi->regm = devm_regmap_init_mmio(dev, hdmi->regs, reg_config);
  1960. if (IS_ERR(hdmi->regm)) {
  1961. dev_err(dev, "Failed to configure regmap\n");
  1962. ret = PTR_ERR(hdmi->regm);
  1963. goto err_res;
  1964. }
  1965. } else {
  1966. hdmi->regm = plat_data->regm;
  1967. }
  1968. hdmi->isfr_clk = devm_clk_get(hdmi->dev, "isfr");
  1969. if (IS_ERR(hdmi->isfr_clk)) {
  1970. ret = PTR_ERR(hdmi->isfr_clk);
  1971. dev_err(hdmi->dev, "Unable to get HDMI isfr clk: %d\n", ret);
  1972. goto err_res;
  1973. }
  1974. ret = clk_prepare_enable(hdmi->isfr_clk);
  1975. if (ret) {
  1976. dev_err(hdmi->dev, "Cannot enable HDMI isfr clock: %d\n", ret);
  1977. goto err_res;
  1978. }
  1979. hdmi->iahb_clk = devm_clk_get(hdmi->dev, "iahb");
  1980. if (IS_ERR(hdmi->iahb_clk)) {
  1981. ret = PTR_ERR(hdmi->iahb_clk);
  1982. dev_err(hdmi->dev, "Unable to get HDMI iahb clk: %d\n", ret);
  1983. goto err_isfr;
  1984. }
  1985. ret = clk_prepare_enable(hdmi->iahb_clk);
  1986. if (ret) {
  1987. dev_err(hdmi->dev, "Cannot enable HDMI iahb clock: %d\n", ret);
  1988. goto err_isfr;
  1989. }
  1990. hdmi->cec_clk = devm_clk_get(hdmi->dev, "cec");
  1991. if (PTR_ERR(hdmi->cec_clk) == -ENOENT) {
  1992. hdmi->cec_clk = NULL;
  1993. } else if (IS_ERR(hdmi->cec_clk)) {
  1994. ret = PTR_ERR(hdmi->cec_clk);
  1995. if (ret != -EPROBE_DEFER)
  1996. dev_err(hdmi->dev, "Cannot get HDMI cec clock: %d\n",
  1997. ret);
  1998. hdmi->cec_clk = NULL;
  1999. goto err_iahb;
  2000. } else {
  2001. ret = clk_prepare_enable(hdmi->cec_clk);
  2002. if (ret) {
  2003. dev_err(hdmi->dev, "Cannot enable HDMI cec clock: %d\n",
  2004. ret);
  2005. goto err_iahb;
  2006. }
  2007. }
  2008. /* Product and revision IDs */
  2009. hdmi->version = (hdmi_readb(hdmi, HDMI_DESIGN_ID) << 8)
  2010. | (hdmi_readb(hdmi, HDMI_REVISION_ID) << 0);
  2011. prod_id0 = hdmi_readb(hdmi, HDMI_PRODUCT_ID0);
  2012. prod_id1 = hdmi_readb(hdmi, HDMI_PRODUCT_ID1);
  2013. if (prod_id0 != HDMI_PRODUCT_ID0_HDMI_TX ||
  2014. (prod_id1 & ~HDMI_PRODUCT_ID1_HDCP) != HDMI_PRODUCT_ID1_HDMI_TX) {
  2015. dev_err(dev, "Unsupported HDMI controller (%04x:%02x:%02x)\n",
  2016. hdmi->version, prod_id0, prod_id1);
  2017. ret = -ENODEV;
  2018. goto err_iahb;
  2019. }
  2020. ret = dw_hdmi_detect_phy(hdmi);
  2021. if (ret < 0)
  2022. goto err_iahb;
  2023. dev_info(dev, "Detected HDMI TX controller v%x.%03x %s HDCP (%s)\n",
  2024. hdmi->version >> 12, hdmi->version & 0xfff,
  2025. prod_id1 & HDMI_PRODUCT_ID1_HDCP ? "with" : "without",
  2026. hdmi->phy.name);
  2027. initialize_hdmi_ih_mutes(hdmi);
  2028. irq = platform_get_irq(pdev, 0);
  2029. if (irq < 0) {
  2030. ret = irq;
  2031. goto err_iahb;
  2032. }
  2033. ret = devm_request_threaded_irq(dev, irq, dw_hdmi_hardirq,
  2034. dw_hdmi_irq, IRQF_SHARED,
  2035. dev_name(dev), hdmi);
  2036. if (ret)
  2037. goto err_iahb;
  2038. hdmi->cec_notifier = cec_notifier_get(dev);
  2039. if (!hdmi->cec_notifier) {
  2040. ret = -ENOMEM;
  2041. goto err_iahb;
  2042. }
  2043. /*
  2044. * To prevent overflows in HDMI_IH_FC_STAT2, set the clk regenerator
  2045. * N and cts values before enabling phy
  2046. */
  2047. hdmi_init_clk_regenerator(hdmi);
  2048. /* If DDC bus is not specified, try to register HDMI I2C bus */
  2049. if (!hdmi->ddc) {
  2050. hdmi->ddc = dw_hdmi_i2c_adapter(hdmi);
  2051. if (IS_ERR(hdmi->ddc))
  2052. hdmi->ddc = NULL;
  2053. }
  2054. hdmi->bridge.driver_private = hdmi;
  2055. hdmi->bridge.funcs = &dw_hdmi_bridge_funcs;
  2056. #ifdef CONFIG_OF
  2057. hdmi->bridge.of_node = pdev->dev.of_node;
  2058. #endif
  2059. dw_hdmi_setup_i2c(hdmi);
  2060. if (hdmi->phy.ops->setup_hpd)
  2061. hdmi->phy.ops->setup_hpd(hdmi, hdmi->phy.data);
  2062. memset(&pdevinfo, 0, sizeof(pdevinfo));
  2063. pdevinfo.parent = dev;
  2064. pdevinfo.id = PLATFORM_DEVID_AUTO;
  2065. config0 = hdmi_readb(hdmi, HDMI_CONFIG0_ID);
  2066. config3 = hdmi_readb(hdmi, HDMI_CONFIG3_ID);
  2067. if (iores && config3 & HDMI_CONFIG3_AHBAUDDMA) {
  2068. struct dw_hdmi_audio_data audio;
  2069. audio.phys = iores->start;
  2070. audio.base = hdmi->regs;
  2071. audio.irq = irq;
  2072. audio.hdmi = hdmi;
  2073. audio.eld = hdmi->connector.eld;
  2074. hdmi->enable_audio = dw_hdmi_ahb_audio_enable;
  2075. hdmi->disable_audio = dw_hdmi_ahb_audio_disable;
  2076. pdevinfo.name = "dw-hdmi-ahb-audio";
  2077. pdevinfo.data = &audio;
  2078. pdevinfo.size_data = sizeof(audio);
  2079. pdevinfo.dma_mask = DMA_BIT_MASK(32);
  2080. hdmi->audio = platform_device_register_full(&pdevinfo);
  2081. } else if (config0 & HDMI_CONFIG0_I2S) {
  2082. struct dw_hdmi_i2s_audio_data audio;
  2083. audio.hdmi = hdmi;
  2084. audio.write = hdmi_writeb;
  2085. audio.read = hdmi_readb;
  2086. hdmi->enable_audio = dw_hdmi_i2s_audio_enable;
  2087. hdmi->disable_audio = dw_hdmi_i2s_audio_disable;
  2088. pdevinfo.name = "dw-hdmi-i2s-audio";
  2089. pdevinfo.data = &audio;
  2090. pdevinfo.size_data = sizeof(audio);
  2091. pdevinfo.dma_mask = DMA_BIT_MASK(32);
  2092. hdmi->audio = platform_device_register_full(&pdevinfo);
  2093. }
  2094. if (config0 & HDMI_CONFIG0_CEC) {
  2095. cec.hdmi = hdmi;
  2096. cec.ops = &dw_hdmi_cec_ops;
  2097. cec.irq = irq;
  2098. pdevinfo.name = "dw-hdmi-cec";
  2099. pdevinfo.data = &cec;
  2100. pdevinfo.size_data = sizeof(cec);
  2101. pdevinfo.dma_mask = 0;
  2102. hdmi->cec = platform_device_register_full(&pdevinfo);
  2103. }
  2104. /* Reset HDMI DDC I2C master controller and mute I2CM interrupts */
  2105. if (hdmi->i2c)
  2106. dw_hdmi_i2c_init(hdmi);
  2107. return hdmi;
  2108. err_iahb:
  2109. if (hdmi->i2c) {
  2110. i2c_del_adapter(&hdmi->i2c->adap);
  2111. hdmi->ddc = NULL;
  2112. }
  2113. if (hdmi->cec_notifier)
  2114. cec_notifier_put(hdmi->cec_notifier);
  2115. clk_disable_unprepare(hdmi->iahb_clk);
  2116. if (hdmi->cec_clk)
  2117. clk_disable_unprepare(hdmi->cec_clk);
  2118. err_isfr:
  2119. clk_disable_unprepare(hdmi->isfr_clk);
  2120. err_res:
  2121. i2c_put_adapter(hdmi->ddc);
  2122. return ERR_PTR(ret);
  2123. }
  2124. static void __dw_hdmi_remove(struct dw_hdmi *hdmi)
  2125. {
  2126. if (hdmi->audio && !IS_ERR(hdmi->audio))
  2127. platform_device_unregister(hdmi->audio);
  2128. if (!IS_ERR(hdmi->cec))
  2129. platform_device_unregister(hdmi->cec);
  2130. /* Disable all interrupts */
  2131. hdmi_writeb(hdmi, ~0, HDMI_IH_MUTE_PHY_STAT0);
  2132. if (hdmi->cec_notifier)
  2133. cec_notifier_put(hdmi->cec_notifier);
  2134. clk_disable_unprepare(hdmi->iahb_clk);
  2135. clk_disable_unprepare(hdmi->isfr_clk);
  2136. if (hdmi->cec_clk)
  2137. clk_disable_unprepare(hdmi->cec_clk);
  2138. if (hdmi->i2c)
  2139. i2c_del_adapter(&hdmi->i2c->adap);
  2140. else
  2141. i2c_put_adapter(hdmi->ddc);
  2142. }
  2143. /* -----------------------------------------------------------------------------
  2144. * Probe/remove API, used from platforms based on the DRM bridge API.
  2145. */
  2146. struct dw_hdmi *dw_hdmi_probe(struct platform_device *pdev,
  2147. const struct dw_hdmi_plat_data *plat_data)
  2148. {
  2149. struct dw_hdmi *hdmi;
  2150. hdmi = __dw_hdmi_probe(pdev, plat_data);
  2151. if (IS_ERR(hdmi))
  2152. return hdmi;
  2153. drm_bridge_add(&hdmi->bridge);
  2154. return hdmi;
  2155. }
  2156. EXPORT_SYMBOL_GPL(dw_hdmi_probe);
  2157. void dw_hdmi_remove(struct dw_hdmi *hdmi)
  2158. {
  2159. drm_bridge_remove(&hdmi->bridge);
  2160. __dw_hdmi_remove(hdmi);
  2161. }
  2162. EXPORT_SYMBOL_GPL(dw_hdmi_remove);
  2163. /* -----------------------------------------------------------------------------
  2164. * Bind/unbind API, used from platforms based on the component framework.
  2165. */
  2166. struct dw_hdmi *dw_hdmi_bind(struct platform_device *pdev,
  2167. struct drm_encoder *encoder,
  2168. const struct dw_hdmi_plat_data *plat_data)
  2169. {
  2170. struct dw_hdmi *hdmi;
  2171. int ret;
  2172. hdmi = __dw_hdmi_probe(pdev, plat_data);
  2173. if (IS_ERR(hdmi))
  2174. return hdmi;
  2175. ret = drm_bridge_attach(encoder, &hdmi->bridge, NULL);
  2176. if (ret) {
  2177. dw_hdmi_remove(hdmi);
  2178. DRM_ERROR("Failed to initialize bridge with drm\n");
  2179. return ERR_PTR(ret);
  2180. }
  2181. return hdmi;
  2182. }
  2183. EXPORT_SYMBOL_GPL(dw_hdmi_bind);
  2184. void dw_hdmi_unbind(struct dw_hdmi *hdmi)
  2185. {
  2186. __dw_hdmi_remove(hdmi);
  2187. }
  2188. EXPORT_SYMBOL_GPL(dw_hdmi_unbind);
  2189. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
  2190. MODULE_AUTHOR("Andy Yan <andy.yan@rock-chips.com>");
  2191. MODULE_AUTHOR("Yakir Yang <ykk@rock-chips.com>");
  2192. MODULE_AUTHOR("Vladimir Zapolskiy <vladimir_zapolskiy@mentor.com>");
  2193. MODULE_DESCRIPTION("DW HDMI transmitter driver");
  2194. MODULE_LICENSE("GPL");
  2195. MODULE_ALIAS("platform:dw-hdmi");