1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2015 MediaTek Inc. 4 */ 5 6 #include <linux/clk.h> 7 #include <linux/pm_runtime.h> 8 #include <linux/soc/mediatek/mtk-cmdq.h> 9 #include <linux/soc/mediatek/mtk-mmsys.h> 10 11 #include <asm/barrier.h> 12 #include <soc/mediatek/smi.h> 13 14 #include <drm/drm_atomic_helper.h> 15 #include <drm/drm_plane_helper.h> 16 #include <drm/drm_probe_helper.h> 17 #include <drm/drm_vblank.h> 18 19 #include "mtk_drm_drv.h" 20 #include "mtk_drm_crtc.h" 21 #include "mtk_drm_ddp.h" 22 #include "mtk_drm_ddp_comp.h" 23 #include "mtk_drm_gem.h" 24 #include "mtk_drm_plane.h" 25 26 /** 27 * struct mtk_drm_crtc - MediaTek specific crtc structure. 28 * @base: crtc object. 29 * @enabled: records whether crtc_enable succeeded 30 * @planes: array of 4 drm_plane structures, one for each overlay plane 31 * @pending_planes: whether any plane has pending changes to be applied 32 * @mmsys_dev: pointer to the mmsys device for configuration registers 33 * @mutex: handle to one of the ten disp_mutex streams 34 * @ddp_comp_nr: number of components in ddp_comp 35 * @ddp_comp: array of pointers the mtk_ddp_comp structures used by this crtc 36 */ 37 struct mtk_drm_crtc { 38 struct drm_crtc base; 39 bool enabled; 40 41 bool pending_needs_vblank; 42 struct drm_pending_vblank_event *event; 43 44 struct drm_plane *planes; 45 unsigned int layer_nr; 46 bool pending_planes; 47 bool pending_async_planes; 48 49 #if IS_REACHABLE(CONFIG_MTK_CMDQ) 50 struct cmdq_client *cmdq_client; 51 u32 cmdq_event; 52 #endif 53 54 struct device *mmsys_dev; 55 struct mtk_disp_mutex *mutex; 56 unsigned int ddp_comp_nr; 57 struct mtk_ddp_comp **ddp_comp; 58 59 /* lock for display hardware access */ 60 struct mutex hw_lock; 61 }; 62 63 struct mtk_crtc_state { 64 struct drm_crtc_state base; 65 66 bool pending_config; 67 unsigned int pending_width; 68 unsigned int pending_height; 69 unsigned int pending_vrefresh; 70 }; 71 72 static inline struct mtk_drm_crtc *to_mtk_crtc(struct drm_crtc *c) 73 { 74 return container_of(c, struct mtk_drm_crtc, base); 75 } 76 77 static inline struct mtk_crtc_state *to_mtk_crtc_state(struct drm_crtc_state *s) 78 { 79 return container_of(s, struct mtk_crtc_state, base); 80 } 81 82 static void mtk_drm_crtc_finish_page_flip(struct mtk_drm_crtc *mtk_crtc) 83 { 84 struct drm_crtc *crtc = &mtk_crtc->base; 85 unsigned long flags; 86 87 spin_lock_irqsave(&crtc->dev->event_lock, flags); 88 drm_crtc_send_vblank_event(crtc, mtk_crtc->event); 89 drm_crtc_vblank_put(crtc); 90 mtk_crtc->event = NULL; 91 spin_unlock_irqrestore(&crtc->dev->event_lock, flags); 92 } 93 94 static void mtk_drm_finish_page_flip(struct mtk_drm_crtc *mtk_crtc) 95 { 96 drm_crtc_handle_vblank(&mtk_crtc->base); 97 if (mtk_crtc->pending_needs_vblank) { 98 mtk_drm_crtc_finish_page_flip(mtk_crtc); 99 mtk_crtc->pending_needs_vblank = false; 100 } 101 } 102 103 static void mtk_drm_crtc_destroy(struct drm_crtc *crtc) 104 { 105 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc); 106 107 mtk_disp_mutex_put(mtk_crtc->mutex); 108 109 drm_crtc_cleanup(crtc); 110 } 111 112 static void mtk_drm_crtc_reset(struct drm_crtc *crtc) 113 { 114 struct mtk_crtc_state *state; 115 116 if (crtc->state) { 117 __drm_atomic_helper_crtc_destroy_state(crtc->state); 118 119 state = to_mtk_crtc_state(crtc->state); 120 memset(state, 0, sizeof(*state)); 121 } else { 122 state = kzalloc(sizeof(*state), GFP_KERNEL); 123 if (!state) 124 return; 125 crtc->state = &state->base; 126 } 127 128 state->base.crtc = crtc; 129 } 130 131 static struct drm_crtc_state *mtk_drm_crtc_duplicate_state(struct drm_crtc *crtc) 132 { 133 struct mtk_crtc_state *state; 134 135 state = kzalloc(sizeof(*state), GFP_KERNEL); 136 if (!state) 137 return NULL; 138 139 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base); 140 141 WARN_ON(state->base.crtc != crtc); 142 state->base.crtc = crtc; 143 144 return &state->base; 145 } 146 147 static void mtk_drm_crtc_destroy_state(struct drm_crtc *crtc, 148 struct drm_crtc_state *state) 149 { 150 __drm_atomic_helper_crtc_destroy_state(state); 151 kfree(to_mtk_crtc_state(state)); 152 } 153 154 static bool mtk_drm_crtc_mode_fixup(struct drm_crtc *crtc, 155 const struct drm_display_mode *mode, 156 struct drm_display_mode *adjusted_mode) 157 { 158 /* Nothing to do here, but this callback is mandatory. */ 159 return true; 160 } 161 162 static void mtk_drm_crtc_mode_set_nofb(struct drm_crtc *crtc) 163 { 164 struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state); 165 166 state->pending_width = crtc->mode.hdisplay; 167 state->pending_height = crtc->mode.vdisplay; 168 state->pending_vrefresh = crtc->mode.vrefresh; 169 wmb(); /* Make sure the above parameters are set before update */ 170 state->pending_config = true; 171 } 172 173 static int mtk_drm_crtc_enable_vblank(struct drm_crtc *crtc) 174 { 175 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc); 176 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0]; 177 178 mtk_ddp_comp_enable_vblank(comp, &mtk_crtc->base); 179 180 return 0; 181 } 182 183 static void mtk_drm_crtc_disable_vblank(struct drm_crtc *crtc) 184 { 185 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc); 186 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0]; 187 188 mtk_ddp_comp_disable_vblank(comp); 189 } 190 191 static int mtk_crtc_ddp_clk_enable(struct mtk_drm_crtc *mtk_crtc) 192 { 193 int ret; 194 int i; 195 196 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) { 197 ret = clk_prepare_enable(mtk_crtc->ddp_comp[i]->clk); 198 if (ret) { 199 DRM_ERROR("Failed to enable clock %d: %d\n", i, ret); 200 goto err; 201 } 202 } 203 204 return 0; 205 err: 206 while (--i >= 0) 207 clk_disable_unprepare(mtk_crtc->ddp_comp[i]->clk); 208 return ret; 209 } 210 211 static void mtk_crtc_ddp_clk_disable(struct mtk_drm_crtc *mtk_crtc) 212 { 213 int i; 214 215 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) 216 clk_disable_unprepare(mtk_crtc->ddp_comp[i]->clk); 217 } 218 219 static 220 struct mtk_ddp_comp *mtk_drm_ddp_comp_for_plane(struct drm_crtc *crtc, 221 struct drm_plane *plane, 222 unsigned int *local_layer) 223 { 224 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc); 225 struct mtk_ddp_comp *comp; 226 int i, count = 0; 227 unsigned int local_index = plane - mtk_crtc->planes; 228 229 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) { 230 comp = mtk_crtc->ddp_comp[i]; 231 if (local_index < (count + mtk_ddp_comp_layer_nr(comp))) { 232 *local_layer = local_index - count; 233 return comp; 234 } 235 count += mtk_ddp_comp_layer_nr(comp); 236 } 237 238 WARN(1, "Failed to find component for plane %d\n", plane->index); 239 return NULL; 240 } 241 242 #if IS_REACHABLE(CONFIG_MTK_CMDQ) 243 static void ddp_cmdq_cb(struct cmdq_cb_data data) 244 { 245 cmdq_pkt_destroy(data.data); 246 } 247 #endif 248 249 static int mtk_crtc_ddp_hw_init(struct mtk_drm_crtc *mtk_crtc) 250 { 251 struct drm_crtc *crtc = &mtk_crtc->base; 252 struct drm_connector *connector; 253 struct drm_encoder *encoder; 254 struct drm_connector_list_iter conn_iter; 255 unsigned int width, height, vrefresh, bpc = MTK_MAX_BPC; 256 int ret; 257 int i; 258 259 if (WARN_ON(!crtc->state)) 260 return -EINVAL; 261 262 width = crtc->state->adjusted_mode.hdisplay; 263 height = crtc->state->adjusted_mode.vdisplay; 264 vrefresh = crtc->state->adjusted_mode.vrefresh; 265 266 drm_for_each_encoder(encoder, crtc->dev) { 267 if (encoder->crtc != crtc) 268 continue; 269 270 drm_connector_list_iter_begin(crtc->dev, &conn_iter); 271 drm_for_each_connector_iter(connector, &conn_iter) { 272 if (connector->encoder != encoder) 273 continue; 274 if (connector->display_info.bpc != 0 && 275 bpc > connector->display_info.bpc) 276 bpc = connector->display_info.bpc; 277 } 278 drm_connector_list_iter_end(&conn_iter); 279 } 280 281 ret = pm_runtime_get_sync(crtc->dev->dev); 282 if (ret < 0) { 283 DRM_ERROR("Failed to enable power domain: %d\n", ret); 284 return ret; 285 } 286 287 ret = mtk_disp_mutex_prepare(mtk_crtc->mutex); 288 if (ret < 0) { 289 DRM_ERROR("Failed to enable mutex clock: %d\n", ret); 290 goto err_pm_runtime_put; 291 } 292 293 ret = mtk_crtc_ddp_clk_enable(mtk_crtc); 294 if (ret < 0) { 295 DRM_ERROR("Failed to enable component clocks: %d\n", ret); 296 goto err_mutex_unprepare; 297 } 298 299 for (i = 0; i < mtk_crtc->ddp_comp_nr - 1; i++) { 300 mtk_mmsys_ddp_connect(mtk_crtc->mmsys_dev, 301 mtk_crtc->ddp_comp[i]->id, 302 mtk_crtc->ddp_comp[i + 1]->id); 303 mtk_disp_mutex_add_comp(mtk_crtc->mutex, 304 mtk_crtc->ddp_comp[i]->id); 305 } 306 mtk_disp_mutex_add_comp(mtk_crtc->mutex, mtk_crtc->ddp_comp[i]->id); 307 mtk_disp_mutex_enable(mtk_crtc->mutex); 308 309 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) { 310 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[i]; 311 312 if (i == 1) 313 mtk_ddp_comp_bgclr_in_on(comp); 314 315 mtk_ddp_comp_config(comp, width, height, vrefresh, bpc, NULL); 316 mtk_ddp_comp_start(comp); 317 } 318 319 /* Initially configure all planes */ 320 for (i = 0; i < mtk_crtc->layer_nr; i++) { 321 struct drm_plane *plane = &mtk_crtc->planes[i]; 322 struct mtk_plane_state *plane_state; 323 struct mtk_ddp_comp *comp; 324 unsigned int local_layer; 325 326 plane_state = to_mtk_plane_state(plane->state); 327 comp = mtk_drm_ddp_comp_for_plane(crtc, plane, &local_layer); 328 if (comp) 329 mtk_ddp_comp_layer_config(comp, local_layer, 330 plane_state, NULL); 331 } 332 333 return 0; 334 335 err_mutex_unprepare: 336 mtk_disp_mutex_unprepare(mtk_crtc->mutex); 337 err_pm_runtime_put: 338 pm_runtime_put(crtc->dev->dev); 339 return ret; 340 } 341 342 static void mtk_crtc_ddp_hw_fini(struct mtk_drm_crtc *mtk_crtc) 343 { 344 struct drm_device *drm = mtk_crtc->base.dev; 345 struct drm_crtc *crtc = &mtk_crtc->base; 346 int i; 347 348 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) { 349 mtk_ddp_comp_stop(mtk_crtc->ddp_comp[i]); 350 if (i == 1) 351 mtk_ddp_comp_bgclr_in_off(mtk_crtc->ddp_comp[i]); 352 } 353 354 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) 355 mtk_disp_mutex_remove_comp(mtk_crtc->mutex, 356 mtk_crtc->ddp_comp[i]->id); 357 mtk_disp_mutex_disable(mtk_crtc->mutex); 358 for (i = 0; i < mtk_crtc->ddp_comp_nr - 1; i++) { 359 mtk_mmsys_ddp_disconnect(mtk_crtc->mmsys_dev, 360 mtk_crtc->ddp_comp[i]->id, 361 mtk_crtc->ddp_comp[i + 1]->id); 362 mtk_disp_mutex_remove_comp(mtk_crtc->mutex, 363 mtk_crtc->ddp_comp[i]->id); 364 } 365 mtk_disp_mutex_remove_comp(mtk_crtc->mutex, mtk_crtc->ddp_comp[i]->id); 366 mtk_crtc_ddp_clk_disable(mtk_crtc); 367 mtk_disp_mutex_unprepare(mtk_crtc->mutex); 368 369 pm_runtime_put(drm->dev); 370 371 if (crtc->state->event && !crtc->state->active) { 372 spin_lock_irq(&crtc->dev->event_lock); 373 drm_crtc_send_vblank_event(crtc, crtc->state->event); 374 crtc->state->event = NULL; 375 spin_unlock_irq(&crtc->dev->event_lock); 376 } 377 } 378 379 static void mtk_crtc_ddp_config(struct drm_crtc *crtc, 380 struct cmdq_pkt *cmdq_handle) 381 { 382 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc); 383 struct mtk_crtc_state *state = to_mtk_crtc_state(mtk_crtc->base.state); 384 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0]; 385 unsigned int i; 386 unsigned int local_layer; 387 388 /* 389 * TODO: instead of updating the registers here, we should prepare 390 * working registers in atomic_commit and let the hardware command 391 * queue update module registers on vblank. 392 */ 393 if (state->pending_config) { 394 mtk_ddp_comp_config(comp, state->pending_width, 395 state->pending_height, 396 state->pending_vrefresh, 0, 397 cmdq_handle); 398 399 state->pending_config = false; 400 } 401 402 if (mtk_crtc->pending_planes) { 403 for (i = 0; i < mtk_crtc->layer_nr; i++) { 404 struct drm_plane *plane = &mtk_crtc->planes[i]; 405 struct mtk_plane_state *plane_state; 406 407 plane_state = to_mtk_plane_state(plane->state); 408 409 if (!plane_state->pending.config) 410 continue; 411 412 comp = mtk_drm_ddp_comp_for_plane(crtc, plane, 413 &local_layer); 414 415 if (comp) 416 mtk_ddp_comp_layer_config(comp, local_layer, 417 plane_state, 418 cmdq_handle); 419 plane_state->pending.config = false; 420 } 421 mtk_crtc->pending_planes = false; 422 } 423 424 if (mtk_crtc->pending_async_planes) { 425 for (i = 0; i < mtk_crtc->layer_nr; i++) { 426 struct drm_plane *plane = &mtk_crtc->planes[i]; 427 struct mtk_plane_state *plane_state; 428 429 plane_state = to_mtk_plane_state(plane->state); 430 431 if (!plane_state->pending.async_config) 432 continue; 433 434 comp = mtk_drm_ddp_comp_for_plane(crtc, plane, 435 &local_layer); 436 437 if (comp) 438 mtk_ddp_comp_layer_config(comp, local_layer, 439 plane_state, 440 cmdq_handle); 441 plane_state->pending.async_config = false; 442 } 443 mtk_crtc->pending_async_planes = false; 444 } 445 } 446 447 static void mtk_drm_crtc_hw_config(struct mtk_drm_crtc *mtk_crtc) 448 { 449 #if IS_REACHABLE(CONFIG_MTK_CMDQ) 450 struct cmdq_pkt *cmdq_handle; 451 #endif 452 struct drm_crtc *crtc = &mtk_crtc->base; 453 struct mtk_drm_private *priv = crtc->dev->dev_private; 454 unsigned int pending_planes = 0, pending_async_planes = 0; 455 int i; 456 457 mutex_lock(&mtk_crtc->hw_lock); 458 for (i = 0; i < mtk_crtc->layer_nr; i++) { 459 struct drm_plane *plane = &mtk_crtc->planes[i]; 460 struct mtk_plane_state *plane_state; 461 462 plane_state = to_mtk_plane_state(plane->state); 463 if (plane_state->pending.dirty) { 464 plane_state->pending.config = true; 465 plane_state->pending.dirty = false; 466 pending_planes |= BIT(i); 467 } else if (plane_state->pending.async_dirty) { 468 plane_state->pending.async_config = true; 469 plane_state->pending.async_dirty = false; 470 pending_async_planes |= BIT(i); 471 } 472 } 473 if (pending_planes) 474 mtk_crtc->pending_planes = true; 475 if (pending_async_planes) 476 mtk_crtc->pending_async_planes = true; 477 478 if (priv->data->shadow_register) { 479 mtk_disp_mutex_acquire(mtk_crtc->mutex); 480 mtk_crtc_ddp_config(crtc, NULL); 481 mtk_disp_mutex_release(mtk_crtc->mutex); 482 } 483 #if IS_REACHABLE(CONFIG_MTK_CMDQ) 484 if (mtk_crtc->cmdq_client) { 485 mbox_flush(mtk_crtc->cmdq_client->chan, 2000); 486 cmdq_handle = cmdq_pkt_create(mtk_crtc->cmdq_client, PAGE_SIZE); 487 cmdq_pkt_clear_event(cmdq_handle, mtk_crtc->cmdq_event); 488 cmdq_pkt_wfe(cmdq_handle, mtk_crtc->cmdq_event); 489 mtk_crtc_ddp_config(crtc, cmdq_handle); 490 cmdq_pkt_finalize(cmdq_handle); 491 cmdq_pkt_flush_async(cmdq_handle, ddp_cmdq_cb, cmdq_handle); 492 } 493 #endif 494 mutex_unlock(&mtk_crtc->hw_lock); 495 } 496 497 int mtk_drm_crtc_plane_check(struct drm_crtc *crtc, struct drm_plane *plane, 498 struct mtk_plane_state *state) 499 { 500 unsigned int local_layer; 501 struct mtk_ddp_comp *comp; 502 503 comp = mtk_drm_ddp_comp_for_plane(crtc, plane, &local_layer); 504 if (comp) 505 return mtk_ddp_comp_layer_check(comp, local_layer, state); 506 return 0; 507 } 508 509 void mtk_drm_crtc_async_update(struct drm_crtc *crtc, struct drm_plane *plane, 510 struct drm_plane_state *new_state) 511 { 512 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc); 513 const struct drm_plane_helper_funcs *plane_helper_funcs = 514 plane->helper_private; 515 516 if (!mtk_crtc->enabled) 517 return; 518 519 plane_helper_funcs->atomic_update(plane, new_state); 520 mtk_drm_crtc_hw_config(mtk_crtc); 521 } 522 523 static void mtk_drm_crtc_atomic_enable(struct drm_crtc *crtc, 524 struct drm_crtc_state *old_state) 525 { 526 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc); 527 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0]; 528 int ret; 529 530 DRM_DEBUG_DRIVER("%s %d\n", __func__, crtc->base.id); 531 532 ret = mtk_smi_larb_get(comp->larb_dev); 533 if (ret) { 534 DRM_ERROR("Failed to get larb: %d\n", ret); 535 return; 536 } 537 538 ret = mtk_crtc_ddp_hw_init(mtk_crtc); 539 if (ret) { 540 mtk_smi_larb_put(comp->larb_dev); 541 return; 542 } 543 544 drm_crtc_vblank_on(crtc); 545 mtk_crtc->enabled = true; 546 } 547 548 static void mtk_drm_crtc_atomic_disable(struct drm_crtc *crtc, 549 struct drm_crtc_state *old_state) 550 { 551 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc); 552 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0]; 553 int i; 554 555 DRM_DEBUG_DRIVER("%s %d\n", __func__, crtc->base.id); 556 if (!mtk_crtc->enabled) 557 return; 558 559 /* Set all pending plane state to disabled */ 560 for (i = 0; i < mtk_crtc->layer_nr; i++) { 561 struct drm_plane *plane = &mtk_crtc->planes[i]; 562 struct mtk_plane_state *plane_state; 563 564 plane_state = to_mtk_plane_state(plane->state); 565 plane_state->pending.enable = false; 566 plane_state->pending.config = true; 567 } 568 mtk_crtc->pending_planes = true; 569 570 mtk_drm_crtc_hw_config(mtk_crtc); 571 /* Wait for planes to be disabled */ 572 drm_crtc_wait_one_vblank(crtc); 573 574 drm_crtc_vblank_off(crtc); 575 mtk_crtc_ddp_hw_fini(mtk_crtc); 576 mtk_smi_larb_put(comp->larb_dev); 577 578 mtk_crtc->enabled = false; 579 } 580 581 static void mtk_drm_crtc_atomic_begin(struct drm_crtc *crtc, 582 struct drm_crtc_state *old_crtc_state) 583 { 584 struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state); 585 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc); 586 587 if (mtk_crtc->event && state->base.event) 588 DRM_ERROR("new event while there is still a pending event\n"); 589 590 if (state->base.event) { 591 state->base.event->pipe = drm_crtc_index(crtc); 592 WARN_ON(drm_crtc_vblank_get(crtc) != 0); 593 mtk_crtc->event = state->base.event; 594 state->base.event = NULL; 595 } 596 } 597 598 static void mtk_drm_crtc_atomic_flush(struct drm_crtc *crtc, 599 struct drm_crtc_state *old_crtc_state) 600 { 601 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc); 602 int i; 603 604 if (mtk_crtc->event) 605 mtk_crtc->pending_needs_vblank = true; 606 if (crtc->state->color_mgmt_changed) 607 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) { 608 mtk_ddp_gamma_set(mtk_crtc->ddp_comp[i], crtc->state); 609 mtk_ddp_ctm_set(mtk_crtc->ddp_comp[i], crtc->state); 610 } 611 mtk_drm_crtc_hw_config(mtk_crtc); 612 } 613 614 static const struct drm_crtc_funcs mtk_crtc_funcs = { 615 .set_config = drm_atomic_helper_set_config, 616 .page_flip = drm_atomic_helper_page_flip, 617 .destroy = mtk_drm_crtc_destroy, 618 .reset = mtk_drm_crtc_reset, 619 .atomic_duplicate_state = mtk_drm_crtc_duplicate_state, 620 .atomic_destroy_state = mtk_drm_crtc_destroy_state, 621 .gamma_set = drm_atomic_helper_legacy_gamma_set, 622 .enable_vblank = mtk_drm_crtc_enable_vblank, 623 .disable_vblank = mtk_drm_crtc_disable_vblank, 624 }; 625 626 static const struct drm_crtc_helper_funcs mtk_crtc_helper_funcs = { 627 .mode_fixup = mtk_drm_crtc_mode_fixup, 628 .mode_set_nofb = mtk_drm_crtc_mode_set_nofb, 629 .atomic_begin = mtk_drm_crtc_atomic_begin, 630 .atomic_flush = mtk_drm_crtc_atomic_flush, 631 .atomic_enable = mtk_drm_crtc_atomic_enable, 632 .atomic_disable = mtk_drm_crtc_atomic_disable, 633 }; 634 635 static int mtk_drm_crtc_init(struct drm_device *drm, 636 struct mtk_drm_crtc *mtk_crtc, 637 unsigned int pipe) 638 { 639 struct drm_plane *primary = NULL; 640 struct drm_plane *cursor = NULL; 641 int i, ret; 642 643 for (i = 0; i < mtk_crtc->layer_nr; i++) { 644 if (mtk_crtc->planes[i].type == DRM_PLANE_TYPE_PRIMARY) 645 primary = &mtk_crtc->planes[i]; 646 else if (mtk_crtc->planes[i].type == DRM_PLANE_TYPE_CURSOR) 647 cursor = &mtk_crtc->planes[i]; 648 } 649 650 ret = drm_crtc_init_with_planes(drm, &mtk_crtc->base, primary, cursor, 651 &mtk_crtc_funcs, NULL); 652 if (ret) 653 goto err_cleanup_crtc; 654 655 drm_crtc_helper_add(&mtk_crtc->base, &mtk_crtc_helper_funcs); 656 657 return 0; 658 659 err_cleanup_crtc: 660 drm_crtc_cleanup(&mtk_crtc->base); 661 return ret; 662 } 663 664 void mtk_crtc_ddp_irq(struct drm_crtc *crtc, struct mtk_ddp_comp *comp) 665 { 666 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc); 667 struct mtk_drm_private *priv = crtc->dev->dev_private; 668 669 #if IS_REACHABLE(CONFIG_MTK_CMDQ) 670 if (!priv->data->shadow_register && !mtk_crtc->cmdq_client) 671 #else 672 if (!priv->data->shadow_register) 673 #endif 674 mtk_crtc_ddp_config(crtc, NULL); 675 676 mtk_drm_finish_page_flip(mtk_crtc); 677 } 678 679 static int mtk_drm_crtc_num_comp_planes(struct mtk_drm_crtc *mtk_crtc, 680 int comp_idx) 681 { 682 struct mtk_ddp_comp *comp; 683 684 if (comp_idx > 1) 685 return 0; 686 687 comp = mtk_crtc->ddp_comp[comp_idx]; 688 if (!comp->funcs) 689 return 0; 690 691 if (comp_idx == 1 && !comp->funcs->bgclr_in_on) 692 return 0; 693 694 return mtk_ddp_comp_layer_nr(comp); 695 } 696 697 static inline 698 enum drm_plane_type mtk_drm_crtc_plane_type(unsigned int plane_idx, 699 unsigned int num_planes) 700 { 701 if (plane_idx == 0) 702 return DRM_PLANE_TYPE_PRIMARY; 703 else if (plane_idx == (num_planes - 1)) 704 return DRM_PLANE_TYPE_CURSOR; 705 else 706 return DRM_PLANE_TYPE_OVERLAY; 707 708 } 709 710 static int mtk_drm_crtc_init_comp_planes(struct drm_device *drm_dev, 711 struct mtk_drm_crtc *mtk_crtc, 712 int comp_idx, int pipe) 713 { 714 int num_planes = mtk_drm_crtc_num_comp_planes(mtk_crtc, comp_idx); 715 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[comp_idx]; 716 int i, ret; 717 718 for (i = 0; i < num_planes; i++) { 719 ret = mtk_plane_init(drm_dev, 720 &mtk_crtc->planes[mtk_crtc->layer_nr], 721 BIT(pipe), 722 mtk_drm_crtc_plane_type(mtk_crtc->layer_nr, 723 num_planes), 724 mtk_ddp_comp_supported_rotations(comp)); 725 if (ret) 726 return ret; 727 728 mtk_crtc->layer_nr++; 729 } 730 return 0; 731 } 732 733 int mtk_drm_crtc_create(struct drm_device *drm_dev, 734 const enum mtk_ddp_comp_id *path, unsigned int path_len) 735 { 736 struct mtk_drm_private *priv = drm_dev->dev_private; 737 struct device *dev = drm_dev->dev; 738 struct mtk_drm_crtc *mtk_crtc; 739 unsigned int num_comp_planes = 0; 740 int pipe = priv->num_pipes; 741 int ret; 742 int i; 743 bool has_ctm = false; 744 uint gamma_lut_size = 0; 745 746 if (!path) 747 return 0; 748 749 for (i = 0; i < path_len; i++) { 750 enum mtk_ddp_comp_id comp_id = path[i]; 751 struct device_node *node; 752 753 node = priv->comp_node[comp_id]; 754 if (!node) { 755 dev_info(dev, 756 "Not creating crtc %d because component %d is disabled or missing\n", 757 pipe, comp_id); 758 return 0; 759 } 760 } 761 762 mtk_crtc = devm_kzalloc(dev, sizeof(*mtk_crtc), GFP_KERNEL); 763 if (!mtk_crtc) 764 return -ENOMEM; 765 766 mtk_crtc->mmsys_dev = priv->mmsys_dev; 767 mtk_crtc->ddp_comp_nr = path_len; 768 mtk_crtc->ddp_comp = devm_kmalloc_array(dev, mtk_crtc->ddp_comp_nr, 769 sizeof(*mtk_crtc->ddp_comp), 770 GFP_KERNEL); 771 if (!mtk_crtc->ddp_comp) 772 return -ENOMEM; 773 774 mtk_crtc->mutex = mtk_disp_mutex_get(priv->mutex_dev, pipe); 775 if (IS_ERR(mtk_crtc->mutex)) { 776 ret = PTR_ERR(mtk_crtc->mutex); 777 dev_err(dev, "Failed to get mutex: %d\n", ret); 778 return ret; 779 } 780 781 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) { 782 enum mtk_ddp_comp_id comp_id = path[i]; 783 struct mtk_ddp_comp *comp; 784 struct device_node *node; 785 786 node = priv->comp_node[comp_id]; 787 comp = priv->ddp_comp[comp_id]; 788 if (!comp) { 789 dev_err(dev, "Component %pOF not initialized\n", node); 790 ret = -ENODEV; 791 return ret; 792 } 793 794 mtk_crtc->ddp_comp[i] = comp; 795 796 if (comp->funcs) { 797 if (comp->funcs->gamma_set) 798 gamma_lut_size = MTK_LUT_SIZE; 799 800 if (comp->funcs->ctm_set) 801 has_ctm = true; 802 } 803 } 804 805 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) 806 num_comp_planes += mtk_drm_crtc_num_comp_planes(mtk_crtc, i); 807 808 mtk_crtc->planes = devm_kcalloc(dev, num_comp_planes, 809 sizeof(struct drm_plane), GFP_KERNEL); 810 811 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) { 812 ret = mtk_drm_crtc_init_comp_planes(drm_dev, mtk_crtc, i, 813 pipe); 814 if (ret) 815 return ret; 816 } 817 818 ret = mtk_drm_crtc_init(drm_dev, mtk_crtc, pipe); 819 if (ret < 0) 820 return ret; 821 822 if (gamma_lut_size) 823 drm_mode_crtc_set_gamma_size(&mtk_crtc->base, gamma_lut_size); 824 drm_crtc_enable_color_mgmt(&mtk_crtc->base, 0, has_ctm, gamma_lut_size); 825 priv->num_pipes++; 826 mutex_init(&mtk_crtc->hw_lock); 827 828 #if IS_REACHABLE(CONFIG_MTK_CMDQ) 829 mtk_crtc->cmdq_client = 830 cmdq_mbox_create(mtk_crtc->mmsys_dev, 831 drm_crtc_index(&mtk_crtc->base), 832 2000); 833 if (IS_ERR(mtk_crtc->cmdq_client)) { 834 dev_dbg(dev, "mtk_crtc %d failed to create mailbox client, writing register by CPU now\n", 835 drm_crtc_index(&mtk_crtc->base)); 836 mtk_crtc->cmdq_client = NULL; 837 } 838 ret = of_property_read_u32_index(priv->mutex_node, 839 "mediatek,gce-events", 840 drm_crtc_index(&mtk_crtc->base), 841 &mtk_crtc->cmdq_event); 842 if (ret) 843 dev_dbg(dev, "mtk_crtc %d failed to get mediatek,gce-events property\n", 844 drm_crtc_index(&mtk_crtc->base)); 845 #endif 846 return 0; 847 } 848