dw-hdmi.c 50 KB

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