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_mipi_dsi.h>
14 #include <drm/drm_of.h>
15 #include <drm/drm_panel.h>
20 * A set of helper functions to aid DRM drivers in parsing standard DT
25 * drm_of_crtc_port_mask - find the mask of a registered CRTC by port OF node
29 * Given a port OF node, return the possible mask of the corresponding
30 * CRTC within a device's list of CRTCs. Returns zero if not found.
32 uint32_t drm_of_crtc_port_mask(struct drm_device *dev,
33 struct device_node *port)
35 unsigned int index = 0;
38 drm_for_each_crtc(tmp, dev) {
39 if (tmp->port == port)
47 EXPORT_SYMBOL(drm_of_crtc_port_mask);
50 * drm_of_find_possible_crtcs - find the possible CRTCs for an encoder port
52 * @port: encoder port to scan for endpoints
54 * Scan all endpoints attached to a port, locate their attached CRTCs,
55 * and generate the DRM mask of CRTCs which may be attached to this
58 * See Documentation/devicetree/bindings/graph.txt for the bindings.
60 uint32_t drm_of_find_possible_crtcs(struct drm_device *dev,
61 struct device_node *port)
63 struct device_node *remote_port, *ep;
64 uint32_t possible_crtcs = 0;
66 for_each_endpoint_of_node(port, ep) {
67 remote_port = of_graph_get_remote_port(ep);
73 possible_crtcs |= drm_of_crtc_port_mask(dev, remote_port);
75 of_node_put(remote_port);
78 return possible_crtcs;
80 EXPORT_SYMBOL(drm_of_find_possible_crtcs);
83 * drm_of_component_match_add - Add a component helper OF node match rule
84 * @master: master device
85 * @matchptr: component match pointer
86 * @compare: compare function used for matching component
89 void drm_of_component_match_add(struct device *master,
90 struct component_match **matchptr,
91 int (*compare)(struct device *, void *),
92 struct device_node *node)
95 component_match_add_release(master, matchptr, component_release_of,
98 EXPORT_SYMBOL_GPL(drm_of_component_match_add);
101 * drm_of_component_probe - Generic probe function for a component based master
102 * @dev: master device containing the OF node
103 * @compare_of: compare function used for matching components
104 * @m_ops: component master ops to be used
106 * Parse the platform device OF node and bind all the components associated
107 * with the master. Interface ports are added before the encoders in order to
108 * satisfy their .bind requirements
109 * See Documentation/devicetree/bindings/graph.txt for the bindings.
111 * Returns zero if successful, or one of the standard error codes if it fails.
113 int drm_of_component_probe(struct device *dev,
114 int (*compare_of)(struct device *, void *),
115 const struct component_master_ops *m_ops)
117 struct device_node *ep, *port, *remote;
118 struct component_match *match = NULL;
125 * Bind the crtc's ports first, so that drm_of_find_possible_crtcs()
126 * called from encoder's .bind callbacks works as expected
129 port = of_parse_phandle(dev->of_node, "ports", i);
133 if (of_device_is_available(port->parent))
134 drm_of_component_match_add(dev, &match, compare_of,
141 dev_err(dev, "missing 'ports' property\n");
146 dev_err(dev, "no available port\n");
151 * For bound crtcs, bind the encoders attached to their remote endpoint
154 port = of_parse_phandle(dev->of_node, "ports", i);
158 if (!of_device_is_available(port->parent)) {
163 for_each_child_of_node(port, ep) {
164 remote = of_graph_get_remote_port_parent(ep);
165 if (!remote || !of_device_is_available(remote)) {
168 } else if (!of_device_is_available(remote->parent)) {
169 dev_warn(dev, "parent device of %pOF is not available\n",
175 drm_of_component_match_add(dev, &match, compare_of,
182 return component_master_add_with_match(dev, m_ops, match);
184 EXPORT_SYMBOL(drm_of_component_probe);
187 * drm_of_encoder_active_endpoint - return the active encoder endpoint
188 * @node: device tree node containing encoder input ports
189 * @encoder: drm_encoder
191 * Given an encoder device node and a drm_encoder with a connected crtc,
192 * parse the encoder endpoint connecting to the crtc port.
194 int drm_of_encoder_active_endpoint(struct device_node *node,
195 struct drm_encoder *encoder,
196 struct of_endpoint *endpoint)
198 struct device_node *ep;
199 struct drm_crtc *crtc = encoder->crtc;
200 struct device_node *port;
206 for_each_endpoint_of_node(node, ep) {
207 port = of_graph_get_remote_port(ep);
209 if (port == crtc->port) {
210 ret = of_graph_parse_endpoint(ep, endpoint);
218 EXPORT_SYMBOL_GPL(drm_of_encoder_active_endpoint);
221 * drm_of_find_panel_or_bridge - return connected panel or bridge device
222 * @np: device tree node containing encoder output ports
223 * @port: port in the device tree node
224 * @endpoint: endpoint in the device tree node
225 * @panel: pointer to hold returned drm_panel
226 * @bridge: pointer to hold returned drm_bridge
228 * Given a DT node's port and endpoint number, find the connected node and
229 * return either the associated struct drm_panel or drm_bridge device. Either
230 * @panel or @bridge must not be NULL.
232 * This function is deprecated and should not be used in new drivers. Use
233 * devm_drm_of_get_bridge() instead.
235 * Returns zero if successful, or one of the standard error codes if it fails.
237 int drm_of_find_panel_or_bridge(const struct device_node *np,
238 int port, int endpoint,
239 struct drm_panel **panel,
240 struct drm_bridge **bridge)
242 int ret = -EPROBE_DEFER;
243 struct device_node *remote;
245 if (!panel && !bridge)
251 * of_graph_get_remote_node() produces a noisy error message if port
252 * node isn't found and the absence of the port is a legit case here,
253 * so at first we silently check whether graph presents in the
256 if (!of_graph_is_present(np))
259 remote = of_graph_get_remote_node(np, port, endpoint);
264 *panel = of_drm_find_panel(remote);
271 /* No panel found yet, check for a bridge next. */
274 *bridge = of_drm_find_bridge(remote);
286 EXPORT_SYMBOL_GPL(drm_of_find_panel_or_bridge);
288 enum drm_of_lvds_pixels {
289 DRM_OF_LVDS_EVEN = BIT(0),
290 DRM_OF_LVDS_ODD = BIT(1),
293 static int drm_of_lvds_get_port_pixels_type(struct device_node *port_node)
296 of_property_read_bool(port_node, "dual-lvds-even-pixels");
298 of_property_read_bool(port_node, "dual-lvds-odd-pixels");
300 return (even_pixels ? DRM_OF_LVDS_EVEN : 0) |
301 (odd_pixels ? DRM_OF_LVDS_ODD : 0);
304 static int drm_of_lvds_get_remote_pixels_type(
305 const struct device_node *port_node)
307 struct device_node *endpoint = NULL;
308 int pixels_type = -EPIPE;
310 for_each_child_of_node(port_node, endpoint) {
311 struct device_node *remote_port;
314 if (!of_node_name_eq(endpoint, "endpoint"))
317 remote_port = of_graph_get_remote_port(endpoint);
319 of_node_put(endpoint);
323 current_pt = drm_of_lvds_get_port_pixels_type(remote_port);
324 of_node_put(remote_port);
326 pixels_type = current_pt;
329 * Sanity check, ensure that all remote endpoints have the same
330 * pixel type. We may lift this restriction later if we need to
331 * support multiple sinks with different dual-link
332 * configurations by passing the endpoints explicitly to
333 * drm_of_lvds_get_dual_link_pixel_order().
335 if (!current_pt || pixels_type != current_pt) {
336 of_node_put(endpoint);
345 * drm_of_lvds_get_dual_link_pixel_order - Get LVDS dual-link pixel order
346 * @port1: First DT port node of the Dual-link LVDS source
347 * @port2: Second DT port node of the Dual-link LVDS source
349 * An LVDS dual-link connection is made of two links, with even pixels
350 * transitting on one link, and odd pixels on the other link. This function
351 * returns, for two ports of an LVDS dual-link source, which port shall transmit
352 * the even and odd pixels, based on the requirements of the connected sink.
354 * The pixel order is determined from the dual-lvds-even-pixels and
355 * dual-lvds-odd-pixels properties in the sink's DT port nodes. If those
356 * properties are not present, or if their usage is not valid, this function
359 * If either port is not connected, this function returns -EPIPE.
361 * @port1 and @port2 are typically DT sibling nodes, but may have different
362 * parents when, for instance, two separate LVDS encoders carry the even and odd
366 * * DRM_LVDS_DUAL_LINK_EVEN_ODD_PIXELS - @port1 carries even pixels and @port2
368 * * DRM_LVDS_DUAL_LINK_ODD_EVEN_PIXELS - @port1 carries odd pixels and @port2
369 * carries even pixels
370 * * -EINVAL - @port1 and @port2 are not connected to a dual-link LVDS sink, or
371 * the sink configuration is invalid
372 * * -EPIPE - when @port1 or @port2 are not connected
374 int drm_of_lvds_get_dual_link_pixel_order(const struct device_node *port1,
375 const struct device_node *port2)
377 int remote_p1_pt, remote_p2_pt;
379 if (!port1 || !port2)
382 remote_p1_pt = drm_of_lvds_get_remote_pixels_type(port1);
383 if (remote_p1_pt < 0)
386 remote_p2_pt = drm_of_lvds_get_remote_pixels_type(port2);
387 if (remote_p2_pt < 0)
391 * A valid dual-lVDS bus is found when one remote port is marked with
392 * "dual-lvds-even-pixels", and the other remote port is marked with
393 * "dual-lvds-odd-pixels", bail out if the markers are not right.
395 if (remote_p1_pt + remote_p2_pt != DRM_OF_LVDS_EVEN + DRM_OF_LVDS_ODD)
398 return remote_p1_pt == DRM_OF_LVDS_EVEN ?
399 DRM_LVDS_DUAL_LINK_EVEN_ODD_PIXELS :
400 DRM_LVDS_DUAL_LINK_ODD_EVEN_PIXELS;
402 EXPORT_SYMBOL_GPL(drm_of_lvds_get_dual_link_pixel_order);
405 * drm_of_lvds_get_data_mapping - Get LVDS data mapping
406 * @port: DT port node of the LVDS source or sink
408 * Convert DT "data-mapping" property string value into media bus format value.
411 * * MEDIA_BUS_FMT_RGB666_1X7X3_SPWG - data-mapping is "jeida-18"
412 * * MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA - data-mapping is "jeida-24"
413 * * MEDIA_BUS_FMT_RGB888_1X7X4_SPWG - data-mapping is "vesa-24"
414 * * -EINVAL - the "data-mapping" property is unsupported
415 * * -ENODEV - the "data-mapping" property is missing
417 int drm_of_lvds_get_data_mapping(const struct device_node *port)
422 ret = of_property_read_string(port, "data-mapping", &mapping);
426 if (!strcmp(mapping, "jeida-18"))
427 return MEDIA_BUS_FMT_RGB666_1X7X3_SPWG;
428 if (!strcmp(mapping, "jeida-24"))
429 return MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA;
430 if (!strcmp(mapping, "vesa-24"))
431 return MEDIA_BUS_FMT_RGB888_1X7X4_SPWG;
435 EXPORT_SYMBOL_GPL(drm_of_lvds_get_data_mapping);
438 * drm_of_get_data_lanes_count - Get DSI/(e)DP data lane count
439 * @endpoint: DT endpoint node of the DSI/(e)DP source or sink
440 * @min: minimum supported number of data lanes
441 * @max: maximum supported number of data lanes
443 * Count DT "data-lanes" property elements and check for validity.
446 * * min..max - positive integer count of "data-lanes" elements
447 * * -ve - the "data-lanes" property is missing or invalid
448 * * -EINVAL - the "data-lanes" property is unsupported
450 int drm_of_get_data_lanes_count(const struct device_node *endpoint,
451 const unsigned int min, const unsigned int max)
455 ret = of_property_count_u32_elems(endpoint, "data-lanes");
459 if (ret < min || ret > max)
464 EXPORT_SYMBOL_GPL(drm_of_get_data_lanes_count);
467 * drm_of_get_data_lanes_count_ep - Get DSI/(e)DP data lane count by endpoint
468 * @port: DT port node of the DSI/(e)DP source or sink
469 * @port_reg: identifier (value of reg property) of the parent port node
470 * @reg: identifier (value of reg property) of the endpoint node
471 * @min: minimum supported number of data lanes
472 * @max: maximum supported number of data lanes
474 * Count DT "data-lanes" property elements and check for validity.
475 * This variant uses endpoint specifier.
478 * * min..max - positive integer count of "data-lanes" elements
479 * * -EINVAL - the "data-mapping" property is unsupported
480 * * -ENODEV - the "data-mapping" property is missing
482 int drm_of_get_data_lanes_count_ep(const struct device_node *port,
483 int port_reg, int reg,
484 const unsigned int min,
485 const unsigned int max)
487 struct device_node *endpoint;
490 endpoint = of_graph_get_endpoint_by_regs(port, port_reg, reg);
491 ret = drm_of_get_data_lanes_count(endpoint, min, max);
492 of_node_put(endpoint);
496 EXPORT_SYMBOL_GPL(drm_of_get_data_lanes_count_ep);
498 #if IS_ENABLED(CONFIG_DRM_MIPI_DSI)
501 * drm_of_get_dsi_bus - find the DSI bus for a given device
502 * @dev: parent device of display (SPI, I2C)
504 * Gets parent DSI bus for a DSI device controlled through a bus other
505 * than MIPI-DCS (SPI, I2C, etc.) using the Device Tree.
507 * Returns pointer to mipi_dsi_host if successful, -EINVAL if the
508 * request is unsupported, -EPROBE_DEFER if the DSI host is found but
509 * not available, or -ENODEV otherwise.
511 struct mipi_dsi_host *drm_of_get_dsi_bus(struct device *dev)
513 struct mipi_dsi_host *dsi_host;
514 struct device_node *endpoint, *dsi_host_node;
517 * Get first endpoint child from device.
519 endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
521 return ERR_PTR(-ENODEV);
524 * Follow the first endpoint to get the DSI host node and then
525 * release the endpoint since we no longer need it.
527 dsi_host_node = of_graph_get_remote_port_parent(endpoint);
528 of_node_put(endpoint);
530 return ERR_PTR(-ENODEV);
533 * Get the DSI host from the DSI host node. If we get an error
534 * or the return is null assume we're not ready to probe just
535 * yet. Release the DSI host node since we're done with it.
537 dsi_host = of_find_mipi_dsi_host_by_node(dsi_host_node);
538 of_node_put(dsi_host_node);
539 if (IS_ERR_OR_NULL(dsi_host))
540 return ERR_PTR(-EPROBE_DEFER);
544 EXPORT_SYMBOL_GPL(drm_of_get_dsi_bus);
546 #endif /* CONFIG_DRM_MIPI_DSI */