xref: /OK3568_Linux_fs/kernel/drivers/gpu/drm/rockchip/rk618/rk618_hdmi.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2018 Rockchip Electronics Co. Ltd.
4  *
5  * Author: Chen Shunqing <csq@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/mfd/rk618.h>
14 #include <linux/mfd/syscon.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/of_device.h>
18 #include <linux/regmap.h>
19 #ifdef CONFIG_SWITCH
20 #include <linux/switch.h>
21 #endif
22 
23 #include <drm/drm_of.h>
24 #include <drm/drm_drv.h>
25 #include <drm/drm_atomic_helper.h>
26 #include <drm/drm_crtc_helper.h>
27 #include <drm/drm_edid.h>
28 #include <drm/drm_probe_helper.h>
29 
30 #include <sound/hdmi-codec.h>
31 
32 #include "../rockchip_drm_drv.h"
33 
34 #define RK618_HDMI_BASE			0x0400
35 
36 #define DDC_SEGMENT_ADDR		0x30
37 
38 enum PWR_MODE {
39 	NORMAL,
40 	LOWER_PWR,
41 };
42 
43 #define HDMI_SCL_RATE			(100 * 1000)
44 #define DDC_BUS_FREQ_L			0x4b
45 #define DDC_BUS_FREQ_H			0x4c
46 
47 #define HDMI_SYS_CTRL			0x00
48 #define m_RST_ANALOG			(1 << 6)
49 #define v_RST_ANALOG			(0 << 6)
50 #define v_NOT_RST_ANALOG		(1 << 6)
51 #define m_RST_DIGITAL			(1 << 5)
52 #define v_RST_DIGITAL			(0 << 5)
53 #define v_NOT_RST_DIGITAL		(1 << 5)
54 #define m_REG_CLK_INV			(1 << 4)
55 #define v_REG_CLK_NOT_INV		(0 << 4)
56 #define v_REG_CLK_INV			(1 << 4)
57 #define m_VCLK_INV			(1 << 3)
58 #define v_VCLK_NOT_INV			(0 << 3)
59 #define v_VCLK_INV			(1 << 3)
60 #define m_REG_CLK_SOURCE		(1 << 2)
61 #define v_REG_CLK_SOURCE_TMDS		(0 << 2)
62 #define v_REG_CLK_SOURCE_SYS		(1 << 2)
63 #define m_POWER				(1 << 1)
64 #define v_PWR_ON			(0 << 1)
65 #define v_PWR_OFF			(1 << 1)
66 #define m_INT_POL			(1 << 0)
67 #define v_INT_POL_HIGH			1
68 #define v_INT_POL_LOW			0
69 
70 #define HDMI_VIDEO_CONTROL1		0x01
71 #define m_VIDEO_INPUT_FORMAT		(7 << 1)
72 #define m_DE_SOURCE			(1 << 0)
73 #define v_VIDEO_INPUT_FORMAT(n)		((n) << 1)
74 #define v_DE_EXTERNAL			1
75 #define v_DE_INTERNAL			0
76 enum {
77 	VIDEO_INPUT_SDR_RGB444 = 0,
78 	VIDEO_INPUT_DDR_RGB444 = 5,
79 	VIDEO_INPUT_DDR_YCBCR422 = 6
80 };
81 
82 #define HDMI_VIDEO_CONTROL2		0x02
83 #define m_VIDEO_OUTPUT_COLOR		(3 << 6)
84 #define m_VIDEO_INPUT_BITS		(3 << 4)
85 #define m_VIDEO_INPUT_CSP		(1 << 0)
86 #define v_VIDEO_OUTPUT_COLOR(n)		(((n) & 0x3) << 6)
87 #define v_VIDEO_INPUT_BITS(n)		((n) << 4)
88 #define v_VIDEO_INPUT_CSP(n)		((n) << 0)
89 enum {
90 	VIDEO_INPUT_12BITS = 0,
91 	VIDEO_INPUT_10BITS = 1,
92 	VIDEO_INPUT_REVERT = 2,
93 	VIDEO_INPUT_8BITS = 3,
94 };
95 
96 #define HDMI_VIDEO_CONTROL		0x03
97 #define m_VIDEO_AUTO_CSC		(1 << 7)
98 #define v_VIDEO_AUTO_CSC(n)		((n) << 7)
99 #define m_VIDEO_C0_C2_SWAP		(1 << 0)
100 #define v_VIDEO_C0_C2_SWAP(n)		((n) << 0)
101 enum {
102 	C0_C2_CHANGE_ENABLE = 0,
103 	C0_C2_CHANGE_DISABLE = 1,
104 	AUTO_CSC_DISABLE = 0,
105 	AUTO_CSC_ENABLE = 1,
106 };
107 
108 #define HDMI_VIDEO_CONTROL3		0x04
109 #define m_COLOR_DEPTH_NOT_INDICATED	(1 << 4)
110 #define m_SOF				(1 << 3)
111 #define m_COLOR_RANGE			(1 << 2)
112 #define m_CSC				(1 << 0)
113 #define v_COLOR_DEPTH_NOT_INDICATED(n)	((n) << 4)
114 #define v_SOF_ENABLE			(0 << 3)
115 #define v_SOF_DISABLE			(1 << 3)
116 #define v_COLOR_RANGE_FULL		(1 << 2)
117 #define v_COLOR_RANGE_LIMITED		(0 << 2)
118 #define v_CSC_ENABLE			1
119 #define v_CSC_DISABLE			0
120 
121 #define HDMI_AV_MUTE			0x05
122 #define m_AVMUTE_CLEAR			(1 << 7)
123 #define m_AVMUTE_ENABLE			(1 << 6)
124 #define m_AUDIO_PD			(1 << 2)
125 #define m_AUDIO_MUTE			(1 << 1)
126 #define m_VIDEO_BLACK			(1 << 0)
127 #define v_AVMUTE_CLEAR(n)		((n) << 7)
128 #define v_AVMUTE_ENABLE(n)		((n) << 6)
129 #define v_AUDIO_MUTE(n)			((n) << 1)
130 #define v_AUDIO_PD(n)			((n) << 2)
131 #define v_VIDEO_MUTE(n)			((n) << 0)
132 
133 #define HDMI_VIDEO_TIMING_CTL		0x08
134 #define v_HSYNC_POLARITY(n)		((n) << 3)
135 #define v_VSYNC_POLARITY(n)		((n) << 2)
136 #define v_INETLACE(n)			((n) << 1)
137 #define v_EXTERANL_VIDEO(n)		((n) << 0)
138 
139 #define HDMI_VIDEO_EXT_HTOTAL_L		0x09
140 #define HDMI_VIDEO_EXT_HTOTAL_H		0x0a
141 #define HDMI_VIDEO_EXT_HBLANK_L		0x0b
142 #define HDMI_VIDEO_EXT_HBLANK_H		0x0c
143 #define HDMI_VIDEO_EXT_HDELAY_L		0x0d
144 #define HDMI_VIDEO_EXT_HDELAY_H		0x0e
145 #define HDMI_VIDEO_EXT_HDURATION_L	0x0f
146 #define HDMI_VIDEO_EXT_HDURATION_H	0x10
147 #define HDMI_VIDEO_EXT_VTOTAL_L		0x11
148 #define HDMI_VIDEO_EXT_VTOTAL_H		0x12
149 #define HDMI_VIDEO_EXT_VBLANK		0x13
150 #define HDMI_VIDEO_EXT_VDELAY		0x14
151 #define HDMI_VIDEO_EXT_VDURATION	0x15
152 
153 #define HDMI_VIDEO_CSC_COEF		0x18
154 
155 #define HDMI_AUDIO_CTRL1		0x35
156 enum {
157 	CTS_SOURCE_INTERNAL = 0,
158 	CTS_SOURCE_EXTERNAL = 1,
159 };
160 
161 #define v_CTS_SOURCE(n)			((n) << 7)
162 
163 enum {
164 	DOWNSAMPLE_DISABLE = 0,
165 	DOWNSAMPLE_1_2 = 1,
166 	DOWNSAMPLE_1_4 = 2,
167 };
168 
169 #define v_DOWN_SAMPLE(n)		((n) << 5)
170 
171 enum {
172 	AUDIO_SOURCE_IIS = 0,
173 	AUDIO_SOURCE_SPDIF = 1,
174 };
175 
176 #define v_AUDIO_SOURCE(n)		((n) << 3)
177 
178 #define v_MCLK_ENABLE(n)		((n) << 2)
179 enum {
180 	MCLK_128FS = 0,
181 	MCLK_256FS = 1,
182 	MCLK_384FS = 2,
183 	MCLK_512FS = 3,
184 };
185 
186 #define v_MCLK_RATIO(n)			(n)
187 
188 #define AUDIO_SAMPLE_RATE		0x37
189 enum {
190 	AUDIO_32K = 0x3,
191 	AUDIO_441K = 0x0,
192 	AUDIO_48K = 0x2,
193 	AUDIO_882K = 0x8,
194 	AUDIO_96K = 0xa,
195 	AUDIO_1764K = 0xc,
196 	AUDIO_192K = 0xe,
197 };
198 
199 #define AUDIO_I2S_MODE			0x38
200 enum {
201 	I2S_CHANNEL_1_2 = 1,
202 	I2S_CHANNEL_3_4 = 3,
203 	I2S_CHANNEL_5_6 = 7,
204 	I2S_CHANNEL_7_8 = 0xf
205 };
206 
207 #define v_I2S_CHANNEL(n)		((n) << 2)
208 enum {
209 	I2S_STANDARD = 0,
210 	I2S_LEFT_JUSTIFIED = 1,
211 	I2S_RIGHT_JUSTIFIED = 2,
212 };
213 
214 #define v_I2S_MODE(n)			(n)
215 
216 #define AUDIO_I2S_MAP			0x39
217 #define AUDIO_I2S_SWAPS_SPDIF		0x3a
218 #define v_SPIDF_FREQ(n)			(n)
219 
220 #define N_32K				0x1000
221 #define N_441K				0x1880
222 #define N_882K				0x3100
223 #define N_1764K				0x6200
224 #define N_48K				0x1800
225 #define N_96K				0x3000
226 #define N_192K				0x6000
227 
228 #define HDMI_AUDIO_CHANNEL_STATUS	0x3e
229 #define m_AUDIO_STATUS_NLPCM		(1 << 7)
230 #define m_AUDIO_STATUS_USE		(1 << 6)
231 #define m_AUDIO_STATUS_COPYRIGHT	(1 << 5)
232 #define m_AUDIO_STATUS_ADDITION		(3 << 2)
233 #define m_AUDIO_STATUS_CLK_ACCURACY	(2 << 0)
234 #define v_AUDIO_STATUS_NLPCM(n)		(((n) & 1) << 7)
235 #define AUDIO_N_H			0x3f
236 #define AUDIO_N_M			0x40
237 #define AUDIO_N_L			0x41
238 
239 #define HDMI_AUDIO_CTS_H		0x45
240 #define HDMI_AUDIO_CTS_M		0x46
241 #define HDMI_AUDIO_CTS_L		0x47
242 
243 #define HDMI_DDC_CLK_L			0x4b
244 #define HDMI_DDC_CLK_H			0x4c
245 
246 #define HDMI_EDID_SEGMENT_POINTER	0x4d
247 #define HDMI_EDID_WORD_ADDR		0x4e
248 #define HDMI_EDID_FIFO_OFFSET		0x4f
249 #define HDMI_EDID_FIFO_ADDR		0x50
250 
251 #define HDMI_PACKET_SEND_MANUAL		0x9c
252 #define HDMI_PACKET_SEND_AUTO		0x9d
253 #define m_PACKET_GCP_EN			(1 << 7)
254 #define m_PACKET_MSI_EN			(1 << 6)
255 #define m_PACKET_SDI_EN			(1 << 5)
256 #define m_PACKET_VSI_EN			(1 << 4)
257 #define v_PACKET_GCP_EN(n)		(((n) & 1) << 7)
258 #define v_PACKET_MSI_EN(n)		(((n) & 1) << 6)
259 #define v_PACKET_SDI_EN(n)		(((n) & 1) << 5)
260 #define v_PACKET_VSI_EN(n)		(((n) & 1) << 4)
261 
262 #define HDMI_CONTROL_PACKET_BUF_INDEX	0x9f
263 enum {
264 	INFOFRAME_VSI = 0x05,
265 	INFOFRAME_AVI = 0x06,
266 	INFOFRAME_AAI = 0x08,
267 };
268 
269 #define HDMI_CONTROL_PACKET_ADDR	0xa0
270 #define HDMI_MAXIMUM_INFO_FRAME_SIZE	0x11
271 enum {
272 	AVI_COLOR_MODE_RGB = 0,
273 	AVI_COLOR_MODE_YCBCR422 = 1,
274 	AVI_COLOR_MODE_YCBCR444 = 2,
275 	AVI_COLORIMETRY_NO_DATA = 0,
276 
277 	AVI_COLORIMETRY_SMPTE_170M = 1,
278 	AVI_COLORIMETRY_ITU709 = 2,
279 	AVI_COLORIMETRY_EXTENDED = 3,
280 
281 	AVI_CODED_FRAME_ASPECT_NO_DATA = 0,
282 	AVI_CODED_FRAME_ASPECT_4_3 = 1,
283 	AVI_CODED_FRAME_ASPECT_16_9 = 2,
284 
285 	ACTIVE_ASPECT_RATE_SAME_AS_CODED_FRAME = 0x08,
286 	ACTIVE_ASPECT_RATE_4_3 = 0x09,
287 	ACTIVE_ASPECT_RATE_16_9 = 0x0A,
288 	ACTIVE_ASPECT_RATE_14_9 = 0x0B,
289 };
290 
291 #define HDMI_HDCP_CTRL			0x52
292 #define m_HDMI_DVI			(1 << 1)
293 #define v_HDMI_DVI(n)			((n) << 1)
294 
295 #define HDMI_INTERRUPT_MASK1		0xc0
296 #define HDMI_INTERRUPT_STATUS1		0xc1
297 #define m_INT_HOTPLUG_RK618		BIT(7)
298 #define	m_INT_ACTIVE_VSYNC		(1 << 5)
299 #define m_INT_EDID_READY		(1 << 2)
300 
301 #define HDMI_INTERRUPT_MASK2		0xc2
302 #define HDMI_INTERRUPT_STATUS2		0xc3
303 #define m_INT_HDCP_ERR			(1 << 7)
304 #define m_INT_BKSV_FLAG			(1 << 6)
305 #define m_INT_HDCP_OK			(1 << 4)
306 
307 #define HDMI_STATUS			0xc8
308 #define m_HOTPLUG			(1 << 7)
309 #define m_MASK_INT_HOTPLUG		(1 << 5)
310 #define m_INT_HOTPLUG			(1 << 1)
311 #define v_MASK_INT_HOTPLUG(n)		(((n) & 0x1) << 5)
312 
313 #define HDMI_COLORBAR                   0xc9
314 
315 #define HDMI_PHY_SYNC			0xce
316 #define HDMI_PHY_SYS_CTL		0xe0
317 #define m_TMDS_CLK_SOURCE		(1 << 5)
318 #define v_TMDS_FROM_PLL			(0 << 5)
319 #define v_TMDS_FROM_GEN			(1 << 5)
320 #define m_PHASE_CLK			(1 << 4)
321 #define v_DEFAULT_PHASE			(0 << 4)
322 #define v_SYNC_PHASE			(1 << 4)
323 #define m_TMDS_CURRENT_PWR		(1 << 3)
324 #define v_TURN_ON_CURRENT		(0 << 3)
325 #define v_CAT_OFF_CURRENT		(1 << 3)
326 #define m_BANDGAP_PWR			(1 << 2)
327 #define v_BANDGAP_PWR_UP		(0 << 2)
328 #define v_BANDGAP_PWR_DOWN		(1 << 2)
329 #define m_PLL_PWR			(1 << 1)
330 #define v_PLL_PWR_UP			(0 << 1)
331 #define v_PLL_PWR_DOWN			(1 << 1)
332 #define m_TMDS_CHG_PWR			(1 << 0)
333 #define v_TMDS_CHG_PWR_UP		(0 << 0)
334 #define v_TMDS_CHG_PWR_DOWN		(1 << 0)
335 
336 #define HDMI_PHY_CHG_PWR		0xe1
337 #define v_CLK_CHG_PWR(n)		(((n) & 1) << 3)
338 #define v_DATA_CHG_PWR(n)		(((n) & 7) << 0)
339 
340 #define HDMI_PHY_DRIVER			0xe2
341 #define v_CLK_MAIN_DRIVER(n)		((n) << 4)
342 #define v_DATA_MAIN_DRIVER(n)		((n) << 0)
343 
344 #define HDMI_PHY_PRE_EMPHASIS		0xe3
345 #define v_PRE_EMPHASIS(n)		(((n) & 7) << 4)
346 #define v_CLK_PRE_DRIVER(n)		(((n) & 3) << 2)
347 #define v_DATA_PRE_DRIVER(n)		(((n) & 3) << 0)
348 
349 #define HDMI_PHY_FEEDBACK_DIV_RATIO_LOW		0xe7
350 #define v_FEEDBACK_DIV_LOW(n)			((n) & 0xff)
351 #define HDMI_PHY_FEEDBACK_DIV_RATIO_HIGH	0xe8
352 #define v_FEEDBACK_DIV_HIGH(n)			((n) & 1)
353 
354 #define HDMI_PHY_PRE_DIV_RATIO		0xed
355 #define v_PRE_DIV_RATIO(n)		((n) & 0x1f)
356 
357 #define HDMI_CEC_CTRL			0xd0
358 #define m_ADJUST_FOR_HISENSE		(1 << 6)
359 #define m_REJECT_RX_BROADCAST		(1 << 5)
360 #define m_BUSFREETIME_ENABLE		(1 << 2)
361 #define m_REJECT_RX			(1 << 1)
362 #define m_START_TX			(1 << 0)
363 
364 #define HDMI_CEC_DATA			0xd1
365 #define HDMI_CEC_TX_OFFSET		0xd2
366 #define HDMI_CEC_RX_OFFSET		0xd3
367 #define HDMI_CEC_CLK_H			0xd4
368 #define HDMI_CEC_CLK_L			0xd5
369 #define HDMI_CEC_TX_LENGTH		0xd6
370 #define HDMI_CEC_RX_LENGTH		0xd7
371 #define HDMI_CEC_TX_INT_MASK		0xd8
372 #define m_TX_DONE			(1 << 3)
373 #define m_TX_NOACK			(1 << 2)
374 #define m_TX_BROADCAST_REJ		(1 << 1)
375 #define m_TX_BUSNOTFREE			(1 << 0)
376 
377 #define HDMI_CEC_RX_INT_MASK		0xd9
378 #define m_RX_LA_ERR			(1 << 4)
379 #define m_RX_GLITCH			(1 << 3)
380 #define m_RX_DONE			(1 << 0)
381 
382 #define HDMI_CEC_TX_INT			0xda
383 #define HDMI_CEC_RX_INT			0xdb
384 #define HDMI_CEC_BUSFREETIME_L		0xdc
385 #define HDMI_CEC_BUSFREETIME_H		0xdd
386 #define HDMI_CEC_LOGICADDR		0xde
387 
388 struct audio_info {
389 	int sample_rate;
390 	int channels;
391 	int sample_width;
392 };
393 
394 struct hdmi_data_info {
395 	int vic;
396 	bool sink_is_hdmi;
397 	bool sink_has_audio;
398 	unsigned int enc_in_format;
399 	unsigned int enc_out_format;
400 	unsigned int colorimetry;
401 };
402 
403 struct rk618_hdmi_i2c {
404 	struct i2c_adapter adap;
405 
406 	u8 ddc_addr;
407 	u8 segment_addr;
408 
409 	struct mutex lock;
410 };
411 
412 struct rk618_hdmi_phy_config {
413 	unsigned long mpixelclock;
414 	u8 pre_emphasis;	/* pre-emphasis value */
415 	u8 vlev_ctr;		/* voltage level control */
416 };
417 
418 struct rk618_hdmi {
419 	struct device *dev;
420 	int irq;
421 	struct regmap *regmap;
422 	struct rk618 *parent;
423 	struct clk *clock;
424 
425 	struct drm_bridge base;
426 	struct drm_connector connector;
427 	struct drm_bridge *bridge;
428 
429 	struct rk618_hdmi_i2c *i2c;
430 	struct i2c_adapter *ddc;
431 
432 	unsigned int tmds_rate;
433 
434 	struct platform_device *audio_pdev;
435 	bool audio_enable;
436 
437 	struct hdmi_data_info	hdmi_data;
438 	struct drm_display_mode previous_mode;
439 #ifdef CONFIG_SWITCH
440 	struct switch_dev switchdev;
441 #endif
442 	struct rockchip_drm_sub_dev sub_dev;
443 };
444 
445 enum {
446 	CSC_ITU601_16_235_TO_RGB_0_255_8BIT,
447 	CSC_ITU601_0_255_TO_RGB_0_255_8BIT,
448 	CSC_ITU709_16_235_TO_RGB_0_255_8BIT,
449 	CSC_RGB_0_255_TO_ITU601_16_235_8BIT,
450 	CSC_RGB_0_255_TO_ITU709_16_235_8BIT,
451 	CSC_RGB_0_255_TO_RGB_16_235_8BIT,
452 };
453 
454 static const char coeff_csc[][24] = {
455 	/*
456 	 * YUV2RGB:601 SD mode(Y[16:235], UV[16:240], RGB[0:255]):
457 	 *   R = 1.164*Y + 1.596*V - 204
458 	 *   G = 1.164*Y - 0.391*U - 0.813*V + 154
459 	 *   B = 1.164*Y + 2.018*U - 258
460 	 */
461 	{
462 		0x04, 0xa7, 0x00, 0x00, 0x06, 0x62, 0x02, 0xcc,
463 		0x04, 0xa7, 0x11, 0x90, 0x13, 0x40, 0x00, 0x9a,
464 		0x04, 0xa7, 0x08, 0x12, 0x00, 0x00, 0x03, 0x02
465 	},
466 	/*
467 	 * YUV2RGB:601 SD mode(YUV[0:255],RGB[0:255]):
468 	 *   R = Y + 1.402*V - 248
469 	 *   G = Y - 0.344*U - 0.714*V + 135
470 	 *   B = Y + 1.772*U - 227
471 	 */
472 	{
473 		0x04, 0x00, 0x00, 0x00, 0x05, 0x9b, 0x02, 0xf8,
474 		0x04, 0x00, 0x11, 0x60, 0x12, 0xdb, 0x00, 0x87,
475 		0x04, 0x00, 0x07, 0x16, 0x00, 0x00, 0x02, 0xe3
476 	},
477 	/*
478 	 * YUV2RGB:709 HD mode(Y[16:235],UV[16:240],RGB[0:255]):
479 	 *   R = 1.164*Y + 1.793*V - 248
480 	 *   G = 1.164*Y - 0.213*U - 0.534*V + 77
481 	 *   B = 1.164*Y + 2.115*U - 289
482 	 */
483 	{
484 		0x04, 0xa7, 0x00, 0x00, 0x07, 0x2c, 0x02, 0xf8,
485 		0x04, 0xa7, 0x10, 0xda, 0x12, 0x22, 0x00, 0x4d,
486 		0x04, 0xa7, 0x08, 0x74, 0x00, 0x00, 0x03, 0x21
487 	},
488 
489 	/*
490 	 * RGB2YUV:601 SD mode:
491 	 *   Cb = -0.291G - 0.148R + 0.439B + 128
492 	 *   Y  = 0.504G  + 0.257R + 0.098B + 16
493 	 *   Cr = -0.368G + 0.439R - 0.071B + 128
494 	 */
495 	{
496 		0x11, 0x5f, 0x01, 0x82, 0x10, 0x23, 0x00, 0x80,
497 		0x02, 0x1c, 0x00, 0xa1, 0x00, 0x36, 0x00, 0x1e,
498 		0x11, 0x29, 0x10, 0x59, 0x01, 0x82, 0x00, 0x80
499 	},
500 	/*
501 	 * RGB2YUV:709 HD mode:
502 	 *   Cb = - 0.338G - 0.101R + 0.439B + 128
503 	 *   Y  = 0.614G   + 0.183R + 0.062B + 16
504 	 *   Cr = - 0.399G + 0.439R - 0.040B + 128
505 	 */
506 	{
507 		0x11, 0x98, 0x01, 0xc1, 0x10, 0x28, 0x00, 0x80,
508 		0x02, 0x74, 0x00, 0xbb, 0x00, 0x3f, 0x00, 0x10,
509 		0x11, 0x5a, 0x10, 0x67, 0x01, 0xc1, 0x00, 0x80
510 	},
511 	/*
512 	 * RGB[0:255]2RGB[16:235]:
513 	 *   R' = R x (235-16)/255 + 16;
514 	 *   G' = G x (235-16)/255 + 16;
515 	 *   B' = B x (235-16)/255 + 16;
516 	 */
517 	{
518 		0x00, 0x00, 0x03, 0x6F, 0x00, 0x00, 0x00, 0x10,
519 		0x03, 0x6F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
520 		0x00, 0x00, 0x00, 0x00, 0x03, 0x6F, 0x00, 0x10
521 	},
522 };
523 
bridge_to_hdmi(struct drm_bridge * b)524 static inline struct rk618_hdmi *bridge_to_hdmi(struct drm_bridge *b)
525 {
526 	return container_of(b, struct rk618_hdmi, base);
527 }
528 
connector_to_hdmi(struct drm_connector * c)529 static inline struct rk618_hdmi *connector_to_hdmi(struct drm_connector *c)
530 {
531 	return container_of(c, struct rk618_hdmi, connector);
532 }
533 
hdmi_readb(struct rk618_hdmi * hdmi,u16 offset)534 static inline u8 hdmi_readb(struct rk618_hdmi *hdmi, u16 offset)
535 {
536 	u32 val;
537 
538 	regmap_read(hdmi->regmap, (RK618_HDMI_BASE + ((offset) << 2)), &val);
539 
540 	return val;
541 }
542 
hdmi_writeb(struct rk618_hdmi * hdmi,u16 offset,u32 val)543 static inline void hdmi_writeb(struct rk618_hdmi *hdmi, u16 offset, u32 val)
544 {
545 	regmap_write(hdmi->regmap, (RK618_HDMI_BASE + ((offset) << 2)), val);
546 }
547 
rk618_hdmi_set_polarity(struct rk618_hdmi * hdmi,int vic)548 static void rk618_hdmi_set_polarity(struct rk618_hdmi *hdmi, int vic)
549 {
550 	u32 val, mask = HDMI_HSYNC_POL_INV | HDMI_VSYNC_POL_INV;
551 
552 	if (vic == 76 || vic == 75 || vic == 5 || vic == 20 ||
553 	    vic == 39 || vic == 16 || vic == 4)
554 		val = HDMI_HSYNC_POL_INV | HDMI_VSYNC_POL_INV;
555 	else
556 		val = 0;
557 
558 	regmap_update_bits(hdmi->parent->regmap, RK618_MISC_CON, mask, val);
559 }
560 
rk618_hdmi_pol_init(struct rk618_hdmi * hdmi,int pol)561 static void rk618_hdmi_pol_init(struct rk618_hdmi *hdmi, int pol)
562 {
563 	u32 val;
564 
565 	if (pol)
566 		val = 0x0;
567 	else
568 		val = 0x20;
569 	regmap_update_bits(hdmi->parent->regmap, RK618_MISC_CON,
570 			   INT_ACTIVE_LOW, val);
571 
572 	regmap_update_bits(hdmi->parent->regmap,
573 			   RK618_MISC_CON, HDMI_CLK_SEL_MASK,
574 			   HDMI_CLK_SEL_VIDEO_INF0_CLK);
575 }
576 
hdmi_modb(struct rk618_hdmi * hdmi,u16 offset,u32 msk,u32 val)577 static inline void hdmi_modb(struct rk618_hdmi *hdmi, u16 offset,
578 			     u32 msk, u32 val)
579 {
580 	u8 temp = hdmi_readb(hdmi, offset) & ~msk;
581 
582 	temp |= val & msk;
583 	hdmi_writeb(hdmi, offset, temp);
584 }
585 
rk618_hdmi_i2c_init(struct rk618_hdmi * hdmi)586 static void rk618_hdmi_i2c_init(struct rk618_hdmi *hdmi)
587 {
588 	int ddc_bus_freq;
589 
590 	ddc_bus_freq = (hdmi->tmds_rate >> 2) / HDMI_SCL_RATE;
591 
592 	hdmi_writeb(hdmi, DDC_BUS_FREQ_L, ddc_bus_freq & 0xFF);
593 	hdmi_writeb(hdmi, DDC_BUS_FREQ_H, (ddc_bus_freq >> 8) & 0xFF);
594 
595 	/* Clear the EDID interrupt flag and mute the interrupt */
596 	hdmi_writeb(hdmi, HDMI_INTERRUPT_MASK1, 0);
597 	hdmi_writeb(hdmi, HDMI_INTERRUPT_STATUS1, m_INT_EDID_READY);
598 	hdmi_modb(hdmi, HDMI_INTERRUPT_MASK1, m_INT_HOTPLUG_RK618,
599 		  m_INT_HOTPLUG_RK618);
600 }
601 
rk618_hdmi_sys_power(struct rk618_hdmi * hdmi,bool enable)602 static void rk618_hdmi_sys_power(struct rk618_hdmi *hdmi, bool enable)
603 {
604 	if (enable)
605 		hdmi_modb(hdmi, HDMI_SYS_CTRL, m_POWER, v_PWR_ON);
606 	else
607 		hdmi_modb(hdmi, HDMI_SYS_CTRL, m_POWER, v_PWR_OFF);
608 }
609 
610 static struct rk618_hdmi_phy_config rk618_hdmi_phy_config[] = {
611 	/* pixelclk pre-emp vlev */
612 	{ 74250000,  0x0f, 0xaa },
613 	{ 165000000, 0x0f, 0xaa },
614 	{ ~0UL,	     0x00, 0x00 }
615 };
616 
rk618_hdmi_set_pwr_mode(struct rk618_hdmi * hdmi,int mode)617 static void rk618_hdmi_set_pwr_mode(struct rk618_hdmi *hdmi, int mode)
618 {
619 	const struct rk618_hdmi_phy_config *phy_config =
620 						rk618_hdmi_phy_config;
621 
622 	switch (mode) {
623 	case NORMAL:
624 		rk618_hdmi_sys_power(hdmi, false);
625 		for (; phy_config->mpixelclock != ~0UL; phy_config++)
626 			if (hdmi->tmds_rate <= phy_config->mpixelclock)
627 				break;
628 		if (!phy_config->mpixelclock)
629 			return;
630 		hdmi_writeb(hdmi, HDMI_PHY_PRE_EMPHASIS,
631 			    phy_config->pre_emphasis);
632 		hdmi_writeb(hdmi, HDMI_PHY_DRIVER, phy_config->vlev_ctr);
633 
634 		hdmi_writeb(hdmi, HDMI_PHY_SYS_CTL, 0x2d);
635 		hdmi_writeb(hdmi, HDMI_PHY_SYS_CTL, 0x2c);
636 		hdmi_writeb(hdmi, HDMI_PHY_SYS_CTL, 0x28);
637 		hdmi_writeb(hdmi, HDMI_PHY_SYS_CTL, 0x20);
638 		hdmi_writeb(hdmi, HDMI_PHY_CHG_PWR, 0x0f);
639 		hdmi_writeb(hdmi, HDMI_PHY_SYNC, 0x00);
640 		hdmi_writeb(hdmi, HDMI_PHY_SYNC, 0x01);
641 
642 		rk618_hdmi_sys_power(hdmi, true);
643 		break;
644 
645 	case LOWER_PWR:
646 		rk618_hdmi_sys_power(hdmi, false);
647 		hdmi_writeb(hdmi, HDMI_PHY_DRIVER, 0x00);
648 		hdmi_writeb(hdmi, HDMI_PHY_PRE_EMPHASIS, 0x00);
649 		hdmi_writeb(hdmi, HDMI_PHY_CHG_PWR, 0x00);
650 		hdmi_writeb(hdmi, HDMI_PHY_SYS_CTL, 0x2f);
651 		break;
652 
653 	default:
654 		dev_err(hdmi->dev, "Unknown power mode %d\n", mode);
655 	}
656 }
657 
rk618_hdmi_reset(struct rk618_hdmi * hdmi)658 static void rk618_hdmi_reset(struct rk618_hdmi *hdmi)
659 {
660 	u32 val;
661 	u32 msk;
662 
663 	hdmi_modb(hdmi, HDMI_SYS_CTRL, m_RST_DIGITAL, v_NOT_RST_DIGITAL);
664 	usleep_range(100, 110);
665 
666 	hdmi_modb(hdmi, HDMI_SYS_CTRL, m_RST_ANALOG, v_NOT_RST_ANALOG);
667 	usleep_range(100, 110);
668 
669 	msk = m_REG_CLK_INV | m_REG_CLK_SOURCE | m_POWER | m_INT_POL;
670 	val = v_REG_CLK_INV | v_REG_CLK_SOURCE_SYS | v_PWR_ON | v_INT_POL_HIGH;
671 	hdmi_modb(hdmi, HDMI_SYS_CTRL, msk, val);
672 
673 	rk618_hdmi_set_pwr_mode(hdmi, NORMAL);
674 }
675 
rk618_hdmi_upload_frame(struct rk618_hdmi * hdmi,int setup_rc,union hdmi_infoframe * frame,u32 frame_index,u32 mask,u32 disable,u32 enable)676 static int rk618_hdmi_upload_frame(struct rk618_hdmi *hdmi, int setup_rc,
677 				   union hdmi_infoframe *frame, u32 frame_index,
678 				   u32 mask, u32 disable, u32 enable)
679 {
680 	if (mask)
681 		hdmi_modb(hdmi, HDMI_PACKET_SEND_AUTO, mask, disable);
682 
683 	hdmi_writeb(hdmi, HDMI_CONTROL_PACKET_BUF_INDEX, frame_index);
684 
685 	if (setup_rc >= 0) {
686 		u8 packed_frame[HDMI_MAXIMUM_INFO_FRAME_SIZE];
687 		ssize_t rc, i;
688 
689 		rc = hdmi_infoframe_pack(frame, packed_frame,
690 					 sizeof(packed_frame));
691 		if (rc < 0)
692 			return rc;
693 
694 		for (i = 0; i < rc; i++)
695 			hdmi_writeb(hdmi, HDMI_CONTROL_PACKET_ADDR + i,
696 				    packed_frame[i]);
697 
698 		if (mask)
699 			hdmi_modb(hdmi, HDMI_PACKET_SEND_AUTO, mask, enable);
700 	}
701 
702 	return setup_rc;
703 }
704 
rk618_hdmi_config_video_vsi(struct rk618_hdmi * hdmi,struct drm_display_mode * mode)705 static int rk618_hdmi_config_video_vsi(struct rk618_hdmi *hdmi,
706 				       struct drm_display_mode *mode)
707 {
708 	union hdmi_infoframe frame;
709 	int rc;
710 
711 	rc = drm_hdmi_vendor_infoframe_from_display_mode(&frame.vendor.hdmi,
712 							 &hdmi->connector,
713 							 mode);
714 
715 	return rk618_hdmi_upload_frame(hdmi, rc, &frame,
716 				       INFOFRAME_VSI, m_PACKET_VSI_EN,
717 				       v_PACKET_VSI_EN(0), v_PACKET_VSI_EN(1));
718 }
719 
rk618_hdmi_config_video_avi(struct rk618_hdmi * hdmi,struct drm_display_mode * mode)720 static int rk618_hdmi_config_video_avi(struct rk618_hdmi *hdmi,
721 				       struct drm_display_mode *mode)
722 {
723 	union hdmi_infoframe frame;
724 	int rc;
725 
726 	rc = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi, &hdmi->connector, mode);
727 
728 	if (hdmi->hdmi_data.enc_out_format == HDMI_COLORSPACE_YUV444)
729 		frame.avi.colorspace = HDMI_COLORSPACE_YUV444;
730 	else if (hdmi->hdmi_data.enc_out_format == HDMI_COLORSPACE_YUV422)
731 		frame.avi.colorspace = HDMI_COLORSPACE_YUV422;
732 	else
733 		frame.avi.colorspace = HDMI_COLORSPACE_RGB;
734 
735 	if (frame.avi.colorspace != HDMI_COLORSPACE_RGB)
736 		frame.avi.colorimetry = hdmi->hdmi_data.colorimetry;
737 
738 	frame.avi.scan_mode = HDMI_SCAN_MODE_NONE;
739 
740 	return rk618_hdmi_upload_frame(hdmi, rc, &frame,
741 				       INFOFRAME_AVI, 0, 0, 0);
742 }
743 
rk618_hdmi_config_audio_aai(struct rk618_hdmi * hdmi,struct audio_info * audio)744 static int rk618_hdmi_config_audio_aai(struct rk618_hdmi *hdmi,
745 				       struct audio_info *audio)
746 {
747 	struct hdmi_audio_infoframe *faudio;
748 	union hdmi_infoframe frame;
749 	int rc;
750 
751 	rc = hdmi_audio_infoframe_init(&frame.audio);
752 	faudio = (struct hdmi_audio_infoframe *)&frame;
753 
754 	faudio->channels = audio->channels;
755 
756 	return rk618_hdmi_upload_frame(hdmi, rc, &frame,
757 				       INFOFRAME_AAI, 0, 0, 0);
758 }
759 
rk618_hdmi_config_video_csc(struct rk618_hdmi * hdmi)760 static int rk618_hdmi_config_video_csc(struct rk618_hdmi *hdmi)
761 {
762 	struct hdmi_data_info *data = &hdmi->hdmi_data;
763 	int c0_c2_change = 0;
764 	int csc_enable = 0;
765 	int csc_mode = 0;
766 	int auto_csc = 0;
767 	int value;
768 	int i;
769 
770 	/* Input video mode is SDR RGB24bit, data enable signal from external */
771 	hdmi_writeb(hdmi, HDMI_VIDEO_CONTROL1, v_DE_EXTERNAL |
772 		    v_VIDEO_INPUT_FORMAT(VIDEO_INPUT_SDR_RGB444));
773 
774 	/* Input color hardcode to RGB, and output color hardcode to RGB888 */
775 	value = v_VIDEO_INPUT_BITS(VIDEO_INPUT_8BITS) |
776 		v_VIDEO_OUTPUT_COLOR(0) |
777 		v_VIDEO_INPUT_CSP(0);
778 	hdmi_writeb(hdmi, HDMI_VIDEO_CONTROL2, value);
779 
780 	if (data->enc_in_format == data->enc_out_format) {
781 		if ((data->enc_in_format == HDMI_COLORSPACE_RGB) ||
782 		    (data->enc_in_format >= HDMI_COLORSPACE_YUV444)) {
783 			value = v_SOF_DISABLE | v_COLOR_DEPTH_NOT_INDICATED(1);
784 			hdmi_writeb(hdmi, HDMI_VIDEO_CONTROL3, value);
785 
786 			hdmi_modb(hdmi, HDMI_VIDEO_CONTROL,
787 				  m_VIDEO_AUTO_CSC | m_VIDEO_C0_C2_SWAP,
788 				  v_VIDEO_AUTO_CSC(AUTO_CSC_DISABLE) |
789 				  v_VIDEO_C0_C2_SWAP(C0_C2_CHANGE_DISABLE));
790 			return 0;
791 		}
792 	}
793 
794 	if (data->colorimetry == HDMI_COLORIMETRY_ITU_601) {
795 		if ((data->enc_in_format == HDMI_COLORSPACE_RGB) &&
796 		    (data->enc_out_format == HDMI_COLORSPACE_YUV444)) {
797 			csc_mode = CSC_RGB_0_255_TO_ITU601_16_235_8BIT;
798 			auto_csc = AUTO_CSC_DISABLE;
799 			c0_c2_change = C0_C2_CHANGE_DISABLE;
800 			csc_enable = v_CSC_ENABLE;
801 		} else if ((data->enc_in_format == HDMI_COLORSPACE_YUV444) &&
802 			   (data->enc_out_format == HDMI_COLORSPACE_RGB)) {
803 			csc_mode = CSC_ITU601_16_235_TO_RGB_0_255_8BIT;
804 			auto_csc = AUTO_CSC_ENABLE;
805 			c0_c2_change = C0_C2_CHANGE_DISABLE;
806 			csc_enable = v_CSC_DISABLE;
807 		}
808 	} else {
809 		if ((data->enc_in_format == HDMI_COLORSPACE_RGB) &&
810 		    (data->enc_out_format == HDMI_COLORSPACE_YUV444)) {
811 			csc_mode = CSC_RGB_0_255_TO_ITU709_16_235_8BIT;
812 			auto_csc = AUTO_CSC_DISABLE;
813 			c0_c2_change = C0_C2_CHANGE_DISABLE;
814 			csc_enable = v_CSC_ENABLE;
815 		} else if ((data->enc_in_format == HDMI_COLORSPACE_YUV444) &&
816 			   (data->enc_out_format == HDMI_COLORSPACE_RGB)) {
817 			csc_mode = CSC_ITU709_16_235_TO_RGB_0_255_8BIT;
818 			auto_csc = AUTO_CSC_ENABLE;
819 			c0_c2_change = C0_C2_CHANGE_DISABLE;
820 			csc_enable = v_CSC_DISABLE;
821 		}
822 	}
823 
824 	for (i = 0; i < 24; i++)
825 		hdmi_writeb(hdmi, HDMI_VIDEO_CSC_COEF + i,
826 			    coeff_csc[csc_mode][i]);
827 
828 	value = v_SOF_DISABLE | csc_enable | v_COLOR_DEPTH_NOT_INDICATED(1);
829 	hdmi_writeb(hdmi, HDMI_VIDEO_CONTROL3, value);
830 	hdmi_modb(hdmi, HDMI_VIDEO_CONTROL, m_VIDEO_AUTO_CSC |
831 		  m_VIDEO_C0_C2_SWAP, v_VIDEO_AUTO_CSC(auto_csc) |
832 		  v_VIDEO_C0_C2_SWAP(c0_c2_change));
833 
834 	return 0;
835 }
836 
rk618_hdmi_config_video_timing(struct rk618_hdmi * hdmi,struct drm_display_mode * mode)837 static int rk618_hdmi_config_video_timing(struct rk618_hdmi *hdmi,
838 					  struct drm_display_mode *mode)
839 {
840 	int value;
841 
842 	/* Set detail external video timing polarity and interlace mode */
843 	value = v_EXTERANL_VIDEO(1);
844 	value |= mode->flags & DRM_MODE_FLAG_PHSYNC ?
845 		 v_HSYNC_POLARITY(1) : v_HSYNC_POLARITY(0);
846 	value |= mode->flags & DRM_MODE_FLAG_PVSYNC ?
847 		 v_VSYNC_POLARITY(1) : v_VSYNC_POLARITY(0);
848 	value |= mode->flags & DRM_MODE_FLAG_INTERLACE ?
849 		 v_INETLACE(1) : v_INETLACE(0);
850 	hdmi_writeb(hdmi, HDMI_VIDEO_TIMING_CTL, value);
851 
852 	/* Set detail external video timing */
853 	value = mode->htotal;
854 	hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HTOTAL_L, value & 0xFF);
855 	hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HTOTAL_H, (value >> 8) & 0xFF);
856 
857 	value = mode->htotal - mode->hdisplay;
858 	hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HBLANK_L, value & 0xFF);
859 	hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HBLANK_H, (value >> 8) & 0xFF);
860 
861 	value = mode->htotal - mode->hsync_start;
862 	hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HDELAY_L, value & 0xFF);
863 	hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HDELAY_H, (value >> 8) & 0xFF);
864 
865 	value = mode->hsync_end - mode->hsync_start;
866 	hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HDURATION_L, value & 0xFF);
867 	hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HDURATION_H, (value >> 8) & 0xFF);
868 
869 	value = mode->vtotal;
870 	hdmi_writeb(hdmi, HDMI_VIDEO_EXT_VTOTAL_L, value & 0xFF);
871 	hdmi_writeb(hdmi, HDMI_VIDEO_EXT_VTOTAL_H, (value >> 8) & 0xFF);
872 
873 	value = mode->vtotal - mode->vdisplay;
874 	hdmi_writeb(hdmi, HDMI_VIDEO_EXT_VBLANK, value & 0xFF);
875 
876 	value = mode->vtotal - mode->vsync_start;
877 	hdmi_writeb(hdmi, HDMI_VIDEO_EXT_VDELAY, value & 0xFF);
878 
879 	value = mode->vsync_end - mode->vsync_start;
880 	hdmi_writeb(hdmi, HDMI_VIDEO_EXT_VDURATION, value & 0xFF);
881 
882 	hdmi_writeb(hdmi, HDMI_PHY_PRE_DIV_RATIO, 0x1e);
883 	hdmi_writeb(hdmi, HDMI_PHY_FEEDBACK_DIV_RATIO_LOW, 0x2c);
884 	hdmi_writeb(hdmi, HDMI_PHY_FEEDBACK_DIV_RATIO_HIGH, 0x01);
885 
886 	return 0;
887 }
888 
rk618_hdmi_setup(struct rk618_hdmi * hdmi,struct drm_display_mode * mode)889 static int rk618_hdmi_setup(struct rk618_hdmi *hdmi,
890 			    struct drm_display_mode *mode)
891 {
892 	hdmi->hdmi_data.vic = drm_match_cea_mode(mode);
893 
894 	hdmi->hdmi_data.enc_in_format = HDMI_COLORSPACE_RGB;
895 	hdmi->hdmi_data.enc_out_format = HDMI_COLORSPACE_RGB;
896 
897 	if ((hdmi->hdmi_data.vic == 6) || (hdmi->hdmi_data.vic == 7) ||
898 	    (hdmi->hdmi_data.vic == 21) || (hdmi->hdmi_data.vic == 22) ||
899 	    (hdmi->hdmi_data.vic == 2) || (hdmi->hdmi_data.vic == 3) ||
900 	    (hdmi->hdmi_data.vic == 17) || (hdmi->hdmi_data.vic == 18))
901 		hdmi->hdmi_data.colorimetry = HDMI_COLORIMETRY_ITU_601;
902 	else
903 		hdmi->hdmi_data.colorimetry = HDMI_COLORIMETRY_ITU_709;
904 
905 	/* Mute video and audio output */
906 	hdmi_modb(hdmi, HDMI_AV_MUTE, m_AUDIO_MUTE | m_VIDEO_BLACK,
907 		  v_AUDIO_MUTE(1) | v_VIDEO_MUTE(1));
908 
909 	/* Set HDMI Mode */
910 	hdmi_writeb(hdmi, HDMI_HDCP_CTRL,
911 		    v_HDMI_DVI(hdmi->hdmi_data.sink_is_hdmi));
912 
913 	rk618_hdmi_config_video_timing(hdmi, mode);
914 
915 	rk618_hdmi_config_video_csc(hdmi);
916 
917 	if (hdmi->hdmi_data.sink_is_hdmi) {
918 		rk618_hdmi_config_video_avi(hdmi, mode);
919 		rk618_hdmi_config_video_vsi(hdmi, mode);
920 	}
921 
922 	/*
923 	 * When IP controller have configured to an accurate video
924 	 * timing, then the TMDS clock source would be switched to
925 	 * DCLK_LCDC, so we need to init the TMDS rate to mode pixel
926 	 * clock rate, and reconfigure the DDC clock.
927 	 */
928 	hdmi->tmds_rate = mode->clock * 1000;
929 	rk618_hdmi_i2c_init(hdmi);
930 
931 	/* Unmute video and audio output */
932 	hdmi_modb(hdmi, HDMI_AV_MUTE, m_VIDEO_BLACK, v_VIDEO_MUTE(0));
933 	if (hdmi->audio_enable)
934 		hdmi_modb(hdmi, HDMI_AV_MUTE, m_AUDIO_MUTE, v_AUDIO_MUTE(0));
935 
936 	return 0;
937 }
938 
rk618_hdmi_hpd_detect(struct rk618_hdmi * hdmi)939 static bool rk618_hdmi_hpd_detect(struct rk618_hdmi *hdmi)
940 {
941 	return !!(hdmi_readb(hdmi, HDMI_STATUS) & m_HOTPLUG);
942 }
943 
944 static enum drm_connector_status
rk618_hdmi_connector_detect(struct drm_connector * connector,bool force)945 rk618_hdmi_connector_detect(struct drm_connector *connector, bool force)
946 {
947 	struct rk618_hdmi *hdmi = connector_to_hdmi(connector);
948 	bool status;
949 
950 	status = rk618_hdmi_hpd_detect(hdmi);
951 #ifdef CONFIG_SWITCH
952 	switch_set_state(&hdmi->switchdev, status);
953 #endif
954 
955 	return status ? connector_status_connected :
956 			connector_status_disconnected;
957 }
958 
rk618_hdmi_connector_get_modes(struct drm_connector * connector)959 static int rk618_hdmi_connector_get_modes(struct drm_connector *connector)
960 {
961 	struct rk618_hdmi *hdmi = connector_to_hdmi(connector);
962 	struct drm_display_info *info = &connector->display_info;
963 	struct edid *edid = NULL;
964 	int ret = 0;
965 
966 	if (!hdmi->ddc)
967 		return 0;
968 
969 	if (rk618_hdmi_hpd_detect(hdmi))
970 		edid = drm_get_edid(connector, hdmi->ddc);
971 
972 	if (edid) {
973 		hdmi->hdmi_data.sink_is_hdmi = drm_detect_hdmi_monitor(edid);
974 		hdmi->hdmi_data.sink_has_audio = drm_detect_monitor_audio(edid);
975 		drm_connector_update_edid_property(connector, edid);
976 		ret = drm_add_edid_modes(connector, edid);
977 		kfree(edid);
978 	} else {
979 		hdmi->hdmi_data.sink_is_hdmi = true;
980 		hdmi->hdmi_data.sink_has_audio = true;
981 		ret = rockchip_drm_add_modes_noedid(connector);
982 		info->edid_hdmi_dc_modes = 0;
983 		info->hdmi.y420_dc_modes = 0;
984 		info->color_formats = 0;
985 
986 		dev_info(hdmi->dev, "failed to get edid\n");
987 	}
988 
989 	return ret;
990 }
991 
992 static enum drm_mode_status
rk618_hdmi_connector_mode_valid(struct drm_connector * connector,struct drm_display_mode * mode)993 rk618_hdmi_connector_mode_valid(struct drm_connector *connector,
994 				struct drm_display_mode *mode)
995 {
996 	if ((mode->hdisplay == 1920 && mode->vdisplay == 1080) ||
997 	    (mode->hdisplay == 1280 && mode->vdisplay == 720))
998 		return MODE_OK;
999 	else
1000 		return MODE_BAD;
1001 }
1002 
1003 static struct drm_encoder *
rk618_hdmi_connector_best_encoder(struct drm_connector * connector)1004 rk618_hdmi_connector_best_encoder(struct drm_connector *connector)
1005 {
1006 	struct rk618_hdmi *hdmi = connector_to_hdmi(connector);
1007 
1008 	return hdmi->base.encoder;
1009 }
1010 
1011 static int
rk618_hdmi_probe_single_connector_modes(struct drm_connector * connector,uint32_t maxX,uint32_t maxY)1012 rk618_hdmi_probe_single_connector_modes(struct drm_connector *connector,
1013 					uint32_t maxX, uint32_t maxY)
1014 {
1015 	return drm_helper_probe_single_connector_modes(connector, 1920, 1080);
1016 }
1017 
1018 static const struct drm_connector_funcs rk618_hdmi_connector_funcs = {
1019 	.fill_modes = rk618_hdmi_probe_single_connector_modes,
1020 	.detect = rk618_hdmi_connector_detect,
1021 	.destroy = drm_connector_cleanup,
1022 	.reset = drm_atomic_helper_connector_reset,
1023 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1024 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1025 };
1026 
1027 static const struct drm_connector_helper_funcs
1028 rk618_hdmi_connector_helper_funcs = {
1029 	.get_modes = rk618_hdmi_connector_get_modes,
1030 	.mode_valid = rk618_hdmi_connector_mode_valid,
1031 	.best_encoder = rk618_hdmi_connector_best_encoder,
1032 };
1033 
rk618_hdmi_bridge_mode_set(struct drm_bridge * bridge,const struct drm_display_mode * mode,const struct drm_display_mode * adj_mode)1034 static void rk618_hdmi_bridge_mode_set(struct drm_bridge *bridge,
1035 				       const struct drm_display_mode *mode,
1036 				       const struct drm_display_mode *adj_mode)
1037 {
1038 	struct rk618_hdmi *hdmi = bridge_to_hdmi(bridge);
1039 
1040 	/* Store the display mode for plugin/DPMS poweron events */
1041 	memcpy(&hdmi->previous_mode, adj_mode, sizeof(hdmi->previous_mode));
1042 }
1043 
rk618_hdmi_bridge_enable(struct drm_bridge * bridge)1044 static void rk618_hdmi_bridge_enable(struct drm_bridge *bridge)
1045 {
1046 	struct rk618_hdmi *hdmi = bridge_to_hdmi(bridge);
1047 
1048 	clk_prepare_enable(hdmi->clock);
1049 
1050 	if (!rk618_hdmi_hpd_detect(hdmi)) {
1051 		rk618_hdmi_set_pwr_mode(hdmi, LOWER_PWR);
1052 		return;
1053 	}
1054 
1055 	rk618_hdmi_setup(hdmi, &hdmi->previous_mode);
1056 	rk618_hdmi_set_polarity(hdmi, hdmi->hdmi_data.vic);
1057 	rk618_hdmi_set_pwr_mode(hdmi, NORMAL);
1058 }
1059 
rk618_hdmi_bridge_disable(struct drm_bridge * bridge)1060 static void rk618_hdmi_bridge_disable(struct drm_bridge *bridge)
1061 {
1062 	struct rk618_hdmi *hdmi = bridge_to_hdmi(bridge);
1063 
1064 	rk618_hdmi_set_pwr_mode(hdmi, LOWER_PWR);
1065 
1066 	clk_disable_unprepare(hdmi->clock);
1067 }
1068 
rk618_hdmi_bridge_attach(struct drm_bridge * bridge,enum drm_bridge_attach_flags flags)1069 static int rk618_hdmi_bridge_attach(struct drm_bridge *bridge,
1070 				    enum drm_bridge_attach_flags flags)
1071 {
1072 	struct rk618_hdmi *hdmi = bridge_to_hdmi(bridge);
1073 	struct device *dev = hdmi->dev;
1074 	struct drm_connector *connector = &hdmi->connector;
1075 	struct drm_device *drm = bridge->dev;
1076 	struct device_node *endpoint;
1077 	int ret;
1078 
1079 	connector->polled = DRM_CONNECTOR_POLL_HPD;
1080 
1081 	ret = drm_connector_init(drm, connector, &rk618_hdmi_connector_funcs,
1082 				 DRM_MODE_CONNECTOR_HDMIA);
1083 	if (ret) {
1084 		dev_err(hdmi->dev, "Failed to initialize connector with drm\n");
1085 		return ret;
1086 	}
1087 
1088 	drm_connector_helper_add(connector,
1089 				 &rk618_hdmi_connector_helper_funcs);
1090 	drm_connector_attach_encoder(connector, bridge->encoder);
1091 
1092 	hdmi->sub_dev.connector = &hdmi->connector;
1093 	hdmi->sub_dev.of_node = hdmi->dev->of_node;
1094 	rockchip_drm_register_sub_dev(&hdmi->sub_dev);
1095 
1096 	endpoint = of_graph_get_endpoint_by_regs(dev->of_node, 1, -1);
1097 	if (endpoint && of_device_is_available(endpoint)) {
1098 		struct device_node *remote;
1099 
1100 		remote = of_graph_get_remote_port_parent(endpoint);
1101 		of_node_put(endpoint);
1102 		if (!remote || !of_device_is_available(remote))
1103 			return -ENODEV;
1104 
1105 		hdmi->bridge = of_drm_find_bridge(remote);
1106 		of_node_put(remote);
1107 		if (!hdmi->bridge)
1108 			return -EPROBE_DEFER;
1109 
1110 		ret = drm_bridge_attach(bridge->encoder, hdmi->bridge, bridge, 0);
1111 		if (ret) {
1112 			dev_err(dev, "failed to attach bridge\n");
1113 			return ret;
1114 		}
1115 	}
1116 
1117 	return 0;
1118 }
1119 
rk618_hdmi_bridge_detach(struct drm_bridge * bridge)1120 static void rk618_hdmi_bridge_detach(struct drm_bridge *bridge)
1121 {
1122 	struct rk618_hdmi *hdmi = bridge_to_hdmi(bridge);
1123 
1124 	rockchip_drm_unregister_sub_dev(&hdmi->sub_dev);
1125 }
1126 
1127 static const struct drm_bridge_funcs rk618_hdmi_bridge_funcs = {
1128 	.attach = rk618_hdmi_bridge_attach,
1129 	.detach = rk618_hdmi_bridge_detach,
1130 	.mode_set = rk618_hdmi_bridge_mode_set,
1131 	.enable = rk618_hdmi_bridge_enable,
1132 	.disable = rk618_hdmi_bridge_disable,
1133 };
1134 
1135 static int
rk618_hdmi_audio_config_set(struct rk618_hdmi * hdmi,struct audio_info * audio)1136 rk618_hdmi_audio_config_set(struct rk618_hdmi *hdmi, struct audio_info *audio)
1137 {
1138 	int rate, N, channel;
1139 
1140 	if (audio->channels < 3)
1141 		channel = I2S_CHANNEL_1_2;
1142 	else if (audio->channels < 5)
1143 		channel = I2S_CHANNEL_3_4;
1144 	else if (audio->channels < 7)
1145 		channel = I2S_CHANNEL_5_6;
1146 	else
1147 		channel = I2S_CHANNEL_7_8;
1148 
1149 	switch (audio->sample_rate) {
1150 	case 32000:
1151 		rate = AUDIO_32K;
1152 		N = N_32K;
1153 		break;
1154 	case 44100:
1155 		rate = AUDIO_441K;
1156 		N = N_441K;
1157 		break;
1158 	case 48000:
1159 		rate = AUDIO_48K;
1160 		N = N_48K;
1161 		break;
1162 	case 88200:
1163 		rate = AUDIO_882K;
1164 		N = N_882K;
1165 		break;
1166 	case 96000:
1167 		rate = AUDIO_96K;
1168 		N = N_96K;
1169 		break;
1170 	case 176400:
1171 		rate = AUDIO_1764K;
1172 		N = N_1764K;
1173 		break;
1174 	case 192000:
1175 		rate = AUDIO_192K;
1176 		N = N_192K;
1177 		break;
1178 	default:
1179 		dev_err(hdmi->dev, "[%s] not support such sample rate %d\n",
1180 			__func__, audio->sample_rate);
1181 		return -ENOENT;
1182 	}
1183 
1184 	/* set_audio source I2S */
1185 	hdmi_writeb(hdmi, HDMI_AUDIO_CTRL1, 0x01);
1186 	hdmi_writeb(hdmi, AUDIO_SAMPLE_RATE, rate);
1187 	hdmi_writeb(hdmi, AUDIO_I2S_MODE, v_I2S_MODE(I2S_STANDARD) |
1188 		    v_I2S_CHANNEL(channel));
1189 
1190 	hdmi_writeb(hdmi, AUDIO_I2S_MAP, 0x00);
1191 	hdmi_writeb(hdmi, AUDIO_I2S_SWAPS_SPDIF, 0);
1192 
1193 	/* Set N value */
1194 	hdmi_writeb(hdmi, AUDIO_N_H, (N >> 16) & 0x0F);
1195 	hdmi_writeb(hdmi, AUDIO_N_M, (N >> 8) & 0xFF);
1196 	hdmi_writeb(hdmi, AUDIO_N_L, N & 0xFF);
1197 
1198 	/*Set hdmi nlpcm mode to support hdmi bitstream*/
1199 	hdmi_writeb(hdmi, HDMI_AUDIO_CHANNEL_STATUS, v_AUDIO_STATUS_NLPCM(0));
1200 
1201 	return rk618_hdmi_config_audio_aai(hdmi, audio);
1202 }
1203 
rk618_hdmi_audio_hw_params(struct device * dev,void * d,struct hdmi_codec_daifmt * daifmt,struct hdmi_codec_params * params)1204 static int rk618_hdmi_audio_hw_params(struct device *dev, void *d,
1205 				      struct hdmi_codec_daifmt *daifmt,
1206 				      struct hdmi_codec_params *params)
1207 {
1208 	struct rk618_hdmi *hdmi = dev_get_drvdata(dev);
1209 	struct audio_info audio = {
1210 		.sample_width = params->sample_width,
1211 		.sample_rate = params->sample_rate,
1212 		.channels = params->channels,
1213 	};
1214 
1215 	if (!hdmi->hdmi_data.sink_has_audio) {
1216 		dev_err(hdmi->dev, "Sink do not support audio!\n");
1217 		return -ENODEV;
1218 	}
1219 
1220 	if (!hdmi->base.encoder->crtc)
1221 		return -ENODEV;
1222 
1223 	switch (daifmt->fmt) {
1224 	case HDMI_I2S:
1225 		break;
1226 	default:
1227 		dev_err(dev, "%s: Invalid format %d\n", __func__, daifmt->fmt);
1228 		return -EINVAL;
1229 	}
1230 
1231 	return rk618_hdmi_audio_config_set(hdmi, &audio);
1232 }
1233 
rk618_hdmi_audio_shutdown(struct device * dev,void * d)1234 static void rk618_hdmi_audio_shutdown(struct device *dev, void *d)
1235 {
1236 	/* do nothing */
1237 }
1238 
rk618_hdmi_audio_mute_stream(struct device * dev,void * d,bool mute,int direction)1239 static int rk618_hdmi_audio_mute_stream(struct device *dev, void *d, bool mute, int direction)
1240 {
1241 	struct rk618_hdmi *hdmi = dev_get_drvdata(dev);
1242 
1243 	if (!hdmi->hdmi_data.sink_has_audio) {
1244 		dev_err(hdmi->dev, "Sink do not support audio!\n");
1245 		return -ENODEV;
1246 	}
1247 
1248 	hdmi->audio_enable = !mute;
1249 
1250 	if (mute)
1251 		hdmi_modb(hdmi, HDMI_AV_MUTE, m_AUDIO_MUTE | m_AUDIO_PD,
1252 			  v_AUDIO_MUTE(1) | v_AUDIO_PD(1));
1253 	else
1254 		hdmi_modb(hdmi, HDMI_AV_MUTE, m_AUDIO_MUTE | m_AUDIO_PD,
1255 			  v_AUDIO_MUTE(0) | v_AUDIO_PD(0));
1256 
1257 	return 0;
1258 }
1259 
rk618_hdmi_audio_get_eld(struct device * dev,void * d,uint8_t * buf,size_t len)1260 static int rk618_hdmi_audio_get_eld(struct device *dev, void *d,
1261 				    uint8_t *buf, size_t len)
1262 {
1263 	struct rk618_hdmi *hdmi = dev_get_drvdata(dev);
1264 	struct drm_mode_config *config = &hdmi->base.dev->mode_config;
1265 	struct drm_connector *connector;
1266 	int ret = -ENODEV;
1267 
1268 	mutex_lock(&config->mutex);
1269 	list_for_each_entry(connector, &config->connector_list, head) {
1270 		if (hdmi->base.encoder == connector->encoder) {
1271 			memcpy(buf, connector->eld,
1272 			       min(sizeof(connector->eld), len));
1273 			ret = 0;
1274 		}
1275 	}
1276 	mutex_unlock(&config->mutex);
1277 
1278 	return ret;
1279 }
1280 
1281 static const struct hdmi_codec_ops audio_codec_ops = {
1282 	.hw_params = rk618_hdmi_audio_hw_params,
1283 	.audio_shutdown = rk618_hdmi_audio_shutdown,
1284 	.mute_stream = rk618_hdmi_audio_mute_stream,
1285 	.get_eld = rk618_hdmi_audio_get_eld,
1286 };
1287 
rk618_hdmi_audio_codec_init(struct rk618_hdmi * hdmi,struct device * dev)1288 static int rk618_hdmi_audio_codec_init(struct rk618_hdmi *hdmi,
1289 				       struct device *dev)
1290 {
1291 	struct hdmi_codec_pdata codec_data = {
1292 		.i2s = 1,
1293 		.ops = &audio_codec_ops,
1294 		.max_i2s_channels = 8,
1295 	};
1296 
1297 	hdmi->audio_enable = false;
1298 	hdmi->audio_pdev = platform_device_register_data(dev,
1299 				HDMI_CODEC_DRV_NAME, PLATFORM_DEVID_NONE,
1300 				&codec_data, sizeof(codec_data));
1301 
1302 	return PTR_ERR_OR_ZERO(hdmi->audio_pdev);
1303 }
1304 
rk618_hdmi_irq(int irq,void * dev_id)1305 static irqreturn_t rk618_hdmi_irq(int irq, void *dev_id)
1306 {
1307 	struct rk618_hdmi *hdmi = dev_id;
1308 
1309 	hdmi_modb(hdmi, HDMI_INTERRUPT_STATUS1, m_INT_HOTPLUG_RK618,
1310 		  m_INT_HOTPLUG_RK618);
1311 
1312 	if (hdmi->connector.dev)
1313 		drm_helper_hpd_irq_event(hdmi->connector.dev);
1314 
1315 	return IRQ_HANDLED;
1316 }
1317 
rk618_hdmi_i2c_read(struct rk618_hdmi * hdmi,struct i2c_msg * msgs)1318 static int rk618_hdmi_i2c_read(struct rk618_hdmi *hdmi, struct i2c_msg *msgs)
1319 {
1320 	int length = msgs->len;
1321 	u8 *buf = msgs->buf;
1322 	int i;
1323 	u32 c;
1324 
1325 	for (i = 0; i < 10; i++) {
1326 		msleep(20);
1327 		c = hdmi_readb(hdmi, HDMI_INTERRUPT_STATUS1);
1328 
1329 		if (c & m_INT_EDID_READY)
1330 			break;
1331 	}
1332 	if ((c & m_INT_EDID_READY) == 0)
1333 		return -EAGAIN;
1334 
1335 	while (length--)
1336 		*buf++ = hdmi_readb(hdmi, HDMI_EDID_FIFO_ADDR);
1337 
1338 	return 0;
1339 }
1340 
rk618_hdmi_i2c_write(struct rk618_hdmi * hdmi,struct i2c_msg * msgs)1341 static int rk618_hdmi_i2c_write(struct rk618_hdmi *hdmi, struct i2c_msg *msgs)
1342 {
1343 	/*
1344 	 * The DDC module only support read EDID message, so
1345 	 * we assume that each word write to this i2c adapter
1346 	 * should be the offset of EDID word address.
1347 	 */
1348 	if ((msgs->len != 1) ||
1349 	    ((msgs->addr != DDC_ADDR) && (msgs->addr != DDC_SEGMENT_ADDR)))
1350 		return -EINVAL;
1351 
1352 	if (msgs->addr == DDC_ADDR)
1353 		hdmi->i2c->ddc_addr = msgs->buf[0];
1354 	if (msgs->addr == DDC_SEGMENT_ADDR) {
1355 		hdmi->i2c->segment_addr = msgs->buf[0];
1356 		return 0;
1357 	}
1358 
1359 	/* Set edid fifo first addr */
1360 	hdmi_writeb(hdmi, HDMI_EDID_FIFO_OFFSET, 0x00);
1361 
1362 	/* Set edid word address 0x00/0x80 */
1363 	hdmi_writeb(hdmi, HDMI_EDID_WORD_ADDR, hdmi->i2c->ddc_addr);
1364 
1365 	/* Set edid segment pointer */
1366 	hdmi_writeb(hdmi, HDMI_EDID_SEGMENT_POINTER, hdmi->i2c->segment_addr);
1367 
1368 	return 0;
1369 }
1370 
rk618_hdmi_i2c_xfer(struct i2c_adapter * adap,struct i2c_msg * msgs,int num)1371 static int rk618_hdmi_i2c_xfer(struct i2c_adapter *adap,
1372 			       struct i2c_msg *msgs, int num)
1373 {
1374 	struct rk618_hdmi *hdmi = i2c_get_adapdata(adap);
1375 	struct rk618_hdmi_i2c *i2c = hdmi->i2c;
1376 	int i, ret = 0;
1377 
1378 	mutex_lock(&i2c->lock);
1379 
1380 	hdmi->i2c->ddc_addr = 0;
1381 	hdmi->i2c->segment_addr = 0;
1382 
1383 	/* Clear the EDID interrupt flag and unmute the interrupt */
1384 	hdmi_writeb(hdmi, HDMI_INTERRUPT_MASK1, m_INT_EDID_READY);
1385 	hdmi_writeb(hdmi, HDMI_INTERRUPT_STATUS1, m_INT_EDID_READY);
1386 
1387 	for (i = 0; i < num; i++) {
1388 		dev_dbg(hdmi->dev, "xfer: num: %d/%d, len: %d, flags: %#x\n",
1389 			i + 1, num, msgs[i].len, msgs[i].flags);
1390 
1391 		if (msgs[i].flags & I2C_M_RD)
1392 			ret = rk618_hdmi_i2c_read(hdmi, &msgs[i]);
1393 		else
1394 			ret = rk618_hdmi_i2c_write(hdmi, &msgs[i]);
1395 
1396 		if (ret < 0)
1397 			break;
1398 	}
1399 
1400 	if (!ret)
1401 		ret = num;
1402 
1403 	/* Mute HDMI EDID interrupt */
1404 	hdmi_writeb(hdmi, HDMI_INTERRUPT_MASK1, 0);
1405 	hdmi_modb(hdmi, HDMI_INTERRUPT_MASK1, m_INT_HOTPLUG_RK618,
1406 		  m_INT_HOTPLUG_RK618);
1407 
1408 	mutex_unlock(&i2c->lock);
1409 
1410 	return ret;
1411 }
1412 
rk618_hdmi_i2c_func(struct i2c_adapter * adapter)1413 static u32 rk618_hdmi_i2c_func(struct i2c_adapter *adapter)
1414 {
1415 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
1416 }
1417 
1418 static const struct i2c_algorithm rk618_hdmi_algorithm = {
1419 	.master_xfer	= rk618_hdmi_i2c_xfer,
1420 	.functionality	= rk618_hdmi_i2c_func,
1421 };
1422 
rk618_hdmi_i2c_adapter(struct rk618_hdmi * hdmi)1423 static struct i2c_adapter *rk618_hdmi_i2c_adapter(struct rk618_hdmi *hdmi)
1424 {
1425 	struct i2c_adapter *adap;
1426 	struct rk618_hdmi_i2c *i2c;
1427 	int ret;
1428 
1429 	i2c = devm_kzalloc(hdmi->dev, sizeof(*i2c), GFP_KERNEL);
1430 	if (!i2c)
1431 		return ERR_PTR(-ENOMEM);
1432 
1433 	mutex_init(&i2c->lock);
1434 
1435 	adap = &i2c->adap;
1436 	adap->class = I2C_CLASS_DDC;
1437 	adap->owner = THIS_MODULE;
1438 	adap->dev.parent = hdmi->dev;
1439 	adap->dev.of_node = hdmi->dev->of_node;
1440 	adap->algo = &rk618_hdmi_algorithm;
1441 	strlcpy(adap->name, "RK618 HDMI", sizeof(adap->name));
1442 	i2c_set_adapdata(adap, hdmi);
1443 
1444 	ret = i2c_add_adapter(adap);
1445 	if (ret) {
1446 		dev_warn(hdmi->dev, "cannot add %s I2C adapter\n", adap->name);
1447 		devm_kfree(hdmi->dev, i2c);
1448 		return ERR_PTR(ret);
1449 	}
1450 
1451 	hdmi->i2c = i2c;
1452 
1453 	dev_info(hdmi->dev, "registered %s I2C bus driver\n", adap->name);
1454 
1455 	return adap;
1456 }
1457 
1458 static const struct regmap_range rk618_hdmi_volatile_reg_ranges[] = {
1459 	regmap_reg_range(0x0400, 0x07b4),
1460 };
1461 
1462 static const struct regmap_access_table rk618_hdmi_volatile_regs = {
1463 	.yes_ranges = rk618_hdmi_volatile_reg_ranges,
1464 	.n_yes_ranges = ARRAY_SIZE(rk618_hdmi_volatile_reg_ranges),
1465 };
1466 
rk618_is_read_enable_reg(struct device * dev,unsigned int reg)1467 static bool rk618_is_read_enable_reg(struct device *dev, unsigned int reg)
1468 {
1469 	if (reg >= RK618_HDMI_BASE &&
1470 	    reg <= (HDMI_CEC_LOGICADDR * RK618_HDMI_BASE))
1471 		return true;
1472 
1473 	return false;
1474 }
1475 
1476 static const struct regmap_config rk618_hdmi_regmap_config = {
1477 	.name = "hdmi",
1478 	.reg_bits = 16,
1479 	.val_bits = 32,
1480 	.reg_stride = 4,
1481 	.max_register = 0x07b4,
1482 	.cache_type = REGCACHE_RBTREE,
1483 	.reg_format_endian = REGMAP_ENDIAN_NATIVE,
1484 	.val_format_endian = REGMAP_ENDIAN_NATIVE,
1485 	.readable_reg = rk618_is_read_enable_reg,
1486 	.volatile_table = &rk618_hdmi_volatile_regs,
1487 };
1488 
rk618_hdmi_probe(struct platform_device * pdev)1489 static int rk618_hdmi_probe(struct platform_device *pdev)
1490 {
1491 	struct rk618 *rk618 = dev_get_drvdata(pdev->dev.parent);
1492 	struct device *dev = &pdev->dev;
1493 	struct rk618_hdmi *hdmi;
1494 	int irq;
1495 	int ret;
1496 
1497 	if (!of_device_is_available(dev->of_node))
1498 		return -ENODEV;
1499 
1500 	hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
1501 	if (!hdmi)
1502 		return -ENOMEM;
1503 
1504 	hdmi->dev = dev;
1505 	hdmi->parent = rk618;
1506 	platform_set_drvdata(pdev, hdmi);
1507 
1508 	irq = platform_get_irq(pdev, 0);
1509 	if (irq < 0)
1510 		return irq;
1511 
1512 	hdmi->regmap = devm_regmap_init_i2c(rk618->client,
1513 					    &rk618_hdmi_regmap_config);
1514 	if (IS_ERR(hdmi->regmap)) {
1515 		ret = PTR_ERR(hdmi->regmap);
1516 		dev_err(dev, "failed to allocate register map: %d\n", ret);
1517 		return PTR_ERR(hdmi->regmap);
1518 	}
1519 
1520 	hdmi->clock = devm_clk_get(dev, "hdmi");
1521 	if (IS_ERR(hdmi->clock)) {
1522 		dev_err(dev, "Unable to get HDMI clock\n");
1523 		return PTR_ERR(hdmi->clock);
1524 	}
1525 
1526 	rk618_hdmi_pol_init(hdmi, 0);
1527 	rk618_hdmi_reset(hdmi);
1528 
1529 	hdmi->ddc = rk618_hdmi_i2c_adapter(hdmi);
1530 	if (IS_ERR(hdmi->ddc)) {
1531 		ret = PTR_ERR(hdmi->ddc);
1532 		hdmi->ddc = NULL;
1533 		return ret;
1534 	}
1535 
1536 	/*
1537 	 * When IP controller haven't configured to an accurate video
1538 	 * timing, then the TMDS clock source would be switched to
1539 	 * PCLK_HDMI, so we need to init the TMDS rate to PCLK rate,
1540 	 * and reconfigure the DDC clock.
1541 	 */
1542 	hdmi->tmds_rate = clk_get_rate(rk618->clkin);
1543 
1544 	rk618_hdmi_i2c_init(hdmi);
1545 	rk618_hdmi_audio_codec_init(hdmi, dev);
1546 
1547 	/* Unmute hotplug interrupt */
1548 	hdmi_modb(hdmi, HDMI_STATUS, m_MASK_INT_HOTPLUG, v_MASK_INT_HOTPLUG(1));
1549 	hdmi_modb(hdmi, HDMI_INTERRUPT_STATUS1, m_INT_HOTPLUG_RK618,
1550 		  m_INT_HOTPLUG_RK618);
1551 
1552 	ret = devm_request_threaded_irq(dev, irq, NULL,
1553 					rk618_hdmi_irq,
1554 					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1555 					dev_name(dev), hdmi);
1556 	if (ret) {
1557 		dev_err(dev, "failed to request hdmi irq: %d\n", ret);
1558 		return ret;
1559 	}
1560 
1561 	hdmi->base.funcs = &rk618_hdmi_bridge_funcs;
1562 	hdmi->base.of_node = dev->of_node;
1563 	drm_bridge_add(&hdmi->base);
1564 
1565 #ifdef CONFIG_SWITCH
1566 	hdmi->switchdev.name = "hdmi";
1567 	switch_dev_register(&hdmi->switchdev);
1568 #endif
1569 
1570 	return 0;
1571 }
1572 
rk618_hdmi_remove(struct platform_device * pdev)1573 static int rk618_hdmi_remove(struct platform_device *pdev)
1574 {
1575 	struct rk618_hdmi *hdmi = platform_get_drvdata(pdev);
1576 
1577 	drm_bridge_remove(&hdmi->base);
1578 	i2c_put_adapter(hdmi->ddc);
1579 #ifdef CONFIG_SWITCH
1580 	switch_dev_unregister(&hdmi->switchdev);
1581 #endif
1582 
1583 	return 0;
1584 }
1585 
1586 static const struct of_device_id rk618_hdmi_dt_ids[] = {
1587 	{ .compatible = "rockchip,rk618-hdmi", },
1588 	{},
1589 };
1590 MODULE_DEVICE_TABLE(of, rk618_hdmi_dt_ids);
1591 
1592 static struct platform_driver rk618_hdmi_driver = {
1593 	.probe  = rk618_hdmi_probe,
1594 	.remove = rk618_hdmi_remove,
1595 	.driver = {
1596 		.name = "rk618-hdmi",
1597 		.of_match_table = rk618_hdmi_dt_ids,
1598 	},
1599 };
1600 
1601 module_platform_driver(rk618_hdmi_driver);
1602 
1603 MODULE_AUTHOR("Chen Shunqing <csq@rock-chips.com>");
1604 MODULE_AUTHOR("Zheng Yang <zhengyang@rock-chips.com>");
1605 MODULE_AUTHOR("Yakir Yang <ykk@rock-chips.com>");
1606 MODULE_DESCRIPTION("Rockchip RK618 HDMI driver");
1607 MODULE_LICENSE("GPL v2");
1608