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_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)
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);
33 return imx_media_add_pad_link(imxmd, pad, remote_sd_node, NULL,
34 local_pad, remote_pad);
37 static int of_get_port_count(const struct device_node *np)
39 struct device_node *ports, *child;
42 /* check if this node has a ports subnode */
43 ports = of_get_child_by_name(np, "ports");
47 for_each_child_of_node(np, child)
48 if (of_node_cmp(child->name, "port") == 0)
56 * find the remote device node and remote port id (remote pad #)
57 * given local endpoint node
59 static void of_get_remote_pad(struct device_node *epnode,
60 struct device_node **remote_node,
63 struct device_node *rp, *rpp;
64 struct device_node *remote;
66 rp = of_graph_get_remote_port(epnode);
67 rpp = of_graph_get_remote_port_parent(epnode);
69 if (of_device_is_compatible(rpp, "fsl,imx6q-ipu")) {
70 /* the remote is one of the CSI ports */
76 if (of_property_read_u32(rp, "reg", remote_pad))
81 if (!of_device_is_available(remote)) {
85 *remote_node = remote;
90 of_parse_subdev(struct imx_media_dev *imxmd, struct device_node *sd_np,
91 bool is_csi_port, struct imx_media_subdev **subdev)
93 struct imx_media_subdev *imxsd;
96 if (!of_device_is_available(sd_np)) {
97 dev_dbg(imxmd->md.dev, "%s: %s not enabled\n", __func__,
100 /* unavailable is not an error */
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);
108 if (ret == -EEXIST) {
109 /* already added, everything is fine */
114 /* other error, can't continue */
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.
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;
136 num_pads = of_get_port_count(sd_np);
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);
146 * we got to this node from this single source port,
147 * there are no sink pads.
149 imxsd->num_sink_pads = 0;
152 if (imxsd->num_sink_pads >= num_pads)
155 imxsd->num_src_pads = num_pads - imxsd->num_sink_pads;
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);
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;
168 pad = &imxsd->pad[i];
169 pad->pad.flags = (i < imxsd->num_sink_pads) ?
170 MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE;
173 port = (i < imxsd->num_sink_pads) ? sd_np : NULL;
175 port = of_graph_get_port_by_id(sd_np, i);
179 for_each_child_of_node(port, epnode) {
180 of_get_remote_pad(epnode, &remote_np, &remote_pad);
184 ret = of_add_pad_link(imxmd, pad, sd_np, remote_np,
189 if (i < imxsd->num_sink_pads) {
190 /* follow sink endpoints upstream */
191 ret = of_parse_subdev(imxmd, remote_np,
192 false, &remote_imxsd);
197 of_node_put(remote_np);
203 of_node_put(remote_np);
212 int imx_media_of_parse(struct imx_media_dev *imxmd,
213 struct imx_media_subdev *(*csi)[4],
214 struct device_node *np)
216 struct imx_media_subdev *lcsi;
217 struct device_node *csi_np;
222 csi_np = of_parse_phandle(np, "ports", i);
226 ret = of_parse_subdev(imxmd, csi_np, true, &lcsi);
230 ret = of_property_read_u32(csi_np, "reg", &csi_id);
232 dev_err(imxmd->md.dev,
233 "%s: csi port missing reg property!\n",
238 ipu_id = of_alias_get_id(csi_np->parent, "ipu");
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);
248 (*csi)[ipu_id * 2 + csi_id] = lcsi;