1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2016 Laurent Pinchart <laurent.pinchart@ideasonboard.com> 4 * Copyright (C) 2017 Broadcom 5 */ 6 7 #include <drm/drm_atomic_helper.h> 8 #include <drm/drm_bridge.h> 9 #include <drm/drm_connector.h> 10 #include <drm/drm_encoder.h> 11 #include <drm/drm_modeset_helper_vtables.h> 12 #include <drm/drm_panel.h> 13 #include <drm/drm_print.h> 14 #include <drm/drm_probe_helper.h> 15 16 struct panel_bridge { 17 struct drm_bridge bridge; 18 struct drm_connector connector; 19 struct drm_panel *panel; 20 u32 connector_type; 21 }; 22 23 static inline struct panel_bridge * 24 drm_bridge_to_panel_bridge(struct drm_bridge *bridge) 25 { 26 return container_of(bridge, struct panel_bridge, bridge); 27 } 28 29 static inline struct panel_bridge * 30 drm_connector_to_panel_bridge(struct drm_connector *connector) 31 { 32 return container_of(connector, struct panel_bridge, connector); 33 } 34 35 static int panel_bridge_connector_get_modes(struct drm_connector *connector) 36 { 37 struct panel_bridge *panel_bridge = 38 drm_connector_to_panel_bridge(connector); 39 40 return drm_panel_get_modes(panel_bridge->panel, connector); 41 } 42 43 static const struct drm_connector_helper_funcs 44 panel_bridge_connector_helper_funcs = { 45 .get_modes = panel_bridge_connector_get_modes, 46 }; 47 48 static const struct drm_connector_funcs panel_bridge_connector_funcs = { 49 .reset = drm_atomic_helper_connector_reset, 50 .fill_modes = drm_helper_probe_single_connector_modes, 51 .destroy = drm_connector_cleanup, 52 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 53 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 54 }; 55 56 static int panel_bridge_attach(struct drm_bridge *bridge, 57 enum drm_bridge_attach_flags flags) 58 { 59 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge); 60 struct drm_connector *connector = &panel_bridge->connector; 61 int ret; 62 63 if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR) 64 return 0; 65 66 if (!bridge->encoder) { 67 DRM_ERROR("Missing encoder\n"); 68 return -ENODEV; 69 } 70 71 drm_connector_helper_add(connector, 72 &panel_bridge_connector_helper_funcs); 73 74 ret = drm_connector_init(bridge->dev, connector, 75 &panel_bridge_connector_funcs, 76 panel_bridge->connector_type); 77 if (ret) { 78 DRM_ERROR("Failed to initialize connector\n"); 79 return ret; 80 } 81 82 drm_connector_attach_encoder(&panel_bridge->connector, 83 bridge->encoder); 84 85 ret = drm_panel_attach(panel_bridge->panel, &panel_bridge->connector); 86 if (ret < 0) 87 return ret; 88 89 return 0; 90 } 91 92 static void panel_bridge_detach(struct drm_bridge *bridge) 93 { 94 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge); 95 96 drm_panel_detach(panel_bridge->panel); 97 } 98 99 static void panel_bridge_pre_enable(struct drm_bridge *bridge) 100 { 101 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge); 102 103 drm_panel_prepare(panel_bridge->panel); 104 } 105 106 static void panel_bridge_enable(struct drm_bridge *bridge) 107 { 108 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge); 109 110 drm_panel_enable(panel_bridge->panel); 111 } 112 113 static void panel_bridge_disable(struct drm_bridge *bridge) 114 { 115 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge); 116 117 drm_panel_disable(panel_bridge->panel); 118 } 119 120 static void panel_bridge_post_disable(struct drm_bridge *bridge) 121 { 122 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge); 123 124 drm_panel_unprepare(panel_bridge->panel); 125 } 126 127 static int panel_bridge_get_modes(struct drm_bridge *bridge, 128 struct drm_connector *connector) 129 { 130 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge); 131 132 return drm_panel_get_modes(panel_bridge->panel, connector); 133 } 134 135 static const struct drm_bridge_funcs panel_bridge_bridge_funcs = { 136 .attach = panel_bridge_attach, 137 .detach = panel_bridge_detach, 138 .pre_enable = panel_bridge_pre_enable, 139 .enable = panel_bridge_enable, 140 .disable = panel_bridge_disable, 141 .post_disable = panel_bridge_post_disable, 142 .get_modes = panel_bridge_get_modes, 143 .atomic_reset = drm_atomic_helper_bridge_reset, 144 .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state, 145 .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state, 146 .atomic_get_input_bus_fmts = drm_atomic_helper_bridge_propagate_bus_fmt, 147 }; 148 149 /** 150 * drm_panel_bridge_add - Creates a &drm_bridge and &drm_connector that 151 * just calls the appropriate functions from &drm_panel. 152 * 153 * @panel: The drm_panel being wrapped. Must be non-NULL. 154 * 155 * For drivers converting from directly using drm_panel: The expected 156 * usage pattern is that during either encoder module probe or DSI 157 * host attach, a drm_panel will be looked up through 158 * drm_of_find_panel_or_bridge(). drm_panel_bridge_add() is used to 159 * wrap that panel in the new bridge, and the result can then be 160 * passed to drm_bridge_attach(). The drm_panel_prepare() and related 161 * functions can be dropped from the encoder driver (they're now 162 * called by the KMS helpers before calling into the encoder), along 163 * with connector creation. When done with the bridge (after 164 * drm_mode_config_cleanup() if the bridge has already been attached), then 165 * drm_panel_bridge_remove() to free it. 166 * 167 * The connector type is set to @panel->connector_type, which must be set to a 168 * known type. Calling this function with a panel whose connector type is 169 * DRM_MODE_CONNECTOR_Unknown will return ERR_PTR(-EINVAL). 170 * 171 * See devm_drm_panel_bridge_add() for an automatically managed version of this 172 * function. 173 */ 174 struct drm_bridge *drm_panel_bridge_add(struct drm_panel *panel) 175 { 176 if (WARN_ON(panel->connector_type == DRM_MODE_CONNECTOR_Unknown)) 177 return ERR_PTR(-EINVAL); 178 179 return drm_panel_bridge_add_typed(panel, panel->connector_type); 180 } 181 EXPORT_SYMBOL(drm_panel_bridge_add); 182 183 /** 184 * drm_panel_bridge_add_typed - Creates a &drm_bridge and &drm_connector with 185 * an explicit connector type. 186 * @panel: The drm_panel being wrapped. Must be non-NULL. 187 * @connector_type: The connector type (DRM_MODE_CONNECTOR_*) 188 * 189 * This is just like drm_panel_bridge_add(), but forces the connector type to 190 * @connector_type instead of infering it from the panel. 191 * 192 * This function is deprecated and should not be used in new drivers. Use 193 * drm_panel_bridge_add() instead, and fix panel drivers as necessary if they 194 * don't report a connector type. 195 */ 196 struct drm_bridge *drm_panel_bridge_add_typed(struct drm_panel *panel, 197 u32 connector_type) 198 { 199 struct panel_bridge *panel_bridge; 200 201 if (!panel) 202 return ERR_PTR(-EINVAL); 203 204 panel_bridge = devm_kzalloc(panel->dev, sizeof(*panel_bridge), 205 GFP_KERNEL); 206 if (!panel_bridge) 207 return ERR_PTR(-ENOMEM); 208 209 panel_bridge->connector_type = connector_type; 210 panel_bridge->panel = panel; 211 212 panel_bridge->bridge.funcs = &panel_bridge_bridge_funcs; 213 #ifdef CONFIG_OF 214 panel_bridge->bridge.of_node = panel->dev->of_node; 215 #endif 216 panel_bridge->bridge.ops = DRM_BRIDGE_OP_MODES; 217 panel_bridge->bridge.type = connector_type; 218 219 drm_bridge_add(&panel_bridge->bridge); 220 221 return &panel_bridge->bridge; 222 } 223 EXPORT_SYMBOL(drm_panel_bridge_add_typed); 224 225 /** 226 * drm_panel_bridge_remove - Unregisters and frees a drm_bridge 227 * created by drm_panel_bridge_add(). 228 * 229 * @bridge: The drm_bridge being freed. 230 */ 231 void drm_panel_bridge_remove(struct drm_bridge *bridge) 232 { 233 struct panel_bridge *panel_bridge; 234 235 if (!bridge) 236 return; 237 238 if (bridge->funcs != &panel_bridge_bridge_funcs) 239 return; 240 241 panel_bridge = drm_bridge_to_panel_bridge(bridge); 242 243 drm_bridge_remove(bridge); 244 devm_kfree(panel_bridge->panel->dev, bridge); 245 } 246 EXPORT_SYMBOL(drm_panel_bridge_remove); 247 248 static void devm_drm_panel_bridge_release(struct device *dev, void *res) 249 { 250 struct drm_bridge **bridge = res; 251 252 drm_panel_bridge_remove(*bridge); 253 } 254 255 /** 256 * devm_drm_panel_bridge_add - Creates a managed &drm_bridge and &drm_connector 257 * that just calls the appropriate functions from &drm_panel. 258 * @dev: device to tie the bridge lifetime to 259 * @panel: The drm_panel being wrapped. Must be non-NULL. 260 * 261 * This is the managed version of drm_panel_bridge_add() which automatically 262 * calls drm_panel_bridge_remove() when @dev is unbound. 263 */ 264 struct drm_bridge *devm_drm_panel_bridge_add(struct device *dev, 265 struct drm_panel *panel) 266 { 267 if (WARN_ON(panel->connector_type == DRM_MODE_CONNECTOR_Unknown)) 268 return ERR_PTR(-EINVAL); 269 270 return devm_drm_panel_bridge_add_typed(dev, panel, 271 panel->connector_type); 272 } 273 EXPORT_SYMBOL(devm_drm_panel_bridge_add); 274 275 /** 276 * devm_drm_panel_bridge_add_typed - Creates a managed &drm_bridge and 277 * &drm_connector with an explicit connector type. 278 * @dev: device to tie the bridge lifetime to 279 * @panel: The drm_panel being wrapped. Must be non-NULL. 280 * @connector_type: The connector type (DRM_MODE_CONNECTOR_*) 281 * 282 * This is just like devm_drm_panel_bridge_add(), but forces the connector type 283 * to @connector_type instead of infering it from the panel. 284 * 285 * This function is deprecated and should not be used in new drivers. Use 286 * devm_drm_panel_bridge_add() instead, and fix panel drivers as necessary if 287 * they don't report a connector type. 288 */ 289 struct drm_bridge *devm_drm_panel_bridge_add_typed(struct device *dev, 290 struct drm_panel *panel, 291 u32 connector_type) 292 { 293 struct drm_bridge **ptr, *bridge; 294 295 ptr = devres_alloc(devm_drm_panel_bridge_release, sizeof(*ptr), 296 GFP_KERNEL); 297 if (!ptr) 298 return ERR_PTR(-ENOMEM); 299 300 bridge = drm_panel_bridge_add_typed(panel, connector_type); 301 if (!IS_ERR(bridge)) { 302 *ptr = bridge; 303 devres_add(dev, ptr); 304 } else { 305 devres_free(ptr); 306 } 307 308 return bridge; 309 } 310 EXPORT_SYMBOL(devm_drm_panel_bridge_add_typed); 311 312 /** 313 * drm_panel_bridge_connector - return the connector for the panel bridge 314 * @bridge: The drm_bridge. 315 * 316 * drm_panel_bridge creates the connector. 317 * This function gives external access to the connector. 318 * 319 * Returns: Pointer to drm_connector 320 */ 321 struct drm_connector *drm_panel_bridge_connector(struct drm_bridge *bridge) 322 { 323 struct panel_bridge *panel_bridge; 324 325 panel_bridge = drm_bridge_to_panel_bridge(bridge); 326 327 return &panel_bridge->connector; 328 } 329 EXPORT_SYMBOL(drm_panel_bridge_connector); 330