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_encoder.h>
10 #include <drm/drm_panel.h>
11 #include <drm/drm_of.h>
16 * A set of helper functions to aid DRM drivers in parsing standard DT
20 static void drm_release_of(struct device *dev, void *data)
26 * drm_of_crtc_port_mask - find the mask of a registered CRTC by port OF node
30 * Given a port OF node, return the possible mask of the corresponding
31 * CRTC within a device's list of CRTCs. Returns zero if not found.
33 uint32_t drm_of_crtc_port_mask(struct drm_device *dev,
34 struct device_node *port)
36 unsigned int index = 0;
39 drm_for_each_crtc(tmp, dev) {
40 if (tmp->port == port)
48 EXPORT_SYMBOL(drm_of_crtc_port_mask);
51 * drm_of_find_possible_crtcs - find the possible CRTCs for an encoder port
53 * @port: encoder port to scan for endpoints
55 * Scan all endpoints attached to a port, locate their attached CRTCs,
56 * and generate the DRM mask of CRTCs which may be attached to this
59 * See Documentation/devicetree/bindings/graph.txt for the bindings.
61 uint32_t drm_of_find_possible_crtcs(struct drm_device *dev,
62 struct device_node *port)
64 struct device_node *remote_port, *ep;
65 uint32_t possible_crtcs = 0;
67 for_each_endpoint_of_node(port, ep) {
68 remote_port = of_graph_get_remote_port(ep);
74 possible_crtcs |= drm_of_crtc_port_mask(dev, remote_port);
76 of_node_put(remote_port);
79 return possible_crtcs;
81 EXPORT_SYMBOL(drm_of_find_possible_crtcs);
84 * drm_of_component_match_add - Add a component helper OF node match rule
85 * @master: master device
86 * @matchptr: component match pointer
87 * @compare: compare function used for matching component
90 void drm_of_component_match_add(struct device *master,
91 struct component_match **matchptr,
92 int (*compare)(struct device *, void *),
93 struct device_node *node)
96 component_match_add_release(master, matchptr, drm_release_of,
99 EXPORT_SYMBOL_GPL(drm_of_component_match_add);
102 * drm_of_component_probe - Generic probe function for a component based master
103 * @dev: master device containing the OF node
104 * @compare_of: compare function used for matching components
105 * @m_ops: component master ops to be used
107 * Parse the platform device OF node and bind all the components associated
108 * with the master. Interface ports are added before the encoders in order to
109 * satisfy their .bind requirements
110 * See Documentation/devicetree/bindings/graph.txt for the bindings.
112 * Returns zero if successful, or one of the standard error codes if it fails.
114 int drm_of_component_probe(struct device *dev,
115 int (*compare_of)(struct device *, void *),
116 const struct component_master_ops *m_ops)
118 struct device_node *ep, *port, *remote;
119 struct component_match *match = NULL;
126 * Bind the crtc's ports first, so that drm_of_find_possible_crtcs()
127 * called from encoder's .bind callbacks works as expected
130 port = of_parse_phandle(dev->of_node, "ports", i);
134 if (of_device_is_available(port->parent))
135 drm_of_component_match_add(dev, &match, compare_of,
142 dev_err(dev, "missing 'ports' property\n");
147 dev_err(dev, "no available port\n");
152 * For bound crtcs, bind the encoders attached to their remote endpoint
155 port = of_parse_phandle(dev->of_node, "ports", i);
159 if (!of_device_is_available(port->parent)) {
164 for_each_child_of_node(port, ep) {
165 remote = of_graph_get_remote_port_parent(ep);
166 if (!remote || !of_device_is_available(remote)) {
169 } else if (!of_device_is_available(remote->parent)) {
170 dev_warn(dev, "parent device of %pOF is not available\n",
176 drm_of_component_match_add(dev, &match, compare_of,
183 return component_master_add_with_match(dev, m_ops, match);
185 EXPORT_SYMBOL(drm_of_component_probe);
188 * drm_of_encoder_active_endpoint - return the active encoder endpoint
189 * @node: device tree node containing encoder input ports
190 * @encoder: drm_encoder
192 * Given an encoder device node and a drm_encoder with a connected crtc,
193 * parse the encoder endpoint connecting to the crtc port.
195 int drm_of_encoder_active_endpoint(struct device_node *node,
196 struct drm_encoder *encoder,
197 struct of_endpoint *endpoint)
199 struct device_node *ep;
200 struct drm_crtc *crtc = encoder->crtc;
201 struct device_node *port;
207 for_each_endpoint_of_node(node, ep) {
208 port = of_graph_get_remote_port(ep);
210 if (port == crtc->port) {
211 ret = of_graph_parse_endpoint(ep, endpoint);
219 EXPORT_SYMBOL_GPL(drm_of_encoder_active_endpoint);
222 * drm_of_find_panel_or_bridge - return connected panel or bridge device
223 * @np: device tree node containing encoder output ports
224 * @port: port in the device tree node
225 * @endpoint: endpoint in the device tree node
226 * @panel: pointer to hold returned drm_panel
227 * @bridge: pointer to hold returned drm_bridge
229 * Given a DT node's port and endpoint number, find the connected node and
230 * return either the associated struct drm_panel or drm_bridge device. Either
231 * @panel or @bridge must not be NULL.
233 * Returns zero if successful, or one of the standard error codes if it fails.
235 int drm_of_find_panel_or_bridge(const struct device_node *np,
236 int port, int endpoint,
237 struct drm_panel **panel,
238 struct drm_bridge **bridge)
240 int ret = -EPROBE_DEFER;
241 struct device_node *remote;
243 if (!panel && !bridge)
248 remote = of_graph_get_remote_node(np, port, endpoint);
252 if (!of_device_is_available(remote)) {
258 *panel = of_drm_find_panel(remote);
265 /* No panel found yet, check for a bridge next. */
268 *bridge = of_drm_find_bridge(remote);
280 EXPORT_SYMBOL_GPL(drm_of_find_panel_or_bridge);