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