Home | History | Annotate | Line # | Download | only in drm
drm_bridge.c revision 1.3.6.3
      1  1.3.6.3    martin /*	$NetBSD: drm_bridge.c,v 1.3.6.3 2020/04/08 14:08:21 martin Exp $	*/
      2  1.3.6.2  christos 
      3  1.3.6.2  christos /*
      4  1.3.6.2  christos  * Copyright (c) 2014 Samsung Electronics Co., Ltd
      5  1.3.6.2  christos  *
      6  1.3.6.2  christos  * Permission is hereby granted, free of charge, to any person obtaining a
      7  1.3.6.2  christos  * copy of this software and associated documentation files (the "Software"),
      8  1.3.6.2  christos  * to deal in the Software without restriction, including without limitation
      9  1.3.6.2  christos  * the rights to use, copy, modify, merge, publish, distribute, sub license,
     10  1.3.6.2  christos  * and/or sell copies of the Software, and to permit persons to whom the
     11  1.3.6.2  christos  * Software is furnished to do so, subject to the following conditions:
     12  1.3.6.2  christos  *
     13  1.3.6.2  christos  * The above copyright notice and this permission notice (including the
     14  1.3.6.2  christos  * next paragraph) shall be included in all copies or substantial portions
     15  1.3.6.2  christos  * of the Software.
     16  1.3.6.2  christos  *
     17  1.3.6.2  christos  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18  1.3.6.2  christos  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19  1.3.6.2  christos  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
     20  1.3.6.2  christos  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     21  1.3.6.2  christos  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     22  1.3.6.2  christos  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     23  1.3.6.2  christos  * DEALINGS IN THE SOFTWARE.
     24  1.3.6.2  christos  */
     25  1.3.6.2  christos 
     26  1.3.6.2  christos #include <sys/cdefs.h>
     27  1.3.6.3    martin __KERNEL_RCSID(0, "$NetBSD: drm_bridge.c,v 1.3.6.3 2020/04/08 14:08:21 martin Exp $");
     28  1.3.6.2  christos 
     29  1.3.6.2  christos #include <linux/err.h>
     30  1.3.6.2  christos #include <linux/module.h>
     31  1.3.6.2  christos 
     32  1.3.6.2  christos #include <drm/drm_crtc.h>
     33  1.3.6.2  christos 
     34  1.3.6.2  christos #include "drm/drmP.h"
     35  1.3.6.2  christos 
     36  1.3.6.2  christos /**
     37  1.3.6.2  christos  * DOC: overview
     38  1.3.6.2  christos  *
     39  1.3.6.2  christos  * drm_bridge represents a device that hangs on to an encoder. These are handy
     40  1.3.6.2  christos  * when a regular drm_encoder entity isn't enough to represent the entire
     41  1.3.6.2  christos  * encoder chain.
     42  1.3.6.2  christos  *
     43  1.3.6.2  christos  * A bridge is always associated to a single drm_encoder at a time, but can be
     44  1.3.6.2  christos  * either connected to it directly, or through an intermediate bridge:
     45  1.3.6.2  christos  *
     46  1.3.6.2  christos  * encoder ---> bridge B ---> bridge A
     47  1.3.6.2  christos  *
     48  1.3.6.2  christos  * Here, the output of the encoder feeds to bridge B, and that furthers feeds to
     49  1.3.6.2  christos  * bridge A.
     50  1.3.6.2  christos  *
     51  1.3.6.2  christos  * The driver using the bridge is responsible to make the associations between
     52  1.3.6.2  christos  * the encoder and bridges. Once these links are made, the bridges will
     53  1.3.6.2  christos  * participate along with encoder functions to perform mode_set/enable/disable
     54  1.3.6.2  christos  * through the ops provided in drm_bridge_funcs.
     55  1.3.6.2  christos  *
     56  1.3.6.2  christos  * drm_bridge, like drm_panel, aren't drm_mode_object entities like planes,
     57  1.3.6.2  christos  * crtcs, encoders or connectors. They just provide additional hooks to get the
     58  1.3.6.2  christos  * desired output at the end of the encoder chain.
     59  1.3.6.2  christos  */
     60  1.3.6.2  christos 
     61  1.3.6.2  christos #ifdef __NetBSD__
     62  1.3.6.2  christos static struct mutex bridge_lock;
     63  1.3.6.2  christos static struct list_head bridge_list = LIST_HEAD_INIT(bridge_list);
     64  1.3.6.2  christos #else
     65  1.3.6.2  christos static DEFINE_MUTEX(bridge_lock);
     66  1.3.6.2  christos static LIST_HEAD(bridge_list);
     67  1.3.6.2  christos #endif
     68  1.3.6.2  christos 
     69  1.3.6.3    martin #ifdef __NetBSD__
     70  1.3.6.3    martin void drm_bridge_init_lock(void)
     71  1.3.6.3    martin {
     72  1.3.6.3    martin 	linux_mutex_init(&bridge_lock);
     73  1.3.6.3    martin }
     74  1.3.6.3    martin void drm_bridge_fini_lock(void)
     75  1.3.6.3    martin {
     76  1.3.6.3    martin 	linux_mutex_destroy(&bridge_lock);
     77  1.3.6.3    martin }
     78  1.3.6.3    martin #endif
     79  1.3.6.3    martin 
     80  1.3.6.2  christos /**
     81  1.3.6.2  christos  * drm_bridge_add - add the given bridge to the global bridge list
     82  1.3.6.2  christos  *
     83  1.3.6.2  christos  * @bridge: bridge control structure
     84  1.3.6.2  christos  *
     85  1.3.6.2  christos  * RETURNS:
     86  1.3.6.2  christos  * Unconditionally returns Zero.
     87  1.3.6.2  christos  */
     88  1.3.6.2  christos int drm_bridge_add(struct drm_bridge *bridge)
     89  1.3.6.2  christos {
     90  1.3.6.2  christos 	mutex_lock(&bridge_lock);
     91  1.3.6.2  christos 	list_add_tail(&bridge->list, &bridge_list);
     92  1.3.6.2  christos 	mutex_unlock(&bridge_lock);
     93  1.3.6.2  christos 
     94  1.3.6.2  christos 	return 0;
     95  1.3.6.2  christos }
     96  1.3.6.2  christos EXPORT_SYMBOL(drm_bridge_add);
     97  1.3.6.2  christos 
     98  1.3.6.2  christos /**
     99  1.3.6.2  christos  * drm_bridge_remove - remove the given bridge from the global bridge list
    100  1.3.6.2  christos  *
    101  1.3.6.2  christos  * @bridge: bridge control structure
    102  1.3.6.2  christos  */
    103  1.3.6.2  christos void drm_bridge_remove(struct drm_bridge *bridge)
    104  1.3.6.2  christos {
    105  1.3.6.2  christos 	mutex_lock(&bridge_lock);
    106  1.3.6.2  christos 	list_del_init(&bridge->list);
    107  1.3.6.2  christos 	mutex_unlock(&bridge_lock);
    108  1.3.6.2  christos }
    109  1.3.6.2  christos EXPORT_SYMBOL(drm_bridge_remove);
    110  1.3.6.2  christos 
    111  1.3.6.2  christos /**
    112  1.3.6.2  christos  * drm_bridge_attach - associate given bridge to our DRM device
    113  1.3.6.2  christos  *
    114  1.3.6.2  christos  * @dev: DRM device
    115  1.3.6.2  christos  * @bridge: bridge control structure
    116  1.3.6.2  christos  *
    117  1.3.6.2  christos  * called by a kms driver to link one of our encoder/bridge to the given
    118  1.3.6.2  christos  * bridge.
    119  1.3.6.2  christos  *
    120  1.3.6.2  christos  * Note that setting up links between the bridge and our encoder/bridge
    121  1.3.6.2  christos  * objects needs to be handled by the kms driver itself
    122  1.3.6.2  christos  *
    123  1.3.6.2  christos  * RETURNS:
    124  1.3.6.2  christos  * Zero on success, error code on failure
    125  1.3.6.2  christos  */
    126  1.3.6.2  christos int drm_bridge_attach(struct drm_device *dev, struct drm_bridge *bridge)
    127  1.3.6.2  christos {
    128  1.3.6.2  christos 	if (!dev || !bridge)
    129  1.3.6.2  christos 		return -EINVAL;
    130  1.3.6.2  christos 
    131  1.3.6.2  christos 	if (bridge->dev)
    132  1.3.6.2  christos 		return -EBUSY;
    133  1.3.6.2  christos 
    134  1.3.6.2  christos 	bridge->dev = dev;
    135  1.3.6.2  christos 
    136  1.3.6.2  christos 	if (bridge->funcs->attach)
    137  1.3.6.2  christos 		return bridge->funcs->attach(bridge);
    138  1.3.6.2  christos 
    139  1.3.6.2  christos 	return 0;
    140  1.3.6.2  christos }
    141  1.3.6.2  christos EXPORT_SYMBOL(drm_bridge_attach);
    142  1.3.6.2  christos 
    143  1.3.6.2  christos /**
    144  1.3.6.2  christos  * DOC: bridge callbacks
    145  1.3.6.2  christos  *
    146  1.3.6.2  christos  * The drm_bridge_funcs ops are populated by the bridge driver. The drm
    147  1.3.6.2  christos  * internals(atomic and crtc helpers) use the helpers defined in drm_bridge.c
    148  1.3.6.2  christos  * These helpers call a specific drm_bridge_funcs op for all the bridges
    149  1.3.6.2  christos  * during encoder configuration.
    150  1.3.6.2  christos  *
    151  1.3.6.2  christos  * When creating a bridge driver, one can implement drm_bridge_funcs op with
    152  1.3.6.2  christos  * the help of these rough rules:
    153  1.3.6.2  christos  *
    154  1.3.6.2  christos  * pre_enable: this contains things needed to be done for the bridge before
    155  1.3.6.2  christos  * its clock and timings are enabled by its source. For a bridge, its source
    156  1.3.6.2  christos  * is generally the encoder or bridge just before it in the encoder chain.
    157  1.3.6.2  christos  *
    158  1.3.6.2  christos  * enable: this contains things needed to be done for the bridge once its
    159  1.3.6.2  christos  * source is enabled. In other words, enable is called once the source is
    160  1.3.6.2  christos  * ready with clock and timing needed by the bridge.
    161  1.3.6.2  christos  *
    162  1.3.6.2  christos  * disable: this contains things needed to be done for the bridge assuming
    163  1.3.6.2  christos  * that its source is still enabled, i.e. clock and timings are still on.
    164  1.3.6.2  christos  *
    165  1.3.6.2  christos  * post_disable: this contains things needed to be done for the bridge once
    166  1.3.6.2  christos  * its source is disabled, i.e. once clocks and timings are off.
    167  1.3.6.2  christos  *
    168  1.3.6.2  christos  * mode_fixup: this should fixup the given mode for the bridge. It is called
    169  1.3.6.2  christos  * after the encoder's mode fixup. mode_fixup can also reject a mode completely
    170  1.3.6.2  christos  * if it's unsuitable for the hardware.
    171  1.3.6.2  christos  *
    172  1.3.6.2  christos  * mode_set: this sets up the mode for the bridge. It assumes that its source
    173  1.3.6.2  christos  * (an encoder or a bridge) has set the mode too.
    174  1.3.6.2  christos  */
    175  1.3.6.2  christos 
    176  1.3.6.2  christos /**
    177  1.3.6.2  christos  * drm_bridge_mode_fixup - fixup proposed mode for all bridges in the
    178  1.3.6.2  christos  *			   encoder chain
    179  1.3.6.2  christos  * @bridge: bridge control structure
    180  1.3.6.2  christos  * @mode: desired mode to be set for the bridge
    181  1.3.6.2  christos  * @adjusted_mode: updated mode that works for this bridge
    182  1.3.6.2  christos  *
    183  1.3.6.2  christos  * Calls 'mode_fixup' drm_bridge_funcs op for all the bridges in the
    184  1.3.6.2  christos  * encoder chain, starting from the first bridge to the last.
    185  1.3.6.2  christos  *
    186  1.3.6.2  christos  * Note: the bridge passed should be the one closest to the encoder
    187  1.3.6.2  christos  *
    188  1.3.6.2  christos  * RETURNS:
    189  1.3.6.2  christos  * true on success, false on failure
    190  1.3.6.2  christos  */
    191  1.3.6.2  christos bool drm_bridge_mode_fixup(struct drm_bridge *bridge,
    192  1.3.6.2  christos 			const struct drm_display_mode *mode,
    193  1.3.6.2  christos 			struct drm_display_mode *adjusted_mode)
    194  1.3.6.2  christos {
    195  1.3.6.2  christos 	bool ret = true;
    196  1.3.6.2  christos 
    197  1.3.6.2  christos 	if (!bridge)
    198  1.3.6.2  christos 		return true;
    199  1.3.6.2  christos 
    200  1.3.6.2  christos 	if (bridge->funcs->mode_fixup)
    201  1.3.6.2  christos 		ret = bridge->funcs->mode_fixup(bridge, mode, adjusted_mode);
    202  1.3.6.2  christos 
    203  1.3.6.2  christos 	ret = ret && drm_bridge_mode_fixup(bridge->next, mode, adjusted_mode);
    204  1.3.6.2  christos 
    205  1.3.6.2  christos 	return ret;
    206  1.3.6.2  christos }
    207  1.3.6.2  christos EXPORT_SYMBOL(drm_bridge_mode_fixup);
    208  1.3.6.2  christos 
    209  1.3.6.2  christos /**
    210  1.3.6.2  christos  * drm_bridge_disable - calls 'disable' drm_bridge_funcs op for all
    211  1.3.6.2  christos  *			bridges in the encoder chain.
    212  1.3.6.2  christos  * @bridge: bridge control structure
    213  1.3.6.2  christos  *
    214  1.3.6.2  christos  * Calls 'disable' drm_bridge_funcs op for all the bridges in the encoder
    215  1.3.6.2  christos  * chain, starting from the last bridge to the first. These are called before
    216  1.3.6.2  christos  * calling the encoder's prepare op.
    217  1.3.6.2  christos  *
    218  1.3.6.2  christos  * Note: the bridge passed should be the one closest to the encoder
    219  1.3.6.2  christos  */
    220  1.3.6.2  christos void drm_bridge_disable(struct drm_bridge *bridge)
    221  1.3.6.2  christos {
    222  1.3.6.2  christos 	if (!bridge)
    223  1.3.6.2  christos 		return;
    224  1.3.6.2  christos 
    225  1.3.6.2  christos 	drm_bridge_disable(bridge->next);
    226  1.3.6.2  christos 
    227  1.3.6.2  christos 	bridge->funcs->disable(bridge);
    228  1.3.6.2  christos }
    229  1.3.6.2  christos EXPORT_SYMBOL(drm_bridge_disable);
    230  1.3.6.2  christos 
    231  1.3.6.2  christos /**
    232  1.3.6.2  christos  * drm_bridge_post_disable - calls 'post_disable' drm_bridge_funcs op for
    233  1.3.6.2  christos  *			     all bridges in the encoder chain.
    234  1.3.6.2  christos  * @bridge: bridge control structure
    235  1.3.6.2  christos  *
    236  1.3.6.2  christos  * Calls 'post_disable' drm_bridge_funcs op for all the bridges in the
    237  1.3.6.2  christos  * encoder chain, starting from the first bridge to the last. These are called
    238  1.3.6.2  christos  * after completing the encoder's prepare op.
    239  1.3.6.2  christos  *
    240  1.3.6.2  christos  * Note: the bridge passed should be the one closest to the encoder
    241  1.3.6.2  christos  */
    242  1.3.6.2  christos void drm_bridge_post_disable(struct drm_bridge *bridge)
    243  1.3.6.2  christos {
    244  1.3.6.2  christos 	if (!bridge)
    245  1.3.6.2  christos 		return;
    246  1.3.6.2  christos 
    247  1.3.6.2  christos 	bridge->funcs->post_disable(bridge);
    248  1.3.6.2  christos 
    249  1.3.6.2  christos 	drm_bridge_post_disable(bridge->next);
    250  1.3.6.2  christos }
    251  1.3.6.2  christos EXPORT_SYMBOL(drm_bridge_post_disable);
    252  1.3.6.2  christos 
    253  1.3.6.2  christos /**
    254  1.3.6.2  christos  * drm_bridge_mode_set - set proposed mode for all bridges in the
    255  1.3.6.2  christos  *			 encoder chain
    256  1.3.6.2  christos  * @bridge: bridge control structure
    257  1.3.6.2  christos  * @mode: desired mode to be set for the bridge
    258  1.3.6.2  christos  * @adjusted_mode: updated mode that works for this bridge
    259  1.3.6.2  christos  *
    260  1.3.6.2  christos  * Calls 'mode_set' drm_bridge_funcs op for all the bridges in the
    261  1.3.6.2  christos  * encoder chain, starting from the first bridge to the last.
    262  1.3.6.2  christos  *
    263  1.3.6.2  christos  * Note: the bridge passed should be the one closest to the encoder
    264  1.3.6.2  christos  */
    265  1.3.6.2  christos void drm_bridge_mode_set(struct drm_bridge *bridge,
    266  1.3.6.2  christos 			struct drm_display_mode *mode,
    267  1.3.6.2  christos 			struct drm_display_mode *adjusted_mode)
    268  1.3.6.2  christos {
    269  1.3.6.2  christos 	if (!bridge)
    270  1.3.6.2  christos 		return;
    271  1.3.6.2  christos 
    272  1.3.6.2  christos 	if (bridge->funcs->mode_set)
    273  1.3.6.2  christos 		bridge->funcs->mode_set(bridge, mode, adjusted_mode);
    274  1.3.6.2  christos 
    275  1.3.6.2  christos 	drm_bridge_mode_set(bridge->next, mode, adjusted_mode);
    276  1.3.6.2  christos }
    277  1.3.6.2  christos EXPORT_SYMBOL(drm_bridge_mode_set);
    278  1.3.6.2  christos 
    279  1.3.6.2  christos /**
    280  1.3.6.2  christos  * drm_bridge_pre_enable - calls 'pre_enable' drm_bridge_funcs op for all
    281  1.3.6.2  christos  *			   bridges in the encoder chain.
    282  1.3.6.2  christos  * @bridge: bridge control structure
    283  1.3.6.2  christos  *
    284  1.3.6.2  christos  * Calls 'pre_enable' drm_bridge_funcs op for all the bridges in the encoder
    285  1.3.6.2  christos  * chain, starting from the last bridge to the first. These are called
    286  1.3.6.2  christos  * before calling the encoder's commit op.
    287  1.3.6.2  christos  *
    288  1.3.6.2  christos  * Note: the bridge passed should be the one closest to the encoder
    289  1.3.6.2  christos  */
    290  1.3.6.2  christos void drm_bridge_pre_enable(struct drm_bridge *bridge)
    291  1.3.6.2  christos {
    292  1.3.6.2  christos 	if (!bridge)
    293  1.3.6.2  christos 		return;
    294  1.3.6.2  christos 
    295  1.3.6.2  christos 	drm_bridge_pre_enable(bridge->next);
    296  1.3.6.2  christos 
    297  1.3.6.2  christos 	bridge->funcs->pre_enable(bridge);
    298  1.3.6.2  christos }
    299  1.3.6.2  christos EXPORT_SYMBOL(drm_bridge_pre_enable);
    300  1.3.6.2  christos 
    301  1.3.6.2  christos /**
    302  1.3.6.2  christos  * drm_bridge_enable - calls 'enable' drm_bridge_funcs op for all bridges
    303  1.3.6.2  christos  *		       in the encoder chain.
    304  1.3.6.2  christos  * @bridge: bridge control structure
    305  1.3.6.2  christos  *
    306  1.3.6.2  christos  * Calls 'enable' drm_bridge_funcs op for all the bridges in the encoder
    307  1.3.6.2  christos  * chain, starting from the first bridge to the last. These are called
    308  1.3.6.2  christos  * after completing the encoder's commit op.
    309  1.3.6.2  christos  *
    310  1.3.6.2  christos  * Note that the bridge passed should be the one closest to the encoder
    311  1.3.6.2  christos  */
    312  1.3.6.2  christos void drm_bridge_enable(struct drm_bridge *bridge)
    313  1.3.6.2  christos {
    314  1.3.6.2  christos 	if (!bridge)
    315  1.3.6.2  christos 		return;
    316  1.3.6.2  christos 
    317  1.3.6.2  christos 	bridge->funcs->enable(bridge);
    318  1.3.6.2  christos 
    319  1.3.6.2  christos 	drm_bridge_enable(bridge->next);
    320  1.3.6.2  christos }
    321  1.3.6.2  christos EXPORT_SYMBOL(drm_bridge_enable);
    322  1.3.6.2  christos 
    323  1.3.6.2  christos #ifdef CONFIG_OF
    324  1.3.6.2  christos /**
    325  1.3.6.2  christos  * of_drm_find_bridge - find the bridge corresponding to the device node in
    326  1.3.6.2  christos  *			the global bridge list
    327  1.3.6.2  christos  *
    328  1.3.6.2  christos  * @np: device node
    329  1.3.6.2  christos  *
    330  1.3.6.2  christos  * RETURNS:
    331  1.3.6.2  christos  * drm_bridge control struct on success, NULL on failure
    332  1.3.6.2  christos  */
    333  1.3.6.2  christos struct drm_bridge *of_drm_find_bridge(struct device_node *np)
    334  1.3.6.2  christos {
    335  1.3.6.2  christos 	struct drm_bridge *bridge;
    336  1.3.6.2  christos 
    337  1.3.6.2  christos 	mutex_lock(&bridge_lock);
    338  1.3.6.2  christos 
    339  1.3.6.2  christos 	list_for_each_entry(bridge, &bridge_list, list) {
    340  1.3.6.2  christos 		if (bridge->of_node == np) {
    341  1.3.6.2  christos 			mutex_unlock(&bridge_lock);
    342  1.3.6.2  christos 			return bridge;
    343  1.3.6.2  christos 		}
    344  1.3.6.2  christos 	}
    345  1.3.6.2  christos 
    346  1.3.6.2  christos 	mutex_unlock(&bridge_lock);
    347  1.3.6.2  christos 	return NULL;
    348  1.3.6.2  christos }
    349  1.3.6.2  christos EXPORT_SYMBOL(of_drm_find_bridge);
    350  1.3.6.2  christos #endif
    351  1.3.6.2  christos 
    352  1.3.6.2  christos MODULE_AUTHOR("Ajay Kumar <ajaykumar.rs (at) samsung.com>");
    353  1.3.6.2  christos MODULE_DESCRIPTION("DRM bridge infrastructure");
    354  1.3.6.2  christos MODULE_LICENSE("GPL and additional rights");
    355