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/media-bus-format.h>
7 #include <linux/of_graph.h>
9 #include <drm/drm_bridge.h>
10 #include <drm/drm_crtc.h>
11 #include <drm/drm_device.h>
12 #include <drm/drm_encoder.h>
13 #include <drm/drm_of.h>
14 #include <drm/drm_panel.h>
19 * A set of helper functions to aid DRM drivers in parsing standard DT
24 * drm_of_crtc_port_mask - find the mask of a registered CRTC by port OF node
28 * Given a port OF node, return the possible mask of the corresponding
29 * CRTC within a device's list of CRTCs. Returns zero if not found.
31 uint32_t drm_of_crtc_port_mask(struct drm_device *dev,
32 struct device_node *port)
34 unsigned int index = 0;
37 drm_for_each_crtc(tmp, dev) {
38 if (tmp->port == port)
46 EXPORT_SYMBOL(drm_of_crtc_port_mask);
49 * drm_of_find_possible_crtcs - find the possible CRTCs for an encoder port
51 * @port: encoder port to scan for endpoints
53 * Scan all endpoints attached to a port, locate their attached CRTCs,
54 * and generate the DRM mask of CRTCs which may be attached to this
57 * See Documentation/devicetree/bindings/graph.txt for the bindings.
59 uint32_t drm_of_find_possible_crtcs(struct drm_device *dev,
60 struct device_node *port)
62 struct device_node *remote_port, *ep;
63 uint32_t possible_crtcs = 0;
65 for_each_endpoint_of_node(port, ep) {
66 remote_port = of_graph_get_remote_port(ep);
72 possible_crtcs |= drm_of_crtc_port_mask(dev, remote_port);
74 of_node_put(remote_port);
77 return possible_crtcs;
79 EXPORT_SYMBOL(drm_of_find_possible_crtcs);
82 * drm_of_component_match_add - Add a component helper OF node match rule
83 * @master: master device
84 * @matchptr: component match pointer
85 * @compare: compare function used for matching component
88 void drm_of_component_match_add(struct device *master,
89 struct component_match **matchptr,
90 int (*compare)(struct device *, void *),
91 struct device_node *node)
94 component_match_add_release(master, matchptr, component_release_of,
97 EXPORT_SYMBOL_GPL(drm_of_component_match_add);
100 * drm_of_component_probe - Generic probe function for a component based master
101 * @dev: master device containing the OF node
102 * @compare_of: compare function used for matching components
103 * @m_ops: component master ops to be used
105 * Parse the platform device OF node and bind all the components associated
106 * with the master. Interface ports are added before the encoders in order to
107 * satisfy their .bind requirements
108 * See Documentation/devicetree/bindings/graph.txt for the bindings.
110 * Returns zero if successful, or one of the standard error codes if it fails.
112 int drm_of_component_probe(struct device *dev,
113 int (*compare_of)(struct device *, void *),
114 const struct component_master_ops *m_ops)
116 struct device_node *ep, *port, *remote;
117 struct component_match *match = NULL;
124 * Bind the crtc's ports first, so that drm_of_find_possible_crtcs()
125 * called from encoder's .bind callbacks works as expected
128 port = of_parse_phandle(dev->of_node, "ports", i);
132 if (of_device_is_available(port->parent))
133 drm_of_component_match_add(dev, &match, compare_of,
140 dev_err(dev, "missing 'ports' property\n");
145 dev_err(dev, "no available port\n");
150 * For bound crtcs, bind the encoders attached to their remote endpoint
153 port = of_parse_phandle(dev->of_node, "ports", i);
157 if (!of_device_is_available(port->parent)) {
162 for_each_child_of_node(port, ep) {
163 remote = of_graph_get_remote_port_parent(ep);
164 if (!remote || !of_device_is_available(remote)) {
167 } else if (!of_device_is_available(remote->parent)) {
168 dev_warn(dev, "parent device of %pOF is not available\n",
174 drm_of_component_match_add(dev, &match, compare_of,
181 return component_master_add_with_match(dev, m_ops, match);
183 EXPORT_SYMBOL(drm_of_component_probe);
186 * drm_of_encoder_active_endpoint - return the active encoder endpoint
187 * @node: device tree node containing encoder input ports
188 * @encoder: drm_encoder
190 * Given an encoder device node and a drm_encoder with a connected crtc,
191 * parse the encoder endpoint connecting to the crtc port.
193 int drm_of_encoder_active_endpoint(struct device_node *node,
194 struct drm_encoder *encoder,
195 struct of_endpoint *endpoint)
197 struct device_node *ep;
198 struct drm_crtc *crtc = encoder->crtc;
199 struct device_node *port;
205 for_each_endpoint_of_node(node, ep) {
206 port = of_graph_get_remote_port(ep);
208 if (port == crtc->port) {
209 ret = of_graph_parse_endpoint(ep, endpoint);
217 EXPORT_SYMBOL_GPL(drm_of_encoder_active_endpoint);
220 * drm_of_find_panel_or_bridge - return connected panel or bridge device
221 * @np: device tree node containing encoder output ports
222 * @port: port in the device tree node
223 * @endpoint: endpoint in the device tree node
224 * @panel: pointer to hold returned drm_panel
225 * @bridge: pointer to hold returned drm_bridge
227 * Given a DT node's port and endpoint number, find the connected node and
228 * return either the associated struct drm_panel or drm_bridge device. Either
229 * @panel or @bridge must not be NULL.
231 * This function is deprecated and should not be used in new drivers. Use
232 * devm_drm_of_get_bridge() instead.
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)
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
255 if (!of_graph_is_present(np))
258 remote = of_graph_get_remote_node(np, port, endpoint);
263 *panel = of_drm_find_panel(remote);
270 /* No panel found yet, check for a bridge next. */
273 *bridge = of_drm_find_bridge(remote);
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)
295 of_property_read_bool(port_node, "dual-lvds-even-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;
313 if (!of_node_name_eq(endpoint, "endpoint"))
316 remote_port = of_graph_get_remote_port(endpoint);
318 of_node_put(endpoint);
322 current_pt = drm_of_lvds_get_port_pixels_type(remote_port);
323 of_node_put(remote_port);
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 of_node_put(endpoint);
344 * drm_of_lvds_get_dual_link_pixel_order - Get LVDS dual-link pixel order
345 * @port1: First DT port node of the Dual-link LVDS source
346 * @port2: Second DT port node of the Dual-link LVDS source
348 * An LVDS dual-link connection is made of two links, with even pixels
349 * transitting on one link, and odd pixels on the other link. This function
350 * returns, for two ports of an LVDS dual-link source, which port shall transmit
351 * the even and odd pixels, based on the requirements of the connected sink.
353 * The pixel order is determined from the dual-lvds-even-pixels and
354 * dual-lvds-odd-pixels properties in the sink's DT port nodes. If those
355 * properties are not present, or if their usage is not valid, this function
358 * If either port is not connected, this function returns -EPIPE.
360 * @port1 and @port2 are typically DT sibling nodes, but may have different
361 * parents when, for instance, two separate LVDS encoders carry the even and odd
365 * * DRM_LVDS_DUAL_LINK_EVEN_ODD_PIXELS - @port1 carries even pixels and @port2
367 * * DRM_LVDS_DUAL_LINK_ODD_EVEN_PIXELS - @port1 carries odd pixels and @port2
368 * carries even pixels
369 * * -EINVAL - @port1 and @port2 are not connected to a dual-link LVDS sink, or
370 * the sink configuration is invalid
371 * * -EPIPE - when @port1 or @port2 are not connected
373 int drm_of_lvds_get_dual_link_pixel_order(const struct device_node *port1,
374 const struct device_node *port2)
376 int remote_p1_pt, remote_p2_pt;
378 if (!port1 || !port2)
381 remote_p1_pt = drm_of_lvds_get_remote_pixels_type(port1);
382 if (remote_p1_pt < 0)
385 remote_p2_pt = drm_of_lvds_get_remote_pixels_type(port2);
386 if (remote_p2_pt < 0)
390 * A valid dual-lVDS bus is found when one remote port is marked with
391 * "dual-lvds-even-pixels", and the other remote port is marked with
392 * "dual-lvds-odd-pixels", bail out if the markers are not right.
394 if (remote_p1_pt + remote_p2_pt != DRM_OF_LVDS_EVEN + DRM_OF_LVDS_ODD)
397 return remote_p1_pt == DRM_OF_LVDS_EVEN ?
398 DRM_LVDS_DUAL_LINK_EVEN_ODD_PIXELS :
399 DRM_LVDS_DUAL_LINK_ODD_EVEN_PIXELS;
401 EXPORT_SYMBOL_GPL(drm_of_lvds_get_dual_link_pixel_order);
404 * drm_of_lvds_get_data_mapping - Get LVDS data mapping
405 * @port: DT port node of the LVDS source or sink
407 * Convert DT "data-mapping" property string value into media bus format value.
410 * * MEDIA_BUS_FMT_RGB666_1X7X3_SPWG - data-mapping is "jeida-18"
411 * * MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA - data-mapping is "jeida-24"
412 * * MEDIA_BUS_FMT_RGB888_1X7X4_SPWG - data-mapping is "vesa-24"
413 * * -EINVAL - the "data-mapping" property is unsupported
414 * * -ENODEV - the "data-mapping" property is missing
416 int drm_of_lvds_get_data_mapping(const struct device_node *port)
421 ret = of_property_read_string(port, "data-mapping", &mapping);
425 if (!strcmp(mapping, "jeida-18"))
426 return MEDIA_BUS_FMT_RGB666_1X7X3_SPWG;
427 if (!strcmp(mapping, "jeida-24"))
428 return MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA;
429 if (!strcmp(mapping, "vesa-24"))
430 return MEDIA_BUS_FMT_RGB888_1X7X4_SPWG;
434 EXPORT_SYMBOL_GPL(drm_of_lvds_get_data_mapping);
437 * drm_of_get_data_lanes_count - Get DSI/(e)DP data lane count
438 * @endpoint: DT endpoint node of the DSI/(e)DP source or sink
439 * @min: minimum supported number of data lanes
440 * @max: maximum supported number of data lanes
442 * Count DT "data-lanes" property elements and check for validity.
445 * * min..max - positive integer count of "data-lanes" elements
446 * * -ve - the "data-lanes" property is missing or invalid
447 * * -EINVAL - the "data-lanes" property is unsupported
449 int drm_of_get_data_lanes_count(const struct device_node *endpoint,
450 const unsigned int min, const unsigned int max)
454 ret = of_property_count_u32_elems(endpoint, "data-lanes");
458 if (ret < min || ret > max)
463 EXPORT_SYMBOL_GPL(drm_of_get_data_lanes_count);
466 * drm_of_get_data_lanes_count_ep - Get DSI/(e)DP data lane count by endpoint
467 * @port: DT port node of the DSI/(e)DP source or sink
468 * @port_reg: identifier (value of reg property) of the parent port node
469 * @reg: identifier (value of reg property) of the endpoint node
470 * @min: minimum supported number of data lanes
471 * @max: maximum supported number of data lanes
473 * Count DT "data-lanes" property elements and check for validity.
474 * This variant uses endpoint specifier.
477 * * min..max - positive integer count of "data-lanes" elements
478 * * -EINVAL - the "data-mapping" property is unsupported
479 * * -ENODEV - the "data-mapping" property is missing
481 int drm_of_get_data_lanes_count_ep(const struct device_node *port,
482 int port_reg, int reg,
483 const unsigned int min,
484 const unsigned int max)
486 struct device_node *endpoint;
489 endpoint = of_graph_get_endpoint_by_regs(port, port_reg, reg);
490 ret = drm_of_get_data_lanes_count(endpoint, min, max);
491 of_node_put(endpoint);
495 EXPORT_SYMBOL_GPL(drm_of_get_data_lanes_count_ep);