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