mtk_hdmi.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014 MediaTek Inc.
  4. * Author: Jie Qiu <jie.qiu@mediatek.com>
  5. */
  6. #include <linux/arm-smccc.h>
  7. #include <linux/clk.h>
  8. #include <linux/delay.h>
  9. #include <linux/hdmi.h>
  10. #include <linux/i2c.h>
  11. #include <linux/io.h>
  12. #include <linux/kernel.h>
  13. #include <linux/mfd/syscon.h>
  14. #include <linux/module.h>
  15. #include <linux/mutex.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/of.h>
  18. #include <linux/of_graph.h>
  19. #include <linux/phy/phy.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/regmap.h>
  22. #include <sound/hdmi-codec.h>
  23. #include <drm/drm_atomic_helper.h>
  24. #include <drm/drm_bridge.h>
  25. #include <drm/drm_crtc.h>
  26. #include <drm/drm_edid.h>
  27. #include <drm/drm_print.h>
  28. #include <drm/drm_probe_helper.h>
  29. #include "mtk_cec.h"
  30. #include "mtk_hdmi_common.h"
  31. #include "mtk_hdmi_regs.h"
  32. #define NCTS_BYTES 7
  33. enum mtk_hdmi_clk_id {
  34. MTK_HDMI_CLK_HDMI_PIXEL,
  35. MTK_HDMI_CLK_HDMI_PLL,
  36. MTK_HDMI_CLK_AUD_BCLK,
  37. MTK_HDMI_CLK_AUD_SPDIF,
  38. MTK_HDMI_CLK_COUNT
  39. };
  40. static void mtk_hdmi_hw_vid_black(struct mtk_hdmi *hdmi, bool black)
  41. {
  42. regmap_update_bits(hdmi->regs, VIDEO_CFG_4,
  43. VIDEO_SOURCE_SEL, black ? GEN_RGB : NORMAL_PATH);
  44. }
  45. static void mtk_hdmi_hw_make_reg_writable(struct mtk_hdmi *hdmi, bool enable)
  46. {
  47. struct arm_smccc_res res;
  48. /*
  49. * MT8173 HDMI hardware has an output control bit to enable/disable HDMI
  50. * output. This bit can only be controlled in ARM supervisor mode.
  51. * The ARM trusted firmware provides an API for the HDMI driver to set
  52. * this control bit to enable HDMI output in supervisor mode.
  53. */
  54. if (hdmi->conf && hdmi->conf->tz_disabled)
  55. regmap_update_bits(hdmi->sys_regmap,
  56. hdmi->sys_offset + HDMI_SYS_CFG20,
  57. 0x80008005, enable ? 0x80000005 : 0x8000);
  58. else
  59. arm_smccc_smc(MTK_SIP_SET_AUTHORIZED_SECURE_REG, 0x14000904,
  60. 0x80000000, 0, 0, 0, 0, 0, &res);
  61. regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG20,
  62. HDMI_PCLK_FREE_RUN, enable ? HDMI_PCLK_FREE_RUN : 0);
  63. regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG1C,
  64. HDMI_ON | ANLG_ON, enable ? (HDMI_ON | ANLG_ON) : 0);
  65. }
  66. static void mtk_hdmi_hw_1p4_version_enable(struct mtk_hdmi *hdmi, bool enable)
  67. {
  68. regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG20,
  69. HDMI2P0_EN, enable ? 0 : HDMI2P0_EN);
  70. }
  71. static void mtk_hdmi_hw_aud_mute(struct mtk_hdmi *hdmi)
  72. {
  73. regmap_set_bits(hdmi->regs, GRL_AUDIO_CFG, AUDIO_ZERO);
  74. }
  75. static void mtk_hdmi_hw_aud_unmute(struct mtk_hdmi *hdmi)
  76. {
  77. regmap_clear_bits(hdmi->regs, GRL_AUDIO_CFG, AUDIO_ZERO);
  78. }
  79. static void mtk_hdmi_hw_reset(struct mtk_hdmi *hdmi)
  80. {
  81. regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG1C,
  82. HDMI_RST, HDMI_RST);
  83. regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG1C,
  84. HDMI_RST, 0);
  85. regmap_clear_bits(hdmi->regs, GRL_CFG3, CFG3_CONTROL_PACKET_DELAY);
  86. regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG1C,
  87. ANLG_ON, ANLG_ON);
  88. }
  89. static void mtk_hdmi_hw_enable_notice(struct mtk_hdmi *hdmi, bool enable_notice)
  90. {
  91. regmap_update_bits(hdmi->regs, GRL_CFG2, CFG2_NOTICE_EN,
  92. enable_notice ? CFG2_NOTICE_EN : 0);
  93. }
  94. static void mtk_hdmi_hw_write_int_mask(struct mtk_hdmi *hdmi, u32 int_mask)
  95. {
  96. regmap_write(hdmi->regs, GRL_INT_MASK, int_mask);
  97. }
  98. static void mtk_hdmi_hw_enable_dvi_mode(struct mtk_hdmi *hdmi, bool enable)
  99. {
  100. regmap_update_bits(hdmi->regs, GRL_CFG1, CFG1_DVI, enable ? CFG1_DVI : 0);
  101. }
  102. static void mtk_hdmi_hw_send_info_frame(struct mtk_hdmi *hdmi, u8 *buffer,
  103. u8 len)
  104. {
  105. u32 ctrl_reg = GRL_CTRL;
  106. int i;
  107. u8 *frame_data;
  108. enum hdmi_infoframe_type frame_type;
  109. u8 frame_ver;
  110. u8 frame_len;
  111. u8 checksum;
  112. int ctrl_frame_en = 0;
  113. frame_type = *buffer++;
  114. frame_ver = *buffer++;
  115. frame_len = *buffer++;
  116. checksum = *buffer++;
  117. frame_data = buffer;
  118. dev_dbg(hdmi->dev,
  119. "frame_type:0x%x,frame_ver:0x%x,frame_len:0x%x,checksum:0x%x\n",
  120. frame_type, frame_ver, frame_len, checksum);
  121. switch (frame_type) {
  122. case HDMI_INFOFRAME_TYPE_AVI:
  123. ctrl_frame_en = CTRL_AVI_EN;
  124. ctrl_reg = GRL_CTRL;
  125. break;
  126. case HDMI_INFOFRAME_TYPE_SPD:
  127. ctrl_frame_en = CTRL_SPD_EN;
  128. ctrl_reg = GRL_CTRL;
  129. break;
  130. case HDMI_INFOFRAME_TYPE_AUDIO:
  131. ctrl_frame_en = CTRL_AUDIO_EN;
  132. ctrl_reg = GRL_CTRL;
  133. break;
  134. case HDMI_INFOFRAME_TYPE_VENDOR:
  135. ctrl_frame_en = VS_EN;
  136. ctrl_reg = GRL_ACP_ISRC_CTRL;
  137. break;
  138. default:
  139. dev_err(hdmi->dev, "Unknown infoframe type %d\n", frame_type);
  140. return;
  141. }
  142. regmap_clear_bits(hdmi->regs, ctrl_reg, ctrl_frame_en);
  143. regmap_write(hdmi->regs, GRL_INFOFRM_TYPE, frame_type);
  144. regmap_write(hdmi->regs, GRL_INFOFRM_VER, frame_ver);
  145. regmap_write(hdmi->regs, GRL_INFOFRM_LNG, frame_len);
  146. regmap_write(hdmi->regs, GRL_IFM_PORT, checksum);
  147. for (i = 0; i < frame_len; i++)
  148. regmap_write(hdmi->regs, GRL_IFM_PORT, frame_data[i]);
  149. regmap_set_bits(hdmi->regs, ctrl_reg, ctrl_frame_en);
  150. }
  151. static void mtk_hdmi_hw_send_aud_packet(struct mtk_hdmi *hdmi, bool enable)
  152. {
  153. regmap_update_bits(hdmi->regs, GRL_SHIFT_R2,
  154. AUDIO_PACKET_OFF, enable ? 0 : AUDIO_PACKET_OFF);
  155. }
  156. static void mtk_hdmi_hw_config_sys(struct mtk_hdmi *hdmi)
  157. {
  158. regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG20,
  159. HDMI_OUT_FIFO_EN | MHL_MODE_ON, 0);
  160. usleep_range(2000, 4000);
  161. regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG20,
  162. HDMI_OUT_FIFO_EN | MHL_MODE_ON, HDMI_OUT_FIFO_EN);
  163. }
  164. static void mtk_hdmi_hw_set_deep_color_mode(struct mtk_hdmi *hdmi)
  165. {
  166. regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG20,
  167. DEEP_COLOR_MODE_MASK | DEEP_COLOR_EN,
  168. COLOR_8BIT_MODE);
  169. }
  170. static void mtk_hdmi_hw_send_av_mute(struct mtk_hdmi *hdmi)
  171. {
  172. regmap_clear_bits(hdmi->regs, GRL_CFG4, CTRL_AVMUTE);
  173. usleep_range(2000, 4000);
  174. regmap_set_bits(hdmi->regs, GRL_CFG4, CTRL_AVMUTE);
  175. }
  176. static void mtk_hdmi_hw_send_av_unmute(struct mtk_hdmi *hdmi)
  177. {
  178. regmap_update_bits(hdmi->regs, GRL_CFG4, CFG4_AV_UNMUTE_EN | CFG4_AV_UNMUTE_SET,
  179. CFG4_AV_UNMUTE_EN);
  180. usleep_range(2000, 4000);
  181. regmap_update_bits(hdmi->regs, GRL_CFG4, CFG4_AV_UNMUTE_EN | CFG4_AV_UNMUTE_SET,
  182. CFG4_AV_UNMUTE_SET);
  183. }
  184. static void mtk_hdmi_hw_ncts_enable(struct mtk_hdmi *hdmi, bool on)
  185. {
  186. regmap_update_bits(hdmi->regs, GRL_CTS_CTRL, CTS_CTRL_SOFT,
  187. on ? 0 : CTS_CTRL_SOFT);
  188. }
  189. static void mtk_hdmi_hw_ncts_auto_write_enable(struct mtk_hdmi *hdmi,
  190. bool enable)
  191. {
  192. regmap_update_bits(hdmi->regs, GRL_CTS_CTRL, NCTS_WRI_ANYTIME,
  193. enable ? NCTS_WRI_ANYTIME : 0);
  194. }
  195. static void mtk_hdmi_hw_msic_setting(struct mtk_hdmi *hdmi,
  196. struct drm_display_mode *mode)
  197. {
  198. regmap_clear_bits(hdmi->regs, GRL_CFG4, CFG4_MHL_MODE);
  199. if (mode->flags & DRM_MODE_FLAG_INTERLACE &&
  200. mode->clock == 74250 &&
  201. mode->vdisplay == 1080)
  202. regmap_clear_bits(hdmi->regs, GRL_CFG2, CFG2_MHL_DE_SEL);
  203. else
  204. regmap_set_bits(hdmi->regs, GRL_CFG2, CFG2_MHL_DE_SEL);
  205. }
  206. static void mtk_hdmi_hw_aud_set_channel_swap(struct mtk_hdmi *hdmi,
  207. enum hdmi_aud_channel_swap_type swap)
  208. {
  209. u8 swap_bit;
  210. switch (swap) {
  211. case HDMI_AUD_SWAP_LR:
  212. swap_bit = LR_SWAP;
  213. break;
  214. case HDMI_AUD_SWAP_LFE_CC:
  215. swap_bit = LFE_CC_SWAP;
  216. break;
  217. case HDMI_AUD_SWAP_LSRS:
  218. swap_bit = LSRS_SWAP;
  219. break;
  220. case HDMI_AUD_SWAP_RLS_RRS:
  221. swap_bit = RLS_RRS_SWAP;
  222. break;
  223. case HDMI_AUD_SWAP_LR_STATUS:
  224. swap_bit = LR_STATUS_SWAP;
  225. break;
  226. default:
  227. swap_bit = LFE_CC_SWAP;
  228. break;
  229. }
  230. regmap_update_bits(hdmi->regs, GRL_CH_SWAP, 0xff, swap_bit);
  231. }
  232. static void mtk_hdmi_hw_aud_set_bit_num(struct mtk_hdmi *hdmi,
  233. enum hdmi_audio_sample_size bit_num)
  234. {
  235. u32 val;
  236. switch (bit_num) {
  237. case HDMI_AUDIO_SAMPLE_SIZE_16:
  238. val = AOUT_16BIT;
  239. break;
  240. case HDMI_AUDIO_SAMPLE_SIZE_20:
  241. val = AOUT_20BIT;
  242. break;
  243. case HDMI_AUDIO_SAMPLE_SIZE_24:
  244. case HDMI_AUDIO_SAMPLE_SIZE_STREAM:
  245. val = AOUT_24BIT;
  246. break;
  247. }
  248. regmap_update_bits(hdmi->regs, GRL_AOUT_CFG, AOUT_BNUM_SEL_MASK, val);
  249. }
  250. static void mtk_hdmi_hw_aud_set_i2s_fmt(struct mtk_hdmi *hdmi,
  251. enum hdmi_aud_i2s_fmt i2s_fmt)
  252. {
  253. u32 val;
  254. regmap_read(hdmi->regs, GRL_CFG0, &val);
  255. val &= ~(CFG0_W_LENGTH_MASK | CFG0_I2S_MODE_MASK);
  256. switch (i2s_fmt) {
  257. case HDMI_I2S_MODE_RJT_24BIT:
  258. val |= CFG0_I2S_MODE_RTJ | CFG0_W_LENGTH_24BIT;
  259. break;
  260. case HDMI_I2S_MODE_RJT_16BIT:
  261. val |= CFG0_I2S_MODE_RTJ | CFG0_W_LENGTH_16BIT;
  262. break;
  263. case HDMI_I2S_MODE_LJT_24BIT:
  264. default:
  265. val |= CFG0_I2S_MODE_LTJ | CFG0_W_LENGTH_24BIT;
  266. break;
  267. case HDMI_I2S_MODE_LJT_16BIT:
  268. val |= CFG0_I2S_MODE_LTJ | CFG0_W_LENGTH_16BIT;
  269. break;
  270. case HDMI_I2S_MODE_I2S_24BIT:
  271. val |= CFG0_I2S_MODE_I2S | CFG0_W_LENGTH_24BIT;
  272. break;
  273. case HDMI_I2S_MODE_I2S_16BIT:
  274. val |= CFG0_I2S_MODE_I2S | CFG0_W_LENGTH_16BIT;
  275. break;
  276. }
  277. regmap_write(hdmi->regs, GRL_CFG0, val);
  278. }
  279. static void mtk_hdmi_hw_audio_config(struct mtk_hdmi *hdmi, bool dst)
  280. {
  281. const u8 mask = HIGH_BIT_RATE | DST_NORMAL_DOUBLE | SACD_DST | DSD_SEL;
  282. u8 val;
  283. /* Disable high bitrate, set DST packet normal/double */
  284. regmap_clear_bits(hdmi->regs, GRL_AOUT_CFG, HIGH_BIT_RATE_PACKET_ALIGN);
  285. if (dst)
  286. val = DST_NORMAL_DOUBLE | SACD_DST;
  287. else
  288. val = 0;
  289. regmap_update_bits(hdmi->regs, GRL_AUDIO_CFG, mask, val);
  290. }
  291. static void mtk_hdmi_hw_aud_set_i2s_chan_num(struct mtk_hdmi *hdmi,
  292. enum hdmi_aud_channel_type channel_type,
  293. u8 channel_count)
  294. {
  295. unsigned int ch_switch;
  296. u8 i2s_uv;
  297. ch_switch = CH_SWITCH(7, 7) | CH_SWITCH(6, 6) |
  298. CH_SWITCH(5, 5) | CH_SWITCH(4, 4) |
  299. CH_SWITCH(3, 3) | CH_SWITCH(1, 2) |
  300. CH_SWITCH(2, 1) | CH_SWITCH(0, 0);
  301. if (channel_count == 2) {
  302. i2s_uv = I2S_UV_CH_EN(0);
  303. } else if (channel_count == 3 || channel_count == 4) {
  304. if (channel_count == 4 &&
  305. (channel_type == HDMI_AUD_CHAN_TYPE_3_0_LRS ||
  306. channel_type == HDMI_AUD_CHAN_TYPE_4_0))
  307. i2s_uv = I2S_UV_CH_EN(2) | I2S_UV_CH_EN(0);
  308. else
  309. i2s_uv = I2S_UV_CH_EN(3) | I2S_UV_CH_EN(2);
  310. } else if (channel_count == 6 || channel_count == 5) {
  311. if (channel_count == 6 &&
  312. channel_type != HDMI_AUD_CHAN_TYPE_5_1 &&
  313. channel_type != HDMI_AUD_CHAN_TYPE_4_1_CLRS) {
  314. i2s_uv = I2S_UV_CH_EN(3) | I2S_UV_CH_EN(2) |
  315. I2S_UV_CH_EN(1) | I2S_UV_CH_EN(0);
  316. } else {
  317. i2s_uv = I2S_UV_CH_EN(2) | I2S_UV_CH_EN(1) |
  318. I2S_UV_CH_EN(0);
  319. }
  320. } else if (channel_count == 8 || channel_count == 7) {
  321. i2s_uv = I2S_UV_CH_EN(3) | I2S_UV_CH_EN(2) |
  322. I2S_UV_CH_EN(1) | I2S_UV_CH_EN(0);
  323. } else {
  324. i2s_uv = I2S_UV_CH_EN(0);
  325. }
  326. regmap_write(hdmi->regs, GRL_CH_SW0, ch_switch & 0xff);
  327. regmap_write(hdmi->regs, GRL_CH_SW1, (ch_switch >> 8) & 0xff);
  328. regmap_write(hdmi->regs, GRL_CH_SW2, (ch_switch >> 16) & 0xff);
  329. regmap_write(hdmi->regs, GRL_I2S_UV, i2s_uv);
  330. }
  331. static void mtk_hdmi_hw_aud_set_input_type(struct mtk_hdmi *hdmi,
  332. enum hdmi_aud_input_type input_type)
  333. {
  334. u32 val;
  335. regmap_read(hdmi->regs, GRL_CFG1, &val);
  336. if (input_type == HDMI_AUD_INPUT_I2S &&
  337. (val & CFG1_SPDIF) == CFG1_SPDIF) {
  338. val &= ~CFG1_SPDIF;
  339. } else if (input_type == HDMI_AUD_INPUT_SPDIF &&
  340. (val & CFG1_SPDIF) == 0) {
  341. val |= CFG1_SPDIF;
  342. }
  343. regmap_write(hdmi->regs, GRL_CFG1, val);
  344. }
  345. static void mtk_hdmi_hw_aud_set_channel_status(struct mtk_hdmi *hdmi,
  346. u8 *channel_status)
  347. {
  348. int i;
  349. for (i = 0; i < 5; i++) {
  350. regmap_write(hdmi->regs, GRL_I2S_C_STA0 + i * 4, channel_status[i]);
  351. regmap_write(hdmi->regs, GRL_L_STATUS_0 + i * 4, channel_status[i]);
  352. regmap_write(hdmi->regs, GRL_R_STATUS_0 + i * 4, channel_status[i]);
  353. }
  354. for (; i < 24; i++) {
  355. regmap_write(hdmi->regs, GRL_L_STATUS_0 + i * 4, 0);
  356. regmap_write(hdmi->regs, GRL_R_STATUS_0 + i * 4, 0);
  357. }
  358. }
  359. static void mtk_hdmi_hw_aud_src_reenable(struct mtk_hdmi *hdmi)
  360. {
  361. u32 val;
  362. regmap_read(hdmi->regs, GRL_MIX_CTRL, &val);
  363. if (val & MIX_CTRL_SRC_EN) {
  364. val &= ~MIX_CTRL_SRC_EN;
  365. regmap_write(hdmi->regs, GRL_MIX_CTRL, val);
  366. usleep_range(255, 512);
  367. val |= MIX_CTRL_SRC_EN;
  368. regmap_write(hdmi->regs, GRL_MIX_CTRL, val);
  369. }
  370. }
  371. static void mtk_hdmi_hw_aud_src_disable(struct mtk_hdmi *hdmi)
  372. {
  373. u32 val;
  374. regmap_read(hdmi->regs, GRL_MIX_CTRL, &val);
  375. val &= ~MIX_CTRL_SRC_EN;
  376. regmap_write(hdmi->regs, GRL_MIX_CTRL, val);
  377. regmap_write(hdmi->regs, GRL_SHIFT_L1, 0x00);
  378. }
  379. static void mtk_hdmi_hw_aud_set_mclk(struct mtk_hdmi *hdmi,
  380. enum hdmi_aud_mclk mclk)
  381. {
  382. u32 val;
  383. regmap_read(hdmi->regs, GRL_CFG5, &val);
  384. val &= CFG5_CD_RATIO_MASK;
  385. switch (mclk) {
  386. case HDMI_AUD_MCLK_128FS:
  387. val |= CFG5_FS128;
  388. break;
  389. case HDMI_AUD_MCLK_256FS:
  390. val |= CFG5_FS256;
  391. break;
  392. case HDMI_AUD_MCLK_384FS:
  393. val |= CFG5_FS384;
  394. break;
  395. case HDMI_AUD_MCLK_512FS:
  396. val |= CFG5_FS512;
  397. break;
  398. case HDMI_AUD_MCLK_768FS:
  399. val |= CFG5_FS768;
  400. break;
  401. default:
  402. val |= CFG5_FS256;
  403. break;
  404. }
  405. regmap_write(hdmi->regs, GRL_CFG5, val);
  406. }
  407. static void do_hdmi_hw_aud_set_ncts(struct mtk_hdmi *hdmi, unsigned int n,
  408. unsigned int cts)
  409. {
  410. unsigned char val[NCTS_BYTES];
  411. int i;
  412. regmap_write(hdmi->regs, GRL_NCTS, 0);
  413. regmap_write(hdmi->regs, GRL_NCTS, 0);
  414. regmap_write(hdmi->regs, GRL_NCTS, 0);
  415. memset(val, 0, sizeof(val));
  416. val[0] = (cts >> 24) & 0xff;
  417. val[1] = (cts >> 16) & 0xff;
  418. val[2] = (cts >> 8) & 0xff;
  419. val[3] = cts & 0xff;
  420. val[4] = (n >> 16) & 0xff;
  421. val[5] = (n >> 8) & 0xff;
  422. val[6] = n & 0xff;
  423. for (i = 0; i < NCTS_BYTES; i++)
  424. regmap_write(hdmi->regs, GRL_NCTS, val[i]);
  425. }
  426. static void mtk_hdmi_hw_aud_set_ncts(struct mtk_hdmi *hdmi,
  427. unsigned int sample_rate,
  428. unsigned int clock)
  429. {
  430. unsigned int n, cts;
  431. mtk_hdmi_get_ncts(sample_rate, clock, &n, &cts);
  432. dev_dbg(hdmi->dev, "%s: sample_rate=%u, clock=%d, cts=%u, n=%u\n",
  433. __func__, sample_rate, clock, n, cts);
  434. regmap_update_bits(hdmi->regs, DUMMY_304, AUDIO_I2S_NCTS_SEL, AUDIO_I2S_NCTS_SEL_64);
  435. do_hdmi_hw_aud_set_ncts(hdmi, n, cts);
  436. }
  437. static u8 mtk_hdmi_aud_get_chnl_count(enum hdmi_aud_channel_type channel_type)
  438. {
  439. switch (channel_type) {
  440. case HDMI_AUD_CHAN_TYPE_1_0:
  441. case HDMI_AUD_CHAN_TYPE_1_1:
  442. case HDMI_AUD_CHAN_TYPE_2_0:
  443. return 2;
  444. case HDMI_AUD_CHAN_TYPE_2_1:
  445. case HDMI_AUD_CHAN_TYPE_3_0:
  446. return 3;
  447. case HDMI_AUD_CHAN_TYPE_3_1:
  448. case HDMI_AUD_CHAN_TYPE_4_0:
  449. case HDMI_AUD_CHAN_TYPE_3_0_LRS:
  450. return 4;
  451. case HDMI_AUD_CHAN_TYPE_4_1:
  452. case HDMI_AUD_CHAN_TYPE_5_0:
  453. case HDMI_AUD_CHAN_TYPE_3_1_LRS:
  454. case HDMI_AUD_CHAN_TYPE_4_0_CLRS:
  455. return 5;
  456. case HDMI_AUD_CHAN_TYPE_5_1:
  457. case HDMI_AUD_CHAN_TYPE_6_0:
  458. case HDMI_AUD_CHAN_TYPE_4_1_CLRS:
  459. case HDMI_AUD_CHAN_TYPE_6_0_CS:
  460. case HDMI_AUD_CHAN_TYPE_6_0_CH:
  461. case HDMI_AUD_CHAN_TYPE_6_0_OH:
  462. case HDMI_AUD_CHAN_TYPE_6_0_CHR:
  463. return 6;
  464. case HDMI_AUD_CHAN_TYPE_6_1:
  465. case HDMI_AUD_CHAN_TYPE_6_1_CS:
  466. case HDMI_AUD_CHAN_TYPE_6_1_CH:
  467. case HDMI_AUD_CHAN_TYPE_6_1_OH:
  468. case HDMI_AUD_CHAN_TYPE_6_1_CHR:
  469. case HDMI_AUD_CHAN_TYPE_7_0:
  470. case HDMI_AUD_CHAN_TYPE_7_0_LH_RH:
  471. case HDMI_AUD_CHAN_TYPE_7_0_LSR_RSR:
  472. case HDMI_AUD_CHAN_TYPE_7_0_LC_RC:
  473. case HDMI_AUD_CHAN_TYPE_7_0_LW_RW:
  474. case HDMI_AUD_CHAN_TYPE_7_0_LSD_RSD:
  475. case HDMI_AUD_CHAN_TYPE_7_0_LSS_RSS:
  476. case HDMI_AUD_CHAN_TYPE_7_0_LHS_RHS:
  477. case HDMI_AUD_CHAN_TYPE_7_0_CS_CH:
  478. case HDMI_AUD_CHAN_TYPE_7_0_CS_OH:
  479. case HDMI_AUD_CHAN_TYPE_7_0_CS_CHR:
  480. case HDMI_AUD_CHAN_TYPE_7_0_CH_OH:
  481. case HDMI_AUD_CHAN_TYPE_7_0_CH_CHR:
  482. case HDMI_AUD_CHAN_TYPE_7_0_OH_CHR:
  483. case HDMI_AUD_CHAN_TYPE_7_0_LSS_RSS_LSR_RSR:
  484. case HDMI_AUD_CHAN_TYPE_8_0_LH_RH_CS:
  485. return 7;
  486. case HDMI_AUD_CHAN_TYPE_7_1:
  487. case HDMI_AUD_CHAN_TYPE_7_1_LH_RH:
  488. case HDMI_AUD_CHAN_TYPE_7_1_LSR_RSR:
  489. case HDMI_AUD_CHAN_TYPE_7_1_LC_RC:
  490. case HDMI_AUD_CHAN_TYPE_7_1_LW_RW:
  491. case HDMI_AUD_CHAN_TYPE_7_1_LSD_RSD:
  492. case HDMI_AUD_CHAN_TYPE_7_1_LSS_RSS:
  493. case HDMI_AUD_CHAN_TYPE_7_1_LHS_RHS:
  494. case HDMI_AUD_CHAN_TYPE_7_1_CS_CH:
  495. case HDMI_AUD_CHAN_TYPE_7_1_CS_OH:
  496. case HDMI_AUD_CHAN_TYPE_7_1_CS_CHR:
  497. case HDMI_AUD_CHAN_TYPE_7_1_CH_OH:
  498. case HDMI_AUD_CHAN_TYPE_7_1_CH_CHR:
  499. case HDMI_AUD_CHAN_TYPE_7_1_OH_CHR:
  500. case HDMI_AUD_CHAN_TYPE_7_1_LSS_RSS_LSR_RSR:
  501. return 8;
  502. default:
  503. return 2;
  504. }
  505. }
  506. static int mtk_hdmi_video_change_vpll(struct mtk_hdmi *hdmi, u32 clock)
  507. {
  508. unsigned long rate;
  509. int ret;
  510. /* The DPI driver already should have set TVDPLL to the correct rate */
  511. ret = clk_set_rate(hdmi->clk[MTK_HDMI_CLK_HDMI_PLL], clock);
  512. if (ret) {
  513. dev_err(hdmi->dev, "Failed to set PLL to %u Hz: %d\n", clock,
  514. ret);
  515. return ret;
  516. }
  517. rate = clk_get_rate(hdmi->clk[MTK_HDMI_CLK_HDMI_PLL]);
  518. if (DIV_ROUND_CLOSEST(rate, 1000) != DIV_ROUND_CLOSEST(clock, 1000))
  519. dev_warn(hdmi->dev, "Want PLL %u Hz, got %lu Hz\n", clock,
  520. rate);
  521. else
  522. dev_dbg(hdmi->dev, "Want PLL %u Hz, got %lu Hz\n", clock, rate);
  523. mtk_hdmi_hw_config_sys(hdmi);
  524. mtk_hdmi_hw_set_deep_color_mode(hdmi);
  525. return 0;
  526. }
  527. static void mtk_hdmi_video_set_display_mode(struct mtk_hdmi *hdmi,
  528. struct drm_display_mode *mode)
  529. {
  530. mtk_hdmi_hw_reset(hdmi);
  531. mtk_hdmi_hw_enable_notice(hdmi, true);
  532. mtk_hdmi_hw_write_int_mask(hdmi, 0xff);
  533. mtk_hdmi_hw_enable_dvi_mode(hdmi, hdmi->dvi_mode);
  534. mtk_hdmi_hw_ncts_auto_write_enable(hdmi, true);
  535. mtk_hdmi_hw_msic_setting(hdmi, mode);
  536. }
  537. static void mtk_hdmi_aud_set_input(struct mtk_hdmi *hdmi)
  538. {
  539. enum hdmi_aud_channel_type chan_type;
  540. u8 chan_count;
  541. bool dst;
  542. mtk_hdmi_hw_aud_set_channel_swap(hdmi, HDMI_AUD_SWAP_LFE_CC);
  543. regmap_set_bits(hdmi->regs, GRL_MIX_CTRL, MIX_CTRL_FLAT);
  544. if (hdmi->aud_param.aud_input_type == HDMI_AUD_INPUT_SPDIF &&
  545. hdmi->aud_param.aud_codec == HDMI_AUDIO_CODING_TYPE_DST) {
  546. mtk_hdmi_hw_aud_set_bit_num(hdmi, HDMI_AUDIO_SAMPLE_SIZE_24);
  547. } else if (hdmi->aud_param.aud_i2s_fmt == HDMI_I2S_MODE_LJT_24BIT) {
  548. hdmi->aud_param.aud_i2s_fmt = HDMI_I2S_MODE_LJT_16BIT;
  549. }
  550. mtk_hdmi_hw_aud_set_i2s_fmt(hdmi, hdmi->aud_param.aud_i2s_fmt);
  551. mtk_hdmi_hw_aud_set_bit_num(hdmi, HDMI_AUDIO_SAMPLE_SIZE_24);
  552. dst = ((hdmi->aud_param.aud_input_type == HDMI_AUD_INPUT_SPDIF) &&
  553. (hdmi->aud_param.aud_codec == HDMI_AUDIO_CODING_TYPE_DST));
  554. mtk_hdmi_hw_audio_config(hdmi, dst);
  555. if (hdmi->aud_param.aud_input_type == HDMI_AUD_INPUT_SPDIF)
  556. chan_type = HDMI_AUD_CHAN_TYPE_2_0;
  557. else
  558. chan_type = hdmi->aud_param.aud_input_chan_type;
  559. chan_count = mtk_hdmi_aud_get_chnl_count(chan_type);
  560. mtk_hdmi_hw_aud_set_i2s_chan_num(hdmi, chan_type, chan_count);
  561. mtk_hdmi_hw_aud_set_input_type(hdmi, hdmi->aud_param.aud_input_type);
  562. }
  563. static int mtk_hdmi_aud_set_src(struct mtk_hdmi *hdmi,
  564. struct drm_display_mode *display_mode)
  565. {
  566. unsigned int sample_rate = hdmi->aud_param.codec_params.sample_rate;
  567. mtk_hdmi_hw_ncts_enable(hdmi, false);
  568. mtk_hdmi_hw_aud_src_disable(hdmi);
  569. regmap_clear_bits(hdmi->regs, GRL_CFG2, CFG2_ACLK_INV);
  570. if (hdmi->aud_param.aud_input_type == HDMI_AUD_INPUT_I2S) {
  571. switch (sample_rate) {
  572. case 32000:
  573. case 44100:
  574. case 48000:
  575. case 88200:
  576. case 96000:
  577. break;
  578. default:
  579. return -EINVAL;
  580. }
  581. mtk_hdmi_hw_aud_set_mclk(hdmi, hdmi->aud_param.aud_mclk);
  582. } else {
  583. switch (sample_rate) {
  584. case 32000:
  585. case 44100:
  586. case 48000:
  587. break;
  588. default:
  589. return -EINVAL;
  590. }
  591. mtk_hdmi_hw_aud_set_mclk(hdmi, HDMI_AUD_MCLK_128FS);
  592. }
  593. mtk_hdmi_hw_aud_set_ncts(hdmi, sample_rate, display_mode->clock);
  594. mtk_hdmi_hw_aud_src_reenable(hdmi);
  595. return 0;
  596. }
  597. static int mtk_hdmi_aud_output_config(struct mtk_hdmi *hdmi,
  598. struct drm_display_mode *display_mode)
  599. {
  600. mtk_hdmi_hw_aud_mute(hdmi);
  601. mtk_hdmi_hw_send_aud_packet(hdmi, false);
  602. mtk_hdmi_aud_set_input(hdmi);
  603. mtk_hdmi_aud_set_src(hdmi, display_mode);
  604. mtk_hdmi_hw_aud_set_channel_status(hdmi,
  605. hdmi->aud_param.codec_params.iec.status);
  606. usleep_range(50, 100);
  607. mtk_hdmi_hw_ncts_enable(hdmi, true);
  608. mtk_hdmi_hw_send_aud_packet(hdmi, true);
  609. mtk_hdmi_hw_aud_unmute(hdmi);
  610. return 0;
  611. }
  612. static int mtk_hdmi_setup_avi_infoframe(struct mtk_hdmi *hdmi,
  613. struct drm_display_mode *mode)
  614. {
  615. struct hdmi_avi_infoframe frame;
  616. u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AVI_INFOFRAME_SIZE];
  617. ssize_t err;
  618. err = drm_hdmi_avi_infoframe_from_display_mode(&frame,
  619. hdmi->curr_conn, mode);
  620. if (err < 0) {
  621. dev_err(hdmi->dev,
  622. "Failed to get AVI infoframe from mode: %zd\n", err);
  623. return err;
  624. }
  625. err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
  626. if (err < 0) {
  627. dev_err(hdmi->dev, "Failed to pack AVI infoframe: %zd\n", err);
  628. return err;
  629. }
  630. mtk_hdmi_hw_send_info_frame(hdmi, buffer, sizeof(buffer));
  631. return 0;
  632. }
  633. static int mtk_hdmi_setup_spd_infoframe(struct mtk_hdmi *hdmi)
  634. {
  635. struct drm_bridge *bridge = &hdmi->bridge;
  636. struct hdmi_spd_infoframe frame;
  637. u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_SPD_INFOFRAME_SIZE];
  638. ssize_t err;
  639. err = hdmi_spd_infoframe_init(&frame, bridge->vendor, bridge->product);
  640. if (err < 0) {
  641. dev_err(hdmi->dev, "Failed to initialize SPD infoframe: %zd\n",
  642. err);
  643. return err;
  644. }
  645. err = hdmi_spd_infoframe_pack(&frame, buffer, sizeof(buffer));
  646. if (err < 0) {
  647. dev_err(hdmi->dev, "Failed to pack SDP infoframe: %zd\n", err);
  648. return err;
  649. }
  650. mtk_hdmi_hw_send_info_frame(hdmi, buffer, sizeof(buffer));
  651. return 0;
  652. }
  653. static int mtk_hdmi_setup_audio_infoframe(struct mtk_hdmi *hdmi)
  654. {
  655. struct hdmi_audio_infoframe frame;
  656. u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
  657. ssize_t err;
  658. err = hdmi_audio_infoframe_init(&frame);
  659. if (err < 0) {
  660. dev_err(hdmi->dev, "Failed to setup audio infoframe: %zd\n",
  661. err);
  662. return err;
  663. }
  664. frame.coding_type = HDMI_AUDIO_CODING_TYPE_STREAM;
  665. frame.sample_frequency = HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM;
  666. frame.sample_size = HDMI_AUDIO_SAMPLE_SIZE_STREAM;
  667. frame.channels = mtk_hdmi_aud_get_chnl_count(
  668. hdmi->aud_param.aud_input_chan_type);
  669. err = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
  670. if (err < 0) {
  671. dev_err(hdmi->dev, "Failed to pack audio infoframe: %zd\n",
  672. err);
  673. return err;
  674. }
  675. mtk_hdmi_hw_send_info_frame(hdmi, buffer, sizeof(buffer));
  676. return 0;
  677. }
  678. static int mtk_hdmi_setup_vendor_specific_infoframe(struct mtk_hdmi *hdmi,
  679. struct drm_display_mode *mode)
  680. {
  681. struct hdmi_vendor_infoframe frame;
  682. u8 buffer[10];
  683. ssize_t err;
  684. err = drm_hdmi_vendor_infoframe_from_display_mode(&frame,
  685. hdmi->curr_conn, mode);
  686. if (err) {
  687. dev_err(hdmi->dev,
  688. "Failed to get vendor infoframe from mode: %zd\n", err);
  689. return err;
  690. }
  691. err = hdmi_vendor_infoframe_pack(&frame, buffer, sizeof(buffer));
  692. if (err < 0) {
  693. dev_err(hdmi->dev, "Failed to pack vendor infoframe: %zd\n",
  694. err);
  695. return err;
  696. }
  697. mtk_hdmi_hw_send_info_frame(hdmi, buffer, sizeof(buffer));
  698. return 0;
  699. }
  700. static void mtk_hdmi_audio_enable(struct mtk_hdmi *hdmi)
  701. {
  702. mtk_hdmi_hw_send_aud_packet(hdmi, true);
  703. hdmi->audio_enable = true;
  704. }
  705. static void mtk_hdmi_audio_disable(struct mtk_hdmi *hdmi)
  706. {
  707. mtk_hdmi_hw_send_aud_packet(hdmi, false);
  708. hdmi->audio_enable = false;
  709. }
  710. static int mtk_hdmi_output_set_display_mode(struct mtk_hdmi *hdmi,
  711. struct drm_display_mode *mode)
  712. {
  713. int ret;
  714. mtk_hdmi_hw_vid_black(hdmi, true);
  715. mtk_hdmi_hw_aud_mute(hdmi);
  716. mtk_hdmi_hw_send_av_mute(hdmi);
  717. phy_power_off(hdmi->phy);
  718. ret = mtk_hdmi_video_change_vpll(hdmi,
  719. mode->clock * 1000);
  720. if (ret) {
  721. dev_err(hdmi->dev, "Failed to set vpll: %d\n", ret);
  722. return ret;
  723. }
  724. mtk_hdmi_video_set_display_mode(hdmi, mode);
  725. phy_power_on(hdmi->phy);
  726. mtk_hdmi_aud_output_config(hdmi, mode);
  727. mtk_hdmi_hw_vid_black(hdmi, false);
  728. mtk_hdmi_hw_aud_unmute(hdmi);
  729. mtk_hdmi_hw_send_av_unmute(hdmi);
  730. return 0;
  731. }
  732. static const char * const mtk_hdmi_clk_names[MTK_HDMI_CLK_COUNT] = {
  733. [MTK_HDMI_CLK_HDMI_PIXEL] = "pixel",
  734. [MTK_HDMI_CLK_HDMI_PLL] = "pll",
  735. [MTK_HDMI_CLK_AUD_BCLK] = "bclk",
  736. [MTK_HDMI_CLK_AUD_SPDIF] = "spdif",
  737. };
  738. static int mtk_hdmi_clk_enable_audio(struct mtk_hdmi *hdmi)
  739. {
  740. int ret;
  741. ret = clk_prepare_enable(hdmi->clk[MTK_HDMI_CLK_AUD_BCLK]);
  742. if (ret)
  743. return ret;
  744. ret = clk_prepare_enable(hdmi->clk[MTK_HDMI_CLK_AUD_SPDIF]);
  745. if (ret) {
  746. clk_disable_unprepare(hdmi->clk[MTK_HDMI_CLK_AUD_BCLK]);
  747. return ret;
  748. }
  749. return 0;
  750. }
  751. static void mtk_hdmi_clk_disable_audio(struct mtk_hdmi *hdmi)
  752. {
  753. clk_disable_unprepare(hdmi->clk[MTK_HDMI_CLK_AUD_BCLK]);
  754. clk_disable_unprepare(hdmi->clk[MTK_HDMI_CLK_AUD_SPDIF]);
  755. }
  756. static enum drm_connector_status
  757. mtk_hdmi_update_plugged_status(struct mtk_hdmi *hdmi)
  758. {
  759. bool connected;
  760. mutex_lock(&hdmi->update_plugged_status_lock);
  761. connected = mtk_cec_hpd_high(hdmi->cec_dev);
  762. if (hdmi->plugged_cb && hdmi->codec_dev)
  763. hdmi->plugged_cb(hdmi->codec_dev, connected);
  764. mutex_unlock(&hdmi->update_plugged_status_lock);
  765. return connected ?
  766. connector_status_connected : connector_status_disconnected;
  767. }
  768. static enum drm_connector_status mtk_hdmi_detect(struct mtk_hdmi *hdmi)
  769. {
  770. return mtk_hdmi_update_plugged_status(hdmi);
  771. }
  772. static enum drm_mode_status
  773. mtk_hdmi_bridge_mode_valid(struct drm_bridge *bridge,
  774. const struct drm_display_info *info,
  775. const struct drm_display_mode *mode)
  776. {
  777. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  778. dev_dbg(hdmi->dev, "xres=%d, yres=%d, refresh=%d, intl=%d clock=%d\n",
  779. mode->hdisplay, mode->vdisplay, drm_mode_vrefresh(mode),
  780. !!(mode->flags & DRM_MODE_FLAG_INTERLACE), mode->clock * 1000);
  781. if (hdmi->conf) {
  782. if (hdmi->conf->cea_modes_only && !drm_match_cea_mode(mode))
  783. return MODE_BAD;
  784. if (hdmi->conf->max_mode_clock &&
  785. mode->clock > hdmi->conf->max_mode_clock)
  786. return MODE_CLOCK_HIGH;
  787. }
  788. if (mode->clock < 27000)
  789. return MODE_CLOCK_LOW;
  790. if (mode->clock > 297000)
  791. return MODE_CLOCK_HIGH;
  792. return drm_mode_validate_size(mode, 0x1fff, 0x1fff);
  793. }
  794. static void mtk_hdmi_hpd_event(bool hpd, struct device *dev)
  795. {
  796. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  797. if (hdmi && hdmi->bridge.encoder && hdmi->bridge.encoder->dev) {
  798. static enum drm_connector_status status;
  799. status = mtk_hdmi_detect(hdmi);
  800. drm_helper_hpd_irq_event(hdmi->bridge.encoder->dev);
  801. drm_bridge_hpd_notify(&hdmi->bridge, status);
  802. }
  803. }
  804. /*
  805. * Bridge callbacks
  806. */
  807. static enum drm_connector_status
  808. mtk_hdmi_bridge_detect(struct drm_bridge *bridge, struct drm_connector *connector)
  809. {
  810. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  811. return mtk_hdmi_detect(hdmi);
  812. }
  813. static const struct drm_edid *mtk_hdmi_bridge_edid_read(struct drm_bridge *bridge,
  814. struct drm_connector *connector)
  815. {
  816. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  817. const struct drm_edid *drm_edid;
  818. if (!hdmi->ddc_adpt)
  819. return NULL;
  820. drm_edid = drm_edid_read_ddc(connector, hdmi->ddc_adpt);
  821. if (drm_edid) {
  822. /*
  823. * FIXME: This should use !connector->display_info.has_audio (or
  824. * !connector->display_info.is_hdmi) from a path that has read
  825. * the EDID and called drm_edid_connector_update().
  826. */
  827. const struct edid *edid = drm_edid_raw(drm_edid);
  828. hdmi->dvi_mode = !drm_detect_monitor_audio(edid);
  829. }
  830. return drm_edid;
  831. }
  832. static int mtk_hdmi_bridge_attach(struct drm_bridge *bridge,
  833. struct drm_encoder *encoder,
  834. enum drm_bridge_attach_flags flags)
  835. {
  836. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  837. int ret;
  838. if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
  839. DRM_ERROR("%s: The flag DRM_BRIDGE_ATTACH_NO_CONNECTOR must be supplied\n",
  840. __func__);
  841. return -EINVAL;
  842. }
  843. if (hdmi->bridge.next_bridge) {
  844. ret = drm_bridge_attach(encoder, hdmi->bridge.next_bridge,
  845. bridge, flags);
  846. if (ret)
  847. return ret;
  848. }
  849. mtk_cec_set_hpd_event(hdmi->cec_dev, mtk_hdmi_hpd_event, hdmi->dev);
  850. return 0;
  851. }
  852. static void mtk_hdmi_bridge_atomic_disable(struct drm_bridge *bridge,
  853. struct drm_atomic_state *state)
  854. {
  855. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  856. if (!hdmi->enabled)
  857. return;
  858. phy_power_off(hdmi->phy);
  859. clk_disable_unprepare(hdmi->clk[MTK_HDMI_CLK_HDMI_PIXEL]);
  860. clk_disable_unprepare(hdmi->clk[MTK_HDMI_CLK_HDMI_PLL]);
  861. hdmi->curr_conn = NULL;
  862. hdmi->enabled = false;
  863. }
  864. static void mtk_hdmi_bridge_atomic_post_disable(struct drm_bridge *bridge,
  865. struct drm_atomic_state *state)
  866. {
  867. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  868. if (!hdmi->powered)
  869. return;
  870. mtk_hdmi_hw_1p4_version_enable(hdmi, true);
  871. mtk_hdmi_hw_make_reg_writable(hdmi, false);
  872. hdmi->powered = false;
  873. }
  874. static void mtk_hdmi_bridge_atomic_pre_enable(struct drm_bridge *bridge,
  875. struct drm_atomic_state *state)
  876. {
  877. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  878. mtk_hdmi_hw_make_reg_writable(hdmi, true);
  879. mtk_hdmi_hw_1p4_version_enable(hdmi, true);
  880. hdmi->powered = true;
  881. }
  882. static void mtk_hdmi_send_infoframe(struct mtk_hdmi *hdmi,
  883. struct drm_display_mode *mode)
  884. {
  885. mtk_hdmi_setup_audio_infoframe(hdmi);
  886. mtk_hdmi_setup_avi_infoframe(hdmi, mode);
  887. mtk_hdmi_setup_spd_infoframe(hdmi);
  888. if (mode->flags & DRM_MODE_FLAG_3D_MASK)
  889. mtk_hdmi_setup_vendor_specific_infoframe(hdmi, mode);
  890. }
  891. static void mtk_hdmi_bridge_atomic_enable(struct drm_bridge *bridge,
  892. struct drm_atomic_state *state)
  893. {
  894. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  895. /* Retrieve the connector through the atomic state. */
  896. hdmi->curr_conn = drm_atomic_get_new_connector_for_encoder(state,
  897. bridge->encoder);
  898. mtk_hdmi_output_set_display_mode(hdmi, &hdmi->mode);
  899. clk_prepare_enable(hdmi->clk[MTK_HDMI_CLK_HDMI_PLL]);
  900. clk_prepare_enable(hdmi->clk[MTK_HDMI_CLK_HDMI_PIXEL]);
  901. phy_power_on(hdmi->phy);
  902. mtk_hdmi_send_infoframe(hdmi, &hdmi->mode);
  903. hdmi->enabled = true;
  904. }
  905. static const struct drm_bridge_funcs mtk_hdmi_bridge_funcs = {
  906. .mode_valid = mtk_hdmi_bridge_mode_valid,
  907. .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
  908. .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
  909. .atomic_reset = drm_atomic_helper_bridge_reset,
  910. .attach = mtk_hdmi_bridge_attach,
  911. .mode_fixup = mtk_hdmi_bridge_mode_fixup,
  912. .atomic_disable = mtk_hdmi_bridge_atomic_disable,
  913. .atomic_post_disable = mtk_hdmi_bridge_atomic_post_disable,
  914. .mode_set = mtk_hdmi_bridge_mode_set,
  915. .atomic_pre_enable = mtk_hdmi_bridge_atomic_pre_enable,
  916. .atomic_enable = mtk_hdmi_bridge_atomic_enable,
  917. .detect = mtk_hdmi_bridge_detect,
  918. .edid_read = mtk_hdmi_bridge_edid_read,
  919. };
  920. /*
  921. * HDMI audio codec callbacks
  922. */
  923. static int mtk_hdmi_audio_hw_params(struct device *dev, void *data,
  924. struct hdmi_codec_daifmt *daifmt,
  925. struct hdmi_codec_params *params)
  926. {
  927. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  928. if (!hdmi->audio_enable) {
  929. dev_err(hdmi->dev, "hdmi audio is in disable state!\n");
  930. return -EINVAL;
  931. }
  932. mtk_hdmi_audio_params(hdmi, daifmt, params);
  933. mtk_hdmi_aud_output_config(hdmi, &hdmi->mode);
  934. return 0;
  935. }
  936. static int mtk_hdmi_audio_startup(struct device *dev, void *data)
  937. {
  938. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  939. mtk_hdmi_audio_enable(hdmi);
  940. return 0;
  941. }
  942. static void mtk_hdmi_audio_shutdown(struct device *dev, void *data)
  943. {
  944. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  945. mtk_hdmi_audio_disable(hdmi);
  946. }
  947. static int
  948. mtk_hdmi_audio_mute(struct device *dev, void *data,
  949. bool enable, int direction)
  950. {
  951. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  952. if (enable)
  953. mtk_hdmi_hw_aud_mute(hdmi);
  954. else
  955. mtk_hdmi_hw_aud_unmute(hdmi);
  956. return 0;
  957. }
  958. static int mtk_hdmi_audio_hook_plugged_cb(struct device *dev, void *data,
  959. hdmi_codec_plugged_cb fn,
  960. struct device *codec_dev)
  961. {
  962. struct mtk_hdmi *hdmi = data;
  963. mtk_hdmi_audio_set_plugged_cb(hdmi, fn, codec_dev);
  964. mtk_hdmi_update_plugged_status(hdmi);
  965. return 0;
  966. }
  967. static const struct hdmi_codec_ops mtk_hdmi_audio_codec_ops = {
  968. .hw_params = mtk_hdmi_audio_hw_params,
  969. .audio_startup = mtk_hdmi_audio_startup,
  970. .audio_shutdown = mtk_hdmi_audio_shutdown,
  971. .mute_stream = mtk_hdmi_audio_mute,
  972. .get_eld = mtk_hdmi_audio_get_eld,
  973. .hook_plugged_cb = mtk_hdmi_audio_hook_plugged_cb,
  974. };
  975. static int mtk_hdmi_probe(struct platform_device *pdev)
  976. {
  977. struct mtk_hdmi *hdmi;
  978. int ret;
  979. hdmi = mtk_hdmi_common_probe(pdev);
  980. if (IS_ERR(hdmi))
  981. return PTR_ERR(hdmi);
  982. if (!hdmi->cec_dev)
  983. return dev_err_probe(hdmi->dev, -ENODEV, "CEC is required by HDMIv1\n");
  984. ret = mtk_hdmi_clk_enable_audio(hdmi);
  985. if (ret)
  986. return dev_err_probe(hdmi->dev, ret,
  987. "Failed to enable audio clocks\n");
  988. return 0;
  989. }
  990. static void mtk_hdmi_remove(struct platform_device *pdev)
  991. {
  992. struct mtk_hdmi *hdmi = platform_get_drvdata(pdev);
  993. mtk_hdmi_clk_disable_audio(hdmi);
  994. }
  995. static __maybe_unused int mtk_hdmi_suspend(struct device *dev)
  996. {
  997. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  998. mtk_hdmi_clk_disable_audio(hdmi);
  999. return 0;
  1000. }
  1001. static __maybe_unused int mtk_hdmi_resume(struct device *dev)
  1002. {
  1003. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  1004. return mtk_hdmi_clk_enable_audio(hdmi);
  1005. }
  1006. static SIMPLE_DEV_PM_OPS(mtk_hdmi_pm_ops, mtk_hdmi_suspend, mtk_hdmi_resume);
  1007. static const struct mtk_hdmi_ver_conf mtk_hdmi_v1_ver_conf = {
  1008. .bridge_funcs = &mtk_hdmi_bridge_funcs,
  1009. .codec_ops = &mtk_hdmi_audio_codec_ops,
  1010. .mtk_hdmi_clock_names = mtk_hdmi_clk_names,
  1011. .num_clocks = ARRAY_SIZE(mtk_hdmi_clk_names)
  1012. };
  1013. static const struct mtk_hdmi_conf mtk_hdmi_conf_mt2701 = {
  1014. .tz_disabled = true,
  1015. .ver_conf = &mtk_hdmi_v1_ver_conf
  1016. };
  1017. static const struct mtk_hdmi_conf mtk_hdmi_conf_mt8167 = {
  1018. .cea_modes_only = true,
  1019. .max_mode_clock = 148500,
  1020. .ver_conf = &mtk_hdmi_v1_ver_conf
  1021. };
  1022. static const struct mtk_hdmi_conf mtk_hdmi_conf_mt8173 = {
  1023. .ver_conf = &mtk_hdmi_v1_ver_conf
  1024. };
  1025. static const struct of_device_id mtk_hdmi_of_ids[] = {
  1026. { .compatible = "mediatek,mt2701-hdmi", .data = &mtk_hdmi_conf_mt2701 },
  1027. { .compatible = "mediatek,mt8167-hdmi", .data = &mtk_hdmi_conf_mt8167 },
  1028. { .compatible = "mediatek,mt8173-hdmi", .data = &mtk_hdmi_conf_mt8173 },
  1029. { /* sentinel */ }
  1030. };
  1031. MODULE_DEVICE_TABLE(of, mtk_hdmi_of_ids);
  1032. static struct platform_driver mtk_hdmi_driver = {
  1033. .probe = mtk_hdmi_probe,
  1034. .remove = mtk_hdmi_remove,
  1035. .driver = {
  1036. .name = "mediatek-drm-hdmi",
  1037. .of_match_table = mtk_hdmi_of_ids,
  1038. .pm = &mtk_hdmi_pm_ops,
  1039. },
  1040. };
  1041. module_platform_driver(mtk_hdmi_driver);
  1042. MODULE_AUTHOR("Jie Qiu <jie.qiu@mediatek.com>");
  1043. MODULE_DESCRIPTION("MediaTek HDMI Driver");
  1044. MODULE_LICENSE("GPL v2");
  1045. MODULE_IMPORT_NS("DRM_MTK_HDMI_V1");
  1046. MODULE_IMPORT_NS("DRM_MTK_HDMI");