Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / drm / drm_of.c
blobca04c34e8251863d19324e4eb063616ba18034c1
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/component.h>
3 #include <linux/export.h>
4 #include <linux/list.h>
5 #include <linux/of_graph.h>
7 #include <drm/drm_bridge.h>
8 #include <drm/drm_crtc.h>
9 #include <drm/drm_device.h>
10 #include <drm/drm_encoder.h>
11 #include <drm/drm_of.h>
12 #include <drm/drm_panel.h>
14 /**
15 * DOC: overview
17 * A set of helper functions to aid DRM drivers in parsing standard DT
18 * properties.
21 static void drm_release_of(struct device *dev, void *data)
23 of_node_put(data);
26 /**
27 * drm_of_crtc_port_mask - find the mask of a registered CRTC by port OF node
28 * @dev: DRM device
29 * @port: port OF node
31 * Given a port OF node, return the possible mask of the corresponding
32 * CRTC within a device's list of CRTCs. Returns zero if not found.
34 uint32_t drm_of_crtc_port_mask(struct drm_device *dev,
35 struct device_node *port)
37 unsigned int index = 0;
38 struct drm_crtc *tmp;
40 drm_for_each_crtc(tmp, dev) {
41 if (tmp->port == port)
42 return 1 << index;
44 index++;
47 return 0;
49 EXPORT_SYMBOL(drm_of_crtc_port_mask);
51 /**
52 * drm_of_find_possible_crtcs - find the possible CRTCs for an encoder port
53 * @dev: DRM device
54 * @port: encoder port to scan for endpoints
56 * Scan all endpoints attached to a port, locate their attached CRTCs,
57 * and generate the DRM mask of CRTCs which may be attached to this
58 * encoder.
60 * See Documentation/devicetree/bindings/graph.txt for the bindings.
62 uint32_t drm_of_find_possible_crtcs(struct drm_device *dev,
63 struct device_node *port)
65 struct device_node *remote_port, *ep;
66 uint32_t possible_crtcs = 0;
68 for_each_endpoint_of_node(port, ep) {
69 remote_port = of_graph_get_remote_port(ep);
70 if (!remote_port) {
71 of_node_put(ep);
72 return 0;
75 possible_crtcs |= drm_of_crtc_port_mask(dev, remote_port);
77 of_node_put(remote_port);
80 return possible_crtcs;
82 EXPORT_SYMBOL(drm_of_find_possible_crtcs);
84 /**
85 * drm_of_component_match_add - Add a component helper OF node match rule
86 * @master: master device
87 * @matchptr: component match pointer
88 * @compare: compare function used for matching component
89 * @node: of_node
91 void drm_of_component_match_add(struct device *master,
92 struct component_match **matchptr,
93 int (*compare)(struct device *, void *),
94 struct device_node *node)
96 of_node_get(node);
97 component_match_add_release(master, matchptr, drm_release_of,
98 compare, node);
100 EXPORT_SYMBOL_GPL(drm_of_component_match_add);
103 * drm_of_component_probe - Generic probe function for a component based master
104 * @dev: master device containing the OF node
105 * @compare_of: compare function used for matching components
106 * @m_ops: component master ops to be used
108 * Parse the platform device OF node and bind all the components associated
109 * with the master. Interface ports are added before the encoders in order to
110 * satisfy their .bind requirements
111 * See Documentation/devicetree/bindings/graph.txt for the bindings.
113 * Returns zero if successful, or one of the standard error codes if it fails.
115 int drm_of_component_probe(struct device *dev,
116 int (*compare_of)(struct device *, void *),
117 const struct component_master_ops *m_ops)
119 struct device_node *ep, *port, *remote;
120 struct component_match *match = NULL;
121 int i;
123 if (!dev->of_node)
124 return -EINVAL;
127 * Bind the crtc's ports first, so that drm_of_find_possible_crtcs()
128 * called from encoder's .bind callbacks works as expected
130 for (i = 0; ; i++) {
131 port = of_parse_phandle(dev->of_node, "ports", i);
132 if (!port)
133 break;
135 if (of_device_is_available(port->parent))
136 drm_of_component_match_add(dev, &match, compare_of,
137 port);
139 of_node_put(port);
142 if (i == 0) {
143 dev_err(dev, "missing 'ports' property\n");
144 return -ENODEV;
147 if (!match) {
148 dev_err(dev, "no available port\n");
149 return -ENODEV;
153 * For bound crtcs, bind the encoders attached to their remote endpoint
155 for (i = 0; ; i++) {
156 port = of_parse_phandle(dev->of_node, "ports", i);
157 if (!port)
158 break;
160 if (!of_device_is_available(port->parent)) {
161 of_node_put(port);
162 continue;
165 for_each_child_of_node(port, ep) {
166 remote = of_graph_get_remote_port_parent(ep);
167 if (!remote || !of_device_is_available(remote)) {
168 of_node_put(remote);
169 continue;
170 } else if (!of_device_is_available(remote->parent)) {
171 dev_warn(dev, "parent device of %pOF is not available\n",
172 remote);
173 of_node_put(remote);
174 continue;
177 drm_of_component_match_add(dev, &match, compare_of,
178 remote);
179 of_node_put(remote);
181 of_node_put(port);
184 return component_master_add_with_match(dev, m_ops, match);
186 EXPORT_SYMBOL(drm_of_component_probe);
189 * drm_of_encoder_active_endpoint - return the active encoder endpoint
190 * @node: device tree node containing encoder input ports
191 * @encoder: drm_encoder
193 * Given an encoder device node and a drm_encoder with a connected crtc,
194 * parse the encoder endpoint connecting to the crtc port.
196 int drm_of_encoder_active_endpoint(struct device_node *node,
197 struct drm_encoder *encoder,
198 struct of_endpoint *endpoint)
200 struct device_node *ep;
201 struct drm_crtc *crtc = encoder->crtc;
202 struct device_node *port;
203 int ret;
205 if (!node || !crtc)
206 return -EINVAL;
208 for_each_endpoint_of_node(node, ep) {
209 port = of_graph_get_remote_port(ep);
210 of_node_put(port);
211 if (port == crtc->port) {
212 ret = of_graph_parse_endpoint(ep, endpoint);
213 of_node_put(ep);
214 return ret;
218 return -EINVAL;
220 EXPORT_SYMBOL_GPL(drm_of_encoder_active_endpoint);
223 * drm_of_find_panel_or_bridge - return connected panel or bridge device
224 * @np: device tree node containing encoder output ports
225 * @port: port in the device tree node
226 * @endpoint: endpoint in the device tree node
227 * @panel: pointer to hold returned drm_panel
228 * @bridge: pointer to hold returned drm_bridge
230 * Given a DT node's port and endpoint number, find the connected node and
231 * return either the associated struct drm_panel or drm_bridge device. Either
232 * @panel or @bridge must not be NULL.
234 * Returns zero if successful, or one of the standard error codes if it fails.
236 int drm_of_find_panel_or_bridge(const struct device_node *np,
237 int port, int endpoint,
238 struct drm_panel **panel,
239 struct drm_bridge **bridge)
241 int ret = -EPROBE_DEFER;
242 struct device_node *remote;
244 if (!panel && !bridge)
245 return -EINVAL;
246 if (panel)
247 *panel = NULL;
250 * of_graph_get_remote_node() produces a noisy error message if port
251 * node isn't found and the absence of the port is a legit case here,
252 * so at first we silently check whether graph presents in the
253 * device-tree node.
255 if (!of_graph_is_present(np))
256 return -ENODEV;
258 remote = of_graph_get_remote_node(np, port, endpoint);
259 if (!remote)
260 return -ENODEV;
262 if (panel) {
263 *panel = of_drm_find_panel(remote);
264 if (!IS_ERR(*panel))
265 ret = 0;
266 else
267 *panel = NULL;
270 /* No panel found yet, check for a bridge next. */
271 if (bridge) {
272 if (ret) {
273 *bridge = of_drm_find_bridge(remote);
274 if (*bridge)
275 ret = 0;
276 } else {
277 *bridge = NULL;
282 of_node_put(remote);
283 return ret;
285 EXPORT_SYMBOL_GPL(drm_of_find_panel_or_bridge);
287 enum drm_of_lvds_pixels {
288 DRM_OF_LVDS_EVEN = BIT(0),
289 DRM_OF_LVDS_ODD = BIT(1),
292 static int drm_of_lvds_get_port_pixels_type(struct device_node *port_node)
294 bool even_pixels =
295 of_property_read_bool(port_node, "dual-lvds-even-pixels");
296 bool odd_pixels =
297 of_property_read_bool(port_node, "dual-lvds-odd-pixels");
299 return (even_pixels ? DRM_OF_LVDS_EVEN : 0) |
300 (odd_pixels ? DRM_OF_LVDS_ODD : 0);
303 static int drm_of_lvds_get_remote_pixels_type(
304 const struct device_node *port_node)
306 struct device_node *endpoint = NULL;
307 int pixels_type = -EPIPE;
309 for_each_child_of_node(port_node, endpoint) {
310 struct device_node *remote_port;
311 int current_pt;
313 if (!of_node_name_eq(endpoint, "endpoint"))
314 continue;
316 remote_port = of_graph_get_remote_port(endpoint);
317 if (!remote_port) {
318 of_node_put(remote_port);
319 return -EPIPE;
322 current_pt = drm_of_lvds_get_port_pixels_type(remote_port);
323 of_node_put(remote_port);
324 if (pixels_type < 0)
325 pixels_type = current_pt;
328 * Sanity check, ensure that all remote endpoints have the same
329 * pixel type. We may lift this restriction later if we need to
330 * support multiple sinks with different dual-link
331 * configurations by passing the endpoints explicitly to
332 * drm_of_lvds_get_dual_link_pixel_order().
334 if (!current_pt || pixels_type != current_pt)
335 return -EINVAL;
338 return pixels_type;
342 * drm_of_lvds_get_dual_link_pixel_order - Get LVDS dual-link pixel order
343 * @port1: First DT port node of the Dual-link LVDS source
344 * @port2: Second DT port node of the Dual-link LVDS source
346 * An LVDS dual-link connection is made of two links, with even pixels
347 * transitting on one link, and odd pixels on the other link. This function
348 * returns, for two ports of an LVDS dual-link source, which port shall transmit
349 * the even and odd pixels, based on the requirements of the connected sink.
351 * The pixel order is determined from the dual-lvds-even-pixels and
352 * dual-lvds-odd-pixels properties in the sink's DT port nodes. If those
353 * properties are not present, or if their usage is not valid, this function
354 * returns -EINVAL.
356 * If either port is not connected, this function returns -EPIPE.
358 * @port1 and @port2 are typically DT sibling nodes, but may have different
359 * parents when, for instance, two separate LVDS encoders carry the even and odd
360 * pixels.
362 * Return:
363 * * DRM_LVDS_DUAL_LINK_EVEN_ODD_PIXELS - @port1 carries even pixels and @port2
364 * carries odd pixels
365 * * DRM_LVDS_DUAL_LINK_ODD_EVEN_PIXELS - @port1 carries odd pixels and @port2
366 * carries even pixels
367 * * -EINVAL - @port1 and @port2 are not connected to a dual-link LVDS sink, or
368 * the sink configuration is invalid
369 * * -EPIPE - when @port1 or @port2 are not connected
371 int drm_of_lvds_get_dual_link_pixel_order(const struct device_node *port1,
372 const struct device_node *port2)
374 int remote_p1_pt, remote_p2_pt;
376 if (!port1 || !port2)
377 return -EINVAL;
379 remote_p1_pt = drm_of_lvds_get_remote_pixels_type(port1);
380 if (remote_p1_pt < 0)
381 return remote_p1_pt;
383 remote_p2_pt = drm_of_lvds_get_remote_pixels_type(port2);
384 if (remote_p2_pt < 0)
385 return remote_p2_pt;
388 * A valid dual-lVDS bus is found when one remote port is marked with
389 * "dual-lvds-even-pixels", and the other remote port is marked with
390 * "dual-lvds-odd-pixels", bail out if the markers are not right.
392 if (remote_p1_pt + remote_p2_pt != DRM_OF_LVDS_EVEN + DRM_OF_LVDS_ODD)
393 return -EINVAL;
395 return remote_p1_pt == DRM_OF_LVDS_EVEN ?
396 DRM_LVDS_DUAL_LINK_EVEN_ODD_PIXELS :
397 DRM_LVDS_DUAL_LINK_ODD_EVEN_PIXELS;
399 EXPORT_SYMBOL_GPL(drm_of_lvds_get_dual_link_pixel_order);