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 int i, num_ports, ret;
81 if (!of_device_is_available(sd_np)) {
82 dev_dbg(imxmd->md.dev, "%s: %s not enabled\n", __func__,
84 /* unavailable is not an error */
88 /* register this subdev with async notifier */
89 ret = imx_media_add_async_subdev(imxmd, of_fwnode_handle(sd_np),
93 /* already added, everything is fine */
97 /* other error, can't continue */
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.
106 num_ports = is_csi_port ? 1 : of_get_port_count(sd_np);
108 for (i = 0; i < num_ports; i++) {
109 struct device_node *epnode = NULL, *port, *remote_np;
111 port = is_csi_port ? sd_np : of_graph_get_port_by_id(sd_np, i);
115 for_each_child_of_node(port, epnode) {
118 remote_is_csi = of_get_remote(epnode, &remote_np);
122 ret = of_parse_subdev(imxmd, remote_np, remote_is_csi);
123 of_node_put(remote_np);
139 int imx_media_add_of_subdevs(struct imx_media_dev *imxmd,
140 struct device_node *np)
142 struct device_node *csi_np;
146 csi_np = of_parse_phandle(np, "ports", i);
150 ret = of_parse_subdev(imxmd, csi_np, true);
160 * Create a single media link to/from sd using a fwnode link.
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.
166 static int create_of_link(struct imx_media_dev *imxmd,
167 struct v4l2_subdev *sd,
168 struct v4l2_fwnode_link *link)
170 struct v4l2_subdev *remote, *src, *sink;
171 int src_pad, sink_pad;
173 if (link->local_port >= sd->entity.num_pads)
176 remote = imx_media_find_subdev_by_fwnode(imxmd, link->remote_node);
180 if (sd->entity.pads[link->local_port].flags & MEDIA_PAD_FL_SINK) {
182 src_pad = link->remote_port;
184 sink_pad = link->local_port;
187 src_pad = link->local_port;
189 sink_pad = link->remote_port;
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]))
197 v4l2_info(sd->v4l2_dev, "%s:%d -> %s:%d\n",
198 src->name, src_pad, sink->name, sink_pad);
200 return media_create_pad_link(&src->entity, src_pad,
201 &sink->entity, sink_pad, 0);
205 * Create media links to/from sd using its device-tree endpoints.
207 int imx_media_create_of_links(struct imx_media_dev *imxmd,
208 struct v4l2_subdev *sd)
210 struct v4l2_fwnode_link link;
211 struct device_node *ep;
214 for_each_endpoint_of_node(sd->dev->of_node, ep) {
215 ret = v4l2_fwnode_parse_link(of_fwnode_handle(ep), &link);
219 ret = create_of_link(imxmd, sd, &link);
220 v4l2_fwnode_put_link(&link);
229 * Create media links to the given CSI subdevice's sink pads,
230 * using its device-tree endpoints.
232 int imx_media_create_csi_of_links(struct imx_media_dev *imxmd,
233 struct v4l2_subdev *csi)
235 struct device_node *csi_np = csi->dev->of_node;
236 struct device_node *ep;
238 for_each_child_of_node(csi_np, ep) {
239 struct fwnode_handle *fwnode, *csi_ep;
240 struct v4l2_fwnode_link link;
243 memset(&link, 0, sizeof(link));
245 link.local_node = of_fwnode_handle(csi_np);
246 link.local_port = CSI_SINK_PAD;
248 csi_ep = of_fwnode_handle(ep);
250 fwnode = fwnode_graph_get_remote_endpoint(csi_ep);
254 fwnode = fwnode_get_parent(fwnode);
255 fwnode_property_read_u32(fwnode, "reg", &link.remote_port);
256 fwnode = fwnode_get_next_parent(fwnode);
257 if (is_of_node(fwnode) &&
258 of_node_cmp(to_of_node(fwnode)->name, "ports") == 0)
259 fwnode = fwnode_get_next_parent(fwnode);
260 link.remote_node = fwnode;
262 ret = create_of_link(imxmd, csi, &link);
263 fwnode_handle_put(link.remote_node);