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