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 <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_managed.h>
12 #include <drm/drm_modeset_helper_vtables.h>
13 #include <drm/drm_of.h>
14 #include <drm/drm_panel.h>
15 #include <drm/drm_print.h>
16 #include <drm/drm_probe_helper.h>
19 struct drm_bridge bridge;
20 struct drm_connector connector;
21 struct drm_panel *panel;
25 static inline struct panel_bridge *
26 drm_bridge_to_panel_bridge(struct drm_bridge *bridge)
28 return container_of(bridge, struct panel_bridge, bridge);
31 static inline struct panel_bridge *
32 drm_connector_to_panel_bridge(struct drm_connector *connector)
34 return container_of(connector, struct panel_bridge, connector);
37 static int panel_bridge_connector_get_modes(struct drm_connector *connector)
39 struct panel_bridge *panel_bridge =
40 drm_connector_to_panel_bridge(connector);
42 return drm_panel_get_modes(panel_bridge->panel, connector);
45 static const struct drm_connector_helper_funcs
46 panel_bridge_connector_helper_funcs = {
47 .get_modes = panel_bridge_connector_get_modes,
50 static const struct drm_connector_funcs panel_bridge_connector_funcs = {
51 .reset = drm_atomic_helper_connector_reset,
52 .fill_modes = drm_helper_probe_single_connector_modes,
53 .destroy = drm_connector_cleanup,
54 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
55 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
58 static int panel_bridge_attach(struct drm_bridge *bridge,
59 enum drm_bridge_attach_flags flags)
61 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
62 struct drm_connector *connector = &panel_bridge->connector;
65 if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
68 if (!bridge->encoder) {
69 DRM_ERROR("Missing encoder\n");
73 drm_connector_helper_add(connector,
74 &panel_bridge_connector_helper_funcs);
76 ret = drm_connector_init(bridge->dev, connector,
77 &panel_bridge_connector_funcs,
78 panel_bridge->connector_type);
80 DRM_ERROR("Failed to initialize connector\n");
84 drm_connector_attach_encoder(&panel_bridge->connector,
87 if (bridge->dev->registered) {
88 if (connector->funcs->reset)
89 connector->funcs->reset(connector);
90 drm_connector_register(connector);
96 static void panel_bridge_detach(struct drm_bridge *bridge)
98 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
99 struct drm_connector *connector = &panel_bridge->connector;
102 * Cleanup the connector if we know it was initialized.
104 * FIXME: This wouldn't be needed if the panel_bridge structure was
105 * allocated with drmm_kzalloc(). This might be tricky since the
106 * drm_device pointer can only be retrieved when the bridge is attached.
109 drm_connector_cleanup(connector);
112 static void panel_bridge_pre_enable(struct drm_bridge *bridge)
114 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
116 drm_panel_prepare(panel_bridge->panel);
119 static void panel_bridge_enable(struct drm_bridge *bridge)
121 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
123 drm_panel_enable(panel_bridge->panel);
126 static void panel_bridge_disable(struct drm_bridge *bridge)
128 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
130 drm_panel_disable(panel_bridge->panel);
133 static void panel_bridge_post_disable(struct drm_bridge *bridge)
135 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
137 drm_panel_unprepare(panel_bridge->panel);
140 static int panel_bridge_get_modes(struct drm_bridge *bridge,
141 struct drm_connector *connector)
143 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
145 return drm_panel_get_modes(panel_bridge->panel, connector);
148 static void panel_bridge_debugfs_init(struct drm_bridge *bridge,
151 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
152 struct drm_panel *panel = panel_bridge->panel;
154 root = debugfs_create_dir("panel", root);
155 if (panel->funcs->debugfs_init)
156 panel->funcs->debugfs_init(panel, root);
159 static const struct drm_bridge_funcs panel_bridge_bridge_funcs = {
160 .attach = panel_bridge_attach,
161 .detach = panel_bridge_detach,
162 .pre_enable = panel_bridge_pre_enable,
163 .enable = panel_bridge_enable,
164 .disable = panel_bridge_disable,
165 .post_disable = panel_bridge_post_disable,
166 .get_modes = panel_bridge_get_modes,
167 .atomic_reset = drm_atomic_helper_bridge_reset,
168 .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
169 .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
170 .atomic_get_input_bus_fmts = drm_atomic_helper_bridge_propagate_bus_fmt,
171 .debugfs_init = panel_bridge_debugfs_init,
175 * drm_bridge_is_panel - Checks if a drm_bridge is a panel_bridge.
177 * @bridge: The drm_bridge to be checked.
179 * Returns true if the bridge is a panel bridge, or false otherwise.
181 bool drm_bridge_is_panel(const struct drm_bridge *bridge)
183 return bridge->funcs == &panel_bridge_bridge_funcs;
185 EXPORT_SYMBOL(drm_bridge_is_panel);
188 * drm_panel_bridge_add - Creates a &drm_bridge and &drm_connector that
189 * just calls the appropriate functions from &drm_panel.
191 * @panel: The drm_panel being wrapped. Must be non-NULL.
193 * For drivers converting from directly using drm_panel: The expected
194 * usage pattern is that during either encoder module probe or DSI
195 * host attach, a drm_panel will be looked up through
196 * drm_of_find_panel_or_bridge(). drm_panel_bridge_add() is used to
197 * wrap that panel in the new bridge, and the result can then be
198 * passed to drm_bridge_attach(). The drm_panel_prepare() and related
199 * functions can be dropped from the encoder driver (they're now
200 * called by the KMS helpers before calling into the encoder), along
201 * with connector creation. When done with the bridge (after
202 * drm_mode_config_cleanup() if the bridge has already been attached), then
203 * drm_panel_bridge_remove() to free it.
205 * The connector type is set to @panel->connector_type, which must be set to a
206 * known type. Calling this function with a panel whose connector type is
207 * DRM_MODE_CONNECTOR_Unknown will return ERR_PTR(-EINVAL).
209 * See devm_drm_panel_bridge_add() for an automatically managed version of this
212 struct drm_bridge *drm_panel_bridge_add(struct drm_panel *panel)
214 if (WARN_ON(panel->connector_type == DRM_MODE_CONNECTOR_Unknown))
215 return ERR_PTR(-EINVAL);
217 return drm_panel_bridge_add_typed(panel, panel->connector_type);
219 EXPORT_SYMBOL(drm_panel_bridge_add);
222 * drm_panel_bridge_add_typed - Creates a &drm_bridge and &drm_connector with
223 * an explicit connector type.
224 * @panel: The drm_panel being wrapped. Must be non-NULL.
225 * @connector_type: The connector type (DRM_MODE_CONNECTOR_*)
227 * This is just like drm_panel_bridge_add(), but forces the connector type to
228 * @connector_type instead of infering it from the panel.
230 * This function is deprecated and should not be used in new drivers. Use
231 * drm_panel_bridge_add() instead, and fix panel drivers as necessary if they
232 * don't report a connector type.
234 struct drm_bridge *drm_panel_bridge_add_typed(struct drm_panel *panel,
237 struct panel_bridge *panel_bridge;
240 return ERR_PTR(-EINVAL);
242 panel_bridge = devm_kzalloc(panel->dev, sizeof(*panel_bridge),
245 return ERR_PTR(-ENOMEM);
247 panel_bridge->connector_type = connector_type;
248 panel_bridge->panel = panel;
250 panel_bridge->bridge.funcs = &panel_bridge_bridge_funcs;
252 panel_bridge->bridge.of_node = panel->dev->of_node;
254 panel_bridge->bridge.ops = DRM_BRIDGE_OP_MODES;
255 panel_bridge->bridge.type = connector_type;
257 drm_bridge_add(&panel_bridge->bridge);
259 return &panel_bridge->bridge;
261 EXPORT_SYMBOL(drm_panel_bridge_add_typed);
264 * drm_panel_bridge_remove - Unregisters and frees a drm_bridge
265 * created by drm_panel_bridge_add().
267 * @bridge: The drm_bridge being freed.
269 void drm_panel_bridge_remove(struct drm_bridge *bridge)
271 struct panel_bridge *panel_bridge;
276 if (bridge->funcs != &panel_bridge_bridge_funcs)
279 panel_bridge = drm_bridge_to_panel_bridge(bridge);
281 drm_bridge_remove(bridge);
282 devm_kfree(panel_bridge->panel->dev, bridge);
284 EXPORT_SYMBOL(drm_panel_bridge_remove);
287 * drm_panel_bridge_set_orientation - Set the connector's panel orientation
288 * from the bridge that can be transformed to panel bridge.
290 * @connector: The connector to be set panel orientation.
291 * @bridge: The drm_bridge to be transformed to panel bridge.
293 * Returns 0 on success, negative errno on failure.
295 int drm_panel_bridge_set_orientation(struct drm_connector *connector,
296 struct drm_bridge *bridge)
298 struct panel_bridge *panel_bridge;
300 panel_bridge = drm_bridge_to_panel_bridge(bridge);
302 return drm_connector_set_orientation_from_panel(connector,
303 panel_bridge->panel);
305 EXPORT_SYMBOL(drm_panel_bridge_set_orientation);
307 static void devm_drm_panel_bridge_release(struct device *dev, void *res)
309 struct drm_bridge **bridge = res;
311 drm_panel_bridge_remove(*bridge);
315 * devm_drm_panel_bridge_add - Creates a managed &drm_bridge and &drm_connector
316 * that just calls the appropriate functions from &drm_panel.
317 * @dev: device to tie the bridge lifetime to
318 * @panel: The drm_panel being wrapped. Must be non-NULL.
320 * This is the managed version of drm_panel_bridge_add() which automatically
321 * calls drm_panel_bridge_remove() when @dev is unbound.
323 struct drm_bridge *devm_drm_panel_bridge_add(struct device *dev,
324 struct drm_panel *panel)
326 if (WARN_ON(panel->connector_type == DRM_MODE_CONNECTOR_Unknown))
327 return ERR_PTR(-EINVAL);
329 return devm_drm_panel_bridge_add_typed(dev, panel,
330 panel->connector_type);
332 EXPORT_SYMBOL(devm_drm_panel_bridge_add);
335 * devm_drm_panel_bridge_add_typed - Creates a managed &drm_bridge and
336 * &drm_connector with an explicit connector type.
337 * @dev: device to tie the bridge lifetime to
338 * @panel: The drm_panel being wrapped. Must be non-NULL.
339 * @connector_type: The connector type (DRM_MODE_CONNECTOR_*)
341 * This is just like devm_drm_panel_bridge_add(), but forces the connector type
342 * to @connector_type instead of infering it from the panel.
344 * This function is deprecated and should not be used in new drivers. Use
345 * devm_drm_panel_bridge_add() instead, and fix panel drivers as necessary if
346 * they don't report a connector type.
348 struct drm_bridge *devm_drm_panel_bridge_add_typed(struct device *dev,
349 struct drm_panel *panel,
352 struct drm_bridge **ptr, *bridge;
354 ptr = devres_alloc(devm_drm_panel_bridge_release, sizeof(*ptr),
357 return ERR_PTR(-ENOMEM);
359 bridge = drm_panel_bridge_add_typed(panel, connector_type);
360 if (!IS_ERR(bridge)) {
362 devres_add(dev, ptr);
369 EXPORT_SYMBOL(devm_drm_panel_bridge_add_typed);
371 static void drmm_drm_panel_bridge_release(struct drm_device *drm, void *ptr)
373 struct drm_bridge *bridge = ptr;
375 drm_panel_bridge_remove(bridge);
379 * drmm_panel_bridge_add - Creates a DRM-managed &drm_bridge and
380 * &drm_connector that just calls the
381 * appropriate functions from &drm_panel.
383 * @drm: DRM device to tie the bridge lifetime to
384 * @panel: The drm_panel being wrapped. Must be non-NULL.
386 * This is the DRM-managed version of drm_panel_bridge_add() which
387 * automatically calls drm_panel_bridge_remove() when @dev is cleaned
390 struct drm_bridge *drmm_panel_bridge_add(struct drm_device *drm,
391 struct drm_panel *panel)
393 struct drm_bridge *bridge;
396 bridge = drm_panel_bridge_add_typed(panel, panel->connector_type);
400 ret = drmm_add_action_or_reset(drm, drmm_drm_panel_bridge_release,
407 EXPORT_SYMBOL(drmm_panel_bridge_add);
410 * drm_panel_bridge_connector - return the connector for the panel bridge
411 * @bridge: The drm_bridge.
413 * drm_panel_bridge creates the connector.
414 * This function gives external access to the connector.
416 * Returns: Pointer to drm_connector
418 struct drm_connector *drm_panel_bridge_connector(struct drm_bridge *bridge)
420 struct panel_bridge *panel_bridge;
422 panel_bridge = drm_bridge_to_panel_bridge(bridge);
424 return &panel_bridge->connector;
426 EXPORT_SYMBOL(drm_panel_bridge_connector);
430 * devm_drm_of_get_bridge - Return next bridge in the chain
431 * @dev: device to tie the bridge lifetime to
432 * @np: device tree node containing encoder output ports
433 * @port: port in the device tree node
434 * @endpoint: endpoint in the device tree node
436 * Given a DT node's port and endpoint number, finds the connected node
437 * and returns the associated bridge if any, or creates and returns a
438 * drm panel bridge instance if a panel is connected.
440 * Returns a pointer to the bridge if successful, or an error pointer
443 struct drm_bridge *devm_drm_of_get_bridge(struct device *dev,
444 struct device_node *np,
445 u32 port, u32 endpoint)
447 struct drm_bridge *bridge;
448 struct drm_panel *panel;
451 ret = drm_of_find_panel_or_bridge(np, port, endpoint,
457 bridge = devm_drm_panel_bridge_add(dev, panel);
461 EXPORT_SYMBOL(devm_drm_of_get_bridge);