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 a chain of bridges:: 43 * 44 * [ CRTC ---> ] Encoder ---> Bridge A ---> Bridge B 45 * 46 * Here, the output of the encoder feeds to bridge A, and that furthers feeds to 47 * bridge B. Bridge chains can be arbitrarily long, and shall be fully linear: 48 * Chaining multiple bridges to the output of a bridge, or the same bridge to 49 * the output of different bridges, is not supported. 50 * 51 * Display drivers are responsible for linking encoders with the first bridge 52 * in the chains. This is done by acquiring the appropriate bridge with 53 * of_drm_find_bridge() or drm_of_find_panel_or_bridge(), or creating it for a 54 * panel with drm_panel_bridge_add_typed() (or the managed version 55 * devm_drm_panel_bridge_add_typed()). Once acquired, the bridge shall be 56 * attached to the encoder with a call to drm_bridge_attach(). 57 * 58 * Bridges are responsible for linking themselves with the next bridge in the 59 * chain, if any. This is done the same way as for encoders, with the call to 60 * drm_bridge_attach() occurring in the &drm_bridge_funcs.attach operation. 61 * 62 * Once these links are created, the bridges can participate along with encoder 63 * functions to perform mode validation and fixup (through 64 * drm_bridge_chain_mode_valid() and drm_atomic_bridge_chain_check()), mode 65 * setting (through drm_bridge_chain_mode_set()), enable (through 66 * drm_atomic_bridge_chain_pre_enable() and drm_atomic_bridge_chain_enable()) 67 * and disable (through drm_atomic_bridge_chain_disable() and 68 * drm_atomic_bridge_chain_post_disable()). Those functions call the 69 * corresponding operations provided in &drm_bridge_funcs in sequence for all 70 * bridges in the chain. 71 * 72 * For display drivers that use the atomic helpers 73 * drm_atomic_helper_check_modeset(), 74 * drm_atomic_helper_commit_modeset_enables() and 75 * drm_atomic_helper_commit_modeset_disables() (either directly in hand-rolled 76 * commit check and commit tail handlers, or through the higher-level 77 * drm_atomic_helper_check() and drm_atomic_helper_commit_tail() or 78 * drm_atomic_helper_commit_tail_rpm() helpers), this is done transparently and 79 * requires no intervention from the driver. For other drivers, the relevant 80 * DRM bridge chain functions shall be called manually. 81 * 82 * Bridges also participate in implementing the &drm_connector at the end of 83 * the bridge chain. Display drivers may use the drm_bridge_connector_init() 84 * helper to create the &drm_connector, or implement it manually on top of the 85 * connector-related operations exposed by the bridge (see the overview 86 * documentation of bridge operations for more details). 87 * 88 * &drm_bridge, like &drm_panel, aren't &drm_mode_object entities like planes, 89 * CRTCs, encoders or connectors and hence are not visible to userspace. They 90 * just provide additional hooks to get the desired output at the end of the 91 * encoder chain. 92 */ 93 94 static DEFINE_MUTEX(bridge_lock); 95 static LIST_HEAD(bridge_list); 96 97 /** 98 * drm_bridge_add - add the given bridge to the global bridge list 99 * 100 * @bridge: bridge control structure 101 */ 102 void drm_bridge_add(struct drm_bridge *bridge) 103 { 104 mutex_init(&bridge->hpd_mutex); 105 106 mutex_lock(&bridge_lock); 107 list_add_tail(&bridge->list, &bridge_list); 108 mutex_unlock(&bridge_lock); 109 } 110 EXPORT_SYMBOL(drm_bridge_add); 111 112 /** 113 * drm_bridge_remove - remove the given bridge from the global bridge list 114 * 115 * @bridge: bridge control structure 116 */ 117 void drm_bridge_remove(struct drm_bridge *bridge) 118 { 119 mutex_lock(&bridge_lock); 120 list_del_init(&bridge->list); 121 mutex_unlock(&bridge_lock); 122 123 mutex_destroy(&bridge->hpd_mutex); 124 } 125 EXPORT_SYMBOL(drm_bridge_remove); 126 127 static struct drm_private_state * 128 drm_bridge_atomic_duplicate_priv_state(struct drm_private_obj *obj) 129 { 130 struct drm_bridge *bridge = drm_priv_to_bridge(obj); 131 struct drm_bridge_state *state; 132 133 state = bridge->funcs->atomic_duplicate_state(bridge); 134 return state ? &state->base : NULL; 135 } 136 137 static void 138 drm_bridge_atomic_destroy_priv_state(struct drm_private_obj *obj, 139 struct drm_private_state *s) 140 { 141 struct drm_bridge_state *state = drm_priv_to_bridge_state(s); 142 struct drm_bridge *bridge = drm_priv_to_bridge(obj); 143 144 bridge->funcs->atomic_destroy_state(bridge, state); 145 } 146 147 static const struct drm_private_state_funcs drm_bridge_priv_state_funcs = { 148 .atomic_duplicate_state = drm_bridge_atomic_duplicate_priv_state, 149 .atomic_destroy_state = drm_bridge_atomic_destroy_priv_state, 150 }; 151 152 /** 153 * drm_bridge_attach - attach the bridge to an encoder's chain 154 * 155 * @encoder: DRM encoder 156 * @bridge: bridge to attach 157 * @previous: previous bridge in the chain (optional) 158 * @flags: DRM_BRIDGE_ATTACH_* flags 159 * 160 * Called by a kms driver to link the bridge to an encoder's chain. The previous 161 * argument specifies the previous bridge in the chain. If NULL, the bridge is 162 * linked directly at the encoder's output. Otherwise it is linked at the 163 * previous bridge's output. 164 * 165 * If non-NULL the previous bridge must be already attached by a call to this 166 * function. 167 * 168 * Note that bridges attached to encoders are auto-detached during encoder 169 * cleanup in drm_encoder_cleanup(), so drm_bridge_attach() should generally 170 * *not* be balanced with a drm_bridge_detach() in driver code. 171 * 172 * RETURNS: 173 * Zero on success, error code on failure 174 */ 175 int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge, 176 struct drm_bridge *previous, 177 enum drm_bridge_attach_flags flags) 178 { 179 int ret; 180 181 if (!encoder || !bridge) 182 return -EINVAL; 183 184 if (previous && (!previous->dev || previous->encoder != encoder)) 185 return -EINVAL; 186 187 if (bridge->dev) 188 return -EBUSY; 189 190 bridge->dev = encoder->dev; 191 bridge->encoder = encoder; 192 193 if (previous) 194 list_add(&bridge->chain_node, &previous->chain_node); 195 else 196 list_add(&bridge->chain_node, &encoder->bridge_chain); 197 198 if (bridge->funcs->attach) { 199 ret = bridge->funcs->attach(bridge, flags); 200 if (ret < 0) 201 goto err_reset_bridge; 202 } 203 204 if (bridge->funcs->atomic_reset) { 205 struct drm_bridge_state *state; 206 207 state = bridge->funcs->atomic_reset(bridge); 208 if (IS_ERR(state)) { 209 ret = PTR_ERR(state); 210 goto err_detach_bridge; 211 } 212 213 drm_atomic_private_obj_init(bridge->dev, &bridge->base, 214 &state->base, 215 &drm_bridge_priv_state_funcs); 216 } 217 218 return 0; 219 220 err_detach_bridge: 221 if (bridge->funcs->detach) 222 bridge->funcs->detach(bridge); 223 224 err_reset_bridge: 225 bridge->dev = NULL; 226 bridge->encoder = NULL; 227 list_del(&bridge->chain_node); 228 return ret; 229 } 230 EXPORT_SYMBOL(drm_bridge_attach); 231 232 void drm_bridge_detach(struct drm_bridge *bridge) 233 { 234 if (WARN_ON(!bridge)) 235 return; 236 237 if (WARN_ON(!bridge->dev)) 238 return; 239 240 if (bridge->funcs->atomic_reset) 241 drm_atomic_private_obj_fini(&bridge->base); 242 243 if (bridge->funcs->detach) 244 bridge->funcs->detach(bridge); 245 246 list_del(&bridge->chain_node); 247 bridge->dev = NULL; 248 } 249 250 /** 251 * DOC: bridge operations 252 * 253 * Bridge drivers expose operations through the &drm_bridge_funcs structure. 254 * The DRM internals (atomic and CRTC helpers) use the helpers defined in 255 * drm_bridge.c to call bridge operations. Those operations are divided in 256 * three big categories to support different parts of the bridge usage. 257 * 258 * - The encoder-related operations support control of the bridges in the 259 * chain, and are roughly counterparts to the &drm_encoder_helper_funcs 260 * operations. They are used by the legacy CRTC and the atomic modeset 261 * helpers to perform mode validation, fixup and setting, and enable and 262 * disable the bridge automatically. 263 * 264 * The enable and disable operations are split in 265 * &drm_bridge_funcs.pre_enable, &drm_bridge_funcs.enable, 266 * &drm_bridge_funcs.disable and &drm_bridge_funcs.post_disable to provide 267 * finer-grained control. 268 * 269 * Bridge drivers may implement the legacy version of those operations, or 270 * the atomic version (prefixed with atomic\_), in which case they shall also 271 * implement the atomic state bookkeeping operations 272 * (&drm_bridge_funcs.atomic_duplicate_state, 273 * &drm_bridge_funcs.atomic_destroy_state and &drm_bridge_funcs.reset). 274 * Mixing atomic and non-atomic versions of the operations is not supported. 275 * 276 * - The bus format negotiation operations 277 * &drm_bridge_funcs.atomic_get_output_bus_fmts and 278 * &drm_bridge_funcs.atomic_get_input_bus_fmts allow bridge drivers to 279 * negotiate the formats transmitted between bridges in the chain when 280 * multiple formats are supported. Negotiation for formats is performed 281 * transparently for display drivers by the atomic modeset helpers. Only 282 * atomic versions of those operations exist, bridge drivers that need to 283 * implement them shall thus also implement the atomic version of the 284 * encoder-related operations. This feature is not supported by the legacy 285 * CRTC helpers. 286 * 287 * - The connector-related operations support implementing a &drm_connector 288 * based on a chain of bridges. DRM bridges traditionally create a 289 * &drm_connector for bridges meant to be used at the end of the chain. This 290 * puts additional burden on bridge drivers, especially for bridges that may 291 * be used in the middle of a chain or at the end of it. Furthermore, it 292 * requires all operations of the &drm_connector to be handled by a single 293 * bridge, which doesn't always match the hardware architecture. 294 * 295 * To simplify bridge drivers and make the connector implementation more 296 * flexible, a new model allows bridges to unconditionally skip creation of 297 * &drm_connector and instead expose &drm_bridge_funcs operations to support 298 * an externally-implemented &drm_connector. Those operations are 299 * &drm_bridge_funcs.detect, &drm_bridge_funcs.get_modes, 300 * &drm_bridge_funcs.get_edid, &drm_bridge_funcs.hpd_notify, 301 * &drm_bridge_funcs.hpd_enable and &drm_bridge_funcs.hpd_disable. When 302 * implemented, display drivers shall create a &drm_connector instance for 303 * each chain of bridges, and implement those connector instances based on 304 * the bridge connector operations. 305 * 306 * Bridge drivers shall implement the connector-related operations for all 307 * the features that the bridge hardware support. For instance, if a bridge 308 * supports reading EDID, the &drm_bridge_funcs.get_edid shall be 309 * implemented. This however doesn't mean that the DDC lines are wired to the 310 * bridge on a particular platform, as they could also be connected to an I2C 311 * controller of the SoC. Support for the connector-related operations on the 312 * running platform is reported through the &drm_bridge.ops flags. Bridge 313 * drivers shall detect which operations they can support on the platform 314 * (usually this information is provided by ACPI or DT), and set the 315 * &drm_bridge.ops flags for all supported operations. A flag shall only be 316 * set if the corresponding &drm_bridge_funcs operation is implemented, but 317 * an implemented operation doesn't necessarily imply that the corresponding 318 * flag will be set. Display drivers shall use the &drm_bridge.ops flags to 319 * decide which bridge to delegate a connector operation to. This mechanism 320 * allows providing a single static const &drm_bridge_funcs instance in 321 * bridge drivers, improving security by storing function pointers in 322 * read-only memory. 323 * 324 * In order to ease transition, bridge drivers may support both the old and 325 * new models by making connector creation optional and implementing the 326 * connected-related bridge operations. Connector creation is then controlled 327 * by the flags argument to the drm_bridge_attach() function. Display drivers 328 * that support the new model and create connectors themselves shall set the 329 * %DRM_BRIDGE_ATTACH_NO_CONNECTOR flag, and bridge drivers shall then skip 330 * connector creation. For intermediate bridges in the chain, the flag shall 331 * be passed to the drm_bridge_attach() call for the downstream bridge. 332 * Bridge drivers that implement the new model only shall return an error 333 * from their &drm_bridge_funcs.attach handler when the 334 * %DRM_BRIDGE_ATTACH_NO_CONNECTOR flag is not set. New display drivers 335 * should use the new model, and convert the bridge drivers they use if 336 * needed, in order to gradually transition to the new model. 337 */ 338 339 /** 340 * drm_bridge_chain_mode_fixup - fixup proposed mode for all bridges in the 341 * encoder chain 342 * @bridge: bridge control structure 343 * @mode: desired mode to be set for the bridge 344 * @adjusted_mode: updated mode that works for this bridge 345 * 346 * Calls &drm_bridge_funcs.mode_fixup for all the bridges in the 347 * encoder chain, starting from the first bridge to the last. 348 * 349 * Note: the bridge passed should be the one closest to the encoder 350 * 351 * RETURNS: 352 * true on success, false on failure 353 */ 354 bool drm_bridge_chain_mode_fixup(struct drm_bridge *bridge, 355 const struct drm_display_mode *mode, 356 struct drm_display_mode *adjusted_mode) 357 { 358 struct drm_encoder *encoder; 359 360 if (!bridge) 361 return true; 362 363 encoder = bridge->encoder; 364 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) { 365 if (!bridge->funcs->mode_fixup) 366 continue; 367 368 if (!bridge->funcs->mode_fixup(bridge, mode, adjusted_mode)) 369 return false; 370 } 371 372 return true; 373 } 374 EXPORT_SYMBOL(drm_bridge_chain_mode_fixup); 375 376 /** 377 * drm_bridge_chain_mode_valid - validate the mode against all bridges in the 378 * encoder chain. 379 * @bridge: bridge control structure 380 * @mode: desired mode to be validated 381 * 382 * Calls &drm_bridge_funcs.mode_valid for all the bridges in the encoder 383 * chain, starting from the first bridge to the last. If at least one bridge 384 * does not accept the mode the function returns the error code. 385 * 386 * Note: the bridge passed should be the one closest to the encoder. 387 * 388 * RETURNS: 389 * MODE_OK on success, drm_mode_status Enum error code on failure 390 */ 391 enum drm_mode_status 392 drm_bridge_chain_mode_valid(struct drm_bridge *bridge, 393 const struct drm_display_mode *mode) 394 { 395 struct drm_encoder *encoder; 396 397 if (!bridge) 398 return MODE_OK; 399 400 encoder = bridge->encoder; 401 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) { 402 enum drm_mode_status ret; 403 404 if (!bridge->funcs->mode_valid) 405 continue; 406 407 ret = bridge->funcs->mode_valid(bridge, mode); 408 if (ret != MODE_OK) 409 return ret; 410 } 411 412 return MODE_OK; 413 } 414 EXPORT_SYMBOL(drm_bridge_chain_mode_valid); 415 416 /** 417 * drm_bridge_chain_disable - disables all bridges in the encoder chain 418 * @bridge: bridge control structure 419 * 420 * Calls &drm_bridge_funcs.disable op for all the bridges in the encoder 421 * chain, starting from the last bridge to the first. These are called before 422 * calling the encoder's prepare op. 423 * 424 * Note: the bridge passed should be the one closest to the encoder 425 */ 426 void drm_bridge_chain_disable(struct drm_bridge *bridge) 427 { 428 struct drm_encoder *encoder; 429 struct drm_bridge *iter; 430 431 if (!bridge) 432 return; 433 434 encoder = bridge->encoder; 435 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) { 436 if (iter->funcs->disable) 437 iter->funcs->disable(iter); 438 439 if (iter == bridge) 440 break; 441 } 442 } 443 EXPORT_SYMBOL(drm_bridge_chain_disable); 444 445 /** 446 * drm_bridge_chain_post_disable - cleans up after disabling all bridges in the 447 * encoder chain 448 * @bridge: bridge control structure 449 * 450 * Calls &drm_bridge_funcs.post_disable op for all the bridges in the 451 * encoder chain, starting from the first bridge to the last. These are called 452 * after completing the encoder's prepare op. 453 * 454 * Note: the bridge passed should be the one closest to the encoder 455 */ 456 void drm_bridge_chain_post_disable(struct drm_bridge *bridge) 457 { 458 struct drm_encoder *encoder; 459 460 if (!bridge) 461 return; 462 463 encoder = bridge->encoder; 464 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) { 465 if (bridge->funcs->post_disable) 466 bridge->funcs->post_disable(bridge); 467 } 468 } 469 EXPORT_SYMBOL(drm_bridge_chain_post_disable); 470 471 /** 472 * drm_bridge_chain_mode_set - set proposed mode for all bridges in the 473 * encoder chain 474 * @bridge: bridge control structure 475 * @mode: desired mode to be set for the encoder chain 476 * @adjusted_mode: updated mode that works for this encoder chain 477 * 478 * Calls &drm_bridge_funcs.mode_set op for all the bridges in the 479 * encoder chain, starting from the first bridge to the last. 480 * 481 * Note: the bridge passed should be the one closest to the encoder 482 */ 483 void drm_bridge_chain_mode_set(struct drm_bridge *bridge, 484 const struct drm_display_mode *mode, 485 const struct drm_display_mode *adjusted_mode) 486 { 487 struct drm_encoder *encoder; 488 489 if (!bridge) 490 return; 491 492 encoder = bridge->encoder; 493 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) { 494 if (bridge->funcs->mode_set) 495 bridge->funcs->mode_set(bridge, mode, adjusted_mode); 496 } 497 } 498 EXPORT_SYMBOL(drm_bridge_chain_mode_set); 499 500 /** 501 * drm_bridge_chain_pre_enable - prepares for enabling all bridges in the 502 * encoder chain 503 * @bridge: bridge control structure 504 * 505 * Calls &drm_bridge_funcs.pre_enable op for all the bridges in the encoder 506 * chain, starting from the last bridge to the first. These are called 507 * before calling the encoder's commit op. 508 * 509 * Note: the bridge passed should be the one closest to the encoder 510 */ 511 void drm_bridge_chain_pre_enable(struct drm_bridge *bridge) 512 { 513 struct drm_encoder *encoder; 514 struct drm_bridge *iter; 515 516 if (!bridge) 517 return; 518 519 encoder = bridge->encoder; 520 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) { 521 if (iter->funcs->pre_enable) 522 iter->funcs->pre_enable(iter); 523 } 524 } 525 EXPORT_SYMBOL(drm_bridge_chain_pre_enable); 526 527 /** 528 * drm_bridge_chain_enable - enables all bridges in the encoder chain 529 * @bridge: bridge control structure 530 * 531 * Calls &drm_bridge_funcs.enable op for all the bridges in the encoder 532 * chain, starting from the first bridge to the last. These are called 533 * after completing the encoder's commit op. 534 * 535 * Note that the bridge passed should be the one closest to the encoder 536 */ 537 void drm_bridge_chain_enable(struct drm_bridge *bridge) 538 { 539 struct drm_encoder *encoder; 540 541 if (!bridge) 542 return; 543 544 encoder = bridge->encoder; 545 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) { 546 if (bridge->funcs->enable) 547 bridge->funcs->enable(bridge); 548 } 549 } 550 EXPORT_SYMBOL(drm_bridge_chain_enable); 551 552 /** 553 * drm_atomic_bridge_chain_disable - disables all bridges in the encoder chain 554 * @bridge: bridge control structure 555 * @old_state: old atomic state 556 * 557 * Calls &drm_bridge_funcs.atomic_disable (falls back on 558 * &drm_bridge_funcs.disable) op for all the bridges in the encoder chain, 559 * starting from the last bridge to the first. These are called before calling 560 * &drm_encoder_helper_funcs.atomic_disable 561 * 562 * Note: the bridge passed should be the one closest to the encoder 563 */ 564 void drm_atomic_bridge_chain_disable(struct drm_bridge *bridge, 565 struct drm_atomic_state *old_state) 566 { 567 struct drm_encoder *encoder; 568 struct drm_bridge *iter; 569 570 if (!bridge) 571 return; 572 573 encoder = bridge->encoder; 574 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) { 575 if (iter->funcs->atomic_disable) { 576 struct drm_bridge_state *old_bridge_state; 577 578 old_bridge_state = 579 drm_atomic_get_old_bridge_state(old_state, 580 iter); 581 if (WARN_ON(!old_bridge_state)) 582 return; 583 584 iter->funcs->atomic_disable(iter, old_bridge_state); 585 } else if (iter->funcs->disable) { 586 iter->funcs->disable(iter); 587 } 588 589 if (iter == bridge) 590 break; 591 } 592 } 593 EXPORT_SYMBOL(drm_atomic_bridge_chain_disable); 594 595 /** 596 * drm_atomic_bridge_chain_post_disable - cleans up after disabling all bridges 597 * in the encoder chain 598 * @bridge: bridge control structure 599 * @old_state: old atomic state 600 * 601 * Calls &drm_bridge_funcs.atomic_post_disable (falls back on 602 * &drm_bridge_funcs.post_disable) op for all the bridges in the encoder chain, 603 * starting from the first bridge to the last. These are called after completing 604 * &drm_encoder_helper_funcs.atomic_disable 605 * 606 * Note: the bridge passed should be the one closest to the encoder 607 */ 608 void drm_atomic_bridge_chain_post_disable(struct drm_bridge *bridge, 609 struct drm_atomic_state *old_state) 610 { 611 struct drm_encoder *encoder; 612 613 if (!bridge) 614 return; 615 616 encoder = bridge->encoder; 617 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) { 618 if (bridge->funcs->atomic_post_disable) { 619 struct drm_bridge_state *old_bridge_state; 620 621 old_bridge_state = 622 drm_atomic_get_old_bridge_state(old_state, 623 bridge); 624 if (WARN_ON(!old_bridge_state)) 625 return; 626 627 bridge->funcs->atomic_post_disable(bridge, 628 old_bridge_state); 629 } else if (bridge->funcs->post_disable) { 630 bridge->funcs->post_disable(bridge); 631 } 632 } 633 } 634 EXPORT_SYMBOL(drm_atomic_bridge_chain_post_disable); 635 636 /** 637 * drm_atomic_bridge_chain_pre_enable - prepares for enabling all bridges in 638 * the encoder chain 639 * @bridge: bridge control structure 640 * @old_state: old atomic state 641 * 642 * Calls &drm_bridge_funcs.atomic_pre_enable (falls back on 643 * &drm_bridge_funcs.pre_enable) op for all the bridges in the encoder chain, 644 * starting from the last bridge to the first. These are called before calling 645 * &drm_encoder_helper_funcs.atomic_enable 646 * 647 * Note: the bridge passed should be the one closest to the encoder 648 */ 649 void drm_atomic_bridge_chain_pre_enable(struct drm_bridge *bridge, 650 struct drm_atomic_state *old_state) 651 { 652 struct drm_encoder *encoder; 653 struct drm_bridge *iter; 654 655 if (!bridge) 656 return; 657 658 encoder = bridge->encoder; 659 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) { 660 if (iter->funcs->atomic_pre_enable) { 661 struct drm_bridge_state *old_bridge_state; 662 663 old_bridge_state = 664 drm_atomic_get_old_bridge_state(old_state, 665 iter); 666 if (WARN_ON(!old_bridge_state)) 667 return; 668 669 iter->funcs->atomic_pre_enable(iter, old_bridge_state); 670 } else if (iter->funcs->pre_enable) { 671 iter->funcs->pre_enable(iter); 672 } 673 674 if (iter == bridge) 675 break; 676 } 677 } 678 EXPORT_SYMBOL(drm_atomic_bridge_chain_pre_enable); 679 680 /** 681 * drm_atomic_bridge_chain_enable - enables all bridges in the encoder chain 682 * @bridge: bridge control structure 683 * @old_state: old atomic state 684 * 685 * Calls &drm_bridge_funcs.atomic_enable (falls back on 686 * &drm_bridge_funcs.enable) op for all the bridges in the encoder chain, 687 * starting from the first bridge to the last. These are called after completing 688 * &drm_encoder_helper_funcs.atomic_enable 689 * 690 * Note: the bridge passed should be the one closest to the encoder 691 */ 692 void drm_atomic_bridge_chain_enable(struct drm_bridge *bridge, 693 struct drm_atomic_state *old_state) 694 { 695 struct drm_encoder *encoder; 696 697 if (!bridge) 698 return; 699 700 encoder = bridge->encoder; 701 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) { 702 if (bridge->funcs->atomic_enable) { 703 struct drm_bridge_state *old_bridge_state; 704 705 old_bridge_state = 706 drm_atomic_get_old_bridge_state(old_state, 707 bridge); 708 if (WARN_ON(!old_bridge_state)) 709 return; 710 711 bridge->funcs->atomic_enable(bridge, old_bridge_state); 712 } else if (bridge->funcs->enable) { 713 bridge->funcs->enable(bridge); 714 } 715 } 716 } 717 EXPORT_SYMBOL(drm_atomic_bridge_chain_enable); 718 719 static int drm_atomic_bridge_check(struct drm_bridge *bridge, 720 struct drm_crtc_state *crtc_state, 721 struct drm_connector_state *conn_state) 722 { 723 if (bridge->funcs->atomic_check) { 724 struct drm_bridge_state *bridge_state; 725 int ret; 726 727 bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state, 728 bridge); 729 if (WARN_ON(!bridge_state)) 730 return -EINVAL; 731 732 ret = bridge->funcs->atomic_check(bridge, bridge_state, 733 crtc_state, conn_state); 734 if (ret) 735 return ret; 736 } else if (bridge->funcs->mode_fixup) { 737 if (!bridge->funcs->mode_fixup(bridge, &crtc_state->mode, 738 &crtc_state->adjusted_mode)) 739 return -EINVAL; 740 } 741 742 return 0; 743 } 744 745 static int select_bus_fmt_recursive(struct drm_bridge *first_bridge, 746 struct drm_bridge *cur_bridge, 747 struct drm_crtc_state *crtc_state, 748 struct drm_connector_state *conn_state, 749 u32 out_bus_fmt) 750 { 751 struct drm_bridge_state *cur_state; 752 unsigned int num_in_bus_fmts, i; 753 struct drm_bridge *prev_bridge; 754 u32 *in_bus_fmts; 755 int ret; 756 757 prev_bridge = drm_bridge_get_prev_bridge(cur_bridge); 758 cur_state = drm_atomic_get_new_bridge_state(crtc_state->state, 759 cur_bridge); 760 761 /* 762 * If bus format negotiation is not supported by this bridge, let's 763 * pass MEDIA_BUS_FMT_FIXED to the previous bridge in the chain and 764 * hope that it can handle this situation gracefully (by providing 765 * appropriate default values). 766 */ 767 if (!cur_bridge->funcs->atomic_get_input_bus_fmts) { 768 if (cur_bridge != first_bridge) { 769 ret = select_bus_fmt_recursive(first_bridge, 770 prev_bridge, crtc_state, 771 conn_state, 772 MEDIA_BUS_FMT_FIXED); 773 if (ret) 774 return ret; 775 } 776 777 /* 778 * Driver does not implement the atomic state hooks, but that's 779 * fine, as long as it does not access the bridge state. 780 */ 781 if (cur_state) { 782 cur_state->input_bus_cfg.format = MEDIA_BUS_FMT_FIXED; 783 cur_state->output_bus_cfg.format = out_bus_fmt; 784 } 785 786 return 0; 787 } 788 789 /* 790 * If the driver implements ->atomic_get_input_bus_fmts() it 791 * should also implement the atomic state hooks. 792 */ 793 if (WARN_ON(!cur_state)) 794 return -EINVAL; 795 796 in_bus_fmts = cur_bridge->funcs->atomic_get_input_bus_fmts(cur_bridge, 797 cur_state, 798 crtc_state, 799 conn_state, 800 out_bus_fmt, 801 &num_in_bus_fmts); 802 if (!num_in_bus_fmts) 803 return -ENOTSUPP; 804 else if (!in_bus_fmts) 805 return -ENOMEM; 806 807 if (first_bridge == cur_bridge) { 808 cur_state->input_bus_cfg.format = in_bus_fmts[0]; 809 cur_state->output_bus_cfg.format = out_bus_fmt; 810 kfree(in_bus_fmts); 811 return 0; 812 } 813 814 for (i = 0; i < num_in_bus_fmts; i++) { 815 ret = select_bus_fmt_recursive(first_bridge, prev_bridge, 816 crtc_state, conn_state, 817 in_bus_fmts[i]); 818 if (ret != -ENOTSUPP) 819 break; 820 } 821 822 if (!ret) { 823 cur_state->input_bus_cfg.format = in_bus_fmts[i]; 824 cur_state->output_bus_cfg.format = out_bus_fmt; 825 } 826 827 kfree(in_bus_fmts); 828 return ret; 829 } 830 831 /* 832 * This function is called by &drm_atomic_bridge_chain_check() just before 833 * calling &drm_bridge_funcs.atomic_check() on all elements of the chain. 834 * It performs bus format negotiation between bridge elements. The negotiation 835 * happens in reverse order, starting from the last element in the chain up to 836 * @bridge. 837 * 838 * Negotiation starts by retrieving supported output bus formats on the last 839 * bridge element and testing them one by one. The test is recursive, meaning 840 * that for each tested output format, the whole chain will be walked backward, 841 * and each element will have to choose an input bus format that can be 842 * transcoded to the requested output format. When a bridge element does not 843 * support transcoding into a specific output format -ENOTSUPP is returned and 844 * the next bridge element will have to try a different format. If none of the 845 * combinations worked, -ENOTSUPP is returned and the atomic modeset will fail. 846 * 847 * This implementation is relying on 848 * &drm_bridge_funcs.atomic_get_output_bus_fmts() and 849 * &drm_bridge_funcs.atomic_get_input_bus_fmts() to gather supported 850 * input/output formats. 851 * 852 * When &drm_bridge_funcs.atomic_get_output_bus_fmts() is not implemented by 853 * the last element of the chain, &drm_atomic_bridge_chain_select_bus_fmts() 854 * tries a single format: &drm_connector.display_info.bus_formats[0] if 855 * available, MEDIA_BUS_FMT_FIXED otherwise. 856 * 857 * When &drm_bridge_funcs.atomic_get_input_bus_fmts() is not implemented, 858 * &drm_atomic_bridge_chain_select_bus_fmts() skips the negotiation on the 859 * bridge element that lacks this hook and asks the previous element in the 860 * chain to try MEDIA_BUS_FMT_FIXED. It's up to bridge drivers to decide what 861 * to do in that case (fail if they want to enforce bus format negotiation, or 862 * provide a reasonable default if they need to support pipelines where not 863 * all elements support bus format negotiation). 864 */ 865 static int 866 drm_atomic_bridge_chain_select_bus_fmts(struct drm_bridge *bridge, 867 struct drm_crtc_state *crtc_state, 868 struct drm_connector_state *conn_state) 869 { 870 struct drm_connector *conn = conn_state->connector; 871 struct drm_encoder *encoder = bridge->encoder; 872 struct drm_bridge_state *last_bridge_state; 873 unsigned int i, num_out_bus_fmts; 874 struct drm_bridge *last_bridge; 875 u32 *out_bus_fmts; 876 int ret = 0; 877 878 last_bridge = list_last_entry(&encoder->bridge_chain, 879 struct drm_bridge, chain_node); 880 last_bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state, 881 last_bridge); 882 883 if (last_bridge->funcs->atomic_get_output_bus_fmts) { 884 const struct drm_bridge_funcs *funcs = last_bridge->funcs; 885 886 /* 887 * If the driver implements ->atomic_get_output_bus_fmts() it 888 * should also implement the atomic state hooks. 889 */ 890 if (WARN_ON(!last_bridge_state)) 891 return -EINVAL; 892 893 out_bus_fmts = funcs->atomic_get_output_bus_fmts(last_bridge, 894 last_bridge_state, 895 crtc_state, 896 conn_state, 897 &num_out_bus_fmts); 898 if (!num_out_bus_fmts) 899 return -ENOTSUPP; 900 else if (!out_bus_fmts) 901 return -ENOMEM; 902 } else { 903 num_out_bus_fmts = 1; 904 out_bus_fmts = kmalloc(sizeof(*out_bus_fmts), GFP_KERNEL); 905 if (!out_bus_fmts) 906 return -ENOMEM; 907 908 if (conn->display_info.num_bus_formats && 909 conn->display_info.bus_formats) 910 out_bus_fmts[0] = conn->display_info.bus_formats[0]; 911 else 912 out_bus_fmts[0] = MEDIA_BUS_FMT_FIXED; 913 } 914 915 for (i = 0; i < num_out_bus_fmts; i++) { 916 ret = select_bus_fmt_recursive(bridge, last_bridge, crtc_state, 917 conn_state, out_bus_fmts[i]); 918 if (ret != -ENOTSUPP) 919 break; 920 } 921 922 kfree(out_bus_fmts); 923 924 return ret; 925 } 926 927 static void 928 drm_atomic_bridge_propagate_bus_flags(struct drm_bridge *bridge, 929 struct drm_connector *conn, 930 struct drm_atomic_state *state) 931 { 932 struct drm_bridge_state *bridge_state, *next_bridge_state; 933 struct drm_bridge *next_bridge; 934 u32 output_flags = 0; 935 936 bridge_state = drm_atomic_get_new_bridge_state(state, bridge); 937 938 /* No bridge state attached to this bridge => nothing to propagate. */ 939 if (!bridge_state) 940 return; 941 942 next_bridge = drm_bridge_get_next_bridge(bridge); 943 944 /* 945 * Let's try to apply the most common case here, that is, propagate 946 * display_info flags for the last bridge, and propagate the input 947 * flags of the next bridge element to the output end of the current 948 * bridge when the bridge is not the last one. 949 * There are exceptions to this rule, like when signal inversion is 950 * happening at the board level, but that's something drivers can deal 951 * with from their &drm_bridge_funcs.atomic_check() implementation by 952 * simply overriding the flags value we've set here. 953 */ 954 if (!next_bridge) { 955 output_flags = conn->display_info.bus_flags; 956 } else { 957 next_bridge_state = drm_atomic_get_new_bridge_state(state, 958 next_bridge); 959 /* 960 * No bridge state attached to the next bridge, just leave the 961 * flags to 0. 962 */ 963 if (next_bridge_state) 964 output_flags = next_bridge_state->input_bus_cfg.flags; 965 } 966 967 bridge_state->output_bus_cfg.flags = output_flags; 968 969 /* 970 * Propage the output flags to the input end of the bridge. Again, it's 971 * not necessarily what all bridges want, but that's what most of them 972 * do, and by doing that by default we avoid forcing drivers to 973 * duplicate the "dummy propagation" logic. 974 */ 975 bridge_state->input_bus_cfg.flags = output_flags; 976 } 977 978 /** 979 * drm_atomic_bridge_chain_check() - Do an atomic check on the bridge chain 980 * @bridge: bridge control structure 981 * @crtc_state: new CRTC state 982 * @conn_state: new connector state 983 * 984 * First trigger a bus format negotiation before calling 985 * &drm_bridge_funcs.atomic_check() (falls back on 986 * &drm_bridge_funcs.mode_fixup()) op for all the bridges in the encoder chain, 987 * starting from the last bridge to the first. These are called before calling 988 * &drm_encoder_helper_funcs.atomic_check() 989 * 990 * RETURNS: 991 * 0 on success, a negative error code on failure 992 */ 993 int drm_atomic_bridge_chain_check(struct drm_bridge *bridge, 994 struct drm_crtc_state *crtc_state, 995 struct drm_connector_state *conn_state) 996 { 997 struct drm_connector *conn = conn_state->connector; 998 struct drm_encoder *encoder; 999 struct drm_bridge *iter; 1000 int ret; 1001 1002 if (!bridge) 1003 return 0; 1004 1005 ret = drm_atomic_bridge_chain_select_bus_fmts(bridge, crtc_state, 1006 conn_state); 1007 if (ret) 1008 return ret; 1009 1010 encoder = bridge->encoder; 1011 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) { 1012 int ret; 1013 1014 /* 1015 * Bus flags are propagated by default. If a bridge needs to 1016 * tweak the input bus flags for any reason, it should happen 1017 * in its &drm_bridge_funcs.atomic_check() implementation such 1018 * that preceding bridges in the chain can propagate the new 1019 * bus flags. 1020 */ 1021 drm_atomic_bridge_propagate_bus_flags(iter, conn, 1022 crtc_state->state); 1023 1024 ret = drm_atomic_bridge_check(iter, crtc_state, conn_state); 1025 if (ret) 1026 return ret; 1027 1028 if (iter == bridge) 1029 break; 1030 } 1031 1032 return 0; 1033 } 1034 EXPORT_SYMBOL(drm_atomic_bridge_chain_check); 1035 1036 /** 1037 * drm_bridge_detect - check if anything is attached to the bridge output 1038 * @bridge: bridge control structure 1039 * 1040 * If the bridge supports output detection, as reported by the 1041 * DRM_BRIDGE_OP_DETECT bridge ops flag, call &drm_bridge_funcs.detect for the 1042 * bridge and return the connection status. Otherwise return 1043 * connector_status_unknown. 1044 * 1045 * RETURNS: 1046 * The detection status on success, or connector_status_unknown if the bridge 1047 * doesn't support output detection. 1048 */ 1049 enum drm_connector_status drm_bridge_detect(struct drm_bridge *bridge) 1050 { 1051 if (!(bridge->ops & DRM_BRIDGE_OP_DETECT)) 1052 return connector_status_unknown; 1053 1054 return bridge->funcs->detect(bridge); 1055 } 1056 EXPORT_SYMBOL_GPL(drm_bridge_detect); 1057 1058 /** 1059 * drm_bridge_get_modes - fill all modes currently valid for the sink into the 1060 * @connector 1061 * @bridge: bridge control structure 1062 * @connector: the connector to fill with modes 1063 * 1064 * If the bridge supports output modes retrieval, as reported by the 1065 * DRM_BRIDGE_OP_MODES bridge ops flag, call &drm_bridge_funcs.get_modes to 1066 * fill the connector with all valid modes and return the number of modes 1067 * added. Otherwise return 0. 1068 * 1069 * RETURNS: 1070 * The number of modes added to the connector. 1071 */ 1072 int drm_bridge_get_modes(struct drm_bridge *bridge, 1073 struct drm_connector *connector) 1074 { 1075 if (!(bridge->ops & DRM_BRIDGE_OP_MODES)) 1076 return 0; 1077 1078 return bridge->funcs->get_modes(bridge, connector); 1079 } 1080 EXPORT_SYMBOL_GPL(drm_bridge_get_modes); 1081 1082 /** 1083 * drm_bridge_get_edid - get the EDID data of the connected display 1084 * @bridge: bridge control structure 1085 * @connector: the connector to read EDID for 1086 * 1087 * If the bridge supports output EDID retrieval, as reported by the 1088 * DRM_BRIDGE_OP_EDID bridge ops flag, call &drm_bridge_funcs.get_edid to 1089 * get the EDID and return it. Otherwise return ERR_PTR(-ENOTSUPP). 1090 * 1091 * RETURNS: 1092 * The retrieved EDID on success, or an error pointer otherwise. 1093 */ 1094 struct edid *drm_bridge_get_edid(struct drm_bridge *bridge, 1095 struct drm_connector *connector) 1096 { 1097 if (!(bridge->ops & DRM_BRIDGE_OP_EDID)) 1098 return ERR_PTR(-ENOTSUPP); 1099 1100 return bridge->funcs->get_edid(bridge, connector); 1101 } 1102 EXPORT_SYMBOL_GPL(drm_bridge_get_edid); 1103 1104 /** 1105 * drm_bridge_hpd_enable - enable hot plug detection for the bridge 1106 * @bridge: bridge control structure 1107 * @cb: hot-plug detection callback 1108 * @data: data to be passed to the hot-plug detection callback 1109 * 1110 * Call &drm_bridge_funcs.hpd_enable if implemented and register the given @cb 1111 * and @data as hot plug notification callback. From now on the @cb will be 1112 * called with @data when an output status change is detected by the bridge, 1113 * until hot plug notification gets disabled with drm_bridge_hpd_disable(). 1114 * 1115 * Hot plug detection is supported only if the DRM_BRIDGE_OP_HPD flag is set in 1116 * bridge->ops. This function shall not be called when the flag is not set. 1117 * 1118 * Only one hot plug detection callback can be registered at a time, it is an 1119 * error to call this function when hot plug detection is already enabled for 1120 * the bridge. 1121 */ 1122 void drm_bridge_hpd_enable(struct drm_bridge *bridge, 1123 void (*cb)(void *data, 1124 enum drm_connector_status status), 1125 void *data) 1126 { 1127 if (!(bridge->ops & DRM_BRIDGE_OP_HPD)) 1128 return; 1129 1130 mutex_lock(&bridge->hpd_mutex); 1131 1132 if (WARN(bridge->hpd_cb, "Hot plug detection already enabled\n")) 1133 goto unlock; 1134 1135 bridge->hpd_cb = cb; 1136 bridge->hpd_data = data; 1137 1138 if (bridge->funcs->hpd_enable) 1139 bridge->funcs->hpd_enable(bridge); 1140 1141 unlock: 1142 mutex_unlock(&bridge->hpd_mutex); 1143 } 1144 EXPORT_SYMBOL_GPL(drm_bridge_hpd_enable); 1145 1146 /** 1147 * drm_bridge_hpd_disable - disable hot plug detection for the bridge 1148 * @bridge: bridge control structure 1149 * 1150 * Call &drm_bridge_funcs.hpd_disable if implemented and unregister the hot 1151 * plug detection callback previously registered with drm_bridge_hpd_enable(). 1152 * Once this function returns the callback will not be called by the bridge 1153 * when an output status change occurs. 1154 * 1155 * Hot plug detection is supported only if the DRM_BRIDGE_OP_HPD flag is set in 1156 * bridge->ops. This function shall not be called when the flag is not set. 1157 */ 1158 void drm_bridge_hpd_disable(struct drm_bridge *bridge) 1159 { 1160 if (!(bridge->ops & DRM_BRIDGE_OP_HPD)) 1161 return; 1162 1163 mutex_lock(&bridge->hpd_mutex); 1164 if (bridge->funcs->hpd_disable) 1165 bridge->funcs->hpd_disable(bridge); 1166 1167 bridge->hpd_cb = NULL; 1168 bridge->hpd_data = NULL; 1169 mutex_unlock(&bridge->hpd_mutex); 1170 } 1171 EXPORT_SYMBOL_GPL(drm_bridge_hpd_disable); 1172 1173 /** 1174 * drm_bridge_hpd_notify - notify hot plug detection events 1175 * @bridge: bridge control structure 1176 * @status: output connection status 1177 * 1178 * Bridge drivers shall call this function to report hot plug events when they 1179 * detect a change in the output status, when hot plug detection has been 1180 * enabled by drm_bridge_hpd_enable(). 1181 * 1182 * This function shall be called in a context that can sleep. 1183 */ 1184 void drm_bridge_hpd_notify(struct drm_bridge *bridge, 1185 enum drm_connector_status status) 1186 { 1187 mutex_lock(&bridge->hpd_mutex); 1188 if (bridge->hpd_cb) 1189 bridge->hpd_cb(bridge->hpd_data, status); 1190 mutex_unlock(&bridge->hpd_mutex); 1191 } 1192 EXPORT_SYMBOL_GPL(drm_bridge_hpd_notify); 1193 1194 #ifdef CONFIG_OF 1195 /** 1196 * of_drm_find_bridge - find the bridge corresponding to the device node in 1197 * the global bridge list 1198 * 1199 * @np: device node 1200 * 1201 * RETURNS: 1202 * drm_bridge control struct on success, NULL on failure 1203 */ 1204 struct drm_bridge *of_drm_find_bridge(struct device_node *np) 1205 { 1206 struct drm_bridge *bridge; 1207 1208 mutex_lock(&bridge_lock); 1209 1210 list_for_each_entry(bridge, &bridge_list, list) { 1211 if (bridge->of_node == np) { 1212 mutex_unlock(&bridge_lock); 1213 return bridge; 1214 } 1215 } 1216 1217 mutex_unlock(&bridge_lock); 1218 return NULL; 1219 } 1220 EXPORT_SYMBOL(of_drm_find_bridge); 1221 #endif 1222 1223 MODULE_AUTHOR("Ajay Kumar <ajaykumar.rs@samsung.com>"); 1224 MODULE_DESCRIPTION("DRM bridge infrastructure"); 1225 MODULE_LICENSE("GPL and additional rights"); 1226