xref: /linux/drivers/staging/media/tegra-video/vi.c (revision 84b9b44b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2020 NVIDIA CORPORATION.  All rights reserved.
4  */
5 
6 #include <linux/bitmap.h>
7 #include <linux/clk.h>
8 #include <linux/delay.h>
9 #include <linux/host1x.h>
10 #include <linux/lcm.h>
11 #include <linux/list.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 #include <linux/of_device.h>
15 #include <linux/of_graph.h>
16 #include <linux/platform_device.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/slab.h>
20 
21 #include <media/v4l2-dv-timings.h>
22 #include <media/v4l2-event.h>
23 #include <media/v4l2-fh.h>
24 #include <media/v4l2-fwnode.h>
25 #include <media/v4l2-ioctl.h>
26 #include <media/videobuf2-dma-contig.h>
27 
28 #include <soc/tegra/pmc.h>
29 
30 #include "vi.h"
31 #include "video.h"
32 
33 #define MAX_CID_CONTROLS		1
34 
35 static const struct tegra_video_format tegra_default_format = {
36 	.img_dt = TEGRA_IMAGE_DT_RAW10,
37 	.bit_width = 10,
38 	.code = MEDIA_BUS_FMT_SRGGB10_1X10,
39 	.bpp = 2,
40 	.img_fmt = TEGRA_IMAGE_FORMAT_DEF,
41 	.fourcc = V4L2_PIX_FMT_SRGGB10,
42 };
43 
44 static inline struct tegra_vi *
45 host1x_client_to_vi(struct host1x_client *client)
46 {
47 	return container_of(client, struct tegra_vi, client);
48 }
49 
50 static inline struct tegra_channel_buffer *
51 to_tegra_channel_buffer(struct vb2_v4l2_buffer *vb)
52 {
53 	return container_of(vb, struct tegra_channel_buffer, buf);
54 }
55 
56 static inline struct tegra_vi_graph_entity *
57 to_tegra_vi_graph_entity(struct v4l2_async_subdev *asd)
58 {
59 	return container_of(asd, struct tegra_vi_graph_entity, asd);
60 }
61 
62 static int tegra_get_format_idx_by_code(struct tegra_vi *vi,
63 					unsigned int code,
64 					unsigned int offset)
65 {
66 	unsigned int i;
67 
68 	for (i = offset; i < vi->soc->nformats; ++i) {
69 		if (vi->soc->video_formats[i].code == code)
70 			return i;
71 	}
72 
73 	return -1;
74 }
75 
76 static u32 tegra_get_format_fourcc_by_idx(struct tegra_vi *vi,
77 					  unsigned int index)
78 {
79 	if (index >= vi->soc->nformats)
80 		return -EINVAL;
81 
82 	return vi->soc->video_formats[index].fourcc;
83 }
84 
85 static const struct tegra_video_format *
86 tegra_get_format_by_fourcc(struct tegra_vi *vi, u32 fourcc)
87 {
88 	unsigned int i;
89 
90 	for (i = 0; i < vi->soc->nformats; ++i) {
91 		if (vi->soc->video_formats[i].fourcc == fourcc)
92 			return &vi->soc->video_formats[i];
93 	}
94 
95 	return NULL;
96 }
97 
98 /*
99  * videobuf2 queue operations
100  */
101 static int tegra_channel_queue_setup(struct vb2_queue *vq,
102 				     unsigned int *nbuffers,
103 				     unsigned int *nplanes,
104 				     unsigned int sizes[],
105 				     struct device *alloc_devs[])
106 {
107 	struct tegra_vi_channel *chan = vb2_get_drv_priv(vq);
108 
109 	if (*nplanes)
110 		return sizes[0] < chan->format.sizeimage ? -EINVAL : 0;
111 
112 	*nplanes = 1;
113 	sizes[0] = chan->format.sizeimage;
114 	alloc_devs[0] = chan->vi->dev;
115 
116 	return 0;
117 }
118 
119 static int tegra_channel_buffer_prepare(struct vb2_buffer *vb)
120 {
121 	struct tegra_vi_channel *chan = vb2_get_drv_priv(vb->vb2_queue);
122 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
123 	struct tegra_channel_buffer *buf = to_tegra_channel_buffer(vbuf);
124 	unsigned long size = chan->format.sizeimage;
125 
126 	if (vb2_plane_size(vb, 0) < size) {
127 		v4l2_err(chan->video.v4l2_dev,
128 			 "buffer too small (%lu < %lu)\n",
129 			 vb2_plane_size(vb, 0), size);
130 		return -EINVAL;
131 	}
132 
133 	vb2_set_plane_payload(vb, 0, size);
134 	buf->chan = chan;
135 	buf->addr = vb2_dma_contig_plane_dma_addr(vb, 0);
136 
137 	return 0;
138 }
139 
140 static void tegra_channel_buffer_queue(struct vb2_buffer *vb)
141 {
142 	struct tegra_vi_channel *chan = vb2_get_drv_priv(vb->vb2_queue);
143 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
144 	struct tegra_channel_buffer *buf = to_tegra_channel_buffer(vbuf);
145 
146 	/* put buffer into the capture queue */
147 	spin_lock(&chan->start_lock);
148 	list_add_tail(&buf->queue, &chan->capture);
149 	spin_unlock(&chan->start_lock);
150 
151 	/* wait up kthread for capture */
152 	wake_up_interruptible(&chan->start_wait);
153 }
154 
155 struct v4l2_subdev *
156 tegra_channel_get_remote_csi_subdev(struct tegra_vi_channel *chan)
157 {
158 	struct media_pad *pad;
159 
160 	pad = media_pad_remote_pad_first(&chan->pad);
161 	if (!pad)
162 		return NULL;
163 
164 	return media_entity_to_v4l2_subdev(pad->entity);
165 }
166 
167 struct v4l2_subdev *
168 tegra_channel_get_remote_source_subdev(struct tegra_vi_channel *chan)
169 {
170 	struct media_pad *pad;
171 	struct v4l2_subdev *subdev;
172 	struct media_entity *entity;
173 
174 	subdev = tegra_channel_get_remote_csi_subdev(chan);
175 	if (!subdev)
176 		return NULL;
177 
178 	pad = &subdev->entity.pads[0];
179 	while (!(pad->flags & MEDIA_PAD_FL_SOURCE)) {
180 		pad = media_pad_remote_pad_first(pad);
181 		if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
182 			break;
183 		entity = pad->entity;
184 		pad = &entity->pads[0];
185 		subdev = media_entity_to_v4l2_subdev(entity);
186 	}
187 
188 	return subdev;
189 }
190 
191 static int tegra_channel_enable_stream(struct tegra_vi_channel *chan)
192 {
193 	struct v4l2_subdev *csi_subdev, *src_subdev;
194 	struct tegra_csi_channel *csi_chan;
195 	int ret, err;
196 
197 	/*
198 	 * Tegra CSI receiver can detect the first LP to HS transition.
199 	 * So, start the CSI stream-on prior to sensor stream-on and
200 	 * vice-versa for stream-off.
201 	 */
202 	csi_subdev = tegra_channel_get_remote_csi_subdev(chan);
203 	ret = v4l2_subdev_call(csi_subdev, video, s_stream, true);
204 	if (ret < 0 && ret != -ENOIOCTLCMD)
205 		return ret;
206 
207 	if (IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
208 		return 0;
209 
210 	csi_chan = v4l2_get_subdevdata(csi_subdev);
211 	/*
212 	 * TRM has incorrectly documented to wait for done status from
213 	 * calibration logic after CSI interface power on.
214 	 * As per the design, calibration results are latched and applied
215 	 * to the pads only when the link is in LP11 state which will happen
216 	 * during the sensor stream-on.
217 	 * CSI subdev stream-on triggers start of MIPI pads calibration.
218 	 * Wait for calibration to finish here after sensor subdev stream-on.
219 	 */
220 	src_subdev = tegra_channel_get_remote_source_subdev(chan);
221 	ret = v4l2_subdev_call(src_subdev, video, s_stream, true);
222 	err = tegra_mipi_finish_calibration(csi_chan->mipi);
223 
224 	if (ret < 0 && ret != -ENOIOCTLCMD)
225 		goto err_disable_csi_stream;
226 
227 	if (err < 0)
228 		dev_warn(csi_chan->csi->dev,
229 			 "MIPI calibration failed: %d\n", err);
230 
231 	return 0;
232 
233 err_disable_csi_stream:
234 	v4l2_subdev_call(csi_subdev, video, s_stream, false);
235 	return ret;
236 }
237 
238 static int tegra_channel_disable_stream(struct tegra_vi_channel *chan)
239 {
240 	struct v4l2_subdev *subdev;
241 	int ret;
242 
243 	/*
244 	 * Stream-off subdevices in reverse order to stream-on.
245 	 * Remote source subdev in TPG mode is same as CSI subdev.
246 	 */
247 	subdev = tegra_channel_get_remote_source_subdev(chan);
248 	ret = v4l2_subdev_call(subdev, video, s_stream, false);
249 	if (ret < 0 && ret != -ENOIOCTLCMD)
250 		return ret;
251 
252 	if (IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
253 		return 0;
254 
255 	subdev = tegra_channel_get_remote_csi_subdev(chan);
256 	ret = v4l2_subdev_call(subdev, video, s_stream, false);
257 	if (ret < 0 && ret != -ENOIOCTLCMD)
258 		return ret;
259 
260 	return 0;
261 }
262 
263 int tegra_channel_set_stream(struct tegra_vi_channel *chan, bool on)
264 {
265 	int ret;
266 
267 	if (on)
268 		ret = tegra_channel_enable_stream(chan);
269 	else
270 		ret = tegra_channel_disable_stream(chan);
271 
272 	return ret;
273 }
274 
275 void tegra_channel_release_buffers(struct tegra_vi_channel *chan,
276 				   enum vb2_buffer_state state)
277 {
278 	struct tegra_channel_buffer *buf, *nbuf;
279 
280 	spin_lock(&chan->start_lock);
281 	list_for_each_entry_safe(buf, nbuf, &chan->capture, queue) {
282 		vb2_buffer_done(&buf->buf.vb2_buf, state);
283 		list_del(&buf->queue);
284 	}
285 	spin_unlock(&chan->start_lock);
286 
287 	spin_lock(&chan->done_lock);
288 	list_for_each_entry_safe(buf, nbuf, &chan->done, queue) {
289 		vb2_buffer_done(&buf->buf.vb2_buf, state);
290 		list_del(&buf->queue);
291 	}
292 	spin_unlock(&chan->done_lock);
293 }
294 
295 static int tegra_channel_start_streaming(struct vb2_queue *vq, u32 count)
296 {
297 	struct tegra_vi_channel *chan = vb2_get_drv_priv(vq);
298 	int ret;
299 
300 	ret = pm_runtime_resume_and_get(chan->vi->dev);
301 	if (ret < 0) {
302 		dev_err(chan->vi->dev, "failed to get runtime PM: %d\n", ret);
303 		return ret;
304 	}
305 
306 	ret = chan->vi->ops->vi_start_streaming(vq, count);
307 	if (ret < 0)
308 		pm_runtime_put(chan->vi->dev);
309 
310 	return ret;
311 }
312 
313 static void tegra_channel_stop_streaming(struct vb2_queue *vq)
314 {
315 	struct tegra_vi_channel *chan = vb2_get_drv_priv(vq);
316 
317 	chan->vi->ops->vi_stop_streaming(vq);
318 	pm_runtime_put(chan->vi->dev);
319 }
320 
321 static const struct vb2_ops tegra_channel_queue_qops = {
322 	.queue_setup = tegra_channel_queue_setup,
323 	.buf_prepare = tegra_channel_buffer_prepare,
324 	.buf_queue = tegra_channel_buffer_queue,
325 	.wait_prepare = vb2_ops_wait_prepare,
326 	.wait_finish = vb2_ops_wait_finish,
327 	.start_streaming = tegra_channel_start_streaming,
328 	.stop_streaming = tegra_channel_stop_streaming,
329 };
330 
331 /*
332  * V4L2 ioctl operations
333  */
334 static int tegra_channel_querycap(struct file *file, void *fh,
335 				  struct v4l2_capability *cap)
336 {
337 	struct tegra_vi_channel *chan = video_drvdata(file);
338 
339 	strscpy(cap->driver, "tegra-video", sizeof(cap->driver));
340 	strscpy(cap->card, chan->video.name, sizeof(cap->card));
341 	snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
342 		 dev_name(chan->vi->dev));
343 
344 	return 0;
345 }
346 
347 static int tegra_channel_g_parm(struct file *file, void *fh,
348 				struct v4l2_streamparm *a)
349 {
350 	struct tegra_vi_channel *chan = video_drvdata(file);
351 	struct v4l2_subdev *subdev;
352 
353 	subdev = tegra_channel_get_remote_source_subdev(chan);
354 	return v4l2_g_parm_cap(&chan->video, subdev, a);
355 }
356 
357 static int tegra_channel_s_parm(struct file *file, void *fh,
358 				struct v4l2_streamparm *a)
359 {
360 	struct tegra_vi_channel *chan = video_drvdata(file);
361 	struct v4l2_subdev *subdev;
362 
363 	subdev = tegra_channel_get_remote_source_subdev(chan);
364 	return v4l2_s_parm_cap(&chan->video, subdev, a);
365 }
366 
367 static int tegra_channel_enum_framesizes(struct file *file, void *fh,
368 					 struct v4l2_frmsizeenum *sizes)
369 {
370 	int ret;
371 	struct tegra_vi_channel *chan = video_drvdata(file);
372 	struct v4l2_subdev *subdev;
373 	const struct tegra_video_format *fmtinfo;
374 	struct v4l2_subdev_frame_size_enum fse = {
375 		.index = sizes->index,
376 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
377 	};
378 
379 	fmtinfo = tegra_get_format_by_fourcc(chan->vi, sizes->pixel_format);
380 	if (!fmtinfo)
381 		return -EINVAL;
382 
383 	fse.code = fmtinfo->code;
384 
385 	subdev = tegra_channel_get_remote_source_subdev(chan);
386 	ret = v4l2_subdev_call(subdev, pad, enum_frame_size, NULL, &fse);
387 	if (ret)
388 		return ret;
389 
390 	sizes->type = V4L2_FRMSIZE_TYPE_DISCRETE;
391 	sizes->discrete.width = fse.max_width;
392 	sizes->discrete.height = fse.max_height;
393 
394 	return 0;
395 }
396 
397 static int tegra_channel_enum_frameintervals(struct file *file, void *fh,
398 					     struct v4l2_frmivalenum *ivals)
399 {
400 	int ret;
401 	struct tegra_vi_channel *chan = video_drvdata(file);
402 	struct v4l2_subdev *subdev;
403 	const struct tegra_video_format *fmtinfo;
404 	struct v4l2_subdev_frame_interval_enum fie = {
405 		.index = ivals->index,
406 		.width = ivals->width,
407 		.height = ivals->height,
408 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
409 	};
410 
411 	fmtinfo = tegra_get_format_by_fourcc(chan->vi, ivals->pixel_format);
412 	if (!fmtinfo)
413 		return -EINVAL;
414 
415 	fie.code = fmtinfo->code;
416 
417 	subdev = tegra_channel_get_remote_source_subdev(chan);
418 	ret = v4l2_subdev_call(subdev, pad, enum_frame_interval, NULL, &fie);
419 	if (ret)
420 		return ret;
421 
422 	ivals->type = V4L2_FRMIVAL_TYPE_DISCRETE;
423 	ivals->discrete.numerator = fie.interval.numerator;
424 	ivals->discrete.denominator = fie.interval.denominator;
425 
426 	return 0;
427 }
428 
429 static int tegra_channel_enum_format(struct file *file, void *fh,
430 				     struct v4l2_fmtdesc *f)
431 {
432 	struct tegra_vi_channel *chan = video_drvdata(file);
433 	unsigned int index = 0, i;
434 	unsigned long *fmts_bitmap = chan->tpg_fmts_bitmap;
435 
436 	if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
437 		fmts_bitmap = chan->fmts_bitmap;
438 
439 	if (f->index >= bitmap_weight(fmts_bitmap, MAX_FORMAT_NUM))
440 		return -EINVAL;
441 
442 	for (i = 0; i < f->index + 1; i++, index++)
443 		index = find_next_bit(fmts_bitmap, MAX_FORMAT_NUM, index);
444 
445 	f->pixelformat = tegra_get_format_fourcc_by_idx(chan->vi, index - 1);
446 
447 	return 0;
448 }
449 
450 static int tegra_channel_get_format(struct file *file, void *fh,
451 				    struct v4l2_format *format)
452 {
453 	struct tegra_vi_channel *chan = video_drvdata(file);
454 
455 	format->fmt.pix = chan->format;
456 
457 	return 0;
458 }
459 
460 static void tegra_channel_fmt_align(struct tegra_vi_channel *chan,
461 				    struct v4l2_pix_format *pix,
462 				    unsigned int bpp)
463 {
464 	unsigned int min_bpl;
465 	unsigned int max_bpl;
466 	unsigned int bpl;
467 
468 	/*
469 	 * The transfer alignment requirements are expressed in bytes.
470 	 * Clamp the requested width and height to the limits.
471 	 */
472 	pix->width = clamp(pix->width, TEGRA_MIN_WIDTH, TEGRA_MAX_WIDTH);
473 	pix->height = clamp(pix->height, TEGRA_MIN_HEIGHT, TEGRA_MAX_HEIGHT);
474 
475 	/* Clamp the requested bytes per line value. If the maximum bytes per
476 	 * line value is zero, the module doesn't support user configurable
477 	 * line sizes. Override the requested value with the minimum in that
478 	 * case.
479 	 */
480 	min_bpl = pix->width * bpp;
481 	max_bpl = rounddown(TEGRA_MAX_WIDTH, SURFACE_ALIGN_BYTES);
482 	bpl = roundup(pix->bytesperline, SURFACE_ALIGN_BYTES);
483 
484 	pix->bytesperline = clamp(bpl, min_bpl, max_bpl);
485 	pix->sizeimage = pix->bytesperline * pix->height;
486 	if (pix->pixelformat == V4L2_PIX_FMT_NV16)
487 		pix->sizeimage *= 2;
488 }
489 
490 static int __tegra_channel_try_format(struct tegra_vi_channel *chan,
491 				      struct v4l2_pix_format *pix)
492 {
493 	const struct tegra_video_format *fmtinfo;
494 	static struct lock_class_key key;
495 	struct v4l2_subdev *subdev;
496 	struct v4l2_subdev_format fmt = {
497 		.which = V4L2_SUBDEV_FORMAT_TRY,
498 	};
499 	struct v4l2_subdev_state *sd_state;
500 	struct v4l2_subdev_frame_size_enum fse = {
501 		.which = V4L2_SUBDEV_FORMAT_TRY,
502 	};
503 	struct v4l2_subdev_selection sdsel = {
504 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
505 		.target = V4L2_SEL_TGT_CROP_BOUNDS,
506 	};
507 	int ret;
508 
509 	subdev = tegra_channel_get_remote_source_subdev(chan);
510 	if (!subdev)
511 		return -ENODEV;
512 
513 	/*
514 	 * FIXME: Drop this call, drivers are not supposed to use
515 	 * __v4l2_subdev_state_alloc().
516 	 */
517 	sd_state = __v4l2_subdev_state_alloc(subdev, "tegra:state->lock",
518 					     &key);
519 	if (IS_ERR(sd_state))
520 		return PTR_ERR(sd_state);
521 	/*
522 	 * Retrieve the format information and if requested format isn't
523 	 * supported, keep the current format.
524 	 */
525 	fmtinfo = tegra_get_format_by_fourcc(chan->vi, pix->pixelformat);
526 	if (!fmtinfo) {
527 		pix->pixelformat = chan->format.pixelformat;
528 		pix->colorspace = chan->format.colorspace;
529 		fmtinfo = tegra_get_format_by_fourcc(chan->vi,
530 						     pix->pixelformat);
531 	}
532 
533 	pix->field = V4L2_FIELD_NONE;
534 	fmt.pad = 0;
535 	v4l2_fill_mbus_format(&fmt.format, pix, fmtinfo->code);
536 
537 	/*
538 	 * Attempt to obtain the format size from subdev.
539 	 * If not available, try to get crop boundary from subdev.
540 	 */
541 	fse.code = fmtinfo->code;
542 	ret = v4l2_subdev_call(subdev, pad, enum_frame_size, sd_state, &fse);
543 	if (ret) {
544 		if (!v4l2_subdev_has_op(subdev, pad, get_selection)) {
545 			sd_state->pads->try_crop.width = 0;
546 			sd_state->pads->try_crop.height = 0;
547 		} else {
548 			ret = v4l2_subdev_call(subdev, pad, get_selection,
549 					       NULL, &sdsel);
550 			if (ret)
551 				return -EINVAL;
552 
553 			sd_state->pads->try_crop.width = sdsel.r.width;
554 			sd_state->pads->try_crop.height = sdsel.r.height;
555 		}
556 	} else {
557 		sd_state->pads->try_crop.width = fse.max_width;
558 		sd_state->pads->try_crop.height = fse.max_height;
559 	}
560 
561 	ret = v4l2_subdev_call(subdev, pad, set_fmt, sd_state, &fmt);
562 	if (ret < 0)
563 		return ret;
564 
565 	v4l2_fill_pix_format(pix, &fmt.format);
566 	tegra_channel_fmt_align(chan, pix, fmtinfo->bpp);
567 
568 	__v4l2_subdev_state_free(sd_state);
569 
570 	return 0;
571 }
572 
573 static int tegra_channel_try_format(struct file *file, void *fh,
574 				    struct v4l2_format *format)
575 {
576 	struct tegra_vi_channel *chan = video_drvdata(file);
577 
578 	return __tegra_channel_try_format(chan, &format->fmt.pix);
579 }
580 
581 static void tegra_channel_update_gangports(struct tegra_vi_channel *chan)
582 {
583 	if (chan->format.width <= 1920)
584 		chan->numgangports = 1;
585 	else
586 		chan->numgangports = chan->totalports;
587 }
588 
589 static int tegra_channel_set_format(struct file *file, void *fh,
590 				    struct v4l2_format *format)
591 {
592 	struct tegra_vi_channel *chan = video_drvdata(file);
593 	const struct tegra_video_format *fmtinfo;
594 	struct v4l2_subdev_format fmt = {
595 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
596 	};
597 	struct v4l2_subdev *subdev;
598 	struct v4l2_pix_format *pix = &format->fmt.pix;
599 	int ret;
600 
601 	if (vb2_is_busy(&chan->queue))
602 		return -EBUSY;
603 
604 	/* get supported format by try_fmt */
605 	ret = __tegra_channel_try_format(chan, pix);
606 	if (ret)
607 		return ret;
608 
609 	fmtinfo = tegra_get_format_by_fourcc(chan->vi, pix->pixelformat);
610 
611 	fmt.pad = 0;
612 	v4l2_fill_mbus_format(&fmt.format, pix, fmtinfo->code);
613 	subdev = tegra_channel_get_remote_source_subdev(chan);
614 	ret = v4l2_subdev_call(subdev, pad, set_fmt, NULL, &fmt);
615 	if (ret < 0)
616 		return ret;
617 
618 	v4l2_fill_pix_format(pix, &fmt.format);
619 	tegra_channel_fmt_align(chan, pix, fmtinfo->bpp);
620 
621 	chan->format = *pix;
622 	chan->fmtinfo = fmtinfo;
623 	tegra_channel_update_gangports(chan);
624 
625 	return 0;
626 }
627 
628 static int tegra_channel_set_subdev_active_fmt(struct tegra_vi_channel *chan)
629 {
630 	int ret, index;
631 	struct v4l2_subdev *subdev;
632 	struct v4l2_subdev_format fmt = {
633 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
634 	};
635 
636 	/*
637 	 * Initialize channel format to the sub-device active format if there
638 	 * is corresponding match in the Tegra supported video formats.
639 	 */
640 	subdev = tegra_channel_get_remote_source_subdev(chan);
641 	ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
642 	if (ret)
643 		return ret;
644 
645 	index = tegra_get_format_idx_by_code(chan->vi, fmt.format.code, 0);
646 	if (index < 0)
647 		return -EINVAL;
648 
649 	chan->fmtinfo = &chan->vi->soc->video_formats[index];
650 	v4l2_fill_pix_format(&chan->format, &fmt.format);
651 	chan->format.pixelformat = chan->fmtinfo->fourcc;
652 	chan->format.bytesperline = chan->format.width * chan->fmtinfo->bpp;
653 	chan->format.sizeimage = chan->format.bytesperline *
654 				 chan->format.height;
655 	tegra_channel_fmt_align(chan, &chan->format, chan->fmtinfo->bpp);
656 	tegra_channel_update_gangports(chan);
657 
658 	return 0;
659 }
660 
661 static int
662 tegra_channel_subscribe_event(struct v4l2_fh *fh,
663 			      const struct v4l2_event_subscription *sub)
664 {
665 	switch (sub->type) {
666 	case V4L2_EVENT_SOURCE_CHANGE:
667 		return v4l2_event_subscribe(fh, sub, 4, NULL);
668 	}
669 
670 	return v4l2_ctrl_subscribe_event(fh, sub);
671 }
672 
673 static int tegra_channel_g_selection(struct file *file, void *priv,
674 				     struct v4l2_selection *sel)
675 {
676 	struct tegra_vi_channel *chan = video_drvdata(file);
677 	struct v4l2_subdev *subdev;
678 	struct v4l2_subdev_format fmt = {
679 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
680 	};
681 	struct v4l2_subdev_selection sdsel = {
682 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
683 		.target = sel->target,
684 	};
685 	int ret;
686 
687 	subdev = tegra_channel_get_remote_source_subdev(chan);
688 	if (!v4l2_subdev_has_op(subdev, pad, get_selection))
689 		return -ENOTTY;
690 
691 	if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
692 		return -EINVAL;
693 	/*
694 	 * Try the get selection operation and fallback to get format if not
695 	 * implemented.
696 	 */
697 	ret = v4l2_subdev_call(subdev, pad, get_selection, NULL, &sdsel);
698 	if (!ret)
699 		sel->r = sdsel.r;
700 	if (ret != -ENOIOCTLCMD)
701 		return ret;
702 
703 	ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
704 	if (ret < 0)
705 		return ret;
706 
707 	sel->r.left = 0;
708 	sel->r.top = 0;
709 	sel->r.width = fmt.format.width;
710 	sel->r.height = fmt.format.height;
711 
712 	return 0;
713 }
714 
715 static int tegra_channel_s_selection(struct file *file, void *fh,
716 				     struct v4l2_selection *sel)
717 {
718 	struct tegra_vi_channel *chan = video_drvdata(file);
719 	struct v4l2_subdev *subdev;
720 	int ret;
721 	struct v4l2_subdev_selection sdsel = {
722 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
723 		.target = sel->target,
724 		.flags = sel->flags,
725 		.r = sel->r,
726 	};
727 
728 	subdev = tegra_channel_get_remote_source_subdev(chan);
729 	if (!v4l2_subdev_has_op(subdev, pad, set_selection))
730 		return -ENOTTY;
731 
732 	if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
733 		return -EINVAL;
734 
735 	if (vb2_is_busy(&chan->queue))
736 		return -EBUSY;
737 
738 	ret = v4l2_subdev_call(subdev, pad, set_selection, NULL, &sdsel);
739 	if (!ret) {
740 		sel->r = sdsel.r;
741 		/*
742 		 * Subdev active format resolution may have changed during
743 		 * set selection operation. So, update channel format to
744 		 * the sub-device active format.
745 		 */
746 		return tegra_channel_set_subdev_active_fmt(chan);
747 	}
748 
749 	return ret;
750 }
751 
752 static int tegra_channel_g_edid(struct file *file, void *fh,
753 				struct v4l2_edid *edid)
754 {
755 	struct tegra_vi_channel *chan = video_drvdata(file);
756 	struct v4l2_subdev *subdev;
757 
758 	subdev = tegra_channel_get_remote_source_subdev(chan);
759 	if (!v4l2_subdev_has_op(subdev, pad, get_edid))
760 		return -ENOTTY;
761 
762 	return v4l2_subdev_call(subdev, pad, get_edid, edid);
763 }
764 
765 static int tegra_channel_s_edid(struct file *file, void *fh,
766 				struct v4l2_edid *edid)
767 {
768 	struct tegra_vi_channel *chan = video_drvdata(file);
769 	struct v4l2_subdev *subdev;
770 
771 	subdev = tegra_channel_get_remote_source_subdev(chan);
772 	if (!v4l2_subdev_has_op(subdev, pad, set_edid))
773 		return -ENOTTY;
774 
775 	return v4l2_subdev_call(subdev, pad, set_edid, edid);
776 }
777 
778 static int tegra_channel_g_dv_timings(struct file *file, void *fh,
779 				      struct v4l2_dv_timings *timings)
780 {
781 	struct tegra_vi_channel *chan = video_drvdata(file);
782 	struct v4l2_subdev *subdev;
783 
784 	subdev = tegra_channel_get_remote_source_subdev(chan);
785 	if (!v4l2_subdev_has_op(subdev, video, g_dv_timings))
786 		return -ENOTTY;
787 
788 	return v4l2_device_call_until_err(chan->video.v4l2_dev, 0,
789 					  video, g_dv_timings, timings);
790 }
791 
792 static int tegra_channel_s_dv_timings(struct file *file, void *fh,
793 				      struct v4l2_dv_timings *timings)
794 {
795 	struct tegra_vi_channel *chan = video_drvdata(file);
796 	struct v4l2_subdev *subdev;
797 	struct v4l2_bt_timings *bt = &timings->bt;
798 	struct v4l2_dv_timings curr_timings;
799 	int ret;
800 
801 	subdev = tegra_channel_get_remote_source_subdev(chan);
802 	if (!v4l2_subdev_has_op(subdev, video, s_dv_timings))
803 		return -ENOTTY;
804 
805 	ret = tegra_channel_g_dv_timings(file, fh, &curr_timings);
806 	if (ret)
807 		return ret;
808 
809 	if (v4l2_match_dv_timings(timings, &curr_timings, 0, false))
810 		return 0;
811 
812 	if (vb2_is_busy(&chan->queue))
813 		return -EBUSY;
814 
815 	ret = v4l2_device_call_until_err(chan->video.v4l2_dev, 0,
816 					 video, s_dv_timings, timings);
817 	if (ret)
818 		return ret;
819 
820 	chan->format.width = bt->width;
821 	chan->format.height = bt->height;
822 	chan->format.bytesperline = bt->width * chan->fmtinfo->bpp;
823 	chan->format.sizeimage = chan->format.bytesperline * bt->height;
824 	tegra_channel_fmt_align(chan, &chan->format, chan->fmtinfo->bpp);
825 	tegra_channel_update_gangports(chan);
826 
827 	return 0;
828 }
829 
830 static int tegra_channel_query_dv_timings(struct file *file, void *fh,
831 					  struct v4l2_dv_timings *timings)
832 {
833 	struct tegra_vi_channel *chan = video_drvdata(file);
834 	struct v4l2_subdev *subdev;
835 
836 	subdev = tegra_channel_get_remote_source_subdev(chan);
837 	if (!v4l2_subdev_has_op(subdev, video, query_dv_timings))
838 		return -ENOTTY;
839 
840 	return v4l2_device_call_until_err(chan->video.v4l2_dev, 0,
841 					  video, query_dv_timings, timings);
842 }
843 
844 static int tegra_channel_enum_dv_timings(struct file *file, void *fh,
845 					 struct v4l2_enum_dv_timings *timings)
846 {
847 	struct tegra_vi_channel *chan = video_drvdata(file);
848 	struct v4l2_subdev *subdev;
849 
850 	subdev = tegra_channel_get_remote_source_subdev(chan);
851 	if (!v4l2_subdev_has_op(subdev, pad, enum_dv_timings))
852 		return -ENOTTY;
853 
854 	return v4l2_subdev_call(subdev, pad, enum_dv_timings, timings);
855 }
856 
857 static int tegra_channel_dv_timings_cap(struct file *file, void *fh,
858 					struct v4l2_dv_timings_cap *cap)
859 {
860 	struct tegra_vi_channel *chan = video_drvdata(file);
861 	struct v4l2_subdev *subdev;
862 
863 	subdev = tegra_channel_get_remote_source_subdev(chan);
864 	if (!v4l2_subdev_has_op(subdev, pad, dv_timings_cap))
865 		return -ENOTTY;
866 
867 	return v4l2_subdev_call(subdev, pad, dv_timings_cap, cap);
868 }
869 
870 static int tegra_channel_log_status(struct file *file, void *fh)
871 {
872 	struct tegra_vi_channel *chan = video_drvdata(file);
873 
874 	v4l2_device_call_all(chan->video.v4l2_dev, 0, core, log_status);
875 
876 	return 0;
877 }
878 
879 static int tegra_channel_enum_input(struct file *file, void *fh,
880 				    struct v4l2_input *inp)
881 {
882 	struct tegra_vi_channel *chan = video_drvdata(file);
883 	struct v4l2_subdev *subdev;
884 
885 	if (inp->index)
886 		return -EINVAL;
887 
888 	inp->type = V4L2_INPUT_TYPE_CAMERA;
889 	subdev = tegra_channel_get_remote_source_subdev(chan);
890 	strscpy(inp->name, subdev->name, sizeof(inp->name));
891 	if (v4l2_subdev_has_op(subdev, pad, dv_timings_cap))
892 		inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
893 
894 	return 0;
895 }
896 
897 static int tegra_channel_g_input(struct file *file, void *priv,
898 				 unsigned int *i)
899 {
900 	*i = 0;
901 
902 	return 0;
903 }
904 
905 static int tegra_channel_s_input(struct file *file, void *priv,
906 				 unsigned int input)
907 {
908 	if (input > 0)
909 		return -EINVAL;
910 
911 	return 0;
912 }
913 
914 static const struct v4l2_ioctl_ops tegra_channel_ioctl_ops = {
915 	.vidioc_querycap		= tegra_channel_querycap,
916 	.vidioc_g_parm			= tegra_channel_g_parm,
917 	.vidioc_s_parm			= tegra_channel_s_parm,
918 	.vidioc_enum_framesizes		= tegra_channel_enum_framesizes,
919 	.vidioc_enum_frameintervals	= tegra_channel_enum_frameintervals,
920 	.vidioc_enum_fmt_vid_cap	= tegra_channel_enum_format,
921 	.vidioc_g_fmt_vid_cap		= tegra_channel_get_format,
922 	.vidioc_s_fmt_vid_cap		= tegra_channel_set_format,
923 	.vidioc_try_fmt_vid_cap		= tegra_channel_try_format,
924 	.vidioc_enum_input		= tegra_channel_enum_input,
925 	.vidioc_g_input			= tegra_channel_g_input,
926 	.vidioc_s_input			= tegra_channel_s_input,
927 	.vidioc_reqbufs			= vb2_ioctl_reqbufs,
928 	.vidioc_prepare_buf		= vb2_ioctl_prepare_buf,
929 	.vidioc_querybuf		= vb2_ioctl_querybuf,
930 	.vidioc_qbuf			= vb2_ioctl_qbuf,
931 	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
932 	.vidioc_create_bufs		= vb2_ioctl_create_bufs,
933 	.vidioc_expbuf			= vb2_ioctl_expbuf,
934 	.vidioc_streamon		= vb2_ioctl_streamon,
935 	.vidioc_streamoff		= vb2_ioctl_streamoff,
936 	.vidioc_subscribe_event		= tegra_channel_subscribe_event,
937 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
938 	.vidioc_g_selection		= tegra_channel_g_selection,
939 	.vidioc_s_selection		= tegra_channel_s_selection,
940 	.vidioc_g_edid			= tegra_channel_g_edid,
941 	.vidioc_s_edid			= tegra_channel_s_edid,
942 	.vidioc_g_dv_timings		= tegra_channel_g_dv_timings,
943 	.vidioc_s_dv_timings		= tegra_channel_s_dv_timings,
944 	.vidioc_query_dv_timings	= tegra_channel_query_dv_timings,
945 	.vidioc_enum_dv_timings		= tegra_channel_enum_dv_timings,
946 	.vidioc_dv_timings_cap		= tegra_channel_dv_timings_cap,
947 	.vidioc_log_status		= tegra_channel_log_status,
948 };
949 
950 /*
951  * V4L2 file operations
952  */
953 static const struct v4l2_file_operations tegra_channel_fops = {
954 	.owner		= THIS_MODULE,
955 	.unlocked_ioctl	= video_ioctl2,
956 	.open		= v4l2_fh_open,
957 	.release	= vb2_fop_release,
958 	.read		= vb2_fop_read,
959 	.poll		= vb2_fop_poll,
960 	.mmap		= vb2_fop_mmap,
961 };
962 
963 /*
964  * V4L2 control operations
965  */
966 static int vi_s_ctrl(struct v4l2_ctrl *ctrl)
967 {
968 	struct tegra_vi_channel *chan = container_of(ctrl->handler,
969 						     struct tegra_vi_channel,
970 						     ctrl_handler);
971 
972 	switch (ctrl->id) {
973 	case V4L2_CID_TEST_PATTERN:
974 		/* pattern change takes effect on next stream */
975 		chan->pg_mode = ctrl->val + 1;
976 		break;
977 	case V4L2_CID_TEGRA_SYNCPT_TIMEOUT_RETRY:
978 		chan->syncpt_timeout_retry = ctrl->val;
979 		break;
980 	default:
981 		return -EINVAL;
982 	}
983 
984 	return 0;
985 }
986 
987 static const struct v4l2_ctrl_ops vi_ctrl_ops = {
988 	.s_ctrl	= vi_s_ctrl,
989 };
990 
991 #if IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG)
992 static const char *const vi_pattern_strings[] = {
993 	"Black/White Direct Mode",
994 	"Color Patch Mode",
995 };
996 #else
997 static const struct v4l2_ctrl_config syncpt_timeout_ctrl = {
998 	.ops = &vi_ctrl_ops,
999 	.id = V4L2_CID_TEGRA_SYNCPT_TIMEOUT_RETRY,
1000 	.name = "Syncpt timeout retry",
1001 	.type = V4L2_CTRL_TYPE_INTEGER,
1002 	.min = 1,
1003 	.max = 10000,
1004 	.step = 1,
1005 	.def = 5,
1006 };
1007 #endif
1008 
1009 static int tegra_channel_setup_ctrl_handler(struct tegra_vi_channel *chan)
1010 {
1011 	int ret;
1012 
1013 #if IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG)
1014 	/* add test pattern control handler to v4l2 device */
1015 	v4l2_ctrl_new_std_menu_items(&chan->ctrl_handler, &vi_ctrl_ops,
1016 				     V4L2_CID_TEST_PATTERN,
1017 				     ARRAY_SIZE(vi_pattern_strings) - 1,
1018 				     0, 0, vi_pattern_strings);
1019 	if (chan->ctrl_handler.error) {
1020 		dev_err(chan->vi->dev, "failed to add TPG ctrl handler: %d\n",
1021 			chan->ctrl_handler.error);
1022 		v4l2_ctrl_handler_free(&chan->ctrl_handler);
1023 		return chan->ctrl_handler.error;
1024 	}
1025 #else
1026 	struct v4l2_subdev *subdev;
1027 
1028 	/* custom control */
1029 	v4l2_ctrl_new_custom(&chan->ctrl_handler, &syncpt_timeout_ctrl, NULL);
1030 	if (chan->ctrl_handler.error) {
1031 		dev_err(chan->vi->dev, "failed to add %s ctrl handler: %d\n",
1032 			syncpt_timeout_ctrl.name,
1033 			chan->ctrl_handler.error);
1034 		v4l2_ctrl_handler_free(&chan->ctrl_handler);
1035 		return chan->ctrl_handler.error;
1036 	}
1037 
1038 	subdev = tegra_channel_get_remote_source_subdev(chan);
1039 	if (!subdev)
1040 		return -ENODEV;
1041 
1042 	ret = v4l2_ctrl_add_handler(&chan->ctrl_handler, subdev->ctrl_handler,
1043 				    NULL, true);
1044 	if (ret < 0) {
1045 		dev_err(chan->vi->dev,
1046 			"failed to add subdev %s ctrl handler: %d\n",
1047 			subdev->name, ret);
1048 		v4l2_ctrl_handler_free(&chan->ctrl_handler);
1049 		return ret;
1050 	}
1051 #endif
1052 
1053 	/* setup the controls */
1054 	ret = v4l2_ctrl_handler_setup(&chan->ctrl_handler);
1055 	if (ret < 0) {
1056 		dev_err(chan->vi->dev,
1057 			"failed to setup v4l2 ctrl handler: %d\n", ret);
1058 		return ret;
1059 	}
1060 
1061 	return 0;
1062 }
1063 
1064 /* VI only support 2 formats in TPG mode */
1065 static void vi_tpg_fmts_bitmap_init(struct tegra_vi_channel *chan)
1066 {
1067 	int index;
1068 
1069 	bitmap_zero(chan->tpg_fmts_bitmap, MAX_FORMAT_NUM);
1070 
1071 	index = tegra_get_format_idx_by_code(chan->vi,
1072 					     MEDIA_BUS_FMT_SRGGB10_1X10, 0);
1073 	bitmap_set(chan->tpg_fmts_bitmap, index, 1);
1074 
1075 	index = tegra_get_format_idx_by_code(chan->vi,
1076 					     MEDIA_BUS_FMT_RGB888_1X32_PADHI,
1077 					     0);
1078 	bitmap_set(chan->tpg_fmts_bitmap, index, 1);
1079 }
1080 
1081 static int vi_fmts_bitmap_init(struct tegra_vi_channel *chan)
1082 {
1083 	int index, ret, match_code = 0;
1084 	struct v4l2_subdev *subdev;
1085 	struct v4l2_subdev_mbus_code_enum code = {
1086 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
1087 	};
1088 
1089 	bitmap_zero(chan->fmts_bitmap, MAX_FORMAT_NUM);
1090 
1091 	/*
1092 	 * Set the bitmap bits based on all the matched formats between the
1093 	 * available media bus formats of sub-device and the pre-defined Tegra
1094 	 * supported video formats.
1095 	 */
1096 	subdev = tegra_channel_get_remote_source_subdev(chan);
1097 	while (1) {
1098 		ret = v4l2_subdev_call(subdev, pad, enum_mbus_code,
1099 				       NULL, &code);
1100 		if (ret < 0)
1101 			break;
1102 
1103 		index = tegra_get_format_idx_by_code(chan->vi, code.code, 0);
1104 		while (index >= 0) {
1105 			bitmap_set(chan->fmts_bitmap, index, 1);
1106 			if (!match_code)
1107 				match_code = code.code;
1108 			/* look for other formats with same mbus code */
1109 			index = tegra_get_format_idx_by_code(chan->vi,
1110 							     code.code,
1111 							     index + 1);
1112 		}
1113 
1114 		code.index++;
1115 	}
1116 
1117 	/*
1118 	 * Set the bitmap bit corresponding to default tegra video format if
1119 	 * there are no matched formats.
1120 	 */
1121 	if (!match_code) {
1122 		match_code = tegra_default_format.code;
1123 		index = tegra_get_format_idx_by_code(chan->vi, match_code, 0);
1124 		if (WARN_ON(index < 0))
1125 			return -EINVAL;
1126 
1127 		bitmap_set(chan->fmts_bitmap, index, 1);
1128 	}
1129 
1130 	/* initialize channel format to the sub-device active format */
1131 	tegra_channel_set_subdev_active_fmt(chan);
1132 
1133 	return 0;
1134 }
1135 
1136 static void tegra_channel_host1x_syncpts_free(struct tegra_vi_channel *chan)
1137 {
1138 	int i;
1139 
1140 	for (i = 0; i < chan->numgangports; i++) {
1141 		host1x_syncpt_put(chan->mw_ack_sp[i]);
1142 		host1x_syncpt_put(chan->frame_start_sp[i]);
1143 	}
1144 }
1145 
1146 static void tegra_channel_cleanup(struct tegra_vi_channel *chan)
1147 {
1148 	v4l2_ctrl_handler_free(&chan->ctrl_handler);
1149 	media_entity_cleanup(&chan->video.entity);
1150 	tegra_channel_host1x_syncpts_free(chan);
1151 	mutex_destroy(&chan->video_lock);
1152 }
1153 
1154 void tegra_channels_cleanup(struct tegra_vi *vi)
1155 {
1156 	struct tegra_vi_channel *chan, *tmp;
1157 
1158 	if (!vi)
1159 		return;
1160 
1161 	list_for_each_entry_safe(chan, tmp, &vi->vi_chans, list) {
1162 		tegra_channel_cleanup(chan);
1163 		list_del(&chan->list);
1164 		kfree(chan);
1165 	}
1166 }
1167 
1168 static int tegra_channel_host1x_syncpt_init(struct tegra_vi_channel *chan)
1169 {
1170 	struct tegra_vi *vi = chan->vi;
1171 	unsigned long flags = HOST1X_SYNCPT_CLIENT_MANAGED;
1172 	struct host1x_syncpt *fs_sp;
1173 	struct host1x_syncpt *mw_sp;
1174 	int ret, i;
1175 
1176 	for (i = 0; i < chan->numgangports; i++) {
1177 		fs_sp = host1x_syncpt_request(&vi->client, flags);
1178 		if (!fs_sp) {
1179 			dev_err(vi->dev, "failed to request frame start syncpoint\n");
1180 			ret = -ENOMEM;
1181 			goto free_syncpts;
1182 		}
1183 
1184 		mw_sp = host1x_syncpt_request(&vi->client, flags);
1185 		if (!mw_sp) {
1186 			dev_err(vi->dev, "failed to request memory ack syncpoint\n");
1187 			host1x_syncpt_put(fs_sp);
1188 			ret = -ENOMEM;
1189 			goto free_syncpts;
1190 		}
1191 
1192 		chan->frame_start_sp[i] = fs_sp;
1193 		chan->mw_ack_sp[i] = mw_sp;
1194 		spin_lock_init(&chan->sp_incr_lock[i]);
1195 	}
1196 
1197 	return 0;
1198 
1199 free_syncpts:
1200 	tegra_channel_host1x_syncpts_free(chan);
1201 	return ret;
1202 }
1203 
1204 static int tegra_channel_init(struct tegra_vi_channel *chan)
1205 {
1206 	struct tegra_vi *vi = chan->vi;
1207 	struct tegra_video_device *vid = dev_get_drvdata(vi->client.host);
1208 	int ret;
1209 
1210 	mutex_init(&chan->video_lock);
1211 	INIT_LIST_HEAD(&chan->capture);
1212 	INIT_LIST_HEAD(&chan->done);
1213 	spin_lock_init(&chan->start_lock);
1214 	spin_lock_init(&chan->done_lock);
1215 	init_waitqueue_head(&chan->start_wait);
1216 	init_waitqueue_head(&chan->done_wait);
1217 
1218 	/* initialize the video format */
1219 	chan->fmtinfo = &tegra_default_format;
1220 	chan->format.pixelformat = chan->fmtinfo->fourcc;
1221 	chan->format.colorspace = V4L2_COLORSPACE_SRGB;
1222 	chan->format.field = V4L2_FIELD_NONE;
1223 	chan->format.width = TEGRA_DEF_WIDTH;
1224 	chan->format.height = TEGRA_DEF_HEIGHT;
1225 	chan->format.bytesperline = TEGRA_DEF_WIDTH * chan->fmtinfo->bpp;
1226 	chan->format.sizeimage = chan->format.bytesperline * TEGRA_DEF_HEIGHT;
1227 	tegra_channel_fmt_align(chan, &chan->format, chan->fmtinfo->bpp);
1228 
1229 	ret = tegra_channel_host1x_syncpt_init(chan);
1230 	if (ret)
1231 		return ret;
1232 
1233 	/* initialize the media entity */
1234 	chan->pad.flags = MEDIA_PAD_FL_SINK;
1235 	ret = media_entity_pads_init(&chan->video.entity, 1, &chan->pad);
1236 	if (ret < 0) {
1237 		dev_err(vi->dev,
1238 			"failed to initialize media entity: %d\n", ret);
1239 		goto free_syncpts;
1240 	}
1241 
1242 	ret = v4l2_ctrl_handler_init(&chan->ctrl_handler, MAX_CID_CONTROLS);
1243 	if (chan->ctrl_handler.error) {
1244 		dev_err(vi->dev,
1245 			"failed to initialize v4l2 ctrl handler: %d\n", ret);
1246 		goto cleanup_media;
1247 	}
1248 
1249 	/* initialize the video_device */
1250 	chan->video.fops = &tegra_channel_fops;
1251 	chan->video.v4l2_dev = &vid->v4l2_dev;
1252 	chan->video.release = video_device_release_empty;
1253 	chan->video.queue = &chan->queue;
1254 	snprintf(chan->video.name, sizeof(chan->video.name), "%s-%s-%u",
1255 		 dev_name(vi->dev), "output", chan->portnos[0]);
1256 	chan->video.vfl_type = VFL_TYPE_VIDEO;
1257 	chan->video.vfl_dir = VFL_DIR_RX;
1258 	chan->video.ioctl_ops = &tegra_channel_ioctl_ops;
1259 	chan->video.ctrl_handler = &chan->ctrl_handler;
1260 	chan->video.lock = &chan->video_lock;
1261 	chan->video.device_caps = V4L2_CAP_VIDEO_CAPTURE |
1262 				  V4L2_CAP_STREAMING |
1263 				  V4L2_CAP_READWRITE;
1264 	video_set_drvdata(&chan->video, chan);
1265 
1266 	chan->queue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1267 	chan->queue.io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
1268 	chan->queue.lock = &chan->video_lock;
1269 	chan->queue.drv_priv = chan;
1270 	chan->queue.buf_struct_size = sizeof(struct tegra_channel_buffer);
1271 	chan->queue.ops = &tegra_channel_queue_qops;
1272 	chan->queue.mem_ops = &vb2_dma_contig_memops;
1273 	chan->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1274 	chan->queue.min_buffers_needed = 2;
1275 	chan->queue.dev = vi->dev;
1276 	ret = vb2_queue_init(&chan->queue);
1277 	if (ret < 0) {
1278 		dev_err(vi->dev, "failed to initialize vb2 queue: %d\n", ret);
1279 		goto free_v4l2_ctrl_hdl;
1280 	}
1281 
1282 	if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
1283 		v4l2_async_nf_init(&chan->notifier);
1284 
1285 	return 0;
1286 
1287 free_v4l2_ctrl_hdl:
1288 	v4l2_ctrl_handler_free(&chan->ctrl_handler);
1289 cleanup_media:
1290 	media_entity_cleanup(&chan->video.entity);
1291 free_syncpts:
1292 	tegra_channel_host1x_syncpts_free(chan);
1293 	return ret;
1294 }
1295 
1296 static int tegra_vi_channel_alloc(struct tegra_vi *vi, unsigned int port_num,
1297 				  struct device_node *node, unsigned int lanes)
1298 {
1299 	struct tegra_vi_channel *chan;
1300 	unsigned int i;
1301 
1302 	/*
1303 	 * Do not use devm_kzalloc as memory is freed immediately
1304 	 * when device instance is unbound but application might still
1305 	 * be holding the device node open. Channel memory allocated
1306 	 * with kzalloc is freed during video device release callback.
1307 	 */
1308 	chan = kzalloc(sizeof(*chan), GFP_KERNEL);
1309 	if (!chan)
1310 		return -ENOMEM;
1311 
1312 	chan->vi = vi;
1313 	chan->portnos[0] = port_num;
1314 	/*
1315 	 * For data lanes more than maximum csi lanes per brick, multiple of
1316 	 * x4 ports are used simultaneously for capture.
1317 	 */
1318 	if (lanes <= CSI_LANES_PER_BRICK)
1319 		chan->totalports = 1;
1320 	else
1321 		chan->totalports = lanes / CSI_LANES_PER_BRICK;
1322 	chan->numgangports = chan->totalports;
1323 
1324 	for (i = 1; i < chan->totalports; i++)
1325 		chan->portnos[i] = chan->portnos[0] + i * CSI_PORTS_PER_BRICK;
1326 
1327 	chan->of_node = node;
1328 	list_add_tail(&chan->list, &vi->vi_chans);
1329 
1330 	return 0;
1331 }
1332 
1333 static int tegra_vi_tpg_channels_alloc(struct tegra_vi *vi)
1334 {
1335 	unsigned int port_num;
1336 	unsigned int nchannels = vi->soc->vi_max_channels;
1337 	int ret;
1338 
1339 	for (port_num = 0; port_num < nchannels; port_num++) {
1340 		ret = tegra_vi_channel_alloc(vi, port_num,
1341 					     vi->dev->of_node, 2);
1342 		if (ret < 0)
1343 			return ret;
1344 	}
1345 
1346 	return 0;
1347 }
1348 
1349 static int tegra_vi_channels_alloc(struct tegra_vi *vi)
1350 {
1351 	struct device_node *node = vi->dev->of_node;
1352 	struct device_node *ep = NULL;
1353 	struct device_node *ports;
1354 	struct device_node *port;
1355 	unsigned int port_num;
1356 	struct device_node *parent;
1357 	struct v4l2_fwnode_endpoint v4l2_ep = { .bus_type = 0 };
1358 	unsigned int lanes;
1359 	int ret = 0;
1360 
1361 	ports = of_get_child_by_name(node, "ports");
1362 	if (!ports)
1363 		return -ENODEV;
1364 
1365 	for_each_child_of_node(ports, port) {
1366 		if (!of_node_name_eq(port, "port"))
1367 			continue;
1368 
1369 		ret = of_property_read_u32(port, "reg", &port_num);
1370 		if (ret < 0)
1371 			continue;
1372 
1373 		if (port_num > vi->soc->vi_max_channels) {
1374 			dev_err(vi->dev, "invalid port num %d for %pOF\n",
1375 				port_num, port);
1376 			ret = -EINVAL;
1377 			of_node_put(port);
1378 			goto cleanup;
1379 		}
1380 
1381 		ep = of_get_child_by_name(port, "endpoint");
1382 		if (!ep)
1383 			continue;
1384 
1385 		parent = of_graph_get_remote_port_parent(ep);
1386 		of_node_put(ep);
1387 		if (!parent)
1388 			continue;
1389 
1390 		ep = of_graph_get_endpoint_by_regs(parent, 0, 0);
1391 		of_node_put(parent);
1392 		ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep),
1393 						 &v4l2_ep);
1394 		of_node_put(ep);
1395 		if (ret)
1396 			continue;
1397 
1398 		lanes = v4l2_ep.bus.mipi_csi2.num_data_lanes;
1399 		ret = tegra_vi_channel_alloc(vi, port_num, port, lanes);
1400 		if (ret < 0) {
1401 			of_node_put(port);
1402 			goto cleanup;
1403 		}
1404 	}
1405 
1406 cleanup:
1407 	of_node_put(ports);
1408 	return ret;
1409 }
1410 
1411 static int tegra_vi_channels_init(struct tegra_vi *vi)
1412 {
1413 	struct tegra_vi_channel *chan;
1414 	int ret;
1415 
1416 	list_for_each_entry(chan, &vi->vi_chans, list) {
1417 		ret = tegra_channel_init(chan);
1418 		if (ret < 0) {
1419 			dev_err(vi->dev,
1420 				"failed to initialize channel-%d: %d\n",
1421 				chan->portnos[0], ret);
1422 			goto cleanup;
1423 		}
1424 	}
1425 
1426 	return 0;
1427 
1428 cleanup:
1429 	list_for_each_entry_continue_reverse(chan, &vi->vi_chans, list)
1430 		tegra_channel_cleanup(chan);
1431 
1432 	return ret;
1433 }
1434 
1435 void tegra_v4l2_nodes_cleanup_tpg(struct tegra_video_device *vid)
1436 {
1437 	struct tegra_vi *vi = vid->vi;
1438 	struct tegra_csi *csi = vid->csi;
1439 	struct tegra_csi_channel *csi_chan;
1440 	struct tegra_vi_channel *chan;
1441 
1442 	list_for_each_entry(chan, &vi->vi_chans, list)
1443 		vb2_video_unregister_device(&chan->video);
1444 
1445 	list_for_each_entry(csi_chan, &csi->csi_chans, list)
1446 		v4l2_device_unregister_subdev(&csi_chan->subdev);
1447 }
1448 
1449 int tegra_v4l2_nodes_setup_tpg(struct tegra_video_device *vid)
1450 {
1451 	struct tegra_vi *vi = vid->vi;
1452 	struct tegra_csi *csi = vid->csi;
1453 	struct tegra_vi_channel *vi_chan;
1454 	struct tegra_csi_channel *csi_chan;
1455 	u32 link_flags = MEDIA_LNK_FL_ENABLED;
1456 	int ret;
1457 
1458 	if (!vi || !csi)
1459 		return -ENODEV;
1460 
1461 	csi_chan = list_first_entry(&csi->csi_chans,
1462 				    struct tegra_csi_channel, list);
1463 
1464 	list_for_each_entry(vi_chan, &vi->vi_chans, list) {
1465 		struct media_entity *source = &csi_chan->subdev.entity;
1466 		struct media_entity *sink = &vi_chan->video.entity;
1467 		struct media_pad *source_pad = csi_chan->pads;
1468 		struct media_pad *sink_pad = &vi_chan->pad;
1469 
1470 		ret = v4l2_device_register_subdev(&vid->v4l2_dev,
1471 						  &csi_chan->subdev);
1472 		if (ret) {
1473 			dev_err(vi->dev,
1474 				"failed to register subdev: %d\n", ret);
1475 			goto cleanup;
1476 		}
1477 
1478 		ret = video_register_device(&vi_chan->video,
1479 					    VFL_TYPE_VIDEO, -1);
1480 		if (ret < 0) {
1481 			dev_err(vi->dev,
1482 				"failed to register video device: %d\n", ret);
1483 			goto cleanup;
1484 		}
1485 
1486 		dev_dbg(vi->dev, "creating %s:%u -> %s:%u link\n",
1487 			source->name, source_pad->index,
1488 			sink->name, sink_pad->index);
1489 
1490 		ret = media_create_pad_link(source, source_pad->index,
1491 					    sink, sink_pad->index,
1492 					    link_flags);
1493 		if (ret < 0) {
1494 			dev_err(vi->dev,
1495 				"failed to create %s:%u -> %s:%u link: %d\n",
1496 				source->name, source_pad->index,
1497 				sink->name, sink_pad->index, ret);
1498 			goto cleanup;
1499 		}
1500 
1501 		ret = tegra_channel_setup_ctrl_handler(vi_chan);
1502 		if (ret < 0)
1503 			goto cleanup;
1504 
1505 		v4l2_set_subdev_hostdata(&csi_chan->subdev, vi_chan);
1506 		vi_tpg_fmts_bitmap_init(vi_chan);
1507 		csi_chan = list_next_entry(csi_chan, list);
1508 	}
1509 
1510 	return 0;
1511 
1512 cleanup:
1513 	tegra_v4l2_nodes_cleanup_tpg(vid);
1514 	return ret;
1515 }
1516 
1517 static int __maybe_unused vi_runtime_resume(struct device *dev)
1518 {
1519 	struct tegra_vi *vi = dev_get_drvdata(dev);
1520 	int ret;
1521 
1522 	ret = regulator_enable(vi->vdd);
1523 	if (ret) {
1524 		dev_err(dev, "failed to enable VDD supply: %d\n", ret);
1525 		return ret;
1526 	}
1527 
1528 	ret = clk_set_rate(vi->clk, vi->soc->vi_max_clk_hz);
1529 	if (ret) {
1530 		dev_err(dev, "failed to set vi clock rate: %d\n", ret);
1531 		goto disable_vdd;
1532 	}
1533 
1534 	ret = clk_prepare_enable(vi->clk);
1535 	if (ret) {
1536 		dev_err(dev, "failed to enable vi clock: %d\n", ret);
1537 		goto disable_vdd;
1538 	}
1539 
1540 	return 0;
1541 
1542 disable_vdd:
1543 	regulator_disable(vi->vdd);
1544 	return ret;
1545 }
1546 
1547 static int __maybe_unused vi_runtime_suspend(struct device *dev)
1548 {
1549 	struct tegra_vi *vi = dev_get_drvdata(dev);
1550 
1551 	clk_disable_unprepare(vi->clk);
1552 
1553 	regulator_disable(vi->vdd);
1554 
1555 	return 0;
1556 }
1557 
1558 /*
1559  * Graph Management
1560  */
1561 static struct tegra_vi_graph_entity *
1562 tegra_vi_graph_find_entity(struct tegra_vi_channel *chan,
1563 			   const struct fwnode_handle *fwnode)
1564 {
1565 	struct tegra_vi_graph_entity *entity;
1566 	struct v4l2_async_subdev *asd;
1567 
1568 	list_for_each_entry(asd, &chan->notifier.asd_list, asd_list) {
1569 		entity = to_tegra_vi_graph_entity(asd);
1570 		if (entity->asd.match.fwnode == fwnode)
1571 			return entity;
1572 	}
1573 
1574 	return NULL;
1575 }
1576 
1577 static int tegra_vi_graph_build(struct tegra_vi_channel *chan,
1578 				struct tegra_vi_graph_entity *entity)
1579 {
1580 	struct tegra_vi *vi = chan->vi;
1581 	struct tegra_vi_graph_entity *ent;
1582 	struct fwnode_handle *ep = NULL;
1583 	struct v4l2_fwnode_link link;
1584 	struct media_entity *local = entity->entity;
1585 	struct media_entity *remote;
1586 	struct media_pad *local_pad;
1587 	struct media_pad *remote_pad;
1588 	u32 link_flags = MEDIA_LNK_FL_ENABLED;
1589 	int ret = 0;
1590 
1591 	dev_dbg(vi->dev, "creating links for entity %s\n", local->name);
1592 
1593 	while (1) {
1594 		ep = fwnode_graph_get_next_endpoint(entity->asd.match.fwnode,
1595 						    ep);
1596 		if (!ep)
1597 			break;
1598 
1599 		ret = v4l2_fwnode_parse_link(ep, &link);
1600 		if (ret < 0) {
1601 			dev_err(vi->dev, "failed to parse link for %pOF: %d\n",
1602 				to_of_node(ep), ret);
1603 			continue;
1604 		}
1605 
1606 		if (link.local_port >= local->num_pads) {
1607 			dev_err(vi->dev, "invalid port number %u on %pOF\n",
1608 				link.local_port, to_of_node(link.local_node));
1609 			v4l2_fwnode_put_link(&link);
1610 			ret = -EINVAL;
1611 			break;
1612 		}
1613 
1614 		local_pad = &local->pads[link.local_port];
1615 		/* Remote node is vi node. So use channel video entity and pad
1616 		 * as remote/sink.
1617 		 */
1618 		if (link.remote_node == of_fwnode_handle(vi->dev->of_node)) {
1619 			remote = &chan->video.entity;
1620 			remote_pad = &chan->pad;
1621 			goto create_link;
1622 		}
1623 
1624 		/*
1625 		 * Skip sink ports, they will be processed from the other end
1626 		 * of the link.
1627 		 */
1628 		if (local_pad->flags & MEDIA_PAD_FL_SINK) {
1629 			dev_dbg(vi->dev, "skipping sink port %pOF:%u\n",
1630 				to_of_node(link.local_node), link.local_port);
1631 			v4l2_fwnode_put_link(&link);
1632 			continue;
1633 		}
1634 
1635 		/* find the remote entity from notifier list */
1636 		ent = tegra_vi_graph_find_entity(chan, link.remote_node);
1637 		if (!ent) {
1638 			dev_err(vi->dev, "no entity found for %pOF\n",
1639 				to_of_node(link.remote_node));
1640 			v4l2_fwnode_put_link(&link);
1641 			ret = -ENODEV;
1642 			break;
1643 		}
1644 
1645 		remote = ent->entity;
1646 		if (link.remote_port >= remote->num_pads) {
1647 			dev_err(vi->dev, "invalid port number %u on %pOF\n",
1648 				link.remote_port,
1649 				to_of_node(link.remote_node));
1650 			v4l2_fwnode_put_link(&link);
1651 			ret = -EINVAL;
1652 			break;
1653 		}
1654 
1655 		remote_pad = &remote->pads[link.remote_port];
1656 
1657 create_link:
1658 		dev_dbg(vi->dev, "creating %s:%u -> %s:%u link\n",
1659 			local->name, local_pad->index,
1660 			remote->name, remote_pad->index);
1661 
1662 		ret = media_create_pad_link(local, local_pad->index,
1663 					    remote, remote_pad->index,
1664 					    link_flags);
1665 		v4l2_fwnode_put_link(&link);
1666 		if (ret < 0) {
1667 			dev_err(vi->dev,
1668 				"failed to create %s:%u -> %s:%u link: %d\n",
1669 				local->name, local_pad->index,
1670 				remote->name, remote_pad->index, ret);
1671 			break;
1672 		}
1673 	}
1674 
1675 	fwnode_handle_put(ep);
1676 	return ret;
1677 }
1678 
1679 static int tegra_vi_graph_notify_complete(struct v4l2_async_notifier *notifier)
1680 {
1681 	struct tegra_vi_graph_entity *entity;
1682 	struct v4l2_async_subdev *asd;
1683 	struct v4l2_subdev *subdev;
1684 	struct tegra_vi_channel *chan;
1685 	struct tegra_vi *vi;
1686 	int ret;
1687 
1688 	chan = container_of(notifier, struct tegra_vi_channel, notifier);
1689 	vi = chan->vi;
1690 
1691 	dev_dbg(vi->dev, "notify complete, all subdevs registered\n");
1692 
1693 	/*
1694 	 * Video device node should be created at the end of all the device
1695 	 * related initialization/setup.
1696 	 * Current video_register_device() does both initialize and register
1697 	 * video device in same API.
1698 	 *
1699 	 * TODO: Update v4l2-dev driver to split initialize and register into
1700 	 * separate APIs and then update Tegra video driver to do video device
1701 	 * initialize followed by all video device related setup and then
1702 	 * register the video device.
1703 	 */
1704 	ret = video_register_device(&chan->video, VFL_TYPE_VIDEO, -1);
1705 	if (ret < 0) {
1706 		dev_err(vi->dev,
1707 			"failed to register video device: %d\n", ret);
1708 		goto unregister_video;
1709 	}
1710 
1711 	/* create links between the entities */
1712 	list_for_each_entry(asd, &chan->notifier.asd_list, asd_list) {
1713 		entity = to_tegra_vi_graph_entity(asd);
1714 		ret = tegra_vi_graph_build(chan, entity);
1715 		if (ret < 0)
1716 			goto unregister_video;
1717 	}
1718 
1719 	ret = tegra_channel_setup_ctrl_handler(chan);
1720 	if (ret < 0) {
1721 		dev_err(vi->dev,
1722 			"failed to setup channel controls: %d\n", ret);
1723 		goto unregister_video;
1724 	}
1725 
1726 	ret = vi_fmts_bitmap_init(chan);
1727 	if (ret < 0) {
1728 		dev_err(vi->dev,
1729 			"failed to initialize formats bitmap: %d\n", ret);
1730 		goto unregister_video;
1731 	}
1732 
1733 	subdev = tegra_channel_get_remote_csi_subdev(chan);
1734 	if (!subdev) {
1735 		ret = -ENODEV;
1736 		dev_err(vi->dev,
1737 			"failed to get remote csi subdev: %d\n", ret);
1738 		goto unregister_video;
1739 	}
1740 
1741 	v4l2_set_subdev_hostdata(subdev, chan);
1742 
1743 	subdev = tegra_channel_get_remote_source_subdev(chan);
1744 	v4l2_set_subdev_hostdata(subdev, chan);
1745 
1746 	return 0;
1747 
1748 unregister_video:
1749 	vb2_video_unregister_device(&chan->video);
1750 	return ret;
1751 }
1752 
1753 static int tegra_vi_graph_notify_bound(struct v4l2_async_notifier *notifier,
1754 				       struct v4l2_subdev *subdev,
1755 				       struct v4l2_async_subdev *asd)
1756 {
1757 	struct tegra_vi_graph_entity *entity;
1758 	struct tegra_vi *vi;
1759 	struct tegra_vi_channel *chan;
1760 
1761 	chan = container_of(notifier, struct tegra_vi_channel, notifier);
1762 	vi = chan->vi;
1763 
1764 	/*
1765 	 * Locate the entity corresponding to the bound subdev and store the
1766 	 * subdev pointer.
1767 	 */
1768 	entity = tegra_vi_graph_find_entity(chan, subdev->fwnode);
1769 	if (!entity) {
1770 		dev_err(vi->dev, "no entity for subdev %s\n", subdev->name);
1771 		return -EINVAL;
1772 	}
1773 
1774 	if (entity->subdev) {
1775 		dev_err(vi->dev, "duplicate subdev for node %pOF\n",
1776 			to_of_node(entity->asd.match.fwnode));
1777 		return -EINVAL;
1778 	}
1779 
1780 	dev_dbg(vi->dev, "subdev %s bound\n", subdev->name);
1781 	entity->entity = &subdev->entity;
1782 	entity->subdev = subdev;
1783 
1784 	return 0;
1785 }
1786 
1787 static const struct v4l2_async_notifier_operations tegra_vi_async_ops = {
1788 	.bound = tegra_vi_graph_notify_bound,
1789 	.complete = tegra_vi_graph_notify_complete,
1790 };
1791 
1792 static int tegra_vi_graph_parse_one(struct tegra_vi_channel *chan,
1793 				    struct fwnode_handle *fwnode)
1794 {
1795 	struct tegra_vi *vi = chan->vi;
1796 	struct fwnode_handle *ep = NULL;
1797 	struct fwnode_handle *remote = NULL;
1798 	struct tegra_vi_graph_entity *tvge;
1799 	struct device_node *node = NULL;
1800 	int ret;
1801 
1802 	dev_dbg(vi->dev, "parsing node %pOF\n", to_of_node(fwnode));
1803 
1804 	/* parse all the remote entities and put them into the list */
1805 	for_each_endpoint_of_node(to_of_node(fwnode), node) {
1806 		ep = of_fwnode_handle(node);
1807 		remote = fwnode_graph_get_remote_port_parent(ep);
1808 		if (!remote) {
1809 			dev_err(vi->dev,
1810 				"remote device at %pOF not found\n", node);
1811 			ret = -EINVAL;
1812 			goto cleanup;
1813 		}
1814 
1815 		/* skip entities that are already processed */
1816 		if (device_match_fwnode(vi->dev, remote) ||
1817 		    tegra_vi_graph_find_entity(chan, remote)) {
1818 			fwnode_handle_put(remote);
1819 			continue;
1820 		}
1821 
1822 		tvge = v4l2_async_nf_add_fwnode(&chan->notifier, remote,
1823 						struct tegra_vi_graph_entity);
1824 		if (IS_ERR(tvge)) {
1825 			ret = PTR_ERR(tvge);
1826 			dev_err(vi->dev,
1827 				"failed to add subdev to notifier: %d\n", ret);
1828 			fwnode_handle_put(remote);
1829 			goto cleanup;
1830 		}
1831 
1832 		ret = tegra_vi_graph_parse_one(chan, remote);
1833 		if (ret < 0) {
1834 			fwnode_handle_put(remote);
1835 			goto cleanup;
1836 		}
1837 
1838 		fwnode_handle_put(remote);
1839 	}
1840 
1841 	return 0;
1842 
1843 cleanup:
1844 	dev_err(vi->dev, "failed parsing the graph: %d\n", ret);
1845 	v4l2_async_nf_cleanup(&chan->notifier);
1846 	of_node_put(node);
1847 	return ret;
1848 }
1849 
1850 static int tegra_vi_graph_init(struct tegra_vi *vi)
1851 {
1852 	struct tegra_video_device *vid = dev_get_drvdata(vi->client.host);
1853 	struct tegra_vi_channel *chan;
1854 	struct fwnode_handle *fwnode = dev_fwnode(vi->dev);
1855 	int ret;
1856 
1857 	/*
1858 	 * Walk the links to parse the full graph. Each channel will have
1859 	 * one endpoint of the composite node. Start by parsing the
1860 	 * composite node and parse the remote entities in turn.
1861 	 * Each channel will register v4l2 async notifier to make the graph
1862 	 * independent between the channels so we can the current channel
1863 	 * in case of something wrong during graph parsing and continue with
1864 	 * next channels.
1865 	 */
1866 	list_for_each_entry(chan, &vi->vi_chans, list) {
1867 		struct fwnode_handle *ep, *remote;
1868 
1869 		ep = fwnode_graph_get_endpoint_by_id(fwnode,
1870 						     chan->portnos[0], 0, 0);
1871 		if (!ep)
1872 			continue;
1873 
1874 		remote = fwnode_graph_get_remote_port_parent(ep);
1875 		fwnode_handle_put(ep);
1876 
1877 		ret = tegra_vi_graph_parse_one(chan, remote);
1878 		fwnode_handle_put(remote);
1879 		if (ret < 0 || list_empty(&chan->notifier.asd_list))
1880 			continue;
1881 
1882 		chan->notifier.ops = &tegra_vi_async_ops;
1883 		ret = v4l2_async_nf_register(&vid->v4l2_dev, &chan->notifier);
1884 		if (ret < 0) {
1885 			dev_err(vi->dev,
1886 				"failed to register channel %d notifier: %d\n",
1887 				chan->portnos[0], ret);
1888 			v4l2_async_nf_cleanup(&chan->notifier);
1889 		}
1890 	}
1891 
1892 	return 0;
1893 }
1894 
1895 static void tegra_vi_graph_cleanup(struct tegra_vi *vi)
1896 {
1897 	struct tegra_vi_channel *chan;
1898 
1899 	list_for_each_entry(chan, &vi->vi_chans, list) {
1900 		vb2_video_unregister_device(&chan->video);
1901 		v4l2_async_nf_unregister(&chan->notifier);
1902 		v4l2_async_nf_cleanup(&chan->notifier);
1903 	}
1904 }
1905 
1906 static int tegra_vi_init(struct host1x_client *client)
1907 {
1908 	struct tegra_video_device *vid = dev_get_drvdata(client->host);
1909 	struct tegra_vi *vi = host1x_client_to_vi(client);
1910 	struct tegra_vi_channel *chan, *tmp;
1911 	int ret;
1912 
1913 	vid->media_dev.hw_revision = vi->soc->hw_revision;
1914 	snprintf(vid->media_dev.bus_info, sizeof(vid->media_dev.bus_info),
1915 		 "platform:%s", dev_name(vi->dev));
1916 
1917 	INIT_LIST_HEAD(&vi->vi_chans);
1918 
1919 	if (IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
1920 		ret = tegra_vi_tpg_channels_alloc(vi);
1921 	else
1922 		ret = tegra_vi_channels_alloc(vi);
1923 	if (ret < 0) {
1924 		dev_err(vi->dev,
1925 			"failed to allocate vi channels: %d\n", ret);
1926 		goto free_chans;
1927 	}
1928 
1929 	ret = tegra_vi_channels_init(vi);
1930 	if (ret < 0)
1931 		goto free_chans;
1932 
1933 	vid->vi = vi;
1934 
1935 	if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG)) {
1936 		ret = tegra_vi_graph_init(vi);
1937 		if (ret < 0)
1938 			goto cleanup_chans;
1939 	}
1940 
1941 	return 0;
1942 
1943 cleanup_chans:
1944 	list_for_each_entry(chan, &vi->vi_chans, list)
1945 		tegra_channel_cleanup(chan);
1946 free_chans:
1947 	list_for_each_entry_safe(chan, tmp, &vi->vi_chans, list) {
1948 		list_del(&chan->list);
1949 		kfree(chan);
1950 	}
1951 
1952 	return ret;
1953 }
1954 
1955 static int tegra_vi_exit(struct host1x_client *client)
1956 {
1957 	struct tegra_vi *vi = host1x_client_to_vi(client);
1958 
1959 	/*
1960 	 * Do not cleanup the channels here as application might still be
1961 	 * holding video device nodes. Channels cleanup will happen during
1962 	 * v4l2_device release callback which gets called after all video
1963 	 * device nodes are released.
1964 	 */
1965 
1966 	if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG))
1967 		tegra_vi_graph_cleanup(vi);
1968 
1969 	return 0;
1970 }
1971 
1972 static const struct host1x_client_ops vi_client_ops = {
1973 	.init = tegra_vi_init,
1974 	.exit = tegra_vi_exit,
1975 };
1976 
1977 static int tegra_vi_probe(struct platform_device *pdev)
1978 {
1979 	struct tegra_vi *vi;
1980 	int ret;
1981 
1982 	vi = devm_kzalloc(&pdev->dev, sizeof(*vi), GFP_KERNEL);
1983 	if (!vi)
1984 		return -ENOMEM;
1985 
1986 	vi->iomem = devm_platform_ioremap_resource(pdev, 0);
1987 	if (IS_ERR(vi->iomem))
1988 		return PTR_ERR(vi->iomem);
1989 
1990 	vi->soc = of_device_get_match_data(&pdev->dev);
1991 
1992 	vi->clk = devm_clk_get(&pdev->dev, NULL);
1993 	if (IS_ERR(vi->clk)) {
1994 		ret = PTR_ERR(vi->clk);
1995 		dev_err(&pdev->dev, "failed to get vi clock: %d\n", ret);
1996 		return ret;
1997 	}
1998 
1999 	vi->vdd = devm_regulator_get(&pdev->dev, "avdd-dsi-csi");
2000 	if (IS_ERR(vi->vdd)) {
2001 		ret = PTR_ERR(vi->vdd);
2002 		dev_err(&pdev->dev, "failed to get VDD supply: %d\n", ret);
2003 		return ret;
2004 	}
2005 
2006 	if (!pdev->dev.pm_domain) {
2007 		ret = -ENOENT;
2008 		dev_warn(&pdev->dev, "PM domain is not attached: %d\n", ret);
2009 		return ret;
2010 	}
2011 
2012 	ret = devm_of_platform_populate(&pdev->dev);
2013 	if (ret < 0) {
2014 		dev_err(&pdev->dev,
2015 			"failed to populate vi child device: %d\n", ret);
2016 		return ret;
2017 	}
2018 
2019 	vi->dev = &pdev->dev;
2020 	vi->ops = vi->soc->ops;
2021 	platform_set_drvdata(pdev, vi);
2022 	pm_runtime_enable(&pdev->dev);
2023 
2024 	/* initialize host1x interface */
2025 	INIT_LIST_HEAD(&vi->client.list);
2026 	vi->client.ops = &vi_client_ops;
2027 	vi->client.dev = &pdev->dev;
2028 
2029 	ret = host1x_client_register(&vi->client);
2030 	if (ret < 0) {
2031 		dev_err(&pdev->dev,
2032 			"failed to register host1x client: %d\n", ret);
2033 		goto rpm_disable;
2034 	}
2035 
2036 	return 0;
2037 
2038 rpm_disable:
2039 	pm_runtime_disable(&pdev->dev);
2040 	return ret;
2041 }
2042 
2043 static int tegra_vi_remove(struct platform_device *pdev)
2044 {
2045 	struct tegra_vi *vi = platform_get_drvdata(pdev);
2046 
2047 	host1x_client_unregister(&vi->client);
2048 
2049 	pm_runtime_disable(&pdev->dev);
2050 
2051 	return 0;
2052 }
2053 
2054 static const struct of_device_id tegra_vi_of_id_table[] = {
2055 #if defined(CONFIG_ARCH_TEGRA_210_SOC)
2056 	{ .compatible = "nvidia,tegra210-vi", .data = &tegra210_vi_soc },
2057 #endif
2058 	{ }
2059 };
2060 MODULE_DEVICE_TABLE(of, tegra_vi_of_id_table);
2061 
2062 static const struct dev_pm_ops tegra_vi_pm_ops = {
2063 	SET_RUNTIME_PM_OPS(vi_runtime_suspend, vi_runtime_resume, NULL)
2064 };
2065 
2066 struct platform_driver tegra_vi_driver = {
2067 	.driver = {
2068 		.name = "tegra-vi",
2069 		.of_match_table = tegra_vi_of_id_table,
2070 		.pm = &tegra_vi_pm_ops,
2071 	},
2072 	.probe = tegra_vi_probe,
2073 	.remove = tegra_vi_remove,
2074 };
2075