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