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