xref: /OK3568_Linux_fs/kernel/drivers/gpu/drm/bridge/synopsys/dw-hdmi-qp.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) Rockchip Electronics Co.Ltd
4  * Author:
5  *      Algea Cao <algea.cao@rock-chips.com>
6  */
7 #include <linux/clk.h>
8 #include <linux/delay.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/err.h>
11 #include <linux/extcon-provider.h>
12 #include <linux/extcon.h>
13 #include <linux/hdmi.h>
14 #include <linux/irq.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/of_device.h>
18 #include <linux/pinctrl/consumer.h>
19 #include <linux/regmap.h>
20 #include <linux/spinlock.h>
21 
22 #include <drm/drm_atomic.h>
23 #include <drm/drm_atomic_helper.h>
24 #include <drm/drm_crtc_helper.h>
25 #include <drm/drm_dsc.h>
26 #include <drm/drm_edid.h>
27 #include <drm/drm_encoder_slave.h>
28 #include <drm/drm_of.h>
29 #include <drm/drm_panel.h>
30 #include <drm/drm_print.h>
31 #include <drm/drm_probe_helper.h>
32 #include <drm/drm_scdc_helper.h>
33 #include <drm/bridge/dw_hdmi.h>
34 
35 #include <uapi/linux/media-bus-format.h>
36 #include <uapi/linux/videodev2.h>
37 
38 #include "dw-hdmi-qp-audio.h"
39 #include "dw-hdmi-qp.h"
40 #include "dw-hdmi-qp-cec.h"
41 
42 #include <media/cec-notifier.h>
43 
44 #define DDC_CI_ADDR		0x37
45 #define DDC_SEGMENT_ADDR	0x30
46 
47 #define HDMI_EDID_LEN		512
48 
49 /* DW-HDMI Controller >= 0x200a are at least compliant with SCDC version 1 */
50 #define SCDC_MIN_SOURCE_VERSION	0x1
51 
52 #define HDMI14_MAX_TMDSCLK	340000000
53 #define HDMI20_MAX_TMDSCLK_KHZ	600000
54 
55 static const unsigned int dw_hdmi_cable[] = {
56 	EXTCON_DISP_HDMI,
57 	EXTCON_NONE,
58 };
59 
60 /*
61  * Recommended N and Expected CTS Values in FRL Mode in chapter 9.2.2
62  * of HDMI Specification 2.1.
63  */
64 static const struct dw_hdmi_audio_frl_n common_frl_n_table[] = {
65 	{ .r_bit = 3,  .n_32k = 4224, .n_44k1 = 5292, .n_48k = 5760, },
66 	{ .r_bit = 6,  .n_32k = 4032, .n_44k1 = 5292, .n_48k = 6048, },
67 	{ .r_bit = 8,  .n_32k = 4032, .n_44k1 = 3969, .n_48k = 6048, },
68 	{ .r_bit = 10, .n_32k = 3456, .n_44k1 = 3969, .n_48k = 5184, },
69 	{ .r_bit = 12, .n_32k = 3072, .n_44k1 = 3969, .n_48k = 4752, },
70 };
71 
72 /*
73  * Unless otherwise noted, entries in this table are 100% optimization.
74  * Values can be obtained from hdmi_compute_n() but that function is
75  * slow so we pre-compute values we expect to see.
76  *
77  * All 32k and 48k values are expected to be the same (due to the way
78  * the math works) for any rate that's an exact kHz.
79  */
80 static const struct dw_hdmi_audio_tmds_n common_tmds_n_table[] = {
81 	{ .tmds = 25175000, .n_32k = 4096, .n_44k1 = 12854, .n_48k = 6144, },
82 	{ .tmds = 25200000, .n_32k = 4096, .n_44k1 = 5656, .n_48k = 6144, },
83 	{ .tmds = 27000000, .n_32k = 4096, .n_44k1 = 5488, .n_48k = 6144, },
84 	{ .tmds = 28320000, .n_32k = 4096, .n_44k1 = 5586, .n_48k = 6144, },
85 	{ .tmds = 30240000, .n_32k = 4096, .n_44k1 = 5642, .n_48k = 6144, },
86 	{ .tmds = 31500000, .n_32k = 4096, .n_44k1 = 5600, .n_48k = 6144, },
87 	{ .tmds = 32000000, .n_32k = 4096, .n_44k1 = 5733, .n_48k = 6144, },
88 	{ .tmds = 33750000, .n_32k = 4096, .n_44k1 = 6272, .n_48k = 6144, },
89 	{ .tmds = 36000000, .n_32k = 4096, .n_44k1 = 5684, .n_48k = 6144, },
90 	{ .tmds = 40000000, .n_32k = 4096, .n_44k1 = 5733, .n_48k = 6144, },
91 	{ .tmds = 49500000, .n_32k = 4096, .n_44k1 = 5488, .n_48k = 6144, },
92 	{ .tmds = 50000000, .n_32k = 4096, .n_44k1 = 5292, .n_48k = 6144, },
93 	{ .tmds = 54000000, .n_32k = 4096, .n_44k1 = 5684, .n_48k = 6144, },
94 	{ .tmds = 65000000, .n_32k = 4096, .n_44k1 = 7056, .n_48k = 6144, },
95 	{ .tmds = 68250000, .n_32k = 4096, .n_44k1 = 5376, .n_48k = 6144, },
96 	{ .tmds = 71000000, .n_32k = 4096, .n_44k1 = 7056, .n_48k = 6144, },
97 	{ .tmds = 72000000, .n_32k = 4096, .n_44k1 = 5635, .n_48k = 6144, },
98 	{ .tmds = 73250000, .n_32k = 4096, .n_44k1 = 14112, .n_48k = 6144, },
99 	{ .tmds = 74250000, .n_32k = 4096, .n_44k1 = 6272, .n_48k = 6144, },
100 	{ .tmds = 75000000, .n_32k = 4096, .n_44k1 = 5880, .n_48k = 6144, },
101 	{ .tmds = 78750000, .n_32k = 4096, .n_44k1 = 5600, .n_48k = 6144, },
102 	{ .tmds = 78800000, .n_32k = 4096, .n_44k1 = 5292, .n_48k = 6144, },
103 	{ .tmds = 79500000, .n_32k = 4096, .n_44k1 = 4704, .n_48k = 6144, },
104 	{ .tmds = 83500000, .n_32k = 4096, .n_44k1 = 7056, .n_48k = 6144, },
105 	{ .tmds = 85500000, .n_32k = 4096, .n_44k1 = 5488, .n_48k = 6144, },
106 	{ .tmds = 88750000, .n_32k = 4096, .n_44k1 = 14112, .n_48k = 6144, },
107 	{ .tmds = 97750000, .n_32k = 4096, .n_44k1 = 14112, .n_48k = 6144, },
108 	{ .tmds = 101000000, .n_32k = 4096, .n_44k1 = 7056, .n_48k = 6144, },
109 	{ .tmds = 106500000, .n_32k = 4096, .n_44k1 = 4704, .n_48k = 6144, },
110 	{ .tmds = 108000000, .n_32k = 4096, .n_44k1 = 5684, .n_48k = 6144, },
111 	{ .tmds = 115500000, .n_32k = 4096, .n_44k1 = 5712, .n_48k = 6144, },
112 	{ .tmds = 119000000, .n_32k = 4096, .n_44k1 = 5544, .n_48k = 6144, },
113 	{ .tmds = 135000000, .n_32k = 4096, .n_44k1 = 5488, .n_48k = 6144, },
114 	{ .tmds = 146250000, .n_32k = 4096, .n_44k1 = 6272, .n_48k = 6144, },
115 	{ .tmds = 148500000, .n_32k = 4096, .n_44k1 = 5488, .n_48k = 6144, },
116 	{ .tmds = 154000000, .n_32k = 4096, .n_44k1 = 5544, .n_48k = 6144, },
117 	{ .tmds = 162000000, .n_32k = 4096, .n_44k1 = 5684, .n_48k = 6144, },
118 
119 	/* For 297 MHz+ HDMI spec have some other rule for setting N */
120 	{ .tmds = 297000000, .n_32k = 3073, .n_44k1 = 4704, .n_48k = 5120, },
121 	{ .tmds = 594000000, .n_32k = 3073, .n_44k1 = 9408, .n_48k = 10240, },
122 
123 	/* End of table */
124 	{ .tmds = 0,         .n_32k = 0,    .n_44k1 = 0,    .n_48k = 0, },
125 };
126 
127 static const struct drm_display_mode dw_hdmi_default_modes[] = {
128 	/* 16 - 1920x1080@60Hz 16:9 */
129 	{ DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 148500, 1920, 2008,
130 		   2052, 2200, 0, 1080, 1084, 1089, 1125, 0,
131 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
132 	  .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
133 	/* 2 - 720x480@60Hz 4:3 */
134 	{ DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 27000, 720, 736,
135 		   798, 858, 0, 480, 489, 495, 525, 0,
136 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
137 	  .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
138 	/* 4 - 1280x720@60Hz 16:9 */
139 	{ DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 74250, 1280, 1390,
140 		   1430, 1650, 0, 720, 725, 730, 750, 0,
141 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
142 	  .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
143 	/* 31 - 1920x1080@50Hz 16:9 */
144 	{ DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 148500, 1920, 2448,
145 		   2492, 2640, 0, 1080, 1084, 1089, 1125, 0,
146 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
147 	  .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
148 	/* 19 - 1280x720@50Hz 16:9 */
149 	{ DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 74250, 1280, 1720,
150 		   1760, 1980, 0, 720, 725, 730, 750, 0,
151 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
152 	  .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
153 	/* 17 - 720x576@50Hz 4:3 */
154 	{ DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 27000, 720, 732,
155 		   796, 864, 0, 576, 581, 586, 625, 0,
156 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
157 	  .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
158 	/* 2 - 720x480@60Hz 4:3 */
159 	{ DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 27000, 720, 736,
160 		   798, 858, 0, 480, 489, 495, 525, 0,
161 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
162 	  .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
163 };
164 
165 enum frl_mask {
166 	FRL_3GBPS_3LANE = 1,
167 	FRL_6GBPS_3LANE,
168 	FRL_6GBPS_4LANE,
169 	FRL_8GBPS_4LANE,
170 	FRL_10GBPS_4LANE,
171 	FRL_12GBPS_4LANE,
172 };
173 
174 struct hdmi_vmode_qp {
175 	bool mdataenablepolarity;
176 
177 	unsigned int previous_pixelclock;
178 	unsigned long mpixelclock;
179 	unsigned int mpixelrepetitioninput;
180 	unsigned int mpixelrepetitionoutput;
181 	unsigned long previous_tmdsclock;
182 	unsigned int mtmdsclock;
183 };
184 
185 struct hdmi_qp_data_info {
186 	unsigned int enc_in_bus_format;
187 	unsigned int enc_out_bus_format;
188 	unsigned int enc_in_encoding;
189 	unsigned int enc_out_encoding;
190 	unsigned int quant_range;
191 	unsigned int pix_repet_factor;
192 	struct hdmi_vmode_qp video_mode;
193 	bool update;
194 };
195 
196 struct dw_hdmi_qp_i2c {
197 	struct i2c_adapter	adap;
198 
199 	struct mutex		lock;	/* used to serialize data transfers */
200 	struct completion	cmp;
201 	u32			stat;
202 
203 	u8			slave_reg;
204 	bool			is_regaddr;
205 	bool			is_segment;
206 
207 	unsigned int		scl_high_ns;
208 	unsigned int		scl_low_ns;
209 };
210 
211 struct dw_hdmi_phy_data {
212 	enum dw_hdmi_phy_type type;
213 	const char *name;
214 	unsigned int gen;
215 	bool has_svsret;
216 	int (*configure)(struct dw_hdmi_qp *hdmi,
217 			 const struct dw_hdmi_plat_data *pdata,
218 			 unsigned long mpixelclock);
219 };
220 
221 struct dw_hdmi_qp {
222 	struct drm_connector connector;
223 	struct drm_bridge bridge;
224 	struct drm_panel *panel;
225 	struct platform_device *hdcp_dev;
226 	struct platform_device *audio;
227 	struct platform_device *cec;
228 	struct device *dev;
229 	struct dw_hdmi_qp_i2c *i2c;
230 
231 	struct hdmi_qp_data_info hdmi_data;
232 	const struct dw_hdmi_plat_data *plat_data;
233 
234 	int vic;
235 	int main_irq;
236 	int avp_irq;
237 	int earc_irq;
238 
239 	u8 edid[HDMI_EDID_LEN];
240 
241 	struct {
242 		const struct dw_hdmi_qp_phy_ops *ops;
243 		const char *name;
244 		void *data;
245 		bool enabled;
246 	} phy;
247 
248 	struct drm_display_mode previous_mode;
249 
250 	struct i2c_adapter *ddc;
251 	void __iomem *regs;
252 	bool sink_is_hdmi;
253 	bool sink_has_audio;
254 	bool dclk_en;
255 	bool frl_switch;
256 	bool cec_enable;
257 	bool allm_enable;
258 	bool support_hdmi;
259 	int force_output;
260 	int vp_id;
261 	int old_vp_id;
262 
263 	struct mutex mutex;		/* for state below and previous_mode */
264 	struct drm_connector *curr_conn;/* current connector (only valid when !disabled) */
265 	enum drm_connector_force force;	/* mutex-protected force state */
266 	bool disabled;			/* DRM has disabled our bridge */
267 	bool bridge_is_on;		/* indicates the bridge is on */
268 	bool rxsense;			/* rxsense state */
269 	u8 phy_mask;			/* desired phy int mask settings */
270 	u8 mc_clkdis;			/* clock disable register */
271 
272 	bool update;
273 	bool hdr2sdr;
274 
275 	u32 scdc_intr;
276 	u32 flt_intr;
277 	u32 earc_intr;
278 
279 	struct mutex audio_mutex;
280 	unsigned int sample_rate;
281 	unsigned int audio_cts;
282 	unsigned int audio_n;
283 	bool audio_enable;
284 	void (*enable_audio)(struct dw_hdmi_qp *hdmi);
285 	void (*disable_audio)(struct dw_hdmi_qp *hdmi);
286 
287 	struct dentry *debugfs_dir;
288 	bool scramble_low_rates;
289 
290 	struct extcon_dev *extcon;
291 
292 	struct regmap *regm;
293 
294 	bool initialized;		/* hdmi is enabled before bind */
295 	bool logo_plug_out;             /* hdmi is plug out when kernel logo */
296 	struct completion flt_cmp;
297 	struct completion earc_cmp;
298 
299 	struct cec_notifier *cec_notifier;
300 	struct cec_adapter *cec_adap;
301 	struct mutex cec_notifier_mutex;
302 
303 	hdmi_codec_plugged_cb plugged_cb;
304 	struct device *codec_dev;
305 	enum drm_connector_status last_connector_result;
306 };
307 
hdmi_writel(struct dw_hdmi_qp * hdmi,u32 val,int offset)308 static inline void hdmi_writel(struct dw_hdmi_qp *hdmi, u32 val, int offset)
309 {
310 	regmap_write(hdmi->regm, offset, val);
311 }
312 
hdmi_readl(struct dw_hdmi_qp * hdmi,int offset)313 static inline u32 hdmi_readl(struct dw_hdmi_qp *hdmi, int offset)
314 {
315 	unsigned int val = 0;
316 
317 	regmap_read(hdmi->regm, offset, &val);
318 
319 	return val;
320 }
321 
handle_plugged_change(struct dw_hdmi_qp * hdmi,bool plugged)322 static void handle_plugged_change(struct dw_hdmi_qp *hdmi, bool plugged)
323 {
324 	if (hdmi->plugged_cb && hdmi->codec_dev)
325 		hdmi->plugged_cb(hdmi->codec_dev, plugged);
326 }
327 
dw_hdmi_qp_set_plugged_cb(struct dw_hdmi_qp * hdmi,hdmi_codec_plugged_cb fn,struct device * codec_dev)328 int dw_hdmi_qp_set_plugged_cb(struct dw_hdmi_qp *hdmi, hdmi_codec_plugged_cb fn,
329 			      struct device *codec_dev)
330 {
331 	bool plugged;
332 
333 	mutex_lock(&hdmi->mutex);
334 	hdmi->plugged_cb = fn;
335 	hdmi->codec_dev = codec_dev;
336 	plugged = hdmi->last_connector_result == connector_status_connected;
337 	handle_plugged_change(hdmi, plugged);
338 	mutex_unlock(&hdmi->mutex);
339 
340 	return 0;
341 }
342 EXPORT_SYMBOL_GPL(dw_hdmi_qp_set_plugged_cb);
343 
hdmi_modb(struct dw_hdmi_qp * hdmi,u32 data,u32 mask,u32 reg)344 static void hdmi_modb(struct dw_hdmi_qp *hdmi, u32 data, u32 mask, u32 reg)
345 {
346 	regmap_update_bits(hdmi->regm, reg, mask, data);
347 }
348 
hdmi_set_cts_n(struct dw_hdmi_qp * hdmi,unsigned int cts,unsigned int n)349 static void hdmi_set_cts_n(struct dw_hdmi_qp *hdmi, unsigned int cts,
350 			   unsigned int n)
351 {
352 	/* Set N */
353 	hdmi_modb(hdmi, n, AUDPKT_ACR_N_VALUE, AUDPKT_ACR_CONTROL0);
354 
355 	/* Set CTS */
356 	if (cts)
357 		hdmi_modb(hdmi, AUDPKT_ACR_CTS_OVR_EN, AUDPKT_ACR_CTS_OVR_EN_MSK,
358 			  AUDPKT_ACR_CONTROL1);
359 	else
360 		hdmi_modb(hdmi, 0, AUDPKT_ACR_CTS_OVR_EN_MSK,
361 			  AUDPKT_ACR_CONTROL1);
362 
363 	hdmi_modb(hdmi, AUDPKT_ACR_CTS_OVR_VAL(cts), AUDPKT_ACR_CTS_OVR_VAL_MSK,
364 		  AUDPKT_ACR_CONTROL1);
365 }
366 
hdmi_match_frl_n_table(struct dw_hdmi_qp * hdmi,unsigned long r_bit,unsigned long freq)367 static int hdmi_match_frl_n_table(struct dw_hdmi_qp *hdmi,
368 				  unsigned long r_bit,
369 				  unsigned long freq)
370 {
371 	const struct dw_hdmi_audio_frl_n *frl_n = NULL;
372 	int i = 0, n = 0;
373 
374 	for (i = 0; ARRAY_SIZE(common_frl_n_table); i++) {
375 		if (r_bit == common_frl_n_table[i].r_bit) {
376 			frl_n = &common_frl_n_table[i];
377 			break;
378 		}
379 	}
380 
381 	if (!frl_n)
382 		goto err;
383 
384 	switch (freq) {
385 	case 32000:
386 	case 64000:
387 	case 128000:
388 		n = (freq / 32000) * frl_n->n_32k;
389 		break;
390 	case 44100:
391 	case 88200:
392 	case 176400:
393 		n = (freq / 44100) * frl_n->n_44k1;
394 		break;
395 	case 48000:
396 	case 96000:
397 	case 192000:
398 		n = (freq / 48000) * frl_n->n_48k;
399 		break;
400 	default:
401 		goto err;
402 	}
403 
404 	return n;
405 err:
406 	dev_err(hdmi->dev, "FRL; unexpected Rbit: %lu Gbps\n", r_bit);
407 
408 	return 0;
409 }
410 
hdmi_match_tmds_n_table(struct dw_hdmi_qp * hdmi,unsigned long pixel_clk,unsigned long freq)411 static int hdmi_match_tmds_n_table(struct dw_hdmi_qp *hdmi,
412 				   unsigned long pixel_clk,
413 				   unsigned long freq)
414 {
415 	const struct dw_hdmi_plat_data *plat_data = hdmi->plat_data;
416 	const struct dw_hdmi_audio_tmds_n *tmds_n = NULL;
417 	int i;
418 
419 	if (plat_data->tmds_n_table) {
420 		for (i = 0; plat_data->tmds_n_table[i].tmds != 0; i++) {
421 			if (pixel_clk == plat_data->tmds_n_table[i].tmds) {
422 				tmds_n = &plat_data->tmds_n_table[i];
423 				break;
424 			}
425 		}
426 	}
427 
428 	if (tmds_n == NULL) {
429 		for (i = 0; common_tmds_n_table[i].tmds != 0; i++) {
430 			if (pixel_clk == common_tmds_n_table[i].tmds) {
431 				tmds_n = &common_tmds_n_table[i];
432 				break;
433 			}
434 		}
435 	}
436 
437 	if (tmds_n == NULL)
438 		return -ENOENT;
439 
440 	switch (freq) {
441 	case 32000:
442 		return tmds_n->n_32k;
443 	case 44100:
444 	case 88200:
445 	case 176400:
446 		return (freq / 44100) * tmds_n->n_44k1;
447 	case 48000:
448 	case 96000:
449 	case 192000:
450 		return (freq / 48000) * tmds_n->n_48k;
451 	default:
452 		return -ENOENT;
453 	}
454 }
455 
hdmi_audio_math_diff(unsigned int freq,unsigned int n,unsigned int pixel_clk)456 static u64 hdmi_audio_math_diff(unsigned int freq, unsigned int n,
457 				unsigned int pixel_clk)
458 {
459 	u64 final, diff;
460 	u64 cts;
461 
462 	final = (u64)pixel_clk * n;
463 
464 	cts = final;
465 	do_div(cts, 128 * freq);
466 
467 	diff = final - (u64)cts * (128 * freq);
468 
469 	return diff;
470 }
471 
hdmi_compute_n(struct dw_hdmi_qp * hdmi,unsigned long pixel_clk,unsigned long freq)472 static unsigned int hdmi_compute_n(struct dw_hdmi_qp *hdmi,
473 				   unsigned long pixel_clk,
474 				   unsigned long freq)
475 {
476 	unsigned int min_n = DIV_ROUND_UP((128 * freq), 1500);
477 	unsigned int max_n = (128 * freq) / 300;
478 	unsigned int ideal_n = (128 * freq) / 1000;
479 	unsigned int best_n_distance = ideal_n;
480 	unsigned int best_n = 0;
481 	u64 best_diff = U64_MAX;
482 	int n;
483 
484 	/* If the ideal N could satisfy the audio math, then just take it */
485 	if (hdmi_audio_math_diff(freq, ideal_n, pixel_clk) == 0)
486 		return ideal_n;
487 
488 	for (n = min_n; n <= max_n; n++) {
489 		u64 diff = hdmi_audio_math_diff(freq, n, pixel_clk);
490 
491 		if (diff < best_diff || (diff == best_diff &&
492 		    abs(n - ideal_n) < best_n_distance)) {
493 			best_n = n;
494 			best_diff = diff;
495 			best_n_distance = abs(best_n - ideal_n);
496 		}
497 
498 		/*
499 		 * The best N already satisfy the audio math, and also be
500 		 * the closest value to ideal N, so just cut the loop.
501 		 */
502 		if ((best_diff == 0) && (abs(n - ideal_n) > best_n_distance))
503 			break;
504 	}
505 
506 	return best_n;
507 }
508 
hdmi_find_n(struct dw_hdmi_qp * hdmi,unsigned long pixel_clk,unsigned long sample_rate)509 static unsigned int hdmi_find_n(struct dw_hdmi_qp *hdmi, unsigned long pixel_clk,
510 				unsigned long sample_rate)
511 {
512 	struct dw_hdmi_link_config *link_cfg = NULL;
513 	void *data = hdmi->plat_data->phy_data;
514 	int n;
515 
516 	if (hdmi->plat_data->get_link_cfg) {
517 		link_cfg = hdmi->plat_data->get_link_cfg(data);
518 		if (link_cfg && link_cfg->frl_mode)
519 			return hdmi_match_frl_n_table(hdmi, link_cfg->rate_per_lane, sample_rate);
520 	}
521 
522 	n = hdmi_match_tmds_n_table(hdmi, pixel_clk, sample_rate);
523 	if (n > 0)
524 		return n;
525 
526 	dev_warn(hdmi->dev, "Rate %lu missing; compute N dynamically\n",
527 		 pixel_clk);
528 
529 	return hdmi_compute_n(hdmi, pixel_clk, sample_rate);
530 }
531 
dw_hdmi_qp_set_audio_interface(struct dw_hdmi_qp * hdmi,struct hdmi_codec_daifmt * fmt,struct hdmi_codec_params * hparms)532 void dw_hdmi_qp_set_audio_interface(struct dw_hdmi_qp *hdmi,
533 				    struct hdmi_codec_daifmt *fmt,
534 				    struct hdmi_codec_params *hparms)
535 {
536 	u32 conf0 = 0;
537 
538 	mutex_lock(&hdmi->audio_mutex);
539 	if (!hdmi->dclk_en) {
540 		mutex_unlock(&hdmi->audio_mutex);
541 		return;
542 	}
543 
544 	/* Reset the audio data path of the AVP */
545 	hdmi_writel(hdmi, AVP_DATAPATH_PACKET_AUDIO_SWINIT_P, GLOBAL_SWRESET_REQUEST);
546 
547 	/* Disable AUDS, ACR, AUDI */
548 	hdmi_modb(hdmi, 0,
549 		  PKTSCHED_ACR_TX_EN | PKTSCHED_AUDS_TX_EN | PKTSCHED_AUDI_TX_EN,
550 		  PKTSCHED_PKT_EN);
551 
552 	/* Clear the audio FIFO */
553 	hdmi_writel(hdmi, AUDIO_FIFO_CLR_P, AUDIO_INTERFACE_CONTROL0);
554 
555 	/* Select I2S interface as the audio source */
556 	hdmi_modb(hdmi, AUD_IF_I2S, AUD_IF_SEL_MSK, AUDIO_INTERFACE_CONFIG0);
557 
558 	/* Enable the active i2s lanes */
559 	switch (hparms->channels) {
560 	case 7 ... 8:
561 		conf0 |= I2S_LINES_EN(3);
562 		fallthrough;
563 	case 5 ... 6:
564 		conf0 |= I2S_LINES_EN(2);
565 		fallthrough;
566 	case 3 ... 4:
567 		conf0 |= I2S_LINES_EN(1);
568 		fallthrough;
569 	default:
570 		conf0 |= I2S_LINES_EN(0);
571 		break;
572 	}
573 
574 	hdmi_modb(hdmi, conf0, I2S_LINES_EN_MSK, AUDIO_INTERFACE_CONFIG0);
575 
576 	/*
577 	 * Enable bpcuv generated internally for L-PCM, or received
578 	 * from stream for NLPCM/HBR.
579 	 */
580 	switch (fmt->bit_fmt) {
581 	case SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE:
582 		conf0 = (hparms->channels == 8) ? AUD_HBR : AUD_ASP;
583 		conf0 |= I2S_BPCUV_RCV_EN;
584 		break;
585 	default:
586 		conf0 = AUD_ASP | I2S_BPCUV_RCV_DIS;
587 		break;
588 	}
589 
590 	hdmi_modb(hdmi, conf0, I2S_BPCUV_RCV_MSK | AUD_FORMAT_MSK,
591 		  AUDIO_INTERFACE_CONFIG0);
592 
593 	/* Enable audio FIFO auto clear when overflow */
594 	hdmi_modb(hdmi, AUD_FIFO_INIT_ON_OVF_EN, AUD_FIFO_INIT_ON_OVF_MSK,
595 		  AUDIO_INTERFACE_CONFIG0);
596 
597 	mutex_unlock(&hdmi->audio_mutex);
598 }
599 EXPORT_SYMBOL_GPL(dw_hdmi_qp_set_audio_interface);
600 
601 /*
602  * When transmitting IEC60958 linear PCM audio, these registers allow to
603  * configure the channel status information of all the channel status
604  * bits in the IEC60958 frame. For the moment this configuration is only
605  * used when the I2S audio interface, General Purpose Audio (GPA),
606  * or AHB audio DMA (AHBAUDDMA) interface is active
607  * (for S/PDIF interface this information comes from the stream).
608  */
dw_hdmi_qp_set_channel_status(struct dw_hdmi_qp * hdmi,u8 * channel_status,bool ref2stream)609 void dw_hdmi_qp_set_channel_status(struct dw_hdmi_qp *hdmi,
610 				   u8 *channel_status, bool ref2stream)
611 {
612 	mutex_lock(&hdmi->audio_mutex);
613 	if (!hdmi->dclk_en) {
614 		mutex_unlock(&hdmi->audio_mutex);
615 		return;
616 	}
617 
618 	/*
619 	 * AUDPKT_CHSTATUS_OVR0: { RSV, RSV, CS1, CS0 }
620 	 * AUDPKT_CHSTATUS_OVR1: { CS6, CS5, CS4, CS3 }
621 	 *
622 	 *      |  7  |  6  |  5  |  4  |  3  |  2  |  1  |  0  |
623 	 * CS0: |   Mode    |        d        |  c  |  b  |  a  |
624 	 * CS1: |               Category Code                   |
625 	 * CS2: |    Channel Number     |     Source Number     |
626 	 * CS3: |    Clock Accuracy     |     Sample Freq       |
627 	 * CS4: |    Ori Sample Freq    |     Word Length       |
628 	 * CS5: |                                   |   CGMS-A  |
629 	 * CS6~CS23: Reserved
630 	 *
631 	 * a: use of channel status block
632 	 * b: linear PCM identification: 0 for lpcm, 1 for nlpcm
633 	 * c: copyright information
634 	 * d: additional format information
635 	 */
636 
637 	if (ref2stream)
638 		channel_status[0] |= IEC958_AES0_NONAUDIO;
639 
640 	if ((hdmi_readl(hdmi, AUDIO_INTERFACE_CONFIG0) & GENMASK(25, 24)) == AUD_HBR) {
641 		/* fixup cs for HBR */
642 		channel_status[3] = (channel_status[3] & 0xf0) | IEC958_AES3_CON_FS_768000;
643 		channel_status[4] = (channel_status[4] & 0x0f) | IEC958_AES4_CON_ORIGFS_NOTID;
644 	}
645 
646 	hdmi_writel(hdmi, channel_status[0] | (channel_status[1] << 8),
647 		    AUDPKT_CHSTATUS_OVR0);
648 
649 	regmap_bulk_write(hdmi->regm, AUDPKT_CHSTATUS_OVR1, &channel_status[3], 1);
650 
651 	if (ref2stream)
652 		hdmi_modb(hdmi, 0,
653 			  AUDPKT_PBIT_FORCE_EN_MASK | AUDPKT_CHSTATUS_OVR_EN_MASK,
654 			  AUDPKT_CONTROL0);
655 	else
656 		hdmi_modb(hdmi, AUDPKT_PBIT_FORCE_EN | AUDPKT_CHSTATUS_OVR_EN,
657 			  AUDPKT_PBIT_FORCE_EN_MASK | AUDPKT_CHSTATUS_OVR_EN_MASK,
658 			  AUDPKT_CONTROL0);
659 
660 	mutex_unlock(&hdmi->audio_mutex);
661 }
662 EXPORT_SYMBOL_GPL(dw_hdmi_qp_set_channel_status);
663 
hdmi_set_clk_regenerator(struct dw_hdmi_qp * hdmi,unsigned long pixel_clk,unsigned int sample_rate)664 static void hdmi_set_clk_regenerator(struct dw_hdmi_qp *hdmi,
665 	unsigned long pixel_clk, unsigned int sample_rate)
666 {
667 	unsigned int n = 0, cts = 0;
668 
669 	n = hdmi_find_n(hdmi, pixel_clk, sample_rate);
670 
671 	hdmi->audio_n = n;
672 	hdmi->audio_cts = cts;
673 	hdmi_set_cts_n(hdmi, cts, hdmi->audio_enable ? n : 0);
674 }
675 
hdmi_init_clk_regenerator(struct dw_hdmi_qp * hdmi)676 static void hdmi_init_clk_regenerator(struct dw_hdmi_qp *hdmi)
677 {
678 	mutex_lock(&hdmi->audio_mutex);
679 	if (hdmi->dclk_en)
680 		hdmi_set_clk_regenerator(hdmi, 74250000, hdmi->sample_rate);
681 	mutex_unlock(&hdmi->audio_mutex);
682 }
683 
hdmi_clk_regenerator_update_pixel_clock(struct dw_hdmi_qp * hdmi)684 static void hdmi_clk_regenerator_update_pixel_clock(struct dw_hdmi_qp *hdmi)
685 {
686 	mutex_lock(&hdmi->audio_mutex);
687 	if (hdmi->dclk_en)
688 		hdmi_set_clk_regenerator(hdmi, hdmi->hdmi_data.video_mode.mtmdsclock,
689 					 hdmi->sample_rate);
690 	mutex_unlock(&hdmi->audio_mutex);
691 }
692 
dw_hdmi_qp_set_sample_rate(struct dw_hdmi_qp * hdmi,unsigned int rate)693 void dw_hdmi_qp_set_sample_rate(struct dw_hdmi_qp *hdmi, unsigned int rate)
694 {
695 	mutex_lock(&hdmi->audio_mutex);
696 	if (hdmi->dclk_en) {
697 		hdmi->sample_rate = rate;
698 		hdmi_set_clk_regenerator(hdmi, hdmi->hdmi_data.video_mode.mtmdsclock,
699 					 hdmi->sample_rate);
700 	}
701 	mutex_unlock(&hdmi->audio_mutex);
702 }
703 EXPORT_SYMBOL_GPL(dw_hdmi_qp_set_sample_rate);
704 
dw_hdmi_qp_set_channel_count(struct dw_hdmi_qp * hdmi,unsigned int cnt)705 void dw_hdmi_qp_set_channel_count(struct dw_hdmi_qp *hdmi, unsigned int cnt)
706 {
707 }
708 EXPORT_SYMBOL_GPL(dw_hdmi_qp_set_channel_count);
709 
dw_hdmi_qp_set_channel_allocation(struct dw_hdmi_qp * hdmi,unsigned int ca)710 void dw_hdmi_qp_set_channel_allocation(struct dw_hdmi_qp *hdmi, unsigned int ca)
711 {
712 }
713 EXPORT_SYMBOL_GPL(dw_hdmi_qp_set_channel_allocation);
714 
dw_hdmi_qp_init_audio_infoframe(struct dw_hdmi_qp * hdmi)715 static int dw_hdmi_qp_init_audio_infoframe(struct dw_hdmi_qp *hdmi)
716 {
717 	struct hdmi_audio_infoframe frame;
718 	u8 infoframe_buf[HDMI_INFOFRAME_SIZE(AUDIO)];
719 	int ret = 0;
720 
721 	hdmi_audio_infoframe_init(&frame);
722 
723 	frame.coding_type = HDMI_AUDIO_CODING_TYPE_STREAM;
724 	frame.sample_frequency = HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM;
725 	frame.sample_size = HDMI_AUDIO_SAMPLE_SIZE_STREAM;
726 	frame.channels = 2;
727 
728 	ret = hdmi_audio_infoframe_pack(&frame, infoframe_buf,
729 					sizeof(infoframe_buf));
730 	if (ret < 0) {
731 		dev_err(hdmi->dev, "%s: Failed to pack audio infoframe: %d\n",
732 			__func__, ret);
733 		return ret;
734 	}
735 
736 	regmap_bulk_write(hdmi->regm, PKT_AUDI_CONTENTS1, &infoframe_buf[3], 2);
737 	hdmi_modb(hdmi,
738 		  PKTSCHED_ACR_TX_EN | PKTSCHED_AUDI_TX_EN,
739 		  PKTSCHED_ACR_TX_EN | PKTSCHED_AUDI_TX_EN,
740 		  PKTSCHED_PKT_EN);
741 
742 	return 0;
743 }
744 
dw_hdmi_qp_set_audio_infoframe(struct dw_hdmi_qp * hdmi,struct hdmi_codec_params * hparms)745 void dw_hdmi_qp_set_audio_infoframe(struct dw_hdmi_qp *hdmi,
746 				    struct hdmi_codec_params *hparms)
747 {
748 	u8 infoframe_buf[HDMI_INFOFRAME_SIZE(AUDIO)];
749 	int ret = 0;
750 
751 	ret = hdmi_audio_infoframe_pack(&hparms->cea, infoframe_buf,
752 					sizeof(infoframe_buf));
753 	if (!ret) {
754 		dev_err(hdmi->dev, "%s: Failed to pack audio infoframe: %d\n",
755 			__func__, ret);
756 		return;
757 	}
758 
759 	mutex_lock(&hdmi->audio_mutex);
760 	if (!hdmi->dclk_en) {
761 		mutex_unlock(&hdmi->audio_mutex);
762 		return;
763 	}
764 
765 	/*
766 	 * AUDI_CONTENTS0: { RSV, HB2, HB1, RSV }
767 	 * AUDI_CONTENTS1: { PB3, PB2, PB1, PB0 }
768 	 * AUDI_CONTENTS2: { PB7, PB6, PB5, PB4 }
769 	 *
770 	 * PB0: CheckSum
771 	 * PB1: | CT3    | CT2  | CT1  | CT0  | F13  | CC2 | CC1 | CC0 |
772 	 * PB2: | F27    | F26  | F25  | SF2  | SF1  | SF0 | SS1 | SS0 |
773 	 * PB3: | F37    | F36  | F35  | F34  | F33  | F32 | F31 | F30 |
774 	 * PB4: | CA7    | CA6  | CA5  | CA4  | CA3  | CA2 | CA1 | CA0 |
775 	 * PB5: | DM_INH | LSV3 | LSV2 | LSV1 | LSV0 | F52 | F51 | F50 |
776 	 * PB6~PB10: Reserved
777 	 *
778 	 * AUDI_CONTENTS0 default value defined by HDMI specification,
779 	 * and shall only be changed for debug purposes.
780 	 * So, we only configure payload byte from PB0~PB7(2 word total).
781 	 */
782 	regmap_bulk_write(hdmi->regm, PKT_AUDI_CONTENTS1, &infoframe_buf[3], 2);
783 
784 	/* Enable ACR, AUDI */
785 	hdmi_modb(hdmi, PKTSCHED_ACR_TX_EN | PKTSCHED_AUDI_TX_EN,
786 		  PKTSCHED_ACR_TX_EN | PKTSCHED_AUDI_TX_EN,
787 		  PKTSCHED_PKT_EN);
788 
789 	/* Enable AUDS */
790 	hdmi_modb(hdmi, PKTSCHED_AUDS_TX_EN, PKTSCHED_AUDS_TX_EN, PKTSCHED_PKT_EN);
791 	mutex_unlock(&hdmi->audio_mutex);
792 }
793 EXPORT_SYMBOL_GPL(dw_hdmi_qp_set_audio_infoframe);
794 
hdmi_enable_audio_clk(struct dw_hdmi_qp * hdmi,bool enable)795 static void hdmi_enable_audio_clk(struct dw_hdmi_qp *hdmi, bool enable)
796 {
797 	if (enable)
798 		hdmi_modb(hdmi, 0,
799 			  AVP_DATAPATH_PACKET_AUDIO_SWDISABLE, GLOBAL_SWDISABLE);
800 	else
801 		hdmi_modb(hdmi, AVP_DATAPATH_PACKET_AUDIO_SWDISABLE,
802 			  AVP_DATAPATH_PACKET_AUDIO_SWDISABLE, GLOBAL_SWDISABLE);
803 }
804 
dw_hdmi_i2s_audio_enable(struct dw_hdmi_qp * hdmi)805 static void dw_hdmi_i2s_audio_enable(struct dw_hdmi_qp *hdmi)
806 {
807 	hdmi_set_cts_n(hdmi, hdmi->audio_cts, hdmi->audio_n);
808 	hdmi_enable_audio_clk(hdmi, true);
809 }
810 
dw_hdmi_i2s_audio_disable(struct dw_hdmi_qp * hdmi)811 static void dw_hdmi_i2s_audio_disable(struct dw_hdmi_qp *hdmi)
812 {
813 	/*
814 	 * Keep ACR, AUDI, AUDS packet always on to make SINK device
815 	 * active for better compatibility and user experience.
816 	 *
817 	 * This also fix POP sound on some SINK devices which wakeup
818 	 * from suspend to active.
819 	 */
820 	hdmi_modb(hdmi, I2S_BPCUV_RCV_DIS, I2S_BPCUV_RCV_MSK,
821 		  AUDIO_INTERFACE_CONFIG0);
822 	hdmi_modb(hdmi, AUDPKT_PBIT_FORCE_EN | AUDPKT_CHSTATUS_OVR_EN,
823 		  AUDPKT_PBIT_FORCE_EN_MASK | AUDPKT_CHSTATUS_OVR_EN_MASK,
824 		  AUDPKT_CONTROL0);
825 }
826 
dw_hdmi_qp_audio_enable(struct dw_hdmi_qp * hdmi)827 void dw_hdmi_qp_audio_enable(struct dw_hdmi_qp *hdmi)
828 {
829 	mutex_lock(&hdmi->audio_mutex);
830 	if (hdmi->dclk_en) {
831 		hdmi->audio_enable = true;
832 		if (hdmi->enable_audio)
833 			hdmi->enable_audio(hdmi);
834 	}
835 	mutex_unlock(&hdmi->audio_mutex);
836 }
837 EXPORT_SYMBOL_GPL(dw_hdmi_qp_audio_enable);
838 
dw_hdmi_qp_audio_disable(struct dw_hdmi_qp * hdmi)839 void dw_hdmi_qp_audio_disable(struct dw_hdmi_qp *hdmi)
840 {
841 	mutex_lock(&hdmi->audio_mutex);
842 	if (hdmi->dclk_en) {
843 		hdmi->audio_enable = false;
844 		if (hdmi->disable_audio)
845 			hdmi->disable_audio(hdmi);
846 	}
847 	mutex_unlock(&hdmi->audio_mutex);
848 }
849 EXPORT_SYMBOL_GPL(dw_hdmi_qp_audio_disable);
850 
hdmi_bus_fmt_is_rgb(unsigned int bus_format)851 static bool hdmi_bus_fmt_is_rgb(unsigned int bus_format)
852 {
853 	switch (bus_format) {
854 	case MEDIA_BUS_FMT_RGB888_1X24:
855 	case MEDIA_BUS_FMT_RGB101010_1X30:
856 	case MEDIA_BUS_FMT_RGB121212_1X36:
857 	case MEDIA_BUS_FMT_RGB161616_1X48:
858 		return true;
859 
860 	default:
861 		return false;
862 	}
863 }
864 
hdmi_bus_fmt_is_yuv444(unsigned int bus_format)865 static bool hdmi_bus_fmt_is_yuv444(unsigned int bus_format)
866 {
867 	switch (bus_format) {
868 	case MEDIA_BUS_FMT_YUV8_1X24:
869 	case MEDIA_BUS_FMT_YUV10_1X30:
870 	case MEDIA_BUS_FMT_YUV12_1X36:
871 	case MEDIA_BUS_FMT_YUV16_1X48:
872 		return true;
873 
874 	default:
875 		return false;
876 	}
877 }
878 
hdmi_bus_fmt_is_yuv422(unsigned int bus_format)879 static bool hdmi_bus_fmt_is_yuv422(unsigned int bus_format)
880 {
881 	switch (bus_format) {
882 	case MEDIA_BUS_FMT_UYVY8_1X16:
883 	case MEDIA_BUS_FMT_UYVY10_1X20:
884 	case MEDIA_BUS_FMT_UYVY12_1X24:
885 	case MEDIA_BUS_FMT_YUYV8_1X16:
886 	case MEDIA_BUS_FMT_YUYV10_1X20:
887 	case MEDIA_BUS_FMT_YUYV12_1X24:
888 		return true;
889 
890 	default:
891 		return false;
892 	}
893 }
894 
hdmi_bus_fmt_is_yuv420(unsigned int bus_format)895 static bool hdmi_bus_fmt_is_yuv420(unsigned int bus_format)
896 {
897 	switch (bus_format) {
898 	case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
899 	case MEDIA_BUS_FMT_UYYVYY10_0_5X30:
900 	case MEDIA_BUS_FMT_UYYVYY12_0_5X36:
901 	case MEDIA_BUS_FMT_UYYVYY16_0_5X48:
902 		return true;
903 
904 	default:
905 		return false;
906 	}
907 }
908 
hdmi_bus_fmt_color_depth(unsigned int bus_format)909 static int hdmi_bus_fmt_color_depth(unsigned int bus_format)
910 {
911 	switch (bus_format) {
912 	case MEDIA_BUS_FMT_RGB888_1X24:
913 	case MEDIA_BUS_FMT_YUV8_1X24:
914 	case MEDIA_BUS_FMT_UYVY8_1X16:
915 	case MEDIA_BUS_FMT_YUYV8_1X16:
916 	case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
917 		return 8;
918 
919 	case MEDIA_BUS_FMT_RGB101010_1X30:
920 	case MEDIA_BUS_FMT_YUV10_1X30:
921 	case MEDIA_BUS_FMT_UYVY10_1X20:
922 	case MEDIA_BUS_FMT_YUYV10_1X20:
923 	case MEDIA_BUS_FMT_UYYVYY10_0_5X30:
924 		return 10;
925 
926 	case MEDIA_BUS_FMT_RGB121212_1X36:
927 	case MEDIA_BUS_FMT_YUV12_1X36:
928 	case MEDIA_BUS_FMT_UYVY12_1X24:
929 	case MEDIA_BUS_FMT_YUYV12_1X24:
930 	case MEDIA_BUS_FMT_UYYVYY12_0_5X36:
931 		return 12;
932 
933 	case MEDIA_BUS_FMT_RGB161616_1X48:
934 	case MEDIA_BUS_FMT_YUV16_1X48:
935 	case MEDIA_BUS_FMT_UYYVYY16_0_5X48:
936 		return 16;
937 
938 	default:
939 		return 0;
940 	}
941 }
942 
dw_hdmi_i2c_init(struct dw_hdmi_qp * hdmi)943 static void dw_hdmi_i2c_init(struct dw_hdmi_qp *hdmi)
944 {
945 	/* Software reset */
946 	hdmi_writel(hdmi, 0x01, I2CM_CONTROL0);
947 
948 	hdmi_writel(hdmi, 0x085c085c, I2CM_FM_SCL_CONFIG0);
949 
950 	hdmi_modb(hdmi, 0, I2CM_FM_EN, I2CM_INTERFACE_CONTROL0);
951 
952 	/* Clear DONE and ERROR interrupts */
953 	hdmi_writel(hdmi, I2CM_OP_DONE_CLEAR | I2CM_NACK_RCVD_CLEAR,
954 		    MAINUNIT_1_INT_CLEAR);
955 }
956 
dw_hdmi_i2c_read(struct dw_hdmi_qp * hdmi,unsigned char * buf,unsigned int length)957 static int dw_hdmi_i2c_read(struct dw_hdmi_qp *hdmi,
958 			    unsigned char *buf, unsigned int length)
959 {
960 	struct dw_hdmi_qp_i2c *i2c = hdmi->i2c;
961 	int stat;
962 
963 	if (!i2c->is_regaddr) {
964 		dev_dbg(hdmi->dev, "set read register address to 0\n");
965 		i2c->slave_reg = 0x00;
966 		i2c->is_regaddr = true;
967 	}
968 
969 	while (length--) {
970 		reinit_completion(&i2c->cmp);
971 
972 		hdmi_modb(hdmi, i2c->slave_reg++ << 12, I2CM_ADDR,
973 			  I2CM_INTERFACE_CONTROL0);
974 
975 		if (i2c->is_segment)
976 			hdmi_modb(hdmi, I2CM_EXT_READ, I2CM_WR_MASK,
977 				  I2CM_INTERFACE_CONTROL0);
978 		else
979 			hdmi_modb(hdmi, I2CM_FM_READ, I2CM_WR_MASK,
980 				  I2CM_INTERFACE_CONTROL0);
981 
982 		stat = wait_for_completion_timeout(&i2c->cmp, HZ / 10);
983 		if (!stat) {
984 			dev_err(hdmi->dev, "i2c read time out!\n");
985 			hdmi_writel(hdmi, 0x01, I2CM_CONTROL0);
986 			return -EAGAIN;
987 		}
988 
989 		/* Check for error condition on the bus */
990 		if (i2c->stat & I2CM_NACK_RCVD_IRQ) {
991 			dev_err(hdmi->dev, "i2c read err!\n");
992 			hdmi_writel(hdmi, 0x01, I2CM_CONTROL0);
993 			return -EIO;
994 		}
995 
996 		*buf++ = hdmi_readl(hdmi, I2CM_INTERFACE_RDDATA_0_3) & 0xff;
997 		dev_dbg(hdmi->dev, "i2c read done! i2c->stat:%02x 0x%02x\n",
998 			i2c->stat, hdmi_readl(hdmi, I2CM_INTERFACE_RDDATA_0_3));
999 		hdmi_modb(hdmi, 0, I2CM_WR_MASK, I2CM_INTERFACE_CONTROL0);
1000 	}
1001 	i2c->is_segment = false;
1002 
1003 	return 0;
1004 }
1005 
dw_hdmi_i2c_write(struct dw_hdmi_qp * hdmi,unsigned char * buf,unsigned int length)1006 static int dw_hdmi_i2c_write(struct dw_hdmi_qp *hdmi,
1007 			     unsigned char *buf, unsigned int length)
1008 {
1009 	struct dw_hdmi_qp_i2c *i2c = hdmi->i2c;
1010 	int stat;
1011 
1012 	if (!i2c->is_regaddr) {
1013 		/* Use the first write byte as register address */
1014 		i2c->slave_reg = buf[0];
1015 		length--;
1016 		buf++;
1017 		i2c->is_regaddr = true;
1018 	}
1019 
1020 	while (length--) {
1021 		reinit_completion(&i2c->cmp);
1022 
1023 		hdmi_writel(hdmi, *buf++, I2CM_INTERFACE_WRDATA_0_3);
1024 		hdmi_modb(hdmi, i2c->slave_reg++ << 12, I2CM_ADDR,
1025 			  I2CM_INTERFACE_CONTROL0);
1026 		hdmi_modb(hdmi, I2CM_FM_WRITE, I2CM_WR_MASK,
1027 			  I2CM_INTERFACE_CONTROL0);
1028 
1029 		stat = wait_for_completion_timeout(&i2c->cmp, HZ / 10);
1030 		if (!stat) {
1031 			dev_err(hdmi->dev, "i2c write time out!\n");
1032 			hdmi_writel(hdmi, 0x01, I2CM_CONTROL0);
1033 			return -EAGAIN;
1034 		}
1035 
1036 		/* Check for error condition on the bus */
1037 		if (i2c->stat & I2CM_NACK_RCVD_IRQ) {
1038 			dev_err(hdmi->dev, "i2c write nack!\n");
1039 			hdmi_writel(hdmi, 0x01, I2CM_CONTROL0);
1040 			return -EIO;
1041 		}
1042 		hdmi_modb(hdmi, 0, I2CM_WR_MASK, I2CM_INTERFACE_CONTROL0);
1043 	}
1044 	dev_dbg(hdmi->dev, "i2c write done!\n");
1045 	return 0;
1046 }
1047 
dw_hdmi_i2c_xfer(struct i2c_adapter * adap,struct i2c_msg * msgs,int num)1048 static int dw_hdmi_i2c_xfer(struct i2c_adapter *adap,
1049 			    struct i2c_msg *msgs, int num)
1050 {
1051 	struct dw_hdmi_qp *hdmi = i2c_get_adapdata(adap);
1052 	struct dw_hdmi_qp_i2c *i2c = hdmi->i2c;
1053 	u8 addr = msgs[0].addr;
1054 	int i, ret = 0;
1055 
1056 	if (addr == DDC_CI_ADDR)
1057 		/*
1058 		 * The internal I2C controller does not support the multi-byte
1059 		 * read and write operations needed for DDC/CI.
1060 		 * TOFIX: Blacklist the DDC/CI address until we filter out
1061 		 * unsupported I2C operations.
1062 		 */
1063 		return -EOPNOTSUPP;
1064 
1065 	dev_dbg(hdmi->dev, "i2c xfer: num: %d, addr: %#x\n", num, addr);
1066 
1067 	for (i = 0; i < num; i++) {
1068 		if (msgs[i].len == 0) {
1069 			dev_err(hdmi->dev,
1070 				"unsupported transfer %d/%d, no data\n",
1071 				i + 1, num);
1072 			return -EOPNOTSUPP;
1073 		}
1074 	}
1075 
1076 	mutex_lock(&i2c->lock);
1077 
1078 	/* Unmute DONE and ERROR interrupts */
1079 	hdmi_modb(hdmi, I2CM_NACK_RCVD_MASK_N | I2CM_OP_DONE_MASK_N,
1080 		  I2CM_NACK_RCVD_MASK_N | I2CM_OP_DONE_MASK_N,
1081 		  MAINUNIT_1_INT_MASK_N);
1082 
1083 	/* Set slave device address taken from the first I2C message */
1084 	if (addr == DDC_SEGMENT_ADDR && msgs[0].len == 1)
1085 		addr = DDC_ADDR;
1086 
1087 	hdmi_modb(hdmi, addr << 5, I2CM_SLVADDR, I2CM_INTERFACE_CONTROL0);
1088 
1089 	/* Set slave device register address on transfer */
1090 	i2c->is_regaddr = false;
1091 
1092 	/* Set segment pointer for I2C extended read mode operation */
1093 	i2c->is_segment = false;
1094 
1095 	for (i = 0; i < num; i++) {
1096 		dev_dbg(hdmi->dev, "xfer: num: %d/%d, len: %d, flags: %#x\n",
1097 			i + 1, num, msgs[i].len, msgs[i].flags);
1098 
1099 		if (msgs[i].addr == DDC_SEGMENT_ADDR && msgs[i].len == 1) {
1100 			i2c->is_segment = true;
1101 			hdmi_modb(hdmi, DDC_SEGMENT_ADDR, I2CM_SEG_ADDR,
1102 				  I2CM_INTERFACE_CONTROL1);
1103 			hdmi_modb(hdmi, *msgs[i].buf << 7, I2CM_SEG_PTR,
1104 				  I2CM_INTERFACE_CONTROL1);
1105 		} else {
1106 			if (msgs[i].flags & I2C_M_RD)
1107 				ret = dw_hdmi_i2c_read(hdmi, msgs[i].buf,
1108 						       msgs[i].len);
1109 			else
1110 				ret = dw_hdmi_i2c_write(hdmi, msgs[i].buf,
1111 							msgs[i].len);
1112 		}
1113 		if (ret < 0)
1114 			break;
1115 	}
1116 
1117 	if (!ret)
1118 		ret = num;
1119 
1120 	/* Mute DONE and ERROR interrupts */
1121 	hdmi_modb(hdmi, 0, I2CM_OP_DONE_MASK_N | I2CM_NACK_RCVD_MASK_N,
1122 		  MAINUNIT_1_INT_MASK_N);
1123 
1124 	mutex_unlock(&i2c->lock);
1125 
1126 	return ret;
1127 }
1128 
dw_hdmi_i2c_func(struct i2c_adapter * adapter)1129 static u32 dw_hdmi_i2c_func(struct i2c_adapter *adapter)
1130 {
1131 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
1132 }
1133 
1134 static const struct i2c_algorithm dw_hdmi_algorithm = {
1135 	.master_xfer	= dw_hdmi_i2c_xfer,
1136 	.functionality	= dw_hdmi_i2c_func,
1137 };
1138 
dw_hdmi_i2c_adapter(struct dw_hdmi_qp * hdmi)1139 static struct i2c_adapter *dw_hdmi_i2c_adapter(struct dw_hdmi_qp *hdmi)
1140 {
1141 	struct i2c_adapter *adap;
1142 	struct dw_hdmi_qp_i2c *i2c;
1143 	int ret;
1144 
1145 	i2c = devm_kzalloc(hdmi->dev, sizeof(*i2c), GFP_KERNEL);
1146 	if (!i2c)
1147 		return ERR_PTR(-ENOMEM);
1148 
1149 	mutex_init(&i2c->lock);
1150 	init_completion(&i2c->cmp);
1151 
1152 	adap = &i2c->adap;
1153 	adap->class = I2C_CLASS_DDC;
1154 	adap->owner = THIS_MODULE;
1155 	adap->dev.parent = hdmi->dev;
1156 	adap->algo = &dw_hdmi_algorithm;
1157 	strscpy(adap->name, "ddc", sizeof(adap->name));
1158 	i2c_set_adapdata(adap, hdmi);
1159 
1160 	ret = i2c_add_adapter(adap);
1161 	if (ret) {
1162 		dev_warn(hdmi->dev, "cannot add %s I2C adapter\n", adap->name);
1163 		devm_kfree(hdmi->dev, i2c);
1164 		return ERR_PTR(ret);
1165 	}
1166 
1167 	hdmi->i2c = i2c;
1168 
1169 	dev_info(hdmi->dev, "registered %s I2C bus driver\n", adap->name);
1170 
1171 	return adap;
1172 }
1173 
1174 #define HDMI_PHY_EARC_MASK	BIT(29)
1175 
dw_hdmi_qp_set_earc(struct dw_hdmi_qp * hdmi)1176 int dw_hdmi_qp_set_earc(struct dw_hdmi_qp *hdmi)
1177 {
1178 	u32 stat, ret;
1179 
1180 	/* set hdmi phy earc mode */
1181 	hdmi->phy.ops->set_mode(hdmi, hdmi->phy.data, HDMI_PHY_EARC_MASK,
1182 				true);
1183 
1184 	ret = hdmi->phy.ops->init(hdmi, hdmi->phy.data,
1185 				  &hdmi->previous_mode);
1186 	if (ret)
1187 		return ret;
1188 
1189 	hdmi->disabled = false;
1190 
1191 	reinit_completion(&hdmi->earc_cmp);
1192 
1193 	hdmi_modb(hdmi, EARCRX_CMDC_DISCOVERY_TIMEOUT_IRQ |
1194 		  EARCRX_CMDC_DISCOVERY_DONE_IRQ,
1195 		  EARCRX_CMDC_DISCOVERY_TIMEOUT_IRQ |
1196 		  EARCRX_CMDC_DISCOVERY_DONE_IRQ, EARCRX_0_INT_MASK_N);
1197 
1198 	/* start discovery */
1199 	hdmi_modb(hdmi, EARCRX_CMDC_DISCOVERY_EN, EARCRX_CMDC_DISCOVERY_EN,
1200 		  EARCRX_CMDC_CONTROL);
1201 
1202 	/*
1203 	 * The eARC TX device drives a logic-high-voltage-level
1204 	 * pulse on the physical HPD connector pin, after
1205 	 * at least 100 ms of low voltage level to start the
1206 	 * eARC Discovery process.
1207 	 */
1208 	hdmi_modb(hdmi, EARCRX_CONNECTOR_HPD, EARCRX_CONNECTOR_HPD,
1209 		  EARCRX_CMDC_CONTROL);
1210 
1211 	stat = wait_for_completion_timeout(&hdmi->earc_cmp, HZ / 10);
1212 	if (!stat)
1213 		return -EAGAIN;
1214 
1215 	if (hdmi->earc_intr & EARCRX_CMDC_DISCOVERY_TIMEOUT_IRQ) {
1216 		dev_err(hdmi->dev, "discovery timeout\n");
1217 		return -ETIMEDOUT;
1218 	} else if (hdmi->earc_intr & EARCRX_CMDC_DISCOVERY_DONE_IRQ) {
1219 		dev_info(hdmi->dev, "discovery done\n");
1220 	} else {
1221 		dev_err(hdmi->dev, "discovery failed\n");
1222 		return -EINVAL;
1223 	}
1224 
1225 	hdmi_writel(hdmi, 1, EARCRX_DMAC_PHY_CONTROL);
1226 	hdmi_modb(hdmi, EARCRX_CMDC_SWINIT_P, EARCRX_CMDC_SWINIT_P,
1227 		  EARCRX_CMDC_CONFIG0);
1228 
1229 	hdmi_writel(hdmi, 0xf3, EARCRX_DMAC_CONFIG);
1230 	hdmi_writel(hdmi, 0x63, EARCRX_DMAC_CONTROL0);
1231 	hdmi_writel(hdmi, 0xff, EARCRX_DMAC_CONTROL1);
1232 
1233 	hdmi_modb(hdmi, EARCRX_XACTREAD_STOP_CFG | EARCRX_XACTREAD_RETRY_CFG |
1234 		  EARCRX_CMDC_DSCVR_EARCVALID0_TO_DISC1 | EARCRX_CMDC_XACT_RESTART_EN,
1235 		  EARCRX_XACTREAD_STOP_CFG | EARCRX_XACTREAD_RETRY_CFG |
1236 		  EARCRX_CMDC_DSCVR_EARCVALID0_TO_DISC1 | EARCRX_CMDC_XACT_RESTART_EN,
1237 		  EARCRX_CMDC_CONFIG0);
1238 
1239 	hdmi_writel(hdmi, 0, EARCRX_DMAC_CHSTATUS_STREAMER0);
1240 	hdmi_writel(hdmi, 0x1b0e, EARCRX_DMAC_CHSTATUS_STREAMER1);
1241 	hdmi_writel(hdmi, 0, EARCRX_DMAC_CHSTATUS_STREAMER2);
1242 	hdmi_writel(hdmi, 0, EARCRX_DMAC_CHSTATUS_STREAMER3);
1243 	hdmi_writel(hdmi, 0xf2000000, EARCRX_DMAC_CHSTATUS_STREAMER4);
1244 	hdmi_writel(hdmi, 0, EARCRX_DMAC_CHSTATUS_STREAMER5);
1245 	hdmi_writel(hdmi, 0, EARCRX_DMAC_CHSTATUS_STREAMER6);
1246 	hdmi_writel(hdmi, 0, EARCRX_DMAC_CHSTATUS_STREAMER7);
1247 	hdmi_writel(hdmi, 0, EARCRX_DMAC_CHSTATUS_STREAMER8);
1248 
1249 	return 0;
1250 }
1251 EXPORT_SYMBOL_GPL(dw_hdmi_qp_set_earc);
1252 
1253 /* -----------------------------------------------------------------------------
1254  * HDMI TX Setup
1255  */
1256 
hdmi_infoframe_set_checksum(u8 * ptr,int size)1257 static void hdmi_infoframe_set_checksum(u8 *ptr, int size)
1258 {
1259 	u8 csum = 0;
1260 	int i;
1261 
1262 	ptr[3] = 0;
1263 	/* compute checksum */
1264 	for (i = 0; i < size; i++)
1265 		csum += ptr[i];
1266 
1267 	ptr[3] = 256 - csum;
1268 }
1269 
is_hdmi2_sink(const struct drm_connector * connector)1270 static bool is_hdmi2_sink(const struct drm_connector *connector)
1271 {
1272 	if (!connector)
1273 		return true;
1274 
1275 	return connector->display_info.hdmi.scdc.supported ||
1276 		connector->display_info.color_formats & DRM_COLOR_FORMAT_YCRCB420;
1277 }
1278 
hdmi_config_AVI(struct dw_hdmi_qp * hdmi,const struct drm_connector * connector,const struct drm_display_mode * mode)1279 static void hdmi_config_AVI(struct dw_hdmi_qp *hdmi,
1280 			    const struct drm_connector *connector,
1281 			    const struct drm_display_mode *mode)
1282 {
1283 	struct hdmi_avi_infoframe frame;
1284 	u32 val, i, j;
1285 	u8 buff[17];
1286 	enum hdmi_quantization_range rgb_quant_range =
1287 		hdmi->hdmi_data.quant_range;
1288 
1289 	/* Initialise info frame from DRM mode */
1290 	drm_hdmi_avi_infoframe_from_display_mode(&frame, connector, mode);
1291 
1292 	/*
1293 	 * Ignore monitor selectable quantization, use quantization set
1294 	 * by the user
1295 	 */
1296 	drm_hdmi_avi_infoframe_quant_range(&frame, connector, mode, rgb_quant_range);
1297 	if (hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format))
1298 		frame.colorspace = HDMI_COLORSPACE_YUV444;
1299 	else if (hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format))
1300 		frame.colorspace = HDMI_COLORSPACE_YUV422;
1301 	else if (hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format))
1302 		frame.colorspace = HDMI_COLORSPACE_YUV420;
1303 	else
1304 		frame.colorspace = HDMI_COLORSPACE_RGB;
1305 
1306 	/* Set up colorimetry and quant range */
1307 	if (!hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format)) {
1308 		switch (hdmi->hdmi_data.enc_out_encoding) {
1309 		case V4L2_YCBCR_ENC_601:
1310 			if (hdmi->hdmi_data.enc_in_encoding == V4L2_YCBCR_ENC_XV601)
1311 				frame.colorimetry = HDMI_COLORIMETRY_EXTENDED;
1312 			else
1313 				frame.colorimetry = HDMI_COLORIMETRY_ITU_601;
1314 			frame.extended_colorimetry =
1315 					HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
1316 			break;
1317 		case V4L2_YCBCR_ENC_709:
1318 			if (hdmi->hdmi_data.enc_in_encoding == V4L2_YCBCR_ENC_XV709)
1319 				frame.colorimetry = HDMI_COLORIMETRY_EXTENDED;
1320 			else
1321 				frame.colorimetry = HDMI_COLORIMETRY_ITU_709;
1322 			frame.extended_colorimetry =
1323 					HDMI_EXTENDED_COLORIMETRY_XV_YCC_709;
1324 			break;
1325 		case V4L2_YCBCR_ENC_BT2020:
1326 			if (hdmi->hdmi_data.enc_in_encoding == V4L2_YCBCR_ENC_BT2020)
1327 				frame.colorimetry = HDMI_COLORIMETRY_EXTENDED;
1328 			else
1329 				frame.colorimetry = HDMI_COLORIMETRY_ITU_709;
1330 			frame.extended_colorimetry =
1331 					HDMI_EXTENDED_COLORIMETRY_BT2020;
1332 			break;
1333 		default: /* Carries no data */
1334 			frame.colorimetry = HDMI_COLORIMETRY_ITU_601;
1335 			frame.extended_colorimetry =
1336 					HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
1337 			break;
1338 		}
1339 
1340 		frame.ycc_quantization_range = HDMI_YCC_QUANTIZATION_RANGE_LIMITED;
1341 	} else {
1342 		if (hdmi->hdmi_data.enc_out_encoding == V4L2_YCBCR_ENC_BT2020) {
1343 			frame.colorimetry = HDMI_COLORIMETRY_EXTENDED;
1344 			frame.extended_colorimetry =
1345 				HDMI_EXTENDED_COLORIMETRY_BT2020;
1346 		} else {
1347 			frame.colorimetry = HDMI_COLORIMETRY_NONE;
1348 			frame.extended_colorimetry =
1349 				HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
1350 		}
1351 
1352 		if (is_hdmi2_sink(connector) &&
1353 		    frame.quantization_range == HDMI_QUANTIZATION_RANGE_FULL)
1354 			frame.ycc_quantization_range = HDMI_YCC_QUANTIZATION_RANGE_FULL;
1355 		else
1356 			frame.ycc_quantization_range = HDMI_YCC_QUANTIZATION_RANGE_LIMITED;
1357 	}
1358 
1359 	frame.scan_mode = HDMI_SCAN_MODE_NONE;
1360 
1361 	hdmi_avi_infoframe_pack_only(&frame, buff, 17);
1362 
1363 	/* mode which vic >= 128 must use avi version 3 */
1364 	if (hdmi->vic >= 128) {
1365 		frame.version = 3;
1366 		buff[1] = frame.version;
1367 		buff[4] &= 0x1f;
1368 		buff[4] |= ((frame.colorspace & 0x7) << 5);
1369 		buff[7] = hdmi->vic;
1370 		hdmi_infoframe_set_checksum(buff, 17);
1371 	} else if (is_hdmi2_sink(connector)) {
1372 		buff[7] = hdmi->vic;
1373 	}
1374 
1375 	/*
1376 	 * The Designware IP uses a different byte format from standard
1377 	 * AVI info frames, though generally the bits are in the correct
1378 	 * bytes.
1379 	 */
1380 
1381 	val = (frame.version << 8) | (frame.length << 16);
1382 	hdmi_writel(hdmi, val, PKT_AVI_CONTENTS0);
1383 
1384 	for (i = 0; i < 4; i++) {
1385 		for (j = 0; j < 4; j++) {
1386 			if (i * 4 + j >= 14)
1387 				break;
1388 			if (!j)
1389 				val = buff[i * 4 + j + 3];
1390 			val |= buff[i * 4 + j + 3] << (8 * j);
1391 		}
1392 
1393 		hdmi_writel(hdmi, val, PKT_AVI_CONTENTS1 + i * 4);
1394 	}
1395 
1396 	hdmi_modb(hdmi, 0, PKTSCHED_AVI_FIELDRATE, PKTSCHED_PKT_CONFIG1);
1397 
1398 	hdmi_modb(hdmi, PKTSCHED_AVI_TX_EN, PKTSCHED_AVI_TX_EN, PKTSCHED_PKT_EN);
1399 }
1400 
1401 #define VSI_PKT_TYPE		0x81
1402 #define VSI_PKT_VERSION		1
1403 #define HDMI_FORUM_OUI		0xc45dd8
1404 #define ALLM_MODE		BIT(1)
1405 #define HDMI_FORUM_LEN		9
1406 
hdmi_config_vendor_specific_infoframe(struct dw_hdmi_qp * hdmi,const struct drm_connector * connector,const struct drm_display_mode * mode)1407 static void hdmi_config_vendor_specific_infoframe(struct dw_hdmi_qp *hdmi,
1408 						  const struct drm_connector *connector,
1409 						  const struct drm_display_mode *mode)
1410 {
1411 	struct hdmi_vendor_infoframe frame;
1412 	u8 buffer[10];
1413 	u32 val;
1414 	ssize_t err;
1415 	int i, reg;
1416 	struct dw_hdmi_link_config *link_cfg = NULL;
1417 	void *data = hdmi->plat_data->phy_data;
1418 
1419 	if (hdmi->plat_data->get_link_cfg)
1420 		link_cfg = hdmi->plat_data->get_link_cfg(data);
1421 
1422 	hdmi_modb(hdmi, 0, PKTSCHED_VSI_TX_EN, PKTSCHED_PKT_EN);
1423 	for (i = 0; i <= 7; i++)
1424 		hdmi_writel(hdmi, 0, PKT_VSI_CONTENTS0 + i * 4);
1425 
1426 	if (hdmi->allm_enable && (link_cfg->add_func & SUPPORT_HDMI_ALLM)) {
1427 		buffer[0] = VSI_PKT_TYPE;
1428 		buffer[1] = VSI_PKT_VERSION;
1429 		buffer[2] = 5;
1430 		buffer[4] = HDMI_FORUM_OUI & 0xff;
1431 		buffer[5] = (HDMI_FORUM_OUI >> 8) & 0xff;
1432 		buffer[6] = (HDMI_FORUM_OUI >> 16) & 0xff;
1433 		buffer[7] = VSI_PKT_VERSION;
1434 		buffer[8] = ALLM_MODE;
1435 
1436 		hdmi_infoframe_set_checksum(buffer, HDMI_FORUM_LEN);
1437 
1438 		err = 9;
1439 	} else {
1440 		if (is_hdmi2_sink(connector)) {
1441 			hdmi_modb(hdmi, 0, PKTSCHED_VSI_TX_EN, PKTSCHED_PKT_EN);
1442 			return;
1443 		}
1444 		err = drm_hdmi_vendor_infoframe_from_display_mode(&frame, connector,
1445 								  mode);
1446 		if (err < 0)
1447 			/*
1448 			 * Going into that statement does not means vendor infoframe
1449 			 * fails. It just informed us that vendor infoframe is not
1450 			 * needed for the selected mode. Only 4k or stereoscopic 3D
1451 			 * mode requires vendor infoframe. So just simply return.
1452 			 */
1453 			return;
1454 
1455 		err = hdmi_vendor_infoframe_pack(&frame, buffer, sizeof(buffer));
1456 		if (err < 0) {
1457 			dev_err(hdmi->dev, "Failed to pack vendor infoframe: %zd\n",
1458 				err);
1459 			return;
1460 		}
1461 	}
1462 
1463 	/* vsi header */
1464 	val = (buffer[2] << 16) | (buffer[1] << 8) | buffer[0];
1465 	hdmi_writel(hdmi, val, PKT_VSI_CONTENTS0);
1466 
1467 	reg = PKT_VSI_CONTENTS1;
1468 	for (i = 3; i < err; i++) {
1469 		if (i % 4 == 3)
1470 			val = buffer[i];
1471 		if (i % 4 == 0)
1472 			val |= buffer[i] << 8;
1473 		if (i % 4 == 1)
1474 			val |= buffer[i] << 16;
1475 		if (i % 4 == 2)
1476 			val |= buffer[i] << 24;
1477 
1478 		if ((i % 4 == 2) || (i == (err - 1))) {
1479 			hdmi_writel(hdmi, val, reg);
1480 			reg += 4;
1481 		}
1482 	}
1483 
1484 	hdmi_writel(hdmi, 0, PKT_VSI_CONTENTS7);
1485 
1486 	hdmi_modb(hdmi, 0, PKTSCHED_VSI_FIELDRATE, PKTSCHED_PKT_CONFIG1);
1487 	hdmi_modb(hdmi, PKTSCHED_VSI_TX_EN, PKTSCHED_VSI_TX_EN,
1488 		  PKTSCHED_PKT_EN);
1489 }
1490 
hdmi_config_CVTEM(struct dw_hdmi_qp * hdmi)1491 static void hdmi_config_CVTEM(struct dw_hdmi_qp *hdmi)
1492 {
1493 	u8 ds_type = 0;
1494 	u8 sync = 1;
1495 	u8 vfr = 1;
1496 	u8 afr = 0;
1497 	u8 new = 1;
1498 	u8 end = 0;
1499 	u8 data_set_length = 136;
1500 	u8 hb1[6] = { 0x80, 0, 0, 0, 0, 0x40 };
1501 	u8 *pps_body;
1502 	u32 val, i, reg;
1503 	struct drm_display_mode *mode = &hdmi->previous_mode;
1504 	int hsync, hfront, hback;
1505 	struct dw_hdmi_link_config *link_cfg;
1506 	void *data = hdmi->plat_data->phy_data;
1507 
1508 	hdmi_modb(hdmi, 0, PKTSCHED_EMP_CVTEM_TX_EN, PKTSCHED_PKT_EN);
1509 
1510 	if (hdmi->plat_data->get_link_cfg) {
1511 		link_cfg = hdmi->plat_data->get_link_cfg(data);
1512 	} else {
1513 		dev_err(hdmi->dev, "can't get frl link cfg\n");
1514 		return;
1515 	}
1516 
1517 	if (!link_cfg->dsc_mode) {
1518 		dev_info(hdmi->dev, "don't use dsc mode\n");
1519 		return;
1520 	}
1521 
1522 	pps_body = link_cfg->pps_payload;
1523 
1524 	hsync = mode->hsync_end - mode->hsync_start;
1525 	hback = mode->htotal - mode->hsync_end;
1526 	hfront = mode->hsync_start - mode->hdisplay;
1527 
1528 	for (i = 0; i < 6; i++) {
1529 		val = i << 16 | hb1[i] << 8;
1530 		hdmi_writel(hdmi, val, PKT0_EMP_CVTEM_CONTENTS0 + i * 0x20);
1531 	}
1532 
1533 	val = new << 7 | end << 6 | ds_type << 4 | afr << 3 |
1534 	      vfr << 2 | sync << 1;
1535 	hdmi_writel(hdmi, val, PKT0_EMP_CVTEM_CONTENTS1);
1536 
1537 	val = data_set_length << 16 | pps_body[0] << 24;
1538 	hdmi_writel(hdmi, val, PKT0_EMP_CVTEM_CONTENTS2);
1539 
1540 	reg = PKT0_EMP_CVTEM_CONTENTS3;
1541 	for (i = 1; i < 125; i++) {
1542 		if (reg == PKT1_EMP_CVTEM_CONTENTS0 ||
1543 		    reg == PKT2_EMP_CVTEM_CONTENTS0 ||
1544 		    reg == PKT3_EMP_CVTEM_CONTENTS0 ||
1545 		    reg == PKT4_EMP_CVTEM_CONTENTS0 ||
1546 		    reg == PKT5_EMP_CVTEM_CONTENTS0) {
1547 			reg += 4;
1548 			i--;
1549 			continue;
1550 		}
1551 		if (i % 4 == 1)
1552 			val = pps_body[i];
1553 		if (i % 4 == 2)
1554 			val |= pps_body[i] << 8;
1555 		if (i % 4 == 3)
1556 			val |= pps_body[i] << 16;
1557 		if (!(i % 4)) {
1558 			val |= pps_body[i] << 24;
1559 			hdmi_writel(hdmi, val, reg);
1560 			reg += 4;
1561 		}
1562 	}
1563 
1564 	val = (hfront & 0xff) << 24 | pps_body[127] << 16 |
1565 	      pps_body[126] << 8 | pps_body[125];
1566 	hdmi_writel(hdmi, val, PKT4_EMP_CVTEM_CONTENTS6);
1567 
1568 	val = (hback & 0xff) << 24 | ((hsync >> 8) & 0xff) << 16 |
1569 	      (hsync & 0xff) << 8 | ((hfront >> 8) & 0xff);
1570 	hdmi_writel(hdmi, val, PKT4_EMP_CVTEM_CONTENTS7);
1571 
1572 	val = link_cfg->hcactive << 8 | ((hback >> 8) & 0xff);
1573 	hdmi_writel(hdmi, val, PKT5_EMP_CVTEM_CONTENTS1);
1574 
1575 	for (i = PKT5_EMP_CVTEM_CONTENTS2; i <= PKT5_EMP_CVTEM_CONTENTS7; i += 4)
1576 		hdmi_writel(hdmi, 0, i);
1577 
1578 	hdmi_modb(hdmi, PKTSCHED_EMP_CVTEM_TX_EN, PKTSCHED_EMP_CVTEM_TX_EN,
1579 		  PKTSCHED_PKT_EN);
1580 }
1581 
hdmi_config_drm_infoframe(struct dw_hdmi_qp * hdmi,const struct drm_connector * connector)1582 static void hdmi_config_drm_infoframe(struct dw_hdmi_qp *hdmi,
1583 				      const struct drm_connector *connector)
1584 {
1585 	const struct drm_connector_state *conn_state = connector->state;
1586 	struct hdr_output_metadata *hdr_metadata;
1587 	struct hdmi_drm_infoframe frame;
1588 	u8 buffer[30];
1589 	ssize_t err;
1590 	int i;
1591 	u32 val;
1592 
1593 	if (!hdmi->plat_data->use_drm_infoframe)
1594 		return;
1595 
1596 	hdmi_modb(hdmi, 0, PKTSCHED_DRMI_TX_EN, PKTSCHED_PKT_EN);
1597 
1598 	if (!hdmi->connector.hdr_sink_metadata.hdmi_type1.eotf) {
1599 		DRM_DEBUG("No need to set HDR metadata in infoframe\n");
1600 		return;
1601 	}
1602 
1603 	if (!conn_state->hdr_output_metadata) {
1604 		DRM_DEBUG("source metadata not set yet\n");
1605 		return;
1606 	}
1607 
1608 	hdr_metadata = (struct hdr_output_metadata *)
1609 		conn_state->hdr_output_metadata->data;
1610 
1611 	if (!(hdmi->connector.hdr_sink_metadata.hdmi_type1.eotf &
1612 	      BIT(hdr_metadata->hdmi_metadata_type1.eotf))) {
1613 		DRM_ERROR("Not support EOTF %d\n",
1614 			  hdr_metadata->hdmi_metadata_type1.eotf);
1615 		return;
1616 	}
1617 
1618 	err = drm_hdmi_infoframe_set_hdr_metadata(&frame, conn_state);
1619 	if (err < 0)
1620 		return;
1621 
1622 	err = hdmi_drm_infoframe_pack(&frame, buffer, sizeof(buffer));
1623 	if (err < 0) {
1624 		dev_err(hdmi->dev, "Failed to pack drm infoframe: %zd\n", err);
1625 		return;
1626 	}
1627 
1628 	val = (frame.version << 8) | (frame.length << 16);
1629 	hdmi_writel(hdmi, val, PKT_DRMI_CONTENTS0);
1630 
1631 	for (i = 0; i <= frame.length; i++) {
1632 		if (i % 4 == 0)
1633 			val = buffer[3 + i];
1634 		val |= buffer[3 + i] << ((i % 4) * 8);
1635 
1636 		if (i % 4 == 3 || (i == (frame.length)))
1637 			hdmi_writel(hdmi, val, PKT_DRMI_CONTENTS1 + ((i / 4) * 4));
1638 	}
1639 
1640 	hdmi_modb(hdmi, 0, PKTSCHED_DRMI_FIELDRATE, PKTSCHED_PKT_CONFIG1);
1641 
1642 	/*
1643 	 * avi and hdr infoframe cannot be sent at the same time
1644 	 * for compatibility with Huawei TV
1645 	 */
1646 	mdelay(50);
1647 	hdmi_modb(hdmi, PKTSCHED_DRMI_TX_EN, PKTSCHED_DRMI_TX_EN, PKTSCHED_PKT_EN);
1648 
1649 	DRM_DEBUG("%s eotf %d end\n", __func__,
1650 		  hdr_metadata->hdmi_metadata_type1.eotf);
1651 }
1652 
1653 /* Filter out invalid setups to avoid configuring SCDC and scrambling */
dw_hdmi_support_scdc(struct dw_hdmi_qp * hdmi,const struct drm_display_info * display)1654 static bool dw_hdmi_support_scdc(struct dw_hdmi_qp *hdmi,
1655 				 const struct drm_display_info *display)
1656 {
1657 	/* Disable if no DDC bus */
1658 	if (!hdmi->ddc)
1659 		return false;
1660 
1661 	/* Disable if SCDC is not supported, or if an HF-VSDB block is absent */
1662 	if (!display->hdmi.scdc.supported ||
1663 	    !display->hdmi.scdc.scrambling.supported)
1664 		return false;
1665 
1666 	/*
1667 	 * Disable if display only support low TMDS rates and scrambling
1668 	 * for low rates is not supported either
1669 	 */
1670 	if (!display->hdmi.scdc.scrambling.low_rates &&
1671 	    display->max_tmds_clock <= 340000)
1672 		return false;
1673 
1674 	return true;
1675 }
1676 
hdmi_set_frl_mask(int frl_rate)1677 static int hdmi_set_frl_mask(int frl_rate)
1678 {
1679 	switch (frl_rate) {
1680 	case 48:
1681 		return FRL_12GBPS_4LANE;
1682 	case 40:
1683 		return FRL_10GBPS_4LANE;
1684 	case 32:
1685 		return FRL_8GBPS_4LANE;
1686 	case 24:
1687 		return FRL_6GBPS_4LANE;
1688 	case 18:
1689 		return FRL_6GBPS_3LANE;
1690 	case 9:
1691 		return FRL_3GBPS_3LANE;
1692 	}
1693 
1694 	return 0;
1695 }
1696 
hdmi_start_flt(struct dw_hdmi_qp * hdmi,u8 rate)1697 static int hdmi_start_flt(struct dw_hdmi_qp *hdmi, u8 rate)
1698 {
1699 	u8 val;
1700 	u32 value;
1701 	u8 ffe_lv = 0;
1702 	int i = 0;
1703 	bool ltsp = false;
1704 
1705 	hdmi_modb(hdmi, AVP_DATAPATH_VIDEO_SWDISABLE,
1706 		  AVP_DATAPATH_VIDEO_SWDISABLE, GLOBAL_SWDISABLE);
1707 
1708 	/* reset avp data path */
1709 	hdmi_writel(hdmi, BIT(6), GLOBAL_SWRESET_REQUEST);
1710 
1711 	/* FLT_READY & FFE_LEVELS read */
1712 	for (i = 0; i < 20; i++) {
1713 		drm_scdc_readb(hdmi->ddc, SCDC_STATUS_FLAGS_0, &val);
1714 		if (val & BIT(6))
1715 			break;
1716 		msleep(20);
1717 	}
1718 
1719 	if (i == 20) {
1720 		dev_err(hdmi->dev, "sink flt isn't ready\n");
1721 		return -EINVAL;
1722 	}
1723 
1724 	/* clear flt flags */
1725 	drm_scdc_readb(hdmi->ddc, 0x10, &val);
1726 	if (val & BIT(5))
1727 		drm_scdc_writeb(hdmi->ddc, 0x10, BIT(5));
1728 
1729 	/* max ffe level 3 */
1730 	val = 0 << 4 | hdmi_set_frl_mask(rate);
1731 	drm_scdc_writeb(hdmi->ddc, 0x31, val);
1732 
1733 	/* select FRL_RATE & FFE_LEVELS */
1734 	hdmi_writel(hdmi, ffe_lv, FLT_CONFIG0);
1735 
1736 	/* we set max 2s timeout */
1737 	i = 4000;
1738 	while (i--) {
1739 		/* source should poll update flag every 2ms or less */
1740 		usleep_range(400, 500);
1741 		drm_scdc_readb(hdmi->ddc, 0x10, &val);
1742 
1743 		if (!(val & 0x30))
1744 			continue;
1745 
1746 		if (val & BIT(5)) {
1747 			u8 reg_val, ln0, ln1, ln2, ln3;
1748 
1749 			drm_scdc_readb(hdmi->ddc, 0x41, &reg_val);
1750 			ln0 = reg_val & 0xf;
1751 			ln1 = (reg_val >> 4) & 0xf;
1752 
1753 			drm_scdc_readb(hdmi->ddc, 0x42, &reg_val);
1754 			ln2 = reg_val & 0xf;
1755 			ln3 = (reg_val >> 4) & 0xf;
1756 
1757 			if (!ln0 && !ln1 && !ln2 && !ln3) {
1758 				dev_info(hdmi->dev, "goto ltsp\n");
1759 				ltsp = true;
1760 				hdmi_writel(hdmi, 0, FLT_CONFIG1);
1761 			} else if ((ln0 == 0xf) | (ln1 == 0xf) | (ln2 == 0xf) | (ln3 == 0xf)) {
1762 				dev_err(hdmi->dev, "goto lts4\n");
1763 				break;
1764 			} else if ((ln0 == 0xe) | (ln1 == 0xe) | (ln2 == 0xe) | (ln3 == 0xe)) {
1765 				dev_info(hdmi->dev, "goto ffe\n");
1766 				break;
1767 			} else {
1768 				value = (ln3 << 16) | (ln2 << 12) | (ln1 << 8) | (ln0 << 4) | 0xf;
1769 				hdmi_writel(hdmi, value, FLT_CONFIG1);
1770 			}
1771 		}
1772 
1773 		/* only clear frl_start and flt_update */
1774 		drm_scdc_writeb(hdmi->ddc, 0x10, val & 0x30);
1775 
1776 		if ((val & BIT(4)) && ltsp) {
1777 			hdmi_modb(hdmi, 0, AVP_DATAPATH_VIDEO_SWDISABLE, GLOBAL_SWDISABLE);
1778 			dev_info(hdmi->dev, "flt success\n");
1779 			break;
1780 		}
1781 	}
1782 
1783 	if (i < 0) {
1784 		dev_err(hdmi->dev, "flt time out\n");
1785 		return -ETIMEDOUT;
1786 	}
1787 
1788 	return 0;
1789 }
1790 
1791 #define HDMI_MODE_FRL_MASK     BIT(30)
1792 
hdmi_set_op_mode(struct dw_hdmi_qp * hdmi,struct dw_hdmi_link_config * link_cfg,const struct drm_connector * connector)1793 static int hdmi_set_op_mode(struct dw_hdmi_qp *hdmi,
1794 			    struct dw_hdmi_link_config *link_cfg,
1795 			    const struct drm_connector *connector)
1796 {
1797 	int frl_rate;
1798 	int i, ret = 0;
1799 
1800 	if (hdmi->frl_switch)
1801 		return 0;
1802 
1803 	if (!link_cfg->frl_mode) {
1804 		dev_info(hdmi->dev, "dw hdmi qp use tmds mode\n");
1805 		hdmi_modb(hdmi, 0, OPMODE_FRL, LINK_CONFIG0);
1806 		hdmi_modb(hdmi, 0, OPMODE_FRL_4LANES, LINK_CONFIG0);
1807 		if (!hdmi->update) {
1808 			ret = hdmi->phy.ops->init(hdmi, hdmi->phy.data, &hdmi->previous_mode);
1809 			if (!ret)
1810 				hdmi->disabled = false;
1811 		}
1812 
1813 		return ret;
1814 	}
1815 
1816 	if (hdmi->update)
1817 		return 0;
1818 
1819 	if (link_cfg->frl_lanes == 4)
1820 		hdmi_modb(hdmi, OPMODE_FRL_4LANES, OPMODE_FRL_4LANES,
1821 			  LINK_CONFIG0);
1822 	else
1823 		hdmi_modb(hdmi, 0, OPMODE_FRL_4LANES, LINK_CONFIG0);
1824 
1825 	hdmi_modb(hdmi, 1, OPMODE_FRL, LINK_CONFIG0);
1826 
1827 	frl_rate = link_cfg->frl_lanes * link_cfg->rate_per_lane;
1828 
1829 	ret = hdmi->phy.ops->init(hdmi, hdmi->phy.data, &hdmi->previous_mode);
1830 	if (ret)
1831 		return ret;
1832 	hdmi->disabled = false;
1833 
1834 	msleep(50);
1835 
1836 	ret = hdmi_start_flt(hdmi, frl_rate);
1837 	if (ret) {
1838 		hdmi_writel(hdmi, 0, FLT_CONFIG0);
1839 		drm_scdc_writeb(hdmi->ddc, 0x31, 0);
1840 		hdmi_modb(hdmi, 0, AVP_DATAPATH_VIDEO_SWDISABLE, GLOBAL_SWDISABLE);
1841 		return ret;
1842 	}
1843 
1844 	for (i = 0; i < 200; i++) {
1845 		hdmi_modb(hdmi, PKTSCHED_NULL_TX_EN, PKTSCHED_NULL_TX_EN, PKTSCHED_PKT_EN);
1846 		usleep_range(50, 60);
1847 		hdmi_modb(hdmi, 0, PKTSCHED_NULL_TX_EN, PKTSCHED_PKT_EN);
1848 		usleep_range(50, 60);
1849 	}
1850 
1851 	return 0;
1852 }
1853 
1854 static unsigned long
hdmi_get_tmdsclock(struct dw_hdmi_qp * hdmi,unsigned long mpixelclock)1855 hdmi_get_tmdsclock(struct dw_hdmi_qp *hdmi, unsigned long mpixelclock)
1856 {
1857 	unsigned long tmdsclock = mpixelclock;
1858 	unsigned int depth =
1859 		hdmi_bus_fmt_color_depth(hdmi->hdmi_data.enc_out_bus_format);
1860 
1861 	if (!hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format)) {
1862 		switch (depth) {
1863 		case 16:
1864 			tmdsclock = mpixelclock * 2;
1865 			break;
1866 		case 12:
1867 			tmdsclock = mpixelclock * 3 / 2;
1868 			break;
1869 		case 10:
1870 			tmdsclock = mpixelclock * 5 / 4;
1871 			break;
1872 		default:
1873 			break;
1874 		}
1875 	}
1876 
1877 	return tmdsclock;
1878 }
1879 
dw_hdmi_qp_setup(struct dw_hdmi_qp * hdmi,const struct drm_connector * connector,struct drm_display_mode * mode)1880 static int dw_hdmi_qp_setup(struct dw_hdmi_qp *hdmi,
1881 			    const struct drm_connector *connector,
1882 			    struct drm_display_mode *mode)
1883 {
1884 	void *data = hdmi->plat_data->phy_data;
1885 	struct hdmi_vmode_qp *vmode = &hdmi->hdmi_data.video_mode;
1886 	struct dw_hdmi_link_config *link_cfg;
1887 	u8 bytes = 0;
1888 
1889 	hdmi->vic = drm_match_cea_mode(mode);
1890 	if (!hdmi->vic)
1891 		dev_dbg(hdmi->dev, "Non-CEA mode used in HDMI\n");
1892 	else
1893 		dev_dbg(hdmi->dev, "CEA mode used vic=%d\n", hdmi->vic);
1894 
1895 	if (hdmi->plat_data->get_enc_out_encoding)
1896 		hdmi->hdmi_data.enc_out_encoding =
1897 			hdmi->plat_data->get_enc_out_encoding(data);
1898 	else if ((hdmi->vic == 6) || (hdmi->vic == 7) ||
1899 		 (hdmi->vic == 21) || (hdmi->vic == 22) ||
1900 		 (hdmi->vic == 2) || (hdmi->vic == 3) ||
1901 		 (hdmi->vic == 17) || (hdmi->vic == 18))
1902 		hdmi->hdmi_data.enc_out_encoding = V4L2_YCBCR_ENC_601;
1903 	else
1904 		hdmi->hdmi_data.enc_out_encoding = V4L2_YCBCR_ENC_709;
1905 
1906 	if (mode->flags & DRM_MODE_FLAG_DBLCLK) {
1907 		hdmi->hdmi_data.video_mode.mpixelrepetitionoutput = 1;
1908 		hdmi->hdmi_data.video_mode.mpixelrepetitioninput = 1;
1909 	} else {
1910 		hdmi->hdmi_data.video_mode.mpixelrepetitionoutput = 0;
1911 		hdmi->hdmi_data.video_mode.mpixelrepetitioninput = 0;
1912 	}
1913 
1914 	/*  Get input format from plat data or fallback to RGB888 */
1915 	if (hdmi->plat_data->get_input_bus_format)
1916 		hdmi->hdmi_data.enc_in_bus_format =
1917 			hdmi->plat_data->get_input_bus_format(data);
1918 	else if (hdmi->plat_data->input_bus_format)
1919 		hdmi->hdmi_data.enc_in_bus_format =
1920 			hdmi->plat_data->input_bus_format;
1921 	else
1922 		hdmi->hdmi_data.enc_in_bus_format = MEDIA_BUS_FMT_RGB888_1X24;
1923 
1924 	/* Default to RGB888 output format */
1925 	if (hdmi->plat_data->get_output_bus_format)
1926 		hdmi->hdmi_data.enc_out_bus_format =
1927 			hdmi->plat_data->get_output_bus_format(data);
1928 	else
1929 		hdmi->hdmi_data.enc_out_bus_format = MEDIA_BUS_FMT_RGB888_1X24;
1930 
1931 	if (hdmi->plat_data->set_prev_bus_format)
1932 		hdmi->plat_data->set_prev_bus_format(data, hdmi->hdmi_data.enc_out_bus_format);
1933 
1934 	/* Get input encoding from plat data or fallback to none */
1935 	if (hdmi->plat_data->get_enc_in_encoding)
1936 		hdmi->hdmi_data.enc_in_encoding =
1937 			hdmi->plat_data->get_enc_in_encoding(data);
1938 	else if (hdmi->plat_data->input_bus_encoding)
1939 		hdmi->hdmi_data.enc_in_encoding =
1940 			hdmi->plat_data->input_bus_encoding;
1941 	else
1942 		hdmi->hdmi_data.enc_in_encoding = V4L2_YCBCR_ENC_DEFAULT;
1943 
1944 	if (hdmi->plat_data->get_quant_range)
1945 		hdmi->hdmi_data.quant_range =
1946 			hdmi->plat_data->get_quant_range(data);
1947 	else
1948 		hdmi->hdmi_data.quant_range = HDMI_QUANTIZATION_RANGE_DEFAULT;
1949 
1950 	if (hdmi->plat_data->get_link_cfg)
1951 		link_cfg = hdmi->plat_data->get_link_cfg(data);
1952 	else
1953 		return -EINVAL;
1954 
1955 	hdmi->phy.ops->set_mode(hdmi, hdmi->phy.data, HDMI_MODE_FRL_MASK,
1956 				link_cfg->frl_mode);
1957 
1958 	if (!hdmi->update && !hdmi->frl_switch && hdmi->plat_data->link_clk_set)
1959 		hdmi->plat_data->link_clk_set(data, true);
1960 
1961 	/*
1962 	 * According to the dw-hdmi specification 6.4.2
1963 	 * vp_pr_cd[3:0]:
1964 	 * 0000b: No pixel repetition (pixel sent only once)
1965 	 * 0001b: Pixel sent two times (pixel repeated once)
1966 	 */
1967 	hdmi->hdmi_data.pix_repet_factor =
1968 		(mode->flags & DRM_MODE_FLAG_DBLCLK) ? 1 : 0;
1969 	hdmi->hdmi_data.video_mode.mdataenablepolarity = true;
1970 
1971 	vmode->previous_pixelclock = vmode->mpixelclock;
1972 	if (hdmi->plat_data->split_mode)
1973 		mode->crtc_clock /= 2;
1974 	vmode->mpixelclock = mode->crtc_clock * 1000;
1975 	if ((mode->flags & DRM_MODE_FLAG_3D_MASK) == DRM_MODE_FLAG_3D_FRAME_PACKING)
1976 		vmode->mpixelclock *= 2;
1977 	dev_dbg(hdmi->dev, "final pixclk = %ld\n", vmode->mpixelclock);
1978 	vmode->previous_tmdsclock = vmode->mtmdsclock;
1979 	vmode->mtmdsclock = hdmi_get_tmdsclock(hdmi, vmode->mpixelclock);
1980 	if (hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format))
1981 		vmode->mtmdsclock /= 2;
1982 	dev_info(hdmi->dev, "final tmdsclk = %d\n", vmode->mtmdsclock);
1983 
1984 	if (hdmi->plat_data->set_grf_cfg)
1985 		hdmi->plat_data->set_grf_cfg(data);
1986 
1987 	if (hdmi->sink_has_audio) {
1988 		dev_dbg(hdmi->dev, "sink has audio support\n");
1989 
1990 		/* HDMI Initialization Step E - Configure audio */
1991 		hdmi_clk_regenerator_update_pixel_clock(hdmi);
1992 		hdmi_enable_audio_clk(hdmi, hdmi->audio_enable);
1993 	}
1994 
1995 	/* not for DVI mode */
1996 	if (hdmi->sink_is_hdmi) {
1997 		int ret;
1998 
1999 		dev_dbg(hdmi->dev, "%s HDMI mode\n", __func__);
2000 		hdmi_modb(hdmi, 0, OPMODE_DVI, LINK_CONFIG0);
2001 		hdmi_modb(hdmi, HDCP2_BYPASS, HDCP2_BYPASS, HDCP2LOGIC_CONFIG0);
2002 		hdmi_modb(hdmi, KEEPOUT_REKEY_ALWAYS, KEEPOUT_REKEY_CFG, FRAME_COMPOSER_CONFIG9);
2003 
2004 		if (!link_cfg->frl_mode && dw_hdmi_support_scdc(hdmi, &connector->display_info) &&
2005 		    !hdmi->update) {
2006 			if (vmode->mtmdsclock > HDMI14_MAX_TMDSCLK) {
2007 				drm_scdc_readb(hdmi->ddc, SCDC_SINK_VERSION, &bytes);
2008 				drm_scdc_writeb(hdmi->ddc, SCDC_SOURCE_VERSION,
2009 						min_t(u8, bytes, SCDC_MIN_SOURCE_VERSION));
2010 				drm_scdc_set_high_tmds_clock_ratio(hdmi->ddc, 1);
2011 				drm_scdc_set_scrambling(hdmi->ddc, 1);
2012 				hdmi_writel(hdmi, 1, SCRAMB_CONFIG0);
2013 				/* Wait for resuming transmission of TMDS clock and data */
2014 				msleep(100);
2015 			} else {
2016 				drm_scdc_set_high_tmds_clock_ratio(hdmi->ddc, 0);
2017 				drm_scdc_set_scrambling(hdmi->ddc, 0);
2018 				hdmi_writel(hdmi, 0, SCRAMB_CONFIG0);
2019 			}
2020 		}
2021 		/* HDMI Initialization Step F - Configure AVI InfoFrame */
2022 		hdmi_config_AVI(hdmi, connector, mode);
2023 		hdmi_config_vendor_specific_infoframe(hdmi, connector, mode);
2024 		hdmi_config_CVTEM(hdmi);
2025 		hdmi_config_drm_infoframe(hdmi, connector);
2026 		ret = hdmi_set_op_mode(hdmi, link_cfg, connector);
2027 		if (ret) {
2028 			dev_err(hdmi->dev, "%s hdmi set operation mode failed\n", __func__);
2029 			hdmi->frl_switch = false;
2030 			return ret;
2031 		}
2032 	} else {
2033 		hdmi_modb(hdmi, HDCP2_BYPASS, HDCP2_BYPASS, HDCP2LOGIC_CONFIG0);
2034 		hdmi_modb(hdmi, OPMODE_DVI, OPMODE_DVI, LINK_CONFIG0);
2035 		hdmi_writel(hdmi, 2, PKTSCHED_PKT_CONTROL0);
2036 		hdmi_modb(hdmi, PKTSCHED_GCP_TX_EN, PKTSCHED_GCP_TX_EN, PKTSCHED_PKT_EN);
2037 		hdmi->phy.ops->init(hdmi, hdmi->phy.data, &hdmi->previous_mode);
2038 		dev_info(hdmi->dev, "%s DVI mode\n", __func__);
2039 	}
2040 
2041 	hdmi->frl_switch = false;
2042 	return 0;
2043 }
2044 
2045 static enum drm_connector_status
dw_hdmi_connector_detect(struct drm_connector * connector,bool force)2046 dw_hdmi_connector_detect(struct drm_connector *connector, bool force)
2047 {
2048 	struct dw_hdmi_qp *hdmi =
2049 		container_of(connector, struct dw_hdmi_qp, connector);
2050 	struct dw_hdmi_qp *secondary = NULL;
2051 	enum drm_connector_status result, result_secondary;
2052 
2053 	mutex_lock(&hdmi->mutex);
2054 	hdmi->force = DRM_FORCE_UNSPECIFIED;
2055 	mutex_unlock(&hdmi->mutex);
2056 
2057 	if (hdmi->panel)
2058 		return connector_status_connected;
2059 
2060 	if (hdmi->plat_data->left)
2061 		secondary = hdmi->plat_data->left;
2062 	else if (hdmi->plat_data->right)
2063 		secondary = hdmi->plat_data->right;
2064 
2065 	result = hdmi->phy.ops->read_hpd(hdmi, hdmi->phy.data);
2066 
2067 	if (secondary) {
2068 		result_secondary = secondary->phy.ops->read_hpd(secondary, secondary->phy.data);
2069 		if (result == connector_status_connected &&
2070 		    result_secondary == connector_status_connected)
2071 			result = connector_status_connected;
2072 		else
2073 			result = connector_status_disconnected;
2074 	}
2075 
2076 	return result;
2077 }
2078 
2079 static int
dw_hdmi_update_hdr_property(struct drm_connector * connector)2080 dw_hdmi_update_hdr_property(struct drm_connector *connector)
2081 {
2082 	struct drm_device *dev = connector->dev;
2083 	struct dw_hdmi_qp *hdmi = container_of(connector, struct dw_hdmi_qp,
2084 					       connector);
2085 	void *data = hdmi->plat_data->phy_data;
2086 	const struct hdr_static_metadata *metadata =
2087 		&connector->hdr_sink_metadata.hdmi_type1;
2088 	size_t size = sizeof(*metadata);
2089 	struct drm_property *property;
2090 	struct drm_property_blob *blob;
2091 	int ret;
2092 
2093 	if (hdmi->plat_data->get_hdr_property)
2094 		property = hdmi->plat_data->get_hdr_property(data);
2095 	else
2096 		return -EINVAL;
2097 
2098 	if (hdmi->plat_data->get_hdr_blob)
2099 		blob = hdmi->plat_data->get_hdr_blob(data);
2100 	else
2101 		return -EINVAL;
2102 
2103 	ret = drm_property_replace_global_blob(dev, &blob, size, metadata,
2104 					       &connector->base, property);
2105 	return ret;
2106 }
2107 
dw_hdmi_qp_check_output_type_changed(struct dw_hdmi_qp * hdmi)2108 static bool dw_hdmi_qp_check_output_type_changed(struct dw_hdmi_qp *hdmi)
2109 {
2110 	bool sink_hdmi;
2111 
2112 	sink_hdmi = hdmi->sink_is_hdmi;
2113 
2114 	if (hdmi->force_output == 1)
2115 		hdmi->sink_is_hdmi = true;
2116 	else if (hdmi->force_output == 2)
2117 		hdmi->sink_is_hdmi = false;
2118 	else
2119 		hdmi->sink_is_hdmi = hdmi->support_hdmi;
2120 
2121 	if (sink_hdmi != hdmi->sink_is_hdmi)
2122 		return true;
2123 
2124 	return false;
2125 }
2126 
dw_hdmi_connector_get_modes(struct drm_connector * connector)2127 static int dw_hdmi_connector_get_modes(struct drm_connector *connector)
2128 {
2129 	struct dw_hdmi_qp *hdmi =
2130 		container_of(connector, struct dw_hdmi_qp, connector);
2131 	struct hdr_static_metadata *metedata =
2132 		&connector->hdr_sink_metadata.hdmi_type1;
2133 	struct edid *edid;
2134 	struct drm_display_mode *mode;
2135 	struct drm_display_info *info = &connector->display_info;
2136 	void *data = hdmi->plat_data->phy_data;
2137 	int i, ret = 0;
2138 
2139 	if (hdmi->panel)
2140 		return drm_panel_get_modes(hdmi->panel, connector);
2141 
2142 	if (!hdmi->ddc)
2143 		return 0;
2144 
2145 	memset(metedata, 0, sizeof(*metedata));
2146 	edid = drm_get_edid(connector, hdmi->ddc);
2147 	if (edid) {
2148 		dev_dbg(hdmi->dev, "got edid: width[%d] x height[%d]\n",
2149 			edid->width_cm, edid->height_cm);
2150 
2151 		hdmi->support_hdmi = drm_detect_hdmi_monitor(edid);
2152 		hdmi->sink_has_audio = drm_detect_monitor_audio(edid);
2153 		drm_connector_update_edid_property(connector, edid);
2154 		if (hdmi->cec_notifier)
2155 			cec_notifier_set_phys_addr_from_edid(hdmi->cec_notifier, edid);
2156 		if (hdmi->plat_data->get_edid_dsc_info)
2157 			hdmi->plat_data->get_edid_dsc_info(data, edid);
2158 		ret = drm_add_edid_modes(connector, edid);
2159 		if (hdmi->plat_data->get_colorimetry)
2160 			hdmi->plat_data->get_colorimetry(data, edid);
2161 		if (hdmi->plat_data->get_yuv422_format)
2162 			hdmi->plat_data->get_yuv422_format(connector, edid);
2163 		dw_hdmi_update_hdr_property(connector);
2164 		if (ret > 0 && hdmi->plat_data->split_mode) {
2165 			struct dw_hdmi_qp *secondary = NULL;
2166 			void *secondary_data;
2167 
2168 			if (hdmi->plat_data->left)
2169 				secondary = hdmi->plat_data->left;
2170 			else if (hdmi->plat_data->right)
2171 				secondary = hdmi->plat_data->right;
2172 
2173 			if (!secondary)
2174 				return -ENOMEM;
2175 			secondary_data = secondary->plat_data->phy_data;
2176 
2177 			list_for_each_entry(mode, &connector->probed_modes, head)
2178 				hdmi->plat_data->convert_to_split_mode(mode);
2179 
2180 			secondary->sink_is_hdmi = drm_detect_hdmi_monitor(edid);
2181 			secondary->sink_has_audio = drm_detect_monitor_audio(edid);
2182 			if (secondary->cec_notifier)
2183 				cec_notifier_set_phys_addr_from_edid(secondary->cec_notifier,
2184 								     edid);
2185 			if (secondary->plat_data->get_edid_dsc_info)
2186 				secondary->plat_data->get_edid_dsc_info(secondary_data, edid);
2187 		}
2188 		kfree(edid);
2189 	} else {
2190 		hdmi->support_hdmi = true;
2191 		hdmi->sink_has_audio = true;
2192 
2193 		if (hdmi->plat_data->split_mode) {
2194 			if (hdmi->plat_data->left) {
2195 				hdmi->plat_data->left->sink_is_hdmi = true;
2196 				hdmi->plat_data->left->sink_has_audio = true;
2197 			} else if (hdmi->plat_data->right) {
2198 				hdmi->plat_data->right->sink_is_hdmi = true;
2199 				hdmi->plat_data->right->sink_has_audio = true;
2200 			}
2201 		}
2202 
2203 		for (i = 0; i < ARRAY_SIZE(dw_hdmi_default_modes); i++) {
2204 			const struct drm_display_mode *ptr =
2205 				&dw_hdmi_default_modes[i];
2206 
2207 			mode = drm_mode_duplicate(connector->dev, ptr);
2208 			if (mode) {
2209 				if (!i)
2210 					mode->type = DRM_MODE_TYPE_PREFERRED;
2211 				drm_mode_probed_add(connector, mode);
2212 				ret++;
2213 			}
2214 		}
2215 		if (ret > 0 && hdmi->plat_data->split_mode) {
2216 			struct drm_display_mode *mode;
2217 
2218 			list_for_each_entry(mode, &connector->probed_modes, head)
2219 				hdmi->plat_data->convert_to_split_mode(mode);
2220 		}
2221 		info->edid_hdmi_dc_modes = 0;
2222 		info->hdmi.y420_dc_modes = 0;
2223 		info->color_formats = 0;
2224 
2225 		dev_info(hdmi->dev, "failed to get edid\n");
2226 	}
2227 	dw_hdmi_qp_check_output_type_changed(hdmi);
2228 
2229 	return ret;
2230 }
2231 
dw_hdmi_qp_set_allm_enable(struct dw_hdmi_qp * hdmi,bool enable)2232 void dw_hdmi_qp_set_allm_enable(struct dw_hdmi_qp *hdmi, bool enable)
2233 {
2234 	struct dw_hdmi_link_config *link_cfg = NULL;
2235 	void *data;
2236 
2237 	if (!hdmi || !hdmi->curr_conn)
2238 		return;
2239 
2240 	data = hdmi->plat_data->phy_data;
2241 
2242 	if (hdmi->plat_data->get_link_cfg)
2243 		link_cfg = hdmi->plat_data->get_link_cfg(data);
2244 
2245 	if (!link_cfg)
2246 		return;
2247 
2248 	if (enable == hdmi->allm_enable)
2249 		return;
2250 
2251 	hdmi->allm_enable = enable;
2252 
2253 	if (enable && !(link_cfg->add_func & SUPPORT_HDMI_ALLM)) {
2254 		hdmi->allm_enable = false;
2255 		dev_err(hdmi->dev, "sink don't support allm, allm won't be enabled\n");
2256 		return;
2257 	}
2258 
2259 	hdmi_config_vendor_specific_infoframe(hdmi, hdmi->curr_conn, &hdmi->previous_mode);
2260 }
2261 EXPORT_SYMBOL_GPL(dw_hdmi_qp_set_allm_enable);
2262 
2263 static int
dw_hdmi_atomic_connector_set_property(struct drm_connector * connector,struct drm_connector_state * state,struct drm_property * property,uint64_t val)2264 dw_hdmi_atomic_connector_set_property(struct drm_connector *connector,
2265 				      struct drm_connector_state *state,
2266 				      struct drm_property *property,
2267 				      uint64_t val)
2268 {
2269 	struct dw_hdmi_qp *hdmi =
2270 		container_of(connector, struct dw_hdmi_qp, connector);
2271 	const struct dw_hdmi_property_ops *ops = hdmi->plat_data->property_ops;
2272 
2273 	if (ops && ops->set_property)
2274 		return ops->set_property(connector, state, property,
2275 					 val, hdmi->plat_data->phy_data);
2276 	else
2277 		return -EINVAL;
2278 }
2279 
2280 static int
dw_hdmi_atomic_connector_get_property(struct drm_connector * connector,const struct drm_connector_state * state,struct drm_property * property,uint64_t * val)2281 dw_hdmi_atomic_connector_get_property(struct drm_connector *connector,
2282 				      const struct drm_connector_state *state,
2283 				      struct drm_property *property,
2284 				      uint64_t *val)
2285 {
2286 	struct dw_hdmi_qp *hdmi =
2287 		container_of(connector, struct dw_hdmi_qp, connector);
2288 	const struct dw_hdmi_property_ops *ops = hdmi->plat_data->property_ops;
2289 
2290 	if (ops && ops->get_property)
2291 		return ops->get_property(connector, state, property,
2292 					 val, hdmi->plat_data->phy_data);
2293 	else
2294 		return -EINVAL;
2295 }
2296 
2297 static int
dw_hdmi_connector_set_property(struct drm_connector * connector,struct drm_property * property,uint64_t val)2298 dw_hdmi_connector_set_property(struct drm_connector *connector,
2299 			       struct drm_property *property, uint64_t val)
2300 {
2301 	return dw_hdmi_atomic_connector_set_property(connector, NULL,
2302 						     property, val);
2303 }
2304 
dw_hdmi_attach_properties(struct dw_hdmi_qp * hdmi)2305 static void dw_hdmi_attach_properties(struct dw_hdmi_qp *hdmi)
2306 {
2307 	u32 val;
2308 	u64 color = MEDIA_BUS_FMT_YUV8_1X24;
2309 	const struct dw_hdmi_property_ops *ops =
2310 				hdmi->plat_data->property_ops;
2311 	void *data = hdmi->plat_data->phy_data;
2312 	enum drm_connector_status connect_status =
2313 		hdmi->phy.ops->read_hpd(hdmi, hdmi->phy.data);
2314 
2315 	if ((connect_status == connector_status_connected) &&
2316 	    hdmi->initialized) {
2317 		if (hdmi->plat_data->get_grf_color_fmt)
2318 			color = hdmi->plat_data->get_grf_color_fmt(data);
2319 
2320 		val = (hdmi_readl(hdmi, PKT_VSI_CONTENTS1) >> 8) & 0xffffff;
2321 		if (val == HDMI_FORUM_OUI)
2322 			hdmi->allm_enable = true;
2323 		else
2324 			hdmi->allm_enable = false;
2325 	}
2326 
2327 	/*
2328 	 * Because all hdmi registers are configured the same value
2329 	 * between yuv422 8/10 bit. We set a useless bit in uboot to mark
2330 	 * yuv422 10bit.
2331 	 */
2332 	if (color == MEDIA_BUS_FMT_YUYV10_1X20 &&
2333 	    !(hdmi_readl(hdmi, VIDEO_INTERFACE_CONFIG0) & BIT(20)))
2334 		color = MEDIA_BUS_FMT_YUYV8_1X16;
2335 
2336 	if (ops && ops->attach_properties)
2337 		return ops->attach_properties(&hdmi->connector, color, 0,
2338 					      hdmi->plat_data->phy_data, hdmi->allm_enable);
2339 }
2340 
dw_hdmi_destroy_properties(struct dw_hdmi_qp * hdmi)2341 static void dw_hdmi_destroy_properties(struct dw_hdmi_qp *hdmi)
2342 {
2343 	const struct dw_hdmi_property_ops *ops =
2344 				hdmi->plat_data->property_ops;
2345 
2346 	if (ops && ops->destroy_properties)
2347 		return ops->destroy_properties(&hdmi->connector,
2348 					       hdmi->plat_data->phy_data);
2349 }
2350 
2351 static struct drm_encoder *
dw_hdmi_connector_best_encoder(struct drm_connector * connector)2352 dw_hdmi_connector_best_encoder(struct drm_connector *connector)
2353 {
2354 	struct dw_hdmi_qp *hdmi =
2355 		container_of(connector, struct dw_hdmi_qp, connector);
2356 
2357 	return hdmi->bridge.encoder;
2358 }
2359 
dw_hdmi_color_changed(struct drm_connector * connector,struct drm_atomic_state * state)2360 static bool dw_hdmi_color_changed(struct drm_connector *connector,
2361 				  struct drm_atomic_state *state)
2362 {
2363 	struct dw_hdmi_qp *hdmi =
2364 		container_of(connector, struct dw_hdmi_qp, connector);
2365 	void *data = hdmi->plat_data->phy_data;
2366 	struct drm_connector_state *old_state =
2367 		drm_atomic_get_old_connector_state(state, connector);
2368 	struct drm_connector_state *new_state =
2369 		drm_atomic_get_new_connector_state(state, connector);
2370 	bool ret = false;
2371 
2372 	if (hdmi->plat_data->get_color_changed)
2373 		ret = hdmi->plat_data->get_color_changed(data);
2374 
2375 	if (new_state->colorspace != old_state->colorspace)
2376 		ret = true;
2377 
2378 	return ret;
2379 }
2380 
hdr_metadata_equal(struct dw_hdmi_qp * hdmi,const struct drm_connector_state * old_state,const struct drm_connector_state * new_state)2381 static bool hdr_metadata_equal(struct dw_hdmi_qp *hdmi, const struct drm_connector_state *old_state,
2382 			       const struct drm_connector_state *new_state)
2383 {
2384 	struct drm_property_blob *old_blob = old_state->hdr_output_metadata;
2385 	struct drm_property_blob *new_blob = new_state->hdr_output_metadata;
2386 	int i, ret;
2387 	u8 *data;
2388 
2389 	hdmi->hdr2sdr = false;
2390 
2391 	if (!old_blob && !new_blob)
2392 		return true;
2393 
2394 	if (!old_blob) {
2395 		data = (u8 *)new_blob->data;
2396 
2397 		for (i = 0; i < new_blob->length; i++)
2398 			if (data[i])
2399 				return false;
2400 
2401 		return true;
2402 	}
2403 
2404 	if (!new_blob) {
2405 		data = (u8 *)old_blob->data;
2406 
2407 		for (i = 0; i < old_blob->length; i++)
2408 			if (data[i])
2409 				return false;
2410 
2411 		return true;
2412 	}
2413 
2414 	if (old_blob->length != new_blob->length)
2415 		return false;
2416 
2417 	ret = !memcmp(old_blob->data, new_blob->data, old_blob->length);
2418 
2419 	if (!ret && new_blob) {
2420 		data = (u8 *)new_blob->data;
2421 
2422 		for (i = 0; i < new_blob->length; i++)
2423 			if (data[i])
2424 				break;
2425 
2426 		if (i == new_blob->length)
2427 			hdmi->hdr2sdr = true;
2428 	}
2429 
2430 	return ret;
2431 }
2432 
check_hdr_color_change(struct drm_connector_state * old_state,struct drm_connector_state * new_state,struct dw_hdmi_qp * hdmi)2433 static bool check_hdr_color_change(struct drm_connector_state *old_state,
2434 				   struct drm_connector_state *new_state,
2435 				   struct dw_hdmi_qp *hdmi)
2436 {
2437 	void *data = hdmi->plat_data->phy_data;
2438 
2439 	if (!hdr_metadata_equal(hdmi, old_state, new_state)) {
2440 		hdmi->plat_data->check_hdr_color_change(new_state, data);
2441 		return true;
2442 	}
2443 
2444 	return false;
2445 }
2446 
dw_hdmi_connector_atomic_check(struct drm_connector * connector,struct drm_atomic_state * state)2447 static int dw_hdmi_connector_atomic_check(struct drm_connector *connector,
2448 					  struct drm_atomic_state *state)
2449 {
2450 	struct drm_connector_state *old_state =
2451 		drm_atomic_get_old_connector_state(state, connector);
2452 	struct drm_connector_state *new_state =
2453 		drm_atomic_get_new_connector_state(state, connector);
2454 	struct drm_crtc *crtc = new_state->crtc;
2455 	struct drm_crtc *old_crtc = old_state->crtc;
2456 	struct drm_crtc_state *crtc_state, *old_crtc_state;
2457 	struct dw_hdmi_qp *hdmi =
2458 		container_of(connector, struct dw_hdmi_qp, connector);
2459 	struct drm_display_mode mode;
2460 	void *data = hdmi->plat_data->phy_data;
2461 	struct hdmi_vmode_qp *vmode = &hdmi->hdmi_data.video_mode;
2462 
2463 	if (old_crtc) {
2464 		old_crtc_state = drm_atomic_get_crtc_state(state, old_crtc);
2465 		if (IS_ERR(old_crtc_state))
2466 			return PTR_ERR(old_crtc_state);
2467 
2468 		if (hdmi->plat_data->get_vp_id)
2469 			hdmi->old_vp_id = hdmi->plat_data->get_vp_id(old_crtc_state);
2470 	}
2471 
2472 	if (!crtc)
2473 		return 0;
2474 
2475 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
2476 	if (IS_ERR(crtc_state))
2477 		return PTR_ERR(crtc_state);
2478 
2479 	if (hdmi->plat_data->get_vp_id)
2480 		hdmi->vp_id = hdmi->plat_data->get_vp_id(crtc_state);
2481 
2482 	memcpy(&mode, &crtc_state->mode, sizeof(mode));
2483 	/*
2484 	 * If HDMI is enabled in uboot, it's need to record
2485 	 * drm_display_mode and set phy status to enabled.
2486 	 */
2487 	if (!vmode->mpixelclock) {
2488 		struct dw_hdmi_qp *secondary = NULL;
2489 		u8 val;
2490 
2491 		if (hdmi->plat_data->left)
2492 			secondary = hdmi->plat_data->left;
2493 		else if (hdmi->plat_data->right)
2494 			secondary = hdmi->plat_data->right;
2495 		hdmi->curr_conn = connector;
2496 		if (secondary)
2497 			secondary->curr_conn = connector;
2498 		if (hdmi->plat_data->get_enc_in_encoding)
2499 			hdmi->hdmi_data.enc_in_encoding =
2500 				hdmi->plat_data->get_enc_in_encoding(data);
2501 		if (hdmi->plat_data->get_enc_out_encoding)
2502 			hdmi->hdmi_data.enc_out_encoding =
2503 				hdmi->plat_data->get_enc_out_encoding(data);
2504 		if (hdmi->plat_data->get_input_bus_format)
2505 			hdmi->hdmi_data.enc_in_bus_format =
2506 				hdmi->plat_data->get_input_bus_format(data);
2507 		if (hdmi->plat_data->get_output_bus_format)
2508 			hdmi->hdmi_data.enc_out_bus_format =
2509 				hdmi->plat_data->get_output_bus_format(data);
2510 
2511 		if (hdmi->plat_data->split_mode) {
2512 			hdmi->plat_data->convert_to_origin_mode(&mode);
2513 			mode.crtc_clock /= 2;
2514 		}
2515 		memcpy(&hdmi->previous_mode, &mode, sizeof(hdmi->previous_mode));
2516 		vmode->mpixelclock = mode.crtc_clock * 1000;
2517 		vmode->previous_pixelclock = mode.clock;
2518 		vmode->previous_tmdsclock = mode.clock;
2519 		vmode->mtmdsclock = hdmi_get_tmdsclock(hdmi,
2520 						       vmode->mpixelclock);
2521 		if (hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format))
2522 			vmode->mtmdsclock /= 2;
2523 
2524 		/*
2525 		 * If uboot logo enabled, atomic_enable won't be called,
2526 		 * but atomic_disable will be called when hdmi plug out.
2527 		 * That will cause dclk enable count is incorrect. So
2528 		 * we should check ipi/link/video clk to determine whether
2529 		 * uboot logo is enabled.
2530 		 */
2531 		if (hdmi->initialized && !hdmi->dclk_en) {
2532 			mutex_lock(&hdmi->audio_mutex);
2533 			if (hdmi->plat_data->dclk_set)
2534 				hdmi->plat_data->dclk_set(data, true, hdmi->vp_id);
2535 			hdmi->dclk_en = true;
2536 			mutex_unlock(&hdmi->audio_mutex);
2537 			hdmi->curr_conn = connector;
2538 			extcon_set_state_sync(hdmi->extcon, EXTCON_DISP_HDMI, true);
2539 		}
2540 
2541 		drm_scdc_readb(hdmi->ddc, SCDC_TMDS_CONFIG, &val);
2542 		/* if plug out before hdmi bind, reset hdmi */
2543 		if (vmode->mtmdsclock >= 340000000 && vmode->mpixelclock <= 600000000 &&
2544 		    !(val & SCDC_TMDS_BIT_CLOCK_RATIO_BY_40))
2545 			hdmi->logo_plug_out = true;
2546 	}
2547 
2548 	if (check_hdr_color_change(old_state, new_state, hdmi) || hdmi->logo_plug_out ||
2549 	    dw_hdmi_color_changed(connector, state)) {
2550 		u32 mtmdsclk;
2551 
2552 		crtc_state = drm_atomic_get_crtc_state(state, crtc);
2553 		if (IS_ERR(crtc_state))
2554 			return PTR_ERR(crtc_state);
2555 
2556 		if (hdmi->plat_data->update_color_format)
2557 			hdmi->plat_data->update_color_format(new_state, data);
2558 		if (hdmi->plat_data->get_enc_in_encoding)
2559 			hdmi->hdmi_data.enc_in_encoding =
2560 				hdmi->plat_data->get_enc_in_encoding(data);
2561 		if (hdmi->plat_data->get_enc_out_encoding)
2562 			hdmi->hdmi_data.enc_out_encoding =
2563 				hdmi->plat_data->get_enc_out_encoding(data);
2564 		if (hdmi->plat_data->get_input_bus_format)
2565 			hdmi->hdmi_data.enc_in_bus_format =
2566 				hdmi->plat_data->get_input_bus_format(data);
2567 		if (hdmi->plat_data->get_output_bus_format)
2568 			hdmi->hdmi_data.enc_out_bus_format =
2569 				hdmi->plat_data->get_output_bus_format(data);
2570 
2571 		mtmdsclk = hdmi_get_tmdsclock(hdmi, mode.clock);
2572 		if (hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format))
2573 			mtmdsclk /= 2;
2574 
2575 		if (hdmi->hdmi_data.video_mode.mpixelclock == (mode.clock * 1000) &&
2576 		    hdmi->hdmi_data.video_mode.mtmdsclock == (mtmdsclk * 1000) &&
2577 		    mode.clock <= 600000 && !hdmi->disabled && !hdmi->logo_plug_out) {
2578 			hdmi->update = true;
2579 			hdmi_writel(hdmi, 1, PKTSCHED_PKT_CONTROL0);
2580 			hdmi_modb(hdmi, PKTSCHED_GCP_TX_EN, PKTSCHED_GCP_TX_EN, PKTSCHED_PKT_EN);
2581 			mdelay(50);
2582 		} else if (!hdmi->disabled) {
2583 			if (mode.clock > 600000)
2584 				hdmi->frl_switch = true;
2585 			hdmi->update = false;
2586 			crtc_state->mode_changed = true;
2587 			hdmi->logo_plug_out = false;
2588 		}
2589 	}
2590 
2591 	return 0;
2592 }
2593 
dw_hdmi_connector_atomic_commit(struct drm_connector * connector,struct drm_connector_state * state)2594 static void dw_hdmi_connector_atomic_commit(struct drm_connector *connector,
2595 					    struct drm_connector_state *state)
2596 {
2597 	struct dw_hdmi_qp *hdmi =
2598 		container_of(connector, struct dw_hdmi_qp, connector);
2599 
2600 	if (hdmi->update) {
2601 		dw_hdmi_qp_setup(hdmi, hdmi->curr_conn, &hdmi->previous_mode);
2602 		msleep(50);
2603 		hdmi_writel(hdmi, 2, PKTSCHED_PKT_CONTROL0);
2604 		hdmi->update = false;
2605 	}
2606 }
2607 
dw_hdmi_qp_set_output_type(struct dw_hdmi_qp * hdmi,u64 val)2608 void dw_hdmi_qp_set_output_type(struct dw_hdmi_qp *hdmi, u64 val)
2609 {
2610 	hdmi->force_output = val;
2611 
2612 	if (!dw_hdmi_qp_check_output_type_changed(hdmi))
2613 		return;
2614 
2615 	if (hdmi->disabled)
2616 		return;
2617 
2618 	if (!hdmi->sink_is_hdmi)
2619 		hdmi_modb(hdmi, OPMODE_DVI, OPMODE_DVI, LINK_CONFIG0);
2620 	else
2621 		hdmi_modb(hdmi, 0, OPMODE_DVI, LINK_CONFIG0);
2622 }
2623 EXPORT_SYMBOL_GPL(dw_hdmi_qp_set_output_type);
2624 
dw_hdmi_qp_get_output_whether_hdmi(struct dw_hdmi_qp * hdmi)2625 bool dw_hdmi_qp_get_output_whether_hdmi(struct dw_hdmi_qp *hdmi)
2626 {
2627 	return hdmi->sink_is_hdmi;
2628 }
2629 EXPORT_SYMBOL_GPL(dw_hdmi_qp_get_output_whether_hdmi);
2630 
dw_hdmi_qp_get_output_type_cap(struct dw_hdmi_qp * hdmi)2631 int dw_hdmi_qp_get_output_type_cap(struct dw_hdmi_qp *hdmi)
2632 {
2633 	return hdmi->support_hdmi;
2634 }
2635 EXPORT_SYMBOL_GPL(dw_hdmi_qp_get_output_type_cap);
2636 
dw_hdmi_connector_force(struct drm_connector * connector)2637 static void dw_hdmi_connector_force(struct drm_connector *connector)
2638 {
2639 	struct dw_hdmi_qp *hdmi =
2640 		container_of(connector, struct dw_hdmi_qp, connector);
2641 
2642 	mutex_lock(&hdmi->mutex);
2643 
2644 	if (hdmi->force != connector->force) {
2645 		if (!hdmi->disabled && connector->force == DRM_FORCE_OFF)
2646 			extcon_set_state_sync(hdmi->extcon, EXTCON_DISP_HDMI,
2647 					      false);
2648 		else if (hdmi->disabled && connector->force == DRM_FORCE_ON)
2649 			extcon_set_state_sync(hdmi->extcon, EXTCON_DISP_HDMI,
2650 					      true);
2651 	}
2652 
2653 	hdmi->force = connector->force;
2654 	mutex_unlock(&hdmi->mutex);
2655 }
2656 
2657 static const struct drm_connector_funcs dw_hdmi_connector_funcs = {
2658 	.fill_modes = drm_helper_probe_single_connector_modes,
2659 	.detect = dw_hdmi_connector_detect,
2660 	.destroy = drm_connector_cleanup,
2661 	.force = dw_hdmi_connector_force,
2662 	.reset = drm_atomic_helper_connector_reset,
2663 	.set_property = dw_hdmi_connector_set_property,
2664 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
2665 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
2666 	.atomic_set_property = dw_hdmi_atomic_connector_set_property,
2667 	.atomic_get_property = dw_hdmi_atomic_connector_get_property,
2668 };
2669 
2670 static const struct drm_connector_helper_funcs dw_hdmi_connector_helper_funcs = {
2671 	.get_modes = dw_hdmi_connector_get_modes,
2672 	.best_encoder = dw_hdmi_connector_best_encoder,
2673 	.atomic_check = dw_hdmi_connector_atomic_check,
2674 	.atomic_commit = dw_hdmi_connector_atomic_commit,
2675 };
2676 
dw_hdmi_qp_bridge_attach(struct drm_bridge * bridge,enum drm_bridge_attach_flags flags)2677 static int dw_hdmi_qp_bridge_attach(struct drm_bridge *bridge,
2678 				    enum drm_bridge_attach_flags flags)
2679 {
2680 	struct dw_hdmi_qp *hdmi = bridge->driver_private;
2681 	struct drm_encoder *encoder = bridge->encoder;
2682 	struct drm_connector *connector = &hdmi->connector;
2683 	struct cec_connector_info conn_info;
2684 	struct cec_notifier *notifier;
2685 
2686 	if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
2687 		return 0;
2688 
2689 	connector->interlace_allowed = 1;
2690 	connector->polled = DRM_CONNECTOR_POLL_HPD;
2691 
2692 	drm_connector_helper_add(connector, &dw_hdmi_connector_helper_funcs);
2693 
2694 	drm_connector_init(bridge->dev, connector, &dw_hdmi_connector_funcs,
2695 			   DRM_MODE_CONNECTOR_HDMIA);
2696 
2697 	drm_connector_attach_encoder(connector, encoder);
2698 	dw_hdmi_attach_properties(hdmi);
2699 
2700 	if (hdmi->cec_enable) {
2701 		cec_fill_conn_info_from_drm(&conn_info, connector);
2702 		notifier = cec_notifier_conn_register(hdmi->dev, NULL, &conn_info);
2703 		if (!notifier)
2704 			return -ENOMEM;
2705 
2706 		mutex_lock(&hdmi->cec_notifier_mutex);
2707 		hdmi->cec_notifier = notifier;
2708 		mutex_unlock(&hdmi->cec_notifier_mutex);
2709 	}
2710 
2711 	return 0;
2712 }
2713 
dw_hdmi_qp_bridge_detach(struct drm_bridge * bridge)2714 static void dw_hdmi_qp_bridge_detach(struct drm_bridge *bridge)
2715 {
2716 	struct dw_hdmi_qp *hdmi = bridge->driver_private;
2717 
2718 	if (hdmi->cec_notifier) {
2719 		mutex_lock(&hdmi->cec_notifier_mutex);
2720 		cec_notifier_conn_unregister(hdmi->cec_notifier);
2721 		hdmi->cec_notifier = NULL;
2722 		mutex_unlock(&hdmi->cec_notifier_mutex);
2723 	}
2724 }
2725 
2726 static enum drm_mode_status
dw_hdmi_qp_bridge_mode_valid(struct drm_bridge * bridge,const struct drm_display_info * info,const struct drm_display_mode * mode)2727 dw_hdmi_qp_bridge_mode_valid(struct drm_bridge *bridge,
2728 			     const struct drm_display_info *info,
2729 			     const struct drm_display_mode *mode)
2730 {
2731 	if (mode->clock <= 25000)
2732 		return MODE_CLOCK_RANGE;
2733 
2734 	return MODE_OK;
2735 }
2736 
dw_hdmi_qp_bridge_mode_set(struct drm_bridge * bridge,const struct drm_display_mode * orig_mode,const struct drm_display_mode * mode)2737 static void dw_hdmi_qp_bridge_mode_set(struct drm_bridge *bridge,
2738 				       const struct drm_display_mode *orig_mode,
2739 				       const struct drm_display_mode *mode)
2740 {
2741 	struct dw_hdmi_qp *hdmi = bridge->driver_private;
2742 
2743 	mutex_lock(&hdmi->mutex);
2744 
2745 	if (!drm_mode_equal(orig_mode, mode))
2746 		hdmi->frl_switch = false;
2747 	/* Store the display mode for plugin/DKMS poweron events */
2748 	memcpy(&hdmi->previous_mode, mode, sizeof(hdmi->previous_mode));
2749 	if (hdmi->plat_data->split_mode)
2750 		hdmi->plat_data->convert_to_origin_mode(&hdmi->previous_mode);
2751 
2752 	mutex_unlock(&hdmi->mutex);
2753 }
2754 
dw_hdmi_qp_bridge_atomic_disable(struct drm_bridge * bridge,struct drm_bridge_state * old_state)2755 static void dw_hdmi_qp_bridge_atomic_disable(struct drm_bridge *bridge,
2756 					     struct drm_bridge_state *old_state)
2757 {
2758 	struct dw_hdmi_qp *hdmi = bridge->driver_private;
2759 	void *data = hdmi->plat_data->phy_data;
2760 
2761 	if (hdmi->panel)
2762 		drm_panel_disable(hdmi->panel);
2763 
2764 	/* set avmute */
2765 	hdmi_writel(hdmi, 1, PKTSCHED_PKT_CONTROL0);
2766 	mdelay(50);
2767 
2768 	extcon_set_state_sync(hdmi->extcon, EXTCON_DISP_HDMI, false);
2769 	handle_plugged_change(hdmi, false);
2770 	mutex_lock(&hdmi->mutex);
2771 
2772 	if (hdmi->dclk_en) {
2773 		mutex_lock(&hdmi->audio_mutex);
2774 		if (hdmi->plat_data->dclk_set)
2775 			hdmi->plat_data->dclk_set(data, false, hdmi->old_vp_id);
2776 		hdmi->dclk_en = false;
2777 		mutex_unlock(&hdmi->audio_mutex);
2778 	};
2779 
2780 	if (hdmi->phy.ops->disable && !hdmi->frl_switch) {
2781 		hdmi_writel(hdmi, 0, FLT_CONFIG0);
2782 		hdmi_writel(hdmi, 0, SCRAMB_CONFIG0);
2783 		/* set sink frl mode disable */
2784 		if (hdmi->curr_conn && dw_hdmi_support_scdc(hdmi, &hdmi->curr_conn->display_info))
2785 			drm_scdc_writeb(hdmi->ddc, 0x31, 0);
2786 
2787 		hdmi->phy.ops->disable(hdmi, hdmi->phy.data);
2788 		hdmi->disabled = true;
2789 		if (hdmi->plat_data->link_clk_set)
2790 			hdmi->plat_data->link_clk_set(data, false);
2791 	}
2792 
2793 	hdmi->curr_conn = NULL;
2794 	mutex_unlock(&hdmi->mutex);
2795 
2796 	if (hdmi->panel)
2797 		drm_panel_unprepare(hdmi->panel);
2798 }
2799 
dw_hdmi_qp_bridge_atomic_enable(struct drm_bridge * bridge,struct drm_bridge_state * old_state)2800 static void dw_hdmi_qp_bridge_atomic_enable(struct drm_bridge *bridge,
2801 					    struct drm_bridge_state *old_state)
2802 {
2803 	struct dw_hdmi_qp *hdmi = bridge->driver_private;
2804 	struct drm_atomic_state *state = old_state->base.state;
2805 	struct drm_connector *connector;
2806 	void *data = hdmi->plat_data->phy_data;
2807 
2808 	if (hdmi->panel)
2809 		drm_panel_prepare(hdmi->panel);
2810 
2811 	connector = drm_atomic_get_new_connector_for_encoder(state,
2812 							     bridge->encoder);
2813 
2814 	mutex_lock(&hdmi->mutex);
2815 	hdmi->curr_conn = connector;
2816 
2817 	dw_hdmi_qp_setup(hdmi, hdmi->curr_conn, &hdmi->previous_mode);
2818 	hdmi_writel(hdmi, 2, PKTSCHED_PKT_CONTROL0);
2819 	hdmi_modb(hdmi, PKTSCHED_GCP_TX_EN, PKTSCHED_GCP_TX_EN, PKTSCHED_PKT_EN);
2820 	mutex_unlock(&hdmi->mutex);
2821 
2822 	if (!hdmi->dclk_en) {
2823 		mutex_lock(&hdmi->audio_mutex);
2824 		if (hdmi->plat_data->dclk_set)
2825 			hdmi->plat_data->dclk_set(data, true, hdmi->vp_id);
2826 		hdmi->dclk_en = true;
2827 		mutex_unlock(&hdmi->audio_mutex);
2828 	}
2829 	dw_hdmi_qp_init_audio_infoframe(hdmi);
2830 	dw_hdmi_qp_audio_enable(hdmi);
2831 	hdmi_clk_regenerator_update_pixel_clock(hdmi);
2832 
2833 	extcon_set_state_sync(hdmi->extcon, EXTCON_DISP_HDMI, true);
2834 	handle_plugged_change(hdmi, true);
2835 
2836 	if (hdmi->panel)
2837 		drm_panel_enable(hdmi->panel);
2838 }
2839 
2840 static const struct drm_bridge_funcs dw_hdmi_bridge_funcs = {
2841 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
2842 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
2843 	.atomic_reset = drm_atomic_helper_bridge_reset,
2844 	.attach = dw_hdmi_qp_bridge_attach,
2845 	.detach = dw_hdmi_qp_bridge_detach,
2846 	.mode_set = dw_hdmi_qp_bridge_mode_set,
2847 	.mode_valid = dw_hdmi_qp_bridge_mode_valid,
2848 	.atomic_enable = dw_hdmi_qp_bridge_atomic_enable,
2849 	.atomic_disable = dw_hdmi_qp_bridge_atomic_disable,
2850 };
2851 
dw_hdmi_qp_set_cec_adap(struct dw_hdmi_qp * hdmi,struct cec_adapter * adap)2852 void dw_hdmi_qp_set_cec_adap(struct dw_hdmi_qp *hdmi, struct cec_adapter *adap)
2853 {
2854 	hdmi->cec_adap = adap;
2855 }
2856 EXPORT_SYMBOL_GPL(dw_hdmi_qp_set_cec_adap);
2857 
dw_hdmi_qp_main_hardirq(int irq,void * dev_id)2858 static irqreturn_t dw_hdmi_qp_main_hardirq(int irq, void *dev_id)
2859 {
2860 	struct dw_hdmi_qp *hdmi = dev_id;
2861 	struct dw_hdmi_qp_i2c *i2c = hdmi->i2c;
2862 	u32 stat;
2863 
2864 	stat = hdmi_readl(hdmi, MAINUNIT_1_INT_STATUS);
2865 
2866 	i2c->stat = stat & (I2CM_OP_DONE_IRQ | I2CM_READ_REQUEST_IRQ |
2867 			    I2CM_NACK_RCVD_IRQ);
2868 	hdmi->scdc_intr = stat & (SCDC_UPD_FLAGS_RD_IRQ |
2869 				  SCDC_UPD_FLAGS_CHG_IRQ |
2870 				  SCDC_UPD_FLAGS_CLR_IRQ |
2871 				  SCDC_RR_REPLY_STOP_IRQ |
2872 				  SCDC_NACK_RCVD_IRQ);
2873 	hdmi->flt_intr = stat & (FLT_EXIT_TO_LTSP_IRQ |
2874 				 FLT_EXIT_TO_LTS4_IRQ |
2875 				 FLT_EXIT_TO_LTSL_IRQ);
2876 
2877 	dev_dbg(hdmi->dev, "i2c main unit irq:%#x\n", stat);
2878 	if (i2c->stat) {
2879 		hdmi_writel(hdmi, i2c->stat, MAINUNIT_1_INT_CLEAR);
2880 		complete(&i2c->cmp);
2881 	}
2882 
2883 	if (hdmi->flt_intr) {
2884 		dev_dbg(hdmi->dev, "i2c flt irq:%#x\n", hdmi->flt_intr);
2885 		hdmi_writel(hdmi, hdmi->flt_intr, MAINUNIT_1_INT_CLEAR);
2886 		complete(&hdmi->flt_cmp);
2887 	}
2888 
2889 	if (hdmi->scdc_intr) {
2890 		u8 val;
2891 
2892 		dev_dbg(hdmi->dev, "i2c scdc irq:%#x\n", hdmi->scdc_intr);
2893 		hdmi_writel(hdmi, hdmi->scdc_intr, MAINUNIT_1_INT_CLEAR);
2894 		val = hdmi_readl(hdmi, SCDC_STATUS0);
2895 
2896 		/* frl start */
2897 		if (val & BIT(4)) {
2898 			hdmi_modb(hdmi, 0, SCDC_UPD_FLAGS_POLL_EN |
2899 				  SCDC_UPD_FLAGS_AUTO_CLR, SCDC_CONFIG0);
2900 			hdmi_modb(hdmi, 0, SCDC_UPD_FLAGS_RD_IRQ,
2901 				  MAINUNIT_1_INT_MASK_N);
2902 			dev_info(hdmi->dev, "frl start\n");
2903 		}
2904 
2905 	}
2906 
2907 	if (stat)
2908 		return IRQ_HANDLED;
2909 
2910 	return IRQ_NONE;
2911 }
2912 
dw_hdmi_qp_avp_hardirq(int irq,void * dev_id)2913 static irqreturn_t dw_hdmi_qp_avp_hardirq(int irq, void *dev_id)
2914 {
2915 	struct dw_hdmi_qp *hdmi = dev_id;
2916 	u32 stat;
2917 
2918 	stat = hdmi_readl(hdmi, AVP_1_INT_STATUS);
2919 	if (stat) {
2920 		dev_dbg(hdmi->dev, "HDCP irq %#x\n", stat);
2921 		stat &= ~stat;
2922 		hdmi_writel(hdmi, stat, AVP_1_INT_MASK_N);
2923 		return IRQ_WAKE_THREAD;
2924 	}
2925 
2926 	return IRQ_NONE;
2927 }
2928 
dw_hdmi_qp_earc_hardirq(int irq,void * dev_id)2929 static irqreturn_t dw_hdmi_qp_earc_hardirq(int irq, void *dev_id)
2930 {
2931 	struct dw_hdmi_qp *hdmi = dev_id;
2932 	u32 stat;
2933 
2934 	stat = hdmi_readl(hdmi, EARCRX_0_INT_STATUS);
2935 	if (stat) {
2936 		dev_dbg(hdmi->dev, "earc irq %#x\n", stat);
2937 		stat &= ~stat;
2938 		hdmi_writel(hdmi, stat, EARCRX_0_INT_MASK_N);
2939 		return IRQ_WAKE_THREAD;
2940 	}
2941 
2942 	return IRQ_NONE;
2943 }
2944 
dw_hdmi_qp_avp_irq(int irq,void * dev_id)2945 static irqreturn_t dw_hdmi_qp_avp_irq(int irq, void *dev_id)
2946 {
2947 	struct dw_hdmi_qp *hdmi = dev_id;
2948 	u32 stat;
2949 
2950 	stat = hdmi_readl(hdmi, AVP_1_INT_STATUS);
2951 
2952 	if (!stat)
2953 		return IRQ_NONE;
2954 
2955 	hdmi_writel(hdmi, stat, AVP_1_INT_CLEAR);
2956 
2957 	return IRQ_HANDLED;
2958 }
2959 
dw_hdmi_qp_earc_irq(int irq,void * dev_id)2960 static irqreturn_t dw_hdmi_qp_earc_irq(int irq, void *dev_id)
2961 {
2962 	struct dw_hdmi_qp *hdmi = dev_id;
2963 	u32 stat;
2964 
2965 	stat = hdmi_readl(hdmi, EARCRX_0_INT_STATUS);
2966 
2967 	if (!stat)
2968 		return IRQ_NONE;
2969 
2970 	hdmi_writel(hdmi, stat, EARCRX_0_INT_CLEAR);
2971 
2972 	hdmi->earc_intr = stat;
2973 	complete(&hdmi->earc_cmp);
2974 
2975 	return IRQ_HANDLED;
2976 }
2977 
dw_hdmi_detect_phy(struct dw_hdmi_qp * hdmi)2978 static int dw_hdmi_detect_phy(struct dw_hdmi_qp *hdmi)
2979 {
2980 	u8 phy_type;
2981 
2982 	phy_type = hdmi->plat_data->phy_force_vendor ?
2983 				DW_HDMI_PHY_VENDOR_PHY : 0;
2984 
2985 	if (phy_type == DW_HDMI_PHY_VENDOR_PHY) {
2986 		/* Vendor PHYs require support from the glue layer. */
2987 		if (!hdmi->plat_data->qp_phy_ops || !hdmi->plat_data->phy_name) {
2988 			dev_err(hdmi->dev,
2989 				"Vendor HDMI PHY not supported by glue layer\n");
2990 			return -ENODEV;
2991 		}
2992 
2993 		hdmi->phy.ops = hdmi->plat_data->qp_phy_ops;
2994 		hdmi->phy.data = hdmi->plat_data->phy_data;
2995 		hdmi->phy.name = hdmi->plat_data->phy_name;
2996 	}
2997 
2998 	return 0;
2999 }
3000 
dw_hdmi_qp_cec_set_hpd(struct dw_hdmi_qp * hdmi,bool plug_in,bool change)3001 void dw_hdmi_qp_cec_set_hpd(struct dw_hdmi_qp *hdmi, bool plug_in, bool change)
3002 {
3003 	enum drm_connector_status status = plug_in ?
3004 		connector_status_connected : connector_status_disconnected;
3005 
3006 	if (!hdmi->cec_notifier)
3007 		return;
3008 
3009 	if (!plug_in)
3010 		cec_notifier_set_phys_addr(hdmi->cec_notifier,
3011 					   CEC_PHYS_ADDR_INVALID);
3012 
3013 	if (hdmi->bridge.dev) {
3014 		if (change && hdmi->cec_adap && hdmi->cec_adap->devnode.registered)
3015 			cec_queue_pin_hpd_event(hdmi->cec_adap, plug_in, ktime_get());
3016 		drm_bridge_hpd_notify(&hdmi->bridge, status);
3017 	}
3018 }
3019 EXPORT_SYMBOL_GPL(dw_hdmi_qp_cec_set_hpd);
3020 
dw_hdmi_qp_cec_enable(struct dw_hdmi_qp * hdmi)3021 static void dw_hdmi_qp_cec_enable(struct dw_hdmi_qp *hdmi)
3022 {
3023 	mutex_lock(&hdmi->mutex);
3024 	hdmi_modb(hdmi, 0, CEC_SWDISABLE, GLOBAL_SWDISABLE);
3025 	mutex_unlock(&hdmi->mutex);
3026 }
3027 
dw_hdmi_qp_cec_disable(struct dw_hdmi_qp * hdmi)3028 static void dw_hdmi_qp_cec_disable(struct dw_hdmi_qp *hdmi)
3029 {
3030 	mutex_lock(&hdmi->mutex);
3031 	hdmi_modb(hdmi, CEC_SWDISABLE, CEC_SWDISABLE, GLOBAL_SWDISABLE);
3032 	mutex_unlock(&hdmi->mutex);
3033 }
3034 
3035 static const struct dw_hdmi_qp_cec_ops dw_hdmi_qp_cec_ops = {
3036 	.enable = dw_hdmi_qp_cec_enable,
3037 	.disable = dw_hdmi_qp_cec_disable,
3038 	.write = hdmi_writel,
3039 	.read = hdmi_readl,
3040 };
3041 
3042 static const struct regmap_config hdmi_regmap_config = {
3043 	.reg_bits	= 32,
3044 	.val_bits	= 32,
3045 	.reg_stride	= 4,
3046 	.max_register	= EARCRX_1_INT_FORCE,
3047 };
3048 
3049 struct dw_hdmi_qp_reg_table {
3050 	int reg_base;
3051 	int reg_end;
3052 };
3053 
3054 static const struct dw_hdmi_qp_reg_table hdmi_reg_table[] = {
3055 	{0x0, 0xc},
3056 	{0x14, 0x1c},
3057 	{0x44, 0x48},
3058 	{0x50, 0x58},
3059 	{0x80, 0x84},
3060 	{0xa0, 0xc4},
3061 	{0xe0, 0xe8},
3062 	{0xf0, 0x118},
3063 	{0x140, 0x140},
3064 	{0x150, 0x150},
3065 	{0x160, 0x168},
3066 	{0x180, 0x180},
3067 	{0x800, 0x800},
3068 	{0x808, 0x808},
3069 	{0x814, 0x814},
3070 	{0x81c, 0x824},
3071 	{0x834, 0x834},
3072 	{0x840, 0x864},
3073 	{0x86c, 0x86c},
3074 	{0x880, 0x89c},
3075 	{0x8e0, 0x8e8},
3076 	{0x900, 0x900},
3077 	{0x908, 0x90c},
3078 	{0x920, 0x938},
3079 	{0x920, 0x938},
3080 	{0x960, 0x960},
3081 	{0x968, 0x968},
3082 	{0xa20, 0xa20},
3083 	{0xa30, 0xa30},
3084 	{0xa40, 0xa40},
3085 	{0xa54, 0xa54},
3086 	{0xa80, 0xaac},
3087 	{0xab4, 0xab8},
3088 	{0xb00, 0xcbc},
3089 	{0xce0, 0xce0},
3090 	{0xd00, 0xddc},
3091 	{0xe20, 0xe24},
3092 	{0xe40, 0xe44},
3093 	{0xe4c, 0xe4c},
3094 	{0xe60, 0xe80},
3095 	{0xea0, 0xf24},
3096 	{0x1004, 0x100c},
3097 	{0x1020, 0x1030},
3098 	{0x1040, 0x1050},
3099 	{0x1060, 0x1068},
3100 	{0x1800, 0x1820},
3101 	{0x182c, 0x182c},
3102 	{0x1840, 0x1940},
3103 	{0x1960, 0x1a60},
3104 	{0x1b00, 0x1b00},
3105 	{0x1c00, 0x1c00},
3106 	{0x3000, 0x3000},
3107 	{0x3010, 0x3014},
3108 	{0x3020, 0x3024},
3109 	{0x3800, 0x3800},
3110 	{0x3810, 0x3814},
3111 	{0x3820, 0x3824},
3112 	{0x3830, 0x3834},
3113 	{0x3840, 0x3844},
3114 	{0x3850, 0x3854},
3115 	{0x3860, 0x3864},
3116 	{0x3870, 0x3874},
3117 	{0x4000, 0x4004},
3118 	{0x4800, 0x4800},
3119 	{0x4810, 0x4814},
3120 };
3121 
dw_hdmi_ctrl_show(struct seq_file * s,void * v)3122 static int dw_hdmi_ctrl_show(struct seq_file *s, void *v)
3123 {
3124 	struct dw_hdmi_qp *hdmi = s->private;
3125 	u32 i = 0, j = 0, val = 0;
3126 
3127 	seq_puts(s, "\n---------------------------------------------------");
3128 
3129 	for (i = 0; i < ARRAY_SIZE(hdmi_reg_table); i++) {
3130 		for (j = hdmi_reg_table[i].reg_base;
3131 		     j <= hdmi_reg_table[i].reg_end; j += 4) {
3132 			val = hdmi_readl(hdmi, j);
3133 
3134 			if ((j - hdmi_reg_table[i].reg_base) % 16 == 0)
3135 				seq_printf(s, "\n>>>hdmi_ctl %04x:", j);
3136 			seq_printf(s, " %08x", val);
3137 		}
3138 	}
3139 	seq_puts(s, "\n---------------------------------------------------\n");
3140 
3141 	return 0;
3142 }
3143 
dw_hdmi_ctrl_open(struct inode * inode,struct file * file)3144 static int dw_hdmi_ctrl_open(struct inode *inode, struct file *file)
3145 {
3146 	return single_open(file, dw_hdmi_ctrl_show, inode->i_private);
3147 }
3148 
3149 static ssize_t
dw_hdmi_ctrl_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)3150 dw_hdmi_ctrl_write(struct file *file, const char __user *buf,
3151 		   size_t count, loff_t *ppos)
3152 {
3153 	struct dw_hdmi_qp *hdmi =
3154 		((struct seq_file *)file->private_data)->private;
3155 	u32 reg, val;
3156 	char kbuf[25];
3157 
3158 	if (count > 24) {
3159 		dev_err(hdmi->dev, "out of buf range\n");
3160 		return count;
3161 	}
3162 
3163 	if (copy_from_user(kbuf, buf, count))
3164 		return -EFAULT;
3165 	kbuf[count - 1] = '\0';
3166 
3167 	if (sscanf(kbuf, "%x %x", &reg, &val) == -1)
3168 		return -EFAULT;
3169 	if (reg > EARCRX_1_INT_FORCE) {
3170 		dev_err(hdmi->dev, "it is no a hdmi register\n");
3171 		return count;
3172 	}
3173 	dev_info(hdmi->dev, "/**********hdmi register config******/");
3174 	dev_info(hdmi->dev, "\n reg=%x val=%x\n", reg, val);
3175 	hdmi_writel(hdmi, val, reg);
3176 	return count;
3177 }
3178 
3179 static const struct file_operations dw_hdmi_ctrl_fops = {
3180 	.owner = THIS_MODULE,
3181 	.open = dw_hdmi_ctrl_open,
3182 	.read = seq_read,
3183 	.write = dw_hdmi_ctrl_write,
3184 	.llseek = seq_lseek,
3185 	.release = single_release,
3186 };
3187 
dw_hdmi_status_show(struct seq_file * s,void * v)3188 static int dw_hdmi_status_show(struct seq_file *s, void *v)
3189 {
3190 	struct dw_hdmi_qp *hdmi = s->private;
3191 	u32 val;
3192 
3193 	seq_puts(s, "PHY: ");
3194 	if (hdmi->disabled) {
3195 		seq_puts(s, "disabled\n");
3196 		return 0;
3197 	}
3198 	seq_puts(s, "enabled\t\t\tMode: ");
3199 	if (hdmi->sink_is_hdmi)
3200 		seq_puts(s, "HDMI\n");
3201 	else
3202 		seq_puts(s, "DVI\n");
3203 
3204 	if (hdmi->hdmi_data.video_mode.mpixelclock > 600000000) {
3205 		seq_printf(s, "FRL Mode Pixel Clk: %luHz\n",
3206 			   hdmi->hdmi_data.video_mode.mpixelclock);
3207 	} else {
3208 		if (hdmi->hdmi_data.video_mode.mtmdsclock > 340000000)
3209 			val = hdmi->hdmi_data.video_mode.mtmdsclock / 4;
3210 		else
3211 			val = hdmi->hdmi_data.video_mode.mtmdsclock;
3212 		seq_printf(s, "TMDS Mode Pixel Clk: %luHz\t\tTMDS Clk: %uHz\n",
3213 			   hdmi->hdmi_data.video_mode.mpixelclock, val);
3214 	}
3215 	seq_printf(s, "ALLM: %d\n", hdmi->allm_enable);
3216 	seq_puts(s, "Color Format: ");
3217 	if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format))
3218 		seq_puts(s, "RGB");
3219 	else if (hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format))
3220 		seq_puts(s, "YUV444");
3221 	else if (hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format))
3222 		seq_puts(s, "YUV422");
3223 	else if (hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format))
3224 		seq_puts(s, "YUV420");
3225 	else
3226 		seq_puts(s, "UNKNOWN");
3227 	val =  hdmi_bus_fmt_color_depth(hdmi->hdmi_data.enc_out_bus_format);
3228 	seq_printf(s, "\t\tColor Depth: %d bit\n", val);
3229 	seq_puts(s, "Colorimetry: ");
3230 	switch (hdmi->hdmi_data.enc_out_encoding) {
3231 	case V4L2_YCBCR_ENC_601:
3232 		seq_puts(s, "ITU.BT601");
3233 		break;
3234 	case V4L2_YCBCR_ENC_709:
3235 		seq_puts(s, "ITU.BT709");
3236 		break;
3237 	case V4L2_YCBCR_ENC_BT2020:
3238 		seq_puts(s, "ITU.BT2020");
3239 		break;
3240 	default: /* Carries no data */
3241 		seq_puts(s, "ITU.BT601");
3242 		break;
3243 	}
3244 
3245 	seq_puts(s, "\t\tEOTF: ");
3246 
3247 	val = hdmi_readl(hdmi, PKTSCHED_PKT_EN);
3248 	if (!(val & PKTSCHED_DRMI_TX_EN)) {
3249 		seq_puts(s, "Off\n");
3250 		return 0;
3251 	}
3252 
3253 	val = hdmi_readl(hdmi, PKT_DRMI_CONTENTS1);
3254 	val = (val >> 8) & 0x7;
3255 	switch (val) {
3256 	case HDMI_EOTF_TRADITIONAL_GAMMA_SDR:
3257 		seq_puts(s, "SDR");
3258 		break;
3259 	case HDMI_EOTF_TRADITIONAL_GAMMA_HDR:
3260 		seq_puts(s, "HDR");
3261 		break;
3262 	case HDMI_EOTF_SMPTE_ST2084:
3263 		seq_puts(s, "ST2084");
3264 		break;
3265 	case HDMI_EOTF_BT_2100_HLG:
3266 		seq_puts(s, "HLG");
3267 		break;
3268 	default:
3269 		seq_puts(s, "Not Defined\n");
3270 		return 0;
3271 	}
3272 
3273 	val = hdmi_readl(hdmi, PKT_DRMI_CONTENTS1);
3274 	val = (val >> 16) & 0xffff;
3275 	seq_printf(s, "\nx0: %d", val);
3276 	val = hdmi_readl(hdmi, PKT_DRMI_CONTENTS2);
3277 	val = val & 0xffff;
3278 	seq_printf(s, "\t\t\t\ty0: %d\n", val);
3279 	val = hdmi_readl(hdmi, PKT_DRMI_CONTENTS2);
3280 	val = (val >> 16) & 0xffff;
3281 	seq_printf(s, "x1: %d", val);
3282 	val = hdmi_readl(hdmi, PKT_DRMI_CONTENTS3);
3283 	val = val & 0xffff;
3284 	seq_printf(s, "\t\t\t\ty1: %d\n", val);
3285 	val = hdmi_readl(hdmi, PKT_DRMI_CONTENTS3);
3286 	val = (val >> 16) & 0xffff;
3287 	seq_printf(s, "x2: %d", val);
3288 	val = hdmi_readl(hdmi, PKT_DRMI_CONTENTS4);
3289 	val = val & 0xffff;
3290 	seq_printf(s, "\t\t\t\ty2: %d\n", val);
3291 	val = hdmi_readl(hdmi, PKT_DRMI_CONTENTS4);
3292 	val = (val >> 16) & 0xffff;
3293 	seq_printf(s, "white x: %d", val);
3294 	val = hdmi_readl(hdmi, PKT_DRMI_CONTENTS5);
3295 	val = val & 0xffff;
3296 	seq_printf(s, "\t\t\twhite y: %d\n", val);
3297 	val = hdmi_readl(hdmi, PKT_DRMI_CONTENTS5);
3298 	val = (val >> 16) & 0xffff;
3299 	seq_printf(s, "max lum: %d", val);
3300 	val = hdmi_readl(hdmi, PKT_DRMI_CONTENTS6);
3301 	val = val & 0xffff;
3302 	seq_printf(s, "\t\t\tmin lum: %d\n", val);
3303 	val = hdmi_readl(hdmi, PKT_DRMI_CONTENTS6);
3304 	val = (val >> 16) & 0xffff;
3305 	seq_printf(s, "max cll: %d", val);
3306 	val = hdmi_readl(hdmi, PKT_DRMI_CONTENTS7);
3307 	val = val & 0xffff;
3308 	seq_printf(s, "\t\t\tmax fall: %d\n", val);
3309 	return 0;
3310 }
3311 
dw_hdmi_status_open(struct inode * inode,struct file * file)3312 static int dw_hdmi_status_open(struct inode *inode, struct file *file)
3313 {
3314 	return single_open(file, dw_hdmi_status_show, inode->i_private);
3315 }
3316 
3317 static const struct file_operations dw_hdmi_status_fops = {
3318 	.owner = THIS_MODULE,
3319 	.open = dw_hdmi_status_open,
3320 	.read = seq_read,
3321 	.llseek = seq_lseek,
3322 	.release = single_release,
3323 };
3324 
dw_hdmi_register_debugfs(struct device * dev,struct dw_hdmi_qp * hdmi)3325 static void dw_hdmi_register_debugfs(struct device *dev, struct dw_hdmi_qp *hdmi)
3326 {
3327 	u8 buf[11];
3328 
3329 	snprintf(buf, sizeof(buf), "dw-hdmi%d", hdmi->plat_data->id);
3330 	hdmi->debugfs_dir = debugfs_create_dir(buf, NULL);
3331 	if (IS_ERR(hdmi->debugfs_dir)) {
3332 		dev_err(dev, "failed to create debugfs dir!\n");
3333 		return;
3334 	}
3335 
3336 	debugfs_create_file("status", 0400, hdmi->debugfs_dir,
3337 			    hdmi, &dw_hdmi_status_fops);
3338 	debugfs_create_file("ctrl", 0600, hdmi->debugfs_dir,
3339 			    hdmi, &dw_hdmi_ctrl_fops);
3340 }
3341 
3342 static struct dw_hdmi_qp *
__dw_hdmi_probe(struct platform_device * pdev,const struct dw_hdmi_plat_data * plat_data)3343 __dw_hdmi_probe(struct platform_device *pdev,
3344 		const struct dw_hdmi_plat_data *plat_data)
3345 {
3346 	struct device *dev = &pdev->dev;
3347 	struct device_node *np = dev->of_node;
3348 	struct device_node *ddc_node;
3349 	struct dw_hdmi_qp *hdmi;
3350 	struct dw_hdmi_qp_i2s_audio_data audio;
3351 	struct platform_device_info pdevinfo;
3352 	struct dw_hdmi_qp_cec_data cec;
3353 	struct resource *iores = NULL;
3354 	struct drm_panel *panel = NULL;
3355 	int irq;
3356 	int ret;
3357 
3358 	ret = drm_of_find_panel_or_bridge(np, 1, -1, &panel, NULL);
3359 	if (ret < 0 && ret != -ENODEV)
3360 		return ERR_PTR(ret);
3361 
3362 	hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
3363 	if (!hdmi)
3364 		return ERR_PTR(-ENOMEM);
3365 
3366 	hdmi->panel = panel;
3367 	hdmi->connector.stereo_allowed = 1;
3368 	hdmi->plat_data = plat_data;
3369 	hdmi->dev = dev;
3370 	hdmi->sample_rate = 48000;
3371 	hdmi->disabled = true;
3372 
3373 	mutex_init(&hdmi->mutex);
3374 	mutex_init(&hdmi->audio_mutex);
3375 	mutex_init(&hdmi->cec_notifier_mutex);
3376 
3377 	ddc_node = of_parse_phandle(np, "ddc-i2c-bus", 0);
3378 	if (ddc_node) {
3379 		hdmi->ddc = of_get_i2c_adapter_by_node(ddc_node);
3380 		of_node_put(ddc_node);
3381 		if (!hdmi->ddc) {
3382 			dev_dbg(hdmi->dev, "failed to read ddc node\n");
3383 			return ERR_PTR(-EPROBE_DEFER);
3384 		}
3385 
3386 	} else {
3387 		dev_dbg(hdmi->dev, "no ddc property found\n");
3388 	}
3389 
3390 	if (!plat_data->regm) {
3391 		const struct regmap_config *reg_config;
3392 
3393 		reg_config = &hdmi_regmap_config;
3394 
3395 		iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3396 		hdmi->regs = devm_ioremap_resource(dev, iores);
3397 		if (IS_ERR(hdmi->regs)) {
3398 			ret = PTR_ERR(hdmi->regs);
3399 			goto err_ddc;
3400 		}
3401 
3402 		hdmi->regm = devm_regmap_init_mmio(dev, hdmi->regs, reg_config);
3403 		if (IS_ERR(hdmi->regm)) {
3404 			dev_err(dev, "Failed to configure regmap\n");
3405 			ret = PTR_ERR(hdmi->regm);
3406 			goto err_ddc;
3407 		}
3408 	} else {
3409 		hdmi->regm = plat_data->regm;
3410 	}
3411 
3412 	ret = dw_hdmi_detect_phy(hdmi);
3413 	if (ret < 0)
3414 		goto err_ddc;
3415 
3416 	hdmi_writel(hdmi, 0, MAINUNIT_0_INT_MASK_N);
3417 	hdmi_writel(hdmi, 0, MAINUNIT_1_INT_MASK_N);
3418 	hdmi_writel(hdmi, 428571429, TIMER_BASE_CONFIG0);
3419 	hdmi->logo_plug_out = false;
3420 	if (hdmi->phy.ops->read_hpd(hdmi, hdmi->phy.data) == connector_status_connected &&
3421 	    hdmi_readl(hdmi, I2CM_INTERFACE_CONTROL0)) {
3422 		hdmi->initialized = true;
3423 		hdmi->disabled = false;
3424 	}
3425 
3426 	hdmi->sink_is_hdmi = true;
3427 
3428 	/* If DDC bus is not specified, try to register HDMI I2C bus */
3429 	if (!hdmi->ddc) {
3430 		hdmi->ddc = dw_hdmi_i2c_adapter(hdmi);
3431 		if (IS_ERR(hdmi->ddc))
3432 			hdmi->ddc = NULL;
3433 		/*
3434 		 * Read high and low time from device tree. If not available use
3435 		 * the default timing scl clock rate is about 99.6KHz.
3436 		 */
3437 		if (of_property_read_u32(np, "ddc-i2c-scl-high-time-ns",
3438 					 &hdmi->i2c->scl_high_ns))
3439 			hdmi->i2c->scl_high_ns = 4708;
3440 		if (of_property_read_u32(np, "ddc-i2c-scl-low-time-ns",
3441 					 &hdmi->i2c->scl_low_ns))
3442 			hdmi->i2c->scl_low_ns = 4916;
3443 	}
3444 
3445 	/* Reset HDMI DDC I2C master controller and mute I2CM interrupts */
3446 	if (hdmi->i2c)
3447 		dw_hdmi_i2c_init(hdmi);
3448 
3449 	init_completion(&hdmi->flt_cmp);
3450 	init_completion(&hdmi->earc_cmp);
3451 
3452 	if (of_property_read_bool(np, "scramble-low-rates"))
3453 		hdmi->scramble_low_rates = true;
3454 
3455 	hdmi_init_clk_regenerator(hdmi);
3456 
3457 	hdmi->bridge.driver_private = hdmi;
3458 	hdmi->bridge.funcs = &dw_hdmi_bridge_funcs;
3459 #ifdef CONFIG_OF
3460 	hdmi->bridge.of_node = pdev->dev.of_node;
3461 #endif
3462 
3463 	if (hdmi->phy.ops->setup_hpd)
3464 		hdmi->phy.ops->setup_hpd(hdmi, hdmi->phy.data);
3465 
3466 	hdmi->connector.ycbcr_420_allowed = hdmi->plat_data->ycbcr_420_allowed;
3467 
3468 	audio.hdmi	= hdmi;
3469 	audio.eld	= hdmi->connector.eld;
3470 	audio.write	= hdmi_writel;
3471 	audio.read	= hdmi_readl;
3472 	audio.mod	= hdmi_modb;
3473 	hdmi->enable_audio = dw_hdmi_i2s_audio_enable;
3474 	hdmi->disable_audio = dw_hdmi_i2s_audio_disable;
3475 
3476 	memset(&pdevinfo, 0, sizeof(pdevinfo));
3477 	pdevinfo.parent = dev;
3478 	pdevinfo.id = PLATFORM_DEVID_AUTO;
3479 	pdevinfo.name = "dw-hdmi-qp-i2s-audio";
3480 	pdevinfo.data = &audio;
3481 	pdevinfo.size_data = sizeof(audio);
3482 	pdevinfo.dma_mask = DMA_BIT_MASK(32);
3483 	hdmi->audio = platform_device_register_full(&pdevinfo);
3484 
3485 	hdmi->extcon = devm_extcon_dev_allocate(hdmi->dev, dw_hdmi_cable);
3486 	if (IS_ERR(hdmi->extcon)) {
3487 		dev_err(hdmi->dev, "allocate extcon failed\n");
3488 		ret = PTR_ERR(hdmi->extcon);
3489 		goto err_aud;
3490 	}
3491 
3492 	ret = devm_extcon_dev_register(hdmi->dev, hdmi->extcon);
3493 	if (ret) {
3494 		dev_err(hdmi->dev, "failed to register extcon: %d\n", ret);
3495 		goto err_aud;
3496 	}
3497 
3498 	ret = extcon_set_property_capability(hdmi->extcon, EXTCON_DISP_HDMI,
3499 					     EXTCON_PROP_DISP_HPD);
3500 	if (ret) {
3501 		dev_err(hdmi->dev,
3502 			"failed to set USB property capability: %d\n", ret);
3503 		goto err_aud;
3504 	}
3505 
3506 	irq = platform_get_irq(pdev, 0);
3507 	if (irq < 0) {
3508 		ret = irq;
3509 		goto err_aud;
3510 	}
3511 
3512 	hdmi->avp_irq = irq;
3513 	ret = devm_request_threaded_irq(dev, hdmi->avp_irq,
3514 					dw_hdmi_qp_avp_hardirq,
3515 					dw_hdmi_qp_avp_irq, IRQF_SHARED,
3516 					dev_name(dev), hdmi);
3517 	if (ret)
3518 		goto err_aud;
3519 
3520 	irq = platform_get_irq(pdev, 1);
3521 	if (irq < 0) {
3522 		ret = irq;
3523 		goto err_aud;
3524 	}
3525 
3526 	cec.irq = irq;
3527 
3528 	if (of_property_read_bool(np, "cec-enable")) {
3529 		hdmi->cec_enable = true;
3530 		cec.hdmi = hdmi;
3531 		cec.ops = &dw_hdmi_qp_cec_ops;
3532 		pdevinfo.name = "dw-hdmi-qp-cec";
3533 		pdevinfo.data = &cec;
3534 		pdevinfo.size_data = sizeof(cec);
3535 		pdevinfo.dma_mask = 0;
3536 		hdmi->cec = platform_device_register_full(&pdevinfo);
3537 	}
3538 
3539 	irq = platform_get_irq(pdev, 2);
3540 	if (irq < 0) {
3541 		ret = irq;
3542 		goto err_cec;
3543 	}
3544 
3545 	hdmi->earc_irq = irq;
3546 	ret = devm_request_threaded_irq(dev, hdmi->earc_irq,
3547 					dw_hdmi_qp_earc_hardirq,
3548 					dw_hdmi_qp_earc_irq, IRQF_SHARED,
3549 					dev_name(dev), hdmi);
3550 	if (ret)
3551 		goto err_cec;
3552 
3553 	irq = platform_get_irq(pdev, 3);
3554 	if (irq < 0) {
3555 		ret = irq;
3556 		goto err_cec;
3557 	}
3558 
3559 	hdmi->main_irq = irq;
3560 	ret = devm_request_threaded_irq(dev, hdmi->main_irq,
3561 					dw_hdmi_qp_main_hardirq, NULL,
3562 					IRQF_SHARED, dev_name(dev), hdmi);
3563 	if (ret)
3564 		goto err_cec;
3565 
3566 	dw_hdmi_register_debugfs(dev, hdmi);
3567 
3568 	return hdmi;
3569 
3570 err_cec:
3571 	if (!IS_ERR(hdmi->cec))
3572 		platform_device_unregister(hdmi->cec);
3573 
3574 err_aud:
3575 	if (hdmi->audio && !IS_ERR(hdmi->audio))
3576 		platform_device_unregister(hdmi->audio);
3577 
3578 err_ddc:
3579 	if (hdmi->i2c)
3580 		i2c_del_adapter(&hdmi->i2c->adap);
3581 	else
3582 		i2c_put_adapter(hdmi->ddc);
3583 
3584 	if (!hdmi->plat_data->first_screen) {
3585 		dw_hdmi_destroy_properties(hdmi);
3586 		hdmi->connector.funcs->destroy(&hdmi->connector);
3587 	}
3588 
3589 	if (hdmi->bridge.encoder && !hdmi->plat_data->first_screen)
3590 		hdmi->bridge.encoder->funcs->destroy(hdmi->bridge.encoder);
3591 
3592 	return ERR_PTR(ret);
3593 }
3594 
__dw_hdmi_remove(struct dw_hdmi_qp * hdmi)3595 static void __dw_hdmi_remove(struct dw_hdmi_qp *hdmi)
3596 {
3597 	if (hdmi->avp_irq)
3598 		disable_irq(hdmi->avp_irq);
3599 
3600 	if (hdmi->main_irq)
3601 		disable_irq(hdmi->main_irq);
3602 
3603 	if (hdmi->earc_irq)
3604 		disable_irq(hdmi->earc_irq);
3605 
3606 	debugfs_remove_recursive(hdmi->debugfs_dir);
3607 
3608 	if (!hdmi->plat_data->first_screen) {
3609 		dw_hdmi_destroy_properties(hdmi);
3610 		hdmi->connector.funcs->destroy(&hdmi->connector);
3611 	}
3612 
3613 	if (hdmi->audio && !IS_ERR(hdmi->audio))
3614 		platform_device_unregister(hdmi->audio);
3615 
3616 	if (hdmi->bridge.encoder && !hdmi->plat_data->first_screen)
3617 		hdmi->bridge.encoder->funcs->destroy(hdmi->bridge.encoder);
3618 	if (!IS_ERR(hdmi->cec))
3619 		platform_device_unregister(hdmi->cec);
3620 	if (hdmi->i2c)
3621 		i2c_del_adapter(&hdmi->i2c->adap);
3622 	else
3623 		i2c_put_adapter(hdmi->ddc);
3624 }
3625 
3626 /* -----------------------------------------------------------------------------
3627  * Bind/unbind API, used from platforms based on the component framework.
3628  */
dw_hdmi_qp_bind(struct platform_device * pdev,struct drm_encoder * encoder,struct dw_hdmi_plat_data * plat_data)3629 struct dw_hdmi_qp *dw_hdmi_qp_bind(struct platform_device *pdev,
3630 				   struct drm_encoder *encoder,
3631 				   struct dw_hdmi_plat_data *plat_data)
3632 {
3633 	struct dw_hdmi_qp *hdmi;
3634 	int ret;
3635 
3636 	hdmi = __dw_hdmi_probe(pdev, plat_data);
3637 	if (IS_ERR(hdmi))
3638 		return hdmi;
3639 
3640 	if (!plat_data->first_screen) {
3641 		ret = drm_bridge_attach(encoder, &hdmi->bridge, NULL, 0);
3642 		if (ret) {
3643 			__dw_hdmi_remove(hdmi);
3644 			dev_err(hdmi->dev, "Failed to initialize bridge with drm\n");
3645 			return ERR_PTR(ret);
3646 		}
3647 
3648 		plat_data->connector = &hdmi->connector;
3649 	}
3650 
3651 	if (plat_data->split_mode && !hdmi->plat_data->first_screen) {
3652 		struct dw_hdmi_qp *secondary = NULL;
3653 
3654 		if (hdmi->plat_data->left)
3655 			secondary = hdmi->plat_data->left;
3656 		else if (hdmi->plat_data->right)
3657 			secondary = hdmi->plat_data->right;
3658 
3659 		if (!secondary)
3660 			return ERR_PTR(-ENOMEM);
3661 		ret = drm_bridge_attach(encoder, &secondary->bridge, &hdmi->bridge,
3662 					DRM_BRIDGE_ATTACH_NO_CONNECTOR);
3663 		if (ret)
3664 			return ERR_PTR(ret);
3665 	}
3666 
3667 	return hdmi;
3668 }
3669 EXPORT_SYMBOL_GPL(dw_hdmi_qp_bind);
3670 
dw_hdmi_qp_unbind(struct dw_hdmi_qp * hdmi)3671 void dw_hdmi_qp_unbind(struct dw_hdmi_qp *hdmi)
3672 {
3673 	__dw_hdmi_remove(hdmi);
3674 }
3675 EXPORT_SYMBOL_GPL(dw_hdmi_qp_unbind);
3676 
dw_hdmi_qp_suspend(struct device * dev,struct dw_hdmi_qp * hdmi)3677 void dw_hdmi_qp_suspend(struct device *dev, struct dw_hdmi_qp *hdmi)
3678 {
3679 	if (!hdmi) {
3680 		dev_warn(dev, "Hdmi has not been initialized\n");
3681 		return;
3682 	}
3683 
3684 	mutex_lock(&hdmi->mutex);
3685 
3686 	/*
3687 	 * When system shutdown, hdmi should be disabled.
3688 	 * When system suspend, dw_hdmi_qp_bridge_disable will disable hdmi first.
3689 	 * To prevent duplicate operation, we should determine whether hdmi
3690 	 * has been disabled.
3691 	 */
3692 	if (!hdmi->disabled)
3693 		hdmi->disabled = true;
3694 	mutex_unlock(&hdmi->mutex);
3695 
3696 	if (hdmi->avp_irq)
3697 		disable_irq(hdmi->avp_irq);
3698 
3699 	if (hdmi->main_irq)
3700 		disable_irq(hdmi->main_irq);
3701 
3702 	if (hdmi->earc_irq)
3703 		disable_irq(hdmi->earc_irq);
3704 
3705 	pinctrl_pm_select_sleep_state(dev);
3706 }
3707 EXPORT_SYMBOL_GPL(dw_hdmi_qp_suspend);
3708 
dw_hdmi_qp_resume(struct device * dev,struct dw_hdmi_qp * hdmi)3709 void dw_hdmi_qp_resume(struct device *dev, struct dw_hdmi_qp *hdmi)
3710 {
3711 	if (!hdmi) {
3712 		dev_warn(dev, "Hdmi has not been initialized\n");
3713 		return;
3714 	}
3715 
3716 	hdmi_writel(hdmi, 0, MAINUNIT_0_INT_MASK_N);
3717 	hdmi_writel(hdmi, 0, MAINUNIT_1_INT_MASK_N);
3718 	hdmi_writel(hdmi, 428571429, TIMER_BASE_CONFIG0);
3719 
3720 	pinctrl_pm_select_default_state(dev);
3721 
3722 	if (hdmi->cec_adap)
3723 		hdmi->cec_adap->ops->adap_enable(hdmi->cec_adap, true);
3724 
3725 	mutex_lock(&hdmi->mutex);
3726 	if (hdmi->i2c)
3727 		dw_hdmi_i2c_init(hdmi);
3728 	if (hdmi->avp_irq)
3729 		enable_irq(hdmi->avp_irq);
3730 
3731 	if (hdmi->main_irq)
3732 		enable_irq(hdmi->main_irq);
3733 
3734 	if (hdmi->earc_irq)
3735 		enable_irq(hdmi->earc_irq);
3736 
3737 	mutex_unlock(&hdmi->mutex);
3738 }
3739 EXPORT_SYMBOL_GPL(dw_hdmi_qp_resume);
3740 
3741 MODULE_AUTHOR("Algea Cao <algea.cao@rock-chips.com>");
3742 MODULE_DESCRIPTION("DW HDMI QP transmitter driver");
3743 MODULE_LICENSE("GPL");
3744 MODULE_ALIAS("platform:dw-hdmi-qp");
3745