• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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>
17 
18 struct panel_bridge {
19 	struct drm_bridge bridge;
20 	struct drm_connector connector;
21 	struct drm_panel *panel;
22 	u32 connector_type;
23 };
24 
25 static inline struct panel_bridge *
drm_bridge_to_panel_bridge(struct drm_bridge * bridge)26 drm_bridge_to_panel_bridge(struct drm_bridge *bridge)
27 {
28 	return container_of(bridge, struct panel_bridge, bridge);
29 }
30 
31 static inline struct panel_bridge *
drm_connector_to_panel_bridge(struct drm_connector * connector)32 drm_connector_to_panel_bridge(struct drm_connector *connector)
33 {
34 	return container_of(connector, struct panel_bridge, connector);
35 }
36 
panel_bridge_connector_get_modes(struct drm_connector * connector)37 static int panel_bridge_connector_get_modes(struct drm_connector *connector)
38 {
39 	struct panel_bridge *panel_bridge =
40 		drm_connector_to_panel_bridge(connector);
41 
42 	return drm_panel_get_modes(panel_bridge->panel, connector);
43 }
44 
45 static const struct drm_connector_helper_funcs
46 panel_bridge_connector_helper_funcs = {
47 	.get_modes = panel_bridge_connector_get_modes,
48 };
49 
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,
56 };
57 
panel_bridge_attach(struct drm_bridge * bridge,enum drm_bridge_attach_flags flags)58 static int panel_bridge_attach(struct drm_bridge *bridge,
59 			       enum drm_bridge_attach_flags flags)
60 {
61 	struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
62 	struct drm_connector *connector = &panel_bridge->connector;
63 	int ret;
64 
65 	if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
66 		return 0;
67 
68 	if (!bridge->encoder) {
69 		DRM_ERROR("Missing encoder\n");
70 		return -ENODEV;
71 	}
72 
73 	drm_connector_helper_add(connector,
74 				 &panel_bridge_connector_helper_funcs);
75 
76 	ret = drm_connector_init(bridge->dev, connector,
77 				 &panel_bridge_connector_funcs,
78 				 panel_bridge->connector_type);
79 	if (ret) {
80 		DRM_ERROR("Failed to initialize connector\n");
81 		return ret;
82 	}
83 
84 	drm_connector_attach_encoder(&panel_bridge->connector,
85 					  bridge->encoder);
86 
87 	if (bridge->dev->registered) {
88 		if (connector->funcs->reset)
89 			connector->funcs->reset(connector);
90 		drm_connector_register(connector);
91 	}
92 
93 	return 0;
94 }
95 
panel_bridge_detach(struct drm_bridge * bridge)96 static void panel_bridge_detach(struct drm_bridge *bridge)
97 {
98 	struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
99 	struct drm_connector *connector = &panel_bridge->connector;
100 
101 	/*
102 	 * Cleanup the connector if we know it was initialized.
103 	 *
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.
107 	 */
108 	if (connector->dev)
109 		drm_connector_cleanup(connector);
110 }
111 
panel_bridge_pre_enable(struct drm_bridge * bridge)112 static void panel_bridge_pre_enable(struct drm_bridge *bridge)
113 {
114 	struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
115 
116 	drm_panel_prepare(panel_bridge->panel);
117 }
118 
panel_bridge_enable(struct drm_bridge * bridge)119 static void panel_bridge_enable(struct drm_bridge *bridge)
120 {
121 	struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
122 
123 	drm_panel_enable(panel_bridge->panel);
124 }
125 
panel_bridge_disable(struct drm_bridge * bridge)126 static void panel_bridge_disable(struct drm_bridge *bridge)
127 {
128 	struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
129 
130 	drm_panel_disable(panel_bridge->panel);
131 }
132 
panel_bridge_post_disable(struct drm_bridge * bridge)133 static void panel_bridge_post_disable(struct drm_bridge *bridge)
134 {
135 	struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
136 
137 	drm_panel_unprepare(panel_bridge->panel);
138 }
139 
panel_bridge_get_modes(struct drm_bridge * bridge,struct drm_connector * connector)140 static int panel_bridge_get_modes(struct drm_bridge *bridge,
141 				  struct drm_connector *connector)
142 {
143 	struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
144 
145 	return drm_panel_get_modes(panel_bridge->panel, connector);
146 }
147 
panel_bridge_debugfs_init(struct drm_bridge * bridge,struct dentry * root)148 static void panel_bridge_debugfs_init(struct drm_bridge *bridge,
149 				      struct dentry *root)
150 {
151 	struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
152 	struct drm_panel *panel = panel_bridge->panel;
153 
154 	root = debugfs_create_dir("panel", root);
155 	if (panel->funcs->debugfs_init)
156 		panel->funcs->debugfs_init(panel, root);
157 }
158 
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,
172 };
173 
174 /**
175  * drm_bridge_is_panel - Checks if a drm_bridge is a panel_bridge.
176  *
177  * @bridge: The drm_bridge to be checked.
178  *
179  * Returns true if the bridge is a panel bridge, or false otherwise.
180  */
drm_bridge_is_panel(const struct drm_bridge * bridge)181 bool drm_bridge_is_panel(const struct drm_bridge *bridge)
182 {
183 	return bridge->funcs == &panel_bridge_bridge_funcs;
184 }
185 EXPORT_SYMBOL(drm_bridge_is_panel);
186 
187 /**
188  * drm_panel_bridge_add - Creates a &drm_bridge and &drm_connector that
189  * just calls the appropriate functions from &drm_panel.
190  *
191  * @panel: The drm_panel being wrapped.  Must be non-NULL.
192  *
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.
204  *
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).
208  *
209  * See devm_drm_panel_bridge_add() for an automatically managed version of this
210  * function.
211  */
drm_panel_bridge_add(struct drm_panel * panel)212 struct drm_bridge *drm_panel_bridge_add(struct drm_panel *panel)
213 {
214 	if (WARN_ON(panel->connector_type == DRM_MODE_CONNECTOR_Unknown))
215 		return ERR_PTR(-EINVAL);
216 
217 	return drm_panel_bridge_add_typed(panel, panel->connector_type);
218 }
219 EXPORT_SYMBOL(drm_panel_bridge_add);
220 
221 /**
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_*)
226  *
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.
229  *
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.
233  */
drm_panel_bridge_add_typed(struct drm_panel * panel,u32 connector_type)234 struct drm_bridge *drm_panel_bridge_add_typed(struct drm_panel *panel,
235 					      u32 connector_type)
236 {
237 	struct panel_bridge *panel_bridge;
238 
239 	if (!panel)
240 		return ERR_PTR(-EINVAL);
241 
242 	panel_bridge = devm_kzalloc(panel->dev, sizeof(*panel_bridge),
243 				    GFP_KERNEL);
244 	if (!panel_bridge)
245 		return ERR_PTR(-ENOMEM);
246 
247 	panel_bridge->connector_type = connector_type;
248 	panel_bridge->panel = panel;
249 
250 	panel_bridge->bridge.funcs = &panel_bridge_bridge_funcs;
251 #ifdef CONFIG_OF
252 	panel_bridge->bridge.of_node = panel->dev->of_node;
253 #endif
254 	panel_bridge->bridge.ops = DRM_BRIDGE_OP_MODES;
255 	panel_bridge->bridge.type = connector_type;
256 
257 	drm_bridge_add(&panel_bridge->bridge);
258 
259 	return &panel_bridge->bridge;
260 }
261 EXPORT_SYMBOL(drm_panel_bridge_add_typed);
262 
263 /**
264  * drm_panel_bridge_remove - Unregisters and frees a drm_bridge
265  * created by drm_panel_bridge_add().
266  *
267  * @bridge: The drm_bridge being freed.
268  */
drm_panel_bridge_remove(struct drm_bridge * bridge)269 void drm_panel_bridge_remove(struct drm_bridge *bridge)
270 {
271 	struct panel_bridge *panel_bridge;
272 
273 	if (!bridge)
274 		return;
275 
276 	if (bridge->funcs != &panel_bridge_bridge_funcs)
277 		return;
278 
279 	panel_bridge = drm_bridge_to_panel_bridge(bridge);
280 
281 	drm_bridge_remove(bridge);
282 	devm_kfree(panel_bridge->panel->dev, bridge);
283 }
284 EXPORT_SYMBOL(drm_panel_bridge_remove);
285 
286 /**
287  * drm_panel_bridge_set_orientation - Set the connector's panel orientation
288  * from the bridge that can be transformed to panel bridge.
289  *
290  * @connector: The connector to be set panel orientation.
291  * @bridge: The drm_bridge to be transformed to panel bridge.
292  *
293  * Returns 0 on success, negative errno on failure.
294  */
drm_panel_bridge_set_orientation(struct drm_connector * connector,struct drm_bridge * bridge)295 int drm_panel_bridge_set_orientation(struct drm_connector *connector,
296 				     struct drm_bridge *bridge)
297 {
298 	struct panel_bridge *panel_bridge;
299 
300 	panel_bridge = drm_bridge_to_panel_bridge(bridge);
301 
302 	return drm_connector_set_orientation_from_panel(connector,
303 							panel_bridge->panel);
304 }
305 EXPORT_SYMBOL(drm_panel_bridge_set_orientation);
306 
devm_drm_panel_bridge_release(struct device * dev,void * res)307 static void devm_drm_panel_bridge_release(struct device *dev, void *res)
308 {
309 	struct drm_bridge **bridge = res;
310 
311 	drm_panel_bridge_remove(*bridge);
312 }
313 
314 /**
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.
319  *
320  * This is the managed version of drm_panel_bridge_add() which automatically
321  * calls drm_panel_bridge_remove() when @dev is unbound.
322  */
devm_drm_panel_bridge_add(struct device * dev,struct drm_panel * panel)323 struct drm_bridge *devm_drm_panel_bridge_add(struct device *dev,
324 					     struct drm_panel *panel)
325 {
326 	if (WARN_ON(panel->connector_type == DRM_MODE_CONNECTOR_Unknown))
327 		return ERR_PTR(-EINVAL);
328 
329 	return devm_drm_panel_bridge_add_typed(dev, panel,
330 					       panel->connector_type);
331 }
332 EXPORT_SYMBOL(devm_drm_panel_bridge_add);
333 
334 /**
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_*)
340  *
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.
343  *
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.
347  */
devm_drm_panel_bridge_add_typed(struct device * dev,struct drm_panel * panel,u32 connector_type)348 struct drm_bridge *devm_drm_panel_bridge_add_typed(struct device *dev,
349 						   struct drm_panel *panel,
350 						   u32 connector_type)
351 {
352 	struct drm_bridge **ptr, *bridge;
353 
354 	ptr = devres_alloc(devm_drm_panel_bridge_release, sizeof(*ptr),
355 			   GFP_KERNEL);
356 	if (!ptr)
357 		return ERR_PTR(-ENOMEM);
358 
359 	bridge = drm_panel_bridge_add_typed(panel, connector_type);
360 	if (!IS_ERR(bridge)) {
361 		*ptr = bridge;
362 		devres_add(dev, ptr);
363 	} else {
364 		devres_free(ptr);
365 	}
366 
367 	return bridge;
368 }
369 EXPORT_SYMBOL(devm_drm_panel_bridge_add_typed);
370 
drmm_drm_panel_bridge_release(struct drm_device * drm,void * ptr)371 static void drmm_drm_panel_bridge_release(struct drm_device *drm, void *ptr)
372 {
373 	struct drm_bridge *bridge = ptr;
374 
375 	drm_panel_bridge_remove(bridge);
376 }
377 
378 /**
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.
382  *
383  * @drm: DRM device to tie the bridge lifetime to
384  * @panel: The drm_panel being wrapped.  Must be non-NULL.
385  *
386  * This is the DRM-managed version of drm_panel_bridge_add() which
387  * automatically calls drm_panel_bridge_remove() when @dev is cleaned
388  * up.
389  */
drmm_panel_bridge_add(struct drm_device * drm,struct drm_panel * panel)390 struct drm_bridge *drmm_panel_bridge_add(struct drm_device *drm,
391 					 struct drm_panel *panel)
392 {
393 	struct drm_bridge *bridge;
394 	int ret;
395 
396 	bridge = drm_panel_bridge_add_typed(panel, panel->connector_type);
397 	if (IS_ERR(bridge))
398 		return bridge;
399 
400 	ret = drmm_add_action_or_reset(drm, drmm_drm_panel_bridge_release,
401 				       bridge);
402 	if (ret)
403 		return ERR_PTR(ret);
404 
405 	return bridge;
406 }
407 EXPORT_SYMBOL(drmm_panel_bridge_add);
408 
409 /**
410  * drm_panel_bridge_connector - return the connector for the panel bridge
411  * @bridge: The drm_bridge.
412  *
413  * drm_panel_bridge creates the connector.
414  * This function gives external access to the connector.
415  *
416  * Returns: Pointer to drm_connector
417  */
drm_panel_bridge_connector(struct drm_bridge * bridge)418 struct drm_connector *drm_panel_bridge_connector(struct drm_bridge *bridge)
419 {
420 	struct panel_bridge *panel_bridge;
421 
422 	panel_bridge = drm_bridge_to_panel_bridge(bridge);
423 
424 	return &panel_bridge->connector;
425 }
426 EXPORT_SYMBOL(drm_panel_bridge_connector);
427 
428 #ifdef CONFIG_OF
429 /**
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
435  *
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.
439  *
440  * Returns a pointer to the bridge if successful, or an error pointer
441  * otherwise.
442  */
devm_drm_of_get_bridge(struct device * dev,struct device_node * np,u32 port,u32 endpoint)443 struct drm_bridge *devm_drm_of_get_bridge(struct device *dev,
444 					  struct device_node *np,
445 					  u32 port, u32 endpoint)
446 {
447 	struct drm_bridge *bridge;
448 	struct drm_panel *panel;
449 	int ret;
450 
451 	ret = drm_of_find_panel_or_bridge(np, port, endpoint,
452 					  &panel, &bridge);
453 	if (ret)
454 		return ERR_PTR(ret);
455 
456 	if (panel)
457 		bridge = devm_drm_panel_bridge_add(dev, panel);
458 
459 	return bridge;
460 }
461 EXPORT_SYMBOL(devm_drm_of_get_bridge);
462 
463 /**
464  * drmm_of_get_bridge - Return next bridge in the chain
465  * @drm: device to tie the bridge lifetime to
466  * @np: device tree node containing encoder output ports
467  * @port: port in the device tree node
468  * @endpoint: endpoint in the device tree node
469  *
470  * Given a DT node's port and endpoint number, finds the connected node
471  * and returns the associated bridge if any, or creates and returns a
472  * drm panel bridge instance if a panel is connected.
473  *
474  * Returns a drmm managed pointer to the bridge if successful, or an error
475  * pointer otherwise.
476  */
drmm_of_get_bridge(struct drm_device * drm,struct device_node * np,u32 port,u32 endpoint)477 struct drm_bridge *drmm_of_get_bridge(struct drm_device *drm,
478 				      struct device_node *np,
479 				      u32 port, u32 endpoint)
480 {
481 	struct drm_bridge *bridge;
482 	struct drm_panel *panel;
483 	int ret;
484 
485 	ret = drm_of_find_panel_or_bridge(np, port, endpoint,
486 					  &panel, &bridge);
487 	if (ret)
488 		return ERR_PTR(ret);
489 
490 	if (panel)
491 		bridge = drmm_panel_bridge_add(drm, panel);
492 
493 	return bridge;
494 }
495 EXPORT_SYMBOL(drmm_of_get_bridge);
496 
497 #endif
498