File Index
Files and directories
Sign in
FILE

vi.c

File preview with syntax highlighting.

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