vsp1_drv.c 21.4 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
2 3 4
/*
 * vsp1_drv.c  --  R-Car VSP1 Driver
 *
5
 * Copyright (C) 2013-2015 Renesas Electronics Corporation
6 7 8 9 10 11 12 13 14
 *
 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
 */

#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/module.h>
15
#include <linux/of.h>
16
#include <linux/of_device.h>
17
#include <linux/platform_device.h>
18
#include <linux/pm_runtime.h>
19 20
#include <linux/videodev2.h>

21
#include <media/rcar-fcp.h>
22 23
#include <media/v4l2-subdev.h>

24
#include "vsp1.h"
25
#include "vsp1_brx.h"
26
#include "vsp1_clu.h"
27
#include "vsp1_dl.h"
28
#include "vsp1_drm.h"
29
#include "vsp1_hgo.h"
30
#include "vsp1_hgt.h"
31
#include "vsp1_hsit.h"
32
#include "vsp1_lif.h"
33
#include "vsp1_lut.h"
34
#include "vsp1_pipe.h"
35
#include "vsp1_rwpf.h"
36
#include "vsp1_sru.h"
37
#include "vsp1_uds.h"
38
#include "vsp1_uif.h"
39
#include "vsp1_video.h"
40 41 42 43 44 45 46 47 48 49 50

/* -----------------------------------------------------------------------------
 * Interrupt Handling
 */

static irqreturn_t vsp1_irq_handler(int irq, void *data)
{
	u32 mask = VI6_WFP_IRQ_STA_DFE | VI6_WFP_IRQ_STA_FRE;
	struct vsp1_device *vsp1 = data;
	irqreturn_t ret = IRQ_NONE;
	unsigned int i;
51
	u32 status;
52

53
	for (i = 0; i < vsp1->info->wpf_count; ++i) {
54 55 56 57 58 59 60 61
		struct vsp1_rwpf *wpf = vsp1->wpf[i];

		if (wpf == NULL)
			continue;

		status = vsp1_read(vsp1, VI6_WPF_IRQ_STA(i));
		vsp1_write(vsp1, VI6_WPF_IRQ_STA(i), ~status & mask);

62
		if (status & VI6_WFP_IRQ_STA_DFE) {
63
			vsp1_pipeline_frame_end(wpf->entity.pipe);
64 65 66 67 68 69 70 71 72 73 74 75
			ret = IRQ_HANDLED;
		}
	}

	return ret;
}

/* -----------------------------------------------------------------------------
 * Entities
 */

/*
76
 * vsp1_create_sink_links - Create links from all sources to the given sink
77 78 79 80 81 82 83
 *
 * This function creates media links from all valid sources to the given sink
 * pad. Links that would be invalid according to the VSP1 hardware capabilities
 * are skipped. Those include all links
 *
 * - from a UDS to a UDS (UDS entities can't be chained)
 * - from an entity to itself (no loops are allowed)
84 85 86 87
 *
 * Furthermore, the BRS can't be connected to histogram generators, but no
 * special check is currently needed as all VSP instances that include a BRS
 * have no histogram generator.
88
 */
89 90
static int vsp1_create_sink_links(struct vsp1_device *vsp1,
				  struct vsp1_entity *sink)
91 92 93 94 95 96 97 98 99 100 101 102
{
	struct media_entity *entity = &sink->subdev.entity;
	struct vsp1_entity *source;
	unsigned int pad;
	int ret;

	list_for_each_entry(source, &vsp1->entities, list_dev) {
		u32 flags;

		if (source->type == sink->type)
			continue;

103 104 105
		if (source->type == VSP1_ENTITY_HGO ||
		    source->type == VSP1_ENTITY_HGT ||
		    source->type == VSP1_ENTITY_LIF ||
106 107 108 109 110 111 112 113 114 115 116 117
		    source->type == VSP1_ENTITY_WPF)
			continue;

		flags = source->type == VSP1_ENTITY_RPF &&
			sink->type == VSP1_ENTITY_WPF &&
			source->index == sink->index
		      ? MEDIA_LNK_FL_ENABLED : 0;

		for (pad = 0; pad < entity->num_pads; ++pad) {
			if (!(entity->pads[pad].flags & MEDIA_PAD_FL_SINK))
				continue;

118
			ret = media_create_pad_link(&source->subdev.entity,
119 120 121 122
						       source->source_pad,
						       entity, pad, flags);
			if (ret < 0)
				return ret;
123 124

			if (flags & MEDIA_LNK_FL_ENABLED)
125
				source->sink = sink;
126 127 128
		}
	}

129 130
	return 0;
}
131

132
static int vsp1_uapi_create_links(struct vsp1_device *vsp1)
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
{
	struct vsp1_entity *entity;
	unsigned int i;
	int ret;

	list_for_each_entry(entity, &vsp1->entities, list_dev) {
		if (entity->type == VSP1_ENTITY_LIF ||
		    entity->type == VSP1_ENTITY_RPF)
			continue;

		ret = vsp1_create_sink_links(vsp1, entity);
		if (ret < 0)
			return ret;
	}

148 149 150 151 152 153 154 155 156 157
	if (vsp1->hgo) {
		ret = media_create_pad_link(&vsp1->hgo->histo.entity.subdev.entity,
					    HISTO_PAD_SOURCE,
					    &vsp1->hgo->histo.video.entity, 0,
					    MEDIA_LNK_FL_ENABLED |
					    MEDIA_LNK_FL_IMMUTABLE);
		if (ret < 0)
			return ret;
	}

158 159 160 161 162 163 164 165 166 167
	if (vsp1->hgt) {
		ret = media_create_pad_link(&vsp1->hgt->histo.entity.subdev.entity,
					    HISTO_PAD_SOURCE,
					    &vsp1->hgt->histo.video.entity, 0,
					    MEDIA_LNK_FL_ENABLED |
					    MEDIA_LNK_FL_IMMUTABLE);
		if (ret < 0)
			return ret;
	}

168 169 170 171 172
	for (i = 0; i < vsp1->info->lif_count; ++i) {
		if (!vsp1->lif[i])
			continue;

		ret = media_create_pad_link(&vsp1->wpf[i]->entity.subdev.entity,
173
					    RWPF_PAD_SOURCE,
174
					    &vsp1->lif[i]->entity.subdev.entity,
175 176 177 178 179
					    LIF_PAD_SINK, 0);
		if (ret < 0)
			return ret;
	}

180
	for (i = 0; i < vsp1->info->rpf_count; ++i) {
181 182 183 184 185 186 187 188 189 190 191
		struct vsp1_rwpf *rpf = vsp1->rpf[i];

		ret = media_create_pad_link(&rpf->video->video.entity, 0,
					    &rpf->entity.subdev.entity,
					    RWPF_PAD_SINK,
					    MEDIA_LNK_FL_ENABLED |
					    MEDIA_LNK_FL_IMMUTABLE);
		if (ret < 0)
			return ret;
	}

192
	for (i = 0; i < vsp1->info->wpf_count; ++i) {
193 194
		/*
		 * Connect the video device to the WPF. All connections are
195
		 * immutable.
196
		 */
197
		struct vsp1_rwpf *wpf = vsp1->wpf[i];
198

199 200 201
		ret = media_create_pad_link(&wpf->entity.subdev.entity,
					    RWPF_PAD_SOURCE,
					    &wpf->video->video.entity, 0,
202 203
					    MEDIA_LNK_FL_IMMUTABLE |
					    MEDIA_LNK_FL_ENABLED);
204 205
		if (ret < 0)
			return ret;
206 207
	}

208 209 210 211 212
	return 0;
}

static void vsp1_destroy_entities(struct vsp1_device *vsp1)
{
213 214
	struct vsp1_entity *entity, *_entity;
	struct vsp1_video *video, *_video;
215

216
	list_for_each_entry_safe(entity, _entity, &vsp1->entities, list_dev) {
217 218 219 220
		list_del(&entity->list_dev);
		vsp1_entity_destroy(entity);
	}

221 222 223 224 225
	list_for_each_entry_safe(video, _video, &vsp1->videos, list) {
		list_del(&video->list);
		vsp1_video_cleanup(video);
	}

226
	v4l2_device_unregister(&vsp1->v4l2_dev);
227 228
	if (vsp1->info->uapi)
		media_device_unregister(&vsp1->media_dev);
229
	media_device_cleanup(&vsp1->media_dev);
230

231
	if (!vsp1->info->uapi)
232
		vsp1_drm_cleanup(vsp1);
233 234 235 236 237 238 239 240 241 242 243
}

static int vsp1_create_entities(struct vsp1_device *vsp1)
{
	struct media_device *mdev = &vsp1->media_dev;
	struct v4l2_device *vdev = &vsp1->v4l2_dev;
	struct vsp1_entity *entity;
	unsigned int i;
	int ret;

	mdev->dev = vsp1->dev;
244 245
	mdev->hw_revision = vsp1->version;
	strlcpy(mdev->model, vsp1->info->model, sizeof(mdev->model));
246 247
	snprintf(mdev->bus_info, sizeof(mdev->bus_info), "platform:%s",
		 dev_name(mdev->dev));
248
	media_device_init(mdev);
249

250
	vsp1->media_ops.link_setup = vsp1_entity_link_setup;
251 252
	/*
	 * Don't perform link validation when the userspace API is disabled as
253 254 255
	 * the pipeline is configured internally by the driver in that case, and
	 * its configuration can thus be trusted.
	 */
256
	if (vsp1->info->uapi)
257 258
		vsp1->media_ops.link_validate = v4l2_subdev_link_validate;

259 260 261 262 263 264 265 266 267
	vdev->mdev = mdev;
	ret = v4l2_device_register(vsp1->dev, vdev);
	if (ret < 0) {
		dev_err(vsp1->dev, "V4L2 device registration failed (%d)\n",
			ret);
		goto done;
	}

	/* Instantiate all the entities. */
268
	if (vsp1->info->features & VSP1_HAS_BRS) {
269
		vsp1->brs = vsp1_brx_create(vsp1, VSP1_ENTITY_BRS);
270 271 272 273 274 275 276 277
		if (IS_ERR(vsp1->brs)) {
			ret = PTR_ERR(vsp1->brs);
			goto done;
		}

		list_add_tail(&vsp1->brs->entity.list_dev, &vsp1->entities);
	}

278
	if (vsp1->info->features & VSP1_HAS_BRU) {
279
		vsp1->bru = vsp1_brx_create(vsp1, VSP1_ENTITY_BRU);
280 281 282 283
		if (IS_ERR(vsp1->bru)) {
			ret = PTR_ERR(vsp1->bru);
			goto done;
		}
284

285 286
		list_add_tail(&vsp1->bru->entity.list_dev, &vsp1->entities);
	}
287

288 289 290 291 292 293 294 295 296 297
	if (vsp1->info->features & VSP1_HAS_CLU) {
		vsp1->clu = vsp1_clu_create(vsp1);
		if (IS_ERR(vsp1->clu)) {
			ret = PTR_ERR(vsp1->clu);
			goto done;
		}

		list_add_tail(&vsp1->clu->entity.list_dev, &vsp1->entities);
	}

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
	vsp1->hsi = vsp1_hsit_create(vsp1, true);
	if (IS_ERR(vsp1->hsi)) {
		ret = PTR_ERR(vsp1->hsi);
		goto done;
	}

	list_add_tail(&vsp1->hsi->entity.list_dev, &vsp1->entities);

	vsp1->hst = vsp1_hsit_create(vsp1, false);
	if (IS_ERR(vsp1->hst)) {
		ret = PTR_ERR(vsp1->hst);
		goto done;
	}

	list_add_tail(&vsp1->hst->entity.list_dev, &vsp1->entities);

314 315 316 317 318 319 320 321 322 323 324
	if (vsp1->info->features & VSP1_HAS_HGO && vsp1->info->uapi) {
		vsp1->hgo = vsp1_hgo_create(vsp1);
		if (IS_ERR(vsp1->hgo)) {
			ret = PTR_ERR(vsp1->hgo);
			goto done;
		}

		list_add_tail(&vsp1->hgo->histo.entity.list_dev,
			      &vsp1->entities);
	}

325 326 327 328 329 330 331 332 333 334 335
	if (vsp1->info->features & VSP1_HAS_HGT && vsp1->info->uapi) {
		vsp1->hgt = vsp1_hgt_create(vsp1);
		if (IS_ERR(vsp1->hgt)) {
			ret = PTR_ERR(vsp1->hgt);
			goto done;
		}

		list_add_tail(&vsp1->hgt->histo.entity.list_dev,
			      &vsp1->entities);
	}

336
	/*
337
	 * The LIFs are only supported when used in conjunction with the DU, in
338
	 * which case the userspace API is disabled. If the userspace API is
339
	 * enabled skip the LIFs, even when present.
340
	 */
341 342 343 344 345 346 347 348 349
	if (!vsp1->info->uapi) {
		for (i = 0; i < vsp1->info->lif_count; ++i) {
			struct vsp1_lif *lif;

			lif = vsp1_lif_create(vsp1, i);
			if (IS_ERR(lif)) {
				ret = PTR_ERR(lif);
				goto done;
			}
350

351 352 353
			vsp1->lif[i] = lif;
			list_add_tail(&lif->entity.list_dev, &vsp1->entities);
		}
354 355
	}

356
	if (vsp1->info->features & VSP1_HAS_LUT) {
357 358 359 360 361 362 363 364 365
		vsp1->lut = vsp1_lut_create(vsp1);
		if (IS_ERR(vsp1->lut)) {
			ret = PTR_ERR(vsp1->lut);
			goto done;
		}

		list_add_tail(&vsp1->lut->entity.list_dev, &vsp1->entities);
	}

366
	for (i = 0; i < vsp1->info->rpf_count; ++i) {
367 368 369 370 371 372 373 374 375 376
		struct vsp1_rwpf *rpf;

		rpf = vsp1_rpf_create(vsp1, i);
		if (IS_ERR(rpf)) {
			ret = PTR_ERR(rpf);
			goto done;
		}

		vsp1->rpf[i] = rpf;
		list_add_tail(&rpf->entity.list_dev, &vsp1->entities);
377

378
		if (vsp1->info->uapi) {
379
			struct vsp1_video *video = vsp1_video_create(vsp1, rpf);
380

381 382 383 384 385 386 387
			if (IS_ERR(video)) {
				ret = PTR_ERR(video);
				goto done;
			}

			list_add_tail(&video->list, &vsp1->videos);
		}
388 389
	}

390
	if (vsp1->info->features & VSP1_HAS_SRU) {
391 392 393 394 395 396 397 398 399
		vsp1->sru = vsp1_sru_create(vsp1);
		if (IS_ERR(vsp1->sru)) {
			ret = PTR_ERR(vsp1->sru);
			goto done;
		}

		list_add_tail(&vsp1->sru->entity.list_dev, &vsp1->entities);
	}

400
	for (i = 0; i < vsp1->info->uds_count; ++i) {
401 402 403 404 405 406 407 408 409 410 411 412
		struct vsp1_uds *uds;

		uds = vsp1_uds_create(vsp1, i);
		if (IS_ERR(uds)) {
			ret = PTR_ERR(uds);
			goto done;
		}

		vsp1->uds[i] = uds;
		list_add_tail(&uds->entity.list_dev, &vsp1->entities);
	}

413 414 415 416 417 418 419 420 421 422 423 424 425
	for (i = 0; i < vsp1->info->uif_count; ++i) {
		struct vsp1_uif *uif;

		uif = vsp1_uif_create(vsp1, i);
		if (IS_ERR(uif)) {
			ret = PTR_ERR(uif);
			goto done;
		}

		vsp1->uif[i] = uif;
		list_add_tail(&uif->entity.list_dev, &vsp1->entities);
	}

426
	for (i = 0; i < vsp1->info->wpf_count; ++i) {
427 428 429 430 431 432 433 434 435 436
		struct vsp1_rwpf *wpf;

		wpf = vsp1_wpf_create(vsp1, i);
		if (IS_ERR(wpf)) {
			ret = PTR_ERR(wpf);
			goto done;
		}

		vsp1->wpf[i] = wpf;
		list_add_tail(&wpf->entity.list_dev, &vsp1->entities);
437

438
		if (vsp1->info->uapi) {
439
			struct vsp1_video *video = vsp1_video_create(vsp1, wpf);
440

441 442 443 444 445 446 447
			if (IS_ERR(video)) {
				ret = PTR_ERR(video);
				goto done;
			}

			list_add_tail(&video->list, &vsp1->videos);
		}
448 449
	}

450 451 452 453 454 455 456 457
	/* Register all subdevs. */
	list_for_each_entry(entity, &vsp1->entities, list_dev) {
		ret = v4l2_device_register_subdev(&vsp1->v4l2_dev,
						  &entity->subdev);
		if (ret < 0)
			goto done;
	}

458
	/*
459 460
	 * Create links and register subdev nodes if the userspace API is
	 * enabled or initialize the DRM pipeline otherwise.
461
	 */
462
	if (vsp1->info->uapi) {
463 464 465 466
		ret = vsp1_uapi_create_links(vsp1);
		if (ret < 0)
			goto done;

467
		ret = v4l2_device_register_subdev_nodes(&vsp1->v4l2_dev);
468 469
		if (ret < 0)
			goto done;
470

471 472 473 474
		ret = media_device_register(mdev);
	} else {
		ret = vsp1_drm_init(vsp1);
	}
475 476 477 478 479 480 481 482

done:
	if (ret < 0)
		vsp1_destroy_entities(vsp1);

	return ret;
}

483
int vsp1_reset_wpf(struct vsp1_device *vsp1, unsigned int index)
484
{
485
	unsigned int timeout;
486 487 488
	u32 status;

	status = vsp1_read(vsp1, VI6_STATUS);
489 490
	if (!(status & VI6_STATUS_SYS_ACT(index)))
		return 0;
491

492 493 494 495 496
	vsp1_write(vsp1, VI6_SRESET, VI6_SRESET_SRTS(index));
	for (timeout = 10; timeout > 0; --timeout) {
		status = vsp1_read(vsp1, VI6_STATUS);
		if (!(status & VI6_STATUS_SYS_ACT(index)))
			break;
497

498 499
		usleep_range(1000, 2000);
	}
500

501 502 503 504
	if (!timeout) {
		dev_err(vsp1->dev, "failed to reset wpf.%u\n", index);
		return -ETIMEDOUT;
	}
505

506 507
	return 0;
}
508

509 510 511 512 513 514
static int vsp1_device_init(struct vsp1_device *vsp1)
{
	unsigned int i;
	int ret;

	/* Reset any channel that might be running. */
515
	for (i = 0; i < vsp1->info->wpf_count; ++i) {
516 517 518
		ret = vsp1_reset_wpf(vsp1, i);
		if (ret < 0)
			return ret;
519 520 521 522 523
	}

	vsp1_write(vsp1, VI6_CLK_DCSWT, (8 << VI6_CLK_DCSWT_CSTPW_SHIFT) |
		   (8 << VI6_CLK_DCSWT_CSTRW_SHIFT));

524
	for (i = 0; i < vsp1->info->rpf_count; ++i)
525 526
		vsp1_write(vsp1, VI6_DPR_RPF_ROUTE(i), VI6_DPR_NODE_UNUSED);

527
	for (i = 0; i < vsp1->info->uds_count; ++i)
528 529
		vsp1_write(vsp1, VI6_DPR_UDS_ROUTE(i), VI6_DPR_NODE_UNUSED);

530 531 532
	for (i = 0; i < vsp1->info->uif_count; ++i)
		vsp1_write(vsp1, VI6_DPR_UIF_ROUTE(i), VI6_DPR_NODE_UNUSED);

533 534 535 536 537 538 539
	vsp1_write(vsp1, VI6_DPR_SRU_ROUTE, VI6_DPR_NODE_UNUSED);
	vsp1_write(vsp1, VI6_DPR_LUT_ROUTE, VI6_DPR_NODE_UNUSED);
	vsp1_write(vsp1, VI6_DPR_CLU_ROUTE, VI6_DPR_NODE_UNUSED);
	vsp1_write(vsp1, VI6_DPR_HST_ROUTE, VI6_DPR_NODE_UNUSED);
	vsp1_write(vsp1, VI6_DPR_HSI_ROUTE, VI6_DPR_NODE_UNUSED);
	vsp1_write(vsp1, VI6_DPR_BRU_ROUTE, VI6_DPR_NODE_UNUSED);

540 541 542
	if (vsp1->info->features & VSP1_HAS_BRS)
		vsp1_write(vsp1, VI6_DPR_ILV_BRS_ROUTE, VI6_DPR_NODE_UNUSED);

543 544 545 546 547
	vsp1_write(vsp1, VI6_DPR_HGO_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
		   (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
	vsp1_write(vsp1, VI6_DPR_HGT_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
		   (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));

548
	vsp1_dlm_setup(vsp1);
549

550 551 552 553 554 555
	return 0;
}

/*
 * vsp1_device_get - Acquire the VSP1 device
 *
556
 * Make sure the device is not suspended and initialize it if needed.
557
 *
558
 * Return 0 on success or a negative error code otherwise.
559
 */
560
int vsp1_device_get(struct vsp1_device *vsp1)
561
{
562
	int ret;
563

564 565
	ret = pm_runtime_get_sync(vsp1->dev);
	return ret < 0 ? ret : 0;
566 567 568 569 570 571 572 573 574 575
}

/*
 * vsp1_device_put - Release the VSP1 device
 *
 * Decrement the VSP1 reference count and cleanup the device if the last
 * reference is released.
 */
void vsp1_device_put(struct vsp1_device *vsp1)
{
576
	pm_runtime_put_sync(vsp1->dev);
577 578 579 580 581 582
}

/* -----------------------------------------------------------------------------
 * Power Management
 */

583
static int __maybe_unused vsp1_pm_suspend(struct device *dev)
584 585 586
{
	struct vsp1_device *vsp1 = dev_get_drvdata(dev);

587 588 589 590 591 592 593
	/*
	 * When used as part of a display pipeline, the VSP is stopped and
	 * restarted explicitly by the DU.
	 */
	if (!vsp1->drm)
		vsp1_pipelines_suspend(vsp1);

594
	pm_runtime_force_suspend(vsp1->dev);
595

596 597
	return 0;
}
598

599
static int __maybe_unused vsp1_pm_resume(struct device *dev)
600 601
{
	struct vsp1_device *vsp1 = dev_get_drvdata(dev);
602

603
	pm_runtime_force_resume(vsp1->dev);
604 605 606 607 608 609 610

	/*
	 * When used as part of a display pipeline, the VSP is stopped and
	 * restarted explicitly by the DU.
	 */
	if (!vsp1->drm)
		vsp1_pipelines_resume(vsp1);
611

612 613 614
	return 0;
}

615
static int __maybe_unused vsp1_pm_runtime_suspend(struct device *dev)
616
{
617 618 619 620
	struct vsp1_device *vsp1 = dev_get_drvdata(dev);

	rcar_fcp_disable(vsp1->fcp);

621 622
	return 0;
}
623

624
static int __maybe_unused vsp1_pm_runtime_resume(struct device *dev)
625 626 627
{
	struct vsp1_device *vsp1 = dev_get_drvdata(dev);
	int ret;
628

629 630
	if (vsp1->info) {
		ret = vsp1_device_init(vsp1);
631
		if (ret < 0)
632 633
			return ret;
	}
634

635
	return rcar_fcp_enable(vsp1->fcp);
636 637 638 639
}

static const struct dev_pm_ops vsp1_pm_ops = {
	SET_SYSTEM_SLEEP_PM_OPS(vsp1_pm_suspend, vsp1_pm_resume)
640
	SET_RUNTIME_PM_OPS(vsp1_pm_runtime_suspend, vsp1_pm_runtime_resume, NULL)
641 642 643 644 645 646
};

/* -----------------------------------------------------------------------------
 * Platform Driver
 */

647 648 649
static const struct vsp1_device_info vsp1_device_infos[] = {
	{
		.version = VI6_IP_VERSION_MODEL_VSPS_H2,
650
		.model = "VSP1-S",
651
		.gen = 2,
652
		.features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_HGO
653 654
			  | VSP1_HAS_HGT | VSP1_HAS_LUT | VSP1_HAS_SRU
			  | VSP1_HAS_WPF_VFLIP,
655 656 657 658 659 660 661
		.rpf_count = 5,
		.uds_count = 3,
		.wpf_count = 4,
		.num_bru_inputs = 4,
		.uapi = true,
	}, {
		.version = VI6_IP_VERSION_MODEL_VSPR_H2,
662
		.model = "VSP1-R",
663
		.gen = 2,
664
		.features = VSP1_HAS_BRU | VSP1_HAS_SRU | VSP1_HAS_WPF_VFLIP,
665
		.rpf_count = 5,
666
		.uds_count = 3,
667 668 669 670 671
		.wpf_count = 4,
		.num_bru_inputs = 4,
		.uapi = true,
	}, {
		.version = VI6_IP_VERSION_MODEL_VSPD_GEN2,
672
		.model = "VSP1-D",
673
		.gen = 2,
674 675
		.features = VSP1_HAS_BRU | VSP1_HAS_HGO | VSP1_HAS_LUT,
		.lif_count = 1,
676 677
		.rpf_count = 4,
		.uds_count = 1,
678
		.wpf_count = 1,
679 680 681 682
		.num_bru_inputs = 4,
		.uapi = true,
	}, {
		.version = VI6_IP_VERSION_MODEL_VSPS_M2,
683
		.model = "VSP1-S",
684
		.gen = 2,
685
		.features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_HGO
686 687
			  | VSP1_HAS_HGT | VSP1_HAS_LUT | VSP1_HAS_SRU
			  | VSP1_HAS_WPF_VFLIP,
688
		.rpf_count = 5,
689
		.uds_count = 1,
690 691 692
		.wpf_count = 4,
		.num_bru_inputs = 4,
		.uapi = true,
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
	}, {
		.version = VI6_IP_VERSION_MODEL_VSPS_V2H,
		.model = "VSP1V-S",
		.gen = 2,
		.features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_LUT
			  | VSP1_HAS_SRU | VSP1_HAS_WPF_VFLIP,
		.rpf_count = 4,
		.uds_count = 1,
		.wpf_count = 4,
		.num_bru_inputs = 4,
		.uapi = true,
	}, {
		.version = VI6_IP_VERSION_MODEL_VSPD_V2H,
		.model = "VSP1V-D",
		.gen = 2,
708 709
		.features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_LUT,
		.lif_count = 1,
710 711 712 713 714
		.rpf_count = 4,
		.uds_count = 1,
		.wpf_count = 1,
		.num_bru_inputs = 4,
		.uapi = true,
715 716
	}, {
		.version = VI6_IP_VERSION_MODEL_VSPI_GEN3,
717
		.model = "VSP2-I",
718
		.gen = 3,
719 720
		.features = VSP1_HAS_CLU | VSP1_HAS_HGO | VSP1_HAS_HGT
			  | VSP1_HAS_LUT | VSP1_HAS_SRU | VSP1_HAS_WPF_HFLIP
721
			  | VSP1_HAS_WPF_VFLIP,
722 723 724 725 726 727
		.rpf_count = 1,
		.uds_count = 1,
		.wpf_count = 1,
		.uapi = true,
	}, {
		.version = VI6_IP_VERSION_MODEL_VSPBD_GEN3,
728
		.model = "VSP2-BD",
729
		.gen = 3,
730
		.features = VSP1_HAS_BRU | VSP1_HAS_WPF_VFLIP,
731 732 733 734 735 736
		.rpf_count = 5,
		.wpf_count = 1,
		.num_bru_inputs = 5,
		.uapi = true,
	}, {
		.version = VI6_IP_VERSION_MODEL_VSPBC_GEN3,
737
		.model = "VSP2-BC",
738
		.gen = 3,
739 740
		.features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_HGO
			  | VSP1_HAS_LUT | VSP1_HAS_WPF_VFLIP,
741 742 743 744
		.rpf_count = 5,
		.wpf_count = 1,
		.num_bru_inputs = 5,
		.uapi = true,
745 746 747 748 749 750 751 752
	}, {
		.version = VI6_IP_VERSION_MODEL_VSPBS_GEN3,
		.model = "VSP2-BS",
		.gen = 3,
		.features = VSP1_HAS_BRS | VSP1_HAS_WPF_VFLIP,
		.rpf_count = 2,
		.wpf_count = 1,
		.uapi = true,
753 754
	}, {
		.version = VI6_IP_VERSION_MODEL_VSPD_GEN3,
755
		.model = "VSP2-D",
756
		.gen = 3,
757 758
		.features = VSP1_HAS_BRU | VSP1_HAS_WPF_VFLIP,
		.lif_count = 1,
759
		.rpf_count = 5,
760
		.uif_count = 1,
761 762
		.wpf_count = 2,
		.num_bru_inputs = 5,
763 764 765 766
	}, {
		.version = VI6_IP_VERSION_MODEL_VSPD_V3,
		.model = "VSP2-D",
		.gen = 3,
767 768
		.features = VSP1_HAS_BRS | VSP1_HAS_BRU,
		.lif_count = 1,
769
		.rpf_count = 5,
770
		.uif_count = 1,
771 772 773 774 775 776
		.wpf_count = 1,
		.num_bru_inputs = 5,
	}, {
		.version = VI6_IP_VERSION_MODEL_VSPDL_GEN3,
		.model = "VSP2-DL",
		.gen = 3,
777 778
		.features = VSP1_HAS_BRS | VSP1_HAS_BRU,
		.lif_count = 2,
779
		.rpf_count = 5,
780
		.uif_count = 2,
781 782
		.wpf_count = 2,
		.num_bru_inputs = 5,
783 784
	},
};
785

786 787 788
static int vsp1_probe(struct platform_device *pdev)
{
	struct vsp1_device *vsp1;
789
	struct device_node *fcp_node;
790 791
	struct resource *irq;
	struct resource *io;
792
	unsigned int i;
793 794 795 796 797 798 799 800
	int ret;

	vsp1 = devm_kzalloc(&pdev->dev, sizeof(*vsp1), GFP_KERNEL);
	if (vsp1 == NULL)
		return -ENOMEM;

	vsp1->dev = &pdev->dev;
	INIT_LIST_HEAD(&vsp1->entities);
801
	INIT_LIST_HEAD(&vsp1->videos);
802

803 804
	platform_set_drvdata(pdev, vsp1);

805
	/* I/O and IRQ resources (clock managed by the clock PM domain) */
806 807
	io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	vsp1->mmio = devm_ioremap_resource(&pdev->dev, io);
808 809
	if (IS_ERR(vsp1->mmio))
		return PTR_ERR(vsp1->mmio);
810 811 812 813 814 815 816 817 818 819 820 821 822 823

	irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	if (!irq) {
		dev_err(&pdev->dev, "missing IRQ\n");
		return -EINVAL;
	}

	ret = devm_request_irq(&pdev->dev, irq->start, vsp1_irq_handler,
			      IRQF_SHARED, dev_name(&pdev->dev), vsp1);
	if (ret < 0) {
		dev_err(&pdev->dev, "failed to request IRQ\n");
		return ret;
	}

824 825 826 827 828 829 830 831 832 833
	/* FCP (optional) */
	fcp_node = of_parse_phandle(pdev->dev.of_node, "renesas,fcp", 0);
	if (fcp_node) {
		vsp1->fcp = rcar_fcp_get(fcp_node);
		of_node_put(fcp_node);
		if (IS_ERR(vsp1->fcp)) {
			dev_dbg(&pdev->dev, "FCP not found (%ld)\n",
				PTR_ERR(vsp1->fcp));
			return PTR_ERR(vsp1->fcp);
		}
834 835 836 837 838 839 840 841 842

		/*
		 * When the FCP is present, it handles all bus master accesses
		 * for the VSP and must thus be used in place of the VSP device
		 * to map DMA buffers.
		 */
		vsp1->bus_master = rcar_fcp_get_device(vsp1->fcp);
	} else {
		vsp1->bus_master = vsp1->dev;
843 844
	}

845
	/* Configure device parameters based on the version register. */
846 847 848
	pm_runtime_enable(&pdev->dev);

	ret = pm_runtime_get_sync(&pdev->dev);
849
	if (ret < 0)
850
		goto done;
851

852
	vsp1->version = vsp1_read(vsp1, VI6_IP_VERSION);
853
	pm_runtime_put_sync(&pdev->dev);
854

855
	for (i = 0; i < ARRAY_SIZE(vsp1_device_infos); ++i) {
856
		if ((vsp1->version & VI6_IP_VERSION_MODEL_MASK) ==
857 858 859 860 861
		    vsp1_device_infos[i].version) {
			vsp1->info = &vsp1_device_infos[i];
			break;
		}
	}
862

863
	if (!vsp1->info) {
864 865
		dev_err(&pdev->dev, "unsupported IP version 0x%08x\n",
			vsp1->version);
866 867
		ret = -ENXIO;
		goto done;
868 869
	}

870
	dev_dbg(&pdev->dev, "IP version 0x%08x\n", vsp1->version);
871

872 873 874 875
	/* Instanciate entities */
	ret = vsp1_create_entities(vsp1);
	if (ret < 0) {
		dev_err(&pdev->dev, "failed to create entities\n");
876
		goto done;
877 878
	}

879 880 881
done:
	if (ret)
		pm_runtime_disable(&pdev->dev);
882

883
	return ret;
884 885 886 887 888 889 890
}

static int vsp1_remove(struct platform_device *pdev)
{
	struct vsp1_device *vsp1 = platform_get_drvdata(pdev);

	vsp1_destroy_entities(vsp1);
891
	rcar_fcp_put(vsp1->fcp);
892

893 894
	pm_runtime_disable(&pdev->dev);

895 896 897
	return 0;
}

898 899
static const struct of_device_id vsp1_of_match[] = {
	{ .compatible = "renesas,vsp1" },
900
	{ .compatible = "renesas,vsp2" },
901 902
	{ },
};
903
MODULE_DEVICE_TABLE(of, vsp1_of_match);
904

905 906 907 908 909 910
static struct platform_driver vsp1_platform_driver = {
	.probe		= vsp1_probe,
	.remove		= vsp1_remove,
	.driver		= {
		.name	= "vsp1",
		.pm	= &vsp1_pm_ops,
911
		.of_match_table = vsp1_of_match,
912 913 914 915 916 917 918 919 920
	},
};

module_platform_driver(vsp1_platform_driver);

MODULE_ALIAS("vsp1");
MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
MODULE_DESCRIPTION("Renesas VSP1 Driver");
MODULE_LICENSE("GPL");