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