Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2014 Samsung Electronics Co., Ltd
0003  *
0004  * Permission is hereby granted, free of charge, to any person obtaining a
0005  * copy of this software and associated documentation files (the "Software"),
0006  * to deal in the Software without restriction, including without limitation
0007  * the rights to use, copy, modify, merge, publish, distribute, sub license,
0008  * and/or sell copies of the Software, and to permit persons to whom the
0009  * Software is furnished to do so, subject to the following conditions:
0010  *
0011  * The above copyright notice and this permission notice (including the
0012  * next paragraph) shall be included in all copies or substantial portions
0013  * of the Software.
0014  *
0015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0017  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
0018  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0019  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
0020  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
0021  * DEALINGS IN THE SOFTWARE.
0022  */
0023 
0024 #include <linux/err.h>
0025 #include <linux/media-bus-format.h>
0026 #include <linux/module.h>
0027 #include <linux/mutex.h>
0028 
0029 #include <drm/drm_atomic_state_helper.h>
0030 #include <drm/drm_bridge.h>
0031 #include <drm/drm_encoder.h>
0032 #include <drm/drm_of.h>
0033 #include <drm/drm_print.h>
0034 
0035 #include "drm_crtc_internal.h"
0036 
0037 /**
0038  * DOC: overview
0039  *
0040  * &struct drm_bridge represents a device that hangs on to an encoder. These are
0041  * handy when a regular &drm_encoder entity isn't enough to represent the entire
0042  * encoder chain.
0043  *
0044  * A bridge is always attached to a single &drm_encoder at a time, but can be
0045  * either connected to it directly, or through a chain of bridges::
0046  *
0047  *     [ CRTC ---> ] Encoder ---> Bridge A ---> Bridge B
0048  *
0049  * Here, the output of the encoder feeds to bridge A, and that furthers feeds to
0050  * bridge B. Bridge chains can be arbitrarily long, and shall be fully linear:
0051  * Chaining multiple bridges to the output of a bridge, or the same bridge to
0052  * the output of different bridges, is not supported.
0053  *
0054  * &drm_bridge, like &drm_panel, aren't &drm_mode_object entities like planes,
0055  * CRTCs, encoders or connectors and hence are not visible to userspace. They
0056  * just provide additional hooks to get the desired output at the end of the
0057  * encoder chain.
0058  */
0059 
0060 /**
0061  * DOC: display driver integration
0062  *
0063  * Display drivers are responsible for linking encoders with the first bridge
0064  * in the chains. This is done by acquiring the appropriate bridge with
0065  * devm_drm_of_get_bridge(). Once acquired, the bridge shall be attached to the
0066  * encoder with a call to drm_bridge_attach().
0067  *
0068  * Bridges are responsible for linking themselves with the next bridge in the
0069  * chain, if any. This is done the same way as for encoders, with the call to
0070  * drm_bridge_attach() occurring in the &drm_bridge_funcs.attach operation.
0071  *
0072  * Once these links are created, the bridges can participate along with encoder
0073  * functions to perform mode validation and fixup (through
0074  * drm_bridge_chain_mode_valid() and drm_atomic_bridge_chain_check()), mode
0075  * setting (through drm_bridge_chain_mode_set()), enable (through
0076  * drm_atomic_bridge_chain_pre_enable() and drm_atomic_bridge_chain_enable())
0077  * and disable (through drm_atomic_bridge_chain_disable() and
0078  * drm_atomic_bridge_chain_post_disable()). Those functions call the
0079  * corresponding operations provided in &drm_bridge_funcs in sequence for all
0080  * bridges in the chain.
0081  *
0082  * For display drivers that use the atomic helpers
0083  * drm_atomic_helper_check_modeset(),
0084  * drm_atomic_helper_commit_modeset_enables() and
0085  * drm_atomic_helper_commit_modeset_disables() (either directly in hand-rolled
0086  * commit check and commit tail handlers, or through the higher-level
0087  * drm_atomic_helper_check() and drm_atomic_helper_commit_tail() or
0088  * drm_atomic_helper_commit_tail_rpm() helpers), this is done transparently and
0089  * requires no intervention from the driver. For other drivers, the relevant
0090  * DRM bridge chain functions shall be called manually.
0091  *
0092  * Bridges also participate in implementing the &drm_connector at the end of
0093  * the bridge chain. Display drivers may use the drm_bridge_connector_init()
0094  * helper to create the &drm_connector, or implement it manually on top of the
0095  * connector-related operations exposed by the bridge (see the overview
0096  * documentation of bridge operations for more details).
0097  */
0098 
0099 /**
0100  * DOC: special care dsi
0101  *
0102  * The interaction between the bridges and other frameworks involved in
0103  * the probing of the upstream driver and the bridge driver can be
0104  * challenging. Indeed, there's multiple cases that needs to be
0105  * considered:
0106  *
0107  * - The upstream driver doesn't use the component framework and isn't a
0108  *   MIPI-DSI host. In this case, the bridge driver will probe at some
0109  *   point and the upstream driver should try to probe again by returning
0110  *   EPROBE_DEFER as long as the bridge driver hasn't probed.
0111  *
0112  * - The upstream driver doesn't use the component framework, but is a
0113  *   MIPI-DSI host. The bridge device uses the MIPI-DCS commands to be
0114  *   controlled. In this case, the bridge device is a child of the
0115  *   display device and when it will probe it's assured that the display
0116  *   device (and MIPI-DSI host) is present. The upstream driver will be
0117  *   assured that the bridge driver is connected between the
0118  *   &mipi_dsi_host_ops.attach and &mipi_dsi_host_ops.detach operations.
0119  *   Therefore, it must run mipi_dsi_host_register() in its probe
0120  *   function, and then run drm_bridge_attach() in its
0121  *   &mipi_dsi_host_ops.attach hook.
0122  *
0123  * - The upstream driver uses the component framework and is a MIPI-DSI
0124  *   host. The bridge device uses the MIPI-DCS commands to be
0125  *   controlled. This is the same situation than above, and can run
0126  *   mipi_dsi_host_register() in either its probe or bind hooks.
0127  *
0128  * - The upstream driver uses the component framework and is a MIPI-DSI
0129  *   host. The bridge device uses a separate bus (such as I2C) to be
0130  *   controlled. In this case, there's no correlation between the probe
0131  *   of the bridge and upstream drivers, so care must be taken to avoid
0132  *   an endless EPROBE_DEFER loop, with each driver waiting for the
0133  *   other to probe.
0134  *
0135  * The ideal pattern to cover the last item (and all the others in the
0136  * MIPI-DSI host driver case) is to split the operations like this:
0137  *
0138  * - The MIPI-DSI host driver must run mipi_dsi_host_register() in its
0139  *   probe hook. It will make sure that the MIPI-DSI host sticks around,
0140  *   and that the driver's bind can be called.
0141  *
0142  * - In its probe hook, the bridge driver must try to find its MIPI-DSI
0143  *   host, register as a MIPI-DSI device and attach the MIPI-DSI device
0144  *   to its host. The bridge driver is now functional.
0145  *
0146  * - In its &struct mipi_dsi_host_ops.attach hook, the MIPI-DSI host can
0147  *   now add its component. Its bind hook will now be called and since
0148  *   the bridge driver is attached and registered, we can now look for
0149  *   and attach it.
0150  *
0151  * At this point, we're now certain that both the upstream driver and
0152  * the bridge driver are functional and we can't have a deadlock-like
0153  * situation when probing.
0154  */
0155 
0156 static DEFINE_MUTEX(bridge_lock);
0157 static LIST_HEAD(bridge_list);
0158 
0159 /**
0160  * drm_bridge_add - add the given bridge to the global bridge list
0161  *
0162  * @bridge: bridge control structure
0163  */
0164 void drm_bridge_add(struct drm_bridge *bridge)
0165 {
0166     mutex_init(&bridge->hpd_mutex);
0167 
0168     mutex_lock(&bridge_lock);
0169     list_add_tail(&bridge->list, &bridge_list);
0170     mutex_unlock(&bridge_lock);
0171 }
0172 EXPORT_SYMBOL(drm_bridge_add);
0173 
0174 static void drm_bridge_remove_void(void *bridge)
0175 {
0176     drm_bridge_remove(bridge);
0177 }
0178 
0179 /**
0180  * devm_drm_bridge_add - devm managed version of drm_bridge_add()
0181  *
0182  * @dev: device to tie the bridge lifetime to
0183  * @bridge: bridge control structure
0184  *
0185  * This is the managed version of drm_bridge_add() which automatically
0186  * calls drm_bridge_remove() when @dev is unbound.
0187  *
0188  * Return: 0 if no error or negative error code.
0189  */
0190 int devm_drm_bridge_add(struct device *dev, struct drm_bridge *bridge)
0191 {
0192     drm_bridge_add(bridge);
0193     return devm_add_action_or_reset(dev, drm_bridge_remove_void, bridge);
0194 }
0195 EXPORT_SYMBOL(devm_drm_bridge_add);
0196 
0197 /**
0198  * drm_bridge_remove - remove the given bridge from the global bridge list
0199  *
0200  * @bridge: bridge control structure
0201  */
0202 void drm_bridge_remove(struct drm_bridge *bridge)
0203 {
0204     mutex_lock(&bridge_lock);
0205     list_del_init(&bridge->list);
0206     mutex_unlock(&bridge_lock);
0207 
0208     mutex_destroy(&bridge->hpd_mutex);
0209 }
0210 EXPORT_SYMBOL(drm_bridge_remove);
0211 
0212 static struct drm_private_state *
0213 drm_bridge_atomic_duplicate_priv_state(struct drm_private_obj *obj)
0214 {
0215     struct drm_bridge *bridge = drm_priv_to_bridge(obj);
0216     struct drm_bridge_state *state;
0217 
0218     state = bridge->funcs->atomic_duplicate_state(bridge);
0219     return state ? &state->base : NULL;
0220 }
0221 
0222 static void
0223 drm_bridge_atomic_destroy_priv_state(struct drm_private_obj *obj,
0224                      struct drm_private_state *s)
0225 {
0226     struct drm_bridge_state *state = drm_priv_to_bridge_state(s);
0227     struct drm_bridge *bridge = drm_priv_to_bridge(obj);
0228 
0229     bridge->funcs->atomic_destroy_state(bridge, state);
0230 }
0231 
0232 static const struct drm_private_state_funcs drm_bridge_priv_state_funcs = {
0233     .atomic_duplicate_state = drm_bridge_atomic_duplicate_priv_state,
0234     .atomic_destroy_state = drm_bridge_atomic_destroy_priv_state,
0235 };
0236 
0237 /**
0238  * drm_bridge_attach - attach the bridge to an encoder's chain
0239  *
0240  * @encoder: DRM encoder
0241  * @bridge: bridge to attach
0242  * @previous: previous bridge in the chain (optional)
0243  * @flags: DRM_BRIDGE_ATTACH_* flags
0244  *
0245  * Called by a kms driver to link the bridge to an encoder's chain. The previous
0246  * argument specifies the previous bridge in the chain. If NULL, the bridge is
0247  * linked directly at the encoder's output. Otherwise it is linked at the
0248  * previous bridge's output.
0249  *
0250  * If non-NULL the previous bridge must be already attached by a call to this
0251  * function.
0252  *
0253  * Note that bridges attached to encoders are auto-detached during encoder
0254  * cleanup in drm_encoder_cleanup(), so drm_bridge_attach() should generally
0255  * *not* be balanced with a drm_bridge_detach() in driver code.
0256  *
0257  * RETURNS:
0258  * Zero on success, error code on failure
0259  */
0260 int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
0261               struct drm_bridge *previous,
0262               enum drm_bridge_attach_flags flags)
0263 {
0264     int ret;
0265 
0266     if (!encoder || !bridge)
0267         return -EINVAL;
0268 
0269     if (previous && (!previous->dev || previous->encoder != encoder))
0270         return -EINVAL;
0271 
0272     if (bridge->dev)
0273         return -EBUSY;
0274 
0275     bridge->dev = encoder->dev;
0276     bridge->encoder = encoder;
0277 
0278     if (previous)
0279         list_add(&bridge->chain_node, &previous->chain_node);
0280     else
0281         list_add(&bridge->chain_node, &encoder->bridge_chain);
0282 
0283     if (bridge->funcs->attach) {
0284         ret = bridge->funcs->attach(bridge, flags);
0285         if (ret < 0)
0286             goto err_reset_bridge;
0287     }
0288 
0289     if (bridge->funcs->atomic_reset) {
0290         struct drm_bridge_state *state;
0291 
0292         state = bridge->funcs->atomic_reset(bridge);
0293         if (IS_ERR(state)) {
0294             ret = PTR_ERR(state);
0295             goto err_detach_bridge;
0296         }
0297 
0298         drm_atomic_private_obj_init(bridge->dev, &bridge->base,
0299                         &state->base,
0300                         &drm_bridge_priv_state_funcs);
0301     }
0302 
0303     return 0;
0304 
0305 err_detach_bridge:
0306     if (bridge->funcs->detach)
0307         bridge->funcs->detach(bridge);
0308 
0309 err_reset_bridge:
0310     bridge->dev = NULL;
0311     bridge->encoder = NULL;
0312     list_del(&bridge->chain_node);
0313 
0314 #ifdef CONFIG_OF
0315     DRM_ERROR("failed to attach bridge %pOF to encoder %s: %d\n",
0316           bridge->of_node, encoder->name, ret);
0317 #else
0318     DRM_ERROR("failed to attach bridge to encoder %s: %d\n",
0319           encoder->name, ret);
0320 #endif
0321 
0322     return ret;
0323 }
0324 EXPORT_SYMBOL(drm_bridge_attach);
0325 
0326 void drm_bridge_detach(struct drm_bridge *bridge)
0327 {
0328     if (WARN_ON(!bridge))
0329         return;
0330 
0331     if (WARN_ON(!bridge->dev))
0332         return;
0333 
0334     if (bridge->funcs->atomic_reset)
0335         drm_atomic_private_obj_fini(&bridge->base);
0336 
0337     if (bridge->funcs->detach)
0338         bridge->funcs->detach(bridge);
0339 
0340     list_del(&bridge->chain_node);
0341     bridge->dev = NULL;
0342 }
0343 
0344 /**
0345  * DOC: bridge operations
0346  *
0347  * Bridge drivers expose operations through the &drm_bridge_funcs structure.
0348  * The DRM internals (atomic and CRTC helpers) use the helpers defined in
0349  * drm_bridge.c to call bridge operations. Those operations are divided in
0350  * three big categories to support different parts of the bridge usage.
0351  *
0352  * - The encoder-related operations support control of the bridges in the
0353  *   chain, and are roughly counterparts to the &drm_encoder_helper_funcs
0354  *   operations. They are used by the legacy CRTC and the atomic modeset
0355  *   helpers to perform mode validation, fixup and setting, and enable and
0356  *   disable the bridge automatically.
0357  *
0358  *   The enable and disable operations are split in
0359  *   &drm_bridge_funcs.pre_enable, &drm_bridge_funcs.enable,
0360  *   &drm_bridge_funcs.disable and &drm_bridge_funcs.post_disable to provide
0361  *   finer-grained control.
0362  *
0363  *   Bridge drivers may implement the legacy version of those operations, or
0364  *   the atomic version (prefixed with atomic\_), in which case they shall also
0365  *   implement the atomic state bookkeeping operations
0366  *   (&drm_bridge_funcs.atomic_duplicate_state,
0367  *   &drm_bridge_funcs.atomic_destroy_state and &drm_bridge_funcs.reset).
0368  *   Mixing atomic and non-atomic versions of the operations is not supported.
0369  *
0370  * - The bus format negotiation operations
0371  *   &drm_bridge_funcs.atomic_get_output_bus_fmts and
0372  *   &drm_bridge_funcs.atomic_get_input_bus_fmts allow bridge drivers to
0373  *   negotiate the formats transmitted between bridges in the chain when
0374  *   multiple formats are supported. Negotiation for formats is performed
0375  *   transparently for display drivers by the atomic modeset helpers. Only
0376  *   atomic versions of those operations exist, bridge drivers that need to
0377  *   implement them shall thus also implement the atomic version of the
0378  *   encoder-related operations. This feature is not supported by the legacy
0379  *   CRTC helpers.
0380  *
0381  * - The connector-related operations support implementing a &drm_connector
0382  *   based on a chain of bridges. DRM bridges traditionally create a
0383  *   &drm_connector for bridges meant to be used at the end of the chain. This
0384  *   puts additional burden on bridge drivers, especially for bridges that may
0385  *   be used in the middle of a chain or at the end of it. Furthermore, it
0386  *   requires all operations of the &drm_connector to be handled by a single
0387  *   bridge, which doesn't always match the hardware architecture.
0388  *
0389  *   To simplify bridge drivers and make the connector implementation more
0390  *   flexible, a new model allows bridges to unconditionally skip creation of
0391  *   &drm_connector and instead expose &drm_bridge_funcs operations to support
0392  *   an externally-implemented &drm_connector. Those operations are
0393  *   &drm_bridge_funcs.detect, &drm_bridge_funcs.get_modes,
0394  *   &drm_bridge_funcs.get_edid, &drm_bridge_funcs.hpd_notify,
0395  *   &drm_bridge_funcs.hpd_enable and &drm_bridge_funcs.hpd_disable. When
0396  *   implemented, display drivers shall create a &drm_connector instance for
0397  *   each chain of bridges, and implement those connector instances based on
0398  *   the bridge connector operations.
0399  *
0400  *   Bridge drivers shall implement the connector-related operations for all
0401  *   the features that the bridge hardware support. For instance, if a bridge
0402  *   supports reading EDID, the &drm_bridge_funcs.get_edid shall be
0403  *   implemented. This however doesn't mean that the DDC lines are wired to the
0404  *   bridge on a particular platform, as they could also be connected to an I2C
0405  *   controller of the SoC. Support for the connector-related operations on the
0406  *   running platform is reported through the &drm_bridge.ops flags. Bridge
0407  *   drivers shall detect which operations they can support on the platform
0408  *   (usually this information is provided by ACPI or DT), and set the
0409  *   &drm_bridge.ops flags for all supported operations. A flag shall only be
0410  *   set if the corresponding &drm_bridge_funcs operation is implemented, but
0411  *   an implemented operation doesn't necessarily imply that the corresponding
0412  *   flag will be set. Display drivers shall use the &drm_bridge.ops flags to
0413  *   decide which bridge to delegate a connector operation to. This mechanism
0414  *   allows providing a single static const &drm_bridge_funcs instance in
0415  *   bridge drivers, improving security by storing function pointers in
0416  *   read-only memory.
0417  *
0418  *   In order to ease transition, bridge drivers may support both the old and
0419  *   new models by making connector creation optional and implementing the
0420  *   connected-related bridge operations. Connector creation is then controlled
0421  *   by the flags argument to the drm_bridge_attach() function. Display drivers
0422  *   that support the new model and create connectors themselves shall set the
0423  *   %DRM_BRIDGE_ATTACH_NO_CONNECTOR flag, and bridge drivers shall then skip
0424  *   connector creation. For intermediate bridges in the chain, the flag shall
0425  *   be passed to the drm_bridge_attach() call for the downstream bridge.
0426  *   Bridge drivers that implement the new model only shall return an error
0427  *   from their &drm_bridge_funcs.attach handler when the
0428  *   %DRM_BRIDGE_ATTACH_NO_CONNECTOR flag is not set. New display drivers
0429  *   should use the new model, and convert the bridge drivers they use if
0430  *   needed, in order to gradually transition to the new model.
0431  */
0432 
0433 /**
0434  * drm_bridge_chain_mode_fixup - fixup proposed mode for all bridges in the
0435  *               encoder chain
0436  * @bridge: bridge control structure
0437  * @mode: desired mode to be set for the bridge
0438  * @adjusted_mode: updated mode that works for this bridge
0439  *
0440  * Calls &drm_bridge_funcs.mode_fixup for all the bridges in the
0441  * encoder chain, starting from the first bridge to the last.
0442  *
0443  * Note: the bridge passed should be the one closest to the encoder
0444  *
0445  * RETURNS:
0446  * true on success, false on failure
0447  */
0448 bool drm_bridge_chain_mode_fixup(struct drm_bridge *bridge,
0449                  const struct drm_display_mode *mode,
0450                  struct drm_display_mode *adjusted_mode)
0451 {
0452     struct drm_encoder *encoder;
0453 
0454     if (!bridge)
0455         return true;
0456 
0457     encoder = bridge->encoder;
0458     list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
0459         if (!bridge->funcs->mode_fixup)
0460             continue;
0461 
0462         if (!bridge->funcs->mode_fixup(bridge, mode, adjusted_mode))
0463             return false;
0464     }
0465 
0466     return true;
0467 }
0468 EXPORT_SYMBOL(drm_bridge_chain_mode_fixup);
0469 
0470 /**
0471  * drm_bridge_chain_mode_valid - validate the mode against all bridges in the
0472  *               encoder chain.
0473  * @bridge: bridge control structure
0474  * @info: display info against which the mode shall be validated
0475  * @mode: desired mode to be validated
0476  *
0477  * Calls &drm_bridge_funcs.mode_valid for all the bridges in the encoder
0478  * chain, starting from the first bridge to the last. If at least one bridge
0479  * does not accept the mode the function returns the error code.
0480  *
0481  * Note: the bridge passed should be the one closest to the encoder.
0482  *
0483  * RETURNS:
0484  * MODE_OK on success, drm_mode_status Enum error code on failure
0485  */
0486 enum drm_mode_status
0487 drm_bridge_chain_mode_valid(struct drm_bridge *bridge,
0488                 const struct drm_display_info *info,
0489                 const struct drm_display_mode *mode)
0490 {
0491     struct drm_encoder *encoder;
0492 
0493     if (!bridge)
0494         return MODE_OK;
0495 
0496     encoder = bridge->encoder;
0497     list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
0498         enum drm_mode_status ret;
0499 
0500         if (!bridge->funcs->mode_valid)
0501             continue;
0502 
0503         ret = bridge->funcs->mode_valid(bridge, info, mode);
0504         if (ret != MODE_OK)
0505             return ret;
0506     }
0507 
0508     return MODE_OK;
0509 }
0510 EXPORT_SYMBOL(drm_bridge_chain_mode_valid);
0511 
0512 /**
0513  * drm_bridge_chain_disable - disables all bridges in the encoder chain
0514  * @bridge: bridge control structure
0515  *
0516  * Calls &drm_bridge_funcs.disable op for all the bridges in the encoder
0517  * chain, starting from the last bridge to the first. These are called before
0518  * calling the encoder's prepare op.
0519  *
0520  * Note: the bridge passed should be the one closest to the encoder
0521  */
0522 void drm_bridge_chain_disable(struct drm_bridge *bridge)
0523 {
0524     struct drm_encoder *encoder;
0525     struct drm_bridge *iter;
0526 
0527     if (!bridge)
0528         return;
0529 
0530     encoder = bridge->encoder;
0531     list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
0532         if (iter->funcs->disable)
0533             iter->funcs->disable(iter);
0534 
0535         if (iter == bridge)
0536             break;
0537     }
0538 }
0539 EXPORT_SYMBOL(drm_bridge_chain_disable);
0540 
0541 /**
0542  * drm_bridge_chain_post_disable - cleans up after disabling all bridges in the
0543  *                 encoder chain
0544  * @bridge: bridge control structure
0545  *
0546  * Calls &drm_bridge_funcs.post_disable op for all the bridges in the
0547  * encoder chain, starting from the first bridge to the last. These are called
0548  * after completing the encoder's prepare op.
0549  *
0550  * Note: the bridge passed should be the one closest to the encoder
0551  */
0552 void drm_bridge_chain_post_disable(struct drm_bridge *bridge)
0553 {
0554     struct drm_encoder *encoder;
0555 
0556     if (!bridge)
0557         return;
0558 
0559     encoder = bridge->encoder;
0560     list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
0561         if (bridge->funcs->post_disable)
0562             bridge->funcs->post_disable(bridge);
0563     }
0564 }
0565 EXPORT_SYMBOL(drm_bridge_chain_post_disable);
0566 
0567 /**
0568  * drm_bridge_chain_mode_set - set proposed mode for all bridges in the
0569  *                 encoder chain
0570  * @bridge: bridge control structure
0571  * @mode: desired mode to be set for the encoder chain
0572  * @adjusted_mode: updated mode that works for this encoder chain
0573  *
0574  * Calls &drm_bridge_funcs.mode_set op for all the bridges in the
0575  * encoder chain, starting from the first bridge to the last.
0576  *
0577  * Note: the bridge passed should be the one closest to the encoder
0578  */
0579 void drm_bridge_chain_mode_set(struct drm_bridge *bridge,
0580                    const struct drm_display_mode *mode,
0581                    const struct drm_display_mode *adjusted_mode)
0582 {
0583     struct drm_encoder *encoder;
0584 
0585     if (!bridge)
0586         return;
0587 
0588     encoder = bridge->encoder;
0589     list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
0590         if (bridge->funcs->mode_set)
0591             bridge->funcs->mode_set(bridge, mode, adjusted_mode);
0592     }
0593 }
0594 EXPORT_SYMBOL(drm_bridge_chain_mode_set);
0595 
0596 /**
0597  * drm_bridge_chain_pre_enable - prepares for enabling all bridges in the
0598  *               encoder chain
0599  * @bridge: bridge control structure
0600  *
0601  * Calls &drm_bridge_funcs.pre_enable op for all the bridges in the encoder
0602  * chain, starting from the last bridge to the first. These are called
0603  * before calling the encoder's commit op.
0604  *
0605  * Note: the bridge passed should be the one closest to the encoder
0606  */
0607 void drm_bridge_chain_pre_enable(struct drm_bridge *bridge)
0608 {
0609     struct drm_encoder *encoder;
0610     struct drm_bridge *iter;
0611 
0612     if (!bridge)
0613         return;
0614 
0615     encoder = bridge->encoder;
0616     list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
0617         if (iter->funcs->pre_enable)
0618             iter->funcs->pre_enable(iter);
0619 
0620         if (iter == bridge)
0621             break;
0622     }
0623 }
0624 EXPORT_SYMBOL(drm_bridge_chain_pre_enable);
0625 
0626 /**
0627  * drm_bridge_chain_enable - enables all bridges in the encoder chain
0628  * @bridge: bridge control structure
0629  *
0630  * Calls &drm_bridge_funcs.enable op for all the bridges in the encoder
0631  * chain, starting from the first bridge to the last. These are called
0632  * after completing the encoder's commit op.
0633  *
0634  * Note that the bridge passed should be the one closest to the encoder
0635  */
0636 void drm_bridge_chain_enable(struct drm_bridge *bridge)
0637 {
0638     struct drm_encoder *encoder;
0639 
0640     if (!bridge)
0641         return;
0642 
0643     encoder = bridge->encoder;
0644     list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
0645         if (bridge->funcs->enable)
0646             bridge->funcs->enable(bridge);
0647     }
0648 }
0649 EXPORT_SYMBOL(drm_bridge_chain_enable);
0650 
0651 /**
0652  * drm_atomic_bridge_chain_disable - disables all bridges in the encoder chain
0653  * @bridge: bridge control structure
0654  * @old_state: old atomic state
0655  *
0656  * Calls &drm_bridge_funcs.atomic_disable (falls back on
0657  * &drm_bridge_funcs.disable) op for all the bridges in the encoder chain,
0658  * starting from the last bridge to the first. These are called before calling
0659  * &drm_encoder_helper_funcs.atomic_disable
0660  *
0661  * Note: the bridge passed should be the one closest to the encoder
0662  */
0663 void drm_atomic_bridge_chain_disable(struct drm_bridge *bridge,
0664                      struct drm_atomic_state *old_state)
0665 {
0666     struct drm_encoder *encoder;
0667     struct drm_bridge *iter;
0668 
0669     if (!bridge)
0670         return;
0671 
0672     encoder = bridge->encoder;
0673     list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
0674         if (iter->funcs->atomic_disable) {
0675             struct drm_bridge_state *old_bridge_state;
0676 
0677             old_bridge_state =
0678                 drm_atomic_get_old_bridge_state(old_state,
0679                                 iter);
0680             if (WARN_ON(!old_bridge_state))
0681                 return;
0682 
0683             iter->funcs->atomic_disable(iter, old_bridge_state);
0684         } else if (iter->funcs->disable) {
0685             iter->funcs->disable(iter);
0686         }
0687 
0688         if (iter == bridge)
0689             break;
0690     }
0691 }
0692 EXPORT_SYMBOL(drm_atomic_bridge_chain_disable);
0693 
0694 /**
0695  * drm_atomic_bridge_chain_post_disable - cleans up after disabling all bridges
0696  *                    in the encoder chain
0697  * @bridge: bridge control structure
0698  * @old_state: old atomic state
0699  *
0700  * Calls &drm_bridge_funcs.atomic_post_disable (falls back on
0701  * &drm_bridge_funcs.post_disable) op for all the bridges in the encoder chain,
0702  * starting from the first bridge to the last. These are called after completing
0703  * &drm_encoder_helper_funcs.atomic_disable
0704  *
0705  * Note: the bridge passed should be the one closest to the encoder
0706  */
0707 void drm_atomic_bridge_chain_post_disable(struct drm_bridge *bridge,
0708                       struct drm_atomic_state *old_state)
0709 {
0710     struct drm_encoder *encoder;
0711 
0712     if (!bridge)
0713         return;
0714 
0715     encoder = bridge->encoder;
0716     list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
0717         if (bridge->funcs->atomic_post_disable) {
0718             struct drm_bridge_state *old_bridge_state;
0719 
0720             old_bridge_state =
0721                 drm_atomic_get_old_bridge_state(old_state,
0722                                 bridge);
0723             if (WARN_ON(!old_bridge_state))
0724                 return;
0725 
0726             bridge->funcs->atomic_post_disable(bridge,
0727                                old_bridge_state);
0728         } else if (bridge->funcs->post_disable) {
0729             bridge->funcs->post_disable(bridge);
0730         }
0731     }
0732 }
0733 EXPORT_SYMBOL(drm_atomic_bridge_chain_post_disable);
0734 
0735 /**
0736  * drm_atomic_bridge_chain_pre_enable - prepares for enabling all bridges in
0737  *                  the encoder chain
0738  * @bridge: bridge control structure
0739  * @old_state: old atomic state
0740  *
0741  * Calls &drm_bridge_funcs.atomic_pre_enable (falls back on
0742  * &drm_bridge_funcs.pre_enable) op for all the bridges in the encoder chain,
0743  * starting from the last bridge to the first. These are called before calling
0744  * &drm_encoder_helper_funcs.atomic_enable
0745  *
0746  * Note: the bridge passed should be the one closest to the encoder
0747  */
0748 void drm_atomic_bridge_chain_pre_enable(struct drm_bridge *bridge,
0749                     struct drm_atomic_state *old_state)
0750 {
0751     struct drm_encoder *encoder;
0752     struct drm_bridge *iter;
0753 
0754     if (!bridge)
0755         return;
0756 
0757     encoder = bridge->encoder;
0758     list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
0759         if (iter->funcs->atomic_pre_enable) {
0760             struct drm_bridge_state *old_bridge_state;
0761 
0762             old_bridge_state =
0763                 drm_atomic_get_old_bridge_state(old_state,
0764                                 iter);
0765             if (WARN_ON(!old_bridge_state))
0766                 return;
0767 
0768             iter->funcs->atomic_pre_enable(iter, old_bridge_state);
0769         } else if (iter->funcs->pre_enable) {
0770             iter->funcs->pre_enable(iter);
0771         }
0772 
0773         if (iter == bridge)
0774             break;
0775     }
0776 }
0777 EXPORT_SYMBOL(drm_atomic_bridge_chain_pre_enable);
0778 
0779 /**
0780  * drm_atomic_bridge_chain_enable - enables all bridges in the encoder chain
0781  * @bridge: bridge control structure
0782  * @old_state: old atomic state
0783  *
0784  * Calls &drm_bridge_funcs.atomic_enable (falls back on
0785  * &drm_bridge_funcs.enable) op for all the bridges in the encoder chain,
0786  * starting from the first bridge to the last. These are called after completing
0787  * &drm_encoder_helper_funcs.atomic_enable
0788  *
0789  * Note: the bridge passed should be the one closest to the encoder
0790  */
0791 void drm_atomic_bridge_chain_enable(struct drm_bridge *bridge,
0792                     struct drm_atomic_state *old_state)
0793 {
0794     struct drm_encoder *encoder;
0795 
0796     if (!bridge)
0797         return;
0798 
0799     encoder = bridge->encoder;
0800     list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
0801         if (bridge->funcs->atomic_enable) {
0802             struct drm_bridge_state *old_bridge_state;
0803 
0804             old_bridge_state =
0805                 drm_atomic_get_old_bridge_state(old_state,
0806                                 bridge);
0807             if (WARN_ON(!old_bridge_state))
0808                 return;
0809 
0810             bridge->funcs->atomic_enable(bridge, old_bridge_state);
0811         } else if (bridge->funcs->enable) {
0812             bridge->funcs->enable(bridge);
0813         }
0814     }
0815 }
0816 EXPORT_SYMBOL(drm_atomic_bridge_chain_enable);
0817 
0818 static int drm_atomic_bridge_check(struct drm_bridge *bridge,
0819                    struct drm_crtc_state *crtc_state,
0820                    struct drm_connector_state *conn_state)
0821 {
0822     if (bridge->funcs->atomic_check) {
0823         struct drm_bridge_state *bridge_state;
0824         int ret;
0825 
0826         bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state,
0827                                    bridge);
0828         if (WARN_ON(!bridge_state))
0829             return -EINVAL;
0830 
0831         ret = bridge->funcs->atomic_check(bridge, bridge_state,
0832                           crtc_state, conn_state);
0833         if (ret)
0834             return ret;
0835     } else if (bridge->funcs->mode_fixup) {
0836         if (!bridge->funcs->mode_fixup(bridge, &crtc_state->mode,
0837                            &crtc_state->adjusted_mode))
0838             return -EINVAL;
0839     }
0840 
0841     return 0;
0842 }
0843 
0844 static int select_bus_fmt_recursive(struct drm_bridge *first_bridge,
0845                     struct drm_bridge *cur_bridge,
0846                     struct drm_crtc_state *crtc_state,
0847                     struct drm_connector_state *conn_state,
0848                     u32 out_bus_fmt)
0849 {
0850     struct drm_bridge_state *cur_state;
0851     unsigned int num_in_bus_fmts, i;
0852     struct drm_bridge *prev_bridge;
0853     u32 *in_bus_fmts;
0854     int ret;
0855 
0856     prev_bridge = drm_bridge_get_prev_bridge(cur_bridge);
0857     cur_state = drm_atomic_get_new_bridge_state(crtc_state->state,
0858                             cur_bridge);
0859 
0860     /*
0861      * If bus format negotiation is not supported by this bridge, let's
0862      * pass MEDIA_BUS_FMT_FIXED to the previous bridge in the chain and
0863      * hope that it can handle this situation gracefully (by providing
0864      * appropriate default values).
0865      */
0866     if (!cur_bridge->funcs->atomic_get_input_bus_fmts) {
0867         if (cur_bridge != first_bridge) {
0868             ret = select_bus_fmt_recursive(first_bridge,
0869                                prev_bridge, crtc_state,
0870                                conn_state,
0871                                MEDIA_BUS_FMT_FIXED);
0872             if (ret)
0873                 return ret;
0874         }
0875 
0876         /*
0877          * Driver does not implement the atomic state hooks, but that's
0878          * fine, as long as it does not access the bridge state.
0879          */
0880         if (cur_state) {
0881             cur_state->input_bus_cfg.format = MEDIA_BUS_FMT_FIXED;
0882             cur_state->output_bus_cfg.format = out_bus_fmt;
0883         }
0884 
0885         return 0;
0886     }
0887 
0888     /*
0889      * If the driver implements ->atomic_get_input_bus_fmts() it
0890      * should also implement the atomic state hooks.
0891      */
0892     if (WARN_ON(!cur_state))
0893         return -EINVAL;
0894 
0895     in_bus_fmts = cur_bridge->funcs->atomic_get_input_bus_fmts(cur_bridge,
0896                             cur_state,
0897                             crtc_state,
0898                             conn_state,
0899                             out_bus_fmt,
0900                             &num_in_bus_fmts);
0901     if (!num_in_bus_fmts)
0902         return -ENOTSUPP;
0903     else if (!in_bus_fmts)
0904         return -ENOMEM;
0905 
0906     if (first_bridge == cur_bridge) {
0907         cur_state->input_bus_cfg.format = in_bus_fmts[0];
0908         cur_state->output_bus_cfg.format = out_bus_fmt;
0909         kfree(in_bus_fmts);
0910         return 0;
0911     }
0912 
0913     for (i = 0; i < num_in_bus_fmts; i++) {
0914         ret = select_bus_fmt_recursive(first_bridge, prev_bridge,
0915                            crtc_state, conn_state,
0916                            in_bus_fmts[i]);
0917         if (ret != -ENOTSUPP)
0918             break;
0919     }
0920 
0921     if (!ret) {
0922         cur_state->input_bus_cfg.format = in_bus_fmts[i];
0923         cur_state->output_bus_cfg.format = out_bus_fmt;
0924     }
0925 
0926     kfree(in_bus_fmts);
0927     return ret;
0928 }
0929 
0930 /*
0931  * This function is called by &drm_atomic_bridge_chain_check() just before
0932  * calling &drm_bridge_funcs.atomic_check() on all elements of the chain.
0933  * It performs bus format negotiation between bridge elements. The negotiation
0934  * happens in reverse order, starting from the last element in the chain up to
0935  * @bridge.
0936  *
0937  * Negotiation starts by retrieving supported output bus formats on the last
0938  * bridge element and testing them one by one. The test is recursive, meaning
0939  * that for each tested output format, the whole chain will be walked backward,
0940  * and each element will have to choose an input bus format that can be
0941  * transcoded to the requested output format. When a bridge element does not
0942  * support transcoding into a specific output format -ENOTSUPP is returned and
0943  * the next bridge element will have to try a different format. If none of the
0944  * combinations worked, -ENOTSUPP is returned and the atomic modeset will fail.
0945  *
0946  * This implementation is relying on
0947  * &drm_bridge_funcs.atomic_get_output_bus_fmts() and
0948  * &drm_bridge_funcs.atomic_get_input_bus_fmts() to gather supported
0949  * input/output formats.
0950  *
0951  * When &drm_bridge_funcs.atomic_get_output_bus_fmts() is not implemented by
0952  * the last element of the chain, &drm_atomic_bridge_chain_select_bus_fmts()
0953  * tries a single format: &drm_connector.display_info.bus_formats[0] if
0954  * available, MEDIA_BUS_FMT_FIXED otherwise.
0955  *
0956  * When &drm_bridge_funcs.atomic_get_input_bus_fmts() is not implemented,
0957  * &drm_atomic_bridge_chain_select_bus_fmts() skips the negotiation on the
0958  * bridge element that lacks this hook and asks the previous element in the
0959  * chain to try MEDIA_BUS_FMT_FIXED. It's up to bridge drivers to decide what
0960  * to do in that case (fail if they want to enforce bus format negotiation, or
0961  * provide a reasonable default if they need to support pipelines where not
0962  * all elements support bus format negotiation).
0963  */
0964 static int
0965 drm_atomic_bridge_chain_select_bus_fmts(struct drm_bridge *bridge,
0966                     struct drm_crtc_state *crtc_state,
0967                     struct drm_connector_state *conn_state)
0968 {
0969     struct drm_connector *conn = conn_state->connector;
0970     struct drm_encoder *encoder = bridge->encoder;
0971     struct drm_bridge_state *last_bridge_state;
0972     unsigned int i, num_out_bus_fmts;
0973     struct drm_bridge *last_bridge;
0974     u32 *out_bus_fmts;
0975     int ret = 0;
0976 
0977     last_bridge = list_last_entry(&encoder->bridge_chain,
0978                       struct drm_bridge, chain_node);
0979     last_bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state,
0980                                 last_bridge);
0981 
0982     if (last_bridge->funcs->atomic_get_output_bus_fmts) {
0983         const struct drm_bridge_funcs *funcs = last_bridge->funcs;
0984 
0985         /*
0986          * If the driver implements ->atomic_get_output_bus_fmts() it
0987          * should also implement the atomic state hooks.
0988          */
0989         if (WARN_ON(!last_bridge_state))
0990             return -EINVAL;
0991 
0992         out_bus_fmts = funcs->atomic_get_output_bus_fmts(last_bridge,
0993                             last_bridge_state,
0994                             crtc_state,
0995                             conn_state,
0996                             &num_out_bus_fmts);
0997         if (!num_out_bus_fmts)
0998             return -ENOTSUPP;
0999         else if (!out_bus_fmts)
1000             return -ENOMEM;
1001     } else {
1002         num_out_bus_fmts = 1;
1003         out_bus_fmts = kmalloc(sizeof(*out_bus_fmts), GFP_KERNEL);
1004         if (!out_bus_fmts)
1005             return -ENOMEM;
1006 
1007         if (conn->display_info.num_bus_formats &&
1008             conn->display_info.bus_formats)
1009             out_bus_fmts[0] = conn->display_info.bus_formats[0];
1010         else
1011             out_bus_fmts[0] = MEDIA_BUS_FMT_FIXED;
1012     }
1013 
1014     for (i = 0; i < num_out_bus_fmts; i++) {
1015         ret = select_bus_fmt_recursive(bridge, last_bridge, crtc_state,
1016                            conn_state, out_bus_fmts[i]);
1017         if (ret != -ENOTSUPP)
1018             break;
1019     }
1020 
1021     kfree(out_bus_fmts);
1022 
1023     return ret;
1024 }
1025 
1026 static void
1027 drm_atomic_bridge_propagate_bus_flags(struct drm_bridge *bridge,
1028                       struct drm_connector *conn,
1029                       struct drm_atomic_state *state)
1030 {
1031     struct drm_bridge_state *bridge_state, *next_bridge_state;
1032     struct drm_bridge *next_bridge;
1033     u32 output_flags = 0;
1034 
1035     bridge_state = drm_atomic_get_new_bridge_state(state, bridge);
1036 
1037     /* No bridge state attached to this bridge => nothing to propagate. */
1038     if (!bridge_state)
1039         return;
1040 
1041     next_bridge = drm_bridge_get_next_bridge(bridge);
1042 
1043     /*
1044      * Let's try to apply the most common case here, that is, propagate
1045      * display_info flags for the last bridge, and propagate the input
1046      * flags of the next bridge element to the output end of the current
1047      * bridge when the bridge is not the last one.
1048      * There are exceptions to this rule, like when signal inversion is
1049      * happening at the board level, but that's something drivers can deal
1050      * with from their &drm_bridge_funcs.atomic_check() implementation by
1051      * simply overriding the flags value we've set here.
1052      */
1053     if (!next_bridge) {
1054         output_flags = conn->display_info.bus_flags;
1055     } else {
1056         next_bridge_state = drm_atomic_get_new_bridge_state(state,
1057                                 next_bridge);
1058         /*
1059          * No bridge state attached to the next bridge, just leave the
1060          * flags to 0.
1061          */
1062         if (next_bridge_state)
1063             output_flags = next_bridge_state->input_bus_cfg.flags;
1064     }
1065 
1066     bridge_state->output_bus_cfg.flags = output_flags;
1067 
1068     /*
1069      * Propagate the output flags to the input end of the bridge. Again, it's
1070      * not necessarily what all bridges want, but that's what most of them
1071      * do, and by doing that by default we avoid forcing drivers to
1072      * duplicate the "dummy propagation" logic.
1073      */
1074     bridge_state->input_bus_cfg.flags = output_flags;
1075 }
1076 
1077 /**
1078  * drm_atomic_bridge_chain_check() - Do an atomic check on the bridge chain
1079  * @bridge: bridge control structure
1080  * @crtc_state: new CRTC state
1081  * @conn_state: new connector state
1082  *
1083  * First trigger a bus format negotiation before calling
1084  * &drm_bridge_funcs.atomic_check() (falls back on
1085  * &drm_bridge_funcs.mode_fixup()) op for all the bridges in the encoder chain,
1086  * starting from the last bridge to the first. These are called before calling
1087  * &drm_encoder_helper_funcs.atomic_check()
1088  *
1089  * RETURNS:
1090  * 0 on success, a negative error code on failure
1091  */
1092 int drm_atomic_bridge_chain_check(struct drm_bridge *bridge,
1093                   struct drm_crtc_state *crtc_state,
1094                   struct drm_connector_state *conn_state)
1095 {
1096     struct drm_connector *conn = conn_state->connector;
1097     struct drm_encoder *encoder;
1098     struct drm_bridge *iter;
1099     int ret;
1100 
1101     if (!bridge)
1102         return 0;
1103 
1104     ret = drm_atomic_bridge_chain_select_bus_fmts(bridge, crtc_state,
1105                               conn_state);
1106     if (ret)
1107         return ret;
1108 
1109     encoder = bridge->encoder;
1110     list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
1111         int ret;
1112 
1113         /*
1114          * Bus flags are propagated by default. If a bridge needs to
1115          * tweak the input bus flags for any reason, it should happen
1116          * in its &drm_bridge_funcs.atomic_check() implementation such
1117          * that preceding bridges in the chain can propagate the new
1118          * bus flags.
1119          */
1120         drm_atomic_bridge_propagate_bus_flags(iter, conn,
1121                               crtc_state->state);
1122 
1123         ret = drm_atomic_bridge_check(iter, crtc_state, conn_state);
1124         if (ret)
1125             return ret;
1126 
1127         if (iter == bridge)
1128             break;
1129     }
1130 
1131     return 0;
1132 }
1133 EXPORT_SYMBOL(drm_atomic_bridge_chain_check);
1134 
1135 /**
1136  * drm_bridge_detect - check if anything is attached to the bridge output
1137  * @bridge: bridge control structure
1138  *
1139  * If the bridge supports output detection, as reported by the
1140  * DRM_BRIDGE_OP_DETECT bridge ops flag, call &drm_bridge_funcs.detect for the
1141  * bridge and return the connection status. Otherwise return
1142  * connector_status_unknown.
1143  *
1144  * RETURNS:
1145  * The detection status on success, or connector_status_unknown if the bridge
1146  * doesn't support output detection.
1147  */
1148 enum drm_connector_status drm_bridge_detect(struct drm_bridge *bridge)
1149 {
1150     if (!(bridge->ops & DRM_BRIDGE_OP_DETECT))
1151         return connector_status_unknown;
1152 
1153     return bridge->funcs->detect(bridge);
1154 }
1155 EXPORT_SYMBOL_GPL(drm_bridge_detect);
1156 
1157 /**
1158  * drm_bridge_get_modes - fill all modes currently valid for the sink into the
1159  * @connector
1160  * @bridge: bridge control structure
1161  * @connector: the connector to fill with modes
1162  *
1163  * If the bridge supports output modes retrieval, as reported by the
1164  * DRM_BRIDGE_OP_MODES bridge ops flag, call &drm_bridge_funcs.get_modes to
1165  * fill the connector with all valid modes and return the number of modes
1166  * added. Otherwise return 0.
1167  *
1168  * RETURNS:
1169  * The number of modes added to the connector.
1170  */
1171 int drm_bridge_get_modes(struct drm_bridge *bridge,
1172              struct drm_connector *connector)
1173 {
1174     if (!(bridge->ops & DRM_BRIDGE_OP_MODES))
1175         return 0;
1176 
1177     return bridge->funcs->get_modes(bridge, connector);
1178 }
1179 EXPORT_SYMBOL_GPL(drm_bridge_get_modes);
1180 
1181 /**
1182  * drm_bridge_get_edid - get the EDID data of the connected display
1183  * @bridge: bridge control structure
1184  * @connector: the connector to read EDID for
1185  *
1186  * If the bridge supports output EDID retrieval, as reported by the
1187  * DRM_BRIDGE_OP_EDID bridge ops flag, call &drm_bridge_funcs.get_edid to
1188  * get the EDID and return it. Otherwise return NULL.
1189  *
1190  * RETURNS:
1191  * The retrieved EDID on success, or NULL otherwise.
1192  */
1193 struct edid *drm_bridge_get_edid(struct drm_bridge *bridge,
1194                  struct drm_connector *connector)
1195 {
1196     if (!(bridge->ops & DRM_BRIDGE_OP_EDID))
1197         return NULL;
1198 
1199     return bridge->funcs->get_edid(bridge, connector);
1200 }
1201 EXPORT_SYMBOL_GPL(drm_bridge_get_edid);
1202 
1203 /**
1204  * drm_bridge_hpd_enable - enable hot plug detection for the bridge
1205  * @bridge: bridge control structure
1206  * @cb: hot-plug detection callback
1207  * @data: data to be passed to the hot-plug detection callback
1208  *
1209  * Call &drm_bridge_funcs.hpd_enable if implemented and register the given @cb
1210  * and @data as hot plug notification callback. From now on the @cb will be
1211  * called with @data when an output status change is detected by the bridge,
1212  * until hot plug notification gets disabled with drm_bridge_hpd_disable().
1213  *
1214  * Hot plug detection is supported only if the DRM_BRIDGE_OP_HPD flag is set in
1215  * bridge->ops. This function shall not be called when the flag is not set.
1216  *
1217  * Only one hot plug detection callback can be registered at a time, it is an
1218  * error to call this function when hot plug detection is already enabled for
1219  * the bridge.
1220  */
1221 void drm_bridge_hpd_enable(struct drm_bridge *bridge,
1222                void (*cb)(void *data,
1223                       enum drm_connector_status status),
1224                void *data)
1225 {
1226     if (!(bridge->ops & DRM_BRIDGE_OP_HPD))
1227         return;
1228 
1229     mutex_lock(&bridge->hpd_mutex);
1230 
1231     if (WARN(bridge->hpd_cb, "Hot plug detection already enabled\n"))
1232         goto unlock;
1233 
1234     bridge->hpd_cb = cb;
1235     bridge->hpd_data = data;
1236 
1237     if (bridge->funcs->hpd_enable)
1238         bridge->funcs->hpd_enable(bridge);
1239 
1240 unlock:
1241     mutex_unlock(&bridge->hpd_mutex);
1242 }
1243 EXPORT_SYMBOL_GPL(drm_bridge_hpd_enable);
1244 
1245 /**
1246  * drm_bridge_hpd_disable - disable hot plug detection for the bridge
1247  * @bridge: bridge control structure
1248  *
1249  * Call &drm_bridge_funcs.hpd_disable if implemented and unregister the hot
1250  * plug detection callback previously registered with drm_bridge_hpd_enable().
1251  * Once this function returns the callback will not be called by the bridge
1252  * when an output status change occurs.
1253  *
1254  * Hot plug detection is supported only if the DRM_BRIDGE_OP_HPD flag is set in
1255  * bridge->ops. This function shall not be called when the flag is not set.
1256  */
1257 void drm_bridge_hpd_disable(struct drm_bridge *bridge)
1258 {
1259     if (!(bridge->ops & DRM_BRIDGE_OP_HPD))
1260         return;
1261 
1262     mutex_lock(&bridge->hpd_mutex);
1263     if (bridge->funcs->hpd_disable)
1264         bridge->funcs->hpd_disable(bridge);
1265 
1266     bridge->hpd_cb = NULL;
1267     bridge->hpd_data = NULL;
1268     mutex_unlock(&bridge->hpd_mutex);
1269 }
1270 EXPORT_SYMBOL_GPL(drm_bridge_hpd_disable);
1271 
1272 /**
1273  * drm_bridge_hpd_notify - notify hot plug detection events
1274  * @bridge: bridge control structure
1275  * @status: output connection status
1276  *
1277  * Bridge drivers shall call this function to report hot plug events when they
1278  * detect a change in the output status, when hot plug detection has been
1279  * enabled by drm_bridge_hpd_enable().
1280  *
1281  * This function shall be called in a context that can sleep.
1282  */
1283 void drm_bridge_hpd_notify(struct drm_bridge *bridge,
1284                enum drm_connector_status status)
1285 {
1286     mutex_lock(&bridge->hpd_mutex);
1287     if (bridge->hpd_cb)
1288         bridge->hpd_cb(bridge->hpd_data, status);
1289     mutex_unlock(&bridge->hpd_mutex);
1290 }
1291 EXPORT_SYMBOL_GPL(drm_bridge_hpd_notify);
1292 
1293 #ifdef CONFIG_OF
1294 /**
1295  * of_drm_find_bridge - find the bridge corresponding to the device node in
1296  *          the global bridge list
1297  *
1298  * @np: device node
1299  *
1300  * RETURNS:
1301  * drm_bridge control struct on success, NULL on failure
1302  */
1303 struct drm_bridge *of_drm_find_bridge(struct device_node *np)
1304 {
1305     struct drm_bridge *bridge;
1306 
1307     mutex_lock(&bridge_lock);
1308 
1309     list_for_each_entry(bridge, &bridge_list, list) {
1310         if (bridge->of_node == np) {
1311             mutex_unlock(&bridge_lock);
1312             return bridge;
1313         }
1314     }
1315 
1316     mutex_unlock(&bridge_lock);
1317     return NULL;
1318 }
1319 EXPORT_SYMBOL(of_drm_find_bridge);
1320 #endif
1321 
1322 MODULE_AUTHOR("Ajay Kumar <ajaykumar.rs@samsung.com>");
1323 MODULE_DESCRIPTION("DRM bridge infrastructure");
1324 MODULE_LICENSE("GPL and additional rights");