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