UPSTREAM: drm/edid: Extract SADs properly from multiple audio data blocks
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / drm_of.c
1 #include <linux/component.h>
2 #include <linux/export.h>
3 #include <linux/list.h>
4 #include <linux/of_graph.h>
5 #include <drm/drmP.h>
6 #include <drm/drm_crtc.h>
7 #include <drm/drm_of.h>
8
9 /**
10  * drm_crtc_port_mask - find the mask of a registered CRTC by port OF node
11  * @dev: DRM device
12  * @port: port OF node
13  *
14  * Given a port OF node, return the possible mask of the corresponding
15  * CRTC within a device's list of CRTCs.  Returns zero if not found.
16  */
17 static uint32_t drm_crtc_port_mask(struct drm_device *dev,
18                                    struct device_node *port)
19 {
20         unsigned int index = 0;
21         struct drm_crtc *tmp;
22
23         drm_for_each_crtc(tmp, dev) {
24                 if (tmp->port == port)
25                         return 1 << index;
26
27                 index++;
28         }
29
30         return 0;
31 }
32
33 /**
34  * drm_of_find_possible_crtcs - find the possible CRTCs for an encoder port
35  * @dev: DRM device
36  * @port: encoder port to scan for endpoints
37  *
38  * Scan all endpoints attached to a port, locate their attached CRTCs,
39  * and generate the DRM mask of CRTCs which may be attached to this
40  * encoder.
41  *
42  * See Documentation/devicetree/bindings/graph.txt for the bindings.
43  */
44 uint32_t drm_of_find_possible_crtcs(struct drm_device *dev,
45                                     struct device_node *port)
46 {
47         struct device_node *remote_port, *ep;
48         uint32_t possible_crtcs = 0;
49
50         for_each_endpoint_of_node(port, ep) {
51                 if (!of_device_is_available(ep)) {
52                         of_node_put(ep);
53                         continue;
54                 }
55
56                 remote_port = of_graph_get_remote_port(ep);
57                 if (!remote_port) {
58                         of_node_put(ep);
59                         return 0;
60                 }
61
62                 possible_crtcs |= drm_crtc_port_mask(dev, remote_port);
63
64                 of_node_put(remote_port);
65         }
66
67         return possible_crtcs;
68 }
69 EXPORT_SYMBOL(drm_of_find_possible_crtcs);
70
71 /**
72  * drm_of_component_probe - Generic probe function for a component based master
73  * @dev: master device containing the OF node
74  * @compare_of: compare function used for matching components
75  * @master_ops: component master ops to be used
76  *
77  * Parse the platform device OF node and bind all the components associated
78  * with the master. Interface ports are added before the encoders in order to
79  * satisfy their .bind requirements
80  * See Documentation/devicetree/bindings/graph.txt for the bindings.
81  *
82  * Returns zero if successful, or one of the standard error codes if it fails.
83  */
84 int drm_of_component_probe(struct device *dev,
85                            int (*compare_of)(struct device *, void *),
86                            const struct component_master_ops *m_ops)
87 {
88         struct device_node *ep, *port, *remote;
89         struct component_match *match = NULL;
90         int i;
91
92         if (!dev->of_node)
93                 return -EINVAL;
94
95         /*
96          * Bind the crtc's ports first, so that drm_of_find_possible_crtcs()
97          * called from encoder's .bind callbacks works as expected
98          */
99         for (i = 0; ; i++) {
100                 port = of_parse_phandle(dev->of_node, "ports", i);
101                 if (!port)
102                         break;
103
104                 if (!of_device_is_available(port->parent)) {
105                         of_node_put(port);
106                         continue;
107                 }
108
109                 component_match_add(dev, &match, compare_of, port);
110                 of_node_put(port);
111         }
112
113         if (i == 0) {
114                 dev_err(dev, "missing 'ports' property\n");
115                 return -ENODEV;
116         }
117
118         if (!match) {
119                 dev_err(dev, "no available port\n");
120                 return -ENODEV;
121         }
122
123         /*
124          * For bound crtcs, bind the encoders attached to their remote endpoint
125          */
126         for (i = 0; ; i++) {
127                 port = of_parse_phandle(dev->of_node, "ports", i);
128                 if (!port)
129                         break;
130
131                 if (!of_device_is_available(port->parent)) {
132                         of_node_put(port);
133                         continue;
134                 }
135
136                 for_each_child_of_node(port, ep) {
137                         remote = of_graph_get_remote_port_parent(ep);
138                         if (!remote || !of_device_is_available(remote)) {
139                                 of_node_put(remote);
140                                 continue;
141                         } else if (!of_device_is_available(remote->parent)) {
142                                 dev_warn(dev, "parent device of %s is not available\n",
143                                          remote->full_name);
144                                 of_node_put(remote);
145                                 continue;
146                         }
147
148                         component_match_add(dev, &match, compare_of, remote);
149                         of_node_put(remote);
150                 }
151                 of_node_put(port);
152         }
153
154         return component_master_add_with_match(dev, m_ops, match);
155 }
156 EXPORT_SYMBOL(drm_of_component_probe);
157
158 /*
159  * drm_of_encoder_active_endpoint - return the active encoder endpoint
160  * @node: device tree node containing encoder input ports
161  * @encoder: drm_encoder
162  *
163  * Given an encoder device node and a drm_encoder with a connected crtc,
164  * parse the encoder endpoint connecting to the crtc port.
165  */
166 int drm_of_encoder_active_endpoint(struct device_node *node,
167                                    struct drm_encoder *encoder,
168                                    struct of_endpoint *endpoint)
169 {
170         struct device_node *ep;
171         struct drm_crtc *crtc = encoder->crtc;
172         struct device_node *port;
173         int ret;
174
175         if (!node || !crtc)
176                 return -EINVAL;
177
178         for_each_endpoint_of_node(node, ep) {
179                 port = of_graph_get_remote_port(ep);
180                 of_node_put(port);
181                 if (port == crtc->port) {
182                         ret = of_graph_parse_endpoint(ep, endpoint);
183                         of_node_put(ep);
184                         return ret;
185                 }
186         }
187
188         return -EINVAL;
189 }
190 EXPORT_SYMBOL_GPL(drm_of_encoder_active_endpoint);