xref: /linux/drivers/gpu/drm/bridge/ite-it6505.c (revision 2c1ed907520c50326b8f604907a8478b27881a2e)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /*
3  * Copyright (c) 2020, The Linux Foundation. All rights reserved.
4  */
5 #include <linux/bits.h>
6 #include <linux/debugfs.h>
7 #include <linux/delay.h>
8 #include <linux/device.h>
9 #include <linux/err.h>
10 #include <linux/extcon.h>
11 #include <linux/fs.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/i2c.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/regmap.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/types.h>
21 #include <linux/wait.h>
22 #include <linux/bitfield.h>
23 
24 #include <crypto/hash.h>
25 
26 #include <drm/display/drm_dp_helper.h>
27 #include <drm/display/drm_hdcp_helper.h>
28 #include <drm/drm_atomic_helper.h>
29 #include <drm/drm_bridge.h>
30 #include <drm/drm_crtc.h>
31 #include <drm/drm_edid.h>
32 #include <drm/drm_print.h>
33 #include <drm/drm_probe_helper.h>
34 
35 #include <sound/hdmi-codec.h>
36 
37 #define REG_IC_VER 0x04
38 
39 #define REG_RESET_CTRL 0x05
40 #define VIDEO_RESET BIT(0)
41 #define AUDIO_RESET BIT(1)
42 #define ALL_LOGIC_RESET BIT(2)
43 #define AUX_RESET BIT(3)
44 #define HDCP_RESET BIT(4)
45 
46 #define INT_STATUS_01 0x06
47 #define INT_MASK_01 0x09
48 #define INT_HPD_CHANGE 0
49 #define INT_RECEIVE_HPD_IRQ 1
50 #define INT_SCDT_CHANGE 2
51 #define INT_HDCP_FAIL 3
52 #define INT_HDCP_DONE 4
53 #define BIT_OFFSET(x) (((x) - INT_STATUS_01) * BITS_PER_BYTE)
54 #define BIT_INT_HPD INT_HPD_CHANGE
55 #define BIT_INT_HPD_IRQ INT_RECEIVE_HPD_IRQ
56 #define BIT_INT_SCDT INT_SCDT_CHANGE
57 #define BIT_INT_HDCP_FAIL INT_HDCP_FAIL
58 #define BIT_INT_HDCP_DONE INT_HDCP_DONE
59 
60 #define INT_STATUS_02 0x07
61 #define INT_MASK_02 0x0A
62 #define INT_AUX_CMD_FAIL 0
63 #define INT_HDCP_KSV_CHECK 1
64 #define INT_AUDIO_FIFO_ERROR 2
65 #define BIT_INT_AUX_CMD_FAIL (BIT_OFFSET(0x07) + INT_AUX_CMD_FAIL)
66 #define BIT_INT_HDCP_KSV_CHECK (BIT_OFFSET(0x07) + INT_HDCP_KSV_CHECK)
67 #define BIT_INT_AUDIO_FIFO_ERROR (BIT_OFFSET(0x07) + INT_AUDIO_FIFO_ERROR)
68 
69 #define INT_STATUS_03 0x08
70 #define INT_MASK_03 0x0B
71 #define INT_LINK_TRAIN_FAIL 4
72 #define INT_VID_FIFO_ERROR 5
73 #define INT_IO_LATCH_FIFO_OVERFLOW 7
74 #define BIT_INT_LINK_TRAIN_FAIL (BIT_OFFSET(0x08) + INT_LINK_TRAIN_FAIL)
75 #define BIT_INT_VID_FIFO_ERROR (BIT_OFFSET(0x08) + INT_VID_FIFO_ERROR)
76 #define BIT_INT_IO_FIFO_OVERFLOW (BIT_OFFSET(0x08) + INT_IO_LATCH_FIFO_OVERFLOW)
77 
78 #define REG_SYSTEM_STS 0x0D
79 #define INT_STS BIT(0)
80 #define HPD_STS BIT(1)
81 #define VIDEO_STB BIT(2)
82 
83 #define REG_LINK_TRAIN_STS 0x0E
84 #define LINK_STATE_CR BIT(2)
85 #define LINK_STATE_EQ BIT(3)
86 #define LINK_STATE_NORP BIT(4)
87 
88 #define REG_BANK_SEL 0x0F
89 #define REG_CLK_CTRL0 0x10
90 #define M_PCLK_DELAY 0x03
91 
92 #define REG_AUX_OPT 0x11
93 #define AUX_AUTO_RST BIT(0)
94 #define AUX_FIX_FREQ BIT(3)
95 
96 #define REG_DATA_CTRL0 0x12
97 #define VIDEO_LATCH_EDGE BIT(4)
98 #define ENABLE_PCLK_COUNTER BIT(7)
99 
100 #define REG_PCLK_COUNTER_VALUE 0x13
101 
102 #define REG_501_FIFO_CTRL 0x15
103 #define RST_501_FIFO BIT(1)
104 
105 #define REG_TRAIN_CTRL0 0x16
106 #define FORCE_LBR BIT(0)
107 #define LANE_COUNT_MASK 0x06
108 #define LANE_SWAP BIT(3)
109 #define SPREAD_AMP_5 BIT(4)
110 #define FORCE_CR_DONE BIT(5)
111 #define FORCE_EQ_DONE BIT(6)
112 
113 #define REG_TRAIN_CTRL1 0x17
114 #define AUTO_TRAIN BIT(0)
115 #define MANUAL_TRAIN BIT(1)
116 #define FORCE_RETRAIN BIT(2)
117 
118 #define REG_AUX_CTRL 0x23
119 #define CLR_EDID_FIFO BIT(0)
120 #define AUX_USER_MODE BIT(1)
121 #define AUX_NO_SEGMENT_WR BIT(6)
122 #define AUX_EN_FIFO_READ BIT(7)
123 
124 #define REG_AUX_ADR_0_7 0x24
125 #define REG_AUX_ADR_8_15 0x25
126 #define REG_AUX_ADR_16_19 0x26
127 #define REG_AUX_OUT_DATA0 0x27
128 
129 #define REG_AUX_CMD_REQ 0x2B
130 #define M_AUX_REQ_CMD 0x0F
131 #define AUX_BUSY BIT(5)
132 
133 #define REG_AUX_DATA_0_7 0x2C
134 #define REG_AUX_DATA_8_15 0x2D
135 #define REG_AUX_DATA_16_23 0x2E
136 #define REG_AUX_DATA_24_31 0x2F
137 
138 #define REG_AUX_DATA_FIFO 0x2F
139 
140 #define REG_AUX_ERROR_STS 0x9F
141 #define M_AUX_REQ_FAIL 0x03
142 
143 #define REG_HDCP_CTRL1 0x38
144 #define HDCP_CP_ENABLE BIT(0)
145 
146 #define REG_HDCP_TRIGGER 0x39
147 #define HDCP_TRIGGER_START  BIT(0)
148 #define HDCP_TRIGGER_CPIRQ  BIT(1)
149 #define HDCP_TRIGGER_KSV_DONE  BIT(4)
150 #define HDCP_TRIGGER_KSV_FAIL BIT(5)
151 
152 #define REG_HDCP_CTRL2 0x3A
153 #define HDCP_AN_SEL BIT(0)
154 #define HDCP_AN_GEN BIT(1)
155 #define HDCP_HW_HPDIRQ_ACT BIT(2)
156 #define HDCP_EN_M0_READ BIT(5)
157 
158 #define REG_M0_0_7 0x4C
159 #define REG_AN_0_7 0x4C
160 #define REG_SP_CTRL0 0x58
161 #define REG_IP_CTRL1 0x59
162 #define REG_IP_CTRL2 0x5A
163 
164 #define REG_LINK_DRV 0x5C
165 #define DRV_HS BIT(1)
166 
167 #define REG_DRV_LN_DATA_SEL 0x5D
168 
169 #define REG_AUX 0x5E
170 
171 #define REG_VID_BUS_CTRL0 0x60
172 #define IN_DDR BIT(2)
173 #define DDR_CD (0x01 << 6)
174 
175 #define REG_VID_BUS_CTRL1 0x61
176 #define TX_FIFO_RESET BIT(1)
177 
178 #define REG_INPUT_CTRL 0xA0
179 #define INPUT_HSYNC_POL BIT(0)
180 #define INPUT_VSYNC_POL BIT(2)
181 #define INPUT_INTERLACED BIT(4)
182 
183 #define REG_INPUT_HTOTAL 0xA1
184 #define REG_INPUT_HACTIVE_START 0xA3
185 #define REG_INPUT_HACTIVE_WIDTH 0xA5
186 #define REG_INPUT_HFRONT_PORCH 0xA7
187 #define REG_INPUT_HSYNC_WIDTH 0xA9
188 #define REG_INPUT_VTOTAL 0xAB
189 #define REG_INPUT_VACTIVE_START 0xAD
190 #define REG_INPUT_VACTIVE_WIDTH 0xAF
191 #define REG_INPUT_VFRONT_PORCH 0xB1
192 #define REG_INPUT_VSYNC_WIDTH 0xB3
193 
194 #define REG_AUDIO_SRC_CTRL 0xB8
195 #define M_AUDIO_I2S_EN 0x0F
196 #define EN_I2S0 BIT(0)
197 #define EN_I2S1 BIT(1)
198 #define EN_I2S2 BIT(2)
199 #define EN_I2S3 BIT(3)
200 #define AUDIO_FIFO_RESET BIT(7)
201 
202 #define REG_AUDIO_FMT 0xB9
203 #define REG_AUDIO_FIFO_SEL 0xBA
204 
205 #define REG_AUDIO_CTRL0 0xBB
206 #define AUDIO_FULL_PKT BIT(4)
207 #define AUDIO_16B_BOUND BIT(5)
208 
209 #define REG_AUDIO_CTRL1 0xBC
210 #define REG_AUDIO_INPUT_FREQ 0xBE
211 
212 #define REG_IEC958_STS0 0xBF
213 #define REG_IEC958_STS1 0xC0
214 #define REG_IEC958_STS2 0xC1
215 #define REG_IEC958_STS3 0xC2
216 #define REG_IEC958_STS4 0xC3
217 
218 #define REG_HPD_IRQ_TIME 0xC9
219 #define REG_AUX_DEBUG_MODE 0xCA
220 #define REG_AUX_OPT2 0xCB
221 #define REG_HDCP_OPT 0xCE
222 #define REG_USER_DRV_PRE 0xCF
223 
224 #define REG_DATA_MUTE_CTRL 0xD3
225 #define ENABLE_ENHANCED_FRAME BIT(0)
226 #define ENABLE_AUTO_VIDEO_FIFO_RESET BIT(1)
227 #define EN_VID_MUTE BIT(4)
228 #define EN_AUD_MUTE BIT(5)
229 
230 #define REG_TIME_STMP_CTRL 0xD4
231 #define EN_ENHANCE_VID_STMP BIT(0)
232 #define EN_ENHANCE_AUD_STMP BIT(2)
233 #define M_STAMP_STEP 0x30
234 #define EN_SSC_GAT BIT(6)
235 
236 #define REG_INFOFRAME_CTRL 0xE8
237 #define EN_AVI_PKT BIT(0)
238 #define EN_AUD_PKT BIT(1)
239 #define EN_MPG_PKT BIT(2)
240 #define EN_GEN_PKT BIT(3)
241 #define EN_VID_TIME_STMP BIT(4)
242 #define EN_AUD_TIME_STMP BIT(5)
243 #define EN_VID_CTRL_PKT (EN_AVI_PKT | EN_VID_TIME_STMP)
244 #define EN_AUD_CTRL_PKT (EN_AUD_PKT | EN_AUD_TIME_STMP)
245 
246 #define REG_AUDIO_N_0_7 0xDE
247 #define REG_AUDIO_N_8_15 0xDF
248 #define REG_AUDIO_N_16_23 0xE0
249 
250 #define REG_AVI_INFO_DB1 0xE9
251 #define REG_AVI_INFO_DB2 0xEA
252 #define REG_AVI_INFO_DB3 0xEB
253 #define REG_AVI_INFO_DB4 0xEC
254 #define REG_AVI_INFO_DB5 0xED
255 #define REG_AVI_INFO_SUM 0xF6
256 
257 #define REG_AUD_INFOFRAM_DB1 0xF7
258 #define REG_AUD_INFOFRAM_DB2 0xF8
259 #define REG_AUD_INFOFRAM_DB3 0xF9
260 #define REG_AUD_INFOFRAM_DB4 0xFA
261 #define REG_AUD_INFOFRAM_SUM 0xFB
262 
263 /* the following six registers are in bank1 */
264 #define REG_DRV_0_DB_800_MV 0x17E
265 #define REG_PRE_0_DB_800_MV 0x17F
266 #define REG_PRE_3P5_DB_800_MV 0x181
267 #define REG_SSC_CTRL0 0x188
268 #define REG_SSC_CTRL1 0x189
269 #define REG_SSC_CTRL2 0x18A
270 
271 #define REG_AUX_USER_CTRL 0x190
272 #define EN_USER_AUX BIT(0)
273 #define USER_AUX_DONE BIT(1)
274 #define AUX_EVENT BIT(4)
275 
276 #define REG_AUX_USER_DATA_REC 0x191
277 #define M_AUX_IN_REC   0xF0
278 #define M_AUX_OUT_REC  0x0F
279 
280 #define REG_AUX_USER_REPLY 0x19A
281 #define REG_AUX_USER_RXB(n) (n + 0x19B)
282 
283 #define RBR DP_LINK_BW_1_62
284 #define HBR DP_LINK_BW_2_7
285 #define HBR2 DP_LINK_BW_5_4
286 #define HBR3 DP_LINK_BW_8_1
287 
288 #define DPCD_V_1_1 0x11
289 #define MISC_VERB 0xF0
290 #define MISC_VERC 0x70
291 #define I2S_INPUT_FORMAT_STANDARD 0
292 #define I2S_INPUT_FORMAT_32BIT 1
293 #define I2S_INPUT_LEFT_JUSTIFIED 0
294 #define I2S_INPUT_RIGHT_JUSTIFIED 1
295 #define I2S_DATA_1T_DELAY 0
296 #define I2S_DATA_NO_DELAY 1
297 #define I2S_WS_LEFT_CHANNEL 0
298 #define I2S_WS_RIGHT_CHANNEL 1
299 #define I2S_DATA_MSB_FIRST 0
300 #define I2S_DATA_LSB_FIRST 1
301 #define WORD_LENGTH_16BIT 0
302 #define WORD_LENGTH_18BIT 1
303 #define WORD_LENGTH_20BIT 2
304 #define WORD_LENGTH_24BIT 3
305 #define DEBUGFS_DIR_NAME "it6505-debugfs"
306 #define READ_BUFFER_SIZE 400
307 
308 /* Vendor option */
309 #define HDCP_DESIRED 1
310 #define MAX_LANE_COUNT 4
311 #define MAX_LINK_RATE HBR
312 #define AUTO_TRAIN_RETRY 3
313 #define MAX_HDCP_DOWN_STREAM_COUNT 127
314 #define MAX_CR_LEVEL 0x03
315 #define MAX_EQ_LEVEL 0x03
316 #define AUX_WAIT_TIMEOUT_MS 15
317 #define AUX_FIFO_MAX_SIZE 16
318 #define AUX_I2C_MAX_SIZE 4
319 #define AUX_I2C_DEFER_RETRY 4
320 #define PIXEL_CLK_DELAY 1
321 #define PIXEL_CLK_INVERSE 0
322 #define ADJUST_PHASE_THRESHOLD 80000
323 #define DPI_PIXEL_CLK_MAX 95000
324 #define HDCP_SHA1_FIFO_LEN (MAX_HDCP_DOWN_STREAM_COUNT * 5 + 10)
325 #define DEFAULT_PWR_ON 0
326 #define DEFAULT_DRV_HOLD 0
327 
328 #define AUDIO_SELECT I2S
329 #define AUDIO_TYPE LPCM
330 #define AUDIO_SAMPLE_RATE SAMPLE_RATE_48K
331 #define AUDIO_CHANNEL_COUNT 2
332 #define I2S_INPUT_FORMAT I2S_INPUT_FORMAT_32BIT
333 #define I2S_JUSTIFIED I2S_INPUT_LEFT_JUSTIFIED
334 #define I2S_DATA_DELAY I2S_DATA_1T_DELAY
335 #define I2S_WS_CHANNEL I2S_WS_LEFT_CHANNEL
336 #define I2S_DATA_SEQUENCE I2S_DATA_MSB_FIRST
337 #define AUDIO_WORD_LENGTH WORD_LENGTH_24BIT
338 
339 enum aux_cmd_type {
340 	CMD_AUX_NATIVE_READ = 0x0,
341 	CMD_AUX_NATIVE_WRITE = 0x5,
342 	CMD_AUX_GI2C_ADR = 0x08,
343 	CMD_AUX_GI2C_READ = 0x09,
344 	CMD_AUX_GI2C_WRITE = 0x0A,
345 	CMD_AUX_I2C_EDID_READ = 0xB,
346 	CMD_AUX_I2C_READ = 0x0D,
347 	CMD_AUX_I2C_WRITE = 0x0C,
348 
349 	/* KSV read with AUX FIFO extend from CMD_AUX_NATIVE_READ*/
350 	CMD_AUX_GET_KSV_LIST = 0x10,
351 };
352 
353 enum aux_cmd_reply {
354 	REPLY_ACK,
355 	REPLY_NACK,
356 	REPLY_DEFER,
357 };
358 
359 enum link_train_status {
360 	LINK_IDLE,
361 	LINK_BUSY,
362 	LINK_OK,
363 };
364 
365 enum hdcp_state {
366 	HDCP_AUTH_IDLE,
367 	HDCP_AUTH_GOING,
368 	HDCP_AUTH_DONE,
369 };
370 
371 struct it6505_platform_data {
372 	struct regulator *pwr18;
373 	struct regulator *ovdd;
374 	struct gpio_desc *gpiod_reset;
375 };
376 
377 enum it6505_audio_select {
378 	I2S = 0,
379 	SPDIF,
380 };
381 
382 enum it6505_audio_sample_rate {
383 	SAMPLE_RATE_24K = 0x6,
384 	SAMPLE_RATE_32K = 0x3,
385 	SAMPLE_RATE_48K = 0x2,
386 	SAMPLE_RATE_96K = 0xA,
387 	SAMPLE_RATE_192K = 0xE,
388 	SAMPLE_RATE_44_1K = 0x0,
389 	SAMPLE_RATE_88_2K = 0x8,
390 	SAMPLE_RATE_176_4K = 0xC,
391 };
392 
393 enum it6505_audio_type {
394 	LPCM = 0,
395 	NLPCM,
396 	DSS,
397 };
398 
399 struct it6505_audio_data {
400 	enum it6505_audio_select select;
401 	enum it6505_audio_sample_rate sample_rate;
402 	enum it6505_audio_type type;
403 	u8 word_length;
404 	u8 channel_count;
405 	u8 i2s_input_format;
406 	u8 i2s_justified;
407 	u8 i2s_data_delay;
408 	u8 i2s_ws_channel;
409 	u8 i2s_data_sequence;
410 };
411 
412 struct it6505_audio_sample_rate_map {
413 	enum it6505_audio_sample_rate rate;
414 	int sample_rate_value;
415 };
416 
417 struct it6505_drm_dp_link {
418 	unsigned char revision;
419 	unsigned int rate;
420 	unsigned int num_lanes;
421 	unsigned long capabilities;
422 };
423 
424 struct debugfs_entries {
425 	char *name;
426 	const struct file_operations *fops;
427 };
428 
429 struct it6505 {
430 	struct drm_dp_aux aux;
431 	struct drm_bridge bridge;
432 	struct device *dev;
433 	struct it6505_drm_dp_link link;
434 	struct it6505_platform_data pdata;
435 	/*
436 	 * Mutex protects extcon and interrupt functions from interfering
437 	 * each other.
438 	 */
439 	struct mutex extcon_lock;
440 	struct mutex mode_lock; /* used to bridge_detect */
441 	struct mutex aux_lock; /* used to aux data transfers */
442 	struct regmap *regmap;
443 	struct drm_display_mode source_output_mode;
444 	struct drm_display_mode video_info;
445 	struct notifier_block event_nb;
446 	struct extcon_dev *extcon;
447 	struct work_struct extcon_wq;
448 	int extcon_state;
449 	enum drm_connector_status connector_status;
450 	enum link_train_status link_state;
451 	struct work_struct link_works;
452 	u8 dpcd[DP_RECEIVER_CAP_SIZE];
453 	u8 lane_count;
454 	u8 link_rate_bw_code;
455 	u8 sink_count;
456 	bool step_train;
457 	bool branch_device;
458 	bool enable_ssc;
459 	bool lane_swap_disabled;
460 	bool lane_swap;
461 	bool powered;
462 	bool hpd_state;
463 	u32 afe_setting;
464 	u32 max_dpi_pixel_clock;
465 	u32 max_lane_count;
466 	enum hdcp_state hdcp_status;
467 	struct delayed_work hdcp_work;
468 	struct work_struct hdcp_wait_ksv_list;
469 	struct completion extcon_completion;
470 	u8 auto_train_retry;
471 	bool hdcp_desired;
472 	bool is_repeater;
473 	u8 hdcp_down_stream_count;
474 	u8 bksvs[DRM_HDCP_KSV_LEN];
475 	u8 sha1_input[HDCP_SHA1_FIFO_LEN];
476 	bool enable_enhanced_frame;
477 	hdmi_codec_plugged_cb plugged_cb;
478 	struct device *codec_dev;
479 	struct delayed_work delayed_audio;
480 	struct it6505_audio_data audio;
481 	struct dentry *debugfs;
482 
483 	/* it6505 driver hold option */
484 	bool enable_drv_hold;
485 
486 	const struct drm_edid *cached_edid;
487 
488 	int irq;
489 };
490 
491 struct it6505_step_train_para {
492 	u8 voltage_swing[MAX_LANE_COUNT];
493 	u8 pre_emphasis[MAX_LANE_COUNT];
494 };
495 
496 /*
497  * Vendor option afe settings for different platforms
498  * 0: without FPC cable
499  * 1: with FPC cable
500  */
501 
502 static const u8 afe_setting_table[][3] = {
503 	{0x82, 0x00, 0x45},
504 	{0x93, 0x2A, 0x85}
505 };
506 
507 static const struct it6505_audio_sample_rate_map audio_sample_rate_map[] = {
508 	{SAMPLE_RATE_24K, 24000},
509 	{SAMPLE_RATE_32K, 32000},
510 	{SAMPLE_RATE_48K, 48000},
511 	{SAMPLE_RATE_96K, 96000},
512 	{SAMPLE_RATE_192K, 192000},
513 	{SAMPLE_RATE_44_1K, 44100},
514 	{SAMPLE_RATE_88_2K, 88200},
515 	{SAMPLE_RATE_176_4K, 176400},
516 };
517 
518 static const struct regmap_range it6505_bridge_volatile_ranges[] = {
519 	{ .range_min = 0, .range_max = 0x1FF },
520 };
521 
522 static const struct regmap_access_table it6505_bridge_volatile_table = {
523 	.yes_ranges = it6505_bridge_volatile_ranges,
524 	.n_yes_ranges = ARRAY_SIZE(it6505_bridge_volatile_ranges),
525 };
526 
527 static const struct regmap_range_cfg it6505_regmap_banks[] = {
528 	{
529 		.name = "it6505",
530 		.range_min = 0x00,
531 		.range_max = 0x1FF,
532 		.selector_reg = REG_BANK_SEL,
533 		.selector_mask = 0x1,
534 		.selector_shift = 0,
535 		.window_start = 0x00,
536 		.window_len = 0x100,
537 	},
538 };
539 
540 static const struct regmap_config it6505_regmap_config = {
541 	.reg_bits = 8,
542 	.val_bits = 8,
543 	.volatile_table = &it6505_bridge_volatile_table,
544 	.cache_type = REGCACHE_NONE,
545 	.ranges = it6505_regmap_banks,
546 	.num_ranges = ARRAY_SIZE(it6505_regmap_banks),
547 	.max_register = 0x1FF,
548 };
549 
it6505_read(struct it6505 * it6505,unsigned int reg_addr)550 static int it6505_read(struct it6505 *it6505, unsigned int reg_addr)
551 {
552 	unsigned int value;
553 	int err;
554 	struct device *dev = it6505->dev;
555 
556 	if (!it6505->powered)
557 		return -ENODEV;
558 
559 	err = regmap_read(it6505->regmap, reg_addr, &value);
560 	if (err < 0) {
561 		dev_err(dev, "read failed reg[0x%x] err: %d", reg_addr, err);
562 		return err;
563 	}
564 
565 	return value;
566 }
567 
it6505_write(struct it6505 * it6505,unsigned int reg_addr,unsigned int reg_val)568 static int it6505_write(struct it6505 *it6505, unsigned int reg_addr,
569 			unsigned int reg_val)
570 {
571 	int err;
572 	struct device *dev = it6505->dev;
573 
574 	if (!it6505->powered)
575 		return -ENODEV;
576 
577 	err = regmap_write(it6505->regmap, reg_addr, reg_val);
578 
579 	if (err < 0) {
580 		dev_err(dev, "write failed reg[0x%x] = 0x%x err = %d",
581 			reg_addr, reg_val, err);
582 		return err;
583 	}
584 
585 	return 0;
586 }
587 
it6505_set_bits(struct it6505 * it6505,unsigned int reg,unsigned int mask,unsigned int value)588 static int it6505_set_bits(struct it6505 *it6505, unsigned int reg,
589 			   unsigned int mask, unsigned int value)
590 {
591 	int err;
592 	struct device *dev = it6505->dev;
593 
594 	if (!it6505->powered)
595 		return -ENODEV;
596 
597 	err = regmap_update_bits(it6505->regmap, reg, mask, value);
598 	if (err < 0) {
599 		dev_err(dev, "write reg[0x%x] = 0x%x mask = 0x%x failed err %d",
600 			reg, value, mask, err);
601 		return err;
602 	}
603 
604 	return 0;
605 }
606 
it6505_debug_print(struct it6505 * it6505,unsigned int reg,const char * prefix)607 static void it6505_debug_print(struct it6505 *it6505, unsigned int reg,
608 			       const char *prefix)
609 {
610 	struct device *dev = it6505->dev;
611 	int val;
612 
613 	if (!drm_debug_enabled(DRM_UT_DRIVER))
614 		return;
615 
616 	val = it6505_read(it6505, reg);
617 	if (val < 0)
618 		DRM_DEV_DEBUG_DRIVER(dev, "%s reg[%02x] read error (%d)",
619 				     prefix, reg, val);
620 	else
621 		DRM_DEV_DEBUG_DRIVER(dev, "%s reg[%02x] = 0x%02x", prefix, reg,
622 				     val);
623 }
624 
it6505_dpcd_read(struct it6505 * it6505,unsigned long offset)625 static int it6505_dpcd_read(struct it6505 *it6505, unsigned long offset)
626 {
627 	u8 value;
628 	int ret;
629 	struct device *dev = it6505->dev;
630 
631 	ret = drm_dp_dpcd_readb(&it6505->aux, offset, &value);
632 	if (ret < 0) {
633 		dev_err(dev, "DPCD read failed [0x%lx] ret: %d", offset, ret);
634 		return ret;
635 	}
636 	return value;
637 }
638 
it6505_dpcd_write(struct it6505 * it6505,unsigned long offset,u8 datain)639 static int it6505_dpcd_write(struct it6505 *it6505, unsigned long offset,
640 			     u8 datain)
641 {
642 	int ret;
643 	struct device *dev = it6505->dev;
644 
645 	ret = drm_dp_dpcd_writeb(&it6505->aux, offset, datain);
646 	if (ret < 0) {
647 		dev_err(dev, "DPCD write failed [0x%lx] ret: %d", offset, ret);
648 		return ret;
649 	}
650 	return 0;
651 }
652 
it6505_get_dpcd(struct it6505 * it6505,int offset,u8 * dpcd,int num)653 static int it6505_get_dpcd(struct it6505 *it6505, int offset, u8 *dpcd, int num)
654 {
655 	int ret;
656 	struct device *dev = it6505->dev;
657 
658 	ret = drm_dp_dpcd_read(&it6505->aux, offset, dpcd, num);
659 
660 	if (ret < 0)
661 		return ret;
662 
663 	DRM_DEV_DEBUG_DRIVER(dev, "ret = %d DPCD[0x%x] = 0x%*ph", ret, offset,
664 			     num, dpcd);
665 
666 	return 0;
667 }
668 
it6505_dump(struct it6505 * it6505)669 static void it6505_dump(struct it6505 *it6505)
670 {
671 	unsigned int i, j;
672 	u8 regs[16];
673 	struct device *dev = it6505->dev;
674 
675 	for (i = 0; i <= 0xff; i += 16) {
676 		for (j = 0; j < 16; j++)
677 			regs[j] = it6505_read(it6505, i + j);
678 
679 		DRM_DEV_DEBUG_DRIVER(dev, "[0x%02x] = %16ph", i, regs);
680 	}
681 }
682 
it6505_get_sink_hpd_status(struct it6505 * it6505)683 static bool it6505_get_sink_hpd_status(struct it6505 *it6505)
684 {
685 	int reg_0d;
686 
687 	reg_0d = it6505_read(it6505, REG_SYSTEM_STS);
688 
689 	if (reg_0d < 0)
690 		return false;
691 
692 	return reg_0d & HPD_STS;
693 }
694 
it6505_read_word(struct it6505 * it6505,unsigned int reg)695 static int it6505_read_word(struct it6505 *it6505, unsigned int reg)
696 {
697 	int val0, val1;
698 
699 	val0 = it6505_read(it6505, reg);
700 	if (val0 < 0)
701 		return val0;
702 
703 	val1 = it6505_read(it6505, reg + 1);
704 	if (val1 < 0)
705 		return val1;
706 
707 	return (val1 << 8) | val0;
708 }
709 
it6505_calc_video_info(struct it6505 * it6505)710 static void it6505_calc_video_info(struct it6505 *it6505)
711 {
712 	struct device *dev = it6505->dev;
713 	int hsync_pol, vsync_pol, interlaced;
714 	int htotal, hdes, hdew, hfph, hsyncw;
715 	int vtotal, vdes, vdew, vfph, vsyncw;
716 	int rddata, i, pclk, sum = 0;
717 
718 	usleep_range(10000, 15000);
719 	rddata = it6505_read(it6505, REG_INPUT_CTRL);
720 	hsync_pol = rddata & INPUT_HSYNC_POL;
721 	vsync_pol = (rddata & INPUT_VSYNC_POL) >> 2;
722 	interlaced = (rddata & INPUT_INTERLACED) >> 4;
723 
724 	htotal = it6505_read_word(it6505, REG_INPUT_HTOTAL) & 0x1FFF;
725 	hdes = it6505_read_word(it6505, REG_INPUT_HACTIVE_START) & 0x1FFF;
726 	hdew = it6505_read_word(it6505, REG_INPUT_HACTIVE_WIDTH) & 0x1FFF;
727 	hfph = it6505_read_word(it6505, REG_INPUT_HFRONT_PORCH) & 0x1FFF;
728 	hsyncw = it6505_read_word(it6505, REG_INPUT_HSYNC_WIDTH) & 0x1FFF;
729 
730 	vtotal = it6505_read_word(it6505, REG_INPUT_VTOTAL) & 0xFFF;
731 	vdes = it6505_read_word(it6505, REG_INPUT_VACTIVE_START) & 0xFFF;
732 	vdew = it6505_read_word(it6505, REG_INPUT_VACTIVE_WIDTH) & 0xFFF;
733 	vfph = it6505_read_word(it6505, REG_INPUT_VFRONT_PORCH) & 0xFFF;
734 	vsyncw = it6505_read_word(it6505, REG_INPUT_VSYNC_WIDTH) & 0xFFF;
735 
736 	DRM_DEV_DEBUG_DRIVER(dev, "hsync_pol:%d, vsync_pol:%d, interlaced:%d",
737 			     hsync_pol, vsync_pol, interlaced);
738 	DRM_DEV_DEBUG_DRIVER(dev, "hactive_start:%d, vactive_start:%d",
739 			     hdes, vdes);
740 
741 	for (i = 0; i < 3; i++) {
742 		it6505_set_bits(it6505, REG_DATA_CTRL0, ENABLE_PCLK_COUNTER,
743 				ENABLE_PCLK_COUNTER);
744 		usleep_range(10000, 15000);
745 		it6505_set_bits(it6505, REG_DATA_CTRL0, ENABLE_PCLK_COUNTER,
746 				0x00);
747 		rddata = it6505_read_word(it6505, REG_PCLK_COUNTER_VALUE) &
748 			 0xFFF;
749 
750 		sum += rddata;
751 	}
752 
753 	if (sum == 0) {
754 		DRM_DEV_DEBUG_DRIVER(dev, "calc video timing error");
755 		return;
756 	}
757 
758 	sum /= 3;
759 	pclk = 13500 * 2048 / sum;
760 	it6505->video_info.clock = pclk;
761 	it6505->video_info.hdisplay = hdew;
762 	it6505->video_info.hsync_start = hdew + hfph;
763 	it6505->video_info.hsync_end = hdew + hfph + hsyncw;
764 	it6505->video_info.htotal = htotal;
765 	it6505->video_info.vdisplay = vdew;
766 	it6505->video_info.vsync_start = vdew + vfph;
767 	it6505->video_info.vsync_end = vdew + vfph + vsyncw;
768 	it6505->video_info.vtotal = vtotal;
769 
770 	DRM_DEV_DEBUG_DRIVER(dev, DRM_MODE_FMT,
771 			     DRM_MODE_ARG(&it6505->video_info));
772 }
773 
it6505_drm_dp_link_set_power(struct drm_dp_aux * aux,struct it6505_drm_dp_link * link,u8 mode)774 static int it6505_drm_dp_link_set_power(struct drm_dp_aux *aux,
775 					struct it6505_drm_dp_link *link,
776 					u8 mode)
777 {
778 	u8 value;
779 	int err;
780 
781 	/* DP_SET_POWER register is only available on DPCD v1.1 and later */
782 	if (link->revision < DPCD_V_1_1)
783 		return 0;
784 
785 	err = drm_dp_dpcd_readb(aux, DP_SET_POWER, &value);
786 	if (err < 0)
787 		return err;
788 
789 	value &= ~DP_SET_POWER_MASK;
790 	value |= mode;
791 
792 	err = drm_dp_dpcd_writeb(aux, DP_SET_POWER, value);
793 	if (err < 0)
794 		return err;
795 
796 	if (mode == DP_SET_POWER_D0) {
797 		/*
798 		 * According to the DP 1.1 specification, a "Sink Device must
799 		 * exit the power saving state within 1 ms" (Section 2.5.3.1,
800 		 * Table 5-52, "Sink Control Field" (register 0x600).
801 		 */
802 		usleep_range(1000, 2000);
803 	}
804 
805 	return 0;
806 }
807 
it6505_clear_int(struct it6505 * it6505)808 static void it6505_clear_int(struct it6505 *it6505)
809 {
810 	it6505_write(it6505, INT_STATUS_01, 0xFF);
811 	it6505_write(it6505, INT_STATUS_02, 0xFF);
812 	it6505_write(it6505, INT_STATUS_03, 0xFF);
813 }
814 
it6505_int_mask_enable(struct it6505 * it6505)815 static void it6505_int_mask_enable(struct it6505 *it6505)
816 {
817 	it6505_write(it6505, INT_MASK_01, BIT(INT_HPD_CHANGE) |
818 		     BIT(INT_RECEIVE_HPD_IRQ) | BIT(INT_SCDT_CHANGE) |
819 		     BIT(INT_HDCP_FAIL) | BIT(INT_HDCP_DONE));
820 
821 	it6505_write(it6505, INT_MASK_02, BIT(INT_AUX_CMD_FAIL) |
822 		     BIT(INT_HDCP_KSV_CHECK) | BIT(INT_AUDIO_FIFO_ERROR));
823 
824 	it6505_write(it6505, INT_MASK_03, BIT(INT_LINK_TRAIN_FAIL) |
825 		     BIT(INT_VID_FIFO_ERROR) | BIT(INT_IO_LATCH_FIFO_OVERFLOW));
826 }
827 
it6505_int_mask_disable(struct it6505 * it6505)828 static void it6505_int_mask_disable(struct it6505 *it6505)
829 {
830 	it6505_write(it6505, INT_MASK_01, 0x00);
831 	it6505_write(it6505, INT_MASK_02, 0x00);
832 	it6505_write(it6505, INT_MASK_03, 0x00);
833 }
834 
it6505_lane_termination_on(struct it6505 * it6505)835 static void it6505_lane_termination_on(struct it6505 *it6505)
836 {
837 	int regcf;
838 
839 	regcf = it6505_read(it6505, REG_USER_DRV_PRE);
840 
841 	if (regcf == MISC_VERB)
842 		it6505_set_bits(it6505, REG_DRV_LN_DATA_SEL, 0x80, 0x00);
843 
844 	if (regcf == MISC_VERC) {
845 		if (it6505->lane_swap) {
846 			switch (it6505->lane_count) {
847 			case 1:
848 			case 2:
849 				it6505_set_bits(it6505, REG_DRV_LN_DATA_SEL,
850 						0x0C, 0x08);
851 				break;
852 			default:
853 				it6505_set_bits(it6505, REG_DRV_LN_DATA_SEL,
854 						0x0C, 0x0C);
855 				break;
856 			}
857 		} else {
858 			switch (it6505->lane_count) {
859 			case 1:
860 			case 2:
861 				it6505_set_bits(it6505, REG_DRV_LN_DATA_SEL,
862 						0x0C, 0x04);
863 				break;
864 			default:
865 				it6505_set_bits(it6505, REG_DRV_LN_DATA_SEL,
866 						0x0C, 0x0C);
867 				break;
868 			}
869 		}
870 	}
871 }
872 
it6505_lane_termination_off(struct it6505 * it6505)873 static void it6505_lane_termination_off(struct it6505 *it6505)
874 {
875 	int regcf;
876 
877 	regcf = it6505_read(it6505, REG_USER_DRV_PRE);
878 
879 	if (regcf == MISC_VERB)
880 		it6505_set_bits(it6505, REG_DRV_LN_DATA_SEL, 0x80, 0x80);
881 
882 	if (regcf == MISC_VERC)
883 		it6505_set_bits(it6505, REG_DRV_LN_DATA_SEL, 0x0C, 0x00);
884 }
885 
it6505_lane_power_on(struct it6505 * it6505)886 static void it6505_lane_power_on(struct it6505 *it6505)
887 {
888 	it6505_set_bits(it6505, REG_LINK_DRV, 0xF1,
889 			(it6505->lane_swap ?
890 				 GENMASK(7, 8 - it6505->lane_count) :
891 				 GENMASK(3 + it6505->lane_count, 4)) |
892 				0x01);
893 }
894 
it6505_lane_power_off(struct it6505 * it6505)895 static void it6505_lane_power_off(struct it6505 *it6505)
896 {
897 	it6505_set_bits(it6505, REG_LINK_DRV, 0xF0, 0x00);
898 }
899 
it6505_lane_off(struct it6505 * it6505)900 static void it6505_lane_off(struct it6505 *it6505)
901 {
902 	it6505_lane_power_off(it6505);
903 	it6505_lane_termination_off(it6505);
904 }
905 
it6505_aux_termination_on(struct it6505 * it6505)906 static void it6505_aux_termination_on(struct it6505 *it6505)
907 {
908 	int regcf;
909 
910 	regcf = it6505_read(it6505, REG_USER_DRV_PRE);
911 
912 	if (regcf == MISC_VERB)
913 		it6505_lane_termination_on(it6505);
914 
915 	if (regcf == MISC_VERC)
916 		it6505_set_bits(it6505, REG_DRV_LN_DATA_SEL, 0x80, 0x80);
917 }
918 
it6505_aux_power_on(struct it6505 * it6505)919 static void it6505_aux_power_on(struct it6505 *it6505)
920 {
921 	it6505_set_bits(it6505, REG_AUX, 0x02, 0x02);
922 }
923 
it6505_aux_on(struct it6505 * it6505)924 static void it6505_aux_on(struct it6505 *it6505)
925 {
926 	it6505_aux_power_on(it6505);
927 	it6505_aux_termination_on(it6505);
928 }
929 
it6505_aux_reset(struct it6505 * it6505)930 static void it6505_aux_reset(struct it6505 *it6505)
931 {
932 	it6505_set_bits(it6505, REG_RESET_CTRL, AUX_RESET, AUX_RESET);
933 	it6505_set_bits(it6505, REG_RESET_CTRL, AUX_RESET, 0x00);
934 }
935 
it6505_reset_logic(struct it6505 * it6505)936 static void it6505_reset_logic(struct it6505 *it6505)
937 {
938 	regmap_write(it6505->regmap, REG_RESET_CTRL, ALL_LOGIC_RESET);
939 	usleep_range(1000, 1500);
940 }
941 
it6505_aux_op_finished(struct it6505 * it6505)942 static bool it6505_aux_op_finished(struct it6505 *it6505)
943 {
944 	int reg2b = it6505_read(it6505, REG_AUX_CMD_REQ);
945 
946 	if (reg2b < 0)
947 		return false;
948 
949 	return (reg2b & AUX_BUSY) == 0;
950 }
951 
it6505_aux_wait(struct it6505 * it6505)952 static int it6505_aux_wait(struct it6505 *it6505)
953 {
954 	int status;
955 	unsigned long timeout;
956 	struct device *dev = it6505->dev;
957 
958 	timeout = jiffies + msecs_to_jiffies(AUX_WAIT_TIMEOUT_MS) + 1;
959 
960 	while (!it6505_aux_op_finished(it6505)) {
961 		if (time_after(jiffies, timeout)) {
962 			dev_err(dev, "Timed out waiting AUX to finish");
963 			return -ETIMEDOUT;
964 		}
965 		usleep_range(1000, 2000);
966 	}
967 
968 	status = it6505_read(it6505, REG_AUX_ERROR_STS);
969 	if (status < 0) {
970 		dev_err(dev, "Failed to read AUX channel: %d", status);
971 		return status;
972 	}
973 
974 	return 0;
975 }
976 
it6505_aux_operation(struct it6505 * it6505,enum aux_cmd_type cmd,unsigned int address,u8 * buffer,size_t size,enum aux_cmd_reply * reply)977 static ssize_t it6505_aux_operation(struct it6505 *it6505,
978 				    enum aux_cmd_type cmd,
979 				    unsigned int address, u8 *buffer,
980 				    size_t size, enum aux_cmd_reply *reply)
981 {
982 	int i, ret;
983 	bool aux_write_check = false;
984 
985 	if (!it6505_get_sink_hpd_status(it6505))
986 		return -EIO;
987 
988 	/* set AUX user mode */
989 	it6505_set_bits(it6505, REG_AUX_CTRL, AUX_USER_MODE, AUX_USER_MODE);
990 
991 aux_op_start:
992 	/* HW AUX FIFO supports only EDID and DCPD KSV FIFO area */
993 	if (cmd == CMD_AUX_I2C_EDID_READ || cmd == CMD_AUX_GET_KSV_LIST) {
994 		/* AUX EDID FIFO has max length of AUX_FIFO_MAX_SIZE bytes. */
995 		size = min_t(size_t, size, AUX_FIFO_MAX_SIZE);
996 		/* Enable AUX FIFO read back and clear FIFO */
997 		it6505_set_bits(it6505, REG_AUX_CTRL,
998 				AUX_EN_FIFO_READ | CLR_EDID_FIFO,
999 				AUX_EN_FIFO_READ | CLR_EDID_FIFO);
1000 
1001 		it6505_set_bits(it6505, REG_AUX_CTRL,
1002 				AUX_EN_FIFO_READ | CLR_EDID_FIFO,
1003 				AUX_EN_FIFO_READ);
1004 	} else {
1005 		/* The DP AUX transmit buffer has 4 bytes. */
1006 		size = min_t(size_t, size, 4);
1007 		it6505_set_bits(it6505, REG_AUX_CTRL, AUX_NO_SEGMENT_WR,
1008 				AUX_NO_SEGMENT_WR);
1009 	}
1010 
1011 	/* Start Address[7:0] */
1012 	it6505_write(it6505, REG_AUX_ADR_0_7, (address >> 0) & 0xFF);
1013 	/* Start Address[15:8] */
1014 	it6505_write(it6505, REG_AUX_ADR_8_15, (address >> 8) & 0xFF);
1015 	/* WriteNum[3:0]+StartAdr[19:16] */
1016 	it6505_write(it6505, REG_AUX_ADR_16_19,
1017 		     ((address >> 16) & 0x0F) | ((size - 1) << 4));
1018 
1019 	if (cmd == CMD_AUX_NATIVE_WRITE)
1020 		regmap_bulk_write(it6505->regmap, REG_AUX_OUT_DATA0, buffer,
1021 				  size);
1022 
1023 	/* Aux Fire */
1024 	it6505_write(it6505, REG_AUX_CMD_REQ, FIELD_GET(M_AUX_REQ_CMD, cmd));
1025 
1026 	ret = it6505_aux_wait(it6505);
1027 	if (ret < 0)
1028 		goto aux_op_err;
1029 
1030 	ret = it6505_read(it6505, REG_AUX_ERROR_STS);
1031 	if (ret < 0)
1032 		goto aux_op_err;
1033 
1034 	switch ((ret >> 6) & 0x3) {
1035 	case 0:
1036 		*reply = REPLY_ACK;
1037 		break;
1038 	case 1:
1039 		*reply = REPLY_DEFER;
1040 		ret = -EAGAIN;
1041 		goto aux_op_err;
1042 	case 2:
1043 		*reply = REPLY_NACK;
1044 		ret = -EIO;
1045 		goto aux_op_err;
1046 	case 3:
1047 		ret = -ETIMEDOUT;
1048 		goto aux_op_err;
1049 	}
1050 
1051 	/* Read back Native Write data */
1052 	if (cmd == CMD_AUX_NATIVE_WRITE) {
1053 		aux_write_check = true;
1054 		cmd = CMD_AUX_NATIVE_READ;
1055 		goto aux_op_start;
1056 	}
1057 
1058 	if (cmd == CMD_AUX_I2C_EDID_READ || cmd == CMD_AUX_GET_KSV_LIST) {
1059 		for (i = 0; i < size; i++) {
1060 			ret = it6505_read(it6505, REG_AUX_DATA_FIFO);
1061 			if (ret < 0)
1062 				goto aux_op_err;
1063 			buffer[i] = ret;
1064 		}
1065 	} else {
1066 		for (i = 0; i < size; i++) {
1067 			ret = it6505_read(it6505, REG_AUX_DATA_0_7 + i);
1068 			if (ret < 0)
1069 				goto aux_op_err;
1070 
1071 			if (aux_write_check && buffer[size - 1 - i] != ret) {
1072 				ret = -EINVAL;
1073 				goto aux_op_err;
1074 			}
1075 
1076 			buffer[size - 1 - i] = ret;
1077 		}
1078 	}
1079 
1080 	ret = i;
1081 
1082 aux_op_err:
1083 	if (cmd == CMD_AUX_I2C_EDID_READ || cmd == CMD_AUX_GET_KSV_LIST) {
1084 		/* clear AUX FIFO */
1085 		it6505_set_bits(it6505, REG_AUX_CTRL,
1086 				AUX_EN_FIFO_READ | CLR_EDID_FIFO,
1087 				AUX_EN_FIFO_READ | CLR_EDID_FIFO);
1088 		it6505_set_bits(it6505, REG_AUX_CTRL,
1089 				AUX_EN_FIFO_READ | CLR_EDID_FIFO, 0x00);
1090 	}
1091 
1092 	/* Leave AUX user mode */
1093 	it6505_set_bits(it6505, REG_AUX_CTRL, AUX_USER_MODE, 0);
1094 
1095 	return ret;
1096 }
1097 
it6505_aux_do_transfer(struct it6505 * it6505,enum aux_cmd_type cmd,unsigned int address,u8 * buffer,size_t size,enum aux_cmd_reply * reply)1098 static ssize_t it6505_aux_do_transfer(struct it6505 *it6505,
1099 				      enum aux_cmd_type cmd,
1100 				      unsigned int address, u8 *buffer,
1101 				      size_t size, enum aux_cmd_reply *reply)
1102 {
1103 	int i, ret_size, ret = 0, request_size;
1104 	int fifo_max_size = (cmd == CMD_AUX_I2C_EDID_READ || cmd == CMD_AUX_GET_KSV_LIST) ?
1105 						 AUX_FIFO_MAX_SIZE : 4;
1106 
1107 	mutex_lock(&it6505->aux_lock);
1108 	i = 0;
1109 	do {
1110 		request_size = min_t(int, (int)size - i, fifo_max_size);
1111 
1112 		ret_size = it6505_aux_operation(it6505, cmd, address + i,
1113 						buffer + i, request_size,
1114 						reply);
1115 		if (ret_size < 0) {
1116 			ret = ret_size;
1117 			goto aux_op_err;
1118 		}
1119 
1120 		i += request_size;
1121 		ret += ret_size;
1122 	} while (i < size);
1123 
1124 aux_op_err:
1125 	mutex_unlock(&it6505->aux_lock);
1126 	return ret;
1127 }
1128 
it6505_aux_i2c_reply_defer(u8 reply)1129 static bool it6505_aux_i2c_reply_defer(u8 reply)
1130 {
1131 	if (reply == DP_AUX_NATIVE_REPLY_DEFER || reply == DP_AUX_I2C_REPLY_DEFER)
1132 		return true;
1133 	return false;
1134 }
1135 
it6505_aux_i2c_reply_nack(u8 reply)1136 static bool it6505_aux_i2c_reply_nack(u8 reply)
1137 {
1138 	if (reply == DP_AUX_NATIVE_REPLY_NACK || reply == DP_AUX_I2C_REPLY_NACK)
1139 		return true;
1140 	return false;
1141 }
1142 
it6505_aux_i2c_wait(struct it6505 * it6505,u8 * reply)1143 static int it6505_aux_i2c_wait(struct it6505 *it6505, u8 *reply)
1144 {
1145 	int err = 0;
1146 	unsigned long timeout;
1147 	struct device *dev = it6505->dev;
1148 
1149 	timeout = jiffies + msecs_to_jiffies(AUX_WAIT_TIMEOUT_MS) + 1;
1150 
1151 	do {
1152 		if (it6505_read(it6505, REG_AUX_USER_CTRL) & AUX_EVENT)
1153 			break;
1154 		if (time_after(jiffies, timeout)) {
1155 			dev_err(dev, "Timed out waiting AUX I2C, BUSY = %X\n",
1156 				it6505_aux_op_finished(it6505));
1157 			err = -ETIMEDOUT;
1158 			goto end_aux_i2c_wait;
1159 		}
1160 		usleep_range(300, 800);
1161 	} while (!it6505_aux_op_finished(it6505));
1162 
1163 	*reply = it6505_read(it6505, REG_AUX_USER_REPLY) >> 4;
1164 
1165 	if (*reply == 0)
1166 		goto end_aux_i2c_wait;
1167 
1168 	if (it6505_aux_i2c_reply_defer(*reply))
1169 		err = -EBUSY;
1170 	else if (it6505_aux_i2c_reply_nack(*reply))
1171 		err = -ENXIO;
1172 
1173 end_aux_i2c_wait:
1174 	it6505_set_bits(it6505, REG_AUX_USER_CTRL, USER_AUX_DONE, USER_AUX_DONE);
1175 	return err;
1176 }
1177 
it6505_aux_i2c_readb(struct it6505 * it6505,u8 * buf,size_t size,u8 * reply)1178 static int it6505_aux_i2c_readb(struct it6505 *it6505, u8 *buf, size_t size, u8 *reply)
1179 {
1180 	int ret, i;
1181 	int retry;
1182 
1183 	for (retry = 0; retry < AUX_I2C_DEFER_RETRY; retry++) {
1184 		it6505_write(it6505, REG_AUX_CMD_REQ, CMD_AUX_GI2C_READ);
1185 
1186 		ret = it6505_aux_i2c_wait(it6505, reply);
1187 		if (it6505_aux_i2c_reply_defer(*reply))
1188 			continue;
1189 		if (ret >= 0)
1190 			break;
1191 	}
1192 
1193 	for (i = 0; i < size; i++)
1194 		buf[i] = it6505_read(it6505, REG_AUX_USER_RXB(0 + i));
1195 
1196 	return size;
1197 }
1198 
it6505_aux_i2c_writeb(struct it6505 * it6505,u8 * buf,size_t size,u8 * reply)1199 static int it6505_aux_i2c_writeb(struct it6505 *it6505, u8 *buf, size_t size, u8 *reply)
1200 {
1201 	int i, ret;
1202 	int retry;
1203 
1204 	for (i = 0; i < size; i++)
1205 		it6505_write(it6505, REG_AUX_OUT_DATA0 + i, buf[i]);
1206 
1207 	for (retry = 0; retry < AUX_I2C_DEFER_RETRY; retry++) {
1208 		it6505_write(it6505, REG_AUX_CMD_REQ, CMD_AUX_GI2C_WRITE);
1209 
1210 		ret = it6505_aux_i2c_wait(it6505, reply);
1211 		if (it6505_aux_i2c_reply_defer(*reply))
1212 			continue;
1213 		if (ret >= 0)
1214 			break;
1215 	}
1216 	return size;
1217 }
1218 
it6505_aux_i2c_operation(struct it6505 * it6505,struct drm_dp_aux_msg * msg)1219 static ssize_t it6505_aux_i2c_operation(struct it6505 *it6505,
1220 					struct drm_dp_aux_msg *msg)
1221 {
1222 	int ret;
1223 	ssize_t request_size, data_cnt = 0;
1224 	u8 *buffer = msg->buffer;
1225 
1226 	/* set AUX user mode */
1227 	it6505_set_bits(it6505, REG_AUX_CTRL,
1228 			AUX_USER_MODE | AUX_NO_SEGMENT_WR, AUX_USER_MODE);
1229 	it6505_set_bits(it6505, REG_AUX_USER_CTRL, EN_USER_AUX, EN_USER_AUX);
1230 	/* clear AUX FIFO */
1231 	it6505_set_bits(it6505, REG_AUX_CTRL,
1232 			AUX_EN_FIFO_READ | CLR_EDID_FIFO,
1233 			AUX_EN_FIFO_READ | CLR_EDID_FIFO);
1234 
1235 	it6505_set_bits(it6505, REG_AUX_CTRL,
1236 			AUX_EN_FIFO_READ | CLR_EDID_FIFO, 0x00);
1237 
1238 	it6505_write(it6505, REG_AUX_ADR_0_7, 0x00);
1239 	it6505_write(it6505, REG_AUX_ADR_8_15, msg->address << 1);
1240 
1241 	if (msg->size == 0) {
1242 		/* IIC Start/STOP dummy write */
1243 		it6505_write(it6505, REG_AUX_ADR_16_19, msg->request);
1244 		it6505_write(it6505, REG_AUX_CMD_REQ, CMD_AUX_GI2C_ADR);
1245 		ret = it6505_aux_i2c_wait(it6505, &msg->reply);
1246 		goto end_aux_i2c_transfer;
1247 	}
1248 
1249 	/* IIC data transfer */
1250 	data_cnt = 0;
1251 	do {
1252 		request_size = min_t(ssize_t, msg->size - data_cnt, AUX_I2C_MAX_SIZE);
1253 		it6505_write(it6505, REG_AUX_ADR_16_19,
1254 			     msg->request | ((request_size - 1) << 4));
1255 		if ((msg->request & DP_AUX_I2C_READ) == DP_AUX_I2C_READ)
1256 			ret = it6505_aux_i2c_readb(it6505, &buffer[data_cnt],
1257 						   request_size, &msg->reply);
1258 		else
1259 			ret = it6505_aux_i2c_writeb(it6505, &buffer[data_cnt],
1260 						    request_size, &msg->reply);
1261 
1262 		if (ret < 0)
1263 			goto end_aux_i2c_transfer;
1264 
1265 		data_cnt += request_size;
1266 	} while (data_cnt < msg->size);
1267 	ret = data_cnt;
1268 end_aux_i2c_transfer:
1269 
1270 	it6505_set_bits(it6505, REG_AUX_USER_CTRL, EN_USER_AUX, 0);
1271 	it6505_set_bits(it6505, REG_AUX_CTRL, AUX_USER_MODE, 0);
1272 	return ret;
1273 }
1274 
it6505_aux_i2c_transfer(struct drm_dp_aux * aux,struct drm_dp_aux_msg * msg)1275 static ssize_t it6505_aux_i2c_transfer(struct drm_dp_aux *aux,
1276 				       struct drm_dp_aux_msg *msg)
1277 {
1278 	struct it6505 *it6505 = container_of(aux, struct it6505, aux);
1279 
1280 	guard(mutex)(&it6505->aux_lock);
1281 	return it6505_aux_i2c_operation(it6505, msg);
1282 }
1283 
it6505_aux_transfer(struct drm_dp_aux * aux,struct drm_dp_aux_msg * msg)1284 static ssize_t it6505_aux_transfer(struct drm_dp_aux *aux,
1285 				   struct drm_dp_aux_msg *msg)
1286 {
1287 	struct it6505 *it6505 = container_of(aux, struct it6505, aux);
1288 	u8 cmd;
1289 	bool is_i2c = !(msg->request & DP_AUX_NATIVE_WRITE);
1290 	int ret;
1291 	enum aux_cmd_reply reply;
1292 
1293 	if (is_i2c)
1294 		return it6505_aux_i2c_transfer(aux, msg);
1295 
1296 	switch (msg->request) {
1297 	case DP_AUX_NATIVE_READ:
1298 		cmd = CMD_AUX_NATIVE_READ;
1299 		break;
1300 	case DP_AUX_NATIVE_WRITE:
1301 		cmd = CMD_AUX_NATIVE_WRITE;
1302 		break;
1303 	default:
1304 		return -EINVAL;
1305 	}
1306 
1307 	ret = it6505_aux_do_transfer(it6505, cmd, msg->address, msg->buffer,
1308 				     msg->size, &reply);
1309 	if (ret < 0)
1310 		return ret;
1311 
1312 	switch (reply) {
1313 	case REPLY_ACK:
1314 		msg->reply = DP_AUX_NATIVE_REPLY_ACK;
1315 		break;
1316 	case REPLY_NACK:
1317 		msg->reply = DP_AUX_NATIVE_REPLY_NACK;
1318 		break;
1319 	case REPLY_DEFER:
1320 		msg->reply = DP_AUX_NATIVE_REPLY_DEFER;
1321 		break;
1322 	}
1323 
1324 	return ret;
1325 }
1326 
it6505_get_edid_block(void * data,u8 * buf,unsigned int block,size_t len)1327 static int it6505_get_edid_block(void *data, u8 *buf, unsigned int block,
1328 				 size_t len)
1329 {
1330 	struct it6505 *it6505 = data;
1331 	struct device *dev = it6505->dev;
1332 	enum aux_cmd_reply reply;
1333 	int offset, ret, aux_retry = 100;
1334 
1335 	it6505_aux_reset(it6505);
1336 	DRM_DEV_DEBUG_DRIVER(dev, "block number = %d", block);
1337 
1338 	for (offset = 0; offset < EDID_LENGTH;) {
1339 		ret = it6505_aux_do_transfer(it6505, CMD_AUX_I2C_EDID_READ,
1340 					     block * EDID_LENGTH + offset,
1341 					     buf + offset, 8, &reply);
1342 
1343 		if (ret < 0 && ret != -EAGAIN)
1344 			return ret;
1345 
1346 		switch (reply) {
1347 		case REPLY_ACK:
1348 			DRM_DEV_DEBUG_DRIVER(dev, "[0x%02x]: %8ph", offset,
1349 					     buf + offset);
1350 			offset += 8;
1351 			aux_retry = 100;
1352 			break;
1353 		case REPLY_NACK:
1354 			return -EIO;
1355 		case REPLY_DEFER:
1356 			msleep(20);
1357 			if (!(--aux_retry))
1358 				return -EIO;
1359 		}
1360 	}
1361 
1362 	return 0;
1363 }
1364 
it6505_get_ksvlist(struct it6505 * it6505,u8 * buf,size_t len)1365 static int it6505_get_ksvlist(struct it6505 *it6505, u8 *buf, size_t len)
1366 {
1367 	struct device *dev = it6505->dev;
1368 	enum aux_cmd_reply reply;
1369 	int request_size, ret;
1370 	int i = 0;
1371 
1372 	do {
1373 		request_size = min_t(int, (int)len - i, 15);
1374 
1375 		ret = it6505_aux_do_transfer(it6505, CMD_AUX_GET_KSV_LIST,
1376 					     DP_AUX_HDCP_KSV_FIFO,
1377 					     buf + i, request_size, &reply);
1378 
1379 		DRM_DEV_DEBUG_DRIVER(dev, "request_size = %d, ret =%d", request_size, ret);
1380 		if (ret < 0)
1381 			return ret;
1382 
1383 		i += request_size;
1384 	} while (i < len);
1385 
1386 	DRM_DEV_DEBUG_DRIVER(dev, "ksv read cnt = %d down_stream_cnt=%d ", i, i / 5);
1387 
1388 	for (i = 0 ; i < len; i += 5) {
1389 		DRM_DEV_DEBUG_DRIVER(dev, "ksv[%d] = %02X%02X%02X%02X%02X",
1390 				     i / 5, buf[i], buf[i + 1], buf[i + 2], buf[i + 3], buf[i + 4]);
1391 	}
1392 
1393 	return len;
1394 }
1395 
it6505_variable_config(struct it6505 * it6505)1396 static void it6505_variable_config(struct it6505 *it6505)
1397 {
1398 	it6505->link_rate_bw_code = HBR;
1399 	it6505->lane_count = MAX_LANE_COUNT;
1400 	it6505->link_state = LINK_IDLE;
1401 	it6505->hdcp_desired = HDCP_DESIRED;
1402 	it6505->auto_train_retry = AUTO_TRAIN_RETRY;
1403 	it6505->audio.select = AUDIO_SELECT;
1404 	it6505->audio.sample_rate = AUDIO_SAMPLE_RATE;
1405 	it6505->audio.channel_count = AUDIO_CHANNEL_COUNT;
1406 	it6505->audio.type = AUDIO_TYPE;
1407 	it6505->audio.i2s_input_format = I2S_INPUT_FORMAT;
1408 	it6505->audio.i2s_justified = I2S_JUSTIFIED;
1409 	it6505->audio.i2s_data_delay = I2S_DATA_DELAY;
1410 	it6505->audio.i2s_ws_channel = I2S_WS_CHANNEL;
1411 	it6505->audio.i2s_data_sequence = I2S_DATA_SEQUENCE;
1412 	it6505->audio.word_length = AUDIO_WORD_LENGTH;
1413 	memset(it6505->sha1_input, 0, sizeof(it6505->sha1_input));
1414 	memset(it6505->bksvs, 0, sizeof(it6505->bksvs));
1415 }
1416 
it6505_send_video_infoframe(struct it6505 * it6505,struct hdmi_avi_infoframe * frame)1417 static int it6505_send_video_infoframe(struct it6505 *it6505,
1418 				       struct hdmi_avi_infoframe *frame)
1419 {
1420 	u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AVI_INFOFRAME_SIZE];
1421 	int err;
1422 	struct device *dev = it6505->dev;
1423 
1424 	err = hdmi_avi_infoframe_pack(frame, buffer, sizeof(buffer));
1425 	if (err < 0) {
1426 		dev_err(dev, "Failed to pack AVI infoframe: %d", err);
1427 		return err;
1428 	}
1429 
1430 	err = it6505_set_bits(it6505, REG_INFOFRAME_CTRL, EN_AVI_PKT, 0x00);
1431 	if (err)
1432 		return err;
1433 
1434 	err = regmap_bulk_write(it6505->regmap, REG_AVI_INFO_DB1,
1435 				buffer + HDMI_INFOFRAME_HEADER_SIZE,
1436 				frame->length);
1437 	if (err)
1438 		return err;
1439 
1440 	err = it6505_set_bits(it6505, REG_INFOFRAME_CTRL, EN_AVI_PKT,
1441 			      EN_AVI_PKT);
1442 	if (err)
1443 		return err;
1444 
1445 	return 0;
1446 }
1447 
it6505_get_extcon_property(struct it6505 * it6505)1448 static void it6505_get_extcon_property(struct it6505 *it6505)
1449 {
1450 	int err;
1451 	union extcon_property_value property;
1452 	struct device *dev = it6505->dev;
1453 
1454 	if (it6505->extcon && !it6505->lane_swap_disabled) {
1455 		err = extcon_get_property(it6505->extcon, EXTCON_DISP_DP,
1456 					  EXTCON_PROP_USB_TYPEC_POLARITY,
1457 					  &property);
1458 		if (err) {
1459 			dev_err(dev, "get property fail!");
1460 			return;
1461 		}
1462 		it6505->lane_swap = property.intval;
1463 	}
1464 }
1465 
it6505_clk_phase_adjustment(struct it6505 * it6505,const struct drm_display_mode * mode)1466 static void it6505_clk_phase_adjustment(struct it6505 *it6505,
1467 					const struct drm_display_mode *mode)
1468 {
1469 	int clock = mode->clock;
1470 
1471 	it6505_set_bits(it6505, REG_CLK_CTRL0, M_PCLK_DELAY,
1472 			clock < ADJUST_PHASE_THRESHOLD ? PIXEL_CLK_DELAY : 0);
1473 	it6505_set_bits(it6505, REG_DATA_CTRL0, VIDEO_LATCH_EDGE,
1474 			PIXEL_CLK_INVERSE << 4);
1475 }
1476 
it6505_link_reset_step_train(struct it6505 * it6505)1477 static void it6505_link_reset_step_train(struct it6505 *it6505)
1478 {
1479 	it6505_set_bits(it6505, REG_TRAIN_CTRL0,
1480 			FORCE_CR_DONE | FORCE_EQ_DONE, 0x00);
1481 	it6505_dpcd_write(it6505, DP_TRAINING_PATTERN_SET,
1482 			  DP_TRAINING_PATTERN_DISABLE);
1483 }
1484 
it6505_init(struct it6505 * it6505)1485 static void it6505_init(struct it6505 *it6505)
1486 {
1487 	it6505_write(it6505, REG_AUX_OPT, AUX_AUTO_RST | AUX_FIX_FREQ);
1488 	it6505_write(it6505, REG_AUX_CTRL, AUX_NO_SEGMENT_WR);
1489 	it6505_write(it6505, REG_HDCP_CTRL2, HDCP_AN_SEL | HDCP_HW_HPDIRQ_ACT);
1490 	it6505_write(it6505, REG_VID_BUS_CTRL0, IN_DDR | DDR_CD);
1491 	it6505_write(it6505, REG_VID_BUS_CTRL1, 0x01);
1492 	it6505_write(it6505, REG_AUDIO_CTRL0, AUDIO_16B_BOUND);
1493 
1494 	/* chip internal setting, don't modify */
1495 	it6505_write(it6505, REG_HPD_IRQ_TIME, 0xF5);
1496 	it6505_write(it6505, REG_AUX_DEBUG_MODE, 0x4D);
1497 	it6505_write(it6505, REG_AUX_OPT2, 0x17);
1498 	it6505_write(it6505, REG_HDCP_OPT, 0x60);
1499 	it6505_write(it6505, REG_DATA_MUTE_CTRL,
1500 		     EN_VID_MUTE | EN_AUD_MUTE | ENABLE_AUTO_VIDEO_FIFO_RESET);
1501 	it6505_write(it6505, REG_TIME_STMP_CTRL,
1502 		     EN_SSC_GAT | EN_ENHANCE_VID_STMP | EN_ENHANCE_AUD_STMP);
1503 	it6505_write(it6505, REG_INFOFRAME_CTRL, 0x00);
1504 	it6505_write(it6505, REG_DRV_0_DB_800_MV,
1505 		     afe_setting_table[it6505->afe_setting][0]);
1506 	it6505_write(it6505, REG_PRE_0_DB_800_MV,
1507 		     afe_setting_table[it6505->afe_setting][1]);
1508 	it6505_write(it6505, REG_PRE_3P5_DB_800_MV,
1509 		     afe_setting_table[it6505->afe_setting][2]);
1510 	it6505_write(it6505, REG_SSC_CTRL0, 0x9E);
1511 	it6505_write(it6505, REG_SSC_CTRL1, 0x1C);
1512 	it6505_write(it6505, REG_SSC_CTRL2, 0x42);
1513 }
1514 
it6505_video_disable(struct it6505 * it6505)1515 static void it6505_video_disable(struct it6505 *it6505)
1516 {
1517 	it6505_set_bits(it6505, REG_DATA_MUTE_CTRL, EN_VID_MUTE, EN_VID_MUTE);
1518 	it6505_set_bits(it6505, REG_INFOFRAME_CTRL, EN_VID_CTRL_PKT, 0x00);
1519 	it6505_set_bits(it6505, REG_RESET_CTRL, VIDEO_RESET, VIDEO_RESET);
1520 }
1521 
it6505_video_reset(struct it6505 * it6505)1522 static void it6505_video_reset(struct it6505 *it6505)
1523 {
1524 	it6505_link_reset_step_train(it6505);
1525 	it6505_set_bits(it6505, REG_DATA_MUTE_CTRL, EN_VID_MUTE, EN_VID_MUTE);
1526 	it6505_set_bits(it6505, REG_INFOFRAME_CTRL, EN_VID_CTRL_PKT, 0x00);
1527 
1528 	it6505_set_bits(it6505, REG_VID_BUS_CTRL1, TX_FIFO_RESET, TX_FIFO_RESET);
1529 	it6505_set_bits(it6505, REG_VID_BUS_CTRL1, TX_FIFO_RESET, 0x00);
1530 
1531 	it6505_set_bits(it6505, REG_501_FIFO_CTRL, RST_501_FIFO, RST_501_FIFO);
1532 	it6505_set_bits(it6505, REG_501_FIFO_CTRL, RST_501_FIFO, 0x00);
1533 
1534 	it6505_set_bits(it6505, REG_RESET_CTRL, VIDEO_RESET, VIDEO_RESET);
1535 	usleep_range(1000, 2000);
1536 	it6505_set_bits(it6505, REG_RESET_CTRL, VIDEO_RESET, 0x00);
1537 }
1538 
it6505_update_video_parameter(struct it6505 * it6505,const struct drm_display_mode * mode)1539 static void it6505_update_video_parameter(struct it6505 *it6505,
1540 					  const struct drm_display_mode *mode)
1541 {
1542 	it6505_clk_phase_adjustment(it6505, mode);
1543 	it6505_video_disable(it6505);
1544 }
1545 
it6505_audio_input(struct it6505 * it6505)1546 static bool it6505_audio_input(struct it6505 *it6505)
1547 {
1548 	int reg05, regbe;
1549 
1550 	reg05 = it6505_read(it6505, REG_RESET_CTRL);
1551 	it6505_set_bits(it6505, REG_RESET_CTRL, AUDIO_RESET, 0x00);
1552 	usleep_range(3000, 4000);
1553 	regbe = it6505_read(it6505, REG_AUDIO_INPUT_FREQ);
1554 	it6505_write(it6505, REG_RESET_CTRL, reg05);
1555 
1556 	return regbe != 0xFF;
1557 }
1558 
it6505_setup_audio_channel_status(struct it6505 * it6505)1559 static void it6505_setup_audio_channel_status(struct it6505 *it6505)
1560 {
1561 	enum it6505_audio_sample_rate sample_rate = it6505->audio.sample_rate;
1562 	u8 audio_word_length_map[] = { 0x02, 0x04, 0x03, 0x0B };
1563 
1564 	/* Channel Status */
1565 	it6505_write(it6505, REG_IEC958_STS0, it6505->audio.type << 1);
1566 	it6505_write(it6505, REG_IEC958_STS1, 0x00);
1567 	it6505_write(it6505, REG_IEC958_STS2, 0x00);
1568 	it6505_write(it6505, REG_IEC958_STS3, sample_rate);
1569 	it6505_write(it6505, REG_IEC958_STS4, (~sample_rate << 4) |
1570 		     audio_word_length_map[it6505->audio.word_length]);
1571 }
1572 
it6505_setup_audio_format(struct it6505 * it6505)1573 static void it6505_setup_audio_format(struct it6505 *it6505)
1574 {
1575 	/* I2S MODE */
1576 	it6505_write(it6505, REG_AUDIO_FMT,
1577 		     (it6505->audio.word_length << 5) |
1578 		     (it6505->audio.i2s_data_sequence << 4) |
1579 		     (it6505->audio.i2s_ws_channel << 3) |
1580 		     (it6505->audio.i2s_data_delay << 2) |
1581 		     (it6505->audio.i2s_justified << 1) |
1582 		     it6505->audio.i2s_input_format);
1583 	if (it6505->audio.select == SPDIF) {
1584 		it6505_write(it6505, REG_AUDIO_FIFO_SEL, 0x00);
1585 		/* 0x30 = 128*FS */
1586 		it6505_set_bits(it6505, REG_AUX_OPT, 0xF0, 0x30);
1587 	} else {
1588 		it6505_write(it6505, REG_AUDIO_FIFO_SEL, 0xE4);
1589 	}
1590 
1591 	it6505_write(it6505, REG_AUDIO_CTRL0, 0x20);
1592 	it6505_write(it6505, REG_AUDIO_CTRL1, 0x00);
1593 }
1594 
it6505_enable_audio_source(struct it6505 * it6505)1595 static void it6505_enable_audio_source(struct it6505 *it6505)
1596 {
1597 	unsigned int audio_source_count;
1598 
1599 	audio_source_count = BIT(DIV_ROUND_UP(it6505->audio.channel_count, 2))
1600 				 - 1;
1601 
1602 	audio_source_count |= it6505->audio.select << 4;
1603 
1604 	it6505_write(it6505, REG_AUDIO_SRC_CTRL, audio_source_count);
1605 }
1606 
it6505_enable_audio_infoframe(struct it6505 * it6505)1607 static void it6505_enable_audio_infoframe(struct it6505 *it6505)
1608 {
1609 	struct device *dev = it6505->dev;
1610 	u8 audio_info_ca[] = { 0x00, 0x00, 0x01, 0x03, 0x07, 0x0B, 0x0F, 0x1F };
1611 
1612 	DRM_DEV_DEBUG_DRIVER(dev, "infoframe channel_allocation:0x%02x",
1613 			     audio_info_ca[it6505->audio.channel_count - 1]);
1614 
1615 	it6505_write(it6505, REG_AUD_INFOFRAM_DB1, it6505->audio.channel_count
1616 		     - 1);
1617 	it6505_write(it6505, REG_AUD_INFOFRAM_DB2, 0x00);
1618 	it6505_write(it6505, REG_AUD_INFOFRAM_DB3,
1619 		     audio_info_ca[it6505->audio.channel_count - 1]);
1620 	it6505_write(it6505, REG_AUD_INFOFRAM_DB4, 0x00);
1621 	it6505_write(it6505, REG_AUD_INFOFRAM_SUM, 0x00);
1622 
1623 	/* Enable Audio InfoFrame */
1624 	it6505_set_bits(it6505, REG_INFOFRAME_CTRL, EN_AUD_CTRL_PKT,
1625 			EN_AUD_CTRL_PKT);
1626 }
1627 
it6505_disable_audio(struct it6505 * it6505)1628 static void it6505_disable_audio(struct it6505 *it6505)
1629 {
1630 	it6505_set_bits(it6505, REG_DATA_MUTE_CTRL, EN_AUD_MUTE, EN_AUD_MUTE);
1631 	it6505_set_bits(it6505, REG_AUDIO_SRC_CTRL, M_AUDIO_I2S_EN, 0x00);
1632 	it6505_set_bits(it6505, REG_INFOFRAME_CTRL, EN_AUD_CTRL_PKT, 0x00);
1633 	it6505_set_bits(it6505, REG_RESET_CTRL, AUDIO_RESET, AUDIO_RESET);
1634 }
1635 
it6505_enable_audio(struct it6505 * it6505)1636 static void it6505_enable_audio(struct it6505 *it6505)
1637 {
1638 	struct device *dev = it6505->dev;
1639 	int regbe;
1640 
1641 	DRM_DEV_DEBUG_DRIVER(dev, "start");
1642 	it6505_disable_audio(it6505);
1643 
1644 	it6505_setup_audio_channel_status(it6505);
1645 	it6505_setup_audio_format(it6505);
1646 	it6505_enable_audio_source(it6505);
1647 	it6505_enable_audio_infoframe(it6505);
1648 
1649 	it6505_write(it6505, REG_AUDIO_N_0_7, 0x00);
1650 	it6505_write(it6505, REG_AUDIO_N_8_15, 0x80);
1651 	it6505_write(it6505, REG_AUDIO_N_16_23, 0x00);
1652 
1653 	it6505_set_bits(it6505, REG_AUDIO_SRC_CTRL, AUDIO_FIFO_RESET,
1654 			AUDIO_FIFO_RESET);
1655 	it6505_set_bits(it6505, REG_AUDIO_SRC_CTRL, AUDIO_FIFO_RESET, 0x00);
1656 	it6505_set_bits(it6505, REG_RESET_CTRL, AUDIO_RESET, 0x00);
1657 	regbe = it6505_read(it6505, REG_AUDIO_INPUT_FREQ);
1658 	DRM_DEV_DEBUG_DRIVER(dev, "regbe:0x%02x audio input fs: %d.%d kHz",
1659 			     regbe, 6750 / regbe, (6750 % regbe) * 10 / regbe);
1660 	it6505_set_bits(it6505, REG_DATA_MUTE_CTRL, EN_AUD_MUTE, 0x00);
1661 }
1662 
it6505_use_step_train_check(struct it6505 * it6505)1663 static bool it6505_use_step_train_check(struct it6505 *it6505)
1664 {
1665 	if (it6505->link.revision >= 0x12)
1666 		return it6505->dpcd[DP_TRAINING_AUX_RD_INTERVAL] >= 0x01;
1667 
1668 	return true;
1669 }
1670 
it6505_parse_link_capabilities(struct it6505 * it6505)1671 static void it6505_parse_link_capabilities(struct it6505 *it6505)
1672 {
1673 	struct device *dev = it6505->dev;
1674 	struct it6505_drm_dp_link *link = &it6505->link;
1675 	int bcaps;
1676 
1677 	if (it6505->dpcd[0] == 0) {
1678 		dev_err(dev, "DPCD is not initialized");
1679 		return;
1680 	}
1681 
1682 	memset(link, 0, sizeof(*link));
1683 
1684 	link->revision = it6505->dpcd[0];
1685 	link->rate = drm_dp_bw_code_to_link_rate(it6505->dpcd[1]);
1686 	link->num_lanes = it6505->dpcd[2] & DP_MAX_LANE_COUNT_MASK;
1687 
1688 	if (it6505->dpcd[2] & DP_ENHANCED_FRAME_CAP)
1689 		link->capabilities = DP_ENHANCED_FRAME_CAP;
1690 
1691 	DRM_DEV_DEBUG_DRIVER(dev, "DPCD Rev.: %d.%d",
1692 			     link->revision >> 4, link->revision & 0x0F);
1693 
1694 	DRM_DEV_DEBUG_DRIVER(dev, "Sink max link rate: %d.%02d Gbps per lane",
1695 			     link->rate / 100000, link->rate / 1000 % 100);
1696 
1697 	it6505->link_rate_bw_code = drm_dp_link_rate_to_bw_code(link->rate);
1698 	DRM_DEV_DEBUG_DRIVER(dev, "link rate bw code:0x%02x",
1699 			     it6505->link_rate_bw_code);
1700 	it6505->link_rate_bw_code = min_t(int, it6505->link_rate_bw_code,
1701 					  MAX_LINK_RATE);
1702 
1703 	it6505->lane_count = link->num_lanes;
1704 	DRM_DEV_DEBUG_DRIVER(dev, "Sink support %d lanes training",
1705 			     it6505->lane_count);
1706 	it6505->lane_count = min_t(int, it6505->lane_count,
1707 				   it6505->max_lane_count);
1708 
1709 	it6505->branch_device = drm_dp_is_branch(it6505->dpcd);
1710 	DRM_DEV_DEBUG_DRIVER(dev, "Sink %sbranch device",
1711 			     it6505->branch_device ? "" : "Not ");
1712 
1713 	it6505->enable_enhanced_frame = link->capabilities;
1714 	DRM_DEV_DEBUG_DRIVER(dev, "Sink %sSupport Enhanced Framing",
1715 			     it6505->enable_enhanced_frame ? "" : "Not ");
1716 
1717 	it6505->enable_ssc = (it6505->dpcd[DP_MAX_DOWNSPREAD] &
1718 				DP_MAX_DOWNSPREAD_0_5);
1719 	DRM_DEV_DEBUG_DRIVER(dev, "Maximum Down-Spread: %s, %ssupport SSC!",
1720 			     it6505->enable_ssc ? "0.5" : "0",
1721 			     it6505->enable_ssc ? "" : "Not ");
1722 
1723 	it6505->step_train = it6505_use_step_train_check(it6505);
1724 	if (it6505->step_train)
1725 		DRM_DEV_DEBUG_DRIVER(dev, "auto train fail, will step train");
1726 
1727 	bcaps = it6505_dpcd_read(it6505, DP_AUX_HDCP_BCAPS);
1728 	DRM_DEV_DEBUG_DRIVER(dev, "bcaps:0x%02x", bcaps);
1729 	if (bcaps & DP_BCAPS_HDCP_CAPABLE) {
1730 		it6505->is_repeater = (bcaps & DP_BCAPS_REPEATER_PRESENT);
1731 		DRM_DEV_DEBUG_DRIVER(dev, "Support HDCP! Downstream is %s!",
1732 				     it6505->is_repeater ? "repeater" :
1733 				     "receiver");
1734 	} else {
1735 		DRM_DEV_DEBUG_DRIVER(dev, "Sink not support HDCP!");
1736 		it6505->hdcp_desired = false;
1737 	}
1738 	DRM_DEV_DEBUG_DRIVER(dev, "HDCP %s",
1739 			     it6505->hdcp_desired ? "desired" : "undesired");
1740 }
1741 
it6505_setup_ssc(struct it6505 * it6505)1742 static void it6505_setup_ssc(struct it6505 *it6505)
1743 {
1744 	it6505_set_bits(it6505, REG_TRAIN_CTRL0, SPREAD_AMP_5,
1745 			it6505->enable_ssc ? SPREAD_AMP_5 : 0x00);
1746 	if (it6505->enable_ssc) {
1747 		it6505_write(it6505, REG_SSC_CTRL0, 0x9E);
1748 		it6505_write(it6505, REG_SSC_CTRL1, 0x1C);
1749 		it6505_write(it6505, REG_SSC_CTRL2, 0x42);
1750 		it6505_write(it6505, REG_SP_CTRL0, 0x07);
1751 		it6505_write(it6505, REG_IP_CTRL1, 0x29);
1752 		it6505_write(it6505, REG_IP_CTRL2, 0x03);
1753 		/* Stamp Interrupt Step */
1754 		it6505_set_bits(it6505, REG_TIME_STMP_CTRL, M_STAMP_STEP,
1755 				0x10);
1756 		it6505_dpcd_write(it6505, DP_DOWNSPREAD_CTRL,
1757 				  DP_SPREAD_AMP_0_5);
1758 	} else {
1759 		it6505_dpcd_write(it6505, DP_DOWNSPREAD_CTRL, 0x00);
1760 		it6505_set_bits(it6505, REG_TIME_STMP_CTRL, M_STAMP_STEP,
1761 				0x00);
1762 	}
1763 }
1764 
it6505_link_rate_setup(struct it6505 * it6505)1765 static inline void it6505_link_rate_setup(struct it6505 *it6505)
1766 {
1767 	it6505_set_bits(it6505, REG_TRAIN_CTRL0, FORCE_LBR,
1768 			(it6505->link_rate_bw_code == RBR) ? FORCE_LBR : 0x00);
1769 	it6505_set_bits(it6505, REG_LINK_DRV, DRV_HS,
1770 			(it6505->link_rate_bw_code == RBR) ? 0x00 : DRV_HS);
1771 }
1772 
it6505_lane_count_setup(struct it6505 * it6505)1773 static void it6505_lane_count_setup(struct it6505 *it6505)
1774 {
1775 	it6505_get_extcon_property(it6505);
1776 	it6505_set_bits(it6505, REG_TRAIN_CTRL0, LANE_SWAP,
1777 			it6505->lane_swap ? LANE_SWAP : 0x00);
1778 	it6505_set_bits(it6505, REG_TRAIN_CTRL0, LANE_COUNT_MASK,
1779 			(it6505->lane_count - 1) << 1);
1780 }
1781 
it6505_link_training_setup(struct it6505 * it6505)1782 static void it6505_link_training_setup(struct it6505 *it6505)
1783 {
1784 	struct device *dev = it6505->dev;
1785 
1786 	if (it6505->enable_enhanced_frame)
1787 		it6505_set_bits(it6505, REG_DATA_MUTE_CTRL,
1788 				ENABLE_ENHANCED_FRAME, ENABLE_ENHANCED_FRAME);
1789 
1790 	it6505_link_rate_setup(it6505);
1791 	it6505_lane_count_setup(it6505);
1792 	it6505_setup_ssc(it6505);
1793 	DRM_DEV_DEBUG_DRIVER(dev,
1794 			     "%s, %d lanes, %sable ssc, %sable enhanced frame",
1795 			     it6505->link_rate_bw_code != RBR ? "HBR" : "RBR",
1796 			     it6505->lane_count,
1797 			     it6505->enable_ssc ? "en" : "dis",
1798 			     it6505->enable_enhanced_frame ? "en" : "dis");
1799 }
1800 
it6505_link_start_auto_train(struct it6505 * it6505)1801 static bool it6505_link_start_auto_train(struct it6505 *it6505)
1802 {
1803 	int timeout = 500, link_training_state;
1804 	bool state = false;
1805 
1806 	mutex_lock(&it6505->aux_lock);
1807 	it6505_set_bits(it6505, REG_TRAIN_CTRL0,
1808 			FORCE_CR_DONE | FORCE_EQ_DONE, 0x00);
1809 	it6505_write(it6505, REG_TRAIN_CTRL1, FORCE_RETRAIN);
1810 	it6505_write(it6505, REG_TRAIN_CTRL1, AUTO_TRAIN);
1811 
1812 	while (timeout > 0) {
1813 		usleep_range(1000, 2000);
1814 		link_training_state = it6505_read(it6505, REG_LINK_TRAIN_STS);
1815 
1816 		if (link_training_state > 0 &&
1817 		    (link_training_state & LINK_STATE_NORP)) {
1818 			state = true;
1819 			goto unlock;
1820 		}
1821 
1822 		timeout--;
1823 	}
1824 unlock:
1825 	mutex_unlock(&it6505->aux_lock);
1826 
1827 	return state;
1828 }
1829 
it6505_drm_dp_link_configure(struct it6505 * it6505)1830 static int it6505_drm_dp_link_configure(struct it6505 *it6505)
1831 {
1832 	u8 values[2];
1833 	int err;
1834 	struct drm_dp_aux *aux = &it6505->aux;
1835 
1836 	values[0] = it6505->link_rate_bw_code;
1837 	values[1] = it6505->lane_count;
1838 
1839 	if (it6505->enable_enhanced_frame)
1840 		values[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
1841 
1842 	err = drm_dp_dpcd_write(aux, DP_LINK_BW_SET, values, sizeof(values));
1843 	if (err < 0)
1844 		return err;
1845 
1846 	return 0;
1847 }
1848 
it6505_check_voltage_swing_max(u8 lane_voltage_swing_pre_emphasis)1849 static bool it6505_check_voltage_swing_max(u8 lane_voltage_swing_pre_emphasis)
1850 {
1851 	return ((lane_voltage_swing_pre_emphasis & 0x03) == MAX_CR_LEVEL);
1852 }
1853 
it6505_check_pre_emphasis_max(u8 lane_voltage_swing_pre_emphasis)1854 static bool it6505_check_pre_emphasis_max(u8 lane_voltage_swing_pre_emphasis)
1855 {
1856 	return ((lane_voltage_swing_pre_emphasis & 0x03) == MAX_EQ_LEVEL);
1857 }
1858 
it6505_check_max_voltage_swing_reached(u8 * lane_voltage_swing,u8 lane_count)1859 static bool it6505_check_max_voltage_swing_reached(u8 *lane_voltage_swing,
1860 						   u8 lane_count)
1861 {
1862 	u8 i;
1863 
1864 	for (i = 0; i < lane_count; i++) {
1865 		if (lane_voltage_swing[i] & DP_TRAIN_MAX_SWING_REACHED)
1866 			return true;
1867 	}
1868 
1869 	return false;
1870 }
1871 
1872 static bool
step_train_lane_voltage_para_set(struct it6505 * it6505,struct it6505_step_train_para * lane_voltage_pre_emphasis,u8 * lane_voltage_pre_emphasis_set)1873 step_train_lane_voltage_para_set(struct it6505 *it6505,
1874 				 struct it6505_step_train_para
1875 				 *lane_voltage_pre_emphasis,
1876 				 u8 *lane_voltage_pre_emphasis_set)
1877 {
1878 	u8 *voltage_swing = lane_voltage_pre_emphasis->voltage_swing;
1879 	u8 *pre_emphasis = lane_voltage_pre_emphasis->pre_emphasis;
1880 	u8 i;
1881 
1882 	for (i = 0; i < it6505->lane_count; i++) {
1883 		voltage_swing[i] &= 0x03;
1884 		lane_voltage_pre_emphasis_set[i] = voltage_swing[i];
1885 		if (it6505_check_voltage_swing_max(voltage_swing[i]))
1886 			lane_voltage_pre_emphasis_set[i] |=
1887 				DP_TRAIN_MAX_SWING_REACHED;
1888 
1889 		pre_emphasis[i] &= 0x03;
1890 		lane_voltage_pre_emphasis_set[i] |= pre_emphasis[i]
1891 			<< DP_TRAIN_PRE_EMPHASIS_SHIFT;
1892 		if (it6505_check_pre_emphasis_max(pre_emphasis[i]))
1893 			lane_voltage_pre_emphasis_set[i] |=
1894 				DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
1895 		it6505_dpcd_write(it6505, DP_TRAINING_LANE0_SET + i,
1896 				  lane_voltage_pre_emphasis_set[i]);
1897 
1898 		if (lane_voltage_pre_emphasis_set[i] !=
1899 		    it6505_dpcd_read(it6505, DP_TRAINING_LANE0_SET + i))
1900 			return false;
1901 	}
1902 
1903 	return true;
1904 }
1905 
1906 static bool
it6505_step_cr_train(struct it6505 * it6505,struct it6505_step_train_para * lane_voltage_pre_emphasis)1907 it6505_step_cr_train(struct it6505 *it6505,
1908 		     struct it6505_step_train_para *lane_voltage_pre_emphasis)
1909 {
1910 	u8 loop_count = 0, i = 0, j;
1911 	u8 link_status[DP_LINK_STATUS_SIZE] = { 0 };
1912 	u8 lane_level_config[MAX_LANE_COUNT] = { 0 };
1913 	int pre_emphasis_adjust = -1, voltage_swing_adjust = -1;
1914 	const struct drm_dp_aux *aux = &it6505->aux;
1915 
1916 	it6505_dpcd_write(it6505, DP_DOWNSPREAD_CTRL,
1917 			  it6505->enable_ssc ? DP_SPREAD_AMP_0_5 : 0x00);
1918 	it6505_dpcd_write(it6505, DP_TRAINING_PATTERN_SET,
1919 			  DP_TRAINING_PATTERN_1);
1920 
1921 	while (loop_count < 5 && i < 10) {
1922 		i++;
1923 		if (!step_train_lane_voltage_para_set(it6505,
1924 						      lane_voltage_pre_emphasis,
1925 						      lane_level_config))
1926 			continue;
1927 		drm_dp_link_train_clock_recovery_delay(aux, it6505->dpcd);
1928 		drm_dp_dpcd_read_link_status(&it6505->aux, link_status);
1929 
1930 		if (drm_dp_clock_recovery_ok(link_status, it6505->lane_count)) {
1931 			it6505_set_bits(it6505, REG_TRAIN_CTRL0, FORCE_CR_DONE,
1932 					FORCE_CR_DONE);
1933 			return true;
1934 		}
1935 		DRM_DEV_DEBUG_DRIVER(it6505->dev, "cr not done");
1936 
1937 		if (it6505_check_max_voltage_swing_reached(lane_level_config,
1938 							   it6505->lane_count))
1939 			goto cr_train_fail;
1940 
1941 		for (j = 0; j < it6505->lane_count; j++) {
1942 			lane_voltage_pre_emphasis->voltage_swing[j] =
1943 				drm_dp_get_adjust_request_voltage(link_status,
1944 								  j) >>
1945 				DP_TRAIN_VOLTAGE_SWING_SHIFT;
1946 			lane_voltage_pre_emphasis->pre_emphasis[j] =
1947 			drm_dp_get_adjust_request_pre_emphasis(link_status,
1948 							       j) >>
1949 					DP_TRAIN_PRE_EMPHASIS_SHIFT;
1950 			if (voltage_swing_adjust ==
1951 			     lane_voltage_pre_emphasis->voltage_swing[j] &&
1952 			    pre_emphasis_adjust ==
1953 			     lane_voltage_pre_emphasis->pre_emphasis[j]) {
1954 				loop_count++;
1955 				continue;
1956 			}
1957 
1958 			voltage_swing_adjust =
1959 				lane_voltage_pre_emphasis->voltage_swing[j];
1960 			pre_emphasis_adjust =
1961 				lane_voltage_pre_emphasis->pre_emphasis[j];
1962 			loop_count = 0;
1963 
1964 			if (voltage_swing_adjust + pre_emphasis_adjust >
1965 			    MAX_EQ_LEVEL)
1966 				lane_voltage_pre_emphasis->voltage_swing[j] =
1967 					MAX_EQ_LEVEL -
1968 					lane_voltage_pre_emphasis
1969 						->pre_emphasis[j];
1970 		}
1971 	}
1972 
1973 cr_train_fail:
1974 	it6505_dpcd_write(it6505, DP_TRAINING_PATTERN_SET,
1975 			  DP_TRAINING_PATTERN_DISABLE);
1976 
1977 	return false;
1978 }
1979 
1980 static bool
it6505_step_eq_train(struct it6505 * it6505,struct it6505_step_train_para * lane_voltage_pre_emphasis)1981 it6505_step_eq_train(struct it6505 *it6505,
1982 		     struct it6505_step_train_para *lane_voltage_pre_emphasis)
1983 {
1984 	u8 loop_count = 0, i, link_status[DP_LINK_STATUS_SIZE] = { 0 };
1985 	u8 lane_level_config[MAX_LANE_COUNT] = { 0 };
1986 	const struct drm_dp_aux *aux = &it6505->aux;
1987 
1988 	it6505_dpcd_write(it6505, DP_TRAINING_PATTERN_SET,
1989 			  DP_TRAINING_PATTERN_2);
1990 
1991 	while (loop_count < 6) {
1992 		loop_count++;
1993 
1994 		if (!step_train_lane_voltage_para_set(it6505,
1995 						      lane_voltage_pre_emphasis,
1996 						      lane_level_config))
1997 			continue;
1998 
1999 		drm_dp_link_train_channel_eq_delay(aux, it6505->dpcd);
2000 		drm_dp_dpcd_read_link_status(&it6505->aux, link_status);
2001 
2002 		if (!drm_dp_clock_recovery_ok(link_status, it6505->lane_count))
2003 			goto eq_train_fail;
2004 
2005 		if (drm_dp_channel_eq_ok(link_status, it6505->lane_count)) {
2006 			it6505_dpcd_write(it6505, DP_TRAINING_PATTERN_SET,
2007 					  DP_TRAINING_PATTERN_DISABLE);
2008 			it6505_set_bits(it6505, REG_TRAIN_CTRL0, FORCE_EQ_DONE,
2009 					FORCE_EQ_DONE);
2010 			return true;
2011 		}
2012 		DRM_DEV_DEBUG_DRIVER(it6505->dev, "eq not done");
2013 
2014 		for (i = 0; i < it6505->lane_count; i++) {
2015 			lane_voltage_pre_emphasis->voltage_swing[i] =
2016 				drm_dp_get_adjust_request_voltage(link_status,
2017 								  i) >>
2018 				DP_TRAIN_VOLTAGE_SWING_SHIFT;
2019 			lane_voltage_pre_emphasis->pre_emphasis[i] =
2020 			drm_dp_get_adjust_request_pre_emphasis(link_status,
2021 							       i) >>
2022 					DP_TRAIN_PRE_EMPHASIS_SHIFT;
2023 
2024 			if (lane_voltage_pre_emphasis->voltage_swing[i] +
2025 				    lane_voltage_pre_emphasis->pre_emphasis[i] >
2026 			    MAX_EQ_LEVEL)
2027 				lane_voltage_pre_emphasis->voltage_swing[i] =
2028 					0x03 - lane_voltage_pre_emphasis
2029 						       ->pre_emphasis[i];
2030 		}
2031 	}
2032 
2033 eq_train_fail:
2034 	it6505_dpcd_write(it6505, DP_TRAINING_PATTERN_SET,
2035 			  DP_TRAINING_PATTERN_DISABLE);
2036 	return false;
2037 }
2038 
it6505_link_start_step_train(struct it6505 * it6505)2039 static bool it6505_link_start_step_train(struct it6505 *it6505)
2040 {
2041 	int err;
2042 	struct it6505_step_train_para lane_voltage_pre_emphasis = {
2043 		.voltage_swing = { 0 },
2044 		.pre_emphasis = { 0 },
2045 	};
2046 
2047 	DRM_DEV_DEBUG_DRIVER(it6505->dev, "start");
2048 	err = it6505_drm_dp_link_configure(it6505);
2049 
2050 	if (err < 0)
2051 		return false;
2052 	if (!it6505_step_cr_train(it6505, &lane_voltage_pre_emphasis))
2053 		return false;
2054 	if (!it6505_step_eq_train(it6505, &lane_voltage_pre_emphasis))
2055 		return false;
2056 	return true;
2057 }
2058 
it6505_get_video_status(struct it6505 * it6505)2059 static bool it6505_get_video_status(struct it6505 *it6505)
2060 {
2061 	int reg_0d;
2062 
2063 	reg_0d = it6505_read(it6505, REG_SYSTEM_STS);
2064 
2065 	if (reg_0d < 0)
2066 		return false;
2067 
2068 	return reg_0d & VIDEO_STB;
2069 }
2070 
it6505_reset_hdcp(struct it6505 * it6505)2071 static void it6505_reset_hdcp(struct it6505 *it6505)
2072 {
2073 	it6505->hdcp_status = HDCP_AUTH_IDLE;
2074 	/* Disable CP_Desired */
2075 	it6505_set_bits(it6505, REG_HDCP_CTRL1, HDCP_CP_ENABLE, 0x00);
2076 	it6505_set_bits(it6505, REG_RESET_CTRL, HDCP_RESET, HDCP_RESET);
2077 }
2078 
it6505_start_hdcp(struct it6505 * it6505)2079 static void it6505_start_hdcp(struct it6505 *it6505)
2080 {
2081 	struct device *dev = it6505->dev;
2082 
2083 	DRM_DEV_DEBUG_DRIVER(dev, "start");
2084 	it6505_reset_hdcp(it6505);
2085 	queue_delayed_work(system_wq, &it6505->hdcp_work,
2086 			   msecs_to_jiffies(2400));
2087 }
2088 
it6505_stop_hdcp(struct it6505 * it6505)2089 static void it6505_stop_hdcp(struct it6505 *it6505)
2090 {
2091 	it6505_reset_hdcp(it6505);
2092 	cancel_delayed_work(&it6505->hdcp_work);
2093 }
2094 
it6505_hdcp_is_ksv_valid(u8 * ksv)2095 static bool it6505_hdcp_is_ksv_valid(u8 *ksv)
2096 {
2097 	int i, ones = 0;
2098 
2099 	/* KSV has 20 1's and 20 0's */
2100 	for (i = 0; i < DRM_HDCP_KSV_LEN; i++)
2101 		ones += hweight8(ksv[i]);
2102 	if (ones != 20)
2103 		return false;
2104 	return true;
2105 }
2106 
it6505_hdcp_part1_auth(struct it6505 * it6505)2107 static void it6505_hdcp_part1_auth(struct it6505 *it6505)
2108 {
2109 	struct device *dev = it6505->dev;
2110 	u8 hdcp_bcaps;
2111 
2112 	it6505_set_bits(it6505, REG_RESET_CTRL, HDCP_RESET, 0x00);
2113 	/* Disable CP_Desired */
2114 	it6505_set_bits(it6505, REG_HDCP_CTRL1, HDCP_CP_ENABLE, 0x00);
2115 
2116 	usleep_range(1000, 1500);
2117 	hdcp_bcaps = it6505_dpcd_read(it6505, DP_AUX_HDCP_BCAPS);
2118 	DRM_DEV_DEBUG_DRIVER(dev, "DPCD[0x68028]: 0x%02x",
2119 			     hdcp_bcaps);
2120 
2121 	if (!hdcp_bcaps)
2122 		return;
2123 
2124 	/* clear the repeater List Chk Done and fail bit */
2125 	it6505_set_bits(it6505, REG_HDCP_TRIGGER,
2126 			HDCP_TRIGGER_KSV_DONE | HDCP_TRIGGER_KSV_FAIL,
2127 			0x00);
2128 
2129 	/* Enable An Generator */
2130 	it6505_set_bits(it6505, REG_HDCP_CTRL2, HDCP_AN_GEN, HDCP_AN_GEN);
2131 	/* delay1ms(10);*/
2132 	usleep_range(10000, 15000);
2133 	/* Stop An Generator */
2134 	it6505_set_bits(it6505, REG_HDCP_CTRL2, HDCP_AN_GEN, 0x00);
2135 
2136 	it6505_set_bits(it6505, REG_HDCP_CTRL1, HDCP_CP_ENABLE, HDCP_CP_ENABLE);
2137 
2138 	it6505_set_bits(it6505, REG_HDCP_TRIGGER, HDCP_TRIGGER_START,
2139 			HDCP_TRIGGER_START);
2140 
2141 	it6505->hdcp_status = HDCP_AUTH_GOING;
2142 }
2143 
it6505_sha1_digest(struct it6505 * it6505,u8 * sha1_input,unsigned int size,u8 * output_av)2144 static int it6505_sha1_digest(struct it6505 *it6505, u8 *sha1_input,
2145 			      unsigned int size, u8 *output_av)
2146 {
2147 	struct shash_desc *desc;
2148 	struct crypto_shash *tfm;
2149 	int err;
2150 	struct device *dev = it6505->dev;
2151 
2152 	tfm = crypto_alloc_shash("sha1", 0, 0);
2153 	if (IS_ERR(tfm)) {
2154 		dev_err(dev, "crypto_alloc_shash sha1 failed");
2155 		return PTR_ERR(tfm);
2156 	}
2157 	desc = kzalloc(sizeof(*desc) + crypto_shash_descsize(tfm), GFP_KERNEL);
2158 	if (!desc) {
2159 		crypto_free_shash(tfm);
2160 		return -ENOMEM;
2161 	}
2162 
2163 	desc->tfm = tfm;
2164 	err = crypto_shash_digest(desc, sha1_input, size, output_av);
2165 	if (err)
2166 		dev_err(dev, "crypto_shash_digest sha1 failed");
2167 
2168 	crypto_free_shash(tfm);
2169 	kfree(desc);
2170 	return err;
2171 }
2172 
it6505_setup_sha1_input(struct it6505 * it6505,u8 * sha1_input)2173 static int it6505_setup_sha1_input(struct it6505 *it6505, u8 *sha1_input)
2174 {
2175 	struct device *dev = it6505->dev;
2176 	u8 binfo[2];
2177 	int down_stream_count, err, msg_count = 0;
2178 
2179 	err = it6505_get_dpcd(it6505, DP_AUX_HDCP_BINFO, binfo,
2180 			      ARRAY_SIZE(binfo));
2181 
2182 	if (err < 0) {
2183 		dev_err(dev, "Read binfo value Fail");
2184 		return err;
2185 	}
2186 
2187 	down_stream_count = binfo[0] & 0x7F;
2188 	DRM_DEV_DEBUG_DRIVER(dev, "binfo:0x%*ph", (int)ARRAY_SIZE(binfo),
2189 			     binfo);
2190 
2191 	if ((binfo[0] & BIT(7)) || (binfo[1] & BIT(3))) {
2192 		dev_err(dev, "HDCP max cascade device exceed");
2193 		return 0;
2194 	}
2195 
2196 	if (!down_stream_count ||
2197 	    down_stream_count > MAX_HDCP_DOWN_STREAM_COUNT) {
2198 		dev_err(dev, "HDCP down stream count Error %d",
2199 			down_stream_count);
2200 		return 0;
2201 	}
2202 	err =  it6505_get_ksvlist(it6505, sha1_input, down_stream_count * 5);
2203 	if (err < 0)
2204 		return err;
2205 
2206 	msg_count += down_stream_count * 5;
2207 
2208 	it6505->hdcp_down_stream_count = down_stream_count;
2209 	sha1_input[msg_count++] = binfo[0];
2210 	sha1_input[msg_count++] = binfo[1];
2211 
2212 	it6505_set_bits(it6505, REG_HDCP_CTRL2, HDCP_EN_M0_READ,
2213 			HDCP_EN_M0_READ);
2214 
2215 	err = regmap_bulk_read(it6505->regmap, REG_M0_0_7,
2216 			       sha1_input + msg_count, 8);
2217 
2218 	it6505_set_bits(it6505, REG_HDCP_CTRL2, HDCP_EN_M0_READ, 0x00);
2219 
2220 	if (err < 0) {
2221 		dev_err(dev, " Warning, Read M value Fail");
2222 		return err;
2223 	}
2224 
2225 	msg_count += 8;
2226 
2227 	return msg_count;
2228 }
2229 
it6505_hdcp_part2_ksvlist_check(struct it6505 * it6505)2230 static bool it6505_hdcp_part2_ksvlist_check(struct it6505 *it6505)
2231 {
2232 	struct device *dev = it6505->dev;
2233 	u8 av[5][4], bv[5][4];
2234 	int i, err, retry;
2235 
2236 	i = it6505_setup_sha1_input(it6505, it6505->sha1_input);
2237 	if (i <= 0) {
2238 		dev_err(dev, "SHA-1 Input length error %d", i);
2239 		return false;
2240 	}
2241 
2242 	it6505_sha1_digest(it6505, it6505->sha1_input, i, (u8 *)av);
2243 	/*1B-05 V' must retry 3 times */
2244 	for (retry = 0; retry < 3; retry++) {
2245 		err = it6505_get_dpcd(it6505, DP_AUX_HDCP_V_PRIME(0), (u8 *)bv,
2246 				      sizeof(bv));
2247 
2248 		if (err < 0) {
2249 			dev_err(dev, "Read V' value Fail %d", retry);
2250 			continue;
2251 		}
2252 
2253 		for (i = 0; i < 5; i++) {
2254 			if (bv[i][3] != av[i][0] || bv[i][2] != av[i][1] ||
2255 			    av[i][1] != av[i][2] || bv[i][0] != av[i][3])
2256 				break;
2257 
2258 			DRM_DEV_DEBUG_DRIVER(dev, "V' all match!! %d, %d", retry, i);
2259 			return true;
2260 		}
2261 	}
2262 
2263 	DRM_DEV_DEBUG_DRIVER(dev, "V' NOT match!! %d", retry);
2264 	return false;
2265 }
2266 
it6505_hdcp_wait_ksv_list(struct work_struct * work)2267 static void it6505_hdcp_wait_ksv_list(struct work_struct *work)
2268 {
2269 	struct it6505 *it6505 = container_of(work, struct it6505,
2270 					     hdcp_wait_ksv_list);
2271 	struct device *dev = it6505->dev;
2272 	u8 bstatus;
2273 	bool ksv_list_check;
2274 	/* 1B-04 wait ksv list for 5s */
2275 	unsigned long timeout = jiffies +
2276 				msecs_to_jiffies(5000) + 1;
2277 
2278 	for (;;) {
2279 		if (!it6505_get_sink_hpd_status(it6505))
2280 			return;
2281 
2282 		bstatus = it6505_dpcd_read(it6505, DP_AUX_HDCP_BSTATUS);
2283 
2284 		if (bstatus & DP_BSTATUS_READY)
2285 			break;
2286 
2287 		if (time_after(jiffies, timeout)) {
2288 			DRM_DEV_DEBUG_DRIVER(dev, "KSV list wait timeout");
2289 			goto timeout;
2290 		}
2291 
2292 		msleep(20);
2293 	}
2294 
2295 	ksv_list_check = it6505_hdcp_part2_ksvlist_check(it6505);
2296 	DRM_DEV_DEBUG_DRIVER(dev, "ksv list ready, ksv list check %s",
2297 			     ksv_list_check ? "pass" : "fail");
2298 
2299 	if (ksv_list_check)
2300 		return;
2301 
2302 timeout:
2303 	it6505_start_hdcp(it6505);
2304 }
2305 
it6505_hdcp_work(struct work_struct * work)2306 static void it6505_hdcp_work(struct work_struct *work)
2307 {
2308 	struct it6505 *it6505 = container_of(work, struct it6505,
2309 					     hdcp_work.work);
2310 	struct device *dev = it6505->dev;
2311 	int ret;
2312 	u8 link_status[DP_LINK_STATUS_SIZE] = { 0 };
2313 
2314 	DRM_DEV_DEBUG_DRIVER(dev, "start");
2315 
2316 	if (!it6505_get_sink_hpd_status(it6505))
2317 		return;
2318 
2319 	ret = drm_dp_dpcd_read_link_status(&it6505->aux, link_status);
2320 	DRM_DEV_DEBUG_DRIVER(dev, "ret: %d link_status: %*ph", ret,
2321 			     (int)sizeof(link_status), link_status);
2322 
2323 	if (ret < 0 || !drm_dp_channel_eq_ok(link_status, it6505->lane_count) ||
2324 	    !it6505_get_video_status(it6505)) {
2325 		DRM_DEV_DEBUG_DRIVER(dev, "link train not done or no video");
2326 		return;
2327 	}
2328 
2329 	ret = it6505_get_dpcd(it6505, DP_AUX_HDCP_BKSV, it6505->bksvs,
2330 			      ARRAY_SIZE(it6505->bksvs));
2331 	if (ret < 0) {
2332 		dev_err(dev, "fail to get bksv  ret: %d", ret);
2333 		it6505_set_bits(it6505, REG_HDCP_TRIGGER,
2334 				HDCP_TRIGGER_KSV_FAIL, HDCP_TRIGGER_KSV_FAIL);
2335 	}
2336 
2337 	DRM_DEV_DEBUG_DRIVER(dev, "bksv = 0x%*ph",
2338 			     (int)ARRAY_SIZE(it6505->bksvs), it6505->bksvs);
2339 
2340 	if (!it6505_hdcp_is_ksv_valid(it6505->bksvs)) {
2341 		dev_err(dev, "Display Port bksv not valid");
2342 		it6505_set_bits(it6505, REG_HDCP_TRIGGER,
2343 				HDCP_TRIGGER_KSV_FAIL, HDCP_TRIGGER_KSV_FAIL);
2344 	}
2345 
2346 	it6505_hdcp_part1_auth(it6505);
2347 }
2348 
it6505_show_hdcp_info(struct it6505 * it6505)2349 static void it6505_show_hdcp_info(struct it6505 *it6505)
2350 {
2351 	struct device *dev = it6505->dev;
2352 	int i;
2353 	u8 *sha1 = it6505->sha1_input;
2354 
2355 	DRM_DEV_DEBUG_DRIVER(dev, "hdcp_status: %d is_repeater: %d",
2356 			     it6505->hdcp_status, it6505->is_repeater);
2357 	DRM_DEV_DEBUG_DRIVER(dev, "bksv = 0x%*ph",
2358 			     (int)ARRAY_SIZE(it6505->bksvs), it6505->bksvs);
2359 
2360 	if (it6505->is_repeater) {
2361 		DRM_DEV_DEBUG_DRIVER(dev, "hdcp_down_stream_count: %d",
2362 				     it6505->hdcp_down_stream_count);
2363 		DRM_DEV_DEBUG_DRIVER(dev, "sha1_input: 0x%*ph",
2364 				     (int)ARRAY_SIZE(it6505->sha1_input),
2365 				     it6505->sha1_input);
2366 		for (i = 0; i < it6505->hdcp_down_stream_count; i++) {
2367 			DRM_DEV_DEBUG_DRIVER(dev, "KSV_%d = 0x%*ph", i,
2368 					     DRM_HDCP_KSV_LEN, sha1);
2369 			sha1 += DRM_HDCP_KSV_LEN;
2370 		}
2371 		DRM_DEV_DEBUG_DRIVER(dev, "binfo: 0x%2ph M0: 0x%8ph",
2372 				     sha1, sha1 + 2);
2373 	}
2374 }
2375 
it6505_stop_link_train(struct it6505 * it6505)2376 static void it6505_stop_link_train(struct it6505 *it6505)
2377 {
2378 	it6505->link_state = LINK_IDLE;
2379 	cancel_work_sync(&it6505->link_works);
2380 	it6505_write(it6505, REG_TRAIN_CTRL1, FORCE_RETRAIN);
2381 }
2382 
it6505_link_train_ok(struct it6505 * it6505)2383 static void it6505_link_train_ok(struct it6505 *it6505)
2384 {
2385 	struct device *dev = it6505->dev;
2386 
2387 	it6505->link_state = LINK_OK;
2388 	/* disalbe mute enable avi info frame */
2389 	it6505_set_bits(it6505, REG_DATA_MUTE_CTRL, EN_VID_MUTE, 0x00);
2390 	it6505_set_bits(it6505, REG_INFOFRAME_CTRL,
2391 			EN_VID_CTRL_PKT, EN_VID_CTRL_PKT);
2392 
2393 	if (it6505_audio_input(it6505)) {
2394 		DRM_DEV_DEBUG_DRIVER(dev, "Enable audio!");
2395 		it6505_enable_audio(it6505);
2396 	}
2397 
2398 	if (it6505->hdcp_desired)
2399 		it6505_start_hdcp(it6505);
2400 }
2401 
it6505_link_step_train_process(struct it6505 * it6505)2402 static void it6505_link_step_train_process(struct it6505 *it6505)
2403 {
2404 	struct device *dev = it6505->dev;
2405 	int ret, i, step_retry = 3;
2406 
2407 	DRM_DEV_DEBUG_DRIVER(dev, "Start step train");
2408 
2409 	if (it6505->sink_count == 0) {
2410 		DRM_DEV_DEBUG_DRIVER(dev, "it6505->sink_count:%d, force eq",
2411 				     it6505->sink_count);
2412 		it6505_set_bits(it6505,	REG_TRAIN_CTRL0, FORCE_EQ_DONE,
2413 				FORCE_EQ_DONE);
2414 		return;
2415 	}
2416 
2417 	if (!it6505->step_train) {
2418 		DRM_DEV_DEBUG_DRIVER(dev, "not support step train");
2419 		return;
2420 	}
2421 
2422 	/* step training start here */
2423 	for (i = 0; i < step_retry; i++) {
2424 		it6505_link_reset_step_train(it6505);
2425 		ret = it6505_link_start_step_train(it6505);
2426 		DRM_DEV_DEBUG_DRIVER(dev, "step train %s, retry:%d times",
2427 				     ret ? "pass" : "failed", i + 1);
2428 		if (ret) {
2429 			it6505_link_train_ok(it6505);
2430 			return;
2431 		}
2432 	}
2433 
2434 	DRM_DEV_DEBUG_DRIVER(dev, "training fail");
2435 	it6505->link_state = LINK_IDLE;
2436 	it6505_video_reset(it6505);
2437 }
2438 
it6505_link_training_work(struct work_struct * work)2439 static void it6505_link_training_work(struct work_struct *work)
2440 {
2441 	struct it6505 *it6505 = container_of(work, struct it6505, link_works);
2442 	struct device *dev = it6505->dev;
2443 	int ret;
2444 
2445 	DRM_DEV_DEBUG_DRIVER(dev, "it6505->sink_count: %d",
2446 			     it6505->sink_count);
2447 
2448 	if (!it6505_get_sink_hpd_status(it6505))
2449 		return;
2450 
2451 	it6505_link_training_setup(it6505);
2452 	it6505_reset_hdcp(it6505);
2453 	it6505_aux_reset(it6505);
2454 
2455 	if (it6505->auto_train_retry < 1) {
2456 		it6505_link_step_train_process(it6505);
2457 		return;
2458 	}
2459 
2460 	ret = it6505_link_start_auto_train(it6505);
2461 	DRM_DEV_DEBUG_DRIVER(dev, "auto train %s, auto_train_retry: %d",
2462 			     ret ? "pass" : "failed", it6505->auto_train_retry);
2463 
2464 	if (ret) {
2465 		it6505->auto_train_retry = AUTO_TRAIN_RETRY;
2466 		it6505_link_train_ok(it6505);
2467 	} else {
2468 		it6505->auto_train_retry--;
2469 		it6505_dump(it6505);
2470 	}
2471 
2472 }
2473 
it6505_plugged_status_to_codec(struct it6505 * it6505)2474 static void it6505_plugged_status_to_codec(struct it6505 *it6505)
2475 {
2476 	enum drm_connector_status status = it6505->connector_status;
2477 
2478 	if (it6505->plugged_cb && it6505->codec_dev)
2479 		it6505->plugged_cb(it6505->codec_dev,
2480 				   status == connector_status_connected);
2481 }
2482 
it6505_remove_edid(struct it6505 * it6505)2483 static void it6505_remove_edid(struct it6505 *it6505)
2484 {
2485 	drm_edid_free(it6505->cached_edid);
2486 	it6505->cached_edid = NULL;
2487 }
2488 
it6505_process_hpd_irq(struct it6505 * it6505)2489 static int it6505_process_hpd_irq(struct it6505 *it6505)
2490 {
2491 	struct device *dev = it6505->dev;
2492 	int ret, dpcd_sink_count, dp_irq_vector, bstatus;
2493 	u8 link_status[DP_LINK_STATUS_SIZE];
2494 
2495 	if (!it6505_get_sink_hpd_status(it6505)) {
2496 		DRM_DEV_DEBUG_DRIVER(dev, "HPD_IRQ HPD low");
2497 		it6505->sink_count = 0;
2498 		return 0;
2499 	}
2500 
2501 	ret = it6505_dpcd_read(it6505, DP_SINK_COUNT);
2502 	if (ret < 0)
2503 		return ret;
2504 
2505 	dpcd_sink_count = DP_GET_SINK_COUNT(ret);
2506 	DRM_DEV_DEBUG_DRIVER(dev, "dpcd_sink_count: %d it6505->sink_count:%d",
2507 			     dpcd_sink_count, it6505->sink_count);
2508 
2509 	if (it6505->branch_device && dpcd_sink_count != it6505->sink_count) {
2510 		memset(it6505->dpcd, 0, sizeof(it6505->dpcd));
2511 		it6505->sink_count = dpcd_sink_count;
2512 		it6505_reset_logic(it6505);
2513 		it6505_int_mask_enable(it6505);
2514 		it6505_init(it6505);
2515 		it6505_remove_edid(it6505);
2516 		return 0;
2517 	}
2518 
2519 	dp_irq_vector = it6505_dpcd_read(it6505, DP_DEVICE_SERVICE_IRQ_VECTOR);
2520 	if (dp_irq_vector < 0)
2521 		return dp_irq_vector;
2522 
2523 	DRM_DEV_DEBUG_DRIVER(dev, "dp_irq_vector = 0x%02x", dp_irq_vector);
2524 
2525 	if (dp_irq_vector & DP_CP_IRQ) {
2526 		bstatus = it6505_dpcd_read(it6505, DP_AUX_HDCP_BSTATUS);
2527 		if (bstatus < 0)
2528 			return bstatus;
2529 
2530 		DRM_DEV_DEBUG_DRIVER(dev, "Bstatus = 0x%02x", bstatus);
2531 
2532 		/*Check BSTATUS when recive CP_IRQ */
2533 		if (bstatus & DP_BSTATUS_R0_PRIME_READY &&
2534 		    it6505->hdcp_status == HDCP_AUTH_GOING)
2535 			it6505_set_bits(it6505, REG_HDCP_TRIGGER, HDCP_TRIGGER_CPIRQ,
2536 					HDCP_TRIGGER_CPIRQ);
2537 		else if (bstatus & (DP_BSTATUS_REAUTH_REQ | DP_BSTATUS_LINK_FAILURE) &&
2538 			 it6505->hdcp_status == HDCP_AUTH_DONE)
2539 			it6505_start_hdcp(it6505);
2540 	}
2541 
2542 	ret = drm_dp_dpcd_read_link_status(&it6505->aux, link_status);
2543 	if (ret < 0) {
2544 		dev_err(dev, "Fail to read link status ret: %d", ret);
2545 		return ret;
2546 	}
2547 
2548 	DRM_DEV_DEBUG_DRIVER(dev, "link status = 0x%*ph",
2549 			     (int)ARRAY_SIZE(link_status), link_status);
2550 
2551 	if (!drm_dp_channel_eq_ok(link_status, it6505->lane_count)) {
2552 		it6505->auto_train_retry = AUTO_TRAIN_RETRY;
2553 		it6505_video_reset(it6505);
2554 	}
2555 
2556 	return 0;
2557 }
2558 
it6505_irq_hpd(struct it6505 * it6505)2559 static void it6505_irq_hpd(struct it6505 *it6505)
2560 {
2561 	struct device *dev = it6505->dev;
2562 	int dp_sink_count;
2563 
2564 	it6505->hpd_state = it6505_get_sink_hpd_status(it6505);
2565 	DRM_DEV_DEBUG_DRIVER(dev, "hpd change interrupt, change to %s",
2566 			     it6505->hpd_state ? "high" : "low");
2567 
2568 	if (it6505->hpd_state) {
2569 		wait_for_completion_timeout(&it6505->extcon_completion,
2570 					    msecs_to_jiffies(1000));
2571 		it6505_aux_on(it6505);
2572 		if (it6505->dpcd[0] == 0) {
2573 			it6505_get_dpcd(it6505, DP_DPCD_REV, it6505->dpcd,
2574 					ARRAY_SIZE(it6505->dpcd));
2575 			it6505_variable_config(it6505);
2576 			it6505_parse_link_capabilities(it6505);
2577 		}
2578 		it6505->auto_train_retry = AUTO_TRAIN_RETRY;
2579 
2580 		it6505_drm_dp_link_set_power(&it6505->aux, &it6505->link,
2581 					     DP_SET_POWER_D0);
2582 		dp_sink_count = it6505_dpcd_read(it6505, DP_SINK_COUNT);
2583 		it6505->sink_count = DP_GET_SINK_COUNT(dp_sink_count);
2584 
2585 		DRM_DEV_DEBUG_DRIVER(dev, "it6505->sink_count: %d",
2586 				     it6505->sink_count);
2587 
2588 		it6505_lane_termination_on(it6505);
2589 		it6505_lane_power_on(it6505);
2590 
2591 		/*
2592 		 * for some dongle which issue HPD_irq
2593 		 * when sink count change from  0->1
2594 		 * it6505 not able to receive HPD_IRQ
2595 		 * if HW never go into trainig done
2596 		 */
2597 
2598 		if (it6505->branch_device && it6505->sink_count == 0)
2599 			schedule_work(&it6505->link_works);
2600 
2601 		if (!it6505_get_video_status(it6505))
2602 			it6505_video_reset(it6505);
2603 	} else {
2604 		memset(it6505->dpcd, 0, sizeof(it6505->dpcd));
2605 		it6505_remove_edid(it6505);
2606 
2607 		if (it6505->hdcp_desired)
2608 			it6505_stop_hdcp(it6505);
2609 
2610 		it6505_video_disable(it6505);
2611 		it6505_disable_audio(it6505);
2612 		it6505_stop_link_train(it6505);
2613 		it6505_lane_off(it6505);
2614 		it6505_link_reset_step_train(it6505);
2615 	}
2616 
2617 	if (it6505->bridge.dev)
2618 		drm_helper_hpd_irq_event(it6505->bridge.dev);
2619 }
2620 
it6505_irq_hpd_irq(struct it6505 * it6505)2621 static void it6505_irq_hpd_irq(struct it6505 *it6505)
2622 {
2623 	struct device *dev = it6505->dev;
2624 
2625 	DRM_DEV_DEBUG_DRIVER(dev, "hpd_irq interrupt");
2626 
2627 	if (it6505_process_hpd_irq(it6505) < 0)
2628 		DRM_DEV_DEBUG_DRIVER(dev, "process hpd_irq fail!");
2629 }
2630 
it6505_irq_scdt(struct it6505 * it6505)2631 static void it6505_irq_scdt(struct it6505 *it6505)
2632 {
2633 	struct device *dev = it6505->dev;
2634 	bool data;
2635 
2636 	data = it6505_get_video_status(it6505);
2637 	DRM_DEV_DEBUG_DRIVER(dev, "video stable change interrupt, %s",
2638 			     data ? "stable" : "unstable");
2639 	it6505_calc_video_info(it6505);
2640 	it6505_link_reset_step_train(it6505);
2641 
2642 	if (data)
2643 		schedule_work(&it6505->link_works);
2644 }
2645 
it6505_irq_hdcp_done(struct it6505 * it6505)2646 static void it6505_irq_hdcp_done(struct it6505 *it6505)
2647 {
2648 	struct device *dev = it6505->dev;
2649 
2650 	DRM_DEV_DEBUG_DRIVER(dev, "hdcp done interrupt");
2651 	it6505->hdcp_status = HDCP_AUTH_DONE;
2652 	it6505_show_hdcp_info(it6505);
2653 }
2654 
it6505_irq_hdcp_fail(struct it6505 * it6505)2655 static void it6505_irq_hdcp_fail(struct it6505 *it6505)
2656 {
2657 	struct device *dev = it6505->dev;
2658 
2659 	DRM_DEV_DEBUG_DRIVER(dev, "hdcp fail interrupt");
2660 	it6505->hdcp_status = HDCP_AUTH_IDLE;
2661 	it6505_show_hdcp_info(it6505);
2662 	it6505_start_hdcp(it6505);
2663 }
2664 
it6505_irq_aux_cmd_fail(struct it6505 * it6505)2665 static void it6505_irq_aux_cmd_fail(struct it6505 *it6505)
2666 {
2667 	struct device *dev = it6505->dev;
2668 
2669 	DRM_DEV_DEBUG_DRIVER(dev, "AUX PC Request Fail Interrupt");
2670 }
2671 
it6505_irq_hdcp_ksv_check(struct it6505 * it6505)2672 static void it6505_irq_hdcp_ksv_check(struct it6505 *it6505)
2673 {
2674 	struct device *dev = it6505->dev;
2675 
2676 	DRM_DEV_DEBUG_DRIVER(dev, "HDCP repeater R0 event Interrupt");
2677 	/* 1B01 HDCP encription should start when R0 is ready*/
2678 	it6505_set_bits(it6505, REG_HDCP_TRIGGER,
2679 			HDCP_TRIGGER_KSV_DONE, HDCP_TRIGGER_KSV_DONE);
2680 
2681 	schedule_work(&it6505->hdcp_wait_ksv_list);
2682 }
2683 
it6505_irq_audio_fifo_error(struct it6505 * it6505)2684 static void it6505_irq_audio_fifo_error(struct it6505 *it6505)
2685 {
2686 	struct device *dev = it6505->dev;
2687 
2688 	DRM_DEV_DEBUG_DRIVER(dev, "audio fifo error Interrupt");
2689 
2690 	if (it6505_audio_input(it6505))
2691 		it6505_enable_audio(it6505);
2692 }
2693 
it6505_irq_link_train_fail(struct it6505 * it6505)2694 static void it6505_irq_link_train_fail(struct it6505 *it6505)
2695 {
2696 	struct device *dev = it6505->dev;
2697 
2698 	DRM_DEV_DEBUG_DRIVER(dev, "link training fail interrupt");
2699 	schedule_work(&it6505->link_works);
2700 }
2701 
it6505_test_bit(unsigned int bit,const unsigned int * addr)2702 static bool it6505_test_bit(unsigned int bit, const unsigned int *addr)
2703 {
2704 	return 1 & (addr[bit / BITS_PER_BYTE] >> (bit % BITS_PER_BYTE));
2705 }
2706 
it6505_irq_video_handler(struct it6505 * it6505,const int * int_status)2707 static void it6505_irq_video_handler(struct it6505 *it6505, const int *int_status)
2708 {
2709 	struct device *dev = it6505->dev;
2710 	int reg_0d, reg_int03;
2711 
2712 	/*
2713 	 * When video SCDT change with video not stable,
2714 	 * Or video FIFO error, need video reset
2715 	 */
2716 
2717 	if ((!it6505_get_video_status(it6505) &&
2718 	     (it6505_test_bit(INT_SCDT_CHANGE, (unsigned int *)int_status))) ||
2719 	    (it6505_test_bit(BIT_INT_IO_FIFO_OVERFLOW,
2720 			     (unsigned int *)int_status)) ||
2721 	    (it6505_test_bit(BIT_INT_VID_FIFO_ERROR,
2722 			     (unsigned int *)int_status))) {
2723 		it6505->auto_train_retry = AUTO_TRAIN_RETRY;
2724 		flush_work(&it6505->link_works);
2725 		it6505_stop_hdcp(it6505);
2726 		it6505_video_reset(it6505);
2727 
2728 		usleep_range(10000, 11000);
2729 
2730 		/*
2731 		 * Clear FIFO error IRQ to prevent fifo error -> reset loop
2732 		 * HW will trigger SCDT change IRQ again when video stable
2733 		 */
2734 
2735 		reg_int03 = it6505_read(it6505, INT_STATUS_03);
2736 		reg_0d = it6505_read(it6505, REG_SYSTEM_STS);
2737 
2738 		reg_int03 &= (BIT(INT_VID_FIFO_ERROR) | BIT(INT_IO_LATCH_FIFO_OVERFLOW));
2739 		it6505_write(it6505, INT_STATUS_03, reg_int03);
2740 
2741 		DRM_DEV_DEBUG_DRIVER(dev, "reg08 = 0x%02x", reg_int03);
2742 		DRM_DEV_DEBUG_DRIVER(dev, "reg0D = 0x%02x", reg_0d);
2743 
2744 		return;
2745 	}
2746 
2747 	if (it6505_test_bit(INT_SCDT_CHANGE, (unsigned int *)int_status))
2748 		it6505_irq_scdt(it6505);
2749 }
2750 
it6505_int_threaded_handler(int unused,void * data)2751 static irqreturn_t it6505_int_threaded_handler(int unused, void *data)
2752 {
2753 	struct it6505 *it6505 = data;
2754 	struct device *dev = it6505->dev;
2755 	static const struct {
2756 		int bit;
2757 		void (*handler)(struct it6505 *it6505);
2758 	} irq_vec[] = {
2759 		{ BIT_INT_HPD, it6505_irq_hpd },
2760 		{ BIT_INT_HPD_IRQ, it6505_irq_hpd_irq },
2761 		{ BIT_INT_HDCP_FAIL, it6505_irq_hdcp_fail },
2762 		{ BIT_INT_HDCP_DONE, it6505_irq_hdcp_done },
2763 		{ BIT_INT_AUX_CMD_FAIL, it6505_irq_aux_cmd_fail },
2764 		{ BIT_INT_HDCP_KSV_CHECK, it6505_irq_hdcp_ksv_check },
2765 		{ BIT_INT_AUDIO_FIFO_ERROR, it6505_irq_audio_fifo_error },
2766 		{ BIT_INT_LINK_TRAIN_FAIL, it6505_irq_link_train_fail },
2767 	};
2768 	int int_status[3], i;
2769 
2770 	if (it6505->enable_drv_hold || !it6505->powered)
2771 		return IRQ_HANDLED;
2772 
2773 	pm_runtime_get_sync(dev);
2774 
2775 	int_status[0] = it6505_read(it6505, INT_STATUS_01);
2776 	int_status[1] = it6505_read(it6505, INT_STATUS_02);
2777 	int_status[2] = it6505_read(it6505, INT_STATUS_03);
2778 
2779 	it6505_write(it6505, INT_STATUS_01, int_status[0]);
2780 	it6505_write(it6505, INT_STATUS_02, int_status[1]);
2781 	it6505_write(it6505, INT_STATUS_03, int_status[2]);
2782 
2783 	DRM_DEV_DEBUG_DRIVER(dev, "reg06 = 0x%02x", int_status[0]);
2784 	DRM_DEV_DEBUG_DRIVER(dev, "reg07 = 0x%02x", int_status[1]);
2785 	DRM_DEV_DEBUG_DRIVER(dev, "reg08 = 0x%02x", int_status[2]);
2786 	it6505_debug_print(it6505, REG_SYSTEM_STS, "");
2787 
2788 	if (it6505_test_bit(irq_vec[0].bit, (unsigned int *)int_status))
2789 		irq_vec[0].handler(it6505);
2790 
2791 	if (it6505->hpd_state) {
2792 		for (i = 1; i < ARRAY_SIZE(irq_vec); i++) {
2793 			if (it6505_test_bit(irq_vec[i].bit, (unsigned int *)int_status))
2794 				irq_vec[i].handler(it6505);
2795 		}
2796 		it6505_irq_video_handler(it6505, (unsigned int *)int_status);
2797 	}
2798 
2799 	pm_runtime_put_sync(dev);
2800 
2801 	return IRQ_HANDLED;
2802 }
2803 
it6505_poweron(struct it6505 * it6505)2804 static int it6505_poweron(struct it6505 *it6505)
2805 {
2806 	struct device *dev = it6505->dev;
2807 	struct it6505_platform_data *pdata = &it6505->pdata;
2808 	int err;
2809 
2810 	DRM_DEV_DEBUG_DRIVER(dev, "it6505 start powered on");
2811 
2812 	if (it6505->powered) {
2813 		DRM_DEV_DEBUG_DRIVER(dev, "it6505 already powered on");
2814 		return 0;
2815 	}
2816 
2817 	if (pdata->pwr18) {
2818 		err = regulator_enable(pdata->pwr18);
2819 		if (err) {
2820 			DRM_DEV_DEBUG_DRIVER(dev, "Failed to enable VDD18: %d",
2821 					     err);
2822 			return err;
2823 		}
2824 	}
2825 
2826 	if (pdata->ovdd) {
2827 		/* time interval between IVDD and OVDD at least be 1ms */
2828 		usleep_range(1000, 2000);
2829 		err = regulator_enable(pdata->ovdd);
2830 		if (err) {
2831 			regulator_disable(pdata->pwr18);
2832 			return err;
2833 		}
2834 	}
2835 	/* time interval between OVDD and SYSRSTN at least be 10ms */
2836 	if (pdata->gpiod_reset) {
2837 		usleep_range(10000, 20000);
2838 		gpiod_set_value_cansleep(pdata->gpiod_reset, 1);
2839 		usleep_range(1000, 2000);
2840 		gpiod_set_value_cansleep(pdata->gpiod_reset, 0);
2841 		usleep_range(25000, 35000);
2842 	}
2843 
2844 	it6505->powered = true;
2845 	it6505_reset_logic(it6505);
2846 	it6505_int_mask_enable(it6505);
2847 	it6505_init(it6505);
2848 	it6505_lane_off(it6505);
2849 
2850 	enable_irq(it6505->irq);
2851 
2852 	return 0;
2853 }
2854 
it6505_poweroff(struct it6505 * it6505)2855 static int it6505_poweroff(struct it6505 *it6505)
2856 {
2857 	struct device *dev = it6505->dev;
2858 	struct it6505_platform_data *pdata = &it6505->pdata;
2859 	int err;
2860 
2861 	DRM_DEV_DEBUG_DRIVER(dev, "it6505 start power off");
2862 
2863 	if (!it6505->powered) {
2864 		DRM_DEV_DEBUG_DRIVER(dev, "power had been already off");
2865 		return 0;
2866 	}
2867 
2868 	disable_irq_nosync(it6505->irq);
2869 
2870 	if (pdata->gpiod_reset)
2871 		gpiod_set_value_cansleep(pdata->gpiod_reset, 1);
2872 
2873 	if (pdata->pwr18) {
2874 		err = regulator_disable(pdata->pwr18);
2875 		if (err)
2876 			return err;
2877 	}
2878 
2879 	if (pdata->ovdd) {
2880 		err = regulator_disable(pdata->ovdd);
2881 		if (err)
2882 			return err;
2883 	}
2884 
2885 	it6505->powered = false;
2886 	it6505->sink_count = 0;
2887 
2888 	return 0;
2889 }
2890 
it6505_detect(struct it6505 * it6505)2891 static enum drm_connector_status it6505_detect(struct it6505 *it6505)
2892 {
2893 	struct device *dev = it6505->dev;
2894 	enum drm_connector_status status = connector_status_disconnected;
2895 	int dp_sink_count;
2896 
2897 	DRM_DEV_DEBUG_DRIVER(dev, "it6505->sink_count:%d powered:%d",
2898 			     it6505->sink_count, it6505->powered);
2899 
2900 	mutex_lock(&it6505->mode_lock);
2901 
2902 	if (!it6505->powered)
2903 		goto unlock;
2904 
2905 	if (it6505->enable_drv_hold) {
2906 		status = it6505->hpd_state ? connector_status_connected :
2907 					     connector_status_disconnected;
2908 		goto unlock;
2909 	}
2910 
2911 	if (it6505->hpd_state) {
2912 		it6505_drm_dp_link_set_power(&it6505->aux, &it6505->link,
2913 					     DP_SET_POWER_D0);
2914 		dp_sink_count = it6505_dpcd_read(it6505, DP_SINK_COUNT);
2915 		it6505->sink_count = DP_GET_SINK_COUNT(dp_sink_count);
2916 		DRM_DEV_DEBUG_DRIVER(dev, "it6505->sink_count:%d branch:%d",
2917 				     it6505->sink_count, it6505->branch_device);
2918 
2919 		if (it6505->branch_device) {
2920 			status = (it6505->sink_count != 0) ?
2921 				 connector_status_connected :
2922 				 connector_status_disconnected;
2923 		} else {
2924 			status = connector_status_connected;
2925 		}
2926 	} else {
2927 		it6505->sink_count = 0;
2928 		memset(it6505->dpcd, 0, sizeof(it6505->dpcd));
2929 	}
2930 
2931 unlock:
2932 	if (it6505->connector_status != status) {
2933 		it6505->connector_status = status;
2934 		it6505_plugged_status_to_codec(it6505);
2935 	}
2936 
2937 	mutex_unlock(&it6505->mode_lock);
2938 
2939 	return status;
2940 }
2941 
it6505_extcon_notifier(struct notifier_block * self,unsigned long event,void * ptr)2942 static int it6505_extcon_notifier(struct notifier_block *self,
2943 				  unsigned long event, void *ptr)
2944 {
2945 	struct it6505 *it6505 = container_of(self, struct it6505, event_nb);
2946 
2947 	schedule_work(&it6505->extcon_wq);
2948 	return NOTIFY_DONE;
2949 }
2950 
it6505_extcon_work(struct work_struct * work)2951 static void it6505_extcon_work(struct work_struct *work)
2952 {
2953 	struct it6505 *it6505 = container_of(work, struct it6505, extcon_wq);
2954 	struct device *dev = it6505->dev;
2955 	int state, ret;
2956 
2957 	if (it6505->enable_drv_hold)
2958 		return;
2959 
2960 	mutex_lock(&it6505->extcon_lock);
2961 
2962 	state = extcon_get_state(it6505->extcon, EXTCON_DISP_DP);
2963 	DRM_DEV_DEBUG_DRIVER(dev, "EXTCON_DISP_DP = 0x%02x", state);
2964 
2965 	if (state == it6505->extcon_state || unlikely(state < 0))
2966 		goto unlock;
2967 	it6505->extcon_state = state;
2968 	if (state) {
2969 		DRM_DEV_DEBUG_DRIVER(dev, "start to power on");
2970 		msleep(100);
2971 		ret = pm_runtime_get_sync(dev);
2972 
2973 		/*
2974 		 * On system resume, extcon_work can be triggered before
2975 		 * pm_runtime_force_resume re-enables runtime power management.
2976 		 * Handling the error here to make sure the bridge is powered on.
2977 		 */
2978 		if (ret < 0)
2979 			it6505_poweron(it6505);
2980 
2981 		complete_all(&it6505->extcon_completion);
2982 	} else {
2983 		DRM_DEV_DEBUG_DRIVER(dev, "start to power off");
2984 		pm_runtime_put_sync(dev);
2985 		reinit_completion(&it6505->extcon_completion);
2986 
2987 		drm_helper_hpd_irq_event(it6505->bridge.dev);
2988 		memset(it6505->dpcd, 0, sizeof(it6505->dpcd));
2989 		DRM_DEV_DEBUG_DRIVER(dev, "power off it6505 success!");
2990 	}
2991 
2992 unlock:
2993 	mutex_unlock(&it6505->extcon_lock);
2994 }
2995 
it6505_use_notifier_module(struct it6505 * it6505)2996 static int it6505_use_notifier_module(struct it6505 *it6505)
2997 {
2998 	int ret;
2999 	struct device *dev = it6505->dev;
3000 
3001 	it6505->event_nb.notifier_call = it6505_extcon_notifier;
3002 	INIT_WORK(&it6505->extcon_wq, it6505_extcon_work);
3003 	ret = devm_extcon_register_notifier(it6505->dev,
3004 					    it6505->extcon, EXTCON_DISP_DP,
3005 					    &it6505->event_nb);
3006 	if (ret) {
3007 		dev_err(dev, "failed to register notifier for DP");
3008 		return ret;
3009 	}
3010 
3011 	schedule_work(&it6505->extcon_wq);
3012 
3013 	return 0;
3014 }
3015 
it6505_remove_notifier_module(struct it6505 * it6505)3016 static void it6505_remove_notifier_module(struct it6505 *it6505)
3017 {
3018 	if (it6505->extcon) {
3019 		devm_extcon_unregister_notifier(it6505->dev,
3020 						it6505->extcon,	EXTCON_DISP_DP,
3021 						&it6505->event_nb);
3022 
3023 		flush_work(&it6505->extcon_wq);
3024 	}
3025 }
3026 
it6505_delayed_audio(struct work_struct * work)3027 static void __maybe_unused it6505_delayed_audio(struct work_struct *work)
3028 {
3029 	struct it6505 *it6505 = container_of(work, struct it6505,
3030 					     delayed_audio.work);
3031 
3032 	DRM_DEV_DEBUG_DRIVER(it6505->dev, "start");
3033 
3034 	if (!it6505->powered)
3035 		return;
3036 
3037 	if (!it6505->enable_drv_hold)
3038 		it6505_enable_audio(it6505);
3039 }
3040 
it6505_audio_setup_hw_params(struct it6505 * it6505,struct hdmi_codec_params * params)3041 static int __maybe_unused it6505_audio_setup_hw_params(struct it6505 *it6505,
3042 						       struct hdmi_codec_params
3043 						       *params)
3044 {
3045 	struct device *dev = it6505->dev;
3046 	int i = 0;
3047 
3048 	DRM_DEV_DEBUG_DRIVER(dev, "%s %d Hz, %d bit, %d channels\n", __func__,
3049 			     params->sample_rate, params->sample_width,
3050 			     params->cea.channels);
3051 
3052 	if (!it6505->bridge.encoder)
3053 		return -ENODEV;
3054 
3055 	if (params->cea.channels <= 1 || params->cea.channels > 8) {
3056 		DRM_DEV_DEBUG_DRIVER(dev, "channel number: %d not support",
3057 				     it6505->audio.channel_count);
3058 		return -EINVAL;
3059 	}
3060 
3061 	it6505->audio.channel_count = params->cea.channels;
3062 
3063 	while (i < ARRAY_SIZE(audio_sample_rate_map) &&
3064 	       params->sample_rate !=
3065 		       audio_sample_rate_map[i].sample_rate_value) {
3066 		i++;
3067 	}
3068 	if (i == ARRAY_SIZE(audio_sample_rate_map)) {
3069 		DRM_DEV_DEBUG_DRIVER(dev, "sample rate: %d Hz not support",
3070 				     params->sample_rate);
3071 		return -EINVAL;
3072 	}
3073 	it6505->audio.sample_rate = audio_sample_rate_map[i].rate;
3074 
3075 	switch (params->sample_width) {
3076 	case 16:
3077 		it6505->audio.word_length = WORD_LENGTH_16BIT;
3078 		break;
3079 	case 18:
3080 		it6505->audio.word_length = WORD_LENGTH_18BIT;
3081 		break;
3082 	case 20:
3083 		it6505->audio.word_length = WORD_LENGTH_20BIT;
3084 		break;
3085 	case 24:
3086 	case 32:
3087 		it6505->audio.word_length = WORD_LENGTH_24BIT;
3088 		break;
3089 	default:
3090 		DRM_DEV_DEBUG_DRIVER(dev, "wordlength: %d bit not support",
3091 				     params->sample_width);
3092 		return -EINVAL;
3093 	}
3094 
3095 	return 0;
3096 }
3097 
it6505_audio_shutdown(struct device * dev,void * data)3098 static void __maybe_unused it6505_audio_shutdown(struct device *dev, void *data)
3099 {
3100 	struct it6505 *it6505 = dev_get_drvdata(dev);
3101 
3102 	if (it6505->powered)
3103 		it6505_disable_audio(it6505);
3104 }
3105 
it6505_audio_hook_plugged_cb(struct device * dev,void * data,hdmi_codec_plugged_cb fn,struct device * codec_dev)3106 static int __maybe_unused it6505_audio_hook_plugged_cb(struct device *dev,
3107 						       void *data,
3108 						       hdmi_codec_plugged_cb fn,
3109 						       struct device *codec_dev)
3110 {
3111 	struct it6505 *it6505 = data;
3112 
3113 	it6505->plugged_cb = fn;
3114 	it6505->codec_dev = codec_dev;
3115 	it6505_plugged_status_to_codec(it6505);
3116 
3117 	return 0;
3118 }
3119 
bridge_to_it6505(struct drm_bridge * bridge)3120 static inline struct it6505 *bridge_to_it6505(struct drm_bridge *bridge)
3121 {
3122 	return container_of(bridge, struct it6505, bridge);
3123 }
3124 
it6505_bridge_attach(struct drm_bridge * bridge,enum drm_bridge_attach_flags flags)3125 static int it6505_bridge_attach(struct drm_bridge *bridge,
3126 				enum drm_bridge_attach_flags flags)
3127 {
3128 	struct it6505 *it6505 = bridge_to_it6505(bridge);
3129 	struct device *dev = it6505->dev;
3130 	int ret;
3131 
3132 	if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
3133 		DRM_ERROR("DRM_BRIDGE_ATTACH_NO_CONNECTOR must be supplied");
3134 		return -EINVAL;
3135 	}
3136 
3137 	/* Register aux channel */
3138 	it6505->aux.drm_dev = bridge->dev;
3139 
3140 	ret = drm_dp_aux_register(&it6505->aux);
3141 
3142 	if (ret < 0) {
3143 		dev_err(dev, "Failed to register aux: %d", ret);
3144 		return ret;
3145 	}
3146 
3147 	if (it6505->extcon) {
3148 		ret = it6505_use_notifier_module(it6505);
3149 		if (ret < 0) {
3150 			dev_err(dev, "use notifier module failed");
3151 			return ret;
3152 		}
3153 	}
3154 
3155 	return 0;
3156 }
3157 
it6505_bridge_detach(struct drm_bridge * bridge)3158 static void it6505_bridge_detach(struct drm_bridge *bridge)
3159 {
3160 	struct it6505 *it6505 = bridge_to_it6505(bridge);
3161 
3162 	flush_work(&it6505->link_works);
3163 	it6505_remove_notifier_module(it6505);
3164 }
3165 
3166 static enum drm_mode_status
it6505_bridge_mode_valid(struct drm_bridge * bridge,const struct drm_display_info * info,const struct drm_display_mode * mode)3167 it6505_bridge_mode_valid(struct drm_bridge *bridge,
3168 			 const struct drm_display_info *info,
3169 			 const struct drm_display_mode *mode)
3170 {
3171 	struct it6505 *it6505 = bridge_to_it6505(bridge);
3172 
3173 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
3174 		return MODE_NO_INTERLACE;
3175 
3176 	if (mode->clock > it6505->max_dpi_pixel_clock)
3177 		return MODE_CLOCK_HIGH;
3178 
3179 	it6505->video_info.clock = mode->clock;
3180 
3181 	return MODE_OK;
3182 }
3183 
it6505_bridge_atomic_enable(struct drm_bridge * bridge,struct drm_bridge_state * old_state)3184 static void it6505_bridge_atomic_enable(struct drm_bridge *bridge,
3185 					struct drm_bridge_state *old_state)
3186 {
3187 	struct it6505 *it6505 = bridge_to_it6505(bridge);
3188 	struct device *dev = it6505->dev;
3189 	struct drm_atomic_state *state = old_state->base.state;
3190 	struct hdmi_avi_infoframe frame;
3191 	struct drm_crtc_state *crtc_state;
3192 	struct drm_connector_state *conn_state;
3193 	struct drm_display_mode *mode;
3194 	struct drm_connector *connector;
3195 	int ret;
3196 
3197 	DRM_DEV_DEBUG_DRIVER(dev, "start");
3198 
3199 	connector = drm_atomic_get_new_connector_for_encoder(state,
3200 							     bridge->encoder);
3201 
3202 	if (WARN_ON(!connector))
3203 		return;
3204 
3205 	conn_state = drm_atomic_get_new_connector_state(state, connector);
3206 
3207 	if (WARN_ON(!conn_state))
3208 		return;
3209 
3210 	crtc_state = drm_atomic_get_new_crtc_state(state, conn_state->crtc);
3211 
3212 	if (WARN_ON(!crtc_state))
3213 		return;
3214 
3215 	mode = &crtc_state->adjusted_mode;
3216 
3217 	if (WARN_ON(!mode))
3218 		return;
3219 
3220 	ret = drm_hdmi_avi_infoframe_from_display_mode(&frame,
3221 						       connector,
3222 						       mode);
3223 	if (ret)
3224 		dev_err(dev, "Failed to setup AVI infoframe: %d", ret);
3225 
3226 	it6505_update_video_parameter(it6505, mode);
3227 
3228 	ret = it6505_send_video_infoframe(it6505, &frame);
3229 
3230 	if (ret)
3231 		dev_err(dev, "Failed to send AVI infoframe: %d", ret);
3232 
3233 	it6505_int_mask_enable(it6505);
3234 	it6505_video_reset(it6505);
3235 
3236 	it6505_drm_dp_link_set_power(&it6505->aux, &it6505->link,
3237 				     DP_SET_POWER_D0);
3238 }
3239 
it6505_bridge_atomic_disable(struct drm_bridge * bridge,struct drm_bridge_state * old_state)3240 static void it6505_bridge_atomic_disable(struct drm_bridge *bridge,
3241 					 struct drm_bridge_state *old_state)
3242 {
3243 	struct it6505 *it6505 = bridge_to_it6505(bridge);
3244 	struct device *dev = it6505->dev;
3245 
3246 	DRM_DEV_DEBUG_DRIVER(dev, "start");
3247 
3248 	if (it6505->powered) {
3249 		it6505_drm_dp_link_set_power(&it6505->aux, &it6505->link,
3250 					     DP_SET_POWER_D3);
3251 		it6505_video_disable(it6505);
3252 	}
3253 }
3254 
it6505_bridge_atomic_pre_enable(struct drm_bridge * bridge,struct drm_bridge_state * old_state)3255 static void it6505_bridge_atomic_pre_enable(struct drm_bridge *bridge,
3256 					    struct drm_bridge_state *old_state)
3257 {
3258 	struct it6505 *it6505 = bridge_to_it6505(bridge);
3259 	struct device *dev = it6505->dev;
3260 
3261 	DRM_DEV_DEBUG_DRIVER(dev, "start");
3262 
3263 	pm_runtime_get_sync(dev);
3264 }
3265 
it6505_bridge_atomic_post_disable(struct drm_bridge * bridge,struct drm_bridge_state * old_state)3266 static void it6505_bridge_atomic_post_disable(struct drm_bridge *bridge,
3267 					      struct drm_bridge_state *old_state)
3268 {
3269 	struct it6505 *it6505 = bridge_to_it6505(bridge);
3270 	struct device *dev = it6505->dev;
3271 
3272 	DRM_DEV_DEBUG_DRIVER(dev, "start");
3273 
3274 	pm_runtime_put_sync(dev);
3275 }
3276 
3277 static enum drm_connector_status
it6505_bridge_detect(struct drm_bridge * bridge)3278 it6505_bridge_detect(struct drm_bridge *bridge)
3279 {
3280 	struct it6505 *it6505 = bridge_to_it6505(bridge);
3281 
3282 	return it6505_detect(it6505);
3283 }
3284 
it6505_bridge_edid_read(struct drm_bridge * bridge,struct drm_connector * connector)3285 static const struct drm_edid *it6505_bridge_edid_read(struct drm_bridge *bridge,
3286 						      struct drm_connector *connector)
3287 {
3288 	struct it6505 *it6505 = bridge_to_it6505(bridge);
3289 	struct device *dev = it6505->dev;
3290 
3291 	if (!it6505->cached_edid) {
3292 		it6505->cached_edid = drm_edid_read_custom(connector,
3293 							   it6505_get_edid_block,
3294 							   it6505);
3295 
3296 		if (!it6505->cached_edid) {
3297 			DRM_DEV_DEBUG_DRIVER(dev, "failed to get edid!");
3298 			return NULL;
3299 		}
3300 	}
3301 
3302 	return drm_edid_dup(it6505->cached_edid);
3303 }
3304 
3305 static const struct drm_bridge_funcs it6505_bridge_funcs = {
3306 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
3307 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
3308 	.atomic_reset = drm_atomic_helper_bridge_reset,
3309 	.attach = it6505_bridge_attach,
3310 	.detach = it6505_bridge_detach,
3311 	.mode_valid = it6505_bridge_mode_valid,
3312 	.atomic_enable = it6505_bridge_atomic_enable,
3313 	.atomic_disable = it6505_bridge_atomic_disable,
3314 	.atomic_pre_enable = it6505_bridge_atomic_pre_enable,
3315 	.atomic_post_disable = it6505_bridge_atomic_post_disable,
3316 	.detect = it6505_bridge_detect,
3317 	.edid_read = it6505_bridge_edid_read,
3318 };
3319 
it6505_bridge_resume(struct device * dev)3320 static __maybe_unused int it6505_bridge_resume(struct device *dev)
3321 {
3322 	struct it6505 *it6505 = dev_get_drvdata(dev);
3323 
3324 	return it6505_poweron(it6505);
3325 }
3326 
it6505_bridge_suspend(struct device * dev)3327 static __maybe_unused int it6505_bridge_suspend(struct device *dev)
3328 {
3329 	struct it6505 *it6505 = dev_get_drvdata(dev);
3330 
3331 	it6505_remove_edid(it6505);
3332 
3333 	return it6505_poweroff(it6505);
3334 }
3335 
3336 static const struct dev_pm_ops it6505_bridge_pm_ops = {
3337 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)
3338 	SET_RUNTIME_PM_OPS(it6505_bridge_suspend, it6505_bridge_resume, NULL)
3339 };
3340 
it6505_init_pdata(struct it6505 * it6505)3341 static int it6505_init_pdata(struct it6505 *it6505)
3342 {
3343 	struct it6505_platform_data *pdata = &it6505->pdata;
3344 	struct device *dev = it6505->dev;
3345 
3346 	/* 1.0V digital core power regulator  */
3347 	pdata->pwr18 = devm_regulator_get(dev, "pwr18");
3348 	if (IS_ERR(pdata->pwr18)) {
3349 		dev_err(dev, "pwr18 regulator not found");
3350 		return PTR_ERR(pdata->pwr18);
3351 	}
3352 
3353 	pdata->ovdd = devm_regulator_get(dev, "ovdd");
3354 	if (IS_ERR(pdata->ovdd)) {
3355 		dev_err(dev, "ovdd regulator not found");
3356 		return PTR_ERR(pdata->ovdd);
3357 	}
3358 
3359 	pdata->gpiod_reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
3360 	if (IS_ERR(pdata->gpiod_reset)) {
3361 		dev_err(dev, "gpiod_reset gpio not found");
3362 		return PTR_ERR(pdata->gpiod_reset);
3363 	}
3364 
3365 	return 0;
3366 }
3367 
it6505_get_data_lanes_count(const struct device_node * endpoint,const unsigned int min,const unsigned int max)3368 static int it6505_get_data_lanes_count(const struct device_node *endpoint,
3369 				       const unsigned int min,
3370 				       const unsigned int max)
3371 {
3372 	int ret;
3373 
3374 	ret = of_property_count_u32_elems(endpoint, "data-lanes");
3375 	if (ret < 0)
3376 		return ret;
3377 
3378 	if (ret < min || ret > max)
3379 		return -EINVAL;
3380 
3381 	return ret;
3382 }
3383 
it6505_parse_dt(struct it6505 * it6505)3384 static void it6505_parse_dt(struct it6505 *it6505)
3385 {
3386 	struct device *dev = it6505->dev;
3387 	struct device_node *np = dev->of_node, *ep = NULL;
3388 	int len;
3389 	u64 link_frequencies;
3390 	u32 data_lanes[4];
3391 	u32 *afe_setting = &it6505->afe_setting;
3392 	u32 *max_lane_count = &it6505->max_lane_count;
3393 	u32 *max_dpi_pixel_clock = &it6505->max_dpi_pixel_clock;
3394 
3395 	it6505->lane_swap_disabled =
3396 		device_property_read_bool(dev, "no-laneswap");
3397 
3398 	if (it6505->lane_swap_disabled)
3399 		it6505->lane_swap = false;
3400 
3401 	if (device_property_read_u32(dev, "afe-setting", afe_setting) == 0) {
3402 		if (*afe_setting >= ARRAY_SIZE(afe_setting_table)) {
3403 			dev_err(dev, "afe setting error, use default");
3404 			*afe_setting = 0;
3405 		}
3406 	} else {
3407 		*afe_setting = 0;
3408 	}
3409 
3410 	ep = of_graph_get_endpoint_by_regs(np, 1, 0);
3411 	of_node_put(ep);
3412 
3413 	if (ep) {
3414 		len = it6505_get_data_lanes_count(ep, 1, 4);
3415 
3416 		if (len > 0 && len != 3) {
3417 			of_property_read_u32_array(ep, "data-lanes",
3418 						   data_lanes, len);
3419 			*max_lane_count = len;
3420 		} else {
3421 			*max_lane_count = MAX_LANE_COUNT;
3422 			dev_err(dev, "error data-lanes, use default");
3423 		}
3424 	} else {
3425 		*max_lane_count = MAX_LANE_COUNT;
3426 		dev_err(dev, "error endpoint, use default");
3427 	}
3428 
3429 	ep = of_graph_get_endpoint_by_regs(np, 0, 0);
3430 	of_node_put(ep);
3431 
3432 	if (ep) {
3433 		len = of_property_read_variable_u64_array(ep,
3434 							  "link-frequencies",
3435 							  &link_frequencies, 0,
3436 							  1);
3437 		if (len >= 0) {
3438 			do_div(link_frequencies, 1000);
3439 			if (link_frequencies > 297000) {
3440 				dev_err(dev,
3441 					"max pixel clock error, use default");
3442 				*max_dpi_pixel_clock = DPI_PIXEL_CLK_MAX;
3443 			} else {
3444 				*max_dpi_pixel_clock = link_frequencies;
3445 			}
3446 		} else {
3447 			dev_err(dev, "error link frequencies, use default");
3448 			*max_dpi_pixel_clock = DPI_PIXEL_CLK_MAX;
3449 		}
3450 	} else {
3451 		dev_err(dev, "error endpoint, use default");
3452 		*max_dpi_pixel_clock = DPI_PIXEL_CLK_MAX;
3453 	}
3454 
3455 	DRM_DEV_DEBUG_DRIVER(dev, "using afe_setting: %u, max_lane_count: %u",
3456 			     it6505->afe_setting, it6505->max_lane_count);
3457 	DRM_DEV_DEBUG_DRIVER(dev, "using max_dpi_pixel_clock: %u kHz",
3458 			     it6505->max_dpi_pixel_clock);
3459 }
3460 
receive_timing_debugfs_show(struct file * file,char __user * buf,size_t len,loff_t * ppos)3461 static ssize_t receive_timing_debugfs_show(struct file *file, char __user *buf,
3462 					   size_t len, loff_t *ppos)
3463 {
3464 	struct it6505 *it6505 = file->private_data;
3465 	struct drm_display_mode *vid;
3466 	u8 read_buf[READ_BUFFER_SIZE];
3467 	u8 *str = read_buf, *end = read_buf + READ_BUFFER_SIZE;
3468 	ssize_t ret, count;
3469 
3470 	if (!it6505)
3471 		return -ENODEV;
3472 
3473 	it6505_calc_video_info(it6505);
3474 	vid = &it6505->video_info;
3475 	str += scnprintf(str, end - str, "---video timing---\n");
3476 	str += scnprintf(str, end - str, "PCLK:%d.%03dMHz\n",
3477 			 vid->clock / 1000, vid->clock % 1000);
3478 	str += scnprintf(str, end - str, "HTotal:%d\n", vid->htotal);
3479 	str += scnprintf(str, end - str, "HActive:%d\n", vid->hdisplay);
3480 	str += scnprintf(str, end - str, "HFrontPorch:%d\n",
3481 			 vid->hsync_start - vid->hdisplay);
3482 	str += scnprintf(str, end - str, "HSyncWidth:%d\n",
3483 			 vid->hsync_end - vid->hsync_start);
3484 	str += scnprintf(str, end - str, "HBackPorch:%d\n",
3485 			 vid->htotal - vid->hsync_end);
3486 	str += scnprintf(str, end - str, "VTotal:%d\n", vid->vtotal);
3487 	str += scnprintf(str, end - str, "VActive:%d\n", vid->vdisplay);
3488 	str += scnprintf(str, end - str, "VFrontPorch:%d\n",
3489 			 vid->vsync_start - vid->vdisplay);
3490 	str += scnprintf(str, end - str, "VSyncWidth:%d\n",
3491 			 vid->vsync_end - vid->vsync_start);
3492 	str += scnprintf(str, end - str, "VBackPorch:%d\n",
3493 			 vid->vtotal - vid->vsync_end);
3494 
3495 	count = str - read_buf;
3496 	ret = simple_read_from_buffer(buf, len, ppos, read_buf, count);
3497 
3498 	return ret;
3499 }
3500 
force_power_on_off_debugfs_write(void * data,u64 value)3501 static int force_power_on_off_debugfs_write(void *data, u64 value)
3502 {
3503 	struct it6505 *it6505 = data;
3504 
3505 	if (!it6505)
3506 		return -ENODEV;
3507 
3508 	if (value)
3509 		it6505_poweron(it6505);
3510 	else
3511 		it6505_poweroff(it6505);
3512 
3513 	return 0;
3514 }
3515 
enable_drv_hold_debugfs_show(void * data,u64 * buf)3516 static int enable_drv_hold_debugfs_show(void *data, u64 *buf)
3517 {
3518 	struct it6505 *it6505 = data;
3519 
3520 	if (!it6505)
3521 		return -ENODEV;
3522 
3523 	*buf = it6505->enable_drv_hold;
3524 
3525 	return 0;
3526 }
3527 
enable_drv_hold_debugfs_write(void * data,u64 drv_hold)3528 static int enable_drv_hold_debugfs_write(void *data, u64 drv_hold)
3529 {
3530 	struct it6505 *it6505 = data;
3531 
3532 	if (!it6505)
3533 		return -ENODEV;
3534 
3535 	it6505->enable_drv_hold = drv_hold;
3536 
3537 	if (it6505->enable_drv_hold) {
3538 		it6505_int_mask_disable(it6505);
3539 	} else {
3540 		it6505_clear_int(it6505);
3541 		it6505_int_mask_enable(it6505);
3542 
3543 		if (it6505->powered) {
3544 			it6505->connector_status =
3545 					it6505_get_sink_hpd_status(it6505) ?
3546 					connector_status_connected :
3547 					connector_status_disconnected;
3548 		} else {
3549 			it6505->connector_status =
3550 					connector_status_disconnected;
3551 		}
3552 	}
3553 
3554 	return 0;
3555 }
3556 
3557 static const struct file_operations receive_timing_fops = {
3558 	.owner = THIS_MODULE,
3559 	.open = simple_open,
3560 	.read = receive_timing_debugfs_show,
3561 	.llseek = default_llseek,
3562 };
3563 
3564 DEFINE_DEBUGFS_ATTRIBUTE(fops_force_power, NULL,
3565 			 force_power_on_off_debugfs_write, "%llu\n");
3566 
3567 DEFINE_DEBUGFS_ATTRIBUTE(fops_enable_drv_hold, enable_drv_hold_debugfs_show,
3568 			 enable_drv_hold_debugfs_write, "%llu\n");
3569 
3570 static const struct debugfs_entries debugfs_entry[] = {
3571 	{ "receive_timing", &receive_timing_fops },
3572 	{ "force_power_on_off", &fops_force_power },
3573 	{ "enable_drv_hold", &fops_enable_drv_hold },
3574 	{ NULL, NULL },
3575 };
3576 
debugfs_create_files(struct it6505 * it6505)3577 static void debugfs_create_files(struct it6505 *it6505)
3578 {
3579 	int i = 0;
3580 
3581 	while (debugfs_entry[i].name && debugfs_entry[i].fops) {
3582 		debugfs_create_file(debugfs_entry[i].name, 0644,
3583 				    it6505->debugfs, it6505,
3584 				    debugfs_entry[i].fops);
3585 		i++;
3586 	}
3587 }
3588 
debugfs_init(struct it6505 * it6505)3589 static void debugfs_init(struct it6505 *it6505)
3590 {
3591 	struct device *dev = it6505->dev;
3592 
3593 	it6505->debugfs = debugfs_create_dir(DEBUGFS_DIR_NAME, NULL);
3594 
3595 	if (IS_ERR(it6505->debugfs)) {
3596 		dev_err(dev, "failed to create debugfs root");
3597 		return;
3598 	}
3599 
3600 	debugfs_create_files(it6505);
3601 }
3602 
it6505_debugfs_remove(struct it6505 * it6505)3603 static void it6505_debugfs_remove(struct it6505 *it6505)
3604 {
3605 	debugfs_remove_recursive(it6505->debugfs);
3606 }
3607 
it6505_shutdown(struct i2c_client * client)3608 static void it6505_shutdown(struct i2c_client *client)
3609 {
3610 	struct it6505 *it6505 = dev_get_drvdata(&client->dev);
3611 
3612 	if (it6505->powered)
3613 		it6505_lane_off(it6505);
3614 }
3615 
it6505_i2c_probe(struct i2c_client * client)3616 static int it6505_i2c_probe(struct i2c_client *client)
3617 {
3618 	struct it6505 *it6505;
3619 	struct device *dev = &client->dev;
3620 	struct extcon_dev *extcon;
3621 	int err;
3622 
3623 	it6505 = devm_kzalloc(&client->dev, sizeof(*it6505), GFP_KERNEL);
3624 	if (!it6505)
3625 		return -ENOMEM;
3626 
3627 	mutex_init(&it6505->extcon_lock);
3628 	mutex_init(&it6505->mode_lock);
3629 	mutex_init(&it6505->aux_lock);
3630 
3631 	it6505->bridge.of_node = client->dev.of_node;
3632 	it6505->connector_status = connector_status_disconnected;
3633 	it6505->dev = &client->dev;
3634 	i2c_set_clientdata(client, it6505);
3635 
3636 	/* get extcon device from DTS */
3637 	extcon = extcon_get_edev_by_phandle(dev, 0);
3638 	if (PTR_ERR(extcon) == -EPROBE_DEFER)
3639 		return -EPROBE_DEFER;
3640 	if (IS_ERR(extcon)) {
3641 		dev_err(dev, "can not get extcon device!");
3642 		return PTR_ERR(extcon);
3643 	}
3644 
3645 	it6505->extcon = extcon;
3646 
3647 	it6505->regmap = devm_regmap_init_i2c(client, &it6505_regmap_config);
3648 	if (IS_ERR(it6505->regmap)) {
3649 		dev_err(dev, "regmap i2c init failed");
3650 		err = PTR_ERR(it6505->regmap);
3651 		return err;
3652 	}
3653 
3654 	err = it6505_init_pdata(it6505);
3655 	if (err) {
3656 		dev_err(dev, "Failed to initialize pdata: %d", err);
3657 		return err;
3658 	}
3659 
3660 	it6505_parse_dt(it6505);
3661 
3662 	it6505->irq = client->irq;
3663 
3664 	if (!it6505->irq) {
3665 		dev_err(dev, "Failed to get INTP IRQ");
3666 		err = -ENODEV;
3667 		return err;
3668 	}
3669 
3670 	err = devm_request_threaded_irq(&client->dev, it6505->irq, NULL,
3671 					it6505_int_threaded_handler,
3672 					IRQF_TRIGGER_LOW | IRQF_ONESHOT |
3673 					IRQF_NO_AUTOEN,
3674 					"it6505-intp", it6505);
3675 	if (err) {
3676 		dev_err(dev, "Failed to request INTP threaded IRQ: %d", err);
3677 		return err;
3678 	}
3679 
3680 	INIT_WORK(&it6505->link_works, it6505_link_training_work);
3681 	INIT_WORK(&it6505->hdcp_wait_ksv_list, it6505_hdcp_wait_ksv_list);
3682 	INIT_DELAYED_WORK(&it6505->hdcp_work, it6505_hdcp_work);
3683 	init_completion(&it6505->extcon_completion);
3684 	memset(it6505->dpcd, 0, sizeof(it6505->dpcd));
3685 	it6505->powered = false;
3686 	it6505->enable_drv_hold = DEFAULT_DRV_HOLD;
3687 
3688 	if (DEFAULT_PWR_ON)
3689 		it6505_poweron(it6505);
3690 
3691 	DRM_DEV_DEBUG_DRIVER(dev, "it6505 device name: %s", dev_name(dev));
3692 	debugfs_init(it6505);
3693 	pm_runtime_enable(dev);
3694 
3695 	it6505->aux.name = "DP-AUX";
3696 	it6505->aux.dev = dev;
3697 	it6505->aux.transfer = it6505_aux_transfer;
3698 	drm_dp_aux_init(&it6505->aux);
3699 
3700 	it6505->bridge.funcs = &it6505_bridge_funcs;
3701 	it6505->bridge.type = DRM_MODE_CONNECTOR_DisplayPort;
3702 	it6505->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID |
3703 			     DRM_BRIDGE_OP_HPD;
3704 	drm_bridge_add(&it6505->bridge);
3705 
3706 	return 0;
3707 }
3708 
it6505_i2c_remove(struct i2c_client * client)3709 static void it6505_i2c_remove(struct i2c_client *client)
3710 {
3711 	struct it6505 *it6505 = i2c_get_clientdata(client);
3712 
3713 	drm_bridge_remove(&it6505->bridge);
3714 	drm_dp_aux_unregister(&it6505->aux);
3715 	it6505_debugfs_remove(it6505);
3716 	it6505_poweroff(it6505);
3717 	it6505_remove_edid(it6505);
3718 }
3719 
3720 static const struct i2c_device_id it6505_id[] = {
3721 	{ "it6505" },
3722 	{ }
3723 };
3724 
3725 MODULE_DEVICE_TABLE(i2c, it6505_id);
3726 
3727 static const struct of_device_id it6505_of_match[] = {
3728 	{ .compatible = "ite,it6505" },
3729 	{ }
3730 };
3731 MODULE_DEVICE_TABLE(of, it6505_of_match);
3732 
3733 static struct i2c_driver it6505_i2c_driver = {
3734 	.driver = {
3735 		.name = "it6505",
3736 		.of_match_table = it6505_of_match,
3737 		.pm = &it6505_bridge_pm_ops,
3738 	},
3739 	.probe = it6505_i2c_probe,
3740 	.remove = it6505_i2c_remove,
3741 	.shutdown = it6505_shutdown,
3742 	.id_table = it6505_id,
3743 };
3744 
3745 module_i2c_driver(it6505_i2c_driver);
3746 
3747 MODULE_AUTHOR("Allen Chen <allen.chen@ite.com.tw>");
3748 MODULE_DESCRIPTION("IT6505 DisplayPort Transmitter driver");
3749 MODULE_LICENSE("GPL v2");
3750