1 /* 2 * Copyright (c) 2014 Samsung Electronics Co., Ltd 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sub license, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the 12 * next paragraph) shall be included in all copies or substantial portions 13 * of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21 * DEALINGS IN THE SOFTWARE. 22 */ 23 24 #include <linux/err.h> 25 #include <linux/module.h> 26 #include <linux/mutex.h> 27 28 #include <drm/drm_atomic_state_helper.h> 29 #include <drm/drm_bridge.h> 30 #include <drm/drm_encoder.h> 31 32 #include "drm_crtc_internal.h" 33 34 /** 35 * DOC: overview 36 * 37 * &struct drm_bridge represents a device that hangs on to an encoder. These are 38 * handy when a regular &drm_encoder entity isn't enough to represent the entire 39 * encoder chain. 40 * 41 * A bridge is always attached to a single &drm_encoder at a time, but can be 42 * either connected to it directly, or through an intermediate bridge:: 43 * 44 * encoder ---> bridge B ---> bridge A 45 * 46 * Here, the output of the encoder feeds to bridge B, and that furthers feeds to 47 * bridge A. 48 * 49 * The driver using the bridge is responsible to make the associations between 50 * the encoder and bridges. Once these links are made, the bridges will 51 * participate along with encoder functions to perform mode_set/enable/disable 52 * through the ops provided in &drm_bridge_funcs. 53 * 54 * drm_bridge, like drm_panel, aren't drm_mode_object entities like planes, 55 * CRTCs, encoders or connectors and hence are not visible to userspace. They 56 * just provide additional hooks to get the desired output at the end of the 57 * encoder chain. 58 * 59 * Bridges can also be chained up using the &drm_bridge.chain_node field. 60 * 61 * Both legacy CRTC helpers and the new atomic modeset helpers support bridges. 62 */ 63 64 static DEFINE_MUTEX(bridge_lock); 65 static LIST_HEAD(bridge_list); 66 67 /** 68 * drm_bridge_add - add the given bridge to the global bridge list 69 * 70 * @bridge: bridge control structure 71 */ 72 void drm_bridge_add(struct drm_bridge *bridge) 73 { 74 mutex_lock(&bridge_lock); 75 list_add_tail(&bridge->list, &bridge_list); 76 mutex_unlock(&bridge_lock); 77 } 78 EXPORT_SYMBOL(drm_bridge_add); 79 80 /** 81 * drm_bridge_remove - remove the given bridge from the global bridge list 82 * 83 * @bridge: bridge control structure 84 */ 85 void drm_bridge_remove(struct drm_bridge *bridge) 86 { 87 mutex_lock(&bridge_lock); 88 list_del_init(&bridge->list); 89 mutex_unlock(&bridge_lock); 90 } 91 EXPORT_SYMBOL(drm_bridge_remove); 92 93 static struct drm_bridge_state * 94 drm_atomic_default_bridge_duplicate_state(struct drm_bridge *bridge) 95 { 96 struct drm_bridge_state *new; 97 98 if (WARN_ON(!bridge->base.state)) 99 return NULL; 100 101 new = kzalloc(sizeof(*new), GFP_KERNEL); 102 if (new) 103 __drm_atomic_helper_bridge_duplicate_state(bridge, new); 104 105 return new; 106 } 107 108 static struct drm_private_state * 109 drm_bridge_atomic_duplicate_priv_state(struct drm_private_obj *obj) 110 { 111 struct drm_bridge *bridge = drm_priv_to_bridge(obj); 112 struct drm_bridge_state *state; 113 114 if (bridge->funcs->atomic_duplicate_state) 115 state = bridge->funcs->atomic_duplicate_state(bridge); 116 else 117 state = drm_atomic_default_bridge_duplicate_state(bridge); 118 119 return state ? &state->base : NULL; 120 } 121 122 static void 123 drm_atomic_default_bridge_destroy_state(struct drm_bridge *bridge, 124 struct drm_bridge_state *state) 125 { 126 /* Just a simple kfree() for now */ 127 kfree(state); 128 } 129 130 static void 131 drm_bridge_atomic_destroy_priv_state(struct drm_private_obj *obj, 132 struct drm_private_state *s) 133 { 134 struct drm_bridge_state *state = drm_priv_to_bridge_state(s); 135 struct drm_bridge *bridge = drm_priv_to_bridge(obj); 136 137 if (bridge->funcs->atomic_destroy_state) 138 bridge->funcs->atomic_destroy_state(bridge, state); 139 else 140 drm_atomic_default_bridge_destroy_state(bridge, state); 141 } 142 143 static const struct drm_private_state_funcs drm_bridge_priv_state_funcs = { 144 .atomic_duplicate_state = drm_bridge_atomic_duplicate_priv_state, 145 .atomic_destroy_state = drm_bridge_atomic_destroy_priv_state, 146 }; 147 148 static struct drm_bridge_state * 149 drm_atomic_default_bridge_reset(struct drm_bridge *bridge) 150 { 151 struct drm_bridge_state *bridge_state; 152 153 bridge_state = kzalloc(sizeof(*bridge_state), GFP_KERNEL); 154 if (!bridge_state) 155 return ERR_PTR(-ENOMEM); 156 157 __drm_atomic_helper_bridge_reset(bridge, bridge_state); 158 return bridge_state; 159 } 160 161 /** 162 * drm_bridge_attach - attach the bridge to an encoder's chain 163 * 164 * @encoder: DRM encoder 165 * @bridge: bridge to attach 166 * @previous: previous bridge in the chain (optional) 167 * 168 * Called by a kms driver to link the bridge to an encoder's chain. The previous 169 * argument specifies the previous bridge in the chain. If NULL, the bridge is 170 * linked directly at the encoder's output. Otherwise it is linked at the 171 * previous bridge's output. 172 * 173 * If non-NULL the previous bridge must be already attached by a call to this 174 * function. 175 * 176 * Note that bridges attached to encoders are auto-detached during encoder 177 * cleanup in drm_encoder_cleanup(), so drm_bridge_attach() should generally 178 * *not* be balanced with a drm_bridge_detach() in driver code. 179 * 180 * RETURNS: 181 * Zero on success, error code on failure 182 */ 183 int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge, 184 struct drm_bridge *previous) 185 { 186 struct drm_bridge_state *state; 187 int ret; 188 189 if (!encoder || !bridge) 190 return -EINVAL; 191 192 if (previous && (!previous->dev || previous->encoder != encoder)) 193 return -EINVAL; 194 195 if (bridge->dev) 196 return -EBUSY; 197 198 bridge->dev = encoder->dev; 199 bridge->encoder = encoder; 200 201 if (previous) 202 list_add(&bridge->chain_node, &previous->chain_node); 203 else 204 list_add(&bridge->chain_node, &encoder->bridge_chain); 205 206 if (bridge->funcs->attach) { 207 ret = bridge->funcs->attach(bridge); 208 if (ret < 0) 209 goto err_reset_bridge; 210 } 211 212 if (bridge->funcs->atomic_reset) 213 state = bridge->funcs->atomic_reset(bridge); 214 else 215 state = drm_atomic_default_bridge_reset(bridge); 216 217 if (IS_ERR(state)) { 218 ret = PTR_ERR(state); 219 goto err_detach_bridge; 220 } 221 222 drm_atomic_private_obj_init(bridge->dev, &bridge->base, 223 &state->base, 224 &drm_bridge_priv_state_funcs); 225 226 return 0; 227 228 err_detach_bridge: 229 if (bridge->funcs->detach) 230 bridge->funcs->detach(bridge); 231 232 err_reset_bridge: 233 bridge->dev = NULL; 234 bridge->encoder = NULL; 235 list_del(&bridge->chain_node); 236 return ret; 237 } 238 EXPORT_SYMBOL(drm_bridge_attach); 239 240 void drm_bridge_detach(struct drm_bridge *bridge) 241 { 242 if (WARN_ON(!bridge)) 243 return; 244 245 if (WARN_ON(!bridge->dev)) 246 return; 247 248 drm_atomic_private_obj_fini(&bridge->base); 249 250 if (bridge->funcs->detach) 251 bridge->funcs->detach(bridge); 252 253 list_del(&bridge->chain_node); 254 bridge->dev = NULL; 255 } 256 257 /** 258 * DOC: bridge callbacks 259 * 260 * The &drm_bridge_funcs ops are populated by the bridge driver. The DRM 261 * internals (atomic and CRTC helpers) use the helpers defined in drm_bridge.c 262 * These helpers call a specific &drm_bridge_funcs op for all the bridges 263 * during encoder configuration. 264 * 265 * For detailed specification of the bridge callbacks see &drm_bridge_funcs. 266 */ 267 268 /** 269 * drm_bridge_chain_mode_fixup - fixup proposed mode for all bridges in the 270 * encoder chain 271 * @bridge: bridge control structure 272 * @mode: desired mode to be set for the bridge 273 * @adjusted_mode: updated mode that works for this bridge 274 * 275 * Calls &drm_bridge_funcs.mode_fixup for all the bridges in the 276 * encoder chain, starting from the first bridge to the last. 277 * 278 * Note: the bridge passed should be the one closest to the encoder 279 * 280 * RETURNS: 281 * true on success, false on failure 282 */ 283 bool drm_bridge_chain_mode_fixup(struct drm_bridge *bridge, 284 const struct drm_display_mode *mode, 285 struct drm_display_mode *adjusted_mode) 286 { 287 struct drm_encoder *encoder; 288 289 if (!bridge) 290 return true; 291 292 encoder = bridge->encoder; 293 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) { 294 if (!bridge->funcs->mode_fixup) 295 continue; 296 297 if (!bridge->funcs->mode_fixup(bridge, mode, adjusted_mode)) 298 return false; 299 } 300 301 return true; 302 } 303 EXPORT_SYMBOL(drm_bridge_chain_mode_fixup); 304 305 /** 306 * drm_bridge_chain_mode_valid - validate the mode against all bridges in the 307 * encoder chain. 308 * @bridge: bridge control structure 309 * @mode: desired mode to be validated 310 * 311 * Calls &drm_bridge_funcs.mode_valid for all the bridges in the encoder 312 * chain, starting from the first bridge to the last. If at least one bridge 313 * does not accept the mode the function returns the error code. 314 * 315 * Note: the bridge passed should be the one closest to the encoder. 316 * 317 * RETURNS: 318 * MODE_OK on success, drm_mode_status Enum error code on failure 319 */ 320 enum drm_mode_status 321 drm_bridge_chain_mode_valid(struct drm_bridge *bridge, 322 const struct drm_display_mode *mode) 323 { 324 struct drm_encoder *encoder; 325 326 if (!bridge) 327 return MODE_OK; 328 329 encoder = bridge->encoder; 330 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) { 331 enum drm_mode_status ret; 332 333 if (!bridge->funcs->mode_valid) 334 continue; 335 336 ret = bridge->funcs->mode_valid(bridge, mode); 337 if (ret != MODE_OK) 338 return ret; 339 } 340 341 return MODE_OK; 342 } 343 EXPORT_SYMBOL(drm_bridge_chain_mode_valid); 344 345 /** 346 * drm_bridge_chain_disable - disables all bridges in the encoder chain 347 * @bridge: bridge control structure 348 * 349 * Calls &drm_bridge_funcs.disable op for all the bridges in the encoder 350 * chain, starting from the last bridge to the first. These are called before 351 * calling the encoder's prepare op. 352 * 353 * Note: the bridge passed should be the one closest to the encoder 354 */ 355 void drm_bridge_chain_disable(struct drm_bridge *bridge) 356 { 357 struct drm_encoder *encoder; 358 struct drm_bridge *iter; 359 360 if (!bridge) 361 return; 362 363 encoder = bridge->encoder; 364 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) { 365 if (iter->funcs->disable) 366 iter->funcs->disable(iter); 367 368 if (iter == bridge) 369 break; 370 } 371 } 372 EXPORT_SYMBOL(drm_bridge_chain_disable); 373 374 /** 375 * drm_bridge_chain_post_disable - cleans up after disabling all bridges in the 376 * encoder chain 377 * @bridge: bridge control structure 378 * 379 * Calls &drm_bridge_funcs.post_disable op for all the bridges in the 380 * encoder chain, starting from the first bridge to the last. These are called 381 * after completing the encoder's prepare op. 382 * 383 * Note: the bridge passed should be the one closest to the encoder 384 */ 385 void drm_bridge_chain_post_disable(struct drm_bridge *bridge) 386 { 387 struct drm_encoder *encoder; 388 389 if (!bridge) 390 return; 391 392 encoder = bridge->encoder; 393 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) { 394 if (bridge->funcs->post_disable) 395 bridge->funcs->post_disable(bridge); 396 } 397 } 398 EXPORT_SYMBOL(drm_bridge_chain_post_disable); 399 400 /** 401 * drm_bridge_chain_mode_set - set proposed mode for all bridges in the 402 * encoder chain 403 * @bridge: bridge control structure 404 * @mode: desired mode to be set for the encoder chain 405 * @adjusted_mode: updated mode that works for this encoder chain 406 * 407 * Calls &drm_bridge_funcs.mode_set op for all the bridges in the 408 * encoder chain, starting from the first bridge to the last. 409 * 410 * Note: the bridge passed should be the one closest to the encoder 411 */ 412 void drm_bridge_chain_mode_set(struct drm_bridge *bridge, 413 const struct drm_display_mode *mode, 414 const struct drm_display_mode *adjusted_mode) 415 { 416 struct drm_encoder *encoder; 417 418 if (!bridge) 419 return; 420 421 encoder = bridge->encoder; 422 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) { 423 if (bridge->funcs->mode_set) 424 bridge->funcs->mode_set(bridge, mode, adjusted_mode); 425 } 426 } 427 EXPORT_SYMBOL(drm_bridge_chain_mode_set); 428 429 /** 430 * drm_bridge_chain_pre_enable - prepares for enabling all bridges in the 431 * encoder chain 432 * @bridge: bridge control structure 433 * 434 * Calls &drm_bridge_funcs.pre_enable op for all the bridges in the encoder 435 * chain, starting from the last bridge to the first. These are called 436 * before calling the encoder's commit op. 437 * 438 * Note: the bridge passed should be the one closest to the encoder 439 */ 440 void drm_bridge_chain_pre_enable(struct drm_bridge *bridge) 441 { 442 struct drm_encoder *encoder; 443 struct drm_bridge *iter; 444 445 if (!bridge) 446 return; 447 448 encoder = bridge->encoder; 449 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) { 450 if (iter->funcs->pre_enable) 451 iter->funcs->pre_enable(iter); 452 } 453 } 454 EXPORT_SYMBOL(drm_bridge_chain_pre_enable); 455 456 /** 457 * drm_bridge_chain_enable - enables all bridges in the encoder chain 458 * @bridge: bridge control structure 459 * 460 * Calls &drm_bridge_funcs.enable op for all the bridges in the encoder 461 * chain, starting from the first bridge to the last. These are called 462 * after completing the encoder's commit op. 463 * 464 * Note that the bridge passed should be the one closest to the encoder 465 */ 466 void drm_bridge_chain_enable(struct drm_bridge *bridge) 467 { 468 struct drm_encoder *encoder; 469 470 if (!bridge) 471 return; 472 473 encoder = bridge->encoder; 474 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) { 475 if (bridge->funcs->enable) 476 bridge->funcs->enable(bridge); 477 } 478 } 479 EXPORT_SYMBOL(drm_bridge_chain_enable); 480 481 /** 482 * drm_atomic_bridge_chain_disable - disables all bridges in the encoder chain 483 * @bridge: bridge control structure 484 * @old_state: old atomic state 485 * 486 * Calls &drm_bridge_funcs.atomic_disable (falls back on 487 * &drm_bridge_funcs.disable) op for all the bridges in the encoder chain, 488 * starting from the last bridge to the first. These are called before calling 489 * &drm_encoder_helper_funcs.atomic_disable 490 * 491 * Note: the bridge passed should be the one closest to the encoder 492 */ 493 void drm_atomic_bridge_chain_disable(struct drm_bridge *bridge, 494 struct drm_atomic_state *old_state) 495 { 496 struct drm_encoder *encoder; 497 struct drm_bridge *iter; 498 499 if (!bridge) 500 return; 501 502 encoder = bridge->encoder; 503 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) { 504 if (iter->funcs->atomic_disable) 505 iter->funcs->atomic_disable(iter, old_state); 506 else if (iter->funcs->disable) 507 iter->funcs->disable(iter); 508 509 if (iter == bridge) 510 break; 511 } 512 } 513 EXPORT_SYMBOL(drm_atomic_bridge_chain_disable); 514 515 /** 516 * drm_atomic_bridge_chain_post_disable - cleans up after disabling all bridges 517 * in the encoder chain 518 * @bridge: bridge control structure 519 * @old_state: old atomic state 520 * 521 * Calls &drm_bridge_funcs.atomic_post_disable (falls back on 522 * &drm_bridge_funcs.post_disable) op for all the bridges in the encoder chain, 523 * starting from the first bridge to the last. These are called after completing 524 * &drm_encoder_helper_funcs.atomic_disable 525 * 526 * Note: the bridge passed should be the one closest to the encoder 527 */ 528 void drm_atomic_bridge_chain_post_disable(struct drm_bridge *bridge, 529 struct drm_atomic_state *old_state) 530 { 531 struct drm_encoder *encoder; 532 533 if (!bridge) 534 return; 535 536 encoder = bridge->encoder; 537 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) { 538 if (bridge->funcs->atomic_post_disable) 539 bridge->funcs->atomic_post_disable(bridge, old_state); 540 else if (bridge->funcs->post_disable) 541 bridge->funcs->post_disable(bridge); 542 } 543 } 544 EXPORT_SYMBOL(drm_atomic_bridge_chain_post_disable); 545 546 /** 547 * drm_atomic_bridge_chain_pre_enable - prepares for enabling all bridges in 548 * the encoder chain 549 * @bridge: bridge control structure 550 * @old_state: old atomic state 551 * 552 * Calls &drm_bridge_funcs.atomic_pre_enable (falls back on 553 * &drm_bridge_funcs.pre_enable) op for all the bridges in the encoder chain, 554 * starting from the last bridge to the first. These are called before calling 555 * &drm_encoder_helper_funcs.atomic_enable 556 * 557 * Note: the bridge passed should be the one closest to the encoder 558 */ 559 void drm_atomic_bridge_chain_pre_enable(struct drm_bridge *bridge, 560 struct drm_atomic_state *old_state) 561 { 562 struct drm_encoder *encoder; 563 struct drm_bridge *iter; 564 565 if (!bridge) 566 return; 567 568 encoder = bridge->encoder; 569 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) { 570 if (iter->funcs->atomic_pre_enable) 571 iter->funcs->atomic_pre_enable(iter, old_state); 572 else if (iter->funcs->pre_enable) 573 iter->funcs->pre_enable(iter); 574 575 if (iter == bridge) 576 break; 577 } 578 } 579 EXPORT_SYMBOL(drm_atomic_bridge_chain_pre_enable); 580 581 /** 582 * drm_atomic_bridge_chain_enable - enables all bridges in the encoder chain 583 * @bridge: bridge control structure 584 * @old_state: old atomic state 585 * 586 * Calls &drm_bridge_funcs.atomic_enable (falls back on 587 * &drm_bridge_funcs.enable) op for all the bridges in the encoder chain, 588 * starting from the first bridge to the last. These are called after completing 589 * &drm_encoder_helper_funcs.atomic_enable 590 * 591 * Note: the bridge passed should be the one closest to the encoder 592 */ 593 void drm_atomic_bridge_chain_enable(struct drm_bridge *bridge, 594 struct drm_atomic_state *old_state) 595 { 596 struct drm_encoder *encoder; 597 598 if (!bridge) 599 return; 600 601 encoder = bridge->encoder; 602 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) { 603 if (bridge->funcs->atomic_enable) 604 bridge->funcs->atomic_enable(bridge, old_state); 605 else if (bridge->funcs->enable) 606 bridge->funcs->enable(bridge); 607 } 608 } 609 EXPORT_SYMBOL(drm_atomic_bridge_chain_enable); 610 611 /** 612 * __drm_atomic_helper_bridge_reset() - Initialize a bridge state to its 613 * default 614 * @bridge: the bridge this state is refers to 615 * @state: bridge state to initialize 616 * 617 * Initialize the bridge state to default values. This is meant to be* called 618 * by the bridge &drm_plane_funcs.reset hook for bridges that subclass the 619 * bridge state. 620 */ 621 void __drm_atomic_helper_bridge_reset(struct drm_bridge *bridge, 622 struct drm_bridge_state *state) 623 { 624 memset(state, 0, sizeof(*state)); 625 state->bridge = bridge; 626 } 627 EXPORT_SYMBOL(__drm_atomic_helper_bridge_reset); 628 629 /** 630 * __drm_atomic_helper_bridge_duplicate_state() - Copy atomic bridge state 631 * @bridge: bridge object 632 * @state: atomic bridge state 633 * 634 * Copies atomic state from a bridge's current state and resets inferred values. 635 * This is useful for drivers that subclass the bridge state. 636 */ 637 void __drm_atomic_helper_bridge_duplicate_state(struct drm_bridge *bridge, 638 struct drm_bridge_state *state) 639 { 640 __drm_atomic_helper_private_obj_duplicate_state(&bridge->base, 641 &state->base); 642 state->bridge = bridge; 643 } 644 EXPORT_SYMBOL(__drm_atomic_helper_bridge_duplicate_state); 645 646 #ifdef CONFIG_OF 647 /** 648 * of_drm_find_bridge - find the bridge corresponding to the device node in 649 * the global bridge list 650 * 651 * @np: device node 652 * 653 * RETURNS: 654 * drm_bridge control struct on success, NULL on failure 655 */ 656 struct drm_bridge *of_drm_find_bridge(struct device_node *np) 657 { 658 struct drm_bridge *bridge; 659 660 mutex_lock(&bridge_lock); 661 662 list_for_each_entry(bridge, &bridge_list, list) { 663 if (bridge->of_node == np) { 664 mutex_unlock(&bridge_lock); 665 return bridge; 666 } 667 } 668 669 mutex_unlock(&bridge_lock); 670 return NULL; 671 } 672 EXPORT_SYMBOL(of_drm_find_bridge); 673 #endif 674 675 MODULE_AUTHOR("Ajay Kumar <ajaykumar.rs@samsung.com>"); 676 MODULE_DESCRIPTION("DRM bridge infrastructure"); 677 MODULE_LICENSE("GPL and additional rights"); 678