xref: /linux/drivers/gpu/drm/bridge/panel.c (revision 3f0a50f345f78183f6e9b39c2f45ca5dcaa511ca)
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_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>
16 
17 struct panel_bridge {
18 	struct drm_bridge bridge;
19 	struct drm_connector connector;
20 	struct drm_panel *panel;
21 	u32 connector_type;
22 };
23 
24 static inline struct panel_bridge *
25 drm_bridge_to_panel_bridge(struct drm_bridge *bridge)
26 {
27 	return container_of(bridge, struct panel_bridge, bridge);
28 }
29 
30 static inline struct panel_bridge *
31 drm_connector_to_panel_bridge(struct drm_connector *connector)
32 {
33 	return container_of(connector, struct panel_bridge, connector);
34 }
35 
36 static int panel_bridge_connector_get_modes(struct drm_connector *connector)
37 {
38 	struct panel_bridge *panel_bridge =
39 		drm_connector_to_panel_bridge(connector);
40 
41 	return drm_panel_get_modes(panel_bridge->panel, connector);
42 }
43 
44 static const struct drm_connector_helper_funcs
45 panel_bridge_connector_helper_funcs = {
46 	.get_modes = panel_bridge_connector_get_modes,
47 };
48 
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,
55 };
56 
57 static int panel_bridge_attach(struct drm_bridge *bridge,
58 			       enum drm_bridge_attach_flags flags)
59 {
60 	struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
61 	struct drm_connector *connector = &panel_bridge->connector;
62 	int ret;
63 
64 	if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
65 		return 0;
66 
67 	if (!bridge->encoder) {
68 		DRM_ERROR("Missing encoder\n");
69 		return -ENODEV;
70 	}
71 
72 	drm_connector_helper_add(connector,
73 				 &panel_bridge_connector_helper_funcs);
74 
75 	ret = drm_connector_init(bridge->dev, connector,
76 				 &panel_bridge_connector_funcs,
77 				 panel_bridge->connector_type);
78 	if (ret) {
79 		DRM_ERROR("Failed to initialize connector\n");
80 		return ret;
81 	}
82 
83 	drm_connector_attach_encoder(&panel_bridge->connector,
84 					  bridge->encoder);
85 
86 	return 0;
87 }
88 
89 static void panel_bridge_detach(struct drm_bridge *bridge)
90 {
91 	struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
92 	struct drm_connector *connector = &panel_bridge->connector;
93 
94 	/*
95 	 * Cleanup the connector if we know it was initialized.
96 	 *
97 	 * FIXME: This wouldn't be needed if the panel_bridge structure was
98 	 * allocated with drmm_kzalloc(). This might be tricky since the
99 	 * drm_device pointer can only be retrieved when the bridge is attached.
100 	 */
101 	if (connector->dev)
102 		drm_connector_cleanup(connector);
103 }
104 
105 static void panel_bridge_pre_enable(struct drm_bridge *bridge)
106 {
107 	struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
108 
109 	drm_panel_prepare(panel_bridge->panel);
110 }
111 
112 static void panel_bridge_enable(struct drm_bridge *bridge)
113 {
114 	struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
115 
116 	drm_panel_enable(panel_bridge->panel);
117 }
118 
119 static void panel_bridge_disable(struct drm_bridge *bridge)
120 {
121 	struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
122 
123 	drm_panel_disable(panel_bridge->panel);
124 }
125 
126 static void panel_bridge_post_disable(struct drm_bridge *bridge)
127 {
128 	struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
129 
130 	drm_panel_unprepare(panel_bridge->panel);
131 }
132 
133 static int panel_bridge_get_modes(struct drm_bridge *bridge,
134 				  struct drm_connector *connector)
135 {
136 	struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
137 
138 	return drm_panel_get_modes(panel_bridge->panel, connector);
139 }
140 
141 static void panel_bridge_debugfs_init(struct drm_bridge *bridge,
142 				      struct dentry *root)
143 {
144 	struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
145 	struct drm_panel *panel = panel_bridge->panel;
146 
147 	root = debugfs_create_dir("panel", root);
148 	if (panel->funcs->debugfs_init)
149 		panel->funcs->debugfs_init(panel, root);
150 }
151 
152 static const struct drm_bridge_funcs panel_bridge_bridge_funcs = {
153 	.attach = panel_bridge_attach,
154 	.detach = panel_bridge_detach,
155 	.pre_enable = panel_bridge_pre_enable,
156 	.enable = panel_bridge_enable,
157 	.disable = panel_bridge_disable,
158 	.post_disable = panel_bridge_post_disable,
159 	.get_modes = panel_bridge_get_modes,
160 	.atomic_reset = drm_atomic_helper_bridge_reset,
161 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
162 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
163 	.atomic_get_input_bus_fmts = drm_atomic_helper_bridge_propagate_bus_fmt,
164 	.debugfs_init = panel_bridge_debugfs_init,
165 };
166 
167 /**
168  * drm_panel_bridge_add - Creates a &drm_bridge and &drm_connector that
169  * just calls the appropriate functions from &drm_panel.
170  *
171  * @panel: The drm_panel being wrapped.  Must be non-NULL.
172  *
173  * For drivers converting from directly using drm_panel: The expected
174  * usage pattern is that during either encoder module probe or DSI
175  * host attach, a drm_panel will be looked up through
176  * drm_of_find_panel_or_bridge().  drm_panel_bridge_add() is used to
177  * wrap that panel in the new bridge, and the result can then be
178  * passed to drm_bridge_attach().  The drm_panel_prepare() and related
179  * functions can be dropped from the encoder driver (they're now
180  * called by the KMS helpers before calling into the encoder), along
181  * with connector creation.  When done with the bridge (after
182  * drm_mode_config_cleanup() if the bridge has already been attached), then
183  * drm_panel_bridge_remove() to free it.
184  *
185  * The connector type is set to @panel->connector_type, which must be set to a
186  * known type. Calling this function with a panel whose connector type is
187  * DRM_MODE_CONNECTOR_Unknown will return ERR_PTR(-EINVAL).
188  *
189  * See devm_drm_panel_bridge_add() for an automatically managed version of this
190  * function.
191  */
192 struct drm_bridge *drm_panel_bridge_add(struct drm_panel *panel)
193 {
194 	if (WARN_ON(panel->connector_type == DRM_MODE_CONNECTOR_Unknown))
195 		return ERR_PTR(-EINVAL);
196 
197 	return drm_panel_bridge_add_typed(panel, panel->connector_type);
198 }
199 EXPORT_SYMBOL(drm_panel_bridge_add);
200 
201 /**
202  * drm_panel_bridge_add_typed - Creates a &drm_bridge and &drm_connector with
203  * an explicit connector type.
204  * @panel: The drm_panel being wrapped.  Must be non-NULL.
205  * @connector_type: The connector type (DRM_MODE_CONNECTOR_*)
206  *
207  * This is just like drm_panel_bridge_add(), but forces the connector type to
208  * @connector_type instead of infering it from the panel.
209  *
210  * This function is deprecated and should not be used in new drivers. Use
211  * drm_panel_bridge_add() instead, and fix panel drivers as necessary if they
212  * don't report a connector type.
213  */
214 struct drm_bridge *drm_panel_bridge_add_typed(struct drm_panel *panel,
215 					      u32 connector_type)
216 {
217 	struct panel_bridge *panel_bridge;
218 
219 	if (!panel)
220 		return ERR_PTR(-EINVAL);
221 
222 	panel_bridge = devm_kzalloc(panel->dev, sizeof(*panel_bridge),
223 				    GFP_KERNEL);
224 	if (!panel_bridge)
225 		return ERR_PTR(-ENOMEM);
226 
227 	panel_bridge->connector_type = connector_type;
228 	panel_bridge->panel = panel;
229 
230 	panel_bridge->bridge.funcs = &panel_bridge_bridge_funcs;
231 #ifdef CONFIG_OF
232 	panel_bridge->bridge.of_node = panel->dev->of_node;
233 #endif
234 	panel_bridge->bridge.ops = DRM_BRIDGE_OP_MODES;
235 	panel_bridge->bridge.type = connector_type;
236 
237 	drm_bridge_add(&panel_bridge->bridge);
238 
239 	return &panel_bridge->bridge;
240 }
241 EXPORT_SYMBOL(drm_panel_bridge_add_typed);
242 
243 /**
244  * drm_panel_bridge_remove - Unregisters and frees a drm_bridge
245  * created by drm_panel_bridge_add().
246  *
247  * @bridge: The drm_bridge being freed.
248  */
249 void drm_panel_bridge_remove(struct drm_bridge *bridge)
250 {
251 	struct panel_bridge *panel_bridge;
252 
253 	if (!bridge)
254 		return;
255 
256 	if (bridge->funcs != &panel_bridge_bridge_funcs)
257 		return;
258 
259 	panel_bridge = drm_bridge_to_panel_bridge(bridge);
260 
261 	drm_bridge_remove(bridge);
262 	devm_kfree(panel_bridge->panel->dev, bridge);
263 }
264 EXPORT_SYMBOL(drm_panel_bridge_remove);
265 
266 static void devm_drm_panel_bridge_release(struct device *dev, void *res)
267 {
268 	struct drm_bridge **bridge = res;
269 
270 	drm_panel_bridge_remove(*bridge);
271 }
272 
273 /**
274  * devm_drm_panel_bridge_add - Creates a managed &drm_bridge and &drm_connector
275  * that just calls the appropriate functions from &drm_panel.
276  * @dev: device to tie the bridge lifetime to
277  * @panel: The drm_panel being wrapped.  Must be non-NULL.
278  *
279  * This is the managed version of drm_panel_bridge_add() which automatically
280  * calls drm_panel_bridge_remove() when @dev is unbound.
281  */
282 struct drm_bridge *devm_drm_panel_bridge_add(struct device *dev,
283 					     struct drm_panel *panel)
284 {
285 	if (WARN_ON(panel->connector_type == DRM_MODE_CONNECTOR_Unknown))
286 		return ERR_PTR(-EINVAL);
287 
288 	return devm_drm_panel_bridge_add_typed(dev, panel,
289 					       panel->connector_type);
290 }
291 EXPORT_SYMBOL(devm_drm_panel_bridge_add);
292 
293 /**
294  * devm_drm_panel_bridge_add_typed - Creates a managed &drm_bridge and
295  * &drm_connector with an explicit connector type.
296  * @dev: device to tie the bridge lifetime to
297  * @panel: The drm_panel being wrapped.  Must be non-NULL.
298  * @connector_type: The connector type (DRM_MODE_CONNECTOR_*)
299  *
300  * This is just like devm_drm_panel_bridge_add(), but forces the connector type
301  * to @connector_type instead of infering it from the panel.
302  *
303  * This function is deprecated and should not be used in new drivers. Use
304  * devm_drm_panel_bridge_add() instead, and fix panel drivers as necessary if
305  * they don't report a connector type.
306  */
307 struct drm_bridge *devm_drm_panel_bridge_add_typed(struct device *dev,
308 						   struct drm_panel *panel,
309 						   u32 connector_type)
310 {
311 	struct drm_bridge **ptr, *bridge;
312 
313 	ptr = devres_alloc(devm_drm_panel_bridge_release, sizeof(*ptr),
314 			   GFP_KERNEL);
315 	if (!ptr)
316 		return ERR_PTR(-ENOMEM);
317 
318 	bridge = drm_panel_bridge_add_typed(panel, connector_type);
319 	if (!IS_ERR(bridge)) {
320 		*ptr = bridge;
321 		devres_add(dev, ptr);
322 	} else {
323 		devres_free(ptr);
324 	}
325 
326 	return bridge;
327 }
328 EXPORT_SYMBOL(devm_drm_panel_bridge_add_typed);
329 
330 /**
331  * drm_panel_bridge_connector - return the connector for the panel bridge
332  * @bridge: The drm_bridge.
333  *
334  * drm_panel_bridge creates the connector.
335  * This function gives external access to the connector.
336  *
337  * Returns: Pointer to drm_connector
338  */
339 struct drm_connector *drm_panel_bridge_connector(struct drm_bridge *bridge)
340 {
341 	struct panel_bridge *panel_bridge;
342 
343 	panel_bridge = drm_bridge_to_panel_bridge(bridge);
344 
345 	return &panel_bridge->connector;
346 }
347 EXPORT_SYMBOL(drm_panel_bridge_connector);
348 
349 #ifdef CONFIG_OF
350 /**
351  * devm_drm_of_get_bridge - Return next bridge in the chain
352  * @dev: device to tie the bridge lifetime to
353  * @np: device tree node containing encoder output ports
354  * @port: port in the device tree node
355  * @endpoint: endpoint in the device tree node
356  *
357  * Given a DT node's port and endpoint number, finds the connected node
358  * and returns the associated bridge if any, or creates and returns a
359  * drm panel bridge instance if a panel is connected.
360  *
361  * Returns a pointer to the bridge if successful, or an error pointer
362  * otherwise.
363  */
364 struct drm_bridge *devm_drm_of_get_bridge(struct device *dev,
365 					  struct device_node *np,
366 					  u32 port, u32 endpoint)
367 {
368 	struct drm_bridge *bridge;
369 	struct drm_panel *panel;
370 	int ret;
371 
372 	ret = drm_of_find_panel_or_bridge(np, port, endpoint,
373 					  &panel, &bridge);
374 	if (ret)
375 		return ERR_PTR(ret);
376 
377 	if (panel)
378 		bridge = devm_drm_panel_bridge_add(dev, panel);
379 
380 	return bridge;
381 }
382 EXPORT_SYMBOL(devm_drm_of_get_bridge);
383 #endif
384