xref: /linux/drivers/gpu/drm/rockchip/inno_hdmi.c (revision 55a42f78ffd386e01a5404419f8c5ded7db70a21)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) Rockchip Electronics Co., Ltd.
4  *    Zheng Yang <zhengyang@rock-chips.com>
5  *    Yakir Yang <ykk@rock-chips.com>
6  */
7 
8 #include <linux/irq.h>
9 #include <linux/clk.h>
10 #include <linux/delay.h>
11 #include <linux/err.h>
12 #include <linux/hdmi.h>
13 #include <linux/hw_bitfield.h>
14 #include <linux/mfd/syscon.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/platform_device.h>
19 #include <linux/regmap.h>
20 
21 #include <drm/drm_atomic.h>
22 #include <drm/drm_atomic_helper.h>
23 #include <drm/drm_edid.h>
24 #include <drm/drm_of.h>
25 #include <drm/drm_probe_helper.h>
26 #include <drm/drm_simple_kms_helper.h>
27 
28 #include <drm/display/drm_hdmi_helper.h>
29 #include <drm/display/drm_hdmi_state_helper.h>
30 
31 #include "rockchip_drm_drv.h"
32 
33 #define INNO_HDMI_MIN_TMDS_CLOCK  25000000U
34 
35 #define DDC_SEGMENT_ADDR		0x30
36 
37 #define HDMI_SCL_RATE			(100 * 1000)
38 
39 #define DDC_BUS_FREQ_L			0x4b
40 #define DDC_BUS_FREQ_H			0x4c
41 
42 #define HDMI_SYS_CTRL			0x00
43 #define m_RST_ANALOG			BIT(6)
44 #define v_RST_ANALOG			(0 << 6)
45 #define v_NOT_RST_ANALOG		BIT(6)
46 #define m_RST_DIGITAL			BIT(5)
47 #define v_RST_DIGITAL			(0 << 5)
48 #define v_NOT_RST_DIGITAL		BIT(5)
49 #define m_REG_CLK_INV			BIT(4)
50 #define v_REG_CLK_NOT_INV		(0 << 4)
51 #define v_REG_CLK_INV			BIT(4)
52 #define m_VCLK_INV			BIT(3)
53 #define v_VCLK_NOT_INV			(0 << 3)
54 #define v_VCLK_INV			BIT(3)
55 #define m_REG_CLK_SOURCE		BIT(2)
56 #define v_REG_CLK_SOURCE_TMDS		(0 << 2)
57 #define v_REG_CLK_SOURCE_SYS		BIT(2)
58 #define m_POWER				BIT(1)
59 #define v_PWR_ON			(0 << 1)
60 #define v_PWR_OFF			BIT(1)
61 #define m_INT_POL			BIT(0)
62 #define v_INT_POL_HIGH			1
63 #define v_INT_POL_LOW			0
64 
65 #define HDMI_VIDEO_CONTRL1		0x01
66 #define m_VIDEO_INPUT_FORMAT		(7 << 1)
67 #define m_DE_SOURCE			BIT(0)
68 #define v_VIDEO_INPUT_FORMAT(n)		((n) << 1)
69 #define v_DE_EXTERNAL			1
70 #define v_DE_INTERNAL			0
71 enum {
72 	VIDEO_INPUT_SDR_RGB444 = 0,
73 	VIDEO_INPUT_DDR_RGB444 = 5,
74 	VIDEO_INPUT_DDR_YCBCR422 = 6
75 };
76 
77 #define HDMI_VIDEO_CONTRL2		0x02
78 #define m_VIDEO_OUTPUT_COLOR		(3 << 6)
79 #define m_VIDEO_INPUT_BITS		(3 << 4)
80 #define m_VIDEO_INPUT_CSP		BIT(0)
81 #define v_VIDEO_OUTPUT_COLOR(n)		(((n) & 0x3) << 6)
82 #define v_VIDEO_INPUT_BITS(n)		((n) << 4)
83 #define v_VIDEO_INPUT_CSP(n)		((n) << 0)
84 enum {
85 	VIDEO_INPUT_12BITS = 0,
86 	VIDEO_INPUT_10BITS = 1,
87 	VIDEO_INPUT_REVERT = 2,
88 	VIDEO_INPUT_8BITS = 3,
89 };
90 
91 #define HDMI_VIDEO_CONTRL		0x03
92 #define m_VIDEO_AUTO_CSC		BIT(7)
93 #define v_VIDEO_AUTO_CSC(n)		((n) << 7)
94 #define m_VIDEO_C0_C2_SWAP		BIT(0)
95 #define v_VIDEO_C0_C2_SWAP(n)		((n) << 0)
96 enum {
97 	C0_C2_CHANGE_ENABLE = 0,
98 	C0_C2_CHANGE_DISABLE = 1,
99 	AUTO_CSC_DISABLE = 0,
100 	AUTO_CSC_ENABLE = 1,
101 };
102 
103 #define HDMI_VIDEO_CONTRL3		0x04
104 #define m_COLOR_DEPTH_NOT_INDICATED	BIT(4)
105 #define m_SOF				BIT(3)
106 #define m_COLOR_RANGE			BIT(2)
107 #define m_CSC				BIT(0)
108 #define v_COLOR_DEPTH_NOT_INDICATED(n)	((n) << 4)
109 #define v_SOF_ENABLE			(0 << 3)
110 #define v_SOF_DISABLE			BIT(3)
111 #define v_COLOR_RANGE_FULL		BIT(2)
112 #define v_COLOR_RANGE_LIMITED		(0 << 2)
113 #define v_CSC_ENABLE			1
114 #define v_CSC_DISABLE			0
115 
116 #define HDMI_AV_MUTE			0x05
117 #define m_AVMUTE_CLEAR			BIT(7)
118 #define m_AVMUTE_ENABLE			BIT(6)
119 #define m_AUDIO_MUTE			BIT(1)
120 #define m_VIDEO_BLACK			BIT(0)
121 #define v_AVMUTE_CLEAR(n)		((n) << 7)
122 #define v_AVMUTE_ENABLE(n)		((n) << 6)
123 #define v_AUDIO_MUTE(n)			((n) << 1)
124 #define v_VIDEO_MUTE(n)			((n) << 0)
125 
126 #define HDMI_VIDEO_TIMING_CTL		0x08
127 #define v_HSYNC_POLARITY(n)		((n) << 3)
128 #define v_VSYNC_POLARITY(n)		((n) << 2)
129 #define v_INETLACE(n)			((n) << 1)
130 #define v_EXTERANL_VIDEO(n)		((n) << 0)
131 
132 #define HDMI_VIDEO_EXT_HTOTAL_L		0x09
133 #define HDMI_VIDEO_EXT_HTOTAL_H		0x0a
134 #define HDMI_VIDEO_EXT_HBLANK_L		0x0b
135 #define HDMI_VIDEO_EXT_HBLANK_H		0x0c
136 #define HDMI_VIDEO_EXT_HDELAY_L		0x0d
137 #define HDMI_VIDEO_EXT_HDELAY_H		0x0e
138 #define HDMI_VIDEO_EXT_HDURATION_L	0x0f
139 #define HDMI_VIDEO_EXT_HDURATION_H	0x10
140 #define HDMI_VIDEO_EXT_VTOTAL_L		0x11
141 #define HDMI_VIDEO_EXT_VTOTAL_H		0x12
142 #define HDMI_VIDEO_EXT_VBLANK		0x13
143 #define HDMI_VIDEO_EXT_VDELAY		0x14
144 #define HDMI_VIDEO_EXT_VDURATION	0x15
145 
146 #define HDMI_VIDEO_CSC_COEF		0x18
147 
148 #define HDMI_AUDIO_CTRL1		0x35
149 enum {
150 	CTS_SOURCE_INTERNAL = 0,
151 	CTS_SOURCE_EXTERNAL = 1,
152 };
153 
154 #define v_CTS_SOURCE(n)			((n) << 7)
155 
156 enum {
157 	DOWNSAMPLE_DISABLE = 0,
158 	DOWNSAMPLE_1_2 = 1,
159 	DOWNSAMPLE_1_4 = 2,
160 };
161 
162 #define v_DOWN_SAMPLE(n)		((n) << 5)
163 
164 enum {
165 	AUDIO_SOURCE_IIS = 0,
166 	AUDIO_SOURCE_SPDIF = 1,
167 };
168 
169 #define v_AUDIO_SOURCE(n)		((n) << 3)
170 
171 #define v_MCLK_ENABLE(n)		((n) << 2)
172 
173 enum {
174 	MCLK_128FS = 0,
175 	MCLK_256FS = 1,
176 	MCLK_384FS = 2,
177 	MCLK_512FS = 3,
178 };
179 
180 #define v_MCLK_RATIO(n)			(n)
181 
182 #define AUDIO_SAMPLE_RATE		0x37
183 
184 enum {
185 	AUDIO_32K = 0x3,
186 	AUDIO_441K = 0x0,
187 	AUDIO_48K = 0x2,
188 	AUDIO_882K = 0x8,
189 	AUDIO_96K = 0xa,
190 	AUDIO_1764K = 0xc,
191 	AUDIO_192K = 0xe,
192 };
193 
194 #define AUDIO_I2S_MODE			0x38
195 
196 enum {
197 	I2S_CHANNEL_1_2 = 1,
198 	I2S_CHANNEL_3_4 = 3,
199 	I2S_CHANNEL_5_6 = 7,
200 	I2S_CHANNEL_7_8 = 0xf
201 };
202 
203 #define v_I2S_CHANNEL(n)		((n) << 2)
204 
205 enum {
206 	I2S_STANDARD = 0,
207 	I2S_LEFT_JUSTIFIED = 1,
208 	I2S_RIGHT_JUSTIFIED = 2,
209 };
210 
211 #define v_I2S_MODE(n)			(n)
212 
213 #define AUDIO_I2S_MAP			0x39
214 #define AUDIO_I2S_SWAPS_SPDIF		0x3a
215 #define v_SPIDF_FREQ(n)			(n)
216 
217 #define N_32K				0x1000
218 #define N_441K				0x1880
219 #define N_882K				0x3100
220 #define N_1764K				0x6200
221 #define N_48K				0x1800
222 #define N_96K				0x3000
223 #define N_192K				0x6000
224 
225 #define HDMI_AUDIO_CHANNEL_STATUS	0x3e
226 #define m_AUDIO_STATUS_NLPCM		BIT(7)
227 #define m_AUDIO_STATUS_USE		BIT(6)
228 #define m_AUDIO_STATUS_COPYRIGHT	BIT(5)
229 #define m_AUDIO_STATUS_ADDITION		(3 << 2)
230 #define m_AUDIO_STATUS_CLK_ACCURACY	(2 << 0)
231 #define v_AUDIO_STATUS_NLPCM(n)		(((n) & 1) << 7)
232 #define AUDIO_N_H			0x3f
233 #define AUDIO_N_M			0x40
234 #define AUDIO_N_L			0x41
235 
236 #define HDMI_AUDIO_CTS_H		0x45
237 #define HDMI_AUDIO_CTS_M		0x46
238 #define HDMI_AUDIO_CTS_L		0x47
239 
240 #define HDMI_DDC_CLK_L			0x4b
241 #define HDMI_DDC_CLK_H			0x4c
242 
243 #define HDMI_EDID_SEGMENT_POINTER	0x4d
244 #define HDMI_EDID_WORD_ADDR		0x4e
245 #define HDMI_EDID_FIFO_OFFSET		0x4f
246 #define HDMI_EDID_FIFO_ADDR		0x50
247 
248 #define HDMI_PACKET_SEND_MANUAL		0x9c
249 #define HDMI_PACKET_SEND_AUTO		0x9d
250 #define m_PACKET_GCP_EN			BIT(7)
251 #define m_PACKET_MSI_EN			BIT(6)
252 #define m_PACKET_SDI_EN			BIT(5)
253 #define m_PACKET_VSI_EN			BIT(4)
254 #define v_PACKET_GCP_EN(n)		(((n) & 1) << 7)
255 #define v_PACKET_MSI_EN(n)		(((n) & 1) << 6)
256 #define v_PACKET_SDI_EN(n)		(((n) & 1) << 5)
257 #define v_PACKET_VSI_EN(n)		(((n) & 1) << 4)
258 
259 #define HDMI_CONTROL_PACKET_BUF_INDEX	0x9f
260 
261 enum {
262 	INFOFRAME_VSI = 0x05,
263 	INFOFRAME_AVI = 0x06,
264 	INFOFRAME_AAI = 0x08,
265 };
266 
267 #define HDMI_CONTROL_PACKET_ADDR	0xa0
268 #define HDMI_MAXIMUM_INFO_FRAME_SIZE	0x11
269 
270 enum {
271 	AVI_COLOR_MODE_RGB = 0,
272 	AVI_COLOR_MODE_YCBCR422 = 1,
273 	AVI_COLOR_MODE_YCBCR444 = 2,
274 	AVI_COLORIMETRY_NO_DATA = 0,
275 
276 	AVI_COLORIMETRY_SMPTE_170M = 1,
277 	AVI_COLORIMETRY_ITU709 = 2,
278 	AVI_COLORIMETRY_EXTENDED = 3,
279 
280 	AVI_CODED_FRAME_ASPECT_NO_DATA = 0,
281 	AVI_CODED_FRAME_ASPECT_4_3 = 1,
282 	AVI_CODED_FRAME_ASPECT_16_9 = 2,
283 
284 	ACTIVE_ASPECT_RATE_SAME_AS_CODED_FRAME = 0x08,
285 	ACTIVE_ASPECT_RATE_4_3 = 0x09,
286 	ACTIVE_ASPECT_RATE_16_9 = 0x0A,
287 	ACTIVE_ASPECT_RATE_14_9 = 0x0B,
288 };
289 
290 #define HDMI_HDCP_CTRL			0x52
291 #define m_HDMI_DVI			BIT(1)
292 #define v_HDMI_DVI(n)			((n) << 1)
293 
294 #define HDMI_INTERRUPT_MASK1		0xc0
295 #define HDMI_INTERRUPT_STATUS1		0xc1
296 #define	m_INT_ACTIVE_VSYNC		BIT(5)
297 #define m_INT_EDID_READY		BIT(2)
298 
299 #define HDMI_INTERRUPT_MASK2		0xc2
300 #define HDMI_INTERRUPT_STATUS2		0xc3
301 #define m_INT_HDCP_ERR			BIT(7)
302 #define m_INT_BKSV_FLAG			BIT(6)
303 #define m_INT_HDCP_OK			BIT(4)
304 
305 #define HDMI_STATUS			0xc8
306 #define m_HOTPLUG			BIT(7)
307 #define m_MASK_INT_HOTPLUG		BIT(5)
308 #define m_INT_HOTPLUG			BIT(1)
309 #define v_MASK_INT_HOTPLUG(n)		(((n) & 0x1) << 5)
310 
311 #define HDMI_COLORBAR                   0xc9
312 
313 #define HDMI_PHY_SYNC			0xce
314 #define HDMI_PHY_SYS_CTL		0xe0
315 #define m_TMDS_CLK_SOURCE		BIT(5)
316 #define v_TMDS_FROM_PLL			(0 << 5)
317 #define v_TMDS_FROM_GEN			BIT(5)
318 #define m_PHASE_CLK			BIT(4)
319 #define v_DEFAULT_PHASE			(0 << 4)
320 #define v_SYNC_PHASE			BIT(4)
321 #define m_TMDS_CURRENT_PWR		BIT(3)
322 #define v_TURN_ON_CURRENT		(0 << 3)
323 #define v_CAT_OFF_CURRENT		BIT(3)
324 #define m_BANDGAP_PWR			BIT(2)
325 #define v_BANDGAP_PWR_UP		(0 << 2)
326 #define v_BANDGAP_PWR_DOWN		BIT(2)
327 #define m_PLL_PWR			BIT(1)
328 #define v_PLL_PWR_UP			(0 << 1)
329 #define v_PLL_PWR_DOWN			BIT(1)
330 #define m_TMDS_CHG_PWR			BIT(0)
331 #define v_TMDS_CHG_PWR_UP		(0 << 0)
332 #define v_TMDS_CHG_PWR_DOWN		BIT(0)
333 
334 #define HDMI_PHY_CHG_PWR		0xe1
335 #define v_CLK_CHG_PWR(n)		(((n) & 1) << 3)
336 #define v_DATA_CHG_PWR(n)		(((n) & 7) << 0)
337 
338 #define HDMI_PHY_DRIVER			0xe2
339 #define v_CLK_MAIN_DRIVER(n)		((n) << 4)
340 #define v_DATA_MAIN_DRIVER(n)		((n) << 0)
341 
342 #define HDMI_PHY_PRE_EMPHASIS		0xe3
343 #define v_PRE_EMPHASIS(n)		(((n) & 7) << 4)
344 #define v_CLK_PRE_DRIVER(n)		(((n) & 3) << 2)
345 #define v_DATA_PRE_DRIVER(n)		(((n) & 3) << 0)
346 
347 #define HDMI_PHY_FEEDBACK_DIV_RATIO_LOW		0xe7
348 #define v_FEEDBACK_DIV_LOW(n)			((n) & 0xff)
349 #define HDMI_PHY_FEEDBACK_DIV_RATIO_HIGH	0xe8
350 #define v_FEEDBACK_DIV_HIGH(n)			((n) & 1)
351 
352 #define HDMI_PHY_PRE_DIV_RATIO		0xed
353 #define v_PRE_DIV_RATIO(n)		((n) & 0x1f)
354 
355 #define HDMI_CEC_CTRL			0xd0
356 #define m_ADJUST_FOR_HISENSE		BIT(6)
357 #define m_REJECT_RX_BROADCAST		BIT(5)
358 #define m_BUSFREETIME_ENABLE		BIT(2)
359 #define m_REJECT_RX			BIT(1)
360 #define m_START_TX			BIT(0)
361 
362 #define HDMI_CEC_DATA			0xd1
363 #define HDMI_CEC_TX_OFFSET		0xd2
364 #define HDMI_CEC_RX_OFFSET		0xd3
365 #define HDMI_CEC_CLK_H			0xd4
366 #define HDMI_CEC_CLK_L			0xd5
367 #define HDMI_CEC_TX_LENGTH		0xd6
368 #define HDMI_CEC_RX_LENGTH		0xd7
369 #define HDMI_CEC_TX_INT_MASK		0xd8
370 #define m_TX_DONE			BIT(3)
371 #define m_TX_NOACK			BIT(2)
372 #define m_TX_BROADCAST_REJ		BIT(1)
373 #define m_TX_BUSNOTFREE			BIT(0)
374 
375 #define HDMI_CEC_RX_INT_MASK		0xd9
376 #define m_RX_LA_ERR			BIT(4)
377 #define m_RX_GLITCH			BIT(3)
378 #define m_RX_DONE			BIT(0)
379 
380 #define HDMI_CEC_TX_INT			0xda
381 #define HDMI_CEC_RX_INT			0xdb
382 #define HDMI_CEC_BUSFREETIME_L		0xdc
383 #define HDMI_CEC_BUSFREETIME_H		0xdd
384 #define HDMI_CEC_LOGICADDR		0xde
385 
386 #define RK3036_GRF_SOC_CON2	0x148
387 #define RK3036_HDMI_PHSYNC	BIT(4)
388 #define RK3036_HDMI_PVSYNC	BIT(5)
389 
390 enum inno_hdmi_dev_type {
391 	RK3036_HDMI,
392 	RK3128_HDMI,
393 };
394 
395 struct inno_hdmi_phy_config {
396 	unsigned long pixelclock;
397 	u8 pre_emphasis;
398 	u8 voltage_level_control;
399 };
400 
401 struct inno_hdmi_variant {
402 	enum inno_hdmi_dev_type dev_type;
403 	struct inno_hdmi_phy_config *phy_configs;
404 	struct inno_hdmi_phy_config *default_phy_config;
405 };
406 
407 struct inno_hdmi_i2c {
408 	struct i2c_adapter adap;
409 
410 	u8 ddc_addr;
411 	u8 segment_addr;
412 
413 	struct mutex lock;
414 	struct completion cmp;
415 };
416 
417 struct inno_hdmi {
418 	struct device *dev;
419 
420 	struct clk *pclk;
421 	struct clk *refclk;
422 	void __iomem *regs;
423 	struct regmap *grf;
424 
425 	struct drm_connector	connector;
426 	struct rockchip_encoder	encoder;
427 
428 	struct inno_hdmi_i2c *i2c;
429 	struct i2c_adapter *ddc;
430 
431 	const struct inno_hdmi_variant *variant;
432 };
433 
434 struct inno_hdmi_connector_state {
435 	struct drm_connector_state	base;
436 	unsigned int			colorimetry;
437 };
438 
439 static struct inno_hdmi *encoder_to_inno_hdmi(struct drm_encoder *encoder)
440 {
441 	struct rockchip_encoder *rkencoder = to_rockchip_encoder(encoder);
442 
443 	return container_of(rkencoder, struct inno_hdmi, encoder);
444 }
445 
446 static struct inno_hdmi *connector_to_inno_hdmi(struct drm_connector *connector)
447 {
448 	return container_of(connector, struct inno_hdmi, connector);
449 }
450 
451 #define to_inno_hdmi_conn_state(conn_state) \
452 	container_of_const(conn_state, struct inno_hdmi_connector_state, base)
453 
454 enum {
455 	CSC_RGB_0_255_TO_ITU601_16_235_8BIT,
456 	CSC_RGB_0_255_TO_ITU709_16_235_8BIT,
457 	CSC_RGB_0_255_TO_RGB_16_235_8BIT,
458 };
459 
460 static const char coeff_csc[][24] = {
461 	/*
462 	 * RGB2YUV:601 SD mode:
463 	 *   Cb = -0.291G - 0.148R + 0.439B + 128
464 	 *   Y  = 0.504G  + 0.257R + 0.098B + 16
465 	 *   Cr = -0.368G + 0.439R - 0.071B + 128
466 	 */
467 	{
468 		0x11, 0x5f, 0x01, 0x82, 0x10, 0x23, 0x00, 0x80,
469 		0x02, 0x1c, 0x00, 0xa1, 0x00, 0x36, 0x00, 0x1e,
470 		0x11, 0x29, 0x10, 0x59, 0x01, 0x82, 0x00, 0x80
471 	},
472 	/*
473 	 * RGB2YUV:709 HD mode:
474 	 *   Cb = - 0.338G - 0.101R + 0.439B + 128
475 	 *   Y  = 0.614G   + 0.183R + 0.062B + 16
476 	 *   Cr = - 0.399G + 0.439R - 0.040B + 128
477 	 */
478 	{
479 		0x11, 0x98, 0x01, 0xc1, 0x10, 0x28, 0x00, 0x80,
480 		0x02, 0x74, 0x00, 0xbb, 0x00, 0x3f, 0x00, 0x10,
481 		0x11, 0x5a, 0x10, 0x67, 0x01, 0xc1, 0x00, 0x80
482 	},
483 	/*
484 	 * RGB[0:255]2RGB[16:235]:
485 	 *   R' = R x (235-16)/255 + 16;
486 	 *   G' = G x (235-16)/255 + 16;
487 	 *   B' = B x (235-16)/255 + 16;
488 	 */
489 	{
490 		0x00, 0x00, 0x03, 0x6F, 0x00, 0x00, 0x00, 0x10,
491 		0x03, 0x6F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
492 		0x00, 0x00, 0x00, 0x00, 0x03, 0x6F, 0x00, 0x10
493 	},
494 };
495 
496 static struct inno_hdmi_phy_config rk3036_hdmi_phy_configs[] = {
497 	{  74250000, 0x3f, 0xbb },
498 	{ 165000000, 0x6f, 0xbb },
499 	{      ~0UL, 0x00, 0x00 }
500 };
501 
502 static struct inno_hdmi_phy_config rk3128_hdmi_phy_configs[] = {
503 	{  74250000, 0x3f, 0xaa },
504 	{ 165000000, 0x5f, 0xaa },
505 	{      ~0UL, 0x00, 0x00 }
506 };
507 
508 static int inno_hdmi_find_phy_config(struct inno_hdmi *hdmi,
509 				     unsigned long pixelclk)
510 {
511 	const struct inno_hdmi_phy_config *phy_configs =
512 						hdmi->variant->phy_configs;
513 	int i;
514 
515 	for (i = 0; phy_configs[i].pixelclock != ~0UL; i++) {
516 		if (pixelclk <= phy_configs[i].pixelclock)
517 			return i;
518 	}
519 
520 	DRM_DEV_DEBUG(hdmi->dev, "No phy configuration for pixelclock %lu\n",
521 		      pixelclk);
522 
523 	return -EINVAL;
524 }
525 
526 static inline u8 hdmi_readb(struct inno_hdmi *hdmi, u16 offset)
527 {
528 	return readl_relaxed(hdmi->regs + (offset) * 0x04);
529 }
530 
531 static inline void hdmi_writeb(struct inno_hdmi *hdmi, u16 offset, u32 val)
532 {
533 	writel_relaxed(val, hdmi->regs + (offset) * 0x04);
534 }
535 
536 static inline void hdmi_modb(struct inno_hdmi *hdmi, u16 offset,
537 			     u32 msk, u32 val)
538 {
539 	u8 temp = hdmi_readb(hdmi, offset) & ~msk;
540 
541 	temp |= val & msk;
542 	hdmi_writeb(hdmi, offset, temp);
543 }
544 
545 static void inno_hdmi_i2c_init(struct inno_hdmi *hdmi, unsigned long long rate)
546 {
547 	unsigned long long ddc_bus_freq = rate >> 2;
548 
549 	do_div(ddc_bus_freq, HDMI_SCL_RATE);
550 
551 	hdmi_writeb(hdmi, DDC_BUS_FREQ_L, ddc_bus_freq & 0xFF);
552 	hdmi_writeb(hdmi, DDC_BUS_FREQ_H, (ddc_bus_freq >> 8) & 0xFF);
553 
554 	/* Clear the EDID interrupt flag and mute the interrupt */
555 	hdmi_writeb(hdmi, HDMI_INTERRUPT_MASK1, 0);
556 	hdmi_writeb(hdmi, HDMI_INTERRUPT_STATUS1, m_INT_EDID_READY);
557 }
558 
559 static void inno_hdmi_sys_power(struct inno_hdmi *hdmi, bool enable)
560 {
561 	if (enable)
562 		hdmi_modb(hdmi, HDMI_SYS_CTRL, m_POWER, v_PWR_ON);
563 	else
564 		hdmi_modb(hdmi, HDMI_SYS_CTRL, m_POWER, v_PWR_OFF);
565 }
566 
567 static void inno_hdmi_standby(struct inno_hdmi *hdmi)
568 {
569 	inno_hdmi_sys_power(hdmi, false);
570 
571 	hdmi_writeb(hdmi, HDMI_PHY_DRIVER, 0x00);
572 	hdmi_writeb(hdmi, HDMI_PHY_PRE_EMPHASIS, 0x00);
573 	hdmi_writeb(hdmi, HDMI_PHY_CHG_PWR, 0x00);
574 	hdmi_writeb(hdmi, HDMI_PHY_SYS_CTL, 0x15);
575 };
576 
577 static void inno_hdmi_power_up(struct inno_hdmi *hdmi,
578 			       unsigned long mpixelclock)
579 {
580 	struct inno_hdmi_phy_config *phy_config;
581 	int ret = inno_hdmi_find_phy_config(hdmi, mpixelclock);
582 
583 	if (ret < 0) {
584 		phy_config = hdmi->variant->default_phy_config;
585 		DRM_DEV_ERROR(hdmi->dev,
586 			      "Using default phy configuration for TMDS rate %lu",
587 			      mpixelclock);
588 	} else {
589 		phy_config = &hdmi->variant->phy_configs[ret];
590 	}
591 
592 	inno_hdmi_sys_power(hdmi, false);
593 
594 	hdmi_writeb(hdmi, HDMI_PHY_PRE_EMPHASIS, phy_config->pre_emphasis);
595 	hdmi_writeb(hdmi, HDMI_PHY_DRIVER, phy_config->voltage_level_control);
596 	hdmi_writeb(hdmi, HDMI_PHY_SYS_CTL, 0x15);
597 	hdmi_writeb(hdmi, HDMI_PHY_SYS_CTL, 0x14);
598 	hdmi_writeb(hdmi, HDMI_PHY_SYS_CTL, 0x10);
599 	hdmi_writeb(hdmi, HDMI_PHY_CHG_PWR, 0x0f);
600 	hdmi_writeb(hdmi, HDMI_PHY_SYNC, 0x00);
601 	hdmi_writeb(hdmi, HDMI_PHY_SYNC, 0x01);
602 
603 	inno_hdmi_sys_power(hdmi, true);
604 };
605 
606 static void inno_hdmi_init_hw(struct inno_hdmi *hdmi)
607 {
608 	u32 val;
609 	u32 msk;
610 
611 	hdmi_modb(hdmi, HDMI_SYS_CTRL, m_RST_DIGITAL, v_NOT_RST_DIGITAL);
612 	usleep_range(100, 150);
613 
614 	hdmi_modb(hdmi, HDMI_SYS_CTRL, m_RST_ANALOG, v_NOT_RST_ANALOG);
615 	usleep_range(100, 150);
616 
617 	msk = m_REG_CLK_INV | m_REG_CLK_SOURCE | m_POWER | m_INT_POL;
618 	val = v_REG_CLK_INV | v_REG_CLK_SOURCE_SYS | v_PWR_ON | v_INT_POL_HIGH;
619 	hdmi_modb(hdmi, HDMI_SYS_CTRL, msk, val);
620 
621 	inno_hdmi_standby(hdmi);
622 
623 	/*
624 	 * When the controller isn't configured to an accurate
625 	 * video timing and there is no reference clock available,
626 	 * then the TMDS clock source would be switched to PCLK_HDMI,
627 	 * so we need to init the TMDS rate to PCLK rate, and
628 	 * reconfigure the DDC clock.
629 	 */
630 	if (hdmi->refclk)
631 		inno_hdmi_i2c_init(hdmi, clk_get_rate(hdmi->refclk));
632 	else
633 		inno_hdmi_i2c_init(hdmi, clk_get_rate(hdmi->pclk));
634 
635 	/* Unmute hotplug interrupt */
636 	hdmi_modb(hdmi, HDMI_STATUS, m_MASK_INT_HOTPLUG, v_MASK_INT_HOTPLUG(1));
637 }
638 
639 static int inno_hdmi_disable_frame(struct drm_connector *connector,
640 				   enum hdmi_infoframe_type type)
641 {
642 	struct inno_hdmi *hdmi = connector_to_inno_hdmi(connector);
643 
644 	if (type != HDMI_INFOFRAME_TYPE_AVI) {
645 		drm_err(connector->dev,
646 			"Unsupported infoframe type: %u\n", type);
647 		return 0;
648 	}
649 
650 	hdmi_writeb(hdmi, HDMI_CONTROL_PACKET_BUF_INDEX, INFOFRAME_AVI);
651 
652 	return 0;
653 }
654 
655 static int inno_hdmi_upload_frame(struct drm_connector *connector,
656 				  enum hdmi_infoframe_type type,
657 				  const u8 *buffer, size_t len)
658 {
659 	struct inno_hdmi *hdmi = connector_to_inno_hdmi(connector);
660 	ssize_t i;
661 
662 	if (type != HDMI_INFOFRAME_TYPE_AVI) {
663 		drm_err(connector->dev,
664 			"Unsupported infoframe type: %u\n", type);
665 		return 0;
666 	}
667 
668 	inno_hdmi_disable_frame(connector, type);
669 
670 	for (i = 0; i < len; i++)
671 		hdmi_writeb(hdmi, HDMI_CONTROL_PACKET_ADDR + i, buffer[i]);
672 
673 	return 0;
674 }
675 
676 static const struct drm_connector_hdmi_funcs inno_hdmi_hdmi_connector_funcs = {
677 	.clear_infoframe	= inno_hdmi_disable_frame,
678 	.write_infoframe	= inno_hdmi_upload_frame,
679 };
680 
681 static int inno_hdmi_config_video_csc(struct inno_hdmi *hdmi)
682 {
683 	struct drm_connector *connector = &hdmi->connector;
684 	struct drm_connector_state *conn_state = connector->state;
685 	struct inno_hdmi_connector_state *inno_conn_state =
686 					to_inno_hdmi_conn_state(conn_state);
687 	int c0_c2_change = 0;
688 	int csc_enable = 0;
689 	int csc_mode = 0;
690 	int auto_csc = 0;
691 	int value;
692 	int i;
693 
694 	/* Input video mode is SDR RGB24bit, data enable signal from external */
695 	hdmi_writeb(hdmi, HDMI_VIDEO_CONTRL1, v_DE_EXTERNAL |
696 		    v_VIDEO_INPUT_FORMAT(VIDEO_INPUT_SDR_RGB444));
697 
698 	/* Input color hardcode to RGB, and output color hardcode to RGB888 */
699 	value = v_VIDEO_INPUT_BITS(VIDEO_INPUT_8BITS) |
700 		v_VIDEO_OUTPUT_COLOR(0) |
701 		v_VIDEO_INPUT_CSP(0);
702 	hdmi_writeb(hdmi, HDMI_VIDEO_CONTRL2, value);
703 
704 	if (conn_state->hdmi.output_format == HDMI_COLORSPACE_RGB) {
705 		if (conn_state->hdmi.is_limited_range) {
706 			csc_mode = CSC_RGB_0_255_TO_RGB_16_235_8BIT;
707 			auto_csc = AUTO_CSC_DISABLE;
708 			c0_c2_change = C0_C2_CHANGE_DISABLE;
709 			csc_enable = v_CSC_ENABLE;
710 
711 		} else {
712 			value = v_SOF_DISABLE | v_COLOR_DEPTH_NOT_INDICATED(1);
713 			hdmi_writeb(hdmi, HDMI_VIDEO_CONTRL3, value);
714 
715 			hdmi_modb(hdmi, HDMI_VIDEO_CONTRL,
716 				  m_VIDEO_AUTO_CSC | m_VIDEO_C0_C2_SWAP,
717 				  v_VIDEO_AUTO_CSC(AUTO_CSC_DISABLE) |
718 				  v_VIDEO_C0_C2_SWAP(C0_C2_CHANGE_DISABLE));
719 			return 0;
720 		}
721 	} else {
722 		if (inno_conn_state->colorimetry == HDMI_COLORIMETRY_ITU_601) {
723 			if (conn_state->hdmi.output_format == HDMI_COLORSPACE_YUV444) {
724 				csc_mode = CSC_RGB_0_255_TO_ITU601_16_235_8BIT;
725 				auto_csc = AUTO_CSC_DISABLE;
726 				c0_c2_change = C0_C2_CHANGE_DISABLE;
727 				csc_enable = v_CSC_ENABLE;
728 			}
729 		} else {
730 			if (conn_state->hdmi.output_format == HDMI_COLORSPACE_YUV444) {
731 				csc_mode = CSC_RGB_0_255_TO_ITU709_16_235_8BIT;
732 				auto_csc = AUTO_CSC_DISABLE;
733 				c0_c2_change = C0_C2_CHANGE_DISABLE;
734 				csc_enable = v_CSC_ENABLE;
735 			}
736 		}
737 	}
738 
739 	for (i = 0; i < 24; i++)
740 		hdmi_writeb(hdmi, HDMI_VIDEO_CSC_COEF + i,
741 			    coeff_csc[csc_mode][i]);
742 
743 	value = v_SOF_DISABLE | csc_enable | v_COLOR_DEPTH_NOT_INDICATED(1);
744 	hdmi_writeb(hdmi, HDMI_VIDEO_CONTRL3, value);
745 	hdmi_modb(hdmi, HDMI_VIDEO_CONTRL, m_VIDEO_AUTO_CSC |
746 		  m_VIDEO_C0_C2_SWAP, v_VIDEO_AUTO_CSC(auto_csc) |
747 		  v_VIDEO_C0_C2_SWAP(c0_c2_change));
748 
749 	return 0;
750 }
751 
752 static int inno_hdmi_config_video_timing(struct inno_hdmi *hdmi,
753 					 struct drm_display_mode *mode)
754 {
755 	int value, psync;
756 
757 	if (hdmi->variant->dev_type == RK3036_HDMI) {
758 		psync = mode->flags & DRM_MODE_FLAG_PHSYNC ? 1 : 0;
759 		value = FIELD_PREP_WM16(RK3036_HDMI_PHSYNC, psync);
760 		psync = mode->flags & DRM_MODE_FLAG_PVSYNC ? 1 : 0;
761 		value |= FIELD_PREP_WM16(RK3036_HDMI_PVSYNC, psync);
762 		regmap_write(hdmi->grf, RK3036_GRF_SOC_CON2, value);
763 	}
764 
765 	/* Set detail external video timing polarity and interlace mode */
766 	value = v_EXTERANL_VIDEO(1);
767 	value |= mode->flags & DRM_MODE_FLAG_PHSYNC ?
768 		 v_HSYNC_POLARITY(1) : v_HSYNC_POLARITY(0);
769 	value |= mode->flags & DRM_MODE_FLAG_PVSYNC ?
770 		 v_VSYNC_POLARITY(1) : v_VSYNC_POLARITY(0);
771 	value |= mode->flags & DRM_MODE_FLAG_INTERLACE ?
772 		 v_INETLACE(1) : v_INETLACE(0);
773 	hdmi_writeb(hdmi, HDMI_VIDEO_TIMING_CTL, value);
774 
775 	/* Set detail external video timing */
776 	value = mode->htotal;
777 	hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HTOTAL_L, value & 0xFF);
778 	hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HTOTAL_H, (value >> 8) & 0xFF);
779 
780 	value = mode->htotal - mode->hdisplay;
781 	hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HBLANK_L, value & 0xFF);
782 	hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HBLANK_H, (value >> 8) & 0xFF);
783 
784 	value = mode->htotal - mode->hsync_start;
785 	hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HDELAY_L, value & 0xFF);
786 	hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HDELAY_H, (value >> 8) & 0xFF);
787 
788 	value = mode->hsync_end - mode->hsync_start;
789 	hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HDURATION_L, value & 0xFF);
790 	hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HDURATION_H, (value >> 8) & 0xFF);
791 
792 	value = mode->vtotal;
793 	hdmi_writeb(hdmi, HDMI_VIDEO_EXT_VTOTAL_L, value & 0xFF);
794 	hdmi_writeb(hdmi, HDMI_VIDEO_EXT_VTOTAL_H, (value >> 8) & 0xFF);
795 
796 	value = mode->vtotal - mode->vdisplay;
797 	hdmi_writeb(hdmi, HDMI_VIDEO_EXT_VBLANK, value & 0xFF);
798 
799 	value = mode->vtotal - mode->vsync_start;
800 	hdmi_writeb(hdmi, HDMI_VIDEO_EXT_VDELAY, value & 0xFF);
801 
802 	value = mode->vsync_end - mode->vsync_start;
803 	hdmi_writeb(hdmi, HDMI_VIDEO_EXT_VDURATION, value & 0xFF);
804 
805 	hdmi_writeb(hdmi, HDMI_PHY_PRE_DIV_RATIO, 0x1e);
806 	hdmi_writeb(hdmi, HDMI_PHY_FEEDBACK_DIV_RATIO_LOW, 0x2c);
807 	hdmi_writeb(hdmi, HDMI_PHY_FEEDBACK_DIV_RATIO_HIGH, 0x01);
808 
809 	return 0;
810 }
811 
812 static int inno_hdmi_setup(struct inno_hdmi *hdmi,
813 			   struct drm_atomic_state *state)
814 {
815 	struct drm_connector *connector = &hdmi->connector;
816 	struct drm_display_info *display = &connector->display_info;
817 	struct drm_connector_state *new_conn_state;
818 	struct drm_crtc_state *new_crtc_state;
819 
820 	new_conn_state = drm_atomic_get_new_connector_state(state, connector);
821 	if (WARN_ON(!new_conn_state))
822 		return -EINVAL;
823 
824 	new_crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
825 	if (WARN_ON(!new_crtc_state))
826 		return -EINVAL;
827 
828 	/* Mute video and audio output */
829 	hdmi_modb(hdmi, HDMI_AV_MUTE, m_AUDIO_MUTE | m_VIDEO_BLACK,
830 		  v_AUDIO_MUTE(1) | v_VIDEO_MUTE(1));
831 
832 	/* Set HDMI Mode */
833 	hdmi_writeb(hdmi, HDMI_HDCP_CTRL,
834 		    v_HDMI_DVI(display->is_hdmi));
835 
836 	inno_hdmi_config_video_timing(hdmi, &new_crtc_state->adjusted_mode);
837 
838 	inno_hdmi_config_video_csc(hdmi);
839 
840 	drm_atomic_helper_connector_hdmi_update_infoframes(connector, state);
841 
842 	/*
843 	 * When IP controller have configured to an accurate video
844 	 * timing, then the TMDS clock source would be switched to
845 	 * DCLK_LCDC, so we need to init the TMDS rate to mode pixel
846 	 * clock rate, and reconfigure the DDC clock.
847 	 */
848 	inno_hdmi_i2c_init(hdmi, new_conn_state->hdmi.tmds_char_rate);
849 
850 	/* Unmute video and audio output */
851 	hdmi_modb(hdmi, HDMI_AV_MUTE, m_AUDIO_MUTE | m_VIDEO_BLACK,
852 		  v_AUDIO_MUTE(0) | v_VIDEO_MUTE(0));
853 
854 	inno_hdmi_power_up(hdmi, new_conn_state->hdmi.tmds_char_rate);
855 
856 	return 0;
857 }
858 
859 static enum drm_mode_status inno_hdmi_display_mode_valid(struct inno_hdmi *hdmi,
860 							 const struct drm_display_mode *mode)
861 {
862 	unsigned long mpixelclk, max_tolerance;
863 	long rounded_refclk;
864 
865 	/* No support for double-clock modes */
866 	if (mode->flags & DRM_MODE_FLAG_DBLCLK)
867 		return MODE_BAD;
868 
869 	mpixelclk = mode->clock * 1000;
870 
871 	if (mpixelclk < INNO_HDMI_MIN_TMDS_CLOCK)
872 		return MODE_CLOCK_LOW;
873 
874 	if (inno_hdmi_find_phy_config(hdmi, mpixelclk) < 0)
875 		return MODE_CLOCK_HIGH;
876 
877 	if (hdmi->refclk) {
878 		rounded_refclk = clk_round_rate(hdmi->refclk, mpixelclk);
879 		if (rounded_refclk < 0)
880 			return MODE_BAD;
881 
882 		/* Vesa DMT standard mentions +/- 0.5% max tolerance */
883 		max_tolerance = mpixelclk / 200;
884 		if (abs_diff((unsigned long)rounded_refclk, mpixelclk) > max_tolerance)
885 			return MODE_NOCLOCK;
886 	}
887 
888 	return MODE_OK;
889 }
890 
891 static void inno_hdmi_encoder_enable(struct drm_encoder *encoder,
892 				     struct drm_atomic_state *state)
893 {
894 	struct inno_hdmi *hdmi = encoder_to_inno_hdmi(encoder);
895 
896 	inno_hdmi_setup(hdmi, state);
897 }
898 
899 static void inno_hdmi_encoder_disable(struct drm_encoder *encoder,
900 				      struct drm_atomic_state *state)
901 {
902 	struct inno_hdmi *hdmi = encoder_to_inno_hdmi(encoder);
903 
904 	inno_hdmi_standby(hdmi);
905 }
906 
907 static int
908 inno_hdmi_encoder_atomic_check(struct drm_encoder *encoder,
909 			       struct drm_crtc_state *crtc_state,
910 			       struct drm_connector_state *conn_state)
911 {
912 	struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
913 	struct drm_display_mode *mode = &crtc_state->adjusted_mode;
914 	u8 vic = drm_match_cea_mode(mode);
915 	struct inno_hdmi_connector_state *inno_conn_state =
916 					to_inno_hdmi_conn_state(conn_state);
917 
918 	s->output_mode = ROCKCHIP_OUT_MODE_P888;
919 	s->output_type = DRM_MODE_CONNECTOR_HDMIA;
920 
921 	if (vic == 6 || vic == 7 ||
922 	    vic == 21 || vic == 22 ||
923 	    vic == 2 || vic == 3 ||
924 	    vic == 17 || vic == 18)
925 		inno_conn_state->colorimetry = HDMI_COLORIMETRY_ITU_601;
926 	else
927 		inno_conn_state->colorimetry = HDMI_COLORIMETRY_ITU_709;
928 
929 	return 0;
930 }
931 
932 static const struct drm_encoder_helper_funcs inno_hdmi_encoder_helper_funcs = {
933 	.atomic_check	= inno_hdmi_encoder_atomic_check,
934 	.atomic_enable	= inno_hdmi_encoder_enable,
935 	.atomic_disable	= inno_hdmi_encoder_disable,
936 };
937 
938 static enum drm_connector_status
939 inno_hdmi_connector_detect(struct drm_connector *connector, bool force)
940 {
941 	struct inno_hdmi *hdmi = connector_to_inno_hdmi(connector);
942 
943 	return (hdmi_readb(hdmi, HDMI_STATUS) & m_HOTPLUG) ?
944 		connector_status_connected : connector_status_disconnected;
945 }
946 
947 static int inno_hdmi_connector_get_modes(struct drm_connector *connector)
948 {
949 	struct inno_hdmi *hdmi = connector_to_inno_hdmi(connector);
950 	const struct drm_edid *drm_edid;
951 	int ret = 0;
952 
953 	if (!hdmi->ddc)
954 		return 0;
955 
956 	drm_edid = drm_edid_read_ddc(connector, hdmi->ddc);
957 	drm_edid_connector_update(connector, drm_edid);
958 	ret = drm_edid_connector_add_modes(connector);
959 	drm_edid_free(drm_edid);
960 
961 	return ret;
962 }
963 
964 static enum drm_mode_status
965 inno_hdmi_connector_mode_valid(struct drm_connector *connector,
966 			       const struct drm_display_mode *mode)
967 {
968 	struct inno_hdmi *hdmi = connector_to_inno_hdmi(connector);
969 
970 	return  inno_hdmi_display_mode_valid(hdmi, mode);
971 }
972 
973 static void
974 inno_hdmi_connector_destroy_state(struct drm_connector *connector,
975 				  struct drm_connector_state *state)
976 {
977 	struct inno_hdmi_connector_state *inno_conn_state =
978 						to_inno_hdmi_conn_state(state);
979 
980 	__drm_atomic_helper_connector_destroy_state(&inno_conn_state->base);
981 	kfree(inno_conn_state);
982 }
983 
984 static void inno_hdmi_connector_reset(struct drm_connector *connector)
985 {
986 	struct inno_hdmi_connector_state *inno_conn_state;
987 
988 	if (connector->state) {
989 		inno_hdmi_connector_destroy_state(connector, connector->state);
990 		connector->state = NULL;
991 	}
992 
993 	inno_conn_state = kzalloc(sizeof(*inno_conn_state), GFP_KERNEL);
994 	if (!inno_conn_state)
995 		return;
996 
997 	__drm_atomic_helper_connector_reset(connector, &inno_conn_state->base);
998 	__drm_atomic_helper_connector_hdmi_reset(connector, connector->state);
999 
1000 	inno_conn_state->colorimetry = HDMI_COLORIMETRY_ITU_709;
1001 }
1002 
1003 static struct drm_connector_state *
1004 inno_hdmi_connector_duplicate_state(struct drm_connector *connector)
1005 {
1006 	struct inno_hdmi_connector_state *inno_conn_state;
1007 
1008 	if (WARN_ON(!connector->state))
1009 		return NULL;
1010 
1011 	inno_conn_state = kmemdup(to_inno_hdmi_conn_state(connector->state),
1012 				  sizeof(*inno_conn_state), GFP_KERNEL);
1013 
1014 	if (!inno_conn_state)
1015 		return NULL;
1016 
1017 	__drm_atomic_helper_connector_duplicate_state(connector,
1018 						      &inno_conn_state->base);
1019 
1020 	return &inno_conn_state->base;
1021 }
1022 
1023 static const struct drm_connector_funcs inno_hdmi_connector_funcs = {
1024 	.fill_modes = drm_helper_probe_single_connector_modes,
1025 	.detect = inno_hdmi_connector_detect,
1026 	.reset = inno_hdmi_connector_reset,
1027 	.atomic_duplicate_state = inno_hdmi_connector_duplicate_state,
1028 	.atomic_destroy_state = inno_hdmi_connector_destroy_state,
1029 };
1030 
1031 static struct drm_connector_helper_funcs inno_hdmi_connector_helper_funcs = {
1032 	.atomic_check = drm_atomic_helper_connector_hdmi_check,
1033 	.get_modes = inno_hdmi_connector_get_modes,
1034 	.mode_valid = inno_hdmi_connector_mode_valid,
1035 };
1036 
1037 static int inno_hdmi_register(struct drm_device *drm, struct inno_hdmi *hdmi)
1038 {
1039 	struct drm_encoder *encoder = &hdmi->encoder.encoder;
1040 	struct device *dev = hdmi->dev;
1041 
1042 	encoder->possible_crtcs = drm_of_find_possible_crtcs(drm, dev->of_node);
1043 
1044 	/*
1045 	 * If we failed to find the CRTC(s) which this encoder is
1046 	 * supposed to be connected to, it's because the CRTC has
1047 	 * not been registered yet.  Defer probing, and hope that
1048 	 * the required CRTC is added later.
1049 	 */
1050 	if (encoder->possible_crtcs == 0)
1051 		return -EPROBE_DEFER;
1052 
1053 	drm_encoder_helper_add(encoder, &inno_hdmi_encoder_helper_funcs);
1054 	drm_simple_encoder_init(drm, encoder, DRM_MODE_ENCODER_TMDS);
1055 
1056 	hdmi->connector.polled = DRM_CONNECTOR_POLL_HPD;
1057 
1058 	drm_connector_helper_add(&hdmi->connector,
1059 				 &inno_hdmi_connector_helper_funcs);
1060 	drmm_connector_hdmi_init(drm, &hdmi->connector,
1061 				 "Rockchip", "Inno HDMI",
1062 				 &inno_hdmi_connector_funcs,
1063 				 &inno_hdmi_hdmi_connector_funcs,
1064 				 DRM_MODE_CONNECTOR_HDMIA,
1065 				 hdmi->ddc,
1066 				 BIT(HDMI_COLORSPACE_RGB),
1067 				 8);
1068 
1069 	drm_connector_attach_encoder(&hdmi->connector, encoder);
1070 
1071 	return 0;
1072 }
1073 
1074 static irqreturn_t inno_hdmi_i2c_irq(struct inno_hdmi *hdmi)
1075 {
1076 	struct inno_hdmi_i2c *i2c = hdmi->i2c;
1077 	u8 stat;
1078 
1079 	stat = hdmi_readb(hdmi, HDMI_INTERRUPT_STATUS1);
1080 	if (!(stat & m_INT_EDID_READY))
1081 		return IRQ_NONE;
1082 
1083 	/* Clear HDMI EDID interrupt flag */
1084 	hdmi_writeb(hdmi, HDMI_INTERRUPT_STATUS1, m_INT_EDID_READY);
1085 
1086 	complete(&i2c->cmp);
1087 
1088 	return IRQ_HANDLED;
1089 }
1090 
1091 static irqreturn_t inno_hdmi_hardirq(int irq, void *dev_id)
1092 {
1093 	struct inno_hdmi *hdmi = dev_id;
1094 	irqreturn_t ret = IRQ_NONE;
1095 	u8 interrupt;
1096 
1097 	if (hdmi->i2c)
1098 		ret = inno_hdmi_i2c_irq(hdmi);
1099 
1100 	interrupt = hdmi_readb(hdmi, HDMI_STATUS);
1101 	if (interrupt & m_INT_HOTPLUG) {
1102 		hdmi_modb(hdmi, HDMI_STATUS, m_INT_HOTPLUG, m_INT_HOTPLUG);
1103 		ret = IRQ_WAKE_THREAD;
1104 	}
1105 
1106 	return ret;
1107 }
1108 
1109 static irqreturn_t inno_hdmi_irq(int irq, void *dev_id)
1110 {
1111 	struct inno_hdmi *hdmi = dev_id;
1112 
1113 	drm_helper_hpd_irq_event(hdmi->connector.dev);
1114 
1115 	return IRQ_HANDLED;
1116 }
1117 
1118 static int inno_hdmi_i2c_read(struct inno_hdmi *hdmi, struct i2c_msg *msgs)
1119 {
1120 	int length = msgs->len;
1121 	u8 *buf = msgs->buf;
1122 	int ret;
1123 
1124 	ret = wait_for_completion_timeout(&hdmi->i2c->cmp, HZ / 10);
1125 	if (!ret)
1126 		return -EAGAIN;
1127 
1128 	while (length--)
1129 		*buf++ = hdmi_readb(hdmi, HDMI_EDID_FIFO_ADDR);
1130 
1131 	return 0;
1132 }
1133 
1134 static int inno_hdmi_i2c_write(struct inno_hdmi *hdmi, struct i2c_msg *msgs)
1135 {
1136 	/*
1137 	 * The DDC module only support read EDID message, so
1138 	 * we assume that each word write to this i2c adapter
1139 	 * should be the offset of EDID word address.
1140 	 */
1141 	if (msgs->len != 1 || (msgs->addr != DDC_ADDR && msgs->addr != DDC_SEGMENT_ADDR))
1142 		return -EINVAL;
1143 
1144 	reinit_completion(&hdmi->i2c->cmp);
1145 
1146 	if (msgs->addr == DDC_SEGMENT_ADDR)
1147 		hdmi->i2c->segment_addr = msgs->buf[0];
1148 	if (msgs->addr == DDC_ADDR)
1149 		hdmi->i2c->ddc_addr = msgs->buf[0];
1150 
1151 	/* Set edid fifo first addr */
1152 	hdmi_writeb(hdmi, HDMI_EDID_FIFO_OFFSET, 0x00);
1153 
1154 	/* Set edid word address 0x00/0x80 */
1155 	hdmi_writeb(hdmi, HDMI_EDID_WORD_ADDR, hdmi->i2c->ddc_addr);
1156 
1157 	/* Set edid segment pointer */
1158 	hdmi_writeb(hdmi, HDMI_EDID_SEGMENT_POINTER, hdmi->i2c->segment_addr);
1159 
1160 	return 0;
1161 }
1162 
1163 static int inno_hdmi_i2c_xfer(struct i2c_adapter *adap,
1164 			      struct i2c_msg *msgs, int num)
1165 {
1166 	struct inno_hdmi *hdmi = i2c_get_adapdata(adap);
1167 	struct inno_hdmi_i2c *i2c = hdmi->i2c;
1168 	int i, ret = 0;
1169 
1170 	mutex_lock(&i2c->lock);
1171 
1172 	/* Clear the EDID interrupt flag and unmute the interrupt */
1173 	hdmi_writeb(hdmi, HDMI_INTERRUPT_MASK1, m_INT_EDID_READY);
1174 	hdmi_writeb(hdmi, HDMI_INTERRUPT_STATUS1, m_INT_EDID_READY);
1175 
1176 	for (i = 0; i < num; i++) {
1177 		DRM_DEV_DEBUG(hdmi->dev,
1178 			      "xfer: num: %d/%d, len: %d, flags: %#x\n",
1179 			      i + 1, num, msgs[i].len, msgs[i].flags);
1180 
1181 		if (msgs[i].flags & I2C_M_RD)
1182 			ret = inno_hdmi_i2c_read(hdmi, &msgs[i]);
1183 		else
1184 			ret = inno_hdmi_i2c_write(hdmi, &msgs[i]);
1185 
1186 		if (ret < 0)
1187 			break;
1188 	}
1189 
1190 	if (!ret)
1191 		ret = num;
1192 
1193 	/* Mute HDMI EDID interrupt */
1194 	hdmi_writeb(hdmi, HDMI_INTERRUPT_MASK1, 0);
1195 
1196 	mutex_unlock(&i2c->lock);
1197 
1198 	return ret;
1199 }
1200 
1201 static u32 inno_hdmi_i2c_func(struct i2c_adapter *adapter)
1202 {
1203 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
1204 }
1205 
1206 static const struct i2c_algorithm inno_hdmi_algorithm = {
1207 	.master_xfer	= inno_hdmi_i2c_xfer,
1208 	.functionality	= inno_hdmi_i2c_func,
1209 };
1210 
1211 static struct i2c_adapter *inno_hdmi_i2c_adapter(struct inno_hdmi *hdmi)
1212 {
1213 	struct i2c_adapter *adap;
1214 	struct inno_hdmi_i2c *i2c;
1215 	int ret;
1216 
1217 	i2c = devm_kzalloc(hdmi->dev, sizeof(*i2c), GFP_KERNEL);
1218 	if (!i2c)
1219 		return ERR_PTR(-ENOMEM);
1220 
1221 	mutex_init(&i2c->lock);
1222 	init_completion(&i2c->cmp);
1223 
1224 	adap = &i2c->adap;
1225 	adap->owner = THIS_MODULE;
1226 	adap->dev.parent = hdmi->dev;
1227 	adap->dev.of_node = hdmi->dev->of_node;
1228 	adap->algo = &inno_hdmi_algorithm;
1229 	strscpy(adap->name, "Inno HDMI", sizeof(adap->name));
1230 	i2c_set_adapdata(adap, hdmi);
1231 
1232 	ret = devm_i2c_add_adapter(hdmi->dev, adap);
1233 	if (ret) {
1234 		dev_warn(hdmi->dev, "cannot add %s I2C adapter\n", adap->name);
1235 		return ERR_PTR(ret);
1236 	}
1237 
1238 	hdmi->i2c = i2c;
1239 
1240 	DRM_DEV_INFO(hdmi->dev, "registered %s I2C bus driver\n", adap->name);
1241 
1242 	return adap;
1243 }
1244 
1245 static int inno_hdmi_bind(struct device *dev, struct device *master,
1246 				 void *data)
1247 {
1248 	struct platform_device *pdev = to_platform_device(dev);
1249 	struct drm_device *drm = data;
1250 	struct inno_hdmi *hdmi;
1251 	const struct inno_hdmi_variant *variant;
1252 	int irq;
1253 	int ret;
1254 
1255 	hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
1256 	if (!hdmi)
1257 		return -ENOMEM;
1258 
1259 	hdmi->dev = dev;
1260 
1261 	variant = of_device_get_match_data(hdmi->dev);
1262 	if (!variant)
1263 		return -EINVAL;
1264 
1265 	hdmi->variant = variant;
1266 
1267 	hdmi->regs = devm_platform_ioremap_resource(pdev, 0);
1268 	if (IS_ERR(hdmi->regs))
1269 		return PTR_ERR(hdmi->regs);
1270 
1271 	hdmi->pclk = devm_clk_get_enabled(hdmi->dev, "pclk");
1272 	if (IS_ERR(hdmi->pclk))
1273 		return dev_err_probe(dev, PTR_ERR(hdmi->pclk), "Unable to get HDMI pclk\n");
1274 
1275 	hdmi->refclk = devm_clk_get_optional_enabled(hdmi->dev, "ref");
1276 	if (IS_ERR(hdmi->refclk))
1277 		return dev_err_probe(dev, PTR_ERR(hdmi->refclk), "Unable to get HDMI refclk\n");
1278 
1279 	if (hdmi->variant->dev_type == RK3036_HDMI) {
1280 		hdmi->grf = syscon_regmap_lookup_by_phandle(dev->of_node, "rockchip,grf");
1281 		if (IS_ERR(hdmi->grf))
1282 			return dev_err_probe(dev,
1283 					     PTR_ERR(hdmi->grf), "Unable to get rockchip,grf\n");
1284 	}
1285 
1286 	irq = platform_get_irq(pdev, 0);
1287 	if (irq < 0)
1288 		return irq;
1289 
1290 	inno_hdmi_init_hw(hdmi);
1291 
1292 	hdmi->ddc = inno_hdmi_i2c_adapter(hdmi);
1293 	if (IS_ERR(hdmi->ddc))
1294 		return PTR_ERR(hdmi->ddc);
1295 
1296 	ret = inno_hdmi_register(drm, hdmi);
1297 	if (ret)
1298 		return ret;
1299 
1300 	dev_set_drvdata(dev, hdmi);
1301 
1302 	ret = devm_request_threaded_irq(dev, irq, inno_hdmi_hardirq,
1303 					inno_hdmi_irq, IRQF_SHARED,
1304 					dev_name(dev), hdmi);
1305 	if (ret < 0)
1306 		goto err_cleanup_hdmi;
1307 
1308 	return 0;
1309 err_cleanup_hdmi:
1310 	hdmi->connector.funcs->destroy(&hdmi->connector);
1311 	hdmi->encoder.encoder.funcs->destroy(&hdmi->encoder.encoder);
1312 	return ret;
1313 }
1314 
1315 static void inno_hdmi_unbind(struct device *dev, struct device *master,
1316 			     void *data)
1317 {
1318 	struct inno_hdmi *hdmi = dev_get_drvdata(dev);
1319 
1320 	hdmi->connector.funcs->destroy(&hdmi->connector);
1321 	hdmi->encoder.encoder.funcs->destroy(&hdmi->encoder.encoder);
1322 }
1323 
1324 static const struct component_ops inno_hdmi_ops = {
1325 	.bind	= inno_hdmi_bind,
1326 	.unbind	= inno_hdmi_unbind,
1327 };
1328 
1329 static int inno_hdmi_probe(struct platform_device *pdev)
1330 {
1331 	return component_add(&pdev->dev, &inno_hdmi_ops);
1332 }
1333 
1334 static void inno_hdmi_remove(struct platform_device *pdev)
1335 {
1336 	component_del(&pdev->dev, &inno_hdmi_ops);
1337 }
1338 
1339 static const struct inno_hdmi_variant rk3036_inno_hdmi_variant = {
1340 	.dev_type = RK3036_HDMI,
1341 	.phy_configs = rk3036_hdmi_phy_configs,
1342 	.default_phy_config = &rk3036_hdmi_phy_configs[1],
1343 };
1344 
1345 static const struct inno_hdmi_variant rk3128_inno_hdmi_variant = {
1346 	.dev_type = RK3128_HDMI,
1347 	.phy_configs = rk3128_hdmi_phy_configs,
1348 	.default_phy_config = &rk3128_hdmi_phy_configs[1],
1349 };
1350 
1351 static const struct of_device_id inno_hdmi_dt_ids[] = {
1352 	{ .compatible = "rockchip,rk3036-inno-hdmi",
1353 	  .data = &rk3036_inno_hdmi_variant,
1354 	},
1355 	{ .compatible = "rockchip,rk3128-inno-hdmi",
1356 	  .data = &rk3128_inno_hdmi_variant,
1357 	},
1358 	{},
1359 };
1360 MODULE_DEVICE_TABLE(of, inno_hdmi_dt_ids);
1361 
1362 struct platform_driver inno_hdmi_driver = {
1363 	.probe  = inno_hdmi_probe,
1364 	.remove = inno_hdmi_remove,
1365 	.driver = {
1366 		.name = "innohdmi-rockchip",
1367 		.of_match_table = inno_hdmi_dt_ids,
1368 	},
1369 };
1370