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