dw_hdmi.c 51 KB

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