Ajay Kumar | 3d3f8b1 | 2015-01-20 22:08:44 +0530 | [diff] [blame] | 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 | #include <drm/drm_crtc.h> |
| 28 | |
| 29 | #include "drm/drmP.h" |
| 30 | |
Archit Taneja | 2331b4e | 2015-05-21 11:03:17 +0530 | [diff] [blame] | 31 | /** |
| 32 | * DOC: overview |
| 33 | * |
Daniel Vetter | da024fe | 2015-12-04 09:45:47 +0100 | [diff] [blame] | 34 | * struct &drm_bridge represents a device that hangs on to an encoder. These are |
| 35 | * handy when a regular &drm_encoder entity isn't enough to represent the entire |
Archit Taneja | 2331b4e | 2015-05-21 11:03:17 +0530 | [diff] [blame] | 36 | * encoder chain. |
| 37 | * |
Daniel Vetter | da024fe | 2015-12-04 09:45:47 +0100 | [diff] [blame] | 38 | * A bridge is always attached to a single &drm_encoder at a time, but can be |
Daniel Vetter | da5335b | 2016-05-31 22:55:13 +0200 | [diff] [blame] | 39 | * either connected to it directly, or through an intermediate bridge:: |
Archit Taneja | 2331b4e | 2015-05-21 11:03:17 +0530 | [diff] [blame] | 40 | * |
Daniel Vetter | da024fe | 2015-12-04 09:45:47 +0100 | [diff] [blame] | 41 | * encoder ---> bridge B ---> bridge A |
Archit Taneja | 2331b4e | 2015-05-21 11:03:17 +0530 | [diff] [blame] | 42 | * |
| 43 | * Here, the output of the encoder feeds to bridge B, and that furthers feeds to |
| 44 | * bridge A. |
| 45 | * |
| 46 | * The driver using the bridge is responsible to make the associations between |
| 47 | * the encoder and bridges. Once these links are made, the bridges will |
| 48 | * participate along with encoder functions to perform mode_set/enable/disable |
Daniel Vetter | da024fe | 2015-12-04 09:45:47 +0100 | [diff] [blame] | 49 | * through the ops provided in &drm_bridge_funcs. |
Archit Taneja | 2331b4e | 2015-05-21 11:03:17 +0530 | [diff] [blame] | 50 | * |
| 51 | * drm_bridge, like drm_panel, aren't drm_mode_object entities like planes, |
Daniel Vetter | da024fe | 2015-12-04 09:45:47 +0100 | [diff] [blame] | 52 | * CRTCs, encoders or connectors and hence are not visible to userspace. They |
| 53 | * just provide additional hooks to get the desired output at the end of the |
| 54 | * encoder chain. |
| 55 | * |
| 56 | * Bridges can also be chained up using the next pointer in struct &drm_bridge. |
| 57 | * |
| 58 | * Both legacy CRTC helpers and the new atomic modeset helpers support bridges. |
Archit Taneja | 2331b4e | 2015-05-21 11:03:17 +0530 | [diff] [blame] | 59 | */ |
| 60 | |
Ajay Kumar | 3d3f8b1 | 2015-01-20 22:08:44 +0530 | [diff] [blame] | 61 | static DEFINE_MUTEX(bridge_lock); |
| 62 | static LIST_HEAD(bridge_list); |
| 63 | |
Archit Taneja | 2331b4e | 2015-05-21 11:03:17 +0530 | [diff] [blame] | 64 | /** |
| 65 | * drm_bridge_add - add the given bridge to the global bridge list |
| 66 | * |
| 67 | * @bridge: bridge control structure |
| 68 | * |
| 69 | * RETURNS: |
| 70 | * Unconditionally returns Zero. |
| 71 | */ |
Ajay Kumar | 3d3f8b1 | 2015-01-20 22:08:44 +0530 | [diff] [blame] | 72 | int drm_bridge_add(struct drm_bridge *bridge) |
| 73 | { |
| 74 | mutex_lock(&bridge_lock); |
| 75 | list_add_tail(&bridge->list, &bridge_list); |
| 76 | mutex_unlock(&bridge_lock); |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | EXPORT_SYMBOL(drm_bridge_add); |
| 81 | |
Archit Taneja | 2331b4e | 2015-05-21 11:03:17 +0530 | [diff] [blame] | 82 | /** |
| 83 | * drm_bridge_remove - remove the given bridge from the global bridge list |
| 84 | * |
| 85 | * @bridge: bridge control structure |
| 86 | */ |
Ajay Kumar | 3d3f8b1 | 2015-01-20 22:08:44 +0530 | [diff] [blame] | 87 | void drm_bridge_remove(struct drm_bridge *bridge) |
| 88 | { |
| 89 | mutex_lock(&bridge_lock); |
| 90 | list_del_init(&bridge->list); |
| 91 | mutex_unlock(&bridge_lock); |
| 92 | } |
| 93 | EXPORT_SYMBOL(drm_bridge_remove); |
| 94 | |
Archit Taneja | 2331b4e | 2015-05-21 11:03:17 +0530 | [diff] [blame] | 95 | /** |
| 96 | * drm_bridge_attach - associate given bridge to our DRM device |
| 97 | * |
| 98 | * @dev: DRM device |
| 99 | * @bridge: bridge control structure |
| 100 | * |
Andrea Merello | cf3bef9 | 2016-08-25 11:04:32 +0200 | [diff] [blame^] | 101 | * Called by a kms driver to link one of our encoder/bridge to the given |
Archit Taneja | 2331b4e | 2015-05-21 11:03:17 +0530 | [diff] [blame] | 102 | * bridge. |
| 103 | * |
| 104 | * Note that setting up links between the bridge and our encoder/bridge |
Andrea Merello | cf3bef9 | 2016-08-25 11:04:32 +0200 | [diff] [blame^] | 105 | * objects needs to be handled by the kms driver itself. |
Archit Taneja | 2331b4e | 2015-05-21 11:03:17 +0530 | [diff] [blame] | 106 | * |
| 107 | * RETURNS: |
| 108 | * Zero on success, error code on failure |
| 109 | */ |
Ville Syrjälä | 43fc884 | 2015-03-13 14:51:25 +0200 | [diff] [blame] | 110 | int drm_bridge_attach(struct drm_device *dev, struct drm_bridge *bridge) |
Ajay Kumar | 3d3f8b1 | 2015-01-20 22:08:44 +0530 | [diff] [blame] | 111 | { |
| 112 | if (!dev || !bridge) |
| 113 | return -EINVAL; |
| 114 | |
| 115 | if (bridge->dev) |
| 116 | return -EBUSY; |
| 117 | |
| 118 | bridge->dev = dev; |
| 119 | |
| 120 | if (bridge->funcs->attach) |
| 121 | return bridge->funcs->attach(bridge); |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | EXPORT_SYMBOL(drm_bridge_attach); |
| 126 | |
Archit Taneja | 862e686 | 2015-05-21 11:03:16 +0530 | [diff] [blame] | 127 | /** |
Andrea Merello | cf3bef9 | 2016-08-25 11:04:32 +0200 | [diff] [blame^] | 128 | * drm_bridge_detach - deassociate given bridge from its DRM device |
| 129 | * |
| 130 | * @bridge: bridge control structure |
| 131 | * |
| 132 | * Called by a kms driver to unlink the given bridge from its DRM device. |
| 133 | * |
| 134 | * Note that tearing down links between the bridge and our encoder/bridge |
| 135 | * objects needs to be handled by the kms driver itself. |
| 136 | */ |
| 137 | void drm_bridge_detach(struct drm_bridge *bridge) |
| 138 | { |
| 139 | if (WARN_ON(!bridge)) |
| 140 | return; |
| 141 | |
| 142 | if (WARN_ON(!bridge->dev)) |
| 143 | return; |
| 144 | |
| 145 | if (bridge->funcs->detach) |
| 146 | bridge->funcs->detach(bridge); |
| 147 | |
| 148 | bridge->dev = NULL; |
| 149 | } |
| 150 | EXPORT_SYMBOL(drm_bridge_detach); |
| 151 | |
| 152 | /** |
Archit Taneja | 2331b4e | 2015-05-21 11:03:17 +0530 | [diff] [blame] | 153 | * DOC: bridge callbacks |
| 154 | * |
Daniel Vetter | da024fe | 2015-12-04 09:45:47 +0100 | [diff] [blame] | 155 | * The &drm_bridge_funcs ops are populated by the bridge driver. The DRM |
| 156 | * internals (atomic and CRTC helpers) use the helpers defined in drm_bridge.c |
| 157 | * These helpers call a specific &drm_bridge_funcs op for all the bridges |
Archit Taneja | 2331b4e | 2015-05-21 11:03:17 +0530 | [diff] [blame] | 158 | * during encoder configuration. |
| 159 | * |
Daniel Vetter | da024fe | 2015-12-04 09:45:47 +0100 | [diff] [blame] | 160 | * For detailed specification of the bridge callbacks see &drm_bridge_funcs. |
Archit Taneja | 2331b4e | 2015-05-21 11:03:17 +0530 | [diff] [blame] | 161 | */ |
| 162 | |
| 163 | /** |
Archit Taneja | 862e686 | 2015-05-21 11:03:16 +0530 | [diff] [blame] | 164 | * drm_bridge_mode_fixup - fixup proposed mode for all bridges in the |
| 165 | * encoder chain |
| 166 | * @bridge: bridge control structure |
| 167 | * @mode: desired mode to be set for the bridge |
| 168 | * @adjusted_mode: updated mode that works for this bridge |
| 169 | * |
Daniel Vetter | da024fe | 2015-12-04 09:45:47 +0100 | [diff] [blame] | 170 | * Calls ->mode_fixup() &drm_bridge_funcs op for all the bridges in the |
Archit Taneja | 862e686 | 2015-05-21 11:03:16 +0530 | [diff] [blame] | 171 | * encoder chain, starting from the first bridge to the last. |
| 172 | * |
| 173 | * Note: the bridge passed should be the one closest to the encoder |
| 174 | * |
| 175 | * RETURNS: |
| 176 | * true on success, false on failure |
| 177 | */ |
| 178 | bool drm_bridge_mode_fixup(struct drm_bridge *bridge, |
| 179 | const struct drm_display_mode *mode, |
| 180 | struct drm_display_mode *adjusted_mode) |
| 181 | { |
| 182 | bool ret = true; |
| 183 | |
| 184 | if (!bridge) |
| 185 | return true; |
| 186 | |
| 187 | if (bridge->funcs->mode_fixup) |
| 188 | ret = bridge->funcs->mode_fixup(bridge, mode, adjusted_mode); |
| 189 | |
| 190 | ret = ret && drm_bridge_mode_fixup(bridge->next, mode, adjusted_mode); |
| 191 | |
| 192 | return ret; |
| 193 | } |
| 194 | EXPORT_SYMBOL(drm_bridge_mode_fixup); |
| 195 | |
| 196 | /** |
Daniel Vetter | da024fe | 2015-12-04 09:45:47 +0100 | [diff] [blame] | 197 | * drm_bridge_disable - calls ->disable() &drm_bridge_funcs op for all |
Archit Taneja | 862e686 | 2015-05-21 11:03:16 +0530 | [diff] [blame] | 198 | * bridges in the encoder chain. |
| 199 | * @bridge: bridge control structure |
| 200 | * |
Daniel Vetter | da024fe | 2015-12-04 09:45:47 +0100 | [diff] [blame] | 201 | * Calls ->disable() &drm_bridge_funcs op for all the bridges in the encoder |
Archit Taneja | 862e686 | 2015-05-21 11:03:16 +0530 | [diff] [blame] | 202 | * chain, starting from the last bridge to the first. These are called before |
| 203 | * calling the encoder's prepare op. |
| 204 | * |
| 205 | * Note: the bridge passed should be the one closest to the encoder |
| 206 | */ |
| 207 | void drm_bridge_disable(struct drm_bridge *bridge) |
| 208 | { |
| 209 | if (!bridge) |
| 210 | return; |
| 211 | |
| 212 | drm_bridge_disable(bridge->next); |
| 213 | |
Laurent Pinchart | c8a3b2a | 2016-02-26 11:51:06 +0200 | [diff] [blame] | 214 | if (bridge->funcs->disable) |
| 215 | bridge->funcs->disable(bridge); |
Archit Taneja | 862e686 | 2015-05-21 11:03:16 +0530 | [diff] [blame] | 216 | } |
| 217 | EXPORT_SYMBOL(drm_bridge_disable); |
| 218 | |
| 219 | /** |
Daniel Vetter | da024fe | 2015-12-04 09:45:47 +0100 | [diff] [blame] | 220 | * drm_bridge_post_disable - calls ->post_disable() &drm_bridge_funcs op for |
Archit Taneja | 862e686 | 2015-05-21 11:03:16 +0530 | [diff] [blame] | 221 | * all bridges in the encoder chain. |
| 222 | * @bridge: bridge control structure |
| 223 | * |
Daniel Vetter | da024fe | 2015-12-04 09:45:47 +0100 | [diff] [blame] | 224 | * Calls ->post_disable() &drm_bridge_funcs op for all the bridges in the |
Archit Taneja | 862e686 | 2015-05-21 11:03:16 +0530 | [diff] [blame] | 225 | * encoder chain, starting from the first bridge to the last. These are called |
| 226 | * after completing the encoder's prepare op. |
| 227 | * |
| 228 | * Note: the bridge passed should be the one closest to the encoder |
| 229 | */ |
| 230 | void drm_bridge_post_disable(struct drm_bridge *bridge) |
| 231 | { |
| 232 | if (!bridge) |
| 233 | return; |
| 234 | |
Laurent Pinchart | c8a3b2a | 2016-02-26 11:51:06 +0200 | [diff] [blame] | 235 | if (bridge->funcs->post_disable) |
| 236 | bridge->funcs->post_disable(bridge); |
Archit Taneja | 862e686 | 2015-05-21 11:03:16 +0530 | [diff] [blame] | 237 | |
| 238 | drm_bridge_post_disable(bridge->next); |
| 239 | } |
| 240 | EXPORT_SYMBOL(drm_bridge_post_disable); |
| 241 | |
| 242 | /** |
| 243 | * drm_bridge_mode_set - set proposed mode for all bridges in the |
| 244 | * encoder chain |
| 245 | * @bridge: bridge control structure |
| 246 | * @mode: desired mode to be set for the bridge |
| 247 | * @adjusted_mode: updated mode that works for this bridge |
| 248 | * |
Daniel Vetter | da024fe | 2015-12-04 09:45:47 +0100 | [diff] [blame] | 249 | * Calls ->mode_set() &drm_bridge_funcs op for all the bridges in the |
Archit Taneja | 862e686 | 2015-05-21 11:03:16 +0530 | [diff] [blame] | 250 | * encoder chain, starting from the first bridge to the last. |
| 251 | * |
| 252 | * Note: the bridge passed should be the one closest to the encoder |
| 253 | */ |
| 254 | void drm_bridge_mode_set(struct drm_bridge *bridge, |
| 255 | struct drm_display_mode *mode, |
| 256 | struct drm_display_mode *adjusted_mode) |
| 257 | { |
| 258 | if (!bridge) |
| 259 | return; |
| 260 | |
| 261 | if (bridge->funcs->mode_set) |
| 262 | bridge->funcs->mode_set(bridge, mode, adjusted_mode); |
| 263 | |
| 264 | drm_bridge_mode_set(bridge->next, mode, adjusted_mode); |
| 265 | } |
| 266 | EXPORT_SYMBOL(drm_bridge_mode_set); |
| 267 | |
| 268 | /** |
Daniel Vetter | da024fe | 2015-12-04 09:45:47 +0100 | [diff] [blame] | 269 | * drm_bridge_pre_enable - calls ->pre_enable() &drm_bridge_funcs op for all |
Archit Taneja | 862e686 | 2015-05-21 11:03:16 +0530 | [diff] [blame] | 270 | * bridges in the encoder chain. |
| 271 | * @bridge: bridge control structure |
| 272 | * |
Daniel Vetter | da024fe | 2015-12-04 09:45:47 +0100 | [diff] [blame] | 273 | * Calls ->pre_enable() &drm_bridge_funcs op for all the bridges in the encoder |
Archit Taneja | 862e686 | 2015-05-21 11:03:16 +0530 | [diff] [blame] | 274 | * chain, starting from the last bridge to the first. These are called |
| 275 | * before calling the encoder's commit op. |
| 276 | * |
| 277 | * Note: the bridge passed should be the one closest to the encoder |
| 278 | */ |
| 279 | void drm_bridge_pre_enable(struct drm_bridge *bridge) |
| 280 | { |
| 281 | if (!bridge) |
| 282 | return; |
| 283 | |
| 284 | drm_bridge_pre_enable(bridge->next); |
| 285 | |
Laurent Pinchart | c8a3b2a | 2016-02-26 11:51:06 +0200 | [diff] [blame] | 286 | if (bridge->funcs->pre_enable) |
| 287 | bridge->funcs->pre_enable(bridge); |
Archit Taneja | 862e686 | 2015-05-21 11:03:16 +0530 | [diff] [blame] | 288 | } |
| 289 | EXPORT_SYMBOL(drm_bridge_pre_enable); |
| 290 | |
| 291 | /** |
Daniel Vetter | da024fe | 2015-12-04 09:45:47 +0100 | [diff] [blame] | 292 | * drm_bridge_enable - calls ->enable() &drm_bridge_funcs op for all bridges |
Archit Taneja | 862e686 | 2015-05-21 11:03:16 +0530 | [diff] [blame] | 293 | * in the encoder chain. |
| 294 | * @bridge: bridge control structure |
| 295 | * |
Daniel Vetter | da024fe | 2015-12-04 09:45:47 +0100 | [diff] [blame] | 296 | * Calls ->enable() &drm_bridge_funcs op for all the bridges in the encoder |
Archit Taneja | 862e686 | 2015-05-21 11:03:16 +0530 | [diff] [blame] | 297 | * chain, starting from the first bridge to the last. These are called |
| 298 | * after completing the encoder's commit op. |
| 299 | * |
| 300 | * Note that the bridge passed should be the one closest to the encoder |
| 301 | */ |
| 302 | void drm_bridge_enable(struct drm_bridge *bridge) |
| 303 | { |
| 304 | if (!bridge) |
| 305 | return; |
| 306 | |
Laurent Pinchart | c8a3b2a | 2016-02-26 11:51:06 +0200 | [diff] [blame] | 307 | if (bridge->funcs->enable) |
| 308 | bridge->funcs->enable(bridge); |
Archit Taneja | 862e686 | 2015-05-21 11:03:16 +0530 | [diff] [blame] | 309 | |
| 310 | drm_bridge_enable(bridge->next); |
| 311 | } |
| 312 | EXPORT_SYMBOL(drm_bridge_enable); |
| 313 | |
Ajay Kumar | 3d3f8b1 | 2015-01-20 22:08:44 +0530 | [diff] [blame] | 314 | #ifdef CONFIG_OF |
Archit Taneja | 2331b4e | 2015-05-21 11:03:17 +0530 | [diff] [blame] | 315 | /** |
| 316 | * of_drm_find_bridge - find the bridge corresponding to the device node in |
| 317 | * the global bridge list |
| 318 | * |
| 319 | * @np: device node |
| 320 | * |
| 321 | * RETURNS: |
| 322 | * drm_bridge control struct on success, NULL on failure |
| 323 | */ |
Ajay Kumar | 3d3f8b1 | 2015-01-20 22:08:44 +0530 | [diff] [blame] | 324 | struct drm_bridge *of_drm_find_bridge(struct device_node *np) |
| 325 | { |
| 326 | struct drm_bridge *bridge; |
| 327 | |
| 328 | mutex_lock(&bridge_lock); |
| 329 | |
| 330 | list_for_each_entry(bridge, &bridge_list, list) { |
| 331 | if (bridge->of_node == np) { |
| 332 | mutex_unlock(&bridge_lock); |
| 333 | return bridge; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | mutex_unlock(&bridge_lock); |
| 338 | return NULL; |
| 339 | } |
| 340 | EXPORT_SYMBOL(of_drm_find_bridge); |
| 341 | #endif |
| 342 | |
| 343 | MODULE_AUTHOR("Ajay Kumar <ajaykumar.rs@samsung.com>"); |
| 344 | MODULE_DESCRIPTION("DRM bridge infrastructure"); |
| 345 | MODULE_LICENSE("GPL and additional rights"); |