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