1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2016 Laurent Pinchart <laurent.pinchart@ideasonboard.com>
4 * Copyright (C) 2017 Broadcom
7 #include <linux/device.h>
9 #include <drm/drm_atomic_helper.h>
10 #include <drm/drm_bridge.h>
11 #include <drm/drm_connector.h>
12 #include <drm/drm_encoder.h>
13 #include <drm/drm_managed.h>
14 #include <drm/drm_modeset_helper_vtables.h>
15 #include <drm/drm_of.h>
16 #include <drm/drm_panel.h>
17 #include <drm/drm_print.h>
18 #include <drm/drm_probe_helper.h>
21 struct drm_bridge bridge;
22 struct drm_connector connector;
23 struct drm_panel *panel;
24 struct device_link *link;
28 static inline struct panel_bridge *
29 drm_bridge_to_panel_bridge(struct drm_bridge *bridge)
31 return container_of(bridge, struct panel_bridge, bridge);
34 static inline struct panel_bridge *
35 drm_connector_to_panel_bridge(struct drm_connector *connector)
37 return container_of(connector, struct panel_bridge, connector);
40 static int panel_bridge_connector_get_modes(struct drm_connector *connector)
42 struct panel_bridge *panel_bridge =
43 drm_connector_to_panel_bridge(connector);
45 return drm_panel_get_modes(panel_bridge->panel, connector);
48 static const struct drm_connector_helper_funcs
49 panel_bridge_connector_helper_funcs = {
50 .get_modes = panel_bridge_connector_get_modes,
53 static const struct drm_connector_funcs panel_bridge_connector_funcs = {
54 .reset = drm_atomic_helper_connector_reset,
55 .fill_modes = drm_helper_probe_single_connector_modes,
56 .destroy = drm_connector_cleanup,
57 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
58 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
61 static int panel_bridge_attach(struct drm_bridge *bridge,
62 enum drm_bridge_attach_flags flags)
64 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
65 struct drm_connector *connector = &panel_bridge->connector;
66 struct drm_panel *panel = panel_bridge->panel;
67 struct drm_device *drm_dev = bridge->dev;
70 panel_bridge->link = device_link_add(drm_dev->dev, panel->dev,
72 if (!panel_bridge->link) {
73 DRM_ERROR("Failed to add device link between %s and %s\n",
74 dev_name(drm_dev->dev), dev_name(panel->dev));
78 if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
81 if (!bridge->encoder) {
82 DRM_ERROR("Missing encoder\n");
83 device_link_del(panel_bridge->link);
87 drm_connector_helper_add(connector,
88 &panel_bridge_connector_helper_funcs);
90 ret = drm_connector_init(bridge->dev, connector,
91 &panel_bridge_connector_funcs,
92 panel_bridge->connector_type);
94 DRM_ERROR("Failed to initialize connector\n");
95 device_link_del(panel_bridge->link);
99 drm_panel_bridge_set_orientation(connector, bridge);
101 drm_connector_attach_encoder(&panel_bridge->connector,
104 if (bridge->dev->registered) {
105 if (connector->funcs->reset)
106 connector->funcs->reset(connector);
107 drm_connector_register(connector);
113 static void panel_bridge_detach(struct drm_bridge *bridge)
115 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
116 struct drm_connector *connector = &panel_bridge->connector;
118 device_link_del(panel_bridge->link);
121 * Cleanup the connector if we know it was initialized.
123 * FIXME: This wouldn't be needed if the panel_bridge structure was
124 * allocated with drmm_kzalloc(). This might be tricky since the
125 * drm_device pointer can only be retrieved when the bridge is attached.
128 drm_connector_cleanup(connector);
131 static void panel_bridge_atomic_pre_enable(struct drm_bridge *bridge,
132 struct drm_bridge_state *old_bridge_state)
134 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
135 struct drm_atomic_state *atomic_state = old_bridge_state->base.state;
136 struct drm_encoder *encoder = bridge->encoder;
137 struct drm_crtc *crtc;
138 struct drm_crtc_state *old_crtc_state;
140 crtc = drm_atomic_get_new_crtc_for_encoder(atomic_state, encoder);
144 old_crtc_state = drm_atomic_get_old_crtc_state(atomic_state, crtc);
145 if (old_crtc_state && old_crtc_state->self_refresh_active)
148 drm_panel_prepare(panel_bridge->panel);
151 static void panel_bridge_atomic_enable(struct drm_bridge *bridge,
152 struct drm_bridge_state *old_bridge_state)
154 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
155 struct drm_atomic_state *atomic_state = old_bridge_state->base.state;
156 struct drm_encoder *encoder = bridge->encoder;
157 struct drm_crtc *crtc;
158 struct drm_crtc_state *old_crtc_state;
160 crtc = drm_atomic_get_new_crtc_for_encoder(atomic_state, encoder);
164 old_crtc_state = drm_atomic_get_old_crtc_state(atomic_state, crtc);
165 if (old_crtc_state && old_crtc_state->self_refresh_active)
168 drm_panel_enable(panel_bridge->panel);
171 static void panel_bridge_atomic_disable(struct drm_bridge *bridge,
172 struct drm_bridge_state *old_bridge_state)
174 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
175 struct drm_atomic_state *atomic_state = old_bridge_state->base.state;
176 struct drm_encoder *encoder = bridge->encoder;
177 struct drm_crtc *crtc;
178 struct drm_crtc_state *new_crtc_state;
180 crtc = drm_atomic_get_old_crtc_for_encoder(atomic_state, encoder);
184 new_crtc_state = drm_atomic_get_new_crtc_state(atomic_state, crtc);
185 if (new_crtc_state && new_crtc_state->self_refresh_active)
188 drm_panel_disable(panel_bridge->panel);
191 static void panel_bridge_atomic_post_disable(struct drm_bridge *bridge,
192 struct drm_bridge_state *old_bridge_state)
194 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
195 struct drm_atomic_state *atomic_state = old_bridge_state->base.state;
196 struct drm_encoder *encoder = bridge->encoder;
197 struct drm_crtc *crtc;
198 struct drm_crtc_state *new_crtc_state;
200 crtc = drm_atomic_get_old_crtc_for_encoder(atomic_state, encoder);
204 new_crtc_state = drm_atomic_get_new_crtc_state(atomic_state, crtc);
205 if (new_crtc_state && new_crtc_state->self_refresh_active)
208 drm_panel_unprepare(panel_bridge->panel);
211 static int panel_bridge_get_modes(struct drm_bridge *bridge,
212 struct drm_connector *connector)
214 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
216 return drm_panel_get_modes(panel_bridge->panel, connector);
219 static void panel_bridge_debugfs_init(struct drm_bridge *bridge,
222 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
223 struct drm_panel *panel = panel_bridge->panel;
225 root = debugfs_create_dir("panel", root);
226 if (panel->funcs->debugfs_init)
227 panel->funcs->debugfs_init(panel, root);
230 static const struct drm_bridge_funcs panel_bridge_bridge_funcs = {
231 .attach = panel_bridge_attach,
232 .detach = panel_bridge_detach,
233 .atomic_pre_enable = panel_bridge_atomic_pre_enable,
234 .atomic_enable = panel_bridge_atomic_enable,
235 .atomic_disable = panel_bridge_atomic_disable,
236 .atomic_post_disable = panel_bridge_atomic_post_disable,
237 .get_modes = panel_bridge_get_modes,
238 .atomic_reset = drm_atomic_helper_bridge_reset,
239 .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
240 .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
241 .atomic_get_input_bus_fmts = drm_atomic_helper_bridge_propagate_bus_fmt,
242 .debugfs_init = panel_bridge_debugfs_init,
246 * drm_bridge_is_panel - Checks if a drm_bridge is a panel_bridge.
248 * @bridge: The drm_bridge to be checked.
250 * Returns true if the bridge is a panel bridge, or false otherwise.
252 bool drm_bridge_is_panel(const struct drm_bridge *bridge)
254 return bridge->funcs == &panel_bridge_bridge_funcs;
256 EXPORT_SYMBOL(drm_bridge_is_panel);
259 * drm_panel_bridge_add - Creates a &drm_bridge and &drm_connector that
260 * just calls the appropriate functions from &drm_panel.
262 * @panel: The drm_panel being wrapped. Must be non-NULL.
264 * For drivers converting from directly using drm_panel: The expected
265 * usage pattern is that during either encoder module probe or DSI
266 * host attach, a drm_panel will be looked up through
267 * drm_of_find_panel_or_bridge(). drm_panel_bridge_add() is used to
268 * wrap that panel in the new bridge, and the result can then be
269 * passed to drm_bridge_attach(). The drm_panel_prepare() and related
270 * functions can be dropped from the encoder driver (they're now
271 * called by the KMS helpers before calling into the encoder), along
272 * with connector creation. When done with the bridge (after
273 * drm_mode_config_cleanup() if the bridge has already been attached), then
274 * drm_panel_bridge_remove() to free it.
276 * The connector type is set to @panel->connector_type, which must be set to a
277 * known type. Calling this function with a panel whose connector type is
278 * DRM_MODE_CONNECTOR_Unknown will return ERR_PTR(-EINVAL).
280 * See devm_drm_panel_bridge_add() for an automatically managed version of this
283 struct drm_bridge *drm_panel_bridge_add(struct drm_panel *panel)
285 if (WARN_ON(panel->connector_type == DRM_MODE_CONNECTOR_Unknown))
286 return ERR_PTR(-EINVAL);
288 return drm_panel_bridge_add_typed(panel, panel->connector_type);
290 EXPORT_SYMBOL(drm_panel_bridge_add);
293 * drm_panel_bridge_add_typed - Creates a &drm_bridge and &drm_connector with
294 * an explicit connector type.
295 * @panel: The drm_panel being wrapped. Must be non-NULL.
296 * @connector_type: The connector type (DRM_MODE_CONNECTOR_*)
298 * This is just like drm_panel_bridge_add(), but forces the connector type to
299 * @connector_type instead of infering it from the panel.
301 * This function is deprecated and should not be used in new drivers. Use
302 * drm_panel_bridge_add() instead, and fix panel drivers as necessary if they
303 * don't report a connector type.
305 struct drm_bridge *drm_panel_bridge_add_typed(struct drm_panel *panel,
308 struct panel_bridge *panel_bridge;
311 return ERR_PTR(-EINVAL);
313 panel_bridge = devm_kzalloc(panel->dev, sizeof(*panel_bridge),
316 return ERR_PTR(-ENOMEM);
318 panel_bridge->connector_type = connector_type;
319 panel_bridge->panel = panel;
321 panel_bridge->bridge.funcs = &panel_bridge_bridge_funcs;
322 panel_bridge->bridge.of_node = panel->dev->of_node;
323 panel_bridge->bridge.ops = DRM_BRIDGE_OP_MODES;
324 panel_bridge->bridge.type = connector_type;
326 drm_bridge_add(&panel_bridge->bridge);
328 return &panel_bridge->bridge;
330 EXPORT_SYMBOL(drm_panel_bridge_add_typed);
333 * drm_panel_bridge_remove - Unregisters and frees a drm_bridge
334 * created by drm_panel_bridge_add().
336 * @bridge: The drm_bridge being freed.
338 void drm_panel_bridge_remove(struct drm_bridge *bridge)
340 struct panel_bridge *panel_bridge;
345 if (bridge->funcs != &panel_bridge_bridge_funcs)
348 panel_bridge = drm_bridge_to_panel_bridge(bridge);
350 drm_bridge_remove(bridge);
351 devm_kfree(panel_bridge->panel->dev, bridge);
353 EXPORT_SYMBOL(drm_panel_bridge_remove);
356 * drm_panel_bridge_set_orientation - Set the connector's panel orientation
357 * from the bridge that can be transformed to panel bridge.
359 * @connector: The connector to be set panel orientation.
360 * @bridge: The drm_bridge to be transformed to panel bridge.
362 * Returns 0 on success, negative errno on failure.
364 int drm_panel_bridge_set_orientation(struct drm_connector *connector,
365 struct drm_bridge *bridge)
367 struct panel_bridge *panel_bridge;
369 panel_bridge = drm_bridge_to_panel_bridge(bridge);
371 return drm_connector_set_orientation_from_panel(connector,
372 panel_bridge->panel);
374 EXPORT_SYMBOL(drm_panel_bridge_set_orientation);
376 static void devm_drm_panel_bridge_release(struct device *dev, void *res)
378 struct drm_bridge **bridge = res;
380 drm_panel_bridge_remove(*bridge);
384 * devm_drm_panel_bridge_add - Creates a managed &drm_bridge and &drm_connector
385 * that just calls the appropriate functions from &drm_panel.
386 * @dev: device to tie the bridge lifetime to
387 * @panel: The drm_panel being wrapped. Must be non-NULL.
389 * This is the managed version of drm_panel_bridge_add() which automatically
390 * calls drm_panel_bridge_remove() when @dev is unbound.
392 struct drm_bridge *devm_drm_panel_bridge_add(struct device *dev,
393 struct drm_panel *panel)
395 if (WARN_ON(panel->connector_type == DRM_MODE_CONNECTOR_Unknown))
396 return ERR_PTR(-EINVAL);
398 return devm_drm_panel_bridge_add_typed(dev, panel,
399 panel->connector_type);
401 EXPORT_SYMBOL(devm_drm_panel_bridge_add);
404 * devm_drm_panel_bridge_add_typed - Creates a managed &drm_bridge and
405 * &drm_connector with an explicit connector type.
406 * @dev: device to tie the bridge lifetime to
407 * @panel: The drm_panel being wrapped. Must be non-NULL.
408 * @connector_type: The connector type (DRM_MODE_CONNECTOR_*)
410 * This is just like devm_drm_panel_bridge_add(), but forces the connector type
411 * to @connector_type instead of infering it from the panel.
413 * This function is deprecated and should not be used in new drivers. Use
414 * devm_drm_panel_bridge_add() instead, and fix panel drivers as necessary if
415 * they don't report a connector type.
417 struct drm_bridge *devm_drm_panel_bridge_add_typed(struct device *dev,
418 struct drm_panel *panel,
421 struct drm_bridge **ptr, *bridge;
423 ptr = devres_alloc(devm_drm_panel_bridge_release, sizeof(*ptr),
426 return ERR_PTR(-ENOMEM);
428 bridge = drm_panel_bridge_add_typed(panel, connector_type);
429 if (IS_ERR(bridge)) {
434 bridge->pre_enable_prev_first = panel->prepare_prev_first;
437 devres_add(dev, ptr);
441 EXPORT_SYMBOL(devm_drm_panel_bridge_add_typed);
443 static void drmm_drm_panel_bridge_release(struct drm_device *drm, void *ptr)
445 struct drm_bridge *bridge = ptr;
447 drm_panel_bridge_remove(bridge);
451 * drmm_panel_bridge_add - Creates a DRM-managed &drm_bridge and
452 * &drm_connector that just calls the
453 * appropriate functions from &drm_panel.
455 * @drm: DRM device to tie the bridge lifetime to
456 * @panel: The drm_panel being wrapped. Must be non-NULL.
458 * This is the DRM-managed version of drm_panel_bridge_add() which
459 * automatically calls drm_panel_bridge_remove() when @dev is cleaned
462 struct drm_bridge *drmm_panel_bridge_add(struct drm_device *drm,
463 struct drm_panel *panel)
465 struct drm_bridge *bridge;
468 bridge = drm_panel_bridge_add_typed(panel, panel->connector_type);
472 ret = drmm_add_action_or_reset(drm, drmm_drm_panel_bridge_release,
477 bridge->pre_enable_prev_first = panel->prepare_prev_first;
481 EXPORT_SYMBOL(drmm_panel_bridge_add);
484 * drm_panel_bridge_connector - return the connector for the panel bridge
485 * @bridge: The drm_bridge.
487 * drm_panel_bridge creates the connector.
488 * This function gives external access to the connector.
490 * Returns: Pointer to drm_connector
492 struct drm_connector *drm_panel_bridge_connector(struct drm_bridge *bridge)
494 struct panel_bridge *panel_bridge;
496 panel_bridge = drm_bridge_to_panel_bridge(bridge);
498 return &panel_bridge->connector;
500 EXPORT_SYMBOL(drm_panel_bridge_connector);
504 * devm_drm_of_get_bridge - Return next bridge in the chain
505 * @dev: device to tie the bridge lifetime to
506 * @np: device tree node containing encoder output ports
507 * @port: port in the device tree node
508 * @endpoint: endpoint in the device tree node
510 * Given a DT node's port and endpoint number, finds the connected node
511 * and returns the associated bridge if any, or creates and returns a
512 * drm panel bridge instance if a panel is connected.
514 * Returns a pointer to the bridge if successful, or an error pointer
517 struct drm_bridge *devm_drm_of_get_bridge(struct device *dev,
518 struct device_node *np,
519 u32 port, u32 endpoint)
521 struct drm_bridge *bridge;
522 struct drm_panel *panel;
525 ret = drm_of_find_panel_or_bridge(np, port, endpoint,
531 bridge = devm_drm_panel_bridge_add(dev, panel);
535 EXPORT_SYMBOL(devm_drm_of_get_bridge);
538 * drmm_of_get_bridge - Return next bridge in the chain
539 * @drm: device to tie the bridge lifetime to
540 * @np: device tree node containing encoder output ports
541 * @port: port in the device tree node
542 * @endpoint: endpoint in the device tree node
544 * Given a DT node's port and endpoint number, finds the connected node
545 * and returns the associated bridge if any, or creates and returns a
546 * drm panel bridge instance if a panel is connected.
548 * Returns a drmm managed pointer to the bridge if successful, or an error
551 struct drm_bridge *drmm_of_get_bridge(struct drm_device *drm,
552 struct device_node *np,
553 u32 port, u32 endpoint)
555 struct drm_bridge *bridge;
556 struct drm_panel *panel;
559 ret = drm_of_find_panel_or_bridge(np, port, endpoint,
565 bridge = drmm_panel_bridge_add(drm, panel);
569 EXPORT_SYMBOL(drmm_of_get_bridge);