/*1* Copyright (c) 2014 Samsung Electronics Co., Ltd2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sub license,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the11* next paragraph) shall be included in all copies or substantial portions12* of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER20* DEALINGS IN THE SOFTWARE.21*/2223#include <linux/debugfs.h>24#include <linux/err.h>25#include <linux/export.h>26#include <linux/media-bus-format.h>27#include <linux/module.h>28#include <linux/mutex.h>2930#include <drm/drm_atomic_state_helper.h>31#include <drm/drm_bridge.h>32#include <drm/drm_debugfs.h>33#include <drm/drm_edid.h>34#include <drm/drm_encoder.h>35#include <drm/drm_file.h>36#include <drm/drm_of.h>37#include <drm/drm_print.h>3839#include "drm_crtc_internal.h"4041/**42* DOC: overview43*44* &struct drm_bridge represents a device that hangs on to an encoder. These are45* handy when a regular &drm_encoder entity isn't enough to represent the entire46* encoder chain.47*48* A bridge is always attached to a single &drm_encoder at a time, but can be49* either connected to it directly, or through a chain of bridges::50*51* [ CRTC ---> ] Encoder ---> Bridge A ---> Bridge B52*53* Here, the output of the encoder feeds to bridge A, and that furthers feeds to54* bridge B. Bridge chains can be arbitrarily long, and shall be fully linear:55* Chaining multiple bridges to the output of a bridge, or the same bridge to56* the output of different bridges, is not supported.57*58* &drm_bridge, like &drm_panel, aren't &drm_mode_object entities like planes,59* CRTCs, encoders or connectors and hence are not visible to userspace. They60* just provide additional hooks to get the desired output at the end of the61* encoder chain.62*/6364/**65* DOC: display driver integration66*67* Display drivers are responsible for linking encoders with the first bridge68* in the chains. This is done by acquiring the appropriate bridge with69* devm_drm_of_get_bridge(). Once acquired, the bridge shall be attached to the70* encoder with a call to drm_bridge_attach().71*72* Bridges are responsible for linking themselves with the next bridge in the73* chain, if any. This is done the same way as for encoders, with the call to74* drm_bridge_attach() occurring in the &drm_bridge_funcs.attach operation.75*76* Once these links are created, the bridges can participate along with encoder77* functions to perform mode validation and fixup (through78* drm_bridge_chain_mode_valid() and drm_atomic_bridge_chain_check()), mode79* setting (through drm_bridge_chain_mode_set()), enable (through80* drm_atomic_bridge_chain_pre_enable() and drm_atomic_bridge_chain_enable())81* and disable (through drm_atomic_bridge_chain_disable() and82* drm_atomic_bridge_chain_post_disable()). Those functions call the83* corresponding operations provided in &drm_bridge_funcs in sequence for all84* bridges in the chain.85*86* For display drivers that use the atomic helpers87* drm_atomic_helper_check_modeset(),88* drm_atomic_helper_commit_modeset_enables() and89* drm_atomic_helper_commit_modeset_disables() (either directly in hand-rolled90* commit check and commit tail handlers, or through the higher-level91* drm_atomic_helper_check() and drm_atomic_helper_commit_tail() or92* drm_atomic_helper_commit_tail_rpm() helpers), this is done transparently and93* requires no intervention from the driver. For other drivers, the relevant94* DRM bridge chain functions shall be called manually.95*96* Bridges also participate in implementing the &drm_connector at the end of97* the bridge chain. Display drivers may use the drm_bridge_connector_init()98* helper to create the &drm_connector, or implement it manually on top of the99* connector-related operations exposed by the bridge (see the overview100* documentation of bridge operations for more details).101*/102103/**104* DOC: special care dsi105*106* The interaction between the bridges and other frameworks involved in107* the probing of the upstream driver and the bridge driver can be108* challenging. Indeed, there's multiple cases that needs to be109* considered:110*111* - The upstream driver doesn't use the component framework and isn't a112* MIPI-DSI host. In this case, the bridge driver will probe at some113* point and the upstream driver should try to probe again by returning114* EPROBE_DEFER as long as the bridge driver hasn't probed.115*116* - The upstream driver doesn't use the component framework, but is a117* MIPI-DSI host. The bridge device uses the MIPI-DCS commands to be118* controlled. In this case, the bridge device is a child of the119* display device and when it will probe it's assured that the display120* device (and MIPI-DSI host) is present. The upstream driver will be121* assured that the bridge driver is connected between the122* &mipi_dsi_host_ops.attach and &mipi_dsi_host_ops.detach operations.123* Therefore, it must run mipi_dsi_host_register() in its probe124* function, and then run drm_bridge_attach() in its125* &mipi_dsi_host_ops.attach hook.126*127* - The upstream driver uses the component framework and is a MIPI-DSI128* host. The bridge device uses the MIPI-DCS commands to be129* controlled. This is the same situation than above, and can run130* mipi_dsi_host_register() in either its probe or bind hooks.131*132* - The upstream driver uses the component framework and is a MIPI-DSI133* host. The bridge device uses a separate bus (such as I2C) to be134* controlled. In this case, there's no correlation between the probe135* of the bridge and upstream drivers, so care must be taken to avoid136* an endless EPROBE_DEFER loop, with each driver waiting for the137* other to probe.138*139* The ideal pattern to cover the last item (and all the others in the140* MIPI-DSI host driver case) is to split the operations like this:141*142* - The MIPI-DSI host driver must run mipi_dsi_host_register() in its143* probe hook. It will make sure that the MIPI-DSI host sticks around,144* and that the driver's bind can be called.145*146* - In its probe hook, the bridge driver must try to find its MIPI-DSI147* host, register as a MIPI-DSI device and attach the MIPI-DSI device148* to its host. The bridge driver is now functional.149*150* - In its &struct mipi_dsi_host_ops.attach hook, the MIPI-DSI host can151* now add its component. Its bind hook will now be called and since152* the bridge driver is attached and registered, we can now look for153* and attach it.154*155* At this point, we're now certain that both the upstream driver and156* the bridge driver are functional and we can't have a deadlock-like157* situation when probing.158*/159160/**161* DOC: dsi bridge operations162*163* DSI host interfaces are expected to be implemented as bridges rather than164* encoders, however there are a few aspects of their operation that need to165* be defined in order to provide a consistent interface.166*167* A DSI host should keep the PHY powered down until the pre_enable operation is168* called. All lanes are in an undefined idle state up to this point, and it169* must not be assumed that it is LP-11.170* pre_enable should initialise the PHY, set the data lanes to LP-11, and the171* clock lane to either LP-11 or HS depending on the mode_flag172* %MIPI_DSI_CLOCK_NON_CONTINUOUS.173*174* Ordinarily the downstream bridge DSI peripheral pre_enable will have been175* called before the DSI host. If the DSI peripheral requires LP-11 and/or176* the clock lane to be in HS mode prior to pre_enable, then it can set the177* &pre_enable_prev_first flag to request the pre_enable (and178* post_disable) order to be altered to enable the DSI host first.179*180* Either the CRTC being enabled, or the DSI host enable operation should switch181* the host to actively transmitting video on the data lanes.182*183* The reverse also applies. The DSI host disable operation or stopping the CRTC184* should stop transmitting video, and the data lanes should return to the LP-11185* state. The DSI host &post_disable operation should disable the PHY.186* If the &pre_enable_prev_first flag is set, then the DSI peripheral's187* bridge &post_disable will be called before the DSI host's post_disable.188*189* Whilst it is valid to call &host_transfer prior to pre_enable or after190* post_disable, the exact state of the lanes is undefined at this point. The191* DSI host should initialise the interface, transmit the data, and then disable192* the interface again.193*194* Ultra Low Power State (ULPS) is not explicitly supported by DRM. If195* implemented, it therefore needs to be handled entirely within the DSI Host196* driver.197*/198199/* Protect bridge_list and bridge_lingering_list */200static DEFINE_MUTEX(bridge_lock);201static LIST_HEAD(bridge_list);202static LIST_HEAD(bridge_lingering_list);203204static void __drm_bridge_free(struct kref *kref)205{206struct drm_bridge *bridge = container_of(kref, struct drm_bridge, refcount);207208mutex_lock(&bridge_lock);209list_del(&bridge->list);210mutex_unlock(&bridge_lock);211212if (bridge->funcs->destroy)213bridge->funcs->destroy(bridge);214215kfree(bridge->container);216}217218/**219* drm_bridge_get - Acquire a bridge reference220* @bridge: DRM bridge221*222* This function increments the bridge's refcount.223*224* Returns:225* Pointer to @bridge.226*/227struct drm_bridge *drm_bridge_get(struct drm_bridge *bridge)228{229if (bridge)230kref_get(&bridge->refcount);231232return bridge;233}234EXPORT_SYMBOL(drm_bridge_get);235236/**237* drm_bridge_put - Release a bridge reference238* @bridge: DRM bridge239*240* This function decrements the bridge's reference count and frees the241* object if the reference count drops to zero.242*/243void drm_bridge_put(struct drm_bridge *bridge)244{245if (bridge)246kref_put(&bridge->refcount, __drm_bridge_free);247}248EXPORT_SYMBOL(drm_bridge_put);249250/**251* drm_bridge_put_void - wrapper to drm_bridge_put() taking a void pointer252*253* @data: pointer to @struct drm_bridge, cast to a void pointer254*255* Wrapper of drm_bridge_put() to be used when a function taking a void256* pointer is needed, for example as a devm action.257*/258static void drm_bridge_put_void(void *data)259{260struct drm_bridge *bridge = (struct drm_bridge *)data;261262drm_bridge_put(bridge);263}264265void *__devm_drm_bridge_alloc(struct device *dev, size_t size, size_t offset,266const struct drm_bridge_funcs *funcs)267{268void *container;269struct drm_bridge *bridge;270int err;271272if (!funcs) {273dev_warn(dev, "Missing funcs pointer\n");274return ERR_PTR(-EINVAL);275}276277container = kzalloc(size, GFP_KERNEL);278if (!container)279return ERR_PTR(-ENOMEM);280281bridge = container + offset;282INIT_LIST_HEAD(&bridge->list);283bridge->container = container;284bridge->funcs = funcs;285kref_init(&bridge->refcount);286287err = devm_add_action_or_reset(dev, drm_bridge_put_void, bridge);288if (err)289return ERR_PTR(err);290291return container;292}293EXPORT_SYMBOL(__devm_drm_bridge_alloc);294295/**296* drm_bridge_add - register a bridge297*298* @bridge: bridge control structure299*300* Add the given bridge to the global list of bridges, where they can be301* found by users via of_drm_find_bridge().302*303* The bridge to be added must have been allocated by304* devm_drm_bridge_alloc().305*/306void drm_bridge_add(struct drm_bridge *bridge)307{308if (!bridge->container)309DRM_WARN("DRM bridge corrupted or not allocated by devm_drm_bridge_alloc()\n");310311drm_bridge_get(bridge);312313/*314* If the bridge was previously added and then removed, it is now315* in bridge_lingering_list. Remove it or bridge_lingering_list will be316* corrupted when adding this bridge to bridge_list below.317*/318if (!list_empty(&bridge->list))319list_del_init(&bridge->list);320321mutex_init(&bridge->hpd_mutex);322323if (bridge->ops & DRM_BRIDGE_OP_HDMI)324bridge->ycbcr_420_allowed = !!(bridge->supported_formats &325BIT(HDMI_COLORSPACE_YUV420));326327mutex_lock(&bridge_lock);328list_add_tail(&bridge->list, &bridge_list);329mutex_unlock(&bridge_lock);330}331EXPORT_SYMBOL(drm_bridge_add);332333static void drm_bridge_remove_void(void *bridge)334{335drm_bridge_remove(bridge);336}337338/**339* devm_drm_bridge_add - devm managed version of drm_bridge_add()340*341* @dev: device to tie the bridge lifetime to342* @bridge: bridge control structure343*344* This is the managed version of drm_bridge_add() which automatically345* calls drm_bridge_remove() when @dev is unbound.346*347* Return: 0 if no error or negative error code.348*/349int devm_drm_bridge_add(struct device *dev, struct drm_bridge *bridge)350{351drm_bridge_add(bridge);352return devm_add_action_or_reset(dev, drm_bridge_remove_void, bridge);353}354EXPORT_SYMBOL(devm_drm_bridge_add);355356/**357* drm_bridge_remove - unregister a bridge358*359* @bridge: bridge control structure360*361* Remove the given bridge from the global list of registered bridges, so362* it won't be found by users via of_drm_find_bridge(), and add it to the363* lingering bridge list, to keep track of it until its allocated memory is364* eventually freed.365*/366void drm_bridge_remove(struct drm_bridge *bridge)367{368mutex_lock(&bridge_lock);369list_move_tail(&bridge->list, &bridge_lingering_list);370mutex_unlock(&bridge_lock);371372mutex_destroy(&bridge->hpd_mutex);373374drm_bridge_put(bridge);375}376EXPORT_SYMBOL(drm_bridge_remove);377378static struct drm_private_state *379drm_bridge_atomic_duplicate_priv_state(struct drm_private_obj *obj)380{381struct drm_bridge *bridge = drm_priv_to_bridge(obj);382struct drm_bridge_state *state;383384state = bridge->funcs->atomic_duplicate_state(bridge);385return state ? &state->base : NULL;386}387388static void389drm_bridge_atomic_destroy_priv_state(struct drm_private_obj *obj,390struct drm_private_state *s)391{392struct drm_bridge_state *state = drm_priv_to_bridge_state(s);393struct drm_bridge *bridge = drm_priv_to_bridge(obj);394395bridge->funcs->atomic_destroy_state(bridge, state);396}397398static const struct drm_private_state_funcs drm_bridge_priv_state_funcs = {399.atomic_duplicate_state = drm_bridge_atomic_duplicate_priv_state,400.atomic_destroy_state = drm_bridge_atomic_destroy_priv_state,401};402403static bool drm_bridge_is_atomic(struct drm_bridge *bridge)404{405return bridge->funcs->atomic_reset != NULL;406}407408/**409* drm_bridge_attach - attach the bridge to an encoder's chain410*411* @encoder: DRM encoder412* @bridge: bridge to attach413* @previous: previous bridge in the chain (optional)414* @flags: DRM_BRIDGE_ATTACH_* flags415*416* Called by a kms driver to link the bridge to an encoder's chain. The previous417* argument specifies the previous bridge in the chain. If NULL, the bridge is418* linked directly at the encoder's output. Otherwise it is linked at the419* previous bridge's output.420*421* If non-NULL the previous bridge must be already attached by a call to this422* function.423*424* The bridge to be attached must have been previously added by425* drm_bridge_add().426*427* Note that bridges attached to encoders are auto-detached during encoder428* cleanup in drm_encoder_cleanup(), so drm_bridge_attach() should generally429* *not* be balanced with a drm_bridge_detach() in driver code.430*431* RETURNS:432* Zero on success, error code on failure433*/434int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,435struct drm_bridge *previous,436enum drm_bridge_attach_flags flags)437{438int ret;439440if (!encoder || !bridge)441return -EINVAL;442443if (!bridge->container)444DRM_WARN("DRM bridge corrupted or not allocated by devm_drm_bridge_alloc()\n");445446if (list_empty(&bridge->list))447DRM_WARN("Missing drm_bridge_add() before attach\n");448449drm_bridge_get(bridge);450451if (previous && (!previous->dev || previous->encoder != encoder)) {452ret = -EINVAL;453goto err_put_bridge;454}455456if (bridge->dev) {457ret = -EBUSY;458goto err_put_bridge;459}460461bridge->dev = encoder->dev;462bridge->encoder = encoder;463464if (previous)465list_add(&bridge->chain_node, &previous->chain_node);466else467list_add(&bridge->chain_node, &encoder->bridge_chain);468469if (bridge->funcs->attach) {470ret = bridge->funcs->attach(bridge, encoder, flags);471if (ret < 0)472goto err_reset_bridge;473}474475if (drm_bridge_is_atomic(bridge)) {476struct drm_bridge_state *state;477478state = bridge->funcs->atomic_reset(bridge);479if (IS_ERR(state)) {480ret = PTR_ERR(state);481goto err_detach_bridge;482}483484drm_atomic_private_obj_init(bridge->dev, &bridge->base,485&state->base,486&drm_bridge_priv_state_funcs);487}488489return 0;490491err_detach_bridge:492if (bridge->funcs->detach)493bridge->funcs->detach(bridge);494495err_reset_bridge:496bridge->dev = NULL;497bridge->encoder = NULL;498list_del(&bridge->chain_node);499500if (ret != -EPROBE_DEFER)501DRM_ERROR("failed to attach bridge %pOF to encoder %s: %d\n",502bridge->of_node, encoder->name, ret);503else504dev_err_probe(encoder->dev->dev, -EPROBE_DEFER,505"failed to attach bridge %pOF to encoder %s\n",506bridge->of_node, encoder->name);507508err_put_bridge:509drm_bridge_put(bridge);510return ret;511}512EXPORT_SYMBOL(drm_bridge_attach);513514void drm_bridge_detach(struct drm_bridge *bridge)515{516if (WARN_ON(!bridge))517return;518519if (WARN_ON(!bridge->dev))520return;521522if (drm_bridge_is_atomic(bridge))523drm_atomic_private_obj_fini(&bridge->base);524525if (bridge->funcs->detach)526bridge->funcs->detach(bridge);527528list_del(&bridge->chain_node);529bridge->dev = NULL;530drm_bridge_put(bridge);531}532533/**534* DOC: bridge operations535*536* Bridge drivers expose operations through the &drm_bridge_funcs structure.537* The DRM internals (atomic and CRTC helpers) use the helpers defined in538* drm_bridge.c to call bridge operations. Those operations are divided in539* three big categories to support different parts of the bridge usage.540*541* - The encoder-related operations support control of the bridges in the542* chain, and are roughly counterparts to the &drm_encoder_helper_funcs543* operations. They are used by the legacy CRTC and the atomic modeset544* helpers to perform mode validation, fixup and setting, and enable and545* disable the bridge automatically.546*547* The enable and disable operations are split in548* &drm_bridge_funcs.pre_enable, &drm_bridge_funcs.enable,549* &drm_bridge_funcs.disable and &drm_bridge_funcs.post_disable to provide550* finer-grained control.551*552* Bridge drivers may implement the legacy version of those operations, or553* the atomic version (prefixed with atomic\_), in which case they shall also554* implement the atomic state bookkeeping operations555* (&drm_bridge_funcs.atomic_duplicate_state,556* &drm_bridge_funcs.atomic_destroy_state and &drm_bridge_funcs.reset).557* Mixing atomic and non-atomic versions of the operations is not supported.558*559* - The bus format negotiation operations560* &drm_bridge_funcs.atomic_get_output_bus_fmts and561* &drm_bridge_funcs.atomic_get_input_bus_fmts allow bridge drivers to562* negotiate the formats transmitted between bridges in the chain when563* multiple formats are supported. Negotiation for formats is performed564* transparently for display drivers by the atomic modeset helpers. Only565* atomic versions of those operations exist, bridge drivers that need to566* implement them shall thus also implement the atomic version of the567* encoder-related operations. This feature is not supported by the legacy568* CRTC helpers.569*570* - The connector-related operations support implementing a &drm_connector571* based on a chain of bridges. DRM bridges traditionally create a572* &drm_connector for bridges meant to be used at the end of the chain. This573* puts additional burden on bridge drivers, especially for bridges that may574* be used in the middle of a chain or at the end of it. Furthermore, it575* requires all operations of the &drm_connector to be handled by a single576* bridge, which doesn't always match the hardware architecture.577*578* To simplify bridge drivers and make the connector implementation more579* flexible, a new model allows bridges to unconditionally skip creation of580* &drm_connector and instead expose &drm_bridge_funcs operations to support581* an externally-implemented &drm_connector. Those operations are582* &drm_bridge_funcs.detect, &drm_bridge_funcs.get_modes,583* &drm_bridge_funcs.get_edid, &drm_bridge_funcs.hpd_notify,584* &drm_bridge_funcs.hpd_enable and &drm_bridge_funcs.hpd_disable. When585* implemented, display drivers shall create a &drm_connector instance for586* each chain of bridges, and implement those connector instances based on587* the bridge connector operations.588*589* Bridge drivers shall implement the connector-related operations for all590* the features that the bridge hardware support. For instance, if a bridge591* supports reading EDID, the &drm_bridge_funcs.get_edid shall be592* implemented. This however doesn't mean that the DDC lines are wired to the593* bridge on a particular platform, as they could also be connected to an I2C594* controller of the SoC. Support for the connector-related operations on the595* running platform is reported through the &drm_bridge.ops flags. Bridge596* drivers shall detect which operations they can support on the platform597* (usually this information is provided by ACPI or DT), and set the598* &drm_bridge.ops flags for all supported operations. A flag shall only be599* set if the corresponding &drm_bridge_funcs operation is implemented, but600* an implemented operation doesn't necessarily imply that the corresponding601* flag will be set. Display drivers shall use the &drm_bridge.ops flags to602* decide which bridge to delegate a connector operation to. This mechanism603* allows providing a single static const &drm_bridge_funcs instance in604* bridge drivers, improving security by storing function pointers in605* read-only memory.606*607* In order to ease transition, bridge drivers may support both the old and608* new models by making connector creation optional and implementing the609* connected-related bridge operations. Connector creation is then controlled610* by the flags argument to the drm_bridge_attach() function. Display drivers611* that support the new model and create connectors themselves shall set the612* %DRM_BRIDGE_ATTACH_NO_CONNECTOR flag, and bridge drivers shall then skip613* connector creation. For intermediate bridges in the chain, the flag shall614* be passed to the drm_bridge_attach() call for the downstream bridge.615* Bridge drivers that implement the new model only shall return an error616* from their &drm_bridge_funcs.attach handler when the617* %DRM_BRIDGE_ATTACH_NO_CONNECTOR flag is not set. New display drivers618* should use the new model, and convert the bridge drivers they use if619* needed, in order to gradually transition to the new model.620*/621622/**623* drm_bridge_chain_mode_valid - validate the mode against all bridges in the624* encoder chain.625* @bridge: bridge control structure626* @info: display info against which the mode shall be validated627* @mode: desired mode to be validated628*629* Calls &drm_bridge_funcs.mode_valid for all the bridges in the encoder630* chain, starting from the first bridge to the last. If at least one bridge631* does not accept the mode the function returns the error code.632*633* Note: the bridge passed should be the one closest to the encoder.634*635* RETURNS:636* MODE_OK on success, drm_mode_status Enum error code on failure637*/638enum drm_mode_status639drm_bridge_chain_mode_valid(struct drm_bridge *bridge,640const struct drm_display_info *info,641const struct drm_display_mode *mode)642{643struct drm_encoder *encoder;644645if (!bridge)646return MODE_OK;647648encoder = bridge->encoder;649list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {650enum drm_mode_status ret;651652if (!bridge->funcs->mode_valid)653continue;654655ret = bridge->funcs->mode_valid(bridge, info, mode);656if (ret != MODE_OK)657return ret;658}659660return MODE_OK;661}662EXPORT_SYMBOL(drm_bridge_chain_mode_valid);663664/**665* drm_bridge_chain_mode_set - set proposed mode for all bridges in the666* encoder chain667* @bridge: bridge control structure668* @mode: desired mode to be set for the encoder chain669* @adjusted_mode: updated mode that works for this encoder chain670*671* Calls &drm_bridge_funcs.mode_set op for all the bridges in the672* encoder chain, starting from the first bridge to the last.673*674* Note: the bridge passed should be the one closest to the encoder675*/676void drm_bridge_chain_mode_set(struct drm_bridge *bridge,677const struct drm_display_mode *mode,678const struct drm_display_mode *adjusted_mode)679{680struct drm_encoder *encoder;681682if (!bridge)683return;684685encoder = bridge->encoder;686list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {687if (bridge->funcs->mode_set)688bridge->funcs->mode_set(bridge, mode, adjusted_mode);689}690}691EXPORT_SYMBOL(drm_bridge_chain_mode_set);692693/**694* drm_atomic_bridge_chain_disable - disables all bridges in the encoder chain695* @bridge: bridge control structure696* @state: atomic state being committed697*698* Calls &drm_bridge_funcs.atomic_disable (falls back on699* &drm_bridge_funcs.disable) op for all the bridges in the encoder chain,700* starting from the last bridge to the first. These are called before calling701* &drm_encoder_helper_funcs.atomic_disable702*703* Note: the bridge passed should be the one closest to the encoder704*/705void drm_atomic_bridge_chain_disable(struct drm_bridge *bridge,706struct drm_atomic_state *state)707{708struct drm_encoder *encoder;709struct drm_bridge *iter;710711if (!bridge)712return;713714encoder = bridge->encoder;715list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {716if (iter->funcs->atomic_disable) {717iter->funcs->atomic_disable(iter, state);718} else if (iter->funcs->disable) {719iter->funcs->disable(iter);720}721722if (iter == bridge)723break;724}725}726EXPORT_SYMBOL(drm_atomic_bridge_chain_disable);727728static void drm_atomic_bridge_call_post_disable(struct drm_bridge *bridge,729struct drm_atomic_state *state)730{731if (state && bridge->funcs->atomic_post_disable)732bridge->funcs->atomic_post_disable(bridge, state);733else if (bridge->funcs->post_disable)734bridge->funcs->post_disable(bridge);735}736737/**738* drm_atomic_bridge_chain_post_disable - cleans up after disabling all bridges739* in the encoder chain740* @bridge: bridge control structure741* @state: atomic state being committed742*743* Calls &drm_bridge_funcs.atomic_post_disable (falls back on744* &drm_bridge_funcs.post_disable) op for all the bridges in the encoder chain,745* starting from the first bridge to the last. These are called after completing746* &drm_encoder_helper_funcs.atomic_disable747*748* If a bridge sets @pre_enable_prev_first, then the @post_disable for that749* bridge will be called before the previous one to reverse the @pre_enable750* calling direction.751*752* Example:753* Bridge A ---> Bridge B ---> Bridge C ---> Bridge D ---> Bridge E754*755* With pre_enable_prev_first flag enable in Bridge B, D, E then the resulting756* @post_disable order would be,757* Bridge B, Bridge A, Bridge E, Bridge D, Bridge C.758*759* Note: the bridge passed should be the one closest to the encoder760*/761void drm_atomic_bridge_chain_post_disable(struct drm_bridge *bridge,762struct drm_atomic_state *state)763{764struct drm_encoder *encoder;765struct drm_bridge *next, *limit;766767if (!bridge)768return;769770encoder = bridge->encoder;771772list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {773limit = NULL;774775if (!list_is_last(&bridge->chain_node, &encoder->bridge_chain)) {776next = list_next_entry(bridge, chain_node);777778if (next->pre_enable_prev_first) {779/* next bridge had requested that prev780* was enabled first, so disabled last781*/782limit = next;783784/* Find the next bridge that has NOT requested785* prev to be enabled first / disabled last786*/787list_for_each_entry_from(next, &encoder->bridge_chain,788chain_node) {789if (!next->pre_enable_prev_first) {790next = list_prev_entry(next, chain_node);791limit = next;792break;793}794795if (list_is_last(&next->chain_node,796&encoder->bridge_chain)) {797limit = next;798break;799}800}801802/* Call these bridges in reverse order */803list_for_each_entry_from_reverse(next, &encoder->bridge_chain,804chain_node) {805if (next == bridge)806break;807808drm_atomic_bridge_call_post_disable(next,809state);810}811}812}813814drm_atomic_bridge_call_post_disable(bridge, state);815816if (limit)817/* Jump all bridges that we have already post_disabled */818bridge = limit;819}820}821EXPORT_SYMBOL(drm_atomic_bridge_chain_post_disable);822823static void drm_atomic_bridge_call_pre_enable(struct drm_bridge *bridge,824struct drm_atomic_state *state)825{826if (state && bridge->funcs->atomic_pre_enable)827bridge->funcs->atomic_pre_enable(bridge, state);828else if (bridge->funcs->pre_enable)829bridge->funcs->pre_enable(bridge);830}831832/**833* drm_atomic_bridge_chain_pre_enable - prepares for enabling all bridges in834* the encoder chain835* @bridge: bridge control structure836* @state: atomic state being committed837*838* Calls &drm_bridge_funcs.atomic_pre_enable (falls back on839* &drm_bridge_funcs.pre_enable) op for all the bridges in the encoder chain,840* starting from the last bridge to the first. These are called before calling841* &drm_encoder_helper_funcs.atomic_enable842*843* If a bridge sets @pre_enable_prev_first, then the pre_enable for the844* prev bridge will be called before pre_enable of this bridge.845*846* Example:847* Bridge A ---> Bridge B ---> Bridge C ---> Bridge D ---> Bridge E848*849* With pre_enable_prev_first flag enable in Bridge B, D, E then the resulting850* @pre_enable order would be,851* Bridge C, Bridge D, Bridge E, Bridge A, Bridge B.852*853* Note: the bridge passed should be the one closest to the encoder854*/855void drm_atomic_bridge_chain_pre_enable(struct drm_bridge *bridge,856struct drm_atomic_state *state)857{858struct drm_encoder *encoder;859struct drm_bridge *iter, *next, *limit;860861if (!bridge)862return;863864encoder = bridge->encoder;865866list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {867if (iter->pre_enable_prev_first) {868next = iter;869limit = bridge;870list_for_each_entry_from_reverse(next,871&encoder->bridge_chain,872chain_node) {873if (next == bridge)874break;875876if (!next->pre_enable_prev_first) {877/* Found first bridge that does NOT878* request prev to be enabled first879*/880limit = next;881break;882}883}884885list_for_each_entry_from(next, &encoder->bridge_chain, chain_node) {886/* Call requested prev bridge pre_enable887* in order.888*/889if (next == iter)890/* At the first bridge to request prev891* bridges called first.892*/893break;894895drm_atomic_bridge_call_pre_enable(next, state);896}897}898899drm_atomic_bridge_call_pre_enable(iter, state);900901if (iter->pre_enable_prev_first)902/* Jump all bridges that we have already pre_enabled */903iter = limit;904905if (iter == bridge)906break;907}908}909EXPORT_SYMBOL(drm_atomic_bridge_chain_pre_enable);910911/**912* drm_atomic_bridge_chain_enable - enables all bridges in the encoder chain913* @bridge: bridge control structure914* @state: atomic state being committed915*916* Calls &drm_bridge_funcs.atomic_enable (falls back on917* &drm_bridge_funcs.enable) op for all the bridges in the encoder chain,918* starting from the first bridge to the last. These are called after completing919* &drm_encoder_helper_funcs.atomic_enable920*921* Note: the bridge passed should be the one closest to the encoder922*/923void drm_atomic_bridge_chain_enable(struct drm_bridge *bridge,924struct drm_atomic_state *state)925{926struct drm_encoder *encoder;927928if (!bridge)929return;930931encoder = bridge->encoder;932list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {933if (bridge->funcs->atomic_enable) {934bridge->funcs->atomic_enable(bridge, state);935} else if (bridge->funcs->enable) {936bridge->funcs->enable(bridge);937}938}939}940EXPORT_SYMBOL(drm_atomic_bridge_chain_enable);941942static int drm_atomic_bridge_check(struct drm_bridge *bridge,943struct drm_crtc_state *crtc_state,944struct drm_connector_state *conn_state)945{946if (bridge->funcs->atomic_check) {947struct drm_bridge_state *bridge_state;948int ret;949950bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state,951bridge);952if (WARN_ON(!bridge_state))953return -EINVAL;954955ret = bridge->funcs->atomic_check(bridge, bridge_state,956crtc_state, conn_state);957if (ret)958return ret;959} else if (bridge->funcs->mode_fixup) {960if (!bridge->funcs->mode_fixup(bridge, &crtc_state->mode,961&crtc_state->adjusted_mode))962return -EINVAL;963}964965return 0;966}967968static int select_bus_fmt_recursive(struct drm_bridge *first_bridge,969struct drm_bridge *cur_bridge,970struct drm_crtc_state *crtc_state,971struct drm_connector_state *conn_state,972u32 out_bus_fmt)973{974unsigned int i, num_in_bus_fmts = 0;975struct drm_bridge_state *cur_state;976struct drm_bridge *prev_bridge __free(drm_bridge_put) =977drm_bridge_get_prev_bridge(cur_bridge);978u32 *in_bus_fmts;979int ret;980981cur_state = drm_atomic_get_new_bridge_state(crtc_state->state,982cur_bridge);983984/*985* If bus format negotiation is not supported by this bridge, let's986* pass MEDIA_BUS_FMT_FIXED to the previous bridge in the chain and987* hope that it can handle this situation gracefully (by providing988* appropriate default values).989*/990if (!cur_bridge->funcs->atomic_get_input_bus_fmts) {991if (cur_bridge != first_bridge) {992ret = select_bus_fmt_recursive(first_bridge,993prev_bridge, crtc_state,994conn_state,995MEDIA_BUS_FMT_FIXED);996if (ret)997return ret;998}9991000/*1001* Driver does not implement the atomic state hooks, but that's1002* fine, as long as it does not access the bridge state.1003*/1004if (cur_state) {1005cur_state->input_bus_cfg.format = MEDIA_BUS_FMT_FIXED;1006cur_state->output_bus_cfg.format = out_bus_fmt;1007}10081009return 0;1010}10111012/*1013* If the driver implements ->atomic_get_input_bus_fmts() it1014* should also implement the atomic state hooks.1015*/1016if (WARN_ON(!cur_state))1017return -EINVAL;10181019in_bus_fmts = cur_bridge->funcs->atomic_get_input_bus_fmts(cur_bridge,1020cur_state,1021crtc_state,1022conn_state,1023out_bus_fmt,1024&num_in_bus_fmts);1025if (!num_in_bus_fmts)1026return -ENOTSUPP;1027else if (!in_bus_fmts)1028return -ENOMEM;10291030if (first_bridge == cur_bridge) {1031cur_state->input_bus_cfg.format = in_bus_fmts[0];1032cur_state->output_bus_cfg.format = out_bus_fmt;1033kfree(in_bus_fmts);1034return 0;1035}10361037for (i = 0; i < num_in_bus_fmts; i++) {1038ret = select_bus_fmt_recursive(first_bridge, prev_bridge,1039crtc_state, conn_state,1040in_bus_fmts[i]);1041if (ret != -ENOTSUPP)1042break;1043}10441045if (!ret) {1046cur_state->input_bus_cfg.format = in_bus_fmts[i];1047cur_state->output_bus_cfg.format = out_bus_fmt;1048}10491050kfree(in_bus_fmts);1051return ret;1052}10531054/*1055* This function is called by &drm_atomic_bridge_chain_check() just before1056* calling &drm_bridge_funcs.atomic_check() on all elements of the chain.1057* It performs bus format negotiation between bridge elements. The negotiation1058* happens in reverse order, starting from the last element in the chain up to1059* @bridge.1060*1061* Negotiation starts by retrieving supported output bus formats on the last1062* bridge element and testing them one by one. The test is recursive, meaning1063* that for each tested output format, the whole chain will be walked backward,1064* and each element will have to choose an input bus format that can be1065* transcoded to the requested output format. When a bridge element does not1066* support transcoding into a specific output format -ENOTSUPP is returned and1067* the next bridge element will have to try a different format. If none of the1068* combinations worked, -ENOTSUPP is returned and the atomic modeset will fail.1069*1070* This implementation is relying on1071* &drm_bridge_funcs.atomic_get_output_bus_fmts() and1072* &drm_bridge_funcs.atomic_get_input_bus_fmts() to gather supported1073* input/output formats.1074*1075* When &drm_bridge_funcs.atomic_get_output_bus_fmts() is not implemented by1076* the last element of the chain, &drm_atomic_bridge_chain_select_bus_fmts()1077* tries a single format: &drm_connector.display_info.bus_formats[0] if1078* available, MEDIA_BUS_FMT_FIXED otherwise.1079*1080* When &drm_bridge_funcs.atomic_get_input_bus_fmts() is not implemented,1081* &drm_atomic_bridge_chain_select_bus_fmts() skips the negotiation on the1082* bridge element that lacks this hook and asks the previous element in the1083* chain to try MEDIA_BUS_FMT_FIXED. It's up to bridge drivers to decide what1084* to do in that case (fail if they want to enforce bus format negotiation, or1085* provide a reasonable default if they need to support pipelines where not1086* all elements support bus format negotiation).1087*/1088static int1089drm_atomic_bridge_chain_select_bus_fmts(struct drm_bridge *bridge,1090struct drm_crtc_state *crtc_state,1091struct drm_connector_state *conn_state)1092{1093struct drm_connector *conn = conn_state->connector;1094struct drm_encoder *encoder = bridge->encoder;1095struct drm_bridge_state *last_bridge_state;1096unsigned int i, num_out_bus_fmts = 0;1097u32 *out_bus_fmts;1098int ret = 0;10991100struct drm_bridge *last_bridge __free(drm_bridge_put) =1101drm_bridge_get(list_last_entry(&encoder->bridge_chain,1102struct drm_bridge, chain_node));1103last_bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state,1104last_bridge);11051106if (last_bridge->funcs->atomic_get_output_bus_fmts) {1107const struct drm_bridge_funcs *funcs = last_bridge->funcs;11081109/*1110* If the driver implements ->atomic_get_output_bus_fmts() it1111* should also implement the atomic state hooks.1112*/1113if (WARN_ON(!last_bridge_state))1114return -EINVAL;11151116out_bus_fmts = funcs->atomic_get_output_bus_fmts(last_bridge,1117last_bridge_state,1118crtc_state,1119conn_state,1120&num_out_bus_fmts);1121if (!num_out_bus_fmts)1122return -ENOTSUPP;1123else if (!out_bus_fmts)1124return -ENOMEM;1125} else {1126num_out_bus_fmts = 1;1127out_bus_fmts = kmalloc(sizeof(*out_bus_fmts), GFP_KERNEL);1128if (!out_bus_fmts)1129return -ENOMEM;11301131if (conn->display_info.num_bus_formats &&1132conn->display_info.bus_formats)1133out_bus_fmts[0] = conn->display_info.bus_formats[0];1134else1135out_bus_fmts[0] = MEDIA_BUS_FMT_FIXED;1136}11371138for (i = 0; i < num_out_bus_fmts; i++) {1139ret = select_bus_fmt_recursive(bridge, last_bridge, crtc_state,1140conn_state, out_bus_fmts[i]);1141if (ret != -ENOTSUPP)1142break;1143}11441145kfree(out_bus_fmts);11461147return ret;1148}11491150static void1151drm_atomic_bridge_propagate_bus_flags(struct drm_bridge *bridge,1152struct drm_connector *conn,1153struct drm_atomic_state *state)1154{1155struct drm_bridge_state *bridge_state, *next_bridge_state;1156u32 output_flags = 0;11571158bridge_state = drm_atomic_get_new_bridge_state(state, bridge);11591160/* No bridge state attached to this bridge => nothing to propagate. */1161if (!bridge_state)1162return;11631164struct drm_bridge *next_bridge __free(drm_bridge_put) = drm_bridge_get_next_bridge(bridge);11651166/*1167* Let's try to apply the most common case here, that is, propagate1168* display_info flags for the last bridge, and propagate the input1169* flags of the next bridge element to the output end of the current1170* bridge when the bridge is not the last one.1171* There are exceptions to this rule, like when signal inversion is1172* happening at the board level, but that's something drivers can deal1173* with from their &drm_bridge_funcs.atomic_check() implementation by1174* simply overriding the flags value we've set here.1175*/1176if (!next_bridge) {1177output_flags = conn->display_info.bus_flags;1178} else {1179next_bridge_state = drm_atomic_get_new_bridge_state(state,1180next_bridge);1181/*1182* No bridge state attached to the next bridge, just leave the1183* flags to 0.1184*/1185if (next_bridge_state)1186output_flags = next_bridge_state->input_bus_cfg.flags;1187}11881189bridge_state->output_bus_cfg.flags = output_flags;11901191/*1192* Propagate the output flags to the input end of the bridge. Again, it's1193* not necessarily what all bridges want, but that's what most of them1194* do, and by doing that by default we avoid forcing drivers to1195* duplicate the "dummy propagation" logic.1196*/1197bridge_state->input_bus_cfg.flags = output_flags;1198}11991200/**1201* drm_atomic_bridge_chain_check() - Do an atomic check on the bridge chain1202* @bridge: bridge control structure1203* @crtc_state: new CRTC state1204* @conn_state: new connector state1205*1206* First trigger a bus format negotiation before calling1207* &drm_bridge_funcs.atomic_check() (falls back on1208* &drm_bridge_funcs.mode_fixup()) op for all the bridges in the encoder chain,1209* starting from the last bridge to the first. These are called before calling1210* &drm_encoder_helper_funcs.atomic_check()1211*1212* RETURNS:1213* 0 on success, a negative error code on failure1214*/1215int drm_atomic_bridge_chain_check(struct drm_bridge *bridge,1216struct drm_crtc_state *crtc_state,1217struct drm_connector_state *conn_state)1218{1219struct drm_connector *conn = conn_state->connector;1220struct drm_encoder *encoder;1221struct drm_bridge *iter;1222int ret;12231224if (!bridge)1225return 0;12261227ret = drm_atomic_bridge_chain_select_bus_fmts(bridge, crtc_state,1228conn_state);1229if (ret)1230return ret;12311232encoder = bridge->encoder;1233list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {1234int ret;12351236/*1237* Bus flags are propagated by default. If a bridge needs to1238* tweak the input bus flags for any reason, it should happen1239* in its &drm_bridge_funcs.atomic_check() implementation such1240* that preceding bridges in the chain can propagate the new1241* bus flags.1242*/1243drm_atomic_bridge_propagate_bus_flags(iter, conn,1244crtc_state->state);12451246ret = drm_atomic_bridge_check(iter, crtc_state, conn_state);1247if (ret)1248return ret;12491250if (iter == bridge)1251break;1252}12531254return 0;1255}1256EXPORT_SYMBOL(drm_atomic_bridge_chain_check);12571258/**1259* drm_bridge_detect - check if anything is attached to the bridge output1260* @bridge: bridge control structure1261* @connector: attached connector1262*1263* If the bridge supports output detection, as reported by the1264* DRM_BRIDGE_OP_DETECT bridge ops flag, call &drm_bridge_funcs.detect for the1265* bridge and return the connection status. Otherwise return1266* connector_status_unknown.1267*1268* RETURNS:1269* The detection status on success, or connector_status_unknown if the bridge1270* doesn't support output detection.1271*/1272enum drm_connector_status1273drm_bridge_detect(struct drm_bridge *bridge, struct drm_connector *connector)1274{1275if (!(bridge->ops & DRM_BRIDGE_OP_DETECT))1276return connector_status_unknown;12771278return bridge->funcs->detect(bridge, connector);1279}1280EXPORT_SYMBOL_GPL(drm_bridge_detect);12811282/**1283* drm_bridge_get_modes - fill all modes currently valid for the sink into the1284* @connector1285* @bridge: bridge control structure1286* @connector: the connector to fill with modes1287*1288* If the bridge supports output modes retrieval, as reported by the1289* DRM_BRIDGE_OP_MODES bridge ops flag, call &drm_bridge_funcs.get_modes to1290* fill the connector with all valid modes and return the number of modes1291* added. Otherwise return 0.1292*1293* RETURNS:1294* The number of modes added to the connector.1295*/1296int drm_bridge_get_modes(struct drm_bridge *bridge,1297struct drm_connector *connector)1298{1299if (!(bridge->ops & DRM_BRIDGE_OP_MODES))1300return 0;13011302return bridge->funcs->get_modes(bridge, connector);1303}1304EXPORT_SYMBOL_GPL(drm_bridge_get_modes);13051306/**1307* drm_bridge_edid_read - read the EDID data of the connected display1308* @bridge: bridge control structure1309* @connector: the connector to read EDID for1310*1311* If the bridge supports output EDID retrieval, as reported by the1312* DRM_BRIDGE_OP_EDID bridge ops flag, call &drm_bridge_funcs.edid_read to get1313* the EDID and return it. Otherwise return NULL.1314*1315* RETURNS:1316* The retrieved EDID on success, or NULL otherwise.1317*/1318const struct drm_edid *drm_bridge_edid_read(struct drm_bridge *bridge,1319struct drm_connector *connector)1320{1321if (!(bridge->ops & DRM_BRIDGE_OP_EDID))1322return NULL;13231324return bridge->funcs->edid_read(bridge, connector);1325}1326EXPORT_SYMBOL_GPL(drm_bridge_edid_read);13271328/**1329* drm_bridge_hpd_enable - enable hot plug detection for the bridge1330* @bridge: bridge control structure1331* @cb: hot-plug detection callback1332* @data: data to be passed to the hot-plug detection callback1333*1334* Call &drm_bridge_funcs.hpd_enable if implemented and register the given @cb1335* and @data as hot plug notification callback. From now on the @cb will be1336* called with @data when an output status change is detected by the bridge,1337* until hot plug notification gets disabled with drm_bridge_hpd_disable().1338*1339* Hot plug detection is supported only if the DRM_BRIDGE_OP_HPD flag is set in1340* bridge->ops. This function shall not be called when the flag is not set.1341*1342* Only one hot plug detection callback can be registered at a time, it is an1343* error to call this function when hot plug detection is already enabled for1344* the bridge.1345*/1346void drm_bridge_hpd_enable(struct drm_bridge *bridge,1347void (*cb)(void *data,1348enum drm_connector_status status),1349void *data)1350{1351if (!(bridge->ops & DRM_BRIDGE_OP_HPD))1352return;13531354mutex_lock(&bridge->hpd_mutex);13551356if (WARN(bridge->hpd_cb, "Hot plug detection already enabled\n"))1357goto unlock;13581359bridge->hpd_cb = cb;1360bridge->hpd_data = data;13611362if (bridge->funcs->hpd_enable)1363bridge->funcs->hpd_enable(bridge);13641365unlock:1366mutex_unlock(&bridge->hpd_mutex);1367}1368EXPORT_SYMBOL_GPL(drm_bridge_hpd_enable);13691370/**1371* drm_bridge_hpd_disable - disable hot plug detection for the bridge1372* @bridge: bridge control structure1373*1374* Call &drm_bridge_funcs.hpd_disable if implemented and unregister the hot1375* plug detection callback previously registered with drm_bridge_hpd_enable().1376* Once this function returns the callback will not be called by the bridge1377* when an output status change occurs.1378*1379* Hot plug detection is supported only if the DRM_BRIDGE_OP_HPD flag is set in1380* bridge->ops. This function shall not be called when the flag is not set.1381*/1382void drm_bridge_hpd_disable(struct drm_bridge *bridge)1383{1384if (!(bridge->ops & DRM_BRIDGE_OP_HPD))1385return;13861387mutex_lock(&bridge->hpd_mutex);1388if (bridge->funcs->hpd_disable)1389bridge->funcs->hpd_disable(bridge);13901391bridge->hpd_cb = NULL;1392bridge->hpd_data = NULL;1393mutex_unlock(&bridge->hpd_mutex);1394}1395EXPORT_SYMBOL_GPL(drm_bridge_hpd_disable);13961397/**1398* drm_bridge_hpd_notify - notify hot plug detection events1399* @bridge: bridge control structure1400* @status: output connection status1401*1402* Bridge drivers shall call this function to report hot plug events when they1403* detect a change in the output status, when hot plug detection has been1404* enabled by drm_bridge_hpd_enable().1405*1406* This function shall be called in a context that can sleep.1407*/1408void drm_bridge_hpd_notify(struct drm_bridge *bridge,1409enum drm_connector_status status)1410{1411mutex_lock(&bridge->hpd_mutex);1412if (bridge->hpd_cb)1413bridge->hpd_cb(bridge->hpd_data, status);1414mutex_unlock(&bridge->hpd_mutex);1415}1416EXPORT_SYMBOL_GPL(drm_bridge_hpd_notify);14171418#ifdef CONFIG_OF1419/**1420* of_drm_find_bridge - find the bridge corresponding to the device node in1421* the global bridge list1422*1423* @np: device node1424*1425* RETURNS:1426* drm_bridge control struct on success, NULL on failure1427*/1428struct drm_bridge *of_drm_find_bridge(struct device_node *np)1429{1430struct drm_bridge *bridge;14311432mutex_lock(&bridge_lock);14331434list_for_each_entry(bridge, &bridge_list, list) {1435if (bridge->of_node == np) {1436mutex_unlock(&bridge_lock);1437return bridge;1438}1439}14401441mutex_unlock(&bridge_lock);1442return NULL;1443}1444EXPORT_SYMBOL(of_drm_find_bridge);1445#endif14461447/**1448* devm_drm_put_bridge - Release a bridge reference obtained via devm1449* @dev: device that got the bridge via devm1450* @bridge: pointer to a struct drm_bridge obtained via devm1451*1452* Same as drm_bridge_put() for bridge pointers obtained via devm functions1453* such as devm_drm_bridge_alloc().1454*1455* This function is a temporary workaround and MUST NOT be used. Manual1456* handling of bridge lifetime is inherently unsafe.1457*/1458void devm_drm_put_bridge(struct device *dev, struct drm_bridge *bridge)1459{1460devm_release_action(dev, drm_bridge_put_void, bridge);1461}1462EXPORT_SYMBOL(devm_drm_put_bridge);14631464static void drm_bridge_debugfs_show_bridge(struct drm_printer *p,1465struct drm_bridge *bridge,1466unsigned int idx,1467bool lingering)1468{1469drm_printf(p, "bridge[%u]: %ps\n", idx, bridge->funcs);14701471drm_printf(p, "\trefcount: %u%s\n", kref_read(&bridge->refcount),1472lingering ? " [lingering]" : "");14731474drm_printf(p, "\ttype: [%d] %s\n",1475bridge->type,1476drm_get_connector_type_name(bridge->type));14771478/* The OF node could be freed after drm_bridge_remove() */1479if (bridge->of_node && !lingering)1480drm_printf(p, "\tOF: %pOFfc\n", bridge->of_node);14811482drm_printf(p, "\tops: [0x%x]", bridge->ops);1483if (bridge->ops & DRM_BRIDGE_OP_DETECT)1484drm_puts(p, " detect");1485if (bridge->ops & DRM_BRIDGE_OP_EDID)1486drm_puts(p, " edid");1487if (bridge->ops & DRM_BRIDGE_OP_HPD)1488drm_puts(p, " hpd");1489if (bridge->ops & DRM_BRIDGE_OP_MODES)1490drm_puts(p, " modes");1491if (bridge->ops & DRM_BRIDGE_OP_HDMI)1492drm_puts(p, " hdmi");1493drm_puts(p, "\n");1494}14951496static int allbridges_show(struct seq_file *m, void *data)1497{1498struct drm_printer p = drm_seq_file_printer(m);1499struct drm_bridge *bridge;1500unsigned int idx = 0;15011502mutex_lock(&bridge_lock);15031504list_for_each_entry(bridge, &bridge_list, list)1505drm_bridge_debugfs_show_bridge(&p, bridge, idx++, false);15061507list_for_each_entry(bridge, &bridge_lingering_list, list)1508drm_bridge_debugfs_show_bridge(&p, bridge, idx++, true);15091510mutex_unlock(&bridge_lock);15111512return 0;1513}1514DEFINE_SHOW_ATTRIBUTE(allbridges);15151516static int encoder_bridges_show(struct seq_file *m, void *data)1517{1518struct drm_encoder *encoder = m->private;1519struct drm_printer p = drm_seq_file_printer(m);1520unsigned int idx = 0;15211522drm_for_each_bridge_in_chain_scoped(encoder, bridge)1523drm_bridge_debugfs_show_bridge(&p, bridge, idx++, false);15241525return 0;1526}1527DEFINE_SHOW_ATTRIBUTE(encoder_bridges);15281529void drm_bridge_debugfs_params(struct dentry *root)1530{1531debugfs_create_file("bridges", 0444, root, NULL, &allbridges_fops);1532}15331534void drm_bridge_debugfs_encoder_params(struct dentry *root,1535struct drm_encoder *encoder)1536{1537/* bridges list */1538debugfs_create_file("bridges", 0444, root, encoder, &encoder_bridges_fops);1539}15401541MODULE_AUTHOR("Ajay Kumar <[email protected]>");1542MODULE_DESCRIPTION("DRM bridge infrastructure");1543MODULE_LICENSE("GPL and additional rights");154415451546