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>
17 * A set of helper functions to aid DRM drivers in parsing standard DT
22 * drm_of_crtc_port_mask - find the mask of a registered CRTC by port OF node
26 * Given a port OF node, return the possible mask of the corresponding
27 * CRTC within a device's list of CRTCs. Returns zero if not found.
29 uint32_t drm_of_crtc_port_mask(struct drm_device *dev,
30 struct device_node *port)
32 unsigned int index = 0;
35 drm_for_each_crtc(tmp, dev) {
36 if (tmp->port == port)
44 EXPORT_SYMBOL(drm_of_crtc_port_mask);
47 * drm_of_find_possible_crtcs - find the possible CRTCs for an encoder port
49 * @port: encoder port to scan for endpoints
51 * Scan all endpoints attached to a port, locate their attached CRTCs,
52 * and generate the DRM mask of CRTCs which may be attached to this
55 * See Documentation/devicetree/bindings/graph.txt for the bindings.
57 uint32_t drm_of_find_possible_crtcs(struct drm_device *dev,
58 struct device_node *port)
60 struct device_node *remote_port, *ep;
61 uint32_t possible_crtcs = 0;
63 for_each_endpoint_of_node(port, ep) {
64 remote_port = of_graph_get_remote_port(ep);
70 possible_crtcs |= drm_of_crtc_port_mask(dev, remote_port);
72 of_node_put(remote_port);
75 return possible_crtcs;
77 EXPORT_SYMBOL(drm_of_find_possible_crtcs);
80 * drm_of_component_match_add - Add a component helper OF node match rule
81 * @master: master device
82 * @matchptr: component match pointer
83 * @compare: compare function used for matching component
86 void drm_of_component_match_add(struct device *master,
87 struct component_match **matchptr,
88 int (*compare)(struct device *, void *),
89 struct device_node *node)
92 component_match_add_release(master, matchptr, component_release_of,
95 EXPORT_SYMBOL_GPL(drm_of_component_match_add);
98 * drm_of_component_probe - Generic probe function for a component based master
99 * @dev: master device containing the OF node
100 * @compare_of: compare function used for matching components
101 * @m_ops: component master ops to be used
103 * Parse the platform device OF node and bind all the components associated
104 * with the master. Interface ports are added before the encoders in order to
105 * satisfy their .bind requirements
106 * See Documentation/devicetree/bindings/graph.txt for the bindings.
108 * Returns zero if successful, or one of the standard error codes if it fails.
110 int drm_of_component_probe(struct device *dev,
111 int (*compare_of)(struct device *, void *),
112 const struct component_master_ops *m_ops)
114 struct device_node *ep, *port, *remote;
115 struct component_match *match = NULL;
122 * Bind the crtc's ports first, so that drm_of_find_possible_crtcs()
123 * called from encoder's .bind callbacks works as expected
126 port = of_parse_phandle(dev->of_node, "ports", i);
130 if (of_device_is_available(port->parent))
131 drm_of_component_match_add(dev, &match, compare_of,
138 dev_err(dev, "missing 'ports' property\n");
143 dev_err(dev, "no available port\n");
148 * For bound crtcs, bind the encoders attached to their remote endpoint
151 port = of_parse_phandle(dev->of_node, "ports", i);
155 if (!of_device_is_available(port->parent)) {
160 for_each_child_of_node(port, ep) {
161 remote = of_graph_get_remote_port_parent(ep);
162 if (!remote || !of_device_is_available(remote)) {
165 } else if (!of_device_is_available(remote->parent)) {
166 dev_warn(dev, "parent device of %pOF is not available\n",
172 drm_of_component_match_add(dev, &match, compare_of,
179 return component_master_add_with_match(dev, m_ops, match);
181 EXPORT_SYMBOL(drm_of_component_probe);
184 * drm_of_encoder_active_endpoint - return the active encoder endpoint
185 * @node: device tree node containing encoder input ports
186 * @encoder: drm_encoder
188 * Given an encoder device node and a drm_encoder with a connected crtc,
189 * parse the encoder endpoint connecting to the crtc port.
191 int drm_of_encoder_active_endpoint(struct device_node *node,
192 struct drm_encoder *encoder,
193 struct of_endpoint *endpoint)
195 struct device_node *ep;
196 struct drm_crtc *crtc = encoder->crtc;
197 struct device_node *port;
203 for_each_endpoint_of_node(node, ep) {
204 port = of_graph_get_remote_port(ep);
206 if (port == crtc->port) {
207 ret = of_graph_parse_endpoint(ep, endpoint);
215 EXPORT_SYMBOL_GPL(drm_of_encoder_active_endpoint);
217 static int find_panel_or_bridge(struct device_node *node,
218 struct drm_panel **panel,
219 struct drm_bridge **bridge)
222 *panel = of_drm_find_panel(node);
226 /* Clear the panel pointer in case of error. */
230 /* No panel found yet, check for a bridge next. */
232 *bridge = of_drm_find_bridge(node);
237 return -EPROBE_DEFER;
241 * drm_of_find_panel_or_bridge - return connected panel or bridge device
242 * @np: device tree node containing encoder output ports
243 * @port: port in the device tree node
244 * @endpoint: endpoint in the device tree node
245 * @panel: pointer to hold returned drm_panel
246 * @bridge: pointer to hold returned drm_bridge
248 * Given a DT node's port and endpoint number, find the connected node and
249 * return either the associated struct drm_panel or drm_bridge device. Either
250 * @panel or @bridge must not be NULL.
252 * This function is deprecated and should not be used in new drivers. Use
253 * devm_drm_of_get_bridge() instead.
255 * Returns zero if successful, or one of the standard error codes if it fails.
257 int drm_of_find_panel_or_bridge(const struct device_node *np,
258 int port, int endpoint,
259 struct drm_panel **panel,
260 struct drm_bridge **bridge)
262 struct device_node *node;
265 if (!panel && !bridge)
273 /* Check for a graph on the device node first. */
274 if (of_graph_is_present(np)) {
275 node = of_graph_get_remote_node(np, port, endpoint);
277 ret = find_panel_or_bridge(node, panel, bridge);
285 /* Otherwise check for any child node other than port/ports. */
286 for_each_available_child_of_node(np, node) {
287 if (of_node_name_eq(node, "port") ||
288 of_node_name_eq(node, "ports"))
291 ret = find_panel_or_bridge(node, panel, bridge);
294 /* Stop at the first found occurrence. */
299 return -EPROBE_DEFER;
301 EXPORT_SYMBOL_GPL(drm_of_find_panel_or_bridge);
303 enum drm_of_lvds_pixels {
304 DRM_OF_LVDS_EVEN = BIT(0),
305 DRM_OF_LVDS_ODD = BIT(1),
308 static int drm_of_lvds_get_port_pixels_type(struct device_node *port_node)
311 of_property_read_bool(port_node, "dual-lvds-even-pixels");
313 of_property_read_bool(port_node, "dual-lvds-odd-pixels");
315 return (even_pixels ? DRM_OF_LVDS_EVEN : 0) |
316 (odd_pixels ? DRM_OF_LVDS_ODD : 0);
319 static int drm_of_lvds_get_remote_pixels_type(
320 const struct device_node *port_node)
322 struct device_node *endpoint = NULL;
323 int pixels_type = -EPIPE;
325 for_each_child_of_node(port_node, endpoint) {
326 struct device_node *remote_port;
329 if (!of_node_name_eq(endpoint, "endpoint"))
332 remote_port = of_graph_get_remote_port(endpoint);
334 of_node_put(endpoint);
338 current_pt = drm_of_lvds_get_port_pixels_type(remote_port);
339 of_node_put(remote_port);
341 pixels_type = current_pt;
344 * Sanity check, ensure that all remote endpoints have the same
345 * pixel type. We may lift this restriction later if we need to
346 * support multiple sinks with different dual-link
347 * configurations by passing the endpoints explicitly to
348 * drm_of_lvds_get_dual_link_pixel_order().
350 if (!current_pt || pixels_type != current_pt) {
351 of_node_put(endpoint);
360 * drm_of_lvds_get_dual_link_pixel_order - Get LVDS dual-link pixel order
361 * @port1: First DT port node of the Dual-link LVDS source
362 * @port2: Second DT port node of the Dual-link LVDS source
364 * An LVDS dual-link connection is made of two links, with even pixels
365 * transitting on one link, and odd pixels on the other link. This function
366 * returns, for two ports of an LVDS dual-link source, which port shall transmit
367 * the even and odd pixels, based on the requirements of the connected sink.
369 * The pixel order is determined from the dual-lvds-even-pixels and
370 * dual-lvds-odd-pixels properties in the sink's DT port nodes. If those
371 * properties are not present, or if their usage is not valid, this function
374 * If either port is not connected, this function returns -EPIPE.
376 * @port1 and @port2 are typically DT sibling nodes, but may have different
377 * parents when, for instance, two separate LVDS encoders carry the even and odd
381 * * DRM_LVDS_DUAL_LINK_EVEN_ODD_PIXELS - @port1 carries even pixels and @port2
383 * * DRM_LVDS_DUAL_LINK_ODD_EVEN_PIXELS - @port1 carries odd pixels and @port2
384 * carries even pixels
385 * * -EINVAL - @port1 and @port2 are not connected to a dual-link LVDS sink, or
386 * the sink configuration is invalid
387 * * -EPIPE - when @port1 or @port2 are not connected
389 int drm_of_lvds_get_dual_link_pixel_order(const struct device_node *port1,
390 const struct device_node *port2)
392 int remote_p1_pt, remote_p2_pt;
394 if (!port1 || !port2)
397 remote_p1_pt = drm_of_lvds_get_remote_pixels_type(port1);
398 if (remote_p1_pt < 0)
401 remote_p2_pt = drm_of_lvds_get_remote_pixels_type(port2);
402 if (remote_p2_pt < 0)
406 * A valid dual-lVDS bus is found when one remote port is marked with
407 * "dual-lvds-even-pixels", and the other remote port is marked with
408 * "dual-lvds-odd-pixels", bail out if the markers are not right.
410 if (remote_p1_pt + remote_p2_pt != DRM_OF_LVDS_EVEN + DRM_OF_LVDS_ODD)
413 return remote_p1_pt == DRM_OF_LVDS_EVEN ?
414 DRM_LVDS_DUAL_LINK_EVEN_ODD_PIXELS :
415 DRM_LVDS_DUAL_LINK_ODD_EVEN_PIXELS;
417 EXPORT_SYMBOL_GPL(drm_of_lvds_get_dual_link_pixel_order);
420 * drm_of_lvds_get_data_mapping - Get LVDS data mapping
421 * @port: DT port node of the LVDS source or sink
423 * Convert DT "data-mapping" property string value into media bus format value.
426 * * MEDIA_BUS_FMT_RGB666_1X7X3_SPWG - data-mapping is "jeida-18"
427 * * MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA - data-mapping is "jeida-24"
428 * * MEDIA_BUS_FMT_RGB888_1X7X4_SPWG - data-mapping is "vesa-24"
429 * * -EINVAL - the "data-mapping" property is unsupported
430 * * -ENODEV - the "data-mapping" property is missing
432 int drm_of_lvds_get_data_mapping(const struct device_node *port)
437 ret = of_property_read_string(port, "data-mapping", &mapping);
441 if (!strcmp(mapping, "jeida-18"))
442 return MEDIA_BUS_FMT_RGB666_1X7X3_SPWG;
443 if (!strcmp(mapping, "jeida-24"))
444 return MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA;
445 if (!strcmp(mapping, "vesa-24"))
446 return MEDIA_BUS_FMT_RGB888_1X7X4_SPWG;
450 EXPORT_SYMBOL_GPL(drm_of_lvds_get_data_mapping);