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 			    bv[i][1] != av[i][2] || bv[i][0] != av[i][3])
2256 				break;
2257 
2258 		if (i == 5) {
2259 			DRM_DEV_DEBUG_DRIVER(dev, "V' all match!! %d", retry);
2260 			return true;
2261 		}
2262 	}
2263 
2264 	DRM_DEV_DEBUG_DRIVER(dev, "V' NOT match!! %d", retry);
2265 	return false;
2266 }
2267 
it6505_hdcp_wait_ksv_list(struct work_struct * work)2268 static void it6505_hdcp_wait_ksv_list(struct work_struct *work)
2269 {
2270 	struct it6505 *it6505 = container_of(work, struct it6505,
2271 					     hdcp_wait_ksv_list);
2272 	struct device *dev = it6505->dev;
2273 	u8 bstatus;
2274 	bool ksv_list_check;
2275 	/* 1B-04 wait ksv list for 5s */
2276 	unsigned long timeout = jiffies +
2277 				msecs_to_jiffies(5000) + 1;
2278 
2279 	for (;;) {
2280 		if (!it6505_get_sink_hpd_status(it6505))
2281 			return;
2282 
2283 		bstatus = it6505_dpcd_read(it6505, DP_AUX_HDCP_BSTATUS);
2284 
2285 		if (bstatus & DP_BSTATUS_READY)
2286 			break;
2287 
2288 		if (time_after(jiffies, timeout)) {
2289 			DRM_DEV_DEBUG_DRIVER(dev, "KSV list wait timeout");
2290 			goto timeout;
2291 		}
2292 
2293 		msleep(20);
2294 	}
2295 
2296 	ksv_list_check = it6505_hdcp_part2_ksvlist_check(it6505);
2297 	DRM_DEV_DEBUG_DRIVER(dev, "ksv list ready, ksv list check %s",
2298 			     ksv_list_check ? "pass" : "fail");
2299 
2300 	if (ksv_list_check)
2301 		return;
2302 
2303 timeout:
2304 	it6505_start_hdcp(it6505);
2305 }
2306 
it6505_hdcp_work(struct work_struct * work)2307 static void it6505_hdcp_work(struct work_struct *work)
2308 {
2309 	struct it6505 *it6505 = container_of(work, struct it6505,
2310 					     hdcp_work.work);
2311 	struct device *dev = it6505->dev;
2312 	int ret;
2313 	u8 link_status[DP_LINK_STATUS_SIZE] = { 0 };
2314 
2315 	DRM_DEV_DEBUG_DRIVER(dev, "start");
2316 
2317 	if (!it6505_get_sink_hpd_status(it6505))
2318 		return;
2319 
2320 	ret = drm_dp_dpcd_read_link_status(&it6505->aux, link_status);
2321 	DRM_DEV_DEBUG_DRIVER(dev, "ret: %d link_status: %*ph", ret,
2322 			     (int)sizeof(link_status), link_status);
2323 
2324 	if (ret < 0 || !drm_dp_channel_eq_ok(link_status, it6505->lane_count) ||
2325 	    !it6505_get_video_status(it6505)) {
2326 		DRM_DEV_DEBUG_DRIVER(dev, "link train not done or no video");
2327 		return;
2328 	}
2329 
2330 	ret = it6505_get_dpcd(it6505, DP_AUX_HDCP_BKSV, it6505->bksvs,
2331 			      ARRAY_SIZE(it6505->bksvs));
2332 	if (ret < 0) {
2333 		dev_err(dev, "fail to get bksv  ret: %d", ret);
2334 		it6505_set_bits(it6505, REG_HDCP_TRIGGER,
2335 				HDCP_TRIGGER_KSV_FAIL, HDCP_TRIGGER_KSV_FAIL);
2336 	}
2337 
2338 	DRM_DEV_DEBUG_DRIVER(dev, "bksv = 0x%*ph",
2339 			     (int)ARRAY_SIZE(it6505->bksvs), it6505->bksvs);
2340 
2341 	if (!it6505_hdcp_is_ksv_valid(it6505->bksvs)) {
2342 		dev_err(dev, "Display Port bksv not valid");
2343 		it6505_set_bits(it6505, REG_HDCP_TRIGGER,
2344 				HDCP_TRIGGER_KSV_FAIL, HDCP_TRIGGER_KSV_FAIL);
2345 	}
2346 
2347 	it6505_hdcp_part1_auth(it6505);
2348 }
2349 
it6505_show_hdcp_info(struct it6505 * it6505)2350 static void it6505_show_hdcp_info(struct it6505 *it6505)
2351 {
2352 	struct device *dev = it6505->dev;
2353 	int i;
2354 	u8 *sha1 = it6505->sha1_input;
2355 
2356 	DRM_DEV_DEBUG_DRIVER(dev, "hdcp_status: %d is_repeater: %d",
2357 			     it6505->hdcp_status, it6505->is_repeater);
2358 	DRM_DEV_DEBUG_DRIVER(dev, "bksv = 0x%*ph",
2359 			     (int)ARRAY_SIZE(it6505->bksvs), it6505->bksvs);
2360 
2361 	if (it6505->is_repeater) {
2362 		DRM_DEV_DEBUG_DRIVER(dev, "hdcp_down_stream_count: %d",
2363 				     it6505->hdcp_down_stream_count);
2364 		DRM_DEV_DEBUG_DRIVER(dev, "sha1_input: 0x%*ph",
2365 				     (int)ARRAY_SIZE(it6505->sha1_input),
2366 				     it6505->sha1_input);
2367 		for (i = 0; i < it6505->hdcp_down_stream_count; i++) {
2368 			DRM_DEV_DEBUG_DRIVER(dev, "KSV_%d = 0x%*ph", i,
2369 					     DRM_HDCP_KSV_LEN, sha1);
2370 			sha1 += DRM_HDCP_KSV_LEN;
2371 		}
2372 		DRM_DEV_DEBUG_DRIVER(dev, "binfo: 0x%2ph M0: 0x%8ph",
2373 				     sha1, sha1 + 2);
2374 	}
2375 }
2376 
it6505_stop_link_train(struct it6505 * it6505)2377 static void it6505_stop_link_train(struct it6505 *it6505)
2378 {
2379 	it6505->link_state = LINK_IDLE;
2380 	cancel_work_sync(&it6505->link_works);
2381 	it6505_write(it6505, REG_TRAIN_CTRL1, FORCE_RETRAIN);
2382 }
2383 
it6505_link_train_ok(struct it6505 * it6505)2384 static void it6505_link_train_ok(struct it6505 *it6505)
2385 {
2386 	struct device *dev = it6505->dev;
2387 
2388 	it6505->link_state = LINK_OK;
2389 	/* disalbe mute enable avi info frame */
2390 	it6505_set_bits(it6505, REG_DATA_MUTE_CTRL, EN_VID_MUTE, 0x00);
2391 	it6505_set_bits(it6505, REG_INFOFRAME_CTRL,
2392 			EN_VID_CTRL_PKT, EN_VID_CTRL_PKT);
2393 
2394 	if (it6505_audio_input(it6505)) {
2395 		DRM_DEV_DEBUG_DRIVER(dev, "Enable audio!");
2396 		it6505_enable_audio(it6505);
2397 	}
2398 
2399 	if (it6505->hdcp_desired)
2400 		it6505_start_hdcp(it6505);
2401 }
2402 
it6505_link_step_train_process(struct it6505 * it6505)2403 static void it6505_link_step_train_process(struct it6505 *it6505)
2404 {
2405 	struct device *dev = it6505->dev;
2406 	int ret, i, step_retry = 3;
2407 
2408 	DRM_DEV_DEBUG_DRIVER(dev, "Start step train");
2409 
2410 	if (it6505->sink_count == 0) {
2411 		DRM_DEV_DEBUG_DRIVER(dev, "it6505->sink_count:%d, force eq",
2412 				     it6505->sink_count);
2413 		it6505_set_bits(it6505,	REG_TRAIN_CTRL0, FORCE_EQ_DONE,
2414 				FORCE_EQ_DONE);
2415 		return;
2416 	}
2417 
2418 	if (!it6505->step_train) {
2419 		DRM_DEV_DEBUG_DRIVER(dev, "not support step train");
2420 		return;
2421 	}
2422 
2423 	/* step training start here */
2424 	for (i = 0; i < step_retry; i++) {
2425 		it6505_link_reset_step_train(it6505);
2426 		ret = it6505_link_start_step_train(it6505);
2427 		DRM_DEV_DEBUG_DRIVER(dev, "step train %s, retry:%d times",
2428 				     ret ? "pass" : "failed", i + 1);
2429 		if (ret) {
2430 			it6505_link_train_ok(it6505);
2431 			return;
2432 		}
2433 	}
2434 
2435 	DRM_DEV_DEBUG_DRIVER(dev, "training fail");
2436 	it6505->link_state = LINK_IDLE;
2437 	it6505_video_reset(it6505);
2438 }
2439 
it6505_link_training_work(struct work_struct * work)2440 static void it6505_link_training_work(struct work_struct *work)
2441 {
2442 	struct it6505 *it6505 = container_of(work, struct it6505, link_works);
2443 	struct device *dev = it6505->dev;
2444 	int ret;
2445 
2446 	DRM_DEV_DEBUG_DRIVER(dev, "it6505->sink_count: %d",
2447 			     it6505->sink_count);
2448 
2449 	if (!it6505_get_sink_hpd_status(it6505))
2450 		return;
2451 
2452 	it6505_link_training_setup(it6505);
2453 	it6505_reset_hdcp(it6505);
2454 	it6505_aux_reset(it6505);
2455 
2456 	if (it6505->auto_train_retry < 1) {
2457 		it6505_link_step_train_process(it6505);
2458 		return;
2459 	}
2460 
2461 	ret = it6505_link_start_auto_train(it6505);
2462 	DRM_DEV_DEBUG_DRIVER(dev, "auto train %s, auto_train_retry: %d",
2463 			     ret ? "pass" : "failed", it6505->auto_train_retry);
2464 
2465 	if (ret) {
2466 		it6505->auto_train_retry = AUTO_TRAIN_RETRY;
2467 		it6505_link_train_ok(it6505);
2468 	} else {
2469 		it6505->auto_train_retry--;
2470 		it6505_dump(it6505);
2471 	}
2472 
2473 }
2474 
it6505_plugged_status_to_codec(struct it6505 * it6505)2475 static void it6505_plugged_status_to_codec(struct it6505 *it6505)
2476 {
2477 	enum drm_connector_status status = it6505->connector_status;
2478 
2479 	if (it6505->plugged_cb && it6505->codec_dev)
2480 		it6505->plugged_cb(it6505->codec_dev,
2481 				   status == connector_status_connected);
2482 }
2483 
it6505_remove_edid(struct it6505 * it6505)2484 static void it6505_remove_edid(struct it6505 *it6505)
2485 {
2486 	drm_edid_free(it6505->cached_edid);
2487 	it6505->cached_edid = NULL;
2488 }
2489 
it6505_process_hpd_irq(struct it6505 * it6505)2490 static int it6505_process_hpd_irq(struct it6505 *it6505)
2491 {
2492 	struct device *dev = it6505->dev;
2493 	int ret, dpcd_sink_count, dp_irq_vector, bstatus;
2494 	u8 link_status[DP_LINK_STATUS_SIZE];
2495 
2496 	if (!it6505_get_sink_hpd_status(it6505)) {
2497 		DRM_DEV_DEBUG_DRIVER(dev, "HPD_IRQ HPD low");
2498 		it6505->sink_count = 0;
2499 		return 0;
2500 	}
2501 
2502 	ret = it6505_dpcd_read(it6505, DP_SINK_COUNT);
2503 	if (ret < 0)
2504 		return ret;
2505 
2506 	dpcd_sink_count = DP_GET_SINK_COUNT(ret);
2507 	DRM_DEV_DEBUG_DRIVER(dev, "dpcd_sink_count: %d it6505->sink_count:%d",
2508 			     dpcd_sink_count, it6505->sink_count);
2509 
2510 	if (it6505->branch_device && dpcd_sink_count != it6505->sink_count) {
2511 		memset(it6505->dpcd, 0, sizeof(it6505->dpcd));
2512 		it6505->sink_count = dpcd_sink_count;
2513 		it6505_reset_logic(it6505);
2514 		it6505_int_mask_enable(it6505);
2515 		it6505_init(it6505);
2516 		it6505_remove_edid(it6505);
2517 		return 0;
2518 	}
2519 
2520 	dp_irq_vector = it6505_dpcd_read(it6505, DP_DEVICE_SERVICE_IRQ_VECTOR);
2521 	if (dp_irq_vector < 0)
2522 		return dp_irq_vector;
2523 
2524 	DRM_DEV_DEBUG_DRIVER(dev, "dp_irq_vector = 0x%02x", dp_irq_vector);
2525 
2526 	if (dp_irq_vector & DP_CP_IRQ) {
2527 		bstatus = it6505_dpcd_read(it6505, DP_AUX_HDCP_BSTATUS);
2528 		if (bstatus < 0)
2529 			return bstatus;
2530 
2531 		DRM_DEV_DEBUG_DRIVER(dev, "Bstatus = 0x%02x", bstatus);
2532 
2533 		/*Check BSTATUS when recive CP_IRQ */
2534 		if (bstatus & DP_BSTATUS_R0_PRIME_READY &&
2535 		    it6505->hdcp_status == HDCP_AUTH_GOING)
2536 			it6505_set_bits(it6505, REG_HDCP_TRIGGER, HDCP_TRIGGER_CPIRQ,
2537 					HDCP_TRIGGER_CPIRQ);
2538 		else if (bstatus & (DP_BSTATUS_REAUTH_REQ | DP_BSTATUS_LINK_FAILURE) &&
2539 			 it6505->hdcp_status == HDCP_AUTH_DONE)
2540 			it6505_start_hdcp(it6505);
2541 	}
2542 
2543 	ret = drm_dp_dpcd_read_link_status(&it6505->aux, link_status);
2544 	if (ret < 0) {
2545 		dev_err(dev, "Fail to read link status ret: %d", ret);
2546 		return ret;
2547 	}
2548 
2549 	DRM_DEV_DEBUG_DRIVER(dev, "link status = 0x%*ph",
2550 			     (int)ARRAY_SIZE(link_status), link_status);
2551 
2552 	if (!drm_dp_channel_eq_ok(link_status, it6505->lane_count)) {
2553 		it6505->auto_train_retry = AUTO_TRAIN_RETRY;
2554 		it6505_video_reset(it6505);
2555 	}
2556 
2557 	return 0;
2558 }
2559 
it6505_irq_hpd(struct it6505 * it6505)2560 static void it6505_irq_hpd(struct it6505 *it6505)
2561 {
2562 	struct device *dev = it6505->dev;
2563 	int dp_sink_count;
2564 
2565 	it6505->hpd_state = it6505_get_sink_hpd_status(it6505);
2566 	DRM_DEV_DEBUG_DRIVER(dev, "hpd change interrupt, change to %s",
2567 			     it6505->hpd_state ? "high" : "low");
2568 
2569 	if (it6505->hpd_state) {
2570 		wait_for_completion_timeout(&it6505->extcon_completion,
2571 					    msecs_to_jiffies(1000));
2572 		it6505_aux_on(it6505);
2573 		if (it6505->dpcd[0] == 0) {
2574 			it6505_get_dpcd(it6505, DP_DPCD_REV, it6505->dpcd,
2575 					ARRAY_SIZE(it6505->dpcd));
2576 			it6505_variable_config(it6505);
2577 			it6505_parse_link_capabilities(it6505);
2578 		}
2579 		it6505->auto_train_retry = AUTO_TRAIN_RETRY;
2580 
2581 		it6505_drm_dp_link_set_power(&it6505->aux, &it6505->link,
2582 					     DP_SET_POWER_D0);
2583 		dp_sink_count = it6505_dpcd_read(it6505, DP_SINK_COUNT);
2584 		it6505->sink_count = DP_GET_SINK_COUNT(dp_sink_count);
2585 
2586 		DRM_DEV_DEBUG_DRIVER(dev, "it6505->sink_count: %d",
2587 				     it6505->sink_count);
2588 
2589 		it6505_lane_termination_on(it6505);
2590 		it6505_lane_power_on(it6505);
2591 
2592 		/*
2593 		 * for some dongle which issue HPD_irq
2594 		 * when sink count change from  0->1
2595 		 * it6505 not able to receive HPD_IRQ
2596 		 * if HW never go into trainig done
2597 		 */
2598 
2599 		if (it6505->branch_device && it6505->sink_count == 0)
2600 			schedule_work(&it6505->link_works);
2601 
2602 		if (!it6505_get_video_status(it6505))
2603 			it6505_video_reset(it6505);
2604 	} else {
2605 		memset(it6505->dpcd, 0, sizeof(it6505->dpcd));
2606 		it6505_remove_edid(it6505);
2607 
2608 		if (it6505->hdcp_desired)
2609 			it6505_stop_hdcp(it6505);
2610 
2611 		it6505_video_disable(it6505);
2612 		it6505_disable_audio(it6505);
2613 		it6505_stop_link_train(it6505);
2614 		it6505_lane_off(it6505);
2615 		it6505_link_reset_step_train(it6505);
2616 	}
2617 
2618 	if (it6505->bridge.dev)
2619 		drm_helper_hpd_irq_event(it6505->bridge.dev);
2620 }
2621 
it6505_irq_hpd_irq(struct it6505 * it6505)2622 static void it6505_irq_hpd_irq(struct it6505 *it6505)
2623 {
2624 	struct device *dev = it6505->dev;
2625 
2626 	DRM_DEV_DEBUG_DRIVER(dev, "hpd_irq interrupt");
2627 
2628 	if (it6505_process_hpd_irq(it6505) < 0)
2629 		DRM_DEV_DEBUG_DRIVER(dev, "process hpd_irq fail!");
2630 }
2631 
it6505_irq_scdt(struct it6505 * it6505)2632 static void it6505_irq_scdt(struct it6505 *it6505)
2633 {
2634 	struct device *dev = it6505->dev;
2635 	bool data;
2636 
2637 	data = it6505_get_video_status(it6505);
2638 	DRM_DEV_DEBUG_DRIVER(dev, "video stable change interrupt, %s",
2639 			     data ? "stable" : "unstable");
2640 	it6505_calc_video_info(it6505);
2641 	it6505_link_reset_step_train(it6505);
2642 
2643 	if (data)
2644 		schedule_work(&it6505->link_works);
2645 }
2646 
it6505_irq_hdcp_done(struct it6505 * it6505)2647 static void it6505_irq_hdcp_done(struct it6505 *it6505)
2648 {
2649 	struct device *dev = it6505->dev;
2650 
2651 	DRM_DEV_DEBUG_DRIVER(dev, "hdcp done interrupt");
2652 	it6505->hdcp_status = HDCP_AUTH_DONE;
2653 	it6505_show_hdcp_info(it6505);
2654 }
2655 
it6505_irq_hdcp_fail(struct it6505 * it6505)2656 static void it6505_irq_hdcp_fail(struct it6505 *it6505)
2657 {
2658 	struct device *dev = it6505->dev;
2659 
2660 	DRM_DEV_DEBUG_DRIVER(dev, "hdcp fail interrupt");
2661 	it6505->hdcp_status = HDCP_AUTH_IDLE;
2662 	it6505_show_hdcp_info(it6505);
2663 	it6505_start_hdcp(it6505);
2664 }
2665 
it6505_irq_aux_cmd_fail(struct it6505 * it6505)2666 static void it6505_irq_aux_cmd_fail(struct it6505 *it6505)
2667 {
2668 	struct device *dev = it6505->dev;
2669 
2670 	DRM_DEV_DEBUG_DRIVER(dev, "AUX PC Request Fail Interrupt");
2671 }
2672 
it6505_irq_hdcp_ksv_check(struct it6505 * it6505)2673 static void it6505_irq_hdcp_ksv_check(struct it6505 *it6505)
2674 {
2675 	struct device *dev = it6505->dev;
2676 
2677 	DRM_DEV_DEBUG_DRIVER(dev, "HDCP repeater R0 event Interrupt");
2678 	/* 1B01 HDCP encription should start when R0 is ready*/
2679 	it6505_set_bits(it6505, REG_HDCP_TRIGGER,
2680 			HDCP_TRIGGER_KSV_DONE, HDCP_TRIGGER_KSV_DONE);
2681 
2682 	schedule_work(&it6505->hdcp_wait_ksv_list);
2683 }
2684 
it6505_irq_audio_fifo_error(struct it6505 * it6505)2685 static void it6505_irq_audio_fifo_error(struct it6505 *it6505)
2686 {
2687 	struct device *dev = it6505->dev;
2688 
2689 	DRM_DEV_DEBUG_DRIVER(dev, "audio fifo error Interrupt");
2690 
2691 	if (it6505_audio_input(it6505))
2692 		it6505_enable_audio(it6505);
2693 }
2694 
it6505_irq_link_train_fail(struct it6505 * it6505)2695 static void it6505_irq_link_train_fail(struct it6505 *it6505)
2696 {
2697 	struct device *dev = it6505->dev;
2698 
2699 	DRM_DEV_DEBUG_DRIVER(dev, "link training fail interrupt");
2700 	schedule_work(&it6505->link_works);
2701 }
2702 
it6505_test_bit(unsigned int bit,const unsigned int * addr)2703 static bool it6505_test_bit(unsigned int bit, const unsigned int *addr)
2704 {
2705 	return 1 & (addr[bit / BITS_PER_BYTE] >> (bit % BITS_PER_BYTE));
2706 }
2707 
it6505_irq_video_handler(struct it6505 * it6505,const int * int_status)2708 static void it6505_irq_video_handler(struct it6505 *it6505, const int *int_status)
2709 {
2710 	struct device *dev = it6505->dev;
2711 	int reg_0d, reg_int03;
2712 
2713 	/*
2714 	 * When video SCDT change with video not stable,
2715 	 * Or video FIFO error, need video reset
2716 	 */
2717 
2718 	if ((!it6505_get_video_status(it6505) &&
2719 	     (it6505_test_bit(INT_SCDT_CHANGE, (unsigned int *)int_status))) ||
2720 	    (it6505_test_bit(BIT_INT_IO_FIFO_OVERFLOW,
2721 			     (unsigned int *)int_status)) ||
2722 	    (it6505_test_bit(BIT_INT_VID_FIFO_ERROR,
2723 			     (unsigned int *)int_status))) {
2724 		it6505->auto_train_retry = AUTO_TRAIN_RETRY;
2725 		flush_work(&it6505->link_works);
2726 		it6505_stop_hdcp(it6505);
2727 		it6505_video_reset(it6505);
2728 
2729 		usleep_range(10000, 11000);
2730 
2731 		/*
2732 		 * Clear FIFO error IRQ to prevent fifo error -> reset loop
2733 		 * HW will trigger SCDT change IRQ again when video stable
2734 		 */
2735 
2736 		reg_int03 = it6505_read(it6505, INT_STATUS_03);
2737 		reg_0d = it6505_read(it6505, REG_SYSTEM_STS);
2738 
2739 		reg_int03 &= (BIT(INT_VID_FIFO_ERROR) | BIT(INT_IO_LATCH_FIFO_OVERFLOW));
2740 		it6505_write(it6505, INT_STATUS_03, reg_int03);
2741 
2742 		DRM_DEV_DEBUG_DRIVER(dev, "reg08 = 0x%02x", reg_int03);
2743 		DRM_DEV_DEBUG_DRIVER(dev, "reg0D = 0x%02x", reg_0d);
2744 
2745 		return;
2746 	}
2747 
2748 	if (it6505_test_bit(INT_SCDT_CHANGE, (unsigned int *)int_status))
2749 		it6505_irq_scdt(it6505);
2750 }
2751 
it6505_int_threaded_handler(int unused,void * data)2752 static irqreturn_t it6505_int_threaded_handler(int unused, void *data)
2753 {
2754 	struct it6505 *it6505 = data;
2755 	struct device *dev = it6505->dev;
2756 	static const struct {
2757 		int bit;
2758 		void (*handler)(struct it6505 *it6505);
2759 	} irq_vec[] = {
2760 		{ BIT_INT_HPD, it6505_irq_hpd },
2761 		{ BIT_INT_HPD_IRQ, it6505_irq_hpd_irq },
2762 		{ BIT_INT_HDCP_FAIL, it6505_irq_hdcp_fail },
2763 		{ BIT_INT_HDCP_DONE, it6505_irq_hdcp_done },
2764 		{ BIT_INT_AUX_CMD_FAIL, it6505_irq_aux_cmd_fail },
2765 		{ BIT_INT_HDCP_KSV_CHECK, it6505_irq_hdcp_ksv_check },
2766 		{ BIT_INT_AUDIO_FIFO_ERROR, it6505_irq_audio_fifo_error },
2767 		{ BIT_INT_LINK_TRAIN_FAIL, it6505_irq_link_train_fail },
2768 	};
2769 	int int_status[3], i;
2770 
2771 	if (it6505->enable_drv_hold || !it6505->powered)
2772 		return IRQ_HANDLED;
2773 
2774 	pm_runtime_get_sync(dev);
2775 
2776 	int_status[0] = it6505_read(it6505, INT_STATUS_01);
2777 	int_status[1] = it6505_read(it6505, INT_STATUS_02);
2778 	int_status[2] = it6505_read(it6505, INT_STATUS_03);
2779 
2780 	it6505_write(it6505, INT_STATUS_01, int_status[0]);
2781 	it6505_write(it6505, INT_STATUS_02, int_status[1]);
2782 	it6505_write(it6505, INT_STATUS_03, int_status[2]);
2783 
2784 	DRM_DEV_DEBUG_DRIVER(dev, "reg06 = 0x%02x", int_status[0]);
2785 	DRM_DEV_DEBUG_DRIVER(dev, "reg07 = 0x%02x", int_status[1]);
2786 	DRM_DEV_DEBUG_DRIVER(dev, "reg08 = 0x%02x", int_status[2]);
2787 	it6505_debug_print(it6505, REG_SYSTEM_STS, "");
2788 
2789 	if (it6505_test_bit(irq_vec[0].bit, (unsigned int *)int_status))
2790 		irq_vec[0].handler(it6505);
2791 
2792 	if (it6505->hpd_state) {
2793 		for (i = 1; i < ARRAY_SIZE(irq_vec); i++) {
2794 			if (it6505_test_bit(irq_vec[i].bit, (unsigned int *)int_status))
2795 				irq_vec[i].handler(it6505);
2796 		}
2797 		it6505_irq_video_handler(it6505, (unsigned int *)int_status);
2798 	}
2799 
2800 	pm_runtime_put_sync(dev);
2801 
2802 	return IRQ_HANDLED;
2803 }
2804 
it6505_poweron(struct it6505 * it6505)2805 static int it6505_poweron(struct it6505 *it6505)
2806 {
2807 	struct device *dev = it6505->dev;
2808 	struct it6505_platform_data *pdata = &it6505->pdata;
2809 	int err;
2810 
2811 	DRM_DEV_DEBUG_DRIVER(dev, "it6505 start powered on");
2812 
2813 	if (it6505->powered) {
2814 		DRM_DEV_DEBUG_DRIVER(dev, "it6505 already powered on");
2815 		return 0;
2816 	}
2817 
2818 	if (pdata->pwr18) {
2819 		err = regulator_enable(pdata->pwr18);
2820 		if (err) {
2821 			DRM_DEV_DEBUG_DRIVER(dev, "Failed to enable VDD18: %d",
2822 					     err);
2823 			return err;
2824 		}
2825 	}
2826 
2827 	if (pdata->ovdd) {
2828 		/* time interval between IVDD and OVDD at least be 1ms */
2829 		usleep_range(1000, 2000);
2830 		err = regulator_enable(pdata->ovdd);
2831 		if (err) {
2832 			regulator_disable(pdata->pwr18);
2833 			return err;
2834 		}
2835 	}
2836 	/* time interval between OVDD and SYSRSTN at least be 10ms */
2837 	if (pdata->gpiod_reset) {
2838 		usleep_range(10000, 20000);
2839 		gpiod_set_value_cansleep(pdata->gpiod_reset, 1);
2840 		usleep_range(1000, 2000);
2841 		gpiod_set_value_cansleep(pdata->gpiod_reset, 0);
2842 		usleep_range(25000, 35000);
2843 	}
2844 
2845 	it6505->powered = true;
2846 	it6505_reset_logic(it6505);
2847 	it6505_int_mask_enable(it6505);
2848 	it6505_init(it6505);
2849 	it6505_lane_off(it6505);
2850 
2851 	enable_irq(it6505->irq);
2852 
2853 	return 0;
2854 }
2855 
it6505_poweroff(struct it6505 * it6505)2856 static int it6505_poweroff(struct it6505 *it6505)
2857 {
2858 	struct device *dev = it6505->dev;
2859 	struct it6505_platform_data *pdata = &it6505->pdata;
2860 	int err;
2861 
2862 	DRM_DEV_DEBUG_DRIVER(dev, "it6505 start power off");
2863 
2864 	if (!it6505->powered) {
2865 		DRM_DEV_DEBUG_DRIVER(dev, "power had been already off");
2866 		return 0;
2867 	}
2868 
2869 	disable_irq_nosync(it6505->irq);
2870 
2871 	if (pdata->gpiod_reset)
2872 		gpiod_set_value_cansleep(pdata->gpiod_reset, 1);
2873 
2874 	if (pdata->pwr18) {
2875 		err = regulator_disable(pdata->pwr18);
2876 		if (err)
2877 			return err;
2878 	}
2879 
2880 	if (pdata->ovdd) {
2881 		err = regulator_disable(pdata->ovdd);
2882 		if (err)
2883 			return err;
2884 	}
2885 
2886 	it6505->powered = false;
2887 	it6505->sink_count = 0;
2888 
2889 	return 0;
2890 }
2891 
it6505_detect(struct it6505 * it6505)2892 static enum drm_connector_status it6505_detect(struct it6505 *it6505)
2893 {
2894 	struct device *dev = it6505->dev;
2895 	enum drm_connector_status status = connector_status_disconnected;
2896 	int dp_sink_count;
2897 
2898 	DRM_DEV_DEBUG_DRIVER(dev, "it6505->sink_count:%d powered:%d",
2899 			     it6505->sink_count, it6505->powered);
2900 
2901 	mutex_lock(&it6505->mode_lock);
2902 
2903 	if (!it6505->powered)
2904 		goto unlock;
2905 
2906 	if (it6505->enable_drv_hold) {
2907 		status = it6505->hpd_state ? connector_status_connected :
2908 					     connector_status_disconnected;
2909 		goto unlock;
2910 	}
2911 
2912 	if (it6505->hpd_state) {
2913 		it6505_drm_dp_link_set_power(&it6505->aux, &it6505->link,
2914 					     DP_SET_POWER_D0);
2915 		dp_sink_count = it6505_dpcd_read(it6505, DP_SINK_COUNT);
2916 		it6505->sink_count = DP_GET_SINK_COUNT(dp_sink_count);
2917 		DRM_DEV_DEBUG_DRIVER(dev, "it6505->sink_count:%d branch:%d",
2918 				     it6505->sink_count, it6505->branch_device);
2919 
2920 		if (it6505->branch_device) {
2921 			status = (it6505->sink_count != 0) ?
2922 				 connector_status_connected :
2923 				 connector_status_disconnected;
2924 		} else {
2925 			status = connector_status_connected;
2926 		}
2927 	} else {
2928 		it6505->sink_count = 0;
2929 		memset(it6505->dpcd, 0, sizeof(it6505->dpcd));
2930 	}
2931 
2932 unlock:
2933 	if (it6505->connector_status != status) {
2934 		it6505->connector_status = status;
2935 		it6505_plugged_status_to_codec(it6505);
2936 	}
2937 
2938 	mutex_unlock(&it6505->mode_lock);
2939 
2940 	return status;
2941 }
2942 
it6505_extcon_notifier(struct notifier_block * self,unsigned long event,void * ptr)2943 static int it6505_extcon_notifier(struct notifier_block *self,
2944 				  unsigned long event, void *ptr)
2945 {
2946 	struct it6505 *it6505 = container_of(self, struct it6505, event_nb);
2947 
2948 	schedule_work(&it6505->extcon_wq);
2949 	return NOTIFY_DONE;
2950 }
2951 
it6505_extcon_work(struct work_struct * work)2952 static void it6505_extcon_work(struct work_struct *work)
2953 {
2954 	struct it6505 *it6505 = container_of(work, struct it6505, extcon_wq);
2955 	struct device *dev = it6505->dev;
2956 	int state, ret;
2957 
2958 	if (it6505->enable_drv_hold)
2959 		return;
2960 
2961 	mutex_lock(&it6505->extcon_lock);
2962 
2963 	state = extcon_get_state(it6505->extcon, EXTCON_DISP_DP);
2964 	DRM_DEV_DEBUG_DRIVER(dev, "EXTCON_DISP_DP = 0x%02x", state);
2965 
2966 	if (state == it6505->extcon_state || unlikely(state < 0))
2967 		goto unlock;
2968 	it6505->extcon_state = state;
2969 	if (state) {
2970 		DRM_DEV_DEBUG_DRIVER(dev, "start to power on");
2971 		msleep(100);
2972 		ret = pm_runtime_get_sync(dev);
2973 
2974 		/*
2975 		 * On system resume, extcon_work can be triggered before
2976 		 * pm_runtime_force_resume re-enables runtime power management.
2977 		 * Handling the error here to make sure the bridge is powered on.
2978 		 */
2979 		if (ret < 0)
2980 			it6505_poweron(it6505);
2981 
2982 		complete_all(&it6505->extcon_completion);
2983 	} else {
2984 		DRM_DEV_DEBUG_DRIVER(dev, "start to power off");
2985 		pm_runtime_put_sync(dev);
2986 		reinit_completion(&it6505->extcon_completion);
2987 
2988 		drm_helper_hpd_irq_event(it6505->bridge.dev);
2989 		memset(it6505->dpcd, 0, sizeof(it6505->dpcd));
2990 		DRM_DEV_DEBUG_DRIVER(dev, "power off it6505 success!");
2991 	}
2992 
2993 unlock:
2994 	mutex_unlock(&it6505->extcon_lock);
2995 }
2996 
it6505_use_notifier_module(struct it6505 * it6505)2997 static int it6505_use_notifier_module(struct it6505 *it6505)
2998 {
2999 	int ret;
3000 	struct device *dev = it6505->dev;
3001 
3002 	it6505->event_nb.notifier_call = it6505_extcon_notifier;
3003 	INIT_WORK(&it6505->extcon_wq, it6505_extcon_work);
3004 	ret = devm_extcon_register_notifier(it6505->dev,
3005 					    it6505->extcon, EXTCON_DISP_DP,
3006 					    &it6505->event_nb);
3007 	if (ret) {
3008 		dev_err(dev, "failed to register notifier for DP");
3009 		return ret;
3010 	}
3011 
3012 	schedule_work(&it6505->extcon_wq);
3013 
3014 	return 0;
3015 }
3016 
it6505_remove_notifier_module(struct it6505 * it6505)3017 static void it6505_remove_notifier_module(struct it6505 *it6505)
3018 {
3019 	if (it6505->extcon) {
3020 		devm_extcon_unregister_notifier(it6505->dev,
3021 						it6505->extcon,	EXTCON_DISP_DP,
3022 						&it6505->event_nb);
3023 
3024 		flush_work(&it6505->extcon_wq);
3025 	}
3026 }
3027 
it6505_delayed_audio(struct work_struct * work)3028 static void __maybe_unused it6505_delayed_audio(struct work_struct *work)
3029 {
3030 	struct it6505 *it6505 = container_of(work, struct it6505,
3031 					     delayed_audio.work);
3032 
3033 	DRM_DEV_DEBUG_DRIVER(it6505->dev, "start");
3034 
3035 	if (!it6505->powered)
3036 		return;
3037 
3038 	if (!it6505->enable_drv_hold)
3039 		it6505_enable_audio(it6505);
3040 }
3041 
it6505_audio_setup_hw_params(struct it6505 * it6505,struct hdmi_codec_params * params)3042 static int __maybe_unused it6505_audio_setup_hw_params(struct it6505 *it6505,
3043 						       struct hdmi_codec_params
3044 						       *params)
3045 {
3046 	struct device *dev = it6505->dev;
3047 	int i = 0;
3048 
3049 	DRM_DEV_DEBUG_DRIVER(dev, "%s %d Hz, %d bit, %d channels\n", __func__,
3050 			     params->sample_rate, params->sample_width,
3051 			     params->cea.channels);
3052 
3053 	if (!it6505->bridge.encoder)
3054 		return -ENODEV;
3055 
3056 	if (params->cea.channels <= 1 || params->cea.channels > 8) {
3057 		DRM_DEV_DEBUG_DRIVER(dev, "channel number: %d not support",
3058 				     it6505->audio.channel_count);
3059 		return -EINVAL;
3060 	}
3061 
3062 	it6505->audio.channel_count = params->cea.channels;
3063 
3064 	while (i < ARRAY_SIZE(audio_sample_rate_map) &&
3065 	       params->sample_rate !=
3066 		       audio_sample_rate_map[i].sample_rate_value) {
3067 		i++;
3068 	}
3069 	if (i == ARRAY_SIZE(audio_sample_rate_map)) {
3070 		DRM_DEV_DEBUG_DRIVER(dev, "sample rate: %d Hz not support",
3071 				     params->sample_rate);
3072 		return -EINVAL;
3073 	}
3074 	it6505->audio.sample_rate = audio_sample_rate_map[i].rate;
3075 
3076 	switch (params->sample_width) {
3077 	case 16:
3078 		it6505->audio.word_length = WORD_LENGTH_16BIT;
3079 		break;
3080 	case 18:
3081 		it6505->audio.word_length = WORD_LENGTH_18BIT;
3082 		break;
3083 	case 20:
3084 		it6505->audio.word_length = WORD_LENGTH_20BIT;
3085 		break;
3086 	case 24:
3087 	case 32:
3088 		it6505->audio.word_length = WORD_LENGTH_24BIT;
3089 		break;
3090 	default:
3091 		DRM_DEV_DEBUG_DRIVER(dev, "wordlength: %d bit not support",
3092 				     params->sample_width);
3093 		return -EINVAL;
3094 	}
3095 
3096 	return 0;
3097 }
3098 
it6505_audio_shutdown(struct device * dev,void * data)3099 static void __maybe_unused it6505_audio_shutdown(struct device *dev, void *data)
3100 {
3101 	struct it6505 *it6505 = dev_get_drvdata(dev);
3102 
3103 	if (it6505->powered)
3104 		it6505_disable_audio(it6505);
3105 }
3106 
it6505_audio_hook_plugged_cb(struct device * dev,void * data,hdmi_codec_plugged_cb fn,struct device * codec_dev)3107 static int __maybe_unused it6505_audio_hook_plugged_cb(struct device *dev,
3108 						       void *data,
3109 						       hdmi_codec_plugged_cb fn,
3110 						       struct device *codec_dev)
3111 {
3112 	struct it6505 *it6505 = data;
3113 
3114 	it6505->plugged_cb = fn;
3115 	it6505->codec_dev = codec_dev;
3116 	it6505_plugged_status_to_codec(it6505);
3117 
3118 	return 0;
3119 }
3120 
bridge_to_it6505(struct drm_bridge * bridge)3121 static inline struct it6505 *bridge_to_it6505(struct drm_bridge *bridge)
3122 {
3123 	return container_of(bridge, struct it6505, bridge);
3124 }
3125 
it6505_bridge_attach(struct drm_bridge * bridge,enum drm_bridge_attach_flags flags)3126 static int it6505_bridge_attach(struct drm_bridge *bridge,
3127 				enum drm_bridge_attach_flags flags)
3128 {
3129 	struct it6505 *it6505 = bridge_to_it6505(bridge);
3130 	struct device *dev = it6505->dev;
3131 	int ret;
3132 
3133 	if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
3134 		DRM_ERROR("DRM_BRIDGE_ATTACH_NO_CONNECTOR must be supplied");
3135 		return -EINVAL;
3136 	}
3137 
3138 	/* Register aux channel */
3139 	it6505->aux.drm_dev = bridge->dev;
3140 
3141 	ret = drm_dp_aux_register(&it6505->aux);
3142 
3143 	if (ret < 0) {
3144 		dev_err(dev, "Failed to register aux: %d", ret);
3145 		return ret;
3146 	}
3147 
3148 	if (it6505->extcon) {
3149 		ret = it6505_use_notifier_module(it6505);
3150 		if (ret < 0) {
3151 			dev_err(dev, "use notifier module failed");
3152 			return ret;
3153 		}
3154 	}
3155 
3156 	return 0;
3157 }
3158 
it6505_bridge_detach(struct drm_bridge * bridge)3159 static void it6505_bridge_detach(struct drm_bridge *bridge)
3160 {
3161 	struct it6505 *it6505 = bridge_to_it6505(bridge);
3162 
3163 	flush_work(&it6505->link_works);
3164 	it6505_remove_notifier_module(it6505);
3165 }
3166 
3167 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)3168 it6505_bridge_mode_valid(struct drm_bridge *bridge,
3169 			 const struct drm_display_info *info,
3170 			 const struct drm_display_mode *mode)
3171 {
3172 	struct it6505 *it6505 = bridge_to_it6505(bridge);
3173 
3174 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
3175 		return MODE_NO_INTERLACE;
3176 
3177 	if (mode->clock > it6505->max_dpi_pixel_clock)
3178 		return MODE_CLOCK_HIGH;
3179 
3180 	it6505->video_info.clock = mode->clock;
3181 
3182 	return MODE_OK;
3183 }
3184 
it6505_bridge_atomic_enable(struct drm_bridge * bridge,struct drm_bridge_state * old_state)3185 static void it6505_bridge_atomic_enable(struct drm_bridge *bridge,
3186 					struct drm_bridge_state *old_state)
3187 {
3188 	struct it6505 *it6505 = bridge_to_it6505(bridge);
3189 	struct device *dev = it6505->dev;
3190 	struct drm_atomic_state *state = old_state->base.state;
3191 	struct hdmi_avi_infoframe frame;
3192 	struct drm_crtc_state *crtc_state;
3193 	struct drm_connector_state *conn_state;
3194 	struct drm_display_mode *mode;
3195 	struct drm_connector *connector;
3196 	int ret;
3197 
3198 	DRM_DEV_DEBUG_DRIVER(dev, "start");
3199 
3200 	connector = drm_atomic_get_new_connector_for_encoder(state,
3201 							     bridge->encoder);
3202 
3203 	if (WARN_ON(!connector))
3204 		return;
3205 
3206 	conn_state = drm_atomic_get_new_connector_state(state, connector);
3207 
3208 	if (WARN_ON(!conn_state))
3209 		return;
3210 
3211 	crtc_state = drm_atomic_get_new_crtc_state(state, conn_state->crtc);
3212 
3213 	if (WARN_ON(!crtc_state))
3214 		return;
3215 
3216 	mode = &crtc_state->adjusted_mode;
3217 
3218 	if (WARN_ON(!mode))
3219 		return;
3220 
3221 	ret = drm_hdmi_avi_infoframe_from_display_mode(&frame,
3222 						       connector,
3223 						       mode);
3224 	if (ret)
3225 		dev_err(dev, "Failed to setup AVI infoframe: %d", ret);
3226 
3227 	it6505_update_video_parameter(it6505, mode);
3228 
3229 	ret = it6505_send_video_infoframe(it6505, &frame);
3230 
3231 	if (ret)
3232 		dev_err(dev, "Failed to send AVI infoframe: %d", ret);
3233 
3234 	it6505_int_mask_enable(it6505);
3235 	it6505_video_reset(it6505);
3236 
3237 	it6505_drm_dp_link_set_power(&it6505->aux, &it6505->link,
3238 				     DP_SET_POWER_D0);
3239 }
3240 
it6505_bridge_atomic_disable(struct drm_bridge * bridge,struct drm_bridge_state * old_state)3241 static void it6505_bridge_atomic_disable(struct drm_bridge *bridge,
3242 					 struct drm_bridge_state *old_state)
3243 {
3244 	struct it6505 *it6505 = bridge_to_it6505(bridge);
3245 	struct device *dev = it6505->dev;
3246 
3247 	DRM_DEV_DEBUG_DRIVER(dev, "start");
3248 
3249 	if (it6505->powered) {
3250 		it6505_drm_dp_link_set_power(&it6505->aux, &it6505->link,
3251 					     DP_SET_POWER_D3);
3252 		it6505_video_disable(it6505);
3253 	}
3254 }
3255 
it6505_bridge_atomic_pre_enable(struct drm_bridge * bridge,struct drm_bridge_state * old_state)3256 static void it6505_bridge_atomic_pre_enable(struct drm_bridge *bridge,
3257 					    struct drm_bridge_state *old_state)
3258 {
3259 	struct it6505 *it6505 = bridge_to_it6505(bridge);
3260 	struct device *dev = it6505->dev;
3261 
3262 	DRM_DEV_DEBUG_DRIVER(dev, "start");
3263 
3264 	pm_runtime_get_sync(dev);
3265 }
3266 
it6505_bridge_atomic_post_disable(struct drm_bridge * bridge,struct drm_bridge_state * old_state)3267 static void it6505_bridge_atomic_post_disable(struct drm_bridge *bridge,
3268 					      struct drm_bridge_state *old_state)
3269 {
3270 	struct it6505 *it6505 = bridge_to_it6505(bridge);
3271 	struct device *dev = it6505->dev;
3272 
3273 	DRM_DEV_DEBUG_DRIVER(dev, "start");
3274 
3275 	pm_runtime_put_sync(dev);
3276 }
3277 
3278 static enum drm_connector_status
it6505_bridge_detect(struct drm_bridge * bridge)3279 it6505_bridge_detect(struct drm_bridge *bridge)
3280 {
3281 	struct it6505 *it6505 = bridge_to_it6505(bridge);
3282 
3283 	return it6505_detect(it6505);
3284 }
3285 
it6505_bridge_edid_read(struct drm_bridge * bridge,struct drm_connector * connector)3286 static const struct drm_edid *it6505_bridge_edid_read(struct drm_bridge *bridge,
3287 						      struct drm_connector *connector)
3288 {
3289 	struct it6505 *it6505 = bridge_to_it6505(bridge);
3290 	struct device *dev = it6505->dev;
3291 
3292 	if (!it6505->cached_edid) {
3293 		it6505->cached_edid = drm_edid_read_custom(connector,
3294 							   it6505_get_edid_block,
3295 							   it6505);
3296 
3297 		if (!it6505->cached_edid) {
3298 			DRM_DEV_DEBUG_DRIVER(dev, "failed to get edid!");
3299 			return NULL;
3300 		}
3301 	}
3302 
3303 	return drm_edid_dup(it6505->cached_edid);
3304 }
3305 
3306 static const struct drm_bridge_funcs it6505_bridge_funcs = {
3307 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
3308 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
3309 	.atomic_reset = drm_atomic_helper_bridge_reset,
3310 	.attach = it6505_bridge_attach,
3311 	.detach = it6505_bridge_detach,
3312 	.mode_valid = it6505_bridge_mode_valid,
3313 	.atomic_enable = it6505_bridge_atomic_enable,
3314 	.atomic_disable = it6505_bridge_atomic_disable,
3315 	.atomic_pre_enable = it6505_bridge_atomic_pre_enable,
3316 	.atomic_post_disable = it6505_bridge_atomic_post_disable,
3317 	.detect = it6505_bridge_detect,
3318 	.edid_read = it6505_bridge_edid_read,
3319 };
3320 
it6505_bridge_resume(struct device * dev)3321 static __maybe_unused int it6505_bridge_resume(struct device *dev)
3322 {
3323 	struct it6505 *it6505 = dev_get_drvdata(dev);
3324 
3325 	return it6505_poweron(it6505);
3326 }
3327 
it6505_bridge_suspend(struct device * dev)3328 static __maybe_unused int it6505_bridge_suspend(struct device *dev)
3329 {
3330 	struct it6505 *it6505 = dev_get_drvdata(dev);
3331 
3332 	it6505_remove_edid(it6505);
3333 
3334 	return it6505_poweroff(it6505);
3335 }
3336 
3337 static const struct dev_pm_ops it6505_bridge_pm_ops = {
3338 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)
3339 	SET_RUNTIME_PM_OPS(it6505_bridge_suspend, it6505_bridge_resume, NULL)
3340 };
3341 
it6505_init_pdata(struct it6505 * it6505)3342 static int it6505_init_pdata(struct it6505 *it6505)
3343 {
3344 	struct it6505_platform_data *pdata = &it6505->pdata;
3345 	struct device *dev = it6505->dev;
3346 
3347 	/* 1.0V digital core power regulator  */
3348 	pdata->pwr18 = devm_regulator_get(dev, "pwr18");
3349 	if (IS_ERR(pdata->pwr18)) {
3350 		dev_err(dev, "pwr18 regulator not found");
3351 		return PTR_ERR(pdata->pwr18);
3352 	}
3353 
3354 	pdata->ovdd = devm_regulator_get(dev, "ovdd");
3355 	if (IS_ERR(pdata->ovdd)) {
3356 		dev_err(dev, "ovdd regulator not found");
3357 		return PTR_ERR(pdata->ovdd);
3358 	}
3359 
3360 	pdata->gpiod_reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
3361 	if (IS_ERR(pdata->gpiod_reset)) {
3362 		dev_err(dev, "gpiod_reset gpio not found");
3363 		return PTR_ERR(pdata->gpiod_reset);
3364 	}
3365 
3366 	return 0;
3367 }
3368 
it6505_get_data_lanes_count(const struct device_node * endpoint,const unsigned int min,const unsigned int max)3369 static int it6505_get_data_lanes_count(const struct device_node *endpoint,
3370 				       const unsigned int min,
3371 				       const unsigned int max)
3372 {
3373 	int ret;
3374 
3375 	ret = of_property_count_u32_elems(endpoint, "data-lanes");
3376 	if (ret < 0)
3377 		return ret;
3378 
3379 	if (ret < min || ret > max)
3380 		return -EINVAL;
3381 
3382 	return ret;
3383 }
3384 
it6505_parse_dt(struct it6505 * it6505)3385 static void it6505_parse_dt(struct it6505 *it6505)
3386 {
3387 	struct device *dev = it6505->dev;
3388 	struct device_node *np = dev->of_node, *ep = NULL;
3389 	int len;
3390 	u64 link_frequencies;
3391 	u32 data_lanes[4];
3392 	u32 *afe_setting = &it6505->afe_setting;
3393 	u32 *max_lane_count = &it6505->max_lane_count;
3394 	u32 *max_dpi_pixel_clock = &it6505->max_dpi_pixel_clock;
3395 
3396 	it6505->lane_swap_disabled =
3397 		device_property_read_bool(dev, "no-laneswap");
3398 
3399 	if (it6505->lane_swap_disabled)
3400 		it6505->lane_swap = false;
3401 
3402 	if (device_property_read_u32(dev, "afe-setting", afe_setting) == 0) {
3403 		if (*afe_setting >= ARRAY_SIZE(afe_setting_table)) {
3404 			dev_err(dev, "afe setting error, use default");
3405 			*afe_setting = 0;
3406 		}
3407 	} else {
3408 		*afe_setting = 0;
3409 	}
3410 
3411 	ep = of_graph_get_endpoint_by_regs(np, 1, 0);
3412 	of_node_put(ep);
3413 
3414 	if (ep) {
3415 		len = it6505_get_data_lanes_count(ep, 1, 4);
3416 
3417 		if (len > 0 && len != 3) {
3418 			of_property_read_u32_array(ep, "data-lanes",
3419 						   data_lanes, len);
3420 			*max_lane_count = len;
3421 		} else {
3422 			*max_lane_count = MAX_LANE_COUNT;
3423 			dev_err(dev, "error data-lanes, use default");
3424 		}
3425 	} else {
3426 		*max_lane_count = MAX_LANE_COUNT;
3427 		dev_err(dev, "error endpoint, use default");
3428 	}
3429 
3430 	ep = of_graph_get_endpoint_by_regs(np, 0, 0);
3431 	of_node_put(ep);
3432 
3433 	if (ep) {
3434 		len = of_property_read_variable_u64_array(ep,
3435 							  "link-frequencies",
3436 							  &link_frequencies, 0,
3437 							  1);
3438 		if (len >= 0) {
3439 			do_div(link_frequencies, 1000);
3440 			if (link_frequencies > 297000) {
3441 				dev_err(dev,
3442 					"max pixel clock error, use default");
3443 				*max_dpi_pixel_clock = DPI_PIXEL_CLK_MAX;
3444 			} else {
3445 				*max_dpi_pixel_clock = link_frequencies;
3446 			}
3447 		} else {
3448 			dev_err(dev, "error link frequencies, use default");
3449 			*max_dpi_pixel_clock = DPI_PIXEL_CLK_MAX;
3450 		}
3451 	} else {
3452 		dev_err(dev, "error endpoint, use default");
3453 		*max_dpi_pixel_clock = DPI_PIXEL_CLK_MAX;
3454 	}
3455 
3456 	DRM_DEV_DEBUG_DRIVER(dev, "using afe_setting: %u, max_lane_count: %u",
3457 			     it6505->afe_setting, it6505->max_lane_count);
3458 	DRM_DEV_DEBUG_DRIVER(dev, "using max_dpi_pixel_clock: %u kHz",
3459 			     it6505->max_dpi_pixel_clock);
3460 }
3461 
receive_timing_debugfs_show(struct file * file,char __user * buf,size_t len,loff_t * ppos)3462 static ssize_t receive_timing_debugfs_show(struct file *file, char __user *buf,
3463 					   size_t len, loff_t *ppos)
3464 {
3465 	struct it6505 *it6505 = file->private_data;
3466 	struct drm_display_mode *vid;
3467 	u8 read_buf[READ_BUFFER_SIZE];
3468 	u8 *str = read_buf, *end = read_buf + READ_BUFFER_SIZE;
3469 	ssize_t ret, count;
3470 
3471 	if (!it6505)
3472 		return -ENODEV;
3473 
3474 	it6505_calc_video_info(it6505);
3475 	vid = &it6505->video_info;
3476 	str += scnprintf(str, end - str, "---video timing---\n");
3477 	str += scnprintf(str, end - str, "PCLK:%d.%03dMHz\n",
3478 			 vid->clock / 1000, vid->clock % 1000);
3479 	str += scnprintf(str, end - str, "HTotal:%d\n", vid->htotal);
3480 	str += scnprintf(str, end - str, "HActive:%d\n", vid->hdisplay);
3481 	str += scnprintf(str, end - str, "HFrontPorch:%d\n",
3482 			 vid->hsync_start - vid->hdisplay);
3483 	str += scnprintf(str, end - str, "HSyncWidth:%d\n",
3484 			 vid->hsync_end - vid->hsync_start);
3485 	str += scnprintf(str, end - str, "HBackPorch:%d\n",
3486 			 vid->htotal - vid->hsync_end);
3487 	str += scnprintf(str, end - str, "VTotal:%d\n", vid->vtotal);
3488 	str += scnprintf(str, end - str, "VActive:%d\n", vid->vdisplay);
3489 	str += scnprintf(str, end - str, "VFrontPorch:%d\n",
3490 			 vid->vsync_start - vid->vdisplay);
3491 	str += scnprintf(str, end - str, "VSyncWidth:%d\n",
3492 			 vid->vsync_end - vid->vsync_start);
3493 	str += scnprintf(str, end - str, "VBackPorch:%d\n",
3494 			 vid->vtotal - vid->vsync_end);
3495 
3496 	count = str - read_buf;
3497 	ret = simple_read_from_buffer(buf, len, ppos, read_buf, count);
3498 
3499 	return ret;
3500 }
3501 
force_power_on_off_debugfs_write(void * data,u64 value)3502 static int force_power_on_off_debugfs_write(void *data, u64 value)
3503 {
3504 	struct it6505 *it6505 = data;
3505 
3506 	if (!it6505)
3507 		return -ENODEV;
3508 
3509 	if (value)
3510 		it6505_poweron(it6505);
3511 	else
3512 		it6505_poweroff(it6505);
3513 
3514 	return 0;
3515 }
3516 
enable_drv_hold_debugfs_show(void * data,u64 * buf)3517 static int enable_drv_hold_debugfs_show(void *data, u64 *buf)
3518 {
3519 	struct it6505 *it6505 = data;
3520 
3521 	if (!it6505)
3522 		return -ENODEV;
3523 
3524 	*buf = it6505->enable_drv_hold;
3525 
3526 	return 0;
3527 }
3528 
enable_drv_hold_debugfs_write(void * data,u64 drv_hold)3529 static int enable_drv_hold_debugfs_write(void *data, u64 drv_hold)
3530 {
3531 	struct it6505 *it6505 = data;
3532 
3533 	if (!it6505)
3534 		return -ENODEV;
3535 
3536 	it6505->enable_drv_hold = drv_hold;
3537 
3538 	if (it6505->enable_drv_hold) {
3539 		it6505_int_mask_disable(it6505);
3540 	} else {
3541 		it6505_clear_int(it6505);
3542 		it6505_int_mask_enable(it6505);
3543 
3544 		if (it6505->powered) {
3545 			it6505->connector_status =
3546 					it6505_get_sink_hpd_status(it6505) ?
3547 					connector_status_connected :
3548 					connector_status_disconnected;
3549 		} else {
3550 			it6505->connector_status =
3551 					connector_status_disconnected;
3552 		}
3553 	}
3554 
3555 	return 0;
3556 }
3557 
3558 static const struct file_operations receive_timing_fops = {
3559 	.owner = THIS_MODULE,
3560 	.open = simple_open,
3561 	.read = receive_timing_debugfs_show,
3562 	.llseek = default_llseek,
3563 };
3564 
3565 DEFINE_DEBUGFS_ATTRIBUTE(fops_force_power, NULL,
3566 			 force_power_on_off_debugfs_write, "%llu\n");
3567 
3568 DEFINE_DEBUGFS_ATTRIBUTE(fops_enable_drv_hold, enable_drv_hold_debugfs_show,
3569 			 enable_drv_hold_debugfs_write, "%llu\n");
3570 
3571 static const struct debugfs_entries debugfs_entry[] = {
3572 	{ "receive_timing", &receive_timing_fops },
3573 	{ "force_power_on_off", &fops_force_power },
3574 	{ "enable_drv_hold", &fops_enable_drv_hold },
3575 	{ NULL, NULL },
3576 };
3577 
debugfs_create_files(struct it6505 * it6505)3578 static void debugfs_create_files(struct it6505 *it6505)
3579 {
3580 	int i = 0;
3581 
3582 	while (debugfs_entry[i].name && debugfs_entry[i].fops) {
3583 		debugfs_create_file(debugfs_entry[i].name, 0644,
3584 				    it6505->debugfs, it6505,
3585 				    debugfs_entry[i].fops);
3586 		i++;
3587 	}
3588 }
3589 
debugfs_init(struct it6505 * it6505)3590 static void debugfs_init(struct it6505 *it6505)
3591 {
3592 	struct device *dev = it6505->dev;
3593 
3594 	it6505->debugfs = debugfs_create_dir(DEBUGFS_DIR_NAME, NULL);
3595 
3596 	if (IS_ERR(it6505->debugfs)) {
3597 		dev_err(dev, "failed to create debugfs root");
3598 		return;
3599 	}
3600 
3601 	debugfs_create_files(it6505);
3602 }
3603 
it6505_debugfs_remove(struct it6505 * it6505)3604 static void it6505_debugfs_remove(struct it6505 *it6505)
3605 {
3606 	debugfs_remove_recursive(it6505->debugfs);
3607 }
3608 
it6505_shutdown(struct i2c_client * client)3609 static void it6505_shutdown(struct i2c_client *client)
3610 {
3611 	struct it6505 *it6505 = dev_get_drvdata(&client->dev);
3612 
3613 	if (it6505->powered)
3614 		it6505_lane_off(it6505);
3615 }
3616 
it6505_i2c_probe(struct i2c_client * client)3617 static int it6505_i2c_probe(struct i2c_client *client)
3618 {
3619 	struct it6505 *it6505;
3620 	struct device *dev = &client->dev;
3621 	struct extcon_dev *extcon;
3622 	int err;
3623 
3624 	it6505 = devm_kzalloc(&client->dev, sizeof(*it6505), GFP_KERNEL);
3625 	if (!it6505)
3626 		return -ENOMEM;
3627 
3628 	mutex_init(&it6505->extcon_lock);
3629 	mutex_init(&it6505->mode_lock);
3630 	mutex_init(&it6505->aux_lock);
3631 
3632 	it6505->bridge.of_node = client->dev.of_node;
3633 	it6505->connector_status = connector_status_disconnected;
3634 	it6505->dev = &client->dev;
3635 	i2c_set_clientdata(client, it6505);
3636 
3637 	/* get extcon device from DTS */
3638 	extcon = extcon_get_edev_by_phandle(dev, 0);
3639 	if (PTR_ERR(extcon) == -EPROBE_DEFER)
3640 		return -EPROBE_DEFER;
3641 	if (IS_ERR(extcon)) {
3642 		dev_err(dev, "can not get extcon device!");
3643 		return PTR_ERR(extcon);
3644 	}
3645 
3646 	it6505->extcon = extcon;
3647 
3648 	it6505->regmap = devm_regmap_init_i2c(client, &it6505_regmap_config);
3649 	if (IS_ERR(it6505->regmap)) {
3650 		dev_err(dev, "regmap i2c init failed");
3651 		err = PTR_ERR(it6505->regmap);
3652 		return err;
3653 	}
3654 
3655 	err = it6505_init_pdata(it6505);
3656 	if (err) {
3657 		dev_err(dev, "Failed to initialize pdata: %d", err);
3658 		return err;
3659 	}
3660 
3661 	it6505_parse_dt(it6505);
3662 
3663 	it6505->irq = client->irq;
3664 
3665 	if (!it6505->irq) {
3666 		dev_err(dev, "Failed to get INTP IRQ");
3667 		err = -ENODEV;
3668 		return err;
3669 	}
3670 
3671 	err = devm_request_threaded_irq(&client->dev, it6505->irq, NULL,
3672 					it6505_int_threaded_handler,
3673 					IRQF_TRIGGER_LOW | IRQF_ONESHOT |
3674 					IRQF_NO_AUTOEN,
3675 					"it6505-intp", it6505);
3676 	if (err) {
3677 		dev_err(dev, "Failed to request INTP threaded IRQ: %d", err);
3678 		return err;
3679 	}
3680 
3681 	INIT_WORK(&it6505->link_works, it6505_link_training_work);
3682 	INIT_WORK(&it6505->hdcp_wait_ksv_list, it6505_hdcp_wait_ksv_list);
3683 	INIT_DELAYED_WORK(&it6505->hdcp_work, it6505_hdcp_work);
3684 	init_completion(&it6505->extcon_completion);
3685 	memset(it6505->dpcd, 0, sizeof(it6505->dpcd));
3686 	it6505->powered = false;
3687 	it6505->enable_drv_hold = DEFAULT_DRV_HOLD;
3688 
3689 	if (DEFAULT_PWR_ON)
3690 		it6505_poweron(it6505);
3691 
3692 	DRM_DEV_DEBUG_DRIVER(dev, "it6505 device name: %s", dev_name(dev));
3693 	debugfs_init(it6505);
3694 	pm_runtime_enable(dev);
3695 
3696 	it6505->aux.name = "DP-AUX";
3697 	it6505->aux.dev = dev;
3698 	it6505->aux.transfer = it6505_aux_transfer;
3699 	drm_dp_aux_init(&it6505->aux);
3700 
3701 	it6505->bridge.funcs = &it6505_bridge_funcs;
3702 	it6505->bridge.type = DRM_MODE_CONNECTOR_DisplayPort;
3703 	it6505->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID |
3704 			     DRM_BRIDGE_OP_HPD;
3705 	drm_bridge_add(&it6505->bridge);
3706 
3707 	return 0;
3708 }
3709 
it6505_i2c_remove(struct i2c_client * client)3710 static void it6505_i2c_remove(struct i2c_client *client)
3711 {
3712 	struct it6505 *it6505 = i2c_get_clientdata(client);
3713 
3714 	drm_bridge_remove(&it6505->bridge);
3715 	drm_dp_aux_unregister(&it6505->aux);
3716 	it6505_debugfs_remove(it6505);
3717 	it6505_poweroff(it6505);
3718 	it6505_remove_edid(it6505);
3719 }
3720 
3721 static const struct i2c_device_id it6505_id[] = {
3722 	{ "it6505" },
3723 	{ }
3724 };
3725 
3726 MODULE_DEVICE_TABLE(i2c, it6505_id);
3727 
3728 static const struct of_device_id it6505_of_match[] = {
3729 	{ .compatible = "ite,it6505" },
3730 	{ }
3731 };
3732 MODULE_DEVICE_TABLE(of, it6505_of_match);
3733 
3734 static struct i2c_driver it6505_i2c_driver = {
3735 	.driver = {
3736 		.name = "it6505",
3737 		.of_match_table = it6505_of_match,
3738 		.pm = &it6505_bridge_pm_ops,
3739 	},
3740 	.probe = it6505_i2c_probe,
3741 	.remove = it6505_i2c_remove,
3742 	.shutdown = it6505_shutdown,
3743 	.id_table = it6505_id,
3744 };
3745 
3746 module_i2c_driver(it6505_i2c_driver);
3747 
3748 MODULE_AUTHOR("Allen Chen <[email protected]>");
3749 MODULE_DESCRIPTION("IT6505 DisplayPort Transmitter driver");
3750 MODULE_LICENSE("GPL v2");
3751