summaryrefslogtreecommitdiff
path: root/drivers/staging/media/imx/imx-media-of.c
blob: 12df09f524901e554fc13e2fab6972005de6a0c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * Media driver for Freescale i.MX5/6 SOC
 *
 * Open Firmware parsing.
 *
 * Copyright (c) 2016 Mentor Graphics Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */
#include <linux/of_platform.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <media/v4l2-fwnode.h>
#include <media/v4l2-subdev.h>
#include <media/videobuf2-dma-contig.h>
#include <linux/of_graph.h>
#include <video/imx-ipu-v3.h>
#include "imx-media.h"

static int of_add_pad_link(struct imx_media_dev *imxmd,
			   struct imx_media_pad *pad,
			   struct device_node *local_sd_node,
			   struct device_node *remote_sd_node,
			   int local_pad, int remote_pad)
{
	dev_dbg(imxmd->md.dev, "%s: adding %s:%d -> %s:%d\n", __func__,
		local_sd_node->name, local_pad,
		remote_sd_node->name, remote_pad);

	return imx_media_add_pad_link(imxmd, pad, remote_sd_node, NULL,
				      local_pad, remote_pad);
}

static void of_parse_sensor(struct imx_media_dev *imxmd,
			    struct imx_media_subdev *sensor,
			    struct device_node *sensor_np)
{
	struct device_node *endpoint;

	endpoint = of_graph_get_next_endpoint(sensor_np, NULL);
	if (endpoint) {
		v4l2_fwnode_endpoint_parse(of_fwnode_handle(endpoint),
					   &sensor->sensor_ep);
		of_node_put(endpoint);
	}
}

static int of_get_port_count(const struct device_node *np)
{
	struct device_node *ports, *child;
	int num = 0;

	/* check if this node has a ports subnode */
	ports = of_get_child_by_name(np, "ports");
	if (ports)
		np = ports;

	for_each_child_of_node(np, child)
		if (of_node_cmp(child->name, "port") == 0)
			num++;

	of_node_put(ports);
	return num;
}

/*
 * find the remote device node and remote port id (remote pad #)
 * given local endpoint node
 */
static void of_get_remote_pad(struct device_node *epnode,
			      struct device_node **remote_node,
			      int *remote_pad)
{
	struct device_node *rp, *rpp;
	struct device_node *remote;

	rp = of_graph_get_remote_port(epnode);
	rpp = of_graph_get_remote_port_parent(epnode);

	if (of_device_is_compatible(rpp, "fsl,imx6q-ipu")) {
		/* the remote is one of the CSI ports */
		remote = rp;
		*remote_pad = 0;
		of_node_put(rpp);
	} else {
		remote = rpp;
		if (of_property_read_u32(rp, "reg", remote_pad))
			*remote_pad = 0;
		of_node_put(rp);
	}

	if (!of_device_is_available(remote)) {
		of_node_put(remote);
		*remote_node = NULL;
	} else {
		*remote_node = remote;
	}
}

static int
of_parse_subdev(struct imx_media_dev *imxmd, struct device_node *sd_np,
		bool is_csi_port, struct imx_media_subdev **subdev)
{
	struct imx_media_subdev *imxsd;
	int i, num_pads, ret;

	if (!of_device_is_available(sd_np)) {
		dev_dbg(imxmd->md.dev, "%s: %s not enabled\n", __func__,
			sd_np->name);
		*subdev = NULL;
		/* unavailable is not an error */
		return 0;
	}

	/* register this subdev with async notifier */
	imxsd = imx_media_add_async_subdev(imxmd, sd_np, NULL);
	ret = PTR_ERR_OR_ZERO(imxsd);
	if (ret) {
		if (ret == -EEXIST) {
			/* already added, everything is fine */
			*subdev = NULL;
			return 0;
		}

		/* other error, can't continue */
		return ret;
	}
	*subdev = imxsd;

	if (is_csi_port) {
		/*
		 * the ipu-csi has one sink port and two source ports.
		 * The source ports are not represented in the device tree,
		 * but are described by the internal pads and links later.
		 */
		num_pads = CSI_NUM_PADS;
		imxsd->num_sink_pads = CSI_NUM_SINK_PADS;
	} else if (of_device_is_compatible(sd_np, "fsl,imx6-mipi-csi2")) {
		num_pads = of_get_port_count(sd_np);
		/* the mipi csi2 receiver has only one sink port */
		imxsd->num_sink_pads = 1;
	} else if (of_device_is_compatible(sd_np, "video-mux")) {
		num_pads = of_get_port_count(sd_np);
		/* for the video mux, all but the last port are sinks */
		imxsd->num_sink_pads = num_pads - 1;
	} else {
		num_pads = of_get_port_count(sd_np);
		if (num_pads != 1) {
			/* confused, but no reason to give up here */
			dev_warn(imxmd->md.dev,
				 "%s: unknown device %s with %d ports\n",
				 __func__, sd_np->name, num_pads);
			return 0;
		}

		/*
		 * we got to this node from this single source port,
		 * there are no sink pads.
		 */
		imxsd->num_sink_pads = 0;
	}

	if (imxsd->num_sink_pads >= num_pads)
		return -EINVAL;

	imxsd->num_src_pads = num_pads - imxsd->num_sink_pads;

	dev_dbg(imxmd->md.dev, "%s: %s has %d pads (%d sink, %d src)\n",
		__func__, sd_np->name, num_pads,
		imxsd->num_sink_pads, imxsd->num_src_pads);

	/*
	 * With no sink, this subdev node is the original source
	 * of video, parse it's media bus for use by the pipeline.
	 */
	if (imxsd->num_sink_pads == 0)
		of_parse_sensor(imxmd, imxsd, sd_np);

	for (i = 0; i < num_pads; i++) {
		struct device_node *epnode = NULL, *port, *remote_np;
		struct imx_media_subdev *remote_imxsd;
		struct imx_media_pad *pad;
		int remote_pad;

		/* init this pad */
		pad = &imxsd->pad[i];
		pad->pad.flags = (i < imxsd->num_sink_pads) ?
			MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE;

		if (is_csi_port)
			port = (i < imxsd->num_sink_pads) ? sd_np : NULL;
		else
			port = of_graph_get_port_by_id(sd_np, i);
		if (!port)
			continue;

		for_each_child_of_node(port, epnode) {
			of_get_remote_pad(epnode, &remote_np, &remote_pad);
			if (!remote_np)
				continue;

			ret = of_add_pad_link(imxmd, pad, sd_np, remote_np,
					      i, remote_pad);
			if (ret)
				break;

			if (i < imxsd->num_sink_pads) {
				/* follow sink endpoints upstream */
				ret = of_parse_subdev(imxmd, remote_np,
						      false, &remote_imxsd);
				if (ret)
					break;
			}

			of_node_put(remote_np);
		}

		if (port != sd_np)
			of_node_put(port);
		if (ret) {
			of_node_put(remote_np);
			of_node_put(epnode);
			break;
		}
	}

	return ret;
}

int imx_media_of_parse(struct imx_media_dev *imxmd,
		       struct imx_media_subdev *(*csi)[4],
		       struct device_node *np)
{
	struct imx_media_subdev *lcsi;
	struct device_node *csi_np;
	u32 ipu_id, csi_id;
	int i, ret;

	for (i = 0; ; i++) {
		csi_np = of_parse_phandle(np, "ports", i);
		if (!csi_np)
			break;

		ret = of_parse_subdev(imxmd, csi_np, true, &lcsi);
		if (ret)
			goto err_put;

		ret = of_property_read_u32(csi_np, "reg", &csi_id);
		if (ret) {
			dev_err(imxmd->md.dev,
				"%s: csi port missing reg property!\n",
				__func__);
			goto err_put;
		}

		ipu_id = of_alias_get_id(csi_np->parent, "ipu");
		of_node_put(csi_np);

		if (ipu_id > 1 || csi_id > 1) {
			dev_err(imxmd->md.dev,
				"%s: invalid ipu/csi id (%u/%u)\n",
				__func__, ipu_id, csi_id);
			return -EINVAL;
		}

		(*csi)[ipu_id * 2 + csi_id] = lcsi;
	}

	return 0;
err_put:
	of_node_put(csi_np);
	return ret;
}