883ad8595c853ce8db4034cd2db2505d9eab66a0
[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_add_pad_link(struct imx_media_dev *imxmd,
24                            struct imx_media_pad *pad,
25                            struct device_node *local_sd_node,
26                            struct device_node *remote_sd_node,
27                            int local_pad, int remote_pad)
28 {
29         dev_dbg(imxmd->md.dev, "%s: adding %s:%d -> %s:%d\n", __func__,
30                 local_sd_node->name, local_pad,
31                 remote_sd_node->name, remote_pad);
32
33         return imx_media_add_pad_link(imxmd, pad, remote_sd_node, NULL,
34                                       local_pad, remote_pad);
35 }
36
37 static int of_get_port_count(const struct device_node *np)
38 {
39         struct device_node *ports, *child;
40         int num = 0;
41
42         /* check if this node has a ports subnode */
43         ports = of_get_child_by_name(np, "ports");
44         if (ports)
45                 np = ports;
46
47         for_each_child_of_node(np, child)
48                 if (of_node_cmp(child->name, "port") == 0)
49                         num++;
50
51         of_node_put(ports);
52         return num;
53 }
54
55 /*
56  * find the remote device node and remote port id (remote pad #)
57  * given local endpoint node
58  */
59 static void of_get_remote_pad(struct device_node *epnode,
60                               struct device_node **remote_node,
61                               int *remote_pad)
62 {
63         struct device_node *rp, *rpp;
64         struct device_node *remote;
65
66         rp = of_graph_get_remote_port(epnode);
67         rpp = of_graph_get_remote_port_parent(epnode);
68
69         if (of_device_is_compatible(rpp, "fsl,imx6q-ipu")) {
70                 /* the remote is one of the CSI ports */
71                 remote = rp;
72                 *remote_pad = 0;
73                 of_node_put(rpp);
74         } else {
75                 remote = rpp;
76                 if (of_property_read_u32(rp, "reg", remote_pad))
77                         *remote_pad = 0;
78                 of_node_put(rp);
79         }
80
81         if (!of_device_is_available(remote)) {
82                 of_node_put(remote);
83                 *remote_node = NULL;
84         } else {
85                 *remote_node = remote;
86         }
87 }
88
89 static int
90 of_parse_subdev(struct imx_media_dev *imxmd, struct device_node *sd_np,
91                 bool is_csi_port, struct imx_media_subdev **subdev)
92 {
93         struct imx_media_subdev *imxsd;
94         int i, num_pads, ret;
95
96         if (!of_device_is_available(sd_np)) {
97                 dev_dbg(imxmd->md.dev, "%s: %s not enabled\n", __func__,
98                         sd_np->name);
99                 *subdev = NULL;
100                 /* unavailable is not an error */
101                 return 0;
102         }
103
104         /* register this subdev with async notifier */
105         imxsd = imx_media_add_async_subdev(imxmd, sd_np, NULL);
106         ret = PTR_ERR_OR_ZERO(imxsd);
107         if (ret) {
108                 if (ret == -EEXIST) {
109                         /* already added, everything is fine */
110                         *subdev = NULL;
111                         return 0;
112                 }
113
114                 /* other error, can't continue */
115                 return ret;
116         }
117         *subdev = imxsd;
118
119         if (is_csi_port) {
120                 /*
121                  * the ipu-csi has one sink port and two source ports.
122                  * The source ports are not represented in the device tree,
123                  * but are described by the internal pads and links later.
124                  */
125                 num_pads = CSI_NUM_PADS;
126                 imxsd->num_sink_pads = CSI_NUM_SINK_PADS;
127         } else if (of_device_is_compatible(sd_np, "fsl,imx6-mipi-csi2")) {
128                 num_pads = of_get_port_count(sd_np);
129                 /* the mipi csi2 receiver has only one sink port */
130                 imxsd->num_sink_pads = 1;
131         } else if (of_device_is_compatible(sd_np, "video-mux")) {
132                 num_pads = of_get_port_count(sd_np);
133                 /* for the video mux, all but the last port are sinks */
134                 imxsd->num_sink_pads = num_pads - 1;
135         } else {
136                 num_pads = of_get_port_count(sd_np);
137                 if (num_pads != 1) {
138                         /* confused, but no reason to give up here */
139                         dev_warn(imxmd->md.dev,
140                                  "%s: unknown device %s with %d ports\n",
141                                  __func__, sd_np->name, num_pads);
142                         return 0;
143                 }
144
145                 /*
146                  * we got to this node from this single source port,
147                  * there are no sink pads.
148                  */
149                 imxsd->num_sink_pads = 0;
150         }
151
152         if (imxsd->num_sink_pads >= num_pads)
153                 return -EINVAL;
154
155         imxsd->num_src_pads = num_pads - imxsd->num_sink_pads;
156
157         dev_dbg(imxmd->md.dev, "%s: %s has %d pads (%d sink, %d src)\n",
158                 __func__, sd_np->name, num_pads,
159                 imxsd->num_sink_pads, imxsd->num_src_pads);
160
161         for (i = 0; i < num_pads; i++) {
162                 struct device_node *epnode = NULL, *port, *remote_np;
163                 struct imx_media_subdev *remote_imxsd;
164                 struct imx_media_pad *pad;
165                 int remote_pad;
166
167                 /* init this pad */
168                 pad = &imxsd->pad[i];
169                 pad->pad.flags = (i < imxsd->num_sink_pads) ?
170                         MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE;
171
172                 if (is_csi_port)
173                         port = (i < imxsd->num_sink_pads) ? sd_np : NULL;
174                 else
175                         port = of_graph_get_port_by_id(sd_np, i);
176                 if (!port)
177                         continue;
178
179                 for_each_child_of_node(port, epnode) {
180                         of_get_remote_pad(epnode, &remote_np, &remote_pad);
181                         if (!remote_np)
182                                 continue;
183
184                         ret = of_add_pad_link(imxmd, pad, sd_np, remote_np,
185                                               i, remote_pad);
186                         if (ret)
187                                 break;
188
189                         if (i < imxsd->num_sink_pads) {
190                                 /* follow sink endpoints upstream */
191                                 ret = of_parse_subdev(imxmd, remote_np,
192                                                       false, &remote_imxsd);
193                                 if (ret)
194                                         break;
195                         }
196
197                         of_node_put(remote_np);
198                 }
199
200                 if (port != sd_np)
201                         of_node_put(port);
202                 if (ret) {
203                         of_node_put(remote_np);
204                         of_node_put(epnode);
205                         break;
206                 }
207         }
208
209         return ret;
210 }
211
212 int imx_media_of_parse(struct imx_media_dev *imxmd,
213                        struct imx_media_subdev *(*csi)[4],
214                        struct device_node *np)
215 {
216         struct imx_media_subdev *lcsi;
217         struct device_node *csi_np;
218         u32 ipu_id, csi_id;
219         int i, ret;
220
221         for (i = 0; ; i++) {
222                 csi_np = of_parse_phandle(np, "ports", i);
223                 if (!csi_np)
224                         break;
225
226                 ret = of_parse_subdev(imxmd, csi_np, true, &lcsi);
227                 if (ret)
228                         goto err_put;
229
230                 ret = of_property_read_u32(csi_np, "reg", &csi_id);
231                 if (ret) {
232                         dev_err(imxmd->md.dev,
233                                 "%s: csi port missing reg property!\n",
234                                 __func__);
235                         goto err_put;
236                 }
237
238                 ipu_id = of_alias_get_id(csi_np->parent, "ipu");
239                 of_node_put(csi_np);
240
241                 if (ipu_id > 1 || csi_id > 1) {
242                         dev_err(imxmd->md.dev,
243                                 "%s: invalid ipu/csi id (%u/%u)\n",
244                                 __func__, ipu_id, csi_id);
245                         return -EINVAL;
246                 }
247
248                 (*csi)[ipu_id * 2 + csi_id] = lcsi;
249         }
250
251         return 0;
252 err_put:
253         of_node_put(csi_np);
254         return ret;
255 }