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_modeset_helper_vtables.h>
12 #include <drm/drm_of.h>
13 #include <drm/drm_panel.h>
14 #include <drm/drm_print.h>
15 #include <drm/drm_probe_helper.h>
18 struct drm_bridge bridge;
19 struct drm_connector connector;
20 struct drm_panel *panel;
24 static inline struct panel_bridge *
25 drm_bridge_to_panel_bridge(struct drm_bridge *bridge)
27 return container_of(bridge, struct panel_bridge, bridge);
30 static inline struct panel_bridge *
31 drm_connector_to_panel_bridge(struct drm_connector *connector)
33 return container_of(connector, struct panel_bridge, connector);
36 static int panel_bridge_connector_get_modes(struct drm_connector *connector)
38 struct panel_bridge *panel_bridge =
39 drm_connector_to_panel_bridge(connector);
41 return drm_panel_get_modes(panel_bridge->panel, connector);
44 static const struct drm_connector_helper_funcs
45 panel_bridge_connector_helper_funcs = {
46 .get_modes = panel_bridge_connector_get_modes,
49 static const struct drm_connector_funcs panel_bridge_connector_funcs = {
50 .reset = drm_atomic_helper_connector_reset,
51 .fill_modes = drm_helper_probe_single_connector_modes,
52 .destroy = drm_connector_cleanup,
53 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
54 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
57 static int panel_bridge_attach(struct drm_bridge *bridge,
58 enum drm_bridge_attach_flags flags)
60 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
61 struct drm_connector *connector = &panel_bridge->connector;
64 if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
67 if (!bridge->encoder) {
68 DRM_ERROR("Missing encoder\n");
72 drm_connector_helper_add(connector,
73 &panel_bridge_connector_helper_funcs);
75 ret = drm_connector_init(bridge->dev, connector,
76 &panel_bridge_connector_funcs,
77 panel_bridge->connector_type);
79 DRM_ERROR("Failed to initialize connector\n");
83 drm_connector_attach_encoder(&panel_bridge->connector,
86 if (bridge->dev->registered) {
87 if (connector->funcs->reset)
88 connector->funcs->reset(connector);
89 drm_connector_register(connector);
95 static void panel_bridge_detach(struct drm_bridge *bridge)
97 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
98 struct drm_connector *connector = &panel_bridge->connector;
101 * Cleanup the connector if we know it was initialized.
103 * FIXME: This wouldn't be needed if the panel_bridge structure was
104 * allocated with drmm_kzalloc(). This might be tricky since the
105 * drm_device pointer can only be retrieved when the bridge is attached.
108 drm_connector_cleanup(connector);
111 static void panel_bridge_pre_enable(struct drm_bridge *bridge)
113 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
115 drm_panel_prepare(panel_bridge->panel);
118 static void panel_bridge_enable(struct drm_bridge *bridge)
120 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
122 drm_panel_enable(panel_bridge->panel);
125 static void panel_bridge_disable(struct drm_bridge *bridge)
127 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
129 drm_panel_disable(panel_bridge->panel);
132 static void panel_bridge_post_disable(struct drm_bridge *bridge)
134 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
136 drm_panel_unprepare(panel_bridge->panel);
139 static int panel_bridge_get_modes(struct drm_bridge *bridge,
140 struct drm_connector *connector)
142 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
144 return drm_panel_get_modes(panel_bridge->panel, connector);
147 static void panel_bridge_debugfs_init(struct drm_bridge *bridge,
150 struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
151 struct drm_panel *panel = panel_bridge->panel;
153 root = debugfs_create_dir("panel", root);
154 if (panel->funcs->debugfs_init)
155 panel->funcs->debugfs_init(panel, root);
158 static const struct drm_bridge_funcs panel_bridge_bridge_funcs = {
159 .attach = panel_bridge_attach,
160 .detach = panel_bridge_detach,
161 .pre_enable = panel_bridge_pre_enable,
162 .enable = panel_bridge_enable,
163 .disable = panel_bridge_disable,
164 .post_disable = panel_bridge_post_disable,
165 .get_modes = panel_bridge_get_modes,
166 .atomic_reset = drm_atomic_helper_bridge_reset,
167 .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
168 .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
169 .atomic_get_input_bus_fmts = drm_atomic_helper_bridge_propagate_bus_fmt,
170 .debugfs_init = panel_bridge_debugfs_init,
174 * drm_bridge_is_panel - Checks if a drm_bridge is a panel_bridge.
176 * @bridge: The drm_bridge to be checked.
178 * Returns true if the bridge is a panel bridge, or false otherwise.
180 bool drm_bridge_is_panel(const struct drm_bridge *bridge)
182 return bridge->funcs == &panel_bridge_bridge_funcs;
184 EXPORT_SYMBOL(drm_bridge_is_panel);
187 * drm_panel_bridge_add - Creates a &drm_bridge and &drm_connector that
188 * just calls the appropriate functions from &drm_panel.
190 * @panel: The drm_panel being wrapped. Must be non-NULL.
192 * For drivers converting from directly using drm_panel: The expected
193 * usage pattern is that during either encoder module probe or DSI
194 * host attach, a drm_panel will be looked up through
195 * drm_of_find_panel_or_bridge(). drm_panel_bridge_add() is used to
196 * wrap that panel in the new bridge, and the result can then be
197 * passed to drm_bridge_attach(). The drm_panel_prepare() and related
198 * functions can be dropped from the encoder driver (they're now
199 * called by the KMS helpers before calling into the encoder), along
200 * with connector creation. When done with the bridge (after
201 * drm_mode_config_cleanup() if the bridge has already been attached), then
202 * drm_panel_bridge_remove() to free it.
204 * The connector type is set to @panel->connector_type, which must be set to a
205 * known type. Calling this function with a panel whose connector type is
206 * DRM_MODE_CONNECTOR_Unknown will return ERR_PTR(-EINVAL).
208 * See devm_drm_panel_bridge_add() for an automatically managed version of this
211 struct drm_bridge *drm_panel_bridge_add(struct drm_panel *panel)
213 if (WARN_ON(panel->connector_type == DRM_MODE_CONNECTOR_Unknown))
214 return ERR_PTR(-EINVAL);
216 return drm_panel_bridge_add_typed(panel, panel->connector_type);
218 EXPORT_SYMBOL(drm_panel_bridge_add);
221 * drm_panel_bridge_add_typed - Creates a &drm_bridge and &drm_connector with
222 * an explicit connector type.
223 * @panel: The drm_panel being wrapped. Must be non-NULL.
224 * @connector_type: The connector type (DRM_MODE_CONNECTOR_*)
226 * This is just like drm_panel_bridge_add(), but forces the connector type to
227 * @connector_type instead of infering it from the panel.
229 * This function is deprecated and should not be used in new drivers. Use
230 * drm_panel_bridge_add() instead, and fix panel drivers as necessary if they
231 * don't report a connector type.
233 struct drm_bridge *drm_panel_bridge_add_typed(struct drm_panel *panel,
236 struct panel_bridge *panel_bridge;
239 return ERR_PTR(-EINVAL);
241 panel_bridge = devm_kzalloc(panel->dev, sizeof(*panel_bridge),
244 return ERR_PTR(-ENOMEM);
246 panel_bridge->connector_type = connector_type;
247 panel_bridge->panel = panel;
249 panel_bridge->bridge.funcs = &panel_bridge_bridge_funcs;
251 panel_bridge->bridge.of_node = panel->dev->of_node;
253 panel_bridge->bridge.ops = DRM_BRIDGE_OP_MODES;
254 panel_bridge->bridge.type = connector_type;
256 drm_bridge_add(&panel_bridge->bridge);
258 return &panel_bridge->bridge;
260 EXPORT_SYMBOL(drm_panel_bridge_add_typed);
263 * drm_panel_bridge_remove - Unregisters and frees a drm_bridge
264 * created by drm_panel_bridge_add().
266 * @bridge: The drm_bridge being freed.
268 void drm_panel_bridge_remove(struct drm_bridge *bridge)
270 struct panel_bridge *panel_bridge;
275 if (bridge->funcs != &panel_bridge_bridge_funcs)
278 panel_bridge = drm_bridge_to_panel_bridge(bridge);
280 drm_bridge_remove(bridge);
281 devm_kfree(panel_bridge->panel->dev, bridge);
283 EXPORT_SYMBOL(drm_panel_bridge_remove);
286 * drm_panel_bridge_set_orientation - Set the connector's panel orientation
287 * from the bridge that can be transformed to panel bridge.
289 * @connector: The connector to be set panel orientation.
290 * @bridge: The drm_bridge to be transformed to panel bridge.
292 * Returns 0 on success, negative errno on failure.
294 int drm_panel_bridge_set_orientation(struct drm_connector *connector,
295 struct drm_bridge *bridge)
297 struct panel_bridge *panel_bridge;
299 panel_bridge = drm_bridge_to_panel_bridge(bridge);
301 return drm_connector_set_orientation_from_panel(connector,
302 panel_bridge->panel);
304 EXPORT_SYMBOL(drm_panel_bridge_set_orientation);
306 static void devm_drm_panel_bridge_release(struct device *dev, void *res)
308 struct drm_bridge **bridge = res;
310 drm_panel_bridge_remove(*bridge);
314 * devm_drm_panel_bridge_add - Creates a managed &drm_bridge and &drm_connector
315 * that just calls the appropriate functions from &drm_panel.
316 * @dev: device to tie the bridge lifetime to
317 * @panel: The drm_panel being wrapped. Must be non-NULL.
319 * This is the managed version of drm_panel_bridge_add() which automatically
320 * calls drm_panel_bridge_remove() when @dev is unbound.
322 struct drm_bridge *devm_drm_panel_bridge_add(struct device *dev,
323 struct drm_panel *panel)
325 if (WARN_ON(panel->connector_type == DRM_MODE_CONNECTOR_Unknown))
326 return ERR_PTR(-EINVAL);
328 return devm_drm_panel_bridge_add_typed(dev, panel,
329 panel->connector_type);
331 EXPORT_SYMBOL(devm_drm_panel_bridge_add);
334 * devm_drm_panel_bridge_add_typed - Creates a managed &drm_bridge and
335 * &drm_connector with an explicit connector type.
336 * @dev: device to tie the bridge lifetime to
337 * @panel: The drm_panel being wrapped. Must be non-NULL.
338 * @connector_type: The connector type (DRM_MODE_CONNECTOR_*)
340 * This is just like devm_drm_panel_bridge_add(), but forces the connector type
341 * to @connector_type instead of infering it from the panel.
343 * This function is deprecated and should not be used in new drivers. Use
344 * devm_drm_panel_bridge_add() instead, and fix panel drivers as necessary if
345 * they don't report a connector type.
347 struct drm_bridge *devm_drm_panel_bridge_add_typed(struct device *dev,
348 struct drm_panel *panel,
351 struct drm_bridge **ptr, *bridge;
353 ptr = devres_alloc(devm_drm_panel_bridge_release, sizeof(*ptr),
356 return ERR_PTR(-ENOMEM);
358 bridge = drm_panel_bridge_add_typed(panel, connector_type);
359 if (!IS_ERR(bridge)) {
361 devres_add(dev, ptr);
368 EXPORT_SYMBOL(devm_drm_panel_bridge_add_typed);
371 * drm_panel_bridge_connector - return the connector for the panel bridge
372 * @bridge: The drm_bridge.
374 * drm_panel_bridge creates the connector.
375 * This function gives external access to the connector.
377 * Returns: Pointer to drm_connector
379 struct drm_connector *drm_panel_bridge_connector(struct drm_bridge *bridge)
381 struct panel_bridge *panel_bridge;
383 panel_bridge = drm_bridge_to_panel_bridge(bridge);
385 return &panel_bridge->connector;
387 EXPORT_SYMBOL(drm_panel_bridge_connector);
391 * devm_drm_of_get_bridge - Return next bridge in the chain
392 * @dev: device to tie the bridge lifetime to
393 * @np: device tree node containing encoder output ports
394 * @port: port in the device tree node
395 * @endpoint: endpoint in the device tree node
397 * Given a DT node's port and endpoint number, finds the connected node
398 * and returns the associated bridge if any, or creates and returns a
399 * drm panel bridge instance if a panel is connected.
401 * Returns a pointer to the bridge if successful, or an error pointer
404 struct drm_bridge *devm_drm_of_get_bridge(struct device *dev,
405 struct device_node *np,
406 u32 port, u32 endpoint)
408 struct drm_bridge *bridge;
409 struct drm_panel *panel;
412 ret = drm_of_find_panel_or_bridge(np, port, endpoint,
418 bridge = devm_drm_panel_bridge_add(dev, panel);
422 EXPORT_SYMBOL(devm_drm_of_get_bridge);