1 #include <linux/component.h>
2 #include <linux/export.h>
3 #include <linux/list.h>
4 #include <linux/of_graph.h>
6 #include <drm/drm_bridge.h>
7 #include <drm/drm_crtc.h>
8 #include <drm/drm_encoder.h>
9 #include <drm/drm_panel.h>
10 #include <drm/drm_of.h>
15 * A set of helper functions to aid DRM drivers in parsing standard DT
19 static void drm_release_of(struct device
*dev
, void *data
)
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
, drm_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 * Returns zero if successful, or one of the standard error codes if it fails.
234 int drm_of_find_panel_or_bridge(const struct device_node
*np
,
235 int port
, int endpoint
,
236 struct drm_panel
**panel
,
237 struct drm_bridge
**bridge
)
239 int ret
= -EPROBE_DEFER
;
240 struct device_node
*remote
;
242 if (!panel
&& !bridge
)
247 remote
= of_graph_get_remote_node(np
, port
, endpoint
);
251 if (!of_device_is_available(remote
)) {
257 *panel
= of_drm_find_panel(remote
);
264 /* No panel found yet, check for a bridge next. */
267 *bridge
= of_drm_find_bridge(remote
);
279 EXPORT_SYMBOL_GPL(drm_of_find_panel_or_bridge
);