1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Analog Devices ADV7511 HDMI transmitter driver 4 * 5 * Copyright 2012 Analog Devices Inc. 6 */ 7 8 #include <linux/clk.h> 9 #include <linux/device.h> 10 #include <linux/gpio/consumer.h> 11 #include <linux/module.h> 12 #include <linux/of.h> 13 #include <linux/slab.h> 14 15 #include <sound/pcm.h> 16 17 #include <drm/drm_atomic.h> 18 #include <drm/drm_atomic_helper.h> 19 #include <drm/drm_bridge_connector.h> 20 #include <drm/drm_edid.h> 21 #include <drm/drm_of.h> 22 #include <drm/drm_print.h> 23 #include <drm/drm_probe_helper.h> 24 #include <drm/display/drm_hdmi_helper.h> 25 #include <drm/display/drm_hdmi_state_helper.h> 26 27 #include "adv7511.h" 28 29 /* ADI recommended values for proper operation. */ 30 static const struct reg_sequence adv7511_fixed_registers[] = { 31 { 0x98, 0x03 }, 32 { 0x9a, 0xe0 }, 33 { 0x9c, 0x30 }, 34 { 0x9d, 0x61 }, 35 { 0xa2, 0xa4 }, 36 { 0xa3, 0xa4 }, 37 { 0xe0, 0xd0 }, 38 { 0xf9, 0x00 }, 39 { 0x55, 0x02 }, 40 }; 41 42 /* ----------------------------------------------------------------------------- 43 * Register access 44 */ 45 46 static const uint8_t adv7511_register_defaults[] = { 47 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00 */ 48 0x00, 0x00, 0x01, 0x0e, 0xbc, 0x18, 0x01, 0x13, 49 0x25, 0x37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10 */ 50 0x46, 0x62, 0x04, 0xa8, 0x00, 0x00, 0x1c, 0x84, 51 0x1c, 0xbf, 0x04, 0xa8, 0x1e, 0x70, 0x02, 0x1e, /* 20 */ 52 0x00, 0x00, 0x04, 0xa8, 0x08, 0x12, 0x1b, 0xac, 53 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 30 */ 54 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0xb0, 55 0x00, 0x50, 0x90, 0x7e, 0x79, 0x70, 0x00, 0x00, /* 40 */ 56 0x00, 0xa8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 57 0x00, 0x00, 0x02, 0x0d, 0x00, 0x00, 0x00, 0x00, /* 50 */ 58 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 59 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 60 */ 60 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 61 0x01, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 70 */ 62 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 63 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 80 */ 64 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 65 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, /* 90 */ 66 0x0b, 0x02, 0x00, 0x18, 0x5a, 0x60, 0x00, 0x00, 67 0x00, 0x00, 0x80, 0x80, 0x08, 0x04, 0x00, 0x00, /* a0 */ 68 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x14, 69 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b0 */ 70 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 71 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c0 */ 72 0x00, 0x03, 0x00, 0x00, 0x02, 0x00, 0x01, 0x04, 73 0x30, 0xff, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, /* d0 */ 74 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x01, 75 0x80, 0x75, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, /* e0 */ 76 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 77 0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x11, 0x00, /* f0 */ 78 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 79 }; 80 81 static bool adv7511_register_volatile(struct device *dev, unsigned int reg) 82 { 83 switch (reg) { 84 case ADV7511_REG_CHIP_REVISION: 85 case ADV7511_REG_SPDIF_FREQ: 86 case ADV7511_REG_CTS_AUTOMATIC1: 87 case ADV7511_REG_CTS_AUTOMATIC2: 88 case ADV7511_REG_VIC_DETECTED: 89 case ADV7511_REG_VIC_SEND: 90 case ADV7511_REG_AUX_VIC_DETECTED: 91 case ADV7511_REG_STATUS: 92 case ADV7511_REG_GC(1): 93 case ADV7511_REG_INT(0): 94 case ADV7511_REG_INT(1): 95 case ADV7511_REG_PLL_STATUS: 96 case ADV7511_REG_AN(0): 97 case ADV7511_REG_AN(1): 98 case ADV7511_REG_AN(2): 99 case ADV7511_REG_AN(3): 100 case ADV7511_REG_AN(4): 101 case ADV7511_REG_AN(5): 102 case ADV7511_REG_AN(6): 103 case ADV7511_REG_AN(7): 104 case ADV7511_REG_HDCP_STATUS: 105 case ADV7511_REG_BCAPS: 106 case ADV7511_REG_BKSV(0): 107 case ADV7511_REG_BKSV(1): 108 case ADV7511_REG_BKSV(2): 109 case ADV7511_REG_BKSV(3): 110 case ADV7511_REG_BKSV(4): 111 case ADV7511_REG_DDC_STATUS: 112 case ADV7511_REG_EDID_READ_CTRL: 113 case ADV7511_REG_BSTATUS(0): 114 case ADV7511_REG_BSTATUS(1): 115 case ADV7511_REG_CHIP_ID_HIGH: 116 case ADV7511_REG_CHIP_ID_LOW: 117 return true; 118 } 119 120 return false; 121 } 122 123 static const struct regmap_config adv7511_regmap_config = { 124 .reg_bits = 8, 125 .val_bits = 8, 126 127 .max_register = 0xff, 128 .cache_type = REGCACHE_MAPLE, 129 .reg_defaults_raw = adv7511_register_defaults, 130 .num_reg_defaults_raw = ARRAY_SIZE(adv7511_register_defaults), 131 132 .volatile_reg = adv7511_register_volatile, 133 }; 134 135 static const struct regmap_config adv7511_packet_config = { 136 .reg_bits = 8, 137 .val_bits = 8, 138 139 .max_register = 0xff, 140 }; 141 142 /* ----------------------------------------------------------------------------- 143 * Hardware configuration 144 */ 145 146 static void adv7511_set_colormap(struct adv7511 *adv7511, bool enable, 147 const uint16_t *coeff, 148 unsigned int scaling_factor) 149 { 150 unsigned int i; 151 152 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1), 153 ADV7511_CSC_UPDATE_MODE, ADV7511_CSC_UPDATE_MODE); 154 155 if (enable) { 156 for (i = 0; i < 12; ++i) { 157 regmap_update_bits(adv7511->regmap, 158 ADV7511_REG_CSC_UPPER(i), 159 0x1f, coeff[i] >> 8); 160 regmap_write(adv7511->regmap, 161 ADV7511_REG_CSC_LOWER(i), 162 coeff[i] & 0xff); 163 } 164 } 165 166 if (enable) 167 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0), 168 0xe0, 0x80 | (scaling_factor << 5)); 169 else 170 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0), 171 0x80, 0x00); 172 173 regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1), 174 ADV7511_CSC_UPDATE_MODE, 0); 175 } 176 177 static int adv7511_packet_enable(struct adv7511 *adv7511, unsigned int packet) 178 { 179 if (packet & 0xff) 180 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0, 181 packet, 0xff); 182 183 if (packet & 0xff00) { 184 packet >>= 8; 185 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1, 186 packet, 0xff); 187 } 188 189 return 0; 190 } 191 192 static int adv7511_packet_disable(struct adv7511 *adv7511, unsigned int packet) 193 { 194 if (packet & 0xff) 195 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0, 196 packet, 0x00); 197 198 if (packet & 0xff00) { 199 packet >>= 8; 200 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1, 201 packet, 0x00); 202 } 203 204 return 0; 205 } 206 207 /* Coefficients for adv7511 color space conversion */ 208 static const uint16_t adv7511_csc_ycbcr_to_rgb[] = { 209 0x0734, 0x04ad, 0x0000, 0x1c1b, 210 0x1ddc, 0x04ad, 0x1f24, 0x0135, 211 0x0000, 0x04ad, 0x087c, 0x1b77, 212 }; 213 214 static void adv7511_set_config_csc(struct adv7511 *adv7511, 215 struct drm_connector *connector, 216 bool rgb) 217 { 218 struct adv7511_video_config config; 219 bool output_format_422, output_format_ycbcr; 220 unsigned int mode; 221 222 if (rgb) { 223 config.csc_enable = false; 224 output_format_422 = false; 225 output_format_ycbcr = false; 226 } else { 227 config.csc_scaling_factor = ADV7511_CSC_SCALING_4; 228 config.csc_coefficents = adv7511_csc_ycbcr_to_rgb; 229 230 if ((connector->display_info.color_formats & 231 DRM_COLOR_FORMAT_YCBCR422) && 232 connector->display_info.is_hdmi) { 233 config.csc_enable = false; 234 output_format_422 = true; 235 output_format_ycbcr = true; 236 } else { 237 config.csc_enable = true; 238 output_format_422 = false; 239 output_format_ycbcr = false; 240 } 241 } 242 243 if (connector->display_info.is_hdmi) 244 mode = ADV7511_HDMI_CFG_MODE_HDMI; 245 else 246 mode = ADV7511_HDMI_CFG_MODE_DVI; 247 248 adv7511_set_colormap(adv7511, config.csc_enable, 249 config.csc_coefficents, 250 config.csc_scaling_factor); 251 252 regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x81, 253 (output_format_422 << 7) | output_format_ycbcr); 254 255 regmap_update_bits(adv7511->regmap, ADV7511_REG_HDCP_HDMI_CFG, 256 ADV7511_HDMI_CFG_MODE_MASK, mode); 257 } 258 259 static void adv7511_set_link_config(struct adv7511 *adv7511, 260 const struct adv7511_link_config *config) 261 { 262 /* 263 * The input style values documented in the datasheet don't match the 264 * hardware register field values :-( 265 */ 266 static const unsigned int input_styles[4] = { 0, 2, 1, 3 }; 267 268 unsigned int clock_delay; 269 unsigned int color_depth; 270 unsigned int input_id; 271 272 clock_delay = (config->clock_delay + 1200) / 400; 273 color_depth = config->input_color_depth == 8 ? 3 274 : (config->input_color_depth == 10 ? 1 : 2); 275 276 /* TODO Support input ID 6 */ 277 if (config->input_colorspace != HDMI_COLORSPACE_YUV422) 278 input_id = config->input_clock == ADV7511_INPUT_CLOCK_DDR 279 ? 5 : 0; 280 else if (config->input_clock == ADV7511_INPUT_CLOCK_DDR) 281 input_id = config->embedded_sync ? 8 : 7; 282 else if (config->input_clock == ADV7511_INPUT_CLOCK_2X) 283 input_id = config->embedded_sync ? 4 : 3; 284 else 285 input_id = config->embedded_sync ? 2 : 1; 286 287 regmap_update_bits(adv7511->regmap, ADV7511_REG_I2C_FREQ_ID_CFG, 0xf, 288 input_id); 289 regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x7e, 290 (color_depth << 4) | 291 (input_styles[config->input_style] << 2)); 292 regmap_write(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG2, 293 config->input_justification << 3); 294 regmap_write(adv7511->regmap, ADV7511_REG_TIMING_GEN_SEQ, 295 config->sync_pulse << 2); 296 297 regmap_write(adv7511->regmap, 0xba, clock_delay << 5); 298 299 adv7511->embedded_sync = config->embedded_sync; 300 adv7511->hsync_polarity = config->hsync_polarity; 301 adv7511->vsync_polarity = config->vsync_polarity; 302 adv7511->rgb = config->input_colorspace == HDMI_COLORSPACE_RGB; 303 } 304 305 static void __adv7511_power_on(struct adv7511 *adv7511) 306 { 307 adv7511->current_edid_segment = -1; 308 309 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER, 310 ADV7511_POWER_POWER_DOWN, 0); 311 if (adv7511->i2c_main->irq) { 312 /* 313 * Documentation says the INT_ENABLE registers are reset in 314 * POWER_DOWN mode. My 7511w preserved the bits, however. 315 * Still, let's be safe and stick to the documentation. 316 */ 317 regmap_write(adv7511->regmap, ADV7511_REG_INT_ENABLE(0), 318 ADV7511_INT0_EDID_READY | ADV7511_INT0_HPD); 319 regmap_update_bits(adv7511->regmap, 320 ADV7511_REG_INT_ENABLE(1), 321 ADV7511_INT1_DDC_ERROR, 322 ADV7511_INT1_DDC_ERROR); 323 } 324 325 /* 326 * Per spec it is allowed to pulse the HPD signal to indicate that the 327 * EDID information has changed. Some monitors do this when they wakeup 328 * from standby or are enabled. When the HPD goes low the adv7511 is 329 * reset and the outputs are disabled which might cause the monitor to 330 * go to standby again. To avoid this we ignore the HPD pin for the 331 * first few seconds after enabling the output. On the other hand 332 * adv7535 require to enable HPD Override bit for proper HPD. 333 */ 334 if (adv7511->info->hpd_override_enable) 335 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2, 336 ADV7535_REG_POWER2_HPD_OVERRIDE, 337 ADV7535_REG_POWER2_HPD_OVERRIDE); 338 else 339 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2, 340 ADV7511_REG_POWER2_HPD_SRC_MASK, 341 ADV7511_REG_POWER2_HPD_SRC_NONE); 342 } 343 344 static void adv7511_power_on(struct adv7511 *adv7511) 345 { 346 __adv7511_power_on(adv7511); 347 348 /* 349 * Most of the registers are reset during power down or when HPD is low. 350 */ 351 regcache_sync(adv7511->regmap); 352 353 if (adv7511->info->has_dsi) 354 adv7533_dsi_power_on(adv7511); 355 adv7511->powered = true; 356 } 357 358 static void __adv7511_power_off(struct adv7511 *adv7511) 359 { 360 /* TODO: setup additional power down modes */ 361 if (adv7511->info->hpd_override_enable) 362 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2, 363 ADV7535_REG_POWER2_HPD_OVERRIDE, 0); 364 365 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER, 366 ADV7511_POWER_POWER_DOWN, 367 ADV7511_POWER_POWER_DOWN); 368 regmap_update_bits(adv7511->regmap, 369 ADV7511_REG_INT_ENABLE(1), 370 ADV7511_INT1_DDC_ERROR, 0); 371 regcache_mark_dirty(adv7511->regmap); 372 } 373 374 static void adv7511_power_off(struct adv7511 *adv7511) 375 { 376 __adv7511_power_off(adv7511); 377 if (adv7511->info->has_dsi) 378 adv7533_dsi_power_off(adv7511); 379 adv7511->powered = false; 380 } 381 382 /* ----------------------------------------------------------------------------- 383 * Interrupt and hotplug detection 384 */ 385 386 static bool adv7511_hpd(struct adv7511 *adv7511) 387 { 388 unsigned int irq0; 389 int ret; 390 391 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0); 392 if (ret < 0) 393 return false; 394 395 if (irq0 & ADV7511_INT0_HPD) { 396 regmap_write(adv7511->regmap, ADV7511_REG_INT(0), 397 ADV7511_INT0_HPD); 398 return true; 399 } 400 401 return false; 402 } 403 404 static void adv7511_hpd_work(struct work_struct *work) 405 { 406 struct adv7511 *adv7511 = container_of(work, struct adv7511, hpd_work); 407 enum drm_connector_status status; 408 unsigned int val; 409 int ret; 410 411 ret = regmap_read(adv7511->regmap, ADV7511_REG_STATUS, &val); 412 if (ret < 0) 413 status = connector_status_disconnected; 414 else if (val & ADV7511_STATUS_HPD) 415 status = connector_status_connected; 416 else 417 status = connector_status_disconnected; 418 419 /* 420 * The bridge resets its registers on unplug. So when we get a plug 421 * event and we're already supposed to be powered, cycle the bridge to 422 * restore its state. 423 */ 424 if (status == connector_status_connected && 425 adv7511->status == connector_status_disconnected && 426 adv7511->powered) { 427 regcache_mark_dirty(adv7511->regmap); 428 adv7511_power_on(adv7511); 429 } 430 431 if (adv7511->status != status) { 432 adv7511->status = status; 433 434 drm_bridge_hpd_notify(&adv7511->bridge, status); 435 } 436 } 437 438 static int adv7511_irq_process(struct adv7511 *adv7511, bool process_hpd) 439 { 440 unsigned int irq0, irq1; 441 int ret; 442 int cec_status = IRQ_NONE; 443 int irq_status = IRQ_NONE; 444 445 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0); 446 if (ret < 0) 447 return ret; 448 449 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(1), &irq1); 450 if (ret < 0) 451 return ret; 452 453 regmap_write(adv7511->regmap, ADV7511_REG_INT(0), irq0); 454 regmap_write(adv7511->regmap, ADV7511_REG_INT(1), irq1); 455 456 if (process_hpd && irq0 & ADV7511_INT0_HPD && adv7511->bridge.encoder) { 457 schedule_work(&adv7511->hpd_work); 458 irq_status = IRQ_HANDLED; 459 } 460 461 if (irq0 & ADV7511_INT0_EDID_READY || irq1 & ADV7511_INT1_DDC_ERROR) { 462 adv7511->edid_read = true; 463 464 if (adv7511->i2c_main->irq) 465 wake_up_all(&adv7511->wq); 466 irq_status = IRQ_HANDLED; 467 } 468 469 #ifdef CONFIG_DRM_I2C_ADV7511_CEC 470 cec_status = adv7511_cec_irq_process(adv7511, irq1); 471 #endif 472 473 /* If there is no IRQ to handle, exit indicating no IRQ data */ 474 if (irq_status == IRQ_HANDLED || cec_status == IRQ_HANDLED) 475 return IRQ_HANDLED; 476 477 return IRQ_NONE; 478 } 479 480 static irqreturn_t adv7511_irq_handler(int irq, void *devid) 481 { 482 struct adv7511 *adv7511 = devid; 483 int ret; 484 485 ret = adv7511_irq_process(adv7511, true); 486 return ret < 0 ? IRQ_NONE : ret; 487 } 488 489 /* ----------------------------------------------------------------------------- 490 * EDID retrieval 491 */ 492 493 static int adv7511_wait_for_edid(struct adv7511 *adv7511, int timeout) 494 { 495 int ret; 496 497 if (adv7511->i2c_main->irq) { 498 ret = wait_event_interruptible_timeout(adv7511->wq, 499 adv7511->edid_read, msecs_to_jiffies(timeout)); 500 } else { 501 for (; timeout > 0; timeout -= 25) { 502 ret = adv7511_irq_process(adv7511, false); 503 if (ret < 0) 504 break; 505 506 if (adv7511->edid_read) 507 break; 508 509 msleep(25); 510 } 511 } 512 513 return adv7511->edid_read ? 0 : -EIO; 514 } 515 516 static int adv7511_get_edid_block(void *data, u8 *buf, unsigned int block, 517 size_t len) 518 { 519 struct adv7511 *adv7511 = data; 520 struct i2c_msg xfer[2]; 521 uint8_t offset; 522 unsigned int i; 523 int ret; 524 525 if (len > 128) 526 return -EINVAL; 527 528 if (adv7511->current_edid_segment != block / 2) { 529 unsigned int status; 530 531 ret = regmap_read(adv7511->regmap, ADV7511_REG_DDC_STATUS, 532 &status); 533 if (ret < 0) 534 return ret; 535 536 if (status != 2) { 537 adv7511->edid_read = false; 538 regmap_write(adv7511->regmap, ADV7511_REG_EDID_SEGMENT, 539 block); 540 ret = adv7511_wait_for_edid(adv7511, 200); 541 if (ret < 0) 542 return ret; 543 } 544 545 /* Break this apart, hopefully more I2C controllers will 546 * support 64 byte transfers than 256 byte transfers 547 */ 548 549 xfer[0].addr = adv7511->i2c_edid->addr; 550 xfer[0].flags = 0; 551 xfer[0].len = 1; 552 xfer[0].buf = &offset; 553 xfer[1].addr = adv7511->i2c_edid->addr; 554 xfer[1].flags = I2C_M_RD; 555 xfer[1].len = 64; 556 xfer[1].buf = adv7511->edid_buf; 557 558 offset = 0; 559 560 for (i = 0; i < 4; ++i) { 561 ret = i2c_transfer(adv7511->i2c_edid->adapter, xfer, 562 ARRAY_SIZE(xfer)); 563 if (ret < 0) 564 return ret; 565 else if (ret != 2) 566 return -EIO; 567 568 xfer[1].buf += 64; 569 offset += 64; 570 } 571 572 adv7511->current_edid_segment = block / 2; 573 } 574 575 if (block % 2 == 0) 576 memcpy(buf, adv7511->edid_buf, len); 577 else 578 memcpy(buf, adv7511->edid_buf + 128, len); 579 580 return 0; 581 } 582 583 /* ----------------------------------------------------------------------------- 584 * ADV75xx helpers 585 */ 586 587 static const struct drm_edid *adv7511_edid_read(struct adv7511 *adv7511, 588 struct drm_connector *connector) 589 { 590 const struct drm_edid *drm_edid; 591 592 /* Reading the EDID only works if the device is powered */ 593 if (!adv7511->powered) { 594 unsigned int edid_i2c_addr = 595 (adv7511->i2c_edid->addr << 1); 596 597 __adv7511_power_on(adv7511); 598 599 /* Reset the EDID_I2C_ADDR register as it might be cleared */ 600 regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR, 601 edid_i2c_addr); 602 } 603 604 drm_edid = drm_edid_read_custom(connector, adv7511_get_edid_block, adv7511); 605 606 if (!adv7511->powered) 607 __adv7511_power_off(adv7511); 608 609 return drm_edid; 610 } 611 612 static enum drm_connector_status 613 adv7511_detect(struct adv7511 *adv7511) 614 { 615 enum drm_connector_status status; 616 unsigned int val; 617 bool hpd; 618 int ret; 619 620 ret = regmap_read(adv7511->regmap, ADV7511_REG_STATUS, &val); 621 if (ret < 0) 622 return connector_status_disconnected; 623 624 if (val & ADV7511_STATUS_HPD) 625 status = connector_status_connected; 626 else 627 status = connector_status_disconnected; 628 629 hpd = adv7511_hpd(adv7511); 630 631 /* The chip resets itself when the cable is disconnected, so in case 632 * there is a pending HPD interrupt and the cable is connected there was 633 * at least one transition from disconnected to connected and the chip 634 * has to be reinitialized. */ 635 if (status == connector_status_connected && hpd && adv7511->powered) { 636 regcache_mark_dirty(adv7511->regmap); 637 adv7511_power_on(adv7511); 638 if (adv7511->status == connector_status_connected) 639 status = connector_status_disconnected; 640 } else { 641 /* Renable HPD sensing */ 642 if (adv7511->info->hpd_override_enable) 643 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2, 644 ADV7535_REG_POWER2_HPD_OVERRIDE, 645 ADV7535_REG_POWER2_HPD_OVERRIDE); 646 else 647 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2, 648 ADV7511_REG_POWER2_HPD_SRC_MASK, 649 ADV7511_REG_POWER2_HPD_SRC_BOTH); 650 } 651 652 adv7511->status = status; 653 return status; 654 } 655 656 static void adv7511_mode_set(struct adv7511 *adv7511, 657 const struct drm_display_mode *adj_mode) 658 { 659 unsigned int low_refresh_rate; 660 unsigned int hsync_polarity = 0; 661 unsigned int vsync_polarity = 0; 662 663 if (adv7511->embedded_sync) { 664 unsigned int hsync_offset, hsync_len; 665 unsigned int vsync_offset, vsync_len; 666 667 hsync_offset = adj_mode->crtc_hsync_start - 668 adj_mode->crtc_hdisplay; 669 vsync_offset = adj_mode->crtc_vsync_start - 670 adj_mode->crtc_vdisplay; 671 hsync_len = adj_mode->crtc_hsync_end - 672 adj_mode->crtc_hsync_start; 673 vsync_len = adj_mode->crtc_vsync_end - 674 adj_mode->crtc_vsync_start; 675 676 /* The hardware vsync generator has a off-by-one bug */ 677 vsync_offset += 1; 678 679 regmap_write(adv7511->regmap, ADV7511_REG_HSYNC_PLACEMENT_MSB, 680 ((hsync_offset >> 10) & 0x7) << 5); 681 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(0), 682 (hsync_offset >> 2) & 0xff); 683 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(1), 684 ((hsync_offset & 0x3) << 6) | 685 ((hsync_len >> 4) & 0x3f)); 686 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(2), 687 ((hsync_len & 0xf) << 4) | 688 ((vsync_offset >> 6) & 0xf)); 689 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(3), 690 ((vsync_offset & 0x3f) << 2) | 691 ((vsync_len >> 8) & 0x3)); 692 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(4), 693 vsync_len & 0xff); 694 695 hsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PHSYNC); 696 vsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PVSYNC); 697 } else { 698 enum adv7511_sync_polarity mode_hsync_polarity; 699 enum adv7511_sync_polarity mode_vsync_polarity; 700 701 /** 702 * If the input signal is always low or always high we want to 703 * invert or let it passthrough depending on the polarity of the 704 * current mode. 705 **/ 706 if (adj_mode->flags & DRM_MODE_FLAG_NHSYNC) 707 mode_hsync_polarity = ADV7511_SYNC_POLARITY_LOW; 708 else 709 mode_hsync_polarity = ADV7511_SYNC_POLARITY_HIGH; 710 711 if (adj_mode->flags & DRM_MODE_FLAG_NVSYNC) 712 mode_vsync_polarity = ADV7511_SYNC_POLARITY_LOW; 713 else 714 mode_vsync_polarity = ADV7511_SYNC_POLARITY_HIGH; 715 716 if (adv7511->hsync_polarity != mode_hsync_polarity && 717 adv7511->hsync_polarity != 718 ADV7511_SYNC_POLARITY_PASSTHROUGH) 719 hsync_polarity = 1; 720 721 if (adv7511->vsync_polarity != mode_vsync_polarity && 722 adv7511->vsync_polarity != 723 ADV7511_SYNC_POLARITY_PASSTHROUGH) 724 vsync_polarity = 1; 725 } 726 727 if (drm_mode_vrefresh(adj_mode) <= 24) 728 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_24HZ; 729 else if (drm_mode_vrefresh(adj_mode) <= 25) 730 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_25HZ; 731 else if (drm_mode_vrefresh(adj_mode) <= 30) 732 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_30HZ; 733 else 734 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_NONE; 735 736 if (adv7511->info->type == ADV7511) 737 regmap_update_bits(adv7511->regmap, 0xfb, 738 0x6, low_refresh_rate << 1); 739 else 740 regmap_update_bits(adv7511->regmap, 0x4a, 741 0xc, low_refresh_rate << 2); 742 743 regmap_update_bits(adv7511->regmap, 0x17, 744 0x60, (vsync_polarity << 6) | (hsync_polarity << 5)); 745 746 drm_mode_copy(&adv7511->curr_mode, adj_mode); 747 748 /* Update horizontal/vertical porch params */ 749 if (adv7511->info->has_dsi && adv7511->use_timing_gen) 750 adv7533_dsi_config_timing_gen(adv7511); 751 752 /* 753 * TODO Test first order 4:2:2 to 4:4:4 up conversion method, which is 754 * supposed to give better results. 755 */ 756 757 adv7511->f_tmds = adj_mode->clock; 758 } 759 760 static int adv7511_connector_init(struct adv7511 *adv) 761 { 762 struct drm_bridge *bridge = &adv->bridge; 763 struct drm_connector *connector; 764 765 connector = drm_bridge_connector_init(bridge->dev, bridge->encoder); 766 if (IS_ERR(connector)) { 767 DRM_ERROR("Failed to initialize connector with drm\n"); 768 return PTR_ERR(connector); 769 } 770 771 drm_connector_attach_encoder(connector, bridge->encoder); 772 773 return 0; 774 } 775 776 /* ----------------------------------------------------------------------------- 777 * DRM Bridge Operations 778 */ 779 780 static const struct adv7511 *bridge_to_adv7511_const(const struct drm_bridge *bridge) 781 { 782 return container_of(bridge, struct adv7511, bridge); 783 } 784 785 static void adv7511_bridge_atomic_enable(struct drm_bridge *bridge, 786 struct drm_atomic_state *state) 787 { 788 struct adv7511 *adv = bridge_to_adv7511(bridge); 789 struct drm_connector *connector; 790 struct drm_connector_state *conn_state; 791 struct drm_crtc_state *crtc_state; 792 793 adv7511_power_on(adv); 794 795 connector = drm_atomic_get_new_connector_for_encoder(state, bridge->encoder); 796 if (WARN_ON(!connector)) 797 return; 798 799 conn_state = drm_atomic_get_new_connector_state(state, connector); 800 if (WARN_ON(!conn_state)) 801 return; 802 803 crtc_state = drm_atomic_get_new_crtc_state(state, conn_state->crtc); 804 if (WARN_ON(!crtc_state)) 805 return; 806 807 adv7511_set_config_csc(adv, connector, adv->rgb); 808 809 adv7511_mode_set(adv, &crtc_state->adjusted_mode); 810 811 drm_atomic_helper_connector_hdmi_update_infoframes(connector, state); 812 } 813 814 static void adv7511_bridge_atomic_disable(struct drm_bridge *bridge, 815 struct drm_atomic_state *state) 816 { 817 struct adv7511 *adv = bridge_to_adv7511(bridge); 818 819 adv7511_power_off(adv); 820 } 821 822 static enum drm_mode_status 823 adv7511_bridge_hdmi_tmds_char_rate_valid(const struct drm_bridge *bridge, 824 const struct drm_display_mode *mode, 825 unsigned long long tmds_rate) 826 { 827 const struct adv7511 *adv = bridge_to_adv7511_const(bridge); 828 829 if (tmds_rate > 1000ULL * adv->info->max_mode_clock_khz) 830 return MODE_CLOCK_HIGH; 831 832 return MODE_OK; 833 } 834 835 static enum drm_mode_status adv7511_bridge_mode_valid(struct drm_bridge *bridge, 836 const struct drm_display_info *info, 837 const struct drm_display_mode *mode) 838 { 839 struct adv7511 *adv = bridge_to_adv7511(bridge); 840 841 if (!adv->info->has_dsi) 842 return MODE_OK; 843 844 return adv7533_mode_valid(adv, mode); 845 } 846 847 static int adv7511_bridge_attach(struct drm_bridge *bridge, 848 struct drm_encoder *encoder, 849 enum drm_bridge_attach_flags flags) 850 { 851 struct adv7511 *adv = bridge_to_adv7511(bridge); 852 int ret = 0; 853 854 if (adv->next_bridge) { 855 ret = drm_bridge_attach(encoder, adv->next_bridge, bridge, 856 flags | DRM_BRIDGE_ATTACH_NO_CONNECTOR); 857 if (ret) 858 return ret; 859 } 860 861 if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) { 862 ret = adv7511_connector_init(adv); 863 if (ret < 0) 864 return ret; 865 } 866 867 if (adv->i2c_main->irq) 868 regmap_write(adv->regmap, ADV7511_REG_INT_ENABLE(0), 869 ADV7511_INT0_HPD); 870 871 return ret; 872 } 873 874 static enum drm_connector_status 875 adv7511_bridge_detect(struct drm_bridge *bridge, struct drm_connector *connector) 876 { 877 struct adv7511 *adv = bridge_to_adv7511(bridge); 878 879 return adv7511_detect(adv); 880 } 881 882 static const struct drm_edid *adv7511_bridge_edid_read(struct drm_bridge *bridge, 883 struct drm_connector *connector) 884 { 885 struct adv7511 *adv = bridge_to_adv7511(bridge); 886 887 return adv7511_edid_read(adv, connector); 888 } 889 890 static int adv7511_bridge_hdmi_clear_infoframe(struct drm_bridge *bridge, 891 enum hdmi_infoframe_type type) 892 { 893 struct adv7511 *adv7511 = bridge_to_adv7511(bridge); 894 895 switch (type) { 896 case HDMI_INFOFRAME_TYPE_AVI: 897 adv7511_packet_disable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME); 898 break; 899 case HDMI_INFOFRAME_TYPE_SPD: 900 adv7511_packet_disable(adv7511, ADV7511_PACKET_ENABLE_SPD); 901 break; 902 case HDMI_INFOFRAME_TYPE_VENDOR: 903 adv7511_packet_disable(adv7511, ADV7511_PACKET_ENABLE_SPARE1); 904 break; 905 default: 906 drm_dbg_driver(adv7511->bridge.dev, "Unsupported HDMI InfoFrame %x\n", type); 907 break; 908 } 909 910 return 0; 911 } 912 913 static int adv7511_bridge_hdmi_write_infoframe(struct drm_bridge *bridge, 914 enum hdmi_infoframe_type type, 915 const u8 *buffer, size_t len) 916 { 917 struct adv7511 *adv7511 = bridge_to_adv7511(bridge); 918 919 adv7511_bridge_hdmi_clear_infoframe(bridge, type); 920 921 switch (type) { 922 case HDMI_INFOFRAME_TYPE_AVI: 923 /* The AVI infoframe id is not configurable */ 924 regmap_bulk_write(adv7511->regmap, ADV7511_REG_AVI_INFOFRAME_VERSION, 925 buffer + 1, len - 1); 926 927 adv7511_packet_enable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME); 928 break; 929 case HDMI_INFOFRAME_TYPE_SPD: 930 regmap_bulk_write(adv7511->regmap_packet, ADV7511_PACKET_SPD(0), 931 buffer, len); 932 adv7511_packet_enable(adv7511, ADV7511_PACKET_ENABLE_SPD); 933 break; 934 case HDMI_INFOFRAME_TYPE_VENDOR: 935 regmap_bulk_write(adv7511->regmap_packet, ADV7511_PACKET_SPARE1(0), 936 buffer, len); 937 adv7511_packet_enable(adv7511, ADV7511_PACKET_ENABLE_SPARE1); 938 break; 939 default: 940 drm_dbg_driver(adv7511->bridge.dev, "Unsupported HDMI InfoFrame %x\n", type); 941 break; 942 } 943 944 return 0; 945 } 946 947 static const struct drm_bridge_funcs adv7511_bridge_funcs = { 948 .mode_valid = adv7511_bridge_mode_valid, 949 .attach = adv7511_bridge_attach, 950 .detect = adv7511_bridge_detect, 951 .edid_read = adv7511_bridge_edid_read, 952 953 .atomic_enable = adv7511_bridge_atomic_enable, 954 .atomic_disable = adv7511_bridge_atomic_disable, 955 .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state, 956 .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state, 957 .atomic_reset = drm_atomic_helper_bridge_reset, 958 959 .hdmi_tmds_char_rate_valid = adv7511_bridge_hdmi_tmds_char_rate_valid, 960 .hdmi_clear_infoframe = adv7511_bridge_hdmi_clear_infoframe, 961 .hdmi_write_infoframe = adv7511_bridge_hdmi_write_infoframe, 962 963 .hdmi_audio_startup = adv7511_hdmi_audio_startup, 964 .hdmi_audio_prepare = adv7511_hdmi_audio_prepare, 965 .hdmi_audio_shutdown = adv7511_hdmi_audio_shutdown, 966 967 .hdmi_cec_init = adv7511_cec_init, 968 .hdmi_cec_enable = adv7511_cec_enable, 969 .hdmi_cec_log_addr = adv7511_cec_log_addr, 970 .hdmi_cec_transmit = adv7511_cec_transmit, 971 }; 972 973 /* ----------------------------------------------------------------------------- 974 * Probe & remove 975 */ 976 977 static const char * const adv7511_supply_names[] = { 978 "avdd", 979 "dvdd", 980 "pvdd", 981 "bgvdd", 982 "dvdd-3v", 983 }; 984 985 static const char * const adv7533_supply_names[] = { 986 "avdd", 987 "dvdd", 988 "pvdd", 989 "a2vdd", 990 "v3p3", 991 "v1p2", 992 }; 993 994 static int adv7511_init_regulators(struct adv7511 *adv) 995 { 996 const char * const *supply_names = adv->info->supply_names; 997 unsigned int num_supplies = adv->info->num_supplies; 998 struct device *dev = &adv->i2c_main->dev; 999 unsigned int i; 1000 int ret; 1001 1002 adv->supplies = devm_kcalloc(dev, num_supplies, 1003 sizeof(*adv->supplies), GFP_KERNEL); 1004 if (!adv->supplies) 1005 return -ENOMEM; 1006 1007 for (i = 0; i < num_supplies; i++) 1008 adv->supplies[i].supply = supply_names[i]; 1009 1010 ret = devm_regulator_bulk_get(dev, num_supplies, adv->supplies); 1011 if (ret) 1012 return ret; 1013 1014 return regulator_bulk_enable(num_supplies, adv->supplies); 1015 } 1016 1017 static void adv7511_uninit_regulators(struct adv7511 *adv) 1018 { 1019 regulator_bulk_disable(adv->info->num_supplies, adv->supplies); 1020 } 1021 1022 static bool adv7511_cec_register_volatile(struct device *dev, unsigned int reg) 1023 { 1024 struct i2c_client *i2c = to_i2c_client(dev); 1025 struct adv7511 *adv7511 = i2c_get_clientdata(i2c); 1026 1027 reg -= adv7511->info->reg_cec_offset; 1028 1029 switch (reg) { 1030 case ADV7511_REG_CEC_RX1_FRAME_HDR: 1031 case ADV7511_REG_CEC_RX1_FRAME_DATA0 ... ADV7511_REG_CEC_RX1_FRAME_DATA0 + 14: 1032 case ADV7511_REG_CEC_RX1_FRAME_LEN: 1033 case ADV7511_REG_CEC_RX2_FRAME_HDR: 1034 case ADV7511_REG_CEC_RX2_FRAME_DATA0 ... ADV7511_REG_CEC_RX2_FRAME_DATA0 + 14: 1035 case ADV7511_REG_CEC_RX2_FRAME_LEN: 1036 case ADV7511_REG_CEC_RX3_FRAME_HDR: 1037 case ADV7511_REG_CEC_RX3_FRAME_DATA0 ... ADV7511_REG_CEC_RX3_FRAME_DATA0 + 14: 1038 case ADV7511_REG_CEC_RX3_FRAME_LEN: 1039 case ADV7511_REG_CEC_RX_STATUS: 1040 case ADV7511_REG_CEC_RX_BUFFERS: 1041 case ADV7511_REG_CEC_TX_LOW_DRV_CNT: 1042 return true; 1043 } 1044 1045 return false; 1046 } 1047 1048 static const struct regmap_config adv7511_cec_regmap_config = { 1049 .reg_bits = 8, 1050 .val_bits = 8, 1051 1052 .max_register = 0xff, 1053 .cache_type = REGCACHE_MAPLE, 1054 .volatile_reg = adv7511_cec_register_volatile, 1055 }; 1056 1057 static int adv7511_init_cec_regmap(struct adv7511 *adv) 1058 { 1059 int ret; 1060 1061 adv->i2c_cec = i2c_new_ancillary_device(adv->i2c_main, "cec", 1062 ADV7511_CEC_I2C_ADDR_DEFAULT); 1063 if (IS_ERR(adv->i2c_cec)) 1064 return PTR_ERR(adv->i2c_cec); 1065 1066 regmap_write(adv->regmap, ADV7511_REG_CEC_I2C_ADDR, 1067 adv->i2c_cec->addr << 1); 1068 1069 i2c_set_clientdata(adv->i2c_cec, adv); 1070 1071 adv->regmap_cec = devm_regmap_init_i2c(adv->i2c_cec, 1072 &adv7511_cec_regmap_config); 1073 if (IS_ERR(adv->regmap_cec)) { 1074 ret = PTR_ERR(adv->regmap_cec); 1075 goto err; 1076 } 1077 1078 if (adv->info->reg_cec_offset == ADV7533_REG_CEC_OFFSET) { 1079 ret = adv7533_patch_cec_registers(adv); 1080 if (ret) 1081 goto err; 1082 } 1083 1084 return 0; 1085 err: 1086 i2c_unregister_device(adv->i2c_cec); 1087 return ret; 1088 } 1089 1090 static int adv7511_parse_dt(struct device_node *np, 1091 struct adv7511_link_config *config) 1092 { 1093 const char *str; 1094 int ret; 1095 1096 of_property_read_u32(np, "adi,input-depth", &config->input_color_depth); 1097 if (config->input_color_depth != 8 && config->input_color_depth != 10 && 1098 config->input_color_depth != 12) 1099 return -EINVAL; 1100 1101 ret = of_property_read_string(np, "adi,input-colorspace", &str); 1102 if (ret < 0) 1103 return ret; 1104 1105 if (!strcmp(str, "rgb")) 1106 config->input_colorspace = HDMI_COLORSPACE_RGB; 1107 else if (!strcmp(str, "yuv422")) 1108 config->input_colorspace = HDMI_COLORSPACE_YUV422; 1109 else if (!strcmp(str, "yuv444")) 1110 config->input_colorspace = HDMI_COLORSPACE_YUV444; 1111 else 1112 return -EINVAL; 1113 1114 ret = of_property_read_string(np, "adi,input-clock", &str); 1115 if (ret < 0) 1116 return ret; 1117 1118 if (!strcmp(str, "1x")) 1119 config->input_clock = ADV7511_INPUT_CLOCK_1X; 1120 else if (!strcmp(str, "2x")) 1121 config->input_clock = ADV7511_INPUT_CLOCK_2X; 1122 else if (!strcmp(str, "ddr")) 1123 config->input_clock = ADV7511_INPUT_CLOCK_DDR; 1124 else 1125 return -EINVAL; 1126 1127 if (config->input_colorspace == HDMI_COLORSPACE_YUV422 || 1128 config->input_clock != ADV7511_INPUT_CLOCK_1X) { 1129 ret = of_property_read_u32(np, "adi,input-style", 1130 &config->input_style); 1131 if (ret) 1132 return ret; 1133 1134 if (config->input_style < 1 || config->input_style > 3) 1135 return -EINVAL; 1136 1137 ret = of_property_read_string(np, "adi,input-justification", 1138 &str); 1139 if (ret < 0) 1140 return ret; 1141 1142 if (!strcmp(str, "left")) 1143 config->input_justification = 1144 ADV7511_INPUT_JUSTIFICATION_LEFT; 1145 else if (!strcmp(str, "evenly")) 1146 config->input_justification = 1147 ADV7511_INPUT_JUSTIFICATION_EVENLY; 1148 else if (!strcmp(str, "right")) 1149 config->input_justification = 1150 ADV7511_INPUT_JUSTIFICATION_RIGHT; 1151 else 1152 return -EINVAL; 1153 1154 } else { 1155 config->input_style = 1; 1156 config->input_justification = ADV7511_INPUT_JUSTIFICATION_LEFT; 1157 } 1158 1159 of_property_read_u32(np, "adi,clock-delay", &config->clock_delay); 1160 if (config->clock_delay < -1200 || config->clock_delay > 1600) 1161 return -EINVAL; 1162 1163 config->embedded_sync = of_property_read_bool(np, "adi,embedded-sync"); 1164 1165 /* Hardcode the sync pulse configurations for now. */ 1166 config->sync_pulse = ADV7511_INPUT_SYNC_PULSE_NONE; 1167 config->vsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH; 1168 config->hsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH; 1169 1170 return 0; 1171 } 1172 1173 static int adv7511_probe(struct i2c_client *i2c) 1174 { 1175 struct adv7511_link_config link_config; 1176 struct adv7511 *adv7511; 1177 struct device *dev = &i2c->dev; 1178 unsigned int val; 1179 int ret; 1180 1181 if (!dev->of_node) 1182 return -EINVAL; 1183 1184 adv7511 = devm_drm_bridge_alloc(dev, struct adv7511, bridge, 1185 &adv7511_bridge_funcs); 1186 if (IS_ERR(adv7511)) 1187 return PTR_ERR(adv7511); 1188 1189 adv7511->i2c_main = i2c; 1190 adv7511->powered = false; 1191 adv7511->status = connector_status_disconnected; 1192 adv7511->info = i2c_get_match_data(i2c); 1193 1194 memset(&link_config, 0, sizeof(link_config)); 1195 1196 ret = drm_of_find_panel_or_bridge(dev->of_node, 1, -1, NULL, 1197 &adv7511->next_bridge); 1198 if (ret && ret != -ENODEV) 1199 return ret; 1200 1201 if (adv7511->info->link_config) 1202 ret = adv7511_parse_dt(dev->of_node, &link_config); 1203 else 1204 ret = adv7533_parse_dt(dev->of_node, adv7511); 1205 if (ret) 1206 return ret; 1207 1208 ret = adv7511_init_regulators(adv7511); 1209 if (ret) { 1210 dev_err_probe(dev, ret, "failed to init regulators\n"); 1211 goto err_of_node_put; 1212 } 1213 1214 /* 1215 * The power down GPIO is optional. If present, toggle it from active to 1216 * inactive to wake up the encoder. 1217 */ 1218 adv7511->gpio_pd = devm_gpiod_get_optional(dev, "pd", GPIOD_OUT_HIGH); 1219 if (IS_ERR(adv7511->gpio_pd)) { 1220 ret = PTR_ERR(adv7511->gpio_pd); 1221 goto uninit_regulators; 1222 } 1223 1224 if (adv7511->gpio_pd) { 1225 usleep_range(5000, 6000); 1226 gpiod_set_value_cansleep(adv7511->gpio_pd, 0); 1227 } 1228 1229 adv7511->regmap = devm_regmap_init_i2c(i2c, &adv7511_regmap_config); 1230 if (IS_ERR(adv7511->regmap)) { 1231 ret = PTR_ERR(adv7511->regmap); 1232 goto uninit_regulators; 1233 } 1234 1235 ret = regmap_read(adv7511->regmap, ADV7511_REG_CHIP_REVISION, &val); 1236 if (ret) 1237 goto uninit_regulators; 1238 dev_dbg(dev, "Rev. %d\n", val); 1239 1240 if (adv7511->info->type == ADV7511) 1241 ret = regmap_register_patch(adv7511->regmap, 1242 adv7511_fixed_registers, 1243 ARRAY_SIZE(adv7511_fixed_registers)); 1244 else 1245 ret = adv7533_patch_registers(adv7511); 1246 if (ret) 1247 goto uninit_regulators; 1248 1249 adv7511_packet_disable(adv7511, 0xffff); 1250 1251 adv7511->i2c_edid = i2c_new_ancillary_device(i2c, "edid", 1252 ADV7511_EDID_I2C_ADDR_DEFAULT); 1253 if (IS_ERR(adv7511->i2c_edid)) { 1254 ret = PTR_ERR(adv7511->i2c_edid); 1255 goto uninit_regulators; 1256 } 1257 1258 regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR, 1259 adv7511->i2c_edid->addr << 1); 1260 1261 adv7511->i2c_packet = i2c_new_ancillary_device(i2c, "packet", 1262 ADV7511_PACKET_I2C_ADDR_DEFAULT); 1263 if (IS_ERR(adv7511->i2c_packet)) { 1264 ret = PTR_ERR(adv7511->i2c_packet); 1265 goto err_i2c_unregister_edid; 1266 } 1267 1268 adv7511->regmap_packet = devm_regmap_init_i2c(adv7511->i2c_packet, 1269 &adv7511_packet_config); 1270 if (IS_ERR(adv7511->regmap_packet)) { 1271 ret = PTR_ERR(adv7511->regmap_packet); 1272 goto err_i2c_unregister_packet; 1273 } 1274 1275 regmap_write(adv7511->regmap, ADV7511_REG_PACKET_I2C_ADDR, 1276 adv7511->i2c_packet->addr << 1); 1277 1278 ret = adv7511_init_cec_regmap(adv7511); 1279 if (ret) 1280 goto err_i2c_unregister_packet; 1281 1282 INIT_WORK(&adv7511->hpd_work, adv7511_hpd_work); 1283 1284 adv7511_power_off(adv7511); 1285 1286 i2c_set_clientdata(i2c, adv7511); 1287 1288 if (adv7511->info->link_config) 1289 adv7511_set_link_config(adv7511, &link_config); 1290 1291 regmap_write(adv7511->regmap, ADV7511_REG_CEC_CTRL, 1292 ADV7511_CEC_CTRL_POWER_DOWN); 1293 1294 adv7511->bridge.ops = DRM_BRIDGE_OP_DETECT | 1295 DRM_BRIDGE_OP_EDID | 1296 DRM_BRIDGE_OP_HDMI; 1297 if (adv7511->i2c_main->irq) 1298 adv7511->bridge.ops |= DRM_BRIDGE_OP_HPD; 1299 1300 adv7511->bridge.vendor = "Analog"; 1301 adv7511->bridge.product = adv7511->info->name; 1302 1303 #ifdef CONFIG_DRM_I2C_ADV7511_AUDIO 1304 adv7511->bridge.ops |= DRM_BRIDGE_OP_HDMI_AUDIO; 1305 adv7511->bridge.hdmi_audio_dev = dev; 1306 adv7511->bridge.hdmi_audio_max_i2s_playback_channels = 2; 1307 adv7511->bridge.hdmi_audio_i2s_formats = (SNDRV_PCM_FMTBIT_S16_LE | 1308 SNDRV_PCM_FMTBIT_S20_3LE | 1309 SNDRV_PCM_FMTBIT_S24_3LE | 1310 SNDRV_PCM_FMTBIT_S24_LE | 1311 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE), 1312 adv7511->bridge.hdmi_audio_spdif_playback = 1; 1313 adv7511->bridge.hdmi_audio_dai_port = 2; 1314 #endif 1315 1316 #ifdef CONFIG_DRM_I2C_ADV7511_CEC 1317 adv7511->bridge.ops |= DRM_BRIDGE_OP_HDMI_CEC_ADAPTER; 1318 adv7511->bridge.hdmi_cec_dev = dev; 1319 adv7511->bridge.hdmi_cec_adapter_name = dev_name(dev); 1320 adv7511->bridge.hdmi_cec_available_las = ADV7511_MAX_ADDRS; 1321 #endif 1322 1323 adv7511->bridge.of_node = dev->of_node; 1324 adv7511->bridge.type = DRM_MODE_CONNECTOR_HDMIA; 1325 1326 drm_bridge_add(&adv7511->bridge); 1327 1328 if (i2c->irq) { 1329 init_waitqueue_head(&adv7511->wq); 1330 1331 ret = devm_request_threaded_irq(dev, i2c->irq, NULL, 1332 adv7511_irq_handler, 1333 IRQF_ONESHOT | IRQF_SHARED, 1334 dev_name(dev), 1335 adv7511); 1336 if (ret) 1337 goto err_unregister_audio; 1338 } 1339 1340 if (adv7511->info->has_dsi) { 1341 ret = adv7533_attach_dsi(adv7511); 1342 if (ret) 1343 goto err_unregister_audio; 1344 } 1345 1346 return 0; 1347 1348 err_unregister_audio: 1349 drm_bridge_remove(&adv7511->bridge); 1350 i2c_unregister_device(adv7511->i2c_cec); 1351 clk_disable_unprepare(adv7511->cec_clk); 1352 err_i2c_unregister_packet: 1353 i2c_unregister_device(adv7511->i2c_packet); 1354 err_i2c_unregister_edid: 1355 i2c_unregister_device(adv7511->i2c_edid); 1356 uninit_regulators: 1357 adv7511_uninit_regulators(adv7511); 1358 err_of_node_put: 1359 of_node_put(adv7511->host_node); 1360 1361 return ret; 1362 } 1363 1364 static void adv7511_remove(struct i2c_client *i2c) 1365 { 1366 struct adv7511 *adv7511 = i2c_get_clientdata(i2c); 1367 1368 of_node_put(adv7511->host_node); 1369 1370 adv7511_uninit_regulators(adv7511); 1371 1372 drm_bridge_remove(&adv7511->bridge); 1373 1374 i2c_unregister_device(adv7511->i2c_cec); 1375 clk_disable_unprepare(adv7511->cec_clk); 1376 1377 i2c_unregister_device(adv7511->i2c_packet); 1378 i2c_unregister_device(adv7511->i2c_edid); 1379 } 1380 1381 static const struct adv7511_chip_info adv7511_chip_info = { 1382 .type = ADV7511, 1383 .name = "ADV7511", 1384 .max_mode_clock_khz = 165000, 1385 .supply_names = adv7511_supply_names, 1386 .num_supplies = ARRAY_SIZE(adv7511_supply_names), 1387 .link_config = true, 1388 }; 1389 1390 static const struct adv7511_chip_info adv7533_chip_info = { 1391 .type = ADV7533, 1392 .name = "ADV7533", 1393 .max_mode_clock_khz = 80000, 1394 .max_lane_freq_khz = 800000, 1395 .supply_names = adv7533_supply_names, 1396 .num_supplies = ARRAY_SIZE(adv7533_supply_names), 1397 .reg_cec_offset = ADV7533_REG_CEC_OFFSET, 1398 .has_dsi = true, 1399 }; 1400 1401 static const struct adv7511_chip_info adv7535_chip_info = { 1402 .type = ADV7535, 1403 .name = "ADV7535", 1404 .max_mode_clock_khz = 148500, 1405 .max_lane_freq_khz = 891000, 1406 .supply_names = adv7533_supply_names, 1407 .num_supplies = ARRAY_SIZE(adv7533_supply_names), 1408 .reg_cec_offset = ADV7533_REG_CEC_OFFSET, 1409 .has_dsi = true, 1410 .hpd_override_enable = true, 1411 }; 1412 1413 static const struct i2c_device_id adv7511_i2c_ids[] = { 1414 { "adv7511", (kernel_ulong_t)&adv7511_chip_info }, 1415 { "adv7511w", (kernel_ulong_t)&adv7511_chip_info }, 1416 { "adv7513", (kernel_ulong_t)&adv7511_chip_info }, 1417 { "adv7533", (kernel_ulong_t)&adv7533_chip_info }, 1418 { "adv7535", (kernel_ulong_t)&adv7535_chip_info }, 1419 { } 1420 }; 1421 MODULE_DEVICE_TABLE(i2c, adv7511_i2c_ids); 1422 1423 static const struct of_device_id adv7511_of_ids[] = { 1424 { .compatible = "adi,adv7511", .data = &adv7511_chip_info }, 1425 { .compatible = "adi,adv7511w", .data = &adv7511_chip_info }, 1426 { .compatible = "adi,adv7513", .data = &adv7511_chip_info }, 1427 { .compatible = "adi,adv7533", .data = &adv7533_chip_info }, 1428 { .compatible = "adi,adv7535", .data = &adv7535_chip_info }, 1429 { } 1430 }; 1431 MODULE_DEVICE_TABLE(of, adv7511_of_ids); 1432 1433 static struct mipi_dsi_driver adv7533_dsi_driver = { 1434 .driver.name = "adv7533", 1435 }; 1436 1437 static struct i2c_driver adv7511_driver = { 1438 .driver = { 1439 .name = "adv7511", 1440 .of_match_table = adv7511_of_ids, 1441 }, 1442 .id_table = adv7511_i2c_ids, 1443 .probe = adv7511_probe, 1444 .remove = adv7511_remove, 1445 }; 1446 1447 static int __init adv7511_init(void) 1448 { 1449 int ret; 1450 1451 if (IS_ENABLED(CONFIG_DRM_MIPI_DSI)) { 1452 ret = mipi_dsi_driver_register(&adv7533_dsi_driver); 1453 if (ret) 1454 return ret; 1455 } 1456 1457 ret = i2c_add_driver(&adv7511_driver); 1458 if (ret) { 1459 if (IS_ENABLED(CONFIG_DRM_MIPI_DSI)) 1460 mipi_dsi_driver_unregister(&adv7533_dsi_driver); 1461 } 1462 1463 return ret; 1464 } 1465 module_init(adv7511_init); 1466 1467 static void __exit adv7511_exit(void) 1468 { 1469 i2c_del_driver(&adv7511_driver); 1470 1471 if (IS_ENABLED(CONFIG_DRM_MIPI_DSI)) 1472 mipi_dsi_driver_unregister(&adv7533_dsi_driver); 1473 } 1474 module_exit(adv7511_exit); 1475 1476 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); 1477 MODULE_DESCRIPTION("ADV7511 HDMI transmitter driver"); 1478 MODULE_LICENSE("GPL"); 1479