1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) Rockchip Electronics Co., Ltd. 4 * Author: Chris Zhong <zyw@rock-chips.com> 5 */ 6 7 #include <linux/clk.h> 8 #include <linux/component.h> 9 #include <linux/extcon.h> 10 #include <linux/firmware.h> 11 #include <linux/mfd/syscon.h> 12 #include <linux/phy/phy.h> 13 #include <linux/regmap.h> 14 #include <linux/reset.h> 15 16 #include <sound/hdmi-codec.h> 17 18 #include <drm/display/drm_dp_helper.h> 19 #include <drm/display/drm_hdmi_audio_helper.h> 20 #include <drm/drm_atomic_helper.h> 21 #include <drm/drm_bridge_connector.h> 22 #include <drm/drm_edid.h> 23 #include <drm/drm_of.h> 24 #include <drm/drm_print.h> 25 #include <drm/drm_probe_helper.h> 26 27 #include "cdn-dp-core.h" 28 #include "cdn-dp-reg.h" 29 30 static inline struct cdn_dp_device *bridge_to_dp(struct drm_bridge *bridge) 31 { 32 return container_of(bridge, struct cdn_dp_device, bridge); 33 } 34 35 static inline struct cdn_dp_device *encoder_to_dp(struct drm_encoder *encoder) 36 { 37 struct rockchip_encoder *rkencoder = to_rockchip_encoder(encoder); 38 39 return container_of(rkencoder, struct cdn_dp_device, encoder); 40 } 41 42 #define GRF_SOC_CON9 0x6224 43 #define DP_SEL_VOP_LIT BIT(12) 44 #define GRF_SOC_CON26 0x6268 45 #define DPTX_HPD_SEL (3 << 12) 46 #define DPTX_HPD_DEL (2 << 12) 47 #define DPTX_HPD_SEL_MASK (3 << 28) 48 49 #define CDN_FW_TIMEOUT_MS (64 * 1000) 50 #define CDN_DPCD_TIMEOUT_MS 5000 51 #define CDN_DP_FIRMWARE "rockchip/dptx.bin" 52 MODULE_FIRMWARE(CDN_DP_FIRMWARE); 53 54 struct cdn_dp_data { 55 u8 max_phy; 56 }; 57 58 static struct cdn_dp_data rk3399_cdn_dp = { 59 .max_phy = 2, 60 }; 61 62 static const struct of_device_id cdn_dp_dt_ids[] = { 63 { .compatible = "rockchip,rk3399-cdn-dp", 64 .data = (void *)&rk3399_cdn_dp }, 65 {} 66 }; 67 68 MODULE_DEVICE_TABLE(of, cdn_dp_dt_ids); 69 70 static int cdn_dp_grf_write(struct cdn_dp_device *dp, 71 unsigned int reg, unsigned int val) 72 { 73 int ret; 74 75 ret = clk_prepare_enable(dp->grf_clk); 76 if (ret) { 77 DRM_DEV_ERROR(dp->dev, "Failed to prepare_enable grf clock\n"); 78 return ret; 79 } 80 81 ret = regmap_write(dp->grf, reg, val); 82 if (ret) { 83 DRM_DEV_ERROR(dp->dev, "Could not write to GRF: %d\n", ret); 84 clk_disable_unprepare(dp->grf_clk); 85 return ret; 86 } 87 88 clk_disable_unprepare(dp->grf_clk); 89 90 return 0; 91 } 92 93 static int cdn_dp_clk_enable(struct cdn_dp_device *dp) 94 { 95 int ret; 96 unsigned long rate; 97 98 ret = clk_prepare_enable(dp->pclk); 99 if (ret < 0) { 100 DRM_DEV_ERROR(dp->dev, "cannot enable dp pclk %d\n", ret); 101 goto err_pclk; 102 } 103 104 ret = clk_prepare_enable(dp->core_clk); 105 if (ret < 0) { 106 DRM_DEV_ERROR(dp->dev, "cannot enable core_clk %d\n", ret); 107 goto err_core_clk; 108 } 109 110 ret = pm_runtime_get_sync(dp->dev); 111 if (ret < 0) { 112 DRM_DEV_ERROR(dp->dev, "cannot get pm runtime %d\n", ret); 113 goto err_pm_runtime_get; 114 } 115 116 reset_control_assert(dp->core_rst); 117 reset_control_assert(dp->dptx_rst); 118 reset_control_assert(dp->apb_rst); 119 reset_control_deassert(dp->core_rst); 120 reset_control_deassert(dp->dptx_rst); 121 reset_control_deassert(dp->apb_rst); 122 123 rate = clk_get_rate(dp->core_clk); 124 if (!rate) { 125 DRM_DEV_ERROR(dp->dev, "get clk rate failed\n"); 126 ret = -EINVAL; 127 goto err_set_rate; 128 } 129 130 cdn_dp_set_fw_clk(dp, rate); 131 cdn_dp_clock_reset(dp); 132 133 return 0; 134 135 err_set_rate: 136 pm_runtime_put(dp->dev); 137 err_pm_runtime_get: 138 clk_disable_unprepare(dp->core_clk); 139 err_core_clk: 140 clk_disable_unprepare(dp->pclk); 141 err_pclk: 142 return ret; 143 } 144 145 static void cdn_dp_clk_disable(struct cdn_dp_device *dp) 146 { 147 pm_runtime_put_sync(dp->dev); 148 clk_disable_unprepare(dp->pclk); 149 clk_disable_unprepare(dp->core_clk); 150 } 151 152 static int cdn_dp_get_port_lanes(struct cdn_dp_port *port) 153 { 154 struct extcon_dev *edev = port->extcon; 155 union extcon_property_value property; 156 int dptx; 157 u8 lanes; 158 159 dptx = extcon_get_state(edev, EXTCON_DISP_DP); 160 if (dptx > 0) { 161 extcon_get_property(edev, EXTCON_DISP_DP, 162 EXTCON_PROP_USB_SS, &property); 163 if (property.intval) 164 lanes = 2; 165 else 166 lanes = 4; 167 } else { 168 lanes = 0; 169 } 170 171 return lanes; 172 } 173 174 static int cdn_dp_get_sink_count(struct cdn_dp_device *dp, u8 *sink_count) 175 { 176 int ret; 177 u8 value; 178 179 *sink_count = 0; 180 ret = cdn_dp_dpcd_read(dp, DP_SINK_COUNT, &value, 1); 181 if (ret) 182 return ret; 183 184 *sink_count = DP_GET_SINK_COUNT(value); 185 return 0; 186 } 187 188 static struct cdn_dp_port *cdn_dp_connected_port(struct cdn_dp_device *dp) 189 { 190 struct cdn_dp_port *port; 191 int i, lanes; 192 193 for (i = 0; i < dp->ports; i++) { 194 port = dp->port[i]; 195 lanes = cdn_dp_get_port_lanes(port); 196 if (lanes) 197 return port; 198 } 199 return NULL; 200 } 201 202 static bool cdn_dp_check_sink_connection(struct cdn_dp_device *dp) 203 { 204 unsigned long timeout = jiffies + msecs_to_jiffies(CDN_DPCD_TIMEOUT_MS); 205 struct cdn_dp_port *port; 206 u8 sink_count = 0; 207 208 if (dp->active_port < 0 || dp->active_port >= dp->ports) { 209 DRM_DEV_ERROR(dp->dev, "active_port is wrong!\n"); 210 return false; 211 } 212 213 port = dp->port[dp->active_port]; 214 215 /* 216 * Attempt to read sink count, retry in case the sink may not be ready. 217 * 218 * Sinks are *supposed* to come up within 1ms from an off state, but 219 * some docks need more time to power up. 220 */ 221 while (time_before(jiffies, timeout)) { 222 if (!extcon_get_state(port->extcon, EXTCON_DISP_DP)) 223 return false; 224 225 if (!cdn_dp_get_sink_count(dp, &sink_count)) 226 return sink_count ? true : false; 227 228 usleep_range(5000, 10000); 229 } 230 231 DRM_DEV_ERROR(dp->dev, "Get sink capability timed out\n"); 232 return false; 233 } 234 235 static enum drm_connector_status 236 cdn_dp_bridge_detect(struct drm_bridge *bridge, struct drm_connector *connector) 237 { 238 struct cdn_dp_device *dp = bridge_to_dp(bridge); 239 enum drm_connector_status status = connector_status_disconnected; 240 241 mutex_lock(&dp->lock); 242 if (dp->connected) 243 status = connector_status_connected; 244 mutex_unlock(&dp->lock); 245 246 return status; 247 } 248 249 static const struct drm_edid * 250 cdn_dp_bridge_edid_read(struct drm_bridge *bridge, struct drm_connector *connector) 251 { 252 struct cdn_dp_device *dp = bridge_to_dp(bridge); 253 const struct drm_edid *drm_edid; 254 255 mutex_lock(&dp->lock); 256 drm_edid = drm_edid_read_custom(connector, cdn_dp_get_edid_block, dp); 257 mutex_unlock(&dp->lock); 258 259 return drm_edid; 260 } 261 262 static enum drm_mode_status 263 cdn_dp_bridge_mode_valid(struct drm_bridge *bridge, 264 const struct drm_display_info *display_info, 265 const struct drm_display_mode *mode) 266 { 267 struct cdn_dp_device *dp = bridge_to_dp(bridge); 268 u32 requested, actual, rate, sink_max, source_max = 0; 269 u8 lanes, bpc; 270 271 /* If DP is disconnected, every mode is invalid */ 272 if (!dp->connected) 273 return MODE_BAD; 274 275 switch (display_info->bpc) { 276 case 10: 277 bpc = 10; 278 break; 279 case 6: 280 bpc = 6; 281 break; 282 default: 283 bpc = 8; 284 break; 285 } 286 287 requested = mode->clock * bpc * 3 / 1000; 288 289 source_max = dp->lanes; 290 sink_max = drm_dp_max_lane_count(dp->dpcd); 291 lanes = min(source_max, sink_max); 292 293 source_max = drm_dp_bw_code_to_link_rate(CDN_DP_MAX_LINK_RATE); 294 sink_max = drm_dp_max_link_rate(dp->dpcd); 295 rate = min(source_max, sink_max); 296 297 actual = rate * lanes / 100; 298 299 /* efficiency is about 0.8 */ 300 actual = actual * 8 / 10; 301 302 if (requested > actual) { 303 DRM_DEV_DEBUG_KMS(dp->dev, 304 "requested=%d, actual=%d, clock=%d\n", 305 requested, actual, mode->clock); 306 return MODE_CLOCK_HIGH; 307 } 308 309 return MODE_OK; 310 } 311 312 static int cdn_dp_firmware_init(struct cdn_dp_device *dp) 313 { 314 int ret; 315 const u32 *iram_data, *dram_data; 316 const struct firmware *fw = dp->fw; 317 const struct cdn_firmware_header *hdr; 318 319 hdr = (struct cdn_firmware_header *)fw->data; 320 if (fw->size != le32_to_cpu(hdr->size_bytes)) { 321 DRM_DEV_ERROR(dp->dev, "firmware is invalid\n"); 322 return -EINVAL; 323 } 324 325 iram_data = (const u32 *)(fw->data + hdr->header_size); 326 dram_data = (const u32 *)(fw->data + hdr->header_size + hdr->iram_size); 327 328 ret = cdn_dp_load_firmware(dp, iram_data, hdr->iram_size, 329 dram_data, hdr->dram_size); 330 if (ret) 331 return ret; 332 333 ret = cdn_dp_set_firmware_active(dp, true); 334 if (ret) { 335 DRM_DEV_ERROR(dp->dev, "active ucpu failed: %d\n", ret); 336 return ret; 337 } 338 339 return cdn_dp_event_config(dp); 340 } 341 342 static int cdn_dp_get_sink_capability(struct cdn_dp_device *dp) 343 { 344 int ret; 345 346 if (!cdn_dp_check_sink_connection(dp)) 347 return -ENODEV; 348 349 ret = cdn_dp_dpcd_read(dp, DP_DPCD_REV, dp->dpcd, 350 DP_RECEIVER_CAP_SIZE); 351 if (ret) { 352 DRM_DEV_ERROR(dp->dev, "Failed to get caps %d\n", ret); 353 return ret; 354 } 355 356 return 0; 357 } 358 359 static int cdn_dp_enable_phy(struct cdn_dp_device *dp, struct cdn_dp_port *port) 360 { 361 union extcon_property_value property; 362 int ret; 363 364 if (!port->phy_enabled) { 365 ret = phy_power_on(port->phy); 366 if (ret) { 367 DRM_DEV_ERROR(dp->dev, "phy power on failed: %d\n", 368 ret); 369 goto err_phy; 370 } 371 port->phy_enabled = true; 372 } 373 374 ret = cdn_dp_grf_write(dp, GRF_SOC_CON26, 375 DPTX_HPD_SEL_MASK | DPTX_HPD_SEL); 376 if (ret) { 377 DRM_DEV_ERROR(dp->dev, "Failed to write HPD_SEL %d\n", ret); 378 goto err_power_on; 379 } 380 381 ret = cdn_dp_get_hpd_status(dp); 382 if (ret <= 0) { 383 if (!ret) 384 DRM_DEV_ERROR(dp->dev, "hpd does not exist\n"); 385 goto err_power_on; 386 } 387 388 ret = extcon_get_property(port->extcon, EXTCON_DISP_DP, 389 EXTCON_PROP_USB_TYPEC_POLARITY, &property); 390 if (ret) { 391 DRM_DEV_ERROR(dp->dev, "get property failed\n"); 392 goto err_power_on; 393 } 394 395 port->lanes = cdn_dp_get_port_lanes(port); 396 ret = cdn_dp_set_host_cap(dp, port->lanes, property.intval); 397 if (ret) { 398 DRM_DEV_ERROR(dp->dev, "set host capabilities failed: %d\n", 399 ret); 400 goto err_power_on; 401 } 402 403 dp->active_port = port->id; 404 return 0; 405 406 err_power_on: 407 if (phy_power_off(port->phy)) 408 DRM_DEV_ERROR(dp->dev, "phy power off failed: %d", ret); 409 else 410 port->phy_enabled = false; 411 412 err_phy: 413 cdn_dp_grf_write(dp, GRF_SOC_CON26, 414 DPTX_HPD_SEL_MASK | DPTX_HPD_DEL); 415 return ret; 416 } 417 418 static int cdn_dp_disable_phy(struct cdn_dp_device *dp, 419 struct cdn_dp_port *port) 420 { 421 int ret; 422 423 if (port->phy_enabled) { 424 ret = phy_power_off(port->phy); 425 if (ret) { 426 DRM_DEV_ERROR(dp->dev, "phy power off failed: %d", ret); 427 return ret; 428 } 429 } 430 431 port->phy_enabled = false; 432 port->lanes = 0; 433 dp->active_port = -1; 434 return 0; 435 } 436 437 static int cdn_dp_disable(struct cdn_dp_device *dp) 438 { 439 int ret, i; 440 441 if (!dp->active) 442 return 0; 443 444 for (i = 0; i < dp->ports; i++) 445 cdn_dp_disable_phy(dp, dp->port[i]); 446 447 ret = cdn_dp_grf_write(dp, GRF_SOC_CON26, 448 DPTX_HPD_SEL_MASK | DPTX_HPD_DEL); 449 if (ret) { 450 DRM_DEV_ERROR(dp->dev, "Failed to clear hpd sel %d\n", 451 ret); 452 return ret; 453 } 454 455 cdn_dp_set_firmware_active(dp, false); 456 cdn_dp_clk_disable(dp); 457 dp->active = false; 458 dp->max_lanes = 0; 459 dp->max_rate = 0; 460 461 return 0; 462 } 463 464 static int cdn_dp_enable(struct cdn_dp_device *dp) 465 { 466 int ret, i, lanes; 467 struct cdn_dp_port *port; 468 469 port = cdn_dp_connected_port(dp); 470 if (!port) { 471 DRM_DEV_ERROR(dp->dev, 472 "Can't enable without connection\n"); 473 return -ENODEV; 474 } 475 476 if (dp->active) 477 return 0; 478 479 ret = cdn_dp_clk_enable(dp); 480 if (ret) 481 return ret; 482 483 ret = cdn_dp_firmware_init(dp); 484 if (ret) { 485 DRM_DEV_ERROR(dp->dev, "firmware init failed: %d", ret); 486 goto err_clk_disable; 487 } 488 489 /* only enable the port that connected with downstream device */ 490 for (i = port->id; i < dp->ports; i++) { 491 port = dp->port[i]; 492 lanes = cdn_dp_get_port_lanes(port); 493 if (lanes) { 494 ret = cdn_dp_enable_phy(dp, port); 495 if (ret) 496 continue; 497 498 ret = cdn_dp_get_sink_capability(dp); 499 if (ret) { 500 cdn_dp_disable_phy(dp, port); 501 } else { 502 dp->active = true; 503 dp->lanes = port->lanes; 504 return 0; 505 } 506 } 507 } 508 509 err_clk_disable: 510 cdn_dp_clk_disable(dp); 511 return ret; 512 } 513 514 static void cdn_dp_bridge_mode_set(struct drm_bridge *bridge, 515 const struct drm_display_mode *mode, 516 const struct drm_display_mode *adjusted) 517 { 518 struct cdn_dp_device *dp = bridge_to_dp(bridge); 519 struct video_info *video = &dp->video_info; 520 521 video->color_fmt = PXL_RGB; 522 video->v_sync_polarity = !!(mode->flags & DRM_MODE_FLAG_NVSYNC); 523 video->h_sync_polarity = !!(mode->flags & DRM_MODE_FLAG_NHSYNC); 524 525 drm_mode_copy(&dp->mode, adjusted); 526 } 527 528 static bool cdn_dp_check_link_status(struct cdn_dp_device *dp) 529 { 530 u8 link_status[DP_LINK_STATUS_SIZE]; 531 struct cdn_dp_port *port = cdn_dp_connected_port(dp); 532 u8 sink_lanes = drm_dp_max_lane_count(dp->dpcd); 533 534 if (!port || !dp->max_rate || !dp->max_lanes) 535 return false; 536 537 if (cdn_dp_dpcd_read(dp, DP_LANE0_1_STATUS, link_status, 538 DP_LINK_STATUS_SIZE)) { 539 DRM_ERROR("Failed to get link status\n"); 540 return false; 541 } 542 543 /* if link training is requested we should perform it always */ 544 return drm_dp_channel_eq_ok(link_status, min(port->lanes, sink_lanes)); 545 } 546 547 static void cdn_dp_display_info_update(struct cdn_dp_device *dp, 548 struct drm_display_info *display_info) 549 { 550 struct video_info *video = &dp->video_info; 551 552 switch (display_info->bpc) { 553 case 10: 554 video->color_depth = 10; 555 break; 556 case 6: 557 video->color_depth = 6; 558 break; 559 default: 560 video->color_depth = 8; 561 break; 562 } 563 } 564 565 static void cdn_dp_bridge_atomic_enable(struct drm_bridge *bridge, struct drm_atomic_commit *state) 566 { 567 struct cdn_dp_device *dp = bridge_to_dp(bridge); 568 struct drm_connector *connector; 569 int ret, val; 570 571 connector = drm_atomic_get_new_connector_for_encoder(state, bridge->encoder); 572 if (!connector) 573 return; 574 575 cdn_dp_display_info_update(dp, &connector->display_info); 576 577 ret = drm_of_encoder_active_endpoint_id(dp->dev->of_node, &dp->encoder.encoder); 578 if (ret < 0) { 579 DRM_DEV_ERROR(dp->dev, "Could not get vop id, %d", ret); 580 return; 581 } 582 583 DRM_DEV_DEBUG_KMS(dp->dev, "vop %s output to cdn-dp\n", 584 (ret) ? "LIT" : "BIG"); 585 if (ret) 586 val = DP_SEL_VOP_LIT | (DP_SEL_VOP_LIT << 16); 587 else 588 val = DP_SEL_VOP_LIT << 16; 589 590 ret = cdn_dp_grf_write(dp, GRF_SOC_CON9, val); 591 if (ret) 592 return; 593 594 mutex_lock(&dp->lock); 595 596 ret = cdn_dp_enable(dp); 597 if (ret) { 598 DRM_DEV_ERROR(dp->dev, "Failed to enable bridge %d\n", 599 ret); 600 goto out; 601 } 602 if (!cdn_dp_check_link_status(dp)) { 603 ret = cdn_dp_train_link(dp); 604 if (ret) { 605 DRM_DEV_ERROR(dp->dev, "Failed link train %d\n", ret); 606 goto out; 607 } 608 } 609 610 ret = cdn_dp_set_video_status(dp, CONTROL_VIDEO_IDLE); 611 if (ret) { 612 DRM_DEV_ERROR(dp->dev, "Failed to idle video %d\n", ret); 613 goto out; 614 } 615 616 ret = cdn_dp_config_video(dp); 617 if (ret) { 618 DRM_DEV_ERROR(dp->dev, "Failed to config video %d\n", ret); 619 goto out; 620 } 621 622 ret = cdn_dp_set_video_status(dp, CONTROL_VIDEO_VALID); 623 if (ret) { 624 DRM_DEV_ERROR(dp->dev, "Failed to valid video %d\n", ret); 625 goto out; 626 } 627 628 out: 629 mutex_unlock(&dp->lock); 630 } 631 632 static void cdn_dp_bridge_atomic_disable(struct drm_bridge *bridge, struct drm_atomic_commit *state) 633 { 634 struct cdn_dp_device *dp = bridge_to_dp(bridge); 635 int ret; 636 637 mutex_lock(&dp->lock); 638 639 if (dp->active) { 640 ret = cdn_dp_disable(dp); 641 if (ret) { 642 DRM_DEV_ERROR(dp->dev, "Failed to disable bridge %d\n", 643 ret); 644 } 645 } 646 mutex_unlock(&dp->lock); 647 648 /* 649 * In the following 2 cases, we need to run the event_work to re-enable 650 * the DP: 651 * 1. If there is not just one port device is connected, and remove one 652 * device from a port, the DP will be disabled here, at this case, 653 * run the event_work to re-open DP for the other port. 654 * 2. If re-training or re-config failed, the DP will be disabled here. 655 * run the event_work to re-connect it. 656 */ 657 if (!dp->connected && cdn_dp_connected_port(dp)) 658 schedule_work(&dp->event_work); 659 } 660 661 static int cdn_dp_encoder_atomic_check(struct drm_encoder *encoder, 662 struct drm_crtc_state *crtc_state, 663 struct drm_connector_state *conn_state) 664 { 665 struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state); 666 667 s->output_mode = ROCKCHIP_OUT_MODE_AAAA; 668 s->output_type = DRM_MODE_CONNECTOR_DisplayPort; 669 670 return 0; 671 } 672 673 static const struct drm_encoder_funcs cdn_dp_encoder_funcs = { 674 .destroy = drm_encoder_cleanup, 675 }; 676 677 static const struct drm_encoder_helper_funcs cdn_dp_encoder_helper_funcs = { 678 .atomic_check = cdn_dp_encoder_atomic_check, 679 }; 680 681 static int cdn_dp_parse_dt(struct cdn_dp_device *dp) 682 { 683 struct device *dev = dp->dev; 684 struct device_node *np = dev->of_node; 685 struct platform_device *pdev = to_platform_device(dev); 686 687 dp->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf"); 688 if (IS_ERR(dp->grf)) { 689 DRM_DEV_ERROR(dev, "cdn-dp needs rockchip,grf property\n"); 690 return PTR_ERR(dp->grf); 691 } 692 693 dp->regs = devm_platform_ioremap_resource(pdev, 0); 694 if (IS_ERR(dp->regs)) { 695 DRM_DEV_ERROR(dev, "ioremap reg failed\n"); 696 return PTR_ERR(dp->regs); 697 } 698 699 dp->core_clk = devm_clk_get(dev, "core-clk"); 700 if (IS_ERR(dp->core_clk)) { 701 DRM_DEV_ERROR(dev, "cannot get core_clk_dp\n"); 702 return PTR_ERR(dp->core_clk); 703 } 704 705 dp->pclk = devm_clk_get(dev, "pclk"); 706 if (IS_ERR(dp->pclk)) { 707 DRM_DEV_ERROR(dev, "cannot get pclk\n"); 708 return PTR_ERR(dp->pclk); 709 } 710 711 dp->spdif_clk = devm_clk_get(dev, "spdif"); 712 if (IS_ERR(dp->spdif_clk)) { 713 DRM_DEV_ERROR(dev, "cannot get spdif_clk\n"); 714 return PTR_ERR(dp->spdif_clk); 715 } 716 717 dp->grf_clk = devm_clk_get(dev, "grf"); 718 if (IS_ERR(dp->grf_clk)) { 719 DRM_DEV_ERROR(dev, "cannot get grf clk\n"); 720 return PTR_ERR(dp->grf_clk); 721 } 722 723 dp->spdif_rst = devm_reset_control_get(dev, "spdif"); 724 if (IS_ERR(dp->spdif_rst)) { 725 DRM_DEV_ERROR(dev, "no spdif reset control found\n"); 726 return PTR_ERR(dp->spdif_rst); 727 } 728 729 dp->dptx_rst = devm_reset_control_get(dev, "dptx"); 730 if (IS_ERR(dp->dptx_rst)) { 731 DRM_DEV_ERROR(dev, "no uphy reset control found\n"); 732 return PTR_ERR(dp->dptx_rst); 733 } 734 735 dp->core_rst = devm_reset_control_get(dev, "core"); 736 if (IS_ERR(dp->core_rst)) { 737 DRM_DEV_ERROR(dev, "no core reset control found\n"); 738 return PTR_ERR(dp->core_rst); 739 } 740 741 dp->apb_rst = devm_reset_control_get(dev, "apb"); 742 if (IS_ERR(dp->apb_rst)) { 743 DRM_DEV_ERROR(dev, "no apb reset control found\n"); 744 return PTR_ERR(dp->apb_rst); 745 } 746 747 return 0; 748 } 749 750 static int cdn_dp_audio_prepare(struct drm_bridge *bridge, 751 struct drm_connector *connector, 752 struct hdmi_codec_daifmt *daifmt, 753 struct hdmi_codec_params *params) 754 { 755 struct cdn_dp_device *dp = bridge_to_dp(bridge); 756 struct audio_info audio = { 757 .sample_width = params->sample_width, 758 .sample_rate = params->sample_rate, 759 .channels = params->channels, 760 }; 761 int ret; 762 763 mutex_lock(&dp->lock); 764 if (!dp->active) { 765 ret = -ENODEV; 766 goto out; 767 } 768 769 switch (daifmt->fmt) { 770 case HDMI_I2S: 771 audio.format = AFMT_I2S; 772 break; 773 case HDMI_SPDIF: 774 audio.format = AFMT_SPDIF; 775 break; 776 default: 777 drm_err(bridge->dev, "Invalid format %d\n", daifmt->fmt); 778 ret = -EINVAL; 779 goto out; 780 } 781 782 ret = cdn_dp_audio_config(dp, &audio); 783 if (!ret) 784 dp->audio_info = audio; 785 786 out: 787 mutex_unlock(&dp->lock); 788 return ret; 789 } 790 791 static void cdn_dp_audio_shutdown(struct drm_bridge *bridge, 792 struct drm_connector *connector) 793 { 794 struct cdn_dp_device *dp = bridge_to_dp(bridge); 795 int ret; 796 797 mutex_lock(&dp->lock); 798 if (!dp->active) 799 goto out; 800 801 ret = cdn_dp_audio_stop(dp, &dp->audio_info); 802 if (!ret) 803 dp->audio_info.format = AFMT_UNUSED; 804 out: 805 mutex_unlock(&dp->lock); 806 } 807 808 static int cdn_dp_audio_mute_stream(struct drm_bridge *bridge, 809 struct drm_connector *connector, 810 bool enable, int direction) 811 { 812 struct cdn_dp_device *dp = bridge_to_dp(bridge); 813 int ret; 814 815 mutex_lock(&dp->lock); 816 if (!dp->active) { 817 ret = -ENODEV; 818 goto out; 819 } 820 821 ret = cdn_dp_audio_mute(dp, enable); 822 823 out: 824 mutex_unlock(&dp->lock); 825 return ret; 826 } 827 828 static const struct drm_bridge_funcs cdn_dp_bridge_funcs = { 829 .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state, 830 .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state, 831 .atomic_create_state = drm_atomic_helper_bridge_create_state, 832 .detect = cdn_dp_bridge_detect, 833 .edid_read = cdn_dp_bridge_edid_read, 834 .atomic_enable = cdn_dp_bridge_atomic_enable, 835 .atomic_disable = cdn_dp_bridge_atomic_disable, 836 .mode_valid = cdn_dp_bridge_mode_valid, 837 .mode_set = cdn_dp_bridge_mode_set, 838 839 .dp_audio_prepare = cdn_dp_audio_prepare, 840 .dp_audio_mute_stream = cdn_dp_audio_mute_stream, 841 .dp_audio_shutdown = cdn_dp_audio_shutdown, 842 }; 843 844 static int cdn_dp_request_firmware(struct cdn_dp_device *dp) 845 { 846 int ret; 847 unsigned long timeout = jiffies + msecs_to_jiffies(CDN_FW_TIMEOUT_MS); 848 unsigned long sleep = 1000; 849 850 WARN_ON(!mutex_is_locked(&dp->lock)); 851 852 if (dp->fw_loaded) 853 return 0; 854 855 /* Drop the lock before getting the firmware to avoid blocking boot */ 856 mutex_unlock(&dp->lock); 857 858 while (time_before(jiffies, timeout)) { 859 ret = request_firmware(&dp->fw, CDN_DP_FIRMWARE, dp->dev); 860 if (ret == -ENOENT) { 861 msleep(sleep); 862 sleep *= 2; 863 continue; 864 } else if (ret) { 865 DRM_DEV_ERROR(dp->dev, 866 "failed to request firmware: %d\n", ret); 867 goto out; 868 } 869 870 dp->fw_loaded = true; 871 ret = 0; 872 goto out; 873 } 874 875 DRM_DEV_ERROR(dp->dev, "Timed out trying to load firmware\n"); 876 ret = -ETIMEDOUT; 877 out: 878 mutex_lock(&dp->lock); 879 return ret; 880 } 881 882 static void cdn_dp_pd_event_work(struct work_struct *work) 883 { 884 struct cdn_dp_device *dp = container_of(work, struct cdn_dp_device, 885 event_work); 886 int ret; 887 888 mutex_lock(&dp->lock); 889 890 if (dp->suspended) 891 goto out; 892 893 ret = cdn_dp_request_firmware(dp); 894 if (ret) 895 goto out; 896 897 dp->connected = true; 898 899 /* Not connected, notify userspace to disable the block */ 900 if (!cdn_dp_connected_port(dp)) { 901 DRM_DEV_INFO(dp->dev, "Not connected; disabling cdn\n"); 902 dp->connected = false; 903 904 /* Connected but not enabled, enable the block */ 905 } else if (!dp->active) { 906 DRM_DEV_INFO(dp->dev, "Connected, not enabled; enabling cdn\n"); 907 ret = cdn_dp_enable(dp); 908 if (ret) { 909 DRM_DEV_ERROR(dp->dev, "Enabling dp failed: %d\n", ret); 910 dp->connected = false; 911 } 912 913 /* Enabled and connected to a dongle without a sink, notify userspace */ 914 } else if (!cdn_dp_check_sink_connection(dp)) { 915 DRM_DEV_INFO(dp->dev, "Connected without sink; assert hpd\n"); 916 dp->connected = false; 917 918 /* Enabled and connected with a sink, re-train if requested */ 919 } else if (!cdn_dp_check_link_status(dp)) { 920 unsigned int rate = dp->max_rate; 921 unsigned int lanes = dp->max_lanes; 922 struct drm_display_mode *mode = &dp->mode; 923 924 DRM_DEV_INFO(dp->dev, "Connected with sink; re-train link\n"); 925 ret = cdn_dp_train_link(dp); 926 if (ret) { 927 dp->connected = false; 928 DRM_DEV_ERROR(dp->dev, "Training link failed: %d\n", ret); 929 goto out; 930 } 931 932 /* If training result is changed, update the video config */ 933 if (mode->clock && 934 (rate != dp->max_rate || lanes != dp->max_lanes)) { 935 ret = cdn_dp_config_video(dp); 936 if (ret) { 937 dp->connected = false; 938 DRM_DEV_ERROR(dp->dev, "Failed to configure video: %d\n", ret); 939 } 940 } 941 } 942 943 out: 944 mutex_unlock(&dp->lock); 945 drm_bridge_hpd_notify(&dp->bridge, 946 dp->connected ? connector_status_connected 947 : connector_status_disconnected); 948 } 949 950 static int cdn_dp_pd_event(struct notifier_block *nb, 951 unsigned long event, void *priv) 952 { 953 struct cdn_dp_port *port = container_of(nb, struct cdn_dp_port, 954 event_nb); 955 struct cdn_dp_device *dp = port->dp; 956 957 /* 958 * It would be nice to be able to just do the work inline right here. 959 * However, we need to make a bunch of calls that might sleep in order 960 * to turn on the block/phy, so use a worker instead. 961 */ 962 schedule_work(&dp->event_work); 963 964 return NOTIFY_DONE; 965 } 966 967 static int cdn_dp_bind(struct device *dev, struct device *master, void *data) 968 { 969 struct cdn_dp_device *dp = dev_get_drvdata(dev); 970 struct drm_encoder *encoder; 971 struct drm_connector *connector; 972 struct cdn_dp_port *port; 973 struct drm_device *drm_dev = data; 974 int ret, i; 975 976 ret = cdn_dp_parse_dt(dp); 977 if (ret < 0) 978 return ret; 979 980 dp->drm_dev = drm_dev; 981 dp->connected = false; 982 dp->active = false; 983 dp->active_port = -1; 984 dp->fw_loaded = false; 985 986 INIT_WORK(&dp->event_work, cdn_dp_pd_event_work); 987 988 encoder = &dp->encoder.encoder; 989 990 encoder->possible_crtcs = drm_of_find_possible_crtcs(drm_dev, 991 dev->of_node); 992 DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs); 993 994 ret = drm_encoder_init(drm_dev, encoder, &cdn_dp_encoder_funcs, 995 DRM_MODE_ENCODER_TMDS, NULL); 996 if (ret) { 997 DRM_ERROR("failed to initialize encoder with drm\n"); 998 return ret; 999 } 1000 1001 drm_encoder_helper_add(encoder, &cdn_dp_encoder_helper_funcs); 1002 1003 dp->bridge.ops = 1004 DRM_BRIDGE_OP_DETECT | 1005 DRM_BRIDGE_OP_EDID | 1006 DRM_BRIDGE_OP_HPD | 1007 DRM_BRIDGE_OP_DP_AUDIO; 1008 dp->bridge.of_node = dp->dev->of_node; 1009 dp->bridge.type = DRM_MODE_CONNECTOR_DisplayPort; 1010 dp->bridge.hdmi_audio_dev = dp->dev; 1011 dp->bridge.hdmi_audio_max_i2s_playback_channels = 8; 1012 dp->bridge.hdmi_audio_spdif_playback = 1; 1013 dp->bridge.hdmi_audio_dai_port = -1; 1014 1015 ret = devm_drm_bridge_add(dev, &dp->bridge); 1016 if (ret) 1017 return ret; 1018 1019 ret = drm_bridge_attach(encoder, &dp->bridge, NULL, DRM_BRIDGE_ATTACH_NO_CONNECTOR); 1020 if (ret) 1021 return ret; 1022 1023 connector = drm_bridge_connector_init(drm_dev, encoder); 1024 if (IS_ERR(connector)) { 1025 ret = PTR_ERR(connector); 1026 dev_err(dp->dev, "failed to init bridge connector: %d\n", ret); 1027 return ret; 1028 } 1029 1030 for (i = 0; i < dp->ports; i++) { 1031 port = dp->port[i]; 1032 1033 port->event_nb.notifier_call = cdn_dp_pd_event; 1034 ret = devm_extcon_register_notifier(dp->dev, port->extcon, 1035 EXTCON_DISP_DP, 1036 &port->event_nb); 1037 if (ret) { 1038 DRM_DEV_ERROR(dev, 1039 "register EXTCON_DISP_DP notifier err\n"); 1040 return ret; 1041 } 1042 } 1043 1044 pm_runtime_enable(dev); 1045 1046 schedule_work(&dp->event_work); 1047 1048 return 0; 1049 } 1050 1051 static void cdn_dp_unbind(struct device *dev, struct device *master, void *data) 1052 { 1053 struct cdn_dp_device *dp = dev_get_drvdata(dev); 1054 struct drm_encoder *encoder = &dp->encoder.encoder; 1055 1056 cancel_work_sync(&dp->event_work); 1057 encoder->funcs->destroy(encoder); 1058 1059 pm_runtime_disable(dev); 1060 if (dp->fw_loaded) 1061 release_firmware(dp->fw); 1062 } 1063 1064 static const struct component_ops cdn_dp_component_ops = { 1065 .bind = cdn_dp_bind, 1066 .unbind = cdn_dp_unbind, 1067 }; 1068 1069 static int cdn_dp_suspend(struct device *dev) 1070 { 1071 struct cdn_dp_device *dp = dev_get_drvdata(dev); 1072 int ret = 0; 1073 1074 mutex_lock(&dp->lock); 1075 if (dp->active) 1076 ret = cdn_dp_disable(dp); 1077 dp->suspended = true; 1078 mutex_unlock(&dp->lock); 1079 1080 return ret; 1081 } 1082 1083 static __maybe_unused int cdn_dp_resume(struct device *dev) 1084 { 1085 struct cdn_dp_device *dp = dev_get_drvdata(dev); 1086 1087 mutex_lock(&dp->lock); 1088 dp->suspended = false; 1089 if (dp->fw_loaded) 1090 schedule_work(&dp->event_work); 1091 mutex_unlock(&dp->lock); 1092 1093 return 0; 1094 } 1095 1096 static int cdn_dp_probe(struct platform_device *pdev) 1097 { 1098 struct device *dev = &pdev->dev; 1099 const struct of_device_id *match; 1100 struct cdn_dp_data *dp_data; 1101 struct cdn_dp_port *port; 1102 struct cdn_dp_device *dp; 1103 struct extcon_dev *extcon; 1104 struct phy *phy; 1105 int ret; 1106 int i; 1107 1108 dp = devm_drm_bridge_alloc(dev, struct cdn_dp_device, bridge, 1109 &cdn_dp_bridge_funcs); 1110 if (IS_ERR(dp)) 1111 return PTR_ERR(dp); 1112 dp->dev = dev; 1113 1114 match = of_match_node(cdn_dp_dt_ids, pdev->dev.of_node); 1115 dp_data = (struct cdn_dp_data *)match->data; 1116 1117 for (i = 0; i < dp_data->max_phy; i++) { 1118 extcon = extcon_get_edev_by_phandle(dev, i); 1119 phy = devm_of_phy_get_by_index(dev, dev->of_node, i); 1120 1121 if (PTR_ERR(extcon) == -EPROBE_DEFER || 1122 PTR_ERR(phy) == -EPROBE_DEFER) 1123 return -EPROBE_DEFER; 1124 1125 if (IS_ERR(extcon) || IS_ERR(phy)) 1126 continue; 1127 1128 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL); 1129 if (!port) 1130 return -ENOMEM; 1131 1132 port->extcon = extcon; 1133 port->phy = phy; 1134 port->dp = dp; 1135 port->id = i; 1136 dp->port[dp->ports++] = port; 1137 } 1138 1139 if (!dp->ports) { 1140 DRM_DEV_ERROR(dev, "missing extcon or phy\n"); 1141 return -EINVAL; 1142 } 1143 1144 mutex_init(&dp->lock); 1145 dev_set_drvdata(dev, dp); 1146 1147 ret = component_add(dev, &cdn_dp_component_ops); 1148 if (ret) 1149 return ret; 1150 1151 return 0; 1152 } 1153 1154 static void cdn_dp_remove(struct platform_device *pdev) 1155 { 1156 struct cdn_dp_device *dp = platform_get_drvdata(pdev); 1157 1158 platform_device_unregister(dp->audio_pdev); 1159 cdn_dp_suspend(dp->dev); 1160 component_del(&pdev->dev, &cdn_dp_component_ops); 1161 } 1162 1163 static void cdn_dp_shutdown(struct platform_device *pdev) 1164 { 1165 struct cdn_dp_device *dp = platform_get_drvdata(pdev); 1166 1167 cdn_dp_suspend(dp->dev); 1168 } 1169 1170 static const struct dev_pm_ops cdn_dp_pm_ops = { 1171 SET_SYSTEM_SLEEP_PM_OPS(cdn_dp_suspend, 1172 cdn_dp_resume) 1173 }; 1174 1175 struct platform_driver cdn_dp_driver = { 1176 .probe = cdn_dp_probe, 1177 .remove = cdn_dp_remove, 1178 .shutdown = cdn_dp_shutdown, 1179 .driver = { 1180 .name = "cdn-dp", 1181 .of_match_table = cdn_dp_dt_ids, 1182 .pm = &cdn_dp_pm_ops, 1183 }, 1184 }; 1185