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