1 /* 2 * Copyright (c) 2014 Samsung Electronics Co., Ltd 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sub license, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the 12 * next paragraph) shall be included in all copies or substantial portions 13 * of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21 * DEALINGS IN THE SOFTWARE. 22 */ 23 24 #include <linux/err.h> 25 #include <linux/module.h> 26 #include <linux/mutex.h> 27 28 #include <drm/drm_atomic_state_helper.h> 29 #include <drm/drm_bridge.h> 30 #include <drm/drm_encoder.h> 31 32 #include "drm_crtc_internal.h" 33 34 /** 35 * DOC: overview 36 * 37 * &struct drm_bridge represents a device that hangs on to an encoder. These are 38 * handy when a regular &drm_encoder entity isn't enough to represent the entire 39 * encoder chain. 40 * 41 * A bridge is always attached to a single &drm_encoder at a time, but can be 42 * either connected to it directly, or through an intermediate bridge:: 43 * 44 * encoder ---> bridge B ---> bridge A 45 * 46 * Here, the output of the encoder feeds to bridge B, and that furthers feeds to 47 * bridge A. 48 * 49 * The driver using the bridge is responsible to make the associations between 50 * the encoder and bridges. Once these links are made, the bridges will 51 * participate along with encoder functions to perform mode_set/enable/disable 52 * through the ops provided in &drm_bridge_funcs. 53 * 54 * drm_bridge, like drm_panel, aren't drm_mode_object entities like planes, 55 * CRTCs, encoders or connectors and hence are not visible to userspace. They 56 * just provide additional hooks to get the desired output at the end of the 57 * encoder chain. 58 * 59 * Bridges can also be chained up using the &drm_bridge.chain_node field. 60 * 61 * Both legacy CRTC helpers and the new atomic modeset helpers support bridges. 62 */ 63 64 static DEFINE_MUTEX(bridge_lock); 65 static LIST_HEAD(bridge_list); 66 67 /** 68 * drm_bridge_add - add the given bridge to the global bridge list 69 * 70 * @bridge: bridge control structure 71 */ 72 void drm_bridge_add(struct drm_bridge *bridge) 73 { 74 mutex_lock(&bridge_lock); 75 list_add_tail(&bridge->list, &bridge_list); 76 mutex_unlock(&bridge_lock); 77 } 78 EXPORT_SYMBOL(drm_bridge_add); 79 80 /** 81 * drm_bridge_remove - remove the given bridge from the global bridge list 82 * 83 * @bridge: bridge control structure 84 */ 85 void drm_bridge_remove(struct drm_bridge *bridge) 86 { 87 mutex_lock(&bridge_lock); 88 list_del_init(&bridge->list); 89 mutex_unlock(&bridge_lock); 90 } 91 EXPORT_SYMBOL(drm_bridge_remove); 92 93 static struct drm_private_state * 94 drm_bridge_atomic_duplicate_priv_state(struct drm_private_obj *obj) 95 { 96 struct drm_bridge *bridge = drm_priv_to_bridge(obj); 97 struct drm_bridge_state *state; 98 99 state = bridge->funcs->atomic_duplicate_state(bridge); 100 return state ? &state->base : NULL; 101 } 102 103 static void 104 drm_bridge_atomic_destroy_priv_state(struct drm_private_obj *obj, 105 struct drm_private_state *s) 106 { 107 struct drm_bridge_state *state = drm_priv_to_bridge_state(s); 108 struct drm_bridge *bridge = drm_priv_to_bridge(obj); 109 110 bridge->funcs->atomic_destroy_state(bridge, state); 111 } 112 113 static const struct drm_private_state_funcs drm_bridge_priv_state_funcs = { 114 .atomic_duplicate_state = drm_bridge_atomic_duplicate_priv_state, 115 .atomic_destroy_state = drm_bridge_atomic_destroy_priv_state, 116 }; 117 118 /** 119 * drm_bridge_attach - attach the bridge to an encoder's chain 120 * 121 * @encoder: DRM encoder 122 * @bridge: bridge to attach 123 * @previous: previous bridge in the chain (optional) 124 * 125 * Called by a kms driver to link the bridge to an encoder's chain. The previous 126 * argument specifies the previous bridge in the chain. If NULL, the bridge is 127 * linked directly at the encoder's output. Otherwise it is linked at the 128 * previous bridge's output. 129 * 130 * If non-NULL the previous bridge must be already attached by a call to this 131 * function. 132 * 133 * Note that bridges attached to encoders are auto-detached during encoder 134 * cleanup in drm_encoder_cleanup(), so drm_bridge_attach() should generally 135 * *not* be balanced with a drm_bridge_detach() in driver code. 136 * 137 * RETURNS: 138 * Zero on success, error code on failure 139 */ 140 int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge, 141 struct drm_bridge *previous) 142 { 143 int ret; 144 145 if (!encoder || !bridge) 146 return -EINVAL; 147 148 if (previous && (!previous->dev || previous->encoder != encoder)) 149 return -EINVAL; 150 151 if (bridge->dev) 152 return -EBUSY; 153 154 bridge->dev = encoder->dev; 155 bridge->encoder = encoder; 156 157 if (previous) 158 list_add(&bridge->chain_node, &previous->chain_node); 159 else 160 list_add(&bridge->chain_node, &encoder->bridge_chain); 161 162 if (bridge->funcs->attach) { 163 ret = bridge->funcs->attach(bridge); 164 if (ret < 0) 165 goto err_reset_bridge; 166 } 167 168 if (bridge->funcs->atomic_reset) { 169 struct drm_bridge_state *state; 170 171 state = bridge->funcs->atomic_reset(bridge); 172 if (IS_ERR(state)) { 173 ret = PTR_ERR(state); 174 goto err_detach_bridge; 175 } 176 177 drm_atomic_private_obj_init(bridge->dev, &bridge->base, 178 &state->base, 179 &drm_bridge_priv_state_funcs); 180 } 181 182 return 0; 183 184 err_detach_bridge: 185 if (bridge->funcs->detach) 186 bridge->funcs->detach(bridge); 187 188 err_reset_bridge: 189 bridge->dev = NULL; 190 bridge->encoder = NULL; 191 list_del(&bridge->chain_node); 192 return ret; 193 } 194 EXPORT_SYMBOL(drm_bridge_attach); 195 196 void drm_bridge_detach(struct drm_bridge *bridge) 197 { 198 if (WARN_ON(!bridge)) 199 return; 200 201 if (WARN_ON(!bridge->dev)) 202 return; 203 204 if (bridge->funcs->atomic_reset) 205 drm_atomic_private_obj_fini(&bridge->base); 206 207 if (bridge->funcs->detach) 208 bridge->funcs->detach(bridge); 209 210 list_del(&bridge->chain_node); 211 bridge->dev = NULL; 212 } 213 214 /** 215 * DOC: bridge callbacks 216 * 217 * The &drm_bridge_funcs ops are populated by the bridge driver. The DRM 218 * internals (atomic and CRTC helpers) use the helpers defined in drm_bridge.c 219 * These helpers call a specific &drm_bridge_funcs op for all the bridges 220 * during encoder configuration. 221 * 222 * For detailed specification of the bridge callbacks see &drm_bridge_funcs. 223 */ 224 225 /** 226 * drm_bridge_chain_mode_fixup - fixup proposed mode for all bridges in the 227 * encoder chain 228 * @bridge: bridge control structure 229 * @mode: desired mode to be set for the bridge 230 * @adjusted_mode: updated mode that works for this bridge 231 * 232 * Calls &drm_bridge_funcs.mode_fixup for all the bridges in the 233 * encoder chain, starting from the first bridge to the last. 234 * 235 * Note: the bridge passed should be the one closest to the encoder 236 * 237 * RETURNS: 238 * true on success, false on failure 239 */ 240 bool drm_bridge_chain_mode_fixup(struct drm_bridge *bridge, 241 const struct drm_display_mode *mode, 242 struct drm_display_mode *adjusted_mode) 243 { 244 struct drm_encoder *encoder; 245 246 if (!bridge) 247 return true; 248 249 encoder = bridge->encoder; 250 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) { 251 if (!bridge->funcs->mode_fixup) 252 continue; 253 254 if (!bridge->funcs->mode_fixup(bridge, mode, adjusted_mode)) 255 return false; 256 } 257 258 return true; 259 } 260 EXPORT_SYMBOL(drm_bridge_chain_mode_fixup); 261 262 /** 263 * drm_bridge_chain_mode_valid - validate the mode against all bridges in the 264 * encoder chain. 265 * @bridge: bridge control structure 266 * @mode: desired mode to be validated 267 * 268 * Calls &drm_bridge_funcs.mode_valid for all the bridges in the encoder 269 * chain, starting from the first bridge to the last. If at least one bridge 270 * does not accept the mode the function returns the error code. 271 * 272 * Note: the bridge passed should be the one closest to the encoder. 273 * 274 * RETURNS: 275 * MODE_OK on success, drm_mode_status Enum error code on failure 276 */ 277 enum drm_mode_status 278 drm_bridge_chain_mode_valid(struct drm_bridge *bridge, 279 const struct drm_display_mode *mode) 280 { 281 struct drm_encoder *encoder; 282 283 if (!bridge) 284 return MODE_OK; 285 286 encoder = bridge->encoder; 287 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) { 288 enum drm_mode_status ret; 289 290 if (!bridge->funcs->mode_valid) 291 continue; 292 293 ret = bridge->funcs->mode_valid(bridge, mode); 294 if (ret != MODE_OK) 295 return ret; 296 } 297 298 return MODE_OK; 299 } 300 EXPORT_SYMBOL(drm_bridge_chain_mode_valid); 301 302 /** 303 * drm_bridge_chain_disable - disables all bridges in the encoder chain 304 * @bridge: bridge control structure 305 * 306 * Calls &drm_bridge_funcs.disable op for all the bridges in the encoder 307 * chain, starting from the last bridge to the first. These are called before 308 * calling the encoder's prepare op. 309 * 310 * Note: the bridge passed should be the one closest to the encoder 311 */ 312 void drm_bridge_chain_disable(struct drm_bridge *bridge) 313 { 314 struct drm_encoder *encoder; 315 struct drm_bridge *iter; 316 317 if (!bridge) 318 return; 319 320 encoder = bridge->encoder; 321 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) { 322 if (iter->funcs->disable) 323 iter->funcs->disable(iter); 324 325 if (iter == bridge) 326 break; 327 } 328 } 329 EXPORT_SYMBOL(drm_bridge_chain_disable); 330 331 /** 332 * drm_bridge_chain_post_disable - cleans up after disabling all bridges in the 333 * encoder chain 334 * @bridge: bridge control structure 335 * 336 * Calls &drm_bridge_funcs.post_disable op for all the bridges in the 337 * encoder chain, starting from the first bridge to the last. These are called 338 * after completing the encoder's prepare op. 339 * 340 * Note: the bridge passed should be the one closest to the encoder 341 */ 342 void drm_bridge_chain_post_disable(struct drm_bridge *bridge) 343 { 344 struct drm_encoder *encoder; 345 346 if (!bridge) 347 return; 348 349 encoder = bridge->encoder; 350 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) { 351 if (bridge->funcs->post_disable) 352 bridge->funcs->post_disable(bridge); 353 } 354 } 355 EXPORT_SYMBOL(drm_bridge_chain_post_disable); 356 357 /** 358 * drm_bridge_chain_mode_set - set proposed mode for all bridges in the 359 * encoder chain 360 * @bridge: bridge control structure 361 * @mode: desired mode to be set for the encoder chain 362 * @adjusted_mode: updated mode that works for this encoder chain 363 * 364 * Calls &drm_bridge_funcs.mode_set op for all the bridges in the 365 * encoder chain, starting from the first bridge to the last. 366 * 367 * Note: the bridge passed should be the one closest to the encoder 368 */ 369 void drm_bridge_chain_mode_set(struct drm_bridge *bridge, 370 const struct drm_display_mode *mode, 371 const struct drm_display_mode *adjusted_mode) 372 { 373 struct drm_encoder *encoder; 374 375 if (!bridge) 376 return; 377 378 encoder = bridge->encoder; 379 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) { 380 if (bridge->funcs->mode_set) 381 bridge->funcs->mode_set(bridge, mode, adjusted_mode); 382 } 383 } 384 EXPORT_SYMBOL(drm_bridge_chain_mode_set); 385 386 /** 387 * drm_bridge_chain_pre_enable - prepares for enabling all bridges in the 388 * encoder chain 389 * @bridge: bridge control structure 390 * 391 * Calls &drm_bridge_funcs.pre_enable op for all the bridges in the encoder 392 * chain, starting from the last bridge to the first. These are called 393 * before calling the encoder's commit op. 394 * 395 * Note: the bridge passed should be the one closest to the encoder 396 */ 397 void drm_bridge_chain_pre_enable(struct drm_bridge *bridge) 398 { 399 struct drm_encoder *encoder; 400 struct drm_bridge *iter; 401 402 if (!bridge) 403 return; 404 405 encoder = bridge->encoder; 406 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) { 407 if (iter->funcs->pre_enable) 408 iter->funcs->pre_enable(iter); 409 } 410 } 411 EXPORT_SYMBOL(drm_bridge_chain_pre_enable); 412 413 /** 414 * drm_bridge_chain_enable - enables all bridges in the encoder chain 415 * @bridge: bridge control structure 416 * 417 * Calls &drm_bridge_funcs.enable op for all the bridges in the encoder 418 * chain, starting from the first bridge to the last. These are called 419 * after completing the encoder's commit op. 420 * 421 * Note that the bridge passed should be the one closest to the encoder 422 */ 423 void drm_bridge_chain_enable(struct drm_bridge *bridge) 424 { 425 struct drm_encoder *encoder; 426 427 if (!bridge) 428 return; 429 430 encoder = bridge->encoder; 431 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) { 432 if (bridge->funcs->enable) 433 bridge->funcs->enable(bridge); 434 } 435 } 436 EXPORT_SYMBOL(drm_bridge_chain_enable); 437 438 /** 439 * drm_atomic_bridge_chain_disable - disables all bridges in the encoder chain 440 * @bridge: bridge control structure 441 * @old_state: old atomic state 442 * 443 * Calls &drm_bridge_funcs.atomic_disable (falls back on 444 * &drm_bridge_funcs.disable) op for all the bridges in the encoder chain, 445 * starting from the last bridge to the first. These are called before calling 446 * &drm_encoder_helper_funcs.atomic_disable 447 * 448 * Note: the bridge passed should be the one closest to the encoder 449 */ 450 void drm_atomic_bridge_chain_disable(struct drm_bridge *bridge, 451 struct drm_atomic_state *old_state) 452 { 453 struct drm_encoder *encoder; 454 struct drm_bridge *iter; 455 456 if (!bridge) 457 return; 458 459 encoder = bridge->encoder; 460 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) { 461 if (iter->funcs->atomic_disable) { 462 struct drm_bridge_state *old_bridge_state; 463 464 old_bridge_state = 465 drm_atomic_get_old_bridge_state(old_state, 466 iter); 467 if (WARN_ON(!old_bridge_state)) 468 return; 469 470 iter->funcs->atomic_disable(iter, old_bridge_state); 471 } else if (iter->funcs->disable) { 472 iter->funcs->disable(iter); 473 } 474 475 if (iter == bridge) 476 break; 477 } 478 } 479 EXPORT_SYMBOL(drm_atomic_bridge_chain_disable); 480 481 /** 482 * drm_atomic_bridge_chain_post_disable - cleans up after disabling all bridges 483 * in the encoder chain 484 * @bridge: bridge control structure 485 * @old_state: old atomic state 486 * 487 * Calls &drm_bridge_funcs.atomic_post_disable (falls back on 488 * &drm_bridge_funcs.post_disable) op for all the bridges in the encoder chain, 489 * starting from the first bridge to the last. These are called after completing 490 * &drm_encoder_helper_funcs.atomic_disable 491 * 492 * Note: the bridge passed should be the one closest to the encoder 493 */ 494 void drm_atomic_bridge_chain_post_disable(struct drm_bridge *bridge, 495 struct drm_atomic_state *old_state) 496 { 497 struct drm_encoder *encoder; 498 499 if (!bridge) 500 return; 501 502 encoder = bridge->encoder; 503 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) { 504 if (bridge->funcs->atomic_post_disable) { 505 struct drm_bridge_state *old_bridge_state; 506 507 old_bridge_state = 508 drm_atomic_get_old_bridge_state(old_state, 509 bridge); 510 if (WARN_ON(!old_bridge_state)) 511 return; 512 513 bridge->funcs->atomic_post_disable(bridge, 514 old_bridge_state); 515 } else if (bridge->funcs->post_disable) { 516 bridge->funcs->post_disable(bridge); 517 } 518 } 519 } 520 EXPORT_SYMBOL(drm_atomic_bridge_chain_post_disable); 521 522 /** 523 * drm_atomic_bridge_chain_pre_enable - prepares for enabling all bridges in 524 * the encoder chain 525 * @bridge: bridge control structure 526 * @old_state: old atomic state 527 * 528 * Calls &drm_bridge_funcs.atomic_pre_enable (falls back on 529 * &drm_bridge_funcs.pre_enable) op for all the bridges in the encoder chain, 530 * starting from the last bridge to the first. These are called before calling 531 * &drm_encoder_helper_funcs.atomic_enable 532 * 533 * Note: the bridge passed should be the one closest to the encoder 534 */ 535 void drm_atomic_bridge_chain_pre_enable(struct drm_bridge *bridge, 536 struct drm_atomic_state *old_state) 537 { 538 struct drm_encoder *encoder; 539 struct drm_bridge *iter; 540 541 if (!bridge) 542 return; 543 544 encoder = bridge->encoder; 545 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) { 546 if (iter->funcs->atomic_pre_enable) { 547 struct drm_bridge_state *old_bridge_state; 548 549 old_bridge_state = 550 drm_atomic_get_old_bridge_state(old_state, 551 iter); 552 if (WARN_ON(!old_bridge_state)) 553 return; 554 555 iter->funcs->atomic_pre_enable(iter, old_bridge_state); 556 } else if (iter->funcs->pre_enable) { 557 iter->funcs->pre_enable(iter); 558 } 559 560 if (iter == bridge) 561 break; 562 } 563 } 564 EXPORT_SYMBOL(drm_atomic_bridge_chain_pre_enable); 565 566 /** 567 * drm_atomic_bridge_chain_enable - enables all bridges in the encoder chain 568 * @bridge: bridge control structure 569 * @old_state: old atomic state 570 * 571 * Calls &drm_bridge_funcs.atomic_enable (falls back on 572 * &drm_bridge_funcs.enable) op for all the bridges in the encoder chain, 573 * starting from the first bridge to the last. These are called after completing 574 * &drm_encoder_helper_funcs.atomic_enable 575 * 576 * Note: the bridge passed should be the one closest to the encoder 577 */ 578 void drm_atomic_bridge_chain_enable(struct drm_bridge *bridge, 579 struct drm_atomic_state *old_state) 580 { 581 struct drm_encoder *encoder; 582 583 if (!bridge) 584 return; 585 586 encoder = bridge->encoder; 587 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) { 588 if (bridge->funcs->atomic_enable) { 589 struct drm_bridge_state *old_bridge_state; 590 591 old_bridge_state = 592 drm_atomic_get_old_bridge_state(old_state, 593 bridge); 594 if (WARN_ON(!old_bridge_state)) 595 return; 596 597 bridge->funcs->atomic_enable(bridge, old_bridge_state); 598 } else if (bridge->funcs->enable) { 599 bridge->funcs->enable(bridge); 600 } 601 } 602 } 603 EXPORT_SYMBOL(drm_atomic_bridge_chain_enable); 604 605 static int drm_atomic_bridge_check(struct drm_bridge *bridge, 606 struct drm_crtc_state *crtc_state, 607 struct drm_connector_state *conn_state) 608 { 609 if (bridge->funcs->atomic_check) { 610 struct drm_bridge_state *bridge_state; 611 int ret; 612 613 bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state, 614 bridge); 615 if (WARN_ON(!bridge_state)) 616 return -EINVAL; 617 618 ret = bridge->funcs->atomic_check(bridge, bridge_state, 619 crtc_state, conn_state); 620 if (ret) 621 return ret; 622 } else if (bridge->funcs->mode_fixup) { 623 if (!bridge->funcs->mode_fixup(bridge, &crtc_state->mode, 624 &crtc_state->adjusted_mode)) 625 return -EINVAL; 626 } 627 628 return 0; 629 } 630 631 static int select_bus_fmt_recursive(struct drm_bridge *first_bridge, 632 struct drm_bridge *cur_bridge, 633 struct drm_crtc_state *crtc_state, 634 struct drm_connector_state *conn_state, 635 u32 out_bus_fmt) 636 { 637 struct drm_bridge_state *cur_state; 638 unsigned int num_in_bus_fmts, i; 639 struct drm_bridge *prev_bridge; 640 u32 *in_bus_fmts; 641 int ret; 642 643 prev_bridge = drm_bridge_get_prev_bridge(cur_bridge); 644 cur_state = drm_atomic_get_new_bridge_state(crtc_state->state, 645 cur_bridge); 646 647 /* 648 * If bus format negotiation is not supported by this bridge, let's 649 * pass MEDIA_BUS_FMT_FIXED to the previous bridge in the chain and 650 * hope that it can handle this situation gracefully (by providing 651 * appropriate default values). 652 */ 653 if (!cur_bridge->funcs->atomic_get_input_bus_fmts) { 654 if (cur_bridge != first_bridge) { 655 ret = select_bus_fmt_recursive(first_bridge, 656 prev_bridge, crtc_state, 657 conn_state, 658 MEDIA_BUS_FMT_FIXED); 659 if (ret) 660 return ret; 661 } 662 663 /* 664 * Driver does not implement the atomic state hooks, but that's 665 * fine, as long as it does not access the bridge state. 666 */ 667 if (cur_state) { 668 cur_state->input_bus_cfg.format = MEDIA_BUS_FMT_FIXED; 669 cur_state->output_bus_cfg.format = out_bus_fmt; 670 } 671 672 return 0; 673 } 674 675 /* 676 * If the driver implements ->atomic_get_input_bus_fmts() it 677 * should also implement the atomic state hooks. 678 */ 679 if (WARN_ON(!cur_state)) 680 return -EINVAL; 681 682 in_bus_fmts = cur_bridge->funcs->atomic_get_input_bus_fmts(cur_bridge, 683 cur_state, 684 crtc_state, 685 conn_state, 686 out_bus_fmt, 687 &num_in_bus_fmts); 688 if (!num_in_bus_fmts) 689 return -ENOTSUPP; 690 else if (!in_bus_fmts) 691 return -ENOMEM; 692 693 if (first_bridge == cur_bridge) { 694 cur_state->input_bus_cfg.format = in_bus_fmts[0]; 695 cur_state->output_bus_cfg.format = out_bus_fmt; 696 kfree(in_bus_fmts); 697 return 0; 698 } 699 700 for (i = 0; i < num_in_bus_fmts; i++) { 701 ret = select_bus_fmt_recursive(first_bridge, prev_bridge, 702 crtc_state, conn_state, 703 in_bus_fmts[i]); 704 if (ret != -ENOTSUPP) 705 break; 706 } 707 708 if (!ret) { 709 cur_state->input_bus_cfg.format = in_bus_fmts[i]; 710 cur_state->output_bus_cfg.format = out_bus_fmt; 711 } 712 713 kfree(in_bus_fmts); 714 return ret; 715 } 716 717 /* 718 * This function is called by &drm_atomic_bridge_chain_check() just before 719 * calling &drm_bridge_funcs.atomic_check() on all elements of the chain. 720 * It performs bus format negotiation between bridge elements. The negotiation 721 * happens in reverse order, starting from the last element in the chain up to 722 * @bridge. 723 * 724 * Negotiation starts by retrieving supported output bus formats on the last 725 * bridge element and testing them one by one. The test is recursive, meaning 726 * that for each tested output format, the whole chain will be walked backward, 727 * and each element will have to choose an input bus format that can be 728 * transcoded to the requested output format. When a bridge element does not 729 * support transcoding into a specific output format -ENOTSUPP is returned and 730 * the next bridge element will have to try a different format. If none of the 731 * combinations worked, -ENOTSUPP is returned and the atomic modeset will fail. 732 * 733 * This implementation is relying on 734 * &drm_bridge_funcs.atomic_get_output_bus_fmts() and 735 * &drm_bridge_funcs.atomic_get_input_bus_fmts() to gather supported 736 * input/output formats. 737 * 738 * When &drm_bridge_funcs.atomic_get_output_bus_fmts() is not implemented by 739 * the last element of the chain, &drm_atomic_bridge_chain_select_bus_fmts() 740 * tries a single format: &drm_connector.display_info.bus_formats[0] if 741 * available, MEDIA_BUS_FMT_FIXED otherwise. 742 * 743 * When &drm_bridge_funcs.atomic_get_input_bus_fmts() is not implemented, 744 * &drm_atomic_bridge_chain_select_bus_fmts() skips the negotiation on the 745 * bridge element that lacks this hook and asks the previous element in the 746 * chain to try MEDIA_BUS_FMT_FIXED. It's up to bridge drivers to decide what 747 * to do in that case (fail if they want to enforce bus format negotiation, or 748 * provide a reasonable default if they need to support pipelines where not 749 * all elements support bus format negotiation). 750 */ 751 static int 752 drm_atomic_bridge_chain_select_bus_fmts(struct drm_bridge *bridge, 753 struct drm_crtc_state *crtc_state, 754 struct drm_connector_state *conn_state) 755 { 756 struct drm_connector *conn = conn_state->connector; 757 struct drm_encoder *encoder = bridge->encoder; 758 struct drm_bridge_state *last_bridge_state; 759 unsigned int i, num_out_bus_fmts; 760 struct drm_bridge *last_bridge; 761 u32 *out_bus_fmts; 762 int ret = 0; 763 764 last_bridge = list_last_entry(&encoder->bridge_chain, 765 struct drm_bridge, chain_node); 766 last_bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state, 767 last_bridge); 768 769 if (last_bridge->funcs->atomic_get_output_bus_fmts) { 770 const struct drm_bridge_funcs *funcs = last_bridge->funcs; 771 772 /* 773 * If the driver implements ->atomic_get_output_bus_fmts() it 774 * should also implement the atomic state hooks. 775 */ 776 if (WARN_ON(!last_bridge_state)) 777 return -EINVAL; 778 779 out_bus_fmts = funcs->atomic_get_output_bus_fmts(last_bridge, 780 last_bridge_state, 781 crtc_state, 782 conn_state, 783 &num_out_bus_fmts); 784 if (!num_out_bus_fmts) 785 return -ENOTSUPP; 786 else if (!out_bus_fmts) 787 return -ENOMEM; 788 } else { 789 num_out_bus_fmts = 1; 790 out_bus_fmts = kmalloc(sizeof(*out_bus_fmts), GFP_KERNEL); 791 if (!out_bus_fmts) 792 return -ENOMEM; 793 794 if (conn->display_info.num_bus_formats && 795 conn->display_info.bus_formats) 796 out_bus_fmts[0] = conn->display_info.bus_formats[0]; 797 else 798 out_bus_fmts[0] = MEDIA_BUS_FMT_FIXED; 799 } 800 801 for (i = 0; i < num_out_bus_fmts; i++) { 802 ret = select_bus_fmt_recursive(bridge, last_bridge, crtc_state, 803 conn_state, out_bus_fmts[i]); 804 if (ret != -ENOTSUPP) 805 break; 806 } 807 808 kfree(out_bus_fmts); 809 810 return ret; 811 } 812 813 static void 814 drm_atomic_bridge_propagate_bus_flags(struct drm_bridge *bridge, 815 struct drm_connector *conn, 816 struct drm_atomic_state *state) 817 { 818 struct drm_bridge_state *bridge_state, *next_bridge_state; 819 struct drm_bridge *next_bridge; 820 u32 output_flags = 0; 821 822 bridge_state = drm_atomic_get_new_bridge_state(state, bridge); 823 824 /* No bridge state attached to this bridge => nothing to propagate. */ 825 if (!bridge_state) 826 return; 827 828 next_bridge = drm_bridge_get_next_bridge(bridge); 829 830 /* 831 * Let's try to apply the most common case here, that is, propagate 832 * display_info flags for the last bridge, and propagate the input 833 * flags of the next bridge element to the output end of the current 834 * bridge when the bridge is not the last one. 835 * There are exceptions to this rule, like when signal inversion is 836 * happening at the board level, but that's something drivers can deal 837 * with from their &drm_bridge_funcs.atomic_check() implementation by 838 * simply overriding the flags value we've set here. 839 */ 840 if (!next_bridge) { 841 output_flags = conn->display_info.bus_flags; 842 } else { 843 next_bridge_state = drm_atomic_get_new_bridge_state(state, 844 next_bridge); 845 /* 846 * No bridge state attached to the next bridge, just leave the 847 * flags to 0. 848 */ 849 if (next_bridge_state) 850 output_flags = next_bridge_state->input_bus_cfg.flags; 851 } 852 853 bridge_state->output_bus_cfg.flags = output_flags; 854 855 /* 856 * Propage the output flags to the input end of the bridge. Again, it's 857 * not necessarily what all bridges want, but that's what most of them 858 * do, and by doing that by default we avoid forcing drivers to 859 * duplicate the "dummy propagation" logic. 860 */ 861 bridge_state->input_bus_cfg.flags = output_flags; 862 } 863 864 /** 865 * drm_atomic_bridge_chain_check() - Do an atomic check on the bridge chain 866 * @bridge: bridge control structure 867 * @crtc_state: new CRTC state 868 * @conn_state: new connector state 869 * 870 * First trigger a bus format negotiation before calling 871 * &drm_bridge_funcs.atomic_check() (falls back on 872 * &drm_bridge_funcs.mode_fixup()) op for all the bridges in the encoder chain, 873 * starting from the last bridge to the first. These are called before calling 874 * &drm_encoder_helper_funcs.atomic_check() 875 * 876 * RETURNS: 877 * 0 on success, a negative error code on failure 878 */ 879 int drm_atomic_bridge_chain_check(struct drm_bridge *bridge, 880 struct drm_crtc_state *crtc_state, 881 struct drm_connector_state *conn_state) 882 { 883 struct drm_connector *conn = conn_state->connector; 884 struct drm_encoder *encoder; 885 struct drm_bridge *iter; 886 int ret; 887 888 if (!bridge) 889 return 0; 890 891 ret = drm_atomic_bridge_chain_select_bus_fmts(bridge, crtc_state, 892 conn_state); 893 if (ret) 894 return ret; 895 896 encoder = bridge->encoder; 897 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) { 898 int ret; 899 900 /* 901 * Bus flags are propagated by default. If a bridge needs to 902 * tweak the input bus flags for any reason, it should happen 903 * in its &drm_bridge_funcs.atomic_check() implementation such 904 * that preceding bridges in the chain can propagate the new 905 * bus flags. 906 */ 907 drm_atomic_bridge_propagate_bus_flags(iter, conn, 908 crtc_state->state); 909 910 ret = drm_atomic_bridge_check(iter, crtc_state, conn_state); 911 if (ret) 912 return ret; 913 914 if (iter == bridge) 915 break; 916 } 917 918 return 0; 919 } 920 EXPORT_SYMBOL(drm_atomic_bridge_chain_check); 921 922 #ifdef CONFIG_OF 923 /** 924 * of_drm_find_bridge - find the bridge corresponding to the device node in 925 * the global bridge list 926 * 927 * @np: device node 928 * 929 * RETURNS: 930 * drm_bridge control struct on success, NULL on failure 931 */ 932 struct drm_bridge *of_drm_find_bridge(struct device_node *np) 933 { 934 struct drm_bridge *bridge; 935 936 mutex_lock(&bridge_lock); 937 938 list_for_each_entry(bridge, &bridge_list, list) { 939 if (bridge->of_node == np) { 940 mutex_unlock(&bridge_lock); 941 return bridge; 942 } 943 } 944 945 mutex_unlock(&bridge_lock); 946 return NULL; 947 } 948 EXPORT_SYMBOL(of_drm_find_bridge); 949 #endif 950 951 MODULE_AUTHOR("Ajay Kumar <ajaykumar.rs@samsung.com>"); 952 MODULE_DESCRIPTION("DRM bridge infrastructure"); 953 MODULE_LICENSE("GPL and additional rights"); 954