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>
12 static void drm_release_of(struct device
*dev
, void *data
)
18 * drm_crtc_port_mask - find the mask of a registered CRTC by port OF node
22 * Given a port OF node, return the possible mask of the corresponding
23 * CRTC within a device's list of CRTCs. Returns zero if not found.
25 static uint32_t drm_crtc_port_mask(struct drm_device
*dev
,
26 struct device_node
*port
)
28 unsigned int index
= 0;
31 drm_for_each_crtc(tmp
, dev
) {
32 if (tmp
->port
== port
)
42 * drm_of_find_possible_crtcs - find the possible CRTCs for an encoder port
44 * @port: encoder port to scan for endpoints
46 * Scan all endpoints attached to a port, locate their attached CRTCs,
47 * and generate the DRM mask of CRTCs which may be attached to this
50 * See Documentation/devicetree/bindings/graph.txt for the bindings.
52 uint32_t drm_of_find_possible_crtcs(struct drm_device
*dev
,
53 struct device_node
*port
)
55 struct device_node
*remote_port
, *ep
;
56 uint32_t possible_crtcs
= 0;
58 for_each_endpoint_of_node(port
, ep
) {
59 remote_port
= of_graph_get_remote_port(ep
);
65 possible_crtcs
|= drm_crtc_port_mask(dev
, remote_port
);
67 of_node_put(remote_port
);
70 return possible_crtcs
;
72 EXPORT_SYMBOL(drm_of_find_possible_crtcs
);
75 * drm_of_component_match_add - Add a component helper OF node match rule
76 * @master: master device
77 * @matchptr: component match pointer
78 * @compare: compare function used for matching component
81 void drm_of_component_match_add(struct device
*master
,
82 struct component_match
**matchptr
,
83 int (*compare
)(struct device
*, void *),
84 struct device_node
*node
)
87 component_match_add_release(master
, matchptr
, drm_release_of
,
90 EXPORT_SYMBOL_GPL(drm_of_component_match_add
);
93 * drm_of_component_probe - Generic probe function for a component based master
94 * @dev: master device containing the OF node
95 * @compare_of: compare function used for matching components
96 * @master_ops: component master ops to be used
98 * Parse the platform device OF node and bind all the components associated
99 * with the master. Interface ports are added before the encoders in order to
100 * satisfy their .bind requirements
101 * See Documentation/devicetree/bindings/graph.txt for the bindings.
103 * Returns zero if successful, or one of the standard error codes if it fails.
105 int drm_of_component_probe(struct device
*dev
,
106 int (*compare_of
)(struct device
*, void *),
107 const struct component_master_ops
*m_ops
)
109 struct device_node
*ep
, *port
, *remote
;
110 struct component_match
*match
= NULL
;
117 * Bind the crtc's ports first, so that drm_of_find_possible_crtcs()
118 * called from encoder's .bind callbacks works as expected
121 port
= of_parse_phandle(dev
->of_node
, "ports", i
);
125 if (!of_device_is_available(port
->parent
)) {
130 drm_of_component_match_add(dev
, &match
, compare_of
, port
);
135 dev_err(dev
, "missing 'ports' property\n");
140 dev_err(dev
, "no available port\n");
145 * For bound crtcs, bind the encoders attached to their remote endpoint
148 port
= of_parse_phandle(dev
->of_node
, "ports", i
);
152 if (!of_device_is_available(port
->parent
)) {
157 for_each_child_of_node(port
, ep
) {
158 remote
= of_graph_get_remote_port_parent(ep
);
159 if (!remote
|| !of_device_is_available(remote
)) {
162 } else if (!of_device_is_available(remote
->parent
)) {
163 dev_warn(dev
, "parent device of %pOF is not available\n",
169 drm_of_component_match_add(dev
, &match
, compare_of
,
176 return component_master_add_with_match(dev
, m_ops
, match
);
178 EXPORT_SYMBOL(drm_of_component_probe
);
181 * drm_of_encoder_active_endpoint - return the active encoder endpoint
182 * @node: device tree node containing encoder input ports
183 * @encoder: drm_encoder
185 * Given an encoder device node and a drm_encoder with a connected crtc,
186 * parse the encoder endpoint connecting to the crtc port.
188 int drm_of_encoder_active_endpoint(struct device_node
*node
,
189 struct drm_encoder
*encoder
,
190 struct of_endpoint
*endpoint
)
192 struct device_node
*ep
;
193 struct drm_crtc
*crtc
= encoder
->crtc
;
194 struct device_node
*port
;
200 for_each_endpoint_of_node(node
, ep
) {
201 port
= of_graph_get_remote_port(ep
);
203 if (port
== crtc
->port
) {
204 ret
= of_graph_parse_endpoint(ep
, endpoint
);
212 EXPORT_SYMBOL_GPL(drm_of_encoder_active_endpoint
);
215 * drm_of_find_panel_or_bridge - return connected panel or bridge device
216 * @np: device tree node containing encoder output ports
217 * @panel: pointer to hold returned drm_panel
218 * @bridge: pointer to hold returned drm_bridge
220 * Given a DT node's port and endpoint number, find the connected node and
221 * return either the associated struct drm_panel or drm_bridge device. Either
222 * @panel or @bridge must not be NULL.
224 * Returns zero if successful, or one of the standard error codes if it fails.
226 int drm_of_find_panel_or_bridge(const struct device_node
*np
,
227 int port
, int endpoint
,
228 struct drm_panel
**panel
,
229 struct drm_bridge
**bridge
)
231 int ret
= -EPROBE_DEFER
;
232 struct device_node
*remote
;
234 if (!panel
&& !bridge
)
239 remote
= of_graph_get_remote_node(np
, port
, endpoint
);
244 *panel
= of_drm_find_panel(remote
);
249 /* No panel found yet, check for a bridge next. */
252 *bridge
= of_drm_find_bridge(remote
);
264 EXPORT_SYMBOL_GPL(drm_of_find_panel_or_bridge
);