1 /* 2 * Copyright (c) 2016 Intel Corporation 3 * 4 * Permission to use, copy, modify, distribute, and sell this software and its 5 * documentation for any purpose is hereby granted without fee, provided that 6 * the above copyright notice appear in all copies and that both that copyright 7 * notice and this permission notice appear in supporting documentation, and 8 * that the name of the copyright holders not be used in advertising or 9 * publicity pertaining to distribution of the software without specific, 10 * written prior permission. The copyright holders make no representations 11 * about the suitability of this software for any purpose. It is provided "as 12 * is" without express or implied warranty. 13 * 14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 20 * OF THIS SOFTWARE. 21 */ 22 23 #ifndef __DRM_BRIDGE_H__ 24 #define __DRM_BRIDGE_H__ 25 26 #include <linux/list.h> 27 #include <linux/ctype.h> 28 29 #include <drm/drm_atomic.h> 30 #include <drm/drm_encoder.h> 31 #include <drm/drm_mode_object.h> 32 #include <drm/drm_modes.h> 33 34 struct drm_bridge; 35 struct drm_bridge_timings; 36 struct drm_panel; 37 38 /** 39 * struct drm_bridge_state - Atomic bridge state object 40 * @base: inherit from &drm_private_state 41 * @bridge: the bridge this state refers to 42 */ 43 struct drm_bridge_state { 44 struct drm_private_state base; 45 46 struct drm_bridge *bridge; 47 }; 48 49 static inline struct drm_bridge_state * 50 drm_priv_to_bridge_state(struct drm_private_state *priv) 51 { 52 return container_of(priv, struct drm_bridge_state, base); 53 } 54 55 /** 56 * struct drm_bridge_funcs - drm_bridge control functions 57 */ 58 struct drm_bridge_funcs { 59 /** 60 * @attach: 61 * 62 * This callback is invoked whenever our bridge is being attached to a 63 * &drm_encoder. 64 * 65 * The @attach callback is optional. 66 * 67 * RETURNS: 68 * 69 * Zero on success, error code on failure. 70 */ 71 int (*attach)(struct drm_bridge *bridge); 72 73 /** 74 * @detach: 75 * 76 * This callback is invoked whenever our bridge is being detached from a 77 * &drm_encoder. 78 * 79 * The @detach callback is optional. 80 */ 81 void (*detach)(struct drm_bridge *bridge); 82 83 /** 84 * @mode_valid: 85 * 86 * This callback is used to check if a specific mode is valid in this 87 * bridge. This should be implemented if the bridge has some sort of 88 * restriction in the modes it can display. For example, a given bridge 89 * may be responsible to set a clock value. If the clock can not 90 * produce all the values for the available modes then this callback 91 * can be used to restrict the number of modes to only the ones that 92 * can be displayed. 93 * 94 * This hook is used by the probe helpers to filter the mode list in 95 * drm_helper_probe_single_connector_modes(), and it is used by the 96 * atomic helpers to validate modes supplied by userspace in 97 * drm_atomic_helper_check_modeset(). 98 * 99 * The @mode_valid callback is optional. 100 * 101 * NOTE: 102 * 103 * Since this function is both called from the check phase of an atomic 104 * commit, and the mode validation in the probe paths it is not allowed 105 * to look at anything else but the passed-in mode, and validate it 106 * against configuration-invariant hardward constraints. Any further 107 * limits which depend upon the configuration can only be checked in 108 * @mode_fixup. 109 * 110 * RETURNS: 111 * 112 * drm_mode_status Enum 113 */ 114 enum drm_mode_status (*mode_valid)(struct drm_bridge *bridge, 115 const struct drm_display_mode *mode); 116 117 /** 118 * @mode_fixup: 119 * 120 * This callback is used to validate and adjust a mode. The parameter 121 * mode is the display mode that should be fed to the next element in 122 * the display chain, either the final &drm_connector or the next 123 * &drm_bridge. The parameter adjusted_mode is the input mode the bridge 124 * requires. It can be modified by this callback and does not need to 125 * match mode. See also &drm_crtc_state.adjusted_mode for more details. 126 * 127 * This is the only hook that allows a bridge to reject a modeset. If 128 * this function passes all other callbacks must succeed for this 129 * configuration. 130 * 131 * The @mode_fixup callback is optional. 132 * 133 * NOTE: 134 * 135 * This function is called in the check phase of atomic modesets, which 136 * can be aborted for any reason (including on userspace's request to 137 * just check whether a configuration would be possible). Drivers MUST 138 * NOT touch any persistent state (hardware or software) or data 139 * structures except the passed in @state parameter. 140 * 141 * Also beware that userspace can request its own custom modes, neither 142 * core nor helpers filter modes to the list of probe modes reported by 143 * the GETCONNECTOR IOCTL and stored in &drm_connector.modes. To ensure 144 * that modes are filtered consistently put any bridge constraints and 145 * limits checks into @mode_valid. 146 * 147 * RETURNS: 148 * 149 * True if an acceptable configuration is possible, false if the modeset 150 * operation should be rejected. 151 */ 152 bool (*mode_fixup)(struct drm_bridge *bridge, 153 const struct drm_display_mode *mode, 154 struct drm_display_mode *adjusted_mode); 155 /** 156 * @disable: 157 * 158 * This callback should disable the bridge. It is called right before 159 * the preceding element in the display pipe is disabled. If the 160 * preceding element is a bridge this means it's called before that 161 * bridge's @disable vfunc. If the preceding element is a &drm_encoder 162 * it's called right before the &drm_encoder_helper_funcs.disable, 163 * &drm_encoder_helper_funcs.prepare or &drm_encoder_helper_funcs.dpms 164 * hook. 165 * 166 * The bridge can assume that the display pipe (i.e. clocks and timing 167 * signals) feeding it is still running when this callback is called. 168 * 169 * The @disable callback is optional. 170 */ 171 void (*disable)(struct drm_bridge *bridge); 172 173 /** 174 * @post_disable: 175 * 176 * This callback should disable the bridge. It is called right after the 177 * preceding element in the display pipe is disabled. If the preceding 178 * element is a bridge this means it's called after that bridge's 179 * @post_disable function. If the preceding element is a &drm_encoder 180 * it's called right after the encoder's 181 * &drm_encoder_helper_funcs.disable, &drm_encoder_helper_funcs.prepare 182 * or &drm_encoder_helper_funcs.dpms hook. 183 * 184 * The bridge must assume that the display pipe (i.e. clocks and timing 185 * singals) feeding it is no longer running when this callback is 186 * called. 187 * 188 * The @post_disable callback is optional. 189 */ 190 void (*post_disable)(struct drm_bridge *bridge); 191 192 /** 193 * @mode_set: 194 * 195 * This callback should set the given mode on the bridge. It is called 196 * after the @mode_set callback for the preceding element in the display 197 * pipeline has been called already. If the bridge is the first element 198 * then this would be &drm_encoder_helper_funcs.mode_set. The display 199 * pipe (i.e. clocks and timing signals) is off when this function is 200 * called. 201 * 202 * The adjusted_mode parameter is the mode output by the CRTC for the 203 * first bridge in the chain. It can be different from the mode 204 * parameter that contains the desired mode for the connector at the end 205 * of the bridges chain, for instance when the first bridge in the chain 206 * performs scaling. The adjusted mode is mostly useful for the first 207 * bridge in the chain and is likely irrelevant for the other bridges. 208 * 209 * For atomic drivers the adjusted_mode is the mode stored in 210 * &drm_crtc_state.adjusted_mode. 211 * 212 * NOTE: 213 * 214 * If a need arises to store and access modes adjusted for other 215 * locations than the connection between the CRTC and the first bridge, 216 * the DRM framework will have to be extended with DRM bridge states. 217 */ 218 void (*mode_set)(struct drm_bridge *bridge, 219 const struct drm_display_mode *mode, 220 const struct drm_display_mode *adjusted_mode); 221 /** 222 * @pre_enable: 223 * 224 * This callback should enable the bridge. It is called right before 225 * the preceding element in the display pipe is enabled. If the 226 * preceding element is a bridge this means it's called before that 227 * bridge's @pre_enable function. If the preceding element is a 228 * &drm_encoder it's called right before the encoder's 229 * &drm_encoder_helper_funcs.enable, &drm_encoder_helper_funcs.commit or 230 * &drm_encoder_helper_funcs.dpms hook. 231 * 232 * The display pipe (i.e. clocks and timing signals) feeding this bridge 233 * will not yet be running when this callback is called. The bridge must 234 * not enable the display link feeding the next bridge in the chain (if 235 * there is one) when this callback is called. 236 * 237 * The @pre_enable callback is optional. 238 */ 239 void (*pre_enable)(struct drm_bridge *bridge); 240 241 /** 242 * @enable: 243 * 244 * This callback should enable the bridge. It is called right after 245 * the preceding element in the display pipe is enabled. If the 246 * preceding element is a bridge this means it's called after that 247 * bridge's @enable function. If the preceding element is a 248 * &drm_encoder it's called right after the encoder's 249 * &drm_encoder_helper_funcs.enable, &drm_encoder_helper_funcs.commit or 250 * &drm_encoder_helper_funcs.dpms hook. 251 * 252 * The bridge can assume that the display pipe (i.e. clocks and timing 253 * signals) feeding it is running when this callback is called. This 254 * callback must enable the display link feeding the next bridge in the 255 * chain if there is one. 256 * 257 * The @enable callback is optional. 258 */ 259 void (*enable)(struct drm_bridge *bridge); 260 261 /** 262 * @atomic_pre_enable: 263 * 264 * This callback should enable the bridge. It is called right before 265 * the preceding element in the display pipe is enabled. If the 266 * preceding element is a bridge this means it's called before that 267 * bridge's @atomic_pre_enable or @pre_enable function. If the preceding 268 * element is a &drm_encoder it's called right before the encoder's 269 * &drm_encoder_helper_funcs.atomic_enable hook. 270 * 271 * The display pipe (i.e. clocks and timing signals) feeding this bridge 272 * will not yet be running when this callback is called. The bridge must 273 * not enable the display link feeding the next bridge in the chain (if 274 * there is one) when this callback is called. 275 * 276 * Note that this function will only be invoked in the context of an 277 * atomic commit. It will not be invoked from 278 * &drm_bridge_chain_pre_enable. It would be prudent to also provide an 279 * implementation of @pre_enable if you are expecting driver calls into 280 * &drm_bridge_chain_pre_enable. 281 * 282 * The @atomic_pre_enable callback is optional. 283 */ 284 void (*atomic_pre_enable)(struct drm_bridge *bridge, 285 struct drm_atomic_state *old_state); 286 287 /** 288 * @atomic_enable: 289 * 290 * This callback should enable the bridge. It is called right after 291 * the preceding element in the display pipe is enabled. If the 292 * preceding element is a bridge this means it's called after that 293 * bridge's @atomic_enable or @enable function. If the preceding element 294 * is a &drm_encoder it's called right after the encoder's 295 * &drm_encoder_helper_funcs.atomic_enable hook. 296 * 297 * The bridge can assume that the display pipe (i.e. clocks and timing 298 * signals) feeding it is running when this callback is called. This 299 * callback must enable the display link feeding the next bridge in the 300 * chain if there is one. 301 * 302 * Note that this function will only be invoked in the context of an 303 * atomic commit. It will not be invoked from &drm_bridge_chain_enable. 304 * It would be prudent to also provide an implementation of @enable if 305 * you are expecting driver calls into &drm_bridge_chain_enable. 306 * 307 * The @atomic_enable callback is optional. 308 */ 309 void (*atomic_enable)(struct drm_bridge *bridge, 310 struct drm_atomic_state *old_state); 311 /** 312 * @atomic_disable: 313 * 314 * This callback should disable the bridge. It is called right before 315 * the preceding element in the display pipe is disabled. If the 316 * preceding element is a bridge this means it's called before that 317 * bridge's @atomic_disable or @disable vfunc. If the preceding element 318 * is a &drm_encoder it's called right before the 319 * &drm_encoder_helper_funcs.atomic_disable hook. 320 * 321 * The bridge can assume that the display pipe (i.e. clocks and timing 322 * signals) feeding it is still running when this callback is called. 323 * 324 * Note that this function will only be invoked in the context of an 325 * atomic commit. It will not be invoked from 326 * &drm_bridge_chain_disable. It would be prudent to also provide an 327 * implementation of @disable if you are expecting driver calls into 328 * &drm_bridge_chain_disable. 329 * 330 * The @atomic_disable callback is optional. 331 */ 332 void (*atomic_disable)(struct drm_bridge *bridge, 333 struct drm_atomic_state *old_state); 334 335 /** 336 * @atomic_post_disable: 337 * 338 * This callback should disable the bridge. It is called right after the 339 * preceding element in the display pipe is disabled. If the preceding 340 * element is a bridge this means it's called after that bridge's 341 * @atomic_post_disable or @post_disable function. If the preceding 342 * element is a &drm_encoder it's called right after the encoder's 343 * &drm_encoder_helper_funcs.atomic_disable hook. 344 * 345 * The bridge must assume that the display pipe (i.e. clocks and timing 346 * signals) feeding it is no longer running when this callback is 347 * called. 348 * 349 * Note that this function will only be invoked in the context of an 350 * atomic commit. It will not be invoked from 351 * &drm_bridge_chain_post_disable. 352 * It would be prudent to also provide an implementation of 353 * @post_disable if you are expecting driver calls into 354 * &drm_bridge_chain_post_disable. 355 * 356 * The @atomic_post_disable callback is optional. 357 */ 358 void (*atomic_post_disable)(struct drm_bridge *bridge, 359 struct drm_atomic_state *old_state); 360 361 /** 362 * @atomic_duplicate_state: 363 * 364 * Duplicate the current bridge state object (which is guaranteed to be 365 * non-NULL). 366 * 367 * The atomic_duplicate_state() is optional. When not implemented the 368 * core allocates a drm_bridge_state object and calls 369 * &__drm_atomic_helper_bridge_duplicate_state() to initialize it. 370 * 371 * RETURNS: 372 * A valid drm_bridge_state object or NULL if the allocation fails. 373 */ 374 struct drm_bridge_state *(*atomic_duplicate_state)(struct drm_bridge *bridge); 375 376 /** 377 * @atomic_destroy_state: 378 * 379 * Destroy a bridge state object previously allocated by 380 * &drm_bridge_funcs.atomic_duplicate_state(). 381 * 382 * The atomic_destroy_state hook is optional. When not implemented the 383 * core calls kfree() on the state. 384 */ 385 void (*atomic_destroy_state)(struct drm_bridge *bridge, 386 struct drm_bridge_state *state); 387 388 /** 389 * @atomic_reset: 390 * 391 * Reset the bridge to a predefined state (or retrieve its current 392 * state) and return a &drm_bridge_state object matching this state. 393 * This function is called at attach time. 394 * 395 * The atomic_reset hook is optional. When not implemented the core 396 * allocates a new state and calls &__drm_atomic_helper_bridge_reset(). 397 * 398 * RETURNS: 399 * A valid drm_bridge_state object in case of success, an ERR_PTR() 400 * giving the reason of the failure otherwise. 401 */ 402 struct drm_bridge_state *(*atomic_reset)(struct drm_bridge *bridge); 403 }; 404 405 /** 406 * struct drm_bridge_timings - timing information for the bridge 407 */ 408 struct drm_bridge_timings { 409 /** 410 * @input_bus_flags: 411 * 412 * Tells what additional settings for the pixel data on the bus 413 * this bridge requires (like pixel signal polarity). See also 414 * &drm_display_info->bus_flags. 415 */ 416 u32 input_bus_flags; 417 /** 418 * @setup_time_ps: 419 * 420 * Defines the time in picoseconds the input data lines must be 421 * stable before the clock edge. 422 */ 423 u32 setup_time_ps; 424 /** 425 * @hold_time_ps: 426 * 427 * Defines the time in picoseconds taken for the bridge to sample the 428 * input signal after the clock edge. 429 */ 430 u32 hold_time_ps; 431 /** 432 * @dual_link: 433 * 434 * True if the bus operates in dual-link mode. The exact meaning is 435 * dependent on the bus type. For LVDS buses, this indicates that even- 436 * and odd-numbered pixels are received on separate links. 437 */ 438 bool dual_link; 439 }; 440 441 /** 442 * struct drm_bridge - central DRM bridge control structure 443 */ 444 struct drm_bridge { 445 /** @base: inherit from &drm_private_object */ 446 struct drm_private_obj base; 447 /** @dev: DRM device this bridge belongs to */ 448 struct drm_device *dev; 449 /** @encoder: encoder to which this bridge is connected */ 450 struct drm_encoder *encoder; 451 /** @chain_node: used to form a bridge chain */ 452 struct list_head chain_node; 453 #ifdef CONFIG_OF 454 /** @of_node: device node pointer to the bridge */ 455 struct device_node *of_node; 456 #endif 457 /** @list: to keep track of all added bridges */ 458 struct list_head list; 459 /** 460 * @timings: 461 * 462 * the timing specification for the bridge, if any (may be NULL) 463 */ 464 const struct drm_bridge_timings *timings; 465 /** @funcs: control functions */ 466 const struct drm_bridge_funcs *funcs; 467 /** @driver_private: pointer to the bridge driver's internal context */ 468 void *driver_private; 469 }; 470 471 static inline struct drm_bridge * 472 drm_priv_to_bridge(struct drm_private_obj *priv) 473 { 474 return container_of(priv, struct drm_bridge, base); 475 } 476 477 void drm_bridge_add(struct drm_bridge *bridge); 478 void drm_bridge_remove(struct drm_bridge *bridge); 479 struct drm_bridge *of_drm_find_bridge(struct device_node *np); 480 int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge, 481 struct drm_bridge *previous); 482 483 /** 484 * drm_bridge_get_next_bridge() - Get the next bridge in the chain 485 * @bridge: bridge object 486 * 487 * RETURNS: 488 * the next bridge in the chain after @bridge, or NULL if @bridge is the last. 489 */ 490 static inline struct drm_bridge * 491 drm_bridge_get_next_bridge(struct drm_bridge *bridge) 492 { 493 if (list_is_last(&bridge->chain_node, &bridge->encoder->bridge_chain)) 494 return NULL; 495 496 return list_next_entry(bridge, chain_node); 497 } 498 499 /** 500 * drm_bridge_get_prev_bridge() - Get the previous bridge in the chain 501 * @bridge: bridge object 502 * 503 * RETURNS: 504 * the previous bridge in the chain, or NULL if @bridge is the first. 505 */ 506 static inline struct drm_bridge * 507 drm_bridge_get_prev_bridge(struct drm_bridge *bridge) 508 { 509 if (list_is_first(&bridge->chain_node, &bridge->encoder->bridge_chain)) 510 return NULL; 511 512 return list_prev_entry(bridge, chain_node); 513 } 514 515 /** 516 * drm_bridge_chain_get_first_bridge() - Get the first bridge in the chain 517 * @encoder: encoder object 518 * 519 * RETURNS: 520 * the first bridge in the chain, or NULL if @encoder has no bridge attached 521 * to it. 522 */ 523 static inline struct drm_bridge * 524 drm_bridge_chain_get_first_bridge(struct drm_encoder *encoder) 525 { 526 return list_first_entry_or_null(&encoder->bridge_chain, 527 struct drm_bridge, chain_node); 528 } 529 530 /** 531 * drm_for_each_bridge_in_chain() - Iterate over all bridges present in a chain 532 * @encoder: the encoder to iterate bridges on 533 * @bridge: a bridge pointer updated to point to the current bridge at each 534 * iteration 535 * 536 * Iterate over all bridges present in the bridge chain attached to @encoder. 537 */ 538 #define drm_for_each_bridge_in_chain(encoder, bridge) \ 539 list_for_each_entry(bridge, &(encoder)->bridge_chain, chain_node) 540 541 bool drm_bridge_chain_mode_fixup(struct drm_bridge *bridge, 542 const struct drm_display_mode *mode, 543 struct drm_display_mode *adjusted_mode); 544 enum drm_mode_status 545 drm_bridge_chain_mode_valid(struct drm_bridge *bridge, 546 const struct drm_display_mode *mode); 547 void drm_bridge_chain_disable(struct drm_bridge *bridge); 548 void drm_bridge_chain_post_disable(struct drm_bridge *bridge); 549 void drm_bridge_chain_mode_set(struct drm_bridge *bridge, 550 const struct drm_display_mode *mode, 551 const struct drm_display_mode *adjusted_mode); 552 void drm_bridge_chain_pre_enable(struct drm_bridge *bridge); 553 void drm_bridge_chain_enable(struct drm_bridge *bridge); 554 555 void drm_atomic_bridge_chain_disable(struct drm_bridge *bridge, 556 struct drm_atomic_state *state); 557 void drm_atomic_bridge_chain_post_disable(struct drm_bridge *bridge, 558 struct drm_atomic_state *state); 559 void drm_atomic_bridge_chain_pre_enable(struct drm_bridge *bridge, 560 struct drm_atomic_state *state); 561 void drm_atomic_bridge_chain_enable(struct drm_bridge *bridge, 562 struct drm_atomic_state *state); 563 564 void __drm_atomic_helper_bridge_reset(struct drm_bridge *bridge, 565 struct drm_bridge_state *state); 566 void __drm_atomic_helper_bridge_duplicate_state(struct drm_bridge *bridge, 567 struct drm_bridge_state *new); 568 569 static inline struct drm_bridge_state * 570 drm_atomic_get_bridge_state(struct drm_atomic_state *state, 571 struct drm_bridge *bridge) 572 { 573 struct drm_private_state *obj_state; 574 575 obj_state = drm_atomic_get_private_obj_state(state, &bridge->base); 576 if (IS_ERR(obj_state)) 577 return ERR_CAST(obj_state); 578 579 return drm_priv_to_bridge_state(obj_state); 580 } 581 582 static inline struct drm_bridge_state * 583 drm_atomic_get_old_bridge_state(struct drm_atomic_state *state, 584 struct drm_bridge *bridge) 585 { 586 struct drm_private_state *obj_state; 587 588 obj_state = drm_atomic_get_old_private_obj_state(state, &bridge->base); 589 if (!obj_state) 590 return NULL; 591 592 return drm_priv_to_bridge_state(obj_state); 593 } 594 595 static inline struct drm_bridge_state * 596 drm_atomic_get_new_bridge_state(struct drm_atomic_state *state, 597 struct drm_bridge *bridge) 598 { 599 struct drm_private_state *obj_state; 600 601 obj_state = drm_atomic_get_new_private_obj_state(state, &bridge->base); 602 if (!obj_state) 603 return NULL; 604 605 return drm_priv_to_bridge_state(obj_state); 606 } 607 608 #ifdef CONFIG_DRM_PANEL_BRIDGE 609 struct drm_bridge *drm_panel_bridge_add(struct drm_panel *panel); 610 struct drm_bridge *drm_panel_bridge_add_typed(struct drm_panel *panel, 611 u32 connector_type); 612 void drm_panel_bridge_remove(struct drm_bridge *bridge); 613 struct drm_bridge *devm_drm_panel_bridge_add(struct device *dev, 614 struct drm_panel *panel); 615 struct drm_bridge *devm_drm_panel_bridge_add_typed(struct device *dev, 616 struct drm_panel *panel, 617 u32 connector_type); 618 struct drm_connector *drm_panel_bridge_connector(struct drm_bridge *bridge); 619 #endif 620 621 #endif 622