xref: /linux/drivers/gpu/drm/bridge/panel.c (revision d7f39aee79f04eeaa42085728423501b33ac5be5)
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 <linux/debugfs.h>
8 
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>
19 
20 struct panel_bridge {
21 	struct drm_bridge bridge;
22 	struct drm_connector connector;
23 	struct drm_panel *panel;
24 	u32 connector_type;
25 };
26 
27 static inline struct panel_bridge *
28 drm_bridge_to_panel_bridge(struct drm_bridge *bridge)
29 {
30 	return container_of(bridge, struct panel_bridge, bridge);
31 }
32 
33 static inline struct panel_bridge *
34 drm_connector_to_panel_bridge(struct drm_connector *connector)
35 {
36 	return container_of(connector, struct panel_bridge, connector);
37 }
38 
39 static int panel_bridge_connector_get_modes(struct drm_connector *connector)
40 {
41 	struct panel_bridge *panel_bridge =
42 		drm_connector_to_panel_bridge(connector);
43 
44 	return drm_panel_get_modes(panel_bridge->panel, connector);
45 }
46 
47 static const struct drm_connector_helper_funcs
48 panel_bridge_connector_helper_funcs = {
49 	.get_modes = panel_bridge_connector_get_modes,
50 };
51 
52 static const struct drm_connector_funcs panel_bridge_connector_funcs = {
53 	.reset = drm_atomic_helper_connector_reset,
54 	.fill_modes = drm_helper_probe_single_connector_modes,
55 	.destroy = drm_connector_cleanup,
56 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
57 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
58 };
59 
60 static int panel_bridge_attach(struct drm_bridge *bridge,
61 			       enum drm_bridge_attach_flags flags)
62 {
63 	struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
64 	struct drm_connector *connector = &panel_bridge->connector;
65 	int ret;
66 
67 	if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
68 		return 0;
69 
70 	if (!bridge->encoder) {
71 		DRM_ERROR("Missing encoder\n");
72 		return -ENODEV;
73 	}
74 
75 	drm_connector_helper_add(connector,
76 				 &panel_bridge_connector_helper_funcs);
77 
78 	ret = drm_connector_init(bridge->dev, connector,
79 				 &panel_bridge_connector_funcs,
80 				 panel_bridge->connector_type);
81 	if (ret) {
82 		DRM_ERROR("Failed to initialize connector\n");
83 		return ret;
84 	}
85 
86 	drm_panel_bridge_set_orientation(connector, bridge);
87 
88 	drm_connector_attach_encoder(&panel_bridge->connector,
89 					  bridge->encoder);
90 
91 	if (bridge->dev->registered) {
92 		if (connector->funcs->reset)
93 			connector->funcs->reset(connector);
94 		drm_connector_register(connector);
95 	}
96 
97 	return 0;
98 }
99 
100 static void panel_bridge_detach(struct drm_bridge *bridge)
101 {
102 	struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
103 	struct drm_connector *connector = &panel_bridge->connector;
104 
105 	/*
106 	 * Cleanup the connector if we know it was initialized.
107 	 *
108 	 * FIXME: This wouldn't be needed if the panel_bridge structure was
109 	 * allocated with drmm_kzalloc(). This might be tricky since the
110 	 * drm_device pointer can only be retrieved when the bridge is attached.
111 	 */
112 	if (connector->dev)
113 		drm_connector_cleanup(connector);
114 }
115 
116 static void panel_bridge_atomic_pre_enable(struct drm_bridge *bridge,
117 				struct drm_bridge_state *old_bridge_state)
118 {
119 	struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
120 	struct drm_atomic_state *atomic_state = old_bridge_state->base.state;
121 	struct drm_encoder *encoder = bridge->encoder;
122 	struct drm_crtc *crtc;
123 	struct drm_crtc_state *old_crtc_state;
124 
125 	crtc = drm_atomic_get_new_crtc_for_encoder(atomic_state, encoder);
126 	if (!crtc)
127 		return;
128 
129 	old_crtc_state = drm_atomic_get_old_crtc_state(atomic_state, crtc);
130 	if (old_crtc_state && old_crtc_state->self_refresh_active)
131 		return;
132 
133 	drm_panel_prepare(panel_bridge->panel);
134 }
135 
136 static void panel_bridge_atomic_enable(struct drm_bridge *bridge,
137 				struct drm_bridge_state *old_bridge_state)
138 {
139 	struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
140 	struct drm_atomic_state *atomic_state = old_bridge_state->base.state;
141 	struct drm_encoder *encoder = bridge->encoder;
142 	struct drm_crtc *crtc;
143 	struct drm_crtc_state *old_crtc_state;
144 
145 	crtc = drm_atomic_get_new_crtc_for_encoder(atomic_state, encoder);
146 	if (!crtc)
147 		return;
148 
149 	old_crtc_state = drm_atomic_get_old_crtc_state(atomic_state, crtc);
150 	if (old_crtc_state && old_crtc_state->self_refresh_active)
151 		return;
152 
153 	drm_panel_enable(panel_bridge->panel);
154 }
155 
156 static void panel_bridge_atomic_disable(struct drm_bridge *bridge,
157 				struct drm_bridge_state *old_bridge_state)
158 {
159 	struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
160 	struct drm_atomic_state *atomic_state = old_bridge_state->base.state;
161 	struct drm_encoder *encoder = bridge->encoder;
162 	struct drm_crtc *crtc;
163 	struct drm_crtc_state *new_crtc_state;
164 
165 	crtc = drm_atomic_get_old_crtc_for_encoder(atomic_state, encoder);
166 	if (!crtc)
167 		return;
168 
169 	new_crtc_state = drm_atomic_get_new_crtc_state(atomic_state, crtc);
170 	if (new_crtc_state && new_crtc_state->self_refresh_active)
171 		return;
172 
173 	drm_panel_disable(panel_bridge->panel);
174 }
175 
176 static void panel_bridge_atomic_post_disable(struct drm_bridge *bridge,
177 				struct drm_bridge_state *old_bridge_state)
178 {
179 	struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
180 	struct drm_atomic_state *atomic_state = old_bridge_state->base.state;
181 	struct drm_encoder *encoder = bridge->encoder;
182 	struct drm_crtc *crtc;
183 	struct drm_crtc_state *new_crtc_state;
184 
185 	crtc = drm_atomic_get_old_crtc_for_encoder(atomic_state, encoder);
186 	if (!crtc)
187 		return;
188 
189 	new_crtc_state = drm_atomic_get_new_crtc_state(atomic_state, crtc);
190 	if (new_crtc_state && new_crtc_state->self_refresh_active)
191 		return;
192 
193 	drm_panel_unprepare(panel_bridge->panel);
194 }
195 
196 static int panel_bridge_get_modes(struct drm_bridge *bridge,
197 				  struct drm_connector *connector)
198 {
199 	struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
200 
201 	return drm_panel_get_modes(panel_bridge->panel, connector);
202 }
203 
204 static void panel_bridge_debugfs_init(struct drm_bridge *bridge,
205 				      struct dentry *root)
206 {
207 	struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
208 	struct drm_panel *panel = panel_bridge->panel;
209 
210 	root = debugfs_create_dir("panel", root);
211 	if (panel->funcs->debugfs_init)
212 		panel->funcs->debugfs_init(panel, root);
213 }
214 
215 static const struct drm_bridge_funcs panel_bridge_bridge_funcs = {
216 	.attach = panel_bridge_attach,
217 	.detach = panel_bridge_detach,
218 	.atomic_pre_enable = panel_bridge_atomic_pre_enable,
219 	.atomic_enable = panel_bridge_atomic_enable,
220 	.atomic_disable = panel_bridge_atomic_disable,
221 	.atomic_post_disable = panel_bridge_atomic_post_disable,
222 	.get_modes = panel_bridge_get_modes,
223 	.atomic_reset = drm_atomic_helper_bridge_reset,
224 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
225 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
226 	.atomic_get_input_bus_fmts = drm_atomic_helper_bridge_propagate_bus_fmt,
227 	.debugfs_init = panel_bridge_debugfs_init,
228 };
229 
230 /**
231  * drm_bridge_is_panel - Checks if a drm_bridge is a panel_bridge.
232  *
233  * @bridge: The drm_bridge to be checked.
234  *
235  * Returns true if the bridge is a panel bridge, or false otherwise.
236  */
237 bool drm_bridge_is_panel(const struct drm_bridge *bridge)
238 {
239 	return bridge->funcs == &panel_bridge_bridge_funcs;
240 }
241 EXPORT_SYMBOL(drm_bridge_is_panel);
242 
243 /**
244  * drm_panel_bridge_add - Creates a &drm_bridge and &drm_connector that
245  * just calls the appropriate functions from &drm_panel.
246  *
247  * @panel: The drm_panel being wrapped.  Must be non-NULL.
248  *
249  * For drivers converting from directly using drm_panel: The expected
250  * usage pattern is that during either encoder module probe or DSI
251  * host attach, a drm_panel will be looked up through
252  * drm_of_find_panel_or_bridge().  drm_panel_bridge_add() is used to
253  * wrap that panel in the new bridge, and the result can then be
254  * passed to drm_bridge_attach().  The drm_panel_prepare() and related
255  * functions can be dropped from the encoder driver (they're now
256  * called by the KMS helpers before calling into the encoder), along
257  * with connector creation.  When done with the bridge (after
258  * drm_mode_config_cleanup() if the bridge has already been attached), then
259  * drm_panel_bridge_remove() to free it.
260  *
261  * The connector type is set to @panel->connector_type, which must be set to a
262  * known type. Calling this function with a panel whose connector type is
263  * DRM_MODE_CONNECTOR_Unknown will return ERR_PTR(-EINVAL).
264  *
265  * See devm_drm_panel_bridge_add() for an automatically managed version of this
266  * function.
267  */
268 struct drm_bridge *drm_panel_bridge_add(struct drm_panel *panel)
269 {
270 	if (WARN_ON(panel->connector_type == DRM_MODE_CONNECTOR_Unknown))
271 		return ERR_PTR(-EINVAL);
272 
273 	return drm_panel_bridge_add_typed(panel, panel->connector_type);
274 }
275 EXPORT_SYMBOL(drm_panel_bridge_add);
276 
277 /**
278  * drm_panel_bridge_add_typed - Creates a &drm_bridge and &drm_connector with
279  * an explicit connector type.
280  * @panel: The drm_panel being wrapped.  Must be non-NULL.
281  * @connector_type: The connector type (DRM_MODE_CONNECTOR_*)
282  *
283  * This is just like drm_panel_bridge_add(), but forces the connector type to
284  * @connector_type instead of infering it from the panel.
285  *
286  * This function is deprecated and should not be used in new drivers. Use
287  * drm_panel_bridge_add() instead, and fix panel drivers as necessary if they
288  * don't report a connector type.
289  */
290 struct drm_bridge *drm_panel_bridge_add_typed(struct drm_panel *panel,
291 					      u32 connector_type)
292 {
293 	struct panel_bridge *panel_bridge;
294 
295 	if (!panel)
296 		return ERR_PTR(-EINVAL);
297 
298 	panel_bridge = devm_kzalloc(panel->dev, sizeof(*panel_bridge),
299 				    GFP_KERNEL);
300 	if (!panel_bridge)
301 		return ERR_PTR(-ENOMEM);
302 
303 	panel_bridge->connector_type = connector_type;
304 	panel_bridge->panel = panel;
305 
306 	panel_bridge->bridge.funcs = &panel_bridge_bridge_funcs;
307 	panel_bridge->bridge.of_node = panel->dev->of_node;
308 	panel_bridge->bridge.ops = DRM_BRIDGE_OP_MODES;
309 	panel_bridge->bridge.type = connector_type;
310 
311 	drm_bridge_add(&panel_bridge->bridge);
312 
313 	return &panel_bridge->bridge;
314 }
315 EXPORT_SYMBOL(drm_panel_bridge_add_typed);
316 
317 /**
318  * drm_panel_bridge_remove - Unregisters and frees a drm_bridge
319  * created by drm_panel_bridge_add().
320  *
321  * @bridge: The drm_bridge being freed.
322  */
323 void drm_panel_bridge_remove(struct drm_bridge *bridge)
324 {
325 	struct panel_bridge *panel_bridge;
326 
327 	if (!bridge)
328 		return;
329 
330 	if (bridge->funcs != &panel_bridge_bridge_funcs)
331 		return;
332 
333 	panel_bridge = drm_bridge_to_panel_bridge(bridge);
334 
335 	drm_bridge_remove(bridge);
336 	devm_kfree(panel_bridge->panel->dev, bridge);
337 }
338 EXPORT_SYMBOL(drm_panel_bridge_remove);
339 
340 /**
341  * drm_panel_bridge_set_orientation - Set the connector's panel orientation
342  * from the bridge that can be transformed to panel bridge.
343  *
344  * @connector: The connector to be set panel orientation.
345  * @bridge: The drm_bridge to be transformed to panel bridge.
346  *
347  * Returns 0 on success, negative errno on failure.
348  */
349 int drm_panel_bridge_set_orientation(struct drm_connector *connector,
350 				     struct drm_bridge *bridge)
351 {
352 	struct panel_bridge *panel_bridge;
353 
354 	panel_bridge = drm_bridge_to_panel_bridge(bridge);
355 
356 	return drm_connector_set_orientation_from_panel(connector,
357 							panel_bridge->panel);
358 }
359 EXPORT_SYMBOL(drm_panel_bridge_set_orientation);
360 
361 static void devm_drm_panel_bridge_release(struct device *dev, void *res)
362 {
363 	struct drm_bridge *bridge = *(struct drm_bridge **)res;
364 
365 	if (!bridge)
366 		return;
367 
368 	drm_bridge_remove(bridge);
369 }
370 
371 /**
372  * devm_drm_panel_bridge_add - Creates a managed &drm_bridge and &drm_connector
373  * that just calls the appropriate functions from &drm_panel.
374  * @dev: device to tie the bridge lifetime to
375  * @panel: The drm_panel being wrapped.  Must be non-NULL.
376  *
377  * This is the managed version of drm_panel_bridge_add() which automatically
378  * calls drm_panel_bridge_remove() when @dev is unbound.
379  */
380 struct drm_bridge *devm_drm_panel_bridge_add(struct device *dev,
381 					     struct drm_panel *panel)
382 {
383 	if (WARN_ON(panel->connector_type == DRM_MODE_CONNECTOR_Unknown))
384 		return ERR_PTR(-EINVAL);
385 
386 	return devm_drm_panel_bridge_add_typed(dev, panel,
387 					       panel->connector_type);
388 }
389 EXPORT_SYMBOL(devm_drm_panel_bridge_add);
390 
391 /**
392  * devm_drm_panel_bridge_add_typed - Creates a managed &drm_bridge and
393  * &drm_connector with an explicit connector type.
394  * @dev: device to tie the bridge lifetime to
395  * @panel: The drm_panel being wrapped.  Must be non-NULL.
396  * @connector_type: The connector type (DRM_MODE_CONNECTOR_*)
397  *
398  * This is just like devm_drm_panel_bridge_add(), but forces the connector type
399  * to @connector_type instead of infering it from the panel.
400  *
401  * This function is deprecated and should not be used in new drivers. Use
402  * devm_drm_panel_bridge_add() instead, and fix panel drivers as necessary if
403  * they don't report a connector type.
404  */
405 struct drm_bridge *devm_drm_panel_bridge_add_typed(struct device *dev,
406 						   struct drm_panel *panel,
407 						   u32 connector_type)
408 {
409 	struct drm_bridge **ptr, *bridge;
410 
411 	ptr = devres_alloc(devm_drm_panel_bridge_release, sizeof(*ptr),
412 			   GFP_KERNEL);
413 	if (!ptr)
414 		return ERR_PTR(-ENOMEM);
415 
416 	bridge = drm_panel_bridge_add_typed(panel, connector_type);
417 	if (IS_ERR(bridge)) {
418 		devres_free(ptr);
419 		return bridge;
420 	}
421 
422 	bridge->pre_enable_prev_first = panel->prepare_prev_first;
423 
424 	*ptr = bridge;
425 	devres_add(dev, ptr);
426 
427 	return bridge;
428 }
429 EXPORT_SYMBOL(devm_drm_panel_bridge_add_typed);
430 
431 static void drmm_drm_panel_bridge_release(struct drm_device *drm, void *ptr)
432 {
433 	struct drm_bridge *bridge = ptr;
434 
435 	drm_panel_bridge_remove(bridge);
436 }
437 
438 /**
439  * drmm_panel_bridge_add - Creates a DRM-managed &drm_bridge and
440  *                         &drm_connector that just calls the
441  *                         appropriate functions from &drm_panel.
442  *
443  * @drm: DRM device to tie the bridge lifetime to
444  * @panel: The drm_panel being wrapped.  Must be non-NULL.
445  *
446  * This is the DRM-managed version of drm_panel_bridge_add() which
447  * automatically calls drm_panel_bridge_remove() when @dev is cleaned
448  * up.
449  */
450 struct drm_bridge *drmm_panel_bridge_add(struct drm_device *drm,
451 					 struct drm_panel *panel)
452 {
453 	struct drm_bridge *bridge;
454 	int ret;
455 
456 	bridge = drm_panel_bridge_add_typed(panel, panel->connector_type);
457 	if (IS_ERR(bridge))
458 		return bridge;
459 
460 	ret = drmm_add_action_or_reset(drm, drmm_drm_panel_bridge_release,
461 				       bridge);
462 	if (ret)
463 		return ERR_PTR(ret);
464 
465 	bridge->pre_enable_prev_first = panel->prepare_prev_first;
466 
467 	return bridge;
468 }
469 EXPORT_SYMBOL(drmm_panel_bridge_add);
470 
471 /**
472  * drm_panel_bridge_connector - return the connector for the panel bridge
473  * @bridge: The drm_bridge.
474  *
475  * drm_panel_bridge creates the connector.
476  * This function gives external access to the connector.
477  *
478  * Returns: Pointer to drm_connector
479  */
480 struct drm_connector *drm_panel_bridge_connector(struct drm_bridge *bridge)
481 {
482 	struct panel_bridge *panel_bridge;
483 
484 	panel_bridge = drm_bridge_to_panel_bridge(bridge);
485 
486 	return &panel_bridge->connector;
487 }
488 EXPORT_SYMBOL(drm_panel_bridge_connector);
489 
490 #ifdef CONFIG_OF
491 /**
492  * devm_drm_of_get_bridge - Return next bridge in the chain
493  * @dev: device to tie the bridge lifetime to
494  * @np: device tree node containing encoder output ports
495  * @port: port in the device tree node
496  * @endpoint: endpoint in the device tree node
497  *
498  * Given a DT node's port and endpoint number, finds the connected node
499  * and returns the associated bridge if any, or creates and returns a
500  * drm panel bridge instance if a panel is connected.
501  *
502  * Returns a pointer to the bridge if successful, or an error pointer
503  * otherwise.
504  */
505 struct drm_bridge *devm_drm_of_get_bridge(struct device *dev,
506 					  struct device_node *np,
507 					  u32 port, u32 endpoint)
508 {
509 	struct drm_bridge *bridge;
510 	struct drm_panel *panel;
511 	int ret;
512 
513 	ret = drm_of_find_panel_or_bridge(np, port, endpoint,
514 					  &panel, &bridge);
515 	if (ret)
516 		return ERR_PTR(ret);
517 
518 	if (panel)
519 		bridge = devm_drm_panel_bridge_add(dev, panel);
520 
521 	return bridge;
522 }
523 EXPORT_SYMBOL(devm_drm_of_get_bridge);
524 
525 /**
526  * drmm_of_get_bridge - Return next bridge in the chain
527  * @drm: device to tie the bridge lifetime to
528  * @np: device tree node containing encoder output ports
529  * @port: port in the device tree node
530  * @endpoint: endpoint in the device tree node
531  *
532  * Given a DT node's port and endpoint number, finds the connected node
533  * and returns the associated bridge if any, or creates and returns a
534  * drm panel bridge instance if a panel is connected.
535  *
536  * Returns a drmm managed pointer to the bridge if successful, or an error
537  * pointer otherwise.
538  */
539 struct drm_bridge *drmm_of_get_bridge(struct drm_device *drm,
540 				      struct device_node *np,
541 				      u32 port, u32 endpoint)
542 {
543 	struct drm_bridge *bridge;
544 	struct drm_panel *panel;
545 	int ret;
546 
547 	ret = drm_of_find_panel_or_bridge(np, port, endpoint,
548 					  &panel, &bridge);
549 	if (ret)
550 		return ERR_PTR(ret);
551 
552 	if (panel)
553 		bridge = drmm_panel_bridge_add(drm, panel);
554 
555 	return bridge;
556 }
557 EXPORT_SYMBOL(drmm_of_get_bridge);
558 
559 #endif
560