xref: /linux/drivers/gpu/drm/mediatek/mtk_hdmi.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
28f83f268SJie Qiu /*
38f83f268SJie Qiu  * Copyright (c) 2014 MediaTek Inc.
48f83f268SJie Qiu  * Author: Jie Qiu <jie.qiu@mediatek.com>
58f83f268SJie Qiu  */
69aef5867SSam Ravnborg 
756ba355dSJie Qiu #include <linux/arm-smccc.h>
88f83f268SJie Qiu #include <linux/clk.h>
98f83f268SJie Qiu #include <linux/delay.h>
108f83f268SJie Qiu #include <linux/hdmi.h>
118f83f268SJie Qiu #include <linux/i2c.h>
128f83f268SJie Qiu #include <linux/io.h>
138f83f268SJie Qiu #include <linux/kernel.h>
148f83f268SJie Qiu #include <linux/mfd/syscon.h>
15a481bf2fSCK Hu #include <linux/module.h>
16f07980d4STzung-Bi Shih #include <linux/mutex.h>
178f83f268SJie Qiu #include <linux/of_platform.h>
188f83f268SJie Qiu #include <linux/of.h>
198f83f268SJie Qiu #include <linux/of_graph.h>
208f83f268SJie Qiu #include <linux/phy/phy.h>
218f83f268SJie Qiu #include <linux/platform_device.h>
228f83f268SJie Qiu #include <linux/regmap.h>
239aef5867SSam Ravnborg 
248f83f268SJie Qiu #include <sound/hdmi-codec.h>
259aef5867SSam Ravnborg 
269aef5867SSam Ravnborg #include <drm/drm_atomic_helper.h>
27ee68c743SBoris Brezillon #include <drm/drm_bridge.h>
289aef5867SSam Ravnborg #include <drm/drm_crtc.h>
299aef5867SSam Ravnborg #include <drm/drm_edid.h>
309aef5867SSam Ravnborg #include <drm/drm_print.h>
319aef5867SSam Ravnborg #include <drm/drm_probe_helper.h>
329aef5867SSam Ravnborg 
338f83f268SJie Qiu #include "mtk_cec.h"
348f83f268SJie Qiu #include "mtk_hdmi.h"
358f83f268SJie Qiu #include "mtk_hdmi_regs.h"
368f83f268SJie Qiu 
378f83f268SJie Qiu #define NCTS_BYTES	7
388f83f268SJie Qiu 
398f83f268SJie Qiu enum mtk_hdmi_clk_id {
408f83f268SJie Qiu 	MTK_HDMI_CLK_HDMI_PIXEL,
418f83f268SJie Qiu 	MTK_HDMI_CLK_HDMI_PLL,
428f83f268SJie Qiu 	MTK_HDMI_CLK_AUD_BCLK,
438f83f268SJie Qiu 	MTK_HDMI_CLK_AUD_SPDIF,
448f83f268SJie Qiu 	MTK_HDMI_CLK_COUNT
458f83f268SJie Qiu };
468f83f268SJie Qiu 
478f83f268SJie Qiu enum hdmi_aud_input_type {
488f83f268SJie Qiu 	HDMI_AUD_INPUT_I2S = 0,
498f83f268SJie Qiu 	HDMI_AUD_INPUT_SPDIF,
508f83f268SJie Qiu };
518f83f268SJie Qiu 
528f83f268SJie Qiu enum hdmi_aud_i2s_fmt {
538f83f268SJie Qiu 	HDMI_I2S_MODE_RJT_24BIT = 0,
548f83f268SJie Qiu 	HDMI_I2S_MODE_RJT_16BIT,
558f83f268SJie Qiu 	HDMI_I2S_MODE_LJT_24BIT,
568f83f268SJie Qiu 	HDMI_I2S_MODE_LJT_16BIT,
578f83f268SJie Qiu 	HDMI_I2S_MODE_I2S_24BIT,
588f83f268SJie Qiu 	HDMI_I2S_MODE_I2S_16BIT
598f83f268SJie Qiu };
608f83f268SJie Qiu 
618f83f268SJie Qiu enum hdmi_aud_mclk {
628f83f268SJie Qiu 	HDMI_AUD_MCLK_128FS,
638f83f268SJie Qiu 	HDMI_AUD_MCLK_192FS,
648f83f268SJie Qiu 	HDMI_AUD_MCLK_256FS,
658f83f268SJie Qiu 	HDMI_AUD_MCLK_384FS,
668f83f268SJie Qiu 	HDMI_AUD_MCLK_512FS,
678f83f268SJie Qiu 	HDMI_AUD_MCLK_768FS,
688f83f268SJie Qiu 	HDMI_AUD_MCLK_1152FS,
698f83f268SJie Qiu };
708f83f268SJie Qiu 
718f83f268SJie Qiu enum hdmi_aud_channel_type {
728f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_1_0 = 0,
738f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_1_1,
748f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_2_0,
758f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_2_1,
768f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_3_0,
778f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_3_1,
788f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_4_0,
798f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_4_1,
808f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_5_0,
818f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_5_1,
828f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_6_0,
838f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_6_1,
848f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_7_0,
858f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_7_1,
868f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_3_0_LRS,
878f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_3_1_LRS,
888f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_4_0_CLRS,
898f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_4_1_CLRS,
908f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_6_1_CS,
918f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_6_1_CH,
928f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_6_1_OH,
938f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_6_1_CHR,
948f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_7_1_LH_RH,
958f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_7_1_LSR_RSR,
968f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_7_1_LC_RC,
978f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_7_1_LW_RW,
988f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_7_1_LSD_RSD,
998f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_7_1_LSS_RSS,
1008f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_7_1_LHS_RHS,
1018f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_7_1_CS_CH,
1028f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_7_1_CS_OH,
1038f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_7_1_CS_CHR,
1048f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_7_1_CH_OH,
1058f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_7_1_CH_CHR,
1068f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_7_1_OH_CHR,
1078f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_7_1_LSS_RSS_LSR_RSR,
1088f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_6_0_CS,
1098f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_6_0_CH,
1108f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_6_0_OH,
1118f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_6_0_CHR,
1128f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_7_0_LH_RH,
1138f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_7_0_LSR_RSR,
1148f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_7_0_LC_RC,
1158f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_7_0_LW_RW,
1168f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_7_0_LSD_RSD,
1178f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_7_0_LSS_RSS,
1188f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_7_0_LHS_RHS,
1198f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_7_0_CS_CH,
1208f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_7_0_CS_OH,
1218f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_7_0_CS_CHR,
1228f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_7_0_CH_OH,
1238f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_7_0_CH_CHR,
1248f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_7_0_OH_CHR,
1258f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_7_0_LSS_RSS_LSR_RSR,
1268f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_8_0_LH_RH_CS,
1278f83f268SJie Qiu 	HDMI_AUD_CHAN_TYPE_UNKNOWN = 0xFF
1288f83f268SJie Qiu };
1298f83f268SJie Qiu 
1308f83f268SJie Qiu enum hdmi_aud_channel_swap_type {
1318f83f268SJie Qiu 	HDMI_AUD_SWAP_LR,
1328f83f268SJie Qiu 	HDMI_AUD_SWAP_LFE_CC,
1338f83f268SJie Qiu 	HDMI_AUD_SWAP_LSRS,
1348f83f268SJie Qiu 	HDMI_AUD_SWAP_RLS_RRS,
1358f83f268SJie Qiu 	HDMI_AUD_SWAP_LR_STATUS,
1368f83f268SJie Qiu };
1378f83f268SJie Qiu 
1388f83f268SJie Qiu struct hdmi_audio_param {
1398f83f268SJie Qiu 	enum hdmi_audio_coding_type aud_codec;
1408f83f268SJie Qiu 	enum hdmi_audio_sample_size aud_sampe_size;
1418f83f268SJie Qiu 	enum hdmi_aud_input_type aud_input_type;
1428f83f268SJie Qiu 	enum hdmi_aud_i2s_fmt aud_i2s_fmt;
1438f83f268SJie Qiu 	enum hdmi_aud_mclk aud_mclk;
1448f83f268SJie Qiu 	enum hdmi_aud_channel_type aud_input_chan_type;
1458f83f268SJie Qiu 	struct hdmi_codec_params codec_params;
1468f83f268SJie Qiu };
1478f83f268SJie Qiu 
14896f51a4dSCK Hu struct mtk_hdmi_conf {
14996f51a4dSCK Hu 	bool tz_disabled;
15041ca9caaSNeil Armstrong 	bool cea_modes_only;
151c91026a9SNeil Armstrong 	unsigned long max_mode_clock;
15296f51a4dSCK Hu };
15396f51a4dSCK Hu 
1548f83f268SJie Qiu struct mtk_hdmi {
1558f83f268SJie Qiu 	struct drm_bridge bridge;
1563bb80f24SLaurent Pinchart 	struct drm_bridge *next_bridge;
1572e477391SDafna Hirschfeld 	struct drm_connector *curr_conn;/* current connector (only valid when 'enabled') */
1588f83f268SJie Qiu 	struct device *dev;
15996f51a4dSCK Hu 	const struct mtk_hdmi_conf *conf;
1608f83f268SJie Qiu 	struct phy *phy;
1618f83f268SJie Qiu 	struct device *cec_dev;
1628f83f268SJie Qiu 	struct i2c_adapter *ddc_adpt;
1638f83f268SJie Qiu 	struct clk *clk[MTK_HDMI_CLK_COUNT];
1648f83f268SJie Qiu 	struct drm_display_mode mode;
1658f83f268SJie Qiu 	bool dvi_mode;
1668f83f268SJie Qiu 	u32 min_clock;
1678f83f268SJie Qiu 	u32 max_clock;
1688f83f268SJie Qiu 	u32 max_hdisplay;
1698f83f268SJie Qiu 	u32 max_vdisplay;
1708f83f268SJie Qiu 	u32 ibias;
1718f83f268SJie Qiu 	u32 ibias_up;
1728f83f268SJie Qiu 	struct regmap *sys_regmap;
1738f83f268SJie Qiu 	unsigned int sys_offset;
1748f83f268SJie Qiu 	void __iomem *regs;
1758f83f268SJie Qiu 	enum hdmi_colorspace csp;
1768f83f268SJie Qiu 	struct hdmi_audio_param aud_param;
1778f83f268SJie Qiu 	bool audio_enable;
1788f83f268SJie Qiu 	bool powered;
1798f83f268SJie Qiu 	bool enabled;
1805d3c6447STzung-Bi Shih 	hdmi_codec_plugged_cb plugged_cb;
1815d3c6447STzung-Bi Shih 	struct device *codec_dev;
182f07980d4STzung-Bi Shih 	struct mutex update_plugged_status_lock;
1838f83f268SJie Qiu };
1848f83f268SJie Qiu 
hdmi_ctx_from_bridge(struct drm_bridge * b)1858f83f268SJie Qiu static inline struct mtk_hdmi *hdmi_ctx_from_bridge(struct drm_bridge *b)
1868f83f268SJie Qiu {
1878f83f268SJie Qiu 	return container_of(b, struct mtk_hdmi, bridge);
1888f83f268SJie Qiu }
1898f83f268SJie Qiu 
mtk_hdmi_read(struct mtk_hdmi * hdmi,u32 offset)1908f83f268SJie Qiu static u32 mtk_hdmi_read(struct mtk_hdmi *hdmi, u32 offset)
1918f83f268SJie Qiu {
1928f83f268SJie Qiu 	return readl(hdmi->regs + offset);
1938f83f268SJie Qiu }
1948f83f268SJie Qiu 
mtk_hdmi_write(struct mtk_hdmi * hdmi,u32 offset,u32 val)1958f83f268SJie Qiu static void mtk_hdmi_write(struct mtk_hdmi *hdmi, u32 offset, u32 val)
1968f83f268SJie Qiu {
1978f83f268SJie Qiu 	writel(val, hdmi->regs + offset);
1988f83f268SJie Qiu }
1998f83f268SJie Qiu 
mtk_hdmi_clear_bits(struct mtk_hdmi * hdmi,u32 offset,u32 bits)2008f83f268SJie Qiu static void mtk_hdmi_clear_bits(struct mtk_hdmi *hdmi, u32 offset, u32 bits)
2018f83f268SJie Qiu {
2028f83f268SJie Qiu 	void __iomem *reg = hdmi->regs + offset;
2038f83f268SJie Qiu 	u32 tmp;
2048f83f268SJie Qiu 
2058f83f268SJie Qiu 	tmp = readl(reg);
2068f83f268SJie Qiu 	tmp &= ~bits;
2078f83f268SJie Qiu 	writel(tmp, reg);
2088f83f268SJie Qiu }
2098f83f268SJie Qiu 
mtk_hdmi_set_bits(struct mtk_hdmi * hdmi,u32 offset,u32 bits)2108f83f268SJie Qiu static void mtk_hdmi_set_bits(struct mtk_hdmi *hdmi, u32 offset, u32 bits)
2118f83f268SJie Qiu {
2128f83f268SJie Qiu 	void __iomem *reg = hdmi->regs + offset;
2138f83f268SJie Qiu 	u32 tmp;
2148f83f268SJie Qiu 
2158f83f268SJie Qiu 	tmp = readl(reg);
2168f83f268SJie Qiu 	tmp |= bits;
2178f83f268SJie Qiu 	writel(tmp, reg);
2188f83f268SJie Qiu }
2198f83f268SJie Qiu 
mtk_hdmi_mask(struct mtk_hdmi * hdmi,u32 offset,u32 val,u32 mask)2208f83f268SJie Qiu static void mtk_hdmi_mask(struct mtk_hdmi *hdmi, u32 offset, u32 val, u32 mask)
2218f83f268SJie Qiu {
2228f83f268SJie Qiu 	void __iomem *reg = hdmi->regs + offset;
2238f83f268SJie Qiu 	u32 tmp;
2248f83f268SJie Qiu 
2258f83f268SJie Qiu 	tmp = readl(reg);
2268f83f268SJie Qiu 	tmp = (tmp & ~mask) | (val & mask);
2278f83f268SJie Qiu 	writel(tmp, reg);
2288f83f268SJie Qiu }
2298f83f268SJie Qiu 
mtk_hdmi_hw_vid_black(struct mtk_hdmi * hdmi,bool black)2308f83f268SJie Qiu static void mtk_hdmi_hw_vid_black(struct mtk_hdmi *hdmi, bool black)
2318f83f268SJie Qiu {
2328f83f268SJie Qiu 	mtk_hdmi_mask(hdmi, VIDEO_CFG_4, black ? GEN_RGB : NORMAL_PATH,
2338f83f268SJie Qiu 		      VIDEO_SOURCE_SEL);
2348f83f268SJie Qiu }
2358f83f268SJie Qiu 
mtk_hdmi_hw_make_reg_writable(struct mtk_hdmi * hdmi,bool enable)2368f83f268SJie Qiu static void mtk_hdmi_hw_make_reg_writable(struct mtk_hdmi *hdmi, bool enable)
2378f83f268SJie Qiu {
23856ba355dSJie Qiu 	struct arm_smccc_res res;
23956ba355dSJie Qiu 
24056ba355dSJie Qiu 	/*
24156ba355dSJie Qiu 	 * MT8173 HDMI hardware has an output control bit to enable/disable HDMI
24256ba355dSJie Qiu 	 * output. This bit can only be controlled in ARM supervisor mode.
24356ba355dSJie Qiu 	 * The ARM trusted firmware provides an API for the HDMI driver to set
24456ba355dSJie Qiu 	 * this control bit to enable HDMI output in supervisor mode.
24556ba355dSJie Qiu 	 */
24696f51a4dSCK Hu 	if (hdmi->conf && hdmi->conf->tz_disabled)
2470fc721b2Schunhui dai 		regmap_update_bits(hdmi->sys_regmap,
2480fc721b2Schunhui dai 				   hdmi->sys_offset + HDMI_SYS_CFG20,
2490fc721b2Schunhui dai 				   0x80008005, enable ? 0x80000005 : 0x8000);
2500fc721b2Schunhui dai 	else
2510fc721b2Schunhui dai 		arm_smccc_smc(MTK_SIP_SET_AUTHORIZED_SECURE_REG, 0x14000904,
2520fc721b2Schunhui dai 			      0x80000000, 0, 0, 0, 0, 0, &res);
25356ba355dSJie Qiu 
2548f83f268SJie Qiu 	regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG20,
2558f83f268SJie Qiu 			   HDMI_PCLK_FREE_RUN, enable ? HDMI_PCLK_FREE_RUN : 0);
2568f83f268SJie Qiu 	regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG1C,
2578f83f268SJie Qiu 			   HDMI_ON | ANLG_ON, enable ? (HDMI_ON | ANLG_ON) : 0);
2588f83f268SJie Qiu }
2598f83f268SJie Qiu 
mtk_hdmi_hw_1p4_version_enable(struct mtk_hdmi * hdmi,bool enable)2608f83f268SJie Qiu static void mtk_hdmi_hw_1p4_version_enable(struct mtk_hdmi *hdmi, bool enable)
2618f83f268SJie Qiu {
2628f83f268SJie Qiu 	regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG20,
2638f83f268SJie Qiu 			   HDMI2P0_EN, enable ? 0 : HDMI2P0_EN);
2648f83f268SJie Qiu }
2658f83f268SJie Qiu 
mtk_hdmi_hw_aud_mute(struct mtk_hdmi * hdmi)2668f83f268SJie Qiu static void mtk_hdmi_hw_aud_mute(struct mtk_hdmi *hdmi)
2678f83f268SJie Qiu {
2688f83f268SJie Qiu 	mtk_hdmi_set_bits(hdmi, GRL_AUDIO_CFG, AUDIO_ZERO);
2698f83f268SJie Qiu }
2708f83f268SJie Qiu 
mtk_hdmi_hw_aud_unmute(struct mtk_hdmi * hdmi)2718f83f268SJie Qiu static void mtk_hdmi_hw_aud_unmute(struct mtk_hdmi *hdmi)
2728f83f268SJie Qiu {
2738f83f268SJie Qiu 	mtk_hdmi_clear_bits(hdmi, GRL_AUDIO_CFG, AUDIO_ZERO);
2748f83f268SJie Qiu }
2758f83f268SJie Qiu 
mtk_hdmi_hw_reset(struct mtk_hdmi * hdmi)2768f83f268SJie Qiu static void mtk_hdmi_hw_reset(struct mtk_hdmi *hdmi)
2778f83f268SJie Qiu {
2788f83f268SJie Qiu 	regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG1C,
2798f83f268SJie Qiu 			   HDMI_RST, HDMI_RST);
2808f83f268SJie Qiu 	regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG1C,
2818f83f268SJie Qiu 			   HDMI_RST, 0);
2828f83f268SJie Qiu 	mtk_hdmi_clear_bits(hdmi, GRL_CFG3, CFG3_CONTROL_PACKET_DELAY);
2838f83f268SJie Qiu 	regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG1C,
2848f83f268SJie Qiu 			   ANLG_ON, ANLG_ON);
2858f83f268SJie Qiu }
2868f83f268SJie Qiu 
mtk_hdmi_hw_enable_notice(struct mtk_hdmi * hdmi,bool enable_notice)2878f83f268SJie Qiu static void mtk_hdmi_hw_enable_notice(struct mtk_hdmi *hdmi, bool enable_notice)
2888f83f268SJie Qiu {
2898f83f268SJie Qiu 	mtk_hdmi_mask(hdmi, GRL_CFG2, enable_notice ? CFG2_NOTICE_EN : 0,
2908f83f268SJie Qiu 		      CFG2_NOTICE_EN);
2918f83f268SJie Qiu }
2928f83f268SJie Qiu 
mtk_hdmi_hw_write_int_mask(struct mtk_hdmi * hdmi,u32 int_mask)2938f83f268SJie Qiu static void mtk_hdmi_hw_write_int_mask(struct mtk_hdmi *hdmi, u32 int_mask)
2948f83f268SJie Qiu {
2958f83f268SJie Qiu 	mtk_hdmi_write(hdmi, GRL_INT_MASK, int_mask);
2968f83f268SJie Qiu }
2978f83f268SJie Qiu 
mtk_hdmi_hw_enable_dvi_mode(struct mtk_hdmi * hdmi,bool enable)2988f83f268SJie Qiu static void mtk_hdmi_hw_enable_dvi_mode(struct mtk_hdmi *hdmi, bool enable)
2998f83f268SJie Qiu {
3008f83f268SJie Qiu 	mtk_hdmi_mask(hdmi, GRL_CFG1, enable ? CFG1_DVI : 0, CFG1_DVI);
3018f83f268SJie Qiu }
3028f83f268SJie Qiu 
mtk_hdmi_hw_send_info_frame(struct mtk_hdmi * hdmi,u8 * buffer,u8 len)3038f83f268SJie Qiu static void mtk_hdmi_hw_send_info_frame(struct mtk_hdmi *hdmi, u8 *buffer,
3048f83f268SJie Qiu 					u8 len)
3058f83f268SJie Qiu {
3068f83f268SJie Qiu 	u32 ctrl_reg = GRL_CTRL;
3078f83f268SJie Qiu 	int i;
3088f83f268SJie Qiu 	u8 *frame_data;
3098f83f268SJie Qiu 	enum hdmi_infoframe_type frame_type;
3108f83f268SJie Qiu 	u8 frame_ver;
3118f83f268SJie Qiu 	u8 frame_len;
3128f83f268SJie Qiu 	u8 checksum;
3138f83f268SJie Qiu 	int ctrl_frame_en = 0;
3148f83f268SJie Qiu 
3153cda3d31SBernard Zhao 	frame_type = *buffer++;
3163cda3d31SBernard Zhao 	frame_ver = *buffer++;
3173cda3d31SBernard Zhao 	frame_len = *buffer++;
3183cda3d31SBernard Zhao 	checksum = *buffer++;
3198f83f268SJie Qiu 	frame_data = buffer;
3208f83f268SJie Qiu 
3218f83f268SJie Qiu 	dev_dbg(hdmi->dev,
3228f83f268SJie Qiu 		"frame_type:0x%x,frame_ver:0x%x,frame_len:0x%x,checksum:0x%x\n",
3238f83f268SJie Qiu 		frame_type, frame_ver, frame_len, checksum);
3248f83f268SJie Qiu 
3258f83f268SJie Qiu 	switch (frame_type) {
3268f83f268SJie Qiu 	case HDMI_INFOFRAME_TYPE_AVI:
3278f83f268SJie Qiu 		ctrl_frame_en = CTRL_AVI_EN;
3288f83f268SJie Qiu 		ctrl_reg = GRL_CTRL;
3298f83f268SJie Qiu 		break;
3308f83f268SJie Qiu 	case HDMI_INFOFRAME_TYPE_SPD:
3318f83f268SJie Qiu 		ctrl_frame_en = CTRL_SPD_EN;
3328f83f268SJie Qiu 		ctrl_reg = GRL_CTRL;
3338f83f268SJie Qiu 		break;
3348f83f268SJie Qiu 	case HDMI_INFOFRAME_TYPE_AUDIO:
3358f83f268SJie Qiu 		ctrl_frame_en = CTRL_AUDIO_EN;
3368f83f268SJie Qiu 		ctrl_reg = GRL_CTRL;
3378f83f268SJie Qiu 		break;
3388f83f268SJie Qiu 	case HDMI_INFOFRAME_TYPE_VENDOR:
3398f83f268SJie Qiu 		ctrl_frame_en = VS_EN;
3408f83f268SJie Qiu 		ctrl_reg = GRL_ACP_ISRC_CTRL;
3418f83f268SJie Qiu 		break;
3421f1635b1SSean Paul 	default:
3431f1635b1SSean Paul 		dev_err(hdmi->dev, "Unknown infoframe type %d\n", frame_type);
3441f1635b1SSean Paul 		return;
3458f83f268SJie Qiu 	}
3468f83f268SJie Qiu 	mtk_hdmi_clear_bits(hdmi, ctrl_reg, ctrl_frame_en);
3478f83f268SJie Qiu 	mtk_hdmi_write(hdmi, GRL_INFOFRM_TYPE, frame_type);
3488f83f268SJie Qiu 	mtk_hdmi_write(hdmi, GRL_INFOFRM_VER, frame_ver);
3498f83f268SJie Qiu 	mtk_hdmi_write(hdmi, GRL_INFOFRM_LNG, frame_len);
3508f83f268SJie Qiu 
3518f83f268SJie Qiu 	mtk_hdmi_write(hdmi, GRL_IFM_PORT, checksum);
3528f83f268SJie Qiu 	for (i = 0; i < frame_len; i++)
3538f83f268SJie Qiu 		mtk_hdmi_write(hdmi, GRL_IFM_PORT, frame_data[i]);
3548f83f268SJie Qiu 
3558f83f268SJie Qiu 	mtk_hdmi_set_bits(hdmi, ctrl_reg, ctrl_frame_en);
3568f83f268SJie Qiu }
3578f83f268SJie Qiu 
mtk_hdmi_hw_send_aud_packet(struct mtk_hdmi * hdmi,bool enable)3588f83f268SJie Qiu static void mtk_hdmi_hw_send_aud_packet(struct mtk_hdmi *hdmi, bool enable)
3598f83f268SJie Qiu {
3608f83f268SJie Qiu 	mtk_hdmi_mask(hdmi, GRL_SHIFT_R2, enable ? 0 : AUDIO_PACKET_OFF,
3618f83f268SJie Qiu 		      AUDIO_PACKET_OFF);
3628f83f268SJie Qiu }
3638f83f268SJie Qiu 
mtk_hdmi_hw_config_sys(struct mtk_hdmi * hdmi)3648f83f268SJie Qiu static void mtk_hdmi_hw_config_sys(struct mtk_hdmi *hdmi)
3658f83f268SJie Qiu {
3668f83f268SJie Qiu 	regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG20,
3678f83f268SJie Qiu 			   HDMI_OUT_FIFO_EN | MHL_MODE_ON, 0);
3688f83f268SJie Qiu 	usleep_range(2000, 4000);
3698f83f268SJie Qiu 	regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG20,
3708f83f268SJie Qiu 			   HDMI_OUT_FIFO_EN | MHL_MODE_ON, HDMI_OUT_FIFO_EN);
3718f83f268SJie Qiu }
3728f83f268SJie Qiu 
mtk_hdmi_hw_set_deep_color_mode(struct mtk_hdmi * hdmi)3738f83f268SJie Qiu static void mtk_hdmi_hw_set_deep_color_mode(struct mtk_hdmi *hdmi)
3748f83f268SJie Qiu {
3758f83f268SJie Qiu 	regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG20,
3768f83f268SJie Qiu 			   DEEP_COLOR_MODE_MASK | DEEP_COLOR_EN,
3778f83f268SJie Qiu 			   COLOR_8BIT_MODE);
3788f83f268SJie Qiu }
3798f83f268SJie Qiu 
mtk_hdmi_hw_send_av_mute(struct mtk_hdmi * hdmi)3808f83f268SJie Qiu static void mtk_hdmi_hw_send_av_mute(struct mtk_hdmi *hdmi)
3818f83f268SJie Qiu {
3828f83f268SJie Qiu 	mtk_hdmi_clear_bits(hdmi, GRL_CFG4, CTRL_AVMUTE);
3838f83f268SJie Qiu 	usleep_range(2000, 4000);
3848f83f268SJie Qiu 	mtk_hdmi_set_bits(hdmi, GRL_CFG4, CTRL_AVMUTE);
3858f83f268SJie Qiu }
3868f83f268SJie Qiu 
mtk_hdmi_hw_send_av_unmute(struct mtk_hdmi * hdmi)3878f83f268SJie Qiu static void mtk_hdmi_hw_send_av_unmute(struct mtk_hdmi *hdmi)
3888f83f268SJie Qiu {
3898f83f268SJie Qiu 	mtk_hdmi_mask(hdmi, GRL_CFG4, CFG4_AV_UNMUTE_EN,
3908f83f268SJie Qiu 		      CFG4_AV_UNMUTE_EN | CFG4_AV_UNMUTE_SET);
3918f83f268SJie Qiu 	usleep_range(2000, 4000);
3928f83f268SJie Qiu 	mtk_hdmi_mask(hdmi, GRL_CFG4, CFG4_AV_UNMUTE_SET,
3938f83f268SJie Qiu 		      CFG4_AV_UNMUTE_EN | CFG4_AV_UNMUTE_SET);
3948f83f268SJie Qiu }
3958f83f268SJie Qiu 
mtk_hdmi_hw_ncts_enable(struct mtk_hdmi * hdmi,bool on)3968f83f268SJie Qiu static void mtk_hdmi_hw_ncts_enable(struct mtk_hdmi *hdmi, bool on)
3978f83f268SJie Qiu {
3988f83f268SJie Qiu 	mtk_hdmi_mask(hdmi, GRL_CTS_CTRL, on ? 0 : CTS_CTRL_SOFT,
3998f83f268SJie Qiu 		      CTS_CTRL_SOFT);
4008f83f268SJie Qiu }
4018f83f268SJie Qiu 
mtk_hdmi_hw_ncts_auto_write_enable(struct mtk_hdmi * hdmi,bool enable)4028f83f268SJie Qiu static void mtk_hdmi_hw_ncts_auto_write_enable(struct mtk_hdmi *hdmi,
4038f83f268SJie Qiu 					       bool enable)
4048f83f268SJie Qiu {
4058f83f268SJie Qiu 	mtk_hdmi_mask(hdmi, GRL_CTS_CTRL, enable ? NCTS_WRI_ANYTIME : 0,
4068f83f268SJie Qiu 		      NCTS_WRI_ANYTIME);
4078f83f268SJie Qiu }
4088f83f268SJie Qiu 
mtk_hdmi_hw_msic_setting(struct mtk_hdmi * hdmi,struct drm_display_mode * mode)4098f83f268SJie Qiu static void mtk_hdmi_hw_msic_setting(struct mtk_hdmi *hdmi,
4108f83f268SJie Qiu 				     struct drm_display_mode *mode)
4118f83f268SJie Qiu {
4128f83f268SJie Qiu 	mtk_hdmi_clear_bits(hdmi, GRL_CFG4, CFG4_MHL_MODE);
4138f83f268SJie Qiu 
4148f83f268SJie Qiu 	if (mode->flags & DRM_MODE_FLAG_INTERLACE &&
4158f83f268SJie Qiu 	    mode->clock == 74250 &&
4168f83f268SJie Qiu 	    mode->vdisplay == 1080)
4178f83f268SJie Qiu 		mtk_hdmi_clear_bits(hdmi, GRL_CFG2, CFG2_MHL_DE_SEL);
4188f83f268SJie Qiu 	else
4198f83f268SJie Qiu 		mtk_hdmi_set_bits(hdmi, GRL_CFG2, CFG2_MHL_DE_SEL);
4208f83f268SJie Qiu }
4218f83f268SJie Qiu 
mtk_hdmi_hw_aud_set_channel_swap(struct mtk_hdmi * hdmi,enum hdmi_aud_channel_swap_type swap)4228f83f268SJie Qiu static void mtk_hdmi_hw_aud_set_channel_swap(struct mtk_hdmi *hdmi,
4238f83f268SJie Qiu 					enum hdmi_aud_channel_swap_type swap)
4248f83f268SJie Qiu {
4258f83f268SJie Qiu 	u8 swap_bit;
4268f83f268SJie Qiu 
4278f83f268SJie Qiu 	switch (swap) {
4288f83f268SJie Qiu 	case HDMI_AUD_SWAP_LR:
4298f83f268SJie Qiu 		swap_bit = LR_SWAP;
4308f83f268SJie Qiu 		break;
4318f83f268SJie Qiu 	case HDMI_AUD_SWAP_LFE_CC:
4328f83f268SJie Qiu 		swap_bit = LFE_CC_SWAP;
4338f83f268SJie Qiu 		break;
4348f83f268SJie Qiu 	case HDMI_AUD_SWAP_LSRS:
4358f83f268SJie Qiu 		swap_bit = LSRS_SWAP;
4368f83f268SJie Qiu 		break;
4378f83f268SJie Qiu 	case HDMI_AUD_SWAP_RLS_RRS:
4388f83f268SJie Qiu 		swap_bit = RLS_RRS_SWAP;
4398f83f268SJie Qiu 		break;
4408f83f268SJie Qiu 	case HDMI_AUD_SWAP_LR_STATUS:
4418f83f268SJie Qiu 		swap_bit = LR_STATUS_SWAP;
4428f83f268SJie Qiu 		break;
4438f83f268SJie Qiu 	default:
4448f83f268SJie Qiu 		swap_bit = LFE_CC_SWAP;
4458f83f268SJie Qiu 		break;
4468f83f268SJie Qiu 	}
4478f83f268SJie Qiu 	mtk_hdmi_mask(hdmi, GRL_CH_SWAP, swap_bit, 0xff);
4488f83f268SJie Qiu }
4498f83f268SJie Qiu 
mtk_hdmi_hw_aud_set_bit_num(struct mtk_hdmi * hdmi,enum hdmi_audio_sample_size bit_num)4508f83f268SJie Qiu static void mtk_hdmi_hw_aud_set_bit_num(struct mtk_hdmi *hdmi,
4518f83f268SJie Qiu 					enum hdmi_audio_sample_size bit_num)
4528f83f268SJie Qiu {
4538f83f268SJie Qiu 	u32 val;
4548f83f268SJie Qiu 
4558f83f268SJie Qiu 	switch (bit_num) {
4568f83f268SJie Qiu 	case HDMI_AUDIO_SAMPLE_SIZE_16:
4578f83f268SJie Qiu 		val = AOUT_16BIT;
4588f83f268SJie Qiu 		break;
4598f83f268SJie Qiu 	case HDMI_AUDIO_SAMPLE_SIZE_20:
4608f83f268SJie Qiu 		val = AOUT_20BIT;
4618f83f268SJie Qiu 		break;
4628f83f268SJie Qiu 	case HDMI_AUDIO_SAMPLE_SIZE_24:
4638f83f268SJie Qiu 	case HDMI_AUDIO_SAMPLE_SIZE_STREAM:
4648f83f268SJie Qiu 		val = AOUT_24BIT;
4658f83f268SJie Qiu 		break;
4668f83f268SJie Qiu 	}
4678f83f268SJie Qiu 
4688f83f268SJie Qiu 	mtk_hdmi_mask(hdmi, GRL_AOUT_CFG, val, AOUT_BNUM_SEL_MASK);
4698f83f268SJie Qiu }
4708f83f268SJie Qiu 
mtk_hdmi_hw_aud_set_i2s_fmt(struct mtk_hdmi * hdmi,enum hdmi_aud_i2s_fmt i2s_fmt)4718f83f268SJie Qiu static void mtk_hdmi_hw_aud_set_i2s_fmt(struct mtk_hdmi *hdmi,
4728f83f268SJie Qiu 					enum hdmi_aud_i2s_fmt i2s_fmt)
4738f83f268SJie Qiu {
4748f83f268SJie Qiu 	u32 val;
4758f83f268SJie Qiu 
4768f83f268SJie Qiu 	val = mtk_hdmi_read(hdmi, GRL_CFG0);
4778f83f268SJie Qiu 	val &= ~(CFG0_W_LENGTH_MASK | CFG0_I2S_MODE_MASK);
4788f83f268SJie Qiu 
4798f83f268SJie Qiu 	switch (i2s_fmt) {
4808f83f268SJie Qiu 	case HDMI_I2S_MODE_RJT_24BIT:
4818f83f268SJie Qiu 		val |= CFG0_I2S_MODE_RTJ | CFG0_W_LENGTH_24BIT;
4828f83f268SJie Qiu 		break;
4838f83f268SJie Qiu 	case HDMI_I2S_MODE_RJT_16BIT:
4848f83f268SJie Qiu 		val |= CFG0_I2S_MODE_RTJ | CFG0_W_LENGTH_16BIT;
4858f83f268SJie Qiu 		break;
4868f83f268SJie Qiu 	case HDMI_I2S_MODE_LJT_24BIT:
4878f83f268SJie Qiu 	default:
4888f83f268SJie Qiu 		val |= CFG0_I2S_MODE_LTJ | CFG0_W_LENGTH_24BIT;
4898f83f268SJie Qiu 		break;
4908f83f268SJie Qiu 	case HDMI_I2S_MODE_LJT_16BIT:
4918f83f268SJie Qiu 		val |= CFG0_I2S_MODE_LTJ | CFG0_W_LENGTH_16BIT;
4928f83f268SJie Qiu 		break;
4938f83f268SJie Qiu 	case HDMI_I2S_MODE_I2S_24BIT:
4948f83f268SJie Qiu 		val |= CFG0_I2S_MODE_I2S | CFG0_W_LENGTH_24BIT;
4958f83f268SJie Qiu 		break;
4968f83f268SJie Qiu 	case HDMI_I2S_MODE_I2S_16BIT:
4978f83f268SJie Qiu 		val |= CFG0_I2S_MODE_I2S | CFG0_W_LENGTH_16BIT;
4988f83f268SJie Qiu 		break;
4998f83f268SJie Qiu 	}
5008f83f268SJie Qiu 	mtk_hdmi_write(hdmi, GRL_CFG0, val);
5018f83f268SJie Qiu }
5028f83f268SJie Qiu 
mtk_hdmi_hw_audio_config(struct mtk_hdmi * hdmi,bool dst)5038f83f268SJie Qiu static void mtk_hdmi_hw_audio_config(struct mtk_hdmi *hdmi, bool dst)
5048f83f268SJie Qiu {
5058f83f268SJie Qiu 	const u8 mask = HIGH_BIT_RATE | DST_NORMAL_DOUBLE | SACD_DST | DSD_SEL;
5068f83f268SJie Qiu 	u8 val;
5078f83f268SJie Qiu 
5088f83f268SJie Qiu 	/* Disable high bitrate, set DST packet normal/double */
5098f83f268SJie Qiu 	mtk_hdmi_clear_bits(hdmi, GRL_AOUT_CFG, HIGH_BIT_RATE_PACKET_ALIGN);
5108f83f268SJie Qiu 
5118f83f268SJie Qiu 	if (dst)
5128f83f268SJie Qiu 		val = DST_NORMAL_DOUBLE | SACD_DST;
5138f83f268SJie Qiu 	else
5148f83f268SJie Qiu 		val = 0;
5158f83f268SJie Qiu 
5168f83f268SJie Qiu 	mtk_hdmi_mask(hdmi, GRL_AUDIO_CFG, val, mask);
5178f83f268SJie Qiu }
5188f83f268SJie Qiu 
mtk_hdmi_hw_aud_set_i2s_chan_num(struct mtk_hdmi * hdmi,enum hdmi_aud_channel_type channel_type,u8 channel_count)5198f83f268SJie Qiu static void mtk_hdmi_hw_aud_set_i2s_chan_num(struct mtk_hdmi *hdmi,
5208f83f268SJie Qiu 					enum hdmi_aud_channel_type channel_type,
5218f83f268SJie Qiu 					u8 channel_count)
5228f83f268SJie Qiu {
5238f83f268SJie Qiu 	unsigned int ch_switch;
5248f83f268SJie Qiu 	u8 i2s_uv;
5258f83f268SJie Qiu 
5268f83f268SJie Qiu 	ch_switch = CH_SWITCH(7, 7) | CH_SWITCH(6, 6) |
5278f83f268SJie Qiu 		    CH_SWITCH(5, 5) | CH_SWITCH(4, 4) |
5288f83f268SJie Qiu 		    CH_SWITCH(3, 3) | CH_SWITCH(1, 2) |
5298f83f268SJie Qiu 		    CH_SWITCH(2, 1) | CH_SWITCH(0, 0);
5308f83f268SJie Qiu 
5318f83f268SJie Qiu 	if (channel_count == 2) {
5328f83f268SJie Qiu 		i2s_uv = I2S_UV_CH_EN(0);
5338f83f268SJie Qiu 	} else if (channel_count == 3 || channel_count == 4) {
5348f83f268SJie Qiu 		if (channel_count == 4 &&
5358f83f268SJie Qiu 		    (channel_type == HDMI_AUD_CHAN_TYPE_3_0_LRS ||
5368f83f268SJie Qiu 		    channel_type == HDMI_AUD_CHAN_TYPE_4_0))
5378f83f268SJie Qiu 			i2s_uv = I2S_UV_CH_EN(2) | I2S_UV_CH_EN(0);
5388f83f268SJie Qiu 		else
5398f83f268SJie Qiu 			i2s_uv = I2S_UV_CH_EN(3) | I2S_UV_CH_EN(2);
5408f83f268SJie Qiu 	} else if (channel_count == 6 || channel_count == 5) {
5418f83f268SJie Qiu 		if (channel_count == 6 &&
5428f83f268SJie Qiu 		    channel_type != HDMI_AUD_CHAN_TYPE_5_1 &&
5438f83f268SJie Qiu 		    channel_type != HDMI_AUD_CHAN_TYPE_4_1_CLRS) {
5448f83f268SJie Qiu 			i2s_uv = I2S_UV_CH_EN(3) | I2S_UV_CH_EN(2) |
5458f83f268SJie Qiu 				 I2S_UV_CH_EN(1) | I2S_UV_CH_EN(0);
5468f83f268SJie Qiu 		} else {
5478f83f268SJie Qiu 			i2s_uv = I2S_UV_CH_EN(2) | I2S_UV_CH_EN(1) |
5488f83f268SJie Qiu 				 I2S_UV_CH_EN(0);
5498f83f268SJie Qiu 		}
5508f83f268SJie Qiu 	} else if (channel_count == 8 || channel_count == 7) {
5518f83f268SJie Qiu 		i2s_uv = I2S_UV_CH_EN(3) | I2S_UV_CH_EN(2) |
5528f83f268SJie Qiu 			 I2S_UV_CH_EN(1) | I2S_UV_CH_EN(0);
5538f83f268SJie Qiu 	} else {
5548f83f268SJie Qiu 		i2s_uv = I2S_UV_CH_EN(0);
5558f83f268SJie Qiu 	}
5568f83f268SJie Qiu 
5578f83f268SJie Qiu 	mtk_hdmi_write(hdmi, GRL_CH_SW0, ch_switch & 0xff);
5588f83f268SJie Qiu 	mtk_hdmi_write(hdmi, GRL_CH_SW1, (ch_switch >> 8) & 0xff);
5598f83f268SJie Qiu 	mtk_hdmi_write(hdmi, GRL_CH_SW2, (ch_switch >> 16) & 0xff);
5608f83f268SJie Qiu 	mtk_hdmi_write(hdmi, GRL_I2S_UV, i2s_uv);
5618f83f268SJie Qiu }
5628f83f268SJie Qiu 
mtk_hdmi_hw_aud_set_input_type(struct mtk_hdmi * hdmi,enum hdmi_aud_input_type input_type)5638f83f268SJie Qiu static void mtk_hdmi_hw_aud_set_input_type(struct mtk_hdmi *hdmi,
5648f83f268SJie Qiu 					   enum hdmi_aud_input_type input_type)
5658f83f268SJie Qiu {
5668f83f268SJie Qiu 	u32 val;
5678f83f268SJie Qiu 
5688f83f268SJie Qiu 	val = mtk_hdmi_read(hdmi, GRL_CFG1);
5698f83f268SJie Qiu 	if (input_type == HDMI_AUD_INPUT_I2S &&
5708f83f268SJie Qiu 	    (val & CFG1_SPDIF) == CFG1_SPDIF) {
5718f83f268SJie Qiu 		val &= ~CFG1_SPDIF;
5728f83f268SJie Qiu 	} else if (input_type == HDMI_AUD_INPUT_SPDIF &&
5738f83f268SJie Qiu 		(val & CFG1_SPDIF) == 0) {
5748f83f268SJie Qiu 		val |= CFG1_SPDIF;
5758f83f268SJie Qiu 	}
5768f83f268SJie Qiu 	mtk_hdmi_write(hdmi, GRL_CFG1, val);
5778f83f268SJie Qiu }
5788f83f268SJie Qiu 
mtk_hdmi_hw_aud_set_channel_status(struct mtk_hdmi * hdmi,u8 * channel_status)5798f83f268SJie Qiu static void mtk_hdmi_hw_aud_set_channel_status(struct mtk_hdmi *hdmi,
5808f83f268SJie Qiu 					       u8 *channel_status)
5818f83f268SJie Qiu {
5828f83f268SJie Qiu 	int i;
5838f83f268SJie Qiu 
5848f83f268SJie Qiu 	for (i = 0; i < 5; i++) {
5858f83f268SJie Qiu 		mtk_hdmi_write(hdmi, GRL_I2S_C_STA0 + i * 4, channel_status[i]);
5868f83f268SJie Qiu 		mtk_hdmi_write(hdmi, GRL_L_STATUS_0 + i * 4, channel_status[i]);
5878f83f268SJie Qiu 		mtk_hdmi_write(hdmi, GRL_R_STATUS_0 + i * 4, channel_status[i]);
5888f83f268SJie Qiu 	}
5898f83f268SJie Qiu 	for (; i < 24; i++) {
5908f83f268SJie Qiu 		mtk_hdmi_write(hdmi, GRL_L_STATUS_0 + i * 4, 0);
5918f83f268SJie Qiu 		mtk_hdmi_write(hdmi, GRL_R_STATUS_0 + i * 4, 0);
5928f83f268SJie Qiu 	}
5938f83f268SJie Qiu }
5948f83f268SJie Qiu 
mtk_hdmi_hw_aud_src_reenable(struct mtk_hdmi * hdmi)5958f83f268SJie Qiu static void mtk_hdmi_hw_aud_src_reenable(struct mtk_hdmi *hdmi)
5968f83f268SJie Qiu {
5978f83f268SJie Qiu 	u32 val;
5988f83f268SJie Qiu 
5998f83f268SJie Qiu 	val = mtk_hdmi_read(hdmi, GRL_MIX_CTRL);
6008f83f268SJie Qiu 	if (val & MIX_CTRL_SRC_EN) {
6018f83f268SJie Qiu 		val &= ~MIX_CTRL_SRC_EN;
6028f83f268SJie Qiu 		mtk_hdmi_write(hdmi, GRL_MIX_CTRL, val);
6038f83f268SJie Qiu 		usleep_range(255, 512);
6048f83f268SJie Qiu 		val |= MIX_CTRL_SRC_EN;
6058f83f268SJie Qiu 		mtk_hdmi_write(hdmi, GRL_MIX_CTRL, val);
6068f83f268SJie Qiu 	}
6078f83f268SJie Qiu }
6088f83f268SJie Qiu 
mtk_hdmi_hw_aud_src_disable(struct mtk_hdmi * hdmi)6098f83f268SJie Qiu static void mtk_hdmi_hw_aud_src_disable(struct mtk_hdmi *hdmi)
6108f83f268SJie Qiu {
6118f83f268SJie Qiu 	u32 val;
6128f83f268SJie Qiu 
6138f83f268SJie Qiu 	val = mtk_hdmi_read(hdmi, GRL_MIX_CTRL);
6148f83f268SJie Qiu 	val &= ~MIX_CTRL_SRC_EN;
6158f83f268SJie Qiu 	mtk_hdmi_write(hdmi, GRL_MIX_CTRL, val);
6168f83f268SJie Qiu 	mtk_hdmi_write(hdmi, GRL_SHIFT_L1, 0x00);
6178f83f268SJie Qiu }
6188f83f268SJie Qiu 
mtk_hdmi_hw_aud_set_mclk(struct mtk_hdmi * hdmi,enum hdmi_aud_mclk mclk)6198f83f268SJie Qiu static void mtk_hdmi_hw_aud_set_mclk(struct mtk_hdmi *hdmi,
6208f83f268SJie Qiu 				     enum hdmi_aud_mclk mclk)
6218f83f268SJie Qiu {
6228f83f268SJie Qiu 	u32 val;
6238f83f268SJie Qiu 
6248f83f268SJie Qiu 	val = mtk_hdmi_read(hdmi, GRL_CFG5);
6258f83f268SJie Qiu 	val &= CFG5_CD_RATIO_MASK;
6268f83f268SJie Qiu 
6278f83f268SJie Qiu 	switch (mclk) {
6288f83f268SJie Qiu 	case HDMI_AUD_MCLK_128FS:
6298f83f268SJie Qiu 		val |= CFG5_FS128;
6308f83f268SJie Qiu 		break;
6318f83f268SJie Qiu 	case HDMI_AUD_MCLK_256FS:
6328f83f268SJie Qiu 		val |= CFG5_FS256;
6338f83f268SJie Qiu 		break;
6348f83f268SJie Qiu 	case HDMI_AUD_MCLK_384FS:
6358f83f268SJie Qiu 		val |= CFG5_FS384;
6368f83f268SJie Qiu 		break;
6378f83f268SJie Qiu 	case HDMI_AUD_MCLK_512FS:
6388f83f268SJie Qiu 		val |= CFG5_FS512;
6398f83f268SJie Qiu 		break;
6408f83f268SJie Qiu 	case HDMI_AUD_MCLK_768FS:
6418f83f268SJie Qiu 		val |= CFG5_FS768;
6428f83f268SJie Qiu 		break;
6438f83f268SJie Qiu 	default:
6448f83f268SJie Qiu 		val |= CFG5_FS256;
6458f83f268SJie Qiu 		break;
6468f83f268SJie Qiu 	}
6478f83f268SJie Qiu 	mtk_hdmi_write(hdmi, GRL_CFG5, val);
6488f83f268SJie Qiu }
6498f83f268SJie Qiu 
6508f83f268SJie Qiu struct hdmi_acr_n {
6518f83f268SJie Qiu 	unsigned int clock;
6528f83f268SJie Qiu 	unsigned int n[3];
6538f83f268SJie Qiu };
6548f83f268SJie Qiu 
6558f83f268SJie Qiu /* Recommended N values from HDMI specification, tables 7-1 to 7-3 */
6568f83f268SJie Qiu static const struct hdmi_acr_n hdmi_rec_n_table[] = {
6578f83f268SJie Qiu 	/* Clock, N: 32kHz 44.1kHz 48kHz */
6588f83f268SJie Qiu 	{  25175, {  4576,  7007,  6864 } },
6598f83f268SJie Qiu 	{  74176, { 11648, 17836, 11648 } },
6608f83f268SJie Qiu 	{ 148352, { 11648,  8918,  5824 } },
6618f83f268SJie Qiu 	{ 296703, {  5824,  4459,  5824 } },
6628f83f268SJie Qiu 	{ 297000, {  3072,  4704,  5120 } },
6638f83f268SJie Qiu 	{      0, {  4096,  6272,  6144 } }, /* all other TMDS clocks */
6648f83f268SJie Qiu };
6658f83f268SJie Qiu 
6668f83f268SJie Qiu /**
6678f83f268SJie Qiu  * hdmi_recommended_n() - Return N value recommended by HDMI specification
6688f83f268SJie Qiu  * @freq: audio sample rate in Hz
6698f83f268SJie Qiu  * @clock: rounded TMDS clock in kHz
6708f83f268SJie Qiu  */
hdmi_recommended_n(unsigned int freq,unsigned int clock)6718f83f268SJie Qiu static unsigned int hdmi_recommended_n(unsigned int freq, unsigned int clock)
6728f83f268SJie Qiu {
6738f83f268SJie Qiu 	const struct hdmi_acr_n *recommended;
6748f83f268SJie Qiu 	unsigned int i;
6758f83f268SJie Qiu 
6768f83f268SJie Qiu 	for (i = 0; i < ARRAY_SIZE(hdmi_rec_n_table) - 1; i++) {
6778f83f268SJie Qiu 		if (clock == hdmi_rec_n_table[i].clock)
6788f83f268SJie Qiu 			break;
6798f83f268SJie Qiu 	}
6808f83f268SJie Qiu 	recommended = hdmi_rec_n_table + i;
6818f83f268SJie Qiu 
6828f83f268SJie Qiu 	switch (freq) {
6838f83f268SJie Qiu 	case 32000:
6848f83f268SJie Qiu 		return recommended->n[0];
6858f83f268SJie Qiu 	case 44100:
6868f83f268SJie Qiu 		return recommended->n[1];
6878f83f268SJie Qiu 	case 48000:
6888f83f268SJie Qiu 		return recommended->n[2];
6898f83f268SJie Qiu 	case 88200:
6908f83f268SJie Qiu 		return recommended->n[1] * 2;
6918f83f268SJie Qiu 	case 96000:
6928f83f268SJie Qiu 		return recommended->n[2] * 2;
6938f83f268SJie Qiu 	case 176400:
6948f83f268SJie Qiu 		return recommended->n[1] * 4;
6958f83f268SJie Qiu 	case 192000:
6968f83f268SJie Qiu 		return recommended->n[2] * 4;
6978f83f268SJie Qiu 	default:
6988f83f268SJie Qiu 		return (128 * freq) / 1000;
6998f83f268SJie Qiu 	}
7008f83f268SJie Qiu }
7018f83f268SJie Qiu 
hdmi_mode_clock_to_hz(unsigned int clock)7028f83f268SJie Qiu static unsigned int hdmi_mode_clock_to_hz(unsigned int clock)
7038f83f268SJie Qiu {
7048f83f268SJie Qiu 	switch (clock) {
7058f83f268SJie Qiu 	case 25175:
7068f83f268SJie Qiu 		return 25174825;	/* 25.2/1.001 MHz */
7078f83f268SJie Qiu 	case 74176:
7088f83f268SJie Qiu 		return 74175824;	/* 74.25/1.001 MHz */
7098f83f268SJie Qiu 	case 148352:
7108f83f268SJie Qiu 		return 148351648;	/* 148.5/1.001 MHz */
7118f83f268SJie Qiu 	case 296703:
7128f83f268SJie Qiu 		return 296703297;	/* 297/1.001 MHz */
7138f83f268SJie Qiu 	default:
7148f83f268SJie Qiu 		return clock * 1000;
7158f83f268SJie Qiu 	}
7168f83f268SJie Qiu }
7178f83f268SJie Qiu 
hdmi_expected_cts(unsigned int audio_sample_rate,unsigned int tmds_clock,unsigned int n)7188f83f268SJie Qiu static unsigned int hdmi_expected_cts(unsigned int audio_sample_rate,
7198f83f268SJie Qiu 				      unsigned int tmds_clock, unsigned int n)
7208f83f268SJie Qiu {
7218f83f268SJie Qiu 	return DIV_ROUND_CLOSEST_ULL((u64)hdmi_mode_clock_to_hz(tmds_clock) * n,
7228f83f268SJie Qiu 				     128 * audio_sample_rate);
7238f83f268SJie Qiu }
7248f83f268SJie Qiu 
do_hdmi_hw_aud_set_ncts(struct mtk_hdmi * hdmi,unsigned int n,unsigned int cts)7258f83f268SJie Qiu static void do_hdmi_hw_aud_set_ncts(struct mtk_hdmi *hdmi, unsigned int n,
7268f83f268SJie Qiu 				    unsigned int cts)
7278f83f268SJie Qiu {
7288f83f268SJie Qiu 	unsigned char val[NCTS_BYTES];
7298f83f268SJie Qiu 	int i;
7308f83f268SJie Qiu 
7318f83f268SJie Qiu 	mtk_hdmi_write(hdmi, GRL_NCTS, 0);
7328f83f268SJie Qiu 	mtk_hdmi_write(hdmi, GRL_NCTS, 0);
7338f83f268SJie Qiu 	mtk_hdmi_write(hdmi, GRL_NCTS, 0);
7348f83f268SJie Qiu 	memset(val, 0, sizeof(val));
7358f83f268SJie Qiu 
7368f83f268SJie Qiu 	val[0] = (cts >> 24) & 0xff;
7378f83f268SJie Qiu 	val[1] = (cts >> 16) & 0xff;
7388f83f268SJie Qiu 	val[2] = (cts >> 8) & 0xff;
7398f83f268SJie Qiu 	val[3] = cts & 0xff;
7408f83f268SJie Qiu 
7418f83f268SJie Qiu 	val[4] = (n >> 16) & 0xff;
7428f83f268SJie Qiu 	val[5] = (n >> 8) & 0xff;
7438f83f268SJie Qiu 	val[6] = n & 0xff;
7448f83f268SJie Qiu 
7458f83f268SJie Qiu 	for (i = 0; i < NCTS_BYTES; i++)
7468f83f268SJie Qiu 		mtk_hdmi_write(hdmi, GRL_NCTS, val[i]);
7478f83f268SJie Qiu }
7488f83f268SJie Qiu 
mtk_hdmi_hw_aud_set_ncts(struct mtk_hdmi * hdmi,unsigned int sample_rate,unsigned int clock)7498f83f268SJie Qiu static void mtk_hdmi_hw_aud_set_ncts(struct mtk_hdmi *hdmi,
7508f83f268SJie Qiu 				     unsigned int sample_rate,
7518f83f268SJie Qiu 				     unsigned int clock)
7528f83f268SJie Qiu {
7538f83f268SJie Qiu 	unsigned int n, cts;
7548f83f268SJie Qiu 
7558f83f268SJie Qiu 	n = hdmi_recommended_n(sample_rate, clock);
7568f83f268SJie Qiu 	cts = hdmi_expected_cts(sample_rate, clock, n);
7578f83f268SJie Qiu 
7588f83f268SJie Qiu 	dev_dbg(hdmi->dev, "%s: sample_rate=%u, clock=%d, cts=%u, n=%u\n",
7598f83f268SJie Qiu 		__func__, sample_rate, clock, n, cts);
7608f83f268SJie Qiu 
7618f83f268SJie Qiu 	mtk_hdmi_mask(hdmi, DUMMY_304, AUDIO_I2S_NCTS_SEL_64,
7628f83f268SJie Qiu 		      AUDIO_I2S_NCTS_SEL);
7638f83f268SJie Qiu 	do_hdmi_hw_aud_set_ncts(hdmi, n, cts);
7648f83f268SJie Qiu }
7658f83f268SJie Qiu 
mtk_hdmi_aud_get_chnl_count(enum hdmi_aud_channel_type channel_type)7668f83f268SJie Qiu static u8 mtk_hdmi_aud_get_chnl_count(enum hdmi_aud_channel_type channel_type)
7678f83f268SJie Qiu {
7688f83f268SJie Qiu 	switch (channel_type) {
7698f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_1_0:
7708f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_1_1:
7718f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_2_0:
7728f83f268SJie Qiu 		return 2;
7738f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_2_1:
7748f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_3_0:
7758f83f268SJie Qiu 		return 3;
7768f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_3_1:
7778f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_4_0:
7788f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_3_0_LRS:
7798f83f268SJie Qiu 		return 4;
7808f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_4_1:
7818f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_5_0:
7828f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_3_1_LRS:
7838f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_4_0_CLRS:
7848f83f268SJie Qiu 		return 5;
7858f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_5_1:
7868f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_6_0:
7878f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_4_1_CLRS:
7888f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_6_0_CS:
7898f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_6_0_CH:
7908f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_6_0_OH:
7918f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_6_0_CHR:
7928f83f268SJie Qiu 		return 6;
7938f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_6_1:
7948f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_6_1_CS:
7958f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_6_1_CH:
7968f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_6_1_OH:
7978f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_6_1_CHR:
7988f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_7_0:
7998f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_7_0_LH_RH:
8008f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_7_0_LSR_RSR:
8018f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_7_0_LC_RC:
8028f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_7_0_LW_RW:
8038f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_7_0_LSD_RSD:
8048f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_7_0_LSS_RSS:
8058f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_7_0_LHS_RHS:
8068f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_7_0_CS_CH:
8078f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_7_0_CS_OH:
8088f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_7_0_CS_CHR:
8098f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_7_0_CH_OH:
8108f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_7_0_CH_CHR:
8118f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_7_0_OH_CHR:
8128f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_7_0_LSS_RSS_LSR_RSR:
8138f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_8_0_LH_RH_CS:
8148f83f268SJie Qiu 		return 7;
8158f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_7_1:
8168f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_7_1_LH_RH:
8178f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_7_1_LSR_RSR:
8188f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_7_1_LC_RC:
8198f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_7_1_LW_RW:
8208f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_7_1_LSD_RSD:
8218f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_7_1_LSS_RSS:
8228f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_7_1_LHS_RHS:
8238f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_7_1_CS_CH:
8248f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_7_1_CS_OH:
8258f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_7_1_CS_CHR:
8268f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_7_1_CH_OH:
8278f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_7_1_CH_CHR:
8288f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_7_1_OH_CHR:
8298f83f268SJie Qiu 	case HDMI_AUD_CHAN_TYPE_7_1_LSS_RSS_LSR_RSR:
8308f83f268SJie Qiu 		return 8;
8318f83f268SJie Qiu 	default:
8328f83f268SJie Qiu 		return 2;
8338f83f268SJie Qiu 	}
8348f83f268SJie Qiu }
8358f83f268SJie Qiu 
mtk_hdmi_video_change_vpll(struct mtk_hdmi * hdmi,u32 clock)8368f83f268SJie Qiu static int mtk_hdmi_video_change_vpll(struct mtk_hdmi *hdmi, u32 clock)
8378f83f268SJie Qiu {
8388f83f268SJie Qiu 	unsigned long rate;
8398f83f268SJie Qiu 	int ret;
8408f83f268SJie Qiu 
8418f83f268SJie Qiu 	/* The DPI driver already should have set TVDPLL to the correct rate */
8428f83f268SJie Qiu 	ret = clk_set_rate(hdmi->clk[MTK_HDMI_CLK_HDMI_PLL], clock);
8438f83f268SJie Qiu 	if (ret) {
8448f83f268SJie Qiu 		dev_err(hdmi->dev, "Failed to set PLL to %u Hz: %d\n", clock,
8458f83f268SJie Qiu 			ret);
8468f83f268SJie Qiu 		return ret;
8478f83f268SJie Qiu 	}
8488f83f268SJie Qiu 
8498f83f268SJie Qiu 	rate = clk_get_rate(hdmi->clk[MTK_HDMI_CLK_HDMI_PLL]);
8508f83f268SJie Qiu 
8518f83f268SJie Qiu 	if (DIV_ROUND_CLOSEST(rate, 1000) != DIV_ROUND_CLOSEST(clock, 1000))
8528f83f268SJie Qiu 		dev_warn(hdmi->dev, "Want PLL %u Hz, got %lu Hz\n", clock,
8538f83f268SJie Qiu 			 rate);
8548f83f268SJie Qiu 	else
8558f83f268SJie Qiu 		dev_dbg(hdmi->dev, "Want PLL %u Hz, got %lu Hz\n", clock, rate);
8568f83f268SJie Qiu 
8578f83f268SJie Qiu 	mtk_hdmi_hw_config_sys(hdmi);
8588f83f268SJie Qiu 	mtk_hdmi_hw_set_deep_color_mode(hdmi);
8598f83f268SJie Qiu 	return 0;
8608f83f268SJie Qiu }
8618f83f268SJie Qiu 
mtk_hdmi_video_set_display_mode(struct mtk_hdmi * hdmi,struct drm_display_mode * mode)8628f83f268SJie Qiu static void mtk_hdmi_video_set_display_mode(struct mtk_hdmi *hdmi,
8638f83f268SJie Qiu 					    struct drm_display_mode *mode)
8648f83f268SJie Qiu {
8658f83f268SJie Qiu 	mtk_hdmi_hw_reset(hdmi);
8668f83f268SJie Qiu 	mtk_hdmi_hw_enable_notice(hdmi, true);
8678f83f268SJie Qiu 	mtk_hdmi_hw_write_int_mask(hdmi, 0xff);
8688f83f268SJie Qiu 	mtk_hdmi_hw_enable_dvi_mode(hdmi, hdmi->dvi_mode);
8698f83f268SJie Qiu 	mtk_hdmi_hw_ncts_auto_write_enable(hdmi, true);
8708f83f268SJie Qiu 
8718f83f268SJie Qiu 	mtk_hdmi_hw_msic_setting(hdmi, mode);
8728f83f268SJie Qiu }
8738f83f268SJie Qiu 
8748f83f268SJie Qiu 
mtk_hdmi_aud_set_input(struct mtk_hdmi * hdmi)875a812ca19SBernard Zhao static void mtk_hdmi_aud_set_input(struct mtk_hdmi *hdmi)
8768f83f268SJie Qiu {
8778f83f268SJie Qiu 	enum hdmi_aud_channel_type chan_type;
8788f83f268SJie Qiu 	u8 chan_count;
8798f83f268SJie Qiu 	bool dst;
8808f83f268SJie Qiu 
8818f83f268SJie Qiu 	mtk_hdmi_hw_aud_set_channel_swap(hdmi, HDMI_AUD_SWAP_LFE_CC);
8828f83f268SJie Qiu 	mtk_hdmi_set_bits(hdmi, GRL_MIX_CTRL, MIX_CTRL_FLAT);
8838f83f268SJie Qiu 
8848f83f268SJie Qiu 	if (hdmi->aud_param.aud_input_type == HDMI_AUD_INPUT_SPDIF &&
8858f83f268SJie Qiu 	    hdmi->aud_param.aud_codec == HDMI_AUDIO_CODING_TYPE_DST) {
8868f83f268SJie Qiu 		mtk_hdmi_hw_aud_set_bit_num(hdmi, HDMI_AUDIO_SAMPLE_SIZE_24);
8878f83f268SJie Qiu 	} else if (hdmi->aud_param.aud_i2s_fmt == HDMI_I2S_MODE_LJT_24BIT) {
8888f83f268SJie Qiu 		hdmi->aud_param.aud_i2s_fmt = HDMI_I2S_MODE_LJT_16BIT;
8898f83f268SJie Qiu 	}
8908f83f268SJie Qiu 
8918f83f268SJie Qiu 	mtk_hdmi_hw_aud_set_i2s_fmt(hdmi, hdmi->aud_param.aud_i2s_fmt);
8928f83f268SJie Qiu 	mtk_hdmi_hw_aud_set_bit_num(hdmi, HDMI_AUDIO_SAMPLE_SIZE_24);
8938f83f268SJie Qiu 
8948f83f268SJie Qiu 	dst = ((hdmi->aud_param.aud_input_type == HDMI_AUD_INPUT_SPDIF) &&
8958f83f268SJie Qiu 	       (hdmi->aud_param.aud_codec == HDMI_AUDIO_CODING_TYPE_DST));
8968f83f268SJie Qiu 	mtk_hdmi_hw_audio_config(hdmi, dst);
8978f83f268SJie Qiu 
8988f83f268SJie Qiu 	if (hdmi->aud_param.aud_input_type == HDMI_AUD_INPUT_SPDIF)
8998f83f268SJie Qiu 		chan_type = HDMI_AUD_CHAN_TYPE_2_0;
9008f83f268SJie Qiu 	else
9018f83f268SJie Qiu 		chan_type = hdmi->aud_param.aud_input_chan_type;
9028f83f268SJie Qiu 	chan_count = mtk_hdmi_aud_get_chnl_count(chan_type);
9038f83f268SJie Qiu 	mtk_hdmi_hw_aud_set_i2s_chan_num(hdmi, chan_type, chan_count);
9048f83f268SJie Qiu 	mtk_hdmi_hw_aud_set_input_type(hdmi, hdmi->aud_param.aud_input_type);
9058f83f268SJie Qiu }
9068f83f268SJie Qiu 
mtk_hdmi_aud_set_src(struct mtk_hdmi * hdmi,struct drm_display_mode * display_mode)9078f83f268SJie Qiu static int mtk_hdmi_aud_set_src(struct mtk_hdmi *hdmi,
9088f83f268SJie Qiu 				struct drm_display_mode *display_mode)
9098f83f268SJie Qiu {
9108f83f268SJie Qiu 	unsigned int sample_rate = hdmi->aud_param.codec_params.sample_rate;
9118f83f268SJie Qiu 
912a812ca19SBernard Zhao 	mtk_hdmi_hw_ncts_enable(hdmi, false);
9138f83f268SJie Qiu 	mtk_hdmi_hw_aud_src_disable(hdmi);
9148f83f268SJie Qiu 	mtk_hdmi_clear_bits(hdmi, GRL_CFG2, CFG2_ACLK_INV);
9158f83f268SJie Qiu 
9168f83f268SJie Qiu 	if (hdmi->aud_param.aud_input_type == HDMI_AUD_INPUT_I2S) {
9178f83f268SJie Qiu 		switch (sample_rate) {
9188f83f268SJie Qiu 		case 32000:
9198f83f268SJie Qiu 		case 44100:
9208f83f268SJie Qiu 		case 48000:
9218f83f268SJie Qiu 		case 88200:
9228f83f268SJie Qiu 		case 96000:
9238f83f268SJie Qiu 			break;
9248f83f268SJie Qiu 		default:
9258f83f268SJie Qiu 			return -EINVAL;
9268f83f268SJie Qiu 		}
9278f83f268SJie Qiu 		mtk_hdmi_hw_aud_set_mclk(hdmi, hdmi->aud_param.aud_mclk);
9288f83f268SJie Qiu 	} else {
9298f83f268SJie Qiu 		switch (sample_rate) {
9308f83f268SJie Qiu 		case 32000:
9318f83f268SJie Qiu 		case 44100:
9328f83f268SJie Qiu 		case 48000:
9338f83f268SJie Qiu 			break;
9348f83f268SJie Qiu 		default:
9358f83f268SJie Qiu 			return -EINVAL;
9368f83f268SJie Qiu 		}
9378f83f268SJie Qiu 		mtk_hdmi_hw_aud_set_mclk(hdmi, HDMI_AUD_MCLK_128FS);
9388f83f268SJie Qiu 	}
9398f83f268SJie Qiu 
9408f83f268SJie Qiu 	mtk_hdmi_hw_aud_set_ncts(hdmi, sample_rate, display_mode->clock);
9418f83f268SJie Qiu 
9428f83f268SJie Qiu 	mtk_hdmi_hw_aud_src_reenable(hdmi);
9438f83f268SJie Qiu 	return 0;
9448f83f268SJie Qiu }
9458f83f268SJie Qiu 
mtk_hdmi_aud_output_config(struct mtk_hdmi * hdmi,struct drm_display_mode * display_mode)9468f83f268SJie Qiu static int mtk_hdmi_aud_output_config(struct mtk_hdmi *hdmi,
9478f83f268SJie Qiu 				      struct drm_display_mode *display_mode)
9488f83f268SJie Qiu {
9498f83f268SJie Qiu 	mtk_hdmi_hw_aud_mute(hdmi);
950a812ca19SBernard Zhao 	mtk_hdmi_hw_send_aud_packet(hdmi, false);
9518f83f268SJie Qiu 
9528f83f268SJie Qiu 	mtk_hdmi_aud_set_input(hdmi);
9538f83f268SJie Qiu 	mtk_hdmi_aud_set_src(hdmi, display_mode);
9548f83f268SJie Qiu 	mtk_hdmi_hw_aud_set_channel_status(hdmi,
9558f83f268SJie Qiu 			hdmi->aud_param.codec_params.iec.status);
9568f83f268SJie Qiu 
9578f83f268SJie Qiu 	usleep_range(50, 100);
9588f83f268SJie Qiu 
959a812ca19SBernard Zhao 	mtk_hdmi_hw_ncts_enable(hdmi, true);
960a812ca19SBernard Zhao 	mtk_hdmi_hw_send_aud_packet(hdmi, true);
9618f83f268SJie Qiu 	mtk_hdmi_hw_aud_unmute(hdmi);
9628f83f268SJie Qiu 	return 0;
9638f83f268SJie Qiu }
9648f83f268SJie Qiu 
mtk_hdmi_setup_avi_infoframe(struct mtk_hdmi * hdmi,struct drm_display_mode * mode)9658f83f268SJie Qiu static int mtk_hdmi_setup_avi_infoframe(struct mtk_hdmi *hdmi,
9668f83f268SJie Qiu 					struct drm_display_mode *mode)
9678f83f268SJie Qiu {
9688f83f268SJie Qiu 	struct hdmi_avi_infoframe frame;
9693852489cSBernard Zhao 	u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AVI_INFOFRAME_SIZE];
9708f83f268SJie Qiu 	ssize_t err;
9718f83f268SJie Qiu 
97213d0add3SVille Syrjälä 	err = drm_hdmi_avi_infoframe_from_display_mode(&frame,
9732e477391SDafna Hirschfeld 						       hdmi->curr_conn, mode);
9748f83f268SJie Qiu 	if (err < 0) {
9758f83f268SJie Qiu 		dev_err(hdmi->dev,
9768f83f268SJie Qiu 			"Failed to get AVI infoframe from mode: %zd\n", err);
9778f83f268SJie Qiu 		return err;
9788f83f268SJie Qiu 	}
9798f83f268SJie Qiu 
9808f83f268SJie Qiu 	err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
9818f83f268SJie Qiu 	if (err < 0) {
9828f83f268SJie Qiu 		dev_err(hdmi->dev, "Failed to pack AVI infoframe: %zd\n", err);
9838f83f268SJie Qiu 		return err;
9848f83f268SJie Qiu 	}
9858f83f268SJie Qiu 
9868f83f268SJie Qiu 	mtk_hdmi_hw_send_info_frame(hdmi, buffer, sizeof(buffer));
9878f83f268SJie Qiu 	return 0;
9888f83f268SJie Qiu }
9898f83f268SJie Qiu 
mtk_hdmi_setup_spd_infoframe(struct mtk_hdmi * hdmi,const char * vendor,const char * product)9908f83f268SJie Qiu static int mtk_hdmi_setup_spd_infoframe(struct mtk_hdmi *hdmi,
9918f83f268SJie Qiu 					const char *vendor,
9928f83f268SJie Qiu 					const char *product)
9938f83f268SJie Qiu {
9948f83f268SJie Qiu 	struct hdmi_spd_infoframe frame;
9953852489cSBernard Zhao 	u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_SPD_INFOFRAME_SIZE];
9968f83f268SJie Qiu 	ssize_t err;
9978f83f268SJie Qiu 
9988f83f268SJie Qiu 	err = hdmi_spd_infoframe_init(&frame, vendor, product);
9998f83f268SJie Qiu 	if (err < 0) {
10008f83f268SJie Qiu 		dev_err(hdmi->dev, "Failed to initialize SPD infoframe: %zd\n",
10018f83f268SJie Qiu 			err);
10028f83f268SJie Qiu 		return err;
10038f83f268SJie Qiu 	}
10048f83f268SJie Qiu 
10058f83f268SJie Qiu 	err = hdmi_spd_infoframe_pack(&frame, buffer, sizeof(buffer));
10068f83f268SJie Qiu 	if (err < 0) {
10078f83f268SJie Qiu 		dev_err(hdmi->dev, "Failed to pack SDP infoframe: %zd\n", err);
10088f83f268SJie Qiu 		return err;
10098f83f268SJie Qiu 	}
10108f83f268SJie Qiu 
10118f83f268SJie Qiu 	mtk_hdmi_hw_send_info_frame(hdmi, buffer, sizeof(buffer));
10128f83f268SJie Qiu 	return 0;
10138f83f268SJie Qiu }
10148f83f268SJie Qiu 
mtk_hdmi_setup_audio_infoframe(struct mtk_hdmi * hdmi)10158f83f268SJie Qiu static int mtk_hdmi_setup_audio_infoframe(struct mtk_hdmi *hdmi)
10168f83f268SJie Qiu {
10178f83f268SJie Qiu 	struct hdmi_audio_infoframe frame;
10183852489cSBernard Zhao 	u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
10198f83f268SJie Qiu 	ssize_t err;
10208f83f268SJie Qiu 
10218f83f268SJie Qiu 	err = hdmi_audio_infoframe_init(&frame);
10228f83f268SJie Qiu 	if (err < 0) {
10238f83f268SJie Qiu 		dev_err(hdmi->dev, "Failed to setup audio infoframe: %zd\n",
10248f83f268SJie Qiu 			err);
10258f83f268SJie Qiu 		return err;
10268f83f268SJie Qiu 	}
10278f83f268SJie Qiu 
10288f83f268SJie Qiu 	frame.coding_type = HDMI_AUDIO_CODING_TYPE_STREAM;
10298f83f268SJie Qiu 	frame.sample_frequency = HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM;
10308f83f268SJie Qiu 	frame.sample_size = HDMI_AUDIO_SAMPLE_SIZE_STREAM;
10318f83f268SJie Qiu 	frame.channels = mtk_hdmi_aud_get_chnl_count(
10328f83f268SJie Qiu 					hdmi->aud_param.aud_input_chan_type);
10338f83f268SJie Qiu 
10348f83f268SJie Qiu 	err = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
10358f83f268SJie Qiu 	if (err < 0) {
10368f83f268SJie Qiu 		dev_err(hdmi->dev, "Failed to pack audio infoframe: %zd\n",
10378f83f268SJie Qiu 			err);
10388f83f268SJie Qiu 		return err;
10398f83f268SJie Qiu 	}
10408f83f268SJie Qiu 
10418f83f268SJie Qiu 	mtk_hdmi_hw_send_info_frame(hdmi, buffer, sizeof(buffer));
10428f83f268SJie Qiu 	return 0;
10438f83f268SJie Qiu }
10448f83f268SJie Qiu 
mtk_hdmi_setup_vendor_specific_infoframe(struct mtk_hdmi * hdmi,struct drm_display_mode * mode)10458f83f268SJie Qiu static int mtk_hdmi_setup_vendor_specific_infoframe(struct mtk_hdmi *hdmi,
10468f83f268SJie Qiu 						struct drm_display_mode *mode)
10478f83f268SJie Qiu {
10488f83f268SJie Qiu 	struct hdmi_vendor_infoframe frame;
10498f83f268SJie Qiu 	u8 buffer[10];
10508f83f268SJie Qiu 	ssize_t err;
10518f83f268SJie Qiu 
1052f1781e9bSVille Syrjälä 	err = drm_hdmi_vendor_infoframe_from_display_mode(&frame,
10532e477391SDafna Hirschfeld 							  hdmi->curr_conn, mode);
10548f83f268SJie Qiu 	if (err) {
10558f83f268SJie Qiu 		dev_err(hdmi->dev,
10568f83f268SJie Qiu 			"Failed to get vendor infoframe from mode: %zd\n", err);
10578f83f268SJie Qiu 		return err;
10588f83f268SJie Qiu 	}
10598f83f268SJie Qiu 
10608f83f268SJie Qiu 	err = hdmi_vendor_infoframe_pack(&frame, buffer, sizeof(buffer));
1061014580ffSYYS 	if (err < 0) {
10628f83f268SJie Qiu 		dev_err(hdmi->dev, "Failed to pack vendor infoframe: %zd\n",
10638f83f268SJie Qiu 			err);
10648f83f268SJie Qiu 		return err;
10658f83f268SJie Qiu 	}
10668f83f268SJie Qiu 
10678f83f268SJie Qiu 	mtk_hdmi_hw_send_info_frame(hdmi, buffer, sizeof(buffer));
10688f83f268SJie Qiu 	return 0;
10698f83f268SJie Qiu }
10708f83f268SJie Qiu 
mtk_hdmi_output_init(struct mtk_hdmi * hdmi)10718f83f268SJie Qiu static int mtk_hdmi_output_init(struct mtk_hdmi *hdmi)
10728f83f268SJie Qiu {
10738f83f268SJie Qiu 	struct hdmi_audio_param *aud_param = &hdmi->aud_param;
10748f83f268SJie Qiu 
10758f83f268SJie Qiu 	hdmi->csp = HDMI_COLORSPACE_RGB;
10768f83f268SJie Qiu 	aud_param->aud_codec = HDMI_AUDIO_CODING_TYPE_PCM;
10778f83f268SJie Qiu 	aud_param->aud_sampe_size = HDMI_AUDIO_SAMPLE_SIZE_16;
10788f83f268SJie Qiu 	aud_param->aud_input_type = HDMI_AUD_INPUT_I2S;
10798f83f268SJie Qiu 	aud_param->aud_i2s_fmt = HDMI_I2S_MODE_I2S_24BIT;
10808f83f268SJie Qiu 	aud_param->aud_mclk = HDMI_AUD_MCLK_128FS;
10818f83f268SJie Qiu 	aud_param->aud_input_chan_type = HDMI_AUD_CHAN_TYPE_2_0;
10828f83f268SJie Qiu 
10838f83f268SJie Qiu 	return 0;
10848f83f268SJie Qiu }
10858f83f268SJie Qiu 
mtk_hdmi_audio_enable(struct mtk_hdmi * hdmi)1086188af070SBaoyou Xie static void mtk_hdmi_audio_enable(struct mtk_hdmi *hdmi)
10878f83f268SJie Qiu {
1088a812ca19SBernard Zhao 	mtk_hdmi_hw_send_aud_packet(hdmi, true);
10898f83f268SJie Qiu 	hdmi->audio_enable = true;
10908f83f268SJie Qiu }
10918f83f268SJie Qiu 
mtk_hdmi_audio_disable(struct mtk_hdmi * hdmi)1092188af070SBaoyou Xie static void mtk_hdmi_audio_disable(struct mtk_hdmi *hdmi)
10938f83f268SJie Qiu {
1094a812ca19SBernard Zhao 	mtk_hdmi_hw_send_aud_packet(hdmi, false);
10958f83f268SJie Qiu 	hdmi->audio_enable = false;
10968f83f268SJie Qiu }
10978f83f268SJie Qiu 
mtk_hdmi_audio_set_param(struct mtk_hdmi * hdmi,struct hdmi_audio_param * param)1098188af070SBaoyou Xie static int mtk_hdmi_audio_set_param(struct mtk_hdmi *hdmi,
10998f83f268SJie Qiu 				    struct hdmi_audio_param *param)
11008f83f268SJie Qiu {
11018f83f268SJie Qiu 	if (!hdmi->audio_enable) {
11028f83f268SJie Qiu 		dev_err(hdmi->dev, "hdmi audio is in disable state!\n");
11038f83f268SJie Qiu 		return -EINVAL;
11048f83f268SJie Qiu 	}
11058f83f268SJie Qiu 	dev_dbg(hdmi->dev, "codec:%d, input:%d, channel:%d, fs:%d\n",
11068f83f268SJie Qiu 		param->aud_codec, param->aud_input_type,
11078f83f268SJie Qiu 		param->aud_input_chan_type, param->codec_params.sample_rate);
11088f83f268SJie Qiu 	memcpy(&hdmi->aud_param, param, sizeof(*param));
11098f83f268SJie Qiu 	return mtk_hdmi_aud_output_config(hdmi, &hdmi->mode);
11108f83f268SJie Qiu }
11118f83f268SJie Qiu 
mtk_hdmi_output_set_display_mode(struct mtk_hdmi * hdmi,struct drm_display_mode * mode)11128f83f268SJie Qiu static int mtk_hdmi_output_set_display_mode(struct mtk_hdmi *hdmi,
11138f83f268SJie Qiu 					    struct drm_display_mode *mode)
11148f83f268SJie Qiu {
11158f83f268SJie Qiu 	int ret;
11168f83f268SJie Qiu 
11178f83f268SJie Qiu 	mtk_hdmi_hw_vid_black(hdmi, true);
11188f83f268SJie Qiu 	mtk_hdmi_hw_aud_mute(hdmi);
11198f83f268SJie Qiu 	mtk_hdmi_hw_send_av_mute(hdmi);
11208f83f268SJie Qiu 	phy_power_off(hdmi->phy);
11218f83f268SJie Qiu 
11228f83f268SJie Qiu 	ret = mtk_hdmi_video_change_vpll(hdmi,
11238f83f268SJie Qiu 					 mode->clock * 1000);
11248f83f268SJie Qiu 	if (ret) {
11258f83f268SJie Qiu 		dev_err(hdmi->dev, "Failed to set vpll: %d\n", ret);
11268f83f268SJie Qiu 		return ret;
11278f83f268SJie Qiu 	}
11288f83f268SJie Qiu 	mtk_hdmi_video_set_display_mode(hdmi, mode);
11298f83f268SJie Qiu 
11308f83f268SJie Qiu 	phy_power_on(hdmi->phy);
11318f83f268SJie Qiu 	mtk_hdmi_aud_output_config(hdmi, mode);
11328f83f268SJie Qiu 
11338f83f268SJie Qiu 	mtk_hdmi_hw_vid_black(hdmi, false);
11348f83f268SJie Qiu 	mtk_hdmi_hw_aud_unmute(hdmi);
11358f83f268SJie Qiu 	mtk_hdmi_hw_send_av_unmute(hdmi);
11368f83f268SJie Qiu 
11378f83f268SJie Qiu 	return 0;
11388f83f268SJie Qiu }
11398f83f268SJie Qiu 
11408f83f268SJie Qiu static const char * const mtk_hdmi_clk_names[MTK_HDMI_CLK_COUNT] = {
11418f83f268SJie Qiu 	[MTK_HDMI_CLK_HDMI_PIXEL] = "pixel",
11428f83f268SJie Qiu 	[MTK_HDMI_CLK_HDMI_PLL] = "pll",
11438f83f268SJie Qiu 	[MTK_HDMI_CLK_AUD_BCLK] = "bclk",
11448f83f268SJie Qiu 	[MTK_HDMI_CLK_AUD_SPDIF] = "spdif",
11458f83f268SJie Qiu };
11468f83f268SJie Qiu 
mtk_hdmi_get_all_clk(struct mtk_hdmi * hdmi,struct device_node * np)11478f83f268SJie Qiu static int mtk_hdmi_get_all_clk(struct mtk_hdmi *hdmi,
11488f83f268SJie Qiu 				struct device_node *np)
11498f83f268SJie Qiu {
11508f83f268SJie Qiu 	int i;
11518f83f268SJie Qiu 
11528f83f268SJie Qiu 	for (i = 0; i < ARRAY_SIZE(mtk_hdmi_clk_names); i++) {
11538f83f268SJie Qiu 		hdmi->clk[i] = of_clk_get_by_name(np,
11548f83f268SJie Qiu 						  mtk_hdmi_clk_names[i]);
11558f83f268SJie Qiu 		if (IS_ERR(hdmi->clk[i]))
11568f83f268SJie Qiu 			return PTR_ERR(hdmi->clk[i]);
11578f83f268SJie Qiu 	}
11588f83f268SJie Qiu 	return 0;
11598f83f268SJie Qiu }
11608f83f268SJie Qiu 
mtk_hdmi_clk_enable_audio(struct mtk_hdmi * hdmi)11618f83f268SJie Qiu static int mtk_hdmi_clk_enable_audio(struct mtk_hdmi *hdmi)
11628f83f268SJie Qiu {
11638f83f268SJie Qiu 	int ret;
11648f83f268SJie Qiu 
11658f83f268SJie Qiu 	ret = clk_prepare_enable(hdmi->clk[MTK_HDMI_CLK_AUD_BCLK]);
11668f83f268SJie Qiu 	if (ret)
11678f83f268SJie Qiu 		return ret;
11688f83f268SJie Qiu 
11698f83f268SJie Qiu 	ret = clk_prepare_enable(hdmi->clk[MTK_HDMI_CLK_AUD_SPDIF]);
11708f83f268SJie Qiu 	if (ret)
11718f83f268SJie Qiu 		goto err;
11728f83f268SJie Qiu 
11738f83f268SJie Qiu 	return 0;
11748f83f268SJie Qiu err:
11758f83f268SJie Qiu 	clk_disable_unprepare(hdmi->clk[MTK_HDMI_CLK_AUD_BCLK]);
11768f83f268SJie Qiu 	return ret;
11778f83f268SJie Qiu }
11788f83f268SJie Qiu 
mtk_hdmi_clk_disable_audio(struct mtk_hdmi * hdmi)11798f83f268SJie Qiu static void mtk_hdmi_clk_disable_audio(struct mtk_hdmi *hdmi)
11808f83f268SJie Qiu {
11818f83f268SJie Qiu 	clk_disable_unprepare(hdmi->clk[MTK_HDMI_CLK_AUD_BCLK]);
11828f83f268SJie Qiu 	clk_disable_unprepare(hdmi->clk[MTK_HDMI_CLK_AUD_SPDIF]);
11838f83f268SJie Qiu }
11848f83f268SJie Qiu 
11855d3c6447STzung-Bi Shih static enum drm_connector_status
mtk_hdmi_update_plugged_status(struct mtk_hdmi * hdmi)11865d3c6447STzung-Bi Shih mtk_hdmi_update_plugged_status(struct mtk_hdmi *hdmi)
11875d3c6447STzung-Bi Shih {
1188f07980d4STzung-Bi Shih 	bool connected;
11895d3c6447STzung-Bi Shih 
1190f07980d4STzung-Bi Shih 	mutex_lock(&hdmi->update_plugged_status_lock);
1191f07980d4STzung-Bi Shih 	connected = mtk_cec_hpd_high(hdmi->cec_dev);
11925d3c6447STzung-Bi Shih 	if (hdmi->plugged_cb && hdmi->codec_dev)
11935d3c6447STzung-Bi Shih 		hdmi->plugged_cb(hdmi->codec_dev, connected);
1194f07980d4STzung-Bi Shih 	mutex_unlock(&hdmi->update_plugged_status_lock);
11955d3c6447STzung-Bi Shih 
11965d3c6447STzung-Bi Shih 	return connected ?
11975d3c6447STzung-Bi Shih 	       connector_status_connected : connector_status_disconnected;
11985d3c6447STzung-Bi Shih }
11995d3c6447STzung-Bi Shih 
mtk_hdmi_detect(struct mtk_hdmi * hdmi)12002e477391SDafna Hirschfeld static enum drm_connector_status mtk_hdmi_detect(struct mtk_hdmi *hdmi)
12018f83f268SJie Qiu {
12025d3c6447STzung-Bi Shih 	return mtk_hdmi_update_plugged_status(hdmi);
12038f83f268SJie Qiu }
12048f83f268SJie Qiu 
1205890d6375SNathan Chancellor static enum drm_mode_status
mtk_hdmi_bridge_mode_valid(struct drm_bridge * bridge,const struct drm_display_info * info,const struct drm_display_mode * mode)1206890d6375SNathan Chancellor mtk_hdmi_bridge_mode_valid(struct drm_bridge *bridge,
12072e477391SDafna Hirschfeld 			   const struct drm_display_info *info,
12082e477391SDafna Hirschfeld 			   const struct drm_display_mode *mode)
12098f83f268SJie Qiu {
12102e477391SDafna Hirschfeld 	struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
12118f83f268SJie Qiu 
12128f83f268SJie Qiu 	dev_dbg(hdmi->dev, "xres=%d, yres=%d, refresh=%d, intl=%d clock=%d\n",
12130425662fSVille Syrjälä 		mode->hdisplay, mode->vdisplay, drm_mode_vrefresh(mode),
12148f83f268SJie Qiu 		!!(mode->flags & DRM_MODE_FLAG_INTERLACE), mode->clock * 1000);
12158f83f268SJie Qiu 
12163b8e19a0SAngeloGioacchino Del Regno 	if (hdmi->conf) {
121741ca9caaSNeil Armstrong 		if (hdmi->conf->cea_modes_only && !drm_match_cea_mode(mode))
121841ca9caaSNeil Armstrong 			return MODE_BAD;
121941ca9caaSNeil Armstrong 
1220c91026a9SNeil Armstrong 		if (hdmi->conf->max_mode_clock &&
1221c91026a9SNeil Armstrong 		    mode->clock > hdmi->conf->max_mode_clock)
1222c91026a9SNeil Armstrong 			return MODE_CLOCK_HIGH;
12233b8e19a0SAngeloGioacchino Del Regno 	}
1224c91026a9SNeil Armstrong 
12258f83f268SJie Qiu 	if (mode->clock < 27000)
12268f83f268SJie Qiu 		return MODE_CLOCK_LOW;
12278f83f268SJie Qiu 	if (mode->clock > 297000)
12288f83f268SJie Qiu 		return MODE_CLOCK_HIGH;
12298f83f268SJie Qiu 
12308f83f268SJie Qiu 	return drm_mode_validate_size(mode, 0x1fff, 0x1fff);
12318f83f268SJie Qiu }
12328f83f268SJie Qiu 
mtk_hdmi_hpd_event(bool hpd,struct device * dev)12338f83f268SJie Qiu static void mtk_hdmi_hpd_event(bool hpd, struct device *dev)
12348f83f268SJie Qiu {
12358f83f268SJie Qiu 	struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
12368f83f268SJie Qiu 
12372e477391SDafna Hirschfeld 	if (hdmi && hdmi->bridge.encoder && hdmi->bridge.encoder->dev) {
12382e477391SDafna Hirschfeld 		static enum drm_connector_status status;
12392e477391SDafna Hirschfeld 
12402e477391SDafna Hirschfeld 		status = mtk_hdmi_detect(hdmi);
12418f83f268SJie Qiu 		drm_helper_hpd_irq_event(hdmi->bridge.encoder->dev);
12422e477391SDafna Hirschfeld 		drm_bridge_hpd_notify(&hdmi->bridge, status);
12432e477391SDafna Hirschfeld 	}
12448f83f268SJie Qiu }
12458f83f268SJie Qiu 
12468f83f268SJie Qiu /*
12478f83f268SJie Qiu  * Bridge callbacks
12488f83f268SJie Qiu  */
12498f83f268SJie Qiu 
mtk_hdmi_bridge_detect(struct drm_bridge * bridge)12502e477391SDafna Hirschfeld static enum drm_connector_status mtk_hdmi_bridge_detect(struct drm_bridge *bridge)
12512e477391SDafna Hirschfeld {
12522e477391SDafna Hirschfeld 	struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
12532e477391SDafna Hirschfeld 
12542e477391SDafna Hirschfeld 	return mtk_hdmi_detect(hdmi);
12552e477391SDafna Hirschfeld }
12562e477391SDafna Hirschfeld 
mtk_hdmi_bridge_edid_read(struct drm_bridge * bridge,struct drm_connector * connector)1257ada5281aSJani Nikula static const struct drm_edid *mtk_hdmi_bridge_edid_read(struct drm_bridge *bridge,
12582e477391SDafna Hirschfeld 							struct drm_connector *connector)
12592e477391SDafna Hirschfeld {
12602e477391SDafna Hirschfeld 	struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
1261ada5281aSJani Nikula 	const struct drm_edid *drm_edid;
12622e477391SDafna Hirschfeld 
12632e477391SDafna Hirschfeld 	if (!hdmi->ddc_adpt)
12642e477391SDafna Hirschfeld 		return NULL;
1265ada5281aSJani Nikula 	drm_edid = drm_edid_read_ddc(connector, hdmi->ddc_adpt);
1266ada5281aSJani Nikula 	if (drm_edid) {
1267ada5281aSJani Nikula 		/*
1268ada5281aSJani Nikula 		 * FIXME: This should use !connector->display_info.has_audio (or
1269ada5281aSJani Nikula 		 * !connector->display_info.is_hdmi) from a path that has read
1270ada5281aSJani Nikula 		 * the EDID and called drm_edid_connector_update().
1271ada5281aSJani Nikula 		 */
1272ada5281aSJani Nikula 		const struct edid *edid = drm_edid_raw(drm_edid);
1273ada5281aSJani Nikula 
12742e477391SDafna Hirschfeld 		hdmi->dvi_mode = !drm_detect_monitor_audio(edid);
1275ada5281aSJani Nikula 	}
1276ada5281aSJani Nikula 
1277ada5281aSJani Nikula 	return drm_edid;
12782e477391SDafna Hirschfeld }
12792e477391SDafna Hirschfeld 
mtk_hdmi_bridge_attach(struct drm_bridge * bridge,enum drm_bridge_attach_flags flags)1280a25b988fSLaurent Pinchart static int mtk_hdmi_bridge_attach(struct drm_bridge *bridge,
1281a25b988fSLaurent Pinchart 				  enum drm_bridge_attach_flags flags)
12828f83f268SJie Qiu {
12838f83f268SJie Qiu 	struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
12848f83f268SJie Qiu 	int ret;
12858f83f268SJie Qiu 
12862e477391SDafna Hirschfeld 	if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
12872e477391SDafna Hirschfeld 		DRM_ERROR("%s: The flag DRM_BRIDGE_ATTACH_NO_CONNECTOR must be supplied\n",
12882e477391SDafna Hirschfeld 			  __func__);
1289a25b988fSLaurent Pinchart 		return -EINVAL;
1290a25b988fSLaurent Pinchart 	}
1291a25b988fSLaurent Pinchart 
12923bb80f24SLaurent Pinchart 	if (hdmi->next_bridge) {
12933bb80f24SLaurent Pinchart 		ret = drm_bridge_attach(bridge->encoder, hdmi->next_bridge,
1294a25b988fSLaurent Pinchart 					bridge, flags);
1295fb8d617fSLaurent Pinchart 		if (ret)
12968f83f268SJie Qiu 			return ret;
12978f83f268SJie Qiu 	}
12988f83f268SJie Qiu 
12998f83f268SJie Qiu 	mtk_cec_set_hpd_event(hdmi->cec_dev, mtk_hdmi_hpd_event, hdmi->dev);
13008f83f268SJie Qiu 
13018f83f268SJie Qiu 	return 0;
13028f83f268SJie Qiu }
13038f83f268SJie Qiu 
mtk_hdmi_bridge_mode_fixup(struct drm_bridge * bridge,const struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)13048f83f268SJie Qiu static bool mtk_hdmi_bridge_mode_fixup(struct drm_bridge *bridge,
13058f83f268SJie Qiu 				       const struct drm_display_mode *mode,
13068f83f268SJie Qiu 				       struct drm_display_mode *adjusted_mode)
13078f83f268SJie Qiu {
13088f83f268SJie Qiu 	return true;
13098f83f268SJie Qiu }
13108f83f268SJie Qiu 
mtk_hdmi_bridge_atomic_disable(struct drm_bridge * bridge,struct drm_bridge_state * old_bridge_state)1311053d231fSDafna Hirschfeld static void mtk_hdmi_bridge_atomic_disable(struct drm_bridge *bridge,
1312053d231fSDafna Hirschfeld 					   struct drm_bridge_state *old_bridge_state)
13138f83f268SJie Qiu {
13148f83f268SJie Qiu 	struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
13158f83f268SJie Qiu 
13168f83f268SJie Qiu 	if (!hdmi->enabled)
13178f83f268SJie Qiu 		return;
13188f83f268SJie Qiu 
13198f83f268SJie Qiu 	phy_power_off(hdmi->phy);
13208f83f268SJie Qiu 	clk_disable_unprepare(hdmi->clk[MTK_HDMI_CLK_HDMI_PIXEL]);
13218f83f268SJie Qiu 	clk_disable_unprepare(hdmi->clk[MTK_HDMI_CLK_HDMI_PLL]);
13228f83f268SJie Qiu 
13232e477391SDafna Hirschfeld 	hdmi->curr_conn = NULL;
13242e477391SDafna Hirschfeld 
13258f83f268SJie Qiu 	hdmi->enabled = false;
13268f83f268SJie Qiu }
13278f83f268SJie Qiu 
mtk_hdmi_bridge_atomic_post_disable(struct drm_bridge * bridge,struct drm_bridge_state * old_state)1328053d231fSDafna Hirschfeld static void mtk_hdmi_bridge_atomic_post_disable(struct drm_bridge *bridge,
1329053d231fSDafna Hirschfeld 						struct drm_bridge_state *old_state)
13308f83f268SJie Qiu {
13318f83f268SJie Qiu 	struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
13328f83f268SJie Qiu 
13338f83f268SJie Qiu 	if (!hdmi->powered)
13348f83f268SJie Qiu 		return;
13358f83f268SJie Qiu 
13368f83f268SJie Qiu 	mtk_hdmi_hw_1p4_version_enable(hdmi, true);
13378f83f268SJie Qiu 	mtk_hdmi_hw_make_reg_writable(hdmi, false);
13388f83f268SJie Qiu 
13398f83f268SJie Qiu 	hdmi->powered = false;
13408f83f268SJie Qiu }
13418f83f268SJie Qiu 
mtk_hdmi_bridge_mode_set(struct drm_bridge * bridge,const struct drm_display_mode * mode,const struct drm_display_mode * adjusted_mode)13428f83f268SJie Qiu static void mtk_hdmi_bridge_mode_set(struct drm_bridge *bridge,
134363f8f3baSLaurent Pinchart 				const struct drm_display_mode *mode,
134463f8f3baSLaurent Pinchart 				const struct drm_display_mode *adjusted_mode)
13458f83f268SJie Qiu {
13468f83f268SJie Qiu 	struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
13478f83f268SJie Qiu 
13488f83f268SJie Qiu 	dev_dbg(hdmi->dev, "cur info: name:%s, hdisplay:%d\n",
13498f83f268SJie Qiu 		adjusted_mode->name, adjusted_mode->hdisplay);
13508f83f268SJie Qiu 	dev_dbg(hdmi->dev, "hsync_start:%d,hsync_end:%d, htotal:%d",
13518f83f268SJie Qiu 		adjusted_mode->hsync_start, adjusted_mode->hsync_end,
13528f83f268SJie Qiu 		adjusted_mode->htotal);
13538f83f268SJie Qiu 	dev_dbg(hdmi->dev, "hskew:%d, vdisplay:%d\n",
13548f83f268SJie Qiu 		adjusted_mode->hskew, adjusted_mode->vdisplay);
13558f83f268SJie Qiu 	dev_dbg(hdmi->dev, "vsync_start:%d, vsync_end:%d, vtotal:%d",
13568f83f268SJie Qiu 		adjusted_mode->vsync_start, adjusted_mode->vsync_end,
13578f83f268SJie Qiu 		adjusted_mode->vtotal);
13588f83f268SJie Qiu 	dev_dbg(hdmi->dev, "vscan:%d, flag:%d\n",
13598f83f268SJie Qiu 		adjusted_mode->vscan, adjusted_mode->flags);
13608f83f268SJie Qiu 
13618f83f268SJie Qiu 	drm_mode_copy(&hdmi->mode, adjusted_mode);
13628f83f268SJie Qiu }
13638f83f268SJie Qiu 
mtk_hdmi_bridge_atomic_pre_enable(struct drm_bridge * bridge,struct drm_bridge_state * old_state)1364053d231fSDafna Hirschfeld static void mtk_hdmi_bridge_atomic_pre_enable(struct drm_bridge *bridge,
1365053d231fSDafna Hirschfeld 					      struct drm_bridge_state *old_state)
13668f83f268SJie Qiu {
13678f83f268SJie Qiu 	struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
13688f83f268SJie Qiu 
13698f83f268SJie Qiu 	mtk_hdmi_hw_make_reg_writable(hdmi, true);
13708f83f268SJie Qiu 	mtk_hdmi_hw_1p4_version_enable(hdmi, true);
13718f83f268SJie Qiu 
13728f83f268SJie Qiu 	hdmi->powered = true;
13738f83f268SJie Qiu }
13748f83f268SJie Qiu 
mtk_hdmi_send_infoframe(struct mtk_hdmi * hdmi,struct drm_display_mode * mode)1375d542b7c4SJunzhi Zhao static void mtk_hdmi_send_infoframe(struct mtk_hdmi *hdmi,
1376d542b7c4SJunzhi Zhao 				    struct drm_display_mode *mode)
1377d542b7c4SJunzhi Zhao {
1378d542b7c4SJunzhi Zhao 	mtk_hdmi_setup_audio_infoframe(hdmi);
1379d542b7c4SJunzhi Zhao 	mtk_hdmi_setup_avi_infoframe(hdmi, mode);
1380d542b7c4SJunzhi Zhao 	mtk_hdmi_setup_spd_infoframe(hdmi, "mediatek", "On-chip HDMI");
1381d542b7c4SJunzhi Zhao 	if (mode->flags & DRM_MODE_FLAG_3D_MASK)
1382d542b7c4SJunzhi Zhao 		mtk_hdmi_setup_vendor_specific_infoframe(hdmi, mode);
1383d542b7c4SJunzhi Zhao }
1384d542b7c4SJunzhi Zhao 
mtk_hdmi_bridge_atomic_enable(struct drm_bridge * bridge,struct drm_bridge_state * old_state)1385053d231fSDafna Hirschfeld static void mtk_hdmi_bridge_atomic_enable(struct drm_bridge *bridge,
1386053d231fSDafna Hirschfeld 					  struct drm_bridge_state *old_state)
13878f83f268SJie Qiu {
13882e477391SDafna Hirschfeld 	struct drm_atomic_state *state = old_state->base.state;
13898f83f268SJie Qiu 	struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
13908f83f268SJie Qiu 
13912e477391SDafna Hirschfeld 	/* Retrieve the connector through the atomic state. */
13922e477391SDafna Hirschfeld 	hdmi->curr_conn = drm_atomic_get_new_connector_for_encoder(state,
13932e477391SDafna Hirschfeld 								   bridge->encoder);
13942e477391SDafna Hirschfeld 
13958f83f268SJie Qiu 	mtk_hdmi_output_set_display_mode(hdmi, &hdmi->mode);
13968f83f268SJie Qiu 	clk_prepare_enable(hdmi->clk[MTK_HDMI_CLK_HDMI_PLL]);
13978f83f268SJie Qiu 	clk_prepare_enable(hdmi->clk[MTK_HDMI_CLK_HDMI_PIXEL]);
13988f83f268SJie Qiu 	phy_power_on(hdmi->phy);
1399d542b7c4SJunzhi Zhao 	mtk_hdmi_send_infoframe(hdmi, &hdmi->mode);
14008f83f268SJie Qiu 
14018f83f268SJie Qiu 	hdmi->enabled = true;
14028f83f268SJie Qiu }
14038f83f268SJie Qiu 
14048f83f268SJie Qiu static const struct drm_bridge_funcs mtk_hdmi_bridge_funcs = {
14052e477391SDafna Hirschfeld 	.mode_valid = mtk_hdmi_bridge_mode_valid,
1406053d231fSDafna Hirschfeld 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
1407053d231fSDafna Hirschfeld 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
1408053d231fSDafna Hirschfeld 	.atomic_reset = drm_atomic_helper_bridge_reset,
14098f83f268SJie Qiu 	.attach = mtk_hdmi_bridge_attach,
14108f83f268SJie Qiu 	.mode_fixup = mtk_hdmi_bridge_mode_fixup,
1411053d231fSDafna Hirschfeld 	.atomic_disable = mtk_hdmi_bridge_atomic_disable,
1412053d231fSDafna Hirschfeld 	.atomic_post_disable = mtk_hdmi_bridge_atomic_post_disable,
14138f83f268SJie Qiu 	.mode_set = mtk_hdmi_bridge_mode_set,
1414053d231fSDafna Hirschfeld 	.atomic_pre_enable = mtk_hdmi_bridge_atomic_pre_enable,
1415053d231fSDafna Hirschfeld 	.atomic_enable = mtk_hdmi_bridge_atomic_enable,
14162e477391SDafna Hirschfeld 	.detect = mtk_hdmi_bridge_detect,
1417ada5281aSJani Nikula 	.edid_read = mtk_hdmi_bridge_edid_read,
14188f83f268SJie Qiu };
14198f83f268SJie Qiu 
mtk_hdmi_dt_parse_pdata(struct mtk_hdmi * hdmi,struct platform_device * pdev)14208f83f268SJie Qiu static int mtk_hdmi_dt_parse_pdata(struct mtk_hdmi *hdmi,
14218f83f268SJie Qiu 				   struct platform_device *pdev)
14228f83f268SJie Qiu {
14238f83f268SJie Qiu 	struct device *dev = &pdev->dev;
14248f83f268SJie Qiu 	struct device_node *np = dev->of_node;
142586418f90SRob Herring 	struct device_node *cec_np, *remote, *i2c_np;
14268f83f268SJie Qiu 	struct platform_device *cec_pdev;
14278f83f268SJie Qiu 	struct regmap *regmap;
14288f83f268SJie Qiu 	struct resource *mem;
14298f83f268SJie Qiu 	int ret;
14308f83f268SJie Qiu 
14318f83f268SJie Qiu 	ret = mtk_hdmi_get_all_clk(hdmi, np);
14328f83f268SJie Qiu 	if (ret) {
1433af19d645SMatthias Brugger 		if (ret != -EPROBE_DEFER)
14348f83f268SJie Qiu 			dev_err(dev, "Failed to get clocks: %d\n", ret);
1435af19d645SMatthias Brugger 
14368f83f268SJie Qiu 		return ret;
14378f83f268SJie Qiu 	}
14388f83f268SJie Qiu 
14398f83f268SJie Qiu 	/* The CEC module handles HDMI hotplug detection */
1440ceff2f4dSJohan Hovold 	cec_np = of_get_compatible_child(np->parent, "mediatek,mt8173-cec");
14418f83f268SJie Qiu 	if (!cec_np) {
14428f83f268SJie Qiu 		dev_err(dev, "Failed to find CEC node\n");
14438f83f268SJie Qiu 		return -EINVAL;
14448f83f268SJie Qiu 	}
14458f83f268SJie Qiu 
14468f83f268SJie Qiu 	cec_pdev = of_find_device_by_node(cec_np);
14478f83f268SJie Qiu 	if (!cec_pdev) {
14484bf99144SRob Herring 		dev_err(hdmi->dev, "Waiting for CEC device %pOF\n",
14494bf99144SRob Herring 			cec_np);
1450ceff2f4dSJohan Hovold 		of_node_put(cec_np);
14518f83f268SJie Qiu 		return -EPROBE_DEFER;
14528f83f268SJie Qiu 	}
1453ceff2f4dSJohan Hovold 	of_node_put(cec_np);
14548f83f268SJie Qiu 	hdmi->cec_dev = &cec_pdev->dev;
14558f83f268SJie Qiu 
14568f83f268SJie Qiu 	/*
14578f83f268SJie Qiu 	 * The mediatek,syscon-hdmi property contains a phandle link to the
14588f83f268SJie Qiu 	 * MMSYS_CONFIG device and the register offset of the HDMI_SYS_CFG
14598f83f268SJie Qiu 	 * registers it contains.
14608f83f268SJie Qiu 	 */
14618f83f268SJie Qiu 	regmap = syscon_regmap_lookup_by_phandle(np, "mediatek,syscon-hdmi");
14628f83f268SJie Qiu 	ret = of_property_read_u32_index(np, "mediatek,syscon-hdmi", 1,
14638f83f268SJie Qiu 					 &hdmi->sys_offset);
14648f83f268SJie Qiu 	if (IS_ERR(regmap))
14658f83f268SJie Qiu 		ret = PTR_ERR(regmap);
14668f83f268SJie Qiu 	if (ret) {
14678f83f268SJie Qiu 		dev_err(dev,
14688f83f268SJie Qiu 			"Failed to get system configuration registers: %d\n",
14698f83f268SJie Qiu 			ret);
14700680a622SYu Kuai 		goto put_device;
14718f83f268SJie Qiu 	}
14728f83f268SJie Qiu 	hdmi->sys_regmap = regmap;
14738f83f268SJie Qiu 
14748f83f268SJie Qiu 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
14758f83f268SJie Qiu 	hdmi->regs = devm_ioremap_resource(dev, mem);
14760680a622SYu Kuai 	if (IS_ERR(hdmi->regs)) {
14770680a622SYu Kuai 		ret = PTR_ERR(hdmi->regs);
14780680a622SYu Kuai 		goto put_device;
14790680a622SYu Kuai 	}
14808f83f268SJie Qiu 
148186418f90SRob Herring 	remote = of_graph_get_remote_node(np, 1, 0);
14820680a622SYu Kuai 	if (!remote) {
14830680a622SYu Kuai 		ret = -EINVAL;
14840680a622SYu Kuai 		goto put_device;
14850680a622SYu Kuai 	}
14868f83f268SJie Qiu 
14878f83f268SJie Qiu 	if (!of_device_is_compatible(remote, "hdmi-connector")) {
14883bb80f24SLaurent Pinchart 		hdmi->next_bridge = of_drm_find_bridge(remote);
14893bb80f24SLaurent Pinchart 		if (!hdmi->next_bridge) {
14908f83f268SJie Qiu 			dev_err(dev, "Waiting for external bridge\n");
14918f83f268SJie Qiu 			of_node_put(remote);
14920680a622SYu Kuai 			ret = -EPROBE_DEFER;
14930680a622SYu Kuai 			goto put_device;
14948f83f268SJie Qiu 		}
14958f83f268SJie Qiu 	}
14968f83f268SJie Qiu 
14978f83f268SJie Qiu 	i2c_np = of_parse_phandle(remote, "ddc-i2c-bus", 0);
14988f83f268SJie Qiu 	if (!i2c_np) {
14994bf99144SRob Herring 		dev_err(dev, "Failed to find ddc-i2c-bus node in %pOF\n",
15004bf99144SRob Herring 			remote);
15018f83f268SJie Qiu 		of_node_put(remote);
15020680a622SYu Kuai 		ret = -EINVAL;
15030680a622SYu Kuai 		goto put_device;
15048f83f268SJie Qiu 	}
15058f83f268SJie Qiu 	of_node_put(remote);
15068f83f268SJie Qiu 
15078f83f268SJie Qiu 	hdmi->ddc_adpt = of_find_i2c_adapter_by_node(i2c_np);
15082ae2c331SWen Yang 	of_node_put(i2c_np);
15098f83f268SJie Qiu 	if (!hdmi->ddc_adpt) {
15108f83f268SJie Qiu 		dev_err(dev, "Failed to get ddc i2c adapter by node\n");
15110680a622SYu Kuai 		ret = -EINVAL;
15120680a622SYu Kuai 		goto put_device;
15138f83f268SJie Qiu 	}
15148f83f268SJie Qiu 
15158f83f268SJie Qiu 	return 0;
15160680a622SYu Kuai put_device:
15170680a622SYu Kuai 	put_device(hdmi->cec_dev);
15180680a622SYu Kuai 	return ret;
15198f83f268SJie Qiu }
15208f83f268SJie Qiu 
15218f83f268SJie Qiu /*
15228f83f268SJie Qiu  * HDMI audio codec callbacks
15238f83f268SJie Qiu  */
15248f83f268SJie Qiu 
mtk_hdmi_audio_hw_params(struct device * dev,void * data,struct hdmi_codec_daifmt * daifmt,struct hdmi_codec_params * params)15255dd0775eSDave Airlie static int mtk_hdmi_audio_hw_params(struct device *dev, void *data,
15268f83f268SJie Qiu 				    struct hdmi_codec_daifmt *daifmt,
15278f83f268SJie Qiu 				    struct hdmi_codec_params *params)
15288f83f268SJie Qiu {
15298f83f268SJie Qiu 	struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
15308f83f268SJie Qiu 	struct hdmi_audio_param hdmi_params;
15318f83f268SJie Qiu 	unsigned int chan = params->cea.channels;
15328f83f268SJie Qiu 
15338f83f268SJie Qiu 	dev_dbg(hdmi->dev, "%s: %u Hz, %d bit, %d channels\n", __func__,
15348f83f268SJie Qiu 		params->sample_rate, params->sample_width, chan);
15358f83f268SJie Qiu 
15368f83f268SJie Qiu 	if (!hdmi->bridge.encoder)
15378f83f268SJie Qiu 		return -ENODEV;
15388f83f268SJie Qiu 
15398f83f268SJie Qiu 	switch (chan) {
15408f83f268SJie Qiu 	case 2:
15418f83f268SJie Qiu 		hdmi_params.aud_input_chan_type = HDMI_AUD_CHAN_TYPE_2_0;
15428f83f268SJie Qiu 		break;
15438f83f268SJie Qiu 	case 4:
15448f83f268SJie Qiu 		hdmi_params.aud_input_chan_type = HDMI_AUD_CHAN_TYPE_4_0;
15458f83f268SJie Qiu 		break;
15468f83f268SJie Qiu 	case 6:
15478f83f268SJie Qiu 		hdmi_params.aud_input_chan_type = HDMI_AUD_CHAN_TYPE_5_1;
15488f83f268SJie Qiu 		break;
15498f83f268SJie Qiu 	case 8:
15508f83f268SJie Qiu 		hdmi_params.aud_input_chan_type = HDMI_AUD_CHAN_TYPE_7_1;
15518f83f268SJie Qiu 		break;
15528f83f268SJie Qiu 	default:
15538f83f268SJie Qiu 		dev_err(hdmi->dev, "channel[%d] not supported!\n", chan);
15548f83f268SJie Qiu 		return -EINVAL;
15558f83f268SJie Qiu 	}
15568f83f268SJie Qiu 
15578f83f268SJie Qiu 	switch (params->sample_rate) {
15588f83f268SJie Qiu 	case 32000:
15598f83f268SJie Qiu 	case 44100:
15608f83f268SJie Qiu 	case 48000:
15618f83f268SJie Qiu 	case 88200:
15628f83f268SJie Qiu 	case 96000:
15638f83f268SJie Qiu 	case 176400:
15648f83f268SJie Qiu 	case 192000:
15658f83f268SJie Qiu 		break;
15668f83f268SJie Qiu 	default:
15678f83f268SJie Qiu 		dev_err(hdmi->dev, "rate[%d] not supported!\n",
15688f83f268SJie Qiu 			params->sample_rate);
15698f83f268SJie Qiu 		return -EINVAL;
15708f83f268SJie Qiu 	}
15718f83f268SJie Qiu 
15728f83f268SJie Qiu 	switch (daifmt->fmt) {
15738f83f268SJie Qiu 	case HDMI_I2S:
15748f83f268SJie Qiu 		hdmi_params.aud_codec = HDMI_AUDIO_CODING_TYPE_PCM;
15758f83f268SJie Qiu 		hdmi_params.aud_sampe_size = HDMI_AUDIO_SAMPLE_SIZE_16;
15768f83f268SJie Qiu 		hdmi_params.aud_input_type = HDMI_AUD_INPUT_I2S;
15778f83f268SJie Qiu 		hdmi_params.aud_i2s_fmt = HDMI_I2S_MODE_I2S_24BIT;
15788f83f268SJie Qiu 		hdmi_params.aud_mclk = HDMI_AUD_MCLK_128FS;
15798f83f268SJie Qiu 		break;
1580d1ef028dSchunhui dai 	case HDMI_SPDIF:
1581d1ef028dSchunhui dai 		hdmi_params.aud_codec = HDMI_AUDIO_CODING_TYPE_PCM;
1582d1ef028dSchunhui dai 		hdmi_params.aud_sampe_size = HDMI_AUDIO_SAMPLE_SIZE_16;
1583d1ef028dSchunhui dai 		hdmi_params.aud_input_type = HDMI_AUD_INPUT_SPDIF;
1584d1ef028dSchunhui dai 		break;
15858f83f268SJie Qiu 	default:
15868f83f268SJie Qiu 		dev_err(hdmi->dev, "%s: Invalid DAI format %d\n", __func__,
15878f83f268SJie Qiu 			daifmt->fmt);
15888f83f268SJie Qiu 		return -EINVAL;
15898f83f268SJie Qiu 	}
15908f83f268SJie Qiu 
15918f83f268SJie Qiu 	memcpy(&hdmi_params.codec_params, params,
15928f83f268SJie Qiu 	       sizeof(hdmi_params.codec_params));
15938f83f268SJie Qiu 
15948f83f268SJie Qiu 	mtk_hdmi_audio_set_param(hdmi, &hdmi_params);
15958f83f268SJie Qiu 
15968f83f268SJie Qiu 	return 0;
15978f83f268SJie Qiu }
15988f83f268SJie Qiu 
mtk_hdmi_audio_startup(struct device * dev,void * data)15995dd0775eSDave Airlie static int mtk_hdmi_audio_startup(struct device *dev, void *data)
16008f83f268SJie Qiu {
16018f83f268SJie Qiu 	struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
16028f83f268SJie Qiu 
16038f83f268SJie Qiu 	mtk_hdmi_audio_enable(hdmi);
16048f83f268SJie Qiu 
16058f83f268SJie Qiu 	return 0;
16068f83f268SJie Qiu }
16078f83f268SJie Qiu 
mtk_hdmi_audio_shutdown(struct device * dev,void * data)16085dd0775eSDave Airlie static void mtk_hdmi_audio_shutdown(struct device *dev, void *data)
16098f83f268SJie Qiu {
16108f83f268SJie Qiu 	struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
16118f83f268SJie Qiu 
16128f83f268SJie Qiu 	mtk_hdmi_audio_disable(hdmi);
16138f83f268SJie Qiu }
16148f83f268SJie Qiu 
1615188af070SBaoyou Xie static int
mtk_hdmi_audio_mute(struct device * dev,void * data,bool enable,int direction)1616d789710fSKuninori Morimoto mtk_hdmi_audio_mute(struct device *dev, void *data,
1617d789710fSKuninori Morimoto 		    bool enable, int direction)
16188f83f268SJie Qiu {
16198f83f268SJie Qiu 	struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
16208f83f268SJie Qiu 
16218f83f268SJie Qiu 	if (enable)
16228f83f268SJie Qiu 		mtk_hdmi_hw_aud_mute(hdmi);
16238f83f268SJie Qiu 	else
16248f83f268SJie Qiu 		mtk_hdmi_hw_aud_unmute(hdmi);
16258f83f268SJie Qiu 
16268f83f268SJie Qiu 	return 0;
16278f83f268SJie Qiu }
16288f83f268SJie Qiu 
mtk_hdmi_audio_get_eld(struct device * dev,void * data,uint8_t * buf,size_t len)16295dd0775eSDave Airlie static int mtk_hdmi_audio_get_eld(struct device *dev, void *data, uint8_t *buf, size_t len)
16308f83f268SJie Qiu {
16318f83f268SJie Qiu 	struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
16328f83f268SJie Qiu 
16332e477391SDafna Hirschfeld 	if (hdmi->enabled)
16342e477391SDafna Hirschfeld 		memcpy(buf, hdmi->curr_conn->eld, min(sizeof(hdmi->curr_conn->eld), len));
16352e477391SDafna Hirschfeld 	else
16362e477391SDafna Hirschfeld 		memset(buf, 0, len);
16378f83f268SJie Qiu 	return 0;
16388f83f268SJie Qiu }
16398f83f268SJie Qiu 
mtk_hdmi_audio_hook_plugged_cb(struct device * dev,void * data,hdmi_codec_plugged_cb fn,struct device * codec_dev)16405d3c6447STzung-Bi Shih static int mtk_hdmi_audio_hook_plugged_cb(struct device *dev, void *data,
16415d3c6447STzung-Bi Shih 					  hdmi_codec_plugged_cb fn,
16425d3c6447STzung-Bi Shih 					  struct device *codec_dev)
16435d3c6447STzung-Bi Shih {
16445d3c6447STzung-Bi Shih 	struct mtk_hdmi *hdmi = data;
16455d3c6447STzung-Bi Shih 
1646f07980d4STzung-Bi Shih 	mutex_lock(&hdmi->update_plugged_status_lock);
16475d3c6447STzung-Bi Shih 	hdmi->plugged_cb = fn;
16485d3c6447STzung-Bi Shih 	hdmi->codec_dev = codec_dev;
1649f07980d4STzung-Bi Shih 	mutex_unlock(&hdmi->update_plugged_status_lock);
1650f07980d4STzung-Bi Shih 
16515d3c6447STzung-Bi Shih 	mtk_hdmi_update_plugged_status(hdmi);
16525d3c6447STzung-Bi Shih 
16535d3c6447STzung-Bi Shih 	return 0;
16545d3c6447STzung-Bi Shih }
16555d3c6447STzung-Bi Shih 
16568f83f268SJie Qiu static const struct hdmi_codec_ops mtk_hdmi_audio_codec_ops = {
16578f83f268SJie Qiu 	.hw_params = mtk_hdmi_audio_hw_params,
16588f83f268SJie Qiu 	.audio_startup = mtk_hdmi_audio_startup,
16598f83f268SJie Qiu 	.audio_shutdown = mtk_hdmi_audio_shutdown,
1660d789710fSKuninori Morimoto 	.mute_stream = mtk_hdmi_audio_mute,
16618f83f268SJie Qiu 	.get_eld = mtk_hdmi_audio_get_eld,
16625d3c6447STzung-Bi Shih 	.hook_plugged_cb = mtk_hdmi_audio_hook_plugged_cb,
1663d789710fSKuninori Morimoto 	.no_capture_mute = 1,
16648f83f268SJie Qiu };
16658f83f268SJie Qiu 
mtk_hdmi_register_audio_driver(struct device * dev)1666f9eb06cdSTzung-Bi Shih static int mtk_hdmi_register_audio_driver(struct device *dev)
16678f83f268SJie Qiu {
16685d3c6447STzung-Bi Shih 	struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
16698f83f268SJie Qiu 	struct hdmi_codec_pdata codec_data = {
16708f83f268SJie Qiu 		.ops = &mtk_hdmi_audio_codec_ops,
16718f83f268SJie Qiu 		.max_i2s_channels = 2,
16728f83f268SJie Qiu 		.i2s = 1,
16735d3c6447STzung-Bi Shih 		.data = hdmi,
16748f83f268SJie Qiu 	};
16758f83f268SJie Qiu 	struct platform_device *pdev;
16768f83f268SJie Qiu 
16778f83f268SJie Qiu 	pdev = platform_device_register_data(dev, HDMI_CODEC_DRV_NAME,
16788f83f268SJie Qiu 					     PLATFORM_DEVID_AUTO, &codec_data,
16798f83f268SJie Qiu 					     sizeof(codec_data));
16808f83f268SJie Qiu 	if (IS_ERR(pdev))
1681f9eb06cdSTzung-Bi Shih 		return PTR_ERR(pdev);
16828f83f268SJie Qiu 
16838f83f268SJie Qiu 	DRM_INFO("%s driver bound to HDMI\n", HDMI_CODEC_DRV_NAME);
1684f9eb06cdSTzung-Bi Shih 	return 0;
16858f83f268SJie Qiu }
16868f83f268SJie Qiu 
mtk_hdmi_probe(struct platform_device * pdev)16874bda3ca5SHsiao Chien Sung static int mtk_hdmi_probe(struct platform_device *pdev)
16888f83f268SJie Qiu {
16898f83f268SJie Qiu 	struct mtk_hdmi *hdmi;
16908f83f268SJie Qiu 	struct device *dev = &pdev->dev;
16918f83f268SJie Qiu 	int ret;
16928f83f268SJie Qiu 
16938f83f268SJie Qiu 	hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
16948f83f268SJie Qiu 	if (!hdmi)
16958f83f268SJie Qiu 		return -ENOMEM;
16968f83f268SJie Qiu 
16978f83f268SJie Qiu 	hdmi->dev = dev;
169896f51a4dSCK Hu 	hdmi->conf = of_device_get_match_data(dev);
16998f83f268SJie Qiu 
17008f83f268SJie Qiu 	ret = mtk_hdmi_dt_parse_pdata(hdmi, pdev);
17018f83f268SJie Qiu 	if (ret)
17028f83f268SJie Qiu 		return ret;
17038f83f268SJie Qiu 
17048f83f268SJie Qiu 	hdmi->phy = devm_phy_get(dev, "hdmi");
1705*45b70f71SNícolas F. R. A. Prado 	if (IS_ERR(hdmi->phy))
1706*45b70f71SNícolas F. R. A. Prado 		return dev_err_probe(dev, PTR_ERR(hdmi->phy),
1707*45b70f71SNícolas F. R. A. Prado 				     "Failed to get HDMI PHY\n");
17088f83f268SJie Qiu 
1709f07980d4STzung-Bi Shih 	mutex_init(&hdmi->update_plugged_status_lock);
17108f83f268SJie Qiu 	platform_set_drvdata(pdev, hdmi);
17118f83f268SJie Qiu 
17128f83f268SJie Qiu 	ret = mtk_hdmi_output_init(hdmi);
1713*45b70f71SNícolas F. R. A. Prado 	if (ret)
1714*45b70f71SNícolas F. R. A. Prado 		return dev_err_probe(dev, ret,
1715*45b70f71SNícolas F. R. A. Prado 				     "Failed to initialize hdmi output\n");
17168f83f268SJie Qiu 
1717f9eb06cdSTzung-Bi Shih 	ret = mtk_hdmi_register_audio_driver(dev);
1718*45b70f71SNícolas F. R. A. Prado 	if (ret)
1719*45b70f71SNícolas F. R. A. Prado 		return dev_err_probe(dev, ret,
1720*45b70f71SNícolas F. R. A. Prado 				     "Failed to register audio driver\n");
17218f83f268SJie Qiu 
17228f83f268SJie Qiu 	hdmi->bridge.funcs = &mtk_hdmi_bridge_funcs;
17238f83f268SJie Qiu 	hdmi->bridge.of_node = pdev->dev.of_node;
17242e477391SDafna Hirschfeld 	hdmi->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID
17252e477391SDafna Hirschfeld 			 | DRM_BRIDGE_OP_HPD;
17262e477391SDafna Hirschfeld 	hdmi->bridge.type = DRM_MODE_CONNECTOR_HDMIA;
1727e6910091SInki Dae 	drm_bridge_add(&hdmi->bridge);
17288f83f268SJie Qiu 
17298f83f268SJie Qiu 	ret = mtk_hdmi_clk_enable_audio(hdmi);
17308f83f268SJie Qiu 	if (ret) {
1731*45b70f71SNícolas F. R. A. Prado 		drm_bridge_remove(&hdmi->bridge);
1732*45b70f71SNícolas F. R. A. Prado 		return dev_err_probe(dev, ret,
1733*45b70f71SNícolas F. R. A. Prado 				     "Failed to enable audio clocks\n");
17348f83f268SJie Qiu 	}
17358f83f268SJie Qiu 
17368f83f268SJie Qiu 	return 0;
17378f83f268SJie Qiu }
17388f83f268SJie Qiu 
mtk_hdmi_remove(struct platform_device * pdev)17394bda3ca5SHsiao Chien Sung static void mtk_hdmi_remove(struct platform_device *pdev)
17408f83f268SJie Qiu {
17418f83f268SJie Qiu 	struct mtk_hdmi *hdmi = platform_get_drvdata(pdev);
17428f83f268SJie Qiu 
17438f83f268SJie Qiu 	drm_bridge_remove(&hdmi->bridge);
17448f83f268SJie Qiu 	mtk_hdmi_clk_disable_audio(hdmi);
17458f83f268SJie Qiu }
17468f83f268SJie Qiu 
17478f83f268SJie Qiu #ifdef CONFIG_PM_SLEEP
mtk_hdmi_suspend(struct device * dev)17488f83f268SJie Qiu static int mtk_hdmi_suspend(struct device *dev)
17498f83f268SJie Qiu {
17508f83f268SJie Qiu 	struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
17518f83f268SJie Qiu 
17528f83f268SJie Qiu 	mtk_hdmi_clk_disable_audio(hdmi);
17535ab546f5SEnric Balletbo i Serra 
17548f83f268SJie Qiu 	return 0;
17558f83f268SJie Qiu }
17568f83f268SJie Qiu 
mtk_hdmi_resume(struct device * dev)17578f83f268SJie Qiu static int mtk_hdmi_resume(struct device *dev)
17588f83f268SJie Qiu {
17598f83f268SJie Qiu 	struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
17608f83f268SJie Qiu 	int ret = 0;
17618f83f268SJie Qiu 
17628f83f268SJie Qiu 	ret = mtk_hdmi_clk_enable_audio(hdmi);
17638f83f268SJie Qiu 	if (ret) {
17648f83f268SJie Qiu 		dev_err(dev, "hdmi resume failed!\n");
17658f83f268SJie Qiu 		return ret;
17668f83f268SJie Qiu 	}
17678f83f268SJie Qiu 
17688f83f268SJie Qiu 	return 0;
17698f83f268SJie Qiu }
17708f83f268SJie Qiu #endif
17718f83f268SJie Qiu static SIMPLE_DEV_PM_OPS(mtk_hdmi_pm_ops,
17728f83f268SJie Qiu 			 mtk_hdmi_suspend, mtk_hdmi_resume);
17738f83f268SJie Qiu 
177496f51a4dSCK Hu static const struct mtk_hdmi_conf mtk_hdmi_conf_mt2701 = {
177596f51a4dSCK Hu 	.tz_disabled = true,
177696f51a4dSCK Hu };
177796f51a4dSCK Hu 
1778511cf7d1SNeil Armstrong static const struct mtk_hdmi_conf mtk_hdmi_conf_mt8167 = {
1779511cf7d1SNeil Armstrong 	.max_mode_clock = 148500,
1780511cf7d1SNeil Armstrong 	.cea_modes_only = true,
1781511cf7d1SNeil Armstrong };
1782511cf7d1SNeil Armstrong 
17834bda3ca5SHsiao Chien Sung static const struct of_device_id mtk_hdmi_of_ids[] = {
178496f51a4dSCK Hu 	{ .compatible = "mediatek,mt2701-hdmi",
178596f51a4dSCK Hu 	  .data = &mtk_hdmi_conf_mt2701,
178696f51a4dSCK Hu 	},
1787511cf7d1SNeil Armstrong 	{ .compatible = "mediatek,mt8167-hdmi",
1788511cf7d1SNeil Armstrong 	  .data = &mtk_hdmi_conf_mt8167,
1789511cf7d1SNeil Armstrong 	},
179096f51a4dSCK Hu 	{ .compatible = "mediatek,mt8173-hdmi",
179196f51a4dSCK Hu 	},
17928f83f268SJie Qiu 	{}
17938f83f268SJie Qiu };
17944bda3ca5SHsiao Chien Sung MODULE_DEVICE_TABLE(of, mtk_hdmi_of_ids);
17958f83f268SJie Qiu 
17968f83f268SJie Qiu static struct platform_driver mtk_hdmi_driver = {
17974bda3ca5SHsiao Chien Sung 	.probe = mtk_hdmi_probe,
17984bda3ca5SHsiao Chien Sung 	.remove_new = mtk_hdmi_remove,
17998f83f268SJie Qiu 	.driver = {
18008f83f268SJie Qiu 		.name = "mediatek-drm-hdmi",
18014bda3ca5SHsiao Chien Sung 		.of_match_table = mtk_hdmi_of_ids,
18028f83f268SJie Qiu 		.pm = &mtk_hdmi_pm_ops,
18038f83f268SJie Qiu 	},
18048f83f268SJie Qiu };
18058f83f268SJie Qiu 
18068f83f268SJie Qiu static struct platform_driver * const mtk_hdmi_drivers[] = {
18078f83f268SJie Qiu 	&mtk_hdmi_ddc_driver,
18088f83f268SJie Qiu 	&mtk_cec_driver,
18098f83f268SJie Qiu 	&mtk_hdmi_driver,
18108f83f268SJie Qiu };
18118f83f268SJie Qiu 
mtk_hdmitx_init(void)18128f83f268SJie Qiu static int __init mtk_hdmitx_init(void)
18138f83f268SJie Qiu {
1814446b8c54SPhilipp Zabel 	return platform_register_drivers(mtk_hdmi_drivers,
1815446b8c54SPhilipp Zabel 					 ARRAY_SIZE(mtk_hdmi_drivers));
18168f83f268SJie Qiu }
18178f83f268SJie Qiu 
mtk_hdmitx_exit(void)18188f83f268SJie Qiu static void __exit mtk_hdmitx_exit(void)
18198f83f268SJie Qiu {
1820446b8c54SPhilipp Zabel 	platform_unregister_drivers(mtk_hdmi_drivers,
1821446b8c54SPhilipp Zabel 				    ARRAY_SIZE(mtk_hdmi_drivers));
18228f83f268SJie Qiu }
18238f83f268SJie Qiu 
18248f83f268SJie Qiu module_init(mtk_hdmitx_init);
18258f83f268SJie Qiu module_exit(mtk_hdmitx_exit);
18268f83f268SJie Qiu 
18278f83f268SJie Qiu MODULE_AUTHOR("Jie Qiu <jie.qiu@mediatek.com>");
18288f83f268SJie Qiu MODULE_DESCRIPTION("MediaTek HDMI Driver");
18298f83f268SJie Qiu MODULE_LICENSE("GPL v2");
1830