Linux Audio

Check our new training course

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