1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2015 MediaTek Inc. 4 * Author: YT SHEN <yt.shen@mediatek.com> 5 */ 6 7 #include <linux/component.h> 8 #include <linux/module.h> 9 #include <linux/of.h> 10 #include <linux/of_platform.h> 11 #include <linux/platform_device.h> 12 #include <linux/pm_runtime.h> 13 #include <linux/dma-mapping.h> 14 15 #include <drm/drm_atomic.h> 16 #include <drm/drm_atomic_helper.h> 17 #include <drm/drm_client_setup.h> 18 #include <drm/drm_drv.h> 19 #include <drm/drm_fbdev_dma.h> 20 #include <drm/drm_fourcc.h> 21 #include <drm/drm_gem.h> 22 #include <drm/drm_gem_framebuffer_helper.h> 23 #include <drm/drm_ioctl.h> 24 #include <drm/drm_of.h> 25 #include <drm/drm_probe_helper.h> 26 #include <drm/drm_vblank.h> 27 28 #include "mtk_crtc.h" 29 #include "mtk_ddp_comp.h" 30 #include "mtk_drm_drv.h" 31 #include "mtk_gem.h" 32 33 #define DRIVER_NAME "mediatek" 34 #define DRIVER_DESC "Mediatek SoC DRM" 35 #define DRIVER_DATE "20150513" 36 #define DRIVER_MAJOR 1 37 #define DRIVER_MINOR 0 38 39 static const struct drm_mode_config_helper_funcs mtk_drm_mode_config_helpers = { 40 .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm, 41 }; 42 43 static struct drm_framebuffer * 44 mtk_drm_mode_fb_create(struct drm_device *dev, 45 struct drm_file *file, 46 const struct drm_mode_fb_cmd2 *cmd) 47 { 48 const struct drm_format_info *info = drm_get_format_info(dev, cmd); 49 50 if (info->num_planes != 1) 51 return ERR_PTR(-EINVAL); 52 53 return drm_gem_fb_create(dev, file, cmd); 54 } 55 56 static const struct drm_mode_config_funcs mtk_drm_mode_config_funcs = { 57 .fb_create = mtk_drm_mode_fb_create, 58 .atomic_check = drm_atomic_helper_check, 59 .atomic_commit = drm_atomic_helper_commit, 60 }; 61 62 static const unsigned int mt2701_mtk_ddp_main[] = { 63 DDP_COMPONENT_OVL0, 64 DDP_COMPONENT_RDMA0, 65 DDP_COMPONENT_COLOR0, 66 DDP_COMPONENT_BLS, 67 DDP_COMPONENT_DSI0, 68 }; 69 70 static const unsigned int mt2701_mtk_ddp_ext[] = { 71 DDP_COMPONENT_RDMA1, 72 DDP_COMPONENT_DPI0, 73 }; 74 75 static const unsigned int mt7623_mtk_ddp_main[] = { 76 DDP_COMPONENT_OVL0, 77 DDP_COMPONENT_RDMA0, 78 DDP_COMPONENT_COLOR0, 79 DDP_COMPONENT_BLS, 80 DDP_COMPONENT_DPI0, 81 }; 82 83 static const unsigned int mt7623_mtk_ddp_ext[] = { 84 DDP_COMPONENT_RDMA1, 85 DDP_COMPONENT_DSI0, 86 }; 87 88 static const unsigned int mt2712_mtk_ddp_main[] = { 89 DDP_COMPONENT_OVL0, 90 DDP_COMPONENT_COLOR0, 91 DDP_COMPONENT_AAL0, 92 DDP_COMPONENT_OD0, 93 DDP_COMPONENT_RDMA0, 94 DDP_COMPONENT_DPI0, 95 DDP_COMPONENT_PWM0, 96 }; 97 98 static const unsigned int mt2712_mtk_ddp_ext[] = { 99 DDP_COMPONENT_OVL1, 100 DDP_COMPONENT_COLOR1, 101 DDP_COMPONENT_AAL1, 102 DDP_COMPONENT_OD1, 103 DDP_COMPONENT_RDMA1, 104 DDP_COMPONENT_DPI1, 105 DDP_COMPONENT_PWM1, 106 }; 107 108 static const unsigned int mt2712_mtk_ddp_third[] = { 109 DDP_COMPONENT_RDMA2, 110 DDP_COMPONENT_DSI3, 111 DDP_COMPONENT_PWM2, 112 }; 113 114 static unsigned int mt8167_mtk_ddp_main[] = { 115 DDP_COMPONENT_OVL0, 116 DDP_COMPONENT_COLOR0, 117 DDP_COMPONENT_CCORR, 118 DDP_COMPONENT_AAL0, 119 DDP_COMPONENT_GAMMA, 120 DDP_COMPONENT_DITHER0, 121 DDP_COMPONENT_RDMA0, 122 DDP_COMPONENT_DSI0, 123 }; 124 125 static const unsigned int mt8173_mtk_ddp_main[] = { 126 DDP_COMPONENT_OVL0, 127 DDP_COMPONENT_COLOR0, 128 DDP_COMPONENT_AAL0, 129 DDP_COMPONENT_OD0, 130 DDP_COMPONENT_RDMA0, 131 DDP_COMPONENT_UFOE, 132 DDP_COMPONENT_DSI0, 133 DDP_COMPONENT_PWM0, 134 }; 135 136 static const unsigned int mt8173_mtk_ddp_ext[] = { 137 DDP_COMPONENT_OVL1, 138 DDP_COMPONENT_COLOR1, 139 DDP_COMPONENT_GAMMA, 140 DDP_COMPONENT_RDMA1, 141 DDP_COMPONENT_DPI0, 142 }; 143 144 static const unsigned int mt8183_mtk_ddp_main[] = { 145 DDP_COMPONENT_OVL0, 146 DDP_COMPONENT_OVL_2L0, 147 DDP_COMPONENT_RDMA0, 148 DDP_COMPONENT_COLOR0, 149 DDP_COMPONENT_CCORR, 150 DDP_COMPONENT_AAL0, 151 DDP_COMPONENT_GAMMA, 152 DDP_COMPONENT_DITHER0, 153 DDP_COMPONENT_DSI0, 154 }; 155 156 static const unsigned int mt8183_mtk_ddp_ext[] = { 157 DDP_COMPONENT_OVL_2L1, 158 DDP_COMPONENT_RDMA1, 159 DDP_COMPONENT_DPI0, 160 }; 161 162 static const unsigned int mt8186_mtk_ddp_main[] = { 163 DDP_COMPONENT_OVL0, 164 DDP_COMPONENT_RDMA0, 165 DDP_COMPONENT_COLOR0, 166 DDP_COMPONENT_CCORR, 167 DDP_COMPONENT_AAL0, 168 DDP_COMPONENT_GAMMA, 169 DDP_COMPONENT_POSTMASK0, 170 DDP_COMPONENT_DITHER0, 171 DDP_COMPONENT_DSI0, 172 }; 173 174 static const unsigned int mt8186_mtk_ddp_ext[] = { 175 DDP_COMPONENT_OVL_2L0, 176 DDP_COMPONENT_RDMA1, 177 DDP_COMPONENT_DPI0, 178 }; 179 180 static const unsigned int mt8188_mtk_ddp_main[] = { 181 DDP_COMPONENT_OVL0, 182 DDP_COMPONENT_RDMA0, 183 DDP_COMPONENT_COLOR0, 184 DDP_COMPONENT_CCORR, 185 DDP_COMPONENT_AAL0, 186 DDP_COMPONENT_GAMMA, 187 DDP_COMPONENT_POSTMASK0, 188 DDP_COMPONENT_DITHER0, 189 }; 190 191 static const struct mtk_drm_route mt8188_mtk_ddp_main_routes[] = { 192 {0, DDP_COMPONENT_DP_INTF0}, 193 {0, DDP_COMPONENT_DSI0}, 194 }; 195 196 static const unsigned int mt8192_mtk_ddp_main[] = { 197 DDP_COMPONENT_OVL0, 198 DDP_COMPONENT_OVL_2L0, 199 DDP_COMPONENT_RDMA0, 200 DDP_COMPONENT_COLOR0, 201 DDP_COMPONENT_CCORR, 202 DDP_COMPONENT_AAL0, 203 DDP_COMPONENT_GAMMA, 204 DDP_COMPONENT_POSTMASK0, 205 DDP_COMPONENT_DITHER0, 206 DDP_COMPONENT_DSI0, 207 }; 208 209 static const unsigned int mt8192_mtk_ddp_ext[] = { 210 DDP_COMPONENT_OVL_2L2, 211 DDP_COMPONENT_RDMA4, 212 DDP_COMPONENT_DPI0, 213 }; 214 215 static const unsigned int mt8195_mtk_ddp_main[] = { 216 DDP_COMPONENT_OVL0, 217 DDP_COMPONENT_RDMA0, 218 DDP_COMPONENT_COLOR0, 219 DDP_COMPONENT_CCORR, 220 DDP_COMPONENT_AAL0, 221 DDP_COMPONENT_GAMMA, 222 DDP_COMPONENT_DITHER0, 223 DDP_COMPONENT_DSC0, 224 DDP_COMPONENT_MERGE0, 225 DDP_COMPONENT_DP_INTF0, 226 }; 227 228 static const unsigned int mt8195_mtk_ddp_ext[] = { 229 DDP_COMPONENT_DRM_OVL_ADAPTOR, 230 DDP_COMPONENT_MERGE5, 231 DDP_COMPONENT_DP_INTF1, 232 }; 233 234 static const struct mtk_mmsys_driver_data mt2701_mmsys_driver_data = { 235 .main_path = mt2701_mtk_ddp_main, 236 .main_len = ARRAY_SIZE(mt2701_mtk_ddp_main), 237 .ext_path = mt2701_mtk_ddp_ext, 238 .ext_len = ARRAY_SIZE(mt2701_mtk_ddp_ext), 239 .shadow_register = true, 240 .mmsys_dev_num = 1, 241 }; 242 243 static const struct mtk_mmsys_driver_data mt7623_mmsys_driver_data = { 244 .main_path = mt7623_mtk_ddp_main, 245 .main_len = ARRAY_SIZE(mt7623_mtk_ddp_main), 246 .ext_path = mt7623_mtk_ddp_ext, 247 .ext_len = ARRAY_SIZE(mt7623_mtk_ddp_ext), 248 .shadow_register = true, 249 .mmsys_dev_num = 1, 250 }; 251 252 static const struct mtk_mmsys_driver_data mt2712_mmsys_driver_data = { 253 .main_path = mt2712_mtk_ddp_main, 254 .main_len = ARRAY_SIZE(mt2712_mtk_ddp_main), 255 .ext_path = mt2712_mtk_ddp_ext, 256 .ext_len = ARRAY_SIZE(mt2712_mtk_ddp_ext), 257 .third_path = mt2712_mtk_ddp_third, 258 .third_len = ARRAY_SIZE(mt2712_mtk_ddp_third), 259 .mmsys_dev_num = 1, 260 }; 261 262 static const struct mtk_mmsys_driver_data mt8167_mmsys_driver_data = { 263 .main_path = mt8167_mtk_ddp_main, 264 .main_len = ARRAY_SIZE(mt8167_mtk_ddp_main), 265 .mmsys_dev_num = 1, 266 }; 267 268 static const struct mtk_mmsys_driver_data mt8173_mmsys_driver_data = { 269 .main_path = mt8173_mtk_ddp_main, 270 .main_len = ARRAY_SIZE(mt8173_mtk_ddp_main), 271 .ext_path = mt8173_mtk_ddp_ext, 272 .ext_len = ARRAY_SIZE(mt8173_mtk_ddp_ext), 273 .mmsys_dev_num = 1, 274 }; 275 276 static const struct mtk_mmsys_driver_data mt8183_mmsys_driver_data = { 277 .main_path = mt8183_mtk_ddp_main, 278 .main_len = ARRAY_SIZE(mt8183_mtk_ddp_main), 279 .ext_path = mt8183_mtk_ddp_ext, 280 .ext_len = ARRAY_SIZE(mt8183_mtk_ddp_ext), 281 .mmsys_dev_num = 1, 282 }; 283 284 static const struct mtk_mmsys_driver_data mt8186_mmsys_driver_data = { 285 .main_path = mt8186_mtk_ddp_main, 286 .main_len = ARRAY_SIZE(mt8186_mtk_ddp_main), 287 .ext_path = mt8186_mtk_ddp_ext, 288 .ext_len = ARRAY_SIZE(mt8186_mtk_ddp_ext), 289 .mmsys_dev_num = 1, 290 }; 291 292 static const struct mtk_mmsys_driver_data mt8188_vdosys0_driver_data = { 293 .main_path = mt8188_mtk_ddp_main, 294 .main_len = ARRAY_SIZE(mt8188_mtk_ddp_main), 295 .conn_routes = mt8188_mtk_ddp_main_routes, 296 .num_conn_routes = ARRAY_SIZE(mt8188_mtk_ddp_main_routes), 297 .mmsys_dev_num = 2, 298 .max_width = 8191, 299 .min_width = 1, 300 .min_height = 1, 301 }; 302 303 static const struct mtk_mmsys_driver_data mt8192_mmsys_driver_data = { 304 .main_path = mt8192_mtk_ddp_main, 305 .main_len = ARRAY_SIZE(mt8192_mtk_ddp_main), 306 .ext_path = mt8192_mtk_ddp_ext, 307 .ext_len = ARRAY_SIZE(mt8192_mtk_ddp_ext), 308 .mmsys_dev_num = 1, 309 }; 310 311 static const struct mtk_mmsys_driver_data mt8195_vdosys0_driver_data = { 312 .main_path = mt8195_mtk_ddp_main, 313 .main_len = ARRAY_SIZE(mt8195_mtk_ddp_main), 314 .mmsys_dev_num = 2, 315 .max_width = 8191, 316 .min_width = 1, 317 .min_height = 1, 318 }; 319 320 static const struct mtk_mmsys_driver_data mt8195_vdosys1_driver_data = { 321 .ext_path = mt8195_mtk_ddp_ext, 322 .ext_len = ARRAY_SIZE(mt8195_mtk_ddp_ext), 323 .mmsys_id = 1, 324 .mmsys_dev_num = 2, 325 .max_width = 8191, 326 .min_width = 2, /* 2-pixel align when ethdr is bypassed */ 327 .min_height = 1, 328 }; 329 330 static const struct of_device_id mtk_drm_of_ids[] = { 331 { .compatible = "mediatek,mt2701-mmsys", 332 .data = &mt2701_mmsys_driver_data}, 333 { .compatible = "mediatek,mt7623-mmsys", 334 .data = &mt7623_mmsys_driver_data}, 335 { .compatible = "mediatek,mt2712-mmsys", 336 .data = &mt2712_mmsys_driver_data}, 337 { .compatible = "mediatek,mt8167-mmsys", 338 .data = &mt8167_mmsys_driver_data}, 339 { .compatible = "mediatek,mt8173-mmsys", 340 .data = &mt8173_mmsys_driver_data}, 341 { .compatible = "mediatek,mt8183-mmsys", 342 .data = &mt8183_mmsys_driver_data}, 343 { .compatible = "mediatek,mt8186-mmsys", 344 .data = &mt8186_mmsys_driver_data}, 345 { .compatible = "mediatek,mt8188-vdosys0", 346 .data = &mt8188_vdosys0_driver_data}, 347 { .compatible = "mediatek,mt8188-vdosys1", 348 .data = &mt8195_vdosys1_driver_data}, 349 { .compatible = "mediatek,mt8192-mmsys", 350 .data = &mt8192_mmsys_driver_data}, 351 { .compatible = "mediatek,mt8195-mmsys", 352 .data = &mt8195_vdosys0_driver_data}, 353 { .compatible = "mediatek,mt8195-vdosys0", 354 .data = &mt8195_vdosys0_driver_data}, 355 { .compatible = "mediatek,mt8195-vdosys1", 356 .data = &mt8195_vdosys1_driver_data}, 357 { } 358 }; 359 MODULE_DEVICE_TABLE(of, mtk_drm_of_ids); 360 361 static int mtk_drm_match(struct device *dev, void *data) 362 { 363 if (!strncmp(dev_name(dev), "mediatek-drm", sizeof("mediatek-drm") - 1)) 364 return true; 365 return false; 366 } 367 368 static bool mtk_drm_get_all_drm_priv(struct device *dev) 369 { 370 struct mtk_drm_private *drm_priv = dev_get_drvdata(dev); 371 struct mtk_drm_private *all_drm_priv[MAX_CRTC]; 372 struct mtk_drm_private *temp_drm_priv; 373 struct device_node *phandle = dev->parent->of_node; 374 const struct of_device_id *of_id; 375 struct device_node *node; 376 struct device *drm_dev; 377 unsigned int cnt = 0; 378 int i, j; 379 380 for_each_child_of_node(phandle->parent, node) { 381 struct platform_device *pdev; 382 383 of_id = of_match_node(mtk_drm_of_ids, node); 384 if (!of_id) 385 continue; 386 387 pdev = of_find_device_by_node(node); 388 if (!pdev) 389 continue; 390 391 drm_dev = device_find_child(&pdev->dev, NULL, mtk_drm_match); 392 if (!drm_dev) 393 continue; 394 395 temp_drm_priv = dev_get_drvdata(drm_dev); 396 if (!temp_drm_priv) 397 continue; 398 399 if (temp_drm_priv->data->main_len) 400 all_drm_priv[CRTC_MAIN] = temp_drm_priv; 401 else if (temp_drm_priv->data->ext_len) 402 all_drm_priv[CRTC_EXT] = temp_drm_priv; 403 else if (temp_drm_priv->data->third_len) 404 all_drm_priv[CRTC_THIRD] = temp_drm_priv; 405 406 if (temp_drm_priv->mtk_drm_bound) 407 cnt++; 408 409 if (cnt == MAX_CRTC) 410 break; 411 } 412 413 if (drm_priv->data->mmsys_dev_num == cnt) { 414 for (i = 0; i < cnt; i++) 415 for (j = 0; j < cnt; j++) 416 all_drm_priv[j]->all_drm_private[i] = all_drm_priv[i]; 417 418 return true; 419 } 420 421 return false; 422 } 423 424 static bool mtk_drm_find_mmsys_comp(struct mtk_drm_private *private, int comp_id) 425 { 426 const struct mtk_mmsys_driver_data *drv_data = private->data; 427 int i; 428 429 if (drv_data->main_path) 430 for (i = 0; i < drv_data->main_len; i++) 431 if (drv_data->main_path[i] == comp_id) 432 return true; 433 434 if (drv_data->ext_path) 435 for (i = 0; i < drv_data->ext_len; i++) 436 if (drv_data->ext_path[i] == comp_id) 437 return true; 438 439 if (drv_data->third_path) 440 for (i = 0; i < drv_data->third_len; i++) 441 if (drv_data->third_path[i] == comp_id) 442 return true; 443 444 if (drv_data->num_conn_routes) 445 for (i = 0; i < drv_data->num_conn_routes; i++) 446 if (drv_data->conn_routes[i].route_ddp == comp_id) 447 return true; 448 449 return false; 450 } 451 452 static int mtk_drm_kms_init(struct drm_device *drm) 453 { 454 struct mtk_drm_private *private = drm->dev_private; 455 struct mtk_drm_private *priv_n; 456 struct device *dma_dev = NULL; 457 struct drm_crtc *crtc; 458 int ret, i, j; 459 460 if (drm_firmware_drivers_only()) 461 return -ENODEV; 462 463 ret = drmm_mode_config_init(drm); 464 if (ret) 465 goto put_mutex_dev; 466 467 drm->mode_config.min_width = 64; 468 drm->mode_config.min_height = 64; 469 470 /* 471 * set max width and height as default value(4096x4096). 472 * this value would be used to check framebuffer size limitation 473 * at drm_mode_addfb(). 474 */ 475 drm->mode_config.max_width = 4096; 476 drm->mode_config.max_height = 4096; 477 drm->mode_config.funcs = &mtk_drm_mode_config_funcs; 478 drm->mode_config.helper_private = &mtk_drm_mode_config_helpers; 479 480 for (i = 0; i < private->data->mmsys_dev_num; i++) { 481 drm->dev_private = private->all_drm_private[i]; 482 ret = component_bind_all(private->all_drm_private[i]->dev, drm); 483 if (ret) 484 goto put_mutex_dev; 485 } 486 487 /* 488 * Ensure internal panels are at the top of the connector list before 489 * crtc creation. 490 */ 491 drm_helper_move_panel_connectors_to_head(drm); 492 493 /* 494 * 1. We currently support two fixed data streams, each optional, 495 * and each statically assigned to a crtc: 496 * OVL0 -> COLOR0 -> AAL -> OD -> RDMA0 -> UFOE -> DSI0 ... 497 * 2. For multi mmsys architecture, crtc path data are located in 498 * different drm private data structures. Loop through crtc index to 499 * create crtc from the main path and then ext_path and finally the 500 * third path. 501 */ 502 for (i = 0; i < MAX_CRTC; i++) { 503 for (j = 0; j < private->data->mmsys_dev_num; j++) { 504 priv_n = private->all_drm_private[j]; 505 506 if (priv_n->data->max_width) 507 drm->mode_config.max_width = priv_n->data->max_width; 508 509 if (priv_n->data->min_width) 510 drm->mode_config.min_width = priv_n->data->min_width; 511 512 if (priv_n->data->min_height) 513 drm->mode_config.min_height = priv_n->data->min_height; 514 515 if (i == CRTC_MAIN && priv_n->data->main_len) { 516 ret = mtk_crtc_create(drm, priv_n->data->main_path, 517 priv_n->data->main_len, j, 518 priv_n->data->conn_routes, 519 priv_n->data->num_conn_routes); 520 if (ret) 521 goto err_component_unbind; 522 523 continue; 524 } else if (i == CRTC_EXT && priv_n->data->ext_len) { 525 ret = mtk_crtc_create(drm, priv_n->data->ext_path, 526 priv_n->data->ext_len, j, NULL, 0); 527 if (ret) 528 goto err_component_unbind; 529 530 continue; 531 } else if (i == CRTC_THIRD && priv_n->data->third_len) { 532 ret = mtk_crtc_create(drm, priv_n->data->third_path, 533 priv_n->data->third_len, j, NULL, 0); 534 if (ret) 535 goto err_component_unbind; 536 537 continue; 538 } 539 } 540 } 541 542 /* IGT will check if the cursor size is configured */ 543 drm->mode_config.cursor_width = 512; 544 drm->mode_config.cursor_height = 512; 545 546 /* Use OVL device for all DMA memory allocations */ 547 crtc = drm_crtc_from_index(drm, 0); 548 if (crtc) 549 dma_dev = mtk_crtc_dma_dev_get(crtc); 550 if (!dma_dev) { 551 ret = -ENODEV; 552 dev_err(drm->dev, "Need at least one OVL device\n"); 553 goto err_component_unbind; 554 } 555 556 for (i = 0; i < private->data->mmsys_dev_num; i++) 557 private->all_drm_private[i]->dma_dev = dma_dev; 558 559 /* 560 * Configure the DMA segment size to make sure we get contiguous IOVA 561 * when importing PRIME buffers. 562 */ 563 ret = dma_set_max_seg_size(dma_dev, UINT_MAX); 564 if (ret) { 565 dev_err(dma_dev, "Failed to set DMA segment size\n"); 566 goto err_component_unbind; 567 } 568 569 ret = drm_vblank_init(drm, MAX_CRTC); 570 if (ret < 0) 571 goto err_component_unbind; 572 573 drm_kms_helper_poll_init(drm); 574 drm_mode_config_reset(drm); 575 576 return 0; 577 578 err_component_unbind: 579 for (i = 0; i < private->data->mmsys_dev_num; i++) 580 component_unbind_all(private->all_drm_private[i]->dev, drm); 581 put_mutex_dev: 582 for (i = 0; i < private->data->mmsys_dev_num; i++) 583 put_device(private->all_drm_private[i]->mutex_dev); 584 585 return ret; 586 } 587 588 static void mtk_drm_kms_deinit(struct drm_device *drm) 589 { 590 drm_kms_helper_poll_fini(drm); 591 drm_atomic_helper_shutdown(drm); 592 593 component_unbind_all(drm->dev, drm); 594 } 595 596 DEFINE_DRM_GEM_FOPS(mtk_drm_fops); 597 598 /* 599 * We need to override this because the device used to import the memory is 600 * not dev->dev, as drm_gem_prime_import() expects. 601 */ 602 static struct drm_gem_object *mtk_gem_prime_import(struct drm_device *dev, 603 struct dma_buf *dma_buf) 604 { 605 struct mtk_drm_private *private = dev->dev_private; 606 607 return drm_gem_prime_import_dev(dev, dma_buf, private->dma_dev); 608 } 609 610 static const struct drm_driver mtk_drm_driver = { 611 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC, 612 613 .dumb_create = mtk_gem_dumb_create, 614 DRM_FBDEV_DMA_DRIVER_OPS, 615 616 .gem_prime_import = mtk_gem_prime_import, 617 .gem_prime_import_sg_table = mtk_gem_prime_import_sg_table, 618 .fops = &mtk_drm_fops, 619 620 .name = DRIVER_NAME, 621 .desc = DRIVER_DESC, 622 .date = DRIVER_DATE, 623 .major = DRIVER_MAJOR, 624 .minor = DRIVER_MINOR, 625 }; 626 627 static int compare_dev(struct device *dev, void *data) 628 { 629 return dev == (struct device *)data; 630 } 631 632 static int mtk_drm_bind(struct device *dev) 633 { 634 struct mtk_drm_private *private = dev_get_drvdata(dev); 635 struct platform_device *pdev; 636 struct drm_device *drm; 637 int ret, i; 638 639 pdev = of_find_device_by_node(private->mutex_node); 640 if (!pdev) { 641 dev_err(dev, "Waiting for disp-mutex device %pOF\n", 642 private->mutex_node); 643 of_node_put(private->mutex_node); 644 return -EPROBE_DEFER; 645 } 646 647 private->mutex_dev = &pdev->dev; 648 private->mtk_drm_bound = true; 649 private->dev = dev; 650 651 if (!mtk_drm_get_all_drm_priv(dev)) 652 return 0; 653 654 drm = drm_dev_alloc(&mtk_drm_driver, dev); 655 if (IS_ERR(drm)) 656 return PTR_ERR(drm); 657 658 private->drm_master = true; 659 drm->dev_private = private; 660 for (i = 0; i < private->data->mmsys_dev_num; i++) 661 private->all_drm_private[i]->drm = drm; 662 663 ret = mtk_drm_kms_init(drm); 664 if (ret < 0) 665 goto err_free; 666 667 ret = drm_dev_register(drm, 0); 668 if (ret < 0) 669 goto err_deinit; 670 671 drm_client_setup(drm, NULL); 672 673 return 0; 674 675 err_deinit: 676 mtk_drm_kms_deinit(drm); 677 err_free: 678 private->drm = NULL; 679 drm_dev_put(drm); 680 return ret; 681 } 682 683 static void mtk_drm_unbind(struct device *dev) 684 { 685 struct mtk_drm_private *private = dev_get_drvdata(dev); 686 687 /* for multi mmsys dev, unregister drm dev in mmsys master */ 688 if (private->drm_master) { 689 drm_dev_unregister(private->drm); 690 mtk_drm_kms_deinit(private->drm); 691 drm_dev_put(private->drm); 692 } 693 private->mtk_drm_bound = false; 694 private->drm_master = false; 695 private->drm = NULL; 696 } 697 698 static const struct component_master_ops mtk_drm_ops = { 699 .bind = mtk_drm_bind, 700 .unbind = mtk_drm_unbind, 701 }; 702 703 static const struct of_device_id mtk_ddp_comp_dt_ids[] = { 704 { .compatible = "mediatek,mt8167-disp-aal", 705 .data = (void *)MTK_DISP_AAL}, 706 { .compatible = "mediatek,mt8173-disp-aal", 707 .data = (void *)MTK_DISP_AAL}, 708 { .compatible = "mediatek,mt8183-disp-aal", 709 .data = (void *)MTK_DISP_AAL}, 710 { .compatible = "mediatek,mt8192-disp-aal", 711 .data = (void *)MTK_DISP_AAL}, 712 { .compatible = "mediatek,mt8167-disp-ccorr", 713 .data = (void *)MTK_DISP_CCORR }, 714 { .compatible = "mediatek,mt8183-disp-ccorr", 715 .data = (void *)MTK_DISP_CCORR }, 716 { .compatible = "mediatek,mt8192-disp-ccorr", 717 .data = (void *)MTK_DISP_CCORR }, 718 { .compatible = "mediatek,mt2701-disp-color", 719 .data = (void *)MTK_DISP_COLOR }, 720 { .compatible = "mediatek,mt8167-disp-color", 721 .data = (void *)MTK_DISP_COLOR }, 722 { .compatible = "mediatek,mt8173-disp-color", 723 .data = (void *)MTK_DISP_COLOR }, 724 { .compatible = "mediatek,mt8167-disp-dither", 725 .data = (void *)MTK_DISP_DITHER }, 726 { .compatible = "mediatek,mt8183-disp-dither", 727 .data = (void *)MTK_DISP_DITHER }, 728 { .compatible = "mediatek,mt8195-disp-dsc", 729 .data = (void *)MTK_DISP_DSC }, 730 { .compatible = "mediatek,mt8167-disp-gamma", 731 .data = (void *)MTK_DISP_GAMMA, }, 732 { .compatible = "mediatek,mt8173-disp-gamma", 733 .data = (void *)MTK_DISP_GAMMA, }, 734 { .compatible = "mediatek,mt8183-disp-gamma", 735 .data = (void *)MTK_DISP_GAMMA, }, 736 { .compatible = "mediatek,mt8195-disp-gamma", 737 .data = (void *)MTK_DISP_GAMMA, }, 738 { .compatible = "mediatek,mt8195-disp-merge", 739 .data = (void *)MTK_DISP_MERGE }, 740 { .compatible = "mediatek,mt2701-disp-mutex", 741 .data = (void *)MTK_DISP_MUTEX }, 742 { .compatible = "mediatek,mt2712-disp-mutex", 743 .data = (void *)MTK_DISP_MUTEX }, 744 { .compatible = "mediatek,mt8167-disp-mutex", 745 .data = (void *)MTK_DISP_MUTEX }, 746 { .compatible = "mediatek,mt8173-disp-mutex", 747 .data = (void *)MTK_DISP_MUTEX }, 748 { .compatible = "mediatek,mt8183-disp-mutex", 749 .data = (void *)MTK_DISP_MUTEX }, 750 { .compatible = "mediatek,mt8186-disp-mutex", 751 .data = (void *)MTK_DISP_MUTEX }, 752 { .compatible = "mediatek,mt8188-disp-mutex", 753 .data = (void *)MTK_DISP_MUTEX }, 754 { .compatible = "mediatek,mt8192-disp-mutex", 755 .data = (void *)MTK_DISP_MUTEX }, 756 { .compatible = "mediatek,mt8195-disp-mutex", 757 .data = (void *)MTK_DISP_MUTEX }, 758 { .compatible = "mediatek,mt8173-disp-od", 759 .data = (void *)MTK_DISP_OD }, 760 { .compatible = "mediatek,mt2701-disp-ovl", 761 .data = (void *)MTK_DISP_OVL }, 762 { .compatible = "mediatek,mt8167-disp-ovl", 763 .data = (void *)MTK_DISP_OVL }, 764 { .compatible = "mediatek,mt8173-disp-ovl", 765 .data = (void *)MTK_DISP_OVL }, 766 { .compatible = "mediatek,mt8183-disp-ovl", 767 .data = (void *)MTK_DISP_OVL }, 768 { .compatible = "mediatek,mt8192-disp-ovl", 769 .data = (void *)MTK_DISP_OVL }, 770 { .compatible = "mediatek,mt8195-disp-ovl", 771 .data = (void *)MTK_DISP_OVL }, 772 { .compatible = "mediatek,mt8183-disp-ovl-2l", 773 .data = (void *)MTK_DISP_OVL_2L }, 774 { .compatible = "mediatek,mt8192-disp-ovl-2l", 775 .data = (void *)MTK_DISP_OVL_2L }, 776 { .compatible = "mediatek,mt8192-disp-postmask", 777 .data = (void *)MTK_DISP_POSTMASK }, 778 { .compatible = "mediatek,mt2701-disp-pwm", 779 .data = (void *)MTK_DISP_BLS }, 780 { .compatible = "mediatek,mt8167-disp-pwm", 781 .data = (void *)MTK_DISP_PWM }, 782 { .compatible = "mediatek,mt8173-disp-pwm", 783 .data = (void *)MTK_DISP_PWM }, 784 { .compatible = "mediatek,mt2701-disp-rdma", 785 .data = (void *)MTK_DISP_RDMA }, 786 { .compatible = "mediatek,mt8167-disp-rdma", 787 .data = (void *)MTK_DISP_RDMA }, 788 { .compatible = "mediatek,mt8173-disp-rdma", 789 .data = (void *)MTK_DISP_RDMA }, 790 { .compatible = "mediatek,mt8183-disp-rdma", 791 .data = (void *)MTK_DISP_RDMA }, 792 { .compatible = "mediatek,mt8195-disp-rdma", 793 .data = (void *)MTK_DISP_RDMA }, 794 { .compatible = "mediatek,mt8173-disp-ufoe", 795 .data = (void *)MTK_DISP_UFOE }, 796 { .compatible = "mediatek,mt8173-disp-wdma", 797 .data = (void *)MTK_DISP_WDMA }, 798 { .compatible = "mediatek,mt2701-dpi", 799 .data = (void *)MTK_DPI }, 800 { .compatible = "mediatek,mt8167-dsi", 801 .data = (void *)MTK_DSI }, 802 { .compatible = "mediatek,mt8173-dpi", 803 .data = (void *)MTK_DPI }, 804 { .compatible = "mediatek,mt8183-dpi", 805 .data = (void *)MTK_DPI }, 806 { .compatible = "mediatek,mt8186-dpi", 807 .data = (void *)MTK_DPI }, 808 { .compatible = "mediatek,mt8188-dp-intf", 809 .data = (void *)MTK_DP_INTF }, 810 { .compatible = "mediatek,mt8192-dpi", 811 .data = (void *)MTK_DPI }, 812 { .compatible = "mediatek,mt8195-dp-intf", 813 .data = (void *)MTK_DP_INTF }, 814 { .compatible = "mediatek,mt2701-dsi", 815 .data = (void *)MTK_DSI }, 816 { .compatible = "mediatek,mt8173-dsi", 817 .data = (void *)MTK_DSI }, 818 { .compatible = "mediatek,mt8183-dsi", 819 .data = (void *)MTK_DSI }, 820 { .compatible = "mediatek,mt8186-dsi", 821 .data = (void *)MTK_DSI }, 822 { .compatible = "mediatek,mt8188-dsi", 823 .data = (void *)MTK_DSI }, 824 { } 825 }; 826 827 static int mtk_drm_probe(struct platform_device *pdev) 828 { 829 struct device *dev = &pdev->dev; 830 struct device_node *phandle = dev->parent->of_node; 831 const struct of_device_id *of_id; 832 struct mtk_drm_private *private; 833 struct device_node *node; 834 struct component_match *match = NULL; 835 struct platform_device *ovl_adaptor; 836 int ret; 837 int i; 838 839 private = devm_kzalloc(dev, sizeof(*private), GFP_KERNEL); 840 if (!private) 841 return -ENOMEM; 842 843 private->mmsys_dev = dev->parent; 844 if (!private->mmsys_dev) { 845 dev_err(dev, "Failed to get MMSYS device\n"); 846 return -ENODEV; 847 } 848 849 of_id = of_match_node(mtk_drm_of_ids, phandle); 850 if (!of_id) 851 return -ENODEV; 852 853 private->data = of_id->data; 854 855 private->all_drm_private = devm_kmalloc_array(dev, private->data->mmsys_dev_num, 856 sizeof(*private->all_drm_private), 857 GFP_KERNEL); 858 if (!private->all_drm_private) 859 return -ENOMEM; 860 861 /* Bringup ovl_adaptor */ 862 if (mtk_drm_find_mmsys_comp(private, DDP_COMPONENT_DRM_OVL_ADAPTOR)) { 863 ovl_adaptor = platform_device_register_data(dev, "mediatek-disp-ovl-adaptor", 864 PLATFORM_DEVID_AUTO, 865 (void *)private->mmsys_dev, 866 sizeof(*private->mmsys_dev)); 867 private->ddp_comp[DDP_COMPONENT_DRM_OVL_ADAPTOR].dev = &ovl_adaptor->dev; 868 mtk_ddp_comp_init(NULL, &private->ddp_comp[DDP_COMPONENT_DRM_OVL_ADAPTOR], 869 DDP_COMPONENT_DRM_OVL_ADAPTOR); 870 component_match_add(dev, &match, compare_dev, &ovl_adaptor->dev); 871 } 872 873 /* Iterate over sibling DISP function blocks */ 874 for_each_child_of_node(phandle->parent, node) { 875 const struct of_device_id *of_id; 876 enum mtk_ddp_comp_type comp_type; 877 int comp_id; 878 879 of_id = of_match_node(mtk_ddp_comp_dt_ids, node); 880 if (!of_id) 881 continue; 882 883 if (!of_device_is_available(node)) { 884 dev_dbg(dev, "Skipping disabled component %pOF\n", 885 node); 886 continue; 887 } 888 889 comp_type = (enum mtk_ddp_comp_type)(uintptr_t)of_id->data; 890 891 if (comp_type == MTK_DISP_MUTEX) { 892 int id; 893 894 id = of_alias_get_id(node, "mutex"); 895 if (id < 0 || id == private->data->mmsys_id) { 896 private->mutex_node = of_node_get(node); 897 dev_dbg(dev, "get mutex for mmsys %d", private->data->mmsys_id); 898 } 899 continue; 900 } 901 902 comp_id = mtk_ddp_comp_get_id(node, comp_type); 903 if (comp_id < 0) { 904 dev_warn(dev, "Skipping unknown component %pOF\n", 905 node); 906 continue; 907 } 908 909 if (!mtk_drm_find_mmsys_comp(private, comp_id)) 910 continue; 911 912 private->comp_node[comp_id] = of_node_get(node); 913 914 /* 915 * Currently only the AAL, CCORR, COLOR, GAMMA, MERGE, OVL, RDMA, DSI, and DPI 916 * blocks have separate component platform drivers and initialize their own 917 * DDP component structure. The others are initialized here. 918 */ 919 if (comp_type == MTK_DISP_AAL || 920 comp_type == MTK_DISP_CCORR || 921 comp_type == MTK_DISP_COLOR || 922 comp_type == MTK_DISP_GAMMA || 923 comp_type == MTK_DISP_MERGE || 924 comp_type == MTK_DISP_OVL || 925 comp_type == MTK_DISP_OVL_2L || 926 comp_type == MTK_DISP_OVL_ADAPTOR || 927 comp_type == MTK_DISP_RDMA || 928 comp_type == MTK_DP_INTF || 929 comp_type == MTK_DPI || 930 comp_type == MTK_DSI) { 931 dev_info(dev, "Adding component match for %pOF\n", 932 node); 933 drm_of_component_match_add(dev, &match, component_compare_of, 934 node); 935 } 936 937 ret = mtk_ddp_comp_init(node, &private->ddp_comp[comp_id], comp_id); 938 if (ret) { 939 of_node_put(node); 940 goto err_node; 941 } 942 } 943 944 if (!private->mutex_node) { 945 dev_err(dev, "Failed to find disp-mutex node\n"); 946 ret = -ENODEV; 947 goto err_node; 948 } 949 950 pm_runtime_enable(dev); 951 952 platform_set_drvdata(pdev, private); 953 954 ret = component_master_add_with_match(dev, &mtk_drm_ops, match); 955 if (ret) 956 goto err_pm; 957 958 return 0; 959 960 err_pm: 961 pm_runtime_disable(dev); 962 err_node: 963 of_node_put(private->mutex_node); 964 for (i = 0; i < DDP_COMPONENT_DRM_ID_MAX; i++) 965 of_node_put(private->comp_node[i]); 966 return ret; 967 } 968 969 static void mtk_drm_remove(struct platform_device *pdev) 970 { 971 struct mtk_drm_private *private = platform_get_drvdata(pdev); 972 int i; 973 974 component_master_del(&pdev->dev, &mtk_drm_ops); 975 pm_runtime_disable(&pdev->dev); 976 of_node_put(private->mutex_node); 977 for (i = 0; i < DDP_COMPONENT_DRM_ID_MAX; i++) 978 of_node_put(private->comp_node[i]); 979 } 980 981 static void mtk_drm_shutdown(struct platform_device *pdev) 982 { 983 struct mtk_drm_private *private = platform_get_drvdata(pdev); 984 985 drm_atomic_helper_shutdown(private->drm); 986 } 987 988 static int mtk_drm_sys_prepare(struct device *dev) 989 { 990 struct mtk_drm_private *private = dev_get_drvdata(dev); 991 struct drm_device *drm = private->drm; 992 993 if (private->drm_master) 994 return drm_mode_config_helper_suspend(drm); 995 else 996 return 0; 997 } 998 999 static void mtk_drm_sys_complete(struct device *dev) 1000 { 1001 struct mtk_drm_private *private = dev_get_drvdata(dev); 1002 struct drm_device *drm = private->drm; 1003 int ret = 0; 1004 1005 if (private->drm_master) 1006 ret = drm_mode_config_helper_resume(drm); 1007 if (ret) 1008 dev_err(dev, "Failed to resume\n"); 1009 } 1010 1011 static const struct dev_pm_ops mtk_drm_pm_ops = { 1012 .prepare = mtk_drm_sys_prepare, 1013 .complete = mtk_drm_sys_complete, 1014 }; 1015 1016 static struct platform_driver mtk_drm_platform_driver = { 1017 .probe = mtk_drm_probe, 1018 .remove_new = mtk_drm_remove, 1019 .shutdown = mtk_drm_shutdown, 1020 .driver = { 1021 .name = "mediatek-drm", 1022 .pm = &mtk_drm_pm_ops, 1023 }, 1024 }; 1025 1026 static struct platform_driver * const mtk_drm_drivers[] = { 1027 &mtk_disp_aal_driver, 1028 &mtk_disp_ccorr_driver, 1029 &mtk_disp_color_driver, 1030 &mtk_disp_gamma_driver, 1031 &mtk_disp_merge_driver, 1032 &mtk_disp_ovl_adaptor_driver, 1033 &mtk_disp_ovl_driver, 1034 &mtk_disp_rdma_driver, 1035 &mtk_dpi_driver, 1036 &mtk_drm_platform_driver, 1037 &mtk_dsi_driver, 1038 &mtk_ethdr_driver, 1039 &mtk_mdp_rdma_driver, 1040 &mtk_padding_driver, 1041 }; 1042 1043 static int __init mtk_drm_init(void) 1044 { 1045 return platform_register_drivers(mtk_drm_drivers, 1046 ARRAY_SIZE(mtk_drm_drivers)); 1047 } 1048 1049 static void __exit mtk_drm_exit(void) 1050 { 1051 platform_unregister_drivers(mtk_drm_drivers, 1052 ARRAY_SIZE(mtk_drm_drivers)); 1053 } 1054 1055 module_init(mtk_drm_init); 1056 module_exit(mtk_drm_exit); 1057 1058 MODULE_AUTHOR("YT SHEN <yt.shen@mediatek.com>"); 1059 MODULE_DESCRIPTION("Mediatek SoC DRM driver"); 1060 MODULE_LICENSE("GPL v2"); 1061