dw-hdmi.c 60 KB

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