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 lt9611_bridge_detect(struct drm_bridge *bridge) 547 { 548 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 549 unsigned int reg_val = 0; 550 int connected = 0; 551 552 regmap_read(lt9611->regmap, 0x825e, ®_val); 553 connected = (reg_val & (BIT(2) | BIT(0))); 554 555 lt9611->status = connected ? connector_status_connected : 556 connector_status_disconnected; 557 558 return lt9611->status; 559 } 560 561 static int lt9611_read_edid(struct lt9611 *lt9611) 562 { 563 unsigned int temp; 564 int ret = 0; 565 int i, j; 566 567 /* memset to clear old buffer, if any */ 568 memset(lt9611->edid_buf, 0, sizeof(lt9611->edid_buf)); 569 570 regmap_write(lt9611->regmap, 0x8503, 0xc9); 571 572 /* 0xA0 is EDID device address */ 573 regmap_write(lt9611->regmap, 0x8504, 0xa0); 574 /* 0x00 is EDID offset address */ 575 regmap_write(lt9611->regmap, 0x8505, 0x00); 576 577 /* length for read */ 578 regmap_write(lt9611->regmap, 0x8506, EDID_LEN); 579 regmap_write(lt9611->regmap, 0x8514, 0x7f); 580 581 for (i = 0; i < EDID_LOOP; i++) { 582 /* offset address */ 583 regmap_write(lt9611->regmap, 0x8505, i * EDID_LEN); 584 regmap_write(lt9611->regmap, 0x8507, 0x36); 585 regmap_write(lt9611->regmap, 0x8507, 0x31); 586 regmap_write(lt9611->regmap, 0x8507, 0x37); 587 usleep_range(5000, 10000); 588 589 regmap_read(lt9611->regmap, 0x8540, &temp); 590 591 if (temp & KEY_DDC_ACCS_DONE) { 592 for (j = 0; j < EDID_LEN; j++) { 593 regmap_read(lt9611->regmap, 0x8583, &temp); 594 lt9611->edid_buf[i * EDID_LEN + j] = temp; 595 } 596 597 } else if (temp & DDC_NO_ACK) { /* DDC No Ack or Abitration lost */ 598 dev_err(lt9611->dev, "read edid failed: no ack\n"); 599 ret = -EIO; 600 goto end; 601 602 } else { 603 dev_err(lt9611->dev, "read edid failed: access not done\n"); 604 ret = -EIO; 605 goto end; 606 } 607 } 608 609 end: 610 regmap_write(lt9611->regmap, 0x8507, 0x1f); 611 return ret; 612 } 613 614 static int 615 lt9611_get_edid_block(void *data, u8 *buf, unsigned int block, size_t len) 616 { 617 struct lt9611 *lt9611 = data; 618 int ret; 619 620 if (len > 128) 621 return -EINVAL; 622 623 /* supports up to 1 extension block */ 624 /* TODO: add support for more extension blocks */ 625 if (block > 1) 626 return -EINVAL; 627 628 if (block == 0) { 629 ret = lt9611_read_edid(lt9611); 630 if (ret) { 631 dev_err(lt9611->dev, "edid read failed\n"); 632 return ret; 633 } 634 } 635 636 block %= 2; 637 memcpy(buf, lt9611->edid_buf + (block * 128), len); 638 639 return 0; 640 } 641 642 /* bridge funcs */ 643 static void 644 lt9611_bridge_atomic_enable(struct drm_bridge *bridge, 645 struct drm_bridge_state *old_bridge_state) 646 { 647 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 648 struct drm_atomic_state *state = old_bridge_state->base.state; 649 struct drm_connector *connector; 650 struct drm_connector_state *conn_state; 651 struct drm_crtc_state *crtc_state; 652 struct drm_display_mode *mode; 653 unsigned int postdiv; 654 655 connector = drm_atomic_get_new_connector_for_encoder(state, bridge->encoder); 656 if (WARN_ON(!connector)) 657 return; 658 659 conn_state = drm_atomic_get_new_connector_state(state, connector); 660 if (WARN_ON(!conn_state)) 661 return; 662 663 crtc_state = drm_atomic_get_new_crtc_state(state, conn_state->crtc); 664 if (WARN_ON(!crtc_state)) 665 return; 666 667 mode = &crtc_state->adjusted_mode; 668 669 lt9611_mipi_input_digital(lt9611, mode); 670 lt9611_pll_setup(lt9611, mode, &postdiv); 671 lt9611_mipi_video_setup(lt9611, mode); 672 lt9611_pcr_setup(lt9611, mode, postdiv); 673 674 if (lt9611_power_on(lt9611)) { 675 dev_err(lt9611->dev, "power on failed\n"); 676 return; 677 } 678 679 lt9611_mipi_input_analog(lt9611); 680 drm_atomic_helper_connector_hdmi_update_infoframes(connector, state); 681 lt9611_hdmi_tx_digital(lt9611, connector->display_info.is_hdmi); 682 lt9611_hdmi_tx_phy(lt9611); 683 684 msleep(500); 685 686 lt9611_video_check(lt9611); 687 688 /* Enable HDMI output */ 689 regmap_write(lt9611->regmap, 0x8130, 0xea); 690 } 691 692 static void 693 lt9611_bridge_atomic_disable(struct drm_bridge *bridge, 694 struct drm_bridge_state *old_bridge_state) 695 { 696 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 697 int ret; 698 699 /* Disable HDMI output */ 700 ret = regmap_write(lt9611->regmap, 0x8130, 0x6a); 701 if (ret) { 702 dev_err(lt9611->dev, "video on failed\n"); 703 return; 704 } 705 706 if (lt9611_power_off(lt9611)) { 707 dev_err(lt9611->dev, "power on failed\n"); 708 return; 709 } 710 } 711 712 static struct mipi_dsi_device *lt9611_attach_dsi(struct lt9611 *lt9611, 713 struct device_node *dsi_node) 714 { 715 const struct mipi_dsi_device_info info = { "lt9611", 0, lt9611->dev->of_node}; 716 struct mipi_dsi_device *dsi; 717 struct mipi_dsi_host *host; 718 struct device *dev = lt9611->dev; 719 int ret; 720 721 host = of_find_mipi_dsi_host_by_node(dsi_node); 722 if (!host) 723 return ERR_PTR(dev_err_probe(lt9611->dev, -EPROBE_DEFER, "failed to find dsi host\n")); 724 725 dsi = devm_mipi_dsi_device_register_full(dev, host, &info); 726 if (IS_ERR(dsi)) { 727 dev_err(lt9611->dev, "failed to create dsi device\n"); 728 return dsi; 729 } 730 731 dsi->lanes = 4; 732 dsi->format = MIPI_DSI_FMT_RGB888; 733 dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE | 734 MIPI_DSI_MODE_VIDEO_HSE; 735 736 ret = devm_mipi_dsi_attach(dev, dsi); 737 if (ret < 0) { 738 dev_err(dev, "failed to attach dsi to host\n"); 739 return ERR_PTR(ret); 740 } 741 742 return dsi; 743 } 744 745 static int lt9611_bridge_attach(struct drm_bridge *bridge, 746 enum drm_bridge_attach_flags flags) 747 { 748 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 749 750 return drm_bridge_attach(bridge->encoder, lt9611->next_bridge, 751 bridge, flags); 752 } 753 754 static enum drm_mode_status lt9611_bridge_mode_valid(struct drm_bridge *bridge, 755 const struct drm_display_info *info, 756 const struct drm_display_mode *mode) 757 { 758 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 759 760 if (mode->hdisplay > 3840) 761 return MODE_BAD_HVALUE; 762 763 if (mode->hdisplay > 2000 && !lt9611->dsi1_node) 764 return MODE_PANEL; 765 766 return MODE_OK; 767 } 768 769 static void lt9611_bridge_atomic_pre_enable(struct drm_bridge *bridge, 770 struct drm_bridge_state *old_bridge_state) 771 { 772 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 773 static const struct reg_sequence reg_cfg[] = { 774 { 0x8102, 0x12 }, 775 { 0x8123, 0x40 }, 776 { 0x8130, 0xea }, 777 { 0x8011, 0xfa }, 778 }; 779 780 if (!lt9611->sleep) 781 return; 782 783 regmap_multi_reg_write(lt9611->regmap, 784 reg_cfg, ARRAY_SIZE(reg_cfg)); 785 786 lt9611->sleep = false; 787 } 788 789 static void 790 lt9611_bridge_atomic_post_disable(struct drm_bridge *bridge, 791 struct drm_bridge_state *old_bridge_state) 792 { 793 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 794 795 lt9611_sleep_setup(lt9611); 796 } 797 798 static const struct drm_edid *lt9611_bridge_edid_read(struct drm_bridge *bridge, 799 struct drm_connector *connector) 800 { 801 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 802 803 lt9611_power_on(lt9611); 804 return drm_edid_read_custom(connector, lt9611_get_edid_block, lt9611); 805 } 806 807 static void lt9611_bridge_hpd_enable(struct drm_bridge *bridge) 808 { 809 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 810 811 lt9611_enable_hpd_interrupts(lt9611); 812 } 813 814 #define MAX_INPUT_SEL_FORMATS 1 815 816 static u32 * 817 lt9611_atomic_get_input_bus_fmts(struct drm_bridge *bridge, 818 struct drm_bridge_state *bridge_state, 819 struct drm_crtc_state *crtc_state, 820 struct drm_connector_state *conn_state, 821 u32 output_fmt, 822 unsigned int *num_input_fmts) 823 { 824 u32 *input_fmts; 825 826 *num_input_fmts = 0; 827 828 input_fmts = kcalloc(MAX_INPUT_SEL_FORMATS, sizeof(*input_fmts), 829 GFP_KERNEL); 830 if (!input_fmts) 831 return NULL; 832 833 /* This is the DSI-end bus format */ 834 input_fmts[0] = MEDIA_BUS_FMT_RGB888_1X24; 835 *num_input_fmts = 1; 836 837 return input_fmts; 838 } 839 840 /* 841 * Other working frames: 842 * - 0x01, 0x84df 843 * - 0x04, 0x84c0 844 */ 845 #define LT9611_INFOFRAME_AUDIO 0x02 846 #define LT9611_INFOFRAME_AVI 0x08 847 #define LT9611_INFOFRAME_SPD 0x10 848 #define LT9611_INFOFRAME_VENDOR 0x20 849 850 static int lt9611_hdmi_clear_infoframe(struct drm_bridge *bridge, 851 enum hdmi_infoframe_type type) 852 { 853 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 854 unsigned int mask; 855 856 switch (type) { 857 case HDMI_INFOFRAME_TYPE_AUDIO: 858 mask = LT9611_INFOFRAME_AUDIO; 859 break; 860 861 case HDMI_INFOFRAME_TYPE_AVI: 862 mask = LT9611_INFOFRAME_AVI; 863 break; 864 865 case HDMI_INFOFRAME_TYPE_SPD: 866 mask = LT9611_INFOFRAME_SPD; 867 break; 868 869 case HDMI_INFOFRAME_TYPE_VENDOR: 870 mask = LT9611_INFOFRAME_VENDOR; 871 break; 872 873 default: 874 drm_dbg_driver(lt9611->bridge.dev, "Unsupported HDMI InfoFrame %x\n", type); 875 mask = 0; 876 break; 877 } 878 879 if (mask) 880 regmap_update_bits(lt9611->regmap, 0x843d, mask, 0); 881 882 return 0; 883 } 884 885 static int lt9611_hdmi_write_infoframe(struct drm_bridge *bridge, 886 enum hdmi_infoframe_type type, 887 const u8 *buffer, size_t len) 888 { 889 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 890 unsigned int mask, addr; 891 int i; 892 893 switch (type) { 894 case HDMI_INFOFRAME_TYPE_AUDIO: 895 mask = LT9611_INFOFRAME_AUDIO; 896 addr = 0x84b2; 897 break; 898 899 case HDMI_INFOFRAME_TYPE_AVI: 900 mask = LT9611_INFOFRAME_AVI; 901 addr = 0x8440; 902 break; 903 904 case HDMI_INFOFRAME_TYPE_SPD: 905 mask = LT9611_INFOFRAME_SPD; 906 addr = 0x8493; 907 break; 908 909 case HDMI_INFOFRAME_TYPE_VENDOR: 910 mask = LT9611_INFOFRAME_VENDOR; 911 addr = 0x8474; 912 break; 913 914 default: 915 drm_dbg_driver(lt9611->bridge.dev, "Unsupported HDMI InfoFrame %x\n", type); 916 mask = 0; 917 break; 918 } 919 920 if (mask) { 921 for (i = 0; i < len; i++) 922 regmap_write(lt9611->regmap, addr + i, buffer[i]); 923 924 regmap_update_bits(lt9611->regmap, 0x843d, mask, mask); 925 } 926 927 return 0; 928 } 929 930 static enum drm_mode_status 931 lt9611_hdmi_tmds_char_rate_valid(const struct drm_bridge *bridge, 932 const struct drm_display_mode *mode, 933 unsigned long long tmds_rate) 934 { 935 /* 297 MHz for 4k@30 mode */ 936 if (tmds_rate > 297000000) 937 return MODE_CLOCK_HIGH; 938 939 return MODE_OK; 940 } 941 942 static int lt9611_hdmi_audio_startup(struct drm_connector *connector, 943 struct drm_bridge *bridge) 944 { 945 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 946 947 regmap_write(lt9611->regmap, 0x82d6, 0x8c); 948 regmap_write(lt9611->regmap, 0x82d7, 0x04); 949 950 regmap_write(lt9611->regmap, 0x8406, 0x08); 951 regmap_write(lt9611->regmap, 0x8407, 0x10); 952 953 regmap_write(lt9611->regmap, 0x8434, 0xd5); 954 955 return 0; 956 } 957 958 static int lt9611_hdmi_audio_prepare(struct drm_connector *connector, 959 struct drm_bridge *bridge, 960 struct hdmi_codec_daifmt *fmt, 961 struct hdmi_codec_params *hparms) 962 { 963 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 964 965 if (hparms->sample_rate == 48000) 966 regmap_write(lt9611->regmap, 0x840f, 0x2b); 967 else if (hparms->sample_rate == 96000) 968 regmap_write(lt9611->regmap, 0x840f, 0xab); 969 else 970 return -EINVAL; 971 972 regmap_write(lt9611->regmap, 0x8435, 0x00); 973 regmap_write(lt9611->regmap, 0x8436, 0x18); 974 regmap_write(lt9611->regmap, 0x8437, 0x00); 975 976 return drm_atomic_helper_connector_hdmi_update_audio_infoframe(connector, 977 &hparms->cea); 978 } 979 980 static void lt9611_hdmi_audio_shutdown(struct drm_connector *connector, 981 struct drm_bridge *bridge) 982 { 983 struct lt9611 *lt9611 = bridge_to_lt9611(bridge); 984 985 drm_atomic_helper_connector_hdmi_clear_audio_infoframe(connector); 986 987 regmap_write(lt9611->regmap, 0x8406, 0x00); 988 regmap_write(lt9611->regmap, 0x8407, 0x00); 989 } 990 991 static const struct drm_bridge_funcs lt9611_bridge_funcs = { 992 .attach = lt9611_bridge_attach, 993 .mode_valid = lt9611_bridge_mode_valid, 994 .detect = lt9611_bridge_detect, 995 .edid_read = lt9611_bridge_edid_read, 996 .hpd_enable = lt9611_bridge_hpd_enable, 997 998 .atomic_pre_enable = lt9611_bridge_atomic_pre_enable, 999 .atomic_enable = lt9611_bridge_atomic_enable, 1000 .atomic_disable = lt9611_bridge_atomic_disable, 1001 .atomic_post_disable = lt9611_bridge_atomic_post_disable, 1002 .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state, 1003 .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state, 1004 .atomic_reset = drm_atomic_helper_bridge_reset, 1005 .atomic_get_input_bus_fmts = lt9611_atomic_get_input_bus_fmts, 1006 1007 .hdmi_tmds_char_rate_valid = lt9611_hdmi_tmds_char_rate_valid, 1008 .hdmi_write_infoframe = lt9611_hdmi_write_infoframe, 1009 .hdmi_clear_infoframe = lt9611_hdmi_clear_infoframe, 1010 1011 .hdmi_audio_startup = lt9611_hdmi_audio_startup, 1012 .hdmi_audio_prepare = lt9611_hdmi_audio_prepare, 1013 .hdmi_audio_shutdown = lt9611_hdmi_audio_shutdown, 1014 }; 1015 1016 static int lt9611_parse_dt(struct device *dev, 1017 struct lt9611 *lt9611) 1018 { 1019 lt9611->dsi0_node = of_graph_get_remote_node(dev->of_node, 0, -1); 1020 if (!lt9611->dsi0_node) { 1021 dev_err(lt9611->dev, "failed to get remote node for primary dsi\n"); 1022 return -ENODEV; 1023 } 1024 1025 lt9611->dsi1_node = of_graph_get_remote_node(dev->of_node, 1, -1); 1026 1027 lt9611->ac_mode = of_property_read_bool(dev->of_node, "lt,ac-mode"); 1028 1029 return drm_of_find_panel_or_bridge(dev->of_node, 2, -1, NULL, <9611->next_bridge); 1030 } 1031 1032 static int lt9611_gpio_init(struct lt9611 *lt9611) 1033 { 1034 struct device *dev = lt9611->dev; 1035 1036 lt9611->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 1037 if (IS_ERR(lt9611->reset_gpio)) { 1038 dev_err(dev, "failed to acquire reset gpio\n"); 1039 return PTR_ERR(lt9611->reset_gpio); 1040 } 1041 1042 lt9611->enable_gpio = devm_gpiod_get_optional(dev, "enable", 1043 GPIOD_OUT_LOW); 1044 if (IS_ERR(lt9611->enable_gpio)) { 1045 dev_err(dev, "failed to acquire enable gpio\n"); 1046 return PTR_ERR(lt9611->enable_gpio); 1047 } 1048 1049 return 0; 1050 } 1051 1052 static int lt9611_read_device_rev(struct lt9611 *lt9611) 1053 { 1054 unsigned int rev; 1055 int ret; 1056 1057 regmap_write(lt9611->regmap, 0x80ee, 0x01); 1058 ret = regmap_read(lt9611->regmap, 0x8002, &rev); 1059 if (ret) 1060 dev_err(lt9611->dev, "failed to read revision: %d\n", ret); 1061 else 1062 dev_info(lt9611->dev, "LT9611 revision: 0x%x\n", rev); 1063 1064 return ret; 1065 } 1066 1067 static int lt9611_probe(struct i2c_client *client) 1068 { 1069 struct lt9611 *lt9611; 1070 struct device *dev = &client->dev; 1071 int ret; 1072 1073 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 1074 dev_err(dev, "device doesn't support I2C\n"); 1075 return -ENODEV; 1076 } 1077 1078 lt9611 = devm_kzalloc(dev, sizeof(*lt9611), GFP_KERNEL); 1079 if (!lt9611) 1080 return -ENOMEM; 1081 1082 lt9611->dev = dev; 1083 lt9611->client = client; 1084 lt9611->sleep = false; 1085 1086 lt9611->regmap = devm_regmap_init_i2c(client, <9611_regmap_config); 1087 if (IS_ERR(lt9611->regmap)) { 1088 dev_err(lt9611->dev, "regmap i2c init failed\n"); 1089 return PTR_ERR(lt9611->regmap); 1090 } 1091 1092 ret = lt9611_parse_dt(dev, lt9611); 1093 if (ret) { 1094 dev_err(dev, "failed to parse device tree\n"); 1095 return ret; 1096 } 1097 1098 ret = lt9611_gpio_init(lt9611); 1099 if (ret < 0) 1100 goto err_of_put; 1101 1102 ret = lt9611_regulator_init(lt9611); 1103 if (ret < 0) 1104 goto err_of_put; 1105 1106 lt9611_assert_5v(lt9611); 1107 1108 ret = lt9611_regulator_enable(lt9611); 1109 if (ret) 1110 goto err_of_put; 1111 1112 lt9611_reset(lt9611); 1113 1114 ret = lt9611_read_device_rev(lt9611); 1115 if (ret) { 1116 dev_err(dev, "failed to read chip rev\n"); 1117 goto err_disable_regulators; 1118 } 1119 1120 ret = devm_request_threaded_irq(dev, client->irq, NULL, 1121 lt9611_irq_thread_handler, 1122 IRQF_ONESHOT, "lt9611", lt9611); 1123 if (ret) { 1124 dev_err(dev, "failed to request irq\n"); 1125 goto err_disable_regulators; 1126 } 1127 1128 i2c_set_clientdata(client, lt9611); 1129 1130 /* Disable Audio InfoFrame, enabled by default */ 1131 regmap_update_bits(lt9611->regmap, 0x843d, LT9611_INFOFRAME_AUDIO, 0); 1132 1133 lt9611->bridge.funcs = <9611_bridge_funcs; 1134 lt9611->bridge.of_node = client->dev.of_node; 1135 lt9611->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID | 1136 DRM_BRIDGE_OP_HPD | DRM_BRIDGE_OP_MODES | 1137 DRM_BRIDGE_OP_HDMI; 1138 lt9611->bridge.type = DRM_MODE_CONNECTOR_HDMIA; 1139 lt9611->bridge.vendor = "Lontium"; 1140 lt9611->bridge.product = "LT9611"; 1141 lt9611->bridge.hdmi_audio_dev = dev; 1142 lt9611->bridge.hdmi_audio_max_i2s_playback_channels = 8; 1143 lt9611->bridge.hdmi_audio_dai_port = 2; 1144 1145 drm_bridge_add(<9611->bridge); 1146 1147 /* Attach primary DSI */ 1148 lt9611->dsi0 = lt9611_attach_dsi(lt9611, lt9611->dsi0_node); 1149 if (IS_ERR(lt9611->dsi0)) { 1150 ret = PTR_ERR(lt9611->dsi0); 1151 goto err_remove_bridge; 1152 } 1153 1154 /* Attach secondary DSI, if specified */ 1155 if (lt9611->dsi1_node) { 1156 lt9611->dsi1 = lt9611_attach_dsi(lt9611, lt9611->dsi1_node); 1157 if (IS_ERR(lt9611->dsi1)) { 1158 ret = PTR_ERR(lt9611->dsi1); 1159 goto err_remove_bridge; 1160 } 1161 } 1162 1163 lt9611_enable_hpd_interrupts(lt9611); 1164 1165 return 0; 1166 1167 err_remove_bridge: 1168 drm_bridge_remove(<9611->bridge); 1169 1170 err_disable_regulators: 1171 regulator_bulk_disable(ARRAY_SIZE(lt9611->supplies), lt9611->supplies); 1172 1173 err_of_put: 1174 of_node_put(lt9611->dsi1_node); 1175 of_node_put(lt9611->dsi0_node); 1176 1177 return ret; 1178 } 1179 1180 static void lt9611_remove(struct i2c_client *client) 1181 { 1182 struct lt9611 *lt9611 = i2c_get_clientdata(client); 1183 1184 disable_irq(client->irq); 1185 drm_bridge_remove(<9611->bridge); 1186 1187 regulator_bulk_disable(ARRAY_SIZE(lt9611->supplies), lt9611->supplies); 1188 1189 of_node_put(lt9611->dsi1_node); 1190 of_node_put(lt9611->dsi0_node); 1191 } 1192 1193 static const struct i2c_device_id lt9611_id[] = { 1194 { "lontium,lt9611" }, 1195 {} 1196 }; 1197 MODULE_DEVICE_TABLE(i2c, lt9611_id); 1198 1199 static const struct of_device_id lt9611_match_table[] = { 1200 { .compatible = "lontium,lt9611" }, 1201 { } 1202 }; 1203 MODULE_DEVICE_TABLE(of, lt9611_match_table); 1204 1205 static struct i2c_driver lt9611_driver = { 1206 .driver = { 1207 .name = "lt9611", 1208 .of_match_table = lt9611_match_table, 1209 }, 1210 .probe = lt9611_probe, 1211 .remove = lt9611_remove, 1212 .id_table = lt9611_id, 1213 }; 1214 module_i2c_driver(lt9611_driver); 1215 1216 MODULE_DESCRIPTION("Lontium LT9611 DSI/HDMI bridge driver"); 1217 MODULE_LICENSE("GPL v2"); 1218