1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2018, The Linux Foundation. All rights reserved. 4 * Copyright (c) 2019-2020. Linaro Limited. 5 */ 6 7 #include <linux/gpio/consumer.h> 8 #include <linux/i2c.h> 9 #include <linux/interrupt.h> 10 #include <linux/media-bus-format.h> 11 #include <linux/module.h> 12 #include <linux/of_graph.h> 13 #include <linux/platform_device.h> 14 #include <linux/regmap.h> 15 #include <linux/regulator/consumer.h> 16 17 #include <sound/hdmi-codec.h> 18 19 #include <drm/drm_atomic_helper.h> 20 #include <drm/drm_bridge.h> 21 #include <drm/drm_edid.h> 22 #include <drm/drm_mipi_dsi.h> 23 #include <drm/drm_of.h> 24 #include <drm/drm_print.h> 25 #include <drm/drm_probe_helper.h> 26 #include <drm/display/drm_hdmi_helper.h> 27 #include <drm/display/drm_hdmi_state_helper.h> 28 29 #define EDID_SEG_SIZE 256 30 #define EDID_LEN 32 31 #define EDID_LOOP 8 32 #define KEY_DDC_ACCS_DONE 0x02 33 #define DDC_NO_ACK 0x50 34 35 #define LT9611_4LANES 0 36 37 struct lt9611 { 38 struct device *dev; 39 struct drm_bridge bridge; 40 struct drm_bridge *next_bridge; 41 42 struct regmap *regmap; 43 44 struct device_node *dsi0_node; 45 struct device_node *dsi1_node; 46 struct mipi_dsi_device *dsi0; 47 struct mipi_dsi_device *dsi1; 48 49 bool ac_mode; 50 51 struct gpio_desc *reset_gpio; 52 struct gpio_desc *enable_gpio; 53 54 bool power_on; 55 bool sleep; 56 57 struct regulator_bulk_data supplies[2]; 58 59 struct i2c_client *client; 60 61 enum drm_connector_status status; 62 63 u8 edid_buf[EDID_SEG_SIZE]; 64 }; 65 66 #define LT9611_PAGE_CONTROL 0xff 67 68 static const struct regmap_range_cfg lt9611_ranges[] = { 69 { 70 .name = "register_range", 71 .range_min = 0, 72 .range_max = 0x85ff, 73 .selector_reg = LT9611_PAGE_CONTROL, 74 .selector_mask = 0xff, 75 .selector_shift = 0, 76 .window_start = 0, 77 .window_len = 0x100, 78 }, 79 }; 80 81 static const struct regmap_config lt9611_regmap_config = { 82 .reg_bits = 8, 83 .val_bits = 8, 84 .max_register = 0xffff, 85 .ranges = lt9611_ranges, 86 .num_ranges = ARRAY_SIZE(lt9611_ranges), 87 }; 88 89 static struct lt9611 *bridge_to_lt9611(struct drm_bridge *bridge) 90 { 91 return container_of(bridge, struct lt9611, bridge); 92 } 93 94 static int lt9611_mipi_input_analog(struct lt9611 *lt9611) 95 { 96 const struct reg_sequence reg_cfg[] = { 97 { 0x8106, 0x40 }, /* port A rx current */ 98 { 0x810a, 0xfe }, /* port A ldo voltage set */ 99 { 0x810b, 0xbf }, /* enable port A lprx */ 100 { 0x8111, 0x40 }, /* port B rx current */ 101 { 0x8115, 0xfe }, /* port B ldo voltage set */ 102 { 0x8116, 0xbf }, /* enable port B lprx */ 103 104 { 0x811c, 0x03 }, /* PortA clk lane no-LP mode */ 105 { 0x8120, 0x03 }, /* PortB clk lane with-LP mode */ 106 }; 107 108 return regmap_multi_reg_write(lt9611->regmap, reg_cfg, ARRAY_SIZE(reg_cfg)); 109 } 110 111 static int lt9611_mipi_input_digital(struct lt9611 *lt9611, 112 const struct drm_display_mode *mode) 113 { 114 struct reg_sequence reg_cfg[] = { 115 { 0x8300, LT9611_4LANES }, 116 { 0x830a, 0x00 }, 117 { 0x824f, 0x80 }, 118 { 0x8250, 0x10 }, 119 { 0x8302, 0x0a }, 120 { 0x8306, 0x0a }, 121 }; 122 123 if (lt9611->dsi1_node) 124 reg_cfg[1].def = 0x03; 125 126 return regmap_multi_reg_write(lt9611->regmap, reg_cfg, ARRAY_SIZE(reg_cfg)); 127 } 128 129 static void lt9611_mipi_video_setup(struct lt9611 *lt9611, 130 const struct drm_display_mode *mode) 131 { 132 u32 h_total, hactive, hsync_len, hfront_porch, hsync_porch; 133 u32 v_total, vactive, vsync_len, vfront_porch, vsync_porch; 134 135 h_total = mode->htotal; 136 v_total = mode->vtotal; 137 138 hactive = mode->hdisplay; 139 hsync_len = mode->hsync_end - mode->hsync_start; 140 hfront_porch = mode->hsync_start - mode->hdisplay; 141 hsync_porch = mode->htotal - mode->hsync_start; 142 143 vactive = mode->vdisplay; 144 vsync_len = mode->vsync_end - mode->vsync_start; 145 vfront_porch = mode->vsync_start - mode->vdisplay; 146 vsync_porch = mode->vtotal - mode->vsync_start; 147 148 regmap_write(lt9611->regmap, 0x830d, (u8)(v_total / 256)); 149 regmap_write(lt9611->regmap, 0x830e, (u8)(v_total % 256)); 150 151 regmap_write(lt9611->regmap, 0x830f, (u8)(vactive / 256)); 152 regmap_write(lt9611->regmap, 0x8310, (u8)(vactive % 256)); 153 154 regmap_write(lt9611->regmap, 0x8311, (u8)(h_total / 256)); 155 regmap_write(lt9611->regmap, 0x8312, (u8)(h_total % 256)); 156 157 regmap_write(lt9611->regmap, 0x8313, (u8)(hactive / 256)); 158 regmap_write(lt9611->regmap, 0x8314, (u8)(hactive % 256)); 159 160 regmap_write(lt9611->regmap, 0x8315, (u8)(vsync_len % 256)); 161 regmap_write(lt9611->regmap, 0x8316, (u8)(hsync_len % 256)); 162 163 regmap_write(lt9611->regmap, 0x8317, (u8)(vfront_porch % 256)); 164 165 regmap_write(lt9611->regmap, 0x8318, (u8)(vsync_porch % 256)); 166 167 regmap_write(lt9611->regmap, 0x8319, (u8)(hfront_porch % 256)); 168 169 regmap_write(lt9611->regmap, 0x831a, (u8)(hsync_porch / 256) | 170 ((hfront_porch / 256) << 4)); 171 regmap_write(lt9611->regmap, 0x831b, (u8)(hsync_porch % 256)); 172 } 173 174 static void lt9611_pcr_setup(struct lt9611 *lt9611, const struct drm_display_mode *mode, unsigned int postdiv) 175 { 176 unsigned int pcr_m = mode->clock * 5 * postdiv / 27000; 177 const struct reg_sequence reg_cfg[] = { 178 { 0x830b, 0x01 }, 179 { 0x830c, 0x10 }, 180 { 0x8348, 0x00 }, 181 { 0x8349, 0x81 }, 182 183 /* stage 1 */ 184 { 0x8321, 0x4a }, 185 { 0x8324, 0x71 }, 186 { 0x8325, 0x30 }, 187 { 0x832a, 0x01 }, 188 189 /* stage 2 */ 190 { 0x834a, 0x40 }, 191 192 /* MK limit */ 193 { 0x832d, 0x38 }, 194 { 0x8331, 0x08 }, 195 }; 196 u8 pol = 0x10; 197 198 if (mode->flags & DRM_MODE_FLAG_NHSYNC) 199 pol |= 0x2; 200 if (mode->flags & DRM_MODE_FLAG_NVSYNC) 201 pol |= 0x1; 202 regmap_write(lt9611->regmap, 0x831d, pol); 203 204 regmap_multi_reg_write(lt9611->regmap, reg_cfg, ARRAY_SIZE(reg_cfg)); 205 if (lt9611->dsi1_node) { 206 unsigned int hact = mode->hdisplay; 207 208 hact >>= 2; 209 hact += 0x50; 210 hact = min(hact, 0x3e0U); 211 regmap_write(lt9611->regmap, 0x830b, hact / 256); 212 regmap_write(lt9611->regmap, 0x830c, hact % 256); 213 regmap_write(lt9611->regmap, 0x8348, hact / 256); 214 regmap_write(lt9611->regmap, 0x8349, hact % 256); 215 } 216 217 regmap_write(lt9611->regmap, 0x8326, pcr_m); 218 219 /* pcr rst */ 220 regmap_write(lt9611->regmap, 0x8011, 0x5a); 221 regmap_write(lt9611->regmap, 0x8011, 0xfa); 222 } 223 224 static int lt9611_pll_setup(struct lt9611 *lt9611, const struct drm_display_mode *mode, unsigned int *postdiv) 225 { 226 unsigned int pclk = mode->clock; 227 const struct reg_sequence reg_cfg[] = { 228 /* txpll init */ 229 { 0x8123, 0x40 }, 230 { 0x8124, 0x64 }, 231 { 0x8125, 0x80 }, 232 { 0x8126, 0x55 }, 233 { 0x812c, 0x37 }, 234 { 0x812f, 0x01 }, 235 { 0x8126, 0x55 }, 236 { 0x8127, 0x66 }, 237 { 0x8128, 0x88 }, 238 { 0x812a, 0x20 }, 239 }; 240 241 regmap_multi_reg_write(lt9611->regmap, reg_cfg, ARRAY_SIZE(reg_cfg)); 242 243 if (pclk > 150000) { 244 regmap_write(lt9611->regmap, 0x812d, 0x88); 245 *postdiv = 1; 246 } else if (pclk > 70000) { 247 regmap_write(lt9611->regmap, 0x812d, 0x99); 248 *postdiv = 2; 249 } else { 250 regmap_write(lt9611->regmap, 0x812d, 0xaa); 251 *postdiv = 4; 252 } 253 254 /* 255 * first divide pclk by 2 first 256 * - write divide by 64k to 19:16 bits which means shift by 17 257 * - write divide by 256 to 15:8 bits which means shift by 9 258 * - write remainder to 7:0 bits, which means shift by 1 259 */ 260 regmap_write(lt9611->regmap, 0x82e3, pclk >> 17); /* pclk[19:16] */ 261 regmap_write(lt9611->regmap, 0x82e4, pclk >> 9); /* pclk[15:8] */ 262 regmap_write(lt9611->regmap, 0x82e5, pclk >> 1); /* pclk[7:0] */ 263 264 regmap_write(lt9611->regmap, 0x82de, 0x20); 265 regmap_write(lt9611->regmap, 0x82de, 0xe0); 266 267 regmap_write(lt9611->regmap, 0x8016, 0xf1); 268 regmap_write(lt9611->regmap, 0x8016, 0xf3); 269 270 return 0; 271 } 272 273 static int lt9611_read_video_check(struct lt9611 *lt9611, unsigned int reg) 274 { 275 unsigned int temp, temp2; 276 int ret; 277 278 ret = regmap_read(lt9611->regmap, reg, &temp); 279 if (ret) 280 return ret; 281 temp <<= 8; 282 ret = regmap_read(lt9611->regmap, reg + 1, &temp2); 283 if (ret) 284 return ret; 285 286 return (temp + temp2); 287 } 288 289 static int lt9611_video_check(struct lt9611 *lt9611) 290 { 291 u32 v_total, vactive, hactive_a, hactive_b, h_total_sysclk; 292 int temp; 293 294 /* top module video check */ 295 296 /* vactive */ 297 temp = lt9611_read_video_check(lt9611, 0x8282); 298 if (temp < 0) 299 goto end; 300 vactive = temp; 301 302 /* v_total */ 303 temp = lt9611_read_video_check(lt9611, 0x826c); 304 if (temp < 0) 305 goto end; 306 v_total = temp; 307 308 /* h_total_sysclk */ 309 temp = lt9611_read_video_check(lt9611, 0x8286); 310 if (temp < 0) 311 goto end; 312 h_total_sysclk = temp; 313 314 /* hactive_a */ 315 temp = lt9611_read_video_check(lt9611, 0x8382); 316 if (temp < 0) 317 goto end; 318 hactive_a = temp / 3; 319 320 /* hactive_b */ 321 temp = lt9611_read_video_check(lt9611, 0x8386); 322 if (temp < 0) 323 goto end; 324 hactive_b = temp / 3; 325 326 dev_info(lt9611->dev, 327 "video check: hactive_a=%d, hactive_b=%d, vactive=%d, v_total=%d, h_total_sysclk=%d\n", 328 hactive_a, hactive_b, vactive, v_total, h_total_sysclk); 329 330 return 0; 331 332 end: 333 dev_err(lt9611->dev, "read video check error\n"); 334 return temp; 335 } 336 337 static void lt9611_hdmi_tx_digital(struct lt9611 *lt9611, bool is_hdmi) 338 { 339 if (is_hdmi) 340 regmap_write(lt9611->regmap, 0x82d6, 0x8c); 341 else 342 regmap_write(lt9611->regmap, 0x82d6, 0x0c); 343 regmap_write(lt9611->regmap, 0x82d7, 0x04); 344 } 345 346 static void lt9611_hdmi_tx_phy(struct lt9611 *lt9611) 347 { 348 struct reg_sequence reg_cfg[] = { 349 { 0x8130, 0x6a }, 350 { 0x8131, 0x44 }, /* HDMI DC mode */ 351 { 0x8132, 0x4a }, 352 { 0x8133, 0x0b }, 353 { 0x8134, 0x00 }, 354 { 0x8135, 0x00 }, 355 { 0x8136, 0x00 }, 356 { 0x8137, 0x44 }, 357 { 0x813f, 0x0f }, 358 { 0x8140, 0xa0 }, 359 { 0x8141, 0xa0 }, 360 { 0x8142, 0xa0 }, 361 { 0x8143, 0xa0 }, 362 { 0x8144, 0x0a }, 363 }; 364 365 /* HDMI AC mode */ 366 if (lt9611->ac_mode) 367 reg_cfg[2].def = 0x73; 368 369 regmap_multi_reg_write(lt9611->regmap, reg_cfg, ARRAY_SIZE(reg_cfg)); 370 } 371 372 static irqreturn_t lt9611_irq_thread_handler(int irq, void *dev_id) 373 { 374 struct lt9611 *lt9611 = dev_id; 375 unsigned int irq_flag0 = 0; 376 unsigned int irq_flag3 = 0; 377 378 regmap_read(lt9611->regmap, 0x820f, &irq_flag3); 379 regmap_read(lt9611->regmap, 0x820c, &irq_flag0); 380 381 /* hpd changed low */ 382 if (irq_flag3 & 0x80) { 383 dev_info(lt9611->dev, "hdmi cable disconnected\n"); 384 385 regmap_write(lt9611->regmap, 0x8207, 0xbf); 386 regmap_write(lt9611->regmap, 0x8207, 0x3f); 387 } 388 389 /* hpd changed high */ 390 if (irq_flag3 & 0x40) { 391 dev_info(lt9611->dev, "hdmi cable connected\n"); 392 393 regmap_write(lt9611->regmap, 0x8207, 0x7f); 394 regmap_write(lt9611->regmap, 0x8207, 0x3f); 395 } 396 397 if (irq_flag3 & 0xc0 && lt9611->bridge.dev) 398 drm_kms_helper_hotplug_event(lt9611->bridge.dev); 399 400 /* video input changed */ 401 if (irq_flag0 & 0x01) { 402 dev_info(lt9611->dev, "video input changed\n"); 403 regmap_write(lt9611->regmap, 0x829e, 0xff); 404 regmap_write(lt9611->regmap, 0x829e, 0xf7); 405 regmap_write(lt9611->regmap, 0x8204, 0xff); 406 regmap_write(lt9611->regmap, 0x8204, 0xfe); 407 } 408 409 return IRQ_HANDLED; 410 } 411 412 static void lt9611_enable_hpd_interrupts(struct lt9611 *lt9611) 413 { 414 unsigned int val; 415 416 regmap_read(lt9611->regmap, 0x8203, &val); 417 418 val &= ~0xc0; 419 regmap_write(lt9611->regmap, 0x8203, val); 420 regmap_write(lt9611->regmap, 0x8207, 0xff); /* clear */ 421 regmap_write(lt9611->regmap, 0x8207, 0x3f); 422 } 423 424 static void lt9611_sleep_setup(struct lt9611 *lt9611) 425 { 426 const struct reg_sequence sleep_setup[] = { 427 { 0x8024, 0x76 }, 428 { 0x8023, 0x01 }, 429 { 0x8157, 0x03 }, /* set addr pin as output */ 430 { 0x8149, 0x0b }, 431 432 { 0x8102, 0x48 }, /* MIPI Rx power down */ 433 { 0x8123, 0x80 }, 434 { 0x8130, 0x00 }, 435 { 0x8011, 0x0a }, 436 }; 437 438 regmap_multi_reg_write(lt9611->regmap, 439 sleep_setup, ARRAY_SIZE(sleep_setup)); 440 lt9611->sleep = true; 441 } 442 443 static int lt9611_power_on(struct lt9611 *lt9611) 444 { 445 int ret; 446 const struct reg_sequence seq[] = { 447 /* LT9611_System_Init */ 448 { 0x8101, 0x18 }, /* sel xtal clock */ 449 450 /* timer for frequency meter */ 451 { 0x821b, 0x69 }, /* timer 2 */ 452 { 0x821c, 0x78 }, 453 { 0x82cb, 0x69 }, /* timer 1 */ 454 { 0x82cc, 0x78 }, 455 456 /* irq init */ 457 { 0x8251, 0x01 }, 458 { 0x8258, 0x0a }, /* hpd irq */ 459 { 0x8259, 0x80 }, /* hpd debounce width */ 460 { 0x829e, 0xf7 }, /* video check irq */ 461 462 /* power consumption for work */ 463 { 0x8004, 0xf0 }, 464 { 0x8006, 0xf0 }, 465 { 0x800a, 0x80 }, 466 { 0x800b, 0x40 }, 467 { 0x800d, 0xef }, 468 { 0x8011, 0xfa }, 469 }; 470 471 if (lt9611->power_on) 472 return 0; 473 474 ret = regmap_multi_reg_write(lt9611->regmap, seq, ARRAY_SIZE(seq)); 475 if (!ret) 476 lt9611->power_on = true; 477 478 return ret; 479 } 480 481 static int lt9611_power_off(struct lt9611 *lt9611) 482 { 483 int ret; 484 485 ret = regmap_write(lt9611->regmap, 0x8130, 0x6a); 486 if (!ret) 487 lt9611->power_on = false; 488 489 return ret; 490 } 491 492 static void lt9611_reset(struct lt9611 *lt9611) 493 { 494 gpiod_set_value_cansleep(lt9611->reset_gpio, 1); 495 msleep(20); 496 497 gpiod_set_value_cansleep(lt9611->reset_gpio, 0); 498 msleep(20); 499 500 gpiod_set_value_cansleep(lt9611->reset_gpio, 1); 501 msleep(100); 502 } 503 504 static void lt9611_assert_5v(struct lt9611 *lt9611) 505 { 506 if (!lt9611->enable_gpio) 507 return; 508 509 gpiod_set_value_cansleep(lt9611->enable_gpio, 1); 510 msleep(20); 511 } 512 513 static int lt9611_regulator_init(struct lt9611 *lt9611) 514 { 515 int ret; 516 517 lt9611->supplies[0].supply = "vdd"; 518 lt9611->supplies[1].supply = "vcc"; 519 520 ret = devm_regulator_bulk_get(lt9611->dev, 2, lt9611->supplies); 521 if (ret < 0) 522 return ret; 523 524 return regulator_set_load(lt9611->supplies[0].consumer, 300000); 525 } 526 527 static int lt9611_regulator_enable(struct lt9611 *lt9611) 528 { 529 int ret; 530 531 ret = regulator_enable(lt9611->supplies[0].consumer); 532 if (ret < 0) 533 return ret; 534 535 usleep_range(1000, 10000); 536 537 ret = regulator_enable(lt9611->supplies[1].consumer); 538 if (ret < 0) { 539 regulator_disable(lt9611->supplies[0].consumer); 540 return ret; 541 } 542 543 return 0; 544 } 545 546 static enum drm_connector_status 547 lt9611_bridge_detect(struct drm_bridge *bridge, struct drm_connector *connector) 548 { 549 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 550 unsigned int reg_val = 0; 551 int connected = 0; 552 553 regmap_read(lt9611->regmap, 0x825e, ®_val); 554 connected = (reg_val & (BIT(2) | BIT(0))); 555 556 lt9611->status = connected ? connector_status_connected : 557 connector_status_disconnected; 558 559 return lt9611->status; 560 } 561 562 static int lt9611_read_edid(struct lt9611 *lt9611) 563 { 564 unsigned int temp; 565 int ret = 0; 566 int i, j; 567 568 /* memset to clear old buffer, if any */ 569 memset(lt9611->edid_buf, 0, sizeof(lt9611->edid_buf)); 570 571 regmap_write(lt9611->regmap, 0x8503, 0xc9); 572 573 /* 0xA0 is EDID device address */ 574 regmap_write(lt9611->regmap, 0x8504, 0xa0); 575 /* 0x00 is EDID offset address */ 576 regmap_write(lt9611->regmap, 0x8505, 0x00); 577 578 /* length for read */ 579 regmap_write(lt9611->regmap, 0x8506, EDID_LEN); 580 regmap_write(lt9611->regmap, 0x8514, 0x7f); 581 582 for (i = 0; i < EDID_LOOP; i++) { 583 /* offset address */ 584 regmap_write(lt9611->regmap, 0x8505, i * EDID_LEN); 585 regmap_write(lt9611->regmap, 0x8507, 0x36); 586 regmap_write(lt9611->regmap, 0x8507, 0x31); 587 regmap_write(lt9611->regmap, 0x8507, 0x37); 588 usleep_range(5000, 10000); 589 590 regmap_read(lt9611->regmap, 0x8540, &temp); 591 592 if (temp & KEY_DDC_ACCS_DONE) { 593 for (j = 0; j < EDID_LEN; j++) { 594 regmap_read(lt9611->regmap, 0x8583, &temp); 595 lt9611->edid_buf[i * EDID_LEN + j] = temp; 596 } 597 598 } else if (temp & DDC_NO_ACK) { /* DDC No Ack or Abitration lost */ 599 dev_err(lt9611->dev, "read edid failed: no ack\n"); 600 ret = -EIO; 601 goto end; 602 603 } else { 604 dev_err(lt9611->dev, "read edid failed: access not done\n"); 605 ret = -EIO; 606 goto end; 607 } 608 } 609 610 end: 611 regmap_write(lt9611->regmap, 0x8507, 0x1f); 612 return ret; 613 } 614 615 static int 616 lt9611_get_edid_block(void *data, u8 *buf, unsigned int block, size_t len) 617 { 618 struct lt9611 *lt9611 = data; 619 int ret; 620 621 if (len > 128) 622 return -EINVAL; 623 624 /* supports up to 1 extension block */ 625 /* TODO: add support for more extension blocks */ 626 if (block > 1) 627 return -EINVAL; 628 629 if (block == 0) { 630 ret = lt9611_read_edid(lt9611); 631 if (ret) { 632 dev_err(lt9611->dev, "edid read failed\n"); 633 return ret; 634 } 635 } 636 637 block %= 2; 638 memcpy(buf, lt9611->edid_buf + (block * 128), len); 639 640 return 0; 641 } 642 643 /* bridge funcs */ 644 static void lt9611_bridge_atomic_enable(struct drm_bridge *bridge, 645 struct drm_atomic_state *state) 646 { 647 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 648 struct drm_connector *connector; 649 struct drm_connector_state *conn_state; 650 struct drm_crtc_state *crtc_state; 651 struct drm_display_mode *mode; 652 unsigned int postdiv; 653 654 connector = drm_atomic_get_new_connector_for_encoder(state, bridge->encoder); 655 if (WARN_ON(!connector)) 656 return; 657 658 conn_state = drm_atomic_get_new_connector_state(state, connector); 659 if (WARN_ON(!conn_state)) 660 return; 661 662 crtc_state = drm_atomic_get_new_crtc_state(state, conn_state->crtc); 663 if (WARN_ON(!crtc_state)) 664 return; 665 666 mode = &crtc_state->adjusted_mode; 667 668 lt9611_mipi_input_digital(lt9611, mode); 669 lt9611_pll_setup(lt9611, mode, &postdiv); 670 lt9611_mipi_video_setup(lt9611, mode); 671 lt9611_pcr_setup(lt9611, mode, postdiv); 672 673 if (lt9611_power_on(lt9611)) { 674 dev_err(lt9611->dev, "power on failed\n"); 675 return; 676 } 677 678 lt9611_mipi_input_analog(lt9611); 679 drm_atomic_helper_connector_hdmi_update_infoframes(connector, state); 680 lt9611_hdmi_tx_digital(lt9611, connector->display_info.is_hdmi); 681 lt9611_hdmi_tx_phy(lt9611); 682 683 msleep(500); 684 685 lt9611_video_check(lt9611); 686 687 /* Enable HDMI output */ 688 regmap_write(lt9611->regmap, 0x8130, 0xea); 689 } 690 691 static void lt9611_bridge_atomic_disable(struct drm_bridge *bridge, 692 struct drm_atomic_state *state) 693 { 694 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 695 int ret; 696 697 /* Disable HDMI output */ 698 ret = regmap_write(lt9611->regmap, 0x8130, 0x6a); 699 if (ret) { 700 dev_err(lt9611->dev, "video on failed\n"); 701 return; 702 } 703 704 if (lt9611_power_off(lt9611)) { 705 dev_err(lt9611->dev, "power on failed\n"); 706 return; 707 } 708 } 709 710 static struct mipi_dsi_device *lt9611_attach_dsi(struct lt9611 *lt9611, 711 struct device_node *dsi_node) 712 { 713 const struct mipi_dsi_device_info info = { "lt9611", 0, lt9611->dev->of_node}; 714 struct mipi_dsi_device *dsi; 715 struct mipi_dsi_host *host; 716 struct device *dev = lt9611->dev; 717 int ret; 718 719 host = of_find_mipi_dsi_host_by_node(dsi_node); 720 if (!host) 721 return ERR_PTR(dev_err_probe(lt9611->dev, -EPROBE_DEFER, "failed to find dsi host\n")); 722 723 dsi = devm_mipi_dsi_device_register_full(dev, host, &info); 724 if (IS_ERR(dsi)) { 725 dev_err(lt9611->dev, "failed to create dsi device\n"); 726 return dsi; 727 } 728 729 dsi->lanes = 4; 730 dsi->format = MIPI_DSI_FMT_RGB888; 731 dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE | 732 MIPI_DSI_MODE_VIDEO_HSE; 733 734 ret = devm_mipi_dsi_attach(dev, dsi); 735 if (ret < 0) { 736 dev_err(dev, "failed to attach dsi to host\n"); 737 return ERR_PTR(ret); 738 } 739 740 return dsi; 741 } 742 743 static int lt9611_bridge_attach(struct drm_bridge *bridge, 744 struct drm_encoder *encoder, 745 enum drm_bridge_attach_flags flags) 746 { 747 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 748 749 return drm_bridge_attach(encoder, lt9611->next_bridge, 750 bridge, flags); 751 } 752 753 static enum drm_mode_status lt9611_bridge_mode_valid(struct drm_bridge *bridge, 754 const struct drm_display_info *info, 755 const struct drm_display_mode *mode) 756 { 757 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 758 759 if (mode->hdisplay > 3840) 760 return MODE_BAD_HVALUE; 761 762 if (mode->hdisplay > 2000 && !lt9611->dsi1_node) 763 return MODE_PANEL; 764 765 return MODE_OK; 766 } 767 768 static void lt9611_bridge_atomic_pre_enable(struct drm_bridge *bridge, 769 struct drm_atomic_state *state) 770 { 771 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 772 static const struct reg_sequence reg_cfg[] = { 773 { 0x8102, 0x12 }, 774 { 0x8123, 0x40 }, 775 { 0x8130, 0xea }, 776 { 0x8011, 0xfa }, 777 }; 778 779 if (!lt9611->sleep) 780 return; 781 782 regmap_multi_reg_write(lt9611->regmap, 783 reg_cfg, ARRAY_SIZE(reg_cfg)); 784 785 lt9611->sleep = false; 786 } 787 788 static void lt9611_bridge_atomic_post_disable(struct drm_bridge *bridge, 789 struct drm_atomic_state *state) 790 { 791 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 792 793 lt9611_sleep_setup(lt9611); 794 } 795 796 static const struct drm_edid *lt9611_bridge_edid_read(struct drm_bridge *bridge, 797 struct drm_connector *connector) 798 { 799 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 800 801 lt9611_power_on(lt9611); 802 return drm_edid_read_custom(connector, lt9611_get_edid_block, lt9611); 803 } 804 805 static void lt9611_bridge_hpd_enable(struct drm_bridge *bridge) 806 { 807 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 808 809 lt9611_enable_hpd_interrupts(lt9611); 810 } 811 812 #define MAX_INPUT_SEL_FORMATS 1 813 814 static u32 * 815 lt9611_atomic_get_input_bus_fmts(struct drm_bridge *bridge, 816 struct drm_bridge_state *bridge_state, 817 struct drm_crtc_state *crtc_state, 818 struct drm_connector_state *conn_state, 819 u32 output_fmt, 820 unsigned int *num_input_fmts) 821 { 822 u32 *input_fmts; 823 824 *num_input_fmts = 0; 825 826 input_fmts = kcalloc(MAX_INPUT_SEL_FORMATS, sizeof(*input_fmts), 827 GFP_KERNEL); 828 if (!input_fmts) 829 return NULL; 830 831 /* This is the DSI-end bus format */ 832 input_fmts[0] = MEDIA_BUS_FMT_RGB888_1X24; 833 *num_input_fmts = 1; 834 835 return input_fmts; 836 } 837 838 /* 839 * Other working frames: 840 * - 0x01, 0x84df 841 * - 0x04, 0x84c0 842 */ 843 #define LT9611_INFOFRAME_AUDIO 0x02 844 #define LT9611_INFOFRAME_AVI 0x08 845 #define LT9611_INFOFRAME_SPD 0x10 846 #define LT9611_INFOFRAME_HDMI 0x20 847 848 static int lt9611_hdmi_clear_audio_infoframe(struct drm_bridge *bridge) 849 { 850 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 851 852 regmap_update_bits(lt9611->regmap, 0x843d, LT9611_INFOFRAME_AUDIO, 0); 853 854 return 0; 855 } 856 857 static int lt9611_hdmi_clear_avi_infoframe(struct drm_bridge *bridge) 858 { 859 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 860 861 regmap_update_bits(lt9611->regmap, 0x843d, LT9611_INFOFRAME_AVI, 0); 862 863 return 0; 864 } 865 866 static int lt9611_hdmi_clear_spd_infoframe(struct drm_bridge *bridge) 867 { 868 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 869 870 regmap_update_bits(lt9611->regmap, 0x843d, LT9611_INFOFRAME_SPD, 0); 871 872 return 0; 873 } 874 875 static int lt9611_hdmi_clear_hdmi_infoframe(struct drm_bridge *bridge) 876 { 877 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 878 879 regmap_update_bits(lt9611->regmap, 0x843d, LT9611_INFOFRAME_HDMI, 0); 880 881 return 0; 882 } 883 884 static int lt9611_hdmi_write_audio_infoframe(struct drm_bridge *bridge, 885 const u8 *buffer, size_t len) 886 { 887 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 888 int i; 889 890 for (i = 0; i < len; i++) 891 regmap_write(lt9611->regmap, 0x84b2 + i, buffer[i]); 892 893 regmap_update_bits(lt9611->regmap, 0x843d, LT9611_INFOFRAME_AUDIO, LT9611_INFOFRAME_AUDIO); 894 895 return 0; 896 } 897 898 static int lt9611_hdmi_write_avi_infoframe(struct drm_bridge *bridge, 899 const u8 *buffer, size_t len) 900 { 901 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 902 int i; 903 904 for (i = 0; i < len; i++) 905 regmap_write(lt9611->regmap, 0x8440 + i, buffer[i]); 906 907 regmap_update_bits(lt9611->regmap, 0x843d, LT9611_INFOFRAME_AVI, LT9611_INFOFRAME_AVI); 908 909 return 0; 910 } 911 912 static int lt9611_hdmi_write_spd_infoframe(struct drm_bridge *bridge, 913 const u8 *buffer, size_t len) 914 { 915 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 916 int i; 917 918 for (i = 0; i < len; i++) 919 regmap_write(lt9611->regmap, 0x8493 + i, buffer[i]); 920 921 regmap_update_bits(lt9611->regmap, 0x843d, LT9611_INFOFRAME_SPD, LT9611_INFOFRAME_SPD); 922 923 return 0; 924 } 925 926 static int lt9611_hdmi_write_hdmi_infoframe(struct drm_bridge *bridge, 927 const u8 *buffer, size_t len) 928 { 929 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 930 int i; 931 932 for (i = 0; i < len; i++) 933 regmap_write(lt9611->regmap, 0x8474 + i, buffer[i]); 934 935 regmap_update_bits(lt9611->regmap, 0x843d, LT9611_INFOFRAME_HDMI, LT9611_INFOFRAME_HDMI); 936 937 return 0; 938 } 939 940 static enum drm_mode_status 941 lt9611_hdmi_tmds_char_rate_valid(const struct drm_bridge *bridge, 942 const struct drm_display_mode *mode, 943 unsigned long long tmds_rate) 944 { 945 /* 297 MHz for 4k@30 mode */ 946 if (tmds_rate > 297000000) 947 return MODE_CLOCK_HIGH; 948 949 return MODE_OK; 950 } 951 952 static int lt9611_hdmi_audio_startup(struct drm_bridge *bridge, 953 struct drm_connector *connector) 954 { 955 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 956 957 regmap_write(lt9611->regmap, 0x82d6, 0x8c); 958 regmap_write(lt9611->regmap, 0x82d7, 0x04); 959 960 regmap_write(lt9611->regmap, 0x8406, 0x08); 961 regmap_write(lt9611->regmap, 0x8407, 0x10); 962 963 regmap_write(lt9611->regmap, 0x8434, 0xd5); 964 965 return 0; 966 } 967 968 static int lt9611_hdmi_audio_prepare(struct drm_bridge *bridge, 969 struct drm_connector *connector, 970 struct hdmi_codec_daifmt *fmt, 971 struct hdmi_codec_params *hparms) 972 { 973 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 974 975 if (hparms->sample_rate == 48000) 976 regmap_write(lt9611->regmap, 0x840f, 0x2b); 977 else if (hparms->sample_rate == 96000) 978 regmap_write(lt9611->regmap, 0x840f, 0xab); 979 else 980 return -EINVAL; 981 982 regmap_write(lt9611->regmap, 0x8435, 0x00); 983 regmap_write(lt9611->regmap, 0x8436, 0x18); 984 regmap_write(lt9611->regmap, 0x8437, 0x00); 985 986 return drm_atomic_helper_connector_hdmi_update_audio_infoframe(connector, 987 &hparms->cea); 988 } 989 990 static void lt9611_hdmi_audio_shutdown(struct drm_bridge *bridge, 991 struct drm_connector *connector) 992 { 993 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 994 995 drm_atomic_helper_connector_hdmi_clear_audio_infoframe(connector); 996 997 regmap_write(lt9611->regmap, 0x8406, 0x00); 998 regmap_write(lt9611->regmap, 0x8407, 0x00); 999 } 1000 1001 static const struct drm_bridge_funcs lt9611_bridge_funcs = { 1002 .attach = lt9611_bridge_attach, 1003 .mode_valid = lt9611_bridge_mode_valid, 1004 .detect = lt9611_bridge_detect, 1005 .edid_read = lt9611_bridge_edid_read, 1006 .hpd_enable = lt9611_bridge_hpd_enable, 1007 1008 .atomic_pre_enable = lt9611_bridge_atomic_pre_enable, 1009 .atomic_enable = lt9611_bridge_atomic_enable, 1010 .atomic_disable = lt9611_bridge_atomic_disable, 1011 .atomic_post_disable = lt9611_bridge_atomic_post_disable, 1012 .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state, 1013 .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state, 1014 .atomic_reset = drm_atomic_helper_bridge_reset, 1015 .atomic_get_input_bus_fmts = lt9611_atomic_get_input_bus_fmts, 1016 1017 .hdmi_tmds_char_rate_valid = lt9611_hdmi_tmds_char_rate_valid, 1018 .hdmi_write_audio_infoframe = lt9611_hdmi_write_audio_infoframe, 1019 .hdmi_clear_audio_infoframe = lt9611_hdmi_clear_audio_infoframe, 1020 .hdmi_write_avi_infoframe = lt9611_hdmi_write_avi_infoframe, 1021 .hdmi_clear_avi_infoframe = lt9611_hdmi_clear_avi_infoframe, 1022 .hdmi_write_spd_infoframe = lt9611_hdmi_write_spd_infoframe, 1023 .hdmi_clear_spd_infoframe = lt9611_hdmi_clear_spd_infoframe, 1024 .hdmi_write_hdmi_infoframe = lt9611_hdmi_write_hdmi_infoframe, 1025 .hdmi_clear_hdmi_infoframe = lt9611_hdmi_clear_hdmi_infoframe, 1026 1027 .hdmi_audio_startup = lt9611_hdmi_audio_startup, 1028 .hdmi_audio_prepare = lt9611_hdmi_audio_prepare, 1029 .hdmi_audio_shutdown = lt9611_hdmi_audio_shutdown, 1030 }; 1031 1032 static int lt9611_parse_dt(struct device *dev, 1033 struct lt9611 *lt9611) 1034 { 1035 lt9611->dsi0_node = of_graph_get_remote_node(dev->of_node, 0, -1); 1036 if (!lt9611->dsi0_node) { 1037 dev_err(lt9611->dev, "failed to get remote node for primary dsi\n"); 1038 return -ENODEV; 1039 } 1040 1041 lt9611->dsi1_node = of_graph_get_remote_node(dev->of_node, 1, -1); 1042 1043 lt9611->ac_mode = of_property_read_bool(dev->of_node, "lt,ac-mode"); 1044 1045 return drm_of_find_panel_or_bridge(dev->of_node, 2, -1, NULL, <9611->next_bridge); 1046 } 1047 1048 static int lt9611_gpio_init(struct lt9611 *lt9611) 1049 { 1050 struct device *dev = lt9611->dev; 1051 1052 lt9611->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 1053 if (IS_ERR(lt9611->reset_gpio)) { 1054 dev_err(dev, "failed to acquire reset gpio\n"); 1055 return PTR_ERR(lt9611->reset_gpio); 1056 } 1057 1058 lt9611->enable_gpio = devm_gpiod_get_optional(dev, "enable", 1059 GPIOD_OUT_LOW); 1060 if (IS_ERR(lt9611->enable_gpio)) { 1061 dev_err(dev, "failed to acquire enable gpio\n"); 1062 return PTR_ERR(lt9611->enable_gpio); 1063 } 1064 1065 return 0; 1066 } 1067 1068 static int lt9611_read_device_rev(struct lt9611 *lt9611) 1069 { 1070 unsigned int rev; 1071 int ret; 1072 1073 regmap_write(lt9611->regmap, 0x80ee, 0x01); 1074 ret = regmap_read(lt9611->regmap, 0x8002, &rev); 1075 if (ret) 1076 dev_err(lt9611->dev, "failed to read revision: %d\n", ret); 1077 else 1078 dev_info(lt9611->dev, "LT9611 revision: 0x%x\n", rev); 1079 1080 return ret; 1081 } 1082 1083 static int lt9611_probe(struct i2c_client *client) 1084 { 1085 struct lt9611 *lt9611; 1086 struct device *dev = &client->dev; 1087 int ret; 1088 1089 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 1090 dev_err(dev, "device doesn't support I2C\n"); 1091 return -ENODEV; 1092 } 1093 1094 lt9611 = devm_drm_bridge_alloc(dev, struct lt9611, bridge, 1095 <9611_bridge_funcs); 1096 if (IS_ERR(lt9611)) 1097 return PTR_ERR(lt9611); 1098 1099 lt9611->dev = dev; 1100 lt9611->client = client; 1101 lt9611->sleep = false; 1102 1103 lt9611->regmap = devm_regmap_init_i2c(client, <9611_regmap_config); 1104 if (IS_ERR(lt9611->regmap)) { 1105 dev_err(lt9611->dev, "regmap i2c init failed\n"); 1106 return PTR_ERR(lt9611->regmap); 1107 } 1108 1109 ret = lt9611_parse_dt(dev, lt9611); 1110 if (ret) { 1111 dev_err(dev, "failed to parse device tree\n"); 1112 return ret; 1113 } 1114 1115 ret = lt9611_gpio_init(lt9611); 1116 if (ret < 0) 1117 goto err_of_put; 1118 1119 ret = lt9611_regulator_init(lt9611); 1120 if (ret < 0) 1121 goto err_of_put; 1122 1123 lt9611_assert_5v(lt9611); 1124 1125 ret = lt9611_regulator_enable(lt9611); 1126 if (ret) 1127 goto err_of_put; 1128 1129 lt9611_reset(lt9611); 1130 1131 ret = lt9611_read_device_rev(lt9611); 1132 if (ret) { 1133 dev_err(dev, "failed to read chip rev\n"); 1134 goto err_disable_regulators; 1135 } 1136 1137 ret = devm_request_threaded_irq(dev, client->irq, NULL, 1138 lt9611_irq_thread_handler, 1139 IRQF_ONESHOT, "lt9611", lt9611); 1140 if (ret) { 1141 dev_err(dev, "failed to request irq\n"); 1142 goto err_disable_regulators; 1143 } 1144 1145 i2c_set_clientdata(client, lt9611); 1146 1147 /* Disable Audio InfoFrame, enabled by default */ 1148 regmap_update_bits(lt9611->regmap, 0x843d, LT9611_INFOFRAME_AUDIO, 0); 1149 1150 lt9611->bridge.of_node = client->dev.of_node; 1151 lt9611->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID | 1152 DRM_BRIDGE_OP_HPD | DRM_BRIDGE_OP_MODES | 1153 DRM_BRIDGE_OP_HDMI | DRM_BRIDGE_OP_HDMI_AUDIO | 1154 DRM_BRIDGE_OP_HDMI_SPD_INFOFRAME; 1155 lt9611->bridge.type = DRM_MODE_CONNECTOR_HDMIA; 1156 lt9611->bridge.vendor = "Lontium"; 1157 lt9611->bridge.product = "LT9611"; 1158 lt9611->bridge.hdmi_audio_dev = dev; 1159 lt9611->bridge.hdmi_audio_max_i2s_playback_channels = 8; 1160 lt9611->bridge.hdmi_audio_dai_port = 2; 1161 1162 drm_bridge_add(<9611->bridge); 1163 1164 /* Attach primary DSI */ 1165 lt9611->dsi0 = lt9611_attach_dsi(lt9611, lt9611->dsi0_node); 1166 if (IS_ERR(lt9611->dsi0)) { 1167 ret = PTR_ERR(lt9611->dsi0); 1168 goto err_remove_bridge; 1169 } 1170 1171 /* Attach secondary DSI, if specified */ 1172 if (lt9611->dsi1_node) { 1173 lt9611->dsi1 = lt9611_attach_dsi(lt9611, lt9611->dsi1_node); 1174 if (IS_ERR(lt9611->dsi1)) { 1175 ret = PTR_ERR(lt9611->dsi1); 1176 goto err_remove_bridge; 1177 } 1178 } 1179 1180 lt9611_enable_hpd_interrupts(lt9611); 1181 1182 return 0; 1183 1184 err_remove_bridge: 1185 drm_bridge_remove(<9611->bridge); 1186 1187 err_disable_regulators: 1188 regulator_bulk_disable(ARRAY_SIZE(lt9611->supplies), lt9611->supplies); 1189 1190 err_of_put: 1191 of_node_put(lt9611->dsi1_node); 1192 of_node_put(lt9611->dsi0_node); 1193 1194 return ret; 1195 } 1196 1197 static void lt9611_remove(struct i2c_client *client) 1198 { 1199 struct lt9611 *lt9611 = i2c_get_clientdata(client); 1200 1201 disable_irq(client->irq); 1202 drm_bridge_remove(<9611->bridge); 1203 1204 regulator_bulk_disable(ARRAY_SIZE(lt9611->supplies), lt9611->supplies); 1205 1206 of_node_put(lt9611->dsi1_node); 1207 of_node_put(lt9611->dsi0_node); 1208 } 1209 1210 static const struct i2c_device_id lt9611_id[] = { 1211 { "lontium,lt9611" }, 1212 {} 1213 }; 1214 MODULE_DEVICE_TABLE(i2c, lt9611_id); 1215 1216 static const struct of_device_id lt9611_match_table[] = { 1217 { .compatible = "lontium,lt9611" }, 1218 { } 1219 }; 1220 MODULE_DEVICE_TABLE(of, lt9611_match_table); 1221 1222 static struct i2c_driver lt9611_driver = { 1223 .driver = { 1224 .name = "lt9611", 1225 .of_match_table = lt9611_match_table, 1226 }, 1227 .probe = lt9611_probe, 1228 .remove = lt9611_remove, 1229 .id_table = lt9611_id, 1230 }; 1231 module_i2c_driver(lt9611_driver); 1232 1233 MODULE_DESCRIPTION("Lontium LT9611 DSI/HDMI bridge driver"); 1234 MODULE_LICENSE("GPL v2"); 1235