xref: /openbmc/linux/drivers/gpu/drm/bridge/panel.c (revision 675aaf05)
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/drmP.h>
8 #include <drm/drm_panel.h>
9 #include <drm/drm_atomic_helper.h>
10 #include <drm/drm_connector.h>
11 #include <drm/drm_encoder.h>
12 #include <drm/drm_modeset_helper_vtables.h>
13 #include <drm/drm_probe_helper.h>
14 #include <drm/drm_panel.h>
15 
16 struct panel_bridge {
17 	struct drm_bridge bridge;
18 	struct drm_connector connector;
19 	struct drm_panel *panel;
20 	u32 connector_type;
21 };
22 
23 static inline struct panel_bridge *
24 drm_bridge_to_panel_bridge(struct drm_bridge *bridge)
25 {
26 	return container_of(bridge, struct panel_bridge, bridge);
27 }
28 
29 static inline struct panel_bridge *
30 drm_connector_to_panel_bridge(struct drm_connector *connector)
31 {
32 	return container_of(connector, struct panel_bridge, connector);
33 }
34 
35 static int panel_bridge_connector_get_modes(struct drm_connector *connector)
36 {
37 	struct panel_bridge *panel_bridge =
38 		drm_connector_to_panel_bridge(connector);
39 
40 	return drm_panel_get_modes(panel_bridge->panel);
41 }
42 
43 static const struct drm_connector_helper_funcs
44 panel_bridge_connector_helper_funcs = {
45 	.get_modes = panel_bridge_connector_get_modes,
46 };
47 
48 static const struct drm_connector_funcs panel_bridge_connector_funcs = {
49 	.reset = drm_atomic_helper_connector_reset,
50 	.fill_modes = drm_helper_probe_single_connector_modes,
51 	.destroy = drm_connector_cleanup,
52 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
53 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
54 };
55 
56 static int panel_bridge_attach(struct drm_bridge *bridge)
57 {
58 	struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
59 	struct drm_connector *connector = &panel_bridge->connector;
60 	int ret;
61 
62 	if (!bridge->encoder) {
63 		DRM_ERROR("Missing encoder\n");
64 		return -ENODEV;
65 	}
66 
67 	drm_connector_helper_add(connector,
68 				 &panel_bridge_connector_helper_funcs);
69 
70 	ret = drm_connector_init(bridge->dev, connector,
71 				 &panel_bridge_connector_funcs,
72 				 panel_bridge->connector_type);
73 	if (ret) {
74 		DRM_ERROR("Failed to initialize connector\n");
75 		return ret;
76 	}
77 
78 	drm_connector_attach_encoder(&panel_bridge->connector,
79 					  bridge->encoder);
80 
81 	ret = drm_panel_attach(panel_bridge->panel, &panel_bridge->connector);
82 	if (ret < 0)
83 		return ret;
84 
85 	return 0;
86 }
87 
88 static void panel_bridge_detach(struct drm_bridge *bridge)
89 {
90 	struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
91 
92 	drm_panel_detach(panel_bridge->panel);
93 }
94 
95 static void panel_bridge_pre_enable(struct drm_bridge *bridge)
96 {
97 	struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
98 
99 	drm_panel_prepare(panel_bridge->panel);
100 }
101 
102 static void panel_bridge_enable(struct drm_bridge *bridge)
103 {
104 	struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
105 
106 	drm_panel_enable(panel_bridge->panel);
107 }
108 
109 static void panel_bridge_disable(struct drm_bridge *bridge)
110 {
111 	struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
112 
113 	drm_panel_disable(panel_bridge->panel);
114 }
115 
116 static void panel_bridge_post_disable(struct drm_bridge *bridge)
117 {
118 	struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
119 
120 	drm_panel_unprepare(panel_bridge->panel);
121 }
122 
123 static const struct drm_bridge_funcs panel_bridge_bridge_funcs = {
124 	.attach = panel_bridge_attach,
125 	.detach = panel_bridge_detach,
126 	.pre_enable = panel_bridge_pre_enable,
127 	.enable = panel_bridge_enable,
128 	.disable = panel_bridge_disable,
129 	.post_disable = panel_bridge_post_disable,
130 };
131 
132 /**
133  * drm_panel_bridge_add - Creates a &drm_bridge and &drm_connector that
134  * just calls the appropriate functions from &drm_panel.
135  *
136  * @panel: The drm_panel being wrapped.  Must be non-NULL.
137  * @connector_type: The DRM_MODE_CONNECTOR_* for the connector to be
138  * created.
139  *
140  * For drivers converting from directly using drm_panel: The expected
141  * usage pattern is that during either encoder module probe or DSI
142  * host attach, a drm_panel will be looked up through
143  * drm_of_find_panel_or_bridge().  drm_panel_bridge_add() is used to
144  * wrap that panel in the new bridge, and the result can then be
145  * passed to drm_bridge_attach().  The drm_panel_prepare() and related
146  * functions can be dropped from the encoder driver (they're now
147  * called by the KMS helpers before calling into the encoder), along
148  * with connector creation.  When done with the bridge (after
149  * drm_mode_config_cleanup() if the bridge has already been attached), then
150  * drm_panel_bridge_remove() to free it.
151  *
152  * See devm_drm_panel_bridge_add() for an automatically manged version of this
153  * function.
154  */
155 struct drm_bridge *drm_panel_bridge_add(struct drm_panel *panel,
156 					u32 connector_type)
157 {
158 	struct panel_bridge *panel_bridge;
159 
160 	if (!panel)
161 		return ERR_PTR(-EINVAL);
162 
163 	panel_bridge = devm_kzalloc(panel->dev, sizeof(*panel_bridge),
164 				    GFP_KERNEL);
165 	if (!panel_bridge)
166 		return ERR_PTR(-ENOMEM);
167 
168 	panel_bridge->connector_type = connector_type;
169 	panel_bridge->panel = panel;
170 
171 	panel_bridge->bridge.funcs = &panel_bridge_bridge_funcs;
172 #ifdef CONFIG_OF
173 	panel_bridge->bridge.of_node = panel->dev->of_node;
174 #endif
175 
176 	drm_bridge_add(&panel_bridge->bridge);
177 
178 	return &panel_bridge->bridge;
179 }
180 EXPORT_SYMBOL(drm_panel_bridge_add);
181 
182 /**
183  * drm_panel_bridge_remove - Unregisters and frees a drm_bridge
184  * created by drm_panel_bridge_add().
185  *
186  * @bridge: The drm_bridge being freed.
187  */
188 void drm_panel_bridge_remove(struct drm_bridge *bridge)
189 {
190 	struct panel_bridge *panel_bridge;
191 
192 	if (!bridge)
193 		return;
194 
195 	if (bridge->funcs != &panel_bridge_bridge_funcs)
196 		return;
197 
198 	panel_bridge = drm_bridge_to_panel_bridge(bridge);
199 
200 	drm_bridge_remove(bridge);
201 	devm_kfree(panel_bridge->panel->dev, bridge);
202 }
203 EXPORT_SYMBOL(drm_panel_bridge_remove);
204 
205 static void devm_drm_panel_bridge_release(struct device *dev, void *res)
206 {
207 	struct drm_bridge **bridge = res;
208 
209 	drm_panel_bridge_remove(*bridge);
210 }
211 
212 /**
213  * devm_drm_panel_bridge_add - Creates a managed &drm_bridge and &drm_connector
214  * that just calls the appropriate functions from &drm_panel.
215  * @dev: device to tie the bridge lifetime to
216  * @panel: The drm_panel being wrapped.  Must be non-NULL.
217  * @connector_type: The DRM_MODE_CONNECTOR_* for the connector to be
218  * created.
219  *
220  * This is the managed version of drm_panel_bridge_add() which automatically
221  * calls drm_panel_bridge_remove() when @dev is unbound.
222  */
223 struct drm_bridge *devm_drm_panel_bridge_add(struct device *dev,
224 					     struct drm_panel *panel,
225 					     u32 connector_type)
226 {
227 	struct drm_bridge **ptr, *bridge;
228 
229 	ptr = devres_alloc(devm_drm_panel_bridge_release, sizeof(*ptr),
230 			   GFP_KERNEL);
231 	if (!ptr)
232 		return ERR_PTR(-ENOMEM);
233 
234 	bridge = drm_panel_bridge_add(panel, connector_type);
235 	if (!IS_ERR(bridge)) {
236 		*ptr = bridge;
237 		devres_add(dev, ptr);
238 	} else {
239 		devres_free(ptr);
240 	}
241 
242 	return bridge;
243 }
244 EXPORT_SYMBOL(devm_drm_panel_bridge_add);
245