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