mtk_hdmi_v2.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * MediaTek HDMI v2 IP driver
  4. *
  5. * Copyright (c) 2022 MediaTek Inc.
  6. * Copyright (c) 2022 BayLibre, SAS
  7. * Copyright (c) 2024 Collabora Ltd.
  8. * AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
  9. */
  10. #include <linux/bitfield.h>
  11. #include <linux/clk.h>
  12. #include <linux/debugfs.h>
  13. #include <linux/delay.h>
  14. #include <linux/device.h>
  15. #include <linux/err.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/irq.h>
  18. #include <linux/kernel.h>
  19. #include <linux/mutex.h>
  20. #include <linux/of.h>
  21. #include <linux/of_platform.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/regmap.h>
  24. #include <linux/suspend.h>
  25. #include <linux/units.h>
  26. #include <linux/phy/phy.h>
  27. #include <drm/display/drm_hdmi_helper.h>
  28. #include <drm/display/drm_hdmi_state_helper.h>
  29. #include <drm/display/drm_scdc_helper.h>
  30. #include <drm/drm_drv.h>
  31. #include <drm/drm_edid.h>
  32. #include <drm/drm_print.h>
  33. #include <drm/drm_probe_helper.h>
  34. #include "mtk_hdmi_common.h"
  35. #include "mtk_hdmi_regs_v2.h"
  36. #define MTK_HDMI_V2_CLOCK_MIN 27000
  37. #define MTK_HDMI_V2_CLOCK_MAX 594000
  38. #define HPD_PORD_HWIRQS (HTPLG_R_INT | HTPLG_F_INT | PORD_F_INT | PORD_R_INT)
  39. enum mtk_hdmi_v2_clk_id {
  40. MTK_HDMI_V2_CLK_HDCP_SEL,
  41. MTK_HDMI_V2_CLK_HDCP_24M_SEL,
  42. MTK_HDMI_V2_CLK_VPP_SPLIT_HDMI,
  43. MTK_HDMI_V2_CLK_HDMI_APB_SEL,
  44. MTK_HDMI_V2_CLK_COUNT,
  45. };
  46. const char *const mtk_hdmi_v2_clk_names[MTK_HDMI_V2_CLK_COUNT] = {
  47. [MTK_HDMI_V2_CLK_HDMI_APB_SEL] = "bus",
  48. [MTK_HDMI_V2_CLK_HDCP_SEL] = "hdcp",
  49. [MTK_HDMI_V2_CLK_HDCP_24M_SEL] = "hdcp24m",
  50. [MTK_HDMI_V2_CLK_VPP_SPLIT_HDMI] = "hdmi-split",
  51. };
  52. static inline void mtk_hdmi_v2_hwirq_disable(struct mtk_hdmi *hdmi)
  53. {
  54. regmap_write(hdmi->regs, TOP_INT_ENABLE00, 0);
  55. regmap_write(hdmi->regs, TOP_INT_ENABLE01, 0);
  56. }
  57. static inline void mtk_hdmi_v2_enable_hpd_pord_irq(struct mtk_hdmi *hdmi, bool enable)
  58. {
  59. if (enable)
  60. regmap_set_bits(hdmi->regs, TOP_INT_ENABLE00, HPD_PORD_HWIRQS);
  61. else
  62. regmap_clear_bits(hdmi->regs, TOP_INT_ENABLE00, HPD_PORD_HWIRQS);
  63. }
  64. static inline void mtk_hdmi_v2_set_sw_hpd(struct mtk_hdmi *hdmi, bool enable)
  65. {
  66. if (enable) {
  67. regmap_set_bits(hdmi->regs, hdmi->conf->reg_hdmi_tx_cfg, HDMITX_SW_HPD);
  68. regmap_set_bits(hdmi->regs, HDCP2X_CTRL_0, HDCP2X_HPD_OVR);
  69. regmap_set_bits(hdmi->regs, HDCP2X_CTRL_0, HDCP2X_HPD_SW);
  70. } else {
  71. regmap_clear_bits(hdmi->regs, HDCP2X_CTRL_0, HDCP2X_HPD_OVR);
  72. regmap_clear_bits(hdmi->regs, HDCP2X_CTRL_0, HDCP2X_HPD_SW);
  73. regmap_clear_bits(hdmi->regs, hdmi->conf->reg_hdmi_tx_cfg, HDMITX_SW_HPD);
  74. }
  75. }
  76. static inline void mtk_hdmi_v2_enable_scrambling(struct mtk_hdmi *hdmi, bool enable)
  77. {
  78. struct drm_scdc *scdc = &hdmi->curr_conn->display_info.hdmi.scdc;
  79. if (enable)
  80. regmap_set_bits(hdmi->regs, TOP_CFG00, SCR_ON | HDMI2_ON);
  81. else
  82. regmap_clear_bits(hdmi->regs, TOP_CFG00, SCR_ON | HDMI2_ON);
  83. if (scdc->supported) {
  84. if (scdc->scrambling.supported)
  85. drm_scdc_set_scrambling(hdmi->curr_conn, enable);
  86. drm_scdc_set_high_tmds_clock_ratio(hdmi->curr_conn, enable);
  87. }
  88. }
  89. static void mtk_hdmi_v2_hw_vid_mute(struct mtk_hdmi *hdmi, bool enable)
  90. {
  91. /* If enabled, sends a black image */
  92. if (enable)
  93. regmap_set_bits(hdmi->regs, TOP_VMUTE_CFG1, REG_VMUTE_EN);
  94. else
  95. regmap_clear_bits(hdmi->regs, TOP_VMUTE_CFG1, REG_VMUTE_EN);
  96. }
  97. static void mtk_hdmi_v2_hw_aud_mute(struct mtk_hdmi *hdmi, bool enable)
  98. {
  99. u32 aip, val;
  100. if (!enable) {
  101. regmap_clear_bits(hdmi->regs, AIP_TXCTRL, AUD_MUTE_FIFO_EN);
  102. return;
  103. }
  104. regmap_read(hdmi->regs, AIP_CTRL, &aip);
  105. val = AUD_MUTE_FIFO_EN;
  106. if (aip & DSD_EN)
  107. val |= DSD_MUTE_EN;
  108. regmap_update_bits(hdmi->regs, AIP_TXCTRL, val, val);
  109. }
  110. static void mtk_hdmi_v2_hw_reset(struct mtk_hdmi *hdmi)
  111. {
  112. regmap_clear_bits(hdmi->regs, hdmi->conf->reg_hdmi_tx_cfg, HDMITX_SW_RSTB);
  113. udelay(5);
  114. regmap_set_bits(hdmi->regs, hdmi->conf->reg_hdmi_tx_cfg, HDMITX_SW_RSTB);
  115. }
  116. static inline u32 mtk_hdmi_v2_format_hw_packet(const u8 *buffer, u8 len)
  117. {
  118. unsigned short i;
  119. u32 val = 0;
  120. for (i = 0; i < len; i++)
  121. val |= buffer[i] << (i * 8);
  122. return val;
  123. }
  124. static int mtk_hdmi_v2_hdmi_write_audio_infoframe(struct drm_bridge *bridge,
  125. const u8 *buffer, size_t len)
  126. {
  127. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  128. regmap_clear_bits(hdmi->regs, TOP_INFO_EN, AUD_EN | AUD_EN_WR);
  129. regmap_clear_bits(hdmi->regs, TOP_INFO_RPT, AUD_RPT_EN);
  130. regmap_write(hdmi->regs, TOP_AIF_HEADER, mtk_hdmi_v2_format_hw_packet(&buffer[0], 3));
  131. regmap_write(hdmi->regs, TOP_AIF_PKT00, mtk_hdmi_v2_format_hw_packet(&buffer[3], 3));
  132. regmap_write(hdmi->regs, TOP_AIF_PKT01, mtk_hdmi_v2_format_hw_packet(&buffer[7], 2));
  133. regmap_write(hdmi->regs, TOP_AIF_PKT02, 0);
  134. regmap_write(hdmi->regs, TOP_AIF_PKT03, 0);
  135. regmap_set_bits(hdmi->regs, TOP_INFO_RPT, AUD_RPT_EN);
  136. regmap_set_bits(hdmi->regs, TOP_INFO_EN, AUD_EN | AUD_EN_WR);
  137. return 0;
  138. }
  139. static int mtk_hdmi_v2_hdmi_write_avi_infoframe(struct drm_bridge *bridge,
  140. const u8 *buffer, size_t len)
  141. {
  142. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  143. regmap_clear_bits(hdmi->regs, TOP_INFO_EN, AVI_EN_WR | AVI_EN);
  144. regmap_clear_bits(hdmi->regs, TOP_INFO_RPT, AVI_RPT_EN);
  145. regmap_write(hdmi->regs, TOP_AVI_HEADER, mtk_hdmi_v2_format_hw_packet(&buffer[0], 3));
  146. regmap_write(hdmi->regs, TOP_AVI_PKT00, mtk_hdmi_v2_format_hw_packet(&buffer[3], 4));
  147. regmap_write(hdmi->regs, TOP_AVI_PKT01, mtk_hdmi_v2_format_hw_packet(&buffer[7], 3));
  148. regmap_write(hdmi->regs, TOP_AVI_PKT02, mtk_hdmi_v2_format_hw_packet(&buffer[10], 4));
  149. regmap_write(hdmi->regs, TOP_AVI_PKT03, mtk_hdmi_v2_format_hw_packet(&buffer[14], 3));
  150. regmap_write(hdmi->regs, TOP_AVI_PKT04, 0);
  151. regmap_write(hdmi->regs, TOP_AVI_PKT05, 0);
  152. regmap_set_bits(hdmi->regs, TOP_INFO_RPT, AVI_RPT_EN);
  153. regmap_set_bits(hdmi->regs, TOP_INFO_EN, AVI_EN_WR | AVI_EN);
  154. return 0;
  155. }
  156. static int mtk_hdmi_v2_hdmi_write_spd_infoframe(struct drm_bridge *bridge,
  157. const u8 *buffer, size_t len)
  158. {
  159. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  160. regmap_clear_bits(hdmi->regs, TOP_INFO_EN, SPD_EN_WR | SPD_EN);
  161. regmap_clear_bits(hdmi->regs, TOP_INFO_RPT, SPD_RPT_EN);
  162. regmap_write(hdmi->regs, TOP_SPDIF_HEADER, mtk_hdmi_v2_format_hw_packet(&buffer[0], 3));
  163. regmap_write(hdmi->regs, TOP_SPDIF_PKT00, mtk_hdmi_v2_format_hw_packet(&buffer[3], 4));
  164. regmap_write(hdmi->regs, TOP_SPDIF_PKT01, mtk_hdmi_v2_format_hw_packet(&buffer[7], 3));
  165. regmap_write(hdmi->regs, TOP_SPDIF_PKT02, mtk_hdmi_v2_format_hw_packet(&buffer[10], 4));
  166. regmap_write(hdmi->regs, TOP_SPDIF_PKT03, mtk_hdmi_v2_format_hw_packet(&buffer[14], 3));
  167. regmap_write(hdmi->regs, TOP_SPDIF_PKT04, mtk_hdmi_v2_format_hw_packet(&buffer[17], 4));
  168. regmap_write(hdmi->regs, TOP_SPDIF_PKT05, mtk_hdmi_v2_format_hw_packet(&buffer[21], 3));
  169. regmap_write(hdmi->regs, TOP_SPDIF_PKT06, mtk_hdmi_v2_format_hw_packet(&buffer[24], 4));
  170. regmap_write(hdmi->regs, TOP_SPDIF_PKT07, buffer[28]);
  171. regmap_set_bits(hdmi->regs, TOP_INFO_EN, SPD_EN_WR | SPD_EN);
  172. regmap_set_bits(hdmi->regs, TOP_INFO_RPT, SPD_RPT_EN);
  173. return 0;
  174. }
  175. static int mtk_hdmi_v2_hdmi_write_hdmi_infoframe(struct drm_bridge *bridge,
  176. const u8 *buffer, size_t len)
  177. {
  178. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  179. regmap_clear_bits(hdmi->regs, TOP_INFO_EN, VSIF_EN_WR | VSIF_EN);
  180. regmap_clear_bits(hdmi->regs, TOP_INFO_RPT, VSIF_RPT_EN);
  181. regmap_write(hdmi->regs, TOP_VSIF_HEADER, mtk_hdmi_v2_format_hw_packet(&buffer[0], 3));
  182. regmap_write(hdmi->regs, TOP_VSIF_PKT00, mtk_hdmi_v2_format_hw_packet(&buffer[3], 4));
  183. regmap_write(hdmi->regs, TOP_VSIF_PKT01, mtk_hdmi_v2_format_hw_packet(&buffer[7], 2));
  184. regmap_write(hdmi->regs, TOP_VSIF_PKT02, 0);
  185. regmap_write(hdmi->regs, TOP_VSIF_PKT03, 0);
  186. regmap_write(hdmi->regs, TOP_VSIF_PKT04, 0);
  187. regmap_write(hdmi->regs, TOP_VSIF_PKT05, 0);
  188. regmap_write(hdmi->regs, TOP_VSIF_PKT06, 0);
  189. regmap_write(hdmi->regs, TOP_VSIF_PKT07, 0);
  190. regmap_set_bits(hdmi->regs, TOP_INFO_EN, VSIF_EN_WR | VSIF_EN);
  191. regmap_set_bits(hdmi->regs, TOP_INFO_RPT, VSIF_RPT_EN);
  192. return 0;
  193. }
  194. static void mtk_hdmi_yuv420_downsampling(struct mtk_hdmi *hdmi, bool enable)
  195. {
  196. u32 val;
  197. regmap_read(hdmi->regs, VID_DOWNSAMPLE_CONFIG, &val);
  198. if (enable) {
  199. regmap_set_bits(hdmi->regs, hdmi->conf->reg_hdmi_tx_cfg, HDMI_YUV420_MODE);
  200. val |= C444_C422_CONFIG_ENABLE | C422_C420_CONFIG_ENABLE;
  201. val |= C422_C420_CONFIG_OUT_CB_OR_CR;
  202. val &= ~C422_C420_CONFIG_BYPASS;
  203. regmap_write(hdmi->regs, VID_DOWNSAMPLE_CONFIG, val);
  204. regmap_set_bits(hdmi->regs, VID_OUT_FORMAT, OUTPUT_FORMAT_DEMUX_420_ENABLE);
  205. } else {
  206. regmap_clear_bits(hdmi->regs, hdmi->conf->reg_hdmi_tx_cfg, HDMI_YUV420_MODE);
  207. val &= ~(C444_C422_CONFIG_ENABLE | C422_C420_CONFIG_ENABLE);
  208. val &= ~C422_C420_CONFIG_OUT_CB_OR_CR;
  209. val |= C422_C420_CONFIG_BYPASS;
  210. regmap_write(hdmi->regs, VID_DOWNSAMPLE_CONFIG, val);
  211. regmap_clear_bits(hdmi->regs, VID_OUT_FORMAT, OUTPUT_FORMAT_DEMUX_420_ENABLE);
  212. }
  213. }
  214. static int mtk_hdmi_v2_setup_audio_infoframe(struct mtk_hdmi *hdmi)
  215. {
  216. struct hdmi_codec_params *params = &hdmi->aud_param.codec_params;
  217. struct hdmi_audio_infoframe frame;
  218. u8 buffer[14];
  219. ssize_t ret;
  220. memcpy(&frame, &params->cea, sizeof(frame));
  221. ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
  222. if (ret < 0)
  223. return ret;
  224. mtk_hdmi_v2_hdmi_write_audio_infoframe(&hdmi->bridge, buffer, sizeof(buffer));
  225. return 0;
  226. }
  227. static inline void mtk_hdmi_v2_hw_gcp_avmute(struct mtk_hdmi *hdmi, bool mute)
  228. {
  229. u32 val;
  230. regmap_read(hdmi->regs, TOP_CFG01, &val);
  231. val &= ~(CP_CLR_MUTE_EN | CP_SET_MUTE_EN);
  232. if (mute) {
  233. val |= CP_SET_MUTE_EN;
  234. val &= ~CP_CLR_MUTE_EN;
  235. } else {
  236. val |= CP_CLR_MUTE_EN;
  237. val &= ~CP_SET_MUTE_EN;
  238. }
  239. regmap_write(hdmi->regs, TOP_CFG01, val);
  240. regmap_set_bits(hdmi->regs, TOP_INFO_RPT, CP_RPT_EN);
  241. regmap_set_bits(hdmi->regs, TOP_INFO_EN, CP_EN | CP_EN_WR);
  242. }
  243. static void mtk_hdmi_v2_hw_ncts_enable(struct mtk_hdmi *hdmi, bool enable)
  244. {
  245. if (enable)
  246. regmap_set_bits(hdmi->regs, AIP_CTRL, CTS_SW_SEL);
  247. else
  248. regmap_clear_bits(hdmi->regs, AIP_CTRL, CTS_SW_SEL);
  249. }
  250. static void mtk_hdmi_v2_hw_aud_set_channel_status(struct mtk_hdmi *hdmi)
  251. {
  252. u8 *ch_status = hdmi->aud_param.codec_params.iec.status;
  253. /* Only the first 5 to 7 bytes of Channel Status contain useful information */
  254. regmap_write(hdmi->regs, AIP_I2S_CHST0, mtk_hdmi_v2_format_hw_packet(&ch_status[0], 4));
  255. regmap_write(hdmi->regs, AIP_I2S_CHST1, mtk_hdmi_v2_format_hw_packet(&ch_status[4], 3));
  256. }
  257. static void mtk_hdmi_v2_hw_aud_set_ncts(struct mtk_hdmi *hdmi,
  258. unsigned int sample_rate,
  259. unsigned int clock)
  260. {
  261. unsigned int n, cts;
  262. mtk_hdmi_get_ncts(sample_rate, clock, &n, &cts);
  263. regmap_write(hdmi->regs, AIP_N_VAL, n);
  264. regmap_write(hdmi->regs, AIP_CTS_SVAL, cts);
  265. }
  266. static void mtk_hdmi_v2_hw_aud_enable(struct mtk_hdmi *hdmi, bool enable)
  267. {
  268. if (enable)
  269. regmap_clear_bits(hdmi->regs, AIP_TXCTRL, AUD_PACKET_DROP);
  270. else
  271. regmap_set_bits(hdmi->regs, AIP_TXCTRL, AUD_PACKET_DROP);
  272. }
  273. static u32 mtk_hdmi_v2_aud_output_channel_map(u8 sd0, u8 sd1, u8 sd2, u8 sd3,
  274. u8 sd4, u8 sd5, u8 sd6, u8 sd7)
  275. {
  276. u32 val;
  277. /*
  278. * Each of the Output Channels (0-7) can be mapped to get their input
  279. * from any of the available Input Channels (0-7): this function
  280. * takes input channel numbers and formats a value that must then
  281. * be written to the TOP_AUD_MAP hardware register by the caller.
  282. */
  283. val = FIELD_PREP(SD0_MAP, sd0) | FIELD_PREP(SD1_MAP, sd1);
  284. val |= FIELD_PREP(SD2_MAP, sd2) | FIELD_PREP(SD3_MAP, sd3);
  285. val |= FIELD_PREP(SD4_MAP, sd4) | FIELD_PREP(SD5_MAP, sd5);
  286. val |= FIELD_PREP(SD6_MAP, sd6) | FIELD_PREP(SD7_MAP, sd7);
  287. return val;
  288. }
  289. static void mtk_hdmi_audio_dsd_config(struct mtk_hdmi *hdmi,
  290. unsigned char chnum, bool dsd_bypass)
  291. {
  292. u32 channel_map;
  293. regmap_update_bits(hdmi->regs, AIP_CTRL, SPDIF_EN | DSD_EN | HBRA_ON, DSD_EN);
  294. regmap_set_bits(hdmi->regs, AIP_TXCTRL, DSD_MUTE_EN);
  295. if (dsd_bypass)
  296. channel_map = mtk_hdmi_v2_aud_output_channel_map(0, 2, 4, 6, 1, 3, 5, 7);
  297. else
  298. channel_map = mtk_hdmi_v2_aud_output_channel_map(0, 5, 1, 0, 3, 2, 4, 0);
  299. regmap_write(hdmi->regs, TOP_AUD_MAP, channel_map);
  300. regmap_clear_bits(hdmi->regs, AIP_SPDIF_CTRL, I2S2DSD_EN);
  301. }
  302. static inline void mtk_hdmi_v2_hw_i2s_fifo_map(struct mtk_hdmi *hdmi, u32 fifo_mapping)
  303. {
  304. regmap_update_bits(hdmi->regs, AIP_I2S_CTRL,
  305. FIFO0_MAP | FIFO1_MAP | FIFO2_MAP | FIFO3_MAP, fifo_mapping);
  306. }
  307. static inline void mtk_hdmi_v2_hw_i2s_ch_number(struct mtk_hdmi *hdmi, u8 chnum)
  308. {
  309. regmap_update_bits(hdmi->regs, AIP_CTRL, I2S_EN, FIELD_PREP(I2S_EN, chnum));
  310. }
  311. static void mtk_hdmi_v2_hw_i2s_ch_mapping(struct mtk_hdmi *hdmi, u8 chnum, u8 mapping)
  312. {
  313. u32 fifo_map;
  314. u8 bdata;
  315. switch (chnum) {
  316. default:
  317. case 2:
  318. bdata = 0x1;
  319. break;
  320. case 3:
  321. bdata = 0x3;
  322. break;
  323. case 6:
  324. if (mapping == 0x0e) {
  325. bdata = 0xf;
  326. break;
  327. }
  328. fallthrough;
  329. case 5:
  330. bdata = 0x7;
  331. break;
  332. case 7:
  333. case 8:
  334. bdata = 0xf;
  335. break;
  336. }
  337. /* Assign default FIFO mapping: SD0 to FIFO0, SD1 to FIFO1, etc. */
  338. fifo_map = FIELD_PREP(FIFO0_MAP, 0) | FIELD_PREP(FIFO1_MAP, 1);
  339. fifo_map |= FIELD_PREP(FIFO2_MAP, 2) | FIELD_PREP(FIFO3_MAP, 3);
  340. mtk_hdmi_v2_hw_i2s_fifo_map(hdmi, fifo_map);
  341. mtk_hdmi_v2_hw_i2s_ch_number(hdmi, bdata);
  342. /*
  343. * Set HDMI Audio packet layout indicator:
  344. * Layout 0 is for two channels
  345. * Layout 1 is for up to eight channels
  346. */
  347. if (chnum == 2)
  348. regmap_set_bits(hdmi->regs, AIP_TXCTRL, AUD_LAYOUT_1);
  349. else
  350. regmap_clear_bits(hdmi->regs, AIP_TXCTRL, AUD_LAYOUT_1);
  351. }
  352. static void mtk_hdmi_i2s_data_fmt(struct mtk_hdmi *hdmi, unsigned char fmt)
  353. {
  354. u32 val;
  355. regmap_read(hdmi->regs, AIP_I2S_CTRL, &val);
  356. val &= ~(WS_HIGH | I2S_1ST_BIT_NOSHIFT | JUSTIFY_RIGHT);
  357. switch (fmt) {
  358. case HDMI_I2S_MODE_RJT_24BIT:
  359. case HDMI_I2S_MODE_RJT_16BIT:
  360. val |= (WS_HIGH | I2S_1ST_BIT_NOSHIFT | JUSTIFY_RIGHT);
  361. break;
  362. case HDMI_I2S_MODE_LJT_24BIT:
  363. case HDMI_I2S_MODE_LJT_16BIT:
  364. val |= (WS_HIGH | I2S_1ST_BIT_NOSHIFT);
  365. break;
  366. case HDMI_I2S_MODE_I2S_24BIT:
  367. case HDMI_I2S_MODE_I2S_16BIT:
  368. default:
  369. break;
  370. }
  371. regmap_write(hdmi->regs, AIP_I2S_CTRL, val);
  372. }
  373. static inline void mtk_hdmi_i2s_sck_edge_rise(struct mtk_hdmi *hdmi, bool rise)
  374. {
  375. if (rise)
  376. regmap_set_bits(hdmi->regs, AIP_I2S_CTRL, SCK_EDGE_RISE);
  377. else
  378. regmap_clear_bits(hdmi->regs, AIP_I2S_CTRL, SCK_EDGE_RISE);
  379. }
  380. static inline void mtk_hdmi_i2s_cbit_order(struct mtk_hdmi *hdmi, unsigned int cbit)
  381. {
  382. regmap_update_bits(hdmi->regs, AIP_I2S_CTRL, CBIT_ORDER_SAME, cbit);
  383. }
  384. static inline void mtk_hdmi_i2s_vbit(struct mtk_hdmi *hdmi, unsigned int vbit)
  385. {
  386. /* V bit: 0 for PCM, 1 for Compressed data */
  387. regmap_update_bits(hdmi->regs, AIP_I2S_CTRL, VBIT_COMPRESSED, vbit);
  388. }
  389. static inline void mtk_hdmi_i2s_data_direction(struct mtk_hdmi *hdmi, unsigned int is_lsb)
  390. {
  391. regmap_update_bits(hdmi->regs, AIP_I2S_CTRL, I2S_DATA_DIR_LSB, is_lsb);
  392. }
  393. static inline void mtk_hdmi_v2_hw_audio_type(struct mtk_hdmi *hdmi, unsigned int spdif_i2s)
  394. {
  395. regmap_update_bits(hdmi->regs, AIP_CTRL, SPDIF_EN, FIELD_PREP(SPDIF_EN, spdif_i2s));
  396. }
  397. static u8 mtk_hdmi_v2_get_i2s_ch_mapping(struct mtk_hdmi *hdmi, u8 channel_type)
  398. {
  399. switch (channel_type) {
  400. case HDMI_AUD_CHAN_TYPE_1_1:
  401. case HDMI_AUD_CHAN_TYPE_2_1:
  402. return 0x01;
  403. case HDMI_AUD_CHAN_TYPE_3_0:
  404. return 0x02;
  405. case HDMI_AUD_CHAN_TYPE_3_1:
  406. return 0x03;
  407. case HDMI_AUD_CHAN_TYPE_3_0_LRS:
  408. case HDMI_AUD_CHAN_TYPE_4_0:
  409. return 0x08;
  410. case HDMI_AUD_CHAN_TYPE_5_1:
  411. return 0x0b;
  412. case HDMI_AUD_CHAN_TYPE_4_1_CLRS:
  413. case HDMI_AUD_CHAN_TYPE_6_0:
  414. case HDMI_AUD_CHAN_TYPE_6_0_CS:
  415. case HDMI_AUD_CHAN_TYPE_6_0_CH:
  416. case HDMI_AUD_CHAN_TYPE_6_0_OH:
  417. case HDMI_AUD_CHAN_TYPE_6_0_CHR:
  418. return 0x0e;
  419. case HDMI_AUD_CHAN_TYPE_1_0:
  420. case HDMI_AUD_CHAN_TYPE_2_0:
  421. case HDMI_AUD_CHAN_TYPE_3_1_LRS:
  422. case HDMI_AUD_CHAN_TYPE_4_1:
  423. case HDMI_AUD_CHAN_TYPE_5_0:
  424. case HDMI_AUD_CHAN_TYPE_4_0_CLRS:
  425. case HDMI_AUD_CHAN_TYPE_6_1:
  426. case HDMI_AUD_CHAN_TYPE_6_1_CS:
  427. case HDMI_AUD_CHAN_TYPE_6_1_CH:
  428. case HDMI_AUD_CHAN_TYPE_6_1_OH:
  429. case HDMI_AUD_CHAN_TYPE_6_1_CHR:
  430. case HDMI_AUD_CHAN_TYPE_7_0:
  431. case HDMI_AUD_CHAN_TYPE_7_0_LH_RH:
  432. case HDMI_AUD_CHAN_TYPE_7_0_LSR_RSR:
  433. case HDMI_AUD_CHAN_TYPE_7_0_LC_RC:
  434. case HDMI_AUD_CHAN_TYPE_7_0_LW_RW:
  435. case HDMI_AUD_CHAN_TYPE_7_0_LSD_RSD:
  436. case HDMI_AUD_CHAN_TYPE_7_0_LSS_RSS:
  437. case HDMI_AUD_CHAN_TYPE_7_0_LHS_RHS:
  438. case HDMI_AUD_CHAN_TYPE_7_0_CS_CH:
  439. case HDMI_AUD_CHAN_TYPE_7_0_CS_OH:
  440. case HDMI_AUD_CHAN_TYPE_7_0_CS_CHR:
  441. case HDMI_AUD_CHAN_TYPE_7_0_CH_OH:
  442. case HDMI_AUD_CHAN_TYPE_7_0_CH_CHR:
  443. case HDMI_AUD_CHAN_TYPE_7_0_OH_CHR:
  444. case HDMI_AUD_CHAN_TYPE_7_0_LSS_RSS_LSR_RSR:
  445. case HDMI_AUD_CHAN_TYPE_8_0_LH_RH_CS:
  446. case HDMI_AUD_CHAN_TYPE_7_1:
  447. case HDMI_AUD_CHAN_TYPE_7_1_LH_RH:
  448. case HDMI_AUD_CHAN_TYPE_7_1_LSR_RSR:
  449. case HDMI_AUD_CHAN_TYPE_7_1_LC_RC:
  450. case HDMI_AUD_CHAN_TYPE_7_1_LW_RW:
  451. case HDMI_AUD_CHAN_TYPE_7_1_LSD_RSD:
  452. case HDMI_AUD_CHAN_TYPE_7_1_LSS_RSS:
  453. case HDMI_AUD_CHAN_TYPE_7_1_LHS_RHS:
  454. case HDMI_AUD_CHAN_TYPE_7_1_CS_CH:
  455. case HDMI_AUD_CHAN_TYPE_7_1_CS_OH:
  456. case HDMI_AUD_CHAN_TYPE_7_1_CS_CHR:
  457. case HDMI_AUD_CHAN_TYPE_7_1_CH_OH:
  458. case HDMI_AUD_CHAN_TYPE_7_1_CH_CHR:
  459. case HDMI_AUD_CHAN_TYPE_7_1_OH_CHR:
  460. case HDMI_AUD_CHAN_TYPE_7_1_LSS_RSS_LSR_RSR:
  461. default:
  462. return 0;
  463. }
  464. return 0;
  465. }
  466. static inline void mtk_hdmi_v2_hw_i2s_ch_swap(struct mtk_hdmi *hdmi)
  467. {
  468. regmap_update_bits(hdmi->regs, AIP_SPDIF_CTRL, MAX_2UI_I2S_HI_WRITE,
  469. FIELD_PREP(MAX_2UI_I2S_HI_WRITE, MAX_2UI_I2S_LFE_CC_SWAP));
  470. }
  471. static void mtk_hdmi_hbr_config(struct mtk_hdmi *hdmi, bool dsd_bypass)
  472. {
  473. const u32 hbr_mask = SPDIF_EN | DSD_EN | HBRA_ON;
  474. if (dsd_bypass) {
  475. regmap_update_bits(hdmi->regs, AIP_CTRL, hbr_mask, HBRA_ON);
  476. regmap_set_bits(hdmi->regs, AIP_CTRL, I2S_EN);
  477. } else {
  478. regmap_update_bits(hdmi->regs, AIP_CTRL, hbr_mask, SPDIF_EN);
  479. regmap_set_bits(hdmi->regs, AIP_CTRL, SPDIF_INTERNAL_MODULE);
  480. regmap_set_bits(hdmi->regs, AIP_CTRL, HBR_FROM_SPDIF);
  481. regmap_set_bits(hdmi->regs, AIP_CTRL, CTS_CAL_N4);
  482. }
  483. }
  484. static inline void mtk_hdmi_v2_hw_spdif_config(struct mtk_hdmi *hdmi)
  485. {
  486. regmap_clear_bits(hdmi->regs, AIP_SPDIF_CTRL, WR_1UI_LOCK);
  487. regmap_clear_bits(hdmi->regs, AIP_SPDIF_CTRL, FS_OVERRIDE_WRITE);
  488. regmap_clear_bits(hdmi->regs, AIP_SPDIF_CTRL, WR_2UI_LOCK);
  489. regmap_update_bits(hdmi->regs, AIP_SPDIF_CTRL, MAX_1UI_WRITE,
  490. FIELD_PREP(MAX_1UI_WRITE, 4));
  491. regmap_update_bits(hdmi->regs, AIP_SPDIF_CTRL, MAX_2UI_SPDIF_WRITE,
  492. FIELD_PREP(MAX_2UI_SPDIF_WRITE, 9));
  493. regmap_update_bits(hdmi->regs, AIP_SPDIF_CTRL, AUD_ERR_THRESH,
  494. FIELD_PREP(AUD_ERR_THRESH, 4));
  495. regmap_set_bits(hdmi->regs, AIP_SPDIF_CTRL, I2S2DSD_EN);
  496. }
  497. static void mtk_hdmi_v2_aud_set_input(struct mtk_hdmi *hdmi)
  498. {
  499. struct hdmi_audio_param *aud_param = &hdmi->aud_param;
  500. struct hdmi_codec_params *codec_params = &aud_param->codec_params;
  501. u8 i2s_ch_map;
  502. u32 out_ch_map;
  503. /* Write the default output channel map. CH0 maps to SD0, CH1 maps to SD1, etc */
  504. out_ch_map = mtk_hdmi_v2_aud_output_channel_map(0, 1, 2, 3, 4, 5, 6, 7);
  505. regmap_write(hdmi->regs, TOP_AUD_MAP, out_ch_map);
  506. regmap_update_bits(hdmi->regs, AIP_SPDIF_CTRL, MAX_2UI_I2S_HI_WRITE, 0);
  507. regmap_clear_bits(hdmi->regs, AIP_CTRL,
  508. SPDIF_EN | DSD_EN | HBRA_ON | CTS_CAL_N4 |
  509. HBR_FROM_SPDIF | SPDIF_INTERNAL_MODULE);
  510. regmap_clear_bits(hdmi->regs, AIP_TXCTRL, DSD_MUTE_EN | AUD_LAYOUT_1);
  511. if (aud_param->aud_input_type == HDMI_AUD_INPUT_I2S) {
  512. switch (aud_param->aud_codec) {
  513. case HDMI_AUDIO_CODING_TYPE_DTS_HD:
  514. case HDMI_AUDIO_CODING_TYPE_MLP:
  515. mtk_hdmi_i2s_data_fmt(hdmi, aud_param->aud_i2s_fmt);
  516. mtk_hdmi_hbr_config(hdmi, true);
  517. break;
  518. case HDMI_AUDIO_CODING_TYPE_DSD:
  519. mtk_hdmi_audio_dsd_config(hdmi, codec_params->channels, 0);
  520. mtk_hdmi_v2_hw_i2s_ch_mapping(hdmi, codec_params->channels, 1);
  521. break;
  522. default:
  523. mtk_hdmi_i2s_data_fmt(hdmi, aud_param->aud_i2s_fmt);
  524. mtk_hdmi_i2s_sck_edge_rise(hdmi, true);
  525. mtk_hdmi_i2s_cbit_order(hdmi, CBIT_ORDER_SAME);
  526. mtk_hdmi_i2s_vbit(hdmi, 0); /* PCM data */
  527. mtk_hdmi_i2s_data_direction(hdmi, 0); /* MSB first */
  528. mtk_hdmi_v2_hw_audio_type(hdmi, HDMI_AUD_INPUT_I2S);
  529. i2s_ch_map = mtk_hdmi_v2_get_i2s_ch_mapping(hdmi,
  530. aud_param->aud_input_chan_type);
  531. mtk_hdmi_v2_hw_i2s_ch_mapping(hdmi, codec_params->channels, i2s_ch_map);
  532. mtk_hdmi_v2_hw_i2s_ch_swap(hdmi);
  533. }
  534. } else {
  535. if (codec_params->sample_rate == 768000 &&
  536. (aud_param->aud_codec == HDMI_AUDIO_CODING_TYPE_DTS_HD ||
  537. aud_param->aud_codec == HDMI_AUDIO_CODING_TYPE_MLP)) {
  538. mtk_hdmi_hbr_config(hdmi, false);
  539. } else {
  540. mtk_hdmi_v2_hw_spdif_config(hdmi);
  541. mtk_hdmi_v2_hw_i2s_ch_mapping(hdmi, 2, 0);
  542. }
  543. }
  544. }
  545. static inline void mtk_hdmi_v2_hw_audio_input_enable(struct mtk_hdmi *hdmi, bool ena)
  546. {
  547. if (ena)
  548. regmap_set_bits(hdmi->regs, AIP_CTRL, AUD_IN_EN);
  549. else
  550. regmap_clear_bits(hdmi->regs, AIP_CTRL, AUD_IN_EN);
  551. }
  552. static void mtk_hdmi_v2_aip_ctrl_init(struct mtk_hdmi *hdmi)
  553. {
  554. regmap_set_bits(hdmi->regs, AIP_CTRL,
  555. AUD_SEL_OWRT | NO_MCLK_CTSGEN_SEL | MCLK_EN | CTS_REQ_EN);
  556. regmap_clear_bits(hdmi->regs, AIP_TPI_CTRL, TPI_AUDIO_LOOKUP_EN);
  557. }
  558. static void mtk_hdmi_v2_audio_reset(struct mtk_hdmi *hdmi, bool reset)
  559. {
  560. const u32 arst_bits = RST4AUDIO | RST4AUDIO_FIFO | RST4AUDIO_ACR;
  561. if (reset)
  562. regmap_set_bits(hdmi->regs, AIP_TXCTRL, arst_bits);
  563. else
  564. regmap_clear_bits(hdmi->regs, AIP_TXCTRL, arst_bits);
  565. }
  566. static void mtk_hdmi_v2_aud_output_config(struct mtk_hdmi *hdmi,
  567. struct drm_display_mode *display_mode)
  568. {
  569. /* Shut down and reset the HDMI Audio HW to avoid glitching */
  570. mtk_hdmi_v2_hw_aud_mute(hdmi, true);
  571. mtk_hdmi_v2_hw_aud_enable(hdmi, false);
  572. mtk_hdmi_v2_audio_reset(hdmi, true);
  573. /* Configure the main hardware params and get out of reset */
  574. mtk_hdmi_v2_aip_ctrl_init(hdmi);
  575. mtk_hdmi_v2_aud_set_input(hdmi);
  576. mtk_hdmi_v2_hw_aud_set_channel_status(hdmi);
  577. mtk_hdmi_v2_setup_audio_infoframe(hdmi);
  578. mtk_hdmi_v2_hw_audio_input_enable(hdmi, true);
  579. mtk_hdmi_v2_audio_reset(hdmi, false);
  580. /* Ignore N/CTS packet transmission requests and configure */
  581. mtk_hdmi_v2_hw_ncts_enable(hdmi, false);
  582. mtk_hdmi_v2_hw_aud_set_ncts(hdmi, hdmi->aud_param.codec_params.sample_rate,
  583. display_mode->clock);
  584. /* Wait for the HW to apply settings */
  585. usleep_range(25, 50);
  586. /* Hardware is fully configured: enable TX of N/CTS pkts and unmute */
  587. mtk_hdmi_v2_hw_ncts_enable(hdmi, true);
  588. mtk_hdmi_v2_hw_aud_enable(hdmi, true);
  589. mtk_hdmi_v2_hw_aud_mute(hdmi, false);
  590. }
  591. static void mtk_hdmi_v2_change_video_resolution(struct mtk_hdmi *hdmi,
  592. struct drm_connector_state *conn_state)
  593. {
  594. mtk_hdmi_v2_hw_reset(hdmi);
  595. mtk_hdmi_v2_set_sw_hpd(hdmi, true);
  596. udelay(2);
  597. regmap_write(hdmi->regs, HDCP_TOP_CTRL, 0);
  598. /*
  599. * Enable HDCP reauthentication interrupt: the HW uses this internally
  600. * for the HPD state machine even if HDCP encryption is not enabled.
  601. */
  602. regmap_set_bits(hdmi->regs, TOP_INT_ENABLE00, HDCP2X_RX_REAUTH_REQ_DDCM_INT);
  603. /* Enable hotplug and pord interrupts */
  604. mtk_hdmi_v2_enable_hpd_pord_irq(hdmi, true);
  605. /* Force enabling HDCP HPD */
  606. regmap_set_bits(hdmi->regs, HDCP2X_CTRL_0, HDCP2X_HPD_OVR);
  607. regmap_set_bits(hdmi->regs, HDCP2X_CTRL_0, HDCP2X_HPD_SW);
  608. /* Set 8 bits per pixel */
  609. regmap_update_bits(hdmi->regs, TOP_CFG00, TMDS_PACK_MODE,
  610. FIELD_PREP(TMDS_PACK_MODE, TMDS_PACK_MODE_8BPP));
  611. /* Disable generating deepcolor packets */
  612. regmap_clear_bits(hdmi->regs, TOP_CFG00, DEEPCOLOR_PKT_EN);
  613. /* Disable adding deepcolor information to the general packet */
  614. regmap_clear_bits(hdmi->regs, TOP_MISC_CTLR, DEEP_COLOR_ADD);
  615. if (hdmi->curr_conn->display_info.is_hdmi)
  616. regmap_set_bits(hdmi->regs, TOP_CFG00, HDMI_MODE_HDMI);
  617. else
  618. regmap_clear_bits(hdmi->regs, TOP_CFG00, HDMI_MODE_HDMI);
  619. udelay(5);
  620. mtk_hdmi_v2_hw_vid_mute(hdmi, true);
  621. mtk_hdmi_v2_hw_aud_mute(hdmi, true);
  622. mtk_hdmi_v2_hw_gcp_avmute(hdmi, false);
  623. regmap_update_bits(hdmi->regs, TOP_CFG01,
  624. NULL_PKT_VSYNC_HIGH_EN | NULL_PKT_EN, NULL_PKT_VSYNC_HIGH_EN);
  625. usleep_range(100, 150);
  626. /* Enable scrambling if tmds clock is 340MHz or more */
  627. mtk_hdmi_v2_enable_scrambling(hdmi, hdmi->mode.clock >= 340 * KILO);
  628. switch (conn_state->hdmi.output_format) {
  629. default:
  630. case HDMI_COLORSPACE_RGB:
  631. case HDMI_COLORSPACE_YUV444:
  632. /* Disable YUV420 downsampling for RGB and YUV444 */
  633. mtk_hdmi_yuv420_downsampling(hdmi, false);
  634. break;
  635. case HDMI_COLORSPACE_YUV422:
  636. /*
  637. * YUV420 downsampling is special and needs a bit of setup
  638. * so we disable everything there before doing anything else.
  639. *
  640. * YUV422 downsampling instead just needs one bit to be set.
  641. */
  642. mtk_hdmi_yuv420_downsampling(hdmi, false);
  643. regmap_set_bits(hdmi->regs, VID_DOWNSAMPLE_CONFIG,
  644. C444_C422_CONFIG_ENABLE);
  645. break;
  646. case HDMI_COLORSPACE_YUV420:
  647. mtk_hdmi_yuv420_downsampling(hdmi, true);
  648. break;
  649. }
  650. }
  651. static void mtk_hdmi_v2_output_set_display_mode(struct mtk_hdmi *hdmi,
  652. struct drm_connector_state *conn_state,
  653. struct drm_display_mode *mode)
  654. {
  655. union phy_configure_opts opts = {
  656. .dp = { .link_rate = hdmi->mode.clock * KILO }
  657. };
  658. int ret;
  659. ret = phy_configure(hdmi->phy, &opts);
  660. if (ret)
  661. dev_err(hdmi->dev, "Setting clock=%d failed: %d", mode->clock, ret);
  662. mtk_hdmi_v2_change_video_resolution(hdmi, conn_state);
  663. mtk_hdmi_v2_aud_output_config(hdmi, mode);
  664. }
  665. static int mtk_hdmi_v2_clk_enable(struct mtk_hdmi *hdmi)
  666. {
  667. int ret;
  668. ret = clk_prepare_enable(hdmi->clk[MTK_HDMI_V2_CLK_HDCP_SEL]);
  669. if (ret)
  670. return ret;
  671. ret = clk_prepare_enable(hdmi->clk[MTK_HDMI_V2_CLK_HDCP_24M_SEL]);
  672. if (ret)
  673. goto disable_hdcp_clk;
  674. ret = clk_prepare_enable(hdmi->clk[MTK_HDMI_V2_CLK_HDMI_APB_SEL]);
  675. if (ret)
  676. goto disable_hdcp_24m_clk;
  677. ret = clk_prepare_enable(hdmi->clk[MTK_HDMI_V2_CLK_VPP_SPLIT_HDMI]);
  678. if (ret)
  679. goto disable_bus_clk;
  680. return 0;
  681. disable_bus_clk:
  682. clk_disable_unprepare(hdmi->clk[MTK_HDMI_V2_CLK_HDMI_APB_SEL]);
  683. disable_hdcp_24m_clk:
  684. clk_disable_unprepare(hdmi->clk[MTK_HDMI_V2_CLK_HDCP_24M_SEL]);
  685. disable_hdcp_clk:
  686. clk_disable_unprepare(hdmi->clk[MTK_HDMI_V2_CLK_HDCP_SEL]);
  687. return ret;
  688. }
  689. static void mtk_hdmi_v2_clk_disable(struct mtk_hdmi *hdmi)
  690. {
  691. clk_disable_unprepare(hdmi->clk[MTK_HDMI_V2_CLK_VPP_SPLIT_HDMI]);
  692. clk_disable_unprepare(hdmi->clk[MTK_HDMI_V2_CLK_HDMI_APB_SEL]);
  693. clk_disable_unprepare(hdmi->clk[MTK_HDMI_V2_CLK_HDCP_24M_SEL]);
  694. clk_disable_unprepare(hdmi->clk[MTK_HDMI_V2_CLK_HDCP_SEL]);
  695. }
  696. static enum hdmi_hpd_state mtk_hdmi_v2_hpd_pord_status(struct mtk_hdmi *hdmi)
  697. {
  698. u8 hpd_pin_sta, pord_pin_sta;
  699. u32 hpd_status;
  700. regmap_read(hdmi->regs, HPD_DDC_STATUS, &hpd_status);
  701. hpd_pin_sta = FIELD_GET(HPD_PIN_STA, hpd_status);
  702. pord_pin_sta = FIELD_GET(PORD_PIN_STA, hpd_status);
  703. /*
  704. * Inform that the cable is plugged in (hpd_pin_sta) so that the
  705. * sink can be powered on by switching the 5V VBUS as required by
  706. * the HDMI spec for reading EDID and for HDMI Audio registers to
  707. * be accessible.
  708. *
  709. * PORD detection succeeds only when the cable is plugged in and
  710. * the sink is powered on: reaching that state means that the
  711. * communication with the sink can be started.
  712. *
  713. * Please note that when the cable is plugged out the HPD pin will
  714. * be the first one to fall, while PORD may still be in rise state
  715. * for a few more milliseconds, so we decide HDMI_PLUG_OUT without
  716. * checking PORD at all (we check only HPD falling for that).
  717. */
  718. if (hpd_pin_sta && pord_pin_sta)
  719. return HDMI_PLUG_IN_AND_SINK_POWER_ON;
  720. else if (hpd_pin_sta)
  721. return HDMI_PLUG_IN_ONLY;
  722. else
  723. return HDMI_PLUG_OUT;
  724. }
  725. static irqreturn_t mtk_hdmi_v2_isr(int irq, void *arg)
  726. {
  727. struct mtk_hdmi *hdmi = arg;
  728. unsigned int irq_sta;
  729. int ret = IRQ_HANDLED;
  730. regmap_read(hdmi->regs, TOP_INT_STA00, &irq_sta);
  731. /* Handle Hotplug Detection interrupts */
  732. if (irq_sta & HPD_PORD_HWIRQS) {
  733. /*
  734. * Disable the HPD/PORD IRQs now and until thread done to
  735. * avoid interrupt storm that could happen with bad cables
  736. */
  737. mtk_hdmi_v2_enable_hpd_pord_irq(hdmi, false);
  738. ret = IRQ_WAKE_THREAD;
  739. /* Clear HPD/PORD irqs to avoid unwanted retriggering */
  740. regmap_write(hdmi->regs, TOP_INT_CLR00, HPD_PORD_HWIRQS);
  741. regmap_write(hdmi->regs, TOP_INT_CLR00, 0);
  742. }
  743. return ret;
  744. }
  745. static irqreturn_t __mtk_hdmi_v2_isr_thread(struct mtk_hdmi *hdmi)
  746. {
  747. enum hdmi_hpd_state hpd;
  748. hpd = mtk_hdmi_v2_hpd_pord_status(hdmi);
  749. if (hpd != hdmi->hpd) {
  750. struct drm_encoder *encoder = hdmi->bridge.encoder;
  751. hdmi->hpd = hpd;
  752. if (encoder && encoder->dev)
  753. drm_helper_hpd_irq_event(hdmi->bridge.encoder->dev);
  754. }
  755. mtk_hdmi_v2_enable_hpd_pord_irq(hdmi, true);
  756. return IRQ_HANDLED;
  757. }
  758. static irqreturn_t mtk_hdmi_v2_isr_thread(int irq, void *arg)
  759. {
  760. struct mtk_hdmi *hdmi = arg;
  761. /*
  762. * Debounce HDMI monitor HPD status.
  763. * Empirical testing shows that 30ms is enough wait
  764. */
  765. msleep(30);
  766. return __mtk_hdmi_v2_isr_thread(hdmi);
  767. }
  768. static int mtk_hdmi_v2_enable(struct mtk_hdmi *hdmi)
  769. {
  770. bool was_active = pm_runtime_active(hdmi->dev);
  771. int ret;
  772. ret = pm_runtime_resume_and_get(hdmi->dev);
  773. if (ret) {
  774. dev_err(hdmi->dev, "Cannot resume HDMI\n");
  775. return ret;
  776. }
  777. ret = mtk_hdmi_v2_clk_enable(hdmi);
  778. if (ret) {
  779. pm_runtime_put(hdmi->dev);
  780. return ret;
  781. }
  782. if (!was_active) {
  783. mtk_hdmi_v2_hw_reset(hdmi);
  784. mtk_hdmi_v2_set_sw_hpd(hdmi, true);
  785. }
  786. return 0;
  787. }
  788. static void mtk_hdmi_v2_disable(struct mtk_hdmi *hdmi)
  789. {
  790. mtk_hdmi_v2_clk_disable(hdmi);
  791. pm_runtime_put_sync(hdmi->dev);
  792. }
  793. /*
  794. * Bridge callbacks
  795. */
  796. static int mtk_hdmi_v2_bridge_attach(struct drm_bridge *bridge,
  797. struct drm_encoder *encoder,
  798. enum drm_bridge_attach_flags flags)
  799. {
  800. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  801. int ret;
  802. if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
  803. DRM_ERROR("The flag DRM_BRIDGE_ATTACH_NO_CONNECTOR must be supplied\n");
  804. return -EINVAL;
  805. }
  806. if (hdmi->bridge.next_bridge) {
  807. ret = drm_bridge_attach(encoder, hdmi->bridge.next_bridge, bridge, flags);
  808. if (ret)
  809. return ret;
  810. }
  811. ret = mtk_hdmi_v2_enable(hdmi);
  812. if (ret)
  813. return ret;
  814. /* Enable Hotplug and Pord pins internal debouncing */
  815. regmap_set_bits(hdmi->regs, HPD_DDC_CTRL,
  816. HPD_DDC_HPD_DBNC_EN | HPD_DDC_PORD_DBNC_EN);
  817. irq_clear_status_flags(hdmi->irq, IRQ_NOAUTOEN);
  818. enable_irq(hdmi->irq);
  819. /*
  820. * Check if any HDMI monitor was connected before probing this driver
  821. * and/or attaching the bridge, without debouncing: if so, we want to
  822. * notify the DRM so that we start outputting an image ASAP.
  823. * Note that calling the ISR thread function will also perform a HW
  824. * registers write that enables both the HPD and Pord interrupts.
  825. */
  826. __mtk_hdmi_v2_isr_thread(hdmi);
  827. mtk_hdmi_v2_disable(hdmi);
  828. return 0;
  829. }
  830. static void mtk_hdmi_v2_bridge_detach(struct drm_bridge *bridge)
  831. {
  832. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  833. WARN_ON(pm_runtime_active(hdmi->dev));
  834. /* The controller is already powered off, just disable irq here */
  835. disable_irq(hdmi->irq);
  836. }
  837. static void mtk_hdmi_v2_handle_plugged_change(struct mtk_hdmi *hdmi, bool plugged)
  838. {
  839. mutex_lock(&hdmi->update_plugged_status_lock);
  840. if (hdmi->plugged_cb && hdmi->codec_dev)
  841. hdmi->plugged_cb(hdmi->codec_dev, plugged);
  842. mutex_unlock(&hdmi->update_plugged_status_lock);
  843. }
  844. static void mtk_hdmi_v2_bridge_pre_enable(struct drm_bridge *bridge,
  845. struct drm_atomic_state *state)
  846. {
  847. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  848. struct drm_connector_state *conn_state;
  849. union phy_configure_opts opts = {
  850. .dp = { .link_rate = hdmi->mode.clock * KILO }
  851. };
  852. int ret;
  853. /* Power on the controller before trying to write to registers */
  854. ret = mtk_hdmi_v2_enable(hdmi);
  855. if (WARN_ON(ret))
  856. return;
  857. /* Retrieve the connector through the atomic state */
  858. hdmi->curr_conn = drm_atomic_get_new_connector_for_encoder(state, bridge->encoder);
  859. conn_state = drm_atomic_get_new_connector_state(state, hdmi->curr_conn);
  860. if (WARN_ON(!conn_state))
  861. return;
  862. /*
  863. * Preconfigure the HDMI controller and the HDMI PHY at pre_enable
  864. * stage to make sure that this IP is ready and clocked before the
  865. * mtk_dpi gets powered on and before it enables the output.
  866. */
  867. mtk_hdmi_v2_output_set_display_mode(hdmi, conn_state, &hdmi->mode);
  868. /* Reconfigure phy clock link with appropriate rate */
  869. phy_configure(hdmi->phy, &opts);
  870. /* Power on the PHY here to make sure that DPI_HDMI is clocked */
  871. phy_power_on(hdmi->phy);
  872. hdmi->powered = true;
  873. }
  874. static void mtk_hdmi_v2_bridge_enable(struct drm_bridge *bridge,
  875. struct drm_atomic_state *state)
  876. {
  877. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  878. int ret;
  879. if (WARN_ON(!hdmi->powered))
  880. return;
  881. ret = drm_atomic_helper_connector_hdmi_update_infoframes(hdmi->curr_conn, state);
  882. if (ret)
  883. dev_err(hdmi->dev, "Could not update infoframes: %d\n", ret);
  884. mtk_hdmi_v2_hw_vid_mute(hdmi, false);
  885. /* signal the connect event to audio codec */
  886. mtk_hdmi_v2_handle_plugged_change(hdmi, true);
  887. hdmi->enabled = true;
  888. }
  889. static void mtk_hdmi_v2_bridge_disable(struct drm_bridge *bridge,
  890. struct drm_atomic_state *state)
  891. {
  892. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  893. if (!hdmi->enabled)
  894. return;
  895. mtk_hdmi_v2_hw_gcp_avmute(hdmi, true);
  896. msleep(50);
  897. mtk_hdmi_v2_hw_vid_mute(hdmi, true);
  898. mtk_hdmi_v2_hw_aud_mute(hdmi, true);
  899. msleep(50);
  900. hdmi->enabled = false;
  901. }
  902. static void mtk_hdmi_v2_bridge_post_disable(struct drm_bridge *bridge,
  903. struct drm_atomic_state *state)
  904. {
  905. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  906. if (!hdmi->powered)
  907. return;
  908. phy_power_off(hdmi->phy);
  909. hdmi->powered = false;
  910. /* signal the disconnect event to audio codec */
  911. mtk_hdmi_v2_handle_plugged_change(hdmi, false);
  912. /* Power off */
  913. mtk_hdmi_v2_disable(hdmi);
  914. }
  915. static enum drm_connector_status mtk_hdmi_v2_bridge_detect(struct drm_bridge *bridge,
  916. struct drm_connector *connector)
  917. {
  918. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  919. return hdmi->hpd != HDMI_PLUG_OUT ?
  920. connector_status_connected : connector_status_disconnected;
  921. }
  922. static const struct drm_edid *mtk_hdmi_v2_bridge_edid_read(struct drm_bridge *bridge,
  923. struct drm_connector *connector)
  924. {
  925. return drm_edid_read(connector);
  926. }
  927. static void mtk_hdmi_v2_hpd_enable(struct drm_bridge *bridge)
  928. {
  929. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  930. int ret;
  931. ret = mtk_hdmi_v2_enable(hdmi);
  932. if (ret) {
  933. dev_err(hdmi->dev, "Cannot power on controller for HPD: %d\n", ret);
  934. return;
  935. }
  936. mtk_hdmi_v2_enable_hpd_pord_irq(hdmi, true);
  937. }
  938. static void mtk_hdmi_v2_hpd_disable(struct drm_bridge *bridge)
  939. {
  940. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  941. mtk_hdmi_v2_enable_hpd_pord_irq(hdmi, false);
  942. mtk_hdmi_v2_disable(hdmi);
  943. }
  944. static enum drm_mode_status
  945. mtk_hdmi_v2_hdmi_tmds_char_rate_valid(const struct drm_bridge *bridge,
  946. const struct drm_display_mode *mode,
  947. unsigned long long tmds_rate)
  948. {
  949. if (mode->clock < MTK_HDMI_V2_CLOCK_MIN)
  950. return MODE_CLOCK_LOW;
  951. else if (mode->clock > MTK_HDMI_V2_CLOCK_MAX)
  952. return MODE_CLOCK_HIGH;
  953. else
  954. return MODE_OK;
  955. }
  956. static int mtk_hdmi_v2_hdmi_clear_audio_infoframe(struct drm_bridge *bridge)
  957. {
  958. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  959. regmap_clear_bits(hdmi->regs, TOP_INFO_EN, AUD_EN_WR | AUD_EN);
  960. regmap_clear_bits(hdmi->regs, TOP_INFO_RPT, AUD_RPT_EN);
  961. return 0;
  962. }
  963. static int mtk_hdmi_v2_hdmi_clear_avi_infoframe(struct drm_bridge *bridge)
  964. {
  965. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  966. regmap_clear_bits(hdmi->regs, TOP_INFO_EN, AVI_EN_WR | AVI_EN);
  967. regmap_clear_bits(hdmi->regs, TOP_INFO_RPT, AVI_RPT_EN);
  968. return 0;
  969. }
  970. static int mtk_hdmi_v2_hdmi_clear_spd_infoframe(struct drm_bridge *bridge)
  971. {
  972. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  973. regmap_clear_bits(hdmi->regs, TOP_INFO_EN, SPD_EN_WR | SPD_EN);
  974. regmap_clear_bits(hdmi->regs, TOP_INFO_RPT, SPD_RPT_EN);
  975. return 0;
  976. }
  977. static int mtk_hdmi_v2_hdmi_clear_hdmi_infoframe(struct drm_bridge *bridge)
  978. {
  979. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  980. regmap_clear_bits(hdmi->regs, TOP_INFO_EN, VSIF_EN_WR | VSIF_EN);
  981. regmap_clear_bits(hdmi->regs, TOP_INFO_RPT, VSIF_RPT_EN);
  982. return 0;
  983. }
  984. static int mtk_hdmi_v2_set_abist(struct mtk_hdmi *hdmi, bool enable)
  985. {
  986. struct drm_display_mode *mode = &hdmi->mode;
  987. int abist_format = -EINVAL;
  988. bool interlaced;
  989. if (!enable) {
  990. regmap_clear_bits(hdmi->regs, TOP_CFG00, HDMI_ABIST_ENABLE);
  991. return 0;
  992. }
  993. if (!mode->hdisplay || !mode->vdisplay)
  994. return -EINVAL;
  995. interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
  996. switch (mode->hdisplay) {
  997. case 720:
  998. if (mode->vdisplay == 480)
  999. abist_format = 2;
  1000. else if (mode->vdisplay == 576)
  1001. abist_format = 11;
  1002. break;
  1003. case 1280:
  1004. if (mode->vdisplay == 720)
  1005. abist_format = 3;
  1006. break;
  1007. case 1440:
  1008. if (mode->vdisplay == 480)
  1009. abist_format = interlaced ? 5 : 9;
  1010. else if (mode->vdisplay == 576)
  1011. abist_format = interlaced ? 14 : 18;
  1012. break;
  1013. case 1920:
  1014. if (mode->vdisplay == 1080)
  1015. abist_format = interlaced ? 4 : 10;
  1016. break;
  1017. case 3840:
  1018. if (mode->vdisplay == 2160)
  1019. abist_format = 25;
  1020. break;
  1021. case 4096:
  1022. if (mode->vdisplay == 2160)
  1023. abist_format = 26;
  1024. break;
  1025. default:
  1026. break;
  1027. }
  1028. if (abist_format < 0)
  1029. return abist_format;
  1030. regmap_update_bits(hdmi->regs, TOP_CFG00, HDMI_ABIST_VIDEO_FORMAT,
  1031. FIELD_PREP(HDMI_ABIST_VIDEO_FORMAT, abist_format));
  1032. regmap_set_bits(hdmi->regs, TOP_CFG00, HDMI_ABIST_ENABLE);
  1033. return 0;
  1034. }
  1035. static int mtk_hdmi_v2_debug_abist_show(struct seq_file *m, void *arg)
  1036. {
  1037. struct mtk_hdmi *hdmi = m->private;
  1038. bool en;
  1039. u32 val;
  1040. int ret;
  1041. if (!hdmi)
  1042. return -EINVAL;
  1043. ret = regmap_read(hdmi->regs, TOP_CFG00, &val);
  1044. if (ret)
  1045. return ret;
  1046. en = FIELD_GET(HDMI_ABIST_ENABLE, val);
  1047. seq_printf(m, "HDMI Automated Built-In Self Test: %s\n",
  1048. en ? "Enabled" : "Disabled");
  1049. return 0;
  1050. }
  1051. static ssize_t mtk_hdmi_v2_debug_abist_write(struct file *file,
  1052. const char __user *ubuf,
  1053. size_t len, loff_t *offp)
  1054. {
  1055. struct seq_file *m = file->private_data;
  1056. int ret;
  1057. u32 en;
  1058. if (!m || !m->private || *offp)
  1059. return -EINVAL;
  1060. ret = kstrtouint_from_user(ubuf, len, 0, &en);
  1061. if (ret)
  1062. return ret;
  1063. if (en < 0 || en > 1)
  1064. return -EINVAL;
  1065. mtk_hdmi_v2_set_abist((struct mtk_hdmi *)m->private, en);
  1066. return len;
  1067. }
  1068. static int mtk_hdmi_v2_debug_abist_open(struct inode *inode, struct file *file)
  1069. {
  1070. return single_open(file, mtk_hdmi_v2_debug_abist_show, inode->i_private);
  1071. }
  1072. static const struct file_operations mtk_hdmi_debug_abist_fops = {
  1073. .owner = THIS_MODULE,
  1074. .open = mtk_hdmi_v2_debug_abist_open,
  1075. .read = seq_read,
  1076. .write = mtk_hdmi_v2_debug_abist_write,
  1077. .llseek = seq_lseek,
  1078. .release = single_release,
  1079. };
  1080. static void mtk_hdmi_v2_debugfs_init(struct drm_bridge *bridge, struct dentry *root)
  1081. {
  1082. struct mtk_hdmi *dpi = hdmi_ctx_from_bridge(bridge);
  1083. debugfs_create_file("hdmi_abist", 0640, root, dpi, &mtk_hdmi_debug_abist_fops);
  1084. }
  1085. static const struct drm_bridge_funcs mtk_v2_hdmi_bridge_funcs = {
  1086. .attach = mtk_hdmi_v2_bridge_attach,
  1087. .detach = mtk_hdmi_v2_bridge_detach,
  1088. .mode_fixup = mtk_hdmi_bridge_mode_fixup,
  1089. .mode_set = mtk_hdmi_bridge_mode_set,
  1090. .atomic_pre_enable = mtk_hdmi_v2_bridge_pre_enable,
  1091. .atomic_enable = mtk_hdmi_v2_bridge_enable,
  1092. .atomic_disable = mtk_hdmi_v2_bridge_disable,
  1093. .atomic_post_disable = mtk_hdmi_v2_bridge_post_disable,
  1094. .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
  1095. .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
  1096. .atomic_reset = drm_atomic_helper_bridge_reset,
  1097. .detect = mtk_hdmi_v2_bridge_detect,
  1098. .edid_read = mtk_hdmi_v2_bridge_edid_read,
  1099. .hpd_enable = mtk_hdmi_v2_hpd_enable,
  1100. .hpd_disable = mtk_hdmi_v2_hpd_disable,
  1101. .hdmi_tmds_char_rate_valid = mtk_hdmi_v2_hdmi_tmds_char_rate_valid,
  1102. .hdmi_clear_audio_infoframe = mtk_hdmi_v2_hdmi_clear_audio_infoframe,
  1103. .hdmi_write_audio_infoframe = mtk_hdmi_v2_hdmi_write_audio_infoframe,
  1104. .hdmi_clear_avi_infoframe = mtk_hdmi_v2_hdmi_clear_avi_infoframe,
  1105. .hdmi_write_avi_infoframe = mtk_hdmi_v2_hdmi_write_avi_infoframe,
  1106. .hdmi_clear_spd_infoframe = mtk_hdmi_v2_hdmi_clear_spd_infoframe,
  1107. .hdmi_write_spd_infoframe = mtk_hdmi_v2_hdmi_write_spd_infoframe,
  1108. .hdmi_clear_hdmi_infoframe = mtk_hdmi_v2_hdmi_clear_hdmi_infoframe,
  1109. .hdmi_write_hdmi_infoframe = mtk_hdmi_v2_hdmi_write_hdmi_infoframe,
  1110. .debugfs_init = mtk_hdmi_v2_debugfs_init,
  1111. };
  1112. /*
  1113. * HDMI audio codec callbacks
  1114. */
  1115. static int mtk_hdmi_v2_audio_hook_plugged_cb(struct device *dev, void *data,
  1116. hdmi_codec_plugged_cb fn,
  1117. struct device *codec_dev)
  1118. {
  1119. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  1120. bool plugged;
  1121. if (!hdmi)
  1122. return -ENODEV;
  1123. mtk_hdmi_audio_set_plugged_cb(hdmi, fn, codec_dev);
  1124. plugged = (hdmi->hpd == HDMI_PLUG_IN_AND_SINK_POWER_ON);
  1125. mtk_hdmi_v2_handle_plugged_change(hdmi, plugged);
  1126. return 0;
  1127. }
  1128. static int mtk_hdmi_v2_audio_hw_params(struct device *dev, void *data,
  1129. struct hdmi_codec_daifmt *daifmt,
  1130. struct hdmi_codec_params *params)
  1131. {
  1132. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  1133. if (hdmi->audio_enable) {
  1134. mtk_hdmi_audio_params(hdmi, daifmt, params);
  1135. mtk_hdmi_v2_aud_output_config(hdmi, &hdmi->mode);
  1136. }
  1137. return 0;
  1138. }
  1139. static int mtk_hdmi_v2_audio_startup(struct device *dev, void *data)
  1140. {
  1141. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  1142. mtk_hdmi_v2_hw_aud_enable(hdmi, true);
  1143. hdmi->audio_enable = true;
  1144. return 0;
  1145. }
  1146. static void mtk_hdmi_v2_audio_shutdown(struct device *dev, void *data)
  1147. {
  1148. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  1149. hdmi->audio_enable = false;
  1150. mtk_hdmi_v2_hw_aud_enable(hdmi, false);
  1151. }
  1152. static int mtk_hdmi_v2_audio_mute(struct device *dev, void *data, bool enable, int dir)
  1153. {
  1154. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  1155. mtk_hdmi_v2_hw_aud_mute(hdmi, enable);
  1156. return 0;
  1157. }
  1158. static const struct hdmi_codec_ops mtk_hdmi_v2_audio_codec_ops = {
  1159. .hw_params = mtk_hdmi_v2_audio_hw_params,
  1160. .audio_startup = mtk_hdmi_v2_audio_startup,
  1161. .audio_shutdown = mtk_hdmi_v2_audio_shutdown,
  1162. .mute_stream = mtk_hdmi_v2_audio_mute,
  1163. .get_eld = mtk_hdmi_audio_get_eld,
  1164. .hook_plugged_cb = mtk_hdmi_v2_audio_hook_plugged_cb,
  1165. };
  1166. static __maybe_unused int mtk_hdmi_v2_suspend(struct device *dev)
  1167. {
  1168. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  1169. mtk_hdmi_v2_disable(hdmi);
  1170. return 0;
  1171. }
  1172. static __maybe_unused int mtk_hdmi_v2_resume(struct device *dev)
  1173. {
  1174. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  1175. return mtk_hdmi_v2_enable(hdmi);
  1176. }
  1177. static SIMPLE_DEV_PM_OPS(mtk_hdmi_v2_pm_ops, mtk_hdmi_v2_suspend, mtk_hdmi_v2_resume);
  1178. static const struct mtk_hdmi_ver_conf mtk_hdmi_conf_v2 = {
  1179. .bridge_funcs = &mtk_v2_hdmi_bridge_funcs,
  1180. .codec_ops = &mtk_hdmi_v2_audio_codec_ops,
  1181. .mtk_hdmi_clock_names = mtk_hdmi_v2_clk_names,
  1182. .num_clocks = MTK_HDMI_V2_CLK_COUNT,
  1183. .interlace_allowed = true,
  1184. };
  1185. static const struct mtk_hdmi_conf mtk_hdmi_conf_mt8188 = {
  1186. .ver_conf = &mtk_hdmi_conf_v2,
  1187. .reg_hdmi_tx_cfg = HDMITX_CONFIG_MT8188
  1188. };
  1189. static const struct mtk_hdmi_conf mtk_hdmi_conf_mt8195 = {
  1190. .ver_conf = &mtk_hdmi_conf_v2,
  1191. .reg_hdmi_tx_cfg = HDMITX_CONFIG_MT8195
  1192. };
  1193. static int mtk_hdmi_v2_probe(struct platform_device *pdev)
  1194. {
  1195. struct mtk_hdmi *hdmi;
  1196. int ret;
  1197. /* Populate HDMI sub-devices if present */
  1198. ret = devm_of_platform_populate(&pdev->dev);
  1199. if (ret)
  1200. return ret;
  1201. hdmi = mtk_hdmi_common_probe(pdev);
  1202. if (IS_ERR(hdmi))
  1203. return PTR_ERR(hdmi);
  1204. hdmi->hpd = HDMI_PLUG_OUT;
  1205. /* Disable all HW interrupts at probe stage */
  1206. mtk_hdmi_v2_hwirq_disable(hdmi);
  1207. /*
  1208. * In case bootloader leaves HDMI enabled before booting, make
  1209. * sure that any interrupt that was left is cleared by setting
  1210. * all bits in the INT_CLR registers for all 32+19 interrupts.
  1211. */
  1212. regmap_write(hdmi->regs, TOP_INT_CLR00, GENMASK(31, 0));
  1213. regmap_write(hdmi->regs, TOP_INT_CLR01, GENMASK(18, 0));
  1214. /* Restore interrupt clearing registers to zero */
  1215. regmap_write(hdmi->regs, TOP_INT_CLR00, 0);
  1216. regmap_write(hdmi->regs, TOP_INT_CLR01, 0);
  1217. /*
  1218. * Install the ISR but keep it disabled: as the interrupts are
  1219. * being set up in the .bridge_attach() callback which will
  1220. * enable both the right HW IRQs and the ISR.
  1221. */
  1222. irq_set_status_flags(hdmi->irq, IRQ_NOAUTOEN);
  1223. ret = devm_request_threaded_irq(&pdev->dev, hdmi->irq, mtk_hdmi_v2_isr,
  1224. mtk_hdmi_v2_isr_thread,
  1225. IRQ_TYPE_LEVEL_HIGH,
  1226. dev_name(&pdev->dev), hdmi);
  1227. if (ret)
  1228. return dev_err_probe(&pdev->dev, ret, "Cannot request IRQ\n");
  1229. ret = devm_pm_runtime_enable(&pdev->dev);
  1230. if (ret)
  1231. return dev_err_probe(&pdev->dev, ret, "Cannot enable Runtime PM\n");
  1232. return 0;
  1233. }
  1234. static void mtk_hdmi_v2_remove(struct platform_device *pdev)
  1235. {
  1236. struct mtk_hdmi *hdmi = platform_get_drvdata(pdev);
  1237. i2c_put_adapter(hdmi->ddc_adpt);
  1238. }
  1239. static const struct of_device_id mtk_drm_hdmi_v2_of_ids[] = {
  1240. { .compatible = "mediatek,mt8188-hdmi-tx", .data = &mtk_hdmi_conf_mt8188 },
  1241. { .compatible = "mediatek,mt8195-hdmi-tx", .data = &mtk_hdmi_conf_mt8195 },
  1242. { /* sentinel */ }
  1243. };
  1244. MODULE_DEVICE_TABLE(of, mtk_drm_hdmi_v2_of_ids);
  1245. static struct platform_driver mtk_hdmi_v2_driver = {
  1246. .probe = mtk_hdmi_v2_probe,
  1247. .remove = mtk_hdmi_v2_remove,
  1248. .driver = {
  1249. .name = "mediatek-drm-hdmi-v2",
  1250. .of_match_table = mtk_drm_hdmi_v2_of_ids,
  1251. .pm = &mtk_hdmi_v2_pm_ops,
  1252. },
  1253. };
  1254. module_platform_driver(mtk_hdmi_v2_driver);
  1255. MODULE_AUTHOR("AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>>");
  1256. MODULE_DESCRIPTION("MediaTek HDMIv2 Driver");
  1257. MODULE_LICENSE("GPL");
  1258. MODULE_IMPORT_NS("DRM_MTK_HDMI");