1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014 MediaTek Inc.
4 * Author: Jie Qiu <jie.qiu@mediatek.com>
7 #include <drm/drm_atomic_helper.h>
8 #include <drm/drm_crtc.h>
9 #include <drm/drm_probe_helper.h>
10 #include <drm/drm_edid.h>
11 #include <linux/arm-smccc.h>
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/hdmi.h>
15 #include <linux/i2c.h>
17 #include <linux/kernel.h>
18 #include <linux/mfd/syscon.h>
19 #include <linux/of_platform.h>
21 #include <linux/of_gpio.h>
22 #include <linux/of_graph.h>
23 #include <linux/phy/phy.h>
24 #include <linux/platform_device.h>
25 #include <linux/regmap.h>
26 #include <sound/hdmi-codec.h>
29 #include "mtk_hdmi_regs.h"
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,
41 enum hdmi_aud_input_type {
42 HDMI_AUD_INPUT_I2S = 0,
46 enum hdmi_aud_i2s_fmt {
47 HDMI_I2S_MODE_RJT_24BIT = 0,
48 HDMI_I2S_MODE_RJT_16BIT,
49 HDMI_I2S_MODE_LJT_24BIT,
50 HDMI_I2S_MODE_LJT_16BIT,
51 HDMI_I2S_MODE_I2S_24BIT,
52 HDMI_I2S_MODE_I2S_16BIT
65 enum hdmi_aud_channel_type {
66 HDMI_AUD_CHAN_TYPE_1_0 = 0,
67 HDMI_AUD_CHAN_TYPE_1_1,
68 HDMI_AUD_CHAN_TYPE_2_0,
69 HDMI_AUD_CHAN_TYPE_2_1,
70 HDMI_AUD_CHAN_TYPE_3_0,
71 HDMI_AUD_CHAN_TYPE_3_1,
72 HDMI_AUD_CHAN_TYPE_4_0,
73 HDMI_AUD_CHAN_TYPE_4_1,
74 HDMI_AUD_CHAN_TYPE_5_0,
75 HDMI_AUD_CHAN_TYPE_5_1,
76 HDMI_AUD_CHAN_TYPE_6_0,
77 HDMI_AUD_CHAN_TYPE_6_1,
78 HDMI_AUD_CHAN_TYPE_7_0,
79 HDMI_AUD_CHAN_TYPE_7_1,
80 HDMI_AUD_CHAN_TYPE_3_0_LRS,
81 HDMI_AUD_CHAN_TYPE_3_1_LRS,
82 HDMI_AUD_CHAN_TYPE_4_0_CLRS,
83 HDMI_AUD_CHAN_TYPE_4_1_CLRS,
84 HDMI_AUD_CHAN_TYPE_6_1_CS,
85 HDMI_AUD_CHAN_TYPE_6_1_CH,
86 HDMI_AUD_CHAN_TYPE_6_1_OH,
87 HDMI_AUD_CHAN_TYPE_6_1_CHR,
88 HDMI_AUD_CHAN_TYPE_7_1_LH_RH,
89 HDMI_AUD_CHAN_TYPE_7_1_LSR_RSR,
90 HDMI_AUD_CHAN_TYPE_7_1_LC_RC,
91 HDMI_AUD_CHAN_TYPE_7_1_LW_RW,
92 HDMI_AUD_CHAN_TYPE_7_1_LSD_RSD,
93 HDMI_AUD_CHAN_TYPE_7_1_LSS_RSS,
94 HDMI_AUD_CHAN_TYPE_7_1_LHS_RHS,
95 HDMI_AUD_CHAN_TYPE_7_1_CS_CH,
96 HDMI_AUD_CHAN_TYPE_7_1_CS_OH,
97 HDMI_AUD_CHAN_TYPE_7_1_CS_CHR,
98 HDMI_AUD_CHAN_TYPE_7_1_CH_OH,
99 HDMI_AUD_CHAN_TYPE_7_1_CH_CHR,
100 HDMI_AUD_CHAN_TYPE_7_1_OH_CHR,
101 HDMI_AUD_CHAN_TYPE_7_1_LSS_RSS_LSR_RSR,
102 HDMI_AUD_CHAN_TYPE_6_0_CS,
103 HDMI_AUD_CHAN_TYPE_6_0_CH,
104 HDMI_AUD_CHAN_TYPE_6_0_OH,
105 HDMI_AUD_CHAN_TYPE_6_0_CHR,
106 HDMI_AUD_CHAN_TYPE_7_0_LH_RH,
107 HDMI_AUD_CHAN_TYPE_7_0_LSR_RSR,
108 HDMI_AUD_CHAN_TYPE_7_0_LC_RC,
109 HDMI_AUD_CHAN_TYPE_7_0_LW_RW,
110 HDMI_AUD_CHAN_TYPE_7_0_LSD_RSD,
111 HDMI_AUD_CHAN_TYPE_7_0_LSS_RSS,
112 HDMI_AUD_CHAN_TYPE_7_0_LHS_RHS,
113 HDMI_AUD_CHAN_TYPE_7_0_CS_CH,
114 HDMI_AUD_CHAN_TYPE_7_0_CS_OH,
115 HDMI_AUD_CHAN_TYPE_7_0_CS_CHR,
116 HDMI_AUD_CHAN_TYPE_7_0_CH_OH,
117 HDMI_AUD_CHAN_TYPE_7_0_CH_CHR,
118 HDMI_AUD_CHAN_TYPE_7_0_OH_CHR,
119 HDMI_AUD_CHAN_TYPE_7_0_LSS_RSS_LSR_RSR,
120 HDMI_AUD_CHAN_TYPE_8_0_LH_RH_CS,
121 HDMI_AUD_CHAN_TYPE_UNKNOWN = 0xFF
124 enum hdmi_aud_channel_swap_type {
126 HDMI_AUD_SWAP_LFE_CC,
128 HDMI_AUD_SWAP_RLS_RRS,
129 HDMI_AUD_SWAP_LR_STATUS,
132 struct hdmi_audio_param {
133 enum hdmi_audio_coding_type aud_codec;
134 enum hdmi_audio_sample_size aud_sampe_size;
135 enum hdmi_aud_input_type aud_input_type;
136 enum hdmi_aud_i2s_fmt aud_i2s_fmt;
137 enum hdmi_aud_mclk aud_mclk;
138 enum hdmi_aud_channel_type aud_input_chan_type;
139 struct hdmi_codec_params codec_params;
143 struct drm_bridge bridge;
144 struct drm_bridge *next_bridge;
145 struct drm_connector conn;
148 struct device *cec_dev;
149 struct i2c_adapter *ddc_adpt;
150 struct clk *clk[MTK_HDMI_CLK_COUNT];
151 struct drm_display_mode mode;
159 struct regmap *sys_regmap;
160 unsigned int sys_offset;
162 enum hdmi_colorspace csp;
163 struct hdmi_audio_param aud_param;
169 static inline struct mtk_hdmi *hdmi_ctx_from_bridge(struct drm_bridge *b)
171 return container_of(b, struct mtk_hdmi, bridge);
174 static inline struct mtk_hdmi *hdmi_ctx_from_conn(struct drm_connector *c)
176 return container_of(c, struct mtk_hdmi, conn);
179 static u32 mtk_hdmi_read(struct mtk_hdmi *hdmi, u32 offset)
181 return readl(hdmi->regs + offset);
184 static void mtk_hdmi_write(struct mtk_hdmi *hdmi, u32 offset, u32 val)
186 writel(val, hdmi->regs + offset);
189 static void mtk_hdmi_clear_bits(struct mtk_hdmi *hdmi, u32 offset, u32 bits)
191 void __iomem *reg = hdmi->regs + offset;
199 static void mtk_hdmi_set_bits(struct mtk_hdmi *hdmi, u32 offset, u32 bits)
201 void __iomem *reg = hdmi->regs + offset;
209 static void mtk_hdmi_mask(struct mtk_hdmi *hdmi, u32 offset, u32 val, u32 mask)
211 void __iomem *reg = hdmi->regs + offset;
215 tmp = (tmp & ~mask) | (val & mask);
219 static void mtk_hdmi_hw_vid_black(struct mtk_hdmi *hdmi, bool black)
221 mtk_hdmi_mask(hdmi, VIDEO_CFG_4, black ? GEN_RGB : NORMAL_PATH,
225 static void mtk_hdmi_hw_make_reg_writable(struct mtk_hdmi *hdmi, bool enable)
227 struct arm_smccc_res res;
228 struct mtk_hdmi_phy *hdmi_phy = phy_get_drvdata(hdmi->phy);
231 * MT8173 HDMI hardware has an output control bit to enable/disable HDMI
232 * output. This bit can only be controlled in ARM supervisor mode.
233 * The ARM trusted firmware provides an API for the HDMI driver to set
234 * this control bit to enable HDMI output in supervisor mode.
236 if (hdmi_phy->conf && hdmi_phy->conf->tz_disabled)
237 regmap_update_bits(hdmi->sys_regmap,
238 hdmi->sys_offset + HDMI_SYS_CFG20,
239 0x80008005, enable ? 0x80000005 : 0x8000);
241 arm_smccc_smc(MTK_SIP_SET_AUTHORIZED_SECURE_REG, 0x14000904,
242 0x80000000, 0, 0, 0, 0, 0, &res);
244 regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG20,
245 HDMI_PCLK_FREE_RUN, enable ? HDMI_PCLK_FREE_RUN : 0);
246 regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG1C,
247 HDMI_ON | ANLG_ON, enable ? (HDMI_ON | ANLG_ON) : 0);
250 static void mtk_hdmi_hw_1p4_version_enable(struct mtk_hdmi *hdmi, bool enable)
252 regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG20,
253 HDMI2P0_EN, enable ? 0 : HDMI2P0_EN);
256 static void mtk_hdmi_hw_aud_mute(struct mtk_hdmi *hdmi)
258 mtk_hdmi_set_bits(hdmi, GRL_AUDIO_CFG, AUDIO_ZERO);
261 static void mtk_hdmi_hw_aud_unmute(struct mtk_hdmi *hdmi)
263 mtk_hdmi_clear_bits(hdmi, GRL_AUDIO_CFG, AUDIO_ZERO);
266 static void mtk_hdmi_hw_reset(struct mtk_hdmi *hdmi)
268 regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG1C,
270 regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG1C,
272 mtk_hdmi_clear_bits(hdmi, GRL_CFG3, CFG3_CONTROL_PACKET_DELAY);
273 regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG1C,
277 static void mtk_hdmi_hw_enable_notice(struct mtk_hdmi *hdmi, bool enable_notice)
279 mtk_hdmi_mask(hdmi, GRL_CFG2, enable_notice ? CFG2_NOTICE_EN : 0,
283 static void mtk_hdmi_hw_write_int_mask(struct mtk_hdmi *hdmi, u32 int_mask)
285 mtk_hdmi_write(hdmi, GRL_INT_MASK, int_mask);
288 static void mtk_hdmi_hw_enable_dvi_mode(struct mtk_hdmi *hdmi, bool enable)
290 mtk_hdmi_mask(hdmi, GRL_CFG1, enable ? CFG1_DVI : 0, CFG1_DVI);
293 static void mtk_hdmi_hw_send_info_frame(struct mtk_hdmi *hdmi, u8 *buffer,
296 u32 ctrl_reg = GRL_CTRL;
299 enum hdmi_infoframe_type frame_type;
303 int ctrl_frame_en = 0;
305 frame_type = *buffer;
316 "frame_type:0x%x,frame_ver:0x%x,frame_len:0x%x,checksum:0x%x\n",
317 frame_type, frame_ver, frame_len, checksum);
319 switch (frame_type) {
320 case HDMI_INFOFRAME_TYPE_AVI:
321 ctrl_frame_en = CTRL_AVI_EN;
324 case HDMI_INFOFRAME_TYPE_SPD:
325 ctrl_frame_en = CTRL_SPD_EN;
328 case HDMI_INFOFRAME_TYPE_AUDIO:
329 ctrl_frame_en = CTRL_AUDIO_EN;
332 case HDMI_INFOFRAME_TYPE_VENDOR:
333 ctrl_frame_en = VS_EN;
334 ctrl_reg = GRL_ACP_ISRC_CTRL;
337 mtk_hdmi_clear_bits(hdmi, ctrl_reg, ctrl_frame_en);
338 mtk_hdmi_write(hdmi, GRL_INFOFRM_TYPE, frame_type);
339 mtk_hdmi_write(hdmi, GRL_INFOFRM_VER, frame_ver);
340 mtk_hdmi_write(hdmi, GRL_INFOFRM_LNG, frame_len);
342 mtk_hdmi_write(hdmi, GRL_IFM_PORT, checksum);
343 for (i = 0; i < frame_len; i++)
344 mtk_hdmi_write(hdmi, GRL_IFM_PORT, frame_data[i]);
346 mtk_hdmi_set_bits(hdmi, ctrl_reg, ctrl_frame_en);
349 static void mtk_hdmi_hw_send_aud_packet(struct mtk_hdmi *hdmi, bool enable)
351 mtk_hdmi_mask(hdmi, GRL_SHIFT_R2, enable ? 0 : AUDIO_PACKET_OFF,
355 static void mtk_hdmi_hw_config_sys(struct mtk_hdmi *hdmi)
357 regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG20,
358 HDMI_OUT_FIFO_EN | MHL_MODE_ON, 0);
359 usleep_range(2000, 4000);
360 regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG20,
361 HDMI_OUT_FIFO_EN | MHL_MODE_ON, HDMI_OUT_FIFO_EN);
364 static void mtk_hdmi_hw_set_deep_color_mode(struct mtk_hdmi *hdmi)
366 regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG20,
367 DEEP_COLOR_MODE_MASK | DEEP_COLOR_EN,
371 static void mtk_hdmi_hw_send_av_mute(struct mtk_hdmi *hdmi)
373 mtk_hdmi_clear_bits(hdmi, GRL_CFG4, CTRL_AVMUTE);
374 usleep_range(2000, 4000);
375 mtk_hdmi_set_bits(hdmi, GRL_CFG4, CTRL_AVMUTE);
378 static void mtk_hdmi_hw_send_av_unmute(struct mtk_hdmi *hdmi)
380 mtk_hdmi_mask(hdmi, GRL_CFG4, CFG4_AV_UNMUTE_EN,
381 CFG4_AV_UNMUTE_EN | CFG4_AV_UNMUTE_SET);
382 usleep_range(2000, 4000);
383 mtk_hdmi_mask(hdmi, GRL_CFG4, CFG4_AV_UNMUTE_SET,
384 CFG4_AV_UNMUTE_EN | CFG4_AV_UNMUTE_SET);
387 static void mtk_hdmi_hw_ncts_enable(struct mtk_hdmi *hdmi, bool on)
389 mtk_hdmi_mask(hdmi, GRL_CTS_CTRL, on ? 0 : CTS_CTRL_SOFT,
393 static void mtk_hdmi_hw_ncts_auto_write_enable(struct mtk_hdmi *hdmi,
396 mtk_hdmi_mask(hdmi, GRL_CTS_CTRL, enable ? NCTS_WRI_ANYTIME : 0,
400 static void mtk_hdmi_hw_msic_setting(struct mtk_hdmi *hdmi,
401 struct drm_display_mode *mode)
403 mtk_hdmi_clear_bits(hdmi, GRL_CFG4, CFG4_MHL_MODE);
405 if (mode->flags & DRM_MODE_FLAG_INTERLACE &&
406 mode->clock == 74250 &&
407 mode->vdisplay == 1080)
408 mtk_hdmi_clear_bits(hdmi, GRL_CFG2, CFG2_MHL_DE_SEL);
410 mtk_hdmi_set_bits(hdmi, GRL_CFG2, CFG2_MHL_DE_SEL);
413 static void mtk_hdmi_hw_aud_set_channel_swap(struct mtk_hdmi *hdmi,
414 enum hdmi_aud_channel_swap_type swap)
419 case HDMI_AUD_SWAP_LR:
422 case HDMI_AUD_SWAP_LFE_CC:
423 swap_bit = LFE_CC_SWAP;
425 case HDMI_AUD_SWAP_LSRS:
426 swap_bit = LSRS_SWAP;
428 case HDMI_AUD_SWAP_RLS_RRS:
429 swap_bit = RLS_RRS_SWAP;
431 case HDMI_AUD_SWAP_LR_STATUS:
432 swap_bit = LR_STATUS_SWAP;
435 swap_bit = LFE_CC_SWAP;
438 mtk_hdmi_mask(hdmi, GRL_CH_SWAP, swap_bit, 0xff);
441 static void mtk_hdmi_hw_aud_set_bit_num(struct mtk_hdmi *hdmi,
442 enum hdmi_audio_sample_size bit_num)
447 case HDMI_AUDIO_SAMPLE_SIZE_16:
450 case HDMI_AUDIO_SAMPLE_SIZE_20:
453 case HDMI_AUDIO_SAMPLE_SIZE_24:
454 case HDMI_AUDIO_SAMPLE_SIZE_STREAM:
459 mtk_hdmi_mask(hdmi, GRL_AOUT_CFG, val, AOUT_BNUM_SEL_MASK);
462 static void mtk_hdmi_hw_aud_set_i2s_fmt(struct mtk_hdmi *hdmi,
463 enum hdmi_aud_i2s_fmt i2s_fmt)
467 val = mtk_hdmi_read(hdmi, GRL_CFG0);
468 val &= ~(CFG0_W_LENGTH_MASK | CFG0_I2S_MODE_MASK);
471 case HDMI_I2S_MODE_RJT_24BIT:
472 val |= CFG0_I2S_MODE_RTJ | CFG0_W_LENGTH_24BIT;
474 case HDMI_I2S_MODE_RJT_16BIT:
475 val |= CFG0_I2S_MODE_RTJ | CFG0_W_LENGTH_16BIT;
477 case HDMI_I2S_MODE_LJT_24BIT:
479 val |= CFG0_I2S_MODE_LTJ | CFG0_W_LENGTH_24BIT;
481 case HDMI_I2S_MODE_LJT_16BIT:
482 val |= CFG0_I2S_MODE_LTJ | CFG0_W_LENGTH_16BIT;
484 case HDMI_I2S_MODE_I2S_24BIT:
485 val |= CFG0_I2S_MODE_I2S | CFG0_W_LENGTH_24BIT;
487 case HDMI_I2S_MODE_I2S_16BIT:
488 val |= CFG0_I2S_MODE_I2S | CFG0_W_LENGTH_16BIT;
491 mtk_hdmi_write(hdmi, GRL_CFG0, val);
494 static void mtk_hdmi_hw_audio_config(struct mtk_hdmi *hdmi, bool dst)
496 const u8 mask = HIGH_BIT_RATE | DST_NORMAL_DOUBLE | SACD_DST | DSD_SEL;
499 /* Disable high bitrate, set DST packet normal/double */
500 mtk_hdmi_clear_bits(hdmi, GRL_AOUT_CFG, HIGH_BIT_RATE_PACKET_ALIGN);
503 val = DST_NORMAL_DOUBLE | SACD_DST;
507 mtk_hdmi_mask(hdmi, GRL_AUDIO_CFG, val, mask);
510 static void mtk_hdmi_hw_aud_set_i2s_chan_num(struct mtk_hdmi *hdmi,
511 enum hdmi_aud_channel_type channel_type,
514 unsigned int ch_switch;
517 ch_switch = CH_SWITCH(7, 7) | CH_SWITCH(6, 6) |
518 CH_SWITCH(5, 5) | CH_SWITCH(4, 4) |
519 CH_SWITCH(3, 3) | CH_SWITCH(1, 2) |
520 CH_SWITCH(2, 1) | CH_SWITCH(0, 0);
522 if (channel_count == 2) {
523 i2s_uv = I2S_UV_CH_EN(0);
524 } else if (channel_count == 3 || channel_count == 4) {
525 if (channel_count == 4 &&
526 (channel_type == HDMI_AUD_CHAN_TYPE_3_0_LRS ||
527 channel_type == HDMI_AUD_CHAN_TYPE_4_0))
528 i2s_uv = I2S_UV_CH_EN(2) | I2S_UV_CH_EN(0);
530 i2s_uv = I2S_UV_CH_EN(3) | I2S_UV_CH_EN(2);
531 } else if (channel_count == 6 || channel_count == 5) {
532 if (channel_count == 6 &&
533 channel_type != HDMI_AUD_CHAN_TYPE_5_1 &&
534 channel_type != HDMI_AUD_CHAN_TYPE_4_1_CLRS) {
535 i2s_uv = I2S_UV_CH_EN(3) | I2S_UV_CH_EN(2) |
536 I2S_UV_CH_EN(1) | I2S_UV_CH_EN(0);
538 i2s_uv = I2S_UV_CH_EN(2) | I2S_UV_CH_EN(1) |
541 } else if (channel_count == 8 || channel_count == 7) {
542 i2s_uv = I2S_UV_CH_EN(3) | I2S_UV_CH_EN(2) |
543 I2S_UV_CH_EN(1) | I2S_UV_CH_EN(0);
545 i2s_uv = I2S_UV_CH_EN(0);
548 mtk_hdmi_write(hdmi, GRL_CH_SW0, ch_switch & 0xff);
549 mtk_hdmi_write(hdmi, GRL_CH_SW1, (ch_switch >> 8) & 0xff);
550 mtk_hdmi_write(hdmi, GRL_CH_SW2, (ch_switch >> 16) & 0xff);
551 mtk_hdmi_write(hdmi, GRL_I2S_UV, i2s_uv);
554 static void mtk_hdmi_hw_aud_set_input_type(struct mtk_hdmi *hdmi,
555 enum hdmi_aud_input_type input_type)
559 val = mtk_hdmi_read(hdmi, GRL_CFG1);
560 if (input_type == HDMI_AUD_INPUT_I2S &&
561 (val & CFG1_SPDIF) == CFG1_SPDIF) {
563 } else if (input_type == HDMI_AUD_INPUT_SPDIF &&
564 (val & CFG1_SPDIF) == 0) {
567 mtk_hdmi_write(hdmi, GRL_CFG1, val);
570 static void mtk_hdmi_hw_aud_set_channel_status(struct mtk_hdmi *hdmi,
575 for (i = 0; i < 5; i++) {
576 mtk_hdmi_write(hdmi, GRL_I2S_C_STA0 + i * 4, channel_status[i]);
577 mtk_hdmi_write(hdmi, GRL_L_STATUS_0 + i * 4, channel_status[i]);
578 mtk_hdmi_write(hdmi, GRL_R_STATUS_0 + i * 4, channel_status[i]);
580 for (; i < 24; i++) {
581 mtk_hdmi_write(hdmi, GRL_L_STATUS_0 + i * 4, 0);
582 mtk_hdmi_write(hdmi, GRL_R_STATUS_0 + i * 4, 0);
586 static void mtk_hdmi_hw_aud_src_reenable(struct mtk_hdmi *hdmi)
590 val = mtk_hdmi_read(hdmi, GRL_MIX_CTRL);
591 if (val & MIX_CTRL_SRC_EN) {
592 val &= ~MIX_CTRL_SRC_EN;
593 mtk_hdmi_write(hdmi, GRL_MIX_CTRL, val);
594 usleep_range(255, 512);
595 val |= MIX_CTRL_SRC_EN;
596 mtk_hdmi_write(hdmi, GRL_MIX_CTRL, val);
600 static void mtk_hdmi_hw_aud_src_disable(struct mtk_hdmi *hdmi)
604 val = mtk_hdmi_read(hdmi, GRL_MIX_CTRL);
605 val &= ~MIX_CTRL_SRC_EN;
606 mtk_hdmi_write(hdmi, GRL_MIX_CTRL, val);
607 mtk_hdmi_write(hdmi, GRL_SHIFT_L1, 0x00);
610 static void mtk_hdmi_hw_aud_set_mclk(struct mtk_hdmi *hdmi,
611 enum hdmi_aud_mclk mclk)
615 val = mtk_hdmi_read(hdmi, GRL_CFG5);
616 val &= CFG5_CD_RATIO_MASK;
619 case HDMI_AUD_MCLK_128FS:
622 case HDMI_AUD_MCLK_256FS:
625 case HDMI_AUD_MCLK_384FS:
628 case HDMI_AUD_MCLK_512FS:
631 case HDMI_AUD_MCLK_768FS:
638 mtk_hdmi_write(hdmi, GRL_CFG5, val);
646 /* Recommended N values from HDMI specification, tables 7-1 to 7-3 */
647 static const struct hdmi_acr_n hdmi_rec_n_table[] = {
648 /* Clock, N: 32kHz 44.1kHz 48kHz */
649 { 25175, { 4576, 7007, 6864 } },
650 { 74176, { 11648, 17836, 11648 } },
651 { 148352, { 11648, 8918, 5824 } },
652 { 296703, { 5824, 4459, 5824 } },
653 { 297000, { 3072, 4704, 5120 } },
654 { 0, { 4096, 6272, 6144 } }, /* all other TMDS clocks */
658 * hdmi_recommended_n() - Return N value recommended by HDMI specification
659 * @freq: audio sample rate in Hz
660 * @clock: rounded TMDS clock in kHz
662 static unsigned int hdmi_recommended_n(unsigned int freq, unsigned int clock)
664 const struct hdmi_acr_n *recommended;
667 for (i = 0; i < ARRAY_SIZE(hdmi_rec_n_table) - 1; i++) {
668 if (clock == hdmi_rec_n_table[i].clock)
671 recommended = hdmi_rec_n_table + i;
675 return recommended->n[0];
677 return recommended->n[1];
679 return recommended->n[2];
681 return recommended->n[1] * 2;
683 return recommended->n[2] * 2;
685 return recommended->n[1] * 4;
687 return recommended->n[2] * 4;
689 return (128 * freq) / 1000;
693 static unsigned int hdmi_mode_clock_to_hz(unsigned int clock)
697 return 25174825; /* 25.2/1.001 MHz */
699 return 74175824; /* 74.25/1.001 MHz */
701 return 148351648; /* 148.5/1.001 MHz */
703 return 296703297; /* 297/1.001 MHz */
709 static unsigned int hdmi_expected_cts(unsigned int audio_sample_rate,
710 unsigned int tmds_clock, unsigned int n)
712 return DIV_ROUND_CLOSEST_ULL((u64)hdmi_mode_clock_to_hz(tmds_clock) * n,
713 128 * audio_sample_rate);
716 static void do_hdmi_hw_aud_set_ncts(struct mtk_hdmi *hdmi, unsigned int n,
719 unsigned char val[NCTS_BYTES];
722 mtk_hdmi_write(hdmi, GRL_NCTS, 0);
723 mtk_hdmi_write(hdmi, GRL_NCTS, 0);
724 mtk_hdmi_write(hdmi, GRL_NCTS, 0);
725 memset(val, 0, sizeof(val));
727 val[0] = (cts >> 24) & 0xff;
728 val[1] = (cts >> 16) & 0xff;
729 val[2] = (cts >> 8) & 0xff;
732 val[4] = (n >> 16) & 0xff;
733 val[5] = (n >> 8) & 0xff;
736 for (i = 0; i < NCTS_BYTES; i++)
737 mtk_hdmi_write(hdmi, GRL_NCTS, val[i]);
740 static void mtk_hdmi_hw_aud_set_ncts(struct mtk_hdmi *hdmi,
741 unsigned int sample_rate,
746 n = hdmi_recommended_n(sample_rate, clock);
747 cts = hdmi_expected_cts(sample_rate, clock, n);
749 dev_dbg(hdmi->dev, "%s: sample_rate=%u, clock=%d, cts=%u, n=%u\n",
750 __func__, sample_rate, clock, n, cts);
752 mtk_hdmi_mask(hdmi, DUMMY_304, AUDIO_I2S_NCTS_SEL_64,
754 do_hdmi_hw_aud_set_ncts(hdmi, n, cts);
757 static u8 mtk_hdmi_aud_get_chnl_count(enum hdmi_aud_channel_type channel_type)
759 switch (channel_type) {
760 case HDMI_AUD_CHAN_TYPE_1_0:
761 case HDMI_AUD_CHAN_TYPE_1_1:
762 case HDMI_AUD_CHAN_TYPE_2_0:
764 case HDMI_AUD_CHAN_TYPE_2_1:
765 case HDMI_AUD_CHAN_TYPE_3_0:
767 case HDMI_AUD_CHAN_TYPE_3_1:
768 case HDMI_AUD_CHAN_TYPE_4_0:
769 case HDMI_AUD_CHAN_TYPE_3_0_LRS:
771 case HDMI_AUD_CHAN_TYPE_4_1:
772 case HDMI_AUD_CHAN_TYPE_5_0:
773 case HDMI_AUD_CHAN_TYPE_3_1_LRS:
774 case HDMI_AUD_CHAN_TYPE_4_0_CLRS:
776 case HDMI_AUD_CHAN_TYPE_5_1:
777 case HDMI_AUD_CHAN_TYPE_6_0:
778 case HDMI_AUD_CHAN_TYPE_4_1_CLRS:
779 case HDMI_AUD_CHAN_TYPE_6_0_CS:
780 case HDMI_AUD_CHAN_TYPE_6_0_CH:
781 case HDMI_AUD_CHAN_TYPE_6_0_OH:
782 case HDMI_AUD_CHAN_TYPE_6_0_CHR:
784 case HDMI_AUD_CHAN_TYPE_6_1:
785 case HDMI_AUD_CHAN_TYPE_6_1_CS:
786 case HDMI_AUD_CHAN_TYPE_6_1_CH:
787 case HDMI_AUD_CHAN_TYPE_6_1_OH:
788 case HDMI_AUD_CHAN_TYPE_6_1_CHR:
789 case HDMI_AUD_CHAN_TYPE_7_0:
790 case HDMI_AUD_CHAN_TYPE_7_0_LH_RH:
791 case HDMI_AUD_CHAN_TYPE_7_0_LSR_RSR:
792 case HDMI_AUD_CHAN_TYPE_7_0_LC_RC:
793 case HDMI_AUD_CHAN_TYPE_7_0_LW_RW:
794 case HDMI_AUD_CHAN_TYPE_7_0_LSD_RSD:
795 case HDMI_AUD_CHAN_TYPE_7_0_LSS_RSS:
796 case HDMI_AUD_CHAN_TYPE_7_0_LHS_RHS:
797 case HDMI_AUD_CHAN_TYPE_7_0_CS_CH:
798 case HDMI_AUD_CHAN_TYPE_7_0_CS_OH:
799 case HDMI_AUD_CHAN_TYPE_7_0_CS_CHR:
800 case HDMI_AUD_CHAN_TYPE_7_0_CH_OH:
801 case HDMI_AUD_CHAN_TYPE_7_0_CH_CHR:
802 case HDMI_AUD_CHAN_TYPE_7_0_OH_CHR:
803 case HDMI_AUD_CHAN_TYPE_7_0_LSS_RSS_LSR_RSR:
804 case HDMI_AUD_CHAN_TYPE_8_0_LH_RH_CS:
806 case HDMI_AUD_CHAN_TYPE_7_1:
807 case HDMI_AUD_CHAN_TYPE_7_1_LH_RH:
808 case HDMI_AUD_CHAN_TYPE_7_1_LSR_RSR:
809 case HDMI_AUD_CHAN_TYPE_7_1_LC_RC:
810 case HDMI_AUD_CHAN_TYPE_7_1_LW_RW:
811 case HDMI_AUD_CHAN_TYPE_7_1_LSD_RSD:
812 case HDMI_AUD_CHAN_TYPE_7_1_LSS_RSS:
813 case HDMI_AUD_CHAN_TYPE_7_1_LHS_RHS:
814 case HDMI_AUD_CHAN_TYPE_7_1_CS_CH:
815 case HDMI_AUD_CHAN_TYPE_7_1_CS_OH:
816 case HDMI_AUD_CHAN_TYPE_7_1_CS_CHR:
817 case HDMI_AUD_CHAN_TYPE_7_1_CH_OH:
818 case HDMI_AUD_CHAN_TYPE_7_1_CH_CHR:
819 case HDMI_AUD_CHAN_TYPE_7_1_OH_CHR:
820 case HDMI_AUD_CHAN_TYPE_7_1_LSS_RSS_LSR_RSR:
827 static int mtk_hdmi_video_change_vpll(struct mtk_hdmi *hdmi, u32 clock)
832 /* The DPI driver already should have set TVDPLL to the correct rate */
833 ret = clk_set_rate(hdmi->clk[MTK_HDMI_CLK_HDMI_PLL], clock);
835 dev_err(hdmi->dev, "Failed to set PLL to %u Hz: %d\n", clock,
840 rate = clk_get_rate(hdmi->clk[MTK_HDMI_CLK_HDMI_PLL]);
842 if (DIV_ROUND_CLOSEST(rate, 1000) != DIV_ROUND_CLOSEST(clock, 1000))
843 dev_warn(hdmi->dev, "Want PLL %u Hz, got %lu Hz\n", clock,
846 dev_dbg(hdmi->dev, "Want PLL %u Hz, got %lu Hz\n", clock, rate);
848 mtk_hdmi_hw_config_sys(hdmi);
849 mtk_hdmi_hw_set_deep_color_mode(hdmi);
853 static void mtk_hdmi_video_set_display_mode(struct mtk_hdmi *hdmi,
854 struct drm_display_mode *mode)
856 mtk_hdmi_hw_reset(hdmi);
857 mtk_hdmi_hw_enable_notice(hdmi, true);
858 mtk_hdmi_hw_write_int_mask(hdmi, 0xff);
859 mtk_hdmi_hw_enable_dvi_mode(hdmi, hdmi->dvi_mode);
860 mtk_hdmi_hw_ncts_auto_write_enable(hdmi, true);
862 mtk_hdmi_hw_msic_setting(hdmi, mode);
865 static int mtk_hdmi_aud_enable_packet(struct mtk_hdmi *hdmi, bool enable)
867 mtk_hdmi_hw_send_aud_packet(hdmi, enable);
871 static int mtk_hdmi_aud_on_off_hw_ncts(struct mtk_hdmi *hdmi, bool on)
873 mtk_hdmi_hw_ncts_enable(hdmi, on);
877 static int mtk_hdmi_aud_set_input(struct mtk_hdmi *hdmi)
879 enum hdmi_aud_channel_type chan_type;
883 mtk_hdmi_hw_aud_set_channel_swap(hdmi, HDMI_AUD_SWAP_LFE_CC);
884 mtk_hdmi_set_bits(hdmi, GRL_MIX_CTRL, MIX_CTRL_FLAT);
886 if (hdmi->aud_param.aud_input_type == HDMI_AUD_INPUT_SPDIF &&
887 hdmi->aud_param.aud_codec == HDMI_AUDIO_CODING_TYPE_DST) {
888 mtk_hdmi_hw_aud_set_bit_num(hdmi, HDMI_AUDIO_SAMPLE_SIZE_24);
889 } else if (hdmi->aud_param.aud_i2s_fmt == HDMI_I2S_MODE_LJT_24BIT) {
890 hdmi->aud_param.aud_i2s_fmt = HDMI_I2S_MODE_LJT_16BIT;
893 mtk_hdmi_hw_aud_set_i2s_fmt(hdmi, hdmi->aud_param.aud_i2s_fmt);
894 mtk_hdmi_hw_aud_set_bit_num(hdmi, HDMI_AUDIO_SAMPLE_SIZE_24);
896 dst = ((hdmi->aud_param.aud_input_type == HDMI_AUD_INPUT_SPDIF) &&
897 (hdmi->aud_param.aud_codec == HDMI_AUDIO_CODING_TYPE_DST));
898 mtk_hdmi_hw_audio_config(hdmi, dst);
900 if (hdmi->aud_param.aud_input_type == HDMI_AUD_INPUT_SPDIF)
901 chan_type = HDMI_AUD_CHAN_TYPE_2_0;
903 chan_type = hdmi->aud_param.aud_input_chan_type;
904 chan_count = mtk_hdmi_aud_get_chnl_count(chan_type);
905 mtk_hdmi_hw_aud_set_i2s_chan_num(hdmi, chan_type, chan_count);
906 mtk_hdmi_hw_aud_set_input_type(hdmi, hdmi->aud_param.aud_input_type);
911 static int mtk_hdmi_aud_set_src(struct mtk_hdmi *hdmi,
912 struct drm_display_mode *display_mode)
914 unsigned int sample_rate = hdmi->aud_param.codec_params.sample_rate;
916 mtk_hdmi_aud_on_off_hw_ncts(hdmi, false);
917 mtk_hdmi_hw_aud_src_disable(hdmi);
918 mtk_hdmi_clear_bits(hdmi, GRL_CFG2, CFG2_ACLK_INV);
920 if (hdmi->aud_param.aud_input_type == HDMI_AUD_INPUT_I2S) {
921 switch (sample_rate) {
931 mtk_hdmi_hw_aud_set_mclk(hdmi, hdmi->aud_param.aud_mclk);
933 switch (sample_rate) {
941 mtk_hdmi_hw_aud_set_mclk(hdmi, HDMI_AUD_MCLK_128FS);
944 mtk_hdmi_hw_aud_set_ncts(hdmi, sample_rate, display_mode->clock);
946 mtk_hdmi_hw_aud_src_reenable(hdmi);
950 static int mtk_hdmi_aud_output_config(struct mtk_hdmi *hdmi,
951 struct drm_display_mode *display_mode)
953 mtk_hdmi_hw_aud_mute(hdmi);
954 mtk_hdmi_aud_enable_packet(hdmi, false);
956 mtk_hdmi_aud_set_input(hdmi);
957 mtk_hdmi_aud_set_src(hdmi, display_mode);
958 mtk_hdmi_hw_aud_set_channel_status(hdmi,
959 hdmi->aud_param.codec_params.iec.status);
961 usleep_range(50, 100);
963 mtk_hdmi_aud_on_off_hw_ncts(hdmi, true);
964 mtk_hdmi_aud_enable_packet(hdmi, true);
965 mtk_hdmi_hw_aud_unmute(hdmi);
969 static int mtk_hdmi_setup_avi_infoframe(struct mtk_hdmi *hdmi,
970 struct drm_display_mode *mode)
972 struct hdmi_avi_infoframe frame;
976 err = drm_hdmi_avi_infoframe_from_display_mode(&frame,
980 "Failed to get AVI infoframe from mode: %zd\n", err);
984 err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
986 dev_err(hdmi->dev, "Failed to pack AVI infoframe: %zd\n", err);
990 mtk_hdmi_hw_send_info_frame(hdmi, buffer, sizeof(buffer));
994 static int mtk_hdmi_setup_spd_infoframe(struct mtk_hdmi *hdmi,
998 struct hdmi_spd_infoframe frame;
1002 err = hdmi_spd_infoframe_init(&frame, vendor, product);
1004 dev_err(hdmi->dev, "Failed to initialize SPD infoframe: %zd\n",
1009 err = hdmi_spd_infoframe_pack(&frame, buffer, sizeof(buffer));
1011 dev_err(hdmi->dev, "Failed to pack SDP infoframe: %zd\n", err);
1015 mtk_hdmi_hw_send_info_frame(hdmi, buffer, sizeof(buffer));
1019 static int mtk_hdmi_setup_audio_infoframe(struct mtk_hdmi *hdmi)
1021 struct hdmi_audio_infoframe frame;
1025 err = hdmi_audio_infoframe_init(&frame);
1027 dev_err(hdmi->dev, "Failed to setup audio infoframe: %zd\n",
1032 frame.coding_type = HDMI_AUDIO_CODING_TYPE_STREAM;
1033 frame.sample_frequency = HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM;
1034 frame.sample_size = HDMI_AUDIO_SAMPLE_SIZE_STREAM;
1035 frame.channels = mtk_hdmi_aud_get_chnl_count(
1036 hdmi->aud_param.aud_input_chan_type);
1038 err = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
1040 dev_err(hdmi->dev, "Failed to pack audio infoframe: %zd\n",
1045 mtk_hdmi_hw_send_info_frame(hdmi, buffer, sizeof(buffer));
1049 static int mtk_hdmi_setup_vendor_specific_infoframe(struct mtk_hdmi *hdmi,
1050 struct drm_display_mode *mode)
1052 struct hdmi_vendor_infoframe frame;
1056 err = drm_hdmi_vendor_infoframe_from_display_mode(&frame,
1060 "Failed to get vendor infoframe from mode: %zd\n", err);
1064 err = hdmi_vendor_infoframe_pack(&frame, buffer, sizeof(buffer));
1066 dev_err(hdmi->dev, "Failed to pack vendor infoframe: %zd\n",
1071 mtk_hdmi_hw_send_info_frame(hdmi, buffer, sizeof(buffer));
1075 static int mtk_hdmi_output_init(struct mtk_hdmi *hdmi)
1077 struct hdmi_audio_param *aud_param = &hdmi->aud_param;
1079 hdmi->csp = HDMI_COLORSPACE_RGB;
1080 aud_param->aud_codec = HDMI_AUDIO_CODING_TYPE_PCM;
1081 aud_param->aud_sampe_size = HDMI_AUDIO_SAMPLE_SIZE_16;
1082 aud_param->aud_input_type = HDMI_AUD_INPUT_I2S;
1083 aud_param->aud_i2s_fmt = HDMI_I2S_MODE_I2S_24BIT;
1084 aud_param->aud_mclk = HDMI_AUD_MCLK_128FS;
1085 aud_param->aud_input_chan_type = HDMI_AUD_CHAN_TYPE_2_0;
1090 static void mtk_hdmi_audio_enable(struct mtk_hdmi *hdmi)
1092 mtk_hdmi_aud_enable_packet(hdmi, true);
1093 hdmi->audio_enable = true;
1096 static void mtk_hdmi_audio_disable(struct mtk_hdmi *hdmi)
1098 mtk_hdmi_aud_enable_packet(hdmi, false);
1099 hdmi->audio_enable = false;
1102 static int mtk_hdmi_audio_set_param(struct mtk_hdmi *hdmi,
1103 struct hdmi_audio_param *param)
1105 if (!hdmi->audio_enable) {
1106 dev_err(hdmi->dev, "hdmi audio is in disable state!\n");
1109 dev_dbg(hdmi->dev, "codec:%d, input:%d, channel:%d, fs:%d\n",
1110 param->aud_codec, param->aud_input_type,
1111 param->aud_input_chan_type, param->codec_params.sample_rate);
1112 memcpy(&hdmi->aud_param, param, sizeof(*param));
1113 return mtk_hdmi_aud_output_config(hdmi, &hdmi->mode);
1116 static int mtk_hdmi_output_set_display_mode(struct mtk_hdmi *hdmi,
1117 struct drm_display_mode *mode)
1121 mtk_hdmi_hw_vid_black(hdmi, true);
1122 mtk_hdmi_hw_aud_mute(hdmi);
1123 mtk_hdmi_hw_send_av_mute(hdmi);
1124 phy_power_off(hdmi->phy);
1126 ret = mtk_hdmi_video_change_vpll(hdmi,
1127 mode->clock * 1000);
1129 dev_err(hdmi->dev, "Failed to set vpll: %d\n", ret);
1132 mtk_hdmi_video_set_display_mode(hdmi, mode);
1134 phy_power_on(hdmi->phy);
1135 mtk_hdmi_aud_output_config(hdmi, mode);
1137 mtk_hdmi_hw_vid_black(hdmi, false);
1138 mtk_hdmi_hw_aud_unmute(hdmi);
1139 mtk_hdmi_hw_send_av_unmute(hdmi);
1144 static const char * const mtk_hdmi_clk_names[MTK_HDMI_CLK_COUNT] = {
1145 [MTK_HDMI_CLK_HDMI_PIXEL] = "pixel",
1146 [MTK_HDMI_CLK_HDMI_PLL] = "pll",
1147 [MTK_HDMI_CLK_AUD_BCLK] = "bclk",
1148 [MTK_HDMI_CLK_AUD_SPDIF] = "spdif",
1151 static int mtk_hdmi_get_all_clk(struct mtk_hdmi *hdmi,
1152 struct device_node *np)
1156 for (i = 0; i < ARRAY_SIZE(mtk_hdmi_clk_names); i++) {
1157 hdmi->clk[i] = of_clk_get_by_name(np,
1158 mtk_hdmi_clk_names[i]);
1159 if (IS_ERR(hdmi->clk[i]))
1160 return PTR_ERR(hdmi->clk[i]);
1165 static int mtk_hdmi_clk_enable_audio(struct mtk_hdmi *hdmi)
1169 ret = clk_prepare_enable(hdmi->clk[MTK_HDMI_CLK_AUD_BCLK]);
1173 ret = clk_prepare_enable(hdmi->clk[MTK_HDMI_CLK_AUD_SPDIF]);
1179 clk_disable_unprepare(hdmi->clk[MTK_HDMI_CLK_AUD_BCLK]);
1183 static void mtk_hdmi_clk_disable_audio(struct mtk_hdmi *hdmi)
1185 clk_disable_unprepare(hdmi->clk[MTK_HDMI_CLK_AUD_BCLK]);
1186 clk_disable_unprepare(hdmi->clk[MTK_HDMI_CLK_AUD_SPDIF]);
1189 static enum drm_connector_status hdmi_conn_detect(struct drm_connector *conn,
1192 struct mtk_hdmi *hdmi = hdmi_ctx_from_conn(conn);
1194 return mtk_cec_hpd_high(hdmi->cec_dev) ?
1195 connector_status_connected : connector_status_disconnected;
1198 static void hdmi_conn_destroy(struct drm_connector *conn)
1200 struct mtk_hdmi *hdmi = hdmi_ctx_from_conn(conn);
1202 mtk_cec_set_hpd_event(hdmi->cec_dev, NULL, NULL);
1204 drm_connector_cleanup(conn);
1207 static int mtk_hdmi_conn_get_modes(struct drm_connector *conn)
1209 struct mtk_hdmi *hdmi = hdmi_ctx_from_conn(conn);
1213 if (!hdmi->ddc_adpt)
1216 edid = drm_get_edid(conn, hdmi->ddc_adpt);
1220 hdmi->dvi_mode = !drm_detect_monitor_audio(edid);
1222 drm_connector_update_edid_property(conn, edid);
1224 ret = drm_add_edid_modes(conn, edid);
1229 static int mtk_hdmi_conn_mode_valid(struct drm_connector *conn,
1230 struct drm_display_mode *mode)
1232 struct mtk_hdmi *hdmi = hdmi_ctx_from_conn(conn);
1234 dev_dbg(hdmi->dev, "xres=%d, yres=%d, refresh=%d, intl=%d clock=%d\n",
1235 mode->hdisplay, mode->vdisplay, mode->vrefresh,
1236 !!(mode->flags & DRM_MODE_FLAG_INTERLACE), mode->clock * 1000);
1238 if (hdmi->bridge.next) {
1239 struct drm_display_mode adjusted_mode;
1241 drm_mode_copy(&adjusted_mode, mode);
1242 if (!drm_bridge_mode_fixup(hdmi->bridge.next, mode,
1247 if (mode->clock < 27000)
1248 return MODE_CLOCK_LOW;
1249 if (mode->clock > 297000)
1250 return MODE_CLOCK_HIGH;
1252 return drm_mode_validate_size(mode, 0x1fff, 0x1fff);
1255 static struct drm_encoder *mtk_hdmi_conn_best_enc(struct drm_connector *conn)
1257 struct mtk_hdmi *hdmi = hdmi_ctx_from_conn(conn);
1259 return hdmi->bridge.encoder;
1262 static const struct drm_connector_funcs mtk_hdmi_connector_funcs = {
1263 .detect = hdmi_conn_detect,
1264 .fill_modes = drm_helper_probe_single_connector_modes,
1265 .destroy = hdmi_conn_destroy,
1266 .reset = drm_atomic_helper_connector_reset,
1267 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1268 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1271 static const struct drm_connector_helper_funcs
1272 mtk_hdmi_connector_helper_funcs = {
1273 .get_modes = mtk_hdmi_conn_get_modes,
1274 .mode_valid = mtk_hdmi_conn_mode_valid,
1275 .best_encoder = mtk_hdmi_conn_best_enc,
1278 static void mtk_hdmi_hpd_event(bool hpd, struct device *dev)
1280 struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
1282 if (hdmi && hdmi->bridge.encoder && hdmi->bridge.encoder->dev)
1283 drm_helper_hpd_irq_event(hdmi->bridge.encoder->dev);
1290 static int mtk_hdmi_bridge_attach(struct drm_bridge *bridge)
1292 struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
1295 ret = drm_connector_init(bridge->encoder->dev, &hdmi->conn,
1296 &mtk_hdmi_connector_funcs,
1297 DRM_MODE_CONNECTOR_HDMIA);
1299 dev_err(hdmi->dev, "Failed to initialize connector: %d\n", ret);
1302 drm_connector_helper_add(&hdmi->conn, &mtk_hdmi_connector_helper_funcs);
1304 hdmi->conn.polled = DRM_CONNECTOR_POLL_HPD;
1305 hdmi->conn.interlace_allowed = true;
1306 hdmi->conn.doublescan_allowed = false;
1308 ret = drm_connector_attach_encoder(&hdmi->conn,
1312 "Failed to attach connector to encoder: %d\n", ret);
1316 if (hdmi->next_bridge) {
1317 ret = drm_bridge_attach(bridge->encoder, hdmi->next_bridge,
1321 "Failed to attach external bridge: %d\n", ret);
1326 mtk_cec_set_hpd_event(hdmi->cec_dev, mtk_hdmi_hpd_event, hdmi->dev);
1331 static bool mtk_hdmi_bridge_mode_fixup(struct drm_bridge *bridge,
1332 const struct drm_display_mode *mode,
1333 struct drm_display_mode *adjusted_mode)
1338 static void mtk_hdmi_bridge_disable(struct drm_bridge *bridge)
1340 struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
1345 phy_power_off(hdmi->phy);
1346 clk_disable_unprepare(hdmi->clk[MTK_HDMI_CLK_HDMI_PIXEL]);
1347 clk_disable_unprepare(hdmi->clk[MTK_HDMI_CLK_HDMI_PLL]);
1349 hdmi->enabled = false;
1352 static void mtk_hdmi_bridge_post_disable(struct drm_bridge *bridge)
1354 struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
1359 mtk_hdmi_hw_1p4_version_enable(hdmi, true);
1360 mtk_hdmi_hw_make_reg_writable(hdmi, false);
1362 hdmi->powered = false;
1365 static void mtk_hdmi_bridge_mode_set(struct drm_bridge *bridge,
1366 const struct drm_display_mode *mode,
1367 const struct drm_display_mode *adjusted_mode)
1369 struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
1371 dev_dbg(hdmi->dev, "cur info: name:%s, hdisplay:%d\n",
1372 adjusted_mode->name, adjusted_mode->hdisplay);
1373 dev_dbg(hdmi->dev, "hsync_start:%d,hsync_end:%d, htotal:%d",
1374 adjusted_mode->hsync_start, adjusted_mode->hsync_end,
1375 adjusted_mode->htotal);
1376 dev_dbg(hdmi->dev, "hskew:%d, vdisplay:%d\n",
1377 adjusted_mode->hskew, adjusted_mode->vdisplay);
1378 dev_dbg(hdmi->dev, "vsync_start:%d, vsync_end:%d, vtotal:%d",
1379 adjusted_mode->vsync_start, adjusted_mode->vsync_end,
1380 adjusted_mode->vtotal);
1381 dev_dbg(hdmi->dev, "vscan:%d, flag:%d\n",
1382 adjusted_mode->vscan, adjusted_mode->flags);
1384 drm_mode_copy(&hdmi->mode, adjusted_mode);
1387 static void mtk_hdmi_bridge_pre_enable(struct drm_bridge *bridge)
1389 struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
1391 mtk_hdmi_hw_make_reg_writable(hdmi, true);
1392 mtk_hdmi_hw_1p4_version_enable(hdmi, true);
1394 hdmi->powered = true;
1397 static void mtk_hdmi_send_infoframe(struct mtk_hdmi *hdmi,
1398 struct drm_display_mode *mode)
1400 mtk_hdmi_setup_audio_infoframe(hdmi);
1401 mtk_hdmi_setup_avi_infoframe(hdmi, mode);
1402 mtk_hdmi_setup_spd_infoframe(hdmi, "mediatek", "On-chip HDMI");
1403 if (mode->flags & DRM_MODE_FLAG_3D_MASK)
1404 mtk_hdmi_setup_vendor_specific_infoframe(hdmi, mode);
1407 static void mtk_hdmi_bridge_enable(struct drm_bridge *bridge)
1409 struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
1411 mtk_hdmi_output_set_display_mode(hdmi, &hdmi->mode);
1412 clk_prepare_enable(hdmi->clk[MTK_HDMI_CLK_HDMI_PLL]);
1413 clk_prepare_enable(hdmi->clk[MTK_HDMI_CLK_HDMI_PIXEL]);
1414 phy_power_on(hdmi->phy);
1415 mtk_hdmi_send_infoframe(hdmi, &hdmi->mode);
1417 hdmi->enabled = true;
1420 static const struct drm_bridge_funcs mtk_hdmi_bridge_funcs = {
1421 .attach = mtk_hdmi_bridge_attach,
1422 .mode_fixup = mtk_hdmi_bridge_mode_fixup,
1423 .disable = mtk_hdmi_bridge_disable,
1424 .post_disable = mtk_hdmi_bridge_post_disable,
1425 .mode_set = mtk_hdmi_bridge_mode_set,
1426 .pre_enable = mtk_hdmi_bridge_pre_enable,
1427 .enable = mtk_hdmi_bridge_enable,
1430 static int mtk_hdmi_dt_parse_pdata(struct mtk_hdmi *hdmi,
1431 struct platform_device *pdev)
1433 struct device *dev = &pdev->dev;
1434 struct device_node *np = dev->of_node;
1435 struct device_node *cec_np, *remote, *i2c_np;
1436 struct platform_device *cec_pdev;
1437 struct regmap *regmap;
1438 struct resource *mem;
1441 ret = mtk_hdmi_get_all_clk(hdmi, np);
1443 dev_err(dev, "Failed to get clocks: %d\n", ret);
1447 /* The CEC module handles HDMI hotplug detection */
1448 cec_np = of_get_compatible_child(np->parent, "mediatek,mt8173-cec");
1450 dev_err(dev, "Failed to find CEC node\n");
1454 cec_pdev = of_find_device_by_node(cec_np);
1456 dev_err(hdmi->dev, "Waiting for CEC device %pOF\n",
1458 of_node_put(cec_np);
1459 return -EPROBE_DEFER;
1461 of_node_put(cec_np);
1462 hdmi->cec_dev = &cec_pdev->dev;
1465 * The mediatek,syscon-hdmi property contains a phandle link to the
1466 * MMSYS_CONFIG device and the register offset of the HDMI_SYS_CFG
1467 * registers it contains.
1469 regmap = syscon_regmap_lookup_by_phandle(np, "mediatek,syscon-hdmi");
1470 ret = of_property_read_u32_index(np, "mediatek,syscon-hdmi", 1,
1473 ret = PTR_ERR(regmap);
1476 "Failed to get system configuration registers: %d\n",
1480 hdmi->sys_regmap = regmap;
1482 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1483 hdmi->regs = devm_ioremap_resource(dev, mem);
1484 if (IS_ERR(hdmi->regs))
1485 return PTR_ERR(hdmi->regs);
1487 remote = of_graph_get_remote_node(np, 1, 0);
1491 if (!of_device_is_compatible(remote, "hdmi-connector")) {
1492 hdmi->next_bridge = of_drm_find_bridge(remote);
1493 if (!hdmi->next_bridge) {
1494 dev_err(dev, "Waiting for external bridge\n");
1495 of_node_put(remote);
1496 return -EPROBE_DEFER;
1500 i2c_np = of_parse_phandle(remote, "ddc-i2c-bus", 0);
1502 dev_err(dev, "Failed to find ddc-i2c-bus node in %pOF\n",
1504 of_node_put(remote);
1507 of_node_put(remote);
1509 hdmi->ddc_adpt = of_find_i2c_adapter_by_node(i2c_np);
1510 of_node_put(i2c_np);
1511 if (!hdmi->ddc_adpt) {
1512 dev_err(dev, "Failed to get ddc i2c adapter by node\n");
1520 * HDMI audio codec callbacks
1523 static int mtk_hdmi_audio_hw_params(struct device *dev, void *data,
1524 struct hdmi_codec_daifmt *daifmt,
1525 struct hdmi_codec_params *params)
1527 struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
1528 struct hdmi_audio_param hdmi_params;
1529 unsigned int chan = params->cea.channels;
1531 dev_dbg(hdmi->dev, "%s: %u Hz, %d bit, %d channels\n", __func__,
1532 params->sample_rate, params->sample_width, chan);
1534 if (!hdmi->bridge.encoder)
1539 hdmi_params.aud_input_chan_type = HDMI_AUD_CHAN_TYPE_2_0;
1542 hdmi_params.aud_input_chan_type = HDMI_AUD_CHAN_TYPE_4_0;
1545 hdmi_params.aud_input_chan_type = HDMI_AUD_CHAN_TYPE_5_1;
1548 hdmi_params.aud_input_chan_type = HDMI_AUD_CHAN_TYPE_7_1;
1551 dev_err(hdmi->dev, "channel[%d] not supported!\n", chan);
1555 switch (params->sample_rate) {
1565 dev_err(hdmi->dev, "rate[%d] not supported!\n",
1566 params->sample_rate);
1570 switch (daifmt->fmt) {
1572 hdmi_params.aud_codec = HDMI_AUDIO_CODING_TYPE_PCM;
1573 hdmi_params.aud_sampe_size = HDMI_AUDIO_SAMPLE_SIZE_16;
1574 hdmi_params.aud_input_type = HDMI_AUD_INPUT_I2S;
1575 hdmi_params.aud_i2s_fmt = HDMI_I2S_MODE_I2S_24BIT;
1576 hdmi_params.aud_mclk = HDMI_AUD_MCLK_128FS;
1579 hdmi_params.aud_codec = HDMI_AUDIO_CODING_TYPE_PCM;
1580 hdmi_params.aud_sampe_size = HDMI_AUDIO_SAMPLE_SIZE_16;
1581 hdmi_params.aud_input_type = HDMI_AUD_INPUT_SPDIF;
1584 dev_err(hdmi->dev, "%s: Invalid DAI format %d\n", __func__,
1589 memcpy(&hdmi_params.codec_params, params,
1590 sizeof(hdmi_params.codec_params));
1592 mtk_hdmi_audio_set_param(hdmi, &hdmi_params);
1597 static int mtk_hdmi_audio_startup(struct device *dev, void *data)
1599 struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
1601 dev_dbg(dev, "%s\n", __func__);
1603 mtk_hdmi_audio_enable(hdmi);
1608 static void mtk_hdmi_audio_shutdown(struct device *dev, void *data)
1610 struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
1612 dev_dbg(dev, "%s\n", __func__);
1614 mtk_hdmi_audio_disable(hdmi);
1618 mtk_hdmi_audio_digital_mute(struct device *dev, void *data, bool enable)
1620 struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
1622 dev_dbg(dev, "%s(%d)\n", __func__, enable);
1625 mtk_hdmi_hw_aud_mute(hdmi);
1627 mtk_hdmi_hw_aud_unmute(hdmi);
1632 static int mtk_hdmi_audio_get_eld(struct device *dev, void *data, uint8_t *buf, size_t len)
1634 struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
1636 dev_dbg(dev, "%s\n", __func__);
1638 memcpy(buf, hdmi->conn.eld, min(sizeof(hdmi->conn.eld), len));
1643 static const struct hdmi_codec_ops mtk_hdmi_audio_codec_ops = {
1644 .hw_params = mtk_hdmi_audio_hw_params,
1645 .audio_startup = mtk_hdmi_audio_startup,
1646 .audio_shutdown = mtk_hdmi_audio_shutdown,
1647 .digital_mute = mtk_hdmi_audio_digital_mute,
1648 .get_eld = mtk_hdmi_audio_get_eld,
1651 static void mtk_hdmi_register_audio_driver(struct device *dev)
1653 struct hdmi_codec_pdata codec_data = {
1654 .ops = &mtk_hdmi_audio_codec_ops,
1655 .max_i2s_channels = 2,
1658 struct platform_device *pdev;
1660 pdev = platform_device_register_data(dev, HDMI_CODEC_DRV_NAME,
1661 PLATFORM_DEVID_AUTO, &codec_data,
1662 sizeof(codec_data));
1666 DRM_INFO("%s driver bound to HDMI\n", HDMI_CODEC_DRV_NAME);
1669 static int mtk_drm_hdmi_probe(struct platform_device *pdev)
1671 struct mtk_hdmi *hdmi;
1672 struct device *dev = &pdev->dev;
1675 hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
1681 ret = mtk_hdmi_dt_parse_pdata(hdmi, pdev);
1685 hdmi->phy = devm_phy_get(dev, "hdmi");
1686 if (IS_ERR(hdmi->phy)) {
1687 ret = PTR_ERR(hdmi->phy);
1688 dev_err(dev, "Failed to get HDMI PHY: %d\n", ret);
1692 platform_set_drvdata(pdev, hdmi);
1694 ret = mtk_hdmi_output_init(hdmi);
1696 dev_err(dev, "Failed to initialize hdmi output\n");
1700 mtk_hdmi_register_audio_driver(dev);
1702 hdmi->bridge.funcs = &mtk_hdmi_bridge_funcs;
1703 hdmi->bridge.of_node = pdev->dev.of_node;
1704 drm_bridge_add(&hdmi->bridge);
1706 ret = mtk_hdmi_clk_enable_audio(hdmi);
1708 dev_err(dev, "Failed to enable audio clocks: %d\n", ret);
1709 goto err_bridge_remove;
1712 dev_dbg(dev, "mediatek hdmi probe success\n");
1716 drm_bridge_remove(&hdmi->bridge);
1720 static int mtk_drm_hdmi_remove(struct platform_device *pdev)
1722 struct mtk_hdmi *hdmi = platform_get_drvdata(pdev);
1724 drm_bridge_remove(&hdmi->bridge);
1725 mtk_hdmi_clk_disable_audio(hdmi);
1729 #ifdef CONFIG_PM_SLEEP
1730 static int mtk_hdmi_suspend(struct device *dev)
1732 struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
1734 mtk_hdmi_clk_disable_audio(hdmi);
1735 dev_dbg(dev, "hdmi suspend success!\n");
1739 static int mtk_hdmi_resume(struct device *dev)
1741 struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
1744 ret = mtk_hdmi_clk_enable_audio(hdmi);
1746 dev_err(dev, "hdmi resume failed!\n");
1750 dev_dbg(dev, "hdmi resume success!\n");
1754 static SIMPLE_DEV_PM_OPS(mtk_hdmi_pm_ops,
1755 mtk_hdmi_suspend, mtk_hdmi_resume);
1757 static const struct of_device_id mtk_drm_hdmi_of_ids[] = {
1758 { .compatible = "mediatek,mt8173-hdmi", },
1762 static struct platform_driver mtk_hdmi_driver = {
1763 .probe = mtk_drm_hdmi_probe,
1764 .remove = mtk_drm_hdmi_remove,
1766 .name = "mediatek-drm-hdmi",
1767 .of_match_table = mtk_drm_hdmi_of_ids,
1768 .pm = &mtk_hdmi_pm_ops,
1772 static struct platform_driver * const mtk_hdmi_drivers[] = {
1773 &mtk_hdmi_phy_driver,
1774 &mtk_hdmi_ddc_driver,
1779 static int __init mtk_hdmitx_init(void)
1781 return platform_register_drivers(mtk_hdmi_drivers,
1782 ARRAY_SIZE(mtk_hdmi_drivers));
1785 static void __exit mtk_hdmitx_exit(void)
1787 platform_unregister_drivers(mtk_hdmi_drivers,
1788 ARRAY_SIZE(mtk_hdmi_drivers));
1791 module_init(mtk_hdmitx_init);
1792 module_exit(mtk_hdmitx_exit);
1794 MODULE_AUTHOR("Jie Qiu <jie.qiu@mediatek.com>");
1795 MODULE_DESCRIPTION("MediaTek HDMI Driver");
1796 MODULE_LICENSE("GPL v2");