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 bool of_get_remote(struct device_node *epnode,
45 struct device_node **remote_node)
47 struct device_node *rp, *rpp;
48 struct device_node *remote;
51 rp = of_graph_get_remote_port(epnode);
52 rpp = of_graph_get_remote_port_parent(epnode);
54 if (of_device_is_compatible(rpp, "fsl,imx6q-ipu")) {
55 /* the remote is one of the CSI ports */
65 if (!of_device_is_available(remote)) {
69 *remote_node = remote;
76 of_parse_subdev(struct imx_media_dev *imxmd, struct device_node *sd_np,
79 struct imx_media_subdev *imxsd;
80 int i, num_ports, ret;
82 if (!of_device_is_available(sd_np)) {
83 dev_dbg(imxmd->md.dev, "%s: %s not enabled\n", __func__,
85 /* unavailable is not an error */
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);
94 /* already added, everything is fine */
98 /* other error, can't continue */
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.
107 num_ports = is_csi_port ? 1 : of_get_port_count(sd_np);
109 for (i = 0; i < num_ports; i++) {
110 struct device_node *epnode = NULL, *port, *remote_np;
112 port = is_csi_port ? sd_np : of_graph_get_port_by_id(sd_np, i);
116 for_each_child_of_node(port, epnode) {
119 remote_is_csi = of_get_remote(epnode, &remote_np);
123 ret = of_parse_subdev(imxmd, remote_np, remote_is_csi);
124 of_node_put(remote_np);
140 int imx_media_add_of_subdevs(struct imx_media_dev *imxmd,
141 struct device_node *np)
143 struct device_node *csi_np;
147 csi_np = of_parse_phandle(np, "ports", i);
151 ret = of_parse_subdev(imxmd, csi_np, true);
161 * Create a single media link to/from imxsd using a fwnode link.
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.
167 static int create_of_link(struct imx_media_dev *imxmd,
168 struct imx_media_subdev *imxsd,
169 struct v4l2_fwnode_link *link)
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;
176 if (link->local_port >= sd->entity.num_pads)
179 remote = imx_media_find_async_subdev(imxmd,
180 to_of_node(link->remote_node),
185 if (sd->entity.pads[link->local_port].flags & MEDIA_PAD_FL_SINK) {
187 src_pad = link->remote_port;
189 sink_pad = link->local_port;
192 src_pad = link->local_port;
194 sink_pad = link->remote_port;
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]))
202 v4l2_info(sd->v4l2_dev, "%s:%d -> %s:%d\n",
203 src->name, src_pad, sink->name, sink_pad);
205 return media_create_pad_link(&src->entity, src_pad,
206 &sink->entity, sink_pad, 0);
210 * Create media links to/from imxsd using its device-tree endpoints.
212 int imx_media_create_of_links(struct imx_media_dev *imxmd,
213 struct imx_media_subdev *imxsd)
215 struct v4l2_subdev *sd = imxsd->sd;
216 struct v4l2_fwnode_link link;
217 struct device_node *ep;
220 for_each_endpoint_of_node(sd->dev->of_node, ep) {
221 ret = v4l2_fwnode_parse_link(of_fwnode_handle(ep), &link);
225 ret = create_of_link(imxmd, imxsd, &link);
226 v4l2_fwnode_put_link(&link);
235 * Create media links to the given CSI subdevice's sink pads,
236 * using its device-tree endpoints.
238 int imx_media_create_csi_of_links(struct imx_media_dev *imxmd,
239 struct imx_media_subdev *csi)
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;
247 link.local_node = of_fwnode_handle(csi_np);
248 link.local_port = CSI_SINK_PAD;
250 for_each_child_of_node(csi_np, ep) {
251 csi_ep = of_fwnode_handle(ep);
253 fwnode = fwnode_graph_get_remote_endpoint(csi_ep);
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;
265 ret = create_of_link(imxmd, csi, &link);
266 fwnode_handle_put(link.remote_node);