a085e5213ef022b09b12d1565a279b464e1b1f38
[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 bool 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         bool is_csi_port;
50
51         rp = of_graph_get_remote_port(epnode);
52         rpp = of_graph_get_remote_port_parent(epnode);
53
54         if (of_device_is_compatible(rpp, "fsl,imx6q-ipu")) {
55                 /* the remote is one of the CSI ports */
56                 remote = rp;
57                 of_node_put(rpp);
58                 is_csi_port = true;
59         } else {
60                 remote = rpp;
61                 of_node_put(rp);
62                 is_csi_port = false;
63         }
64
65         if (!of_device_is_available(remote)) {
66                 of_node_put(remote);
67                 *remote_node = NULL;
68         } else {
69                 *remote_node = remote;
70         }
71
72         return is_csi_port;
73 }
74
75 static int
76 of_parse_subdev(struct imx_media_dev *imxmd, struct device_node *sd_np,
77                 bool is_csi_port)
78 {
79         struct imx_media_subdev *imxsd;
80         int i, num_ports, ret;
81
82         if (!of_device_is_available(sd_np)) {
83                 dev_dbg(imxmd->md.dev, "%s: %s not enabled\n", __func__,
84                         sd_np->name);
85                 /* unavailable is not an error */
86                 return 0;
87         }
88
89         /* register this subdev with async notifier */
90         imxsd = imx_media_add_async_subdev(imxmd, sd_np, NULL);
91         ret = PTR_ERR_OR_ZERO(imxsd);
92         if (ret) {
93                 if (ret == -EEXIST) {
94                         /* already added, everything is fine */
95                         return 0;
96                 }
97
98                 /* other error, can't continue */
99                 return ret;
100         }
101
102         /*
103          * the ipu-csi has one sink port. The source pads are not
104          * represented in the device tree by port nodes, but are
105          * described by the internal pads and links later.
106          */
107         num_ports = is_csi_port ? 1 : of_get_port_count(sd_np);
108
109         for (i = 0; i < num_ports; i++) {
110                 struct device_node *epnode = NULL, *port, *remote_np;
111
112                 port = is_csi_port ? sd_np : of_graph_get_port_by_id(sd_np, i);
113                 if (!port)
114                         continue;
115
116                 for_each_child_of_node(port, epnode) {
117                         bool remote_is_csi;
118
119                         remote_is_csi = of_get_remote(epnode, &remote_np);
120                         if (!remote_np)
121                                 continue;
122
123                         ret = of_parse_subdev(imxmd, remote_np, remote_is_csi);
124                         of_node_put(remote_np);
125                         if (ret)
126                                 break;
127                 }
128
129                 if (port != sd_np)
130                         of_node_put(port);
131                 if (ret) {
132                         of_node_put(epnode);
133                         break;
134                 }
135         }
136
137         return ret;
138 }
139
140 int imx_media_add_of_subdevs(struct imx_media_dev *imxmd,
141                              struct device_node *np)
142 {
143         struct device_node *csi_np;
144         int i, ret;
145
146         for (i = 0; ; i++) {
147                 csi_np = of_parse_phandle(np, "ports", i);
148                 if (!csi_np)
149                         break;
150
151                 ret = of_parse_subdev(imxmd, csi_np, true);
152                 of_node_put(csi_np);
153                 if (ret)
154                         return ret;
155         }
156
157         return 0;
158 }
159
160 /*
161  * Create a single media link to/from imxsd using a fwnode link.
162  *
163  * NOTE: this function assumes an OF port node is equivalent to
164  * a media pad (port id equal to media pad index), and that an
165  * OF endpoint node is equivalent to a media link.
166  */
167 static int create_of_link(struct imx_media_dev *imxmd,
168                           struct imx_media_subdev *imxsd,
169                           struct v4l2_fwnode_link *link)
170 {
171         struct v4l2_subdev *sd = imxsd->sd;
172         struct imx_media_subdev *remote;
173         struct v4l2_subdev *src, *sink;
174         int src_pad, sink_pad;
175
176         if (link->local_port >= sd->entity.num_pads)
177                 return -EINVAL;
178
179         remote = imx_media_find_async_subdev(imxmd,
180                                              to_of_node(link->remote_node),
181                                              NULL);
182         if (!remote)
183                 return 0;
184
185         if (sd->entity.pads[link->local_port].flags & MEDIA_PAD_FL_SINK) {
186                 src = remote->sd;
187                 src_pad = link->remote_port;
188                 sink = sd;
189                 sink_pad = link->local_port;
190         } else {
191                 src = sd;
192                 src_pad = link->local_port;
193                 sink = remote->sd;
194                 sink_pad = link->remote_port;
195         }
196
197         /* make sure link doesn't already exist before creating */
198         if (media_entity_find_link(&src->entity.pads[src_pad],
199                                    &sink->entity.pads[sink_pad]))
200                 return 0;
201
202         v4l2_info(sd->v4l2_dev, "%s:%d -> %s:%d\n",
203                   src->name, src_pad, sink->name, sink_pad);
204
205         return media_create_pad_link(&src->entity, src_pad,
206                                      &sink->entity, sink_pad, 0);
207 }
208
209 /*
210  * Create media links to/from imxsd using its device-tree endpoints.
211  */
212 int imx_media_create_of_links(struct imx_media_dev *imxmd,
213                               struct imx_media_subdev *imxsd)
214 {
215         struct v4l2_subdev *sd = imxsd->sd;
216         struct v4l2_fwnode_link link;
217         struct device_node *ep;
218         int ret;
219
220         for_each_endpoint_of_node(sd->dev->of_node, ep) {
221                 ret = v4l2_fwnode_parse_link(of_fwnode_handle(ep), &link);
222                 if (ret)
223                         continue;
224
225                 ret = create_of_link(imxmd, imxsd, &link);
226                 v4l2_fwnode_put_link(&link);
227                 if (ret)
228                         return ret;
229         }
230
231         return 0;
232 }
233
234 /*
235  * Create media links to the given CSI subdevice's sink pads,
236  * using its device-tree endpoints.
237  */
238 int imx_media_create_csi_of_links(struct imx_media_dev *imxmd,
239                                   struct imx_media_subdev *csi)
240 {
241         struct device_node *csi_np = csi->sd->dev->of_node;
242         struct fwnode_handle *fwnode, *csi_ep;
243         struct v4l2_fwnode_link link;
244         struct device_node *ep;
245         int ret;
246
247         link.local_node = of_fwnode_handle(csi_np);
248         link.local_port = CSI_SINK_PAD;
249
250         for_each_child_of_node(csi_np, ep) {
251                 csi_ep = of_fwnode_handle(ep);
252
253                 fwnode = fwnode_graph_get_remote_endpoint(csi_ep);
254                 if (!fwnode)
255                         continue;
256
257                 fwnode = fwnode_get_parent(fwnode);
258                 fwnode_property_read_u32(fwnode, "reg", &link.remote_port);
259                 fwnode = fwnode_get_next_parent(fwnode);
260                 if (is_of_node(fwnode) &&
261                     of_node_cmp(to_of_node(fwnode)->name, "ports") == 0)
262                         fwnode = fwnode_get_next_parent(fwnode);
263                 link.remote_node = fwnode;
264
265                 ret = create_of_link(imxmd, csi, &link);
266                 fwnode_handle_put(link.remote_node);
267                 if (ret)
268                         return ret;
269         }
270
271         return 0;
272 }