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