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