1e67f6037SNeil Armstrong // SPDX-License-Identifier: GPL-2.0-or-later
2e67f6037SNeil Armstrong /*
3e67f6037SNeil Armstrong  * Copyright (C) 2016 BayLibre, SAS
4e67f6037SNeil Armstrong  * Author: Neil Armstrong <narmstrong@baylibre.com>
5e67f6037SNeil Armstrong  * Copyright (C) 2015 Amlogic, Inc. All rights reserved.
6e67f6037SNeil Armstrong  */
7e67f6037SNeil Armstrong 
8e67f6037SNeil Armstrong #include <linux/clk.h>
9e67f6037SNeil Armstrong #include <linux/component.h>
10e67f6037SNeil Armstrong #include <linux/kernel.h>
11e67f6037SNeil Armstrong #include <linux/module.h>
12e67f6037SNeil Armstrong #include <linux/of_device.h>
13e67f6037SNeil Armstrong #include <linux/of_graph.h>
14e67f6037SNeil Armstrong #include <linux/regulator/consumer.h>
15e67f6037SNeil Armstrong #include <linux/reset.h>
16e67f6037SNeil Armstrong 
170af5e0b4SNeil Armstrong #include <media/cec-notifier.h>
180af5e0b4SNeil Armstrong 
19e67f6037SNeil Armstrong #include <drm/drm_atomic_helper.h>
20e67f6037SNeil Armstrong #include <drm/drm_bridge.h>
210af5e0b4SNeil Armstrong #include <drm/drm_bridge_connector.h>
22e67f6037SNeil Armstrong #include <drm/drm_device.h>
23e67f6037SNeil Armstrong #include <drm/drm_edid.h>
24e67f6037SNeil Armstrong #include <drm/drm_probe_helper.h>
25e67f6037SNeil Armstrong #include <drm/drm_simple_kms_helper.h>
26e67f6037SNeil Armstrong 
27e67f6037SNeil Armstrong #include <linux/media-bus-format.h>
28e67f6037SNeil Armstrong #include <linux/videodev2.h>
29e67f6037SNeil Armstrong 
30e67f6037SNeil Armstrong #include "meson_drv.h"
31e67f6037SNeil Armstrong #include "meson_registers.h"
32e67f6037SNeil Armstrong #include "meson_vclk.h"
33e67f6037SNeil Armstrong #include "meson_venc.h"
34e67f6037SNeil Armstrong #include "meson_encoder_hdmi.h"
35e67f6037SNeil Armstrong 
36e67f6037SNeil Armstrong struct meson_encoder_hdmi {
37e67f6037SNeil Armstrong 	struct drm_encoder encoder;
38e67f6037SNeil Armstrong 	struct drm_bridge bridge;
39e67f6037SNeil Armstrong 	struct drm_bridge *next_bridge;
400af5e0b4SNeil Armstrong 	struct drm_connector *connector;
41e67f6037SNeil Armstrong 	struct meson_drm *priv;
42e67f6037SNeil Armstrong 	unsigned long output_bus_fmt;
430af5e0b4SNeil Armstrong 	struct cec_notifier *cec_notifier;
44e67f6037SNeil Armstrong };
45e67f6037SNeil Armstrong 
46e67f6037SNeil Armstrong #define bridge_to_meson_encoder_hdmi(x) \
47e67f6037SNeil Armstrong 	container_of(x, struct meson_encoder_hdmi, bridge)
48e67f6037SNeil Armstrong 
49e67f6037SNeil Armstrong static int meson_encoder_hdmi_attach(struct drm_bridge *bridge,
50e67f6037SNeil Armstrong 				     enum drm_bridge_attach_flags flags)
51e67f6037SNeil Armstrong {
52e67f6037SNeil Armstrong 	struct meson_encoder_hdmi *encoder_hdmi = bridge_to_meson_encoder_hdmi(bridge);
53e67f6037SNeil Armstrong 
54e67f6037SNeil Armstrong 	return drm_bridge_attach(bridge->encoder, encoder_hdmi->next_bridge,
55e67f6037SNeil Armstrong 				 &encoder_hdmi->bridge, flags);
56e67f6037SNeil Armstrong }
57e67f6037SNeil Armstrong 
580af5e0b4SNeil Armstrong static void meson_encoder_hdmi_detach(struct drm_bridge *bridge)
590af5e0b4SNeil Armstrong {
600af5e0b4SNeil Armstrong 	struct meson_encoder_hdmi *encoder_hdmi = bridge_to_meson_encoder_hdmi(bridge);
610af5e0b4SNeil Armstrong 
620af5e0b4SNeil Armstrong 	cec_notifier_conn_unregister(encoder_hdmi->cec_notifier);
630af5e0b4SNeil Armstrong 	encoder_hdmi->cec_notifier = NULL;
640af5e0b4SNeil Armstrong }
650af5e0b4SNeil Armstrong 
66e67f6037SNeil Armstrong static void meson_encoder_hdmi_set_vclk(struct meson_encoder_hdmi *encoder_hdmi,
67e67f6037SNeil Armstrong 					const struct drm_display_mode *mode)
68e67f6037SNeil Armstrong {
69e67f6037SNeil Armstrong 	struct meson_drm *priv = encoder_hdmi->priv;
70e67f6037SNeil Armstrong 	int vic = drm_match_cea_mode(mode);
71e67f6037SNeil Armstrong 	unsigned int phy_freq;
72e67f6037SNeil Armstrong 	unsigned int vclk_freq;
73e67f6037SNeil Armstrong 	unsigned int venc_freq;
74e67f6037SNeil Armstrong 	unsigned int hdmi_freq;
75e67f6037SNeil Armstrong 
76e67f6037SNeil Armstrong 	vclk_freq = mode->clock;
77e67f6037SNeil Armstrong 
78e67f6037SNeil Armstrong 	/* For 420, pixel clock is half unlike venc clock */
79e67f6037SNeil Armstrong 	if (encoder_hdmi->output_bus_fmt == MEDIA_BUS_FMT_UYYVYY8_0_5X24)
80e67f6037SNeil Armstrong 		vclk_freq /= 2;
81e67f6037SNeil Armstrong 
82e67f6037SNeil Armstrong 	/* TMDS clock is pixel_clock * 10 */
83e67f6037SNeil Armstrong 	phy_freq = vclk_freq * 10;
84e67f6037SNeil Armstrong 
85e67f6037SNeil Armstrong 	if (!vic) {
86e67f6037SNeil Armstrong 		meson_vclk_setup(priv, MESON_VCLK_TARGET_DMT, phy_freq,
87e67f6037SNeil Armstrong 				 vclk_freq, vclk_freq, vclk_freq, false);
88e67f6037SNeil Armstrong 		return;
89e67f6037SNeil Armstrong 	}
90e67f6037SNeil Armstrong 
91e67f6037SNeil Armstrong 	/* 480i/576i needs global pixel doubling */
92e67f6037SNeil Armstrong 	if (mode->flags & DRM_MODE_FLAG_DBLCLK)
93e67f6037SNeil Armstrong 		vclk_freq *= 2;
94e67f6037SNeil Armstrong 
95e67f6037SNeil Armstrong 	venc_freq = vclk_freq;
96e67f6037SNeil Armstrong 	hdmi_freq = vclk_freq;
97e67f6037SNeil Armstrong 
98e67f6037SNeil Armstrong 	/* VENC double pixels for 1080i, 720p and YUV420 modes */
99e67f6037SNeil Armstrong 	if (meson_venc_hdmi_venc_repeat(vic) ||
100e67f6037SNeil Armstrong 	    encoder_hdmi->output_bus_fmt == MEDIA_BUS_FMT_UYYVYY8_0_5X24)
101e67f6037SNeil Armstrong 		venc_freq *= 2;
102e67f6037SNeil Armstrong 
103e67f6037SNeil Armstrong 	vclk_freq = max(venc_freq, hdmi_freq);
104e67f6037SNeil Armstrong 
105e67f6037SNeil Armstrong 	if (mode->flags & DRM_MODE_FLAG_DBLCLK)
106e67f6037SNeil Armstrong 		venc_freq /= 2;
107e67f6037SNeil Armstrong 
108e67f6037SNeil Armstrong 	dev_dbg(priv->dev, "vclk:%d phy=%d venc=%d hdmi=%d enci=%d\n",
109e67f6037SNeil Armstrong 		phy_freq, vclk_freq, venc_freq, hdmi_freq,
110e67f6037SNeil Armstrong 		priv->venc.hdmi_use_enci);
111e67f6037SNeil Armstrong 
112e67f6037SNeil Armstrong 	meson_vclk_setup(priv, MESON_VCLK_TARGET_HDMI, phy_freq, vclk_freq,
113e67f6037SNeil Armstrong 			 venc_freq, hdmi_freq, priv->venc.hdmi_use_enci);
114e67f6037SNeil Armstrong }
115e67f6037SNeil Armstrong 
116e67f6037SNeil Armstrong static enum drm_mode_status meson_encoder_hdmi_mode_valid(struct drm_bridge *bridge,
117e67f6037SNeil Armstrong 					const struct drm_display_info *display_info,
118e67f6037SNeil Armstrong 					const struct drm_display_mode *mode)
119e67f6037SNeil Armstrong {
120e67f6037SNeil Armstrong 	struct meson_encoder_hdmi *encoder_hdmi = bridge_to_meson_encoder_hdmi(bridge);
121e67f6037SNeil Armstrong 	struct meson_drm *priv = encoder_hdmi->priv;
122e67f6037SNeil Armstrong 	bool is_hdmi2_sink = display_info->hdmi.scdc.supported;
123e67f6037SNeil Armstrong 	unsigned int phy_freq;
124e67f6037SNeil Armstrong 	unsigned int vclk_freq;
125e67f6037SNeil Armstrong 	unsigned int venc_freq;
126e67f6037SNeil Armstrong 	unsigned int hdmi_freq;
127e67f6037SNeil Armstrong 	int vic = drm_match_cea_mode(mode);
128e67f6037SNeil Armstrong 	enum drm_mode_status status;
129e67f6037SNeil Armstrong 
130e67f6037SNeil Armstrong 	dev_dbg(priv->dev, "Modeline " DRM_MODE_FMT "\n", DRM_MODE_ARG(mode));
131e67f6037SNeil Armstrong 
132e67f6037SNeil Armstrong 	/* If sink does not support 540MHz, reject the non-420 HDMI2 modes */
133e67f6037SNeil Armstrong 	if (display_info->max_tmds_clock &&
134e67f6037SNeil Armstrong 	    mode->clock > display_info->max_tmds_clock &&
135e67f6037SNeil Armstrong 	    !drm_mode_is_420_only(display_info, mode) &&
136e67f6037SNeil Armstrong 	    !drm_mode_is_420_also(display_info, mode))
137e67f6037SNeil Armstrong 		return MODE_BAD;
138e67f6037SNeil Armstrong 
139e67f6037SNeil Armstrong 	/* Check against non-VIC supported modes */
140e67f6037SNeil Armstrong 	if (!vic) {
141e67f6037SNeil Armstrong 		status = meson_venc_hdmi_supported_mode(mode);
142e67f6037SNeil Armstrong 		if (status != MODE_OK)
143e67f6037SNeil Armstrong 			return status;
144e67f6037SNeil Armstrong 
145e67f6037SNeil Armstrong 		return meson_vclk_dmt_supported_freq(priv, mode->clock);
146e67f6037SNeil Armstrong 	/* Check against supported VIC modes */
147e67f6037SNeil Armstrong 	} else if (!meson_venc_hdmi_supported_vic(vic))
148e67f6037SNeil Armstrong 		return MODE_BAD;
149e67f6037SNeil Armstrong 
150e67f6037SNeil Armstrong 	vclk_freq = mode->clock;
151e67f6037SNeil Armstrong 
152e67f6037SNeil Armstrong 	/* For 420, pixel clock is half unlike venc clock */
153e67f6037SNeil Armstrong 	if (drm_mode_is_420_only(display_info, mode) ||
154e67f6037SNeil Armstrong 	    (!is_hdmi2_sink &&
155e67f6037SNeil Armstrong 	     drm_mode_is_420_also(display_info, mode)))
156e67f6037SNeil Armstrong 		vclk_freq /= 2;
157e67f6037SNeil Armstrong 
158e67f6037SNeil Armstrong 	/* TMDS clock is pixel_clock * 10 */
159e67f6037SNeil Armstrong 	phy_freq = vclk_freq * 10;
160e67f6037SNeil Armstrong 
161e67f6037SNeil Armstrong 	/* 480i/576i needs global pixel doubling */
162e67f6037SNeil Armstrong 	if (mode->flags & DRM_MODE_FLAG_DBLCLK)
163e67f6037SNeil Armstrong 		vclk_freq *= 2;
164e67f6037SNeil Armstrong 
165e67f6037SNeil Armstrong 	venc_freq = vclk_freq;
166e67f6037SNeil Armstrong 	hdmi_freq = vclk_freq;
167e67f6037SNeil Armstrong 
168e67f6037SNeil Armstrong 	/* VENC double pixels for 1080i, 720p and YUV420 modes */
169e67f6037SNeil Armstrong 	if (meson_venc_hdmi_venc_repeat(vic) ||
170e67f6037SNeil Armstrong 	    drm_mode_is_420_only(display_info, mode) ||
171e67f6037SNeil Armstrong 	    (!is_hdmi2_sink &&
172e67f6037SNeil Armstrong 	     drm_mode_is_420_also(display_info, mode)))
173e67f6037SNeil Armstrong 		venc_freq *= 2;
174e67f6037SNeil Armstrong 
175e67f6037SNeil Armstrong 	vclk_freq = max(venc_freq, hdmi_freq);
176e67f6037SNeil Armstrong 
177e67f6037SNeil Armstrong 	if (mode->flags & DRM_MODE_FLAG_DBLCLK)
178e67f6037SNeil Armstrong 		venc_freq /= 2;
179e67f6037SNeil Armstrong 
180e67f6037SNeil Armstrong 	dev_dbg(priv->dev, "%s: vclk:%d phy=%d venc=%d hdmi=%d\n",
181e67f6037SNeil Armstrong 		__func__, phy_freq, vclk_freq, venc_freq, hdmi_freq);
182e67f6037SNeil Armstrong 
183e67f6037SNeil Armstrong 	return meson_vclk_vic_supported_freq(priv, phy_freq, vclk_freq);
184e67f6037SNeil Armstrong }
185e67f6037SNeil Armstrong 
186e67f6037SNeil Armstrong static void meson_encoder_hdmi_atomic_enable(struct drm_bridge *bridge,
187e67f6037SNeil Armstrong 					     struct drm_bridge_state *bridge_state)
188e67f6037SNeil Armstrong {
189e67f6037SNeil Armstrong 	struct meson_encoder_hdmi *encoder_hdmi = bridge_to_meson_encoder_hdmi(bridge);
190e67f6037SNeil Armstrong 	struct drm_atomic_state *state = bridge_state->base.state;
191e67f6037SNeil Armstrong 	unsigned int ycrcb_map = VPU_HDMI_OUTPUT_CBYCR;
192e67f6037SNeil Armstrong 	struct meson_drm *priv = encoder_hdmi->priv;
193e67f6037SNeil Armstrong 	struct drm_connector_state *conn_state;
194e67f6037SNeil Armstrong 	const struct drm_display_mode *mode;
195e67f6037SNeil Armstrong 	struct drm_crtc_state *crtc_state;
196e67f6037SNeil Armstrong 	struct drm_connector *connector;
197e67f6037SNeil Armstrong 	bool yuv420_mode = false;
198e67f6037SNeil Armstrong 	int vic;
199e67f6037SNeil Armstrong 
200e67f6037SNeil Armstrong 	connector = drm_atomic_get_new_connector_for_encoder(state, bridge->encoder);
201e67f6037SNeil Armstrong 	if (WARN_ON(!connector))
202e67f6037SNeil Armstrong 		return;
203e67f6037SNeil Armstrong 
204e67f6037SNeil Armstrong 	conn_state = drm_atomic_get_new_connector_state(state, connector);
205e67f6037SNeil Armstrong 	if (WARN_ON(!conn_state))
206e67f6037SNeil Armstrong 		return;
207e67f6037SNeil Armstrong 
208e67f6037SNeil Armstrong 	crtc_state = drm_atomic_get_new_crtc_state(state, conn_state->crtc);
209e67f6037SNeil Armstrong 	if (WARN_ON(!crtc_state))
210e67f6037SNeil Armstrong 		return;
211e67f6037SNeil Armstrong 
212e67f6037SNeil Armstrong 	mode = &crtc_state->adjusted_mode;
213e67f6037SNeil Armstrong 
214e67f6037SNeil Armstrong 	vic = drm_match_cea_mode(mode);
215e67f6037SNeil Armstrong 
216e67f6037SNeil Armstrong 	dev_dbg(priv->dev, "\"%s\" vic %d\n", mode->name, vic);
217e67f6037SNeil Armstrong 
218e67f6037SNeil Armstrong 	if (encoder_hdmi->output_bus_fmt == MEDIA_BUS_FMT_UYYVYY8_0_5X24) {
219e67f6037SNeil Armstrong 		ycrcb_map = VPU_HDMI_OUTPUT_CRYCB;
220e67f6037SNeil Armstrong 		yuv420_mode = true;
221e67f6037SNeil Armstrong 	}
222e67f6037SNeil Armstrong 
223e67f6037SNeil Armstrong 	/* VENC + VENC-DVI Mode setup */
224e67f6037SNeil Armstrong 	meson_venc_hdmi_mode_set(priv, vic, ycrcb_map, yuv420_mode, mode);
225e67f6037SNeil Armstrong 
226e67f6037SNeil Armstrong 	/* VCLK Set clock */
227e67f6037SNeil Armstrong 	meson_encoder_hdmi_set_vclk(encoder_hdmi, mode);
228e67f6037SNeil Armstrong 
229e67f6037SNeil Armstrong 	if (encoder_hdmi->output_bus_fmt == MEDIA_BUS_FMT_UYYVYY8_0_5X24)
230e67f6037SNeil Armstrong 		/* Setup YUV420 to HDMI-TX, no 10bit diphering */
231e67f6037SNeil Armstrong 		writel_relaxed(2 | (2 << 2),
232e67f6037SNeil Armstrong 			       priv->io_base + _REG(VPU_HDMI_FMT_CTRL));
233e67f6037SNeil Armstrong 	else
234e67f6037SNeil Armstrong 		/* Setup YUV444 to HDMI-TX, no 10bit diphering */
235e67f6037SNeil Armstrong 		writel_relaxed(0, priv->io_base + _REG(VPU_HDMI_FMT_CTRL));
236e67f6037SNeil Armstrong 
237e67f6037SNeil Armstrong 	dev_dbg(priv->dev, "%s\n", priv->venc.hdmi_use_enci ? "VENCI" : "VENCP");
238e67f6037SNeil Armstrong 
239e67f6037SNeil Armstrong 	if (priv->venc.hdmi_use_enci)
240e67f6037SNeil Armstrong 		writel_relaxed(1, priv->io_base + _REG(ENCI_VIDEO_EN));
241e67f6037SNeil Armstrong 	else
242e67f6037SNeil Armstrong 		writel_relaxed(1, priv->io_base + _REG(ENCP_VIDEO_EN));
243e67f6037SNeil Armstrong }
244e67f6037SNeil Armstrong 
245e67f6037SNeil Armstrong static void meson_encoder_hdmi_atomic_disable(struct drm_bridge *bridge,
246e67f6037SNeil Armstrong 					     struct drm_bridge_state *bridge_state)
247e67f6037SNeil Armstrong {
248e67f6037SNeil Armstrong 	struct meson_encoder_hdmi *encoder_hdmi = bridge_to_meson_encoder_hdmi(bridge);
249e67f6037SNeil Armstrong 	struct meson_drm *priv = encoder_hdmi->priv;
250e67f6037SNeil Armstrong 
251e67f6037SNeil Armstrong 	writel_bits_relaxed(0x3, 0,
252e67f6037SNeil Armstrong 			    priv->io_base + _REG(VPU_HDMI_SETTING));
253e67f6037SNeil Armstrong 
254e67f6037SNeil Armstrong 	writel_relaxed(0, priv->io_base + _REG(ENCI_VIDEO_EN));
255e67f6037SNeil Armstrong 	writel_relaxed(0, priv->io_base + _REG(ENCP_VIDEO_EN));
256e67f6037SNeil Armstrong }
257e67f6037SNeil Armstrong 
258e67f6037SNeil Armstrong static const u32 meson_encoder_hdmi_out_bus_fmts[] = {
259e67f6037SNeil Armstrong 	MEDIA_BUS_FMT_YUV8_1X24,
260e67f6037SNeil Armstrong 	MEDIA_BUS_FMT_UYYVYY8_0_5X24,
261e67f6037SNeil Armstrong };
262e67f6037SNeil Armstrong 
263e67f6037SNeil Armstrong static u32 *
264e67f6037SNeil Armstrong meson_encoder_hdmi_get_inp_bus_fmts(struct drm_bridge *bridge,
265e67f6037SNeil Armstrong 					struct drm_bridge_state *bridge_state,
266e67f6037SNeil Armstrong 					struct drm_crtc_state *crtc_state,
267e67f6037SNeil Armstrong 					struct drm_connector_state *conn_state,
268e67f6037SNeil Armstrong 					u32 output_fmt,
269e67f6037SNeil Armstrong 					unsigned int *num_input_fmts)
270e67f6037SNeil Armstrong {
271e67f6037SNeil Armstrong 	u32 *input_fmts = NULL;
272e67f6037SNeil Armstrong 	int i;
273e67f6037SNeil Armstrong 
274e67f6037SNeil Armstrong 	*num_input_fmts = 0;
275e67f6037SNeil Armstrong 
276e67f6037SNeil Armstrong 	for (i = 0 ; i < ARRAY_SIZE(meson_encoder_hdmi_out_bus_fmts) ; ++i) {
277e67f6037SNeil Armstrong 		if (output_fmt == meson_encoder_hdmi_out_bus_fmts[i]) {
278e67f6037SNeil Armstrong 			*num_input_fmts = 1;
279e67f6037SNeil Armstrong 			input_fmts = kcalloc(*num_input_fmts,
280e67f6037SNeil Armstrong 					     sizeof(*input_fmts),
281e67f6037SNeil Armstrong 					     GFP_KERNEL);
282e67f6037SNeil Armstrong 			if (!input_fmts)
283e67f6037SNeil Armstrong 				return NULL;
284e67f6037SNeil Armstrong 
285e67f6037SNeil Armstrong 			input_fmts[0] = output_fmt;
286e67f6037SNeil Armstrong 
287e67f6037SNeil Armstrong 			break;
288e67f6037SNeil Armstrong 		}
289e67f6037SNeil Armstrong 	}
290e67f6037SNeil Armstrong 
291e67f6037SNeil Armstrong 	return input_fmts;
292e67f6037SNeil Armstrong }
293e67f6037SNeil Armstrong 
294e67f6037SNeil Armstrong static int meson_encoder_hdmi_atomic_check(struct drm_bridge *bridge,
295e67f6037SNeil Armstrong 					struct drm_bridge_state *bridge_state,
296e67f6037SNeil Armstrong 					struct drm_crtc_state *crtc_state,
297e67f6037SNeil Armstrong 					struct drm_connector_state *conn_state)
298e67f6037SNeil Armstrong {
299e67f6037SNeil Armstrong 	struct meson_encoder_hdmi *encoder_hdmi = bridge_to_meson_encoder_hdmi(bridge);
300e67f6037SNeil Armstrong 	struct drm_connector_state *old_conn_state =
301e67f6037SNeil Armstrong 		drm_atomic_get_old_connector_state(conn_state->state, conn_state->connector);
302e67f6037SNeil Armstrong 	struct meson_drm *priv = encoder_hdmi->priv;
303e67f6037SNeil Armstrong 
304e67f6037SNeil Armstrong 	encoder_hdmi->output_bus_fmt = bridge_state->output_bus_cfg.format;
305e67f6037SNeil Armstrong 
306e67f6037SNeil Armstrong 	dev_dbg(priv->dev, "output_bus_fmt %lx\n", encoder_hdmi->output_bus_fmt);
307e67f6037SNeil Armstrong 
308e67f6037SNeil Armstrong 	if (!drm_connector_atomic_hdr_metadata_equal(old_conn_state, conn_state))
309e67f6037SNeil Armstrong 		crtc_state->mode_changed = true;
310e67f6037SNeil Armstrong 
311e67f6037SNeil Armstrong 	return 0;
312e67f6037SNeil Armstrong }
313e67f6037SNeil Armstrong 
3140af5e0b4SNeil Armstrong static void meson_encoder_hdmi_hpd_notify(struct drm_bridge *bridge,
3150af5e0b4SNeil Armstrong 					  enum drm_connector_status status)
3160af5e0b4SNeil Armstrong {
3170af5e0b4SNeil Armstrong 	struct meson_encoder_hdmi *encoder_hdmi = bridge_to_meson_encoder_hdmi(bridge);
3180af5e0b4SNeil Armstrong 	struct edid *edid;
3190af5e0b4SNeil Armstrong 
3200af5e0b4SNeil Armstrong 	if (!encoder_hdmi->cec_notifier)
3210af5e0b4SNeil Armstrong 		return;
3220af5e0b4SNeil Armstrong 
3230af5e0b4SNeil Armstrong 	if (status == connector_status_connected) {
3240af5e0b4SNeil Armstrong 		edid = drm_bridge_get_edid(encoder_hdmi->next_bridge, encoder_hdmi->connector);
3250af5e0b4SNeil Armstrong 		if (!edid)
3260af5e0b4SNeil Armstrong 			return;
3270af5e0b4SNeil Armstrong 
3280af5e0b4SNeil Armstrong 		cec_notifier_set_phys_addr_from_edid(encoder_hdmi->cec_notifier, edid);
3290af5e0b4SNeil Armstrong 	} else
3300af5e0b4SNeil Armstrong 		cec_notifier_phys_addr_invalidate(encoder_hdmi->cec_notifier);
3310af5e0b4SNeil Armstrong }
3320af5e0b4SNeil Armstrong 
333e67f6037SNeil Armstrong static const struct drm_bridge_funcs meson_encoder_hdmi_bridge_funcs = {
334e67f6037SNeil Armstrong 	.attach = meson_encoder_hdmi_attach,
3350af5e0b4SNeil Armstrong 	.detach = meson_encoder_hdmi_detach,
336e67f6037SNeil Armstrong 	.mode_valid = meson_encoder_hdmi_mode_valid,
3370af5e0b4SNeil Armstrong 	.hpd_notify = meson_encoder_hdmi_hpd_notify,
338e67f6037SNeil Armstrong 	.atomic_enable = meson_encoder_hdmi_atomic_enable,
339e67f6037SNeil Armstrong 	.atomic_disable = meson_encoder_hdmi_atomic_disable,
340e67f6037SNeil Armstrong 	.atomic_get_input_bus_fmts = meson_encoder_hdmi_get_inp_bus_fmts,
341e67f6037SNeil Armstrong 	.atomic_check = meson_encoder_hdmi_atomic_check,
342e67f6037SNeil Armstrong 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
343e67f6037SNeil Armstrong 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
344e67f6037SNeil Armstrong 	.atomic_reset = drm_atomic_helper_bridge_reset,
345e67f6037SNeil Armstrong };
346e67f6037SNeil Armstrong 
347e67f6037SNeil Armstrong int meson_encoder_hdmi_init(struct meson_drm *priv)
348e67f6037SNeil Armstrong {
349e67f6037SNeil Armstrong 	struct meson_encoder_hdmi *meson_encoder_hdmi;
3500af5e0b4SNeil Armstrong 	struct platform_device *pdev;
351e67f6037SNeil Armstrong 	struct device_node *remote;
352e67f6037SNeil Armstrong 	int ret;
353e67f6037SNeil Armstrong 
354e67f6037SNeil Armstrong 	meson_encoder_hdmi = devm_kzalloc(priv->dev, sizeof(*meson_encoder_hdmi), GFP_KERNEL);
355e67f6037SNeil Armstrong 	if (!meson_encoder_hdmi)
356e67f6037SNeil Armstrong 		return -ENOMEM;
357e67f6037SNeil Armstrong 
358e67f6037SNeil Armstrong 	/* HDMI Transceiver Bridge */
359e67f6037SNeil Armstrong 	remote = of_graph_get_remote_node(priv->dev->of_node, 1, 0);
360e67f6037SNeil Armstrong 	if (!remote) {
361e67f6037SNeil Armstrong 		dev_err(priv->dev, "HDMI transceiver device is disabled");
362e67f6037SNeil Armstrong 		return 0;
363e67f6037SNeil Armstrong 	}
364e67f6037SNeil Armstrong 
365e67f6037SNeil Armstrong 	meson_encoder_hdmi->next_bridge = of_drm_find_bridge(remote);
366e67f6037SNeil Armstrong 	if (!meson_encoder_hdmi->next_bridge) {
367e67f6037SNeil Armstrong 		dev_err(priv->dev, "Failed to find HDMI transceiver bridge\n");
368e67f6037SNeil Armstrong 		return -EPROBE_DEFER;
369e67f6037SNeil Armstrong 	}
370e67f6037SNeil Armstrong 
371e67f6037SNeil Armstrong 	/* HDMI Encoder Bridge */
372e67f6037SNeil Armstrong 	meson_encoder_hdmi->bridge.funcs = &meson_encoder_hdmi_bridge_funcs;
373e67f6037SNeil Armstrong 	meson_encoder_hdmi->bridge.of_node = priv->dev->of_node;
374e67f6037SNeil Armstrong 	meson_encoder_hdmi->bridge.type = DRM_MODE_CONNECTOR_HDMIA;
3750af5e0b4SNeil Armstrong 	meson_encoder_hdmi->bridge.interlace_allowed = true;
376e67f6037SNeil Armstrong 
377e67f6037SNeil Armstrong 	drm_bridge_add(&meson_encoder_hdmi->bridge);
378e67f6037SNeil Armstrong 
379e67f6037SNeil Armstrong 	meson_encoder_hdmi->priv = priv;
380e67f6037SNeil Armstrong 
381e67f6037SNeil Armstrong 	/* Encoder */
382e67f6037SNeil Armstrong 	ret = drm_simple_encoder_init(priv->drm, &meson_encoder_hdmi->encoder,
383e67f6037SNeil Armstrong 				      DRM_MODE_ENCODER_TMDS);
384e67f6037SNeil Armstrong 	if (ret) {
385e67f6037SNeil Armstrong 		dev_err(priv->dev, "Failed to init HDMI encoder: %d\n", ret);
386e67f6037SNeil Armstrong 		return ret;
387e67f6037SNeil Armstrong 	}
388e67f6037SNeil Armstrong 
389e67f6037SNeil Armstrong 	meson_encoder_hdmi->encoder.possible_crtcs = BIT(0);
390e67f6037SNeil Armstrong 
391e67f6037SNeil Armstrong 	/* Attach HDMI Encoder Bridge to Encoder */
3920af5e0b4SNeil Armstrong 	ret = drm_bridge_attach(&meson_encoder_hdmi->encoder, &meson_encoder_hdmi->bridge, NULL,
3930af5e0b4SNeil Armstrong 				DRM_BRIDGE_ATTACH_NO_CONNECTOR);
394e67f6037SNeil Armstrong 	if (ret) {
395e67f6037SNeil Armstrong 		dev_err(priv->dev, "Failed to attach bridge: %d\n", ret);
396e67f6037SNeil Armstrong 		return ret;
397e67f6037SNeil Armstrong 	}
398e67f6037SNeil Armstrong 
3990af5e0b4SNeil Armstrong 	/* Initialize & attach Bridge Connector */
4000af5e0b4SNeil Armstrong 	meson_encoder_hdmi->connector = drm_bridge_connector_init(priv->drm,
4010af5e0b4SNeil Armstrong 							&meson_encoder_hdmi->encoder);
4020af5e0b4SNeil Armstrong 	if (IS_ERR(meson_encoder_hdmi->connector)) {
4030af5e0b4SNeil Armstrong 		dev_err(priv->dev, "Unable to create HDMI bridge connector\n");
4040af5e0b4SNeil Armstrong 		return PTR_ERR(meson_encoder_hdmi->connector);
4050af5e0b4SNeil Armstrong 	}
4060af5e0b4SNeil Armstrong 	drm_connector_attach_encoder(meson_encoder_hdmi->connector,
4070af5e0b4SNeil Armstrong 				     &meson_encoder_hdmi->encoder);
4080af5e0b4SNeil Armstrong 
409e67f6037SNeil Armstrong 	/*
410e67f6037SNeil Armstrong 	 * We should have now in place:
4110af5e0b4SNeil Armstrong 	 * encoder->[hdmi encoder bridge]->[dw-hdmi bridge]->[display connector bridge]->[display connector]
412e67f6037SNeil Armstrong 	 */
413e67f6037SNeil Armstrong 
4140af5e0b4SNeil Armstrong 	/*
4150af5e0b4SNeil Armstrong 	 * drm_connector_attach_max_bpc_property() requires the
4160af5e0b4SNeil Armstrong 	 * connector to have a state.
4170af5e0b4SNeil Armstrong 	 */
4180af5e0b4SNeil Armstrong 	drm_atomic_helper_connector_reset(meson_encoder_hdmi->connector);
4190af5e0b4SNeil Armstrong 
4200af5e0b4SNeil Armstrong 	if (meson_vpu_is_compatible(priv, VPU_COMPATIBLE_GXL) ||
4210af5e0b4SNeil Armstrong 	    meson_vpu_is_compatible(priv, VPU_COMPATIBLE_GXM) ||
4220af5e0b4SNeil Armstrong 	    meson_vpu_is_compatible(priv, VPU_COMPATIBLE_G12A))
4230af5e0b4SNeil Armstrong 		drm_connector_attach_hdr_output_metadata_property(meson_encoder_hdmi->connector);
4240af5e0b4SNeil Armstrong 
4250af5e0b4SNeil Armstrong 	drm_connector_attach_max_bpc_property(meson_encoder_hdmi->connector, 8, 8);
4260af5e0b4SNeil Armstrong 
4270af5e0b4SNeil Armstrong 	/* Handle this here until handled by drm_bridge_connector_init() */
4280af5e0b4SNeil Armstrong 	meson_encoder_hdmi->connector->ycbcr_420_allowed = true;
4290af5e0b4SNeil Armstrong 
4300af5e0b4SNeil Armstrong 	pdev = of_find_device_by_node(remote);
4310af5e0b4SNeil Armstrong 	if (pdev) {
4320af5e0b4SNeil Armstrong 		struct cec_connector_info conn_info;
4330af5e0b4SNeil Armstrong 		struct cec_notifier *notifier;
4340af5e0b4SNeil Armstrong 
4350af5e0b4SNeil Armstrong 		cec_fill_conn_info_from_drm(&conn_info, meson_encoder_hdmi->connector);
4360af5e0b4SNeil Armstrong 
4370af5e0b4SNeil Armstrong 		notifier = cec_notifier_conn_register(&pdev->dev, NULL, &conn_info);
438*73810768SMiaoqian Lin 		if (!notifier) {
439*73810768SMiaoqian Lin 			put_device(&pdev->dev);
4400af5e0b4SNeil Armstrong 			return -ENOMEM;
441*73810768SMiaoqian Lin 		}
4420af5e0b4SNeil Armstrong 
4430af5e0b4SNeil Armstrong 		meson_encoder_hdmi->cec_notifier = notifier;
4440af5e0b4SNeil Armstrong 	}
4450af5e0b4SNeil Armstrong 
446e67f6037SNeil Armstrong 	dev_dbg(priv->dev, "HDMI encoder initialized\n");
447e67f6037SNeil Armstrong 
448e67f6037SNeil Armstrong 	return 0;
449e67f6037SNeil Armstrong }
450