vsp1_drv.c 14.3 KB
Newer Older
1 2 3
/*
 * vsp1_drv.c  --  R-Car VSP1 Driver
 *
4
 * Copyright (C) 2013-2015 Renesas Electronics Corporation
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/module.h>
19
#include <linux/of.h>
20 21 22 23
#include <linux/platform_device.h>
#include <linux/videodev2.h>

#include "vsp1.h"
24
#include "vsp1_bru.h"
25
#include "vsp1_hsit.h"
26
#include "vsp1_lif.h"
27
#include "vsp1_lut.h"
28
#include "vsp1_rwpf.h"
29
#include "vsp1_sru.h"
30
#include "vsp1_uds.h"
31
#include "vsp1_video.h"
32 33 34 35 36 37 38 39 40 41 42 43

/* -----------------------------------------------------------------------------
 * 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;

44
	for (i = 0; i < vsp1->pdata.wpf_count; ++i) {
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
		struct vsp1_rwpf *wpf = vsp1->wpf[i];
		struct vsp1_pipeline *pipe;
		u32 status;

		if (wpf == NULL)
			continue;

		pipe = to_vsp1_pipeline(&wpf->entity.subdev.entity);
		status = vsp1_read(vsp1, VI6_WPF_IRQ_STA(i));
		vsp1_write(vsp1, VI6_WPF_IRQ_STA(i), ~status & mask);

		if (status & VI6_WFP_IRQ_STA_FRE) {
			vsp1_pipeline_frame_end(pipe);
			ret = IRQ_HANDLED;
		}
	}

	return ret;
}

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

/*
 * vsp1_create_links - Create links from all sources to the given sink
 *
 * 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)
 */
static int vsp1_create_links(struct vsp1_device *vsp1, struct vsp1_entity *sink)
{
	struct media_entity *entity = &sink->subdev.entity;
	struct vsp1_entity *source;
	unsigned int pad;
	int ret;

86 87 88 89 90 91
	if (sink->type == VSP1_ENTITY_RPF) {
		struct vsp1_rwpf *rpf = to_rwpf(&sink->subdev);

		/* RPFs have no source entities, just connect their source pad
		 * to their video device.
		 */
92 93
		return media_create_pad_link(&rpf->video->video.entity, 0,
					     &rpf->entity.subdev.entity,
94 95 96 97 98
					     RWPF_PAD_SINK,
					     MEDIA_LNK_FL_ENABLED |
					     MEDIA_LNK_FL_IMMUTABLE);
	}

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
	list_for_each_entry(source, &vsp1->entities, list_dev) {
		u32 flags;

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

		if (source->type == VSP1_ENTITY_LIF ||
		    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 125

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

129 130 131 132 133 134 135 136 137 138 139 140 141
	if (sink->type == VSP1_ENTITY_WPF) {
		struct vsp1_rwpf *wpf = to_rwpf(&sink->subdev);
		unsigned int flags = MEDIA_LNK_FL_ENABLED;

		/* Connect the video device to the WPF. All connections are
		 * immutable except for the WPF0 source link if a LIF is
		 * present.
		 */
		if (!(vsp1->pdata.features & VSP1_HAS_LIF) || sink->index != 0)
			flags |= MEDIA_LNK_FL_IMMUTABLE;

		return media_create_pad_link(&wpf->entity.subdev.entity,
					     RWPF_PAD_SOURCE,
142
					     &wpf->video->video.entity,
143
					     0, flags);
144 145
	}

146 147 148 149 150
	return 0;
}

static void vsp1_destroy_entities(struct vsp1_device *vsp1)
{
151 152
	struct vsp1_entity *entity, *_entity;
	struct vsp1_video *video, *_video;
153

154
	list_for_each_entry_safe(entity, _entity, &vsp1->entities, list_dev) {
155 156 157 158
		list_del(&entity->list_dev);
		vsp1_entity_destroy(entity);
	}

159 160 161 162 163
	list_for_each_entry_safe(video, _video, &vsp1->videos, list) {
		list_del(&video->list);
		vsp1_video_cleanup(video);
	}

164 165
	v4l2_device_unregister(&vsp1->v4l2_dev);
	media_device_unregister(&vsp1->media_dev);
166
	media_device_cleanup(&vsp1->media_dev);
167 168 169 170 171 172 173 174 175 176 177 178
}

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;
	strlcpy(mdev->model, "VSP1", sizeof(mdev->model));
179 180
	snprintf(mdev->bus_info, sizeof(mdev->bus_info), "platform:%s",
		 dev_name(mdev->dev));
181
	media_device_init(mdev);
182 183 184 185 186 187 188 189 190 191

	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. */
192 193 194 195 196 197 198 199
	vsp1->bru = vsp1_bru_create(vsp1);
	if (IS_ERR(vsp1->bru)) {
		ret = PTR_ERR(vsp1->bru);
		goto done;
	}

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

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
	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);

216
	if (vsp1->pdata.features & VSP1_HAS_LIF) {
217 218 219 220 221 222 223 224 225
		vsp1->lif = vsp1_lif_create(vsp1);
		if (IS_ERR(vsp1->lif)) {
			ret = PTR_ERR(vsp1->lif);
			goto done;
		}

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

226
	if (vsp1->pdata.features & VSP1_HAS_LUT) {
227 228 229 230 231 232 233 234 235
		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);
	}

236
	for (i = 0; i < vsp1->pdata.rpf_count; ++i) {
237
		struct vsp1_video *video;
238 239 240 241 242 243 244 245 246 247
		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);
248 249 250 251 252 253 254 255

		video = vsp1_video_create(vsp1, rpf);
		if (IS_ERR(video)) {
			ret = PTR_ERR(video);
			goto done;
		}

		list_add_tail(&video->list, &vsp1->videos);
256 257
	}

258
	if (vsp1->pdata.features & VSP1_HAS_SRU) {
259 260 261 262 263 264 265 266 267
		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);
	}

268
	for (i = 0; i < vsp1->pdata.uds_count; ++i) {
269 270 271 272 273 274 275 276 277 278 279 280
		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);
	}

281
	for (i = 0; i < vsp1->pdata.wpf_count; ++i) {
282
		struct vsp1_video *video;
283 284 285 286 287 288 289 290 291 292
		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);
293 294 295 296 297 298 299 300 301

		video = vsp1_video_create(vsp1, wpf);
		if (IS_ERR(video)) {
			ret = PTR_ERR(video);
			goto done;
		}

		list_add_tail(&video->list, &vsp1->videos);
		wpf->entity.sink = &video->video.entity;
302 303
	}

304 305 306 307 308 309 310 311
	/* 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;
	}

312 313
	/* Create links. */
	list_for_each_entry(entity, &vsp1->entities, list_dev) {
314 315
		if (entity->type == VSP1_ENTITY_LIF)
			continue;
316

317 318 319
		ret = vsp1_create_links(vsp1, entity);
		if (ret < 0)
			goto done;
320 321
	}

322
	if (vsp1->pdata.features & VSP1_HAS_LIF) {
323
		ret = media_create_pad_link(
324 325 326 327 328 329 330
			&vsp1->wpf[0]->entity.subdev.entity, RWPF_PAD_SOURCE,
			&vsp1->lif->entity.subdev.entity, LIF_PAD_SINK, 0);
		if (ret < 0)
			return ret;
	}

	ret = v4l2_device_register_subdev_nodes(&vsp1->v4l2_dev);
331 332 333 334
	if (ret < 0)
		goto done;

	ret = media_device_register(mdev);
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350

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

	return ret;
}

static int vsp1_device_init(struct vsp1_device *vsp1)
{
	unsigned int i;
	u32 status;

	/* Reset any channel that might be running. */
	status = vsp1_read(vsp1, VI6_STATUS);

351
	for (i = 0; i < vsp1->pdata.wpf_count; ++i) {
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
		unsigned int timeout;

		if (!(status & VI6_STATUS_SYS_ACT(i)))
			continue;

		vsp1_write(vsp1, VI6_SRESET, VI6_SRESET_SRTS(i));
		for (timeout = 10; timeout > 0; --timeout) {
			status = vsp1_read(vsp1, VI6_STATUS);
			if (!(status & VI6_STATUS_SYS_ACT(i)))
				break;

			usleep_range(1000, 2000);
		}

		if (!timeout) {
			dev_err(vsp1->dev, "failed to reset wpf.%u\n", i);
			return -ETIMEDOUT;
		}
	}

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

375
	for (i = 0; i < vsp1->pdata.rpf_count; ++i)
376 377
		vsp1_write(vsp1, VI6_DPR_RPF_ROUTE(i), VI6_DPR_NODE_UNUSED);

378
	for (i = 0; i < vsp1->pdata.uds_count; ++i)
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
		vsp1_write(vsp1, VI6_DPR_UDS_ROUTE(i), VI6_DPR_NODE_UNUSED);

	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);

	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));

	return 0;
}

/*
 * vsp1_device_get - Acquire the VSP1 device
 *
 * Increment the VSP1 reference count and initialize the device if the first
 * reference is taken.
 *
402
 * Return 0 on success or a negative error code otherwise.
403
 */
404
int vsp1_device_get(struct vsp1_device *vsp1)
405
{
406
	int ret = 0;
407 408 409 410 411

	mutex_lock(&vsp1->lock);
	if (vsp1->ref_count > 0)
		goto done;

412
	ret = clk_prepare_enable(vsp1->clock);
413
	if (ret < 0)
414 415 416 417
		goto done;

	ret = vsp1_device_init(vsp1);
	if (ret < 0) {
418
		clk_disable_unprepare(vsp1->clock);
419 420 421 422
		goto done;
	}

done:
423
	if (!ret)
424 425 426
		vsp1->ref_count++;

	mutex_unlock(&vsp1->lock);
427
	return ret;
428 429 430 431 432 433 434 435 436 437 438 439 440
}

/*
 * 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)
{
	mutex_lock(&vsp1->lock);

	if (--vsp1->ref_count == 0)
441
		clk_disable_unprepare(vsp1->clock);
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459

	mutex_unlock(&vsp1->lock);
}

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

#ifdef CONFIG_PM_SLEEP
static int vsp1_pm_suspend(struct device *dev)
{
	struct vsp1_device *vsp1 = dev_get_drvdata(dev);

	WARN_ON(mutex_is_locked(&vsp1->lock));

	if (vsp1->ref_count == 0)
		return 0;

460 461
	vsp1_pipelines_suspend(vsp1);

462
	clk_disable_unprepare(vsp1->clock);
463

464 465 466 467 468 469 470 471 472
	return 0;
}

static int vsp1_pm_resume(struct device *dev)
{
	struct vsp1_device *vsp1 = dev_get_drvdata(dev);

	WARN_ON(mutex_is_locked(&vsp1->lock));

473
	if (vsp1->ref_count == 0)
474 475
		return 0;

476 477 478 479 480
	clk_prepare_enable(vsp1->clock);

	vsp1_pipelines_resume(vsp1);

	return 0;
481 482 483 484 485 486 487 488 489 490 491
}
#endif

static const struct dev_pm_ops vsp1_pm_ops = {
	SET_SYSTEM_SLEEP_PM_OPS(vsp1_pm_suspend, vsp1_pm_resume)
};

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

492
static int vsp1_parse_dt(struct vsp1_device *vsp1)
493
{
494 495 496 497 498 499 500 501 502 503 504 505 506
	struct device_node *np = vsp1->dev->of_node;
	struct vsp1_platform_data *pdata = &vsp1->pdata;

	if (of_property_read_bool(np, "renesas,has-lif"))
		pdata->features |= VSP1_HAS_LIF;
	if (of_property_read_bool(np, "renesas,has-lut"))
		pdata->features |= VSP1_HAS_LUT;
	if (of_property_read_bool(np, "renesas,has-sru"))
		pdata->features |= VSP1_HAS_SRU;

	of_property_read_u32(np, "renesas,#rpf", &pdata->rpf_count);
	of_property_read_u32(np, "renesas,#uds", &pdata->uds_count);
	of_property_read_u32(np, "renesas,#wpf", &pdata->wpf_count);
507

L
Laurent Pinchart 已提交
508
	if (pdata->rpf_count <= 0 || pdata->rpf_count > VSP1_MAX_RPF) {
509
		dev_err(vsp1->dev, "invalid number of RPF (%u)\n",
510
			pdata->rpf_count);
511
		return -EINVAL;
512 513
	}

514
	if (pdata->uds_count > VSP1_MAX_UDS) {
515
		dev_err(vsp1->dev, "invalid number of UDS (%u)\n",
516
			pdata->uds_count);
517
		return -EINVAL;
518 519
	}

L
Laurent Pinchart 已提交
520
	if (pdata->wpf_count <= 0 || pdata->wpf_count > VSP1_MAX_WPF) {
521
		dev_err(vsp1->dev, "invalid number of WPF (%u)\n",
522
			pdata->wpf_count);
523
		return -EINVAL;
524 525
	}

526 527 528
	return 0;
}

529 530 531 532 533 534 535 536 537 538 539 540 541 542
static int vsp1_probe(struct platform_device *pdev)
{
	struct vsp1_device *vsp1;
	struct resource *irq;
	struct resource *io;
	int ret;

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

	vsp1->dev = &pdev->dev;
	mutex_init(&vsp1->lock);
	INIT_LIST_HEAD(&vsp1->entities);
543
	INIT_LIST_HEAD(&vsp1->videos);
544

545
	ret = vsp1_parse_dt(vsp1);
546 547 548
	if (ret < 0)
		return ret;

549 550 551
	/* I/O, IRQ and clock resources */
	io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	vsp1->mmio = devm_ioremap_resource(&pdev->dev, io);
552 553
	if (IS_ERR(vsp1->mmio))
		return PTR_ERR(vsp1->mmio);
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594

	vsp1->clock = devm_clk_get(&pdev->dev, NULL);
	if (IS_ERR(vsp1->clock)) {
		dev_err(&pdev->dev, "failed to get clock\n");
		return PTR_ERR(vsp1->clock);
	}

	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;
	}

	/* Instanciate entities */
	ret = vsp1_create_entities(vsp1);
	if (ret < 0) {
		dev_err(&pdev->dev, "failed to create entities\n");
		return ret;
	}

	platform_set_drvdata(pdev, vsp1);

	return 0;
}

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

	vsp1_destroy_entities(vsp1);

	return 0;
}

595 596 597 598 599
static const struct of_device_id vsp1_of_match[] = {
	{ .compatible = "renesas,vsp1" },
	{ },
};

600 601 602 603 604 605
static struct platform_driver vsp1_platform_driver = {
	.probe		= vsp1_probe,
	.remove		= vsp1_remove,
	.driver		= {
		.name	= "vsp1",
		.pm	= &vsp1_pm_ops,
606
		.of_match_table = vsp1_of_match,
607 608 609 610 611 612 613 614 615
	},
};

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");