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