media: staging/imx: remove static media link arrays
[platform/kernel/linux-rpi.git] / drivers / staging / media / imx / imx-media-of.c
1 /*
2  * Media driver for Freescale i.MX5/6 SOC
3  *
4  * Open Firmware parsing.
5  *
6  * Copyright (c) 2016 Mentor Graphics Inc.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  */
13 #include <linux/of_platform.h>
14 #include <media/v4l2-ctrls.h>
15 #include <media/v4l2-device.h>
16 #include <media/v4l2-fwnode.h>
17 #include <media/v4l2-subdev.h>
18 #include <media/videobuf2-dma-contig.h>
19 #include <linux/of_graph.h>
20 #include <video/imx-ipu-v3.h>
21 #include "imx-media.h"
22
23 static int of_get_port_count(const struct device_node *np)
24 {
25         struct device_node *ports, *child;
26         int num = 0;
27
28         /* check if this node has a ports subnode */
29         ports = of_get_child_by_name(np, "ports");
30         if (ports)
31                 np = ports;
32
33         for_each_child_of_node(np, child)
34                 if (of_node_cmp(child->name, "port") == 0)
35                         num++;
36
37         of_node_put(ports);
38         return num;
39 }
40
41 /*
42  * find the remote device node given local endpoint node
43  */
44 static void of_get_remote(struct device_node *epnode,
45                           struct device_node **remote_node)
46 {
47         struct device_node *rp, *rpp;
48         struct device_node *remote;
49
50         rp = of_graph_get_remote_port(epnode);
51         rpp = of_graph_get_remote_port_parent(epnode);
52
53         if (of_device_is_compatible(rpp, "fsl,imx6q-ipu")) {
54                 /* the remote is one of the CSI ports */
55                 remote = rp;
56                 of_node_put(rpp);
57         } else {
58                 remote = rpp;
59                 of_node_put(rp);
60         }
61
62         if (!of_device_is_available(remote)) {
63                 of_node_put(remote);
64                 *remote_node = NULL;
65         } else {
66                 *remote_node = remote;
67         }
68 }
69
70 static int
71 of_parse_subdev(struct imx_media_dev *imxmd, struct device_node *sd_np,
72                 bool is_csi_port)
73 {
74         struct imx_media_subdev *imxsd;
75         int i, num_pads, ret;
76
77         if (!of_device_is_available(sd_np)) {
78                 dev_dbg(imxmd->md.dev, "%s: %s not enabled\n", __func__,
79                         sd_np->name);
80                 /* unavailable is not an error */
81                 return 0;
82         }
83
84         /* register this subdev with async notifier */
85         imxsd = imx_media_add_async_subdev(imxmd, sd_np, NULL);
86         ret = PTR_ERR_OR_ZERO(imxsd);
87         if (ret) {
88                 if (ret == -EEXIST) {
89                         /* already added, everything is fine */
90                         return 0;
91                 }
92
93                 /* other error, can't continue */
94                 return ret;
95         }
96
97         if (is_csi_port) {
98                 /*
99                  * the ipu-csi has one sink port and two source ports.
100                  * The source ports are not represented in the device tree,
101                  * but are described by the internal pads and links later.
102                  */
103                 num_pads = CSI_NUM_PADS;
104                 imxsd->num_sink_pads = CSI_NUM_SINK_PADS;
105         } else if (of_device_is_compatible(sd_np, "fsl,imx6-mipi-csi2")) {
106                 num_pads = of_get_port_count(sd_np);
107                 /* the mipi csi2 receiver has only one sink port */
108                 imxsd->num_sink_pads = 1;
109         } else if (of_device_is_compatible(sd_np, "video-mux")) {
110                 num_pads = of_get_port_count(sd_np);
111                 /* for the video mux, all but the last port are sinks */
112                 imxsd->num_sink_pads = num_pads - 1;
113         } else {
114                 num_pads = of_get_port_count(sd_np);
115                 if (num_pads != 1) {
116                         /* confused, but no reason to give up here */
117                         dev_warn(imxmd->md.dev,
118                                  "%s: unknown device %s with %d ports\n",
119                                  __func__, sd_np->name, num_pads);
120                         return 0;
121                 }
122
123                 /*
124                  * we got to this node from this single source port,
125                  * there are no sink pads.
126                  */
127                 imxsd->num_sink_pads = 0;
128         }
129
130         if (imxsd->num_sink_pads >= num_pads)
131                 return -EINVAL;
132
133         imxsd->num_src_pads = num_pads - imxsd->num_sink_pads;
134
135         dev_dbg(imxmd->md.dev, "%s: %s has %d pads (%d sink, %d src)\n",
136                 __func__, sd_np->name, num_pads,
137                 imxsd->num_sink_pads, imxsd->num_src_pads);
138
139         for (i = 0; i < num_pads; i++) {
140                 struct device_node *epnode = NULL, *port, *remote_np;
141
142                 if (is_csi_port)
143                         port = (i < imxsd->num_sink_pads) ? sd_np : NULL;
144                 else
145                         port = of_graph_get_port_by_id(sd_np, i);
146                 if (!port)
147                         continue;
148
149                 for_each_child_of_node(port, epnode) {
150                         of_get_remote(epnode, &remote_np);
151                         if (!remote_np)
152                                 continue;
153
154                         if (i < imxsd->num_sink_pads) {
155                                 /* follow sink endpoints upstream */
156                                 ret = of_parse_subdev(imxmd, remote_np, false);
157                                 if (ret)
158                                         break;
159                         }
160
161                         of_node_put(remote_np);
162                 }
163
164                 if (port != sd_np)
165                         of_node_put(port);
166                 if (ret) {
167                         of_node_put(remote_np);
168                         of_node_put(epnode);
169                         break;
170                 }
171         }
172
173         return ret;
174 }
175
176 int imx_media_add_of_subdevs(struct imx_media_dev *imxmd,
177                              struct device_node *np)
178 {
179         struct device_node *csi_np;
180         int i, ret;
181
182         for (i = 0; ; i++) {
183                 csi_np = of_parse_phandle(np, "ports", i);
184                 if (!csi_np)
185                         break;
186
187                 ret = of_parse_subdev(imxmd, csi_np, true);
188                 of_node_put(csi_np);
189                 if (ret)
190                         return ret;
191         }
192
193         return 0;
194 }
195
196 /*
197  * Create a single media link to/from imxsd using a fwnode link.
198  *
199  * NOTE: this function assumes an OF port node is equivalent to
200  * a media pad (port id equal to media pad index), and that an
201  * OF endpoint node is equivalent to a media link.
202  */
203 static int create_of_link(struct imx_media_dev *imxmd,
204                           struct imx_media_subdev *imxsd,
205                           struct v4l2_fwnode_link *link)
206 {
207         struct v4l2_subdev *sd = imxsd->sd;
208         struct imx_media_subdev *remote;
209         struct v4l2_subdev *src, *sink;
210         int src_pad, sink_pad;
211
212         if (link->local_port >= sd->entity.num_pads)
213                 return -EINVAL;
214
215         remote = imx_media_find_async_subdev(imxmd,
216                                              to_of_node(link->remote_node),
217                                              NULL);
218         if (!remote)
219                 return 0;
220
221         if (sd->entity.pads[link->local_port].flags & MEDIA_PAD_FL_SINK) {
222                 src = remote->sd;
223                 src_pad = link->remote_port;
224                 sink = sd;
225                 sink_pad = link->local_port;
226         } else {
227                 src = sd;
228                 src_pad = link->local_port;
229                 sink = remote->sd;
230                 sink_pad = link->remote_port;
231         }
232
233         /* make sure link doesn't already exist before creating */
234         if (media_entity_find_link(&src->entity.pads[src_pad],
235                                    &sink->entity.pads[sink_pad]))
236                 return 0;
237
238         v4l2_info(sd->v4l2_dev, "%s:%d -> %s:%d\n",
239                   src->name, src_pad, sink->name, sink_pad);
240
241         return media_create_pad_link(&src->entity, src_pad,
242                                      &sink->entity, sink_pad, 0);
243 }
244
245 /*
246  * Create media links to/from imxsd using its device-tree endpoints.
247  */
248 int imx_media_create_of_links(struct imx_media_dev *imxmd,
249                               struct imx_media_subdev *imxsd)
250 {
251         struct v4l2_subdev *sd = imxsd->sd;
252         struct v4l2_fwnode_link link;
253         struct device_node *ep;
254         int ret;
255
256         for_each_endpoint_of_node(sd->dev->of_node, ep) {
257                 ret = v4l2_fwnode_parse_link(of_fwnode_handle(ep), &link);
258                 if (ret)
259                         continue;
260
261                 ret = create_of_link(imxmd, imxsd, &link);
262                 v4l2_fwnode_put_link(&link);
263                 if (ret)
264                         return ret;
265         }
266
267         return 0;
268 }
269
270 /*
271  * Create media links to the given CSI subdevice's sink pads,
272  * using its device-tree endpoints.
273  */
274 int imx_media_create_csi_of_links(struct imx_media_dev *imxmd,
275                                   struct imx_media_subdev *csi)
276 {
277         struct device_node *csi_np = csi->sd->dev->of_node;
278         struct fwnode_handle *fwnode, *csi_ep;
279         struct v4l2_fwnode_link link;
280         struct device_node *ep;
281         int ret;
282
283         link.local_node = of_fwnode_handle(csi_np);
284         link.local_port = CSI_SINK_PAD;
285
286         for_each_child_of_node(csi_np, ep) {
287                 csi_ep = of_fwnode_handle(ep);
288
289                 fwnode = fwnode_graph_get_remote_endpoint(csi_ep);
290                 if (!fwnode)
291                         continue;
292
293                 fwnode = fwnode_get_parent(fwnode);
294                 fwnode_property_read_u32(fwnode, "reg", &link.remote_port);
295                 fwnode = fwnode_get_next_parent(fwnode);
296                 if (is_of_node(fwnode) &&
297                     of_node_cmp(to_of_node(fwnode)->name, "ports") == 0)
298                         fwnode = fwnode_get_next_parent(fwnode);
299                 link.remote_node = fwnode;
300
301                 ret = create_of_link(imxmd, csi, &link);
302                 fwnode_handle_put(link.remote_node);
303                 if (ret)
304                         return ret;
305         }
306
307         return 0;
308 }