2 * Media driver for Freescale i.MX5/6 SOC
4 * Open Firmware parsing.
6 * Copyright (c) 2016 Mentor Graphics Inc.
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.
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"
23 static int of_get_port_count(const struct device_node *np)
25 struct device_node *ports, *child;
28 /* check if this node has a ports subnode */
29 ports = of_get_child_by_name(np, "ports");
33 for_each_child_of_node(np, child)
34 if (of_node_cmp(child->name, "port") == 0)
42 * find the remote device node given local endpoint node
44 static void of_get_remote(struct device_node *epnode,
45 struct device_node **remote_node)
47 struct device_node *rp, *rpp;
48 struct device_node *remote;
50 rp = of_graph_get_remote_port(epnode);
51 rpp = of_graph_get_remote_port_parent(epnode);
53 if (of_device_is_compatible(rpp, "fsl,imx6q-ipu")) {
54 /* the remote is one of the CSI ports */
62 if (!of_device_is_available(remote)) {
66 *remote_node = remote;
71 of_parse_subdev(struct imx_media_dev *imxmd, struct device_node *sd_np,
74 struct imx_media_subdev *imxsd;
77 if (!of_device_is_available(sd_np)) {
78 dev_dbg(imxmd->md.dev, "%s: %s not enabled\n", __func__,
80 /* unavailable is not an error */
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);
89 /* already added, everything is fine */
93 /* other error, can't continue */
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.
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;
114 num_pads = of_get_port_count(sd_np);
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);
124 * we got to this node from this single source port,
125 * there are no sink pads.
127 imxsd->num_sink_pads = 0;
130 if (imxsd->num_sink_pads >= num_pads)
133 imxsd->num_src_pads = num_pads - imxsd->num_sink_pads;
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);
139 for (i = 0; i < num_pads; i++) {
140 struct device_node *epnode = NULL, *port, *remote_np;
143 port = (i < imxsd->num_sink_pads) ? sd_np : NULL;
145 port = of_graph_get_port_by_id(sd_np, i);
149 for_each_child_of_node(port, epnode) {
150 of_get_remote(epnode, &remote_np);
154 if (i < imxsd->num_sink_pads) {
155 /* follow sink endpoints upstream */
156 ret = of_parse_subdev(imxmd, remote_np, false);
161 of_node_put(remote_np);
167 of_node_put(remote_np);
176 int imx_media_add_of_subdevs(struct imx_media_dev *imxmd,
177 struct device_node *np)
179 struct device_node *csi_np;
183 csi_np = of_parse_phandle(np, "ports", i);
187 ret = of_parse_subdev(imxmd, csi_np, true);
197 * Create a single media link to/from imxsd using a fwnode link.
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.
203 static int create_of_link(struct imx_media_dev *imxmd,
204 struct imx_media_subdev *imxsd,
205 struct v4l2_fwnode_link *link)
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;
212 if (link->local_port >= sd->entity.num_pads)
215 remote = imx_media_find_async_subdev(imxmd,
216 to_of_node(link->remote_node),
221 if (sd->entity.pads[link->local_port].flags & MEDIA_PAD_FL_SINK) {
223 src_pad = link->remote_port;
225 sink_pad = link->local_port;
228 src_pad = link->local_port;
230 sink_pad = link->remote_port;
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]))
238 v4l2_info(sd->v4l2_dev, "%s:%d -> %s:%d\n",
239 src->name, src_pad, sink->name, sink_pad);
241 return media_create_pad_link(&src->entity, src_pad,
242 &sink->entity, sink_pad, 0);
246 * Create media links to/from imxsd using its device-tree endpoints.
248 int imx_media_create_of_links(struct imx_media_dev *imxmd,
249 struct imx_media_subdev *imxsd)
251 struct v4l2_subdev *sd = imxsd->sd;
252 struct v4l2_fwnode_link link;
253 struct device_node *ep;
256 for_each_endpoint_of_node(sd->dev->of_node, ep) {
257 ret = v4l2_fwnode_parse_link(of_fwnode_handle(ep), &link);
261 ret = create_of_link(imxmd, imxsd, &link);
262 v4l2_fwnode_put_link(&link);
271 * Create media links to the given CSI subdevice's sink pads,
272 * using its device-tree endpoints.
274 int imx_media_create_csi_of_links(struct imx_media_dev *imxmd,
275 struct imx_media_subdev *csi)
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;
283 link.local_node = of_fwnode_handle(csi_np);
284 link.local_port = CSI_SINK_PAD;
286 for_each_child_of_node(csi_np, ep) {
287 csi_ep = of_fwnode_handle(ep);
289 fwnode = fwnode_graph_get_remote_endpoint(csi_ep);
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;
301 ret = create_of_link(imxmd, csi, &link);
302 fwnode_handle_put(link.remote_node);